clang 22.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 <memory>
21
22namespace clang {
23
24/// Defines sync scope values used internally by clang.
25///
26/// The enum values start from 0 and are contiguous. They are mainly used for
27/// enumerating all supported sync scope values and mapping them to LLVM
28/// sync scopes. Their numerical values may be different from the corresponding
29/// sync scope enums used in source languages.
30///
31/// In atomic builtin and expressions, language-specific sync scope enums are
32/// used. Currently only OpenCL memory scope enums are supported and assumed
33/// to be used by all languages. However, in the future, other languages may
34/// define their own set of sync scope enums. The language-specific sync scope
35/// values are represented by class AtomicScopeModel and its derived classes.
36///
37/// To add a new enum value:
38/// Add the enum value to enum class SyncScope.
39/// Update enum value Last if necessary.
40/// Update getAsString.
41///
61
62inline llvm::StringRef getAsString(SyncScope S) {
63 switch (S) {
65 return "system_scope";
67 return "device_scope";
69 return "workgroup_scope";
71 return "cluster_scope";
73 return "wavefront_scope";
75 return "single_scope";
77 return "hip_singlethread";
79 return "hip_wavefront";
81 return "hip_workgroup";
83 return "hip_cluster";
85 return "hip_agent";
87 return "hip_system";
89 return "opencl_workgroup";
91 return "opencl_device";
93 return "opencl_allsvmdevices";
95 return "opencl_subgroup";
96 }
97 llvm_unreachable("Invalid sync scope");
98}
99
100/// Defines the kind of atomic scope models.
102
103/// Defines the interface for sync scope model.
105public:
106 virtual ~AtomicScopeModel() {}
107 /// Maps language specific sync scope values to internal
108 /// SyncScope enum.
109 virtual SyncScope map(unsigned S) const = 0;
110
111 /// Check if the compile-time constant sync scope value
112 /// is valid.
113 virtual bool isValid(unsigned S) const = 0;
114
115 /// Get all possible sync scope values that might be
116 /// encountered at runtime for the current language.
118
119 /// If atomic builtin function is called with invalid
120 /// sync scope value at runtime, it will fall back to a valid
121 /// sync scope value returned by this function.
122 virtual unsigned getFallBackValue() const = 0;
123
124 /// Create an atomic scope model by AtomicScopeModelKind.
125 /// \return an empty std::unique_ptr for AtomicScopeModelKind::None.
126 static std::unique_ptr<AtomicScopeModel> create(AtomicScopeModelKind K);
127};
128
129/// Defines the sync scope model for OpenCL.
131public:
132 /// The enum values match the pre-defined macros
133 /// __OPENCL_MEMORY_SCOPE_*, which are used to define memory_scope_*
134 /// enums in opencl-c-base.h.
142
144
145 SyncScope map(unsigned S) const override {
146 switch (static_cast<ID>(S)) {
147 case WorkGroup:
149 case Device:
151 case AllSVMDevices:
153 case SubGroup:
155 }
156 llvm_unreachable("Invalid language sync scope value");
157 }
158
159 bool isValid(unsigned S) const override {
160 return S >= static_cast<unsigned>(WorkGroup) &&
161 S <= static_cast<unsigned>(Last);
162 }
163
165 static_assert(Last == SubGroup, "Does not include all sync scopes");
166 static const unsigned Scopes[] = {
167 static_cast<unsigned>(WorkGroup), static_cast<unsigned>(Device),
168 static_cast<unsigned>(AllSVMDevices), static_cast<unsigned>(SubGroup)};
169 return llvm::ArrayRef(Scopes);
170 }
171
172 unsigned getFallBackValue() const override {
173 return static_cast<unsigned>(AllSVMDevices);
174 }
175};
176
177/// Defines the sync scope model for HIP.
179public:
180 /// The enum values match the pre-defined macros
181 /// __HIP_MEMORY_SCOPE_*, which are used to define memory_scope_*
182 /// enums in hip-c.h.
183 /// These may be present in pch files or bitcode so preserve existing values
184 /// when adding a new ID.
196
198
199 SyncScope map(unsigned S) const override {
200 switch (static_cast<ID>(S)) {
201 case SingleThread:
203 case Wavefront:
205 case Workgroup:
207 case Cluster:
209 case Agent:
210 return SyncScope::HIPAgent;
211 case System:
213 case End:
214 break;
215 }
216 llvm_unreachable("Invalid language sync scope value");
217 }
218
219 bool isValid(unsigned S) const override {
220 return S >= static_cast<unsigned>(SingleThread) &&
221 S <= static_cast<unsigned>(Last);
222 }
223
225 static const unsigned Scopes[] = {
226 static_cast<unsigned>(SingleThread), static_cast<unsigned>(Wavefront),
227 static_cast<unsigned>(Workgroup), static_cast<unsigned>(Cluster),
228 static_cast<unsigned>(System), static_cast<unsigned>(Agent)};
229 static_assert(sizeof(Scopes) / sizeof(Scopes[0]) == Count,
230 "Does not include all sync scopes");
231 return llvm::ArrayRef(Scopes);
232 }
233
234 unsigned getFallBackValue() const override {
235 return static_cast<unsigned>(System);
236 }
237};
238
239/// Defines the generic atomic scope model.
241public:
242 /// The enum values match predefined built-in macros __MEMORY_SCOPE_*.
243 /// These may be present in pch files or bitcode so preserve existing values
244 /// when adding a new ID.
255
257
258 SyncScope map(unsigned S) const override {
259 switch (static_cast<ID>(S)) {
260 case Device:
262 case System:
264 case Workgroup:
266 case Cluster:
268 case Wavefront:
270 case Single:
272 case Count:
273 break;
274 }
275 llvm_unreachable("Invalid language sync scope value");
276 }
277
278 bool isValid(unsigned S) const override {
279 return S <= static_cast<unsigned>(Last);
280 }
281
283 static const unsigned Scopes[] = {
284 static_cast<unsigned>(System), static_cast<unsigned>(Device),
285 static_cast<unsigned>(Workgroup), static_cast<unsigned>(Cluster),
286 static_cast<unsigned>(Wavefront), static_cast<unsigned>(Single)};
287 static_assert(sizeof(Scopes) / sizeof(Scopes[0]) == Count,
288 "Does not include all sync scopes");
289 return llvm::ArrayRef(Scopes);
290 }
291
292 unsigned getFallBackValue() const override {
293 return static_cast<unsigned>(System);
294 }
295};
296
297inline std::unique_ptr<AtomicScopeModel>
299 switch (K) {
301 return std::unique_ptr<AtomicScopeModel>{};
303 return std::make_unique<AtomicScopeOpenCLModel>();
305 return std::make_unique<AtomicScopeHIPModel>();
307 return std::make_unique<AtomicScopeGenericModel>();
308 }
309 llvm_unreachable("Invalid atomic scope model kind");
310}
311} // namespace clang
312
313#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:278
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:292
ID
The enum values match predefined built-in macros __MEMORY_SCOPE_*.
Definition SyncScope.h:245
ArrayRef< unsigned > getRuntimeValues() const override
Get all possible sync scope values that might be encountered at runtime for the current language.
Definition SyncScope.h:282
SyncScope map(unsigned S) const override
Maps language specific sync scope values to internal SyncScope enum.
Definition SyncScope.h:258
bool isValid(unsigned S) const override
Check if the compile-time constant sync scope value is valid.
Definition SyncScope.h:219
ID
The enum values match the pre-defined macros __HIP_MEMORY_SCOPE_*, which are used to define memory_sc...
Definition SyncScope.h:185
ArrayRef< unsigned > getRuntimeValues() const override
Get all possible sync scope values that might be encountered at runtime for the current language.
Definition SyncScope.h:224
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:234
SyncScope map(unsigned S) const override
Maps language specific sync scope values to internal SyncScope enum.
Definition SyncScope.h:199
Defines the interface for sync scope model.
Definition SyncScope.h:104
static std::unique_ptr< AtomicScopeModel > create(AtomicScopeModelKind K)
Create an atomic scope model by AtomicScopeModelKind.
Definition SyncScope.h:298
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:106
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:135
ArrayRef< unsigned > getRuntimeValues() const override
Get all possible sync scope values that might be encountered at runtime for the current language.
Definition SyncScope.h:164
bool isValid(unsigned S) const override
Check if the compile-time constant sync scope value is valid.
Definition SyncScope.h:159
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:172
SyncScope map(unsigned S) const override
Maps language specific sync scope values to internal SyncScope enum.
Definition SyncScope.h:145
The JSON file list parser is used to communicate input to InstallAPI.
AtomicScopeModelKind
Defines the kind of atomic scope models.
Definition SyncScope.h:101
SyncScope
Defines sync scope values used internally by clang.
Definition SyncScope.h:42
llvm::StringRef getAsString(SyncScope S)
Definition SyncScope.h:62
@ Generic
not a target-specific vector type
Definition TypeBase.h:4136
@ None
The alignment was not explicit in code.
Definition ASTContext.h:178