HLModule.cpp 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298
  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. if (name == "RayTracingAccelerationStructure")
  663. return true;
  664. name = name.ltrim("RasterizerOrdered");
  665. name = name.ltrim("RW");
  666. if (name == "ByteAddressBuffer")
  667. return true;
  668. if (name.startswith("Buffer"))
  669. return true;
  670. if (name.startswith("StructuredBuffer"))
  671. return true;
  672. if (name.startswith("Texture1D"))
  673. return true;
  674. if (name.startswith("Texture1DArray"))
  675. return true;
  676. if (name.startswith("Texture2D"))
  677. return true;
  678. if (name.startswith("Texture2DArray"))
  679. return true;
  680. if (name.startswith("Texture3D"))
  681. return true;
  682. if (name.startswith("TextureCube"))
  683. return true;
  684. if (name.startswith("TextureCubeArray"))
  685. return true;
  686. if (name.startswith("Texture2DMS"))
  687. return true;
  688. if (name.startswith("Texture2DMSArray"))
  689. return true;
  690. }
  691. return false;
  692. }
  693. void HLModule::GetParameterRowsAndCols(Type *Ty, unsigned &rows, unsigned &cols,
  694. DxilParameterAnnotation &paramAnnotation) {
  695. if (Ty->isPointerTy())
  696. Ty = Ty->getPointerElementType();
  697. // For array input of HS, DS, GS,
  698. // we need to skip the first level which size is based on primitive type.
  699. DxilParamInputQual inputQual = paramAnnotation.GetParamInputQual();
  700. bool skipOneLevelArray = inputQual == DxilParamInputQual::InputPatch;
  701. skipOneLevelArray |= inputQual == DxilParamInputQual::OutputPatch;
  702. skipOneLevelArray |= inputQual == DxilParamInputQual::InputPrimitive;
  703. if (skipOneLevelArray) {
  704. if (Ty->isArrayTy())
  705. Ty = Ty->getArrayElementType();
  706. }
  707. unsigned arraySize = 1;
  708. while (Ty->isArrayTy()) {
  709. arraySize *= Ty->getArrayNumElements();
  710. Ty = Ty->getArrayElementType();
  711. }
  712. rows = 1;
  713. cols = 1;
  714. if (paramAnnotation.HasMatrixAnnotation()) {
  715. const DxilMatrixAnnotation &matrix = paramAnnotation.GetMatrixAnnotation();
  716. if (matrix.Orientation == MatrixOrientation::RowMajor) {
  717. rows = matrix.Rows;
  718. cols = matrix.Cols;
  719. } else {
  720. DXASSERT(matrix.Orientation == MatrixOrientation::ColumnMajor, "");
  721. cols = matrix.Rows;
  722. rows = matrix.Cols;
  723. }
  724. } else if (Ty->isVectorTy())
  725. cols = Ty->getVectorNumElements();
  726. rows *= arraySize;
  727. }
  728. static Value *MergeGEP(GEPOperator *SrcGEP, GetElementPtrInst *GEP) {
  729. IRBuilder<> Builder(GEP);
  730. SmallVector<Value *, 8> Indices;
  731. // Find out whether the last index in the source GEP is a sequential idx.
  732. bool EndsWithSequential = false;
  733. for (gep_type_iterator I = gep_type_begin(*SrcGEP), E = gep_type_end(*SrcGEP);
  734. I != E; ++I)
  735. EndsWithSequential = !(*I)->isStructTy();
  736. if (EndsWithSequential) {
  737. Value *Sum;
  738. Value *SO1 = SrcGEP->getOperand(SrcGEP->getNumOperands() - 1);
  739. Value *GO1 = GEP->getOperand(1);
  740. if (SO1 == Constant::getNullValue(SO1->getType())) {
  741. Sum = GO1;
  742. } else if (GO1 == Constant::getNullValue(GO1->getType())) {
  743. Sum = SO1;
  744. } else {
  745. // If they aren't the same type, then the input hasn't been processed
  746. // by the loop above yet (which canonicalizes sequential index types to
  747. // intptr_t). Just avoid transforming this until the input has been
  748. // normalized.
  749. if (SO1->getType() != GO1->getType())
  750. return nullptr;
  751. // Only do the combine when GO1 and SO1 are both constants. Only in
  752. // this case, we are sure the cost after the merge is never more than
  753. // that before the merge.
  754. if (!isa<Constant>(GO1) || !isa<Constant>(SO1))
  755. return nullptr;
  756. Sum = Builder.CreateAdd(SO1, GO1);
  757. }
  758. // Update the GEP in place if possible.
  759. if (SrcGEP->getNumOperands() == 2) {
  760. GEP->setOperand(0, SrcGEP->getOperand(0));
  761. GEP->setOperand(1, Sum);
  762. return GEP;
  763. }
  764. Indices.append(SrcGEP->op_begin() + 1, SrcGEP->op_end() - 1);
  765. Indices.push_back(Sum);
  766. Indices.append(GEP->op_begin() + 2, GEP->op_end());
  767. } else if (isa<Constant>(*GEP->idx_begin()) &&
  768. cast<Constant>(*GEP->idx_begin())->isNullValue() &&
  769. SrcGEP->getNumOperands() != 1) {
  770. // Otherwise we can do the fold if the first index of the GEP is a zero
  771. Indices.append(SrcGEP->op_begin() + 1, SrcGEP->op_end());
  772. Indices.append(GEP->idx_begin() + 1, GEP->idx_end());
  773. }
  774. if (!Indices.empty())
  775. return Builder.CreateInBoundsGEP(SrcGEP->getSourceElementType(),
  776. SrcGEP->getOperand(0), Indices,
  777. GEP->getName());
  778. else
  779. llvm_unreachable("must merge");
  780. }
  781. void HLModule::MergeGepUse(Value *V) {
  782. for (auto U = V->user_begin(); U != V->user_end();) {
  783. auto Use = U++;
  784. if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(*Use)) {
  785. if (GEPOperator *prevGEP = dyn_cast<GEPOperator>(V)) {
  786. // merge the 2 GEPs
  787. Value *newGEP = MergeGEP(prevGEP, GEP);
  788. // Don't need to replace when GEP is updated in place
  789. if (newGEP != GEP) {
  790. GEP->replaceAllUsesWith(newGEP);
  791. GEP->eraseFromParent();
  792. }
  793. MergeGepUse(newGEP);
  794. } else {
  795. MergeGepUse(*Use);
  796. }
  797. } else if (GEPOperator *GEPOp = dyn_cast<GEPOperator>(*Use)) {
  798. if (GEPOperator *prevGEP = dyn_cast<GEPOperator>(V)) {
  799. // merge the 2 GEPs
  800. Value *newGEP = MergeGEP(prevGEP, GEP);
  801. // Don't need to replace when GEP is updated in place
  802. if (newGEP != GEP) {
  803. GEP->replaceAllUsesWith(newGEP);
  804. GEP->eraseFromParent();
  805. }
  806. MergeGepUse(newGEP);
  807. } else {
  808. MergeGepUse(*Use);
  809. }
  810. }
  811. }
  812. if (V->user_empty()) {
  813. // Only remove GEP here, root ptr will be removed by DCE.
  814. if (GetElementPtrInst *I = dyn_cast<GetElementPtrInst>(V))
  815. I->eraseFromParent();
  816. }
  817. }
  818. template<typename BuilderTy>
  819. CallInst *HLModule::EmitHLOperationCall(BuilderTy &Builder,
  820. HLOpcodeGroup group, unsigned opcode,
  821. Type *RetType,
  822. ArrayRef<Value *> paramList,
  823. llvm::Module &M) {
  824. SmallVector<llvm::Type *, 4> paramTyList;
  825. // Add the opcode param
  826. llvm::Type *opcodeTy = llvm::Type::getInt32Ty(M.getContext());
  827. paramTyList.emplace_back(opcodeTy);
  828. for (Value *param : paramList) {
  829. paramTyList.emplace_back(param->getType());
  830. }
  831. llvm::FunctionType *funcTy =
  832. llvm::FunctionType::get(RetType, paramTyList, false);
  833. Function *opFunc = GetOrCreateHLFunction(M, funcTy, group, opcode);
  834. SmallVector<Value *, 4> opcodeParamList;
  835. Value *opcodeConst = Constant::getIntegerValue(opcodeTy, APInt(32, opcode));
  836. opcodeParamList.emplace_back(opcodeConst);
  837. opcodeParamList.append(paramList.begin(), paramList.end());
  838. return Builder.CreateCall(opFunc, opcodeParamList);
  839. }
  840. template
  841. CallInst *HLModule::EmitHLOperationCall(IRBuilder<> &Builder,
  842. HLOpcodeGroup group, unsigned opcode,
  843. Type *RetType,
  844. ArrayRef<Value *> paramList,
  845. llvm::Module &M);
  846. unsigned HLModule::FindCastOp(bool fromUnsigned, bool toUnsigned,
  847. llvm::Type *SrcTy, llvm::Type *DstTy) {
  848. Instruction::CastOps castOp = llvm::Instruction::CastOps::BitCast;
  849. if (SrcTy->isAggregateType() || DstTy->isAggregateType())
  850. return llvm::Instruction::CastOps::BitCast;
  851. uint32_t SrcBitSize = SrcTy->getScalarSizeInBits();
  852. uint32_t DstBitSize = DstTy->getScalarSizeInBits();
  853. if (SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy()) {
  854. if (SrcBitSize > DstBitSize)
  855. return Instruction::Trunc;
  856. if (toUnsigned)
  857. return Instruction::ZExt;
  858. else
  859. return Instruction::SExt;
  860. }
  861. if (SrcTy->isFPOrFPVectorTy() && DstTy->isFPOrFPVectorTy()) {
  862. if (SrcBitSize > DstBitSize)
  863. return Instruction::FPTrunc;
  864. else
  865. return Instruction::FPExt;
  866. }
  867. if (SrcTy->isIntOrIntVectorTy() && DstTy->isFPOrFPVectorTy())
  868. if (fromUnsigned)
  869. return Instruction::UIToFP;
  870. else
  871. return Instruction::SIToFP;
  872. if (SrcTy->isFPOrFPVectorTy() && DstTy->isIntOrIntVectorTy())
  873. if (toUnsigned)
  874. return Instruction::FPToUI;
  875. else
  876. return Instruction::FPToSI;
  877. DXASSERT_NOMSG(0);
  878. return castOp;
  879. }
  880. bool HLModule::HasPreciseAttributeWithMetadata(Instruction *I) {
  881. return DxilMDHelper::IsMarkedPrecise(I);
  882. }
  883. void HLModule::MarkPreciseAttributeWithMetadata(Instruction *I) {
  884. return DxilMDHelper::MarkPrecise(I);
  885. }
  886. void HLModule::ClearPreciseAttributeWithMetadata(Instruction *I) {
  887. I->setMetadata(DxilMDHelper::kDxilPreciseAttributeMDName, nullptr);
  888. }
  889. static void MarkPreciseAttribute(Function *F) {
  890. LLVMContext &Ctx = F->getContext();
  891. MDNode *preciseNode = MDNode::get(
  892. Ctx, {MDString::get(Ctx, DxilMDHelper::kDxilPreciseAttributeMDName)});
  893. F->setMetadata(DxilMDHelper::kDxilPreciseAttributeMDName, preciseNode);
  894. }
  895. static void MarkPreciseAttributeOnValWithFunctionCall(
  896. llvm::Value *V, llvm::IRBuilder<> &Builder, llvm::Module &M) {
  897. Type *Ty = V->getType();
  898. Type *EltTy = Ty->getScalarType();
  899. // TODO: Only do this on basic types.
  900. FunctionType *preciseFuncTy =
  901. FunctionType::get(Type::getVoidTy(M.getContext()), {EltTy}, false);
  902. // The function will be deleted after precise propagate.
  903. std::string preciseFuncName = "dx.attribute.precise.";
  904. raw_string_ostream mangledNameStr(preciseFuncName);
  905. EltTy->print(mangledNameStr);
  906. mangledNameStr.flush();
  907. Function *preciseFunc =
  908. cast<Function>(M.getOrInsertFunction(preciseFuncName, preciseFuncTy));
  909. if (!HLModule::HasPreciseAttribute(preciseFunc))
  910. MarkPreciseAttribute(preciseFunc);
  911. if (Ty->isVectorTy()) {
  912. for (unsigned i = 0; i < Ty->getVectorNumElements(); i++) {
  913. Value *Elt = Builder.CreateExtractElement(V, i);
  914. Builder.CreateCall(preciseFunc, {Elt});
  915. }
  916. } else
  917. Builder.CreateCall(preciseFunc, {V});
  918. }
  919. void HLModule::MarkPreciseAttributeOnPtrWithFunctionCall(llvm::Value *Ptr,
  920. llvm::Module &M) {
  921. for (User *U : Ptr->users()) {
  922. // Skip load inst.
  923. if (LoadInst *LI = dyn_cast<LoadInst>(U))
  924. continue;
  925. if (StoreInst *SI = dyn_cast<StoreInst>(U)) {
  926. Value *V = SI->getValueOperand();
  927. if (isa<Instruction>(V)) {
  928. // Mark the Value with function call.
  929. IRBuilder<> Builder(SI);
  930. MarkPreciseAttributeOnValWithFunctionCall(V, Builder, M);
  931. }
  932. } else if (CallInst *CI = dyn_cast<CallInst>(U)) {
  933. if (CI->getType()->isVoidTy()) {
  934. IRBuilder<> Builder(CI);
  935. // For void type, cannot use as function arg.
  936. // Mark all arg for it?
  937. for (auto &arg : CI->arg_operands()) {
  938. MarkPreciseAttributeOnValWithFunctionCall(arg, Builder, M);
  939. }
  940. } else {
  941. IRBuilder<> Builder(CI->getNextNode());
  942. MarkPreciseAttributeOnValWithFunctionCall(CI, Builder, M);
  943. }
  944. } else {
  945. // Must be GEP here.
  946. GetElementPtrInst *GEP = cast<GetElementPtrInst>(U);
  947. MarkPreciseAttributeOnPtrWithFunctionCall(GEP, M);
  948. }
  949. }
  950. }
  951. bool HLModule::HasPreciseAttribute(Function *F) {
  952. MDNode *preciseNode =
  953. F->getMetadata(DxilMDHelper::kDxilPreciseAttributeMDName);
  954. return preciseNode != nullptr;
  955. }
  956. void HLModule::MarkDxilResourceAttrib(llvm::Function *F, MDNode *MD) {
  957. F->setMetadata(DxilMDHelper::kHLDxilResourceAttributeMDName, MD);
  958. }
  959. MDNode *HLModule::GetDxilResourceAttrib(llvm::Function *F) {
  960. return F->getMetadata(DxilMDHelper::kHLDxilResourceAttributeMDName);
  961. }
  962. void HLModule::MarkDxilResourceAttrib(llvm::Argument *Arg, llvm::MDNode *MD) {
  963. unsigned i = Arg->getArgNo();
  964. Function *F = Arg->getParent();
  965. DxilFunctionAnnotation *FuncAnnot = m_pTypeSystem->GetFunctionAnnotation(F);
  966. if (!FuncAnnot) {
  967. DXASSERT(0, "Invalid function");
  968. return;
  969. }
  970. DxilParameterAnnotation &ParamAnnot = FuncAnnot->GetParameterAnnotation(i);
  971. ParamAnnot.SetResourceAttribute(MD);
  972. }
  973. MDNode *HLModule::GetDxilResourceAttrib(llvm::Argument *Arg) {
  974. unsigned i = Arg->getArgNo();
  975. Function *F = Arg->getParent();
  976. DxilFunctionAnnotation *FuncAnnot = m_pTypeSystem->GetFunctionAnnotation(F);
  977. if (!FuncAnnot)
  978. return nullptr;
  979. DxilParameterAnnotation &ParamAnnot = FuncAnnot->GetParameterAnnotation(i);
  980. return ParamAnnot.GetResourceAttribute();
  981. }
  982. MDNode *HLModule::GetDxilResourceAttrib(Type *Ty, Module &M) {
  983. for (Function &F : M.functions()) {
  984. if (hlsl::GetHLOpcodeGroupByName(&F) == HLOpcodeGroup::HLCreateHandle) {
  985. Type *ResTy = F.getFunctionType()->getParamType(
  986. HLOperandIndex::kCreateHandleResourceOpIdx);
  987. if (ResTy == Ty)
  988. return GetDxilResourceAttrib(&F);
  989. }
  990. }
  991. return nullptr;
  992. }
  993. DIGlobalVariable *
  994. HLModule::FindGlobalVariableDebugInfo(GlobalVariable *GV,
  995. DebugInfoFinder &DbgInfoFinder) {
  996. struct GlobalFinder {
  997. GlobalVariable *GV;
  998. bool operator()(llvm::DIGlobalVariable *const arg) const {
  999. return arg->getVariable() == GV;
  1000. }
  1001. };
  1002. GlobalFinder F = {GV};
  1003. DebugInfoFinder::global_variable_iterator Found =
  1004. std::find_if(DbgInfoFinder.global_variables().begin(),
  1005. DbgInfoFinder.global_variables().end(), F);
  1006. if (Found != DbgInfoFinder.global_variables().end()) {
  1007. return *Found;
  1008. }
  1009. return nullptr;
  1010. }
  1011. static void AddDIGlobalVariable(DIBuilder &Builder, DIGlobalVariable *LocDIGV,
  1012. StringRef Name, DIType *DITy,
  1013. GlobalVariable *GV, DebugInfoFinder &DbgInfoFinder, bool removeLocDIGV) {
  1014. DIGlobalVariable *EltDIGV = Builder.createGlobalVariable(
  1015. LocDIGV->getScope(), Name, GV->getName(), LocDIGV->getFile(),
  1016. LocDIGV->getLine(), DITy, false, GV);
  1017. DICompileUnit *DICU = dyn_cast<DICompileUnit>(LocDIGV->getScope());
  1018. if (!DICU) {
  1019. DISubprogram *DIS = dyn_cast<DISubprogram>(LocDIGV->getScope());
  1020. if (DIS) {
  1021. // Find the DICU which has this Subprogram.
  1022. NamedMDNode *CompileUnits = GV->getParent()->getNamedMetadata("llvm.dbg.cu");
  1023. DXASSERT_NOMSG(CompileUnits);
  1024. for (unsigned I = 0, E = CompileUnits->getNumOperands(); I != E; ++I) {
  1025. auto *CU = cast<DICompileUnit>(CompileUnits->getOperand(I));
  1026. DXASSERT(CU , "Expected valid compile unit");
  1027. for (DISubprogram *SP : CU->getSubprograms()) {
  1028. if (SP == DIS) {
  1029. DICU = CU;
  1030. break;
  1031. }
  1032. }
  1033. }
  1034. }
  1035. }
  1036. DXASSERT_NOMSG(DICU);
  1037. // Add global to CU.
  1038. auto *GlobalVariables = DICU->getRawGlobalVariables();
  1039. DXASSERT_NOMSG(GlobalVariables);
  1040. MDTuple *GTuple = cast<MDTuple>(GlobalVariables);
  1041. std::vector<Metadata *> AllGVs(GTuple->operands().begin(),
  1042. GTuple->operands().end());
  1043. if (removeLocDIGV) {
  1044. auto locIt = std::find(AllGVs.begin(), AllGVs.end(), LocDIGV);
  1045. AllGVs.erase(locIt);
  1046. }
  1047. AllGVs.emplace_back(EltDIGV);
  1048. DICU->replaceGlobalVariables(MDTuple::get(GV->getContext(), AllGVs));
  1049. DXVERIFY_NOMSG(DbgInfoFinder.appendGlobalVariable(EltDIGV));
  1050. }
  1051. void HLModule::CreateElementGlobalVariableDebugInfo(
  1052. GlobalVariable *GV, DebugInfoFinder &DbgInfoFinder, GlobalVariable *EltGV,
  1053. unsigned sizeInBits, unsigned alignInBits, unsigned offsetInBits,
  1054. StringRef eltName) {
  1055. DIGlobalVariable *DIGV = FindGlobalVariableDebugInfo(GV, DbgInfoFinder);
  1056. DXASSERT_NOMSG(DIGV);
  1057. DIBuilder Builder(*GV->getParent());
  1058. DITypeIdentifierMap EmptyMap;
  1059. DIType *DITy = DIGV->getType().resolve(EmptyMap);
  1060. DIScope *DITyScope = DITy->getScope().resolve(EmptyMap);
  1061. // Create Elt type.
  1062. DIType *EltDITy =
  1063. Builder.createMemberType(DITyScope, DITy->getName().str() + eltName.str(),
  1064. DITy->getFile(), DITy->getLine(), sizeInBits,
  1065. alignInBits, offsetInBits, /*Flags*/ 0, DITy);
  1066. AddDIGlobalVariable(Builder, DIGV, DIGV->getName().str() + eltName.str(),
  1067. EltDITy, EltGV, DbgInfoFinder, /*removeDIGV*/false);
  1068. }
  1069. void HLModule::UpdateGlobalVariableDebugInfo(
  1070. llvm::GlobalVariable *GV, llvm::DebugInfoFinder &DbgInfoFinder,
  1071. llvm::GlobalVariable *NewGV) {
  1072. DIGlobalVariable *DIGV = FindGlobalVariableDebugInfo(GV, DbgInfoFinder);
  1073. DXASSERT_NOMSG(DIGV);
  1074. DIBuilder Builder(*GV->getParent());
  1075. DITypeIdentifierMap EmptyMap;
  1076. DIType *DITy = DIGV->getType().resolve(EmptyMap);
  1077. AddDIGlobalVariable(Builder, DIGV, DIGV->getName(), DITy, NewGV,
  1078. DbgInfoFinder,/*removeDIGV*/true);
  1079. }
  1080. DebugInfoFinder &HLModule::GetOrCreateDebugInfoFinder() {
  1081. if (m_pDebugInfoFinder == nullptr) {
  1082. m_pDebugInfoFinder = llvm::make_unique<llvm::DebugInfoFinder>();
  1083. m_pDebugInfoFinder->processModule(*m_pModule);
  1084. }
  1085. return *m_pDebugInfoFinder;
  1086. }
  1087. //------------------------------------------------------------------------------
  1088. //
  1089. // Signature methods.
  1090. //
  1091. HLExtraPropertyHelper::HLExtraPropertyHelper(llvm::Module *pModule)
  1092. : DxilExtraPropertyHelper(pModule) {
  1093. }
  1094. void HLExtraPropertyHelper::EmitSignatureElementProperties(const DxilSignatureElement &SE,
  1095. vector<Metadata *> &MDVals) {
  1096. }
  1097. void HLExtraPropertyHelper::LoadSignatureElementProperties(const MDOperand &MDO,
  1098. DxilSignatureElement &SE) {
  1099. if (MDO.get() == nullptr)
  1100. return;
  1101. }
  1102. } // namespace hlsl
  1103. namespace llvm {
  1104. hlsl::HLModule &Module::GetOrCreateHLModule(bool skipInit) {
  1105. std::unique_ptr<hlsl::HLModule> M;
  1106. if (!HasHLModule()) {
  1107. M = llvm::make_unique<hlsl::HLModule>(this);
  1108. if (!skipInit) {
  1109. M->LoadHLMetadata();
  1110. }
  1111. SetHLModule(M.release());
  1112. }
  1113. return GetHLModule();
  1114. }
  1115. void Module::ResetHLModule() {
  1116. if (HasHLModule()) {
  1117. delete TheHLModule;
  1118. TheHLModule = nullptr;
  1119. }
  1120. }
  1121. }