forked from microsoft/DirectXShaderCompiler
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHLMatrixBitcastLowerPass.cpp
More file actions
277 lines (253 loc) · 9.54 KB
/
HLMatrixBitcastLowerPass.cpp
File metadata and controls
277 lines (253 loc) · 9.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
///////////////////////////////////////////////////////////////////////////////
// //
// HLMatrixBitcastLowerPass.cpp //
// Copyright (C) Microsoft Corporation. All rights reserved. //
// This file is distributed under the University of Illinois Open Source //
// License. See LICENSE.TXT for details. //
// //
///////////////////////////////////////////////////////////////////////////////
#include "dxc/DXIL/DxilModule.h"
#include "dxc/DXIL/DxilOperations.h"
#include "dxc/DXIL/DxilUtil.h"
#include "dxc/HLSL/DxilGenerationPass.h"
#include "dxc/HLSL/HLMatrixLowerHelper.h"
#include "dxc/HLSL/HLMatrixLowerPass.h"
#include "dxc/HLSL/HLMatrixType.h"
#include "dxc/Support/Global.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/Module.h"
#include "llvm/Pass.h"
#include <unordered_set>
#include <vector>
using namespace llvm;
using namespace hlsl;
using namespace hlsl::HLMatrixLower;
// Matrix Bitcast lower.
// After linking Lower matrix bitcast patterns like:
// %169 = bitcast [72 x float]* %0 to [6 x %class.matrix.float.4.3]*
// %conv.i = fptoui float %164 to i32
// %arrayidx.i = getelementptr inbounds [6 x %class.matrix.float.4.3], [6 x
// %class.matrix.float.4.3]* %169, i32 0, i32 %conv.i %170 = bitcast
// %class.matrix.float.4.3* %arrayidx.i to <12 x float>*
namespace {
// Translate matrix type to array type.
Type *LowerMatrixTypeToOneDimArray(Type *Ty) {
if (HLMatrixType MatTy = HLMatrixType::dyn_cast(Ty)) {
Type *EltTy = MatTy.getElementTypeForReg();
return ArrayType::get(EltTy, MatTy.getNumElements());
} else {
return Ty;
}
}
Type *LowerMatrixArrayPointerToOneDimArray(Type *Ty) {
unsigned addrSpace = Ty->getPointerAddressSpace();
Ty = Ty->getPointerElementType();
unsigned arraySize = 1;
while (Ty->isArrayTy()) {
arraySize *= Ty->getArrayNumElements();
Ty = Ty->getArrayElementType();
}
HLMatrixType MatTy = HLMatrixType::cast(Ty);
arraySize *= MatTy.getNumElements();
Ty = ArrayType::get(MatTy.getElementTypeForReg(), arraySize);
return PointerType::get(Ty, addrSpace);
}
Type *TryLowerMatTy(Type *Ty) {
Type *VecTy = nullptr;
if (HLMatrixType::isMatrixArrayPtr(Ty)) {
VecTy = LowerMatrixArrayPointerToOneDimArray(Ty);
} else if (isa<PointerType>(Ty) &&
HLMatrixType::isa(Ty->getPointerElementType())) {
VecTy = LowerMatrixTypeToOneDimArray(Ty->getPointerElementType());
VecTy = PointerType::get(VecTy, Ty->getPointerAddressSpace());
}
return VecTy;
}
class MatrixBitcastLowerPass : public FunctionPass {
bool SupportsVectors = false;
public:
static char ID; // Pass identification, replacement for typeid
explicit MatrixBitcastLowerPass() : FunctionPass(ID) {}
StringRef getPassName() const override { return "Matrix Bitcast lower"; }
bool runOnFunction(Function &F) override {
DxilModule &DM = F.getParent()->GetOrCreateDxilModule();
SupportsVectors = DM.GetShaderModel()->IsSM69Plus();
bool bUpdated = false;
std::unordered_set<BitCastInst *> matCastSet;
for (auto blkIt = F.begin(); blkIt != F.end(); ++blkIt) {
BasicBlock *BB = blkIt;
for (auto iIt = BB->begin(); iIt != BB->end();) {
Instruction *I = (iIt++);
if (BitCastInst *BCI = dyn_cast<BitCastInst>(I)) {
// Mutate mat to vec.
Type *ToTy = BCI->getType();
if (TryLowerMatTy(ToTy)) {
matCastSet.insert(BCI);
bUpdated = true;
}
}
}
}
// Remove bitcast which has CallInst user.
if (DM.GetShaderModel()->IsLib()) {
for (auto it = matCastSet.begin(); it != matCastSet.end();) {
BitCastInst *BCI = *(it++);
if (hasCallUser(BCI)) {
matCastSet.erase(BCI);
}
}
}
// Lower matrix first.
for (BitCastInst *BCI : matCastSet) {
lowerMatrix(DM, BCI, BCI->getOperand(0));
}
return bUpdated;
}
private:
void lowerMatrix(DxilModule &DM, Instruction *M, Value *A);
bool hasCallUser(Instruction *M);
};
} // namespace
bool MatrixBitcastLowerPass::hasCallUser(Instruction *M) {
for (auto it = M->user_begin(); it != M->user_end();) {
User *U = *(it++);
if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(U)) {
Type *EltTy = GEP->getType()->getPointerElementType();
if (HLMatrixType::isa(EltTy)) {
if (hasCallUser(GEP))
return true;
} else {
DXASSERT(0, "invalid GEP for matrix");
}
} else if (BitCastInst *BCI = dyn_cast<BitCastInst>(U)) {
if (hasCallUser(BCI))
return true;
} else if (LoadInst *LI = dyn_cast<LoadInst>(U)) {
if (isa<VectorType>(LI->getType())) {
} else {
DXASSERT(0, "invalid load for matrix");
}
} else if (StoreInst *ST = dyn_cast<StoreInst>(U)) {
Value *V = ST->getValueOperand();
if (isa<VectorType>(V->getType())) {
} else {
DXASSERT(0, "invalid load for matrix");
}
} else if (isa<CallInst>(U)) {
return true;
} else {
DXASSERT(0, "invalid use of matrix");
}
}
return false;
}
namespace {
Value *CreateEltGEP(Value *A, unsigned i, Value *zeroIdx,
IRBuilder<> &Builder) {
Value *GEP = nullptr;
if (GetElementPtrInst *GEPA = dyn_cast<GetElementPtrInst>(A)) {
// A should be gep oneDimArray, 0, index * matSize
// Here add eltIdx to index * matSize foreach elt.
Instruction *EltGEP = GEPA->clone();
unsigned eltIdx = EltGEP->getNumOperands() - 1;
Value *NewIdx =
Builder.CreateAdd(EltGEP->getOperand(eltIdx), Builder.getInt32(i));
EltGEP->setOperand(eltIdx, NewIdx);
Builder.Insert(EltGEP);
GEP = EltGEP;
} else {
GEP = Builder.CreateInBoundsGEP(A, {zeroIdx, Builder.getInt32(i)});
}
return GEP;
}
} // namespace
void MatrixBitcastLowerPass::lowerMatrix(DxilModule &DM, Instruction *M,
Value *A) {
for (auto it = M->user_begin(); it != M->user_end();) {
User *U = *(it++);
if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(U)) {
Type *EltTy = GEP->getType()->getPointerElementType();
if (HLMatrixType::isa(EltTy)) {
// Change gep matrixArray, 0, index
// into
// gep oneDimArray, 0, index * matSize
IRBuilder<> Builder(GEP);
SmallVector<Value *, 2> idxList(GEP->idx_begin(), GEP->idx_end());
DXASSERT(idxList.size() == 2,
"else not one dim matrix array index to matrix");
unsigned NumElts = HLMatrixType::cast(EltTy).getNumElements();
if (!SupportsVectors || NumElts == 1) {
Value *MatSize = Builder.getInt32(NumElts);
idxList.back() = Builder.CreateMul(idxList.back(), MatSize);
}
Value *NewGEP = Builder.CreateGEP(A, idxList);
lowerMatrix(DM, GEP, NewGEP);
DXASSERT(GEP->user_empty(), "else lower matrix fail");
GEP->eraseFromParent();
} else {
DXASSERT(0, "invalid GEP for matrix");
}
} else if (BitCastInst *BCI = dyn_cast<BitCastInst>(U)) {
lowerMatrix(DM, BCI, A);
DXASSERT(BCI->user_empty(), "else lower matrix fail");
BCI->eraseFromParent();
} else if (LoadInst *LI = dyn_cast<LoadInst>(U)) {
if (VectorType *Ty = dyn_cast<VectorType>(LI->getType())) {
IRBuilder<> Builder(LI);
Value *NewVec = nullptr;
unsigned VecSize = Ty->getVectorNumElements();
if (SupportsVectors && VecSize > 1) {
// Create a replacement load using the vector pointer.
Instruction *NewLd = LI->clone();
unsigned VecIdx = NewLd->getNumOperands() - 1;
NewLd->setOperand(VecIdx, A);
Builder.Insert(NewLd);
NewVec = NewLd;
} else {
Value *zeroIdx = Builder.getInt32(0);
NewVec = UndefValue::get(LI->getType());
for (unsigned i = 0; i < VecSize; i++) {
Value *GEP = CreateEltGEP(A, i, zeroIdx, Builder);
Value *Elt = Builder.CreateLoad(GEP);
NewVec = Builder.CreateInsertElement(NewVec, Elt, i);
}
}
LI->replaceAllUsesWith(NewVec);
LI->eraseFromParent();
} else {
DXASSERT(0, "invalid load for matrix");
}
} else if (StoreInst *ST = dyn_cast<StoreInst>(U)) {
Value *V = ST->getValueOperand();
if (VectorType *Ty = dyn_cast<VectorType>(V->getType())) {
IRBuilder<> Builder(LI);
if (SupportsVectors && Ty->getVectorNumElements() > 1) {
// Create a replacement store using the vector pointer.
Instruction *NewSt = ST->clone();
unsigned VecIdx = NewSt->getNumOperands() - 1;
NewSt->setOperand(VecIdx, A);
Builder.Insert(NewSt);
} else {
Value *zeroIdx = Builder.getInt32(0);
unsigned vecSize = Ty->getNumElements();
for (unsigned i = 0; i < vecSize; i++) {
Value *GEP = CreateEltGEP(A, i, zeroIdx, Builder);
Value *Elt = Builder.CreateExtractElement(V, i);
Builder.CreateStore(Elt, GEP);
}
}
ST->eraseFromParent();
} else {
DXASSERT(0, "invalid load for matrix");
}
} else {
DXASSERT(0, "invalid use of matrix");
}
}
}
char MatrixBitcastLowerPass::ID = 0;
FunctionPass *llvm::createMatrixBitcastLowerPass() {
return new MatrixBitcastLowerPass();
}
INITIALIZE_PASS(MatrixBitcastLowerPass, "matrixbitcastlower",
"Matrix Bitcast lower", false, false)