-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[MLIR][buffer-deallocation] Introduce copies only for MemRef typed values. #121582
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
base: main
Are you sure you want to change the base?
[MLIR][buffer-deallocation] Introduce copies only for MemRef typed values. #121582
Conversation
@llvm/pr-subscribers-mlir Author: None (erick-xanadu) ChangesHello, I know that the buffer-deallocation pass is deprecated, but just in case anyone else is still relying on it, we found this issue with buffer-deallocation. The following test case segfaults on main: func.func public @<!-- -->jit_main() {
%c0 = arith.constant 0 : i1
%4 = scf.while (%arg1 = %c0) : (i1) -> (i1) {
scf.condition(%c0) %arg1 : i1
} do {
^bb0(%arg1: i1):
%alloc_1 = memref.alloc() {alignment = 64 : i64} : memref<i32>
%7 = func.call @<!-- -->jitted_fun_0(%alloc_1, %arg1) : (memref<i32>, i1) -> (i1)
scf.yield %7#<!-- -->0: i1
}
return
}
func.func private @<!-- -->jitted_fun_0(%arg1: memref<i32>, %arg2: i1) -> (i1) {
return %arg2 : i1
} It looks like it is attempting to introduce a clone for the i1. With the changes submitted, I believe the error is corrected. Happy to improve the test. It would be nice if this is still merged (if deemed a fix) despite buffer-deallocation being deprecated as other people may encounter this. Thanks, and happy to make changes to improve this fix. Full diff: https://github.com/llvm/llvm-project/pull/121582.diff 2 Files Affected:
diff --git a/mlir/lib/Dialect/Bufferization/Transforms/BufferDeallocation.cpp b/mlir/lib/Dialect/Bufferization/Transforms/BufferDeallocation.cpp
index a0a81d4add7121..36edbb3395eddf 100644
--- a/mlir/lib/Dialect/Bufferization/Transforms/BufferDeallocation.cpp
+++ b/mlir/lib/Dialect/Bufferization/Transforms/BufferDeallocation.cpp
@@ -308,6 +308,9 @@ class BufferDeallocation : public BufferPlacementTransformationBase {
// Add new allocs and additional clone operations.
for (Value value : valuesToFree) {
+ if (!isa<BaseMemRefType>(value.getType())) {
+ continue;
+ }
if (failed(isa<BlockArgument>(value)
? introduceBlockArgCopy(cast<BlockArgument>(value))
: introduceValueCopyForRegionResult(value)))
diff --git a/mlir/test/Dialect/Bufferization/Transforms/buffer-deallocation.mlir b/mlir/test/Dialect/Bufferization/Transforms/buffer-deallocation.mlir
index 3fbe3913c6549e..a6e6f724a822ac 100644
--- a/mlir/test/Dialect/Bufferization/Transforms/buffer-deallocation.mlir
+++ b/mlir/test/Dialect/Bufferization/Transforms/buffer-deallocation.mlir
@@ -1271,6 +1271,27 @@ func.func @while_two_arg(%arg0: index) {
// -----
+// CHECK-LABEL: func @while_fun
+func.func @while_fun() {
+ %c0 = arith.constant 0 : i1
+ %4 = scf.while (%arg1 = %c0) : (i1) -> (i1) {
+ scf.condition(%c0) %arg1 : i1
+ } do {
+ ^bb0(%arg1: i1):
+ %alloc_1 = memref.alloc() {alignment = 64 : i64} : memref<i32>
+ %7 = func.call @foo(%alloc_1, %arg1) : (memref<i32>, i1) -> (i1)
+ scf.yield %7#0: i1
+ }
+ return
+}
+
+func.func private @foo(%arg1: memref<i32>, %arg2: i1) -> (i1) {
+ return %arg2 : i1
+}
+
+// -----
+
+// CHECK-LABEL: func @while_three_arg
func.func @while_three_arg(%arg0: index) {
// CHECK: %[[ALLOC:.*]] = memref.alloc
%a = memref.alloc(%arg0) : memref<?xf32>
|
@llvm/pr-subscribers-mlir-bufferization Author: None (erick-xanadu) ChangesHello, I know that the buffer-deallocation pass is deprecated, but just in case anyone else is still relying on it, we found this issue with buffer-deallocation. The following test case segfaults on main: func.func public @<!-- -->jit_main() {
%c0 = arith.constant 0 : i1
%4 = scf.while (%arg1 = %c0) : (i1) -> (i1) {
scf.condition(%c0) %arg1 : i1
} do {
^bb0(%arg1: i1):
%alloc_1 = memref.alloc() {alignment = 64 : i64} : memref<i32>
%7 = func.call @<!-- -->jitted_fun_0(%alloc_1, %arg1) : (memref<i32>, i1) -> (i1)
scf.yield %7#<!-- -->0: i1
}
return
}
func.func private @<!-- -->jitted_fun_0(%arg1: memref<i32>, %arg2: i1) -> (i1) {
return %arg2 : i1
} It looks like it is attempting to introduce a clone for the i1. With the changes submitted, I believe the error is corrected. Happy to improve the test. It would be nice if this is still merged (if deemed a fix) despite buffer-deallocation being deprecated as other people may encounter this. Thanks, and happy to make changes to improve this fix. Full diff: https://github.com/llvm/llvm-project/pull/121582.diff 2 Files Affected:
diff --git a/mlir/lib/Dialect/Bufferization/Transforms/BufferDeallocation.cpp b/mlir/lib/Dialect/Bufferization/Transforms/BufferDeallocation.cpp
index a0a81d4add7121..36edbb3395eddf 100644
--- a/mlir/lib/Dialect/Bufferization/Transforms/BufferDeallocation.cpp
+++ b/mlir/lib/Dialect/Bufferization/Transforms/BufferDeallocation.cpp
@@ -308,6 +308,9 @@ class BufferDeallocation : public BufferPlacementTransformationBase {
// Add new allocs and additional clone operations.
for (Value value : valuesToFree) {
+ if (!isa<BaseMemRefType>(value.getType())) {
+ continue;
+ }
if (failed(isa<BlockArgument>(value)
? introduceBlockArgCopy(cast<BlockArgument>(value))
: introduceValueCopyForRegionResult(value)))
diff --git a/mlir/test/Dialect/Bufferization/Transforms/buffer-deallocation.mlir b/mlir/test/Dialect/Bufferization/Transforms/buffer-deallocation.mlir
index 3fbe3913c6549e..a6e6f724a822ac 100644
--- a/mlir/test/Dialect/Bufferization/Transforms/buffer-deallocation.mlir
+++ b/mlir/test/Dialect/Bufferization/Transforms/buffer-deallocation.mlir
@@ -1271,6 +1271,27 @@ func.func @while_two_arg(%arg0: index) {
// -----
+// CHECK-LABEL: func @while_fun
+func.func @while_fun() {
+ %c0 = arith.constant 0 : i1
+ %4 = scf.while (%arg1 = %c0) : (i1) -> (i1) {
+ scf.condition(%c0) %arg1 : i1
+ } do {
+ ^bb0(%arg1: i1):
+ %alloc_1 = memref.alloc() {alignment = 64 : i64} : memref<i32>
+ %7 = func.call @foo(%alloc_1, %arg1) : (memref<i32>, i1) -> (i1)
+ scf.yield %7#0: i1
+ }
+ return
+}
+
+func.func private @foo(%arg1: memref<i32>, %arg2: i1) -> (i1) {
+ return %arg2 : i1
+}
+
+// -----
+
+// CHECK-LABEL: func @while_three_arg
func.func @while_three_arg(%arg0: index) {
// CHECK: %[[ALLOC:.*]] = memref.alloc
%a = memref.alloc(%arg0) : memref<?xf32>
|
✅ With the latest revision this PR passed the C/C++ code formatter. |
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.
Are you using this pass a lot? I wanted to delete this pass from MLIR soon.
@@ -308,6 +308,9 @@ class BufferDeallocation : public BufferPlacementTransformationBase { | |||
|
|||
// Add new allocs and additional clone operations. | |||
for (Value value : valuesToFree) { | |||
if (!isa<BaseMemRefType>(value.getType())) { |
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.
Can you put the check earlier, so that non-memref values are not even added to valuesToFree
?
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.
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.
Hi @matthias-springer, I implemented the change but I noticed that my test case failed. If I do this, it appears that a deallocation will be placed in the block following the scf.while
op. This means that the original test fails because the memref.alloc
inside the scf.while
body does not dominate the memref.dealloc
operation that will be inserted. I think that no memref.dealloc
should be inserted though (but I am not sure) because it is passed to a function which means that the allocated pointer may be stored on the heap or elsewhere. I'll try to take a closer look, but if you have a quick hunch about what can be wrong, please let me know. :)
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.
I'm not too familiar with this pass unfortunately...
Thanks for the review @matthias-springer ! We've been meaning to update to the new bufferization infrastructure although we haven't completed the migration yet (currently we are indeed relying on this pass heavily). Is the new deallocation pass useable without one-shot bufferization? |
Yes, the new deallocation pass ( |
…ation (#1408) **Context:** This patch fixes a bug in upstream MLIR that prevents buffer deallocation from working in certain cases. The patch was also submitted upstream: llvm/llvm-project#121582 **Description of the Change:** This PR adds a patch to the llvm-project source, and updates the relevant build recipes. The llvm step during wheel builds is now also handled through Make instead of replicating the CMake configuration in each wheel script. **Benefits:** Unblocks our friends over at Qrisp. **Possible Drawbacks:** Patching dependencies is not great, but hopefully temporary. **Related GitHub Issues:** [sc-81444]
…ation (#1408) **Context:** This patch fixes a bug in upstream MLIR that prevents buffer deallocation from working in certain cases. The patch was also submitted upstream: llvm/llvm-project#121582 **Description of the Change:** This PR adds a patch to the llvm-project source, and updates the relevant build recipes. The llvm step during wheel builds is now also handled through Make instead of replicating the CMake configuration in each wheel script. **Benefits:** Unblocks our friends over at Qrisp. **Possible Drawbacks:** Patching dependencies is not great, but hopefully temporary. **Related GitHub Issues:** [sc-81444]
Yes, but we are planning on updating soon. Thanks! |
Hello,
I know that the buffer-deallocation pass is deprecated, but just in case anyone else is still relying on it, we found this issue with buffer-deallocation. The following test case segfaults on main:
It looks like it is attempting to introduce a clone for the i1. With the changes submitted, I believe the error is corrected. Happy to improve the test. It would be nice if this is still merged (if deemed a fix) despite buffer-deallocation being deprecated as other people may encounter this.
Thanks, and happy to make changes to improve this fix.