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