DxilModule.cpp 46 KB

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