remove_duplicates_pass.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. // Copyright (c) 2017 Pierre Moreau
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include "source/opt/remove_duplicates_pass.h"
  15. #include <algorithm>
  16. #include <string>
  17. #include <unordered_map>
  18. #include <unordered_set>
  19. #include <vector>
  20. #include "source/opcode.h"
  21. #include "source/opt/decoration_manager.h"
  22. #include "source/opt/ir_context.h"
  23. namespace spvtools {
  24. namespace opt {
  25. Pass::Status RemoveDuplicatesPass::Process() {
  26. bool modified = RemoveDuplicateCapabilities();
  27. modified |= RemoveDuplicatesExtInstImports();
  28. modified |= RemoveDuplicateTypes();
  29. modified |= RemoveDuplicateDecorations();
  30. return modified ? Status::SuccessWithChange : Status::SuccessWithoutChange;
  31. }
  32. bool RemoveDuplicatesPass::RemoveDuplicateCapabilities() const {
  33. bool modified = false;
  34. if (context()->capabilities().empty()) {
  35. return modified;
  36. }
  37. std::unordered_set<uint32_t> capabilities;
  38. for (auto* i = &*context()->capability_begin(); i;) {
  39. auto res = capabilities.insert(i->GetSingleWordOperand(0u));
  40. if (res.second) {
  41. // Never seen before, keep it.
  42. i = i->NextNode();
  43. } else {
  44. // It's a duplicate, remove it.
  45. i = context()->KillInst(i);
  46. modified = true;
  47. }
  48. }
  49. return modified;
  50. }
  51. bool RemoveDuplicatesPass::RemoveDuplicatesExtInstImports() const {
  52. bool modified = false;
  53. if (context()->ext_inst_imports().empty()) {
  54. return modified;
  55. }
  56. std::unordered_map<std::string, spv::Id> ext_inst_imports;
  57. for (auto* i = &*context()->ext_inst_import_begin(); i;) {
  58. auto res = ext_inst_imports.emplace(i->GetInOperand(0u).AsString(),
  59. i->result_id());
  60. if (res.second) {
  61. // Never seen before, keep it.
  62. i = i->NextNode();
  63. } else {
  64. // It's a duplicate, remove it.
  65. context()->ReplaceAllUsesWith(i->result_id(), res.first->second);
  66. i = context()->KillInst(i);
  67. modified = true;
  68. }
  69. }
  70. return modified;
  71. }
  72. bool RemoveDuplicatesPass::RemoveDuplicateTypes() const {
  73. bool modified = false;
  74. if (context()->types_values().empty()) {
  75. return modified;
  76. }
  77. analysis::TypeManager type_manager(context()->consumer(), context());
  78. std::vector<Instruction*> visited_types;
  79. std::vector<analysis::ForwardPointer> visited_forward_pointers;
  80. std::vector<Instruction*> to_delete;
  81. for (auto* i = &*context()->types_values_begin(); i; i = i->NextNode()) {
  82. const bool is_i_forward_pointer =
  83. i->opcode() == spv::Op::OpTypeForwardPointer;
  84. // We only care about types.
  85. if (!spvOpcodeGeneratesType(i->opcode()) && !is_i_forward_pointer) {
  86. continue;
  87. }
  88. if (!is_i_forward_pointer) {
  89. // Is the current type equal to one of the types we have already visited?
  90. spv::Id id_to_keep = 0u;
  91. analysis::Type* i_type = type_manager.GetType(i->result_id());
  92. assert(i_type);
  93. // TODO(dneto0): Use a trie to avoid quadratic behaviour? Extract the
  94. // ResultIdTrie from unify_const_pass.cpp for this.
  95. for (auto j : visited_types) {
  96. analysis::Type* j_type = type_manager.GetType(j->result_id());
  97. assert(j_type);
  98. if (*i_type == *j_type) {
  99. id_to_keep = j->result_id();
  100. break;
  101. }
  102. }
  103. if (id_to_keep == 0u) {
  104. // This is a never seen before type, keep it around.
  105. visited_types.emplace_back(i);
  106. } else {
  107. // The same type has already been seen before, remove this one.
  108. context()->KillNamesAndDecorates(i->result_id());
  109. context()->ReplaceAllUsesWith(i->result_id(), id_to_keep);
  110. modified = true;
  111. to_delete.emplace_back(i);
  112. }
  113. } else {
  114. analysis::ForwardPointer i_type(
  115. i->GetSingleWordInOperand(0u),
  116. (spv::StorageClass)i->GetSingleWordInOperand(1u));
  117. i_type.SetTargetPointer(
  118. type_manager.GetType(i_type.target_id())->AsPointer());
  119. // TODO(dneto0): Use a trie to avoid quadratic behaviour? Extract the
  120. // ResultIdTrie from unify_const_pass.cpp for this.
  121. const bool found_a_match =
  122. std::find(std::begin(visited_forward_pointers),
  123. std::end(visited_forward_pointers),
  124. i_type) != std::end(visited_forward_pointers);
  125. if (!found_a_match) {
  126. // This is a never seen before type, keep it around.
  127. visited_forward_pointers.emplace_back(i_type);
  128. } else {
  129. // The same type has already been seen before, remove this one.
  130. modified = true;
  131. to_delete.emplace_back(i);
  132. }
  133. }
  134. }
  135. for (auto i : to_delete) {
  136. context()->KillInst(i);
  137. }
  138. return modified;
  139. }
  140. // TODO(pierremoreau): Duplicate decoration groups should be removed. For
  141. // example, in
  142. // OpDecorate %1 Constant
  143. // %1 = OpDecorationGroup
  144. // OpDecorate %2 Constant
  145. // %2 = OpDecorationGroup
  146. // OpGroupDecorate %1 %3
  147. // OpGroupDecorate %2 %4
  148. // group %2 could be removed.
  149. bool RemoveDuplicatesPass::RemoveDuplicateDecorations() const {
  150. bool modified = false;
  151. std::vector<const Instruction*> visited_decorations;
  152. analysis::DecorationManager decoration_manager(context()->module());
  153. for (auto* i = &*context()->annotation_begin(); i;) {
  154. // Is the current decoration equal to one of the decorations we have
  155. // already visited?
  156. bool already_visited = false;
  157. // TODO(dneto0): Use a trie to avoid quadratic behaviour? Extract the
  158. // ResultIdTrie from unify_const_pass.cpp for this.
  159. for (const Instruction* j : visited_decorations) {
  160. if (decoration_manager.AreDecorationsTheSame(&*i, j, false)) {
  161. already_visited = true;
  162. break;
  163. }
  164. }
  165. if (!already_visited) {
  166. // This is a never seen before decoration, keep it around.
  167. visited_decorations.emplace_back(&*i);
  168. i = i->NextNode();
  169. } else {
  170. // The same decoration has already been seen before, remove this one.
  171. modified = true;
  172. i = context()->KillInst(i);
  173. }
  174. }
  175. return modified;
  176. }
  177. } // namespace opt
  178. } // namespace spvtools