https://github.com/xlauko created https://github.com/llvm/llvm-project/pull/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. CIRGen never starts an entry block with a label, so the test uses hand-written CIR. Assisted-by: Claude Code (claude-opus-5-5). >From f1d8e9b2f55422b16d6a17c5ed0ab3e77e929529 Mon Sep 17 00:00:00 2001 From: Henrich Lauko <[email protected]> Date: Wed, 18 Nov 2026 09:42:28 +0000 Subject: [PATCH] [CIR] Do not inline a cleanup scope body that starts with a label 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. CIRGen never starts an entry block with a label, so the test uses hand-written CIR. Assisted-by: Claude Code (claude-opus-5-5). --- clang/lib/CIR/Dialect/IR/CIRDialect.cpp | 5 ++++ .../Transforms/canonicalize-cleanup-scope.cir | 24 +++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/clang/lib/CIR/Dialect/IR/CIRDialect.cpp b/clang/lib/CIR/Dialect/IR/CIRDialect.cpp index 7e511dbaddff09..f7bc7e298413a6 100644 --- a/clang/lib/CIR/Dialect/IR/CIRDialect.cpp +++ b/clang/lib/CIR/Dialect/IR/CIRDialect.cpp @@ -2264,6 +2264,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 67be879d73b9de..0a264a08275e1f 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 { _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
