dead_insert_elim_pass.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. // Copyright (c) 2018 The Khronos Group Inc.
  2. // Copyright (c) 2018 Valve Corporation
  3. // Copyright (c) 2018 LunarG Inc.
  4. //
  5. // Licensed under the Apache License, Version 2.0 (the "License");
  6. // you may not use this file except in compliance with the License.
  7. // You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing, software
  12. // distributed under the License is distributed on an "AS IS" BASIS,
  13. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. // See the License for the specific language governing permissions and
  15. // limitations under the License.
  16. #include "source/opt/dead_insert_elim_pass.h"
  17. #include "source/opt/composite.h"
  18. #include "source/opt/ir_context.h"
  19. #include "source/opt/iterator.h"
  20. #include "spirv/1.2/GLSL.std.450.h"
  21. namespace spvtools {
  22. namespace opt {
  23. namespace {
  24. const uint32_t kTypeVectorCountInIdx = 1;
  25. const uint32_t kTypeMatrixCountInIdx = 1;
  26. const uint32_t kTypeArrayLengthIdInIdx = 1;
  27. const uint32_t kTypeIntWidthInIdx = 0;
  28. const uint32_t kConstantValueInIdx = 0;
  29. const uint32_t kInsertObjectIdInIdx = 0;
  30. const uint32_t kInsertCompositeIdInIdx = 1;
  31. } // anonymous namespace
  32. uint32_t DeadInsertElimPass::NumComponents(Instruction* typeInst) {
  33. switch (typeInst->opcode()) {
  34. case SpvOpTypeVector: {
  35. return typeInst->GetSingleWordInOperand(kTypeVectorCountInIdx);
  36. } break;
  37. case SpvOpTypeMatrix: {
  38. return typeInst->GetSingleWordInOperand(kTypeMatrixCountInIdx);
  39. } break;
  40. case SpvOpTypeArray: {
  41. uint32_t lenId =
  42. typeInst->GetSingleWordInOperand(kTypeArrayLengthIdInIdx);
  43. Instruction* lenInst = get_def_use_mgr()->GetDef(lenId);
  44. if (lenInst->opcode() != SpvOpConstant) return 0;
  45. uint32_t lenTypeId = lenInst->type_id();
  46. Instruction* lenTypeInst = get_def_use_mgr()->GetDef(lenTypeId);
  47. // TODO(greg-lunarg): Support non-32-bit array length
  48. if (lenTypeInst->GetSingleWordInOperand(kTypeIntWidthInIdx) != 32)
  49. return 0;
  50. return lenInst->GetSingleWordInOperand(kConstantValueInIdx);
  51. } break;
  52. case SpvOpTypeStruct: {
  53. return typeInst->NumInOperands();
  54. } break;
  55. default: { return 0; } break;
  56. }
  57. }
  58. void DeadInsertElimPass::MarkInsertChain(
  59. Instruction* insertChain, std::vector<uint32_t>* pExtIndices,
  60. uint32_t extOffset, std::unordered_set<uint32_t>* visited_phis) {
  61. // Not currently optimizing array inserts.
  62. Instruction* typeInst = get_def_use_mgr()->GetDef(insertChain->type_id());
  63. if (typeInst->opcode() == SpvOpTypeArray) return;
  64. // Insert chains are only composed of inserts and phis
  65. if (insertChain->opcode() != SpvOpCompositeInsert &&
  66. insertChain->opcode() != SpvOpPhi)
  67. return;
  68. // If extract indices are empty, mark all subcomponents if type
  69. // is constant length.
  70. if (pExtIndices == nullptr) {
  71. uint32_t cnum = NumComponents(typeInst);
  72. if (cnum > 0) {
  73. std::vector<uint32_t> extIndices;
  74. for (uint32_t i = 0; i < cnum; i++) {
  75. extIndices.clear();
  76. extIndices.push_back(i);
  77. std::unordered_set<uint32_t> sub_visited_phis;
  78. MarkInsertChain(insertChain, &extIndices, 0, &sub_visited_phis);
  79. }
  80. return;
  81. }
  82. }
  83. Instruction* insInst = insertChain;
  84. while (insInst->opcode() == SpvOpCompositeInsert) {
  85. // If no extract indices, mark insert and inserted object (which might
  86. // also be an insert chain) and continue up the chain though the input
  87. // composite.
  88. //
  89. // Note: We mark inserted objects in this function (rather than in
  90. // EliminateDeadInsertsOnePass) because in some cases, we can do it
  91. // more accurately here.
  92. if (pExtIndices == nullptr) {
  93. liveInserts_.insert(insInst->result_id());
  94. uint32_t objId = insInst->GetSingleWordInOperand(kInsertObjectIdInIdx);
  95. std::unordered_set<uint32_t> obj_visited_phis;
  96. MarkInsertChain(get_def_use_mgr()->GetDef(objId), nullptr, 0,
  97. &obj_visited_phis);
  98. // If extract indices match insert, we are done. Mark insert and
  99. // inserted object.
  100. } else if (ExtInsMatch(*pExtIndices, insInst, extOffset)) {
  101. liveInserts_.insert(insInst->result_id());
  102. uint32_t objId = insInst->GetSingleWordInOperand(kInsertObjectIdInIdx);
  103. std::unordered_set<uint32_t> obj_visited_phis;
  104. MarkInsertChain(get_def_use_mgr()->GetDef(objId), nullptr, 0,
  105. &obj_visited_phis);
  106. break;
  107. // If non-matching intersection, mark insert
  108. } else if (ExtInsConflict(*pExtIndices, insInst, extOffset)) {
  109. liveInserts_.insert(insInst->result_id());
  110. // If more extract indices than insert, we are done. Use remaining
  111. // extract indices to mark inserted object.
  112. uint32_t numInsertIndices = insInst->NumInOperands() - 2;
  113. if (pExtIndices->size() - extOffset > numInsertIndices) {
  114. uint32_t objId = insInst->GetSingleWordInOperand(kInsertObjectIdInIdx);
  115. std::unordered_set<uint32_t> obj_visited_phis;
  116. MarkInsertChain(get_def_use_mgr()->GetDef(objId), pExtIndices,
  117. extOffset + numInsertIndices, &obj_visited_phis);
  118. break;
  119. // If fewer extract indices than insert, also mark inserted object and
  120. // continue up chain.
  121. } else {
  122. uint32_t objId = insInst->GetSingleWordInOperand(kInsertObjectIdInIdx);
  123. std::unordered_set<uint32_t> obj_visited_phis;
  124. MarkInsertChain(get_def_use_mgr()->GetDef(objId), nullptr, 0,
  125. &obj_visited_phis);
  126. }
  127. }
  128. // Get next insert in chain
  129. const uint32_t compId =
  130. insInst->GetSingleWordInOperand(kInsertCompositeIdInIdx);
  131. insInst = get_def_use_mgr()->GetDef(compId);
  132. }
  133. // If insert chain ended with phi, do recursive call on each operand
  134. if (insInst->opcode() != SpvOpPhi) return;
  135. // Mark phi visited to prevent potential infinite loop. If phi is already
  136. // visited, return to avoid infinite loop.
  137. if (visited_phis->count(insInst->result_id()) != 0) return;
  138. visited_phis->insert(insInst->result_id());
  139. // Phis may have duplicate inputs values for different edges, prune incoming
  140. // ids lists before recursing.
  141. std::vector<uint32_t> ids;
  142. for (uint32_t i = 0; i < insInst->NumInOperands(); i += 2) {
  143. ids.push_back(insInst->GetSingleWordInOperand(i));
  144. }
  145. std::sort(ids.begin(), ids.end());
  146. auto new_end = std::unique(ids.begin(), ids.end());
  147. for (auto id_iter = ids.begin(); id_iter != new_end; ++id_iter) {
  148. Instruction* pi = get_def_use_mgr()->GetDef(*id_iter);
  149. MarkInsertChain(pi, pExtIndices, extOffset, visited_phis);
  150. }
  151. }
  152. bool DeadInsertElimPass::EliminateDeadInserts(Function* func) {
  153. bool modified = false;
  154. bool lastmodified = true;
  155. // Each pass can delete dead instructions, thus potentially revealing
  156. // new dead insertions ie insertions with no uses.
  157. while (lastmodified) {
  158. lastmodified = EliminateDeadInsertsOnePass(func);
  159. modified |= lastmodified;
  160. }
  161. return modified;
  162. }
  163. bool DeadInsertElimPass::EliminateDeadInsertsOnePass(Function* func) {
  164. bool modified = false;
  165. liveInserts_.clear();
  166. visitedPhis_.clear();
  167. // Mark all live inserts
  168. for (auto bi = func->begin(); bi != func->end(); ++bi) {
  169. for (auto ii = bi->begin(); ii != bi->end(); ++ii) {
  170. // Only process Inserts and composite Phis
  171. SpvOp op = ii->opcode();
  172. Instruction* typeInst = get_def_use_mgr()->GetDef(ii->type_id());
  173. if (op != SpvOpCompositeInsert &&
  174. (op != SpvOpPhi || !spvOpcodeIsComposite(typeInst->opcode())))
  175. continue;
  176. // The marking algorithm can be expensive for large arrays and the
  177. // efficacy of eliminating dead inserts into arrays is questionable.
  178. // Skip optimizing array inserts for now. Just mark them live.
  179. // TODO(greg-lunarg): Eliminate dead array inserts
  180. if (op == SpvOpCompositeInsert) {
  181. if (typeInst->opcode() == SpvOpTypeArray) {
  182. liveInserts_.insert(ii->result_id());
  183. continue;
  184. }
  185. }
  186. const uint32_t id = ii->result_id();
  187. get_def_use_mgr()->ForEachUser(id, [&ii, this](Instruction* user) {
  188. switch (user->opcode()) {
  189. case SpvOpCompositeInsert:
  190. case SpvOpPhi:
  191. // Use by insert or phi does not initiate marking
  192. break;
  193. case SpvOpCompositeExtract: {
  194. // Capture extract indices
  195. std::vector<uint32_t> extIndices;
  196. uint32_t icnt = 0;
  197. user->ForEachInOperand([&icnt, &extIndices](const uint32_t* idp) {
  198. if (icnt > 0) extIndices.push_back(*idp);
  199. ++icnt;
  200. });
  201. // Mark all inserts in chain that intersect with extract
  202. std::unordered_set<uint32_t> visited_phis;
  203. MarkInsertChain(&*ii, &extIndices, 0, &visited_phis);
  204. } break;
  205. default: {
  206. // Mark inserts in chain for all components
  207. MarkInsertChain(&*ii, nullptr, 0, nullptr);
  208. } break;
  209. }
  210. });
  211. }
  212. }
  213. // Find and disconnect dead inserts
  214. std::vector<Instruction*> dead_instructions;
  215. for (auto bi = func->begin(); bi != func->end(); ++bi) {
  216. for (auto ii = bi->begin(); ii != bi->end(); ++ii) {
  217. if (ii->opcode() != SpvOpCompositeInsert) continue;
  218. const uint32_t id = ii->result_id();
  219. if (liveInserts_.find(id) != liveInserts_.end()) continue;
  220. const uint32_t replId =
  221. ii->GetSingleWordInOperand(kInsertCompositeIdInIdx);
  222. (void)context()->ReplaceAllUsesWith(id, replId);
  223. dead_instructions.push_back(&*ii);
  224. modified = true;
  225. }
  226. }
  227. // DCE dead inserts
  228. while (!dead_instructions.empty()) {
  229. Instruction* inst = dead_instructions.back();
  230. dead_instructions.pop_back();
  231. DCEInst(inst, [&dead_instructions](Instruction* other_inst) {
  232. auto i = std::find(dead_instructions.begin(), dead_instructions.end(),
  233. other_inst);
  234. if (i != dead_instructions.end()) {
  235. dead_instructions.erase(i);
  236. }
  237. });
  238. }
  239. return modified;
  240. }
  241. Pass::Status DeadInsertElimPass::Process() {
  242. // Process all entry point functions.
  243. ProcessFunction pfn = [this](Function* fp) {
  244. return EliminateDeadInserts(fp);
  245. };
  246. bool modified = context()->ProcessEntryPointCallTree(pfn);
  247. return modified ? Status::SuccessWithChange : Status::SuccessWithoutChange;
  248. }
  249. } // namespace opt
  250. } // namespace spvtools