Skip to content

Commit da59464

Browse files
committed
[InstrProf] Add version into llvm-profdata
This patch adds support of printing profile version into llvm-profdata which was proposed in: https://discourse.llvm.org/t/llvm-profdata-failure-guarantees-for-code-coverage/64924 Differential Revision: https://reviews.llvm.org/D135317
1 parent 9409bbb commit da59464

File tree

4 files changed

+57
-4
lines changed

4 files changed

+57
-4
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// REQUIRES: linux
2+
// RUN: %clang_profgen -O2 -o %t %s
3+
// RUN: env LLVM_PROFILE_FILE=%t.profraw %run %t
4+
// RUN: llvm-profdata show --profile-version %t.profraw > %t.profraw.out
5+
// RUN: FileCheck %s --check-prefix=RAW-PROF < %t.profraw.out
6+
7+
// RUN: rm -rf %t.profdir
8+
// RUN: env LLVM_PROFILE_FILE=%t.profdir/default_%m.profraw %run %t
9+
// RUN: env LLVM_PROFILE_FILE=%t.profdir/default_%m.profraw %run %t
10+
// RUN: env LLVM_PROFILE_FILE=%t.profdir/default_%m.profraw %run %t
11+
// RUN: llvm-profdata show --profile-version %t.profdir/default_*.profraw > %t.profraw.out
12+
// RUN: FileCheck %s --check-prefix=INDEXED-PROF < %t.profraw.out
13+
14+
void foo() {}
15+
16+
void bar() {}
17+
18+
int main() {
19+
foo();
20+
bar();
21+
return 0;
22+
}
23+
24+
// RAW-PROF: Instrumentation level: Front-end
25+
// RAW-PROF-NEXT: Total functions: 3
26+
// RAW-PROF-NEXT: Maximum function count: 1
27+
// RAW-PROF-NEXT: Maximum internal block count: 0
28+
// RAW-PROF-NEXT: Profile version: {{[0-9]+}}
29+
30+
// INDEXED-PROF: Instrumentation level: Front-end
31+
// INDEXED-PROF-NEXT: Total functions: 3
32+
// INDEXED-PROF-NEXT: Maximum function count: 3
33+
// INDEXED-PROF-NEXT: Maximum internal block count: 0
34+
// INDEXED-PROF-NEXT: Profile version: {{[0-9]+}}

llvm/docs/CommandGuide/llvm-profdata.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,10 @@ OPTIONS
245245

246246
Print details for every function.
247247

248+
.. option:: --binary-ids
249+
250+
Print embedded binary ids in a profile.
251+
248252
.. option:: --counts
249253

250254
Print the counter values for the displayed functions.
@@ -297,6 +301,10 @@ OPTIONS
297301
Only output names of functions whose max count value are below the cutoff
298302
value.
299303

304+
.. option:: --profile-version
305+
306+
Print profile version.
307+
300308
.. option:: --showcs
301309

302310
Only show context sensitive profile counts. The default is to filter all

llvm/include/llvm/ProfileData/InstrProfReader.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,9 @@ class InstrProfReader {
103103
InstrProfIterator<> begin() { return InstrProfIterator<>(this); }
104104
InstrProfIterator<> end() { return InstrProfIterator<>(); }
105105

106+
/// Return the profile version.
107+
virtual uint64_t getVersion() const = 0;
108+
106109
virtual bool isIRLevelProfile() const = 0;
107110

108111
virtual bool hasCSIRLevelProfile() const = 0;
@@ -215,6 +218,9 @@ class TextInstrProfReader : public InstrProfReader {
215218
/// Return true if the given buffer is in text instrprof format.
216219
static bool hasFormat(const MemoryBuffer &Buffer);
217220

221+
// Text format does not have version, so return 0.
222+
uint64_t getVersion() const override { return 0; }
223+
218224
bool isIRLevelProfile() const override {
219225
return static_cast<bool>(ProfileKind & InstrProfKind::IRInstrumentation);
220226
}
@@ -306,6 +312,8 @@ class RawInstrProfReader : public InstrProfReader {
306312
Error readNextRecord(NamedInstrProfRecord &Record) override;
307313
Error printBinaryIds(raw_ostream &OS) override;
308314

315+
uint64_t getVersion() const override { return Version; }
316+
309317
bool isIRLevelProfile() const override {
310318
return (Version & VARIANT_MASK_IR_PROF) != 0;
311319
}
@@ -608,7 +616,7 @@ class IndexedInstrProfReader : public InstrProfReader {
608616
IndexedInstrProfReader &operator=(const IndexedInstrProfReader &) = delete;
609617

610618
/// Return the profile version.
611-
uint64_t getVersion() const { return Index->getVersion(); }
619+
uint64_t getVersion() const override { return Index->getVersion(); }
612620
bool isIRLevelProfile() const override { return Index->isIRLevelProfile(); }
613621
bool hasCSIRLevelProfile() const override {
614622
return Index->hasCSIRLevelProfile();

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2252,7 +2252,7 @@ static int showInstrProfile(const std::string &Filename, bool ShowCounts,
22522252
uint64_t ValueCutoff, bool OnlyListBelow,
22532253
const std::string &ShowFunction, bool TextFormat,
22542254
bool ShowBinaryIds, bool ShowCovered,
2255-
raw_fd_ostream &OS) {
2255+
bool ShowProfileVersion, raw_fd_ostream &OS) {
22562256
auto ReaderOrErr = InstrProfReader::create(Filename);
22572257
std::vector<uint32_t> Cutoffs = std::move(DetailedSummaryCutoffs);
22582258
if (ShowDetailedSummary && Cutoffs.empty()) {
@@ -2462,6 +2462,8 @@ static int showInstrProfile(const std::string &Filename, bool ShowCounts,
24622462
if (Error E = Reader->printBinaryIds(OS))
24632463
exitWithError(std::move(E), Filename);
24642464

2465+
if (ShowProfileVersion)
2466+
OS << "Profile version: " << Reader->getVersion() << "\n";
24652467
return 0;
24662468
}
24672469

@@ -2786,7 +2788,8 @@ static int show_main(int argc, const char *argv[]) {
27862788
cl::opt<std::string> ProfiledBinary(
27872789
"profiled-binary", cl::init(""),
27882790
cl::desc("Path to binary from which the profile was collected."));
2789-
2791+
cl::opt<bool> ShowProfileVersion("profile-version", cl::init(false),
2792+
cl::desc("Show profile version. "));
27902793
cl::ParseCommandLineOptions(argc, argv, "LLVM profile data summary\n");
27912794

27922795
if (Filename.empty() && DebugInfoFilename.empty())
@@ -2817,7 +2820,7 @@ static int show_main(int argc, const char *argv[]) {
28172820
Filename, ShowCounts, TopNFunctions, ShowIndirectCallTargets,
28182821
ShowMemOPSizes, ShowDetailedSummary, DetailedSummaryCutoffs,
28192822
ShowAllFunctions, ShowCS, ValueCutoff, OnlyListBelow, ShowFunction,
2820-
TextFormat, ShowBinaryIds, ShowCovered, OS);
2823+
TextFormat, ShowBinaryIds, ShowCovered, ShowProfileVersion, OS);
28212824
if (ProfileKind == sample)
28222825
return showSampleProfile(
28232826
Filename, ShowCounts, TopNFunctions, ShowAllFunctions,

0 commit comments

Comments
 (0)