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