diff --git a/bolt/include/bolt/Profile/Heatmap.h b/bolt/include/bolt/Profile/Heatmap.h index fc1e2cd30011e..9813e7fed486d 100644 --- a/bolt/include/bolt/Profile/Heatmap.h +++ b/bolt/include/bolt/Profile/Heatmap.h @@ -52,6 +52,9 @@ class Heatmap { : BucketSize(BucketSize), MinAddress(MinAddress), MaxAddress(MaxAddress), TextSections(TextSections) {} + uint64_t HotStart{0}; + uint64_t HotEnd{0}; + inline bool ignoreAddress(uint64_t Address) const { return (Address > MaxAddress) || (Address < MinAddress); } diff --git a/bolt/lib/Profile/DataAggregator.cpp b/bolt/lib/Profile/DataAggregator.cpp index c7db9d262e942..6beb60741406e 100644 --- a/bolt/lib/Profile/DataAggregator.cpp +++ b/bolt/lib/Profile/DataAggregator.cpp @@ -1316,6 +1316,14 @@ std::error_code DataAggregator::printLBRHeatMap() { } Heatmap HM(opts::HeatmapBlock, opts::HeatmapMinAddress, opts::HeatmapMaxAddress, getTextSections(BC)); + auto getSymbolValue = [&](const MCSymbol *Symbol) -> uint64_t { + if (Symbol) + if (ErrorOr SymValue = BC->getSymbolValue(*Symbol)) + return SymValue.get(); + return 0; + }; + HM.HotStart = getSymbolValue(BC->getHotTextStartSymbol()); + HM.HotEnd = getSymbolValue(BC->getHotTextEndSymbol()); if (!NumTotalSamples) { if (opts::BasicAggregation) { diff --git a/bolt/lib/Profile/Heatmap.cpp b/bolt/lib/Profile/Heatmap.cpp index 003db3cc61137..c66c2e5487613 100644 --- a/bolt/lib/Profile/Heatmap.cpp +++ b/bolt/lib/Profile/Heatmap.cpp @@ -8,6 +8,7 @@ #include "bolt/Profile/Heatmap.h" #include "bolt/Utils/CommandLineOpts.h" +#include "llvm/ADT/AddressRanges.h" #include "llvm/ADT/StringMap.h" #include "llvm/ADT/Twine.h" #include "llvm/Support/Debug.h" @@ -313,6 +314,9 @@ void Heatmap::printSectionHotness(raw_ostream &OS) const { UnmappedHotness += Frequency; }; + AddressRange HotTextRange(HotStart, HotEnd); + StringRef HotTextName = "[hot text]"; + for (const std::pair &KV : Map) { NumTotalCounts += KV.second; // We map an address bucket to the first section (lowest address) @@ -328,15 +332,24 @@ void Heatmap::printSectionHotness(raw_ostream &OS) const { } SectionHotness[TextSections[TextSectionIndex].Name] += KV.second; ++BucketUtilization[TextSections[TextSectionIndex].Name]; + if (HotTextRange.contains(Address)) { + SectionHotness[HotTextName] += KV.second; + ++BucketUtilization[HotTextName]; + } } + std::vector Sections(TextSections); + // Append synthetic hot text section to TextSections + if (!HotTextRange.empty()) + Sections.emplace_back(SectionNameAndRange{HotTextName, HotStart, HotEnd}); + assert(NumTotalCounts > 0 && "total number of heatmap buckets should be greater than 0"); OS << "Section Name, Begin Address, End Address, Percentage Hotness, " << "Utilization Pct, Partition Score\n"; const uint64_t MappedCounts = NumTotalCounts - UnmappedHotness; - for (const auto [Name, Begin, End] : TextSections) { + for (const auto [Name, Begin, End] : Sections) { const float Hotness = 1. * SectionHotness[Name] / NumTotalCounts; const float MappedHotness = MappedCounts ? 1. * SectionHotness[Name] / MappedCounts : 0; diff --git a/bolt/lib/Rewrite/RewriteInstance.cpp b/bolt/lib/Rewrite/RewriteInstance.cpp index 614938d0e3b65..dd519431fb2e3 100644 --- a/bolt/lib/Rewrite/RewriteInstance.cpp +++ b/bolt/lib/Rewrite/RewriteInstance.cpp @@ -968,8 +968,9 @@ void RewriteInstance::discoverFileObjects() { continue; } - // Ignore input hot markers - if (SymName == "__hot_start" || SymName == "__hot_end") + // Ignore input hot markers unless in heatmap mode + if ((SymName == "__hot_start" || SymName == "__hot_end") && + !opts::HeatmapMode) continue; FileSymRefs.emplace(SymbolAddress, Symbol); diff --git a/bolt/test/X86/heatmap-preagg.test b/bolt/test/X86/heatmap-preagg.test index 702dc804f5133..306e74800a353 100644 --- a/bolt/test/X86/heatmap-preagg.test +++ b/bolt/test/X86/heatmap-preagg.test @@ -13,6 +13,7 @@ RUN: --reorder-functions=cdsort --enable-bat --dyno-stats --skip-funcs=main RUN: llvm-bolt-heatmap %t.out -o %t2 --pa -p %p/Inputs/blarge_new_bat.preagg.txt \ RUN: 2>&1 | FileCheck --check-prefix CHECK-HEATMAP-BAT %s RUN: FileCheck %s --check-prefix CHECK-SEC-HOT-BAT --input-file %t2-section-hotness.csv +RUN: llvm-nm -n %t.out | FileCheck %s --check-prefix=CHECK-HOT-SYMS CHECK-HEATMAP: PERF2BOLT: read 81 aggregated LBR entries 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. CHECK-SEC-HOT-BAT-NEXT: .fini, 0x401c28, 0x401c35, 0.0000, 0.0000, 0.0000 CHECK-SEC-HOT-BAT-NEXT: .text, 0x800000, 0x8002cc, 38.7595, 91.6667, 0.3553 CHECK-SEC-HOT-BAT-NEXT: .text.cold, 0x800300, 0x800415, 0.0000, 0.0000, 0.0000 +CHECK-SEC-HOT-BAT-NEXT: [hot text], 0x800000, 0x8002cc, 38.7595, 91.6667, 0.3553 +CHECK-HOT-SYMS: 800000 W __hot_start +CHECK-HOT-SYMS: 8002cc W __hot_end