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"
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 
27 namespace llvm {
28 
29 class raw_ostream;
30 
31 namespace orc {
32 
33 /// A utility class for building TargetMachines for JITs.
34 class JITTargetMachineBuilder {
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.
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.
getDefaultDataLayoutForTarget()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.
setCPU(std::string CPU)74   JITTargetMachineBuilder &setCPU(std::string CPU) {
75     this->CPU = std::move(CPU);
76     return *this;
77   }
78 
79   /// Set the relocation model.
setRelocationModel(Optional<Reloc::Model> RM)80   JITTargetMachineBuilder &setRelocationModel(Optional<Reloc::Model> RM) {
81     this->RM = std::move(RM);
82     return *this;
83   }
84 
85   /// Get the relocation model.
getRelocationModel()86   const Optional<Reloc::Model> &getRelocationModel() const { return RM; }
87 
88   /// Set the code model.
setCodeModel(Optional<CodeModel::Model> CM)89   JITTargetMachineBuilder &setCodeModel(Optional<CodeModel::Model> CM) {
90     this->CM = std::move(CM);
91     return *this;
92   }
93 
94   /// Get the code model.
getCodeModel()95   const Optional<CodeModel::Model> &getCodeModel() const { return CM; }
96 
97   /// Set the LLVM CodeGen optimization level.
setCodeGenOptLevel(CodeGenOpt::Level OptLevel)98   JITTargetMachineBuilder &setCodeGenOptLevel(CodeGenOpt::Level OptLevel) {
99     this->OptLevel = OptLevel;
100     return *this;
101   }
102 
103   /// Set subtarget features.
setFeatures(StringRef FeatureString)104   JITTargetMachineBuilder &setFeatures(StringRef FeatureString) {
105     Features = SubtargetFeatures(FeatureString);
106     return *this;
107   }
108 
109   /// Add subtarget features.
110   JITTargetMachineBuilder &
111   addFeatures(const std::vector<std::string> &FeatureVec);
112 
113   /// Access subtarget features.
getFeatures()114   SubtargetFeatures &getFeatures() { return Features; }
115 
116   /// Access subtarget features.
getFeatures()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.
setOptions(TargetOptions Options)125   JITTargetMachineBuilder &setOptions(TargetOptions Options) {
126     this->Options = std::move(Options);
127     return *this;
128   }
129 
130   /// Access TargetOptions.
getOptions()131   TargetOptions &getOptions() { return Options; }
132 
133   /// Access TargetOptions.
getOptions()134   const TargetOptions &getOptions() const { return Options; }
135 
136   /// Access Triple.
getTargetTriple()137   Triple &getTargetTriple() { return TT; }
138 
139   /// Access Triple.
getTargetTriple()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;
153   Optional<Reloc::Model> RM;
154   Optional<CodeModel::Model> CM;
155   CodeGenOpt::Level OptLevel = CodeGenOpt::Default;
156 };
157 
158 } // end namespace orc
159 } // end namespace llvm
160 
161 #endif // LLVM_EXECUTIONENGINE_ORC_JITTARGETMACHINEBUILDER_H
162