clang  15.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 
22 namespace 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 ///
42 enum class SyncScope {
46  HIPAgent,
47  HIPSystem,
53 };
54 
55 inline llvm::StringRef getAsString(SyncScope S) {
56  switch (S) {
58  return "hip_singlethread";
60  return "hip_wavefront";
62  return "hip_workgroup";
64  return "hip_agent";
66  return "hip_system";
68  return "opencl_workgroup";
70  return "opencl_device";
72  return "opencl_allsvmdevices";
74  return "opencl_subgroup";
75  }
76  llvm_unreachable("Invalid synch scope");
77 }
78 
79 /// Defines the kind of atomic scope models.
81 
82 /// Defines the interface for synch scope model.
84 public:
85  virtual ~AtomicScopeModel() {}
86  /// Maps language specific synch scope values to internal
87  /// SyncScope enum.
88  virtual SyncScope map(unsigned S) const = 0;
89 
90  /// Check if the compile-time constant synch scope value
91  /// is valid.
92  virtual bool isValid(unsigned S) const = 0;
93 
94  /// Get all possible synch scope values that might be
95  /// encountered at runtime for the current language.
96  virtual ArrayRef<unsigned> getRuntimeValues() const = 0;
97 
98  /// If atomic builtin function is called with invalid
99  /// synch scope value at runtime, it will fall back to a valid
100  /// synch scope value returned by this function.
101  virtual unsigned getFallBackValue() const = 0;
102 
103  /// Create an atomic scope model by AtomicScopeModelKind.
104  /// \return an empty std::unique_ptr for AtomicScopeModelKind::None.
105  static std::unique_ptr<AtomicScopeModel> create(AtomicScopeModelKind K);
106 };
107 
108 /// Defines the synch scope model for OpenCL.
110 public:
111  /// The enum values match the pre-defined macros
112  /// __OPENCL_MEMORY_SCOPE_*, which are used to define memory_scope_*
113  /// enums in opencl-c-base.h.
114  enum ID {
116  Device = 2,
118  SubGroup = 4,
120  };
121 
123 
124  SyncScope map(unsigned S) const override {
125  switch (static_cast<ID>(S)) {
126  case WorkGroup:
128  case Device:
130  case AllSVMDevices:
132  case SubGroup:
134  }
135  llvm_unreachable("Invalid language synch scope value");
136  }
137 
138  bool isValid(unsigned S) const override {
139  return S >= static_cast<unsigned>(WorkGroup) &&
140  S <= static_cast<unsigned>(Last);
141  }
142 
144  static_assert(Last == SubGroup, "Does not include all synch scopes");
145  static const unsigned Scopes[] = {
146  static_cast<unsigned>(WorkGroup), static_cast<unsigned>(Device),
147  static_cast<unsigned>(AllSVMDevices), static_cast<unsigned>(SubGroup)};
148  return llvm::makeArrayRef(Scopes);
149  }
150 
151  unsigned getFallBackValue() const override {
152  return static_cast<unsigned>(AllSVMDevices);
153  }
154 };
155 
156 /// Defines the synch scope model for HIP.
158 public:
159  /// The enum values match the pre-defined macros
160  /// __HIP_MEMORY_SCOPE_*, which are used to define memory_scope_*
161  /// enums in hip-c.h.
162  enum ID {
166  Agent = 4,
167  System = 5,
169  };
170 
172 
173  SyncScope map(unsigned S) const override {
174  switch (static_cast<ID>(S)) {
175  case SingleThread:
177  case Wavefront:
179  case Workgroup:
181  case Agent:
182  return SyncScope::HIPAgent;
183  case System:
184  return SyncScope::HIPSystem;
185  }
186  llvm_unreachable("Invalid language synch scope value");
187  }
188 
189  bool isValid(unsigned S) const override {
190  return S >= static_cast<unsigned>(SingleThread) &&
191  S <= static_cast<unsigned>(Last);
192  }
193 
195  static_assert(Last == System, "Does not include all synch scopes");
196  static const unsigned Scopes[] = {
197  static_cast<unsigned>(SingleThread), static_cast<unsigned>(Wavefront),
198  static_cast<unsigned>(Workgroup), static_cast<unsigned>(Agent),
199  static_cast<unsigned>(System)};
200  return llvm::makeArrayRef(Scopes);
201  }
202 
203  unsigned getFallBackValue() const override {
204  return static_cast<unsigned>(System);
205  }
206 };
207 
208 inline std::unique_ptr<AtomicScopeModel>
210  switch (K) {
212  return std::unique_ptr<AtomicScopeModel>{};
214  return std::make_unique<AtomicScopeOpenCLModel>();
216  return std::make_unique<AtomicScopeHIPModel>();
217  }
218  llvm_unreachable("Invalid atomic scope model kind");
219 }
220 } // namespace clang
221 
222 #endif
clang::AtomicScopeHIPModel::Last
@ Last
Definition: SyncScope.h:168
clang::AtomicScopeHIPModel::Wavefront
@ Wavefront
Definition: SyncScope.h:164
clang::AtomicScopeOpenCLModel::WorkGroup
@ WorkGroup
Definition: SyncScope.h:115
clang::SyncScope::OpenCLWorkGroup
@ OpenCLWorkGroup
clang::AtomicScopeOpenCLModel
Defines the synch scope model for OpenCL.
Definition: SyncScope.h:109
clang::AtomicScopeHIPModel::getFallBackValue
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:203
clang::AtomicScopeModel::create
static std::unique_ptr< AtomicScopeModel > create(AtomicScopeModelKind K)
Create an atomic scope model by AtomicScopeModelKind.
Definition: SyncScope.h:209
clang::SyncScope::HIPAgent
@ HIPAgent
clang::AtomicScopeOpenCLModel::SubGroup
@ SubGroup
Definition: SyncScope.h:118
clang::AtomicScopeHIPModel::Workgroup
@ Workgroup
Definition: SyncScope.h:165
clang::AtomicScopeOpenCLModel::getRuntimeValues
ArrayRef< unsigned > getRuntimeValues() const override
Get all possible synch scope values that might be encountered at runtime for the current language.
Definition: SyncScope.h:143
clang::SyncScope::HIPWorkgroup
@ HIPWorkgroup
clang::AtomicScopeModelKind::OpenCL
@ OpenCL
clang::getAsString
llvm::StringRef getAsString(SyncScope S)
Definition: SyncScope.h:55
clang::SyncScope::HIPSingleThread
@ HIPSingleThread
clang::AtomicScopeHIPModel::System
@ System
Definition: SyncScope.h:167
clang::AtomicScopeHIPModel::AtomicScopeHIPModel
AtomicScopeHIPModel()
Definition: SyncScope.h:171
LangOptions.h
clang::AtomicScopeModel::getFallBackValue
virtual unsigned getFallBackValue() const =0
If atomic builtin function is called with invalid synch scope value at runtime, it will fall back to ...
clang::AtomicScopeModel
Defines the interface for synch scope model.
Definition: SyncScope.h:83
clang::SyncScope
SyncScope
Defines synch scope values used internally by clang.
Definition: SyncScope.h:42
clang::AtomicScopeOpenCLModel::isValid
bool isValid(unsigned S) const override
Check if the compile-time constant synch scope value is valid.
Definition: SyncScope.h:138
clang::AtomicScopeModel::~AtomicScopeModel
virtual ~AtomicScopeModel()
Definition: SyncScope.h:85
clang::AtomicScopeOpenCLModel::ID
ID
The enum values match the pre-defined macros __OPENCL_MEMORY_SCOPE_*, which are used to define memory...
Definition: SyncScope.h:114
clang::SyncScope::HIPWavefront
@ HIPWavefront
clang::SyncScope::HIPSystem
@ HIPSystem
clang::SyncScope::OpenCLAllSVMDevices
@ OpenCLAllSVMDevices
clang::AtomicScopeHIPModel::isValid
bool isValid(unsigned S) const override
Check if the compile-time constant synch scope value is valid.
Definition: SyncScope.h:189
clang::AtomicScopeModelKind::HIP
@ HIP
clang::AtomicScopeModel::isValid
virtual bool isValid(unsigned S) const =0
Check if the compile-time constant synch scope value is valid.
clang::SyncScope::OpenCLDevice
@ OpenCLDevice
llvm::ArrayRef< unsigned >
clang::AtomicScopeOpenCLModel::Last
@ Last
Definition: SyncScope.h:119
clang::AtomicScopeModelKind::None
@ None
clang::AtomicScopeModel::getRuntimeValues
virtual ArrayRef< unsigned > getRuntimeValues() const =0
Get all possible synch scope values that might be encountered at runtime for the current language.
clang::AtomicScopeHIPModel
Defines the synch scope model for HIP.
Definition: SyncScope.h:157
clang::AtomicScopeHIPModel::getRuntimeValues
ArrayRef< unsigned > getRuntimeValues() const override
Get all possible synch scope values that might be encountered at runtime for the current language.
Definition: SyncScope.h:194
clang::AtomicScopeHIPModel::SingleThread
@ SingleThread
Definition: SyncScope.h:163
clang::AtomicScopeOpenCLModel::AllSVMDevices
@ AllSVMDevices
Definition: SyncScope.h:117
clang::AtomicScopeHIPModel::ID
ID
The enum values match the pre-defined macros __HIP_MEMORY_SCOPE_*, which are used to define memory_sc...
Definition: SyncScope.h:162
clang
Definition: CalledOnceCheck.h:17
clang::AtomicScopeModel::map
virtual SyncScope map(unsigned S) const =0
Maps language specific synch scope values to internal SyncScope enum.
clang::AtomicScopeHIPModel::map
SyncScope map(unsigned S) const override
Maps language specific synch scope values to internal SyncScope enum.
Definition: SyncScope.h:173
clang::AtomicScopeOpenCLModel::Device
@ Device
Definition: SyncScope.h:116
clang::SyncScope::OpenCLSubGroup
@ OpenCLSubGroup
clang::AtomicScopeModelKind
AtomicScopeModelKind
Defines the kind of atomic scope models.
Definition: SyncScope.h:80
clang::AtomicScopeOpenCLModel::getFallBackValue
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:151
clang::AtomicScopeHIPModel::Agent
@ Agent
Definition: SyncScope.h:166
clang::SyncScope::Last
@ Last
clang::AtomicScopeOpenCLModel::AtomicScopeOpenCLModel
AtomicScopeOpenCLModel()
Definition: SyncScope.h:122
clang::AtomicScopeOpenCLModel::map
SyncScope map(unsigned S) const override
Maps language specific synch scope values to internal SyncScope enum.
Definition: SyncScope.h:124