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 if (!T.isAMDGCN())
28 return Ret;
29 llvm::AMDGPU::GPUKind ProcKind = llvm::AMDGPU::parseArchAMDGCN(CPU: Proc);
30 if (ProcKind == llvm::AMDGPU::GK_NONE)
31 return Ret;
32 const llvm::AMDGPU::AMDGPUFeatureBitset &Features =
33 llvm::AMDGPU::getFeatureBitset(AK: ProcKind);
34 if (Features.test(I: llvm::AMDGPU::FEAT_SRAMECC_SUPPORT))
35 Ret.push_back(Elt: "sramecc");
36 // Only allow xnack in target ID if the processor supports on/off modes.
37 if (Features.test(I: llvm::AMDGPU::FEAT_XNACK_ON_OFF_MODES))
38 Ret.push_back(Elt: "xnack");
39 return Ret;
40}
41
42llvm::SmallVector<llvm::StringRef, 4>
43getAllPossibleTargetIDFeatures(const llvm::Triple &T,
44 llvm::StringRef Processor) {
45 llvm::SmallVector<llvm::StringRef, 4> Ret;
46 if (T.isAMDGPU())
47 return getAllPossibleAMDGPUTargetIDFeatures(T, Proc: 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, Arch: Processor);
56 return Processor;
57}
58
59llvm::StringRef getProcessorFromTargetID(const llvm::Triple &T,
60 llvm::StringRef TargetID) {
61 auto Split = TargetID.split(Separator: ':');
62 return getCanonicalProcessorName(T, Processor: 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(Separator: ':');
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(Separator: ':');
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(Key: Feature, Args&: 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, Processor: *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(V: 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(TargetID: ID, FeatureMap: &Features);
160 auto [Loc, Inserted] = FeatureMap.try_emplace(Key: Proc, Args: ID, Args&: Features);
161 if (!Inserted) {
162 auto &ExistingFeatures = Loc->second.Features;
163 if (llvm::any_of(Range&: Features, P: [&](auto &F) {
164 return ExistingFeatures.count(F.first()) == 0;
165 }))
166 return std::make_pair(x&: Loc->second.TargetID, y: 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(TargetID: Provided, FeatureMap: &ProvidedFeatures);
176 llvm::StringRef RequestedProc =
177 *parseTargetIDWithFormatCheckingOnly(TargetID: Requested, FeatureMap: &RequestedFeatures);
178 if (ProvidedProc != RequestedProc)
179 return false;
180 for (const auto &F : ProvidedFeatures) {
181 auto Loc = RequestedFeatures.find(Key: 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(S: llvm::sys::path::Style::native))
196 llvm::replace(Range&: FileName, OldValue: ':', NewValue: '@');
197 return FileName;
198}
199
200} // namespace clang
201