Skip to content

Commit e4369aa

Browse files
committed
Merge branch 'main' of https://github.com/llvm/llvm-project into implicit-binding-constructor
2 parents 2ff496b + a3ba00a commit e4369aa

File tree

1,140 files changed

+686100
-216446
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

1,140 files changed

+686100
-216446
lines changed

bolt/include/bolt/Profile/DataAggregator.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -212,11 +212,6 @@ class DataAggregator : public DataReader {
212212
uint64_t NumTraces{0};
213213
uint64_t NumInvalidTraces{0};
214214
uint64_t NumLongRangeTraces{0};
215-
/// Specifies how many samples were recorded in cold areas if we are dealing
216-
/// with profiling data collected in a bolted binary. For LBRs, incremented
217-
/// for the source of the branch to avoid counting cold activity twice (one
218-
/// for source and another for destination).
219-
uint64_t NumColdSamples{0};
220215
uint64_t NumTotalSamples{0};
221216

222217
/// Looks into system PATH for Linux Perf and set up the aggregator to use it
@@ -473,7 +468,6 @@ class DataAggregator : public DataReader {
473468
void dump(const PerfMemSample &Sample) const;
474469

475470
/// Profile diagnostics print methods
476-
void printColdSamplesDiagnostic() const;
477471
void printLongRangeTracesDiagnostic() const;
478472
void printBranchSamplesDiagnostics() const;
479473
void printBasicSamplesDiagnostics(uint64_t OutOfRangeSamples) const;

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/include/bolt/Utils/CommandLineOpts.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,13 @@
1717

1818
namespace opts {
1919

20-
extern bool HeatmapMode;
20+
enum HeatmapModeKind {
21+
HM_None = 0,
22+
HM_Exclusive, // llvm-bolt-heatmap
23+
HM_Optional // perf2bolt --heatmap
24+
};
25+
26+
extern HeatmapModeKind HeatmapMode;
2127
extern bool BinaryAnalysisMode;
2228

2329
extern llvm::cl::OptionCategory BoltCategory;
@@ -45,6 +51,7 @@ extern llvm::cl::opt<unsigned> HeatmapBlock;
4551
extern llvm::cl::opt<unsigned long long> HeatmapMaxAddress;
4652
extern llvm::cl::opt<unsigned long long> HeatmapMinAddress;
4753
extern llvm::cl::opt<bool> HeatmapPrintMappings;
54+
extern llvm::cl::opt<std::string> HeatmapOutput;
4855
extern llvm::cl::opt<bool> HotData;
4956
extern llvm::cl::opt<bool> HotFunctionsAtEnd;
5057
extern llvm::cl::opt<bool> HotText;

bolt/lib/Core/BinaryFunction.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ extern cl::opt<bool> UpdateDebugSections;
6666
extern cl::opt<unsigned> Verbosity;
6767

6868
extern bool BinaryAnalysisMode;
69-
extern bool HeatmapMode;
69+
extern HeatmapModeKind HeatmapMode;
7070
extern bool processAllFunctions();
7171

7272
static cl::opt<bool> CheckEncoding(

bolt/lib/Profile/DataAggregator.cpp

Lines changed: 42 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,10 @@ void DataAggregator::findPerfExecutable() {
164164
void DataAggregator::start() {
165165
outs() << "PERF2BOLT: Starting data aggregation job for " << Filename << "\n";
166166

167+
// Turn on heatmap building if requested by --heatmap flag.
168+
if (!opts::HeatmapMode && opts::HeatmapOutput.getNumOccurrences())
169+
opts::HeatmapMode = opts::HeatmapModeKind::HM_Optional;
170+
167171
// Don't launch perf for pre-aggregated files or when perf input is specified
168172
// by the user.
169173
if (opts::ReadPreAggregated || !opts::ReadPerfEvents.empty())
@@ -502,24 +506,25 @@ Error DataAggregator::preprocessProfile(BinaryContext &BC) {
502506
errs() << "PERF2BOLT: failed to parse samples\n";
503507

504508
// Special handling for memory events
505-
if (prepareToParse("mem events", MemEventsPPI, MemEventsErrorCallback))
506-
return Error::success();
507-
508-
if (const std::error_code EC = parseMemEvents())
509-
errs() << "PERF2BOLT: failed to parse memory events: " << EC.message()
510-
<< '\n';
509+
if (!prepareToParse("mem events", MemEventsPPI, MemEventsErrorCallback))
510+
if (const std::error_code EC = parseMemEvents())
511+
errs() << "PERF2BOLT: failed to parse memory events: " << EC.message()
512+
<< '\n';
511513

512514
deleteTempFiles();
513515

514516
heatmap:
515-
if (opts::HeatmapMode) {
516-
if (std::error_code EC = printLBRHeatMap()) {
517-
errs() << "ERROR: failed to print heat map: " << EC.message() << '\n';
518-
exit(1);
519-
}
520-
exit(0);
521-
}
522-
return Error::success();
517+
if (!opts::HeatmapMode)
518+
return Error::success();
519+
520+
if (std::error_code EC = printLBRHeatMap())
521+
return errorCodeToError(EC);
522+
523+
if (opts::HeatmapMode == opts::HeatmapModeKind::HM_Optional)
524+
return Error::success();
525+
526+
assert(opts::HeatmapMode == opts::HeatmapModeKind::HM_Exclusive);
527+
exit(0);
523528
}
524529

525530
Error DataAggregator::readProfile(BinaryContext &BC) {
@@ -635,8 +640,6 @@ bool DataAggregator::doBasicSample(BinaryFunction &OrigFunc, uint64_t Address,
635640

636641
BinaryFunction *ParentFunc = getBATParentFunction(OrigFunc);
637642
BinaryFunction &Func = ParentFunc ? *ParentFunc : OrigFunc;
638-
if (ParentFunc || (BAT && !BAT->isBATFunction(Func.getAddress())))
639-
NumColdSamples += Count;
640643
// Attach executed bytes to parent function in case of cold fragment.
641644
Func.SampleCountInBytes += Count * BlockSize;
642645

@@ -740,15 +743,10 @@ bool DataAggregator::doBranch(uint64_t From, uint64_t To, uint64_t Count,
740743
if (BAT)
741744
Addr = BAT->translate(Func->getAddress(), Addr, IsFrom);
742745

743-
BinaryFunction *ParentFunc = getBATParentFunction(*Func);
744-
if (IsFrom &&
745-
(ParentFunc || (BAT && !BAT->isBATFunction(Func->getAddress()))))
746-
NumColdSamples += Count;
747-
748-
if (!ParentFunc)
749-
return std::pair{Func, IsRet};
746+
if (BinaryFunction *ParentFunc = getBATParentFunction(*Func))
747+
Func = ParentFunc;
750748

751-
return std::pair{ParentFunc, IsRet};
749+
return std::pair{Func, IsRet};
752750
};
753751

754752
auto [FromFunc, IsReturn] = handleAddress(From, /*IsFrom*/ true);
@@ -1318,6 +1316,14 @@ std::error_code DataAggregator::printLBRHeatMap() {
13181316
}
13191317
Heatmap HM(opts::HeatmapBlock, opts::HeatmapMinAddress,
13201318
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());
13211327

13221328
if (!NumTotalSamples) {
13231329
if (opts::BasicAggregation) {
@@ -1351,15 +1357,14 @@ std::error_code DataAggregator::printLBRHeatMap() {
13511357
exit(1);
13521358
}
13531359

1354-
HM.print(opts::OutputFilename);
1355-
if (opts::OutputFilename == "-")
1356-
HM.printCDF(opts::OutputFilename);
1357-
else
1358-
HM.printCDF(opts::OutputFilename + ".csv");
1359-
if (opts::OutputFilename == "-")
1360-
HM.printSectionHotness(opts::OutputFilename);
1361-
else
1362-
HM.printSectionHotness(opts::OutputFilename + "-section-hotness.csv");
1360+
HM.print(opts::HeatmapOutput);
1361+
if (opts::HeatmapOutput == "-") {
1362+
HM.printCDF(opts::HeatmapOutput);
1363+
HM.printSectionHotness(opts::HeatmapOutput);
1364+
} else {
1365+
HM.printCDF(opts::HeatmapOutput + ".csv");
1366+
HM.printSectionHotness(opts::HeatmapOutput + "-section-hotness.csv");
1367+
}
13631368

13641369
return std::error_code();
13651370
}
@@ -1386,7 +1391,7 @@ void DataAggregator::parseLBRSample(const PerfBranchSample &Sample,
13861391
const uint64_t TraceTo = NextLBR->From;
13871392
const BinaryFunction *TraceBF =
13881393
getBinaryFunctionContainingAddress(TraceFrom);
1389-
if (opts::HeatmapMode) {
1394+
if (opts::HeatmapMode == opts::HeatmapModeKind::HM_Exclusive) {
13901395
FTInfo &Info = FallthroughLBRs[Trace(TraceFrom, TraceTo)];
13911396
++Info.InternCount;
13921397
} else if (TraceBF && TraceBF->containsAddress(TraceTo)) {
@@ -1424,7 +1429,7 @@ void DataAggregator::parseLBRSample(const PerfBranchSample &Sample,
14241429
NextLBR = &LBR;
14251430

14261431
// Record branches outside binary functions for heatmap.
1427-
if (opts::HeatmapMode) {
1432+
if (opts::HeatmapMode == opts::HeatmapModeKind::HM_Exclusive) {
14281433
TakenBranchInfo &Info = BranchLBRs[Trace(LBR.From, LBR.To)];
14291434
++Info.TakenCount;
14301435
continue;
@@ -1439,26 +1444,13 @@ void DataAggregator::parseLBRSample(const PerfBranchSample &Sample,
14391444
}
14401445
// Record LBR addresses not covered by fallthroughs (bottom-of-stack source
14411446
// and top-of-stack target) as basic samples for heatmap.
1442-
if (opts::HeatmapMode && !Sample.LBR.empty()) {
1447+
if (opts::HeatmapMode == opts::HeatmapModeKind::HM_Exclusive &&
1448+
!Sample.LBR.empty()) {
14431449
++BasicSamples[Sample.LBR.front().To];
14441450
++BasicSamples[Sample.LBR.back().From];
14451451
}
14461452
}
14471453

1448-
void DataAggregator::printColdSamplesDiagnostic() const {
1449-
if (NumColdSamples > 0) {
1450-
const float ColdSamples = NumColdSamples * 100.0f / NumTotalSamples;
1451-
outs() << "PERF2BOLT: " << NumColdSamples
1452-
<< format(" (%.1f%%)", ColdSamples)
1453-
<< " samples recorded in cold regions of split functions.\n";
1454-
if (ColdSamples > 5.0f)
1455-
outs()
1456-
<< "WARNING: The BOLT-processed binary where samples were collected "
1457-
"likely used bad data or your service observed a large shift in "
1458-
"profile. You may want to audit this\n";
1459-
}
1460-
}
1461-
14621454
void DataAggregator::printLongRangeTracesDiagnostic() const {
14631455
outs() << "PERF2BOLT: out of range traces involving unknown regions: "
14641456
<< NumLongRangeTraces;
@@ -1499,7 +1491,6 @@ void DataAggregator::printBranchSamplesDiagnostics() const {
14991491
"collection. The generated data may be ineffective for improving "
15001492
"performance\n\n";
15011493
printLongRangeTracesDiagnostic();
1502-
printColdSamplesDiagnostic();
15031494
}
15041495

15051496
void DataAggregator::printBasicSamplesDiagnostics(
@@ -1511,7 +1502,6 @@ void DataAggregator::printBasicSamplesDiagnostics(
15111502
"binary is probably not the same binary used during profiling "
15121503
"collection. The generated data may be ineffective for improving "
15131504
"performance\n\n";
1514-
printColdSamplesDiagnostic();
15151505
}
15161506

15171507
void DataAggregator::printBranchStacksDiagnostics(

bolt/lib/Profile/Heatmap.cpp

Lines changed: 35 additions & 14 deletions
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"
@@ -297,20 +298,25 @@ void Heatmap::printSectionHotness(StringRef FileName) const {
297298
void Heatmap::printSectionHotness(raw_ostream &OS) const {
298299
uint64_t NumTotalCounts = 0;
299300
StringMap<uint64_t> SectionHotness;
301+
StringMap<uint64_t> BucketUtilization;
300302
unsigned TextSectionIndex = 0;
301303

302304
if (TextSections.empty())
303305
return;
304306

305307
uint64_t UnmappedHotness = 0;
306308
auto RecordUnmappedBucket = [&](uint64_t Address, uint64_t Frequency) {
307-
errs() << "Couldn't map the address bucket [0x" << Twine::utohexstr(Address)
308-
<< ", 0x" << Twine::utohexstr(Address + BucketSize)
309-
<< "] containing " << Frequency
310-
<< " samples to a text section in the binary.";
309+
if (opts::Verbosity >= 1)
310+
errs() << "Couldn't map the address bucket [0x"
311+
<< Twine::utohexstr(Address) << ", 0x"
312+
<< Twine::utohexstr(Address + BucketSize) << "] containing "
313+
<< Frequency << " samples to a text section in the binary.";
311314
UnmappedHotness += Frequency;
312315
};
313316

317+
AddressRange HotTextRange(HotStart, HotEnd);
318+
StringRef HotTextName = "[hot text]";
319+
314320
for (const std::pair<const uint64_t, uint64_t> &KV : Map) {
315321
NumTotalCounts += KV.second;
316322
// We map an address bucket to the first section (lowest address)
@@ -325,23 +331,38 @@ void Heatmap::printSectionHotness(raw_ostream &OS) const {
325331
continue;
326332
}
327333
SectionHotness[TextSections[TextSectionIndex].Name] += KV.second;
334+
++BucketUtilization[TextSections[TextSectionIndex].Name];
335+
if (HotTextRange.contains(Address)) {
336+
SectionHotness[HotTextName] += KV.second;
337+
++BucketUtilization[HotTextName];
338+
}
328339
}
329340

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+
330346
assert(NumTotalCounts > 0 &&
331347
"total number of heatmap buckets should be greater than 0");
332348

333-
OS << "Section Name, Begin Address, End Address, Percentage Hotness\n";
334-
for (auto &TextSection : TextSections) {
335-
OS << TextSection.Name << ", 0x"
336-
<< Twine::utohexstr(TextSection.BeginAddress) << ", 0x"
337-
<< Twine::utohexstr(TextSection.EndAddress) << ", "
338-
<< format("%.4f",
339-
100.0 * SectionHotness[TextSection.Name] / NumTotalCounts)
340-
<< "\n";
349+
OS << "Section Name, Begin Address, End Address, Percentage Hotness, "
350+
<< "Utilization Pct, Partition Score\n";
351+
const uint64_t MappedCounts = NumTotalCounts - UnmappedHotness;
352+
for (const auto [Name, Begin, End] : Sections) {
353+
const float Hotness = 1. * SectionHotness[Name] / NumTotalCounts;
354+
const float MappedHotness =
355+
MappedCounts ? 1. * SectionHotness[Name] / MappedCounts : 0;
356+
const uint64_t NumBuckets =
357+
End / BucketSize + !!(End % BucketSize) - Begin / BucketSize;
358+
const float Utilization = 1. * BucketUtilization[Name] / NumBuckets;
359+
const float PartitionScore = MappedHotness * Utilization;
360+
OS << formatv("{0}, {1:x}, {2:x}, {3:f4}, {4:f4}, {5:f4}\n", Name, Begin,
361+
End, 100. * Hotness, 100. * Utilization, PartitionScore);
341362
}
342363
if (UnmappedHotness > 0)
343-
OS << "[unmapped], 0x0, 0x0, "
344-
<< format("%.4f", 100.0 * UnmappedHotness / NumTotalCounts) << "\n";
364+
OS << formatv("[unmapped], 0x0, 0x0, {0:f4}, 0, 0\n",
365+
100.0 * UnmappedHotness / NumTotalCounts);
345366
}
346367
} // namespace bolt
347368
} // namespace llvm

bolt/lib/Rewrite/RewriteInstance.cpp

Lines changed: 7 additions & 5 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);
@@ -1453,7 +1454,8 @@ void RewriteInstance::updateRtFiniReloc() {
14531454
}
14541455

14551456
void RewriteInstance::registerFragments() {
1456-
if (!BC->HasSplitFunctions)
1457+
if (!BC->HasSplitFunctions ||
1458+
opts::HeatmapMode == opts::HeatmapModeKind::HM_Exclusive)
14571459
return;
14581460

14591461
// Process fragments with ambiguous parents separately as they are typically a
@@ -1998,7 +2000,7 @@ Error RewriteInstance::readSpecialSections() {
19982000
BC->getUniqueSectionByName(BoltAddressTranslation::SECTION_NAME)) {
19992001
BC->HasBATSection = true;
20002002
// Do not read BAT when plotting a heatmap
2001-
if (!opts::HeatmapMode) {
2003+
if (opts::HeatmapMode != opts::HeatmapModeKind::HM_Exclusive) {
20022004
if (std::error_code EC = BAT->parse(BC->outs(), BATSec->getContents())) {
20032005
BC->errs() << "BOLT-ERROR: failed to parse BOLT address translation "
20042006
"table.\n";
@@ -2037,7 +2039,7 @@ Error RewriteInstance::readSpecialSections() {
20372039
}
20382040

20392041
// Force non-relocation mode for heatmap generation
2040-
if (opts::HeatmapMode)
2042+
if (opts::HeatmapMode == opts::HeatmapModeKind::HM_Exclusive)
20412043
BC->HasRelocations = false;
20422044

20432045
if (BC->HasRelocations)

bolt/lib/Utils/CommandLineOpts.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ const char *BoltRevision =
2828

2929
namespace opts {
3030

31-
bool HeatmapMode = false;
31+
HeatmapModeKind HeatmapMode = HM_None;
3232
bool BinaryAnalysisMode = false;
3333

3434
cl::OptionCategory BoltCategory("BOLT generic options");
@@ -124,6 +124,10 @@ cl::opt<bool> HeatmapPrintMappings(
124124
"sections (default false)"),
125125
cl::Optional, cl::cat(HeatmapCategory));
126126

127+
cl::opt<std::string> HeatmapOutput("heatmap",
128+
cl::desc("print heatmap to a given file"),
129+
cl::Optional, cl::cat(HeatmapCategory));
130+
127131
cl::opt<bool> HotData("hot-data",
128132
cl::desc("hot data symbols support (relocation mode)"),
129133
cl::cat(BoltCategory));

0 commit comments

Comments
 (0)