From 9e5a67e57f715ec5eda915db1261cdf4c4a04642 Mon Sep 17 00:00:00 2001 From: Oli Scherer Date: Mon, 10 Jul 2023 09:33:34 +0000 Subject: [PATCH 1/2] Permit pre-evaluated constants in simd_shuffle --- .../rustc_codegen_ssa/src/mir/constant.rs | 14 ++++++++++++++ tests/ui/simd/shuffle.rs | 19 +++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/compiler/rustc_codegen_ssa/src/mir/constant.rs b/compiler/rustc_codegen_ssa/src/mir/constant.rs index 1c5031dfc4b..47a166e4e1b 100644 --- a/compiler/rustc_codegen_ssa/src/mir/constant.rs +++ b/compiler/rustc_codegen_ssa/src/mir/constant.rs @@ -67,6 +67,20 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> { ) -> Result>, ErrorHandled> { let uv = match constant.literal { mir::ConstantKind::Unevaluated(uv, _) => uv.shrink(), + mir::ConstantKind::Ty(c) => match c.kind() { + // A constant that came from a const generic but was then used as an argument to old-style + // simd_shuffle (passing as argument instead of as a generic param). + rustc_type_ir::ConstKind::Value(valtree) => return Ok(Some(valtree)), + other => span_bug!(constant.span, "{other:#?}"), + }, + // We should never encounter `ConstantKind::Val` unless MIR opts (like const prop) evaluate + // a constant and write that value back into `Operand`s. This could happen, but is unlikely. + // Also: all users of `simd_shuffle` are on unstable and already need to take a lot of care + // around intrinsics. For an issue to happen here, it would require a macro expanding to a + // `simd_shuffle` call without wrapping the constant argument in a `const {}` block, but + // the user pass through arbitrary expressions. + // FIXME(oli-obk): replace the magic const generic argument of `simd_shuffle` with a real + // const generic. other => span_bug!(constant.span, "{other:#?}"), }; let uv = self.monomorphize(uv); diff --git a/tests/ui/simd/shuffle.rs b/tests/ui/simd/shuffle.rs index 3592adfdc6a..de41c9e25dd 100644 --- a/tests/ui/simd/shuffle.rs +++ b/tests/ui/simd/shuffle.rs @@ -1,14 +1,21 @@ //run-pass #![feature(repr_simd, platform_intrinsics)] +#![allow(incomplete_features)] +#![feature(adt_const_params)] extern "platform-intrinsic" { fn simd_shuffle(a: T, b: T, i: I) -> U; + fn simd_shuffle16(x: T, y: T, idx: [u32; 16]) -> U; } #[derive(Copy, Clone)] #[repr(simd)] struct Simd([T; N]); +pub unsafe fn __shuffle_vector16(x: T, y: T) -> U { + simd_shuffle16(x, y, IDX) +} + fn main() { const I1: [u32; 4] = [0, 2, 4, 6]; const I2: [u32; 2] = [1, 5]; @@ -21,4 +28,16 @@ fn main() { let y: Simd = simd_shuffle(a, b, I2); assert_eq!(y.0, [1, 5]); } + // Test that an indirection (via an unnamed constant) + // through a const generic parameter also works. + // See https://github.com/rust-lang/rust/issues/113500 for details. + let a = Simd::([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]); + let b = Simd::([16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31]); + unsafe { + __shuffle_vector16::< + { [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16] }, + Simd, + Simd, + >(a, b); + } } From c7428d50520446ccd44ca89bbbf4ff7a4725570e Mon Sep 17 00:00:00 2001 From: Oli Scherer Date: Thu, 20 Jul 2023 08:53:09 +0000 Subject: [PATCH 2/2] Monomorphize constants before inspecting them --- compiler/rustc_codegen_ssa/src/mir/constant.rs | 2 +- tests/ui/simd/shuffle.rs | 5 ++++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/compiler/rustc_codegen_ssa/src/mir/constant.rs b/compiler/rustc_codegen_ssa/src/mir/constant.rs index 47a166e4e1b..babcf9bee24 100644 --- a/compiler/rustc_codegen_ssa/src/mir/constant.rs +++ b/compiler/rustc_codegen_ssa/src/mir/constant.rs @@ -65,7 +65,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> { &self, constant: &mir::Constant<'tcx>, ) -> Result>, ErrorHandled> { - let uv = match constant.literal { + let uv = match self.monomorphize(constant.literal) { mir::ConstantKind::Unevaluated(uv, _) => uv.shrink(), mir::ConstantKind::Ty(c) => match c.kind() { // A constant that came from a const generic but was then used as an argument to old-style diff --git a/tests/ui/simd/shuffle.rs b/tests/ui/simd/shuffle.rs index de41c9e25dd..461243d4892 100644 --- a/tests/ui/simd/shuffle.rs +++ b/tests/ui/simd/shuffle.rs @@ -1,4 +1,7 @@ -//run-pass +// run-pass +// revisions: opt noopt +//[noopt] compile-flags: -Copt-level=0 +//[opt] compile-flags: -O #![feature(repr_simd, platform_intrinsics)] #![allow(incomplete_features)] #![feature(adt_const_params)]