xref: /llvm-project/llvm/lib/ExecutionEngine/Orc/JITTargetMachineBuilder.cpp (revision 8bac17709e16d22aa51a1d4411a0241305dcdbff)
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/Support/TargetRegistry.h"
12 
13 namespace llvm {
14 namespace orc {
15 
16 JITTargetMachineBuilder::JITTargetMachineBuilder(Triple TT)
17     : TT(std::move(TT)) {
18   Options.EmulatedTLS = true;
19   Options.ExplicitEmulatedTLS = true;
20 }
21 
22 Expected<JITTargetMachineBuilder> JITTargetMachineBuilder::detectHost() {
23   // FIXME: getProcessTriple is bogus. It returns the host LLVM was compiled on,
24   //        rather than a valid triple for the current process.
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::SubtargetFeatures SubtargetFeatures;
31   llvm::StringMap<bool> FeatureMap;
32   llvm::sys::getHostCPUFeatures(FeatureMap);
33   for (auto &Feature : FeatureMap)
34     SubtargetFeatures.AddFeature(Feature.first(), Feature.second);
35 
36   TMBuilder.setCPU(llvm::sys::getHostCPUName());
37   TMBuilder.addFeatures(SubtargetFeatures.getFeatures());
38 
39   return TMBuilder;
40 }
41 
42 Expected<std::unique_ptr<TargetMachine>>
43 JITTargetMachineBuilder::createTargetMachine() {
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",
55                                    inconvertibleErrorCode());
56 
57   return std::unique_ptr<TargetMachine>(TM);
58 }
59 
60 JITTargetMachineBuilder &JITTargetMachineBuilder::addFeatures(
61     const std::vector<std::string> &FeatureVec) {
62   for (const auto &F : FeatureVec)
63     Features.AddFeature(F);
64   return *this;
65 }
66 
67 } // End namespace orc.
68 } // End namespace llvm.
69