LLVM 24.0.0git
JITTargetMachineBuilder.cpp
Go to the documentation of this file.
1//===----- JITTargetMachineBuilder.cpp - Build TargetMachines for JIT -----===//
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
10
14
15namespace llvm {
16namespace orc {
17
19 : TT(std::move(TT)) {
20 Options.EmulatedTLS = true;
21 Options.UseInitArray = true;
22}
23
26
27 // Retrieve host CPU name and sub-target features and add them to builder.
28 // Relocation model, code model and codegen opt level are kept to default
29 // values.
30 for (const auto &Feature : llvm::sys::getHostCPUFeatures())
31 TMBuilder.getFeatures().AddFeature(Feature.first(), Feature.second);
32
33 TMBuilder.setCPU(std::string(llvm::sys::getHostCPUName()));
34
35 return TMBuilder;
36}
37
40
41 std::string ErrMsg;
42 auto *TheTarget = TargetRegistry::lookupTarget(TT, ErrMsg);
43 if (!TheTarget)
44 return make_error<StringError>(std::move(ErrMsg), inconvertibleErrorCode());
45
46 if (!TheTarget->hasJIT())
47 return make_error<StringError>("Target has no JIT support",
49
50 auto *TM = TheTarget->createTargetMachine(
51 TT, CPU, Features.getString(), Options, RM, CM, OptLevel, /*JIT=*/true);
52 if (!TM)
53 return make_error<StringError>("Could not allocate target machine",
55
56 return std::unique_ptr<TargetMachine>(TM);
57}
58
61 return DataLayout::parse(
62 TT.computeDataLayout(Options.MCOptions.getABIName()));
63}
64
66 const std::vector<std::string> &FeatureVec) {
67 for (const auto &F : FeatureVec)
68 Features.AddFeature(F);
69 return *this;
70}
71
72#ifndef NDEBUG
74 OS << Indent << "{\n"
75 << Indent << " Triple = \"" << JTMB.TT.str() << "\"\n"
76 << Indent << " CPU = \"" << JTMB.CPU << "\"\n"
77 << Indent << " Features = \"" << JTMB.Features.getString() << "\"\n"
78 << Indent << " Options = <not-printable>\n"
79 << Indent << " Relocation Model = ";
80
81 if (JTMB.RM) {
82 switch (*JTMB.RM) {
83 case Reloc::Static:
84 OS << "Static";
85 break;
86 case Reloc::PIC_:
87 OS << "PIC_";
88 break;
90 OS << "DynamicNoPIC";
91 break;
92 case Reloc::ROPI:
93 OS << "ROPI";
94 break;
95 case Reloc::RWPI:
96 OS << "RWPI";
97 break;
99 OS << "ROPI_RWPI";
100 break;
101 }
102 } else
103 OS << "unspecified (will use target default)";
104
105 OS << "\n"
106 << Indent << " Code Model = ";
107
108 if (JTMB.CM) {
109 switch (*JTMB.CM) {
110 case CodeModel::Tiny:
111 OS << "Tiny";
112 break;
113 case CodeModel::Small:
114 OS << "Small";
115 break;
117 OS << "Kernel";
118 break;
120 OS << "Medium";
121 break;
122 case CodeModel::Large:
123 OS << "Large";
124 break;
125 }
126 } else
127 OS << "unspecified (will use target default)";
128
129 OS << "\n"
130 << Indent << " Optimization Level = ";
131 switch (JTMB.OptLevel) {
133 OS << "None";
134 break;
136 OS << "Less";
137 break;
139 OS << "Default";
140 break;
142 OS << "Aggressive";
143 break;
144 }
145
146 OS << "\n" << Indent << "}\n";
147}
148#endif // NDEBUG
149
150} // End namespace orc.
151} // End namespace llvm.
#define F(x, y, z)
Definition MD5.cpp:54
static LLVM_ABI Expected< DataLayout > parse(StringRef LayoutString)
Parse a data layout string and return the layout.
Tagged union holding either a T or a Error.
Definition Error.h:485
LLVM_ABI void AddFeature(StringRef String, bool Enable=true)
Adds Features.
Triple - Helper class for working with autoconf configuration names.
Definition Triple.h:48
LLVM_ABI void print(raw_ostream &OS) const
LLVM_ABI Expected< DataLayout > getDefaultDataLayoutForTarget() const
Get the default DataLayout for the target.
LLVM_ABI JITTargetMachineBuilder & addFeatures(const std::vector< std::string > &FeatureVec)
Add subtarget features.
SubtargetFeatures & getFeatures()
Access subtarget features.
static LLVM_ABI Expected< JITTargetMachineBuilder > detectHost()
Create a JITTargetMachineBuilder for the host system.
LLVM_ABI JITTargetMachineBuilder(Triple TT)
Create a JITTargetMachineBuilder based on the given triple.
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
@ DynamicNoPIC
Definition CodeGen.h:26
LLVM_ABI StringMap< bool, MallocAllocator > getHostCPUFeatures()
getHostCPUFeatures - Get the LLVM names for the host CPU features.
Definition Host.cpp:2640
LLVM_ABI StringRef getHostCPUName()
getHostCPUName - Get the LLVM name for the host CPU.
Definition Host.cpp:2046
LLVM_ABI std::string getProcessTriple()
getProcessTriple() - Return an appropriate target triple for generating code to be loaded into the cu...
Definition Host.cpp:2674
This is an optimization pass for GlobalISel generic memory operations.
LLVM_ABI std::error_code inconvertibleErrorCode()
The value returned by this function can be returned from convertToErrorCode for Error values where no...
Definition Error.cpp:94
@ Default
-O2, -Os, -Oz
Definition CodeGen.h:230
Error make_error(ArgTs &&... Args)
Make a Error instance representing failure using the given error info type.
Definition Error.h:340
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1933
Implement std::hash so that hash_code can be used in STL containers.
Definition BitVector.h:878
static LLVM_ABI const Target * lookupTarget(const Triple &TheTriple, std::string &Error)
lookupTarget - Lookup a target based on a target triple.