DxilLegalizeSampleOffsetPass.cpp 7.3 KB

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