blob: 27a0a3244d8492ab121c6aa78c5a1e25287a77b7 [file] [log] [blame]
Alexey Bataev9959db52014-05-06 10:08:46 +00001//===----- 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 Kramer2f5db8b2014-08-13 16:25:19 +000014#ifndef LLVM_CLANG_LIB_CODEGEN_CGOPENMPRUNTIME_H
15#define LLVM_CLANG_LIB_CODEGEN_CGOPENMPRUNTIME_H
Alexey Bataev9959db52014-05-06 10:08:46 +000016
Alexey Bataev62b63b12015-03-10 07:28:44 +000017#include "clang/AST/Type.h"
Alexander Musmanc6388682014-12-15 07:07:06 +000018#include "clang/Basic/OpenMPKinds.h"
Chandler Carruth0d9593d2015-01-14 11:29:14 +000019#include "clang/Basic/SourceLocation.h"
Alexey Bataev9959db52014-05-06 10:08:46 +000020#include "llvm/ADT/DenseMap.h"
Alexey Bataev97720002014-11-11 04:05:39 +000021#include "llvm/ADT/DenseSet.h"
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +000022#include "llvm/ADT/StringMap.h"
Alexey Bataev97720002014-11-11 04:05:39 +000023#include "llvm/IR/ValueHandle.h"
Alexey Bataev18095712014-10-10 12:19:54 +000024
25namespace llvm {
26class ArrayType;
27class Constant;
28class Function;
29class FunctionType;
Alexey Bataev97720002014-11-11 04:05:39 +000030class GlobalVariable;
Alexey Bataev18095712014-10-10 12:19:54 +000031class StructType;
32class Type;
33class Value;
34} // namespace llvm
Alexey Bataev9959db52014-05-06 10:08:46 +000035
Alexey Bataev9959db52014-05-06 10:08:46 +000036namespace clang {
Alexey Bataevcc37cc12014-11-20 04:34:54 +000037class Expr;
Alexey Bataev18095712014-10-10 12:19:54 +000038class OMPExecutableDirective;
39class VarDecl;
40
Alexey Bataev9959db52014-05-06 10:08:46 +000041namespace CodeGen {
42
Alexey Bataev18095712014-10-10 12:19:54 +000043class CodeGenFunction;
44class CodeGenModule;
Alexey Bataev9959db52014-05-06 10:08:46 +000045
Alexey Bataev6f1ffc02015-04-10 04:50:10 +000046typedef llvm::function_ref<void(CodeGenFunction &)> RegionCodeGenTy;
47
Alexey Bataev9959db52014-05-06 10:08:46 +000048class CGOpenMPRuntime {
Alexey Bataev6f1ffc02015-04-10 04:50:10 +000049private:
Alexey Bataev8f7c1b02014-12-05 04:09:23 +000050 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 Musman21212e42015-03-13 10:38:23 +000071 // Call to void __kmpc_for_static_fini(ident_t *loc, kmp_int32 global_tid);
Alexander Musmanc6388682014-12-15 07:07:06 +000072 OMPRTL__kmpc_for_static_fini,
Alexey Bataev8f7c1b02014-12-05 04:09:23 +000073 // 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 Bataevd76df6d2015-02-24 12:55:09 +000082 // Call to void __kmpc_flush(ident_t *loc);
Alexey Bataev8f7c1b02014-12-05 04:09:23 +000083 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 Bataev9f797f32015-02-05 05:57:51 +000088 // Call to kmp_int32 __kmpc_omp_taskyield(ident_t *, kmp_int32 global_tid,
89 // int end_part);
90 OMPRTL__kmpc_omp_taskyield,
Alexey Bataev6956e2e2015-02-05 06:35:41 +000091 // 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 Bataev62b63b12015-03-10 07:28:44 +000095 // 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 Bataeva63048e2015-03-23 06:18:07 +0000102 // Call to void __kmpc_copyprivate(ident_t *loc, kmp_int32 global_tid,
Alexey Bataev66beaa92015-04-30 03:47:32 +0000103 // size_t cpy_size, void *cpy_data, void(*cpy_func)(void *, void *),
Alexey Bataeva63048e2015-03-23 06:18:07 +0000104 // kmp_int32 didit);
105 OMPRTL__kmpc_copyprivate,
Alexey Bataev794ba0d2015-04-10 10:43:45 +0000106 // Call to kmp_int32 __kmpc_reduce(ident_t *loc, kmp_int32 global_tid,
107 // kmp_int32 num_vars, size_t reduce_size, void *reduce_data, void
108 // (*reduce_func)(void *lhs_data, void *rhs_data), kmp_critical_name *lck);
109 OMPRTL__kmpc_reduce,
110 // Call to kmp_int32 __kmpc_reduce_nowait(ident_t *loc, kmp_int32
111 // global_tid, kmp_int32 num_vars, size_t reduce_size, void *reduce_data,
112 // void (*reduce_func)(void *lhs_data, void *rhs_data), kmp_critical_name
113 // *lck);
114 OMPRTL__kmpc_reduce_nowait,
115 // Call to void __kmpc_end_reduce(ident_t *loc, kmp_int32 global_tid,
116 // kmp_critical_name *lck);
117 OMPRTL__kmpc_end_reduce,
118 // Call to void __kmpc_end_reduce_nowait(ident_t *loc, kmp_int32 global_tid,
119 // kmp_critical_name *lck);
120 OMPRTL__kmpc_end_reduce_nowait,
Alexey Bataev1d677132015-04-22 13:57:31 +0000121 // Call to void __kmpc_omp_task_begin_if0(ident_t *, kmp_int32 gtid,
122 // kmp_task_t * new_task);
123 OMPRTL__kmpc_omp_task_begin_if0,
124 // Call to void __kmpc_omp_task_complete_if0(ident_t *, kmp_int32 gtid,
125 // kmp_task_t * new_task);
126 OMPRTL__kmpc_omp_task_complete_if0,
Alexey Bataev98eb6e32015-04-22 11:15:40 +0000127 // Call to void __kmpc_ordered(ident_t *loc, kmp_int32 global_tid);
128 OMPRTL__kmpc_ordered,
129 // Call to void __kmpc_end_ordered(ident_t *loc, kmp_int32 global_tid);
130 OMPRTL__kmpc_end_ordered,
Alexey Bataev8b8e2022015-04-27 05:22:09 +0000131 // Call to kmp_int32 __kmpc_omp_taskwait(ident_t *loc, kmp_int32
132 // global_tid);
133 OMPRTL__kmpc_omp_taskwait,
Alexey Bataevc30dd2d2015-06-18 12:14:09 +0000134 // Call to void __kmpc_taskgroup(ident_t *loc, kmp_int32 global_tid);
135 OMPRTL__kmpc_taskgroup,
136 // Call to void __kmpc_end_taskgroup(ident_t *loc, kmp_int32 global_tid);
137 OMPRTL__kmpc_end_taskgroup,
Alexey Bataev7f210c62015-06-18 13:40:03 +0000138 // Call to void __kmpc_push_proc_bind(ident_t *loc, kmp_int32 global_tid,
139 // int proc_bind);
140 OMPRTL__kmpc_push_proc_bind,
Alexey Bataev1d2353d2015-06-24 11:01:36 +0000141 // Call to kmp_int32 __kmpc_omp_task_with_deps(ident_t *loc_ref, kmp_int32
142 // gtid, kmp_task_t * new_task, kmp_int32 ndeps, kmp_depend_info_t
143 // *dep_list, kmp_int32 ndeps_noalias, kmp_depend_info_t *noalias_dep_list);
144 OMPRTL__kmpc_omp_task_with_deps,
145 // Call to void __kmpc_omp_wait_deps(ident_t *loc_ref, kmp_int32
146 // gtid, kmp_int32 ndeps, kmp_depend_info_t *dep_list, kmp_int32
147 // ndeps_noalias, kmp_depend_info_t *noalias_dep_list);
148 OMPRTL__kmpc_omp_wait_deps,
Alexey Bataev8f7c1b02014-12-05 04:09:23 +0000149 };
150
Alexey Bataev9959db52014-05-06 10:08:46 +0000151 /// \brief Values for bit flags used in the ident_t to describe the fields.
152 /// All enumeric elements are named and described in accordance with the code
153 /// from http://llvm.org/svn/llvm-project/openmp/trunk/runtime/src/kmp.h
154 enum OpenMPLocationFlags {
155 /// \brief Use trampoline for internal microtask.
156 OMP_IDENT_IMD = 0x01,
157 /// \brief Use c-style ident structure.
158 OMP_IDENT_KMPC = 0x02,
159 /// \brief Atomic reduction option for kmpc_reduce.
160 OMP_ATOMIC_REDUCE = 0x10,
161 /// \brief Explicit 'barrier' directive.
162 OMP_IDENT_BARRIER_EXPL = 0x20,
163 /// \brief Implicit barrier in code.
164 OMP_IDENT_BARRIER_IMPL = 0x40,
165 /// \brief Implicit barrier in 'for' directive.
166 OMP_IDENT_BARRIER_IMPL_FOR = 0x40,
167 /// \brief Implicit barrier in 'sections' directive.
168 OMP_IDENT_BARRIER_IMPL_SECTIONS = 0xC0,
169 /// \brief Implicit barrier in 'single' directive.
170 OMP_IDENT_BARRIER_IMPL_SINGLE = 0x140
171 };
Alexey Bataev9959db52014-05-06 10:08:46 +0000172 CodeGenModule &CGM;
173 /// \brief Default const ident_t object used for initialization of all other
174 /// ident_t objects.
175 llvm::Constant *DefaultOpenMPPSource;
Alexey Bataev18095712014-10-10 12:19:54 +0000176 /// \brief Map of flags and corresponding default locations.
Alexey Bataev15007ba2014-05-07 06:18:01 +0000177 typedef llvm::DenseMap<unsigned, llvm::Value *> OpenMPDefaultLocMapTy;
178 OpenMPDefaultLocMapTy OpenMPDefaultLocMap;
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000179 llvm::Value *getOrCreateDefaultLocation(OpenMPLocationFlags Flags);
Alexey Bataev9959db52014-05-06 10:08:46 +0000180 /// \brief Describes ident structure that describes a source location.
181 /// All descriptions are taken from
182 /// http://llvm.org/svn/llvm-project/openmp/trunk/runtime/src/kmp.h
183 /// Original structure:
184 /// typedef struct ident {
185 /// kmp_int32 reserved_1; /**< might be used in Fortran;
186 /// see above */
187 /// kmp_int32 flags; /**< also f.flags; KMP_IDENT_xxx flags;
188 /// KMP_IDENT_KMPC identifies this union
189 /// member */
190 /// kmp_int32 reserved_2; /**< not really used in Fortran any more;
191 /// see above */
192 ///#if USE_ITT_BUILD
193 /// /* but currently used for storing
194 /// region-specific ITT */
195 /// /* contextual information. */
196 ///#endif /* USE_ITT_BUILD */
197 /// kmp_int32 reserved_3; /**< source[4] in Fortran, do not use for
198 /// C++ */
199 /// char const *psource; /**< String describing the source location.
200 /// The string is composed of semi-colon separated
201 // fields which describe the source file,
202 /// the function and a pair of line numbers that
203 /// delimit the construct.
204 /// */
205 /// } ident_t;
206 enum IdentFieldIndex {
207 /// \brief might be used in Fortran
208 IdentField_Reserved_1,
209 /// \brief OMP_IDENT_xxx flags; OMP_IDENT_KMPC identifies this union member.
210 IdentField_Flags,
211 /// \brief Not really used in Fortran any more
212 IdentField_Reserved_2,
213 /// \brief Source[4] in Fortran, do not use for C++
214 IdentField_Reserved_3,
215 /// \brief String describing the source location. The string is composed of
216 /// semi-colon separated fields which describe the source file, the function
217 /// and a pair of line numbers that delimit the construct.
218 IdentField_PSource
219 };
220 llvm::StructType *IdentTy;
Alexey Bataev18095712014-10-10 12:19:54 +0000221 /// \brief Map for SourceLocation and OpenMP runtime library debug locations.
Alexey Bataevf002aca2014-05-30 05:48:40 +0000222 typedef llvm::DenseMap<unsigned, llvm::Value *> OpenMPDebugLocMapTy;
223 OpenMPDebugLocMapTy OpenMPDebugLocMap;
Alexey Bataev9959db52014-05-06 10:08:46 +0000224 /// \brief The type for a microtask which gets passed to __kmpc_fork_call().
225 /// Original representation is:
226 /// typedef void (kmpc_micro)(kmp_int32 global_tid, kmp_int32 bound_tid,...);
227 llvm::FunctionType *Kmpc_MicroTy;
Alexey Bataev18095712014-10-10 12:19:54 +0000228 /// \brief Stores debug location and ThreadID for the function.
229 struct DebugLocThreadIdTy {
230 llvm::Value *DebugLoc;
231 llvm::Value *ThreadID;
232 };
233 /// \brief Map of local debug location, ThreadId and functions.
234 typedef llvm::DenseMap<llvm::Function *, DebugLocThreadIdTy>
235 OpenMPLocThreadIDMapTy;
236 OpenMPLocThreadIDMapTy OpenMPLocThreadIDMap;
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +0000237 /// \brief Type kmp_critical_name, originally defined as typedef kmp_int32
238 /// kmp_critical_name[8];
239 llvm::ArrayType *KmpCriticalNameTy;
Alexey Bataev97720002014-11-11 04:05:39 +0000240 /// \brief An ordered map of auto-generated variables to their unique names.
241 /// It stores variables with the following names: 1) ".gomp_critical_user_" +
242 /// <critical_section_name> + ".var" for "omp critical" directives; 2)
243 /// <mangled_name_for_global_var> + ".cache." for cache for threadprivate
244 /// variables.
245 llvm::StringMap<llvm::AssertingVH<llvm::Constant>, llvm::BumpPtrAllocator>
246 InternalVars;
Alexey Bataev62b63b12015-03-10 07:28:44 +0000247 /// \brief Type typedef kmp_int32 (* kmp_routine_entry_t)(kmp_int32, void *);
248 llvm::Type *KmpRoutineEntryPtrTy;
249 QualType KmpRoutineEntryPtrQTy;
Alexey Bataev8fc69dc2015-05-18 07:54:53 +0000250 /// \brief Type typedef struct kmp_task {
251 /// void * shareds; /**< pointer to block of pointers to
252 /// shared vars */
253 /// kmp_routine_entry_t routine; /**< pointer to routine to call for
254 /// executing task */
255 /// kmp_int32 part_id; /**< part id for the task */
256 /// kmp_routine_entry_t destructors; /* pointer to function to invoke
257 /// deconstructors of firstprivate C++ objects */
258 /// } kmp_task_t;
259 QualType KmpTaskTQTy;
Alexey Bataev1d2353d2015-06-24 11:01:36 +0000260 /// \brief Type typedef struct kmp_depend_info {
261 /// kmp_intptr_t base_addr;
262 /// size_t len;
263 /// struct {
264 /// bool in:1;
265 /// bool out:1;
266 /// } flags;
267 /// } kmp_depend_info_t;
268 QualType KmpDependInfoTy;
269
Alexey Bataev62b63b12015-03-10 07:28:44 +0000270
271 /// \brief Build type kmp_routine_entry_t (if not built yet).
272 void emitKmpRoutineEntryT(QualType KmpInt32Ty);
Alexey Bataev9959db52014-05-06 10:08:46 +0000273
Alexey Bataev9959db52014-05-06 10:08:46 +0000274 /// \brief Emits object of ident_t type with info for source location.
Alexey Bataev9959db52014-05-06 10:08:46 +0000275 /// \param Flags Flags for OpenMP location.
276 ///
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000277 llvm::Value *emitUpdateLocation(CodeGenFunction &CGF, SourceLocation Loc,
278 OpenMPLocationFlags Flags = OMP_IDENT_KMPC);
Alexey Bataev9959db52014-05-06 10:08:46 +0000279
Alexey Bataevd74d0602014-10-13 06:02:40 +0000280 /// \brief Returns pointer to ident_t type.
Alexey Bataev9959db52014-05-06 10:08:46 +0000281 llvm::Type *getIdentTyPointerTy();
282
Alexey Bataevd74d0602014-10-13 06:02:40 +0000283 /// \brief Returns pointer to kmpc_micro type.
Alexey Bataev9959db52014-05-06 10:08:46 +0000284 llvm::Type *getKmpc_MicroPointerTy();
285
286 /// \brief Returns specified OpenMP runtime function.
287 /// \param Function OpenMP runtime function.
288 /// \return Specified function.
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000289 llvm::Constant *createRuntimeFunction(OpenMPRTLFunction Function);
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +0000290
Alexander Musman21212e42015-03-13 10:38:23 +0000291 /// \brief Returns __kmpc_for_static_init_* runtime function for the specified
292 /// size \a IVSize and sign \a IVSigned.
293 llvm::Constant *createForStaticInitFunction(unsigned IVSize, bool IVSigned);
294
Alexander Musman92bdaab2015-03-12 13:37:50 +0000295 /// \brief Returns __kmpc_dispatch_init_* runtime function for the specified
296 /// size \a IVSize and sign \a IVSigned.
297 llvm::Constant *createDispatchInitFunction(unsigned IVSize, bool IVSigned);
298
299 /// \brief Returns __kmpc_dispatch_next_* runtime function for the specified
300 /// size \a IVSize and sign \a IVSigned.
301 llvm::Constant *createDispatchNextFunction(unsigned IVSize, bool IVSigned);
302
Alexey Bataev98eb6e32015-04-22 11:15:40 +0000303 /// \brief Returns __kmpc_dispatch_fini_* runtime function for the specified
304 /// size \a IVSize and sign \a IVSigned.
305 llvm::Constant *createDispatchFiniFunction(unsigned IVSize, bool IVSigned);
306
Alexey Bataev97720002014-11-11 04:05:39 +0000307 /// \brief If the specified mangled name is not in the module, create and
308 /// return threadprivate cache object. This object is a pointer's worth of
309 /// storage that's reserved for use by the OpenMP runtime.
NAKAMURA Takumicdcbfba2014-11-11 07:58:06 +0000310 /// \param VD Threadprivate variable.
Alexey Bataev97720002014-11-11 04:05:39 +0000311 /// \return Cache variable for the specified threadprivate.
312 llvm::Constant *getOrCreateThreadPrivateCache(const VarDecl *VD);
313
Alexey Bataevd74d0602014-10-13 06:02:40 +0000314 /// \brief Emits address of the word in a memory where current thread id is
315 /// stored.
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000316 virtual llvm::Value *emitThreadIDAddress(CodeGenFunction &CGF,
Alexey Bataevd74d0602014-10-13 06:02:40 +0000317 SourceLocation Loc);
318
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000319 /// \brief Gets thread id value for the current thread.
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000320 ///
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000321 llvm::Value *getThreadID(CodeGenFunction &CGF, SourceLocation Loc);
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000322
Alexey Bataev97720002014-11-11 04:05:39 +0000323 /// \brief Gets (if variable with the given name already exist) or creates
324 /// internal global variable with the specified Name. The created variable has
325 /// linkage CommonLinkage by default and is initialized by null value.
326 /// \param Ty Type of the global variable. If it is exist already the type
327 /// must be the same.
328 /// \param Name Name of the variable.
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000329 llvm::Constant *getOrCreateInternalVariable(llvm::Type *Ty,
Alexey Bataev97720002014-11-11 04:05:39 +0000330 const llvm::Twine &Name);
331
332 /// \brief Set of threadprivate variables with the generated initializer.
333 llvm::DenseSet<const VarDecl *> ThreadPrivateWithDefinition;
334
335 /// \brief Emits initialization code for the threadprivate variables.
336 /// \param VDAddr Address of the global variable \a VD.
337 /// \param Ctor Pointer to a global init function for \a VD.
338 /// \param CopyCtor Pointer to a global copy function for \a VD.
339 /// \param Dtor Pointer to a global destructor function for \a VD.
340 /// \param Loc Location of threadprivate declaration.
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000341 void emitThreadPrivateVarInit(CodeGenFunction &CGF, llvm::Value *VDAddr,
342 llvm::Value *Ctor, llvm::Value *CopyCtor,
343 llvm::Value *Dtor, SourceLocation Loc);
Alexey Bataev97720002014-11-11 04:05:39 +0000344
Alexey Bataev75ddfab2014-12-01 11:32:38 +0000345 /// \brief Returns corresponding lock object for the specified critical region
346 /// name. If the lock object does not exist it is created, otherwise the
347 /// reference to the existing copy is returned.
348 /// \param CriticalName Name of the critical region.
349 ///
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000350 llvm::Value *getCriticalRegionLock(StringRef CriticalName);
Alexey Bataev75ddfab2014-12-01 11:32:38 +0000351
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000352public:
353 explicit CGOpenMPRuntime(CodeGenModule &CGM);
354 virtual ~CGOpenMPRuntime() {}
Alexey Bataev91797552015-03-18 04:13:55 +0000355 virtual void clear();
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000356
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000357 /// \brief Emits outlined function for the specified OpenMP parallel directive
358 /// \a D. This outlined function has type void(*)(kmp_int32 *ThreadID,
359 /// kmp_int32 BoundID, struct context_vars*).
Alexey Bataev18095712014-10-10 12:19:54 +0000360 /// \param D OpenMP directive.
361 /// \param ThreadIDVar Variable for thread id in the current OpenMP region.
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000362 /// \param CodeGen Code generation sequence for the \a D directive.
363 virtual llvm::Value *
364 emitParallelOutlinedFunction(const OMPExecutableDirective &D,
365 const VarDecl *ThreadIDVar,
366 const RegionCodeGenTy &CodeGen);
Alexey Bataev18095712014-10-10 12:19:54 +0000367
Alexey Bataev62b63b12015-03-10 07:28:44 +0000368 /// \brief Emits outlined function for the OpenMP task directive \a D. This
369 /// outlined function has type void(*)(kmp_int32 ThreadID, kmp_int32
370 /// PartID, struct context_vars*).
371 /// \param D OpenMP directive.
372 /// \param ThreadIDVar Variable for thread id in the current OpenMP region.
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000373 /// \param CodeGen Code generation sequence for the \a D directive.
Alexey Bataev62b63b12015-03-10 07:28:44 +0000374 ///
375 virtual llvm::Value *emitTaskOutlinedFunction(const OMPExecutableDirective &D,
376 const VarDecl *ThreadIDVar,
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000377 const RegionCodeGenTy &CodeGen);
Alexey Bataev62b63b12015-03-10 07:28:44 +0000378
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000379 /// \brief Cleans up references to the objects in finished function.
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000380 ///
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000381 void functionFinished(CodeGenFunction &CGF);
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000382
Alexey Bataev1d677132015-04-22 13:57:31 +0000383 /// \brief Emits code for parallel or serial call of the \a OutlinedFn with
384 /// variables captured in a record which address is stored in \a
385 /// CapturedStruct.
Alexey Bataev18095712014-10-10 12:19:54 +0000386 /// \param OutlinedFn Outlined function to be run in parallel threads. Type of
Alexey Bataev62b63b12015-03-10 07:28:44 +0000387 /// this function is void(*)(kmp_int32 *, kmp_int32, struct context_vars*).
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000388 /// \param CapturedStruct A pointer to the record with the references to
389 /// variables used in \a OutlinedFn function.
Alexey Bataev1d677132015-04-22 13:57:31 +0000390 /// \param IfCond Condition in the associated 'if' clause, if it was
391 /// specified, nullptr otherwise.
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000392 ///
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000393 virtual void emitParallelCall(CodeGenFunction &CGF, SourceLocation Loc,
394 llvm::Value *OutlinedFn,
Alexey Bataev1d677132015-04-22 13:57:31 +0000395 llvm::Value *CapturedStruct,
396 const Expr *IfCond);
Alexey Bataevd74d0602014-10-13 06:02:40 +0000397
Alexey Bataev75ddfab2014-12-01 11:32:38 +0000398 /// \brief Emits a critical region.
Alexey Bataev18095712014-10-10 12:19:54 +0000399 /// \param CriticalName Name of the critical region.
Alexey Bataev75ddfab2014-12-01 11:32:38 +0000400 /// \param CriticalOpGen Generator for the statement associated with the given
401 /// critical region.
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000402 virtual void emitCriticalRegion(CodeGenFunction &CGF, StringRef CriticalName,
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000403 const RegionCodeGenTy &CriticalOpGen,
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000404 SourceLocation Loc);
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000405
Alexey Bataev8d690652014-12-04 07:23:53 +0000406 /// \brief Emits a master region.
407 /// \param MasterOpGen Generator for the statement associated with the given
408 /// master region.
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000409 virtual void emitMasterRegion(CodeGenFunction &CGF,
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000410 const RegionCodeGenTy &MasterOpGen,
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000411 SourceLocation Loc);
Alexey Bataev8d690652014-12-04 07:23:53 +0000412
Alexey Bataev9f797f32015-02-05 05:57:51 +0000413 /// \brief Emits code for a taskyield directive.
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000414 virtual void emitTaskyieldCall(CodeGenFunction &CGF, SourceLocation Loc);
Alexey Bataev9f797f32015-02-05 05:57:51 +0000415
Alexey Bataevc30dd2d2015-06-18 12:14:09 +0000416 /// \brief Emit a taskgroup region.
417 /// \param TaskgroupOpGen Generator for the statement associated with the
418 /// given taskgroup region.
419 virtual void emitTaskgroupRegion(CodeGenFunction &CGF,
420 const RegionCodeGenTy &TaskgroupOpGen,
421 SourceLocation Loc);
422
Alexey Bataev6956e2e2015-02-05 06:35:41 +0000423 /// \brief Emits a single region.
424 /// \param SingleOpGen Generator for the statement associated with the given
425 /// single region.
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000426 virtual void emitSingleRegion(CodeGenFunction &CGF,
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000427 const RegionCodeGenTy &SingleOpGen,
Alexey Bataeva63048e2015-03-23 06:18:07 +0000428 SourceLocation Loc,
429 ArrayRef<const Expr *> CopyprivateVars,
Alexey Bataev420d45b2015-04-14 05:11:24 +0000430 ArrayRef<const Expr *> DestExprs,
Alexey Bataeva63048e2015-03-23 06:18:07 +0000431 ArrayRef<const Expr *> SrcExprs,
Alexey Bataeva63048e2015-03-23 06:18:07 +0000432 ArrayRef<const Expr *> AssignmentOps);
Alexey Bataev6956e2e2015-02-05 06:35:41 +0000433
Alexey Bataev98eb6e32015-04-22 11:15:40 +0000434 /// \brief Emit an ordered region.
435 /// \param OrderedOpGen Generator for the statement associated with the given
Alexey Bataevc30dd2d2015-06-18 12:14:09 +0000436 /// ordered region.
Alexey Bataev98eb6e32015-04-22 11:15:40 +0000437 virtual void emitOrderedRegion(CodeGenFunction &CGF,
438 const RegionCodeGenTy &OrderedOpGen,
439 SourceLocation Loc);
440
Alexey Bataevf2685682015-03-30 04:30:22 +0000441 /// \brief Emit an implicit/explicit barrier for OpenMP threads.
442 /// \param Kind Directive for which this implicit barrier call must be
443 /// generated. Must be OMPD_barrier for explicit barrier generation.
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000444 ///
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000445 virtual void emitBarrierCall(CodeGenFunction &CGF, SourceLocation Loc,
Alexey Bataevf2685682015-03-30 04:30:22 +0000446 OpenMPDirectiveKind Kind);
Alexey Bataevb2059782014-10-13 08:23:51 +0000447
Alexander Musmanc6388682014-12-15 07:07:06 +0000448 /// \brief Check if the specified \a ScheduleKind is static non-chunked.
449 /// This kind of worksharing directive is emitted without outer loop.
450 /// \param ScheduleKind Schedule kind specified in the 'schedule' clause.
451 /// \param Chunked True if chunk is specified in the clause.
452 ///
453 virtual bool isStaticNonchunked(OpenMPScheduleClauseKind ScheduleKind,
454 bool Chunked) const;
455
Alexander Musmandf7a8e22015-01-22 08:49:35 +0000456 /// \brief Check if the specified \a ScheduleKind is dynamic.
457 /// This kind of worksharing directive is emitted without outer loop.
458 /// \param ScheduleKind Schedule Kind specified in the 'schedule' clause.
459 ///
460 virtual bool isDynamic(OpenMPScheduleClauseKind ScheduleKind) const;
461
Alexander Musmanc6388682014-12-15 07:07:06 +0000462 /// \brief Call the appropriate runtime routine to initialize it before start
463 /// of loop.
464 ///
465 /// Depending on the loop schedule, it is nesessary to call some runtime
466 /// routine before start of the OpenMP loop to get the loop upper / lower
467 /// bounds \a LB and \a UB and stride \a ST.
468 ///
469 /// \param CGF Reference to current CodeGenFunction.
470 /// \param Loc Clang source location.
NAKAMURA Takumieca08382014-12-17 14:47:06 +0000471 /// \param SchedKind Schedule kind, specified by the 'schedule' clause.
Alexander Musmanc6388682014-12-15 07:07:06 +0000472 /// \param IVSize Size of the iteration variable in bits.
473 /// \param IVSigned Sign of the interation variable.
Alexey Bataevd7589ffe2015-05-20 13:12:48 +0000474 /// \param Ordered true if loop is ordered, false otherwise.
Alexander Musmanc6388682014-12-15 07:07:06 +0000475 /// \param IL Address of the output variable in which the flag of the
476 /// last iteration is returned.
477 /// \param LB Address of the output variable in which the lower iteration
478 /// number is returned.
479 /// \param UB Address of the output variable in which the upper iteration
480 /// number is returned.
481 /// \param ST Address of the output variable in which the stride value is
482 /// returned nesessary to generated the static_chunked scheduled loop.
483 /// \param Chunk Value of the chunk for the static_chunked scheduled loop.
484 /// For the default (nullptr) value, the chunk 1 will be used.
485 ///
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000486 virtual void emitForInit(CodeGenFunction &CGF, SourceLocation Loc,
487 OpenMPScheduleClauseKind SchedKind, unsigned IVSize,
Alexey Bataevd7589ffe2015-05-20 13:12:48 +0000488 bool IVSigned, bool Ordered, llvm::Value *IL,
489 llvm::Value *LB, llvm::Value *UB, llvm::Value *ST,
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000490 llvm::Value *Chunk = nullptr);
Alexander Musmanc6388682014-12-15 07:07:06 +0000491
492 /// \brief Call the appropriate runtime routine to notify that we finished
Alexey Bataev98eb6e32015-04-22 11:15:40 +0000493 /// iteration of the ordered loop with the dynamic scheduling.
494 ///
495 /// \param CGF Reference to current CodeGenFunction.
496 /// \param Loc Clang source location.
497 /// \param IVSize Size of the iteration variable in bits.
498 /// \param IVSigned Sign of the interation variable.
499 ///
Alexey Bataevd7589ffe2015-05-20 13:12:48 +0000500 virtual void emitForOrderedIterationEnd(CodeGenFunction &CGF,
501 SourceLocation Loc, unsigned IVSize,
502 bool IVSigned);
Alexey Bataev98eb6e32015-04-22 11:15:40 +0000503
504 /// \brief Call the appropriate runtime routine to notify that we finished
Alexander Musmanc6388682014-12-15 07:07:06 +0000505 /// all the work with current loop.
506 ///
507 /// \param CGF Reference to current CodeGenFunction.
508 /// \param Loc Clang source location.
Alexander Musmanc6388682014-12-15 07:07:06 +0000509 ///
Alexey Bataev98eb6e32015-04-22 11:15:40 +0000510 virtual void emitForStaticFinish(CodeGenFunction &CGF, SourceLocation Loc);
Alexander Musmanc6388682014-12-15 07:07:06 +0000511
Alexander Musman92bdaab2015-03-12 13:37:50 +0000512 /// Call __kmpc_dispatch_next(
513 /// ident_t *loc, kmp_int32 tid, kmp_int32 *p_lastiter,
514 /// kmp_int[32|64] *p_lower, kmp_int[32|64] *p_upper,
515 /// kmp_int[32|64] *p_stride);
516 /// \param IVSize Size of the iteration variable in bits.
517 /// \param IVSigned Sign of the interation variable.
518 /// \param IL Address of the output variable in which the flag of the
519 /// last iteration is returned.
520 /// \param LB Address of the output variable in which the lower iteration
521 /// number is returned.
522 /// \param UB Address of the output variable in which the upper iteration
523 /// number is returned.
524 /// \param ST Address of the output variable in which the stride value is
525 /// returned.
526 virtual llvm::Value *emitForNext(CodeGenFunction &CGF, SourceLocation Loc,
527 unsigned IVSize, bool IVSigned,
528 llvm::Value *IL, llvm::Value *LB,
529 llvm::Value *UB, llvm::Value *ST);
530
Alexey Bataevb2059782014-10-13 08:23:51 +0000531 /// \brief Emits call to void __kmpc_push_num_threads(ident_t *loc, kmp_int32
532 /// global_tid, kmp_int32 num_threads) to generate code for 'num_threads'
533 /// clause.
534 /// \param NumThreads An integer value of threads.
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000535 virtual void emitNumThreadsClause(CodeGenFunction &CGF,
536 llvm::Value *NumThreads,
537 SourceLocation Loc);
Alexey Bataev97720002014-11-11 04:05:39 +0000538
Alexey Bataev7f210c62015-06-18 13:40:03 +0000539 /// \brief Emit call to void __kmpc_push_proc_bind(ident_t *loc, kmp_int32
540 /// global_tid, int proc_bind) to generate code for 'proc_bind' clause.
541 virtual void emitProcBindClause(CodeGenFunction &CGF,
542 OpenMPProcBindClauseKind ProcBind,
543 SourceLocation Loc);
544
Alexey Bataev97720002014-11-11 04:05:39 +0000545 /// \brief Returns address of the threadprivate variable for the current
546 /// thread.
NAKAMURA Takumicdcbfba2014-11-11 07:58:06 +0000547 /// \param VD Threadprivate variable.
Alexey Bataev97720002014-11-11 04:05:39 +0000548 /// \param VDAddr Address of the global variable \a VD.
549 /// \param Loc Location of the reference to threadprivate var.
550 /// \return Address of the threadprivate variable for the current thread.
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000551 virtual llvm::Value *getAddrOfThreadPrivate(CodeGenFunction &CGF,
552 const VarDecl *VD,
553 llvm::Value *VDAddr,
554 SourceLocation Loc);
Alexey Bataev97720002014-11-11 04:05:39 +0000555
556 /// \brief Emit a code for initialization of threadprivate variable. It emits
557 /// a call to runtime library which adds initial value to the newly created
558 /// threadprivate variable (if it is not constant) and registers destructor
559 /// for the variable (if any).
560 /// \param VD Threadprivate variable.
561 /// \param VDAddr Address of the global variable \a VD.
562 /// \param Loc Location of threadprivate declaration.
563 /// \param PerformInit true if initialization expression is not constant.
564 virtual llvm::Function *
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000565 emitThreadPrivateVarDefinition(const VarDecl *VD, llvm::Value *VDAddr,
566 SourceLocation Loc, bool PerformInit,
567 CodeGenFunction *CGF = nullptr);
Alexey Bataevcc37cc12014-11-20 04:34:54 +0000568
569 /// \brief Emit flush of the variables specified in 'omp flush' directive.
570 /// \param Vars List of variables to flush.
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000571 virtual void emitFlush(CodeGenFunction &CGF, ArrayRef<const Expr *> Vars,
572 SourceLocation Loc);
Alexey Bataev62b63b12015-03-10 07:28:44 +0000573
574 /// \brief Emit task region for the task directive. The task region is
Nico Weber20b0ce32015-04-28 18:19:18 +0000575 /// emitted in several steps:
Alexey Bataev62b63b12015-03-10 07:28:44 +0000576 /// 1. Emit a call to kmp_task_t *__kmpc_omp_task_alloc(ident_t *, kmp_int32
577 /// gtid, kmp_int32 flags, size_t sizeof_kmp_task_t, size_t sizeof_shareds,
578 /// kmp_routine_entry_t *task_entry). Here task_entry is a pointer to the
579 /// function:
580 /// kmp_int32 .omp_task_entry.(kmp_int32 gtid, kmp_task_t *tt) {
581 /// TaskFunction(gtid, tt->part_id, tt->shareds);
582 /// return 0;
583 /// }
584 /// 2. Copy a list of shared variables to field shareds of the resulting
585 /// structure kmp_task_t returned by the previous call (if any).
586 /// 3. Copy a pointer to destructions function to field destructions of the
587 /// resulting structure kmp_task_t.
588 /// 4. Emit a call to kmp_int32 __kmpc_omp_task(ident_t *, kmp_int32 gtid,
589 /// kmp_task_t *new_task), where new_task is a resulting structure from
590 /// previous items.
Alexey Bataev36c1eb92015-04-30 06:51:57 +0000591 /// \param D Current task directive.
Alexey Bataev62b63b12015-03-10 07:28:44 +0000592 /// \param Tied true if the task is tied (the task is tied to the thread that
593 /// can suspend its task region), false - untied (the task is not tied to any
594 /// thread).
595 /// \param Final Contains either constant bool value, or llvm::Value * of i1
596 /// type for final clause. If the value is true, the task forces all of its
597 /// child tasks to become final and included tasks.
598 /// \param TaskFunction An LLVM function with type void (*)(i32 /*gtid*/, i32
599 /// /*part_id*/, captured_struct */*__context*/);
600 /// \param SharedsTy A type which contains references the shared variables.
Alexey Bataev1d2353d2015-06-24 11:01:36 +0000601 /// \param Shareds Context with the list of shared variables from the \p
Alexey Bataev62b63b12015-03-10 07:28:44 +0000602 /// TaskFunction.
Alexey Bataev1d677132015-04-22 13:57:31 +0000603 /// \param IfCond Not a nullptr if 'if' clause was specified, nullptr
604 /// otherwise.
Alexey Bataev36c1eb92015-04-30 06:51:57 +0000605 /// \param PrivateVars List of references to private variables for the task
606 /// directive.
607 /// \param PrivateCopies List of private copies for each private variable in
Alexey Bataev9e034042015-05-05 04:05:12 +0000608 /// \p PrivateVars.
609 /// \param FirstprivateVars List of references to private variables for the
610 /// task directive.
611 /// \param FirstprivateCopies List of private copies for each private variable
612 /// in \p FirstprivateVars.
613 /// \param FirstprivateInits List of references to auto generated variables
614 /// used for initialization of a single array element. Used if firstprivate
615 /// variable is of array type.
Alexey Bataev1d2353d2015-06-24 11:01:36 +0000616 /// \param Dependences List of dependences for the 'task' construct, including
617 /// original expression and dependency type.
618 virtual void emitTaskCall(
619 CodeGenFunction &CGF, SourceLocation Loc, const OMPExecutableDirective &D,
620 bool Tied, llvm::PointerIntPair<llvm::Value *, 1, bool> Final,
621 llvm::Value *TaskFunction, QualType SharedsTy, llvm::Value *Shareds,
622 const Expr *IfCond, ArrayRef<const Expr *> PrivateVars,
623 ArrayRef<const Expr *> PrivateCopies,
624 ArrayRef<const Expr *> FirstprivateVars,
625 ArrayRef<const Expr *> FirstprivateCopies,
626 ArrayRef<const Expr *> FirstprivateInits,
627 ArrayRef<std::pair<OpenMPDependClauseKind, const Expr *>> Dependences);
Alexey Bataev794ba0d2015-04-10 10:43:45 +0000628
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000629 /// \brief Emit code for the directive that does not require outlining.
630 ///
631 /// \param CodeGen Code generation sequence for the \a D directive.
632 virtual void emitInlinedDirective(CodeGenFunction &CGF,
633 const RegionCodeGenTy &CodeGen);
Alexey Bataev794ba0d2015-04-10 10:43:45 +0000634 /// \brief Emit a code for reduction clause. Next code should be emitted for
635 /// reduction:
636 /// \code
637 ///
638 /// static kmp_critical_name lock = { 0 };
639 ///
640 /// void reduce_func(void *lhs[<n>], void *rhs[<n>]) {
641 /// ...
642 /// *(Type<i>*)lhs[i] = RedOp<i>(*(Type<i>*)lhs[i], *(Type<i>*)rhs[i]);
643 /// ...
644 /// }
645 ///
646 /// ...
647 /// void *RedList[<n>] = {&<RHSExprs>[0], ..., &<RHSExprs>[<n>-1]};
648 /// switch (__kmpc_reduce{_nowait}(<loc>, <gtid>, <n>, sizeof(RedList),
649 /// RedList, reduce_func, &<lock>)) {
650 /// case 1:
651 /// ...
652 /// <LHSExprs>[i] = RedOp<i>(*<LHSExprs>[i], *<RHSExprs>[i]);
653 /// ...
654 /// __kmpc_end_reduce{_nowait}(<loc>, <gtid>, &<lock>);
655 /// break;
656 /// case 2:
657 /// ...
658 /// Atomic(<LHSExprs>[i] = RedOp<i>(*<LHSExprs>[i], *<RHSExprs>[i]));
659 /// ...
660 /// break;
661 /// default:;
662 /// }
663 /// \endcode
664 ///
665 /// \param LHSExprs List of LHS in \a ReductionOps reduction operations.
666 /// \param RHSExprs List of RHS in \a ReductionOps reduction operations.
667 /// \param ReductionOps List of reduction operations in form 'LHS binop RHS'
668 /// or 'operator binop(LHS, RHS)'.
669 /// \param WithNowait true if parent directive has also nowait clause, false
670 /// otherwise.
671 virtual void emitReduction(CodeGenFunction &CGF, SourceLocation Loc,
672 ArrayRef<const Expr *> LHSExprs,
673 ArrayRef<const Expr *> RHSExprs,
674 ArrayRef<const Expr *> ReductionOps,
Alexey Bataev89e7e8e2015-06-17 06:21:39 +0000675 bool WithNowait, bool SimpleReduction);
Alexey Bataev8b8e2022015-04-27 05:22:09 +0000676
677 /// \brief Emit code for 'taskwait' directive.
678 virtual void emitTaskwaitCall(CodeGenFunction &CGF, SourceLocation Loc);
Alexey Bataev9959db52014-05-06 10:08:46 +0000679};
Alexey Bataev8cbe0a62015-02-26 10:27:34 +0000680
Alexey Bataev23b69422014-06-18 07:08:49 +0000681} // namespace CodeGen
682} // namespace clang
Alexey Bataev9959db52014-05-06 10:08:46 +0000683
684#endif