[Mlir-commits] [mlir] [MLIR][SCF] fix scf.index_switch fold convergence (#98535) (PR #98680)
Keyi Zhang
llvmlistbot at llvm.org
Fri Jul 12 11:56:32 PDT 2024
https://github.com/Kuree created https://github.com/llvm/llvm-project/pull/98680
If the `scf.index_switch` op has no result, the current fold logic results in an infinite loop (see #98535). The is because `fold` mechanism does not support zero-result ops. This PR moves the fold logic to a canonicalizer and fix the issue.
>From 8a45c8817fc5193a2dd2d4c1a32398e3d6330158 Mon Sep 17 00:00:00 2001
From: Keyi Zhang <keyi at efficient.computer>
Date: Fri, 12 Jul 2024 11:53:09 -0700
Subject: [PATCH] [MLIR][SCF] fix scf.index_switch fold convergence (#98535)
---
mlir/include/mlir/Dialect/SCF/IR/SCFOps.td | 2 +-
mlir/lib/Dialect/SCF/IR/SCF.cpp | 53 ++++++++++++----------
mlir/test/Dialect/SCF/canonicalize.mlir | 16 +++++++
3 files changed, 46 insertions(+), 25 deletions(-)
diff --git a/mlir/include/mlir/Dialect/SCF/IR/SCFOps.td b/mlir/include/mlir/Dialect/SCF/IR/SCFOps.td
index f35ea962bea16..acbcbae105dbf 100644
--- a/mlir/include/mlir/Dialect/SCF/IR/SCFOps.td
+++ b/mlir/include/mlir/Dialect/SCF/IR/SCFOps.td
@@ -1159,7 +1159,7 @@ def IndexSwitchOp : SCF_Op<"index_switch", [RecursiveMemoryEffects,
Block &getCaseBlock(unsigned idx);
}];
- let hasFolder = 1;
+ let hasCanonicalizer = 1;
let hasVerifier = 1;
}
diff --git a/mlir/lib/Dialect/SCF/IR/SCF.cpp b/mlir/lib/Dialect/SCF/IR/SCF.cpp
index 907d7f794593d..c09c5ca417dd2 100644
--- a/mlir/lib/Dialect/SCF/IR/SCF.cpp
+++ b/mlir/lib/Dialect/SCF/IR/SCF.cpp
@@ -4297,33 +4297,38 @@ void IndexSwitchOp::getRegionInvocationBounds(
bounds.emplace_back(/*lb=*/0, /*ub=*/i == liveIndex);
}
-LogicalResult IndexSwitchOp::fold(FoldAdaptor adaptor,
- SmallVectorImpl<OpFoldResult> &results) {
- std::optional<int64_t> maybeCst = getConstantIntValue(getArg());
- if (!maybeCst.has_value())
- return failure();
- int64_t cst = *maybeCst;
- int64_t caseIdx, e = getNumCases();
- for (caseIdx = 0; caseIdx < e; ++caseIdx) {
- if (cst == getCases()[caseIdx])
- break;
- }
+struct FoldConstantCase : OpRewritePattern<scf::IndexSwitchOp> {
+ using OpRewritePattern<scf::IndexSwitchOp>::OpRewritePattern;
- Region &r = (caseIdx < getNumCases()) ? getCaseRegions()[caseIdx]
- : getDefaultRegion();
- Block &source = r.front();
- results.assign(source.getTerminator()->getOperands().begin(),
- source.getTerminator()->getOperands().end());
+ LogicalResult matchAndRewrite(scf::IndexSwitchOp op,
+ PatternRewriter &rewriter) const override {
+ std::optional<int64_t> maybeCst = getConstantIntValue(op.getArg());
+ if (!maybeCst.has_value())
+ return failure();
+ int64_t cst = *maybeCst;
+ int64_t caseIdx, e = op.getNumCases();
+ for (caseIdx = 0; caseIdx < e; ++caseIdx) {
+ if (cst == op.getCases()[caseIdx])
+ break;
+ }
- Block *pDestination = (*this)->getBlock();
- if (!pDestination)
- return failure();
- Block::iterator insertionPoint = (*this)->getIterator();
- pDestination->getOperations().splice(insertionPoint, source.getOperations(),
- source.getOperations().begin(),
- std::prev(source.getOperations().end()));
+ Region &r = (caseIdx < op.getNumCases()) ? op.getCaseRegions()[caseIdx]
+ : op.getDefaultRegion();
+ Block &source = r.front();
+ auto *terminator = source.getTerminator();
+ SmallVector<Value> results = terminator->getOperands();
- return success();
+ rewriter.inlineBlockBefore(&source, op);
+ rewriter.eraseOp(terminator);
+ rewriter.replaceOp(op, results);
+
+ return success();
+ }
+};
+
+void IndexSwitchOp::getCanonicalizationPatterns(RewritePatternSet &results,
+ MLIRContext *context) {
+ results.add<FoldConstantCase>(context);
}
//===----------------------------------------------------------------------===//
diff --git a/mlir/test/Dialect/SCF/canonicalize.mlir b/mlir/test/Dialect/SCF/canonicalize.mlir
index 459ccd73cfe61..00e89d39b6679 100644
--- a/mlir/test/Dialect/SCF/canonicalize.mlir
+++ b/mlir/test/Dialect/SCF/canonicalize.mlir
@@ -1846,3 +1846,19 @@ func.func @index_switch_fold() -> (f32, f32) {
// CHECK-NEXT: %[[c1:.*]] = arith.constant 1.000000e+00 : f32
// CHECK-NEXT: %[[c42:.*]] = arith.constant 4.200000e+01 : f32
// CHECK-NEXT: return %[[c1]], %[[c42]] : f32, f32
+
+func.func @index_switch_fold_no_res() {
+ %c1 = arith.constant 1 : index
+ scf.index_switch %c1
+ case 0 {
+ scf.yield
+ }
+ default {
+ "test.op"() : () -> ()
+ scf.yield
+ }
+ return
+}
+
+// CHECK-LABEL: func.func @index_switch_fold_no_res()
+// CHECK-NEXT: "test.op"() : () -> ()
More information about the Mlir-commits
mailing list