]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - COFF/LTO.cpp
Vendor import of lld trunk r351319 (just before the release_80 branch
[FreeBSD/FreeBSD.git] / COFF / LTO.cpp
1 //===- LTO.cpp ------------------------------------------------------------===//
2 //
3 //                             The LLVM Linker
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "LTO.h"
11 #include "Config.h"
12 #include "InputFiles.h"
13 #include "Symbols.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"
31 #include <algorithm>
32 #include <cstddef>
33 #include <memory>
34 #include <string>
35 #include <system_error>
36 #include <vector>
37
38 using namespace llvm;
39 using namespace llvm::object;
40
41 using namespace lld;
42 using namespace lld::coff;
43
44 static std::unique_ptr<lto::LTO> createLTO() {
45   lto::Config C;
46   C.Options = InitTargetOptionsFromCodeGenFlags();
47
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;
52
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;
58   else
59     C.RelocModel = Reloc::PIC_;
60   C.DisableVerify = true;
61   C.DiagHandler = diagnosticHandler;
62   C.OptLevel = Config->LTOO;
63   C.CPU = GetCPUStr();
64   C.MAttrs = GetMAttrs();
65
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);
74 }
75
76 BitcodeCompiler::BitcodeCompiler() : LTOObj(createLTO()) {}
77
78 BitcodeCompiler::~BitcodeCompiler() = default;
79
80 static void undefine(Symbol *S) { replaceSymbol<Undefined>(S, S->getName()); }
81
82 void BitcodeCompiler::add(BitcodeFile &F) {
83   lto::InputFile &Obj = *F.Obj;
84   unsigned SymNum = 0;
85   std::vector<Symbol *> SymBodies = F.getSymbols();
86   std::vector<lto::SymbolResolution> Resols(SymBodies.size());
87
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];
92     ++SymNum;
93
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
98     // be removed.
99     R.Prevailing = !ObjSym.isUndefined() && Sym->getFile() == &F;
100     R.VisibleToRegularObj = Sym->IsUsedInRegularObj;
101     if (R.Prevailing)
102       undefine(Sym);
103   }
104   checkError(LTOObj->add(std::move(F.Obj), Resols));
105 }
106
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);
113
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);
122         }));
123
124   checkError(LTOObj->run(
125       [&](size_t Task) {
126         return llvm::make_unique<lto::NativeObjectStream>(
127             llvm::make_unique<raw_svector_ostream>(Buf[Task]));
128       },
129       Cache));
130
131   if (!Config->LTOCache.empty())
132     pruneCache(Config->LTOCache, Config->LTOCachePolicy);
133
134   std::vector<StringRef> Ret;
135   for (unsigned I = 0; I != MaxTasks; ++I) {
136     if (Buf[I].empty())
137       continue;
138     if (Config->SaveTemps) {
139       if (I == 0)
140         saveBuffer(Buf[I], Config->OutputFile + ".lto.obj");
141       else
142         saveBuffer(Buf[I], Config->OutputFile + Twine(I) + ".lto.obj");
143     }
144     Ret.emplace_back(Buf[I].data(), Buf[I].size());
145   }
146
147   for (std::unique_ptr<MemoryBuffer> &File : Files)
148     if (File)
149       Ret.push_back(File->getBuffer());
150
151   return Ret;
152 }