blob: 3ed52ba575db3cadda0b3bd06fa9780e03c161be [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;
Mehdi Aminif8c2f082016-08-22 16:17:40 +000050 Hook = [=](unsigned Task, const 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>
Davide Italiano24c29b12016-09-07 01:08:31 +0000106createTargetMachine(Config &Conf, StringRef TheTriple,
107 const Target *TheTarget) {
Teresa Johnson9ba95f92016-08-11 14:58:12 +0000108 SubtargetFeatures Features;
109 Features.getDefaultSubtargetFeatures(Triple(TheTriple));
Davide Italiano24c29b12016-09-07 01:08:31 +0000110 for (const std::string &A : Conf.MAttrs)
Teresa Johnson9ba95f92016-08-11 14:58:12 +0000111 Features.AddFeature(A);
112
113 return std::unique_ptr<TargetMachine>(TheTarget->createTargetMachine(
Davide Italiano24c29b12016-09-07 01:08:31 +0000114 TheTriple, Conf.CPU, Features.getString(), Conf.Options, Conf.RelocModel,
115 Conf.CodeModel, Conf.CGOptLevel));
Teresa Johnson9ba95f92016-08-11 14:58:12 +0000116}
117
Davide Italiano24c29b12016-09-07 01:08:31 +0000118static void runOldPMPasses(Config &Conf, Module &Mod, TargetMachine *TM,
Davide Italiano1e9d3d32016-08-31 17:02:44 +0000119 bool IsThinLto) {
Teresa Johnson9ba95f92016-08-11 14:58:12 +0000120 legacy::PassManager passes;
121 passes.add(createTargetTransformInfoWrapperPass(TM->getTargetIRAnalysis()));
122
123 PassManagerBuilder PMB;
124 PMB.LibraryInfo = new TargetLibraryInfoImpl(Triple(TM->getTargetTriple()));
125 PMB.Inliner = createFunctionInliningPass();
126 // Unconditionally verify input since it is not verified before this
127 // point and has unknown origin.
128 PMB.VerifyInput = true;
Davide Italiano24c29b12016-09-07 01:08:31 +0000129 PMB.VerifyOutput = !Conf.DisableVerify;
Teresa Johnson9ba95f92016-08-11 14:58:12 +0000130 PMB.LoopVectorize = true;
131 PMB.SLPVectorize = true;
Davide Italiano24c29b12016-09-07 01:08:31 +0000132 PMB.OptLevel = Conf.OptLevel;
Teresa Johnson9ba95f92016-08-11 14:58:12 +0000133 if (IsThinLto)
134 PMB.populateThinLTOPassManager(passes);
135 else
136 PMB.populateLTOPassManager(passes);
Davide Italiano24c29b12016-09-07 01:08:31 +0000137 passes.run(Mod);
Davide Italiano1e9d3d32016-08-31 17:02:44 +0000138}
Teresa Johnson9ba95f92016-08-11 14:58:12 +0000139
Davide Italiano24c29b12016-09-07 01:08:31 +0000140bool opt(Config &Conf, TargetMachine *TM, unsigned Task, Module &Mod,
Davide Italiano1e9d3d32016-08-31 17:02:44 +0000141 bool IsThinLto) {
Davide Italiano24c29b12016-09-07 01:08:31 +0000142 Mod.setDataLayout(TM->createDataLayout());
143 runOldPMPasses(Conf, Mod, TM, IsThinLto);
144 return !Conf.PostOptModuleHook || Conf.PostOptModuleHook(Task, Mod);
Teresa Johnson9ba95f92016-08-11 14:58:12 +0000145}
146
Mehdi Aminiadc0e262016-08-23 21:30:12 +0000147/// Monolithic LTO does not support caching (yet), this is a convenient wrapper
148/// around AddOutput to workaround this.
149static AddOutputFn getUncachedOutputWrapper(AddOutputFn &AddOutput,
150 unsigned Task) {
151 return [Task, &AddOutput](unsigned TaskId) {
152 auto Output = AddOutput(Task);
153 if (Output->isCachingEnabled() && Output->tryLoadFromCache(""))
154 report_fatal_error("Cache hit without a valid key?");
Mehdi Aminiadc0e262016-08-23 21:30:12 +0000155 assert(Task == TaskId && "Unexpexted TaskId mismatch");
156 return Output;
157 };
158}
159
Davide Italiano24c29b12016-09-07 01:08:31 +0000160void codegen(Config &Conf, TargetMachine *TM, AddOutputFn AddOutput,
161 unsigned Task, Module &Mod) {
162 if (Conf.PreCodeGenModuleHook && !Conf.PreCodeGenModuleHook(Task, Mod))
Teresa Johnson9ba95f92016-08-11 14:58:12 +0000163 return;
164
Mehdi Amini970800e2016-08-17 06:23:09 +0000165 auto Output = AddOutput(Task);
166 std::unique_ptr<raw_pwrite_stream> OS = Output->getStream();
Teresa Johnson9ba95f92016-08-11 14:58:12 +0000167 legacy::PassManager CodeGenPasses;
168 if (TM->addPassesToEmitFile(CodeGenPasses, *OS,
169 TargetMachine::CGFT_ObjectFile))
170 report_fatal_error("Failed to setup codegen");
Davide Italiano24c29b12016-09-07 01:08:31 +0000171 CodeGenPasses.run(Mod);
Teresa Johnson9ba95f92016-08-11 14:58:12 +0000172}
173
Mehdi Amini970800e2016-08-17 06:23:09 +0000174void splitCodeGen(Config &C, TargetMachine *TM, AddOutputFn AddOutput,
Teresa Johnson9ba95f92016-08-11 14:58:12 +0000175 unsigned ParallelCodeGenParallelismLevel,
Davide Italiano24c29b12016-09-07 01:08:31 +0000176 std::unique_ptr<Module> Mod) {
Teresa Johnson9ba95f92016-08-11 14:58:12 +0000177 ThreadPool CodegenThreadPool(ParallelCodeGenParallelismLevel);
178 unsigned ThreadCount = 0;
179 const Target *T = &TM->getTarget();
180
181 SplitModule(
Davide Italiano24c29b12016-09-07 01:08:31 +0000182 std::move(Mod), ParallelCodeGenParallelismLevel,
Teresa Johnson9ba95f92016-08-11 14:58:12 +0000183 [&](std::unique_ptr<Module> MPart) {
184 // We want to clone the module in a new context to multi-thread the
185 // codegen. We do it by serializing partition modules to bitcode
186 // (while still on the main thread, in order to avoid data races) and
187 // spinning up new threads which deserialize the partitions into
188 // separate contexts.
189 // FIXME: Provide a more direct way to do this in LLVM.
190 SmallString<0> BC;
191 raw_svector_ostream BCOS(BC);
192 WriteBitcodeToFile(MPart.get(), BCOS);
193
194 // Enqueue the task
195 CodegenThreadPool.async(
196 [&](const SmallString<0> &BC, unsigned ThreadId) {
197 LTOLLVMContext Ctx(C);
198 ErrorOr<std::unique_ptr<Module>> MOrErr = parseBitcodeFile(
199 MemoryBufferRef(StringRef(BC.data(), BC.size()), "ld-temp.o"),
200 Ctx);
201 if (!MOrErr)
202 report_fatal_error("Failed to read bitcode");
203 std::unique_ptr<Module> MPartInCtx = std::move(MOrErr.get());
204
205 std::unique_ptr<TargetMachine> TM =
206 createTargetMachine(C, MPartInCtx->getTargetTriple(), T);
Mehdi Aminiadc0e262016-08-23 21:30:12 +0000207
208 codegen(C, TM.get(),
209 getUncachedOutputWrapper(AddOutput, ThreadId), ThreadId,
210 *MPartInCtx);
Teresa Johnson9ba95f92016-08-11 14:58:12 +0000211 },
212 // Pass BC using std::move to ensure that it get moved rather than
213 // copied into the thread's context.
214 std::move(BC), ThreadCount++);
215 },
216 false);
217}
218
Davide Italiano24c29b12016-09-07 01:08:31 +0000219Expected<const Target *> initAndLookupTarget(Config &C, Module &Mod) {
Teresa Johnson9ba95f92016-08-11 14:58:12 +0000220 if (!C.OverrideTriple.empty())
Davide Italiano24c29b12016-09-07 01:08:31 +0000221 Mod.setTargetTriple(C.OverrideTriple);
222 else if (Mod.getTargetTriple().empty())
223 Mod.setTargetTriple(C.DefaultTriple);
Teresa Johnson9ba95f92016-08-11 14:58:12 +0000224
225 std::string Msg;
Davide Italiano24c29b12016-09-07 01:08:31 +0000226 const Target *T = TargetRegistry::lookupTarget(Mod.getTargetTriple(), Msg);
Teresa Johnson9ba95f92016-08-11 14:58:12 +0000227 if (!T)
228 return make_error<StringError>(Msg, inconvertibleErrorCode());
229 return T;
230}
231
232}
233
Mehdi Amini970800e2016-08-17 06:23:09 +0000234Error lto::backend(Config &C, AddOutputFn AddOutput,
Teresa Johnson9ba95f92016-08-11 14:58:12 +0000235 unsigned ParallelCodeGenParallelismLevel,
Davide Italiano24c29b12016-09-07 01:08:31 +0000236 std::unique_ptr<Module> Mod) {
237 Expected<const Target *> TOrErr = initAndLookupTarget(C, *Mod);
Teresa Johnson9ba95f92016-08-11 14:58:12 +0000238 if (!TOrErr)
239 return TOrErr.takeError();
240
241 std::unique_ptr<TargetMachine> TM =
Davide Italiano24c29b12016-09-07 01:08:31 +0000242 createTargetMachine(C, Mod->getTargetTriple(), *TOrErr);
Teresa Johnson9ba95f92016-08-11 14:58:12 +0000243
Mehdi Aminid310b472016-08-22 06:25:41 +0000244 if (!C.CodeGenOnly)
Davide Italiano24c29b12016-09-07 01:08:31 +0000245 if (!opt(C, TM.get(), 0, *Mod, /*IsThinLto=*/false))
Mehdi Aminid310b472016-08-22 06:25:41 +0000246 return Error();
Teresa Johnson9ba95f92016-08-11 14:58:12 +0000247
Mehdi Aminiadc0e262016-08-23 21:30:12 +0000248 if (ParallelCodeGenParallelismLevel == 1) {
Davide Italiano24c29b12016-09-07 01:08:31 +0000249 codegen(C, TM.get(), getUncachedOutputWrapper(AddOutput, 0), 0, *Mod);
Mehdi Aminiadc0e262016-08-23 21:30:12 +0000250 } else {
Mehdi Amini970800e2016-08-17 06:23:09 +0000251 splitCodeGen(C, TM.get(), AddOutput, ParallelCodeGenParallelismLevel,
Davide Italiano24c29b12016-09-07 01:08:31 +0000252 std::move(Mod));
Mehdi Aminiadc0e262016-08-23 21:30:12 +0000253 }
Teresa Johnson9ba95f92016-08-11 14:58:12 +0000254 return Error();
255}
256
Mehdi Amini970800e2016-08-17 06:23:09 +0000257Error lto::thinBackend(Config &Conf, unsigned Task, AddOutputFn AddOutput,
Mehdi Aminiacc50c42016-08-16 00:44:46 +0000258 Module &Mod, ModuleSummaryIndex &CombinedIndex,
Teresa Johnson9ba95f92016-08-11 14:58:12 +0000259 const FunctionImporter::ImportMapTy &ImportList,
260 const GVSummaryMapTy &DefinedGlobals,
261 MapVector<StringRef, MemoryBufferRef> &ModuleMap) {
Mehdi Aminiacc50c42016-08-16 00:44:46 +0000262 Expected<const Target *> TOrErr = initAndLookupTarget(Conf, Mod);
Teresa Johnson9ba95f92016-08-11 14:58:12 +0000263 if (!TOrErr)
264 return TOrErr.takeError();
265
266 std::unique_ptr<TargetMachine> TM =
Mehdi Aminiacc50c42016-08-16 00:44:46 +0000267 createTargetMachine(Conf, Mod.getTargetTriple(), *TOrErr);
Teresa Johnson9ba95f92016-08-11 14:58:12 +0000268
Mehdi Aminid310b472016-08-22 06:25:41 +0000269 if (Conf.CodeGenOnly) {
270 codegen(Conf, TM.get(), AddOutput, Task, Mod);
271 return Error();
272 }
273
Mehdi Aminiacc50c42016-08-16 00:44:46 +0000274 if (Conf.PreOptModuleHook && !Conf.PreOptModuleHook(Task, Mod))
Teresa Johnson9ba95f92016-08-11 14:58:12 +0000275 return Error();
276
Mehdi Aminiacc50c42016-08-16 00:44:46 +0000277 renameModuleForThinLTO(Mod, CombinedIndex);
Teresa Johnson9ba95f92016-08-11 14:58:12 +0000278
Mehdi Amini8ac7b322016-08-18 00:59:24 +0000279 thinLTOResolveWeakForLinkerModule(Mod, DefinedGlobals);
280
Mehdi Aminiacc50c42016-08-16 00:44:46 +0000281 if (Conf.PostPromoteModuleHook && !Conf.PostPromoteModuleHook(Task, Mod))
Teresa Johnson9ba95f92016-08-11 14:58:12 +0000282 return Error();
283
284 if (!DefinedGlobals.empty())
Mehdi Aminiacc50c42016-08-16 00:44:46 +0000285 thinLTOInternalizeModule(Mod, DefinedGlobals);
Teresa Johnson9ba95f92016-08-11 14:58:12 +0000286
Mehdi Aminiacc50c42016-08-16 00:44:46 +0000287 if (Conf.PostInternalizeModuleHook &&
288 !Conf.PostInternalizeModuleHook(Task, Mod))
Teresa Johnson9ba95f92016-08-11 14:58:12 +0000289 return Error();
290
291 auto ModuleLoader = [&](StringRef Identifier) {
Mehdi Amini9ec5a612016-08-23 16:53:34 +0000292 assert(Mod.getContext().isODRUniquingDebugTypes() &&
293 "ODR Type uniquing shoudl be enabled on the context");
Teresa Johnson9ba95f92016-08-11 14:58:12 +0000294 return std::move(getLazyBitcodeModule(MemoryBuffer::getMemBuffer(
295 ModuleMap[Identifier], false),
Mehdi Aminiacc50c42016-08-16 00:44:46 +0000296 Mod.getContext(),
Teresa Johnson9ba95f92016-08-11 14:58:12 +0000297 /*ShouldLazyLoadMetadata=*/true)
298 .get());
299 };
300
301 FunctionImporter Importer(CombinedIndex, ModuleLoader);
Mehdi Aminiacc50c42016-08-16 00:44:46 +0000302 Importer.importFunctions(Mod, ImportList);
Teresa Johnson9ba95f92016-08-11 14:58:12 +0000303
Mehdi Aminiacc50c42016-08-16 00:44:46 +0000304 if (Conf.PostImportModuleHook && !Conf.PostImportModuleHook(Task, Mod))
Teresa Johnson9ba95f92016-08-11 14:58:12 +0000305 return Error();
306
Mehdi Aminiacc50c42016-08-16 00:44:46 +0000307 if (!opt(Conf, TM.get(), Task, Mod, /*IsThinLto=*/true))
Teresa Johnson9ba95f92016-08-11 14:58:12 +0000308 return Error();
309
Mehdi Amini970800e2016-08-17 06:23:09 +0000310 codegen(Conf, TM.get(), AddOutput, Task, Mod);
Teresa Johnson9ba95f92016-08-11 14:58:12 +0000311 return Error();
312}