[BOLT] Control aggregation mode output profile file format
authorAmir Ayupov <aaupov@fb.com>
Mon, 19 Sep 2022 20:36:50 +0000 (13:36 -0700)
committerAmir Ayupov <aaupov@fb.com>
Mon, 19 Sep 2022 20:37:10 +0000 (13:37 -0700)
In perf2bolt and `-aggregate-only` BOLT mode, the output profile file is written
in fdata format by default. Provide a knob `-profile-format=[fdata,yaml]` to
control the format.
Note that `-w` option still dumps in YAML format.

Reviewed By: #bolt, maksfb

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

bolt/include/bolt/Utils/CommandLineOpts.h
bolt/lib/Profile/DataAggregator.cpp
bolt/lib/Rewrite/RewriteInstance.cpp
bolt/lib/Utils/CommandLineOpts.cpp
bolt/test/X86/pre-aggregated-perf.test

index 9216048..b7cca81 100644 (file)
@@ -49,6 +49,11 @@ extern llvm::cl::opt<std::string> OutputFilename;
 extern llvm::cl::opt<std::string> PerfData;
 extern llvm::cl::opt<bool> PrintCacheMetrics;
 extern llvm::cl::opt<bool> PrintSections;
+
+// The format to use with -o in aggregation mode (perf2bolt)
+enum ProfileFormatKind { PF_Fdata, PF_YAML };
+
+extern llvm::cl::opt<ProfileFormatKind> ProfileFormat;
 extern llvm::cl::opt<bool> SplitEH;
 extern llvm::cl::opt<bool> StrictMode;
 extern llvm::cl::opt<bool> TimeOpts;
index 6ac315e..1e45af7 100644 (file)
@@ -78,6 +78,8 @@ MaxSamples("max-samples",
   cl::Hidden,
   cl::cat(AggregatorCategory));
 
+extern cl::opt<opts::ProfileFormatKind> ProfileFormat;
+
 cl::opt<bool> ReadPreAggregated(
     "pa", cl::desc("skip perf and read data from a pre-aggregated file format"),
     cl::cat(AggregatorCategory));
@@ -610,7 +612,8 @@ Error DataAggregator::readProfile(BinaryContext &BC) {
     convertBranchData(Function);
   }
 
-  if (opts::AggregateOnly) {
+  if (opts::AggregateOnly &&
+      opts::ProfileFormat == opts::ProfileFormatKind::PF_Fdata) {
     if (std::error_code EC = writeAggregatedFile(opts::OutputFilename))
       report_error("cannot create output data file", EC);
   }
index 5c2d124..434bea0 100644 (file)
@@ -2838,6 +2838,11 @@ void RewriteInstance::processProfileData() {
     YAMLProfileWriter PW(opts::SaveProfile);
     PW.writeProfile(*this);
   }
+  if (opts::AggregateOnly &&
+      opts::ProfileFormat == opts::ProfileFormatKind::PF_YAML) {
+    YAMLProfileWriter PW(opts::OutputFilename);
+    PW.writeProfile(*this);
+  }
 
   // Release memory used by profile reader.
   ProfileReader.reset();
index b90a735..d17d68a 100644 (file)
@@ -154,6 +154,15 @@ cl::opt<bool> PrintSections("print-sections",
                             cl::desc("print all registered sections"),
                             cl::Hidden, cl::cat(BoltCategory));
 
+cl::opt<ProfileFormatKind> ProfileFormat(
+    "profile-format",
+    cl::desc(
+        "format to dump profile output in aggregation mode, default is fdata"),
+    cl::init(PF_Fdata),
+    cl::values(clEnumValN(PF_Fdata, "fdata", "offset-based plaintext format"),
+               clEnumValN(PF_YAML, "yaml", "dense YAML reprensentation")),
+    cl::ZeroOrMore, cl::Hidden, cl::cat(BoltCategory));
+
 cl::opt<bool> SplitEH("split-eh", cl::desc("split C++ exception handling code"),
                       cl::Hidden, cl::cat(BoltOptCategory));
 
index 4421c76..c737034 100644 (file)
 REQUIRES: system-linux
 
 RUN: yaml2obj %p/Inputs/blarge.yaml &> %t.exe
-RUN: perf2bolt %t.exe -o %t -pa -p %p/Inputs/pre-aggregated.txt -w %t.new
+RUN: perf2bolt %t.exe -o %t --pa -p %p/Inputs/pre-aggregated.txt -w %t.new
 RUN: cat %t | sort | FileCheck %s -check-prefix=PERF2BOLT
 RUN: cat %t.new | FileCheck %s -check-prefix=NEWFORMAT
 
+# Test --profile-format option with perf2bolt
+RUN: perf2bolt %t.exe -o %t.fdata --pa -p %p/Inputs/pre-aggregated.txt \
+RUN:   --profile-format=fdata
+RUN: cat %t.fdata | sort | FileCheck %s -check-prefix=PERF2BOLT
+
+RUN: perf2bolt %t.exe -o %t.yaml --pa -p %p/Inputs/pre-aggregated.txt \
+RUN:   --profile-format=yaml
+RUN: cat %t.yaml | FileCheck %s -check-prefix=NEWFORMAT
+
+# Test --profile-format option with llvm-bolt --aggregate-only
+RUN: llvm-bolt %t.exe -o %t.bolt.fdata --pa -p %p/Inputs/pre-aggregated.txt \
+RUN:   --aggregate-only --profile-format=fdata
+RUN: cat %t.bolt.fdata | sort | FileCheck %s -check-prefix=PERF2BOLT
+
+RUN: llvm-bolt %t.exe -o %t.bolt.yaml --pa -p %p/Inputs/pre-aggregated.txt \
+RUN:   --aggregate-only --profile-format=yaml
+RUN: cat %t.bolt.yaml | FileCheck %s -check-prefix=NEWFORMAT
+
 PERF2BOLT: 0 [unknown] 7f36d18d60c0 1 main 53c 0 2
 PERF2BOLT: 1 main 451 1 SolveCubic 0 0 2
 PERF2BOLT: 1 main 490 0 [unknown] 4005f0 0 1