xref: /freebsd/contrib/llvm-project/lld/COFF/LTO.cpp (revision 9c77fb6aaa366cbabc80ee1b834bcfe4df135491)
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 
35 using namespace llvm;
36 using namespace llvm::object;
37 using namespace lld;
38 using namespace lld::coff;
39 
40 std::string BitcodeCompiler::getThinLTOOutputFile(StringRef path) {
41   return lto::getThinLTOOutputFile(path, ctx.config.thinLTOPrefixReplaceOld,
42                                    ctx.config.thinLTOPrefixReplaceNew);
43 }
44 
45 lto::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(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       ctx.config.ltoCgo.value_or(args::getCGOptLevel(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(ctx.config.outputFile))
91         WriteBitcodeToFile(m, *os, 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(c.addSaveTemps(std::string(ctx.config.outputFile) + ".",
101                               /*UseInputModulePath*/ true,
102                               ctx.config.saveTempsArgs));
103   return c;
104 }
105 
106 BitcodeCompiler::BitcodeCompiler(COFFLinkerContext &c) : ctx(c) {
107   // Initialize indexFile.
108   if (!ctx.config.thinLTOIndexOnlyArg.empty())
109     indexFile = openFile(ctx.config.thinLTOIndexOnlyArg);
110 
111   // Initialize ltoObj.
112   lto::ThinBackend backend;
113   if (!ctx.config.dtltoDistributor.empty()) {
114     backend = lto::createOutOfProcessThinBackend(
115         llvm::hardware_concurrency(ctx.config.thinLTOJobs),
116         /*OnWrite=*/nullptr,
117         /*ShouldEmitIndexFiles=*/false,
118         /*ShouldEmitImportFiles=*/false, ctx.config.outputFile,
119         ctx.config.dtltoDistributor, ctx.config.dtltoDistributorArgs,
120         ctx.config.dtltoCompiler, ctx.config.dtltoCompilerArgs,
121         !ctx.config.saveTempsArgs.empty());
122   } else if (ctx.config.thinLTOIndexOnly) {
123     auto OnIndexWrite = [&](StringRef S) { thinIndices.erase(S); };
124     backend = lto::createWriteIndexesThinBackend(
125         llvm::hardware_concurrency(ctx.config.thinLTOJobs),
126         std::string(ctx.config.thinLTOPrefixReplaceOld),
127         std::string(ctx.config.thinLTOPrefixReplaceNew),
128         std::string(ctx.config.thinLTOPrefixReplaceNativeObject),
129         ctx.config.thinLTOEmitImportsFiles, indexFile.get(), OnIndexWrite);
130   } else {
131     backend = lto::createInProcessThinBackend(
132         llvm::heavyweight_hardware_concurrency(ctx.config.thinLTOJobs));
133   }
134 
135   ltoObj = std::make_unique<lto::LTO>(createConfig(), backend,
136                                       ctx.config.ltoPartitions);
137 }
138 
139 BitcodeCompiler::~BitcodeCompiler() = default;
140 
141 static void undefine(Symbol *s) { replaceSymbol<Undefined>(s, s->getName()); }
142 
143 void 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(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(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(ltoObj->add(std::move(f.obj), resols));
174 }
175 
176 // Merge all the bitcode files we have seen, codegen the result
177 // and return the resulting objects.
178 std::vector<InputFile *> BitcodeCompiler::compile() {
179   unsigned maxTasks = ltoObj->getMaxTasks();
180   buf.resize(maxTasks);
181   files.resize(maxTasks);
182   file_names.resize(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(localCache("ThinLTO", "Thin", ctx.config.ltoCache,
190                              [&](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(ltoObj->run(
197       [&](size_t task, const Twine &moduleName) {
198         buf[task].first = moduleName.str();
199         return std::make_unique<CachedFileStream>(
200             std::make_unique<raw_svector_ostream>(buf[task].second));
201       },
202       cache));
203 
204   // Emit empty index files for non-indexed files
205   for (StringRef s : thinIndices) {
206     std::string path = getThinLTOOutputFile(s);
207     openFile(path + ".thinlto.bc");
208     if (ctx.config.thinLTOEmitImportsFiles)
209       openFile(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(buf[0].second, ctx.config.ltoObjPath);
218     if (indexFile)
219       indexFile->close();
220     return {};
221   }
222 
223   if (!ctx.config.ltoCache.empty())
224     pruneCache(ctx.config.ltoCache, ctx.config.ltoCachePolicy, 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(Twine(ctx.config.outputFile) + ".lto" +
251                        (i == 0 ? Twine("") : Twine('.') + Twine(i)) + Ext);
252     } else {
253       StringRef directory = sys::path::parent_path(bitcodeFilePath);
254       StringRef baseName = sys::path::stem(bitcodeFilePath);
255       StringRef outputFileBaseName = sys::path::filename(ctx.config.outputFile);
256       SmallString<64> path;
257       sys::path::append(path, directory,
258                         outputFileBaseName + ".lto." + baseName + Ext);
259       sys::path::remove_dots(path, true);
260       ltoObjName = saver().save(path.str());
261     }
262     if (llvm::is_contained(ctx.config.saveTempsArgs, "prelink") || emitASM)
263       saveBuffer(buf[i].second, ltoObjName);
264     if (!emitASM)
265       ret.push_back(ObjFile::create(ctx, MemoryBufferRef(objBuf, ltoObjName)));
266   }
267 
268   return ret;
269 }
270