Skip to content

Commit dd6eddc

Browse files
committed
Build all tests for better cross-compilation
1 parent 3688f88 commit dd6eddc

File tree

1 file changed

+63
-77
lines changed
  • fearless_simd_dev_macros/src

1 file changed

+63
-77
lines changed

fearless_simd_dev_macros/src/lib.rs

Lines changed: 63 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -23,91 +23,85 @@ pub fn simd_test(_: TokenStream, item: TokenStream) -> TokenStream {
2323
let avx2_name = get_ident("avx2");
2424
let wasm_name = get_ident("wasm");
2525

26-
let include_fallback = !exclude_fallback(&input_fn_name.to_string());
27-
#[cfg(target_arch = "aarch64")]
28-
let include_neon = std::arch::is_aarch64_feature_detected!("neon")
29-
&& !exclude_neon(&input_fn_name.to_string());
30-
#[cfg(not(target_arch = "aarch64"))]
31-
let include_neon = false;
32-
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
33-
let include_sse4 =
34-
std::arch::is_x86_feature_detected!("sse4.2") && !exclude_sse4(&input_fn_name.to_string());
35-
#[cfg(not(any(target_arch = "x86", target_arch = "x86_64")))]
36-
let include_sse4 = false;
37-
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
38-
let include_avx2 = std::arch::is_x86_feature_detected!("avx2")
39-
&& std::arch::is_x86_feature_detected!("fma")
40-
&& !exclude_avx2(&input_fn_name.to_string());
41-
#[cfg(not(any(target_arch = "x86", target_arch = "x86_64")))]
42-
let include_avx2 = false;
43-
// Note that we cannot feature-gate this with `target_arch`. If we run
44-
// `cargo test --target wasm32-wasip1`, then the `target_arch` will still be set to
45-
// the operating system you are running on. Because of this, we instead add the `target_arch`
46-
// feature gate to the actual test.
47-
let include_wasm = !exclude_wasm(&input_fn_name.to_string());
48-
49-
let fallback_snippet = if include_fallback {
50-
quote! {
51-
#[test]
52-
fn #fallback_name() {
53-
let fallback = fearless_simd::Fallback::new();
54-
#input_fn_name(fallback);
55-
}
26+
let ignore_attr = |f: fn(&str) -> bool| {
27+
let should_ignore = f(&input_fn_name.to_string());
28+
if should_ignore {
29+
quote! { #[ignore] }
30+
} else {
31+
quote! {}
5632
}
57-
} else {
58-
quote! {}
5933
};
6034

61-
let neon_snippet = if include_neon {
62-
quote! {
63-
#[cfg(target_arch = "aarch64")]
64-
#[test]
65-
fn #neon_name() {
35+
let ignore_fallback = ignore_attr(exclude_fallback);
36+
let ignore_neon = ignore_attr(exclude_neon);
37+
let ignore_sse4 = ignore_attr(exclude_sse4);
38+
let ignore_avx2 = ignore_attr(exclude_avx2);
39+
let ignore_wasm = ignore_attr(exclude_wasm);
40+
41+
let fallback_snippet = quote! {
42+
#[test]
43+
#ignore_fallback
44+
fn #fallback_name() {
45+
let fallback = fearless_simd::Fallback::new();
46+
#input_fn_name(fallback);
47+
}
48+
};
49+
50+
// All of the architecture-specific tests need to be included every time, and #[cfg]'d out depending on the target
51+
// architecture. We can't use `CARGO_CFG_TARGET_ARCH` to conditionally omit them because it's not available when
52+
// proc macros are evaluated.
53+
54+
let neon_snippet = quote! {
55+
#[cfg(target_arch = "aarch64")]
56+
#[test]
57+
#ignore_neon
58+
fn #neon_name() {
59+
if std::arch::is_aarch64_feature_detected!("neon") {
6660
let neon = unsafe { fearless_simd::aarch64::Neon::new_unchecked() };
6761
#input_fn_name(neon);
6862
}
6963
}
70-
} else {
71-
quote! {}
7264
};
7365

74-
let sse4_snippet = if include_sse4 {
75-
quote! {
76-
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
77-
#[test]
78-
fn #sse4_name() {
79-
let sse4 = unsafe { fearless_simd::x86::Sse4_2::new_unchecked() };
80-
#input_fn_name(sse4);
81-
}
66+
// There is currently no way to conditionally ignore a test at runtime (see
67+
// https://internals.rust-lang.org/t/pre-rfc-skippable-tests/14611). Instead, we have to assert that these CPU
68+
// features are always detected and fail the test if they aren't.
69+
70+
let sse4_snippet = quote! {
71+
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
72+
#[test]
73+
#ignore_sse4
74+
fn #sse4_name() {
75+
assert!(std::arch::is_x86_feature_detected!("sse4.2"));
76+
77+
let sse4 = unsafe { fearless_simd::x86::Sse4_2::new_unchecked() };
78+
#input_fn_name(sse4);
8279
}
83-
} else {
84-
quote! {}
8580
};
8681

87-
let avx2_snippet = if include_avx2 {
88-
quote! {
89-
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
90-
#[test]
91-
fn #avx2_name() {
92-
let avx2 = unsafe { fearless_simd::x86::Avx2::new_unchecked() };
93-
#input_fn_name(avx2);
94-
}
82+
let avx2_snippet = quote! {
83+
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
84+
#[test]
85+
#ignore_avx2
86+
fn #avx2_name() {
87+
assert!(
88+
std::arch::is_x86_feature_detected!("avx2")
89+
&& std::arch::is_x86_feature_detected!("fma")
90+
);
91+
92+
let avx2 = unsafe { fearless_simd::x86::Avx2::new_unchecked() };
93+
#input_fn_name(avx2);
9594
}
96-
} else {
97-
quote! {}
9895
};
9996

100-
let wasm_snippet = if include_wasm {
101-
quote! {
102-
#[cfg(all(target_arch = "wasm32", target_feature = "simd128"))]
103-
#[test]
104-
fn #wasm_name() {
105-
let wasm = unsafe { fearless_simd::wasm32::WasmSimd128::new_unchecked() };
106-
#input_fn_name(wasm);
107-
}
97+
let wasm_snippet = quote! {
98+
#[cfg(all(target_arch = "wasm32", target_feature = "simd128"))]
99+
#[test]
100+
#ignore_wasm
101+
fn #wasm_name() {
102+
let wasm = unsafe { fearless_simd::wasm32::WasmSimd128::new_unchecked() };
103+
#input_fn_name(wasm);
108104
}
109-
} else {
110-
quote! {}
111105
};
112106

113107
quote! {
@@ -125,8 +119,6 @@ pub fn simd_test(_: TokenStream, item: TokenStream) -> TokenStream {
125119
// You can update below functions if you want to exclude certain tests from different architectures
126120
// (for example because they haven't been implemented yet).
127121

128-
#[allow(clippy::allow_attributes, reason = "Lints only apply in some cfgs.")]
129-
#[allow(dead_code, reason = "Used only on aarch64, but always type-checked.")]
130122
fn exclude_neon(_test_name: &str) -> bool {
131123
false
132124
}
@@ -135,8 +127,6 @@ fn exclude_fallback(_test_name: &str) -> bool {
135127
false
136128
}
137129

138-
#[allow(clippy::allow_attributes, reason = "Lints only apply in some cfgs.")]
139-
#[allow(dead_code, reason = "Used only on x86(-64), but always type-checked.")]
140130
fn exclude_sse4(test_name: &str) -> bool {
141131
matches!(
142132
test_name,
@@ -145,8 +135,6 @@ fn exclude_sse4(test_name: &str) -> bool {
145135
) || test_name.contains("precise")
146136
}
147137

148-
#[allow(clippy::allow_attributes, reason = "Lints only apply in some cfgs.")]
149-
#[allow(dead_code, reason = "Used only on x86(-64), but always type-checked.")]
150138
fn exclude_avx2(test_name: &str) -> bool {
151139
matches!(
152140
test_name,
@@ -155,8 +143,6 @@ fn exclude_avx2(test_name: &str) -> bool {
155143
) || test_name.contains("precise")
156144
}
157145

158-
#[allow(clippy::allow_attributes, reason = "Lints only apply in some cfgs.")]
159-
#[allow(dead_code, reason = "Used only on wasm32, but always type-checked.")]
160146
fn exclude_wasm(test_name: &str) -> bool {
161147
matches!(
162148
test_name,

0 commit comments

Comments
 (0)