Skip to content

Commit 5f1e240

Browse files
committed
make geps have array types to make the validator happy
1 parent 74b05e0 commit 5f1e240

File tree

2 files changed

+40
-17
lines changed

2 files changed

+40
-17
lines changed

llvm/lib/Target/DirectX/DXILLegalizePass.cpp

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,11 @@ static void fixI8UseChain(Instruction &I,
9494
Type *ElementType = NewOperands[0]->getType();
9595
if (auto *AI = dyn_cast<AllocaInst>(NewOperands[0]))
9696
ElementType = AI->getAllocatedType();
97-
if (auto *GEP = dyn_cast<GetElementPtrInst>(NewOperands[0]))
97+
if (auto *GEP = dyn_cast<GetElementPtrInst>(NewOperands[0])) {
9898
ElementType = GEP->getSourceElementType();
99+
if (ElementType->isArrayTy())
100+
ElementType = ElementType->getArrayElementType();
101+
}
99102
LoadInst *NewLoad = Builder.CreateLoad(ElementType, NewOperands[0]);
100103
ReplacedValues[Load] = NewLoad;
101104
ToRemove.push_back(Load);
@@ -116,9 +119,23 @@ static void fixI8UseChain(Instruction &I,
116119
uint32_t ByteOffset = Offset->getZExtValue();
117120
uint32_t ElemSize = Load->getDataLayout().getTypeAllocSize(ElementType);
118121
uint32_t Index = ByteOffset / ElemSize;
119-
Value *NewGEP = Builder.CreateGEP(ElementType, GEP->getPointerOperand(),
120-
Builder.getInt32(Index), GEP->getName(),
121-
GEP->getNoWrapFlags());
122+
123+
Value *PtrOperand = GEP->getPointerOperand();
124+
Type *GEPType = GEP->getPointerOperandType();
125+
126+
if (auto *GV = dyn_cast<GlobalVariable>(PtrOperand))
127+
GEPType = GV->getValueType();
128+
if (auto *AI = dyn_cast<AllocaInst>(PtrOperand))
129+
GEPType = AI->getAllocatedType();
130+
131+
if (auto *ArrTy = dyn_cast<ArrayType>(GEPType))
132+
GEPType = ArrTy;
133+
else
134+
GEPType = ArrayType::get(ElementType, 1); // its a scalar
135+
136+
Value *NewGEP = Builder.CreateGEP(
137+
GEPType, PtrOperand, {Builder.getInt32(0), Builder.getInt32(Index)},
138+
GEP->getName(), GEP->getNoWrapFlags());
122139

123140
LoadInst *NewLoad = Builder.CreateLoad(ElementType, NewGEP);
124141
ReplacedValues[Load] = NewLoad;
@@ -181,6 +198,7 @@ static void fixI8UseChain(Instruction &I,
181198
Cast->replaceAllUsesWith(Replacement);
182199
return;
183200
}
201+
184202
Value *AdjustedCast = nullptr;
185203
if (Cast->getOpcode() == Instruction::ZExt)
186204
AdjustedCast = Builder.CreateZExtOrTrunc(Replacement, Cast->getType());
@@ -200,12 +218,17 @@ static void fixI8UseChain(Instruction &I,
200218
BasePtr = ReplacedValues[BasePtr];
201219

202220
Type *ElementType = BasePtr->getType();
221+
203222
if (auto *AI = dyn_cast<AllocaInst>(BasePtr))
204223
ElementType = AI->getAllocatedType();
205224
if (auto *GV = dyn_cast<GlobalVariable>(BasePtr))
206225
ElementType = GV->getValueType();
226+
227+
Type *GEPType = ElementType;
207228
if (auto *ArrTy = dyn_cast<ArrayType>(ElementType))
208229
ElementType = ArrTy->getArrayElementType();
230+
else
231+
GEPType = ArrayType::get(ElementType, 1); // its a scalar
209232

210233
ConstantInt *Offset = dyn_cast<ConstantInt>(GEP->getOperand(1));
211234
// Note: i8 to i32 offset conversion without emitting IR requires constant
@@ -217,9 +240,9 @@ static void fixI8UseChain(Instruction &I,
217240
uint32_t ElemSize = GEP->getDataLayout().getTypeAllocSize(ElementType);
218241
assert(ElemSize > 0 && "ElementSize must be set");
219242
uint32_t Index = ByteOffset / ElemSize;
220-
Value *NewGEP =
221-
Builder.CreateGEP(ElementType, BasePtr, Builder.getInt32(Index),
222-
GEP->getName(), GEP->getNoWrapFlags());
243+
Value *NewGEP = Builder.CreateGEP(
244+
GEPType, BasePtr, {Builder.getInt32(0), Builder.getInt32(Index)},
245+
GEP->getName(), GEP->getNoWrapFlags());
223246
ReplacedValues[GEP] = NewGEP;
224247
GEP->replaceAllUsesWith(NewGEP);
225248
ToRemove.push_back(GEP);

llvm/test/CodeGen/DirectX/legalize-i8.ll

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,8 @@ define i32 @all_imm() {
110110
define i32 @scalar_i8_geps() {
111111
; CHECK-LABEL: define i32 @scalar_i8_geps(
112112
; CHECK-NEXT: [[ALLOCA:%.*]] = alloca i32, align 4
113-
; CHECK-NEXT: [[GEP:%.*]] = getelementptr inbounds nuw i32, ptr [[ALLOCA]], i32 0
114-
; CHECK-NEXT: [[LOAD:%.*]] = load i32, ptr [[GEP]], align 4
113+
; CHECK-NEXT: [[GEP:%.*]] = getelementptr inbounds nuw [1 x i32], ptr [[ALLOCA]], i32 0, i32 0
114+
; CHECK: [[LOAD:%.*]] = load i32, ptr [[GEP]], align 4
115115
; CHECK-NEXT: ret i32 [[LOAD]]
116116
%1 = alloca i8, align 4
117117
%2 = getelementptr inbounds nuw i8, ptr %1, i32 0
@@ -123,8 +123,8 @@ define i32 @scalar_i8_geps() {
123123
define i32 @i8_geps_index0() {
124124
; CHECK-LABEL: define i32 @i8_geps_index0(
125125
; CHECK-NEXT: [[ALLOCA:%.*]] = alloca [2 x i32], align 8
126-
; CHECK-NEXT: [[GEP:%.*]] = getelementptr inbounds nuw i32, ptr [[ALLOCA]], i32 0
127-
; CHECK-NEXT: [[LOAD:%.*]] = load i32, ptr [[GEP]], align 4
126+
; CHECK-NEXT: [[GEP:%.*]] = getelementptr inbounds nuw [2 x i32], ptr [[ALLOCA]], i32 0, i32 0
127+
; CHECK: [[LOAD:%.*]] = load i32, ptr [[GEP]], align 4
128128
; CHECK-NEXT: ret i32 [[LOAD]]
129129
%1 = alloca [2 x i32], align 8
130130
%2 = getelementptr inbounds nuw i8, ptr %1, i32 0
@@ -136,8 +136,8 @@ define i32 @i8_geps_index0() {
136136
define i32 @i8_geps_index1() {
137137
; CHECK-LABEL: define i32 @i8_geps_index1(
138138
; CHECK-NEXT: [[ALLOCA:%.*]] = alloca [2 x i32], align 8
139-
; CHECK-NEXT: [[GEP:%.*]] = getelementptr inbounds nuw i32, ptr [[ALLOCA]], i32 1
140-
; CHECK-NEXT: [[LOAD:%.*]] = load i32, ptr [[GEP]]
139+
; CHECK-NEXT: [[GEP:%.*]] = getelementptr inbounds nuw [2 x i32], ptr [[ALLOCA]], i32 0, i32 1
140+
; CHECK: [[LOAD:%.*]] = load i32, ptr [[GEP]], align 4
141141
; CHECK-NEXT: ret i32 [[LOAD]]
142142
%1 = alloca [2 x i32], align 8
143143
%2 = getelementptr inbounds nuw i8, ptr %1, i32 4
@@ -149,9 +149,9 @@ define i32 @i8_geps_index1() {
149149
define i32 @i8_gep_store() {
150150
; CHECK-LABEL: define i32 @i8_gep_store(
151151
; CHECK-NEXT: [[ALLOCA:%.*]] = alloca [2 x i32], align 8
152-
; CHECK-NEXT: [[GEP:%.*]] = getelementptr inbounds nuw i32, ptr [[ALLOCA]], i32 1
152+
; CHECK-NEXT: [[GEP:%.*]] = getelementptr inbounds nuw [2 x i32], ptr [[ALLOCA]], i32 0, i32 1
153153
; CHECK-NEXT: store i32 1, ptr [[GEP]], align 4
154-
; CHECK-NEXT: [[LOAD:%.*]] = load i32, ptr [[GEP]]
154+
; CHECK: [[LOAD:%.*]] = load i32, ptr [[GEP]], align 4
155155
; CHECK-NEXT: ret i32 [[LOAD]]
156156
%1 = alloca [2 x i32], align 8
157157
%2 = getelementptr inbounds nuw i8, ptr %1, i32 4
@@ -164,7 +164,7 @@ define i32 @i8_gep_store() {
164164
@g = local_unnamed_addr addrspace(3) global [2 x float] zeroinitializer, align 4
165165
define float @i8_gep_global_index() {
166166
; CHECK-LABEL: define float @i8_gep_global_index(
167-
; CHECK-NEXT: [[LOAD:%.*]] = load float, ptr addrspace(3) getelementptr inbounds nuw (float, ptr addrspace(3) @g, i32 1), align 4
167+
; CHECK-NEXT: [[LOAD:%.*]] = load float, ptr addrspace(3) getelementptr inbounds nuw ([2 x float], ptr addrspace(3) @g, i32 0, i32 1), align 4
168168
; CHECK-NEXT: ret float [[LOAD]]
169169
%1 = getelementptr inbounds nuw i8, ptr addrspace(3) @g, i32 4
170170
%2 = load float, ptr addrspace(3) %1, align 4
@@ -173,7 +173,7 @@ define float @i8_gep_global_index() {
173173

174174
define float @i8_gep_global_constexpr() {
175175
; CHECK-LABEL: define float @i8_gep_global_constexpr(
176-
; CHECK-NEXT: [[LOAD:%.*]] = load float, ptr addrspace(3) getelementptr inbounds nuw (float, ptr addrspace(3) @g, i32 1), align 4
176+
; CHECK-NEXT: [[LOAD:%.*]] = load float, ptr addrspace(3) getelementptr inbounds nuw ([2 x float], ptr addrspace(3) @g, i32 0, i32 1), align 4
177177
; CHECK-NEXT: ret float [[LOAD]]
178178
%1 = load float, ptr addrspace(3) getelementptr inbounds nuw (i8, ptr addrspace(3) @g, i32 4), align 4
179179
ret float %1

0 commit comments

Comments
 (0)