Skip to content

Commit ac81cb7

Browse files
committed
Allow ptrtoint/inttoptr of non-integral pointer types in IR
I don't like landing this change, but it's an acknowledgement of a practical reality. Despite not having well specified semantics for inttoptr and ptrtoint involving non-integral pointer types, they are used in practice. Here's a quick summary of the current pragmatic reality: * I happen to know that the main external user of non-integral pointers has effectively disabled the verifier rules. * RS4GC (the lowering pass for abstract GC machine model which is the key motivation for non-integral pointers), even supports them. We just have all the tests using an integral pointer space to let the verifier run. * Certain idioms (such as alignment checks for alignment N, where any relocation is guaranteed to be N byte aligned) are fine in practice. * As implemented, inttoptr/ptrtoint are CSEd and are not control dependent. This means that any code which is intending to check a particular bit pattern at site of use must be wrapped in an intrinsic or external function call. This change allows them in the Verifier, and updates the LangRef to specific them as implementation dependent. This allows us to acknowledge current reality while still leaving ourselves room to punt on figuring out "good" semantics until the future.
1 parent 22dea69 commit ac81cb7

File tree

5 files changed

+58
-44
lines changed

5 files changed

+58
-44
lines changed

llvm/docs/LangRef.rst

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -601,10 +601,11 @@ Non-integral pointer types represent pointers that have an *unspecified* bitwise
601601
representation; that is, the integral representation may be target dependent or
602602
unstable (not backed by a fixed integer).
603603

604-
``inttoptr`` instructions converting integers to non-integral pointer types are
605-
ill-typed, and so are ``ptrtoint`` instructions converting values of
606-
non-integral pointer types to integers. Vector versions of said instructions
607-
are ill-typed as well.
604+
``inttoptr`` and ``ptrtoint`` instructions converting integers to non-integral
605+
pointer types or vice versa are implementation defined, and subject to likely
606+
future revision in semantics. Vector versions of said instructions are as well.
607+
Users of non-integral-pointer types are advised not to design around current
608+
semantics as they may very well change in the nearish future.
608609

609610
.. _globalvars:
610611

llvm/lib/IR/Verifier.cpp

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2178,19 +2178,6 @@ void Verifier::visitConstantExpr(const ConstantExpr *CE) {
21782178
Assert(CastInst::castIsValid(Instruction::BitCast, CE->getOperand(0),
21792179
CE->getType()),
21802180
"Invalid bitcast", CE);
2181-
2182-
if (CE->getOpcode() == Instruction::IntToPtr ||
2183-
CE->getOpcode() == Instruction::PtrToInt) {
2184-
auto *PtrTy = CE->getOpcode() == Instruction::IntToPtr
2185-
? CE->getType()
2186-
: CE->getOperand(0)->getType();
2187-
StringRef Msg = CE->getOpcode() == Instruction::IntToPtr
2188-
? "inttoptr not supported for non-integral pointers"
2189-
: "ptrtoint not supported for non-integral pointers";
2190-
Assert(
2191-
!DL.isNonIntegralPointerType(cast<PointerType>(PtrTy->getScalarType())),
2192-
Msg);
2193-
}
21942181
}
21952182

21962183
bool Verifier::verifyAttributeCount(AttributeList Attrs, unsigned Params) {
@@ -3041,10 +3028,6 @@ void Verifier::visitPtrToIntInst(PtrToIntInst &I) {
30413028

30423029
Assert(SrcTy->isPtrOrPtrVectorTy(), "PtrToInt source must be pointer", &I);
30433030

3044-
if (auto *PTy = dyn_cast<PointerType>(SrcTy->getScalarType()))
3045-
Assert(!DL.isNonIntegralPointerType(PTy),
3046-
"ptrtoint not supported for non-integral pointers");
3047-
30483031
Assert(DestTy->isIntOrIntVectorTy(), "PtrToInt result must be integral", &I);
30493032
Assert(SrcTy->isVectorTy() == DestTy->isVectorTy(), "PtrToInt type mismatch",
30503033
&I);
@@ -3068,10 +3051,6 @@ void Verifier::visitIntToPtrInst(IntToPtrInst &I) {
30683051
"IntToPtr source must be an integral", &I);
30693052
Assert(DestTy->isPtrOrPtrVectorTy(), "IntToPtr result must be a pointer", &I);
30703053

3071-
if (auto *PTy = dyn_cast<PointerType>(DestTy->getScalarType()))
3072-
Assert(!DL.isNonIntegralPointerType(PTy),
3073-
"inttoptr not supported for non-integral pointers");
3074-
30753054
Assert(SrcTy->isVectorTy() == DestTy->isVectorTy(), "IntToPtr type mismatch",
30763055
&I);
30773056
if (SrcTy->isVectorTy()) {
@@ -4539,12 +4518,9 @@ void Verifier::visitInstruction(Instruction &I) {
45394518
Assert(CBI && &CBI->getCalledOperandUse() == &I.getOperandUse(i),
45404519
"Cannot take the address of an inline asm!", &I);
45414520
} else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(I.getOperand(i))) {
4542-
if (CE->getType()->isPtrOrPtrVectorTy() ||
4543-
!DL.getNonIntegralAddressSpaces().empty()) {
4521+
if (CE->getType()->isPtrOrPtrVectorTy()) {
45444522
// If we have a ConstantExpr pointer, we need to see if it came from an
4545-
// illegal bitcast. If the datalayout string specifies non-integral
4546-
// address spaces then we also need to check for illegal ptrtoint and
4547-
// inttoptr expressions.
4523+
// illegal bitcast.
45484524
visitConstantExprsRecursively(CE);
45494525
}
45504526
}

llvm/test/Transforms/RewriteStatepointsForGC/base-inttoptr.ll

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
; RUN: opt -S -rewrite-statepoints-for-gc < %s | FileCheck %s
33

44
target triple = "x86_64-unknown-linux-gnu"
5+
target datalayout = "e-ni:1:6"
56

67
declare void @foo()
78

llvm/test/Transforms/RewriteStatepointsForGC/constants.ll

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
; RUN: opt -S -rewrite-statepoints-for-gc < %s | FileCheck %s
22
; RUN: opt -S -passes=rewrite-statepoints-for-gc < %s | FileCheck %s
33

4+
target datalayout = "e-ni:1:6"
5+
46
; constants don't get relocated.
57
@G = addrspace(1) global i8 5
68

Lines changed: 48 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,84 +1,118 @@
1-
; RUN: not opt -verify < %s 2>&1 | FileCheck %s
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2+
; RUN: opt -verify -S < %s 2>&1 | FileCheck %s
23

34
target datalayout = "e-ni:4:6"
45

56
define i64 @f_0(i8 addrspace(4)* %ptr) {
6-
; CHECK: ptrtoint not supported for non-integral pointers
7+
; CHECK-LABEL: @f_0(
8+
; CHECK-NEXT: [[VAL:%.*]] = ptrtoint i8 addrspace(4)* [[PTR:%.*]] to i64
9+
; CHECK-NEXT: ret i64 [[VAL]]
10+
;
711
%val = ptrtoint i8 addrspace(4)* %ptr to i64
812
ret i64 %val
913
}
1014

1115
define <4 x i64> @f_1(<4 x i8 addrspace(4)*> %ptr) {
12-
; CHECK: ptrtoint not supported for non-integral pointers
16+
; CHECK-LABEL: @f_1(
17+
; CHECK-NEXT: [[VAL:%.*]] = ptrtoint <4 x i8 addrspace(4)*> [[PTR:%.*]] to <4 x i64>
18+
; CHECK-NEXT: ret <4 x i64> [[VAL]]
19+
;
1320
%val = ptrtoint <4 x i8 addrspace(4)*> %ptr to <4 x i64>
1421
ret <4 x i64> %val
1522
}
1623

1724
define i64 @f_2(i8 addrspace(3)* %ptr) {
18-
; Negative test
25+
; CHECK-LABEL: @f_2(
26+
; CHECK-NEXT: [[VAL:%.*]] = ptrtoint i8 addrspace(3)* [[PTR:%.*]] to i64
27+
; CHECK-NEXT: ret i64 [[VAL]]
28+
;
1929
%val = ptrtoint i8 addrspace(3)* %ptr to i64
2030
ret i64 %val
2131
}
2232

2333
define i8 addrspace(4)* @f_3(i64 %integer) {
24-
; CHECK: inttoptr not supported for non-integral pointers
34+
; CHECK-LABEL: @f_3(
35+
; CHECK-NEXT: [[VAL:%.*]] = inttoptr i64 [[INTEGER:%.*]] to i8 addrspace(4)*
36+
; CHECK-NEXT: ret i8 addrspace(4)* [[VAL]]
37+
;
2538
%val = inttoptr i64 %integer to i8 addrspace(4)*
2639
ret i8 addrspace(4)* %val
2740
}
2841

2942
define <4 x i8 addrspace(4)*> @f_4(<4 x i64> %integer) {
30-
; CHECK: inttoptr not supported for non-integral pointers
43+
; CHECK-LABEL: @f_4(
44+
; CHECK-NEXT: [[VAL:%.*]] = inttoptr <4 x i64> [[INTEGER:%.*]] to <4 x i8 addrspace(4)*>
45+
; CHECK-NEXT: ret <4 x i8 addrspace(4)*> [[VAL]]
46+
;
3147
%val = inttoptr <4 x i64> %integer to <4 x i8 addrspace(4)*>
3248
ret <4 x i8 addrspace(4)*> %val
3349
}
3450

3551
define i8 addrspace(3)* @f_5(i64 %integer) {
36-
; Negative test
52+
; CHECK-LABEL: @f_5(
53+
; CHECK-NEXT: [[VAL:%.*]] = inttoptr i64 [[INTEGER:%.*]] to i8 addrspace(3)*
54+
; CHECK-NEXT: ret i8 addrspace(3)* [[VAL]]
55+
;
3756
%val = inttoptr i64 %integer to i8 addrspace(3)*
3857
ret i8 addrspace(3)* %val
3958
}
4059

4160
define i64 @f_6(i8 addrspace(6)* %ptr) {
42-
; CHECK: ptrtoint not supported for non-integral pointers
61+
; CHECK-LABEL: @f_6(
62+
; CHECK-NEXT: [[VAL:%.*]] = ptrtoint i8 addrspace(6)* [[PTR:%.*]] to i64
63+
; CHECK-NEXT: ret i64 [[VAL]]
64+
;
4365
%val = ptrtoint i8 addrspace(6)* %ptr to i64
4466
ret i64 %val
4567
}
4668

4769
define i8 addrspace(4)* @f_7() {
48-
; CHECK: inttoptr not supported for non-integral pointers
70+
; CHECK-LABEL: @f_7(
71+
; CHECK-NEXT: ret i8 addrspace(4)* inttoptr (i64 50 to i8 addrspace(4)*)
72+
;
4973
ret i8 addrspace(4)* inttoptr (i64 50 to i8 addrspace(4)*)
5074
}
5175

5276
@global0 = addrspace(4) constant i8 42
5377

5478
define i64 @f_8() {
55-
; CHECK: ptrtoint not supported for non-integral pointers
79+
; CHECK-LABEL: @f_8(
80+
; CHECK-NEXT: ret i64 ptrtoint (i8 addrspace(4)* @global0 to i64)
81+
;
5682
ret i64 ptrtoint (i8 addrspace(4)* @global0 to i64)
5783
}
5884

5985
define i8 addrspace(4)* @f_9() {
60-
; CHECK: inttoptr not supported for non-integral pointers
86+
; CHECK-LABEL: @f_9(
87+
; CHECK-NEXT: ret i8 addrspace(4)* getelementptr (i8, i8 addrspace(4)* inttoptr (i64 55 to i8 addrspace(4)*), i32 100)
88+
;
6189
ret i8 addrspace(4)* getelementptr (i8, i8 addrspace(4)* inttoptr (i64 55 to i8 addrspace(4)*), i32 100)
6290
}
6391

6492
@global1 = addrspace(4) constant i8 42
6593

6694
define i8 addrspace(4)* @f_10() {
67-
; CHECK: ptrtoint not supported for non-integral pointers
95+
; CHECK-LABEL: @f_10(
96+
; CHECK-NEXT: ret i8 addrspace(4)* getelementptr (i8, i8 addrspace(4)* @global0, i64 ptrtoint (i8 addrspace(4)* @global1 to i64))
97+
;
6898
ret i8 addrspace(4)* getelementptr (i8, i8 addrspace(4)* @global0, i64 ptrtoint (i8 addrspace(4)* @global1 to i64))
6999
}
70100

71101
@cycle_0 = addrspace(4) constant i64 ptrtoint (i64 addrspace(4)* addrspace(4)* @cycle_1 to i64)
72102
@cycle_1 = addrspace(4) constant i64 addrspace(4) * @cycle_0
73103

74104
define i64 addrspace(4)* addrspace(4)* @f_11() {
75-
; CHECK: ptrtoint not supported for non-integral pointers
105+
; CHECK-LABEL: @f_11(
106+
; CHECK-NEXT: ret i64 addrspace(4)* addrspace(4)* @cycle_1
107+
;
76108
ret i64 addrspace(4)* addrspace(4)* @cycle_1
77109
}
78110

79111
@cycle_self = addrspace(4) constant i64 ptrtoint (i64 addrspace(4)* @cycle_self to i64)
80112

81113
define i64 addrspace(4)* @f_12() {
82-
; CHECK: ptrtoint not supported for non-integral pointers
114+
; CHECK-LABEL: @f_12(
115+
; CHECK-NEXT: ret i64 addrspace(4)* @cycle_self
116+
;
83117
ret i64 addrspace(4)* @cycle_self
84118
}

0 commit comments

Comments
 (0)