LLVM  14.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 
16 #include "llvm/ADT/Optional.h"
17 #include "llvm/ADT/Triple.h"
19 #include "llvm/Support/CodeGen.h"
20 #include "llvm/Support/Error.h"
23 #include <memory>
24 #include <string>
25 #include <vector>
26 
27 namespace llvm {
28 
29 class raw_ostream;
30 
31 namespace orc {
32 
33 /// A utility class for building TargetMachines for JITs.
35 #ifndef NDEBUG
37 #endif
38 public:
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.
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.
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.
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.
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  /// Returns the CPU string.
83  const std::string &getCPU() const { return CPU; }
84 
85  /// Set the relocation model.
87  this->RM = std::move(RM);
88  return *this;
89  }
90 
91  /// Get the relocation model.
92  const Optional<Reloc::Model> &getRelocationModel() const { return RM; }
93 
94  /// Set the code model.
96  this->CM = std::move(CM);
97  return *this;
98  }
99 
100  /// Get the code model.
101  const Optional<CodeModel::Model> &getCodeModel() const { return CM; }
102 
103  /// Set the LLVM CodeGen optimization level.
105  this->OptLevel = OptLevel;
106  return *this;
107  }
108 
109  /// Set subtarget features.
111  Features = SubtargetFeatures(FeatureString);
112  return *this;
113  }
114 
115  /// Add subtarget features.
117  addFeatures(const std::vector<std::string> &FeatureVec);
118 
119  /// Access subtarget features.
120  SubtargetFeatures &getFeatures() { return Features; }
121 
122  /// Access subtarget features.
123  const SubtargetFeatures &getFeatures() const { return Features; }
124 
125  /// Set TargetOptions.
126  ///
127  /// Note: This operation will overwrite any previously configured options,
128  /// including EmulatedTLS and ExplicitEmulatedTLS which
129  /// the JITTargetMachineBuilder sets by default. Clients are responsible
130  /// for re-enabling these overwritten options.
132  this->Options = std::move(Options);
133  return *this;
134  }
135 
136  /// Access TargetOptions.
138 
139  /// Access TargetOptions.
140  const TargetOptions &getOptions() const { return Options; }
141 
142  /// Access Triple.
143  Triple &getTargetTriple() { return TT; }
144 
145  /// Access Triple.
146  const Triple &getTargetTriple() const { return TT; }
147 
148 private:
149  Triple TT;
150  std::string CPU;
151  SubtargetFeatures Features;
156 };
157 
158 #ifndef NDEBUG
160 public:
162  StringRef Indent)
163  : JTMB(JTMB), Indent(Indent) {}
164  void print(raw_ostream &OS) const;
165 
167  const JITTargetMachineBuilderPrinter &JTMBP) {
168  JTMBP.print(OS);
169  return OS;
170  }
171 
172 private:
174  StringRef Indent;
175 };
176 #endif // NDEBUG
177 
178 } // end namespace orc
179 } // end namespace llvm
180 
181 #endif // LLVM_EXECUTIONENGINE_ORC_JITTARGETMACHINEBUILDER_H
llvm::orc::JITTargetMachineBuilder::getDefaultDataLayoutForTarget
Expected< DataLayout > getDefaultDataLayoutForTarget()
Get the default DataLayout for the target.
Definition: JITTargetMachineBuilder.h:69
llvm::orc::JITTargetMachineBuilder
A utility class for building TargetMachines for JITs.
Definition: JITTargetMachineBuilder.h:34
llvm
---------------------— PointerInfo ------------------------------------—
Definition: AllocatorList.h:23
llvm::orc::JITTargetMachineBuilder::createTargetMachine
Expected< std::unique_ptr< TargetMachine > > createTargetMachine()
Create a TargetMachine.
Definition: JITTargetMachineBuilder.cpp:43
Optional.h
llvm::TargetOptions
Definition: TargetOptions.h:124
llvm::orc::JITTargetMachineBuilderPrinter::operator<<
friend raw_ostream & operator<<(raw_ostream &OS, const JITTargetMachineBuilderPrinter &JTMBP)
Definition: JITTargetMachineBuilder.h:166
llvm::orc::JITTargetMachineBuilder::getFeatures
const SubtargetFeatures & getFeatures() const
Access subtarget features.
Definition: JITTargetMachineBuilder.h:123
llvm::orc::JITTargetMachineBuilder::getOptions
const TargetOptions & getOptions() const
Access TargetOptions.
Definition: JITTargetMachineBuilder.h:140
Error.h
llvm::Triple
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:45
llvm::Optional< Reloc::Model >
llvm::orc::JITTargetMachineBuilder::getFeatures
SubtargetFeatures & getFeatures()
Access subtarget features.
Definition: JITTargetMachineBuilder.h:120
llvm::orc::JITTargetMachineBuilder::setRelocationModel
JITTargetMachineBuilder & setRelocationModel(Optional< Reloc::Model > RM)
Set the relocation model.
Definition: JITTargetMachineBuilder.h:86
llvm::Expected
Tagged union holding either a T or a Error.
Definition: APFloat.h:42
llvm::orc::JITTargetMachineBuilder::getTargetTriple
const Triple & getTargetTriple() const
Access Triple.
Definition: JITTargetMachineBuilder.h:146
SubtargetFeature.h
TargetMachine.h
llvm::orc::JITTargetMachineBuilder::setCodeModel
JITTargetMachineBuilder & setCodeModel(Optional< CodeModel::Model > CM)
Set the code model.
Definition: JITTargetMachineBuilder.h:95
llvm::SubtargetFeatures
Manages the enabling and disabling of subtarget specific features.
Definition: SubtargetFeature.h:183
Options
const char LLVMTargetMachineRef LLVMPassBuilderOptionsRef Options
Definition: PassBuilderBindings.cpp:48
llvm::raw_ostream
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:53
llvm::orc::JITTargetMachineBuilder::detectHost
static Expected< JITTargetMachineBuilder > detectHost()
Create a JITTargetMachineBuilder for the host system.
Definition: JITTargetMachineBuilder.cpp:24
llvm::orc::JITTargetMachineBuilder::setCPU
JITTargetMachineBuilder & setCPU(std::string CPU)
Set the CPU string.
Definition: JITTargetMachineBuilder.h:77
llvm::CodeGenOpt::Default
@ Default
Definition: CodeGen.h:55
move
compiles ldr LCPI1_0 ldr ldr mov lsr tst moveq r1 ldr LCPI1_1 and r0 bx lr It would be better to do something like to fold the shift into the conditional move
Definition: README.txt:546
llvm::orc::JITTargetMachineBuilder::JITTargetMachineBuilder
JITTargetMachineBuilder(Triple TT)
Create a JITTargetMachineBuilder based on the given triple.
Definition: JITTargetMachineBuilder.cpp:18
llvm::orc::JITTargetMachineBuilderPrinter
Definition: JITTargetMachineBuilder.h:159
Triple.h
TargetOptions.h
llvm::AArch64::RM
@ RM
Definition: AArch64ISelLowering.h:472
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
llvm::CodeGenOpt::Level
Level
Definition: CodeGen.h:52
llvm::orc::JITTargetMachineBuilder::getCodeModel
const Optional< CodeModel::Model > & getCodeModel() const
Get the code model.
Definition: JITTargetMachineBuilder.h:101
llvm::orc::JITTargetMachineBuilder::setFeatures
JITTargetMachineBuilder & setFeatures(StringRef FeatureString)
Set subtarget features.
Definition: JITTargetMachineBuilder.h:110
llvm::orc::JITTargetMachineBuilder::setCodeGenOptLevel
JITTargetMachineBuilder & setCodeGenOptLevel(CodeGenOpt::Level OptLevel)
Set the LLVM CodeGen optimization level.
Definition: JITTargetMachineBuilder.h:104
CodeGen.h
llvm::orc::JITTargetMachineBuilder::getOptions
TargetOptions & getOptions()
Access TargetOptions.
Definition: JITTargetMachineBuilder.h:137
llvm::orc::JITTargetMachineBuilder::addFeatures
JITTargetMachineBuilder & addFeatures(const std::vector< std::string > &FeatureVec)
Add subtarget features.
Definition: JITTargetMachineBuilder.cpp:60
llvm::orc::JITTargetMachineBuilder::getTargetTriple
Triple & getTargetTriple()
Access Triple.
Definition: JITTargetMachineBuilder.h:143
TM
const char LLVMTargetMachineRef TM
Definition: PassBuilderBindings.cpp:47
llvm::orc::JITTargetMachineBuilderPrinter::JITTargetMachineBuilderPrinter
JITTargetMachineBuilderPrinter(JITTargetMachineBuilder &JTMB, StringRef Indent)
Definition: JITTargetMachineBuilder.h:161
llvm::orc::JITTargetMachineBuilder::getCPU
const std::string & getCPU() const
Returns the CPU string.
Definition: JITTargetMachineBuilder.h:83
llvm::orc::JITTargetMachineBuilder::getRelocationModel
const Optional< Reloc::Model > & getRelocationModel() const
Get the relocation model.
Definition: JITTargetMachineBuilder.h:92
llvm::orc::JITTargetMachineBuilder::setOptions
JITTargetMachineBuilder & setOptions(TargetOptions Options)
Set TargetOptions.
Definition: JITTargetMachineBuilder.h:131
llvm::orc::JITTargetMachineBuilderPrinter::print
void print(raw_ostream &OS) const
Definition: JITTargetMachineBuilder.cpp:68