blob: 1f9c3bbf8229469c8e02eaa7164125ceb6276971 [file] [log] [blame]
Daniel Dunbar078a71e2009-07-15 04:24:58 +00001//===--- TargetRegistry.cpp - Target registration -------------------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Daniel Dunbar078a71e2009-07-15 04:24:58 +00006//
7//===----------------------------------------------------------------------===//
8
Evan Cheng2bb40352011-08-24 18:08:43 +00009#include "llvm/Support/TargetRegistry.h"
Chandler Carruth2baac022011-07-22 07:50:44 +000010#include "llvm/ADT/STLExtras.h"
11#include "llvm/ADT/StringRef.h"
Chandler Carruth2baac022011-07-22 07:50:44 +000012#include "llvm/Support/raw_ostream.h"
Daniel Dunbar078a71e2009-07-15 04:24:58 +000013#include <cassert>
Chandler Carruth2baac022011-07-22 07:50:44 +000014#include <vector>
Daniel Dunbar078a71e2009-07-15 04:24:58 +000015using namespace llvm;
16
Daniel Dunbar1c663322009-07-15 07:09:29 +000017// Clients are responsible for avoid race conditions in registration.
Craig Topperc10719f2014-04-07 04:17:22 +000018static Target *FirstTarget = nullptr;
Daniel Dunbar078a71e2009-07-15 04:24:58 +000019
David Blaikie46c561c2015-05-11 22:20:48 +000020iterator_range<TargetRegistry::iterator> TargetRegistry::targets() {
21 return make_range(iterator(FirstTarget), iterator());
Daniel Dunbar78c8fb12009-07-16 02:06:09 +000022}
23
Kevin Enderbyfe3d0052012-05-08 23:38:45 +000024const Target *TargetRegistry::lookupTarget(const std::string &ArchName,
25 Triple &TheTriple,
26 std::string &Error) {
27 // Allocate target machine. First, check whether the user has explicitly
28 // specified an architecture to compile for. If so we have to look it up by
29 // name, because it might be a backend that has no mapping to a target triple.
Craig Topperc10719f2014-04-07 04:17:22 +000030 const Target *TheTarget = nullptr;
Kevin Enderbyfe3d0052012-05-08 23:38:45 +000031 if (!ArchName.empty()) {
David Majnemer562e8292016-08-12 00:18:03 +000032 auto I = find_if(targets(),
David Blaikie46c561c2015-05-11 22:20:48 +000033 [&](const Target &T) { return ArchName == T.getName(); });
Kevin Enderbyfe3d0052012-05-08 23:38:45 +000034
David Blaikie46c561c2015-05-11 22:20:48 +000035 if (I == targets().end()) {
Kevin Enderbyfe3d0052012-05-08 23:38:45 +000036 Error = "error: invalid target '" + ArchName + "'.\n";
Craig Topperc10719f2014-04-07 04:17:22 +000037 return nullptr;
Kevin Enderbyfe3d0052012-05-08 23:38:45 +000038 }
39
David Blaikie46c561c2015-05-11 22:20:48 +000040 TheTarget = &*I;
41
Kevin Enderbyfe3d0052012-05-08 23:38:45 +000042 // Adjust the triple to match (if known), otherwise stick with the
43 // given triple.
44 Triple::ArchType Type = Triple::getArchTypeForLLVMName(ArchName);
45 if (Type != Triple::UnknownArch)
46 TheTriple.setArch(Type);
47 } else {
48 // Get the target specific parser.
49 std::string TempError;
50 TheTarget = TargetRegistry::lookupTarget(TheTriple.getTriple(), TempError);
Craig Topper8d399f82014-04-09 04:20:00 +000051 if (!TheTarget) {
Kevin Enderbyfe3d0052012-05-08 23:38:45 +000052 Error = ": error: unable to get target for '"
53 + TheTriple.getTriple()
54 + "', see --version and --triple.\n";
Craig Topperc10719f2014-04-07 04:17:22 +000055 return nullptr;
Kevin Enderbyfe3d0052012-05-08 23:38:45 +000056 }
57 }
58
59 return TheTarget;
60}
61
Daniel Dunbar47d67912009-07-26 02:12:58 +000062const Target *TargetRegistry::lookupTarget(const std::string &TT,
Daniel Dunbar47d67912009-07-26 02:12:58 +000063 std::string &Error) {
Daniel Dunbar50745bf2009-07-17 15:50:49 +000064 // Provide special warning when no targets are initialized.
David Blaikie46c561c2015-05-11 22:20:48 +000065 if (targets().begin() == targets().end()) {
Daniel Dunbar50745bf2009-07-17 15:50:49 +000066 Error = "Unable to find target for this triple (no targets are registered)";
Craig Topperc10719f2014-04-07 04:17:22 +000067 return nullptr;
Daniel Dunbar50745bf2009-07-17 15:50:49 +000068 }
David Blaikie46c561c2015-05-11 22:20:48 +000069 Triple::ArchType Arch = Triple(TT).getArch();
70 auto ArchMatch = [&](const Target &T) { return T.ArchMatchFn(Arch); };
David Majnemer562e8292016-08-12 00:18:03 +000071 auto I = find_if(targets(), ArchMatch);
Daniel Dunbar078a71e2009-07-15 04:24:58 +000072
David Blaikie46c561c2015-05-11 22:20:48 +000073 if (I == targets().end()) {
JF Bastiena1cca512018-08-23 03:40:31 +000074 Error = "No available targets are compatible with triple \"" + TT + "\"";
Craig Topperc10719f2014-04-07 04:17:22 +000075 return nullptr;
Daniel Dunbar078a71e2009-07-15 04:24:58 +000076 }
77
David Blaikie46c561c2015-05-11 22:20:48 +000078 auto J = std::find_if(std::next(I), targets().end(), ArchMatch);
79 if (J != targets().end()) {
Mehdi Amini9af9a9d2016-10-01 07:08:23 +000080 Error = std::string("Cannot choose between targets \"") + I->Name +
81 "\" and \"" + J->Name + "\"";
David Blaikie46c561c2015-05-11 22:20:48 +000082 return nullptr;
83 }
84
85 return &*I;
Daniel Dunbar078a71e2009-07-15 04:24:58 +000086}
87
Daniel Sanders725584e2017-11-15 23:55:44 +000088void TargetRegistry::RegisterTarget(Target &T, const char *Name,
Mehdi Amini9af9a9d2016-10-01 07:08:23 +000089 const char *ShortDesc,
Daniel Sanders725584e2017-11-15 23:55:44 +000090 const char *BackendName,
Rafael Espindoladfc14702013-12-13 16:05:32 +000091 Target::ArchMatchFnTy ArchMatchFn,
Daniel Dunbar691a4782009-07-25 10:09:50 +000092 bool HasJIT) {
Mehdi Amini9af9a9d2016-10-01 07:08:23 +000093 assert(Name && ShortDesc && ArchMatchFn &&
Daniel Dunbar078a71e2009-07-15 04:24:58 +000094 "Missing required target information!");
Daniel Dunbare8338102009-07-15 20:24:03 +000095
96 // Check if this target has already been initialized, we allow this as a
97 // convenience to some clients.
Mehdi Amini9af9a9d2016-10-01 07:08:23 +000098 if (T.Name)
Daniel Dunbare8338102009-07-15 20:24:03 +000099 return;
Fangrui Songf78650a2018-07-30 19:41:25 +0000100
Daniel Dunbar078a71e2009-07-15 04:24:58 +0000101 // Add to the list of targets.
102 T.Next = FirstTarget;
Daniel Dunbar54995de2009-07-15 07:37:49 +0000103 FirstTarget = &T;
Daniel Dunbar078a71e2009-07-15 04:24:58 +0000104
105 T.Name = Name;
106 T.ShortDesc = ShortDesc;
Daniel Sanders725584e2017-11-15 23:55:44 +0000107 T.BackendName = BackendName;
Rafael Espindoladfc14702013-12-13 16:05:32 +0000108 T.ArchMatchFn = ArchMatchFn;
Daniel Dunbar691a4782009-07-25 10:09:50 +0000109 T.HasJIT = HasJIT;
Daniel Dunbar078a71e2009-07-15 04:24:58 +0000110}
111
Benjamin Krameradf1ea82014-03-07 21:52:38 +0000112static int TargetArraySortFn(const std::pair<StringRef, const Target *> *LHS,
113 const std::pair<StringRef, const Target *> *RHS) {
114 return LHS->first.compare(RHS->first);
115}
116
Dimitry Andricbc3feaa2017-06-06 21:54:04 +0000117void TargetRegistry::printRegisteredTargetsForVersion(raw_ostream &OS) {
Chandler Carruth2baac022011-07-22 07:50:44 +0000118 std::vector<std::pair<StringRef, const Target*> > Targets;
119 size_t Width = 0;
David Blaikie46c561c2015-05-11 22:20:48 +0000120 for (const auto &T : TargetRegistry::targets()) {
121 Targets.push_back(std::make_pair(T.getName(), &T));
Chandler Carruth2baac022011-07-22 07:50:44 +0000122 Width = std::max(Width, Targets.back().first.size());
123 }
Benjamin Krameradf1ea82014-03-07 21:52:38 +0000124 array_pod_sort(Targets.begin(), Targets.end(), TargetArraySortFn);
Chandler Carruth2baac022011-07-22 07:50:44 +0000125
Chandler Carruth2baac022011-07-22 07:50:44 +0000126 OS << " Registered Targets:\n";
127 for (unsigned i = 0, e = Targets.size(); i != e; ++i) {
128 OS << " " << Targets[i].first;
129 OS.indent(Width - Targets[i].first.size()) << " - "
130 << Targets[i].second->getShortDescription() << '\n';
131 }
132 if (Targets.empty())
133 OS << " (none)\n";
134}