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

source code of lld/COFF/LTO.cpp