LLVM 24.0.0git
OMP.h
Go to the documentation of this file.
1//===-- OMP.h - Core OpenMP definitions and declarations ---------- 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// This file contains the core set of OpenMP definitions and declarations.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_FRONTEND_OPENMP_OMP_H
14#define LLVM_FRONTEND_OPENMP_OMP_H
15
17
18#include "llvm/ADT/ArrayRef.h"
19#include "llvm/ADT/Bitset.h"
22#include "llvm/ADT/Sequence.h"
24#include "llvm/ADT/StringRef.h"
26
27#include "llvm/Frontend/OpenMP/OMP.h.inc"
28
29namespace llvm::omp {
30template <typename Enum, size_t Size> struct EnumSet;
31
32namespace detail {
33template <size_t Size>
34static constexpr inline size_t findFirstSet(size_t Begin, size_t End,
35 const llvm::Bitset<Size> &Set) {
36 unsigned BeginWord = Begin / 64;
37 unsigned EndWord = (End + 63) / 64;
38
39 for (unsigned I = BeginWord; I < EndWord; ++I) {
40 uint64_t Word = Set.getWord64(I);
41 if (I == BeginWord && Begin % 64 != 0) {
42 Word &= ~uint64_t() << (Begin % 64);
43 }
44 auto Count = static_cast<unsigned>(llvm::countr_zero_constexpr(Word));
45 if (Count < 64) {
46 unsigned Idx = I * 64 + Count;
47 if (Idx >= Begin && Idx < End)
48 return Idx;
49 }
50 }
51 return Size;
52}
53
54template <typename Enum, size_t Size> struct EnumSetIterator {
55 using value_type = Enum;
56 static constexpr size_t enum_size = Size;
57
58 constexpr EnumSetIterator(const EnumSet<Enum, Size> &Set, size_t At)
59 : Set(Set), At(At) {}
60
61 constexpr Enum operator*() const;
62 constexpr auto &operator++();
63
64 constexpr bool operator==(const EnumSetIterator<Enum, Size> &Other) const {
65 return &Set == &Other.Set && At == Other.At;
66 }
67 constexpr bool operator!=(const EnumSetIterator<Enum, Size> &Other) const {
68 return !operator==(Other);
69 }
70
71private:
72 const EnumSet<Enum, Size> &Set;
73 size_t At;
74};
75} // namespace detail
76
77template <typename Enum>
78inline constexpr size_t EnumSize =
79 llvm::to_underlying(Enum::Last_) - llvm::to_underlying(Enum::First_) + 1;
80
81template <typename Enum, size_t Size = EnumSize<Enum>>
82struct EnumSet : public llvm::Bitset<Size> {
83 using value_type = Enum;
85 using Base::Base;
87
88 constexpr EnumSet(Base &&B) : Base(std::move(B)) {}
89 constexpr EnumSet(std::initializer_list<value_type> Init) {
90 for (value_type E : Init) {
91 auto Value = static_cast<unsigned>(E);
92 assert(Value < Base::size() && "Invalid enumeration value");
94 }
95 }
96
97 constexpr bool empty() const { return Base::none(); }
98 constexpr size_t size() const { return Base::count(); }
99 constexpr size_t max_size() const { return Size; }
100
101 constexpr bool test(Enum E) const {
102 return Base::test(static_cast<unsigned>(E));
103 }
104 constexpr bool operator[](Enum E) const {
105 return Base::operator[](static_cast<unsigned>(E));
106 }
107 constexpr EnumSet &flip(Enum E) {
108 Base::flip(static_cast<unsigned>(E));
109 return *this;
110 }
111 constexpr EnumSet &reset(Enum E) {
112 Base::reset(static_cast<unsigned>(E));
113 return *this;
114 }
115 constexpr EnumSet &set(Enum E) {
116 Base::set(static_cast<unsigned>(E));
117 return *this;
118 }
119
120 constexpr EnumSet &operator|=(const EnumSet &S) {
122 return *this;
123 }
124 constexpr EnumSet &operator&=(const EnumSet &S) {
126 return *this;
127 }
128 constexpr EnumSet operator|(const EnumSet &S) const {
129 EnumSet T{*this};
130 return T |= S;
131 }
132 constexpr EnumSet operator&(const EnumSet &S) const {
133 EnumSet T{*this};
134 return T &= S;
135 }
136
137 constexpr iterator begin() const {
138 return iterator(*this, detail::findFirstSet<Size>(0, Size, *this));
139 }
140 constexpr iterator end() const { return iterator(*this, Size); }
141};
142
143namespace detail {
144template <typename Enum, size_t Size>
146 // Older gcc doesn't like assert(Set.Base::test(At));
147 assert((static_cast<const llvm::Bitset<Size> &>(Set).test(At)));
148 return static_cast<Enum>(At);
149}
150
151template <typename Enum, size_t Size>
153 At = findFirstSet<Size>(At + 1, Size, Set);
154 return *this;
155}
156} // namespace detail
157
160
163
166
168
172
173static constexpr inline auto clauses() {
174 return llvm::enum_seq_inclusive(Clause::First_, Clause::Last_);
175}
176
177static constexpr inline auto directives() {
178 return llvm::enum_seq_inclusive(Directive::First_, Directive::Last_);
179}
180
181/// Can clause C have an iterator-modifier.
182static constexpr inline bool canHaveIterator(Clause C) {
183 // [5.2:67:5]
184 switch (C) {
185 case OMPC_affinity:
186 case OMPC_depend:
187 case OMPC_from:
188 case OMPC_map:
189 case OMPC_to:
190 return true;
191 default:
192 return false;
193 }
194}
195
196// Can clause C create a private copy of a variable.
197static constexpr inline bool isPrivatizingClause(Clause C, Version V) {
198 switch (C) {
199 case OMPC_firstprivate:
200 case OMPC_in_reduction:
201 case OMPC_lastprivate:
202 case OMPC_linear:
203 case OMPC_private:
204 case OMPC_reduction:
205 case OMPC_task_reduction:
206 return true;
207 case OMPC_detach:
208 case OMPC_induction:
209 case OMPC_is_device_ptr:
210 case OMPC_use_device_ptr:
211 return V >= 60;
212 default:
213 return false;
214 }
215}
216
217static constexpr inline bool isDataSharingAttributeClause(Clause C, Version V) {
218 // The "Version" parameter is in case the result is version-depenent
219 // in the future.
220 (void)V;
221 switch (C) {
222 case OMPC_detach:
223 case OMPC_firstprivate:
224 case OMPC_has_device_addr:
225 case OMPC_induction:
226 case OMPC_in_reduction:
227 case OMPC_is_device_ptr:
228 case OMPC_lastprivate:
229 case OMPC_linear:
230 case OMPC_private:
231 case OMPC_reduction:
232 case OMPC_shared:
233 case OMPC_task_reduction:
234 case OMPC_use_device_addr:
235 case OMPC_use_device_ptr:
236 case OMPC_uses_allocators:
237 return true;
238 default:
239 return false;
240 }
241}
242
243static constexpr inline bool isEndClause(Clause C) {
244 switch (C) {
245 case OMPC_copyprivate:
246 case OMPC_nowait:
247 return true;
248 default:
249 return false;
250 }
251}
252
253static constexpr Version FallbackVersion(52);
255
256/// Can directive D, under some circumstances, create a private copy
257/// of a variable in given OpenMP version?
259
261
262/// Create a nicer version of a function name for humans to look at.
263LLVM_ABI std::string prettifyFunctionName(StringRef FunctionName);
264
265/// Deconstruct an OpenMP kernel name into the parent function name and the line
266/// number.
267LLVM_ABI std::string deconstructOpenMPKernelName(StringRef KernelName,
268 unsigned &LineNo);
269
270} // namespace llvm::omp
271
272#endif // LLVM_FRONTEND_OPENMP_OMP_H
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
unsigned uint64_t
static GCRegistry::Add< ShadowStackGC > C("shadow-stack", "Very portable GC for uncooperative code generators")
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
#define LLVM_ABI
Definition Compiler.h:215
This file defines DenseMapInfo traits for DenseMap.
#define I(x, y, z)
Definition MD5.cpp:57
#define T
modulo schedule test
This file contains library features backported from future STL versions.
Provides some synthesis utilities to produce sequences of values.
This file defines the SmallVector class.
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
This is a constexpr reimplementation of a subset of std::bitset.
Definition Bitset.h:30
constexpr size_t count() const
Definition Bitset.h:130
constexpr Bitset & flip(unsigned I)
Definition Bitset.h:99
constexpr Bitset & set()
Definition Bitset.h:81
constexpr bool none() const
Definition Bitset.h:120
constexpr bool operator[](unsigned I) const
Definition Bitset.h:104
constexpr size_t size() const
Definition Bitset.h:111
constexpr Bitset & operator|=(const Bitset &RHS)
Definition Bitset.h:160
constexpr Bitset & operator&=(const Bitset &RHS)
Definition Bitset.h:149
constexpr bool test(unsigned I) const
Definition Bitset.h:109
constexpr Bitset & reset(unsigned I)
Definition Bitset.h:94
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
LLVM Value Representation.
Definition Value.h:75
static constexpr size_t findFirstSet(size_t Begin, size_t End, const llvm::Bitset< Size > &Set)
Definition OMP.h:34
static constexpr auto clauses()
Definition OMP.h:173
LLVM_ABI bool isCombinedConstruct(Directive D)
Definition OMP.cpp:205
LLVM_ABI std::string deconstructOpenMPKernelName(StringRef KernelName, unsigned &LineNo)
Deconstruct an OpenMP kernel name into the parent function name and the line number.
Definition OMP.cpp:249
static constexpr bool isEndClause(Clause C)
Definition OMP.h:243
EnumSet< Directive > Directives
Definition OMP.h:159
LLVM_ABI ArrayRef< Directive > getLeafOrCompositeConstructs(Directive D, SmallVectorImpl< Directive > &Output)
Definition OMP.cpp:117
static constexpr bool canHaveIterator(Clause C)
Can clause C have an iterator-modifier.
Definition OMP.h:182
LLVM_ABI bool isCompositeConstruct(Directive D)
Definition OMP.cpp:197
static constexpr Version FallbackVersion(52)
LLVM_ABI bool isPrivatizingConstruct(Directive D, Version V)
Can directive D, under some circumstances, create a private copy of a variable in given OpenMP versio...
Definition OMP.cpp:217
EnumSet< Clause > Clauses
Definition OMP.h:158
LLVM_ABI Directive getCompoundConstruct(ArrayRef< Directive > Parts)
Definition OMP.cpp:144
LLVM_ABI ArrayRef< Version > getOpenMPVersions()
Definition OMP.cpp:211
LLVM_ABI ArrayRef< StringRef > getReservedLocatorNames()
Definition OMP.cpp:229
LLVM_ABI bool isLeafConstruct(Directive D)
Definition OMP.cpp:195
LLVM_ABI ArrayRef< Directive > getLeafConstructsOrSelf(Directive D)
Definition OMP.cpp:106
static constexpr bool isPrivatizingClause(Clause C, Version V)
Definition OMP.h:197
constexpr size_t EnumSize
Definition OMP.h:78
LLVM_ABI std::string prettifyFunctionName(StringRef FunctionName)
Create a nicer version of a function name for humans to look at.
Definition OMP.cpp:235
static constexpr auto directives()
Definition OMP.h:177
LLVM_ABI ArrayRef< Directive > getLeafConstructs(Directive D)
Definition OMP.cpp:98
static constexpr bool isDataSharingAttributeClause(Clause C, Version V)
Definition OMP.h:217
constexpr std::underlying_type_t< Enum > to_underlying(Enum E)
Returns underlying integer value of an enum.
constexpr auto enum_seq_inclusive(EnumT Begin, EnumT End)
Iterate over an enum type from Begin to End inclusive.
Definition Sequence.h:401
@ Other
Any other memory.
Definition ModRef.h:68
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Count
Definition InstrProf.h:145
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1933
constexpr int countr_zero_constexpr(T Val)
Count number of 0's from the least significant bit to the most stopping at the first 1.
Definition bit.h:190
@ End
Run the machine verifier once, at the end of the pipeline.
Definition CodeGen.h:267
Implement std::hash so that hash_code can be used in STL containers.
Definition BitVector.h:878
constexpr bool operator[](Enum E) const
Definition OMP.h:104
detail::EnumSetIterator< Enum, Size > iterator
Definition OMP.h:86
constexpr EnumSet(std::initializer_list< value_type > Init)
Definition OMP.h:89
constexpr size_t max_size() const
Definition OMP.h:99
constexpr bool test(Enum E) const
Definition OMP.h:101
constexpr EnumSet(Base &&B)
Definition OMP.h:88
constexpr EnumSet & set(Enum E)
Definition OMP.h:115
constexpr EnumSet & reset(Enum E)
Definition OMP.h:111
constexpr EnumSet & operator|=(const EnumSet &S)
Definition OMP.h:120
constexpr iterator begin() const
Definition OMP.h:137
constexpr EnumSet operator&(const EnumSet &S) const
Definition OMP.h:132
constexpr size_t size() const
Definition OMP.h:98
constexpr EnumSet & operator&=(const EnumSet &S)
Definition OMP.h:124
constexpr bool empty() const
Definition OMP.h:97
Enum value_type
Definition OMP.h:83
constexpr EnumSet operator|(const EnumSet &S) const
Definition OMP.h:128
llvm::Bitset< Size > Base
Definition OMP.h:84
constexpr EnumSet & flip(Enum E)
Definition OMP.h:107
constexpr iterator end() const
Definition OMP.h:140
constexpr bool operator==(const EnumSetIterator< Enum, Size > &Other) const
Definition OMP.h:64
static constexpr size_t enum_size
Definition OMP.h:56
constexpr Enum operator*() const
Definition OMP.h:145
constexpr auto & operator++()
Definition OMP.h:152
constexpr EnumSetIterator(const EnumSet< Enum, Size > &Set, size_t At)
Definition OMP.h:58
constexpr bool operator!=(const EnumSetIterator< Enum, Size > &Other) const
Definition OMP.h:67