DxilModule.cpp 40 KB

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