diff options
| author | Stuart Cook <Zalathar@users.noreply.github.com> | 2025-08-18 15:31:11 +1000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-08-18 15:31:11 +1000 |
| commit | 1e454c64b248b8b2083c308714d23fa526442b2f (patch) | |
| tree | 05325e66c2c597e25a810a73437ec5e706e5723a /compiler/rustc_codegen_llvm/src/llvm/ffi.rs | |
| parent | 152b43c7cb9705b071394e4deb40d82e20c12552 (diff) | |
| parent | 04ff1444bb89379d7489bda9edc65ebaae4db037 (diff) | |
| download | rust-1e454c64b248b8b2083c308714d23fa526442b2f.tar.gz rust-1e454c64b248b8b2083c308714d23fa526442b2f.zip | |
Rollup merge of #145309 - winstonallo:issue-145271-fix, r=tgross35
Fix `-Zregparm` for LLVM builtins
This fixes the issue where `-Zregparm=N` was not working correctly when calling LLVM intrinsics
By default on `x86-32`, arguments are passed on the stack. The `-Zregparm=N` flag allows the first `N` arguments to be passed in registers instead.
When calling intrinsics like `memset`, LLVM still passes parameters on the stack, which prevents optimizations like tail calls.
As proposed by ````@tgross35,```` I fixed this by setting the `NumRegisterParameters` LLVM module flag to `N` when the `-Zregparm=N` is set.
```rust
// compiler/rust_codegen_llvm/src/context.rs#375-382
if let Some(regparm_count) = sess.opts.unstable_opts.regparm {
llvm::add_module_flag_u32(
llmod,
llvm::ModuleFlagMergeBehavior::Error,
"NumRegisterParameters",
regparm_count,
);
}
```
[Here](https://rust.godbolt.org/z/YMezreo48) is a before/after compiler explorer.
Here is the final result for the code snippet in the original issue:
```asm
entrypoint:
push esi
mov esi, eax
mov eax, ecx
mov ecx, esi
pop esi
jmp memset ; Tail call parameters in registers
```
Fixes: https://github.com/rust-lang/rust/issues/145271
Diffstat (limited to 'compiler/rustc_codegen_llvm/src/llvm/ffi.rs')
0 files changed, 0 insertions, 0 deletions
