[clang] 0ee4054 - [CIR] Do not inline a cleanup scope body that starts with a label (#243775)
via cfe-commits
cfe-commits at lists.llvm.org
Tue Nov 24 08:54:40 PST 2026
Author: Henrich Lauko
Date: 2026-11-24T17:54:34+01:00
New Revision: 0ee4054e052752ce1dfc4f182ba27d83e75c9f49
URL: https://github.com/llvm/llvm-project/commit/0ee4054e052752ce1dfc4f182ba27d83e75c9f49
DIFF: https://github.com/llvm/llvm-project/commit/0ee4054e052752ce1dfc4f182ba27d83e75c9f49.diff
LOG: [CIR] Do not inline a cleanup scope body that starts with a label (#243775)
`cir.cleanup.scope` canonicalization inlines a single-block body in
front of the scope when its cleanup region is trivial. If the body
starts with a `cir.label` and other operations precede the scope, the
label lands in the middle of a block and verification fails with "must
be the first operation in a block". The canonicalizer now keeps a body
that starts with a label in its scope. Splitting the parent block would
also work, but it adds a block and a branch for IR that CIRGen does not
produce.
Assisted-by: Claude Code (claude-opus-5-5).
Added:
Modified:
clang/lib/CIR/Dialect/IR/CIRDialect.cpp
clang/test/CIR/Transforms/canonicalize-cleanup-scope.cir
Removed:
################################################################################
diff --git a/clang/lib/CIR/Dialect/IR/CIRDialect.cpp b/clang/lib/CIR/Dialect/IR/CIRDialect.cpp
index 8238023ac8d3e..f7f55c754a012 100644
--- a/clang/lib/CIR/Dialect/IR/CIRDialect.cpp
+++ b/clang/lib/CIR/Dialect/IR/CIRDialect.cpp
@@ -2275,6 +2275,11 @@ LogicalResult cir::CleanupScopeOp::canonicalize(CleanupScopeOp op,
if (!isa<cir::YieldOp>(bodyBlock.getTerminator()))
return failure();
+ // A cir.label must start its block, and inlining could put it behind other
+ // operations, so keep a body that starts with one.
+ if (isa<cir::LabelOp>(bodyBlock.front()))
+ return failure();
+
Operation *yield = bodyBlock.getTerminator();
rewriter.inlineBlockBefore(&bodyBlock, op);
rewriter.eraseOp(yield);
diff --git a/clang/test/CIR/Transforms/canonicalize-cleanup-scope.cir b/clang/test/CIR/Transforms/canonicalize-cleanup-scope.cir
index 67be879d73b9d..0a264a08275e1 100644
--- a/clang/test/CIR/Transforms/canonicalize-cleanup-scope.cir
+++ b/clang/test/CIR/Transforms/canonicalize-cleanup-scope.cir
@@ -138,6 +138,30 @@ cir.func @trivial_cleanup_multi_ops(%arg0: !cir.ptr<!rec_S>) {
// CHECK-NEXT: cir.call @dtor
// CHECK-NEXT: cir.return
+// Trivial cleanup, but the body starts with a label, which must start a
+// block. The body stays in its scope.
+cir.func @trivial_cleanup_label_body_kept() {
+ cir.call @side_effect() : () -> ()
+ cir.cleanup.scope {
+ cir.label "L"
+ cir.call @side_effect() : () -> ()
+ cir.yield
+ } cleanup normal {
+ cir.yield
+ }
+ cir.return
+}
+// CHECK-LABEL: @trivial_cleanup_label_body_kept
+// CHECK-NEXT: cir.call @side_effect()
+// CHECK-NEXT: cir.cleanup.scope {
+// CHECK-NEXT: cir.label "L"
+// CHECK-NEXT: cir.call @side_effect()
+// CHECK-NEXT: cir.yield
+// CHECK-NEXT: } cleanup normal {
+// CHECK-NEXT: cir.yield
+// CHECK-NEXT: }
+// CHECK-NEXT: cir.return
+
// Nested: inner EH with empty body erased, outer kept.
cir.func @nested_inner_eh_erased(%arg0: !cir.ptr<!rec_S>) {
cir.cleanup.scope {
More information about the cfe-commits
mailing list