HLModule.cpp 42 KB

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