DxilGenerationPass.cpp 76 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // //
  3. // DxilGenerationPass.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. // DxilGenerationPass implementation. //
  9. // //
  10. ///////////////////////////////////////////////////////////////////////////////
  11. #include "dxc/HLSL/DxilGenerationPass.h"
  12. #include "dxc/HLSL/DxilOperations.h"
  13. #include "dxc/HLSL/DxilModule.h"
  14. #include "dxc/HLSL/HLModule.h"
  15. #include "dxc/HLSL/HLOperations.h"
  16. #include "dxc/HLSL/HLMatrixLowerHelper.h"
  17. #include "dxc/HlslIntrinsicOp.h"
  18. #include "dxc/Support/Global.h"
  19. #include "dxc/HLSL/DxilTypeSystem.h"
  20. #include "dxc/HLSL/HLOperationLower.h"
  21. #include "HLSignatureLower.h"
  22. #include "dxc/HLSL/DxilUtil.h"
  23. #include "llvm/IR/GetElementPtrTypeIterator.h"
  24. #include "llvm/IR/IRBuilder.h"
  25. #include "llvm/IR/Instructions.h"
  26. #include "llvm/IR/InstIterator.h"
  27. #include "llvm/IR/IntrinsicInst.h"
  28. #include "llvm/IR/Module.h"
  29. #include "llvm/IR/DebugInfo.h"
  30. #include "llvm/IR/PassManager.h"
  31. #include "llvm/ADT/BitVector.h"
  32. #include "llvm/Pass.h"
  33. #include "llvm/Transforms/Utils/SSAUpdater.h"
  34. #include "llvm/Analysis/AssumptionCache.h"
  35. #include "llvm/Transforms/Utils/PromoteMemToReg.h"
  36. #include <memory>
  37. #include <unordered_set>
  38. #include <iterator>
  39. using namespace llvm;
  40. using namespace hlsl;
  41. // TODO: use hlsl namespace for the most of this file.
  42. namespace {
  43. // Collect unused phi of resources and remove them.
  44. class ResourceRemover : public LoadAndStorePromoter {
  45. AllocaInst *AI;
  46. mutable std::unordered_set<PHINode *> unusedPhis;
  47. public:
  48. ResourceRemover(ArrayRef<Instruction *> Insts, SSAUpdater &S)
  49. : LoadAndStorePromoter(Insts, S), AI(nullptr) {}
  50. void run(AllocaInst *AI, const SmallVectorImpl<Instruction *> &Insts) {
  51. // Remember which alloca we're promoting (for isInstInList).
  52. this->AI = AI;
  53. LoadAndStorePromoter::run(Insts);
  54. for (PHINode *P : unusedPhis) {
  55. P->eraseFromParent();
  56. }
  57. }
  58. bool
  59. isInstInList(Instruction *I,
  60. const SmallVectorImpl<Instruction *> &Insts) const override {
  61. if (LoadInst *LI = dyn_cast<LoadInst>(I))
  62. return LI->getOperand(0) == AI;
  63. return cast<StoreInst>(I)->getPointerOperand() == AI;
  64. }
  65. void replaceLoadWithValue(LoadInst *LI, Value *V) const override {
  66. if (PHINode *PHI = dyn_cast<PHINode>(V)) {
  67. if (PHI->user_empty())
  68. unusedPhis.insert(PHI);
  69. }
  70. LI->replaceAllUsesWith(UndefValue::get(LI->getType()));
  71. }
  72. };
  73. void InitResourceBase(const DxilResourceBase *pSource, DxilResourceBase *pDest) {
  74. DXASSERT_NOMSG(pSource->GetClass() == pDest->GetClass());
  75. pDest->SetKind(pSource->GetKind());
  76. pDest->SetID(pSource->GetID());
  77. pDest->SetSpaceID(pSource->GetSpaceID());
  78. pDest->SetLowerBound(pSource->GetLowerBound());
  79. pDest->SetRangeSize(pSource->GetRangeSize());
  80. pDest->SetGlobalSymbol(pSource->GetGlobalSymbol());
  81. pDest->SetGlobalName(pSource->GetGlobalName());
  82. pDest->SetHandle(pSource->GetHandle());
  83. }
  84. void InitResource(const DxilResource *pSource, DxilResource *pDest) {
  85. pDest->SetCompType(pSource->GetCompType());
  86. pDest->SetSampleCount(pSource->GetSampleCount());
  87. pDest->SetElementStride(pSource->GetElementStride());
  88. pDest->SetGloballyCoherent(pSource->IsGloballyCoherent());
  89. pDest->SetHasCounter(pSource->HasCounter());
  90. pDest->SetRW(pSource->IsRW());
  91. pDest->SetROV(pSource->IsROV());
  92. InitResourceBase(pSource, pDest);
  93. }
  94. void InitDxilModuleFromHLModule(HLModule &H, DxilModule &M, DxilEntrySignature *pSig, bool HasDebugInfo) {
  95. std::unique_ptr<DxilEntrySignature> pSigPtr(pSig);
  96. // Subsystems.
  97. unsigned ValMajor, ValMinor;
  98. H.GetValidatorVersion(ValMajor, ValMinor);
  99. M.SetValidatorVersion(ValMajor, ValMinor);
  100. M.SetShaderModel(H.GetShaderModel());
  101. // Entry function.
  102. Function *EntryFn = H.GetEntryFunction();
  103. DxilFunctionProps *FnProps = H.HasDxilFunctionProps(EntryFn) ? &H.GetDxilFunctionProps(EntryFn) : nullptr;
  104. M.SetEntryFunction(EntryFn);
  105. M.SetEntryFunctionName(H.GetEntryFunctionName());
  106. std::vector<GlobalVariable* > &LLVMUsed = M.GetLLVMUsed();
  107. // Resources
  108. for (auto && C : H.GetCBuffers()) {
  109. auto b = make_unique<DxilCBuffer>();
  110. InitResourceBase(C.get(), b.get());
  111. b->SetSize(C->GetSize());
  112. if (HasDebugInfo)
  113. LLVMUsed.emplace_back(cast<GlobalVariable>(b->GetGlobalSymbol()));
  114. b->SetGlobalSymbol(UndefValue::get(b->GetGlobalSymbol()->getType()));
  115. M.AddCBuffer(std::move(b));
  116. }
  117. for (auto && C : H.GetUAVs()) {
  118. auto b = make_unique<DxilResource>();
  119. InitResource(C.get(), b.get());
  120. if (HasDebugInfo)
  121. LLVMUsed.emplace_back(cast<GlobalVariable>(b->GetGlobalSymbol()));
  122. b->SetGlobalSymbol(UndefValue::get(b->GetGlobalSymbol()->getType()));
  123. M.AddUAV(std::move(b));
  124. }
  125. for (auto && C : H.GetSRVs()) {
  126. auto b = make_unique<DxilResource>();
  127. InitResource(C.get(), b.get());
  128. if (HasDebugInfo)
  129. LLVMUsed.emplace_back(cast<GlobalVariable>(b->GetGlobalSymbol()));
  130. b->SetGlobalSymbol(UndefValue::get(b->GetGlobalSymbol()->getType()));
  131. M.AddSRV(std::move(b));
  132. }
  133. for (auto && C : H.GetSamplers()) {
  134. auto b = make_unique<DxilSampler>();
  135. InitResourceBase(C.get(), b.get());
  136. b->SetSamplerKind(C->GetSamplerKind());
  137. if (HasDebugInfo)
  138. LLVMUsed.emplace_back(cast<GlobalVariable>(b->GetGlobalSymbol()));
  139. b->SetGlobalSymbol(UndefValue::get(b->GetGlobalSymbol()->getType()));
  140. M.AddSampler(std::move(b));
  141. }
  142. // Signatures.
  143. M.ResetEntrySignature(pSigPtr.release());
  144. M.ResetRootSignature(H.ReleaseRootSignature());
  145. // Shader properties.
  146. //bool m_bDisableOptimizations;
  147. M.m_ShaderFlags.SetDisableOptimizations(H.GetHLOptions().bDisableOptimizations);
  148. //bool m_bDisableMathRefactoring;
  149. //bool m_bEnableDoublePrecision;
  150. //bool m_bEnableDoubleExtensions;
  151. //M.CollectShaderFlags();
  152. //bool m_bForceEarlyDepthStencil;
  153. //bool m_bEnableRawAndStructuredBuffers;
  154. //bool m_bEnableMSAD;
  155. //M.m_ShaderFlags.SetAllResourcesBound(H.GetHLOptions().bAllResourcesBound);
  156. M.m_ShaderFlags.SetUseNativeLowPrecision(!H.GetHLOptions().bUseMinPrecision);
  157. if (FnProps)
  158. M.SetShaderProperties(FnProps);
  159. // Move function props.
  160. if (M.GetShaderModel()->IsLib())
  161. M.ResetFunctionPropsMap(H.ReleaseFunctionPropsMap());
  162. // DXIL type system.
  163. M.ResetTypeSystem(H.ReleaseTypeSystem());
  164. // Dxil OP.
  165. M.ResetOP(H.ReleaseOP());
  166. // Keep llvm used.
  167. M.EmitLLVMUsed();
  168. M.m_ShaderFlags.SetAllResourcesBound(H.GetHLOptions().bAllResourcesBound);
  169. // Update Validator Version
  170. M.UpgradeToMinValidatorVersion();
  171. }
  172. class DxilGenerationPass : public ModulePass {
  173. HLModule *m_pHLModule;
  174. bool m_HasDbgInfo;
  175. HLSLExtensionsCodegenHelper *m_extensionsCodegenHelper;
  176. public:
  177. static char ID; // Pass identification, replacement for typeid
  178. explicit DxilGenerationPass(bool NoOpt = false)
  179. : ModulePass(ID), m_pHLModule(nullptr), NotOptimized(NoOpt), m_extensionsCodegenHelper(nullptr) {}
  180. const char *getPassName() const override { return "DXIL Generator"; }
  181. void SetExtensionsHelper(HLSLExtensionsCodegenHelper *helper) {
  182. m_extensionsCodegenHelper = helper;
  183. }
  184. bool runOnModule(Module &M) override {
  185. m_pHLModule = &M.GetOrCreateHLModule();
  186. const ShaderModel *SM = m_pHLModule->GetShaderModel();
  187. // Load up debug information, to cross-reference values and the instructions
  188. // used to load them.
  189. m_HasDbgInfo = getDebugMetadataVersionFromModule(M) != 0;
  190. std::unique_ptr<DxilEntrySignature> pSig =
  191. llvm::make_unique<DxilEntrySignature>(SM->GetKind(), M.GetHLModule().GetHLOptions().bUseMinPrecision);
  192. // EntrySig for shader functions.
  193. std::unordered_map<llvm::Function *, std::unique_ptr<DxilEntrySignature>>
  194. DxilEntrySignatureMap;
  195. if (!SM->IsLib()) {
  196. HLSignatureLower sigLower(m_pHLModule->GetEntryFunction(), *m_pHLModule,
  197. *pSig);
  198. sigLower.Run();
  199. } else {
  200. for (auto It = M.begin(); It != M.end();) {
  201. Function &F = *(It++);
  202. // Lower signature for each entry function.
  203. if (m_pHLModule->HasDxilFunctionProps(&F)) {
  204. DxilFunctionProps &props = m_pHLModule->GetDxilFunctionProps(&F);
  205. std::unique_ptr<DxilEntrySignature> pSig =
  206. llvm::make_unique<DxilEntrySignature>(props.shaderKind, m_pHLModule->GetHLOptions().bUseMinPrecision);
  207. HLSignatureLower sigLower(&F, *m_pHLModule, *pSig);
  208. sigLower.Run();
  209. DxilEntrySignatureMap[&F] = std::move(pSig);
  210. }
  211. }
  212. }
  213. std::unordered_set<LoadInst *> UpdateCounterSet;
  214. std::unordered_set<Value *> NonUniformSet;
  215. GenerateDxilOperations(M, UpdateCounterSet, NonUniformSet);
  216. std::unordered_map<Instruction *, Value *> handleMap;
  217. GenerateDxilCBufferHandles(NonUniformSet);
  218. GenerateParamDxilResourceHandles(handleMap);
  219. GenerateDxilResourceHandles(UpdateCounterSet, NonUniformSet);
  220. AddCreateHandleForPhiNodeAndSelect(m_pHLModule->GetOP());
  221. // For module which not promote mem2reg.
  222. // Remove local resource alloca/load/store/phi.
  223. for (auto It = M.begin(); It != M.end();) {
  224. Function &F = *(It++);
  225. if (!F.isDeclaration()) {
  226. RemoveLocalDxilResourceAllocas(&F);
  227. if (hlsl::GetHLOpcodeGroupByName(&F) == HLOpcodeGroup::HLCreateHandle) {
  228. if (F.user_empty()) {
  229. F.eraseFromParent();
  230. } else {
  231. M.getContext().emitError("Fail to lower createHandle.");
  232. }
  233. }
  234. }
  235. }
  236. // Translate precise on allocas into function call to keep the information after mem2reg.
  237. // The function calls will be removed after propagate precise attribute.
  238. TranslatePreciseAttribute();
  239. // Change struct type to legacy layout for cbuf and struct buf for min precision data types.
  240. if (M.GetHLModule().GetHLOptions().bUseMinPrecision)
  241. UpdateStructTypeForLegacyLayout();
  242. // High-level metadata should now be turned into low-level metadata.
  243. const bool SkipInit = true;
  244. hlsl::DxilModule &DxilMod = M.GetOrCreateDxilModule(SkipInit);
  245. InitDxilModuleFromHLModule(*m_pHLModule, DxilMod, pSig.release(),
  246. m_HasDbgInfo);
  247. if (SM->IsLib())
  248. DxilMod.ResetEntrySignatureMap(std::move(DxilEntrySignatureMap));
  249. HLModule::ClearHLMetadata(M);
  250. M.ResetHLModule();
  251. // We now have a DXIL representation - record this.
  252. SetPauseResumePasses(M, "hlsl-dxilemit", "hlsl-dxilload");
  253. // Remove debug code when not debug info.
  254. if (!m_HasDbgInfo)
  255. DxilMod.StripDebugRelatedCode();
  256. return true;
  257. }
  258. private:
  259. void RemoveLocalDxilResourceAllocas(Function *F);
  260. void
  261. TranslateDxilResourceUses(DxilResourceBase &res,
  262. std::unordered_set<LoadInst *> &UpdateCounterSet,
  263. std::unordered_set<Value *> &NonUniformSet);
  264. void
  265. GenerateDxilResourceHandles(std::unordered_set<LoadInst *> &UpdateCounterSet,
  266. std::unordered_set<Value *> &NonUniformSet);
  267. void AddCreateHandleForPhiNodeAndSelect(OP *hlslOP);
  268. void TranslateParamDxilResourceHandles(Function *F, std::unordered_map<Instruction *, Value *> &handleMap);
  269. void GenerateParamDxilResourceHandles(
  270. std::unordered_map<Instruction *, Value *> &handleMap);
  271. // Generate DXIL cbuffer handles.
  272. void
  273. GenerateDxilCBufferHandles(std::unordered_set<Value *> &NonUniformSet);
  274. // change built-in funtion into DXIL operations
  275. void GenerateDxilOperations(Module &M,
  276. std::unordered_set<LoadInst *> &UpdateCounterSet,
  277. std::unordered_set<Value *> &NonUniformSet);
  278. // Change struct type to legacy layout for cbuf and struct buf.
  279. void UpdateStructTypeForLegacyLayout();
  280. // Translate precise attribute into HL function call.
  281. void TranslatePreciseAttribute();
  282. // Input module is not optimized.
  283. bool NotOptimized;
  284. };
  285. }
  286. static Value *MergeImmResClass(Value *resClass) {
  287. if (ConstantInt *Imm = dyn_cast<ConstantInt>(resClass)) {
  288. return resClass;
  289. } else {
  290. PHINode *phi = cast<PHINode>(resClass);
  291. Value *immResClass = MergeImmResClass(phi->getIncomingValue(0));
  292. unsigned numOperands = phi->getNumOperands();
  293. for (unsigned i=0;i<numOperands;i++)
  294. phi->setIncomingValue(i, immResClass);
  295. return immResClass;
  296. }
  297. }
  298. static const StringRef kResourceMapErrorMsg = "local resource not guaranteed to map to unique global resource.";
  299. static Value *SelectOnOperand(Value *Cond, CallInst *CIT, CallInst *CIF,
  300. unsigned idx, IRBuilder<> &Builder) {
  301. Value *OpT = CIT->getArgOperand(idx);
  302. Value *OpF = CIF->getArgOperand(idx);
  303. Value *OpSel = OpT;
  304. if (OpT != OpF) {
  305. OpSel = Builder.CreateSelect(Cond, OpT, OpF);
  306. }
  307. return OpSel;
  308. }
  309. static void ReplaceResourceUserWithHandle(LoadInst *Res, Value *handle) {
  310. for (auto resUser = Res->user_begin(); resUser != Res->user_end();) {
  311. CallInst *CI = dyn_cast<CallInst>(*(resUser++));
  312. DXASSERT(GetHLOpcodeGroupByName(CI->getCalledFunction()) ==
  313. HLOpcodeGroup::HLCreateHandle,
  314. "must be createHandle");
  315. CI->replaceAllUsesWith(handle);
  316. CI->eraseFromParent();
  317. }
  318. Res->eraseFromParent();
  319. }
  320. static bool IsResourceType(Type *Ty) {
  321. bool isResource = HLModule::IsHLSLObjectType(Ty);
  322. if (ArrayType *AT = dyn_cast<ArrayType>(Ty)) {
  323. Type *EltTy = AT->getElementType();
  324. while (isa<ArrayType>(EltTy)) {
  325. EltTy = EltTy->getArrayElementType();
  326. }
  327. isResource = HLModule::IsHLSLObjectType(EltTy);
  328. // TODO: support local resource array.
  329. DXASSERT(!isResource, "local resource array");
  330. }
  331. return isResource;
  332. }
  333. void DxilGenerationPass::RemoveLocalDxilResourceAllocas(Function *F) {
  334. BasicBlock &BB = F->getEntryBlock(); // Get the entry node for the function
  335. std::unordered_set<AllocaInst *> localResources;
  336. for (BasicBlock::iterator I = BB.begin(), E = --BB.end(); I != E; ++I)
  337. if (AllocaInst *AI = dyn_cast<AllocaInst>(I)) { // Is it an alloca?
  338. if (IsResourceType(AI->getAllocatedType())) {
  339. localResources.insert(AI);
  340. }
  341. }
  342. SSAUpdater SSA;
  343. SmallVector<Instruction *, 4> Insts;
  344. for (AllocaInst *AI : localResources) {
  345. // Build list of instructions to promote.
  346. for (User *U : AI->users())
  347. Insts.emplace_back(cast<Instruction>(U));
  348. ResourceRemover(Insts, SSA).run(AI, Insts);
  349. Insts.clear();
  350. }
  351. }
  352. void DxilGenerationPass::TranslateParamDxilResourceHandles(Function *F, std::unordered_map<Instruction *, Value *> &handleMap) {
  353. Type *handleTy = m_pHLModule->GetOP()->GetHandleType();
  354. IRBuilder<> Builder(F->getEntryBlock().getFirstInsertionPt());
  355. for (Argument &arg : F->args()) {
  356. Type *Ty = arg.getType();
  357. if (isa<PointerType>(Ty))
  358. Ty = Ty->getPointerElementType();
  359. SmallVector<unsigned,4> arraySizeList;
  360. while (isa<ArrayType>(Ty)) {
  361. arraySizeList.push_back(Ty->getArrayNumElements());
  362. Ty = Ty->getArrayElementType();
  363. }
  364. DXIL::ResourceClass RC = m_pHLModule->GetResourceClass(Ty);
  365. if (RC != DXIL::ResourceClass::Invalid) {
  366. Type *curTy = handleTy;
  367. for (auto it = arraySizeList.rbegin(), E = arraySizeList.rend(); it != E;
  368. it++) {
  369. curTy = ArrayType::get(curTy, *it);
  370. }
  371. curTy = PointerType::get(curTy, 0);
  372. CallInst *castToHandle = cast<CallInst>(HLModule::EmitHLOperationCall(
  373. Builder, HLOpcodeGroup::HLCast, 0, curTy,
  374. {UndefValue::get(arg.getType())}, *F->getParent()));
  375. for (User *U : arg.users()) {
  376. Instruction *I = cast<Instruction>(U);
  377. IRBuilder<> userBuilder(I);
  378. if (LoadInst *ldInst = dyn_cast<LoadInst>(U)) {
  379. Value *handleLd = userBuilder.CreateLoad(castToHandle);
  380. handleMap[ldInst] = handleLd;
  381. } else if (StoreInst *stInst = dyn_cast<StoreInst>(U)) {
  382. Value *res = stInst->getValueOperand();
  383. Value *handle = HLModule::EmitHLOperationCall(
  384. userBuilder, HLOpcodeGroup::HLCast, 0, handleTy, {res},
  385. *F->getParent());
  386. userBuilder.CreateStore(handle, castToHandle);
  387. } else if (CallInst *CI = dyn_cast<CallInst>(U)) {
  388. // Don't flatten argument here.
  389. continue;
  390. } else {
  391. DXASSERT(
  392. dyn_cast<GEPOperator>(U) != nullptr,
  393. "else AddOpcodeParamForIntrinsic in CodeGen did not patch uses "
  394. "to only have ld/st refer to temp object");
  395. GEPOperator *GEP = cast<GEPOperator>(U);
  396. std::vector<Value *> idxList(GEP->idx_begin(), GEP->idx_end());
  397. Value *handleGEP = userBuilder.CreateGEP(castToHandle, idxList);
  398. for (auto GEPU : GEP->users()) {
  399. Instruction *GEPI = cast<Instruction>(GEPU);
  400. IRBuilder<> gepUserBuilder(GEPI);
  401. if (LoadInst *ldInst = dyn_cast<LoadInst>(GEPU)) {
  402. handleMap[ldInst] = gepUserBuilder.CreateLoad(handleGEP);
  403. } else {
  404. StoreInst *stInst = cast<StoreInst>(GEPU);
  405. Value *res = stInst->getValueOperand();
  406. Value *handle = HLModule::EmitHLOperationCall(
  407. gepUserBuilder, HLOpcodeGroup::HLCast, 0, handleTy, {res},
  408. *F->getParent());
  409. gepUserBuilder.CreateStore(handle, handleGEP);
  410. }
  411. }
  412. }
  413. }
  414. castToHandle->setArgOperand(0, &arg);
  415. }
  416. }
  417. }
  418. void DxilGenerationPass::GenerateParamDxilResourceHandles(
  419. std::unordered_map<Instruction *, Value *> &handleMap) {
  420. Module &M = *m_pHLModule->GetModule();
  421. for (Function &F : M.functions()) {
  422. if (!F.isDeclaration())
  423. TranslateParamDxilResourceHandles(&F, handleMap);
  424. }
  425. }
  426. void DxilGenerationPass::TranslateDxilResourceUses(
  427. DxilResourceBase &res, std::unordered_set<LoadInst *> &UpdateCounterSet,
  428. std::unordered_set<Value *> &NonUniformSet) {
  429. OP *hlslOP = m_pHLModule->GetOP();
  430. Function *createHandle = hlslOP->GetOpFunc(
  431. OP::OpCode::CreateHandle, llvm::Type::getVoidTy(m_pHLModule->GetCtx()));
  432. Value *opArg = hlslOP->GetU32Const((unsigned)OP::OpCode::CreateHandle);
  433. bool isViewResource = res.GetClass() == DXIL::ResourceClass::SRV || res.GetClass() == DXIL::ResourceClass::UAV;
  434. bool isROV = isViewResource && static_cast<DxilResource &>(res).IsROV();
  435. std::string handleName = (res.GetGlobalName() + Twine("_") + Twine(res.GetResClassName())).str();
  436. if (isViewResource)
  437. handleName += (Twine("_") + Twine(res.GetResDimName())).str();
  438. if (isROV)
  439. handleName += "_ROV";
  440. Value *resClassArg = hlslOP->GetU8Const(
  441. static_cast<std::underlying_type<DxilResourceBase::Class>::type>(
  442. res.GetClass()));
  443. Value *resIDArg = hlslOP->GetU32Const(res.GetID());
  444. // resLowerBound will be added after allocation in DxilCondenseResources.
  445. Value *resLowerBound = hlslOP->GetU32Const(0);
  446. // TODO: Set Non-uniform resource bit based on whether index comes from IOP_NonUniformResourceIndex.
  447. Value *isUniformRes = hlslOP->GetI1Const(0);
  448. Value *GV = res.GetGlobalSymbol();
  449. Module *pM = m_pHLModule->GetModule();
  450. // TODO: add debug info to create handle.
  451. DIVariable *DIV = nullptr;
  452. DILocation *DL = nullptr;
  453. if (m_HasDbgInfo) {
  454. DebugInfoFinder &Finder = m_pHLModule->GetOrCreateDebugInfoFinder();
  455. DIV =
  456. HLModule::FindGlobalVariableDebugInfo(cast<GlobalVariable>(GV), Finder);
  457. if (DIV)
  458. // TODO: how to get col?
  459. DL =
  460. DILocation::get(pM->getContext(), DIV->getLine(), 1, DIV->getScope());
  461. }
  462. bool isResArray = res.GetRangeSize() > 1;
  463. std::unordered_map<Function *, Instruction *> handleMapOnFunction;
  464. Value *createHandleArgs[] = {opArg, resClassArg, resIDArg, resLowerBound,
  465. isUniformRes};
  466. for (iplist<Function>::iterator F : pM->getFunctionList()) {
  467. if (!F->isDeclaration()) {
  468. if (!isResArray) {
  469. IRBuilder<> Builder(F->getEntryBlock().getFirstInsertionPt());
  470. if (m_HasDbgInfo) {
  471. // TODO: set debug info.
  472. //Builder.SetCurrentDebugLocation(DL);
  473. }
  474. handleMapOnFunction[F] = Builder.CreateCall(createHandle, createHandleArgs, handleName);
  475. }
  476. }
  477. }
  478. for (auto U = GV->user_begin(), E = GV->user_end(); U != E; ) {
  479. User *user = *(U++);
  480. // Skip unused user.
  481. if (user->user_empty())
  482. continue;
  483. if (LoadInst *ldInst = dyn_cast<LoadInst>(user)) {
  484. if (UpdateCounterSet.count(ldInst)) {
  485. DxilResource *resource = llvm::dyn_cast<DxilResource>(&res);
  486. DXASSERT_NOMSG(resource);
  487. DXASSERT_NOMSG(resource->GetClass() == DXIL::ResourceClass::UAV);
  488. resource->SetHasCounter(true);
  489. }
  490. Function *userF = ldInst->getParent()->getParent();
  491. DXASSERT(handleMapOnFunction.count(userF), "must exist");
  492. Value *handle = handleMapOnFunction[userF];
  493. ReplaceResourceUserWithHandle(ldInst, handle);
  494. } else {
  495. DXASSERT(dyn_cast<GEPOperator>(user) != nullptr,
  496. "else AddOpcodeParamForIntrinsic in CodeGen did not patch uses "
  497. "to only have ld/st refer to temp object");
  498. GEPOperator *GEP = cast<GEPOperator>(user);
  499. Value *idx = nullptr;
  500. if (GEP->getNumIndices() == 2) {
  501. // one dim array of resource
  502. idx = (GEP->idx_begin() + 1)->get();
  503. } else {
  504. gep_type_iterator GEPIt = gep_type_begin(GEP), E = gep_type_end(GEP);
  505. // Must be instruction for multi dim array.
  506. std::unique_ptr<IRBuilder<> > Builder;
  507. if (GetElementPtrInst *GEPInst = dyn_cast<GetElementPtrInst>(GEP)) {
  508. Builder = std::make_unique<IRBuilder<> >(GEPInst);
  509. } else {
  510. Builder = std::make_unique<IRBuilder<> >(GV->getContext());
  511. }
  512. for (; GEPIt != E; ++GEPIt) {
  513. if (GEPIt->isArrayTy()) {
  514. unsigned arraySize = GEPIt->getArrayNumElements();
  515. Value * tmpIdx = GEPIt.getOperand();
  516. if (idx == nullptr)
  517. idx = tmpIdx;
  518. else {
  519. idx = Builder->CreateMul(idx, Builder->getInt32(arraySize));
  520. idx = Builder->CreateAdd(idx, tmpIdx);
  521. }
  522. }
  523. }
  524. }
  525. createHandleArgs[DXIL::OperandIndex::kCreateHandleResIndexOpIdx] = idx;
  526. if (!NonUniformSet.count(idx))
  527. createHandleArgs[DXIL::OperandIndex::kCreateHandleIsUniformOpIdx] =
  528. isUniformRes;
  529. else
  530. createHandleArgs[DXIL::OperandIndex::kCreateHandleIsUniformOpIdx] =
  531. hlslOP->GetI1Const(1);
  532. Value *handle = nullptr;
  533. if (GetElementPtrInst *GEPInst = dyn_cast<GetElementPtrInst>(GEP)) {
  534. IRBuilder<> Builder = IRBuilder<>(GEPInst);
  535. handle = Builder.CreateCall(createHandle, createHandleArgs, handleName);
  536. }
  537. for (auto GEPU = GEP->user_begin(), GEPE = GEP->user_end(); GEPU != GEPE; ) {
  538. // Must be load inst.
  539. LoadInst *ldInst = cast<LoadInst>(*(GEPU++));
  540. if (UpdateCounterSet.count(ldInst)) {
  541. DxilResource *resource = dyn_cast<DxilResource>(&res);
  542. DXASSERT_NOMSG(resource);
  543. DXASSERT_NOMSG(resource->GetClass() == DXIL::ResourceClass::UAV);
  544. resource->SetHasCounter(true);
  545. }
  546. if (handle) {
  547. ReplaceResourceUserWithHandle(ldInst, handle);
  548. }
  549. else {
  550. IRBuilder<> Builder = IRBuilder<>(ldInst);
  551. Value *localHandle = Builder.CreateCall(createHandle, createHandleArgs, handleName);
  552. ReplaceResourceUserWithHandle(ldInst, localHandle);
  553. }
  554. }
  555. }
  556. }
  557. // Erase unused handle.
  558. for (auto It : handleMapOnFunction) {
  559. Instruction *I = It.second;
  560. if (I->user_empty())
  561. I->eraseFromParent();
  562. }
  563. }
  564. void DxilGenerationPass::GenerateDxilResourceHandles(
  565. std::unordered_set<LoadInst *> &UpdateCounterSet,
  566. std::unordered_set<Value *> &NonUniformSet) {
  567. // Create sampler handle first, may be used by SRV operations.
  568. for (size_t i = 0; i < m_pHLModule->GetSamplers().size(); i++) {
  569. DxilSampler &S = m_pHLModule->GetSampler(i);
  570. TranslateDxilResourceUses(S, UpdateCounterSet, NonUniformSet);
  571. }
  572. for (size_t i = 0; i < m_pHLModule->GetSRVs().size(); i++) {
  573. HLResource &SRV = m_pHLModule->GetSRV(i);
  574. TranslateDxilResourceUses(SRV, UpdateCounterSet, NonUniformSet);
  575. }
  576. for (size_t i = 0; i < m_pHLModule->GetUAVs().size(); i++) {
  577. HLResource &UAV = m_pHLModule->GetUAV(i);
  578. TranslateDxilResourceUses(UAV, UpdateCounterSet, NonUniformSet);
  579. }
  580. }
  581. static void
  582. AddResourceToSet(Instruction *Res, std::unordered_set<Instruction *> &resSet) {
  583. unsigned startOpIdx = 0;
  584. // Skip Cond for Select.
  585. if (isa<SelectInst>(Res))
  586. startOpIdx = 1;
  587. else if (!isa<PHINode>(Res))
  588. // Only check phi and select here.
  589. return;
  590. // Already add.
  591. if (resSet.count(Res))
  592. return;
  593. resSet.insert(Res);
  594. // Scan operand to add resource node which only used by phi/select.
  595. unsigned numOperands = Res->getNumOperands();
  596. for (unsigned i = startOpIdx; i < numOperands; i++) {
  597. Value *V = Res->getOperand(i);
  598. if (Instruction *I = dyn_cast<Instruction>(V)) {
  599. AddResourceToSet(I, resSet);
  600. }
  601. }
  602. }
  603. // Transform
  604. //
  605. // %g_texture_texture_2d1 = call %dx.types.Handle @dx.op.createHandle(i32 57, i8 0, i32 0, i32 0, i1 false)
  606. // %g_texture_texture_2d = call %dx.types.Handle @dx.op.createHandle(i32 57, i8 0, i32 0, i32 2, i1 false)
  607. // %13 = select i1 %cmp, %dx.types.Handle %g_texture_texture_2d1, %dx.types.Handle %g_texture_texture_2d
  608. // Into
  609. // %11 = select i1 %cmp, i32 0, i32 2
  610. // %12 = call %dx.types.Handle @dx.op.createHandle(i32 57, i8 0, i32 0, i32 %11, i1 false)
  611. //
  612. static bool MergeHandleOpWithSameValue(Instruction *HandleOp,
  613. unsigned startOpIdx,
  614. unsigned numOperands) {
  615. Value *op0 = nullptr;
  616. for (unsigned i = startOpIdx; i < numOperands; i++) {
  617. Value *op = HandleOp->getOperand(i);
  618. if (i == startOpIdx) {
  619. op0 = op;
  620. } else {
  621. if (op0 != op)
  622. op0 = nullptr;
  623. }
  624. }
  625. if (op0) {
  626. HandleOp->replaceAllUsesWith(op0);
  627. return true;
  628. }
  629. return false;
  630. }
  631. static void
  632. UpdateHandleOperands(Instruction *Res,
  633. std::unordered_map<Instruction *, CallInst *> &handleMap,
  634. std::unordered_set<Instruction *> &nonUniformOps) {
  635. unsigned numOperands = Res->getNumOperands();
  636. unsigned startOpIdx = 0;
  637. // Skip Cond for Select.
  638. if (SelectInst *Sel = dyn_cast<SelectInst>(Res))
  639. startOpIdx = 1;
  640. CallInst *Handle = handleMap[Res];
  641. Instruction *resClass = cast<Instruction>(
  642. Handle->getArgOperand(DXIL::OperandIndex::kCreateHandleResClassOpIdx));
  643. Instruction *resID = cast<Instruction>(
  644. Handle->getArgOperand(DXIL::OperandIndex::kCreateHandleResIDOpIdx));
  645. Instruction *resAddr = cast<Instruction>(
  646. Handle->getArgOperand(DXIL::OperandIndex::kCreateHandleResIndexOpIdx));
  647. for (unsigned i = startOpIdx; i < numOperands; i++) {
  648. if (!isa<Instruction>(Res->getOperand(i))) {
  649. Res->getContext().emitError(Res, kResourceMapErrorMsg);
  650. continue;
  651. }
  652. Instruction *ResOp = cast<Instruction>(Res->getOperand(i));
  653. CallInst *HandleOp = dyn_cast<CallInst>(ResOp);
  654. if (!HandleOp) {
  655. if (handleMap.count(ResOp)) {
  656. Res->getContext().emitError(Res, kResourceMapErrorMsg);
  657. continue;
  658. }
  659. HandleOp = handleMap[ResOp];
  660. }
  661. Value *resClassOp =
  662. HandleOp->getArgOperand(DXIL::OperandIndex::kCreateHandleResClassOpIdx);
  663. Value *resIDOp =
  664. HandleOp->getArgOperand(DXIL::OperandIndex::kCreateHandleResIDOpIdx);
  665. Value *resAddrOp =
  666. HandleOp->getArgOperand(DXIL::OperandIndex::kCreateHandleResIndexOpIdx);
  667. resClass->setOperand(i, resClassOp);
  668. resID->setOperand(i, resIDOp);
  669. resAddr->setOperand(i, resAddrOp);
  670. }
  671. if (!MergeHandleOpWithSameValue(resClass, startOpIdx, numOperands))
  672. nonUniformOps.insert(resClass);
  673. if (!MergeHandleOpWithSameValue(resID, startOpIdx, numOperands))
  674. nonUniformOps.insert(resID);
  675. MergeHandleOpWithSameValue(resAddr, startOpIdx, numOperands);
  676. }
  677. void DxilGenerationPass::AddCreateHandleForPhiNodeAndSelect(OP *hlslOP) {
  678. Function *createHandle = hlslOP->GetOpFunc(
  679. OP::OpCode::CreateHandle, llvm::Type::getVoidTy(hlslOP->GetCtx()));
  680. std::unordered_set<PHINode *> objPhiList;
  681. std::unordered_set<SelectInst *> objSelectList;
  682. std::unordered_set<Instruction *> resSelectSet;
  683. for (User *U : createHandle->users()) {
  684. for (User *HandleU : U->users()) {
  685. Instruction *I = cast<Instruction>(HandleU);
  686. if (!isa<CallInst>(I))
  687. AddResourceToSet(I, resSelectSet);
  688. }
  689. }
  690. // Generate Handle inst for Res inst.
  691. FunctionType *FT = createHandle->getFunctionType();
  692. Value *opArg = hlslOP->GetU32Const((unsigned)OP::OpCode::CreateHandle);
  693. Type *resClassTy =
  694. FT->getParamType(DXIL::OperandIndex::kCreateHandleResClassOpIdx);
  695. Type *resIDTy = FT->getParamType(DXIL::OperandIndex::kCreateHandleResIDOpIdx);
  696. Type *resAddrTy =
  697. FT->getParamType(DXIL::OperandIndex::kCreateHandleResIndexOpIdx);
  698. Value *UndefResClass = UndefValue::get(resClassTy);
  699. Value *UndefResID = UndefValue::get(resIDTy);
  700. Value *UndefResAddr = UndefValue::get(resAddrTy);
  701. // phi/select node resource is not uniform
  702. Value *nonUniformRes = hlslOP->GetI1Const(1);
  703. std::unordered_map<Instruction *, CallInst *> handleMap;
  704. for (Instruction *Res : resSelectSet) {
  705. unsigned numOperands = Res->getNumOperands();
  706. IRBuilder<> Builder(Res);
  707. unsigned startOpIdx = 0;
  708. // Skip Cond for Select.
  709. if (SelectInst *Sel = dyn_cast<SelectInst>(Res)) {
  710. startOpIdx = 1;
  711. Value *Cond = Sel->getCondition();
  712. Value *resClassSel =
  713. Builder.CreateSelect(Cond, UndefResClass, UndefResClass);
  714. Value *resIDSel = Builder.CreateSelect(Cond, UndefResID, UndefResID);
  715. Value *resAddrSel =
  716. Builder.CreateSelect(Cond, UndefResAddr, UndefResAddr);
  717. CallInst *HandleSel =
  718. Builder.CreateCall(createHandle, {opArg, resClassSel, resIDSel,
  719. resAddrSel, nonUniformRes});
  720. handleMap[Res] = HandleSel;
  721. Res->replaceAllUsesWith(HandleSel);
  722. } else {
  723. PHINode *Phi = cast<PHINode>(Res); // res class must be same.
  724. PHINode *resClassPhi = Builder.CreatePHI(resClassTy, numOperands);
  725. PHINode *resIDPhi = Builder.CreatePHI(resIDTy, numOperands);
  726. PHINode *resAddrPhi = Builder.CreatePHI(resAddrTy, numOperands);
  727. for (unsigned i = 0; i < numOperands; i++) {
  728. BasicBlock *BB = Phi->getIncomingBlock(i);
  729. resClassPhi->addIncoming(UndefResClass, BB);
  730. resIDPhi->addIncoming(UndefResID, BB);
  731. resAddrPhi->addIncoming(UndefResAddr, BB);
  732. }
  733. IRBuilder<> HandleBuilder(Phi->getParent()->getFirstNonPHI());
  734. CallInst *HandlePhi =
  735. HandleBuilder.CreateCall(createHandle, {opArg, resClassPhi, resIDPhi,
  736. resAddrPhi, nonUniformRes});
  737. handleMap[Res] = HandlePhi;
  738. Res->replaceAllUsesWith(HandlePhi);
  739. }
  740. }
  741. // Update operand for Handle phi/select.
  742. // If ResClass or ResID is phi/select, save to nonUniformOps.
  743. std::unordered_set<Instruction *> nonUniformOps;
  744. for (Instruction *Res : resSelectSet) {
  745. UpdateHandleOperands(Res, handleMap, nonUniformOps);
  746. }
  747. bool bIsLib = m_pHLModule->GetShaderModel()->IsLib();
  748. // ResClass and ResID must be uniform.
  749. // Try to merge res class, res id into imm.
  750. while (1) {
  751. bool bUpdated = false;
  752. for (auto It = nonUniformOps.begin(); It != nonUniformOps.end();) {
  753. Instruction *I = *(It++);
  754. unsigned numOperands = I->getNumOperands();
  755. unsigned startOpIdx = 0;
  756. // Skip Cond for Select.
  757. if (SelectInst *Sel = dyn_cast<SelectInst>(I))
  758. startOpIdx = 1;
  759. if (MergeHandleOpWithSameValue(I, startOpIdx, numOperands)) {
  760. nonUniformOps.erase(I);
  761. bUpdated = true;
  762. }
  763. }
  764. if (!bUpdated) {
  765. if (!nonUniformOps.empty() && !bIsLib) {
  766. for (Instruction *I : nonUniformOps) {
  767. // Non uniform res class or res id.
  768. FT->getContext().emitError(I, kResourceMapErrorMsg);
  769. }
  770. return;
  771. }
  772. break;
  773. }
  774. }
  775. // Remove useless select/phi.
  776. for (Instruction *Res : resSelectSet) {
  777. Res->eraseFromParent();
  778. }
  779. }
  780. void DxilGenerationPass::GenerateDxilCBufferHandles(
  781. std::unordered_set<Value *> &NonUniformSet) {
  782. // For CBuffer, handle are mapped to HLCreateHandle.
  783. OP *hlslOP = m_pHLModule->GetOP();
  784. Function *createHandle = hlslOP->GetOpFunc(
  785. OP::OpCode::CreateHandle, llvm::Type::getVoidTy(m_pHLModule->GetCtx()));
  786. Value *opArg = hlslOP->GetU32Const((unsigned)OP::OpCode::CreateHandle);
  787. Value *resClassArg = hlslOP->GetU8Const(
  788. static_cast<std::underlying_type<DxilResourceBase::Class>::type>(
  789. DXIL::ResourceClass::CBuffer));
  790. for (size_t i = 0; i < m_pHLModule->GetCBuffers().size(); i++) {
  791. DxilCBuffer &CB = m_pHLModule->GetCBuffer(i);
  792. GlobalVariable *GV = cast<GlobalVariable>(CB.GetGlobalSymbol());
  793. // Remove GEP created in HLObjectOperationLowerHelper::UniformCbPtr.
  794. GV->removeDeadConstantUsers();
  795. std::string handleName = std::string(GV->getName()) + "_buffer";
  796. Value *args[] = {opArg, resClassArg, nullptr, nullptr,
  797. hlslOP->GetI1Const(0)};
  798. DIVariable *DIV = nullptr;
  799. DILocation *DL = nullptr;
  800. if (m_HasDbgInfo) {
  801. DebugInfoFinder &Finder = m_pHLModule->GetOrCreateDebugInfoFinder();
  802. DIV = HLModule::FindGlobalVariableDebugInfo(GV, Finder);
  803. if (DIV)
  804. // TODO: how to get col?
  805. DL = DILocation::get(createHandle->getContext(), DIV->getLine(), 1,
  806. DIV->getScope());
  807. }
  808. Value *resIDArg = hlslOP->GetU32Const(CB.GetID());
  809. args[DXIL::OperandIndex::kCreateHandleResIDOpIdx] = resIDArg;
  810. // resLowerBound will be added after allocation in DxilCondenseResources.
  811. Value *resLowerBound = hlslOP->GetU32Const(0);
  812. if (CB.GetRangeSize() == 1) {
  813. args[DXIL::OperandIndex::kCreateHandleResIndexOpIdx] = resLowerBound;
  814. for (auto U = GV->user_begin(); U != GV->user_end(); ) {
  815. // Must HLCreateHandle.
  816. CallInst *CI = cast<CallInst>(*(U++));
  817. // Put createHandle to entry block.
  818. auto InsertPt =
  819. CI->getParent()->getParent()->getEntryBlock().getFirstInsertionPt();
  820. IRBuilder<> Builder(InsertPt);
  821. CallInst *handle = Builder.CreateCall(createHandle, args, handleName);
  822. if (m_HasDbgInfo) {
  823. // TODO: add debug info.
  824. //handle->setDebugLoc(DL);
  825. }
  826. CI->replaceAllUsesWith(handle);
  827. CI->eraseFromParent();
  828. }
  829. } else {
  830. for (auto U = GV->user_begin(); U != GV->user_end(); ) {
  831. // Must HLCreateHandle.
  832. CallInst *CI = cast<CallInst>(*(U++));
  833. IRBuilder<> Builder(CI);
  834. Value *CBIndex = CI->getArgOperand(HLOperandIndex::kCreateHandleIndexOpIdx);
  835. args[DXIL::OperandIndex::kCreateHandleResIndexOpIdx] =
  836. CBIndex;
  837. if (isa<ConstantInt>(CBIndex)) {
  838. // Put createHandle to entry block for const index.
  839. auto InsertPt = CI->getParent()
  840. ->getParent()
  841. ->getEntryBlock()
  842. .getFirstInsertionPt();
  843. Builder.SetInsertPoint(InsertPt);
  844. }
  845. if (!NonUniformSet.count(CBIndex))
  846. args[DXIL::OperandIndex::kCreateHandleIsUniformOpIdx] =
  847. hlslOP->GetI1Const(0);
  848. else
  849. args[DXIL::OperandIndex::kCreateHandleIsUniformOpIdx] =
  850. hlslOP->GetI1Const(1);
  851. CallInst *handle = Builder.CreateCall(createHandle, args, handleName);
  852. CI->replaceAllUsesWith(handle);
  853. CI->eraseFromParent();
  854. }
  855. }
  856. }
  857. }
  858. void DxilGenerationPass::GenerateDxilOperations(
  859. Module &M, std::unordered_set<LoadInst *> &UpdateCounterSet,
  860. std::unordered_set<Value *> &NonUniformSet) {
  861. // remove all functions except entry function
  862. Function *entry = m_pHLModule->GetEntryFunction();
  863. const ShaderModel *pSM = m_pHLModule->GetShaderModel();
  864. Function *patchConstantFunc = nullptr;
  865. if (pSM->IsHS()) {
  866. DxilFunctionProps &funcProps = m_pHLModule->GetDxilFunctionProps(entry);
  867. patchConstantFunc = funcProps.ShaderProps.HS.patchConstantFunc;
  868. }
  869. if (!pSM->IsLib()) {
  870. for (auto F = M.begin(); F != M.end();) {
  871. Function *func = F++;
  872. if (func->isDeclaration())
  873. continue;
  874. if (func == entry)
  875. continue;
  876. if (func == patchConstantFunc)
  877. continue;
  878. if (func->user_empty())
  879. func->eraseFromParent();
  880. }
  881. }
  882. TranslateBuiltinOperations(*m_pHLModule, m_extensionsCodegenHelper,
  883. UpdateCounterSet, NonUniformSet);
  884. // Remove unused HL Operation functions.
  885. std::vector<Function *> deadList;
  886. for (iplist<Function>::iterator F : M.getFunctionList()) {
  887. hlsl::HLOpcodeGroup group = hlsl::GetHLOpcodeGroupByName(F);
  888. if (group != HLOpcodeGroup::NotHL || F->isIntrinsic())
  889. if (F->user_empty())
  890. deadList.emplace_back(F);
  891. }
  892. for (Function *F : deadList)
  893. F->eraseFromParent();
  894. }
  895. static void TranslatePreciseAttributeOnFunction(Function &F, Module &M) {
  896. BasicBlock &BB = F.getEntryBlock(); // Get the entry node for the function
  897. // Find allocas that has precise attribute, by looking at all instructions in
  898. // the entry node
  899. for (BasicBlock::iterator I = BB.begin(), E = BB.end(); I != E;) {
  900. Instruction *Inst = (I++);
  901. if (AllocaInst *AI = dyn_cast<AllocaInst>(Inst)) {
  902. if (HLModule::HasPreciseAttributeWithMetadata(AI)) {
  903. HLModule::MarkPreciseAttributeOnPtrWithFunctionCall(AI, M);
  904. }
  905. } else {
  906. DXASSERT(!HLModule::HasPreciseAttributeWithMetadata(Inst), "Only alloca can has precise metadata.");
  907. }
  908. }
  909. FastMathFlags FMF;
  910. FMF.setUnsafeAlgebra();
  911. // Set fast math for all FPMathOperators.
  912. // Already set FastMath in options. But that only enable things like fadd.
  913. // Every inst which type is float can be cast to FPMathOperator.
  914. for (Function::iterator BBI = F.begin(), BBE = F.end(); BBI != BBE; ++BBI) {
  915. BasicBlock *BB = BBI;
  916. for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) {
  917. if (FPMathOperator *FPMath = dyn_cast<FPMathOperator>(I)) {
  918. // Set precise fast math on those instructions that support it.
  919. if (DxilModule::PreservesFastMathFlags(I))
  920. I->copyFastMathFlags(FMF);
  921. }
  922. }
  923. }
  924. }
  925. void DxilGenerationPass::TranslatePreciseAttribute() {
  926. bool bIEEEStrict = m_pHLModule->GetHLOptions().bIEEEStrict;
  927. // If IEEE strict, everying is precise, don't need to mark it.
  928. if (bIEEEStrict)
  929. return;
  930. Module &M = *m_pHLModule->GetModule();
  931. // TODO: If not inline every function, for function has call site with precise
  932. // argument and call site without precise argument, need to clone the function
  933. // to propagate the precise for the precise call site.
  934. // This should be done at CGMSHLSLRuntime::FinishCodeGen.
  935. Function *EntryFn = m_pHLModule->GetEntryFunction();
  936. if (!m_pHLModule->GetShaderModel()->IsLib()) {
  937. TranslatePreciseAttributeOnFunction(*EntryFn, M);
  938. }
  939. if (m_pHLModule->GetShaderModel()->IsHS()) {
  940. DxilFunctionProps &EntryQual = m_pHLModule->GetDxilFunctionProps(EntryFn);
  941. Function *patchConstantFunc = EntryQual.ShaderProps.HS.patchConstantFunc;
  942. TranslatePreciseAttributeOnFunction(*patchConstantFunc, M);
  943. }
  944. }
  945. char DxilGenerationPass::ID = 0;
  946. ModulePass *llvm::createDxilGenerationPass(bool NotOptimized, hlsl::HLSLExtensionsCodegenHelper *extensionsHelper) {
  947. DxilGenerationPass *dxilPass = new DxilGenerationPass(NotOptimized);
  948. dxilPass->SetExtensionsHelper(extensionsHelper);
  949. return dxilPass;
  950. }
  951. INITIALIZE_PASS(DxilGenerationPass, "dxilgen", "HLSL DXIL Generation", false, false)
  952. ///////////////////////////////////////////////////////////////////////////////
  953. namespace {
  954. StructType *UpdateStructTypeForLegacyLayout(StructType *ST, bool IsCBuf,
  955. DxilTypeSystem &TypeSys, Module &M);
  956. Type *UpdateFieldTypeForLegacyLayout(Type *Ty, bool IsCBuf, DxilFieldAnnotation &annotation,
  957. DxilTypeSystem &TypeSys, Module &M) {
  958. DXASSERT(!Ty->isPointerTy(), "struct field should not be a pointer");
  959. if (Ty->isArrayTy()) {
  960. Type *EltTy = Ty->getArrayElementType();
  961. Type *UpdatedTy = UpdateFieldTypeForLegacyLayout(EltTy, IsCBuf, annotation, TypeSys, M);
  962. if (EltTy == UpdatedTy)
  963. return Ty;
  964. else
  965. return ArrayType::get(UpdatedTy, Ty->getArrayNumElements());
  966. } else if (HLMatrixLower::IsMatrixType(Ty)) {
  967. DXASSERT(annotation.HasMatrixAnnotation(), "must a matrix");
  968. unsigned rows, cols;
  969. Type *EltTy = HLMatrixLower::GetMatrixInfo(Ty, cols, rows);
  970. // Get cols and rows from annotation.
  971. const DxilMatrixAnnotation &matrix = annotation.GetMatrixAnnotation();
  972. if (matrix.Orientation == MatrixOrientation::RowMajor) {
  973. rows = matrix.Rows;
  974. cols = matrix.Cols;
  975. } else {
  976. DXASSERT(matrix.Orientation == MatrixOrientation::ColumnMajor, "");
  977. cols = matrix.Rows;
  978. rows = matrix.Cols;
  979. }
  980. // CBuffer matrix must 4 * 4 bytes align.
  981. if (IsCBuf)
  982. cols = 4;
  983. EltTy = UpdateFieldTypeForLegacyLayout(EltTy, IsCBuf, annotation, TypeSys, M);
  984. Type *rowTy = VectorType::get(EltTy, cols);
  985. return ArrayType::get(rowTy, rows);
  986. } else if (StructType *ST = dyn_cast<StructType>(Ty)) {
  987. return UpdateStructTypeForLegacyLayout(ST, IsCBuf, TypeSys, M);
  988. } else if (Ty->isVectorTy()) {
  989. Type *EltTy = Ty->getVectorElementType();
  990. Type *UpdatedTy = UpdateFieldTypeForLegacyLayout(EltTy, IsCBuf, annotation, TypeSys, M);
  991. if (EltTy == UpdatedTy)
  992. return Ty;
  993. else
  994. return VectorType::get(UpdatedTy, Ty->getVectorNumElements());
  995. } else {
  996. Type *i32Ty = Type::getInt32Ty(Ty->getContext());
  997. // Basic types.
  998. if (Ty->isHalfTy()) {
  999. return Type::getFloatTy(Ty->getContext());
  1000. } else if (IntegerType *ITy = dyn_cast<IntegerType>(Ty)) {
  1001. if (ITy->getBitWidth() < 32)
  1002. return i32Ty;
  1003. else
  1004. return Ty;
  1005. } else
  1006. return Ty;
  1007. }
  1008. }
  1009. StructType *UpdateStructTypeForLegacyLayout(StructType *ST, bool IsCBuf,
  1010. DxilTypeSystem &TypeSys, Module &M) {
  1011. bool bUpdated = false;
  1012. unsigned fieldsCount = ST->getNumElements();
  1013. std::vector<Type *> fieldTypes(fieldsCount);
  1014. DxilStructAnnotation *SA = TypeSys.GetStructAnnotation(ST);
  1015. DXASSERT(SA, "must have annotation for struct type");
  1016. for (unsigned i = 0; i < fieldsCount; i++) {
  1017. Type *EltTy = ST->getElementType(i);
  1018. Type *UpdatedTy =
  1019. UpdateFieldTypeForLegacyLayout(EltTy, IsCBuf, SA->GetFieldAnnotation(i), TypeSys, M);
  1020. fieldTypes[i] = UpdatedTy;
  1021. if (EltTy != UpdatedTy)
  1022. bUpdated = true;
  1023. }
  1024. if (!bUpdated) {
  1025. return ST;
  1026. } else {
  1027. std::string legacyName = "dx.alignment.legacy." + ST->getName().str();
  1028. if (StructType *legacyST = M.getTypeByName(legacyName))
  1029. return legacyST;
  1030. StructType *NewST = StructType::create(ST->getContext(), fieldTypes, legacyName);
  1031. DxilStructAnnotation *NewSA = TypeSys.AddStructAnnotation(NewST);
  1032. // Clone annotation.
  1033. *NewSA = *SA;
  1034. return NewST;
  1035. }
  1036. }
  1037. void UpdateStructTypeForLegacyLayout(DxilResourceBase &Res, DxilTypeSystem &TypeSys, Module &M) {
  1038. GlobalVariable *GV = cast<GlobalVariable>(Res.GetGlobalSymbol());
  1039. Type *Ty = GV->getType()->getPointerElementType();
  1040. bool IsResourceArray = Res.GetRangeSize() != 1;
  1041. if (IsResourceArray) {
  1042. // Support Array of struct buffer.
  1043. if (Ty->isArrayTy())
  1044. Ty = Ty->getArrayElementType();
  1045. }
  1046. StructType *ST = cast<StructType>(Ty);
  1047. if (ST->isOpaque()) {
  1048. DXASSERT(Res.GetClass() == DxilResourceBase::Class::CBuffer,
  1049. "Only cbuffer can have opaque struct.");
  1050. return;
  1051. }
  1052. Type *UpdatedST = UpdateStructTypeForLegacyLayout(ST, IsResourceArray, TypeSys, M);
  1053. if (ST != UpdatedST) {
  1054. Type *Ty = GV->getType()->getPointerElementType();
  1055. if (IsResourceArray) {
  1056. // Support Array of struct buffer.
  1057. if (Ty->isArrayTy()) {
  1058. UpdatedST = ArrayType::get(UpdatedST, Ty->getArrayNumElements());
  1059. }
  1060. }
  1061. GlobalVariable *NewGV = cast<GlobalVariable>(M.getOrInsertGlobal(GV->getName().str() + "_legacy", UpdatedST));
  1062. Res.SetGlobalSymbol(NewGV);
  1063. // Delete old GV.
  1064. for (auto UserIt = GV->user_begin(); UserIt != GV->user_end(); ) {
  1065. Value *User = *(UserIt++);
  1066. if (Instruction *I = dyn_cast<Instruction>(User)) {
  1067. if (!User->user_empty())
  1068. I->replaceAllUsesWith(UndefValue::get(I->getType()));
  1069. I->eraseFromParent();
  1070. } else {
  1071. ConstantExpr *CE = cast<ConstantExpr>(User);
  1072. if (!CE->user_empty())
  1073. CE->replaceAllUsesWith(UndefValue::get(CE->getType()));
  1074. }
  1075. }
  1076. GV->removeDeadConstantUsers();
  1077. GV->eraseFromParent();
  1078. }
  1079. }
  1080. void UpdateStructTypeForLegacyLayoutOnHLM(HLModule &HLM) {
  1081. DxilTypeSystem &TypeSys = HLM.GetTypeSystem();
  1082. Module &M = *HLM.GetModule();
  1083. for (auto &CBuf : HLM.GetCBuffers()) {
  1084. UpdateStructTypeForLegacyLayout(*CBuf.get(), TypeSys, M);
  1085. }
  1086. for (auto &UAV : HLM.GetUAVs()) {
  1087. if (UAV->GetKind() == DxilResourceBase::Kind::StructuredBuffer)
  1088. UpdateStructTypeForLegacyLayout(*UAV.get(), TypeSys, M);
  1089. }
  1090. for (auto &SRV : HLM.GetSRVs()) {
  1091. if (SRV->GetKind() == DxilResourceBase::Kind::StructuredBuffer)
  1092. UpdateStructTypeForLegacyLayout(*SRV.get(), TypeSys, M);
  1093. }
  1094. }
  1095. }
  1096. void DxilGenerationPass::UpdateStructTypeForLegacyLayout() {
  1097. UpdateStructTypeForLegacyLayoutOnHLM(*m_pHLModule);
  1098. }
  1099. ///////////////////////////////////////////////////////////////////////////////
  1100. namespace {
  1101. class HLEmitMetadata : public ModulePass {
  1102. public:
  1103. static char ID; // Pass identification, replacement for typeid
  1104. explicit HLEmitMetadata() : ModulePass(ID) {}
  1105. const char *getPassName() const override { return "HLSL High-Level Metadata Emit"; }
  1106. bool runOnModule(Module &M) override {
  1107. if (M.HasHLModule()) {
  1108. HLModule::ClearHLMetadata(M);
  1109. M.GetHLModule().EmitHLMetadata();
  1110. return true;
  1111. }
  1112. return false;
  1113. }
  1114. };
  1115. }
  1116. char HLEmitMetadata::ID = 0;
  1117. ModulePass *llvm::createHLEmitMetadataPass() {
  1118. return new HLEmitMetadata();
  1119. }
  1120. INITIALIZE_PASS(HLEmitMetadata, "hlsl-hlemit", "HLSL High-Level Metadata Emit", false, false)
  1121. ///////////////////////////////////////////////////////////////////////////////
  1122. namespace {
  1123. class HLEnsureMetadata : public ModulePass {
  1124. public:
  1125. static char ID; // Pass identification, replacement for typeid
  1126. explicit HLEnsureMetadata() : ModulePass(ID) {}
  1127. const char *getPassName() const override { return "HLSL High-Level Metadata Ensure"; }
  1128. bool runOnModule(Module &M) override {
  1129. if (!M.HasHLModule()) {
  1130. M.GetOrCreateHLModule();
  1131. return true;
  1132. }
  1133. return false;
  1134. }
  1135. };
  1136. }
  1137. char HLEnsureMetadata::ID = 0;
  1138. ModulePass *llvm::createHLEnsureMetadataPass() {
  1139. return new HLEnsureMetadata();
  1140. }
  1141. INITIALIZE_PASS(HLEnsureMetadata, "hlsl-hlensure", "HLSL High-Level Metadata Ensure", false, false)
  1142. ///////////////////////////////////////////////////////////////////////////////
  1143. // Precise propagate.
  1144. namespace {
  1145. class DxilPrecisePropagatePass : public ModulePass {
  1146. HLModule *m_pHLModule;
  1147. public:
  1148. static char ID; // Pass identification, replacement for typeid
  1149. explicit DxilPrecisePropagatePass() : ModulePass(ID), m_pHLModule(nullptr) {}
  1150. const char *getPassName() const override { return "DXIL Precise Propagate"; }
  1151. bool runOnModule(Module &M) override {
  1152. DxilModule &dxilModule = M.GetOrCreateDxilModule();
  1153. DxilTypeSystem &typeSys = dxilModule.GetTypeSystem();
  1154. std::unordered_set<Instruction*> processedSet;
  1155. std::vector<Function*> deadList;
  1156. for (Function &F : M.functions()) {
  1157. if (HLModule::HasPreciseAttribute(&F)) {
  1158. PropagatePreciseOnFunctionUser(F, typeSys, processedSet);
  1159. deadList.emplace_back(&F);
  1160. }
  1161. }
  1162. for (Function *F : deadList)
  1163. F->eraseFromParent();
  1164. return true;
  1165. }
  1166. private:
  1167. void PropagatePreciseOnFunctionUser(
  1168. Function &F, DxilTypeSystem &typeSys,
  1169. std::unordered_set<Instruction *> &processedSet);
  1170. };
  1171. char DxilPrecisePropagatePass::ID = 0;
  1172. }
  1173. static void PropagatePreciseAttribute(Instruction *I, DxilTypeSystem &typeSys,
  1174. std::unordered_set<Instruction *> &processedSet);
  1175. static void PropagatePreciseAttributeOnOperand(
  1176. Value *V, DxilTypeSystem &typeSys, LLVMContext &Context,
  1177. std::unordered_set<Instruction *> &processedSet) {
  1178. Instruction *I = dyn_cast<Instruction>(V);
  1179. // Skip none inst.
  1180. if (!I)
  1181. return;
  1182. FPMathOperator *FPMath = dyn_cast<FPMathOperator>(I);
  1183. // Skip none FPMath
  1184. if (!FPMath)
  1185. return;
  1186. // Skip inst already marked.
  1187. if (processedSet.count(I) > 0)
  1188. return;
  1189. // TODO: skip precise on integer type, sample instruction...
  1190. processedSet.insert(I);
  1191. // Set precise fast math on those instructions that support it.
  1192. if (DxilModule::PreservesFastMathFlags(I))
  1193. DxilModule::SetPreciseFastMathFlags(I);
  1194. // Fast math not work on call, use metadata.
  1195. if (CallInst *CI = dyn_cast<CallInst>(I))
  1196. HLModule::MarkPreciseAttributeWithMetadata(CI);
  1197. PropagatePreciseAttribute(I, typeSys, processedSet);
  1198. }
  1199. static void PropagatePreciseAttributeOnPointer(
  1200. Value *Ptr, DxilTypeSystem &typeSys, LLVMContext &Context,
  1201. std::unordered_set<Instruction *> &processedSet) {
  1202. // Find all store and propagate on the val operand of store.
  1203. // For CallInst, if Ptr is used as out parameter, mark it.
  1204. for (User *U : Ptr->users()) {
  1205. Instruction *user = cast<Instruction>(U);
  1206. if (StoreInst *stInst = dyn_cast<StoreInst>(user)) {
  1207. Value *val = stInst->getValueOperand();
  1208. PropagatePreciseAttributeOnOperand(val, typeSys, Context, processedSet);
  1209. } else if (CallInst *CI = dyn_cast<CallInst>(user)) {
  1210. bool bReadOnly = true;
  1211. Function *F = CI->getCalledFunction();
  1212. const DxilFunctionAnnotation *funcAnnotation =
  1213. typeSys.GetFunctionAnnotation(F);
  1214. for (unsigned i = 0; i < CI->getNumArgOperands(); ++i) {
  1215. if (Ptr != CI->getArgOperand(i))
  1216. continue;
  1217. const DxilParameterAnnotation &paramAnnotation =
  1218. funcAnnotation->GetParameterAnnotation(i);
  1219. // OutputPatch and OutputStream will be checked after scalar repl.
  1220. // Here only check out/inout
  1221. if (paramAnnotation.GetParamInputQual() == DxilParamInputQual::Out ||
  1222. paramAnnotation.GetParamInputQual() == DxilParamInputQual::Inout) {
  1223. bReadOnly = false;
  1224. break;
  1225. }
  1226. }
  1227. if (!bReadOnly)
  1228. PropagatePreciseAttributeOnOperand(CI, typeSys, Context, processedSet);
  1229. }
  1230. }
  1231. }
  1232. static void
  1233. PropagatePreciseAttribute(Instruction *I, DxilTypeSystem &typeSys,
  1234. std::unordered_set<Instruction *> &processedSet) {
  1235. LLVMContext &Context = I->getContext();
  1236. if (AllocaInst *AI = dyn_cast<AllocaInst>(I)) {
  1237. PropagatePreciseAttributeOnPointer(AI, typeSys, Context, processedSet);
  1238. } else if (CallInst *CI = dyn_cast<CallInst>(I)) {
  1239. // Propagate every argument.
  1240. // TODO: only propagate precise argument.
  1241. for (Value *src : I->operands())
  1242. PropagatePreciseAttributeOnOperand(src, typeSys, Context, processedSet);
  1243. } else if (FPMathOperator *FPMath = dyn_cast<FPMathOperator>(I)) {
  1244. // TODO: only propagate precise argument.
  1245. for (Value *src : I->operands())
  1246. PropagatePreciseAttributeOnOperand(src, typeSys, Context, processedSet);
  1247. } else if (LoadInst *ldInst = dyn_cast<LoadInst>(I)) {
  1248. Value *Ptr = ldInst->getPointerOperand();
  1249. PropagatePreciseAttributeOnPointer(Ptr, typeSys, Context, processedSet);
  1250. } else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(I))
  1251. PropagatePreciseAttributeOnPointer(GEP, typeSys, Context, processedSet);
  1252. // TODO: support more case which need
  1253. }
  1254. void DxilPrecisePropagatePass::PropagatePreciseOnFunctionUser(
  1255. Function &F, DxilTypeSystem &typeSys,
  1256. std::unordered_set<Instruction *> &processedSet) {
  1257. LLVMContext &Context = F.getContext();
  1258. for (auto U = F.user_begin(), E = F.user_end(); U != E;) {
  1259. CallInst *CI = cast<CallInst>(*(U++));
  1260. Value *V = CI->getArgOperand(0);
  1261. PropagatePreciseAttributeOnOperand(V, typeSys, Context, processedSet);
  1262. CI->eraseFromParent();
  1263. }
  1264. }
  1265. ModulePass *llvm::createDxilPrecisePropagatePass() {
  1266. return new DxilPrecisePropagatePass();
  1267. }
  1268. INITIALIZE_PASS(DxilPrecisePropagatePass, "hlsl-dxil-precise", "DXIL precise attribute propagate", false, false)
  1269. ///////////////////////////////////////////////////////////////////////////////
  1270. namespace {
  1271. class HLDeadFunctionElimination : public ModulePass {
  1272. public:
  1273. static char ID; // Pass identification, replacement for typeid
  1274. explicit HLDeadFunctionElimination () : ModulePass(ID) {}
  1275. const char *getPassName() const override { return "Remove all unused function except entry from HLModule"; }
  1276. bool runOnModule(Module &M) override {
  1277. if (M.HasHLModule()) {
  1278. HLModule &HLM = M.GetHLModule();
  1279. bool IsLib = HLM.GetShaderModel()->IsLib();
  1280. // Remove unused functions except entry and patch constant func.
  1281. // For library profile, only remove unused external functions.
  1282. Function *EntryFunc = HLM.GetEntryFunction();
  1283. Function *PatchConstantFunc = HLM.GetPatchConstantFunction();
  1284. return dxilutil::RemoveUnusedFunctions(M, EntryFunc, PatchConstantFunc,
  1285. IsLib);
  1286. }
  1287. return false;
  1288. }
  1289. };
  1290. }
  1291. char HLDeadFunctionElimination::ID = 0;
  1292. ModulePass *llvm::createHLDeadFunctionEliminationPass() {
  1293. return new HLDeadFunctionElimination();
  1294. }
  1295. INITIALIZE_PASS(HLDeadFunctionElimination, "hl-dfe", "Remove all unused function except entry from HLModule", false, false)
  1296. ///////////////////////////////////////////////////////////////////////////////
  1297. // Legalize resource use.
  1298. // Map local or static global resource to global resource.
  1299. // Require inline for static global resource.
  1300. namespace {
  1301. class DxilLegalizeStaticResourceUsePass : public ModulePass {
  1302. public:
  1303. static char ID; // Pass identification, replacement for typeid
  1304. explicit DxilLegalizeStaticResourceUsePass()
  1305. : ModulePass(ID) {}
  1306. const char *getPassName() const override {
  1307. return "DXIL Legalize Static Resource Use";
  1308. }
  1309. bool runOnModule(Module &M) override {
  1310. HLModule &HLM = M.GetOrCreateHLModule();
  1311. OP *hlslOP = HLM.GetOP();
  1312. Type *HandleTy = hlslOP->GetHandleType();
  1313. // Promote static global variables.
  1314. PromoteStaticGlobalResources(M);
  1315. // Lower handle cast.
  1316. for (Function &F : M.functions()) {
  1317. if (!F.isDeclaration())
  1318. continue;
  1319. HLOpcodeGroup group = hlsl::GetHLOpcodeGroupByName(&F);
  1320. if (group != HLOpcodeGroup::HLCast)
  1321. continue;
  1322. Type *Ty = F.getFunctionType()->getReturnType();
  1323. if (Ty->isPointerTy())
  1324. Ty = Ty->getPointerElementType();
  1325. if (HLModule::IsHLSLObjectType(Ty)) {
  1326. TransformHandleCast(F);
  1327. }
  1328. }
  1329. Value *UndefHandle = UndefValue::get(HandleTy);
  1330. if (!UndefHandle->user_empty()) {
  1331. for (User *U : UndefHandle->users()) {
  1332. // Report error if undef handle used for function call.
  1333. if (isa<CallInst>(U)) {
  1334. M.getContext().emitError(kResourceMapErrorMsg);
  1335. }
  1336. }
  1337. }
  1338. return true;
  1339. }
  1340. private:
  1341. void PromoteStaticGlobalResources(Module &M);
  1342. void TransformHandleCast(Function &F);
  1343. };
  1344. char DxilLegalizeStaticResourceUsePass::ID = 0;
  1345. class DxilLegalizeResourceUsePass : public FunctionPass {
  1346. HLModule *m_pHLModule;
  1347. void getAnalysisUsage(AnalysisUsage &AU) const override;
  1348. public:
  1349. static char ID; // Pass identification, replacement for typeid
  1350. explicit DxilLegalizeResourceUsePass()
  1351. : FunctionPass(ID), m_pHLModule(nullptr) {}
  1352. const char *getPassName() const override {
  1353. return "DXIL Legalize Resource Use";
  1354. }
  1355. bool runOnFunction(Function &F) override {
  1356. // Promote local resource first.
  1357. PromoteLocalResource(F);
  1358. return true;
  1359. }
  1360. private:
  1361. void PromoteLocalResource(Function &F);
  1362. };
  1363. char DxilLegalizeResourceUsePass::ID = 0;
  1364. }
  1365. void DxilLegalizeResourceUsePass::getAnalysisUsage(AnalysisUsage &AU) const {
  1366. AU.addRequired<AssumptionCacheTracker>();
  1367. AU.addRequired<DominatorTreeWrapperPass>();
  1368. AU.setPreservesAll();
  1369. }
  1370. void DxilLegalizeResourceUsePass::PromoteLocalResource(Function &F) {
  1371. std::vector<AllocaInst *> Allocas;
  1372. DominatorTree *DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
  1373. AssumptionCache &AC =
  1374. getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F);
  1375. HLModule &HLM = F.getParent()->GetOrCreateHLModule();
  1376. OP *hlslOP = HLM.GetOP();
  1377. Type *HandleTy = hlslOP->GetHandleType();
  1378. bool IsLib = HLM.GetShaderModel()->IsLib();
  1379. BasicBlock &BB = F.getEntryBlock();
  1380. unsigned allocaSize = 0;
  1381. while (1) {
  1382. Allocas.clear();
  1383. // Find allocas that are safe to promote, by looking at all instructions in
  1384. // the entry node
  1385. for (BasicBlock::iterator I = BB.begin(), E = --BB.end(); I != E; ++I)
  1386. if (AllocaInst *AI = dyn_cast<AllocaInst>(I)) { // Is it an alloca?
  1387. if (HandleTy == dxilutil::GetArrayEltTy(AI->getAllocatedType())) {
  1388. // Skip for unpromotable for lib.
  1389. if (!isAllocaPromotable(AI) && IsLib)
  1390. continue;
  1391. DXASSERT(isAllocaPromotable(AI), "otherwise, non-promotable resource array alloca found");
  1392. Allocas.push_back(AI);
  1393. }
  1394. }
  1395. if (Allocas.empty())
  1396. break;
  1397. // No update.
  1398. // Report error and break.
  1399. if (allocaSize == Allocas.size()) {
  1400. F.getContext().emitError(kResourceMapErrorMsg);
  1401. break;
  1402. }
  1403. allocaSize = Allocas.size();
  1404. PromoteMemToReg(Allocas, *DT, nullptr, &AC);
  1405. }
  1406. return;
  1407. }
  1408. FunctionPass *llvm::createDxilLegalizeResourceUsePass() {
  1409. return new DxilLegalizeResourceUsePass();
  1410. }
  1411. INITIALIZE_PASS_BEGIN(DxilLegalizeResourceUsePass,
  1412. "hlsl-dxil-legalize-resource-use",
  1413. "DXIL legalize resource use", false, true)
  1414. INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
  1415. INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
  1416. INITIALIZE_PASS_END(DxilLegalizeResourceUsePass,
  1417. "hlsl-dxil-legalize-resource-use",
  1418. "DXIL legalize resource use", false, true)
  1419. void DxilLegalizeStaticResourceUsePass::PromoteStaticGlobalResources(
  1420. Module &M) {
  1421. HLModule &HLM = M.GetOrCreateHLModule();
  1422. Type *HandleTy = HLM.GetOP()->GetHandleType();
  1423. std::set<GlobalVariable *> staticResources;
  1424. for (auto &GV : M.globals()) {
  1425. if (GV.getLinkage() == GlobalValue::LinkageTypes::InternalLinkage &&
  1426. HandleTy == dxilutil::GetArrayEltTy(GV.getType())) {
  1427. staticResources.insert(&GV);
  1428. }
  1429. }
  1430. SSAUpdater SSA;
  1431. SmallVector<Instruction *, 4> Insts;
  1432. // Make sure every resource load has mapped to global variable.
  1433. while (!staticResources.empty()) {
  1434. bool bUpdated = false;
  1435. for (auto it = staticResources.begin(); it != staticResources.end();) {
  1436. GlobalVariable *GV = *(it++);
  1437. // Build list of instructions to promote.
  1438. for (User *U : GV->users()) {
  1439. Instruction *I = cast<Instruction>(U);
  1440. Insts.emplace_back(I);
  1441. }
  1442. LoadAndStorePromoter(Insts, SSA).run(Insts);
  1443. if (GV->user_empty()) {
  1444. bUpdated = true;
  1445. staticResources.erase(GV);
  1446. }
  1447. Insts.clear();
  1448. }
  1449. if (!bUpdated) {
  1450. M.getContext().emitError(kResourceMapErrorMsg);
  1451. break;
  1452. }
  1453. }
  1454. }
  1455. static void ReplaceResUseWithHandle(Instruction *Res, Value *Handle) {
  1456. Type *HandleTy = Handle->getType();
  1457. for (auto ResU = Res->user_begin(); ResU != Res->user_end();) {
  1458. Instruction *I = cast<Instruction>(*(ResU++));
  1459. if (isa<LoadInst>(I)) {
  1460. ReplaceResUseWithHandle(I, Handle);
  1461. } else if (isa<CallInst>(I)) {
  1462. if (I->getType() == HandleTy)
  1463. I->replaceAllUsesWith(Handle);
  1464. else
  1465. DXASSERT(0, "must createHandle here");
  1466. } else {
  1467. DXASSERT(0, "should only used by load and createHandle");
  1468. }
  1469. if (I->user_empty()) {
  1470. I->eraseFromParent();
  1471. }
  1472. }
  1473. }
  1474. void DxilLegalizeStaticResourceUsePass::TransformHandleCast(Function &F) {
  1475. for (auto U = F.user_begin(); U != F.user_end(); ) {
  1476. CallInst *CI = cast<CallInst>(*(U++));
  1477. Value *Handle = CI->getArgOperand(HLOperandIndex::kUnaryOpSrc0Idx);
  1478. ReplaceResUseWithHandle(CI, Handle);
  1479. if (CI->user_empty())
  1480. CI->eraseFromParent();
  1481. }
  1482. }
  1483. ModulePass *llvm::createDxilLegalizeStaticResourceUsePass() {
  1484. return new DxilLegalizeStaticResourceUsePass();
  1485. }
  1486. INITIALIZE_PASS(DxilLegalizeStaticResourceUsePass,
  1487. "hlsl-dxil-legalize-static-resource-use",
  1488. "DXIL legalize static resource use", false, false)
  1489. ///////////////////////////////////////////////////////////////////////////////
  1490. // Legalize EvalOperations.
  1491. // Make sure src of EvalOperations are from function parameter.
  1492. // This is needed in order to translate EvaluateAttribute operations that traces
  1493. // back to LoadInput operations during translation stage. Promoting load/store
  1494. // instructions beforehand will allow us to easily trace back to loadInput from
  1495. // function call.
  1496. namespace {
  1497. class DxilLegalizeEvalOperations : public ModulePass {
  1498. public:
  1499. static char ID; // Pass identification, replacement for typeid
  1500. explicit DxilLegalizeEvalOperations() : ModulePass(ID) {}
  1501. const char *getPassName() const override {
  1502. return "DXIL Legalize EvalOperations";
  1503. }
  1504. bool runOnModule(Module &M) override {
  1505. for (Function &F : M.getFunctionList()) {
  1506. hlsl::HLOpcodeGroup group = hlsl::GetHLOpcodeGroup(&F);
  1507. if (group != HLOpcodeGroup::NotHL) {
  1508. std::vector<CallInst *> EvalFunctionCalls;
  1509. // Find all EvaluateAttribute calls
  1510. for (User *U : F.users()) {
  1511. if (CallInst *CI = dyn_cast<CallInst>(U)) {
  1512. IntrinsicOp evalOp =
  1513. static_cast<IntrinsicOp>(hlsl::GetHLOpcode(CI));
  1514. if (evalOp == IntrinsicOp::IOP_EvaluateAttributeAtSample ||
  1515. evalOp == IntrinsicOp::IOP_EvaluateAttributeCentroid ||
  1516. evalOp == IntrinsicOp::IOP_EvaluateAttributeSnapped) {
  1517. EvalFunctionCalls.push_back(CI);
  1518. }
  1519. }
  1520. }
  1521. if (EvalFunctionCalls.empty()) {
  1522. continue;
  1523. }
  1524. // Start from the call instruction, find all allocas that this call
  1525. // uses.
  1526. std::unordered_set<AllocaInst *> allocas;
  1527. for (CallInst *CI : EvalFunctionCalls) {
  1528. FindAllocasForEvalOperations(CI, allocas);
  1529. }
  1530. SSAUpdater SSA;
  1531. SmallVector<Instruction *, 4> Insts;
  1532. for (AllocaInst *AI : allocas) {
  1533. for (User *user : AI->users()) {
  1534. if (isa<LoadInst>(user) || isa<StoreInst>(user)) {
  1535. Insts.emplace_back(cast<Instruction>(user));
  1536. }
  1537. }
  1538. LoadAndStorePromoter(Insts, SSA).run(Insts);
  1539. Insts.clear();
  1540. }
  1541. }
  1542. }
  1543. return true;
  1544. }
  1545. private:
  1546. void FindAllocasForEvalOperations(Value *val,
  1547. std::unordered_set<AllocaInst *> &allocas);
  1548. };
  1549. char DxilLegalizeEvalOperations::ID = 0;
  1550. // Find allocas for EvaluateAttribute operations
  1551. void DxilLegalizeEvalOperations::FindAllocasForEvalOperations(
  1552. Value *val, std::unordered_set<AllocaInst *> &allocas) {
  1553. Value *CurVal = val;
  1554. while (!isa<AllocaInst>(CurVal)) {
  1555. if (CallInst *CI = dyn_cast<CallInst>(CurVal)) {
  1556. CurVal = CI->getOperand(HLOperandIndex::kUnaryOpSrc0Idx);
  1557. } else if (InsertElementInst *IE = dyn_cast<InsertElementInst>(CurVal)) {
  1558. Value *arg0 =
  1559. IE->getOperand(0); // Could be another insertelement or undef
  1560. Value *arg1 = IE->getOperand(1);
  1561. FindAllocasForEvalOperations(arg0, allocas);
  1562. CurVal = arg1;
  1563. } else if (ShuffleVectorInst *SV = dyn_cast<ShuffleVectorInst>(CurVal)) {
  1564. Value *arg0 = SV->getOperand(0);
  1565. Value *arg1 = SV->getOperand(1);
  1566. FindAllocasForEvalOperations(
  1567. arg0, allocas); // Shuffle vector could come from different allocas
  1568. CurVal = arg1;
  1569. } else if (ExtractElementInst *EE = dyn_cast<ExtractElementInst>(CurVal)) {
  1570. CurVal = EE->getOperand(0);
  1571. } else if (LoadInst *LI = dyn_cast<LoadInst>(CurVal)) {
  1572. CurVal = LI->getOperand(0);
  1573. } else {
  1574. break;
  1575. }
  1576. }
  1577. if (AllocaInst *AI = dyn_cast<AllocaInst>(CurVal)) {
  1578. allocas.insert(AI);
  1579. }
  1580. }
  1581. } // namespace
  1582. ModulePass *llvm::createDxilLegalizeEvalOperationsPass() {
  1583. return new DxilLegalizeEvalOperations();
  1584. }
  1585. INITIALIZE_PASS(DxilLegalizeEvalOperations,
  1586. "hlsl-dxil-legalize-eval-operations",
  1587. "DXIL legalize eval operations", false, false)
  1588. ///////////////////////////////////////////////////////////////////////////////
  1589. // Translate RawBufferLoad/RawBufferStore
  1590. // This pass is to make sure that we generate correct buffer load for DXIL
  1591. // For DXIL < 1.2, rawBufferLoad will be translated to BufferLoad instruction
  1592. // without mask.
  1593. // For DXIL >= 1.2, if min precision is enabled, currently generation pass is
  1594. // producing i16/f16 return type for min precisions. For rawBuffer, we will
  1595. // change this so that min precisions are returning its actual scalar type (i32/f32)
  1596. // and will be truncated to their corresponding types after loading / before storing.
  1597. namespace {
  1598. class DxilTranslateRawBuffer : public ModulePass {
  1599. public:
  1600. static char ID;
  1601. explicit DxilTranslateRawBuffer() : ModulePass(ID) {}
  1602. bool runOnModule(Module &M) {
  1603. unsigned major, minor;
  1604. M.GetDxilModule().GetDxilVersion(major, minor);
  1605. DxilModule::ShaderFlags flag = M.GetDxilModule().m_ShaderFlags;
  1606. if (major == 1 && minor < 2) {
  1607. for (auto F = M.functions().begin(), E = M.functions().end(); F != E;) {
  1608. Function *func = &*(F++);
  1609. if (func->hasName()) {
  1610. if (func->getName().startswith("dx.op.rawBufferLoad")) {
  1611. ReplaceRawBufferLoad(func, M);
  1612. func->eraseFromParent();
  1613. } else if (func->getName().startswith("dx.op.rawBufferStore")) {
  1614. ReplaceRawBufferStore(func, M);
  1615. func->eraseFromParent();
  1616. }
  1617. }
  1618. }
  1619. } else if (!flag.GetUseNativeLowPrecision()) {
  1620. for (auto F = M.functions().begin(), E = M.functions().end(); F != E;) {
  1621. Function *func = &*(F++);
  1622. if (func->hasName()) {
  1623. if (func->getName().startswith("dx.op.rawBufferLoad")) {
  1624. ReplaceMinPrecisionRawBufferLoad(func, M);
  1625. } else if (func->getName().startswith("dx.op.rawBufferStore")) {
  1626. ReplaceMinPrecisionRawBufferStore(func, M);
  1627. }
  1628. }
  1629. }
  1630. }
  1631. return true;
  1632. }
  1633. private:
  1634. // Replace RawBufferLoad/Store to BufferLoad/Store for DXIL < 1.2
  1635. void ReplaceRawBufferLoad(Function *F, Module &M);
  1636. void ReplaceRawBufferStore(Function *F, Module &M);
  1637. // Replace RawBufferLoad/Store of min-precision types to have its actual storage size
  1638. void ReplaceMinPrecisionRawBufferLoad(Function *F, Module &M);
  1639. void ReplaceMinPrecisionRawBufferStore(Function *F, Module &M);
  1640. void ReplaceMinPrecisionRawBufferLoadByType(Function *F, Type *FromTy,
  1641. Type *ToTy, OP *Op,
  1642. const DataLayout &DL);
  1643. };
  1644. } // namespace
  1645. void DxilTranslateRawBuffer::ReplaceRawBufferLoad(Function *F,
  1646. Module &M) {
  1647. OP *op = M.GetDxilModule().GetOP();
  1648. Type *RTy = F->getReturnType();
  1649. if (StructType *STy = dyn_cast<StructType>(RTy)) {
  1650. Type *ETy = STy->getElementType(0);
  1651. Function *newFunction = op->GetOpFunc(hlsl::DXIL::OpCode::BufferLoad, ETy);
  1652. for (auto U = F->user_begin(), E = F->user_end(); U != E;) {
  1653. User *user = *(U++);
  1654. if (CallInst *CI = dyn_cast<CallInst>(user)) {
  1655. IRBuilder<> Builder(CI);
  1656. SmallVector<Value *, 4> args;
  1657. args.emplace_back(op->GetI32Const((unsigned)DXIL::OpCode::BufferLoad));
  1658. for (unsigned i = 1; i < 4; ++i) {
  1659. args.emplace_back(CI->getArgOperand(i));
  1660. }
  1661. CallInst *newCall = Builder.CreateCall(newFunction, args);
  1662. CI->replaceAllUsesWith(newCall);
  1663. CI->eraseFromParent();
  1664. } else {
  1665. DXASSERT(false, "function can only be used with call instructions.");
  1666. }
  1667. }
  1668. } else {
  1669. DXASSERT(false, "RawBufferLoad should return struct type.");
  1670. }
  1671. }
  1672. void DxilTranslateRawBuffer::ReplaceRawBufferStore(Function *F,
  1673. Module &M) {
  1674. OP *op = M.GetDxilModule().GetOP();
  1675. Type *RTy = F->getReturnType();
  1676. DXASSERT(RTy->isVoidTy(), "rawBufferStore should return a void type.");
  1677. Type *ETy = F->getFunctionType()->getParamType(4); // value
  1678. Function *newFunction = op->GetOpFunc(hlsl::DXIL::OpCode::BufferStore, ETy);
  1679. for (auto U = F->user_begin(), E = F->user_end(); U != E;) {
  1680. User *user = *(U++);
  1681. if (CallInst *CI = dyn_cast<CallInst>(user)) {
  1682. IRBuilder<> Builder(CI);
  1683. SmallVector<Value *, 4> args;
  1684. args.emplace_back(op->GetI32Const((unsigned)DXIL::OpCode::BufferStore));
  1685. for (unsigned i = 1; i < 9; ++i) {
  1686. args.emplace_back(CI->getArgOperand(i));
  1687. }
  1688. Builder.CreateCall(newFunction, args);
  1689. CI->eraseFromParent();
  1690. }
  1691. else {
  1692. DXASSERT(false, "function can only be used with call instructions.");
  1693. }
  1694. }
  1695. }
  1696. void DxilTranslateRawBuffer::ReplaceMinPrecisionRawBufferLoad(Function *F,
  1697. Module &M) {
  1698. OP *Op = M.GetDxilModule().GetOP();
  1699. Type *RetTy = F->getReturnType();
  1700. if (StructType *STy = dyn_cast<StructType>(RetTy)) {
  1701. Type *EltTy = STy->getElementType(0);
  1702. if (EltTy->isHalfTy()) {
  1703. ReplaceMinPrecisionRawBufferLoadByType(F, Type::getHalfTy(M.getContext()),
  1704. Type::getFloatTy(M.getContext()),
  1705. Op, M.getDataLayout());
  1706. } else if (EltTy == Type::getInt16Ty(M.getContext())) {
  1707. ReplaceMinPrecisionRawBufferLoadByType(
  1708. F, Type::getInt16Ty(M.getContext()), Type::getInt32Ty(M.getContext()),
  1709. Op, M.getDataLayout());
  1710. }
  1711. } else {
  1712. DXASSERT(false, "RawBufferLoad should return struct type.");
  1713. }
  1714. }
  1715. void DxilTranslateRawBuffer::ReplaceMinPrecisionRawBufferStore(Function *F,
  1716. Module &M) {
  1717. Type *RetTy = F->getReturnType();
  1718. DXASSERT(RetTy->isVoidTy(), "rawBufferStore should return a void type.");
  1719. Type *ETy = F->getFunctionType()->getParamType(4); // value
  1720. Type *NewETy;
  1721. if (ETy->isHalfTy()) {
  1722. NewETy = Type::getFloatTy(M.getContext());
  1723. }
  1724. else if (ETy == Type::getInt16Ty(M.getContext())) {
  1725. NewETy = Type::getInt32Ty(M.getContext());
  1726. }
  1727. else {
  1728. return; // not a min precision type
  1729. }
  1730. Function *newFunction = M.GetDxilModule().GetOP()->GetOpFunc(
  1731. DXIL::OpCode::RawBufferStore, NewETy);
  1732. // for each function
  1733. // add argument 4-7 to its upconverted values
  1734. // replace function call
  1735. for (auto FuncUser = F->user_begin(), FuncEnd = F->user_end(); FuncUser != FuncEnd;) {
  1736. CallInst *CI = dyn_cast<CallInst>(*(FuncUser++));
  1737. DXASSERT(CI, "function user must be a call instruction.");
  1738. IRBuilder<> CIBuilder(CI);
  1739. SmallVector<Value *, 9> Args;
  1740. for (unsigned i = 0; i < 4; ++i) {
  1741. Args.emplace_back(CI->getArgOperand(i));
  1742. }
  1743. // values to store should be converted to its higher precision types
  1744. if (ETy->isHalfTy()) {
  1745. for (unsigned i = 4; i < 8; ++i) {
  1746. Value *NewV = CIBuilder.CreateFPExt(CI->getArgOperand(i),
  1747. Type::getFloatTy(M.getContext()));
  1748. Args.emplace_back(NewV);
  1749. }
  1750. }
  1751. else if (ETy == Type::getInt16Ty(M.getContext())) {
  1752. // This case only applies to typed buffer since Store operation of byte
  1753. // address buffer for min precision is handled by implicit conversion on
  1754. // intrinsic call. Since we are extending integer, we have to know if we
  1755. // should sign ext or zero ext. We can do this by iterating checking the
  1756. // size of the element at struct type and comp type at type annotation
  1757. CallInst *handleCI = dyn_cast<CallInst>(CI->getArgOperand(1));
  1758. DXASSERT(handleCI, "otherwise handle was not an argument to buffer store.");
  1759. ConstantInt *resClass = dyn_cast<ConstantInt>(handleCI->getArgOperand(1));
  1760. DXASSERT(resClass && resClass->getSExtValue() ==
  1761. (unsigned)DXIL::ResourceClass::UAV,
  1762. "otherwise buffer store called on non uav kind.");
  1763. ConstantInt *rangeID = dyn_cast<ConstantInt>(handleCI->getArgOperand(2)); // range id or idx?
  1764. DXASSERT(rangeID, "wrong createHandle call.");
  1765. DxilResource dxilRes = M.GetDxilModule().GetUAV(rangeID->getSExtValue());
  1766. StructType *STy = dyn_cast<StructType>(dxilRes.GetRetType());
  1767. DxilStructAnnotation *SAnnot = M.GetDxilModule().GetTypeSystem().GetStructAnnotation(STy);
  1768. ConstantInt *offsetInt = dyn_cast<ConstantInt>(CI->getArgOperand(3));
  1769. unsigned offset = offsetInt->getSExtValue();
  1770. unsigned currentOffset = 0;
  1771. for (DxilStructTypeIterator iter = begin(STy, SAnnot), ItEnd = end(STy, SAnnot); iter != ItEnd; ++iter) {
  1772. std::pair<Type *, DxilFieldAnnotation*> pair = *iter;
  1773. currentOffset += M.getDataLayout().getTypeAllocSize(pair.first);
  1774. if (currentOffset > offset) {
  1775. if (pair.second->GetCompType().IsUIntTy()) {
  1776. for (unsigned i = 4; i < 8; ++i) {
  1777. Value *NewV = CIBuilder.CreateZExt(CI->getArgOperand(i), Type::getInt32Ty(M.getContext()));
  1778. Args.emplace_back(NewV);
  1779. }
  1780. break;
  1781. }
  1782. else if (pair.second->GetCompType().IsIntTy()) {
  1783. for (unsigned i = 4; i < 8; ++i) {
  1784. Value *NewV = CIBuilder.CreateSExt(CI->getArgOperand(i), Type::getInt32Ty(M.getContext()));
  1785. Args.emplace_back(NewV);
  1786. }
  1787. break;
  1788. }
  1789. else {
  1790. DXASSERT(false, "Invalid comp type");
  1791. }
  1792. }
  1793. }
  1794. }
  1795. // mask
  1796. Args.emplace_back(CI->getArgOperand(8));
  1797. // alignment
  1798. Args.emplace_back(M.GetDxilModule().GetOP()->GetI32Const(
  1799. M.getDataLayout().getTypeAllocSize(NewETy)));
  1800. CIBuilder.CreateCall(newFunction, Args);
  1801. CI->eraseFromParent();
  1802. }
  1803. }
  1804. void DxilTranslateRawBuffer::ReplaceMinPrecisionRawBufferLoadByType(
  1805. Function *F, Type *FromTy, Type *ToTy, OP *Op, const DataLayout &DL) {
  1806. Function *newFunction = Op->GetOpFunc(DXIL::OpCode::RawBufferLoad, ToTy);
  1807. for (auto FUser = F->user_begin(), FEnd = F->user_end(); FUser != FEnd;) {
  1808. User *UserCI = *(FUser++);
  1809. if (CallInst *CI = dyn_cast<CallInst>(UserCI)) {
  1810. IRBuilder<> CIBuilder(CI);
  1811. SmallVector<Value *, 5> newFuncArgs;
  1812. // opcode, handle, index, elementOffset, mask
  1813. // Compiler is generating correct element offset even for min precision types
  1814. // So no need to recalculate here
  1815. for (unsigned i = 0; i < 5; ++i) {
  1816. newFuncArgs.emplace_back(CI->getArgOperand(i));
  1817. }
  1818. // new alignment for new type
  1819. newFuncArgs.emplace_back(Op->GetI32Const(DL.getTypeAllocSize(ToTy)));
  1820. CallInst *newCI = CIBuilder.CreateCall(newFunction, newFuncArgs);
  1821. for (auto CIUser = CI->user_begin(), CIEnd = CI->user_end();
  1822. CIUser != CIEnd;) {
  1823. User *UserEV = *(CIUser++);
  1824. if (ExtractValueInst *EV = dyn_cast<ExtractValueInst>(UserEV)) {
  1825. IRBuilder<> EVBuilder(EV);
  1826. ArrayRef<unsigned> Indices = EV->getIndices();
  1827. DXASSERT(Indices.size() == 1, "Otherwise we have wrong extract value.");
  1828. Value *newEV = EVBuilder.CreateExtractValue(newCI, Indices);
  1829. Value *newTruncV;
  1830. if (4 == Indices[0]) { // Don't truncate status
  1831. newTruncV = newEV;
  1832. }
  1833. else if (FromTy->isHalfTy()) {
  1834. newTruncV = EVBuilder.CreateFPTrunc(newEV, FromTy);
  1835. } else if (FromTy->isIntegerTy()) {
  1836. newTruncV = EVBuilder.CreateTrunc(newEV, FromTy);
  1837. } else {
  1838. DXASSERT(false, "unexpected type conversion");
  1839. }
  1840. EV->replaceAllUsesWith(newTruncV);
  1841. EV->eraseFromParent();
  1842. }
  1843. }
  1844. CI->eraseFromParent();
  1845. }
  1846. }
  1847. F->eraseFromParent();
  1848. }
  1849. char DxilTranslateRawBuffer::ID = 0;
  1850. ModulePass *llvm::createDxilTranslateRawBuffer() {
  1851. return new DxilTranslateRawBuffer();
  1852. }
  1853. INITIALIZE_PASS(DxilTranslateRawBuffer, "hlsl-translate-dxil-raw-buffer",
  1854. "Translate raw buffer load", false, false)