Skip to content

Commit cebc837

Browse files
authored
[CodeLayout] Pre-process execution counts before layout (#70501)
BOLT fails to process binaries in non-LBR mode, as some blocks marked as having a zero execution count. Adjusting code layout to process such blocks without assertions. This is NFC for all other use cases.
1 parent 6acd167 commit cebc837

File tree

1 file changed

+17
-10
lines changed

1 file changed

+17
-10
lines changed

llvm/lib/Transforms/Utils/CodeLayout.cpp

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -614,7 +614,7 @@ class ExtTSPImpl {
614614
void initialize(const ArrayRef<uint64_t> &NodeSizes,
615615
const ArrayRef<uint64_t> &NodeCounts,
616616
const ArrayRef<EdgeCount> &EdgeCounts) {
617-
// Initialize nodes
617+
// Initialize nodes.
618618
AllNodes.reserve(NumNodes);
619619
for (uint64_t Idx = 0; Idx < NumNodes; Idx++) {
620620
uint64_t Size = std::max<uint64_t>(NodeSizes[Idx], 1ULL);
@@ -625,7 +625,7 @@ class ExtTSPImpl {
625625
AllNodes.emplace_back(Idx, Size, ExecutionCount);
626626
}
627627

628-
// Initialize jumps between nodes
628+
// Initialize jumps between the nodes.
629629
SuccNodes.resize(NumNodes);
630630
PredNodes.resize(NumNodes);
631631
std::vector<uint64_t> OutDegree(NumNodes, 0);
@@ -644,6 +644,9 @@ class ExtTSPImpl {
644644
AllJumps.emplace_back(&PredNode, &SuccNode, Edge.count);
645645
SuccNode.InJumps.push_back(&AllJumps.back());
646646
PredNode.OutJumps.push_back(&AllJumps.back());
647+
// Adjust execution counts.
648+
PredNode.ExecutionCount = std::max(PredNode.ExecutionCount, Edge.count);
649+
SuccNode.ExecutionCount = std::max(SuccNode.ExecutionCount, Edge.count);
647650
}
648651
}
649652
for (JumpT &Jump : AllJumps) {
@@ -667,6 +670,7 @@ class ExtTSPImpl {
667670
AllEdges.reserve(AllJumps.size());
668671
for (NodeT &PredNode : AllNodes) {
669672
for (JumpT *Jump : PredNode.OutJumps) {
673+
assert(Jump->ExecutionCount > 0 && "incorrectly initialized jump");
670674
NodeT *SuccNode = Jump->Target;
671675
ChainEdge *CurEdge = PredNode.CurChain->getEdge(SuccNode->CurChain);
672676
// This edge is already present in the graph.
@@ -760,13 +764,13 @@ class ExtTSPImpl {
760764
// Skip the merge if the ratio between the densities exceeds
761765
// MaxMergeDensityRatio. Smaller values of the option result in fewer
762766
// merges, and hence, more chains.
763-
auto ChainPredDensity = ChainPred->density();
764-
auto ChainSuccDensity = ChainSucc->density();
765-
auto [minDensity, maxDensity] =
766-
std::minmax(ChainPredDensity, ChainSuccDensity);
767-
assert(minDensity > 0.0 && maxDensity > 0.0 &&
767+
const double ChainPredDensity = ChainPred->density();
768+
const double ChainSuccDensity = ChainSucc->density();
769+
assert(ChainPredDensity > 0.0 && ChainSuccDensity > 0.0 &&
768770
"incorrectly computed chain densities");
769-
const double Ratio = maxDensity / minDensity;
771+
auto [MinDensity, MaxDensity] =
772+
std::minmax(ChainPredDensity, ChainSuccDensity);
773+
const double Ratio = MaxDensity / MinDensity;
770774
if (Ratio > MaxMergeDensityRatio)
771775
continue;
772776

@@ -1084,6 +1088,9 @@ class CDSortImpl {
10841088
AllJumps.back().Offset = EdgeOffsets[I];
10851089
SuccNode.InJumps.push_back(&AllJumps.back());
10861090
PredNode.OutJumps.push_back(&AllJumps.back());
1091+
// Adjust execution counts.
1092+
PredNode.ExecutionCount = std::max(PredNode.ExecutionCount, Count);
1093+
SuccNode.ExecutionCount = std::max(SuccNode.ExecutionCount, Count);
10871094
}
10881095
}
10891096

@@ -1104,13 +1111,13 @@ class CDSortImpl {
11041111
for (JumpT *Jump : PredNode.OutJumps) {
11051112
NodeT *SuccNode = Jump->Target;
11061113
ChainEdge *CurEdge = PredNode.CurChain->getEdge(SuccNode->CurChain);
1107-
// this edge is already present in the graph.
1114+
// This edge is already present in the graph.
11081115
if (CurEdge != nullptr) {
11091116
assert(SuccNode->CurChain->getEdge(PredNode.CurChain) != nullptr);
11101117
CurEdge->appendJump(Jump);
11111118
continue;
11121119
}
1113-
// this is a new edge.
1120+
// This is a new edge.
11141121
AllEdges.emplace_back(Jump);
11151122
PredNode.CurChain->addEdge(SuccNode->CurChain, &AllEdges.back());
11161123
SuccNode->CurChain->addEdge(PredNode.CurChain, &AllEdges.back());

0 commit comments

Comments
 (0)