2009-07-15 12:24:58 +08:00
|
|
|
//===--- TargetRegistry.cpp - Target registration -------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2011-08-25 02:08:43 +08:00
|
|
|
#include "llvm/Support/TargetRegistry.h"
|
2011-07-22 15:50:44 +08:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
|
|
|
#include "llvm/ADT/StringRef.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2009-07-15 12:24:58 +08:00
|
|
|
#include <cassert>
|
2011-07-22 15:50:44 +08:00
|
|
|
#include <vector>
|
2009-07-15 12:24:58 +08:00
|
|
|
using namespace llvm;
|
|
|
|
|
2009-07-15 15:09:29 +08:00
|
|
|
// Clients are responsible for avoid race conditions in registration.
|
2014-04-07 12:17:22 +08:00
|
|
|
static Target *FirstTarget = nullptr;
|
2009-07-15 12:24:58 +08:00
|
|
|
|
2015-05-12 06:20:48 +08:00
|
|
|
iterator_range<TargetRegistry::iterator> TargetRegistry::targets() {
|
|
|
|
return make_range(iterator(FirstTarget), iterator());
|
2009-07-16 10:06:09 +08:00
|
|
|
}
|
|
|
|
|
2012-05-09 07:38:45 +08:00
|
|
|
const Target *TargetRegistry::lookupTarget(const std::string &ArchName,
|
|
|
|
Triple &TheTriple,
|
|
|
|
std::string &Error) {
|
|
|
|
// Allocate target machine. First, check whether the user has explicitly
|
|
|
|
// specified an architecture to compile for. If so we have to look it up by
|
|
|
|
// name, because it might be a backend that has no mapping to a target triple.
|
2014-04-07 12:17:22 +08:00
|
|
|
const Target *TheTarget = nullptr;
|
2012-05-09 07:38:45 +08:00
|
|
|
if (!ArchName.empty()) {
|
2016-08-12 08:18:03 +08:00
|
|
|
auto I = find_if(targets(),
|
2015-05-12 06:20:48 +08:00
|
|
|
[&](const Target &T) { return ArchName == T.getName(); });
|
2012-05-09 07:38:45 +08:00
|
|
|
|
2015-05-12 06:20:48 +08:00
|
|
|
if (I == targets().end()) {
|
2012-05-09 07:38:45 +08:00
|
|
|
Error = "error: invalid target '" + ArchName + "'.\n";
|
2014-04-07 12:17:22 +08:00
|
|
|
return nullptr;
|
2012-05-09 07:38:45 +08:00
|
|
|
}
|
|
|
|
|
2015-05-12 06:20:48 +08:00
|
|
|
TheTarget = &*I;
|
|
|
|
|
2012-05-09 07:38:45 +08:00
|
|
|
// Adjust the triple to match (if known), otherwise stick with the
|
|
|
|
// given triple.
|
|
|
|
Triple::ArchType Type = Triple::getArchTypeForLLVMName(ArchName);
|
|
|
|
if (Type != Triple::UnknownArch)
|
|
|
|
TheTriple.setArch(Type);
|
|
|
|
} else {
|
|
|
|
// Get the target specific parser.
|
|
|
|
std::string TempError;
|
|
|
|
TheTarget = TargetRegistry::lookupTarget(TheTriple.getTriple(), TempError);
|
2014-04-09 12:20:00 +08:00
|
|
|
if (!TheTarget) {
|
2012-05-09 07:38:45 +08:00
|
|
|
Error = ": error: unable to get target for '"
|
|
|
|
+ TheTriple.getTriple()
|
|
|
|
+ "', see --version and --triple.\n";
|
2014-04-07 12:17:22 +08:00
|
|
|
return nullptr;
|
2012-05-09 07:38:45 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return TheTarget;
|
|
|
|
}
|
|
|
|
|
2009-07-26 10:12:58 +08:00
|
|
|
const Target *TargetRegistry::lookupTarget(const std::string &TT,
|
|
|
|
std::string &Error) {
|
2009-07-17 23:50:49 +08:00
|
|
|
// Provide special warning when no targets are initialized.
|
2015-05-12 06:20:48 +08:00
|
|
|
if (targets().begin() == targets().end()) {
|
2009-07-17 23:50:49 +08:00
|
|
|
Error = "Unable to find target for this triple (no targets are registered)";
|
2014-04-07 12:17:22 +08:00
|
|
|
return nullptr;
|
2009-07-17 23:50:49 +08:00
|
|
|
}
|
2015-05-12 06:20:48 +08:00
|
|
|
Triple::ArchType Arch = Triple(TT).getArch();
|
|
|
|
auto ArchMatch = [&](const Target &T) { return T.ArchMatchFn(Arch); };
|
2016-08-12 08:18:03 +08:00
|
|
|
auto I = find_if(targets(), ArchMatch);
|
2009-07-15 12:24:58 +08:00
|
|
|
|
2015-05-12 06:20:48 +08:00
|
|
|
if (I == targets().end()) {
|
2018-08-23 11:40:31 +08:00
|
|
|
Error = "No available targets are compatible with triple \"" + TT + "\"";
|
2014-04-07 12:17:22 +08:00
|
|
|
return nullptr;
|
2009-07-15 12:24:58 +08:00
|
|
|
}
|
|
|
|
|
2015-05-12 06:20:48 +08:00
|
|
|
auto J = std::find_if(std::next(I), targets().end(), ArchMatch);
|
|
|
|
if (J != targets().end()) {
|
2016-10-01 15:08:23 +08:00
|
|
|
Error = std::string("Cannot choose between targets \"") + I->Name +
|
|
|
|
"\" and \"" + J->Name + "\"";
|
2015-05-12 06:20:48 +08:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return &*I;
|
2009-07-15 12:24:58 +08:00
|
|
|
}
|
|
|
|
|
Add backend name to Target to enable runtime info to be fed back into TableGen
Summary:
Make it possible to feed runtime information back to tablegen to enable
profile-guided tablegen-eration, detection of untested tablegen definitions, etc.
Being a cross-compiler by nature, LLVM will potentially collect data for multiple
architectures (e.g. when running 'ninja check'). We therefore need a way for
TableGen to figure out what data applies to the backend it is generating at the
time. This patch achieves that by including the name of the 'def X : Target ...'
for the backend in the TargetRegistry.
Reviewers: qcolombet
Reviewed By: qcolombet
Subscribers: jholewinski, arsenm, jyknight, aditya_nandakumar, sdardis, nemanjai, ab, nhaehnle, t.p.northover, javed.absar, qcolombet, llvm-commits, fedor.sergeev
Differential Revision: https://reviews.llvm.org/D39742
llvm-svn: 318352
2017-11-16 07:55:44 +08:00
|
|
|
void TargetRegistry::RegisterTarget(Target &T, const char *Name,
|
2016-10-01 15:08:23 +08:00
|
|
|
const char *ShortDesc,
|
Add backend name to Target to enable runtime info to be fed back into TableGen
Summary:
Make it possible to feed runtime information back to tablegen to enable
profile-guided tablegen-eration, detection of untested tablegen definitions, etc.
Being a cross-compiler by nature, LLVM will potentially collect data for multiple
architectures (e.g. when running 'ninja check'). We therefore need a way for
TableGen to figure out what data applies to the backend it is generating at the
time. This patch achieves that by including the name of the 'def X : Target ...'
for the backend in the TargetRegistry.
Reviewers: qcolombet
Reviewed By: qcolombet
Subscribers: jholewinski, arsenm, jyknight, aditya_nandakumar, sdardis, nemanjai, ab, nhaehnle, t.p.northover, javed.absar, qcolombet, llvm-commits, fedor.sergeev
Differential Revision: https://reviews.llvm.org/D39742
llvm-svn: 318352
2017-11-16 07:55:44 +08:00
|
|
|
const char *BackendName,
|
2013-12-14 00:05:32 +08:00
|
|
|
Target::ArchMatchFnTy ArchMatchFn,
|
2009-07-25 18:09:50 +08:00
|
|
|
bool HasJIT) {
|
2016-10-01 15:08:23 +08:00
|
|
|
assert(Name && ShortDesc && ArchMatchFn &&
|
2009-07-15 12:24:58 +08:00
|
|
|
"Missing required target information!");
|
2009-07-16 04:24:03 +08:00
|
|
|
|
|
|
|
// Check if this target has already been initialized, we allow this as a
|
|
|
|
// convenience to some clients.
|
2016-10-01 15:08:23 +08:00
|
|
|
if (T.Name)
|
2009-07-16 04:24:03 +08:00
|
|
|
return;
|
2018-07-31 03:41:25 +08:00
|
|
|
|
2009-07-15 12:24:58 +08:00
|
|
|
// Add to the list of targets.
|
|
|
|
T.Next = FirstTarget;
|
2009-07-15 15:37:49 +08:00
|
|
|
FirstTarget = &T;
|
2009-07-15 12:24:58 +08:00
|
|
|
|
|
|
|
T.Name = Name;
|
|
|
|
T.ShortDesc = ShortDesc;
|
Add backend name to Target to enable runtime info to be fed back into TableGen
Summary:
Make it possible to feed runtime information back to tablegen to enable
profile-guided tablegen-eration, detection of untested tablegen definitions, etc.
Being a cross-compiler by nature, LLVM will potentially collect data for multiple
architectures (e.g. when running 'ninja check'). We therefore need a way for
TableGen to figure out what data applies to the backend it is generating at the
time. This patch achieves that by including the name of the 'def X : Target ...'
for the backend in the TargetRegistry.
Reviewers: qcolombet
Reviewed By: qcolombet
Subscribers: jholewinski, arsenm, jyknight, aditya_nandakumar, sdardis, nemanjai, ab, nhaehnle, t.p.northover, javed.absar, qcolombet, llvm-commits, fedor.sergeev
Differential Revision: https://reviews.llvm.org/D39742
llvm-svn: 318352
2017-11-16 07:55:44 +08:00
|
|
|
T.BackendName = BackendName;
|
2013-12-14 00:05:32 +08:00
|
|
|
T.ArchMatchFn = ArchMatchFn;
|
2009-07-25 18:09:50 +08:00
|
|
|
T.HasJIT = HasJIT;
|
2009-07-15 12:24:58 +08:00
|
|
|
}
|
|
|
|
|
2014-03-08 05:52:38 +08:00
|
|
|
static int TargetArraySortFn(const std::pair<StringRef, const Target *> *LHS,
|
|
|
|
const std::pair<StringRef, const Target *> *RHS) {
|
|
|
|
return LHS->first.compare(RHS->first);
|
|
|
|
}
|
|
|
|
|
2017-06-07 05:54:04 +08:00
|
|
|
void TargetRegistry::printRegisteredTargetsForVersion(raw_ostream &OS) {
|
2011-07-22 15:50:44 +08:00
|
|
|
std::vector<std::pair<StringRef, const Target*> > Targets;
|
|
|
|
size_t Width = 0;
|
2015-05-12 06:20:48 +08:00
|
|
|
for (const auto &T : TargetRegistry::targets()) {
|
|
|
|
Targets.push_back(std::make_pair(T.getName(), &T));
|
2011-07-22 15:50:44 +08:00
|
|
|
Width = std::max(Width, Targets.back().first.size());
|
|
|
|
}
|
2014-03-08 05:52:38 +08:00
|
|
|
array_pod_sort(Targets.begin(), Targets.end(), TargetArraySortFn);
|
2011-07-22 15:50:44 +08:00
|
|
|
|
|
|
|
OS << " Registered Targets:\n";
|
|
|
|
for (unsigned i = 0, e = Targets.size(); i != e; ++i) {
|
|
|
|
OS << " " << Targets[i].first;
|
|
|
|
OS.indent(Width - Targets[i].first.size()) << " - "
|
|
|
|
<< Targets[i].second->getShortDescription() << '\n';
|
|
|
|
}
|
|
|
|
if (Targets.empty())
|
|
|
|
OS << " (none)\n";
|
|
|
|
}
|