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 | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 17 | #include "clang/AST/Type.h" |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 18 | #include "clang/Basic/OpenMPKinds.h" |
Chandler Carruth | 0d9593d | 2015-01-14 11:29:14 +0000 | [diff] [blame] | 19 | #include "clang/Basic/SourceLocation.h" |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/DenseMap.h" |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/DenseSet.h" |
Alexey Bataev | 3a3bf0b | 2014-09-22 10:01:53 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/StringMap.h" |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 23 | #include "llvm/IR/ValueHandle.h" |
Alexey Bataev | 1809571 | 2014-10-10 12:19:54 +0000 | [diff] [blame] | 24 | |
| 25 | namespace llvm { |
| 26 | class ArrayType; |
| 27 | class Constant; |
| 28 | class Function; |
| 29 | class FunctionType; |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 30 | class GlobalVariable; |
Alexey Bataev | 1809571 | 2014-10-10 12:19:54 +0000 | [diff] [blame] | 31 | class StructType; |
| 32 | class Type; |
| 33 | class Value; |
| 34 | } // namespace llvm |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 35 | |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 36 | namespace clang { |
Alexey Bataev | cc37cc1 | 2014-11-20 04:34:54 +0000 | [diff] [blame] | 37 | class Expr; |
Alexey Bataev | 1809571 | 2014-10-10 12:19:54 +0000 | [diff] [blame] | 38 | class OMPExecutableDirective; |
| 39 | class VarDecl; |
| 40 | |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 41 | namespace CodeGen { |
| 42 | |
Alexey Bataev | 1809571 | 2014-10-10 12:19:54 +0000 | [diff] [blame] | 43 | class CodeGenFunction; |
| 44 | class CodeGenModule; |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 45 | |
| 46 | class CGOpenMPRuntime { |
Alexey Bataev | 8f7c1b0 | 2014-12-05 04:09:23 +0000 | [diff] [blame] | 47 | enum OpenMPRTLFunction { |
| 48 | /// \brief Call to void __kmpc_fork_call(ident_t *loc, kmp_int32 argc, |
| 49 | /// kmpc_micro microtask, ...); |
| 50 | OMPRTL__kmpc_fork_call, |
| 51 | /// \brief Call to void *__kmpc_threadprivate_cached(ident_t *loc, |
| 52 | /// kmp_int32 global_tid, void *data, size_t size, void ***cache); |
| 53 | OMPRTL__kmpc_threadprivate_cached, |
| 54 | /// \brief Call to void __kmpc_threadprivate_register( ident_t *, |
| 55 | /// void *data, kmpc_ctor ctor, kmpc_cctor cctor, kmpc_dtor dtor); |
| 56 | OMPRTL__kmpc_threadprivate_register, |
| 57 | // Call to __kmpc_int32 kmpc_global_thread_num(ident_t *loc); |
| 58 | OMPRTL__kmpc_global_thread_num, |
| 59 | // Call to void __kmpc_critical(ident_t *loc, kmp_int32 global_tid, |
| 60 | // kmp_critical_name *crit); |
| 61 | OMPRTL__kmpc_critical, |
| 62 | // Call to void __kmpc_end_critical(ident_t *loc, kmp_int32 global_tid, |
| 63 | // kmp_critical_name *crit); |
| 64 | OMPRTL__kmpc_end_critical, |
| 65 | // Call to kmp_int32 __kmpc_cancel_barrier(ident_t *loc, kmp_int32 |
| 66 | // global_tid); |
| 67 | OMPRTL__kmpc_cancel_barrier, |
Alexander Musman | 21212e4 | 2015-03-13 10:38:23 +0000 | [diff] [blame] | 68 | // Call to void __kmpc_for_static_fini(ident_t *loc, kmp_int32 global_tid); |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 69 | OMPRTL__kmpc_for_static_fini, |
Alexey Bataev | 8f7c1b0 | 2014-12-05 04:09:23 +0000 | [diff] [blame] | 70 | // Call to void __kmpc_serialized_parallel(ident_t *loc, kmp_int32 |
| 71 | // global_tid); |
| 72 | OMPRTL__kmpc_serialized_parallel, |
| 73 | // Call to void __kmpc_end_serialized_parallel(ident_t *loc, kmp_int32 |
| 74 | // global_tid); |
| 75 | OMPRTL__kmpc_end_serialized_parallel, |
| 76 | // Call to void __kmpc_push_num_threads(ident_t *loc, kmp_int32 global_tid, |
| 77 | // kmp_int32 num_threads); |
| 78 | OMPRTL__kmpc_push_num_threads, |
Alexey Bataev | d76df6d | 2015-02-24 12:55:09 +0000 | [diff] [blame] | 79 | // Call to void __kmpc_flush(ident_t *loc); |
Alexey Bataev | 8f7c1b0 | 2014-12-05 04:09:23 +0000 | [diff] [blame] | 80 | OMPRTL__kmpc_flush, |
| 81 | // Call to kmp_int32 __kmpc_master(ident_t *, kmp_int32 global_tid); |
| 82 | OMPRTL__kmpc_master, |
| 83 | // Call to void __kmpc_end_master(ident_t *, kmp_int32 global_tid); |
| 84 | OMPRTL__kmpc_end_master, |
Alexey Bataev | 9f797f3 | 2015-02-05 05:57:51 +0000 | [diff] [blame] | 85 | // Call to kmp_int32 __kmpc_omp_taskyield(ident_t *, kmp_int32 global_tid, |
| 86 | // int end_part); |
| 87 | OMPRTL__kmpc_omp_taskyield, |
Alexey Bataev | 6956e2e | 2015-02-05 06:35:41 +0000 | [diff] [blame] | 88 | // Call to kmp_int32 __kmpc_single(ident_t *, kmp_int32 global_tid); |
| 89 | OMPRTL__kmpc_single, |
| 90 | // Call to void __kmpc_end_single(ident_t *, kmp_int32 global_tid); |
| 91 | OMPRTL__kmpc_end_single, |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 92 | // Call to kmp_task_t * __kmpc_omp_task_alloc(ident_t *, kmp_int32 gtid, |
| 93 | // kmp_int32 flags, size_t sizeof_kmp_task_t, size_t sizeof_shareds, |
| 94 | // kmp_routine_entry_t *task_entry); |
| 95 | OMPRTL__kmpc_omp_task_alloc, |
| 96 | // Call to kmp_int32 __kmpc_omp_task(ident_t *, kmp_int32 gtid, kmp_task_t * |
| 97 | // new_task); |
| 98 | OMPRTL__kmpc_omp_task, |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 99 | // Call to void __kmpc_copyprivate(ident_t *loc, kmp_int32 global_tid, |
| 100 | // kmp_int32 cpy_size, void *cpy_data, void(*cpy_func)(void *, void *), |
| 101 | // kmp_int32 didit); |
| 102 | OMPRTL__kmpc_copyprivate, |
Alexey Bataev | 8f7c1b0 | 2014-12-05 04:09:23 +0000 | [diff] [blame] | 103 | }; |
| 104 | |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 105 | /// \brief Values for bit flags used in the ident_t to describe the fields. |
| 106 | /// All enumeric elements are named and described in accordance with the code |
| 107 | /// from http://llvm.org/svn/llvm-project/openmp/trunk/runtime/src/kmp.h |
| 108 | enum OpenMPLocationFlags { |
| 109 | /// \brief Use trampoline for internal microtask. |
| 110 | OMP_IDENT_IMD = 0x01, |
| 111 | /// \brief Use c-style ident structure. |
| 112 | OMP_IDENT_KMPC = 0x02, |
| 113 | /// \brief Atomic reduction option for kmpc_reduce. |
| 114 | OMP_ATOMIC_REDUCE = 0x10, |
| 115 | /// \brief Explicit 'barrier' directive. |
| 116 | OMP_IDENT_BARRIER_EXPL = 0x20, |
| 117 | /// \brief Implicit barrier in code. |
| 118 | OMP_IDENT_BARRIER_IMPL = 0x40, |
| 119 | /// \brief Implicit barrier in 'for' directive. |
| 120 | OMP_IDENT_BARRIER_IMPL_FOR = 0x40, |
| 121 | /// \brief Implicit barrier in 'sections' directive. |
| 122 | OMP_IDENT_BARRIER_IMPL_SECTIONS = 0xC0, |
| 123 | /// \brief Implicit barrier in 'single' directive. |
| 124 | OMP_IDENT_BARRIER_IMPL_SINGLE = 0x140 |
| 125 | }; |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 126 | CodeGenModule &CGM; |
| 127 | /// \brief Default const ident_t object used for initialization of all other |
| 128 | /// ident_t objects. |
| 129 | llvm::Constant *DefaultOpenMPPSource; |
Alexey Bataev | 1809571 | 2014-10-10 12:19:54 +0000 | [diff] [blame] | 130 | /// \brief Map of flags and corresponding default locations. |
Alexey Bataev | 15007ba | 2014-05-07 06:18:01 +0000 | [diff] [blame] | 131 | typedef llvm::DenseMap<unsigned, llvm::Value *> OpenMPDefaultLocMapTy; |
| 132 | OpenMPDefaultLocMapTy OpenMPDefaultLocMap; |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 133 | llvm::Value *getOrCreateDefaultLocation(OpenMPLocationFlags Flags); |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 134 | /// \brief Describes ident structure that describes a source location. |
| 135 | /// All descriptions are taken from |
| 136 | /// http://llvm.org/svn/llvm-project/openmp/trunk/runtime/src/kmp.h |
| 137 | /// Original structure: |
| 138 | /// typedef struct ident { |
| 139 | /// kmp_int32 reserved_1; /**< might be used in Fortran; |
| 140 | /// see above */ |
| 141 | /// kmp_int32 flags; /**< also f.flags; KMP_IDENT_xxx flags; |
| 142 | /// KMP_IDENT_KMPC identifies this union |
| 143 | /// member */ |
| 144 | /// kmp_int32 reserved_2; /**< not really used in Fortran any more; |
| 145 | /// see above */ |
| 146 | ///#if USE_ITT_BUILD |
| 147 | /// /* but currently used for storing |
| 148 | /// region-specific ITT */ |
| 149 | /// /* contextual information. */ |
| 150 | ///#endif /* USE_ITT_BUILD */ |
| 151 | /// kmp_int32 reserved_3; /**< source[4] in Fortran, do not use for |
| 152 | /// C++ */ |
| 153 | /// char const *psource; /**< String describing the source location. |
| 154 | /// The string is composed of semi-colon separated |
| 155 | // fields which describe the source file, |
| 156 | /// the function and a pair of line numbers that |
| 157 | /// delimit the construct. |
| 158 | /// */ |
| 159 | /// } ident_t; |
| 160 | enum IdentFieldIndex { |
| 161 | /// \brief might be used in Fortran |
| 162 | IdentField_Reserved_1, |
| 163 | /// \brief OMP_IDENT_xxx flags; OMP_IDENT_KMPC identifies this union member. |
| 164 | IdentField_Flags, |
| 165 | /// \brief Not really used in Fortran any more |
| 166 | IdentField_Reserved_2, |
| 167 | /// \brief Source[4] in Fortran, do not use for C++ |
| 168 | IdentField_Reserved_3, |
| 169 | /// \brief String describing the source location. The string is composed of |
| 170 | /// semi-colon separated fields which describe the source file, the function |
| 171 | /// and a pair of line numbers that delimit the construct. |
| 172 | IdentField_PSource |
| 173 | }; |
| 174 | llvm::StructType *IdentTy; |
Alexey Bataev | 1809571 | 2014-10-10 12:19:54 +0000 | [diff] [blame] | 175 | /// \brief Map for SourceLocation and OpenMP runtime library debug locations. |
Alexey Bataev | f002aca | 2014-05-30 05:48:40 +0000 | [diff] [blame] | 176 | typedef llvm::DenseMap<unsigned, llvm::Value *> OpenMPDebugLocMapTy; |
| 177 | OpenMPDebugLocMapTy OpenMPDebugLocMap; |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 178 | /// \brief The type for a microtask which gets passed to __kmpc_fork_call(). |
| 179 | /// Original representation is: |
| 180 | /// typedef void (kmpc_micro)(kmp_int32 global_tid, kmp_int32 bound_tid,...); |
| 181 | llvm::FunctionType *Kmpc_MicroTy; |
Alexey Bataev | 1809571 | 2014-10-10 12:19:54 +0000 | [diff] [blame] | 182 | /// \brief Stores debug location and ThreadID for the function. |
| 183 | struct DebugLocThreadIdTy { |
| 184 | llvm::Value *DebugLoc; |
| 185 | llvm::Value *ThreadID; |
| 186 | }; |
| 187 | /// \brief Map of local debug location, ThreadId and functions. |
| 188 | typedef llvm::DenseMap<llvm::Function *, DebugLocThreadIdTy> |
| 189 | OpenMPLocThreadIDMapTy; |
| 190 | OpenMPLocThreadIDMapTy OpenMPLocThreadIDMap; |
Alexey Bataev | 3a3bf0b | 2014-09-22 10:01:53 +0000 | [diff] [blame] | 191 | /// \brief Type kmp_critical_name, originally defined as typedef kmp_int32 |
| 192 | /// kmp_critical_name[8]; |
| 193 | llvm::ArrayType *KmpCriticalNameTy; |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 194 | /// \brief An ordered map of auto-generated variables to their unique names. |
| 195 | /// It stores variables with the following names: 1) ".gomp_critical_user_" + |
| 196 | /// <critical_section_name> + ".var" for "omp critical" directives; 2) |
| 197 | /// <mangled_name_for_global_var> + ".cache." for cache for threadprivate |
| 198 | /// variables. |
| 199 | llvm::StringMap<llvm::AssertingVH<llvm::Constant>, llvm::BumpPtrAllocator> |
| 200 | InternalVars; |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 201 | /// \brief Type typedef kmp_int32 (* kmp_routine_entry_t)(kmp_int32, void *); |
| 202 | llvm::Type *KmpRoutineEntryPtrTy; |
| 203 | QualType KmpRoutineEntryPtrQTy; |
| 204 | |
| 205 | /// \brief Build type kmp_routine_entry_t (if not built yet). |
| 206 | void emitKmpRoutineEntryT(QualType KmpInt32Ty); |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 207 | |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 208 | /// \brief Emits object of ident_t type with info for source location. |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 209 | /// \param Flags Flags for OpenMP location. |
| 210 | /// |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 211 | llvm::Value *emitUpdateLocation(CodeGenFunction &CGF, SourceLocation Loc, |
| 212 | OpenMPLocationFlags Flags = OMP_IDENT_KMPC); |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 213 | |
Alexey Bataev | d74d060 | 2014-10-13 06:02:40 +0000 | [diff] [blame] | 214 | /// \brief Returns pointer to ident_t type. |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 215 | llvm::Type *getIdentTyPointerTy(); |
| 216 | |
Alexey Bataev | d74d060 | 2014-10-13 06:02:40 +0000 | [diff] [blame] | 217 | /// \brief Returns pointer to kmpc_micro type. |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 218 | llvm::Type *getKmpc_MicroPointerTy(); |
| 219 | |
| 220 | /// \brief Returns specified OpenMP runtime function. |
| 221 | /// \param Function OpenMP runtime function. |
| 222 | /// \return Specified function. |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 223 | llvm::Constant *createRuntimeFunction(OpenMPRTLFunction Function); |
Alexey Bataev | 3a3bf0b | 2014-09-22 10:01:53 +0000 | [diff] [blame] | 224 | |
Alexander Musman | 21212e4 | 2015-03-13 10:38:23 +0000 | [diff] [blame] | 225 | /// \brief Returns __kmpc_for_static_init_* runtime function for the specified |
| 226 | /// size \a IVSize and sign \a IVSigned. |
| 227 | llvm::Constant *createForStaticInitFunction(unsigned IVSize, bool IVSigned); |
| 228 | |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 229 | /// \brief Returns __kmpc_dispatch_init_* runtime function for the specified |
| 230 | /// size \a IVSize and sign \a IVSigned. |
| 231 | llvm::Constant *createDispatchInitFunction(unsigned IVSize, bool IVSigned); |
| 232 | |
| 233 | /// \brief Returns __kmpc_dispatch_next_* runtime function for the specified |
| 234 | /// size \a IVSize and sign \a IVSigned. |
| 235 | llvm::Constant *createDispatchNextFunction(unsigned IVSize, bool IVSigned); |
| 236 | |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 237 | /// \brief If the specified mangled name is not in the module, create and |
| 238 | /// return threadprivate cache object. This object is a pointer's worth of |
| 239 | /// storage that's reserved for use by the OpenMP runtime. |
NAKAMURA Takumi | cdcbfba | 2014-11-11 07:58:06 +0000 | [diff] [blame] | 240 | /// \param VD Threadprivate variable. |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 241 | /// \return Cache variable for the specified threadprivate. |
| 242 | llvm::Constant *getOrCreateThreadPrivateCache(const VarDecl *VD); |
| 243 | |
Alexey Bataev | d74d060 | 2014-10-13 06:02:40 +0000 | [diff] [blame] | 244 | /// \brief Emits address of the word in a memory where current thread id is |
| 245 | /// stored. |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 246 | virtual llvm::Value *emitThreadIDAddress(CodeGenFunction &CGF, |
Alexey Bataev | d74d060 | 2014-10-13 06:02:40 +0000 | [diff] [blame] | 247 | SourceLocation Loc); |
| 248 | |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 249 | /// \brief Gets thread id value for the current thread. |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 250 | /// |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 251 | llvm::Value *getThreadID(CodeGenFunction &CGF, SourceLocation Loc); |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 252 | |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 253 | /// \brief Gets (if variable with the given name already exist) or creates |
| 254 | /// internal global variable with the specified Name. The created variable has |
| 255 | /// linkage CommonLinkage by default and is initialized by null value. |
| 256 | /// \param Ty Type of the global variable. If it is exist already the type |
| 257 | /// must be the same. |
| 258 | /// \param Name Name of the variable. |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 259 | llvm::Constant *getOrCreateInternalVariable(llvm::Type *Ty, |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 260 | const llvm::Twine &Name); |
| 261 | |
| 262 | /// \brief Set of threadprivate variables with the generated initializer. |
| 263 | llvm::DenseSet<const VarDecl *> ThreadPrivateWithDefinition; |
| 264 | |
| 265 | /// \brief Emits initialization code for the threadprivate variables. |
| 266 | /// \param VDAddr Address of the global variable \a VD. |
| 267 | /// \param Ctor Pointer to a global init function for \a VD. |
| 268 | /// \param CopyCtor Pointer to a global copy function for \a VD. |
| 269 | /// \param Dtor Pointer to a global destructor function for \a VD. |
| 270 | /// \param Loc Location of threadprivate declaration. |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 271 | void emitThreadPrivateVarInit(CodeGenFunction &CGF, llvm::Value *VDAddr, |
| 272 | llvm::Value *Ctor, llvm::Value *CopyCtor, |
| 273 | llvm::Value *Dtor, SourceLocation Loc); |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 274 | |
Alexey Bataev | 75ddfab | 2014-12-01 11:32:38 +0000 | [diff] [blame] | 275 | /// \brief Returns corresponding lock object for the specified critical region |
| 276 | /// name. If the lock object does not exist it is created, otherwise the |
| 277 | /// reference to the existing copy is returned. |
| 278 | /// \param CriticalName Name of the critical region. |
| 279 | /// |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 280 | llvm::Value *getCriticalRegionLock(StringRef CriticalName); |
Alexey Bataev | 75ddfab | 2014-12-01 11:32:38 +0000 | [diff] [blame] | 281 | |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 282 | public: |
| 283 | explicit CGOpenMPRuntime(CodeGenModule &CGM); |
| 284 | virtual ~CGOpenMPRuntime() {} |
Alexey Bataev | 9179755 | 2015-03-18 04:13:55 +0000 | [diff] [blame] | 285 | virtual void clear(); |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 286 | |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 287 | /// \brief Emits outlined function for the specified OpenMP directive \a D. |
| 288 | /// This outlined function has type void(*)(kmp_int32 *ThreadID, kmp_int32 |
| 289 | /// BoundID, struct context_vars*). |
Alexey Bataev | 1809571 | 2014-10-10 12:19:54 +0000 | [diff] [blame] | 290 | /// \param D OpenMP directive. |
| 291 | /// \param ThreadIDVar Variable for thread id in the current OpenMP region. |
| 292 | /// |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 293 | virtual llvm::Value *emitOutlinedFunction(const OMPExecutableDirective &D, |
| 294 | const VarDecl *ThreadIDVar); |
Alexey Bataev | 1809571 | 2014-10-10 12:19:54 +0000 | [diff] [blame] | 295 | |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 296 | /// \brief Emits outlined function for the OpenMP task directive \a D. This |
| 297 | /// outlined function has type void(*)(kmp_int32 ThreadID, kmp_int32 |
| 298 | /// PartID, struct context_vars*). |
| 299 | /// \param D OpenMP directive. |
| 300 | /// \param ThreadIDVar Variable for thread id in the current OpenMP region. |
NAKAMURA Takumi | 7304a24 | 2015-03-11 03:51:40 +0000 | [diff] [blame] | 301 | /// \param PartIDVar If not nullptr - variable used for part id in tasks. |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 302 | /// |
| 303 | virtual llvm::Value *emitTaskOutlinedFunction(const OMPExecutableDirective &D, |
| 304 | const VarDecl *ThreadIDVar, |
| 305 | const VarDecl *PartIDVar); |
| 306 | |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 307 | /// \brief Cleans up references to the objects in finished function. |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 308 | /// |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 309 | void functionFinished(CodeGenFunction &CGF); |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 310 | |
| 311 | /// \brief Emits code for parallel call of the \a OutlinedFn with variables |
| 312 | /// captured in a record which address is stored in \a CapturedStruct. |
Alexey Bataev | 1809571 | 2014-10-10 12:19:54 +0000 | [diff] [blame] | 313 | /// \param OutlinedFn Outlined function to be run in parallel threads. Type of |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 314 | /// this function is void(*)(kmp_int32 *, kmp_int32, struct context_vars*). |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 315 | /// \param CapturedStruct A pointer to the record with the references to |
| 316 | /// variables used in \a OutlinedFn function. |
| 317 | /// |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 318 | virtual void emitParallelCall(CodeGenFunction &CGF, SourceLocation Loc, |
| 319 | llvm::Value *OutlinedFn, |
| 320 | llvm::Value *CapturedStruct); |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 321 | |
Alexey Bataev | d74d060 | 2014-10-13 06:02:40 +0000 | [diff] [blame] | 322 | /// \brief Emits code for serial call of the \a OutlinedFn with variables |
| 323 | /// captured in a record which address is stored in \a CapturedStruct. |
| 324 | /// \param OutlinedFn Outlined function to be run in serial mode. |
| 325 | /// \param CapturedStruct A pointer to the record with the references to |
| 326 | /// variables used in \a OutlinedFn function. |
| 327 | /// |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 328 | virtual void emitSerialCall(CodeGenFunction &CGF, SourceLocation Loc, |
| 329 | llvm::Value *OutlinedFn, |
| 330 | llvm::Value *CapturedStruct); |
Alexey Bataev | d74d060 | 2014-10-13 06:02:40 +0000 | [diff] [blame] | 331 | |
Alexey Bataev | 75ddfab | 2014-12-01 11:32:38 +0000 | [diff] [blame] | 332 | /// \brief Emits a critical region. |
Alexey Bataev | 1809571 | 2014-10-10 12:19:54 +0000 | [diff] [blame] | 333 | /// \param CriticalName Name of the critical region. |
Alexey Bataev | 75ddfab | 2014-12-01 11:32:38 +0000 | [diff] [blame] | 334 | /// \param CriticalOpGen Generator for the statement associated with the given |
| 335 | /// critical region. |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 336 | virtual void emitCriticalRegion(CodeGenFunction &CGF, StringRef CriticalName, |
| 337 | const std::function<void()> &CriticalOpGen, |
| 338 | SourceLocation Loc); |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 339 | |
Alexey Bataev | 8d69065 | 2014-12-04 07:23:53 +0000 | [diff] [blame] | 340 | /// \brief Emits a master region. |
| 341 | /// \param MasterOpGen Generator for the statement associated with the given |
| 342 | /// master region. |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 343 | virtual void emitMasterRegion(CodeGenFunction &CGF, |
| 344 | const std::function<void()> &MasterOpGen, |
| 345 | SourceLocation Loc); |
Alexey Bataev | 8d69065 | 2014-12-04 07:23:53 +0000 | [diff] [blame] | 346 | |
Alexey Bataev | 9f797f3 | 2015-02-05 05:57:51 +0000 | [diff] [blame] | 347 | /// \brief Emits code for a taskyield directive. |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 348 | virtual void emitTaskyieldCall(CodeGenFunction &CGF, SourceLocation Loc); |
Alexey Bataev | 9f797f3 | 2015-02-05 05:57:51 +0000 | [diff] [blame] | 349 | |
Alexey Bataev | 6956e2e | 2015-02-05 06:35:41 +0000 | [diff] [blame] | 350 | /// \brief Emits a single region. |
| 351 | /// \param SingleOpGen Generator for the statement associated with the given |
| 352 | /// single region. |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 353 | virtual void emitSingleRegion(CodeGenFunction &CGF, |
| 354 | const std::function<void()> &SingleOpGen, |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 355 | SourceLocation Loc, |
| 356 | ArrayRef<const Expr *> CopyprivateVars, |
| 357 | ArrayRef<const Expr *> SrcExprs, |
| 358 | ArrayRef<const Expr *> DstExprs, |
| 359 | ArrayRef<const Expr *> AssignmentOps); |
Alexey Bataev | 6956e2e | 2015-02-05 06:35:41 +0000 | [diff] [blame] | 360 | |
Alexey Bataev | 8f7c1b0 | 2014-12-05 04:09:23 +0000 | [diff] [blame] | 361 | /// \brief Emits explicit barrier for OpenMP threads. |
| 362 | /// \param IsExplicit true, if it is explicitly specified barrier. |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 363 | /// |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 364 | virtual void emitBarrierCall(CodeGenFunction &CGF, SourceLocation Loc, |
| 365 | bool IsExplicit = true); |
Alexey Bataev | b205978 | 2014-10-13 08:23:51 +0000 | [diff] [blame] | 366 | |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 367 | /// \brief Check if the specified \a ScheduleKind is static non-chunked. |
| 368 | /// This kind of worksharing directive is emitted without outer loop. |
| 369 | /// \param ScheduleKind Schedule kind specified in the 'schedule' clause. |
| 370 | /// \param Chunked True if chunk is specified in the clause. |
| 371 | /// |
| 372 | virtual bool isStaticNonchunked(OpenMPScheduleClauseKind ScheduleKind, |
| 373 | bool Chunked) const; |
| 374 | |
Alexander Musman | df7a8e2 | 2015-01-22 08:49:35 +0000 | [diff] [blame] | 375 | /// \brief Check if the specified \a ScheduleKind is dynamic. |
| 376 | /// This kind of worksharing directive is emitted without outer loop. |
| 377 | /// \param ScheduleKind Schedule Kind specified in the 'schedule' clause. |
| 378 | /// |
| 379 | virtual bool isDynamic(OpenMPScheduleClauseKind ScheduleKind) const; |
| 380 | |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 381 | /// \brief Call the appropriate runtime routine to initialize it before start |
| 382 | /// of loop. |
| 383 | /// |
| 384 | /// Depending on the loop schedule, it is nesessary to call some runtime |
| 385 | /// routine before start of the OpenMP loop to get the loop upper / lower |
| 386 | /// bounds \a LB and \a UB and stride \a ST. |
| 387 | /// |
| 388 | /// \param CGF Reference to current CodeGenFunction. |
| 389 | /// \param Loc Clang source location. |
NAKAMURA Takumi | eca0838 | 2014-12-17 14:47:06 +0000 | [diff] [blame] | 390 | /// \param SchedKind Schedule kind, specified by the 'schedule' clause. |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 391 | /// \param IVSize Size of the iteration variable in bits. |
| 392 | /// \param IVSigned Sign of the interation variable. |
| 393 | /// \param IL Address of the output variable in which the flag of the |
| 394 | /// last iteration is returned. |
| 395 | /// \param LB Address of the output variable in which the lower iteration |
| 396 | /// number is returned. |
| 397 | /// \param UB Address of the output variable in which the upper iteration |
| 398 | /// number is returned. |
| 399 | /// \param ST Address of the output variable in which the stride value is |
| 400 | /// returned nesessary to generated the static_chunked scheduled loop. |
| 401 | /// \param Chunk Value of the chunk for the static_chunked scheduled loop. |
| 402 | /// For the default (nullptr) value, the chunk 1 will be used. |
| 403 | /// |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 404 | virtual void emitForInit(CodeGenFunction &CGF, SourceLocation Loc, |
| 405 | OpenMPScheduleClauseKind SchedKind, unsigned IVSize, |
| 406 | bool IVSigned, llvm::Value *IL, llvm::Value *LB, |
| 407 | llvm::Value *UB, llvm::Value *ST, |
| 408 | llvm::Value *Chunk = nullptr); |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 409 | |
| 410 | /// \brief Call the appropriate runtime routine to notify that we finished |
| 411 | /// all the work with current loop. |
| 412 | /// |
| 413 | /// \param CGF Reference to current CodeGenFunction. |
| 414 | /// \param Loc Clang source location. |
| 415 | /// \param ScheduleKind Schedule kind, specified by the 'schedule' clause. |
| 416 | /// |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 417 | virtual void emitForFinish(CodeGenFunction &CGF, SourceLocation Loc, |
| 418 | OpenMPScheduleClauseKind ScheduleKind); |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 419 | |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 420 | /// Call __kmpc_dispatch_next( |
| 421 | /// ident_t *loc, kmp_int32 tid, kmp_int32 *p_lastiter, |
| 422 | /// kmp_int[32|64] *p_lower, kmp_int[32|64] *p_upper, |
| 423 | /// kmp_int[32|64] *p_stride); |
| 424 | /// \param IVSize Size of the iteration variable in bits. |
| 425 | /// \param IVSigned Sign of the interation variable. |
| 426 | /// \param IL Address of the output variable in which the flag of the |
| 427 | /// last iteration is returned. |
| 428 | /// \param LB Address of the output variable in which the lower iteration |
| 429 | /// number is returned. |
| 430 | /// \param UB Address of the output variable in which the upper iteration |
| 431 | /// number is returned. |
| 432 | /// \param ST Address of the output variable in which the stride value is |
| 433 | /// returned. |
| 434 | virtual llvm::Value *emitForNext(CodeGenFunction &CGF, SourceLocation Loc, |
| 435 | unsigned IVSize, bool IVSigned, |
| 436 | llvm::Value *IL, llvm::Value *LB, |
| 437 | llvm::Value *UB, llvm::Value *ST); |
| 438 | |
Alexey Bataev | b205978 | 2014-10-13 08:23:51 +0000 | [diff] [blame] | 439 | /// \brief Emits call to void __kmpc_push_num_threads(ident_t *loc, kmp_int32 |
| 440 | /// global_tid, kmp_int32 num_threads) to generate code for 'num_threads' |
| 441 | /// clause. |
| 442 | /// \param NumThreads An integer value of threads. |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 443 | virtual void emitNumThreadsClause(CodeGenFunction &CGF, |
| 444 | llvm::Value *NumThreads, |
| 445 | SourceLocation Loc); |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 446 | |
| 447 | /// \brief Returns address of the threadprivate variable for the current |
| 448 | /// thread. |
NAKAMURA Takumi | cdcbfba | 2014-11-11 07:58:06 +0000 | [diff] [blame] | 449 | /// \param VD Threadprivate variable. |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 450 | /// \param VDAddr Address of the global variable \a VD. |
| 451 | /// \param Loc Location of the reference to threadprivate var. |
| 452 | /// \return Address of the threadprivate variable for the current thread. |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 453 | virtual llvm::Value *getAddrOfThreadPrivate(CodeGenFunction &CGF, |
| 454 | const VarDecl *VD, |
| 455 | llvm::Value *VDAddr, |
| 456 | SourceLocation Loc); |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 457 | |
| 458 | /// \brief Emit a code for initialization of threadprivate variable. It emits |
| 459 | /// a call to runtime library which adds initial value to the newly created |
| 460 | /// threadprivate variable (if it is not constant) and registers destructor |
| 461 | /// for the variable (if any). |
| 462 | /// \param VD Threadprivate variable. |
| 463 | /// \param VDAddr Address of the global variable \a VD. |
| 464 | /// \param Loc Location of threadprivate declaration. |
| 465 | /// \param PerformInit true if initialization expression is not constant. |
| 466 | virtual llvm::Function * |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 467 | emitThreadPrivateVarDefinition(const VarDecl *VD, llvm::Value *VDAddr, |
| 468 | SourceLocation Loc, bool PerformInit, |
| 469 | CodeGenFunction *CGF = nullptr); |
Alexey Bataev | cc37cc1 | 2014-11-20 04:34:54 +0000 | [diff] [blame] | 470 | |
| 471 | /// \brief Emit flush of the variables specified in 'omp flush' directive. |
| 472 | /// \param Vars List of variables to flush. |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 473 | virtual void emitFlush(CodeGenFunction &CGF, ArrayRef<const Expr *> Vars, |
| 474 | SourceLocation Loc); |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 475 | |
| 476 | /// \brief Emit task region for the task directive. The task region is |
| 477 | /// emmitted in several steps: |
| 478 | /// 1. Emit a call to kmp_task_t *__kmpc_omp_task_alloc(ident_t *, kmp_int32 |
| 479 | /// gtid, kmp_int32 flags, size_t sizeof_kmp_task_t, size_t sizeof_shareds, |
| 480 | /// kmp_routine_entry_t *task_entry). Here task_entry is a pointer to the |
| 481 | /// function: |
| 482 | /// kmp_int32 .omp_task_entry.(kmp_int32 gtid, kmp_task_t *tt) { |
| 483 | /// TaskFunction(gtid, tt->part_id, tt->shareds); |
| 484 | /// return 0; |
| 485 | /// } |
| 486 | /// 2. Copy a list of shared variables to field shareds of the resulting |
| 487 | /// structure kmp_task_t returned by the previous call (if any). |
| 488 | /// 3. Copy a pointer to destructions function to field destructions of the |
| 489 | /// resulting structure kmp_task_t. |
| 490 | /// 4. Emit a call to kmp_int32 __kmpc_omp_task(ident_t *, kmp_int32 gtid, |
| 491 | /// kmp_task_t *new_task), where new_task is a resulting structure from |
| 492 | /// previous items. |
| 493 | /// \param Tied true if the task is tied (the task is tied to the thread that |
| 494 | /// can suspend its task region), false - untied (the task is not tied to any |
| 495 | /// thread). |
| 496 | /// \param Final Contains either constant bool value, or llvm::Value * of i1 |
| 497 | /// type for final clause. If the value is true, the task forces all of its |
| 498 | /// child tasks to become final and included tasks. |
| 499 | /// \param TaskFunction An LLVM function with type void (*)(i32 /*gtid*/, i32 |
| 500 | /// /*part_id*/, captured_struct */*__context*/); |
| 501 | /// \param SharedsTy A type which contains references the shared variables. |
| 502 | /// \param Shareds Context with the list of shared variables from the \a |
| 503 | /// TaskFunction. |
| 504 | virtual void emitTaskCall(CodeGenFunction &CGF, SourceLocation Loc, bool Tied, |
| 505 | llvm::PointerIntPair<llvm::Value *, 1, bool> Final, |
| 506 | llvm::Value *TaskFunction, QualType SharedsTy, |
| 507 | llvm::Value *Shareds); |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 508 | }; |
Alexey Bataev | 8cbe0a6 | 2015-02-26 10:27:34 +0000 | [diff] [blame] | 509 | |
| 510 | /// \brief RAII for emitting code of CapturedStmt without function outlining. |
| 511 | class InlinedOpenMPRegionRAII { |
| 512 | CodeGenFunction &CGF; |
| 513 | |
| 514 | public: |
| 515 | InlinedOpenMPRegionRAII(CodeGenFunction &CGF, |
| 516 | const OMPExecutableDirective &D); |
| 517 | ~InlinedOpenMPRegionRAII(); |
| 518 | }; |
Alexey Bataev | 23b6942 | 2014-06-18 07:08:49 +0000 | [diff] [blame] | 519 | } // namespace CodeGen |
| 520 | } // namespace clang |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 521 | |
| 522 | #endif |