Skip to content

Commit 73be288

Browse files
author
spupyrev
committed
review 2
1 parent aa5b01d commit 73be288

File tree

4 files changed

+41
-44
lines changed

4 files changed

+41
-44
lines changed

llvm/include/llvm/Support/BalancedPartitioning.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,9 @@ class BPFunctionNode {
6161
using IDT = uint64_t;
6262
/// The type of UtilityNode
6363
struct UtilityNodeT {
64-
UtilityNodeT(uint32_t id, uint32_t weight = 1) : id(id), weight(weight) {}
65-
uint32_t id;
66-
uint32_t weight;
64+
UtilityNodeT(uint32_t Id, uint32_t Weight = 1) : Id(Id), Weight(Weight) {}
65+
uint32_t Id;
66+
uint32_t Weight;
6767
};
6868

6969
/// \param UtilityNodes the set of utility nodes (must be unique'd)

llvm/lib/ProfileData/InstrProf.cpp

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -905,6 +905,20 @@ void InstrProfRecord::addValueData(uint32_t ValueKind, uint32_t Site,
905905
ValueSites.emplace_back(VData, VData + N);
906906
}
907907

908+
// Deduplicate utility nodes for a given function.
909+
// TODO: One may experiment with accumulating the weights of duplicates.
910+
void sortAndDeduplicate(SmallVector<BPFunctionNode::UtilityNodeT, 4> &UNs) {
911+
using UtilityNodeT = BPFunctionNode::UtilityNodeT;
912+
llvm::sort(UNs, [](const UtilityNodeT &L, const UtilityNodeT &R) {
913+
return L.Id < R.Id;
914+
});
915+
UNs.erase(std::unique(UNs.begin(), UNs.end(),
916+
[](const UtilityNodeT &L, const UtilityNodeT &R) {
917+
return L.Id == R.Id;
918+
}),
919+
UNs.end());
920+
}
921+
908922
std::vector<BPFunctionNode> TemporalProfTraceTy::createBPFunctionNodes(
909923
ArrayRef<TemporalProfTraceTy> Traces) {
910924
using IDT = BPFunctionNode::IDT;
@@ -940,14 +954,7 @@ std::vector<BPFunctionNode> TemporalProfTraceTy::createBPFunctionNodes(
940954
std::vector<BPFunctionNode> Nodes;
941955
for (auto Id : FunctionIds) {
942956
auto &UNs = FuncGroups[Id];
943-
llvm::sort(UNs, [](const UtilityNodeT &L, const UtilityNodeT &R) {
944-
return L.id < R.id;
945-
});
946-
UNs.erase(std::unique(UNs.begin(), UNs.end(),
947-
[](const UtilityNodeT &L, const UtilityNodeT &R) {
948-
return L.id == R.id;
949-
}),
950-
UNs.end());
957+
sortAndDeduplicate(UNs);
951958
Nodes.emplace_back(Id, UNs);
952959
}
953960
return Nodes;

llvm/lib/Support/BalancedPartitioning.cpp

Lines changed: 22 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,10 @@
2020
using namespace llvm;
2121
#define DEBUG_TYPE "balanced-partitioning"
2222

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-
3223
void BPFunctionNode::dump(raw_ostream &OS) const {
3324
OS << "{ID=" << Id << " Utilities={";
3425
for (auto &N : UtilityNodes)
35-
OS << N.id << " ,";
26+
OS << N.Id << " ,";
3627
OS << "}";
3728
if (Bucket.has_value())
3829
OS << " Bucket=" << Bucket.value();
@@ -180,42 +171,41 @@ void BalancedPartitioning::runIterations(const FunctionNodeRange Nodes,
180171
unsigned RecDepth, unsigned LeftBucket,
181172
unsigned RightBucket,
182173
std::mt19937 &RNG) const {
183-
unsigned NumNodes = std::distance(Nodes.begin(), Nodes.end());
174+
// Count the degree of each utility node.
184175
DenseMap<uint32_t, unsigned> UtilityNodeIndex;
185176
for (auto &N : Nodes)
186177
for (auto &UN : N.UtilityNodes)
187-
++UtilityNodeIndex[UN.id];
178+
++UtilityNodeIndex[UN.Id];
188179
// 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());
190182
for (auto &N : Nodes)
191183
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;
194186
});
195187

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.
197189
UtilityNodeIndex.clear();
198190
for (auto &N : Nodes)
199191
for (auto &UN : N.UtilityNodes)
200-
UN.id = UtilityNodeIndex.insert({UN.id, UtilityNodeIndex.size()})
192+
UN.Id = UtilityNodeIndex.insert({UN.Id, UtilityNodeIndex.size()})
201193
.first->second;
202194

203-
// Initialize signatures
195+
// Initialize signatures.
204196
SignaturesT Signatures(/*Size=*/UtilityNodeIndex.size());
205197
for (auto &N : Nodes) {
206198
for (auto &UN : N.UtilityNodes) {
207-
assert(UN.id < Signatures.size());
199+
assert(UN.Id < Signatures.size());
208200
if (N.Bucket == LeftBucket)
209-
Signatures[UN.id].LeftCount++;
201+
Signatures[UN.Id].LeftCount++;
210202
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;
219209
}
220210
}
221211

@@ -306,14 +296,14 @@ bool BalancedPartitioning::moveFunctionNode(BPFunctionNode &N,
306296
// Update signatures and invalidate gain cache
307297
if (FromLeftToRight) {
308298
for (auto &UN : N.UtilityNodes) {
309-
auto &Signature = Signatures[UN.id];
299+
auto &Signature = Signatures[UN.Id];
310300
Signature.LeftCount--;
311301
Signature.RightCount++;
312302
Signature.CachedGainIsValid = false;
313303
}
314304
} else {
315305
for (auto &UN : N.UtilityNodes) {
316-
auto &Signature = Signatures[UN.id];
306+
auto &Signature = Signatures[UN.Id];
317307
Signature.LeftCount++;
318308
Signature.RightCount--;
319309
Signature.CachedGainIsValid = false;
@@ -342,8 +332,8 @@ float BalancedPartitioning::moveGain(const BPFunctionNode &N,
342332
const SignaturesT &Signatures) {
343333
float Gain = 0.f;
344334
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);
347337
return Gain;
348338
}
349339

llvm/unittests/ProfileData/BPFunctionNodeTest.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ void PrintTo(const BPFunctionNode &Node, std::ostream *OS) {
2525

2626
TEST(BPFunctionNodeTest, Basic) {
2727
auto UNIdsAre = [](auto... Ids) {
28-
return UnorderedElementsAre(Field("id", &BPFunctionNode::UtilityNodeT::id,
28+
return UnorderedElementsAre(Field("Id", &BPFunctionNode::UtilityNodeT::Id,
2929
std::forward<uint32_t>(Ids))...);
3030
};
3131
auto NodeIs = [](BPFunctionNode::IDT Id,

0 commit comments

Comments
 (0)