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 "Symbols.h"
13#include "lld/Common/CommonLinkerContext.h"
14#include "lld/Common/ErrorHandler.h"
15#include "lld/Common/Filesystem.h"
16#include "lld/Common/Strings.h"
17#include "lld/Common/TargetOptionsCommandFlags.h"
18#include "llvm/ADT/StringRef.h"
19#include "llvm/ADT/Twine.h"
20#include "llvm/Bitcode/BitcodeWriter.h"
21#include "llvm/IR/DiagnosticPrinter.h"
22#include "llvm/LTO/Config.h"
23#include "llvm/LTO/LTO.h"
24#include "llvm/Support/Caching.h"
25#include "llvm/Support/CodeGen.h"
26#include "llvm/Support/MemoryBuffer.h"
27#include "llvm/Support/Path.h"
28#include "llvm/Support/raw_ostream.h"
29#include <cstddef>
30#include <memory>
31#include <string>
32#include <vector>
33
34using namespace llvm;
35using namespace lld::wasm;
36using namespace lld;
37
38static std::string getThinLTOOutputFile(StringRef modulePath) {
39 return lto::getThinLTOOutputFile(Path: modulePath, OldPrefix: ctx.arg.thinLTOPrefixReplaceOld,
40 NewPrefix: ctx.arg.thinLTOPrefixReplaceNew);
41}
42
43static lto::Config createConfig() {
44 lto::Config c;
45 c.LibraryOptions = &ctx.arg.libraryOptions;
46 c.Options = getTargetOptions(libraryOptions: ctx.arg.libraryOptions);
47
48 // Always emit a section per function/data with LTO.
49 c.Options.FunctionSections = true;
50 c.Options.DataSections = true;
51
52 c.DisableVerify = ctx.arg.disableVerify;
53 c.DiagHandler = diagnosticHandler;
54 c.OptLevel = ctx.arg.ltoo;
55 c.CPU = getCPUStr(libraryOptions: ctx.arg.libraryOptions);
56 c.MAttrs = getMAttrs(libraryOptions: ctx.arg.libraryOptions);
57
58 // If shared memory is enabled, ensure the TargetMachine backend is
59 // instantiated with atomics and bulk-memory features so that empty
60 // partitioned ThinLTO modules don't incorrectly strip TLS variables or fall
61 // back to defaults. This bypasses a bug where deleted functions take their
62 // target-features away. This is only necessary for atomics (and not other
63 // features) because Atomics and TLS are the only features we lower away
64 // during codegen.
65 if (ctx.arg.sharedMemory) {
66 c.MAttrs.push_back(x: "+atomics");
67 c.MAttrs.push_back(x: "+bulk-memory");
68 }
69
70 c.CGOptLevel = ctx.arg.ltoCgo;
71 c.DebugPassManager = ctx.arg.ltoDebugPassManager;
72 c.AlwaysEmitRegularLTOObj = !ctx.arg.ltoObjPath.empty();
73
74 if (auto relocModel = getRelocModelFromCMModel(libraryOptions: ctx.arg.libraryOptions))
75 c.RelocModel = *relocModel;
76 else if (ctx.arg.relocatable)
77 c.RelocModel = std::nullopt;
78 else if (ctx.isPic)
79 c.RelocModel = Reloc::PIC_;
80 else if (ctx.arg.unresolvedSymbols == UnresolvedPolicy::ImportDynamic)
81 // With ImportDynamic we also need to use the PIC relocation model so that
82 // external symbols are references via the GOT.
83 // TODO(sbc): This should probably be Reloc::DynamicNoPIC, but the backend
84 // doesn't currently support that.
85 c.RelocModel = Reloc::PIC_;
86 else
87 c.RelocModel = Reloc::Static;
88
89 if (ctx.arg.saveTemps)
90 checkError(e: c.addSaveTemps(OutputFileName: ctx.arg.outputFile.str() + ".",
91 /*UseInputModulePath*/ true));
92 return c;
93}
94
95namespace lld::wasm {
96
97BitcodeCompiler::BitcodeCompiler() {
98 // Initialize indexFile.
99 if (!ctx.arg.thinLTOIndexOnlyArg.empty())
100 indexFile = openFile(file: ctx.arg.thinLTOIndexOnlyArg);
101
102 // Initialize ltoObj.
103 lto::ThinBackend backend;
104 auto onIndexWrite = [&](StringRef s) { thinIndices.erase(V: s); };
105 if (ctx.arg.thinLTOIndexOnly) {
106 backend = lto::createWriteIndexesThinBackend(
107 Parallelism: llvm::hardware_concurrency(Num: ctx.arg.thinLTOJobs),
108 OldPrefix: std::string(ctx.arg.thinLTOPrefixReplaceOld),
109 NewPrefix: std::string(ctx.arg.thinLTOPrefixReplaceNew),
110 NativeObjectPrefix: std::string(ctx.arg.thinLTOPrefixReplaceNativeObject),
111 ShouldEmitImportsFiles: ctx.arg.thinLTOEmitImportsFiles, LinkedObjectsFile: indexFile.get(), OnWrite: onIndexWrite);
112 } else {
113 backend = lto::createInProcessThinBackend(
114 Parallelism: llvm::heavyweight_hardware_concurrency(Num: ctx.arg.thinLTOJobs),
115 OnWrite: onIndexWrite, ShouldEmitIndexFiles: ctx.arg.thinLTOEmitIndexFiles,
116 ShouldEmitImportsFiles: ctx.arg.thinLTOEmitImportsFiles);
117 }
118 ltoObj = std::make_unique<lto::LTO>(args: createConfig(), args&: backend,
119 args&: ctx.arg.ltoPartitions);
120}
121
122BitcodeCompiler::~BitcodeCompiler() = default;
123
124static void undefine(Symbol *s) {
125 if (auto f = dyn_cast<DefinedFunction>(Val: s))
126 // If the signature is null, there were no calls from non-bitcode objects.
127 replaceSymbol<UndefinedFunction>(s: f, arg: f->getName(), arg: std::nullopt,
128 arg: std::nullopt, arg: 0, arg: f->getFile(),
129 arg&: f->signature, arg: f->signature != nullptr);
130 else if (isa<DefinedData>(Val: s))
131 replaceSymbol<UndefinedData>(s, arg: s->getName(), arg: 0, arg: s->getFile());
132 else
133 llvm_unreachable("unexpected symbol kind");
134}
135
136void BitcodeCompiler::add(BitcodeFile &f) {
137 lto::InputFile &obj = *f.obj;
138 unsigned symNum = 0;
139 ArrayRef<Symbol *> syms = f.getSymbols();
140 std::vector<lto::SymbolResolution> resols(syms.size());
141
142 if (ctx.arg.thinLTOEmitIndexFiles) {
143 thinIndices.insert(V: obj.getName());
144 }
145
146 // Provide a resolution to the LTO API for each symbol.
147 for (const lto::InputFile::Symbol &objSym : obj.symbols()) {
148 Symbol *sym = syms[symNum];
149 lto::SymbolResolution &r = resols[symNum];
150 ++symNum;
151
152 // Ideally we shouldn't check for SF_Undefined but currently IRObjectFile
153 // reports two symbols for module ASM defined. Without this check, lld
154 // flags an undefined in IR with a definition in ASM as prevailing.
155 // Once IRObjectFile is fixed to report only one symbol this hack can
156 // be removed.
157 r.Prevailing = !objSym.isUndefined() && sym->getFile() == &f;
158 r.VisibleToRegularObj = ctx.arg.relocatable || sym->isUsedInRegularObj ||
159 sym->isNoStrip() ||
160 (r.Prevailing && sym->isExported());
161 if (r.Prevailing)
162 undefine(s: sym);
163
164 // We tell LTO to not apply interprocedural optimization for wrapped
165 // (with --wrap) symbols because otherwise LTO would inline them while
166 // their values are still not final.
167 r.LinkerRedefined = !sym->canInline;
168 }
169 checkError(e: ltoObj->add(Obj: std::move(f.obj), Res: resols));
170}
171
172// If LazyObjFile has not been added to link, emit empty index files.
173// This is needed because this is what GNU gold plugin does and we have a
174// distributed build system that depends on that behavior.
175static void thinLTOCreateEmptyIndexFiles() {
176 DenseSet<StringRef> linkedBitCodeFiles;
177 for (BitcodeFile *f : ctx.bitcodeFiles)
178 linkedBitCodeFiles.insert(V: f->getName());
179
180 for (BitcodeFile *f : ctx.lazyBitcodeFiles) {
181 if (!f->lazy)
182 continue;
183 if (linkedBitCodeFiles.contains(V: f->getName()))
184 continue;
185 std::string path =
186 replaceThinLTOSuffix(path: getThinLTOOutputFile(modulePath: f->obj->getName()));
187 std::unique_ptr<raw_fd_ostream> os = openFile(file: path + ".thinlto.bc");
188 if (!os)
189 continue;
190
191 ModuleSummaryIndex m(/*HaveGVs*/ false);
192 m.setSkipModuleByDistributedBackend();
193 writeIndexToFile(Index: m, Out&: *os);
194 if (ctx.arg.thinLTOEmitImportsFiles)
195 openFile(file: path + ".imports");
196 }
197}
198
199void BitcodeCompiler::setBitcodeLibFuncs(ArrayRef<StringRef> bitcodeLibFuncs) {
200 ltoObj->setBitcodeLibFuncs(bitcodeLibFuncs);
201}
202
203// Merge all the bitcode files we have seen, codegen the result
204// and return the resulting objects.
205SmallVector<InputFile *, 0> BitcodeCompiler::compile() {
206 unsigned maxTasks = ltoObj->getMaxTasks();
207 buf.resize(N: maxTasks);
208 files.resize(new_size: maxTasks);
209 filenames.resize(N: maxTasks);
210
211 // The --thinlto-cache-dir option specifies the path to a directory in which
212 // to cache native object files for ThinLTO incremental builds. If a path was
213 // specified, configure LTO to use it as the cache directory.
214 FileCache cache;
215 if (!ctx.arg.thinLTOCacheDir.empty())
216 cache = check(e: localCache(CacheNameRef: "ThinLTO", TempFilePrefixRef: "Thin", CacheDirectoryPathRef: ctx.arg.thinLTOCacheDir,
217 AddBuffer: [&](size_t task, const Twine &moduleName,
218 std::unique_ptr<MemoryBuffer> mb) {
219 files[task] = std::move(mb);
220 }));
221
222 checkError(e: ltoObj->run(
223 AddStream: [&](size_t task, const Twine &moduleName) {
224 buf[task].first = moduleName.str();
225 return std::make_unique<CachedFileStream>(
226 args: std::make_unique<raw_svector_ostream>(args&: buf[task].second));
227 },
228 Cache: cache));
229
230 // Emit empty index files for non-indexed files but not in single-module mode.
231 for (StringRef s : thinIndices) {
232 std::string path(s);
233 openFile(file: path + ".thinlto.bc");
234 if (ctx.arg.thinLTOEmitImportsFiles)
235 openFile(file: path + ".imports");
236 }
237
238 if (ctx.arg.thinLTOEmitIndexFiles)
239 thinLTOCreateEmptyIndexFiles();
240
241 if (ctx.arg.thinLTOIndexOnly) {
242 if (!ctx.arg.ltoObjPath.empty())
243 saveBuffer(buffer: buf[0].second, path: ctx.arg.ltoObjPath);
244
245 // ThinLTO with index only option is required to generate only the index
246 // files. After that, we exit from linker and ThinLTO backend runs in a
247 // distributed environment.
248 if (indexFile)
249 indexFile->close();
250 return {};
251 }
252
253 if (!ctx.arg.thinLTOCacheDir.empty())
254 check(
255 e: pruneCache(Path: ctx.arg.thinLTOCacheDir, Policy: ctx.arg.thinLTOCachePolicy, Files: files));
256
257 SmallVector<InputFile *, 0> ret;
258 for (unsigned i = 0; i != maxTasks; ++i) {
259 StringRef objBuf = buf[i].second;
260 StringRef bitcodeFilePath = buf[i].first;
261 if (files[i]) {
262 // When files[i] is not null, we get the native relocatable file from the
263 // cache. filenames[i] contains the original BitcodeFile's identifier.
264 objBuf = files[i]->getBuffer();
265 bitcodeFilePath = filenames[i];
266 } else {
267 objBuf = buf[i].second;
268 bitcodeFilePath = buf[i].first;
269 }
270 if (objBuf.empty())
271 continue;
272
273 // If the input bitcode file is path/to/x.o and -o specifies a.out, the
274 // corresponding native relocatable file path will look like:
275 // path/to/a.out.lto.x.o.
276 StringRef ltoObjName;
277 if (bitcodeFilePath == "ld-temp.o") {
278 ltoObjName =
279 saver().save(S: Twine(ctx.arg.outputFile) + ".lto" +
280 (i == 0 ? Twine("") : Twine('.') + Twine(i)) + ".o");
281 } else {
282 StringRef directory = sys::path::parent_path(path: bitcodeFilePath);
283 // For an archive member, which has an identifier like "d/a.a(coll.o at
284 // 8)" (see BitcodeFile::BitcodeFile), use the filename; otherwise, use
285 // the stem (d/a.o => a).
286 StringRef baseName = bitcodeFilePath.ends_with(Suffix: ")")
287 ? sys::path::filename(path: bitcodeFilePath)
288 : sys::path::stem(path: bitcodeFilePath);
289 StringRef outputFileBaseName = sys::path::filename(path: ctx.arg.outputFile);
290 SmallString<256> path;
291 sys::path::append(path, a: directory,
292 b: outputFileBaseName + ".lto." + baseName + ".o");
293 sys::path::remove_dots(path, remove_dot_dot: true);
294 ltoObjName = saver().save(S: path.str());
295 }
296 if (ctx.arg.saveTemps)
297 saveBuffer(buffer: objBuf, path: ltoObjName);
298 ret.emplace_back(Args: createObjectFile(mb: MemoryBufferRef(objBuf, ltoObjName)));
299 }
300
301 if (!ctx.arg.ltoObjPath.empty()) {
302 saveBuffer(buffer: buf[0].second, path: ctx.arg.ltoObjPath);
303 for (unsigned i = 1; i != maxTasks; ++i)
304 saveBuffer(buffer: buf[i].second, path: ctx.arg.ltoObjPath + Twine(i));
305 }
306
307 return ret;
308}
309
310} // namespace lld::wasm
311