-
Notifications
You must be signed in to change notification settings - Fork 15.3k
Revert "[MLIR][SCF] Sink scf.if from scf.while before region into after region in scf-uplift-while-to-for" #169888
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
+1
−109
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
…er regio…" This reverts commit 25d027b.
Member
|
@llvm/pr-subscribers-mlir @llvm/pr-subscribers-mlir-scf Author: Ming Yan (NexMing) ChangesReverts llvm/llvm-project#165216 Full diff: https://github.com/llvm/llvm-project/pull/169888.diff 2 Files Affected:
diff --git a/mlir/lib/Dialect/SCF/Transforms/UpliftWhileToFor.cpp b/mlir/lib/Dialect/SCF/Transforms/UpliftWhileToFor.cpp
index 9f242f9e62b8e..ec1044aaa42ac 100644
--- a/mlir/lib/Dialect/SCF/Transforms/UpliftWhileToFor.cpp
+++ b/mlir/lib/Dialect/SCF/Transforms/UpliftWhileToFor.cpp
@@ -19,83 +19,6 @@
using namespace mlir;
namespace {
-/// Move an scf.if op that is directly before the scf.condition op in the while
-/// before region, and whose condition matches the condition of the
-/// scf.condition op, down into the while after region.
-///
-/// scf.while (%init) : (...) -> ... {
-/// %cond = ...
-/// %res = scf.if %cond -> (...) {
-/// use1(%init)
-/// %then_val = ...
-/// ... // then block
-/// scf.yield %then_val
-/// } else {
-/// scf.yield %init
-/// }
-/// scf.condition(%cond) %res
-/// } do {
-/// ^bb0(%arg):
-/// use2(%arg)
-/// ...
-///
-/// becomes
-/// scf.while (%init) : (...) -> ... {
-/// %cond = ...
-/// scf.condition(%cond) %init
-/// } do {
-/// ^bb0(%arg): :
-/// use1(%arg)
-/// ... // if then block
-/// %then_val = ...
-/// use2(%then_val)
-/// ...
-struct WhileMoveIfDown : public OpRewritePattern<scf::WhileOp> {
- using OpRewritePattern<scf::WhileOp>::OpRewritePattern;
-
- LogicalResult matchAndRewrite(scf::WhileOp op,
- PatternRewriter &rewriter) const override {
- // Check that the first opeation produces one result and that result must
- // have exactly two uses (these two uses come from the `scf.if` and
- // `scf.condition` operations).
- Operation &condOp = op.getBeforeBody()->front();
- if (condOp.getNumResults() != 1 || !condOp.getResult(0).hasNUses(2))
- return failure();
-
- Value condVal = condOp.getResult(0);
- auto ifOp = dyn_cast<scf::IfOp>(condOp.getNextNode());
- if (!ifOp || ifOp.getCondition() != condVal)
- return failure();
-
- auto term = dyn_cast<scf::ConditionOp>(ifOp->getNextNode());
- if (!term || term.getCondition() != condVal)
- return failure();
-
- // Check that if results and else yield operands match the scf.condition op
- // arguments and while before arguments respectively.
- if (!llvm::equal(ifOp->getResults(), term.getArgs()) ||
- !llvm::equal(ifOp.elseYield()->getOperands(), op.getBeforeArguments()))
- return failure();
-
- // Update uses and move the if op into the after region.
- rewriter.replaceAllUsesWith(op.getAfterArguments(),
- ifOp.thenYield()->getOperands());
- rewriter.replaceUsesWithIf(op.getBeforeArguments(), op.getAfterArguments(),
- [&](OpOperand &use) {
- return ifOp.getThenRegion().isAncestor(
- use.getOwner()->getParentRegion());
- });
- rewriter.modifyOpInPlace(
- term, [&]() { term.getArgsMutable().assign(op.getBeforeArguments()); });
-
- rewriter.eraseOp(ifOp.thenYield());
- rewriter.inlineBlockBefore(ifOp.thenBlock(), op.getAfterBody(),
- op.getAfterBody()->begin());
- rewriter.eraseOp(ifOp);
- return success();
- }
-};
-
struct UpliftWhileOp : public OpRewritePattern<scf::WhileOp> {
using OpRewritePattern::OpRewritePattern;
@@ -344,5 +267,5 @@ FailureOr<scf::ForOp> mlir::scf::upliftWhileToForLoop(RewriterBase &rewriter,
}
void mlir::scf::populateUpliftWhileToForPatterns(RewritePatternSet &patterns) {
- patterns.add<UpliftWhileOp, WhileMoveIfDown>(patterns.getContext());
+ patterns.add<UpliftWhileOp>(patterns.getContext());
}
diff --git a/mlir/test/Dialect/SCF/uplift-while.mlir b/mlir/test/Dialect/SCF/uplift-while.mlir
index 736112824c515..cbe2ce5076ad2 100644
--- a/mlir/test/Dialect/SCF/uplift-while.mlir
+++ b/mlir/test/Dialect/SCF/uplift-while.mlir
@@ -185,34 +185,3 @@ func.func @uplift_while(%arg0: index, %arg1: index, %arg2: index) -> (i32, f32)
// CHECK: %[[T2:.*]] = "test.test2"(%[[ARG2]]) : (f32) -> f32
// CHECK: scf.yield %[[T1]], %[[T2]] : i32, f32
// CHECK: return %[[RES]]#0, %[[RES]]#1 : i32, f32
-
-// -----
-
-func.func @uplift_while(%low: index, %upper: index, %val : i32) -> i32 {
- %c1 = arith.constant 1 : index
- %1:2 = scf.while (%iv = %low, %iter = %val) : (index, i32) -> (index, i32) {
- %2 = arith.cmpi slt, %iv, %upper : index
- %3:2 = scf.if %2 -> (index, i32) {
- %4 = "test.test"(%iter) : (i32) -> i32
- %5 = arith.addi %iv, %c1 : index
- scf.yield %5, %4 : index, i32
- } else {
- scf.yield %iv, %iter : index, i32
- }
- scf.condition(%2) %3#0, %3#1 : index, i32
- } do {
- ^bb0(%arg0: index, %arg1: i32):
- scf.yield %arg0, %arg1 : index, i32
- }
- return %1#1 : i32
-}
-
-// CHECK-LABEL: func.func @uplift_while(
-// CHECK-SAME: %[[ARG0:.*]]: index, %[[ARG1:.*]]: index, %[[ARG2:.*]]: i32) -> i32 {
-// CHECK: %[[CONSTANT_0:.*]] = arith.constant 1 : index
-// CHECK: %[[FOR_0:.*]] = scf.for %[[VAL_0:.*]] = %[[ARG0]] to %[[ARG1]] step %[[CONSTANT_0]] iter_args(%[[VAL_1:.*]] = %[[ARG2]]) -> (i32) {
-// CHECK: %[[VAL_2:.*]] = "test.test"(%[[VAL_1]]) : (i32) -> i32
-// CHECK: scf.yield %[[VAL_2]] : i32
-// CHECK: }
-// CHECK: return %[[FOR_0]] : i32
-// CHECK: }
|
aahrun
pushed a commit
to aahrun/llvm-project
that referenced
this pull request
Dec 1, 2025
…er region in scf-uplift-while-to-for" (llvm#169888) Reverts llvm#165216 It is implemented in llvm#169892 .
augusto2112
pushed a commit
to augusto2112/llvm-project
that referenced
this pull request
Dec 3, 2025
…er region in scf-uplift-while-to-for" (llvm#169888) Reverts llvm#165216 It is implemented in llvm#169892 .
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Reverts #165216
It is implemented in #169892 .