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

source code of lld/COFF/LTO.cpp