[sanitizer] Improve FreeBSD ASLR detection
[llvm-project.git] / openmp / libomptarget / DeviceRTL / include / State.h
blobc860bd1b98b8a310979566072caa9abd4ae10025
1 //===-------- State.h - OpenMP State & ICV interface ------------- 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 //
10 //===----------------------------------------------------------------------===//
12 #ifndef OMPTARGET_STATE_H
13 #define OMPTARGET_STATE_H
15 #include "Debug.h"
16 #include "Types.h"
18 #pragma omp declare target
20 namespace _OMP {
22 namespace state {
24 inline constexpr uint32_t SharedScratchpadSize = SHARED_SCRATCHPAD_SIZE;
26 /// Initialize the state machinery. Must be called by all threads.
27 void init(bool IsSPMD);
29 /// TODO
30 enum ValueKind {
31 VK_NThreads,
32 VK_Level,
33 VK_ActiveLevel,
34 VK_MaxActiveLevels,
35 VK_RunSched,
36 // ---
37 VK_RunSchedChunk,
38 VK_ParallelRegionFn,
39 VK_ParallelTeamSize,
42 /// TODO
43 void enterDataEnvironment();
45 /// TODO
46 void exitDataEnvironment();
48 /// TODO
49 struct DateEnvironmentRAII {
50 DateEnvironmentRAII() { enterDataEnvironment(); }
51 ~DateEnvironmentRAII() { exitDataEnvironment(); }
54 /// TODO
55 void resetStateForThread(uint32_t TId);
57 uint32_t &lookup32(ValueKind VK, bool IsReadonly);
58 void *&lookupPtr(ValueKind VK, bool IsReadonly);
60 /// A class without actual state used to provide a nice interface to lookup and
61 /// update ICV values we can declare in global scope.
62 template <typename Ty, ValueKind Kind> struct Value {
63 __attribute__((flatten, always_inline)) operator Ty() {
64 return lookup(/* IsReadonly */ true);
67 __attribute__((flatten, always_inline)) Value &operator=(const Ty &Other) {
68 set(Other);
69 return *this;
72 __attribute__((flatten, always_inline)) Value &operator++() {
73 inc(1);
74 return *this;
77 __attribute__((flatten, always_inline)) Value &operator--() {
78 inc(-1);
79 return *this;
82 private:
83 Ty &lookup(bool IsReadonly) {
84 Ty &t = lookup32(Kind, IsReadonly);
85 return t;
88 Ty &inc(int UpdateVal) {
89 return (lookup(/* IsReadonly */ false) += UpdateVal);
92 Ty &set(Ty UpdateVal) { return (lookup(/* IsReadonly */ false) = UpdateVal); }
94 template <typename VTy, typename Ty2> friend struct ValueRAII;
97 /// A mookup class without actual state used to provide
98 /// a nice interface to lookup and update ICV values
99 /// we can declare in global scope.
100 template <typename Ty, ValueKind Kind> struct PtrValue {
101 __attribute__((flatten, always_inline)) operator Ty() {
102 return lookup(/* IsReadonly */ true);
105 __attribute__((flatten, always_inline)) PtrValue &operator=(const Ty Other) {
106 set(Other);
107 return *this;
110 private:
111 Ty &lookup(bool IsReadonly) { return lookupPtr(Kind, IsReadonly); }
113 Ty &set(Ty UpdateVal) { return (lookup(/* IsReadonly */ false) = UpdateVal); }
115 template <typename VTy, typename Ty2> friend struct ValueRAII;
118 template <typename VTy, typename Ty> struct ValueRAII {
119 ValueRAII(VTy &V, Ty NewValue, Ty OldValue, bool Active)
120 : Ptr(Active ? V.lookup(/* IsReadonly */ false) : Val), Val(OldValue),
121 Active(Active) {
122 if (!Active)
123 return;
124 ASSERT(Ptr == OldValue && "ValueRAII initialization with wrong old value!");
125 Ptr = NewValue;
127 ~ValueRAII() {
128 if (Active)
129 Ptr = Val;
132 private:
133 Ty &Ptr;
134 Ty Val;
135 bool Active;
138 /// TODO
139 inline state::Value<uint32_t, state::VK_RunSchedChunk> RunSchedChunk;
141 /// TODO
142 inline state::Value<uint32_t, state::VK_ParallelTeamSize> ParallelTeamSize;
144 /// TODO
145 inline state::PtrValue<ParallelRegionFnTy, state::VK_ParallelRegionFn>
146 ParallelRegionFn;
148 void runAndCheckState(void(Func(void)));
150 void assumeInitialState(bool IsSPMD);
152 } // namespace state
154 namespace icv {
156 /// TODO
157 inline state::Value<uint32_t, state::VK_NThreads> NThreads;
159 /// TODO
160 inline state::Value<uint32_t, state::VK_Level> Level;
162 /// The `active-level` describes which of the parallel level counted with the
163 /// `level-var` is active. There can only be one.
165 /// active-level-var is 1, if ActiveLevelVar is not 0, otherweise it is 0.
166 inline state::Value<uint32_t, state::VK_ActiveLevel> ActiveLevel;
168 /// TODO
169 inline state::Value<uint32_t, state::VK_MaxActiveLevels> MaxActiveLevels;
171 /// TODO
172 inline state::Value<uint32_t, state::VK_RunSched> RunSched;
174 } // namespace icv
176 namespace memory {
178 /// Alloca \p Size bytes in shared memory, if possible, for \p Reason.
180 /// Note: See the restrictions on __kmpc_alloc_shared for proper usage.
181 void *allocShared(uint64_t Size, const char *Reason);
183 /// Free \p Ptr, alloated via allocShared, for \p Reason.
185 /// Note: See the restrictions on __kmpc_free_shared for proper usage.
186 void freeShared(void *Ptr, uint64_t Bytes, const char *Reason);
188 /// Alloca \p Size bytes in global memory, if possible, for \p Reason.
189 void *allocGlobal(uint64_t Size, const char *Reason);
191 /// Return a pointer to the dynamic shared memory buffer.
192 void *getDynamicBuffer();
194 /// Free \p Ptr, alloated via allocGlobal, for \p Reason.
195 void freeGlobal(void *Ptr, const char *Reason);
197 } // namespace memory
199 } // namespace _OMP
201 #pragma omp end declare target
203 #endif