[clang] [CIR] Load return values before cleanups that can change them (PR #244202)
Andy Kaylor via cfe-commits
cfe-commits at lists.llvm.org
Fri Nov 20 09:50:06 PST 2026
================
@@ -1167,11 +1167,42 @@ class CIRCleanupScopeOpFlattening
collectExitsInCleanup(cleanupBodyRegion, /*ignoreBreak=*/false);
}
+ // Whether the slot \p alloca read by \p loadOp may change before the end of
+ // \p cleanupRegion: its address escapes, or a store or lifetime marker on it
+ // runs after the load, in the load's block or in the cleanup.
+ static bool slotMayChangeBeforeCleanupEnd(cir::AllocaOp alloca,
+ cir::LoadOp loadOp,
+ mlir::Region &cleanupRegion) {
+ mlir::Block *loadBlock = loadOp->getBlock();
+ return llvm::any_of(alloca->getUsers(), [&](mlir::Operation *user) {
+ // Reads never change the slot.
+ if (mlir::isa<cir::LoadOp>(user))
+ return false;
+
+ // Storing the address, or using it in anything but a store to the slot
+ // or a lifetime marker, lets it escape.
+ if (auto storeOp = mlir::dyn_cast<cir::StoreOp>(user)) {
+ if (storeOp.getValue() == alloca.getResult())
+ return true;
+ } else if (!mlir::isa<cir::LifetimeStartOp, cir::LifetimeEndOp>(user)) {
+ return true;
----------------
andykaylor wrote:
So, any lifetime begin/end blocks the sinking? I guess the lifetime end would always have to be between the load and the return, but it seems like this is something we could handle better. Or maybe it's just as well to leave it up to the optimizer.
https://github.com/llvm/llvm-project/pull/244202
More information about the cfe-commits
mailing list