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
11 changes: 10 additions & 1 deletion clang/lib/CIR/Dialect/Transforms/CIRCanonicalize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,16 @@ struct RemoveEmptyScope : public OpRewritePattern<ScopeOp> {
LogicalResult match(ScopeOp op) const final {
// TODO: Remove this logic once CIR uses MLIR infrastructure to remove
// trivially dead operations
return success(op.isEmpty());
if (op.isEmpty()) {
return success();
}

Region *region = op.getRegions().front();
if (region && region->getBlocks().front().getOperations().size() == 1) {
return success(isa<YieldOp>(region->getBlocks().front().front()));
}

return failure();
}

void rewrite(ScopeOp op, PatternRewriter &rewriter) const final {
Expand Down
9 changes: 9 additions & 0 deletions clang/test/CIR/Transforms/merge-cleanups.cir
Original file line number Diff line number Diff line change
Expand Up @@ -138,4 +138,13 @@ module {
cir.return %0 : !cir.ptr<!s32i, addrspace(target<2>)>
}

// Should remove scope with only yield
cir.func @removeBlockWithScopeYeild(%arg0: !s32i) {
cir.scope {
cir.yield
}
cir.return
}
// CHECK: cir.func @removeBlockWithScopeYeild
// CHECK-NEXT: cir.return
}
Loading