From d4332eb3b792f614d77d4e2a07e71a8cb42267a2 Mon Sep 17 00:00:00 2001 From: Florian Hahn Date: Fri, 20 Apr 2018 10:18:36 +0000 Subject: [PATCH] [LTO] Add stats-file option to LTO/Config.h. This patch adds a StatsFile option to LTO/Config.h and updates both LLVMGold and llvm-lto2 to set it. Reviewers: MatzeB, tejohnson, espindola Reviewed By: tejohnson Differential Revision: https://reviews.llvm.org/D45531 llvm-svn: 330411 --- llvm/include/llvm/LTO/Config.h | 3 +++ llvm/lib/LTO/LTO.cpp | 24 +++++++++++++++++++--- llvm/test/tools/gold/X86/stats-file-option.ll | 23 +++++++++++++++++++++ llvm/test/tools/llvm-lto2/X86/stats-file-option.ll | 23 +++++++++++++++++++++ llvm/tools/gold/gold-plugin.cpp | 5 +++++ llvm/tools/llvm-lto2/llvm-lto2.cpp | 4 ++++ 6 files changed, 79 insertions(+), 3 deletions(-) create mode 100644 llvm/test/tools/gold/X86/stats-file-option.ll create mode 100644 llvm/test/tools/llvm-lto2/X86/stats-file-option.ll diff --git a/llvm/include/llvm/LTO/Config.h b/llvm/include/llvm/LTO/Config.h index 21ad781..2dac130 100644 --- a/llvm/include/llvm/LTO/Config.h +++ b/llvm/include/llvm/LTO/Config.h @@ -88,6 +88,9 @@ struct Config { /// Whether to emit the pass manager debuggging informations. bool DebugPassManager = false; + /// Statistics output file path. + std::string StatsFile; + bool ShouldDiscardValueNames = true; DiagnosticHandlerFunction DiagHandler; diff --git a/llvm/lib/LTO/LTO.cpp b/llvm/lib/LTO/LTO.cpp index e4dffbf..186e3d2 100644 --- a/llvm/lib/LTO/LTO.cpp +++ b/llvm/lib/LTO/LTO.cpp @@ -12,6 +12,7 @@ //===----------------------------------------------------------------------===// #include "llvm/LTO/LTO.h" +#include "llvm/ADT/Statistic.h" #include "llvm/Analysis/TargetLibraryInfo.h" #include "llvm/Analysis/TargetTransformInfo.h" #include "llvm/Bitcode/BitcodeReader.h" @@ -791,9 +792,26 @@ Error LTO::run(AddStreamFn AddStream, NativeObjectCache Cache) { }; computeDeadSymbols(ThinLTO.CombinedIndex, GUIDPreservedSymbols, isPrevailing); - if (auto E = runRegularLTO(AddStream)) - return E; - return runThinLTO(AddStream, Cache); + // Setup output file to emit statistics. + std::unique_ptr StatsFile = nullptr; + if (!Conf.StatsFile.empty()) { + EnableStatistics(false); + std::error_code EC; + StatsFile = + llvm::make_unique(Conf.StatsFile, EC, sys::fs::F_None); + if (EC) + return errorCodeToError(EC); + StatsFile->keep(); + } + + Error Result = runRegularLTO(AddStream); + if (!Result) + Result = runThinLTO(AddStream, Cache); + + if (StatsFile) + PrintStatisticsJSON(StatsFile->os()); + + return Result; } Error LTO::runRegularLTO(AddStreamFn AddStream) { diff --git a/llvm/test/tools/gold/X86/stats-file-option.ll b/llvm/test/tools/gold/X86/stats-file-option.ll new file mode 100644 index 0000000..ba3e926 --- /dev/null +++ b/llvm/test/tools/gold/X86/stats-file-option.ll @@ -0,0 +1,23 @@ +; RUN: llvm-as -o %t.bc %s + +; Try to save statistics to file. +; RUN: %gold -plugin %llvmshlibdir/LLVMgold%shlibext -plugin-opt=stats-file=%t2.stats \ +; RUN: -m elf_x86_64 -r -o %t.o %t.bc +; RUN: FileCheck --input-file=%t2.stats %s + +; CHECK: { +; CHECK: "asm-printer.EmittedInsts": +; CHECK: } + + +target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128" +target triple = "x86_64-unknown-linux-gnu" + +define i32 @foo() { + ret i32 10 +} + +; Try to save statistics to an invalid file. +; RUN: not %gold -plugin %llvmshlibdir/LLVMgold%shlibext -plugin-opt=stats-file=%t2/foo.stats \ +; RUN: -m elf_x86_64 -r -o %t.o %t.bc 2>&1 | FileCheck --check-prefix=ERROR %s +; ERROR: LLVM gold plugin: No such file or directory diff --git a/llvm/test/tools/llvm-lto2/X86/stats-file-option.ll b/llvm/test/tools/llvm-lto2/X86/stats-file-option.ll new file mode 100644 index 0000000..61c06a3 --- /dev/null +++ b/llvm/test/tools/llvm-lto2/X86/stats-file-option.ll @@ -0,0 +1,23 @@ +; RUN: llvm-as < %s > %t1.bc + +; Try to save statistics to file. +; RUN: llvm-lto2 run %t1.bc -o %t.o -r %t1.bc,patatino,px -stats-file=%t2.stats +; RUN: FileCheck --input-file=%t2.stats %s + +target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128" +target triple = "x86_64-unknown-linux-gnu" + +define void @patatino() { + fence seq_cst + ret void +} + +; CHECK: { +; CHECK: "asm-printer.EmittedInsts": +; CHECK: } + + +; Try to save statistics to an invalid file. +; RUN: not llvm-lto2 run %t1.bc -o %t.o -r %t1.bc,patatino,px \ +; RUN: -stats-file=%t2/foo.stats 2>&1 | FileCheck --check-prefix=ERROR %s +; ERROR: LTO::run failed: No such file or directory diff --git a/llvm/tools/gold/gold-plugin.cpp b/llvm/tools/gold/gold-plugin.cpp index e39abe6..e505824 100644 --- a/llvm/tools/gold/gold-plugin.cpp +++ b/llvm/tools/gold/gold-plugin.cpp @@ -203,6 +203,8 @@ namespace options { static std::string objcopy; // Directory to store the .dwo files. static std::string dwo_dir; + /// Statistics output filename. + static std::string stats_file; // Optimization remarks filename and hotness options static std::string OptRemarksFilename; @@ -278,6 +280,8 @@ namespace options { OptRemarksFilename = opt.substr(strlen("opt-remarks-filename=")); } else if (opt == "opt-remarks-with-hotness") { OptRemarksWithHotness = true; + } else if (opt.startswith("stats-file=")) { + stats_file = opt.substr(strlen("stats-file=")); } else { // Save this option to pass to the code generator. // ParseCommandLineOptions() expects argv[0] to be program name. Lazily @@ -899,6 +903,7 @@ static std::unique_ptr createLTO(IndexWriteCallback OnIndexWrite, // Debug new pass manager if requested Conf.DebugPassManager = options::debug_pass_manager; + Conf.StatsFile = options::stats_file; return llvm::make_unique(std::move(Conf), Backend, options::ParallelCodeGenParallelismLevel); } diff --git a/llvm/tools/llvm-lto2/llvm-lto2.cpp b/llvm/tools/llvm-lto2/llvm-lto2.cpp index 1305aa5..aab0f6f 100644 --- a/llvm/tools/llvm-lto2/llvm-lto2.cpp +++ b/llvm/tools/llvm-lto2/llvm-lto2.cpp @@ -113,6 +113,9 @@ static cl::opt DebugPassManager("debug-pass-manager", cl::init(false), cl::Hidden, cl::desc("Print pass management debugging information")); +static cl::opt + StatsFile("stats-file", cl::desc("Filename to write statistics to")); + static void check(Error E, std::string Msg) { if (!E) return; @@ -240,6 +243,7 @@ static int run(int argc, char **argv) { Conf.OverrideTriple = OverrideTriple; Conf.DefaultTriple = DefaultTriple; + Conf.StatsFile = StatsFile; ThinBackend Backend; if (ThinLTODistributedIndexes) -- 2.7.4