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