diff options
| author | Jubilee <workingjubilee@gmail.com> | 2025-06-24 19:45:31 -0700 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-06-24 19:45:31 -0700 |
| commit | adaf34045566999aa7770ef3e4c8ef4bdd1ad906 (patch) | |
| tree | 127ce9482363be6dd373bcf7660d0284e726de96 /tests/codegen | |
| parent | 891dc0fb0972c93d0f9b88520ed19d65427d80c8 (diff) | |
| parent | 5d16a7b88450624971004ffc2ce6dbde0bb03871 (diff) | |
| download | rust-adaf34045566999aa7770ef3e4c8ef4bdd1ad906.tar.gz rust-adaf34045566999aa7770ef3e4c8ef4bdd1ad906.zip | |
Rollup merge of #142768 - scottmcm:avoid-unneeded-bitcast, r=WaffleLapkin
Avoid a bitcast FFI call in transmuting For things that only change the valid ranges, we can just return the input, rather than making the `LLVMBuildBitCast` call and having *it* then do nothing. I tried to tweak this a bit more and broke stuff, so I also added some extra tests for that as we apparently didn't have coverage.
Diffstat (limited to 'tests/codegen')
| -rw-r--r-- | tests/codegen/transmute-scalar.rs | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/tests/codegen/transmute-scalar.rs b/tests/codegen/transmute-scalar.rs index c080259a917..c57ade58c30 100644 --- a/tests/codegen/transmute-scalar.rs +++ b/tests/codegen/transmute-scalar.rs @@ -55,3 +55,48 @@ pub fn ptr_to_int(p: *mut u16) -> usize { pub fn int_to_ptr(i: usize) -> *mut u16 { unsafe { std::mem::transmute(i) } } + +// This is the one case where signedness matters to transmuting: +// the LLVM type is `i8` here because of `repr(i8)`, +// whereas below with the `repr(u8)` it's `i1` in LLVM instead. +#[repr(i8)] +pub enum FakeBoolSigned { + False = 0, + True = 1, +} + +// CHECK-LABEL: define{{.*}}i8 @bool_to_fake_bool_signed(i1 zeroext %b) +// CHECK: %_0 = zext i1 %b to i8 +// CHECK-NEXT: ret i8 %_0 +#[no_mangle] +pub fn bool_to_fake_bool_signed(b: bool) -> FakeBoolSigned { + unsafe { std::mem::transmute(b) } +} + +// CHECK-LABEL: define{{.*}}i1 @fake_bool_signed_to_bool(i8 %b) +// CHECK: %_0 = trunc nuw i8 %b to i1 +// CHECK-NEXT: ret i1 %_0 +#[no_mangle] +pub fn fake_bool_signed_to_bool(b: FakeBoolSigned) -> bool { + unsafe { std::mem::transmute(b) } +} + +#[repr(u8)] +pub enum FakeBoolUnsigned { + False = 0, + True = 1, +} + +// CHECK-LABEL: define{{.*}}i1 @bool_to_fake_bool_unsigned(i1 zeroext %b) +// CHECK: ret i1 %b +#[no_mangle] +pub fn bool_to_fake_bool_unsigned(b: bool) -> FakeBoolUnsigned { + unsafe { std::mem::transmute(b) } +} + +// CHECK-LABEL: define{{.*}}i1 @fake_bool_unsigned_to_bool(i1 zeroext %b) +// CHECK: ret i1 %b +#[no_mangle] +pub fn fake_bool_unsigned_to_bool(b: FakeBoolUnsigned) -> bool { + unsafe { std::mem::transmute(b) } +} |
