LLVM  11.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 public:
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.
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.
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.
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.
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.
81  this->RM = std::move(RM);
82  return *this;
83  }
84 
85  /// Get the relocation model.
86  const Optional<Reloc::Model> &getRelocationModel() const { return RM; }
87 
88  /// Set the code model.
90  this->CM = std::move(CM);
91  return *this;
92  }
93 
94  /// Get the code model.
95  const Optional<CodeModel::Model> &getCodeModel() const { return CM; }
96 
97  /// Set the LLVM CodeGen optimization level.
99  this->OptLevel = OptLevel;
100  return *this;
101  }
102 
103  /// Set subtarget features.
105  Features = SubtargetFeatures(FeatureString);
106  return *this;
107  }
108 
109  /// Add subtarget features.
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 
119  /// 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.
126  this->Options = std::move(Options);
127  return *this;
128  }
129 
130  /// 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 
142 #ifndef NDEBUG
143  /// Debug-dump a JITTargetMachineBuilder.
144  friend raw_ostream &operator<<(raw_ostream &OS,
145  const JITTargetMachineBuilder &JTMB);
146 #endif
147 
148 private:
149  Triple TT;
150  std::string CPU;
151  SubtargetFeatures Features;
152  TargetOptions Options;
156 };
157 
158 } // end namespace orc
159 } // end namespace llvm
160 
161 #endif // LLVM_EXECUTIONENGINE_ORC_JITTARGETMACHINEBUILDER_H
const SubtargetFeatures & getFeatures() const
Access subtarget features.
JITTargetMachineBuilder & setOptions(TargetOptions Options)
Set TargetOptions.
This class represents lattice values for constants.
Definition: AllocatorList.h:23
const TargetOptions & getOptions() const
Access TargetOptions.
JITTargetMachineBuilder & setRelocationModel(Optional< Reloc::Model > RM)
Set the relocation model.
SubtargetFeatures & getFeatures()
Access subtarget features.
const Optional< CodeModel::Model > & getCodeModel() const
Get the code model.
friend raw_ostream & operator<<(raw_ostream &OS, const JITTargetMachineBuilder &JTMB)
Debug-dump a JITTargetMachineBuilder.
Tagged union holding either a T or a Error.
Definition: APFloat.h:42
static Expected< JITTargetMachineBuilder > detectHost()
Create a JITTargetMachineBuilder for the host system.
Expected< DataLayout > getDefaultDataLayoutForTarget()
Get the default DataLayout for the target.
JITTargetMachineBuilder(Triple TT)
Create a JITTargetMachineBuilder based on the given triple.
JITTargetMachineBuilder & addFeatures(const std::vector< std::string > &FeatureVec)
Add subtarget features.
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:45
JITTargetMachineBuilder & setFeatures(StringRef FeatureString)
Set subtarget features.
Manages the enabling and disabling of subtarget specific features.
JITTargetMachineBuilder & setCPU(std::string CPU)
Set the CPU string.
const Triple & getTargetTriple() const
Access Triple.
Expected< std::unique_ptr< TargetMachine > > createTargetMachine()
Create a TargetMachine.
TargetOptions & getOptions()
Access TargetOptions.
JITTargetMachineBuilder & setCodeGenOptLevel(CodeGenOpt::Level OptLevel)
Set the LLVM CodeGen optimization level.
JITTargetMachineBuilder & setCodeModel(Optional< CodeModel::Model > CM)
Set the code model.
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:46
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:57
A utility class for building TargetMachines for JITs.
const Optional< Reloc::Model > & getRelocationModel() const
Get the relocation model.