Skip to content

Commit

Permalink
rust_for_linux: -Zreg-struct-return commandline flag for X86 (#116973)
Browse files Browse the repository at this point in the history
  • Loading branch information
azhogin committed Sep 24, 2024
1 parent caecc18 commit ad600b5
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 4 deletions.
5 changes: 4 additions & 1 deletion compiler/rustc_codegen_gcc/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -575,7 +575,10 @@ impl<'gcc, 'tcx> HasWasmCAbiOpt for CodegenCx<'gcc, 'tcx> {

impl<'gcc, 'tcx> HasX86AbiOpt for CodegenCx<'gcc, 'tcx> {
fn x86_abi_opt(&self) -> X86Abi {
X86Abi { regparm: self.tcx.sess.opts.unstable_opts.regparm }
X86Abi {
regparm: self.tcx.sess.opts.unstable_opts.regparm,
reg_struct_return: self.tcx.sess.opts.unstable_opts.reg_struct_return,
}
}
}

Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_interface/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -834,6 +834,7 @@ fn test_unstable_options_tracking_hash() {
tracked!(profile_emit, Some(PathBuf::from("abc")));
tracked!(profile_sample_use, Some(PathBuf::from("abc")));
tracked!(profiler_runtime, "abc".to_string());
tracked!(reg_struct_return, true);
tracked!(regparm, Some(3));
tracked!(relax_elf_relocations, Some(true));
tracked!(remap_cwd_prefix, Some(PathBuf::from("abc")));
Expand Down
5 changes: 4 additions & 1 deletion compiler/rustc_middle/src/ty/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -538,7 +538,10 @@ impl<'tcx> HasWasmCAbiOpt for TyCtxt<'tcx> {

impl<'tcx> HasX86AbiOpt for TyCtxt<'tcx> {
fn x86_abi_opt(&self) -> X86Abi {
X86Abi { regparm: self.sess.opts.unstable_opts.regparm }
X86Abi {
regparm: self.sess.opts.unstable_opts.regparm,
reg_struct_return: self.sess.opts.unstable_opts.reg_struct_return,
}
}
}

Expand Down
3 changes: 3 additions & 0 deletions compiler/rustc_session/src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1978,6 +1978,9 @@ options! {
"enable queries of the dependency graph for regression testing (default: no)"),
randomize_layout: bool = (false, parse_bool, [TRACKED],
"randomize the layout of types (default: no)"),
reg_struct_return: bool = (false, parse_bool, [TRACKED],
"On x86-32 targets, it overrides the default ABI to return small structs in registers.
It is UNSOUND to link together crates that use different values for this flag!"),
regparm: Option<u32> = (None, parse_opt_number, [TRACKED],
"On x86-32 targets, setting this to N causes the compiler to pass N arguments \
in registers EAX, EDX, and ECX instead of on the stack.\
Expand Down
5 changes: 4 additions & 1 deletion compiler/rustc_target/src/abi/call/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -899,7 +899,10 @@ impl<'a, Ty> FnAbi<'a, Ty> {
}
_ => (x86::Flavor::General, None),
};
x86::compute_abi_info(cx, self, x86::X86Options { flavor, regparm });
let reg_struct_return = cx.x86_abi_opt().reg_struct_return;
x86::compute_abi_info(
cx, self, x86::X86Options { flavor, regparm, reg_struct_return }
);
}
"x86_64" => match abi {
spec::abi::Abi::SysV64 { .. } => x86_64::compute_abi_info(cx, self),
Expand Down
3 changes: 2 additions & 1 deletion compiler/rustc_target/src/abi/call/x86.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pub(crate) enum Flavor {
pub(crate) struct X86Options {
pub flavor: Flavor,
pub regparm: Option<u32>,
pub reg_struct_return: bool,
}

pub(crate) fn compute_abi_info<'a, Ty, C>(cx: &C, fn_abi: &mut FnAbi<'a, Ty>, opts: X86Options)
Expand All @@ -29,7 +30,7 @@ where
// https://www.angelcode.com/dev/callconv/callconv.html
// Clang's ABI handling is in lib/CodeGen/TargetInfo.cpp
let t = cx.target_spec();
if t.abi_return_struct_as_int {
if t.abi_return_struct_as_int || opts.reg_struct_return {
// According to Clang, everyone but MSVC returns single-element
// float aggregates directly in a floating-point register.
if !t.is_like_msvc && fn_abi.ret.layout.is_single_fp_element(cx) {
Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_target/src/spec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2056,6 +2056,8 @@ pub struct X86Abi {
/// On x86-32 targets, the regparm N causes the compiler to pass arguments
/// in registers EAX, EDX, and ECX instead of on the stack.
pub regparm: Option<u32>,
/// Override the default ABI to return small structs in registers
pub reg_struct_return: bool,
}

pub trait HasX86AbiOpt {
Expand Down
52 changes: 52 additions & 0 deletions tests/codegen/reg-struct-return.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Checks how `reg-struct-return` flag works with different calling conventions:
// Return struct with 8/16/32/64 bit size will be converted into i8/i16/i32/i64 (like abi_return_struct_as_int target spec).
// x86 only.

//@ compile-flags: --target i686-unknown-linux-gnu -Zreg-struct-return -O -C no-prepopulate-passes
//@ needs-llvm-components: x86

#![crate_type = "lib"]

pub mod tests {
#[repr(C)]
pub struct Foo {
x: u32,
y: u32,
}
// CHECK: i64 @f1(i32 inreg noundef %_1, i32 inreg noundef %_2)
#[no_mangle]
pub extern "fastcall" fn f1(x: u32, y: u32) -> Foo {
Foo { x, y }
}

// reg-struct-return doesn't work for "Rust" calling conv
// CHECK: { i32, i32 } @f2(i32 inreg noundef %_1, i32 inreg noundef %_2)
#[no_mangle]
pub extern "Rust" fn f2(x: u32, y: u32) -> Foo {
Foo { x, y }
}

// CHECK: i64 @f3(i32 inreg noundef %_1, i32 inreg noundef %_2)
#[no_mangle]
pub extern "C" fn f3(x: u32, y: u32) -> Foo {
Foo { x, y }
}

// CHECK: i64 @f4(i32 inreg noundef %_1, i32 inreg noundef %_2)
#[no_mangle]
pub extern "cdecl" fn f4(x: u32, y: u32) -> Foo {
Foo { x, y }
}

// CHECK: i64 @f5(i32 inreg noundef %_1, i32 inreg noundef %_2)
#[no_mangle]
pub extern "stdcall" fn f5(x: u32, y: u32) -> Foo {
Foo { x, y }
}

// CHECK: i64 @f6(i32 inreg noundef %_1, i32 inreg noundef %_2)
#[no_mangle]
pub extern "thiscall" fn f6(x: u32, y: u32) -> Foo {
Foo { x, y }
}
}

0 comments on commit ad600b5

Please sign in to comment.