https://github.com/xlauko updated https://github.com/llvm/llvm-project/pull/232217
>From b0589b5b3f60f3aa5e3b7769195b6ec71c5f0305 Mon Sep 17 00:00:00 2001 From: Henrich Lauko <[email protected]> Date: Thu, 15 Oct 2026 07:46:35 +0000 Subject: [PATCH] [CIR] Keep cir.cleanup.scope cleanup regions reachable RegionBranchOpInterface::getSuccessorRegions must return every region that may run next, but an unwind out of a call in the body, or a cir.return, cir.break, cir.continue or cir.goto out of it, runs the cleanup without passing a region branch point. A cleanup reached only that way had no predecessor, so generic passes treated it as dead, and -remove-dead-values erased an EH cleanup that stores to memory. Now the cleanup region is a successor of the parent and of every body exit, and a body exit that the cleanup kind does not cover can also leave the op. Co-Authored-By: Claude Opus 5.5 <[email protected]> --- clang/lib/CIR/Dialect/IR/CIRDialect.cpp | 36 ++++++------- .../remove-dead-values-cleanup-scope.cir | 33 ++++++++++++ clang/unittests/CIR/ControlFlowTest.cpp | 50 ++++++++++--------- 3 files changed, 79 insertions(+), 40 deletions(-) create mode 100644 clang/test/CIR/Transforms/remove-dead-values-cleanup-scope.cir diff --git a/clang/lib/CIR/Dialect/IR/CIRDialect.cpp b/clang/lib/CIR/Dialect/IR/CIRDialect.cpp index 84e7611335b534..d585a0c5e108fc 100644 --- a/clang/lib/CIR/Dialect/IR/CIRDialect.cpp +++ b/clang/lib/CIR/Dialect/IR/CIRDialect.cpp @@ -2096,31 +2096,33 @@ LogicalResult cir::CleanupScopeOp::verify() { void cir::CleanupScopeOp::getSuccessorRegions( mlir::RegionBranchPoint point, SmallVectorImpl<RegionSuccessor> ®ions) { - // Execution always starts in the body region. + // The interface has no branch point for the paths that run the cleanup from + // the middle of the body: an unwind out of a call, or a cir.return, + // cir.break, cir.continue or cir.goto. The cleanup region is therefore a + // potential successor of every branch point in the body as well as of the + // parent. The edge from the parent stands in for such a path taken before + // the body has any effect, and the edges from body exits for one taken + // after some of its effects. if (point.isParent()) { regions.emplace_back(&getBodyRegion()); + regions.emplace_back(&getCleanupRegion()); return; } mlir::Operation *term = point.getTerminatorPredecessorOrNull(); - assert(term && "expected a terminator predecessor"); - - if (term->getParentRegion() == &getBodyRegion()) { - // cir.resume unwinds out of the body. Every other terminator, such as - // cir.yield or cir.co_return, is a normal exit. - cir::CleanupKindAttr kind = getCleanupKindAttr(); - if (isa<cir::ResumeOp>(term) ? kind.isEH() : kind.isNormal()) { - regions.emplace_back(&getCleanupRegion()); - return; - } + if (term->getParentRegion() == &getCleanupRegion()) { + regions.emplace_back(getOperation()); + return; } - // Exiting the cleanup region, or a body exit that skips it, leaves the - // operation. An unwind out of the middle of the body is not a - // RegionBranchPoint, and neither are cir.return, cir.break, cir.continue or - // cir.goto, so a cleanup region reached only that way has no predecessor - // here. - regions.emplace_back(getOperation()); + regions.emplace_back(&getCleanupRegion()); + + // cir.resume unwinds out of the body. Every other terminator, such as + // cir.yield or cir.co_return, is a normal exit. An exit that the cleanup + // kind does not cover can also leave the operation directly. + cir::CleanupKindAttr kind = getCleanupKindAttr(); + if (isa<cir::ResumeOp>(term) ? !kind.isEH() : !kind.isNormal()) + regions.emplace_back(getOperation()); } LogicalResult cir::CleanupScopeOp::canonicalize(CleanupScopeOp op, diff --git a/clang/test/CIR/Transforms/remove-dead-values-cleanup-scope.cir b/clang/test/CIR/Transforms/remove-dead-values-cleanup-scope.cir new file mode 100644 index 00000000000000..5aebe22a04739a --- /dev/null +++ b/clang/test/CIR/Transforms/remove-dead-values-cleanup-scope.cir @@ -0,0 +1,33 @@ +// RUN: cir-opt %s -remove-dead-values -o - | FileCheck %s + +// The EH cleanup runs only when @may_throw unwinds, which is not a region +// branch point. The cleanup region must still count as reachable, or generic +// passes such as -remove-dead-values erase it. + +!s32i = !cir.int<s, 32> + +module { + cir.func private @may_throw() + + cir.func @eh_cleanup(%p : !cir.ptr<!s32i>) { + cir.cleanup.scope { + cir.call @may_throw() : () -> () + cir.yield + } cleanup eh { + %c = cir.const #cir.int<7> : !s32i + cir.store %c, %p : !s32i, !cir.ptr<!s32i> + cir.yield + } + cir.return + } + + // CHECK-LABEL: cir.func @eh_cleanup + // CHECK: cir.cleanup.scope { + // CHECK-NEXT: cir.call @may_throw() : () -> () + // CHECK-NEXT: cir.yield + // CHECK-NEXT: } cleanup eh { + // CHECK-NEXT: %[[C:.*]] = cir.const #cir.int<7> : !s32i + // CHECK-NEXT: cir.store %[[C]], %{{.*}} : !s32i, !cir.ptr<!s32i> + // CHECK-NEXT: cir.yield + // CHECK-NEXT: } +} diff --git a/clang/unittests/CIR/ControlFlowTest.cpp b/clang/unittests/CIR/ControlFlowTest.cpp index 1cad991d860b72..c7f6a7eee8cc55 100644 --- a/clang/unittests/CIR/ControlFlowTest.cpp +++ b/clang/unittests/CIR/ControlFlowTest.cpp @@ -80,13 +80,10 @@ static void expectTerminatorSuccessors(Region ®ion, } /// Verify control flow interface consistency beyond what mlir::verify checks: -/// - Every non-empty region is reachable, unless `requireAllReachable` is -/// false +/// - 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 -static void -verifyControlFlowInterfaceConsistency(RegionBranchOpInterface op, - bool requireAllReachable = true) { +static void verifyControlFlowInterfaceConsistency(RegionBranchOpInterface op) { EXPECT_TRUE(succeeded(mlir::verify(op))) << "MLIR verifier failed for '" << op->getName().getStringRef().str() << "'"; @@ -123,7 +120,7 @@ verifyControlFlowInterfaceConsistency(RegionBranchOpInterface op, for (Region ®ion : op->getRegions()) { if (region.empty()) continue; - EXPECT_TRUE(!requireAllReachable || allReachable.contains(®ion)) + EXPECT_TRUE(allReachable.contains(®ion)) << "Region #" << region.getRegionNumber() << " is non-empty but not reachable from any branch point"; EXPECT_TRUE(regionsWithExit.contains(®ion)) @@ -135,18 +132,21 @@ verifyControlFlowInterfaceConsistency(RegionBranchOpInterface op, } /// Check a cir.cleanup.scope whose body has a single exit. The parent enters -/// the body, the body exit enters the cleanup region if `bodyRunsCleanup` and -/// leaves the op otherwise, and the cleanup region leaves the op. +/// the body or the cleanup region. The body exit enters the cleanup region, +/// and also leaves the op unless `bodyRunsCleanup`. The cleanup region leaves +/// the op. static void expectCleanupScopeSuccessors(cir::CleanupScopeOp op, bool bodyRunsCleanup) { Region &body = op.getBodyRegion(); Region &cleanup = op.getCleanupRegion(); - expectSuccessors(op, RegionBranchPoint::parent(), {&body}); + expectSuccessors(op, RegionBranchPoint::parent(), {&body, &cleanup}); RegionBranchTerminatorOpInterface bodyTerm = getTerminator(body); ASSERT_TRUE(bodyTerm); - expectSuccessors(op, RegionBranchPoint(bodyTerm), - {bodyRunsCleanup ? &cleanup : nullptr}); + if (bodyRunsCleanup) + expectSuccessors(op, RegionBranchPoint(bodyTerm), {&cleanup}); + else + expectSuccessors(op, RegionBranchPoint(bodyTerm), {&cleanup, nullptr}); expectTerminatorSuccessors(cleanup, {nullptr}); RegionBranchOpInterface branch = op; @@ -154,10 +154,7 @@ static void expectCleanupScopeSuccessors(cir::CleanupScopeOp op, EXPECT_FALSE(branch.isRepetitiveRegion(1)); EXPECT_FALSE(branch.hasLoop()); - // Without the body edge nothing reaches the cleanup region. - verifyControlFlowInterfaceConsistency( - op, - /*requireAllReachable=*/bodyRunsCleanup); + verifyControlFlowInterfaceConsistency(op); } //===----------------------------------------------------------------------===// @@ -573,7 +570,8 @@ TEST_F(CIRControlFlowTest, CleanupScopeOpYieldEH) { cir.return } )CIR"); - // An EH-only cleanup is skipped by the body's normal cir.yield. + // An EH-only cleanup does not run on the body's normal cir.yield, but stays + // a successor for an unwind from the middle of the body. expectCleanupScopeSuccessors(findFirstOp<cir::CleanupScopeOp>(*module), /*bodyRunsCleanup=*/false); } @@ -609,7 +607,8 @@ TEST_F(CIRControlFlowTest, CleanupScopeOpResumeNormal) { cir.return } )CIR"); - // A normal-only cleanup is skipped by the body's unwinding cir.resume. + // A normal-only cleanup does not run on the body's unwinding cir.resume, but + // stays a successor for a cir.return or similar from the middle of the body. expectCleanupScopeSuccessors(findFirstOp<cir::CleanupScopeOp>(*module), /*bodyRunsCleanup=*/false); } @@ -637,8 +636,9 @@ TEST_F(CIRControlFlowTest, CleanupScopeOpMultiBlockBodyNormal) { )CIR"); auto cleanupScopeOp = findFirstOp<cir::CleanupScopeOp>(*module); - expectSuccessors(cleanupScopeOp, RegionBranchPoint::parent(), - {&cleanupScopeOp.getBodyRegion()}); + expectSuccessors( + cleanupScopeOp, RegionBranchPoint::parent(), + {&cleanupScopeOp.getBodyRegion(), &cleanupScopeOp.getCleanupRegion()}); // The cir.brcond block is not a branch point, so only the two exiting // blocks show up here. @@ -653,15 +653,19 @@ TEST_F(CIRControlFlowTest, CleanupScopeOpMultiBlockBodyNormal) { EXPECT_TRUE(isa<cir::YieldOp>(*bodyTerms[0])); EXPECT_TRUE(isa<cir::ResumeOp>(*bodyTerms[1])); - // The normal exit runs the cleanup, the unwind exit skips it. + // The normal exit runs the cleanup. The unwind exit can also leave the op. expectSuccessors(cleanupScopeOp, RegionBranchPoint(bodyTerms[0]), {&cleanupScopeOp.getCleanupRegion()}); - expectSuccessors(cleanupScopeOp, RegionBranchPoint(bodyTerms[1]), {nullptr}); + expectSuccessors(cleanupScopeOp, RegionBranchPoint(bodyTerms[1]), + {&cleanupScopeOp.getCleanupRegion(), nullptr}); expectTerminatorSuccessors(cleanupScopeOp.getCleanupRegion(), {nullptr}); - EXPECT_FALSE(asRegionBranch(cleanupScopeOp).hasLoop()); + // hasLoop() reports a loop whenever two exits reach the same region, so + // check that no region can reach itself instead. + RegionBranchOpInterface branch = asRegionBranch(cleanupScopeOp); + EXPECT_FALSE(branch.isRepetitiveRegion(0)); + EXPECT_FALSE(branch.isRepetitiveRegion(1)); - // The cleanup region does have a predecessor here, via the normal exit. verifyControlFlowInterfaceConsistency(cleanupScopeOp); } _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
