HLModule.cpp 42 KB

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