From 03e5b5d539a79e69dac826590250f90fea99925a Mon Sep 17 00:00:00 2001 From: Ben Kimock Date: Sun, 18 Feb 2024 19:42:27 -0500 Subject: [PATCH] Lower transmutes from int to pointer type as gep on null --- compiler/rustc_codegen_ssa/src/mir/rvalue.rs | 4 ++-- tests/codegen/intrinsics/transmute.rs | 2 +- tests/codegen/transmute-scalar.rs | 2 +- tests/ui/abi/foreign/foreign-call-no-runtime.rs | 6 +++--- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/compiler/rustc_codegen_ssa/src/mir/rvalue.rs b/compiler/rustc_codegen_ssa/src/mir/rvalue.rs index 5c6060a7159aa..7b968b2806812 100644 --- a/compiler/rustc_codegen_ssa/src/mir/rvalue.rs +++ b/compiler/rustc_codegen_ssa/src/mir/rvalue.rs @@ -305,11 +305,11 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> { imm = match (from_scalar.primitive(), to_scalar.primitive()) { (Int(..) | F32 | F64, Int(..) | F32 | F64) => bx.bitcast(imm, to_backend_ty), (Pointer(..), Pointer(..)) => bx.pointercast(imm, to_backend_ty), - (Int(..), Pointer(..)) => bx.inttoptr(imm, to_backend_ty), + (Int(..), Pointer(..)) => bx.gep(bx.type_i8(), bx.const_null(bx.type_ptr()), &[imm]), (Pointer(..), Int(..)) => bx.ptrtoint(imm, to_backend_ty), (F32 | F64, Pointer(..)) => { let int_imm = bx.bitcast(imm, bx.cx().type_isize()); - bx.inttoptr(int_imm, to_backend_ty) + bx.gep(bx.type_i8(), bx.const_null(bx.type_ptr()), &[int_imm]) } (Pointer(..), F32 | F64) => { let int_imm = bx.ptrtoint(imm, bx.cx().type_isize()); diff --git a/tests/codegen/intrinsics/transmute.rs b/tests/codegen/intrinsics/transmute.rs index eff16050875c5..edc2f758bc0b0 100644 --- a/tests/codegen/intrinsics/transmute.rs +++ b/tests/codegen/intrinsics/transmute.rs @@ -371,7 +371,7 @@ pub unsafe fn check_issue_110005(x: (usize, bool)) -> Option> { // CHECK-LABEL: @check_pair_to_dst_ref( #[no_mangle] pub unsafe fn check_pair_to_dst_ref<'a>(x: (usize, usize)) -> &'a [u8] { - // CHECK: %_0.0 = inttoptr i64 %x.0 to ptr + // CHECK: %_0.0 = getelementptr i8, ptr null, i64 %x.0 // CHECK: %0 = insertvalue { ptr, i64 } poison, ptr %_0.0, 0 // CHECK: %1 = insertvalue { ptr, i64 } %0, i64 %x.1, 1 // CHECK: ret { ptr, i64 } %1 diff --git a/tests/codegen/transmute-scalar.rs b/tests/codegen/transmute-scalar.rs index 39126b024a6e9..e7518f426da97 100644 --- a/tests/codegen/transmute-scalar.rs +++ b/tests/codegen/transmute-scalar.rs @@ -49,7 +49,7 @@ pub fn ptr_to_int(p: *mut u16) -> usize { } // CHECK: define{{.*}}ptr @int_to_ptr([[USIZE]] %i) -// CHECK: %_0 = inttoptr [[USIZE]] %i to ptr +// CHECK: %_0 = getelementptr i8, ptr null, i64 %i // CHECK-NEXT: ret ptr %_0 #[no_mangle] pub fn int_to_ptr(i: usize) -> *mut u16 { diff --git a/tests/ui/abi/foreign/foreign-call-no-runtime.rs b/tests/ui/abi/foreign/foreign-call-no-runtime.rs index 7f847d55721c3..054d9d5cc85ce 100644 --- a/tests/ui/abi/foreign/foreign-call-no-runtime.rs +++ b/tests/ui/abi/foreign/foreign-call-no-runtime.rs @@ -40,21 +40,21 @@ pub fn main() { extern "C" fn callback_isize(data: libc::uintptr_t) { unsafe { - let data: *const isize = mem::transmute(data); + let data = data as *const isize; assert_eq!(*data, 100); } } extern "C" fn callback_i64(data: libc::uintptr_t) { unsafe { - let data: *const i64 = mem::transmute(data); + let data = data as *const i64; assert_eq!(*data, 100); } } extern "C" fn callback_i32(data: libc::uintptr_t) { unsafe { - let data: *const i32 = mem::transmute(data); + let data = data as *const i32; assert_eq!(*data, 100); } }