DxilPIXMeshShaderOutputInstrumentation.cpp 13 KB

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