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 /tests/assembly-llvm | |
| 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 'tests/assembly-llvm')
| -rw-r--r-- | tests/assembly-llvm/regparm-module-flag.rs | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/tests/assembly-llvm/regparm-module-flag.rs b/tests/assembly-llvm/regparm-module-flag.rs new file mode 100644 index 00000000000..67ef44285ea --- /dev/null +++ b/tests/assembly-llvm/regparm-module-flag.rs @@ -0,0 +1,70 @@ +// Test the regparm ABI with builtin and non-builtin calls +// Issue: https://github.com/rust-lang/rust/issues/145271 +//@ add-core-stubs +//@ assembly-output: emit-asm +//@ compile-flags: -O --target=i686-unknown-linux-gnu -Crelocation-model=static +//@ revisions: REGPARM1 REGPARM2 REGPARM3 +//@[REGPARM1] compile-flags: -Zregparm=1 +//@[REGPARM2] compile-flags: -Zregparm=2 +//@[REGPARM3] compile-flags: -Zregparm=3 +//@ needs-llvm-components: x86 +#![feature(no_core)] +#![no_std] +#![no_core] +#![crate_type = "lib"] + +extern crate minicore; +use minicore::*; + +unsafe extern "C" { + fn memset(p: *mut c_void, val: i32, len: usize) -> *mut c_void; + fn non_builtin_memset(p: *mut c_void, val: i32, len: usize) -> *mut c_void; +} + +#[unsafe(no_mangle)] +pub unsafe extern "C" fn entrypoint(len: usize, ptr: *mut c_void, val: i32) -> *mut c_void { + // REGPARM1-LABEL: entrypoint + // REGPARM1: movl %e{{.*}}, %ecx + // REGPARM1: pushl + // REGPARM1: pushl + // REGPARM1: calll memset + + // REGPARM2-LABEL: entrypoint + // REGPARM2: movl 16(%esp), %edx + // REGPARM2: movl %e{{.*}}, (%esp) + // REGPARM2: movl %e{{.*}}, %eax + // REGPARM2: calll memset + + // REGPARM3-LABEL: entrypoint + // REGPARM3: movl %e{{.*}}, %esi + // REGPARM3: movl %e{{.*}}, %eax + // REGPARM3: movl %e{{.*}}, %ecx + // REGPARM3: jmp memset + unsafe { memset(ptr, val, len) } +} + +#[unsafe(no_mangle)] +pub unsafe extern "C" fn non_builtin_entrypoint( + len: usize, + ptr: *mut c_void, + val: i32, +) -> *mut c_void { + // REGPARM1-LABEL: non_builtin_entrypoint + // REGPARM1: movl %e{{.*}}, %ecx + // REGPARM1: pushl + // REGPARM1: pushl + // REGPARM1: calll non_builtin_memset + + // REGPARM2-LABEL: non_builtin_entrypoint + // REGPARM2: movl 16(%esp), %edx + // REGPARM2: movl %e{{.*}}, (%esp) + // REGPARM2: movl %e{{.*}}, %eax + // REGPARM2: calll non_builtin_memset + + // REGPARM3-LABEL: non_builtin_entrypoint + // REGPARM3: movl %e{{.*}}, %esi + // REGPARM3: movl %e{{.*}}, %eax + // REGPARM3: movl %e{{.*}}, %ecx + // REGPARM3: jmp non_builtin_memset + unsafe { non_builtin_memset(ptr, val, len) } +} |
