LLVM  15.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/MC/TargetRegistry.h"
12 #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  Options.UseInitArray = true;
23 }
24 
26  // FIXME: getProcessTriple is bogus. It returns the host LLVM was compiled on,
27  // rather than a valid triple for the current process.
29 
30  // Retrieve host CPU name and sub-target features and add them to builder.
31  // Relocation model, code model and codegen opt level are kept to default
32  // values.
35  for (auto &Feature : FeatureMap)
36  TMBuilder.getFeatures().AddFeature(Feature.first(), Feature.second);
37 
38  TMBuilder.setCPU(std::string(llvm::sys::getHostCPUName()));
39 
40  return TMBuilder;
41 }
42 
45 
46  std::string ErrMsg;
47  auto *TheTarget = TargetRegistry::lookupTarget(TT.getTriple(), ErrMsg);
48  if (!TheTarget)
49  return make_error<StringError>(std::move(ErrMsg), inconvertibleErrorCode());
50 
51  auto *TM =
52  TheTarget->createTargetMachine(TT.getTriple(), CPU, Features.getString(),
53  Options, RM, CM, OptLevel, /*JIT*/ true);
54  if (!TM)
55  return make_error<StringError>("Could not allocate target machine",
57 
58  return std::unique_ptr<TargetMachine>(TM);
59 }
60 
62  const std::vector<std::string> &FeatureVec) {
63  for (const auto &F : FeatureVec)
64  Features.AddFeature(F);
65  return *this;
66 }
67 
68 #ifndef NDEBUG
70  OS << Indent << "{\n"
71  << Indent << " Triple = \"" << JTMB.TT.str() << "\"\n"
72  << Indent << " CPU = \"" << JTMB.CPU << "\"\n"
73  << Indent << " Features = \"" << JTMB.Features.getString() << "\"\n"
74  << Indent << " Options = <not-printable>\n"
75  << Indent << " Relocation Model = ";
76 
77  if (JTMB.RM) {
78  switch (*JTMB.RM) {
79  case Reloc::Static:
80  OS << "Static";
81  break;
82  case Reloc::PIC_:
83  OS << "PIC_";
84  break;
86  OS << "DynamicNoPIC";
87  break;
88  case Reloc::ROPI:
89  OS << "ROPI";
90  break;
91  case Reloc::RWPI:
92  OS << "RWPI";
93  break;
94  case Reloc::ROPI_RWPI:
95  OS << "ROPI_RWPI";
96  break;
97  }
98  } else
99  OS << "unspecified (will use target default)";
100 
101  OS << "\n"
102  << Indent << " Code Model = ";
103 
104  if (JTMB.CM) {
105  switch (*JTMB.CM) {
106  case CodeModel::Tiny:
107  OS << "Tiny";
108  break;
109  case CodeModel::Small:
110  OS << "Small";
111  break;
112  case CodeModel::Kernel:
113  OS << "Kernel";
114  break;
115  case CodeModel::Medium:
116  OS << "Medium";
117  break;
118  case CodeModel::Large:
119  OS << "Large";
120  break;
121  }
122  } else
123  OS << "unspecified (will use target default)";
124 
125  OS << "\n"
126  << Indent << " Optimization Level = ";
127  switch (JTMB.OptLevel) {
128  case CodeGenOpt::None:
129  OS << "None";
130  break;
131  case CodeGenOpt::Less:
132  OS << "Less";
133  break;
134  case CodeGenOpt::Default:
135  OS << "Default";
136  break;
138  OS << "Aggressive";
139  break;
140  }
141 
142  OS << "\n" << Indent << "}\n";
143 }
144 #endif // NDEBUG
145 
146 } // End namespace orc.
147 } // End namespace llvm.
llvm::orc::JITTargetMachineBuilder
A utility class for building TargetMachines for JITs.
Definition: JITTargetMachineBuilder.h:34
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:17
llvm::orc::JITTargetMachineBuilder::createTargetMachine
Expected< std::unique_ptr< TargetMachine > > createTargetMachine()
Create a TargetMachine.
Definition: JITTargetMachineBuilder.cpp:44
llvm::FeatureMap
const std::array< TensorSpec, NumberOfFeatures > FeatureMap
Definition: MLInlineAdvisor.cpp:62
llvm::SubtargetFeatures::AddFeature
void AddFeature(StringRef String, bool Enable=true)
Adds Features.
Definition: SubtargetFeature.cpp:37
llvm::CodeModel::Medium
@ Medium
Definition: CodeGen.h:28
Host.h
llvm::Triple
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
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:41
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:55
llvm::Triple::str
const std::string & str() const
Definition: Triple.h:404
llvm::TargetOptions::UseInitArray
unsigned UseInitArray
UseInitArray - Use .init_array instead of .ctors for static constructors.
Definition: TargetOptions.h:248
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:54
llvm::TargetOptions::EmulatedTLS
unsigned EmulatedTLS
EmulatedTLS - This flag enables emulated TLS model, using emutls function in the runtime library.
Definition: TargetOptions.h:291
llvm::orc::JITTargetMachineBuilder::detectHost
static Expected< JITTargetMachineBuilder > detectHost()
Create a JITTargetMachineBuilder for the host system.
Definition: JITTargetMachineBuilder.cpp:25
llvm::Triple::getTriple
const std::string & getTriple() const
Definition: Triple.h:406
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:50
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:1675
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:851
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:1805
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:61
llvm::sys::getHostCPUName
StringRef getHostCPUName()
getHostCPUName - Get the LLVM name for the host CPU.
Definition: Host.cpp:1417
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:294
JITTargetMachineBuilder.h
llvm::orc::JITTargetMachineBuilderPrinter::print
void print(raw_ostream &OS) const
Definition: JITTargetMachineBuilder.cpp:69