aboutsummaryrefslogtreecommitdiff
path: root/llvm/tools/llvm-profdata/llvm-profdata.cpp
diff options
context:
space:
mode:
authorKazu Hirata <kazu@google.com>2022-08-09 16:24:53 -0700
committerKazu Hirata <kazu@google.com>2022-08-09 16:24:53 -0700
commita044d0491efeb821f1f23a935515692984b21ad5 (patch)
treea98da0b39e8c4fb6ff95383d5504fa4de58c32ed /llvm/tools/llvm-profdata/llvm-profdata.cpp
parentdf9a23e2feda18f0308b6d4dbd591ebe6b605aa4 (diff)
downloadllvm-a044d0491efeb821f1f23a935515692984b21ad5.zip
llvm-a044d0491efeb821f1f23a935515692984b21ad5.tar.gz
llvm-a044d0491efeb821f1f23a935515692984b21ad5.tar.bz2
[llvm-profdata] Support JSON as as an output-only format
This patch teaches llvm-profdata to output the sample profile in the JSON format. The new option is intended to be used for research and development purposes. For example, one can write a Python script to take a JSON file and analyze how similar different inline instances of a given function are to each other. I've chosen JSON because Python can parse it reasonably fast, and it just takes a couple of lines to read the whole data: import json with open ('profile.json') as f: profile = json.load(f) Differential Revision: https://reviews.llvm.org/D130944
Diffstat (limited to 'llvm/tools/llvm-profdata/llvm-profdata.cpp')
-rw-r--r--llvm/tools/llvm-profdata/llvm-profdata.cpp28
1 files changed, 20 insertions, 8 deletions
diff --git a/llvm/tools/llvm-profdata/llvm-profdata.cpp b/llvm/tools/llvm-profdata/llvm-profdata.cpp
index 3af8f80..e392128 100644
--- a/llvm/tools/llvm-profdata/llvm-profdata.cpp
+++ b/llvm/tools/llvm-profdata/llvm-profdata.cpp
@@ -2488,7 +2488,7 @@ static int showSampleProfile(const std::string &Filename, bool ShowCounts,
const std::string &ShowFunction,
bool ShowProfileSymbolList,
bool ShowSectionInfoOnly, bool ShowHotFuncList,
- raw_fd_ostream &OS) {
+ bool JsonFormat, raw_fd_ostream &OS) {
using namespace sampleprof;
LLVMContext Context;
auto ReaderOrErr =
@@ -2505,11 +2505,20 @@ static int showSampleProfile(const std::string &Filename, bool ShowCounts,
if (std::error_code EC = Reader->read())
exitWithErrorCode(EC, Filename);
- if (ShowAllFunctions || ShowFunction.empty())
- Reader->dump(OS);
- else
+ if (ShowAllFunctions || ShowFunction.empty()) {
+ if (JsonFormat)
+ Reader->dumpJson(OS);
+ else
+ Reader->dump(OS);
+ } else {
+ if (JsonFormat)
+ exitWithError(
+ "the JSON format is supported only when all functions are to "
+ "be printed");
+
// TODO: parse context string to support filtering by contexts.
Reader->dumpFunctionProfile(StringRef(ShowFunction), OS);
+ }
if (ShowProfileSymbolList) {
std::unique_ptr<sampleprof::ProfileSymbolList> ReaderList =
@@ -2582,6 +2591,9 @@ static int show_main(int argc, const char *argv[]) {
cl::opt<bool> TextFormat(
"text", cl::init(false),
cl::desc("Show instr profile data in text dump format"));
+ cl::opt<bool> JsonFormat(
+ "json", cl::init(false),
+ cl::desc("Show sample profile data in the JSON format"));
cl::opt<bool> ShowIndirectCallTargets(
"ic-targets", cl::init(false),
cl::desc("Show indirect call site target values for shown functions"));
@@ -2679,10 +2691,10 @@ static int show_main(int argc, const char *argv[]) {
ShowAllFunctions, ShowCS, ValueCutoff, OnlyListBelow, ShowFunction,
TextFormat, ShowBinaryIds, ShowCovered, OS);
if (ProfileKind == sample)
- return showSampleProfile(Filename, ShowCounts, TopNFunctions,
- ShowAllFunctions, ShowDetailedSummary,
- ShowFunction, ShowProfileSymbolList,
- ShowSectionInfoOnly, ShowHotFuncList, OS);
+ return showSampleProfile(
+ Filename, ShowCounts, TopNFunctions, ShowAllFunctions,
+ ShowDetailedSummary, ShowFunction, ShowProfileSymbolList,
+ ShowSectionInfoOnly, ShowHotFuncList, JsonFormat, OS);
return showMemProfProfile(Filename, ProfiledBinary, OS);
}