llvm.org GIT mirror llvm / 3c2d4bf
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 Daniel Dunbar 10 years ago
11 changed file(s) with 148 addition(s) and 70 deletion(s). Raw diff Collapse all Expand all
9494 /// @{
9595
9696 Triple() : Data(""), Arch(InvalidArch) {}
97 explicit Triple(const char *Str) : Data(Str), Arch(InvalidArch) {}
97 explicit Triple(const StringRef &Str) : Data(Str), Arch(InvalidArch) {}
9898 explicit Triple(const char *ArchStr, const char *VendorStr, const char *OSStr)
9999 : Data(ArchStr), Arch(InvalidArch) {
100100 Data += '-';
211211 /// getOSTypeName - Get the canonical name for the \arg Kind vendor.
212212 static const char *getOSTypeName(OSType Kind);
213213
214 /// getArchTypeForLLVMName - The canonical type for the given LLVM
215 /// architecture name (e.g., "x86").
216 static ArchType getArchTypeForLLVMName(const StringRef &Str);
217
214218 /// @}
215219 };
216220
5050
5151 typedef TargetMachine *(*TargetMachineCtorTy)(const Target &,
5252 const Module &,
53 const std::string &,
5354 const std::string &);
5455 typedef FunctionPass *(*AsmPrinterCtorTy)(formatted_raw_ostream &,
5556 TargetMachine &,
109110 /// hasAsmParser - Check if this target supports .s parsing.
110111 bool hasAsmParser() const { return AsmParserCtorFn != 0; }
111112
112 /// createTargetMachine - Create a target specific machine implementation.
113 /// createTargetMachine - Create a target specific machine implementation
114 /// for the module \arg M and \arg Triple.
115 ///
116 /// \arg M - This argument is used for some machines to access the target
117 /// data.
118 /// \arg Triple - This argument is used to determine the target machine
119 /// feature set; it should always be provided. Generally this should be
120 /// either the target triple from the module, or the target triple of the
121 /// host if that does not exist.
113122 TargetMachine *createTargetMachine(const Module &M,
123 const std::string &Triple,
114124 const std::string &Features) const {
115125 if (!TargetMachineCtorFn)
116126 return 0;
117 return TargetMachineCtorFn(*this, M, Features);
127 return TargetMachineCtorFn(*this, M, Triple, Features);
118128 }
119129
120130 /// createAsmPrinter - Create a target specific assembly printer pass.
324334
325335 private:
326336 static TargetMachine *Allocator(const Target &T, const Module &M,
337 const std::string &TT,
327338 const std::string &FS) {
328 return new TargetMachineImpl(T, M.getTargetTriple(), FS);
339 return new TargetMachineImpl(T, TT, FS);
329340 }
330341 };
331342
337348
338349 private:
339350 static TargetMachine *Allocator(const Target &T, const Module &M,
351 const std::string &TT,
340352 const std::string &FS) {
341353 return new TargetMachineImpl(T, M, FS);
342354 }
1515 #include "JIT.h"
1616 #include "llvm/Module.h"
1717 #include "llvm/ModuleProvider.h"
18 #include "llvm/ADT/Triple.h"
1819 #include "llvm/Support/CommandLine.h"
19 #include "llvm/Support/Streams.h"
20 #include "llvm/Support/raw_ostream.h"
21 #include "llvm/System/Host.h"
2022 #include "llvm/Target/SubtargetFeature.h"
2123 #include "llvm/Target/TargetMachine.h"
2224 #include "llvm/Target/TargetRegistry.h"
4042 /// selectTarget - Pick a target either via -march or by guessing the native
4143 /// arch. Add any CPU features specified via -mcpu or -mattr.
4244 TargetMachine *JIT::selectTarget(ModuleProvider *MP, std::string *ErrorStr) {
43 const Target *TheTarget = 0;
44 if (MArch.empty()) {
45 std::string Error;
46 TheTarget = TargetRegistry::getClosestTargetForJIT(Error);
47 if (TheTarget == 0) {
48 if (ErrorStr)
49 *ErrorStr = Error;
50 return 0;
51 }
52 } else {
53 for (TargetRegistry::iterator it = TargetRegistry::begin(),
54 ie = TargetRegistry::end(); it != ie; ++it) {
55 if (MArch == it->getName()) {
56 TheTarget = &*it;
57 break;
58 }
59 }
60
61 if (TheTarget == 0) {
62 if (ErrorStr)
63 *ErrorStr = std::string("invalid target '" + MArch + "'.\n");
64 return 0;
65 }
45 Triple TheTriple(sys::getHostTriple());
6646
67 if (!TheTarget->hasJIT()) {
68 cerr << "WARNING: This target JIT is not designed for the host you are"
47 // Adjust the triple to match what the user requested.
48 if (!MArch.empty())
49 TheTriple.setArch(Triple::getArchTypeForLLVMName(MArch));
50
51 std::string Error;
52 const Target *TheTarget =
53 TargetRegistry::lookupTarget(TheTriple.getTriple(),
54 /*FallbackToHost=*/false,
55 /*RequireJIT=*/false,
56 Error);
57 if (TheTarget == 0) {
58 if (ErrorStr)
59 *ErrorStr = Error;
60 return 0;
61 }
62
63 if (!TheTarget->hasJIT()) {
64 errs() << "WARNING: This target JIT is not designed for the host you are"
6965 << " running. If bad things happen, please choose a different "
7066 << "-march switch.\n";
71 }
7267 }
7368
7469 // Package up features to be passed to target/subtarget
8378
8479 // Allocate a target...
8580 TargetMachine *Target =
86 TheTarget->createTargetMachine(*MP->getModule(), FeaturesStr);
81 TheTarget->createTargetMachine(*MP->getModule(), TheTriple.getTriple(),
82 FeaturesStr);
8783 assert(Target && "Could not allocate target machine!");
8884 return Target;
8985 }
6868 }
6969
7070 return "";
71 }
72
73 Triple::ArchType Triple::getArchTypeForLLVMName(const StringRef &Name) {
74 if (Name == "alpha")
75 return alpha;
76 if (Name == "arm")
77 return arm;
78 if (Name == "bfin")
79 return bfin;
80 if (Name == "cellspu")
81 return cellspu;
82 if (Name == "mips")
83 return mips;
84 if (Name == "mipsel")
85 return mipsel;
86 if (Name == "msp430")
87 return msp430;
88 if (Name == "ppc64")
89 return ppc64;
90 if (Name == "ppc")
91 return ppc;
92 if (Name == "sparc")
93 return sparc;
94 if (Name == "systemz")
95 return systemz;
96 if (Name == "thumb")
97 return thumb;
98 if (Name == "x86")
99 return x86;
100 if (Name == "x86_64")
101 return x86_64;
102 if (Name == "xcore")
103 return xcore;
104
105 return UnknownArch;
71106 }
72107
73108 //
2323 #include "llvm/Intrinsics.h"
2424 #include "llvm/IntrinsicInst.h"
2525 #include "llvm/InlineAsm.h"
26 #include "llvm/ADT/StringExtras.h"
27 #include "llvm/ADT/STLExtras.h"
2628 #include "llvm/Analysis/ConstantsScanner.h"
2729 #include "llvm/Analysis/FindUsedTypes.h"
2830 #include "llvm/Analysis/LoopInfo.h"
4042 #include "llvm/Support/InstVisitor.h"
4143 #include "llvm/Support/Mangler.h"
4244 #include "llvm/Support/MathExtras.h"
43 #include "llvm/ADT/StringExtras.h"
44 #include "llvm/ADT/STLExtras.h"
45 #include "llvm/Support/MathExtras.h"
45 #include "llvm/System/Host.h"
4646 #include "llvm/Config/config.h"
4747 #include
4848 #include
31803180
31813181 // Grab the translation table from TargetAsmInfo if it exists.
31823182 if (!TAsm) {
3183 std::string Triple = TheModule->getTargetTriple();
3184 if (Triple.empty())
3185 Triple = llvm::sys::getHostTriple();
3186
31833187 std::string E;
31843188 const Target *Match =
3185 TargetRegistry::lookupTarget(TheModule->getTargetTriple(),
3186 /*FallbackToHost=*/true,
3189 TargetRegistry::lookupTarget(Triple,
3190 /*FallbackToHost=*/false,
31873191 /*RequireJIT=*/false,
31883192 E);
31893193 if (Match) {
31903194 // Per platform Target Machines don't exist, so create it;
31913195 // this must be done only once.
3192 const TargetMachine* TM = Match->createTargetMachine(*TheModule, "");
3196 const TargetMachine* TM = Match->createTargetMachine(*TheModule, Triple,
3197 "");
31933198 TAsm = TM->getTargetAsmInfo();
31943199 }
31953200 }
None ; RUN: llvm-as < %s | llc -march=mips | grep wsbw | count 1
0 ; RUN: llvm-as < %s | llc | grep wsbw | count 1
11
22 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"
33 target triple = "psp"
None ; RUN: llvm-as < %s | llc -march=x86 -mtriple=mingw32 | \
0 ; RUN: llvm-as < %s | llc -mtriple=i386-unknown-mingw32 | \
11 ; RUN: grep {@12}
22
33 ; Check that a fastcall function gets correct mangling
None ; RUN: llvm-as < %s | llc -march=x86 | not grep rep
1 ; RUN: llvm-as < %s | llc -march=x86 | grep memset
0 ; RUN: llvm-as < %s | llc | not grep rep
1 ; RUN: llvm-as < %s | llc | grep memset
2
3 target triple = "i386"
24
35 declare void @llvm.memset.i32(i8*, i8, i32, i32) nounwind
46
1212 //
1313 //===----------------------------------------------------------------------===//
1414
15 #include "llvm/Bitcode/ReaderWriter.h"
16 #include "llvm/CodeGen/FileWriters.h"
17 #include "llvm/CodeGen/LinkAllCodegenComponents.h"
18 #include "llvm/CodeGen/LinkAllAsmWriterComponents.h"
19 #include "llvm/CodeGen/ObjectCodeEmitter.h"
20 #include "llvm/Target/SubtargetFeature.h"
21 #include "llvm/Target/TargetData.h"
22 #include "llvm/Target/TargetMachine.h"
23 #include "llvm/Target/TargetRegistry.h"
24 #include "llvm/Transforms/Scalar.h"
2515 #include "llvm/LLVMContext.h"
2616 #include "llvm/Module.h"
2717 #include "llvm/ModuleProvider.h"
2818 #include "llvm/PassManager.h"
2919 #include "llvm/Pass.h"
20 #include "llvm/ADT/Triple.h"
21 #include "llvm/Analysis/Verifier.h"
22 #include "llvm/Bitcode/ReaderWriter.h"
23 #include "llvm/CodeGen/FileWriters.h"
24 #include "llvm/CodeGen/LinkAllAsmWriterComponents.h"
25 #include "llvm/CodeGen/LinkAllCodegenComponents.h"
26 #include "llvm/CodeGen/ObjectCodeEmitter.h"
27 #include "llvm/Config/config.h"
28 #include "llvm/LinkAllVMCore.h"
3029 #include "llvm/Support/CommandLine.h"
3130 #include "llvm/Support/FileUtilities.h"
3231 #include "llvm/Support/FormattedStream.h"
3433 #include "llvm/Support/MemoryBuffer.h"
3534 #include "llvm/Support/PluginLoader.h"
3635 #include "llvm/Support/PrettyStackTrace.h"
37 #include "llvm/Analysis/Verifier.h"
36 #include "llvm/System/Host.h"
3837 #include "llvm/System/Signals.h"
39 #include "llvm/Config/config.h"
40 #include "llvm/LinkAllVMCore.h"
38 #include "llvm/Target/SubtargetFeature.h"
39 #include "llvm/Target/TargetData.h"
40 #include "llvm/Target/TargetMachine.h"
41 #include "llvm/Target/TargetRegistry.h"
4142 #include "llvm/Target/TargetSelect.h"
43 #include "llvm/Transforms/Scalar.h"
4244 #include
4345 using namespace llvm;
4446
233235 if (!TargetTriple.empty())
234236 mod.setTargetTriple(TargetTriple);
235237
236 // Allocate target machine. First, check whether the user has
237 // explicitly specified an architecture to compile for.
238 Triple TheTriple(mod.getTargetTriple());
239 if (TheTriple.getTriple().empty())
240 TheTriple.setTriple(sys::getHostTriple());
241
242 // Allocate target machine. First, check whether the user has explicitly
243 // specified an architecture to compile for. If so we have to look it up by
244 // name, because it might be a backend that has no mapping to a target triple.
238245 const Target *TheTarget = 0;
239246 if (!MArch.empty()) {
240247 for (TargetRegistry::iterator it = TargetRegistry::begin(),
248255 if (!TheTarget) {
249256 errs() << argv[0] << ": error: invalid target '" << MArch << "'.\n";
250257 return 1;
251 }
258 }
259
260 // Adjust the triple to match (if known), otherwise stick with the
261 // module/host triple.
262 Triple::ArchType Type = Triple::getArchTypeForLLVMName(MArch);
263 if (Type != Triple::UnknownArch)
264 TheTriple.setArch(Type);
252265 } else {
253266 std::string Err;
254 TheTarget = TargetRegistry::lookupTarget(mod.getTargetTriple(),
255 /*FallbackToHost=*/true,
267 TheTarget = TargetRegistry::lookupTarget(TheTriple.getTriple(),
268 /*FallbackToHost=*/false,
256269 /*RequireJIT=*/false,
257270 Err);
258271 if (TheTarget == 0) {
274287 }
275288
276289 std::auto_ptr
277 target(TheTarget->createTargetMachine(mod, FeaturesStr));
290 target(TheTarget->createTargetMachine(mod, TheTriple.getTriple(),
291 FeaturesStr));
278292 assert(target.get() && "Could not allocate target machine!");
279293 TargetMachine &Target = *target.get();
280294
3434 #include "llvm/Support/MemoryBuffer.h"
3535 #include "llvm/Support/StandardPasses.h"
3636 #include "llvm/Support/SystemUtils.h"
37 #include "llvm/System/Host.h"
3738 #include "llvm/System/Signals.h"
3839 #include "llvm/Target/SubtargetFeature.h"
3940 #include "llvm/Target/TargetOptions.h"
325326 bool LTOCodeGenerator::determineTarget(std::string& errMsg)
326327 {
327328 if ( _target == NULL ) {
329 std::string Triple = _linker.getModule()->getTargetTriple();
330 if (Triple.empty())
331 Triple = sys::getHostTriple();
332
328333 // create target machine from info for merged modules
329334 Module* mergedModule = _linker.getModule();
330335 const Target *march =
331 TargetRegistry::lookupTarget(mergedModule->getTargetTriple(),
332 /*FallbackToHost=*/true,
336 TargetRegistry::lookupTarget(Triple,
337 /*FallbackToHost=*/false,
333338 /*RequireJIT=*/false,
334339 errMsg);
335340 if ( march == NULL )
350355 }
351356
352357 // construct LTModule, hand over ownership of module and target
353 std::string FeatureStr =
354 getFeatureString(_linker.getModule()->getTargetTriple().c_str());
355 _target = march->createTargetMachine(*mergedModule, FeatureStr.c_str());
358 std::string FeatureStr = getFeatureString(Triple.c_str());
359 _target = march->createTargetMachine(*mergedModule, Triple, FeatureStr);
356360 }
357361 return false;
358362 }
2323 #include "llvm/Support/Mangler.h"
2424 #include "llvm/Support/MemoryBuffer.h"
2525 #include "llvm/Support/MathExtras.h"
26 #include "llvm/System/Host.h"
2627 #include "llvm/System/Path.h"
2728 #include "llvm/System/Process.h"
2829 #include "llvm/Target/SubtargetFeature.h"
148149 OwningPtr m(ParseBitcodeFile(buffer, getGlobalContext(), &errMsg));
149150 if ( !m )
150151 return NULL;
152
153 std::string Triple = m->getTargetTriple();
154 if (Triple.empty())
155 Triple = sys::getHostTriple();
156
151157 // find machine architecture for this module
152 const Target* march = TargetRegistry::lookupTarget(m->getTargetTriple(),
153 /*FallbackToHost=*/true,
158 const Target* march = TargetRegistry::lookupTarget(Triple,
159 /*FallbackToHost=*/false,
154160 /*RequireJIT=*/false,
155161 errMsg);
156162 if ( march == NULL )
157163 return NULL;
158164
159165 // construct LTModule, hand over ownership of module and target
160 std::string FeatureStr = getFeatureString(m->getTargetTriple().c_str());
161 TargetMachine* target = march->createTargetMachine(*m, FeatureStr);
166 std::string FeatureStr = getFeatureString(Triple.c_str());
167 TargetMachine* target = march->createTargetMachine(*m, Triple, FeatureStr);
162168 return new LTOModule(m.take(), target);
163169 }
164170