clang 24.0.0git
SyncScope.h
Go to the documentation of this file.
1//===--- SyncScope.h - Atomic synchronization scopes ------------*- C++ -*-===//
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/// \file
10/// Provides definitions for the atomic synchronization scopes.
11///
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_CLANG_BASIC_SYNCSCOPE_H
15#define LLVM_CLANG_BASIC_SYNCSCOPE_H
16
18#include "llvm/ADT/ArrayRef.h"
19#include "llvm/ADT/StringRef.h"
20#include "llvm/TargetParser/AtomicScope.h"
21#include <memory>
22
23namespace clang {
24
25/// Defines sync scope values used internally by clang.
26///
27/// The enum values start from 0 and are contiguous. They are mainly used for
28/// enumerating all supported sync scope values and mapping them to LLVM
29/// sync scopes. Their numerical values may be different from the corresponding
30/// sync scope enums used in source languages.
31///
32/// In atomic builtin and expressions, language-specific sync scope enums are
33/// used. Currently only OpenCL memory scope enums are supported and assumed
34/// to be used by all languages. However, in the future, other languages may
35/// define their own set of sync scope enums. The language-specific sync scope
36/// values are represented by class AtomicScopeModel and its derived classes.
37///
38/// To add a new enum value:
39/// Add the enum value to enum class SyncScope.
40/// Update enum value Last if necessary.
41/// Update getAsString.
42///
62
63inline llvm::StringRef getAsString(SyncScope S) {
64 switch (S) {
66 return "system_scope";
68 return "device_scope";
70 return "workgroup_scope";
72 return "cluster_scope";
74 return "wavefront_scope";
76 return "single_scope";
78 return "hip_singlethread";
80 return "hip_wavefront";
82 return "hip_workgroup";
84 return "hip_cluster";
86 return "hip_agent";
88 return "hip_system";
90 return "opencl_workgroup";
92 return "opencl_device";
94 return "opencl_allsvmdevices";
96 return "opencl_subgroup";
97 }
98 llvm_unreachable("Invalid sync scope");
99}
100
101/// Defines the kind of atomic scope models.
103
104/// Defines the interface for sync scope model.
106public:
107 virtual ~AtomicScopeModel() {}
108 /// Maps language specific sync scope values to internal
109 /// SyncScope enum.
110 virtual SyncScope map(unsigned S) const = 0;
111
112 /// Check if the compile-time constant sync scope value
113 /// is valid.
114 virtual bool isValid(unsigned S) const = 0;
115
116 /// Get all possible sync scope values that might be
117 /// encountered at runtime for the current language.
119
120 /// If atomic builtin function is called with invalid
121 /// sync scope value at runtime, it will fall back to a valid
122 /// sync scope value returned by this function.
123 virtual unsigned getFallBackValue() const = 0;
124
125 /// Create an atomic scope model by AtomicScopeModelKind.
126 /// \return an empty std::unique_ptr for AtomicScopeModelKind::None.
127 static std::unique_ptr<AtomicScopeModel> create(AtomicScopeModelKind K);
128};
129
130/// Defines the sync scope model for OpenCL.
132public:
133 /// The enum values match the pre-defined macros
134 /// __OPENCL_MEMORY_SCOPE_*, which are used to define memory_scope_*
135 /// enums in opencl-c-base.h.
143
145
146 SyncScope map(unsigned S) const override {
147 switch (static_cast<ID>(S)) {
148 case WorkGroup:
150 case Device:
152 case AllSVMDevices:
154 case SubGroup:
156 }
157 llvm_unreachable("Invalid language sync scope value");
158 }
159
160 bool isValid(unsigned S) const override {
161 return S >= static_cast<unsigned>(WorkGroup) &&
162 S <= static_cast<unsigned>(Last);
163 }
164
166 static_assert(Last == SubGroup, "Does not include all sync scopes");
167 static const unsigned Scopes[] = {
168 static_cast<unsigned>(WorkGroup), static_cast<unsigned>(Device),
169 static_cast<unsigned>(AllSVMDevices), static_cast<unsigned>(SubGroup)};
170 return llvm::ArrayRef(Scopes);
171 }
172
173 unsigned getFallBackValue() const override {
174 return static_cast<unsigned>(AllSVMDevices);
175 }
176};
177
178/// Defines the sync scope model for HIP.
180public:
181 /// The enum values match the pre-defined macros
182 /// __HIP_MEMORY_SCOPE_*, which are used to define memory_scope_*
183 /// enums in hip-c.h.
184 /// These may be present in pch files or bitcode so preserve existing values
185 /// when adding a new ID.
197
199
200 SyncScope map(unsigned S) const override {
201 switch (static_cast<ID>(S)) {
202 case SingleThread:
204 case Wavefront:
206 case Workgroup:
208 case Cluster:
210 case Agent:
211 return SyncScope::HIPAgent;
212 case System:
214 case End:
215 break;
216 }
217 llvm_unreachable("Invalid language sync scope value");
218 }
219
220 bool isValid(unsigned S) const override {
221 return S >= static_cast<unsigned>(SingleThread) &&
222 S <= static_cast<unsigned>(Last);
223 }
224
226 static const unsigned Scopes[] = {
227 static_cast<unsigned>(SingleThread), static_cast<unsigned>(Wavefront),
228 static_cast<unsigned>(Workgroup), static_cast<unsigned>(Cluster),
229 static_cast<unsigned>(System), static_cast<unsigned>(Agent)};
230 static_assert(sizeof(Scopes) / sizeof(Scopes[0]) == Count,
231 "Does not include all sync scopes");
232 return llvm::ArrayRef(Scopes);
233 }
234
235 unsigned getFallBackValue() const override {
236 return static_cast<unsigned>(System);
237 }
238};
239
240/// Defines the generic atomic scope model.
242public:
243 /// The enum values match predefined built-in macros __MEMORY_SCOPE_*.
244 /// These are ABI-sensitive (they may be present in pch files or bitcode) so
245 /// existing values must be preserved.
246 enum ID {
247 System = static_cast<unsigned>(llvm::AtomicScope::System),
248 Device = static_cast<unsigned>(llvm::AtomicScope::Device),
249 Workgroup = static_cast<unsigned>(llvm::AtomicScope::Workgroup),
250 Wavefront = static_cast<unsigned>(llvm::AtomicScope::Wavefront),
251 Single = static_cast<unsigned>(llvm::AtomicScope::Single),
252 Cluster = static_cast<unsigned>(llvm::AtomicScope::Cluster),
255 };
256
258
259 SyncScope map(unsigned S) const override {
260 switch (static_cast<ID>(S)) {
261 case Device:
263 case System:
265 case Workgroup:
267 case Cluster:
269 case Wavefront:
271 case Single:
273 case Count:
274 break;
275 }
276 llvm_unreachable("Invalid language sync scope value");
277 }
278
279 bool isValid(unsigned S) const override {
280 return S <= static_cast<unsigned>(Last);
281 }
282
284 static const unsigned Scopes[] = {
285 static_cast<unsigned>(System), static_cast<unsigned>(Device),
286 static_cast<unsigned>(Workgroup), static_cast<unsigned>(Cluster),
287 static_cast<unsigned>(Wavefront), static_cast<unsigned>(Single)};
288 static_assert(sizeof(Scopes) / sizeof(Scopes[0]) == Count,
289 "Does not include all sync scopes");
290 return llvm::ArrayRef(Scopes);
291 }
292
293 unsigned getFallBackValue() const override {
294 return static_cast<unsigned>(System);
295 }
296};
297
298inline std::unique_ptr<AtomicScopeModel>
300 switch (K) {
302 return std::unique_ptr<AtomicScopeModel>{};
304 return std::make_unique<AtomicScopeOpenCLModel>();
306 return std::make_unique<AtomicScopeHIPModel>();
308 return std::make_unique<AtomicScopeGenericModel>();
309 }
310 llvm_unreachable("Invalid atomic scope model kind");
311}
312} // namespace clang
313
314#endif
Defines the clang::LangOptions interface.
bool isValid(unsigned S) const override
Check if the compile-time constant sync scope value is valid.
Definition SyncScope.h:279
unsigned getFallBackValue() const override
If atomic builtin function is called with invalid sync scope value at runtime, it will fall back to a...
Definition SyncScope.h:293
ID
The enum values match predefined built-in macros __MEMORY_SCOPE_*.
Definition SyncScope.h:246
ArrayRef< unsigned > getRuntimeValues() const override
Get all possible sync scope values that might be encountered at runtime for the current language.
Definition SyncScope.h:283
SyncScope map(unsigned S) const override
Maps language specific sync scope values to internal SyncScope enum.
Definition SyncScope.h:259
bool isValid(unsigned S) const override
Check if the compile-time constant sync scope value is valid.
Definition SyncScope.h:220
ID
The enum values match the pre-defined macros __HIP_MEMORY_SCOPE_*, which are used to define memory_sc...
Definition SyncScope.h:186
ArrayRef< unsigned > getRuntimeValues() const override
Get all possible sync scope values that might be encountered at runtime for the current language.
Definition SyncScope.h:225
unsigned getFallBackValue() const override
If atomic builtin function is called with invalid sync scope value at runtime, it will fall back to a...
Definition SyncScope.h:235
SyncScope map(unsigned S) const override
Maps language specific sync scope values to internal SyncScope enum.
Definition SyncScope.h:200
Defines the interface for sync scope model.
Definition SyncScope.h:105
static std::unique_ptr< AtomicScopeModel > create(AtomicScopeModelKind K)
Create an atomic scope model by AtomicScopeModelKind.
Definition SyncScope.h:299
virtual ArrayRef< unsigned > getRuntimeValues() const =0
Get all possible sync scope values that might be encountered at runtime for the current language.
virtual SyncScope map(unsigned S) const =0
Maps language specific sync scope values to internal SyncScope enum.
virtual unsigned getFallBackValue() const =0
If atomic builtin function is called with invalid sync scope value at runtime, it will fall back to a...
virtual ~AtomicScopeModel()
Definition SyncScope.h:107
virtual bool isValid(unsigned S) const =0
Check if the compile-time constant sync scope value is valid.
ID
The enum values match the pre-defined macros __OPENCL_MEMORY_SCOPE_*, which are used to define memory...
Definition SyncScope.h:136
ArrayRef< unsigned > getRuntimeValues() const override
Get all possible sync scope values that might be encountered at runtime for the current language.
Definition SyncScope.h:165
bool isValid(unsigned S) const override
Check if the compile-time constant sync scope value is valid.
Definition SyncScope.h:160
unsigned getFallBackValue() const override
If atomic builtin function is called with invalid sync scope value at runtime, it will fall back to a...
Definition SyncScope.h:173
SyncScope map(unsigned S) const override
Maps language specific sync scope values to internal SyncScope enum.
Definition SyncScope.h:146
The JSON file list parser is used to communicate input to InstallAPI.
AtomicScopeModelKind
Defines the kind of atomic scope models.
Definition SyncScope.h:102
SyncScope
Defines sync scope values used internally by clang.
Definition SyncScope.h:43
llvm::StringRef getAsString(SyncScope S)
Definition SyncScope.h:63
@ Generic
not a target-specific vector type
Definition TypeBase.h:4247
@ None
The alignment was not explicit in code.
Definition ASTContext.h:176