Skip to content

Commit 5ee5d65

Browse files
committed
[BOLT] Extend calculateEmittedSize for Block Size Calculation
This commit modifies BinaryContext::calculateEmittedSize to update the BinaryBasicBlock::OutputAddressRange for each basic block in the input BF. The modification is done in place, where BB.OutputAddressRange.second less BB.OutputAddressRange.first now gives the emitted size of the basic block.
1 parent 2c87571 commit 5ee5d65

File tree

2 files changed

+32
-6
lines changed

2 files changed

+32
-6
lines changed

bolt/include/bolt/Core/BinaryContext.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1230,6 +1230,9 @@ class BinaryContext {
12301230
///
12311231
/// Return the pair where the first size is for the main part, and the second
12321232
/// size is for the cold one.
1233+
/// Modify BinaryBasicBlock::OutputAddressRange for each basic block in the
1234+
/// function in place so that BB.OutputAddressRange.second less
1235+
/// BB.OutputAddressRange.first gives the emitted size of BB.
12331236
std::pair<size_t, size_t> calculateEmittedSize(BinaryFunction &BF,
12341237
bool FixBranches = true);
12351238

bolt/lib/Core/BinaryContext.cpp

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2331,14 +2331,37 @@ BinaryContext::calculateEmittedSize(BinaryFunction &BF, bool FixBranches) {
23312331
MCAsmLayout Layout(Assembler);
23322332
Assembler.layout(Layout);
23332333

2334+
// Obtain fragment sizes.
2335+
std::vector<uint64_t> FragmentSizes;
2336+
// Main fragment size.
23342337
const uint64_t HotSize =
23352338
Layout.getSymbolOffset(*EndLabel) - Layout.getSymbolOffset(*StartLabel);
2336-
const uint64_t ColdSize =
2337-
std::accumulate(SplitLabels.begin(), SplitLabels.end(), 0ULL,
2338-
[&](const uint64_t Accu, const LabelRange &Labels) {
2339-
return Accu + Layout.getSymbolOffset(*Labels.second) -
2340-
Layout.getSymbolOffset(*Labels.first);
2341-
});
2339+
FragmentSizes.push_back(HotSize);
2340+
// Split fragment sizes.
2341+
uint64_t ColdSize = 0;
2342+
for (const auto &Labels : SplitLabels) {
2343+
uint64_t Size = Layout.getSymbolOffset(*Labels.second) -
2344+
Layout.getSymbolOffset(*Labels.first);
2345+
FragmentSizes.push_back(Size);
2346+
ColdSize += Size;
2347+
}
2348+
2349+
// Populate new start and end offsets of each basic block.
2350+
BinaryBasicBlock *PrevBB = nullptr;
2351+
uint64_t FragmentIndex = 0;
2352+
for (FunctionFragment &FF : BF.getLayout().fragments()) {
2353+
for (BinaryBasicBlock *BB : FF) {
2354+
const uint64_t BBStartOffset = Layout.getSymbolOffset(*(BB->getLabel()));
2355+
BB->setOutputStartAddress(BBStartOffset);
2356+
if (PrevBB)
2357+
PrevBB->setOutputEndAddress(BBStartOffset);
2358+
PrevBB = BB;
2359+
}
2360+
if (PrevBB)
2361+
PrevBB->setOutputEndAddress(FragmentSizes[FragmentIndex]);
2362+
FragmentIndex++;
2363+
PrevBB = nullptr;
2364+
}
23422365

23432366
// Clean-up the effect of the code emission.
23442367
for (const MCSymbol &Symbol : Assembler.symbols()) {

0 commit comments

Comments
 (0)