blob: b1c70705757b7d528f546ab10f5a3cc4e78a9cf3 [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 Bataev8f7c1b02014-12-05 04:09:23 +0000134 };
135
Alexey Bataev9959db52014-05-06 10:08:46 +0000136 /// \brief Values for bit flags used in the ident_t to describe the fields.
137 /// All enumeric elements are named and described in accordance with the code
138 /// from http://llvm.org/svn/llvm-project/openmp/trunk/runtime/src/kmp.h
139 enum OpenMPLocationFlags {
140 /// \brief Use trampoline for internal microtask.
141 OMP_IDENT_IMD = 0x01,
142 /// \brief Use c-style ident structure.
143 OMP_IDENT_KMPC = 0x02,
144 /// \brief Atomic reduction option for kmpc_reduce.
145 OMP_ATOMIC_REDUCE = 0x10,
146 /// \brief Explicit 'barrier' directive.
147 OMP_IDENT_BARRIER_EXPL = 0x20,
148 /// \brief Implicit barrier in code.
149 OMP_IDENT_BARRIER_IMPL = 0x40,
150 /// \brief Implicit barrier in 'for' directive.
151 OMP_IDENT_BARRIER_IMPL_FOR = 0x40,
152 /// \brief Implicit barrier in 'sections' directive.
153 OMP_IDENT_BARRIER_IMPL_SECTIONS = 0xC0,
154 /// \brief Implicit barrier in 'single' directive.
155 OMP_IDENT_BARRIER_IMPL_SINGLE = 0x140
156 };
Alexey Bataev9959db52014-05-06 10:08:46 +0000157 CodeGenModule &CGM;
158 /// \brief Default const ident_t object used for initialization of all other
159 /// ident_t objects.
160 llvm::Constant *DefaultOpenMPPSource;
Alexey Bataev18095712014-10-10 12:19:54 +0000161 /// \brief Map of flags and corresponding default locations.
Alexey Bataev15007ba2014-05-07 06:18:01 +0000162 typedef llvm::DenseMap<unsigned, llvm::Value *> OpenMPDefaultLocMapTy;
163 OpenMPDefaultLocMapTy OpenMPDefaultLocMap;
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000164 llvm::Value *getOrCreateDefaultLocation(OpenMPLocationFlags Flags);
Alexey Bataev9959db52014-05-06 10:08:46 +0000165 /// \brief Describes ident structure that describes a source location.
166 /// All descriptions are taken from
167 /// http://llvm.org/svn/llvm-project/openmp/trunk/runtime/src/kmp.h
168 /// Original structure:
169 /// typedef struct ident {
170 /// kmp_int32 reserved_1; /**< might be used in Fortran;
171 /// see above */
172 /// kmp_int32 flags; /**< also f.flags; KMP_IDENT_xxx flags;
173 /// KMP_IDENT_KMPC identifies this union
174 /// member */
175 /// kmp_int32 reserved_2; /**< not really used in Fortran any more;
176 /// see above */
177 ///#if USE_ITT_BUILD
178 /// /* but currently used for storing
179 /// region-specific ITT */
180 /// /* contextual information. */
181 ///#endif /* USE_ITT_BUILD */
182 /// kmp_int32 reserved_3; /**< source[4] in Fortran, do not use for
183 /// C++ */
184 /// char const *psource; /**< String describing the source location.
185 /// The string is composed of semi-colon separated
186 // fields which describe the source file,
187 /// the function and a pair of line numbers that
188 /// delimit the construct.
189 /// */
190 /// } ident_t;
191 enum IdentFieldIndex {
192 /// \brief might be used in Fortran
193 IdentField_Reserved_1,
194 /// \brief OMP_IDENT_xxx flags; OMP_IDENT_KMPC identifies this union member.
195 IdentField_Flags,
196 /// \brief Not really used in Fortran any more
197 IdentField_Reserved_2,
198 /// \brief Source[4] in Fortran, do not use for C++
199 IdentField_Reserved_3,
200 /// \brief String describing the source location. The string is composed of
201 /// semi-colon separated fields which describe the source file, the function
202 /// and a pair of line numbers that delimit the construct.
203 IdentField_PSource
204 };
205 llvm::StructType *IdentTy;
Alexey Bataev18095712014-10-10 12:19:54 +0000206 /// \brief Map for SourceLocation and OpenMP runtime library debug locations.
Alexey Bataevf002aca2014-05-30 05:48:40 +0000207 typedef llvm::DenseMap<unsigned, llvm::Value *> OpenMPDebugLocMapTy;
208 OpenMPDebugLocMapTy OpenMPDebugLocMap;
Alexey Bataev9959db52014-05-06 10:08:46 +0000209 /// \brief The type for a microtask which gets passed to __kmpc_fork_call().
210 /// Original representation is:
211 /// typedef void (kmpc_micro)(kmp_int32 global_tid, kmp_int32 bound_tid,...);
212 llvm::FunctionType *Kmpc_MicroTy;
Alexey Bataev18095712014-10-10 12:19:54 +0000213 /// \brief Stores debug location and ThreadID for the function.
214 struct DebugLocThreadIdTy {
215 llvm::Value *DebugLoc;
216 llvm::Value *ThreadID;
217 };
218 /// \brief Map of local debug location, ThreadId and functions.
219 typedef llvm::DenseMap<llvm::Function *, DebugLocThreadIdTy>
220 OpenMPLocThreadIDMapTy;
221 OpenMPLocThreadIDMapTy OpenMPLocThreadIDMap;
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +0000222 /// \brief Type kmp_critical_name, originally defined as typedef kmp_int32
223 /// kmp_critical_name[8];
224 llvm::ArrayType *KmpCriticalNameTy;
Alexey Bataev97720002014-11-11 04:05:39 +0000225 /// \brief An ordered map of auto-generated variables to their unique names.
226 /// It stores variables with the following names: 1) ".gomp_critical_user_" +
227 /// <critical_section_name> + ".var" for "omp critical" directives; 2)
228 /// <mangled_name_for_global_var> + ".cache." for cache for threadprivate
229 /// variables.
230 llvm::StringMap<llvm::AssertingVH<llvm::Constant>, llvm::BumpPtrAllocator>
231 InternalVars;
Alexey Bataev62b63b12015-03-10 07:28:44 +0000232 /// \brief Type typedef kmp_int32 (* kmp_routine_entry_t)(kmp_int32, void *);
233 llvm::Type *KmpRoutineEntryPtrTy;
234 QualType KmpRoutineEntryPtrQTy;
235
236 /// \brief Build type kmp_routine_entry_t (if not built yet).
237 void emitKmpRoutineEntryT(QualType KmpInt32Ty);
Alexey Bataev9959db52014-05-06 10:08:46 +0000238
Alexey Bataev9959db52014-05-06 10:08:46 +0000239 /// \brief Emits object of ident_t type with info for source location.
Alexey Bataev9959db52014-05-06 10:08:46 +0000240 /// \param Flags Flags for OpenMP location.
241 ///
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000242 llvm::Value *emitUpdateLocation(CodeGenFunction &CGF, SourceLocation Loc,
243 OpenMPLocationFlags Flags = OMP_IDENT_KMPC);
Alexey Bataev9959db52014-05-06 10:08:46 +0000244
Alexey Bataevd74d0602014-10-13 06:02:40 +0000245 /// \brief Returns pointer to ident_t type.
Alexey Bataev9959db52014-05-06 10:08:46 +0000246 llvm::Type *getIdentTyPointerTy();
247
Alexey Bataevd74d0602014-10-13 06:02:40 +0000248 /// \brief Returns pointer to kmpc_micro type.
Alexey Bataev9959db52014-05-06 10:08:46 +0000249 llvm::Type *getKmpc_MicroPointerTy();
250
251 /// \brief Returns specified OpenMP runtime function.
252 /// \param Function OpenMP runtime function.
253 /// \return Specified function.
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000254 llvm::Constant *createRuntimeFunction(OpenMPRTLFunction Function);
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +0000255
Alexander Musman21212e42015-03-13 10:38:23 +0000256 /// \brief Returns __kmpc_for_static_init_* runtime function for the specified
257 /// size \a IVSize and sign \a IVSigned.
258 llvm::Constant *createForStaticInitFunction(unsigned IVSize, bool IVSigned);
259
Alexander Musman92bdaab2015-03-12 13:37:50 +0000260 /// \brief Returns __kmpc_dispatch_init_* runtime function for the specified
261 /// size \a IVSize and sign \a IVSigned.
262 llvm::Constant *createDispatchInitFunction(unsigned IVSize, bool IVSigned);
263
264 /// \brief Returns __kmpc_dispatch_next_* runtime function for the specified
265 /// size \a IVSize and sign \a IVSigned.
266 llvm::Constant *createDispatchNextFunction(unsigned IVSize, bool IVSigned);
267
Alexey Bataev98eb6e32015-04-22 11:15:40 +0000268 /// \brief Returns __kmpc_dispatch_fini_* runtime function for the specified
269 /// size \a IVSize and sign \a IVSigned.
270 llvm::Constant *createDispatchFiniFunction(unsigned IVSize, bool IVSigned);
271
Alexey Bataev97720002014-11-11 04:05:39 +0000272 /// \brief If the specified mangled name is not in the module, create and
273 /// return threadprivate cache object. This object is a pointer's worth of
274 /// storage that's reserved for use by the OpenMP runtime.
NAKAMURA Takumicdcbfba2014-11-11 07:58:06 +0000275 /// \param VD Threadprivate variable.
Alexey Bataev97720002014-11-11 04:05:39 +0000276 /// \return Cache variable for the specified threadprivate.
277 llvm::Constant *getOrCreateThreadPrivateCache(const VarDecl *VD);
278
Alexey Bataevd74d0602014-10-13 06:02:40 +0000279 /// \brief Emits address of the word in a memory where current thread id is
280 /// stored.
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000281 virtual llvm::Value *emitThreadIDAddress(CodeGenFunction &CGF,
Alexey Bataevd74d0602014-10-13 06:02:40 +0000282 SourceLocation Loc);
283
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000284 /// \brief Gets thread id value for the current thread.
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000285 ///
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000286 llvm::Value *getThreadID(CodeGenFunction &CGF, SourceLocation Loc);
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000287
Alexey Bataev97720002014-11-11 04:05:39 +0000288 /// \brief Gets (if variable with the given name already exist) or creates
289 /// internal global variable with the specified Name. The created variable has
290 /// linkage CommonLinkage by default and is initialized by null value.
291 /// \param Ty Type of the global variable. If it is exist already the type
292 /// must be the same.
293 /// \param Name Name of the variable.
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000294 llvm::Constant *getOrCreateInternalVariable(llvm::Type *Ty,
Alexey Bataev97720002014-11-11 04:05:39 +0000295 const llvm::Twine &Name);
296
297 /// \brief Set of threadprivate variables with the generated initializer.
298 llvm::DenseSet<const VarDecl *> ThreadPrivateWithDefinition;
299
300 /// \brief Emits initialization code for the threadprivate variables.
301 /// \param VDAddr Address of the global variable \a VD.
302 /// \param Ctor Pointer to a global init function for \a VD.
303 /// \param CopyCtor Pointer to a global copy function for \a VD.
304 /// \param Dtor Pointer to a global destructor function for \a VD.
305 /// \param Loc Location of threadprivate declaration.
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000306 void emitThreadPrivateVarInit(CodeGenFunction &CGF, llvm::Value *VDAddr,
307 llvm::Value *Ctor, llvm::Value *CopyCtor,
308 llvm::Value *Dtor, SourceLocation Loc);
Alexey Bataev97720002014-11-11 04:05:39 +0000309
Alexey Bataev75ddfab2014-12-01 11:32:38 +0000310 /// \brief Returns corresponding lock object for the specified critical region
311 /// name. If the lock object does not exist it is created, otherwise the
312 /// reference to the existing copy is returned.
313 /// \param CriticalName Name of the critical region.
314 ///
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000315 llvm::Value *getCriticalRegionLock(StringRef CriticalName);
Alexey Bataev75ddfab2014-12-01 11:32:38 +0000316
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000317public:
318 explicit CGOpenMPRuntime(CodeGenModule &CGM);
319 virtual ~CGOpenMPRuntime() {}
Alexey Bataev91797552015-03-18 04:13:55 +0000320 virtual void clear();
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000321
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000322 /// \brief Emits outlined function for the specified OpenMP parallel directive
323 /// \a D. This outlined function has type void(*)(kmp_int32 *ThreadID,
324 /// kmp_int32 BoundID, struct context_vars*).
Alexey Bataev18095712014-10-10 12:19:54 +0000325 /// \param D OpenMP directive.
326 /// \param ThreadIDVar Variable for thread id in the current OpenMP region.
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000327 /// \param CodeGen Code generation sequence for the \a D directive.
328 virtual llvm::Value *
329 emitParallelOutlinedFunction(const OMPExecutableDirective &D,
330 const VarDecl *ThreadIDVar,
331 const RegionCodeGenTy &CodeGen);
Alexey Bataev18095712014-10-10 12:19:54 +0000332
Alexey Bataev62b63b12015-03-10 07:28:44 +0000333 /// \brief Emits outlined function for the OpenMP task directive \a D. This
334 /// outlined function has type void(*)(kmp_int32 ThreadID, kmp_int32
335 /// PartID, struct context_vars*).
336 /// \param D OpenMP directive.
337 /// \param ThreadIDVar Variable for thread id in the current OpenMP region.
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000338 /// \param CodeGen Code generation sequence for the \a D directive.
Alexey Bataev62b63b12015-03-10 07:28:44 +0000339 ///
340 virtual llvm::Value *emitTaskOutlinedFunction(const OMPExecutableDirective &D,
341 const VarDecl *ThreadIDVar,
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000342 const RegionCodeGenTy &CodeGen);
Alexey Bataev62b63b12015-03-10 07:28:44 +0000343
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000344 /// \brief Cleans up references to the objects in finished function.
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000345 ///
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000346 void functionFinished(CodeGenFunction &CGF);
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000347
Alexey Bataev1d677132015-04-22 13:57:31 +0000348 /// \brief Emits code for parallel or serial call of the \a OutlinedFn with
349 /// variables captured in a record which address is stored in \a
350 /// CapturedStruct.
Alexey Bataev18095712014-10-10 12:19:54 +0000351 /// \param OutlinedFn Outlined function to be run in parallel threads. Type of
Alexey Bataev62b63b12015-03-10 07:28:44 +0000352 /// this function is void(*)(kmp_int32 *, kmp_int32, struct context_vars*).
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000353 /// \param CapturedStruct A pointer to the record with the references to
354 /// variables used in \a OutlinedFn function.
Alexey Bataev1d677132015-04-22 13:57:31 +0000355 /// \param IfCond Condition in the associated 'if' clause, if it was
356 /// specified, nullptr otherwise.
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000357 ///
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000358 virtual void emitParallelCall(CodeGenFunction &CGF, SourceLocation Loc,
359 llvm::Value *OutlinedFn,
Alexey Bataev1d677132015-04-22 13:57:31 +0000360 llvm::Value *CapturedStruct,
361 const Expr *IfCond);
Alexey Bataevd74d0602014-10-13 06:02:40 +0000362
Alexey Bataev75ddfab2014-12-01 11:32:38 +0000363 /// \brief Emits a critical region.
Alexey Bataev18095712014-10-10 12:19:54 +0000364 /// \param CriticalName Name of the critical region.
Alexey Bataev75ddfab2014-12-01 11:32:38 +0000365 /// \param CriticalOpGen Generator for the statement associated with the given
366 /// critical region.
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000367 virtual void emitCriticalRegion(CodeGenFunction &CGF, StringRef CriticalName,
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000368 const RegionCodeGenTy &CriticalOpGen,
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000369 SourceLocation Loc);
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000370
Alexey Bataev8d690652014-12-04 07:23:53 +0000371 /// \brief Emits a master region.
372 /// \param MasterOpGen Generator for the statement associated with the given
373 /// master region.
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000374 virtual void emitMasterRegion(CodeGenFunction &CGF,
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000375 const RegionCodeGenTy &MasterOpGen,
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000376 SourceLocation Loc);
Alexey Bataev8d690652014-12-04 07:23:53 +0000377
Alexey Bataev9f797f32015-02-05 05:57:51 +0000378 /// \brief Emits code for a taskyield directive.
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000379 virtual void emitTaskyieldCall(CodeGenFunction &CGF, SourceLocation Loc);
Alexey Bataev9f797f32015-02-05 05:57:51 +0000380
Alexey Bataev6956e2e2015-02-05 06:35:41 +0000381 /// \brief Emits a single region.
382 /// \param SingleOpGen Generator for the statement associated with the given
383 /// single region.
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000384 virtual void emitSingleRegion(CodeGenFunction &CGF,
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000385 const RegionCodeGenTy &SingleOpGen,
Alexey Bataeva63048e2015-03-23 06:18:07 +0000386 SourceLocation Loc,
387 ArrayRef<const Expr *> CopyprivateVars,
Alexey Bataev420d45b2015-04-14 05:11:24 +0000388 ArrayRef<const Expr *> DestExprs,
Alexey Bataeva63048e2015-03-23 06:18:07 +0000389 ArrayRef<const Expr *> SrcExprs,
Alexey Bataeva63048e2015-03-23 06:18:07 +0000390 ArrayRef<const Expr *> AssignmentOps);
Alexey Bataev6956e2e2015-02-05 06:35:41 +0000391
Alexey Bataev98eb6e32015-04-22 11:15:40 +0000392 /// \brief Emit an ordered region.
393 /// \param OrderedOpGen Generator for the statement associated with the given
394 /// critical region.
395 virtual void emitOrderedRegion(CodeGenFunction &CGF,
396 const RegionCodeGenTy &OrderedOpGen,
397 SourceLocation Loc);
398
Alexey Bataevf2685682015-03-30 04:30:22 +0000399 /// \brief Emit an implicit/explicit barrier for OpenMP threads.
400 /// \param Kind Directive for which this implicit barrier call must be
401 /// generated. Must be OMPD_barrier for explicit barrier generation.
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000402 ///
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000403 virtual void emitBarrierCall(CodeGenFunction &CGF, SourceLocation Loc,
Alexey Bataevf2685682015-03-30 04:30:22 +0000404 OpenMPDirectiveKind Kind);
Alexey Bataevb2059782014-10-13 08:23:51 +0000405
Alexander Musmanc6388682014-12-15 07:07:06 +0000406 /// \brief Check if the specified \a ScheduleKind is static non-chunked.
407 /// This kind of worksharing directive is emitted without outer loop.
408 /// \param ScheduleKind Schedule kind specified in the 'schedule' clause.
409 /// \param Chunked True if chunk is specified in the clause.
410 ///
411 virtual bool isStaticNonchunked(OpenMPScheduleClauseKind ScheduleKind,
412 bool Chunked) const;
413
Alexander Musmandf7a8e22015-01-22 08:49:35 +0000414 /// \brief Check if the specified \a ScheduleKind is dynamic.
415 /// This kind of worksharing directive is emitted without outer loop.
416 /// \param ScheduleKind Schedule Kind specified in the 'schedule' clause.
417 ///
418 virtual bool isDynamic(OpenMPScheduleClauseKind ScheduleKind) const;
419
Alexander Musmanc6388682014-12-15 07:07:06 +0000420 /// \brief Call the appropriate runtime routine to initialize it before start
421 /// of loop.
422 ///
423 /// Depending on the loop schedule, it is nesessary to call some runtime
424 /// routine before start of the OpenMP loop to get the loop upper / lower
425 /// bounds \a LB and \a UB and stride \a ST.
426 ///
427 /// \param CGF Reference to current CodeGenFunction.
428 /// \param Loc Clang source location.
NAKAMURA Takumieca08382014-12-17 14:47:06 +0000429 /// \param SchedKind Schedule kind, specified by the 'schedule' clause.
Alexander Musmanc6388682014-12-15 07:07:06 +0000430 /// \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 nesessary to generated the static_chunked scheduled loop.
440 /// \param Chunk Value of the chunk for the static_chunked scheduled loop.
441 /// For the default (nullptr) value, the chunk 1 will be used.
442 ///
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000443 virtual void emitForInit(CodeGenFunction &CGF, SourceLocation Loc,
444 OpenMPScheduleClauseKind SchedKind, unsigned IVSize,
445 bool IVSigned, llvm::Value *IL, llvm::Value *LB,
446 llvm::Value *UB, llvm::Value *ST,
447 llvm::Value *Chunk = nullptr);
Alexander Musmanc6388682014-12-15 07:07:06 +0000448
449 /// \brief Call the appropriate runtime routine to notify that we finished
Alexey Bataev98eb6e32015-04-22 11:15:40 +0000450 /// iteration of the ordered loop with the dynamic scheduling.
451 ///
452 /// \param CGF Reference to current CodeGenFunction.
453 /// \param Loc Clang source location.
454 /// \param IVSize Size of the iteration variable in bits.
455 /// \param IVSigned Sign of the interation variable.
456 ///
457 virtual void emitForOrderedDynamicIterationEnd(CodeGenFunction &CGF,
458 SourceLocation Loc,
459 unsigned IVSize,
460 bool IVSigned);
461
462 /// \brief Call the appropriate runtime routine to notify that we finished
Alexander Musmanc6388682014-12-15 07:07:06 +0000463 /// all the work with current loop.
464 ///
465 /// \param CGF Reference to current CodeGenFunction.
466 /// \param Loc Clang source location.
Alexander Musmanc6388682014-12-15 07:07:06 +0000467 ///
Alexey Bataev98eb6e32015-04-22 11:15:40 +0000468 virtual void emitForStaticFinish(CodeGenFunction &CGF, SourceLocation Loc);
Alexander Musmanc6388682014-12-15 07:07:06 +0000469
Alexander Musman92bdaab2015-03-12 13:37:50 +0000470 /// Call __kmpc_dispatch_next(
471 /// ident_t *loc, kmp_int32 tid, kmp_int32 *p_lastiter,
472 /// kmp_int[32|64] *p_lower, kmp_int[32|64] *p_upper,
473 /// kmp_int[32|64] *p_stride);
474 /// \param IVSize Size of the iteration variable in bits.
475 /// \param IVSigned Sign of the interation variable.
476 /// \param IL Address of the output variable in which the flag of the
477 /// last iteration is returned.
478 /// \param LB Address of the output variable in which the lower iteration
479 /// number is returned.
480 /// \param UB Address of the output variable in which the upper iteration
481 /// number is returned.
482 /// \param ST Address of the output variable in which the stride value is
483 /// returned.
484 virtual llvm::Value *emitForNext(CodeGenFunction &CGF, SourceLocation Loc,
485 unsigned IVSize, bool IVSigned,
486 llvm::Value *IL, llvm::Value *LB,
487 llvm::Value *UB, llvm::Value *ST);
488
Alexey Bataevb2059782014-10-13 08:23:51 +0000489 /// \brief Emits call to void __kmpc_push_num_threads(ident_t *loc, kmp_int32
490 /// global_tid, kmp_int32 num_threads) to generate code for 'num_threads'
491 /// clause.
492 /// \param NumThreads An integer value of threads.
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000493 virtual void emitNumThreadsClause(CodeGenFunction &CGF,
494 llvm::Value *NumThreads,
495 SourceLocation Loc);
Alexey Bataev97720002014-11-11 04:05:39 +0000496
497 /// \brief Returns address of the threadprivate variable for the current
498 /// thread.
NAKAMURA Takumicdcbfba2014-11-11 07:58:06 +0000499 /// \param VD Threadprivate variable.
Alexey Bataev97720002014-11-11 04:05:39 +0000500 /// \param VDAddr Address of the global variable \a VD.
501 /// \param Loc Location of the reference to threadprivate var.
502 /// \return Address of the threadprivate variable for the current thread.
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000503 virtual llvm::Value *getAddrOfThreadPrivate(CodeGenFunction &CGF,
504 const VarDecl *VD,
505 llvm::Value *VDAddr,
506 SourceLocation Loc);
Alexey Bataev97720002014-11-11 04:05:39 +0000507
508 /// \brief Emit a code for initialization of threadprivate variable. It emits
509 /// a call to runtime library which adds initial value to the newly created
510 /// threadprivate variable (if it is not constant) and registers destructor
511 /// for the variable (if any).
512 /// \param VD Threadprivate variable.
513 /// \param VDAddr Address of the global variable \a VD.
514 /// \param Loc Location of threadprivate declaration.
515 /// \param PerformInit true if initialization expression is not constant.
516 virtual llvm::Function *
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000517 emitThreadPrivateVarDefinition(const VarDecl *VD, llvm::Value *VDAddr,
518 SourceLocation Loc, bool PerformInit,
519 CodeGenFunction *CGF = nullptr);
Alexey Bataevcc37cc12014-11-20 04:34:54 +0000520
521 /// \brief Emit flush of the variables specified in 'omp flush' directive.
522 /// \param Vars List of variables to flush.
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000523 virtual void emitFlush(CodeGenFunction &CGF, ArrayRef<const Expr *> Vars,
524 SourceLocation Loc);
Alexey Bataev62b63b12015-03-10 07:28:44 +0000525
526 /// \brief Emit task region for the task directive. The task region is
Nico Weber20b0ce32015-04-28 18:19:18 +0000527 /// emitted in several steps:
Alexey Bataev62b63b12015-03-10 07:28:44 +0000528 /// 1. Emit a call to kmp_task_t *__kmpc_omp_task_alloc(ident_t *, kmp_int32
529 /// gtid, kmp_int32 flags, size_t sizeof_kmp_task_t, size_t sizeof_shareds,
530 /// kmp_routine_entry_t *task_entry). Here task_entry is a pointer to the
531 /// function:
532 /// kmp_int32 .omp_task_entry.(kmp_int32 gtid, kmp_task_t *tt) {
533 /// TaskFunction(gtid, tt->part_id, tt->shareds);
534 /// return 0;
535 /// }
536 /// 2. Copy a list of shared variables to field shareds of the resulting
537 /// structure kmp_task_t returned by the previous call (if any).
538 /// 3. Copy a pointer to destructions function to field destructions of the
539 /// resulting structure kmp_task_t.
540 /// 4. Emit a call to kmp_int32 __kmpc_omp_task(ident_t *, kmp_int32 gtid,
541 /// kmp_task_t *new_task), where new_task is a resulting structure from
542 /// previous items.
543 /// \param Tied true if the task is tied (the task is tied to the thread that
544 /// can suspend its task region), false - untied (the task is not tied to any
545 /// thread).
546 /// \param Final Contains either constant bool value, or llvm::Value * of i1
547 /// type for final clause. If the value is true, the task forces all of its
548 /// child tasks to become final and included tasks.
549 /// \param TaskFunction An LLVM function with type void (*)(i32 /*gtid*/, i32
550 /// /*part_id*/, captured_struct */*__context*/);
551 /// \param SharedsTy A type which contains references the shared variables.
552 /// \param Shareds Context with the list of shared variables from the \a
553 /// TaskFunction.
Alexey Bataev1d677132015-04-22 13:57:31 +0000554 /// \param IfCond Not a nullptr if 'if' clause was specified, nullptr
555 /// otherwise.
Alexey Bataev62b63b12015-03-10 07:28:44 +0000556 virtual void emitTaskCall(CodeGenFunction &CGF, SourceLocation Loc, bool Tied,
557 llvm::PointerIntPair<llvm::Value *, 1, bool> Final,
558 llvm::Value *TaskFunction, QualType SharedsTy,
Alexey Bataev1d677132015-04-22 13:57:31 +0000559 llvm::Value *Shareds, const Expr *IfCond);
Alexey Bataev794ba0d2015-04-10 10:43:45 +0000560
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000561 /// \brief Emit code for the directive that does not require outlining.
562 ///
563 /// \param CodeGen Code generation sequence for the \a D directive.
564 virtual void emitInlinedDirective(CodeGenFunction &CGF,
565 const RegionCodeGenTy &CodeGen);
Alexey Bataev794ba0d2015-04-10 10:43:45 +0000566 /// \brief Emit a code for reduction clause. Next code should be emitted for
567 /// reduction:
568 /// \code
569 ///
570 /// static kmp_critical_name lock = { 0 };
571 ///
572 /// void reduce_func(void *lhs[<n>], void *rhs[<n>]) {
573 /// ...
574 /// *(Type<i>*)lhs[i] = RedOp<i>(*(Type<i>*)lhs[i], *(Type<i>*)rhs[i]);
575 /// ...
576 /// }
577 ///
578 /// ...
579 /// void *RedList[<n>] = {&<RHSExprs>[0], ..., &<RHSExprs>[<n>-1]};
580 /// switch (__kmpc_reduce{_nowait}(<loc>, <gtid>, <n>, sizeof(RedList),
581 /// RedList, reduce_func, &<lock>)) {
582 /// case 1:
583 /// ...
584 /// <LHSExprs>[i] = RedOp<i>(*<LHSExprs>[i], *<RHSExprs>[i]);
585 /// ...
586 /// __kmpc_end_reduce{_nowait}(<loc>, <gtid>, &<lock>);
587 /// break;
588 /// case 2:
589 /// ...
590 /// Atomic(<LHSExprs>[i] = RedOp<i>(*<LHSExprs>[i], *<RHSExprs>[i]));
591 /// ...
592 /// break;
593 /// default:;
594 /// }
595 /// \endcode
596 ///
597 /// \param LHSExprs List of LHS in \a ReductionOps reduction operations.
598 /// \param RHSExprs List of RHS in \a ReductionOps reduction operations.
599 /// \param ReductionOps List of reduction operations in form 'LHS binop RHS'
600 /// or 'operator binop(LHS, RHS)'.
601 /// \param WithNowait true if parent directive has also nowait clause, false
602 /// otherwise.
603 virtual void emitReduction(CodeGenFunction &CGF, SourceLocation Loc,
604 ArrayRef<const Expr *> LHSExprs,
605 ArrayRef<const Expr *> RHSExprs,
606 ArrayRef<const Expr *> ReductionOps,
607 bool WithNowait);
Alexey Bataev8b8e2022015-04-27 05:22:09 +0000608
609 /// \brief Emit code for 'taskwait' directive.
610 virtual void emitTaskwaitCall(CodeGenFunction &CGF, SourceLocation Loc);
Alexey Bataev9959db52014-05-06 10:08:46 +0000611};
Alexey Bataev8cbe0a62015-02-26 10:27:34 +0000612
Alexey Bataev23b69422014-06-18 07:08:49 +0000613} // namespace CodeGen
614} // namespace clang
Alexey Bataev9959db52014-05-06 10:08:46 +0000615
616#endif