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.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@77946 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/ExecutionEngine/JIT/TargetSelect.cpp b/lib/ExecutionEngine/JIT/TargetSelect.cpp
index 84b745b..55ff441 100644
--- a/lib/ExecutionEngine/JIT/TargetSelect.cpp
+++ b/lib/ExecutionEngine/JIT/TargetSelect.cpp
@@ -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;
}
diff --git a/lib/Support/Triple.cpp b/lib/Support/Triple.cpp
index 391c986..1cdaac0 100644
--- a/lib/Support/Triple.cpp
+++ b/lib/Support/Triple.cpp
@@ -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 {
diff --git a/lib/Target/CBackend/CBackend.cpp b/lib/Target/CBackend/CBackend.cpp
index 84f7c11..c014006 100644
--- a/lib/Target/CBackend/CBackend.cpp
+++ b/lib/Target/CBackend/CBackend.cpp
@@ -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();
}
}