LLVM 23.0.0git
TargetRegistry.cpp
Go to the documentation of this file.
1//===--- TargetRegistry.cpp - Target registration -------------------------===//
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
10#include "llvm/ADT/STLExtras.h"
11#include "llvm/ADT/StringRef.h"
14#include "llvm/MC/MCContext.h"
16#include "llvm/MC/MCLFI.h"
19#include "llvm/Support/Regex.h"
21#include <cassert>
22#include <vector>
23using namespace llvm;
24
25// Clients are responsible for avoid race conditions in registration.
26static Target *FirstTarget = nullptr;
27
29 if (Features.empty())
30 return true;
31
32 static const llvm::Regex pattern("^([+-][^,]+)(,[+-][^,]+)*,?$");
33 return pattern.match(Features);
34}
35
37 const Triple &T, MCContext &Ctx, std::unique_ptr<MCAsmBackend> TAB,
38 std::unique_ptr<MCObjectWriter> OW, std::unique_ptr<MCCodeEmitter> Emitter,
39 const MCSubtargetInfo &STI) const {
40 MCStreamer *S = nullptr;
41 switch (T.getObjectFormat()) {
43 llvm_unreachable("Unknown object format");
44 case Triple::COFF:
45 assert((T.isOSWindows() || T.isUEFI()) &&
46 "only Windows and UEFI COFF are supported");
47 S = COFFStreamerCtorFn(Ctx, std::move(TAB), std::move(OW),
48 std::move(Emitter));
49 break;
50 case Triple::MachO:
51 if (MachOStreamerCtorFn)
52 S = MachOStreamerCtorFn(Ctx, std::move(TAB), std::move(OW),
53 std::move(Emitter));
54 else
55 S = createMachOStreamer(Ctx, std::move(TAB), std::move(OW),
56 std::move(Emitter), false);
57 break;
58 case Triple::ELF:
59 if (ELFStreamerCtorFn)
60 S = ELFStreamerCtorFn(T, Ctx, std::move(TAB), std::move(OW),
61 std::move(Emitter));
62 else
63 S = createELFStreamer(Ctx, std::move(TAB), std::move(OW),
64 std::move(Emitter));
65 break;
66 case Triple::Wasm:
67 S = createWasmStreamer(Ctx, std::move(TAB), std::move(OW),
68 std::move(Emitter));
69 break;
70 case Triple::GOFF:
71 S = createGOFFStreamer(Ctx, std::move(TAB), std::move(OW),
72 std::move(Emitter));
73 break;
74 case Triple::XCOFF:
75 S = XCOFFStreamerCtorFn(T, Ctx, std::move(TAB), std::move(OW),
76 std::move(Emitter));
77 break;
78 case Triple::SPIRV:
79 S = createSPIRVStreamer(Ctx, std::move(TAB), std::move(OW),
80 std::move(Emitter));
81 break;
83 S = createDXContainerStreamer(Ctx, std::move(TAB), std::move(OW),
84 std::move(Emitter));
85 break;
86 }
87 if (ObjectTargetStreamerCtorFn)
88 ObjectTargetStreamerCtorFn(*S, STI);
89 if (T.isLFI())
90 initializeLFIMCStreamer(*S, Ctx, T);
91 return S;
92}
93
95 std::unique_ptr<formatted_raw_ostream> OS,
96 std::unique_ptr<MCInstPrinter> IP,
97 std::unique_ptr<MCCodeEmitter> CE,
98 std::unique_ptr<MCAsmBackend> TAB) const {
99 MCInstPrinter *Printer = IP.get();
100 formatted_raw_ostream &OSRef = *OS;
101 MCStreamer *S;
102 if (AsmStreamerCtorFn)
103 S = AsmStreamerCtorFn(Ctx, std::move(OS), std::move(IP), std::move(CE),
104 std::move(TAB));
105 else
106 S = llvm::createAsmStreamer(Ctx, std::move(OS), std::move(IP),
107 std::move(CE), std::move(TAB));
108
110 return S;
111}
112
116
118 Triple &TheTriple,
119 std::string &Error) {
120 // Allocate target machine. First, check whether the user has explicitly
121 // specified an architecture to compile for. If so we have to look it up by
122 // name, because it might be a backend that has no mapping to a target triple.
123 const Target *TheTarget = nullptr;
124 if (!ArchName.empty()) {
125 auto I = find_if(targets(),
126 [&](const Target &T) { return ArchName == T.getName(); });
127
128 if (I == targets().end()) {
129 Error = ("invalid target '" + ArchName + "'.").str();
130 return nullptr;
131 }
132
133 TheTarget = &*I;
134
135 // Adjust the triple to match (if known), otherwise stick with the
136 // given triple.
139 TheTriple.setArch(Type);
140 } else {
141 // Get the target specific parser.
142 std::string TempError;
143 TheTarget = TargetRegistry::lookupTarget(TheTriple, TempError);
144 if (!TheTarget) {
145 Error = "unable to get target for '" + TheTriple.getTriple() +
146 "', see --version and --triple.";
147 return nullptr;
148 }
149 }
150
151 return TheTarget;
152}
153
155 std::string &Error) {
156 // Provide special warning when no targets are initialized.
157 if (targets().begin() == targets().end()) {
158 Error = "Unable to find target for this triple (no targets are registered)";
159 return nullptr;
160 }
161 Triple::ArchType Arch = TT.getArch();
162 auto ArchMatch = [&](const Target &T) { return T.ArchMatchFn(Arch); };
163 auto I = find_if(targets(), ArchMatch);
164
165 if (I == targets().end()) {
166 Error =
167 "No available targets are compatible with triple \"" + TT.str() + "\"";
168 return nullptr;
169 }
170
171 auto J = std::find_if(std::next(I), targets().end(), ArchMatch);
172 if (J != targets().end()) {
173 Error = std::string("Cannot choose between targets \"") + I->Name +
174 "\" and \"" + J->Name + "\"";
175 return nullptr;
176 }
177
178 return &*I;
179}
180
182 const char *ShortDesc,
183 const char *BackendName,
184 Target::ArchMatchFnTy ArchMatchFn,
185 bool HasJIT) {
186 assert(Name && ShortDesc && ArchMatchFn &&
187 "Missing required target information!");
188
189 // Check if this target has already been initialized, we allow this as a
190 // convenience to some clients.
191 if (T.Name)
192 return;
193
194 // Add to the list of targets.
195 T.Next = FirstTarget;
196 FirstTarget = &T;
197
198 T.Name = Name;
199 T.ShortDesc = ShortDesc;
200 T.BackendName = BackendName;
201 T.ArchMatchFn = ArchMatchFn;
202 T.HasJIT = HasJIT;
203}
204
205static int TargetArraySortFn(const std::pair<StringRef, const Target *> *LHS,
206 const std::pair<StringRef, const Target *> *RHS) {
207 return LHS->first.compare(RHS->first);
208}
209
211 std::vector<std::pair<StringRef, const Target*> > Targets;
212 size_t Width = 0;
213 for (const auto &T : TargetRegistry::targets()) {
214 Targets.push_back(std::make_pair(T.getName(), &T));
215 Width = std::max(Width, Targets.back().first.size());
216 }
217 array_pod_sort(Targets.begin(), Targets.end(), TargetArraySortFn);
218
219 OS << "\n";
220 OS << " Registered Targets:\n";
221 for (const auto &Target : Targets) {
222 OS << " " << Target.first;
223 OS.indent(Width - Target.first.size())
224 << " - " << Target.second->getShortDescription() << '\n';
225 }
226 if (Targets.empty())
227 OS << " (none)\n";
228}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
amdgpu next use AMDGPU Next Use Analysis Printer
dxil DXContainer Global Emitter
LFI-specific code for MC.
#define I(x, y, z)
Definition MD5.cpp:57
#define T
This file contains some templates that are useful if you are working with the STL at all.
static Target * FirstTarget
static int TargetArraySortFn(const std::pair< StringRef, const Target * > *LHS, const std::pair< StringRef, const Target * > *RHS)
Value * RHS
Value * LHS
Lightweight error class with error context and mandatory checking.
Definition Error.h:159
Context object for machine code objects.
Definition MCContext.h:83
This is an instance of a target assembly language printer that converts an MCInst to valid target ass...
Streaming machine code generation interface.
Definition MCStreamer.h:222
Generic base class for all target subtargets.
LLVM_ABI bool match(StringRef String, SmallVectorImpl< StringRef > *Matches=nullptr, std::string *Error=nullptr) const
matches - Match the regex against a given String.
Definition Regex.cpp:83
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
constexpr bool empty() const
empty - Check if the string is empty.
Definition StringRef.h:140
Target - Wrapper for Target specific information.
LLVM_ABI MCStreamer * createAsmStreamer(MCContext &Ctx, std::unique_ptr< formatted_raw_ostream > OS, std::unique_ptr< MCInstPrinter > IP, std::unique_ptr< MCCodeEmitter > CE, std::unique_ptr< MCAsmBackend > TAB) const
MCTargetStreamer * createAsmTargetStreamer(MCStreamer &S, formatted_raw_ostream &OS, MCInstPrinter *InstPrint) const
LLVM_ABI MCStreamer * createMCObjectStreamer(const Triple &T, MCContext &Ctx, std::unique_ptr< MCAsmBackend > TAB, std::unique_ptr< MCObjectWriter > OW, std::unique_ptr< MCCodeEmitter > Emitter, const MCSubtargetInfo &STI) const
Create a target specific MCStreamer.
bool(*)(Triple::ArchType Arch) ArchMatchFnTy
const char * getShortDescription() const
getShortDescription - Get a short description of the target.
static bool isValidFeatureListFormat(StringRef FeaturesString)
isValidFeatureListFormat - check that FeatureString has the format: "+attr1,+attr2,...
Triple - Helper class for working with autoconf configuration names.
Definition Triple.h:47
@ UnknownArch
Definition Triple.h:50
static LLVM_ABI ArchType getArchTypeForLLVMName(StringRef Str)
The canonical type for the given LLVM architecture name (e.g., "x86").
Definition Triple.cpp:657
const std::string & getTriple() const
Definition Triple.h:496
@ UnknownObjectFormat
Definition Triple.h:331
LLVM_ABI void setArch(ArchType Kind, SubArchType SubArch=NoSubArch)
Set the architecture (first) component of the triple to a known type.
Definition Triple.cpp:1882
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:46
formatted_raw_ostream - A raw_ostream that wraps another one and keeps track of line and column posit...
A range adaptor for a pair of iterators.
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
raw_ostream & indent(unsigned NumSpaces)
indent - Insert 'NumSpaces' spaces.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
This is an optimization pass for GlobalISel generic memory operations.
LLVM_ABI MCStreamer * createELFStreamer(MCContext &Ctx, std::unique_ptr< MCAsmBackend > &&TAB, std::unique_ptr< MCObjectWriter > &&OW, std::unique_ptr< MCCodeEmitter > &&CE)
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
LLVM_ABI void initializeLFIMCStreamer(MCStreamer &Streamer, MCContext &Ctx, const Triple &TheTriple)
Definition MCLFI.cpp:35
LLVM_ABI MCStreamer * createAsmStreamer(MCContext &Ctx, std::unique_ptr< formatted_raw_ostream > OS, std::unique_ptr< MCInstPrinter > InstPrint, std::unique_ptr< MCCodeEmitter > CE, std::unique_ptr< MCAsmBackend > TAB)
Create a machine code streamer which will print out assembly for the native target,...
LLVM_ABI MCStreamer * createMachOStreamer(MCContext &Ctx, std::unique_ptr< MCAsmBackend > &&TAB, std::unique_ptr< MCObjectWriter > &&OW, std::unique_ptr< MCCodeEmitter > &&CE, bool DWARFMustBeAtTheEnd, bool LabelSections=false)
LLVM_ABI MCStreamer * createDXContainerStreamer(MCContext &Ctx, std::unique_ptr< MCAsmBackend > &&TAB, std::unique_ptr< MCObjectWriter > &&OW, std::unique_ptr< MCCodeEmitter > &&CE)
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:1772
LLVM_ABI MCStreamer * createWasmStreamer(MCContext &Ctx, std::unique_ptr< MCAsmBackend > &&TAB, std::unique_ptr< MCObjectWriter > &&OW, std::unique_ptr< MCCodeEmitter > &&CE)
LLVM_ABI MCStreamer * createGOFFStreamer(MCContext &Ctx, std::unique_ptr< MCAsmBackend > &&TAB, std::unique_ptr< MCObjectWriter > &&OW, std::unique_ptr< MCCodeEmitter > &&CE)
void array_pod_sort(IteratorTy Start, IteratorTy End)
array_pod_sort - This sorts an array with the specified start and end extent.
Definition STLExtras.h:1596
LLVM_ABI MCStreamer * createSPIRVStreamer(MCContext &Ctx, std::unique_ptr< MCAsmBackend > &&TAB, std::unique_ptr< MCObjectWriter > &&OW, std::unique_ptr< MCCodeEmitter > &&CE)
static const Target * lookupTarget(StringRef TripleStr, std::string &Error)
lookupTarget - Lookup a target based on a target triple.
static LLVM_ABI void printRegisteredTargetsForVersion(raw_ostream &OS)
printRegisteredTargetsForVersion - Print the registered targets appropriately for inclusion in a tool...
static LLVM_ABI void RegisterTarget(Target &T, const char *Name, const char *ShortDesc, const char *BackendName, Target::ArchMatchFnTy ArchMatchFn, bool HasJIT=false)
RegisterTarget - Register the given target.
static LLVM_ABI iterator_range< iterator > targets()