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

Commit 4c02b4c

Browse files
committed
Add SIMD bitreverse, ctlz, cttz intrinsics
1 parent 3ea0e6e commit 4c02b4c

File tree

6 files changed

+117
-25
lines changed

6 files changed

+117
-25
lines changed

compiler/rustc_codegen_llvm/src/intrinsic.rs

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

2077-
if name == sym::simd_bswap {
2077+
// Unary integer intrinsics
2078+
if matches!(name, sym::simd_bswap | sym::simd_bitreverse | sym::simd_ctlz | sym::simd_cttz) {
20782079
let vec_ty = bx.cx.type_vector(
20792080
match *in_elem.kind() {
20802081
ty::Int(i) => bx.cx.type_int_from_ty(i),
@@ -2088,12 +2089,29 @@ fn generic_simd_intrinsic<'ll, 'tcx>(
20882089
},
20892090
in_len as u64,
20902091
);
2091-
let llvm_intrinsic =
2092-
&format!("llvm.bswap.v{}i{}", in_len, in_elem.int_size_and_signed(bx.tcx()).0.bits(),);
2093-
let fn_ty = bx.type_func(&[vec_ty], vec_ty);
2094-
let f = bx.declare_cfn(llvm_intrinsic, llvm::UnnamedAddr::No, fn_ty);
2095-
let v = bx.call(fn_ty, None, None, f, &[args[0].immediate()], None);
2096-
return Ok(v);
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_bool()], vec_ty);
2108+
let f = bx.declare_cfn(llvm_intrinsic, llvm::UnnamedAddr::No, fn_ty);
2109+
bx.call(fn_ty, None, None, f, &[args[0].immediate(), bx.const_bool(false)], None)
2110+
} else {
2111+
let fn_ty = bx.type_func(&[vec_ty], vec_ty);
2112+
let f = bx.declare_cfn(llvm_intrinsic, llvm::UnnamedAddr::No, fn_ty);
2113+
bx.call(fn_ty, None, None, f, &[args[0].immediate()], None)
2114+
});
20972115
}
20982116

20992117
if name == sym::simd_arith_offset {

compiler/rustc_hir_analysis/src/check/intrinsic.rs

+3
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,9 @@ pub fn check_platform_intrinsic_type(tcx: TyCtxt<'_>, it: &hir::ForeignItem<'_>)
522522
sym::simd_arith_offset => (2, vec![param(0), param(1)], param(0)),
523523
sym::simd_neg
524524
| sym::simd_bswap
525+
| sym::simd_bitreverse
526+
| sym::simd_ctlz
527+
| sym::simd_cttz
525528
| sym::simd_fsqrt
526529
| sym::simd_fsin
527530
| sym::simd_fcos

compiler/rustc_span/src/symbol.rs

+3
Original file line numberDiff line numberDiff line change
@@ -1369,10 +1369,13 @@ symbols! {
13691369
simd_arith_offset,
13701370
simd_as,
13711371
simd_bitmask,
1372+
simd_bitreverse,
13721373
simd_bswap,
13731374
simd_cast,
13741375
simd_cast_ptr,
13751376
simd_ceil,
1377+
simd_ctlz,
1378+
simd_cttz,
13761379
simd_div,
13771380
simd_eq,
13781381
simd_expose_addr,

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

+21
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ extern "platform-intrinsic" {
2828

2929
fn simd_neg<T>(x: T) -> T;
3030
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;
3134
}
3235

3336
fn main() {
@@ -67,6 +70,12 @@ fn main() {
6770
simd_neg(z);
6871
simd_bswap(x);
6972
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);
7079

7180

7281
simd_add(0, 0);
@@ -92,6 +101,12 @@ fn main() {
92101
//~^ ERROR expected SIMD input type, found non-SIMD `i32`
93102
simd_bswap(0);
94103
//~^ 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`
95110

96111

97112
simd_shl(z, z);
@@ -105,6 +120,12 @@ fn main() {
105120
simd_xor(z, z);
106121
//~^ ERROR unsupported operation on `f32x4` with element `f32`
107122
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);
108129
//~^ ERROR unsupported operation on `f32x4` with element `f32`
109130
}
110131
}
Original file line numberDiff line numberDiff line change
@@ -1,105 +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:72: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:74: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:76: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:78: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:80: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:82: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:84: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:86: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:88: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:91:9
56+
--> $DIR/generic-arithmetic-2.rs:100:9
5757
|
5858
LL | simd_neg(0);
5959
| ^^^^^^^^^^^
6060

6161
error[E0511]: invalid monomorphization of `simd_bswap` intrinsic: expected SIMD input type, found non-SIMD `i32`
62-
--> $DIR/generic-arithmetic-2.rs:93:9
62+
--> $DIR/generic-arithmetic-2.rs:102:9
6363
|
6464
LL | simd_bswap(0);
6565
| ^^^^^^^^^^^^^
6666

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+
6785
error[E0511]: invalid monomorphization of `simd_shl` intrinsic: unsupported operation on `f32x4` with element `f32`
68-
--> $DIR/generic-arithmetic-2.rs:97:9
86+
--> $DIR/generic-arithmetic-2.rs:112:9
6987
|
7088
LL | simd_shl(z, z);
7189
| ^^^^^^^^^^^^^^
7290

7391
error[E0511]: invalid monomorphization of `simd_shr` intrinsic: unsupported operation on `f32x4` with element `f32`
74-
--> $DIR/generic-arithmetic-2.rs:99:9
92+
--> $DIR/generic-arithmetic-2.rs:114:9
7593
|
7694
LL | simd_shr(z, z);
7795
| ^^^^^^^^^^^^^^
7896

7997
error[E0511]: invalid monomorphization of `simd_and` intrinsic: unsupported operation on `f32x4` with element `f32`
80-
--> $DIR/generic-arithmetic-2.rs:101:9
98+
--> $DIR/generic-arithmetic-2.rs:116:9
8199
|
82100
LL | simd_and(z, z);
83101
| ^^^^^^^^^^^^^^
84102

85103
error[E0511]: invalid monomorphization of `simd_or` intrinsic: unsupported operation on `f32x4` with element `f32`
86-
--> $DIR/generic-arithmetic-2.rs:103:9
104+
--> $DIR/generic-arithmetic-2.rs:118:9
87105
|
88106
LL | simd_or(z, z);
89107
| ^^^^^^^^^^^^^
90108

91109
error[E0511]: invalid monomorphization of `simd_xor` intrinsic: unsupported operation on `f32x4` with element `f32`
92-
--> $DIR/generic-arithmetic-2.rs:105:9
110+
--> $DIR/generic-arithmetic-2.rs:120:9
93111
|
94112
LL | simd_xor(z, z);
95113
| ^^^^^^^^^^^^^^
96114

97115
error[E0511]: invalid monomorphization of `simd_bswap` intrinsic: unsupported operation on `f32x4` with element `f32`
98-
--> $DIR/generic-arithmetic-2.rs:107:9
116+
--> $DIR/generic-arithmetic-2.rs:122:9
99117
|
100118
LL | simd_bswap(z);
101119
| ^^^^^^^^^^^^^
102120

103-
error: aborting due to 17 previous errors
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
104140

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

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

+11
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ extern "platform-intrinsic" {
4848

4949
fn simd_neg<T>(x: T) -> T;
5050
fn simd_bswap<T>(x: T) -> T;
51+
fn simd_bitreverse<T>(x: T) -> T;
52+
fn simd_ctlz<T>(x: T) -> T;
53+
fn simd_cttz<T>(x: T) -> T;
5154
}
5255

5356
fn main() {
@@ -136,5 +139,13 @@ fn main() {
136139
all_eq!(simd_bswap(x1), i32x4(0x01000000, 0x02000000, 0x03000000, 0x04000000));
137140
all_eq_!(simd_bswap(y1), U32::<4>([0x01000000, 0x02000000, 0x03000000, 0x04000000]));
138141

142+
all_eq!(simd_bitreverse(x1), i32x4(0x80000000u32 as i32, 0x40000000, 0xc0000000u32 as i32, 0x20000000));
143+
all_eq_!(simd_bitreverse(y1), U32::<4>([0x80000000, 0x40000000, 0xc0000000, 0x20000000]));
144+
145+
all_eq!(simd_ctlz(x1), i32x4(31, 30, 30, 29));
146+
all_eq_!(simd_ctlz(y1), U32::<4>([31, 30, 30, 29]));
147+
148+
all_eq!(simd_cttz(x1), i32x4(0, 1, 0, 2));
149+
all_eq_!(simd_cttz(y1), U32::<4>([0, 1, 0, 2]));
139150
}
140151
}

0 commit comments

Comments
 (0)