LLVM 19.0.0git
Go to the documentation of this file.
1//===-- TargetSelect.cpp - Target Chooser Code ----------------------------===//
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
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().
17#include "llvm/IR/Module.h"
24using namespace llvm;
27 Triple TT;
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.setTriple(M->getTargetTriple());
34 return selectTarget(TT, MArch, MCPU, MAttrs);
37/// selectTarget - Pick a target either via -march or by guessing the native
38/// arch. Add any CPU features specified via -mcpu or -mattr.
40 StringRef MArch,
41 StringRef MCPU,
42 const SmallVectorImpl<std::string>& MAttrs) {
43 Triple TheTriple(TargetTriple);
44 if (TheTriple.getTriple().empty())
47 // Adjust the triple to match what the user requested.
48 const Target *TheTarget = nullptr;
49 if (!MArch.empty()) {
51 [&](const Target &T) { return MArch == T.getName(); });
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 }
60 TheTarget = &*I;
62 // Adjust the triple to match (if known), otherwise stick with the
63 // requested/host triple.
66 TheTriple.setArch(Type);
67 } else {
68 std::string Error;
69 TheTarget = TargetRegistry::lookupTarget(TheTriple.getTriple(), Error);
70 if (!TheTarget) {
71 if (ErrorStr)
72 *ErrorStr = Error;
73 return nullptr;
74 }
75 }
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(MAttrs[i]);
83 FeaturesStr = Features.getString();
84 }
86 // Allocate a target...
88 TheTarget->createTargetMachine(TheTriple.getTriple(), MCPU, FeaturesStr,
89 Options, RelocModel, CMModel, OptLevel,
90 /*JIT*/ true);
91 Target->Options.EmulatedTLS = EmulatedTLS;
93 assert(Target && "Could not allocate target machine!");
94 return Target;
#define I(x, y, z)
Definition: MD5.cpp:58
Module.h This file contains the declarations for the Module class.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
TargetMachine * selectTarget()
Lightweight error class with error context and mandatory checking.
Definition: Error.h:160
bool empty() const
Definition: SmallVector.h:94
size_t size() const
Definition: SmallVector.h:91
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: SmallVector.h:586
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
constexpr bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:134
Manages the enabling and disabling of subtarget specific features.
std::string getString() const
Returns features as a string.
void AddFeature(StringRef String, bool Enable=true)
Adds Features.
Primary interface to the complete machine description for the target machine.
Definition: TargetMachine.h:76
Target - Wrapper for Target specific information.
TargetMachine * createTargetMachine(StringRef TT, StringRef CPU, StringRef Features, const TargetOptions &Options, std::optional< Reloc::Model > RM, std::optional< CodeModel::Model > CM=std::nullopt, CodeGenOptLevel OL=CodeGenOptLevel::Default, bool JIT=false) const
createTargetMachine - Create a target specific machine implementation for the specified Triple.
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
@ UnknownArch
Definition: Triple.h:47
void setTriple(const Twine &Str)
Set all components to the new triple Str.
Definition: Triple.cpp:1417
static ArchType getArchTypeForLLVMName(StringRef Str)
The canonical type for the given LLVM architecture name (e.g., "x86").
Definition: Triple.cpp:365
const std::string & getTriple() const
Definition: Triple.h:437
void setArch(ArchType Kind, SubArchType SubArch=NoSubArch)
Set the architecture (first) component of the triple to a known type.
Definition: Triple.cpp:1421
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
std::string getProcessTriple()
getProcessTriple() - Return an appropriate target triple for generating code to be loaded into the cu...
Definition: Host.cpp:2037
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
auto find_if(R &&Range, UnaryPredicate P)
Provide wrappers to std::find_if which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1749
static const Target * lookupTarget(StringRef Triple, std::string &Error)
lookupTarget - Lookup a target based on a target triple.
static iterator_range< iterator > targets()