1 //===----- JITTargetMachineBuilder.cpp - Build TargetMachines for JIT -----===//
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
7 //===----------------------------------------------------------------------===//
9 #include "llvm/ExecutionEngine/Orc/JITTargetMachineBuilder.h"
11 #include "llvm/Support/Host.h"
12 #include "llvm/Support/TargetRegistry.h"
13 #include "llvm/Support/raw_ostream.h"
18 JITTargetMachineBuilder::JITTargetMachineBuilder(Triple TT
)
20 Options
.EmulatedTLS
= true;
21 Options
.ExplicitEmulatedTLS
= true;
24 Expected
<JITTargetMachineBuilder
> JITTargetMachineBuilder::detectHost() {
25 // FIXME: getProcessTriple is bogus. It returns the host LLVM was compiled on,
26 // rather than a valid triple for the current process.
27 JITTargetMachineBuilder
TMBuilder((Triple(sys::getProcessTriple())));
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
32 llvm::StringMap
<bool> FeatureMap
;
33 llvm::sys::getHostCPUFeatures(FeatureMap
);
34 for (auto &Feature
: FeatureMap
)
35 TMBuilder
.getFeatures().AddFeature(Feature
.first(), Feature
.second
);
37 TMBuilder
.setCPU(std::string(llvm::sys::getHostCPUName()));
42 Expected
<std::unique_ptr
<TargetMachine
>>
43 JITTargetMachineBuilder::createTargetMachine() {
46 auto *TheTarget
= TargetRegistry::lookupTarget(TT
.getTriple(), ErrMsg
);
48 return make_error
<StringError
>(std::move(ErrMsg
), inconvertibleErrorCode());
51 TheTarget
->createTargetMachine(TT
.getTriple(), CPU
, Features
.getString(),
52 Options
, RM
, CM
, OptLevel
, /*JIT*/ true);
54 return make_error
<StringError
>("Could not allocate target machine",
55 inconvertibleErrorCode());
57 return std::unique_ptr
<TargetMachine
>(TM
);
60 JITTargetMachineBuilder
&JITTargetMachineBuilder::addFeatures(
61 const std::vector
<std::string
> &FeatureVec
) {
62 for (const auto &F
: FeatureVec
)
63 Features
.AddFeature(F
);
68 void JITTargetMachineBuilderPrinter::print(raw_ostream
&OS
) const {
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 = ";
84 case Reloc::DynamicNoPIC
:
93 case Reloc::ROPI_RWPI
:
98 OS
<< "unspecified (will use target default)";
101 << Indent
<< " Code Model = ";
105 case CodeModel::Tiny
:
108 case CodeModel::Small
:
111 case CodeModel::Kernel
:
114 case CodeModel::Medium
:
117 case CodeModel::Large
:
122 OS
<< "unspecified (will use target default)";
125 << Indent
<< " Optimization Level = ";
126 switch (JTMB
.OptLevel
) {
127 case CodeGenOpt::None
:
130 case CodeGenOpt::Less
:
133 case CodeGenOpt::Default
:
136 case CodeGenOpt::Aggressive
:
141 OS
<< "\n" << Indent
<< "}\n";
145 } // End namespace orc.
146 } // End namespace llvm.