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

Commit 3ea0e6e

Browse files
committed
Add simd_bswap intrinsic
1 parent 0eb5efc commit 3ea0e6e

File tree

6 files changed

+63
-16
lines changed

6 files changed

+63
-16
lines changed

compiler/rustc_codegen_llvm/src/intrinsic.rs

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

2077+
if name == sym::simd_bswap {
2078+
let vec_ty = bx.cx.type_vector(
2079+
match *in_elem.kind() {
2080+
ty::Int(i) => bx.cx.type_int_from_ty(i),
2081+
ty::Uint(i) => bx.cx.type_uint_from_ty(i),
2082+
_ => return_error!(InvalidMonomorphization::UnsupportedOperation {
2083+
span,
2084+
name,
2085+
in_ty,
2086+
in_elem
2087+
}),
2088+
},
2089+
in_len as u64,
2090+
);
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);
2097+
}
2098+
20772099
if name == sym::simd_arith_offset {
20782100
// This also checks that the first operand is a ptr type.
20792101
let pointee = in_elem.builtin_deref(true).unwrap_or_else(|| {

compiler/rustc_hir_analysis/src/check/intrinsic.rs

+1
Original file line numberDiff line numberDiff line change
@@ -521,6 +521,7 @@ 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
524525
| sym::simd_fsqrt
525526
| sym::simd_fsin
526527
| sym::simd_fcos

compiler/rustc_span/src/symbol.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1369,6 +1369,7 @@ symbols! {
13691369
simd_arith_offset,
13701370
simd_as,
13711371
simd_bitmask,
1372+
simd_bswap,
13721373
simd_cast,
13731374
simd_cast_ptr,
13741375
simd_ceil,

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

+7
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ 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;
3031
}
3132

3233
fn main() {
@@ -64,6 +65,8 @@ fn main() {
6465

6566
simd_neg(x);
6667
simd_neg(z);
68+
simd_bswap(x);
69+
simd_bswap(y);
6770

6871

6972
simd_add(0, 0);
@@ -87,6 +90,8 @@ fn main() {
8790

8891
simd_neg(0);
8992
//~^ ERROR expected SIMD input type, found non-SIMD `i32`
93+
simd_bswap(0);
94+
//~^ ERROR expected SIMD input type, found non-SIMD `i32`
9095

9196

9297
simd_shl(z, z);
@@ -98,6 +103,8 @@ fn main() {
98103
simd_or(z, z);
99104
//~^ ERROR unsupported operation on `f32x4` with element `f32`
100105
simd_xor(z, z);
106+
//~^ ERROR unsupported operation on `f32x4` with element `f32`
107+
simd_bswap(z);
101108
//~^ ERROR unsupported operation on `f32x4` with element `f32`
102109
}
103110
}
Original file line numberDiff line numberDiff line change
@@ -1,93 +1,105 @@
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:72: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:74: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:76: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:78: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:80: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:82: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:84: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:86: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:88: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:91: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:93:9
63+
|
64+
LL | simd_bswap(0);
65+
| ^^^^^^^^^^^^^
66+
6167
error[E0511]: invalid monomorphization of `simd_shl` intrinsic: unsupported operation on `f32x4` with element `f32`
62-
--> $DIR/generic-arithmetic-2.rs:92:9
68+
--> $DIR/generic-arithmetic-2.rs:97:9
6369
|
6470
LL | simd_shl(z, z);
6571
| ^^^^^^^^^^^^^^
6672

6773
error[E0511]: invalid monomorphization of `simd_shr` intrinsic: unsupported operation on `f32x4` with element `f32`
68-
--> $DIR/generic-arithmetic-2.rs:94:9
74+
--> $DIR/generic-arithmetic-2.rs:99:9
6975
|
7076
LL | simd_shr(z, z);
7177
| ^^^^^^^^^^^^^^
7278

7379
error[E0511]: invalid monomorphization of `simd_and` intrinsic: unsupported operation on `f32x4` with element `f32`
74-
--> $DIR/generic-arithmetic-2.rs:96:9
80+
--> $DIR/generic-arithmetic-2.rs:101:9
7581
|
7682
LL | simd_and(z, z);
7783
| ^^^^^^^^^^^^^^
7884

7985
error[E0511]: invalid monomorphization of `simd_or` intrinsic: unsupported operation on `f32x4` with element `f32`
80-
--> $DIR/generic-arithmetic-2.rs:98:9
86+
--> $DIR/generic-arithmetic-2.rs:103:9
8187
|
8288
LL | simd_or(z, z);
8389
| ^^^^^^^^^^^^^
8490

8591
error[E0511]: invalid monomorphization of `simd_xor` intrinsic: unsupported operation on `f32x4` with element `f32`
86-
--> $DIR/generic-arithmetic-2.rs:100:9
92+
--> $DIR/generic-arithmetic-2.rs:105:9
8793
|
8894
LL | simd_xor(z, z);
8995
| ^^^^^^^^^^^^^^
9096

91-
error: aborting due to 15 previous errors
97+
error[E0511]: invalid monomorphization of `simd_bswap` intrinsic: unsupported operation on `f32x4` with element `f32`
98+
--> $DIR/generic-arithmetic-2.rs:107:9
99+
|
100+
LL | simd_bswap(z);
101+
| ^^^^^^^^^^^^^
102+
103+
error: aborting due to 17 previous errors
92104

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

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

+4
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ extern "platform-intrinsic" {
4747
fn simd_xor<T>(x: T, y: T) -> T;
4848

4949
fn simd_neg<T>(x: T) -> T;
50+
fn simd_bswap<T>(x: T) -> T;
5051
}
5152

5253
fn main() {
@@ -132,5 +133,8 @@ fn main() {
132133
all_eq!(simd_neg(z1), f32x4(-1.0, -2.0, -3.0, -4.0));
133134
all_eq!(simd_neg(z2), f32x4(-2.0, -3.0, -4.0, -5.0));
134135

136+
all_eq!(simd_bswap(x1), i32x4(0x01000000, 0x02000000, 0x03000000, 0x04000000));
137+
all_eq_!(simd_bswap(y1), U32::<4>([0x01000000, 0x02000000, 0x03000000, 0x04000000]));
138+
135139
}
136140
}

0 commit comments

Comments
 (0)