-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[mlir] Fix liveness analysis #88848
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] Fix liveness analysis #88848
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 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 If you have received no comments on your PR for a week, you can request a review 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 Author: Ivan Kulagin (ikulagin) ChangesThe current implementation does not take into account definitions created by arguments of nested blocks. This leads to an incorrect construction of the live-in set of an outer block. Arguments of nested blocks are added to the live-in set of an outer block. Full diff: https://github.com/llvm/llvm-project/pull/88848.diff 2 Files Affected:
diff --git a/mlir/lib/Analysis/Liveness.cpp b/mlir/lib/Analysis/Liveness.cpp
index a8e0daeabf4061..50ac7129dab87f 100644
--- a/mlir/lib/Analysis/Liveness.cpp
+++ b/mlir/lib/Analysis/Liveness.cpp
@@ -67,11 +67,18 @@ struct BlockInfoBuilder {
// Mark all nested operation results as defined, and nested operation
// operands as used. All defined value will be removed from the used set
// at the end.
- block->walk([&](Operation *op) {
- for (Value result : op->getResults())
- defValues.insert(result);
- for (Value operand : op->getOperands())
- useValues.insert(operand);
+ block->walk([&](Block *nestedBlock) {
+ if (block != nestedBlock) {
+ for (BlockArgument arg : nestedBlock->getArguments()) {
+ defValues.insert(arg);
+ }
+ }
+ for (Operation &op : *nestedBlock) {
+ for (Value result : op.getResults())
+ defValues.insert(result);
+ for (Value operand : op.getOperands())
+ useValues.insert(operand);
+ }
});
llvm::set_subtract(useValues, defValues);
}
diff --git a/mlir/test/Analysis/test-liveness.mlir b/mlir/test/Analysis/test-liveness.mlir
index 8ae3d09a6cd122..17b5e7d5ae8990 100644
--- a/mlir/test/Analysis/test-liveness.mlir
+++ b/mlir/test/Analysis/test-liveness.mlir
@@ -493,3 +493,59 @@ func.func @nested_region3(
}
return %1 : i32
}
+
+// -----
+
+// CHECK-LABEL: Testing : nested_region4
+
+func.func @nested_region4(%arg0: index, %arg1: index, %arg2: index) {
+ // CHECK: Block: 0
+ // CHECK-NEXT: LiveIn:
+ // CHECK-NEXT: LiveOut:
+ // CHECK-NEXT: BeginLivenessIntervals
+ // CHECK-NEXT: val_3
+ // CHECK-NEXT: %c0_i32 = arith.constant 0
+ // CHECK-NEXT: %c1_i32 = arith.constant 1
+ // CHECK-NEXT: %0 = scf.for
+ // COM: Skipping the body of the scf.for...
+ // CHECK: val_4
+ // CHECK-NEXT: %c1_i32 = arith.constant 1
+ // CHECK-NEXT: %0 = scf.for
+ // COM: Skipping the body of the scf.for...
+ // CHECK: // %1 = arith.addi
+ // CHECK-NEXT: val_5
+ // CHECK-NEXT: %0 = scf.for
+ // COM: Skipping the body of the scf.for...
+ // CHECK: EndLivenessIntervals
+ // CHECK-NEXT: BeginCurrentlyLive
+ // CHECK-NEXT: %c0_i32 = arith.constant 0
+ // CHECK-SAME: arg0@0 arg1@0 arg2@0 val_3
+ // CHECK-NEXT: %c1_i32 = arith.constant 1
+ // CHECK-SAME: arg0@0 arg1@0 arg2@0 val_3 val_4
+ // CHECK-NEXT: %0 = scf.for
+ // COM: Skipping the body of the scf.for...
+ // CHECK: arg0@0 arg1@0 arg2@0 val_3 val_4 val_5
+ // CHECK-NEXT: EndCurrentlyLive
+ %c0_i32 = arith.constant 0 : i32
+ %c1_i32 = arith.constant 1 : i32
+
+ %0 = scf.for %arg3 = %arg0 to %arg1 step %arg2 iter_args(%arg4 = %c0_i32) -> (i32) {
+ // CHECK-NEXT: Block: 1
+ // CHECK-NEXT: LiveIn: val_4
+ // CHECK-NEXT: LiveOut:
+ // CHECK-NEXT: BeginLivenessIntervals
+ // CHECK-NEXT: val_8
+ // CHECK-NEXT: %1 = arith.addi
+ // CHECK-NEXT: scf.yield %1
+ // CHECK-NEXT: EndLivenessIntervals
+ // CHECK-NEXT: BeginCurrentlyLive
+ // CHECK-NEXT: %1 = arith.addi
+ // CHECK-SAME: val_4 arg0@1 arg1@1 val_8
+ // CHECK-NEXT: scf.yield %1
+ // CHECK-SAME: val_8
+ // CHECK-NEXT: EndCurrentlyLive
+ %1 = arith.addi %arg4, %c1_i32 : i32
+ scf.yield %1 : i32
+ }
+ return
+}
|
@dfki-mako @jurahul Please take a look at the suggested fix. |
For added context, prior to this change
gives
|
@ikulagin Let us know when you're ready for this to be merged. |
@makslevental All fixes have been made, PR is ready for merge. |
@ikulagin do you need us to merge this for you? |
@makslevental I suggest minimizing the test as I wrote in this comment, what's your opinion? |
I don't have an opinion - I think tight tests aren't so bad (they can be useful as smoke signals sometimes) but if you want to reduce then sure it makes sense. |
Signed-off-by: ikulagin <[email protected]>
Signed-off-by: ikulagin <[email protected]>
Signed-off-by: ikulagin <[email protected]>
@ikulagin 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 Please check whether problems have been caused by your change specifically, as 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. If you don't get any reports, no action is required from you. Your changes are working as expected, well done! |
The current implementation does not take into account definitions created by arguments of nested blocks. This leads to an incorrect construction of the live-in set of an outer block. Arguments of nested blocks are added to the live-in set of an outer block.