-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[mlir][MemRef] Changed AssumeAlignment into a Pure ViewLikeOp #139521
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
[mlir][MemRef] Changed AssumeAlignment into a Pure ViewLikeOp #139521
Conversation
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 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. |
@llvm/pr-subscribers-mlir-gpu @llvm/pr-subscribers-mlir Author: Shay Kleiman (shay-kl) ChangesAssume_alignment has no trait which specifies how it interacts with memory, this causes an issue in OwnershipBasedBufferDeallocation, which require all operations which operate on buffers to have explicit traits defining how the operation interacts with memory. From my understanding, technically the operation is pure, however to make sure the operation doesn't get optimized away it has to have some side effect. I defined it to have similar side effects to CF AssertOp as both are asserts and assume_alignment gets lowered to it in RuntimeOpVerification. I'm not sure if this is correct and would appreciate the opinion of someone more experienced. Full diff: https://github.com/llvm/llvm-project/pull/139521.diff 2 Files Affected:
diff --git a/mlir/include/mlir/Dialect/MemRef/IR/MemRefOps.td b/mlir/include/mlir/Dialect/MemRef/IR/MemRefOps.td
index d6d8161d3117b..856b033f401a0 100644
--- a/mlir/include/mlir/Dialect/MemRef/IR/MemRefOps.td
+++ b/mlir/include/mlir/Dialect/MemRef/IR/MemRefOps.td
@@ -142,7 +142,7 @@ class AllocLikeOp<string mnemonic,
// AssumeAlignmentOp
//===----------------------------------------------------------------------===//
-def AssumeAlignmentOp : MemRef_Op<"assume_alignment"> {
+def AssumeAlignmentOp : MemRef_Op<"assume_alignment",[DeclareOpInterfaceMethods<MemoryEffectsOpInterface>]> {
let summary =
"assertion that gives alignment information to the input memref";
let description = [{
diff --git a/mlir/lib/Dialect/MemRef/IR/MemRefOps.cpp b/mlir/lib/Dialect/MemRef/IR/MemRefOps.cpp
index a0237c18cf2fe..1872a63f93c93 100644
--- a/mlir/lib/Dialect/MemRef/IR/MemRefOps.cpp
+++ b/mlir/lib/Dialect/MemRef/IR/MemRefOps.cpp
@@ -526,6 +526,11 @@ LogicalResult AssumeAlignmentOp::verify() {
return emitOpError("alignment must be power of 2");
return success();
}
+void AssumeAlignmentOp::getEffects(
+ SmallVectorImpl<SideEffects::EffectInstance<MemoryEffects::Effect>>
+ &effects) {
+effects.emplace_back(MemoryEffects::Write::get());
+}
//===----------------------------------------------------------------------===//
// CastOp
|
@llvm/pr-subscribers-mlir-memref Author: Shay Kleiman (shay-kl) ChangesAssume_alignment has no trait which specifies how it interacts with memory, this causes an issue in OwnershipBasedBufferDeallocation, which require all operations which operate on buffers to have explicit traits defining how the operation interacts with memory. From my understanding, technically the operation is pure, however to make sure the operation doesn't get optimized away it has to have some side effect. I defined it to have similar side effects to CF AssertOp as both are asserts and assume_alignment gets lowered to it in RuntimeOpVerification. I'm not sure if this is correct and would appreciate the opinion of someone more experienced. Full diff: https://github.com/llvm/llvm-project/pull/139521.diff 2 Files Affected:
diff --git a/mlir/include/mlir/Dialect/MemRef/IR/MemRefOps.td b/mlir/include/mlir/Dialect/MemRef/IR/MemRefOps.td
index d6d8161d3117b..856b033f401a0 100644
--- a/mlir/include/mlir/Dialect/MemRef/IR/MemRefOps.td
+++ b/mlir/include/mlir/Dialect/MemRef/IR/MemRefOps.td
@@ -142,7 +142,7 @@ class AllocLikeOp<string mnemonic,
// AssumeAlignmentOp
//===----------------------------------------------------------------------===//
-def AssumeAlignmentOp : MemRef_Op<"assume_alignment"> {
+def AssumeAlignmentOp : MemRef_Op<"assume_alignment",[DeclareOpInterfaceMethods<MemoryEffectsOpInterface>]> {
let summary =
"assertion that gives alignment information to the input memref";
let description = [{
diff --git a/mlir/lib/Dialect/MemRef/IR/MemRefOps.cpp b/mlir/lib/Dialect/MemRef/IR/MemRefOps.cpp
index a0237c18cf2fe..1872a63f93c93 100644
--- a/mlir/lib/Dialect/MemRef/IR/MemRefOps.cpp
+++ b/mlir/lib/Dialect/MemRef/IR/MemRefOps.cpp
@@ -526,6 +526,11 @@ LogicalResult AssumeAlignmentOp::verify() {
return emitOpError("alignment must be power of 2");
return success();
}
+void AssumeAlignmentOp::getEffects(
+ SmallVectorImpl<SideEffects::EffectInstance<MemoryEffects::Effect>>
+ &effects) {
+effects.emplace_back(MemoryEffects::Write::get());
+}
//===----------------------------------------------------------------------===//
// CastOp
|
I'm not quite sure how we handle undefined behavior and side effects in this case. Maybe @joker-eph or @kuhar have some opinion. |
4c694da
to
3aae81c
Compare
Assume_alignment has no trait which specifies how it interacts with memory, this causes an issue in OwnershipBasedBufferDeallocation, which require all operations which operate on buffers to have explicit traits defining how the operation interacts with memory. From my understanding, technically the operation is pure, however to make sure the operation doesn't get optimized away it has to have some side effect. I defined it to have similar side effects to CF AssertOp as both are asserts. I'm not sure if this is correct and would appreciate the opinion of someone more experienced.
3aae81c
to
bddbbe9
Compare
I added a lit test to demonstrate the issue with the operation not passing OwnershipBasedBufferDeallocation. |
NoMemoryEffect means no side effects beyond undefined behavior, and Pure means no side-effects and no UB. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Saying it has write semantics is a big pessimization. This is more a compiler hint. If an optimization is removing it, that needs to be fixed in the optimization AFAICS. For example CSE does no CSE operation with no results (even if it has no side-effects).
@shay-kl Is the buffer deallocation pass happy if you use |
@matthias-springer |
@MaheshRavishankar |
I see... The canonicalizer pass would then also remove the op because it is "trivially dead". This is not just a CSE problem. I'd be in favor of giving the op a result and |
@matthias-springer and from now on we use res2 instead of res1? |
Right now without any traits, it has "unknown effect" which is most conservative situation (it has to be read and write to anything). Using dataflow is a cleaner design. |
Made AssumeAlignment a ViewLikeOp that returns a new SSA memref equal to its memref argument and made it have NoMemoryEffect trait. This gives it a defined memory effect that matches what it does in practice and makes it behave nicely with optimizations which won't get rid of it unless its result isn't being used.
@matthias-springer |
Can you update the title and description? |
Assume Alignment is now pure with its ub now deferred with poison
Set AsmResultName for the operation to be assume_align
LGTM but let's wait for others to double check again. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
THis seems good to me, but I dont fully follow why the ViewLikeOpInterface
here?
Dismissing my review cause the main blocker that I had is resolved. Still have question(s) though.
I assumed that an identity operation would count as a view, and that since |
637364b
to
21bb4fd
Compare
✅ With the latest revision this PR passed the C/C++ code formatter. |
21bb4fd
to
9a4e9be
Compare
Changed assertion to assumption and improved description of poison
9a4e9be
to
1c158c1
Compare
I don't have permissions, can you please land it? |
@shay-kl 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! |
LLVM Buildbot has detected a new failure on builder Full details are available at: https://lab.llvm.org/buildbot/#/builders/138/builds/13299 Here is the relevant piece of the build log for the reference
|
Fixed failing lit test here: #140447 |
…llvm#139521)" This reverts commit ffb9bbf.
New Revert: llvm/llvm-project#139521 will be fixed in a followup as it is a large change Currently, it carries 4 reverts: - iree-org/llvm-project@13631cf - iree-org/llvm-project@0357fd9 - iree-org/llvm-project@20a9a98 - iree-org/llvm-project@435bb50
…39521) Made AssumeAlignment a ViewLikeOp that returns a new SSA memref equal to its memref argument and made it have Pure trait. This gives it a defined memory effect that matches what it does in practice and makes it behave nicely with optimizations which won't get rid of it unless its result isn't being used.
Made AssumeAlignment a ViewLikeOp that returns a new SSA memref equal
to its memref argument and made it have Pure trait. This
gives it a defined memory effect that matches what it does in practice
and makes it behave nicely with optimizations which won't get rid of it
unless its result isn't being used.