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

via flang-commits flang-commits at lists.llvm.org
Sat Mar 7 18:10:03 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:

I added the requested expansive check. The test case, with the new `subroutine ss1` provided by @jeanPerier, passes with this addition.

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


More information about the flang-commits mailing list