Skip to content

Commit 1a09803

Browse files
committed
Allow flattening GEPs for Global Variables not in the GlobalMap
1 parent e7b9d21 commit 1a09803

File tree

4 files changed

+30
-41
lines changed

4 files changed

+30
-41
lines changed

llvm/lib/Target/DirectX/DXILFlattenArrays.cpp

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -215,18 +215,6 @@ bool DXILFlattenArraysVisitor::visitGetElementPtrInst(GetElementPtrInst &GEP) {
215215
if (GEPChainInfoMap.contains(cast<GEPOperator>(&GEP)))
216216
return false;
217217

218-
// Construct GEPInfo for this GEP
219-
GEPInfo Info;
220-
221-
// Obtain the variable and constant byte offsets computed by this GEP
222-
const DataLayout &DL = GEP.getDataLayout();
223-
unsigned BitWidth = DL.getIndexTypeSizeInBits(GEP.getType());
224-
Info.ConstantOffset = {BitWidth, 0};
225-
bool Success = GEP.collectOffset(DL, BitWidth, Info.VariableOffsets,
226-
Info.ConstantOffset);
227-
(void)Success;
228-
assert(Success && "Failed to collect offsets for GEP");
229-
230218
Value *PtrOperand = GEP.getPointerOperand();
231219

232220
// Replace a GEP ConstantExpr pointer operand with a GEP instruction so that
@@ -243,7 +231,7 @@ bool DXILFlattenArraysVisitor::visitGetElementPtrInst(GetElementPtrInst &GEP) {
243231
Builder.CreateGEP(GEP.getSourceElementType(), OldGEPI, Indices,
244232
GEP.getName(), GEP.getNoWrapFlags());
245233
assert(isa<GetElementPtrInst>(NewGEP) &&
246-
"Expected newly-created GEP to not be a ConstantExpr");
234+
"Expected newly-created GEP to be an instruction");
247235
GetElementPtrInst *NewGEPI = cast<GetElementPtrInst>(NewGEP);
248236

249237
GEP.replaceAllUsesWith(NewGEPI);
@@ -253,6 +241,18 @@ bool DXILFlattenArraysVisitor::visitGetElementPtrInst(GetElementPtrInst &GEP) {
253241
return true;
254242
}
255243

244+
// Construct GEPInfo for this GEP
245+
GEPInfo Info;
246+
247+
// Obtain the variable and constant byte offsets computed by this GEP
248+
const DataLayout &DL = GEP.getDataLayout();
249+
unsigned BitWidth = DL.getIndexTypeSizeInBits(GEP.getType());
250+
Info.ConstantOffset = {BitWidth, 0};
251+
bool Success = GEP.collectOffset(DL, BitWidth, Info.VariableOffsets,
252+
Info.ConstantOffset);
253+
(void)Success;
254+
assert(Success && "Failed to collect offsets for GEP");
255+
256256
// If there is a parent GEP, inherit the root array type and pointer, and
257257
// merge the byte offsets. Otherwise, this GEP is itself the root of a GEP
258258
// chain and we need to deterine the root array type
@@ -270,15 +270,13 @@ bool DXILFlattenArraysVisitor::visitGetElementPtrInst(GetElementPtrInst &GEP) {
270270

271271
// We should try to determine the type of the root from the pointer rather
272272
// than the GEP's source element type because this could be a scalar GEP
273-
// into a multidimensional array-typed pointer from an Alloca or Global
274-
// Variable.
273+
// into an array-typed pointer from an Alloca or Global Variable.
275274
Type *RootTy = GEP.getSourceElementType();
276275
if (auto *GlobalVar = dyn_cast<GlobalVariable>(PtrOperand)) {
277-
if (!GlobalMap.contains(GlobalVar))
278-
return false;
279-
GlobalVariable *NewGlobal = GlobalMap[GlobalVar];
280-
Info.RootPointerOperand = NewGlobal;
281-
RootTy = NewGlobal->getValueType();
276+
if (GlobalMap.contains(GlobalVar))
277+
GlobalVar = GlobalMap[GlobalVar];
278+
Info.RootPointerOperand = GlobalVar;
279+
RootTy = GlobalVar->getValueType();
282280
} else if (auto *Alloca = dyn_cast<AllocaInst>(PtrOperand)) {
283281
RootTy = Alloca->getAllocatedType();
284282
}
@@ -341,7 +339,8 @@ bool DXILFlattenArraysVisitor::visitGetElementPtrInst(GetElementPtrInst &GEP) {
341339
}
342340

343341
// This GEP is potentially dead at the end of the pass since it may not have
344-
// any users anymore after GEP chains have been collapsed.
342+
// any users anymore after GEP chains have been collapsed. We retain store
343+
// GEPInfo for GEPs down the chain to use to compute their indices.
345344
GEPChainInfoMap.insert({cast<GEPOperator>(&GEP), std::move(Info)});
346345
PotentiallyDeadInstrs.emplace_back(&GEP);
347346
return false;

llvm/test/CodeGen/DirectX/flatten-array.ll

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,7 @@ define void @gep_4d_test () {
123123
@b = internal global [2 x [3 x [4 x i32]]] zeroinitializer, align 16
124124

125125
define void @global_gep_load() {
126-
; CHECK: [[GEP_PTR:%.*]] = getelementptr inbounds [24 x i32], ptr @a.1dim, i32 0, i32 6
127-
; CHECK-NEXT: load i32, ptr [[GEP_PTR]], align 4
126+
; CHECK: {{.*}} = load i32, ptr getelementptr inbounds ([24 x i32], ptr @a.1dim, i32 0, i32 6), align 4
128127
; CHECK-NEXT: ret void
129128
%1 = getelementptr inbounds [2 x [3 x [4 x i32]]], [2 x [3 x [4 x i32]]]* @a, i32 0, i32 0
130129
%2 = getelementptr inbounds [3 x [4 x i32]], [3 x [4 x i32]]* %1, i32 0, i32 1
@@ -177,8 +176,7 @@ define void @global_incomplete_gep_chain(i32 %row, i32 %col) {
177176
}
178177

179178
define void @global_gep_store() {
180-
; CHECK: [[GEP_PTR:%.*]] = getelementptr inbounds [24 x i32], ptr @b.1dim, i32 0, i32 13
181-
; CHECK-NEXT: store i32 1, ptr [[GEP_PTR]], align 4
179+
; CHECK: store i32 1, ptr getelementptr inbounds ([24 x i32], ptr @b.1dim, i32 0, i32 13), align 4
182180
; CHECK-NEXT: ret void
183181
%1 = getelementptr inbounds [2 x [3 x [4 x i32]]], [2 x [3 x [4 x i32]]]* @b, i32 0, i32 1
184182
%2 = getelementptr inbounds [3 x [4 x i32]], [3 x [4 x i32]]* %1, i32 0, i32 0

llvm/test/CodeGen/DirectX/flatten-bug-117273.ll

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,8 @@
88
define internal void @main() {
99
; CHECK-LABEL: define internal void @main() {
1010
; CHECK-NEXT: [[ENTRY:.*:]]
11-
; CHECK-NEXT: [[TMP0:%.*]] = getelementptr [6 x float], ptr @ZerroInitArr.1dim, i32 0, i32 3
12-
; CHECK-NEXT: [[DOTI0:%.*]] = load float, ptr [[TMP0]], align 16
13-
; CHECK-NEXT: [[TMP1:%.*]] = getelementptr [6 x float], ptr @ZerroInitArr.1dim, i32 0, i32 6
14-
; CHECK-NEXT: [[DOTI03:%.*]] = load float, ptr [[TMP1]], align 16
11+
; CHECK-NEXT: [[DOTI0:%.*]] = load float, ptr getelementptr ([6 x float], ptr @ZerroInitArr.1dim, i32 0, i32 3), align 16
12+
; CHECK-NEXT: [[DOTI03:%.*]] = load float, ptr getelementptr ([6 x float], ptr @ZerroInitArr.1dim, i32 0, i32 6), align 16
1513
; CHECK-NEXT: ret void
1614
;
1715
entry:

llvm/test/CodeGen/DirectX/scalar-bug-117273.ll

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,12 @@
88
define internal void @main() #1 {
99
; CHECK-LABEL: define internal void @main() {
1010
; CHECK-NEXT: [[ENTRY:.*:]]
11-
; CHECK-NEXT: [[TMP0:%.*]] = getelementptr inbounds [24 x float], ptr @StaticArr.scalarized.1dim, i32 0, i32 3
12-
; CHECK-NEXT: [[DOTI0:%.*]] = load float, ptr [[TMP0]], align 16
13-
; CHECK-NEXT: [[DOTI1:%.*]] = getelementptr float, ptr [[TMP0]], i32 1
14-
; CHECK-NEXT: [[DOTI11:%.*]] = load float, ptr [[DOTI1]], align 4
15-
; CHECK-NEXT: [[DOTI2:%.*]] = getelementptr float, ptr [[TMP0]], i32 2
16-
; CHECK-NEXT: [[DOTI22:%.*]] = load float, ptr [[DOTI2]], align 8
17-
; CHECK-NEXT: [[TMP1:%.*]] = getelementptr inbounds [24 x float], ptr @StaticArr.scalarized.1dim, i32 0, i32 6
18-
; CHECK-NEXT: [[DOTI03:%.*]] = load float, ptr [[TMP1]], align 16
19-
; CHECK-NEXT: [[DOTI14:%.*]] = getelementptr float, ptr [[TMP1]], i32 1
20-
; CHECK-NEXT: [[DOTI15:%.*]] = load float, ptr [[DOTI14]], align 4
21-
; CHECK-NEXT: [[DOTI26:%.*]] = getelementptr float, ptr [[TMP1]], i32 2
22-
; CHECK-NEXT: [[DOTI27:%.*]] = load float, ptr [[DOTI26]], align 8
11+
; CHECK-NEXT: [[DOTI0:%.*]] = load float, ptr getelementptr inbounds ([24 x float], ptr @StaticArr.scalarized.1dim, i32 0, i32 3), align 16
12+
; CHECK-NEXT: [[DOTI11:%.*]] = load float, ptr getelementptr (float, ptr getelementptr inbounds ([24 x float], ptr @StaticArr.scalarized.1dim, i32 0, i32 3), i32 1), align 4
13+
; CHECK-NEXT: [[DOTI22:%.*]] = load float, ptr getelementptr (float, ptr getelementptr inbounds ([24 x float], ptr @StaticArr.scalarized.1dim, i32 0, i32 3), i32 2), align 8
14+
; CHECK-NEXT: [[DOTI03:%.*]] = load float, ptr getelementptr inbounds ([24 x float], ptr @StaticArr.scalarized.1dim, i32 0, i32 6), align 16
15+
; CHECK-NEXT: [[DOTI15:%.*]] = load float, ptr getelementptr (float, ptr getelementptr inbounds ([24 x float], ptr @StaticArr.scalarized.1dim, i32 0, i32 6), i32 1), align 4
16+
; CHECK-NEXT: [[DOTI27:%.*]] = load float, ptr getelementptr (float, ptr getelementptr inbounds ([24 x float], ptr @StaticArr.scalarized.1dim, i32 0, i32 6), i32 2), align 8
2317
; CHECK-NEXT: ret void
2418
;
2519
entry:

0 commit comments

Comments
 (0)