Skip to content

Commit 9e02838

Browse files
Auto merge of #147541 - saethlin:transmute-carefully, r=<try>
Change int-to-ptr transmute lowering back to inttoptr
2 parents 9725c4b + 511ef23 commit 9e02838

File tree

7 files changed

+28
-8
lines changed

7 files changed

+28
-8
lines changed

compiler/rustc_codegen_ssa/src/mir/rvalue.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1010,14 +1010,14 @@ pub(super) fn transmute_scalar<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
10101010
imm = match (from_scalar.primitive(), to_scalar.primitive()) {
10111011
(Int(..) | Float(_), Int(..) | Float(_)) => bx.bitcast(imm, to_backend_ty),
10121012
(Pointer(..), Pointer(..)) => bx.pointercast(imm, to_backend_ty),
1013-
(Int(..), Pointer(..)) => bx.ptradd(bx.const_null(bx.type_ptr()), imm),
1013+
(Int(..), Pointer(..)) => bx.inttoptr(imm, to_backend_ty),
10141014
(Pointer(..), Int(..)) => {
10151015
// FIXME: this exposes the provenance, which shouldn't be necessary.
10161016
bx.ptrtoint(imm, to_backend_ty)
10171017
}
10181018
(Float(_), Pointer(..)) => {
10191019
let int_imm = bx.bitcast(imm, bx.cx().type_isize());
1020-
bx.ptradd(bx.const_null(bx.type_ptr()), int_imm)
1020+
bx.inttoptr(int_imm, to_backend_ty)
10211021
}
10221022
(Pointer(..), Float(_)) => {
10231023
// FIXME: this exposes the provenance, which shouldn't be necessary.

tests/codegen-llvm/common_prim_int_ptr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#[no_mangle]
1212
pub fn insert_int(x: usize) -> Result<usize, Box<()>> {
1313
// CHECK: start:
14-
// CHECK-NEXT: %[[WO_PROV:.+]] = getelementptr i8, ptr null, [[USIZE:i[0-9]+]] %x
14+
// CHECK-NEXT: %[[WO_PROV:.+]] = inttoptr [[USIZE:i[0-9]+]] %x to ptr
1515
// CHECK-NEXT: %[[R:.+]] = insertvalue { [[USIZE]], ptr } { [[USIZE]] 0, ptr poison }, ptr %[[WO_PROV]], 1
1616
// CHECK-NEXT: ret { [[USIZE]], ptr } %[[R]]
1717
Ok(x)

tests/codegen-llvm/enum/enum-aggregate.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ fn make_ok_ptr(x: NonNull<u16>) -> Result<NonNull<u16>, usize> {
7070
fn make_ok_int(x: usize) -> Result<usize, NonNull<u16>> {
7171
// CHECK-LABEL: { i64, ptr } @make_ok_int(i64 %x)
7272
// CHECK-NEXT: start:
73-
// CHECK-NEXT: %[[NOPROV:.+]] = getelementptr i8, ptr null, i64 %x
73+
// CHECK-NEXT: %[[NOPROV:.+]] = inttoptr i64 %x to ptr
7474
// CHECK-NEXT: %[[R:.+]] = insertvalue { i64, ptr } { i64 0, ptr poison }, ptr %[[NOPROV]], 1
7575
// CHECK-NEXT: ret { i64, ptr } %[[R]]
7676
Ok(x)
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// This is a regression test for https://github.com/rust-lang/rust/issues/147265.
2+
3+
//@ compile-flags: -Copt-level=3
4+
5+
#![crate_type = "lib"]
6+
7+
#[no_mangle]
8+
pub fn mk_result(a: usize) -> Result<u8, *const u8> {
9+
// CHECK-LABEL: @mk_result
10+
// CHECK-NOT: unreachable
11+
match g(a) {
12+
Ok(b) => Ok(unsafe { *(b as *const u8) }),
13+
Err(c) => Err(c),
14+
}
15+
}
16+
17+
#[cold]
18+
fn g(a: usize) -> Result<usize, *const u8> {
19+
Ok(a)
20+
}

tests/codegen-llvm/intrinsics/transmute-niched.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ pub unsafe fn check_four_or_eight_to_nonnull(x: FourOrEight) -> NonNull<u8> {
249249
// OPT: call void @llvm.assume(i1 %1)
250250
// CHECK-NOT: icmp
251251
// CHECK-NOT: assume
252-
// CHECK: %[[RET:.+]] = getelementptr i8, ptr null, i64 %x
252+
// CHECK: %[[RET:.+]] = inttoptr i64 %x to ptr
253253
// CHECK-NEXT: ret ptr %[[RET]]
254254

255255
transmute(x)

tests/codegen-llvm/intrinsics/transmute.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ pub unsafe fn check_pair_with_bool(x: (u8, bool)) -> (bool, i8) {
303303
pub unsafe fn check_float_to_pointer(x: f64) -> *const () {
304304
// CHECK-NOT: alloca
305305
// CHECK: %0 = bitcast double %x to i64
306-
// CHECK: %_0 = getelementptr i8, ptr null, i64 %0
306+
// CHECK: %_0 = inttoptr i64 %0 to ptr
307307
// CHECK: ret ptr %_0
308308
transmute(x)
309309
}
@@ -378,7 +378,7 @@ pub unsafe fn check_issue_110005(x: (usize, bool)) -> Option<Box<[u8]>> {
378378
// CHECK-LABEL: @check_pair_to_dst_ref(
379379
#[no_mangle]
380380
pub unsafe fn check_pair_to_dst_ref<'a>(x: (usize, usize)) -> &'a [u8] {
381-
// CHECK: %_0.0 = getelementptr i8, ptr null, i64 %x.0
381+
// CHECK: %_0.0 = inttoptr i64 %x.0 to ptr
382382
// CHECK: %0 = icmp ne ptr %_0.0, null
383383
// CHECK: call void @llvm.assume(i1 %0)
384384
// CHECK: %1 = insertvalue { ptr, i64 } poison, ptr %_0.0, 0

tests/codegen-llvm/transmute-scalar.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ pub fn ptr_to_int(p: *mut u16) -> usize {
5656
}
5757

5858
// CHECK: define{{.*}}ptr @int_to_ptr([[USIZE]] %i)
59-
// CHECK: %_0 = getelementptr i8, ptr null, [[USIZE]] %i
59+
// CHECK: %_0 = inttoptr [[USIZE]] %i to ptr
6060
// CHECK-NEXT: ret ptr %_0
6161
#[no_mangle]
6262
pub fn int_to_ptr(i: usize) -> *mut u16 {

0 commit comments

Comments
 (0)