Issue 120353
Summary Inefficient Stack Usage in Creating and Passing Large Struct Argument
Labels new issue
Assignees
Reporter jonathan-gruber-jg
    When creating a large struct and passing it as an argument, Clang stores the struct on the stack twice. By a "large" struct, I mean one that is too large to be passed as an argument in the argument registers and must instead be passed on the stack.

A minimal test case is in the attached file test.c.txt (GitHub would not allow me to upload it with the .c extension, sadly), reproduced below for your convenience:
```
struct S {
	void *x, *y, *z, *w;
};

extern int extern_func(struct S);

int tail_call(void *x, void *y, void *z, void *w) {
	struct S a = { x, y, z, w };

	return extern_func(a);
}

int non_tail_call(void *x, void *y, void *z, void *w) {
	struct S a = { x, y, z, w };

	return ~extern_func(a);
}
```

I tested the target architecture x86_64 but am unsure if any other target architectures have this inefficiency.

Host system: Arch Linux, x86_64.

Clang version: official Arch Linux package of clang, version 18.1.8-4.

Command line to reproduce results: clang -c test.c --target=\<arch\> -O\<opt-level\>

x86_64 assembly (Intel syntax), with -Oz, -Os, -O2, or -O3
```
tail_call:
 push   rbp
    mov    rbp,rsp
    sub    rsp,0x40
    mov    QWORD PTR [rbp-0x20],rdi
    mov    QWORD PTR [rbp-0x18],rsi
    mov    QWORD PTR [rbp-0x10],rdx
    mov    QWORD PTR [rbp-0x8],rcx
    movups xmm0,XMMWORD PTR [rbp-0x20]
    movups xmm1,XMMWORD PTR [rbp-0x10]
    movups XMMWORD PTR [rsp+0x10],xmm1
    movups XMMWORD PTR [rsp],xmm0
    call extern_func
    add    rsp,0x40
    pop    rbp
    ret

non_tail_call:
 push   rbp
    mov    rbp,rsp
    sub    rsp,0x40
    mov    QWORD PTR [rbp-0x20],rdi
    mov    QWORD PTR [rbp-0x18],rsi
    mov    QWORD PTR [rbp-0x10],rdx
    mov    QWORD PTR [rbp-0x8],rcx
    movups xmm0,XMMWORD PTR [rbp-0x20]
    movups xmm1,XMMWORD PTR [rbp-0x10]
    movups XMMWORD PTR [rsp+0x10],xmm1
    movups XMMWORD PTR [rsp],xmm0
    call extern_func
    not    eax
    add    rsp,0x40
    pop    rbp
 ret
```

[test.c.txt](https://github.com/user-attachments/files/18174500/test.c.txt)
_______________________________________________
llvm-bugs mailing list
llvm-bugs@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to