| 1114 | using PartitionedSubscripts = |
| 1115 | std::vector<std::set<std::pair<Instruction*, Instruction*>>>; |
| 1116 | PartitionedSubscripts LoopDependenceAnalysis::PartitionSubscripts( |
| 1117 | const std::vector<Instruction*>& source_subscripts, |
| 1118 | const std::vector<Instruction*>& destination_subscripts) { |
| 1119 | PartitionedSubscripts partitions{}; |
| 1120 | |
| 1121 | auto num_subscripts = source_subscripts.size(); |
| 1122 | |
| 1123 | // Create initial partitions with one subscript pair per partition. |
| 1124 | for (size_t i = 0; i < num_subscripts; ++i) { |
| 1125 | partitions.push_back({{source_subscripts[i], destination_subscripts[i]}}); |
| 1126 | } |
| 1127 | |
| 1128 | // Iterate over the loops to create all partitions |
| 1129 | for (auto loop : loops_) { |
| 1130 | int64_t k = -1; |
| 1131 | |
| 1132 | for (size_t j = 0; j < partitions.size(); ++j) { |
| 1133 | auto& current_partition = partitions[j]; |
| 1134 | |
| 1135 | // Does |loop| appear in |current_partition| |
| 1136 | auto it = std::find_if( |
| 1137 | current_partition.begin(), current_partition.end(), |
| 1138 | [loop, |
| 1139 | this](const std::pair<Instruction*, Instruction*>& elem) -> bool { |
| 1140 | auto source_recurrences = |
| 1141 | scalar_evolution_.AnalyzeInstruction(std::get<0>(elem)) |
| 1142 | ->CollectRecurrentNodes(); |
| 1143 | auto destination_recurrences = |
| 1144 | scalar_evolution_.AnalyzeInstruction(std::get<1>(elem)) |
| 1145 | ->CollectRecurrentNodes(); |
| 1146 | |
| 1147 | source_recurrences.insert(source_recurrences.end(), |
| 1148 | destination_recurrences.begin(), |
| 1149 | destination_recurrences.end()); |
| 1150 | |
| 1151 | auto loops_in_pair = CollectLoops(source_recurrences); |
| 1152 | auto end_it = loops_in_pair.end(); |
| 1153 | |
| 1154 | return std::find(loops_in_pair.begin(), end_it, loop) != end_it; |
| 1155 | }); |
| 1156 | |
| 1157 | auto has_loop = it != current_partition.end(); |
| 1158 | |
| 1159 | if (has_loop) { |
| 1160 | if (k == -1) { |
| 1161 | k = j; |
| 1162 | } else { |
| 1163 | // Add |partitions[j]| to |partitions[k]| and discard |partitions[j]| |
| 1164 | partitions[static_cast<size_t>(k)].insert(current_partition.begin(), |
| 1165 | current_partition.end()); |
| 1166 | current_partition.clear(); |
| 1167 | } |
| 1168 | } |
| 1169 | } |
| 1170 | } |
| 1171 | |
| 1172 | // Remove empty (discarded) partitions |
| 1173 | partitions.erase( |
no test coverage detected