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 //===----------------------------------------------------------------------===// 11 12 #ifndef OMPTARGET_STATE_H 13 #define OMPTARGET_STATE_H 14 15 #include "Debug.h" 16 #include "Types.h" 17 18 #pragma omp declare target 19 20 namespace _OMP { 21 22 namespace state { 23 24 inline constexpr uint32_t SharedScratchpadSize = SHARED_SCRATCHPAD_SIZE; 25 26 /// Initialize the state machinery. Must be called by all threads. 27 void init(bool IsSPMD); 28 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, 40 }; 41 42 /// TODO 43 void enterDataEnvironment(); 44 45 /// TODO 46 void exitDataEnvironment(); 47 48 /// TODO 49 struct DateEnvironmentRAII { 50 DateEnvironmentRAII() { enterDataEnvironment(); } 51 ~DateEnvironmentRAII() { exitDataEnvironment(); } 52 }; 53 54 /// TODO 55 void resetStateForThread(uint32_t TId); 56 57 uint32_t &lookup32(ValueKind VK, bool IsReadonly); 58 void *&lookupPtr(ValueKind VK, bool IsReadonly); 59 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); 65 } 66 67 __attribute__((flatten, always_inline)) Value &operator=(const Ty &Other) { 68 set(Other); 69 return *this; 70 } 71 72 __attribute__((flatten, always_inline)) Value &operator++() { 73 inc(1); 74 return *this; 75 } 76 77 __attribute__((flatten, always_inline)) Value &operator--() { 78 inc(-1); 79 return *this; 80 } 81 82 private: 83 Ty &lookup(bool IsReadonly) { 84 Ty &t = lookup32(Kind, IsReadonly); 85 return t; 86 } 87 88 Ty &inc(int UpdateVal) { 89 return (lookup(/* IsReadonly */ false) += UpdateVal); 90 } 91 92 Ty &set(Ty UpdateVal) { return (lookup(/* IsReadonly */ false) = UpdateVal); } 93 94 template <typename VTy, typename Ty2> friend struct ValueRAII; 95 }; 96 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); 103 } 104 105 __attribute__((flatten, always_inline)) PtrValue &operator=(const Ty Other) { 106 set(Other); 107 return *this; 108 } 109 110 private: 111 Ty &lookup(bool IsReadonly) { return lookupPtr(Kind, IsReadonly); } 112 113 Ty &set(Ty UpdateVal) { return (lookup(/* IsReadonly */ false) = UpdateVal); } 114 115 template <typename VTy, typename Ty2> friend struct ValueRAII; 116 }; 117 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; 126 } 127 ~ValueRAII() { 128 if (Active) 129 Ptr = Val; 130 } 131 132 private: 133 Ty &Ptr; 134 Ty Val; 135 bool Active; 136 }; 137 138 /// TODO 139 inline state::Value<uint32_t, state::VK_RunSchedChunk> RunSchedChunk; 140 141 /// TODO 142 inline state::Value<uint32_t, state::VK_ParallelTeamSize> ParallelTeamSize; 143 144 /// TODO 145 inline state::PtrValue<ParallelRegionFnTy, state::VK_ParallelRegionFn> 146 ParallelRegionFn; 147 148 void runAndCheckState(void(Func(void))); 149 150 void assumeInitialState(bool IsSPMD); 151 152 } // namespace state 153 154 namespace icv { 155 156 /// TODO 157 inline state::Value<uint32_t, state::VK_NThreads> NThreads; 158 159 /// TODO 160 inline state::Value<uint32_t, state::VK_Level> Level; 161 162 /// The `active-level` describes which of the parallel level counted with the 163 /// `level-var` is active. There can only be one. 164 /// 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; 167 168 /// TODO 169 inline state::Value<uint32_t, state::VK_MaxActiveLevels> MaxActiveLevels; 170 171 /// TODO 172 inline state::Value<uint32_t, state::VK_RunSched> RunSched; 173 174 } // namespace icv 175 176 namespace memory { 177 178 /// Alloca \p Size bytes in shared memory, if possible, for \p Reason. 179 /// 180 /// Note: See the restrictions on __kmpc_alloc_shared for proper usage. 181 void *allocShared(uint64_t Size, const char *Reason); 182 183 /// Free \p Ptr, alloated via allocShared, for \p Reason. 184 /// 185 /// Note: See the restrictions on __kmpc_free_shared for proper usage. 186 void freeShared(void *Ptr, uint64_t Bytes, const char *Reason); 187 188 /// Alloca \p Size bytes in global memory, if possible, for \p Reason. 189 void *allocGlobal(uint64_t Size, const char *Reason); 190 191 /// Free \p Ptr, alloated via allocGlobal, for \p Reason. 192 void freeGlobal(void *Ptr, const char *Reason); 193 194 } // namespace memory 195 196 } // namespace _OMP 197 198 #pragma omp end declare target 199 200 #endif 201