clang 22.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
10#include "llvm/ADT/STLExtras.h"
11#include "llvm/ADT/SmallSet.h"
12#include "llvm/ADT/SmallVector.h"
13#include "llvm/Support/Path.h"
14#include "llvm/TargetParser/TargetParser.h"
15#include "llvm/TargetParser/Triple.h"
16#include <map>
17#include <optional>
18#include <string>
19
20namespace clang {
21
24 llvm::StringRef Proc) {
25 // Entries in returned vector should be in alphabetical order.
27 auto ProcKind = T.isAMDGCN() ? llvm::AMDGPU::parseArchAMDGCN(Proc)
28 : llvm::AMDGPU::parseArchR600(Proc);
29 if (ProcKind == llvm::AMDGPU::GK_NONE)
30 return Ret;
31 auto Features = T.isAMDGCN() ? llvm::AMDGPU::getArchAttrAMDGCN(ProcKind)
32 : llvm::AMDGPU::getArchAttrR600(ProcKind);
33 if (Features & llvm::AMDGPU::FEATURE_SRAMECC)
34 Ret.push_back("sramecc");
35 if (Features & llvm::AMDGPU::FEATURE_XNACK)
36 Ret.push_back("xnack");
37 return Ret;
38}
39
42 llvm::StringRef Processor) {
44 if (T.isAMDGPU())
45 return getAllPossibleAMDGPUTargetIDFeatures(T, Processor);
46 return Ret;
47}
48
49/// Returns canonical processor name or empty string if \p Processor is invalid.
50static llvm::StringRef getCanonicalProcessorName(const llvm::Triple &T,
51 llvm::StringRef Processor) {
52 if (T.isAMDGPU())
53 return llvm::AMDGPU::getCanonicalArchName(T, Processor);
54 return Processor;
55}
56
57llvm::StringRef getProcessorFromTargetID(const llvm::Triple &T,
58 llvm::StringRef TargetID) {
59 auto Split = TargetID.split(':');
60 return getCanonicalProcessorName(T, Split.first);
61}
62
63// Parse a target ID with format checking only. Do not check whether processor
64// name or features are valid for the processor.
65//
66// A target ID is a processor name followed by a list of target features
67// delimited by colon. Each target feature is a string post-fixed by a plus
68// or minus sign, e.g. gfx908:sramecc+:xnack-.
69static std::optional<llvm::StringRef>
70parseTargetIDWithFormatCheckingOnly(llvm::StringRef TargetID,
71 llvm::StringMap<bool> *FeatureMap) {
72 llvm::StringRef Processor;
73
74 if (TargetID.empty())
75 return llvm::StringRef();
76
77 auto Split = TargetID.split(':');
78 Processor = Split.first;
79 if (Processor.empty())
80 return std::nullopt;
81
82 auto Features = Split.second;
83 if (Features.empty())
84 return Processor;
85
86 llvm::StringMap<bool> LocalFeatureMap;
87 if (!FeatureMap)
88 FeatureMap = &LocalFeatureMap;
89
90 while (!Features.empty()) {
91 auto Splits = Features.split(':');
92 auto Sign = Splits.first.back();
93 auto Feature = Splits.first.drop_back();
94 if (Sign != '+' && Sign != '-')
95 return std::nullopt;
96 bool IsOn = Sign == '+';
97 // Each feature can only show up at most once in target ID.
98 if (!FeatureMap->try_emplace(Feature, IsOn).second)
99 return std::nullopt;
100 Features = Splits.second;
101 }
102 return Processor;
103}
104
105std::optional<llvm::StringRef>
106parseTargetID(const llvm::Triple &T, llvm::StringRef TargetID,
107 llvm::StringMap<bool> *FeatureMap) {
108 auto OptionalProcessor =
109 parseTargetIDWithFormatCheckingOnly(TargetID, FeatureMap);
110
111 if (!OptionalProcessor)
112 return std::nullopt;
113
114 llvm::StringRef Processor = getCanonicalProcessorName(T, *OptionalProcessor);
115 if (Processor.empty())
116 return std::nullopt;
117
118 llvm::SmallSet<llvm::StringRef, 4> AllFeatures(
119 llvm::from_range, getAllPossibleTargetIDFeatures(T, Processor));
120
121 for (auto &&F : *FeatureMap)
122 if (!AllFeatures.count(F.first()))
123 return std::nullopt;
124
125 return Processor;
126}
127
128// A canonical target ID is a target ID containing a canonical processor name
129// and features in alphabetical order.
130std::string getCanonicalTargetID(llvm::StringRef Processor,
131 const llvm::StringMap<bool> &Features) {
132 std::string TargetID = Processor.str();
133 std::map<const llvm::StringRef, bool> OrderedMap;
134 for (const auto &F : Features)
135 OrderedMap[F.first()] = F.second;
136 for (const auto &F : OrderedMap)
137 TargetID = TargetID + ':' + F.first.str() + (F.second ? "+" : "-");
138 return TargetID;
139}
140
141// For a specific processor, a feature either shows up in all target IDs, or
142// does not show up in any target IDs. Otherwise the target ID combination
143// is invalid.
144std::optional<std::pair<llvm::StringRef, llvm::StringRef>>
145getConflictTargetIDCombination(const std::set<llvm::StringRef> &TargetIDs) {
146 struct Info {
147 llvm::StringRef TargetID;
148 llvm::StringMap<bool> Features;
149 Info(llvm::StringRef TargetID, const llvm::StringMap<bool> &Features)
150 : TargetID(TargetID), Features(Features) {}
151 };
152 llvm::StringMap<Info> FeatureMap;
153 for (auto &&ID : TargetIDs) {
154 llvm::StringMap<bool> Features;
155 llvm::StringRef Proc = *parseTargetIDWithFormatCheckingOnly(ID, &Features);
156 auto [Loc, Inserted] = FeatureMap.try_emplace(Proc, ID, Features);
157 if (!Inserted) {
158 auto &ExistingFeatures = Loc->second.Features;
159 if (llvm::any_of(Features, [&](auto &F) {
160 return ExistingFeatures.count(F.first()) == 0;
161 }))
162 return std::make_pair(Loc->second.TargetID, ID);
163 }
164 }
165 return std::nullopt;
166}
167
168bool isCompatibleTargetID(llvm::StringRef Provided, llvm::StringRef Requested) {
169 llvm::StringMap<bool> ProvidedFeatures, RequestedFeatures;
170 llvm::StringRef ProvidedProc =
171 *parseTargetIDWithFormatCheckingOnly(Provided, &ProvidedFeatures);
172 llvm::StringRef RequestedProc =
173 *parseTargetIDWithFormatCheckingOnly(Requested, &RequestedFeatures);
174 if (ProvidedProc != RequestedProc)
175 return false;
176 for (const auto &F : ProvidedFeatures) {
177 auto Loc = RequestedFeatures.find(F.first());
178 // The default (unspecified) value of a feature is 'All', which can match
179 // either 'On' or 'Off'.
180 if (Loc == RequestedFeatures.end())
181 return false;
182 // If a feature is specified, it must have exact match.
183 if (Loc->second != F.second)
184 return false;
185 }
186 return true;
187}
188
189std::string sanitizeTargetIDInFileName(llvm::StringRef TargetID) {
190 std::string FileName = TargetID.str();
191 if (llvm::sys::path::is_style_windows(llvm::sys::path::Style::native))
192 llvm::replace(FileName, ':', '@');
193 return FileName;
194}
195
196} // namespace clang
The JSON file list parser is used to communicate input to InstallAPI.
static llvm::SmallVector< llvm::StringRef, 4 > getAllPossibleAMDGPUTargetIDFeatures(const llvm::Triple &T, llvm::StringRef Proc)
Definition TargetID.cpp:23
std::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:106
static std::optional< llvm::StringRef > parseTargetIDWithFormatCheckingOnly(llvm::StringRef TargetID, llvm::StringMap< bool > *FeatureMap)
Definition TargetID.cpp:70
std::string sanitizeTargetIDInFileName(llvm::StringRef TargetID)
Sanitize a target ID string for use in a file name.
Definition TargetID.cpp:189
llvm::StringRef getProcessorFromTargetID(const llvm::Triple &T, llvm::StringRef OffloadArch)
Get processor name from target ID.
Definition TargetID.cpp:57
std::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:145
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:50
bool isCompatibleTargetID(llvm::StringRef Provided, llvm::StringRef Requested)
Check whether the provided target ID is compatible with the requested target ID.
Definition TargetID.cpp:168
const FunctionProtoType * T
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:41
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:130