blob: ddd0a6d8e58c8dc2b9b6d46138bd7c144cf45381 [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"
Mehdi Amini970800e2016-08-17 06:23:09 +000022#include "llvm/LTO/LTO.h"
Teresa Johnson9ba95f92016-08-11 14:58:12 +000023#include "llvm/MC/SubtargetFeature.h"
24#include "llvm/Support/Error.h"
25#include "llvm/Support/FileSystem.h"
26#include "llvm/Support/TargetRegistry.h"
27#include "llvm/Support/ThreadPool.h"
28#include "llvm/Target/TargetMachine.h"
29#include "llvm/Transforms/IPO.h"
30#include "llvm/Transforms/IPO/PassManagerBuilder.h"
31#include "llvm/Transforms/Utils/FunctionImportUtils.h"
32#include "llvm/Transforms/Utils/SplitModule.h"
33
34using namespace llvm;
35using namespace lto;
36
37Error Config::addSaveTemps(std::string OutputFileName,
38 bool UseInputModulePath) {
39 ShouldDiscardValueNames = false;
40
41 std::error_code EC;
42 ResolutionFile = llvm::make_unique<raw_fd_ostream>(
Mehdi Aminieccffad2016-08-18 00:12:33 +000043 OutputFileName + "resolution.txt", EC, sys::fs::OpenFlags::F_Text);
Teresa Johnson9ba95f92016-08-11 14:58:12 +000044 if (EC)
45 return errorCodeToError(EC);
46
47 auto setHook = [&](std::string PathSuffix, ModuleHookFn &Hook) {
48 // Keep track of the hook provided by the linker, which also needs to run.
49 ModuleHookFn LinkerHook = Hook;
Teresa Johnsonfaa75062016-08-11 20:38:39 +000050 Hook = [=](unsigned Task, Module &M) {
Teresa Johnson9ba95f92016-08-11 14:58:12 +000051 // If the linker's hook returned false, we need to pass that result
52 // through.
53 if (LinkerHook && !LinkerHook(Task, M))
54 return false;
55
56 std::string PathPrefix;
57 // If this is the combined module (not a ThinLTO backend compile) or the
58 // user hasn't requested using the input module's path, emit to a file
59 // named from the provided OutputFileName with the Task ID appended.
60 if (M.getModuleIdentifier() == "ld-temp.o" || !UseInputModulePath) {
Mehdi Aminieccffad2016-08-18 00:12:33 +000061 PathPrefix = OutputFileName + utostr(Task);
Teresa Johnson9ba95f92016-08-11 14:58:12 +000062 } else
63 PathPrefix = M.getModuleIdentifier();
64 std::string Path = PathPrefix + "." + PathSuffix + ".bc";
65 std::error_code EC;
66 raw_fd_ostream OS(Path, EC, sys::fs::OpenFlags::F_None);
67 if (EC) {
68 // Because -save-temps is a debugging feature, we report the error
69 // directly and exit.
70 llvm::errs() << "failed to open " << Path << ": " << EC.message()
71 << '\n';
72 exit(1);
73 }
74 WriteBitcodeToFile(&M, OS, /*ShouldPreserveUseListOrder=*/false);
75 return true;
76 };
77 };
78
79 setHook("0.preopt", PreOptModuleHook);
80 setHook("1.promote", PostPromoteModuleHook);
81 setHook("2.internalize", PostInternalizeModuleHook);
82 setHook("3.import", PostImportModuleHook);
83 setHook("4.opt", PostOptModuleHook);
84 setHook("5.precodegen", PreCodeGenModuleHook);
85
86 CombinedIndexHook = [=](const ModuleSummaryIndex &Index) {
Mehdi Aminieccffad2016-08-18 00:12:33 +000087 std::string Path = OutputFileName + "index.bc";
Teresa Johnson9ba95f92016-08-11 14:58:12 +000088 std::error_code EC;
89 raw_fd_ostream OS(Path, EC, sys::fs::OpenFlags::F_None);
90 if (EC) {
91 // Because -save-temps is a debugging feature, we report the error
92 // directly and exit.
93 llvm::errs() << "failed to open " << Path << ": " << EC.message() << '\n';
94 exit(1);
95 }
96 WriteIndexToFile(Index, OS);
97 return true;
98 };
99
100 return Error();
101}
102
103namespace {
104
105std::unique_ptr<TargetMachine>
106createTargetMachine(Config &C, StringRef TheTriple, const Target *TheTarget) {
107 SubtargetFeatures Features;
108 Features.getDefaultSubtargetFeatures(Triple(TheTriple));
109 for (const std::string &A : C.MAttrs)
110 Features.AddFeature(A);
111
112 return std::unique_ptr<TargetMachine>(TheTarget->createTargetMachine(
113 TheTriple, C.CPU, Features.getString(), C.Options, C.RelocModel,
114 C.CodeModel, C.CGOptLevel));
115}
116
Teresa Johnsonfaa75062016-08-11 20:38:39 +0000117bool opt(Config &C, TargetMachine *TM, unsigned Task, Module &M,
118 bool IsThinLto) {
Teresa Johnson9ba95f92016-08-11 14:58:12 +0000119 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
Mehdi Amini970800e2016-08-17 06:23:09 +0000146void codegen(Config &C, TargetMachine *TM, AddOutputFn AddOutput, unsigned Task,
Teresa Johnson9ba95f92016-08-11 14:58:12 +0000147 Module &M) {
148 if (C.PreCodeGenModuleHook && !C.PreCodeGenModuleHook(Task, M))
149 return;
150
Mehdi Amini970800e2016-08-17 06:23:09 +0000151 auto Output = AddOutput(Task);
152 std::unique_ptr<raw_pwrite_stream> OS = Output->getStream();
Teresa Johnson9ba95f92016-08-11 14:58:12 +0000153 legacy::PassManager CodeGenPasses;
154 if (TM->addPassesToEmitFile(CodeGenPasses, *OS,
155 TargetMachine::CGFT_ObjectFile))
156 report_fatal_error("Failed to setup codegen");
157 CodeGenPasses.run(M);
158}
159
Mehdi Amini970800e2016-08-17 06:23:09 +0000160void splitCodeGen(Config &C, TargetMachine *TM, AddOutputFn AddOutput,
Teresa Johnson9ba95f92016-08-11 14:58:12 +0000161 unsigned ParallelCodeGenParallelismLevel,
162 std::unique_ptr<Module> M) {
163 ThreadPool CodegenThreadPool(ParallelCodeGenParallelismLevel);
164 unsigned ThreadCount = 0;
165 const Target *T = &TM->getTarget();
166
167 SplitModule(
168 std::move(M), ParallelCodeGenParallelismLevel,
169 [&](std::unique_ptr<Module> MPart) {
170 // We want to clone the module in a new context to multi-thread the
171 // codegen. We do it by serializing partition modules to bitcode
172 // (while still on the main thread, in order to avoid data races) and
173 // spinning up new threads which deserialize the partitions into
174 // separate contexts.
175 // FIXME: Provide a more direct way to do this in LLVM.
176 SmallString<0> BC;
177 raw_svector_ostream BCOS(BC);
178 WriteBitcodeToFile(MPart.get(), BCOS);
179
180 // Enqueue the task
181 CodegenThreadPool.async(
182 [&](const SmallString<0> &BC, unsigned ThreadId) {
183 LTOLLVMContext Ctx(C);
184 ErrorOr<std::unique_ptr<Module>> MOrErr = parseBitcodeFile(
185 MemoryBufferRef(StringRef(BC.data(), BC.size()), "ld-temp.o"),
186 Ctx);
187 if (!MOrErr)
188 report_fatal_error("Failed to read bitcode");
189 std::unique_ptr<Module> MPartInCtx = std::move(MOrErr.get());
190
191 std::unique_ptr<TargetMachine> TM =
192 createTargetMachine(C, MPartInCtx->getTargetTriple(), T);
Mehdi Amini970800e2016-08-17 06:23:09 +0000193 codegen(C, TM.get(), AddOutput, ThreadId, *MPartInCtx);
Teresa Johnson9ba95f92016-08-11 14:58:12 +0000194 },
195 // Pass BC using std::move to ensure that it get moved rather than
196 // copied into the thread's context.
197 std::move(BC), ThreadCount++);
198 },
199 false);
200}
201
202Expected<const Target *> initAndLookupTarget(Config &C, Module &M) {
203 if (!C.OverrideTriple.empty())
204 M.setTargetTriple(C.OverrideTriple);
205 else if (M.getTargetTriple().empty())
206 M.setTargetTriple(C.DefaultTriple);
207
208 std::string Msg;
209 const Target *T = TargetRegistry::lookupTarget(M.getTargetTriple(), Msg);
210 if (!T)
211 return make_error<StringError>(Msg, inconvertibleErrorCode());
212 return T;
213}
214
215}
216
Mehdi Amini970800e2016-08-17 06:23:09 +0000217Error lto::backend(Config &C, AddOutputFn AddOutput,
Teresa Johnson9ba95f92016-08-11 14:58:12 +0000218 unsigned ParallelCodeGenParallelismLevel,
219 std::unique_ptr<Module> M) {
220 Expected<const Target *> TOrErr = initAndLookupTarget(C, *M);
221 if (!TOrErr)
222 return TOrErr.takeError();
223
224 std::unique_ptr<TargetMachine> TM =
225 createTargetMachine(C, M->getTargetTriple(), *TOrErr);
226
227 if (!opt(C, TM.get(), 0, *M, /*IsThinLto=*/false))
228 return Error();
229
230 if (ParallelCodeGenParallelismLevel == 1)
Mehdi Amini970800e2016-08-17 06:23:09 +0000231 codegen(C, TM.get(), AddOutput, 0, *M);
Teresa Johnson9ba95f92016-08-11 14:58:12 +0000232 else
Mehdi Amini970800e2016-08-17 06:23:09 +0000233 splitCodeGen(C, TM.get(), AddOutput, ParallelCodeGenParallelismLevel,
Teresa Johnson9ba95f92016-08-11 14:58:12 +0000234 std::move(M));
235 return Error();
236}
237
Mehdi Amini970800e2016-08-17 06:23:09 +0000238Error lto::thinBackend(Config &Conf, unsigned Task, AddOutputFn AddOutput,
Mehdi Aminiacc50c42016-08-16 00:44:46 +0000239 Module &Mod, ModuleSummaryIndex &CombinedIndex,
Teresa Johnson9ba95f92016-08-11 14:58:12 +0000240 const FunctionImporter::ImportMapTy &ImportList,
241 const GVSummaryMapTy &DefinedGlobals,
242 MapVector<StringRef, MemoryBufferRef> &ModuleMap) {
Mehdi Aminiacc50c42016-08-16 00:44:46 +0000243 Expected<const Target *> TOrErr = initAndLookupTarget(Conf, Mod);
Teresa Johnson9ba95f92016-08-11 14:58:12 +0000244 if (!TOrErr)
245 return TOrErr.takeError();
246
247 std::unique_ptr<TargetMachine> TM =
Mehdi Aminiacc50c42016-08-16 00:44:46 +0000248 createTargetMachine(Conf, Mod.getTargetTriple(), *TOrErr);
Teresa Johnson9ba95f92016-08-11 14:58:12 +0000249
Mehdi Aminiacc50c42016-08-16 00:44:46 +0000250 if (Conf.PreOptModuleHook && !Conf.PreOptModuleHook(Task, Mod))
Teresa Johnson9ba95f92016-08-11 14:58:12 +0000251 return Error();
252
Mehdi Aminiacc50c42016-08-16 00:44:46 +0000253 thinLTOResolveWeakForLinkerModule(Mod, DefinedGlobals);
Teresa Johnson9ba95f92016-08-11 14:58:12 +0000254
Mehdi Aminiacc50c42016-08-16 00:44:46 +0000255 renameModuleForThinLTO(Mod, CombinedIndex);
Teresa Johnson9ba95f92016-08-11 14:58:12 +0000256
Mehdi Aminiacc50c42016-08-16 00:44:46 +0000257 if (Conf.PostPromoteModuleHook && !Conf.PostPromoteModuleHook(Task, Mod))
Teresa Johnson9ba95f92016-08-11 14:58:12 +0000258 return Error();
259
260 if (!DefinedGlobals.empty())
Mehdi Aminiacc50c42016-08-16 00:44:46 +0000261 thinLTOInternalizeModule(Mod, DefinedGlobals);
Teresa Johnson9ba95f92016-08-11 14:58:12 +0000262
Mehdi Aminiacc50c42016-08-16 00:44:46 +0000263 if (Conf.PostInternalizeModuleHook &&
264 !Conf.PostInternalizeModuleHook(Task, Mod))
Teresa Johnson9ba95f92016-08-11 14:58:12 +0000265 return Error();
266
267 auto ModuleLoader = [&](StringRef Identifier) {
268 return std::move(getLazyBitcodeModule(MemoryBuffer::getMemBuffer(
269 ModuleMap[Identifier], false),
Mehdi Aminiacc50c42016-08-16 00:44:46 +0000270 Mod.getContext(),
Teresa Johnson9ba95f92016-08-11 14:58:12 +0000271 /*ShouldLazyLoadMetadata=*/true)
272 .get());
273 };
274
275 FunctionImporter Importer(CombinedIndex, ModuleLoader);
Mehdi Aminiacc50c42016-08-16 00:44:46 +0000276 Importer.importFunctions(Mod, ImportList);
Teresa Johnson9ba95f92016-08-11 14:58:12 +0000277
Mehdi Aminiacc50c42016-08-16 00:44:46 +0000278 if (Conf.PostImportModuleHook && !Conf.PostImportModuleHook(Task, Mod))
Teresa Johnson9ba95f92016-08-11 14:58:12 +0000279 return Error();
280
Mehdi Aminiacc50c42016-08-16 00:44:46 +0000281 if (!opt(Conf, TM.get(), Task, Mod, /*IsThinLto=*/true))
Teresa Johnson9ba95f92016-08-11 14:58:12 +0000282 return Error();
283
Mehdi Amini970800e2016-08-17 06:23:09 +0000284 codegen(Conf, TM.get(), AddOutput, Task, Mod);
Teresa Johnson9ba95f92016-08-11 14:58:12 +0000285 return Error();
286}