Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1 | //===----- CGOpenMPRuntime.h - Interface to OpenMP Runtimes -----*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This provides a class for OpenMP runtime code generation. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Benjamin Kramer | 2f5db8b | 2014-08-13 16:25:19 +0000 | [diff] [blame] | 14 | #ifndef LLVM_CLANG_LIB_CODEGEN_CGOPENMPRUNTIME_H |
| 15 | #define LLVM_CLANG_LIB_CODEGEN_CGOPENMPRUNTIME_H |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 16 | |
Alexey Bataev | 1809571 | 2014-10-10 12:19:54 +0000 | [diff] [blame] | 17 | #include "clang/Basic/SourceLocation.h" |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/DenseMap.h" |
Alexey Bataev | 3a3bf0b | 2014-09-22 10:01:53 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/StringMap.h" |
Alexey Bataev | 1809571 | 2014-10-10 12:19:54 +0000 | [diff] [blame] | 20 | |
| 21 | namespace llvm { |
| 22 | class ArrayType; |
| 23 | class Constant; |
| 24 | class Function; |
| 25 | class FunctionType; |
| 26 | class StructType; |
| 27 | class Type; |
| 28 | class Value; |
| 29 | } // namespace llvm |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 30 | |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 31 | namespace clang { |
| 32 | |
Alexey Bataev | 1809571 | 2014-10-10 12:19:54 +0000 | [diff] [blame] | 33 | class OMPExecutableDirective; |
| 34 | class VarDecl; |
| 35 | |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 36 | namespace CodeGen { |
| 37 | |
Alexey Bataev | 1809571 | 2014-10-10 12:19:54 +0000 | [diff] [blame] | 38 | class CodeGenFunction; |
| 39 | class CodeGenModule; |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 40 | |
| 41 | class CGOpenMPRuntime { |
| 42 | public: |
| 43 | /// \brief Values for bit flags used in the ident_t to describe the fields. |
| 44 | /// All enumeric elements are named and described in accordance with the code |
| 45 | /// from http://llvm.org/svn/llvm-project/openmp/trunk/runtime/src/kmp.h |
| 46 | enum OpenMPLocationFlags { |
| 47 | /// \brief Use trampoline for internal microtask. |
| 48 | OMP_IDENT_IMD = 0x01, |
| 49 | /// \brief Use c-style ident structure. |
| 50 | OMP_IDENT_KMPC = 0x02, |
| 51 | /// \brief Atomic reduction option for kmpc_reduce. |
| 52 | OMP_ATOMIC_REDUCE = 0x10, |
| 53 | /// \brief Explicit 'barrier' directive. |
| 54 | OMP_IDENT_BARRIER_EXPL = 0x20, |
| 55 | /// \brief Implicit barrier in code. |
| 56 | OMP_IDENT_BARRIER_IMPL = 0x40, |
| 57 | /// \brief Implicit barrier in 'for' directive. |
| 58 | OMP_IDENT_BARRIER_IMPL_FOR = 0x40, |
| 59 | /// \brief Implicit barrier in 'sections' directive. |
| 60 | OMP_IDENT_BARRIER_IMPL_SECTIONS = 0xC0, |
| 61 | /// \brief Implicit barrier in 'single' directive. |
| 62 | OMP_IDENT_BARRIER_IMPL_SINGLE = 0x140 |
| 63 | }; |
| 64 | enum OpenMPRTLFunction { |
| 65 | // Call to void __kmpc_fork_call(ident_t *loc, kmp_int32 argc, kmpc_micro |
| 66 | // microtask, ...); |
| 67 | OMPRTL__kmpc_fork_call, |
Alexey Bataev | 3a3bf0b | 2014-09-22 10:01:53 +0000 | [diff] [blame] | 68 | // Call to __kmpc_int32 kmpc_global_thread_num(ident_t *loc); |
| 69 | OMPRTL__kmpc_global_thread_num, |
Alexey Bataev | f947218 | 2014-09-22 12:32:31 +0000 | [diff] [blame] | 70 | // Call to void __kmpc_critical(ident_t *loc, kmp_int32 global_tid, |
| 71 | // kmp_critical_name *crit); |
Alexey Bataev | 3a3bf0b | 2014-09-22 10:01:53 +0000 | [diff] [blame] | 72 | OMPRTL__kmpc_critical, |
Alexey Bataev | f947218 | 2014-09-22 12:32:31 +0000 | [diff] [blame] | 73 | // Call to void __kmpc_end_critical(ident_t *loc, kmp_int32 global_tid, |
| 74 | // kmp_critical_name *crit); |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 75 | OMPRTL__kmpc_end_critical, |
| 76 | // Call to void __kmpc_barrier(ident_t *loc, kmp_int32 global_tid); |
| 77 | OMPRTL__kmpc_barrier |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 78 | }; |
| 79 | |
| 80 | private: |
| 81 | CodeGenModule &CGM; |
| 82 | /// \brief Default const ident_t object used for initialization of all other |
| 83 | /// ident_t objects. |
| 84 | llvm::Constant *DefaultOpenMPPSource; |
Alexey Bataev | 1809571 | 2014-10-10 12:19:54 +0000 | [diff] [blame] | 85 | /// \brief Map of flags and corresponding default locations. |
Alexey Bataev | 15007ba | 2014-05-07 06:18:01 +0000 | [diff] [blame] | 86 | typedef llvm::DenseMap<unsigned, llvm::Value *> OpenMPDefaultLocMapTy; |
| 87 | OpenMPDefaultLocMapTy OpenMPDefaultLocMap; |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 88 | llvm::Value *GetOrCreateDefaultOpenMPLocation(OpenMPLocationFlags Flags); |
| 89 | /// \brief Describes ident structure that describes a source location. |
| 90 | /// All descriptions are taken from |
| 91 | /// http://llvm.org/svn/llvm-project/openmp/trunk/runtime/src/kmp.h |
| 92 | /// Original structure: |
| 93 | /// typedef struct ident { |
| 94 | /// kmp_int32 reserved_1; /**< might be used in Fortran; |
| 95 | /// see above */ |
| 96 | /// kmp_int32 flags; /**< also f.flags; KMP_IDENT_xxx flags; |
| 97 | /// KMP_IDENT_KMPC identifies this union |
| 98 | /// member */ |
| 99 | /// kmp_int32 reserved_2; /**< not really used in Fortran any more; |
| 100 | /// see above */ |
| 101 | ///#if USE_ITT_BUILD |
| 102 | /// /* but currently used for storing |
| 103 | /// region-specific ITT */ |
| 104 | /// /* contextual information. */ |
| 105 | ///#endif /* USE_ITT_BUILD */ |
| 106 | /// kmp_int32 reserved_3; /**< source[4] in Fortran, do not use for |
| 107 | /// C++ */ |
| 108 | /// char const *psource; /**< String describing the source location. |
| 109 | /// The string is composed of semi-colon separated |
| 110 | // fields which describe the source file, |
| 111 | /// the function and a pair of line numbers that |
| 112 | /// delimit the construct. |
| 113 | /// */ |
| 114 | /// } ident_t; |
| 115 | enum IdentFieldIndex { |
| 116 | /// \brief might be used in Fortran |
| 117 | IdentField_Reserved_1, |
| 118 | /// \brief OMP_IDENT_xxx flags; OMP_IDENT_KMPC identifies this union member. |
| 119 | IdentField_Flags, |
| 120 | /// \brief Not really used in Fortran any more |
| 121 | IdentField_Reserved_2, |
| 122 | /// \brief Source[4] in Fortran, do not use for C++ |
| 123 | IdentField_Reserved_3, |
| 124 | /// \brief String describing the source location. The string is composed of |
| 125 | /// semi-colon separated fields which describe the source file, the function |
| 126 | /// and a pair of line numbers that delimit the construct. |
| 127 | IdentField_PSource |
| 128 | }; |
| 129 | llvm::StructType *IdentTy; |
Alexey Bataev | 1809571 | 2014-10-10 12:19:54 +0000 | [diff] [blame] | 130 | /// \brief Map for SourceLocation and OpenMP runtime library debug locations. |
Alexey Bataev | f002aca | 2014-05-30 05:48:40 +0000 | [diff] [blame] | 131 | typedef llvm::DenseMap<unsigned, llvm::Value *> OpenMPDebugLocMapTy; |
| 132 | OpenMPDebugLocMapTy OpenMPDebugLocMap; |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 133 | /// \brief The type for a microtask which gets passed to __kmpc_fork_call(). |
| 134 | /// Original representation is: |
| 135 | /// typedef void (kmpc_micro)(kmp_int32 global_tid, kmp_int32 bound_tid,...); |
| 136 | llvm::FunctionType *Kmpc_MicroTy; |
Alexey Bataev | 1809571 | 2014-10-10 12:19:54 +0000 | [diff] [blame] | 137 | /// \brief Stores debug location and ThreadID for the function. |
| 138 | struct DebugLocThreadIdTy { |
| 139 | llvm::Value *DebugLoc; |
| 140 | llvm::Value *ThreadID; |
| 141 | }; |
| 142 | /// \brief Map of local debug location, ThreadId and functions. |
| 143 | typedef llvm::DenseMap<llvm::Function *, DebugLocThreadIdTy> |
| 144 | OpenMPLocThreadIDMapTy; |
| 145 | OpenMPLocThreadIDMapTy OpenMPLocThreadIDMap; |
Alexey Bataev | 3a3bf0b | 2014-09-22 10:01:53 +0000 | [diff] [blame] | 146 | /// \brief Type kmp_critical_name, originally defined as typedef kmp_int32 |
| 147 | /// kmp_critical_name[8]; |
| 148 | llvm::ArrayType *KmpCriticalNameTy; |
| 149 | /// \brief Map of critical regions names and the corresponding lock objects. |
| 150 | llvm::StringMap<llvm::Value *, llvm::BumpPtrAllocator> CriticalRegionVarNames; |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 151 | |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 152 | /// \brief Emits object of ident_t type with info for source location. |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 153 | /// \param Flags Flags for OpenMP location. |
| 154 | /// |
| 155 | llvm::Value * |
| 156 | EmitOpenMPUpdateLocation(CodeGenFunction &CGF, SourceLocation Loc, |
| 157 | OpenMPLocationFlags Flags = OMP_IDENT_KMPC); |
| 158 | |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 159 | /// \brief Returns pointer to ident_t type; |
| 160 | llvm::Type *getIdentTyPointerTy(); |
| 161 | |
| 162 | /// \brief Returns pointer to kmpc_micro type; |
| 163 | llvm::Type *getKmpc_MicroPointerTy(); |
| 164 | |
| 165 | /// \brief Returns specified OpenMP runtime function. |
| 166 | /// \param Function OpenMP runtime function. |
| 167 | /// \return Specified function. |
| 168 | llvm::Constant *CreateRuntimeFunction(OpenMPRTLFunction Function); |
Alexey Bataev | 3a3bf0b | 2014-09-22 10:01:53 +0000 | [diff] [blame] | 169 | |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 170 | /// \brief Gets thread id value for the current thread. |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 171 | /// |
| 172 | llvm::Value *GetOpenMPThreadID(CodeGenFunction &CGF, SourceLocation Loc); |
| 173 | |
| 174 | public: |
| 175 | explicit CGOpenMPRuntime(CodeGenModule &CGM); |
| 176 | virtual ~CGOpenMPRuntime() {} |
| 177 | |
Alexey Bataev | 1809571 | 2014-10-10 12:19:54 +0000 | [diff] [blame] | 178 | /// \brief Emits outlined function for the specified OpenMP directive \a D |
| 179 | /// (required for parallel and task directives). This outlined function has |
| 180 | /// type void(*)(kmp_int32 /*ThreadID*/, kmp_int32 /*BoundID*/, struct |
| 181 | /// context_vars*). |
| 182 | /// \param D OpenMP directive. |
| 183 | /// \param ThreadIDVar Variable for thread id in the current OpenMP region. |
| 184 | /// |
| 185 | virtual llvm::Value * |
| 186 | EmitOpenMPOutlinedFunction(const OMPExecutableDirective &D, |
| 187 | const VarDecl *ThreadIDVar); |
| 188 | |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 189 | /// \brief Cleans up references to the objects in finished function. |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 190 | /// |
| 191 | void FunctionFinished(CodeGenFunction &CGF); |
| 192 | |
| 193 | /// \brief Emits code for parallel call of the \a OutlinedFn with variables |
| 194 | /// captured in a record which address is stored in \a CapturedStruct. |
Alexey Bataev | 1809571 | 2014-10-10 12:19:54 +0000 | [diff] [blame] | 195 | /// \param OutlinedFn Outlined function to be run in parallel threads. Type of |
| 196 | /// this function is void(*)(kmp_int32, kmp_int32, struct context_vars*). |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 197 | /// \param CapturedStruct A pointer to the record with the references to |
| 198 | /// variables used in \a OutlinedFn function. |
| 199 | /// |
| 200 | virtual void EmitOMPParallelCall(CodeGenFunction &CGF, SourceLocation Loc, |
| 201 | llvm::Value *OutlinedFn, |
| 202 | llvm::Value *CapturedStruct); |
| 203 | |
Alexey Bataev | 3a3bf0b | 2014-09-22 10:01:53 +0000 | [diff] [blame] | 204 | /// \brief Returns corresponding lock object for the specified critical region |
| 205 | /// name. If the lock object does not exist it is created, otherwise the |
| 206 | /// reference to the existing copy is returned. |
Alexey Bataev | 1809571 | 2014-10-10 12:19:54 +0000 | [diff] [blame] | 207 | /// \param CriticalName Name of the critical region. |
| 208 | /// |
Alexey Bataev | 3a3bf0b | 2014-09-22 10:01:53 +0000 | [diff] [blame] | 209 | llvm::Value *GetCriticalRegionLock(StringRef CriticalName); |
| 210 | |
| 211 | /// \brief Emits start of the critical region by calling void |
Alexey Bataev | f947218 | 2014-09-22 12:32:31 +0000 | [diff] [blame] | 212 | /// __kmpc_critical(ident_t *loc, kmp_int32 global_tid, kmp_critical_name |
| 213 | /// * \a RegionLock) |
Alexey Bataev | 3a3bf0b | 2014-09-22 10:01:53 +0000 | [diff] [blame] | 214 | /// \param RegionLock The lock object for critical region. |
Alexey Bataev | 3a3bf0b | 2014-09-22 10:01:53 +0000 | [diff] [blame] | 215 | virtual void EmitOMPCriticalRegionStart(CodeGenFunction &CGF, |
| 216 | llvm::Value *RegionLock, |
| 217 | SourceLocation Loc); |
| 218 | |
| 219 | /// \brief Emits end of the critical region by calling void |
Alexey Bataev | f947218 | 2014-09-22 12:32:31 +0000 | [diff] [blame] | 220 | /// __kmpc_end_critical(ident_t *loc, kmp_int32 global_tid, kmp_critical_name |
| 221 | /// * \a RegionLock) |
Alexey Bataev | 3a3bf0b | 2014-09-22 10:01:53 +0000 | [diff] [blame] | 222 | /// \param RegionLock The lock object for critical region. |
Alexey Bataev | 3a3bf0b | 2014-09-22 10:01:53 +0000 | [diff] [blame] | 223 | virtual void EmitOMPCriticalRegionEnd(CodeGenFunction &CGF, |
| 224 | llvm::Value *RegionLock, |
| 225 | SourceLocation Loc); |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 226 | |
| 227 | /// \brief Emits a barrier for OpenMP threads. |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 228 | /// \param Flags Flags for the barrier. |
| 229 | /// |
| 230 | virtual void EmitOMPBarrierCall(CodeGenFunction &CGF, SourceLocation Loc, |
| 231 | OpenMPLocationFlags Flags); |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 232 | }; |
Alexey Bataev | 23b6942 | 2014-06-18 07:08:49 +0000 | [diff] [blame] | 233 | } // namespace CodeGen |
| 234 | } // namespace clang |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 235 | |
| 236 | #endif |