Skip to content

Commit d7560e8

Browse files
committed
Auto merge of #79801 - eddyb:scalar-transmute, r=nagisa
rustc_codegen_ssa: use bitcasts instead of type punning for scalar transmutes. This specifically helps with `f32` <-> `u32` (`from_bits`, `to_bits`) in Rust-GPU (`rustc_codegen_spirv`), where (AFAIK) we don't yet have enough infrastructure to turn type punning memory accesses into SSA bitcasts. (There may be more instances, but the one I've seen myself is `f32::signum` from `num-traits` inspecting e.g. the sign bit) Sadly I've had to make an exception for `transmute`s between pointers and non-pointers, as LLVM disallows using `bitcast` for them. r? `@nagisa` cc `@khyperia`
2 parents 39b841d + 718fba9 commit d7560e8

File tree

2 files changed

+104
-0
lines changed

2 files changed

+104
-0
lines changed

compiler/rustc_codegen_ssa/src/mir/block.rs

+19
Original file line numberDiff line numberDiff line change
@@ -1395,6 +1395,25 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
13951395
dst: PlaceRef<'tcx, Bx::Value>,
13961396
) {
13971397
let src = self.codegen_operand(bx, src);
1398+
1399+
// Special-case transmutes between scalars as simple bitcasts.
1400+
match (&src.layout.abi, &dst.layout.abi) {
1401+
(abi::Abi::Scalar(src_scalar), abi::Abi::Scalar(dst_scalar)) => {
1402+
// HACK(eddyb) LLVM doesn't like `bitcast`s between pointers and non-pointers.
1403+
if (src_scalar.value == abi::Pointer) == (dst_scalar.value == abi::Pointer) {
1404+
assert_eq!(src.layout.size, dst.layout.size);
1405+
1406+
// NOTE(eddyb) the `from_immediate` and `to_immediate_scalar`
1407+
// conversions allow handling `bool`s the same as `u8`s.
1408+
let src = bx.from_immediate(src.immediate());
1409+
let src_as_dst = bx.bitcast(src, bx.backend_type(dst.layout));
1410+
Immediate(bx.to_immediate_scalar(src_as_dst, dst_scalar)).store(bx, dst);
1411+
return;
1412+
}
1413+
}
1414+
_ => {}
1415+
}
1416+
13981417
let llty = bx.backend_type(src.layout);
13991418
let cast_ptr = bx.pointercast(dst.llval, bx.type_ptr_to(llty));
14001419
let align = src.layout.align.abi.min(dst.align);

src/test/codegen/transmute-scalar.rs

+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
// compile-flags: -O -C no-prepopulate-passes
2+
3+
#![crate_type = "lib"]
4+
5+
// FIXME(eddyb) all of these tests show memory stores and loads, even after a
6+
// scalar `bitcast`, more special-casing is required to remove `alloca` usage.
7+
8+
// CHECK: define i32 @f32_to_bits(float %x)
9+
// CHECK: %2 = bitcast float %x to i32
10+
// CHECK-NEXT: store i32 %2, i32* %0
11+
// CHECK-NEXT: %3 = load i32, i32* %0
12+
// CHECK: ret i32 %3
13+
#[no_mangle]
14+
pub fn f32_to_bits(x: f32) -> u32 {
15+
unsafe { std::mem::transmute(x) }
16+
}
17+
18+
// CHECK: define i8 @bool_to_byte(i1 zeroext %b)
19+
// CHECK: %1 = zext i1 %b to i8
20+
// CHECK-NEXT: store i8 %1, i8* %0
21+
// CHECK-NEXT: %2 = load i8, i8* %0
22+
// CHECK: ret i8 %2
23+
#[no_mangle]
24+
pub fn bool_to_byte(b: bool) -> u8 {
25+
unsafe { std::mem::transmute(b) }
26+
}
27+
28+
// CHECK: define zeroext i1 @byte_to_bool(i8 %byte)
29+
// CHECK: %1 = trunc i8 %byte to i1
30+
// CHECK-NEXT: %2 = zext i1 %1 to i8
31+
// CHECK-NEXT: store i8 %2, i8* %0
32+
// CHECK-NEXT: %3 = load i8, i8* %0
33+
// CHECK-NEXT: %4 = trunc i8 %3 to i1
34+
// CHECK: ret i1 %4
35+
#[no_mangle]
36+
pub unsafe fn byte_to_bool(byte: u8) -> bool {
37+
std::mem::transmute(byte)
38+
}
39+
40+
// CHECK: define i8* @ptr_to_ptr(i16* %p)
41+
// CHECK: %2 = bitcast i16* %p to i8*
42+
// CHECK-NEXT: store i8* %2, i8** %0
43+
// CHECK-NEXT: %3 = load i8*, i8** %0
44+
// CHECK: ret i8* %3
45+
#[no_mangle]
46+
pub fn ptr_to_ptr(p: *mut u16) -> *mut u8 {
47+
unsafe { std::mem::transmute(p) }
48+
}
49+
50+
// HACK(eddyb) scalar `transmute`s between pointers and non-pointers are
51+
// currently not special-cased like other scalar `transmute`s, because
52+
// LLVM requires specifically `ptrtoint`/`inttoptr` instead of `bitcast`.
53+
//
54+
// Tests below show the non-special-cased behavior (with the possible
55+
// future special-cased instructions in the "NOTE(eddyb)" comments).
56+
57+
// CHECK: define [[USIZE:i[0-9]+]] @ptr_to_int(i16* %p)
58+
59+
// NOTE(eddyb) see above, the following two CHECK lines should ideally be this:
60+
// %2 = ptrtoint i16* %p to [[USIZE]]
61+
// store [[USIZE]] %2, [[USIZE]]* %0
62+
// CHECK: %2 = bitcast [[USIZE]]* %0 to i16**
63+
// CHECK-NEXT: store i16* %p, i16** %2
64+
65+
// CHECK-NEXT: %3 = load [[USIZE]], [[USIZE]]* %0
66+
// CHECK: ret [[USIZE]] %3
67+
#[no_mangle]
68+
pub fn ptr_to_int(p: *mut u16) -> usize {
69+
unsafe { std::mem::transmute(p) }
70+
}
71+
72+
// CHECK: define i16* @int_to_ptr([[USIZE]] %i)
73+
74+
// NOTE(eddyb) see above, the following two CHECK lines should ideally be this:
75+
// %2 = inttoptr [[USIZE]] %i to i16*
76+
// store i16* %2, i16** %0
77+
// CHECK: %2 = bitcast i16** %0 to [[USIZE]]*
78+
// CHECK-NEXT: store [[USIZE]] %i, [[USIZE]]* %2
79+
80+
// CHECK-NEXT: %3 = load i16*, i16** %0
81+
// CHECK: ret i16* %3
82+
#[no_mangle]
83+
pub fn int_to_ptr(i: usize) -> *mut u16 {
84+
unsafe { std::mem::transmute(i) }
85+
}

0 commit comments

Comments
 (0)