Skip to content

Commit 11be048

Browse files
committed
add SSE2 ABI for x86, and use it for float and SIMD passing
1 parent bb76ecf commit 11be048

33 files changed

+286
-139
lines changed

compiler/rustc_target/src/callconv/mod.rs

Lines changed: 75 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use crate::abi::{
99
self, AddressSpace, Align, BackendRepr, HasDataLayout, Pointer, Size, TyAbiInterface,
1010
TyAndLayout,
1111
};
12-
use crate::spec::{HasTargetSpec, HasWasmCAbiOpt, HasX86AbiOpt, WasmCAbi};
12+
use crate::spec::{HasTargetSpec, HasWasmCAbiOpt, HasX86AbiOpt, RustcAbi, WasmCAbi};
1313

1414
mod aarch64;
1515
mod amdgpu;
@@ -388,6 +388,7 @@ impl<'a, Ty> ArgAbi<'a, Ty> {
388388
/// Pass this argument directly instead. Should NOT be used!
389389
/// Only exists because of past ABI mistakes that will take time to fix
390390
/// (see <https://github.com/rust-lang/rust/issues/115666>).
391+
#[track_caller]
391392
pub fn make_direct_deprecated(&mut self) {
392393
match self.mode {
393394
PassMode::Indirect { .. } => {
@@ -400,6 +401,7 @@ impl<'a, Ty> ArgAbi<'a, Ty> {
400401

401402
/// Pass this argument indirectly, by passing a (thin or wide) pointer to the argument instead.
402403
/// This is valid for both sized and unsized arguments.
404+
#[track_caller]
403405
pub fn make_indirect(&mut self) {
404406
match self.mode {
405407
PassMode::Direct(_) | PassMode::Pair(_, _) => {
@@ -414,6 +416,7 @@ impl<'a, Ty> ArgAbi<'a, Ty> {
414416

415417
/// Same as `make_indirect`, but for arguments that are ignored. Only needed for ABIs that pass
416418
/// ZSTs indirectly.
419+
#[track_caller]
417420
pub fn make_indirect_from_ignore(&mut self) {
418421
match self.mode {
419422
PassMode::Ignore => {
@@ -737,26 +740,47 @@ impl<'a, Ty> FnAbi<'a, Ty> {
737740
C: HasDataLayout + HasTargetSpec,
738741
{
739742
let spec = cx.target_spec();
740-
match &spec.arch[..] {
743+
match &*spec.arch {
741744
"x86" => x86::compute_rust_abi_info(cx, self, abi),
742745
"riscv32" | "riscv64" => riscv::compute_rust_abi_info(cx, self, abi),
743746
"loongarch64" => loongarch::compute_rust_abi_info(cx, self, abi),
744747
"aarch64" => aarch64::compute_rust_abi_info(cx, self),
745748
_ => {}
746749
};
747750

751+
// Decides whether we can pass the given SIMD argument via `PassMode::Direct`.
752+
// May only return `true` if the target will always pass those arguments the same way,
753+
// no matter what the user does with `-Ctarget-feature`! In other words, whatever
754+
// target features are required to pass a SIMD value in registers must be listed in
755+
// the `abi_required_features` for the current target and ABI.
756+
let can_pass_simd_directly = |arg: &ArgAbi<'_, Ty>| match &*spec.arch {
757+
// On x86, if we have SSE2 (which we have by default for x86_64), we can always pass up
758+
// to 128-bit-sized vectors.
759+
"x86" if spec.rustc_abi == Some(RustcAbi::X86Sse2) => arg.layout.size.bits() <= 128,
760+
"x86_64" if spec.rustc_abi != Some(RustcAbi::X86Softfloat) => {
761+
arg.layout.size.bits() <= 128
762+
}
763+
// So far, we haven't implemented this logic for any other target.
764+
_ => false,
765+
};
766+
748767
for (arg_idx, arg) in self
749768
.args
750769
.iter_mut()
751770
.enumerate()
752771
.map(|(idx, arg)| (Some(idx), arg))
753772
.chain(iter::once((None, &mut self.ret)))
754773
{
755-
if arg.is_ignore() {
774+
// If the logic above already picked a specific type to cast the argument to, leave that
775+
// in place.
776+
if matches!(arg.mode, PassMode::Ignore | PassMode::Cast { .. }) {
756777
continue;
757778
}
758779

759-
if arg_idx.is_none() && arg.layout.size > Pointer(AddressSpace::DATA).size(cx) * 2 {
780+
if arg_idx.is_none()
781+
&& arg.layout.size > Pointer(AddressSpace::DATA).size(cx) * 2
782+
&& !matches!(arg.layout.backend_repr, BackendRepr::Vector { .. })
783+
{
760784
// Return values larger than 2 registers using a return area
761785
// pointer. LLVM and Cranelift disagree about how to return
762786
// values that don't fit in the registers designated for return
@@ -765,7 +789,8 @@ impl<'a, Ty> FnAbi<'a, Ty> {
765789
// return value independently and decide to pass it in a
766790
// register or not, which would result in the return value
767791
// being passed partially in registers and partially through a
768-
// return area pointer.
792+
// return area pointer. For large IR-level values such as `i128`,
793+
// cranelift will even split up the value into smaller chunks.
769794
//
770795
// While Cranelift may need to be fixed as the LLVM behavior is
771796
// generally more correct with respect to the surface language,
@@ -795,53 +820,58 @@ impl<'a, Ty> FnAbi<'a, Ty> {
795820
// rustc_target already ensure any return value which doesn't
796821
// fit in the available amount of return registers is passed in
797822
// the right way for the current target.
823+
//
824+
// The adjustment is not necessary nor desired for types with a vector
825+
// representation; those are handled below.
798826
arg.make_indirect();
799827
continue;
800828
}
801829

802830
match arg.layout.backend_repr {
803-
BackendRepr::Memory { .. } => {}
804-
805-
// This is a fun case! The gist of what this is doing is
806-
// that we want callers and callees to always agree on the
807-
// ABI of how they pass SIMD arguments. If we were to *not*
808-
// make these arguments indirect then they'd be immediates
809-
// in LLVM, which means that they'd used whatever the
810-
// appropriate ABI is for the callee and the caller. That
811-
// means, for example, if the caller doesn't have AVX
812-
// enabled but the callee does, then passing an AVX argument
813-
// across this boundary would cause corrupt data to show up.
814-
//
815-
// This problem is fixed by unconditionally passing SIMD
816-
// arguments through memory between callers and callees
817-
// which should get them all to agree on ABI regardless of
818-
// target feature sets. Some more information about this
819-
// issue can be found in #44367.
820-
//
821-
// Note that the intrinsic ABI is exempt here as
822-
// that's how we connect up to LLVM and it's unstable
823-
// anyway, we control all calls to it in libstd.
824-
BackendRepr::Vector { .. }
825-
if abi != ExternAbi::RustIntrinsic && spec.simd_types_indirect =>
826-
{
827-
arg.make_indirect();
828-
continue;
831+
BackendRepr::Memory { .. } => {
832+
// Compute `Aggregate` ABI.
833+
834+
let is_indirect_not_on_stack =
835+
matches!(arg.mode, PassMode::Indirect { on_stack: false, .. });
836+
assert!(is_indirect_not_on_stack);
837+
838+
let size = arg.layout.size;
839+
if arg.layout.is_sized() && size <= Pointer(AddressSpace::DATA).size(cx) {
840+
// We want to pass small aggregates as immediates, but using
841+
// an LLVM aggregate type for this leads to bad optimizations,
842+
// so we pick an appropriately sized integer type instead.
843+
arg.cast_to(Reg { kind: RegKind::Integer, size });
844+
}
829845
}
830846

831-
_ => continue,
832-
}
833-
// Compute `Aggregate` ABI.
834-
835-
let is_indirect_not_on_stack =
836-
matches!(arg.mode, PassMode::Indirect { on_stack: false, .. });
837-
assert!(is_indirect_not_on_stack);
838-
839-
let size = arg.layout.size;
840-
if !arg.layout.is_unsized() && size <= Pointer(AddressSpace::DATA).size(cx) {
841-
// We want to pass small aggregates as immediates, but using
842-
// an LLVM aggregate type for this leads to bad optimizations,
843-
// so we pick an appropriately sized integer type instead.
844-
arg.cast_to(Reg { kind: RegKind::Integer, size });
847+
BackendRepr::Vector { .. } => {
848+
// This is a fun case! The gist of what this is doing is
849+
// that we want callers and callees to always agree on the
850+
// ABI of how they pass SIMD arguments. If we were to *not*
851+
// make these arguments indirect then they'd be immediates
852+
// in LLVM, which means that they'd used whatever the
853+
// appropriate ABI is for the callee and the caller. That
854+
// means, for example, if the caller doesn't have AVX
855+
// enabled but the callee does, then passing an AVX argument
856+
// across this boundary would cause corrupt data to show up.
857+
//
858+
// This problem is fixed by unconditionally passing SIMD
859+
// arguments through memory between callers and callees
860+
// which should get them all to agree on ABI regardless of
861+
// target feature sets. Some more information about this
862+
// issue can be found in #44367.
863+
//
864+
// Note that the intrinsic ABI is exempt here as those are not
865+
// real functions anyway, and the backend expects very specific types.
866+
if abi != ExternAbi::RustIntrinsic
867+
&& spec.simd_types_indirect
868+
&& !can_pass_simd_directly(arg)
869+
{
870+
arg.make_indirect();
871+
}
872+
}
873+
874+
_ => {}
845875
}
846876
}
847877
}

compiler/rustc_target/src/callconv/x86.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ use crate::abi::call::{ArgAttribute, FnAbi, PassMode, Reg, RegKind};
22
use crate::abi::{
33
AddressSpace, Align, BackendRepr, Float, HasDataLayout, Pointer, TyAbiInterface, TyAndLayout,
44
};
5-
use crate::spec::HasTargetSpec;
65
use crate::spec::abi::Abi as SpecAbi;
6+
use crate::spec::{HasTargetSpec, RustcAbi};
77

88
#[derive(PartialEq)]
99
pub(crate) enum Flavor {
@@ -234,8 +234,14 @@ where
234234
_ => false, // anyway not passed via registers on x86
235235
};
236236
if has_float {
237-
if fn_abi.ret.layout.size <= Pointer(AddressSpace::DATA).size(cx) {
238-
// Same size or smaller than pointer, return in a register.
237+
if cx.target_spec().rustc_abi == Some(RustcAbi::X86Sse2)
238+
&& fn_abi.ret.layout.backend_repr.is_scalar()
239+
&& fn_abi.ret.layout.size.bits() <= 128
240+
{
241+
// This is a single scalar that fits into an SSE register.
242+
fn_abi.ret.cast_to(Reg { kind: RegKind::Vector, size: fn_abi.ret.layout.size });
243+
} else if fn_abi.ret.layout.size <= Pointer(AddressSpace::DATA).size(cx) {
244+
// Same size or smaller than pointer, return in an integer register.
239245
fn_abi.ret.cast_to(Reg { kind: RegKind::Integer, size: fn_abi.ret.layout.size });
240246
} else {
241247
// Larger than a pointer, return indirectly.

compiler/rustc_target/src/spec/base/apple/mod.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ use std::borrow::Cow;
22
use std::env;
33

44
use crate::spec::{
5-
Cc, DebuginfoKind, FloatAbi, FramePointer, LinkerFlavor, Lld, SplitDebuginfo, StackProbeType,
6-
StaticCow, TargetOptions, cvs,
5+
Cc, DebuginfoKind, FloatAbi, FramePointer, LinkerFlavor, Lld, RustcAbi, SplitDebuginfo,
6+
StackProbeType, StaticCow, TargetOptions, cvs,
77
};
88

99
#[cfg(test)]
@@ -103,7 +103,7 @@ pub(crate) fn base(
103103
arch: Arch,
104104
abi: TargetAbi,
105105
) -> (TargetOptions, StaticCow<str>, StaticCow<str>) {
106-
let opts = TargetOptions {
106+
let mut opts = TargetOptions {
107107
abi: abi.target_abi().into(),
108108
llvm_floatabi: Some(FloatAbi::Hard),
109109
os: os.into(),
@@ -154,6 +154,10 @@ pub(crate) fn base(
154154

155155
..Default::default()
156156
};
157+
if matches!(arch, Arch::I386 | Arch::I686) {
158+
// All Apple x86-32 targets have SSE2.
159+
opts.rustc_abi = Some(RustcAbi::X86Sse2);
160+
}
157161
(opts, unversioned_llvm_target(os, arch, abi), arch.target_arch())
158162
}
159163

compiler/rustc_target/src/spec/mod.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1117,6 +1117,8 @@ impl ToJson for FloatAbi {
11171117
/// The Rustc-specific variant of the ABI used for this target.
11181118
#[derive(Clone, Copy, PartialEq, Hash, Debug)]
11191119
pub enum RustcAbi {
1120+
/// On x86-32 only: make use of SSE and SSE2 for ABI purposes.
1121+
X86Sse2,
11201122
/// On x86-32/64 only: do not use any FPU or SIMD registers for the ABI.
11211123
X86Softfloat,
11221124
}
@@ -1126,6 +1128,7 @@ impl FromStr for RustcAbi {
11261128

11271129
fn from_str(s: &str) -> Result<RustcAbi, ()> {
11281130
Ok(match s {
1131+
"x86-sse2" => RustcAbi::X86Sse2,
11291132
"x86-softfloat" => RustcAbi::X86Softfloat,
11301133
_ => return Err(()),
11311134
})
@@ -1135,6 +1138,7 @@ impl FromStr for RustcAbi {
11351138
impl ToJson for RustcAbi {
11361139
fn to_json(&self) -> Json {
11371140
match *self {
1141+
RustcAbi::X86Sse2 => "x86-sse2",
11381142
RustcAbi::X86Softfloat => "x86-softfloat",
11391143
}
11401144
.to_json()
@@ -3269,6 +3273,11 @@ impl Target {
32693273
// Check consistency of Rust ABI declaration.
32703274
if let Some(rust_abi) = self.rustc_abi {
32713275
match rust_abi {
3276+
RustcAbi::X86Sse2 => check_matches!(
3277+
&*self.arch,
3278+
"x86",
3279+
"`x86-sse2` ABI is only valid for x86-32 targets"
3280+
),
32723281
RustcAbi::X86Softfloat => check_matches!(
32733282
&*self.arch,
32743283
"x86" | "x86_64",

compiler/rustc_target/src/spec/targets/i586_pc_nto_qnx700.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::spec::base::nto_qnx;
2-
use crate::spec::{StackProbeType, Target, TargetOptions, base};
2+
use crate::spec::{RustcAbi, StackProbeType, Target, TargetOptions, base};
33

44
pub(crate) fn target() -> Target {
55
let mut meta = nto_qnx::meta();
@@ -14,6 +14,7 @@ pub(crate) fn target() -> Target {
1414
.into(),
1515
arch: "x86".into(),
1616
options: TargetOptions {
17+
rustc_abi: Some(RustcAbi::X86Sse2),
1718
cpu: "pentium4".into(),
1819
max_atomic_width: Some(64),
1920
pre_link_args: nto_qnx::pre_link_args(

compiler/rustc_target/src/spec/targets/i586_unknown_linux_gnu.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use crate::spec::Target;
22

33
pub(crate) fn target() -> Target {
44
let mut base = super::i686_unknown_linux_gnu::target();
5+
base.rustc_abi = None;
56
base.cpu = "pentium".into();
67
base.llvm_target = "i586-unknown-linux-gnu".into();
78
base

compiler/rustc_target/src/spec/targets/i586_unknown_linux_musl.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use crate::spec::Target;
22

33
pub(crate) fn target() -> Target {
44
let mut base = super::i686_unknown_linux_musl::target();
5+
base.rustc_abi = None;
56
base.cpu = "pentium".into();
67
base.llvm_target = "i586-unknown-linux-musl".into();
78
// FIXME(compiler-team#422): musl targets should be dynamically linked by default.

compiler/rustc_target/src/spec/targets/i686_linux_android.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::spec::{SanitizerSet, StackProbeType, Target, TargetOptions, base};
1+
use crate::spec::{RustcAbi, SanitizerSet, StackProbeType, Target, TargetOptions, base};
22

33
// See https://developer.android.com/ndk/guides/abis.html#x86
44
// for target ABI requirements.
@@ -8,6 +8,7 @@ pub(crate) fn target() -> Target {
88

99
base.max_atomic_width = Some(64);
1010

11+
base.rustc_abi = Some(RustcAbi::X86Sse2);
1112
// https://developer.android.com/ndk/guides/abis.html#x86
1213
base.cpu = "pentiumpro".into();
1314
base.features = "+mmx,+sse,+sse2,+sse3,+ssse3".into();

compiler/rustc_target/src/spec/targets/i686_pc_windows_gnu.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
use crate::spec::{Cc, FramePointer, LinkerFlavor, Lld, Target, base};
1+
use crate::spec::{Cc, FramePointer, LinkerFlavor, Lld, RustcAbi, Target, base};
22

33
pub(crate) fn target() -> Target {
44
let mut base = base::windows_gnu::opts();
5+
base.rustc_abi = Some(RustcAbi::X86Sse2);
56
base.cpu = "pentium4".into();
67
base.max_atomic_width = Some(64);
78
base.frame_pointer = FramePointer::Always; // Required for backtraces

compiler/rustc_target/src/spec/targets/i686_pc_windows_gnullvm.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
use crate::spec::{Cc, FramePointer, LinkerFlavor, Lld, Target, base};
1+
use crate::spec::{Cc, FramePointer, LinkerFlavor, Lld, RustcAbi, Target, base};
22

33
pub(crate) fn target() -> Target {
44
let mut base = base::windows_gnullvm::opts();
5+
base.rustc_abi = Some(RustcAbi::X86Sse2);
56
base.cpu = "pentium4".into();
67
base.max_atomic_width = Some(64);
78
base.frame_pointer = FramePointer::Always; // Required for backtraces

0 commit comments

Comments
 (0)