blob: 75f3182ee1d504cf6cd5a5bf961ab4d594e76b56 [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"
Davide Italianoec9612d2016-09-07 17:46:16 +000018#include "llvm/Analysis/AliasAnalysis.h"
19#include "llvm/Analysis/CGSCCPassManager.h"
20#include "llvm/Analysis/LoopPassManager.h"
Teresa Johnson9ba95f92016-08-11 14:58:12 +000021#include "llvm/Analysis/TargetLibraryInfo.h"
22#include "llvm/Analysis/TargetTransformInfo.h"
23#include "llvm/Bitcode/ReaderWriter.h"
24#include "llvm/IR/LegacyPassManager.h"
Davide Italianoec9612d2016-09-07 17:46:16 +000025#include "llvm/IR/PassManager.h"
26#include "llvm/IR/Verifier.h"
Mehdi Amini970800e2016-08-17 06:23:09 +000027#include "llvm/LTO/LTO.h"
Teresa Johnson9ba95f92016-08-11 14:58:12 +000028#include "llvm/MC/SubtargetFeature.h"
Davide Italianoec9612d2016-09-07 17:46:16 +000029#include "llvm/Passes/PassBuilder.h"
Teresa Johnson9ba95f92016-08-11 14:58:12 +000030#include "llvm/Support/Error.h"
31#include "llvm/Support/FileSystem.h"
32#include "llvm/Support/TargetRegistry.h"
33#include "llvm/Support/ThreadPool.h"
34#include "llvm/Target/TargetMachine.h"
35#include "llvm/Transforms/IPO.h"
36#include "llvm/Transforms/IPO/PassManagerBuilder.h"
37#include "llvm/Transforms/Utils/FunctionImportUtils.h"
38#include "llvm/Transforms/Utils/SplitModule.h"
39
40using namespace llvm;
41using namespace lto;
42
43Error Config::addSaveTemps(std::string OutputFileName,
44 bool UseInputModulePath) {
45 ShouldDiscardValueNames = false;
46
47 std::error_code EC;
48 ResolutionFile = llvm::make_unique<raw_fd_ostream>(
Mehdi Aminieccffad2016-08-18 00:12:33 +000049 OutputFileName + "resolution.txt", EC, sys::fs::OpenFlags::F_Text);
Teresa Johnson9ba95f92016-08-11 14:58:12 +000050 if (EC)
51 return errorCodeToError(EC);
52
53 auto setHook = [&](std::string PathSuffix, ModuleHookFn &Hook) {
54 // Keep track of the hook provided by the linker, which also needs to run.
55 ModuleHookFn LinkerHook = Hook;
Mehdi Aminif8c2f082016-08-22 16:17:40 +000056 Hook = [=](unsigned Task, const Module &M) {
Teresa Johnson9ba95f92016-08-11 14:58:12 +000057 // If the linker's hook returned false, we need to pass that result
58 // through.
59 if (LinkerHook && !LinkerHook(Task, M))
60 return false;
61
62 std::string PathPrefix;
63 // If this is the combined module (not a ThinLTO backend compile) or the
64 // user hasn't requested using the input module's path, emit to a file
65 // named from the provided OutputFileName with the Task ID appended.
66 if (M.getModuleIdentifier() == "ld-temp.o" || !UseInputModulePath) {
Mehdi Aminieccffad2016-08-18 00:12:33 +000067 PathPrefix = OutputFileName + utostr(Task);
Teresa Johnson9ba95f92016-08-11 14:58:12 +000068 } else
69 PathPrefix = M.getModuleIdentifier();
70 std::string Path = PathPrefix + "." + PathSuffix + ".bc";
71 std::error_code EC;
72 raw_fd_ostream OS(Path, EC, sys::fs::OpenFlags::F_None);
73 if (EC) {
74 // Because -save-temps is a debugging feature, we report the error
75 // directly and exit.
76 llvm::errs() << "failed to open " << Path << ": " << EC.message()
77 << '\n';
78 exit(1);
79 }
80 WriteBitcodeToFile(&M, OS, /*ShouldPreserveUseListOrder=*/false);
81 return true;
82 };
83 };
84
85 setHook("0.preopt", PreOptModuleHook);
86 setHook("1.promote", PostPromoteModuleHook);
87 setHook("2.internalize", PostInternalizeModuleHook);
88 setHook("3.import", PostImportModuleHook);
89 setHook("4.opt", PostOptModuleHook);
90 setHook("5.precodegen", PreCodeGenModuleHook);
91
92 CombinedIndexHook = [=](const ModuleSummaryIndex &Index) {
Mehdi Aminieccffad2016-08-18 00:12:33 +000093 std::string Path = OutputFileName + "index.bc";
Teresa Johnson9ba95f92016-08-11 14:58:12 +000094 std::error_code EC;
95 raw_fd_ostream OS(Path, EC, sys::fs::OpenFlags::F_None);
96 if (EC) {
97 // Because -save-temps is a debugging feature, we report the error
98 // directly and exit.
99 llvm::errs() << "failed to open " << Path << ": " << EC.message() << '\n';
100 exit(1);
101 }
102 WriteIndexToFile(Index, OS);
103 return true;
104 };
105
106 return Error();
107}
108
109namespace {
110
111std::unique_ptr<TargetMachine>
Davide Italiano24c29b12016-09-07 01:08:31 +0000112createTargetMachine(Config &Conf, StringRef TheTriple,
113 const Target *TheTarget) {
Teresa Johnson9ba95f92016-08-11 14:58:12 +0000114 SubtargetFeatures Features;
115 Features.getDefaultSubtargetFeatures(Triple(TheTriple));
Davide Italiano24c29b12016-09-07 01:08:31 +0000116 for (const std::string &A : Conf.MAttrs)
Teresa Johnson9ba95f92016-08-11 14:58:12 +0000117 Features.AddFeature(A);
118
119 return std::unique_ptr<TargetMachine>(TheTarget->createTargetMachine(
Davide Italiano24c29b12016-09-07 01:08:31 +0000120 TheTriple, Conf.CPU, Features.getString(), Conf.Options, Conf.RelocModel,
121 Conf.CodeModel, Conf.CGOptLevel));
Teresa Johnson9ba95f92016-08-11 14:58:12 +0000122}
123
Davide Italianoec9612d2016-09-07 17:46:16 +0000124static void runNewPMCustomPasses(Module &Mod, TargetMachine *TM,
125 std::string PipelineDesc,
126 bool DisableVerify) {
127 PassBuilder PB(TM);
128 AAManager AA;
129 LoopAnalysisManager LAM;
130 FunctionAnalysisManager FAM;
131 CGSCCAnalysisManager CGAM;
132 ModuleAnalysisManager MAM;
133
134 // Register the AA manager first so that our version is the one used.
135 FAM.registerPass([&] { return std::move(AA); });
136
137 // Register all the basic analyses with the managers.
138 PB.registerModuleAnalyses(MAM);
139 PB.registerCGSCCAnalyses(CGAM);
140 PB.registerFunctionAnalyses(FAM);
141 PB.registerLoopAnalyses(LAM);
142 PB.crossRegisterProxies(LAM, FAM, CGAM, MAM);
143
144 ModulePassManager MPM;
145
146 // Always verify the input.
147 MPM.addPass(VerifierPass());
148
149 // Now, add all the passes we've been requested to.
150 if (!PB.parsePassPipeline(MPM, PipelineDesc))
151 report_fatal_error("unable to parse pass pipeline description: " +
152 PipelineDesc);
153
154 if (!DisableVerify)
155 MPM.addPass(VerifierPass());
156 MPM.run(Mod, MAM);
157}
158
Davide Italiano24c29b12016-09-07 01:08:31 +0000159static void runOldPMPasses(Config &Conf, Module &Mod, TargetMachine *TM,
Davide Italiano1e9d3d32016-08-31 17:02:44 +0000160 bool IsThinLto) {
Teresa Johnson9ba95f92016-08-11 14:58:12 +0000161 legacy::PassManager passes;
162 passes.add(createTargetTransformInfoWrapperPass(TM->getTargetIRAnalysis()));
163
164 PassManagerBuilder PMB;
165 PMB.LibraryInfo = new TargetLibraryInfoImpl(Triple(TM->getTargetTriple()));
166 PMB.Inliner = createFunctionInliningPass();
167 // Unconditionally verify input since it is not verified before this
168 // point and has unknown origin.
169 PMB.VerifyInput = true;
Davide Italiano24c29b12016-09-07 01:08:31 +0000170 PMB.VerifyOutput = !Conf.DisableVerify;
Teresa Johnson9ba95f92016-08-11 14:58:12 +0000171 PMB.LoopVectorize = true;
172 PMB.SLPVectorize = true;
Davide Italiano24c29b12016-09-07 01:08:31 +0000173 PMB.OptLevel = Conf.OptLevel;
Teresa Johnson9ba95f92016-08-11 14:58:12 +0000174 if (IsThinLto)
175 PMB.populateThinLTOPassManager(passes);
176 else
177 PMB.populateLTOPassManager(passes);
Davide Italiano24c29b12016-09-07 01:08:31 +0000178 passes.run(Mod);
Davide Italiano1e9d3d32016-08-31 17:02:44 +0000179}
Teresa Johnson9ba95f92016-08-11 14:58:12 +0000180
Davide Italiano24c29b12016-09-07 01:08:31 +0000181bool opt(Config &Conf, TargetMachine *TM, unsigned Task, Module &Mod,
Davide Italiano1e9d3d32016-08-31 17:02:44 +0000182 bool IsThinLto) {
Davide Italiano24c29b12016-09-07 01:08:31 +0000183 Mod.setDataLayout(TM->createDataLayout());
Davide Italianoec9612d2016-09-07 17:46:16 +0000184 if (Conf.OptPipeline.empty())
185 runOldPMPasses(Conf, Mod, TM, IsThinLto);
186 else
187 runNewPMCustomPasses(Mod, TM, Conf.OptPipeline, Conf.DisableVerify);
Davide Italiano24c29b12016-09-07 01:08:31 +0000188 return !Conf.PostOptModuleHook || Conf.PostOptModuleHook(Task, Mod);
Teresa Johnson9ba95f92016-08-11 14:58:12 +0000189}
190
Mehdi Aminiadc0e262016-08-23 21:30:12 +0000191/// Monolithic LTO does not support caching (yet), this is a convenient wrapper
192/// around AddOutput to workaround this.
193static AddOutputFn getUncachedOutputWrapper(AddOutputFn &AddOutput,
194 unsigned Task) {
195 return [Task, &AddOutput](unsigned TaskId) {
196 auto Output = AddOutput(Task);
197 if (Output->isCachingEnabled() && Output->tryLoadFromCache(""))
198 report_fatal_error("Cache hit without a valid key?");
Mehdi Aminiadc0e262016-08-23 21:30:12 +0000199 assert(Task == TaskId && "Unexpexted TaskId mismatch");
200 return Output;
201 };
202}
203
Davide Italiano24c29b12016-09-07 01:08:31 +0000204void codegen(Config &Conf, TargetMachine *TM, AddOutputFn AddOutput,
205 unsigned Task, Module &Mod) {
206 if (Conf.PreCodeGenModuleHook && !Conf.PreCodeGenModuleHook(Task, Mod))
Teresa Johnson9ba95f92016-08-11 14:58:12 +0000207 return;
208
Mehdi Amini970800e2016-08-17 06:23:09 +0000209 auto Output = AddOutput(Task);
210 std::unique_ptr<raw_pwrite_stream> OS = Output->getStream();
Teresa Johnson9ba95f92016-08-11 14:58:12 +0000211 legacy::PassManager CodeGenPasses;
212 if (TM->addPassesToEmitFile(CodeGenPasses, *OS,
213 TargetMachine::CGFT_ObjectFile))
214 report_fatal_error("Failed to setup codegen");
Davide Italiano24c29b12016-09-07 01:08:31 +0000215 CodeGenPasses.run(Mod);
Teresa Johnson9ba95f92016-08-11 14:58:12 +0000216}
217
Mehdi Amini970800e2016-08-17 06:23:09 +0000218void splitCodeGen(Config &C, TargetMachine *TM, AddOutputFn AddOutput,
Teresa Johnson9ba95f92016-08-11 14:58:12 +0000219 unsigned ParallelCodeGenParallelismLevel,
Davide Italiano24c29b12016-09-07 01:08:31 +0000220 std::unique_ptr<Module> Mod) {
Teresa Johnson9ba95f92016-08-11 14:58:12 +0000221 ThreadPool CodegenThreadPool(ParallelCodeGenParallelismLevel);
222 unsigned ThreadCount = 0;
223 const Target *T = &TM->getTarget();
224
225 SplitModule(
Davide Italiano24c29b12016-09-07 01:08:31 +0000226 std::move(Mod), ParallelCodeGenParallelismLevel,
Teresa Johnson9ba95f92016-08-11 14:58:12 +0000227 [&](std::unique_ptr<Module> MPart) {
228 // We want to clone the module in a new context to multi-thread the
229 // codegen. We do it by serializing partition modules to bitcode
230 // (while still on the main thread, in order to avoid data races) and
231 // spinning up new threads which deserialize the partitions into
232 // separate contexts.
233 // FIXME: Provide a more direct way to do this in LLVM.
234 SmallString<0> BC;
235 raw_svector_ostream BCOS(BC);
236 WriteBitcodeToFile(MPart.get(), BCOS);
237
238 // Enqueue the task
239 CodegenThreadPool.async(
240 [&](const SmallString<0> &BC, unsigned ThreadId) {
241 LTOLLVMContext Ctx(C);
242 ErrorOr<std::unique_ptr<Module>> MOrErr = parseBitcodeFile(
243 MemoryBufferRef(StringRef(BC.data(), BC.size()), "ld-temp.o"),
244 Ctx);
245 if (!MOrErr)
246 report_fatal_error("Failed to read bitcode");
247 std::unique_ptr<Module> MPartInCtx = std::move(MOrErr.get());
248
249 std::unique_ptr<TargetMachine> TM =
250 createTargetMachine(C, MPartInCtx->getTargetTriple(), T);
Mehdi Aminiadc0e262016-08-23 21:30:12 +0000251
252 codegen(C, TM.get(),
253 getUncachedOutputWrapper(AddOutput, ThreadId), ThreadId,
254 *MPartInCtx);
Teresa Johnson9ba95f92016-08-11 14:58:12 +0000255 },
256 // Pass BC using std::move to ensure that it get moved rather than
257 // copied into the thread's context.
258 std::move(BC), ThreadCount++);
259 },
260 false);
261}
262
Davide Italiano24c29b12016-09-07 01:08:31 +0000263Expected<const Target *> initAndLookupTarget(Config &C, Module &Mod) {
Teresa Johnson9ba95f92016-08-11 14:58:12 +0000264 if (!C.OverrideTriple.empty())
Davide Italiano24c29b12016-09-07 01:08:31 +0000265 Mod.setTargetTriple(C.OverrideTriple);
266 else if (Mod.getTargetTriple().empty())
267 Mod.setTargetTriple(C.DefaultTriple);
Teresa Johnson9ba95f92016-08-11 14:58:12 +0000268
269 std::string Msg;
Davide Italiano24c29b12016-09-07 01:08:31 +0000270 const Target *T = TargetRegistry::lookupTarget(Mod.getTargetTriple(), Msg);
Teresa Johnson9ba95f92016-08-11 14:58:12 +0000271 if (!T)
272 return make_error<StringError>(Msg, inconvertibleErrorCode());
273 return T;
274}
275
276}
277
Mehdi Amini970800e2016-08-17 06:23:09 +0000278Error lto::backend(Config &C, AddOutputFn AddOutput,
Teresa Johnson9ba95f92016-08-11 14:58:12 +0000279 unsigned ParallelCodeGenParallelismLevel,
Davide Italiano24c29b12016-09-07 01:08:31 +0000280 std::unique_ptr<Module> Mod) {
281 Expected<const Target *> TOrErr = initAndLookupTarget(C, *Mod);
Teresa Johnson9ba95f92016-08-11 14:58:12 +0000282 if (!TOrErr)
283 return TOrErr.takeError();
284
285 std::unique_ptr<TargetMachine> TM =
Davide Italiano24c29b12016-09-07 01:08:31 +0000286 createTargetMachine(C, Mod->getTargetTriple(), *TOrErr);
Teresa Johnson9ba95f92016-08-11 14:58:12 +0000287
Mehdi Aminid310b472016-08-22 06:25:41 +0000288 if (!C.CodeGenOnly)
Davide Italiano24c29b12016-09-07 01:08:31 +0000289 if (!opt(C, TM.get(), 0, *Mod, /*IsThinLto=*/false))
Mehdi Aminid310b472016-08-22 06:25:41 +0000290 return Error();
Teresa Johnson9ba95f92016-08-11 14:58:12 +0000291
Mehdi Aminiadc0e262016-08-23 21:30:12 +0000292 if (ParallelCodeGenParallelismLevel == 1) {
Davide Italiano24c29b12016-09-07 01:08:31 +0000293 codegen(C, TM.get(), getUncachedOutputWrapper(AddOutput, 0), 0, *Mod);
Mehdi Aminiadc0e262016-08-23 21:30:12 +0000294 } else {
Mehdi Amini970800e2016-08-17 06:23:09 +0000295 splitCodeGen(C, TM.get(), AddOutput, ParallelCodeGenParallelismLevel,
Davide Italiano24c29b12016-09-07 01:08:31 +0000296 std::move(Mod));
Mehdi Aminiadc0e262016-08-23 21:30:12 +0000297 }
Teresa Johnson9ba95f92016-08-11 14:58:12 +0000298 return Error();
299}
300
Mehdi Amini970800e2016-08-17 06:23:09 +0000301Error lto::thinBackend(Config &Conf, unsigned Task, AddOutputFn AddOutput,
Mehdi Aminiacc50c42016-08-16 00:44:46 +0000302 Module &Mod, ModuleSummaryIndex &CombinedIndex,
Teresa Johnson9ba95f92016-08-11 14:58:12 +0000303 const FunctionImporter::ImportMapTy &ImportList,
304 const GVSummaryMapTy &DefinedGlobals,
305 MapVector<StringRef, MemoryBufferRef> &ModuleMap) {
Mehdi Aminiacc50c42016-08-16 00:44:46 +0000306 Expected<const Target *> TOrErr = initAndLookupTarget(Conf, Mod);
Teresa Johnson9ba95f92016-08-11 14:58:12 +0000307 if (!TOrErr)
308 return TOrErr.takeError();
309
310 std::unique_ptr<TargetMachine> TM =
Mehdi Aminiacc50c42016-08-16 00:44:46 +0000311 createTargetMachine(Conf, Mod.getTargetTriple(), *TOrErr);
Teresa Johnson9ba95f92016-08-11 14:58:12 +0000312
Mehdi Aminid310b472016-08-22 06:25:41 +0000313 if (Conf.CodeGenOnly) {
314 codegen(Conf, TM.get(), AddOutput, Task, Mod);
315 return Error();
316 }
317
Mehdi Aminiacc50c42016-08-16 00:44:46 +0000318 if (Conf.PreOptModuleHook && !Conf.PreOptModuleHook(Task, Mod))
Teresa Johnson9ba95f92016-08-11 14:58:12 +0000319 return Error();
320
Mehdi Aminiacc50c42016-08-16 00:44:46 +0000321 renameModuleForThinLTO(Mod, CombinedIndex);
Teresa Johnson9ba95f92016-08-11 14:58:12 +0000322
Mehdi Amini8ac7b322016-08-18 00:59:24 +0000323 thinLTOResolveWeakForLinkerModule(Mod, DefinedGlobals);
324
Mehdi Aminiacc50c42016-08-16 00:44:46 +0000325 if (Conf.PostPromoteModuleHook && !Conf.PostPromoteModuleHook(Task, Mod))
Teresa Johnson9ba95f92016-08-11 14:58:12 +0000326 return Error();
327
328 if (!DefinedGlobals.empty())
Mehdi Aminiacc50c42016-08-16 00:44:46 +0000329 thinLTOInternalizeModule(Mod, DefinedGlobals);
Teresa Johnson9ba95f92016-08-11 14:58:12 +0000330
Mehdi Aminiacc50c42016-08-16 00:44:46 +0000331 if (Conf.PostInternalizeModuleHook &&
332 !Conf.PostInternalizeModuleHook(Task, Mod))
Teresa Johnson9ba95f92016-08-11 14:58:12 +0000333 return Error();
334
335 auto ModuleLoader = [&](StringRef Identifier) {
Mehdi Amini9ec5a612016-08-23 16:53:34 +0000336 assert(Mod.getContext().isODRUniquingDebugTypes() &&
337 "ODR Type uniquing shoudl be enabled on the context");
Teresa Johnson9ba95f92016-08-11 14:58:12 +0000338 return std::move(getLazyBitcodeModule(MemoryBuffer::getMemBuffer(
339 ModuleMap[Identifier], false),
Mehdi Aminiacc50c42016-08-16 00:44:46 +0000340 Mod.getContext(),
Teresa Johnson9ba95f92016-08-11 14:58:12 +0000341 /*ShouldLazyLoadMetadata=*/true)
342 .get());
343 };
344
345 FunctionImporter Importer(CombinedIndex, ModuleLoader);
Mehdi Aminiacc50c42016-08-16 00:44:46 +0000346 Importer.importFunctions(Mod, ImportList);
Teresa Johnson9ba95f92016-08-11 14:58:12 +0000347
Mehdi Aminiacc50c42016-08-16 00:44:46 +0000348 if (Conf.PostImportModuleHook && !Conf.PostImportModuleHook(Task, Mod))
Teresa Johnson9ba95f92016-08-11 14:58:12 +0000349 return Error();
350
Mehdi Aminiacc50c42016-08-16 00:44:46 +0000351 if (!opt(Conf, TM.get(), Task, Mod, /*IsThinLto=*/true))
Teresa Johnson9ba95f92016-08-11 14:58:12 +0000352 return Error();
353
Mehdi Amini970800e2016-08-17 06:23:09 +0000354 codegen(Conf, TM.get(), AddOutput, Task, Mod);
Teresa Johnson9ba95f92016-08-11 14:58:12 +0000355 return Error();
356}