remove_duplicates_pass.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. std::vector<Instruction*> visited_types;
  82. std::vector<Instruction*> to_delete;
  83. for (auto* i = &*context()->types_values_begin(); i; i = i->NextNode()) {
  84. // We only care about types.
  85. if (!spvOpcodeGeneratesType((i->opcode())) &&
  86. i->opcode() != SpvOpTypeForwardPointer) {
  87. continue;
  88. }
  89. // Is the current type equal to one of the types we have aready visited?
  90. SpvId id_to_keep = 0u;
  91. // TODO(dneto0): Use a trie to avoid quadratic behaviour? Extract the
  92. // ResultIdTrie from unify_const_pass.cpp for this.
  93. for (auto j : visited_types) {
  94. if (AreTypesEqual(*i, *j, context())) {
  95. id_to_keep = j->result_id();
  96. break;
  97. }
  98. }
  99. if (id_to_keep == 0u) {
  100. // This is a never seen before type, keep it around.
  101. visited_types.emplace_back(i);
  102. } else {
  103. // The same type has already been seen before, remove this one.
  104. context()->KillNamesAndDecorates(i->result_id());
  105. context()->ReplaceAllUsesWith(i->result_id(), id_to_keep);
  106. modified = true;
  107. to_delete.emplace_back(i);
  108. }
  109. }
  110. for (auto i : to_delete) {
  111. context()->KillInst(i);
  112. }
  113. return modified;
  114. }
  115. // TODO(pierremoreau): Duplicate decoration groups should be removed. For
  116. // example, in
  117. // OpDecorate %1 Constant
  118. // %1 = OpDecorationGroup
  119. // OpDecorate %2 Constant
  120. // %2 = OpDecorationGroup
  121. // OpGroupDecorate %1 %3
  122. // OpGroupDecorate %2 %4
  123. // group %2 could be removed.
  124. bool RemoveDuplicatesPass::RemoveDuplicateDecorations() const {
  125. bool modified = false;
  126. std::vector<const Instruction*> visited_decorations;
  127. analysis::DecorationManager decoration_manager(context()->module());
  128. for (auto* i = &*context()->annotation_begin(); i;) {
  129. // Is the current decoration equal to one of the decorations we have aready
  130. // visited?
  131. bool already_visited = false;
  132. // TODO(dneto0): Use a trie to avoid quadratic behaviour? Extract the
  133. // ResultIdTrie from unify_const_pass.cpp for this.
  134. for (const Instruction* j : visited_decorations) {
  135. if (decoration_manager.AreDecorationsTheSame(&*i, j, false)) {
  136. already_visited = true;
  137. break;
  138. }
  139. }
  140. if (!already_visited) {
  141. // This is a never seen before decoration, keep it around.
  142. visited_decorations.emplace_back(&*i);
  143. i = i->NextNode();
  144. } else {
  145. // The same decoration has already been seen before, remove this one.
  146. modified = true;
  147. i = context()->KillInst(i);
  148. }
  149. }
  150. return modified;
  151. }
  152. bool RemoveDuplicatesPass::AreTypesEqual(const Instruction& inst1,
  153. const Instruction& inst2,
  154. IRContext* context) {
  155. if (inst1.opcode() != inst2.opcode()) return false;
  156. if (!IsTypeInst(inst1.opcode())) return false;
  157. const analysis::Type* type1 =
  158. context->get_type_mgr()->GetType(inst1.result_id());
  159. const analysis::Type* type2 =
  160. context->get_type_mgr()->GetType(inst2.result_id());
  161. if (type1 && type2 && *type1 == *type2) return true;
  162. return false;
  163. }
  164. } // namespace opt
  165. } // namespace spvtools