| Issue |
239456
|
| Summary |
[AArch64] Shrink-wrapping bug in tailcc functions with an early return.
|
| Labels |
new issue
|
| Assignees |
|
| Reporter |
pchiusano
|
Here is a short (I believe valid) IR program that segfaults with shrink-wrapping on, and which returns the correct result otherwise.
Here, `f` takes ten arguments, and so `%a8` and `%p9` arrive on the stack.
```llvm
define tailcc i64 @f(ptr %p0, i64 %a1, i64 %a2, ptr %p3, i64 %a4, ptr %p5, i64 %a6, ptr %p7, i64 %a8, ptr %p9) {
entry:
%done = icmp eq i64 %a1, %a2
br i1 %done, label %early, label %body
early:
ret i64 %a4
body:
%w1 = call i64 @work(i64 %a8, ptr %p9)
%w2 = call i64 @work(i64 %w1, ptr %p7)
%w3 = call i64 @work(i64 %w2, ptr %p5)
%s = add i64 %w3, %a6
ret i64 %s
}
define i64 @work(i64 %a, ptr %p) {
%r = add i64 %a, 1
ret i64 %r
}
define i64 @call_f(i64 %x) {
%r = call tailcc i64 @f(ptr null, i64 %x, i64 7, ptr null, i64 42, ptr null, i64 1, ptr null, i64 2, ptr null)
ret i64 %r
}
@fmt = private constant [12 x i8] c"result %ld\0A\00"
declare i32 @printf(ptr, ...)
define i32 @main() {
%r = call i64 @call_f(i64 7)
%p = call i32 (ptr, ...) @printf(ptr @fmt, i64 %r)
ret i32 0
}
```
```
llc -O2 -filetype=obj t.ll -o t.o && ld -o t t.o -lSystem -syslibroot "$(xcrun --show-sdk-path)" && ./t
```
Expected `result 42`. Actual: segmentation fault.
With `-enable-shrink-wrap=false` on the `llc` line, we get `result 42`.
Here is what seems to be happening: on AArch64, a tailcc function with stack arguments that returns on a path needing no frame returns without popping the stack arguments. This is because the pop is emitted only in the epilogue and shrink-wrapping places the epilogue only on paths that have a prologue. So the stack arguments don't get popped.
However the caller has adjusted for the pops, so its stack pointer is now off by the size of the stack arguments, and it restores `fp` and `lr` from the wrong slots.
`swifttailcc` behaves the same way. x86-64 is not affected: there the pop is done by the `ret` instruction on every return path.
Looking at the generated assembly -
`f`: the pop (`add sp, sp, #16`) is in the epilogue, and the early path (`b.eq LBB0_2`) reaches `ret` without it.
```
_f: ; @f
cmp x1, x2
mov x0, x4
b.eq LBB0_2
stp x22, x21, [sp, #-48]! ; 16-byte Folded Spill
stp x20, x19, [sp, #16] ; 16-byte Folded Spill
stp x29, x30, [sp, #32] ; 16-byte Folded Spill
ldp x0, x1, [sp, #48]
mov x21, x7
mov x19, x6
mov x20, x5
bl _work
mov x1, x21
bl _work
mov x1, x20
bl _work
add x0, x0, x19
ldp x29, x30, [sp, #32] ; 16-byte Folded Reload
ldp x20, x19, [sp, #16] ; 16-byte Folded Reload
ldp x22, x21, [sp], #48 ; 16-byte Folded Reload
add sp, sp, #16
LBB0_2: ; %common.ret
ret
```
`call_f` undoes the callee's pop with `sub sp, sp, #16` after the call. When the pop was skipped, `sp` is 16 bytes low, `ldp x29, x30, [sp, #16]` reads the stack argument slots, and `ret` jumps to their contents.
```
_call_f: ; @call_f
sub sp, sp, #32
stp x29, x30, [sp, #16] ; 16-byte Folded Spill
mov x1, x0
mov x0, xzr
mov x3, xzr
mov x5, xzr
mov x7, xzr
mov w8, #2 ; =0x2
mov w2, #7 ; =0x7
mov w4, #42 ; =0x2a
mov w6, #1 ; =0x1
stp x8, xzr, [sp]
bl _f
sub sp, sp, #16
ldp x29, x30, [sp, #16] ; 16-byte Folded Reload
add sp, sp, #32
ret
```
Seen with LLVM 23.1.2 (Homebrew) on macOS arm64 (`arm64-apple-macosx`). Nothing in the IR is Darwin-specific, though, so I'd imagine this will reproduce on Linux arm64 too.
This came up while working on the Unison JIT compiler. Unison is a functional language that uses tail calls as the only looping construct. I've worked around the issue by disabling shrink-wrapping.
Disclaimer: I used an LLM to generate this failing minimal example and I'm not any sort of LLVM expert. I hope this report is still helpful, and thanks for such a great project!
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs