Skip to content

Commit 34bd74e

Browse files
authored
[CIR] Cleanup cir.scopes with a single cir.yield operation (#1291)
Cleanup cir scope if it contains only yield operation Fixes: #455
1 parent cf09dbf commit 34bd74e

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

clang/lib/CIR/Dialect/Transforms/CIRCanonicalize.cpp

+10-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,16 @@ struct RemoveEmptyScope : public OpRewritePattern<ScopeOp> {
6262
LogicalResult match(ScopeOp op) const final {
6363
// TODO: Remove this logic once CIR uses MLIR infrastructure to remove
6464
// trivially dead operations
65-
return success(op.isEmpty());
65+
if (op.isEmpty()) {
66+
return success();
67+
}
68+
69+
Region *region = op.getRegions().front();
70+
if (region && region->getBlocks().front().getOperations().size() == 1) {
71+
return success(isa<YieldOp>(region->getBlocks().front().front()));
72+
}
73+
74+
return failure();
6675
}
6776

6877
void rewrite(ScopeOp op, PatternRewriter &rewriter) const final {

clang/test/CIR/Transforms/merge-cleanups.cir

+9
Original file line numberDiff line numberDiff line change
@@ -138,4 +138,13 @@ module {
138138
cir.return %0 : !cir.ptr<!s32i, addrspace(target<2>)>
139139
}
140140

141+
// Should remove scope with only yield
142+
cir.func @removeBlockWithScopeYeild(%arg0: !s32i) {
143+
cir.scope {
144+
cir.yield
145+
}
146+
cir.return
147+
}
148+
// CHECK: cir.func @removeBlockWithScopeYeild
149+
// CHECK-NEXT: cir.return
141150
}

0 commit comments

Comments
 (0)