LLVM 22.0.0git
JITTargetMachineBuilder.h
Go to the documentation of this file.
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
18#include "llvm/Support/Error.h"
23#include <memory>
24#include <optional>
25#include <string>
26#include <vector>
27
28namespace llvm {
29
30class raw_ostream;
31
32namespace orc {
33
34/// A utility class for building TargetMachines for JITs.
36#ifndef NDEBUG
38#endif
39public:
40 /// Create a JITTargetMachineBuilder based on the given triple.
41 ///
42 /// Note: TargetOptions is default-constructed, then EmulatedTLS is set to
43 /// true. If EmulatedTLS is not required, these values should be reset before
44 /// calling createTargetMachine.
46
47 /// Create a JITTargetMachineBuilder for the host system.
48 ///
49 /// Note: TargetOptions is default-constructed, then EmulatedTLS is set to
50 /// true. If EmulatedTLS is not required, these values should be reset before
51 /// calling createTargetMachine.
53
54 /// Create a TargetMachine.
55 ///
56 /// This operation will fail if the requested target is not registered,
57 /// in which case see llvm/Support/TargetSelect.h. To JIT IR the Target and
58 /// the target's AsmPrinter must both be registered. To JIT assembly
59 /// (including inline and module level assembly) the target's AsmParser must
60 /// also be registered.
62
63 /// Get the default DataLayout for the target.
64 ///
65 /// Note: This is reasonably expensive, as it creates a temporary
66 /// TargetMachine instance under the hood. It is only suitable for use during
67 /// JIT setup.
69 auto TM = createTargetMachine();
70 if (!TM)
71 return TM.takeError();
72 return (*TM)->createDataLayout();
73 }
74
75 /// Set the CPU string.
76 JITTargetMachineBuilder &setCPU(std::string CPU) {
77 this->CPU = std::move(CPU);
78 return *this;
79 }
80
81 /// Returns the CPU string.
82 const std::string &getCPU() const { return CPU; }
83
84 /// Set the relocation model.
85 JITTargetMachineBuilder &setRelocationModel(std::optional<Reloc::Model> RM) {
86 this->RM = std::move(RM);
87 return *this;
88 }
89
90 /// Get the relocation model.
91 const std::optional<Reloc::Model> &getRelocationModel() const { return RM; }
92
93 /// Set the code model.
94 JITTargetMachineBuilder &setCodeModel(std::optional<CodeModel::Model> CM) {
95 this->CM = std::move(CM);
96 return *this;
97 }
98
99 /// Get the code model.
100 const std::optional<CodeModel::Model> &getCodeModel() const { return CM; }
101
102 /// Set the LLVM CodeGen optimization level.
104 this->OptLevel = OptLevel;
105 return *this;
106 }
107
108 /// Set subtarget features.
110 Features = SubtargetFeatures(FeatureString);
111 return *this;
112 }
113
114 /// Add subtarget features.
116 addFeatures(const std::vector<std::string> &FeatureVec);
117
118 /// Access subtarget features.
119 SubtargetFeatures &getFeatures() { return Features; }
120
121 /// Access subtarget features.
122 const SubtargetFeatures &getFeatures() const { return Features; }
123
124 /// Set TargetOptions.
125 ///
126 /// Note: This operation will overwrite any previously configured options,
127 /// including EmulatedTLS and UseInitArray which the JITTargetMachineBuilder
128 /// sets by default. Clients are responsible for re-enabling these overwritten
129 /// options.
131 this->Options = std::move(Options);
132 return *this;
133 }
134
135 /// Access TargetOptions.
137
138 /// Access TargetOptions.
139 const TargetOptions &getOptions() const { return Options; }
140
141 /// Access Triple.
142 Triple &getTargetTriple() { return TT; }
143
144 /// Access Triple.
145 const Triple &getTargetTriple() const { return TT; }
146
147private:
148 Triple TT;
149 std::string CPU;
150 SubtargetFeatures Features;
152 std::optional<Reloc::Model> RM;
153 std::optional<CodeModel::Model> CM;
155};
156
157#ifndef NDEBUG
159public:
161 StringRef Indent)
162 : JTMB(JTMB), Indent(Indent) {}
163 void print(raw_ostream &OS) const;
164
166 const JITTargetMachineBuilderPrinter &JTMBP) {
167 JTMBP.print(OS);
168 return OS;
169 }
170
171private:
173 StringRef Indent;
174};
175#endif // NDEBUG
176
177} // end namespace orc
178} // end namespace llvm
179
180#endif // LLVM_EXECUTIONENGINE_ORC_JITTARGETMACHINEBUILDER_H
#define LLVM_ABI
Definition: Compiler.h:213
static LVOptions Options
Definition: LVOptions.cpp:25
raw_pwrite_stream & OS
Tagged union holding either a T or a Error.
Definition: Error.h:485
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:55
Manages the enabling and disabling of subtarget specific features.
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:47
JITTargetMachineBuilderPrinter(JITTargetMachineBuilder &JTMB, StringRef Indent)
friend raw_ostream & operator<<(raw_ostream &OS, const JITTargetMachineBuilderPrinter &JTMBP)
A utility class for building TargetMachines for JITs.
JITTargetMachineBuilder & setCodeGenOptLevel(CodeGenOptLevel OptLevel)
Set the LLVM CodeGen optimization level.
const std::string & getCPU() const
Returns the CPU string.
const std::optional< Reloc::Model > & getRelocationModel() const
Get the relocation model.
TargetOptions & getOptions()
Access TargetOptions.
const std::optional< CodeModel::Model > & getCodeModel() const
Get the code model.
LLVM_ABI JITTargetMachineBuilder & addFeatures(const std::vector< std::string > &FeatureVec)
Add subtarget features.
SubtargetFeatures & getFeatures()
Access subtarget features.
JITTargetMachineBuilder & setOptions(TargetOptions Options)
Set TargetOptions.
const Triple & getTargetTriple() const
Access Triple.
JITTargetMachineBuilder & setRelocationModel(std::optional< Reloc::Model > RM)
Set the relocation model.
const TargetOptions & getOptions() const
Access TargetOptions.
static LLVM_ABI Expected< JITTargetMachineBuilder > detectHost()
Create a JITTargetMachineBuilder for the host system.
JITTargetMachineBuilder & setCodeModel(std::optional< CodeModel::Model > CM)
Set the code model.
Expected< DataLayout > getDefaultDataLayoutForTarget()
Get the default DataLayout for the target.
JITTargetMachineBuilder & setFeatures(StringRef FeatureString)
Set subtarget features.
const SubtargetFeatures & getFeatures() const
Access subtarget features.
LLVM_ABI Expected< std::unique_ptr< TargetMachine > > createTargetMachine()
Create a TargetMachine.
JITTargetMachineBuilder & setCPU(std::string CPU)
Set the CPU string.
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:53
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
CodeGenOptLevel
Code generation optimization level.
Definition: CodeGen.h:82