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