HLModule.cpp 44 KB

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