xref: /freebsd/contrib/llvm-project/lld/ELF/LTO.cpp (revision 911f0260390e18cf85f3dbf2c719b593efdc1e3c)
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 "SymbolTable.h"
13 #include "Symbols.h"
14 #include "lld/Common/Args.h"
15 #include "lld/Common/ErrorHandler.h"
16 #include "lld/Common/Strings.h"
17 #include "lld/Common/TargetOptionsCommandFlags.h"
18 #include "llvm/ADT/SmallString.h"
19 #include "llvm/ADT/StringRef.h"
20 #include "llvm/ADT/Twine.h"
21 #include "llvm/BinaryFormat/ELF.h"
22 #include "llvm/Bitcode/BitcodeWriter.h"
23 #include "llvm/LTO/Config.h"
24 #include "llvm/LTO/LTO.h"
25 #include "llvm/Support/Caching.h"
26 #include "llvm/Support/CodeGen.h"
27 #include "llvm/Support/Error.h"
28 #include "llvm/Support/FileSystem.h"
29 #include "llvm/Support/MemoryBuffer.h"
30 #include <algorithm>
31 #include <cstddef>
32 #include <memory>
33 #include <string>
34 #include <system_error>
35 #include <vector>
36 
37 using namespace llvm;
38 using namespace llvm::object;
39 using namespace llvm::ELF;
40 using namespace lld;
41 using namespace lld::elf;
42 
43 // Creates an empty file to store a list of object files for final
44 // linking of distributed ThinLTO.
45 static std::unique_ptr<raw_fd_ostream> openFile(StringRef file) {
46   std::error_code ec;
47   auto ret =
48       std::make_unique<raw_fd_ostream>(file, ec, sys::fs::OpenFlags::OF_None);
49   if (ec) {
50     error("cannot open " + file + ": " + ec.message());
51     return nullptr;
52   }
53   return ret;
54 }
55 
56 // The merged bitcode after LTO is large. Try opening a file stream that
57 // supports reading, seeking and writing. Such a file allows BitcodeWriter to
58 // flush buffered data to reduce memory consumption. If this fails, open a file
59 // stream that supports only write.
60 static std::unique_ptr<raw_fd_ostream> openLTOOutputFile(StringRef file) {
61   std::error_code ec;
62   std::unique_ptr<raw_fd_ostream> fs =
63       std::make_unique<raw_fd_stream>(file, ec);
64   if (!ec)
65     return fs;
66   return openFile(file);
67 }
68 
69 static std::string getThinLTOOutputFile(StringRef modulePath) {
70   return lto::getThinLTOOutputFile(
71       std::string(modulePath), std::string(config->thinLTOPrefixReplace.first),
72       std::string(config->thinLTOPrefixReplace.second));
73 }
74 
75 static lto::Config createConfig() {
76   lto::Config c;
77 
78   // LLD supports the new relocations and address-significance tables.
79   c.Options = initTargetOptionsFromCodeGenFlags();
80   c.Options.EmitAddrsig = true;
81   for (StringRef C : config->mllvmOpts)
82     c.MllvmArgs.emplace_back(C.str());
83 
84   // Always emit a section per function/datum with LTO.
85   c.Options.FunctionSections = true;
86   c.Options.DataSections = true;
87 
88   // Check if basic block sections must be used.
89   // Allowed values for --lto-basic-block-sections are "all", "labels",
90   // "<file name specifying basic block ids>", or none.  This is the equivalent
91   // of -fbasic-block-sections= flag in clang.
92   if (!config->ltoBasicBlockSections.empty()) {
93     if (config->ltoBasicBlockSections == "all") {
94       c.Options.BBSections = BasicBlockSection::All;
95     } else if (config->ltoBasicBlockSections == "labels") {
96       c.Options.BBSections = BasicBlockSection::Labels;
97     } else if (config->ltoBasicBlockSections == "none") {
98       c.Options.BBSections = BasicBlockSection::None;
99     } else {
100       ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr =
101           MemoryBuffer::getFile(config->ltoBasicBlockSections.str());
102       if (!MBOrErr) {
103         error("cannot open " + config->ltoBasicBlockSections + ":" +
104               MBOrErr.getError().message());
105       } else {
106         c.Options.BBSectionsFuncListBuf = std::move(*MBOrErr);
107       }
108       c.Options.BBSections = BasicBlockSection::List;
109     }
110   }
111 
112   c.Options.UniqueBasicBlockSectionNames =
113       config->ltoUniqueBasicBlockSectionNames;
114 
115   if (auto relocModel = getRelocModelFromCMModel())
116     c.RelocModel = *relocModel;
117   else if (config->relocatable)
118     c.RelocModel = std::nullopt;
119   else if (config->isPic)
120     c.RelocModel = Reloc::PIC_;
121   else
122     c.RelocModel = Reloc::Static;
123 
124   c.CodeModel = getCodeModelFromCMModel();
125   c.DisableVerify = config->disableVerify;
126   c.DiagHandler = diagnosticHandler;
127   c.OptLevel = config->ltoo;
128   c.CPU = getCPUStr();
129   c.MAttrs = getMAttrs();
130   c.CGOptLevel = args::getCGOptLevel(config->ltoo);
131 
132   c.PTO.LoopVectorization = c.OptLevel > 1;
133   c.PTO.SLPVectorization = c.OptLevel > 1;
134 
135   // Set up a custom pipeline if we've been asked to.
136   c.OptPipeline = std::string(config->ltoNewPmPasses);
137   c.AAPipeline = std::string(config->ltoAAPipeline);
138 
139   // Set up optimization remarks if we've been asked to.
140   c.RemarksFilename = std::string(config->optRemarksFilename);
141   c.RemarksPasses = std::string(config->optRemarksPasses);
142   c.RemarksWithHotness = config->optRemarksWithHotness;
143   c.RemarksHotnessThreshold = config->optRemarksHotnessThreshold;
144   c.RemarksFormat = std::string(config->optRemarksFormat);
145 
146   // Set up output file to emit statistics.
147   c.StatsFile = std::string(config->optStatsFilename);
148 
149   c.SampleProfile = std::string(config->ltoSampleProfile);
150   for (StringRef pluginFn : config->passPlugins)
151     c.PassPlugins.push_back(std::string(pluginFn));
152   c.DebugPassManager = config->ltoDebugPassManager;
153   c.DwoDir = std::string(config->dwoDir);
154 
155   c.HasWholeProgramVisibility = config->ltoWholeProgramVisibility;
156   c.AlwaysEmitRegularLTOObj = !config->ltoObjPath.empty();
157 
158   for (const llvm::StringRef &name : config->thinLTOModulesToCompile)
159     c.ThinLTOModulesToCompile.emplace_back(name);
160 
161   c.TimeTraceEnabled = config->timeTraceEnabled;
162   c.TimeTraceGranularity = config->timeTraceGranularity;
163 
164   c.CSIRProfile = std::string(config->ltoCSProfileFile);
165   c.RunCSIRInstr = config->ltoCSProfileGenerate;
166   c.PGOWarnMismatch = config->ltoPGOWarnMismatch;
167 
168   c.OpaquePointers = config->opaquePointers;
169 
170   if (config->emitLLVM) {
171     c.PostInternalizeModuleHook = [](size_t task, const Module &m) {
172       if (std::unique_ptr<raw_fd_ostream> os =
173               openLTOOutputFile(config->outputFile))
174         WriteBitcodeToFile(m, *os, false);
175       return false;
176     };
177   }
178 
179   if (config->ltoEmitAsm) {
180     c.CGFileType = CGFT_AssemblyFile;
181     c.Options.MCOptions.AsmVerbose = true;
182   }
183 
184   if (!config->saveTempsArgs.empty())
185     checkError(c.addSaveTemps(config->outputFile.str() + ".",
186                               /*UseInputModulePath*/ true,
187                               config->saveTempsArgs));
188   return c;
189 }
190 
191 BitcodeCompiler::BitcodeCompiler() {
192   // Initialize indexFile.
193   if (!config->thinLTOIndexOnlyArg.empty())
194     indexFile = openFile(config->thinLTOIndexOnlyArg);
195 
196   // Initialize ltoObj.
197   lto::ThinBackend backend;
198   auto onIndexWrite = [&](StringRef s) { thinIndices.erase(s); };
199   if (config->thinLTOIndexOnly) {
200     backend = lto::createWriteIndexesThinBackend(
201         std::string(config->thinLTOPrefixReplace.first),
202         std::string(config->thinLTOPrefixReplace.second),
203         config->thinLTOEmitImportsFiles, indexFile.get(), onIndexWrite);
204   } else {
205     backend = lto::createInProcessThinBackend(
206         llvm::heavyweight_hardware_concurrency(config->thinLTOJobs),
207         onIndexWrite, config->thinLTOEmitIndexFiles,
208         config->thinLTOEmitImportsFiles);
209   }
210 
211   ltoObj = std::make_unique<lto::LTO>(createConfig(), backend,
212                                        config->ltoPartitions);
213 
214   // Initialize usedStartStop.
215   if (ctx.bitcodeFiles.empty())
216     return;
217   for (Symbol *sym : symtab.getSymbols()) {
218     if (sym->isPlaceholder())
219       continue;
220     StringRef s = sym->getName();
221     for (StringRef prefix : {"__start_", "__stop_"})
222       if (s.startswith(prefix))
223         usedStartStop.insert(s.substr(prefix.size()));
224   }
225 }
226 
227 BitcodeCompiler::~BitcodeCompiler() = default;
228 
229 void BitcodeCompiler::add(BitcodeFile &f) {
230   lto::InputFile &obj = *f.obj;
231   bool isExec = !config->shared && !config->relocatable;
232 
233   if (config->thinLTOEmitIndexFiles)
234     thinIndices.insert(obj.getName());
235 
236   ArrayRef<Symbol *> syms = f.getSymbols();
237   ArrayRef<lto::InputFile::Symbol> objSyms = obj.symbols();
238   std::vector<lto::SymbolResolution> resols(syms.size());
239 
240   // Provide a resolution to the LTO API for each symbol.
241   for (size_t i = 0, e = syms.size(); i != e; ++i) {
242     Symbol *sym = syms[i];
243     const lto::InputFile::Symbol &objSym = objSyms[i];
244     lto::SymbolResolution &r = resols[i];
245 
246     // Ideally we shouldn't check for SF_Undefined but currently IRObjectFile
247     // reports two symbols for module ASM defined. Without this check, lld
248     // flags an undefined in IR with a definition in ASM as prevailing.
249     // Once IRObjectFile is fixed to report only one symbol this hack can
250     // be removed.
251     r.Prevailing = !objSym.isUndefined() && sym->file == &f;
252 
253     // We ask LTO to preserve following global symbols:
254     // 1) All symbols when doing relocatable link, so that them can be used
255     //    for doing final link.
256     // 2) Symbols that are used in regular objects.
257     // 3) C named sections if we have corresponding __start_/__stop_ symbol.
258     // 4) Symbols that are defined in bitcode files and used for dynamic
259     //    linking.
260     // 5) Symbols that will be referenced after linker wrapping is performed.
261     r.VisibleToRegularObj = config->relocatable || sym->isUsedInRegularObj ||
262                             sym->referencedAfterWrap ||
263                             (r.Prevailing && sym->includeInDynsym()) ||
264                             usedStartStop.count(objSym.getSectionName());
265     // Identify symbols exported dynamically, and that therefore could be
266     // referenced by a shared library not visible to the linker.
267     r.ExportDynamic =
268         sym->computeBinding() != STB_LOCAL &&
269         (config->exportDynamic || sym->exportDynamic || sym->inDynamicList);
270     const auto *dr = dyn_cast<Defined>(sym);
271     r.FinalDefinitionInLinkageUnit =
272         (isExec || sym->visibility() != STV_DEFAULT) && dr &&
273         // Skip absolute symbols from ELF objects, otherwise PC-rel relocations
274         // will be generated by for them, triggering linker errors.
275         // Symbol section is always null for bitcode symbols, hence the check
276         // for isElf(). Skip linker script defined symbols as well: they have
277         // no File defined.
278         !(dr->section == nullptr && (!sym->file || sym->file->isElf()));
279 
280     if (r.Prevailing)
281       Undefined(nullptr, StringRef(), STB_GLOBAL, STV_DEFAULT, sym->type)
282           .overwrite(*sym);
283 
284     // We tell LTO to not apply interprocedural optimization for wrapped
285     // (with --wrap) symbols because otherwise LTO would inline them while
286     // their values are still not final.
287     r.LinkerRedefined = sym->scriptDefined;
288   }
289   checkError(ltoObj->add(std::move(f.obj), resols));
290 }
291 
292 // If LazyObjFile has not been added to link, emit empty index files.
293 // This is needed because this is what GNU gold plugin does and we have a
294 // distributed build system that depends on that behavior.
295 static void thinLTOCreateEmptyIndexFiles() {
296   DenseSet<StringRef> linkedBitCodeFiles;
297   for (BitcodeFile *f : ctx.bitcodeFiles)
298     linkedBitCodeFiles.insert(f->getName());
299 
300   for (BitcodeFile *f : ctx.lazyBitcodeFiles) {
301     if (!f->lazy)
302       continue;
303     if (linkedBitCodeFiles.contains(f->getName()))
304       continue;
305     std::string path =
306         replaceThinLTOSuffix(getThinLTOOutputFile(f->obj->getName()));
307     std::unique_ptr<raw_fd_ostream> os = openFile(path + ".thinlto.bc");
308     if (!os)
309       continue;
310 
311     ModuleSummaryIndex m(/*HaveGVs*/ false);
312     m.setSkipModuleByDistributedBackend();
313     writeIndexToFile(m, *os);
314     if (config->thinLTOEmitImportsFiles)
315       openFile(path + ".imports");
316   }
317 }
318 
319 // Merge all the bitcode files we have seen, codegen the result
320 // and return the resulting ObjectFile(s).
321 std::vector<InputFile *> BitcodeCompiler::compile() {
322   unsigned maxTasks = ltoObj->getMaxTasks();
323   buf.resize(maxTasks);
324   files.resize(maxTasks);
325 
326   // The --thinlto-cache-dir option specifies the path to a directory in which
327   // to cache native object files for ThinLTO incremental builds. If a path was
328   // specified, configure LTO to use it as the cache directory.
329   FileCache cache;
330   if (!config->thinLTOCacheDir.empty())
331     cache = check(localCache("ThinLTO", "Thin", config->thinLTOCacheDir,
332                              [&](size_t task, const Twine &moduleName,
333                                  std::unique_ptr<MemoryBuffer> mb) {
334                                files[task] = std::move(mb);
335                              }));
336 
337   if (!ctx.bitcodeFiles.empty())
338     checkError(ltoObj->run(
339         [&](size_t task, const Twine &moduleName) {
340           return std::make_unique<CachedFileStream>(
341               std::make_unique<raw_svector_ostream>(buf[task]));
342         },
343         cache));
344 
345   // Emit empty index files for non-indexed files but not in single-module mode.
346   if (config->thinLTOModulesToCompile.empty()) {
347     for (StringRef s : thinIndices) {
348       std::string path = getThinLTOOutputFile(s);
349       openFile(path + ".thinlto.bc");
350       if (config->thinLTOEmitImportsFiles)
351         openFile(path + ".imports");
352     }
353   }
354 
355   if (config->thinLTOEmitIndexFiles)
356     thinLTOCreateEmptyIndexFiles();
357 
358   if (config->thinLTOIndexOnly) {
359     if (!config->ltoObjPath.empty())
360       saveBuffer(buf[0], config->ltoObjPath);
361 
362     // ThinLTO with index only option is required to generate only the index
363     // files. After that, we exit from linker and ThinLTO backend runs in a
364     // distributed environment.
365     if (indexFile)
366       indexFile->close();
367     return {};
368   }
369 
370   if (!config->thinLTOCacheDir.empty())
371     pruneCache(config->thinLTOCacheDir, config->thinLTOCachePolicy, files);
372 
373   if (!config->ltoObjPath.empty()) {
374     saveBuffer(buf[0], config->ltoObjPath);
375     for (unsigned i = 1; i != maxTasks; ++i)
376       saveBuffer(buf[i], config->ltoObjPath + Twine(i));
377   }
378 
379   if (config->saveTempsArgs.contains("prelink")) {
380     if (!buf[0].empty())
381       saveBuffer(buf[0], config->outputFile + ".lto.o");
382     for (unsigned i = 1; i != maxTasks; ++i)
383       saveBuffer(buf[i], config->outputFile + Twine(i) + ".lto.o");
384   }
385 
386   if (config->ltoEmitAsm) {
387     saveBuffer(buf[0], config->outputFile);
388     for (unsigned i = 1; i != maxTasks; ++i)
389       saveBuffer(buf[i], config->outputFile + Twine(i));
390     return {};
391   }
392 
393   std::vector<InputFile *> ret;
394   for (unsigned i = 0; i != maxTasks; ++i)
395     if (!buf[i].empty())
396       ret.push_back(createObjFile(MemoryBufferRef(buf[i], "lto.tmp")));
397 
398   for (std::unique_ptr<MemoryBuffer> &file : files)
399     if (file)
400       ret.push_back(createObjFile(*file));
401   return ret;
402 }
403