clang  15.0.0git
TargetID.cpp
Go to the documentation of this file.
1 //===--- TargetID.cpp - Utilities for parsing target ID -------------------===//
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 #include "clang/Basic/TargetID.h"
10 #include "llvm/ADT/SmallSet.h"
11 #include "llvm/ADT/Triple.h"
12 #include "llvm/Support/TargetParser.h"
13 #include "llvm/Support/raw_ostream.h"
14 #include <map>
15 
16 namespace clang {
17 
20  llvm::StringRef Proc) {
21  // Entries in returned vector should be in alphabetical order.
23  auto ProcKind = T.isAMDGCN() ? llvm::AMDGPU::parseArchAMDGCN(Proc)
24  : llvm::AMDGPU::parseArchR600(Proc);
25  if (ProcKind == llvm::AMDGPU::GK_NONE)
26  return Ret;
27  auto Features = T.isAMDGCN() ? llvm::AMDGPU::getArchAttrAMDGCN(ProcKind)
28  : llvm::AMDGPU::getArchAttrR600(ProcKind);
29  if (Features & llvm::AMDGPU::FEATURE_SRAMECC)
30  Ret.push_back("sramecc");
31  if (Features & llvm::AMDGPU::FEATURE_XNACK)
32  Ret.push_back("xnack");
33  return Ret;
34 }
35 
37 getAllPossibleTargetIDFeatures(const llvm::Triple &T,
38  llvm::StringRef Processor) {
40  if (T.isAMDGPU())
41  return getAllPossibleAMDGPUTargetIDFeatures(T, Processor);
42  return Ret;
43 }
44 
45 /// Returns canonical processor name or empty string if \p Processor is invalid.
46 static llvm::StringRef getCanonicalProcessorName(const llvm::Triple &T,
47  llvm::StringRef Processor) {
48  if (T.isAMDGPU())
49  return llvm::AMDGPU::getCanonicalArchName(T, Processor);
50  return Processor;
51 }
52 
53 llvm::StringRef getProcessorFromTargetID(const llvm::Triple &T,
54  llvm::StringRef TargetID) {
55  auto Split = TargetID.split(':');
56  return getCanonicalProcessorName(T, Split.first);
57 }
58 
59 // Parse a target ID with format checking only. Do not check whether processor
60 // name or features are valid for the processor.
61 //
62 // A target ID is a processor name followed by a list of target features
63 // delimited by colon. Each target feature is a string post-fixed by a plus
64 // or minus sign, e.g. gfx908:sramecc+:xnack-.
66 parseTargetIDWithFormatCheckingOnly(llvm::StringRef TargetID,
67  llvm::StringMap<bool> *FeatureMap) {
68  llvm::StringRef Processor;
69 
70  if (TargetID.empty())
71  return llvm::StringRef();
72 
73  auto Split = TargetID.split(':');
74  Processor = Split.first;
75  if (Processor.empty())
76  return llvm::None;
77 
78  auto Features = Split.second;
79  if (Features.empty())
80  return Processor;
81 
82  llvm::StringMap<bool> LocalFeatureMap;
83  if (!FeatureMap)
84  FeatureMap = &LocalFeatureMap;
85 
86  while (!Features.empty()) {
87  auto Splits = Features.split(':');
88  auto Sign = Splits.first.back();
89  auto Feature = Splits.first.drop_back();
90  if (Sign != '+' && Sign != '-')
91  return llvm::None;
92  bool IsOn = Sign == '+';
93  auto Loc = FeatureMap->find(Feature);
94  // Each feature can only show up at most once in target ID.
95  if (Loc != FeatureMap->end())
96  return llvm::None;
97  (*FeatureMap)[Feature] = IsOn;
98  Features = Splits.second;
99  }
100  return Processor;
101 }
102 
104 parseTargetID(const llvm::Triple &T, llvm::StringRef TargetID,
105  llvm::StringMap<bool> *FeatureMap) {
106  auto OptionalProcessor =
107  parseTargetIDWithFormatCheckingOnly(TargetID, FeatureMap);
108 
109  if (!OptionalProcessor)
110  return llvm::None;
111 
112  llvm::StringRef Processor = getCanonicalProcessorName(T, *OptionalProcessor);
113  if (Processor.empty())
114  return llvm::None;
115 
116  llvm::SmallSet<llvm::StringRef, 4> AllFeatures;
117  for (auto &&F : getAllPossibleTargetIDFeatures(T, Processor))
118  AllFeatures.insert(F);
119 
120  for (auto &&F : *FeatureMap)
121  if (!AllFeatures.count(F.first()))
122  return llvm::None;
123 
124  return Processor;
125 }
126 
127 // A canonical target ID is a target ID containing a canonical processor name
128 // and features in alphabetical order.
129 std::string getCanonicalTargetID(llvm::StringRef Processor,
130  const llvm::StringMap<bool> &Features) {
131  std::string TargetID = Processor.str();
132  std::map<const llvm::StringRef, bool> OrderedMap;
133  for (const auto &F : Features)
134  OrderedMap[F.first()] = F.second;
135  for (auto F : OrderedMap)
136  TargetID = TargetID + ':' + F.first.str() + (F.second ? "+" : "-");
137  return TargetID;
138 }
139 
140 // For a specific processor, a feature either shows up in all target IDs, or
141 // does not show up in any target IDs. Otherwise the target ID combination
142 // is invalid.
144 getConflictTargetIDCombination(const std::set<llvm::StringRef> &TargetIDs) {
145  struct Info {
146  llvm::StringRef TargetID;
147  llvm::StringMap<bool> Features;
148  };
149  llvm::StringMap<Info> FeatureMap;
150  for (auto &&ID : TargetIDs) {
151  llvm::StringMap<bool> Features;
152  llvm::StringRef Proc = *parseTargetIDWithFormatCheckingOnly(ID, &Features);
153  auto Loc = FeatureMap.find(Proc);
154  if (Loc == FeatureMap.end())
155  FeatureMap[Proc] = Info{ID, Features};
156  else {
157  auto &ExistingFeatures = Loc->second.Features;
158  if (llvm::any_of(Features, [&](auto &F) {
159  return ExistingFeatures.count(F.first()) == 0;
160  }))
161  return std::make_pair(Loc->second.TargetID, ID);
162  }
163  }
164  return llvm::None;
165 }
166 
167 } // namespace clang
clang::parseTargetIDWithFormatCheckingOnly
static llvm::Optional< llvm::StringRef > parseTargetIDWithFormatCheckingOnly(llvm::StringRef TargetID, llvm::StringMap< bool > *FeatureMap)
Definition: TargetID.cpp:66
string
string(SUBSTRING ${CMAKE_CURRENT_BINARY_DIR} 0 ${PATH_LIB_START} PATH_HEAD) string(SUBSTRING $
Definition: CMakeLists.txt:22
Ret
static bool Ret(InterpState &S, CodePtr &PC, APValue &Result)
Definition: Interp.cpp:34
llvm::SmallVector< llvm::StringRef, 4 >
llvm::Optional< llvm::StringRef >
clang::getProcessorFromTargetID
llvm::StringRef getProcessorFromTargetID(const llvm::Triple &T, llvm::StringRef OffloadArch)
Get processor name from target ID.
Definition: TargetID.cpp:53
TargetID.h
clang::parseTargetID
llvm::Optional< llvm::StringRef > parseTargetID(const llvm::Triple &T, llvm::StringRef OffloadArch, llvm::StringMap< bool > *FeatureMap)
Parse a target ID to get processor and feature map.
Definition: TargetID.cpp:104
clang::getConflictTargetIDCombination
llvm::Optional< std::pair< llvm::StringRef, llvm::StringRef > > getConflictTargetIDCombination(const std::set< llvm::StringRef > &TargetIDs)
Get the conflicted pair of target IDs for a compilation or a bundled code object, assuming TargetIDs ...
Definition: TargetID.cpp:144
clang::getCanonicalTargetID
std::string getCanonicalTargetID(llvm::StringRef Processor, const llvm::StringMap< bool > &Features)
Returns canonical target ID, assuming Processor is canonical and all entries in Features are valid.
Definition: TargetID.cpp:129
DwarfFissionKind::Split
@ Split
clang::getAllPossibleTargetIDFeatures
llvm::SmallVector< llvm::StringRef, 4 > getAllPossibleTargetIDFeatures(const llvm::Triple &T, llvm::StringRef Processor)
Get all feature strings that can be used in target ID for Processor.
Definition: TargetID.cpp:37
clang::Builtin::ID
ID
Definition: Builtins.h:52
clang
Definition: CalledOnceCheck.h:17
clang::getAllPossibleAMDGPUTargetIDFeatures
static llvm::SmallVector< llvm::StringRef, 4 > getAllPossibleAMDGPUTargetIDFeatures(const llvm::Triple &T, llvm::StringRef Proc)
Definition: TargetID.cpp:19
clang::getCanonicalProcessorName
static llvm::StringRef getCanonicalProcessorName(const llvm::Triple &T, llvm::StringRef Processor)
Returns canonical processor name or empty string if Processor is invalid.
Definition: TargetID.cpp:46