HLModule.cpp 46 KB

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