]> granicus.if.org Git - llvm/commitdiff
[LTO] Add plumbing to save stats during LTO on Darwin.
authorFlorian Hahn <flo@fhahn.com>
Fri, 19 Apr 2019 12:36:41 +0000 (12:36 +0000)
committerFlorian Hahn <flo@fhahn.com>
Fri, 19 Apr 2019 12:36:41 +0000 (12:36 +0000)
Gold and ld on Linux already support saving stats, but the
infrastructure is missing on Darwin. Unfortunately it seems like the
configuration from lib/LTO/LTO.cpp is not used.

This patch adds a new LTOStatsFile option and adds plumbing in Clang to
use it on Darwin, similar to the way remarks are handled.

Currnetly the handling of LTO flags seems quite spread out, with a bunch
of duplication. But I am not sure if there is an easy way to improve
that?

Reviewers: anemet, tejohnson, thegameg, steven_wu

Reviewed By: steven_wu

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

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@358753 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/LTO/LTO.h
include/llvm/LTO/legacy/LTOCodeGenerator.h
lib/LTO/LTO.cpp
lib/LTO/LTOCodeGenerator.cpp

index 24e80fbd09e8cd4b7bac63008dad5f153549d372..8fcf1075721c47ba79155ab26ee5bb2413abc140 100644 (file)
@@ -87,6 +87,10 @@ setupOptimizationRemarks(LLVMContext &Context, StringRef LTORemarksFilename,
                          StringRef LTORemarksPasses,
                          bool LTOPassRemarksWithHotness, int Count = -1);
 
+/// Setups the output file for saving statistics.
+Expected<std::unique_ptr<ToolOutputFile>>
+setupStatsFile(StringRef StatsFilename);
+
 class LTO;
 struct SymbolResolution;
 class ThinBackendProc;
index 37fef191a28c3fb3bd44f167a6350a8384368374..d3cb4c8b79a0084f805507d449b6f40875766f83 100644 (file)
@@ -241,6 +241,7 @@ private:
   TargetMachine::CodeGenFileType FileType = TargetMachine::CGFT_ObjectFile;
   std::unique_ptr<ToolOutputFile> DiagnosticOutputFile;
   bool Freestanding = false;
+  std::unique_ptr<ToolOutputFile> StatsFile = nullptr;
 };
 }
 #endif
index 1accbf40b9647b4549da3d68b2e11012bb814ae5..ab5b78071c67731c4f8196c266dc1e80d74a04cf 100644 (file)
@@ -885,16 +885,10 @@ Error LTO::run(AddStreamFn AddStream, NativeObjectCache Cache) {
                                   isPrevailing, Conf.OptLevel > 0);
 
   // Setup output file to emit statistics.
-  std::unique_ptr<ToolOutputFile> StatsFile = nullptr;
-  if (!Conf.StatsFile.empty()) {
-    EnableStatistics(false);
-    std::error_code EC;
-    StatsFile =
-        llvm::make_unique<ToolOutputFile>(Conf.StatsFile, EC, sys::fs::F_None);
-    if (EC)
-      return errorCodeToError(EC);
-    StatsFile->keep();
-  }
+  auto StatsFileOrErr = setupStatsFile(Conf.StatsFile);
+  if (!StatsFileOrErr)
+    return StatsFileOrErr.takeError();
+  std::unique_ptr<ToolOutputFile> StatsFile = std::move(StatsFileOrErr.get());
 
   // Finalize linking of regular LTO modules containing summaries now that
   // we have computed liveness information.
@@ -1343,3 +1337,20 @@ lto::setupOptimizationRemarks(LLVMContext &Context,
   DiagnosticFile->keep();
   return std::move(DiagnosticFile);
 }
+
+Expected<std::unique_ptr<ToolOutputFile>>
+lto::setupStatsFile(StringRef StatsFilename) {
+  // Setup output file to emit statistics.
+  if (StatsFilename.empty())
+    return nullptr;
+
+  llvm::EnableStatistics(false);
+  std::error_code EC;
+  auto StatsFile =
+      llvm::make_unique<ToolOutputFile>(StatsFilename, EC, sys::fs::F_None);
+  if (EC)
+    return errorCodeToError(EC);
+
+  StatsFile->keep();
+  return std::move(StatsFile);
+}
index f02907f7a86189aa3478cda0f3dcd533d637d293..79d41ee896692c200b4cb8fab0c7f51b74c2cb9b 100644 (file)
@@ -95,6 +95,11 @@ cl::opt<bool> LTOPassRemarksWithHotness(
     "lto-pass-remarks-with-hotness",
     cl::desc("With PGO, include profile count in optimization remarks"),
     cl::Hidden);
+
+cl::opt<std::string> LTOStatsFile(
+    "lto-stats-file",
+    cl::desc("Save statistics to the specified file"),
+    cl::Hidden);
 }
 
 LTOCodeGenerator::LTOCodeGenerator(LLVMContext &Context)
@@ -518,6 +523,14 @@ bool LTOCodeGenerator::optimize(bool DisableVerify, bool DisableInline,
   }
   DiagnosticOutputFile = std::move(*DiagFileOrErr);
 
+  // Setup output file to emit statistics.
+  auto StatsFileOrErr = lto::setupStatsFile(LTOStatsFile);
+  if (!StatsFileOrErr) {
+    errs() << "Error: " << toString(StatsFileOrErr.takeError()) << "\n";
+    report_fatal_error("Can't get an output file for the statistics");
+  }
+  StatsFile = std::move(StatsFileOrErr.get());
+
   // We always run the verifier once on the merged module, the `DisableVerify`
   // parameter only applies to subsequent verify.
   verifyMergedModuleOnce();
@@ -584,9 +597,13 @@ bool LTOCodeGenerator::compileOptimized(ArrayRef<raw_pwrite_stream *> Out) {
                               [&]() { return createTargetMachine(); }, FileType,
                               ShouldRestoreGlobalsLinkage);
 
-  // If statistics were requested, print them out after codegen.
-  if (llvm::AreStatisticsEnabled())
-    llvm::PrintStatistics();
+  // If statistics were requested, save them to the specified file or
+  // print them out after codegen.
+  if (StatsFile)
+    PrintStatisticsJSON(StatsFile->os());
+  else if (AreStatisticsEnabled())
+    PrintStatistics();
+
   reportAndResetTimings();
 
   finishOptimizationRemarks();