forked from microsoft/DirectXShaderCompiler
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDxilCounters.cpp
More file actions
437 lines (403 loc) · 15.4 KB
/
DxilCounters.cpp
File metadata and controls
437 lines (403 loc) · 15.4 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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
///////////////////////////////////////////////////////////////////////////////
// //
// DxilCounters.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/DxilCounters.h"
#include "dxc/Support/Global.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/Operator.h"
#include "dxc/DXIL/DxilInstructions.h"
#include "dxc/DXIL/DxilOperations.h"
using namespace llvm;
using namespace hlsl;
using namespace hlsl::DXIL;
namespace hlsl {
namespace {
struct PointerInfo {
enum class MemType : unsigned {
Unknown = 0,
Global_Static,
Global_TGSM,
Alloca
};
MemType memType : 2;
bool isArray : 1;
PointerInfo() : memType(MemType::Unknown), isArray(false) {}
};
typedef DenseMap<Value *, PointerInfo> PointerInfoMap;
PointerInfo GetPointerInfo(Value *V, PointerInfoMap &ptrInfoMap) {
auto it = ptrInfoMap.find(V);
if (it != ptrInfoMap.end())
return it->second;
Type *Ty = V->getType()->getPointerElementType();
ptrInfoMap[V].isArray = Ty->isArrayTy();
if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
if (GV->getType()->getPointerAddressSpace() == DXIL::kTGSMAddrSpace)
ptrInfoMap[V].memType = PointerInfo::MemType::Global_TGSM;
else if (!GV->isConstant() &&
GV->getLinkage() ==
GlobalVariable::LinkageTypes::InternalLinkage &&
GV->getType()->getPointerAddressSpace() == DXIL::kDefaultAddrSpace)
ptrInfoMap[V].memType = PointerInfo::MemType::Global_Static;
} else if (isa<AllocaInst>(V)) {
ptrInfoMap[V].memType = PointerInfo::MemType::Alloca;
} else if (GEPOperator *GEP = dyn_cast<GEPOperator>(V)) {
ptrInfoMap[V] = GetPointerInfo(GEP->getPointerOperand(), ptrInfoMap);
} else if (BitCastOperator *BC = dyn_cast<BitCastOperator>(V)) {
ptrInfoMap[V] = GetPointerInfo(BC->getOperand(0), ptrInfoMap);
} else if (AddrSpaceCastInst *AC = dyn_cast<AddrSpaceCastInst>(V)) {
ptrInfoMap[V] = GetPointerInfo(AC->getOperand(0), ptrInfoMap);
} else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
if (CE->getOpcode() == LLVMAddrSpaceCast)
llvm_unreachable("address space cast is illegal in DxilCounters.");
//} else if (PHINode *PN = dyn_cast<PHINode>(V)) {
// for (auto it = PN->value_op_begin(), e = PN->value_op_end(); it != e;
// ++it) {
// PI = GetPointerInfo(*it, ptrInfoMap);
// if (PI.memType != PointerInfo::MemType::Unknown)
// break;
// }
}
return ptrInfoMap[V];
}
struct ValueInfo {
bool isCbuffer : 1;
bool isConstant : 1;
ValueInfo() : isCbuffer(false), isConstant(false) {}
ValueInfo Combine(const ValueInfo &other) const {
ValueInfo R;
R.isCbuffer = isCbuffer && other.isCbuffer;
R.isConstant = isConstant && other.isConstant;
return R;
}
};
/*<py>
def tab_lines(text):
return [' ' + line for line in text.splitlines()]
def gen_count_dxil_op(counter):
return (['bool CountDxilOp_%s(unsigned op) {' % counter] +
tab_lines(
hctdb_instrhelp.get_instrs_pred("op",
hctdb_instrhelp.counter_pred(counter, True))) +
['}'])
def gen_count_llvm_op(counter):
return (['bool CountLlvmOp_%s(unsigned op) {' % counter] +
tab_lines(
hctdb_instrhelp.get_instrs_pred("op",
hctdb_instrhelp.counter_pred(counter, False), 'llvm_id')) +
['}'])
def gen_counter_functions():
lines = ['// Counter functions for Dxil ops:']
for counter in hctdb_instrhelp.get_dxil_op_counters():
lines += gen_count_dxil_op(counter)
lines.append('// Counter functions for llvm ops:')
for counter in hctdb_instrhelp.get_llvm_op_counters():
lines += gen_count_llvm_op(counter)
return lines
</py>*/
// <py::lines('OPCODE-COUNTERS')>gen_counter_functions()</py>
// OPCODE-COUNTERS:BEGIN
// Counter functions for Dxil ops:
bool CountDxilOp_atomic(unsigned op) {
// Instructions: BufferUpdateCounter=70, AtomicBinOp=78,
// AtomicCompareExchange=79
return op == 70 || (78 <= op && op <= 79);
}
bool CountDxilOp_barrier(unsigned op) {
// Instructions: Barrier=80, BarrierByMemoryType=244,
// BarrierByMemoryHandle=245, BarrierByNodeRecordHandle=246
return op == 80 || (244 <= op && op <= 246);
}
bool CountDxilOp_floats(unsigned op) {
// Instructions: FAbs=6, Saturate=7, IsNaN=8, IsInf=9, IsFinite=10,
// IsNormal=11, Cos=12, Sin=13, Tan=14, Acos=15, Asin=16, Atan=17, Hcos=18,
// Hsin=19, Htan=20, Exp=21, Frc=22, Log=23, Sqrt=24, Rsqrt=25, Round_ne=26,
// Round_ni=27, Round_pi=28, Round_z=29, FMax=35, FMin=36, Fma=47, Dot2=54,
// Dot3=55, Dot4=56, Dot2AddHalf=162, FDot=311
return (6 <= op && op <= 29) || (35 <= op && op <= 36) || op == 47 ||
(54 <= op && op <= 56) || op == 162 || op == 311;
}
bool CountDxilOp_gs_cut(unsigned op) {
// Instructions: CutStream=98, EmitThenCutStream=99
return (98 <= op && op <= 99);
}
bool CountDxilOp_gs_emit(unsigned op) {
// Instructions: EmitStream=97, EmitThenCutStream=99
return op == 97 || op == 99;
}
bool CountDxilOp_ints(unsigned op) {
// Instructions: IMax=37, IMin=38, IMul=41, IMad=48, Ibfe=51,
// Dot4AddI8Packed=163, VectorReduceAnd=309, VectorReduceOr=310
return (37 <= op && op <= 38) || op == 41 || op == 48 || op == 51 ||
op == 163 || (309 <= op && op <= 310);
}
bool CountDxilOp_sig_ld(unsigned op) {
// Instructions: LoadInput=4, LoadOutputControlPoint=103,
// LoadPatchConstant=104
return op == 4 || (103 <= op && op <= 104);
}
bool CountDxilOp_sig_st(unsigned op) {
// Instructions: StoreOutput=5, StorePatchConstant=106, StoreVertexOutput=171,
// StorePrimitiveOutput=172
return op == 5 || op == 106 || (171 <= op && op <= 172);
}
bool CountDxilOp_tex_bias(unsigned op) {
// Instructions: SampleBias=61
return op == 61;
}
bool CountDxilOp_tex_cmp(unsigned op) {
// Instructions: SampleCmp=64, SampleCmpLevelZero=65, TextureGatherCmp=74,
// SampleCmpLevel=224, SampleCmpGrad=254, SampleCmpBias=255
return (64 <= op && op <= 65) || op == 74 || op == 224 ||
(254 <= op && op <= 255);
}
bool CountDxilOp_tex_grad(unsigned op) {
// Instructions: SampleGrad=63
return op == 63;
}
bool CountDxilOp_tex_load(unsigned op) {
// Instructions: TextureLoad=66, BufferLoad=68, RawBufferLoad=139,
// RawBufferVectorLoad=303
return op == 66 || op == 68 || op == 139 || op == 303;
}
bool CountDxilOp_tex_norm(unsigned op) {
// Instructions: Sample=60, SampleLevel=62, TextureGather=73,
// TextureGatherRaw=223
return op == 60 || op == 62 || op == 73 || op == 223;
}
bool CountDxilOp_tex_store(unsigned op) {
// Instructions: TextureStore=67, BufferStore=69, RawBufferStore=140,
// WriteSamplerFeedback=174, WriteSamplerFeedbackBias=175,
// WriteSamplerFeedbackLevel=176, WriteSamplerFeedbackGrad=177,
// TextureStoreSample=225, RawBufferVectorStore=304
return op == 67 || op == 69 || op == 140 || (174 <= op && op <= 177) ||
op == 225 || op == 304;
}
bool CountDxilOp_uints(unsigned op) {
// Instructions: Bfrev=30, Countbits=31, FirstbitLo=32, FirstbitHi=33,
// FirstbitSHi=34, UMax=39, UMin=40, UMul=42, UDiv=43, UAddc=44, USubb=45,
// UMad=49, Msad=50, Ubfe=52, Bfi=53, Dot4AddU8Packed=164,
// VectorReduceAnd=309, VectorReduceOr=310
return (30 <= op && op <= 34) || (39 <= op && op <= 40) ||
(42 <= op && op <= 45) || (49 <= op && op <= 50) ||
(52 <= op && op <= 53) || op == 164 || (309 <= op && op <= 310);
}
// Counter functions for llvm ops:
bool CountLlvmOp_atomic(unsigned op) {
// Instructions: AtomicCmpXchg=31, AtomicRMW=32
return (31 <= op && op <= 32);
}
bool CountLlvmOp_fence(unsigned op) {
// Instructions: Fence=30
return op == 30;
}
bool CountLlvmOp_floats(unsigned op) {
// Instructions: FAdd=9, FSub=11, FMul=13, FDiv=16, FRem=19, FPToUI=36,
// FPToSI=37, UIToFP=38, SIToFP=39, FPTrunc=40, FPExt=41, FCmp=47
return op == 9 || op == 11 || op == 13 || op == 16 || op == 19 ||
(36 <= op && op <= 41) || op == 47;
}
bool CountLlvmOp_ints(unsigned op) {
// Instructions: Add=8, Sub=10, Mul=12, SDiv=15, SRem=18, AShr=22, Trunc=33,
// SExt=35, ICmp=46
return op == 8 || op == 10 || op == 12 || op == 15 || op == 18 || op == 22 ||
op == 33 || op == 35 || op == 46;
}
bool CountLlvmOp_uints(unsigned op) {
// Instructions: UDiv=14, URem=17, Shl=20, LShr=21, And=23, Or=24, Xor=25,
// ZExt=34
return op == 14 || op == 17 || (20 <= op && op <= 21) ||
(23 <= op && op <= 25) || op == 34;
}
// OPCODE-COUNTERS:END
void CountDxilOp(unsigned op, DxilCounters &counters) {
// clang-format off
// Python lines need to be not formatted.
// <py::lines('COUNT-DXIL-OPS')>['if (CountDxilOp_%s(op)) ++counters.%s;' % (c,c) for c in hctdb_instrhelp.get_dxil_op_counters()]</py>
// clang-format on
// COUNT-DXIL-OPS:BEGIN
if (CountDxilOp_atomic(op))
++counters.atomic;
if (CountDxilOp_barrier(op))
++counters.barrier;
if (CountDxilOp_floats(op))
++counters.floats;
if (CountDxilOp_gs_cut(op))
++counters.gs_cut;
if (CountDxilOp_gs_emit(op))
++counters.gs_emit;
if (CountDxilOp_ints(op))
++counters.ints;
if (CountDxilOp_sig_ld(op))
++counters.sig_ld;
if (CountDxilOp_sig_st(op))
++counters.sig_st;
if (CountDxilOp_tex_bias(op))
++counters.tex_bias;
if (CountDxilOp_tex_cmp(op))
++counters.tex_cmp;
if (CountDxilOp_tex_grad(op))
++counters.tex_grad;
if (CountDxilOp_tex_load(op))
++counters.tex_load;
if (CountDxilOp_tex_norm(op))
++counters.tex_norm;
if (CountDxilOp_tex_store(op))
++counters.tex_store;
if (CountDxilOp_uints(op))
++counters.uints;
// COUNT-DXIL-OPS:END
}
void CountLlvmOp(unsigned op, DxilCounters &counters) {
// clang-format off
// Python lines need to be not formatted.
// <py::lines('COUNT-LLVM-OPS')>['if (CountLlvmOp_%s(op)) ++counters.%s;' % (c,c) for c in hctdb_instrhelp.get_llvm_op_counters()]</py>
// clang-format on
// COUNT-LLVM-OPS:BEGIN
if (CountLlvmOp_atomic(op))
++counters.atomic;
if (CountLlvmOp_fence(op))
++counters.fence;
if (CountLlvmOp_floats(op))
++counters.floats;
if (CountLlvmOp_ints(op))
++counters.ints;
if (CountLlvmOp_uints(op))
++counters.uints;
// COUNT-LLVM-OPS:END
}
} // namespace
void CountInstructions(llvm::Module &M, DxilCounters &counters) {
const DataLayout &DL = M.getDataLayout();
PointerInfoMap ptrInfoMap;
for (auto &GV : M.globals()) {
PointerInfo PI = GetPointerInfo(&GV, ptrInfoMap);
if (PI.isArray) {
// Count number of bytes used in global arrays.
Type *pTy = GV.getType()->getPointerElementType();
uint32_t size = DL.getTypeAllocSize(pTy);
switch (PI.memType) {
case PointerInfo::MemType::Global_Static:
counters.array_static_bytes += size;
break;
case PointerInfo::MemType::Global_TGSM:
counters.array_tgsm_bytes += size;
break;
default:
break;
}
}
}
for (auto &F : M.functions()) {
if (F.isDeclaration())
continue;
for (auto itBlock = F.begin(), endBlock = F.end(); itBlock != endBlock;
++itBlock) {
for (auto itInst = itBlock->begin(), endInst = itBlock->end();
itInst != endInst; ++itInst) {
Instruction *I = itInst;
++counters.insts;
if (AllocaInst *AI = dyn_cast<AllocaInst>(I)) {
Type *pTy = AI->getType()->getPointerElementType();
// Count number of bytes used in alloca arrays.
if (pTy->isArrayTy()) {
counters.array_local_bytes += DL.getTypeAllocSize(pTy);
}
} else if (CallInst *CI = dyn_cast<CallInst>(I)) {
if (hlsl::OP::IsDxilOpFuncCallInst(CI)) {
unsigned opcode = static_cast<unsigned>(hlsl::OP::getOpCode(CI));
CountDxilOp(opcode, counters);
}
} else if (isa<LoadInst>(I) || isa<StoreInst>(I)) {
LoadInst *LI = dyn_cast<LoadInst>(I);
StoreInst *SI = dyn_cast<StoreInst>(I);
Value *PtrOp = LI ? LI->getPointerOperand() : SI->getPointerOperand();
PointerInfo PI = GetPointerInfo(PtrOp, ptrInfoMap);
// Count load/store on array elements.
if (PI.isArray) {
switch (PI.memType) {
case PointerInfo::MemType::Alloca:
++counters.array_local_ldst;
break;
case PointerInfo::MemType::Global_Static:
++counters.array_static_ldst;
break;
case PointerInfo::MemType::Global_TGSM:
++counters.array_tgsm_ldst;
break;
default:
break;
}
}
} else if (BranchInst *BI = dyn_cast<BranchInst>(I)) {
if (BI->getNumSuccessors() > 1) {
// TODO: More sophisticated analysis to separate dynamic from static
// branching?
++counters.branches;
}
} else {
// Count llvm ops:
CountLlvmOp(I->getOpcode(), counters);
}
}
}
}
}
struct CounterOffsetByName {
StringRef name;
uint32_t DxilCounters::*ptr;
};
// Must be sorted case-sensitive:
static const CounterOffsetByName CountersByName[] = {
// clang-format off
// Python lines need to be not formatted.
// <py::lines('COUNTER-MEMBER-PTRS')>['{ "%s", &DxilCounters::%s },' % (c,c) for c in hctdb_instrhelp.get_counters()]</py>
// clang-format on
// COUNTER-MEMBER-PTRS:BEGIN
{"array_local_bytes", &DxilCounters::array_local_bytes},
{"array_local_ldst", &DxilCounters::array_local_ldst},
{"array_static_bytes", &DxilCounters::array_static_bytes},
{"array_static_ldst", &DxilCounters::array_static_ldst},
{"array_tgsm_bytes", &DxilCounters::array_tgsm_bytes},
{"array_tgsm_ldst", &DxilCounters::array_tgsm_ldst},
{"atomic", &DxilCounters::atomic},
{"barrier", &DxilCounters::barrier},
{"branches", &DxilCounters::branches},
{"fence", &DxilCounters::fence},
{"floats", &DxilCounters::floats},
{"gs_cut", &DxilCounters::gs_cut},
{"gs_emit", &DxilCounters::gs_emit},
{"insts", &DxilCounters::insts},
{"ints", &DxilCounters::ints},
{"sig_ld", &DxilCounters::sig_ld},
{"sig_st", &DxilCounters::sig_st},
{"tex_bias", &DxilCounters::tex_bias},
{"tex_cmp", &DxilCounters::tex_cmp},
{"tex_grad", &DxilCounters::tex_grad},
{"tex_load", &DxilCounters::tex_load},
{"tex_norm", &DxilCounters::tex_norm},
{"tex_store", &DxilCounters::tex_store},
{"uints", &DxilCounters::uints},
// COUNTER-MEMBER-PTRS:END
};
static int CounterOffsetByNameLess(const CounterOffsetByName &a,
const CounterOffsetByName &b) {
return a.name < b.name;
}
uint32_t *LookupByName(llvm::StringRef name, DxilCounters &counters) {
CounterOffsetByName key = {name, nullptr};
static const CounterOffsetByName *CounterEnd =
CountersByName + _countof(CountersByName);
auto result = std::lower_bound(CountersByName, CounterEnd, key,
CounterOffsetByNameLess);
if (result != CounterEnd && result->name == key.name)
return &(counters.*(result->ptr));
return nullptr;
}
} // namespace hlsl