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