Skip to content

Commit b988d69

Browse files
committed
[infer-address-spaces] Handle complex non-pointer constexpr arguments.
Fixes https://bugs.llvm.org/show_bug.cgi?id=51099 Differential Revision: https://reviews.llvm.org/D106098
1 parent 408f2b8 commit b988d69

File tree

2 files changed

+49
-2
lines changed

2 files changed

+49
-2
lines changed

llvm/lib/Transforms/Scalar/InferAddressSpaces.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -635,8 +635,10 @@ static Value *cloneConstantExprWithNewAddressSpace(
635635
ConstantExpr *CE, unsigned NewAddrSpace,
636636
const ValueToValueMapTy &ValueWithNewAddrSpace, const DataLayout *DL,
637637
const TargetTransformInfo *TTI) {
638-
Type *TargetType = PointerType::getWithSamePointeeType(
639-
cast<PointerType>(CE->getType()), NewAddrSpace);
638+
Type *TargetType = CE->getType()->isPointerTy()
639+
? PointerType::getWithSamePointeeType(
640+
cast<PointerType>(CE->getType()), NewAddrSpace)
641+
: CE->getType();
640642

641643
if (CE->getOpcode() == Instruction::AddrSpaceCast) {
642644
// Because CE is flat, the source address space must be specific.

llvm/test/Transforms/InferAddressSpaces/NVPTX/clone_constexpr.ll

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,51 @@ declare void @f1(i32*, i32) local_unnamed_addr #0
3131
declare void @f2(i64*, i64) local_unnamed_addr #0
3232
declare i32 @llvm.nvvm.read.ptx.sreg.tid.x() #1
3333

34+
; Make sure we can clone GEP which uses complex constant expressions as indices.
35+
; https://bugs.llvm.org/show_bug.cgi?id=51099
36+
@g2 = internal addrspace(3) global [128 x i8] undef, align 1
37+
38+
; CHECK-LABEL: @complex_ce(
39+
; CHECK: %0 = load float, float addrspace(3)* bitcast
40+
; CHECK-SAME: i8 addrspace(3)* getelementptr (i8,
41+
; CHECK-SAME: i8 addrspace(3)* getelementptr inbounds ([128 x i8], [128 x i8] addrspace(3)* @g2, i64 0, i64 0),
42+
; CHECK-SAME: i64 sub (
43+
; CHECK-SAME i64 ptrtoint (
44+
; CHECK-SAME i8 addrspace(3)* getelementptr inbounds ([128 x i8], [128 x i8] addrspace(3)* @g2, i64 0, i64 123) to i64),
45+
; CHECK-SAME: i64 ptrtoint (
46+
; CHECK-SAME: i8 addrspace(3)* getelementptr inbounds ([128 x i8], [128 x i8] addrspace(3)* @g2, i64 2, i64 0) to i64)))
47+
; CHECK-SAME: to float addrspace(3)*)
48+
; Function Attrs: norecurse nounwind
49+
define float @complex_ce(i8* nocapture readnone %a, i8* nocapture readnone %b, i8* nocapture readnone %c) local_unnamed_addr #0 {
50+
entry:
51+
%0 = load float, float* bitcast (
52+
i8* getelementptr (
53+
i8, i8* getelementptr inbounds (
54+
[128 x i8],
55+
[128 x i8]* addrspacecast ([128 x i8] addrspace(3)* @g2 to [128 x i8]*),
56+
i64 0,
57+
i64 0),
58+
i64 sub (
59+
i64 ptrtoint (
60+
i8* getelementptr inbounds (
61+
[128 x i8],
62+
[128 x i8]* addrspacecast ([128 x i8] addrspace(3)* @g2 to [128 x i8]*),
63+
i64 0,
64+
i64 123)
65+
to i64),
66+
i64 ptrtoint (
67+
i8* getelementptr inbounds (
68+
[128 x i8],
69+
[128 x i8]* addrspacecast ([128 x i8] addrspace(3)* @g2 to [128 x i8]*),
70+
i64 2,
71+
i64 0)
72+
to i64)))
73+
to float*), align 4
74+
ret float %0
75+
}
76+
77+
78+
3479
attributes #0 = { convergent nounwind }
3580
attributes #1 = { nounwind readnone }
3681
attributes #2 = { nounwind }

0 commit comments

Comments
 (0)