llvm / llvm/llvm-project

[RISC-V] -fstack-usage underreports when there is a func taking a long list of arguments after commit e246d16

Open
#189,085 8 comments 0 reactions 0 assignees View on GitHub
backend:RISC-V
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

We noticed a breakage by https://github.com/llvm/llvm-project/commit/e246d1615ea037f2dd92527ea1669b070c8e0993
The commit itself doesn't feel like a culprit, more like a trigger.
After this commit, the `-fstack-usage` reporting would be underestimating in some senario.

Created this test program: https://gist.github.com/zinechant/bd9850f0c54f4eb0267d006f37607749

```
clang -c -fstack-usage -mabi=lp64d -mcmodel=medany -Wall -ffunction-sections -fdata-sections --target=riscv64-unknown-elf -O3 -march=rv64gcv test.cc -o test.o
llvm-objdump -- --arch-name=riscv64 --mcpu=generic-rv64 -D test.o > test.objdump
```

test.su
```
test.cc:1:_Z4hash 0 static
test.cc:1149:_Z5entryl 532224 static
```

But in the test.objdump you would find sp goes beyond 532224:
```
0000000000000000 <_Z5entryl>:
0: 7141 addi sp, sp, -0x1f0
2: f786 sd ra, 0x1e8(sp)
4: f3a2 sd s0, 0x1e0(sp)
6: efa6 sd s1, 0x1d8(sp)
8: ebca sd s2, 0x1d0(sp)
a: e7ce sd s3, 0x1c8(sp)
c: e3d2 sd s4, 0x1c0(sp)
e: ff56 sd s5, 0x1b8(sp)
10: fb5a sd s6, 0x1b0(sp)
12: f75e sd s7, 0x1a8(sp)
14: f362 sd s8, 0x1a0(sp)
16: ef66 sd s9, 0x198(sp)
18: eb6a sd s10, 0x190(sp)
1a: e76e sd s11, 0x188(sp)
1c: 1b80 addi s0, sp, 0x1f0
1e: 000825b7 lui a1, 0x82
22: d1058593 addi a1, a1, -0x2f0
26: 40b10133 sub sp, sp, a1
# Here sp is decreased by 0x1f0 + (0x82 << 12) - 0x2f0 = 532224
...
# But still inside entry, right before calling into hash, sp is futher decreased by 0x1f << 8 = 7936
3da6: 40fd li ra, 0x1f
3da8: 00a2 slli ra, ra, 0x8
3daa: 40110133 sub sp, sp, ra
3dae: 000810b7 lui ra, 0x81
3db2: 401400b3 sub ra, s0, ra
3db6: 4e80b083 ld ra, 0x4e8(ra)
3dba: 7e113023 sd ra, 0x7e0(sp)
3dbe: 7e913423 sd s1, 0x7e8(sp)
# And all these stack spaces are actually used, to set up the arguments calling hash
....
4bea: 40a40533 sub a0, s0, a0
4bee: 80053503 ld a0, -0x800(a0)
4bf2: e82a sd a0, 0x10(sp)
4bf4: 00081537 lui a0, 0x81
4bf8: 40a40533 sub a0, s0, a0
4bfc: 80853503 ld a0, -0x7f8(a0)
4c00: ec2a sd a0, 0x18(sp)
```

If we revert https://github.com/llvm/llvm-project/commit/e246d1615ea037f2dd92527ea1669b070c8e0993 and recompile, the stack usage reported via -fstack-usage would actually be 540144, more aligned with 532224 + 7936

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.