]> git.lizzy.rs Git - rust.git/blob - src/test/ui/simd/shuffle-not-out-of-bounds.rs
Rollup merge of #102685 - nbdd0121:unwind, r=m-ou-se
[rust.git] / src / test / ui / simd / shuffle-not-out-of-bounds.rs
1 // build-fail
2 #![allow(non_camel_case_types)]
3 #![feature(repr_simd, platform_intrinsics)]
4
5 // Test for #73542 to verify out-of-bounds shuffle vectors do not compile.
6
7 #[repr(simd)]
8 #[derive(Copy, Clone)]
9 struct u8x2([u8; 2]);
10
11 #[repr(simd)]
12 #[derive(Copy, Clone)]
13 struct u8x4([u8; 4]);
14
15 #[repr(simd)]
16 #[derive(Copy, Clone)]
17 struct u8x8([u8; 8]);
18
19 #[repr(simd)]
20 #[derive(Copy, Clone)]
21 struct u8x16([u8; 16]);
22
23 #[repr(simd)]
24 #[derive(Copy, Clone)]
25 struct u8x32([u8; 32]);
26
27 #[repr(simd)]
28 #[derive(Copy, Clone)]
29 struct u8x64([u8; 64]);
30
31 extern "platform-intrinsic" {
32     pub fn simd_shuffle2<T, U>(x: T, y: T, idx: [u32; 2]) -> U;
33     pub fn simd_shuffle4<T, U>(x: T, y: T, idx: [u32; 4]) -> U;
34     pub fn simd_shuffle8<T, U>(x: T, y: T, idx: [u32; 8]) -> U;
35     pub fn simd_shuffle16<T, U>(x: T, y: T, idx: [u32; 16]) -> U;
36     pub fn simd_shuffle32<T, U>(x: T, y: T, idx: [u32; 32]) -> U;
37     pub fn simd_shuffle64<T, U>(x: T, y: T, idx: [u32; 64]) -> U;
38 }
39
40 // Test vectors by lane size. Since LLVM does not distinguish between a shuffle
41 // over two f32s and a shuffle over two u64s, or any other such combination,
42 // it is not necessary to test every possible vector, only lane counts.
43 macro_rules! test_shuffle_lanes {
44     ($n:literal, $x:ident, $y:ident) => {
45         unsafe {
46                 let shuffle: $x = {
47                     const ARR: [u32; $n] = {
48                         let mut arr = [0; $n];
49                         arr[0] = $n * 2;
50                         arr
51                     };
52                     let mut n: u8 = $n;
53                     let vals = [0; $n].map(|_| { n = n - 1; n });
54                     let vec1 = $x(vals);
55                     let vec2 = $x(vals);
56                     $y(vec1, vec2, ARR)
57                 };
58         }
59     }
60 }
61 //~^^^^^ ERROR: invalid monomorphization of `simd_shuffle2` intrinsic
62 //~| ERROR: invalid monomorphization of `simd_shuffle4` intrinsic
63 //~| ERROR: invalid monomorphization of `simd_shuffle8` intrinsic
64 //~| ERROR: invalid monomorphization of `simd_shuffle16` intrinsic
65 //~| ERROR: invalid monomorphization of `simd_shuffle32` intrinsic
66 //~| ERROR: invalid monomorphization of `simd_shuffle64` intrinsic
67 // Because the test is mostly embedded in a macro, all the errors have the same origin point.
68 // And unfortunately, standard comments, as in the UI test harness, disappear in macros!
69
70 fn main() {
71     test_shuffle_lanes!(2, u8x2, simd_shuffle2);
72     test_shuffle_lanes!(4, u8x4, simd_shuffle4);
73     test_shuffle_lanes!(8, u8x8, simd_shuffle8);
74     test_shuffle_lanes!(16, u8x16, simd_shuffle16);
75     test_shuffle_lanes!(32, u8x32, simd_shuffle32);
76     test_shuffle_lanes!(64, u8x64, simd_shuffle64);
77
78     extern "platform-intrinsic" {
79         fn simd_shuffle<T, I, U>(a: T, b: T, i: I) -> U;
80     }
81     let v = u8x2([0, 0]);
82     const I: [u32; 2] = [4, 4];
83     unsafe {
84         let _: u8x2 = simd_shuffle(v, v, I);
85         //~^ ERROR invalid monomorphization of `simd_shuffle` intrinsic
86     }
87 }