Fix simd_bswap for i8/u8

This commit is contained in:
Caleb Zulawski 2023-07-30 15:40:32 -04:00
parent 89acdae9f2
commit 77ed437de8
2 changed files with 32 additions and 11 deletions

View File

@ -2096,29 +2096,28 @@ fn generic_simd_intrinsic<'ll, 'tcx>(
sym::simd_cttz => "cttz",
_ => unreachable!(),
};
let llvm_intrinsic = &format!(
"llvm.{}.v{}i{}",
intrinsic_name,
in_len,
in_elem.int_size_and_signed(bx.tcx()).0.bits(),
);
let int_size = in_elem.int_size_and_signed(bx.tcx()).0.bits();
let llvm_intrinsic = &format!("llvm.{}.v{}i{}", intrinsic_name, in_len, int_size,);
return Ok(if matches!(name, sym::simd_ctlz | sym::simd_cttz) {
return if name == sym::simd_bswap && int_size == 8 {
// byte swap is no-op for i8/u8
Ok(args[0].immediate())
} else if matches!(name, sym::simd_ctlz | sym::simd_cttz) {
let fn_ty = bx.type_func(&[vec_ty, bx.type_i1()], vec_ty);
let f = bx.declare_cfn(llvm_intrinsic, llvm::UnnamedAddr::No, fn_ty);
bx.call(
Ok(bx.call(
fn_ty,
None,
None,
f,
&[args[0].immediate(), bx.const_int(bx.type_i1(), 0)],
None,
)
))
} else {
let fn_ty = bx.type_func(&[vec_ty], vec_ty);
let f = bx.declare_cfn(llvm_intrinsic, llvm::UnnamedAddr::No, fn_ty);
bx.call(fn_ty, None, None, f, &[args[0].immediate()], None)
});
Ok(bx.call(fn_ty, None, None, f, &[args[0].immediate()], None))
};
}
if name == sym::simd_arith_offset {

View File

@ -0,0 +1,22 @@
// run-pass
#![feature(repr_simd, platform_intrinsics)]
#![allow(non_camel_case_types)]
#[repr(simd)]
#[derive(Copy, Clone)]
struct i8x4([i8; 4]);
#[repr(simd)]
#[derive(Copy, Clone)]
struct u8x4([u8; 4]);
extern "platform-intrinsic" {
fn simd_bswap<T>(x: T) -> T;
}
fn main() {
unsafe {
assert_eq!(simd_bswap(i8x4([0, 1, 2, 3])).0, [0, 1, 2, 3]);
assert_eq!(simd_bswap(u8x4([0, 1, 2, 3])).0, [0, 1, 2, 3]);
}
}