Skip to content

Commit 51e1666

Browse files
committed
fix bitcast of single-element SIMD vectors
1 parent d41e12f commit 51e1666

File tree

2 files changed

+23
-8
lines changed

2 files changed

+23
-8
lines changed

compiler/rustc_codegen_ssa/src/mir/rvalue.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1123,7 +1123,7 @@ pub(super) fn transmute_immediate<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
11231123
// While optimizations will remove no-op transmutes, they might still be
11241124
// there in debug or things that aren't no-op in MIR because they change
11251125
// the Rust type but not the underlying layout/niche.
1126-
if from_scalar == to_scalar {
1126+
if from_scalar == to_scalar && from_backend_ty == to_backend_ty {
11271127
return imm;
11281128
}
11291129

@@ -1142,13 +1142,7 @@ pub(super) fn transmute_immediate<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
11421142
assume_scalar_range(bx, imm, from_scalar, from_backend_ty);
11431143

11441144
imm = match (from_scalar.primitive(), to_scalar.primitive()) {
1145-
(Int(..) | Float(_), Int(..) | Float(_)) => {
1146-
if from_backend_ty == to_backend_ty {
1147-
imm
1148-
} else {
1149-
bx.bitcast(imm, to_backend_ty)
1150-
}
1151-
}
1145+
(Int(..) | Float(_), Int(..) | Float(_)) => bx.bitcast(imm, to_backend_ty),
11521146
(Pointer(..), Pointer(..)) => bx.pointercast(imm, to_backend_ty),
11531147
(Int(..), Pointer(..)) => bx.ptradd(bx.const_null(bx.type_ptr()), imm),
11541148
(Pointer(..), Int(..)) => {

tests/codegen/transmute-scalar.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//@ compile-flags: -C opt-level=0 -C no-prepopulate-passes
22

3+
#![feature(repr_simd)]
34
#![crate_type = "lib"]
45

56
// With opaque ptrs in LLVM, `transmute` can load/store any `alloca` as any type,
@@ -100,3 +101,23 @@ pub fn bool_to_fake_bool_unsigned(b: bool) -> FakeBoolUnsigned {
100101
pub fn fake_bool_unsigned_to_bool(b: FakeBoolUnsigned) -> bool {
101102
unsafe { std::mem::transmute(b) }
102103
}
104+
105+
#[repr(simd)]
106+
#[derive(Clone, Copy)]
107+
struct S([i64; 1]);
108+
109+
// CHECK-LABEL: define i64 @single_element_simd_to_scalar(<1 x i64> %b)
110+
// CHECK: bitcast <1 x i64> %b to i64
111+
// CHECK: ret i64
112+
#[no_mangle]
113+
pub extern "C" fn single_element_simd_to_scalar(b: S) -> i64 {
114+
unsafe { std::mem::transmute(b) }
115+
}
116+
117+
// CHECK-LABEL: define <1 x i64> @scalar_to_single_element_simd(i64 %b)
118+
// CHECK: bitcast i64 %b to <1 x i64>
119+
// CHECK: ret <1 x i64>
120+
#[no_mangle]
121+
pub extern "C" fn scalar_to_single_element_simd(b: i64) -> S {
122+
unsafe { std::mem::transmute(b) }
123+
}

0 commit comments

Comments
 (0)