Skip to content

Commit 5e04432

Browse files
committed
[PGO] Report number of counts being dropped when a hash-mismatch happens
This patch reports number of counts being dropped when a hash-mismatch happens. This information will be helpful to the users -- if the dropped counts are large, the user should redo the instrumentation build and recollect the profile. Differential Revision: https://reviews.llvm.org/D129001
1 parent f2b94bd commit 5e04432

File tree

5 files changed

+44
-12
lines changed

5 files changed

+44
-12
lines changed

Diff for: llvm/include/llvm/ProfileData/InstrProfReader.h

+8-3
Original file line numberDiff line numberDiff line change
@@ -619,9 +619,14 @@ class IndexedInstrProfReader : public InstrProfReader {
619619
/// Read a single record.
620620
Error readNextRecord(NamedInstrProfRecord &Record) override;
621621

622-
/// Return the NamedInstrProfRecord associated with FuncName and FuncHash
623-
Expected<InstrProfRecord> getInstrProfRecord(StringRef FuncName,
624-
uint64_t FuncHash);
622+
/// Return the NamedInstrProfRecord associated with FuncName and FuncHash.
623+
/// When return a hash_mismatch error and MismatchedFuncSum is not nullptr,
624+
/// the sum of all counters in the mismatched function will be set to
625+
/// MismatchedFuncSum. If there are multiple instances of mismatched
626+
/// functions, MismatchedFuncSum returns the maximum.
627+
Expected<InstrProfRecord>
628+
getInstrProfRecord(StringRef FuncName, uint64_t FuncHash,
629+
uint64_t *MismatchedFuncSum = nullptr);
625630

626631
/// Return the memprof record for the function identified by
627632
/// llvm::md5(Name).

Diff for: llvm/lib/ProfileData/InstrProfReader.cpp

+21-3
Original file line numberDiff line numberDiff line change
@@ -1026,10 +1026,10 @@ InstrProfSymtab &IndexedInstrProfReader::getSymtab() {
10261026
return *Symtab;
10271027
}
10281028

1029-
Expected<InstrProfRecord>
1030-
IndexedInstrProfReader::getInstrProfRecord(StringRef FuncName,
1031-
uint64_t FuncHash) {
1029+
Expected<InstrProfRecord> IndexedInstrProfReader::getInstrProfRecord(
1030+
StringRef FuncName, uint64_t FuncHash, uint64_t *MismatchedFuncSum) {
10321031
ArrayRef<NamedInstrProfRecord> Data;
1032+
uint64_t FuncSum = 0;
10331033
Error Err = Remapper->getRecords(FuncName, Data);
10341034
if (Err)
10351035
return std::move(Err);
@@ -1038,6 +1038,19 @@ IndexedInstrProfReader::getInstrProfRecord(StringRef FuncName,
10381038
// A flag to indicate if the records are from the same type
10391039
// of profile (i.e cs vs nocs).
10401040
bool CSBitMatch = false;
1041+
auto getFuncSum = [](const std::vector<uint64_t> &Counts) {
1042+
uint64_t ValueSum = 0;
1043+
for (unsigned I = 0, S = Counts.size(); I < S; I++) {
1044+
uint64_t CountValue = Counts[I];
1045+
if (CountValue == (uint64_t)-1)
1046+
continue;
1047+
// Handle overflow -- if that happens, return max.
1048+
if (std::numeric_limits<uint64_t>::max() - CountValue <= ValueSum)
1049+
return std::numeric_limits<uint64_t>::max();
1050+
ValueSum += CountValue;
1051+
}
1052+
return ValueSum;
1053+
};
10411054

10421055
for (const NamedInstrProfRecord &I : Data) {
10431056
// Check for a match and fill the vector if there is one.
@@ -1046,9 +1059,14 @@ IndexedInstrProfReader::getInstrProfRecord(StringRef FuncName,
10461059
if (NamedInstrProfRecord::hasCSFlagInHash(I.Hash) ==
10471060
NamedInstrProfRecord::hasCSFlagInHash(FuncHash)) {
10481061
CSBitMatch = true;
1062+
if (MismatchedFuncSum == nullptr)
1063+
continue;
1064+
FuncSum = std::max(FuncSum, getFuncSum(I.Counts));
10491065
}
10501066
}
10511067
if (CSBitMatch) {
1068+
if (MismatchedFuncSum != nullptr)
1069+
*MismatchedFuncSum = FuncSum;
10521070
return error(instrprof_error::hash_mismatch);
10531071
}
10541072
return error(instrprof_error::unknown_function);

Diff for: llvm/lib/Transforms/Instrumentation/PGOInstrumentation.cpp

+8-5
Original file line numberDiff line numberDiff line change
@@ -1224,8 +1224,9 @@ static void annotateFunctionWithHashMismatch(Function &F,
12241224
bool PGOUseFunc::readCounters(IndexedInstrProfReader *PGOReader, bool &AllZeros,
12251225
bool &AllMinusOnes) {
12261226
auto &Ctx = M->getContext();
1227-
Expected<InstrProfRecord> Result =
1228-
PGOReader->getInstrProfRecord(FuncInfo.FuncName, FuncInfo.FunctionHash);
1227+
uint64_t MismatchedFuncSum = 0;
1228+
Expected<InstrProfRecord> Result = PGOReader->getInstrProfRecord(
1229+
FuncInfo.FuncName, FuncInfo.FunctionHash, &MismatchedFuncSum);
12291230
if (Error E = Result.takeError()) {
12301231
handleAllErrors(std::move(E), [&](const InstrProfError &IPE) {
12311232
auto Err = IPE.get();
@@ -1254,9 +1255,11 @@ bool PGOUseFunc::readCounters(IndexedInstrProfReader *PGOReader, bool &AllZeros,
12541255
if (SkipWarning)
12551256
return;
12561257

1257-
std::string Msg = IPE.message() + std::string(" ") + F.getName().str() +
1258-
std::string(" Hash = ") +
1259-
std::to_string(FuncInfo.FunctionHash);
1258+
std::string Msg =
1259+
IPE.message() + std::string(" ") + F.getName().str() +
1260+
std::string(" Hash = ") + std::to_string(FuncInfo.FunctionHash) +
1261+
std::string(" up to ") + std::to_string(MismatchedFuncSum) +
1262+
std::string(" count discarded");
12601263

12611264
Ctx.diagnose(
12621265
DiagnosticInfoPGOProfile(M->getName().data(), Msg, DS_Warning));

Diff for: llvm/test/Transforms/PGOProfile/Inputs/diag.proftext

+6
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,9 @@ foo
55
1
66
1
77

8+
foo
9+
12885999999
10+
2
11+
6000
12+
4000
13+

Diff for: llvm/test/Transforms/PGOProfile/diag_mismatch.ll

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
; RUN: llvm-profdata merge %S/Inputs/diag.proftext -o %t.profdata
22
; RUN: opt < %s -passes=pgo-instr-use -pgo-test-profile-file=%t.profdata -S 2>&1 | FileCheck %s
33

4-
; CHECK: warning: {{.+}}: function control flow change detected (hash mismatch) foo
4+
; CHECK: warning: {{.+}}: function control flow change detected (hash mismatch) foo Hash = 742261418966908927 up to 10000 count discarded
55

66
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
77
target triple = "x86_64-unknown-linux-gnu"

0 commit comments

Comments
 (0)