Skip to content

Commit a6503b1

Browse files
committed
Auto merge of #143405 - tgross35:update-builtins, r=tgross35
Update the `compiler-builtins` subtree Update the Josh subtree to rust-lang/compiler-builtins@ed17b95715dd. r? `@ghost`
2 parents 556d20a + be35d37 commit a6503b1

File tree

25 files changed

+465
-258
lines changed

25 files changed

+465
-258
lines changed

library/compiler-builtins/builtins-test-intrinsics/build.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,5 @@ fn main() {
66
println!("cargo::rerun-if-changed=../configure.rs");
77

88
let target = builtins_configure::Target::from_env();
9-
builtins_configure::configure_f16_f128(&target);
109
builtins_configure::configure_aliases(&target);
1110
}

library/compiler-builtins/builtins-test/benches/float_cmp.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ float_bench! {
177177
],
178178
}
179179

180+
#[cfg(f128_enabled)]
180181
float_bench! {
181182
name: cmp_f128_gt,
182183
sig: (a: f128, b: f128) -> CmpResult,
@@ -189,6 +190,7 @@ float_bench! {
189190
asm: []
190191
}
191192

193+
#[cfg(f128_enabled)]
192194
float_bench! {
193195
name: cmp_f128_unord,
194196
sig: (a: f128, b: f128) -> CmpResult,

library/compiler-builtins/builtins-test/build.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,5 +116,4 @@ fn main() {
116116
}
117117

118118
builtins_configure::configure_aliases(&target);
119-
builtins_configure::configure_f16_f128(&target);
120119
}

library/compiler-builtins/builtins-test/tests/conv.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ mod i_to_f {
118118
i128, __floattidf;
119119
}
120120

121-
#[cfg(not(feature = "no-f16-f128"))]
121+
#[cfg(f128_enabled)]
122122
#[cfg(not(any(target_arch = "powerpc", target_arch = "powerpc64")))]
123123
i_to_f! { f128, Quad, not(feature = "no-sys-f128-int-convert"),
124124
u32, __floatunsitf;
@@ -129,7 +129,7 @@ mod i_to_f {
129129
i128, __floattitf;
130130
}
131131

132-
#[cfg(not(feature = "no-f16-f128"))]
132+
#[cfg(f128_enabled)]
133133
#[cfg(any(target_arch = "powerpc", target_arch = "powerpc64"))]
134134
i_to_f! { f128, Quad, not(feature = "no-sys-f128-int-convert"),
135135
u32, __floatunsikf;

library/compiler-builtins/builtins-test/tests/div_rem.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ mod float_div {
147147
f64, __divdf3, Double, all();
148148
}
149149

150-
#[cfg(not(feature = "no-f16-f128"))]
150+
#[cfg(f128_enabled)]
151151
#[cfg(not(any(target_arch = "powerpc", target_arch = "powerpc64")))]
152152
float! {
153153
f128, __divtf3, Quad,
@@ -156,7 +156,7 @@ mod float_div {
156156
not(any(feature = "no-sys-f128", all(target_arch = "aarch64", target_os = "linux")));
157157
}
158158

159-
#[cfg(not(feature = "no-f16-f128"))]
159+
#[cfg(f128_enabled)]
160160
#[cfg(any(target_arch = "powerpc", target_arch = "powerpc64"))]
161161
float! {
162162
f128, __divkf3, Quad, not(feature = "no-sys-f128");

library/compiler-builtins/compiler-builtins/build.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ mod configure;
22

33
use std::env;
44

5-
use configure::{Target, configure_aliases, configure_f16_f128};
5+
use configure::{Target, configure_aliases};
66

77
fn main() {
88
println!("cargo::rerun-if-changed=build.rs");
@@ -12,7 +12,6 @@ fn main() {
1212
let cwd = env::current_dir().unwrap();
1313

1414
configure_check_cfg();
15-
configure_f16_f128(&target);
1615
configure_aliases(&target);
1716

1817
configure_libm(&target);
Lines changed: 30 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Configuration that is shared between `compiler_builtins` and `builtins_test`.
22

3-
use std::env;
3+
use std::process::{Command, Stdio};
4+
use std::{env, str};
45

56
#[derive(Debug)]
67
#[allow(dead_code)]
@@ -16,6 +17,8 @@ pub struct Target {
1617
pub pointer_width: u8,
1718
pub little_endian: bool,
1819
pub features: Vec<String>,
20+
pub reliable_f128: bool,
21+
pub reliable_f16: bool,
1922
}
2023

2124
impl Target {
@@ -32,6 +35,19 @@ impl Target {
3235
.map(|s| s.to_lowercase().replace("_", "-"))
3336
.collect();
3437

38+
// Query rustc for options that Cargo does not provide env for. The bootstrap hack is used
39+
// to get consistent output regardless of channel (`f16`/`f128` config options are hidden
40+
// on stable otherwise).
41+
let mut cmd = Command::new(env::var("RUSTC").unwrap());
42+
cmd.args(["--print=cfg", "--target", &triple])
43+
.env("RUSTC_BOOTSTRAP", "1")
44+
.stderr(Stdio::inherit());
45+
let out = cmd
46+
.output()
47+
.unwrap_or_else(|e| panic!("failed to run `{cmd:?}`: {e}"));
48+
assert!(out.status.success(), "failed to run `{cmd:?}`");
49+
let rustc_cfg = str::from_utf8(&out.stdout).unwrap();
50+
3551
Self {
3652
triple,
3753
triple_split,
@@ -51,6 +67,8 @@ impl Target {
5167
.split(",")
5268
.map(ToOwned::to_owned)
5369
.collect(),
70+
reliable_f128: rustc_cfg.lines().any(|l| l == "target_has_reliable_f128"),
71+
reliable_f16: rustc_cfg.lines().any(|l| l == "target_has_reliable_f16"),
5472
}
5573
}
5674

@@ -74,63 +92,24 @@ pub fn configure_aliases(target: &Target) {
7492
if target.triple_split[0] == "thumbv6m" || target.triple_split[0] == "thumbv8m.base" {
7593
println!("cargo:rustc-cfg=thumb_1")
7694
}
77-
}
78-
79-
/// Configure whether or not `f16` and `f128` support should be enabled.
80-
pub fn configure_f16_f128(target: &Target) {
81-
// Set whether or not `f16` and `f128` are supported at a basic level by LLVM. This only means
82-
// that the backend will not crash when using these types and generates code that can be called
83-
// without crashing (no infinite recursion). This does not mean that the platform doesn't have
84-
// ABI or other bugs.
85-
//
86-
// We do this here rather than in `rust-lang/rust` because configuring via cargo features is
87-
// not straightforward.
88-
//
89-
// Original source of this list:
90-
// <https://github.com/rust-lang/compiler-builtins/pull/652#issuecomment-2266151350>
91-
let f16_enabled = match target.arch.as_str() {
92-
// Unsupported <https://github.com/llvm/llvm-project/issues/94434>
93-
"arm64ec" => false,
94-
// Selection failure <https://github.com/llvm/llvm-project/issues/50374>
95-
"s390x" => false,
96-
// Infinite recursion <https://github.com/llvm/llvm-project/issues/97981>
97-
"csky" => false,
98-
"hexagon" => false,
99-
"powerpc" | "powerpc64" => false,
100-
"sparc" | "sparc64" => false,
101-
"wasm32" | "wasm64" => false,
102-
// Most everything else works as of LLVM 19
103-
_ => true,
104-
};
10595

106-
let f128_enabled = match target.arch.as_str() {
107-
// Unsupported (libcall is not supported) <https://github.com/llvm/llvm-project/issues/121122>
108-
"amdgpu" => false,
109-
// Unsupported <https://github.com/llvm/llvm-project/issues/94434>
110-
"arm64ec" => false,
111-
// FIXME(llvm20): fixed by <https://github.com/llvm/llvm-project/pull/117525>
112-
"mips64" | "mips64r6" => false,
113-
// Selection failure <https://github.com/llvm/llvm-project/issues/95471>
114-
"nvptx64" => false,
115-
// Selection failure <https://github.com/llvm/llvm-project/issues/101545>
116-
"powerpc64" if &target.os == "aix" => false,
117-
// Selection failure <https://github.com/llvm/llvm-project/issues/41838>
118-
"sparc" => false,
119-
// Most everything else works as of LLVM 19
120-
_ => true,
121-
};
96+
/* Not all backends support `f16` and `f128` to the same level on all architectures, so we
97+
* need to disable things if the compiler may crash. See configuration at:
98+
* * https://github.com/rust-lang/rust/blob/c65dccabacdfd6c8a7f7439eba13422fdd89b91e/compiler/rustc_codegen_llvm/src/llvm_util.rs#L367-L432
99+
* * https://github.com/rust-lang/rustc_codegen_gcc/blob/4b5c44b14166083eef8d71f15f5ea1f53fc976a0/src/lib.rs#L496-L507
100+
* * https://github.com/rust-lang/rustc_codegen_cranelift/blob/c713ffab3c6e28ab4b4dd4e392330f786ea657ad/src/lib.rs#L196-L226
101+
*/
122102

123-
// If the feature is set, disable these types.
124-
let disable_both = env::var_os("CARGO_FEATURE_NO_F16_F128").is_some();
103+
// If the feature is set, disable both of these types.
104+
let no_f16_f128 = target.cargo_features.iter().any(|s| s == "no-f16-f128");
125105

126106
println!("cargo::rustc-check-cfg=cfg(f16_enabled)");
127-
println!("cargo::rustc-check-cfg=cfg(f128_enabled)");
128-
129-
if f16_enabled && !disable_both {
107+
if target.reliable_f16 && !no_f16_f128 {
130108
println!("cargo::rustc-cfg=f16_enabled");
131109
}
132110

133-
if f128_enabled && !disable_both {
111+
println!("cargo::rustc-check-cfg=cfg(f128_enabled)");
112+
if target.reliable_f128 && !no_f16_f128 {
134113
println!("cargo::rustc-cfg=f128_enabled");
135114
}
136115
}

library/compiler-builtins/compiler-builtins/src/aarch64.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use core::intrinsics;
55
intrinsics! {
66
#[unsafe(naked)]
77
#[cfg(all(target_os = "uefi", not(feature = "no-asm")))]
8-
pub unsafe extern "C" fn __chkstk() {
8+
pub unsafe extern "custom" fn __chkstk() {
99
core::arch::naked_asm!(
1010
".p2align 2",
1111
"lsl x16, x15, #4",

library/compiler-builtins/compiler-builtins/src/arm.rs

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,18 @@ unsafe extern "C" {
99
}
1010

1111
// SAFETY: these are defined in compiler-builtins
12-
// FIXME(extern_custom), this isn't always the correct ABI
13-
unsafe extern "aapcs" {
12+
unsafe extern "custom" {
1413
// AAPCS is not always the correct ABI for these intrinsics, but we only use this to
1514
// forward another `__aeabi_` call so it doesn't matter.
16-
fn __aeabi_idiv(a: i32, b: i32) -> i32;
15+
fn __aeabi_idiv();
1716
}
1817

1918
intrinsics! {
2019
// NOTE This function and the ones below are implemented using assembly because they are using a
2120
// custom calling convention which can't be implemented using a normal Rust function.
2221
#[unsafe(naked)]
2322
#[cfg(not(target_env = "msvc"))]
24-
pub unsafe extern "C" fn __aeabi_uidivmod() {
23+
pub unsafe extern "custom" fn __aeabi_uidivmod() {
2524
core::arch::naked_asm!(
2625
"push {{lr}}",
2726
"sub sp, sp, #4",
@@ -35,7 +34,7 @@ intrinsics! {
3534
}
3635

3736
#[unsafe(naked)]
38-
pub unsafe extern "C" fn __aeabi_uldivmod() {
37+
pub unsafe extern "custom" fn __aeabi_uldivmod() {
3938
core::arch::naked_asm!(
4039
"push {{r4, lr}}",
4140
"sub sp, sp, #16",
@@ -51,7 +50,7 @@ intrinsics! {
5150
}
5251

5352
#[unsafe(naked)]
54-
pub unsafe extern "C" fn __aeabi_idivmod() {
53+
pub unsafe extern "custom" fn __aeabi_idivmod() {
5554
core::arch::naked_asm!(
5655
"push {{r0, r1, r4, lr}}",
5756
"bl {trampoline}",
@@ -64,7 +63,7 @@ intrinsics! {
6463
}
6564

6665
#[unsafe(naked)]
67-
pub unsafe extern "C" fn __aeabi_ldivmod() {
66+
pub unsafe extern "custom" fn __aeabi_ldivmod() {
6867
core::arch::naked_asm!(
6968
"push {{r4, lr}}",
7069
"sub sp, sp, #16",
@@ -135,8 +134,8 @@ intrinsics! {
135134
/// eight bytes.
136135
#[cfg(not(target_vendor = "apple"))]
137136
pub unsafe extern "aapcs" fn __aeabi_memcpy8(dst: *mut u8, src: *const u8, n: usize) {
138-
debug_assert!(dst.addr() & 7 == 0);
139-
debug_assert!(src.addr() & 7 == 0);
137+
debug_assert!(dst.addr().is_multiple_of(8));
138+
debug_assert!(src.addr().is_multiple_of(8));
140139

141140
// SAFETY: memcpy preconditions apply, less strict alignment.
142141
unsafe { __aeabi_memcpy4(dst, src, n) };
@@ -161,8 +160,8 @@ intrinsics! {
161160
/// four bytes.
162161
#[cfg(not(any(target_vendor = "apple", target_env = "msvc")))]
163162
pub unsafe extern "aapcs" fn __aeabi_memmove4(dst: *mut u8, src: *const u8, n: usize) {
164-
debug_assert!(dst.addr() & 3 == 0);
165-
debug_assert!(src.addr() & 3 == 0);
163+
debug_assert!(dst.addr().is_multiple_of(4));
164+
debug_assert!(src.addr().is_multiple_of(4));
166165

167166
// SAFETY: same preconditions, less strict aligment.
168167
unsafe { __aeabi_memmove(dst, src, n) };
@@ -176,8 +175,8 @@ intrinsics! {
176175
/// eight bytes.
177176
#[cfg(not(any(target_vendor = "apple", target_env = "msvc")))]
178177
pub unsafe extern "aapcs" fn __aeabi_memmove8(dst: *mut u8, src: *const u8, n: usize) {
179-
debug_assert!(dst.addr() & 7 == 0);
180-
debug_assert!(src.addr() & 7 == 0);
178+
debug_assert!(dst.addr().is_multiple_of(8));
179+
debug_assert!(src.addr().is_multiple_of(8));
181180

182181
// SAFETY: memmove preconditions apply, less strict alignment.
183182
unsafe { __aeabi_memmove(dst, src, n) };
@@ -236,7 +235,7 @@ intrinsics! {
236235
/// eight bytes.
237236
#[cfg(not(target_vendor = "apple"))]
238237
pub unsafe extern "aapcs" fn __aeabi_memset8(dst: *mut u8, n: usize, c: i32) {
239-
debug_assert!(dst.addr() & 7 == 0);
238+
debug_assert!(dst.addr().is_multiple_of(8));
240239

241240
// SAFETY: memset preconditions apply, less strict alignment.
242241
unsafe { __aeabi_memset4(dst, n, c) };
@@ -261,7 +260,7 @@ intrinsics! {
261260
/// four bytes.
262261
#[cfg(not(any(target_vendor = "apple", target_env = "msvc")))]
263262
pub unsafe extern "aapcs" fn __aeabi_memclr4(dst: *mut u8, n: usize) {
264-
debug_assert!(dst.addr() & 3 == 0);
263+
debug_assert!(dst.addr().is_multiple_of(4));
265264

266265
// SAFETY: memclr preconditions apply, less strict alignment.
267266
unsafe { __aeabi_memset4(dst, n, 0) };
@@ -275,7 +274,7 @@ intrinsics! {
275274
/// eight bytes.
276275
#[cfg(not(any(target_vendor = "apple", target_env = "msvc")))]
277276
pub unsafe extern "aapcs" fn __aeabi_memclr8(dst: *mut u8, n: usize) {
278-
debug_assert!(dst.addr() & 7 == 0);
277+
debug_assert!(dst.addr().is_multiple_of(8));
279278

280279
// SAFETY: memclr preconditions apply, less strict alignment.
281280
unsafe { __aeabi_memset4(dst, n, 0) };

library/compiler-builtins/compiler-builtins/src/int/udiv.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ intrinsics! {
4444
}
4545

4646
#[unsafe(naked)]
47-
pub unsafe extern "C" fn __udivmodqi4() {
47+
pub unsafe extern "custom" fn __udivmodqi4() {
4848
// compute unsigned 8-bit `n / d` and `n % d`.
4949
//
5050
// Note: GCC implements a [non-standard calling convention](https://gcc.gnu.org/wiki/avr-gcc#Exceptions_to_the_Calling_Convention) for this function.

library/compiler-builtins/compiler-builtins/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
#![cfg_attr(feature = "compiler-builtins", compiler_builtins)]
22
#![cfg_attr(all(target_family = "wasm"), feature(wasm_numeric_instr))]
3+
#![feature(abi_custom)]
34
#![feature(abi_unadjusted)]
45
#![feature(asm_experimental_arch)]
56
#![feature(cfg_target_has_atomic)]
67
#![feature(compiler_builtins)]
78
#![feature(core_intrinsics)]
89
#![feature(linkage)]
10+
#![feature(asm_cfg)]
911
#![feature(naked_functions)]
1012
#![feature(repr_simd)]
1113
#![feature(macro_metavar_expr_concat)]

0 commit comments

Comments
 (0)