-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[mlir] Fix region simplification bug when later blocks use prior block argument values #97960
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
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 @llvm/pr-subscribers-mlir-cf Author: Ben Howe (bmhowe23) ChangesThis fixes #94520 by ensuring that any if any block arguments are being used outside of the original block that the block is not considered a candidate for merging. More details: the root cause of the issue described in #94520 was that Full diff: https://github.com/llvm/llvm-project/pull/97960.diff 2 Files Affected:
diff --git a/mlir/lib/Transforms/Utils/RegionUtils.cpp b/mlir/lib/Transforms/Utils/RegionUtils.cpp
index 4c0f15bafbaba3..4d70a2817deeac 100644
--- a/mlir/lib/Transforms/Utils/RegionUtils.cpp
+++ b/mlir/lib/Transforms/Utils/RegionUtils.cpp
@@ -778,6 +778,15 @@ static LogicalResult mergeIdenticalBlocks(RewriterBase &rewriter,
if (hasNonEmptyRegion)
continue;
+ // Don't allow merging if this block's arguments are used outside of the
+ // original block.
+ bool argHasExternalUsers = llvm::any_of(
+ block->getArguments(), [block](mlir::BlockArgument &arg) {
+ return arg.isUsedOutsideOfBlock(block);
+ });
+ if (argHasExternalUsers)
+ continue;
+
// Try to add this block to an existing cluster.
bool addedToCluster = false;
for (auto &cluster : clusters)
diff --git a/mlir/test/Integration/Dialect/ControlFlow/test-region-simplification.mlir b/mlir/test/Integration/Dialect/ControlFlow/test-region-simplification.mlir
new file mode 100644
index 00000000000000..f425c47addfcb3
--- /dev/null
+++ b/mlir/test/Integration/Dialect/ControlFlow/test-region-simplification.mlir
@@ -0,0 +1,49 @@
+// Baseline check
+// RUN: mlir-opt %s --convert-func-to-llvm --convert-cf-to-llvm | \
+// RUN: mlir-cpu-runner -e nested_loop --entry-point-result=i32 | FileCheck %s
+
+// Region simplification check
+// RUN: mlir-opt %s \
+// RUN: --canonicalize='enable-patterns=AnyPattern region-simplify=aggressive' \
+// RUN: --convert-func-to-llvm --convert-cf-to-llvm | mlir-cpu-runner \
+// RUN: -e nested_loop --entry-point-result=i32 | FileCheck %s
+
+func.func @nested_loop() -> i32 {
+ %c3_i64 = arith.constant 3 : i64
+ %c2_i64 = arith.constant 2 : i64
+ %c0_i64 = arith.constant 0 : i64
+ %c1_i64 = arith.constant 1 : i64
+ %c1_i32 = arith.constant 1 : i32
+ %c0_i32 = arith.constant 0 : i32
+ cf.br ^bb1(%c0_i32, %c0_i64 : i32, i64)
+^bb1(%0: i32, %1: i64): // 2 preds: ^bb0, ^bb8
+ %2 = arith.cmpi ult, %1, %c2_i64 : i64
+ cf.cond_br %2, ^bb2(%0, %1 : i32, i64), ^bb9(%0, %1 : i32, i64)
+^bb2(%3: i32, %4: i64): // pred: ^bb1
+ %5 = arith.addi %4, %c1_i64 : i64
+ cf.br ^bb3(%3, %5 : i32, i64)
+^bb3(%6: i32, %7: i64): // 2 preds: ^bb2, ^bb5
+ %8 = arith.cmpi ult, %7, %c3_i64 : i64
+ cf.cond_br %8, ^bb4(%6, %7 : i32, i64), ^bb6(%6, %7 : i32, i64)
+^bb4(%9: i32, %10: i64): // pred: ^bb3
+ %11 = arith.addi %9, %c1_i32 : i32
+ cf.br ^bb5(%11, %10 : i32, i64)
+^bb5(%12: i32, %13: i64): // pred: ^bb4
+ %14 = arith.addi %13, %c1_i64 : i64
+ cf.br ^bb3(%12, %14 : i32, i64)
+^bb6(%15: i32, %16: i64): // pred: ^bb3
+ cf.br ^bb7
+^bb7: // pred: ^bb6
+ cf.br ^bb8(%15, %4 : i32, i64)
+^bb8(%17: i32, %18: i64): // pred: ^bb7
+ %19 = arith.addi %18, %c1_i64 : i64
+ cf.br ^bb1(%17, %19 : i32, i64)
+^bb9(%20: i32, %21: i64): // pred: ^bb1
+ cf.br ^bb10
+^bb10: // pred: ^bb9
+ return %20 : i32
+}
+
+// If region simplification behaves correctly (by NOT merging ^bb2 and ^bb5),
+// this will be 3.
+// CHECK: 3
|
Looks good to me. |
Pinging @bondhugula, @joker-eph, and @matthias-springer for review. |
Just FYI, this was tested locally by doing $ cmake -S llvm -B build -G Ninja -DLLVM_ENABLE_PROJECTS="clang;lld;mlir" -DCMAKE_BUILD_TYPE=Release -DLLVM_BUILD_TESTS=ON -DMLIR_INCLUDE_INTEGRATION_TESTS=ON
$ cd build
$ ninja
$ ninja check-mlir I know the last item exercised the new test. Without the change, it fails, and with the change, it passes. |
Ping. This is my first PR to this repo, so please let me know if I'm missing something about the process here or if I should rework any of this? Thanks! |
Sorry, this just fell through the review crack. Feel free to ping every week if a PR does not get reviewers attention. |
mlir/test/Integration/Dialect/ControlFlow/test-region-simplification.mlir
Outdated
Show resolved
Hide resolved
I believe I have addressed the only comment so far (moving an integration test into a unit test), and it's been ~2 weeks since there has been any discussion on this, so I figured I'd ping for review 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.
LG, thanks
Thanks, @joker-eph. I don't have commit privileges in this repo. Would you mind pushing this for me? Please let me know if I should do any final "update branch" or rebase. |
@bmhowe23 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! |
This fixes #94520 by ensuring that any if any block arguments are being used outside of the original block that the block is not considered a candidate for merging.
More details: the root cause of the issue described in #94520 was that
^bb2
and^bb5
were being merged despite%4
(an argument to^bb2
) was being used later in^bb7
. When the block merge occurred, that unintentionally changed the value of%4
for all downstream code. This change prevents that from happening.