DxilPromoteResourcePasses.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // //
  3. // DxilPromoteResourcePasses.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. ///////////////////////////////////////////////////////////////////////////////
  9. #include "dxc/DXIL/DxilUtil.h"
  10. #include "dxc/HLSL/DxilGenerationPass.h"
  11. #include "dxc/HLSL/HLModule.h"
  12. #include "llvm/Pass.h"
  13. #include "llvm/Analysis/AssumptionCache.h"
  14. #include "llvm/IR/Dominators.h"
  15. #include "llvm/IR/Function.h"
  16. #include "llvm/IR/Instruction.h"
  17. #include "llvm/IR/Instructions.h"
  18. #include "llvm/IR/LLVMContext.h"
  19. #include "llvm/IR/Module.h"
  20. #include "llvm/IR/Operator.h"
  21. #include "llvm/Transforms/Utils/PromoteMemToReg.h"
  22. #include "llvm/Transforms/Utils/SSAUpdater.h"
  23. #include <unordered_set>
  24. #include <vector>
  25. using namespace llvm;
  26. using namespace hlsl;
  27. // Legalize resource use.
  28. // Map local or static global resource to global resource.
  29. // Require inline for static global resource.
  30. namespace {
  31. static const StringRef kStaticResourceLibErrorMsg = "static global resource use is disallowed in library exports.";
  32. class DxilPromoteStaticResources : public ModulePass {
  33. public:
  34. static char ID; // Pass identification, replacement for typeid
  35. explicit DxilPromoteStaticResources()
  36. : ModulePass(ID) {}
  37. const char *getPassName() const override {
  38. return "DXIL Legalize Static Resource Use";
  39. }
  40. bool runOnModule(Module &M) override {
  41. // Promote static global variables.
  42. return PromoteStaticGlobalResources(M);
  43. }
  44. private:
  45. bool PromoteStaticGlobalResources(Module &M);
  46. };
  47. char DxilPromoteStaticResources::ID = 0;
  48. class DxilPromoteLocalResources : public FunctionPass {
  49. void getAnalysisUsage(AnalysisUsage &AU) const override;
  50. public:
  51. static char ID; // Pass identification, replacement for typeid
  52. explicit DxilPromoteLocalResources()
  53. : FunctionPass(ID) {}
  54. const char *getPassName() const override {
  55. return "DXIL Legalize Resource Use";
  56. }
  57. bool runOnFunction(Function &F) override {
  58. // Promote local resource first.
  59. return PromoteLocalResource(F);
  60. }
  61. private:
  62. bool PromoteLocalResource(Function &F);
  63. };
  64. char DxilPromoteLocalResources::ID = 0;
  65. }
  66. void DxilPromoteLocalResources::getAnalysisUsage(AnalysisUsage &AU) const {
  67. AU.addRequired<AssumptionCacheTracker>();
  68. AU.addRequired<DominatorTreeWrapperPass>();
  69. AU.setPreservesAll();
  70. }
  71. bool DxilPromoteLocalResources::PromoteLocalResource(Function &F) {
  72. bool bModified = false;
  73. std::vector<AllocaInst *> Allocas;
  74. DominatorTree *DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
  75. AssumptionCache &AC =
  76. getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F);
  77. BasicBlock &BB = F.getEntryBlock();
  78. unsigned allocaSize = 0;
  79. while (1) {
  80. Allocas.clear();
  81. // Find allocas that are safe to promote, by looking at all instructions in
  82. // the entry node
  83. for (BasicBlock::iterator I = BB.begin(), E = --BB.end(); I != E; ++I)
  84. if (AllocaInst *AI = dyn_cast<AllocaInst>(I)) { // Is it an alloca?
  85. if (dxilutil::IsHLSLObjectType(dxilutil::GetArrayEltTy(AI->getAllocatedType()))) {
  86. if (isAllocaPromotable(AI))
  87. Allocas.push_back(AI);
  88. }
  89. }
  90. if (Allocas.empty())
  91. break;
  92. // No update.
  93. // Report error and break.
  94. if (allocaSize == Allocas.size()) {
  95. F.getContext().emitError(dxilutil::kResourceMapErrorMsg);
  96. break;
  97. }
  98. allocaSize = Allocas.size();
  99. PromoteMemToReg(Allocas, *DT, nullptr, &AC);
  100. bModified = true;
  101. }
  102. return bModified;
  103. }
  104. FunctionPass *llvm::createDxilPromoteLocalResources() {
  105. return new DxilPromoteLocalResources();
  106. }
  107. INITIALIZE_PASS_BEGIN(DxilPromoteLocalResources,
  108. "hlsl-dxil-promote-local-resources",
  109. "DXIL promote local resource use", false, true)
  110. INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
  111. INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
  112. INITIALIZE_PASS_END(DxilPromoteLocalResources,
  113. "hlsl-dxil-promote-local-resources",
  114. "DXIL promote local resource use", false, true)
  115. bool DxilPromoteStaticResources::PromoteStaticGlobalResources(
  116. Module &M) {
  117. if (M.GetOrCreateHLModule().GetShaderModel()->IsLib()) {
  118. // Read/write to global static resource is disallowed for libraries:
  119. // Resource use needs to be resolved to a single real global resource,
  120. // but it may not be possible since any external function call may re-enter
  121. // at any other library export, which could modify the global static
  122. // between write and read.
  123. // While it could work for certain cases, describing the boundary at
  124. // the HLSL level is difficult, so at this point it's better to disallow.
  125. // example of what could work:
  126. // After inlining, exported functions must have writes to static globals
  127. // before reads, and must not have any external function calls between
  128. // writes and subsequent reads, such that the static global may be
  129. // optimized away for the exported function.
  130. for (auto &GV : M.globals()) {
  131. if (GV.getLinkage() == GlobalVariable::LinkageTypes::InternalLinkage &&
  132. dxilutil::IsHLSLObjectType(dxilutil::GetArrayEltTy(GV.getType()))) {
  133. if (!GV.user_empty()) {
  134. if (Instruction *I = dyn_cast<Instruction>(*GV.user_begin())) {
  135. dxilutil::EmitErrorOnInstruction(I, kStaticResourceLibErrorMsg);
  136. break;
  137. }
  138. }
  139. }
  140. }
  141. return false;
  142. }
  143. bool bModified = false;
  144. std::set<GlobalVariable *> staticResources;
  145. for (auto &GV : M.globals()) {
  146. if (GV.getLinkage() == GlobalVariable::LinkageTypes::InternalLinkage &&
  147. dxilutil::IsHLSLObjectType(dxilutil::GetArrayEltTy(GV.getType()))) {
  148. staticResources.insert(&GV);
  149. }
  150. }
  151. SSAUpdater SSA;
  152. SmallVector<Instruction *, 4> Insts;
  153. // Make sure every resource load has mapped to global variable.
  154. while (!staticResources.empty()) {
  155. bool bUpdated = false;
  156. for (auto it = staticResources.begin(); it != staticResources.end();) {
  157. GlobalVariable *GV = *(it++);
  158. // Build list of instructions to promote.
  159. for (User *U : GV->users()) {
  160. if (isa<LoadInst>(U) || isa<StoreInst>(U)) {
  161. Insts.emplace_back(cast<Instruction>(U));
  162. } else if (GEPOperator *GEP = dyn_cast<GEPOperator>(U)) {
  163. for (User *gepU : GEP->users()) {
  164. DXASSERT_NOMSG(isa<LoadInst>(gepU) || isa<StoreInst>(gepU));
  165. if (isa<LoadInst>(gepU) || isa<StoreInst>(gepU))
  166. Insts.emplace_back(cast<Instruction>(gepU));
  167. }
  168. } else {
  169. DXASSERT(false, "Unhandled user of resource static global");
  170. }
  171. }
  172. LoadAndStorePromoter(Insts, SSA).run(Insts);
  173. GV->removeDeadConstantUsers();
  174. if (GV->user_empty()) {
  175. bUpdated = true;
  176. staticResources.erase(GV);
  177. }
  178. Insts.clear();
  179. }
  180. if (!bUpdated) {
  181. M.getContext().emitError(dxilutil::kResourceMapErrorMsg);
  182. break;
  183. }
  184. bModified = true;
  185. }
  186. return bModified;
  187. }
  188. ModulePass *llvm::createDxilPromoteStaticResources() {
  189. return new DxilPromoteStaticResources();
  190. }
  191. INITIALIZE_PASS(DxilPromoteStaticResources,
  192. "hlsl-dxil-promote-static-resources",
  193. "DXIL promote static resource use", false, false)