Skip to content

Commit e039d16

Browse files
authored
[BOLT][NFC] Disambiguate sample as basic sample (#139350)
Sample is a general term covering both basic (IP) and branch (LBR) profiles. Find and replace ambiguous uses of sample in a basic sample sense. Rename `RawBranchCount` into `RawSampleCount` reflecting its use for both kinds of profile. Rename `PF_LBR` profile type as `PF_BRANCH` reflecting non-LBR based branch profiles (non-brstack SPE, synthesized brstack ETM/PT). Follow-up to #137644. Test Plan: NFC
1 parent bf70f84 commit e039d16

16 files changed

+93
-88
lines changed

bolt/include/bolt/Core/BinaryFunction.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,8 @@ class BinaryFunction {
142142
/// Types of profile the function can use. Could be a combination.
143143
enum {
144144
PF_NONE = 0, /// No profile.
145-
PF_LBR = 1, /// Profile is based on last branch records.
146-
PF_SAMPLE = 2, /// Non-LBR sample-based profile.
145+
PF_BRANCH = 1, /// Profile is based on branches or branch stacks.
146+
PF_BASIC = 2, /// Non-branch IP sample-based profile.
147147
PF_MEMEVENT = 4, /// Profile has mem events.
148148
};
149149

@@ -392,7 +392,7 @@ class BinaryFunction {
392392
float ProfileMatchRatio{0.0f};
393393

394394
/// Raw branch count for this function in the profile.
395-
uint64_t RawBranchCount{0};
395+
uint64_t RawSampleCount{0};
396396

397397
/// Dynamically executed function bytes, used for density computation.
398398
uint64_t SampleCountInBytes{0};
@@ -1893,11 +1893,11 @@ class BinaryFunction {
18931893

18941894
/// Return the raw profile information about the number of branch
18951895
/// executions corresponding to this function.
1896-
uint64_t getRawBranchCount() const { return RawBranchCount; }
1896+
uint64_t getRawSampleCount() const { return RawSampleCount; }
18971897

18981898
/// Set the profile data about the number of branch executions corresponding
18991899
/// to this function.
1900-
void setRawBranchCount(uint64_t Count) { RawBranchCount = Count; }
1900+
void setRawSampleCount(uint64_t Count) { RawSampleCount = Count; }
19011901

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

bolt/include/bolt/Profile/DataAggregator.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,8 @@ class DataAggregator : public DataReader {
257257

258258
/// Semantic actions - parser hooks to interpret parsed perf samples
259259
/// Register a sample (non-LBR mode), i.e. a new hit at \p Address
260-
bool doSample(BinaryFunction &Func, const uint64_t Address, uint64_t Count);
260+
bool doBasicSample(BinaryFunction &Func, const uint64_t Address,
261+
uint64_t Count);
261262

262263
/// Register an intraprocedural branch \p Branch.
263264
bool doIntraBranch(BinaryFunction &Func, uint64_t From, uint64_t To,

bolt/include/bolt/Profile/DataReader.h

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -212,15 +212,16 @@ struct FuncMemData {
212212
/// Similar to BranchInfo, but instead of recording from-to address (an edge),
213213
/// it records the address of a perf event and the number of times samples hit
214214
/// this address.
215-
struct SampleInfo {
215+
struct BasicSampleInfo {
216216
Location Loc;
217217
int64_t Hits;
218218

219-
SampleInfo(Location Loc, int64_t Hits) : Loc(std::move(Loc)), Hits(Hits) {}
219+
BasicSampleInfo(Location Loc, int64_t Hits)
220+
: Loc(std::move(Loc)), Hits(Hits) {}
220221

221-
bool operator==(const SampleInfo &RHS) const { return Loc == RHS.Loc; }
222+
bool operator==(const BasicSampleInfo &RHS) const { return Loc == RHS.Loc; }
222223

223-
bool operator<(const SampleInfo &RHS) const {
224+
bool operator<(const BasicSampleInfo &RHS) const {
224225
if (Loc < RHS.Loc)
225226
return true;
226227

@@ -229,18 +230,18 @@ struct SampleInfo {
229230

230231
void print(raw_ostream &OS) const;
231232

232-
void mergeWith(const SampleInfo &SI);
233+
void mergeWith(const BasicSampleInfo &SI);
233234
};
234235

235236
/// Helper class to store samples recorded in the address space of a given
236237
/// function, analogous to FuncBranchData but for samples instead of branches.
237-
struct FuncSampleData {
238-
typedef std::vector<SampleInfo> ContainerTy;
238+
struct FuncBasicSampleData {
239+
typedef std::vector<BasicSampleInfo> ContainerTy;
239240

240241
StringRef Name;
241242
ContainerTy Data;
242243

243-
FuncSampleData(StringRef Name, ContainerTy Data)
244+
FuncBasicSampleData(StringRef Name, ContainerTy Data)
244245
: Name(Name), Data(std::move(Data)) {}
245246

246247
/// Get the number of samples recorded in [Start, End)
@@ -308,7 +309,7 @@ class DataReader : public ProfileReaderBase {
308309
/// The last step is to infer edge counts based on BB execution count. Note
309310
/// this is the opposite of the LBR way, where we infer BB execution count
310311
/// based on edge counts.
311-
void readSampleData(BinaryFunction &BF);
312+
void readBasicSampleData(BinaryFunction &BF);
312313

313314
/// Convert function-level branch data into instruction annotations.
314315
void convertBranchData(BinaryFunction &BF) const;
@@ -382,7 +383,8 @@ class DataReader : public ProfileReaderBase {
382383
/// Return mem data matching one of the names in \p FuncNames.
383384
FuncMemData *getMemDataForNames(const std::vector<StringRef> &FuncNames);
384385

385-
FuncSampleData *getFuncSampleData(const std::vector<StringRef> &FuncNames);
386+
FuncBasicSampleData *
387+
getFuncBasicSampleData(const std::vector<StringRef> &FuncNames);
386388

387389
/// Return a vector of all FuncBranchData matching the list of names.
388390
/// Internally use fuzzy matching to match special names like LTO-generated
@@ -425,7 +427,7 @@ class DataReader : public ProfileReaderBase {
425427
}
426428

427429
using NamesToBranchesMapTy = std::map<StringRef, FuncBranchData>;
428-
using NamesToSamplesMapTy = std::map<StringRef, FuncSampleData>;
430+
using NamesToBasicSamplesMapTy = std::map<StringRef, FuncBasicSampleData>;
429431
using NamesToMemEventsMapTy = std::map<StringRef, FuncMemData>;
430432
using FuncsToBranchesMapTy =
431433
std::unordered_map<const BinaryFunction *, FuncBranchData *>;
@@ -474,7 +476,7 @@ class DataReader : public ProfileReaderBase {
474476
return parseLocation(EndChar, EndNl, true);
475477
}
476478
ErrorOr<BranchInfo> parseBranchInfo();
477-
ErrorOr<SampleInfo> parseSampleInfo();
479+
ErrorOr<BasicSampleInfo> parseSampleInfo();
478480
ErrorOr<MemInfo> parseMemInfo();
479481
ErrorOr<bool> maybeParseNoLBRFlag();
480482
ErrorOr<bool> maybeParseBATFlag();
@@ -488,7 +490,7 @@ class DataReader : public ProfileReaderBase {
488490
unsigned Line{0};
489491
unsigned Col{0};
490492
NamesToBranchesMapTy NamesToBranches;
491-
NamesToSamplesMapTy NamesToSamples;
493+
NamesToBasicSamplesMapTy NamesToBasicSamples;
492494
NamesToMemEventsMapTy NamesToMemEvents;
493495
FuncsToBranchesMapTy FuncsToBranches;
494496
FuncsToMemDataMapTy FuncsToMemData;

bolt/include/bolt/Profile/ProfileYAMLMapping.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,8 +230,8 @@ LLVM_YAML_STRONG_TYPEDEF(uint16_t, PROFILE_PF)
230230

231231
template <> struct ScalarBitSetTraits<PROFILE_PF> {
232232
static void bitset(IO &io, PROFILE_PF &value) {
233-
io.bitSetCase(value, "lbr", BinaryFunction::PF_LBR);
234-
io.bitSetCase(value, "sample", BinaryFunction::PF_SAMPLE);
233+
io.bitSetCase(value, "lbr", BinaryFunction::PF_BRANCH);
234+
io.bitSetCase(value, "sample", BinaryFunction::PF_BASIC);
235235
io.bitSetCase(value, "memevent", BinaryFunction::PF_MEMEVENT);
236236
}
237237
};

bolt/lib/Core/BinaryFunction.cpp

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

bolt/lib/Core/BinaryFunctionProfile.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ void BinaryFunction::postProcessProfile() {
7070
return;
7171
}
7272

73-
if (!(getProfileFlags() & PF_LBR))
73+
if (!(getProfileFlags() & PF_BRANCH))
7474
return;
7575

7676
// If we have at least some branch data for the function indicate that it

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()) {

bolt/lib/Passes/MCF.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -458,15 +458,15 @@ void EstimateEdgeCounts::runOnFunction(BinaryFunction &BF) {
458458
Error EstimateEdgeCounts::runOnFunctions(BinaryContext &BC) {
459459
if (llvm::none_of(llvm::make_second_range(BC.getBinaryFunctions()),
460460
[](const BinaryFunction &BF) {
461-
return BF.getProfileFlags() == BinaryFunction::PF_SAMPLE;
461+
return BF.getProfileFlags() == BinaryFunction::PF_BASIC;
462462
}))
463463
return Error::success();
464464

465465
ParallelUtilities::WorkFuncTy WorkFun = [&](BinaryFunction &BF) {
466466
runOnFunction(BF);
467467
};
468468
ParallelUtilities::PredicateTy SkipFunc = [&](const BinaryFunction &BF) {
469-
return BF.getProfileFlags() != BinaryFunction::PF_SAMPLE;
469+
return BF.getProfileFlags() != BinaryFunction::PF_BASIC;
470470
};
471471

472472
ParallelUtilities::runOnEachFunction(

bolt/lib/Profile/DataAggregator.cpp

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -568,11 +568,12 @@ void DataAggregator::processProfile(BinaryContext &BC) {
568568
for (auto &BFI : BC.getBinaryFunctions()) {
569569
BinaryFunction &BF = BFI.second;
570570
if (FuncBranchData *FBD = getBranchData(BF)) {
571-
BF.markProfiled(BinaryFunction::PF_LBR);
572-
BF.RawBranchCount = FBD->getNumExecutedBranches();
573-
} else if (FuncSampleData *FSD = getFuncSampleData(BF.getNames())) {
574-
BF.markProfiled(BinaryFunction::PF_SAMPLE);
575-
BF.RawBranchCount = FSD->getSamples();
571+
BF.markProfiled(BinaryFunction::PF_BRANCH);
572+
BF.RawSampleCount = FBD->getNumExecutedBranches();
573+
} else if (FuncBasicSampleData *FSD =
574+
getFuncBasicSampleData(BF.getNames())) {
575+
BF.markProfiled(BinaryFunction::PF_BASIC);
576+
BF.RawSampleCount = FSD->getSamples();
576577
}
577578
}
578579

@@ -627,8 +628,8 @@ StringRef DataAggregator::getLocationName(const BinaryFunction &Func,
627628
return OrigFunc->getOneName();
628629
}
629630

630-
bool DataAggregator::doSample(BinaryFunction &OrigFunc, uint64_t Address,
631-
uint64_t Count) {
631+
bool DataAggregator::doBasicSample(BinaryFunction &OrigFunc, uint64_t Address,
632+
uint64_t Count) {
632633
// To record executed bytes, use basic block size as is regardless of BAT.
633634
uint64_t BlockSize = 0;
634635
if (BinaryBasicBlock *BB = OrigFunc.getBasicBlockContainingOffset(
@@ -642,13 +643,13 @@ bool DataAggregator::doSample(BinaryFunction &OrigFunc, uint64_t Address,
642643
// Attach executed bytes to parent function in case of cold fragment.
643644
Func.SampleCountInBytes += Count * BlockSize;
644645

645-
auto I = NamesToSamples.find(Func.getOneName());
646-
if (I == NamesToSamples.end()) {
646+
auto I = NamesToBasicSamples.find(Func.getOneName());
647+
if (I == NamesToBasicSamples.end()) {
647648
bool Success;
648649
StringRef LocName = getLocationName(Func, BAT);
649-
std::tie(I, Success) = NamesToSamples.insert(
650-
std::make_pair(Func.getOneName(),
651-
FuncSampleData(LocName, FuncSampleData::ContainerTy())));
650+
std::tie(I, Success) = NamesToBasicSamples.insert(std::make_pair(
651+
Func.getOneName(),
652+
FuncBasicSampleData(LocName, FuncBasicSampleData::ContainerTy())));
652653
}
653654

654655
Address -= Func.getAddress();
@@ -1661,7 +1662,7 @@ void DataAggregator::processBasicEvents() {
16611662
continue;
16621663
}
16631664

1664-
doSample(*Func, PC, HitCount);
1665+
doBasicSample(*Func, PC, HitCount);
16651666
}
16661667
outs() << "PERF2BOLT: read " << NumTotalSamples << " samples\n";
16671668

@@ -2192,9 +2193,9 @@ DataAggregator::writeAggregatedFile(StringRef OutputFilename) const {
21922193
OutFile << " " << Entry.getKey();
21932194
OutFile << "\n";
21942195

2195-
for (const auto &KV : NamesToSamples) {
2196-
const FuncSampleData &FSD = KV.second;
2197-
for (const SampleInfo &SI : FSD.Data) {
2196+
for (const auto &KV : NamesToBasicSamples) {
2197+
const FuncBasicSampleData &FSD = KV.second;
2198+
for (const BasicSampleInfo &SI : FSD.Data) {
21982199
writeLocation(SI.Loc);
21992200
OutFile << SI.Hits << "\n";
22002201
++BranchValues;
@@ -2267,8 +2268,8 @@ std::error_code DataAggregator::writeBATYAML(BinaryContext &BC,
22672268
for (const StringMapEntry<std::nullopt_t> &EventEntry : EventNames)
22682269
EventNamesOS << LS << EventEntry.first().str();
22692270

2270-
BP.Header.Flags = opts::BasicAggregation ? BinaryFunction::PF_SAMPLE
2271-
: BinaryFunction::PF_LBR;
2271+
BP.Header.Flags = opts::BasicAggregation ? BinaryFunction::PF_BASIC
2272+
: BinaryFunction::PF_BRANCH;
22722273

22732274
// Add probe inline tree nodes.
22742275
YAMLProfileWriter::InlineTreeDesc InlineTree;

0 commit comments

Comments
 (0)