2
0

remove_duplicates_pass.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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 <cstring>
  17. #include <limits>
  18. #include <string>
  19. #include <unordered_map>
  20. #include <unordered_set>
  21. #include <vector>
  22. #include "source/opcode.h"
  23. #include "source/opt/decoration_manager.h"
  24. #include "source/opt/ir_context.h"
  25. #include "source/opt/reflect.h"
  26. namespace spvtools {
  27. namespace opt {
  28. Pass::Status RemoveDuplicatesPass::Process() {
  29. bool modified = RemoveDuplicateCapabilities();
  30. modified |= RemoveDuplicatesExtInstImports();
  31. modified |= RemoveDuplicateTypes();
  32. modified |= RemoveDuplicateDecorations();
  33. return modified ? Status::SuccessWithChange : Status::SuccessWithoutChange;
  34. }
  35. bool RemoveDuplicatesPass::RemoveDuplicateCapabilities() const {
  36. bool modified = false;
  37. if (context()->capabilities().empty()) {
  38. return modified;
  39. }
  40. std::unordered_set<uint32_t> capabilities;
  41. for (auto* i = &*context()->capability_begin(); i;) {
  42. auto res = capabilities.insert(i->GetSingleWordOperand(0u));
  43. if (res.second) {
  44. // Never seen before, keep it.
  45. i = i->NextNode();
  46. } else {
  47. // It's a duplicate, remove it.
  48. i = context()->KillInst(i);
  49. modified = true;
  50. }
  51. }
  52. return modified;
  53. }
  54. bool RemoveDuplicatesPass::RemoveDuplicatesExtInstImports() const {
  55. bool modified = false;
  56. if (context()->ext_inst_imports().empty()) {
  57. return modified;
  58. }
  59. std::unordered_map<std::string, SpvId> ext_inst_imports;
  60. for (auto* i = &*context()->ext_inst_import_begin(); i;) {
  61. auto res = ext_inst_imports.emplace(i->GetInOperand(0u).AsString(),
  62. i->result_id());
  63. if (res.second) {
  64. // Never seen before, keep it.
  65. i = i->NextNode();
  66. } else {
  67. // It's a duplicate, remove it.
  68. context()->ReplaceAllUsesWith(i->result_id(), res.first->second);
  69. i = context()->KillInst(i);
  70. modified = true;
  71. }
  72. }
  73. return modified;
  74. }
  75. bool RemoveDuplicatesPass::RemoveDuplicateTypes() const {
  76. bool modified = false;
  77. if (context()->types_values().empty()) {
  78. return modified;
  79. }
  80. analysis::TypeManager type_manager(context()->consumer(), context());
  81. std::vector<Instruction*> visited_types;
  82. std::vector<analysis::ForwardPointer> visited_forward_pointers;
  83. std::vector<Instruction*> to_delete;
  84. for (auto* i = &*context()->types_values_begin(); i; i = i->NextNode()) {
  85. const bool is_i_forward_pointer = i->opcode() == SpvOpTypeForwardPointer;
  86. // We only care about types.
  87. if (!spvOpcodeGeneratesType(i->opcode()) && !is_i_forward_pointer) {
  88. continue;
  89. }
  90. if (!is_i_forward_pointer) {
  91. // Is the current type equal to one of the types we have already visited?
  92. SpvId id_to_keep = 0u;
  93. analysis::Type* i_type = type_manager.GetType(i->result_id());
  94. assert(i_type);
  95. // TODO(dneto0): Use a trie to avoid quadratic behaviour? Extract the
  96. // ResultIdTrie from unify_const_pass.cpp for this.
  97. for (auto j : visited_types) {
  98. analysis::Type* j_type = type_manager.GetType(j->result_id());
  99. assert(j_type);
  100. if (*i_type == *j_type) {
  101. id_to_keep = j->result_id();
  102. break;
  103. }
  104. }
  105. if (id_to_keep == 0u) {
  106. // This is a never seen before type, keep it around.
  107. visited_types.emplace_back(i);
  108. } else {
  109. // The same type has already been seen before, remove this one.
  110. context()->KillNamesAndDecorates(i->result_id());
  111. context()->ReplaceAllUsesWith(i->result_id(), id_to_keep);
  112. modified = true;
  113. to_delete.emplace_back(i);
  114. }
  115. } else {
  116. analysis::ForwardPointer i_type(
  117. i->GetSingleWordInOperand(0u),
  118. (SpvStorageClass)i->GetSingleWordInOperand(1u));
  119. i_type.SetTargetPointer(
  120. type_manager.GetType(i_type.target_id())->AsPointer());
  121. // TODO(dneto0): Use a trie to avoid quadratic behaviour? Extract the
  122. // ResultIdTrie from unify_const_pass.cpp for this.
  123. const bool found_a_match =
  124. std::find(std::begin(visited_forward_pointers),
  125. std::end(visited_forward_pointers),
  126. i_type) != std::end(visited_forward_pointers);
  127. if (!found_a_match) {
  128. // This is a never seen before type, keep it around.
  129. visited_forward_pointers.emplace_back(i_type);
  130. } else {
  131. // The same type has already been seen before, remove this one.
  132. modified = true;
  133. to_delete.emplace_back(i);
  134. }
  135. }
  136. }
  137. for (auto i : to_delete) {
  138. context()->KillInst(i);
  139. }
  140. return modified;
  141. }
  142. // TODO(pierremoreau): Duplicate decoration groups should be removed. For
  143. // example, in
  144. // OpDecorate %1 Constant
  145. // %1 = OpDecorationGroup
  146. // OpDecorate %2 Constant
  147. // %2 = OpDecorationGroup
  148. // OpGroupDecorate %1 %3
  149. // OpGroupDecorate %2 %4
  150. // group %2 could be removed.
  151. bool RemoveDuplicatesPass::RemoveDuplicateDecorations() const {
  152. bool modified = false;
  153. std::vector<const Instruction*> visited_decorations;
  154. analysis::DecorationManager decoration_manager(context()->module());
  155. for (auto* i = &*context()->annotation_begin(); i;) {
  156. // Is the current decoration equal to one of the decorations we have
  157. // already visited?
  158. bool already_visited = false;
  159. // TODO(dneto0): Use a trie to avoid quadratic behaviour? Extract the
  160. // ResultIdTrie from unify_const_pass.cpp for this.
  161. for (const Instruction* j : visited_decorations) {
  162. if (decoration_manager.AreDecorationsTheSame(&*i, j, false)) {
  163. already_visited = true;
  164. break;
  165. }
  166. }
  167. if (!already_visited) {
  168. // This is a never seen before decoration, keep it around.
  169. visited_decorations.emplace_back(&*i);
  170. i = i->NextNode();
  171. } else {
  172. // The same decoration has already been seen before, remove this one.
  173. modified = true;
  174. i = context()->KillInst(i);
  175. }
  176. }
  177. return modified;
  178. }
  179. } // namespace opt
  180. } // namespace spvtools