remove_duplicates_pass.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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(
  62. reinterpret_cast<const char*>(i->GetInOperand(0u).words.data()),
  63. i->result_id());
  64. if (res.second) {
  65. // Never seen before, keep it.
  66. i = i->NextNode();
  67. } else {
  68. // It's a duplicate, remove it.
  69. context()->ReplaceAllUsesWith(i->result_id(), res.first->second);
  70. i = context()->KillInst(i);
  71. modified = true;
  72. }
  73. }
  74. return modified;
  75. }
  76. bool RemoveDuplicatesPass::RemoveDuplicateTypes() const {
  77. bool modified = false;
  78. if (context()->types_values().empty()) {
  79. return modified;
  80. }
  81. analysis::TypeManager type_manager(context()->consumer(), context());
  82. std::vector<Instruction*> visited_types;
  83. std::vector<analysis::ForwardPointer> visited_forward_pointers;
  84. std::vector<Instruction*> to_delete;
  85. for (auto* i = &*context()->types_values_begin(); i; i = i->NextNode()) {
  86. const bool is_i_forward_pointer = i->opcode() == SpvOpTypeForwardPointer;
  87. // We only care about types.
  88. if (!spvOpcodeGeneratesType(i->opcode()) && !is_i_forward_pointer) {
  89. continue;
  90. }
  91. if (!is_i_forward_pointer) {
  92. // Is the current type equal to one of the types we have already visited?
  93. SpvId id_to_keep = 0u;
  94. analysis::Type* i_type = type_manager.GetType(i->result_id());
  95. assert(i_type);
  96. // TODO(dneto0): Use a trie to avoid quadratic behaviour? Extract the
  97. // ResultIdTrie from unify_const_pass.cpp for this.
  98. for (auto j : visited_types) {
  99. analysis::Type* j_type = type_manager.GetType(j->result_id());
  100. assert(j_type);
  101. if (*i_type == *j_type) {
  102. id_to_keep = j->result_id();
  103. break;
  104. }
  105. }
  106. if (id_to_keep == 0u) {
  107. // This is a never seen before type, keep it around.
  108. visited_types.emplace_back(i);
  109. } else {
  110. // The same type has already been seen before, remove this one.
  111. context()->KillNamesAndDecorates(i->result_id());
  112. context()->ReplaceAllUsesWith(i->result_id(), id_to_keep);
  113. modified = true;
  114. to_delete.emplace_back(i);
  115. }
  116. } else {
  117. analysis::ForwardPointer i_type(
  118. i->GetSingleWordInOperand(0u),
  119. (SpvStorageClass)i->GetSingleWordInOperand(1u));
  120. i_type.SetTargetPointer(
  121. type_manager.GetType(i_type.target_id())->AsPointer());
  122. // TODO(dneto0): Use a trie to avoid quadratic behaviour? Extract the
  123. // ResultIdTrie from unify_const_pass.cpp for this.
  124. const bool found_a_match =
  125. std::find(std::begin(visited_forward_pointers),
  126. std::end(visited_forward_pointers),
  127. i_type) != std::end(visited_forward_pointers);
  128. if (!found_a_match) {
  129. // This is a never seen before type, keep it around.
  130. visited_forward_pointers.emplace_back(i_type);
  131. } else {
  132. // The same type has already been seen before, remove this one.
  133. modified = true;
  134. to_delete.emplace_back(i);
  135. }
  136. }
  137. }
  138. for (auto i : to_delete) {
  139. context()->KillInst(i);
  140. }
  141. return modified;
  142. }
  143. // TODO(pierremoreau): Duplicate decoration groups should be removed. For
  144. // example, in
  145. // OpDecorate %1 Constant
  146. // %1 = OpDecorationGroup
  147. // OpDecorate %2 Constant
  148. // %2 = OpDecorationGroup
  149. // OpGroupDecorate %1 %3
  150. // OpGroupDecorate %2 %4
  151. // group %2 could be removed.
  152. bool RemoveDuplicatesPass::RemoveDuplicateDecorations() const {
  153. bool modified = false;
  154. std::vector<const Instruction*> visited_decorations;
  155. analysis::DecorationManager decoration_manager(context()->module());
  156. for (auto* i = &*context()->annotation_begin(); i;) {
  157. // Is the current decoration equal to one of the decorations we have
  158. // already visited?
  159. bool already_visited = false;
  160. // TODO(dneto0): Use a trie to avoid quadratic behaviour? Extract the
  161. // ResultIdTrie from unify_const_pass.cpp for this.
  162. for (const Instruction* j : visited_decorations) {
  163. if (decoration_manager.AreDecorationsTheSame(&*i, j, false)) {
  164. already_visited = true;
  165. break;
  166. }
  167. }
  168. if (!already_visited) {
  169. // This is a never seen before decoration, keep it around.
  170. visited_decorations.emplace_back(&*i);
  171. i = i->NextNode();
  172. } else {
  173. // The same decoration has already been seen before, remove this one.
  174. modified = true;
  175. i = context()->KillInst(i);
  176. }
  177. }
  178. return modified;
  179. }
  180. } // namespace opt
  181. } // namespace spvtools