Subversion Repositories QNX 8.QNX8 LLVM/Clang compiler suite

Rev

Blame | Last modification | View Log | Download | RSS feed

  1. //===-Config.h - LLVM Link Time Optimizer Configuration ---------*- C++ -*-===//
  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 defines the lto::Config data structure, which allows clients to
  10. // configure LTO.
  11. //
  12. //===----------------------------------------------------------------------===//
  13.  
  14. #ifndef LLVM_LTO_CONFIG_H
  15. #define LLVM_LTO_CONFIG_H
  16.  
  17. #include "llvm/ADT/DenseSet.h"
  18. #include "llvm/Config/llvm-config.h"
  19. #include "llvm/IR/DiagnosticInfo.h"
  20. #include "llvm/IR/GlobalValue.h"
  21. #include "llvm/IR/LLVMContext.h"
  22. #include "llvm/IR/LegacyPassManager.h"
  23. #include "llvm/Passes/PassBuilder.h"
  24. #include "llvm/Support/CodeGen.h"
  25. #include "llvm/Target/TargetOptions.h"
  26.  
  27. #include <functional>
  28. #include <optional>
  29.  
  30. namespace llvm {
  31.  
  32. class Error;
  33. class Module;
  34. class ModuleSummaryIndex;
  35. class raw_pwrite_stream;
  36.  
  37. namespace lto {
  38.  
  39. /// LTO configuration. A linker can configure LTO by setting fields in this data
  40. /// structure and passing it to the lto::LTO constructor.
  41. struct Config {
  42.   enum VisScheme {
  43.     FromPrevailing,
  44.     ELF,
  45.   };
  46.   // Note: when adding fields here, consider whether they need to be added to
  47.   // computeLTOCacheKey in LTO.cpp.
  48.   std::string CPU;
  49.   TargetOptions Options;
  50.   std::vector<std::string> MAttrs;
  51.   std::vector<std::string> MllvmArgs;
  52.   std::vector<std::string> PassPlugins;
  53.   /// For adding passes that run right before codegen.
  54.   std::function<void(legacy::PassManager &)> PreCodeGenPassesHook;
  55.   std::optional<Reloc::Model> RelocModel = Reloc::PIC_;
  56.   std::optional<CodeModel::Model> CodeModel;
  57.   CodeGenOpt::Level CGOptLevel = CodeGenOpt::Default;
  58.   CodeGenFileType CGFileType = CGFT_ObjectFile;
  59.   unsigned OptLevel = 2;
  60.   bool DisableVerify = false;
  61.  
  62.   /// Use the standard optimization pipeline.
  63.   bool UseDefaultPipeline = false;
  64.  
  65.   /// Flag to indicate that the optimizer should not assume builtins are present
  66.   /// on the target.
  67.   bool Freestanding = false;
  68.  
  69.   /// Disable entirely the optimizer, including importing for ThinLTO
  70.   bool CodeGenOnly = false;
  71.  
  72.   /// Run PGO context sensitive IR instrumentation.
  73.   bool RunCSIRInstr = false;
  74.  
  75.   /// Turn on/off the warning about a hash mismatch in the PGO profile data.
  76.   bool PGOWarnMismatch = true;
  77.  
  78.   /// Asserts whether we can assume whole program visibility during the LTO
  79.   /// link.
  80.   bool HasWholeProgramVisibility = false;
  81.  
  82.   /// Always emit a Regular LTO object even when it is empty because no Regular
  83.   /// LTO modules were linked. This option is useful for some build system which
  84.   /// want to know a priori all possible output files.
  85.   bool AlwaysEmitRegularLTOObj = false;
  86.  
  87.   /// Allows non-imported definitions to get the potentially more constraining
  88.   /// visibility from the prevailing definition. FromPrevailing is the default
  89.   /// because it works for many binary formats. ELF can use the more optimized
  90.   /// 'ELF' scheme.
  91.   VisScheme VisibilityScheme = FromPrevailing;
  92.  
  93.   /// If this field is set, the set of passes run in the middle-end optimizer
  94.   /// will be the one specified by the string. Only works with the new pass
  95.   /// manager as the old one doesn't have this ability.
  96.   std::string OptPipeline;
  97.  
  98.   // If this field is set, it has the same effect of specifying an AA pipeline
  99.   // identified by the string. Only works with the new pass manager, in
  100.   // conjunction OptPipeline.
  101.   std::string AAPipeline;
  102.  
  103.   /// Setting this field will replace target triples in input files with this
  104.   /// triple.
  105.   std::string OverrideTriple;
  106.  
  107.   /// Setting this field will replace unspecified target triples in input files
  108.   /// with this triple.
  109.   std::string DefaultTriple;
  110.  
  111.   /// Context Sensitive PGO profile path.
  112.   std::string CSIRProfile;
  113.  
  114.   /// Sample PGO profile path.
  115.   std::string SampleProfile;
  116.  
  117.   /// Name remapping file for profile data.
  118.   std::string ProfileRemapping;
  119.  
  120.   /// The directory to store .dwo files.
  121.   std::string DwoDir;
  122.  
  123.   /// The name for the split debug info file used for the DW_AT_[GNU_]dwo_name
  124.   /// attribute in the skeleton CU. This should generally only be used when
  125.   /// running an individual backend directly via thinBackend(), as otherwise
  126.   /// all objects would use the same .dwo file. Not used as output path.
  127.   std::string SplitDwarfFile;
  128.  
  129.   /// The path to write a .dwo file to. This should generally only be used when
  130.   /// running an individual backend directly via thinBackend(), as otherwise
  131.   /// all .dwo files will be written to the same path. Not used in skeleton CU.
  132.   std::string SplitDwarfOutput;
  133.  
  134.   /// Optimization remarks file path.
  135.   std::string RemarksFilename;
  136.  
  137.   /// Optimization remarks pass filter.
  138.   std::string RemarksPasses;
  139.  
  140.   /// Whether to emit optimization remarks with hotness informations.
  141.   bool RemarksWithHotness = false;
  142.  
  143.   /// The minimum hotness value a diagnostic needs in order to be included in
  144.   /// optimization diagnostics.
  145.   ///
  146.   /// The threshold is an Optional value, which maps to one of the 3 states:
  147.   /// 1. 0            => threshold disabled. All emarks will be printed.
  148.   /// 2. positive int => manual threshold by user. Remarks with hotness exceed
  149.   ///                    threshold will be printed.
  150.   /// 3. None         => 'auto' threshold by user. The actual value is not
  151.   ///                    available at command line, but will be synced with
  152.   ///                    hotness threhold from profile summary during
  153.   ///                    compilation.
  154.   ///
  155.   /// If threshold option is not specified, it is disabled by default.
  156.   std::optional<uint64_t> RemarksHotnessThreshold = 0;
  157.  
  158.   /// The format used for serializing remarks (default: YAML).
  159.   std::string RemarksFormat;
  160.  
  161.   /// Whether to emit the pass manager debuggging informations.
  162.   bool DebugPassManager = false;
  163.  
  164.   /// Statistics output file path.
  165.   std::string StatsFile;
  166.  
  167.   /// Specific thinLTO modules to compile.
  168.   std::vector<std::string> ThinLTOModulesToCompile;
  169.  
  170.   /// Time trace enabled.
  171.   bool TimeTraceEnabled = false;
  172.  
  173.   /// Time trace granularity.
  174.   unsigned TimeTraceGranularity = 500;
  175.  
  176.   bool ShouldDiscardValueNames = true;
  177.   DiagnosticHandlerFunction DiagHandler;
  178.  
  179.   /// Add FSAFDO discriminators.
  180.   bool AddFSDiscriminator = false;
  181.  
  182.   /// Use opaque pointer types. Used to call LLVMContext::setOpaquePointers
  183.   /// unless already set by the `-opaque-pointers` commandline option.
  184.   bool OpaquePointers = true;
  185.  
  186.   /// If this field is set, LTO will write input file paths and symbol
  187.   /// resolutions here in llvm-lto2 command line flag format. This can be
  188.   /// used for testing and for running the LTO pipeline outside of the linker
  189.   /// with llvm-lto2.
  190.   std::unique_ptr<raw_ostream> ResolutionFile;
  191.  
  192.   /// Tunable parameters for passes in the default pipelines.
  193.   PipelineTuningOptions PTO;
  194.  
  195.   /// The following callbacks deal with tasks, which normally represent the
  196.   /// entire optimization and code generation pipeline for what will become a
  197.   /// single native object file. Each task has a unique identifier between 0 and
  198.   /// getMaxTasks()-1, which is supplied to the callback via the Task parameter.
  199.   /// A task represents the entire pipeline for ThinLTO and regular
  200.   /// (non-parallel) LTO, but a parallel code generation task will be split into
  201.   /// N tasks before code generation, where N is the parallelism level.
  202.   ///
  203.   /// LTO may decide to stop processing a task at any time, for example if the
  204.   /// module is empty or if a module hook (see below) returns false. For this
  205.   /// reason, the client should not expect to receive exactly getMaxTasks()
  206.   /// native object files.
  207.  
  208.   /// A module hook may be used by a linker to perform actions during the LTO
  209.   /// pipeline. For example, a linker may use this function to implement
  210.   /// -save-temps. If this function returns false, any further processing for
  211.   /// that task is aborted.
  212.   ///
  213.   /// Module hooks must be thread safe with respect to the linker's internal
  214.   /// data structures. A module hook will never be called concurrently from
  215.   /// multiple threads with the same task ID, or the same module.
  216.   ///
  217.   /// Note that in out-of-process backend scenarios, none of the hooks will be
  218.   /// called for ThinLTO tasks.
  219.   using ModuleHookFn = std::function<bool(unsigned Task, const Module &)>;
  220.  
  221.   /// This module hook is called after linking (regular LTO) or loading
  222.   /// (ThinLTO) the module, before modifying it.
  223.   ModuleHookFn PreOptModuleHook;
  224.  
  225.   /// This hook is called after promoting any internal functions
  226.   /// (ThinLTO-specific).
  227.   ModuleHookFn PostPromoteModuleHook;
  228.  
  229.   /// This hook is called after internalizing the module.
  230.   ModuleHookFn PostInternalizeModuleHook;
  231.  
  232.   /// This hook is called after importing from other modules (ThinLTO-specific).
  233.   ModuleHookFn PostImportModuleHook;
  234.  
  235.   /// This module hook is called after optimization is complete.
  236.   ModuleHookFn PostOptModuleHook;
  237.  
  238.   /// This module hook is called before code generation. It is similar to the
  239.   /// PostOptModuleHook, but for parallel code generation it is called after
  240.   /// splitting the module.
  241.   ModuleHookFn PreCodeGenModuleHook;
  242.  
  243.   /// A combined index hook is called after all per-module indexes have been
  244.   /// combined (ThinLTO-specific). It can be used to implement -save-temps for
  245.   /// the combined index.
  246.   ///
  247.   /// If this function returns false, any further processing for ThinLTO tasks
  248.   /// is aborted.
  249.   ///
  250.   /// It is called regardless of whether the backend is in-process, although it
  251.   /// is not called from individual backend processes.
  252.   using CombinedIndexHookFn = std::function<bool(
  253.       const ModuleSummaryIndex &Index,
  254.       const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols)>;
  255.   CombinedIndexHookFn CombinedIndexHook;
  256.  
  257.   /// This is a convenience function that configures this Config object to write
  258.   /// temporary files named after the given OutputFileName for each of the LTO
  259.   /// phases to disk. A client can use this function to implement -save-temps.
  260.   ///
  261.   /// FIXME: Temporary files derived from ThinLTO backends are currently named
  262.   /// after the input file name, rather than the output file name, when
  263.   /// UseInputModulePath is set to true.
  264.   ///
  265.   /// Specifically, it (1) sets each of the above module hooks and the combined
  266.   /// index hook to a function that calls the hook function (if any) that was
  267.   /// present in the appropriate field when the addSaveTemps function was
  268.   /// called, and writes the module to a bitcode file with a name prefixed by
  269.   /// the given output file name, and (2) creates a resolution file whose name
  270.   /// is prefixed by the given output file name and sets ResolutionFile to its
  271.   /// file handle.
  272.   ///
  273.   /// SaveTempsArgs can be specified to select which temps to save.
  274.   /// If SaveTempsArgs is not provided, all temps are saved.
  275.   Error addSaveTemps(std::string OutputFileName,
  276.                      bool UseInputModulePath = false,
  277.                      const DenseSet<StringRef> &SaveTempsArgs = {});
  278. };
  279.  
  280. struct LTOLLVMDiagnosticHandler : public DiagnosticHandler {
  281.   DiagnosticHandlerFunction *Fn;
  282.   LTOLLVMDiagnosticHandler(DiagnosticHandlerFunction *DiagHandlerFn)
  283.       : Fn(DiagHandlerFn) {}
  284.   bool handleDiagnostics(const DiagnosticInfo &DI) override {
  285.     (*Fn)(DI);
  286.     return true;
  287.   }
  288. };
  289. /// A derived class of LLVMContext that initializes itself according to a given
  290. /// Config object. The purpose of this class is to tie ownership of the
  291. /// diagnostic handler to the context, as opposed to the Config object (which
  292. /// may be ephemeral).
  293. // FIXME: This should not be required as diagnostic handler is not callback.
  294. struct LTOLLVMContext : LLVMContext {
  295.  
  296.   LTOLLVMContext(const Config &C) : DiagHandler(C.DiagHandler) {
  297.     setDiscardValueNames(C.ShouldDiscardValueNames);
  298.     enableDebugTypeODRUniquing();
  299.     setDiagnosticHandler(
  300.         std::make_unique<LTOLLVMDiagnosticHandler>(&DiagHandler), true);
  301.     setOpaquePointers(C.OpaquePointers);
  302.   }
  303.   DiagnosticHandlerFunction DiagHandler;
  304. };
  305.  
  306. }
  307. }
  308.  
  309. #endif
  310.