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