Skip to content

Commit c5062d7

Browse files
authored
Revert "[mlir] move if-condition propagation to a standalone pass" (#159535)
Reverts #150278 Multiple post-merge comment remained undressed, and some more fundamental issues were also reported in #159165
1 parent 0384f6c commit c5062d7

File tree

6 files changed

+97
-141
lines changed

6 files changed

+97
-141
lines changed

mlir/include/mlir/Dialect/SCF/Transforms/Passes.td

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,6 @@ def SCFForLoopSpecialization : Pass<"scf-for-loop-specialization"> {
4141
let constructor = "mlir::createForLoopSpecializationPass()";
4242
}
4343

44-
def SCFIfConditionPropagation : Pass<"scf-if-condition-propagation"> {
45-
let summary = "Replace usages of if condition with true/false constants in "
46-
"the conditional regions";
47-
let dependentDialects = ["arith::ArithDialect"];
48-
}
49-
5044
def SCFParallelLoopFusion : Pass<"scf-parallel-loop-fusion"> {
5145
let summary = "Fuse adjacent parallel loops";
5246
let constructor = "mlir::createParallelLoopFusionPass()";

mlir/lib/Dialect/SCF/IR/SCF.cpp

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2453,6 +2453,65 @@ struct ConvertTrivialIfToSelect : public OpRewritePattern<IfOp> {
24532453
}
24542454
};
24552455

2456+
/// Allow the true region of an if to assume the condition is true
2457+
/// and vice versa. For example:
2458+
///
2459+
/// scf.if %cmp {
2460+
/// print(%cmp)
2461+
/// }
2462+
///
2463+
/// becomes
2464+
///
2465+
/// scf.if %cmp {
2466+
/// print(true)
2467+
/// }
2468+
///
2469+
struct ConditionPropagation : public OpRewritePattern<IfOp> {
2470+
using OpRewritePattern<IfOp>::OpRewritePattern;
2471+
2472+
LogicalResult matchAndRewrite(IfOp op,
2473+
PatternRewriter &rewriter) const override {
2474+
// Early exit if the condition is constant since replacing a constant
2475+
// in the body with another constant isn't a simplification.
2476+
if (matchPattern(op.getCondition(), m_Constant()))
2477+
return failure();
2478+
2479+
bool changed = false;
2480+
mlir::Type i1Ty = rewriter.getI1Type();
2481+
2482+
// These variables serve to prevent creating duplicate constants
2483+
// and hold constant true or false values.
2484+
Value constantTrue = nullptr;
2485+
Value constantFalse = nullptr;
2486+
2487+
for (OpOperand &use :
2488+
llvm::make_early_inc_range(op.getCondition().getUses())) {
2489+
if (op.getThenRegion().isAncestor(use.getOwner()->getParentRegion())) {
2490+
changed = true;
2491+
2492+
if (!constantTrue)
2493+
constantTrue = rewriter.create<arith::ConstantOp>(
2494+
op.getLoc(), i1Ty, rewriter.getIntegerAttr(i1Ty, 1));
2495+
2496+
rewriter.modifyOpInPlace(use.getOwner(),
2497+
[&]() { use.set(constantTrue); });
2498+
} else if (op.getElseRegion().isAncestor(
2499+
use.getOwner()->getParentRegion())) {
2500+
changed = true;
2501+
2502+
if (!constantFalse)
2503+
constantFalse = rewriter.create<arith::ConstantOp>(
2504+
op.getLoc(), i1Ty, rewriter.getIntegerAttr(i1Ty, 0));
2505+
2506+
rewriter.modifyOpInPlace(use.getOwner(),
2507+
[&]() { use.set(constantFalse); });
2508+
}
2509+
}
2510+
2511+
return success(changed);
2512+
}
2513+
};
2514+
24562515
/// Remove any statements from an if that are equivalent to the condition
24572516
/// or its negation. For example:
24582517
///
@@ -2835,8 +2894,9 @@ struct CombineNestedIfs : public OpRewritePattern<IfOp> {
28352894

28362895
void IfOp::getCanonicalizationPatterns(RewritePatternSet &results,
28372896
MLIRContext *context) {
2838-
results.add<CombineIfs, CombineNestedIfs, ConvertTrivialIfToSelect,
2839-
RemoveEmptyElseBranch, RemoveStaticCondition, RemoveUnusedResults,
2897+
results.add<CombineIfs, CombineNestedIfs, ConditionPropagation,
2898+
ConvertTrivialIfToSelect, RemoveEmptyElseBranch,
2899+
RemoveStaticCondition, RemoveUnusedResults,
28402900
ReplaceIfYieldWithConditionOrValue>(context);
28412901
}
28422902

mlir/lib/Dialect/SCF/Transforms/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ add_mlir_dialect_library(MLIRSCFTransforms
44
ForallToFor.cpp
55
ForallToParallel.cpp
66
ForToWhile.cpp
7-
IfConditionPropagation.cpp
87
LoopCanonicalization.cpp
98
LoopPipelining.cpp
109
LoopRangeFolding.cpp

mlir/lib/Dialect/SCF/Transforms/IfConditionPropagation.cpp

Lines changed: 0 additions & 98 deletions
This file was deleted.

mlir/test/Dialect/SCF/canonicalize.mlir

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -867,6 +867,41 @@ func.func @matmul_on_tensors(%t0: tensor<32x1024xf32>) -> tensor<?x?xf32> {
867867

868868
// -----
869869

870+
// CHECK-LABEL: @cond_prop
871+
func.func @cond_prop(%arg0 : i1) -> index {
872+
%res = scf.if %arg0 -> index {
873+
%res1 = scf.if %arg0 -> index {
874+
%v1 = "test.get_some_value1"() : () -> index
875+
scf.yield %v1 : index
876+
} else {
877+
%v2 = "test.get_some_value2"() : () -> index
878+
scf.yield %v2 : index
879+
}
880+
scf.yield %res1 : index
881+
} else {
882+
%res2 = scf.if %arg0 -> index {
883+
%v3 = "test.get_some_value3"() : () -> index
884+
scf.yield %v3 : index
885+
} else {
886+
%v4 = "test.get_some_value4"() : () -> index
887+
scf.yield %v4 : index
888+
}
889+
scf.yield %res2 : index
890+
}
891+
return %res : index
892+
}
893+
// CHECK-NEXT: %[[if:.+]] = scf.if %arg0 -> (index) {
894+
// CHECK-NEXT: %[[c1:.+]] = "test.get_some_value1"() : () -> index
895+
// CHECK-NEXT: scf.yield %[[c1]] : index
896+
// CHECK-NEXT: } else {
897+
// CHECK-NEXT: %[[c4:.+]] = "test.get_some_value4"() : () -> index
898+
// CHECK-NEXT: scf.yield %[[c4]] : index
899+
// CHECK-NEXT: }
900+
// CHECK-NEXT: return %[[if]] : index
901+
// CHECK-NEXT:}
902+
903+
// -----
904+
870905
// CHECK-LABEL: @replace_if_with_cond1
871906
func.func @replace_if_with_cond1(%arg0 : i1) -> (i32, i1) {
872907
%true = arith.constant true

mlir/test/Dialect/SCF/if-cond-prop.mlir

Lines changed: 0 additions & 34 deletions
This file was deleted.

0 commit comments

Comments
 (0)