1 //===-- TargetSelect.cpp - Target Chooser Code ----------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This just asks the TargetRegistry for the appropriate target to use, and
10 // allows the user to specify a specific one on the commandline with -march=x,
11 // -mcpu=y, and -mattr=a,-b,+c. Clients should initialize targets prior to
12 // calling selectTarget().
14 //===----------------------------------------------------------------------===//
16 #include "llvm/ADT/Triple.h"
17 #include "llvm/ExecutionEngine/ExecutionEngine.h"
18 #include "llvm/IR/Module.h"
19 #include "llvm/MC/SubtargetFeature.h"
20 #include "llvm/Support/Host.h"
21 #include "llvm/Support/TargetRegistry.h"
22 #include "llvm/Target/TargetMachine.h"
26 TargetMachine
*EngineBuilder::selectTarget() {
29 // MCJIT can generate code for remote targets, but the old JIT and Interpreter
30 // must use the host architecture.
31 if (WhichEngine
!= EngineKind::Interpreter
&& M
)
32 TT
.setTriple(M
->getTargetTriple());
34 return selectTarget(TT
, MArch
, MCPU
, MAttrs
);
37 /// selectTarget - Pick a target either via -march or by guessing the native
38 /// arch. Add any CPU features specified via -mcpu or -mattr.
39 TargetMachine
*EngineBuilder::selectTarget(const Triple
&TargetTriple
,
42 const SmallVectorImpl
<std::string
>& MAttrs
) {
43 Triple
TheTriple(TargetTriple
);
44 if (TheTriple
.getTriple().empty())
45 TheTriple
.setTriple(sys::getProcessTriple());
47 // Adjust the triple to match what the user requested.
48 const Target
*TheTarget
= nullptr;
50 auto I
= find_if(TargetRegistry::targets(),
51 [&](const Target
&T
) { return MArch
== T
.getName(); });
53 if (I
== TargetRegistry::targets().end()) {
55 *ErrorStr
= "No available targets are compatible with this -march, "
56 "see -version for the available targets.\n";
62 // Adjust the triple to match (if known), otherwise stick with the
63 // requested/host triple.
64 Triple::ArchType Type
= Triple::getArchTypeForLLVMName(MArch
);
65 if (Type
!= Triple::UnknownArch
)
66 TheTriple
.setArch(Type
);
69 TheTarget
= TargetRegistry::lookupTarget(TheTriple
.getTriple(), Error
);
77 // Package up features to be passed to target/subtarget
78 std::string FeaturesStr
;
79 if (!MAttrs
.empty()) {
80 SubtargetFeatures Features
;
81 for (unsigned i
= 0; i
!= MAttrs
.size(); ++i
)
82 Features
.AddFeature(MAttrs
[i
]);
83 FeaturesStr
= Features
.getString();
86 // FIXME: non-iOS ARM FastISel is broken with MCJIT.
87 if (TheTriple
.getArch() == Triple::arm
&&
89 OptLevel
== CodeGenOpt::None
) {
90 OptLevel
= CodeGenOpt::Less
;
93 // Allocate a target...
94 TargetMachine
*Target
=
95 TheTarget
->createTargetMachine(TheTriple
.getTriple(), MCPU
, FeaturesStr
,
96 Options
, RelocModel
, CMModel
, OptLevel
,
98 Target
->Options
.EmulatedTLS
= EmulatedTLS
;
99 Target
->Options
.ExplicitEmulatedTLS
= true;
101 assert(Target
&& "Could not allocate target machine!");