|
20 | 20 | using namespace llvm;
|
21 | 21 | #define DEBUG_TYPE "balanced-partitioning"
|
22 | 22 |
|
23 |
| -namespace llvm { |
24 |
| -template <> struct format_provider<BPFunctionNode::UtilityNodeT> { |
25 |
| - static void format(const BPFunctionNode::UtilityNodeT &V, raw_ostream &OS, |
26 |
| - StringRef Style) { |
27 |
| - OS << "(" << V.id << "-" << V.weight << ")"; |
28 |
| - } |
29 |
| -}; |
30 |
| -} // namespace llvm |
31 |
| - |
32 | 23 | void BPFunctionNode::dump(raw_ostream &OS) const {
|
33 | 24 | OS << "{ID=" << Id << " Utilities={";
|
34 | 25 | for (auto &N : UtilityNodes)
|
35 |
| - OS << N.id << " ,"; |
| 26 | + OS << N.Id << " ,"; |
36 | 27 | OS << "}";
|
37 | 28 | if (Bucket.has_value())
|
38 | 29 | OS << " Bucket=" << Bucket.value();
|
@@ -180,42 +171,41 @@ void BalancedPartitioning::runIterations(const FunctionNodeRange Nodes,
|
180 | 171 | unsigned RecDepth, unsigned LeftBucket,
|
181 | 172 | unsigned RightBucket,
|
182 | 173 | std::mt19937 &RNG) const {
|
183 |
| - unsigned NumNodes = std::distance(Nodes.begin(), Nodes.end()); |
| 174 | + // Count the degree of each utility node. |
184 | 175 | DenseMap<uint32_t, unsigned> UtilityNodeIndex;
|
185 | 176 | for (auto &N : Nodes)
|
186 | 177 | for (auto &UN : N.UtilityNodes)
|
187 |
| - ++UtilityNodeIndex[UN.id]; |
| 178 | + ++UtilityNodeIndex[UN.Id]; |
188 | 179 | // Remove utility nodes if they have just one edge or are connected to all
|
189 |
| - // functions |
| 180 | + // functions. |
| 181 | + unsigned NumNodes = std::distance(Nodes.begin(), Nodes.end()); |
190 | 182 | for (auto &N : Nodes)
|
191 | 183 | llvm::erase_if(N.UtilityNodes, [&](auto &UN) {
|
192 |
| - return UtilityNodeIndex[UN.id] == 1 || |
193 |
| - UtilityNodeIndex[UN.id] == NumNodes; |
| 184 | + return UtilityNodeIndex[UN.Id] == 1 || |
| 185 | + UtilityNodeIndex[UN.Id] == NumNodes; |
194 | 186 | });
|
195 | 187 |
|
196 |
| - // Renumber utility nodes so they can be used to index into Signatures |
| 188 | + // Renumber utility nodes so they can be used to index into Signatures. |
197 | 189 | UtilityNodeIndex.clear();
|
198 | 190 | for (auto &N : Nodes)
|
199 | 191 | for (auto &UN : N.UtilityNodes)
|
200 |
| - UN.id = UtilityNodeIndex.insert({UN.id, UtilityNodeIndex.size()}) |
| 192 | + UN.Id = UtilityNodeIndex.insert({UN.Id, UtilityNodeIndex.size()}) |
201 | 193 | .first->second;
|
202 | 194 |
|
203 |
| - // Initialize signatures |
| 195 | + // Initialize signatures. |
204 | 196 | SignaturesT Signatures(/*Size=*/UtilityNodeIndex.size());
|
205 | 197 | for (auto &N : Nodes) {
|
206 | 198 | for (auto &UN : N.UtilityNodes) {
|
207 |
| - assert(UN.id < Signatures.size()); |
| 199 | + assert(UN.Id < Signatures.size()); |
208 | 200 | if (N.Bucket == LeftBucket)
|
209 |
| - Signatures[UN.id].LeftCount++; |
| 201 | + Signatures[UN.Id].LeftCount++; |
210 | 202 | else
|
211 |
| - Signatures[UN.id].RightCount++; |
212 |
| - // Identical utility nodes (having the same UN.id) are guaranteed to have |
213 |
| - // the same weight; thus, we can get a new weight only when the signature |
214 |
| - // is uninitialized. |
215 |
| - if (Signatures[UN.id].Weight != UN.weight) { |
216 |
| - assert(Signatures[UN.id].Weight == 1); |
217 |
| - Signatures[UN.id].Weight = UN.weight; |
218 |
| - } |
| 203 | + Signatures[UN.Id].RightCount++; |
| 204 | + // Identical utility nodes (having the same UN.Id) have the same weight |
| 205 | + // (unless there are hash collisions mapping utilities to the same Id); |
| 206 | + // thus, we can get a new weight only when the signature is uninitialized. |
| 207 | + if (Signatures[UN.Id].Weight != UN.Weight) |
| 208 | + Signatures[UN.Id].Weight = UN.Weight; |
219 | 209 | }
|
220 | 210 | }
|
221 | 211 |
|
@@ -306,14 +296,14 @@ bool BalancedPartitioning::moveFunctionNode(BPFunctionNode &N,
|
306 | 296 | // Update signatures and invalidate gain cache
|
307 | 297 | if (FromLeftToRight) {
|
308 | 298 | for (auto &UN : N.UtilityNodes) {
|
309 |
| - auto &Signature = Signatures[UN.id]; |
| 299 | + auto &Signature = Signatures[UN.Id]; |
310 | 300 | Signature.LeftCount--;
|
311 | 301 | Signature.RightCount++;
|
312 | 302 | Signature.CachedGainIsValid = false;
|
313 | 303 | }
|
314 | 304 | } else {
|
315 | 305 | for (auto &UN : N.UtilityNodes) {
|
316 |
| - auto &Signature = Signatures[UN.id]; |
| 306 | + auto &Signature = Signatures[UN.Id]; |
317 | 307 | Signature.LeftCount++;
|
318 | 308 | Signature.RightCount--;
|
319 | 309 | Signature.CachedGainIsValid = false;
|
@@ -342,8 +332,8 @@ float BalancedPartitioning::moveGain(const BPFunctionNode &N,
|
342 | 332 | const SignaturesT &Signatures) {
|
343 | 333 | float Gain = 0.f;
|
344 | 334 | for (auto &UN : N.UtilityNodes)
|
345 |
| - Gain += (FromLeftToRight ? Signatures[UN.id].CachedGainLR |
346 |
| - : Signatures[UN.id].CachedGainRL); |
| 335 | + Gain += (FromLeftToRight ? Signatures[UN.Id].CachedGainLR |
| 336 | + : Signatures[UN.Id].CachedGainRL); |
347 | 337 | return Gain;
|
348 | 338 | }
|
349 | 339 |
|
|
0 commit comments