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