HLModule.cpp 42 KB

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