Add TargetRegistry::lookupTarget.
authorDaniel Dunbar <daniel@zuster.org>
Sun, 26 Jul 2009 02:12:58 +0000 (02:12 +0000)
committerDaniel Dunbar <daniel@zuster.org>
Sun, 26 Jul 2009 02:12:58 +0000 (02:12 +0000)
 - This is a simplified mechanism which just looks up a target based on the
   target triple, with a few additional flags.

 - Remove getClosestStaticTargetForModule, the moral equivalent is now:
     lookupTarget(Mod->getTargetTriple, true, false, ...);

 - This no longer does the fuzzy matching with target data (based on endianness
   and pointer width) that getClosestStaticTargetForModule was doing, but this
   was deemed unnecessary.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@77111 91177308-0d34-0410-b5e6-96231b3b80d8

docs/ReleaseNotes-2.6.html
include/llvm/Target/TargetRegistry.h
lib/Support/TargetRegistry.cpp
lib/Target/CBackend/CBackend.cpp
tools/llc/llc.cpp
tools/llvm-mc/llvm-mc.cpp
tools/lto/LTOCodeGenerator.cpp
tools/lto/LTOModule.cpp

index 7f287bff06a3e9a42be93dbe99e790b7e863bb09..8cc833105e5b464f02661f106daee3b9792b6b09 100644 (file)
@@ -490,6 +490,9 @@ clients should be unaffected by this transition, unless they are used to <tt>Val
   </ul>
 </li>
 
+<li>The registration interfaces for backend Targets has changed (what was
+previously TargetMachineRegistry). FIXME: Complete this section, explain client
+changes, point to documentation on new backend interface.</li>
 
 <li>llvm-dis now fails if output file exists, instead of dumping to stdout.
 FIXME: describe any other tool changes due to the raw_fd_ostream change.  FIXME:
index 64d6e12fea3e2ea3fc23a91bcf5e3e705cd85cf1..420fba5a09d3a28b9d4a422e4da15f8954a69e47 100644 (file)
@@ -183,24 +183,27 @@ namespace llvm {
 
     static iterator end() { return iterator(); }
 
-    /// getClosestStaticTargetForTriple - Given a target triple, pick the most
-    /// capable target for that triple.
-    static const Target *getClosestStaticTargetForTriple(const std::string &TT,
-                                                         std::string &Error);
-
-    /// getClosestStaticTargetForModule - Given an LLVM module, pick the best
-    /// target that is compatible with the module.  If no close target can be
-    /// found, this returns null and sets the Error string to a reason.
-    static const Target *getClosestStaticTargetForModule(const Module &M,
-                                                        std::string &Error);
+    /// lookupTarget - Lookup a target based on a target triple.
+    ///
+    /// \param Triple - The triple to use for finding a target.
+    /// \param FallbackToHost - If true and no target is found for the given
+    /// \arg Triple, then the host's triple will be used.
+    /// \param RequireJIT - Require the target to support JIT compilation.
+    /// \param Error - On failure, an error string describing why no target was
+    /// found.
+    static const Target *lookupTarget(const std::string &Triple,
+                                      bool FallbackToHost,
+                                      bool RequireJIT,
+                                      std::string &Error);
 
     /// getClosestTargetForJIT - Pick the best target that is compatible with
     /// the current host.  If no close target can be found, this returns null
     /// and sets the Error string to a reason.
-    //
-    // FIXME: Do we still need this interface, clients can always look for the
-    // match for the host triple.
-    static const Target *getClosestTargetForJIT(std::string &Error);
+    ///
+    /// Mainted for compatibility through 2.6.
+    static const Target *getClosestTargetForJIT(std::string &Error) {
+      return lookupTarget("", true, true, Error);
+    }
 
     /// @}
     /// @name Target Registration
index f9026f12afd1cef2ff327013ed585ae52279a2c2..4558d2d2159dfcc968da6168a0da407bf6fb0496 100644 (file)
@@ -7,6 +7,7 @@
 //
 //===----------------------------------------------------------------------===//
 
+#include "llvm/Module.h"
 #include "llvm/Target/TargetRegistry.h"
 #include "llvm/System/Host.h"
 #include <cassert>
@@ -19,9 +20,10 @@ TargetRegistry::iterator TargetRegistry::begin() {
   return iterator(FirstTarget);
 }
 
-const Target *
-TargetRegistry::getClosestStaticTargetForTriple(const std::string &TT,
-                                                std::string &Error) {
+const Target *TargetRegistry::lookupTarget(const std::string &TT,
+                                           bool FallbackToHost,
+                                           bool RequireJIT,
+                                           std::string &Error) {
   // Provide special warning when no targets are initialized.
   if (begin() == end()) {
     Error = "Unable to find target for this triple (no targets are registered)";
@@ -30,45 +32,10 @@ TargetRegistry::getClosestStaticTargetForTriple(const std::string &TT,
   const Target *Best = 0, *EquallyBest = 0;
   unsigned BestQuality = 0;
   for (iterator it = begin(), ie = end(); it != ie; ++it) {
-    if (unsigned Qual = it->TripleMatchQualityFn(TT)) {
-      if (!Best || Qual > BestQuality) {
-        Best = &*it;
-        EquallyBest = 0;
-        BestQuality = Qual;
-      } else if (Qual == BestQuality)
-        EquallyBest = &*it;
-    }
-  }
-
-  if (!Best) {
-    Error = "No available targets are compatible with this triple";
-    return 0;
-  }
-
-  // Otherwise, take the best target, but make sure we don't have two equally
-  // good best targets.
-  if (EquallyBest) {
-    Error = std::string("Cannot choose between targets \"") +
-      Best->Name  + "\" and \"" + EquallyBest->Name + "\"";
-    return 0;
-  }
-
-  return Best;
-}
-
-const Target *
-TargetRegistry::getClosestStaticTargetForModule(const Module &M,
-                                                std::string &Error) {
-  // Provide special warning when no targets are initialized.
-  if (begin() == end()) {
-    Error = "Unable to find target for this module (no targets are registered)";
-    return 0;
-  }
+    if (RequireJIT && !it->hasJIT())
+      continue;
 
-  const Target *Best = 0, *EquallyBest = 0;
-  unsigned BestQuality = 0;
-  for (iterator it = begin(), ie = end(); it != ie; ++it) {
-    if (unsigned Qual = it->ModuleMatchQualityFn(M)) {
+    if (unsigned Qual = it->TripleMatchQualityFn(TT)) {
       if (!Best || Qual > BestQuality) {
         Best = &*it;
         EquallyBest = 0;
@@ -78,18 +45,18 @@ TargetRegistry::getClosestStaticTargetForModule(const Module &M,
     }
   }
 
-  // FIXME: This is a hack to ignore super weak matches like msil, etc. and look
-  // by host instead. They will be found again via the triple.
-  if (Best && BestQuality == 1)    
-    Best = EquallyBest = 0;
+  // FIXME: Hack. If we only have an extremely weak match and the client
+  // requested to fall back to the host, then ignore it and try again.
+  if (BestQuality == 1 && FallbackToHost)
+    Best = 0;
 
-  // If that failed, try looking up the host triple.
-  if (!Best)
-    Best = getClosestStaticTargetForTriple(sys::getHostTriple(), Error);
+  // Fallback to the host triple if we didn't find anything.
+  if (!Best && FallbackToHost)
+    return lookupTarget(sys::getHostTriple(), false, RequireJIT, Error);
 
   if (!Best) {
-    Error = "No available targets are compatible with this module";
-    return Best;
+    Error = "No available targets are compatible with this triple";
+    return 0;
   }
 
   // Otherwise, take the best target, but make sure we don't have two equally
@@ -103,41 +70,6 @@ TargetRegistry::getClosestStaticTargetForModule(const Module &M,
   return Best;
 }
 
-const Target *
-TargetRegistry::getClosestTargetForJIT(std::string &Error) {
-  std::string Triple = sys::getHostTriple();
-
-  // Provide special warning when no targets are initialized.
-  if (begin() == end()) {
-    Error = "No JIT is available for this host (no targets are registered)";
-    return 0;
-  }
-
-  const Target *Best = 0, *EquallyBest = 0;
-  unsigned BestQuality = 0;
-  for (iterator it = begin(), ie = end(); it != ie; ++it) {
-    if (!it->hasJIT())
-      continue;
-
-    if (unsigned Qual = it->TripleMatchQualityFn(Triple)) {
-      if (!Best || Qual > BestQuality) {
-        Best = &*it;
-        EquallyBest = 0;
-        BestQuality = Qual;
-      } else if (Qual == BestQuality)
-        EquallyBest = &*it;
-    }
-  }
-
-  if (!Best) {
-    Error = "No JIT is available for this host";
-    return 0;
-  }
-
-  // Return the best, ignoring ties.
-  return Best;
-}
-
 void TargetRegistry::RegisterTarget(Target &T,
                                     const char *Name,
                                     const char *ShortDesc,
index c2681e4f5052525bb806ba2ee5865e496903aa4a..d110684c472d8bd2d700e82b98132d83f01e2969 100644 (file)
@@ -3185,7 +3185,10 @@ std::string CWriter::InterpretASMConstraint(InlineAsm::ConstraintInfo& c) {
   if (!TAsm) {
     std::string E;
     const Target *Match =
-      TargetRegistry::getClosestStaticTargetForModule(*TheModule, E);
+      TargetRegistry::lookupTarget(TheModule->getTargetTriple(), 
+                                   /*FallbackToHost=*/true,
+                                   /*RequireJIT=*/false,
+                                   E);
     if (Match) {
       // Per platform Target Machines don't exist, so create it;
       // this must be done only once.
index 6d662680bfb69722b84e253c58665c06e8a77df1..9f7f0a43f1500d5d913068609e9f0fd579c6ded1 100644 (file)
@@ -252,7 +252,10 @@ int main(int argc, char **argv) {
     }        
   } else {
     std::string Err;
-    TheTarget = TargetRegistry::getClosestStaticTargetForModule(mod, Err);
+    TheTarget = TargetRegistry::lookupTarget(mod.getTargetTriple(), 
+                                             /*FallbackToHost=*/true,
+                                             /*RequireJIT=*/false,
+                                             Err);
     if (TheTarget == 0) {
       errs() << argv[0] << ": error auto-selecting target for module '"
              << Err << "'.  Please use the -march option to explicitly "
index ffc9b559f0756557d99750767044dc70f5b03f19..5212ab1936eb4ca214788be507d18d3027088b6c 100644 (file)
@@ -147,7 +147,10 @@ static int AssembleInput(const char *ProgName) {
   // Get the target specific parser.
   std::string Error;
   const Target *TheTarget =
-    TargetRegistry::getClosestStaticTargetForTriple(Triple, Error);
+    TargetRegistry::lookupTarget(Triple, 
+                                 /*FallbackToHost=*/true,
+                                 /*RequireJIT=*/false,
+                                 Error);
   if (TheTarget == 0) {
     errs() << ProgName << ": error: unable to get target for '" << Triple
            << "', see --version and --triple.\n";
index 93689e3528c1879879090f21372f4f5648a42724..a264e73904f9cfe2b5898896036657d76981b123 100644 (file)
@@ -330,8 +330,10 @@ bool LTOCodeGenerator::determineTarget(std::string& errMsg)
         // create target machine from info for merged modules
         Module* mergedModule = _linker.getModule();
         const Target *march = 
-          TargetRegistry::getClosestStaticTargetForModule(*mergedModule, 
-                                                          errMsg);
+          TargetRegistry::lookupTarget(mergedModule->getTargetTriple(), 
+                                       /*FallbackToHost=*/true,
+                                       /*RequireJIT=*/false,
+                                       errMsg);
         if ( march == NULL )
             return true;
 
index a72938a43b0fd5c91496c56275a0efe6ee0a1380..83dda0cdc6f19ae0ec1dfb46247f1caab4823907 100644 (file)
@@ -145,9 +145,10 @@ LTOModule* LTOModule::makeLTOModule(MemoryBuffer* buffer,
     if ( !m )
         return NULL;
     // find machine architecture for this module
-    const Target* march = 
-      TargetRegistry::getClosestStaticTargetForModule(*m, errMsg);
-
+    const Target* march = TargetRegistry::lookupTarget(m->getTargetTriple(), 
+                                                       /*FallbackToHost=*/true,
+                                                       /*RequireJIT=*/false,
+                                                       errMsg);
     if ( march == NULL ) 
         return NULL;