clang  14.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 {
48 };
49 
50 inline llvm::StringRef getAsString(SyncScope S) {
51  switch (S) {
53  return "opencl_workgroup";
55  return "opencl_device";
57  return "opencl_allsvmdevices";
59  return "opencl_subgroup";
60  }
61  llvm_unreachable("Invalid synch scope");
62 }
63 
64 /// Defines the kind of atomic scope models.
66 
67 /// Defines the interface for synch scope model.
69 public:
70  virtual ~AtomicScopeModel() {}
71  /// Maps language specific synch scope values to internal
72  /// SyncScope enum.
73  virtual SyncScope map(unsigned S) const = 0;
74 
75  /// Check if the compile-time constant synch scope value
76  /// is valid.
77  virtual bool isValid(unsigned S) const = 0;
78 
79  /// Get all possible synch scope values that might be
80  /// encountered at runtime for the current language.
81  virtual ArrayRef<unsigned> getRuntimeValues() const = 0;
82 
83  /// If atomic builtin function is called with invalid
84  /// synch scope value at runtime, it will fall back to a valid
85  /// synch scope value returned by this function.
86  virtual unsigned getFallBackValue() const = 0;
87 
88  /// Create an atomic scope model by AtomicScopeModelKind.
89  /// \return an empty std::unique_ptr for AtomicScopeModelKind::None.
90  static std::unique_ptr<AtomicScopeModel> create(AtomicScopeModelKind K);
91 };
92 
93 /// Defines the synch scope model for OpenCL.
95 public:
96  /// The enum values match the pre-defined macros
97  /// __OPENCL_MEMORY_SCOPE_*, which are used to define memory_scope_*
98  /// enums in opencl-c-base.h.
99  enum ID {
101  Device = 2,
103  SubGroup = 4,
105  };
106 
108 
109  SyncScope map(unsigned S) const override {
110  switch (static_cast<ID>(S)) {
111  case WorkGroup:
113  case Device:
115  case AllSVMDevices:
117  case SubGroup:
119  }
120  llvm_unreachable("Invalid language synch scope value");
121  }
122 
123  bool isValid(unsigned S) const override {
124  return S >= static_cast<unsigned>(WorkGroup) &&
125  S <= static_cast<unsigned>(Last);
126  }
127 
129  static_assert(Last == SubGroup, "Does not include all synch scopes");
130  static const unsigned Scopes[] = {
131  static_cast<unsigned>(WorkGroup), static_cast<unsigned>(Device),
132  static_cast<unsigned>(AllSVMDevices), static_cast<unsigned>(SubGroup)};
133  return llvm::makeArrayRef(Scopes);
134  }
135 
136  unsigned getFallBackValue() const override {
137  return static_cast<unsigned>(AllSVMDevices);
138  }
139 };
140 
141 inline std::unique_ptr<AtomicScopeModel>
143  switch (K) {
145  return std::unique_ptr<AtomicScopeModel>{};
147  return std::make_unique<AtomicScopeOpenCLModel>();
148  }
149  llvm_unreachable("Invalid atomic scope model kind");
150 }
151 }
152 
153 #endif
clang::AtomicScopeOpenCLModel::WorkGroup
@ WorkGroup
Definition: SyncScope.h:100
clang::SyncScope::OpenCLWorkGroup
@ OpenCLWorkGroup
clang::AtomicScopeOpenCLModel
Defines the synch scope model for OpenCL.
Definition: SyncScope.h:94
clang::AtomicScopeModel::create
static std::unique_ptr< AtomicScopeModel > create(AtomicScopeModelKind K)
Create an atomic scope model by AtomicScopeModelKind.
Definition: SyncScope.h:142
clang::AtomicScopeOpenCLModel::SubGroup
@ SubGroup
Definition: SyncScope.h:103
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:128
clang::AtomicScopeModelKind::OpenCL
@ OpenCL
clang::getAsString
llvm::StringRef getAsString(SyncScope S)
Definition: SyncScope.h:50
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:68
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:123
clang::AtomicScopeModel::~AtomicScopeModel
virtual ~AtomicScopeModel()
Definition: SyncScope.h:70
clang::AtomicScopeOpenCLModel::ID
ID
The enum values match the pre-defined macros __OPENCL_MEMORY_SCOPE_*, which are used to define memory...
Definition: SyncScope.h:99
clang::SyncScope::OpenCLAllSVMDevices
@ OpenCLAllSVMDevices
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:104
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::AtomicScopeOpenCLModel::AllSVMDevices
@ AllSVMDevices
Definition: SyncScope.h:102
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::AtomicScopeOpenCLModel::Device
@ Device
Definition: SyncScope.h:101
clang::SyncScope::OpenCLSubGroup
@ OpenCLSubGroup
clang::AtomicScopeModelKind
AtomicScopeModelKind
Defines the kind of atomic scope models.
Definition: SyncScope.h:65
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:136
clang::SyncScope::Last
@ Last
clang::AtomicScopeOpenCLModel::AtomicScopeOpenCLModel
AtomicScopeOpenCLModel()
Definition: SyncScope.h:107
clang::AtomicScopeOpenCLModel::map
SyncScope map(unsigned S) const override
Maps language specific synch scope values to internal SyncScope enum.
Definition: SyncScope.h:109