Skip to content

Commit 7a4f555

Browse files
committed
[DirectX] Bug fix for Data Scalarization crash
1 parent 0aef005 commit 7a4f555

File tree

4 files changed

+77
-20
lines changed

4 files changed

+77
-20
lines changed

llvm/lib/Target/DirectX/DXILDataScalarization.cpp

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -67,28 +67,17 @@ class DataScalarizerVisitor : public InstVisitor<DataScalarizerVisitor, bool> {
6767
private:
6868
GlobalVariable *lookupReplacementGlobal(Value *CurrOperand);
6969
DenseMap<GlobalVariable *, GlobalVariable *> GlobalMap;
70-
SmallVector<WeakTrackingVH, 32> PotentiallyDeadInstrs;
71-
bool finish();
7270
};
7371

7472
bool DataScalarizerVisitor::visit(Function &F) {
7573
assert(!GlobalMap.empty());
76-
ReversePostOrderTraversal<BasicBlock *> RPOT(&F.getEntryBlock());
77-
for (BasicBlock *BB : RPOT) {
78-
for (BasicBlock::iterator II = BB->begin(), IE = BB->end(); II != IE;) {
79-
Instruction *I = &*II;
80-
bool Done = InstVisitor::visit(I);
81-
++II;
82-
if (Done && I->getType()->isVoidTy())
83-
I->eraseFromParent();
84-
}
74+
bool MadeChange = false;
75+
ReversePostOrderTraversal<Function *> RPOT(&F);
76+
for (BasicBlock *BB : make_early_inc_range(RPOT)) {
77+
for (Instruction &I : make_early_inc_range(*BB))
78+
MadeChange |= InstVisitor::visit(I);
8579
}
86-
return finish();
87-
}
88-
89-
bool DataScalarizerVisitor::finish() {
90-
RecursivelyDeleteTriviallyDeadInstructionsPermissive(PotentiallyDeadInstrs);
91-
return true;
80+
return MadeChange;
9281
}
9382

9483
GlobalVariable *
@@ -140,7 +129,7 @@ bool DataScalarizerVisitor::visitGetElementPtrInst(GetElementPtrInst &GEPI) {
140129
Builder.CreateGEP(NewGlobal->getValueType(), NewGlobal, Indices);
141130

142131
GEPI.replaceAllUsesWith(NewGEP);
143-
PotentiallyDeadInstrs.emplace_back(&GEPI);
132+
GEPI.eraseFromParent();
144133
}
145134
return true;
146135
}
@@ -252,8 +241,7 @@ static bool findAndReplaceVectors(Module &M) {
252241
/*RemoveDeadConstants=*/false,
253242
/*IncludeSelf=*/true);
254243
}
255-
if (isa<Instruction>(U)) {
256-
Instruction *Inst = cast<Instruction>(U);
244+
if (Instruction *Inst = dyn_cast<Instruction>(U)) {
257245
Function *F = Inst->getFunction();
258246
if (F)
259247
Impl.visit(*F);

llvm/lib/Target/DirectX/DXILFlattenArrays.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,18 @@ static void collectElements(Constant *Init,
321321
Elements.push_back(Init);
322322
return;
323323
}
324+
auto *LLVMArrayType = dyn_cast<ArrayType>(Init->getType());
325+
if (isa<ConstantAggregateZero>(Init)) {
326+
for (unsigned I = 0; I < LLVMArrayType->getNumElements(); ++I)
327+
Elements.push_back(
328+
Constant::getNullValue(LLVMArrayType->getElementType()));
329+
return;
330+
}
331+
if (isa<UndefValue>(Init)) {
332+
for (unsigned I = 0; I < LLVMArrayType->getNumElements(); ++I)
333+
Elements.push_back(UndefValue::get(LLVMArrayType->getElementType()));
334+
return;
335+
}
324336

325337
// Recursive case: Process each element in the array.
326338
if (auto *ArrayConstant = dyn_cast<ConstantArray>(Init)) {
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
2+
; RUN: opt -S -passes='dxil-flatten-arrays,dxil-op-lower' -mtriple=dxil-pc-shadermodel6.3-library %s | FileCheck %s
3+
4+
5+
@_ZL7Palette = internal constant [2 x [3 x float]] [[3 x float] zeroinitializer, [3 x float] undef], align 16
6+
7+
; CHECK: @_ZL7Palette.1dim = internal constant [6 x float] [float 0.000000e+00, float 0.000000e+00, float 0.000000e+00, float undef, float undef, float undef], align 16
8+
9+
define internal void @_Z4mainDv3_j(<3 x i32> noundef %DID) {
10+
; CHECK-LABEL: define internal void @_Z4mainDv3_j(
11+
; CHECK-SAME: <3 x i32> noundef [[DID:%.*]]) {
12+
; CHECK-NEXT: [[ENTRY:.*:]]
13+
; CHECK-NEXT: [[TMP0:%.*]] = getelementptr [24 x float], ptr @_ZL7Palette.1dim, i32 1
14+
; CHECK-NEXT: [[DOTI0:%.*]] = load float, ptr [[TMP0]], align 16
15+
; CHECK-NEXT: [[TMP1:%.*]] = getelementptr [24 x float], ptr @_ZL7Palette.1dim, i32 2
16+
; CHECK-NEXT: [[DOTI03:%.*]] = load float, ptr [[TMP1]], align 16
17+
; CHECK-NEXT: ret void
18+
;
19+
entry:
20+
%0 = getelementptr [8 x [3 x float]], ptr @_ZL7Palette, i32 0, i32 1
21+
%.i0 = load float, ptr %0, align 16
22+
%1 = getelementptr [8 x [3 x float]], ptr @_ZL7Palette, i32 0, i32 2
23+
%.i03 = load float, ptr %1, align 16
24+
ret void
25+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
2+
; RUN: opt -S -passes='dxil-data-scalarization,dxil-flatten-arrays,function(scalarizer<load-store>),dxil-op-lower' -mtriple=dxil-pc-shadermodel6.3-library %s | FileCheck %s
3+
4+
5+
@_ZL7Palette = internal constant [8 x <3 x float>] [<3 x float> zeroinitializer, <3 x float> splat (float 5.000000e-01), <3 x float> <float 1.000000e+00, float 5.000000e-01, float 5.000000e-01>, <3 x float> <float 5.000000e-01, float 1.000000e+00, float 5.000000e-01>, <3 x float> <float 5.000000e-01, float 5.000000e-01, float 1.000000e+00>, <3 x float> <float 5.000000e-01, float 1.000000e+00, float 1.000000e+00>, <3 x float> <float 1.000000e+00, float 5.000000e-01, float 1.000000e+00>, <3 x float> <float 1.000000e+00, float 1.000000e+00, float 5.000000e-01>], align 16
6+
7+
; Function Attrs: alwaysinline convergent mustprogress norecurse nounwind
8+
define internal void @_Z4mainDv3_j(<3 x i32> noundef %DID) #1 {
9+
; CHECK-LABEL: define internal void @_Z4mainDv3_j(
10+
; CHECK-SAME: <3 x i32> noundef [[DID:%.*]]) {
11+
; CHECK-NEXT: [[ENTRY:.*:]]
12+
; CHECK-NEXT: [[TMP0:%.*]] = getelementptr [24 x float], ptr @_ZL7Palette.scalarized.1dim, i32 1
13+
; CHECK-NEXT: [[DOTI0:%.*]] = load float, ptr [[TMP0]], align 16
14+
; CHECK-NEXT: [[DOTI1:%.*]] = getelementptr float, ptr [[TMP0]], i32 1
15+
; CHECK-NEXT: [[DOTI11:%.*]] = load float, ptr [[DOTI1]], align 4
16+
; CHECK-NEXT: [[DOTI2:%.*]] = getelementptr float, ptr [[TMP0]], i32 2
17+
; CHECK-NEXT: [[DOTI22:%.*]] = load float, ptr [[DOTI2]], align 8
18+
; CHECK-NEXT: [[TMP1:%.*]] = getelementptr [24 x float], ptr @_ZL7Palette.scalarized.1dim, i32 2
19+
; CHECK-NEXT: [[DOTI03:%.*]] = load float, ptr [[TMP1]], align 16
20+
; CHECK-NEXT: [[DOTI14:%.*]] = getelementptr float, ptr [[TMP1]], i32 1
21+
; CHECK-NEXT: [[DOTI15:%.*]] = load float, ptr [[DOTI14]], align 4
22+
; CHECK-NEXT: [[DOTI26:%.*]] = getelementptr float, ptr [[TMP1]], i32 2
23+
; CHECK-NEXT: [[DOTI27:%.*]] = load float, ptr [[DOTI26]], align 8
24+
; CHECK-NEXT: ret void
25+
;
26+
entry:
27+
%arrayidx = getelementptr inbounds [8 x <3 x float>], ptr @_ZL7Palette, i32 0, i32 1
28+
%2 = load <3 x float>, ptr %arrayidx, align 16
29+
%arrayidx2 = getelementptr inbounds [8 x <3 x float>], ptr @_ZL7Palette, i32 0, i32 2
30+
%3 = load <3 x float>, ptr %arrayidx2, align 16
31+
ret void
32+
}

0 commit comments

Comments
 (0)