Skip to content

Commit 2acb371

Browse files
committed
[𝘀𝗽𝗿] initial version
Created using spr 1.3.4
2 parents 258e143 + 6d49502 commit 2acb371

File tree

12 files changed

+188
-439
lines changed

12 files changed

+188
-439
lines changed

bolt/include/bolt/Core/BinaryFunction.h

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -386,8 +386,8 @@ class BinaryFunction {
386386
/// Profile match ratio.
387387
float ProfileMatchRatio{0.0f};
388388

389-
/// Raw branch count for this function in the profile.
390-
uint64_t RawBranchCount{0};
389+
/// Raw sample/branch count for this function in the profile.
390+
uint64_t RawSampleCount{0};
391391

392392
/// Dynamically executed function bytes, used for density computation.
393393
uint64_t SampleCountInBytes{0};
@@ -1880,13 +1880,12 @@ class BinaryFunction {
18801880
/// Return COUNT_NO_PROFILE if there's no profile info.
18811881
uint64_t getExecutionCount() const { return ExecutionCount; }
18821882

1883-
/// Return the raw profile information about the number of branch
1884-
/// executions corresponding to this function.
1885-
uint64_t getRawBranchCount() const { return RawBranchCount; }
1883+
/// Return the raw profile information about the number of samples (basic
1884+
/// profile) or branch executions (branch profile) recorded in this function.
1885+
uint64_t getRawSampleCount() const { return RawSampleCount; }
18861886

1887-
/// Set the profile data about the number of branch executions corresponding
1888-
/// to this function.
1889-
void setRawBranchCount(uint64_t Count) { RawBranchCount = Count; }
1887+
/// Set raw count of samples or branches recorded in this function.
1888+
void setRawSampleCount(uint64_t Count) { RawSampleCount = Count; }
18901889

18911890
/// Return the number of dynamically executed bytes, from raw perf data.
18921891
uint64_t getSampleCountInBytes() const { return SampleCountInBytes; }

bolt/include/bolt/Profile/DataAggregator.h

Lines changed: 17 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -92,46 +92,33 @@ class DataAggregator : public DataReader {
9292
uint64_t Addr;
9393
};
9494

95-
/// Used for parsing specific pre-aggregated input files.
96-
struct AggregatedLBREntry {
97-
enum Type : char { BRANCH = 0, FT, FT_EXTERNAL_ORIGIN, TRACE };
98-
Location From;
99-
Location To;
100-
uint64_t Count;
101-
uint64_t Mispreds;
102-
Type EntryType;
103-
};
104-
10595
struct Trace {
10696
uint64_t From;
10797
uint64_t To;
108-
Trace(uint64_t From, uint64_t To) : From(From), To(To) {}
109-
bool operator==(const Trace &Other) const {
110-
return From == Other.From && To == Other.To;
98+
uint64_t FallthroughEnd;
99+
bool operator==(const Trace &O) const {
100+
return From == O.From && To == O.To && FallthroughEnd == O.FallthroughEnd;
111101
}
112102
};
113103

114104
struct TraceHash {
115105
size_t operator()(const Trace &L) const {
116-
return std::hash<uint64_t>()(L.From << 32 | L.To);
106+
return llvm::hash_combine(L.From, L.To, L.FallthroughEnd);
117107
}
118108
};
119109

120-
struct FTInfo {
121-
uint64_t InternCount{0};
122-
uint64_t ExternCount{0};
123-
};
124-
125110
struct TakenBranchInfo {
126111
uint64_t TakenCount{0};
127112
uint64_t MispredCount{0};
113+
void operator+=(const TakenBranchInfo &O) {
114+
TakenCount += O.TakenCount;
115+
MispredCount += O.MispredCount;
116+
}
128117
};
129118

130119
/// Intermediate storage for profile data. We save the results of parsing
131120
/// and use them later for processing and assigning profile.
132-
std::unordered_map<Trace, TakenBranchInfo, TraceHash> BranchLBRs;
133-
std::unordered_map<Trace, FTInfo, TraceHash> FallthroughLBRs;
134-
std::vector<AggregatedLBREntry> AggregatedLBRs;
121+
std::unordered_map<Trace, TakenBranchInfo, TraceHash> Traces;
135122
std::unordered_map<uint64_t, uint64_t> BasicSamples;
136123
std::vector<PerfMemSample> MemSamples;
137124

@@ -197,19 +184,15 @@ class DataAggregator : public DataReader {
197184

198185
BoltAddressTranslation *BAT{nullptr};
199186

200-
/// Whether pre-aggregated profile needs to convert branch profile into call
201-
/// to continuation fallthrough profile.
202-
bool NeedsConvertRetProfileToCallCont{false};
203-
204187
/// Update function execution profile with a recorded trace.
205188
/// A trace is region of code executed between two LBR entries supplied in
206189
/// execution order.
207190
///
208191
/// Return a vector of offsets corresponding to a trace in a function
209192
/// if the trace is valid, std::nullopt otherwise.
210193
std::optional<SmallVector<std::pair<uint64_t, uint64_t>, 16>>
211-
getFallthroughsInTrace(BinaryFunction &BF, const LBREntry &First,
212-
const LBREntry &Second, uint64_t Count = 1) const;
194+
getFallthroughsInTrace(BinaryFunction &BF, const Trace &Trace,
195+
uint64_t Count) const;
213196

214197
/// Record external entry into the function \p BF.
215198
///
@@ -276,17 +259,16 @@ class DataAggregator : public DataReader {
276259
/// Register a \p Branch.
277260
bool doBranch(uint64_t From, uint64_t To, uint64_t Count, uint64_t Mispreds);
278261

279-
/// Register a trace between two LBR entries supplied in execution order.
280-
bool doTrace(const LBREntry &First, const LBREntry &Second,
281-
uint64_t Count = 1);
262+
/// Register a \p Trace.
263+
bool doTrace(const Trace &Trace, uint64_t Count);
282264

283265
/// Parser helpers
284266
/// Return false if we exhausted our parser buffer and finished parsing
285267
/// everything
286268
bool hasData() const { return !ParsingBuf.empty(); }
287269

288-
/// Print heat map based on LBR samples.
289-
std::error_code printLBRHeatMap();
270+
/// Print heat map based on collected samples.
271+
std::error_code printHeatMap();
290272

291273
/// Parse a single perf sample containing a PID associated with a sequence of
292274
/// LBR entries. If the PID does not correspond to the binary we are looking
@@ -330,7 +312,7 @@ class DataAggregator : public DataReader {
330312
ErrorOr<LBREntry> parseLBREntry();
331313

332314
/// Parse LBR sample.
333-
void parseLBRSample(const PerfBranchSample &Sample, bool NeedsSkylakeFix);
315+
void parseLBRSample(ArrayRef<LBREntry> Samples);
334316

335317
/// Parse and pre-aggregate branch events.
336318
std::error_code parseBranchEvents();
@@ -420,14 +402,7 @@ class DataAggregator : public DataReader {
420402
/// F 41be90 41be90 4
421403
/// B 4b1942 39b57f0 3 0
422404
/// B 4b196f 4b19e0 2 0
423-
void parsePreAggregated();
424-
425-
/// Parse the full output of pre-aggregated LBR samples generated by
426-
/// an external tool.
427-
std::error_code parsePreAggregatedLBRSamples();
428-
429-
/// Process parsed pre-aggregated data.
430-
void processPreAggregated();
405+
std::error_code parsePreAggregated();
431406

432407
/// If \p Address falls into the binary address space based on memory
433408
/// mapping info \p MMI, then adjust it for further processing by subtracting

bolt/include/bolt/Profile/DataReader.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,9 @@ struct FuncSampleData {
252252
/// Get the number of samples recorded in [Start, End)
253253
uint64_t getSamples(uint64_t Start, uint64_t End) const;
254254

255+
/// Returns the total number of samples recorded in this function.
256+
uint64_t getSamples() const;
257+
255258
/// Aggregation helper
256259
DenseMap<uint64_t, size_t> Index;
257260

bolt/include/bolt/Profile/Heatmap.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,9 @@ class Heatmap {
5757
}
5858

5959
/// Register a single sample at \p Address.
60-
void registerAddress(uint64_t Address) {
60+
void registerAddress(uint64_t Address, uint64_t Count) {
6161
if (!ignoreAddress(Address))
62-
++Map[Address / BucketSize];
62+
Map[Address / BucketSize] += Count;
6363
}
6464

6565
/// Register \p Count samples at [\p StartAddress, \p EndAddress ].

bolt/lib/Core/BinaryFunction.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -471,7 +471,7 @@ void BinaryFunction::print(raw_ostream &OS, std::string Annotation) {
471471
OS << "\n Image : 0x" << Twine::utohexstr(getImageAddress());
472472
if (ExecutionCount != COUNT_NO_PROFILE) {
473473
OS << "\n Exec Count : " << ExecutionCount;
474-
OS << "\n Branch Count: " << RawBranchCount;
474+
OS << "\n Branch Count: " << RawSampleCount;
475475
OS << "\n Profile Acc : " << format("%.1f%%", ProfileMatchRatio * 100.0f);
476476
}
477477

bolt/lib/Passes/BinaryPasses.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1445,7 +1445,7 @@ Error PrintProgramStats::runOnFunctions(BinaryContext &BC) {
14451445
if (!Function.hasProfile())
14461446
continue;
14471447

1448-
uint64_t SampleCount = Function.getRawBranchCount();
1448+
uint64_t SampleCount = Function.getRawSampleCount();
14491449
TotalSampleCount += SampleCount;
14501450

14511451
if (Function.hasValidProfile()) {

0 commit comments

Comments
 (0)