Skip to content

Commit 74b05e0

Browse files
committed
Add support for GEPs with global ptr operands. Add support for loads with constexpr GEPs
1 parent 6f4702a commit 74b05e0

File tree

2 files changed

+47
-3
lines changed

2 files changed

+47
-3
lines changed

llvm/lib/Target/DirectX/DXILLegalizePass.cpp

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,8 @@ static void fixI8UseChain(Instruction &I,
8787
return;
8888
}
8989

90-
if (auto *Load = dyn_cast<LoadInst>(&I)) {
91-
if (!I.getType()->isIntegerTy(8))
92-
return;
90+
if (auto *Load = dyn_cast<LoadInst>(&I);
91+
Load && I.getType()->isIntegerTy(8)) {
9392
SmallVector<Value *> NewOperands;
9493
ProcessOperands(NewOperands);
9594
Type *ElementType = NewOperands[0]->getType();
@@ -103,6 +102,31 @@ static void fixI8UseChain(Instruction &I,
103102
return;
104103
}
105104

105+
if (auto *Load = dyn_cast<LoadInst>(&I);
106+
Load && isa<ConstantExpr>(Load->getPointerOperand())) {
107+
auto *CE = dyn_cast<ConstantExpr>(Load->getPointerOperand());
108+
if (!(CE->getOpcode() == Instruction::GetElementPtr))
109+
return;
110+
auto *GEP = dyn_cast<GEPOperator>(CE);
111+
if (!GEP->getSourceElementType()->isIntegerTy(8))
112+
return;
113+
114+
Type *ElementType = Load->getType();
115+
ConstantInt *Offset = dyn_cast<ConstantInt>(GEP->getOperand(1));
116+
uint32_t ByteOffset = Offset->getZExtValue();
117+
uint32_t ElemSize = Load->getDataLayout().getTypeAllocSize(ElementType);
118+
uint32_t Index = ByteOffset / ElemSize;
119+
Value *NewGEP = Builder.CreateGEP(ElementType, GEP->getPointerOperand(),
120+
Builder.getInt32(Index), GEP->getName(),
121+
GEP->getNoWrapFlags());
122+
123+
LoadInst *NewLoad = Builder.CreateLoad(ElementType, NewGEP);
124+
ReplacedValues[Load] = NewLoad;
125+
Load->replaceAllUsesWith(NewLoad);
126+
ToRemove.push_back(Load);
127+
return;
128+
}
129+
106130
if (auto *BO = dyn_cast<BinaryOperator>(&I)) {
107131
if (!I.getType()->isIntegerTy(8))
108132
return;
@@ -178,6 +202,8 @@ static void fixI8UseChain(Instruction &I,
178202
Type *ElementType = BasePtr->getType();
179203
if (auto *AI = dyn_cast<AllocaInst>(BasePtr))
180204
ElementType = AI->getAllocatedType();
205+
if (auto *GV = dyn_cast<GlobalVariable>(BasePtr))
206+
ElementType = GV->getValueType();
181207
if (auto *ArrTy = dyn_cast<ArrayType>(ElementType))
182208
ElementType = ArrTy->getArrayElementType();
183209

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,3 +160,21 @@ define i32 @i8_gep_store() {
160160
%4 = sext i8 %3 to i32
161161
ret i32 %4
162162
}
163+
164+
@g = local_unnamed_addr addrspace(3) global [2 x float] zeroinitializer, align 4
165+
define float @i8_gep_global_index() {
166+
; 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
168+
; CHECK-NEXT: ret float [[LOAD]]
169+
%1 = getelementptr inbounds nuw i8, ptr addrspace(3) @g, i32 4
170+
%2 = load float, ptr addrspace(3) %1, align 4
171+
ret float %2
172+
}
173+
174+
define float @i8_gep_global_constexpr() {
175+
; 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
177+
; CHECK-NEXT: ret float [[LOAD]]
178+
%1 = load float, ptr addrspace(3) getelementptr inbounds nuw (i8, ptr addrspace(3) @g, i32 4), align 4
179+
ret float %1
180+
}

0 commit comments

Comments
 (0)