Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 51 additions & 1 deletion mlir/lib/Dialect/SCF/IR/SCF.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: you can do using Base::Base


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())
return rewriter.notifyMatchFailure(op, "no dead results to fold");

// Create new op without dead results and inline case regions.
auto newOp = IndexSwitchOp::create(rewriter, op.getLoc(), newResultTypes,
op.getArg(), op.getCases(),
op.getCaseRegions().size());
auto inlineCaseRegion = [&](Region &oldRegion, Region &newRegion) {
rewriter.inlineRegionBefore(oldRegion, newRegion, newRegion.begin());
// Remove respective operands from yield op.
Operation *terminator = newRegion.front().getTerminator();
assert(isa<YieldOp>(terminator) && "expected yield op");
rewriter.modifyOpInPlace(
terminator, [&]() { terminator->eraseOperands(deadResults); });
};
for (auto [oldRegion, newRegion] :
llvm::zip_equal(op.getCaseRegions(), newOp.getCaseRegions()))
inlineCaseRegion(oldRegion, newRegion);
inlineCaseRegion(op.getDefaultRegion(), newOp.getDefaultRegion());

// Replace op with new op.
SmallVector<Value> newResults(op.getNumResults(), Value());
unsigned nextNewResult = 0;
for (unsigned idx = 0; idx < op.getNumResults(); ++idx) {
if (deadResults[idx])
continue;
newResults[idx] = newOp.getResult(nextNewResult++);
}
rewriter.replaceOp(op, newResults);
return success();
}
};

void IndexSwitchOp::getCanonicalizationPatterns(RewritePatternSet &results,
MLIRContext *context) {
results.add<FoldConstantCase>(context);
results.add<FoldConstantCase, FoldUnusedIndexSwitchResults>(context);
}

//===----------------------------------------------------------------------===//
Expand Down
31 changes: 31 additions & 0 deletions mlir/test/Dialect/SCF/canonicalize.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -2207,3 +2207,34 @@ func.func @iter_args_cycles_non_cycle_start(%lb : index, %ub : index, %step : in
}
return %res#0, %res#1, %res#2 : i32, i32, i32
}

// -----

// CHECK-LABEL: func @dead_index_switch_result(
// CHECK-SAME: %[[arg0:.*]]: index
// CHECK-DAG: %[[c10:.*]] = arith.constant 10
// CHECK-DAG: %[[c11:.*]] = arith.constant 11
// CHECK: %[[switch:.*]] = scf.index_switch %[[arg0]] -> index
// CHECK: case 1 {
// CHECK: memref.store %[[c10]]
// CHECK: scf.yield %[[arg0]] : index
// CHECK: }
// CHECK: default {
// CHECK: memref.store %[[c11]]
// CHECK: scf.yield %[[arg0]] : index
// CHECK: }
// CHECK: return %[[switch]]
func.func @dead_index_switch_result(%arg0 : index, %arg1 : memref<i32>) -> index {
%non_live, %live = scf.index_switch %arg0 -> i32, index
case 1 {
%c10 = arith.constant 10 : i32
memref.store %c10, %arg1[] : memref<i32>
scf.yield %c10, %arg0 : i32, index
}
default {
%c11 = arith.constant 11 : i32
memref.store %c11, %arg1[] : memref<i32>
scf.yield %c11, %arg0 : i32, index
}
return %live : index
}
Loading