1 //===- JITTargetMachineBuilder.h - Build TargetMachines for JIT -*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // A utitily for building TargetMachines for JITs.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_EXECUTIONENGINE_ORC_JITTARGETMACHINEBUILDER_H
15 #define LLVM_EXECUTIONENGINE_ORC_JITTARGETMACHINEBUILDER_H
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"
31 /// A utility class for building TargetMachines for JITs.
32 class JITTargetMachineBuilder {
34 /// Create a JITTargetMachineBuilder based on the given triple.
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);
42 /// Create a JITTargetMachineBuilder for the host system.
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();
50 /// Create a TargetMachine.
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();
59 /// Get the default DataLayout for the target.
61 /// Note: This is reasonably expensive, as it creates a temporary
62 /// TargetMachine instance under the hood. It is only suitable for use during
64 Expected<DataLayout> getDefaultDataLayoutForTarget() {
65 auto TM = createTargetMachine();
67 return TM.takeError();
68 return (*TM)->createDataLayout();
71 /// Set the CPU string.
72 JITTargetMachineBuilder &setCPU(std::string CPU) {
73 this->CPU = std::move(CPU);
77 /// Set the relocation model.
78 JITTargetMachineBuilder &setRelocationModel(Optional<Reloc::Model> RM) {
79 this->RM = std::move(RM);
83 /// Set the code model.
84 JITTargetMachineBuilder &setCodeModel(Optional<CodeModel::Model> CM) {
85 this->CM = std::move(CM);
89 /// Set the LLVM CodeGen optimization level.
90 JITTargetMachineBuilder &setCodeGenOptLevel(CodeGenOpt::Level OptLevel) {
91 this->OptLevel = OptLevel;
95 /// Add subtarget features.
96 JITTargetMachineBuilder &
97 addFeatures(const std::vector<std::string> &FeatureVec);
99 /// Access subtarget features.
100 SubtargetFeatures &getFeatures() { return Features; }
102 /// Access subtarget features.
103 const SubtargetFeatures &getFeatures() const { return Features; }
105 /// Access TargetOptions.
106 TargetOptions &getOptions() { return Options; }
108 /// Access TargetOptions.
109 const TargetOptions &getOptions() const { return Options; }
112 Triple &getTargetTriple() { return TT; }
115 const Triple &getTargetTriple() const { return TT; }
120 SubtargetFeatures Features;
121 TargetOptions Options;
122 Optional<Reloc::Model> RM;
123 Optional<CodeModel::Model> CM;
124 CodeGenOpt::Level OptLevel = CodeGenOpt::None;
127 } // end namespace orc
128 } // end namespace llvm
130 #endif // LLVM_EXECUTIONENGINE_ORC_JITTARGETMACHINEBUILDER_H