Skip to content

Commit c73c19c

Browse files
authored
[DirectX] Support ConstExpr GEPs (#150082)
- Fixes #150050 - Address the issue of many nested geps - Check for ConstantExpr GEP if we see it check if it needs a global replacement with a new type. Create the new constExpr Gep and set the pointer operand to it. Finally cleanup and remove the old nested geps.
1 parent 2346968 commit c73c19c

File tree

2 files changed

+109
-17
lines changed

2 files changed

+109
-17
lines changed

llvm/lib/Target/DirectX/DXILDataScalarization.cpp

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -301,41 +301,53 @@ bool DataScalarizerVisitor::visitExtractElementInst(ExtractElementInst &EEI) {
301301
}
302302

303303
bool DataScalarizerVisitor::visitGetElementPtrInst(GetElementPtrInst &GEPI) {
304-
Value *PtrOperand = GEPI.getPointerOperand();
305-
Type *OrigGEPType = GEPI.getSourceElementType();
306-
Type *NewGEPType = OrigGEPType;
304+
GEPOperator *GOp = cast<GEPOperator>(&GEPI);
305+
Value *PtrOperand = GOp->getPointerOperand();
306+
Type *NewGEPType = GOp->getSourceElementType();
307307
bool NeedsTransform = false;
308308

309+
// Unwrap GEP ConstantExprs to find the base operand and element type
310+
while (auto *CE = dyn_cast<ConstantExpr>(PtrOperand)) {
311+
if (auto *GEPCE = dyn_cast<GEPOperator>(CE)) {
312+
GOp = GEPCE;
313+
PtrOperand = GEPCE->getPointerOperand();
314+
NewGEPType = GEPCE->getSourceElementType();
315+
} else
316+
break;
317+
}
318+
309319
if (GlobalVariable *NewGlobal = lookupReplacementGlobal(PtrOperand)) {
310320
NewGEPType = NewGlobal->getValueType();
311321
PtrOperand = NewGlobal;
312322
NeedsTransform = true;
313323
} else if (AllocaInst *Alloca = dyn_cast<AllocaInst>(PtrOperand)) {
314324
Type *AllocatedType = Alloca->getAllocatedType();
315-
// Only transform if the allocated type is an array
316-
if (AllocatedType != OrigGEPType && isa<ArrayType>(AllocatedType)) {
325+
if (isa<ArrayType>(AllocatedType) &&
326+
AllocatedType != GOp->getResultElementType()) {
317327
NewGEPType = AllocatedType;
318328
NeedsTransform = true;
319329
}
320330
}
321331

322-
// Scalar geps should remain scalars geps. The dxil-flatten-arrays pass will
323-
// convert these scalar geps into flattened array geps
324-
if (!isa<ArrayType>(OrigGEPType))
325-
NewGEPType = OrigGEPType;
326-
327-
// Note: We bail if this isn't a gep touched via alloca or global
328-
// transformations
329332
if (!NeedsTransform)
330333
return false;
331334

332-
IRBuilder<> Builder(&GEPI);
333-
SmallVector<Value *, MaxVecSize> Indices(GEPI.indices());
335+
// Keep scalar GEPs scalar; dxil-flatten-arrays will do flattening later
336+
if (!isa<ArrayType>(GOp->getSourceElementType()))
337+
NewGEPType = GOp->getSourceElementType();
334338

339+
IRBuilder<> Builder(&GEPI);
340+
SmallVector<Value *, MaxVecSize> Indices(GOp->indices());
335341
Value *NewGEP = Builder.CreateGEP(NewGEPType, PtrOperand, Indices,
336-
GEPI.getName(), GEPI.getNoWrapFlags());
337-
GEPI.replaceAllUsesWith(NewGEP);
338-
GEPI.eraseFromParent();
342+
GOp->getName(), GOp->getNoWrapFlags());
343+
344+
GOp->replaceAllUsesWith(NewGEP);
345+
346+
if (auto *CE = dyn_cast<ConstantExpr>(GOp))
347+
CE->destroyConstant();
348+
else if (auto *OldGEPI = dyn_cast<GetElementPtrInst>(GOp))
349+
OldGEPI->eraseFromParent();
350+
339351
return true;
340352
}
341353

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
; RUN: opt -S -passes='dxil-data-scalarization' -mtriple=dxil-pc-shadermodel6.4-library %s | FileCheck %s --check-prefixes=SCHECK,CHECK
2+
; RUN: opt -S -passes='dxil-data-scalarization,function(scalarizer<load-store>),dxil-flatten-arrays' -mtriple=dxil-pc-shadermodel6.4-library %s | FileCheck %s --check-prefixes=FCHECK,CHECK
3+
4+
@aTile = hidden addrspace(3) global [10 x [10 x <4 x i32>]] zeroinitializer, align 16
5+
@bTile = hidden addrspace(3) global [10 x [10 x i32]] zeroinitializer, align 16
6+
@cTile = internal global [2 x [2 x <2 x i32>]] zeroinitializer, align 16
7+
@dTile = internal global [2 x [2 x [2 x <2 x i32>]]] zeroinitializer, align 16
8+
9+
define void @CSMain() {
10+
; CHECK-LABEL: define void @CSMain() {
11+
; CHECK-NEXT: [[ENTRY:.*:]]
12+
; CHECK-NEXT: [[AFRAGPACKED_I_SCALARIZE:%.*]] = alloca [4 x i32], align 16
13+
;
14+
; SCHECK-NEXT: [[TMP0:%.*]] = getelementptr inbounds [10 x <4 x i32>], ptr addrspace(3) getelementptr inbounds ([10 x [10 x [4 x i32]]], ptr addrspace(3) @aTile.scalarized, i32 0, i32 1), i32 0, i32 2
15+
; SCHECK-NEXT: [[TMP1:%.*]] = load <4 x i32>, ptr addrspace(3) [[TMP0]], align 16
16+
; SCHECK-NEXT: store <4 x i32> [[TMP1]], ptr [[AFRAGPACKED_I_SCALARIZE]], align 16
17+
;
18+
; FCHECK-NEXT: [[AFRAGPACKED_I_SCALARIZE_I14:%.*]] = getelementptr [4 x i32], ptr [[AFRAGPACKED_I_SCALARIZE]], i32 0, i32 1
19+
; FCHECK-NEXT: [[AFRAGPACKED_I_SCALARIZE_I25:%.*]] = getelementptr [4 x i32], ptr [[AFRAGPACKED_I_SCALARIZE]], i32 0, i32 2
20+
; FCHECK-NEXT: [[AFRAGPACKED_I_SCALARIZE_I36:%.*]] = getelementptr [4 x i32], ptr [[AFRAGPACKED_I_SCALARIZE]], i32 0, i32 3
21+
; FCHECK-NEXT: [[DOTI07:%.*]] = load i32, ptr addrspace(3) getelementptr inbounds ([400 x i32], ptr addrspace(3) @aTile.scalarized.1dim, i32 0, i32 48), align 16
22+
; FCHECK-NEXT: [[DOTI119:%.*]] = load i32, ptr addrspace(3) getelementptr ([400 x i32], ptr addrspace(3) @aTile.scalarized.1dim, i32 0, i32 49), align 4
23+
; FCHECK-NEXT: [[DOTI2211:%.*]] = load i32, ptr addrspace(3) getelementptr ([400 x i32], ptr addrspace(3) @aTile.scalarized.1dim, i32 0, i32 50), align 8
24+
; FCHECK-NEXT: [[DOTI3313:%.*]] = load i32, ptr addrspace(3) getelementptr ([400 x i32], ptr addrspace(3) @aTile.scalarized.1dim, i32 0, i32 51), align 4
25+
; FCHECK-NEXT: store i32 [[DOTI07]], ptr [[AFRAGPACKED_I_SCALARIZE]], align 16
26+
; FCHECK-NEXT: store i32 [[DOTI119]], ptr [[AFRAGPACKED_I_SCALARIZE_I14]], align 4
27+
; FCHECK-NEXT: store i32 [[DOTI2211]], ptr [[AFRAGPACKED_I_SCALARIZE_I25]], align 8
28+
; FCHECK-NEXT: store i32 [[DOTI3313]], ptr [[AFRAGPACKED_I_SCALARIZE_I36]], align 4
29+
;
30+
; CHECK-NEXT: ret void
31+
entry:
32+
%aFragPacked.i = alloca <4 x i32>, align 16
33+
%0 = load <4 x i32>, ptr addrspace(3) getelementptr inbounds ([10 x <4 x i32>], ptr addrspace(3) getelementptr inbounds ([10 x [10 x <4 x i32>]], ptr addrspace(3) @aTile, i32 0, i32 1), i32 0, i32 2), align 16
34+
store <4 x i32> %0, ptr %aFragPacked.i, align 16
35+
ret void
36+
}
37+
38+
define void @Main() {
39+
; CHECK-LABEL: define void @Main() {
40+
; CHECK-NEXT: [[ENTRY:.*:]]
41+
; CHECK-NEXT: [[BFRAGPACKED_I:%.*]] = alloca i32, align 16
42+
;
43+
; SCHECK-NEXT: [[TMP0:%.*]] = getelementptr inbounds [10 x i32], ptr addrspace(3) getelementptr inbounds ([10 x [10 x i32]], ptr addrspace(3) @bTile, i32 0, i32 1), i32 0, i32 1
44+
; SCHECK-NEXT: [[TMP1:%.*]] = load i32, ptr addrspace(3) [[TMP0]], align 16
45+
; SCHECK-NEXT: store i32 [[TMP1]], ptr [[BFRAGPACKED_I]], align 16
46+
;
47+
; FCHECK-NEXT: [[TMP0:%.*]] = load i32, ptr addrspace(3) getelementptr inbounds ([100 x i32], ptr addrspace(3) @bTile.1dim, i32 0, i32 11), align 16
48+
; FCHECK-NEXT: store i32 [[TMP0]], ptr [[BFRAGPACKED_I]], align 16
49+
;
50+
; CHECK-NEXT: ret void
51+
entry:
52+
%bFragPacked.i = alloca i32, align 16
53+
%0 = load i32, ptr addrspace(3) getelementptr inbounds ([10 x i32], ptr addrspace(3) getelementptr inbounds ([10 x [10 x i32]], ptr addrspace(3) @bTile, i32 0, i32 1), i32 0, i32 1), align 16
54+
store i32 %0, ptr %bFragPacked.i, align 16
55+
ret void
56+
}
57+
58+
define void @global_nested_geps_3d() {
59+
; CHECK-LABEL: define void @global_nested_geps_3d() {
60+
; SCHECK-NEXT: [[TMP1:%.*]] = getelementptr inbounds <2 x i32>, ptr getelementptr inbounds ([2 x <2 x i32>], ptr getelementptr inbounds ([2 x [2 x [2 x i32]]], ptr @cTile.scalarized, i32 0, i32 1), i32 0, i32 1), i32 0, i32 1
61+
; SCHECK-NEXT: [[TMP2:%.*]] = load i32, ptr [[TMP1]], align 4
62+
;
63+
; FCHECK-NEXT: [[TMP1:%.*]] = load i32, ptr getelementptr inbounds ([8 x i32], ptr @cTile.scalarized.1dim, i32 0, i32 7), align 4
64+
;
65+
; CHECK-NEXT: ret void
66+
%1 = load i32, i32* getelementptr inbounds (<2 x i32>, <2 x i32>* getelementptr inbounds ([2 x <2 x i32>], [2 x <2 x i32>]* getelementptr inbounds ([2 x [2 x <2 x i32>]], [2 x [2 x <2 x i32>]]* @cTile, i32 0, i32 1), i32 0, i32 1), i32 0, i32 1), align 4
67+
ret void
68+
}
69+
70+
define void @global_nested_geps_4d() {
71+
; CHECK-LABEL: define void @global_nested_geps_4d() {
72+
; SCHECK-NEXT: [[TMP1:%.*]] = getelementptr inbounds <2 x i32>, ptr getelementptr inbounds ([2 x <2 x i32>], ptr getelementptr inbounds ([2 x [2 x <2 x i32>]], ptr getelementptr inbounds ([2 x [2 x [2 x [2 x i32]]]], ptr @dTile.scalarized, i32 0, i32 1), i32 0, i32 1), i32 0, i32 1), i32 0, i32 1
73+
; SCHECK-NEXT: [[TMP2:%.*]] = load i32, ptr [[TMP1]], align 4
74+
;
75+
; FCHECK-NEXT: [[TMP1:%.*]] = load i32, ptr getelementptr inbounds ([16 x i32], ptr @dTile.scalarized.1dim, i32 0, i32 15), align 4
76+
;
77+
; CHECK-NEXT: ret void
78+
%1 = load i32, i32* getelementptr inbounds (<2 x i32>, <2 x i32>* getelementptr inbounds ([2 x <2 x i32>], [2 x <2 x i32>]* getelementptr inbounds ([2 x [2 x <2 x i32>]], [2 x [2 x <2 x i32>]]* getelementptr inbounds ([2 x [2 x [2 x <2 x i32>]]], [2 x [2 x [2 x <2 x i32>]]]* @dTile, i32 0, i32 1), i32 0, i32 1), i32 0, i32 1), i32 0, i32 1), align 4
79+
ret void
80+
}

0 commit comments

Comments
 (0)