From ac96f5b39ca7d9fad8571595c476c2db0bce8438 Mon Sep 17 00:00:00 2001 From: Jubilee Young Date: Tue, 15 Sep 2020 16:10:26 -0700 Subject: [PATCH 1/3] Test and reject out-of-bounds shuffle vectors --- src/test/ui/simd/shuffle-not-out-of-bounds.rs | 23 +++++++++++++++++++ .../ui/simd/shuffle-not-out-of-bounds.stderr | 9 ++++++++ 2 files changed, 32 insertions(+) create mode 100644 src/test/ui/simd/shuffle-not-out-of-bounds.rs create mode 100644 src/test/ui/simd/shuffle-not-out-of-bounds.stderr diff --git a/src/test/ui/simd/shuffle-not-out-of-bounds.rs b/src/test/ui/simd/shuffle-not-out-of-bounds.rs new file mode 100644 index 0000000000000..9f41dbb7c78c4 --- /dev/null +++ b/src/test/ui/simd/shuffle-not-out-of-bounds.rs @@ -0,0 +1,23 @@ +// build-fail +#![allow(non_camel_case_types)] +#![feature(repr_simd, platform_intrinsics)] + +// Test for #73542 to verify out-of-bounds shuffle vectors do not compile. + +#[repr(simd)] +#[derive(Copy, Clone)] +struct f32x4(f32, f32, f32, f32); + +extern "platform-intrinsic" { + pub fn simd_shuffle4(x: T, y: T, idx: [u32; 4]) -> U; +} + + +fn main() { + unsafe { + let vec1 = f32x4(1.0, 2.0, 3.0, 4.0); + let vec2 = f32x4(10_000.0, 20_000.0, 30_000.0, 40_000.0); + let shuffled: f32x4 = simd_shuffle4(vec1, vec2, [0, 4, 7, 9]); + //~^ ERROR: invalid monomorphization of `simd_shuffle4` intrinsic: shuffle index #3 is out + } +} diff --git a/src/test/ui/simd/shuffle-not-out-of-bounds.stderr b/src/test/ui/simd/shuffle-not-out-of-bounds.stderr new file mode 100644 index 0000000000000..a834e75f27bed --- /dev/null +++ b/src/test/ui/simd/shuffle-not-out-of-bounds.stderr @@ -0,0 +1,9 @@ +error[E0511]: invalid monomorphization of `simd_shuffle4` intrinsic: shuffle index #3 is out of bounds (limit 8) + --> $DIR/shuffle-not-out-of-bounds.rs:20:31 + | +LL | let shuffled: f32x4 = simd_shuffle4(vec1, vec2, [0, 4, 7, 9]); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0511`. From c47caeaaa9011fa4dd56d7e7b0ef341a0419b378 Mon Sep 17 00:00:00 2001 From: Jubilee Young Date: Fri, 2 Oct 2020 19:23:33 -0700 Subject: [PATCH 2/3] Macro-expand test to cover all possible lanes --- src/test/ui/simd/shuffle-not-out-of-bounds.rs | 187 +++++++++++++++++- .../ui/simd/shuffle-not-out-of-bounds.stderr | 77 +++++++- 2 files changed, 249 insertions(+), 15 deletions(-) diff --git a/src/test/ui/simd/shuffle-not-out-of-bounds.rs b/src/test/ui/simd/shuffle-not-out-of-bounds.rs index 9f41dbb7c78c4..811e5f45d1266 100644 --- a/src/test/ui/simd/shuffle-not-out-of-bounds.rs +++ b/src/test/ui/simd/shuffle-not-out-of-bounds.rs @@ -6,18 +6,185 @@ #[repr(simd)] #[derive(Copy, Clone)] -struct f32x4(f32, f32, f32, f32); +struct u8x2(u8, u8); -extern "platform-intrinsic" { - pub fn simd_shuffle4(x: T, y: T, idx: [u32; 4]) -> U; -} +#[repr(simd)] +#[derive(Copy, Clone)] +struct u8x4(u8, u8, u8, u8); +#[repr(simd)] +#[derive(Copy, Clone)] +struct u8x8(u8, u8, u8, u8, u8, u8, u8, u8); -fn main() { - unsafe { - let vec1 = f32x4(1.0, 2.0, 3.0, 4.0); - let vec2 = f32x4(10_000.0, 20_000.0, 30_000.0, 40_000.0); - let shuffled: f32x4 = simd_shuffle4(vec1, vec2, [0, 4, 7, 9]); - //~^ ERROR: invalid monomorphization of `simd_shuffle4` intrinsic: shuffle index #3 is out +#[repr(simd)] +#[derive(Copy, Clone)] +struct u8x16( + u8, + u8, + u8, + u8, + u8, + u8, + u8, + u8, + u8, + u8, + u8, + u8, + u8, + u8, + u8, + u8, +); + +#[repr(simd)] +#[derive(Copy, Clone)] +struct u8x32( + u8, + u8, + u8, + u8, + u8, + u8, + u8, + u8, + u8, + u8, + u8, + u8, + u8, + u8, + u8, + u8, + u8, + u8, + u8, + u8, + u8, + u8, + u8, + u8, + u8, + u8, + u8, + u8, + u8, + u8, + u8, + u8, +); + +#[repr(simd)] +#[derive(Copy, Clone)] +struct u8x64( + u8, + u8, + u8, + u8, + u8, + u8, + u8, + u8, + u8, + u8, + u8, + u8, + u8, + u8, + u8, + u8, + u8, + u8, + u8, + u8, + u8, + u8, + u8, + u8, + u8, + u8, + u8, + u8, + u8, + u8, + u8, + u8, + u8, + u8, + u8, + u8, + u8, + u8, + u8, + u8, + u8, + u8, + u8, + u8, + u8, + u8, + u8, + u8, + u8, + u8, + u8, + u8, + u8, + u8, + u8, + u8, + u8, + u8, + u8, + u8, + u8, + u8, + u8, + u8, +); + +// Test vectors by lane size. Since LLVM does not distinguish between a shuffle +// over two f32s and a shuffle over two u64s, or any other such combination, +// it is not necessary to test every possible vector, only lane counts. +macro_rules! test_shuffle_lanes { + ($n:literal, $x:ident, $y:ident, $t:tt) => { + unsafe { + let shuffle: $x = { + const ARR: [u32; $n] = { + let mut arr = [0; $n]; + arr[0] = $n * 2; + arr + }; + extern "platform-intrinsic" { + pub fn $y(x: T, y: T, idx: [u32; $n]) -> U; + } + let vec1 = $x$t; + let vec2 = $x$t; + $y(vec1, vec2, ARR) + }; + } } } +//~^^^^^ ERROR: invalid monomorphization of `simd_shuffle2` intrinsic +//~^^^^^^ ERROR: invalid monomorphization of `simd_shuffle4` intrinsic +//~^^^^^^^ ERROR: invalid monomorphization of `simd_shuffle8` intrinsic +//~^^^^^^^^ ERROR: invalid monomorphization of `simd_shuffle16` intrinsic +//~^^^^^^^^^ ERROR: invalid monomorphization of `simd_shuffle32` intrinsic +//~^^^^^^^^^^ ERROR: invalid monomorphization of `simd_shuffle64` intrinsic +// Because the test is mostly embedded in a macro, all the errors have the same origin point. + +fn main() { + test_shuffle_lanes!(2, u8x2, simd_shuffle2, (2, 1)); + test_shuffle_lanes!(4, u8x4, simd_shuffle4, (4, 3, 2, 1)); + test_shuffle_lanes!(8, u8x8, simd_shuffle8, (8, 7, 6, 5, 4, 3, 2, 1)); + test_shuffle_lanes!(16, u8x16, simd_shuffle16, + (16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1)); + test_shuffle_lanes!(32, u8x32, simd_shuffle32, + (32, 31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, + 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1)); + test_shuffle_lanes!(64, u8x64, simd_shuffle64, + (64, 63, 62, 61, 60, 59, 58, 57, 56, 55, 54, 53, 52, 51, 50, 49, + 48, 47, 46, 45, 44, 43, 42, 41, 40, 39, 38, 37, 36, 35, 34, 33, + 32, 31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, + 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1)); +} diff --git a/src/test/ui/simd/shuffle-not-out-of-bounds.stderr b/src/test/ui/simd/shuffle-not-out-of-bounds.stderr index a834e75f27bed..4806f2ca27b9e 100644 --- a/src/test/ui/simd/shuffle-not-out-of-bounds.stderr +++ b/src/test/ui/simd/shuffle-not-out-of-bounds.stderr @@ -1,9 +1,76 @@ -error[E0511]: invalid monomorphization of `simd_shuffle4` intrinsic: shuffle index #3 is out of bounds (limit 8) - --> $DIR/shuffle-not-out-of-bounds.rs:20:31 +error[E0511]: invalid monomorphization of `simd_shuffle2` intrinsic: shuffle index #0 is out of bounds (limit 4) + --> $DIR/shuffle-not-out-of-bounds.rs:163:21 | -LL | let shuffled: f32x4 = simd_shuffle4(vec1, vec2, [0, 4, 7, 9]); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +LL | $y(vec1, vec2, ARR) + | ^^^^^^^^^^^^^^^^^^^ +... +LL | test_shuffle_lanes!(2, u8x2, simd_shuffle2, (2, 1)); + | ---------------------------------------------------- in this macro invocation + | + = note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info) + +error[E0511]: invalid monomorphization of `simd_shuffle4` intrinsic: shuffle index #0 is out of bounds (limit 8) + --> $DIR/shuffle-not-out-of-bounds.rs:163:21 + | +LL | $y(vec1, vec2, ARR) + | ^^^^^^^^^^^^^^^^^^^ +... +LL | test_shuffle_lanes!(4, u8x4, simd_shuffle4, (4, 3, 2, 1)); + | ---------------------------------------------------------- in this macro invocation + | + = note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info) + +error[E0511]: invalid monomorphization of `simd_shuffle8` intrinsic: shuffle index #0 is out of bounds (limit 16) + --> $DIR/shuffle-not-out-of-bounds.rs:163:21 + | +LL | $y(vec1, vec2, ARR) + | ^^^^^^^^^^^^^^^^^^^ +... +LL | test_shuffle_lanes!(8, u8x8, simd_shuffle8, (8, 7, 6, 5, 4, 3, 2, 1)); + | ---------------------------------------------------------------------- in this macro invocation + | + = note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info) + +error[E0511]: invalid monomorphization of `simd_shuffle16` intrinsic: shuffle index #0 is out of bounds (limit 32) + --> $DIR/shuffle-not-out-of-bounds.rs:163:21 + | +LL | $y(vec1, vec2, ARR) + | ^^^^^^^^^^^^^^^^^^^ +... +LL | / test_shuffle_lanes!(16, u8x16, simd_shuffle16, +LL | | (16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1)); + | |_________________________________________________________________- in this macro invocation + | + = note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info) + +error[E0511]: invalid monomorphization of `simd_shuffle32` intrinsic: shuffle index #0 is out of bounds (limit 64) + --> $DIR/shuffle-not-out-of-bounds.rs:163:21 + | +LL | $y(vec1, vec2, ARR) + | ^^^^^^^^^^^^^^^^^^^ +... +LL | / test_shuffle_lanes!(32, u8x32, simd_shuffle32, +LL | | (32, 31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, +LL | | 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1)); + | |_____________________________________________________________- in this macro invocation + | + = note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info) + +error[E0511]: invalid monomorphization of `simd_shuffle64` intrinsic: shuffle index #0 is out of bounds (limit 128) + --> $DIR/shuffle-not-out-of-bounds.rs:163:21 + | +LL | $y(vec1, vec2, ARR) + | ^^^^^^^^^^^^^^^^^^^ +... +LL | / test_shuffle_lanes!(64, u8x64, simd_shuffle64, +LL | | (64, 63, 62, 61, 60, 59, 58, 57, 56, 55, 54, 53, 52, 51, 50, 49, +LL | | 48, 47, 46, 45, 44, 43, 42, 41, 40, 39, 38, 37, 36, 35, 34, 33, +LL | | 32, 31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, +LL | | 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1)); + | |_________________________________________________________________- in this macro invocation + | + = note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info) -error: aborting due to previous error +error: aborting due to 6 previous errors For more information about this error, try `rustc --explain E0511`. From 2fcd1838ed428af379f9f91ece56f2a129466e24 Mon Sep 17 00:00:00 2001 From: Jubilee Young Date: Sat, 3 Oct 2020 12:14:22 -0700 Subject: [PATCH 3/3] Flatten arrows with further comment --- src/test/ui/simd/shuffle-not-out-of-bounds.rs | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/test/ui/simd/shuffle-not-out-of-bounds.rs b/src/test/ui/simd/shuffle-not-out-of-bounds.rs index 811e5f45d1266..8a533453e75fd 100644 --- a/src/test/ui/simd/shuffle-not-out-of-bounds.rs +++ b/src/test/ui/simd/shuffle-not-out-of-bounds.rs @@ -166,12 +166,13 @@ macro_rules! test_shuffle_lanes { } } //~^^^^^ ERROR: invalid monomorphization of `simd_shuffle2` intrinsic -//~^^^^^^ ERROR: invalid monomorphization of `simd_shuffle4` intrinsic -//~^^^^^^^ ERROR: invalid monomorphization of `simd_shuffle8` intrinsic -//~^^^^^^^^ ERROR: invalid monomorphization of `simd_shuffle16` intrinsic -//~^^^^^^^^^ ERROR: invalid monomorphization of `simd_shuffle32` intrinsic -//~^^^^^^^^^^ ERROR: invalid monomorphization of `simd_shuffle64` intrinsic +//~| ERROR: invalid monomorphization of `simd_shuffle4` intrinsic +//~| ERROR: invalid monomorphization of `simd_shuffle8` intrinsic +//~| ERROR: invalid monomorphization of `simd_shuffle16` intrinsic +//~| ERROR: invalid monomorphization of `simd_shuffle32` intrinsic +//~| ERROR: invalid monomorphization of `simd_shuffle64` intrinsic // Because the test is mostly embedded in a macro, all the errors have the same origin point. +// And unfortunately, standard comments, as in the UI test harness, disappear in macros! fn main() { test_shuffle_lanes!(2, u8x2, simd_shuffle2, (2, 1));