1 //===- LTO.cpp ------------------------------------------------------------===//
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
12 #include "InputFiles.h"
14 #include "lld/Common/ErrorHandler.h"
15 #include "lld/Common/Strings.h"
16 #include "lld/Common/TargetOptionsCommandFlags.h"
17 #include "llvm/ADT/STLExtras.h"
18 #include "llvm/ADT/SmallString.h"
19 #include "llvm/ADT/StringRef.h"
20 #include "llvm/ADT/Twine.h"
21 #include "llvm/IR/DiagnosticPrinter.h"
22 #include "llvm/LTO/Caching.h"
23 #include "llvm/LTO/Config.h"
24 #include "llvm/LTO/LTO.h"
25 #include "llvm/Object/SymbolicFile.h"
26 #include "llvm/Support/CodeGen.h"
27 #include "llvm/Support/Error.h"
28 #include "llvm/Support/FileSystem.h"
29 #include "llvm/Support/MemoryBuffer.h"
30 #include "llvm/Support/raw_ostream.h"
35 #include <system_error>
39 using namespace llvm::object;
42 using namespace lld::coff;
44 static std::unique_ptr<lto::LTO> createLTO() {
46 C.Options = InitTargetOptionsFromCodeGenFlags();
48 // Always emit a section per function/datum with LTO. LLVM LTO should get most
49 // of the benefit of linker GC, but there are still opportunities for ICF.
50 C.Options.FunctionSections = true;
51 C.Options.DataSections = true;
53 // Use static reloc model on 32-bit x86 because it usually results in more
54 // compact code, and because there are also known code generation bugs when
55 // using the PIC model (see PR34306).
56 if (Config->Machine == COFF::IMAGE_FILE_MACHINE_I386)
57 C.RelocModel = Reloc::Static;
59 C.RelocModel = Reloc::PIC_;
60 C.DisableVerify = true;
61 C.DiagHandler = diagnosticHandler;
62 C.OptLevel = Config->LTOO;
64 C.MAttrs = GetMAttrs();
66 if (Config->SaveTemps)
67 checkError(C.addSaveTemps(std::string(Config->OutputFile) + ".",
68 /*UseInputModulePath*/ true));
69 lto::ThinBackend Backend;
70 if (Config->ThinLTOJobs != 0)
71 Backend = lto::createInProcessThinBackend(Config->ThinLTOJobs);
72 return llvm::make_unique<lto::LTO>(std::move(C), Backend,
73 Config->LTOPartitions);
76 BitcodeCompiler::BitcodeCompiler() : LTOObj(createLTO()) {}
78 BitcodeCompiler::~BitcodeCompiler() = default;
80 static void undefine(Symbol *S) { replaceSymbol<Undefined>(S, S->getName()); }
82 void BitcodeCompiler::add(BitcodeFile &F) {
83 lto::InputFile &Obj = *F.Obj;
85 std::vector<Symbol *> SymBodies = F.getSymbols();
86 std::vector<lto::SymbolResolution> Resols(SymBodies.size());
88 // Provide a resolution to the LTO API for each symbol.
89 for (const lto::InputFile::Symbol &ObjSym : Obj.symbols()) {
90 Symbol *Sym = SymBodies[SymNum];
91 lto::SymbolResolution &R = Resols[SymNum];
94 // Ideally we shouldn't check for SF_Undefined but currently IRObjectFile
95 // reports two symbols for module ASM defined. Without this check, lld
96 // flags an undefined in IR with a definition in ASM as prevailing.
97 // Once IRObjectFile is fixed to report only one symbol this hack can
99 R.Prevailing = !ObjSym.isUndefined() && Sym->getFile() == &F;
100 R.VisibleToRegularObj = Sym->IsUsedInRegularObj;
104 checkError(LTOObj->add(std::move(F.Obj), Resols));
107 // Merge all the bitcode files we have seen, codegen the result
108 // and return the resulting objects.
109 std::vector<StringRef> BitcodeCompiler::compile() {
110 unsigned MaxTasks = LTOObj->getMaxTasks();
111 Buf.resize(MaxTasks);
112 Files.resize(MaxTasks);
114 // The /lldltocache option specifies the path to a directory in which to cache
115 // native object files for ThinLTO incremental builds. If a path was
116 // specified, configure LTO to use it as the cache directory.
117 lto::NativeObjectCache Cache;
118 if (!Config->LTOCache.empty())
119 Cache = check(lto::localCache(
120 Config->LTOCache, [&](size_t Task, std::unique_ptr<MemoryBuffer> MB) {
121 Files[Task] = std::move(MB);
124 checkError(LTOObj->run(
126 return llvm::make_unique<lto::NativeObjectStream>(
127 llvm::make_unique<raw_svector_ostream>(Buf[Task]));
131 if (!Config->LTOCache.empty())
132 pruneCache(Config->LTOCache, Config->LTOCachePolicy);
134 std::vector<StringRef> Ret;
135 for (unsigned I = 0; I != MaxTasks; ++I) {
138 if (Config->SaveTemps) {
140 saveBuffer(Buf[I], Config->OutputFile + ".lto.obj");
142 saveBuffer(Buf[I], Config->OutputFile + Twine(I) + ".lto.obj");
144 Ret.emplace_back(Buf[I].data(), Buf[I].size());
147 for (std::unique_ptr<MemoryBuffer> &File : Files)
149 Ret.push_back(File->getBuffer());