WaveSensitivityAnalysis.cpp 5.9 KB

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