]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lld/COFF/LTO.cpp
Merge clang 7.0.1 and several follow-up changes
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lld / 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   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);
71 }
72
73 BitcodeCompiler::BitcodeCompiler() : LTOObj(createLTO()) {}
74
75 BitcodeCompiler::~BitcodeCompiler() = default;
76
77 static void undefine(Symbol *S) { replaceSymbol<Undefined>(S, S->getName()); }
78
79 void BitcodeCompiler::add(BitcodeFile &F) {
80   lto::InputFile &Obj = *F.Obj;
81   unsigned SymNum = 0;
82   std::vector<Symbol *> SymBodies = F.getSymbols();
83   std::vector<lto::SymbolResolution> Resols(SymBodies.size());
84
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];
89     ++SymNum;
90
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
95     // be removed.
96     R.Prevailing = !ObjSym.isUndefined() && Sym->getFile() == &F;
97     R.VisibleToRegularObj = Sym->IsUsedInRegularObj;
98     if (R.Prevailing)
99       undefine(Sym);
100   }
101   checkError(LTOObj->add(std::move(F.Obj), Resols));
102 }
103
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);
110
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);
119         }));
120
121   checkError(LTOObj->run(
122       [&](size_t Task) {
123         return llvm::make_unique<lto::NativeObjectStream>(
124             llvm::make_unique<raw_svector_ostream>(Buf[Task]));
125       },
126       Cache));
127
128   if (!Config->LTOCache.empty())
129     pruneCache(Config->LTOCache, Config->LTOCachePolicy);
130
131   std::vector<StringRef> Ret;
132   for (unsigned I = 0; I != MaxTasks; ++I) {
133     if (Buf[I].empty())
134       continue;
135     if (Config->SaveTemps) {
136       if (I == 0)
137         saveBuffer(Buf[I], Config->OutputFile + ".lto.obj");
138       else
139         saveBuffer(Buf[I], Config->OutputFile + Twine(I) + ".lto.obj");
140     }
141     Ret.emplace_back(Buf[I].data(), Buf[I].size());
142   }
143
144   for (std::unique_ptr<MemoryBuffer> &File : Files)
145     if (File)
146       Ret.push_back(File->getBuffer());
147
148   return Ret;
149 }