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