Skip to content

Commit bb7d64f

Browse files
committed
rust_for_linux: -Zregparm=<N> commandline flag for X86 (#116972)
1 parent 8f93a10 commit bb7d64f

File tree

8 files changed

+118
-15
lines changed

8 files changed

+118
-15
lines changed

compiler/rustc_codegen_gcc/src/builder.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ use rustc_span::def_id::DefId;
3030
use rustc_span::Span;
3131
use rustc_target::abi::call::FnAbi;
3232
use rustc_target::abi::{self, Align, HasDataLayout, Size, TargetDataLayout, WrappingRange};
33-
use rustc_target::spec::{HasTargetSpec, HasWasmCAbiOpt, Target, WasmCAbi};
33+
use rustc_target::spec::{HasTargetSpec, HasWasmCAbiOpt, HasX86AbiOpt, Target, WasmCAbi, X86Abi};
3434

3535
use crate::common::{type_is_pointer, SignType, TypeReflection};
3636
use crate::context::CodegenCx;
@@ -2374,6 +2374,12 @@ impl<'tcx> HasWasmCAbiOpt for Builder<'_, '_, 'tcx> {
23742374
}
23752375
}
23762376

2377+
impl<'tcx> HasX86AbiOpt for Builder<'_, '_, 'tcx> {
2378+
fn x86_abi_opt(&self) -> X86Abi {
2379+
self.cx.x86_abi_opt()
2380+
}
2381+
}
2382+
23772383
pub trait ToGccComp {
23782384
fn to_gcc_comparison(&self) -> ComparisonOp;
23792385
}

compiler/rustc_codegen_gcc/src/context.rs

+11-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ use rustc_span::source_map::respan;
2020
use rustc_span::{Span, DUMMY_SP};
2121
use rustc_target::abi::call::FnAbi;
2222
use rustc_target::abi::{HasDataLayout, PointeeInfo, Size, TargetDataLayout, VariantIdx};
23-
use rustc_target::spec::{HasTargetSpec, HasWasmCAbiOpt, Target, TlsModel, WasmCAbi};
23+
use rustc_target::spec::{
24+
HasTargetSpec, HasWasmCAbiOpt, HasX86AbiOpt, Target, TlsModel, WasmCAbi, X86Abi
25+
};
2426

2527
use crate::callee::get_fn;
2628
use crate::common::SignType;
@@ -571,6 +573,14 @@ impl<'gcc, 'tcx> HasWasmCAbiOpt for CodegenCx<'gcc, 'tcx> {
571573
}
572574
}
573575

576+
impl<'gcc, 'tcx> X86AbiOpt for CodegenCx<'gcc, 'tcx> {
577+
fn x86_abi_opt(&self) -> X86Abi {
578+
X86Abi {
579+
regparm: self.tcx.sess.opts.unstable_opts.regparm
580+
}
581+
}
582+
}
583+
574584
impl<'gcc, 'tcx> LayoutOfHelpers<'tcx> for CodegenCx<'gcc, 'tcx> {
575585
type LayoutOfResult = TyAndLayout<'tcx>;
576586

compiler/rustc_middle/src/ty/layout.rs

+17-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ use rustc_span::{ErrorGuaranteed, Span, DUMMY_SP};
1818
use rustc_target::abi::call::FnAbi;
1919
use rustc_target::abi::*;
2020
use rustc_target::spec::abi::Abi as SpecAbi;
21-
use rustc_target::spec::{HasTargetSpec, HasWasmCAbiOpt, PanicStrategy, Target, WasmCAbi};
21+
use rustc_target::spec::{
22+
HasTargetSpec, HasWasmCAbiOpt, HasX86AbiOpt, PanicStrategy, Target, WasmCAbi, X86Abi
23+
};
2224
use tracing::debug;
2325

2426
use crate::error::UnsupportedFnAbi;
@@ -541,6 +543,14 @@ impl<'tcx> HasWasmCAbiOpt for TyCtxt<'tcx> {
541543
}
542544
}
543545

546+
impl<'tcx> HasX86AbiOpt for TyCtxt<'tcx> {
547+
fn x86_abi_opt(&self) -> X86Abi {
548+
X86Abi {
549+
regparm: self.sess.opts.unstable_opts.regparm
550+
}
551+
}
552+
}
553+
544554
impl<'tcx> HasTyCtxt<'tcx> for TyCtxt<'tcx> {
545555
#[inline]
546556
fn tcx(&self) -> TyCtxt<'tcx> {
@@ -592,6 +602,12 @@ impl<'tcx, T: HasWasmCAbiOpt> HasWasmCAbiOpt for LayoutCx<'tcx, T> {
592602
}
593603
}
594604

605+
impl<'tcx, T: HasX86AbiOpt> HasX86AbiOpt for LayoutCx<'tcx, T> {
606+
fn x86_abi_opt(&self) -> X86Abi {
607+
self.tcx.x86_abi_opt()
608+
}
609+
}
610+
595611
impl<'tcx, T: HasTyCtxt<'tcx>> HasTyCtxt<'tcx> for LayoutCx<'tcx, T> {
596612
fn tcx(&self) -> TyCtxt<'tcx> {
597613
self.tcx.tcx()

compiler/rustc_session/src/options.rs

+3
Original file line numberDiff line numberDiff line change
@@ -1964,6 +1964,9 @@ options! {
19641964
"enable queries of the dependency graph for regression testing (default: no)"),
19651965
randomize_layout: bool = (false, parse_bool, [TRACKED],
19661966
"randomize the layout of types (default: no)"),
1967+
regparm: Option<u32> = (None, parse_opt_number, [TRACKED],
1968+
"On x86-32 targets, the regparm <N> causes the compiler to pass arguments \
1969+
in registers EAX, EDX, and ECX instead of on the stack."),
19671970
relax_elf_relocations: Option<bool> = (None, parse_opt_bool, [TRACKED],
19681971
"whether ELF relocations can be relaxed"),
19691972
remap_cwd_prefix: Option<PathBuf> = (None, parse_opt_pathbuf, [TRACKED],

compiler/rustc_target/src/abi/call/mod.rs

+12-9
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use rustc_macros::HashStable_Generic;
55
use rustc_span::Symbol;
66

77
use crate::abi::{self, Abi, Align, FieldsShape, HasDataLayout, Size, TyAbiInterface, TyAndLayout};
8-
use crate::spec::{self, HasTargetSpec, HasWasmCAbiOpt, WasmCAbi};
8+
use crate::spec::{self, HasTargetSpec, HasWasmCAbiOpt, HasX86AbiOpt, WasmCAbi};
99

1010
mod aarch64;
1111
mod amdgpu;
@@ -877,7 +877,7 @@ impl<'a, Ty> FnAbi<'a, Ty> {
877877
) -> Result<(), AdjustForForeignAbiError>
878878
where
879879
Ty: TyAbiInterface<'a, C> + Copy,
880-
C: HasDataLayout + HasTargetSpec + HasWasmCAbiOpt,
880+
C: HasDataLayout + HasTargetSpec + HasWasmCAbiOpt + HasX86AbiOpt,
881881
{
882882
if abi == spec::abi::Abi::X86Interrupt {
883883
if let Some(arg) = self.args.first_mut() {
@@ -890,14 +890,17 @@ impl<'a, Ty> FnAbi<'a, Ty> {
890890
let spec = cx.target_spec();
891891
match &spec.arch[..] {
892892
"x86" => {
893-
let flavor = if let spec::abi::Abi::Fastcall { .. }
894-
| spec::abi::Abi::Vectorcall { .. } = abi
895-
{
896-
x86::Flavor::FastcallOrVectorcall
897-
} else {
898-
x86::Flavor::General
893+
let (flavor, regparm) = match abi {
894+
spec::abi::Abi::Fastcall { .. }
895+
| spec::abi::Abi::Vectorcall { .. } =>
896+
(x86::Flavor::FastcallOrVectorcall, None),
897+
spec::abi::Abi::C { .. }
898+
| spec::abi::Abi::Cdecl { .. }
899+
| spec::abi::Abi::Stdcall { .. } =>
900+
(x86::Flavor::General, cx.x86_abi_opt().regparm),
901+
_ => (x86::Flavor::General, None)
899902
};
900-
x86::compute_abi_info(cx, self, flavor);
903+
x86::compute_abi_info(cx, self, x86::X86Options { flavor, regparm });
901904
}
902905
"x86_64" => match abi {
903906
spec::abi::Abi::SysV64 { .. } => x86_64::compute_abi_info(cx, self),

compiler/rustc_target/src/abi/call/x86.rs

+12-3
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,20 @@ pub(crate) enum Flavor {
88
FastcallOrVectorcall,
99
}
1010

11-
pub(crate) fn compute_abi_info<'a, Ty, C>(cx: &C, fn_abi: &mut FnAbi<'a, Ty>, flavor: Flavor)
11+
pub(crate) struct X86Options {
12+
pub flavor: Flavor,
13+
pub regparm: Option<u32>,
14+
}
15+
16+
pub(crate) fn compute_abi_info<'a, Ty, C>(
17+
cx: &C, fn_abi: &mut FnAbi<'a, Ty>,
18+
opts: X86Options,
19+
)
1220
where
1321
Ty: TyAbiInterface<'a, C> + Copy,
1422
C: HasDataLayout + HasTargetSpec,
1523
{
24+
let flavor = opts.flavor;
1625
if !fn_abi.ret.is_ignore() {
1726
if fn_abi.ret.layout.is_aggregate() && fn_abi.ret.layout.is_sized() {
1827
// Returning a structure. Most often, this will use
@@ -128,7 +137,7 @@ where
128137
}
129138
}
130139

131-
if flavor == Flavor::FastcallOrVectorcall {
140+
if flavor == Flavor::FastcallOrVectorcall || opts.regparm.is_some() {
132141
// Mark arguments as InReg like clang does it,
133142
// so our fastcall/vectorcall is compatible with C/C++ fastcall/vectorcall.
134143

@@ -138,7 +147,7 @@ where
138147
// IsSoftFloatABI is only set to true on ARM platforms,
139148
// which in turn can't be x86?
140149

141-
let mut free_regs = 2;
150+
let mut free_regs: u64 = opts.regparm.unwrap_or(2).into();
142151

143152
for arg in fn_abi.args.iter_mut() {
144153
let attrs = match arg.mode {

compiler/rustc_target/src/spec/mod.rs

+12
Original file line numberDiff line numberDiff line change
@@ -2012,6 +2012,18 @@ pub trait HasWasmCAbiOpt {
20122012
fn wasm_c_abi_opt(&self) -> WasmCAbi;
20132013
}
20142014

2015+
/// x86 (32-bit) abi options.
2016+
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
2017+
pub struct X86Abi {
2018+
/// On x86-32 targets, the regparm <N> causes the compiler to pass arguments
2019+
/// in registers EAX, EDX, and ECX instead of on the stack.
2020+
pub regparm: Option<u32>,
2021+
}
2022+
2023+
pub trait HasX86AbiOpt {
2024+
fn x86_abi_opt(&self) -> X86Abi;
2025+
}
2026+
20152027
type StaticCow<T> = Cow<'static, T>;
20162028

20172029
/// Optional aspects of a target specification.

tests/codegen/regparm-inreg.rs

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Checks how `regparm` flag works with different calling conventions:
2+
// marks function arguments as "inreg" like the C/C++ compilers for the platforms.
3+
// x86 only.
4+
5+
//@ compile-flags: --target i686-unknown-linux-gnu -Zregparm=3 -O -C no-prepopulate-passes
6+
//@ needs-llvm-components: x86
7+
8+
#![crate_type = "lib"]
9+
#![no_core]
10+
#![feature(no_core, lang_items)]
11+
12+
#[lang = "sized"]
13+
trait Sized {}
14+
#[lang = "copy"]
15+
trait Copy {}
16+
17+
pub mod tests {
18+
// regparm doesn't work for "fastcall" calling conv (only 2 inregs)
19+
// CHECK: @f1(i32 inreg noundef %_1, i32 inreg noundef %_2, i32 noundef %_3)
20+
#[no_mangle]
21+
pub extern "fastcall" fn f1(_: i32, _: i32, _: i32) {}
22+
23+
// regparm doesn't work for "Rust" calling conv
24+
// CHECK: @f2(ptr noundef %_1, ptr noundef %_2, ptr noundef %_3)
25+
#[no_mangle]
26+
pub extern "Rust" fn f2(_: i32, _: i32, _: i32) {}
27+
28+
// CHECK: @f3(i32 inreg noundef %_1, i32 inreg noundef %_2, i32 inreg noundef %_3)
29+
#[no_mangle]
30+
pub extern "C" fn f3(_: i32, _: i32, _: i32) {}
31+
32+
// CHECK: @f4(i32 inreg noundef %_1, i32 inreg noundef %_2, i32 inreg noundef %_3)
33+
#[no_mangle]
34+
pub extern "cdecl" fn f4(_: i32, _: i32, _: i32) {}
35+
36+
// CHECK: @f5(i32 inreg noundef %_1, i32 inreg noundef %_2, i32 inreg noundef %_3)
37+
#[no_mangle]
38+
pub extern "stdcall" fn f5(_: i32, _: i32, _: i32) {}
39+
40+
// regparm doesn't work for thiscall
41+
// CHECK: @f6(ptr noundef %_1, ptr noundef %_2, ptr noundef %_3)
42+
#[no_mangle]
43+
pub extern "thiscall" fn f6(_: i32, _: i32, _: i32) {}
44+
}

0 commit comments

Comments
 (0)