[clang] 71fe5cb - [CIR] Keep a region's exit in its last block when merging blocks (#243774)

via cfe-commits cfe-commits at lists.llvm.org
Tue Nov 24 01:19:57 PST 2026


Author: Henrich Lauko
Date: 2026-11-24T10:19:51+01:00
New Revision: 71fe5cb13243fe6da73fb26f12508a1f0760715a

URL: https://github.com/llvm/llvm-project/commit/71fe5cb13243fe6da73fb26f12508a1f0760715a
DIFF: https://github.com/llvm/llvm-project/commit/71fe5cb13243fe6da73fb26f12508a1f0760715a.diff

LOG: [CIR] Keep a region's exit in its last block when merging blocks (#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` no longer merges the region's last block into a
predecessor that does not directly precede it when that block ends in a
region exit, so the exit keeps its position. Exit blocks elsewhere in
the region still merge, because merging them does not change which block
is last. 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).

Added: 
    clang/test/CIR/Transforms/canonicalize-region-exit.cir

Modified: 
    clang/lib/CIR/Dialect/IR/CIRDialect.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/CIR/Dialect/IR/CIRDialect.cpp b/clang/lib/CIR/Dialect/IR/CIRDialect.cpp
index e11419a11c9e9..8238023ac8d3e 100644
--- a/clang/lib/CIR/Dialect/IR/CIRDialect.cpp
+++ b/clang/lib/CIR/Dialect/IR/CIRDialect.cpp
@@ -2333,6 +2333,13 @@ 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. Keep a region
+  // exit in the last block, where CFG flattening and the cir.await verifier
+  // expect it.
+  if (src->getNextNode() != dst && dst == &dst->getParent()->back() &&
+      isa<RegionBranchTerminatorOpInterface>(dst->getTerminator()))
+    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..8341b43a06e18
--- /dev/null
+++ b/clang/test/CIR/Transforms/canonicalize-region-exit.cir
@@ -0,0 +1,113 @@
+// RUN: cir-opt %s -cir-canonicalize -o - | FileCheck %s
+// RUN: cir-opt %s -cir-canonicalize -cir-flatten-cfg -o - | FileCheck %s --check-prefix=FLAT
+
+!s32i = !cir.int<s, 32>
+
+module {
+  cir.func private @ext()
+
+  // The cir.yield block does not directly follow the branch, so merging it
+  // would move the exit out of 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:         ^[[EXIT]]:
+  // CHECK-NEXT:      cir.yield
+  // CHECK-NEXT:    }
+  // CHECK-NEXT:    cir.call @ext()
+
+  // FLAT-LABEL: cir.func @distant_yield()
+  // FLAT:         cir.label "L"
+  // FLAT:       ^{{bb[0-9]+}}:
+  // FLAT-NEXT:    cir.br ^[[AFTER:bb[0-9]+]]
+  // FLAT-NEXT:  ^[[AFTER]]:
+  // 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:         ^[[EXIT]]:
+  // CHECK-NEXT:      cir.condition(%arg0)
+  // CHECK-NEXT:    } do {
+
+  // FLAT-LABEL: cir.func @distant_condition(
+  // FLAT:         cir.brcond %arg0 ^{{bb[0-9]+}}, ^[[AFTER:bb[0-9]+]]
+  // FLAT:       ^[[AFTER]]:
+  // FLAT-NEXT:    cir.return
+
+  // A cir.yield block that is not the region's last one merges even into a
+  // predecessor that does not directly precede it.
+  cir.func @middle_yield(%arg0: !cir.bool) {
+    cir.while {
+      cir.condition(%arg0)
+    } do {
+      cir.br ^bb2
+    ^bb1:  // pred: ^bb1
+      cir.label "L"
+      cir.br ^bb1
+    ^bb2:  // pred: ^bb0
+      cir.yield
+    ^bb3:  // no predecessors
+      cir.label "M"
+      cir.yield
+    }
+    cir.return
+  }
+  // CHECK-LABEL: cir.func @middle_yield(
+  // CHECK:         } do {
+  // CHECK-NEXT:      cir.yield
+  // CHECK-NEXT:    ^{{bb[0-9]+}}:
+  // CHECK-NEXT:      cir.label "L"
+
+  // 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
+}


        


More information about the cfe-commits mailing list