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"
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 const Triple &T, MCContext &Ctx, std::unique_ptr<MCAsmBackend> TAB,
29 std::unique_ptr<MCObjectWriter> OW, std::unique_ptr<MCCodeEmitter> Emitter,
30 const MCSubtargetInfo &STI) const {
31 MCStreamer *S = nullptr;
32 switch (T.getObjectFormat()) {
34 llvm_unreachable("Unknown object format");
35 case Triple::COFF:
36 assert((T.isOSWindows() || T.isUEFI()) &&
37 "only Windows and UEFI COFF are supported");
38 S = COFFStreamerCtorFn(Ctx, std::move(TAB), std::move(OW),
39 std::move(Emitter));
40 break;
41 case Triple::MachO:
42 if (MachOStreamerCtorFn)
43 S = MachOStreamerCtorFn(Ctx, std::move(TAB), std::move(OW),
44 std::move(Emitter));
45 else
46 S = createMachOStreamer(Ctx, std::move(TAB), std::move(OW),
47 std::move(Emitter), false);
48 break;
49 case Triple::ELF:
50 if (ELFStreamerCtorFn)
51 S = ELFStreamerCtorFn(T, Ctx, std::move(TAB), std::move(OW),
52 std::move(Emitter));
53 else
54 S = createELFStreamer(Ctx, std::move(TAB), std::move(OW),
55 std::move(Emitter));
56 break;
57 case Triple::Wasm:
58 S = createWasmStreamer(Ctx, std::move(TAB), std::move(OW),
59 std::move(Emitter));
60 break;
61 case Triple::GOFF:
62 S = createGOFFStreamer(Ctx, std::move(TAB), std::move(OW),
63 std::move(Emitter));
64 break;
65 case Triple::XCOFF:
66 S = XCOFFStreamerCtorFn(T, Ctx, std::move(TAB), std::move(OW),
67 std::move(Emitter));
68 break;
69 case Triple::SPIRV:
70 S = createSPIRVStreamer(Ctx, std::move(TAB), std::move(OW),
71 std::move(Emitter));
72 break;
74 S = createDXContainerStreamer(Ctx, std::move(TAB), std::move(OW),
75 std::move(Emitter));
76 break;
77 }
78 if (ObjectTargetStreamerCtorFn)
79 ObjectTargetStreamerCtorFn(*S, STI);
80 if (T.isLFI())
81 initializeLFIMCStreamer(*S, Ctx, T);
82 return S;
83}
84
86 std::unique_ptr<formatted_raw_ostream> OS,
87 std::unique_ptr<MCInstPrinter> IP,
88 std::unique_ptr<MCCodeEmitter> CE,
89 std::unique_ptr<MCAsmBackend> TAB) const {
90 MCInstPrinter *Printer = IP.get();
91 formatted_raw_ostream &OSRef = *OS;
92 MCStreamer *S;
93 if (AsmStreamerCtorFn)
94 S = AsmStreamerCtorFn(Ctx, std::move(OS), std::move(IP), std::move(CE),
95 std::move(TAB));
96 else
97 S = llvm::createAsmStreamer(Ctx, std::move(OS), std::move(IP),
98 std::move(CE), std::move(TAB));
99
101 return S;
102}
103
107
109 Triple &TheTriple,
110 std::string &Error) {
111 // Allocate target machine. First, check whether the user has explicitly
112 // specified an architecture to compile for. If so we have to look it up by
113 // name, because it might be a backend that has no mapping to a target triple.
114 const Target *TheTarget = nullptr;
115 if (!ArchName.empty()) {
116 auto I = find_if(targets(),
117 [&](const Target &T) { return ArchName == T.getName(); });
118
119 if (I == targets().end()) {
120 Error = ("invalid target '" + ArchName + "'.").str();
121 return nullptr;
122 }
123
124 TheTarget = &*I;
125
126 // Adjust the triple to match (if known), otherwise stick with the
127 // given triple.
130 TheTriple.setArch(Type);
131 } else {
132 // Get the target specific parser.
133 std::string TempError;
134 TheTarget = TargetRegistry::lookupTarget(TheTriple, TempError);
135 if (!TheTarget) {
136 Error = "unable to get target for '" + TheTriple.getTriple() +
137 "', see --version and --triple.";
138 return nullptr;
139 }
140 }
141
142 return TheTarget;
143}
144
146 std::string &Error) {
147 // Provide special warning when no targets are initialized.
148 if (targets().begin() == targets().end()) {
149 Error = "Unable to find target for this triple (no targets are registered)";
150 return nullptr;
151 }
152 Triple::ArchType Arch = TT.getArch();
153 auto ArchMatch = [&](const Target &T) { return T.ArchMatchFn(Arch); };
154 auto I = find_if(targets(), ArchMatch);
155
156 if (I == targets().end()) {
157 Error =
158 "No available targets are compatible with triple \"" + TT.str() + "\"";
159 return nullptr;
160 }
161
162 auto J = std::find_if(std::next(I), targets().end(), ArchMatch);
163 if (J != targets().end()) {
164 Error = std::string("Cannot choose between targets \"") + I->Name +
165 "\" and \"" + J->Name + "\"";
166 return nullptr;
167 }
168
169 return &*I;
170}
171
173 const char *ShortDesc,
174 const char *BackendName,
175 Target::ArchMatchFnTy ArchMatchFn,
176 bool HasJIT) {
177 assert(Name && ShortDesc && ArchMatchFn &&
178 "Missing required target information!");
179
180 // Check if this target has already been initialized, we allow this as a
181 // convenience to some clients.
182 if (T.Name)
183 return;
184
185 // Add to the list of targets.
186 T.Next = FirstTarget;
187 FirstTarget = &T;
188
189 T.Name = Name;
190 T.ShortDesc = ShortDesc;
191 T.BackendName = BackendName;
192 T.ArchMatchFn = ArchMatchFn;
193 T.HasJIT = HasJIT;
194}
195
196static int TargetArraySortFn(const std::pair<StringRef, const Target *> *LHS,
197 const std::pair<StringRef, const Target *> *RHS) {
198 return LHS->first.compare(RHS->first);
199}
200
202 std::vector<std::pair<StringRef, const Target*> > Targets;
203 size_t Width = 0;
204 for (const auto &T : TargetRegistry::targets()) {
205 Targets.push_back(std::make_pair(T.getName(), &T));
206 Width = std::max(Width, Targets.back().first.size());
207 }
208 array_pod_sort(Targets.begin(), Targets.end(), TargetArraySortFn);
209
210 OS << "\n";
211 OS << " Registered Targets:\n";
212 for (const auto &Target : Targets) {
213 OS << " " << Target.first;
214 OS.indent(Width - Target.first.size())
215 << " - " << Target.second->getShortDescription() << '\n';
216 }
217 if (Targets.empty())
218 OS << " (none)\n";
219}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
dxil DXContainer Global Emitter
dxil pretty DXIL Metadata Pretty Printer
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:221
Generic base class for all target subtargets.
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:143
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.
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:456
const std::string & getTriple() const
Definition Triple.h:488
@ UnknownObjectFormat
Definition Triple.h:326
LLVM_ABI void setArch(ArchType Kind, SubArchType SubArch=NoSubArch)
Set the architecture (first) component of the triple to a known type.
Definition Triple.cpp:1678
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:45
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.
Definition Types.h:26
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:34
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()