Skip to content

Commit 9d5d715

Browse files
authored
[BOLT][heatmap] Add synthetic hot text section (#139824)
In heatmap mode, report samples and utilization of the section(s) between hot text markers `[__hot_start, __hot_end)`. The intended use is with multi-way splitting where there are several sections that contain "hot" code (e.g. `.text.warm` with CDSplit). Addresses the comment on #139193 #139193 (review) Test Plan: updated heatmap-preagg.test
1 parent 26fbbfd commit 9d5d715

File tree

5 files changed

+32
-3
lines changed

5 files changed

+32
-3
lines changed

bolt/include/bolt/Profile/Heatmap.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ class Heatmap {
5252
: BucketSize(BucketSize), MinAddress(MinAddress), MaxAddress(MaxAddress),
5353
TextSections(TextSections) {}
5454

55+
uint64_t HotStart{0};
56+
uint64_t HotEnd{0};
57+
5558
inline bool ignoreAddress(uint64_t Address) const {
5659
return (Address > MaxAddress) || (Address < MinAddress);
5760
}

bolt/lib/Profile/DataAggregator.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1316,6 +1316,14 @@ std::error_code DataAggregator::printLBRHeatMap() {
13161316
}
13171317
Heatmap HM(opts::HeatmapBlock, opts::HeatmapMinAddress,
13181318
opts::HeatmapMaxAddress, getTextSections(BC));
1319+
auto getSymbolValue = [&](const MCSymbol *Symbol) -> uint64_t {
1320+
if (Symbol)
1321+
if (ErrorOr<uint64_t> SymValue = BC->getSymbolValue(*Symbol))
1322+
return SymValue.get();
1323+
return 0;
1324+
};
1325+
HM.HotStart = getSymbolValue(BC->getHotTextStartSymbol());
1326+
HM.HotEnd = getSymbolValue(BC->getHotTextEndSymbol());
13191327

13201328
if (!NumTotalSamples) {
13211329
if (opts::BasicAggregation) {

bolt/lib/Profile/Heatmap.cpp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#include "bolt/Profile/Heatmap.h"
1010
#include "bolt/Utils/CommandLineOpts.h"
11+
#include "llvm/ADT/AddressRanges.h"
1112
#include "llvm/ADT/StringMap.h"
1213
#include "llvm/ADT/Twine.h"
1314
#include "llvm/Support/Debug.h"
@@ -313,6 +314,9 @@ void Heatmap::printSectionHotness(raw_ostream &OS) const {
313314
UnmappedHotness += Frequency;
314315
};
315316

317+
AddressRange HotTextRange(HotStart, HotEnd);
318+
StringRef HotTextName = "[hot text]";
319+
316320
for (const std::pair<const uint64_t, uint64_t> &KV : Map) {
317321
NumTotalCounts += KV.second;
318322
// We map an address bucket to the first section (lowest address)
@@ -328,15 +332,24 @@ void Heatmap::printSectionHotness(raw_ostream &OS) const {
328332
}
329333
SectionHotness[TextSections[TextSectionIndex].Name] += KV.second;
330334
++BucketUtilization[TextSections[TextSectionIndex].Name];
335+
if (HotTextRange.contains(Address)) {
336+
SectionHotness[HotTextName] += KV.second;
337+
++BucketUtilization[HotTextName];
338+
}
331339
}
332340

341+
std::vector<SectionNameAndRange> Sections(TextSections);
342+
// Append synthetic hot text section to TextSections
343+
if (!HotTextRange.empty())
344+
Sections.emplace_back(SectionNameAndRange{HotTextName, HotStart, HotEnd});
345+
333346
assert(NumTotalCounts > 0 &&
334347
"total number of heatmap buckets should be greater than 0");
335348

336349
OS << "Section Name, Begin Address, End Address, Percentage Hotness, "
337350
<< "Utilization Pct, Partition Score\n";
338351
const uint64_t MappedCounts = NumTotalCounts - UnmappedHotness;
339-
for (const auto [Name, Begin, End] : TextSections) {
352+
for (const auto [Name, Begin, End] : Sections) {
340353
const float Hotness = 1. * SectionHotness[Name] / NumTotalCounts;
341354
const float MappedHotness =
342355
MappedCounts ? 1. * SectionHotness[Name] / MappedCounts : 0;

bolt/lib/Rewrite/RewriteInstance.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -968,8 +968,9 @@ void RewriteInstance::discoverFileObjects() {
968968
continue;
969969
}
970970

971-
// Ignore input hot markers
972-
if (SymName == "__hot_start" || SymName == "__hot_end")
971+
// Ignore input hot markers unless in heatmap mode
972+
if ((SymName == "__hot_start" || SymName == "__hot_end") &&
973+
!opts::HeatmapMode)
973974
continue;
974975

975976
FileSymRefs.emplace(SymbolAddress, Symbol);

bolt/test/X86/heatmap-preagg.test

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ RUN: --reorder-functions=cdsort --enable-bat --dyno-stats --skip-funcs=main
1313
RUN: llvm-bolt-heatmap %t.out -o %t2 --pa -p %p/Inputs/blarge_new_bat.preagg.txt \
1414
RUN: 2>&1 | FileCheck --check-prefix CHECK-HEATMAP-BAT %s
1515
RUN: FileCheck %s --check-prefix CHECK-SEC-HOT-BAT --input-file %t2-section-hotness.csv
16+
RUN: llvm-nm -n %t.out | FileCheck %s --check-prefix=CHECK-HOT-SYMS
1617

1718
CHECK-HEATMAP: PERF2BOLT: read 81 aggregated LBR entries
1819
CHECK-HEATMAP: HEATMAP: invalid traces: 1
@@ -33,3 +34,6 @@ CHECK-SEC-HOT-BAT-NEXT: .bolt.org.text, 0x4010b0, 0x401c25, 38.3385, 51.0638, 0.
3334
CHECK-SEC-HOT-BAT-NEXT: .fini, 0x401c28, 0x401c35, 0.0000, 0.0000, 0.0000
3435
CHECK-SEC-HOT-BAT-NEXT: .text, 0x800000, 0x8002cc, 38.7595, 91.6667, 0.3553
3536
CHECK-SEC-HOT-BAT-NEXT: .text.cold, 0x800300, 0x800415, 0.0000, 0.0000, 0.0000
37+
CHECK-SEC-HOT-BAT-NEXT: [hot text], 0x800000, 0x8002cc, 38.7595, 91.6667, 0.3553
38+
CHECK-HOT-SYMS: 800000 W __hot_start
39+
CHECK-HOT-SYMS: 8002cc W __hot_end

0 commit comments

Comments
 (0)