https://github.com/xlauko created https://github.com/llvm/llvm-project/pull/243774
`cir.br` canonicalization merges a block into its only predecessor. If that block is the last one in its region and ends in a region exit such as `cir.yield` or `cir.condition`, merging it into a predecessor that does not directly precede it moves the exit out of the last block. FlattenCFG reads the exit of `cir.scope`, `cir.if`, loop condition and several other regions from the last block, and the `cir.await` verifier requires it there. After `-cir-canonicalize -cir-flatten-cfg`, the flattened function either failed to verify or silently lost the code after the region, and a loop condition region hit an assertion in FlattenCFG. `BrOp::canonicalize` now merges a block that ends in a region exit only when it directly follows the branch, so the exit keeps its position. Making every flattener search all blocks for exits would also work. It would relax an invariant that those flatteners and the verifier rely on, while one check in the merge keeps it. The tests use hand-written CIR. Assisted-by: Claude Code (claude-opus-5-5). >From 9a259fc985ee855b0fd9505ea6ab3d28e12dad10 Mon Sep 17 00:00:00 2001 From: Henrich Lauko <[email protected]> Date: Wed, 18 Nov 2026 09:41:25 +0000 Subject: [PATCH] [CIR] Keep a region's exit in its last block when merging blocks cir.br canonicalization merges a block into its only predecessor. If that block is the last one in its region and ends in a region exit such as cir.yield or cir.condition, merging it into a predecessor that does not directly precede it moves the exit out of the last block. FlattenCFG reads the exit of cir.scope, cir.if, loop condition and several other regions from the last block, and the cir.await verifier requires it there. After -cir-canonicalize -cir-flatten-cfg, the flattened function either failed to verify or silently lost the code after the region, and a loop condition region hit an assertion in FlattenCFG. BrOp::canonicalize now merges a block that ends in a region exit only when it directly follows the branch, so the exit keeps its position. Making every flattener search all blocks for exits would also work. It would relax an invariant that those flatteners and the verifier rely on, while one check in the merge keeps it. The tests use hand-written CIR. Assisted-by: Claude Code (claude-opus-5-5). --- clang/lib/CIR/Dialect/IR/CIRDialect.cpp | 9 ++ .../Transforms/canonicalize-region-exit.cir | 118 ++++++++++++++++++ 2 files changed, 127 insertions(+) create mode 100644 clang/test/CIR/Transforms/canonicalize-region-exit.cir diff --git a/clang/lib/CIR/Dialect/IR/CIRDialect.cpp b/clang/lib/CIR/Dialect/IR/CIRDialect.cpp index 7e511dbaddff0..b809291b3648b 100644 --- a/clang/lib/CIR/Dialect/IR/CIRDialect.cpp +++ b/clang/lib/CIR/Dialect/IR/CIRDialect.cpp @@ -2322,6 +2322,15 @@ LogicalResult cir::BrOp::canonicalize(BrOp op, PatternRewriter &rewriter) { if (isa<cir::LabelOp, cir::IndirectBrOp>(dst->front())) return failure(); + // The merged block takes the source's place in the region. If the + // destination ends in a region exit, such as cir.yield or cir.condition, + // merge only when the source directly precedes it, so the exit keeps its + // position. Several CFG flattening patterns and the cir.await verifier + // expect the exit in the region's last block. + if (isa<RegionBranchTerminatorOpInterface>(dst->getTerminator()) && + src->getNextNode() != dst) + return failure(); + // An operand that is an argument of the destination itself (possible in an // unreachable cycle) would be replaced by itself, leaving its other uses // dangling once the destination is erased. diff --git a/clang/test/CIR/Transforms/canonicalize-region-exit.cir b/clang/test/CIR/Transforms/canonicalize-region-exit.cir new file mode 100644 index 0000000000000..f766a6acd9257 --- /dev/null +++ b/clang/test/CIR/Transforms/canonicalize-region-exit.cir @@ -0,0 +1,118 @@ +// RUN: cir-opt %s -cir-canonicalize -o - | FileCheck %s +// RUN: cir-opt %s -cir-canonicalize -cir-flatten-cfg -o - | FileCheck %s --check-prefix=FLAT + +// Merging a block that ends in a region exit into a predecessor that does not +// directly precede it would move the exit out of the region's last block, where +// CFG flattening looks for it. + +!s32i = !cir.int<s, 32> + +module { + cir.func private @ext() + + // The cir.yield block does not directly follow the branch, so it stays the + // scope's last block. After flattening, the scope exit reaches the call + // after the scope instead of acting as the cleanup scope's exit. + cir.func @distant_yield() { + cir.cleanup.scope { + cir.scope { + cir.br ^bb2 + ^bb1: // pred: ^bb1 + cir.label "L" + cir.br ^bb1 + ^bb2: // pred: ^bb0 + cir.yield + } + cir.call @ext() : () -> () + cir.yield + } cleanup normal { + cir.call @ext() : () -> () + cir.yield + } + cir.return + } + // CHECK-LABEL: cir.func @distant_yield() + // CHECK: cir.scope { + // CHECK-NEXT: cir.br ^[[EXIT:bb[0-9]+]] + // CHECK-NEXT: ^[[LOOP:bb[0-9]+]]: + // CHECK-NEXT: cir.label "L" + // CHECK-NEXT: cir.br ^[[LOOP]] + // CHECK-NEXT: ^[[EXIT]]: + // CHECK-NEXT: cir.yield + // CHECK-NEXT: } + // CHECK-NEXT: cir.call @ext() + + // FLAT-LABEL: cir.func @distant_yield() + // FLAT-NEXT: cir.br ^[[BODY:bb[0-9]+]] + // FLAT-NEXT: ^[[BODY]]: + // FLAT-NEXT: cir.br ^[[SCOPE:bb[0-9]+]] + // FLAT-NEXT: ^[[SCOPE]]: + // FLAT-NEXT: cir.br ^[[EXIT:bb[0-9]+]] + // FLAT-NEXT: ^[[LOOP:bb[0-9]+]]: + // FLAT-NEXT: cir.label "L" + // FLAT-NEXT: cir.br ^[[LOOP]] + // FLAT-NEXT: ^[[EXIT]]: + // FLAT-NEXT: cir.br ^[[AFTER:bb[0-9]+]] + // FLAT-NEXT: ^[[AFTER]]: + // FLAT-NEXT: cir.call @ext() + // FLAT-NEXT: cir.br ^[[CLEANUP:bb[0-9]+]] + // FLAT-NEXT: ^[[CLEANUP]]: + // FLAT-NEXT: cir.call @ext() + + // The same holds for the cir.condition that ends a loop condition region. + cir.func @distant_condition(%arg0: !cir.bool) { + cir.while { + cir.br ^bb2 + ^bb1: // pred: ^bb1 + cir.label "L" + cir.br ^bb1 + ^bb2: // pred: ^bb0 + cir.condition(%arg0) + } do { + cir.call @ext() : () -> () + cir.yield + } + cir.return + } + // CHECK-LABEL: cir.func @distant_condition( + // CHECK-NEXT: cir.while { + // CHECK-NEXT: cir.br ^[[EXIT:bb[0-9]+]] + // CHECK-NEXT: ^[[LOOP:bb[0-9]+]]: + // CHECK-NEXT: cir.label "L" + // CHECK-NEXT: cir.br ^[[LOOP]] + // CHECK-NEXT: ^[[EXIT]]: + // CHECK-NEXT: cir.condition(%arg0) + // CHECK-NEXT: } do { + + // FLAT-LABEL: cir.func @distant_condition( + // FLAT-NEXT: cir.br ^[[COND:bb[0-9]+]] + // FLAT-NEXT: ^[[COND]]: + // FLAT-NEXT: cir.br ^[[EXIT:bb[0-9]+]] + // FLAT-NEXT: ^[[LOOP:bb[0-9]+]]: + // FLAT-NEXT: cir.label "L" + // FLAT-NEXT: cir.br ^[[LOOP]] + // FLAT-NEXT: ^[[EXIT]]: + // FLAT-NEXT: cir.brcond %arg0 ^[[BODY:bb[0-9]+]], ^[[AFTER:bb[0-9]+]] + // FLAT-NEXT: ^[[BODY]]: + // FLAT-NEXT: cir.call @ext() + // FLAT-NEXT: cir.br ^[[COND]] + // FLAT-NEXT: ^[[AFTER]]: + // FLAT-NEXT: cir.return + + // The cir.yield block directly follows the branch, so the merged block is + // still the scope's last one. + cir.func @next_yield() { + cir.scope { + cir.call @ext() : () -> () + cir.br ^bb1 + ^bb1: // pred: ^bb0 + cir.yield + } + cir.return + } + // CHECK-LABEL: cir.func @next_yield() + // CHECK-NEXT: cir.scope { + // CHECK-NEXT: cir.call @ext() + // CHECK-NEXT: } + // CHECK-NEXT: cir.return +} _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
