Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 086622c

Browse files
committedFeb 14, 2025·
Auto merge of rust-lang#137037 - RalfJung:x86-sse2-abi, r=<try>
add x86-sse2 (32bit) ABI that requires SSE2 target feature This is the first commit of rust-lang#135408: The primary goal of this is to make SSE2 required for our i686 targets (at least for the ones that use Pentium 4 as their baseline), to ensure they cannot be affected by rust-lang#114479. This has been MCPd in rust-lang/compiler-team#808, and is tracked in rust-lang#133611. We do this by defining a new ABI that these targets select, and making SSE2 required by the ABI (that's the first commit). That's kind of a hack, but it is the easiest way to make a target feature required via the target spec. In a follow-up change (rust-lang#135408), we can actually make use of SSE2 for the ABI, but that is running into some infrastructure issues. r? `@workingjubilee` try-job: aarch64-apple try-job: aarch64-gnu try-job: aarch64-gnu-debug try-job: test-various try-job: x86_64-gnu-nopt try-job: dist-i586-gnu-i586-i686-musl
2 parents bdc97d1 + 2eff215 commit 086622c

25 files changed

+96
-20
lines changed
 

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

+7-3
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

+9
Original file line numberDiff line numberDiff line change
@@ -1109,6 +1109,8 @@ impl ToJson for FloatAbi {
11091109
/// The Rustc-specific variant of the ABI used for this target.
11101110
#[derive(Clone, Copy, PartialEq, Hash, Debug)]
11111111
pub enum RustcAbi {
1112+
/// On x86-32 only: make use of SSE and SSE2 for ABI purposes.
1113+
X86Sse2,
11121114
/// On x86-32/64 only: do not use any FPU or SIMD registers for the ABI.
11131115
X86Softfloat,
11141116
}
@@ -1118,6 +1120,7 @@ impl FromStr for RustcAbi {
11181120

11191121
fn from_str(s: &str) -> Result<RustcAbi, ()> {
11201122
Ok(match s {
1123+
"x86-sse2" => RustcAbi::X86Sse2,
11211124
"x86-softfloat" => RustcAbi::X86Softfloat,
11221125
_ => return Err(()),
11231126
})
@@ -1127,6 +1130,7 @@ impl FromStr for RustcAbi {
11271130
impl ToJson for RustcAbi {
11281131
fn to_json(&self) -> Json {
11291132
match *self {
1133+
RustcAbi::X86Sse2 => "x86-sse2",
11301134
RustcAbi::X86Softfloat => "x86-softfloat",
11311135
}
11321136
.to_json()
@@ -3264,6 +3268,11 @@ impl Target {
32643268
// Check consistency of Rust ABI declaration.
32653269
if let Some(rust_abi) = self.rustc_abi {
32663270
match rust_abi {
3271+
RustcAbi::X86Sse2 => check_matches!(
3272+
&*self.arch,
3273+
"x86",
3274+
"`x86-sse2` ABI is only valid for x86-32 targets"
3275+
),
32673276
RustcAbi::X86Softfloat => check_matches!(
32683277
&*self.arch,
32693278
"x86" | "x86_64",

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

+2-1
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

+1
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; // overwrite the SSE2 ABI set by the base target
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

+1
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; // overwrite the SSE2 ABI set by the base target
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

+2-1
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 = "pentium4".into();
1314
base.features = "+mmx,+sse,+sse2,+sse3,+ssse3".into();

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

+2-1
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

+2-1
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

‎compiler/rustc_target/src/spec/targets/i686_pc_windows_msvc.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
use crate::spec::{LinkerFlavor, Lld, SanitizerSet, Target, base};
1+
use crate::spec::{LinkerFlavor, Lld, RustcAbi, SanitizerSet, Target, base};
22

33
pub(crate) fn target() -> Target {
44
let mut base = base::windows_msvc::opts();
5+
base.rustc_abi = Some(RustcAbi::X86Sse2);
56
base.cpu = "pentium4".into();
67
base.max_atomic_width = Some(64);
78
base.supported_sanitizers = SanitizerSet::ADDRESS;

‎compiler/rustc_target/src/spec/targets/i686_unknown_freebsd.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
use crate::spec::{Cc, LinkerFlavor, Lld, StackProbeType, Target, base};
1+
use crate::spec::{Cc, LinkerFlavor, Lld, RustcAbi, StackProbeType, Target, base};
22

33
pub(crate) fn target() -> Target {
44
let mut base = base::freebsd::opts();
5+
base.rustc_abi = Some(RustcAbi::X86Sse2);
56
base.cpu = "pentium4".into();
67
base.max_atomic_width = Some(64);
78
base.add_pre_link_args(LinkerFlavor::Gnu(Cc::Yes, Lld::No), &["-m32", "-Wl,-znotext"]);

‎compiler/rustc_target/src/spec/targets/i686_unknown_haiku.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
use crate::spec::{Cc, LinkerFlavor, Lld, StackProbeType, Target, base};
1+
use crate::spec::{Cc, LinkerFlavor, Lld, RustcAbi, StackProbeType, Target, base};
22

33
pub(crate) fn target() -> Target {
44
let mut base = base::haiku::opts();
5+
base.rustc_abi = Some(RustcAbi::X86Sse2);
56
base.cpu = "pentium4".into();
67
base.max_atomic_width = Some(64);
78
base.add_pre_link_args(LinkerFlavor::Gnu(Cc::Yes, Lld::No), &["-m32"]);

‎compiler/rustc_target/src/spec/targets/i686_unknown_linux_gnu.rs

+13-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,19 @@
1-
use crate::spec::{Cc, LinkerFlavor, Lld, SanitizerSet, StackProbeType, Target, base};
1+
use crate::spec::{Cc, LinkerFlavor, Lld, RustcAbi, SanitizerSet, StackProbeType, Target, base};
22

33
pub(crate) fn target() -> Target {
44
let mut base = base::linux_gnu::opts();
5+
base.rustc_abi = Some(RustcAbi::X86Sse2);
6+
// Dear distribution packager, if you are changing the base CPU model with the goal of removing
7+
// the SSE2 requirement, make sure to also set the `rustc_abi` to `None` above or else the compiler
8+
// will complain that the chosen ABI cannot be realized with the given CPU features.
9+
// Also note that x86 without SSE2 is *not* considered a Tier 1 target by the Rust project, and
10+
// it has some known floating-point correctness issues mostly caused by a lack of people caring
11+
// for LLVM's x87 support (double-rounding, value truncation; see
12+
// <https://github.com/rust-lang/rust/issues/114479> for details). This can lead to incorrect
13+
// math (Rust generally promises exact math, so this can break code in unexpected ways) and it
14+
// can lead to memory safety violations if floating-point values are used e.g. to access an
15+
// array. If users run into such issues and report bugs upstream and then it turns out that the
16+
// bugs are caused by distribution patches, that leads to confusion and frustration.
517
base.cpu = "pentium4".into();
618
base.max_atomic_width = Some(64);
719
base.supported_sanitizers = SanitizerSet::ADDRESS;

‎compiler/rustc_target/src/spec/targets/i686_unknown_linux_musl.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1-
use crate::spec::{Cc, FramePointer, LinkerFlavor, Lld, StackProbeType, Target, base};
1+
use crate::spec::{Cc, FramePointer, LinkerFlavor, Lld, RustcAbi, StackProbeType, Target, base};
22

33
pub(crate) fn target() -> Target {
44
let mut base = base::linux_musl::opts();
5+
base.rustc_abi = Some(RustcAbi::X86Sse2);
6+
// If you want to change the base CPU, please see `i686_unknown_linux_gnu.rs`
7+
// for an important comment.
58
base.cpu = "pentium4".into();
69
base.max_atomic_width = Some(64);
710
base.add_pre_link_args(LinkerFlavor::Gnu(Cc::Yes, Lld::No), &["-m32", "-Wl,-melf_i386"]);

‎compiler/rustc_target/src/spec/targets/i686_unknown_netbsd.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
use crate::spec::{Cc, LinkerFlavor, Lld, StackProbeType, Target, TargetOptions, base};
1+
use crate::spec::{Cc, LinkerFlavor, Lld, RustcAbi, StackProbeType, Target, TargetOptions, base};
22

33
pub(crate) fn target() -> Target {
44
let mut base = base::netbsd::opts();
5+
base.rustc_abi = Some(RustcAbi::X86Sse2);
56
base.cpu = "pentium4".into();
67
base.max_atomic_width = Some(64);
78
base.add_pre_link_args(LinkerFlavor::Gnu(Cc::Yes, Lld::No), &["-m32"]);

‎compiler/rustc_target/src/spec/targets/i686_unknown_openbsd.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
use crate::spec::{Cc, LinkerFlavor, Lld, StackProbeType, Target, base};
1+
use crate::spec::{Cc, LinkerFlavor, Lld, RustcAbi, StackProbeType, Target, base};
22

33
pub(crate) fn target() -> Target {
44
let mut base = base::openbsd::opts();
5+
base.rustc_abi = Some(RustcAbi::X86Sse2);
56
base.cpu = "pentium4".into();
67
base.max_atomic_width = Some(64);
78
base.add_pre_link_args(LinkerFlavor::Gnu(Cc::Yes, Lld::No), &["-m32", "-fuse-ld=lld"]);

‎compiler/rustc_target/src/spec/targets/i686_uwp_windows_gnu.rs

+2-1
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_uwp_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_uwp_windows_msvc.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
use crate::spec::{Target, base};
1+
use crate::spec::{RustcAbi, Target, base};
22

33
pub(crate) fn target() -> Target {
44
let mut base = base::windows_uwp_msvc::opts();
5+
base.rustc_abi = Some(RustcAbi::X86Sse2);
56
base.cpu = "pentium4".into();
67
base.max_atomic_width = Some(64);
78

‎compiler/rustc_target/src/spec/targets/i686_win7_windows_gnu.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
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();
55
base.vendor = "win7".into();
6+
base.rustc_abi = Some(RustcAbi::X86Sse2);
67
base.cpu = "pentium4".into();
78
base.max_atomic_width = Some(64);
89
base.frame_pointer = FramePointer::Always; // Required for backtraces

‎compiler/rustc_target/src/spec/targets/i686_win7_windows_msvc.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
use crate::spec::{LinkerFlavor, Lld, SanitizerSet, Target, base};
1+
use crate::spec::{LinkerFlavor, Lld, RustcAbi, SanitizerSet, Target, base};
22

33
pub(crate) fn target() -> Target {
44
let mut base = base::windows_msvc::opts();
55
base.vendor = "win7".into();
6+
base.rustc_abi = Some(RustcAbi::X86Sse2);
67
base.cpu = "pentium4".into();
78
base.max_atomic_width = Some(64);
89
base.supported_sanitizers = SanitizerSet::ADDRESS;

‎compiler/rustc_target/src/spec/targets/i686_wrs_vxworks.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
use crate::spec::{Cc, LinkerFlavor, Lld, StackProbeType, Target, base};
1+
use crate::spec::{Cc, LinkerFlavor, Lld, RustcAbi, StackProbeType, Target, base};
22

33
pub(crate) fn target() -> Target {
44
let mut base = base::vxworks::opts();
5+
base.rustc_abi = Some(RustcAbi::X86Sse2);
56
base.cpu = "pentium4".into();
67
base.max_atomic_width = Some(64);
78
base.add_pre_link_args(LinkerFlavor::Gnu(Cc::Yes, Lld::No), &["-m32"]);

‎compiler/rustc_target/src/target_features.rs

+8
Original file line numberDiff line numberDiff line change
@@ -790,6 +790,13 @@ impl Target {
790790
// x87 must be enabled, soft-float must be disabled.
791791
FeatureConstraints { required: &["x87"], incompatible: &["soft-float"] }
792792
}
793+
Some(RustcAbi::X86Sse2) => {
794+
// Extended hardfloat ABI. x87 and SSE2 must be enabled, soft-float must be disabled.
795+
FeatureConstraints {
796+
required: &["x87", "sse2"],
797+
incompatible: &["soft-float"],
798+
}
799+
}
793800
Some(RustcAbi::X86Softfloat) => {
794801
// Softfloat ABI, requires corresponding target feature. That feature trumps
795802
// `x87` and all other FPU features so those do not matter.
@@ -817,6 +824,7 @@ impl Target {
817824
// LLVM handles the rest.
818825
FeatureConstraints { required: &["soft-float"], incompatible: &[] }
819826
}
827+
Some(r) => panic!("invalid Rust ABI for x86_64: {r:?}"),
820828
}
821829
}
822830
"arm" => {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
warning: target feature `sse2` must be enabled to ensure that the ABI of the current target can be implemented correctly
2+
|
3+
= note: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
4+
= note: for more information, see issue #116344 <https://github.com/rust-lang/rust/issues/116344>
5+

‎tests/ui/sse-abi-checks.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Ensure we trigger abi_unsupported_vector_types for target features that are usually enabled
22
//! on a target, but disabled in this file via a `-C` flag.
3-
//@ compile-flags: --crate-type=rlib --target=i686-unknown-linux-gnu -C target-feature=-sse,-sse2
3+
//@ compile-flags: --crate-type=rlib --target=i586-unknown-linux-gnu -C target-feature=-sse,-sse2
44
//@ build-pass
55
//@ ignore-pass (test emits codegen-time warnings)
66
//@ needs-llvm-components: x86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//@ compile-flags: --target=i686-unknown-linux-gnu --crate-type=lib
2+
//@ needs-llvm-components: x86
3+
//@ compile-flags: -Ctarget-cpu=pentium
4+
// For now this is just a warning.
5+
//@ build-pass
6+
//@error-pattern: must be enabled
7+
8+
#![feature(no_core, lang_items)]
9+
#![no_core]
10+
11+
#[lang = "sized"]
12+
pub trait Sized {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
warning: target feature `sse2` must be enabled to ensure that the ABI of the current target can be implemented correctly
2+
|
3+
= note: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
4+
= note: for more information, see issue #116344 <https://github.com/rust-lang/rust/issues/116344>
5+
6+
warning: 1 warning emitted
7+

0 commit comments

Comments
 (0)
Please sign in to comment.