[llvm-commits] [llvm] r77946 - in /llvm/trunk: include/llvm/ADT/Triple.h include/llvm/Target/TargetRegistry.h lib/ExecutionEngine/JIT/TargetSelect.cpp lib/Support/Triple.cpp lib/Target/CBackend/CBackend.cpp test/CodeGen/Blackfin/ test/CodeGen/Mips/2008-08-08-bswap.ll test/CodeGen/X86/fastcall-correct-mangling.ll test/CodeGen/X86/memset-2.ll tools/llc/llc.cpp tools/lto/LTOCodeGenerator.cpp tools/lto/LTOModule.cpp
Daniel Dunbar
daniel at zuster.org
Sun Aug 2 21:04:07 PDT 2009
Author: ddunbar
Date: Sun Aug 2 23:03:51 2009
New Revision: 77946
URL: http://llvm.org/viewvc/llvm-project?rev=77946&view=rev
Log:
Pass target triple string in to TargetMachine constructor.
This is not just a matter of passing in the target triple from the module;
currently backends are making decisions based on the build and host
architecture. The goal is to migrate to making these decisions based off of the
triple (in conjunction with the feature string). Thus most clients pass in the
target triple, or the host triple if that is empty.
This has one important change in the way behavior of the JIT and llc.
For the JIT, it was previously selecting the Target based on the host
(naturally), but it was setting the target machine features based on the triple
from the module. Now it is setting the target machine features based on the
triple of the host.
For LLC, -march was previously only used to select the target, the target
machine features were initialized from the module's triple (which may have been
empty). Now the target triple is taken from the module, or the host's triple is
used if that is empty. Then the triple is adjusted to match -march.
The take away is that -march for llc is now used in conjunction with the host
triple to initialize the subtarget. If users want more deterministic behavior
from llc, they should use -mtriple, or set the triple in the input module.
Modified:
llvm/trunk/include/llvm/ADT/Triple.h
llvm/trunk/include/llvm/Target/TargetRegistry.h
llvm/trunk/lib/ExecutionEngine/JIT/TargetSelect.cpp
llvm/trunk/lib/Support/Triple.cpp
llvm/trunk/lib/Target/CBackend/CBackend.cpp
llvm/trunk/test/CodeGen/Blackfin/ (props changed)
llvm/trunk/test/CodeGen/Mips/2008-08-08-bswap.ll
llvm/trunk/test/CodeGen/X86/fastcall-correct-mangling.ll
llvm/trunk/test/CodeGen/X86/memset-2.ll
llvm/trunk/tools/llc/llc.cpp
llvm/trunk/tools/lto/LTOCodeGenerator.cpp
llvm/trunk/tools/lto/LTOModule.cpp
Modified: llvm/trunk/include/llvm/ADT/Triple.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ADT/Triple.h?rev=77946&r1=77945&r2=77946&view=diff
==============================================================================
--- llvm/trunk/include/llvm/ADT/Triple.h (original)
+++ llvm/trunk/include/llvm/ADT/Triple.h Sun Aug 2 23:03:51 2009
@@ -95,7 +95,7 @@
/// @{
Triple() : Data(""), Arch(InvalidArch) {}
- explicit Triple(const char *Str) : Data(Str), Arch(InvalidArch) {}
+ explicit Triple(const StringRef &Str) : Data(Str), Arch(InvalidArch) {}
explicit Triple(const char *ArchStr, const char *VendorStr, const char *OSStr)
: Data(ArchStr), Arch(InvalidArch) {
Data += '-';
@@ -212,6 +212,10 @@
/// getOSTypeName - Get the canonical name for the \arg Kind vendor.
static const char *getOSTypeName(OSType Kind);
+ /// getArchTypeForLLVMName - The canonical type for the given LLVM
+ /// architecture name (e.g., "x86").
+ static ArchType getArchTypeForLLVMName(const StringRef &Str);
+
/// @}
};
Modified: llvm/trunk/include/llvm/Target/TargetRegistry.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Target/TargetRegistry.h?rev=77946&r1=77945&r2=77946&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Target/TargetRegistry.h (original)
+++ llvm/trunk/include/llvm/Target/TargetRegistry.h Sun Aug 2 23:03:51 2009
@@ -51,6 +51,7 @@
typedef TargetMachine *(*TargetMachineCtorTy)(const Target &,
const Module &,
+ const std::string &,
const std::string &);
typedef FunctionPass *(*AsmPrinterCtorTy)(formatted_raw_ostream &,
TargetMachine &,
@@ -110,12 +111,21 @@
/// hasAsmParser - Check if this target supports .s parsing.
bool hasAsmParser() const { return AsmParserCtorFn != 0; }
- /// createTargetMachine - Create a target specific machine implementation.
+ /// createTargetMachine - Create a target specific machine implementation
+ /// for the module \arg M and \arg Triple.
+ ///
+ /// \arg M - This argument is used for some machines to access the target
+ /// data.
+ /// \arg Triple - This argument is used to determine the target machine
+ /// feature set; it should always be provided. Generally this should be
+ /// either the target triple from the module, or the target triple of the
+ /// host if that does not exist.
TargetMachine *createTargetMachine(const Module &M,
+ const std::string &Triple,
const std::string &Features) const {
if (!TargetMachineCtorFn)
return 0;
- return TargetMachineCtorFn(*this, M, Features);
+ return TargetMachineCtorFn(*this, M, Triple, Features);
}
/// createAsmPrinter - Create a target specific assembly printer pass.
@@ -325,8 +335,9 @@
private:
static TargetMachine *Allocator(const Target &T, const Module &M,
+ const std::string &TT,
const std::string &FS) {
- return new TargetMachineImpl(T, M.getTargetTriple(), FS);
+ return new TargetMachineImpl(T, TT, FS);
}
};
@@ -338,6 +349,7 @@
private:
static TargetMachine *Allocator(const Target &T, const Module &M,
+ const std::string &TT,
const std::string &FS) {
return new TargetMachineImpl(T, M, FS);
}
Modified: llvm/trunk/lib/ExecutionEngine/JIT/TargetSelect.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/ExecutionEngine/JIT/TargetSelect.cpp?rev=77946&r1=77945&r2=77946&view=diff
==============================================================================
--- llvm/trunk/lib/ExecutionEngine/JIT/TargetSelect.cpp (original)
+++ llvm/trunk/lib/ExecutionEngine/JIT/TargetSelect.cpp Sun Aug 2 23:03:51 2009
@@ -16,8 +16,10 @@
#include "JIT.h"
#include "llvm/Module.h"
#include "llvm/ModuleProvider.h"
+#include "llvm/ADT/Triple.h"
#include "llvm/Support/CommandLine.h"
-#include "llvm/Support/Streams.h"
+#include "llvm/Support/raw_ostream.h"
+#include "llvm/System/Host.h"
#include "llvm/Target/SubtargetFeature.h"
#include "llvm/Target/TargetMachine.h"
#include "llvm/Target/TargetRegistry.h"
@@ -41,35 +43,28 @@
/// selectTarget - Pick a target either via -march or by guessing the native
/// arch. Add any CPU features specified via -mcpu or -mattr.
TargetMachine *JIT::selectTarget(ModuleProvider *MP, std::string *ErrorStr) {
- const Target *TheTarget = 0;
- if (MArch.empty()) {
- std::string Error;
- TheTarget = TargetRegistry::getClosestTargetForJIT(Error);
- if (TheTarget == 0) {
- if (ErrorStr)
- *ErrorStr = Error;
- return 0;
- }
- } else {
- for (TargetRegistry::iterator it = TargetRegistry::begin(),
- ie = TargetRegistry::end(); it != ie; ++it) {
- if (MArch == it->getName()) {
- TheTarget = &*it;
- break;
- }
- }
-
- if (TheTarget == 0) {
- if (ErrorStr)
- *ErrorStr = std::string("invalid target '" + MArch + "'.\n");
- return 0;
- }
+ Triple TheTriple(sys::getHostTriple());
- if (!TheTarget->hasJIT()) {
- cerr << "WARNING: This target JIT is not designed for the host you are"
+ // Adjust the triple to match what the user requested.
+ if (!MArch.empty())
+ TheTriple.setArch(Triple::getArchTypeForLLVMName(MArch));
+
+ std::string Error;
+ const Target *TheTarget =
+ TargetRegistry::lookupTarget(TheTriple.getTriple(),
+ /*FallbackToHost=*/false,
+ /*RequireJIT=*/false,
+ Error);
+ if (TheTarget == 0) {
+ if (ErrorStr)
+ *ErrorStr = Error;
+ return 0;
+ }
+
+ if (!TheTarget->hasJIT()) {
+ errs() << "WARNING: This target JIT is not designed for the host you are"
<< " running. If bad things happen, please choose a different "
<< "-march switch.\n";
- }
}
// Package up features to be passed to target/subtarget
@@ -84,7 +79,8 @@
// Allocate a target...
TargetMachine *Target =
- TheTarget->createTargetMachine(*MP->getModule(), FeaturesStr);
+ TheTarget->createTargetMachine(*MP->getModule(), TheTriple.getTriple(),
+ FeaturesStr);
assert(Target && "Could not allocate target machine!");
return Target;
}
Modified: llvm/trunk/lib/Support/Triple.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/Triple.cpp?rev=77946&r1=77945&r2=77946&view=diff
==============================================================================
--- llvm/trunk/lib/Support/Triple.cpp (original)
+++ llvm/trunk/lib/Support/Triple.cpp Sun Aug 2 23:03:51 2009
@@ -71,6 +71,41 @@
return "<invalid>";
}
+Triple::ArchType Triple::getArchTypeForLLVMName(const StringRef &Name) {
+ if (Name == "alpha")
+ return alpha;
+ if (Name == "arm")
+ return arm;
+ if (Name == "bfin")
+ return bfin;
+ if (Name == "cellspu")
+ return cellspu;
+ if (Name == "mips")
+ return mips;
+ if (Name == "mipsel")
+ return mipsel;
+ if (Name == "msp430")
+ return msp430;
+ if (Name == "ppc64")
+ return ppc64;
+ if (Name == "ppc")
+ return ppc;
+ if (Name == "sparc")
+ return sparc;
+ if (Name == "systemz")
+ return systemz;
+ if (Name == "thumb")
+ return thumb;
+ if (Name == "x86")
+ return x86;
+ if (Name == "x86_64")
+ return x86_64;
+ if (Name == "xcore")
+ return xcore;
+
+ return UnknownArch;
+}
+
//
void Triple::Parse() const {
Modified: llvm/trunk/lib/Target/CBackend/CBackend.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Target/CBackend/CBackend.cpp?rev=77946&r1=77945&r2=77946&view=diff
==============================================================================
--- llvm/trunk/lib/Target/CBackend/CBackend.cpp (original)
+++ llvm/trunk/lib/Target/CBackend/CBackend.cpp Sun Aug 2 23:03:51 2009
@@ -24,6 +24,8 @@
#include "llvm/Intrinsics.h"
#include "llvm/IntrinsicInst.h"
#include "llvm/InlineAsm.h"
+#include "llvm/ADT/StringExtras.h"
+#include "llvm/ADT/STLExtras.h"
#include "llvm/Analysis/ConstantsScanner.h"
#include "llvm/Analysis/FindUsedTypes.h"
#include "llvm/Analysis/LoopInfo.h"
@@ -41,9 +43,7 @@
#include "llvm/Support/InstVisitor.h"
#include "llvm/Support/Mangler.h"
#include "llvm/Support/MathExtras.h"
-#include "llvm/ADT/StringExtras.h"
-#include "llvm/ADT/STLExtras.h"
-#include "llvm/Support/MathExtras.h"
+#include "llvm/System/Host.h"
#include "llvm/Config/config.h"
#include <algorithm>
#include <sstream>
@@ -3181,16 +3181,21 @@
// Grab the translation table from TargetAsmInfo if it exists.
if (!TAsm) {
+ std::string Triple = TheModule->getTargetTriple();
+ if (Triple.empty())
+ Triple = llvm::sys::getHostTriple();
+
std::string E;
const Target *Match =
- TargetRegistry::lookupTarget(TheModule->getTargetTriple(),
- /*FallbackToHost=*/true,
+ TargetRegistry::lookupTarget(Triple,
+ /*FallbackToHost=*/false,
/*RequireJIT=*/false,
E);
if (Match) {
// Per platform Target Machines don't exist, so create it;
// this must be done only once.
- const TargetMachine* TM = Match->createTargetMachine(*TheModule, "");
+ const TargetMachine* TM = Match->createTargetMachine(*TheModule, Triple,
+ "");
TAsm = TM->getTargetAsmInfo();
}
}
Propchange: llvm/trunk/test/CodeGen/Blackfin/
------------------------------------------------------------------------------
--- svn:ignore (added)
+++ svn:ignore Sun Aug 2 23:03:51 2009
@@ -0,0 +1 @@
+Output
Modified: llvm/trunk/test/CodeGen/Mips/2008-08-08-bswap.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/CodeGen/Mips/2008-08-08-bswap.ll?rev=77946&r1=77945&r2=77946&view=diff
==============================================================================
--- llvm/trunk/test/CodeGen/Mips/2008-08-08-bswap.ll (original)
+++ llvm/trunk/test/CodeGen/Mips/2008-08-08-bswap.ll Sun Aug 2 23:03:51 2009
@@ -1,4 +1,4 @@
-; RUN: llvm-as < %s | llc -march=mips | grep wsbw | count 1
+; RUN: llvm-as < %s | llc | grep wsbw | count 1
target datalayout = "e-p:32:32:32-i1:8:8-i8:8:32-i16:16:32-i32:32:32-i64:32:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64"
target triple = "psp"
Modified: llvm/trunk/test/CodeGen/X86/fastcall-correct-mangling.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/CodeGen/X86/fastcall-correct-mangling.ll?rev=77946&r1=77945&r2=77946&view=diff
==============================================================================
--- llvm/trunk/test/CodeGen/X86/fastcall-correct-mangling.ll (original)
+++ llvm/trunk/test/CodeGen/X86/fastcall-correct-mangling.ll Sun Aug 2 23:03:51 2009
@@ -1,4 +1,4 @@
-; RUN: llvm-as < %s | llc -march=x86 -mtriple=mingw32 | \
+; RUN: llvm-as < %s | llc -mtriple=i386-unknown-mingw32 | \
; RUN: grep {@12}
; Check that a fastcall function gets correct mangling
Modified: llvm/trunk/test/CodeGen/X86/memset-2.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/CodeGen/X86/memset-2.ll?rev=77946&r1=77945&r2=77946&view=diff
==============================================================================
--- llvm/trunk/test/CodeGen/X86/memset-2.ll (original)
+++ llvm/trunk/test/CodeGen/X86/memset-2.ll Sun Aug 2 23:03:51 2009
@@ -1,5 +1,7 @@
-; RUN: llvm-as < %s | llc -march=x86 | not grep rep
-; RUN: llvm-as < %s | llc -march=x86 | grep memset
+; RUN: llvm-as < %s | llc | not grep rep
+; RUN: llvm-as < %s | llc | grep memset
+
+target triple = "i386"
declare void @llvm.memset.i32(i8*, i8, i32, i32) nounwind
Modified: llvm/trunk/tools/llc/llc.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llc/llc.cpp?rev=77946&r1=77945&r2=77946&view=diff
==============================================================================
--- llvm/trunk/tools/llc/llc.cpp (original)
+++ llvm/trunk/tools/llc/llc.cpp Sun Aug 2 23:03:51 2009
@@ -13,21 +13,20 @@
//
//===----------------------------------------------------------------------===//
-#include "llvm/Bitcode/ReaderWriter.h"
-#include "llvm/CodeGen/FileWriters.h"
-#include "llvm/CodeGen/LinkAllCodegenComponents.h"
-#include "llvm/CodeGen/LinkAllAsmWriterComponents.h"
-#include "llvm/CodeGen/ObjectCodeEmitter.h"
-#include "llvm/Target/SubtargetFeature.h"
-#include "llvm/Target/TargetData.h"
-#include "llvm/Target/TargetMachine.h"
-#include "llvm/Target/TargetRegistry.h"
-#include "llvm/Transforms/Scalar.h"
#include "llvm/LLVMContext.h"
#include "llvm/Module.h"
#include "llvm/ModuleProvider.h"
#include "llvm/PassManager.h"
#include "llvm/Pass.h"
+#include "llvm/ADT/Triple.h"
+#include "llvm/Analysis/Verifier.h"
+#include "llvm/Bitcode/ReaderWriter.h"
+#include "llvm/CodeGen/FileWriters.h"
+#include "llvm/CodeGen/LinkAllAsmWriterComponents.h"
+#include "llvm/CodeGen/LinkAllCodegenComponents.h"
+#include "llvm/CodeGen/ObjectCodeEmitter.h"
+#include "llvm/Config/config.h"
+#include "llvm/LinkAllVMCore.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/FileUtilities.h"
#include "llvm/Support/FormattedStream.h"
@@ -35,11 +34,14 @@
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/PluginLoader.h"
#include "llvm/Support/PrettyStackTrace.h"
-#include "llvm/Analysis/Verifier.h"
+#include "llvm/System/Host.h"
#include "llvm/System/Signals.h"
-#include "llvm/Config/config.h"
-#include "llvm/LinkAllVMCore.h"
+#include "llvm/Target/SubtargetFeature.h"
+#include "llvm/Target/TargetData.h"
+#include "llvm/Target/TargetMachine.h"
+#include "llvm/Target/TargetRegistry.h"
#include "llvm/Target/TargetSelect.h"
+#include "llvm/Transforms/Scalar.h"
#include <memory>
using namespace llvm;
@@ -234,8 +236,13 @@
if (!TargetTriple.empty())
mod.setTargetTriple(TargetTriple);
- // Allocate target machine. First, check whether the user has
- // explicitly specified an architecture to compile for.
+ Triple TheTriple(mod.getTargetTriple());
+ if (TheTriple.getTriple().empty())
+ TheTriple.setTriple(sys::getHostTriple());
+
+ // 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.
const Target *TheTarget = 0;
if (!MArch.empty()) {
for (TargetRegistry::iterator it = TargetRegistry::begin(),
@@ -249,11 +256,17 @@
if (!TheTarget) {
errs() << argv[0] << ": error: invalid target '" << MArch << "'.\n";
return 1;
- }
+ }
+
+ // Adjust the triple to match (if known), otherwise stick with the
+ // module/host triple.
+ Triple::ArchType Type = Triple::getArchTypeForLLVMName(MArch);
+ if (Type != Triple::UnknownArch)
+ TheTriple.setArch(Type);
} else {
std::string Err;
- TheTarget = TargetRegistry::lookupTarget(mod.getTargetTriple(),
- /*FallbackToHost=*/true,
+ TheTarget = TargetRegistry::lookupTarget(TheTriple.getTriple(),
+ /*FallbackToHost=*/false,
/*RequireJIT=*/false,
Err);
if (TheTarget == 0) {
@@ -275,7 +288,8 @@
}
std::auto_ptr<TargetMachine>
- target(TheTarget->createTargetMachine(mod, FeaturesStr));
+ target(TheTarget->createTargetMachine(mod, TheTriple.getTriple(),
+ FeaturesStr));
assert(target.get() && "Could not allocate target machine!");
TargetMachine &Target = *target.get();
Modified: llvm/trunk/tools/lto/LTOCodeGenerator.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/lto/LTOCodeGenerator.cpp?rev=77946&r1=77945&r2=77946&view=diff
==============================================================================
--- llvm/trunk/tools/lto/LTOCodeGenerator.cpp (original)
+++ llvm/trunk/tools/lto/LTOCodeGenerator.cpp Sun Aug 2 23:03:51 2009
@@ -35,6 +35,7 @@
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/StandardPasses.h"
#include "llvm/Support/SystemUtils.h"
+#include "llvm/System/Host.h"
#include "llvm/System/Signals.h"
#include "llvm/Target/SubtargetFeature.h"
#include "llvm/Target/TargetOptions.h"
@@ -326,11 +327,15 @@
bool LTOCodeGenerator::determineTarget(std::string& errMsg)
{
if ( _target == NULL ) {
+ std::string Triple = _linker.getModule()->getTargetTriple();
+ if (Triple.empty())
+ Triple = sys::getHostTriple();
+
// create target machine from info for merged modules
Module* mergedModule = _linker.getModule();
const Target *march =
- TargetRegistry::lookupTarget(mergedModule->getTargetTriple(),
- /*FallbackToHost=*/true,
+ TargetRegistry::lookupTarget(Triple,
+ /*FallbackToHost=*/false,
/*RequireJIT=*/false,
errMsg);
if ( march == NULL )
@@ -351,9 +356,8 @@
}
// construct LTModule, hand over ownership of module and target
- std::string FeatureStr =
- getFeatureString(_linker.getModule()->getTargetTriple().c_str());
- _target = march->createTargetMachine(*mergedModule, FeatureStr.c_str());
+ std::string FeatureStr = getFeatureString(Triple.c_str());
+ _target = march->createTargetMachine(*mergedModule, Triple, FeatureStr);
}
return false;
}
Modified: llvm/trunk/tools/lto/LTOModule.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/lto/LTOModule.cpp?rev=77946&r1=77945&r2=77946&view=diff
==============================================================================
--- llvm/trunk/tools/lto/LTOModule.cpp (original)
+++ llvm/trunk/tools/lto/LTOModule.cpp Sun Aug 2 23:03:51 2009
@@ -24,6 +24,7 @@
#include "llvm/Support/Mangler.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/MathExtras.h"
+#include "llvm/System/Host.h"
#include "llvm/System/Path.h"
#include "llvm/System/Process.h"
#include "llvm/Target/SubtargetFeature.h"
@@ -149,17 +150,22 @@
OwningPtr<Module> m(ParseBitcodeFile(buffer, getGlobalContext(), &errMsg));
if ( !m )
return NULL;
+
+ std::string Triple = m->getTargetTriple();
+ if (Triple.empty())
+ Triple = sys::getHostTriple();
+
// find machine architecture for this module
- const Target* march = TargetRegistry::lookupTarget(m->getTargetTriple(),
- /*FallbackToHost=*/true,
+ const Target* march = TargetRegistry::lookupTarget(Triple,
+ /*FallbackToHost=*/false,
/*RequireJIT=*/false,
errMsg);
if ( march == NULL )
return NULL;
// construct LTModule, hand over ownership of module and target
- std::string FeatureStr = getFeatureString(m->getTargetTriple().c_str());
- TargetMachine* target = march->createTargetMachine(*m, FeatureStr);
+ std::string FeatureStr = getFeatureString(Triple.c_str());
+ TargetMachine* target = march->createTargetMachine(*m, Triple, FeatureStr);
return new LTOModule(m.take(), target);
}
More information about the llvm-commits
mailing list