dead_insert_elim_pass.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. constexpr uint32_t kTypeVectorCountInIdx = 1;
  25. constexpr uint32_t kTypeMatrixCountInIdx = 1;
  26. constexpr uint32_t kTypeArrayLengthIdInIdx = 1;
  27. constexpr uint32_t kTypeIntWidthInIdx = 0;
  28. constexpr uint32_t kConstantValueInIdx = 0;
  29. constexpr uint32_t kInsertObjectIdInIdx = 0;
  30. constexpr uint32_t kInsertCompositeIdInIdx = 1;
  31. } // namespace
  32. uint32_t DeadInsertElimPass::NumComponents(Instruction* typeInst) {
  33. switch (typeInst->opcode()) {
  34. case spv::Op::OpTypeVector: {
  35. return typeInst->GetSingleWordInOperand(kTypeVectorCountInIdx);
  36. } break;
  37. case spv::Op::OpTypeMatrix: {
  38. return typeInst->GetSingleWordInOperand(kTypeMatrixCountInIdx);
  39. } break;
  40. case spv::Op::OpTypeArray: {
  41. uint32_t lenId =
  42. typeInst->GetSingleWordInOperand(kTypeArrayLengthIdInIdx);
  43. Instruction* lenInst = get_def_use_mgr()->GetDef(lenId);
  44. if (lenInst->opcode() != spv::Op::OpConstant) 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 spv::Op::OpTypeStruct: {
  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() == spv::Op::OpTypeArray) return;
  64. // Insert chains are only composed of inserts and phis
  65. if (insertChain->opcode() != spv::Op::OpCompositeInsert &&
  66. insertChain->opcode() != spv::Op::OpPhi)
  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() == spv::Op::OpCompositeInsert) {
  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() != spv::Op::OpPhi) 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. spv::Op op = ii->opcode();
  172. Instruction* typeInst = get_def_use_mgr()->GetDef(ii->type_id());
  173. if (op != spv::Op::OpCompositeInsert &&
  174. (op != spv::Op::OpPhi || !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 == spv::Op::OpCompositeInsert) {
  181. if (typeInst->opcode() == spv::Op::OpTypeArray) {
  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. if (user->IsCommonDebugInstr()) return;
  189. switch (user->opcode()) {
  190. case spv::Op::OpCompositeInsert:
  191. case spv::Op::OpPhi:
  192. // Use by insert or phi does not initiate marking
  193. break;
  194. case spv::Op::OpCompositeExtract: {
  195. // Capture extract indices
  196. std::vector<uint32_t> extIndices;
  197. uint32_t icnt = 0;
  198. user->ForEachInOperand([&icnt, &extIndices](const uint32_t* idp) {
  199. if (icnt > 0) extIndices.push_back(*idp);
  200. ++icnt;
  201. });
  202. // Mark all inserts in chain that intersect with extract
  203. std::unordered_set<uint32_t> visited_phis;
  204. MarkInsertChain(&*ii, &extIndices, 0, &visited_phis);
  205. } break;
  206. default: {
  207. // Mark inserts in chain for all components
  208. std::unordered_set<uint32_t> visited_phis;
  209. MarkInsertChain(&*ii, nullptr, 0, &visited_phis);
  210. } break;
  211. }
  212. });
  213. }
  214. }
  215. // Find and disconnect dead inserts
  216. std::vector<Instruction*> dead_instructions;
  217. for (auto bi = func->begin(); bi != func->end(); ++bi) {
  218. for (auto ii = bi->begin(); ii != bi->end(); ++ii) {
  219. if (ii->opcode() != spv::Op::OpCompositeInsert) continue;
  220. const uint32_t id = ii->result_id();
  221. if (liveInserts_.find(id) != liveInserts_.end()) continue;
  222. const uint32_t replId =
  223. ii->GetSingleWordInOperand(kInsertCompositeIdInIdx);
  224. (void)context()->ReplaceAllUsesWith(id, replId);
  225. dead_instructions.push_back(&*ii);
  226. modified = true;
  227. }
  228. }
  229. // DCE dead inserts
  230. while (!dead_instructions.empty()) {
  231. Instruction* inst = dead_instructions.back();
  232. dead_instructions.pop_back();
  233. DCEInst(inst, [&dead_instructions](Instruction* other_inst) {
  234. auto i = std::find(dead_instructions.begin(), dead_instructions.end(),
  235. other_inst);
  236. if (i != dead_instructions.end()) {
  237. dead_instructions.erase(i);
  238. }
  239. });
  240. }
  241. return modified;
  242. }
  243. Pass::Status DeadInsertElimPass::Process() {
  244. // Process all entry point functions.
  245. ProcessFunction pfn = [this](Function* fp) {
  246. return EliminateDeadInserts(fp);
  247. };
  248. bool modified = context()->ProcessReachableCallTree(pfn);
  249. return modified ? Status::SuccessWithChange : Status::SuccessWithoutChange;
  250. }
  251. } // namespace opt
  252. } // namespace spvtools