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/MC/TargetRegistry.h"
12 #include "llvm/Support/raw_ostream.h"
13 #include "llvm/TargetParser/Host.h"
18 JITTargetMachineBuilder::JITTargetMachineBuilder(Triple TT
)
20 Options
.EmulatedTLS
= true;
21 Options
.UseInitArray
= true;
24 Expected
<JITTargetMachineBuilder
> JITTargetMachineBuilder::detectHost() {
25 JITTargetMachineBuilder
TMBuilder((Triple(sys::getProcessTriple())));
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
30 llvm::StringMap
<bool> FeatureMap
;
31 llvm::sys::getHostCPUFeatures(FeatureMap
);
32 for (auto &Feature
: FeatureMap
)
33 TMBuilder
.getFeatures().AddFeature(Feature
.first(), Feature
.second
);
35 TMBuilder
.setCPU(std::string(llvm::sys::getHostCPUName()));
40 Expected
<std::unique_ptr
<TargetMachine
>>
41 JITTargetMachineBuilder::createTargetMachine() {
44 auto *TheTarget
= TargetRegistry::lookupTarget(TT
.getTriple(), ErrMsg
);
46 return make_error
<StringError
>(std::move(ErrMsg
), inconvertibleErrorCode());
48 if (!TheTarget
->hasJIT())
49 return make_error
<StringError
>("Target has no JIT support",
50 inconvertibleErrorCode());
53 TheTarget
->createTargetMachine(TT
.getTriple(), CPU
, Features
.getString(),
54 Options
, RM
, CM
, OptLevel
, /*JIT*/ true);
56 return make_error
<StringError
>("Could not allocate target machine",
57 inconvertibleErrorCode());
59 return std::unique_ptr
<TargetMachine
>(TM
);
62 JITTargetMachineBuilder
&JITTargetMachineBuilder::addFeatures(
63 const std::vector
<std::string
> &FeatureVec
) {
64 for (const auto &F
: FeatureVec
)
65 Features
.AddFeature(F
);
70 void JITTargetMachineBuilderPrinter::print(raw_ostream
&OS
) const {
72 << Indent
<< " Triple = \"" << JTMB
.TT
.str() << "\"\n"
73 << Indent
<< " CPU = \"" << JTMB
.CPU
<< "\"\n"
74 << Indent
<< " Features = \"" << JTMB
.Features
.getString() << "\"\n"
75 << Indent
<< " Options = <not-printable>\n"
76 << Indent
<< " Relocation Model = ";
86 case Reloc::DynamicNoPIC
:
95 case Reloc::ROPI_RWPI
:
100 OS
<< "unspecified (will use target default)";
103 << Indent
<< " Code Model = ";
107 case CodeModel::Tiny
:
110 case CodeModel::Small
:
113 case CodeModel::Kernel
:
116 case CodeModel::Medium
:
119 case CodeModel::Large
:
124 OS
<< "unspecified (will use target default)";
127 << Indent
<< " Optimization Level = ";
128 switch (JTMB
.OptLevel
) {
129 case CodeGenOptLevel::None
:
132 case CodeGenOptLevel::Less
:
135 case CodeGenOptLevel::Default
:
138 case CodeGenOptLevel::Aggressive
:
143 OS
<< "\n" << Indent
<< "}\n";
147 } // End namespace orc.
148 } // End namespace llvm.