Skip to content

Commit

Permalink
Auto merge of rust-lang#114266 - calebzulawski:simd-bswap, r=compiler…
Browse files Browse the repository at this point in the history
…-errors

Fix simd_bswap for i8/u8

rust-lang#114156 missed this test case ☹️
cc `@workingjubilee`
  • Loading branch information
bors committed Jul 31, 2023
2 parents dfc9d3f + 77ed437 commit 3be07c1
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 11 deletions.
21 changes: 10 additions & 11 deletions compiler/rustc_codegen_llvm/src/intrinsic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
22 changes: 22 additions & 0 deletions tests/ui/simd/intrinsic/generic-bswap-byte.rs
Original file line number Diff line number Diff line change
@@ -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]);
}
}

0 comments on commit 3be07c1

Please sign in to comment.