Skip to content

Commit 5465748

Browse files
committed
Permit pre-evaluated constants in simd_shuffle
1 parent 743333f commit 5465748

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

compiler/rustc_codegen_ssa/src/mir/constant.rs

+14
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,20 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
6767
) -> Result<Option<ty::ValTree<'tcx>>, ErrorHandled> {
6868
let uv = match constant.literal {
6969
mir::ConstantKind::Unevaluated(uv, _) => uv.shrink(),
70+
mir::ConstantKind::Ty(c) => match c.kind() {
71+
// A constant that came from a const generic but was then used as an argument to old-style
72+
// simd_shuffle (passing as argument instead of as a generic param).
73+
rustc_type_ir::ConstKind::Value(valtree) => return Ok(Some(valtree)),
74+
other => span_bug!(constant.span, "{other:#?}"),
75+
},
76+
// We should never encounter `ConstantKind::Val` unless MIR opts (like const prop) evaluate
77+
// a constant and write that value back into `Operand`s. This could happen, but is unlikely.
78+
// Also: all users of `simd_shuffle` are on unstable and already need to take a lot of care
79+
// around intrinsics. For an issue to happen here, it would require a macro expanding to a
80+
// `simd_shuffle` call without wrapping the constant argument in a `const {}` block, but
81+
// the user pass through arbitrary expressions.
82+
// FIXME(oli-obk): replace the magic const generic argument of `simd_shuffle` with a real
83+
// const generic.
7084
other => span_bug!(constant.span, "{other:#?}"),
7185
};
7286
let uv = self.monomorphize(uv);

tests/ui/simd/shuffle.rs

+19
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,21 @@
11
//run-pass
22
#![feature(repr_simd, platform_intrinsics)]
3+
#![allow(incomplete_features)]
4+
#![feature(adt_const_params)]
35

46
extern "platform-intrinsic" {
57
fn simd_shuffle<T, I, U>(a: T, b: T, i: I) -> U;
8+
fn simd_shuffle16<T, U>(x: T, y: T, idx: [u32; 16]) -> U;
69
}
710

811
#[derive(Copy, Clone)]
912
#[repr(simd)]
1013
struct Simd<T, const N: usize>([T; N]);
1114

15+
pub unsafe fn __shuffle_vector16<const IDX: [u32; 16], T, U>(x: T, y: T) -> U {
16+
simd_shuffle16(x, y, IDX)
17+
}
18+
1219
fn main() {
1320
const I1: [u32; 4] = [0, 2, 4, 6];
1421
const I2: [u32; 2] = [1, 5];
@@ -21,4 +28,16 @@ fn main() {
2128
let y: Simd<u8, 2> = simd_shuffle(a, b, I2);
2229
assert_eq!(y.0, [1, 5]);
2330
}
31+
// Test that an indirection (via an unnamed constant)
32+
// through a const generic parameter also works.
33+
// See https://github.com/rust-lang/rust/issues/113500 for details.
34+
let a = Simd::<u8, 16>([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]);
35+
let b = Simd::<u8, 16>([16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31]);
36+
unsafe {
37+
__shuffle_vector16::<
38+
{ [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16] },
39+
Simd<u8, 16>,
40+
Simd<u8, 16>,
41+
>(a, b);
42+
}
2443
}

0 commit comments

Comments
 (0)