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 "COFFLinkerContext.h"
11#include "Config.h"
12#include "InputFiles.h"
13#include "Symbols.h"
14#include "lld/Common/Args.h"
15#include "lld/Common/CommonLinkerContext.h"
16#include "lld/Common/Filesystem.h"
17#include "lld/Common/Strings.h"
18#include "lld/Common/TargetOptionsCommandFlags.h"
19#include "llvm/ADT/STLExtras.h"
20#include "llvm/ADT/StringRef.h"
21#include "llvm/ADT/Twine.h"
22#include "llvm/Bitcode/BitcodeWriter.h"
23#include "llvm/DTLTO/DTLTO.h"
24#include "llvm/IR/DiagnosticPrinter.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/MemoryBuffer.h"
30#include "llvm/Support/TimeProfiler.h"
31#include "llvm/Support/raw_ostream.h"
32#include <cstddef>
33#include <memory>
34#include <string>
35#include <vector>
36
37using namespace llvm;
38using namespace llvm::object;
39using namespace lld;
40using namespace lld::coff;
41
42static AddBufferFn
43createAddBufferFn(std::vector<std::unique_ptr<MemoryBuffer>> &files,
44 std::vector<std::string> &filenames) {
45 return [&files, &filenames](unsigned task, const Twine &moduleName,
46 std::unique_ptr<MemoryBuffer> mb) {
47 files[task] = std::move(mb);
48 filenames[task] = moduleName.str();
49 };
50}
51
52std::string BitcodeCompiler::getThinLTOOutputFile(StringRef path) {
53 return lto::getThinLTOOutputFile(Path: path, OldPrefix: ctx.config.thinLTOPrefixReplaceOld,
54 NewPrefix: ctx.config.thinLTOPrefixReplaceNew);
55}
56
57lto::Config BitcodeCompiler::createConfig() {
58 lto::Config c;
59 c.Options = initTargetOptionsFromCodeGenFlags();
60 c.Options.EmitAddrsig = true;
61 for (StringRef C : ctx.config.mllvmOpts)
62 c.MllvmArgs.emplace_back(args: C.str());
63
64 // Always emit a section per function/datum with LTO. LLVM LTO should get most
65 // of the benefit of linker GC, but there are still opportunities for ICF.
66 c.Options.FunctionSections = true;
67 c.Options.DataSections = true;
68
69 // Use static reloc model on 32-bit x86 because it usually results in more
70 // compact code, and because there are also known code generation bugs when
71 // using the PIC model (see PR34306).
72 if (ctx.config.machine == COFF::IMAGE_FILE_MACHINE_I386)
73 c.RelocModel = Reloc::Static;
74 else
75 c.RelocModel = Reloc::PIC_;
76#ifndef NDEBUG
77 c.DisableVerify = false;
78#else
79 c.DisableVerify = true;
80#endif
81 c.DiagHandler = diagnosticHandler;
82 c.DwoDir = ctx.config.dwoDir.str();
83 c.OptLevel = ctx.config.ltoo;
84 c.CPU = getCPUStr();
85 c.MAttrs = getMAttrs();
86 std::optional<CodeGenOptLevel> optLevelOrNone = CodeGenOpt::getLevel(
87 OL: ctx.config.ltoCgo.value_or(u: args::getCGOptLevel(optLevelLTO: ctx.config.ltoo)));
88 assert(optLevelOrNone && "Invalid optimization level!");
89 c.CGOptLevel = *optLevelOrNone;
90 c.AlwaysEmitRegularLTOObj = !ctx.config.ltoObjPath.empty();
91 c.DebugPassManager = ctx.config.ltoDebugPassManager;
92 c.CSIRProfile = std::string(ctx.config.ltoCSProfileFile);
93 c.RunCSIRInstr = ctx.config.ltoCSProfileGenerate;
94 c.PGOWarnMismatch = ctx.config.ltoPGOWarnMismatch;
95 c.SampleProfile = ctx.config.ltoSampleProfileName;
96 c.TimeTraceEnabled = ctx.config.timeTraceEnabled;
97 c.TimeTraceGranularity = ctx.config.timeTraceGranularity;
98
99 if (ctx.config.emit == EmitKind::LLVM) {
100 c.PreCodeGenModuleHook = [this](size_t task, const Module &m) {
101 if (std::unique_ptr<raw_fd_ostream> os =
102 openLTOOutputFile(file: ctx.config.outputFile))
103 WriteBitcodeToFile(M: m, Out&: *os, ShouldPreserveUseListOrder: false);
104 return false;
105 };
106 } else if (ctx.config.emit == EmitKind::ASM) {
107 c.CGFileType = CodeGenFileType::AssemblyFile;
108 c.Options.MCOptions.AsmVerbose = true;
109 }
110
111 if (!ctx.config.saveTempsArgs.empty())
112 checkError(e: c.addSaveTemps(OutputFileName: std::string(ctx.config.outputFile) + ".",
113 /*UseInputModulePath*/ true,
114 SaveTempsArgs: ctx.config.saveTempsArgs));
115
116 c.PTO.LoopVectorization = c.OptLevel > 1;
117 c.PTO.SLPVectorization = c.OptLevel > 1;
118
119 return c;
120}
121
122BitcodeCompiler::BitcodeCompiler(COFFLinkerContext &c) : ctx(c) {
123 // Initialize indexFile.
124 if (!ctx.config.thinLTOIndexOnlyArg.empty())
125 indexFile = openFile(file: ctx.config.thinLTOIndexOnlyArg);
126
127 // Initialize ltoObj.
128 lto::ThinBackend backend;
129 if (!ctx.config.dtltoDistributor.empty()) {
130 backend = lto::createOutOfProcessThinBackend(
131 Parallelism: llvm::hardware_concurrency(Num: ctx.config.thinLTOJobs),
132 /*OnWrite=*/nullptr,
133 /*ShouldEmitIndexFiles=*/false,
134 /*ShouldEmitImportFiles=*/ShouldEmitImportsFiles: false, LinkerOutputFile: ctx.config.outputFile,
135 Distributor: ctx.config.dtltoDistributor, DistributorArgs: ctx.config.dtltoDistributorArgs,
136 RemoteCompiler: ctx.config.dtltoCompiler, RemoteCompilerPrependArgs: ctx.config.dtltoCompilerPrependArgs,
137 RemoteCompilerArgs: ctx.config.dtltoCompilerArgs, SaveTemps: !ctx.config.saveTempsArgs.empty(),
138 AddBuffer: createAddBufferFn(files, filenames&: file_names));
139 } else if (ctx.config.thinLTOIndexOnly) {
140 auto OnIndexWrite = [&](StringRef S) { thinIndices.erase(V: S); };
141 backend = lto::createWriteIndexesThinBackend(
142 Parallelism: llvm::hardware_concurrency(Num: ctx.config.thinLTOJobs),
143 OldPrefix: std::string(ctx.config.thinLTOPrefixReplaceOld),
144 NewPrefix: std::string(ctx.config.thinLTOPrefixReplaceNew),
145 NativeObjectPrefix: std::string(ctx.config.thinLTOPrefixReplaceNativeObject),
146 ShouldEmitImportsFiles: ctx.config.thinLTOEmitImportsFiles, LinkedObjectsFile: indexFile.get(), OnWrite: OnIndexWrite);
147 } else {
148 backend = lto::createInProcessThinBackend(
149 Parallelism: llvm::heavyweight_hardware_concurrency(Num: ctx.config.thinLTOJobs));
150 }
151
152 if (ctx.config.dtltoDistributor.empty())
153 ltoObj = std::make_unique<lto::LTO>(args: createConfig(), args&: backend,
154 args&: ctx.config.ltoPartitions);
155 else
156 ltoObj = std::make_unique<lto::DTLTO>(
157 args: createConfig(), args&: backend, args&: ctx.config.ltoPartitions,
158 args: llvm::lto::LTO::LTOKind::LTOK_Default, args&: ctx.config.outputFile,
159 args: !ctx.config.saveTempsArgs.empty());
160}
161
162BitcodeCompiler::~BitcodeCompiler() = default;
163
164static void undefine(Symbol *s) { replaceSymbol<Undefined>(s, arg: s->getName()); }
165
166void BitcodeCompiler::add(BitcodeFile &f) {
167 lto::InputFile &obj = *f.obj;
168 unsigned symNum = 0;
169 std::vector<Symbol *> symBodies = f.getSymbols();
170 std::vector<lto::SymbolResolution> resols(symBodies.size());
171
172 if (ctx.config.thinLTOIndexOnly)
173 thinIndices.insert(V: obj.getName());
174
175 // Provide a resolution to the LTO API for each symbol.
176 for (const lto::InputFile::Symbol &objSym : obj.symbols()) {
177 Symbol *sym = symBodies[symNum];
178 lto::SymbolResolution &r = resols[symNum];
179 ++symNum;
180
181 // Ideally we shouldn't check for SF_Undefined but currently IRObjectFile
182 // reports two symbols for module ASM defined. Without this check, lld
183 // flags an undefined in IR with a definition in ASM as prevailing.
184 // Once IRObjectFile is fixed to report only one symbol this hack can
185 // be removed.
186 r.Prevailing = !objSym.isUndefined() && sym->getFile() == &f;
187 r.VisibleToRegularObj = sym->isUsedInRegularObj;
188 if (r.Prevailing)
189 undefine(s: sym);
190
191 // We tell LTO to not apply interprocedural optimization for wrapped
192 // (with -wrap) symbols because otherwise LTO would inline them while
193 // their values are still not final.
194 r.LinkerRedefined = !sym->canInline;
195 }
196 checkError(e: ltoObj->add(Obj: std::move(f.obj), Res: resols));
197}
198
199// Merge all the bitcode files we have seen, codegen the result
200// and return the resulting objects.
201std::vector<InputFile *> BitcodeCompiler::compile() {
202 llvm::TimeTraceScope timeScope("Bitcode compile");
203 unsigned maxTasks = ltoObj->getMaxTasks();
204 buf.resize(new_size: maxTasks);
205 files.resize(new_size: maxTasks);
206 file_names.resize(new_size: maxTasks);
207
208 // The /lldltocache option specifies the path to a directory in which to cache
209 // native object files for ThinLTO incremental builds. If a path was
210 // specified, configure LTO to use it as the cache directory.
211 FileCache cache;
212 if (!ctx.config.ltoCache.empty())
213 cache = check(e: localCache(CacheNameRef: "ThinLTO", TempFilePrefixRef: "Thin", CacheDirectoryPathRef: ctx.config.ltoCache,
214 AddBuffer: createAddBufferFn(files, filenames&: file_names)));
215
216 checkError(e: ltoObj->run(
217 AddStream: [&](size_t task, const Twine &moduleName) {
218 buf[task].first = moduleName.str();
219 return std::make_unique<CachedFileStream>(
220 args: std::make_unique<raw_svector_ostream>(args&: buf[task].second));
221 },
222 Cache: cache));
223
224 // Emit empty index files for non-indexed files
225 for (StringRef s : thinIndices) {
226 std::string path = getThinLTOOutputFile(path: s);
227 openFile(file: path + ".thinlto.bc");
228 if (ctx.config.thinLTOEmitImportsFiles)
229 openFile(file: path + ".imports");
230 }
231
232 // ThinLTO with index only option is required to generate only the index
233 // files. After that, we exit from linker and ThinLTO backend runs in a
234 // distributed environment.
235 if (ctx.config.thinLTOIndexOnly) {
236 if (!ctx.config.ltoObjPath.empty())
237 saveBuffer(buffer: buf[0].second, path: ctx.config.ltoObjPath);
238 if (indexFile)
239 indexFile->close();
240 return {};
241 }
242
243 if (!ctx.config.ltoCache.empty())
244 pruneCache(Path: ctx.config.ltoCache, Policy: ctx.config.ltoCachePolicy, Files: files);
245
246 std::vector<InputFile *> ret;
247 bool emitASM = ctx.config.emit == EmitKind::ASM;
248 const char *Ext = emitASM ? ".s" : ".obj";
249 for (unsigned i = 0; i != maxTasks; ++i) {
250 StringRef bitcodeFilePath;
251 // Get the native object contents either from a MemoryBuffer, for example
252 // from the cache or an external DTLTO backend compilation, or by reading
253 // from memory. Do not use the provided MemoryBuffer directly, or the PDB
254 // will not be deterministic.
255 StringRef objBuf;
256 if (files[i]) {
257 objBuf = files[i]->getBuffer();
258 bitcodeFilePath = file_names[i];
259 } else {
260 objBuf = buf[i].second;
261 bitcodeFilePath = buf[i].first;
262 }
263 if (objBuf.empty())
264 continue;
265
266 // If the input bitcode file is path/to/a.obj, then the corresponding lto
267 // object file name will look something like: path/to/main.exe.lto.a.obj.
268 StringRef ltoObjName;
269 if (bitcodeFilePath == "ld-temp.o") {
270 ltoObjName =
271 saver().save(S: Twine(ctx.config.outputFile) + ".lto" +
272 (i == 0 ? Twine("") : Twine('.') + Twine(i)) + Ext);
273 } else {
274 StringRef directory = sys::path::parent_path(path: bitcodeFilePath);
275 StringRef baseName = sys::path::stem(path: bitcodeFilePath);
276 StringRef outputFileBaseName = sys::path::filename(path: ctx.config.outputFile);
277 SmallString<64> path;
278 sys::path::append(path, a: directory,
279 b: outputFileBaseName + ".lto." + baseName + Ext);
280 sys::path::remove_dots(path, remove_dot_dot: true);
281 ltoObjName = saver().save(S: path.str());
282 }
283 if (llvm::is_contained(Range&: ctx.config.saveTempsArgs, Element: "prelink") || emitASM)
284 saveBuffer(buffer: buf[i].second, path: ltoObjName);
285 if (!emitASM)
286 ret.push_back(x: ObjFile::create(ctx, mb: MemoryBufferRef(objBuf, ltoObjName)));
287 }
288
289 return ret;
290}
291