HLModule.cpp 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // //
  3. // HLModule.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. // HighLevel DX IR module. //
  9. // //
  10. ///////////////////////////////////////////////////////////////////////////////
  11. #include "dxc/HLSL/DxilOperations.h"
  12. #include "dxc/HLSL/DxilShaderModel.h"
  13. #include "dxc/HLSL/DxilCBuffer.h"
  14. #include "dxc/HLSL/HLModule.h"
  15. #include "dxc/HLSL/DxilTypeSystem.h"
  16. #include "dxc/HLSL/DxilRootSignature.h"
  17. #include "llvm/ADT/STLExtras.h"
  18. #include "llvm/IR/Constants.h"
  19. #include "llvm/IR/Function.h"
  20. #include "llvm/IR/IRBuilder.h"
  21. #include "llvm/IR/LLVMContext.h"
  22. #include "llvm/IR/Metadata.h"
  23. #include "llvm/IR/Module.h"
  24. #include "llvm/IR/DebugInfo.h"
  25. #include "llvm/IR/DIBuilder.h"
  26. #include "llvm/Support/raw_ostream.h"
  27. #include "llvm/IR/GetElementPtrTypeIterator.h"
  28. using namespace llvm;
  29. using std::string;
  30. using std::vector;
  31. using std::unique_ptr;
  32. namespace hlsl {
  33. //------------------------------------------------------------------------------
  34. //
  35. // HLModule methods.
  36. //
  37. HLModule::HLModule(Module *pModule)
  38. : m_Ctx(pModule->getContext())
  39. , m_pModule(pModule)
  40. , m_pEntryFunc(nullptr)
  41. , m_EntryName("")
  42. , m_pMDHelper(llvm::make_unique<DxilMDHelper>(
  43. pModule, llvm::make_unique<HLExtraPropertyHelper>(pModule)))
  44. , m_pDebugInfoFinder(nullptr)
  45. , m_pSM(nullptr)
  46. , m_DxilMajor(DXIL::kDxilMajor)
  47. , m_DxilMinor(DXIL::kDxilMinor)
  48. , m_pOP(llvm::make_unique<OP>(pModule->getContext(), pModule))
  49. , m_pTypeSystem(llvm::make_unique<DxilTypeSystem>(pModule)) {
  50. DXASSERT_NOMSG(m_pModule != nullptr);
  51. // Pin LLVM dump methods. TODO: make debug-only.
  52. void (__thiscall Module::*pfnModuleDump)() const = &Module::dump;
  53. void (__thiscall Type::*pfnTypeDump)() const = &Type::dump;
  54. m_pUnused = (char *)&pfnModuleDump - (char *)&pfnTypeDump;
  55. }
  56. HLModule::~HLModule() {
  57. }
  58. LLVMContext &HLModule::GetCtx() const { return m_Ctx; }
  59. Module *HLModule::GetModule() const { return m_pModule; }
  60. OP *HLModule::GetOP() const { return m_pOP.get(); }
  61. void HLModule::SetValidatorVersion(unsigned ValMajor, unsigned ValMinor) {
  62. m_ValMajor = ValMajor;
  63. m_ValMinor = ValMinor;
  64. }
  65. void HLModule::GetValidatorVersion(unsigned &ValMajor, unsigned &ValMinor) const {
  66. ValMajor = m_ValMajor;
  67. ValMinor = m_ValMinor;
  68. }
  69. void HLModule::SetShaderModel(const ShaderModel *pSM) {
  70. DXASSERT(m_pSM == nullptr, "shader model must not change for the module");
  71. DXASSERT(pSM != nullptr && pSM->IsValidForDxil(), "shader model must be valid");
  72. m_pSM = pSM;
  73. m_pSM->GetDxilVersion(m_DxilMajor, m_DxilMinor);
  74. m_pMDHelper->SetShaderModel(m_pSM);
  75. m_RootSignature = llvm::make_unique<RootSignatureHandle>();
  76. }
  77. const ShaderModel *HLModule::GetShaderModel() const {
  78. return m_pSM;
  79. }
  80. uint32_t HLOptions::GetHLOptionsRaw() const {
  81. union Cast {
  82. Cast(const HLOptions &options) {
  83. hlOptions = options;
  84. }
  85. HLOptions hlOptions;
  86. uint32_t rawData;
  87. };
  88. static_assert(sizeof(uint32_t) == sizeof(HLOptions),
  89. "size must match to make sure no undefined bits when cast");
  90. Cast rawCast(*this);
  91. return rawCast.rawData;
  92. }
  93. void HLOptions::SetHLOptionsRaw(uint32_t data) {
  94. union Cast {
  95. Cast(uint32_t data) {
  96. rawData = data;
  97. }
  98. HLOptions hlOptions;
  99. uint64_t rawData;
  100. };
  101. Cast rawCast(data);
  102. *this = rawCast.hlOptions;
  103. }
  104. void HLModule::SetHLOptions(HLOptions &opts) {
  105. m_Options = opts;
  106. }
  107. const HLOptions &HLModule::GetHLOptions() const {
  108. return m_Options;
  109. }
  110. Function *HLModule::GetEntryFunction() const {
  111. return m_pEntryFunc;
  112. }
  113. Function *HLModule::GetPatchConstantFunction() {
  114. if (!m_pSM->IsHS())
  115. return nullptr;
  116. if (!m_pEntryFunc)
  117. return nullptr;
  118. DxilFunctionProps &funcProps = GetDxilFunctionProps(m_pEntryFunc);
  119. return funcProps.ShaderProps.HS.patchConstantFunc;
  120. }
  121. void HLModule::SetEntryFunction(Function *pEntryFunc) {
  122. m_pEntryFunc = pEntryFunc;
  123. }
  124. const string &HLModule::GetEntryFunctionName() const { return m_EntryName; }
  125. void HLModule::SetEntryFunctionName(const string &name) { m_EntryName = name; }
  126. template<typename T> unsigned
  127. HLModule::AddResource(vector<unique_ptr<T> > &Vec, unique_ptr<T> pRes) {
  128. DXASSERT_NOMSG((unsigned)Vec.size() < UINT_MAX);
  129. unsigned Id = (unsigned)Vec.size();
  130. Vec.emplace_back(std::move(pRes));
  131. return Id;
  132. }
  133. unsigned HLModule::AddCBuffer(unique_ptr<DxilCBuffer> pCBuffer) {
  134. return AddResource<DxilCBuffer>(m_CBuffers, std::move(pCBuffer));
  135. }
  136. DxilCBuffer &HLModule::GetCBuffer(unsigned idx) {
  137. return *m_CBuffers[idx];
  138. }
  139. const DxilCBuffer &HLModule::GetCBuffer(unsigned idx) const {
  140. return *m_CBuffers[idx];
  141. }
  142. const vector<unique_ptr<DxilCBuffer> > &HLModule::GetCBuffers() const {
  143. return m_CBuffers;
  144. }
  145. unsigned HLModule::AddSampler(unique_ptr<DxilSampler> pSampler) {
  146. return AddResource<DxilSampler>(m_Samplers, std::move(pSampler));
  147. }
  148. DxilSampler &HLModule::GetSampler(unsigned idx) {
  149. return *m_Samplers[idx];
  150. }
  151. const DxilSampler &HLModule::GetSampler(unsigned idx) const {
  152. return *m_Samplers[idx];
  153. }
  154. const vector<unique_ptr<DxilSampler> > &HLModule::GetSamplers() const {
  155. return m_Samplers;
  156. }
  157. unsigned HLModule::AddSRV(unique_ptr<HLResource> pSRV) {
  158. return AddResource<HLResource>(m_SRVs, std::move(pSRV));
  159. }
  160. HLResource &HLModule::GetSRV(unsigned idx) {
  161. return *m_SRVs[idx];
  162. }
  163. const HLResource &HLModule::GetSRV(unsigned idx) const {
  164. return *m_SRVs[idx];
  165. }
  166. const vector<unique_ptr<HLResource> > &HLModule::GetSRVs() const {
  167. return m_SRVs;
  168. }
  169. unsigned HLModule::AddUAV(unique_ptr<HLResource> pUAV) {
  170. return AddResource<HLResource>(m_UAVs, std::move(pUAV));
  171. }
  172. HLResource &HLModule::GetUAV(unsigned idx) {
  173. return *m_UAVs[idx];
  174. }
  175. const HLResource &HLModule::GetUAV(unsigned idx) const {
  176. return *m_UAVs[idx];
  177. }
  178. const vector<unique_ptr<HLResource> > &HLModule::GetUAVs() const {
  179. return m_UAVs;
  180. }
  181. void HLModule::RemoveFunction(llvm::Function *F) {
  182. DXASSERT_NOMSG(F != nullptr);
  183. m_DxilFunctionPropsMap.erase(F);
  184. if (m_pTypeSystem.get()->GetFunctionAnnotation(F))
  185. m_pTypeSystem.get()->EraseFunctionAnnotation(F);
  186. m_pOP->RemoveFunction(F);
  187. }
  188. template <typename TResource>
  189. bool RemoveResource(std::vector<std::unique_ptr<TResource>> &vec,
  190. GlobalVariable *pVariable) {
  191. for (auto p = vec.begin(), e = vec.end(); p != e; ++p) {
  192. if ((*p)->GetGlobalSymbol() == pVariable) {
  193. p = vec.erase(p);
  194. // Update ID.
  195. for (e = vec.end();p != e; ++p) {
  196. unsigned ID = (*p)->GetID()-1;
  197. (*p)->SetID(ID);
  198. }
  199. return true;
  200. }
  201. }
  202. return false;
  203. }
  204. bool RemoveResource(std::vector<GlobalVariable *> &vec,
  205. llvm::GlobalVariable *pVariable) {
  206. for (auto p = vec.begin(), e = vec.end(); p != e; ++p) {
  207. if (*p == pVariable) {
  208. vec.erase(p);
  209. return true;
  210. }
  211. }
  212. return false;
  213. }
  214. void HLModule::RemoveGlobal(llvm::GlobalVariable *GV) {
  215. RemoveResources(&GV, 1);
  216. }
  217. void HLModule::RemoveResources(llvm::GlobalVariable **ppVariables,
  218. unsigned count) {
  219. DXASSERT_NOMSG(count == 0 || ppVariables != nullptr);
  220. unsigned resourcesRemoved = count;
  221. for (unsigned i = 0; i < count; ++i) {
  222. GlobalVariable *pVariable = ppVariables[i];
  223. // This could be considerably faster - check variable type to see which
  224. // resource type this is rather than scanning all lists, and look for
  225. // usage and removal patterns.
  226. if (RemoveResource(m_CBuffers, pVariable))
  227. continue;
  228. if (RemoveResource(m_SRVs, pVariable))
  229. continue;
  230. if (RemoveResource(m_UAVs, pVariable))
  231. continue;
  232. if (RemoveResource(m_Samplers, pVariable))
  233. continue;
  234. // TODO: do m_TGSMVariables and m_StreamOutputs need maintenance?
  235. --resourcesRemoved; // Global variable is not a resource?
  236. }
  237. }
  238. HLModule::tgsm_iterator HLModule::tgsm_begin() {
  239. return m_TGSMVariables.begin();
  240. }
  241. HLModule::tgsm_iterator HLModule::tgsm_end() {
  242. return m_TGSMVariables.end();
  243. }
  244. void HLModule::AddGroupSharedVariable(GlobalVariable *GV) {
  245. m_TGSMVariables.emplace_back(GV);
  246. }
  247. RootSignatureHandle &HLModule::GetRootSignature() {
  248. return *m_RootSignature;
  249. }
  250. DxilTypeSystem &HLModule::GetTypeSystem() {
  251. return *m_pTypeSystem;
  252. }
  253. DxilTypeSystem *HLModule::ReleaseTypeSystem() {
  254. return m_pTypeSystem.release();
  255. }
  256. hlsl::OP *HLModule::ReleaseOP() {
  257. return m_pOP.release();
  258. }
  259. RootSignatureHandle *HLModule::ReleaseRootSignature() {
  260. return m_RootSignature.release();
  261. }
  262. std::unordered_map<llvm::Function *, std::unique_ptr<DxilFunctionProps>> &&
  263. HLModule::ReleaseFunctionPropsMap() {
  264. return std::move(m_DxilFunctionPropsMap);
  265. }
  266. void HLModule::EmitLLVMUsed() {
  267. if (m_LLVMUsed.empty())
  268. return;
  269. vector<llvm::Constant*> GVs;
  270. GVs.resize(m_LLVMUsed.size());
  271. for (size_t i = 0, e = m_LLVMUsed.size(); i != e; i++) {
  272. GVs[i] = ConstantExpr::getAddrSpaceCast(cast<llvm::Constant>(&*m_LLVMUsed[i]), Type::getInt8PtrTy(m_Ctx));
  273. }
  274. ArrayType *pATy = ArrayType::get(Type::getInt8PtrTy(m_Ctx), GVs.size());
  275. GlobalVariable *pGV = new GlobalVariable(*m_pModule, pATy, false,
  276. GlobalValue::AppendingLinkage,
  277. ConstantArray::get(pATy, GVs),
  278. "llvm.used");
  279. pGV->setSection("llvm.metadata");
  280. }
  281. vector<GlobalVariable* > &HLModule::GetLLVMUsed() {
  282. return m_LLVMUsed;
  283. }
  284. bool HLModule::HasDxilFunctionProps(llvm::Function *F) {
  285. return m_DxilFunctionPropsMap.find(F) != m_DxilFunctionPropsMap.end();
  286. }
  287. DxilFunctionProps &HLModule::GetDxilFunctionProps(llvm::Function *F) {
  288. DXASSERT(m_DxilFunctionPropsMap.count(F) != 0, "cannot find F in map");
  289. return *m_DxilFunctionPropsMap[F];
  290. }
  291. void HLModule::AddDxilFunctionProps(llvm::Function *F, std::unique_ptr<DxilFunctionProps> &info) {
  292. DXASSERT(m_DxilFunctionPropsMap.count(F) == 0, "F already in map, info will be overwritten");
  293. DXASSERT_NOMSG(info->shaderKind != DXIL::ShaderKind::Invalid);
  294. m_DxilFunctionPropsMap[F] = std::move(info);
  295. }
  296. DxilFunctionAnnotation *HLModule::GetFunctionAnnotation(llvm::Function *F) {
  297. return m_pTypeSystem->GetFunctionAnnotation(F);
  298. }
  299. DxilFunctionAnnotation *HLModule::AddFunctionAnnotation(llvm::Function *F) {
  300. DXASSERT(m_pTypeSystem->GetFunctionAnnotation(F)==nullptr, "function annotation already exist");
  301. return m_pTypeSystem->AddFunctionAnnotation(F);
  302. }
  303. void HLModule::AddResourceTypeAnnotation(llvm::Type *Ty,
  304. DXIL::ResourceClass resClass,
  305. DXIL::ResourceKind kind) {
  306. if (m_ResTypeAnnotation.count(Ty) == 0) {
  307. m_ResTypeAnnotation.emplace(Ty, std::make_pair(resClass, kind));
  308. } else {
  309. DXASSERT(resClass == m_ResTypeAnnotation[Ty].first, "resClass mismatch");
  310. DXASSERT(kind == m_ResTypeAnnotation[Ty].second, "kind mismatch");
  311. }
  312. }
  313. DXIL::ResourceClass HLModule::GetResourceClass(llvm::Type *Ty) {
  314. if (m_ResTypeAnnotation.count(Ty) > 0) {
  315. return m_ResTypeAnnotation[Ty].first;
  316. } else {
  317. return DXIL::ResourceClass::Invalid;
  318. }
  319. }
  320. DXIL::ResourceKind HLModule::GetResourceKind(llvm::Type *Ty) {
  321. if (m_ResTypeAnnotation.count(Ty) > 0) {
  322. return m_ResTypeAnnotation[Ty].second;
  323. } else {
  324. return DXIL::ResourceKind::Invalid;
  325. }
  326. }
  327. static unsigned GetIntAt(MDTuple *tuple, unsigned idx) {
  328. return DxilMDHelper::ConstMDToUint32(tuple->getOperand(idx));
  329. }
  330. static unsigned GetFloatAt(MDTuple *tuple, unsigned idx) {
  331. return DxilMDHelper::ConstMDToFloat(tuple->getOperand(idx));
  332. }
  333. DXIL::Float32DenormMode HLModule::GetFloat32DenormMode() const {
  334. return m_Float32DenormMode;
  335. }
  336. void HLModule::SetFloat32DenormMode(const DXIL::Float32DenormMode mode) {
  337. m_Float32DenormMode = mode;
  338. }
  339. static const StringRef kHLDxilFunctionPropertiesMDName = "dx.fnprops";
  340. static const StringRef kHLDxilOptionsMDName = "dx.options";
  341. static const StringRef kHLDxilResourceTypeAnnotationMDName = "dx.resource.type.annotation";
  342. // DXIL metadata serialization/deserialization.
  343. void HLModule::EmitHLMetadata() {
  344. m_pMDHelper->EmitDxilVersion(m_DxilMajor, m_DxilMinor);
  345. m_pMDHelper->EmitValidatorVersion(m_ValMajor, m_ValMinor);
  346. m_pMDHelper->EmitDxilShaderModel(m_pSM);
  347. MDTuple *pMDResources = EmitHLResources();
  348. MDTuple *pMDProperties = EmitHLShaderProperties();
  349. m_pMDHelper->EmitDxilTypeSystem(GetTypeSystem(), m_LLVMUsed);
  350. EmitLLVMUsed();
  351. MDTuple *const pNullMDSig = nullptr;
  352. MDTuple *pEntry = m_pMDHelper->EmitDxilEntryPointTuple(GetEntryFunction(), m_EntryName, pNullMDSig, pMDResources, pMDProperties);
  353. vector<MDNode *> Entries;
  354. Entries.emplace_back(pEntry);
  355. m_pMDHelper->EmitDxilEntryPoints(Entries);
  356. {
  357. NamedMDNode * fnProps = m_pModule->getOrInsertNamedMetadata(kHLDxilFunctionPropertiesMDName);
  358. for (auto && pair : m_DxilFunctionPropsMap) {
  359. const hlsl::DxilFunctionProps * props = pair.second.get();
  360. MDTuple *pProps = m_pMDHelper->EmitDxilFunctionProps(props, pair.first);
  361. fnProps->addOperand(pProps);
  362. }
  363. NamedMDNode * options = m_pModule->getOrInsertNamedMetadata(kHLDxilOptionsMDName);
  364. uint32_t hlOptions = m_Options.GetHLOptionsRaw();
  365. options->addOperand(MDNode::get(m_Ctx, m_pMDHelper->Uint32ToConstMD(hlOptions)));
  366. NamedMDNode * resTyAnnotations = m_pModule->getOrInsertNamedMetadata(kHLDxilResourceTypeAnnotationMDName);
  367. resTyAnnotations->addOperand(EmitResTyAnnotations());
  368. }
  369. if (!m_RootSignature->IsEmpty()) {
  370. m_pMDHelper->EmitRootSignature(*m_RootSignature.get());
  371. }
  372. }
  373. void HLModule::LoadHLMetadata() {
  374. m_pMDHelper->LoadDxilVersion(m_DxilMajor, m_DxilMinor);
  375. m_pMDHelper->LoadValidatorVersion(m_ValMajor, m_ValMinor);
  376. m_pMDHelper->LoadDxilShaderModel(m_pSM);
  377. m_RootSignature = llvm::make_unique<RootSignatureHandle>();
  378. const llvm::NamedMDNode *pEntries = m_pMDHelper->GetDxilEntryPoints();
  379. Function *pEntryFunc;
  380. string EntryName;
  381. const llvm::MDOperand *pSignatures, *pResources, *pProperties;
  382. m_pMDHelper->GetDxilEntryPoint(pEntries->getOperand(0), pEntryFunc, EntryName, pSignatures, pResources, pProperties);
  383. SetEntryFunction(pEntryFunc);
  384. SetEntryFunctionName(EntryName);
  385. LoadHLResources(*pResources);
  386. LoadHLShaderProperties(*pProperties);
  387. m_pMDHelper->LoadDxilTypeSystem(*m_pTypeSystem.get());
  388. {
  389. NamedMDNode * fnProps = m_pModule->getNamedMetadata(kHLDxilFunctionPropertiesMDName);
  390. size_t propIdx = 0;
  391. while (propIdx < fnProps->getNumOperands()) {
  392. MDTuple *pProps = dyn_cast<MDTuple>(fnProps->getOperand(propIdx++));
  393. std::unique_ptr<hlsl::DxilFunctionProps> props =
  394. llvm::make_unique<hlsl::DxilFunctionProps>();
  395. Function *F = m_pMDHelper->LoadDxilFunctionProps(pProps, props.get());
  396. m_DxilFunctionPropsMap[F] = std::move(props);
  397. }
  398. const NamedMDNode * options = m_pModule->getOrInsertNamedMetadata(kHLDxilOptionsMDName);
  399. const MDNode *MDOptions = options->getOperand(0);
  400. m_Options.SetHLOptionsRaw(DxilMDHelper::ConstMDToUint32(MDOptions->getOperand(0)));
  401. NamedMDNode * resTyAnnotations = m_pModule->getOrInsertNamedMetadata(kHLDxilResourceTypeAnnotationMDName);
  402. const MDNode *MDResTyAnnotations = resTyAnnotations->getOperand(0);
  403. if (MDResTyAnnotations->getNumOperands())
  404. LoadResTyAnnotations(MDResTyAnnotations->getOperand(0));
  405. }
  406. m_pMDHelper->LoadRootSignature(*m_RootSignature.get());
  407. }
  408. void HLModule::ClearHLMetadata(llvm::Module &M) {
  409. Module::named_metadata_iterator
  410. b = M.named_metadata_begin(),
  411. e = M.named_metadata_end();
  412. SmallVector<NamedMDNode*, 8> nodes;
  413. for (; b != e; ++b) {
  414. StringRef name = b->getName();
  415. if (name == DxilMDHelper::kDxilVersionMDName ||
  416. name == DxilMDHelper::kDxilShaderModelMDName ||
  417. name == DxilMDHelper::kDxilEntryPointsMDName ||
  418. name == DxilMDHelper::kDxilRootSignatureMDName ||
  419. name == DxilMDHelper::kDxilResourcesMDName ||
  420. name == DxilMDHelper::kDxilTypeSystemMDName ||
  421. name == DxilMDHelper::kDxilValidatorVersionMDName ||
  422. name == kHLDxilFunctionPropertiesMDName || // TODO: adjust to proper name
  423. name == kHLDxilResourceTypeAnnotationMDName ||
  424. name == kHLDxilOptionsMDName ||
  425. name.startswith(DxilMDHelper::kDxilTypeSystemHelperVariablePrefix)) {
  426. nodes.push_back(b);
  427. }
  428. }
  429. for (size_t i = 0; i < nodes.size(); ++i) {
  430. M.eraseNamedMetadata(nodes[i]);
  431. }
  432. }
  433. MDTuple *HLModule::EmitHLResources() {
  434. // Emit SRV records.
  435. MDTuple *pTupleSRVs = nullptr;
  436. if (!m_SRVs.empty()) {
  437. vector<Metadata *> MDVals;
  438. for (size_t i = 0; i < m_SRVs.size(); i++) {
  439. MDVals.emplace_back(m_pMDHelper->EmitDxilSRV(*m_SRVs[i]));
  440. }
  441. pTupleSRVs = MDNode::get(m_Ctx, MDVals);
  442. }
  443. // Emit UAV records.
  444. MDTuple *pTupleUAVs = nullptr;
  445. if (!m_UAVs.empty()) {
  446. vector<Metadata *> MDVals;
  447. for (size_t i = 0; i < m_UAVs.size(); i++) {
  448. MDVals.emplace_back(m_pMDHelper->EmitDxilUAV(*m_UAVs[i]));
  449. }
  450. pTupleUAVs = MDNode::get(m_Ctx, MDVals);
  451. }
  452. // Emit CBuffer records.
  453. MDTuple *pTupleCBuffers = nullptr;
  454. if (!m_CBuffers.empty()) {
  455. vector<Metadata *> MDVals;
  456. for (size_t i = 0; i < m_CBuffers.size(); i++) {
  457. MDVals.emplace_back(m_pMDHelper->EmitDxilCBuffer(*m_CBuffers[i]));
  458. }
  459. pTupleCBuffers = MDNode::get(m_Ctx, MDVals);
  460. }
  461. // Emit Sampler records.
  462. MDTuple *pTupleSamplers = nullptr;
  463. if (!m_Samplers.empty()) {
  464. vector<Metadata *> MDVals;
  465. for (size_t i = 0; i < m_Samplers.size(); i++) {
  466. MDVals.emplace_back(m_pMDHelper->EmitDxilSampler(*m_Samplers[i]));
  467. }
  468. pTupleSamplers = MDNode::get(m_Ctx, MDVals);
  469. }
  470. if (pTupleSRVs != nullptr || pTupleUAVs != nullptr || pTupleCBuffers != nullptr || pTupleSamplers != nullptr) {
  471. return m_pMDHelper->EmitDxilResourceTuple(pTupleSRVs, pTupleUAVs, pTupleCBuffers, pTupleSamplers);
  472. } else {
  473. return nullptr;
  474. }
  475. }
  476. void HLModule::LoadHLResources(const llvm::MDOperand &MDO) {
  477. const llvm::MDTuple *pSRVs, *pUAVs, *pCBuffers, *pSamplers;
  478. m_pMDHelper->GetDxilResources(MDO, pSRVs, pUAVs, pCBuffers, pSamplers);
  479. // Load SRV records.
  480. if (pSRVs != nullptr) {
  481. for (unsigned i = 0; i < pSRVs->getNumOperands(); i++) {
  482. unique_ptr<HLResource> pSRV(new HLResource);
  483. m_pMDHelper->LoadDxilSRV(pSRVs->getOperand(i), *pSRV);
  484. AddSRV(std::move(pSRV));
  485. }
  486. }
  487. // Load UAV records.
  488. if (pUAVs != nullptr) {
  489. for (unsigned i = 0; i < pUAVs->getNumOperands(); i++) {
  490. unique_ptr<HLResource> pUAV(new HLResource);
  491. m_pMDHelper->LoadDxilUAV(pUAVs->getOperand(i), *pUAV);
  492. AddUAV(std::move(pUAV));
  493. }
  494. }
  495. // Load CBuffer records.
  496. if (pCBuffers != nullptr) {
  497. for (unsigned i = 0; i < pCBuffers->getNumOperands(); i++) {
  498. unique_ptr<DxilCBuffer> pCB = llvm::make_unique<DxilCBuffer>();
  499. m_pMDHelper->LoadDxilCBuffer(pCBuffers->getOperand(i), *pCB);
  500. AddCBuffer(std::move(pCB));
  501. }
  502. }
  503. // Load Sampler records.
  504. if (pSamplers != nullptr) {
  505. for (unsigned i = 0; i < pSamplers->getNumOperands(); i++) {
  506. unique_ptr<DxilSampler> pSampler(new DxilSampler);
  507. m_pMDHelper->LoadDxilSampler(pSamplers->getOperand(i), *pSampler);
  508. AddSampler(std::move(pSampler));
  509. }
  510. }
  511. }
  512. llvm::MDTuple *HLModule::EmitResTyAnnotations() {
  513. vector<Metadata *> MDVals;
  514. for (auto &resAnnotation : m_ResTypeAnnotation) {
  515. Metadata *TyMeta =
  516. ValueAsMetadata::get(UndefValue::get(resAnnotation.first));
  517. MDVals.emplace_back(TyMeta);
  518. MDVals.emplace_back(m_pMDHelper->Uint32ToConstMD(
  519. static_cast<unsigned>(resAnnotation.second.first)));
  520. MDVals.emplace_back(m_pMDHelper->Uint32ToConstMD(
  521. static_cast<unsigned>(resAnnotation.second.second)));
  522. }
  523. return MDNode::get(m_Ctx, MDVals);
  524. }
  525. void HLModule::LoadResTyAnnotations(const llvm::MDOperand &MDO) {
  526. if (MDO.get() == nullptr)
  527. return;
  528. const MDTuple *pTupleMD = dyn_cast<MDTuple>(MDO.get());
  529. IFTBOOL(pTupleMD != nullptr, DXC_E_INCORRECT_DXIL_METADATA);
  530. IFTBOOL((pTupleMD->getNumOperands() & 0x3) == 0,
  531. DXC_E_INCORRECT_DXIL_METADATA);
  532. for (unsigned iNode = 0; iNode < pTupleMD->getNumOperands(); iNode += 3) {
  533. const MDOperand &MDTy = pTupleMD->getOperand(iNode);
  534. const MDOperand &MDClass = pTupleMD->getOperand(iNode + 1);
  535. const MDOperand &MDKind = pTupleMD->getOperand(iNode + 2);
  536. Type *Ty = m_pMDHelper->ValueMDToValue(MDTy)->getType();
  537. DXIL::ResourceClass resClass = static_cast<DXIL::ResourceClass>(
  538. DxilMDHelper::ConstMDToUint32(MDClass));
  539. DXIL::ResourceKind kind =
  540. static_cast<DXIL::ResourceKind>(DxilMDHelper::ConstMDToUint32(MDKind));
  541. AddResourceTypeAnnotation(Ty, resClass, kind);
  542. }
  543. }
  544. MDTuple *HLModule::EmitHLShaderProperties() {
  545. return nullptr;
  546. }
  547. void HLModule::LoadHLShaderProperties(const MDOperand &MDO) {
  548. return;
  549. }
  550. MDNode *HLModule::DxilSamplerToMDNode(const DxilSampler &S) {
  551. MDNode *MD = m_pMDHelper->EmitDxilSampler(S);
  552. ValueAsMetadata *ResClass =
  553. m_pMDHelper->Uint32ToConstMD((unsigned)DXIL::ResourceClass::Sampler);
  554. return MDNode::get(m_Ctx, {ResClass, MD});
  555. }
  556. MDNode *HLModule::DxilSRVToMDNode(const DxilResource &SRV) {
  557. MDNode *MD = m_pMDHelper->EmitDxilSRV(SRV);
  558. ValueAsMetadata *ResClass =
  559. m_pMDHelper->Uint32ToConstMD((unsigned)DXIL::ResourceClass::SRV);
  560. return MDNode::get(m_Ctx, {ResClass, MD});
  561. }
  562. MDNode *HLModule::DxilUAVToMDNode(const DxilResource &UAV) {
  563. MDNode *MD = m_pMDHelper->EmitDxilUAV(UAV);
  564. ValueAsMetadata *ResClass =
  565. m_pMDHelper->Uint32ToConstMD((unsigned)DXIL::ResourceClass::UAV);
  566. return MDNode::get(m_Ctx, {ResClass, MD});
  567. }
  568. MDNode *HLModule::DxilCBufferToMDNode(const DxilCBuffer &CB) {
  569. MDNode *MD = m_pMDHelper->EmitDxilCBuffer(CB);
  570. ValueAsMetadata *ResClass =
  571. m_pMDHelper->Uint32ToConstMD((unsigned)DXIL::ResourceClass::CBuffer);
  572. return MDNode::get(m_Ctx, {ResClass, MD});
  573. }
  574. void HLModule::LoadDxilResourceBaseFromMDNode(MDNode *MD, DxilResourceBase &R) {
  575. return m_pMDHelper->LoadDxilResourceBaseFromMDNode(MD, R);
  576. }
  577. void HLModule::AddResourceWithGlobalVariableAndMDNode(llvm::Constant *GV,
  578. llvm::MDNode *MD) {
  579. IFTBOOL(MD->getNumOperands() >= DxilMDHelper::kHLDxilResourceAttributeNumFields,
  580. DXC_E_INCORRECT_DXIL_METADATA);
  581. DxilResource::Class RC =
  582. static_cast<DxilResource::Class>(m_pMDHelper->ConstMDToUint32(
  583. MD->getOperand(DxilMDHelper::kHLDxilResourceAttributeClass)));
  584. const MDOperand &Meta =
  585. MD->getOperand(DxilMDHelper::kHLDxilResourceAttributeMeta);
  586. unsigned rangeSize = 1;
  587. Type *Ty = GV->getType()->getPointerElementType();
  588. if (ArrayType *AT = dyn_cast<ArrayType>(Ty))
  589. rangeSize = AT->getNumElements();
  590. switch (RC) {
  591. case DxilResource::Class::Sampler: {
  592. std::unique_ptr<DxilSampler> S = llvm::make_unique<DxilSampler>();
  593. m_pMDHelper->LoadDxilSampler(Meta, *S);
  594. S->SetGlobalSymbol(GV);
  595. S->SetGlobalName(GV->getName());
  596. S->SetRangeSize(rangeSize);
  597. AddSampler(std::move(S));
  598. } break;
  599. case DxilResource::Class::SRV: {
  600. std::unique_ptr<HLResource> Res = llvm::make_unique<HLResource>();
  601. m_pMDHelper->LoadDxilSRV(Meta, *Res);
  602. Res->SetGlobalSymbol(GV);
  603. Res->SetGlobalName(GV->getName());
  604. Res->SetRangeSize(rangeSize);
  605. AddSRV(std::move(Res));
  606. } break;
  607. case DxilResource::Class::UAV: {
  608. std::unique_ptr<HLResource> Res = llvm::make_unique<HLResource>();
  609. m_pMDHelper->LoadDxilUAV(Meta, *Res);
  610. Res->SetGlobalSymbol(GV);
  611. Res->SetGlobalName(GV->getName());
  612. Res->SetRangeSize(rangeSize);
  613. AddUAV(std::move(Res));
  614. } break;
  615. default:
  616. DXASSERT(0, "Invalid metadata for AddResourceWithGlobalVariableAndMDNode");
  617. }
  618. }
  619. // TODO: Don't check names.
  620. bool HLModule::IsStreamOutputType(llvm::Type *Ty) {
  621. if (StructType *ST = dyn_cast<StructType>(Ty)) {
  622. if (ST->getName().startswith("class.PointStream"))
  623. return true;
  624. if (ST->getName().startswith("class.LineStream"))
  625. return true;
  626. if (ST->getName().startswith("class.TriangleStream"))
  627. return true;
  628. }
  629. return false;
  630. }
  631. bool HLModule::IsStreamOutputPtrType(llvm::Type *Ty) {
  632. if (!Ty->isPointerTy())
  633. return false;
  634. Ty = Ty->getPointerElementType();
  635. return IsStreamOutputType(Ty);
  636. }
  637. bool HLModule::IsHLSLObjectType(llvm::Type *Ty) {
  638. if (llvm::StructType *ST = dyn_cast<llvm::StructType>(Ty)) {
  639. StringRef name = ST->getName();
  640. if (name.startswith("dx.types.wave_t"))
  641. return true;
  642. if (name.endswith("_slice_type"))
  643. return false;
  644. name = name.ltrim("class.");
  645. name = name.ltrim("struct.");
  646. if (name == "SamplerState")
  647. return true;
  648. if (name == "SamplerComparisonState")
  649. return true;
  650. if (name.startswith("TriangleStream"))
  651. return true;
  652. if (name.startswith("PointStream"))
  653. return true;
  654. if (name.startswith("LineStream"))
  655. return true;
  656. if (name.startswith("AppendStructuredBuffer"))
  657. return true;
  658. if (name.startswith("ConsumeStructuredBuffer"))
  659. return true;
  660. if (name.startswith("ConstantBuffer"))
  661. return true;
  662. name = name.ltrim("RasterizerOrdered");
  663. name = name.ltrim("RW");
  664. if (name == "ByteAddressBuffer")
  665. return true;
  666. if (name == "RayTracingAccelerationStructure")
  667. return true;
  668. if (name.startswith("Buffer"))
  669. return true;
  670. if (name.startswith("StructuredBuffer"))
  671. return true;
  672. if (name.startswith("Texture1D"))
  673. return true;
  674. if (name.startswith("Texture1DArray"))
  675. return true;
  676. if (name.startswith("Texture2D"))
  677. return true;
  678. if (name.startswith("Texture2DArray"))
  679. return true;
  680. if (name.startswith("Texture3D"))
  681. return true;
  682. if (name.startswith("TextureCube"))
  683. return true;
  684. if (name.startswith("TextureCubeArray"))
  685. return true;
  686. if (name.startswith("Texture2DMS"))
  687. return true;
  688. if (name.startswith("Texture2DMSArray"))
  689. return true;
  690. }
  691. return false;
  692. }
  693. void HLModule::GetParameterRowsAndCols(Type *Ty, unsigned &rows, unsigned &cols,
  694. DxilParameterAnnotation &paramAnnotation) {
  695. if (Ty->isPointerTy())
  696. Ty = Ty->getPointerElementType();
  697. // For array input of HS, DS, GS,
  698. // we need to skip the first level which size is based on primitive type.
  699. DxilParamInputQual inputQual = paramAnnotation.GetParamInputQual();
  700. bool skipOneLevelArray = inputQual == DxilParamInputQual::InputPatch;
  701. skipOneLevelArray |= inputQual == DxilParamInputQual::OutputPatch;
  702. skipOneLevelArray |= inputQual == DxilParamInputQual::InputPrimitive;
  703. if (skipOneLevelArray) {
  704. if (Ty->isArrayTy())
  705. Ty = Ty->getArrayElementType();
  706. }
  707. unsigned arraySize = 1;
  708. while (Ty->isArrayTy()) {
  709. arraySize *= Ty->getArrayNumElements();
  710. Ty = Ty->getArrayElementType();
  711. }
  712. rows = 1;
  713. cols = 1;
  714. if (paramAnnotation.HasMatrixAnnotation()) {
  715. const DxilMatrixAnnotation &matrix = paramAnnotation.GetMatrixAnnotation();
  716. if (matrix.Orientation == MatrixOrientation::RowMajor) {
  717. rows = matrix.Rows;
  718. cols = matrix.Cols;
  719. } else {
  720. DXASSERT(matrix.Orientation == MatrixOrientation::ColumnMajor, "");
  721. cols = matrix.Rows;
  722. rows = matrix.Cols;
  723. }
  724. } else if (Ty->isVectorTy())
  725. cols = Ty->getVectorNumElements();
  726. rows *= arraySize;
  727. }
  728. static Value *MergeGEP(GEPOperator *SrcGEP, GetElementPtrInst *GEP) {
  729. IRBuilder<> Builder(GEP);
  730. SmallVector<Value *, 8> Indices;
  731. // Find out whether the last index in the source GEP is a sequential idx.
  732. bool EndsWithSequential = false;
  733. for (gep_type_iterator I = gep_type_begin(*SrcGEP), E = gep_type_end(*SrcGEP);
  734. I != E; ++I)
  735. EndsWithSequential = !(*I)->isStructTy();
  736. if (EndsWithSequential) {
  737. Value *Sum;
  738. Value *SO1 = SrcGEP->getOperand(SrcGEP->getNumOperands() - 1);
  739. Value *GO1 = GEP->getOperand(1);
  740. if (SO1 == Constant::getNullValue(SO1->getType())) {
  741. Sum = GO1;
  742. } else if (GO1 == Constant::getNullValue(GO1->getType())) {
  743. Sum = SO1;
  744. } else {
  745. // If they aren't the same type, then the input hasn't been processed
  746. // by the loop above yet (which canonicalizes sequential index types to
  747. // intptr_t). Just avoid transforming this until the input has been
  748. // normalized.
  749. if (SO1->getType() != GO1->getType())
  750. return nullptr;
  751. // Only do the combine when GO1 and SO1 are both constants. Only in
  752. // this case, we are sure the cost after the merge is never more than
  753. // that before the merge.
  754. if (!isa<Constant>(GO1) || !isa<Constant>(SO1))
  755. return nullptr;
  756. Sum = Builder.CreateAdd(SO1, GO1);
  757. }
  758. // Update the GEP in place if possible.
  759. if (SrcGEP->getNumOperands() == 2) {
  760. GEP->setOperand(0, SrcGEP->getOperand(0));
  761. GEP->setOperand(1, Sum);
  762. return GEP;
  763. }
  764. Indices.append(SrcGEP->op_begin() + 1, SrcGEP->op_end() - 1);
  765. Indices.push_back(Sum);
  766. Indices.append(GEP->op_begin() + 2, GEP->op_end());
  767. } else if (isa<Constant>(*GEP->idx_begin()) &&
  768. cast<Constant>(*GEP->idx_begin())->isNullValue() &&
  769. SrcGEP->getNumOperands() != 1) {
  770. // Otherwise we can do the fold if the first index of the GEP is a zero
  771. Indices.append(SrcGEP->op_begin() + 1, SrcGEP->op_end());
  772. Indices.append(GEP->idx_begin() + 1, GEP->idx_end());
  773. }
  774. if (!Indices.empty())
  775. return Builder.CreateInBoundsGEP(SrcGEP->getSourceElementType(),
  776. SrcGEP->getOperand(0), Indices,
  777. GEP->getName());
  778. else
  779. llvm_unreachable("must merge");
  780. }
  781. void HLModule::MergeGepUse(Value *V) {
  782. for (auto U = V->user_begin(); U != V->user_end();) {
  783. auto Use = U++;
  784. if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(*Use)) {
  785. if (GEPOperator *prevGEP = dyn_cast<GEPOperator>(V)) {
  786. // merge the 2 GEPs
  787. Value *newGEP = MergeGEP(prevGEP, GEP);
  788. // Don't need to replace when GEP is updated in place
  789. if (newGEP != GEP) {
  790. GEP->replaceAllUsesWith(newGEP);
  791. GEP->eraseFromParent();
  792. }
  793. MergeGepUse(newGEP);
  794. } else {
  795. MergeGepUse(*Use);
  796. }
  797. } else if (GEPOperator *GEPOp = dyn_cast<GEPOperator>(*Use)) {
  798. if (GEPOperator *prevGEP = dyn_cast<GEPOperator>(V)) {
  799. // merge the 2 GEPs
  800. Value *newGEP = MergeGEP(prevGEP, GEP);
  801. // Don't need to replace when GEP is updated in place
  802. if (newGEP != GEP) {
  803. GEP->replaceAllUsesWith(newGEP);
  804. GEP->eraseFromParent();
  805. }
  806. MergeGepUse(newGEP);
  807. } else {
  808. MergeGepUse(*Use);
  809. }
  810. }
  811. }
  812. if (V->user_empty()) {
  813. // Only remove GEP here, root ptr will be removed by DCE.
  814. if (GetElementPtrInst *I = dyn_cast<GetElementPtrInst>(V))
  815. I->eraseFromParent();
  816. }
  817. }
  818. template<typename BuilderTy>
  819. CallInst *HLModule::EmitHLOperationCall(BuilderTy &Builder,
  820. HLOpcodeGroup group, unsigned opcode,
  821. Type *RetType,
  822. ArrayRef<Value *> paramList,
  823. llvm::Module &M) {
  824. SmallVector<llvm::Type *, 4> paramTyList;
  825. // Add the opcode param
  826. llvm::Type *opcodeTy = llvm::Type::getInt32Ty(M.getContext());
  827. paramTyList.emplace_back(opcodeTy);
  828. for (Value *param : paramList) {
  829. paramTyList.emplace_back(param->getType());
  830. }
  831. llvm::FunctionType *funcTy =
  832. llvm::FunctionType::get(RetType, paramTyList, false);
  833. Function *opFunc = GetOrCreateHLFunction(M, funcTy, group, opcode);
  834. SmallVector<Value *, 4> opcodeParamList;
  835. Value *opcodeConst = Constant::getIntegerValue(opcodeTy, APInt(32, opcode));
  836. opcodeParamList.emplace_back(opcodeConst);
  837. opcodeParamList.append(paramList.begin(), paramList.end());
  838. return Builder.CreateCall(opFunc, opcodeParamList);
  839. }
  840. template
  841. CallInst *HLModule::EmitHLOperationCall(IRBuilder<> &Builder,
  842. HLOpcodeGroup group, unsigned opcode,
  843. Type *RetType,
  844. ArrayRef<Value *> paramList,
  845. llvm::Module &M);
  846. unsigned HLModule::FindCastOp(bool fromUnsigned, bool toUnsigned,
  847. llvm::Type *SrcTy, llvm::Type *DstTy) {
  848. Instruction::CastOps castOp = llvm::Instruction::CastOps::BitCast;
  849. if (SrcTy->isAggregateType() || DstTy->isAggregateType())
  850. return llvm::Instruction::CastOps::BitCast;
  851. uint32_t SrcBitSize = SrcTy->getScalarSizeInBits();
  852. uint32_t DstBitSize = DstTy->getScalarSizeInBits();
  853. if (SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy()) {
  854. if (SrcBitSize > DstBitSize)
  855. return Instruction::Trunc;
  856. if (toUnsigned)
  857. return Instruction::ZExt;
  858. else
  859. return Instruction::SExt;
  860. }
  861. if (SrcTy->isFPOrFPVectorTy() && DstTy->isFPOrFPVectorTy()) {
  862. if (SrcBitSize > DstBitSize)
  863. return Instruction::FPTrunc;
  864. else
  865. return Instruction::FPExt;
  866. }
  867. if (SrcTy->isIntOrIntVectorTy() && DstTy->isFPOrFPVectorTy())
  868. if (fromUnsigned)
  869. return Instruction::UIToFP;
  870. else
  871. return Instruction::SIToFP;
  872. if (SrcTy->isFPOrFPVectorTy() && DstTy->isIntOrIntVectorTy())
  873. if (toUnsigned)
  874. return Instruction::FPToUI;
  875. else
  876. return Instruction::FPToSI;
  877. DXASSERT_NOMSG(0);
  878. return castOp;
  879. }
  880. bool HLModule::HasPreciseAttributeWithMetadata(Instruction *I) {
  881. return DxilMDHelper::IsMarkedPrecise(I);
  882. }
  883. void HLModule::MarkPreciseAttributeWithMetadata(Instruction *I) {
  884. return DxilMDHelper::MarkPrecise(I);
  885. }
  886. void HLModule::ClearPreciseAttributeWithMetadata(Instruction *I) {
  887. I->setMetadata(DxilMDHelper::kDxilPreciseAttributeMDName, nullptr);
  888. }
  889. static void MarkPreciseAttribute(Function *F) {
  890. LLVMContext &Ctx = F->getContext();
  891. MDNode *preciseNode = MDNode::get(
  892. Ctx, {MDString::get(Ctx, DxilMDHelper::kDxilPreciseAttributeMDName)});
  893. F->setMetadata(DxilMDHelper::kDxilPreciseAttributeMDName, preciseNode);
  894. }
  895. static void MarkPreciseAttributeOnValWithFunctionCall(
  896. llvm::Value *V, llvm::IRBuilder<> &Builder, llvm::Module &M) {
  897. Type *Ty = V->getType();
  898. Type *EltTy = Ty->getScalarType();
  899. // TODO: Only do this on basic types.
  900. FunctionType *preciseFuncTy =
  901. FunctionType::get(Type::getVoidTy(M.getContext()), {EltTy}, false);
  902. // The function will be deleted after precise propagate.
  903. std::string preciseFuncName = "dx.attribute.precise.";
  904. raw_string_ostream mangledNameStr(preciseFuncName);
  905. EltTy->print(mangledNameStr);
  906. mangledNameStr.flush();
  907. Function *preciseFunc =
  908. cast<Function>(M.getOrInsertFunction(preciseFuncName, preciseFuncTy));
  909. if (!HLModule::HasPreciseAttribute(preciseFunc))
  910. MarkPreciseAttribute(preciseFunc);
  911. if (Ty->isVectorTy()) {
  912. for (unsigned i = 0; i < Ty->getVectorNumElements(); i++) {
  913. Value *Elt = Builder.CreateExtractElement(V, i);
  914. Builder.CreateCall(preciseFunc, {Elt});
  915. }
  916. } else
  917. Builder.CreateCall(preciseFunc, {V});
  918. }
  919. void HLModule::MarkPreciseAttributeOnPtrWithFunctionCall(llvm::Value *Ptr,
  920. llvm::Module &M) {
  921. for (User *U : Ptr->users()) {
  922. // Skip load inst.
  923. if (LoadInst *LI = dyn_cast<LoadInst>(U))
  924. continue;
  925. if (StoreInst *SI = dyn_cast<StoreInst>(U)) {
  926. Value *V = SI->getValueOperand();
  927. if (isa<Instruction>(V)) {
  928. // Mark the Value with function call.
  929. IRBuilder<> Builder(SI);
  930. MarkPreciseAttributeOnValWithFunctionCall(V, Builder, M);
  931. }
  932. } else if (CallInst *CI = dyn_cast<CallInst>(U)) {
  933. if (CI->getType()->isVoidTy()) {
  934. IRBuilder<> Builder(CI);
  935. // For void type, cannot use as function arg.
  936. // Mark all arg for it?
  937. for (auto &arg : CI->arg_operands()) {
  938. MarkPreciseAttributeOnValWithFunctionCall(arg, Builder, M);
  939. }
  940. } else {
  941. IRBuilder<> Builder(CI->getNextNode());
  942. MarkPreciseAttributeOnValWithFunctionCall(CI, Builder, M);
  943. }
  944. } else {
  945. // Must be GEP here.
  946. GetElementPtrInst *GEP = cast<GetElementPtrInst>(U);
  947. MarkPreciseAttributeOnPtrWithFunctionCall(GEP, M);
  948. }
  949. }
  950. }
  951. bool HLModule::HasPreciseAttribute(Function *F) {
  952. MDNode *preciseNode =
  953. F->getMetadata(DxilMDHelper::kDxilPreciseAttributeMDName);
  954. return preciseNode != nullptr;
  955. }
  956. void HLModule::MarkDxilResourceAttrib(llvm::Function *F, MDNode *MD) {
  957. F->setMetadata(DxilMDHelper::kHLDxilResourceAttributeMDName, MD);
  958. }
  959. MDNode *HLModule::GetDxilResourceAttrib(llvm::Function *F) {
  960. return F->getMetadata(DxilMDHelper::kHLDxilResourceAttributeMDName);
  961. }
  962. void HLModule::MarkDxilResourceAttrib(llvm::Argument *Arg, llvm::MDNode *MD) {
  963. unsigned i = Arg->getArgNo();
  964. Function *F = Arg->getParent();
  965. DxilFunctionAnnotation *FuncAnnot = m_pTypeSystem->GetFunctionAnnotation(F);
  966. if (!FuncAnnot) {
  967. DXASSERT(0, "Invalid function");
  968. return;
  969. }
  970. DxilParameterAnnotation &ParamAnnot = FuncAnnot->GetParameterAnnotation(i);
  971. ParamAnnot.SetResourceAttribute(MD);
  972. }
  973. MDNode *HLModule::GetDxilResourceAttrib(llvm::Argument *Arg) {
  974. unsigned i = Arg->getArgNo();
  975. Function *F = Arg->getParent();
  976. DxilFunctionAnnotation *FuncAnnot = m_pTypeSystem->GetFunctionAnnotation(F);
  977. if (!FuncAnnot)
  978. return nullptr;
  979. DxilParameterAnnotation &ParamAnnot = FuncAnnot->GetParameterAnnotation(i);
  980. return ParamAnnot.GetResourceAttribute();
  981. }
  982. MDNode *HLModule::GetDxilResourceAttrib(Type *Ty, Module &M) {
  983. for (Function &F : M.functions()) {
  984. if (hlsl::GetHLOpcodeGroupByName(&F) == HLOpcodeGroup::HLCreateHandle) {
  985. Type *ResTy = F.getFunctionType()->getParamType(
  986. HLOperandIndex::kCreateHandleResourceOpIdx);
  987. if (ResTy == Ty)
  988. return GetDxilResourceAttrib(&F);
  989. }
  990. }
  991. return nullptr;
  992. }
  993. DIGlobalVariable *
  994. HLModule::FindGlobalVariableDebugInfo(GlobalVariable *GV,
  995. DebugInfoFinder &DbgInfoFinder) {
  996. struct GlobalFinder {
  997. GlobalVariable *GV;
  998. bool operator()(llvm::DIGlobalVariable *const arg) const {
  999. return arg->getVariable() == GV;
  1000. }
  1001. };
  1002. GlobalFinder F = {GV};
  1003. DebugInfoFinder::global_variable_iterator Found =
  1004. std::find_if(DbgInfoFinder.global_variables().begin(),
  1005. DbgInfoFinder.global_variables().end(), F);
  1006. if (Found != DbgInfoFinder.global_variables().end()) {
  1007. return *Found;
  1008. }
  1009. return nullptr;
  1010. }
  1011. static void AddDIGlobalVariable(DIBuilder &Builder, DIGlobalVariable *LocDIGV,
  1012. StringRef Name, DIType *DITy,
  1013. GlobalVariable *GV, DebugInfoFinder &DbgInfoFinder, bool removeLocDIGV) {
  1014. DIGlobalVariable *EltDIGV = Builder.createGlobalVariable(
  1015. LocDIGV->getScope(), Name, GV->getName(), LocDIGV->getFile(),
  1016. LocDIGV->getLine(), DITy, false, GV);
  1017. DICompileUnit *DICU = dyn_cast<DICompileUnit>(LocDIGV->getScope());
  1018. if (!DICU) {
  1019. DISubprogram *DIS = dyn_cast<DISubprogram>(LocDIGV->getScope());
  1020. if (DIS) {
  1021. // Find the DICU which has this Subprogram.
  1022. NamedMDNode *CompileUnits = GV->getParent()->getNamedMetadata("llvm.dbg.cu");
  1023. DXASSERT_NOMSG(CompileUnits);
  1024. for (unsigned I = 0, E = CompileUnits->getNumOperands(); I != E; ++I) {
  1025. auto *CU = cast<DICompileUnit>(CompileUnits->getOperand(I));
  1026. DXASSERT(CU , "Expected valid compile unit");
  1027. for (DISubprogram *SP : CU->getSubprograms()) {
  1028. if (SP == DIS) {
  1029. DICU = CU;
  1030. break;
  1031. }
  1032. }
  1033. }
  1034. }
  1035. }
  1036. DXASSERT_NOMSG(DICU);
  1037. // Add global to CU.
  1038. auto *GlobalVariables = DICU->getRawGlobalVariables();
  1039. DXASSERT_NOMSG(GlobalVariables);
  1040. MDTuple *GTuple = cast<MDTuple>(GlobalVariables);
  1041. std::vector<Metadata *> AllGVs(GTuple->operands().begin(),
  1042. GTuple->operands().end());
  1043. if (removeLocDIGV) {
  1044. auto locIt = std::find(AllGVs.begin(), AllGVs.end(), LocDIGV);
  1045. AllGVs.erase(locIt);
  1046. }
  1047. AllGVs.emplace_back(EltDIGV);
  1048. DICU->replaceGlobalVariables(MDTuple::get(GV->getContext(), AllGVs));
  1049. DXVERIFY_NOMSG(DbgInfoFinder.appendGlobalVariable(EltDIGV));
  1050. }
  1051. void HLModule::CreateElementGlobalVariableDebugInfo(
  1052. GlobalVariable *GV, DebugInfoFinder &DbgInfoFinder, GlobalVariable *EltGV,
  1053. unsigned sizeInBits, unsigned alignInBits, unsigned offsetInBits,
  1054. StringRef eltName) {
  1055. DIGlobalVariable *DIGV = FindGlobalVariableDebugInfo(GV, DbgInfoFinder);
  1056. DXASSERT_NOMSG(DIGV);
  1057. DIBuilder Builder(*GV->getParent());
  1058. DITypeIdentifierMap EmptyMap;
  1059. DIType *DITy = DIGV->getType().resolve(EmptyMap);
  1060. DIScope *DITyScope = DITy->getScope().resolve(EmptyMap);
  1061. // Create Elt type.
  1062. DIType *EltDITy =
  1063. Builder.createMemberType(DITyScope, DITy->getName().str() + eltName.str(),
  1064. DITy->getFile(), DITy->getLine(), sizeInBits,
  1065. alignInBits, offsetInBits, /*Flags*/ 0, DITy);
  1066. AddDIGlobalVariable(Builder, DIGV, DIGV->getName().str() + eltName.str(),
  1067. EltDITy, EltGV, DbgInfoFinder, /*removeDIGV*/false);
  1068. }
  1069. void HLModule::UpdateGlobalVariableDebugInfo(
  1070. llvm::GlobalVariable *GV, llvm::DebugInfoFinder &DbgInfoFinder,
  1071. llvm::GlobalVariable *NewGV) {
  1072. DIGlobalVariable *DIGV = FindGlobalVariableDebugInfo(GV, DbgInfoFinder);
  1073. DXASSERT_NOMSG(DIGV);
  1074. DIBuilder Builder(*GV->getParent());
  1075. DITypeIdentifierMap EmptyMap;
  1076. DIType *DITy = DIGV->getType().resolve(EmptyMap);
  1077. AddDIGlobalVariable(Builder, DIGV, DIGV->getName(), DITy, NewGV,
  1078. DbgInfoFinder,/*removeDIGV*/true);
  1079. }
  1080. DebugInfoFinder &HLModule::GetOrCreateDebugInfoFinder() {
  1081. if (m_pDebugInfoFinder == nullptr) {
  1082. m_pDebugInfoFinder = llvm::make_unique<llvm::DebugInfoFinder>();
  1083. m_pDebugInfoFinder->processModule(*m_pModule);
  1084. }
  1085. return *m_pDebugInfoFinder;
  1086. }
  1087. //------------------------------------------------------------------------------
  1088. //
  1089. // Signature methods.
  1090. //
  1091. HLExtraPropertyHelper::HLExtraPropertyHelper(llvm::Module *pModule)
  1092. : DxilExtraPropertyHelper(pModule) {
  1093. }
  1094. void HLExtraPropertyHelper::EmitSignatureElementProperties(const DxilSignatureElement &SE,
  1095. vector<Metadata *> &MDVals) {
  1096. }
  1097. void HLExtraPropertyHelper::LoadSignatureElementProperties(const MDOperand &MDO,
  1098. DxilSignatureElement &SE) {
  1099. if (MDO.get() == nullptr)
  1100. return;
  1101. }
  1102. } // namespace hlsl
  1103. namespace llvm {
  1104. hlsl::HLModule &Module::GetOrCreateHLModule(bool skipInit) {
  1105. std::unique_ptr<hlsl::HLModule> M;
  1106. if (!HasHLModule()) {
  1107. M = llvm::make_unique<hlsl::HLModule>(this);
  1108. if (!skipInit) {
  1109. M->LoadHLMetadata();
  1110. }
  1111. SetHLModule(M.release());
  1112. }
  1113. return GetHLModule();
  1114. }
  1115. void Module::ResetHLModule() {
  1116. if (HasHLModule()) {
  1117. delete TheHLModule;
  1118. TheHLModule = nullptr;
  1119. }
  1120. }
  1121. }