remove_duplicates_pass.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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 |= RemoveDuplicateExtensions();
  28. modified |= RemoveDuplicatesExtInstImports();
  29. modified |= RemoveDuplicateTypes();
  30. modified |= RemoveDuplicateDecorations();
  31. return modified ? Status::SuccessWithChange : Status::SuccessWithoutChange;
  32. }
  33. bool RemoveDuplicatesPass::RemoveDuplicateExtensions() const {
  34. bool modified = false;
  35. if (context()->extensions().empty()) {
  36. return modified;
  37. }
  38. // set of {condition ID, extension name}
  39. // ID 0 means unconditional extension, ie., OpExtension, otherwise the ID is
  40. // the condition operand of OpConditionalExtensionINTEL.
  41. std::set<std::pair<uint32_t, std::string>> extensions;
  42. for (auto* inst = &*context()->extension_begin(); inst;) {
  43. uint32_t cond_id = 0;
  44. uint32_t i_name = 0;
  45. if (inst->opcode() == spv::Op::OpConditionalExtensionINTEL) {
  46. cond_id = inst->GetOperand(0).AsId();
  47. i_name = 1;
  48. }
  49. auto res =
  50. extensions.insert({cond_id, inst->GetOperand(i_name).AsString()});
  51. if (res.second) {
  52. // Never seen before, keep it.
  53. inst = inst->NextNode();
  54. } else {
  55. // It's a duplicate, remove it.
  56. inst = context()->KillInst(inst);
  57. modified = true;
  58. }
  59. }
  60. return modified;
  61. }
  62. bool RemoveDuplicatesPass::RemoveDuplicateCapabilities() const {
  63. bool modified = false;
  64. if (context()->capabilities().empty()) {
  65. return modified;
  66. }
  67. // set of {condition ID, capability}
  68. // ID 0 means unconditional capability, ie., OpCapability, otherwise the ID is
  69. // the condition operand of OpConditionalCapabilityINTEL.
  70. std::set<std::pair<uint32_t, uint32_t>> capabilities;
  71. for (auto* inst = &*context()->capability_begin(); inst;) {
  72. uint32_t cond_id = 0;
  73. uint32_t i_cap = 0;
  74. if (inst->opcode() == spv::Op::OpConditionalCapabilityINTEL) {
  75. cond_id = inst->GetOperand(0).AsId();
  76. i_cap = 1;
  77. }
  78. auto res =
  79. capabilities.insert({cond_id, inst->GetSingleWordOperand(i_cap)});
  80. if (res.second) {
  81. // Never seen before, keep it.
  82. inst = inst->NextNode();
  83. } else {
  84. // It's a duplicate, remove it.
  85. inst = context()->KillInst(inst);
  86. modified = true;
  87. }
  88. }
  89. return modified;
  90. }
  91. bool RemoveDuplicatesPass::RemoveDuplicatesExtInstImports() const {
  92. bool modified = false;
  93. if (context()->ext_inst_imports().empty()) {
  94. return modified;
  95. }
  96. std::unordered_map<std::string, spv::Id> ext_inst_imports;
  97. for (auto* i = &*context()->ext_inst_import_begin(); i;) {
  98. auto res = ext_inst_imports.emplace(i->GetInOperand(0u).AsString(),
  99. i->result_id());
  100. if (res.second) {
  101. // Never seen before, keep it.
  102. i = i->NextNode();
  103. } else {
  104. // It's a duplicate, remove it.
  105. context()->ReplaceAllUsesWith(i->result_id(), res.first->second);
  106. i = context()->KillInst(i);
  107. modified = true;
  108. }
  109. }
  110. return modified;
  111. }
  112. bool RemoveDuplicatesPass::RemoveDuplicateTypes() const {
  113. bool modified = false;
  114. if (context()->types_values().empty()) {
  115. return modified;
  116. }
  117. analysis::TypeManager type_manager(context()->consumer(), context());
  118. std::vector<Instruction*> visited_types;
  119. std::vector<analysis::ForwardPointer> visited_forward_pointers;
  120. std::vector<Instruction*> to_delete;
  121. for (auto* i = &*context()->types_values_begin(); i; i = i->NextNode()) {
  122. const bool is_i_forward_pointer =
  123. i->opcode() == spv::Op::OpTypeForwardPointer;
  124. // We only care about types.
  125. if (!spvOpcodeGeneratesType(i->opcode()) && !is_i_forward_pointer) {
  126. continue;
  127. }
  128. if (!is_i_forward_pointer) {
  129. // Is the current type equal to one of the types we have already visited?
  130. spv::Id id_to_keep = 0u;
  131. analysis::Type* i_type = type_manager.GetType(i->result_id());
  132. assert(i_type);
  133. // TODO(dneto0): Use a trie to avoid quadratic behaviour? Extract the
  134. // ResultIdTrie from unify_const_pass.cpp for this.
  135. for (auto j : visited_types) {
  136. analysis::Type* j_type = type_manager.GetType(j->result_id());
  137. assert(j_type);
  138. if (*i_type == *j_type) {
  139. id_to_keep = j->result_id();
  140. break;
  141. }
  142. }
  143. if (id_to_keep == 0u) {
  144. // This is a never seen before type, keep it around.
  145. visited_types.emplace_back(i);
  146. } else {
  147. // The same type has already been seen before, remove this one.
  148. context()->KillNamesAndDecorates(i->result_id());
  149. context()->ReplaceAllUsesWith(i->result_id(), id_to_keep);
  150. modified = true;
  151. to_delete.emplace_back(i);
  152. }
  153. } else {
  154. analysis::ForwardPointer i_type(
  155. i->GetSingleWordInOperand(0u),
  156. (spv::StorageClass)i->GetSingleWordInOperand(1u));
  157. i_type.SetTargetPointer(
  158. type_manager.GetType(i_type.target_id())->AsPointer());
  159. // TODO(dneto0): Use a trie to avoid quadratic behaviour? Extract the
  160. // ResultIdTrie from unify_const_pass.cpp for this.
  161. const bool found_a_match =
  162. std::find(std::begin(visited_forward_pointers),
  163. std::end(visited_forward_pointers),
  164. i_type) != std::end(visited_forward_pointers);
  165. if (!found_a_match) {
  166. // This is a never seen before type, keep it around.
  167. visited_forward_pointers.emplace_back(i_type);
  168. } else {
  169. // The same type has already been seen before, remove this one.
  170. modified = true;
  171. to_delete.emplace_back(i);
  172. }
  173. }
  174. }
  175. for (auto i : to_delete) {
  176. context()->KillInst(i);
  177. }
  178. return modified;
  179. }
  180. // TODO(pierremoreau): Duplicate decoration groups should be removed. For
  181. // example, in
  182. // OpDecorate %1 Constant
  183. // %1 = OpDecorationGroup
  184. // OpDecorate %2 Constant
  185. // %2 = OpDecorationGroup
  186. // OpGroupDecorate %1 %3
  187. // OpGroupDecorate %2 %4
  188. // group %2 could be removed.
  189. bool RemoveDuplicatesPass::RemoveDuplicateDecorations() const {
  190. bool modified = false;
  191. std::vector<const Instruction*> visited_decorations;
  192. analysis::DecorationManager decoration_manager(context()->module());
  193. for (auto* i = &*context()->annotation_begin(); i;) {
  194. // Is the current decoration equal to one of the decorations we have
  195. // already visited?
  196. bool already_visited = false;
  197. // TODO(dneto0): Use a trie to avoid quadratic behaviour? Extract the
  198. // ResultIdTrie from unify_const_pass.cpp for this.
  199. for (const Instruction* j : visited_decorations) {
  200. if (decoration_manager.AreDecorationsTheSame(&*i, j, false)) {
  201. already_visited = true;
  202. break;
  203. }
  204. }
  205. if (!already_visited) {
  206. // This is a never seen before decoration, keep it around.
  207. visited_decorations.emplace_back(&*i);
  208. i = i->NextNode();
  209. } else {
  210. // The same decoration has already been seen before, remove this one.
  211. modified = true;
  212. i = context()->KillInst(i);
  213. }
  214. }
  215. return modified;
  216. }
  217. } // namespace opt
  218. } // namespace spvtools