specific_coalescer.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. #include "toolchain/lower/specific_coalescer.h"
  5. #include "common/check.h"
  6. #include "common/vlog.h"
  7. namespace Carbon::Lower {
  8. SpecificCoalescer::SpecificCoalescer(llvm::raw_ostream* vlog_stream,
  9. const SemIR::SpecificStore& specifics)
  10. : vlog_stream_(vlog_stream),
  11. lowered_specifics_type_fingerprint_(specifics, {}),
  12. lowered_specific_fingerprint_(specifics, {}),
  13. equivalent_specifics_(specifics, SemIR::SpecificId::None) {}
  14. auto SpecificCoalescer::CoalesceEquivalentSpecifics(
  15. LoweredSpecificsStore& lowered_specifics,
  16. LoweredLlvmFunctionStore& lowered_llvm_functions) -> void {
  17. for (auto& specifics : lowered_specifics.values()) {
  18. // Collect specifics to delete for each generic. Replace and remove each
  19. // after processing all specifics for a generic. Note, we could also
  20. // replace and remove all specifics after processing all generics.
  21. llvm::SmallVector<SemIR::SpecificId> specifics_to_delete;
  22. // i cannot be unsigned due to the comparison with a negative number when
  23. // the specifics vector is empty.
  24. for (int i = 0; i < static_cast<int>(specifics.size()) - 1; ++i) {
  25. // This specific was already replaced, skip it.
  26. if (equivalent_specifics_.Get(specifics[i]).has_value() &&
  27. equivalent_specifics_.Get(specifics[i]) != specifics[i]) {
  28. specifics_to_delete.push_back(specifics[i]);
  29. specifics[i] = specifics[specifics.size() - 1];
  30. specifics.pop_back();
  31. --i;
  32. continue;
  33. }
  34. // TODO: Improve quadratic behavior by using a single hash based on
  35. // `lowered_specifics_type_fingerprint_` and `common_fingerprint`.
  36. for (int j = i + 1; j < static_cast<int>(specifics.size()); ++j) {
  37. // When the specific was already replaced, skip it.
  38. if (equivalent_specifics_.Get(specifics[j]).has_value() &&
  39. equivalent_specifics_.Get(specifics[j]) != specifics[j]) {
  40. specifics_to_delete.push_back(specifics[j]);
  41. specifics[j] = specifics[specifics.size() - 1];
  42. specifics.pop_back();
  43. --j;
  44. continue;
  45. }
  46. // When the two specifics are not equivalent due to the function type
  47. // info stored in lowered_specifics_types, mark non-equivalance. This
  48. // can be reused to short-cut another path and continue the search for
  49. // other equivalences.
  50. if (!AreFunctionTypesEquivalent(specifics[i], specifics[j])) {
  51. InsertPair(specifics[i], specifics[j], non_equivalent_specifics_);
  52. continue;
  53. }
  54. Set<std::pair<SemIR::SpecificId, SemIR::SpecificId>>
  55. visited_equivalent_specifics;
  56. InsertPair(specifics[i], specifics[j], visited_equivalent_specifics);
  57. // Function type information matches; check usages inside the function
  58. // body that are dependent on the specific. This information has been
  59. // stored in lowered_states while lowering each function body.
  60. if (AreFunctionBodiesEquivalent(specifics[i], specifics[j],
  61. visited_equivalent_specifics)) {
  62. // When processing equivalences, we may change the canonical specific
  63. // multiple times, so we don't delete replaced specifics until the
  64. // end.
  65. visited_equivalent_specifics.ForEach(
  66. [&](std::pair<SemIR::SpecificId, SemIR::SpecificId>
  67. equivalent_entry) {
  68. CARBON_VLOG("Found equivalent specifics: {0}, {1}",
  69. equivalent_entry.first, equivalent_entry.second);
  70. ProcessSpecificEquivalence(equivalent_entry);
  71. });
  72. // Removed the replaced specific from the list of emitted specifics.
  73. // Only the top level, since the others are somewhere else in the
  74. // vector, they will be found and removed during processing.
  75. specifics_to_delete.push_back(specifics[j]);
  76. specifics[j] = specifics[specifics.size() - 1];
  77. specifics.pop_back();
  78. --j;
  79. } else {
  80. // Only mark non-equivalence based on state for starting specifics.
  81. InsertPair(specifics[i], specifics[j], non_equivalent_specifics_);
  82. }
  83. }
  84. }
  85. // Once all equivalences are found for a generic, update and delete up
  86. // equivalent specifics.
  87. for (auto specific_id : specifics_to_delete) {
  88. UpdateAndDeleteLLVMFunction(lowered_llvm_functions, specific_id);
  89. }
  90. }
  91. }
  92. auto SpecificCoalescer::ProcessSpecificEquivalence(
  93. std::pair<SemIR::SpecificId, SemIR::SpecificId> pair) -> void {
  94. auto [specific_id1, specific_id2] = pair;
  95. CARBON_CHECK(specific_id1.has_value() && specific_id2.has_value(),
  96. "Expected values in equivalence check");
  97. auto get_canon = [&](SemIR::SpecificId specific_id) {
  98. auto equiv_id = equivalent_specifics_.Get(specific_id);
  99. return equiv_id.has_value() ? equiv_id : specific_id;
  100. };
  101. auto canon_id1 = get_canon(specific_id1);
  102. auto canon_id2 = get_canon(specific_id2);
  103. if (canon_id1 == canon_id2) {
  104. // Already equivalent, there was a previous replacement.
  105. return;
  106. }
  107. if (canon_id1.index >= canon_id2.index) {
  108. // Prefer the earlier index for canonical values.
  109. std::swap(canon_id1, canon_id2);
  110. }
  111. // Update equivalent_specifics_ for all. This is used as an indicator that
  112. // this specific_id may be the canonical one when reducing the equivalence
  113. // chains in `IsKnownEquivalence`.
  114. equivalent_specifics_.Set(specific_id1, canon_id1);
  115. equivalent_specifics_.Set(specific_id2, canon_id1);
  116. equivalent_specifics_.Set(canon_id1, canon_id1);
  117. equivalent_specifics_.Set(canon_id2, canon_id1);
  118. }
  119. auto SpecificCoalescer::UpdateEquivalentSpecific(SemIR::SpecificId specific_id)
  120. -> void {
  121. if (!equivalent_specifics_.Get(specific_id).has_value()) {
  122. return;
  123. }
  124. llvm::SmallVector<SemIR::SpecificId> stack;
  125. SemIR::SpecificId specific_to_update = specific_id;
  126. SemIR::SpecificId equivalent = equivalent_specifics_.Get(specific_to_update);
  127. SemIR::SpecificId equivalent_next = equivalent_specifics_.Get(equivalent);
  128. while (equivalent != equivalent_next) {
  129. stack.push_back(specific_to_update);
  130. specific_to_update = equivalent;
  131. equivalent = equivalent_next;
  132. equivalent_next = equivalent_specifics_.Get(equivalent_next);
  133. }
  134. for (auto specific : stack) {
  135. equivalent_specifics_.Set(specific, equivalent);
  136. }
  137. }
  138. auto SpecificCoalescer::UpdateAndDeleteLLVMFunction(
  139. LoweredLlvmFunctionStore& lowered_llvm_functions,
  140. SemIR::SpecificId specific_id) -> void {
  141. UpdateEquivalentSpecific(specific_id);
  142. auto* old_function = lowered_llvm_functions.Get(specific_id);
  143. auto* new_function =
  144. lowered_llvm_functions.Get(equivalent_specifics_.Get(specific_id));
  145. old_function->replaceAllUsesWith(new_function);
  146. old_function->eraseFromParent();
  147. lowered_llvm_functions.Set(specific_id, new_function);
  148. }
  149. auto SpecificCoalescer::IsKnownEquivalence(SemIR::SpecificId specific_id1,
  150. SemIR::SpecificId specific_id2)
  151. -> bool {
  152. if (!equivalent_specifics_.Get(specific_id1).has_value() ||
  153. !equivalent_specifics_.Get(specific_id2).has_value()) {
  154. return false;
  155. }
  156. UpdateEquivalentSpecific(specific_id1);
  157. UpdateEquivalentSpecific(specific_id2);
  158. return equivalent_specifics_.Get(specific_id1) ==
  159. equivalent_specifics_.Get(specific_id2);
  160. }
  161. auto SpecificCoalescer::AreFunctionTypesEquivalent(
  162. SemIR::SpecificId specific_id1, SemIR::SpecificId specific_id2) -> bool {
  163. CARBON_CHECK(specific_id1.has_value() && specific_id2.has_value());
  164. return lowered_specifics_type_fingerprint_.Get(specific_id1) ==
  165. lowered_specifics_type_fingerprint_.Get(specific_id2);
  166. }
  167. auto SpecificCoalescer::AreFunctionBodiesEquivalent(
  168. SemIR::SpecificId specific_id1, SemIR::SpecificId specific_id2,
  169. Set<std::pair<SemIR::SpecificId, SemIR::SpecificId>>&
  170. visited_equivalent_specifics) -> bool {
  171. llvm::SmallVector<std::pair<SemIR::SpecificId, SemIR::SpecificId>> worklist;
  172. worklist.push_back({specific_id1, specific_id2});
  173. while (!worklist.empty()) {
  174. auto outer_pair = worklist.pop_back_val();
  175. auto [specific_id1, specific_id2] = outer_pair;
  176. auto state1 = lowered_specific_fingerprint_.Get(specific_id1);
  177. auto state2 = lowered_specific_fingerprint_.Get(specific_id2);
  178. if (state1.common_fingerprint != state2.common_fingerprint) {
  179. InsertPair(specific_id1, specific_id2, non_equivalent_specifics_);
  180. return false;
  181. }
  182. if (state1.specific_fingerprint == state2.specific_fingerprint) {
  183. continue;
  184. }
  185. // A size difference should have been detected by the common fingerprint.
  186. CARBON_CHECK(state1.calls.size() == state2.calls.size(),
  187. "Number of specific calls expected to be the same.");
  188. for (auto [state1_call, state2_call] :
  189. llvm::zip(state1.calls, state2.calls)) {
  190. if (state1_call != state2_call) {
  191. if (ContainsPair(state1_call, state2_call, non_equivalent_specifics_)) {
  192. return false;
  193. }
  194. if (IsKnownEquivalence(state1_call, state2_call)) {
  195. continue;
  196. }
  197. if (!InsertPair(state1_call, state2_call,
  198. visited_equivalent_specifics)) {
  199. continue;
  200. }
  201. // Leave the added equivalence pair in place and continue.
  202. worklist.push_back({state1_call, state2_call});
  203. }
  204. }
  205. }
  206. return true;
  207. }
  208. auto SpecificCoalescer::InsertPair(
  209. SemIR::SpecificId specific_id1, SemIR::SpecificId specific_id2,
  210. Set<std::pair<SemIR::SpecificId, SemIR::SpecificId>>& set_of_pairs)
  211. -> bool {
  212. if (specific_id1.index > specific_id2.index) {
  213. std::swap(specific_id1.index, specific_id2.index);
  214. }
  215. auto insert_result =
  216. set_of_pairs.Insert(std::make_pair(specific_id1, specific_id2));
  217. return insert_result.is_inserted();
  218. }
  219. auto SpecificCoalescer::ContainsPair(
  220. SemIR::SpecificId specific_id1, SemIR::SpecificId specific_id2,
  221. const Set<std::pair<SemIR::SpecificId, SemIR::SpecificId>>& set_of_pairs)
  222. -> bool {
  223. if (specific_id1.index > specific_id2.index) {
  224. std::swap(specific_id1.index, specific_id2.index);
  225. }
  226. return set_of_pairs.Contains(std::make_pair(specific_id1, specific_id2));
  227. }
  228. } // namespace Carbon::Lower