Skip to content
This repository was archived by the owner on Mar 28, 2020. It is now read-only.

Commit a2fbfc9

Browse files
committed
[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 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@330411 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent b120305 commit a2fbfc9

File tree

6 files changed

+79
-3
lines changed

6 files changed

+79
-3
lines changed

include/llvm/LTO/Config.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,9 @@ struct Config {
8888
/// Whether to emit the pass manager debuggging informations.
8989
bool DebugPassManager = false;
9090

91+
/// Statistics output file path.
92+
std::string StatsFile;
93+
9194
bool ShouldDiscardValueNames = true;
9295
DiagnosticHandlerFunction DiagHandler;
9396

lib/LTO/LTO.cpp

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
//===----------------------------------------------------------------------===//
1313

1414
#include "llvm/LTO/LTO.h"
15+
#include "llvm/ADT/Statistic.h"
1516
#include "llvm/Analysis/TargetLibraryInfo.h"
1617
#include "llvm/Analysis/TargetTransformInfo.h"
1718
#include "llvm/Bitcode/BitcodeReader.h"
@@ -791,9 +792,26 @@ Error LTO::run(AddStreamFn AddStream, NativeObjectCache Cache) {
791792
};
792793
computeDeadSymbols(ThinLTO.CombinedIndex, GUIDPreservedSymbols, isPrevailing);
793794

794-
if (auto E = runRegularLTO(AddStream))
795-
return E;
796-
return runThinLTO(AddStream, Cache);
795+
// Setup output file to emit statistics.
796+
std::unique_ptr<ToolOutputFile> StatsFile = nullptr;
797+
if (!Conf.StatsFile.empty()) {
798+
EnableStatistics(false);
799+
std::error_code EC;
800+
StatsFile =
801+
llvm::make_unique<ToolOutputFile>(Conf.StatsFile, EC, sys::fs::F_None);
802+
if (EC)
803+
return errorCodeToError(EC);
804+
StatsFile->keep();
805+
}
806+
807+
Error Result = runRegularLTO(AddStream);
808+
if (!Result)
809+
Result = runThinLTO(AddStream, Cache);
810+
811+
if (StatsFile)
812+
PrintStatisticsJSON(StatsFile->os());
813+
814+
return Result;
797815
}
798816

799817
Error LTO::runRegularLTO(AddStreamFn AddStream) {
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
; RUN: llvm-as -o %t.bc %s
2+
3+
; Try to save statistics to file.
4+
; RUN: %gold -plugin %llvmshlibdir/LLVMgold%shlibext -plugin-opt=stats-file=%t2.stats \
5+
; RUN: -m elf_x86_64 -r -o %t.o %t.bc
6+
; RUN: FileCheck --input-file=%t2.stats %s
7+
8+
; CHECK: {
9+
; CHECK: "asm-printer.EmittedInsts":
10+
; CHECK: }
11+
12+
13+
target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
14+
target triple = "x86_64-unknown-linux-gnu"
15+
16+
define i32 @foo() {
17+
ret i32 10
18+
}
19+
20+
; Try to save statistics to an invalid file.
21+
; RUN: not %gold -plugin %llvmshlibdir/LLVMgold%shlibext -plugin-opt=stats-file=%t2/foo.stats \
22+
; RUN: -m elf_x86_64 -r -o %t.o %t.bc 2>&1 | FileCheck --check-prefix=ERROR %s
23+
; ERROR: LLVM gold plugin: No such file or directory
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
; RUN: llvm-as < %s > %t1.bc
2+
3+
; Try to save statistics to file.
4+
; RUN: llvm-lto2 run %t1.bc -o %t.o -r %t1.bc,patatino,px -stats-file=%t2.stats
5+
; RUN: FileCheck --input-file=%t2.stats %s
6+
7+
target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
8+
target triple = "x86_64-unknown-linux-gnu"
9+
10+
define void @patatino() {
11+
fence seq_cst
12+
ret void
13+
}
14+
15+
; CHECK: {
16+
; CHECK: "asm-printer.EmittedInsts":
17+
; CHECK: }
18+
19+
20+
; Try to save statistics to an invalid file.
21+
; RUN: not llvm-lto2 run %t1.bc -o %t.o -r %t1.bc,patatino,px \
22+
; RUN: -stats-file=%t2/foo.stats 2>&1 | FileCheck --check-prefix=ERROR %s
23+
; ERROR: LTO::run failed: No such file or directory

tools/gold/gold-plugin.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,8 @@ namespace options {
203203
static std::string objcopy;
204204
// Directory to store the .dwo files.
205205
static std::string dwo_dir;
206+
/// Statistics output filename.
207+
static std::string stats_file;
206208

207209
// Optimization remarks filename and hotness options
208210
static std::string OptRemarksFilename;
@@ -278,6 +280,8 @@ namespace options {
278280
OptRemarksFilename = opt.substr(strlen("opt-remarks-filename="));
279281
} else if (opt == "opt-remarks-with-hotness") {
280282
OptRemarksWithHotness = true;
283+
} else if (opt.startswith("stats-file=")) {
284+
stats_file = opt.substr(strlen("stats-file="));
281285
} else {
282286
// Save this option to pass to the code generator.
283287
// ParseCommandLineOptions() expects argv[0] to be program name. Lazily
@@ -899,6 +903,7 @@ static std::unique_ptr<LTO> createLTO(IndexWriteCallback OnIndexWrite,
899903
// Debug new pass manager if requested
900904
Conf.DebugPassManager = options::debug_pass_manager;
901905

906+
Conf.StatsFile = options::stats_file;
902907
return llvm::make_unique<LTO>(std::move(Conf), Backend,
903908
options::ParallelCodeGenParallelismLevel);
904909
}

tools/llvm-lto2/llvm-lto2.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,9 @@ static cl::opt<bool>
113113
DebugPassManager("debug-pass-manager", cl::init(false), cl::Hidden,
114114
cl::desc("Print pass management debugging information"));
115115

116+
static cl::opt<std::string>
117+
StatsFile("stats-file", cl::desc("Filename to write statistics to"));
118+
116119
static void check(Error E, std::string Msg) {
117120
if (!E)
118121
return;
@@ -240,6 +243,7 @@ static int run(int argc, char **argv) {
240243

241244
Conf.OverrideTriple = OverrideTriple;
242245
Conf.DefaultTriple = DefaultTriple;
246+
Conf.StatsFile = StatsFile;
243247

244248
ThinBackend Backend;
245249
if (ThinLTODistributedIndexes)

0 commit comments

Comments
 (0)