[llvm-branch-commits] [mlir] [mlir][SCF] Fold unused `index_switch` results (PR #173560)

Mehdi Amini via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Sun Dec 28 07:49:33 PST 2025


================
@@ -4797,9 +4797,59 @@ struct FoldConstantCase : OpRewritePattern<scf::IndexSwitchOp> {
   }
 };
 
+/// Canonicalization patterns that folds away dead results of
+/// "scf.index_switch" ops.
+struct FoldUnusedIndexSwitchResults : OpRewritePattern<IndexSwitchOp> {
+  using OpRewritePattern<IndexSwitchOp>::OpRewritePattern;
+
+  LogicalResult matchAndRewrite(IndexSwitchOp op,
+                                PatternRewriter &rewriter) const override {
+    // Find dead results.
+    BitVector deadResults(op.getNumResults(), false);
+    SmallVector<Type> newResultTypes;
+    for (auto [idx, result] : llvm::enumerate(op.getResults())) {
+      if (!result.use_empty()) {
+        newResultTypes.push_back(result.getType());
+      } else {
+        deadResults[idx] = true;
+      }
+    }
+    if (!deadResults.any())
----------------
joker-eph wrote:

```suggestion
    if (newResultTypes.size() != op.getNumResults())
```

Nit: cheaper

https://github.com/llvm/llvm-project/pull/173560


More information about the llvm-branch-commits mailing list