brintos

brintos / llvm-project-archived public Read only

0
0
Text · 3.3 KiB · bce90fc Raw
95 lines · cpp
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-exception6//7//===----------------------------------------------------------------------===//8//9// This just asks the TargetRegistry for the appropriate target to use, and10// 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 to12// 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 24using namespace llvm;25 26TargetMachine *EngineBuilder::selectTarget() {27  Triple TT;28 29  // MCJIT can generate code for remote targets, but the old JIT and Interpreter30  // must use the host architecture.31  if (WhichEngine != EngineKind::Interpreter && M)32    TT = M->getTargetTriple();33 34  return selectTarget(TT, MArch, MCPU, MAttrs);35}36 37/// selectTarget - Pick a target either via -march or by guessing the native38/// arch.  Add any CPU features specified via -mcpu or -mattr.39TargetMachine *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(TargetRegistry::targets(),51                     [&](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 the63    // requested/host triple.64    Triple::ArchType Type = Triple::getArchTypeForLLVMName(MArch);65    if (Type != Triple::UnknownArch)66      TheTriple.setArch(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/subtarget78  std::string FeaturesStr;79  if (!MAttrs.empty()) {80    SubtargetFeatures Features;81    for (unsigned i = 0; i != MAttrs.size(); ++i)82      Features.AddFeature(MAttrs[i]);83    FeaturesStr = Features.getString();84  }85 86  // Allocate a target...87  TargetMachine *Target = TheTarget->createTargetMachine(88      TheTriple, MCPU, FeaturesStr, Options, RelocModel, CMModel, OptLevel,89      /*JIT=*/true);90  Target->Options.EmulatedTLS = EmulatedTLS;91 92  assert(Target && "Could not allocate target machine!");93  return Target;94}95