[llvm-branch-commits] [clang] [CIR] Keep volatile and atomic return loads before cleanups (PR #242713)
via llvm-branch-commits
llvm-branch-commits at lists.llvm.org
Sat Nov 14 14:13:14 PST 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clangir
@llvm/pr-subscribers-clang
Author: Henrich Lauko (xlauko)
<details>
<summary>Changes</summary>
When FlattenCFG routes a `cir.return` through a cleanup, it sinks a load of an entry-block alloca that feeds the return into the block after the cleanup. For a volatile or atomic load this moved the access past the cleanup code. Such a load is now spilled to a temporary before the cleanup, like any other return operand.
Assisted-by: Claude Code (claude-opus-5-5).
---
Full diff: https://github.com/llvm/llvm-project/pull/242713.diff
2 Files Affected:
- (modified) clang/lib/CIR/Dialect/Transforms/FlattenCFG.cpp (+4)
- (modified) clang/test/CIR/Transforms/flatten-cleanup-scope-multi-exit.cir (+27)
``````````diff
diff --git a/clang/lib/CIR/Dialect/Transforms/FlattenCFG.cpp b/clang/lib/CIR/Dialect/Transforms/FlattenCFG.cpp
index 8a54faaee5129..a45b88f156b70 100644
--- a/clang/lib/CIR/Dialect/Transforms/FlattenCFG.cpp
+++ b/clang/lib/CIR/Dialect/Transforms/FlattenCFG.cpp
@@ -1188,6 +1188,10 @@ class CIRCleanupScopeOpFlattening
return false;
if (auto loadOp = mlir::dyn_cast<cir::LoadOp>(defOp)) {
+ // A volatile or atomic load must happen before the cleanup runs.
+ if (loadOp.isVolatileOrAtomic())
+ return false;
+
// Only attempt to move loads of allocas in the entry block.
mlir::Value ptr = loadOp.getAddr();
auto funcOp = returnOp->getParentOfType<cir::FuncOp>();
diff --git a/clang/test/CIR/Transforms/flatten-cleanup-scope-multi-exit.cir b/clang/test/CIR/Transforms/flatten-cleanup-scope-multi-exit.cir
index 62413b2c9c1ce..070822210e67c 100644
--- a/clang/test/CIR/Transforms/flatten-cleanup-scope-multi-exit.cir
+++ b/clang/test/CIR/Transforms/flatten-cleanup-scope-multi-exit.cir
@@ -1280,3 +1280,30 @@ cir.func @test_goto_in_nested_cleanup() {
// CHECK: cir.br ^[[END]]
// CHECK: ^[[END]]:
// CHECK: cir.return
+
+// A volatile load of the returned value must happen before the cleanup runs,
+// so it is spilled to a temporary instead of being sunk past the cleanup.
+// CHECK-LABEL: cir.func @test_return_volatile_load(
+// CHECK: %[[TMP:.*]] = cir.alloca "__ret_operand_tmp"
+// CHECK: %[[OBJ:.*]] = cir.alloca "c"
+// CHECK: %[[X:.*]] = cir.alloca "x"
+// CHECK: %[[VAL:.*]] = cir.load volatile %[[X]]
+// CHECK-NEXT: cir.store %[[VAL]], %[[TMP]]
+// CHECK: cir.call @dtor(%[[OBJ]])
+// CHECK: %[[RET:.*]] = cir.load %[[TMP]]
+// CHECK-NEXT: cir.return %[[RET]] : !s32i
+cir.func @test_return_volatile_load(%arg0: !s32i) -> !s32i {
+ %0 = cir.alloca "c" align(4) init : !cir.ptr<!rec_SomeClass>
+ %1 = cir.alloca "x" align(4) init : !cir.ptr<!s32i>
+ cir.store %arg0, %1 : !s32i, !cir.ptr<!s32i>
+ cir.call @ctor(%0) : (!cir.ptr<!rec_SomeClass>) -> ()
+ cir.cleanup.scope {
+ %2 = cir.load volatile %1 : !cir.ptr<!s32i>, !s32i
+ cir.return %2 : !s32i
+ } cleanup normal {
+ cir.call @dtor(%0) : (!cir.ptr<!rec_SomeClass>) -> ()
+ cir.yield
+ }
+ %3 = cir.const #cir.int<0> : !s32i
+ cir.return %3 : !s32i
+}
``````````
</details>
https://github.com/llvm/llvm-project/pull/242713
More information about the llvm-branch-commits
mailing list