Lang Hames | d435ce4 | 2018-09-30 19:12:23 +0000 | [diff] [blame] | 1 | //===----- JITTargetMachineBuilder.cpp - Build TargetMachines for JIT -----===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
Lang Hames | d435ce4 | 2018-09-30 19:12:23 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "llvm/ExecutionEngine/Orc/JITTargetMachineBuilder.h" |
| 10 | |
Reid Kleckner | 90c64a3 | 2019-10-19 00:48:11 +0000 | [diff] [blame] | 11 | #include "llvm/Support/Host.h" |
Lang Hames | d435ce4 | 2018-09-30 19:12:23 +0000 | [diff] [blame] | 12 | #include "llvm/Support/TargetRegistry.h" |
| 13 | |
| 14 | namespace llvm { |
| 15 | namespace orc { |
| 16 | |
| 17 | JITTargetMachineBuilder::JITTargetMachineBuilder(Triple TT) |
| 18 | : TT(std::move(TT)) { |
| 19 | Options.EmulatedTLS = true; |
| 20 | Options.ExplicitEmulatedTLS = true; |
| 21 | } |
| 22 | |
| 23 | Expected<JITTargetMachineBuilder> JITTargetMachineBuilder::detectHost() { |
| 24 | // FIXME: getProcessTriple is bogus. It returns the host LLVM was compiled on, |
| 25 | // rather than a valid triple for the current process. |
Diego Caballero | 8bac177 | 2019-08-06 16:24:17 +0000 | [diff] [blame] | 26 | JITTargetMachineBuilder TMBuilder((Triple(sys::getProcessTriple()))); |
| 27 | |
| 28 | // Retrieve host CPU name and sub-target features and add them to builder. |
| 29 | // Relocation model, code model and codegen opt level are kept to default |
| 30 | // values. |
Diego Caballero | 8bac177 | 2019-08-06 16:24:17 +0000 | [diff] [blame] | 31 | llvm::StringMap<bool> FeatureMap; |
| 32 | llvm::sys::getHostCPUFeatures(FeatureMap); |
| 33 | for (auto &Feature : FeatureMap) |
Lang Hames | 5c05b4a | 2019-12-01 15:51:37 -0800 | [diff] [blame] | 34 | TMBuilder.getFeatures().AddFeature(Feature.first(), Feature.second); |
Diego Caballero | 8bac177 | 2019-08-06 16:24:17 +0000 | [diff] [blame] | 35 | |
Benjamin Kramer | adcd026 | 2020-01-28 20:23:46 +0100 | [diff] [blame^] | 36 | TMBuilder.setCPU(std::string(llvm::sys::getHostCPUName())); |
Diego Caballero | 8bac177 | 2019-08-06 16:24:17 +0000 | [diff] [blame] | 37 | |
| 38 | return TMBuilder; |
Lang Hames | d435ce4 | 2018-09-30 19:12:23 +0000 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | Expected<std::unique_ptr<TargetMachine>> |
| 42 | JITTargetMachineBuilder::createTargetMachine() { |
| 43 | |
| 44 | std::string ErrMsg; |
| 45 | auto *TheTarget = TargetRegistry::lookupTarget(TT.getTriple(), ErrMsg); |
| 46 | if (!TheTarget) |
| 47 | return make_error<StringError>(std::move(ErrMsg), inconvertibleErrorCode()); |
| 48 | |
| 49 | auto *TM = |
| 50 | TheTarget->createTargetMachine(TT.getTriple(), CPU, Features.getString(), |
| 51 | Options, RM, CM, OptLevel, /*JIT*/ true); |
| 52 | if (!TM) |
| 53 | return make_error<StringError>("Could not allocate target machine", |
| 54 | inconvertibleErrorCode()); |
| 55 | |
| 56 | return std::unique_ptr<TargetMachine>(TM); |
| 57 | } |
| 58 | |
| 59 | JITTargetMachineBuilder &JITTargetMachineBuilder::addFeatures( |
| 60 | const std::vector<std::string> &FeatureVec) { |
| 61 | for (const auto &F : FeatureVec) |
| 62 | Features.AddFeature(F); |
| 63 | return *this; |
| 64 | } |
| 65 | |
| 66 | } // End namespace orc. |
| 67 | } // End namespace llvm. |