Skip to content

BalancedPartitioning: minor updates #77568

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions llvm/lib/ProfileData/InstrProf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -921,24 +921,24 @@ std::vector<BPFunctionNode> TemporalProfTraceTy::createBPFunctionNodes(
if (Timestamp < Trace.FunctionNameRefs.size())
FunctionIds.insert(Trace.FunctionNameRefs[Timestamp]);

int N = std::ceil(std::log2(LargestTraceSize));
const int N = Log2_64(LargestTraceSize) + 1;

// TODO: We need to use the Trace.Weight field to give more weight to more
// important utilities
DenseMap<IDT, SmallVector<UtilityNodeT, 4>> FuncGroups;
for (size_t TraceIdx = 0; TraceIdx < Traces.size(); TraceIdx++) {
auto &Trace = Traces[TraceIdx].FunctionNameRefs;
for (size_t Timestamp = 0; Timestamp < Trace.size(); Timestamp++) {
for (int I = std::floor(std::log2(Timestamp + 1)); I < N; I++) {
auto &FunctionId = Trace[Timestamp];
for (int I = Log2_64(Timestamp + 1); I < N; I++) {
auto FunctionId = Trace[Timestamp];
UtilityNodeT GroupId = TraceIdx * N + I;
FuncGroups[FunctionId].push_back(GroupId);
}
}
}

std::vector<BPFunctionNode> Nodes;
for (auto &Id : FunctionIds) {
for (auto Id : FunctionIds) {
auto &UNs = FuncGroups[Id];
llvm::sort(UNs);
UNs.erase(std::unique(UNs.begin(), UNs.end()), UNs.end());
Expand Down
16 changes: 6 additions & 10 deletions llvm/lib/Support/BalancedPartitioning.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ void BalancedPartitioning::bisect(const FunctionNodeRange Nodes,
if (NumNodes <= 1 || RecDepth >= Config.SplitDepth) {
// We've reach the lowest level of the recursion tree. Fall back to the
// original order and assign to buckets.
llvm::stable_sort(Nodes, [](const auto &L, const auto &R) {
llvm::sort(Nodes, [](const auto &L, const auto &R) {
return L.InputOrderIndex < R.InputOrderIndex;
});
for (auto &N : Nodes)
Expand Down Expand Up @@ -167,26 +167,22 @@ void BalancedPartitioning::runIterations(const FunctionNodeRange Nodes,
unsigned RightBucket,
std::mt19937 &RNG) const {
unsigned NumNodes = std::distance(Nodes.begin(), Nodes.end());
DenseMap<BPFunctionNode::UtilityNodeT, unsigned> UtilityNodeDegree;
DenseMap<BPFunctionNode::UtilityNodeT, unsigned> UtilityNodeIndex;
for (auto &N : Nodes)
for (auto &UN : N.UtilityNodes)
++UtilityNodeDegree[UN];
++UtilityNodeIndex[UN];
// Remove utility nodes if they have just one edge or are connected to all
// functions
for (auto &N : Nodes)
llvm::erase_if(N.UtilityNodes, [&](auto &UN) {
return UtilityNodeDegree[UN] <= 1 || UtilityNodeDegree[UN] >= NumNodes;
return UtilityNodeIndex[UN] == 1 || UtilityNodeIndex[UN] == NumNodes;
});

// Renumber utility nodes so they can be used to index into Signatures
DenseMap<BPFunctionNode::UtilityNodeT, unsigned> UtilityNodeIndex;
for (auto &N : Nodes)
for (auto &UN : N.UtilityNodes)
if (!UtilityNodeIndex.count(UN))
UtilityNodeIndex[UN] = UtilityNodeIndex.size();
UtilityNodeIndex.clear();
for (auto &N : Nodes)
for (auto &UN : N.UtilityNodes)
UN = UtilityNodeIndex[UN];
UN = UtilityNodeIndex.insert({UN, UtilityNodeIndex.size()}).first->second;

// Initialize signatures
SignaturesT Signatures(/*Size=*/UtilityNodeIndex.size());
Expand Down
17 changes: 12 additions & 5 deletions llvm/unittests/ProfileData/BPFunctionNodeTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,25 @@ void PrintTo(const BPFunctionNode &Node, std::ostream *OS) {
}

TEST(BPFunctionNodeTest, Basic) {
auto Nodes = TemporalProfTraceTy::createBPFunctionNodes({
TemporalProfTraceTy({0, 1, 2, 3, 4}),
TemporalProfTraceTy({4, 2}),
});

auto NodeIs = [](BPFunctionNode::IDT Id,
ArrayRef<BPFunctionNode::UtilityNodeT> UNs) {
return AllOf(Field("Id", &BPFunctionNode::Id, Id),
Field("UtilityNodes", &BPFunctionNode::UtilityNodes,
UnorderedElementsAreArray(UNs)));
};

auto Nodes = TemporalProfTraceTy::createBPFunctionNodes({
TemporalProfTraceTy({0, 1, 2, 3}),
});
EXPECT_THAT(Nodes,
UnorderedElementsAre(NodeIs(0, {0, 1, 2}), NodeIs(1, {1, 2}),
NodeIs(2, {1, 2}), NodeIs(3, {2})));

Nodes = TemporalProfTraceTy::createBPFunctionNodes({
TemporalProfTraceTy({0, 1, 2, 3, 4}),
TemporalProfTraceTy({4, 2}),
});

EXPECT_THAT(Nodes,
UnorderedElementsAre(NodeIs(0, {0, 1, 2}), NodeIs(1, {1, 2}),
NodeIs(2, {1, 2, 4, 5}), NodeIs(3, {2}),
Expand Down