Daniel Dunbar | 078a71e | 2009-07-15 04:24:58 +0000 | [diff] [blame] | 1 | //===--- TargetRegistry.cpp - Target registration -------------------------===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
Daniel Dunbar | 078a71e | 2009-07-15 04:24:58 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
Evan Cheng | 2bb4035 | 2011-08-24 18:08:43 +0000 | [diff] [blame] | 9 | #include "llvm/Support/TargetRegistry.h" |
Chandler Carruth | 2baac02 | 2011-07-22 07:50:44 +0000 | [diff] [blame] | 10 | #include "llvm/ADT/STLExtras.h" |
| 11 | #include "llvm/ADT/StringRef.h" |
Chandler Carruth | 2baac02 | 2011-07-22 07:50:44 +0000 | [diff] [blame] | 12 | #include "llvm/Support/raw_ostream.h" |
Daniel Dunbar | 078a71e | 2009-07-15 04:24:58 +0000 | [diff] [blame] | 13 | #include <cassert> |
Chandler Carruth | 2baac02 | 2011-07-22 07:50:44 +0000 | [diff] [blame] | 14 | #include <vector> |
Daniel Dunbar | 078a71e | 2009-07-15 04:24:58 +0000 | [diff] [blame] | 15 | using namespace llvm; |
| 16 | |
Daniel Dunbar | 1c66332 | 2009-07-15 07:09:29 +0000 | [diff] [blame] | 17 | // Clients are responsible for avoid race conditions in registration. |
Craig Topper | c10719f | 2014-04-07 04:17:22 +0000 | [diff] [blame] | 18 | static Target *FirstTarget = nullptr; |
Daniel Dunbar | 078a71e | 2009-07-15 04:24:58 +0000 | [diff] [blame] | 19 | |
David Blaikie | 46c561c | 2015-05-11 22:20:48 +0000 | [diff] [blame] | 20 | iterator_range<TargetRegistry::iterator> TargetRegistry::targets() { |
| 21 | return make_range(iterator(FirstTarget), iterator()); |
Daniel Dunbar | 78c8fb1 | 2009-07-16 02:06:09 +0000 | [diff] [blame] | 22 | } |
| 23 | |
Kevin Enderby | fe3d005 | 2012-05-08 23:38:45 +0000 | [diff] [blame] | 24 | const 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 Topper | c10719f | 2014-04-07 04:17:22 +0000 | [diff] [blame] | 30 | const Target *TheTarget = nullptr; |
Kevin Enderby | fe3d005 | 2012-05-08 23:38:45 +0000 | [diff] [blame] | 31 | if (!ArchName.empty()) { |
David Majnemer | 562e829 | 2016-08-12 00:18:03 +0000 | [diff] [blame] | 32 | auto I = find_if(targets(), |
David Blaikie | 46c561c | 2015-05-11 22:20:48 +0000 | [diff] [blame] | 33 | [&](const Target &T) { return ArchName == T.getName(); }); |
Kevin Enderby | fe3d005 | 2012-05-08 23:38:45 +0000 | [diff] [blame] | 34 | |
David Blaikie | 46c561c | 2015-05-11 22:20:48 +0000 | [diff] [blame] | 35 | if (I == targets().end()) { |
Kevin Enderby | fe3d005 | 2012-05-08 23:38:45 +0000 | [diff] [blame] | 36 | Error = "error: invalid target '" + ArchName + "'.\n"; |
Craig Topper | c10719f | 2014-04-07 04:17:22 +0000 | [diff] [blame] | 37 | return nullptr; |
Kevin Enderby | fe3d005 | 2012-05-08 23:38:45 +0000 | [diff] [blame] | 38 | } |
| 39 | |
David Blaikie | 46c561c | 2015-05-11 22:20:48 +0000 | [diff] [blame] | 40 | TheTarget = &*I; |
| 41 | |
Kevin Enderby | fe3d005 | 2012-05-08 23:38:45 +0000 | [diff] [blame] | 42 | // 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 Topper | 8d399f8 | 2014-04-09 04:20:00 +0000 | [diff] [blame] | 51 | if (!TheTarget) { |
Kevin Enderby | fe3d005 | 2012-05-08 23:38:45 +0000 | [diff] [blame] | 52 | Error = ": error: unable to get target for '" |
| 53 | + TheTriple.getTriple() |
| 54 | + "', see --version and --triple.\n"; |
Craig Topper | c10719f | 2014-04-07 04:17:22 +0000 | [diff] [blame] | 55 | return nullptr; |
Kevin Enderby | fe3d005 | 2012-05-08 23:38:45 +0000 | [diff] [blame] | 56 | } |
| 57 | } |
| 58 | |
| 59 | return TheTarget; |
| 60 | } |
| 61 | |
Daniel Dunbar | 47d6791 | 2009-07-26 02:12:58 +0000 | [diff] [blame] | 62 | const Target *TargetRegistry::lookupTarget(const std::string &TT, |
Daniel Dunbar | 47d6791 | 2009-07-26 02:12:58 +0000 | [diff] [blame] | 63 | std::string &Error) { |
Daniel Dunbar | 50745bf | 2009-07-17 15:50:49 +0000 | [diff] [blame] | 64 | // Provide special warning when no targets are initialized. |
David Blaikie | 46c561c | 2015-05-11 22:20:48 +0000 | [diff] [blame] | 65 | if (targets().begin() == targets().end()) { |
Daniel Dunbar | 50745bf | 2009-07-17 15:50:49 +0000 | [diff] [blame] | 66 | Error = "Unable to find target for this triple (no targets are registered)"; |
Craig Topper | c10719f | 2014-04-07 04:17:22 +0000 | [diff] [blame] | 67 | return nullptr; |
Daniel Dunbar | 50745bf | 2009-07-17 15:50:49 +0000 | [diff] [blame] | 68 | } |
David Blaikie | 46c561c | 2015-05-11 22:20:48 +0000 | [diff] [blame] | 69 | Triple::ArchType Arch = Triple(TT).getArch(); |
| 70 | auto ArchMatch = [&](const Target &T) { return T.ArchMatchFn(Arch); }; |
David Majnemer | 562e829 | 2016-08-12 00:18:03 +0000 | [diff] [blame] | 71 | auto I = find_if(targets(), ArchMatch); |
Daniel Dunbar | 078a71e | 2009-07-15 04:24:58 +0000 | [diff] [blame] | 72 | |
David Blaikie | 46c561c | 2015-05-11 22:20:48 +0000 | [diff] [blame] | 73 | if (I == targets().end()) { |
JF Bastien | a1cca51 | 2018-08-23 03:40:31 +0000 | [diff] [blame] | 74 | Error = "No available targets are compatible with triple \"" + TT + "\""; |
Craig Topper | c10719f | 2014-04-07 04:17:22 +0000 | [diff] [blame] | 75 | return nullptr; |
Daniel Dunbar | 078a71e | 2009-07-15 04:24:58 +0000 | [diff] [blame] | 76 | } |
| 77 | |
David Blaikie | 46c561c | 2015-05-11 22:20:48 +0000 | [diff] [blame] | 78 | auto J = std::find_if(std::next(I), targets().end(), ArchMatch); |
| 79 | if (J != targets().end()) { |
Mehdi Amini | 9af9a9d | 2016-10-01 07:08:23 +0000 | [diff] [blame] | 80 | Error = std::string("Cannot choose between targets \"") + I->Name + |
| 81 | "\" and \"" + J->Name + "\""; |
David Blaikie | 46c561c | 2015-05-11 22:20:48 +0000 | [diff] [blame] | 82 | return nullptr; |
| 83 | } |
| 84 | |
| 85 | return &*I; |
Daniel Dunbar | 078a71e | 2009-07-15 04:24:58 +0000 | [diff] [blame] | 86 | } |
| 87 | |
Daniel Sanders | 725584e | 2017-11-15 23:55:44 +0000 | [diff] [blame] | 88 | void TargetRegistry::RegisterTarget(Target &T, const char *Name, |
Mehdi Amini | 9af9a9d | 2016-10-01 07:08:23 +0000 | [diff] [blame] | 89 | const char *ShortDesc, |
Daniel Sanders | 725584e | 2017-11-15 23:55:44 +0000 | [diff] [blame] | 90 | const char *BackendName, |
Rafael Espindola | dfc1470 | 2013-12-13 16:05:32 +0000 | [diff] [blame] | 91 | Target::ArchMatchFnTy ArchMatchFn, |
Daniel Dunbar | 691a478 | 2009-07-25 10:09:50 +0000 | [diff] [blame] | 92 | bool HasJIT) { |
Mehdi Amini | 9af9a9d | 2016-10-01 07:08:23 +0000 | [diff] [blame] | 93 | assert(Name && ShortDesc && ArchMatchFn && |
Daniel Dunbar | 078a71e | 2009-07-15 04:24:58 +0000 | [diff] [blame] | 94 | "Missing required target information!"); |
Daniel Dunbar | e833810 | 2009-07-15 20:24:03 +0000 | [diff] [blame] | 95 | |
| 96 | // Check if this target has already been initialized, we allow this as a |
| 97 | // convenience to some clients. |
Mehdi Amini | 9af9a9d | 2016-10-01 07:08:23 +0000 | [diff] [blame] | 98 | if (T.Name) |
Daniel Dunbar | e833810 | 2009-07-15 20:24:03 +0000 | [diff] [blame] | 99 | return; |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 100 | |
Daniel Dunbar | 078a71e | 2009-07-15 04:24:58 +0000 | [diff] [blame] | 101 | // Add to the list of targets. |
| 102 | T.Next = FirstTarget; |
Daniel Dunbar | 54995de | 2009-07-15 07:37:49 +0000 | [diff] [blame] | 103 | FirstTarget = &T; |
Daniel Dunbar | 078a71e | 2009-07-15 04:24:58 +0000 | [diff] [blame] | 104 | |
| 105 | T.Name = Name; |
| 106 | T.ShortDesc = ShortDesc; |
Daniel Sanders | 725584e | 2017-11-15 23:55:44 +0000 | [diff] [blame] | 107 | T.BackendName = BackendName; |
Rafael Espindola | dfc1470 | 2013-12-13 16:05:32 +0000 | [diff] [blame] | 108 | T.ArchMatchFn = ArchMatchFn; |
Daniel Dunbar | 691a478 | 2009-07-25 10:09:50 +0000 | [diff] [blame] | 109 | T.HasJIT = HasJIT; |
Daniel Dunbar | 078a71e | 2009-07-15 04:24:58 +0000 | [diff] [blame] | 110 | } |
| 111 | |
Benjamin Kramer | adf1ea8 | 2014-03-07 21:52:38 +0000 | [diff] [blame] | 112 | static 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 Andric | bc3feaa | 2017-06-06 21:54:04 +0000 | [diff] [blame] | 117 | void TargetRegistry::printRegisteredTargetsForVersion(raw_ostream &OS) { |
Chandler Carruth | 2baac02 | 2011-07-22 07:50:44 +0000 | [diff] [blame] | 118 | std::vector<std::pair<StringRef, const Target*> > Targets; |
| 119 | size_t Width = 0; |
David Blaikie | 46c561c | 2015-05-11 22:20:48 +0000 | [diff] [blame] | 120 | for (const auto &T : TargetRegistry::targets()) { |
| 121 | Targets.push_back(std::make_pair(T.getName(), &T)); |
Chandler Carruth | 2baac02 | 2011-07-22 07:50:44 +0000 | [diff] [blame] | 122 | Width = std::max(Width, Targets.back().first.size()); |
| 123 | } |
Benjamin Kramer | adf1ea8 | 2014-03-07 21:52:38 +0000 | [diff] [blame] | 124 | array_pod_sort(Targets.begin(), Targets.end(), TargetArraySortFn); |
Chandler Carruth | 2baac02 | 2011-07-22 07:50:44 +0000 | [diff] [blame] | 125 | |
Chandler Carruth | 2baac02 | 2011-07-22 07:50:44 +0000 | [diff] [blame] | 126 | 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 | } |