LLVM  10.0.0
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 namespace orc {
29 
30 /// A utility class for building TargetMachines for JITs.
32 public:
33  /// Create a JITTargetMachineBuilder based on the given triple.
34  ///
35  /// Note: TargetOptions is default-constructed, then EmulatedTLS and
36  /// ExplicitEmulatedTLS are set to true. If EmulatedTLS is not
37  /// required, these values should be reset before calling
38  /// createTargetMachine.
40 
41  /// Create a JITTargetMachineBuilder for the host system.
42  ///
43  /// Note: TargetOptions is default-constructed, then EmulatedTLS and
44  /// ExplicitEmulatedTLS are set to true. If EmulatedTLS is not
45  /// required, these values should be reset before calling
46  /// createTargetMachine.
48 
49  /// Create a TargetMachine.
50  ///
51  /// This operation will fail if the requested target is not registered,
52  /// in which case see llvm/Support/TargetSelect.h. To JIT IR the Target and
53  /// the target's AsmPrinter must both be registered. To JIT assembly
54  /// (including inline and module level assembly) the target's AsmParser must
55  /// also be registered.
57 
58  /// Get the default DataLayout for the target.
59  ///
60  /// Note: This is reasonably expensive, as it creates a temporary
61  /// TargetMachine instance under the hood. It is only suitable for use during
62  /// JIT setup.
64  auto TM = createTargetMachine();
65  if (!TM)
66  return TM.takeError();
67  return (*TM)->createDataLayout();
68  }
69 
70  /// Set the CPU string.
71  JITTargetMachineBuilder &setCPU(std::string CPU) {
72  this->CPU = std::move(CPU);
73  return *this;
74  }
75 
76  /// Set the relocation model.
78  this->RM = std::move(RM);
79  return *this;
80  }
81 
82  /// Get the relocation model.
83  const Optional<Reloc::Model> &getRelocationModel() const { return RM; }
84 
85  /// Set the code model.
87  this->CM = std::move(CM);
88  return *this;
89  }
90 
91  /// Get the code model.
92  const Optional<CodeModel::Model> &getCodeModel() const { return CM; }
93 
94  /// Set the LLVM CodeGen optimization level.
96  this->OptLevel = OptLevel;
97  return *this;
98  }
99 
100  /// Set subtarget features.
102  Features = SubtargetFeatures(FeatureString);
103  return *this;
104  }
105 
106  /// Add subtarget features.
108  addFeatures(const std::vector<std::string> &FeatureVec);
109 
110  /// Access subtarget features.
111  SubtargetFeatures &getFeatures() { return Features; }
112 
113  /// Access subtarget features.
114  const SubtargetFeatures &getFeatures() const { return Features; }
115 
116  /// Set TargetOptions.
117  ///
118  /// Note: This operation will overwrite any previously configured options,
119  /// including EmulatedTLS and ExplicitEmulatedTLS which
120  /// the JITTargetMachineBuilder sets by default. Clients are responsible
121  /// for re-enabling these overwritten options.
123  this->Options = std::move(Options);
124  return *this;
125  }
126 
127  /// Access TargetOptions.
128  TargetOptions &getOptions() { return Options; }
129 
130  /// Access TargetOptions.
131  const TargetOptions &getOptions() const { return Options; }
132 
133  /// Access Triple.
134  Triple &getTargetTriple() { return TT; }
135 
136  /// Access Triple.
137  const Triple &getTargetTriple() const { return TT; }
138 
139 private:
140  Triple TT;
141  std::string CPU;
142  SubtargetFeatures Features;
143  TargetOptions Options;
147 };
148 
149 } // end namespace orc
150 } // end namespace llvm
151 
152 #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.
Tagged union holding either a T or a Error.
Definition: APFloat.h:41
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:43
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.
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:54
A utility class for building TargetMachines for JITs.
const Optional< Reloc::Model > & getRelocationModel() const
Get the relocation model.