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