clang 19.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 synch 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 synch scope values and mapping them to LLVM
28/// synch scopes. Their numerical values may be different from the corresponding
29/// synch scope enums used in source languages.
30///
31/// In atomic builtin and expressions, language-specific synch 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 synch scope enums. The language-specific synch 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///
42enum class SyncScope {
58};
59
60inline llvm::StringRef getAsString(SyncScope S) {
61 switch (S) {
63 return "system_scope";
65 return "device_scope";
67 return "workgroup_scope";
69 return "wavefront_scope";
71 return "single_scope";
73 return "hip_singlethread";
75 return "hip_wavefront";
77 return "hip_workgroup";
79 return "hip_agent";
81 return "hip_system";
83 return "opencl_workgroup";
85 return "opencl_device";
87 return "opencl_allsvmdevices";
89 return "opencl_subgroup";
90 }
91 llvm_unreachable("Invalid synch scope");
92}
93
94/// Defines the kind of atomic scope models.
96
97/// Defines the interface for synch scope model.
99public:
100 virtual ~AtomicScopeModel() {}
101 /// Maps language specific synch scope values to internal
102 /// SyncScope enum.
103 virtual SyncScope map(unsigned S) const = 0;
104
105 /// Check if the compile-time constant synch scope value
106 /// is valid.
107 virtual bool isValid(unsigned S) const = 0;
108
109 /// Get all possible synch scope values that might be
110 /// encountered at runtime for the current language.
112
113 /// If atomic builtin function is called with invalid
114 /// synch scope value at runtime, it will fall back to a valid
115 /// synch scope value returned by this function.
116 virtual unsigned getFallBackValue() const = 0;
117
118 /// Create an atomic scope model by AtomicScopeModelKind.
119 /// \return an empty std::unique_ptr for AtomicScopeModelKind::None.
120 static std::unique_ptr<AtomicScopeModel> create(AtomicScopeModelKind K);
121};
122
123/// Defines the synch scope model for OpenCL.
125public:
126 /// The enum values match the pre-defined macros
127 /// __OPENCL_MEMORY_SCOPE_*, which are used to define memory_scope_*
128 /// enums in opencl-c-base.h.
129 enum ID {
134 Last = SubGroup
135 };
136
138
139 SyncScope map(unsigned S) const override {
140 switch (static_cast<ID>(S)) {
141 case WorkGroup:
143 case Device:
145 case AllSVMDevices:
147 case SubGroup:
149 }
150 llvm_unreachable("Invalid language synch scope value");
151 }
152
153 bool isValid(unsigned S) const override {
154 return S >= static_cast<unsigned>(WorkGroup) &&
155 S <= static_cast<unsigned>(Last);
156 }
157
159 static_assert(Last == SubGroup, "Does not include all synch scopes");
160 static const unsigned Scopes[] = {
161 static_cast<unsigned>(WorkGroup), static_cast<unsigned>(Device),
162 static_cast<unsigned>(AllSVMDevices), static_cast<unsigned>(SubGroup)};
163 return llvm::ArrayRef(Scopes);
164 }
165
166 unsigned getFallBackValue() const override {
167 return static_cast<unsigned>(AllSVMDevices);
168 }
169};
170
171/// Defines the synch scope model for HIP.
173public:
174 /// The enum values match the pre-defined macros
175 /// __HIP_MEMORY_SCOPE_*, which are used to define memory_scope_*
176 /// enums in hip-c.h.
177 enum ID {
181 Agent = 4,
183 Last = System
184 };
185
187
188 SyncScope map(unsigned S) const override {
189 switch (static_cast<ID>(S)) {
190 case SingleThread:
192 case Wavefront:
194 case Workgroup:
196 case Agent:
197 return SyncScope::HIPAgent;
198 case System:
200 }
201 llvm_unreachable("Invalid language synch scope value");
202 }
203
204 bool isValid(unsigned S) const override {
205 return S >= static_cast<unsigned>(SingleThread) &&
206 S <= static_cast<unsigned>(Last);
207 }
208
210 static_assert(Last == System, "Does not include all synch scopes");
211 static const unsigned Scopes[] = {
212 static_cast<unsigned>(SingleThread), static_cast<unsigned>(Wavefront),
213 static_cast<unsigned>(Workgroup), static_cast<unsigned>(Agent),
214 static_cast<unsigned>(System)};
215 return llvm::ArrayRef(Scopes);
216 }
217
218 unsigned getFallBackValue() const override {
219 return static_cast<unsigned>(System);
220 }
221};
222
223/// Defines the generic atomic scope model.
225public:
226 /// The enum values match predefined built-in macros __ATOMIC_SCOPE_*.
227 enum ID {
233 Last = Single
234 };
235
237
238 SyncScope map(unsigned S) const override {
239 switch (static_cast<ID>(S)) {
240 case Device:
242 case System:
244 case Workgroup:
246 case Wavefront:
248 case Single:
250 }
251 llvm_unreachable("Invalid language sync scope value");
252 }
253
254 bool isValid(unsigned S) const override {
255 return S >= static_cast<unsigned>(System) &&
256 S <= static_cast<unsigned>(Last);
257 }
258
260 static_assert(Last == Single, "Does not include all sync scopes");
261 static const unsigned Scopes[] = {
262 static_cast<unsigned>(Device), static_cast<unsigned>(System),
263 static_cast<unsigned>(Workgroup), static_cast<unsigned>(Wavefront),
264 static_cast<unsigned>(Single)};
265 return llvm::ArrayRef(Scopes);
266 }
267
268 unsigned getFallBackValue() const override {
269 return static_cast<unsigned>(System);
270 }
271};
272
273inline std::unique_ptr<AtomicScopeModel>
275 switch (K) {
277 return std::unique_ptr<AtomicScopeModel>{};
279 return std::make_unique<AtomicScopeOpenCLModel>();
281 return std::make_unique<AtomicScopeHIPModel>();
283 return std::make_unique<AtomicScopeGenericModel>();
284 }
285 llvm_unreachable("Invalid atomic scope model kind");
286}
287} // namespace clang
288
289#endif
Defines the clang::LangOptions interface.
Defines the generic atomic scope model.
Definition: SyncScope.h:224
bool isValid(unsigned S) const override
Check if the compile-time constant synch scope value is valid.
Definition: SyncScope.h:254
unsigned getFallBackValue() const override
If atomic builtin function is called with invalid synch scope value at runtime, it will fall back to ...
Definition: SyncScope.h:268
ID
The enum values match predefined built-in macros __ATOMIC_SCOPE_*.
Definition: SyncScope.h:227
ArrayRef< unsigned > getRuntimeValues() const override
Get all possible synch scope values that might be encountered at runtime for the current language.
Definition: SyncScope.h:259
SyncScope map(unsigned S) const override
Maps language specific synch scope values to internal SyncScope enum.
Definition: SyncScope.h:238
Defines the synch scope model for HIP.
Definition: SyncScope.h:172
bool isValid(unsigned S) const override
Check if the compile-time constant synch scope value is valid.
Definition: SyncScope.h:204
ID
The enum values match the pre-defined macros __HIP_MEMORY_SCOPE_*, which are used to define memory_sc...
Definition: SyncScope.h:177
ArrayRef< unsigned > getRuntimeValues() const override
Get all possible synch scope values that might be encountered at runtime for the current language.
Definition: SyncScope.h:209
unsigned getFallBackValue() const override
If atomic builtin function is called with invalid synch scope value at runtime, it will fall back to ...
Definition: SyncScope.h:218
SyncScope map(unsigned S) const override
Maps language specific synch scope values to internal SyncScope enum.
Definition: SyncScope.h:188
Defines the interface for synch scope model.
Definition: SyncScope.h:98
static std::unique_ptr< AtomicScopeModel > create(AtomicScopeModelKind K)
Create an atomic scope model by AtomicScopeModelKind.
Definition: SyncScope.h:274
virtual ArrayRef< unsigned > getRuntimeValues() const =0
Get all possible synch scope values that might be encountered at runtime for the current language.
virtual SyncScope map(unsigned S) const =0
Maps language specific synch scope values to internal SyncScope enum.
virtual unsigned getFallBackValue() const =0
If atomic builtin function is called with invalid synch scope value at runtime, it will fall back to ...
virtual ~AtomicScopeModel()
Definition: SyncScope.h:100
virtual bool isValid(unsigned S) const =0
Check if the compile-time constant synch scope value is valid.
Defines the synch scope model for OpenCL.
Definition: SyncScope.h:124
ID
The enum values match the pre-defined macros __OPENCL_MEMORY_SCOPE_*, which are used to define memory...
Definition: SyncScope.h:129
ArrayRef< unsigned > getRuntimeValues() const override
Get all possible synch scope values that might be encountered at runtime for the current language.
Definition: SyncScope.h:158
bool isValid(unsigned S) const override
Check if the compile-time constant synch scope value is valid.
Definition: SyncScope.h:153
unsigned getFallBackValue() const override
If atomic builtin function is called with invalid synch scope value at runtime, it will fall back to ...
Definition: SyncScope.h:166
SyncScope map(unsigned S) const override
Maps language specific synch scope values to internal SyncScope enum.
Definition: SyncScope.h:139
The JSON file list parser is used to communicate input to InstallAPI.
@ OpenCL
Definition: LangStandard.h:64
AtomicScopeModelKind
Defines the kind of atomic scope models.
Definition: SyncScope.h:95
SyncScope
Defines synch scope values used internally by clang.
Definition: SyncScope.h:42
llvm::StringRef getAsString(SyncScope S)
Definition: SyncScope.h:60
@ Generic
not a target-specific vector type
@ None
The alignment was not explicit in code.