DxilPIXMeshShaderOutputInstrumentation.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // //
  3. // DxilAddPixelHitInstrumentation.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. // Provides a pass to add instrumentation to retrieve mesh shader output. //
  9. // Used by PIX. //
  10. // //
  11. ///////////////////////////////////////////////////////////////////////////////
  12. #include "dxc/DXIL/DxilOperations.h"
  13. #include "dxc/DXIL/DxilUtil.h"
  14. #include "dxc/DXIL/DxilInstructions.h"
  15. #include "dxc/DXIL/DxilModule.h"
  16. #include "dxc/DxilPIXPasses/DxilPIXPasses.h"
  17. #include "dxc/HLSL/DxilGenerationPass.h"
  18. #include "dxc/HLSL/DxilSpanAllocator.h"
  19. #include "llvm/IR/PassManager.h"
  20. #include "llvm/Support/FormattedStream.h"
  21. #include "llvm/Transforms/Utils/Local.h"
  22. #include <deque>
  23. #ifdef _WIN32
  24. #include <winerror.h>
  25. #endif
  26. #include "PixPassHelpers.h"
  27. // Keep these in sync with the same-named value in the debugger application's
  28. // WinPixShaderUtils.h
  29. constexpr uint64_t DebugBufferDumpingGroundSize = 64 * 1024;
  30. // The actual max size per record is much smaller than this, but it never
  31. // hurts to be generous.
  32. constexpr size_t CounterOffsetBeyondUsefulData = DebugBufferDumpingGroundSize / 2;
  33. // Keep these in sync with the same-named values in PIX's MeshShaderOutput.cpp
  34. constexpr uint32_t triangleIndexIndicator = 1;
  35. constexpr uint32_t int32ValueIndicator = 2;
  36. constexpr uint32_t floatValueIndicator = 3;
  37. constexpr uint32_t int16ValueIndicator = 4;
  38. constexpr uint32_t float16ValueIndicator = 5;
  39. using namespace llvm;
  40. using namespace hlsl;
  41. class DxilPIXMeshShaderOutputInstrumentation : public ModulePass
  42. {
  43. public:
  44. static char ID; // Pass identification, replacement for typeid
  45. explicit DxilPIXMeshShaderOutputInstrumentation() : ModulePass(ID) {}
  46. const char *getPassName() const override {
  47. return "DXIL mesh shader output instrumentation";
  48. }
  49. void applyOptions(PassOptions O) override;
  50. bool runOnModule(Module &M) override;
  51. private:
  52. CallInst *m_OutputUAV = nullptr;
  53. int m_RemainingReservedSpaceInBytes = 0;
  54. Constant *m_OffsetMask = nullptr;
  55. uint64_t m_UAVSize = 1024 * 1024;
  56. struct BuilderContext {
  57. Module &M;
  58. DxilModule &DM;
  59. LLVMContext &Ctx;
  60. OP *HlslOP;
  61. IRBuilder<> &Builder;
  62. };
  63. Value *insertInstructionsToCalculateFlattenedGroupIdXandY(BuilderContext &BC);
  64. Value *insertInstructionsToCalculateGroupIdZ(BuilderContext &BC);
  65. Value *reserveDebugEntrySpace(BuilderContext &BC, uint32_t SpaceInBytes);
  66. uint32_t UAVDumpingGroundOffset();
  67. Value *writeDwordAndReturnNewOffset(BuilderContext &BC, Value *TheOffset,
  68. Value *TheValue);
  69. template <typename... T> void Instrument(BuilderContext &BC, T... values);
  70. };
  71. void DxilPIXMeshShaderOutputInstrumentation::applyOptions(PassOptions O)
  72. {
  73. GetPassOptionUInt64(O, "UAVSize", &m_UAVSize, 1024 * 1024);
  74. }
  75. uint32_t DxilPIXMeshShaderOutputInstrumentation::UAVDumpingGroundOffset()
  76. {
  77. return static_cast<uint32_t>(m_UAVSize - DebugBufferDumpingGroundSize);
  78. }
  79. Value *DxilPIXMeshShaderOutputInstrumentation::
  80. insertInstructionsToCalculateFlattenedGroupIdXandY(BuilderContext &BC)
  81. {
  82. Constant *Zero32Arg = BC.HlslOP->GetU32Const(0);
  83. Constant *One32Arg = BC.HlslOP->GetU32Const(1);
  84. auto GroupIdFunc =
  85. BC.HlslOP->GetOpFunc(DXIL::OpCode::GroupId, Type::getInt32Ty(BC.Ctx));
  86. Constant *Opcode = BC.HlslOP->GetU32Const((unsigned)DXIL::OpCode::GroupId);
  87. auto GroupIdX =
  88. BC.Builder.CreateCall(GroupIdFunc, {Opcode, Zero32Arg}, "GroupIdX");
  89. auto GroupIdY =
  90. BC.Builder.CreateCall(GroupIdFunc, {Opcode, One32Arg}, "GroupIdY");
  91. // Spec requires that no group id index is greater than 64k, so we can
  92. // combine two into one 32-bit value:
  93. auto YShifted =
  94. BC.Builder.CreateShl(GroupIdY, 16);
  95. return BC.Builder.CreateAdd(YShifted, GroupIdX);
  96. }
  97. Value *DxilPIXMeshShaderOutputInstrumentation::
  98. insertInstructionsToCalculateGroupIdZ(BuilderContext &BC)
  99. {
  100. Constant *Two32Arg = BC.HlslOP->GetU32Const(2);
  101. auto GroupIdFunc =
  102. BC.HlslOP->GetOpFunc(DXIL::OpCode::GroupId, Type::getInt32Ty(BC.Ctx));
  103. Constant *Opcode = BC.HlslOP->GetU32Const((unsigned)DXIL::OpCode::GroupId);
  104. return BC.Builder.CreateCall(GroupIdFunc, {Opcode, Two32Arg}, "GroupIdZ");
  105. }
  106. Value *DxilPIXMeshShaderOutputInstrumentation::reserveDebugEntrySpace(
  107. BuilderContext &BC, uint32_t SpaceInBytes)
  108. {
  109. // Check the previous caller didn't reserve too much space:
  110. assert(m_RemainingReservedSpaceInBytes == 0);
  111. // Check that the caller didn't ask for so much memory that it will
  112. // overwrite the offset counter:
  113. assert(m_RemainingReservedSpaceInBytes < (int)CounterOffsetBeyondUsefulData);
  114. m_RemainingReservedSpaceInBytes = SpaceInBytes;
  115. // Insert the UAV increment instruction:
  116. Function *AtomicOpFunc =
  117. BC.HlslOP->GetOpFunc(OP::OpCode::AtomicBinOp, Type::getInt32Ty(BC.Ctx));
  118. Constant *AtomicBinOpcode =
  119. BC.HlslOP->GetU32Const((unsigned)OP::OpCode::AtomicBinOp);
  120. Constant *AtomicAdd =
  121. BC.HlslOP->GetU32Const((unsigned)DXIL::AtomicBinOpCode::Add);
  122. Constant *OffsetArg =
  123. BC.HlslOP->GetU32Const(UAVDumpingGroundOffset() + CounterOffsetBeyondUsefulData);
  124. UndefValue *UndefArg = UndefValue::get(Type::getInt32Ty(BC.Ctx));
  125. Constant *Increment = BC.HlslOP->GetU32Const(SpaceInBytes);
  126. auto *PreviousValue = BC.Builder.CreateCall(
  127. AtomicOpFunc,
  128. {
  129. AtomicBinOpcode, // i32, ; opcode
  130. m_OutputUAV, // %dx.types.Handle, ; resource handle
  131. AtomicAdd, // i32, ; binary operation code : EXCHANGE, IADD, AND, OR,
  132. // XOR, IMIN, IMAX, UMIN, UMAX
  133. OffsetArg, // i32, ; coordinate c0: index in bytes
  134. UndefArg, // i32, ; coordinate c1 (unused)
  135. UndefArg, // i32, ; coordinate c2 (unused)
  136. Increment, // i32); increment value
  137. },
  138. "UAVIncResult");
  139. return BC.Builder.CreateAnd(PreviousValue, m_OffsetMask, "MaskedForUAVLimit");
  140. }
  141. Value *DxilPIXMeshShaderOutputInstrumentation::writeDwordAndReturnNewOffset(
  142. BuilderContext &BC, Value *TheOffset, Value *TheValue)
  143. {
  144. Function *StoreValue =
  145. BC.HlslOP->GetOpFunc(OP::OpCode::BufferStore, Type::getInt32Ty(BC.Ctx));
  146. Constant *StoreValueOpcode =
  147. BC.HlslOP->GetU32Const((unsigned)DXIL::OpCode::BufferStore);
  148. UndefValue *Undef32Arg = UndefValue::get(Type::getInt32Ty(BC.Ctx));
  149. Constant *WriteMask_X = BC.HlslOP->GetI8Const(1);
  150. (void)BC.Builder.CreateCall(
  151. StoreValue,
  152. {StoreValueOpcode, // i32 opcode
  153. m_OutputUAV, // %dx.types.Handle, ; resource handle
  154. TheOffset, // i32 c0: index in bytes into UAV
  155. Undef32Arg, // i32 c1: unused
  156. TheValue,
  157. Undef32Arg, // unused values
  158. Undef32Arg, // unused values
  159. Undef32Arg, // unused values
  160. WriteMask_X});
  161. m_RemainingReservedSpaceInBytes -= sizeof(uint32_t);
  162. assert(m_RemainingReservedSpaceInBytes >=
  163. 0); // or else the caller didn't reserve enough space
  164. return BC.Builder.CreateAdd(
  165. TheOffset,
  166. BC.HlslOP->GetU32Const(static_cast<unsigned int>(sizeof(uint32_t))));
  167. }
  168. template <typename... T>
  169. void DxilPIXMeshShaderOutputInstrumentation::Instrument(BuilderContext &BC,
  170. T... values)
  171. {
  172. llvm::SmallVector<llvm::Value *, 10> Values(
  173. {static_cast<llvm::Value *>(values)...});
  174. const uint32_t DwordCount = Values.size();
  175. llvm::Value *byteOffset =
  176. reserveDebugEntrySpace(BC, DwordCount * sizeof(uint32_t));
  177. for (llvm::Value *V : Values)
  178. {
  179. byteOffset = writeDwordAndReturnNewOffset(BC, byteOffset, V);
  180. }
  181. }
  182. bool DxilPIXMeshShaderOutputInstrumentation::runOnModule(Module &M)
  183. {
  184. DxilModule &DM = M.GetOrCreateDxilModule();
  185. LLVMContext &Ctx = M.getContext();
  186. OP *HlslOP = DM.GetOP();
  187. Instruction *firstInsertionPt =
  188. dxilutil::FirstNonAllocaInsertionPt(DM.GetEntryFunction());
  189. IRBuilder<> Builder(firstInsertionPt);
  190. BuilderContext BC{M, DM, Ctx, HlslOP, Builder};
  191. m_OffsetMask = BC.HlslOP->GetU32Const(UAVDumpingGroundOffset() - 1);
  192. m_OutputUAV = PIXPassHelpers::CreateUAV(DM, Builder, 0, "PIX_DebugUAV_Handle");
  193. auto GroupIdXandY = insertInstructionsToCalculateFlattenedGroupIdXandY(BC);
  194. auto GroupIdZ = insertInstructionsToCalculateGroupIdZ(BC);
  195. auto F = HlslOP->GetOpFunc(DXIL::OpCode::EmitIndices, Type::getVoidTy(Ctx));
  196. auto FunctionUses = F->uses();
  197. for (auto FI = FunctionUses.begin(); FI != FunctionUses.end();)
  198. {
  199. auto &FunctionUse = *FI++;
  200. auto FunctionUser = FunctionUse.getUser();
  201. auto Call = cast<CallInst>(FunctionUser);
  202. IRBuilder<> Builder2(Call);
  203. BuilderContext BC2{M, DM, Ctx, HlslOP, Builder2};
  204. Instrument(BC2, BC2.HlslOP->GetI32Const(triangleIndexIndicator),
  205. GroupIdXandY, GroupIdZ, Call->getOperand(1),
  206. Call->getOperand(2), Call->getOperand(3), Call->getOperand(4));
  207. }
  208. struct OutputType
  209. {
  210. Type *type;
  211. uint32_t tag;
  212. };
  213. SmallVector<OutputType, 4> StoreVertexOutputOverloads
  214. {
  215. {Type::getInt32Ty(Ctx), int32ValueIndicator},
  216. {Type::getInt16Ty(Ctx), int16ValueIndicator},
  217. {Type::getFloatTy(Ctx), floatValueIndicator},
  218. {Type::getHalfTy(Ctx), float16ValueIndicator}
  219. };
  220. for (auto const &Overload : StoreVertexOutputOverloads)
  221. {
  222. F = HlslOP->GetOpFunc(DXIL::OpCode::StoreVertexOutput, Overload.type);
  223. FunctionUses = F->uses();
  224. for (auto FI = FunctionUses.begin(); FI != FunctionUses.end();)
  225. {
  226. auto &FunctionUse = *FI++;
  227. auto FunctionUser = FunctionUse.getUser();
  228. auto Call = cast<CallInst>(FunctionUser);
  229. IRBuilder<> Builder2(Call);
  230. BuilderContext BC2{M, DM, Ctx, HlslOP, Builder2};
  231. // Expand column index to 32 bits:
  232. auto ColumnIndex = BC2.Builder.CreateCast(
  233. Instruction::ZExt,
  234. Call->getOperand(3),
  235. Type::getInt32Ty(Ctx));
  236. // Coerce actual value to int32
  237. Value *CoercedValue = Call->getOperand(4);
  238. if (Overload.tag == floatValueIndicator)
  239. {
  240. CoercedValue = BC2.Builder.CreateCast(
  241. Instruction::BitCast,
  242. CoercedValue,
  243. Type::getInt32Ty(Ctx));
  244. }
  245. else if (Overload.tag == float16ValueIndicator)
  246. {
  247. auto * HalfInt = BC2.Builder.CreateCast(
  248. Instruction::BitCast,
  249. CoercedValue,
  250. Type::getInt16Ty(Ctx));
  251. CoercedValue = BC2.Builder.CreateCast(
  252. Instruction::ZExt,
  253. HalfInt,
  254. Type::getInt32Ty(Ctx));
  255. }
  256. else if (Overload.tag == int16ValueIndicator)
  257. {
  258. CoercedValue = BC2.Builder.CreateCast(
  259. Instruction::ZExt,
  260. CoercedValue,
  261. Type::getInt32Ty(Ctx));
  262. }
  263. Instrument(
  264. BC2,
  265. BC2.HlslOP->GetI32Const(Overload.tag),
  266. GroupIdXandY,
  267. GroupIdZ,
  268. Call->getOperand(1),
  269. Call->getOperand(2),
  270. ColumnIndex,
  271. CoercedValue,
  272. Call->getOperand(5));
  273. }
  274. }
  275. DM.ReEmitDxilResources();
  276. return true;
  277. }
  278. char DxilPIXMeshShaderOutputInstrumentation::ID = 0;
  279. ModulePass *llvm::createDxilDxilPIXMeshShaderOutputInstrumentation()
  280. {
  281. return new DxilPIXMeshShaderOutputInstrumentation();
  282. }
  283. INITIALIZE_PASS(DxilPIXMeshShaderOutputInstrumentation,
  284. "hlsl-dxil-pix-meshshader-output-instrumentation",
  285. "DXIL mesh shader output instrumentation for PIX", false, false)