Skip to content

[MLIR] RemoveDeadValues: Allowing IRs with global constants to get dead values removed #116519

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 22, 2024

Conversation

parsifal-47
Copy link
Contributor

This change is related to discussion:
https://discourse.llvm.org/t/question-on-criteria-for-acceptable-ir-in-removedeadvaluespass/83131

I do not know the original reason to disallow the optimization on modules with global private constant. Please let me know what am I missing, I will be happy to make it better. Thank you!

CC: @Wheest

Copy link

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot llvmbot added mlir:core MLIR Core Infrastructure mlir labels Nov 17, 2024
@llvmbot
Copy link
Member

llvmbot commented Nov 17, 2024

@llvm/pr-subscribers-mlir

@llvm/pr-subscribers-mlir-core

Author: Renat Idrisov (parsifal-47)

Changes

This change is related to discussion:
https://discourse.llvm.org/t/question-on-criteria-for-acceptable-ir-in-removedeadvaluespass/83131

I do not know the original reason to disallow the optimization on modules with global private constant. Please let me know what am I missing, I will be happy to make it better. Thank you!

CC: @Wheest


Full diff: https://github.com/llvm/llvm-project/pull/116519.diff

2 Files Affected:

  • (modified) mlir/lib/Transforms/RemoveDeadValues.cpp (-1)
  • (modified) mlir/test/Transforms/remove-dead-values.mlir (+3-3)
diff --git a/mlir/lib/Transforms/RemoveDeadValues.cpp b/mlir/lib/Transforms/RemoveDeadValues.cpp
index 7e45f18b660ba7..edfb1969ab75c7 100644
--- a/mlir/lib/Transforms/RemoveDeadValues.cpp
+++ b/mlir/lib/Transforms/RemoveDeadValues.cpp
@@ -579,7 +579,6 @@ void RemoveDeadValues::runOnOperation() {
     if (op == module)
       return WalkResult::advance();
     if (isa<BranchOpInterface>(op) ||
-        (isa<SymbolOpInterface>(op) && !isa<FunctionOpInterface>(op)) ||
         (isa<SymbolUserOpInterface>(op) && !isa<CallOpInterface>(op))) {
       op->emitError() << "cannot optimize an IR with non-function symbol ops, "
                          "non-call symbol user ops or branch ops\n";
diff --git a/mlir/test/Transforms/remove-dead-values.mlir b/mlir/test/Transforms/remove-dead-values.mlir
index 9f2be3331b6b4b..b06b3320706cc8 100644
--- a/mlir/test/Transforms/remove-dead-values.mlir
+++ b/mlir/test/Transforms/remove-dead-values.mlir
@@ -1,12 +1,12 @@
 // RUN: mlir-opt %s -remove-dead-values -split-input-file -verify-diagnostics | FileCheck %s
 
-// The IR remains untouched because of the presence of a non-function-like
-// symbol op inside the module (const @__dont_touch_unacceptable_ir).
+// The IR is updated regardless of memref.global private constant
 //
 module {
-// expected-error @+1 {{cannot optimize an IR with non-function symbol ops, non-call symbol user ops or branch ops}}
   memref.global "private" constant @__dont_touch_unacceptable_ir : memref<i32> = dense<0>
   func.func @main(%arg0: i32) -> i32 {
+    %0 = tensor.empty() : tensor<10xbf16>
+    // CHECK-NOT: tensor.empty
     return %arg0 : i32
   }
 }

Copy link

⚠️ We detected that you are using a GitHub private e-mail address to contribute to the repo.
Please turn off Keep my email addresses private setting in your account.
See LLVM Discourse for more information.

@codemzs
Copy link
Contributor

codemzs commented Nov 17, 2024

@srishti-pm Can you please review this change? I am curious why the IR is not considered acceptable if it contains something like memref.global op?

Copy link
Contributor

@Wheest Wheest left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From my perspective it should be fine if IR with named symbols like constants use this pass (my recent PR did this for named modules #109990).

A justification for keeping the existing behaviour could be: "that which has a name should be handled more carefully, and not removed unless specifically requested."

However, since this is a blocker for some of my own work, I don't support it, and think this PR should go through.

That being said, I could imagine adding this behaviour back in with --remove-dead-values="remove-user-symbols"

@@ -579,7 +579,6 @@ void RemoveDeadValues::runOnOperation() {
if (op == module)
return WalkResult::advance();
if (isa<BranchOpInterface>(op) ||
(isa<SymbolOpInterface>(op) && !isa<FunctionOpInterface>(op)) ||
(isa<SymbolUserOpInterface>(op) && !isa<CallOpInterface>(op))) {
op->emitError() << "cannot optimize an IR with non-function symbol ops, "
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So the original comment above says:

  // The removal of non-live values is performed iff there are no branch ops,
  // all symbol ops present in the IR are function-like, and all symbol user ops
  // present in the IR are call-like.

and the error message includes non-call symbol user ops. With this PR I believe that IR with non-call symbol user ops are no longer excluded?

Perhaps the error message/comment should be updated to reflect that.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated both the comment and error message, thank you!

//
module {
// expected-error @+1 {{cannot optimize an IR with non-function symbol ops, non-call symbol user ops or branch ops}}
memref.global "private" constant @__dont_touch_unacceptable_ir : memref<i32> = dense<0>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is @__dont_touch_unacceptable_ir still a meaningful name here, since we expect the IR to be updated?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thank you, done!

@River707
Copy link
Contributor

From my perspective I'd love to see all of the acceptableIR checks removed completely. This passes presents itself as some general aggressive DCE, but is currently only applicable in a very limited number of situations. I'd support any form of additions that move it closer towards a more general pass.

@codemzs
Copy link
Contributor

codemzs commented Nov 19, 2024

@River707 @Wheest Who is the right person to ask to commit this change?

@codemzs
Copy link
Contributor

codemzs commented Nov 20, 2024

Hello @joker-eph

It appears that you have recently reviewed and committed changes to this file. Would it be possible for you to review this change?

WalkResult acceptableIR = module->walk([&](Operation *op) {
if (op == module)
return WalkResult::advance();
if (isa<BranchOpInterface>(op) ||
(isa<SymbolOpInterface>(op) && !isa<FunctionOpInterface>(op)) ||
(isa<SymbolUserOpInterface>(op) && !isa<CallOpInterface>(op))) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This last check is also not clear to me. But I'm not sure if this has all to do with some aspects of the dataflow framework?

Copy link
Collaborator

@joker-eph joker-eph left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not clear why this check is in-place (unfortunately the comment documents the "what" and not the "why"...).

In any case we could tighten this back with motivating examples, if they show up, and I suspect the checks could be more targeted: for example instead of disabling the pass entirely, we could disable it on a per-function basis.

@codemzs
Copy link
Contributor

codemzs commented Nov 22, 2024

It's not clear why this check is in-place (unfortunately the comment documents the "what" and not the "why"...).

In any case we could tighten this back with motivating examples, if they show up, and I suspect the checks could be more targeted: for example instead of disabling the pass entirely, we could disable it on a per-function basis.

@joker-eph I was curious if there is anything else needed for this change to be committed?

@joker-eph
Copy link
Collaborator

It is approved, and the author has commit access, so I didn't feel like committing in their place. @parsifal-47 what's up?

@parsifal-47
Copy link
Contributor Author

hmm, I see the following:
"This branch has no conflicts with the base branch
Only those with write access to this repository can merge pull requests."

@parsifal-47
Copy link
Contributor Author

sorry, maybe I do not understand the workflow, the PR is approved, should I look for motivating examples?
I was thinking that we are waiting for all potentially interested people to see it before it gets merged.

@codemzs
Copy link
Contributor

codemzs commented Nov 22, 2024

It is approved, and the author has commit access, so I didn't feel like committing in their place. @parsifal-47 what's up?

@joker-eph Based on my offline conversation with @parsifal-47 it appears he lacks write access. Would it be possible for you to commit and merge this change into llvm:main? Thank you.

@sstamenova sstamenova merged commit 24ced77 into llvm:main Nov 22, 2024
8 checks passed
Copy link

@parsifal-47 Congratulations on having your first Pull Request (PR) merged into the LLVM Project!

Your changes will be combined with recent changes from other authors, then tested by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR.

Please check whether problems have been caused by your change specifically, as the builds can include changes from many authors. It is not uncommon for your change to be included in a build that fails due to someone else's changes, or infrastructure issues.

How to do this, and the rest of the post-merge process, is covered in detail here.

If your change does cause a problem, it may be reverted, or you can revert it yourself. This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again.

If you don't get any reports, no action is required from you. Your changes are working as expected, well done!

joker-eph pushed a commit that referenced this pull request Nov 23, 2024
…Pass (#117405)

This change removes the restriction on `SymbolUserOpInterface` operators
so they can be used with operators that implement `SymbolOpInterface`,
example:

`memref.global` implements `SymbolOpInterface` so it can be used with
`memref.get_global` which implements `SymbolUserOpInterface`

```
// Define a global constant array
memref.global "private" constant @global_array : memref<10xi32> = dense<[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]> : tensor<10xi32>

// Access this global constant within a function
func @use_global() {
  %0 = memref.get_global @global_array : memref<10xi32>
}
```

Reference: #116519 and
https://discourse.llvm.org/t/question-on-criteria-for-acceptable-ir-in-removedeadvaluespass/83131

---------

Co-authored-by: Zeeshan Siddiqui <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
mlir:core MLIR Core Infrastructure mlir
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants