xref: /freebsd/contrib/llvm-project/llvm/lib/ExecutionEngine/Orc/JITTargetMachineBuilder.cpp (revision 95eb4b873b6a8b527c5bd78d7191975dfca38998)
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 
9 #include "llvm/ExecutionEngine/Orc/JITTargetMachineBuilder.h"
10 
11 #include "llvm/MC/TargetRegistry.h"
12 #include "llvm/Support/raw_ostream.h"
13 #include "llvm/TargetParser/Host.h"
14 
15 namespace llvm {
16 namespace orc {
17 
18 JITTargetMachineBuilder::JITTargetMachineBuilder(Triple TT)
19     : TT(std::move(TT)) {
20   Options.EmulatedTLS = true;
21   Options.UseInitArray = true;
22 }
23 
24 Expected<JITTargetMachineBuilder> JITTargetMachineBuilder::detectHost() {
25   JITTargetMachineBuilder TMBuilder((Triple(sys::getProcessTriple())));
26 
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
29   // values.
30   llvm::StringMap<bool> FeatureMap;
31   llvm::sys::getHostCPUFeatures(FeatureMap);
32   for (auto &Feature : FeatureMap)
33     TMBuilder.getFeatures().AddFeature(Feature.first(), Feature.second);
34 
35   TMBuilder.setCPU(std::string(llvm::sys::getHostCPUName()));
36 
37   return TMBuilder;
38 }
39 
40 Expected<std::unique_ptr<TargetMachine>>
41 JITTargetMachineBuilder::createTargetMachine() {
42 
43   std::string ErrMsg;
44   auto *TheTarget = TargetRegistry::lookupTarget(TT.getTriple(), ErrMsg);
45   if (!TheTarget)
46     return make_error<StringError>(std::move(ErrMsg), inconvertibleErrorCode());
47 
48   if (!TheTarget->hasJIT())
49     return make_error<StringError>("Target has no JIT support",
50                                    inconvertibleErrorCode());
51 
52   auto *TM =
53       TheTarget->createTargetMachine(TT.getTriple(), CPU, Features.getString(),
54                                      Options, RM, CM, OptLevel, /*JIT*/ true);
55   if (!TM)
56     return make_error<StringError>("Could not allocate target machine",
57                                    inconvertibleErrorCode());
58 
59   return std::unique_ptr<TargetMachine>(TM);
60 }
61 
62 JITTargetMachineBuilder &JITTargetMachineBuilder::addFeatures(
63     const std::vector<std::string> &FeatureVec) {
64   for (const auto &F : FeatureVec)
65     Features.AddFeature(F);
66   return *this;
67 }
68 
69 #ifndef NDEBUG
70 void JITTargetMachineBuilderPrinter::print(raw_ostream &OS) const {
71   OS << Indent << "{\n"
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 = ";
77 
78   if (JTMB.RM) {
79     switch (*JTMB.RM) {
80     case Reloc::Static:
81       OS << "Static";
82       break;
83     case Reloc::PIC_:
84       OS << "PIC_";
85       break;
86     case Reloc::DynamicNoPIC:
87       OS << "DynamicNoPIC";
88       break;
89     case Reloc::ROPI:
90       OS << "ROPI";
91       break;
92     case Reloc::RWPI:
93       OS << "RWPI";
94       break;
95     case Reloc::ROPI_RWPI:
96       OS << "ROPI_RWPI";
97       break;
98     }
99   } else
100     OS << "unspecified (will use target default)";
101 
102   OS << "\n"
103      << Indent << "  Code Model = ";
104 
105   if (JTMB.CM) {
106     switch (*JTMB.CM) {
107     case CodeModel::Tiny:
108       OS << "Tiny";
109       break;
110     case CodeModel::Small:
111       OS << "Small";
112       break;
113     case CodeModel::Kernel:
114       OS << "Kernel";
115       break;
116     case CodeModel::Medium:
117       OS << "Medium";
118       break;
119     case CodeModel::Large:
120       OS << "Large";
121       break;
122     }
123   } else
124     OS << "unspecified (will use target default)";
125 
126   OS << "\n"
127      << Indent << "  Optimization Level = ";
128   switch (JTMB.OptLevel) {
129   case CodeGenOptLevel::None:
130     OS << "None";
131     break;
132   case CodeGenOptLevel::Less:
133     OS << "Less";
134     break;
135   case CodeGenOptLevel::Default:
136     OS << "Default";
137     break;
138   case CodeGenOptLevel::Aggressive:
139     OS << "Aggressive";
140     break;
141   }
142 
143   OS << "\n" << Indent << "}\n";
144 }
145 #endif // NDEBUG
146 
147 } // End namespace orc.
148 } // End namespace llvm.
149