DxilModule.cpp 43 KB

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