Skip to content

Commit 0fa7fea

Browse files
committed
Auto merge of rust-lang#121282 - saethlin:gep-null-means-no-provenance, r=scottmcm
Lower transmutes from int to pointer type as gep on null I thought of this while looking at rust-lang#121242. See that PR's description for why this lowering is preferable. The UI test that's being changed here crashes without changing the transmutes into casts. Based on that, this PR should not be merged without a crater build-and-test run.
2 parents 5aad51d + 2eb9c6d commit 0fa7fea

File tree

4 files changed

+8
-8
lines changed

4 files changed

+8
-8
lines changed

compiler/rustc_codegen_ssa/src/mir/rvalue.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -306,11 +306,11 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
306306
bx.bitcast(imm, to_backend_ty)
307307
}
308308
(Pointer(..), Pointer(..)) => bx.pointercast(imm, to_backend_ty),
309-
(Int(..), Pointer(..)) => bx.inttoptr(imm, to_backend_ty),
309+
(Int(..), Pointer(..)) => bx.ptradd(bx.const_null(bx.type_ptr()), imm),
310310
(Pointer(..), Int(..)) => bx.ptrtoint(imm, to_backend_ty),
311311
(F16 | F32 | F64 | F128, Pointer(..)) => {
312312
let int_imm = bx.bitcast(imm, bx.cx().type_isize());
313-
bx.inttoptr(int_imm, to_backend_ty)
313+
bx.ptradd(bx.const_null(bx.type_ptr()), int_imm)
314314
}
315315
(Pointer(..), F16 | F32 | F64 | F128) => {
316316
let int_imm = bx.ptrtoint(imm, bx.cx().type_isize());

tests/codegen/intrinsics/transmute.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ pub unsafe fn check_pair_with_bool(x: (u8, bool)) -> (bool, i8) {
296296
pub unsafe fn check_float_to_pointer(x: f64) -> *const () {
297297
// CHECK-NOT: alloca
298298
// CHECK: %0 = bitcast double %x to i64
299-
// CHECK: %_0 = inttoptr i64 %0 to ptr
299+
// CHECK: %_0 = getelementptr i8, ptr null, i64 %0
300300
// CHECK: ret ptr %_0
301301
transmute(x)
302302
}
@@ -371,7 +371,7 @@ pub unsafe fn check_issue_110005(x: (usize, bool)) -> Option<Box<[u8]>> {
371371
// CHECK-LABEL: @check_pair_to_dst_ref(
372372
#[no_mangle]
373373
pub unsafe fn check_pair_to_dst_ref<'a>(x: (usize, usize)) -> &'a [u8] {
374-
// CHECK: %_0.0 = inttoptr i64 %x.0 to ptr
374+
// CHECK: %_0.0 = getelementptr i8, ptr null, i64 %x.0
375375
// CHECK: %0 = insertvalue { ptr, i64 } poison, ptr %_0.0, 0
376376
// CHECK: %1 = insertvalue { ptr, i64 } %0, i64 %x.1, 1
377377
// CHECK: ret { ptr, i64 } %1

tests/codegen/transmute-scalar.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ pub fn ptr_to_int(p: *mut u16) -> usize {
4949
}
5050

5151
// CHECK: define{{.*}}ptr @int_to_ptr([[USIZE]] %i)
52-
// CHECK: %_0 = inttoptr [[USIZE]] %i to ptr
52+
// CHECK: %_0 = getelementptr i8, ptr null, [[USIZE]] %i
5353
// CHECK-NEXT: ret ptr %_0
5454
#[no_mangle]
5555
pub fn int_to_ptr(i: usize) -> *mut u16 {

tests/ui/abi/foreign/foreign-call-no-runtime.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -40,21 +40,21 @@ pub fn main() {
4040

4141
extern "C" fn callback_isize(data: libc::uintptr_t) {
4242
unsafe {
43-
let data: *const isize = mem::transmute(data);
43+
let data = data as *const isize;
4444
assert_eq!(*data, 100);
4545
}
4646
}
4747

4848
extern "C" fn callback_i64(data: libc::uintptr_t) {
4949
unsafe {
50-
let data: *const i64 = mem::transmute(data);
50+
let data = data as *const i64;
5151
assert_eq!(*data, 100);
5252
}
5353
}
5454

5555
extern "C" fn callback_i32(data: libc::uintptr_t) {
5656
unsafe {
57-
let data: *const i32 = mem::transmute(data);
57+
let data = data as *const i32;
5858
assert_eq!(*data, 100);
5959
}
6060
}

0 commit comments

Comments
 (0)