DxilLegalizeSampleOffsetPass.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // //
  3. // DxilSignature.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. // DxilLegalizeSampleOffsetPass implementation. //
  9. // //
  10. ///////////////////////////////////////////////////////////////////////////////
  11. #include "dxc/HLSL/DxilGenerationPass.h"
  12. #include "llvm/Analysis/DxilValueCache.h"
  13. #include "dxc/DXIL/DxilModule.h"
  14. #include "dxc/DXIL/DxilOperations.h"
  15. #include "dxc/DXIL/DxilUtil.h"
  16. #include "llvm/Analysis/InstructionSimplify.h"
  17. #include "llvm/Analysis/LoopInfo.h"
  18. #include "llvm/IR/Constants.h"
  19. #include "llvm/IR/Dominators.h"
  20. #include "llvm/IR/Instructions.h"
  21. #include "llvm/IR/LLVMContext.h"
  22. #include "llvm/IR/LegacyPassManager.h"
  23. #include "llvm/IR/PassManager.h"
  24. #include "llvm/Pass.h"
  25. #include "llvm/Transforms/Scalar.h"
  26. #include <unordered_set>
  27. using std::vector;
  28. using std::unique_ptr;
  29. using namespace llvm;
  30. using namespace hlsl;
  31. ///////////////////////////////////////////////////////////////////////////////
  32. // Legalize Sample offset.
  33. namespace {
  34. // When optimizations are disabled, try to legalize sample offset.
  35. class DxilLegalizeSampleOffsetPass : public FunctionPass {
  36. public:
  37. static char ID; // Pass identification, replacement for typeid
  38. explicit DxilLegalizeSampleOffsetPass() : FunctionPass(ID) {}
  39. const char *getPassName() const override {
  40. return "DXIL legalize sample offset";
  41. }
  42. void getAnalysisUsage(AnalysisUsage &AU) const {
  43. AU.addRequired<DxilValueCache>();
  44. AU.setPreservesAll();
  45. }
  46. bool runOnFunction(Function &F) override {
  47. DxilModule &DM = F.getParent()->GetOrCreateDxilModule();
  48. hlsl::OP *hlslOP = DM.GetOP();
  49. std::vector<Instruction *> illegalOffsets;
  50. CollectIllegalOffsets(illegalOffsets, F, hlslOP);
  51. if (illegalOffsets.empty())
  52. return false;
  53. // Loop unroll if has offset inside loop.
  54. TryUnrollLoop(illegalOffsets, F);
  55. // Collect offset again after mem2reg.
  56. std::vector<Instruction *> ssaIllegalOffsets;
  57. CollectIllegalOffsets(ssaIllegalOffsets, F, hlslOP);
  58. // Run simple optimization to legalize offsets.
  59. LegalizeOffsets(ssaIllegalOffsets);
  60. FinalCheck(illegalOffsets, F, hlslOP);
  61. return true;
  62. }
  63. private:
  64. void TryUnrollLoop(std::vector<Instruction *> &illegalOffsets, Function &F);
  65. void CollectIllegalOffsets(std::vector<Instruction *> &illegalOffsets,
  66. Function &F, hlsl::OP *hlslOP);
  67. void CollectIllegalOffsets(std::vector<Instruction *> &illegalOffsets,
  68. Function &F, DXIL::OpCode opcode,
  69. hlsl::OP *hlslOP);
  70. void LegalizeOffsets(const std::vector<Instruction *> &illegalOffsets);
  71. void FinalCheck(std::vector<Instruction *> &illegalOffsets, Function &F,
  72. hlsl::OP *hlslOP);
  73. };
  74. char DxilLegalizeSampleOffsetPass::ID = 0;
  75. bool HasIllegalOffsetInLoop(std::vector<Instruction *> &illegalOffsets,
  76. Function &F) {
  77. DominatorTreeAnalysis DTA;
  78. DominatorTree DT = DTA.run(F);
  79. LoopInfo LI;
  80. LI.Analyze(DT);
  81. bool findOffset = false;
  82. for (Instruction *I : illegalOffsets) {
  83. BasicBlock *BB = I->getParent();
  84. if (LI.getLoopFor(BB)) {
  85. findOffset = true;
  86. break;
  87. }
  88. }
  89. return findOffset;
  90. }
  91. void CollectIllegalOffset(CallInst *CI,
  92. std::vector<Instruction *> &illegalOffsets) {
  93. Value *offset0 =
  94. CI->getArgOperand(DXIL::OperandIndex::kTextureSampleOffset0OpIdx);
  95. // No offset.
  96. if (isa<UndefValue>(offset0))
  97. return;
  98. for (unsigned i = DXIL::OperandIndex::kTextureSampleOffset0OpIdx;
  99. i <= DXIL::OperandIndex::kTextureSampleOffset2OpIdx; i++) {
  100. Value *offset = CI->getArgOperand(i);
  101. if (Instruction *I = dyn_cast<Instruction>(offset))
  102. illegalOffsets.emplace_back(I);
  103. }
  104. }
  105. }
  106. void DxilLegalizeSampleOffsetPass::FinalCheck(
  107. std::vector<Instruction *> &illegalOffsets, Function &F, hlsl::OP *hlslOP) {
  108. // Collect offset to make sure no illegal offsets.
  109. std::vector<Instruction *> finalIllegalOffsets;
  110. CollectIllegalOffsets(finalIllegalOffsets, F, hlslOP);
  111. if (!finalIllegalOffsets.empty()) {
  112. const StringRef kIllegalOffsetError =
  113. "Offsets for Sample* must be immediated value. "
  114. "Consider unrolling the loop manually and use -O3, "
  115. "it may help in some cases.\n";
  116. std::string errorMsg;
  117. raw_string_ostream errorStr(errorMsg);
  118. for (Instruction *offset : finalIllegalOffsets)
  119. dxilutil::EmitErrorOnInstruction(offset, kIllegalOffsetError);
  120. }
  121. }
  122. void DxilLegalizeSampleOffsetPass::TryUnrollLoop(
  123. std::vector<Instruction *> &illegalOffsets, Function &F) {
  124. legacy::FunctionPassManager PM(F.getParent());
  125. // Scalarize aggregates as mem2reg only applies on scalars.
  126. PM.add(createSROAPass());
  127. // Always need mem2reg for simplify illegal offsets.
  128. PM.add(createPromoteMemoryToRegisterPass());
  129. bool UnrollLoop = HasIllegalOffsetInLoop(illegalOffsets, F);
  130. if (UnrollLoop) {
  131. PM.add(createCFGSimplificationPass());
  132. PM.add(createLCSSAPass());
  133. PM.add(createLoopSimplifyPass());
  134. PM.add(createLoopRotatePass());
  135. PM.add(createLoopUnrollPass(-2, -1, 0, 0));
  136. }
  137. PM.run(F);
  138. if (UnrollLoop) {
  139. DxilValueCache *DVC = &getAnalysis<DxilValueCache>();
  140. DVC->ResetUnknowns();
  141. }
  142. }
  143. void DxilLegalizeSampleOffsetPass::CollectIllegalOffsets(
  144. std::vector<Instruction *> &illegalOffsets, Function &CurF,
  145. hlsl::OP *hlslOP) {
  146. CollectIllegalOffsets(illegalOffsets, CurF, DXIL::OpCode::Sample, hlslOP);
  147. CollectIllegalOffsets(illegalOffsets, CurF, DXIL::OpCode::SampleBias, hlslOP);
  148. CollectIllegalOffsets(illegalOffsets, CurF, DXIL::OpCode::SampleCmp, hlslOP);
  149. CollectIllegalOffsets(illegalOffsets, CurF, DXIL::OpCode::SampleCmpLevelZero,
  150. hlslOP);
  151. CollectIllegalOffsets(illegalOffsets, CurF, DXIL::OpCode::SampleGrad, hlslOP);
  152. CollectIllegalOffsets(illegalOffsets, CurF, DXIL::OpCode::SampleLevel,
  153. hlslOP);
  154. }
  155. void DxilLegalizeSampleOffsetPass::CollectIllegalOffsets(
  156. std::vector<Instruction *> &illegalOffsets, Function &CurF,
  157. DXIL::OpCode opcode, hlsl::OP *hlslOP) {
  158. auto &intrFuncList = hlslOP->GetOpFuncList(opcode);
  159. for (auto it : intrFuncList) {
  160. Function *intrFunc = it.second;
  161. if (!intrFunc)
  162. continue;
  163. for (User *U : intrFunc->users()) {
  164. CallInst *CI = cast<CallInst>(U);
  165. // Skip inst not in current function.
  166. if (CI->getParent()->getParent() != &CurF)
  167. continue;
  168. CollectIllegalOffset(CI, illegalOffsets);
  169. }
  170. }
  171. }
  172. void DxilLegalizeSampleOffsetPass::LegalizeOffsets(
  173. const std::vector<Instruction *> &illegalOffsets) {
  174. if (illegalOffsets.size()) {
  175. DxilValueCache *DVC = &getAnalysis<DxilValueCache>();
  176. for (Instruction *I : illegalOffsets) {
  177. if (Value *V = DVC->GetValue(I)) {
  178. I->replaceAllUsesWith(V);
  179. }
  180. }
  181. }
  182. }
  183. FunctionPass *llvm::createDxilLegalizeSampleOffsetPass() {
  184. return new DxilLegalizeSampleOffsetPass();
  185. }
  186. INITIALIZE_PASS_BEGIN(DxilLegalizeSampleOffsetPass, "dxil-legalize-sample-offset",
  187. "DXIL legalize sample offset", false, false)
  188. INITIALIZE_PASS_DEPENDENCY(DxilValueCache)
  189. INITIALIZE_PASS_END(DxilLegalizeSampleOffsetPass, "dxil-legalize-sample-offset",
  190. "DXIL legalize sample offset", false, false)