Skip to content

Commit f18cfb9

Browse files
authored
[InstrProf] Factor out getRecord() and use NamedInstrProfRecord (#145417)
Factor out code in `populateCounters()` and `populateCoverage()` used to grab the record into `PGOUseFunc::getRecord()` to reduce code duplication. And return `NamedInstrProfRecord` in `getInstrProfRecord()` to avoid an unnecessary cast. No functional change is intented.
1 parent 3bc1fc6 commit f18cfb9

File tree

5 files changed

+41
-41
lines changed

5 files changed

+41
-41
lines changed

clang/lib/CodeGen/CodeGenPGO.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1423,8 +1423,7 @@ void CodeGenPGO::loadRegionCounts(llvm::IndexedInstrProfReader *PGOReader,
14231423
bool IsInMainFile) {
14241424
CGM.getPGOStats().addVisited(IsInMainFile);
14251425
RegionCounts.clear();
1426-
llvm::Expected<llvm::InstrProfRecord> RecordExpected =
1427-
PGOReader->getInstrProfRecord(FuncName, FunctionHash);
1426+
auto RecordExpected = PGOReader->getInstrProfRecord(FuncName, FunctionHash);
14281427
if (auto E = RecordExpected.takeError()) {
14291428
auto IPE = std::get<0>(llvm::InstrProfError::take(std::move(E)));
14301429
if (IPE == llvm::instrprof_error::unknown_function)

llvm/include/llvm/ProfileData/InstrProfReader.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -824,7 +824,7 @@ class LLVM_ABI IndexedInstrProfReader : public InstrProfReader {
824824
/// functions, MismatchedFuncSum returns the maximum. If \c FuncName is not
825825
/// found, try to lookup \c DeprecatedFuncName to handle profiles built by
826826
/// older compilers.
827-
Expected<InstrProfRecord>
827+
Expected<NamedInstrProfRecord>
828828
getInstrProfRecord(StringRef FuncName, uint64_t FuncHash,
829829
StringRef DeprecatedFuncName = "",
830830
uint64_t *MismatchedFuncSum = nullptr);

llvm/lib/ProfileData/InstrProfReader.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1369,7 +1369,7 @@ InstrProfSymtab &IndexedInstrProfReader::getSymtab() {
13691369
return *Symtab;
13701370
}
13711371

1372-
Expected<InstrProfRecord> IndexedInstrProfReader::getInstrProfRecord(
1372+
Expected<NamedInstrProfRecord> IndexedInstrProfReader::getInstrProfRecord(
13731373
StringRef FuncName, uint64_t FuncHash, StringRef DeprecatedFuncName,
13741374
uint64_t *MismatchedFuncSum) {
13751375
ArrayRef<NamedInstrProfRecord> Data;
@@ -1572,7 +1572,7 @@ memprof::AllMemProfData IndexedMemProfReader::getAllMemProfData() const {
15721572
Error IndexedInstrProfReader::getFunctionCounts(StringRef FuncName,
15731573
uint64_t FuncHash,
15741574
std::vector<uint64_t> &Counts) {
1575-
Expected<InstrProfRecord> Record = getInstrProfRecord(FuncName, FuncHash);
1575+
auto Record = getInstrProfRecord(FuncName, FuncHash);
15761576
if (Error E = Record.takeError())
15771577
return error(std::move(E));
15781578

@@ -1583,7 +1583,7 @@ Error IndexedInstrProfReader::getFunctionCounts(StringRef FuncName,
15831583
Error IndexedInstrProfReader::getFunctionBitmap(StringRef FuncName,
15841584
uint64_t FuncHash,
15851585
BitVector &Bitmap) {
1586-
Expected<InstrProfRecord> Record = getInstrProfRecord(FuncName, FuncHash);
1586+
auto Record = getInstrProfRecord(FuncName, FuncHash);
15871587
if (Error E = Record.takeError())
15881588
return error(std::move(E));
15891589

llvm/lib/Transforms/Instrumentation/PGOInstrumentation.cpp

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1176,15 +1176,20 @@ class PGOUseFunc {
11761176

11771177
void handleInstrProfError(Error Err, uint64_t MismatchedFuncSum);
11781178

1179+
/// Get the profile record, assign it to \p ProfileRecord, handle errors if
1180+
/// necessary, and assign \p ProgramMaxCount. \returns true if there are no
1181+
/// errors.
1182+
bool getRecord(IndexedInstrProfReader *PGOReader);
1183+
11791184
// Read counts for the instrumented BB from profile.
1180-
bool readCounters(IndexedInstrProfReader *PGOReader, bool &AllZeros,
1185+
bool readCounters(bool &AllZeros,
11811186
InstrProfRecord::CountPseudoKind &PseudoKind);
11821187

11831188
// Populate the counts for all BBs.
11841189
void populateCounters();
11851190

11861191
// Set block coverage based on profile coverage values.
1187-
void populateCoverage(IndexedInstrProfReader *PGOReader);
1192+
void populateCoverage();
11881193

11891194
// Set the branch weights based on the count values.
11901195
void setBranchWeights();
@@ -1208,7 +1213,7 @@ class PGOUseFunc {
12081213
uint64_t getFuncHash() const { return FuncInfo.FunctionHash; }
12091214

12101215
// Return the profile record for this function;
1211-
InstrProfRecord &getProfileRecord() { return ProfileRecord; }
1216+
NamedInstrProfRecord &getProfileRecord() { return ProfileRecord; }
12121217

12131218
// Return the auxiliary BB information.
12141219
PGOUseBBInfo &getBBInfo(const BasicBlock *BB) const {
@@ -1246,7 +1251,7 @@ class PGOUseFunc {
12461251
uint32_t ProfileCountSize = 0;
12471252

12481253
// ProfileRecord for this function.
1249-
InstrProfRecord ProfileRecord;
1254+
NamedInstrProfRecord ProfileRecord;
12501255

12511256
// Function hotness info derived from profile.
12521257
FuncFreqAttr FreqAttr;
@@ -1441,21 +1446,26 @@ void PGOUseFunc::handleInstrProfError(Error Err, uint64_t MismatchedFuncSum) {
14411446
});
14421447
}
14431448

1444-
// Read the profile from ProfileFileName and assign the value to the
1445-
// instrumented BB and the edges. This function also updates ProgramMaxCount.
1446-
// Return true if the profile are successfully read, and false on errors.
1447-
bool PGOUseFunc::readCounters(IndexedInstrProfReader *PGOReader, bool &AllZeros,
1448-
InstrProfRecord::CountPseudoKind &PseudoKind) {
1449-
auto &Ctx = M->getContext();
1449+
bool PGOUseFunc::getRecord(IndexedInstrProfReader *PGOReader) {
14501450
uint64_t MismatchedFuncSum = 0;
1451-
Expected<InstrProfRecord> Result = PGOReader->getInstrProfRecord(
1451+
auto Result = PGOReader->getInstrProfRecord(
14521452
FuncInfo.FuncName, FuncInfo.FunctionHash, FuncInfo.DeprecatedFuncName,
14531453
&MismatchedFuncSum);
14541454
if (Error E = Result.takeError()) {
14551455
handleInstrProfError(std::move(E), MismatchedFuncSum);
14561456
return false;
14571457
}
14581458
ProfileRecord = std::move(Result.get());
1459+
ProgramMaxCount = PGOReader->getMaximumFunctionCount(IsCS);
1460+
return true;
1461+
}
1462+
1463+
// Read the profile from ProfileFileName and assign the value to the
1464+
// instrumented BB and the edges. Return true if the profile are successfully
1465+
// read, and false on errors.
1466+
bool PGOUseFunc::readCounters(bool &AllZeros,
1467+
InstrProfRecord::CountPseudoKind &PseudoKind) {
1468+
auto &Ctx = M->getContext();
14591469
PseudoKind = ProfileRecord.getCountPseudoKind();
14601470
if (PseudoKind != InstrProfRecord::NotPseudo) {
14611471
return true;
@@ -1488,22 +1498,13 @@ bool PGOUseFunc::readCounters(IndexedInstrProfReader *PGOReader, bool &AllZeros,
14881498
DS_Warning));
14891499
return false;
14901500
}
1491-
ProgramMaxCount = PGOReader->getMaximumFunctionCount(IsCS);
14921501
return true;
14931502
}
14941503

1495-
void PGOUseFunc::populateCoverage(IndexedInstrProfReader *PGOReader) {
1496-
uint64_t MismatchedFuncSum = 0;
1497-
Expected<InstrProfRecord> Result = PGOReader->getInstrProfRecord(
1498-
FuncInfo.FuncName, FuncInfo.FunctionHash, FuncInfo.DeprecatedFuncName,
1499-
&MismatchedFuncSum);
1500-
if (auto Err = Result.takeError()) {
1501-
handleInstrProfError(std::move(Err), MismatchedFuncSum);
1502-
return;
1503-
}
1504+
void PGOUseFunc::populateCoverage() {
15041505
IsCS ? NumOfCSPGOFunc++ : NumOfPGOFunc++;
15051506

1506-
std::vector<uint64_t> &CountsFromProfile = Result.get().Counts;
1507+
ArrayRef<uint64_t> CountsFromProfile = ProfileRecord.Counts;
15071508
DenseMap<const BasicBlock *, bool> Coverage;
15081509
unsigned Index = 0;
15091510
for (auto &BB : F)
@@ -2243,8 +2244,10 @@ static bool annotateAllFunctions(
22432244
PGOUseFunc Func(F, &M, TLI, ComdatMembers, BPI, BFI, LI, PSI, IsCS,
22442245
InstrumentFuncEntry, InstrumentLoopEntries,
22452246
HasSingleByteCoverage);
2247+
if (!Func.getRecord(PGOReader.get()))
2248+
continue;
22462249
if (HasSingleByteCoverage) {
2247-
Func.populateCoverage(PGOReader.get());
2250+
Func.populateCoverage();
22482251
continue;
22492252
}
22502253
// When PseudoKind is set to a vaule other than InstrProfRecord::NotPseudo,
@@ -2253,7 +2256,7 @@ static bool annotateAllFunctions(
22532256
// attribute and drop all the profile counters.
22542257
InstrProfRecord::CountPseudoKind PseudoKind = InstrProfRecord::NotPseudo;
22552258
bool AllZeros = false;
2256-
if (!Func.readCounters(PGOReader.get(), AllZeros, PseudoKind))
2259+
if (!Func.readCounters(AllZeros, PseudoKind))
22572260
continue;
22582261
if (AllZeros) {
22592262
F.setEntryCount(ProfileCount(0, Function::PCT_Real));

llvm/unittests/ProfileData/InstrProfTest.cpp

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ TEST_P(MaybeSparseInstrProfTest, get_instr_prof_record) {
135135
auto Profile = Writer.writeBuffer();
136136
readProfile(std::move(Profile));
137137

138-
Expected<InstrProfRecord> R = Reader->getInstrProfRecord("foo", 0x1234);
138+
auto R = Reader->getInstrProfRecord("foo", 0x1234);
139139
EXPECT_THAT_ERROR(R.takeError(), Succeeded());
140140
ASSERT_EQ(2U, R->Counts.size());
141141
ASSERT_EQ(1U, R->Counts[0]);
@@ -251,7 +251,7 @@ TEST_F(InstrProfTest, test_writer_merge) {
251251
auto Profile = Writer.writeBuffer();
252252
readProfile(std::move(Profile));
253253

254-
Expected<InstrProfRecord> R = Reader->getInstrProfRecord("func1", 0x1234);
254+
auto R = Reader->getInstrProfRecord("func1", 0x1234);
255255
EXPECT_THAT_ERROR(R.takeError(), Succeeded());
256256
ASSERT_EQ(1U, R->Counts.size());
257257
ASSERT_EQ(42U, R->Counts[0]);
@@ -600,7 +600,7 @@ TEST_F(InstrProfTest, test_memprof_merge) {
600600
auto Profile = Writer.writeBuffer();
601601
readProfile(std::move(Profile));
602602

603-
Expected<InstrProfRecord> R = Reader->getInstrProfRecord("func1", 0x1234);
603+
auto R = Reader->getInstrProfRecord("func1", 0x1234);
604604
EXPECT_THAT_ERROR(R.takeError(), Succeeded());
605605
ASSERT_EQ(1U, R->Counts.size());
606606
ASSERT_EQ(42U, R->Counts[0]);
@@ -800,7 +800,7 @@ TEST_P(InstrProfReaderWriterTest, icall_and_vtable_data_read_write) {
800800
// Set reader value prof data endianness.
801801
Reader->setValueProfDataEndianness(getEndianness());
802802

803-
Expected<InstrProfRecord> R = Reader->getInstrProfRecord("caller", 0x1234);
803+
auto R = Reader->getInstrProfRecord("caller", 0x1234);
804804
ASSERT_THAT_ERROR(R.takeError(), Succeeded());
805805

806806
// Test the number of instrumented indirect call sites and the number of
@@ -874,7 +874,7 @@ TEST_P(MaybeSparseInstrProfTest, annotate_vp_data) {
874874
Writer.addRecord(std::move(Record), Err);
875875
auto Profile = Writer.writeBuffer();
876876
readProfile(std::move(Profile));
877-
Expected<InstrProfRecord> R = Reader->getInstrProfRecord("caller", 0x1234);
877+
auto R = Reader->getInstrProfRecord("caller", 0x1234);
878878
EXPECT_THAT_ERROR(R.takeError(), Succeeded());
879879

880880
LLVMContext Ctx;
@@ -1051,7 +1051,7 @@ TEST_P(MaybeSparseInstrProfTest, icall_and_vtable_data_merge) {
10511051

10521052
// Test the number of instrumented value sites and the number of profiled
10531053
// values for each site.
1054-
Expected<InstrProfRecord> R = Reader->getInstrProfRecord("caller", 0x1234);
1054+
auto R = Reader->getInstrProfRecord("caller", 0x1234);
10551055
EXPECT_THAT_ERROR(R.takeError(), Succeeded());
10561056
// For indirect calls.
10571057
ASSERT_EQ(5U, R->getNumValueSites(IPVK_IndirectCallTarget));
@@ -1190,13 +1190,11 @@ TEST_P(ValueProfileMergeEdgeCaseTest, value_profile_data_merge_saturation) {
11901190
readProfile(std::move(Profile));
11911191

11921192
// Verify saturation of counts.
1193-
Expected<InstrProfRecord> ReadRecord1 =
1194-
Reader->getInstrProfRecord("foo", 0x1234);
1193+
auto ReadRecord1 = Reader->getInstrProfRecord("foo", 0x1234);
11951194
ASSERT_THAT_ERROR(ReadRecord1.takeError(), Succeeded());
11961195
EXPECT_EQ(MaxEdgeCount, ReadRecord1->Counts[0]);
11971196

1198-
Expected<InstrProfRecord> ReadRecord2 =
1199-
Reader->getInstrProfRecord("baz", 0x5678);
1197+
auto ReadRecord2 = Reader->getInstrProfRecord("baz", 0x5678);
12001198
ASSERT_TRUE(bool(ReadRecord2));
12011199
ASSERT_EQ(1U, ReadRecord2->getNumValueSites(ValueKind));
12021200
auto VD = ReadRecord2->getValueArrayForSite(ValueKind, 0);
@@ -1241,7 +1239,7 @@ TEST_P(ValueProfileMergeEdgeCaseTest, value_profile_data_merge_site_trunc) {
12411239
auto Profile = Writer.writeBuffer();
12421240
readProfile(std::move(Profile));
12431241

1244-
Expected<InstrProfRecord> R = Reader->getInstrProfRecord("caller", 0x1234);
1242+
auto R = Reader->getInstrProfRecord("caller", 0x1234);
12451243
ASSERT_THAT_ERROR(R.takeError(), Succeeded());
12461244
ASSERT_EQ(2U, R->getNumValueSites(ValueKind));
12471245
auto VD = R->getValueArrayForSite(ValueKind, 0);

0 commit comments

Comments
 (0)