llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

The -fstack-usage option incorrectly calculate size of the stack used by a function

Open rsashka opened this issue 11 months ago • 10 comments

When calculating the size of the stack used by a function, the size of the stack does not include the size of the return address from the function. When compiling a file with any type of optimization, the size of the used stack becomes equal to 0, although it cannot be less than the return address.

size_test.cpp:3:_Z15simple_functionv    0       static
size_test.cpp:8:_Z16complex_functioni   0       static
size_test.cpp:17:_Z9factoriali  0       static
size_test.cpp:22:main   0       static

File su generated by GCC:

size_test.cpp:3:6:void simple_function()        8       static
size_test.cpp:8:6:void complex_function(int)    8       static
size_test.cpp:17:5:int factorial(int)   8       static
size_test.cpp:22:5:int main()   8       static

rsashka avatar Dec 20 '25 16:12 rsashka

Thanks for reporting the issue. I think the size calculated by -fstack-usage should already include the size allocated for return address if the compiler needs to save the return address on the stack. When optimization is enabled, the compiler may decide to not save the return address on the stack if possible. Therefore, having "0" as the stack size is not necessarily wrong. To help us understand the issue better, can you please add a test case (ideally reduced test case) and share the assembly code generated by LLVM and GCC?

pzhengqc avatar Dec 29 '25 22:12 pzhengqc

Option -O0 for clnag

simple_function():
        push    rbp
        mov     rbp, rsp
        mov     dword ptr [rbp - 4], 5
        movsd   xmm0, qword ptr [rip + .LCPI0_0]
        movsd   qword ptr [rbp - 16], xmm0
        pop     rbp
        ret

complex_function(int):
        push    rbp
        mov     rbp, rsp
        sub     rsp, 2064
        mov     dword ptr [rbp - 4], edi
        cmp     dword ptr [rbp - 4], 10
        jle     .LBB1_2
        jmp     .LBB1_2
.LBB1_2:
        call    simple_function()
        add     rsp, 2064
        pop     rbp
        ret

Option -O3 for clnag

simple_function():
        ret

complex_function(int):
        ret

The last case is the maximum optimization level -O3 (the optimizer removes all code since the function does not return a result and does not use the stack), but the ret instruction is present, which is 8 bytes per return address for x86-64.

GCC optimizes the code in the same way (removes all unnecessary code), but correctly accounts for the use of the return address on the stack.

rsashka avatar Dec 30 '25 07:12 rsashka

This seems to be an x86 specific issue, right?

pzhengqc avatar Dec 30 '25 22:12 pzhengqc

No, this is not an x86 architecture issue, but a stack size calculation issue, since unit tests are run on aarch64 triplet, not x86. %clang_cc1 -triple aarch64-unknown -I . -stack-usage-file a.su -emit-obj a.c -o a.o

rsashka avatar Dec 31 '25 04:12 rsashka

At least for aarch64, I don't think this is an issue. In fact, the stack size computation would be wrong if we always add an additional slot for return address as done in FrameInfo.estimateStackSize(MF) + MF.getTarget().getAllocaPointerSize();.

For example, with the patch https://github.com/llvm/llvm-project/pull/173181, the following function

void foo() {}

would generate su file with

b.c:1:foo       8       static

which is incorrect since no return address needs to be pushed onto the stack for aarch64 (see assembly below).

foo: 
        ret

I am not sure about all the other targets though, thus suspecting this is an x86 specific issue.

pzhengqc avatar Dec 31 '25 16:12 pzhengqc

Your clarification concerns a particular case and problem of this platform (AARCH64)

Ultimately, the .su file is needed to estimate the maximum stack size used by all functions (in the worst case, ignoring recursion), not to assess the degree of optimization of any particular function.

After all, if 10 optimized functions are called sequentially, the stack will remain unused only in the last one, and estimating stack usage without taking the return address into account would calculate the required stack size as zero, which is incorrect.

rsashka avatar Dec 31 '25 17:12 rsashka

Please correct me if I am wrong, most of the targets I know use dedicated register for return address and the problem reported only seems to affect x86 AFAIK. My concern is that https://github.com/llvm/llvm-project/pull/173181 would affect all targets by always adding the space needed to save return address on stack. Can we limit the scope of the change to only x86 without affecting other targets?

pzhengqc avatar Dec 31 '25 18:12 pzhengqc

The return register you mentioned, although used on a specific platform, only applies to a single subfunction. If a subfunction itself calls a function, it is forced to save the return register on the stack. This applies to all nested function calls except the last one (which I mentioned in my previous post at the link). Perhaps this is taken into account when optimizing for different target platforms.

While I believe the issue affects all platforms, not just x86, unfortunately, I don't have a good understanding of this aspect of LLVM.

rsashka avatar Dec 31 '25 19:12 rsashka

For aarch64 and other targets where there's a dedicated link register, I believe FrameInfo.getStackSize() already accounts for the case when a function calls another function and needs to save the link register onto the stack.

For example, for aarch64, with the following code

void foo() {}

void bar() {
  foo();
}

the stack size of bar correctly accounted for the space needed to save the link register on the stack.

foo.c:1:foo     0       static
foo.c:3:bar     16      static

Here's the assembly output:

foo: 
        ret

bar: 
        stp     x29, x30, [sp, #-16]!
        mov     x29, sp
        bl      foo
        ldp     x29, x30, [sp], #16
        ret

pzhengqc avatar Dec 31 '25 20:12 pzhengqc

Fixed changes only for the x86 platform

rsashka avatar Jan 06 '26 08:01 rsashka