WaveSensitivityAnalysis.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // //
  3. // WaveSensitivityAnalysis.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. // This file provides support for doing analysis that are aware of wave //
  9. // intrinsics. //
  10. // //
  11. ///////////////////////////////////////////////////////////////////////////////
  12. #include "dxc/HLSL/DxilValidation.h"
  13. #include "dxc/HLSL/DxilGenerationPass.h"
  14. #include "dxc/HLSL/DxilOperations.h"
  15. #include "dxc/HLSL/DxilModule.h"
  16. #include "dxc/HLSL/DxilShaderModel.h"
  17. #include "dxc/HLSL/DxilContainer.h"
  18. #include "dxc/Support/Global.h"
  19. #include "dxc/HLSL/HLOperations.h"
  20. #include "dxc/HLSL/HLModule.h"
  21. #include "dxc/HLSL/DxilInstructions.h"
  22. #include "llvm/ADT/ArrayRef.h"
  23. #include "llvm/IR/LLVMContext.h"
  24. #include "llvm/IR/Module.h"
  25. #include "llvm/IR/Type.h"
  26. #include "llvm/IR/Instructions.h"
  27. #include "llvm/IR/InstIterator.h"
  28. #include "llvm/IR/Constants.h"
  29. #include "llvm/IR/DiagnosticInfo.h"
  30. #include "llvm/IR/DiagnosticPrinter.h"
  31. #include "llvm/ADT/BitVector.h"
  32. #ifdef _WIN32
  33. #include <winerror.h>
  34. #endif
  35. #include "llvm/Support/raw_ostream.h"
  36. #include <unordered_set>
  37. using namespace llvm;
  38. using namespace std;
  39. namespace hlsl {
  40. class WaveSensitivityAnalyzer : public WaveSensitivityAnalysis {
  41. private:
  42. enum WaveSensitivity {
  43. KnownSensitive,
  44. KnownNotSensitive,
  45. Unknown
  46. };
  47. map<Instruction *, WaveSensitivity> InstState;
  48. map<BasicBlock *, WaveSensitivity> BBState;
  49. std::vector<Instruction *> InstWorkList;
  50. std::vector<BasicBlock *> BBWorkList;
  51. bool CheckBBState(BasicBlock *BB, WaveSensitivity WS);
  52. WaveSensitivity GetInstState(Instruction *I);
  53. void UpdateBlock(BasicBlock *BB, WaveSensitivity WS);
  54. void UpdateInst(Instruction *I, WaveSensitivity WS);
  55. void VisitInst(Instruction *I);
  56. public:
  57. void Analyze(Function *F);
  58. bool IsWaveSensitive(Instruction *op);
  59. };
  60. WaveSensitivityAnalysis* WaveSensitivityAnalysis::create() {
  61. return new WaveSensitivityAnalyzer();
  62. }
  63. void WaveSensitivityAnalyzer::Analyze(Function *F) {
  64. UpdateBlock(&F->getEntryBlock(), KnownNotSensitive);
  65. while (!InstWorkList.empty() || !BBWorkList.empty()) {
  66. // Process the instruction work list.
  67. while (!InstWorkList.empty()) {
  68. Instruction *I = InstWorkList.back();
  69. InstWorkList.pop_back();
  70. // "I" got into the work list because it made a transition.
  71. for (User *U : I->users()) {
  72. Instruction *UI = cast<Instruction>(U);
  73. VisitInst(UI);
  74. }
  75. }
  76. // Process the basic block work list.
  77. while (!BBWorkList.empty()) {
  78. BasicBlock *BB = BBWorkList.back();
  79. BBWorkList.pop_back();
  80. // Notify all instructions in this basic block that they need to
  81. // be reevaluated (eg, a block previously though to be insensitive
  82. // is now sensitive).
  83. for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I)
  84. VisitInst(I);
  85. }
  86. }
  87. }
  88. bool WaveSensitivityAnalyzer::CheckBBState(BasicBlock *BB, WaveSensitivity WS) {
  89. auto c = BBState.find(BB);
  90. if (c == BBState.end()) {
  91. return WS == Unknown;
  92. }
  93. else {
  94. return (*c).second == WS;
  95. }
  96. }
  97. WaveSensitivityAnalyzer::WaveSensitivity
  98. WaveSensitivityAnalyzer::GetInstState(Instruction *I) {
  99. auto c = InstState.find(I);
  100. if (c == InstState.end())
  101. return Unknown;
  102. return (*c).second;
  103. }
  104. void WaveSensitivityAnalyzer::UpdateBlock(BasicBlock *BB, WaveSensitivity WS) {
  105. auto c = BBState.find(BB);
  106. // Do not update if an entry is already found and it hasn't changed or
  107. // has already been marked as wave sensitive (an insensitive term might
  108. // try to mark it as such, but this effectively implements the 'any pred'
  109. // rule).
  110. if (c != BBState.end() && ((*c).second == WS || (*c).second == KnownSensitive))
  111. return;
  112. BBState[BB] = WS;
  113. BBWorkList.push_back(BB);
  114. }
  115. void WaveSensitivityAnalyzer::UpdateInst(Instruction *I, WaveSensitivity WS) {
  116. auto c = InstState.find(I);
  117. if (c == InstState.end() || (*c).second != WS) {
  118. InstState[I] = WS;
  119. InstWorkList.push_back(I);
  120. if (TerminatorInst * TI = dyn_cast<TerminatorInst>(I)) {
  121. for (unsigned i = 0; i < TI->getNumSuccessors(); ++i) {
  122. BasicBlock *BB = TI->getSuccessor(i);
  123. UpdateBlock(BB, WS);
  124. }
  125. }
  126. }
  127. }
  128. void WaveSensitivityAnalyzer::VisitInst(Instruction *I) {
  129. unsigned firstArg = 0;
  130. if (CallInst *CI = dyn_cast<CallInst>(I)) {
  131. if (OP::IsDxilOpFuncCallInst(CI)) {
  132. firstArg = 1;
  133. OP::OpCode opcode = OP::GetDxilOpFuncCallInst(CI);
  134. if (OP::IsDxilOpWave(opcode)) {
  135. UpdateInst(I, KnownSensitive);
  136. return;
  137. }
  138. }
  139. }
  140. if (CheckBBState(I->getParent(), KnownSensitive)) {
  141. UpdateInst(I, KnownSensitive);
  142. return;
  143. }
  144. bool allKnownNotSensitive = true;
  145. for (unsigned i = firstArg; i < I->getNumOperands(); ++i) {
  146. Value *V = I->getOperand(i);
  147. if (Instruction *IArg = dyn_cast<Instruction>(V)) {
  148. WaveSensitivity WS = GetInstState(IArg);
  149. if (WS == KnownSensitive) {
  150. UpdateInst(I, KnownSensitive);
  151. return;
  152. }
  153. if (WS == Unknown) {
  154. allKnownNotSensitive = false;
  155. return;
  156. }
  157. }
  158. }
  159. if (allKnownNotSensitive) {
  160. UpdateInst(I, KnownNotSensitive);
  161. }
  162. }
  163. bool WaveSensitivityAnalyzer::IsWaveSensitive(Instruction *op) {
  164. auto c = InstState.find(op);
  165. DXASSERT(c != InstState.end(), "else analysis didn't complete");
  166. DXASSERT((*c).second != Unknown, "else analysis is missing a case");
  167. return (*c).second == KnownSensitive;
  168. }
  169. } // namespace hlsl