Skip to content

Commit 47d48fb

Browse files
committed
add simd_insert_dyn and simd_extract_dyn
1 parent f04bbc6 commit 47d48fb

File tree

6 files changed

+189
-6
lines changed

6 files changed

+189
-6
lines changed

Diff for: compiler/rustc_codegen_llvm/src/intrinsic.rs

+24
Original file line numberDiff line numberDiff line change
@@ -1451,6 +1451,23 @@ fn generic_simd_intrinsic<'ll, 'tcx>(
14511451
bx.const_i32(idx as i32),
14521452
));
14531453
}
1454+
if name == sym::simd_insert_dyn {
1455+
require!(
1456+
in_elem == arg_tys[2],
1457+
InvalidMonomorphization::InsertedType {
1458+
span,
1459+
name,
1460+
in_elem,
1461+
in_ty,
1462+
out_ty: arg_tys[2]
1463+
}
1464+
);
1465+
return Ok(bx.insert_element(
1466+
args[0].immediate(),
1467+
args[2].immediate(),
1468+
args[1].immediate(),
1469+
));
1470+
}
14541471
if name == sym::simd_extract {
14551472
require!(
14561473
ret_ty == in_elem,
@@ -1469,6 +1486,13 @@ fn generic_simd_intrinsic<'ll, 'tcx>(
14691486
}
14701487
return Ok(bx.extract_element(args[0].immediate(), bx.const_i32(idx as i32)));
14711488
}
1489+
if name == sym::simd_extract_dyn {
1490+
require!(
1491+
ret_ty == in_elem,
1492+
InvalidMonomorphization::ReturnType { span, name, in_elem, in_ty, ret_ty }
1493+
);
1494+
return Ok(bx.extract_element(args[0].immediate(), args[1].immediate()));
1495+
}
14721496

14731497
if name == sym::simd_select {
14741498
let m_elem_ty = in_elem;

Diff for: compiler/rustc_hir_analysis/src/check/intrinsic.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -682,8 +682,12 @@ pub fn check_intrinsic_type(
682682
sym::simd_masked_load => (3, 0, vec![param(0), param(1), param(2)], param(2)),
683683
sym::simd_masked_store => (3, 0, vec![param(0), param(1), param(2)], tcx.types.unit),
684684
sym::simd_scatter => (3, 0, vec![param(0), param(1), param(2)], tcx.types.unit),
685-
sym::simd_insert => (2, 0, vec![param(0), tcx.types.u32, param(1)], param(0)),
686-
sym::simd_extract => (2, 0, vec![param(0), tcx.types.u32], param(1)),
685+
sym::simd_insert | sym::simd_insert_dyn => {
686+
(2, 0, vec![param(0), tcx.types.u32, param(1)], param(0))
687+
}
688+
sym::simd_extract | sym::simd_extract_dyn => {
689+
(2, 0, vec![param(0), tcx.types.u32], param(1))
690+
}
687691
sym::simd_cast
688692
| sym::simd_as
689693
| sym::simd_cast_ptr

Diff for: compiler/rustc_span/src/symbol.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1874,6 +1874,7 @@ symbols! {
18741874
simd_eq,
18751875
simd_expose_provenance,
18761876
simd_extract,
1877+
simd_extract_dyn,
18771878
simd_fabs,
18781879
simd_fcos,
18791880
simd_fexp,
@@ -1894,6 +1895,7 @@ symbols! {
18941895
simd_ge,
18951896
simd_gt,
18961897
simd_insert,
1898+
simd_insert_dyn,
18971899
simd_le,
18981900
simd_lt,
18991901
simd_masked_load,

Diff for: library/core/src/intrinsics/simd.rs

+36-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
55
/// Inserts an element into a vector, returning the updated vector.
66
///
7-
/// `T` must be a vector with element type `U`.
7+
/// `T` must be a vector with element type `U`, and `idx` must be `const`.
88
///
99
/// # Safety
1010
///
@@ -16,20 +16,54 @@ pub unsafe fn simd_insert<T, U>(_x: T, _idx: u32, _val: U) -> T {
1616
unreachable!()
1717
}
1818

19-
/// Extracts an element from a vector.
19+
/// Inserts an element into a vector, returning the updated vector.
2020
///
2121
/// `T` must be a vector with element type `U`.
2222
///
23+
/// If the index is `const`, [`simd_insert`] may emit better assembly.
24+
///
2325
/// # Safety
2426
///
2527
/// `idx` must be in-bounds of the vector.
2628
#[rustc_intrinsic]
2729
#[rustc_intrinsic_must_be_overridden]
2830
#[rustc_nounwind]
31+
#[cfg(not(bootstrap))]
32+
pub unsafe fn simd_insert_dyn<T, U>(_x: T, _idx: u32, _val: U) -> T {
33+
unreachable!()
34+
}
35+
36+
/// Extracts an element from a vector.
37+
///
38+
/// `T` must be a vector with element type `U`, and `idx` must be `const`.
39+
///
40+
/// # Safety
41+
///
42+
/// `idx` must be const and in-bounds of the vector.
43+
#[rustc_intrinsic]
44+
#[rustc_intrinsic_must_be_overridden]
45+
#[rustc_nounwind]
2946
pub unsafe fn simd_extract<T, U>(_x: T, _idx: u32) -> U {
3047
unreachable!()
3148
}
3249

50+
/// Extracts an element from a vector.
51+
///
52+
/// `T` must be a vector with element type `U`.
53+
///
54+
/// If the index is `const`, [`simd_extract`] may emit better assembly.
55+
///
56+
/// # Safety
57+
///
58+
/// `idx` must be in-bounds of the vector.
59+
#[rustc_intrinsic]
60+
#[rustc_intrinsic_must_be_overridden]
61+
#[rustc_nounwind]
62+
#[cfg(not(bootstrap))]
63+
pub unsafe fn simd_extract_dyn<T, U>(_x: T, _idx: u32) -> U {
64+
unreachable!()
65+
}
66+
3367
/// Adds two simd vectors elementwise.
3468
///
3569
/// `T` must be a vector of integer or floating point primitive types.

Diff for: tests/codegen/simd/extract-insert-dyn.rs

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
//@compile-flags: -Copt-level=3 -Z merge-functions=disabled
2+
3+
#![feature(core_intrinsics, s390x_target_feature, repr_simd)]
4+
#![no_std]
5+
#![crate_type = "lib"]
6+
#![allow(non_camel_case_types)]
7+
8+
// test that `core::intrinsics::simd::{simd_extract_dyn, simd_insert_dyn}`
9+
// optimize to their dedicated instructions on platforms that support them.
10+
11+
use core::intrinsics::simd::{simd_extract, simd_extract_dyn, simd_insert, simd_insert_dyn};
12+
13+
#[repr(simd)]
14+
#[derive(Clone, Copy)]
15+
pub struct u32x16([u32; 16]);
16+
17+
#[repr(simd)]
18+
#[derive(Clone, Copy)]
19+
pub struct i8x16([i8; 16]);
20+
21+
// CHECK-LABEL: dyn_simd_extract
22+
// CHECK: extractelement <16 x i8> %x, i32 %idx
23+
// CHECK-NEXT: ret
24+
#[no_mangle]
25+
unsafe extern "C" fn dyn_simd_extract(x: i8x16, idx: u32) -> i8 {
26+
simd_extract_dyn(x, idx)
27+
}
28+
29+
// CHECK-LABEL: literal_dyn_simd_extract
30+
// CHECK: extractelement <16 x i8> %x, i64 7
31+
// CHECK-NEXT: ret
32+
#[no_mangle]
33+
unsafe extern "C" fn literal_dyn_simd_extract(x: i8x16) -> i8 {
34+
simd_extract_dyn(x, 7)
35+
}
36+
37+
// CHECK-LABEL: const_dyn_simd_extract
38+
// CHECK: extractelement <16 x i8> %x, i64 7
39+
// CHECK-NEXT: ret
40+
#[no_mangle]
41+
unsafe extern "C" fn const_dyn_simd_extract(x: i8x16) -> i8 {
42+
simd_extract_dyn(x, const { 3 + 4 })
43+
}
44+
45+
// CHECK-LABEL: const_simd_extract
46+
// CHECK: extractelement <16 x i8> %x, i64 7
47+
// CHECK-NEXT: ret
48+
#[no_mangle]
49+
unsafe extern "C" fn const_simd_extract(x: i8x16) -> i8 {
50+
simd_extract(x, const { 3 + 4 })
51+
}
52+
53+
// CHECK-LABEL: dyn_simd_insert
54+
// CHECK: insertelement <16 x i8> %x, i8 %e, i32 %idx
55+
// CHECK-NEXT: ret
56+
#[no_mangle]
57+
unsafe extern "C" fn dyn_simd_insert(x: i8x16, e: i8, idx: u32) -> i8x16 {
58+
simd_insert_dyn(x, idx, e)
59+
}
60+
61+
// CHECK-LABEL: literal_dyn_simd_insert
62+
// CHECK: insertelement <16 x i8> %x, i8 %e, i64 7
63+
// CHECK-NEXT: ret
64+
#[no_mangle]
65+
unsafe extern "C" fn literal_dyn_simd_insert(x: i8x16, e: i8) -> i8x16 {
66+
simd_insert_dyn(x, 7, e)
67+
}
68+
69+
// CHECK-LABEL: const_dyn_simd_insert
70+
// CHECK: insertelement <16 x i8> %x, i8 %e, i64 7
71+
// CHECK-NEXT: ret
72+
#[no_mangle]
73+
unsafe extern "C" fn const_dyn_simd_insert(x: i8x16, e: i8) -> i8x16 {
74+
simd_insert_dyn(x, const { 3 + 4 }, e)
75+
}
76+
77+
// CHECK-LABEL: const_simd_insert
78+
// CHECK: insertelement <16 x i8> %x, i8 %e, i64 7
79+
// CHECK-NEXT: ret
80+
#[no_mangle]
81+
unsafe extern "C" fn const_simd_insert(x: i8x16, e: i8) -> i8x16 {
82+
simd_insert(x, const { 3 + 4 }, e)
83+
}

Diff for: tests/ui/simd/intrinsic/generic-elements-pass.rs

+38-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
//@ run-pass
22
//@ ignore-emscripten FIXME(#45351) hits an LLVM assert
33

4-
#![feature(repr_simd, intrinsics)]
4+
#![feature(repr_simd, intrinsics, core_intrinsics)]
5+
6+
use std::intrinsics::simd::{simd_extract_dyn, simd_insert_dyn};
57

68
#[repr(simd)]
79
#[derive(Copy, Clone, Debug, PartialEq)]
@@ -22,7 +24,6 @@ unsafe fn simd_insert<T, E>(x: T, idx: u32, y: E) -> T;
2224
#[rustc_intrinsic]
2325
unsafe fn simd_extract<T, E>(x: T, idx: u32) -> E;
2426

25-
2627
#[rustc_intrinsic]
2728
unsafe fn simd_shuffle<T, I, U>(x: T, y: T, idx: I) -> U;
2829

@@ -79,6 +80,41 @@ fn main() {
7980
all_eq!(simd_extract(x8, 6), 86);
8081
all_eq!(simd_extract(x8, 7), 87);
8182
}
83+
unsafe {
84+
all_eq!(simd_insert_dyn(x2, 0, 100), i32x2([100, 21]));
85+
all_eq!(simd_insert_dyn(x2, 1, 100), i32x2([20, 100]));
86+
87+
all_eq!(simd_insert_dyn(x4, 0, 100), i32x4([100, 41, 42, 43]));
88+
all_eq!(simd_insert_dyn(x4, 1, 100), i32x4([40, 100, 42, 43]));
89+
all_eq!(simd_insert_dyn(x4, 2, 100), i32x4([40, 41, 100, 43]));
90+
all_eq!(simd_insert_dyn(x4, 3, 100), i32x4([40, 41, 42, 100]));
91+
92+
all_eq!(simd_insert_dyn(x8, 0, 100), i32x8([100, 81, 82, 83, 84, 85, 86, 87]));
93+
all_eq!(simd_insert_dyn(x8, 1, 100), i32x8([80, 100, 82, 83, 84, 85, 86, 87]));
94+
all_eq!(simd_insert_dyn(x8, 2, 100), i32x8([80, 81, 100, 83, 84, 85, 86, 87]));
95+
all_eq!(simd_insert_dyn(x8, 3, 100), i32x8([80, 81, 82, 100, 84, 85, 86, 87]));
96+
all_eq!(simd_insert_dyn(x8, 4, 100), i32x8([80, 81, 82, 83, 100, 85, 86, 87]));
97+
all_eq!(simd_insert_dyn(x8, 5, 100), i32x8([80, 81, 82, 83, 84, 100, 86, 87]));
98+
all_eq!(simd_insert_dyn(x8, 6, 100), i32x8([80, 81, 82, 83, 84, 85, 100, 87]));
99+
all_eq!(simd_insert_dyn(x8, 7, 100), i32x8([80, 81, 82, 83, 84, 85, 86, 100]));
100+
101+
all_eq!(simd_extract_dyn(x2, 0), 20);
102+
all_eq!(simd_extract_dyn(x2, 1), 21);
103+
104+
all_eq!(simd_extract_dyn(x4, 0), 40);
105+
all_eq!(simd_extract_dyn(x4, 1), 41);
106+
all_eq!(simd_extract_dyn(x4, 2), 42);
107+
all_eq!(simd_extract_dyn(x4, 3), 43);
108+
109+
all_eq!(simd_extract_dyn(x8, 0), 80);
110+
all_eq!(simd_extract_dyn(x8, 1), 81);
111+
all_eq!(simd_extract_dyn(x8, 2), 82);
112+
all_eq!(simd_extract_dyn(x8, 3), 83);
113+
all_eq!(simd_extract_dyn(x8, 4), 84);
114+
all_eq!(simd_extract_dyn(x8, 5), 85);
115+
all_eq!(simd_extract_dyn(x8, 6), 86);
116+
all_eq!(simd_extract_dyn(x8, 7), 87);
117+
}
82118

83119
let y2 = i32x2([120, 121]);
84120
let y4 = i32x4([140, 141, 142, 143]);

0 commit comments

Comments
 (0)