Skip to content

Commit 1cb383e

Browse files
committed
[Remarks] Extend -fsave-optimization-record to specify the format
Use -fsave-optimization-record=<format> to specify a different format than the default, which is YAML. For now, only YAML is supported. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@363573 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 3ea8c2a commit 1cb383e

File tree

14 files changed

+95
-18
lines changed

14 files changed

+95
-18
lines changed

include/llvm/IR/RemarkStreamer.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,20 @@ struct RemarkSetupPatternError : RemarkSetupErrorInfo<RemarkSetupPatternError> {
8585
using RemarkSetupErrorInfo<RemarkSetupPatternError>::RemarkSetupErrorInfo;
8686
};
8787

88+
struct RemarkSetupFormatError : RemarkSetupErrorInfo<RemarkSetupFormatError> {
89+
static char ID;
90+
using RemarkSetupErrorInfo<RemarkSetupFormatError>::RemarkSetupErrorInfo;
91+
};
92+
93+
enum class RemarksSerializerFormat { Unknown, YAML };
94+
95+
Expected<RemarksSerializerFormat> parseSerializerFormat(StringRef Format);
96+
8897
/// Setup optimization remarks.
8998
Expected<std::unique_ptr<ToolOutputFile>>
9099
setupOptimizationRemarks(LLVMContext &Context, StringRef RemarksFilename,
91-
StringRef RemarksPasses, bool RemarksWithHotness,
100+
StringRef RemarksPasses, StringRef RemarksFormat,
101+
bool RemarksWithHotness,
92102
unsigned RemarksHotnessThreshold = 0);
93103

94104
} // end namespace llvm

include/llvm/LTO/Config.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,9 @@ struct Config {
108108
/// Whether to emit optimization remarks with hotness informations.
109109
bool RemarksWithHotness = false;
110110

111+
/// The format used for serializing remarks (default: YAML).
112+
std::string RemarksFormat = "";
113+
111114
/// Whether to emit the pass manager debuggging informations.
112115
bool DebugPassManager = false;
113116

include/llvm/LTO/LTO.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "llvm/ADT/StringSet.h"
2121
#include "llvm/IR/DiagnosticInfo.h"
2222
#include "llvm/IR/ModuleSummaryIndex.h"
23+
#include "llvm/IR/RemarkStreamer.h"
2324
#include "llvm/LTO/Config.h"
2425
#include "llvm/Linker/IRMover.h"
2526
#include "llvm/Object/IRSymtab.h"
@@ -85,8 +86,8 @@ std::string getThinLTOOutputFile(const std::string &Path,
8586
/// Setup optimization remarks.
8687
Expected<std::unique_ptr<ToolOutputFile>>
8788
setupOptimizationRemarks(LLVMContext &Context, StringRef RemarksFilename,
88-
StringRef RemarksPasses, bool RemarksWithHotness,
89-
int Count = -1);
89+
StringRef RemarksPasses, StringRef RemarksFormat,
90+
bool RemarksWithHotness, int Count = -1);
9091

9192
/// Setups the output file for saving statistics.
9293
Expected<std::unique_ptr<ToolOutputFile>>

lib/IR/RemarkStreamer.cpp

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,10 +109,37 @@ void RemarkStreamer::emit(const DiagnosticInfoOptimizationBase &Diag) {
109109

110110
char RemarkSetupFileError::ID = 0;
111111
char RemarkSetupPatternError::ID = 0;
112+
char RemarkSetupFormatError::ID = 0;
113+
114+
static std::unique_ptr<remarks::Serializer>
115+
formatToSerializer(RemarksSerializerFormat RemarksFormat, raw_ostream &OS) {
116+
switch (RemarksFormat) {
117+
default:
118+
llvm_unreachable("Unknown remark serializer format.");
119+
return nullptr;
120+
case RemarksSerializerFormat::YAML:
121+
return llvm::make_unique<remarks::YAMLSerializer>(OS);
122+
};
123+
}
124+
125+
Expected<RemarksSerializerFormat>
126+
llvm::parseSerializerFormat(StringRef StrFormat) {
127+
auto Format = StringSwitch<RemarksSerializerFormat>(StrFormat)
128+
.Cases("", "yaml", RemarksSerializerFormat::YAML)
129+
.Default(RemarksSerializerFormat::Unknown);
130+
131+
if (Format == RemarksSerializerFormat::Unknown)
132+
return createStringError(std::make_error_code(std::errc::invalid_argument),
133+
"Unknown remark serializer format: '%s'",
134+
StrFormat.data());
135+
136+
return Format;
137+
}
112138

113139
Expected<std::unique_ptr<ToolOutputFile>>
114140
llvm::setupOptimizationRemarks(LLVMContext &Context, StringRef RemarksFilename,
115-
StringRef RemarksPasses, bool RemarksWithHotness,
141+
StringRef RemarksPasses, StringRef RemarksFormat,
142+
bool RemarksWithHotness,
116143
unsigned RemarksHotnessThreshold) {
117144
if (RemarksWithHotness)
118145
Context.setDiagnosticsHotnessRequested(true);
@@ -131,9 +158,13 @@ llvm::setupOptimizationRemarks(LLVMContext &Context, StringRef RemarksFilename,
131158
if (EC)
132159
return make_error<RemarkSetupFileError>(errorCodeToError(EC));
133160

161+
Expected<RemarksSerializerFormat> Format =
162+
parseSerializerFormat(RemarksFormat);
163+
if (Error E = Format.takeError())
164+
return make_error<RemarkSetupFormatError>(std::move(E));
165+
134166
Context.setRemarkStreamer(llvm::make_unique<RemarkStreamer>(
135-
RemarksFilename,
136-
llvm::make_unique<remarks::YAMLSerializer>(RemarksFile->os())));
167+
RemarksFilename, formatToSerializer(*Format, RemarksFile->os())));
137168

138169
if (!RemarksPasses.empty())
139170
if (Error E = Context.getRemarkStreamer()->setFilter(RemarksPasses))

lib/LTO/LTO.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1339,14 +1339,14 @@ Error LTO::runThinLTO(AddStreamFn AddStream, NativeObjectCache Cache,
13391339

13401340
Expected<std::unique_ptr<ToolOutputFile>>
13411341
lto::setupOptimizationRemarks(LLVMContext &Context, StringRef RemarksFilename,
1342-
StringRef RemarksPasses, bool RemarksWithHotness,
1343-
int Count) {
1342+
StringRef RemarksPasses, StringRef RemarksFormat,
1343+
bool RemarksWithHotness, int Count) {
13441344
std::string Filename = RemarksFilename;
13451345
if (!Filename.empty() && Count != -1)
13461346
Filename += ".thin." + llvm::utostr(Count) + ".yaml";
13471347

13481348
auto ResultOrErr = llvm::setupOptimizationRemarks(
1349-
Context, Filename, RemarksPasses, RemarksWithHotness);
1349+
Context, Filename, RemarksPasses, RemarksFormat, RemarksWithHotness);
13501350
if (Error E = ResultOrErr.takeError())
13511351
return std::move(E);
13521352

lib/LTO/LTOBackend.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -431,9 +431,9 @@ Error lto::backend(Config &C, AddStreamFn AddStream,
431431
std::unique_ptr<TargetMachine> TM = createTargetMachine(C, *TOrErr, *Mod);
432432

433433
// Setup optimization remarks.
434-
auto DiagFileOrErr =
435-
lto::setupOptimizationRemarks(Mod->getContext(), C.RemarksFilename,
436-
C.RemarksPasses, C.RemarksWithHotness);
434+
auto DiagFileOrErr = lto::setupOptimizationRemarks(
435+
Mod->getContext(), C.RemarksFilename, C.RemarksPasses, C.RemarksFormat,
436+
C.RemarksWithHotness);
437437
if (!DiagFileOrErr)
438438
return DiagFileOrErr.takeError();
439439
auto DiagnosticOutputFile = std::move(*DiagFileOrErr);
@@ -488,7 +488,7 @@ Error lto::thinBackend(Config &Conf, unsigned Task, AddStreamFn AddStream,
488488
// Setup optimization remarks.
489489
auto DiagFileOrErr = lto::setupOptimizationRemarks(
490490
Mod.getContext(), Conf.RemarksFilename, Conf.RemarksPasses,
491-
Conf.RemarksWithHotness, Task);
491+
Conf.RemarksFormat, Conf.RemarksWithHotness, Task);
492492
if (!DiagFileOrErr)
493493
return DiagFileOrErr.takeError();
494494
auto DiagnosticOutputFile = std::move(*DiagFileOrErr);

lib/LTO/LTOCodeGenerator.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,11 @@ cl::opt<std::string>
9797
"names match the given regular expression"),
9898
cl::value_desc("regex"));
9999

100+
cl::opt<std::string> RemarksFormat(
101+
"lto-pass-remarks-format",
102+
cl::desc("The format used for serializing remarks (default: YAML)"),
103+
cl::value_desc("format"), cl::init("yaml"));
104+
100105
cl::opt<std::string> LTOStatsFile(
101106
"lto-stats-file",
102107
cl::desc("Save statistics to the specified file"),
@@ -517,8 +522,9 @@ bool LTOCodeGenerator::optimize(bool DisableVerify, bool DisableInline,
517522
if (!this->determineTarget())
518523
return false;
519524

520-
auto DiagFileOrErr = lto::setupOptimizationRemarks(
521-
Context, RemarksFilename, RemarksPasses, RemarksWithHotness);
525+
auto DiagFileOrErr =
526+
lto::setupOptimizationRemarks(Context, RemarksFilename, RemarksPasses,
527+
RemarksFormat, RemarksWithHotness);
522528
if (!DiagFileOrErr) {
523529
errs() << "Error: " << toString(DiagFileOrErr.takeError()) << "\n";
524530
report_fatal_error("Can't get an output file for the remarks");

lib/LTO/ThinLTOCodeGenerator.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ extern cl::opt<bool> LTODiscardValueNames;
7373
extern cl::opt<std::string> RemarksFilename;
7474
extern cl::opt<std::string> RemarksPasses;
7575
extern cl::opt<bool> RemarksWithHotness;
76+
extern cl::opt<std::string> RemarksFormat;
7677
}
7778

7879
namespace {
@@ -1020,7 +1021,7 @@ void ThinLTOCodeGenerator::run() {
10201021
Context.setDiscardValueNames(LTODiscardValueNames);
10211022
Context.enableDebugTypeODRUniquing();
10221023
auto DiagFileOrErr = lto::setupOptimizationRemarks(
1023-
Context, RemarksFilename, RemarksPasses,
1024+
Context, RemarksFilename, RemarksPasses, RemarksFormat,
10241025
RemarksWithHotness, count);
10251026
if (!DiagFileOrErr) {
10261027
errs() << "Error: " << toString(DiagFileOrErr.takeError()) << "\n";

test/ThinLTO/X86/diagnostic-handler-remarks.ll

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
; RUN: llvm-lto -thinlto-action=run \
77
; RUN: -lto-pass-remarks-output=%t.yaml \
88
; RUN: -lto-pass-remarks-filter=inline \
9+
; RUN: -lto-pass-remarks-format=yaml \
910
; RUN: -exported-symbol _func2 \
1011
; RUN: -exported-symbol _main %t1.bc %t2.bc 2>&1 | \
1112
; RUN: FileCheck %s -allow-empty

test/tools/gold/X86/opt-remarks.ll

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33
; RUN: %gold -m elf_x86_64 -plugin %llvmshlibdir/LLVMgold%shlibext -shared \
44
; RUN: -plugin-opt=save-temps \
55
; RUN: -plugin-opt=opt-remarks-passes=inline \
6+
; RUN: -plugin-opt=opt-remarks-format=yaml \
67
; RUN: -plugin-opt=opt-remarks-filename=%t.yaml %t.o -o %t2.o 2>&1
78
; RUN: llvm-dis %t2.o.0.4.opt.bc -o - | FileCheck %s
89
; RUN: %gold -m elf_x86_64 -plugin %llvmshlibdir/LLVMgold%shlibext -shared \
910
; RUN: -plugin-opt=opt-remarks-passes=inline \
11+
; RUN: -plugin-opt=opt-remarks-format=yaml \
1012
; RUN: -plugin-opt=opt-remarks-with-hotness \
1113
; RUN: -plugin-opt=opt-remarks-filename=%t.hot.yaml %t.o -o %t2.o 2>&1
1214
; RUN: cat %t.yaml | FileCheck %s -check-prefix=YAML

0 commit comments

Comments
 (0)