[llvm] 901f555 - [llvm-profdata] Add --output-format option

Ellis Hoag via llvm-commits llvm-commits at lists.llvm.org
Fri Oct 7 09:47:36 PDT 2022


Author: Ellis Hoag
Date: 2022-10-07T09:47:23-07:00
New Revision: 901f555ecade4951871cb62a93a855c9806f9ed5

URL: https://github.com/llvm/llvm-project/commit/901f555ecade4951871cb62a93a855c9806f9ed5
DIFF: https://github.com/llvm/llvm-project/commit/901f555ecade4951871cb62a93a855c9806f9ed5.diff

LOG: [llvm-profdata] Add --output-format option

Add `--output-format` option for the `llvm-profdata show` command to select the type of output. The existing `--text` flag is used to emit text encoded profiles. To avoid confusion, `--output-format=text-encoding` indicates that the output will be profiles encoded in the text format, and `--output-format=text` indicates the default text output that doesn't necessarily represent a profile.
`--output-format=json` is an alias for `--json` and `--output-format=yaml` will be used in D134770.

Reviewed By: phosek

Differential Revision: https://reviews.llvm.org/D135127

Added: 
    

Modified: 
    llvm/docs/CommandGuide/llvm-profdata.rst
    llvm/test/tools/llvm-profdata/sample-profile-json.test
    llvm/tools/llvm-profdata/llvm-profdata.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/docs/CommandGuide/llvm-profdata.rst b/llvm/docs/CommandGuide/llvm-profdata.rst
index e3850c69cb203..12feed5eb690b 100644
--- a/llvm/docs/CommandGuide/llvm-profdata.rst
+++ b/llvm/docs/CommandGuide/llvm-profdata.rst
@@ -253,6 +253,10 @@ OPTIONS
 
  Print the counter values for the displayed functions.
 
+.. option:: --output-format=<json|yaml>
+
+ Emit output in the selected format if supported by the provided profile type.
+
 .. option:: --function=<string>
 
  Print details for a function if the function's name contains the given string.

diff  --git a/llvm/test/tools/llvm-profdata/sample-profile-json.test b/llvm/test/tools/llvm-profdata/sample-profile-json.test
index c9f65ea091409..9a19ab0c74e95 100644
--- a/llvm/test/tools/llvm-profdata/sample-profile-json.test
+++ b/llvm/test/tools/llvm-profdata/sample-profile-json.test
@@ -1,4 +1,5 @@
 RUN: llvm-profdata show --sample --json %p/Inputs/sample-profile.proftext | FileCheck %s --check-prefix=JSON
+RUN: llvm-profdata show --sample --output-format=json %p/Inputs/sample-profile.proftext | FileCheck %s --check-prefix=JSON
 JSON:      [
 JSON-NEXT:   {
 JSON-NEXT:     "name": "main",

diff  --git a/llvm/tools/llvm-profdata/llvm-profdata.cpp b/llvm/tools/llvm-profdata/llvm-profdata.cpp
index ab20782aaad74..3fe91f4b570c5 100644
--- a/llvm/tools/llvm-profdata/llvm-profdata.cpp
+++ b/llvm/tools/llvm-profdata/llvm-profdata.cpp
@@ -57,6 +57,8 @@ enum ProfileFormat {
   PF_Binary
 };
 
+enum class OutputFormat { None, Json, Yaml };
+
 static void warn(Twine Message, std::string Whence = "",
                  std::string Hint = "") {
   WithColor::warning();
@@ -2252,7 +2254,12 @@ static int showInstrProfile(const std::string &Filename, bool ShowCounts,
                             uint64_t ValueCutoff, bool OnlyListBelow,
                             const std::string &ShowFunction, bool TextFormat,
                             bool ShowBinaryIds, bool ShowCovered,
-                            bool ShowProfileVersion, raw_fd_ostream &OS) {
+                            bool ShowProfileVersion, OutputFormat OFormat,
+                            raw_fd_ostream &OS) {
+  if (OFormat == OutputFormat::Json)
+    exitWithError("JSON output is not supported for instr profiles");
+  if (OFormat == OutputFormat::Yaml)
+    exitWithError("YAML output is not supported for instr profiles");
   auto ReaderOrErr = InstrProfReader::create(Filename);
   std::vector<uint32_t> Cutoffs = std::move(DetailedSummaryCutoffs);
   if (ShowDetailedSummary && Cutoffs.empty()) {
@@ -2618,7 +2625,9 @@ static int showSampleProfile(const std::string &Filename, bool ShowCounts,
                              const std::string &ShowFunction,
                              bool ShowProfileSymbolList,
                              bool ShowSectionInfoOnly, bool ShowHotFuncList,
-                             bool JsonFormat, raw_fd_ostream &OS) {
+                             OutputFormat OFormat, raw_fd_ostream &OS) {
+  if (OFormat == OutputFormat::Yaml)
+    exitWithError("YAML output is not supported for sample profiles");
   using namespace sampleprof;
   LLVMContext Context;
   auto ReaderOrErr =
@@ -2636,12 +2645,12 @@ static int showSampleProfile(const std::string &Filename, bool ShowCounts,
     exitWithErrorCode(EC, Filename);
 
   if (ShowAllFunctions || ShowFunction.empty()) {
-    if (JsonFormat)
+    if (OFormat == OutputFormat::Json)
       Reader->dumpJson(OS);
     else
       Reader->dump(OS);
   } else {
-    if (JsonFormat)
+    if (OFormat == OutputFormat::Json)
       exitWithError(
           "the JSON format is supported only when all functions are to "
           "be printed");
@@ -2670,7 +2679,9 @@ static int showSampleProfile(const std::string &Filename, bool ShowCounts,
 
 static int showMemProfProfile(const std::string &Filename,
                               const std::string &ProfiledBinary,
-                              raw_fd_ostream &OS) {
+                              OutputFormat OFormat, raw_fd_ostream &OS) {
+  if (OFormat == OutputFormat::Json)
+    exitWithError("JSON output is not supported for MemProf");
   auto ReaderOr = llvm::memprof::RawMemProfReader::create(
       Filename, ProfiledBinary, /*KeepNames=*/true);
   if (Error E = ReaderOr.takeError())
@@ -2689,10 +2700,15 @@ static int showMemProfProfile(const std::string &Filename,
 static int showDebugInfoCorrelation(const std::string &Filename,
                                     bool ShowDetailedSummary,
                                     bool ShowProfileSymbolList,
-                                    raw_fd_ostream &OS) {
+                                    OutputFormat OFormat, raw_fd_ostream &OS) {
+  if (OFormat == OutputFormat::Json)
+    exitWithError("JSON output is not supported for debug info correlation");
+  if (OFormat == OutputFormat::Yaml)
+    exitWithError("YAML output is not supported for debug info correlation");
   std::unique_ptr<InstrProfCorrelator> Correlator;
   if (auto Err = InstrProfCorrelator::get(Filename).moveInto(Correlator))
     exitWithError(std::move(Err), Filename);
+
   if (auto Err = Correlator->correlateProfileData())
     exitWithError(std::move(Err), Filename);
 
@@ -2718,12 +2734,17 @@ static int show_main(int argc, const char *argv[]) {
 
   cl::opt<bool> ShowCounts("counts", cl::init(false),
                            cl::desc("Show counter values for shown functions"));
+  cl::opt<OutputFormat> OFormat(
+      "output-format", cl::init(OutputFormat::None),
+      cl::desc("Emit output in the selected format if supported"),
+      cl::values(clEnumValN(OutputFormat::Json, "json", "emit JSON"),
+                 clEnumValN(OutputFormat::Yaml, "yaml", "emit YAML")));
   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"));
+      "json", cl::desc("Show sample profile data in the JSON format "
+                       "(deprecated, please use --output-format=json)"));
   cl::opt<bool> ShowIndirectCallTargets(
       "ic-targets", cl::init(false),
       cl::desc("Show indirect call site target values for shown functions"));
@@ -2802,6 +2823,8 @@ static int show_main(int argc, const char *argv[]) {
            << ": Input file name cannot be the same as the output file name!\n";
     return 1;
   }
+  if (JsonFormat)
+    OFormat = OutputFormat::Json;
 
   std::error_code EC;
   raw_fd_ostream OS(OutputFilename.data(), EC, sys::fs::OF_TextWithCRLF);
@@ -2813,20 +2836,21 @@ static int show_main(int argc, const char *argv[]) {
 
   if (!DebugInfoFilename.empty())
     return showDebugInfoCorrelation(DebugInfoFilename, ShowDetailedSummary,
-                                    ShowProfileSymbolList, OS);
+                                    ShowProfileSymbolList, OFormat, OS);
 
   if (ProfileKind == instr)
     return showInstrProfile(
         Filename, ShowCounts, TopNFunctions, ShowIndirectCallTargets,
         ShowMemOPSizes, ShowDetailedSummary, DetailedSummaryCutoffs,
         ShowAllFunctions, ShowCS, ValueCutoff, OnlyListBelow, ShowFunction,
-        TextFormat, ShowBinaryIds, ShowCovered, ShowProfileVersion, OS);
+        TextFormat, ShowBinaryIds, ShowCovered, ShowProfileVersion, OFormat,
+        OS);
   if (ProfileKind == sample)
-    return showSampleProfile(
-        Filename, ShowCounts, TopNFunctions, ShowAllFunctions,
-        ShowDetailedSummary, ShowFunction, ShowProfileSymbolList,
-        ShowSectionInfoOnly, ShowHotFuncList, JsonFormat, OS);
-  return showMemProfProfile(Filename, ProfiledBinary, OS);
+    return showSampleProfile(Filename, ShowCounts, TopNFunctions,
+                             ShowAllFunctions, ShowDetailedSummary,
+                             ShowFunction, ShowProfileSymbolList,
+                             ShowSectionInfoOnly, ShowHotFuncList, OFormat, OS);
+  return showMemProfProfile(Filename, ProfiledBinary, OFormat, OS);
 }
 
 int llvm_profdata_main(int argc, char **argvNonConst) {


        


More information about the llvm-commits mailing list