Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:43:53

0001 //===- JITTargetMachineBuilder.h - Build TargetMachines for JIT -*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 //
0009 // A utitily for building TargetMachines for JITs.
0010 //
0011 //===----------------------------------------------------------------------===//
0012 
0013 #ifndef LLVM_EXECUTIONENGINE_ORC_JITTARGETMACHINEBUILDER_H
0014 #define LLVM_EXECUTIONENGINE_ORC_JITTARGETMACHINEBUILDER_H
0015 
0016 #include "llvm/Support/CodeGen.h"
0017 #include "llvm/Support/Error.h"
0018 #include "llvm/Target/TargetMachine.h"
0019 #include "llvm/Target/TargetOptions.h"
0020 #include "llvm/TargetParser/SubtargetFeature.h"
0021 #include "llvm/TargetParser/Triple.h"
0022 #include <memory>
0023 #include <optional>
0024 #include <string>
0025 #include <vector>
0026 
0027 namespace llvm {
0028 
0029 class raw_ostream;
0030 
0031 namespace orc {
0032 
0033 /// A utility class for building TargetMachines for JITs.
0034 class JITTargetMachineBuilder {
0035 #ifndef NDEBUG
0036   friend class JITTargetMachineBuilderPrinter;
0037 #endif
0038 public:
0039   /// Create a JITTargetMachineBuilder based on the given triple.
0040   ///
0041   /// Note: TargetOptions is default-constructed, then EmulatedTLS is set to
0042   /// true. If EmulatedTLS is not required, these values should be reset before
0043   /// calling createTargetMachine.
0044   JITTargetMachineBuilder(Triple TT);
0045 
0046   /// Create a JITTargetMachineBuilder for the host system.
0047   ///
0048   /// Note: TargetOptions is default-constructed, then EmulatedTLS is set to
0049   /// true. If EmulatedTLS is not required, these values should be reset before
0050   /// calling createTargetMachine.
0051   static Expected<JITTargetMachineBuilder> detectHost();
0052 
0053   /// Create a TargetMachine.
0054   ///
0055   /// This operation will fail if the requested target is not registered,
0056   /// in which case see llvm/Support/TargetSelect.h. To JIT IR the Target and
0057   /// the target's AsmPrinter must both be registered. To JIT assembly
0058   /// (including inline and module level assembly) the target's AsmParser must
0059   /// also be registered.
0060   Expected<std::unique_ptr<TargetMachine>> createTargetMachine();
0061 
0062   /// Get the default DataLayout for the target.
0063   ///
0064   /// Note: This is reasonably expensive, as it creates a temporary
0065   /// TargetMachine instance under the hood. It is only suitable for use during
0066   /// JIT setup.
0067   Expected<DataLayout> getDefaultDataLayoutForTarget() {
0068     auto TM = createTargetMachine();
0069     if (!TM)
0070       return TM.takeError();
0071     return (*TM)->createDataLayout();
0072   }
0073 
0074   /// Set the CPU string.
0075   JITTargetMachineBuilder &setCPU(std::string CPU) {
0076     this->CPU = std::move(CPU);
0077     return *this;
0078   }
0079 
0080   /// Returns the CPU string.
0081   const std::string &getCPU() const { return CPU; }
0082 
0083   /// Set the relocation model.
0084   JITTargetMachineBuilder &setRelocationModel(std::optional<Reloc::Model> RM) {
0085     this->RM = std::move(RM);
0086     return *this;
0087   }
0088 
0089   /// Get the relocation model.
0090   const std::optional<Reloc::Model> &getRelocationModel() const { return RM; }
0091 
0092   /// Set the code model.
0093   JITTargetMachineBuilder &setCodeModel(std::optional<CodeModel::Model> CM) {
0094     this->CM = std::move(CM);
0095     return *this;
0096   }
0097 
0098   /// Get the code model.
0099   const std::optional<CodeModel::Model> &getCodeModel() const { return CM; }
0100 
0101   /// Set the LLVM CodeGen optimization level.
0102   JITTargetMachineBuilder &setCodeGenOptLevel(CodeGenOptLevel OptLevel) {
0103     this->OptLevel = OptLevel;
0104     return *this;
0105   }
0106 
0107   /// Set subtarget features.
0108   JITTargetMachineBuilder &setFeatures(StringRef FeatureString) {
0109     Features = SubtargetFeatures(FeatureString);
0110     return *this;
0111   }
0112 
0113   /// Add subtarget features.
0114   JITTargetMachineBuilder &
0115   addFeatures(const std::vector<std::string> &FeatureVec);
0116 
0117   /// Access subtarget features.
0118   SubtargetFeatures &getFeatures() { return Features; }
0119 
0120   /// Access subtarget features.
0121   const SubtargetFeatures &getFeatures() const { return Features; }
0122 
0123   /// Set TargetOptions.
0124   ///
0125   /// Note: This operation will overwrite any previously configured options,
0126   /// including EmulatedTLS and UseInitArray which the JITTargetMachineBuilder
0127   /// sets by default. Clients are responsible for re-enabling these overwritten
0128   /// options.
0129   JITTargetMachineBuilder &setOptions(TargetOptions Options) {
0130     this->Options = std::move(Options);
0131     return *this;
0132   }
0133 
0134   /// Access TargetOptions.
0135   TargetOptions &getOptions() { return Options; }
0136 
0137   /// Access TargetOptions.
0138   const TargetOptions &getOptions() const { return Options; }
0139 
0140   /// Access Triple.
0141   Triple &getTargetTriple() { return TT; }
0142 
0143   /// Access Triple.
0144   const Triple &getTargetTriple() const { return TT; }
0145 
0146 private:
0147   Triple TT;
0148   std::string CPU;
0149   SubtargetFeatures Features;
0150   TargetOptions Options;
0151   std::optional<Reloc::Model> RM;
0152   std::optional<CodeModel::Model> CM;
0153   CodeGenOptLevel OptLevel = CodeGenOptLevel::Default;
0154 };
0155 
0156 #ifndef NDEBUG
0157 class JITTargetMachineBuilderPrinter {
0158 public:
0159   JITTargetMachineBuilderPrinter(JITTargetMachineBuilder &JTMB,
0160                                  StringRef Indent)
0161       : JTMB(JTMB), Indent(Indent) {}
0162   void print(raw_ostream &OS) const;
0163 
0164   friend raw_ostream &operator<<(raw_ostream &OS,
0165                                  const JITTargetMachineBuilderPrinter &JTMBP) {
0166     JTMBP.print(OS);
0167     return OS;
0168   }
0169 
0170 private:
0171   JITTargetMachineBuilder &JTMB;
0172   StringRef Indent;
0173 };
0174 #endif // NDEBUG
0175 
0176 } // end namespace orc
0177 } // end namespace llvm
0178 
0179 #endif // LLVM_EXECUTIONENGINE_ORC_JITTARGETMACHINEBUILDER_H