LLVM  14.0.0git
TargetSelect.cpp
Go to the documentation of this file.
1 //===-- TargetSelect.cpp - Target Chooser Code ----------------------------===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 //
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().
13 //
14 //===----------------------------------------------------------------------===//
15 
16 #include "llvm/ADT/Triple.h"
18 #include "llvm/IR/Module.h"
20 #include "llvm/Support/Host.h"
23 
24 using namespace llvm;
25 
27  Triple TT;
28 
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());
33 
34  return selectTarget(TT, MArch, MCPU, MAttrs);
35 }
36 
37 /// selectTarget - Pick a target either via -march or by guessing the native
38 /// arch. Add any CPU features specified via -mcpu or -mattr.
40  StringRef MArch,
41  StringRef MCPU,
42  const SmallVectorImpl<std::string>& MAttrs) {
43  Triple TheTriple(TargetTriple);
44  if (TheTriple.getTriple().empty())
45  TheTriple.setTriple(sys::getProcessTriple());
46 
47  // Adjust the triple to match what the user requested.
48  const Target *TheTarget = nullptr;
49  if (!MArch.empty()) {
51  [&](const Target &T) { return MArch == T.getName(); });
52 
53  if (I == TargetRegistry::targets().end()) {
54  if (ErrorStr)
55  *ErrorStr = "No available targets are compatible with this -march, "
56  "see -version for the available targets.\n";
57  return nullptr;
58  }
59 
60  TheTarget = &*I;
61 
62  // Adjust the triple to match (if known), otherwise stick with the
63  // requested/host triple.
66  TheTriple.setArch(Type);
67  } else {
68  std::string Error;
69  TheTarget = TargetRegistry::lookupTarget(TheTriple.getTriple(), Error);
70  if (!TheTarget) {
71  if (ErrorStr)
72  *ErrorStr = Error;
73  return nullptr;
74  }
75  }
76 
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();
84  }
85 
86  // Allocate a target...
88  TheTarget->createTargetMachine(TheTriple.getTriple(), MCPU, FeaturesStr,
89  Options, RelocModel, CMModel, OptLevel,
90  /*JIT*/ true);
91  Target->Options.EmulatedTLS = EmulatedTLS;
92  Target->Options.ExplicitEmulatedTLS = true;
93 
94  assert(Target && "Could not allocate target machine!");
95  return Target;
96 }
i
i
Definition: README.txt:29
llvm::Triple::setTriple
void setTriple(const Twine &Str)
setTriple - Set all components to the new triple Str.
Definition: Triple.cpp:1204
llvm
---------------------— PointerInfo ------------------------------------—
Definition: AllocatorList.h:23
llvm::StringRef::empty
LLVM_NODISCARD bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:153
llvm::Triple::UnknownArch
@ UnknownArch
Definition: Triple.h:48
ExecutionEngine.h
llvm::Target
Target - Wrapper for Target specific information.
Definition: TargetRegistry.h:137
llvm::SubtargetFeatures::AddFeature
void AddFeature(StringRef String, bool Enable=true)
Adds Features.
Definition: SubtargetFeature.cpp:41
Host.h
llvm::Triple::getArchTypeForLLVMName
static ArchType getArchTypeForLLVMName(StringRef Str)
getArchTypeForLLVMName - The canonical type for the given LLVM architecture name (e....
Definition: Triple.cpp:275
llvm::Triple
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:45
llvm::Type
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
Module.h
llvm::sys::path::end
const_iterator end(StringRef path)
Get end iterator over path.
Definition: Path.cpp:233
llvm::AMDGPU::Exp::Target
Target
Definition: SIDefines.h:739
T
#define T
Definition: Mips16ISelLowering.cpp:341
llvm::TargetRegistry::lookupTarget
static const Target * lookupTarget(const std::string &Triple, std::string &Error)
lookupTarget - Lookup a target based on a target triple.
Definition: TargetRegistry.cpp:62
SubtargetFeature.h
TargetMachine.h
llvm::Triple::ArchType
ArchType
Definition: Triple.h:47
llvm::TargetRegistry::targets
static iterator_range< iterator > targets()
Definition: TargetRegistry.cpp:20
llvm::SubtargetFeatures
Manages the enabling and disabling of subtarget specific features.
Definition: SubtargetFeature.h:183
llvm::Triple::getTriple
const std::string & getTriple() const
Definition: Triple.h:378
llvm::SubtargetFeatures::getString
std::string getString() const
Returns features as a string.
Definition: SubtargetFeature.cpp:54
I
#define I(x, y, z)
Definition: MD5.cpp:59
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::TargetMachine
Primary interface to the complete machine description for the target machine.
Definition: TargetMachine.h:79
Triple.h
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
llvm::Triple::setArch
void setArch(ArchType Kind)
setArch - Set the architecture (first) component of the triple to a known type.
Definition: Triple.cpp:1208
llvm::find_if
auto find_if(R &&Range, UnaryPredicate P)
Provide wrappers to std::find_if which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1574
llvm::sys::getProcessTriple
std::string getProcessTriple()
getProcessTriple() - Return an appropriate target triple for generating code to be loaded into the cu...
Definition: Host.cpp:1748
llvm::Error
Lightweight error class with error context and mandatory checking.
Definition: Error.h:157
llvm::SmallVectorImpl< std::string >
llvm::EngineKind::Interpreter
@ Interpreter
Definition: ExecutionEngine.h:525
TargetRegistry.h
llvm::EngineBuilder::selectTarget
TargetMachine * selectTarget()
Definition: TargetSelect.cpp:26
llvm::Target::createTargetMachine
TargetMachine * createTargetMachine(StringRef TT, StringRef CPU, StringRef Features, const TargetOptions &Options, Optional< Reloc::Model > RM, Optional< CodeModel::Model > CM=None, CodeGenOpt::Level OL=CodeGenOpt::Default, bool JIT=false) const
createTargetMachine - Create a target specific machine implementation for the specified Triple.
Definition: TargetRegistry.h:449