DxilPIXMeshShaderOutputInstrumentation.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  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. // Keep these in sync with the same-named value in the debugger application's
  27. // WinPixShaderUtils.h
  28. constexpr uint64_t DebugBufferDumpingGroundSize = 64 * 1024;
  29. // The actual max size per record is much smaller than this, but it never
  30. // hurts to be generous.
  31. constexpr size_t CounterOffsetBeyondUsefulData = DebugBufferDumpingGroundSize / 2;
  32. // Keep these in sync with the same-named values in PIX's MeshShaderOutput.cpp
  33. constexpr uint32_t triangleIndexIndicator = 1;
  34. constexpr uint32_t int32ValueIndicator = 2;
  35. constexpr uint32_t floatValueIndicator = 3;
  36. constexpr uint32_t int16ValueIndicator = 4;
  37. constexpr uint32_t float16ValueIndicator = 5;
  38. using namespace llvm;
  39. using namespace hlsl;
  40. class DxilPIXMeshShaderOutputInstrumentation : public ModulePass
  41. {
  42. public:
  43. static char ID; // Pass identification, replacement for typeid
  44. explicit DxilPIXMeshShaderOutputInstrumentation() : ModulePass(ID) {}
  45. const char *getPassName() const override {
  46. return "DXIL mesh shader output instrumentation";
  47. }
  48. void applyOptions(PassOptions O) override;
  49. bool runOnModule(Module &M) override;
  50. private:
  51. CallInst *m_OutputUAV = nullptr;
  52. int m_RemainingReservedSpaceInBytes = 0;
  53. Constant *m_OffsetMask = nullptr;
  54. uint64_t m_UAVSize = 1024 * 1024;
  55. struct BuilderContext {
  56. Module &M;
  57. DxilModule &DM;
  58. LLVMContext &Ctx;
  59. OP *HlslOP;
  60. IRBuilder<> &Builder;
  61. };
  62. CallInst *addUAV(BuilderContext &BC);
  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. CallInst *DxilPIXMeshShaderOutputInstrumentation::addUAV(BuilderContext &BC)
  80. {
  81. // Set up a UAV with structure of a single int
  82. unsigned int UAVResourceHandle =
  83. static_cast<unsigned int>(BC.DM.GetUAVs().size());
  84. SmallVector<llvm::Type *, 1> Elements{Type::getInt32Ty(BC.Ctx)};
  85. llvm::StructType *UAVStructTy =
  86. llvm::StructType::create(Elements, "PIX_DebugUAV_Type");
  87. std::unique_ptr<DxilResource> pUAV = llvm::make_unique<DxilResource>();
  88. pUAV->SetGlobalName("PIX_DebugUAVName");
  89. pUAV->SetGlobalSymbol(UndefValue::get(UAVStructTy->getPointerTo()));
  90. pUAV->SetID(UAVResourceHandle);
  91. pUAV->SetSpaceID(
  92. (unsigned int)-2); // This is the reserved-for-tools register space
  93. pUAV->SetSampleCount(1);
  94. pUAV->SetGloballyCoherent(false);
  95. pUAV->SetHasCounter(false);
  96. pUAV->SetCompType(CompType::getI32());
  97. pUAV->SetLowerBound(0);
  98. pUAV->SetRangeSize(1);
  99. pUAV->SetKind(DXIL::ResourceKind::RawBuffer);
  100. pUAV->SetRW(true);
  101. auto ID = BC.DM.AddUAV(std::move(pUAV));
  102. assert(ID == UAVResourceHandle);
  103. BC.DM.m_ShaderFlags.SetEnableRawAndStructuredBuffers(true);
  104. // Create handle for the newly-added UAV
  105. Function *CreateHandleOpFunc =
  106. BC.HlslOP->GetOpFunc(DXIL::OpCode::CreateHandle, Type::getVoidTy(BC.Ctx));
  107. Constant *CreateHandleOpcodeArg =
  108. BC.HlslOP->GetU32Const((unsigned)DXIL::OpCode::CreateHandle);
  109. Constant *UAVVArg = BC.HlslOP->GetI8Const(
  110. static_cast<std::underlying_type<DxilResourceBase::Class>::type>(
  111. DXIL::ResourceClass::UAV));
  112. Constant *MetaDataArg = BC.HlslOP->GetU32Const(
  113. ID); // position of the metadata record in the corresponding metadata list
  114. Constant *IndexArg = BC.HlslOP->GetU32Const(0); //
  115. Constant *FalseArg =
  116. BC.HlslOP->GetI1Const(0); // non-uniform resource index: false
  117. return BC.Builder.CreateCall(
  118. CreateHandleOpFunc,
  119. {CreateHandleOpcodeArg, UAVVArg, MetaDataArg, IndexArg, FalseArg},
  120. "PIX_DebugUAV_Handle");
  121. }
  122. Value *DxilPIXMeshShaderOutputInstrumentation::
  123. insertInstructionsToCalculateFlattenedGroupIdXandY(BuilderContext &BC)
  124. {
  125. Constant *Zero32Arg = BC.HlslOP->GetU32Const(0);
  126. Constant *One32Arg = BC.HlslOP->GetU32Const(1);
  127. auto GroupIdFunc =
  128. BC.HlslOP->GetOpFunc(DXIL::OpCode::GroupId, Type::getInt32Ty(BC.Ctx));
  129. Constant *Opcode = BC.HlslOP->GetU32Const((unsigned)DXIL::OpCode::GroupId);
  130. auto GroupIdX =
  131. BC.Builder.CreateCall(GroupIdFunc, {Opcode, Zero32Arg}, "GroupIdX");
  132. auto GroupIdY =
  133. BC.Builder.CreateCall(GroupIdFunc, {Opcode, One32Arg}, "GroupIdY");
  134. // Spec requires that no group id index is greater than 64k, so we can
  135. // combine two into one 32-bit value:
  136. auto YShifted =
  137. BC.Builder.CreateShl(GroupIdY, 16);
  138. return BC.Builder.CreateAdd(YShifted, GroupIdX);
  139. }
  140. Value *DxilPIXMeshShaderOutputInstrumentation::
  141. insertInstructionsToCalculateGroupIdZ(BuilderContext &BC)
  142. {
  143. Constant *Two32Arg = BC.HlslOP->GetU32Const(2);
  144. auto GroupIdFunc =
  145. BC.HlslOP->GetOpFunc(DXIL::OpCode::GroupId, Type::getInt32Ty(BC.Ctx));
  146. Constant *Opcode = BC.HlslOP->GetU32Const((unsigned)DXIL::OpCode::GroupId);
  147. return BC.Builder.CreateCall(GroupIdFunc, {Opcode, Two32Arg}, "GroupIdZ");
  148. }
  149. Value *DxilPIXMeshShaderOutputInstrumentation::reserveDebugEntrySpace(
  150. BuilderContext &BC, uint32_t SpaceInBytes)
  151. {
  152. // Check the previous caller didn't reserve too much space:
  153. assert(m_RemainingReservedSpaceInBytes == 0);
  154. // Check that the caller didn't ask for so much memory that it will
  155. // overwrite the offset counter:
  156. assert(m_RemainingReservedSpaceInBytes < CounterOffsetBeyondUsefulData);
  157. m_RemainingReservedSpaceInBytes = SpaceInBytes;
  158. // Insert the UAV increment instruction:
  159. Function *AtomicOpFunc =
  160. BC.HlslOP->GetOpFunc(OP::OpCode::AtomicBinOp, Type::getInt32Ty(BC.Ctx));
  161. Constant *AtomicBinOpcode =
  162. BC.HlslOP->GetU32Const((unsigned)OP::OpCode::AtomicBinOp);
  163. Constant *AtomicAdd =
  164. BC.HlslOP->GetU32Const((unsigned)DXIL::AtomicBinOpCode::Add);
  165. Constant *OffsetArg =
  166. BC.HlslOP->GetU32Const(UAVDumpingGroundOffset() + CounterOffsetBeyondUsefulData);
  167. UndefValue *UndefArg = UndefValue::get(Type::getInt32Ty(BC.Ctx));
  168. Constant *Increment = BC.HlslOP->GetU32Const(SpaceInBytes);
  169. auto *PreviousValue = BC.Builder.CreateCall(
  170. AtomicOpFunc,
  171. {
  172. AtomicBinOpcode, // i32, ; opcode
  173. m_OutputUAV, // %dx.types.Handle, ; resource handle
  174. AtomicAdd, // i32, ; binary operation code : EXCHANGE, IADD, AND, OR,
  175. // XOR, IMIN, IMAX, UMIN, UMAX
  176. OffsetArg, // i32, ; coordinate c0: index in bytes
  177. UndefArg, // i32, ; coordinate c1 (unused)
  178. UndefArg, // i32, ; coordinate c2 (unused)
  179. Increment, // i32); increment value
  180. },
  181. "UAVIncResult");
  182. return BC.Builder.CreateAnd(PreviousValue, m_OffsetMask, "MaskedForUAVLimit");
  183. }
  184. Value *DxilPIXMeshShaderOutputInstrumentation::writeDwordAndReturnNewOffset(
  185. BuilderContext &BC, Value *TheOffset, Value *TheValue)
  186. {
  187. Function *StoreValue =
  188. BC.HlslOP->GetOpFunc(OP::OpCode::BufferStore, Type::getInt32Ty(BC.Ctx));
  189. Constant *StoreValueOpcode =
  190. BC.HlslOP->GetU32Const((unsigned)DXIL::OpCode::BufferStore);
  191. UndefValue *Undef32Arg = UndefValue::get(Type::getInt32Ty(BC.Ctx));
  192. Constant *WriteMask_X = BC.HlslOP->GetI8Const(1);
  193. (void)BC.Builder.CreateCall(
  194. StoreValue,
  195. {StoreValueOpcode, // i32 opcode
  196. m_OutputUAV, // %dx.types.Handle, ; resource handle
  197. TheOffset, // i32 c0: index in bytes into UAV
  198. Undef32Arg, // i32 c1: unused
  199. TheValue,
  200. Undef32Arg, // unused values
  201. Undef32Arg, // unused values
  202. Undef32Arg, // unused values
  203. WriteMask_X});
  204. m_RemainingReservedSpaceInBytes -= sizeof(uint32_t);
  205. assert(m_RemainingReservedSpaceInBytes >=
  206. 0); // or else the caller didn't reserve enough space
  207. return BC.Builder.CreateAdd(
  208. TheOffset,
  209. BC.HlslOP->GetU32Const(static_cast<unsigned int>(sizeof(uint32_t))));
  210. }
  211. template <typename... T>
  212. void DxilPIXMeshShaderOutputInstrumentation::Instrument(BuilderContext &BC,
  213. T... values)
  214. {
  215. llvm::SmallVector<llvm::Value *, 10> Values(
  216. {static_cast<llvm::Value *>(values)...});
  217. const uint32_t DwordCount = Values.size();
  218. llvm::Value *byteOffset =
  219. reserveDebugEntrySpace(BC, DwordCount * sizeof(uint32_t));
  220. for (llvm::Value *V : Values)
  221. {
  222. byteOffset = writeDwordAndReturnNewOffset(BC, byteOffset, V);
  223. }
  224. }
  225. bool DxilPIXMeshShaderOutputInstrumentation::runOnModule(Module &M)
  226. {
  227. DxilModule &DM = M.GetOrCreateDxilModule();
  228. LLVMContext &Ctx = M.getContext();
  229. OP *HlslOP = DM.GetOP();
  230. Instruction *firstInsertionPt =
  231. dxilutil::FirstNonAllocaInsertionPt(DM.GetEntryFunction());
  232. IRBuilder<> Builder(firstInsertionPt);
  233. BuilderContext BC{M, DM, Ctx, HlslOP, Builder};
  234. m_OffsetMask = BC.HlslOP->GetU32Const(UAVDumpingGroundOffset() - 1);
  235. m_OutputUAV = addUAV(BC);
  236. auto GroupIdXandY = insertInstructionsToCalculateFlattenedGroupIdXandY(BC);
  237. auto GroupIdZ = insertInstructionsToCalculateGroupIdZ(BC);
  238. auto F = HlslOP->GetOpFunc(DXIL::OpCode::EmitIndices, Type::getVoidTy(Ctx));
  239. auto FunctionUses = F->uses();
  240. for (auto FI = FunctionUses.begin(); FI != FunctionUses.end();)
  241. {
  242. auto &FunctionUse = *FI++;
  243. auto FunctionUser = FunctionUse.getUser();
  244. auto Call = cast<CallInst>(FunctionUser);
  245. IRBuilder<> Builder2(Call);
  246. BuilderContext BC2{M, DM, Ctx, HlslOP, Builder2};
  247. Instrument(BC2, BC2.HlslOP->GetI32Const(triangleIndexIndicator),
  248. GroupIdXandY, GroupIdZ, Call->getOperand(1),
  249. Call->getOperand(2), Call->getOperand(3), Call->getOperand(4));
  250. }
  251. struct OutputType
  252. {
  253. Type *type;
  254. uint32_t tag;
  255. };
  256. SmallVector<OutputType, 4> StoreVertexOutputOverloads
  257. {
  258. {Type::getInt32Ty(Ctx), int32ValueIndicator},
  259. {Type::getInt16Ty(Ctx), int16ValueIndicator},
  260. {Type::getFloatTy(Ctx), floatValueIndicator},
  261. {Type::getHalfTy(Ctx), float16ValueIndicator}
  262. };
  263. for (auto const &Overload : StoreVertexOutputOverloads)
  264. {
  265. F = HlslOP->GetOpFunc(DXIL::OpCode::StoreVertexOutput, Overload.type);
  266. FunctionUses = F->uses();
  267. for (auto FI = FunctionUses.begin(); FI != FunctionUses.end();)
  268. {
  269. auto &FunctionUse = *FI++;
  270. auto FunctionUser = FunctionUse.getUser();
  271. auto Call = cast<CallInst>(FunctionUser);
  272. IRBuilder<> Builder2(Call);
  273. BuilderContext BC2{M, DM, Ctx, HlslOP, Builder2};
  274. // Expand column index to 32 bits:
  275. auto ColumnIndex = BC2.Builder.CreateCast(
  276. Instruction::ZExt,
  277. Call->getOperand(3),
  278. Type::getInt32Ty(Ctx));
  279. // Coerce actual value to int32
  280. Value *CoercedValue = Call->getOperand(4);
  281. if (Overload.tag == floatValueIndicator)
  282. {
  283. CoercedValue = BC2.Builder.CreateCast(
  284. Instruction::BitCast,
  285. CoercedValue,
  286. Type::getInt32Ty(Ctx));
  287. }
  288. else if (Overload.tag == float16ValueIndicator)
  289. {
  290. auto * HalfInt = BC2.Builder.CreateCast(
  291. Instruction::BitCast,
  292. CoercedValue,
  293. Type::getInt16Ty(Ctx));
  294. CoercedValue = BC2.Builder.CreateCast(
  295. Instruction::ZExt,
  296. HalfInt,
  297. Type::getInt32Ty(Ctx));
  298. }
  299. else if (Overload.tag == int16ValueIndicator)
  300. {
  301. CoercedValue = BC2.Builder.CreateCast(
  302. Instruction::ZExt,
  303. CoercedValue,
  304. Type::getInt32Ty(Ctx));
  305. }
  306. Instrument(
  307. BC2,
  308. BC2.HlslOP->GetI32Const(Overload.tag),
  309. GroupIdXandY,
  310. GroupIdZ,
  311. Call->getOperand(1),
  312. Call->getOperand(2),
  313. ColumnIndex,
  314. CoercedValue,
  315. Call->getOperand(5));
  316. }
  317. }
  318. DM.ReEmitDxilResources();
  319. return true;
  320. }
  321. char DxilPIXMeshShaderOutputInstrumentation::ID = 0;
  322. ModulePass *llvm::createDxilDxilPIXMeshShaderOutputInstrumentation()
  323. {
  324. return new DxilPIXMeshShaderOutputInstrumentation();
  325. }
  326. INITIALIZE_PASS(DxilPIXMeshShaderOutputInstrumentation,
  327. "hlsl-dxil-pix-meshshader-output-instrumentation",
  328. "DXIL mesh shader output instrumentation for PIX", false, false)