Revert r354244 "[DAGCombiner] Eliminate dead stores to stack."
[llvm-complete.git] / lib / LTO / LTOBackend.cpp
blob02e51fbfa35734d2bce29c9508a816bebe10bde8
1 //===-LTOBackend.cpp - LLVM Link Time Optimizer Backend -------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements the "backend" phase of LTO, i.e. it performs
10 // optimization and code generation on a loaded module. It is generally used
11 // internally by the LTO class but can also be used independently, for example
12 // to implement a standalone ThinLTO backend.
14 //===----------------------------------------------------------------------===//
16 #include "llvm/LTO/LTOBackend.h"
17 #include "llvm/Analysis/AliasAnalysis.h"
18 #include "llvm/Analysis/CGSCCPassManager.h"
19 #include "llvm/Analysis/TargetLibraryInfo.h"
20 #include "llvm/Analysis/TargetTransformInfo.h"
21 #include "llvm/Bitcode/BitcodeReader.h"
22 #include "llvm/Bitcode/BitcodeWriter.h"
23 #include "llvm/IR/LegacyPassManager.h"
24 #include "llvm/IR/PassManager.h"
25 #include "llvm/IR/Verifier.h"
26 #include "llvm/LTO/LTO.h"
27 #include "llvm/MC/SubtargetFeature.h"
28 #include "llvm/Object/ModuleSymbolTable.h"
29 #include "llvm/Passes/PassBuilder.h"
30 #include "llvm/Support/Error.h"
31 #include "llvm/Support/FileSystem.h"
32 #include "llvm/Support/MemoryBuffer.h"
33 #include "llvm/Support/Path.h"
34 #include "llvm/Support/Program.h"
35 #include "llvm/Support/raw_ostream.h"
36 #include "llvm/Support/TargetRegistry.h"
37 #include "llvm/Support/ThreadPool.h"
38 #include "llvm/Target/TargetMachine.h"
39 #include "llvm/Transforms/IPO.h"
40 #include "llvm/Transforms/IPO/PassManagerBuilder.h"
41 #include "llvm/Transforms/Scalar/LoopPassManager.h"
42 #include "llvm/Transforms/Utils/FunctionImportUtils.h"
43 #include "llvm/Transforms/Utils/SplitModule.h"
45 using namespace llvm;
46 using namespace lto;
48 LLVM_ATTRIBUTE_NORETURN static void reportOpenError(StringRef Path, Twine Msg) {
49 errs() << "failed to open " << Path << ": " << Msg << '\n';
50 errs().flush();
51 exit(1);
54 Error Config::addSaveTemps(std::string OutputFileName,
55 bool UseInputModulePath) {
56 ShouldDiscardValueNames = false;
58 std::error_code EC;
59 ResolutionFile = llvm::make_unique<raw_fd_ostream>(
60 OutputFileName + "resolution.txt", EC, sys::fs::OpenFlags::F_Text);
61 if (EC)
62 return errorCodeToError(EC);
64 auto setHook = [&](std::string PathSuffix, ModuleHookFn &Hook) {
65 // Keep track of the hook provided by the linker, which also needs to run.
66 ModuleHookFn LinkerHook = Hook;
67 Hook = [=](unsigned Task, const Module &M) {
68 // If the linker's hook returned false, we need to pass that result
69 // through.
70 if (LinkerHook && !LinkerHook(Task, M))
71 return false;
73 std::string PathPrefix;
74 // If this is the combined module (not a ThinLTO backend compile) or the
75 // user hasn't requested using the input module's path, emit to a file
76 // named from the provided OutputFileName with the Task ID appended.
77 if (M.getModuleIdentifier() == "ld-temp.o" || !UseInputModulePath) {
78 PathPrefix = OutputFileName;
79 if (Task != (unsigned)-1)
80 PathPrefix += utostr(Task) + ".";
81 } else
82 PathPrefix = M.getModuleIdentifier() + ".";
83 std::string Path = PathPrefix + PathSuffix + ".bc";
84 std::error_code EC;
85 raw_fd_ostream OS(Path, EC, sys::fs::OpenFlags::F_None);
86 // Because -save-temps is a debugging feature, we report the error
87 // directly and exit.
88 if (EC)
89 reportOpenError(Path, EC.message());
90 WriteBitcodeToFile(M, OS, /*ShouldPreserveUseListOrder=*/false);
91 return true;
95 setHook("0.preopt", PreOptModuleHook);
96 setHook("1.promote", PostPromoteModuleHook);
97 setHook("2.internalize", PostInternalizeModuleHook);
98 setHook("3.import", PostImportModuleHook);
99 setHook("4.opt", PostOptModuleHook);
100 setHook("5.precodegen", PreCodeGenModuleHook);
102 CombinedIndexHook = [=](const ModuleSummaryIndex &Index) {
103 std::string Path = OutputFileName + "index.bc";
104 std::error_code EC;
105 raw_fd_ostream OS(Path, EC, sys::fs::OpenFlags::F_None);
106 // Because -save-temps is a debugging feature, we report the error
107 // directly and exit.
108 if (EC)
109 reportOpenError(Path, EC.message());
110 WriteIndexToFile(Index, OS);
112 Path = OutputFileName + "index.dot";
113 raw_fd_ostream OSDot(Path, EC, sys::fs::OpenFlags::F_None);
114 if (EC)
115 reportOpenError(Path, EC.message());
116 Index.exportToDot(OSDot);
117 return true;
120 return Error::success();
123 namespace {
125 std::unique_ptr<TargetMachine>
126 createTargetMachine(Config &Conf, const Target *TheTarget, Module &M) {
127 StringRef TheTriple = M.getTargetTriple();
128 SubtargetFeatures Features;
129 Features.getDefaultSubtargetFeatures(Triple(TheTriple));
130 for (const std::string &A : Conf.MAttrs)
131 Features.AddFeature(A);
133 Reloc::Model RelocModel;
134 if (Conf.RelocModel)
135 RelocModel = *Conf.RelocModel;
136 else
137 RelocModel =
138 M.getPICLevel() == PICLevel::NotPIC ? Reloc::Static : Reloc::PIC_;
140 Optional<CodeModel::Model> CodeModel;
141 if (Conf.CodeModel)
142 CodeModel = *Conf.CodeModel;
143 else
144 CodeModel = M.getCodeModel();
146 return std::unique_ptr<TargetMachine>(TheTarget->createTargetMachine(
147 TheTriple, Conf.CPU, Features.getString(), Conf.Options, RelocModel,
148 CodeModel, Conf.CGOptLevel));
151 static void runNewPMPasses(Config &Conf, Module &Mod, TargetMachine *TM,
152 unsigned OptLevel, bool IsThinLTO,
153 ModuleSummaryIndex *ExportSummary,
154 const ModuleSummaryIndex *ImportSummary) {
155 Optional<PGOOptions> PGOOpt;
156 if (!Conf.SampleProfile.empty())
157 PGOOpt = PGOOptions("", "", Conf.SampleProfile, Conf.ProfileRemapping,
158 false, true);
160 PassBuilder PB(TM, PGOOpt);
161 AAManager AA;
163 // Parse a custom AA pipeline if asked to.
164 if (auto Err = PB.parseAAPipeline(AA, "default"))
165 report_fatal_error("Error parsing default AA pipeline");
167 LoopAnalysisManager LAM(Conf.DebugPassManager);
168 FunctionAnalysisManager FAM(Conf.DebugPassManager);
169 CGSCCAnalysisManager CGAM(Conf.DebugPassManager);
170 ModuleAnalysisManager MAM(Conf.DebugPassManager);
172 // Register the AA manager first so that our version is the one used.
173 FAM.registerPass([&] { return std::move(AA); });
175 // Register all the basic analyses with the managers.
176 PB.registerModuleAnalyses(MAM);
177 PB.registerCGSCCAnalyses(CGAM);
178 PB.registerFunctionAnalyses(FAM);
179 PB.registerLoopAnalyses(LAM);
180 PB.crossRegisterProxies(LAM, FAM, CGAM, MAM);
182 ModulePassManager MPM(Conf.DebugPassManager);
183 // FIXME (davide): verify the input.
185 PassBuilder::OptimizationLevel OL;
187 switch (OptLevel) {
188 default:
189 llvm_unreachable("Invalid optimization level");
190 case 0:
191 OL = PassBuilder::O0;
192 break;
193 case 1:
194 OL = PassBuilder::O1;
195 break;
196 case 2:
197 OL = PassBuilder::O2;
198 break;
199 case 3:
200 OL = PassBuilder::O3;
201 break;
204 if (IsThinLTO)
205 MPM = PB.buildThinLTODefaultPipeline(OL, Conf.DebugPassManager,
206 ImportSummary);
207 else
208 MPM = PB.buildLTODefaultPipeline(OL, Conf.DebugPassManager, ExportSummary);
209 MPM.run(Mod, MAM);
211 // FIXME (davide): verify the output.
214 static void runNewPMCustomPasses(Module &Mod, TargetMachine *TM,
215 std::string PipelineDesc,
216 std::string AAPipelineDesc,
217 bool DisableVerify) {
218 PassBuilder PB(TM);
219 AAManager AA;
221 // Parse a custom AA pipeline if asked to.
222 if (!AAPipelineDesc.empty())
223 if (auto Err = PB.parseAAPipeline(AA, AAPipelineDesc))
224 report_fatal_error("unable to parse AA pipeline description '" +
225 AAPipelineDesc + "': " + toString(std::move(Err)));
227 LoopAnalysisManager LAM;
228 FunctionAnalysisManager FAM;
229 CGSCCAnalysisManager CGAM;
230 ModuleAnalysisManager MAM;
232 // Register the AA manager first so that our version is the one used.
233 FAM.registerPass([&] { return std::move(AA); });
235 // Register all the basic analyses with the managers.
236 PB.registerModuleAnalyses(MAM);
237 PB.registerCGSCCAnalyses(CGAM);
238 PB.registerFunctionAnalyses(FAM);
239 PB.registerLoopAnalyses(LAM);
240 PB.crossRegisterProxies(LAM, FAM, CGAM, MAM);
242 ModulePassManager MPM;
244 // Always verify the input.
245 MPM.addPass(VerifierPass());
247 // Now, add all the passes we've been requested to.
248 if (auto Err = PB.parsePassPipeline(MPM, PipelineDesc))
249 report_fatal_error("unable to parse pass pipeline description '" +
250 PipelineDesc + "': " + toString(std::move(Err)));
252 if (!DisableVerify)
253 MPM.addPass(VerifierPass());
254 MPM.run(Mod, MAM);
257 static void runOldPMPasses(Config &Conf, Module &Mod, TargetMachine *TM,
258 bool IsThinLTO, ModuleSummaryIndex *ExportSummary,
259 const ModuleSummaryIndex *ImportSummary) {
260 legacy::PassManager passes;
261 passes.add(createTargetTransformInfoWrapperPass(TM->getTargetIRAnalysis()));
263 PassManagerBuilder PMB;
264 PMB.LibraryInfo = new TargetLibraryInfoImpl(Triple(TM->getTargetTriple()));
265 PMB.Inliner = createFunctionInliningPass();
266 PMB.ExportSummary = ExportSummary;
267 PMB.ImportSummary = ImportSummary;
268 // Unconditionally verify input since it is not verified before this
269 // point and has unknown origin.
270 PMB.VerifyInput = true;
271 PMB.VerifyOutput = !Conf.DisableVerify;
272 PMB.LoopVectorize = true;
273 PMB.SLPVectorize = true;
274 PMB.OptLevel = Conf.OptLevel;
275 PMB.PGOSampleUse = Conf.SampleProfile;
276 if (IsThinLTO)
277 PMB.populateThinLTOPassManager(passes);
278 else
279 PMB.populateLTOPassManager(passes);
280 passes.run(Mod);
283 bool opt(Config &Conf, TargetMachine *TM, unsigned Task, Module &Mod,
284 bool IsThinLTO, ModuleSummaryIndex *ExportSummary,
285 const ModuleSummaryIndex *ImportSummary) {
286 // FIXME: Plumb the combined index into the new pass manager.
287 if (!Conf.OptPipeline.empty())
288 runNewPMCustomPasses(Mod, TM, Conf.OptPipeline, Conf.AAPipeline,
289 Conf.DisableVerify);
290 else if (Conf.UseNewPM)
291 runNewPMPasses(Conf, Mod, TM, Conf.OptLevel, IsThinLTO, ExportSummary,
292 ImportSummary);
293 else
294 runOldPMPasses(Conf, Mod, TM, IsThinLTO, ExportSummary, ImportSummary);
295 return !Conf.PostOptModuleHook || Conf.PostOptModuleHook(Task, Mod);
298 void codegen(Config &Conf, TargetMachine *TM, AddStreamFn AddStream,
299 unsigned Task, Module &Mod) {
300 if (Conf.PreCodeGenModuleHook && !Conf.PreCodeGenModuleHook(Task, Mod))
301 return;
303 std::unique_ptr<ToolOutputFile> DwoOut;
304 SmallString<1024> DwoFile(Conf.DwoPath);
305 if (!Conf.DwoDir.empty()) {
306 std::error_code EC;
307 if (auto EC = llvm::sys::fs::create_directories(Conf.DwoDir))
308 report_fatal_error("Failed to create directory " + Conf.DwoDir + ": " +
309 EC.message());
311 DwoFile = Conf.DwoDir;
312 sys::path::append(DwoFile, std::to_string(Task) + ".dwo");
315 if (!DwoFile.empty()) {
316 std::error_code EC;
317 TM->Options.MCOptions.SplitDwarfFile = DwoFile.str().str();
318 DwoOut = llvm::make_unique<ToolOutputFile>(DwoFile, EC, sys::fs::F_None);
319 if (EC)
320 report_fatal_error("Failed to open " + DwoFile + ": " + EC.message());
323 auto Stream = AddStream(Task);
324 legacy::PassManager CodeGenPasses;
325 if (TM->addPassesToEmitFile(CodeGenPasses, *Stream->OS,
326 DwoOut ? &DwoOut->os() : nullptr,
327 Conf.CGFileType))
328 report_fatal_error("Failed to setup codegen");
329 CodeGenPasses.run(Mod);
331 if (DwoOut)
332 DwoOut->keep();
335 void splitCodeGen(Config &C, TargetMachine *TM, AddStreamFn AddStream,
336 unsigned ParallelCodeGenParallelismLevel,
337 std::unique_ptr<Module> Mod) {
338 ThreadPool CodegenThreadPool(ParallelCodeGenParallelismLevel);
339 unsigned ThreadCount = 0;
340 const Target *T = &TM->getTarget();
342 SplitModule(
343 std::move(Mod), ParallelCodeGenParallelismLevel,
344 [&](std::unique_ptr<Module> MPart) {
345 // We want to clone the module in a new context to multi-thread the
346 // codegen. We do it by serializing partition modules to bitcode
347 // (while still on the main thread, in order to avoid data races) and
348 // spinning up new threads which deserialize the partitions into
349 // separate contexts.
350 // FIXME: Provide a more direct way to do this in LLVM.
351 SmallString<0> BC;
352 raw_svector_ostream BCOS(BC);
353 WriteBitcodeToFile(*MPart, BCOS);
355 // Enqueue the task
356 CodegenThreadPool.async(
357 [&](const SmallString<0> &BC, unsigned ThreadId) {
358 LTOLLVMContext Ctx(C);
359 Expected<std::unique_ptr<Module>> MOrErr = parseBitcodeFile(
360 MemoryBufferRef(StringRef(BC.data(), BC.size()), "ld-temp.o"),
361 Ctx);
362 if (!MOrErr)
363 report_fatal_error("Failed to read bitcode");
364 std::unique_ptr<Module> MPartInCtx = std::move(MOrErr.get());
366 std::unique_ptr<TargetMachine> TM =
367 createTargetMachine(C, T, *MPartInCtx);
369 codegen(C, TM.get(), AddStream, ThreadId, *MPartInCtx);
371 // Pass BC using std::move to ensure that it get moved rather than
372 // copied into the thread's context.
373 std::move(BC), ThreadCount++);
375 false);
377 // Because the inner lambda (which runs in a worker thread) captures our local
378 // variables, we need to wait for the worker threads to terminate before we
379 // can leave the function scope.
380 CodegenThreadPool.wait();
383 Expected<const Target *> initAndLookupTarget(Config &C, Module &Mod) {
384 if (!C.OverrideTriple.empty())
385 Mod.setTargetTriple(C.OverrideTriple);
386 else if (Mod.getTargetTriple().empty())
387 Mod.setTargetTriple(C.DefaultTriple);
389 std::string Msg;
390 const Target *T = TargetRegistry::lookupTarget(Mod.getTargetTriple(), Msg);
391 if (!T)
392 return make_error<StringError>(Msg, inconvertibleErrorCode());
393 return T;
398 static Error
399 finalizeOptimizationRemarks(std::unique_ptr<ToolOutputFile> DiagOutputFile) {
400 // Make sure we flush the diagnostic remarks file in case the linker doesn't
401 // call the global destructors before exiting.
402 if (!DiagOutputFile)
403 return Error::success();
404 DiagOutputFile->keep();
405 DiagOutputFile->os().flush();
406 return Error::success();
409 Error lto::backend(Config &C, AddStreamFn AddStream,
410 unsigned ParallelCodeGenParallelismLevel,
411 std::unique_ptr<Module> Mod,
412 ModuleSummaryIndex &CombinedIndex) {
413 Expected<const Target *> TOrErr = initAndLookupTarget(C, *Mod);
414 if (!TOrErr)
415 return TOrErr.takeError();
417 std::unique_ptr<TargetMachine> TM = createTargetMachine(C, *TOrErr, *Mod);
419 // Setup optimization remarks.
420 auto DiagFileOrErr = lto::setupOptimizationRemarks(
421 Mod->getContext(), C.RemarksFilename, C.RemarksWithHotness);
422 if (!DiagFileOrErr)
423 return DiagFileOrErr.takeError();
424 auto DiagnosticOutputFile = std::move(*DiagFileOrErr);
426 if (!C.CodeGenOnly) {
427 if (!opt(C, TM.get(), 0, *Mod, /*IsThinLTO=*/false,
428 /*ExportSummary=*/&CombinedIndex, /*ImportSummary=*/nullptr))
429 return finalizeOptimizationRemarks(std::move(DiagnosticOutputFile));
432 if (ParallelCodeGenParallelismLevel == 1) {
433 codegen(C, TM.get(), AddStream, 0, *Mod);
434 } else {
435 splitCodeGen(C, TM.get(), AddStream, ParallelCodeGenParallelismLevel,
436 std::move(Mod));
438 return finalizeOptimizationRemarks(std::move(DiagnosticOutputFile));
441 static void dropDeadSymbols(Module &Mod, const GVSummaryMapTy &DefinedGlobals,
442 const ModuleSummaryIndex &Index) {
443 std::vector<GlobalValue*> DeadGVs;
444 for (auto &GV : Mod.global_values())
445 if (GlobalValueSummary *GVS = DefinedGlobals.lookup(GV.getGUID()))
446 if (!Index.isGlobalValueLive(GVS)) {
447 DeadGVs.push_back(&GV);
448 convertToDeclaration(GV);
451 // Now that all dead bodies have been dropped, delete the actual objects
452 // themselves when possible.
453 for (GlobalValue *GV : DeadGVs) {
454 GV->removeDeadConstantUsers();
455 // Might reference something defined in native object (i.e. dropped a
456 // non-prevailing IR def, but we need to keep the declaration).
457 if (GV->use_empty())
458 GV->eraseFromParent();
462 Error lto::thinBackend(Config &Conf, unsigned Task, AddStreamFn AddStream,
463 Module &Mod, const ModuleSummaryIndex &CombinedIndex,
464 const FunctionImporter::ImportMapTy &ImportList,
465 const GVSummaryMapTy &DefinedGlobals,
466 MapVector<StringRef, BitcodeModule> &ModuleMap) {
467 Expected<const Target *> TOrErr = initAndLookupTarget(Conf, Mod);
468 if (!TOrErr)
469 return TOrErr.takeError();
471 std::unique_ptr<TargetMachine> TM = createTargetMachine(Conf, *TOrErr, Mod);
473 // Setup optimization remarks.
474 auto DiagFileOrErr = lto::setupOptimizationRemarks(
475 Mod.getContext(), Conf.RemarksFilename, Conf.RemarksWithHotness, Task);
476 if (!DiagFileOrErr)
477 return DiagFileOrErr.takeError();
478 auto DiagnosticOutputFile = std::move(*DiagFileOrErr);
480 if (Conf.CodeGenOnly) {
481 codegen(Conf, TM.get(), AddStream, Task, Mod);
482 return finalizeOptimizationRemarks(std::move(DiagnosticOutputFile));
485 if (Conf.PreOptModuleHook && !Conf.PreOptModuleHook(Task, Mod))
486 return finalizeOptimizationRemarks(std::move(DiagnosticOutputFile));
488 renameModuleForThinLTO(Mod, CombinedIndex);
490 dropDeadSymbols(Mod, DefinedGlobals, CombinedIndex);
492 thinLTOResolvePrevailingInModule(Mod, DefinedGlobals);
494 if (Conf.PostPromoteModuleHook && !Conf.PostPromoteModuleHook(Task, Mod))
495 return finalizeOptimizationRemarks(std::move(DiagnosticOutputFile));
497 if (!DefinedGlobals.empty())
498 thinLTOInternalizeModule(Mod, DefinedGlobals);
500 if (Conf.PostInternalizeModuleHook &&
501 !Conf.PostInternalizeModuleHook(Task, Mod))
502 return finalizeOptimizationRemarks(std::move(DiagnosticOutputFile));
504 auto ModuleLoader = [&](StringRef Identifier) {
505 assert(Mod.getContext().isODRUniquingDebugTypes() &&
506 "ODR Type uniquing should be enabled on the context");
507 auto I = ModuleMap.find(Identifier);
508 assert(I != ModuleMap.end());
509 return I->second.getLazyModule(Mod.getContext(),
510 /*ShouldLazyLoadMetadata=*/true,
511 /*IsImporting*/ true);
514 FunctionImporter Importer(CombinedIndex, ModuleLoader);
515 if (Error Err = Importer.importFunctions(Mod, ImportList).takeError())
516 return Err;
518 if (Conf.PostImportModuleHook && !Conf.PostImportModuleHook(Task, Mod))
519 return finalizeOptimizationRemarks(std::move(DiagnosticOutputFile));
521 if (!opt(Conf, TM.get(), Task, Mod, /*IsThinLTO=*/true,
522 /*ExportSummary=*/nullptr, /*ImportSummary=*/&CombinedIndex))
523 return finalizeOptimizationRemarks(std::move(DiagnosticOutputFile));
525 codegen(Conf, TM.get(), AddStream, Task, Mod);
526 return finalizeOptimizationRemarks(std::move(DiagnosticOutputFile));