1 //===- JITTargetMachineBuilder.h - Build TargetMachines for JIT -*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 // A utitily for building TargetMachines for JITs.
11 //===----------------------------------------------------------------------===//
13 #ifndef LLVM_EXECUTIONENGINE_ORC_JITTARGETMACHINEBUILDER_H
14 #define LLVM_EXECUTIONENGINE_ORC_JITTARGETMACHINEBUILDER_H
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"
30 /// A utility class for building TargetMachines for JITs.
31 class JITTargetMachineBuilder
{
33 /// Create a JITTargetMachineBuilder based on the given triple.
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
);
41 /// Create a JITTargetMachineBuilder for the host system.
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();
49 /// Create a TargetMachine.
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();
58 /// Get the default DataLayout for the target.
60 /// Note: This is reasonably expensive, as it creates a temporary
61 /// TargetMachine instance under the hood. It is only suitable for use during
63 Expected
<DataLayout
> getDefaultDataLayoutForTarget() {
64 auto TM
= createTargetMachine();
66 return TM
.takeError();
67 return (*TM
)->createDataLayout();
70 /// Set the CPU string.
71 JITTargetMachineBuilder
&setCPU(std::string CPU
) {
72 this->CPU
= std::move(CPU
);
76 /// Set the relocation model.
77 JITTargetMachineBuilder
&setRelocationModel(Optional
<Reloc::Model
> RM
) {
78 this->RM
= std::move(RM
);
82 /// Set the code model.
83 JITTargetMachineBuilder
&setCodeModel(Optional
<CodeModel::Model
> CM
) {
84 this->CM
= std::move(CM
);
88 /// Set the LLVM CodeGen optimization level.
89 JITTargetMachineBuilder
&setCodeGenOptLevel(CodeGenOpt::Level OptLevel
) {
90 this->OptLevel
= OptLevel
;
94 /// Add subtarget features.
95 JITTargetMachineBuilder
&
96 addFeatures(const std::vector
<std::string
> &FeatureVec
);
98 /// Access subtarget features.
99 SubtargetFeatures
&getFeatures() { return Features
; }
101 /// Access subtarget features.
102 const SubtargetFeatures
&getFeatures() const { return Features
; }
104 /// Access TargetOptions.
105 TargetOptions
&getOptions() { return Options
; }
107 /// Access TargetOptions.
108 const TargetOptions
&getOptions() const { return Options
; }
111 Triple
&getTargetTriple() { return TT
; }
114 const Triple
&getTargetTriple() const { return TT
; }
119 SubtargetFeatures Features
;
120 TargetOptions Options
;
121 Optional
<Reloc::Model
> RM
;
122 Optional
<CodeModel::Model
> CM
;
123 CodeGenOpt::Level OptLevel
= CodeGenOpt::None
;
126 } // end namespace orc
127 } // end namespace llvm
129 #endif // LLVM_EXECUTIONENGINE_ORC_JITTARGETMACHINEBUILDER_H