[flang-commits] [flang] [flang] Fix stack-arrays pass moving alloca across stackrestore scope (PR #184727)

via flang-commits flang-commits at lists.llvm.org
Thu Mar 5 08:39:20 PST 2026


================
@@ -668,6 +668,25 @@ InsertionPoint AllocMemConversion::findAllocaInsertionPoint(
     // there were value operands to the allocmem so insert after the last one
     LLVM_DEBUG(llvm::dbgs()
                << "--Placing after last operand: " << *lastOperand << "\n");
+    // Check we aren't moving across a stackrestore scope boundary.
+    // The last operand may have been defined in an earlier stacksave/
+    // stackrestore scope (e.g. due to CSE merging identical size computations,
+    // or HLFIR-to-FIR lowering reusing a single size value across scopes).
+    // Placing the alloca at the operand's location would put it in the wrong
+    // scope, where it gets reclaimed before the allocmem's actual use.
+    // Fall back to the allocmem's own location, matching the "operand declared
+    // in a different block" bail-out logic above.
+    if (lastOperand->getBlock() == oldAlloc->getBlock()) {
----------------
tmjbios wrote:

Eeehhhhh yeah....lemme rework this a bit. While this PR as-is does solve the initial Issue, that first test case you provided demonstrates the hole in this solution & shows the failure still, so a more complete search is needed. I think I can have something later tonight that will be more complete.

https://github.com/llvm/llvm-project/pull/184727


More information about the flang-commits mailing list