LLVM 19.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
11#include "llvm/ADT/StringMap.h"
15
16namespace llvm {
17namespace orc {
18
20 : TT(std::move(TT)) {
21 Options.EmulatedTLS = true;
22 Options.UseInitArray = true;
23}
24
27
28 // Retrieve host CPU name and sub-target features and add them to builder.
29 // Relocation model, code model and codegen opt level are kept to default
30 // values.
33 for (auto &Feature : FeatureMap)
34 TMBuilder.getFeatures().AddFeature(Feature.first(), Feature.second);
35
36 TMBuilder.setCPU(std::string(llvm::sys::getHostCPUName()));
37
38 return TMBuilder;
39}
40
43
44 std::string ErrMsg;
45 auto *TheTarget = TargetRegistry::lookupTarget(TT.getTriple(), ErrMsg);
46 if (!TheTarget)
47 return make_error<StringError>(std::move(ErrMsg), inconvertibleErrorCode());
48
49 if (!TheTarget->hasJIT())
50 return make_error<StringError>("Target has no JIT support",
52
53 auto *TM =
54 TheTarget->createTargetMachine(TT.getTriple(), CPU, Features.getString(),
55 Options, RM, CM, OptLevel, /*JIT*/ true);
56 if (!TM)
57 return make_error<StringError>("Could not allocate target machine",
59
60 return std::unique_ptr<TargetMachine>(TM);
61}
62
64 const std::vector<std::string> &FeatureVec) {
65 for (const auto &F : FeatureVec)
66 Features.AddFeature(F);
67 return *this;
68}
69
70#ifndef NDEBUG
72 OS << Indent << "{\n"
73 << Indent << " Triple = \"" << JTMB.TT.str() << "\"\n"
74 << Indent << " CPU = \"" << JTMB.CPU << "\"\n"
75 << Indent << " Features = \"" << JTMB.Features.getString() << "\"\n"
76 << Indent << " Options = <not-printable>\n"
77 << Indent << " Relocation Model = ";
78
79 if (JTMB.RM) {
80 switch (*JTMB.RM) {
81 case Reloc::Static:
82 OS << "Static";
83 break;
84 case Reloc::PIC_:
85 OS << "PIC_";
86 break;
88 OS << "DynamicNoPIC";
89 break;
90 case Reloc::ROPI:
91 OS << "ROPI";
92 break;
93 case Reloc::RWPI:
94 OS << "RWPI";
95 break;
97 OS << "ROPI_RWPI";
98 break;
99 }
100 } else
101 OS << "unspecified (will use target default)";
102
103 OS << "\n"
104 << Indent << " Code Model = ";
105
106 if (JTMB.CM) {
107 switch (*JTMB.CM) {
108 case CodeModel::Tiny:
109 OS << "Tiny";
110 break;
111 case CodeModel::Small:
112 OS << "Small";
113 break;
115 OS << "Kernel";
116 break;
118 OS << "Medium";
119 break;
120 case CodeModel::Large:
121 OS << "Large";
122 break;
123 }
124 } else
125 OS << "unspecified (will use target default)";
126
127 OS << "\n"
128 << Indent << " Optimization Level = ";
129 switch (JTMB.OptLevel) {
131 OS << "None";
132 break;
134 OS << "Less";
135 break;
137 OS << "Default";
138 break;
140 OS << "Aggressive";
141 break;
142 }
143
144 OS << "\n" << Indent << "}\n";
145}
146#endif // NDEBUG
147
148} // End namespace orc.
149} // End namespace llvm.
This file defines the StringMap class.
#define F(x, y, z)
Definition: MD5.cpp:55
const char LLVMTargetMachineRef TM
raw_pwrite_stream & OS
Tagged union holding either a T or a Error.
Definition: Error.h:481
StringMap - This is an unconventional map that is specialized for handling keys that are "strings",...
Definition: StringMap.h:128
std::string getString() const
Returns features as a string.
void AddFeature(StringRef String, bool Enable=true)
Adds Features.
unsigned UseInitArray
UseInitArray - Use .init_array instead of .ctors for static constructors.
unsigned EmulatedTLS
EmulatedTLS - This flag enables emulated TLS model, using emutls function in the runtime library.
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
const std::string & str() const
Definition: Triple.h:440
const std::string & getTriple() const
Definition: Triple.h:442
A utility class for building TargetMachines for JITs.
JITTargetMachineBuilder & addFeatures(const std::vector< std::string > &FeatureVec)
Add subtarget features.
SubtargetFeatures & getFeatures()
Access subtarget features.
static Expected< JITTargetMachineBuilder > detectHost()
Create a JITTargetMachineBuilder for the host system.
JITTargetMachineBuilder(Triple TT)
Create a JITTargetMachineBuilder based on the given triple.
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:52
@ DynamicNoPIC
Definition: CodeGen.h:25
@ ROPI_RWPI
Definition: CodeGen.h:25
StringRef getHostCPUName()
getHostCPUName - Get the LLVM name for the host CPU.
Definition: Host.cpp:1695
bool getHostCPUFeatures(StringMap< bool, MallocAllocator > &Features)
getHostCPUFeatures - Get the LLVM names for the host CPU features.
std::string getProcessTriple()
getProcessTriple() - Return an appropriate target triple for generating code to be loaded into the cu...
Definition: Host.cpp:2040
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
std::error_code inconvertibleErrorCode()
The value returned by this function can be returned from convertToErrorCode for Error values where no...
Definition: Error.cpp:98
const std::vector< TensorSpec > FeatureMap
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:1849
Implement std::hash so that hash_code can be used in STL containers.
Definition: BitVector.h:858
static const Target * lookupTarget(StringRef Triple, std::string &Error)
lookupTarget - Lookup a target based on a target triple.