LLVM 24.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"
20#include <cassert>
21#include <vector>
22using namespace llvm;
23
24// Clients are responsible for avoid race conditions in registration.
25static Target *FirstTarget = nullptr;
26
28 if (Features.empty())
29 return true;
30
31 // Each feature starts with '+'/'-' followed by at least one non-comma
32 // character. Features are comma-separated with an optional trailing comma.
33 for (size_t I = 0, E = Features.size(); I != E; ++I) {
34 if (I == 0 || Features[I - 1] == ',') {
35 // At the start of a feature: must have a sign and a non-empty name.
36 if ((Features[I] != '+' && Features[I] != '-') ||
37 I + 1 == Features.size() || Features[I + 1] == ',')
38 return false;
39 }
40 }
41 return true;
42}
43
45 const Triple &T, MCContext &Ctx, std::unique_ptr<MCAsmBackend> TAB,
46 std::unique_ptr<MCObjectWriter> OW, std::unique_ptr<MCCodeEmitter> Emitter,
47 const MCSubtargetInfo &STI) const {
48 MCStreamer *S = nullptr;
49 switch (T.getObjectFormat()) {
51 llvm_unreachable("Unknown object format");
52 case Triple::COFF:
53 assert(T.isOSWindowsOrUEFI() && "only Windows and UEFI COFF are supported");
54 S = COFFStreamerCtorFn(Ctx, std::move(TAB), std::move(OW),
55 std::move(Emitter));
56 break;
57 case Triple::MachO:
58 if (MachOStreamerCtorFn)
59 S = MachOStreamerCtorFn(Ctx, std::move(TAB), std::move(OW),
60 std::move(Emitter));
61 else
62 S = createMachOStreamer(Ctx, std::move(TAB), std::move(OW),
63 std::move(Emitter), false);
64 break;
65 case Triple::ELF:
66 if (ELFStreamerCtorFn)
67 S = ELFStreamerCtorFn(T, Ctx, std::move(TAB), std::move(OW),
68 std::move(Emitter));
69 else
70 S = createELFStreamer(Ctx, std::move(TAB), std::move(OW),
71 std::move(Emitter));
72 break;
73 case Triple::Wasm:
74 S = createWasmStreamer(Ctx, std::move(TAB), std::move(OW),
75 std::move(Emitter));
76 break;
77 case Triple::GOFF:
78 S = createGOFFStreamer(Ctx, std::move(TAB), std::move(OW),
79 std::move(Emitter));
80 break;
81 case Triple::XCOFF:
82 S = XCOFFStreamerCtorFn(T, Ctx, std::move(TAB), std::move(OW),
83 std::move(Emitter));
84 break;
85 case Triple::SPIRV:
86 S = createSPIRVStreamer(Ctx, std::move(TAB), std::move(OW),
87 std::move(Emitter));
88 break;
90 S = createDXContainerStreamer(Ctx, std::move(TAB), std::move(OW),
91 std::move(Emitter));
92 break;
93 }
94 if (ObjectTargetStreamerCtorFn)
95 ObjectTargetStreamerCtorFn(*S, STI);
96 if (T.isLFI())
97 initializeLFIMCStreamer(*S, Ctx, T);
98 return S;
99}
100
102 std::unique_ptr<formatted_raw_ostream> OS,
103 std::unique_ptr<MCInstPrinter> IP,
104 std::unique_ptr<MCCodeEmitter> CE,
105 std::unique_ptr<MCAsmBackend> TAB) const {
106 MCInstPrinter *Printer = IP.get();
107 formatted_raw_ostream &OSRef = *OS;
108 MCStreamer *S;
109 if (AsmStreamerCtorFn)
110 S = AsmStreamerCtorFn(Ctx, std::move(OS), std::move(IP), std::move(CE),
111 std::move(TAB));
112 else
113 S = llvm::createAsmStreamer(Ctx, std::move(OS), std::move(IP),
114 std::move(CE), std::move(TAB));
115
117 return S;
118}
119
123
125 Triple &TheTriple,
126 std::string &Error) {
127 // Allocate target machine. First, check whether the user has explicitly
128 // specified an architecture to compile for. If so we have to look it up by
129 // name, because it might be a backend that has no mapping to a target triple.
130 const Target *TheTarget = nullptr;
131 if (!ArchName.empty()) {
132 auto I = find_if(targets(),
133 [&](const Target &T) { return ArchName == T.getName(); });
134
135 if (I == targets().end()) {
136 Error = ("invalid target '" + ArchName + "'.").str();
137 return nullptr;
138 }
139
140 TheTarget = &*I;
141
142 // Adjust the triple to match (if known), otherwise stick with the
143 // given triple.
146 TheTriple.setArch(Type);
147 } else {
148 // Get the target specific parser.
149 std::string TempError;
150 TheTarget = TargetRegistry::lookupTarget(TheTriple, TempError);
151 if (!TheTarget) {
152 Error = "unable to get target for '" + TheTriple.getTriple() +
153 "', see --version and --triple.";
154 return nullptr;
155 }
156 }
157
158 return TheTarget;
159}
160
162 std::string &Error) {
163 // Provide special warning when no targets are initialized.
164 if (targets().begin() == targets().end()) {
165 Error = "Unable to find target for this triple (no targets are registered)";
166 return nullptr;
167 }
168 Triple::ArchType Arch = TT.getArch();
169 auto ArchMatch = [&](const Target &T) { return T.ArchMatchFn(Arch); };
170 auto I = find_if(targets(), ArchMatch);
171
172 if (I == targets().end()) {
173 Error =
174 "No available targets are compatible with triple \"" + TT.str() + "\"";
175 return nullptr;
176 }
177
178 auto J = std::find_if(std::next(I), targets().end(), ArchMatch);
179 if (J != targets().end()) {
180 Error = std::string("Cannot choose between targets \"") + I->Name +
181 "\" and \"" + J->Name + "\"";
182 return nullptr;
183 }
184
185 return &*I;
186}
187
189 const char *ShortDesc,
190 const char *BackendName,
191 Target::ArchMatchFnTy ArchMatchFn,
192 bool HasJIT) {
193 assert(Name && ShortDesc && ArchMatchFn &&
194 "Missing required target information!");
195
196 // Check if this target has already been initialized, we allow this as a
197 // convenience to some clients.
198 if (T.Name)
199 return;
200
201 // Add to the list of targets.
202 T.Next = FirstTarget;
203 FirstTarget = &T;
204
205 T.Name = Name;
206 T.ShortDesc = ShortDesc;
207 T.BackendName = BackendName;
208 T.ArchMatchFn = ArchMatchFn;
209 T.HasJIT = HasJIT;
210}
211
212static int TargetArraySortFn(const std::pair<StringRef, const Target *> *LHS,
213 const std::pair<StringRef, const Target *> *RHS) {
214 return LHS->first.compare(RHS->first);
215}
216
218 std::vector<std::pair<StringRef, const Target*> > Targets;
219 size_t Width = 0;
220 for (const auto &T : TargetRegistry::targets()) {
221 Targets.push_back(std::make_pair(T.getName(), &T));
222 Width = std::max(Width, Targets.back().first.size());
223 }
224 array_pod_sort(Targets.begin(), Targets.end(), TargetArraySortFn);
225
226 OS << "\n";
227 OS << " Registered Targets:\n";
228 for (const auto &Target : Targets) {
229 OS << " " << Target.first;
230 OS.indent(Width - Target.first.size())
231 << " - " << Target.second->getShortDescription() << '\n';
232 }
233 if (Targets.empty())
234 OS << " (none)\n";
235}
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.
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
constexpr bool empty() const
Check if the string is empty.
Definition StringRef.h:141
constexpr size_t size() const
Get the string size.
Definition StringRef.h:144
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 LLVM_ABI 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:48
@ UnknownArch
Definition Triple.h:51
static LLVM_ABI ArchType getArchTypeForLLVMName(StringRef Str)
The canonical type for the given LLVM architecture name (e.g., "x86").
Definition Triple.cpp:455
const std::string & getTriple() const
Definition Triple.h:580
@ UnknownObjectFormat
Definition Triple.h:420
LLVM_ABI void setArch(ArchType Kind, SubArchType SubArch=NoSubArch)
Set the architecture (first) component of the triple to a known type.
Definition Triple.cpp:1675
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:37
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 LLVM_ABI const Target * lookupTarget(const Triple &TheTriple, 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()