blob: 5e06a06cadc8d90e1f506254934080403bb2ec40 [file] [log] [blame]
Teresa Johnson9ba95f92016-08-11 14:58:12 +00001//===-LTOBackend.cpp - LLVM Link Time Optimizer Backend -------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the "backend" phase of LTO, i.e. it performs
11// optimization and code generation on a loaded module. It is generally used
12// internally by the LTO class but can also be used independently, for example
13// to implement a standalone ThinLTO backend.
14//
15//===----------------------------------------------------------------------===//
16
17#include "llvm/LTO/LTOBackend.h"
18#include "llvm/Analysis/TargetLibraryInfo.h"
19#include "llvm/Analysis/TargetTransformInfo.h"
20#include "llvm/Bitcode/ReaderWriter.h"
21#include "llvm/IR/LegacyPassManager.h"
22#include "llvm/MC/SubtargetFeature.h"
23#include "llvm/Support/Error.h"
24#include "llvm/Support/FileSystem.h"
25#include "llvm/Support/TargetRegistry.h"
26#include "llvm/Support/ThreadPool.h"
27#include "llvm/Target/TargetMachine.h"
28#include "llvm/Transforms/IPO.h"
29#include "llvm/Transforms/IPO/PassManagerBuilder.h"
30#include "llvm/Transforms/Utils/FunctionImportUtils.h"
31#include "llvm/Transforms/Utils/SplitModule.h"
32
33using namespace llvm;
34using namespace lto;
35
36Error Config::addSaveTemps(std::string OutputFileName,
37 bool UseInputModulePath) {
38 ShouldDiscardValueNames = false;
39
40 std::error_code EC;
41 ResolutionFile = llvm::make_unique<raw_fd_ostream>(
42 OutputFileName + ".resolution.txt", EC, sys::fs::OpenFlags::F_Text);
43 if (EC)
44 return errorCodeToError(EC);
45
46 auto setHook = [&](std::string PathSuffix, ModuleHookFn &Hook) {
47 // Keep track of the hook provided by the linker, which also needs to run.
48 ModuleHookFn LinkerHook = Hook;
49 Hook = [=](size_t Task, Module &M) {
50 // If the linker's hook returned false, we need to pass that result
51 // through.
52 if (LinkerHook && !LinkerHook(Task, M))
53 return false;
54
55 std::string PathPrefix;
56 // If this is the combined module (not a ThinLTO backend compile) or the
57 // user hasn't requested using the input module's path, emit to a file
58 // named from the provided OutputFileName with the Task ID appended.
59 if (M.getModuleIdentifier() == "ld-temp.o" || !UseInputModulePath) {
60 PathPrefix = OutputFileName;
61 if (Task != 0)
62 PathPrefix += "." + utostr(Task);
63 } else
64 PathPrefix = M.getModuleIdentifier();
65 std::string Path = PathPrefix + "." + PathSuffix + ".bc";
66 std::error_code EC;
67 raw_fd_ostream OS(Path, EC, sys::fs::OpenFlags::F_None);
68 if (EC) {
69 // Because -save-temps is a debugging feature, we report the error
70 // directly and exit.
71 llvm::errs() << "failed to open " << Path << ": " << EC.message()
72 << '\n';
73 exit(1);
74 }
75 WriteBitcodeToFile(&M, OS, /*ShouldPreserveUseListOrder=*/false);
76 return true;
77 };
78 };
79
80 setHook("0.preopt", PreOptModuleHook);
81 setHook("1.promote", PostPromoteModuleHook);
82 setHook("2.internalize", PostInternalizeModuleHook);
83 setHook("3.import", PostImportModuleHook);
84 setHook("4.opt", PostOptModuleHook);
85 setHook("5.precodegen", PreCodeGenModuleHook);
86
87 CombinedIndexHook = [=](const ModuleSummaryIndex &Index) {
88 std::string Path = OutputFileName + ".index.bc";
89 std::error_code EC;
90 raw_fd_ostream OS(Path, EC, sys::fs::OpenFlags::F_None);
91 if (EC) {
92 // Because -save-temps is a debugging feature, we report the error
93 // directly and exit.
94 llvm::errs() << "failed to open " << Path << ": " << EC.message() << '\n';
95 exit(1);
96 }
97 WriteIndexToFile(Index, OS);
98 return true;
99 };
100
101 return Error();
102}
103
104namespace {
105
106std::unique_ptr<TargetMachine>
107createTargetMachine(Config &C, StringRef TheTriple, const Target *TheTarget) {
108 SubtargetFeatures Features;
109 Features.getDefaultSubtargetFeatures(Triple(TheTriple));
110 for (const std::string &A : C.MAttrs)
111 Features.AddFeature(A);
112
113 return std::unique_ptr<TargetMachine>(TheTarget->createTargetMachine(
114 TheTriple, C.CPU, Features.getString(), C.Options, C.RelocModel,
115 C.CodeModel, C.CGOptLevel));
116}
117
118bool opt(Config &C, TargetMachine *TM, size_t Task, Module &M, bool IsThinLto) {
119 M.setDataLayout(TM->createDataLayout());
120
121 legacy::PassManager passes;
122 passes.add(createTargetTransformInfoWrapperPass(TM->getTargetIRAnalysis()));
123
124 PassManagerBuilder PMB;
125 PMB.LibraryInfo = new TargetLibraryInfoImpl(Triple(TM->getTargetTriple()));
126 PMB.Inliner = createFunctionInliningPass();
127 // Unconditionally verify input since it is not verified before this
128 // point and has unknown origin.
129 PMB.VerifyInput = true;
130 PMB.VerifyOutput = !C.DisableVerify;
131 PMB.LoopVectorize = true;
132 PMB.SLPVectorize = true;
133 PMB.OptLevel = C.OptLevel;
134 if (IsThinLto)
135 PMB.populateThinLTOPassManager(passes);
136 else
137 PMB.populateLTOPassManager(passes);
138 passes.run(M);
139
140 if (C.PostOptModuleHook && !C.PostOptModuleHook(Task, M))
141 return false;
142
143 return true;
144}
145
146void codegen(Config &C, TargetMachine *TM, AddStreamFn AddStream, size_t Task,
147 Module &M) {
148 if (C.PreCodeGenModuleHook && !C.PreCodeGenModuleHook(Task, M))
149 return;
150
151 std::unique_ptr<raw_pwrite_stream> OS = AddStream(Task);
152 legacy::PassManager CodeGenPasses;
153 if (TM->addPassesToEmitFile(CodeGenPasses, *OS,
154 TargetMachine::CGFT_ObjectFile))
155 report_fatal_error("Failed to setup codegen");
156 CodeGenPasses.run(M);
157}
158
159void splitCodeGen(Config &C, TargetMachine *TM, AddStreamFn AddStream,
160 unsigned ParallelCodeGenParallelismLevel,
161 std::unique_ptr<Module> M) {
162 ThreadPool CodegenThreadPool(ParallelCodeGenParallelismLevel);
163 unsigned ThreadCount = 0;
164 const Target *T = &TM->getTarget();
165
166 SplitModule(
167 std::move(M), ParallelCodeGenParallelismLevel,
168 [&](std::unique_ptr<Module> MPart) {
169 // We want to clone the module in a new context to multi-thread the
170 // codegen. We do it by serializing partition modules to bitcode
171 // (while still on the main thread, in order to avoid data races) and
172 // spinning up new threads which deserialize the partitions into
173 // separate contexts.
174 // FIXME: Provide a more direct way to do this in LLVM.
175 SmallString<0> BC;
176 raw_svector_ostream BCOS(BC);
177 WriteBitcodeToFile(MPart.get(), BCOS);
178
179 // Enqueue the task
180 CodegenThreadPool.async(
181 [&](const SmallString<0> &BC, unsigned ThreadId) {
182 LTOLLVMContext Ctx(C);
183 ErrorOr<std::unique_ptr<Module>> MOrErr = parseBitcodeFile(
184 MemoryBufferRef(StringRef(BC.data(), BC.size()), "ld-temp.o"),
185 Ctx);
186 if (!MOrErr)
187 report_fatal_error("Failed to read bitcode");
188 std::unique_ptr<Module> MPartInCtx = std::move(MOrErr.get());
189
190 std::unique_ptr<TargetMachine> TM =
191 createTargetMachine(C, MPartInCtx->getTargetTriple(), T);
192 codegen(C, TM.get(), AddStream, ThreadId, *MPartInCtx);
193 },
194 // Pass BC using std::move to ensure that it get moved rather than
195 // copied into the thread's context.
196 std::move(BC), ThreadCount++);
197 },
198 false);
199}
200
201Expected<const Target *> initAndLookupTarget(Config &C, Module &M) {
202 if (!C.OverrideTriple.empty())
203 M.setTargetTriple(C.OverrideTriple);
204 else if (M.getTargetTriple().empty())
205 M.setTargetTriple(C.DefaultTriple);
206
207 std::string Msg;
208 const Target *T = TargetRegistry::lookupTarget(M.getTargetTriple(), Msg);
209 if (!T)
210 return make_error<StringError>(Msg, inconvertibleErrorCode());
211 return T;
212}
213
214}
215
216Error lto::backend(Config &C, AddStreamFn AddStream,
217 unsigned ParallelCodeGenParallelismLevel,
218 std::unique_ptr<Module> M) {
219 Expected<const Target *> TOrErr = initAndLookupTarget(C, *M);
220 if (!TOrErr)
221 return TOrErr.takeError();
222
223 std::unique_ptr<TargetMachine> TM =
224 createTargetMachine(C, M->getTargetTriple(), *TOrErr);
225
226 if (!opt(C, TM.get(), 0, *M, /*IsThinLto=*/false))
227 return Error();
228
229 if (ParallelCodeGenParallelismLevel == 1)
230 codegen(C, TM.get(), AddStream, 0, *M);
231 else
232 splitCodeGen(C, TM.get(), AddStream, ParallelCodeGenParallelismLevel,
233 std::move(M));
234 return Error();
235}
236
237Error lto::thinBackend(Config &C, size_t Task, AddStreamFn AddStream, Module &M,
238 ModuleSummaryIndex &CombinedIndex,
239 const FunctionImporter::ImportMapTy &ImportList,
240 const GVSummaryMapTy &DefinedGlobals,
241 MapVector<StringRef, MemoryBufferRef> &ModuleMap) {
242 Expected<const Target *> TOrErr = initAndLookupTarget(C, M);
243 if (!TOrErr)
244 return TOrErr.takeError();
245
246 std::unique_ptr<TargetMachine> TM =
247 createTargetMachine(C, M.getTargetTriple(), *TOrErr);
248
249 if (C.PreOptModuleHook && !C.PreOptModuleHook(Task, M))
250 return Error();
251
252 thinLTOResolveWeakForLinkerModule(M, DefinedGlobals);
253
254 renameModuleForThinLTO(M, CombinedIndex);
255
256 if (C.PostPromoteModuleHook && !C.PostPromoteModuleHook(Task, M))
257 return Error();
258
259 if (!DefinedGlobals.empty())
260 thinLTOInternalizeModule(M, DefinedGlobals);
261
262 if (C.PostInternalizeModuleHook && !C.PostInternalizeModuleHook(Task, M))
263 return Error();
264
265 auto ModuleLoader = [&](StringRef Identifier) {
266 return std::move(getLazyBitcodeModule(MemoryBuffer::getMemBuffer(
267 ModuleMap[Identifier], false),
268 M.getContext(),
269 /*ShouldLazyLoadMetadata=*/true)
270 .get());
271 };
272
273 FunctionImporter Importer(CombinedIndex, ModuleLoader);
274 Importer.importFunctions(M, ImportList);
275
276 if (C.PostImportModuleHook && !C.PostImportModuleHook(Task, M))
277 return Error();
278
279 if (!opt(C, TM.get(), Task, M, /*IsThinLto=*/true))
280 return Error();
281
282 codegen(C, TM.get(), AddStream, Task, M);
283 return Error();
284}