Skip to content

[CodeLayout] Pre-process execution counts before layout #70501

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
Nov 2, 2023
Merged
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
27 changes: 17 additions & 10 deletions llvm/lib/Transforms/Utils/CodeLayout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -614,7 +614,7 @@ class ExtTSPImpl {
void initialize(const ArrayRef<uint64_t> &NodeSizes,
const ArrayRef<uint64_t> &NodeCounts,
const ArrayRef<EdgeCount> &EdgeCounts) {
// Initialize nodes
// Initialize nodes.
AllNodes.reserve(NumNodes);
for (uint64_t Idx = 0; Idx < NumNodes; Idx++) {
uint64_t Size = std::max<uint64_t>(NodeSizes[Idx], 1ULL);
Expand All @@ -625,7 +625,7 @@ class ExtTSPImpl {
AllNodes.emplace_back(Idx, Size, ExecutionCount);
}

// Initialize jumps between nodes
// Initialize jumps between the nodes.
SuccNodes.resize(NumNodes);
PredNodes.resize(NumNodes);
std::vector<uint64_t> OutDegree(NumNodes, 0);
Expand All @@ -644,6 +644,9 @@ class ExtTSPImpl {
AllJumps.emplace_back(&PredNode, &SuccNode, Edge.count);
SuccNode.InJumps.push_back(&AllJumps.back());
PredNode.OutJumps.push_back(&AllJumps.back());
// Adjust execution counts.
PredNode.ExecutionCount = std::max(PredNode.ExecutionCount, Edge.count);
SuccNode.ExecutionCount = std::max(SuccNode.ExecutionCount, Edge.count);
}
}
for (JumpT &Jump : AllJumps) {
Expand All @@ -667,6 +670,7 @@ class ExtTSPImpl {
AllEdges.reserve(AllJumps.size());
for (NodeT &PredNode : AllNodes) {
for (JumpT *Jump : PredNode.OutJumps) {
assert(Jump->ExecutionCount > 0 && "incorrectly initialized jump");
NodeT *SuccNode = Jump->Target;
ChainEdge *CurEdge = PredNode.CurChain->getEdge(SuccNode->CurChain);
// This edge is already present in the graph.
Expand Down Expand Up @@ -760,13 +764,13 @@ class ExtTSPImpl {
// Skip the merge if the ratio between the densities exceeds
// MaxMergeDensityRatio. Smaller values of the option result in fewer
// merges, and hence, more chains.
auto ChainPredDensity = ChainPred->density();
auto ChainSuccDensity = ChainSucc->density();
auto [minDensity, maxDensity] =
std::minmax(ChainPredDensity, ChainSuccDensity);
assert(minDensity > 0.0 && maxDensity > 0.0 &&
const double ChainPredDensity = ChainPred->density();
const double ChainSuccDensity = ChainSucc->density();
assert(ChainPredDensity > 0.0 && ChainSuccDensity > 0.0 &&
"incorrectly computed chain densities");
const double Ratio = maxDensity / minDensity;
auto [MinDensity, MaxDensity] =
std::minmax(ChainPredDensity, ChainSuccDensity);
const double Ratio = MaxDensity / MinDensity;
if (Ratio > MaxMergeDensityRatio)
continue;

Expand Down Expand Up @@ -1084,6 +1088,9 @@ class CDSortImpl {
AllJumps.back().Offset = EdgeOffsets[I];
SuccNode.InJumps.push_back(&AllJumps.back());
PredNode.OutJumps.push_back(&AllJumps.back());
// Adjust execution counts.
PredNode.ExecutionCount = std::max(PredNode.ExecutionCount, Count);
SuccNode.ExecutionCount = std::max(SuccNode.ExecutionCount, Count);
}
}

Expand All @@ -1104,13 +1111,13 @@ class CDSortImpl {
for (JumpT *Jump : PredNode.OutJumps) {
NodeT *SuccNode = Jump->Target;
ChainEdge *CurEdge = PredNode.CurChain->getEdge(SuccNode->CurChain);
// this edge is already present in the graph.
// This edge is already present in the graph.
if (CurEdge != nullptr) {
assert(SuccNode->CurChain->getEdge(PredNode.CurChain) != nullptr);
CurEdge->appendJump(Jump);
continue;
}
// this is a new edge.
// This is a new edge.
AllEdges.emplace_back(Jump);
PredNode.CurChain->addEdge(SuccNode->CurChain, &AllEdges.back());
SuccNode->CurChain->addEdge(PredNode.CurChain, &AllEdges.back());
Expand Down