[ThinLTO] Add support for optimization remarks to thinBackend

Summary:
Support was added to the regular LTO backend, but not thinBackend.
This patch adds that support.

Reviewers: pcc, davide

Subscribers: mehdi_amini, inglorion, llvm-commits

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

llvm-svn: 331481
This commit is contained in:
Teresa Johnson 2018-05-03 20:24:12 +00:00
parent b78a577179
commit 85cc298c1a
2 changed files with 32 additions and 16 deletions

View File

@ -421,14 +421,15 @@ Expected<const Target *> initAndLookupTarget(Config &C, Module &Mod) {
} }
static void static Error
finalizeOptimizationRemarks(std::unique_ptr<ToolOutputFile> DiagOutputFile) { finalizeOptimizationRemarks(std::unique_ptr<ToolOutputFile> DiagOutputFile) {
// Make sure we flush the diagnostic remarks file in case the linker doesn't // Make sure we flush the diagnostic remarks file in case the linker doesn't
// call the global destructors before exiting. // call the global destructors before exiting.
if (!DiagOutputFile) if (!DiagOutputFile)
return; return Error::success();
DiagOutputFile->keep(); DiagOutputFile->keep();
DiagOutputFile->os().flush(); DiagOutputFile->os().flush();
return Error::success();
} }
Error lto::backend(Config &C, AddStreamFn AddStream, Error lto::backend(Config &C, AddStreamFn AddStream,
@ -450,10 +451,8 @@ Error lto::backend(Config &C, AddStreamFn AddStream,
if (!C.CodeGenOnly) { if (!C.CodeGenOnly) {
if (!opt(C, TM.get(), 0, *Mod, /*IsThinLTO=*/false, if (!opt(C, TM.get(), 0, *Mod, /*IsThinLTO=*/false,
/*ExportSummary=*/&CombinedIndex, /*ImportSummary=*/nullptr)) { /*ExportSummary=*/&CombinedIndex, /*ImportSummary=*/nullptr))
finalizeOptimizationRemarks(std::move(DiagnosticOutputFile)); return finalizeOptimizationRemarks(std::move(DiagnosticOutputFile));
return Error::success();
}
} }
if (ParallelCodeGenParallelismLevel == 1) { if (ParallelCodeGenParallelismLevel == 1) {
@ -462,8 +461,7 @@ Error lto::backend(Config &C, AddStreamFn AddStream,
splitCodeGen(C, TM.get(), AddStream, ParallelCodeGenParallelismLevel, splitCodeGen(C, TM.get(), AddStream, ParallelCodeGenParallelismLevel,
std::move(Mod)); std::move(Mod));
} }
finalizeOptimizationRemarks(std::move(DiagnosticOutputFile)); return finalizeOptimizationRemarks(std::move(DiagnosticOutputFile));
return Error::success();
} }
static void dropDeadSymbols(Module &Mod, const GVSummaryMapTy &DefinedGlobals, static void dropDeadSymbols(Module &Mod, const GVSummaryMapTy &DefinedGlobals,
@ -498,13 +496,20 @@ Error lto::thinBackend(Config &Conf, unsigned Task, AddStreamFn AddStream,
std::unique_ptr<TargetMachine> TM = createTargetMachine(Conf, *TOrErr, Mod); std::unique_ptr<TargetMachine> TM = createTargetMachine(Conf, *TOrErr, Mod);
// Setup optimization remarks.
auto DiagFileOrErr = lto::setupOptimizationRemarks(
Mod.getContext(), Conf.RemarksFilename, Conf.RemarksWithHotness, Task);
if (!DiagFileOrErr)
return DiagFileOrErr.takeError();
auto DiagnosticOutputFile = std::move(*DiagFileOrErr);
if (Conf.CodeGenOnly) { if (Conf.CodeGenOnly) {
codegen(Conf, TM.get(), AddStream, Task, Mod); codegen(Conf, TM.get(), AddStream, Task, Mod);
return Error::success(); return finalizeOptimizationRemarks(std::move(DiagnosticOutputFile));
} }
if (Conf.PreOptModuleHook && !Conf.PreOptModuleHook(Task, Mod)) if (Conf.PreOptModuleHook && !Conf.PreOptModuleHook(Task, Mod))
return Error::success(); return finalizeOptimizationRemarks(std::move(DiagnosticOutputFile));
renameModuleForThinLTO(Mod, CombinedIndex); renameModuleForThinLTO(Mod, CombinedIndex);
@ -513,14 +518,14 @@ Error lto::thinBackend(Config &Conf, unsigned Task, AddStreamFn AddStream,
thinLTOResolveWeakForLinkerModule(Mod, DefinedGlobals); thinLTOResolveWeakForLinkerModule(Mod, DefinedGlobals);
if (Conf.PostPromoteModuleHook && !Conf.PostPromoteModuleHook(Task, Mod)) if (Conf.PostPromoteModuleHook && !Conf.PostPromoteModuleHook(Task, Mod))
return Error::success(); return finalizeOptimizationRemarks(std::move(DiagnosticOutputFile));
if (!DefinedGlobals.empty()) if (!DefinedGlobals.empty())
thinLTOInternalizeModule(Mod, DefinedGlobals); thinLTOInternalizeModule(Mod, DefinedGlobals);
if (Conf.PostInternalizeModuleHook && if (Conf.PostInternalizeModuleHook &&
!Conf.PostInternalizeModuleHook(Task, Mod)) !Conf.PostInternalizeModuleHook(Task, Mod))
return Error::success(); return finalizeOptimizationRemarks(std::move(DiagnosticOutputFile));
auto ModuleLoader = [&](StringRef Identifier) { auto ModuleLoader = [&](StringRef Identifier) {
assert(Mod.getContext().isODRUniquingDebugTypes() && assert(Mod.getContext().isODRUniquingDebugTypes() &&
@ -537,12 +542,12 @@ Error lto::thinBackend(Config &Conf, unsigned Task, AddStreamFn AddStream,
return Err; return Err;
if (Conf.PostImportModuleHook && !Conf.PostImportModuleHook(Task, Mod)) if (Conf.PostImportModuleHook && !Conf.PostImportModuleHook(Task, Mod))
return Error::success(); return finalizeOptimizationRemarks(std::move(DiagnosticOutputFile));
if (!opt(Conf, TM.get(), Task, Mod, /*IsThinLTO=*/true, if (!opt(Conf, TM.get(), Task, Mod, /*IsThinLTO=*/true,
/*ExportSummary=*/nullptr, /*ImportSummary=*/&CombinedIndex)) /*ExportSummary=*/nullptr, /*ImportSummary=*/&CombinedIndex))
return Error::success(); return finalizeOptimizationRemarks(std::move(DiagnosticOutputFile));
codegen(Conf, TM.get(), AddStream, Task, Mod); codegen(Conf, TM.get(), AddStream, Task, Mod);
return Error::success(); return finalizeOptimizationRemarks(std::move(DiagnosticOutputFile));
} }

View File

@ -1,5 +1,7 @@
; RUN: llvm-as < %s >%t.bc ; Test of LTO with opt remarks YAML output.
; First try with Regular LTO
; RUN: llvm-as < %s >%t.bc
; RUN: rm -f %t.yaml ; RUN: rm -f %t.yaml
; RUN: llvm-lto2 run -pass-remarks-output=%t.yaml \ ; RUN: llvm-lto2 run -pass-remarks-output=%t.yaml \
; RUN: -r %t.bc,tinkywinky,p \ ; RUN: -r %t.bc,tinkywinky,p \
@ -7,6 +9,15 @@
; RUN: -r %t.bc,main,px -o %t.o %t.bc ; RUN: -r %t.bc,main,px -o %t.o %t.bc
; RUN: cat %t.yaml | FileCheck %s -check-prefix=YAML ; RUN: cat %t.yaml | FileCheck %s -check-prefix=YAML
; Try again with ThinLTO
; RUN: opt -module-summary %s -o %t.bc
; RUN: rm -f %t.thin.1.yaml
; RUN: llvm-lto2 run -pass-remarks-output=%t \
; RUN: -r %t.bc,tinkywinky,p \
; RUN: -r %t.bc,patatino,px \
; RUN: -r %t.bc,main,px -o %t.o %t.bc
; RUN: cat %t.thin.1.yaml | FileCheck %s -check-prefix=YAML
; YAML: --- !Passed ; YAML: --- !Passed
; YAML-NEXT: Pass: inline ; YAML-NEXT: Pass: inline
; YAML-NEXT: Name: Inlined ; YAML-NEXT: Name: Inlined