[X86] Inefficient stack allocation for aligned arrays
- Dominant language
- LLVM
- Stars
- 40.5k
- Forks
- 18.7k
- PR merge metrics
- PR metrics pending
Description
For the following small reproducer:
```c
void use(char *);
void f(void) {
__attribute__((aligned(64))) char a[64];
use(a);
}
```
gcc generates:
```asm
"f":
push rbp
mov rbp, rsp
and rsp, -64
sub rsp, 64
mov rdi, rsp
call "use"
leave
ret
```
while clang generates:
```asm
f:
push rbp
mov rbp, rsp
and rsp, -64
add rsp, -128
mov rdi, rsp
call use@PLT
mov rsp, rbp
pop rbp
ret
```
The array is 64 bytes and requires 64-byte alignment. After aligning rsp, clang allocates 128 bytes, whereas only 64 bytes are needed. As a result, 64 bytes more stack space is used than necessary.
The issue can be reproduced on godbolt: https://godbolt.org/z/n8TYx5qqT
Contributor guide
Research direction
Start with the C reproducer and generated x86 assembly on the linked Godbolt example, then identify the LLVM x86 backend code responsible for stack allocation and alignment. The work is done when the aligned 64-byte array reserves no unnecessary extra 64-byte block and the generated code remains correct.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- c
- Domain
- compilers, performance
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Active
- Clarity
- Mostly clear
- Newbie friendliness
- 48/100