[llvm-branch-commits] [clang] [CIR] Keep volatile and atomic return loads before cleanups (PR #242713)

Henrich Lauko via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Sat Nov 14 14:11:31 PST 2026


https://github.com/xlauko updated https://github.com/llvm/llvm-project/pull/242713

>From 6f22eb22d221b89c1fef1aa8a28ea96196fe02bb Mon Sep 17 00:00:00 2001
From: Henrich Lauko <hlauko at nvidia.com>
Date: Sat, 14 Nov 2026 22:09:13 +0000
Subject: [PATCH] [CIR] Keep volatile and atomic return loads before cleanups

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).
---
 .../lib/CIR/Dialect/Transforms/FlattenCFG.cpp |  4 +++
 .../flatten-cleanup-scope-multi-exit.cir      | 27 +++++++++++++++++++
 2 files changed, 31 insertions(+)

diff --git a/clang/lib/CIR/Dialect/Transforms/FlattenCFG.cpp b/clang/lib/CIR/Dialect/Transforms/FlattenCFG.cpp
index 8a54faaee5129b..a45b88f156b707 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 62413b2c9c1ce4..070822210e67ca 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
+}



More information about the llvm-branch-commits mailing list