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(ArrayRef<std::string> PassPlugins,
203 PassBuilder &PB) {
204#define HANDLE_EXTENSION(Ext) \
205 get##Ext##PluginInfo().RegisterPassBuilderCallbacks(PB);
206#include "llvm/Support/Extension.def"
207#undef HANDLE_EXTENSION
208
209 // Load requested pass plugins and let them register pass builder callbacks
210 for (auto &PluginFN : PassPlugins) {
211 auto PassPlugin = PassPlugin::Load(Filename: PluginFN);
212 if (!PassPlugin)
213 reportFatalUsageError(Err: PassPlugin.takeError());
214 PassPlugin->registerPassBuilderCallbacks(PB);
215 }
216}
217
218static std::unique_ptr<TargetMachine>
219createTargetMachine(const Config &Conf, const Target *TheTarget, Module &M) {
220 const Triple &TheTriple = M.getTargetTriple();
221 SubtargetFeatures Features;
222 Features.getDefaultSubtargetFeatures(Triple: TheTriple);
223 for (const std::string &A : Conf.MAttrs)
224 Features.AddFeature(String: A);
225
226 std::optional<Reloc::Model> RelocModel;
227 if (Conf.RelocModel)
228 RelocModel = *Conf.RelocModel;
229 else if (M.getModuleFlag(Key: "PIC Level"))
230 RelocModel =
231 M.getPICLevel() == PICLevel::NotPIC ? Reloc::Static : Reloc::PIC_;
232
233 std::optional<CodeModel::Model> CodeModel;
234 if (Conf.CodeModel)
235 CodeModel = *Conf.CodeModel;
236 else
237 CodeModel = M.getCodeModel();
238
239 TargetOptions TargetOpts = Conf.Options;
240 if (TargetOpts.MCOptions.ABIName.empty()) {
241 TargetOpts.MCOptions.ABIName = M.getTargetABIFromMD();
242 }
243
244 std::unique_ptr<TargetMachine> TM(TheTarget->createTargetMachine(
245 TT: TheTriple, CPU: Conf.CPU, Features: Features.getString(), Options: TargetOpts, RM: RelocModel,
246 CM: CodeModel, OL: Conf.CGOptLevel));
247
248 assert(TM && "Failed to create target machine");
249
250 if (std::optional<uint64_t> LargeDataThreshold = M.getLargeDataThreshold())
251 TM->setLargeDataThreshold(*LargeDataThreshold);
252
253 return TM;
254}
255
256static void runNewPMPasses(const Config &Conf, Module &Mod, TargetMachine *TM,
257 unsigned OptLevel, bool IsThinLTO,
258 ModuleSummaryIndex *ExportSummary,
259 const ModuleSummaryIndex *ImportSummary) {
260 std::optional<PGOOptions> PGOOpt;
261 if (!Conf.SampleProfile.empty())
262 PGOOpt = PGOOptions(Conf.SampleProfile, "", Conf.ProfileRemapping,
263 /*MemoryProfile=*/"", PGOOptions::SampleUse,
264 PGOOptions::NoCSAction,
265 PGOOptions::ColdFuncOpt::Default, true);
266 else if (Conf.RunCSIRInstr) {
267 PGOOpt = PGOOptions("", Conf.CSIRProfile, Conf.ProfileRemapping,
268 /*MemoryProfile=*/"", PGOOptions::IRUse,
269 PGOOptions::CSIRInstr, PGOOptions::ColdFuncOpt::Default,
270 Conf.AddFSDiscriminator);
271 } else if (!Conf.CSIRProfile.empty()) {
272 PGOOpt =
273 PGOOptions(Conf.CSIRProfile, "", Conf.ProfileRemapping,
274 /*MemoryProfile=*/"", PGOOptions::IRUse, PGOOptions::CSIRUse,
275 PGOOptions::ColdFuncOpt::Default, Conf.AddFSDiscriminator);
276 NoPGOWarnMismatch = !Conf.PGOWarnMismatch;
277 } else if (Conf.AddFSDiscriminator) {
278 PGOOpt = PGOOptions("", "", "", /*MemoryProfile=*/"", PGOOptions::NoAction,
279 PGOOptions::NoCSAction,
280 PGOOptions::ColdFuncOpt::Default, true);
281 }
282 TM->setPGOOption(PGOOpt);
283
284 LoopAnalysisManager LAM;
285 FunctionAnalysisManager FAM;
286 CGSCCAnalysisManager CGAM;
287 ModuleAnalysisManager MAM;
288
289 PassInstrumentationCallbacks PIC;
290 StandardInstrumentations SI(Mod.getContext(), Conf.DebugPassManager,
291 Conf.VerifyEach);
292 SI.registerCallbacks(PIC, MAM: &MAM);
293 PassBuilder PB(TM, Conf.PTO, PGOOpt, &PIC);
294
295 RegisterPassPlugins(PassPlugins: Conf.PassPlugins, PB);
296
297 std::unique_ptr<TargetLibraryInfoImpl> TLII(
298 new TargetLibraryInfoImpl(TM->getTargetTriple(), TM->Options.VecLib));
299 if (Conf.Freestanding)
300 TLII->disableAllFunctions();
301 FAM.registerPass(PassBuilder: [&] { return TargetLibraryAnalysis(*TLII); });
302
303 // Parse a custom AA pipeline if asked to.
304 if (!Conf.AAPipeline.empty()) {
305 AAManager AA;
306 if (auto Err = PB.parseAAPipeline(AA, PipelineText: Conf.AAPipeline)) {
307 report_fatal_error(reason: Twine("unable to parse AA pipeline description '") +
308 Conf.AAPipeline + "': " + toString(E: std::move(Err)));
309 }
310 // Register the AA manager first so that our version is the one used.
311 FAM.registerPass(PassBuilder: [&] { return std::move(AA); });
312 }
313
314 // Register all the basic analyses with the managers.
315 PB.registerModuleAnalyses(MAM);
316 PB.registerCGSCCAnalyses(CGAM);
317 PB.registerFunctionAnalyses(FAM);
318 PB.registerLoopAnalyses(LAM);
319 PB.crossRegisterProxies(LAM, FAM, CGAM, MAM);
320
321 ModulePassManager MPM;
322
323 if (!Conf.DisableVerify)
324 MPM.addPass(Pass: VerifierPass());
325
326 OptimizationLevel OL;
327
328 switch (OptLevel) {
329 default:
330 llvm_unreachable("Invalid optimization level");
331 case 0:
332 OL = OptimizationLevel::O0;
333 break;
334 case 1:
335 OL = OptimizationLevel::O1;
336 break;
337 case 2:
338 OL = OptimizationLevel::O2;
339 break;
340 case 3:
341 OL = OptimizationLevel::O3;
342 break;
343 }
344
345 // Parse a custom pipeline if asked to.
346 if (!Conf.OptPipeline.empty()) {
347 if (auto Err = PB.parsePassPipeline(MPM, PipelineText: Conf.OptPipeline)) {
348 report_fatal_error(reason: Twine("unable to parse pass pipeline description '") +
349 Conf.OptPipeline + "': " + toString(E: std::move(Err)));
350 }
351 } else if (IsThinLTO) {
352 MPM.addPass(Pass: PB.buildThinLTODefaultPipeline(Level: OL, ImportSummary));
353 } else {
354 MPM.addPass(Pass: PB.buildLTODefaultPipeline(Level: OL, ExportSummary));
355 }
356
357 if (!Conf.DisableVerify)
358 MPM.addPass(Pass: VerifierPass());
359
360 if (PrintPipelinePasses) {
361 std::string PipelineStr;
362 raw_string_ostream OS(PipelineStr);
363 MPM.printPipeline(OS, MapClassName2PassName: [&PIC](StringRef ClassName) {
364 auto PassName = PIC.getPassNameForClassName(ClassName);
365 return PassName.empty() ? ClassName : PassName;
366 });
367 outs() << "pipeline-passes: " << PipelineStr << '\n';
368 }
369
370 MPM.run(IR&: Mod, AM&: MAM);
371}
372
373static bool isEmptyModule(const Module &Mod) {
374 // Module is empty if it has no functions, no globals, no inline asm and no
375 // named metadata (aliases and ifuncs require functions or globals so we
376 // don't need to check those explicitly).
377 return Mod.empty() && Mod.global_empty() && Mod.named_metadata_empty() &&
378 Mod.getModuleInlineAsm().empty();
379}
380
381bool lto::opt(const Config &Conf, TargetMachine *TM, unsigned Task, Module &Mod,
382 bool IsThinLTO, ModuleSummaryIndex *ExportSummary,
383 const ModuleSummaryIndex *ImportSummary,
384 const std::vector<uint8_t> &CmdArgs) {
385 llvm::TimeTraceScope timeScope("opt");
386 if (EmbedBitcode == LTOBitcodeEmbedding::EmbedPostMergePreOptimized) {
387 // FIXME: the motivation for capturing post-merge bitcode and command line
388 // is replicating the compilation environment from bitcode, without needing
389 // to understand the dependencies (the functions to be imported). This
390 // assumes a clang - based invocation, case in which we have the command
391 // line.
392 // It's not very clear how the above motivation would map in the
393 // linker-based case, so we currently don't plumb the command line args in
394 // that case.
395 if (CmdArgs.empty())
396 LLVM_DEBUG(
397 dbgs() << "Post-(Thin)LTO merge bitcode embedding was requested, but "
398 "command line arguments are not available");
399 llvm::embedBitcodeInModule(M&: Mod, Buf: llvm::MemoryBufferRef(),
400 /*EmbedBitcode*/ true, /*EmbedCmdline*/ true,
401 /*Cmdline*/ CmdArgs);
402 }
403 // No need to run any opt passes if the module is empty.
404 // In theory these passes should take almost no time for an empty
405 // module, however, this guards against doing any unnecessary summary-based
406 // analysis in the case of a ThinLTO build where this might be an empty
407 // regular LTO combined module, with a large combined index from ThinLTO.
408 if (!isEmptyModule(Mod)) {
409 // FIXME: Plumb the combined index into the new pass manager.
410 runNewPMPasses(Conf, Mod, TM, OptLevel: Conf.OptLevel, IsThinLTO, ExportSummary,
411 ImportSummary);
412 }
413 return !Conf.PostOptModuleHook || Conf.PostOptModuleHook(Task, Mod);
414}
415
416static void codegen(const Config &Conf, TargetMachine *TM,
417 AddStreamFn AddStream, unsigned Task, Module &Mod,
418 const ModuleSummaryIndex &CombinedIndex) {
419 llvm::TimeTraceScope timeScope("codegen");
420 if (Conf.PreCodeGenModuleHook && !Conf.PreCodeGenModuleHook(Task, Mod))
421 return;
422
423 if (EmbedBitcode == LTOBitcodeEmbedding::EmbedOptimized)
424 llvm::embedBitcodeInModule(M&: Mod, Buf: llvm::MemoryBufferRef(),
425 /*EmbedBitcode*/ true,
426 /*EmbedCmdline*/ false,
427 /*CmdArgs*/ std::vector<uint8_t>());
428
429 std::unique_ptr<ToolOutputFile> DwoOut;
430 SmallString<1024> DwoFile(Conf.SplitDwarfOutput);
431 if (!Conf.DwoDir.empty()) {
432 std::error_code EC;
433 if (auto EC = llvm::sys::fs::create_directories(path: Conf.DwoDir))
434 report_fatal_error(reason: Twine("Failed to create directory ") + Conf.DwoDir +
435 ": " + EC.message());
436
437 DwoFile = Conf.DwoDir;
438 sys::path::append(path&: DwoFile, a: std::to_string(val: Task) + ".dwo");
439 TM->Options.MCOptions.SplitDwarfFile = std::string(DwoFile);
440 } else
441 TM->Options.MCOptions.SplitDwarfFile = Conf.SplitDwarfFile;
442
443 if (!DwoFile.empty()) {
444 std::error_code EC;
445 DwoOut = std::make_unique<ToolOutputFile>(args&: DwoFile, args&: EC, args: sys::fs::OF_None);
446 if (EC)
447 report_fatal_error(reason: Twine("Failed to open ") + DwoFile + ": " +
448 EC.message());
449 }
450
451 Expected<std::unique_ptr<CachedFileStream>> StreamOrErr =
452 AddStream(Task, Mod.getModuleIdentifier());
453 if (Error Err = StreamOrErr.takeError())
454 report_fatal_error(Err: std::move(Err));
455 std::unique_ptr<CachedFileStream> &Stream = *StreamOrErr;
456 TM->Options.ObjectFilenameForDebug = Stream->ObjectPathName;
457
458 // Create the codegen pipeline in its own scope so it gets deleted before
459 // Stream->commit() is called. The commit function of CacheStream deletes
460 // the raw stream, which is too early as streamers (e.g. MCAsmStreamer)
461 // keep the pointer and may use it until their destruction. See #138194.
462 {
463 legacy::PassManager CodeGenPasses;
464 TargetLibraryInfoImpl TLII(Mod.getTargetTriple(), TM->Options.VecLib);
465 CodeGenPasses.add(P: new TargetLibraryInfoWrapperPass(TLII));
466 CodeGenPasses.add(P: new RuntimeLibraryInfoWrapper(
467 Mod.getTargetTriple(), TM->Options.ExceptionModel,
468 TM->Options.FloatABIType, TM->Options.EABIVersion,
469 TM->Options.MCOptions.ABIName, TM->Options.VecLib));
470
471 // No need to make index available if the module is empty.
472 // In theory these passes should not use the index for an empty
473 // module, however, this guards against doing any unnecessary summary-based
474 // analysis in the case of a ThinLTO build where this might be an empty
475 // regular LTO combined module, with a large combined index from ThinLTO.
476 if (!isEmptyModule(Mod))
477 CodeGenPasses.add(
478 P: createImmutableModuleSummaryIndexWrapperPass(Index: &CombinedIndex));
479 if (Conf.PreCodeGenPassesHook)
480 Conf.PreCodeGenPassesHook(CodeGenPasses);
481 if (TM->addPassesToEmitFile(CodeGenPasses, *Stream->OS,
482 DwoOut ? &DwoOut->os() : nullptr,
483 Conf.CGFileType))
484 report_fatal_error(reason: "Failed to setup codegen");
485 CodeGenPasses.run(M&: Mod);
486
487 if (DwoOut)
488 DwoOut->keep();
489 }
490
491 if (Error Err = Stream->commit())
492 report_fatal_error(Err: std::move(Err));
493}
494
495static void splitCodeGen(const Config &C, TargetMachine *TM,
496 AddStreamFn AddStream,
497 unsigned ParallelCodeGenParallelismLevel, Module &Mod,
498 const ModuleSummaryIndex &CombinedIndex) {
499 DefaultThreadPool CodegenThreadPool(
500 heavyweight_hardware_concurrency(ThreadCount: ParallelCodeGenParallelismLevel));
501 unsigned ThreadCount = 0;
502 const Target *T = &TM->getTarget();
503
504 const auto HandleModulePartition =
505 [&](std::unique_ptr<Module> MPart) {
506 // We want to clone the module in a new context to multi-thread the
507 // codegen. We do it by serializing partition modules to bitcode
508 // (while still on the main thread, in order to avoid data races) and
509 // spinning up new threads which deserialize the partitions into
510 // separate contexts.
511 // FIXME: Provide a more direct way to do this in LLVM.
512 SmallString<0> BC;
513 raw_svector_ostream BCOS(BC);
514 WriteBitcodeToFile(M: *MPart, Out&: BCOS);
515
516 // Enqueue the task
517 CodegenThreadPool.async(
518 F: [&](const SmallString<0> &BC, unsigned ThreadId) {
519 LTOLLVMContext Ctx(C);
520 Expected<std::unique_ptr<Module>> MOrErr =
521 parseBitcodeFile(Buffer: MemoryBufferRef(BC.str(), "ld-temp.o"), Context&: Ctx);
522 if (!MOrErr)
523 report_fatal_error(reason: "Failed to read bitcode");
524 std::unique_ptr<Module> MPartInCtx = std::move(MOrErr.get());
525
526 std::unique_ptr<TargetMachine> TM =
527 createTargetMachine(Conf: C, TheTarget: T, M&: *MPartInCtx);
528
529 codegen(Conf: C, TM: TM.get(), AddStream, Task: ThreadId, Mod&: *MPartInCtx,
530 CombinedIndex);
531 },
532 // Pass BC using std::move to ensure that it get moved rather than
533 // copied into the thread's context.
534 ArgList: std::move(BC), ArgList: ThreadCount++);
535 };
536
537 // Try target-specific module splitting first, then fallback to the default.
538 if (!TM->splitModule(M&: Mod, NumParts: ParallelCodeGenParallelismLevel,
539 ModuleCallback: HandleModulePartition)) {
540 SplitModule(M&: Mod, N: ParallelCodeGenParallelismLevel, ModuleCallback: HandleModulePartition,
541 PreserveLocals: false);
542 }
543
544 // Because the inner lambda (which runs in a worker thread) captures our local
545 // variables, we need to wait for the worker threads to terminate before we
546 // can leave the function scope.
547 CodegenThreadPool.wait();
548}
549
550static Expected<const Target *> initAndLookupTarget(const Config &C,
551 Module &Mod) {
552 if (!C.OverrideTriple.empty())
553 Mod.setTargetTriple(Triple(C.OverrideTriple));
554 else if (Mod.getTargetTriple().empty())
555 Mod.setTargetTriple(Triple(C.DefaultTriple));
556
557 std::string Msg;
558 const Target *T = TargetRegistry::lookupTarget(TheTriple: Mod.getTargetTriple(), Error&: Msg);
559 if (!T)
560 return make_error<StringError>(Args&: Msg, Args: inconvertibleErrorCode());
561 return T;
562}
563
564Error lto::finalizeOptimizationRemarks(LLVMRemarkFileHandle DiagOutputFile) {
565 // Make sure we flush the diagnostic remarks file in case the linker doesn't
566 // call the global destructors before exiting.
567 if (!DiagOutputFile)
568 return Error::success();
569 DiagOutputFile.finalize();
570 DiagOutputFile->keep();
571 DiagOutputFile->os().flush();
572 return Error::success();
573}
574
575Error lto::backend(const Config &C, AddStreamFn AddStream,
576 unsigned ParallelCodeGenParallelismLevel, Module &Mod,
577 ModuleSummaryIndex &CombinedIndex) {
578 llvm::TimeTraceScope timeScope("LTO backend");
579 Expected<const Target *> TOrErr = initAndLookupTarget(C, Mod);
580 if (!TOrErr)
581 return TOrErr.takeError();
582
583 std::unique_ptr<TargetMachine> TM = createTargetMachine(Conf: C, TheTarget: *TOrErr, M&: Mod);
584
585 LLVM_DEBUG(dbgs() << "Running regular LTO\n");
586 if (!C.CodeGenOnly) {
587 if (!opt(Conf: C, TM: TM.get(), Task: 0, Mod, /*IsThinLTO=*/false,
588 /*ExportSummary=*/&CombinedIndex, /*ImportSummary=*/nullptr,
589 /*CmdArgs*/ std::vector<uint8_t>()))
590 return Error::success();
591 }
592
593 if (ParallelCodeGenParallelismLevel == 1) {
594 codegen(Conf: C, TM: TM.get(), AddStream, Task: 0, Mod, CombinedIndex);
595 } else {
596 splitCodeGen(C, TM: TM.get(), AddStream, ParallelCodeGenParallelismLevel, Mod,
597 CombinedIndex);
598 }
599 return Error::success();
600}
601
602static void dropDeadSymbols(Module &Mod, const GVSummaryMapTy &DefinedGlobals,
603 const ModuleSummaryIndex &Index) {
604 llvm::TimeTraceScope timeScope("Drop dead symbols");
605 std::vector<GlobalValue*> DeadGVs;
606 for (auto &GV : Mod.global_values())
607 if (GlobalValueSummary *GVS = DefinedGlobals.lookup(Val: GV.getGUID()))
608 if (!Index.isGlobalValueLive(GVS)) {
609 DeadGVs.push_back(x: &GV);
610 convertToDeclaration(GV);
611 }
612
613 // Now that all dead bodies have been dropped, delete the actual objects
614 // themselves when possible.
615 for (GlobalValue *GV : DeadGVs) {
616 GV->removeDeadConstantUsers();
617 // Might reference something defined in native object (i.e. dropped a
618 // non-prevailing IR def, but we need to keep the declaration).
619 if (GV->use_empty())
620 GV->eraseFromParent();
621 }
622}
623
624Error lto::thinBackend(const Config &Conf, unsigned Task, AddStreamFn AddStream,
625 Module &Mod, const ModuleSummaryIndex &CombinedIndex,
626 const FunctionImporter::ImportMapTy &ImportList,
627 const GVSummaryMapTy &DefinedGlobals,
628 MapVector<StringRef, BitcodeModule> *ModuleMap,
629 bool CodeGenOnly, AddStreamFn IRAddStream,
630 const std::vector<uint8_t> &CmdArgs) {
631 llvm::TimeTraceScope timeScope("Thin backend", Mod.getModuleIdentifier());
632 Expected<const Target *> TOrErr = initAndLookupTarget(C: Conf, Mod);
633 if (!TOrErr)
634 return TOrErr.takeError();
635
636 std::unique_ptr<TargetMachine> TM = createTargetMachine(Conf, TheTarget: *TOrErr, M&: Mod);
637
638 // Setup optimization remarks.
639 auto DiagFileOrErr = lto::setupLLVMOptimizationRemarks(
640 Context&: Mod.getContext(), RemarksFilename: Conf.RemarksFilename, RemarksPasses: Conf.RemarksPasses,
641 RemarksFormat: Conf.RemarksFormat, RemarksWithHotness: Conf.RemarksWithHotness, RemarksHotnessThreshold: Conf.RemarksHotnessThreshold,
642 Count: Task);
643 if (!DiagFileOrErr)
644 return DiagFileOrErr.takeError();
645 auto DiagnosticOutputFile = std::move(*DiagFileOrErr);
646
647 // Set the partial sample profile ratio in the profile summary module flag of
648 // the module, if applicable.
649 Mod.setPartialSampleProfileRatio(CombinedIndex);
650
651 LLVM_DEBUG(dbgs() << "Running ThinLTO\n");
652 if (CodeGenOnly) {
653 // If CodeGenOnly is set, we only perform code generation and skip
654 // optimization. This value may differ from Conf.CodeGenOnly.
655 codegen(Conf, TM: TM.get(), AddStream, Task, Mod, CombinedIndex);
656 return finalizeOptimizationRemarks(DiagOutputFile: std::move(DiagnosticOutputFile));
657 }
658
659 if (Conf.PreOptModuleHook && !Conf.PreOptModuleHook(Task, Mod))
660 return finalizeOptimizationRemarks(DiagOutputFile: std::move(DiagnosticOutputFile));
661
662 auto OptimizeAndCodegen =
663 [&](Module &Mod, TargetMachine *TM,
664 LLVMRemarkFileHandle DiagnosticOutputFile) {
665 // Perform optimization and code generation for ThinLTO.
666 if (!opt(Conf, TM, Task, Mod, /*IsThinLTO=*/true,
667 /*ExportSummary=*/nullptr, /*ImportSummary=*/&CombinedIndex,
668 CmdArgs))
669 return finalizeOptimizationRemarks(DiagOutputFile: std::move(DiagnosticOutputFile));
670
671 // Save the current module before the first codegen round.
672 // Note that the second codegen round runs only `codegen()` without
673 // running `opt()`. We're not reaching here as it's bailed out earlier
674 // with `CodeGenOnly` which has been set in `SecondRoundThinBackend`.
675 if (IRAddStream)
676 cgdata::saveModuleForTwoRounds(TheModule: Mod, Task, AddStream: IRAddStream);
677
678 codegen(Conf, TM, AddStream, Task, Mod, CombinedIndex);
679 return finalizeOptimizationRemarks(DiagOutputFile: std::move(DiagnosticOutputFile));
680 };
681
682 if (ThinLTOAssumeMerged)
683 return OptimizeAndCodegen(Mod, TM.get(), std::move(DiagnosticOutputFile));
684
685 // When linking an ELF shared object, dso_local should be dropped. We
686 // conservatively do this for -fpic.
687 bool ClearDSOLocalOnDeclarations =
688 TM->getTargetTriple().isOSBinFormatELF() &&
689 TM->getRelocationModel() != Reloc::Static &&
690 Mod.getPIELevel() == PIELevel::Default;
691 renameModuleForThinLTO(M&: Mod, Index: CombinedIndex, ClearDSOLocalOnDeclarations);
692
693 dropDeadSymbols(Mod, DefinedGlobals, Index: CombinedIndex);
694
695 thinLTOFinalizeInModule(TheModule&: Mod, DefinedGlobals, /*PropagateAttrs=*/true);
696
697 if (Conf.PostPromoteModuleHook && !Conf.PostPromoteModuleHook(Task, Mod))
698 return finalizeOptimizationRemarks(DiagOutputFile: std::move(DiagnosticOutputFile));
699
700 if (!DefinedGlobals.empty())
701 thinLTOInternalizeModule(TheModule&: Mod, DefinedGlobals);
702
703 if (Conf.PostInternalizeModuleHook &&
704 !Conf.PostInternalizeModuleHook(Task, Mod))
705 return finalizeOptimizationRemarks(DiagOutputFile: std::move(DiagnosticOutputFile));
706
707 auto ModuleLoader = [&](StringRef Identifier) {
708 llvm::TimeTraceScope moduleLoaderScope("Module loader", Identifier);
709 assert(Mod.getContext().isODRUniquingDebugTypes() &&
710 "ODR Type uniquing should be enabled on the context");
711 if (ModuleMap) {
712 auto I = ModuleMap->find(Key: Identifier);
713 assert(I != ModuleMap->end());
714 return I->second.getLazyModule(Context&: Mod.getContext(),
715 /*ShouldLazyLoadMetadata=*/true,
716 /*IsImporting*/ true);
717 }
718
719 ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> MBOrErr =
720 llvm::MemoryBuffer::getFile(Filename: Identifier);
721 if (!MBOrErr)
722 return Expected<std::unique_ptr<llvm::Module>>(make_error<StringError>(
723 Args: Twine("Error loading imported file ") + Identifier + " : ",
724 Args: MBOrErr.getError()));
725
726 Expected<BitcodeModule> BMOrErr = findThinLTOModule(MBRef: **MBOrErr);
727 if (!BMOrErr)
728 return Expected<std::unique_ptr<llvm::Module>>(make_error<StringError>(
729 Args: Twine("Error loading imported file ") + Identifier + " : " +
730 toString(E: BMOrErr.takeError()),
731 Args: inconvertibleErrorCode()));
732
733 Expected<std::unique_ptr<Module>> MOrErr =
734 BMOrErr->getLazyModule(Context&: Mod.getContext(),
735 /*ShouldLazyLoadMetadata=*/true,
736 /*IsImporting*/ true);
737 if (MOrErr)
738 (*MOrErr)->setOwnedMemoryBuffer(std::move(*MBOrErr));
739 return MOrErr;
740 };
741
742 {
743 llvm::TimeTraceScope importScope("Import functions");
744 FunctionImporter Importer(CombinedIndex, ModuleLoader,
745 ClearDSOLocalOnDeclarations);
746 if (Error Err = Importer.importFunctions(M&: Mod, ImportList).takeError())
747 return Err;
748 }
749
750 // Do this after any importing so that imported code is updated.
751 updatePublicTypeTestCalls(M&: Mod, WholeProgramVisibilityEnabledInLTO: CombinedIndex.withWholeProgramVisibility());
752
753 if (Conf.PostImportModuleHook && !Conf.PostImportModuleHook(Task, Mod))
754 return finalizeOptimizationRemarks(DiagOutputFile: std::move(DiagnosticOutputFile));
755
756 return OptimizeAndCodegen(Mod, TM.get(), std::move(DiagnosticOutputFile));
757}
758
759BitcodeModule *lto::findThinLTOModule(MutableArrayRef<BitcodeModule> BMs) {
760 if (ThinLTOAssumeMerged && BMs.size() == 1)
761 return BMs.begin();
762
763 for (BitcodeModule &BM : BMs) {
764 Expected<BitcodeLTOInfo> LTOInfo = BM.getLTOInfo();
765 if (LTOInfo && LTOInfo->IsThinLTO)
766 return &BM;
767 }
768 return nullptr;
769}
770
771Expected<BitcodeModule> lto::findThinLTOModule(MemoryBufferRef MBRef) {
772 Expected<std::vector<BitcodeModule>> BMsOrErr = getBitcodeModuleList(Buffer: MBRef);
773 if (!BMsOrErr)
774 return BMsOrErr.takeError();
775
776 // The bitcode file may contain multiple modules, we want the one that is
777 // marked as being the ThinLTO module.
778 if (const BitcodeModule *Bm = lto::findThinLTOModule(BMs: *BMsOrErr))
779 return *Bm;
780
781 return make_error<StringError>(Args: "Could not find module summary",
782 Args: inconvertibleErrorCode());
783}
784
785bool lto::initImportList(const Module &M,
786 const ModuleSummaryIndex &CombinedIndex,
787 FunctionImporter::ImportMapTy &ImportList) {
788 if (ThinLTOAssumeMerged)
789 return true;
790 // We can simply import the values mentioned in the combined index, since
791 // we should only invoke this using the individual indexes written out
792 // via a WriteIndexesThinBackend.
793 for (const auto &GlobalList : CombinedIndex) {
794 // Ignore entries for undefined references.
795 if (GlobalList.second.getSummaryList().empty())
796 continue;
797
798 auto GUID = GlobalList.first;
799 for (const auto &Summary : GlobalList.second.getSummaryList()) {
800 // Skip the summaries for the importing module. These are included to
801 // e.g. record required linkage changes.
802 if (Summary->modulePath() == M.getModuleIdentifier())
803 continue;
804 // Add an entry to provoke importing by thinBackend.
805 ImportList.addGUID(FromModule: Summary->modulePath(), GUID, ImportKind: Summary->importType());
806 }
807 }
808 return true;
809}
810