| Issue |
239359
|
| Summary |
[SPARC] musttail call with stack arguments is silently lowered as a normal call
|
| Labels |
new issue
|
| Assignees |
|
| Reporter |
shalseth
|
On SPARC a `musttail` call that has arguments on the stack is lowered as an ordinary call followed by `ret`, without any error. Six arguments or fewer gives a real tail call. A seventh argument on sparc64, or any stack argument on 32-bit sparc, makes the tail call go away:
```llvm
declare i64 @callee7(i64, i64, i64, i64, i64, i64, i64)
define i64 @caller7(i64 %a, i64 %b, i64 %c, i64 %d, i64 %e, i64 %f, i64 %g) {
%r = musttail call i64 @callee7(i64 %a, i64 %b, i64 %c, i64 %d, i64 %e, i64 %f, i64 %g)
ret i64 %r
}
```
```
$ llc -mtriple=sparc64-unknown-linux-gnu -O2 musttail.ll -o -
caller7:
save %sp, -192, %sp
ldx [%fp+2223], %g2
mov %i5, %o5
mov %i4, %o4
mov %i3, %o3
mov %i2, %o2
mov %i1, %o1
mov %i0, %o0
call callee7
stx %g2, [%sp+2223]
ret
restore %g0, %o0, %o0
```
`-mtriple=sparc-unknown-linux-gnu` with i32 arguments gives the same shape. Since the frame stays live, mutual recursion through `musttail` grows the stack with every step. With the C version in ping.c/pong.c below (100M steps, 7 arguments), the gcc build prints 7 and the clang build segfaults:
```c
/* ping.c */
long pong(long, long, long, long, long, long, long);
long ping(long n, long b, long c, long d, long e, long f, long g)
{
if (n == 0)
return g;
__attribute__((musttail)) return pong(n - 1, b, c, d, e, f, g);
}
```
```c
/* pong.c */
#include <stdio.h>
long ping(long, long, long, long, long, long, long);
long pong(long n, long b, long c, long d, long e, long f, long g)
{
if (n == 0)
return g;
__attribute__((musttail)) return ping(n - 1, b, c, d, e, f, g);
}
int main(void)
{
printf("%ld\n", ping(100000000, 2, 3, 4, 5, 6, 7));
return 0;
}
```
```
$ gcc -O2 ping.c pong.c -o pp-gcc && ./pp-gcc
7
$ clang -O2 ping.c pong.c -o pp-clang && ./pp-clang
Segmentation fault
```
gcc 16.2 makes it a real tail call, because the seventh argument is already in the caller's incoming argument slot:
```
caller7:
save %sp, -176, %sp
call callee7, 0
restore
```
>From the source, `SparcTargetLowering::IsEligibleForTailCallOptimization()` refuses anything beyond the 48 bytes of register-argument save area on sparc64 (0 on sparc), and `LowerCall_64`/`LowerCall_32` then fall back to a normal call without checking `CLI.CB->isMustTailCall()`. As far as I can see, the other backends that can't honour `musttail` stop with "failed to perform tail call elimination on a call site marked musttail" instead.
I ran into this while looking at CPython's copy-and-patch JIT. Its stencils chain through `musttail` calls with seven arguments and depend on `preserve_none` to keep them in registers, and SPARC doesn't support `preserve_none`.
Seen with clang/llc 23.1.4 and llc 21.1.8 on sparc64 Linux (UltraSPARC T4-1), and with llc from main (a4bd6bd2e681). The tail-call code in SparcISelLowering.cpp is the same on main today.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs