LLVM  14.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/Support/Host.h"
14 
15 namespace llvm {
16 namespace orc {
17 
19  : TT(std::move(TT)) {
20  Options.EmulatedTLS = true;
21  Options.ExplicitEmulatedTLS = true;
22 }
23 
25  // FIXME: getProcessTriple is bogus. It returns the host LLVM was compiled on,
26  // rather than a valid triple for the current process.
28 
29  // Retrieve host CPU name and sub-target features and add them to builder.
30  // Relocation model, code model and codegen opt level are kept to default
31  // values.
32  llvm::StringMap<bool> FeatureMap;
34  for (auto &Feature : FeatureMap)
35  TMBuilder.getFeatures().AddFeature(Feature.first(), Feature.second);
36 
37  TMBuilder.setCPU(std::string(llvm::sys::getHostCPUName()));
38 
39  return TMBuilder;
40 }
41 
44 
45  std::string ErrMsg;
46  auto *TheTarget = TargetRegistry::lookupTarget(TT.getTriple(), ErrMsg);
47  if (!TheTarget)
48  return make_error<StringError>(std::move(ErrMsg), inconvertibleErrorCode());
49 
50  auto *TM =
51  TheTarget->createTargetMachine(TT.getTriple(), CPU, Features.getString(),
52  Options, RM, CM, OptLevel, /*JIT*/ true);
53  if (!TM)
54  return make_error<StringError>("Could not allocate target machine",
56 
57  return std::unique_ptr<TargetMachine>(TM);
58 }
59 
61  const std::vector<std::string> &FeatureVec) {
62  for (const auto &F : FeatureVec)
63  Features.AddFeature(F);
64  return *this;
65 }
66 
67 #ifndef NDEBUG
69  OS << Indent << "{\n"
70  << Indent << " Triple = \"" << JTMB.TT.str() << "\"\n"
71  << Indent << " CPU = \"" << JTMB.CPU << "\"\n"
72  << Indent << " Features = \"" << JTMB.Features.getString() << "\"\n"
73  << Indent << " Options = <not-printable>\n"
74  << Indent << " Relocation Model = ";
75 
76  if (JTMB.RM) {
77  switch (*JTMB.RM) {
78  case Reloc::Static:
79  OS << "Static";
80  break;
81  case Reloc::PIC_:
82  OS << "PIC_";
83  break;
85  OS << "DynamicNoPIC";
86  break;
87  case Reloc::ROPI:
88  OS << "ROPI";
89  break;
90  case Reloc::RWPI:
91  OS << "RWPI";
92  break;
93  case Reloc::ROPI_RWPI:
94  OS << "ROPI_RWPI";
95  break;
96  }
97  } else
98  OS << "unspecified (will use target default)";
99 
100  OS << "\n"
101  << Indent << " Code Model = ";
102 
103  if (JTMB.CM) {
104  switch (*JTMB.CM) {
105  case CodeModel::Tiny:
106  OS << "Tiny";
107  break;
108  case CodeModel::Small:
109  OS << "Small";
110  break;
111  case CodeModel::Kernel:
112  OS << "Kernel";
113  break;
114  case CodeModel::Medium:
115  OS << "Medium";
116  break;
117  case CodeModel::Large:
118  OS << "Large";
119  break;
120  }
121  } else
122  OS << "unspecified (will use target default)";
123 
124  OS << "\n"
125  << Indent << " Optimization Level = ";
126  switch (JTMB.OptLevel) {
127  case CodeGenOpt::None:
128  OS << "None";
129  break;
130  case CodeGenOpt::Less:
131  OS << "Less";
132  break;
133  case CodeGenOpt::Default:
134  OS << "Default";
135  break;
137  OS << "Aggressive";
138  break;
139  }
140 
141  OS << "\n" << Indent << "}\n";
142 }
143 #endif // NDEBUG
144 
145 } // End namespace orc.
146 } // End namespace llvm.
llvm::orc::JITTargetMachineBuilder
A utility class for building TargetMachines for JITs.
Definition: JITTargetMachineBuilder.h:34
llvm
---------------------— PointerInfo ------------------------------------—
Definition: AllocatorList.h:23
llvm::orc::JITTargetMachineBuilder::createTargetMachine
Expected< std::unique_ptr< TargetMachine > > createTargetMachine()
Create a TargetMachine.
Definition: JITTargetMachineBuilder.cpp:43
llvm::SubtargetFeatures::AddFeature
void AddFeature(StringRef String, bool Enable=true)
Adds Features.
Definition: SubtargetFeature.cpp:41
llvm::CodeModel::Medium
@ Medium
Definition: CodeGen.h:28
Host.h
llvm::Triple
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:45
llvm::sys::getHostCPUFeatures
bool getHostCPUFeatures(StringMap< bool, MallocAllocator > &Features)
getHostCPUFeatures - Get the LLVM names for the host CPU features.
llvm::Reloc::ROPI_RWPI
@ ROPI_RWPI
Definition: CodeGen.h:22
llvm::orc::JITTargetMachineBuilder::getFeatures
SubtargetFeatures & getFeatures()
Access subtarget features.
Definition: JITTargetMachineBuilder.h:120
llvm::Expected
Tagged union holding either a T or a Error.
Definition: APFloat.h:42
llvm::CodeModel::Kernel
@ Kernel
Definition: CodeGen.h:28
llvm::TargetRegistry::lookupTarget
static const Target * lookupTarget(const std::string &Triple, std::string &Error)
lookupTarget - Lookup a target based on a target triple.
Definition: TargetRegistry.cpp:62
F
#define F(x, y, z)
Definition: MD5.cpp:56
llvm::Triple::str
const std::string & str() const
Definition: Triple.h:376
llvm::CodeModel::Small
@ Small
Definition: CodeGen.h:28
llvm::raw_ostream
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:53
llvm::TargetOptions::EmulatedTLS
unsigned EmulatedTLS
EmulatedTLS - This flag enables emulated TLS model, using emutls function in the runtime library.
Definition: TargetOptions.h:279
llvm::orc::JITTargetMachineBuilder::detectHost
static Expected< JITTargetMachineBuilder > detectHost()
Create a JITTargetMachineBuilder for the host system.
Definition: JITTargetMachineBuilder.cpp:24
llvm::Triple::getTriple
const std::string & getTriple() const
Definition: Triple.h:378
llvm::StringMap< bool >
llvm::orc::JITTargetMachineBuilder::setCPU
JITTargetMachineBuilder & setCPU(std::string CPU)
Set the CPU string.
Definition: JITTargetMachineBuilder.h:77
llvm::SubtargetFeatures::getString
std::string getString() const
Returns features as a string.
Definition: SubtargetFeature.cpp:54
llvm::CodeGenOpt::Default
@ Default
Definition: CodeGen.h:55
move
compiles ldr LCPI1_0 ldr ldr mov lsr tst moveq r1 ldr LCPI1_1 and r0 bx lr It would be better to do something like to fold the shift into the conditional move
Definition: README.txt:546
llvm::orc::JITTargetMachineBuilder::JITTargetMachineBuilder
JITTargetMachineBuilder(Triple TT)
Create a JITTargetMachineBuilder based on the given triple.
Definition: JITTargetMachineBuilder.cpp:18
llvm::move
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:1607
llvm::Reloc::ROPI
@ ROPI
Definition: CodeGen.h:22
llvm::CodeGenOpt::Aggressive
@ Aggressive
Definition: CodeGen.h:56
llvm::Reloc::DynamicNoPIC
@ DynamicNoPIC
Definition: CodeGen.h:22
llvm::CodeGenOpt::None
@ None
Definition: CodeGen.h:53
llvm::Reloc::RWPI
@ RWPI
Definition: CodeGen.h:22
llvm::Reloc::PIC_
@ PIC_
Definition: CodeGen.h:22
llvm::Reloc::Static
@ Static
Definition: CodeGen.h:22
llvm::CodeModel::Tiny
@ Tiny
Definition: CodeGen.h:28
std
Definition: BitVector.h:838
llvm::inconvertibleErrorCode
std::error_code inconvertibleErrorCode()
The value returned by this function can be returned from convertToErrorCode for Error values where no...
Definition: Error.cpp:77
llvm::sys::getProcessTriple
std::string getProcessTriple()
getProcessTriple() - Return an appropriate target triple for generating code to be loaded into the cu...
Definition: Host.cpp:1748
llvm::CodeModel::Large
@ Large
Definition: CodeGen.h:28
llvm::orc::JITTargetMachineBuilder::addFeatures
JITTargetMachineBuilder & addFeatures(const std::vector< std::string > &FeatureVec)
Add subtarget features.
Definition: JITTargetMachineBuilder.cpp:60
llvm::sys::getHostCPUName
StringRef getHostCPUName()
getHostCPUName - Get the LLVM name for the host CPU.
Definition: Host.cpp:1360
TM
const char LLVMTargetMachineRef TM
Definition: PassBuilderBindings.cpp:47
raw_ostream.h
llvm::CodeGenOpt::Less
@ Less
Definition: CodeGen.h:54
TargetRegistry.h
llvm::TargetOptions::ExplicitEmulatedTLS
unsigned ExplicitEmulatedTLS
Whether -emulated-tls or -no-emulated-tls is set.
Definition: TargetOptions.h:282
JITTargetMachineBuilder.h
llvm::orc::JITTargetMachineBuilderPrinter::print
void print(raw_ostream &OS) const
Definition: JITTargetMachineBuilder.cpp:68