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