| 1 | //===-- TargetSelect.cpp - Target Chooser Code ----------------------------===// |
| 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 | // This just asks the TargetRegistry for the appropriate target to use, and |
| 10 | // allows the user to specify a specific one on the commandline with -march=x, |
| 11 | // -mcpu=y, and -mattr=a,-b,+c. Clients should initialize targets prior to |
| 12 | // calling selectTarget(). |
| 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
| 16 | #include "llvm/ExecutionEngine/ExecutionEngine.h" |
| 17 | #include "llvm/IR/Module.h" |
| 18 | #include "llvm/MC/TargetRegistry.h" |
| 19 | #include "llvm/Target/TargetMachine.h" |
| 20 | #include "llvm/TargetParser/Host.h" |
| 21 | #include "llvm/TargetParser/SubtargetFeature.h" |
| 22 | #include "llvm/TargetParser/Triple.h" |
| 23 | |
| 24 | using namespace llvm; |
| 25 | |
| 26 | TargetMachine *EngineBuilder::selectTarget() { |
| 27 | Triple TT; |
| 28 | |
| 29 | // MCJIT can generate code for remote targets, but the old JIT and Interpreter |
| 30 | // must use the host architecture. |
| 31 | if (WhichEngine != EngineKind::Interpreter && M) |
| 32 | TT = M->getTargetTriple(); |
| 33 | |
| 34 | return selectTarget(TargetTriple: TT, MArch, MCPU, MAttrs); |
| 35 | } |
| 36 | |
| 37 | /// selectTarget - Pick a target either via -march or by guessing the native |
| 38 | /// arch. Add any CPU features specified via -mcpu or -mattr. |
| 39 | TargetMachine *EngineBuilder::selectTarget(const Triple &TargetTriple, |
| 40 | StringRef MArch, |
| 41 | StringRef MCPU, |
| 42 | const SmallVectorImpl<std::string>& MAttrs) { |
| 43 | Triple TheTriple(TargetTriple); |
| 44 | if (TheTriple.getTriple().empty()) |
| 45 | TheTriple.setTriple(sys::getProcessTriple()); |
| 46 | |
| 47 | // Adjust the triple to match what the user requested. |
| 48 | const Target *TheTarget = nullptr; |
| 49 | if (!MArch.empty()) { |
| 50 | auto I = find_if(Range: TargetRegistry::targets(), |
| 51 | P: [&](const Target &T) { return MArch == T.getName(); }); |
| 52 | |
| 53 | if (I == TargetRegistry::targets().end()) { |
| 54 | if (ErrorStr) |
| 55 | *ErrorStr = "No available targets are compatible with this -march, " |
| 56 | "see -version for the available targets.\n" ; |
| 57 | return nullptr; |
| 58 | } |
| 59 | |
| 60 | TheTarget = &*I; |
| 61 | |
| 62 | // Adjust the triple to match (if known), otherwise stick with the |
| 63 | // requested/host triple. |
| 64 | Triple::ArchType Type = Triple::getArchTypeForLLVMName(Str: MArch); |
| 65 | if (Type != Triple::UnknownArch) |
| 66 | TheTriple.setArch(Kind: Type); |
| 67 | } else { |
| 68 | std::string Error; |
| 69 | TheTarget = TargetRegistry::lookupTarget(TheTriple, Error); |
| 70 | if (!TheTarget) { |
| 71 | if (ErrorStr) |
| 72 | *ErrorStr = Error; |
| 73 | return nullptr; |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | // Package up features to be passed to target/subtarget |
| 78 | std::string FeaturesStr; |
| 79 | if (!MAttrs.empty()) { |
| 80 | SubtargetFeatures Features; |
| 81 | for (unsigned i = 0; i != MAttrs.size(); ++i) |
| 82 | Features.AddFeature(String: MAttrs[i]); |
| 83 | FeaturesStr = Features.getString(); |
| 84 | } |
| 85 | |
| 86 | // Allocate a target... |
| 87 | TargetMachine *Target = TheTarget->createTargetMachine( |
| 88 | TT: TheTriple, CPU: MCPU, Features: FeaturesStr, Options, RM: RelocModel, CM: CMModel, OL: OptLevel, |
| 89 | /*JIT=*/true); |
| 90 | Target->Options.EmulatedTLS = EmulatedTLS; |
| 91 | |
| 92 | assert(Target && "Could not allocate target machine!" ); |
| 93 | return Target; |
| 94 | } |
| 95 | |