Skip to content

Commit 42ac64e

Browse files
committed
Fix CI for targets that conditionally disable f16 or f128 support
1 parent 6a80b90 commit 42ac64e

File tree

9 files changed

+127
-115
lines changed

9 files changed

+127
-115
lines changed

build.rs

+2-81
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,8 @@
11
use std::{collections::BTreeMap, env, path::PathBuf, sync::atomic::Ordering};
22

3-
#[allow(dead_code)]
4-
struct Target {
5-
triple: String,
6-
os: String,
7-
arch: String,
8-
vendor: String,
9-
env: String,
10-
pointer_width: u8,
11-
little_endian: bool,
12-
features: Vec<String>,
13-
}
14-
15-
impl Target {
16-
fn from_env() -> Self {
17-
let little_endian = match env::var("CARGO_CFG_TARGET_ENDIAN").unwrap().as_str() {
18-
"little" => true,
19-
"big" => false,
20-
x => panic!("unknown endian {x}"),
21-
};
3+
mod configure;
224

23-
Self {
24-
triple: env::var("TARGET").unwrap(),
25-
os: env::var("CARGO_CFG_TARGET_OS").unwrap(),
26-
arch: env::var("CARGO_CFG_TARGET_ARCH").unwrap(),
27-
vendor: env::var("CARGO_CFG_TARGET_VENDOR").unwrap(),
28-
env: env::var("CARGO_CFG_TARGET_ENV").unwrap(),
29-
pointer_width: env::var("CARGO_CFG_TARGET_POINTER_WIDTH")
30-
.unwrap()
31-
.parse()
32-
.unwrap(),
33-
little_endian,
34-
features: env::var("CARGO_CFG_TARGET_FEATURE")
35-
.unwrap_or_default()
36-
.split(",")
37-
.map(ToOwned::to_owned)
38-
.collect(),
39-
}
40-
}
41-
}
5+
use crate::configure::{configure_f16_f128, Target};
426

437
fn main() {
448
println!("cargo:rerun-if-changed=build.rs");
@@ -261,49 +225,6 @@ fn configure_check_cfg() {
261225
println!("cargo::rustc-check-cfg=cfg(assert_no_panic)");
262226
}
263227

264-
/// Configure whether or not `f16` and `f128` support should be enabled.
265-
fn configure_f16_f128(target: &Target) {
266-
// Set whether or not `f16` and `f128` are supported at a basic level by LLVM. This only means
267-
// that the backend will not crash when using these types. This does not mean that the
268-
// backend does the right thing, or that the platform doesn't have ABI bugs.
269-
//
270-
// We do this here rather than in `rust-lang/rust` because configuring via cargo features is
271-
// not straightforward.
272-
//
273-
// Original source of this list:
274-
// <https://github.com/rust-lang/compiler-builtins/pull/652#issuecomment-2266151350>
275-
let (f16_ok, f128_ok) = match target.arch.as_str() {
276-
// `f16` and `f128` both crash <https://github.com/llvm/llvm-project/issues/94434>
277-
"arm64ec" => (false, false),
278-
// `f16` crashes <https://github.com/llvm/llvm-project/issues/50374>
279-
"s390x" => (false, true),
280-
// `f128` crashes <https://github.com/llvm/llvm-project/issues/96432>
281-
"mips64" | "mips64r6" => (true, false),
282-
// `f128` crashes <https://github.com/llvm/llvm-project/issues/101545>
283-
"powerpc64" if &target.os == "aix" => (true, false),
284-
// `f128` crashes <https://github.com/llvm/llvm-project/issues/41838>
285-
"sparc" | "sparcv9" => (true, false),
286-
// `f16` miscompiles <https://github.com/llvm/llvm-project/issues/96438>
287-
"wasm32" | "wasm64" => (false, true),
288-
// Most everything else works as of LLVM 19
289-
_ => (true, true),
290-
};
291-
292-
// If the feature is set, disable these types.
293-
let disable_both = env::var_os("CARGO_FEATURE_NO_F16_F128").is_some();
294-
295-
println!("cargo::rustc-check-cfg=cfg(f16_enabled)");
296-
println!("cargo::rustc-check-cfg=cfg(f128_enabled)");
297-
298-
if f16_ok && !disable_both {
299-
println!("cargo::rustc-cfg=f16_enabled");
300-
}
301-
302-
if f128_ok && !disable_both {
303-
println!("cargo::rustc-cfg=f128_enabled");
304-
}
305-
}
306-
307228
#[cfg(feature = "c")]
308229
mod c {
309230
use std::collections::{BTreeMap, HashSet};

configure.rs

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
use std::env;
2+
3+
#[allow(dead_code)]
4+
pub(crate) struct Target {
5+
pub(crate) triple: String,
6+
pub(crate) os: String,
7+
pub(crate) arch: String,
8+
pub(crate) vendor: String,
9+
pub(crate) env: String,
10+
pub(crate) pointer_width: u8,
11+
pub(crate) little_endian: bool,
12+
pub(crate) features: Vec<String>,
13+
}
14+
15+
impl Target {
16+
pub(crate) fn from_env() -> Self {
17+
let little_endian = match env::var("CARGO_CFG_TARGET_ENDIAN").unwrap().as_str() {
18+
"little" => true,
19+
"big" => false,
20+
x => panic!("unknown endian {x}"),
21+
};
22+
23+
Self {
24+
triple: env::var("TARGET").unwrap(),
25+
os: env::var("CARGO_CFG_TARGET_OS").unwrap(),
26+
arch: env::var("CARGO_CFG_TARGET_ARCH").unwrap(),
27+
vendor: env::var("CARGO_CFG_TARGET_VENDOR").unwrap(),
28+
env: env::var("CARGO_CFG_TARGET_ENV").unwrap(),
29+
pointer_width: env::var("CARGO_CFG_TARGET_POINTER_WIDTH")
30+
.unwrap()
31+
.parse()
32+
.unwrap(),
33+
little_endian,
34+
features: env::var("CARGO_CFG_TARGET_FEATURE")
35+
.unwrap_or_default()
36+
.split(",")
37+
.map(ToOwned::to_owned)
38+
.collect(),
39+
}
40+
}
41+
}
42+
43+
/// Configure whether or not `f16` and `f128` support should be enabled.
44+
pub(crate) fn configure_f16_f128(target: &Target) {
45+
// Set whether or not `f16` and `f128` are supported at a basic level by LLVM. This only means
46+
// that the backend will not crash when using these types. This does not mean that the
47+
// backend does the right thing, or that the platform doesn't have ABI bugs.
48+
//
49+
// We do this here rather than in `rust-lang/rust` because configuring via cargo features is
50+
// not straightforward.
51+
//
52+
// Original source of this list:
53+
// <https://github.com/rust-lang/compiler-builtins/pull/652#issuecomment-2266151350>
54+
let (f16_ok, f128_ok) = match target.arch.as_str() {
55+
// `f16` and `f128` both crash <https://github.com/llvm/llvm-project/issues/94434>
56+
"arm64ec" => (false, false),
57+
// `f16` crashes <https://github.com/llvm/llvm-project/issues/50374>
58+
"s390x" => (false, true),
59+
// `f128` crashes <https://github.com/llvm/llvm-project/issues/96432>
60+
"mips64" | "mips64r6" => (true, false),
61+
// `f128` crashes <https://github.com/llvm/llvm-project/issues/101545>
62+
"powerpc64" if &target.os == "aix" => (true, false),
63+
// `f128` crashes <https://github.com/llvm/llvm-project/issues/41838>
64+
"sparc" | "sparcv9" => (true, false),
65+
// `f16` miscompiles <https://github.com/llvm/llvm-project/issues/96438>
66+
"wasm32" | "wasm64" => (false, true),
67+
// Most everything else works as of LLVM 19
68+
_ => (true, true),
69+
};
70+
71+
// If the feature is set, disable these types.
72+
let disable_both = env::var_os("CARGO_FEATURE_NO_F16_F128").is_some();
73+
74+
println!("cargo::rustc-check-cfg=cfg(f16_enabled)");
75+
println!("cargo::rustc-check-cfg=cfg(f128_enabled)");
76+
77+
if f16_ok && !disable_both {
78+
println!("cargo::rustc-cfg=f16_enabled");
79+
}
80+
81+
if f128_ok && !disable_both {
82+
println!("cargo::rustc-cfg=f128_enabled");
83+
}
84+
}

testcrate/build.rs

+26-19
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::{collections::HashSet, env};
1+
use std::collections::HashSet;
22

33
/// Features to enable
44
#[derive(Debug, PartialEq, Eq, Hash)]
@@ -9,35 +9,39 @@ enum Feature {
99
NoSysF16F128Convert,
1010
}
1111

12+
mod builtins_configure {
13+
include!("../configure.rs");
14+
}
15+
1216
fn main() {
13-
let target = env::var("TARGET").unwrap();
17+
let target = builtins_configure::Target::from_env();
1418
let mut features = HashSet::new();
1519

1620
// These platforms do not have f128 symbols available in their system libraries, so
1721
// skip related tests.
18-
if target.starts_with("arm-")
19-
|| target.contains("apple-darwin")
20-
|| target.contains("windows-msvc")
22+
if target.arch == "arm"
23+
|| target.vendor == "apple"
24+
|| target.env == "msvc"
2125
// GCC and LLVM disagree on the ABI of `f16` and `f128` with MinGW. See
2226
// <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=115054>.
23-
|| target.contains("windows-gnu")
27+
|| (target.os == "windows" && target.env == "gnu")
2428
// FIXME(llvm): There is an ABI incompatibility between GCC and Clang on 32-bit x86.
2529
// See <https://github.com/llvm/llvm-project/issues/77401>.
26-
|| target.starts_with("i686")
30+
|| target.arch == "i686"
2731
// 32-bit PowerPC and 64-bit LE gets code generated that Qemu cannot handle. See
2832
// <https://github.com/rust-lang/compiler-builtins/pull/606#issuecomment-2105635926>.
29-
|| target.starts_with("powerpc-")
30-
|| target.starts_with("powerpc64le-")
33+
|| target.arch == "powerpc"
34+
|| target.arch == "powerpc64le"
3135
// FIXME: We get different results from the builtin functions. See
3236
// <https://github.com/rust-lang/compiler-builtins/pull/606#issuecomment-2105657287>.
33-
|| target.starts_with("powerpc64-")
37+
|| target.arch == "powerpc64"
3438
{
3539
features.insert(Feature::NoSysF128);
3640
features.insert(Feature::NoSysF128IntConvert);
3741
features.insert(Feature::NoSysF16F128Convert);
3842
}
3943

40-
if target.starts_with("i586") || target.starts_with("i686") {
44+
if target.arch == "i586" || target.arch == "i686" {
4145
// 32-bit x86 does not have `__fixunstfti`/`__fixtfti` but does have everything else
4246
features.insert(Feature::NoSysF128IntConvert);
4347
// FIXME: 32-bit x86 has a bug in `f128 -> f16` system libraries
@@ -46,17 +50,18 @@ fn main() {
4650

4751
// These platforms do not have f16 symbols available in their system libraries, so
4852
// skip related tests. Most of these are missing `f16 <-> f32` conversion routines.
49-
if (target.starts_with("aarch64-") && target.contains("linux"))
50-
|| target.starts_with("arm")
51-
|| target.starts_with("powerpc-")
52-
|| target.starts_with("powerpc64-")
53-
|| target.starts_with("powerpc64le-")
54-
|| target.starts_with("i586-")
55-
|| target.contains("windows-")
53+
if (target.arch == "aarch64" && target.os == "linux")
54+
|| target.arch == "arm"
55+
|| target.arch == "powerpc"
56+
|| target.arch == "powerpc64le"
57+
|| target.arch == "powerpc64le"
58+
|| target.arch == "i586"
59+
|| target.os == "windows"
5660
// Linking says "error: function signature mismatch: __extendhfsf2" and seems to
5761
// think the signature is either `(i32) -> f32` or `(f32) -> f32`. See
5862
// <https://github.com/llvm/llvm-project/issues/96438>.
59-
|| target.starts_with("wasm")
63+
|| target.arch == "wasm32"
64+
|| target.arch == "wasm64"
6065
{
6166
features.insert(Feature::NoSysF16);
6267
features.insert(Feature::NoSysF16F128Convert);
@@ -78,4 +83,6 @@ fn main() {
7883
println!("cargo:warning={warning}");
7984
println!("cargo:rustc-cfg=feature=\"{name}\"");
8085
}
86+
87+
builtins_configure::configure_f16_f128(&target);
8188
}

testcrate/src/bench.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ macro_rules! impl_testio {
350350
}
351351
}
352352

353-
#[cfg(not(feature = "no-f16-f128"))]
353+
#[cfg(all(f16_enabled, f128_enabled))]
354354
impl_testio!(float f16, f128);
355355
impl_testio!(float f32, f64);
356356
impl_testio!(int i16, i32, i64, i128);

testcrate/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
//! Some floating point tests are disabled for specific architectures, because they do not have
1414
//! correct rounding.
1515
#![no_std]
16-
#![cfg_attr(not(feature = "no-f16-f128"), feature(f128))]
17-
#![cfg_attr(not(feature = "no-f16-f128"), feature(f16))]
16+
#![cfg_attr(f128_enabled, feature(f128))]
17+
#![cfg_attr(f16_enabled, feature(f16))]
1818
#![feature(isqrt)]
1919

2020
pub mod bench;

testcrate/tests/addsub.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ mod float_addsub {
120120
}
121121
}
122122

123-
#[cfg(not(feature = "no-f16-f128"))]
123+
#[cfg(f128_enabled)]
124124
#[cfg(not(all(target_arch = "x86", not(target_feature = "sse"))))]
125125
#[cfg(not(any(target_arch = "powerpc", target_arch = "powerpc64")))]
126126
mod float_addsub_f128 {
@@ -131,7 +131,7 @@ mod float_addsub_f128 {
131131
}
132132
}
133133

134-
#[cfg(not(feature = "no-f16-f128"))]
134+
#[cfg(f128_enabled)]
135135
#[cfg(any(target_arch = "powerpc", target_arch = "powerpc64"))]
136136
mod float_addsub_f128_ppc {
137137
use super::*;

testcrate/tests/cmp.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ mod float_comparisons {
9494
}
9595

9696
#[test]
97-
#[cfg(not(feature = "no-f16-f128"))]
97+
#[cfg(f128_enabled)]
9898
fn cmp_f128() {
9999
#[cfg(any(target_arch = "powerpc", target_arch = "powerpc64"))]
100100
use compiler_builtins::float::cmp::{

testcrate/tests/conv.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
#![cfg_attr(not(feature = "no-f16-f128"), feature(f16))]
2-
#![cfg_attr(not(feature = "no-f16-f128"), feature(f128))]
1+
#![cfg_attr(f128_enabled, feature(f128))]
2+
#![cfg_attr(f16_enabled, feature(f16))]
33
// makes configuration easier
44
#![allow(unused_macros)]
55
#![allow(unused_imports)]
@@ -176,7 +176,7 @@ mod f_to_i {
176176
}
177177

178178
#[test]
179-
#[cfg(not(feature = "no-f16-f128"))]
179+
#[cfg(f128_enabled)]
180180
fn f128_to_int() {
181181
#[cfg(any(target_arch = "powerpc", target_arch = "powerpc64"))]
182182
use compiler_builtins::float::conv::{
@@ -264,7 +264,7 @@ mod extend {
264264
f32 => f64, Single => Double, __extendsfdf2vfp, all();
265265
}
266266

267-
#[cfg(not(feature = "no-f16-f128"))]
267+
#[cfg(all(f16_enabled, f128_enabled))]
268268
#[cfg(not(any(target_arch = "powerpc", target_arch = "powerpc64")))]
269269
f_to_f! {
270270
extend,
@@ -275,7 +275,7 @@ mod extend {
275275
f64 => f128, Double => Quad, __extenddftf2, not(feature = "no-sys-f128");
276276
}
277277

278-
#[cfg(not(feature = "no-f16-f128"))]
278+
#[cfg(f128_enabled)]
279279
#[cfg(any(target_arch = "powerpc", target_arch = "powerpc64"))]
280280
f_to_f! {
281281
extend,
@@ -299,7 +299,7 @@ mod trunc {
299299
f64 => f32, Double => Single, __truncdfsf2vfp, all();
300300
}
301301

302-
#[cfg(not(feature = "no-f16-f128"))]
302+
#[cfg(all(f16_enabled, f128_enabled))]
303303
#[cfg(not(any(target_arch = "powerpc", target_arch = "powerpc64")))]
304304
f_to_f! {
305305
trunc,
@@ -310,7 +310,7 @@ mod trunc {
310310
f128 => f64, Quad => Double, __trunctfdf2, not(feature = "no-sys-f128");
311311
}
312312

313-
#[cfg(not(feature = "no-f16-f128"))]
313+
#[cfg(f128_enabled)]
314314
#[cfg(any(target_arch = "powerpc", target_arch = "powerpc64"))]
315315
f_to_f! {
316316
trunc,

testcrate/tests/mul.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ mod float_mul {
131131
}
132132
}
133133

134-
#[cfg(not(feature = "no-f16-f128"))]
134+
#[cfg(f128_enabled)]
135135
#[cfg(not(all(target_arch = "x86", not(target_feature = "sse"))))]
136136
#[cfg(not(any(target_arch = "powerpc", target_arch = "powerpc64")))]
137137
mod float_mul_f128 {
@@ -145,7 +145,7 @@ mod float_mul_f128 {
145145
}
146146
}
147147

148-
#[cfg(not(feature = "no-f16-f128"))]
148+
#[cfg(f128_enabled)]
149149
#[cfg(any(target_arch = "powerpc", target_arch = "powerpc64"))]
150150
mod float_mul_f128_ppc {
151151
use super::*;

0 commit comments

Comments
 (0)