Issue 243044
Summary [flang] StackArrays promotes an allocation inside a nested stacksave/stackrestore pair, which the inner restore reclaims while live
Labels flang:fir-hlfir
Assignees
Reporter eugeneepshteyn
    Since 239312 (a93922b35b75), `canAddBlockStackScope` in `flang/lib/Optimizer/Transforms/StackArrays.cpp` accepts a loop block that already contains `llvm.intr.stacksave`/`llvm.intr.stackrestore` pairs. When the scan reaches an accepted `stacksave`, it records the pair and jumps straight to that save's `stackrestore`, so a pair nested inside it is never recorded. `findAllocaLoopInsertionPoint` then checks an allocation only against the outer pair. A `fir.allocmem` created inside the inner pair and freed between the inner and the outer restore is promoted to a `fir.alloca` with no stack scope of its own, and the inner `stackrestore` releases its storage while it is still live.

Reproducer, `nested.fir`:

```mlir
func.func private @extent() -> index
func.func private @init(!fir.heap<!fir.char<1,?>>)
func.func private @use(!fir.heap<!fir.char<1,?>>)
func.func private @usep(!fir.ref<!fir.char<1,?>>)
func.func @nested(%again: i1) {
  cf.br ^loop
^loop:
  %n = fir.call @extent() : () -> index
  %outer = llvm.intr.stacksave : !llvm.ptr
  %inner = llvm.intr.stacksave : !llvm.ptr
 %a = fir.allocmem !fir.char<1,?>(%n : index)
  fir.call @init(%a) : (!fir.heap<!fir.char<1,?>>) -> ()
  llvm.intr.stackrestore %inner : !llvm.ptr
  %b = fir.alloca !fir.char<1,?>(%n : index)
  fir.call @usep(%b) : (!fir.ref<!fir.char<1,?>>) -> ()
  fir.call @use(%a) : (!fir.heap<!fir.char<1,?>>) -> ()
  fir.freemem %a : !fir.heap<!fir.char<1,?>>
  llvm.intr.stackrestore %outer : !llvm.ptr
 cf.cond_br %again, ^loop, ^exit
^exit:
  return
}
```

`fir-opt nested.fir --allocation-placement` on main (e0b437836a21), loop block (`--stack-arrays` gives the same):

```mlir
  ^bb1:  // 2 preds: ^bb0, ^bb1
    %0 = fir.call @extent() : () -> index
    %1 = llvm.intr.stacksave : !llvm.ptr
    %2 = llvm.intr.stacksave : !llvm.ptr
    %3 = fir.alloca !fir.char<1,?>(%0 : index)
    %4 = fir.convert %3 : (!fir.ref<!fir.char<1,?>>) -> !fir.heap<!fir.char<1,?>>
    fir.call @init(%4) : (!fir.heap<!fir.char<1,?>>) -> ()
    llvm.intr.stackrestore %2 : !llvm.ptr
    %5 = fir.alloca !fir.char<1,?>(%0 : index)
    fir.call @usep(%5) : (!fir.ref<!fir.char<1,?>>) -> ()
    fir.call @use(%4) : (!fir.heap<!fir.char<1,?>>) -> ()
    llvm.intr.stackrestore %1 : !llvm.ptr
    cf.cond_br %arg0, ^bb1, ^bb2
```

`%3` lives between the inner save `%2` and its restore, but it is still used after that restore, and `%5` reuses its storage. With 239312 reverted, the `fir.allocmem`/`fir.freemem` pair is left on the heap.

Running it with a small C driver, where `init` writes 17 and `usep` writes 99:

```c
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
int64_t extent(void) { return 1024; }
void init(unsigned char *p) { for (int i = 0; i < 1024; ++i) p[i] = 17; }
void usep(unsigned char *p) { for (int i = 0; i < 1024; ++i) p[i] = 99; }
void use(unsigned char *p) { printf("%d\n", p[0]); }
void nested(bool);
int main(void) { nested(false); return 0; }
```

```console
$ fir-opt nested.fir --allocation-placement -o out.fir
$ flang -fc1 -x fir -emit-obj out.fir -o nested.o
$ clang driver.c nested.o -o nested && ./nested
99
```

The expected output is 17, which is what the same steps print with 239312 reverted. `--stack-arrays` gives the same results.

I have not found Fortran source that lowers to this shape: lowering attaches the stack restores and the temporaries' frees to statement contexts that unwind in last-in, first-out order, so the lifetimes it produces are nested. The pass contract is still broken, though, and the comment in `canAddBlockStackScope` that assumes "the pairs are properly nested" is not checked. Expected: the allocation in this shape stays on the heap (or the block is refused), as before 239312.

(This issue was filed with the help of AI)

_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to