xref: /llvm-project-15.0.7/lld/ELF/LTO.cpp (revision 433eecad)
1 //===- LTO.cpp ------------------------------------------------------------===//
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 #include "LTO.h"
10 #include "Config.h"
11 #include "InputFiles.h"
12 #include "LinkerScript.h"
13 #include "SymbolTable.h"
14 #include "Symbols.h"
15 #include "lld/Common/Args.h"
16 #include "lld/Common/ErrorHandler.h"
17 #include "lld/Common/TargetOptionsCommandFlags.h"
18 #include "llvm/ADT/STLExtras.h"
19 #include "llvm/ADT/SmallString.h"
20 #include "llvm/ADT/StringRef.h"
21 #include "llvm/ADT/Twine.h"
22 #include "llvm/BinaryFormat/ELF.h"
23 #include "llvm/Bitcode/BitcodeReader.h"
24 #include "llvm/Bitcode/BitcodeWriter.h"
25 #include "llvm/IR/DiagnosticPrinter.h"
26 #include "llvm/LTO/Caching.h"
27 #include "llvm/LTO/Config.h"
28 #include "llvm/LTO/LTO.h"
29 #include "llvm/Object/SymbolicFile.h"
30 #include "llvm/Support/CodeGen.h"
31 #include "llvm/Support/Error.h"
32 #include "llvm/Support/FileSystem.h"
33 #include "llvm/Support/MemoryBuffer.h"
34 #include <algorithm>
35 #include <cstddef>
36 #include <memory>
37 #include <string>
38 #include <system_error>
39 #include <vector>
40 
41 using namespace llvm;
42 using namespace llvm::object;
43 using namespace llvm::ELF;
44 
45 using namespace lld;
46 using namespace lld::elf;
47 
48 // Creates an empty file to store a list of object files for final
49 // linking of distributed ThinLTO.
50 static std::unique_ptr<raw_fd_ostream> openFile(StringRef File) {
51   std::error_code EC;
52   auto Ret =
53       llvm::make_unique<raw_fd_ostream>(File, EC, sys::fs::OpenFlags::F_None);
54   if (EC) {
55     error("cannot open " + File + ": " + EC.message());
56     return nullptr;
57   }
58   return Ret;
59 }
60 
61 static std::string getThinLTOOutputFile(StringRef ModulePath) {
62   return lto::getThinLTOOutputFile(ModulePath,
63                                    Config->ThinLTOPrefixReplace.first,
64                                    Config->ThinLTOPrefixReplace.second);
65 }
66 
67 static lto::Config createConfig() {
68   lto::Config C;
69 
70   // LLD supports the new relocations and address-significance tables.
71   C.Options = initTargetOptionsFromCodeGenFlags();
72   C.Options.RelaxELFRelocations = true;
73   C.Options.EmitAddrsig = true;
74 
75   // Always emit a section per function/datum with LTO.
76   C.Options.FunctionSections = true;
77   C.Options.DataSections = true;
78 
79   if (Config->Relocatable)
80     C.RelocModel = None;
81   else if (Config->Pic)
82     C.RelocModel = Reloc::PIC_;
83   else
84     C.RelocModel = Reloc::Static;
85 
86   C.CodeModel = getCodeModelFromCMModel();
87   C.DisableVerify = Config->DisableVerify;
88   C.DiagHandler = diagnosticHandler;
89   C.OptLevel = Config->LTOO;
90   C.CPU = getCPUStr();
91   C.MAttrs = getMAttrs();
92   C.CGOptLevel = args::getCGOptLevel(Config->LTOO);
93 
94   // Set up a custom pipeline if we've been asked to.
95   C.OptPipeline = Config->LTONewPmPasses;
96   C.AAPipeline = Config->LTOAAPipeline;
97 
98   // Set up optimization remarks if we've been asked to.
99   C.RemarksFilename = Config->OptRemarksFilename;
100   C.RemarksPasses = Config->OptRemarksPasses;
101   C.RemarksWithHotness = Config->OptRemarksWithHotness;
102 
103   C.SampleProfile = Config->LTOSampleProfile;
104   C.UseNewPM = Config->LTONewPassManager;
105   C.DebugPassManager = Config->LTODebugPassManager;
106   C.DwoDir = Config->DwoDir;
107 
108   C.CSIRProfile = Config->LTOCSProfileFile;
109   C.RunCSIRInstr = Config->LTOCSProfileGenerate;
110 
111   if (Config->EmitLLVM) {
112     C.PostInternalizeModuleHook = [](size_t Task, const Module &M) {
113       if (std::unique_ptr<raw_fd_ostream> OS = openFile(Config->OutputFile))
114         WriteBitcodeToFile(M, *OS, false);
115       return false;
116     };
117   }
118 
119   if (Config->SaveTemps)
120     checkError(C.addSaveTemps(Config->OutputFile.str() + ".",
121                               /*UseInputModulePath*/ true));
122   return C;
123 }
124 
125 BitcodeCompiler::BitcodeCompiler() {
126   // Initialize IndexFile.
127   if (!Config->ThinLTOIndexOnlyArg.empty())
128     IndexFile = openFile(Config->ThinLTOIndexOnlyArg);
129 
130   // Initialize LTOObj.
131   lto::ThinBackend Backend;
132   if (Config->ThinLTOIndexOnly) {
133     auto OnIndexWrite = [&](StringRef S) { ThinIndices.erase(S); };
134     Backend = lto::createWriteIndexesThinBackend(
135         Config->ThinLTOPrefixReplace.first, Config->ThinLTOPrefixReplace.second,
136         Config->ThinLTOEmitImportsFiles, IndexFile.get(), OnIndexWrite);
137   } else if (Config->ThinLTOJobs != -1U) {
138     Backend = lto::createInProcessThinBackend(Config->ThinLTOJobs);
139   }
140 
141   LTOObj = llvm::make_unique<lto::LTO>(createConfig(), Backend,
142                                        Config->LTOPartitions);
143 
144   // Initialize UsedStartStop.
145   for (Symbol *Sym : Symtab->getSymbols()) {
146     StringRef S = Sym->getName();
147     for (StringRef Prefix : {"__start_", "__stop_"})
148       if (S.startswith(Prefix))
149         UsedStartStop.insert(S.substr(Prefix.size()));
150   }
151 }
152 
153 BitcodeCompiler::~BitcodeCompiler() = default;
154 
155 static void undefine(Symbol *S) {
156   replaceSymbol<Undefined>(S, nullptr, S->getName(), STB_GLOBAL, STV_DEFAULT,
157                            S->Type);
158 }
159 
160 void BitcodeCompiler::add(BitcodeFile &F) {
161   lto::InputFile &Obj = *F.Obj;
162   bool IsExec = !Config->Shared && !Config->Relocatable;
163 
164   if (Config->ThinLTOIndexOnly)
165     ThinIndices.insert(Obj.getName());
166 
167   ArrayRef<Symbol *> Syms = F.getSymbols();
168   ArrayRef<lto::InputFile::Symbol> ObjSyms = Obj.symbols();
169   std::vector<lto::SymbolResolution> Resols(Syms.size());
170 
171   // Provide a resolution to the LTO API for each symbol.
172   for (size_t I = 0, E = Syms.size(); I != E; ++I) {
173     Symbol *Sym = Syms[I];
174     const lto::InputFile::Symbol &ObjSym = ObjSyms[I];
175     lto::SymbolResolution &R = Resols[I];
176 
177     // Ideally we shouldn't check for SF_Undefined but currently IRObjectFile
178     // reports two symbols for module ASM defined. Without this check, lld
179     // flags an undefined in IR with a definition in ASM as prevailing.
180     // Once IRObjectFile is fixed to report only one symbol this hack can
181     // be removed.
182     R.Prevailing = !ObjSym.isUndefined() && Sym->File == &F;
183 
184     // We ask LTO to preserve following global symbols:
185     // 1) All symbols when doing relocatable link, so that them can be used
186     //    for doing final link.
187     // 2) Symbols that are used in regular objects.
188     // 3) C named sections if we have corresponding __start_/__stop_ symbol.
189     // 4) Symbols that are defined in bitcode files and used for dynamic linking.
190     R.VisibleToRegularObj = Config->Relocatable || Sym->IsUsedInRegularObj ||
191                             (R.Prevailing && Sym->includeInDynsym()) ||
192                             UsedStartStop.count(ObjSym.getSectionName());
193     const auto *DR = dyn_cast<Defined>(Sym);
194     R.FinalDefinitionInLinkageUnit =
195         (IsExec || Sym->Visibility != STV_DEFAULT) && DR &&
196         // Skip absolute symbols from ELF objects, otherwise PC-rel relocations
197         // will be generated by for them, triggering linker errors.
198         // Symbol section is always null for bitcode symbols, hence the check
199         // for isElf(). Skip linker script defined symbols as well: they have
200         // no File defined.
201         !(DR->Section == nullptr && (!Sym->File || Sym->File->isElf()));
202 
203     if (R.Prevailing)
204       undefine(Sym);
205 
206     // We tell LTO to not apply interprocedural optimization for wrapped
207     // (with --wrap) symbols because otherwise LTO would inline them while
208     // their values are still not final.
209     R.LinkerRedefined = !Sym->CanInline;
210   }
211   checkError(LTOObj->add(std::move(F.Obj), Resols));
212 }
213 
214 static void createEmptyIndex(StringRef ModulePath) {
215   std::string Path = replaceThinLTOSuffix(getThinLTOOutputFile(ModulePath));
216   std::unique_ptr<raw_fd_ostream> OS = openFile(Path + ".thinlto.bc");
217   if (!OS)
218     return;
219 
220   ModuleSummaryIndex M(/*HaveGVs*/ false);
221   M.setSkipModuleByDistributedBackend();
222   WriteIndexToFile(M, *OS);
223 
224   if (Config->ThinLTOEmitImportsFiles)
225     openFile(Path + ".imports");
226 }
227 
228 // Merge all the bitcode files we have seen, codegen the result
229 // and return the resulting ObjectFile(s).
230 std::vector<InputFile *> BitcodeCompiler::compile() {
231   unsigned MaxTasks = LTOObj->getMaxTasks();
232   Buf.resize(MaxTasks);
233   Files.resize(MaxTasks);
234 
235   // The --thinlto-cache-dir option specifies the path to a directory in which
236   // to cache native object files for ThinLTO incremental builds. If a path was
237   // specified, configure LTO to use it as the cache directory.
238   lto::NativeObjectCache Cache;
239   if (!Config->ThinLTOCacheDir.empty())
240     Cache = check(
241         lto::localCache(Config->ThinLTOCacheDir,
242                         [&](size_t Task, std::unique_ptr<MemoryBuffer> MB) {
243                           Files[Task] = std::move(MB);
244                         }));
245 
246   checkError(LTOObj->run(
247       [&](size_t Task) {
248         return llvm::make_unique<lto::NativeObjectStream>(
249             llvm::make_unique<raw_svector_ostream>(Buf[Task]));
250       },
251       Cache));
252 
253   // Emit empty index files for non-indexed files
254   for (StringRef S : ThinIndices) {
255     std::string Path = getThinLTOOutputFile(S);
256     openFile(Path + ".thinlto.bc");
257     if (Config->ThinLTOEmitImportsFiles)
258       openFile(Path + ".imports");
259   }
260 
261   // If LazyObjFile has not been added to link, emit empty index files.
262   // This is needed because this is what GNU gold plugin does and we have a
263   // distributed build system that depends on that behavior.
264   if (Config->ThinLTOIndexOnly) {
265     for (LazyObjFile *F : LazyObjFiles)
266       if (!F->AddedToLink && isBitcode(F->MB))
267         createEmptyIndex(F->getName());
268 
269     if (!Config->LTOObjPath.empty())
270       saveBuffer(Buf[0], Config->LTOObjPath);
271 
272     // ThinLTO with index only option is required to generate only the index
273     // files. After that, we exit from linker and ThinLTO backend runs in a
274     // distributed environment.
275     if (IndexFile)
276       IndexFile->close();
277     return {};
278   }
279 
280   if (!Config->ThinLTOCacheDir.empty())
281     pruneCache(Config->ThinLTOCacheDir, Config->ThinLTOCachePolicy);
282 
283   if (!Config->LTOObjPath.empty()) {
284     saveBuffer(Buf[0], Config->LTOObjPath);
285     for (unsigned I = 1; I != MaxTasks; ++I)
286       saveBuffer(Buf[I], Config->LTOObjPath + Twine(I));
287   }
288 
289   if (Config->SaveTemps) {
290     saveBuffer(Buf[0], Config->OutputFile + ".lto.o");
291     for (unsigned I = 1; I != MaxTasks; ++I)
292       saveBuffer(Buf[I], Config->OutputFile + Twine(I) + ".lto.o");
293   }
294 
295   std::vector<InputFile *> Ret;
296   for (unsigned I = 0; I != MaxTasks; ++I)
297     if (!Buf[I].empty())
298       Ret.push_back(createObjectFile(MemoryBufferRef(Buf[I], "lto.tmp")));
299 
300   for (std::unique_ptr<MemoryBuffer> &File : Files)
301     if (File)
302       Ret.push_back(createObjectFile(*File));
303   return Ret;
304 }
305