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