HLModule.cpp 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216
  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/IR/Constants.h"
  18. #include "llvm/IR/Function.h"
  19. #include "llvm/IR/IRBuilder.h"
  20. #include "llvm/IR/LLVMContext.h"
  21. #include "llvm/IR/Metadata.h"
  22. #include "llvm/IR/Module.h"
  23. #include "llvm/IR/DebugInfo.h"
  24. #include "llvm/IR/DIBuilder.h"
  25. #include "llvm/Support/raw_ostream.h"
  26. using namespace llvm;
  27. using std::string;
  28. using std::vector;
  29. using std::unique_ptr;
  30. namespace hlsl {
  31. static void
  32. CreateSignatures(const ShaderModel *pSM,
  33. std::unique_ptr<DxilSignature> &InputSignature,
  34. std::unique_ptr<DxilSignature> &OutputSignature,
  35. std::unique_ptr<DxilSignature> &PatchConstantSignature,
  36. std::unique_ptr<RootSignatureHandle> &RootSignature) {
  37. DXIL::ShaderKind shaderKind = pSM->GetKind();
  38. InputSignature = std::make_unique<DxilSignature>(shaderKind, DxilSignature::Kind::Input);
  39. OutputSignature = std::make_unique<DxilSignature>(shaderKind, DxilSignature::Kind::Output);
  40. PatchConstantSignature = std::make_unique<DxilSignature>(shaderKind, DxilSignature::Kind::PatchConstant);
  41. RootSignature = std::make_unique<RootSignatureHandle>();
  42. }
  43. //------------------------------------------------------------------------------
  44. //
  45. // HLModule methods.
  46. //
  47. HLModule::HLModule(Module *pModule)
  48. : m_Ctx(pModule->getContext())
  49. , m_pModule(pModule)
  50. , m_pEntryFunc(nullptr)
  51. , m_EntryName("")
  52. , m_pSM(nullptr)
  53. , m_pOP(std::make_unique<OP>(pModule->getContext(), pModule))
  54. , m_pTypeSystem(std::make_unique<DxilTypeSystem>(pModule))
  55. , m_pMDHelper(std::make_unique<DxilMDHelper>(
  56. pModule, std::make_unique<HLExtraPropertyHelper>(pModule)))
  57. , m_pDebugInfoFinder(nullptr)
  58. , m_DxilMajor(1)
  59. , m_DxilMinor(0) {
  60. DXASSERT_NOMSG(m_pModule != nullptr);
  61. // Pin LLVM dump methods. TODO: make debug-only.
  62. void (__thiscall Module::*pfnModuleDump)() const = &Module::dump;
  63. void (__thiscall Type::*pfnTypeDump)() const = &Type::dump;
  64. m_pUnused = (char *)&pfnModuleDump - (char *)&pfnTypeDump;
  65. }
  66. HLModule::~HLModule() {
  67. }
  68. LLVMContext &HLModule::GetCtx() const { return m_Ctx; }
  69. Module *HLModule::GetModule() const { return m_pModule; }
  70. OP *HLModule::GetOP() const { return m_pOP.get(); }
  71. void HLModule::SetShaderModel(const ShaderModel *pSM) {
  72. DXASSERT(m_pSM == nullptr, "shader model must not change for the module");
  73. m_pSM = pSM;
  74. m_pMDHelper->SetShaderModel(m_pSM);
  75. CreateSignatures(m_pSM, m_InputSignature, m_OutputSignature, m_PatchConstantSignature, m_RootSignature);
  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. void HLModule::SetEntryFunction(Function *pEntryFunc) {
  114. m_pEntryFunc = pEntryFunc;
  115. }
  116. const string &HLModule::GetEntryFunctionName() const { return m_EntryName; }
  117. void HLModule::SetEntryFunctionName(const string &name) { m_EntryName = name; }
  118. template<typename T> unsigned
  119. HLModule::AddResource(vector<unique_ptr<T> > &Vec, unique_ptr<T> pRes) {
  120. DXASSERT_NOMSG((unsigned)Vec.size() < UINT_MAX);
  121. unsigned Id = (unsigned)Vec.size();
  122. Vec.emplace_back(std::move(pRes));
  123. return Id;
  124. }
  125. unsigned HLModule::AddCBuffer(unique_ptr<DxilCBuffer> pCBuffer) {
  126. return AddResource<DxilCBuffer>(m_CBuffers, std::move(pCBuffer));
  127. }
  128. DxilCBuffer &HLModule::GetCBuffer(unsigned idx) {
  129. return *m_CBuffers[idx];
  130. }
  131. const DxilCBuffer &HLModule::GetCBuffer(unsigned idx) const {
  132. return *m_CBuffers[idx];
  133. }
  134. const vector<unique_ptr<DxilCBuffer> > &HLModule::GetCBuffers() const {
  135. return m_CBuffers;
  136. }
  137. unsigned HLModule::AddSampler(unique_ptr<DxilSampler> pSampler) {
  138. return AddResource<DxilSampler>(m_Samplers, std::move(pSampler));
  139. }
  140. DxilSampler &HLModule::GetSampler(unsigned idx) {
  141. return *m_Samplers[idx];
  142. }
  143. const DxilSampler &HLModule::GetSampler(unsigned idx) const {
  144. return *m_Samplers[idx];
  145. }
  146. const vector<unique_ptr<DxilSampler> > &HLModule::GetSamplers() const {
  147. return m_Samplers;
  148. }
  149. unsigned HLModule::AddSRV(unique_ptr<HLResource> pSRV) {
  150. return AddResource<HLResource>(m_SRVs, std::move(pSRV));
  151. }
  152. HLResource &HLModule::GetSRV(unsigned idx) {
  153. return *m_SRVs[idx];
  154. }
  155. const HLResource &HLModule::GetSRV(unsigned idx) const {
  156. return *m_SRVs[idx];
  157. }
  158. const vector<unique_ptr<HLResource> > &HLModule::GetSRVs() const {
  159. return m_SRVs;
  160. }
  161. unsigned HLModule::AddUAV(unique_ptr<HLResource> pUAV) {
  162. return AddResource<HLResource>(m_UAVs, std::move(pUAV));
  163. }
  164. HLResource &HLModule::GetUAV(unsigned idx) {
  165. return *m_UAVs[idx];
  166. }
  167. const HLResource &HLModule::GetUAV(unsigned idx) const {
  168. return *m_UAVs[idx];
  169. }
  170. const vector<unique_ptr<HLResource> > &HLModule::GetUAVs() const {
  171. return m_UAVs;
  172. }
  173. void HLModule::RemoveFunction(llvm::Function *F) {
  174. DXASSERT_NOMSG(F != nullptr);
  175. m_HLFunctionPropsMap.erase(F);
  176. if (m_pTypeSystem.get()->GetFunctionAnnotation(F))
  177. m_pTypeSystem.get()->EraseFunctionAnnotation(F);
  178. }
  179. template <typename TResource>
  180. bool RemoveResource(std::vector<std::unique_ptr<TResource>> &vec,
  181. GlobalVariable *pVariable) {
  182. for (auto p = vec.begin(), e = vec.end(); p != e; ++p) {
  183. if ((*p)->GetGlobalSymbol() == pVariable) {
  184. p = vec.erase(p);
  185. // Update ID.
  186. for (e = vec.end();p != e; ++p) {
  187. unsigned ID = (*p)->GetID()-1;
  188. (*p)->SetID(ID);
  189. }
  190. return true;
  191. }
  192. }
  193. return false;
  194. }
  195. bool RemoveResource(std::vector<GlobalVariable *> &vec,
  196. llvm::GlobalVariable *pVariable) {
  197. for (auto p = vec.begin(), e = vec.end(); p != e; ++p) {
  198. if (*p == pVariable) {
  199. vec.erase(p);
  200. return true;
  201. }
  202. }
  203. return false;
  204. }
  205. void HLModule::RemoveGlobal(llvm::GlobalVariable *GV) {
  206. RemoveResources(&GV, 1);
  207. }
  208. void HLModule::RemoveResources(llvm::GlobalVariable **ppVariables,
  209. unsigned count) {
  210. DXASSERT_NOMSG(count == 0 || ppVariables != nullptr);
  211. unsigned resourcesRemoved = count;
  212. for (unsigned i = 0; i < count; ++i) {
  213. GlobalVariable *pVariable = ppVariables[i];
  214. // This could be considerably faster - check variable type to see which
  215. // resource type this is rather than scanning all lists, and look for
  216. // usage and removal patterns.
  217. if (RemoveResource(m_CBuffers, pVariable))
  218. continue;
  219. if (RemoveResource(m_SRVs, pVariable))
  220. continue;
  221. if (RemoveResource(m_UAVs, pVariable))
  222. continue;
  223. if (RemoveResource(m_Samplers, pVariable))
  224. continue;
  225. // TODO: do m_TGSMVariables and m_StreamOutputs need maintenance?
  226. --resourcesRemoved; // Global variable is not a resource?
  227. }
  228. }
  229. HLModule::tgsm_iterator HLModule::tgsm_begin() {
  230. return m_TGSMVariables.begin();
  231. }
  232. HLModule::tgsm_iterator HLModule::tgsm_end() {
  233. return m_TGSMVariables.end();
  234. }
  235. void HLModule::AddGroupSharedVariable(GlobalVariable *GV) {
  236. m_TGSMVariables.emplace_back(GV);
  237. }
  238. DxilSignature &HLModule::GetInputSignature() {
  239. return *m_InputSignature;
  240. }
  241. DxilSignature &HLModule::GetOutputSignature() {
  242. return *m_OutputSignature;
  243. }
  244. DxilSignature &HLModule::GetPatchConstantSignature() {
  245. return *m_PatchConstantSignature;
  246. }
  247. RootSignatureHandle &HLModule::GetRootSignature() {
  248. return *m_RootSignature;
  249. }
  250. DxilTypeSystem &HLModule::GetTypeSystem() {
  251. return *m_pTypeSystem;
  252. }
  253. DxilSignature *HLModule::ReleaseInputSignature() {
  254. return m_InputSignature.release();
  255. }
  256. DxilSignature *HLModule::ReleaseOutputSignature() {
  257. return m_OutputSignature.release();
  258. }
  259. DxilSignature *HLModule::ReleasePatchConstantSignature() {
  260. return m_PatchConstantSignature.release();
  261. }
  262. DxilTypeSystem *HLModule::ReleaseTypeSystem() {
  263. return m_pTypeSystem.release();
  264. }
  265. RootSignatureHandle *HLModule::ReleaseRootSignature() {
  266. return m_RootSignature.release();
  267. }
  268. void HLModule::EmitLLVMUsed() {
  269. if (m_LLVMUsed.empty())
  270. return;
  271. vector<llvm::Constant*> GVs;
  272. GVs.resize(m_LLVMUsed.size());
  273. for (size_t i = 0, e = m_LLVMUsed.size(); i != e; i++) {
  274. GVs[i] = ConstantExpr::getAddrSpaceCast(cast<llvm::Constant>(&*m_LLVMUsed[i]), Type::getInt8PtrTy(m_Ctx));
  275. }
  276. ArrayType *pATy = ArrayType::get(Type::getInt8PtrTy(m_Ctx), GVs.size());
  277. GlobalVariable *pGV = new GlobalVariable(*m_pModule, pATy, false,
  278. GlobalValue::AppendingLinkage,
  279. ConstantArray::get(pATy, GVs),
  280. "llvm.used");
  281. pGV->setSection("llvm.metadata");
  282. }
  283. vector<GlobalVariable* > &HLModule::GetLLVMUsed() {
  284. return m_LLVMUsed;
  285. }
  286. bool HLModule::HasHLFunctionProps(llvm::Function *F) {
  287. return m_HLFunctionPropsMap.find(F) != m_HLFunctionPropsMap.end();
  288. }
  289. HLFunctionProps &HLModule::GetHLFunctionProps(llvm::Function *F) {
  290. return *m_HLFunctionPropsMap[F];
  291. }
  292. void HLModule::AddHLFunctionProps(llvm::Function *F, std::unique_ptr<HLFunctionProps> &info) {
  293. DXASSERT(m_HLFunctionPropsMap.count(F) == 0, "F already in map, info will be overwritten");
  294. m_HLFunctionPropsMap[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. static const StringRef kHLDxilFunctionPropertiesMDName = "dx.fnprops";
  334. static const StringRef kHLDxilOptionsMDName = "dx.options";
  335. static const StringRef kHLDxilResourceTypeAnnotationMDName = "dx.resource.type.annotation";
  336. // DXIL metadata serialization/deserialization.
  337. void HLModule::EmitHLMetadata() {
  338. m_pMDHelper->EmitDxilVersion(m_DxilMajor, m_DxilMinor);
  339. m_pMDHelper->EmitDxilShaderModel(m_pSM);
  340. MDTuple *pMDSignatures = m_pMDHelper->EmitDxilSignatures(*m_InputSignature,
  341. *m_OutputSignature,
  342. *m_PatchConstantSignature);
  343. MDTuple *pMDResources = EmitHLResources();
  344. MDTuple *pMDProperties = EmitHLShaderProperties();
  345. m_pMDHelper->EmitDxilTypeSystem(GetTypeSystem(), m_LLVMUsed);
  346. EmitLLVMUsed();
  347. MDTuple *pEntry = m_pMDHelper->EmitDxilEntryPointTuple(GetEntryFunction(), m_EntryName, pMDSignatures, pMDResources, pMDProperties);
  348. vector<MDNode *> Entries;
  349. Entries.emplace_back(pEntry);
  350. m_pMDHelper->EmitDxilEntryPoints(Entries);
  351. {
  352. NamedMDNode * fnProps = m_pModule->getOrInsertNamedMetadata(kHLDxilFunctionPropertiesMDName);
  353. for (auto && pair : m_HLFunctionPropsMap) {
  354. const hlsl::HLFunctionProps * props = pair.second.get();
  355. Metadata *MDVals[30];
  356. std::fill(MDVals, MDVals + _countof(MDVals), nullptr);
  357. unsigned valIdx = 0;
  358. MDVals[valIdx++] = ValueAsMetadata::get(pair.first);
  359. switch (m_pSM->GetKind()) {
  360. case DXIL::ShaderKind::Compute:
  361. MDVals[valIdx++] = m_pMDHelper->Uint32ToConstMD(props->ShaderProps.CS.numThreads[0]);
  362. MDVals[valIdx++] = m_pMDHelper->Uint32ToConstMD(props->ShaderProps.CS.numThreads[1]);
  363. MDVals[valIdx++] = m_pMDHelper->Uint32ToConstMD(props->ShaderProps.CS.numThreads[2]);
  364. break;
  365. case DXIL::ShaderKind::Geometry:
  366. MDVals[valIdx++] = m_pMDHelper->Uint8ToConstMD((uint8_t)props->ShaderProps.GS.inputPrimitive);
  367. MDVals[valIdx++] = m_pMDHelper->Uint32ToConstMD(props->ShaderProps.GS.maxVertexCount);
  368. for (size_t i = 0; i < _countof(props->ShaderProps.GS.streamPrimitiveTopologies); ++i)
  369. MDVals[valIdx++] = m_pMDHelper->Uint8ToConstMD((uint8_t)props->ShaderProps.GS.streamPrimitiveTopologies[i]);
  370. break;
  371. case DXIL::ShaderKind::Hull:
  372. MDVals[valIdx++] = ValueAsMetadata::get(props->ShaderProps.HS.patchConstantFunc);
  373. MDVals[valIdx++] = m_pMDHelper->Uint8ToConstMD((uint8_t)props->ShaderProps.HS.domain);
  374. MDVals[valIdx++] = m_pMDHelper->Uint8ToConstMD((uint8_t)props->ShaderProps.HS.partition);
  375. MDVals[valIdx++] = m_pMDHelper->Uint8ToConstMD((uint8_t)props->ShaderProps.HS.outputPrimitive);
  376. MDVals[valIdx++] = m_pMDHelper->Uint32ToConstMD(props->ShaderProps.HS.inputControlPoints);
  377. MDVals[valIdx++] = m_pMDHelper->Uint32ToConstMD(props->ShaderProps.HS.outputControlPoints);
  378. MDVals[valIdx++] = m_pMDHelper->FloatToConstMD(props->ShaderProps.HS.maxTessFactor);
  379. break;
  380. case DXIL::ShaderKind::Domain:
  381. MDVals[valIdx++] = m_pMDHelper->Uint8ToConstMD((uint8_t)props->ShaderProps.DS.domain);
  382. MDVals[valIdx++] = m_pMDHelper->Uint32ToConstMD(props->ShaderProps.DS.inputControlPoints);
  383. break;
  384. case DXIL::ShaderKind::Pixel:
  385. MDVals[valIdx++] = m_pMDHelper->BoolToConstMD(props->ShaderProps.PS.EarlyDepthStencil);
  386. break;
  387. }
  388. fnProps->addOperand(MDTuple::get(m_Ctx, ArrayRef<llvm::Metadata *>(MDVals, valIdx)));
  389. }
  390. NamedMDNode * options = m_pModule->getOrInsertNamedMetadata(kHLDxilOptionsMDName);
  391. uint32_t hlOptions = m_Options.GetHLOptionsRaw();
  392. options->addOperand(MDNode::get(m_Ctx, m_pMDHelper->Uint32ToConstMD(hlOptions)));
  393. NamedMDNode * resTyAnnotations = m_pModule->getOrInsertNamedMetadata(kHLDxilResourceTypeAnnotationMDName);
  394. resTyAnnotations->addOperand(EmitResTyAnnotations());
  395. }
  396. }
  397. void HLModule::LoadHLMetadata() {
  398. m_pMDHelper->LoadDxilVersion(m_DxilMajor, m_DxilMinor);
  399. m_pMDHelper->LoadDxilShaderModel(m_pSM);
  400. CreateSignatures(m_pSM, m_InputSignature, m_OutputSignature, m_PatchConstantSignature, m_RootSignature);
  401. const llvm::NamedMDNode *pEntries = m_pMDHelper->GetDxilEntryPoints();
  402. Function *pEntryFunc;
  403. string EntryName;
  404. const llvm::MDOperand *pSignatures, *pResources, *pProperties;
  405. m_pMDHelper->GetDxilEntryPoint(pEntries->getOperand(0), pEntryFunc, EntryName, pSignatures, pResources, pProperties);
  406. SetEntryFunction(pEntryFunc);
  407. SetEntryFunctionName(EntryName);
  408. m_pMDHelper->LoadDxilSignatures(*pSignatures, *m_InputSignature,
  409. *m_OutputSignature, *m_PatchConstantSignature);
  410. LoadHLResources(*pResources);
  411. LoadHLShaderProperties(*pProperties);
  412. m_pMDHelper->LoadDxilTypeSystem(*m_pTypeSystem.get());
  413. {
  414. NamedMDNode * fnProps = m_pModule->getNamedMetadata(kHLDxilFunctionPropertiesMDName);
  415. size_t propIdx = 0;
  416. while (propIdx < fnProps->getNumOperands()) {
  417. MDTuple *pProps = dyn_cast<MDTuple>(fnProps->getOperand(propIdx++));
  418. std::unique_ptr<hlsl::HLFunctionProps> props = std::make_unique<hlsl::HLFunctionProps>();
  419. unsigned idx = 0;
  420. Function *F = dyn_cast<Function>(dyn_cast<ValueAsMetadata>(pProps->getOperand(idx++))->getValue());
  421. switch (m_pSM->GetKind()) {
  422. case DXIL::ShaderKind::Compute:
  423. props->ShaderProps.CS.numThreads[0] = GetIntAt(pProps, idx++);
  424. props->ShaderProps.CS.numThreads[1] = GetIntAt(pProps, idx++);
  425. props->ShaderProps.CS.numThreads[2] = GetIntAt(pProps, idx++);
  426. break;
  427. case DXIL::ShaderKind::Geometry:
  428. props->ShaderProps.GS.inputPrimitive = (DXIL::InputPrimitive)GetIntAt(pProps, idx++);
  429. props->ShaderProps.GS.maxVertexCount = GetIntAt(pProps, idx++);
  430. for (size_t i = 0; i < _countof(props->ShaderProps.GS.streamPrimitiveTopologies); ++i)
  431. props->ShaderProps.GS.streamPrimitiveTopologies[i] = (DXIL::PrimitiveTopology)GetIntAt(pProps, idx++);
  432. break;
  433. case DXIL::ShaderKind::Hull:
  434. props->ShaderProps.HS.patchConstantFunc = dyn_cast<Function>(dyn_cast<ValueAsMetadata>(pProps->getOperand(idx++))->getValue());
  435. props->ShaderProps.HS.domain = (DXIL::TessellatorDomain)GetIntAt(pProps, idx++);
  436. props->ShaderProps.HS.partition = (DXIL::TessellatorPartitioning)GetIntAt(pProps, idx++);
  437. props->ShaderProps.HS.outputPrimitive = (DXIL::TessellatorOutputPrimitive)GetIntAt(pProps, idx++);
  438. props->ShaderProps.HS.inputControlPoints = GetIntAt(pProps, idx++);
  439. props->ShaderProps.HS.outputControlPoints = GetIntAt(pProps, idx++);
  440. props->ShaderProps.HS.maxTessFactor = GetFloatAt(pProps, idx++);
  441. break;
  442. case DXIL::ShaderKind::Domain:
  443. props->ShaderProps.DS.domain = (DXIL::TessellatorDomain)GetIntAt(pProps, idx++);
  444. props->ShaderProps.DS.inputControlPoints = GetIntAt(pProps, idx++);
  445. break;
  446. case DXIL::ShaderKind::Pixel:
  447. props->ShaderProps.PS.EarlyDepthStencil = GetIntAt(pProps, idx++);
  448. break;
  449. }
  450. m_HLFunctionPropsMap[F] = std::move(props);
  451. }
  452. const NamedMDNode * options = m_pModule->getOrInsertNamedMetadata(kHLDxilOptionsMDName);
  453. const MDNode *MDOptions = options->getOperand(0);
  454. m_Options.SetHLOptionsRaw(DxilMDHelper::ConstMDToUint32(MDOptions->getOperand(0)));
  455. NamedMDNode * resTyAnnotations = m_pModule->getOrInsertNamedMetadata(kHLDxilResourceTypeAnnotationMDName);
  456. const MDNode *MDResTyAnnotations = resTyAnnotations->getOperand(0);
  457. if (MDResTyAnnotations->getNumOperands())
  458. LoadResTyAnnotations(MDResTyAnnotations->getOperand(0));
  459. }
  460. }
  461. void HLModule::ClearHLMetadata(llvm::Module &M) {
  462. Module::named_metadata_iterator
  463. b = M.named_metadata_begin(),
  464. e = M.named_metadata_end();
  465. SmallVector<NamedMDNode*, 8> nodes;
  466. for (; b != e; ++b) {
  467. StringRef name = b->getName();
  468. if (name == DxilMDHelper::kDxilVersionMDName ||
  469. name == DxilMDHelper::kDxilShaderModelMDName ||
  470. name == DxilMDHelper::kDxilEntryPointsMDName ||
  471. name == DxilMDHelper::kDxilResourcesMDName ||
  472. name == DxilMDHelper::kDxilTypeSystemMDName ||
  473. name == kHLDxilFunctionPropertiesMDName || // TODO: adjust to proper name
  474. name == kHLDxilResourceTypeAnnotationMDName ||
  475. name == kHLDxilOptionsMDName ||
  476. name.startswith(DxilMDHelper::kDxilTypeSystemHelperVariablePrefix)) {
  477. nodes.push_back(b);
  478. }
  479. }
  480. for (size_t i = 0; i < nodes.size(); ++i) {
  481. M.eraseNamedMetadata(nodes[i]);
  482. }
  483. }
  484. MDTuple *HLModule::EmitHLResources() {
  485. // Emit SRV records.
  486. MDTuple *pTupleSRVs = nullptr;
  487. if (!m_SRVs.empty()) {
  488. vector<Metadata *> MDVals;
  489. for (size_t i = 0; i < m_SRVs.size(); i++) {
  490. MDVals.emplace_back(m_pMDHelper->EmitDxilSRV(*m_SRVs[i]));
  491. }
  492. pTupleSRVs = MDNode::get(m_Ctx, MDVals);
  493. }
  494. // Emit UAV records.
  495. MDTuple *pTupleUAVs = nullptr;
  496. if (!m_UAVs.empty()) {
  497. vector<Metadata *> MDVals;
  498. for (size_t i = 0; i < m_UAVs.size(); i++) {
  499. MDVals.emplace_back(m_pMDHelper->EmitDxilUAV(*m_UAVs[i]));
  500. }
  501. pTupleUAVs = MDNode::get(m_Ctx, MDVals);
  502. }
  503. // Emit CBuffer records.
  504. MDTuple *pTupleCBuffers = nullptr;
  505. if (!m_CBuffers.empty()) {
  506. vector<Metadata *> MDVals;
  507. for (size_t i = 0; i < m_CBuffers.size(); i++) {
  508. MDVals.emplace_back(m_pMDHelper->EmitDxilCBuffer(*m_CBuffers[i]));
  509. }
  510. pTupleCBuffers = MDNode::get(m_Ctx, MDVals);
  511. }
  512. // Emit Sampler records.
  513. MDTuple *pTupleSamplers = nullptr;
  514. if (!m_Samplers.empty()) {
  515. vector<Metadata *> MDVals;
  516. for (size_t i = 0; i < m_Samplers.size(); i++) {
  517. MDVals.emplace_back(m_pMDHelper->EmitDxilSampler(*m_Samplers[i]));
  518. }
  519. pTupleSamplers = MDNode::get(m_Ctx, MDVals);
  520. }
  521. if (pTupleSRVs != nullptr || pTupleUAVs != nullptr || pTupleCBuffers != nullptr || pTupleSamplers != nullptr) {
  522. return m_pMDHelper->EmitDxilResourceTuple(pTupleSRVs, pTupleUAVs, pTupleCBuffers, pTupleSamplers);
  523. } else {
  524. return nullptr;
  525. }
  526. }
  527. void HLModule::LoadHLResources(const llvm::MDOperand &MDO) {
  528. const llvm::MDTuple *pSRVs, *pUAVs, *pCBuffers, *pSamplers;
  529. m_pMDHelper->GetDxilResources(MDO, pSRVs, pUAVs, pCBuffers, pSamplers);
  530. // Load SRV records.
  531. if (pSRVs != nullptr) {
  532. for (unsigned i = 0; i < pSRVs->getNumOperands(); i++) {
  533. unique_ptr<HLResource> pSRV(new HLResource);
  534. m_pMDHelper->LoadDxilSRV(pSRVs->getOperand(i), *pSRV);
  535. AddSRV(std::move(pSRV));
  536. }
  537. }
  538. // Load UAV records.
  539. if (pUAVs != nullptr) {
  540. for (unsigned i = 0; i < pUAVs->getNumOperands(); i++) {
  541. unique_ptr<HLResource> pUAV(new HLResource);
  542. m_pMDHelper->LoadDxilUAV(pUAVs->getOperand(i), *pUAV);
  543. AddUAV(std::move(pUAV));
  544. }
  545. }
  546. // Load CBuffer records.
  547. if (pCBuffers != nullptr) {
  548. for (unsigned i = 0; i < pCBuffers->getNumOperands(); i++) {
  549. unique_ptr<DxilCBuffer> pCB = std::make_unique<DxilCBuffer>();
  550. m_pMDHelper->LoadDxilCBuffer(pCBuffers->getOperand(i), *pCB);
  551. AddCBuffer(std::move(pCB));
  552. }
  553. }
  554. // Load Sampler records.
  555. if (pSamplers != nullptr) {
  556. for (unsigned i = 0; i < pSamplers->getNumOperands(); i++) {
  557. unique_ptr<DxilSampler> pSampler(new DxilSampler);
  558. m_pMDHelper->LoadDxilSampler(pSamplers->getOperand(i), *pSampler);
  559. AddSampler(std::move(pSampler));
  560. }
  561. }
  562. }
  563. llvm::MDTuple *HLModule::EmitResTyAnnotations() {
  564. vector<Metadata *> MDVals;
  565. for (auto &resAnnotation : m_ResTypeAnnotation) {
  566. Metadata *TyMeta =
  567. ValueAsMetadata::get(UndefValue::get(resAnnotation.first));
  568. MDVals.emplace_back(TyMeta);
  569. MDVals.emplace_back(m_pMDHelper->Uint32ToConstMD(
  570. static_cast<unsigned>(resAnnotation.second.first)));
  571. MDVals.emplace_back(m_pMDHelper->Uint32ToConstMD(
  572. static_cast<unsigned>(resAnnotation.second.second)));
  573. }
  574. return MDNode::get(m_Ctx, MDVals);
  575. }
  576. void HLModule::LoadResTyAnnotations(const llvm::MDOperand &MDO) {
  577. if (MDO.get() == nullptr)
  578. return;
  579. const MDTuple *pTupleMD = dyn_cast<MDTuple>(MDO.get());
  580. IFTBOOL(pTupleMD != nullptr, DXC_E_INCORRECT_DXIL_METADATA);
  581. IFTBOOL((pTupleMD->getNumOperands() & 0x3) == 0,
  582. DXC_E_INCORRECT_DXIL_METADATA);
  583. for (unsigned iNode = 0; iNode < pTupleMD->getNumOperands(); iNode += 3) {
  584. const MDOperand &MDTy = pTupleMD->getOperand(iNode);
  585. const MDOperand &MDClass = pTupleMD->getOperand(iNode + 1);
  586. const MDOperand &MDKind = pTupleMD->getOperand(iNode + 2);
  587. Type *Ty = m_pMDHelper->ValueMDToValue(MDTy)->getType();
  588. DXIL::ResourceClass resClass = static_cast<DXIL::ResourceClass>(
  589. DxilMDHelper::ConstMDToUint32(MDClass));
  590. DXIL::ResourceKind kind =
  591. static_cast<DXIL::ResourceKind>(DxilMDHelper::ConstMDToUint32(MDKind));
  592. AddResourceTypeAnnotation(Ty, resClass, kind);
  593. }
  594. }
  595. MDTuple *HLModule::EmitHLShaderProperties() {
  596. vector<Metadata *> MDVals;
  597. if (!m_RootSignature->IsEmpty()) {
  598. MDVals.emplace_back(m_pMDHelper->Uint32ToConstMD(DxilMDHelper::kDxilRootSignatureTag));
  599. MDVals.emplace_back(m_pMDHelper->EmitRootSignature(*m_RootSignature.get()));
  600. }
  601. return MDNode::get(m_Ctx, MDVals);
  602. }
  603. void HLModule::LoadHLShaderProperties(const MDOperand &MDO) {
  604. if (MDO.get() == nullptr)
  605. return;
  606. const MDTuple *pTupleMD = dyn_cast<MDTuple>(MDO.get());
  607. IFTBOOL(pTupleMD != nullptr, DXC_E_INCORRECT_DXIL_METADATA);
  608. IFTBOOL((pTupleMD->getNumOperands() & 0x1) == 0, DXC_E_INCORRECT_DXIL_METADATA);
  609. for (unsigned iNode = 0; iNode < pTupleMD->getNumOperands(); iNode += 2) {
  610. unsigned Tag = DxilMDHelper::ConstMDToUint32(pTupleMD->getOperand(iNode));
  611. const MDOperand &MDO = pTupleMD->getOperand(iNode + 1);
  612. IFTBOOL(MDO.get() != nullptr, DXC_E_INCORRECT_DXIL_METADATA);
  613. switch (Tag) {
  614. case DxilMDHelper::kDxilRootSignatureTag:
  615. m_pMDHelper->LoadRootSignature(MDO, *m_RootSignature.get());
  616. break;
  617. default:
  618. // Ignore other extended properties for now.
  619. break;
  620. }
  621. }
  622. }
  623. // TODO: Don't check names.
  624. bool HLModule::IsStreamOutputType(llvm::Type *Ty) {
  625. if (StructType *ST = dyn_cast<StructType>(Ty)) {
  626. if (ST->getName().startswith("class.PointStream"))
  627. return true;
  628. if (ST->getName().startswith("class.LineStream"))
  629. return true;
  630. if (ST->getName().startswith("class.TriangleStream"))
  631. return true;
  632. }
  633. return false;
  634. }
  635. bool HLModule::IsStreamOutputPtrType(llvm::Type *Ty) {
  636. if (!Ty->isPointerTy())
  637. return false;
  638. Ty = Ty->getPointerElementType();
  639. return IsStreamOutputType(Ty);
  640. }
  641. bool HLModule::IsHLSLObjectType(llvm::Type *Ty) {
  642. if (llvm::StructType *ST = dyn_cast<llvm::StructType>(Ty)) {
  643. StringRef name = ST->getName();
  644. if (name.startswith("dx.types.wave_t"))
  645. return true;
  646. if (name.endswith("_slice_type"))
  647. return false;
  648. name = name.ltrim("class.");
  649. name = name.ltrim("struct.");
  650. if (name == "SamplerState")
  651. return true;
  652. if (name == "SamplerComparisonState")
  653. return true;
  654. if (name.startswith("TriangleStream"))
  655. return true;
  656. if (name.startswith("PointStream"))
  657. return true;
  658. if (name.startswith("LineStream"))
  659. return true;
  660. if (name.startswith("AppendStructuredBuffer"))
  661. return true;
  662. if (name.startswith("ConsumeStructuredBuffer"))
  663. return true;
  664. if (name.startswith("ConstantBuffer"))
  665. return true;
  666. name = name.ltrim("RasterizerOrdered");
  667. name = name.ltrim("RW");
  668. if (name == "ByteAddressBuffer")
  669. return true;
  670. if (name.startswith("Buffer"))
  671. return true;
  672. if (name.startswith("StructuredBuffer"))
  673. return true;
  674. if (name.startswith("Texture1D"))
  675. return true;
  676. if (name.startswith("Texture1DArray"))
  677. return true;
  678. if (name.startswith("Texture2D"))
  679. return true;
  680. if (name.startswith("Texture2DArray"))
  681. return true;
  682. if (name.startswith("Texture3D"))
  683. return true;
  684. if (name.startswith("TextureCube"))
  685. return true;
  686. if (name.startswith("TextureCubeArray"))
  687. return true;
  688. if (name.startswith("Texture2DMS"))
  689. return true;
  690. if (name.startswith("Texture2DMSArray"))
  691. return true;
  692. }
  693. return false;
  694. }
  695. unsigned
  696. HLModule::GetLegacyCBufferFieldElementSize(DxilFieldAnnotation &fieldAnnotation,
  697. llvm::Type *Ty,
  698. DxilTypeSystem &typeSys) {
  699. while (isa<ArrayType>(Ty)) {
  700. Ty = Ty->getArrayElementType();
  701. }
  702. // Bytes.
  703. unsigned compSize = fieldAnnotation.GetCompType().Is64Bit()?8:4;
  704. unsigned fieldSize = compSize;
  705. if (Ty->isVectorTy()) {
  706. fieldSize *= Ty->getVectorNumElements();
  707. } else if (StructType *ST = dyn_cast<StructType>(Ty)) {
  708. DxilStructAnnotation *EltAnnotation = typeSys.GetStructAnnotation(ST);
  709. if (EltAnnotation) {
  710. fieldSize = EltAnnotation->GetCBufferSize();
  711. } else {
  712. // Calculate size when don't have annotation.
  713. if (fieldAnnotation.HasMatrixAnnotation()) {
  714. const DxilMatrixAnnotation &matAnnotation =
  715. fieldAnnotation.GetMatrixAnnotation();
  716. unsigned rows = matAnnotation.Rows;
  717. unsigned cols = matAnnotation.Cols;
  718. if (matAnnotation.Orientation == MatrixOrientation::ColumnMajor) {
  719. rows = cols;
  720. cols = matAnnotation.Rows;
  721. } else if (matAnnotation.Orientation != MatrixOrientation::RowMajor) {
  722. // Invalid matrix orientation.
  723. fieldSize = 0;
  724. }
  725. fieldSize = (rows - 1) * 16 + cols * 4;
  726. } else {
  727. // Cannot find struct annotation.
  728. fieldSize = 0;
  729. }
  730. }
  731. }
  732. return fieldSize;
  733. }
  734. bool HLModule::IsStaticGlobal(GlobalVariable *GV) {
  735. return GV->getLinkage() == GlobalValue::LinkageTypes::InternalLinkage &&
  736. GV->getType()->getPointerAddressSpace() == DXIL::kDefaultAddrSpace;
  737. }
  738. bool HLModule::IsSharedMemoryGlobal(llvm::GlobalVariable *GV) {
  739. return GV->getType()->getPointerAddressSpace() == DXIL::kTGSMAddrSpace;
  740. }
  741. void HLModule::GetParameterRowsAndCols(Type *Ty, unsigned &rows, unsigned &cols,
  742. DxilParameterAnnotation &paramAnnotation) {
  743. if (Ty->isPointerTy())
  744. Ty = Ty->getPointerElementType();
  745. // For array input of HS, DS, GS,
  746. // we need to skip the first level which size is based on primitive type.
  747. DxilParamInputQual inputQual = paramAnnotation.GetParamInputQual();
  748. bool skipOneLevelArray = inputQual == DxilParamInputQual::InputPatch;
  749. skipOneLevelArray |= inputQual == DxilParamInputQual::OutputPatch;
  750. skipOneLevelArray |= inputQual == DxilParamInputQual::InputPrimitive;
  751. if (skipOneLevelArray) {
  752. if (Ty->isArrayTy())
  753. Ty = Ty->getArrayElementType();
  754. }
  755. unsigned arraySize = 1;
  756. while (Ty->isArrayTy()) {
  757. arraySize *= Ty->getArrayNumElements();
  758. Ty = Ty->getArrayElementType();
  759. }
  760. rows = 1;
  761. cols = 1;
  762. if (paramAnnotation.HasMatrixAnnotation()) {
  763. const DxilMatrixAnnotation &matrix = paramAnnotation.GetMatrixAnnotation();
  764. if (matrix.Orientation == MatrixOrientation::RowMajor) {
  765. rows = matrix.Rows;
  766. cols = matrix.Cols;
  767. } else {
  768. DXASSERT(matrix.Orientation == MatrixOrientation::ColumnMajor, "");
  769. cols = matrix.Rows;
  770. rows = matrix.Cols;
  771. }
  772. } else if (Ty->isVectorTy())
  773. cols = Ty->getVectorNumElements();
  774. rows *= arraySize;
  775. }
  776. // For legacy data layout, everything less than 32 align to 32.
  777. static const StringRef kLegacyLayoutString = "e-m:e-p:32:32-i1:32:32-i8:32:32-i16:32:32-i64:64-f16:32-f80:32-n8:16:32-a:0:32-S32";
  778. const char *HLModule::GetLegacyDataLayoutDesc() {
  779. return kLegacyLayoutString.data();
  780. }
  781. template<typename BuilderTy>
  782. Value *HLModule::EmitHLOperationCall(BuilderTy &Builder,
  783. HLOpcodeGroup group, unsigned opcode,
  784. Type *RetType,
  785. ArrayRef<Value *> paramList,
  786. llvm::Module &M) {
  787. SmallVector<llvm::Type *, 4> paramTyList;
  788. // Add the opcode param
  789. llvm::Type *opcodeTy = llvm::Type::getInt32Ty(M.getContext());
  790. paramTyList.emplace_back(opcodeTy);
  791. for (Value *param : paramList) {
  792. paramTyList.emplace_back(param->getType());
  793. }
  794. llvm::FunctionType *funcTy =
  795. llvm::FunctionType::get(RetType, paramTyList, false);
  796. Function *opFunc = GetOrCreateHLFunction(M, funcTy, group, opcode);
  797. SmallVector<Value *, 4> opcodeParamList;
  798. Value *opcodeConst = Constant::getIntegerValue(opcodeTy, APInt(32, opcode));
  799. opcodeParamList.emplace_back(opcodeConst);
  800. opcodeParamList.append(paramList.begin(), paramList.end());
  801. return Builder.CreateCall(opFunc, opcodeParamList);
  802. }
  803. template
  804. Value *HLModule::EmitHLOperationCall(IRBuilder<> &Builder,
  805. HLOpcodeGroup group, unsigned opcode,
  806. Type *RetType,
  807. ArrayRef<Value *> paramList,
  808. llvm::Module &M);
  809. unsigned HLModule::FindCastOp(bool fromUnsigned, bool toUnsigned,
  810. llvm::Type *SrcTy, llvm::Type *DstTy) {
  811. Instruction::CastOps castOp = llvm::Instruction::CastOps::BitCast;
  812. if (SrcTy->isAggregateType() || DstTy->isAggregateType())
  813. return llvm::Instruction::CastOps::BitCast;
  814. uint32_t SrcBitSize = SrcTy->getScalarSizeInBits();
  815. uint32_t DstBitSize = DstTy->getScalarSizeInBits();
  816. if (SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy()) {
  817. if (SrcBitSize > DstBitSize)
  818. return Instruction::Trunc;
  819. if (toUnsigned)
  820. return Instruction::ZExt;
  821. else
  822. return Instruction::SExt;
  823. }
  824. if (SrcTy->isFPOrFPVectorTy() && DstTy->isFPOrFPVectorTy()) {
  825. if (SrcBitSize > DstBitSize)
  826. return Instruction::FPTrunc;
  827. else
  828. return Instruction::FPExt;
  829. }
  830. if (SrcTy->isIntOrIntVectorTy() && DstTy->isFPOrFPVectorTy())
  831. if (fromUnsigned)
  832. return Instruction::UIToFP;
  833. else
  834. return Instruction::SIToFP;
  835. if (SrcTy->isFPOrFPVectorTy() && DstTy->isIntOrIntVectorTy())
  836. if (toUnsigned)
  837. return Instruction::FPToUI;
  838. else
  839. return Instruction::FPToSI;
  840. DXASSERT_NOMSG(0);
  841. return castOp;
  842. }
  843. bool HLModule::HasPreciseAttributeWithMetadata(Instruction *I) {
  844. MDNode *preciseNode =
  845. I->getMetadata(DxilMDHelper::kDxilPreciseAttributeMDName);
  846. return preciseNode != nullptr;
  847. }
  848. void HLModule::MarkPreciseAttributeWithMetadata(Instruction *I) {
  849. LLVMContext &Ctx = I->getContext();
  850. MDNode *preciseNode = MDNode::get(
  851. Ctx, {MDString::get(Ctx, DxilMDHelper::kDxilPreciseAttributeMDName)});
  852. I->setMetadata(DxilMDHelper::kDxilPreciseAttributeMDName, preciseNode);
  853. }
  854. void HLModule::ClearPreciseAttributeWithMetadata(Instruction *I) {
  855. I->setMetadata(DxilMDHelper::kDxilPreciseAttributeMDName, nullptr);
  856. }
  857. static void MarkPreciseAttribute(Function *F) {
  858. LLVMContext &Ctx = F->getContext();
  859. MDNode *preciseNode = MDNode::get(
  860. Ctx, {MDString::get(Ctx, DxilMDHelper::kDxilPreciseAttributeMDName)});
  861. F->setMetadata(DxilMDHelper::kDxilPreciseAttributeMDName, preciseNode);
  862. }
  863. static void MarkPreciseAttributeOnValWithFunctionCall(
  864. llvm::Value *V, llvm::IRBuilder<> &Builder, llvm::Module &M) {
  865. Type *Ty = V->getType();
  866. Type *EltTy = Ty->getScalarType();
  867. // TODO: Only do this on basic types.
  868. FunctionType *preciseFuncTy =
  869. FunctionType::get(Type::getVoidTy(M.getContext()), {EltTy}, false);
  870. // The function will be deleted after precise propagate.
  871. std::string preciseFuncName = "dx.attribute.precise.";
  872. raw_string_ostream mangledNameStr(preciseFuncName);
  873. EltTy->print(mangledNameStr);
  874. mangledNameStr.flush();
  875. Function *preciseFunc =
  876. cast<Function>(M.getOrInsertFunction(preciseFuncName, preciseFuncTy));
  877. if (!HLModule::HasPreciseAttribute(preciseFunc))
  878. MarkPreciseAttribute(preciseFunc);
  879. if (Ty->isVectorTy()) {
  880. for (unsigned i = 0; i < Ty->getVectorNumElements(); i++) {
  881. Value *Elt = Builder.CreateExtractElement(V, i);
  882. Builder.CreateCall(preciseFunc, {Elt});
  883. }
  884. } else
  885. Builder.CreateCall(preciseFunc, {V});
  886. }
  887. void HLModule::MarkPreciseAttributeOnPtrWithFunctionCall(llvm::Value *Ptr,
  888. llvm::Module &M) {
  889. for (User *U : Ptr->users()) {
  890. // Skip load inst.
  891. if (LoadInst *LI = dyn_cast<LoadInst>(U))
  892. continue;
  893. if (StoreInst *SI = dyn_cast<StoreInst>(U)) {
  894. Value *V = SI->getValueOperand();
  895. if (isa<Instruction>(V)) {
  896. // Mark the Value with function call.
  897. IRBuilder<> Builder(SI);
  898. MarkPreciseAttributeOnValWithFunctionCall(V, Builder, M);
  899. }
  900. } else if (CallInst *CI = dyn_cast<CallInst>(U)) {
  901. if (CI->getType()->isVoidTy()) {
  902. IRBuilder<> Builder(CI);
  903. // For void type, cannot use as function arg.
  904. // Mark all arg for it?
  905. for (auto &arg : CI->arg_operands()) {
  906. MarkPreciseAttributeOnValWithFunctionCall(arg, Builder, M);
  907. }
  908. } else {
  909. IRBuilder<> Builder(CI->getNextNode());
  910. MarkPreciseAttributeOnValWithFunctionCall(CI, Builder, M);
  911. }
  912. } else {
  913. // Must be GEP here.
  914. GetElementPtrInst *GEP = cast<GetElementPtrInst>(U);
  915. MarkPreciseAttributeOnPtrWithFunctionCall(GEP, M);
  916. }
  917. }
  918. }
  919. bool HLModule::HasPreciseAttribute(Function *F) {
  920. MDNode *preciseNode =
  921. F->getMetadata(DxilMDHelper::kDxilPreciseAttributeMDName);
  922. return preciseNode != nullptr;
  923. }
  924. DIGlobalVariable *
  925. HLModule::FindGlobalVariableDebugInfo(GlobalVariable *GV,
  926. DebugInfoFinder &DbgInfoFinder) {
  927. struct GlobalFinder {
  928. GlobalVariable *GV;
  929. bool operator()(llvm::DIGlobalVariable *const arg) const {
  930. return arg->getVariable() == GV;
  931. }
  932. };
  933. GlobalFinder F = {GV};
  934. DebugInfoFinder::global_variable_iterator Found =
  935. std::find_if(DbgInfoFinder.global_variables().begin(),
  936. DbgInfoFinder.global_variables().end(), F);
  937. if (Found != DbgInfoFinder.global_variables().end()) {
  938. return *Found;
  939. }
  940. return nullptr;
  941. }
  942. static void AddDIGlobalVariable(DIBuilder &Builder, DIGlobalVariable *LocDIGV,
  943. StringRef Name, DIType *DITy,
  944. GlobalVariable *GV, DebugInfoFinder &DbgInfoFinder, bool removeLocDIGV) {
  945. DIGlobalVariable *EltDIGV = Builder.createGlobalVariable(
  946. LocDIGV->getScope(), Name, GV->getName(), LocDIGV->getFile(),
  947. LocDIGV->getLine(), DITy, false, GV);
  948. DICompileUnit *DICU = dyn_cast<DICompileUnit>(LocDIGV->getScope());
  949. if (!DICU) {
  950. DISubprogram *DIS = dyn_cast<DISubprogram>(LocDIGV->getScope());
  951. if (DIS) {
  952. // Find the DICU which has this Subprogram.
  953. NamedMDNode *CompileUnits = GV->getParent()->getNamedMetadata("llvm.dbg.cu");
  954. DXASSERT_NOMSG(CompileUnits);
  955. for (unsigned I = 0, E = CompileUnits->getNumOperands(); I != E; ++I) {
  956. auto *CU = cast<DICompileUnit>(CompileUnits->getOperand(I));
  957. DXASSERT(CU , "Expected valid compile unit");
  958. for (DISubprogram *SP : CU->getSubprograms()) {
  959. if (SP == DIS) {
  960. DICU = CU;
  961. break;
  962. }
  963. }
  964. }
  965. }
  966. }
  967. DXASSERT_NOMSG(DICU);
  968. // Add global to CU.
  969. auto *GlobalVariables = DICU->getRawGlobalVariables();
  970. DXASSERT_NOMSG(GlobalVariables);
  971. MDTuple *GTuple = cast<MDTuple>(GlobalVariables);
  972. std::vector<Metadata *> AllGVs(GTuple->operands().begin(),
  973. GTuple->operands().end());
  974. if (removeLocDIGV) {
  975. auto locIt = std::find(AllGVs.begin(), AllGVs.end(), LocDIGV);
  976. AllGVs.erase(locIt);
  977. }
  978. AllGVs.emplace_back(EltDIGV);
  979. DICU->replaceGlobalVariables(MDTuple::get(GV->getContext(), AllGVs));
  980. DXVERIFY_NOMSG(DbgInfoFinder.appendGlobalVariable(EltDIGV));
  981. }
  982. void HLModule::CreateElementGlobalVariableDebugInfo(
  983. GlobalVariable *GV, DebugInfoFinder &DbgInfoFinder, GlobalVariable *EltGV,
  984. unsigned sizeInBits, unsigned alignInBits, unsigned offsetInBits,
  985. StringRef eltName) {
  986. DIGlobalVariable *DIGV = FindGlobalVariableDebugInfo(GV, DbgInfoFinder);
  987. DXASSERT_NOMSG(DIGV);
  988. DIBuilder Builder(*GV->getParent());
  989. DITypeIdentifierMap EmptyMap;
  990. DIType *DITy = DIGV->getType().resolve(EmptyMap);
  991. DIScope *DITyScope = DITy->getScope().resolve(EmptyMap);
  992. // Create Elt type.
  993. DIType *EltDITy =
  994. Builder.createMemberType(DITyScope, DITy->getName().str() + eltName.str(),
  995. DITy->getFile(), DITy->getLine(), sizeInBits,
  996. alignInBits, offsetInBits, /*Flags*/ 0, DITy);
  997. AddDIGlobalVariable(Builder, DIGV, DIGV->getName().str() + eltName.str(),
  998. EltDITy, EltGV, DbgInfoFinder, /*removeDIGV*/false);
  999. }
  1000. void HLModule::UpdateGlobalVariableDebugInfo(
  1001. llvm::GlobalVariable *GV, llvm::DebugInfoFinder &DbgInfoFinder,
  1002. llvm::GlobalVariable *NewGV) {
  1003. DIGlobalVariable *DIGV = FindGlobalVariableDebugInfo(GV, DbgInfoFinder);
  1004. DXASSERT_NOMSG(DIGV);
  1005. DIBuilder Builder(*GV->getParent());
  1006. DITypeIdentifierMap EmptyMap;
  1007. DIType *DITy = DIGV->getType().resolve(EmptyMap);
  1008. AddDIGlobalVariable(Builder, DIGV, DIGV->getName(), DITy, NewGV,
  1009. DbgInfoFinder,/*removeDIGV*/true);
  1010. }
  1011. DebugInfoFinder &HLModule::GetOrCreateDebugInfoFinder() {
  1012. if (m_pDebugInfoFinder == nullptr) {
  1013. m_pDebugInfoFinder = std::make_unique<llvm::DebugInfoFinder>();
  1014. m_pDebugInfoFinder->processModule(*m_pModule);
  1015. }
  1016. return *m_pDebugInfoFinder;
  1017. }
  1018. //------------------------------------------------------------------------------
  1019. //
  1020. // Signature methods.
  1021. //
  1022. HLExtraPropertyHelper::HLExtraPropertyHelper(llvm::Module *pModule)
  1023. : DxilExtraPropertyHelper(pModule) {
  1024. }
  1025. void HLExtraPropertyHelper::EmitSignatureElementProperties(const DxilSignatureElement &SE,
  1026. vector<Metadata *> &MDVals) {
  1027. }
  1028. void HLExtraPropertyHelper::LoadSignatureElementProperties(const MDOperand &MDO,
  1029. DxilSignatureElement &SE) {
  1030. if (MDO.get() == nullptr)
  1031. return;
  1032. }
  1033. } // namespace hlsl
  1034. namespace llvm {
  1035. hlsl::HLModule &Module::GetOrCreateHLModule(bool skipInit) {
  1036. std::unique_ptr<hlsl::HLModule> M;
  1037. if (!HasHLModule()) {
  1038. M = std::make_unique<hlsl::HLModule>(this);
  1039. if (!skipInit) {
  1040. M->LoadHLMetadata();
  1041. }
  1042. SetHLModule(M.release());
  1043. }
  1044. return GetHLModule();
  1045. }
  1046. void Module::ResetHLModule() {
  1047. if (HasHLModule()) {
  1048. delete TheHLModule;
  1049. TheHLModule = nullptr;
  1050. }
  1051. }
  1052. }