xref: /freebsd/contrib/llvm-project/lld/COFF/LTO.cpp (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
10b57cec5SDimitry Andric //===- LTO.cpp ------------------------------------------------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric 
90b57cec5SDimitry Andric #include "LTO.h"
10bdd1243dSDimitry Andric #include "COFFLinkerContext.h"
110b57cec5SDimitry Andric #include "Config.h"
120b57cec5SDimitry Andric #include "InputFiles.h"
130b57cec5SDimitry Andric #include "Symbols.h"
140b57cec5SDimitry Andric #include "lld/Common/Args.h"
1504eeddc0SDimitry Andric #include "lld/Common/CommonLinkerContext.h"
165f757f3fSDimitry Andric #include "lld/Common/Filesystem.h"
170b57cec5SDimitry Andric #include "lld/Common/Strings.h"
180b57cec5SDimitry Andric #include "lld/Common/TargetOptionsCommandFlags.h"
190b57cec5SDimitry Andric #include "llvm/ADT/STLExtras.h"
200b57cec5SDimitry Andric #include "llvm/ADT/SmallString.h"
210b57cec5SDimitry Andric #include "llvm/ADT/StringRef.h"
220b57cec5SDimitry Andric #include "llvm/ADT/Twine.h"
230b57cec5SDimitry Andric #include "llvm/Bitcode/BitcodeWriter.h"
240b57cec5SDimitry Andric #include "llvm/IR/DiagnosticPrinter.h"
250b57cec5SDimitry Andric #include "llvm/LTO/Config.h"
260b57cec5SDimitry Andric #include "llvm/LTO/LTO.h"
270b57cec5SDimitry Andric #include "llvm/Object/SymbolicFile.h"
28349cc55cSDimitry Andric #include "llvm/Support/Caching.h"
290b57cec5SDimitry Andric #include "llvm/Support/CodeGen.h"
300b57cec5SDimitry Andric #include "llvm/Support/Error.h"
310b57cec5SDimitry Andric #include "llvm/Support/FileSystem.h"
320b57cec5SDimitry Andric #include "llvm/Support/MemoryBuffer.h"
330b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h"
340b57cec5SDimitry Andric #include <algorithm>
350b57cec5SDimitry Andric #include <cstddef>
360b57cec5SDimitry Andric #include <memory>
370b57cec5SDimitry Andric #include <string>
380b57cec5SDimitry Andric #include <system_error>
390b57cec5SDimitry Andric #include <vector>
400b57cec5SDimitry Andric 
410b57cec5SDimitry Andric using namespace llvm;
420b57cec5SDimitry Andric using namespace llvm::object;
435ffd83dbSDimitry Andric using namespace lld;
445ffd83dbSDimitry Andric using namespace lld::coff;
450b57cec5SDimitry Andric 
getThinLTOOutputFile(StringRef path)46bdd1243dSDimitry Andric std::string BitcodeCompiler::getThinLTOOutputFile(StringRef path) {
4706c3fb27SDimitry Andric   return lto::getThinLTOOutputFile(path, ctx.config.thinLTOPrefixReplaceOld,
4806c3fb27SDimitry Andric                                    ctx.config.thinLTOPrefixReplaceNew);
490b57cec5SDimitry Andric }
500b57cec5SDimitry Andric 
createConfig()51bdd1243dSDimitry Andric lto::Config BitcodeCompiler::createConfig() {
520b57cec5SDimitry Andric   lto::Config c;
530b57cec5SDimitry Andric   c.Options = initTargetOptionsFromCodeGenFlags();
54fe6060f1SDimitry Andric   c.Options.EmitAddrsig = true;
55bdd1243dSDimitry Andric   for (StringRef C : ctx.config.mllvmOpts)
56bdd1243dSDimitry Andric     c.MllvmArgs.emplace_back(C.str());
570b57cec5SDimitry Andric 
580b57cec5SDimitry Andric   // Always emit a section per function/datum with LTO. LLVM LTO should get most
590b57cec5SDimitry Andric   // of the benefit of linker GC, but there are still opportunities for ICF.
600b57cec5SDimitry Andric   c.Options.FunctionSections = true;
610b57cec5SDimitry Andric   c.Options.DataSections = true;
620b57cec5SDimitry Andric 
630b57cec5SDimitry Andric   // Use static reloc model on 32-bit x86 because it usually results in more
640b57cec5SDimitry Andric   // compact code, and because there are also known code generation bugs when
650b57cec5SDimitry Andric   // using the PIC model (see PR34306).
66bdd1243dSDimitry Andric   if (ctx.config.machine == COFF::IMAGE_FILE_MACHINE_I386)
670b57cec5SDimitry Andric     c.RelocModel = Reloc::Static;
680b57cec5SDimitry Andric   else
690b57cec5SDimitry Andric     c.RelocModel = Reloc::PIC_;
70bdd1243dSDimitry Andric #ifndef NDEBUG
71bdd1243dSDimitry Andric   c.DisableVerify = false;
72bdd1243dSDimitry Andric #else
730b57cec5SDimitry Andric   c.DisableVerify = true;
74bdd1243dSDimitry Andric #endif
750b57cec5SDimitry Andric   c.DiagHandler = diagnosticHandler;
7606c3fb27SDimitry Andric   c.DwoDir = ctx.config.dwoDir.str();
77bdd1243dSDimitry Andric   c.OptLevel = ctx.config.ltoo;
780b57cec5SDimitry Andric   c.CPU = getCPUStr();
790b57cec5SDimitry Andric   c.MAttrs = getMAttrs();
805f757f3fSDimitry Andric   std::optional<CodeGenOptLevel> optLevelOrNone = CodeGenOpt::getLevel(
8106c3fb27SDimitry Andric       ctx.config.ltoCgo.value_or(args::getCGOptLevel(ctx.config.ltoo)));
8206c3fb27SDimitry Andric   assert(optLevelOrNone && "Invalid optimization level!");
8306c3fb27SDimitry Andric   c.CGOptLevel = *optLevelOrNone;
84bdd1243dSDimitry Andric   c.AlwaysEmitRegularLTOObj = !ctx.config.ltoObjPath.empty();
85bdd1243dSDimitry Andric   c.DebugPassManager = ctx.config.ltoDebugPassManager;
86bdd1243dSDimitry Andric   c.CSIRProfile = std::string(ctx.config.ltoCSProfileFile);
87bdd1243dSDimitry Andric   c.RunCSIRInstr = ctx.config.ltoCSProfileGenerate;
88bdd1243dSDimitry Andric   c.PGOWarnMismatch = ctx.config.ltoPGOWarnMismatch;
89*0fca6ea1SDimitry Andric   c.SampleProfile = ctx.config.ltoSampleProfileName;
905f757f3fSDimitry Andric   c.TimeTraceEnabled = ctx.config.timeTraceEnabled;
915f757f3fSDimitry Andric   c.TimeTraceGranularity = ctx.config.timeTraceGranularity;
925f757f3fSDimitry Andric 
935f757f3fSDimitry Andric   if (ctx.config.emit == EmitKind::LLVM) {
94*0fca6ea1SDimitry Andric     c.PreCodeGenModuleHook = [this](size_t task, const Module &m) {
955f757f3fSDimitry Andric       if (std::unique_ptr<raw_fd_ostream> os =
965f757f3fSDimitry Andric               openLTOOutputFile(ctx.config.outputFile))
975f757f3fSDimitry Andric         WriteBitcodeToFile(m, *os, false);
985f757f3fSDimitry Andric       return false;
995f757f3fSDimitry Andric     };
1005f757f3fSDimitry Andric   } else if (ctx.config.emit == EmitKind::ASM) {
1015f757f3fSDimitry Andric     c.CGFileType = CodeGenFileType::AssemblyFile;
1025f757f3fSDimitry Andric     c.Options.MCOptions.AsmVerbose = true;
1035f757f3fSDimitry Andric   }
1040b57cec5SDimitry Andric 
105bdd1243dSDimitry Andric   if (ctx.config.saveTemps)
106bdd1243dSDimitry Andric     checkError(c.addSaveTemps(std::string(ctx.config.outputFile) + ".",
1070b57cec5SDimitry Andric                               /*UseInputModulePath*/ true));
1080b57cec5SDimitry Andric   return c;
1090b57cec5SDimitry Andric }
1100b57cec5SDimitry Andric 
BitcodeCompiler(COFFLinkerContext & c)111bdd1243dSDimitry Andric BitcodeCompiler::BitcodeCompiler(COFFLinkerContext &c) : ctx(c) {
1120b57cec5SDimitry Andric   // Initialize indexFile.
113bdd1243dSDimitry Andric   if (!ctx.config.thinLTOIndexOnlyArg.empty())
114bdd1243dSDimitry Andric     indexFile = openFile(ctx.config.thinLTOIndexOnlyArg);
1150b57cec5SDimitry Andric 
1160b57cec5SDimitry Andric   // Initialize ltoObj.
1170b57cec5SDimitry Andric   lto::ThinBackend backend;
118bdd1243dSDimitry Andric   if (ctx.config.thinLTOIndexOnly) {
1190b57cec5SDimitry Andric     auto OnIndexWrite = [&](StringRef S) { thinIndices.erase(S); };
1200b57cec5SDimitry Andric     backend = lto::createWriteIndexesThinBackend(
12106c3fb27SDimitry Andric         std::string(ctx.config.thinLTOPrefixReplaceOld),
12206c3fb27SDimitry Andric         std::string(ctx.config.thinLTOPrefixReplaceNew),
12306c3fb27SDimitry Andric         std::string(ctx.config.thinLTOPrefixReplaceNativeObject),
124bdd1243dSDimitry Andric         ctx.config.thinLTOEmitImportsFiles, indexFile.get(), OnIndexWrite);
1255ffd83dbSDimitry Andric   } else {
1265ffd83dbSDimitry Andric     backend = lto::createInProcessThinBackend(
127bdd1243dSDimitry Andric         llvm::heavyweight_hardware_concurrency(ctx.config.thinLTOJobs));
1280b57cec5SDimitry Andric   }
1290b57cec5SDimitry Andric 
13085868e8aSDimitry Andric   ltoObj = std::make_unique<lto::LTO>(createConfig(), backend,
131bdd1243dSDimitry Andric                                       ctx.config.ltoPartitions);
1320b57cec5SDimitry Andric }
1330b57cec5SDimitry Andric 
1340b57cec5SDimitry Andric BitcodeCompiler::~BitcodeCompiler() = default;
1350b57cec5SDimitry Andric 
undefine(Symbol * s)1360b57cec5SDimitry Andric static void undefine(Symbol *s) { replaceSymbol<Undefined>(s, s->getName()); }
1370b57cec5SDimitry Andric 
add(BitcodeFile & f)1380b57cec5SDimitry Andric void BitcodeCompiler::add(BitcodeFile &f) {
1390b57cec5SDimitry Andric   lto::InputFile &obj = *f.obj;
1400b57cec5SDimitry Andric   unsigned symNum = 0;
1410b57cec5SDimitry Andric   std::vector<Symbol *> symBodies = f.getSymbols();
1420b57cec5SDimitry Andric   std::vector<lto::SymbolResolution> resols(symBodies.size());
1430b57cec5SDimitry Andric 
144bdd1243dSDimitry Andric   if (ctx.config.thinLTOIndexOnly)
1450b57cec5SDimitry Andric     thinIndices.insert(obj.getName());
1460b57cec5SDimitry Andric 
1470b57cec5SDimitry Andric   // Provide a resolution to the LTO API for each symbol.
1480b57cec5SDimitry Andric   for (const lto::InputFile::Symbol &objSym : obj.symbols()) {
1490b57cec5SDimitry Andric     Symbol *sym = symBodies[symNum];
1500b57cec5SDimitry Andric     lto::SymbolResolution &r = resols[symNum];
1510b57cec5SDimitry Andric     ++symNum;
1520b57cec5SDimitry Andric 
1530b57cec5SDimitry Andric     // Ideally we shouldn't check for SF_Undefined but currently IRObjectFile
1540b57cec5SDimitry Andric     // reports two symbols for module ASM defined. Without this check, lld
1550b57cec5SDimitry Andric     // flags an undefined in IR with a definition in ASM as prevailing.
1560b57cec5SDimitry Andric     // Once IRObjectFile is fixed to report only one symbol this hack can
1570b57cec5SDimitry Andric     // be removed.
1580b57cec5SDimitry Andric     r.Prevailing = !objSym.isUndefined() && sym->getFile() == &f;
1590b57cec5SDimitry Andric     r.VisibleToRegularObj = sym->isUsedInRegularObj;
1600b57cec5SDimitry Andric     if (r.Prevailing)
1610b57cec5SDimitry Andric       undefine(sym);
162e8d8bef9SDimitry Andric 
163e8d8bef9SDimitry Andric     // We tell LTO to not apply interprocedural optimization for wrapped
164e8d8bef9SDimitry Andric     // (with -wrap) symbols because otherwise LTO would inline them while
165e8d8bef9SDimitry Andric     // their values are still not final.
166e8d8bef9SDimitry Andric     r.LinkerRedefined = !sym->canInline;
1670b57cec5SDimitry Andric   }
1680b57cec5SDimitry Andric   checkError(ltoObj->add(std::move(f.obj), resols));
1690b57cec5SDimitry Andric }
1700b57cec5SDimitry Andric 
1710b57cec5SDimitry Andric // Merge all the bitcode files we have seen, codegen the result
1720b57cec5SDimitry Andric // and return the resulting objects.
compile()173bdd1243dSDimitry Andric std::vector<InputFile *> BitcodeCompiler::compile() {
1740b57cec5SDimitry Andric   unsigned maxTasks = ltoObj->getMaxTasks();
1750b57cec5SDimitry Andric   buf.resize(maxTasks);
1760b57cec5SDimitry Andric   files.resize(maxTasks);
177bdd1243dSDimitry Andric   file_names.resize(maxTasks);
1780b57cec5SDimitry Andric 
1790b57cec5SDimitry Andric   // The /lldltocache option specifies the path to a directory in which to cache
1800b57cec5SDimitry Andric   // native object files for ThinLTO incremental builds. If a path was
1810b57cec5SDimitry Andric   // specified, configure LTO to use it as the cache directory.
182349cc55cSDimitry Andric   FileCache cache;
183bdd1243dSDimitry Andric   if (!ctx.config.ltoCache.empty())
184bdd1243dSDimitry Andric     cache = check(localCache("ThinLTO", "Thin", ctx.config.ltoCache,
185bdd1243dSDimitry Andric                              [&](size_t task, const Twine &moduleName,
186bdd1243dSDimitry Andric                                  std::unique_ptr<MemoryBuffer> mb) {
1870b57cec5SDimitry Andric                                files[task] = std::move(mb);
188bdd1243dSDimitry Andric                                file_names[task] = moduleName.str();
1890b57cec5SDimitry Andric                              }));
1900b57cec5SDimitry Andric 
1910b57cec5SDimitry Andric   checkError(ltoObj->run(
192bdd1243dSDimitry Andric       [&](size_t task, const Twine &moduleName) {
193bdd1243dSDimitry Andric         buf[task].first = moduleName.str();
194349cc55cSDimitry Andric         return std::make_unique<CachedFileStream>(
195bdd1243dSDimitry Andric             std::make_unique<raw_svector_ostream>(buf[task].second));
1960b57cec5SDimitry Andric       },
1970b57cec5SDimitry Andric       cache));
1980b57cec5SDimitry Andric 
1990b57cec5SDimitry Andric   // Emit empty index files for non-indexed files
2000b57cec5SDimitry Andric   for (StringRef s : thinIndices) {
2010b57cec5SDimitry Andric     std::string path = getThinLTOOutputFile(s);
2020b57cec5SDimitry Andric     openFile(path + ".thinlto.bc");
203bdd1243dSDimitry Andric     if (ctx.config.thinLTOEmitImportsFiles)
2040b57cec5SDimitry Andric       openFile(path + ".imports");
2050b57cec5SDimitry Andric   }
2060b57cec5SDimitry Andric 
2070b57cec5SDimitry Andric   // ThinLTO with index only option is required to generate only the index
2080b57cec5SDimitry Andric   // files. After that, we exit from linker and ThinLTO backend runs in a
2090b57cec5SDimitry Andric   // distributed environment.
210bdd1243dSDimitry Andric   if (ctx.config.thinLTOIndexOnly) {
211bdd1243dSDimitry Andric     if (!ctx.config.ltoObjPath.empty())
212bdd1243dSDimitry Andric       saveBuffer(buf[0].second, ctx.config.ltoObjPath);
2130b57cec5SDimitry Andric     if (indexFile)
2140b57cec5SDimitry Andric       indexFile->close();
2150b57cec5SDimitry Andric     return {};
2160b57cec5SDimitry Andric   }
2170b57cec5SDimitry Andric 
218bdd1243dSDimitry Andric   if (!ctx.config.ltoCache.empty())
219bdd1243dSDimitry Andric     pruneCache(ctx.config.ltoCache, ctx.config.ltoCachePolicy, files);
2200b57cec5SDimitry Andric 
2215ffd83dbSDimitry Andric   std::vector<InputFile *> ret;
2225f757f3fSDimitry Andric   bool emitASM = ctx.config.emit == EmitKind::ASM;
2235f757f3fSDimitry Andric   const char *Ext = emitASM ? ".s" : ".obj";
2240b57cec5SDimitry Andric   for (unsigned i = 0; i != maxTasks; ++i) {
225bdd1243dSDimitry Andric     StringRef bitcodeFilePath;
2265ffd83dbSDimitry Andric     // Get the native object contents either from the cache or from memory.  Do
2275ffd83dbSDimitry Andric     // not use the cached MemoryBuffer directly, or the PDB will not be
2285ffd83dbSDimitry Andric     // deterministic.
2295ffd83dbSDimitry Andric     StringRef objBuf;
230bdd1243dSDimitry Andric     if (files[i]) {
2315ffd83dbSDimitry Andric       objBuf = files[i]->getBuffer();
232bdd1243dSDimitry Andric       bitcodeFilePath = file_names[i];
233bdd1243dSDimitry Andric     } else {
234bdd1243dSDimitry Andric       objBuf = buf[i].second;
235bdd1243dSDimitry Andric       bitcodeFilePath = buf[i].first;
236bdd1243dSDimitry Andric     }
2375ffd83dbSDimitry Andric     if (objBuf.empty())
2385ffd83dbSDimitry Andric       continue;
2395ffd83dbSDimitry Andric 
240bdd1243dSDimitry Andric     // If the input bitcode file is path/to/a.obj, then the corresponding lto
241bdd1243dSDimitry Andric     // object file name will look something like: path/to/main.exe.lto.a.obj.
242bdd1243dSDimitry Andric     StringRef ltoObjName;
243bdd1243dSDimitry Andric     if (bitcodeFilePath == "ld-temp.o") {
244bdd1243dSDimitry Andric       ltoObjName =
245bdd1243dSDimitry Andric           saver().save(Twine(ctx.config.outputFile) + ".lto" +
2465f757f3fSDimitry Andric                        (i == 0 ? Twine("") : Twine('.') + Twine(i)) + Ext);
247bdd1243dSDimitry Andric     } else {
248bdd1243dSDimitry Andric       StringRef directory = sys::path::parent_path(bitcodeFilePath);
2495f757f3fSDimitry Andric       StringRef baseName = sys::path::stem(bitcodeFilePath);
250bdd1243dSDimitry Andric       StringRef outputFileBaseName = sys::path::filename(ctx.config.outputFile);
251bdd1243dSDimitry Andric       SmallString<64> path;
252bdd1243dSDimitry Andric       sys::path::append(path, directory,
2535f757f3fSDimitry Andric                         outputFileBaseName + ".lto." + baseName + Ext);
254bdd1243dSDimitry Andric       sys::path::remove_dots(path, true);
255bdd1243dSDimitry Andric       ltoObjName = saver().save(path.str());
256bdd1243dSDimitry Andric     }
2575f757f3fSDimitry Andric     if (ctx.config.saveTemps || emitASM)
258bdd1243dSDimitry Andric       saveBuffer(buf[i].second, ltoObjName);
2595f757f3fSDimitry Andric     if (!emitASM)
260349cc55cSDimitry Andric       ret.push_back(make<ObjFile>(ctx, MemoryBufferRef(objBuf, ltoObjName)));
2615ffd83dbSDimitry Andric   }
2620b57cec5SDimitry Andric 
2630b57cec5SDimitry Andric   return ret;
2640b57cec5SDimitry Andric }
265