| 1 | //===- JITTargetMachineBuilder.h - Build TargetMachines for JIT -*- 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 | // A utitily for building TargetMachines for JITs. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| 13 | #ifndef LLVM_EXECUTIONENGINE_ORC_JITTARGETMACHINEBUILDER_H |
| 14 | #define LLVM_EXECUTIONENGINE_ORC_JITTARGETMACHINEBUILDER_H |
| 15 | |
| 16 | #include "llvm/Support/CodeGen.h" |
| 17 | #include "llvm/Support/Compiler.h" |
| 18 | #include "llvm/Support/Error.h" |
| 19 | #include "llvm/Target/TargetMachine.h" |
| 20 | #include "llvm/Target/TargetOptions.h" |
| 21 | #include "llvm/TargetParser/SubtargetFeature.h" |
| 22 | #include "llvm/TargetParser/Triple.h" |
| 23 | #include <memory> |
| 24 | #include <optional> |
| 25 | #include <string> |
| 26 | #include <vector> |
| 27 | |
| 28 | namespace llvm { |
| 29 | |
| 30 | class raw_ostream; |
| 31 | |
| 32 | namespace orc { |
| 33 | |
| 34 | /// A utility class for building TargetMachines for JITs. |
| 35 | class JITTargetMachineBuilder { |
| 36 | #ifndef NDEBUG |
| 37 | friend class JITTargetMachineBuilderPrinter; |
| 38 | #endif |
| 39 | public: |
| 40 | /// Create a JITTargetMachineBuilder based on the given triple. |
| 41 | /// |
| 42 | /// Note: TargetOptions is default-constructed, then EmulatedTLS is set to |
| 43 | /// true. If EmulatedTLS is not required, these values should be reset before |
| 44 | /// calling createTargetMachine. |
| 45 | LLVM_ABI JITTargetMachineBuilder(Triple TT); |
| 46 | |
| 47 | /// Create a JITTargetMachineBuilder for the host system. |
| 48 | /// |
| 49 | /// Note: TargetOptions is default-constructed, then EmulatedTLS is set to |
| 50 | /// true. If EmulatedTLS is not required, these values should be reset before |
| 51 | /// calling createTargetMachine. |
| 52 | LLVM_ABI static Expected<JITTargetMachineBuilder> detectHost(); |
| 53 | |
| 54 | /// Create a TargetMachine. |
| 55 | /// |
| 56 | /// This operation will fail if the requested target is not registered, |
| 57 | /// in which case see llvm/Support/TargetSelect.h. To JIT IR the Target and |
| 58 | /// the target's AsmPrinter must both be registered. To JIT assembly |
| 59 | /// (including inline and module level assembly) the target's AsmParser must |
| 60 | /// also be registered. |
| 61 | LLVM_ABI Expected<std::unique_ptr<TargetMachine>> createTargetMachine(); |
| 62 | |
| 63 | /// Get the default DataLayout for the target. |
| 64 | /// |
| 65 | /// Note: This is reasonably expensive, as it creates a temporary |
| 66 | /// TargetMachine instance under the hood. It is only suitable for use during |
| 67 | /// JIT setup. |
| 68 | Expected<DataLayout> getDefaultDataLayoutForTarget() { |
| 69 | auto TM = createTargetMachine(); |
| 70 | if (!TM) |
| 71 | return TM.takeError(); |
| 72 | return (*TM)->createDataLayout(); |
| 73 | } |
| 74 | |
| 75 | /// Set the CPU string. |
| 76 | JITTargetMachineBuilder &setCPU(std::string CPU) { |
| 77 | this->CPU = std::move(CPU); |
| 78 | return *this; |
| 79 | } |
| 80 | |
| 81 | /// Returns the CPU string. |
| 82 | const std::string &getCPU() const { return CPU; } |
| 83 | |
| 84 | /// Set the relocation model. |
| 85 | JITTargetMachineBuilder &setRelocationModel(std::optional<Reloc::Model> RM) { |
| 86 | this->RM = std::move(RM); |
| 87 | return *this; |
| 88 | } |
| 89 | |
| 90 | /// Get the relocation model. |
| 91 | const std::optional<Reloc::Model> &getRelocationModel() const { return RM; } |
| 92 | |
| 93 | /// Set the code model. |
| 94 | JITTargetMachineBuilder &setCodeModel(std::optional<CodeModel::Model> CM) { |
| 95 | this->CM = std::move(CM); |
| 96 | return *this; |
| 97 | } |
| 98 | |
| 99 | /// Get the code model. |
| 100 | const std::optional<CodeModel::Model> &getCodeModel() const { return CM; } |
| 101 | |
| 102 | /// Set the LLVM CodeGen optimization level. |
| 103 | JITTargetMachineBuilder &setCodeGenOptLevel(CodeGenOptLevel OptLevel) { |
| 104 | this->OptLevel = OptLevel; |
| 105 | return *this; |
| 106 | } |
| 107 | |
| 108 | /// Set subtarget features. |
| 109 | JITTargetMachineBuilder &setFeatures(StringRef FeatureString) { |
| 110 | Features = SubtargetFeatures(FeatureString); |
| 111 | return *this; |
| 112 | } |
| 113 | |
| 114 | /// Add subtarget features. |
| 115 | LLVM_ABI JITTargetMachineBuilder & |
| 116 | addFeatures(const std::vector<std::string> &FeatureVec); |
| 117 | |
| 118 | /// Access subtarget features. |
| 119 | SubtargetFeatures &getFeatures() { return Features; } |
| 120 | |
| 121 | /// Access subtarget features. |
| 122 | const SubtargetFeatures &getFeatures() const { return Features; } |
| 123 | |
| 124 | /// Set TargetOptions. |
| 125 | /// |
| 126 | /// Note: This operation will overwrite any previously configured options, |
| 127 | /// including EmulatedTLS and UseInitArray which the JITTargetMachineBuilder |
| 128 | /// sets by default. Clients are responsible for re-enabling these overwritten |
| 129 | /// options. |
| 130 | JITTargetMachineBuilder &setOptions(TargetOptions Options) { |
| 131 | this->Options = std::move(Options); |
| 132 | return *this; |
| 133 | } |
| 134 | |
| 135 | /// Access TargetOptions. |
| 136 | TargetOptions &getOptions() { return Options; } |
| 137 | |
| 138 | /// Access TargetOptions. |
| 139 | const TargetOptions &getOptions() const { return Options; } |
| 140 | |
| 141 | /// Access Triple. |
| 142 | Triple &getTargetTriple() { return TT; } |
| 143 | |
| 144 | /// Access Triple. |
| 145 | const Triple &getTargetTriple() const { return TT; } |
| 146 | |
| 147 | private: |
| 148 | Triple TT; |
| 149 | std::string CPU; |
| 150 | SubtargetFeatures Features; |
| 151 | TargetOptions Options; |
| 152 | std::optional<Reloc::Model> RM; |
| 153 | std::optional<CodeModel::Model> CM; |
| 154 | CodeGenOptLevel OptLevel = CodeGenOptLevel::Default; |
| 155 | }; |
| 156 | |
| 157 | #ifndef NDEBUG |
| 158 | class JITTargetMachineBuilderPrinter { |
| 159 | public: |
| 160 | JITTargetMachineBuilderPrinter(JITTargetMachineBuilder &JTMB, |
| 161 | StringRef Indent) |
| 162 | : JTMB(JTMB), Indent(Indent) {} |
| 163 | void print(raw_ostream &OS) const; |
| 164 | |
| 165 | friend raw_ostream &operator<<(raw_ostream &OS, |
| 166 | const JITTargetMachineBuilderPrinter &JTMBP) { |
| 167 | JTMBP.print(OS); |
| 168 | return OS; |
| 169 | } |
| 170 | |
| 171 | private: |
| 172 | JITTargetMachineBuilder &JTMB; |
| 173 | StringRef Indent; |
| 174 | }; |
| 175 | #endif // NDEBUG |
| 176 | |
| 177 | } // end namespace orc |
| 178 | } // end namespace llvm |
| 179 | |
| 180 | #endif // LLVM_EXECUTIONENGINE_ORC_JITTARGETMACHINEBUILDER_H |
| 181 | |