xref: /freebsd/contrib/llvm-project/lld/COFF/LTO.cpp (revision 04eeddc0aa8e0a417a16eaf9d7d095207f4a8623)
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"
100b57cec5SDimitry Andric #include "Config.h"
110b57cec5SDimitry Andric #include "InputFiles.h"
120b57cec5SDimitry Andric #include "Symbols.h"
130b57cec5SDimitry Andric #include "lld/Common/Args.h"
14*04eeddc0SDimitry Andric #include "lld/Common/CommonLinkerContext.h"
150b57cec5SDimitry Andric #include "lld/Common/Strings.h"
160b57cec5SDimitry Andric #include "lld/Common/TargetOptionsCommandFlags.h"
170b57cec5SDimitry Andric #include "llvm/ADT/STLExtras.h"
180b57cec5SDimitry Andric #include "llvm/ADT/SmallString.h"
190b57cec5SDimitry Andric #include "llvm/ADT/StringRef.h"
200b57cec5SDimitry Andric #include "llvm/ADT/Twine.h"
210b57cec5SDimitry Andric #include "llvm/Bitcode/BitcodeWriter.h"
220b57cec5SDimitry Andric #include "llvm/IR/DiagnosticPrinter.h"
230b57cec5SDimitry Andric #include "llvm/LTO/Config.h"
240b57cec5SDimitry Andric #include "llvm/LTO/LTO.h"
250b57cec5SDimitry Andric #include "llvm/Object/SymbolicFile.h"
26349cc55cSDimitry Andric #include "llvm/Support/Caching.h"
270b57cec5SDimitry Andric #include "llvm/Support/CodeGen.h"
280b57cec5SDimitry Andric #include "llvm/Support/Error.h"
290b57cec5SDimitry Andric #include "llvm/Support/FileSystem.h"
300b57cec5SDimitry Andric #include "llvm/Support/MemoryBuffer.h"
310b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h"
320b57cec5SDimitry Andric #include <algorithm>
330b57cec5SDimitry Andric #include <cstddef>
340b57cec5SDimitry Andric #include <memory>
350b57cec5SDimitry Andric #include <string>
360b57cec5SDimitry Andric #include <system_error>
370b57cec5SDimitry Andric #include <vector>
380b57cec5SDimitry Andric 
390b57cec5SDimitry Andric using namespace llvm;
400b57cec5SDimitry Andric using namespace llvm::object;
415ffd83dbSDimitry Andric using namespace lld;
425ffd83dbSDimitry Andric using namespace lld::coff;
430b57cec5SDimitry Andric 
440b57cec5SDimitry Andric // Creates an empty file to and returns a raw_fd_ostream to write to it.
450b57cec5SDimitry Andric static std::unique_ptr<raw_fd_ostream> openFile(StringRef file) {
460b57cec5SDimitry Andric   std::error_code ec;
470b57cec5SDimitry Andric   auto ret =
4885868e8aSDimitry Andric       std::make_unique<raw_fd_ostream>(file, ec, sys::fs::OpenFlags::OF_None);
490b57cec5SDimitry Andric   if (ec) {
500b57cec5SDimitry Andric     error("cannot open " + file + ": " + ec.message());
510b57cec5SDimitry Andric     return nullptr;
520b57cec5SDimitry Andric   }
530b57cec5SDimitry Andric   return ret;
540b57cec5SDimitry Andric }
550b57cec5SDimitry Andric 
560b57cec5SDimitry Andric static std::string getThinLTOOutputFile(StringRef path) {
575ffd83dbSDimitry Andric   return lto::getThinLTOOutputFile(
585ffd83dbSDimitry Andric       std::string(path), std::string(config->thinLTOPrefixReplace.first),
595ffd83dbSDimitry Andric       std::string(config->thinLTOPrefixReplace.second));
600b57cec5SDimitry Andric }
610b57cec5SDimitry Andric 
620b57cec5SDimitry Andric static lto::Config createConfig() {
630b57cec5SDimitry Andric   lto::Config c;
640b57cec5SDimitry Andric   c.Options = initTargetOptionsFromCodeGenFlags();
65fe6060f1SDimitry Andric   c.Options.EmitAddrsig = true;
660b57cec5SDimitry Andric 
670b57cec5SDimitry Andric   // Always emit a section per function/datum with LTO. LLVM LTO should get most
680b57cec5SDimitry Andric   // of the benefit of linker GC, but there are still opportunities for ICF.
690b57cec5SDimitry Andric   c.Options.FunctionSections = true;
700b57cec5SDimitry Andric   c.Options.DataSections = true;
710b57cec5SDimitry Andric 
720b57cec5SDimitry Andric   // Use static reloc model on 32-bit x86 because it usually results in more
730b57cec5SDimitry Andric   // compact code, and because there are also known code generation bugs when
740b57cec5SDimitry Andric   // using the PIC model (see PR34306).
750b57cec5SDimitry Andric   if (config->machine == COFF::IMAGE_FILE_MACHINE_I386)
760b57cec5SDimitry Andric     c.RelocModel = Reloc::Static;
770b57cec5SDimitry Andric   else
780b57cec5SDimitry Andric     c.RelocModel = Reloc::PIC_;
790b57cec5SDimitry Andric   c.DisableVerify = true;
800b57cec5SDimitry Andric   c.DiagHandler = diagnosticHandler;
810b57cec5SDimitry Andric   c.OptLevel = config->ltoo;
820b57cec5SDimitry Andric   c.CPU = getCPUStr();
830b57cec5SDimitry Andric   c.MAttrs = getMAttrs();
840b57cec5SDimitry Andric   c.CGOptLevel = args::getCGOptLevel(config->ltoo);
855ffd83dbSDimitry Andric   c.AlwaysEmitRegularLTOObj = !config->ltoObjPath.empty();
86e8d8bef9SDimitry Andric   c.UseNewPM = config->ltoNewPassManager;
87e8d8bef9SDimitry Andric   c.DebugPassManager = config->ltoDebugPassManager;
88fe6060f1SDimitry Andric   c.CSIRProfile = std::string(config->ltoCSProfileFile);
89fe6060f1SDimitry Andric   c.RunCSIRInstr = config->ltoCSProfileGenerate;
90349cc55cSDimitry Andric   c.PGOWarnMismatch = config->ltoPGOWarnMismatch;
910b57cec5SDimitry Andric 
920b57cec5SDimitry Andric   if (config->saveTemps)
930b57cec5SDimitry Andric     checkError(c.addSaveTemps(std::string(config->outputFile) + ".",
940b57cec5SDimitry Andric                               /*UseInputModulePath*/ true));
950b57cec5SDimitry Andric   return c;
960b57cec5SDimitry Andric }
970b57cec5SDimitry Andric 
980b57cec5SDimitry Andric BitcodeCompiler::BitcodeCompiler() {
990b57cec5SDimitry Andric   // Initialize indexFile.
1000b57cec5SDimitry Andric   if (!config->thinLTOIndexOnlyArg.empty())
1010b57cec5SDimitry Andric     indexFile = openFile(config->thinLTOIndexOnlyArg);
1020b57cec5SDimitry Andric 
1030b57cec5SDimitry Andric   // Initialize ltoObj.
1040b57cec5SDimitry Andric   lto::ThinBackend backend;
1050b57cec5SDimitry Andric   if (config->thinLTOIndexOnly) {
1060b57cec5SDimitry Andric     auto OnIndexWrite = [&](StringRef S) { thinIndices.erase(S); };
1070b57cec5SDimitry Andric     backend = lto::createWriteIndexesThinBackend(
1085ffd83dbSDimitry Andric         std::string(config->thinLTOPrefixReplace.first),
1095ffd83dbSDimitry Andric         std::string(config->thinLTOPrefixReplace.second),
1100b57cec5SDimitry Andric         config->thinLTOEmitImportsFiles, indexFile.get(), OnIndexWrite);
1115ffd83dbSDimitry Andric   } else {
1125ffd83dbSDimitry Andric     backend = lto::createInProcessThinBackend(
1135ffd83dbSDimitry Andric         llvm::heavyweight_hardware_concurrency(config->thinLTOJobs));
1140b57cec5SDimitry Andric   }
1150b57cec5SDimitry Andric 
11685868e8aSDimitry Andric   ltoObj = std::make_unique<lto::LTO>(createConfig(), backend,
1170b57cec5SDimitry Andric                                        config->ltoPartitions);
1180b57cec5SDimitry Andric }
1190b57cec5SDimitry Andric 
1200b57cec5SDimitry Andric BitcodeCompiler::~BitcodeCompiler() = default;
1210b57cec5SDimitry Andric 
1220b57cec5SDimitry Andric static void undefine(Symbol *s) { replaceSymbol<Undefined>(s, s->getName()); }
1230b57cec5SDimitry Andric 
1240b57cec5SDimitry Andric void BitcodeCompiler::add(BitcodeFile &f) {
1250b57cec5SDimitry Andric   lto::InputFile &obj = *f.obj;
1260b57cec5SDimitry Andric   unsigned symNum = 0;
1270b57cec5SDimitry Andric   std::vector<Symbol *> symBodies = f.getSymbols();
1280b57cec5SDimitry Andric   std::vector<lto::SymbolResolution> resols(symBodies.size());
1290b57cec5SDimitry Andric 
1300b57cec5SDimitry Andric   if (config->thinLTOIndexOnly)
1310b57cec5SDimitry Andric     thinIndices.insert(obj.getName());
1320b57cec5SDimitry Andric 
1330b57cec5SDimitry Andric   // Provide a resolution to the LTO API for each symbol.
1340b57cec5SDimitry Andric   for (const lto::InputFile::Symbol &objSym : obj.symbols()) {
1350b57cec5SDimitry Andric     Symbol *sym = symBodies[symNum];
1360b57cec5SDimitry Andric     lto::SymbolResolution &r = resols[symNum];
1370b57cec5SDimitry Andric     ++symNum;
1380b57cec5SDimitry Andric 
1390b57cec5SDimitry Andric     // Ideally we shouldn't check for SF_Undefined but currently IRObjectFile
1400b57cec5SDimitry Andric     // reports two symbols for module ASM defined. Without this check, lld
1410b57cec5SDimitry Andric     // flags an undefined in IR with a definition in ASM as prevailing.
1420b57cec5SDimitry Andric     // Once IRObjectFile is fixed to report only one symbol this hack can
1430b57cec5SDimitry Andric     // be removed.
1440b57cec5SDimitry Andric     r.Prevailing = !objSym.isUndefined() && sym->getFile() == &f;
1450b57cec5SDimitry Andric     r.VisibleToRegularObj = sym->isUsedInRegularObj;
1460b57cec5SDimitry Andric     if (r.Prevailing)
1470b57cec5SDimitry Andric       undefine(sym);
148e8d8bef9SDimitry Andric 
149e8d8bef9SDimitry Andric     // We tell LTO to not apply interprocedural optimization for wrapped
150e8d8bef9SDimitry Andric     // (with -wrap) symbols because otherwise LTO would inline them while
151e8d8bef9SDimitry Andric     // their values are still not final.
152e8d8bef9SDimitry Andric     r.LinkerRedefined = !sym->canInline;
1530b57cec5SDimitry Andric   }
1540b57cec5SDimitry Andric   checkError(ltoObj->add(std::move(f.obj), resols));
1550b57cec5SDimitry Andric }
1560b57cec5SDimitry Andric 
1570b57cec5SDimitry Andric // Merge all the bitcode files we have seen, codegen the result
1580b57cec5SDimitry Andric // and return the resulting objects.
159349cc55cSDimitry Andric std::vector<InputFile *> BitcodeCompiler::compile(COFFLinkerContext &ctx) {
1600b57cec5SDimitry Andric   unsigned maxTasks = ltoObj->getMaxTasks();
1610b57cec5SDimitry Andric   buf.resize(maxTasks);
1620b57cec5SDimitry Andric   files.resize(maxTasks);
1630b57cec5SDimitry Andric 
1640b57cec5SDimitry Andric   // The /lldltocache option specifies the path to a directory in which to cache
1650b57cec5SDimitry Andric   // native object files for ThinLTO incremental builds. If a path was
1660b57cec5SDimitry Andric   // specified, configure LTO to use it as the cache directory.
167349cc55cSDimitry Andric   FileCache cache;
1680b57cec5SDimitry Andric   if (!config->ltoCache.empty())
169349cc55cSDimitry Andric     cache =
170349cc55cSDimitry Andric         check(localCache("ThinLTO", "Thin", config->ltoCache,
171349cc55cSDimitry Andric                          [&](size_t task, std::unique_ptr<MemoryBuffer> mb) {
1720b57cec5SDimitry Andric                            files[task] = std::move(mb);
1730b57cec5SDimitry Andric                          }));
1740b57cec5SDimitry Andric 
1750b57cec5SDimitry Andric   checkError(ltoObj->run(
1760b57cec5SDimitry Andric       [&](size_t task) {
177349cc55cSDimitry Andric         return std::make_unique<CachedFileStream>(
17885868e8aSDimitry Andric             std::make_unique<raw_svector_ostream>(buf[task]));
1790b57cec5SDimitry Andric       },
1800b57cec5SDimitry Andric       cache));
1810b57cec5SDimitry Andric 
1820b57cec5SDimitry Andric   // Emit empty index files for non-indexed files
1830b57cec5SDimitry Andric   for (StringRef s : thinIndices) {
1840b57cec5SDimitry Andric     std::string path = getThinLTOOutputFile(s);
1850b57cec5SDimitry Andric     openFile(path + ".thinlto.bc");
1860b57cec5SDimitry Andric     if (config->thinLTOEmitImportsFiles)
1870b57cec5SDimitry Andric       openFile(path + ".imports");
1880b57cec5SDimitry Andric   }
1890b57cec5SDimitry Andric 
1900b57cec5SDimitry Andric   // ThinLTO with index only option is required to generate only the index
1910b57cec5SDimitry Andric   // files. After that, we exit from linker and ThinLTO backend runs in a
1920b57cec5SDimitry Andric   // distributed environment.
1930b57cec5SDimitry Andric   if (config->thinLTOIndexOnly) {
19485868e8aSDimitry Andric     if (!config->ltoObjPath.empty())
19585868e8aSDimitry Andric       saveBuffer(buf[0], config->ltoObjPath);
1960b57cec5SDimitry Andric     if (indexFile)
1970b57cec5SDimitry Andric       indexFile->close();
1980b57cec5SDimitry Andric     return {};
1990b57cec5SDimitry Andric   }
2000b57cec5SDimitry Andric 
2010b57cec5SDimitry Andric   if (!config->ltoCache.empty())
2020b57cec5SDimitry Andric     pruneCache(config->ltoCache, config->ltoCachePolicy);
2030b57cec5SDimitry Andric 
2045ffd83dbSDimitry Andric   std::vector<InputFile *> ret;
2050b57cec5SDimitry Andric   for (unsigned i = 0; i != maxTasks; ++i) {
2065ffd83dbSDimitry Andric     // Assign unique names to LTO objects. This ensures they have unique names
2075ffd83dbSDimitry Andric     // in the PDB if one is produced. The names should look like:
2085ffd83dbSDimitry Andric     // - foo.exe.lto.obj
2095ffd83dbSDimitry Andric     // - foo.exe.lto.1.obj
2105ffd83dbSDimitry Andric     // - ...
2115ffd83dbSDimitry Andric     StringRef ltoObjName =
212*04eeddc0SDimitry Andric         saver().save(Twine(config->outputFile) + ".lto" +
2135ffd83dbSDimitry Andric                      (i == 0 ? Twine("") : Twine('.') + Twine(i)) + ".obj");
2140b57cec5SDimitry Andric 
2155ffd83dbSDimitry Andric     // Get the native object contents either from the cache or from memory.  Do
2165ffd83dbSDimitry Andric     // not use the cached MemoryBuffer directly, or the PDB will not be
2175ffd83dbSDimitry Andric     // deterministic.
2185ffd83dbSDimitry Andric     StringRef objBuf;
2195ffd83dbSDimitry Andric     if (files[i])
2205ffd83dbSDimitry Andric       objBuf = files[i]->getBuffer();
2215ffd83dbSDimitry Andric     else
2225ffd83dbSDimitry Andric       objBuf = buf[i];
2235ffd83dbSDimitry Andric     if (objBuf.empty())
2245ffd83dbSDimitry Andric       continue;
2255ffd83dbSDimitry Andric 
2265ffd83dbSDimitry Andric     if (config->saveTemps)
2275ffd83dbSDimitry Andric       saveBuffer(buf[i], ltoObjName);
228349cc55cSDimitry Andric     ret.push_back(make<ObjFile>(ctx, MemoryBufferRef(objBuf, ltoObjName)));
2295ffd83dbSDimitry Andric   }
2300b57cec5SDimitry Andric 
2310b57cec5SDimitry Andric   return ret;
2320b57cec5SDimitry Andric }
233