| Issue |
237782
|
| Summary |
[IRNormalizer] `normalize` moves a dynamic alloca below `llvm.stacksave`, so `llvm.stackrestore` frees it
|
| Labels |
new issue
|
| Assignees |
|
| Reporter |
phongndo
|
### reproducer
godbolt: https://godbolt.org/z/Wbf3jr458 (trunk `opt -passes=normalize`, plus both versions executed)
```llvm
define i32 @main(i32 %argc, ptr %argv) {
%a = alloca i32, i32 %argc, align 4
%sp = call ptr @llvm.stacksave.p0()
store i32 42, ptr %a, align 4
call void @llvm.stackrestore.p0(ptr %sp)
%b = alloca i32, i32 %argc, align 4
store i32 7, ptr %b, align 4
%v = load i32, ptr %a, align 4
ret i32 %v
}
```
```console
$ opt -S -passes=normalize repro.ll
...
define i32 @main(i32 %a0, ptr %a1) {
bb64766:
%"vl31633llvm.stacksave.p0()" = call ptr @llvm.stacksave.p0()
%"vl62869(%a0)" = alloca i32, i32 %a0, align 4
store i32 42, ptr %"vl62869(%a0)", align 4
call void @llvm.stackrestore.p0(ptr %"vl31633llvm.stacksave.p0()")
%"vl88346(%a0)" = alloca i32, i32 %a0, align 4
store i32 7, ptr %"vl88346(%a0)", align 4
%"op12466(vl62869)" = load i32, ptr %"vl62869(%a0)", align 4
ret i32 %"op12466(vl62869)"
}
$ clang -O0 repro.ll -o before && ./before; echo $?
42
$ opt -S -passes=normalize repro.ll | clang -O0 -x ir - -o after && ./after; echo $?
7
```
`%a` is allocated before the `llvm.stacksave`, but `normalize` moves it below. LangRef: `llvm.stackrestore` "pops any alloca blocks from the stack that were allocated after the `llvm.stacksave` was executed". So the restore now frees `%a`, `%b` reuses its slot, and the final load reads freed stack memory.
Clang emits this pattern for C variable-length arrays in nested scopes:
```c
int printf(const char *, ...);
__attribute__((noinline)) int f(int n) {
int a[n]; /* lives for the whole function */
{
int b[n]; /* freed at the end of this block */
b[0] = 1;
a[0] = 42;
}
{
int c[2 * n]; /* reuses the space freed above */
for (int i = 0; i < 2 * n; i++) c[i] = 7;
if (c[0] != 7) return -1;
}
return a[0];
}
int main(void) {
printf("f(4) = %d\n", f(4));
return 0;
}
```
```console
$ clang -O0 -Xclang -disable-O0-optnone -S -emit-llvm vla.c -o vla.ll
$ clang -O0 vla.ll -o before && ./before
f(4) = 42
$ opt -S -passes=normalize vla.ll | clang -O0 -x ir - -o after && ./after
f(4) = 7
```
The alloca for `a` sinks below the inner block's `llvm.stacksave`, to just before `a[0] = 42`, and the inner `llvm.stackrestore` frees it.
Neither of the usual checkers catches this. `llubi` stops with `Unrecognized instruction: ... @llvm.stacksave.p0()`. Alive2 reports "Transformation seems to be correct!" because it does not model `llvm.stackrestore` freeing allocas.
### suggested fix
`IRNormalizer::reorderInstructions` starts its top-down walk from instructions that are outputs, may read or write memory, or are terminators, and moves every other instruction to just before its first user in the block ([IRNormalizer.cpp#L434](https://github.com/llvm/llvm-project/blob/5c7a463f9eb083009d32b67aea972e4b8432cd23/llvm/lib/Transforms/Utils/IRNormalizer.cpp#L434)). An `alloca` is none of those, and nothing in the IR ties a dynamic alloca to the `llvm.stacksave` that follows it ([Intrinsics.td](https://github.com/llvm/llvm-project/blob/5c7a463f9eb083009d32b67aea972e4b8432cd23/llvm/include/llvm/IR/Intrinsics.td#L1048-L1049): stacksave/stackrestore are marked as writing memory "because we don't otherwise model their dependencies on allocas").
Treat allocas with `!AI->isStaticAlloca()` as sources too, so they keep their position relative to `llvm.stacksave` / `llvm.stackrestore`, which are already sources. This covers a fixed-size alloca outside the entry block too, since that is also dynamic. Static allocas can keep moving. InstCombine refuses to sink allocas for the same reason ([InstructionCombining.cpp#L5640-L5645](https://github.com/llvm/llvm-project/blob/5c7a463f9eb083009d32b67aea972e4b8432cd23/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp#L5640-L5645): "dynamic allocas must not be sunk in between a stacksave / stackrestore pair, which would incorrectly shorten its lifetime").
This is the same class of bug as #236128 (fixed by #237128): an ordering dependence that has no SSA edge. I found it while working on that fix, and I have a patch ready that I'll send shortly.
> Disclaimer: This report was written with LLM assistance. Every result above was reproduced on trunk.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs