Path: blob/main/contrib/llvm-project/llvm/lib/ExecutionEngine/TargetSelect.cpp
35232 views
//===-- TargetSelect.cpp - Target Chooser Code ----------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// This just asks the TargetRegistry for the appropriate target to use, and9// allows the user to specify a specific one on the commandline with -march=x,10// -mcpu=y, and -mattr=a,-b,+c. Clients should initialize targets prior to11// calling selectTarget().12//13//===----------------------------------------------------------------------===//1415#include "llvm/ExecutionEngine/ExecutionEngine.h"16#include "llvm/IR/Module.h"17#include "llvm/MC/TargetRegistry.h"18#include "llvm/Target/TargetMachine.h"19#include "llvm/TargetParser/Host.h"20#include "llvm/TargetParser/SubtargetFeature.h"21#include "llvm/TargetParser/Triple.h"2223using namespace llvm;2425TargetMachine *EngineBuilder::selectTarget() {26Triple TT;2728// MCJIT can generate code for remote targets, but the old JIT and Interpreter29// must use the host architecture.30if (WhichEngine != EngineKind::Interpreter && M)31TT.setTriple(M->getTargetTriple());3233return selectTarget(TT, MArch, MCPU, MAttrs);34}3536/// selectTarget - Pick a target either via -march or by guessing the native37/// arch. Add any CPU features specified via -mcpu or -mattr.38TargetMachine *EngineBuilder::selectTarget(const Triple &TargetTriple,39StringRef MArch,40StringRef MCPU,41const SmallVectorImpl<std::string>& MAttrs) {42Triple TheTriple(TargetTriple);43if (TheTriple.getTriple().empty())44TheTriple.setTriple(sys::getProcessTriple());4546// Adjust the triple to match what the user requested.47const Target *TheTarget = nullptr;48if (!MArch.empty()) {49auto I = find_if(TargetRegistry::targets(),50[&](const Target &T) { return MArch == T.getName(); });5152if (I == TargetRegistry::targets().end()) {53if (ErrorStr)54*ErrorStr = "No available targets are compatible with this -march, "55"see -version for the available targets.\n";56return nullptr;57}5859TheTarget = &*I;6061// Adjust the triple to match (if known), otherwise stick with the62// requested/host triple.63Triple::ArchType Type = Triple::getArchTypeForLLVMName(MArch);64if (Type != Triple::UnknownArch)65TheTriple.setArch(Type);66} else {67std::string Error;68TheTarget = TargetRegistry::lookupTarget(TheTriple.getTriple(), Error);69if (!TheTarget) {70if (ErrorStr)71*ErrorStr = Error;72return nullptr;73}74}7576// Package up features to be passed to target/subtarget77std::string FeaturesStr;78if (!MAttrs.empty()) {79SubtargetFeatures Features;80for (unsigned i = 0; i != MAttrs.size(); ++i)81Features.AddFeature(MAttrs[i]);82FeaturesStr = Features.getString();83}8485// Allocate a target...86TargetMachine *Target =87TheTarget->createTargetMachine(TheTriple.getTriple(), MCPU, FeaturesStr,88Options, RelocModel, CMModel, OptLevel,89/*JIT*/ true);90Target->Options.EmulatedTLS = EmulatedTLS;9192assert(Target && "Could not allocate target machine!");93return Target;94}959697