Skip to content

Commit d687caa

Browse files
committed
[InstrProf] Emit warnings when correlating lightweight profiles
Emit warnings when `InstrProfCorrelator` finds problems with debug info for lightweight instrumentation profile correlation. To prevent excessive printing, only emit the first 5 warnings. In addition, remove a diagnostic about missing debug info in `InstrProfiling.cpp`. Some compiler-generated functions, e.g., `__clang_call_terminate`, does not emit debug info and will fail a build if `-Werror` is used. This warning is not actionable by the user and I have not seen non-compiler-generated functions fail this test. Reviewed By: smeenai Differential Revision: https://reviews.llvm.org/D156006
1 parent 92d7254 commit d687caa

File tree

5 files changed

+75
-38
lines changed

5 files changed

+75
-38
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Disable full debug info and verify that we get warnings during merging
2+
3+
// RUN: %clang_pgogen -o %t -gline-tables-only -mllvm --debug-info-correlate -mllvm --disable-vp=true %S/../Inputs/instrprof-debug-info-correlate-main.cpp %S/../Inputs/instrprof-debug-info-correlate-foo.cpp
4+
// RUN: env LLVM_PROFILE_FILE=%t.proflite %run %t
5+
// RUN: llvm-profdata merge -o %t.profdata --debug-info=%t %t.proflite --max-debug-info-correlation-warnings=2 2>&1 >/dev/null | FileCheck %s --check-prefixes=CHECK,LIMIT --implicit-check-not=warning
6+
// RUN: llvm-profdata merge -o %t.profdata --debug-info=%t %t.proflite --max-debug-info-correlation-warnings=0 2>&1 >/dev/null | FileCheck %s --check-prefixes=CHECK,NOLIMIT --implicit-check-not=warning
7+
8+
// CHECK: warning: Could not find address of function
9+
// CHECK: warning: Could not find address of function
10+
// NOLIMIT: warning: Could not find address of function
11+
// NOLIMIT: warning: Could not find address of function
12+
// NOLIMIT: warning: Could not find address of function
13+
// LIMIT: warning: Suppressed 3 additional warnings

llvm/include/llvm/ProfileData/InstrProfCorrelator.h

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,12 @@ class InstrProfCorrelator {
3636

3737
/// Construct a ProfileData vector used to correlate raw instrumentation data
3838
/// to their functions.
39-
virtual Error correlateProfileData() = 0;
39+
/// \param MaxWarnings the maximum number of warnings to emit (0 = no limit)
40+
virtual Error correlateProfileData(int MaxWarnings) = 0;
4041

4142
/// Process debug info and dump the correlation data.
42-
virtual Error dumpYaml(raw_ostream &OS) = 0;
43+
/// \param MaxWarnings the maximum number of warnings to emit (0 = no limit)
44+
virtual Error dumpYaml(int MaxWarnings, raw_ostream &OS) = 0;
4345

4446
/// Return the number of ProfileData elements.
4547
std::optional<size_t> getDataSize() const;
@@ -131,11 +133,12 @@ class InstrProfCorrelatorImpl : public InstrProfCorrelator {
131133
protected:
132134
std::vector<RawInstrProf::ProfileData<IntPtrT>> Data;
133135

134-
Error correlateProfileData() override;
136+
Error correlateProfileData(int MaxWarnings) override;
135137
virtual void correlateProfileDataImpl(
138+
int MaxWarnings,
136139
InstrProfCorrelator::CorrelationData *Data = nullptr) = 0;
137140

138-
Error dumpYaml(raw_ostream &OS) override;
141+
Error dumpYaml(int MaxWarnings, raw_ostream &OS) override;
139142

140143
void addProbe(StringRef FunctionName, uint64_t CFGHash, IntPtrT CounterOffset,
141144
IntPtrT FunctionPtr, uint32_t NumCounters);
@@ -197,7 +200,10 @@ class DwarfInstrProfCorrelator : public InstrProfCorrelatorImpl<IntPtrT> {
197200
/// NULL
198201
/// NULL
199202
/// \endcode
203+
/// \param MaxWarnings the maximum number of warnings to emit (0 = no limit)
204+
/// \param Data if provided, populate with the correlation data found
200205
void correlateProfileDataImpl(
206+
int MaxWarnings,
201207
InstrProfCorrelator::CorrelationData *Data = nullptr) override;
202208
};
203209

llvm/lib/ProfileData/InstrProfCorrelator.cpp

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
#include "llvm/DebugInfo/DWARF/DWARFUnit.h"
1717
#include "llvm/Object/MachO.h"
1818
#include "llvm/Support/Debug.h"
19+
#include "llvm/Support/Format.h"
20+
#include "llvm/Support/WithColor.h"
1921
#include <optional>
2022

2123
#define DEBUG_TYPE "correlator"
@@ -142,9 +144,9 @@ InstrProfCorrelatorImpl<IntPtrT>::get(
142144
}
143145

144146
template <class IntPtrT>
145-
Error InstrProfCorrelatorImpl<IntPtrT>::correlateProfileData() {
147+
Error InstrProfCorrelatorImpl<IntPtrT>::correlateProfileData(int MaxWarnings) {
146148
assert(Data.empty() && Names.empty() && NamesVec.empty());
147-
correlateProfileDataImpl();
149+
correlateProfileDataImpl(MaxWarnings);
148150
if (Data.empty() || NamesVec.empty())
149151
return make_error<InstrProfError>(
150152
instrprof_error::unable_to_correlate_profile,
@@ -180,9 +182,10 @@ template <> struct yaml::SequenceElementTraits<InstrProfCorrelator::Probe> {
180182
};
181183

182184
template <class IntPtrT>
183-
Error InstrProfCorrelatorImpl<IntPtrT>::dumpYaml(raw_ostream &OS) {
185+
Error InstrProfCorrelatorImpl<IntPtrT>::dumpYaml(int MaxWarnings,
186+
raw_ostream &OS) {
184187
InstrProfCorrelator::CorrelationData Data;
185-
correlateProfileDataImpl(&Data);
188+
correlateProfileDataImpl(MaxWarnings, &Data);
186189
if (Data.Probes.empty())
187190
return make_error<InstrProfError>(
188191
instrprof_error::unable_to_correlate_profile,
@@ -260,7 +263,10 @@ bool DwarfInstrProfCorrelator<IntPtrT>::isDIEOfProbe(const DWARFDie &Die) {
260263

261264
template <class IntPtrT>
262265
void DwarfInstrProfCorrelator<IntPtrT>::correlateProfileDataImpl(
263-
InstrProfCorrelator::CorrelationData *Data) {
266+
int MaxWarnings, InstrProfCorrelator::CorrelationData *Data) {
267+
bool UnlimitedWarnings = (MaxWarnings == 0);
268+
// -N suppressed warnings means we can emit up to N (unsuppressed) warnings
269+
int NumSuppressedWarnings = -MaxWarnings;
264270
auto maybeAddProbe = [&](DWARFDie Die) {
265271
if (!isDIEOfProbe(Die))
266272
return;
@@ -297,28 +303,30 @@ void DwarfInstrProfCorrelator<IntPtrT>::correlateProfileDataImpl(
297303
}
298304
}
299305
if (!FunctionName || !CFGHash || !CounterPtr || !NumCounters) {
300-
LLVM_DEBUG(dbgs() << "Incomplete DIE for probe\n\tFunctionName: "
301-
<< FunctionName << "\n\tCFGHash: " << CFGHash
302-
<< "\n\tCounterPtr: " << CounterPtr
303-
<< "\n\tNumCounters: " << NumCounters);
304-
LLVM_DEBUG(Die.dump(dbgs()));
306+
if (UnlimitedWarnings || ++NumSuppressedWarnings < 1) {
307+
WithColor::warning()
308+
<< "Incomplete DIE for function " << FunctionName
309+
<< ": CFGHash=" << CFGHash << " CounterPtr=" << CounterPtr
310+
<< " NumCounters=" << NumCounters << "\n";
311+
LLVM_DEBUG(Die.dump(dbgs()));
312+
}
305313
return;
306314
}
307315
uint64_t CountersStart = this->Ctx->CountersSectionStart;
308316
uint64_t CountersEnd = this->Ctx->CountersSectionEnd;
309317
if (*CounterPtr < CountersStart || *CounterPtr >= CountersEnd) {
310-
LLVM_DEBUG(
311-
dbgs() << "CounterPtr out of range for probe\n\tFunction Name: "
312-
<< FunctionName << "\n\tExpected: [0x"
313-
<< Twine::utohexstr(CountersStart) << ", 0x"
314-
<< Twine::utohexstr(CountersEnd) << ")\n\tActual: 0x"
315-
<< Twine::utohexstr(*CounterPtr));
316-
LLVM_DEBUG(Die.dump(dbgs()));
318+
if (UnlimitedWarnings || ++NumSuppressedWarnings < 1) {
319+
WithColor::warning()
320+
<< format("CounterPtr out of range for function %s: Actual=0x%x "
321+
"Expected=[0x%x, 0x%x)\n",
322+
*FunctionName, *CounterPtr, CountersStart, CountersEnd);
323+
LLVM_DEBUG(Die.dump(dbgs()));
324+
}
317325
return;
318326
}
319-
if (!FunctionPtr) {
320-
LLVM_DEBUG(dbgs() << "Could not find address of " << *FunctionName
321-
<< "\n");
327+
if (!FunctionPtr && (UnlimitedWarnings || ++NumSuppressedWarnings < 1)) {
328+
WithColor::warning() << format("Could not find address of function %s\n",
329+
*FunctionName);
322330
LLVM_DEBUG(Die.dump(dbgs()));
323331
}
324332
IntPtrT CounterOffset = *CounterPtr - CountersStart;
@@ -348,4 +356,8 @@ void DwarfInstrProfCorrelator<IntPtrT>::correlateProfileDataImpl(
348356
for (auto &CU : DICtx->dwo_units())
349357
for (const auto &Entry : CU->dies())
350358
maybeAddProbe(DWARFDie(CU.get(), &Entry));
359+
360+
if (!UnlimitedWarnings && NumSuppressedWarnings > 0)
361+
WithColor::warning() << format("Suppressed %d additional warnings\n",
362+
NumSuppressedWarnings);
351363
}

llvm/lib/Transforms/Instrumentation/InstrProfiling.cpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1056,12 +1056,6 @@ InstrProfiling::getOrCreateRegionCounters(InstrProfInstBase *Inc) {
10561056
Annotations);
10571057
CounterPtr->addDebugInfo(DICounter);
10581058
DB.finalize();
1059-
} else {
1060-
std::string Msg = ("Missing debug info for function " + Fn->getName() +
1061-
"; required for profile correlation.")
1062-
.str();
1063-
Ctx.diagnose(
1064-
DiagnosticInfoPGOProfile(M->getName().data(), Msg, DS_Warning));
10651059
}
10661060
}
10671061

llvm/tools/llvm-profdata/llvm-profdata.cpp

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -407,8 +407,8 @@ static void
407407
mergeInstrProfile(const WeightedFileVector &Inputs, StringRef DebugInfoFilename,
408408
SymbolRemapper *Remapper, StringRef OutputFilename,
409409
ProfileFormat OutputFormat, uint64_t TraceReservoirSize,
410-
uint64_t MaxTraceLength, bool OutputSparse,
411-
unsigned NumThreads, FailureMode FailMode,
410+
uint64_t MaxTraceLength, int MaxDbgCorrelationWarnings,
411+
bool OutputSparse, unsigned NumThreads, FailureMode FailMode,
412412
const StringRef ProfiledBinary) {
413413
if (OutputFormat == PF_Compact_Binary)
414414
exitWithError("Compact Binary is deprecated");
@@ -421,7 +421,7 @@ mergeInstrProfile(const WeightedFileVector &Inputs, StringRef DebugInfoFilename,
421421
if (auto Err =
422422
InstrProfCorrelator::get(DebugInfoFilename).moveInto(Correlator))
423423
exitWithError(std::move(Err), DebugInfoFilename);
424-
if (auto Err = Correlator->correlateProfileData())
424+
if (auto Err = Correlator->correlateProfileData(MaxDbgCorrelationWarnings))
425425
exitWithError(std::move(Err), DebugInfoFilename);
426426
}
427427

@@ -1270,6 +1270,11 @@ static int merge_main(int argc, const char *argv[]) {
12701270
cl::opt<std::string> DebugInfoFilename(
12711271
"debug-info", cl::init(""),
12721272
cl::desc("Use the provided debug info to correlate the raw profile."));
1273+
cl::opt<unsigned> MaxDbgCorrelationWarnings(
1274+
"max-debug-info-correlation-warnings",
1275+
cl::desc("The maximum number of warnings to emit when correlating "
1276+
"profile from debug info (0 = no limit)"),
1277+
cl::init(5));
12731278
cl::opt<std::string> ProfiledBinary(
12741279
"profiled-binary", cl::init(""),
12751280
cl::desc("Path to binary from which the profile was collected."));
@@ -1331,8 +1336,8 @@ static int merge_main(int argc, const char *argv[]) {
13311336
mergeInstrProfile(WeightedInputs, DebugInfoFilename, Remapper.get(),
13321337
OutputFilename, OutputFormat,
13331338
TemporalProfTraceReservoirSize,
1334-
TemporalProfMaxTraceLength, OutputSparse, NumThreads,
1335-
FailureMode, ProfiledBinary);
1339+
TemporalProfMaxTraceLength, MaxDbgCorrelationWarnings,
1340+
OutputSparse, NumThreads, FailureMode, ProfiledBinary);
13361341
else
13371342
mergeSampleProfile(WeightedInputs, Remapper.get(), OutputFilename,
13381343
OutputFormat, ProfileSymbolListFile, CompressAllSections,
@@ -2877,19 +2882,20 @@ static int showMemProfProfile(const std::string &Filename,
28772882
static int showDebugInfoCorrelation(const std::string &Filename,
28782883
bool ShowDetailedSummary,
28792884
bool ShowProfileSymbolList,
2885+
int MaxDbgCorrelationWarnings,
28802886
ShowFormat SFormat, raw_fd_ostream &OS) {
28812887
if (SFormat == ShowFormat::Json)
28822888
exitWithError("JSON output is not supported for debug info correlation");
28832889
std::unique_ptr<InstrProfCorrelator> Correlator;
28842890
if (auto Err = InstrProfCorrelator::get(Filename).moveInto(Correlator))
28852891
exitWithError(std::move(Err), Filename);
28862892
if (SFormat == ShowFormat::Yaml) {
2887-
if (auto Err = Correlator->dumpYaml(OS))
2893+
if (auto Err = Correlator->dumpYaml(MaxDbgCorrelationWarnings, OS))
28882894
exitWithError(std::move(Err), Filename);
28892895
return 0;
28902896
}
28912897

2892-
if (auto Err = Correlator->correlateProfileData())
2898+
if (auto Err = Correlator->correlateProfileData(MaxDbgCorrelationWarnings))
28932899
exitWithError(std::move(Err), Filename);
28942900

28952901
InstrProfSymtab Symtab;
@@ -2989,6 +2995,11 @@ static int show_main(int argc, const char *argv[]) {
29892995
"debug-info", cl::init(""),
29902996
cl::desc("Read and extract profile metadata from debug info and show "
29912997
"the functions it found."));
2998+
cl::opt<unsigned> MaxDbgCorrelationWarnings(
2999+
"max-debug-info-correlation-warnings",
3000+
cl::desc("The maximum number of warnings to emit when correlating "
3001+
"profile from debug info (0 = no limit)"),
3002+
cl::init(5));
29923003
cl::opt<bool> ShowCovered(
29933004
"covered", cl::init(false),
29943005
cl::desc("Show only the functions that have been executed."));
@@ -3022,7 +3033,8 @@ static int show_main(int argc, const char *argv[]) {
30223033

30233034
if (!DebugInfoFilename.empty())
30243035
return showDebugInfoCorrelation(DebugInfoFilename, ShowDetailedSummary,
3025-
ShowProfileSymbolList, SFormat, OS);
3036+
ShowProfileSymbolList,
3037+
MaxDbgCorrelationWarnings, SFormat, OS);
30263038

30273039
if (ProfileKind == instr)
30283040
return showInstrProfile(

0 commit comments

Comments
 (0)