HLModule.cpp 43 KB

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