Skip to content
/ rust Public
forked from rust-lang/rust

Commit 03a5725

Browse files
committed
Auto merge of rust-lang#114156 - calebzulawski:simd-bswap, r=compiler-errors
Add simd_bswap, simd_bitreverse, simd_ctlz, and simd_cttz intrinsics cc `@workingjubilee`
2 parents a04e649 + ce4a48f commit 03a5725

File tree

6 files changed

+173
-25
lines changed

6 files changed

+173
-25
lines changed

compiler/rustc_codegen_llvm/src/intrinsic.rs

+47
Original file line numberDiff line numberDiff line change
@@ -2074,6 +2074,53 @@ fn generic_simd_intrinsic<'ll, 'tcx>(
20742074
simd_neg: Int => neg, Float => fneg;
20752075
}
20762076

2077+
// Unary integer intrinsics
2078+
if matches!(name, sym::simd_bswap | sym::simd_bitreverse | sym::simd_ctlz | sym::simd_cttz) {
2079+
let vec_ty = bx.cx.type_vector(
2080+
match *in_elem.kind() {
2081+
ty::Int(i) => bx.cx.type_int_from_ty(i),
2082+
ty::Uint(i) => bx.cx.type_uint_from_ty(i),
2083+
_ => return_error!(InvalidMonomorphization::UnsupportedOperation {
2084+
span,
2085+
name,
2086+
in_ty,
2087+
in_elem
2088+
}),
2089+
},
2090+
in_len as u64,
2091+
);
2092+
let intrinsic_name = match name {
2093+
sym::simd_bswap => "bswap",
2094+
sym::simd_bitreverse => "bitreverse",
2095+
sym::simd_ctlz => "ctlz",
2096+
sym::simd_cttz => "cttz",
2097+
_ => unreachable!(),
2098+
};
2099+
let llvm_intrinsic = &format!(
2100+
"llvm.{}.v{}i{}",
2101+
intrinsic_name,
2102+
in_len,
2103+
in_elem.int_size_and_signed(bx.tcx()).0.bits(),
2104+
);
2105+
2106+
return Ok(if matches!(name, sym::simd_ctlz | sym::simd_cttz) {
2107+
let fn_ty = bx.type_func(&[vec_ty, bx.type_i1()], vec_ty);
2108+
let f = bx.declare_cfn(llvm_intrinsic, llvm::UnnamedAddr::No, fn_ty);
2109+
bx.call(
2110+
fn_ty,
2111+
None,
2112+
None,
2113+
f,
2114+
&[args[0].immediate(), bx.const_int(bx.type_i1(), 0)],
2115+
None,
2116+
)
2117+
} else {
2118+
let fn_ty = bx.type_func(&[vec_ty], vec_ty);
2119+
let f = bx.declare_cfn(llvm_intrinsic, llvm::UnnamedAddr::No, fn_ty);
2120+
bx.call(fn_ty, None, None, f, &[args[0].immediate()], None)
2121+
});
2122+
}
2123+
20772124
if name == sym::simd_arith_offset {
20782125
// This also checks that the first operand is a ptr type.
20792126
let pointee = in_elem.builtin_deref(true).unwrap_or_else(|| {

compiler/rustc_hir_analysis/src/check/intrinsic.rs

+4
Original file line numberDiff line numberDiff line change
@@ -521,6 +521,10 @@ pub fn check_platform_intrinsic_type(tcx: TyCtxt<'_>, it: &hir::ForeignItem<'_>)
521521
| sym::simd_saturating_sub => (1, vec![param(0), param(0)], param(0)),
522522
sym::simd_arith_offset => (2, vec![param(0), param(1)], param(0)),
523523
sym::simd_neg
524+
| sym::simd_bswap
525+
| sym::simd_bitreverse
526+
| sym::simd_ctlz
527+
| sym::simd_cttz
524528
| sym::simd_fsqrt
525529
| sym::simd_fsin
526530
| sym::simd_fcos

compiler/rustc_span/src/symbol.rs

+4
Original file line numberDiff line numberDiff line change
@@ -1371,9 +1371,13 @@ symbols! {
13711371
simd_arith_offset,
13721372
simd_as,
13731373
simd_bitmask,
1374+
simd_bitreverse,
1375+
simd_bswap,
13741376
simd_cast,
13751377
simd_cast_ptr,
13761378
simd_ceil,
1379+
simd_ctlz,
1380+
simd_cttz,
13771381
simd_div,
13781382
simd_eq,
13791383
simd_expose_addr,

tests/ui/simd/intrinsic/generic-arithmetic-2.rs

+28
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ extern "platform-intrinsic" {
2727
fn simd_xor<T>(x: T, y: T) -> T;
2828

2929
fn simd_neg<T>(x: T) -> T;
30+
fn simd_bswap<T>(x: T) -> T;
31+
fn simd_bitreverse<T>(x: T) -> T;
32+
fn simd_ctlz<T>(x: T) -> T;
33+
fn simd_cttz<T>(x: T) -> T;
3034
}
3135

3236
fn main() {
@@ -64,6 +68,14 @@ fn main() {
6468

6569
simd_neg(x);
6670
simd_neg(z);
71+
simd_bswap(x);
72+
simd_bswap(y);
73+
simd_bitreverse(x);
74+
simd_bitreverse(y);
75+
simd_ctlz(x);
76+
simd_ctlz(y);
77+
simd_cttz(x);
78+
simd_cttz(y);
6779

6880

6981
simd_add(0, 0);
@@ -87,6 +99,14 @@ fn main() {
8799

88100
simd_neg(0);
89101
//~^ ERROR expected SIMD input type, found non-SIMD `i32`
102+
simd_bswap(0);
103+
//~^ ERROR expected SIMD input type, found non-SIMD `i32`
104+
simd_bitreverse(0);
105+
//~^ ERROR expected SIMD input type, found non-SIMD `i32`
106+
simd_ctlz(0);
107+
//~^ ERROR expected SIMD input type, found non-SIMD `i32`
108+
simd_cttz(0);
109+
//~^ ERROR expected SIMD input type, found non-SIMD `i32`
90110

91111

92112
simd_shl(z, z);
@@ -98,6 +118,14 @@ fn main() {
98118
simd_or(z, z);
99119
//~^ ERROR unsupported operation on `f32x4` with element `f32`
100120
simd_xor(z, z);
121+
//~^ ERROR unsupported operation on `f32x4` with element `f32`
122+
simd_bswap(z);
123+
//~^ ERROR unsupported operation on `f32x4` with element `f32`
124+
simd_bitreverse(z);
125+
//~^ ERROR unsupported operation on `f32x4` with element `f32`
126+
simd_ctlz(z);
127+
//~^ ERROR unsupported operation on `f32x4` with element `f32`
128+
simd_cttz(z);
101129
//~^ ERROR unsupported operation on `f32x4` with element `f32`
102130
}
103131
}
Original file line numberDiff line numberDiff line change
@@ -1,93 +1,141 @@
11
error[E0511]: invalid monomorphization of `simd_add` intrinsic: expected SIMD input type, found non-SIMD `i32`
2-
--> $DIR/generic-arithmetic-2.rs:69:9
2+
--> $DIR/generic-arithmetic-2.rs:81:9
33
|
44
LL | simd_add(0, 0);
55
| ^^^^^^^^^^^^^^
66

77
error[E0511]: invalid monomorphization of `simd_sub` intrinsic: expected SIMD input type, found non-SIMD `i32`
8-
--> $DIR/generic-arithmetic-2.rs:71:9
8+
--> $DIR/generic-arithmetic-2.rs:83:9
99
|
1010
LL | simd_sub(0, 0);
1111
| ^^^^^^^^^^^^^^
1212

1313
error[E0511]: invalid monomorphization of `simd_mul` intrinsic: expected SIMD input type, found non-SIMD `i32`
14-
--> $DIR/generic-arithmetic-2.rs:73:9
14+
--> $DIR/generic-arithmetic-2.rs:85:9
1515
|
1616
LL | simd_mul(0, 0);
1717
| ^^^^^^^^^^^^^^
1818

1919
error[E0511]: invalid monomorphization of `simd_div` intrinsic: expected SIMD input type, found non-SIMD `i32`
20-
--> $DIR/generic-arithmetic-2.rs:75:9
20+
--> $DIR/generic-arithmetic-2.rs:87:9
2121
|
2222
LL | simd_div(0, 0);
2323
| ^^^^^^^^^^^^^^
2424

2525
error[E0511]: invalid monomorphization of `simd_shl` intrinsic: expected SIMD input type, found non-SIMD `i32`
26-
--> $DIR/generic-arithmetic-2.rs:77:9
26+
--> $DIR/generic-arithmetic-2.rs:89:9
2727
|
2828
LL | simd_shl(0, 0);
2929
| ^^^^^^^^^^^^^^
3030

3131
error[E0511]: invalid monomorphization of `simd_shr` intrinsic: expected SIMD input type, found non-SIMD `i32`
32-
--> $DIR/generic-arithmetic-2.rs:79:9
32+
--> $DIR/generic-arithmetic-2.rs:91:9
3333
|
3434
LL | simd_shr(0, 0);
3535
| ^^^^^^^^^^^^^^
3636

3737
error[E0511]: invalid monomorphization of `simd_and` intrinsic: expected SIMD input type, found non-SIMD `i32`
38-
--> $DIR/generic-arithmetic-2.rs:81:9
38+
--> $DIR/generic-arithmetic-2.rs:93:9
3939
|
4040
LL | simd_and(0, 0);
4141
| ^^^^^^^^^^^^^^
4242

4343
error[E0511]: invalid monomorphization of `simd_or` intrinsic: expected SIMD input type, found non-SIMD `i32`
44-
--> $DIR/generic-arithmetic-2.rs:83:9
44+
--> $DIR/generic-arithmetic-2.rs:95:9
4545
|
4646
LL | simd_or(0, 0);
4747
| ^^^^^^^^^^^^^
4848

4949
error[E0511]: invalid monomorphization of `simd_xor` intrinsic: expected SIMD input type, found non-SIMD `i32`
50-
--> $DIR/generic-arithmetic-2.rs:85:9
50+
--> $DIR/generic-arithmetic-2.rs:97:9
5151
|
5252
LL | simd_xor(0, 0);
5353
| ^^^^^^^^^^^^^^
5454

5555
error[E0511]: invalid monomorphization of `simd_neg` intrinsic: expected SIMD input type, found non-SIMD `i32`
56-
--> $DIR/generic-arithmetic-2.rs:88:9
56+
--> $DIR/generic-arithmetic-2.rs:100:9
5757
|
5858
LL | simd_neg(0);
5959
| ^^^^^^^^^^^
6060

61+
error[E0511]: invalid monomorphization of `simd_bswap` intrinsic: expected SIMD input type, found non-SIMD `i32`
62+
--> $DIR/generic-arithmetic-2.rs:102:9
63+
|
64+
LL | simd_bswap(0);
65+
| ^^^^^^^^^^^^^
66+
67+
error[E0511]: invalid monomorphization of `simd_bitreverse` intrinsic: expected SIMD input type, found non-SIMD `i32`
68+
--> $DIR/generic-arithmetic-2.rs:104:9
69+
|
70+
LL | simd_bitreverse(0);
71+
| ^^^^^^^^^^^^^^^^^^
72+
73+
error[E0511]: invalid monomorphization of `simd_ctlz` intrinsic: expected SIMD input type, found non-SIMD `i32`
74+
--> $DIR/generic-arithmetic-2.rs:106:9
75+
|
76+
LL | simd_ctlz(0);
77+
| ^^^^^^^^^^^^
78+
79+
error[E0511]: invalid monomorphization of `simd_cttz` intrinsic: expected SIMD input type, found non-SIMD `i32`
80+
--> $DIR/generic-arithmetic-2.rs:108:9
81+
|
82+
LL | simd_cttz(0);
83+
| ^^^^^^^^^^^^
84+
6185
error[E0511]: invalid monomorphization of `simd_shl` intrinsic: unsupported operation on `f32x4` with element `f32`
62-
--> $DIR/generic-arithmetic-2.rs:92:9
86+
--> $DIR/generic-arithmetic-2.rs:112:9
6387
|
6488
LL | simd_shl(z, z);
6589
| ^^^^^^^^^^^^^^
6690

6791
error[E0511]: invalid monomorphization of `simd_shr` intrinsic: unsupported operation on `f32x4` with element `f32`
68-
--> $DIR/generic-arithmetic-2.rs:94:9
92+
--> $DIR/generic-arithmetic-2.rs:114:9
6993
|
7094
LL | simd_shr(z, z);
7195
| ^^^^^^^^^^^^^^
7296

7397
error[E0511]: invalid monomorphization of `simd_and` intrinsic: unsupported operation on `f32x4` with element `f32`
74-
--> $DIR/generic-arithmetic-2.rs:96:9
98+
--> $DIR/generic-arithmetic-2.rs:116:9
7599
|
76100
LL | simd_and(z, z);
77101
| ^^^^^^^^^^^^^^
78102

79103
error[E0511]: invalid monomorphization of `simd_or` intrinsic: unsupported operation on `f32x4` with element `f32`
80-
--> $DIR/generic-arithmetic-2.rs:98:9
104+
--> $DIR/generic-arithmetic-2.rs:118:9
81105
|
82106
LL | simd_or(z, z);
83107
| ^^^^^^^^^^^^^
84108

85109
error[E0511]: invalid monomorphization of `simd_xor` intrinsic: unsupported operation on `f32x4` with element `f32`
86-
--> $DIR/generic-arithmetic-2.rs:100:9
110+
--> $DIR/generic-arithmetic-2.rs:120:9
87111
|
88112
LL | simd_xor(z, z);
89113
| ^^^^^^^^^^^^^^
90114

91-
error: aborting due to 15 previous errors
115+
error[E0511]: invalid monomorphization of `simd_bswap` intrinsic: unsupported operation on `f32x4` with element `f32`
116+
--> $DIR/generic-arithmetic-2.rs:122:9
117+
|
118+
LL | simd_bswap(z);
119+
| ^^^^^^^^^^^^^
120+
121+
error[E0511]: invalid monomorphization of `simd_bitreverse` intrinsic: unsupported operation on `f32x4` with element `f32`
122+
--> $DIR/generic-arithmetic-2.rs:124:9
123+
|
124+
LL | simd_bitreverse(z);
125+
| ^^^^^^^^^^^^^^^^^^
126+
127+
error[E0511]: invalid monomorphization of `simd_ctlz` intrinsic: unsupported operation on `f32x4` with element `f32`
128+
--> $DIR/generic-arithmetic-2.rs:126:9
129+
|
130+
LL | simd_ctlz(z);
131+
| ^^^^^^^^^^^^
132+
133+
error[E0511]: invalid monomorphization of `simd_cttz` intrinsic: unsupported operation on `f32x4` with element `f32`
134+
--> $DIR/generic-arithmetic-2.rs:128:9
135+
|
136+
LL | simd_cttz(z);
137+
| ^^^^^^^^^^^^
138+
139+
error: aborting due to 23 previous errors
92140

93141
For more information about this error, try `rustc --explain E0511`.

tests/ui/simd/intrinsic/generic-arithmetic-pass.rs

+26-9
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
// run-pass
22
#![allow(non_camel_case_types)]
3-
43
// ignore-emscripten FIXME(#45351) hits an LLVM assert
5-
64
#![feature(repr_simd, platform_intrinsics)]
75

86
#[repr(simd)]
@@ -22,18 +20,17 @@ macro_rules! all_eq {
2220
let a = $a;
2321
let b = $b;
2422
assert!(a.0 == b.0 && a.1 == b.1 && a.2 == b.2 && a.3 == b.3);
25-
}}
23+
}};
2624
}
2725

2826
macro_rules! all_eq_ {
2927
($a: expr, $b: expr) => {{
3028
let a = $a;
3129
let b = $b;
3230
assert!(a.0 == b.0);
33-
}}
31+
}};
3432
}
3533

36-
3734
extern "platform-intrinsic" {
3835
fn simd_add<T>(x: T, y: T) -> T;
3936
fn simd_sub<T>(x: T, y: T) -> T;
@@ -47,6 +44,10 @@ extern "platform-intrinsic" {
4744
fn simd_xor<T>(x: T, y: T) -> T;
4845

4946
fn simd_neg<T>(x: T) -> T;
47+
fn simd_bswap<T>(x: T) -> T;
48+
fn simd_bitreverse<T>(x: T) -> T;
49+
fn simd_ctlz<T>(x: T) -> T;
50+
fn simd_cttz<T>(x: T) -> T;
5051
}
5152

5253
fn main() {
@@ -84,8 +85,8 @@ fn main() {
8485
all_eq_!(simd_div(y1, y1), U32::<4>([1, 1, 1, 1]));
8586
all_eq_!(simd_div(U32::<4>([2, 4, 6, 8]), U32::<4>([2, 2, 2, 2])), y1);
8687
all_eq!(simd_div(z1, z1), f32x4(1.0, 1.0, 1.0, 1.0));
87-
all_eq!(simd_div(z1, z2), f32x4(1.0/2.0, 2.0/3.0, 3.0/4.0, 4.0/5.0));
88-
all_eq!(simd_div(z2, z1), f32x4(2.0/1.0, 3.0/2.0, 4.0/3.0, 5.0/4.0));
88+
all_eq!(simd_div(z1, z2), f32x4(1.0 / 2.0, 2.0 / 3.0, 3.0 / 4.0, 4.0 / 5.0));
89+
all_eq!(simd_div(z2, z1), f32x4(2.0 / 1.0, 3.0 / 2.0, 4.0 / 3.0, 5.0 / 4.0));
8990

9091
all_eq!(simd_rem(x1, x1), i32x4(0, 0, 0, 0));
9192
all_eq!(simd_rem(x2, x1), i32x4(0, 1, 1, 1));
@@ -109,8 +110,10 @@ fn main() {
109110
// ensure we get logical vs. arithmetic shifts correct
110111
let (a, b, c, d) = (-12, -123, -1234, -12345);
111112
all_eq!(simd_shr(i32x4(a, b, c, d), x1), i32x4(a >> 1, b >> 2, c >> 3, d >> 4));
112-
all_eq_!(simd_shr(U32::<4>([a as u32, b as u32, c as u32, d as u32]), y1),
113-
U32::<4>([(a as u32) >> 1, (b as u32) >> 2, (c as u32) >> 3, (d as u32) >> 4]));
113+
all_eq_!(
114+
simd_shr(U32::<4>([a as u32, b as u32, c as u32, d as u32]), y1),
115+
U32::<4>([(a as u32) >> 1, (b as u32) >> 2, (c as u32) >> 3, (d as u32) >> 4])
116+
);
114117

115118
all_eq!(simd_and(x1, x2), i32x4(0, 2, 0, 4));
116119
all_eq!(simd_and(x2, x1), i32x4(0, 2, 0, 4));
@@ -132,5 +135,19 @@ fn main() {
132135
all_eq!(simd_neg(z1), f32x4(-1.0, -2.0, -3.0, -4.0));
133136
all_eq!(simd_neg(z2), f32x4(-2.0, -3.0, -4.0, -5.0));
134137

138+
all_eq!(simd_bswap(x1), i32x4(0x01000000, 0x02000000, 0x03000000, 0x04000000));
139+
all_eq_!(simd_bswap(y1), U32::<4>([0x01000000, 0x02000000, 0x03000000, 0x04000000]));
140+
141+
all_eq!(
142+
simd_bitreverse(x1),
143+
i32x4(0x80000000u32 as i32, 0x40000000, 0xc0000000u32 as i32, 0x20000000)
144+
);
145+
all_eq_!(simd_bitreverse(y1), U32::<4>([0x80000000, 0x40000000, 0xc0000000, 0x20000000]));
146+
147+
all_eq!(simd_ctlz(x1), i32x4(31, 30, 30, 29));
148+
all_eq_!(simd_ctlz(y1), U32::<4>([31, 30, 30, 29]));
149+
150+
all_eq!(simd_cttz(x1), i32x4(0, 1, 0, 2));
151+
all_eq_!(simd_cttz(y1), U32::<4>([0, 1, 0, 2]));
135152
}
136153
}

0 commit comments

Comments
 (0)