[clang] [CIR] Make cir.condition exit to the loop, not to itself (PR #240971)
via cfe-commits
cfe-commits at lists.llvm.org
Tue Nov 10 11:48:29 PST 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: Henrich Lauko (xlauko)
<details>
<summary>Changes</summary>
The loop-exit successor of `cir.condition` was `RegionSuccessor(getOperation())`, which names the condition op instead of the loop around it. An operation successor names the op that control resumes after, and the loop's own `getSuccessorRegions` names the loop for the same edge. MLIR's dataflow analyses use that op. DeadCodeAnalysis recorded the exit as a predecessor of the point after `cir.condition`, so dense analyses never saw control leave a `cir.while`, `cir.for` or `cir.do` without a cleanup region. The forward state after such a loop kept its initial value, and the backward state at the condition ignored the code after the loop. The fix names the loop. The control-flow unit test hid the mismatch because it compared op and terminator successors by region only. It now compares whole successors, so it fails without the fix.
For example, in this function:
```mlir
cir.func @<!-- -->f(%c : !cir.bool, %p : !cir.ptr<!s32i>, %v : !s32i) -> !s32i {
cir.store %v, %p : !s32i, !cir.ptr<!s32i>
cir.while {
cir.condition(%c)
} do {
cir.yield
}
%x = cir.load %p : !cir.ptr<!s32i>, !s32i
cir.return %x : !s32i
}
```
the only exit from `cir.while` is the false edge of `cir.condition`. DeadCodeAnalysis recorded that edge at the end of the condition region, so the point after `cir.while` had no predecessors. A forward dense analysis such as last-modified never joined any state into that point. At the `cir.load` it reported nothing, not the `cir.store` before the loop. With the fix the edge reaches the point after `cir.while`, and the store reaches the load.
Assisted-by: Claude Code (claude-opus-5-5).
---
Full diff: https://github.com/llvm/llvm-project/pull/240971.diff
2 Files Affected:
- (modified) clang/lib/CIR/Dialect/IR/CIRDialect.cpp (+1-1)
- (modified) clang/unittests/CIR/ControlFlowTest.cpp (+11-9)
``````````diff
diff --git a/clang/lib/CIR/Dialect/IR/CIRDialect.cpp b/clang/lib/CIR/Dialect/IR/CIRDialect.cpp
index 0df0893b5757ce..cdb72201224c4d 100644
--- a/clang/lib/CIR/Dialect/IR/CIRDialect.cpp
+++ b/clang/lib/CIR/Dialect/IR/CIRDialect.cpp
@@ -829,7 +829,7 @@ void cir::ConditionOp::getSuccessorRegions(
if (mlir::Region *cleanup = loopOp.maybeGetCleanup())
regions.emplace_back(cleanup);
else
- regions.emplace_back(getOperation());
+ regions.emplace_back(loopOp.getOperation());
return;
}
diff --git a/clang/unittests/CIR/ControlFlowTest.cpp b/clang/unittests/CIR/ControlFlowTest.cpp
index 92fa33b16708ac..07cd2e3c2ace9a 100644
--- a/clang/unittests/CIR/ControlFlowTest.cpp
+++ b/clang/unittests/CIR/ControlFlowTest.cpp
@@ -95,7 +95,8 @@ static void expectTerminatorSuccessors(Region ®ion,
/// Verify control flow interface consistency beyond what mlir::verify checks:
/// - Every non-empty region is reachable
/// - Every non-empty region has at least one branching terminator
-/// - Op and terminator agree on successor regions at every branch point
+/// - Op and terminator agree on successors at every branch point, including
+/// the op that an exit leaves
static void verifyControlFlowInterfaceConsistency(RegionBranchOpInterface op) {
EXPECT_TRUE(succeeded(mlir::verify(op)))
<< "MLIR verifier failed for '" << op->getName().getStringRef().str()
@@ -120,14 +121,15 @@ static void verifyControlFlowInterfaceConsistency(RegionBranchOpInterface op) {
regionsWithExit.insert(term->getParentRegion());
SmallVector<RegionSuccessor> termSuccessors;
term.getSuccessorRegions(/*operands=*/{}, termSuccessors);
- SmallPtrSet<Region *, 4> opSet, termSet;
- for (RegionSuccessor &s : opSuccessors)
- opSet.insert(s.getSuccessor());
- for (RegionSuccessor &s : termSuccessors)
- termSet.insert(s.getSuccessor());
- EXPECT_EQ(opSet, termSet)
- << "op and terminator disagree on successors from region #"
- << term->getParentRegion()->getRegionNumber();
+ unsigned from = term->getParentRegion()->getRegionNumber();
+ for (const RegionSuccessor &s : termSuccessors)
+ EXPECT_TRUE(llvm::is_contained(opSuccessors, s))
+ << "terminator of region #" << from << " reports successor " << s
+ << " that the op does not";
+ for (const RegionSuccessor &s : opSuccessors)
+ EXPECT_TRUE(llvm::is_contained(termSuccessors, s))
+ << "op reports successor " << s << " from region #" << from
+ << " that the terminator does not";
}
for (Region ®ion : op->getRegions()) {
``````````
</details>
https://github.com/llvm/llvm-project/pull/240971
More information about the cfe-commits
mailing list