Author: Henrich Lauko Date: 2026-11-16T21:01:40+01:00 New Revision: e0c9350a3f88ca5a37aaac4c7cc9caebf5c309e0
URL: https://github.com/llvm/llvm-project/commit/e0c9350a3f88ca5a37aaac4c7cc9caebf5c309e0 DIFF: https://github.com/llvm/llvm-project/commit/e0c9350a3f88ca5a37aaac4c7cc9caebf5c309e0.diff LOG: [CIR] Keep volatile and atomic return loads before cleanups (#242713) 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). Added: Modified: clang/lib/CIR/Dialect/Transforms/FlattenCFG.cpp clang/test/CIR/Transforms/flatten-cleanup-scope-multi-exit.cir Removed: ################################################################################ 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 +} _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
