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