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