diff --git a/llvm/tools/llvm-profdata/llvm-profdata.cpp b/llvm/tools/llvm-profdata/llvm-profdata.cpp --- a/llvm/tools/llvm-profdata/llvm-profdata.cpp +++ b/llvm/tools/llvm-profdata/llvm-profdata.cpp @@ -57,6 +57,8 @@ PF_Binary }; +enum class ShowOutputFormat { Text, TextEncoding, Json, Yaml }; + static void warn(Twine Message, std::string Whence = "", std::string Hint = "") { WithColor::warning(); @@ -2250,9 +2252,14 @@ std::vector DetailedSummaryCutoffs, bool ShowAllFunctions, bool ShowCS, uint64_t ValueCutoff, bool OnlyListBelow, - const std::string &ShowFunction, bool TextFormat, - bool ShowBinaryIds, bool ShowCovered, - raw_fd_ostream &OS) { + const std::string &ShowFunction, + ShowOutputFormat OutputFormat, bool ShowBinaryIds, + bool ShowCovered, raw_fd_ostream &OS) { + if (OutputFormat == ShowOutputFormat::Json) + exitWithError("JSON output is not supported for instr profiles"); + if (OutputFormat == ShowOutputFormat::Yaml) + exitWithError("YAML output is not supported for instr profiles"); + bool TextFormat = (OutputFormat == ShowOutputFormat::TextEncoding); auto ReaderOrErr = InstrProfReader::create(Filename); std::vector Cutoffs = std::move(DetailedSummaryCutoffs); if (ShowDetailedSummary && Cutoffs.empty()) { @@ -2610,13 +2617,16 @@ return 0; } -static int showSampleProfile(const std::string &Filename, bool ShowCounts, - uint32_t TopN, bool ShowAllFunctions, - bool ShowDetailedSummary, - const std::string &ShowFunction, - bool ShowProfileSymbolList, - bool ShowSectionInfoOnly, bool ShowHotFuncList, - bool JsonFormat, raw_fd_ostream &OS) { +static int +showSampleProfile(const std::string &Filename, bool ShowCounts, uint32_t TopN, + bool ShowAllFunctions, bool ShowDetailedSummary, + const std::string &ShowFunction, bool ShowProfileSymbolList, + bool ShowSectionInfoOnly, bool ShowHotFuncList, + ShowOutputFormat OutputFormat, raw_fd_ostream &OS) { + if (OutputFormat == ShowOutputFormat::TextEncoding) + exitWithError("TextEncoding output is not supported for sample profiles"); + if (OutputFormat == ShowOutputFormat::Yaml) + exitWithError("YAML output is not supported for sample profiles"); using namespace sampleprof; LLVMContext Context; auto ReaderOrErr = @@ -2634,12 +2644,12 @@ exitWithErrorCode(EC, Filename); if (ShowAllFunctions || ShowFunction.empty()) { - if (JsonFormat) + if (OutputFormat == ShowOutputFormat::Json) Reader->dumpJson(OS); else Reader->dump(OS); } else { - if (JsonFormat) + if (OutputFormat != ShowOutputFormat::Text) exitWithError( "the JSON format is supported only when all functions are to " "be printed"); @@ -2685,12 +2695,21 @@ } static int showDebugInfoCorrelation(const std::string &Filename, + ShowOutputFormat OutputFormat, bool ShowDetailedSummary, bool ShowProfileSymbolList, raw_fd_ostream &OS) { std::unique_ptr Correlator; if (auto Err = InstrProfCorrelator::get(Filename).moveInto(Correlator)) exitWithError(std::move(Err), Filename); + if (OutputFormat == ShowOutputFormat::TextEncoding) + exitWithError( + "TextEncoding output is not supported by for debug info correlation"); + if (OutputFormat == ShowOutputFormat::Json) + exitWithError("JSON output is not supported by for debug info correlation"); + if (OutputFormat == ShowOutputFormat::Yaml) + exitWithError("YAML output is not supported by for debug info correlation"); + if (auto Err = Correlator->correlateProfileData()) exitWithError(std::move(Err), Filename); @@ -2716,12 +2735,22 @@ cl::opt ShowCounts("counts", cl::init(false), cl::desc("Show counter values for shown functions")); + cl::opt OutputFormat( + "output-format", cl::desc("The output format for the show command"), + cl::init(ShowOutputFormat::Text), + cl::values( + clEnumValN(ShowOutputFormat::Text, "text", "print text output"), + clEnumValN(ShowOutputFormat::TextEncoding, "test-encoding", + "emit text encoding"), + clEnumValN(ShowOutputFormat::Json, "json", "emit JSON encoding"), + clEnumValN(ShowOutputFormat::Yaml, "yaml", "emit YAML encoding"))); cl::opt TextFormat( - "text", cl::init(false), - cl::desc("Show instr profile data in text dump format")); + "text", + cl::desc("Show instr profile data in text dump format " + "(deprecated, please use --output-format=text-encoding)")); cl::opt JsonFormat( - "json", cl::init(false), - cl::desc("Show sample profile data in the JSON format")); + "json", cl::desc("Show sample profile data in the JSON format " + "(deprecated, please use --output-format=json)")); cl::opt ShowIndirectCallTargets( "ic-targets", cl::init(false), cl::desc("Show indirect call site target values for shown functions")); @@ -2799,6 +2828,10 @@ << ": Input file name cannot be the same as the output file name!\n"; return 1; } + if (TextFormat) + OutputFormat = ShowOutputFormat::TextEncoding; + if (JsonFormat) + OutputFormat = ShowOutputFormat::Json; std::error_code EC; raw_fd_ostream OS(OutputFilename.data(), EC, sys::fs::OF_TextWithCRLF); @@ -2809,20 +2842,21 @@ WithColor::warning() << "-function argument ignored: showing all functions\n"; if (!DebugInfoFilename.empty()) - return showDebugInfoCorrelation(DebugInfoFilename, ShowDetailedSummary, - ShowProfileSymbolList, OS); + return showDebugInfoCorrelation(DebugInfoFilename, OutputFormat, + ShowDetailedSummary, ShowProfileSymbolList, + OS); if (ProfileKind == instr) return showInstrProfile( Filename, ShowCounts, TopNFunctions, ShowIndirectCallTargets, ShowMemOPSizes, ShowDetailedSummary, DetailedSummaryCutoffs, ShowAllFunctions, ShowCS, ValueCutoff, OnlyListBelow, ShowFunction, - TextFormat, ShowBinaryIds, ShowCovered, OS); + OutputFormat, ShowBinaryIds, ShowCovered, OS); if (ProfileKind == sample) return showSampleProfile( Filename, ShowCounts, TopNFunctions, ShowAllFunctions, ShowDetailedSummary, ShowFunction, ShowProfileSymbolList, - ShowSectionInfoOnly, ShowHotFuncList, JsonFormat, OS); + ShowSectionInfoOnly, ShowHotFuncList, OutputFormat, OS); return showMemProfProfile(Filename, ProfiledBinary, OS); }