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;
63 if (Config->SaveTemps)
64 checkError(C.addSaveTemps(std::string(Config->OutputFile) + ".",
65 /*UseInputModulePath*/ true));
66 lto::ThinBackend Backend;
67 if (Config->ThinLTOJobs != 0)
68 Backend = lto::createInProcessThinBackend(Config->ThinLTOJobs);
69 return llvm::make_unique<lto::LTO>(std::move(C), Backend,
70 Config->LTOPartitions);
73 BitcodeCompiler::BitcodeCompiler() : LTOObj(createLTO()) {}
75 BitcodeCompiler::~BitcodeCompiler() = default;
77 static void undefine(Symbol *S) { replaceSymbol<Undefined>(S, S->getName()); }
79 void BitcodeCompiler::add(BitcodeFile &F) {
80 lto::InputFile &Obj = *F.Obj;
82 std::vector<Symbol *> SymBodies = F.getSymbols();
83 std::vector<lto::SymbolResolution> Resols(SymBodies.size());
85 // Provide a resolution to the LTO API for each symbol.
86 for (const lto::InputFile::Symbol &ObjSym : Obj.symbols()) {
87 Symbol *Sym = SymBodies[SymNum];
88 lto::SymbolResolution &R = Resols[SymNum];
91 // Ideally we shouldn't check for SF_Undefined but currently IRObjectFile
92 // reports two symbols for module ASM defined. Without this check, lld
93 // flags an undefined in IR with a definition in ASM as prevailing.
94 // Once IRObjectFile is fixed to report only one symbol this hack can
96 R.Prevailing = !ObjSym.isUndefined() && Sym->getFile() == &F;
97 R.VisibleToRegularObj = Sym->IsUsedInRegularObj;
101 checkError(LTOObj->add(std::move(F.Obj), Resols));
104 // Merge all the bitcode files we have seen, codegen the result
105 // and return the resulting objects.
106 std::vector<StringRef> BitcodeCompiler::compile() {
107 unsigned MaxTasks = LTOObj->getMaxTasks();
108 Buf.resize(MaxTasks);
109 Files.resize(MaxTasks);
111 // The /lldltocache option specifies the path to a directory in which to cache
112 // native object files for ThinLTO incremental builds. If a path was
113 // specified, configure LTO to use it as the cache directory.
114 lto::NativeObjectCache Cache;
115 if (!Config->LTOCache.empty())
116 Cache = check(lto::localCache(
117 Config->LTOCache, [&](size_t Task, std::unique_ptr<MemoryBuffer> MB) {
118 Files[Task] = std::move(MB);
121 checkError(LTOObj->run(
123 return llvm::make_unique<lto::NativeObjectStream>(
124 llvm::make_unique<raw_svector_ostream>(Buf[Task]));
128 if (!Config->LTOCache.empty())
129 pruneCache(Config->LTOCache, Config->LTOCachePolicy);
131 std::vector<StringRef> Ret;
132 for (unsigned I = 0; I != MaxTasks; ++I) {
135 if (Config->SaveTemps) {
137 saveBuffer(Buf[I], Config->OutputFile + ".lto.obj");
139 saveBuffer(Buf[I], Config->OutputFile + Twine(I) + ".lto.obj");
141 Ret.emplace_back(Buf[I].data(), Buf[I].size());
144 for (std::unique_ptr<MemoryBuffer> &File : Files)
146 Ret.push_back(File->getBuffer());