1 //===-LTOBackend.cpp - LLVM Link Time Optimizer Backend -------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file implements the "backend" phase of LTO, i.e. it performs
10 // optimization and code generation on a loaded module. It is generally used
11 // internally by the LTO class but can also be used independently, for example
12 // to implement a standalone ThinLTO backend.
14 //===----------------------------------------------------------------------===//
16 #include "llvm/LTO/LTOBackend.h"
17 #include "llvm/Analysis/AliasAnalysis.h"
18 #include "llvm/Analysis/CGSCCPassManager.h"
19 #include "llvm/Analysis/TargetLibraryInfo.h"
20 #include "llvm/Analysis/TargetTransformInfo.h"
21 #include "llvm/Bitcode/BitcodeReader.h"
22 #include "llvm/Bitcode/BitcodeWriter.h"
23 #include "llvm/IR/LegacyPassManager.h"
24 #include "llvm/IR/PassManager.h"
25 #include "llvm/IR/Verifier.h"
26 #include "llvm/LTO/LTO.h"
27 #include "llvm/MC/SubtargetFeature.h"
28 #include "llvm/Object/ModuleSymbolTable.h"
29 #include "llvm/Passes/PassBuilder.h"
30 #include "llvm/Support/Error.h"
31 #include "llvm/Support/FileSystem.h"
32 #include "llvm/Support/MemoryBuffer.h"
33 #include "llvm/Support/Path.h"
34 #include "llvm/Support/Program.h"
35 #include "llvm/Support/raw_ostream.h"
36 #include "llvm/Support/TargetRegistry.h"
37 #include "llvm/Support/ThreadPool.h"
38 #include "llvm/Target/TargetMachine.h"
39 #include "llvm/Transforms/IPO.h"
40 #include "llvm/Transforms/IPO/PassManagerBuilder.h"
41 #include "llvm/Transforms/Scalar/LoopPassManager.h"
42 #include "llvm/Transforms/Utils/FunctionImportUtils.h"
43 #include "llvm/Transforms/Utils/SplitModule.h"
48 LLVM_ATTRIBUTE_NORETURN
static void reportOpenError(StringRef Path
, Twine Msg
) {
49 errs() << "failed to open " << Path
<< ": " << Msg
<< '\n';
54 Error
Config::addSaveTemps(std::string OutputFileName
,
55 bool UseInputModulePath
) {
56 ShouldDiscardValueNames
= false;
59 ResolutionFile
= llvm::make_unique
<raw_fd_ostream
>(
60 OutputFileName
+ "resolution.txt", EC
, sys::fs::OpenFlags::F_Text
);
62 return errorCodeToError(EC
);
64 auto setHook
= [&](std::string PathSuffix
, ModuleHookFn
&Hook
) {
65 // Keep track of the hook provided by the linker, which also needs to run.
66 ModuleHookFn LinkerHook
= Hook
;
67 Hook
= [=](unsigned Task
, const Module
&M
) {
68 // If the linker's hook returned false, we need to pass that result
70 if (LinkerHook
&& !LinkerHook(Task
, M
))
73 std::string PathPrefix
;
74 // If this is the combined module (not a ThinLTO backend compile) or the
75 // user hasn't requested using the input module's path, emit to a file
76 // named from the provided OutputFileName with the Task ID appended.
77 if (M
.getModuleIdentifier() == "ld-temp.o" || !UseInputModulePath
) {
78 PathPrefix
= OutputFileName
;
79 if (Task
!= (unsigned)-1)
80 PathPrefix
+= utostr(Task
) + ".";
82 PathPrefix
= M
.getModuleIdentifier() + ".";
83 std::string Path
= PathPrefix
+ PathSuffix
+ ".bc";
85 raw_fd_ostream
OS(Path
, EC
, sys::fs::OpenFlags::F_None
);
86 // Because -save-temps is a debugging feature, we report the error
89 reportOpenError(Path
, EC
.message());
90 WriteBitcodeToFile(M
, OS
, /*ShouldPreserveUseListOrder=*/false);
95 setHook("0.preopt", PreOptModuleHook
);
96 setHook("1.promote", PostPromoteModuleHook
);
97 setHook("2.internalize", PostInternalizeModuleHook
);
98 setHook("3.import", PostImportModuleHook
);
99 setHook("4.opt", PostOptModuleHook
);
100 setHook("5.precodegen", PreCodeGenModuleHook
);
102 CombinedIndexHook
= [=](const ModuleSummaryIndex
&Index
) {
103 std::string Path
= OutputFileName
+ "index.bc";
105 raw_fd_ostream
OS(Path
, EC
, sys::fs::OpenFlags::F_None
);
106 // Because -save-temps is a debugging feature, we report the error
107 // directly and exit.
109 reportOpenError(Path
, EC
.message());
110 WriteIndexToFile(Index
, OS
);
112 Path
= OutputFileName
+ "index.dot";
113 raw_fd_ostream
OSDot(Path
, EC
, sys::fs::OpenFlags::F_None
);
115 reportOpenError(Path
, EC
.message());
116 Index
.exportToDot(OSDot
);
120 return Error::success();
125 std::unique_ptr
<TargetMachine
>
126 createTargetMachine(Config
&Conf
, const Target
*TheTarget
, Module
&M
) {
127 StringRef TheTriple
= M
.getTargetTriple();
128 SubtargetFeatures Features
;
129 Features
.getDefaultSubtargetFeatures(Triple(TheTriple
));
130 for (const std::string
&A
: Conf
.MAttrs
)
131 Features
.AddFeature(A
);
133 Reloc::Model RelocModel
;
135 RelocModel
= *Conf
.RelocModel
;
138 M
.getPICLevel() == PICLevel::NotPIC
? Reloc::Static
: Reloc::PIC_
;
140 Optional
<CodeModel::Model
> CodeModel
;
142 CodeModel
= *Conf
.CodeModel
;
144 CodeModel
= M
.getCodeModel();
146 return std::unique_ptr
<TargetMachine
>(TheTarget
->createTargetMachine(
147 TheTriple
, Conf
.CPU
, Features
.getString(), Conf
.Options
, RelocModel
,
148 CodeModel
, Conf
.CGOptLevel
));
151 static void runNewPMPasses(Config
&Conf
, Module
&Mod
, TargetMachine
*TM
,
152 unsigned OptLevel
, bool IsThinLTO
,
153 ModuleSummaryIndex
*ExportSummary
,
154 const ModuleSummaryIndex
*ImportSummary
) {
155 Optional
<PGOOptions
> PGOOpt
;
156 if (!Conf
.SampleProfile
.empty())
157 PGOOpt
= PGOOptions("", "", Conf
.SampleProfile
, Conf
.ProfileRemapping
,
160 PassBuilder
PB(TM
, PGOOpt
);
163 // Parse a custom AA pipeline if asked to.
164 if (auto Err
= PB
.parseAAPipeline(AA
, "default"))
165 report_fatal_error("Error parsing default AA pipeline");
167 LoopAnalysisManager
LAM(Conf
.DebugPassManager
);
168 FunctionAnalysisManager
FAM(Conf
.DebugPassManager
);
169 CGSCCAnalysisManager
CGAM(Conf
.DebugPassManager
);
170 ModuleAnalysisManager
MAM(Conf
.DebugPassManager
);
172 // Register the AA manager first so that our version is the one used.
173 FAM
.registerPass([&] { return std::move(AA
); });
175 // Register all the basic analyses with the managers.
176 PB
.registerModuleAnalyses(MAM
);
177 PB
.registerCGSCCAnalyses(CGAM
);
178 PB
.registerFunctionAnalyses(FAM
);
179 PB
.registerLoopAnalyses(LAM
);
180 PB
.crossRegisterProxies(LAM
, FAM
, CGAM
, MAM
);
182 ModulePassManager
MPM(Conf
.DebugPassManager
);
183 // FIXME (davide): verify the input.
185 PassBuilder::OptimizationLevel OL
;
189 llvm_unreachable("Invalid optimization level");
191 OL
= PassBuilder::O0
;
194 OL
= PassBuilder::O1
;
197 OL
= PassBuilder::O2
;
200 OL
= PassBuilder::O3
;
205 MPM
= PB
.buildThinLTODefaultPipeline(OL
, Conf
.DebugPassManager
,
208 MPM
= PB
.buildLTODefaultPipeline(OL
, Conf
.DebugPassManager
, ExportSummary
);
211 // FIXME (davide): verify the output.
214 static void runNewPMCustomPasses(Module
&Mod
, TargetMachine
*TM
,
215 std::string PipelineDesc
,
216 std::string AAPipelineDesc
,
217 bool DisableVerify
) {
221 // Parse a custom AA pipeline if asked to.
222 if (!AAPipelineDesc
.empty())
223 if (auto Err
= PB
.parseAAPipeline(AA
, AAPipelineDesc
))
224 report_fatal_error("unable to parse AA pipeline description '" +
225 AAPipelineDesc
+ "': " + toString(std::move(Err
)));
227 LoopAnalysisManager LAM
;
228 FunctionAnalysisManager FAM
;
229 CGSCCAnalysisManager CGAM
;
230 ModuleAnalysisManager MAM
;
232 // Register the AA manager first so that our version is the one used.
233 FAM
.registerPass([&] { return std::move(AA
); });
235 // Register all the basic analyses with the managers.
236 PB
.registerModuleAnalyses(MAM
);
237 PB
.registerCGSCCAnalyses(CGAM
);
238 PB
.registerFunctionAnalyses(FAM
);
239 PB
.registerLoopAnalyses(LAM
);
240 PB
.crossRegisterProxies(LAM
, FAM
, CGAM
, MAM
);
242 ModulePassManager MPM
;
244 // Always verify the input.
245 MPM
.addPass(VerifierPass());
247 // Now, add all the passes we've been requested to.
248 if (auto Err
= PB
.parsePassPipeline(MPM
, PipelineDesc
))
249 report_fatal_error("unable to parse pass pipeline description '" +
250 PipelineDesc
+ "': " + toString(std::move(Err
)));
253 MPM
.addPass(VerifierPass());
257 static void runOldPMPasses(Config
&Conf
, Module
&Mod
, TargetMachine
*TM
,
258 bool IsThinLTO
, ModuleSummaryIndex
*ExportSummary
,
259 const ModuleSummaryIndex
*ImportSummary
) {
260 legacy::PassManager passes
;
261 passes
.add(createTargetTransformInfoWrapperPass(TM
->getTargetIRAnalysis()));
263 PassManagerBuilder PMB
;
264 PMB
.LibraryInfo
= new TargetLibraryInfoImpl(Triple(TM
->getTargetTriple()));
265 PMB
.Inliner
= createFunctionInliningPass();
266 PMB
.ExportSummary
= ExportSummary
;
267 PMB
.ImportSummary
= ImportSummary
;
268 // Unconditionally verify input since it is not verified before this
269 // point and has unknown origin.
270 PMB
.VerifyInput
= true;
271 PMB
.VerifyOutput
= !Conf
.DisableVerify
;
272 PMB
.LoopVectorize
= true;
273 PMB
.SLPVectorize
= true;
274 PMB
.OptLevel
= Conf
.OptLevel
;
275 PMB
.PGOSampleUse
= Conf
.SampleProfile
;
277 PMB
.populateThinLTOPassManager(passes
);
279 PMB
.populateLTOPassManager(passes
);
283 bool opt(Config
&Conf
, TargetMachine
*TM
, unsigned Task
, Module
&Mod
,
284 bool IsThinLTO
, ModuleSummaryIndex
*ExportSummary
,
285 const ModuleSummaryIndex
*ImportSummary
) {
286 // FIXME: Plumb the combined index into the new pass manager.
287 if (!Conf
.OptPipeline
.empty())
288 runNewPMCustomPasses(Mod
, TM
, Conf
.OptPipeline
, Conf
.AAPipeline
,
290 else if (Conf
.UseNewPM
)
291 runNewPMPasses(Conf
, Mod
, TM
, Conf
.OptLevel
, IsThinLTO
, ExportSummary
,
294 runOldPMPasses(Conf
, Mod
, TM
, IsThinLTO
, ExportSummary
, ImportSummary
);
295 return !Conf
.PostOptModuleHook
|| Conf
.PostOptModuleHook(Task
, Mod
);
298 void codegen(Config
&Conf
, TargetMachine
*TM
, AddStreamFn AddStream
,
299 unsigned Task
, Module
&Mod
) {
300 if (Conf
.PreCodeGenModuleHook
&& !Conf
.PreCodeGenModuleHook(Task
, Mod
))
303 std::unique_ptr
<ToolOutputFile
> DwoOut
;
304 SmallString
<1024> DwoFile(Conf
.DwoPath
);
305 if (!Conf
.DwoDir
.empty()) {
307 if (auto EC
= llvm::sys::fs::create_directories(Conf
.DwoDir
))
308 report_fatal_error("Failed to create directory " + Conf
.DwoDir
+ ": " +
311 DwoFile
= Conf
.DwoDir
;
312 sys::path::append(DwoFile
, std::to_string(Task
) + ".dwo");
315 if (!DwoFile
.empty()) {
317 TM
->Options
.MCOptions
.SplitDwarfFile
= DwoFile
.str().str();
318 DwoOut
= llvm::make_unique
<ToolOutputFile
>(DwoFile
, EC
, sys::fs::F_None
);
320 report_fatal_error("Failed to open " + DwoFile
+ ": " + EC
.message());
323 auto Stream
= AddStream(Task
);
324 legacy::PassManager CodeGenPasses
;
325 if (TM
->addPassesToEmitFile(CodeGenPasses
, *Stream
->OS
,
326 DwoOut
? &DwoOut
->os() : nullptr,
328 report_fatal_error("Failed to setup codegen");
329 CodeGenPasses
.run(Mod
);
335 void splitCodeGen(Config
&C
, TargetMachine
*TM
, AddStreamFn AddStream
,
336 unsigned ParallelCodeGenParallelismLevel
,
337 std::unique_ptr
<Module
> Mod
) {
338 ThreadPool
CodegenThreadPool(ParallelCodeGenParallelismLevel
);
339 unsigned ThreadCount
= 0;
340 const Target
*T
= &TM
->getTarget();
343 std::move(Mod
), ParallelCodeGenParallelismLevel
,
344 [&](std::unique_ptr
<Module
> MPart
) {
345 // We want to clone the module in a new context to multi-thread the
346 // codegen. We do it by serializing partition modules to bitcode
347 // (while still on the main thread, in order to avoid data races) and
348 // spinning up new threads which deserialize the partitions into
349 // separate contexts.
350 // FIXME: Provide a more direct way to do this in LLVM.
352 raw_svector_ostream
BCOS(BC
);
353 WriteBitcodeToFile(*MPart
, BCOS
);
356 CodegenThreadPool
.async(
357 [&](const SmallString
<0> &BC
, unsigned ThreadId
) {
358 LTOLLVMContext
Ctx(C
);
359 Expected
<std::unique_ptr
<Module
>> MOrErr
= parseBitcodeFile(
360 MemoryBufferRef(StringRef(BC
.data(), BC
.size()), "ld-temp.o"),
363 report_fatal_error("Failed to read bitcode");
364 std::unique_ptr
<Module
> MPartInCtx
= std::move(MOrErr
.get());
366 std::unique_ptr
<TargetMachine
> TM
=
367 createTargetMachine(C
, T
, *MPartInCtx
);
369 codegen(C
, TM
.get(), AddStream
, ThreadId
, *MPartInCtx
);
371 // Pass BC using std::move to ensure that it get moved rather than
372 // copied into the thread's context.
373 std::move(BC
), ThreadCount
++);
377 // Because the inner lambda (which runs in a worker thread) captures our local
378 // variables, we need to wait for the worker threads to terminate before we
379 // can leave the function scope.
380 CodegenThreadPool
.wait();
383 Expected
<const Target
*> initAndLookupTarget(Config
&C
, Module
&Mod
) {
384 if (!C
.OverrideTriple
.empty())
385 Mod
.setTargetTriple(C
.OverrideTriple
);
386 else if (Mod
.getTargetTriple().empty())
387 Mod
.setTargetTriple(C
.DefaultTriple
);
390 const Target
*T
= TargetRegistry::lookupTarget(Mod
.getTargetTriple(), Msg
);
392 return make_error
<StringError
>(Msg
, inconvertibleErrorCode());
399 finalizeOptimizationRemarks(std::unique_ptr
<ToolOutputFile
> DiagOutputFile
) {
400 // Make sure we flush the diagnostic remarks file in case the linker doesn't
401 // call the global destructors before exiting.
403 return Error::success();
404 DiagOutputFile
->keep();
405 DiagOutputFile
->os().flush();
406 return Error::success();
409 Error
lto::backend(Config
&C
, AddStreamFn AddStream
,
410 unsigned ParallelCodeGenParallelismLevel
,
411 std::unique_ptr
<Module
> Mod
,
412 ModuleSummaryIndex
&CombinedIndex
) {
413 Expected
<const Target
*> TOrErr
= initAndLookupTarget(C
, *Mod
);
415 return TOrErr
.takeError();
417 std::unique_ptr
<TargetMachine
> TM
= createTargetMachine(C
, *TOrErr
, *Mod
);
419 // Setup optimization remarks.
420 auto DiagFileOrErr
= lto::setupOptimizationRemarks(
421 Mod
->getContext(), C
.RemarksFilename
, C
.RemarksWithHotness
);
423 return DiagFileOrErr
.takeError();
424 auto DiagnosticOutputFile
= std::move(*DiagFileOrErr
);
426 if (!C
.CodeGenOnly
) {
427 if (!opt(C
, TM
.get(), 0, *Mod
, /*IsThinLTO=*/false,
428 /*ExportSummary=*/&CombinedIndex
, /*ImportSummary=*/nullptr))
429 return finalizeOptimizationRemarks(std::move(DiagnosticOutputFile
));
432 if (ParallelCodeGenParallelismLevel
== 1) {
433 codegen(C
, TM
.get(), AddStream
, 0, *Mod
);
435 splitCodeGen(C
, TM
.get(), AddStream
, ParallelCodeGenParallelismLevel
,
438 return finalizeOptimizationRemarks(std::move(DiagnosticOutputFile
));
441 static void dropDeadSymbols(Module
&Mod
, const GVSummaryMapTy
&DefinedGlobals
,
442 const ModuleSummaryIndex
&Index
) {
443 std::vector
<GlobalValue
*> DeadGVs
;
444 for (auto &GV
: Mod
.global_values())
445 if (GlobalValueSummary
*GVS
= DefinedGlobals
.lookup(GV
.getGUID()))
446 if (!Index
.isGlobalValueLive(GVS
)) {
447 DeadGVs
.push_back(&GV
);
448 convertToDeclaration(GV
);
451 // Now that all dead bodies have been dropped, delete the actual objects
452 // themselves when possible.
453 for (GlobalValue
*GV
: DeadGVs
) {
454 GV
->removeDeadConstantUsers();
455 // Might reference something defined in native object (i.e. dropped a
456 // non-prevailing IR def, but we need to keep the declaration).
458 GV
->eraseFromParent();
462 Error
lto::thinBackend(Config
&Conf
, unsigned Task
, AddStreamFn AddStream
,
463 Module
&Mod
, const ModuleSummaryIndex
&CombinedIndex
,
464 const FunctionImporter::ImportMapTy
&ImportList
,
465 const GVSummaryMapTy
&DefinedGlobals
,
466 MapVector
<StringRef
, BitcodeModule
> &ModuleMap
) {
467 Expected
<const Target
*> TOrErr
= initAndLookupTarget(Conf
, Mod
);
469 return TOrErr
.takeError();
471 std::unique_ptr
<TargetMachine
> TM
= createTargetMachine(Conf
, *TOrErr
, Mod
);
473 // Setup optimization remarks.
474 auto DiagFileOrErr
= lto::setupOptimizationRemarks(
475 Mod
.getContext(), Conf
.RemarksFilename
, Conf
.RemarksWithHotness
, Task
);
477 return DiagFileOrErr
.takeError();
478 auto DiagnosticOutputFile
= std::move(*DiagFileOrErr
);
480 if (Conf
.CodeGenOnly
) {
481 codegen(Conf
, TM
.get(), AddStream
, Task
, Mod
);
482 return finalizeOptimizationRemarks(std::move(DiagnosticOutputFile
));
485 if (Conf
.PreOptModuleHook
&& !Conf
.PreOptModuleHook(Task
, Mod
))
486 return finalizeOptimizationRemarks(std::move(DiagnosticOutputFile
));
488 renameModuleForThinLTO(Mod
, CombinedIndex
);
490 dropDeadSymbols(Mod
, DefinedGlobals
, CombinedIndex
);
492 thinLTOResolvePrevailingInModule(Mod
, DefinedGlobals
);
494 if (Conf
.PostPromoteModuleHook
&& !Conf
.PostPromoteModuleHook(Task
, Mod
))
495 return finalizeOptimizationRemarks(std::move(DiagnosticOutputFile
));
497 if (!DefinedGlobals
.empty())
498 thinLTOInternalizeModule(Mod
, DefinedGlobals
);
500 if (Conf
.PostInternalizeModuleHook
&&
501 !Conf
.PostInternalizeModuleHook(Task
, Mod
))
502 return finalizeOptimizationRemarks(std::move(DiagnosticOutputFile
));
504 auto ModuleLoader
= [&](StringRef Identifier
) {
505 assert(Mod
.getContext().isODRUniquingDebugTypes() &&
506 "ODR Type uniquing should be enabled on the context");
507 auto I
= ModuleMap
.find(Identifier
);
508 assert(I
!= ModuleMap
.end());
509 return I
->second
.getLazyModule(Mod
.getContext(),
510 /*ShouldLazyLoadMetadata=*/true,
511 /*IsImporting*/ true);
514 FunctionImporter
Importer(CombinedIndex
, ModuleLoader
);
515 if (Error Err
= Importer
.importFunctions(Mod
, ImportList
).takeError())
518 if (Conf
.PostImportModuleHook
&& !Conf
.PostImportModuleHook(Task
, Mod
))
519 return finalizeOptimizationRemarks(std::move(DiagnosticOutputFile
));
521 if (!opt(Conf
, TM
.get(), Task
, Mod
, /*IsThinLTO=*/true,
522 /*ExportSummary=*/nullptr, /*ImportSummary=*/&CombinedIndex
))
523 return finalizeOptimizationRemarks(std::move(DiagnosticOutputFile
));
525 codegen(Conf
, TM
.get(), AddStream
, Task
, Mod
);
526 return finalizeOptimizationRemarks(std::move(DiagnosticOutputFile
));