clang 23.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 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
41getAllPossibleTargetIDFeatures(const llvm::Triple &T,
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 if (Splits.first.empty())
93 return std::nullopt;
94 auto Sign = Splits.first.back();
95 auto Feature = Splits.first.drop_back();
96 if (Sign != '+' && Sign != '-')
97 return std::nullopt;
98 bool IsOn = Sign == '+';
99 // Each feature can only show up at most once in target ID.
100 if (!FeatureMap->try_emplace(Feature, IsOn).second)
101 return std::nullopt;
102 Features = Splits.second;
103 }
104 return Processor;
105}
106
107std::optional<llvm::StringRef>
108parseTargetID(const llvm::Triple &T, llvm::StringRef TargetID,
109 llvm::StringMap<bool> *FeatureMap) {
110 auto OptionalProcessor =
111 parseTargetIDWithFormatCheckingOnly(TargetID, FeatureMap);
112
113 if (!OptionalProcessor)
114 return std::nullopt;
115
116 llvm::StringRef Processor = getCanonicalProcessorName(T, *OptionalProcessor);
117 if (Processor.empty())
118 return std::nullopt;
119
120 llvm::SmallSet<llvm::StringRef, 4> AllFeatures(
121 llvm::from_range, getAllPossibleTargetIDFeatures(T, Processor));
122
123 for (auto &&F : *FeatureMap)
124 if (!AllFeatures.count(F.first()))
125 return std::nullopt;
126
127 return Processor;
128}
129
130// A canonical target ID is a target ID containing a canonical processor name
131// and features in alphabetical order.
132std::string getCanonicalTargetID(llvm::StringRef Processor,
133 const llvm::StringMap<bool> &Features) {
134 std::string TargetID = Processor.str();
135 std::map<const llvm::StringRef, bool> OrderedMap;
136 for (const auto &F : Features)
137 OrderedMap[F.first()] = F.second;
138 for (const auto &F : OrderedMap)
139 TargetID = TargetID + ':' + F.first.str() + (F.second ? "+" : "-");
140 return TargetID;
141}
142
143// For a specific processor, a feature either shows up in all target IDs, or
144// does not show up in any target IDs. Otherwise the target ID combination
145// is invalid.
146std::optional<std::pair<llvm::StringRef, llvm::StringRef>>
147getConflictTargetIDCombination(const std::set<llvm::StringRef> &TargetIDs) {
148 struct Info {
149 llvm::StringRef TargetID;
150 llvm::StringMap<bool> Features;
151 Info(llvm::StringRef TargetID, const llvm::StringMap<bool> &Features)
152 : TargetID(TargetID), Features(Features) {}
153 };
154 llvm::StringMap<Info> FeatureMap;
155 for (auto &&ID : TargetIDs) {
156 llvm::StringMap<bool> Features;
157 llvm::StringRef Proc = *parseTargetIDWithFormatCheckingOnly(ID, &Features);
158 auto [Loc, Inserted] = FeatureMap.try_emplace(Proc, ID, Features);
159 if (!Inserted) {
160 auto &ExistingFeatures = Loc->second.Features;
161 if (llvm::any_of(Features, [&](auto &F) {
162 return ExistingFeatures.count(F.first()) == 0;
163 }))
164 return std::make_pair(Loc->second.TargetID, ID);
165 }
166 }
167 return std::nullopt;
168}
169
170bool isCompatibleTargetID(llvm::StringRef Provided, llvm::StringRef Requested) {
171 llvm::StringMap<bool> ProvidedFeatures, RequestedFeatures;
172 llvm::StringRef ProvidedProc =
173 *parseTargetIDWithFormatCheckingOnly(Provided, &ProvidedFeatures);
174 llvm::StringRef RequestedProc =
175 *parseTargetIDWithFormatCheckingOnly(Requested, &RequestedFeatures);
176 if (ProvidedProc != RequestedProc)
177 return false;
178 for (const auto &F : ProvidedFeatures) {
179 auto Loc = RequestedFeatures.find(F.first());
180 // The default (unspecified) value of a feature is 'All', which can match
181 // either 'On' or 'Off'.
182 if (Loc == RequestedFeatures.end())
183 return false;
184 // If a feature is specified, it must have exact match.
185 if (Loc->second != F.second)
186 return false;
187 }
188 return true;
189}
190
191std::string sanitizeTargetIDInFileName(llvm::StringRef TargetID) {
192 std::string FileName = TargetID.str();
193 if (llvm::sys::path::is_style_windows(llvm::sys::path::Style::native))
194 llvm::replace(FileName, ':', '@');
195 return FileName;
196}
197
198} // 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:108
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:191
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:147
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:170
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:132