Skip to content

Commit e4fc918

Browse files
authored
Extend quir::SwitchOp lowering to correctly lower results. (#153)
This patch extends the quir::SwitchOp lowering to LLVM IR to correctly handle results. These changes are based on the support added to scf::IndexSwitchOp operation, which also supports switch ops with results. Support for lowering the scf::IndexSwitchOp was added to MLIR in commit 91effec85 (llvm/llvm-project@91effec85).
1 parent 9215104 commit e4fc918

File tree

1 file changed

+6
-13
lines changed

1 file changed

+6
-13
lines changed

lib/Conversion/QUIRToStandard/SwitchOpLowering.cpp

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -53,26 +53,19 @@ struct SwitchOpLowering : public OpRewritePattern<SwitchOp> {
5353
LogicalResult
5454
SwitchOpLowering::matchAndRewrite(SwitchOp switchOp,
5555
PatternRewriter &rewriter) const {
56-
assert(switchOp.resultTypes().size() == 0 &&
57-
"switch ops with result values are not supported");
58-
5956
auto loc = switchOp.getLoc();
6057

6158
// Start by splitting the block containing the 'quir.switch' into parts.
6259
// The part before will contain the condition, the part after will be the
6360
// continuation point.
6461
auto *condBlock = rewriter.getInsertionBlock();
6562
auto opPosition = rewriter.getInsertionPoint();
66-
auto *remainingOpsBlock = rewriter.splitBlock(condBlock, opPosition);
67-
Block *continueBlock;
68-
if (switchOp.getNumResults() == 0) {
69-
continueBlock = remainingOpsBlock;
70-
} else {
71-
continueBlock =
72-
rewriter.createBlock(remainingOpsBlock, switchOp.getResultTypes());
73-
rewriter.create<BranchOp>(loc, remainingOpsBlock);
74-
}
75-
63+
Block *continueBlock = rewriter.splitBlock(condBlock, opPosition);
64+
SmallVector<Value> results;
65+
results.reserve(switchOp.getNumResults());
66+
for (Type resultType : switchOp.getResultTypes())
67+
results.push_back(
68+
continueBlock->addArgument(resultType, switchOp.getLoc()));
7669
// Move blocks from the "default" region to the region containing
7770
// 'quir.switch', place it before the continuation block, and branch to it.
7871
auto &defaultRegion = switchOp.defaultRegion();

0 commit comments

Comments
 (0)