1//===-LTOBackend.cpp - LLVM Link Time Optimizer Backend -------------------===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8//
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.
13//
14//===----------------------------------------------------------------------===//
15
16#include "llvm/LTO/LTOBackend.h"
17#include "llvm/Analysis/AliasAnalysis.h"
18#include "llvm/Analysis/CGSCCPassManager.h"
19#include "llvm/Analysis/ModuleSummaryAnalysis.h"
20#include "llvm/Analysis/RuntimeLibcallInfo.h"
21#include "llvm/Analysis/TargetLibraryInfo.h"
22#include "llvm/Bitcode/BitcodeReader.h"
23#include "llvm/Bitcode/BitcodeWriter.h"
24#include "llvm/CGData/CodeGenData.h"
25#include "llvm/IR/LLVMRemarkStreamer.h"
26#include "llvm/IR/LegacyPassManager.h"
27#include "llvm/IR/PassManager.h"
28#include "llvm/IR/Verifier.h"
29#include "llvm/LTO/LTO.h"
30#include "llvm/MC/TargetRegistry.h"
31#include "llvm/Object/ModuleSymbolTable.h"
32#include "llvm/Passes/PassBuilder.h"
33#include "llvm/Passes/StandardInstrumentations.h"
34#include "llvm/Plugins/PassPlugin.h"
35#include "llvm/Support/Error.h"
36#include "llvm/Support/FileSystem.h"
37#include "llvm/Support/MemoryBuffer.h"
38#include "llvm/Support/Path.h"
39#include "llvm/Support/ThreadPool.h"
40#include "llvm/Support/ToolOutputFile.h"
41#include "llvm/Support/VirtualFileSystem.h"
42#include "llvm/Support/raw_ostream.h"
43#include "llvm/Target/TargetMachine.h"
44#include "llvm/TargetParser/SubtargetFeature.h"
45#include "llvm/Transforms/IPO/WholeProgramDevirt.h"
46#include "llvm/Transforms/Utils/FunctionImportUtils.h"
47#include "llvm/Transforms/Utils/SplitModule.h"
48#include <optional>
49
50using namespace llvm;
51using namespace lto;
52
53#define DEBUG_TYPE "lto-backend"
54
55enum class LTOBitcodeEmbedding {
56 DoNotEmbed = 0,
57 EmbedOptimized = 1,
58 EmbedPostMergePreOptimized = 2
59};
60
61static cl::opt<LTOBitcodeEmbedding> EmbedBitcode(
62 "lto-embed-bitcode", cl::init(Val: LTOBitcodeEmbedding::DoNotEmbed),
63 cl::values(clEnumValN(LTOBitcodeEmbedding::DoNotEmbed, "none",
64 "Do not embed"),
65 clEnumValN(LTOBitcodeEmbedding::EmbedOptimized, "optimized",
66 "Embed after all optimization passes"),
67 clEnumValN(LTOBitcodeEmbedding::EmbedPostMergePreOptimized,
68 "post-merge-pre-opt",
69 "Embed post merge, but before optimizations")),
70 cl::desc("Embed LLVM bitcode in object files produced by LTO"));
71
72static cl::opt<bool> ThinLTOAssumeMerged(
73 "thinlto-assume-merged", cl::init(Val: false),
74 cl::desc("Assume the input has already undergone ThinLTO function "
75 "importing and the other pre-optimization pipeline changes."));
76
77static cl::list<std::string>
78 SaveModulesList("filter-save-modules", cl::value_desc("module names"),
79 cl::desc("Only save bitcode for module whose name without "
80 "path matches this for -save-temps options"),
81 cl::CommaSeparated, cl::Hidden);
82
83namespace llvm {
84extern cl::opt<bool> NoPGOWarnMismatch;
85}
86
87[[noreturn]] static void reportOpenError(StringRef Path, Twine Msg) {
88 errs() << "failed to open " << Path << ": " << Msg << '\n';
89 errs().flush();
90 exit(status: 1);
91}
92
93Error Config::addSaveTemps(std::string OutputFileName, bool UseInputModulePath,
94 const DenseSet<StringRef> &SaveTempsArgs) {
95 ShouldDiscardValueNames = false;
96
97 std::error_code EC;
98 if (SaveTempsArgs.empty() || SaveTempsArgs.contains(V: "resolution")) {
99 ResolutionFile =
100 std::make_unique<raw_fd_ostream>(args: OutputFileName + "resolution.txt", args&: EC,
101 args: sys::fs::OpenFlags::OF_TextWithCRLF);
102 if (EC) {
103 ResolutionFile.reset();
104 return errorCodeToError(EC);
105 }
106 }
107
108 auto setHook = [&](std::string PathSuffix, ModuleHookFn &Hook) {
109 // Keep track of the hook provided by the linker, which also needs to run.
110 ModuleHookFn LinkerHook = Hook;
111 Hook = [=, SaveModNames = llvm::SmallVector<std::string, 1>(
112 SaveModulesList.begin(), SaveModulesList.end())](
113 unsigned Task, const Module &M) {
114 // If SaveModulesList is not empty, only do save-temps if the module's
115 // filename (without path) matches a name in the list.
116 if (!SaveModNames.empty() &&
117 !llvm::is_contained(
118 Range: SaveModNames,
119 Element: std::string(llvm::sys::path::filename(path: M.getName()))))
120 return false;
121
122 // If the linker's hook returned false, we need to pass that result
123 // through.
124 if (LinkerHook && !LinkerHook(Task, M))
125 return false;
126
127 std::string PathPrefix;
128 // If this is the combined module (not a ThinLTO backend compile) or the
129 // user hasn't requested using the input module's path, emit to a file
130 // named from the provided OutputFileName with the Task ID appended.
131 if (M.getModuleIdentifier() == "ld-temp.o" || !UseInputModulePath) {
132 PathPrefix = OutputFileName;
133 if (Task != (unsigned)-1)
134 PathPrefix += utostr(X: Task) + ".";
135 } else
136 PathPrefix = M.getModuleIdentifier() + ".";
137 std::string Path = PathPrefix + PathSuffix + ".bc";
138 std::error_code EC;
139 raw_fd_ostream OS(Path, EC, sys::fs::OpenFlags::OF_None);
140 // Because -save-temps is a debugging feature, we report the error
141 // directly and exit.
142 if (EC)
143 reportOpenError(Path, Msg: EC.message());
144 WriteBitcodeToFile(M, Out&: OS, /*ShouldPreserveUseListOrder=*/false);
145 return true;
146 };
147 };
148
149 auto SaveCombinedIndex =
150 [=](const ModuleSummaryIndex &Index,
151 const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols) {
152 std::string Path = OutputFileName + "index.bc";
153 std::error_code EC;
154 raw_fd_ostream OS(Path, EC, sys::fs::OpenFlags::OF_None);
155 // Because -save-temps is a debugging feature, we report the error
156 // directly and exit.
157 if (EC)
158 reportOpenError(Path, Msg: EC.message());
159 writeIndexToFile(Index, Out&: OS);
160
161 Path = OutputFileName + "index.dot";
162 raw_fd_ostream OSDot(Path, EC, sys::fs::OpenFlags::OF_Text);
163 if (EC)
164 reportOpenError(Path, Msg: EC.message());
165 Index.exportToDot(OS&: OSDot, GUIDPreservedSymbols);
166 return true;
167 };
168
169 if (SaveTempsArgs.empty()) {
170 setHook("0.preopt", PreOptModuleHook);
171 setHook("1.promote", PostPromoteModuleHook);
172 setHook("2.internalize", PostInternalizeModuleHook);
173 setHook("3.import", PostImportModuleHook);
174 setHook("4.opt", PostOptModuleHook);
175 setHook("5.precodegen", PreCodeGenModuleHook);
176 CombinedIndexHook = SaveCombinedIndex;
177 } else {
178 if (SaveTempsArgs.contains(V: "preopt"))
179 setHook("0.preopt", PreOptModuleHook);
180 if (SaveTempsArgs.contains(V: "promote"))
181 setHook("1.promote", PostPromoteModuleHook);
182 if (SaveTempsArgs.contains(V: "internalize"))
183 setHook("2.internalize", PostInternalizeModuleHook);
184 if (SaveTempsArgs.contains(V: "import"))
185 setHook("3.import", PostImportModuleHook);
186 if (SaveTempsArgs.contains(V: "opt"))
187 setHook("4.opt", PostOptModuleHook);
188 if (SaveTempsArgs.contains(V: "precodegen"))
189 setHook("5.precodegen", PreCodeGenModuleHook);
190 if (SaveTempsArgs.contains(V: "combinedindex"))
191 CombinedIndexHook = SaveCombinedIndex;
192 }
193
194 return Error::success();
195}
196
197#define HANDLE_EXTENSION(Ext) \
198 llvm::PassPluginLibraryInfo get##Ext##PluginInfo();
199#include "llvm/Support/Extension.def"
200#undef HANDLE_EXTENSION
201
202static void RegisterPassPlugins(const Config &Conf, PassBuilder &PB) {
203#define HANDLE_EXTENSION(Ext) \
204 get##Ext##PluginInfo().RegisterPassBuilderCallbacks(PB);
205#include "llvm/Support/Extension.def"
206#undef HANDLE_EXTENSION
207
208 // Load requested pass plugins and let them register pass builder callbacks
209 for (auto &PluginFN : Conf.PassPluginFilenames) {
210 auto PassPlugin = PassPlugin::Load(Filename: PluginFN);
211 if (!PassPlugin)
212 reportFatalUsageError(Err: PassPlugin.takeError());
213 PassPlugin->registerPassBuilderCallbacks(PB);
214 }
215
216 // Register already loaded plugins
217 for (auto *LoadedPlugin : Conf.LoadedPassPlugins)
218 LoadedPlugin->registerPassBuilderCallbacks(PB);
219}
220
221static std::unique_ptr<TargetMachine>
222createTargetMachine(const Config &Conf, const Target *TheTarget, Module &M) {
223 const Triple &TheTriple = M.getTargetTriple();
224 SubtargetFeatures Features;
225 Features.getDefaultSubtargetFeatures(Triple: TheTriple);
226 for (const std::string &A : Conf.MAttrs)
227 Features.AddFeature(String: A);
228
229 std::optional<Reloc::Model> RelocModel;
230 if (Conf.RelocModel)
231 RelocModel = *Conf.RelocModel;
232 else if (M.getModuleFlag(Key: "PIC Level"))
233 RelocModel =
234 M.getPICLevel() == PICLevel::NotPIC ? Reloc::Static : Reloc::PIC_;
235
236 std::optional<CodeModel::Model> CodeModel;
237 if (Conf.CodeModel)
238 CodeModel = *Conf.CodeModel;
239 else
240 CodeModel = M.getCodeModel();
241
242 TargetOptions TargetOpts = Conf.Options;
243 if (TargetOpts.MCOptions.ABIName.empty()) {
244 TargetOpts.MCOptions.ABIName = M.getTargetABIFromMD();
245 }
246
247 std::unique_ptr<TargetMachine> TM(TheTarget->createTargetMachine(
248 TT: TheTriple, CPU: Conf.CPU, Features: Features.getString(), Options: TargetOpts, RM: RelocModel,
249 CM: CodeModel, OL: Conf.CGOptLevel));
250
251 assert(TM && "Failed to create target machine");
252
253 if (std::optional<uint64_t> LargeDataThreshold = M.getLargeDataThreshold())
254 TM->setLargeDataThreshold(*LargeDataThreshold);
255
256 return TM;
257}
258
259static void runNewPMPasses(const Config &Conf, Module &Mod, TargetMachine *TM,
260 unsigned OptLevel, bool IsThinLTO,
261 ModuleSummaryIndex *ExportSummary,
262 const ModuleSummaryIndex *ImportSummary) {
263 std::optional<PGOOptions> PGOOpt;
264 if (!Conf.SampleProfile.empty())
265 PGOOpt = PGOOptions(Conf.SampleProfile, "", Conf.ProfileRemapping,
266 /*MemoryProfile=*/"", PGOOptions::SampleUse,
267 PGOOptions::NoCSAction,
268 PGOOptions::ColdFuncOpt::Default, true);
269 else if (Conf.RunCSIRInstr) {
270 PGOOpt = PGOOptions("", Conf.CSIRProfile, Conf.ProfileRemapping,
271 /*MemoryProfile=*/"", PGOOptions::IRUse,
272 PGOOptions::CSIRInstr, PGOOptions::ColdFuncOpt::Default,
273 Conf.AddFSDiscriminator);
274 } else if (!Conf.CSIRProfile.empty()) {
275 PGOOpt =
276 PGOOptions(Conf.CSIRProfile, "", Conf.ProfileRemapping,
277 /*MemoryProfile=*/"", PGOOptions::IRUse, PGOOptions::CSIRUse,
278 PGOOptions::ColdFuncOpt::Default, Conf.AddFSDiscriminator);
279 NoPGOWarnMismatch = !Conf.PGOWarnMismatch;
280 } else if (Conf.AddFSDiscriminator) {
281 PGOOpt = PGOOptions("", "", "", /*MemoryProfile=*/"", PGOOptions::NoAction,
282 PGOOptions::NoCSAction,
283 PGOOptions::ColdFuncOpt::Default, true);
284 }
285 TM->setPGOOption(PGOOpt);
286
287 LoopAnalysisManager LAM;
288 FunctionAnalysisManager FAM;
289 CGSCCAnalysisManager CGAM;
290 ModuleAnalysisManager MAM;
291
292 PassInstrumentationCallbacks PIC;
293 StandardInstrumentations SI(Mod.getContext(), Conf.DebugPassManager,
294 Conf.VerifyEach);
295 SI.registerCallbacks(PIC, MAM: &MAM);
296 PassBuilder PB(TM, Conf.PTO, PGOOpt, &PIC);
297
298 RegisterPassPlugins(Conf, PB);
299
300 std::unique_ptr<TargetLibraryInfoImpl> TLII(
301 new TargetLibraryInfoImpl(TM->getTargetTriple(), TM->Options.VecLib));
302 if (Conf.Freestanding)
303 TLII->disableAllFunctions();
304 FAM.registerPass(PassBuilder: [&] { return TargetLibraryAnalysis(*TLII); });
305
306 // Parse a custom AA pipeline if asked to.
307 if (!Conf.AAPipeline.empty()) {
308 AAManager AA;
309 if (auto Err = PB.parseAAPipeline(AA, PipelineText: Conf.AAPipeline)) {
310 report_fatal_error(reason: Twine("unable to parse AA pipeline description '") +
311 Conf.AAPipeline + "': " + toString(E: std::move(Err)));
312 }
313 // Register the AA manager first so that our version is the one used.
314 FAM.registerPass(PassBuilder: [&] { return std::move(AA); });
315 }
316
317 // Register all the basic analyses with the managers.
318 PB.registerModuleAnalyses(MAM);
319 PB.registerCGSCCAnalyses(CGAM);
320 PB.registerFunctionAnalyses(FAM);
321 PB.registerLoopAnalyses(LAM);
322 PB.crossRegisterProxies(LAM, FAM, CGAM, MAM);
323
324 ModulePassManager MPM;
325
326 if (!Conf.DisableVerify)
327 MPM.addPass(Pass: VerifierPass());
328
329 OptimizationLevel OL;
330
331 switch (OptLevel) {
332 default:
333 llvm_unreachable("Invalid optimization level");
334 case 0:
335 OL = OptimizationLevel::O0;
336 break;
337 case 1:
338 OL = OptimizationLevel::O1;
339 break;
340 case 2:
341 OL = OptimizationLevel::O2;
342 break;
343 case 3:
344 OL = OptimizationLevel::O3;
345 break;
346 }
347
348 // Parse a custom pipeline if asked to.
349 if (!Conf.OptPipeline.empty()) {
350 if (auto Err = PB.parsePassPipeline(MPM, PipelineText: Conf.OptPipeline)) {
351 report_fatal_error(reason: Twine("unable to parse pass pipeline description '") +
352 Conf.OptPipeline + "': " + toString(E: std::move(Err)));
353 }
354 } else if (IsThinLTO) {
355 MPM.addPass(Pass: PB.buildThinLTODefaultPipeline(Level: OL, ImportSummary));
356 } else {
357 MPM.addPass(Pass: PB.buildLTODefaultPipeline(Level: OL, ExportSummary));
358 }
359
360 if (!Conf.DisableVerify)
361 MPM.addPass(Pass: VerifierPass());
362
363 if (PrintPipelinePasses) {
364 std::string PipelineStr;
365 raw_string_ostream OS(PipelineStr);
366 MPM.printPipeline(OS, MapClassName2PassName: [&PIC](StringRef ClassName) {
367 auto PassName = PIC.getPassNameForClassName(ClassName);
368 return PassName.empty() ? ClassName : PassName;
369 });
370 outs() << "pipeline-passes: " << PipelineStr << '\n';
371 }
372
373 MPM.run(IR&: Mod, AM&: MAM);
374}
375
376static bool isEmptyModule(const Module &Mod) {
377 // Module is empty if it has no functions, no globals, no inline asm and no
378 // named metadata (aliases and ifuncs require functions or globals so we
379 // don't need to check those explicitly).
380 return Mod.empty() && Mod.global_empty() && Mod.named_metadata_empty() &&
381 Mod.getModuleInlineAsm().empty();
382}
383
384bool lto::opt(const Config &Conf, TargetMachine *TM, unsigned Task, Module &Mod,
385 bool IsThinLTO, ModuleSummaryIndex *ExportSummary,
386 const ModuleSummaryIndex *ImportSummary,
387 const std::vector<uint8_t> &CmdArgs) {
388 llvm::TimeTraceScope timeScope("opt");
389 if (EmbedBitcode == LTOBitcodeEmbedding::EmbedPostMergePreOptimized) {
390 // FIXME: the motivation for capturing post-merge bitcode and command line
391 // is replicating the compilation environment from bitcode, without needing
392 // to understand the dependencies (the functions to be imported). This
393 // assumes a clang - based invocation, case in which we have the command
394 // line.
395 // It's not very clear how the above motivation would map in the
396 // linker-based case, so we currently don't plumb the command line args in
397 // that case.
398 if (CmdArgs.empty())
399 LLVM_DEBUG(
400 dbgs() << "Post-(Thin)LTO merge bitcode embedding was requested, but "
401 "command line arguments are not available");
402 llvm::embedBitcodeInModule(M&: Mod, Buf: llvm::MemoryBufferRef(),
403 /*EmbedBitcode*/ true, /*EmbedCmdline*/ true,
404 /*Cmdline*/ CmdArgs);
405 }
406 // No need to run any opt passes if the module is empty.
407 // In theory these passes should take almost no time for an empty
408 // module, however, this guards against doing any unnecessary summary-based
409 // analysis in the case of a ThinLTO build where this might be an empty
410 // regular LTO combined module, with a large combined index from ThinLTO.
411 if (!isEmptyModule(Mod)) {
412 // FIXME: Plumb the combined index into the new pass manager.
413 runNewPMPasses(Conf, Mod, TM, OptLevel: Conf.OptLevel, IsThinLTO, ExportSummary,
414 ImportSummary);
415 }
416 return !Conf.PostOptModuleHook || Conf.PostOptModuleHook(Task, Mod);
417}
418
419static void codegen(const Config &Conf, TargetMachine *TM,
420 AddStreamFn AddStream, unsigned Task, Module &Mod,
421 const ModuleSummaryIndex &CombinedIndex) {
422 llvm::TimeTraceScope timeScope("codegen");
423 if (Conf.PreCodeGenModuleHook && !Conf.PreCodeGenModuleHook(Task, Mod))
424 return;
425
426 if (EmbedBitcode == LTOBitcodeEmbedding::EmbedOptimized)
427 llvm::embedBitcodeInModule(M&: Mod, Buf: llvm::MemoryBufferRef(),
428 /*EmbedBitcode*/ true,
429 /*EmbedCmdline*/ false,
430 /*CmdArgs*/ std::vector<uint8_t>());
431
432 std::unique_ptr<ToolOutputFile> DwoOut;
433 SmallString<1024> DwoFile(Conf.SplitDwarfOutput);
434 if (!Conf.DwoDir.empty()) {
435 std::error_code EC;
436 if (auto EC = llvm::sys::fs::create_directories(path: Conf.DwoDir))
437 report_fatal_error(reason: Twine("Failed to create directory ") + Conf.DwoDir +
438 ": " + EC.message());
439
440 DwoFile = Conf.DwoDir;
441 sys::path::append(path&: DwoFile, a: std::to_string(val: Task) + ".dwo");
442 TM->Options.MCOptions.SplitDwarfFile = std::string(DwoFile);
443 } else
444 TM->Options.MCOptions.SplitDwarfFile = Conf.SplitDwarfFile;
445
446 if (!DwoFile.empty()) {
447 std::error_code EC;
448 DwoOut = std::make_unique<ToolOutputFile>(args&: DwoFile, args&: EC, args: sys::fs::OF_None);
449 if (EC)
450 report_fatal_error(reason: Twine("Failed to open ") + DwoFile + ": " +
451 EC.message());
452 }
453
454 Expected<std::unique_ptr<CachedFileStream>> StreamOrErr =
455 AddStream(Task, Mod.getModuleIdentifier());
456 if (Error Err = StreamOrErr.takeError())
457 report_fatal_error(Err: std::move(Err));
458 std::unique_ptr<CachedFileStream> &Stream = *StreamOrErr;
459 TM->Options.ObjectFilenameForDebug = Stream->ObjectPathName;
460
461 // Create the codegen pipeline in its own scope so it gets deleted before
462 // Stream->commit() is called. The commit function of CacheStream deletes
463 // the raw stream, which is too early as streamers (e.g. MCAsmStreamer)
464 // keep the pointer and may use it until their destruction. See #138194.
465 {
466 legacy::PassManager CodeGenPasses;
467 TargetLibraryInfoImpl TLII(Mod.getTargetTriple(), TM->Options.VecLib);
468 CodeGenPasses.add(P: new TargetLibraryInfoWrapperPass(TLII));
469 CodeGenPasses.add(P: new RuntimeLibraryInfoWrapper(
470 Mod.getTargetTriple(), TM->Options.ExceptionModel,
471 TM->Options.FloatABIType, TM->Options.EABIVersion,
472 TM->Options.MCOptions.ABIName, TM->Options.VecLib));
473
474 // No need to make index available if the module is empty.
475 // In theory these passes should not use the index for an empty
476 // module, however, this guards against doing any unnecessary summary-based
477 // analysis in the case of a ThinLTO build where this might be an empty
478 // regular LTO combined module, with a large combined index from ThinLTO.
479 if (!isEmptyModule(Mod))
480 CodeGenPasses.add(
481 P: createImmutableModuleSummaryIndexWrapperPass(Index: &CombinedIndex));
482 if (Conf.PreCodeGenPassesHook)
483 Conf.PreCodeGenPassesHook(CodeGenPasses);
484 if (TM->addPassesToEmitFile(CodeGenPasses, *Stream->OS,
485 DwoOut ? &DwoOut->os() : nullptr,
486 Conf.CGFileType))
487 report_fatal_error(reason: "Failed to setup codegen");
488 CodeGenPasses.run(M&: Mod);
489
490 if (DwoOut)
491 DwoOut->keep();
492 }
493
494 if (Error Err = Stream->commit())
495 report_fatal_error(Err: std::move(Err));
496}
497
498static void splitCodeGen(const Config &C, TargetMachine *TM,
499 AddStreamFn AddStream,
500 unsigned ParallelCodeGenParallelismLevel, Module &Mod,
501 const ModuleSummaryIndex &CombinedIndex) {
502 DefaultThreadPool CodegenThreadPool(
503 heavyweight_hardware_concurrency(ThreadCount: ParallelCodeGenParallelismLevel));
504 unsigned ThreadCount = 0;
505 const Target *T = &TM->getTarget();
506
507 const auto HandleModulePartition =
508 [&](std::unique_ptr<Module> MPart) {
509 // We want to clone the module in a new context to multi-thread the
510 // codegen. We do it by serializing partition modules to bitcode
511 // (while still on the main thread, in order to avoid data races) and
512 // spinning up new threads which deserialize the partitions into
513 // separate contexts.
514 // FIXME: Provide a more direct way to do this in LLVM.
515 SmallString<0> BC;
516 raw_svector_ostream BCOS(BC);
517 WriteBitcodeToFile(M: *MPart, Out&: BCOS);
518
519 // Enqueue the task
520 CodegenThreadPool.async(
521 F: [&](const SmallString<0> &BC, unsigned ThreadId) {
522 LTOLLVMContext Ctx(C);
523 Expected<std::unique_ptr<Module>> MOrErr =
524 parseBitcodeFile(Buffer: MemoryBufferRef(BC.str(), "ld-temp.o"), Context&: Ctx);
525 if (!MOrErr)
526 report_fatal_error(reason: "Failed to read bitcode");
527 std::unique_ptr<Module> MPartInCtx = std::move(MOrErr.get());
528
529 std::unique_ptr<TargetMachine> TM =
530 createTargetMachine(Conf: C, TheTarget: T, M&: *MPartInCtx);
531
532 codegen(Conf: C, TM: TM.get(), AddStream, Task: ThreadId, Mod&: *MPartInCtx,
533 CombinedIndex);
534 },
535 // Pass BC using std::move to ensure that it get moved rather than
536 // copied into the thread's context.
537 ArgList: std::move(BC), ArgList: ThreadCount++);
538 };
539
540 // Try target-specific module splitting first, then fallback to the default.
541 if (!TM->splitModule(M&: Mod, NumParts: ParallelCodeGenParallelismLevel,
542 ModuleCallback: HandleModulePartition)) {
543 SplitModule(M&: Mod, N: ParallelCodeGenParallelismLevel, ModuleCallback: HandleModulePartition,
544 PreserveLocals: false);
545 }
546
547 // Because the inner lambda (which runs in a worker thread) captures our local
548 // variables, we need to wait for the worker threads to terminate before we
549 // can leave the function scope.
550 CodegenThreadPool.wait();
551}
552
553static Expected<const Target *> initAndLookupTarget(const Config &C,
554 Module &Mod) {
555 if (!C.OverrideTriple.empty())
556 Mod.setTargetTriple(Triple(C.OverrideTriple));
557 else if (Mod.getTargetTriple().empty())
558 Mod.setTargetTriple(Triple(C.DefaultTriple));
559
560 std::string Msg;
561 const Target *T = TargetRegistry::lookupTarget(TheTriple: Mod.getTargetTriple(), Error&: Msg);
562 if (!T)
563 return make_error<StringError>(Args&: Msg, Args: inconvertibleErrorCode());
564 return T;
565}
566
567Error lto::finalizeOptimizationRemarks(LLVMRemarkFileHandle DiagOutputFile) {
568 // Make sure we flush the diagnostic remarks file in case the linker doesn't
569 // call the global destructors before exiting.
570 if (!DiagOutputFile)
571 return Error::success();
572 DiagOutputFile.finalize();
573 DiagOutputFile->keep();
574 DiagOutputFile->os().flush();
575 return Error::success();
576}
577
578Error lto::backend(const Config &C, AddStreamFn AddStream,
579 unsigned ParallelCodeGenParallelismLevel, Module &Mod,
580 ModuleSummaryIndex &CombinedIndex) {
581 llvm::TimeTraceScope timeScope("LTO backend");
582 Expected<const Target *> TOrErr = initAndLookupTarget(C, Mod);
583 if (!TOrErr)
584 return TOrErr.takeError();
585
586 std::unique_ptr<TargetMachine> TM = createTargetMachine(Conf: C, TheTarget: *TOrErr, M&: Mod);
587
588 LLVM_DEBUG(dbgs() << "Running regular LTO\n");
589 if (!C.CodeGenOnly) {
590 if (!opt(Conf: C, TM: TM.get(), Task: 0, Mod, /*IsThinLTO=*/false,
591 /*ExportSummary=*/&CombinedIndex, /*ImportSummary=*/nullptr,
592 /*CmdArgs*/ std::vector<uint8_t>()))
593 return Error::success();
594 }
595
596 if (ParallelCodeGenParallelismLevel == 1) {
597 codegen(Conf: C, TM: TM.get(), AddStream, Task: 0, Mod, CombinedIndex);
598 } else {
599 splitCodeGen(C, TM: TM.get(), AddStream, ParallelCodeGenParallelismLevel, Mod,
600 CombinedIndex);
601 }
602 return Error::success();
603}
604
605static void dropDeadSymbols(Module &Mod, const GVSummaryMapTy &DefinedGlobals,
606 const ModuleSummaryIndex &Index) {
607 llvm::TimeTraceScope timeScope("Drop dead symbols");
608 std::vector<GlobalValue*> DeadGVs;
609 for (auto &GV : Mod.global_values())
610 if (GlobalValueSummary *GVS = DefinedGlobals.lookup(Val: GV.getGUID()))
611 if (!Index.isGlobalValueLive(GVS)) {
612 DeadGVs.push_back(x: &GV);
613 convertToDeclaration(GV);
614 }
615
616 // Now that all dead bodies have been dropped, delete the actual objects
617 // themselves when possible.
618 for (GlobalValue *GV : DeadGVs) {
619 GV->removeDeadConstantUsers();
620 // Might reference something defined in native object (i.e. dropped a
621 // non-prevailing IR def, but we need to keep the declaration).
622 if (GV->use_empty())
623 GV->eraseFromParent();
624 }
625}
626
627Error lto::thinBackend(const Config &Conf, unsigned Task, AddStreamFn AddStream,
628 Module &Mod, const ModuleSummaryIndex &CombinedIndex,
629 const FunctionImporter::ImportMapTy &ImportList,
630 const GVSummaryMapTy &DefinedGlobals,
631 MapVector<StringRef, BitcodeModule> *ModuleMap,
632 bool CodeGenOnly, AddStreamFn IRAddStream,
633 const std::vector<uint8_t> &CmdArgs) {
634 llvm::TimeTraceScope timeScope("Thin backend", Mod.getModuleIdentifier());
635 Expected<const Target *> TOrErr = initAndLookupTarget(C: Conf, Mod);
636 if (!TOrErr)
637 return TOrErr.takeError();
638
639 std::unique_ptr<TargetMachine> TM = createTargetMachine(Conf, TheTarget: *TOrErr, M&: Mod);
640
641 // Setup optimization remarks.
642 auto DiagFileOrErr = lto::setupLLVMOptimizationRemarks(
643 Context&: Mod.getContext(), RemarksFilename: Conf.RemarksFilename, RemarksPasses: Conf.RemarksPasses,
644 RemarksFormat: Conf.RemarksFormat, RemarksWithHotness: Conf.RemarksWithHotness, RemarksHotnessThreshold: Conf.RemarksHotnessThreshold,
645 Count: Task);
646 if (!DiagFileOrErr)
647 return DiagFileOrErr.takeError();
648 auto DiagnosticOutputFile = std::move(*DiagFileOrErr);
649
650 // Set the partial sample profile ratio in the profile summary module flag of
651 // the module, if applicable.
652 Mod.setPartialSampleProfileRatio(CombinedIndex);
653
654 LLVM_DEBUG(dbgs() << "Running ThinLTO\n");
655 if (CodeGenOnly) {
656 // If CodeGenOnly is set, we only perform code generation and skip
657 // optimization. This value may differ from Conf.CodeGenOnly.
658 codegen(Conf, TM: TM.get(), AddStream, Task, Mod, CombinedIndex);
659 return finalizeOptimizationRemarks(DiagOutputFile: std::move(DiagnosticOutputFile));
660 }
661
662 if (Conf.PreOptModuleHook && !Conf.PreOptModuleHook(Task, Mod))
663 return finalizeOptimizationRemarks(DiagOutputFile: std::move(DiagnosticOutputFile));
664
665 auto OptimizeAndCodegen =
666 [&](Module &Mod, TargetMachine *TM,
667 LLVMRemarkFileHandle DiagnosticOutputFile) {
668 // Perform optimization and code generation for ThinLTO.
669 if (!opt(Conf, TM, Task, Mod, /*IsThinLTO=*/true,
670 /*ExportSummary=*/nullptr, /*ImportSummary=*/&CombinedIndex,
671 CmdArgs))
672 return finalizeOptimizationRemarks(DiagOutputFile: std::move(DiagnosticOutputFile));
673
674 // Save the current module before the first codegen round.
675 // Note that the second codegen round runs only `codegen()` without
676 // running `opt()`. We're not reaching here as it's bailed out earlier
677 // with `CodeGenOnly` which has been set in `SecondRoundThinBackend`.
678 if (IRAddStream)
679 cgdata::saveModuleForTwoRounds(TheModule: Mod, Task, AddStream: IRAddStream);
680
681 codegen(Conf, TM, AddStream, Task, Mod, CombinedIndex);
682 return finalizeOptimizationRemarks(DiagOutputFile: std::move(DiagnosticOutputFile));
683 };
684
685 if (ThinLTOAssumeMerged)
686 return OptimizeAndCodegen(Mod, TM.get(), std::move(DiagnosticOutputFile));
687
688 // When linking an ELF shared object, dso_local should be dropped. We
689 // conservatively do this for -fpic.
690 bool ClearDSOLocalOnDeclarations =
691 TM->getTargetTriple().isOSBinFormatELF() &&
692 TM->getRelocationModel() != Reloc::Static &&
693 Mod.getPIELevel() == PIELevel::Default;
694 renameModuleForThinLTO(M&: Mod, Index: CombinedIndex, ClearDSOLocalOnDeclarations);
695
696 dropDeadSymbols(Mod, DefinedGlobals, Index: CombinedIndex);
697
698 thinLTOFinalizeInModule(TheModule&: Mod, DefinedGlobals, /*PropagateAttrs=*/true);
699
700 if (Conf.PostPromoteModuleHook && !Conf.PostPromoteModuleHook(Task, Mod))
701 return finalizeOptimizationRemarks(DiagOutputFile: std::move(DiagnosticOutputFile));
702
703 if (!DefinedGlobals.empty())
704 thinLTOInternalizeModule(TheModule&: Mod, DefinedGlobals);
705
706 if (Conf.PostInternalizeModuleHook &&
707 !Conf.PostInternalizeModuleHook(Task, Mod))
708 return finalizeOptimizationRemarks(DiagOutputFile: std::move(DiagnosticOutputFile));
709
710 auto ModuleLoader = [&](StringRef Identifier) {
711 llvm::TimeTraceScope moduleLoaderScope("Module loader", Identifier);
712 assert(Mod.getContext().isODRUniquingDebugTypes() &&
713 "ODR Type uniquing should be enabled on the context");
714 if (ModuleMap) {
715 auto I = ModuleMap->find(Key: Identifier);
716 assert(I != ModuleMap->end());
717 return I->second.getLazyModule(Context&: Mod.getContext(),
718 /*ShouldLazyLoadMetadata=*/true,
719 /*IsImporting*/ true);
720 }
721
722 ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> MBOrErr =
723 llvm::MemoryBuffer::getFile(Filename: Identifier);
724 if (!MBOrErr)
725 return Expected<std::unique_ptr<llvm::Module>>(make_error<StringError>(
726 Args: Twine("Error loading imported file ") + Identifier + " : ",
727 Args: MBOrErr.getError()));
728
729 Expected<BitcodeModule> BMOrErr = findThinLTOModule(MBRef: **MBOrErr);
730 if (!BMOrErr)
731 return Expected<std::unique_ptr<llvm::Module>>(make_error<StringError>(
732 Args: Twine("Error loading imported file ") + Identifier + " : " +
733 toString(E: BMOrErr.takeError()),
734 Args: inconvertibleErrorCode()));
735
736 Expected<std::unique_ptr<Module>> MOrErr =
737 BMOrErr->getLazyModule(Context&: Mod.getContext(),
738 /*ShouldLazyLoadMetadata=*/true,
739 /*IsImporting*/ true);
740 if (MOrErr)
741 (*MOrErr)->setOwnedMemoryBuffer(std::move(*MBOrErr));
742 return MOrErr;
743 };
744
745 {
746 llvm::TimeTraceScope importScope("Import functions");
747 FunctionImporter Importer(CombinedIndex, ModuleLoader,
748 ClearDSOLocalOnDeclarations);
749 if (Error Err = Importer.importFunctions(M&: Mod, ImportList).takeError())
750 return Err;
751 }
752
753 // Do this after any importing so that imported code is updated.
754 updatePublicTypeTestCalls(M&: Mod, WholeProgramVisibilityEnabledInLTO: CombinedIndex.withWholeProgramVisibility());
755
756 if (Conf.PostImportModuleHook && !Conf.PostImportModuleHook(Task, Mod))
757 return finalizeOptimizationRemarks(DiagOutputFile: std::move(DiagnosticOutputFile));
758
759 return OptimizeAndCodegen(Mod, TM.get(), std::move(DiagnosticOutputFile));
760}
761
762BitcodeModule *lto::findThinLTOModule(MutableArrayRef<BitcodeModule> BMs) {
763 if (ThinLTOAssumeMerged && BMs.size() == 1)
764 return BMs.begin();
765
766 for (BitcodeModule &BM : BMs) {
767 Expected<BitcodeLTOInfo> LTOInfo = BM.getLTOInfo();
768 if (LTOInfo && LTOInfo->IsThinLTO)
769 return &BM;
770 }
771 return nullptr;
772}
773
774Expected<BitcodeModule> lto::findThinLTOModule(MemoryBufferRef MBRef) {
775 Expected<std::vector<BitcodeModule>> BMsOrErr = getBitcodeModuleList(Buffer: MBRef);
776 if (!BMsOrErr)
777 return BMsOrErr.takeError();
778
779 // The bitcode file may contain multiple modules, we want the one that is
780 // marked as being the ThinLTO module.
781 if (const BitcodeModule *Bm = lto::findThinLTOModule(BMs: *BMsOrErr))
782 return *Bm;
783
784 return make_error<StringError>(Args: "Could not find module summary",
785 Args: inconvertibleErrorCode());
786}
787
788bool lto::initImportList(const Module &M,
789 const ModuleSummaryIndex &CombinedIndex,
790 FunctionImporter::ImportMapTy &ImportList) {
791 if (ThinLTOAssumeMerged)
792 return true;
793 // We can simply import the values mentioned in the combined index, since
794 // we should only invoke this using the individual indexes written out
795 // via a WriteIndexesThinBackend.
796 for (const auto &GlobalList : CombinedIndex) {
797 // Ignore entries for undefined references.
798 if (GlobalList.second.getSummaryList().empty())
799 continue;
800
801 auto GUID = GlobalList.first;
802 for (const auto &Summary : GlobalList.second.getSummaryList()) {
803 // Skip the summaries for the importing module. These are included to
804 // e.g. record required linkage changes.
805 if (Summary->modulePath() == M.getModuleIdentifier())
806 continue;
807 // Add an entry to provoke importing by thinBackend.
808 ImportList.addGUID(FromModule: Summary->modulePath(), GUID, ImportKind: Summary->importType());
809 }
810 }
811 return true;
812}
813