Skip to content

Commit d617ed4

Browse files
committed
Catch incorrect uses of SIMD vectors
1 parent ad10322 commit d617ed4

File tree

3 files changed

+77
-23
lines changed

3 files changed

+77
-23
lines changed

compiler/rustc_typeck/src/collect.rs

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2500,8 +2500,9 @@ fn simd_ffi_feature_check(
25002500
target: &str,
25012501
simd_width: u64,
25022502
simd_elem_width: u64,
2503-
feature: String,
2503+
feature: Option<String>,
25042504
) -> Result<(), Option<&'static str>> {
2505+
let feature = feature.unwrap_or_default();
25052506
match target {
25062507
t if t.contains("x86") => {
25072508
// FIXME: this needs to be architecture dependent and
@@ -2527,7 +2528,7 @@ fn simd_ffi_feature_check(
25272528
32 if feature.contains("avx") => Ok(()),
25282529
32 => Err(Some("avx")),
25292530
64 if feature.contains("avx512") => Ok(()),
2530-
64 => Err(Some("acx512")),
2531+
64 => Err(Some("avx512")),
25312532
_ => Err(None),
25322533
}
25332534
}
@@ -2637,15 +2638,31 @@ fn simd_ffi_check<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId, ast_ty: &hir::Ty<'_>,
26372638
let simd_elem_width = simd_len / simd_size;
26382639
let target: &str = &tcx.sess.target.arch;
26392640

2641+
let type_str = tcx
2642+
.sess
2643+
.source_map()
2644+
.span_to_snippet(ast_ty.span)
2645+
.map_or_else(|_| String::new(), |s| format!("{}", s));
2646+
2647+
if features.is_empty() {
2648+
// Should **NOT** be `Ok()`.
2649+
let f = simd_ffi_feature_check(target, simd_len, simd_elem_width, None).unwrap_err();
2650+
let msg = if let Some(f) = f {
2651+
format!(
2652+
"use of SIMD type `{}` in FFI requires `#[target_feature(enable = \"{}\")]`",
2653+
type_str, f,
2654+
)
2655+
} else {
2656+
format!("use of SIMD type `{}` in FFI not supported by any target features", type_str)
2657+
};
2658+
2659+
tcx.sess.struct_span_err(ast_ty.span, &msg).emit();
2660+
}
2661+
26402662
for f in features {
26412663
if let Err(v) =
2642-
simd_ffi_feature_check(target, simd_len, simd_elem_width, f.to_ident_string())
2664+
simd_ffi_feature_check(target, simd_len, simd_elem_width, Some(f.to_ident_string()))
26432665
{
2644-
let type_str = tcx
2645-
.sess
2646-
.source_map()
2647-
.span_to_snippet(ast_ty.span)
2648-
.map_or_else(|_| String::new(), |s| format!("{}", s));
26492666
let msg = if let Some(f) = v {
26502667
format!(
26512668
"use of SIMD type `{}` in FFI requires `#[target_feature(enable = \"{}\")]`",

src/test/ui/rfcs/rfc-2574-simd-ffi/simd-ffi-x86.rs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// only-x86
12
// only-x86_64
23

34
#![feature(repr_simd)]
@@ -6,6 +7,15 @@
67
#![allow(non_camel_case_types, improper_ctypes)]
78
#![cfg(any(target_arch = "x86", target_arch = "x86_64"))]
89

10+
#[repr(simd)]
11+
struct v128(i128);
12+
13+
#[repr(simd)]
14+
struct v256(i128, i128);
15+
16+
#[repr(simd)]
17+
struct v512(i128, i128, i128, i128);
18+
919
#[repr(simd)]
1020
struct v1024(i128, i128, i128, i128, i128, i128, i128, i128);
1121

@@ -38,22 +48,13 @@ extern {
3848
#[target_feature(enable = "avx512f")]
3949
fn quux3(x: v256);
4050

41-
fn quuux_fail(x: v512); //~ ERROR use of SIMD type `v512` in FFI requires `#[target_feature(enable = "avx512f")]`
51+
fn quuux_fail(x: v512); //~ ERROR use of SIMD type `v512` in FFI requires `#[target_feature(enable = "avx512")]`
4252
#[target_feature(enable = "sse")]
43-
fn quuux_fail2(x: v512); //~ ERROR use of SIMD type `v512` in FFI requires `#[target_feature(enable = "avx512f")]`
53+
fn quuux_fail2(x: v512); //~ ERROR use of SIMD type `v512` in FFI requires `#[target_feature(enable = "avx512")]`
4454
#[target_feature(enable = "avx2")]
45-
fn quuux_fail3(x: v512); //~ ERROR use of SIMD type `v512` in FFI requires `#[target_feature(enable = "avx512f")]`
55+
fn quuux_fail3(x: v512); //~ ERROR use of SIMD type `v512` in FFI requires `#[target_feature(enable = "avx512")]`
4656
#[target_feature(enable = "avx512f")]
4757
fn quuux(x: v512);
4858
}
4959

5060
fn main() {}
51-
52-
#[repr(simd)]
53-
struct v128(i128);
54-
55-
#[repr(simd)]
56-
struct v256(i128, i128);
57-
58-
#[repr(simd)]
59-
struct v512(i128, i128, i128, i128);
Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,56 @@
1+
error: use of SIMD type `v1024` in FFI not supported by any target features
2+
--> $DIR/simd-ffi-x86.rs:13:15
3+
|
4+
LL | fn foo(x: v1024);
5+
| ^^^^^
6+
7+
error: use of SIMD type `v1024` in FFI not supported by any target features
8+
--> $DIR/simd-ffi-x86.rs:14:23
9+
|
10+
LL | fn bar(x: i32, y: v1024);
11+
| ^^^^^
12+
13+
error: use of SIMD type `v1024` in FFI not supported by any target features
14+
--> $DIR/simd-ffi-x86.rs:15:23
15+
|
16+
LL | fn baz(x: i32) -> v1024;
17+
| ^^^^^
18+
19+
error: use of SIMD type `v128` in FFI requires `#[target_feature(enable = "sse")]`
20+
--> $DIR/simd-ffi-x86.rs:17:20
21+
|
22+
LL | fn qux_fail(x: v128);
23+
| ^^^^
24+
25+
error: use of SIMD type `v256` in FFI requires `#[target_feature(enable = "avx")]`
26+
--> $DIR/simd-ffi-x86.rs:31:21
27+
|
28+
LL | fn quux_fail(x: v256);
29+
| ^^^^
30+
131
error: use of SIMD type `v256` in FFI requires `#[target_feature(enable = "avx")]`
232
--> $DIR/simd-ffi-x86.rs:33:22
333
|
434
LL | fn quux_fail2(x: v256);
535
| ^^^^
636

7-
error: use of SIMD type `v512` in FFI requires `#[target_feature(enable = "acx512")]`
37+
error: use of SIMD type `v512` in FFI requires `#[target_feature(enable = "avx512")]`
38+
--> $DIR/simd-ffi-x86.rs:41:22
39+
|
40+
LL | fn quuux_fail(x: v512);
41+
| ^^^^
42+
43+
error: use of SIMD type `v512` in FFI requires `#[target_feature(enable = "avx512")]`
844
--> $DIR/simd-ffi-x86.rs:43:23
945
|
1046
LL | fn quuux_fail2(x: v512);
1147
| ^^^^
1248

13-
error: use of SIMD type `v512` in FFI requires `#[target_feature(enable = "acx512")]`
49+
error: use of SIMD type `v512` in FFI requires `#[target_feature(enable = "avx512")]`
1450
--> $DIR/simd-ffi-x86.rs:45:23
1551
|
1652
LL | fn quuux_fail3(x: v512);
1753
| ^^^^
1854

19-
error: aborting due to 3 previous errors
55+
error: aborting due to 9 previous errors
2056

0 commit comments

Comments
 (0)