LLVM  7.0.0svn
LTOBackend.cpp
Go to the documentation of this file.
1 //===-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"
25 #include "llvm/IR/PassManager.h"
26 #include "llvm/IR/Verifier.h"
27 #include "llvm/LTO/LTO.h"
31 #include "llvm/Support/Error.h"
34 #include "llvm/Support/Path.h"
35 #include "llvm/Support/Program.h"
40 #include "llvm/Transforms/IPO.h"
45 
46 using namespace llvm;
47 using namespace lto;
48 
50  errs() << "failed to open " << Path << ": " << Msg << '\n';
51  errs().flush();
52  exit(1);
53 }
54 
55 Error Config::addSaveTemps(std::string OutputFileName,
56  bool UseInputModulePath) {
58 
59  std::error_code EC;
60  ResolutionFile = llvm::make_unique<raw_fd_ostream>(
61  OutputFileName + "resolution.txt", EC, sys::fs::OpenFlags::F_Text);
62  if (EC)
63  return errorCodeToError(EC);
64 
65  auto setHook = [&](std::string PathSuffix, ModuleHookFn &Hook) {
66  // Keep track of the hook provided by the linker, which also needs to run.
67  ModuleHookFn LinkerHook = Hook;
68  Hook = [=](unsigned Task, const Module &M) {
69  // If the linker's hook returned false, we need to pass that result
70  // through.
71  if (LinkerHook && !LinkerHook(Task, M))
72  return false;
73 
74  std::string PathPrefix;
75  // If this is the combined module (not a ThinLTO backend compile) or the
76  // user hasn't requested using the input module's path, emit to a file
77  // named from the provided OutputFileName with the Task ID appended.
78  if (M.getModuleIdentifier() == "ld-temp.o" || !UseInputModulePath) {
79  PathPrefix = OutputFileName + utostr(Task);
80  } else
81  PathPrefix = M.getModuleIdentifier();
82  std::string Path = PathPrefix + "." + PathSuffix + ".bc";
83  std::error_code EC;
85  // Because -save-temps is a debugging feature, we report the error
86  // directly and exit.
87  if (EC)
88  reportOpenError(Path, EC.message());
89  WriteBitcodeToFile(M, OS, /*ShouldPreserveUseListOrder=*/false);
90  return true;
91  };
92  };
93 
94  setHook("0.preopt", PreOptModuleHook);
95  setHook("1.promote", PostPromoteModuleHook);
96  setHook("2.internalize", PostInternalizeModuleHook);
97  setHook("3.import", PostImportModuleHook);
98  setHook("4.opt", PostOptModuleHook);
99  setHook("5.precodegen", PreCodeGenModuleHook);
100 
102  std::string Path = OutputFileName + "index.bc";
103  std::error_code EC;
105  // Because -save-temps is a debugging feature, we report the error
106  // directly and exit.
107  if (EC)
108  reportOpenError(Path, EC.message());
109  WriteIndexToFile(Index, OS);
110 
111  Path = OutputFileName + "index.dot";
113  if (EC)
114  reportOpenError(Path, EC.message());
115  Index.exportToDot(OSDot);
116  return true;
117  };
118 
119  return Error::success();
120 }
121 
122 namespace {
123 
124 std::unique_ptr<TargetMachine>
125 createTargetMachine(Config &Conf, const Target *TheTarget, Module &M) {
126  StringRef TheTriple = M.getTargetTriple();
128  Features.getDefaultSubtargetFeatures(Triple(TheTriple));
129  for (const std::string &A : Conf.MAttrs)
130  Features.AddFeature(A);
131 
133  if (Conf.RelocModel)
134  RelocModel = *Conf.RelocModel;
135  else
136  RelocModel =
138 
139  return std::unique_ptr<TargetMachine>(TheTarget->createTargetMachine(
140  TheTriple, Conf.CPU, Features.getString(), Conf.Options, RelocModel,
141  Conf.CodeModel, Conf.CGOptLevel));
142 }
143 
144 static void runNewPMPasses(Config &Conf, Module &Mod, TargetMachine *TM,
145  unsigned OptLevel, bool IsThinLTO) {
146  Optional<PGOOptions> PGOOpt;
147  if (!Conf.SampleProfile.empty())
148  PGOOpt = PGOOptions("", "", Conf.SampleProfile, false, true);
149 
150  PassBuilder PB(TM, PGOOpt);
151  AAManager AA;
152 
153  // Parse a custom AA pipeline if asked to.
154  if (!PB.parseAAPipeline(AA, "default"))
155  report_fatal_error("Error parsing default AA pipeline");
156 
161 
162  // Register the AA manager first so that our version is the one used.
163  FAM.registerPass([&] { return std::move(AA); });
164 
165  // Register all the basic analyses with the managers.
166  PB.registerModuleAnalyses(MAM);
167  PB.registerCGSCCAnalyses(CGAM);
168  PB.registerFunctionAnalyses(FAM);
169  PB.registerLoopAnalyses(LAM);
170  PB.crossRegisterProxies(LAM, FAM, CGAM, MAM);
171 
173  // FIXME (davide): verify the input.
174 
176 
177  switch (OptLevel) {
178  default:
179  llvm_unreachable("Invalid optimization level");
180  case 0:
181  OL = PassBuilder::O0;
182  break;
183  case 1:
184  OL = PassBuilder::O1;
185  break;
186  case 2:
187  OL = PassBuilder::O2;
188  break;
189  case 3:
190  OL = PassBuilder::O3;
191  break;
192  }
193 
194  if (IsThinLTO)
196  else
197  MPM = PB.buildLTODefaultPipeline(OL, Conf.DebugPassManager);
198  MPM.run(Mod, MAM);
199 
200  // FIXME (davide): verify the output.
201 }
202 
203 static void runNewPMCustomPasses(Module &Mod, TargetMachine *TM,
204  std::string PipelineDesc,
205  std::string AAPipelineDesc,
206  bool DisableVerify) {
207  PassBuilder PB(TM);
208  AAManager AA;
209 
210  // Parse a custom AA pipeline if asked to.
211  if (!AAPipelineDesc.empty())
212  if (!PB.parseAAPipeline(AA, AAPipelineDesc))
213  report_fatal_error("unable to parse AA pipeline description: " +
214  AAPipelineDesc);
215 
220 
221  // Register the AA manager first so that our version is the one used.
222  FAM.registerPass([&] { return std::move(AA); });
223 
224  // Register all the basic analyses with the managers.
225  PB.registerModuleAnalyses(MAM);
226  PB.registerCGSCCAnalyses(CGAM);
227  PB.registerFunctionAnalyses(FAM);
228  PB.registerLoopAnalyses(LAM);
229  PB.crossRegisterProxies(LAM, FAM, CGAM, MAM);
230 
231  ModulePassManager MPM;
232 
233  // Always verify the input.
234  MPM.addPass(VerifierPass());
235 
236  // Now, add all the passes we've been requested to.
237  if (!PB.parsePassPipeline(MPM, PipelineDesc))
238  report_fatal_error("unable to parse pass pipeline description: " +
239  PipelineDesc);
240 
241  if (!DisableVerify)
242  MPM.addPass(VerifierPass());
243  MPM.run(Mod, MAM);
244 }
245 
246 static void runOldPMPasses(Config &Conf, Module &Mod, TargetMachine *TM,
247  bool IsThinLTO, ModuleSummaryIndex *ExportSummary,
248  const ModuleSummaryIndex *ImportSummary) {
249  legacy::PassManager passes;
251 
252  PassManagerBuilder PMB;
255  PMB.ExportSummary = ExportSummary;
256  PMB.ImportSummary = ImportSummary;
257  // Unconditionally verify input since it is not verified before this
258  // point and has unknown origin.
259  PMB.VerifyInput = true;
260  PMB.VerifyOutput = !Conf.DisableVerify;
261  PMB.LoopVectorize = true;
262  PMB.SLPVectorize = true;
263  PMB.OptLevel = Conf.OptLevel;
264  PMB.PGOSampleUse = Conf.SampleProfile;
265  if (IsThinLTO)
266  PMB.populateThinLTOPassManager(passes);
267  else
268  PMB.populateLTOPassManager(passes);
269  passes.run(Mod);
270 }
271 
272 bool opt(Config &Conf, TargetMachine *TM, unsigned Task, Module &Mod,
273  bool IsThinLTO, ModuleSummaryIndex *ExportSummary,
274  const ModuleSummaryIndex *ImportSummary) {
275  // FIXME: Plumb the combined index into the new pass manager.
276  if (!Conf.OptPipeline.empty())
277  runNewPMCustomPasses(Mod, TM, Conf.OptPipeline, Conf.AAPipeline,
278  Conf.DisableVerify);
279  else if (Conf.UseNewPM)
280  runNewPMPasses(Conf, Mod, TM, Conf.OptLevel, IsThinLTO);
281  else
282  runOldPMPasses(Conf, Mod, TM, IsThinLTO, ExportSummary, ImportSummary);
283  return !Conf.PostOptModuleHook || Conf.PostOptModuleHook(Task, Mod);
284 }
285 
286 void codegenWithSplitDwarf(Config &Conf, TargetMachine *TM,
287  AddStreamFn AddStream, unsigned Task, Module &Mod) {
288  SmallString<128> TempFile;
289  int FD = -1;
290  if (auto EC =
291  sys::fs::createTemporaryFile("lto-llvm-fission", "o", FD, TempFile))
292  report_fatal_error("Could not create temporary file " +
293  TempFile.str() + ": " + EC.message());
294  llvm::raw_fd_ostream OS(FD, true);
296  std::string DwoName = sys::path::filename(Mod.getModuleIdentifier()).str() +
297  "-" + std::to_string(Task) + "-";
298  size_t index = TempFile.str().rfind("lto-llvm-fission");
299  StringRef TempID = TempFile.str().substr(index + 17, 6);
300  DwoName += TempID.str() + ".dwo";
301  sys::path::append(DwarfFile, DwoName);
302  TM->Options.MCOptions.SplitDwarfFile = DwarfFile.str().str();
303 
304  legacy::PassManager CodeGenPasses;
305  if (TM->addPassesToEmitFile(CodeGenPasses, OS, Conf.CGFileType))
306  report_fatal_error("Failed to setup codegen");
307  CodeGenPasses.run(Mod);
308 
309  if (auto EC = llvm::sys::fs::create_directories(Conf.DwoDir))
310  report_fatal_error("Failed to create directory " +
311  Conf.DwoDir + ": " + EC.message());
312 
313  SmallVector<const char*, 5> ExtractArgs, StripArgs;
314  ExtractArgs.push_back(Conf.Objcopy.c_str());
315  ExtractArgs.push_back("--extract-dwo");
316  ExtractArgs.push_back(TempFile.c_str());
317  ExtractArgs.push_back(TM->Options.MCOptions.SplitDwarfFile.c_str());
318  ExtractArgs.push_back(nullptr);
319  StripArgs.push_back(Conf.Objcopy.c_str());
320  StripArgs.push_back("--strip-dwo");
321  StripArgs.push_back(TempFile.c_str());
322  StripArgs.push_back(nullptr);
323 
324  if (auto Ret = sys::ExecuteAndWait(Conf.Objcopy, ExtractArgs.data())) {
325  report_fatal_error("Failed to extract dwo from " + TempFile.str() +
326  ". Exit code " + std::to_string(Ret));
327  }
328  if (auto Ret = sys::ExecuteAndWait(Conf.Objcopy, StripArgs.data())) {
329  report_fatal_error("Failed to strip dwo from " + TempFile.str() +
330  ". Exit code " + std::to_string(Ret));
331  }
332 
333  auto Stream = AddStream(Task);
334  auto Buffer = MemoryBuffer::getFile(TempFile);
335  if (auto EC = Buffer.getError())
336  report_fatal_error("Failed to load file " +
337  TempFile.str() + ": " + EC.message());
338  *Stream->OS << Buffer.get()->getBuffer();
339  if (auto EC = sys::fs::remove(TempFile))
340  report_fatal_error("Failed to delete file " +
341  TempFile.str() + ": " + EC.message());
342 }
343 
344 void codegen(Config &Conf, TargetMachine *TM, AddStreamFn AddStream,
345  unsigned Task, Module &Mod) {
346  if (Conf.PreCodeGenModuleHook && !Conf.PreCodeGenModuleHook(Task, Mod))
347  return;
348 
349  if (!Conf.DwoDir.empty()) {
350  codegenWithSplitDwarf(Conf, TM, AddStream, Task, Mod);
351  return;
352  }
353 
354  auto Stream = AddStream(Task);
355  legacy::PassManager CodeGenPasses;
356  if (TM->addPassesToEmitFile(CodeGenPasses, *Stream->OS, Conf.CGFileType))
357  report_fatal_error("Failed to setup codegen");
358  CodeGenPasses.run(Mod);
359 }
360 
361 void splitCodeGen(Config &C, TargetMachine *TM, AddStreamFn AddStream,
362  unsigned ParallelCodeGenParallelismLevel,
363  std::unique_ptr<Module> Mod) {
364  ThreadPool CodegenThreadPool(ParallelCodeGenParallelismLevel);
365  unsigned ThreadCount = 0;
366  const Target *T = &TM->getTarget();
367 
368  SplitModule(
369  std::move(Mod), ParallelCodeGenParallelismLevel,
370  [&](std::unique_ptr<Module> MPart) {
371  // We want to clone the module in a new context to multi-thread the
372  // codegen. We do it by serializing partition modules to bitcode
373  // (while still on the main thread, in order to avoid data races) and
374  // spinning up new threads which deserialize the partitions into
375  // separate contexts.
376  // FIXME: Provide a more direct way to do this in LLVM.
377  SmallString<0> BC;
378  raw_svector_ostream BCOS(BC);
379  WriteBitcodeToFile(*MPart, BCOS);
380 
381  // Enqueue the task
382  CodegenThreadPool.async(
383  [&](const SmallString<0> &BC, unsigned ThreadId) {
384  LTOLLVMContext Ctx(C);
386  MemoryBufferRef(StringRef(BC.data(), BC.size()), "ld-temp.o"),
387  Ctx);
388  if (!MOrErr)
389  report_fatal_error("Failed to read bitcode");
390  std::unique_ptr<Module> MPartInCtx = std::move(MOrErr.get());
391 
392  std::unique_ptr<TargetMachine> TM =
393  createTargetMachine(C, T, *MPartInCtx);
394 
395  codegen(C, TM.get(), AddStream, ThreadId, *MPartInCtx);
396  },
397  // Pass BC using std::move to ensure that it get moved rather than
398  // copied into the thread's context.
399  std::move(BC), ThreadCount++);
400  },
401  false);
402 
403  // Because the inner lambda (which runs in a worker thread) captures our local
404  // variables, we need to wait for the worker threads to terminate before we
405  // can leave the function scope.
406  CodegenThreadPool.wait();
407 }
408 
409 Expected<const Target *> initAndLookupTarget(Config &C, Module &Mod) {
410  if (!C.OverrideTriple.empty())
412  else if (Mod.getTargetTriple().empty())
414 
415  std::string Msg;
417  if (!T)
418  return make_error<StringError>(Msg, inconvertibleErrorCode());
419  return T;
420 }
421 
422 }
423 
424 static void
425 finalizeOptimizationRemarks(std::unique_ptr<ToolOutputFile> DiagOutputFile) {
426  // Make sure we flush the diagnostic remarks file in case the linker doesn't
427  // call the global destructors before exiting.
428  if (!DiagOutputFile)
429  return;
430  DiagOutputFile->keep();
431  DiagOutputFile->os().flush();
432 }
433 
435  unsigned ParallelCodeGenParallelismLevel,
436  std::unique_ptr<Module> Mod,
437  ModuleSummaryIndex &CombinedIndex) {
438  Expected<const Target *> TOrErr = initAndLookupTarget(C, *Mod);
439  if (!TOrErr)
440  return TOrErr.takeError();
441 
442  std::unique_ptr<TargetMachine> TM = createTargetMachine(C, *TOrErr, *Mod);
443 
444  // Setup optimization remarks.
445  auto DiagFileOrErr = lto::setupOptimizationRemarks(
446  Mod->getContext(), C.RemarksFilename, C.RemarksWithHotness);
447  if (!DiagFileOrErr)
448  return DiagFileOrErr.takeError();
449  auto DiagnosticOutputFile = std::move(*DiagFileOrErr);
450 
451  if (!C.CodeGenOnly) {
452  if (!opt(C, TM.get(), 0, *Mod, /*IsThinLTO=*/false,
453  /*ExportSummary=*/&CombinedIndex, /*ImportSummary=*/nullptr)) {
454  finalizeOptimizationRemarks(std::move(DiagnosticOutputFile));
455  return Error::success();
456  }
457  }
458 
459  if (ParallelCodeGenParallelismLevel == 1) {
460  codegen(C, TM.get(), AddStream, 0, *Mod);
461  } else {
462  splitCodeGen(C, TM.get(), AddStream, ParallelCodeGenParallelismLevel,
463  std::move(Mod));
464  }
465  finalizeOptimizationRemarks(std::move(DiagnosticOutputFile));
466  return Error::success();
467 }
468 
469 static void dropDeadSymbols(Module &Mod, const GVSummaryMapTy &DefinedGlobals,
470  const ModuleSummaryIndex &Index) {
471  std::vector<GlobalValue*> DeadGVs;
472  for (auto &GV : Mod.global_values())
473  if (GlobalValueSummary *GVS = DefinedGlobals.lookup(GV.getGUID()))
474  if (!Index.isGlobalValueLive(GVS)) {
475  DeadGVs.push_back(&GV);
477  }
478 
479  // Now that all dead bodies have been dropped, delete the actual objects
480  // themselves when possible.
481  for (GlobalValue *GV : DeadGVs) {
482  GV->removeDeadConstantUsers();
483  // Might reference something defined in native object (i.e. dropped a
484  // non-prevailing IR def, but we need to keep the declaration).
485  if (GV->use_empty())
486  GV->eraseFromParent();
487  }
488 }
489 
490 Error lto::thinBackend(Config &Conf, unsigned Task, AddStreamFn AddStream,
491  Module &Mod, const ModuleSummaryIndex &CombinedIndex,
492  const FunctionImporter::ImportMapTy &ImportList,
493  const GVSummaryMapTy &DefinedGlobals,
495  Expected<const Target *> TOrErr = initAndLookupTarget(Conf, Mod);
496  if (!TOrErr)
497  return TOrErr.takeError();
498 
499  std::unique_ptr<TargetMachine> TM = createTargetMachine(Conf, *TOrErr, Mod);
500 
501  if (Conf.CodeGenOnly) {
502  codegen(Conf, TM.get(), AddStream, Task, Mod);
503  return Error::success();
504  }
505 
506  if (Conf.PreOptModuleHook && !Conf.PreOptModuleHook(Task, Mod))
507  return Error::success();
508 
509  renameModuleForThinLTO(Mod, CombinedIndex);
510 
511  dropDeadSymbols(Mod, DefinedGlobals, CombinedIndex);
512 
513  thinLTOResolveWeakForLinkerModule(Mod, DefinedGlobals);
514 
515  if (Conf.PostPromoteModuleHook && !Conf.PostPromoteModuleHook(Task, Mod))
516  return Error::success();
517 
518  if (!DefinedGlobals.empty())
519  thinLTOInternalizeModule(Mod, DefinedGlobals);
520 
521  if (Conf.PostInternalizeModuleHook &&
522  !Conf.PostInternalizeModuleHook(Task, Mod))
523  return Error::success();
524 
525  auto ModuleLoader = [&](StringRef Identifier) {
527  "ODR Type uniquing should be enabled on the context");
528  auto I = ModuleMap.find(Identifier);
529  assert(I != ModuleMap.end());
530  return I->second.getLazyModule(Mod.getContext(),
531  /*ShouldLazyLoadMetadata=*/true,
532  /*IsImporting*/ true);
533  };
534 
535  FunctionImporter Importer(CombinedIndex, ModuleLoader);
536  if (Error Err = Importer.importFunctions(Mod, ImportList).takeError())
537  return Err;
538 
539  if (Conf.PostImportModuleHook && !Conf.PostImportModuleHook(Task, Mod))
540  return Error::success();
541 
542  if (!opt(Conf, TM.get(), Task, Mod, /*IsThinLTO=*/true,
543  /*ExportSummary=*/nullptr, /*ImportSummary=*/&CombinedIndex))
544  return Error::success();
545 
546  codegen(Conf, TM.get(), AddStream, Task, Mod);
547  return Error::success();
548 }
static void codegen(Module *M, llvm::raw_pwrite_stream &OS, function_ref< std::unique_ptr< TargetMachine >()> TMFactory, TargetMachine::CodeGenFileType FileType)
Definition: ParallelCG.cpp:28
uint64_t CallInst * C
void push_back(const T &Elt)
Definition: SmallVector.h:212
std::error_code create_directories(const Twine &path, bool IgnoreExisting=true, perms Perms=owner_all|group_all)
Create all the non-existent directories in path.
Definition: Path.cpp:899
#define LLVM_ATTRIBUTE_NORETURN
Definition: Compiler.h:206
const std::string & getTargetTriple() const
Get the target triple which is a string describing the target host.
Definition: Module.h:233
Interfaces for registering analysis passes, producing common pass manager configurations, and parsing of pass pipelines.
raw_ostream & errs()
This returns a reference to a raw_ostream for standard error.
Expected< std::unique_ptr< ToolOutputFile > > setupOptimizationRemarks(LLVMContext &Context, StringRef LTORemarksFilename, bool LTOPassRemarksWithHotness, int Count=-1)
Setup optimization remarks.
Definition: LTO.cpp:1207
std::string CPU
Definition: Config.h:39
std::string AAPipeline
Definition: Config.h:63
LLVM_NODISCARD std::string str() const
str - Get the contents as an std::string.
Definition: StringRef.h:228
CodeGenOpt::Level CGOptLevel
Definition: Config.h:44
PassManagerBuilder - This class is used to set up a standard optimization sequence for languages like...
MCTargetOptions MCOptions
Machine level options.
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:115
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
void getDefaultSubtargetFeatures(const Triple &Triple)
Adds the default features for the specified target triple.
This header provides classes for managing a pipeline of passes over loops in LLVM IR...
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:63
LLVM_ATTRIBUTE_ALWAYS_INLINE size_type size() const
Definition: SmallVector.h:136
TargetMachine * createTargetMachine(StringRef TT, StringRef CPU, StringRef Features, const TargetOptions &Options, Optional< Reloc::Model > RM, Optional< CodeModel::Model > CM=None, CodeGenOpt::Level OL=CodeGenOpt::Default, bool JIT=false) const
createTargetMachine - Create a target specific machine implementation for the specified Triple...
std::error_code remove(const Twine &path, bool IgnoreNonExisting=true)
Remove path.
bool ShouldDiscardValueNames
Definition: Config.h:91
LLVM_NODISCARD size_t rfind(char C, size_t From=npos) const
Search for the last character C in the string.
Definition: StringRef.h:360
std::string OverrideTriple
Setting this field will replace target triples in input files with this triple.
Definition: Config.h:67
void populateThinLTOPassManager(legacy::PassManagerBase &PM)
std::string SplitDwarfFile
bool CodeGenOnly
Disable entirely the optimizer, including importing for ThinLTO.
Definition: Config.h:53
int ExecuteAndWait(StringRef Program, const char **Args, const char **Env=nullptr, ArrayRef< Optional< StringRef >> Redirects={}, unsigned SecondsToWait=0, unsigned MemoryLimit=0, std::string *ErrMsg=nullptr, bool *ExecutionFailed=nullptr)
This function executes the program using the arguments provided.
Definition: Program.cpp:30
void registerModuleAnalyses(ModuleAnalysisManager &MAM)
Registers all available module analysis passes.
void SplitModule(std::unique_ptr< Module > M, unsigned N, function_ref< void(std::unique_ptr< Module > MPart)> ModuleCallback, bool PreserveLocals=false)
Splits the module M into N linkable partitions.
std::unique_ptr< raw_ostream > ResolutionFile
If this field is set, LTO will write input file paths and symbol resolutions here in llvm-lto2 comman...
Definition: Config.h:98
bool convertToDeclaration(GlobalValue &GV)
Converts value GV to declaration, or replaces with a declaration if it is an alias.
ImmutablePass * createTargetTransformInfoWrapperPass(TargetIRAnalysis TIRA)
Create an analysis pass wrapper around a TTI object.
ModuleSummaryIndex * ExportSummary
The module summary index to use for exporting information from the regular LTO phase, for example for the CFI and devirtualization type tests.
std::string getString() const
Returns features as a string.
std::error_code createTemporaryFile(const Twine &Prefix, StringRef Suffix, int &ResultFD, SmallVectorImpl< char > &ResultPath, sys::fs::OpenFlags Flags=sys::fs::F_RW)
Create a file in the system temporary directory.
Definition: Path.cpp:793
const ModuleSummaryIndex * ImportSummary
The module summary index to use for importing information to the thin LTO backends, for example for the CFI and devirtualization type tests.
A raw_ostream that writes to an SmallVector or SmallString.
Definition: raw_ostream.h:494
OptimizationLevel
LLVM-provided high-level optimization levels.
Definition: PassBuilder.h:91
Implementation of the target library information.
This class implements a map that also provides access to all stored values in a deterministic order...
Definition: MapVector.h:38
bool isGlobalValueLive(const GlobalValueSummary *GVS) const
bool DebugPassManager
Whether to emit the pass manager debuggging informations.
Definition: Config.h:89
std::string PGOSampleUse
Path of the sample Profile data file.
Error takeError()
Take ownership of the stored error.
Definition: Error.h:537
static const Target * lookupTarget(const std::string &Triple, std::string &Error)
lookupTarget - Lookup a target based on a target triple.
PreservedAnalyses run(IRUnitT &IR, AnalysisManagerT &AM, ExtraArgTs... ExtraArgs)
Run all of the passes in this manager over the given unit of IR.
Definition: PassManager.h:444
std::shared_future< void > async(Function &&F, Args &&... ArgList)
Asynchronous submission of a task to the pool.
Definition: ThreadPool.h:54
ModuleHookFn PreCodeGenModuleHook
This module hook is called before code generation.
Definition: Config.h:146
Pass * Inliner
Inliner - Specifies the inliner to use.
CombinedIndexHookFn CombinedIndexHook
Definition: Config.h:159
static void finalizeOptimizationRemarks(std::unique_ptr< ToolOutputFile > DiagOutputFile)
Definition: LTOBackend.cpp:425
std::function< std::unique_ptr< NativeObjectStream >unsigned Task)> AddStreamFn
This type defines the callback to add a native object that is generated on the fly.
Definition: LTO.h:174
void append(SmallVectorImpl< char > &path, const Twine &a, const Twine &b="", const Twine &c="", const Twine &d="")
Append to path.
Definition: Path.cpp:466
void add(Pass *P) override
Add a pass to the queue of passes to run.
bool RemarksWithHotness
Whether to emit optimization remarks with hotness informations.
Definition: Config.h:86
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
std::string DwoDir
The directory to store .dwo files.
Definition: Config.h:77
std::vector< std::string > MAttrs
Definition: Config.h:41
LLVMContext & getContext() const
Get the global data context.
Definition: Module.h:237
bool parseAAPipeline(AAManager &AA, StringRef PipelineText)
Parse a textual alias analysis pipeline into the provided AA manager.
void populateLTOPassManager(legacy::PassManagerBase &PM)
bool registerPass(PassBuilderT &&PassBuilder)
Register an analysis pass with the manager.
Definition: PassManager.h:739
This class provides access to building LLVM&#39;s passes.
Definition: PassBuilder.h:57
ModulePassManager buildLTODefaultPipeline(OptimizationLevel Level, bool DebugLogging=false)
Build an LTO default optimization pipeline to a pass manager.
std::function< bool(unsigned Task, const Module &)> ModuleHookFn
The following callbacks deal with tasks, which normally represent the entire optimization and code ge...
Definition: Config.h:124
Tagged union holding either a T or a Error.
Definition: CachePruning.h:23
void AddFeature(StringRef String, bool Enable=true)
Adds Features.
void registerLoopAnalyses(LoopAnalysisManager &LAM)
Registers all available loop analysis passes.
StringRef str() const
Explicit conversion to StringRef.
Definition: SmallString.h:267
bool DisableVerify
Definition: Config.h:47
#define T
unsigned OptLevel
The Optimization Level - Specify the basic optimization level.
void crossRegisterProxies(LoopAnalysisManager &LAM, FunctionAnalysisManager &FAM, CGSCCAnalysisManager &CGAM, ModuleAnalysisManager &MAM)
Cross register the analysis managers through their proxies.
std::string SampleProfile
Sample PGO profile path.
Definition: Config.h:74
TargetOptions Options
Definition: Config.h:40
Optional< CodeModel::Model > CodeModel
Definition: Config.h:43
A ThreadPool for asynchronous parallel execution on a defined number of threads.
Definition: ThreadPool.h:36
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
Definition: StringRef.h:598
Class to hold module path string table and global value map, and encapsulate methods for operating on...
iterator find(const KeyT &Key)
Definition: MapVector.h:148
PassManager manages ModulePassManagers.
static void dropDeadSymbols(Module &Mod, const GVSummaryMapTy &DefinedGlobals, const ModuleSummaryIndex &Index)
Definition: LTOBackend.cpp:469
ModuleHookFn PreOptModuleHook
This module hook is called after linking (regular LTO) or loading (ThinLTO) the module, before modifying it.
Definition: Config.h:128
bool renameModuleForThinLTO(Module &M, const ModuleSummaryIndex &Index, SetVector< GlobalValue *> *GlobalsToImport=nullptr)
Perform in-place global value handling on the given Module for exported local functions renamed and p...
Error errorCodeToError(std::error_code EC)
Helper for converting an std::error_code to a Error.
Definition: Error.cpp:78
TargetLibraryInfoImpl * LibraryInfo
LibraryInfo - Specifies information about the runtime library for the optimizer.
Disable as many optimizations as possible.
Definition: PassBuilder.h:95
A manager for alias analyses.
TargetIRAnalysis getTargetIRAnalysis()
Get a TargetIRAnalysis appropriate for the target.
bool UseNewPM
Use the new pass manager.
Definition: Config.h:50
unsigned OptLevel
Definition: Config.h:46
void WriteBitcodeToFile(const Module &M, raw_ostream &Out, bool ShouldPreserveUseListOrder=false, const ModuleSummaryIndex *Index=nullptr, bool GenerateHash=false, ModuleHash *ModHash=nullptr)
Write the specified module to the specified raw output stream.
bool isODRUniquingDebugTypes() const
Whether there is a string map for uniquing debug info identifiers across the context.
void registerFunctionAnalyses(FunctionAnalysisManager &FAM)
Registers all available function analysis passes.
LTO configuration.
Definition: Config.h:36
std::string DefaultTriple
Setting this field will replace unspecified target triples in input files with this triple...
Definition: Config.h:71
Optimize for fast execution as much as possible.
Definition: PassBuilder.h:149
const Triple & getTargetTriple() const
Function and variable summary information to aid decisions and implementation of importing.
const Target & getTarget() const
ModuleHookFn PostInternalizeModuleHook
This hook is called after internalizing the module.
Definition: Config.h:135
bool run(Module &M)
run - Execute all of the passes scheduled for execution.
Create a verifier pass.
Definition: Verifier.h:137
const std::string & getModuleIdentifier() const
Get the module identifier which is, essentially, the name of the module.
Definition: Module.h:208
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
TargetMachine::CodeGenFileType CGFileType
Definition: Config.h:45
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
std::string RemarksFilename
Optimization remarks file path.
Definition: Config.h:83
static ErrorSuccess success()
Create a success value.
Definition: Error.h:313
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:862
Optimize for fast execution as much as possible without triggering significant incremental compile ti...
Definition: PassBuilder.h:133
bool parsePassPipeline(ModulePassManager &MPM, StringRef PipelineText, bool VerifyEachPass=true, bool DebugLogging=false)
Parse a textual pass pipeline description into a ModulePassManager.
Optional< Reloc::Model > RelocModel
Definition: Config.h:42
reference get()
Returns a reference to the stored T value.
Definition: Error.h:517
std::string utostr(uint64_t X, bool isNeg=false)
Definition: StringExtras.h:193
The access may modify the value stored in memory.
StringMap - This is an unconventional map that is specialized for handling keys that are "strings"...
Definition: StringMap.h:222
Target - Wrapper for Target specific information.
Manages the enabling and disabling of subtarget specific features.
std::string OptPipeline
If this field is set, the set of passes run in the middle-end optimizer will be the one specified by ...
Definition: Config.h:58
void WriteIndexToFile(const ModuleSummaryIndex &Index, raw_ostream &Out, const std::map< std::string, GVSummaryMapTy > *ModuleToSummariesForIndex=nullptr)
Write the specified module summary index to the given raw output stream, where it will be written in ...
std::string Objcopy
The objcopy binary used to extract dwo files.
Definition: Config.h:80
virtual bool addPassesToEmitFile(PassManagerBase &, raw_pwrite_stream &, CodeGenFileType, bool=true, MachineModuleInfo *MMI=nullptr)
Add passes to the specified pass manager to get the specified file emitted.
The file should be opened in text mode on platforms that make this distinction.
Definition: FileSystem.h:688
Expected< std::unique_ptr< Module > > parseBitcodeFile(MemoryBufferRef Buffer, LLVMContext &Context)
Read the specified bitcode file, returning the module.
Expected< bool > importFunctions(Module &M, const ImportMapTy &ImportList)
Import functions in Module M based on the supplied import list.
Error thinBackend(Config &C, unsigned Task, AddStreamFn AddStream, Module &M, const ModuleSummaryIndex &CombinedIndex, const FunctionImporter::ImportMapTy &ImportList, const GVSummaryMapTy &DefinedGlobals, MapVector< StringRef, BitcodeModule > &ModuleMap)
Runs a ThinLTO backend.
Definition: LTOBackend.cpp:490
A raw_ostream that writes to a file descriptor.
Definition: raw_ostream.h:364
ModulePassManager buildThinLTODefaultPipeline(OptimizationLevel Level, bool DebugLogging=false)
Build an ThinLTO default optimization pipeline to a pass manager.
pointer data()
Return a pointer to the vector&#39;s buffer, even if empty().
Definition: SmallVector.h:143
Manages a sequence of passes over a particular unit of IR.
Definition: PassManager.h:420
StringRef filename(StringRef path, Style style=Style::native)
Get filename.
Definition: Path.cpp:576
A struct capturing PGO tunables.
Definition: PassBuilder.h:32
TargetOptions Options
Definition: TargetMachine.h:98
const char * c_str()
Definition: SmallString.h:270
#define I(x, y, z)
Definition: MD5.cpp:58
void thinLTOInternalizeModule(Module &TheModule, const GVSummaryMapTy &DefinedGlobals)
Internalize TheModule based on the information recorded in the summaries during global summary-based ...
void setTargetTriple(StringRef T)
Set the target triple.
Definition: Module.h:269
void thinLTOResolveWeakForLinkerModule(Module &TheModule, const GVSummaryMapTy &DefinedGlobals)
Resolve WeakForLinker values in TheModule based on the information recorded in the summaries during g...
LLVM_NODISCARD bool empty() const
Definition: DenseMap.h:93
ValueT lookup(const_arg_type_t< KeyT > Val) const
lookup - Return the entry for the specified key, or a default constructed value if no such entry exis...
Definition: DenseMap.h:181
This header provides classes for managing passes over SCCs of the call graph.
const std::string to_string(const T &Value)
Definition: ScopedPrinter.h:62
static ErrorOr< std::unique_ptr< MemoryBuffer > > getFile(const Twine &Filename, int64_t FileSize=-1, bool RequiresNullTerminator=true, bool IsVolatile=false)
Open the specified file as a MemoryBuffer, returning a new MemoryBuffer if successful, otherwise returning null.
void registerCGSCCAnalyses(CGSCCAnalysisManager &CGAM)
Registers all available CGSCC analysis passes.
ModuleHookFn PostImportModuleHook
This hook is called after importing from other modules (ThinLTO-specific).
Definition: Config.h:138
Error backend(Config &C, AddStreamFn AddStream, unsigned ParallelCodeGenParallelismLevel, std::unique_ptr< Module > M, ModuleSummaryIndex &CombinedIndex)
Runs a regular LTO backend.
Definition: LTOBackend.cpp:434
iterator end()
Definition: MapVector.h:72
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
Optimize quickly without destroying debuggability.
Definition: PassBuilder.h:115
Pass * createFunctionInliningPass()
createFunctionInliningPass - Return a new pass object that uses a heuristic to inline direct function...
The function importer is automatically importing function from other modules based on the provided su...
std::unique_ptr< Module > splitCodeGen(std::unique_ptr< Module > M, ArrayRef< raw_pwrite_stream *> OSs, ArrayRef< llvm::raw_pwrite_stream *> BCOSs, const std::function< std::unique_ptr< TargetMachine >()> &TMFactory, TargetMachine::CodeGenFileType FT=TargetMachine::CGFT_ObjectFile, bool PreserveLocals=false)
Split M into OSs.size() partitions, and generate code for each.
Definition: ParallelCG.cpp:38
Lightweight error class with error context and mandatory checking.
Definition: Error.h:156
const FeatureBitset Features
ModuleHookFn PostOptModuleHook
This module hook is called after optimization is complete.
Definition: Config.h:141
Primary interface to the complete machine description for the target machine.
Definition: TargetMachine.h:59
PICLevel::Level getPICLevel() const
Returns the PIC level (small or large model)
Definition: Module.cpp:473
Error addSaveTemps(std::string OutputFileName, bool UseInputModulePath=false)
This is a convenience function that configures this Config object to write temporary files named afte...
Definition: LTOBackend.cpp:55
A derived class of LLVMContext that initializes itself according to a given Config object...
Definition: Config.h:194
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
hexagon cext opt
A container for analyses that lazily runs them and caches their results.
void addPass(PassT Pass)
Definition: PassManager.h:485
static LLVM_ATTRIBUTE_NORETURN void reportOpenError(StringRef Path, Twine Msg)
Definition: LTOBackend.cpp:49
This pass exposes codegen information to IR-level passes.
This header defines various interfaces for pass management in LLVM.
ModuleHookFn PostPromoteModuleHook
This hook is called after promoting any internal functions (ThinLTO-specific).
Definition: Config.h:132
iterator_range< global_value_iterator > global_values()
Definition: Module.h:662
void wait()
Blocking wait for all the threads to complete and the queue to be empty.
Definition: ThreadPool.cpp:72
std::error_code inconvertibleErrorCode()
The value returned by this function can be returned from convertToErrorCode for Error values where no...
Definition: Error.cpp:73