Skip to content

Commit 1b7d2ed

Browse files
committed
reduce changes
Created using spr 1.3.4
2 parents a594b23 + 545a98c commit 1b7d2ed

File tree

3 files changed

+26
-65
lines changed

3 files changed

+26
-65
lines changed

bolt/include/bolt/Profile/Heatmap.h

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
#ifndef BOLT_PROFILE_HEATMAP_H
1010
#define BOLT_PROFILE_HEATMAP_H
1111

12-
#include "llvm/ADT/StringMap.h"
1312
#include "llvm/ADT/StringRef.h"
1413
#include <cstdint>
1514
#include <map>
@@ -46,10 +45,6 @@ class Heatmap {
4645
/// Map section names to their address range.
4746
const std::vector<SectionNameAndRange> TextSections;
4847

49-
uint64_t getNumBuckets(uint64_t Begin, uint64_t End) const {
50-
return End / BucketSize + !!(End % BucketSize) - Begin / BucketSize;
51-
};
52-
5348
public:
5449
explicit Heatmap(uint64_t BucketSize = 4096, uint64_t MinAddress = 0,
5550
uint64_t MaxAddress = std::numeric_limits<uint64_t>::max(),
@@ -82,22 +77,9 @@ class Heatmap {
8277

8378
void printCDF(raw_ostream &OS) const;
8479

85-
/// Struct describing individual section hotness.
86-
struct SectionStats {
87-
uint64_t Samples{0};
88-
uint64_t Buckets{0};
89-
};
90-
91-
/// Mapping from section name to associated \p SectionStats. Special entries:
92-
/// - [total] for total stats,
93-
/// - [unmapped] for samples outside any section, if non-zero.
94-
using SectionStatsMap = StringMap<SectionStats>;
95-
96-
SectionStatsMap computeSectionStats() const;
97-
98-
void printSectionHotness(const SectionStatsMap &, StringRef Filename) const;
80+
void printSectionHotness(StringRef Filename) const;
9981

100-
void printSectionHotness(const SectionStatsMap &, raw_ostream &OS) const;
82+
void printSectionHotness(raw_ostream &OS) const;
10183

10284
size_t size() const { return Map.size(); }
10385
};

bolt/lib/Profile/DataAggregator.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1357,12 +1357,10 @@ std::error_code DataAggregator::printLBRHeatMap() {
13571357
HM.printCDF(opts::OutputFilename);
13581358
else
13591359
HM.printCDF(opts::OutputFilename + ".csv");
1360-
Heatmap::SectionStatsMap Stats = HM.computeSectionStats();
13611360
if (opts::OutputFilename == "-")
1362-
HM.printSectionHotness(Stats, opts::OutputFilename);
1361+
HM.printSectionHotness(opts::OutputFilename);
13631362
else
1364-
HM.printSectionHotness(Stats,
1365-
opts::OutputFilename + "-section-hotness.csv");
1363+
HM.printSectionHotness(opts::OutputFilename + "-section-hotness.csv");
13661364

13671365
return std::error_code();
13681366
}

bolt/lib/Profile/Heatmap.cpp

Lines changed: 22 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -284,32 +284,31 @@ void Heatmap::printCDF(raw_ostream &OS) const {
284284
Counts.clear();
285285
}
286286

287-
void Heatmap::printSectionHotness(const Heatmap::SectionStatsMap &Stats,
288-
StringRef FileName) const {
287+
void Heatmap::printSectionHotness(StringRef FileName) const {
289288
std::error_code EC;
290289
raw_fd_ostream OS(FileName, EC, sys::fs::OpenFlags::OF_None);
291290
if (EC) {
292291
errs() << "error opening output file: " << EC.message() << '\n';
293292
exit(1);
294293
}
295-
printSectionHotness(Stats, OS);
294+
printSectionHotness(OS);
296295
}
297296

298-
StringMap<Heatmap::SectionStats> Heatmap::computeSectionStats() const {
297+
void Heatmap::printSectionHotness(raw_ostream &OS) const {
299298
uint64_t NumTotalCounts = 0;
300-
StringMap<SectionStats> Stat;
299+
StringMap<uint64_t> SectionHotness;
300+
StringMap<uint64_t> SectionUtilization;
301301
unsigned TextSectionIndex = 0;
302302

303303
if (TextSections.empty())
304-
return Stat;
304+
return;
305305

306306
uint64_t UnmappedHotness = 0;
307307
auto RecordUnmappedBucket = [&](uint64_t Address, uint64_t Frequency) {
308-
if (opts::Verbosity >= 1)
309-
errs() << "Couldn't map the address bucket ["
310-
<< formatv("{0:x}, {1:x}", Address, Address + BucketSize)
311-
<< "] containing " << Frequency
312-
<< " samples to a text section in the binary.\n";
308+
errs() << "Couldn't map the address bucket [0x" << Twine::utohexstr(Address)
309+
<< ", 0x" << Twine::utohexstr(Address + BucketSize)
310+
<< "] containing " << Frequency
311+
<< " samples to a text section in the binary.";
313312
UnmappedHotness += Frequency;
314313
};
315314

@@ -326,48 +325,30 @@ StringMap<Heatmap::SectionStats> Heatmap::computeSectionStats() const {
326325
RecordUnmappedBucket(Address, Count);
327326
continue;
328327
}
329-
SectionStats &SecStats = Stat[TextSections[TextSectionIndex].Name];
330-
++SecStats.Buckets;
331-
SecStats.Samples += Count;
328+
StringRef Name = TextSections[TextSectionIndex].Name;
329+
SectionHotness[Name] += Count;
330+
++SectionUtilization[Name];
332331
}
333-
Stat["[total]"] = SectionStats{NumTotalCounts, Map.size()};
334-
if (UnmappedHotness)
335-
Stat["[unmapped]"] = SectionStats{UnmappedHotness, 0};
336-
337-
return Stat;
338-
}
339332

340-
void Heatmap::printSectionHotness(const StringMap<SectionStats> &Stats,
341-
raw_ostream &OS) const {
342-
if (TextSections.empty())
343-
return;
333+
auto getNumBuckets = [&](uint64_t Begin, uint64_t End) {
334+
return End / BucketSize + !!(End % BucketSize) - Begin / BucketSize;
335+
};
344336

345-
auto TotalIt = Stats.find("[total]");
346-
assert(TotalIt != Stats.end() && "Malformed SectionStatsMap");
347-
const uint64_t NumTotalCounts = TotalIt->second.Samples;
348337
assert(NumTotalCounts > 0 &&
349338
"total number of heatmap buckets should be greater than 0");
350339

351340
OS << "Section Name, Begin Address, End Address, Percentage Hotness, "
352341
<< "Utilization Pct\n";
353342
for (const auto [Name, Begin, End] : TextSections) {
354-
uint64_t Samples = 0;
355-
uint64_t Buckets = 0;
356-
auto SectionIt = Stats.find(Name);
357-
if (SectionIt != Stats.end()) {
358-
Samples = SectionIt->second.Samples;
359-
Buckets = SectionIt->second.Buckets;
360-
}
361-
const float RelHotness = 100. * Samples / NumTotalCounts;
362-
const float BucketUtilization = 100. * Buckets / getNumBuckets(Begin, End);
343+
const float RelHotness = 100. * SectionHotness[Name] / NumTotalCounts;
344+
const float BucketUtilization =
345+
100. * SectionUtilization[Name] / getNumBuckets(Begin, End);
363346
OS << formatv("{0}, {1:x}, {2:x}, {3:f4}, {4:f4}\n", Name, Begin, End,
364347
RelHotness, BucketUtilization);
365348
}
366-
auto UnmappedIt = Stats.find("[unmapped]");
367-
if (UnmappedIt == Stats.end())
368-
return;
369-
const float UnmappedPct = 100. * UnmappedIt->second.Samples / NumTotalCounts;
370-
OS << formatv("[unmapped], 0x0, 0x0, {0:f4}, 0\n", UnmappedPct);
349+
if (UnmappedHotness > 0)
350+
OS << formatv("[unmapped], 0x0, 0x0, {0:f4}, 0\n",
351+
100.0 * UnmappedHotness / NumTotalCounts);
371352
}
372353
} // namespace bolt
373354
} // namespace llvm

0 commit comments

Comments
 (0)