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/TargetOptionsCommandFlags.h"
16 #include "llvm/ADT/STLExtras.h"
17 #include "llvm/ADT/SmallString.h"
18 #include "llvm/ADT/StringRef.h"
19 #include "llvm/ADT/Twine.h"
20 #include "llvm/IR/DiagnosticPrinter.h"
21 #include "llvm/LTO/Caching.h"
22 #include "llvm/LTO/Config.h"
23 #include "llvm/LTO/LTO.h"
24 #include "llvm/Object/SymbolicFile.h"
25 #include "llvm/Support/CodeGen.h"
26 #include "llvm/Support/Error.h"
27 #include "llvm/Support/FileSystem.h"
28 #include "llvm/Support/MemoryBuffer.h"
29 #include "llvm/Support/raw_ostream.h"
34 #include <system_error>
38 using namespace llvm::object;
41 using namespace lld::coff;
43 static void diagnosticHandler(const DiagnosticInfo &DI) {
44 SmallString<128> ErrStorage;
45 raw_svector_ostream OS(ErrStorage);
46 DiagnosticPrinterRawOStream DP(OS);
51 static void checkError(Error E) {
52 handleAllErrors(std::move(E),
53 [&](ErrorInfoBase &EIB) { error(EIB.message()); });
56 static void saveBuffer(StringRef Buffer, const Twine &Path) {
58 raw_fd_ostream OS(Path.str(), EC, sys::fs::OpenFlags::F_None);
60 error("cannot create " + Path + ": " + EC.message());
64 static std::unique_ptr<lto::LTO> createLTO() {
66 Conf.Options = InitTargetOptionsFromCodeGenFlags();
67 // Use static reloc model on 32-bit x86 because it usually results in more
68 // compact code, and because there are also known code generation bugs when
69 // using the PIC model (see PR34306).
70 if (Config->Machine == COFF::IMAGE_FILE_MACHINE_I386)
71 Conf.RelocModel = Reloc::Static;
73 Conf.RelocModel = Reloc::PIC_;
74 Conf.DisableVerify = true;
75 Conf.DiagHandler = diagnosticHandler;
76 Conf.OptLevel = Config->LTOOptLevel;
77 if (Config->SaveTemps)
78 checkError(Conf.addSaveTemps(std::string(Config->OutputFile) + ".",
79 /*UseInputModulePath*/ true));
80 lto::ThinBackend Backend;
81 if (Config->LTOJobs != 0)
82 Backend = lto::createInProcessThinBackend(Config->LTOJobs);
83 return llvm::make_unique<lto::LTO>(std::move(Conf), Backend,
84 Config->LTOPartitions);
87 BitcodeCompiler::BitcodeCompiler() : LTOObj(createLTO()) {}
89 BitcodeCompiler::~BitcodeCompiler() = default;
91 static void undefine(Symbol *S) { replaceSymbol<Undefined>(S, S->getName()); }
93 void BitcodeCompiler::add(BitcodeFile &F) {
94 lto::InputFile &Obj = *F.Obj;
96 std::vector<Symbol *> SymBodies = F.getSymbols();
97 std::vector<lto::SymbolResolution> Resols(SymBodies.size());
99 // Provide a resolution to the LTO API for each symbol.
100 for (const lto::InputFile::Symbol &ObjSym : Obj.symbols()) {
101 Symbol *Sym = SymBodies[SymNum];
102 lto::SymbolResolution &R = Resols[SymNum];
105 // Ideally we shouldn't check for SF_Undefined but currently IRObjectFile
106 // reports two symbols for module ASM defined. Without this check, lld
107 // flags an undefined in IR with a definition in ASM as prevailing.
108 // Once IRObjectFile is fixed to report only one symbol this hack can
110 R.Prevailing = !ObjSym.isUndefined() && Sym->getFile() == &F;
111 R.VisibleToRegularObj = Sym->IsUsedInRegularObj;
115 checkError(LTOObj->add(std::move(F.Obj), Resols));
118 // Merge all the bitcode files we have seen, codegen the result
119 // and return the resulting objects.
120 std::vector<StringRef> BitcodeCompiler::compile() {
121 unsigned MaxTasks = LTOObj->getMaxTasks();
122 Buff.resize(MaxTasks);
123 Files.resize(MaxTasks);
125 // The /lldltocache option specifies the path to a directory in which to cache
126 // native object files for ThinLTO incremental builds. If a path was
127 // specified, configure LTO to use it as the cache directory.
128 lto::NativeObjectCache Cache;
129 if (!Config->LTOCache.empty())
131 lto::localCache(Config->LTOCache,
132 [&](size_t Task, std::unique_ptr<MemoryBuffer> MB,
133 StringRef Path) { Files[Task] = std::move(MB); }));
135 checkError(LTOObj->run(
137 return llvm::make_unique<lto::NativeObjectStream>(
138 llvm::make_unique<raw_svector_ostream>(Buff[Task]));
142 if (!Config->LTOCache.empty())
143 pruneCache(Config->LTOCache, Config->LTOCachePolicy);
145 std::vector<StringRef> Ret;
146 for (unsigned I = 0; I != MaxTasks; ++I) {
149 if (Config->SaveTemps) {
151 saveBuffer(Buff[I], Config->OutputFile + ".lto.obj");
153 saveBuffer(Buff[I], Config->OutputFile + Twine(I) + ".lto.obj");
155 Ret.emplace_back(Buff[I].data(), Buff[I].size());
158 for (std::unique_ptr<MemoryBuffer> &File : Files)
160 Ret.push_back(File->getBuffer());