DxilModule.cpp 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // //
  3. // DxilModule.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/Support/Global.h"
  10. #include "dxc/HLSL/DxilOperations.h"
  11. #include "dxc/HLSL/DxilModule.h"
  12. #include "dxc/HLSL/DxilShaderModel.h"
  13. #include "dxc/HLSL/DxilSignatureElement.h"
  14. #include "dxc/HLSL/DxilContainer.h"
  15. #include "dxc/HLSL/DxilRootSignature.h"
  16. #include "llvm/IR/Constants.h"
  17. #include "llvm/IR/Function.h"
  18. #include "llvm/IR/Instructions.h"
  19. #include "llvm/IR/LLVMContext.h"
  20. #include "llvm/IR/Metadata.h"
  21. #include "llvm/IR/Module.h"
  22. #include "llvm/IR/DebugInfo.h"
  23. #include "llvm/IR/DiagnosticInfo.h"
  24. #include "llvm/IR/DiagnosticPrinter.h"
  25. #include "llvm/Support/raw_ostream.h"
  26. #include <unordered_set>
  27. using namespace llvm;
  28. using std::string;
  29. using std::vector;
  30. using std::unique_ptr;
  31. namespace {
  32. class DxilErrorDiagnosticInfo : public DiagnosticInfo {
  33. private:
  34. const char *m_message;
  35. public:
  36. DxilErrorDiagnosticInfo(const char *str)
  37. : DiagnosticInfo(DK_FirstPluginKind, DiagnosticSeverity::DS_Error),
  38. m_message(str) { }
  39. __override void print(DiagnosticPrinter &DP) const {
  40. DP << m_message;
  41. }
  42. };
  43. } // anon namespace
  44. namespace hlsl {
  45. //------------------------------------------------------------------------------
  46. //
  47. // DxilModule methods.
  48. //
  49. DxilModule::DxilModule(Module *pModule)
  50. : m_Ctx(pModule->getContext())
  51. , m_pModule(pModule)
  52. , m_pOP(std::make_unique<OP>(pModule->getContext(), pModule))
  53. , m_pTypeSystem(std::make_unique<DxilTypeSystem>(pModule))
  54. , m_pMDHelper(std::make_unique<DxilMDHelper>(pModule, std::make_unique<DxilExtraPropertyHelper>(pModule)))
  55. , m_pDebugInfoFinder(nullptr)
  56. , m_pEntryFunc(nullptr)
  57. , m_EntryName("")
  58. , m_pPatchConstantFunc(nullptr)
  59. , m_pSM(nullptr)
  60. , m_DxilMajor(DXIL::kDxilMajor)
  61. , m_DxilMinor(DXIL::kDxilMinor)
  62. , m_InputPrimitive(DXIL::InputPrimitive::Undefined)
  63. , m_MaxVertexCount(0)
  64. , m_StreamPrimitiveTopology(DXIL::PrimitiveTopology::Undefined)
  65. , m_ActiveStreamMask(0)
  66. , m_NumGSInstances(1)
  67. , m_InputControlPointCount(0)
  68. , m_TessellatorDomain(DXIL::TessellatorDomain::Undefined)
  69. , m_OutputControlPointCount(0)
  70. , m_TessellatorPartitioning(DXIL::TessellatorPartitioning::Undefined)
  71. , m_TessellatorOutputPrimitive(DXIL::TessellatorOutputPrimitive::Undefined)
  72. , m_MaxTessellationFactor(0.f)
  73. , m_RootSignature(nullptr) {
  74. DXASSERT_NOMSG(m_pModule != nullptr);
  75. m_NumThreads[0] = m_NumThreads[1] = m_NumThreads[2] = 0;
  76. #if defined(_DEBUG) || defined(DBG)
  77. // Pin LLVM dump methods.
  78. void (__thiscall Module::*pfnModuleDump)() const = &Module::dump;
  79. void (__thiscall Type::*pfnTypeDump)() const = &Type::dump;
  80. void (__thiscall Function::*pfnViewCFGOnly)() const = &Function::viewCFGOnly;
  81. m_pUnused = (char *)&pfnModuleDump - (char *)&pfnTypeDump;
  82. m_pUnused -= (size_t)&pfnViewCFGOnly;
  83. #endif
  84. }
  85. DxilModule::~DxilModule() {
  86. }
  87. DxilModule::ShaderFlags::ShaderFlags():
  88. m_bDisableOptimizations(false)
  89. , m_bDisableMathRefactoring(false)
  90. , m_bEnableDoublePrecision(false)
  91. , m_bForceEarlyDepthStencil(false)
  92. , m_bEnableRawAndStructuredBuffers(false)
  93. , m_bEnableMinPrecision(false)
  94. , m_bEnableDoubleExtensions(false)
  95. , m_bEnableMSAD(false)
  96. , m_bAllResourcesBound(false)
  97. , m_bViewportAndRTArrayIndex(false)
  98. , m_bInnerCoverage(false)
  99. , m_bStencilRef(false)
  100. , m_bTiledResources(false)
  101. , m_bUAVLoadAdditionalFormats(false)
  102. , m_bLevel9ComparisonFiltering(false)
  103. , m_bCSRawAndStructuredViaShader4X(false)
  104. , m_b64UAVs(false)
  105. , m_UAVsAtEveryStage(false)
  106. , m_bROVS(false)
  107. , m_bWaveOps(false)
  108. , m_bInt64Ops(false)
  109. , m_align0(0)
  110. , m_align1(0)
  111. {}
  112. LLVMContext &DxilModule::GetCtx() const { return m_Ctx; }
  113. Module *DxilModule::GetModule() const { return m_pModule; }
  114. OP *DxilModule::GetOP() const { return m_pOP.get(); }
  115. void DxilModule::SetShaderModel(const ShaderModel *pSM) {
  116. DXASSERT(m_pSM == nullptr || (pSM != nullptr && *m_pSM == *pSM), "shader model must not change for the module");
  117. m_pSM = pSM;
  118. m_pMDHelper->SetShaderModel(m_pSM);
  119. DXIL::ShaderKind shaderKind = pSM->GetKind();
  120. m_InputSignature.reset(new DxilSignature(shaderKind, DXIL::SignatureKind::Input));
  121. m_OutputSignature.reset(new DxilSignature(shaderKind, DXIL::SignatureKind::Output));
  122. m_PatchConstantSignature.reset(new DxilSignature(shaderKind, DXIL::SignatureKind::PatchConstant));
  123. m_RootSignature.reset(new RootSignatureHandle());
  124. }
  125. const ShaderModel *DxilModule::GetShaderModel() const {
  126. return m_pSM;
  127. }
  128. Function *DxilModule::GetEntryFunction() {
  129. return m_pEntryFunc;
  130. }
  131. const Function *DxilModule::GetEntryFunction() const {
  132. return m_pEntryFunc;
  133. }
  134. void DxilModule::SetEntryFunction(Function *pEntryFunc) {
  135. m_pEntryFunc = pEntryFunc;
  136. }
  137. const string &DxilModule::GetEntryFunctionName() const {
  138. return m_EntryName;
  139. }
  140. void DxilModule::SetEntryFunctionName(const string &name) {
  141. m_EntryName = name;
  142. }
  143. llvm::Function *DxilModule::GetPatchConstantFunction() {
  144. return m_pPatchConstantFunc;
  145. }
  146. const llvm::Function *DxilModule::GetPatchConstantFunction() const {
  147. return m_pPatchConstantFunc;
  148. }
  149. void DxilModule::SetPatchConstantFunction(llvm::Function *pFunc) {
  150. m_pPatchConstantFunc = pFunc;
  151. }
  152. unsigned DxilModule::ShaderFlags::GetGlobalFlags() const {
  153. unsigned Flags = 0;
  154. Flags |= m_bDisableOptimizations ? DXIL::kDisableOptimizations : 0;
  155. Flags |= m_bDisableMathRefactoring ? DXIL::kDisableMathRefactoring : 0;
  156. Flags |= m_bEnableDoublePrecision ? DXIL::kEnableDoublePrecision : 0;
  157. Flags |= m_bForceEarlyDepthStencil ? DXIL::kForceEarlyDepthStencil : 0;
  158. Flags |= m_bEnableRawAndStructuredBuffers ? DXIL::kEnableRawAndStructuredBuffers : 0;
  159. Flags |= m_bEnableMinPrecision ? DXIL::kEnableMinPrecision : 0;
  160. Flags |= m_bEnableDoubleExtensions ? DXIL::kEnableDoubleExtensions : 0;
  161. Flags |= m_bEnableMSAD ? DXIL::kEnableMSAD : 0;
  162. Flags |= m_bAllResourcesBound ? DXIL::kAllResourcesBound : 0;
  163. return Flags;
  164. }
  165. uint64_t DxilModule::ShaderFlags::GetFeatureInfo() const {
  166. uint64_t Flags = 0;
  167. Flags |= m_bEnableDoublePrecision ? hlsl::ShaderFeatureInfo_Doubles : 0;
  168. Flags |= m_bEnableMinPrecision ? hlsl::ShaderFeatureInfo_MininumPrecision : 0;
  169. Flags |= m_bEnableDoubleExtensions ? hlsl::ShaderFeatureInfo_11_1_DoubleExtensions : 0;
  170. Flags |= m_bWaveOps ? hlsl::ShaderFeatureInfo_WaveOps : 0;
  171. Flags |= m_bInt64Ops ? hlsl::ShaderFeatureInfo_Int64Ops : 0;
  172. Flags |= m_bROVS ? hlsl::ShaderFeatureInfo_ROVs : 0;
  173. Flags |= m_bViewportAndRTArrayIndex ? hlsl::ShaderFeatureInfo_ViewportAndRTArrayIndexFromAnyShaderFeedingRasterizer : 0;
  174. Flags |= m_bInnerCoverage ? hlsl::ShaderFeatureInfo_InnerCoverage : 0;
  175. Flags |= m_bStencilRef ? hlsl::ShaderFeatureInfo_StencilRef : 0;
  176. Flags |= m_bTiledResources ? hlsl::ShaderFeatureInfo_TiledResources : 0;
  177. Flags |= m_bEnableMSAD ? hlsl::ShaderFeatureInfo_11_1_ShaderExtensions : 0;
  178. Flags |= m_bCSRawAndStructuredViaShader4X ? hlsl::ShaderFeatureInfo_ComputeShadersPlusRawAndStructuredBuffersViaShader4X : 0;
  179. Flags |= m_UAVsAtEveryStage ? hlsl::ShaderFeatureInfo_UAVsAtEveryStage : 0;
  180. Flags |= m_b64UAVs ? hlsl::ShaderFeatureInfo_64UAVs : 0;
  181. Flags |= m_bLevel9ComparisonFiltering ? hlsl::ShaderFeatureInfo_LEVEL9ComparisonFiltering : 0;
  182. Flags |= m_bUAVLoadAdditionalFormats ? hlsl::ShaderFeatureInfo_TypedUAVLoadAdditionalFormats : 0;
  183. return Flags;
  184. }
  185. uint64_t DxilModule::ShaderFlags::GetShaderFlagsRaw() const {
  186. union Cast {
  187. Cast(const DxilModule::ShaderFlags &flags) {
  188. shaderFlags = flags;
  189. }
  190. DxilModule::ShaderFlags shaderFlags;
  191. uint64_t rawData;
  192. };
  193. static_assert(sizeof(uint64_t) == sizeof(DxilModule::ShaderFlags),
  194. "size must match to make sure no undefined bits when cast");
  195. Cast rawCast(*this);
  196. return rawCast.rawData;
  197. }
  198. void DxilModule::ShaderFlags::SetShaderFlagsRaw(uint64_t data) {
  199. union Cast {
  200. Cast(uint64_t data) {
  201. rawData = data;
  202. }
  203. DxilModule::ShaderFlags shaderFlags;
  204. uint64_t rawData;
  205. };
  206. Cast rawCast(data);
  207. *this = rawCast.shaderFlags;
  208. }
  209. unsigned DxilModule::GetGlobalFlags() const {
  210. unsigned Flags = m_ShaderFlags.GetGlobalFlags();
  211. return Flags;
  212. }
  213. void DxilModule::CollectShaderFlags(ShaderFlags &Flags) {
  214. bool hasDouble = false;
  215. // ddiv dfma drcp d2i d2u i2d u2d.
  216. // fma has dxil op. Others should check IR instruction div/cast.
  217. bool hasDoubleExtension = false;
  218. bool has64Int = false;
  219. bool has16FloatInt = false;
  220. bool hasWaveOps = false;
  221. bool hasCheckAccessFully = false;
  222. bool hasMSAD = false;
  223. bool hasMulticomponentUAVLoads = false;
  224. bool hasInnerCoverage = false;
  225. Type *int16Ty = Type::getInt16Ty(GetCtx());
  226. Type *int64Ty = Type::getInt64Ty(GetCtx());
  227. for (Function &F : GetModule()->functions()) {
  228. for (BasicBlock &BB : F.getBasicBlockList()) {
  229. for (Instruction &I : BB.getInstList()) {
  230. // Skip none dxil function call.
  231. if (CallInst *CI = dyn_cast<CallInst>(&I)) {
  232. if (!OP::IsDxilOpFunc(CI->getCalledFunction()))
  233. continue;
  234. }
  235. Type *Ty = I.getType();
  236. bool isDouble = Ty->isDoubleTy();
  237. bool isHalf = Ty->isHalfTy();
  238. bool isInt16 = Ty == int16Ty;
  239. bool isInt64 = Ty == int64Ty;
  240. if (isa<ExtractElementInst>(&I) ||
  241. isa<InsertElementInst>(&I))
  242. continue;
  243. for (Value *operand : I.operands()) {
  244. Type *Ty = operand->getType();
  245. isDouble |= Ty->isDoubleTy();
  246. isHalf |= Ty->isHalfTy();
  247. isInt16 |= Ty == int16Ty;
  248. isInt64 |= Ty == int64Ty;
  249. }
  250. if (isDouble) {
  251. hasDouble = true;
  252. switch (I.getOpcode()) {
  253. case Instruction::FDiv:
  254. case Instruction::UIToFP:
  255. case Instruction::SIToFP:
  256. case Instruction::FPToUI:
  257. case Instruction::FPToSI:
  258. hasDoubleExtension = true;
  259. break;
  260. }
  261. }
  262. has16FloatInt |= isHalf;
  263. has16FloatInt |= isInt16;
  264. has64Int |= isInt64;
  265. if (CallInst *CI = dyn_cast<CallInst>(&I)) {
  266. if (!OP::IsDxilOpFunc(CI->getCalledFunction()))
  267. continue;
  268. Value *opcodeArg = CI->getArgOperand(DXIL::OperandIndex::kOpcodeIdx);
  269. ConstantInt *opcodeConst = dyn_cast<ConstantInt>(opcodeArg);
  270. DXASSERT(opcodeConst, "DXIL opcode arg must be immediate");
  271. unsigned opcode = opcodeConst->getLimitedValue();
  272. DXASSERT(opcode < static_cast<unsigned>(DXIL::OpCode::NumOpCodes),
  273. "invalid DXIL opcode");
  274. DXIL::OpCode dxilOp = static_cast<DXIL::OpCode>(opcode);
  275. if (hlsl::OP::IsDxilOpWave(dxilOp))
  276. hasWaveOps = true;
  277. switch (dxilOp) {
  278. case DXIL::OpCode::CheckAccessFullyMapped:
  279. hasCheckAccessFully = true;
  280. break;
  281. case DXIL::OpCode::Msad:
  282. hasMSAD = true;
  283. break;
  284. case DXIL::OpCode::BufferLoad:
  285. case DXIL::OpCode::TextureLoad: {
  286. Value *resHandle = CI->getArgOperand(DXIL::OperandIndex::kBufferStoreHandleOpIdx);
  287. CallInst *handleCall = cast<CallInst>(resHandle);
  288. if (ConstantInt *resClassArg =
  289. dyn_cast<ConstantInt>(handleCall->getArgOperand(
  290. DXIL::OperandIndex::kCreateHandleResClassOpIdx))) {
  291. DXIL::ResourceClass resClass = static_cast<DXIL::ResourceClass>(
  292. resClassArg->getLimitedValue());
  293. if (resClass == DXIL::ResourceClass::UAV) {
  294. // For DXIL, all uav load is multi component load.
  295. hasMulticomponentUAVLoads = true;
  296. }
  297. } else if (PHINode *resClassPhi = dyn_cast<
  298. PHINode>(handleCall->getArgOperand(
  299. DXIL::OperandIndex::kCreateHandleResClassOpIdx))) {
  300. unsigned numOperands = resClassPhi->getNumOperands();
  301. for (unsigned i = 0; i < numOperands; i++) {
  302. if (ConstantInt *resClassArg = dyn_cast<ConstantInt>(
  303. resClassPhi->getIncomingValue(i))) {
  304. DXIL::ResourceClass resClass =
  305. static_cast<DXIL::ResourceClass>(
  306. resClassArg->getLimitedValue());
  307. if (resClass == DXIL::ResourceClass::UAV) {
  308. // For DXIL, all uav load is multi component load.
  309. hasMulticomponentUAVLoads = true;
  310. break;
  311. }
  312. }
  313. }
  314. }
  315. } break;
  316. case DXIL::OpCode::Fma:
  317. hasDoubleExtension |= isDouble;
  318. break;
  319. case DXIL::OpCode::InnerCoverage:
  320. hasInnerCoverage = true;
  321. break;
  322. default:
  323. // Normal opcodes.
  324. break;
  325. }
  326. }
  327. }
  328. }
  329. }
  330. Flags.SetEnableDoublePrecision(hasDouble);
  331. Flags.SetInt64Ops(has64Int);
  332. Flags.SetEnableMinPrecision(has16FloatInt);
  333. Flags.SetEnableDoubleExtensions(hasDoubleExtension);
  334. Flags.SetWaveOps(hasWaveOps);
  335. Flags.SetTiledResources(hasCheckAccessFully);
  336. Flags.SetEnableMSAD(hasMSAD);
  337. Flags.SetUAVLoadAdditionalFormats(hasMulticomponentUAVLoads);
  338. const ShaderModel *SM = GetShaderModel();
  339. if (SM->IsPS()) {
  340. bool hasStencilRef = false;
  341. DxilSignature &outS = GetOutputSignature();
  342. for (auto &&E : outS.GetElements()) {
  343. if (E->GetKind() == Semantic::Kind::StencilRef) {
  344. hasStencilRef = true;
  345. } else if (E->GetKind() == Semantic::Kind::InnerCoverage) {
  346. hasInnerCoverage = true;
  347. }
  348. }
  349. Flags.SetStencilRef(hasStencilRef);
  350. Flags.SetInnerCoverage(hasInnerCoverage);
  351. }
  352. bool checkInputRTArrayIndex =
  353. SM->IsGS() || SM->IsDS() || SM->IsHS() || SM->IsPS();
  354. if (checkInputRTArrayIndex) {
  355. bool hasViewportArrayIndex = false;
  356. bool hasRenderTargetArrayIndex = false;
  357. DxilSignature &inS = GetInputSignature();
  358. for (auto &E : inS.GetElements()) {
  359. if (E->GetKind() == Semantic::Kind::ViewPortArrayIndex) {
  360. hasViewportArrayIndex = true;
  361. } else if (E->GetKind() == Semantic::Kind::RenderTargetArrayIndex) {
  362. hasRenderTargetArrayIndex = true;
  363. }
  364. }
  365. Flags.SetViewportAndRTArrayIndex(hasViewportArrayIndex |
  366. hasRenderTargetArrayIndex);
  367. }
  368. bool checkOutputRTArrayIndex =
  369. SM->IsVS() || SM->IsDS() || SM->IsHS() || SM->IsPS();
  370. if (checkOutputRTArrayIndex) {
  371. bool hasViewportArrayIndex = false;
  372. bool hasRenderTargetArrayIndex = false;
  373. DxilSignature &outS = GetOutputSignature();
  374. for (auto &E : outS.GetElements()) {
  375. if (E->GetKind() == Semantic::Kind::ViewPortArrayIndex) {
  376. hasViewportArrayIndex = true;
  377. } else if (E->GetKind() == Semantic::Kind::RenderTargetArrayIndex) {
  378. hasRenderTargetArrayIndex = true;
  379. }
  380. }
  381. Flags.SetViewportAndRTArrayIndex(hasViewportArrayIndex |
  382. hasRenderTargetArrayIndex);
  383. }
  384. unsigned NumUAVs = m_UAVs.size();
  385. const unsigned kSmallUAVCount = 8;
  386. if (NumUAVs > kSmallUAVCount)
  387. Flags.Set64UAVs(true);
  388. if (NumUAVs && !(SM->IsCS() || SM->IsPS()))
  389. Flags.SetUAVsAtEveryStage(true);
  390. bool hasRawAndStructuredBuffer = false;
  391. for (auto &UAV : m_UAVs) {
  392. if (UAV->IsROV())
  393. Flags.SetROVs(true);
  394. switch (UAV->GetKind()) {
  395. case DXIL::ResourceKind::RawBuffer:
  396. case DXIL::ResourceKind::StructuredBuffer:
  397. hasRawAndStructuredBuffer = true;
  398. break;
  399. default:
  400. // Not raw/structured.
  401. break;
  402. }
  403. }
  404. for (auto &SRV : m_SRVs) {
  405. switch (SRV->GetKind()) {
  406. case DXIL::ResourceKind::RawBuffer:
  407. case DXIL::ResourceKind::StructuredBuffer:
  408. hasRawAndStructuredBuffer = true;
  409. break;
  410. default:
  411. // Not raw/structured.
  412. break;
  413. }
  414. }
  415. Flags.SetEnableRawAndStructuredBuffers(hasRawAndStructuredBuffer);
  416. bool hasCSRawAndStructuredViaShader4X =
  417. hasRawAndStructuredBuffer && m_pSM->GetMajor() == 4 && m_pSM->IsCS();
  418. Flags.SetCSRawAndStructuredViaShader4X(hasCSRawAndStructuredViaShader4X);
  419. }
  420. void DxilModule::CollectShaderFlags() {
  421. CollectShaderFlags(m_ShaderFlags);
  422. }
  423. uint64_t DxilModule::ShaderFlags::GetShaderFlagsRawForCollection() {
  424. // This should be all the flags that can be set by DxilModule::CollectShaderFlags.
  425. ShaderFlags Flags;
  426. Flags.SetEnableDoublePrecision(true);
  427. Flags.SetInt64Ops(true);
  428. Flags.SetEnableMinPrecision(true);
  429. Flags.SetEnableDoubleExtensions(true);
  430. Flags.SetWaveOps(true);
  431. Flags.SetTiledResources(true);
  432. Flags.SetEnableMSAD(true);
  433. Flags.SetUAVLoadAdditionalFormats(true);
  434. Flags.SetStencilRef(true);
  435. Flags.SetInnerCoverage(true);
  436. Flags.SetViewportAndRTArrayIndex(true);
  437. Flags.Set64UAVs(true);
  438. Flags.SetUAVsAtEveryStage(true);
  439. Flags.SetEnableRawAndStructuredBuffers(true);
  440. Flags.SetCSRawAndStructuredViaShader4X(true);
  441. return Flags.GetShaderFlagsRaw();
  442. }
  443. DXIL::InputPrimitive DxilModule::GetInputPrimitive() const {
  444. return m_InputPrimitive;
  445. }
  446. void DxilModule::SetInputPrimitive(DXIL::InputPrimitive IP) {
  447. DXASSERT_NOMSG(m_InputPrimitive == DXIL::InputPrimitive::Undefined);
  448. DXASSERT_NOMSG(DXIL::InputPrimitive::Undefined < IP && IP < DXIL::InputPrimitive::LastEntry);
  449. m_InputPrimitive = IP;
  450. }
  451. unsigned DxilModule::GetMaxVertexCount() const {
  452. DXASSERT_NOMSG(m_MaxVertexCount != 0);
  453. return m_MaxVertexCount;
  454. }
  455. void DxilModule::SetMaxVertexCount(unsigned Count) {
  456. DXASSERT_NOMSG(m_MaxVertexCount == 0);
  457. m_MaxVertexCount = Count;
  458. }
  459. DXIL::PrimitiveTopology DxilModule::GetStreamPrimitiveTopology() const {
  460. return m_StreamPrimitiveTopology;
  461. }
  462. void DxilModule::SetStreamPrimitiveTopology(DXIL::PrimitiveTopology Topology) {
  463. m_StreamPrimitiveTopology = Topology;
  464. }
  465. bool DxilModule::HasMultipleOutputStreams() const {
  466. if (!m_pSM->IsGS()) {
  467. return false;
  468. } else {
  469. unsigned NumStreams = (m_ActiveStreamMask & 0x1) +
  470. ((m_ActiveStreamMask & 0x2) >> 1) +
  471. ((m_ActiveStreamMask & 0x4) >> 2) +
  472. ((m_ActiveStreamMask & 0x8) >> 3);
  473. DXASSERT_NOMSG(NumStreams <= DXIL::kNumOutputStreams);
  474. return NumStreams > 1;
  475. }
  476. }
  477. unsigned DxilModule::GetOutputStream() const {
  478. if (!m_pSM->IsGS()) {
  479. return 0;
  480. } else {
  481. DXASSERT_NOMSG(!HasMultipleOutputStreams());
  482. switch (m_ActiveStreamMask) {
  483. case 0x1: return 0;
  484. case 0x2: return 1;
  485. case 0x4: return 2;
  486. case 0x8: return 3;
  487. default: DXASSERT_NOMSG(false);
  488. }
  489. return (unsigned)(-1);
  490. }
  491. }
  492. unsigned DxilModule::GetGSInstanceCount() const {
  493. return m_NumGSInstances;
  494. }
  495. void DxilModule::SetGSInstanceCount(unsigned Count) {
  496. m_NumGSInstances = Count;
  497. }
  498. bool DxilModule::IsStreamActive(unsigned Stream) const {
  499. return (m_ActiveStreamMask & (1<<Stream)) != 0;
  500. }
  501. void DxilModule::SetStreamActive(unsigned Stream, bool bActive) {
  502. if (bActive) {
  503. m_ActiveStreamMask |= (1<<Stream);
  504. } else {
  505. m_ActiveStreamMask &= ~(1<<Stream);
  506. }
  507. }
  508. void DxilModule::SetActiveStreamMask(unsigned Mask) {
  509. m_ActiveStreamMask = Mask;
  510. }
  511. unsigned DxilModule::GetActiveStreamMask() const {
  512. return m_ActiveStreamMask;
  513. }
  514. unsigned DxilModule::GetInputControlPointCount() const {
  515. return m_InputControlPointCount;
  516. }
  517. void DxilModule::SetInputControlPointCount(unsigned NumICPs) {
  518. m_InputControlPointCount = NumICPs;
  519. }
  520. DXIL::TessellatorDomain DxilModule::GetTessellatorDomain() const {
  521. return m_TessellatorDomain;
  522. }
  523. void DxilModule::SetTessellatorDomain(DXIL::TessellatorDomain TessDomain) {
  524. m_TessellatorDomain = TessDomain;
  525. }
  526. unsigned DxilModule::GetOutputControlPointCount() const {
  527. return m_OutputControlPointCount;
  528. }
  529. void DxilModule::SetOutputControlPointCount(unsigned NumOCPs) {
  530. m_OutputControlPointCount = NumOCPs;
  531. }
  532. DXIL::TessellatorPartitioning DxilModule::GetTessellatorPartitioning() const {
  533. return m_TessellatorPartitioning;
  534. }
  535. void DxilModule::SetTessellatorPartitioning(DXIL::TessellatorPartitioning TessPartitioning) {
  536. m_TessellatorPartitioning = TessPartitioning;
  537. }
  538. DXIL::TessellatorOutputPrimitive DxilModule::GetTessellatorOutputPrimitive() const {
  539. return m_TessellatorOutputPrimitive;
  540. }
  541. void DxilModule::SetTessellatorOutputPrimitive(DXIL::TessellatorOutputPrimitive TessOutputPrimitive) {
  542. m_TessellatorOutputPrimitive = TessOutputPrimitive;
  543. }
  544. float DxilModule::GetMaxTessellationFactor() const {
  545. return m_MaxTessellationFactor;
  546. }
  547. void DxilModule::SetMaxTessellationFactor(float MaxTessellationFactor) {
  548. m_MaxTessellationFactor = MaxTessellationFactor;
  549. }
  550. template<typename T> unsigned
  551. DxilModule::AddResource(vector<unique_ptr<T> > &Vec, unique_ptr<T> pRes) {
  552. DXASSERT_NOMSG((unsigned)Vec.size() < UINT_MAX);
  553. unsigned Id = (unsigned)Vec.size();
  554. Vec.emplace_back(std::move(pRes));
  555. return Id;
  556. }
  557. unsigned DxilModule::AddCBuffer(unique_ptr<DxilCBuffer> pCB) {
  558. return AddResource<DxilCBuffer>(m_CBuffers, std::move(pCB));
  559. }
  560. DxilCBuffer &DxilModule::GetCBuffer(unsigned idx) {
  561. return *m_CBuffers[idx];
  562. }
  563. const DxilCBuffer &DxilModule::GetCBuffer(unsigned idx) const {
  564. return *m_CBuffers[idx];
  565. }
  566. const vector<unique_ptr<DxilCBuffer> > &DxilModule::GetCBuffers() const {
  567. return m_CBuffers;
  568. }
  569. unsigned DxilModule::AddSampler(unique_ptr<DxilSampler> pSampler) {
  570. return AddResource<DxilSampler>(m_Samplers, std::move(pSampler));
  571. }
  572. DxilSampler &DxilModule::GetSampler(unsigned idx) {
  573. return *m_Samplers[idx];
  574. }
  575. const DxilSampler &DxilModule::GetSampler(unsigned idx) const {
  576. return *m_Samplers[idx];
  577. }
  578. const vector<unique_ptr<DxilSampler> > &DxilModule::GetSamplers() const {
  579. return m_Samplers;
  580. }
  581. unsigned DxilModule::AddSRV(unique_ptr<DxilResource> pSRV) {
  582. return AddResource<DxilResource>(m_SRVs, std::move(pSRV));
  583. }
  584. DxilResource &DxilModule::GetSRV(unsigned idx) {
  585. return *m_SRVs[idx];
  586. }
  587. const DxilResource &DxilModule::GetSRV(unsigned idx) const {
  588. return *m_SRVs[idx];
  589. }
  590. const vector<unique_ptr<DxilResource> > &DxilModule::GetSRVs() const {
  591. return m_SRVs;
  592. }
  593. unsigned DxilModule::AddUAV(unique_ptr<DxilResource> pUAV) {
  594. return AddResource<DxilResource>(m_UAVs, std::move(pUAV));
  595. }
  596. DxilResource &DxilModule::GetUAV(unsigned idx) {
  597. return *m_UAVs[idx];
  598. }
  599. const DxilResource &DxilModule::GetUAV(unsigned idx) const {
  600. return *m_UAVs[idx];
  601. }
  602. const vector<unique_ptr<DxilResource> > &DxilModule::GetUAVs() const {
  603. return m_UAVs;
  604. }
  605. template <typename TResource>
  606. static void RemoveResources(std::vector<std::unique_ptr<TResource>> &vec,
  607. std::unordered_set<unsigned> &immResID) {
  608. for (std::vector<std::unique_ptr<TResource>>::iterator p = vec.begin(); p != vec.end();) {
  609. std::vector<std::unique_ptr<TResource>>::iterator c = p++;
  610. if (immResID.count((*c)->GetID()) == 0) {
  611. p = vec.erase(c);
  612. }
  613. }
  614. }
  615. static void CollectUsedResource(Value *resID,
  616. std::unordered_set<Value *> &usedResID) {
  617. if (usedResID.count(resID) > 0)
  618. return;
  619. usedResID.insert(resID);
  620. if (ConstantInt *cResID = dyn_cast<ConstantInt>(resID)) {
  621. // Do nothing
  622. } else if (ZExtInst *ZEI = dyn_cast<ZExtInst>(resID)) {
  623. if (ZEI->getSrcTy()->isIntegerTy()) {
  624. IntegerType *ITy = cast<IntegerType>(ZEI->getSrcTy());
  625. if (ITy->getBitWidth() == 1) {
  626. usedResID.insert(ConstantInt::get(ZEI->getDestTy(), 0));
  627. usedResID.insert(ConstantInt::get(ZEI->getDestTy(), 1));
  628. }
  629. }
  630. } else if (SelectInst *SI = dyn_cast<SelectInst>(resID)) {
  631. CollectUsedResource(SI->getTrueValue(), usedResID);
  632. CollectUsedResource(SI->getFalseValue(), usedResID);
  633. } else {
  634. PHINode *Phi = cast<PHINode>(resID);
  635. for (Use &U : Phi->incoming_values()) {
  636. CollectUsedResource(U.get(), usedResID);
  637. }
  638. }
  639. }
  640. static void ConvertUsedResource(std::unordered_set<unsigned> &immResID,
  641. std::unordered_set<Value *> &usedResID) {
  642. for (Value *V : usedResID) {
  643. if (ConstantInt *cResID = dyn_cast<ConstantInt>(V)) {
  644. immResID.insert(cResID->getLimitedValue());
  645. }
  646. }
  647. }
  648. void DxilModule::RemoveFunction(llvm::Function *F) {
  649. DXASSERT_NOMSG(F != nullptr);
  650. if (m_pTypeSystem.get()->GetFunctionAnnotation(F))
  651. m_pTypeSystem.get()->EraseFunctionAnnotation(F);
  652. m_pOP->RemoveFunction(F);
  653. }
  654. void DxilModule::RemoveUnusedResources() {
  655. hlsl::OP *hlslOP = GetOP();
  656. Function *createHandleFunc = hlslOP->GetOpFunc(DXIL::OpCode::CreateHandle, Type::getVoidTy(GetCtx()));
  657. if (createHandleFunc->user_empty()) {
  658. m_CBuffers.clear();
  659. m_UAVs.clear();
  660. m_SRVs.clear();
  661. m_Samplers.clear();
  662. createHandleFunc->eraseFromParent();
  663. return;
  664. }
  665. std::unordered_set<Value *> usedUAVID;
  666. std::unordered_set<Value *> usedSRVID;
  667. std::unordered_set<Value *> usedSamplerID;
  668. std::unordered_set<Value *> usedCBufID;
  669. // Collect used ID.
  670. for (User *U : createHandleFunc->users()) {
  671. CallInst *CI = cast<CallInst>(U);
  672. Value *vResClass =
  673. CI->getArgOperand(DXIL::OperandIndex::kCreateHandleResClassOpIdx);
  674. ConstantInt *cResClass = cast<ConstantInt>(vResClass);
  675. DXIL::ResourceClass resClass =
  676. static_cast<DXIL::ResourceClass>(cResClass->getLimitedValue());
  677. // Skip unused resource handle.
  678. if (CI->user_empty())
  679. continue;
  680. Value *resID =
  681. CI->getArgOperand(DXIL::OperandIndex::kCreateHandleResIDOpIdx);
  682. switch (resClass) {
  683. case DXIL::ResourceClass::CBuffer:
  684. CollectUsedResource(resID, usedCBufID);
  685. break;
  686. case DXIL::ResourceClass::Sampler:
  687. CollectUsedResource(resID, usedSamplerID);
  688. break;
  689. case DXIL::ResourceClass::SRV:
  690. CollectUsedResource(resID, usedSRVID);
  691. break;
  692. case DXIL::ResourceClass::UAV:
  693. CollectUsedResource(resID, usedUAVID);
  694. break;
  695. default:
  696. DXASSERT(0, "invalid res class");
  697. break;
  698. }
  699. }
  700. std::unordered_set<unsigned> immUAVID;
  701. std::unordered_set<unsigned> immSRVID;
  702. std::unordered_set<unsigned> immSamplerID;
  703. std::unordered_set<unsigned> immCBufID;
  704. ConvertUsedResource(immUAVID, usedUAVID);
  705. RemoveResources(m_UAVs, immUAVID);
  706. ConvertUsedResource(immSRVID, usedSRVID);
  707. ConvertUsedResource(immSamplerID, usedSamplerID);
  708. ConvertUsedResource(immCBufID, usedCBufID);
  709. RemoveResources(m_SRVs, immSRVID);
  710. RemoveResources(m_Samplers, immSamplerID);
  711. RemoveResources(m_CBuffers, immCBufID);
  712. }
  713. DxilSignature &DxilModule::GetInputSignature() {
  714. return *m_InputSignature;
  715. }
  716. const DxilSignature &DxilModule::GetInputSignature() const {
  717. return *m_InputSignature;
  718. }
  719. DxilSignature &DxilModule::GetOutputSignature() {
  720. return *m_OutputSignature;
  721. }
  722. const DxilSignature &DxilModule::GetOutputSignature() const {
  723. return *m_OutputSignature;
  724. }
  725. DxilSignature &DxilModule::GetPatchConstantSignature() {
  726. return *m_PatchConstantSignature;
  727. }
  728. const DxilSignature &DxilModule::GetPatchConstantSignature() const {
  729. return *m_PatchConstantSignature;
  730. }
  731. const RootSignatureHandle &DxilModule::GetRootSignature() const {
  732. return *m_RootSignature;
  733. }
  734. void DxilModule::StripRootSignatureFromMetadata() {
  735. NamedMDNode *pRootSignatureNamedMD = GetModule()->getNamedMetadata(DxilMDHelper::kDxilRootSignatureMDName);
  736. if (pRootSignatureNamedMD) {
  737. GetModule()->eraseNamedMetadata(pRootSignatureNamedMD);
  738. }
  739. }
  740. void DxilModule::ResetInputSignature(DxilSignature *pValue) {
  741. m_InputSignature.reset(pValue);
  742. }
  743. void DxilModule::ResetOutputSignature(DxilSignature *pValue) {
  744. m_OutputSignature.reset(pValue);
  745. }
  746. void DxilModule::ResetPatchConstantSignature(DxilSignature *pValue) {
  747. m_PatchConstantSignature.reset(pValue);
  748. }
  749. void DxilModule::ResetRootSignature(RootSignatureHandle *pValue) {
  750. m_RootSignature.reset(pValue);
  751. }
  752. DxilTypeSystem &DxilModule::GetTypeSystem() {
  753. return *m_pTypeSystem;
  754. }
  755. void DxilModule::ResetTypeSystem(DxilTypeSystem *pValue) {
  756. m_pTypeSystem.reset(pValue);
  757. }
  758. void DxilModule::ResetOP(hlsl::OP *hlslOP) {
  759. m_pOP.reset(hlslOP);
  760. }
  761. void DxilModule::EmitLLVMUsed() {
  762. if (m_LLVMUsed.empty())
  763. return;
  764. vector<llvm::Constant*> GVs;
  765. Type *pI8PtrType = Type::getInt8PtrTy(m_Ctx, DXIL::kDefaultAddrSpace);
  766. GVs.resize(m_LLVMUsed.size());
  767. for (size_t i = 0, e = m_LLVMUsed.size(); i != e; i++) {
  768. Constant *pConst = cast<Constant>(&*m_LLVMUsed[i]);
  769. PointerType * pPtrType = dyn_cast<PointerType>(pConst->getType());
  770. if (pPtrType->getPointerAddressSpace() != DXIL::kDefaultAddrSpace) {
  771. // Cast pointer to addrspace 0, as LLVMUsed elements must have the same type.
  772. GVs[i] = ConstantExpr::getAddrSpaceCast(pConst, pI8PtrType);
  773. } else {
  774. GVs[i] = ConstantExpr::getPointerCast(pConst, pI8PtrType);
  775. }
  776. }
  777. ArrayType *pATy = ArrayType::get(pI8PtrType, GVs.size());
  778. StringRef llvmUsedName = "llvm.used";
  779. if (GlobalVariable *oldGV = m_pModule->getGlobalVariable(llvmUsedName)) {
  780. oldGV->eraseFromParent();
  781. }
  782. GlobalVariable *pGV = new GlobalVariable(*m_pModule, pATy, false,
  783. GlobalValue::AppendingLinkage,
  784. ConstantArray::get(pATy, GVs),
  785. llvmUsedName);
  786. pGV->setSection("llvm.metadata");
  787. }
  788. vector<GlobalVariable* > &DxilModule::GetLLVMUsed() {
  789. return m_LLVMUsed;
  790. }
  791. // DXIL metadata serialization/deserialization.
  792. void DxilModule::EmitDxilMetadata() {
  793. m_pMDHelper->EmitDxilVersion(m_DxilMajor, m_DxilMinor);
  794. m_pMDHelper->EmitDxilShaderModel(m_pSM);
  795. MDTuple *pMDSignatures = m_pMDHelper->EmitDxilSignatures(*m_InputSignature,
  796. *m_OutputSignature,
  797. *m_PatchConstantSignature);
  798. MDTuple *pMDResources = EmitDxilResources();
  799. MDTuple *pMDProperties = EmitDxilShaderProperties();
  800. m_pMDHelper->EmitDxilTypeSystem(GetTypeSystem(), m_LLVMUsed);
  801. EmitLLVMUsed();
  802. MDTuple *pEntry = m_pMDHelper->EmitDxilEntryPointTuple(GetEntryFunction(), m_EntryName, pMDSignatures, pMDResources, pMDProperties);
  803. vector<MDNode *> Entries;
  804. Entries.emplace_back(pEntry);
  805. m_pMDHelper->EmitDxilEntryPoints(Entries);
  806. if (!m_RootSignature->IsEmpty()) {
  807. m_pMDHelper->EmitRootSignature(*m_RootSignature.get());
  808. }
  809. }
  810. bool DxilModule::IsKnownNamedMetaData(llvm::NamedMDNode &Node) {
  811. return DxilMDHelper::IsKnownNamedMetaData(Node);
  812. }
  813. void DxilModule::LoadDxilMetadata() {
  814. m_pMDHelper->LoadDxilVersion(m_DxilMajor, m_DxilMinor);
  815. const ShaderModel *loadedModule;
  816. m_pMDHelper->LoadDxilShaderModel(loadedModule);
  817. SetShaderModel(loadedModule);
  818. DXASSERT(m_InputSignature != nullptr, "else SetShaderModel didn't create input signature");
  819. const llvm::NamedMDNode *pEntries = m_pMDHelper->GetDxilEntryPoints();
  820. IFTBOOL(pEntries->getNumOperands() == 1, DXC_E_INCORRECT_DXIL_METADATA);
  821. Function *pEntryFunc;
  822. string EntryName;
  823. const llvm::MDOperand *pSignatures, *pResources, *pProperties;
  824. m_pMDHelper->GetDxilEntryPoint(pEntries->getOperand(0), pEntryFunc, EntryName, pSignatures, pResources, pProperties);
  825. SetEntryFunction(pEntryFunc);
  826. SetEntryFunctionName(EntryName);
  827. m_pMDHelper->LoadDxilSignatures(*pSignatures, *m_InputSignature,
  828. *m_OutputSignature, *m_PatchConstantSignature);
  829. LoadDxilResources(*pResources);
  830. LoadDxilShaderProperties(*pProperties);
  831. m_pMDHelper->LoadDxilTypeSystem(*m_pTypeSystem.get());
  832. m_pMDHelper->LoadRootSignature(*m_RootSignature.get());
  833. }
  834. MDTuple *DxilModule::EmitDxilResources() {
  835. // Emit SRV records.
  836. MDTuple *pTupleSRVs = nullptr;
  837. if (!m_SRVs.empty()) {
  838. vector<Metadata *> MDVals;
  839. for (size_t i = 0; i < m_SRVs.size(); i++) {
  840. MDVals.emplace_back(m_pMDHelper->EmitDxilSRV(*m_SRVs[i]));
  841. }
  842. pTupleSRVs = MDNode::get(m_Ctx, MDVals);
  843. }
  844. // Emit UAV records.
  845. MDTuple *pTupleUAVs = nullptr;
  846. if (!m_UAVs.empty()) {
  847. vector<Metadata *> MDVals;
  848. for (size_t i = 0; i < m_UAVs.size(); i++) {
  849. MDVals.emplace_back(m_pMDHelper->EmitDxilUAV(*m_UAVs[i]));
  850. }
  851. pTupleUAVs = MDNode::get(m_Ctx, MDVals);
  852. }
  853. // Emit CBuffer records.
  854. MDTuple *pTupleCBuffers = nullptr;
  855. if (!m_CBuffers.empty()) {
  856. vector<Metadata *> MDVals;
  857. for (size_t i = 0; i < m_CBuffers.size(); i++) {
  858. MDVals.emplace_back(m_pMDHelper->EmitDxilCBuffer(*m_CBuffers[i]));
  859. }
  860. pTupleCBuffers = MDNode::get(m_Ctx, MDVals);
  861. }
  862. // Emit Sampler records.
  863. MDTuple *pTupleSamplers = nullptr;
  864. if (!m_Samplers.empty()) {
  865. vector<Metadata *> MDVals;
  866. for (size_t i = 0; i < m_Samplers.size(); i++) {
  867. MDVals.emplace_back(m_pMDHelper->EmitDxilSampler(*m_Samplers[i]));
  868. }
  869. pTupleSamplers = MDNode::get(m_Ctx, MDVals);
  870. }
  871. if (pTupleSRVs != nullptr || pTupleUAVs != nullptr || pTupleCBuffers != nullptr || pTupleSamplers != nullptr) {
  872. return m_pMDHelper->EmitDxilResourceTuple(pTupleSRVs, pTupleUAVs, pTupleCBuffers, pTupleSamplers);
  873. } else {
  874. return nullptr;
  875. }
  876. }
  877. void DxilModule::LoadDxilResources(const llvm::MDOperand &MDO) {
  878. if (MDO.get() == nullptr)
  879. return;
  880. const llvm::MDTuple *pSRVs, *pUAVs, *pCBuffers, *pSamplers;
  881. m_pMDHelper->GetDxilResources(MDO, pSRVs, pUAVs, pCBuffers, pSamplers);
  882. // Load SRV records.
  883. if (pSRVs != nullptr) {
  884. for (unsigned i = 0; i < pSRVs->getNumOperands(); i++) {
  885. unique_ptr<DxilResource> pSRV(new DxilResource);
  886. m_pMDHelper->LoadDxilSRV(pSRVs->getOperand(i), *pSRV);
  887. AddSRV(std::move(pSRV));
  888. }
  889. }
  890. // Load UAV records.
  891. if (pUAVs != nullptr) {
  892. for (unsigned i = 0; i < pUAVs->getNumOperands(); i++) {
  893. unique_ptr<DxilResource> pUAV(new DxilResource);
  894. m_pMDHelper->LoadDxilUAV(pUAVs->getOperand(i), *pUAV);
  895. AddUAV(std::move(pUAV));
  896. }
  897. }
  898. // Load CBuffer records.
  899. if (pCBuffers != nullptr) {
  900. for (unsigned i = 0; i < pCBuffers->getNumOperands(); i++) {
  901. unique_ptr<DxilCBuffer> pCB(new DxilCBuffer);
  902. m_pMDHelper->LoadDxilCBuffer(pCBuffers->getOperand(i), *pCB);
  903. AddCBuffer(std::move(pCB));
  904. }
  905. }
  906. // Load Sampler records.
  907. if (pSamplers != nullptr) {
  908. for (unsigned i = 0; i < pSamplers->getNumOperands(); i++) {
  909. unique_ptr<DxilSampler> pSampler(new DxilSampler);
  910. m_pMDHelper->LoadDxilSampler(pSamplers->getOperand(i), *pSampler);
  911. AddSampler(std::move(pSampler));
  912. }
  913. }
  914. }
  915. MDTuple *DxilModule::EmitDxilShaderProperties() {
  916. vector<Metadata *> MDVals;
  917. // DXIL shader flags.
  918. uint64_t Flags = m_ShaderFlags.GetShaderFlagsRaw();
  919. if (Flags != 0) {
  920. MDVals.emplace_back(m_pMDHelper->Uint32ToConstMD(DxilMDHelper::kDxilShaderFlagsTag));
  921. MDVals.emplace_back(m_pMDHelper->Uint64ToConstMD(Flags));
  922. }
  923. // Compute shader.
  924. if (m_pSM->IsCS()) {
  925. MDVals.emplace_back(m_pMDHelper->Uint32ToConstMD(DxilMDHelper::kDxilNumThreadsTag));
  926. vector<Metadata *> NumThreadVals;
  927. NumThreadVals.emplace_back(m_pMDHelper->Uint32ToConstMD(m_NumThreads[0]));
  928. NumThreadVals.emplace_back(m_pMDHelper->Uint32ToConstMD(m_NumThreads[1]));
  929. NumThreadVals.emplace_back(m_pMDHelper->Uint32ToConstMD(m_NumThreads[2]));
  930. MDVals.emplace_back(MDNode::get(m_Ctx, NumThreadVals));
  931. }
  932. // Geometry shader.
  933. if (m_pSM->IsGS()) {
  934. MDVals.emplace_back(m_pMDHelper->Uint32ToConstMD(DxilMDHelper::kDxilGSStateTag));
  935. MDTuple *pMDTuple = m_pMDHelper->EmitDxilGSState(m_InputPrimitive,
  936. m_MaxVertexCount,
  937. GetActiveStreamMask(),
  938. m_StreamPrimitiveTopology,
  939. m_NumGSInstances);
  940. MDVals.emplace_back(pMDTuple);
  941. }
  942. // Domain shader.
  943. if (m_pSM->IsDS()) {
  944. MDVals.emplace_back(m_pMDHelper->Uint32ToConstMD(DxilMDHelper::kDxilDSStateTag));
  945. MDTuple *pMDTuple = m_pMDHelper->EmitDxilDSState(m_TessellatorDomain,
  946. m_InputControlPointCount);
  947. MDVals.emplace_back(pMDTuple);
  948. }
  949. // Hull shader.
  950. if (m_pSM->IsHS()) {
  951. MDVals.emplace_back(m_pMDHelper->Uint32ToConstMD(DxilMDHelper::kDxilHSStateTag));
  952. MDTuple *pMDTuple = m_pMDHelper->EmitDxilHSState(m_pPatchConstantFunc,
  953. m_InputControlPointCount,
  954. m_OutputControlPointCount,
  955. m_TessellatorDomain,
  956. m_TessellatorPartitioning,
  957. m_TessellatorOutputPrimitive,
  958. m_MaxTessellationFactor);
  959. MDVals.emplace_back(pMDTuple);
  960. }
  961. if (!MDVals.empty())
  962. return MDNode::get(m_Ctx, MDVals);
  963. else
  964. return nullptr;
  965. }
  966. void DxilModule::LoadDxilShaderProperties(const MDOperand &MDO) {
  967. if (MDO.get() == nullptr)
  968. return;
  969. const MDTuple *pTupleMD = dyn_cast<MDTuple>(MDO.get());
  970. IFTBOOL(pTupleMD != nullptr, DXC_E_INCORRECT_DXIL_METADATA);
  971. IFTBOOL((pTupleMD->getNumOperands() & 0x1) == 0, DXC_E_INCORRECT_DXIL_METADATA);
  972. for (unsigned iNode = 0; iNode < pTupleMD->getNumOperands(); iNode += 2) {
  973. unsigned Tag = DxilMDHelper::ConstMDToUint32(pTupleMD->getOperand(iNode));
  974. const MDOperand &MDO = pTupleMD->getOperand(iNode + 1);
  975. IFTBOOL(MDO.get() != nullptr, DXC_E_INCORRECT_DXIL_METADATA);
  976. switch (Tag) {
  977. case DxilMDHelper::kDxilShaderFlagsTag:
  978. m_ShaderFlags.SetShaderFlagsRaw(DxilMDHelper::ConstMDToUint64(MDO));
  979. break;
  980. case DxilMDHelper::kDxilNumThreadsTag: {
  981. MDNode *pNode = cast<MDNode>(MDO.get());
  982. m_NumThreads[0] = DxilMDHelper::ConstMDToUint32(pNode->getOperand(0));
  983. m_NumThreads[1] = DxilMDHelper::ConstMDToUint32(pNode->getOperand(1));
  984. m_NumThreads[2] = DxilMDHelper::ConstMDToUint32(pNode->getOperand(2));
  985. break;
  986. }
  987. case DxilMDHelper::kDxilGSStateTag: {
  988. m_pMDHelper->LoadDxilGSState(MDO, m_InputPrimitive, m_MaxVertexCount, m_ActiveStreamMask,
  989. m_StreamPrimitiveTopology, m_NumGSInstances);
  990. break;
  991. }
  992. case DxilMDHelper::kDxilDSStateTag:
  993. m_pMDHelper->LoadDxilDSState(MDO, m_TessellatorDomain, m_InputControlPointCount);
  994. break;
  995. case DxilMDHelper::kDxilHSStateTag:
  996. m_pMDHelper->LoadDxilHSState(MDO,
  997. m_pPatchConstantFunc,
  998. m_InputControlPointCount,
  999. m_OutputControlPointCount,
  1000. m_TessellatorDomain,
  1001. m_TessellatorPartitioning,
  1002. m_TessellatorOutputPrimitive,
  1003. m_MaxTessellationFactor);
  1004. break;
  1005. default:
  1006. DXASSERT(false, "Unknown extended shader properties tag");
  1007. break;
  1008. }
  1009. }
  1010. }
  1011. void DxilModule::StripDebugRelatedCode() {
  1012. // Remove all users of global resources.
  1013. for (GlobalVariable &GV : m_pModule->globals()) {
  1014. if (GV.hasInternalLinkage())
  1015. continue;
  1016. if (GV.getType()->getPointerAddressSpace() == DXIL::kTGSMAddrSpace)
  1017. continue;
  1018. for (auto git = GV.user_begin(); git != GV.user_end();) {
  1019. User *U = *(git++);
  1020. // Try to remove load of GV.
  1021. if (LoadInst *LI = dyn_cast<LoadInst>(U)) {
  1022. for (auto it = LI->user_begin(); it != LI->user_end();) {
  1023. Instruction *LIUser = cast<Instruction>(*(it++));
  1024. if (StoreInst *SI = dyn_cast<StoreInst>(LIUser)) {
  1025. Value *Ptr = SI->getPointerOperand();
  1026. SI->eraseFromParent();
  1027. if (Instruction *PtrInst = dyn_cast<Instruction>(Ptr)) {
  1028. if (Ptr->user_empty())
  1029. PtrInst->eraseFromParent();
  1030. }
  1031. }
  1032. }
  1033. if (LI->user_empty())
  1034. LI->eraseFromParent();
  1035. } else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(U)) {
  1036. for (auto GEPIt = GEP->user_begin(); GEPIt != GEP->user_end();) {
  1037. User *GEPU = *(GEPIt++);
  1038. // Try to remove load of GEP.
  1039. if (LoadInst *LI = dyn_cast<LoadInst>(GEPU)) {
  1040. for (auto it = LI->user_begin(); it != LI->user_end();) {
  1041. Instruction *LIUser = cast<Instruction>(*(it++));
  1042. if (StoreInst *SI = dyn_cast<StoreInst>(LIUser)) {
  1043. Value *Ptr = SI->getPointerOperand();
  1044. SI->eraseFromParent();
  1045. if (Instruction *PtrInst = dyn_cast<Instruction>(Ptr)) {
  1046. if (Ptr->user_empty())
  1047. PtrInst->eraseFromParent();
  1048. }
  1049. }
  1050. if (LI->user_empty())
  1051. LI->eraseFromParent();
  1052. }
  1053. }
  1054. }
  1055. if (GEP->user_empty())
  1056. GEP->eraseFromParent();
  1057. }
  1058. }
  1059. }
  1060. }
  1061. DebugInfoFinder &DxilModule::GetOrCreateDebugInfoFinder() {
  1062. if (m_pDebugInfoFinder == nullptr) {
  1063. m_pDebugInfoFinder = std::make_unique<llvm::DebugInfoFinder>();
  1064. m_pDebugInfoFinder->processModule(*m_pModule);
  1065. }
  1066. return *m_pDebugInfoFinder;
  1067. }
  1068. hlsl::DxilModule *hlsl::DxilModule::TryGetDxilModule(llvm::Module *pModule) {
  1069. LLVMContext &Ctx = pModule->getContext();
  1070. std::string diagStr;
  1071. raw_string_ostream diagStream(diagStr);
  1072. hlsl::DxilModule *pDxilModule = nullptr;
  1073. // TODO: add detail error in DxilMDHelper.
  1074. try {
  1075. pDxilModule = &pModule->GetOrCreateDxilModule();
  1076. } catch (const ::hlsl::Exception &hlslException) {
  1077. diagStream << "load dxil metadata failed -";
  1078. try {
  1079. const char *msg = hlslException.what();
  1080. if (msg == nullptr || *msg == '\0')
  1081. diagStream << " error code " << hlslException.hr << "\n";
  1082. else
  1083. diagStream << msg;
  1084. } catch (...) {
  1085. diagStream << " unable to retrieve error message.\n";
  1086. }
  1087. Ctx.diagnose(DxilErrorDiagnosticInfo(diagStream.str().c_str()));
  1088. } catch (...) {
  1089. Ctx.diagnose(DxilErrorDiagnosticInfo("load dxil metadata failed - unknown error.\n"));
  1090. }
  1091. return pDxilModule;
  1092. }
  1093. } // namespace hlsl
  1094. namespace llvm {
  1095. hlsl::DxilModule &Module::GetOrCreateDxilModule(bool skipInit) {
  1096. std::unique_ptr<hlsl::DxilModule> M;
  1097. if (!HasDxilModule()) {
  1098. M = std::make_unique<hlsl::DxilModule>(this);
  1099. if (!skipInit) {
  1100. M->LoadDxilMetadata();
  1101. }
  1102. SetDxilModule(M.release());
  1103. }
  1104. return GetDxilModule();
  1105. }
  1106. void Module::ResetDxilModule() {
  1107. if (HasDxilModule()) {
  1108. delete TheDxilModule;
  1109. TheDxilModule = nullptr;
  1110. }
  1111. }
  1112. }