2
0

DxilCounters.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // //
  3. // DxilCounters.cpp //
  4. // Copyright (C) Microsoft Corporation. All rights reserved. //
  5. // This file is distributed under the University of Illinois Open Source //
  6. // License. See LICENSE.TXT for details. //
  7. // //
  8. ///////////////////////////////////////////////////////////////////////////////
  9. #include "dxc/DXIL/DxilCounters.h"
  10. #include "dxc/Support/Global.h"
  11. #include "llvm/IR/LLVMContext.h"
  12. #include "llvm/IR/Operator.h"
  13. #include "llvm/IR/Module.h"
  14. #include "llvm/ADT/DenseMap.h"
  15. #include "dxc/DXIL/DxilOperations.h"
  16. #include "dxc/DXIL/DxilInstructions.h"
  17. using namespace llvm;
  18. using namespace hlsl;
  19. using namespace hlsl::DXIL;
  20. namespace hlsl {
  21. namespace {
  22. struct PointerInfo {
  23. enum class MemType : unsigned {
  24. Unknown = 0,
  25. Global_Static,
  26. Global_TGSM,
  27. Alloca
  28. };
  29. MemType memType : 2;
  30. bool isArray : 1;
  31. PointerInfo() :
  32. memType(MemType::Unknown),
  33. isArray(false)
  34. {}
  35. };
  36. typedef DenseMap<Value*, PointerInfo> PointerInfoMap;
  37. PointerInfo GetPointerInfo(Value* V, PointerInfoMap &ptrInfoMap) {
  38. auto it = ptrInfoMap.find(V);
  39. if (it != ptrInfoMap.end())
  40. return it->second;
  41. PointerInfo &PI = ptrInfoMap[V];
  42. Type *Ty = V->getType()->getPointerElementType();
  43. PI.isArray = Ty->isArrayTy();
  44. if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
  45. if (GV->getType()->getPointerAddressSpace() == DXIL::kTGSMAddrSpace)
  46. PI.memType = PointerInfo::MemType::Global_TGSM;
  47. else if (!GV->isConstant() &&
  48. GV->getLinkage() == GlobalVariable::LinkageTypes::InternalLinkage &&
  49. GV->getType()->getPointerAddressSpace() == DXIL::kDefaultAddrSpace)
  50. PI.memType = PointerInfo::MemType::Global_Static;
  51. } else if (AllocaInst *AI = dyn_cast<AllocaInst>(V)) {
  52. PI.memType = PointerInfo::MemType::Alloca;
  53. } else if (GEPOperator *GEP = dyn_cast<GEPOperator>(V)) {
  54. PI = GetPointerInfo(GEP->getPointerOperand(), ptrInfoMap);
  55. } else if (BitCastOperator *BC = dyn_cast<BitCastOperator>(V)) {
  56. PI = GetPointerInfo(BC->getOperand(0), ptrInfoMap);
  57. } else if (AddrSpaceCastInst *AC = dyn_cast<AddrSpaceCastInst>(V)) {
  58. PI = GetPointerInfo(AC->getOperand(0), ptrInfoMap);
  59. } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
  60. if (CE->getOpcode() == LLVMAddrSpaceCast)
  61. PI = GetPointerInfo(AC->getOperand(0), ptrInfoMap);
  62. //} else if (PHINode *PN = dyn_cast<PHINode>(V)) {
  63. // for (auto it = PN->value_op_begin(), e = PN->value_op_end(); it != e; ++it) {
  64. // PI = GetPointerInfo(*it, ptrInfoMap);
  65. // if (PI.memType != PointerInfo::MemType::Unknown)
  66. // break;
  67. // }
  68. }
  69. return PI;
  70. };
  71. struct ValueInfo {
  72. bool isCbuffer : 1;
  73. bool isConstant : 1;
  74. ValueInfo() :
  75. isCbuffer(false),
  76. isConstant(false)
  77. {}
  78. ValueInfo Combine(const ValueInfo &other) const {
  79. ValueInfo R;
  80. R.isCbuffer = isCbuffer && other.isCbuffer;
  81. R.isConstant = isConstant && other.isConstant;
  82. return R;
  83. }
  84. };
  85. /*<py>
  86. def tab_lines(text):
  87. return [' ' + line for line in text.splitlines()]
  88. def gen_count_dxil_op(counter):
  89. return (['bool CountDxilOp_%s(unsigned op) {' % counter] +
  90. tab_lines(
  91. hctdb_instrhelp.get_instrs_pred("op", hctdb_instrhelp.counter_pred(counter, True))) +
  92. ['}'])
  93. def gen_count_llvm_op(counter):
  94. return (['bool CountLlvmOp_%s(unsigned op) {' % counter] +
  95. tab_lines(
  96. hctdb_instrhelp.get_instrs_pred("op", hctdb_instrhelp.counter_pred(counter, False), 'llvm_id')) +
  97. ['}'])
  98. def gen_counter_functions():
  99. lines = ['// Counter functions for Dxil ops:']
  100. for counter in hctdb_instrhelp.get_dxil_op_counters():
  101. lines += gen_count_dxil_op(counter)
  102. lines.append('// Counter functions for llvm ops:')
  103. for counter in hctdb_instrhelp.get_llvm_op_counters():
  104. lines += gen_count_llvm_op(counter)
  105. return lines
  106. </py>*/
  107. // <py::lines('OPCODE-COUNTERS')>gen_counter_functions()</py>
  108. // OPCODE-COUNTERS:BEGIN
  109. // Counter functions for Dxil ops:
  110. bool CountDxilOp_atomic(unsigned op) {
  111. // Instructions: BufferUpdateCounter=70, AtomicBinOp=78,
  112. // AtomicCompareExchange=79
  113. return op == 70 || (78 <= op && op <= 79);
  114. }
  115. bool CountDxilOp_barrier(unsigned op) {
  116. // Instructions: Barrier=80
  117. return op == 80;
  118. }
  119. bool CountDxilOp_floats(unsigned op) {
  120. // Instructions: FAbs=6, Saturate=7, IsNaN=8, IsInf=9, IsFinite=10,
  121. // IsNormal=11, Cos=12, Sin=13, Tan=14, Acos=15, Asin=16, Atan=17, Hcos=18,
  122. // Hsin=19, Htan=20, Exp=21, Frc=22, Log=23, Sqrt=24, Rsqrt=25, Round_ne=26,
  123. // Round_ni=27, Round_pi=28, Round_z=29, FMax=35, FMin=36, Fma=47, Dot2=54,
  124. // Dot3=55, Dot4=56, Dot2AddHalf=162
  125. return (6 <= op && op <= 29) || (35 <= op && op <= 36) || op == 47 || (54 <= op && op <= 56) || op == 162;
  126. }
  127. bool CountDxilOp_gs_cut(unsigned op) {
  128. // Instructions: CutStream=98, EmitThenCutStream=99
  129. return (98 <= op && op <= 99);
  130. }
  131. bool CountDxilOp_gs_emit(unsigned op) {
  132. // Instructions: EmitStream=97, EmitThenCutStream=99
  133. return op == 97 || op == 99;
  134. }
  135. bool CountDxilOp_ints(unsigned op) {
  136. // Instructions: IMax=37, IMin=38, IMul=41, IMad=48, Ibfe=51,
  137. // Dot4AddI8Packed=163
  138. return (37 <= op && op <= 38) || op == 41 || op == 48 || op == 51 || op == 163;
  139. }
  140. bool CountDxilOp_sig_ld(unsigned op) {
  141. // Instructions: LoadInput=4, LoadOutputControlPoint=103, LoadPatchConstant=104
  142. return op == 4 || (103 <= op && op <= 104);
  143. }
  144. bool CountDxilOp_sig_st(unsigned op) {
  145. // Instructions: StoreOutput=5, StorePatchConstant=106, StoreVertexOutput=171,
  146. // StorePrimitiveOutput=172
  147. return op == 5 || op == 106 || (171 <= op && op <= 172);
  148. }
  149. bool CountDxilOp_tex_bias(unsigned op) {
  150. // Instructions: SampleBias=61
  151. return op == 61;
  152. }
  153. bool CountDxilOp_tex_cmp(unsigned op) {
  154. // Instructions: SampleCmp=64, SampleCmpLevelZero=65, TextureGatherCmp=74
  155. return (64 <= op && op <= 65) || op == 74;
  156. }
  157. bool CountDxilOp_tex_grad(unsigned op) {
  158. // Instructions: SampleGrad=63
  159. return op == 63;
  160. }
  161. bool CountDxilOp_tex_load(unsigned op) {
  162. // Instructions: TextureLoad=66, BufferLoad=68, RawBufferLoad=139
  163. return op == 66 || op == 68 || op == 139;
  164. }
  165. bool CountDxilOp_tex_norm(unsigned op) {
  166. // Instructions: Sample=60, SampleLevel=62, TextureGather=73
  167. return op == 60 || op == 62 || op == 73;
  168. }
  169. bool CountDxilOp_tex_store(unsigned op) {
  170. // Instructions: TextureStore=67, BufferStore=69, RawBufferStore=140,
  171. // WriteSamplerFeedback=174, WriteSamplerFeedbackBias=175,
  172. // WriteSamplerFeedbackLevel=176, WriteSamplerFeedbackGrad=177
  173. return op == 67 || op == 69 || op == 140 || (174 <= op && op <= 177);
  174. }
  175. bool CountDxilOp_uints(unsigned op) {
  176. // Instructions: Bfrev=30, Countbits=31, FirstbitLo=32, FirstbitHi=33,
  177. // FirstbitSHi=34, UMax=39, UMin=40, UMul=42, UDiv=43, UAddc=44, USubb=45,
  178. // UMad=49, Msad=50, Ubfe=52, Bfi=53, Dot4AddU8Packed=164
  179. return (30 <= op && op <= 34) || (39 <= op && op <= 40) || (42 <= op && op <= 45) || (49 <= op && op <= 50) || (52 <= op && op <= 53) || op == 164;
  180. }
  181. // Counter functions for llvm ops:
  182. bool CountLlvmOp_atomic(unsigned op) {
  183. // Instructions: AtomicCmpXchg=31, AtomicRMW=32
  184. return (31 <= op && op <= 32);
  185. }
  186. bool CountLlvmOp_fence(unsigned op) {
  187. // Instructions: Fence=30
  188. return op == 30;
  189. }
  190. bool CountLlvmOp_floats(unsigned op) {
  191. // Instructions: FAdd=9, FSub=11, FMul=13, FDiv=16, FRem=19, FPToUI=36,
  192. // FPToSI=37, UIToFP=38, SIToFP=39, FPTrunc=40, FPExt=41, FCmp=47
  193. return op == 9 || op == 11 || op == 13 || op == 16 || op == 19 || (36 <= op && op <= 41) || op == 47;
  194. }
  195. bool CountLlvmOp_ints(unsigned op) {
  196. // Instructions: Add=8, Sub=10, Mul=12, SDiv=15, SRem=18, AShr=22, Trunc=33,
  197. // SExt=35, ICmp=46
  198. return op == 8 || op == 10 || op == 12 || op == 15 || op == 18 || op == 22 || op == 33 || op == 35 || op == 46;
  199. }
  200. bool CountLlvmOp_uints(unsigned op) {
  201. // Instructions: UDiv=14, URem=17, Shl=20, LShr=21, And=23, Or=24, Xor=25,
  202. // ZExt=34
  203. return op == 14 || op == 17 || (20 <= op && op <= 21) || (23 <= op && op <= 25) || op == 34;
  204. }
  205. // OPCODE-COUNTERS:END
  206. void CountDxilOp(unsigned op, DxilCounters &counters) {
  207. // <py::lines('COUNT-DXIL-OPS')>['if (CountDxilOp_%s(op)) ++counters.%s;' % (c,c) for c in hctdb_instrhelp.get_dxil_op_counters()]</py>
  208. // COUNT-DXIL-OPS:BEGIN
  209. if (CountDxilOp_atomic(op)) ++counters.atomic;
  210. if (CountDxilOp_barrier(op)) ++counters.barrier;
  211. if (CountDxilOp_floats(op)) ++counters.floats;
  212. if (CountDxilOp_gs_cut(op)) ++counters.gs_cut;
  213. if (CountDxilOp_gs_emit(op)) ++counters.gs_emit;
  214. if (CountDxilOp_ints(op)) ++counters.ints;
  215. if (CountDxilOp_sig_ld(op)) ++counters.sig_ld;
  216. if (CountDxilOp_sig_st(op)) ++counters.sig_st;
  217. if (CountDxilOp_tex_bias(op)) ++counters.tex_bias;
  218. if (CountDxilOp_tex_cmp(op)) ++counters.tex_cmp;
  219. if (CountDxilOp_tex_grad(op)) ++counters.tex_grad;
  220. if (CountDxilOp_tex_load(op)) ++counters.tex_load;
  221. if (CountDxilOp_tex_norm(op)) ++counters.tex_norm;
  222. if (CountDxilOp_tex_store(op)) ++counters.tex_store;
  223. if (CountDxilOp_uints(op)) ++counters.uints;
  224. // COUNT-DXIL-OPS:END
  225. }
  226. void CountLlvmOp(unsigned op, DxilCounters &counters) {
  227. // <py::lines('COUNT-LLVM-OPS')>['if (CountLlvmOp_%s(op)) ++counters.%s;' % (c,c) for c in hctdb_instrhelp.get_llvm_op_counters()]</py>
  228. // COUNT-LLVM-OPS:BEGIN
  229. if (CountLlvmOp_atomic(op)) ++counters.atomic;
  230. if (CountLlvmOp_fence(op)) ++counters.fence;
  231. if (CountLlvmOp_floats(op)) ++counters.floats;
  232. if (CountLlvmOp_ints(op)) ++counters.ints;
  233. if (CountLlvmOp_uints(op)) ++counters.uints;
  234. // COUNT-LLVM-OPS:END
  235. }
  236. } // namespace
  237. void CountInstructions(llvm::Module &M, DxilCounters& counters) {
  238. const DataLayout &DL = M.getDataLayout();
  239. PointerInfoMap ptrInfoMap;
  240. for (auto &GV : M.globals()) {
  241. PointerInfo PI = GetPointerInfo(&GV, ptrInfoMap);
  242. if (PI.isArray) {
  243. // Count number of bytes used in global arrays.
  244. Type *pTy = GV.getType()->getPointerElementType();
  245. uint32_t size = DL.getTypeAllocSize(pTy);
  246. switch (PI.memType) {
  247. case PointerInfo::MemType::Global_Static: counters.array_static_bytes += size; break;
  248. case PointerInfo::MemType::Global_TGSM: counters.array_tgsm_bytes += size; break;
  249. default: break;
  250. }
  251. }
  252. }
  253. for (auto &F : M.functions()) {
  254. if (F.isDeclaration())
  255. continue;
  256. for (auto itBlock = F.begin(), endBlock = F.end(); itBlock != endBlock; ++itBlock) {
  257. for (auto itInst = itBlock->begin(), endInst = itBlock->end(); itInst != endInst; ++itInst) {
  258. Instruction* I = itInst;
  259. ++counters.insts;
  260. if (AllocaInst *AI = dyn_cast<AllocaInst>(I)) {
  261. Type *pTy = AI->getType()->getPointerElementType();
  262. // Count number of bytes used in alloca arrays.
  263. if (pTy->isArrayTy()) {
  264. counters.array_local_bytes += DL.getTypeAllocSize(pTy);
  265. }
  266. } else if (CallInst *CI = dyn_cast<CallInst>(I)) {
  267. if (hlsl::OP::IsDxilOpFuncCallInst(CI)) {
  268. unsigned opcode = (unsigned)llvm::cast<llvm::ConstantInt>(I->getOperand(0))->getZExtValue();
  269. CountDxilOp(opcode, counters);
  270. }
  271. } else if (isa<LoadInst>(I) || isa<StoreInst>(I)) {
  272. LoadInst *LI = dyn_cast<LoadInst>(I);
  273. StoreInst *SI = dyn_cast<StoreInst>(I);
  274. Value *PtrOp = LI ? LI->getPointerOperand() : SI->getPointerOperand();
  275. PointerInfo PI = GetPointerInfo(PtrOp, ptrInfoMap);
  276. // Count load/store on array elements.
  277. if (PI.isArray) {
  278. switch (PI.memType) {
  279. case PointerInfo::MemType::Alloca: ++counters.array_local_ldst; break;
  280. case PointerInfo::MemType::Global_Static: ++counters.array_static_ldst; break;
  281. case PointerInfo::MemType::Global_TGSM: ++counters.array_tgsm_ldst; break;
  282. default: break;
  283. }
  284. }
  285. } else if (BranchInst *BI = dyn_cast<BranchInst>(I)) {
  286. if (BI->getNumSuccessors() > 1) {
  287. // TODO: More sophisticated analysis to separate dynamic from static branching?
  288. ++counters.branches;
  289. }
  290. } else {
  291. // Count llvm ops:
  292. CountLlvmOp(I->getOpcode(), counters);
  293. }
  294. }
  295. }
  296. }
  297. }
  298. struct CounterOffsetByName {
  299. StringRef name;
  300. uint32_t DxilCounters::*ptr;
  301. };
  302. // Must be sorted case-sensitive:
  303. static const CounterOffsetByName CountersByName[] = {
  304. // <py::lines('COUNTER-MEMBER-PTRS')>['{ "%s", &DxilCounters::%s },' % (c,c) for c in hctdb_instrhelp.get_counters()]</py>
  305. // COUNTER-MEMBER-PTRS:BEGIN
  306. { "array_local_bytes", &DxilCounters::array_local_bytes },
  307. { "array_local_ldst", &DxilCounters::array_local_ldst },
  308. { "array_static_bytes", &DxilCounters::array_static_bytes },
  309. { "array_static_ldst", &DxilCounters::array_static_ldst },
  310. { "array_tgsm_bytes", &DxilCounters::array_tgsm_bytes },
  311. { "array_tgsm_ldst", &DxilCounters::array_tgsm_ldst },
  312. { "atomic", &DxilCounters::atomic },
  313. { "barrier", &DxilCounters::barrier },
  314. { "branches", &DxilCounters::branches },
  315. { "fence", &DxilCounters::fence },
  316. { "floats", &DxilCounters::floats },
  317. { "gs_cut", &DxilCounters::gs_cut },
  318. { "gs_emit", &DxilCounters::gs_emit },
  319. { "insts", &DxilCounters::insts },
  320. { "ints", &DxilCounters::ints },
  321. { "sig_ld", &DxilCounters::sig_ld },
  322. { "sig_st", &DxilCounters::sig_st },
  323. { "tex_bias", &DxilCounters::tex_bias },
  324. { "tex_cmp", &DxilCounters::tex_cmp },
  325. { "tex_grad", &DxilCounters::tex_grad },
  326. { "tex_load", &DxilCounters::tex_load },
  327. { "tex_norm", &DxilCounters::tex_norm },
  328. { "tex_store", &DxilCounters::tex_store },
  329. { "uints", &DxilCounters::uints },
  330. // COUNTER-MEMBER-PTRS:END
  331. };
  332. static int CounterOffsetByNameLess(const CounterOffsetByName &a, const CounterOffsetByName &b) {
  333. return a.name < b.name;
  334. }
  335. uint32_t *LookupByName(llvm::StringRef name, DxilCounters& counters) {
  336. CounterOffsetByName key = {name, nullptr};
  337. static const CounterOffsetByName *CounterEnd = CountersByName +_countof(CountersByName);
  338. auto result = std::lower_bound(CountersByName, CounterEnd, key, CounterOffsetByNameLess);
  339. if (result != CounterEnd && result->name == key.name)
  340. return &(counters.*(result->ptr));
  341. return nullptr;
  342. }
  343. } // namespace hlsl