blob: 45cadd2c94fab05ad678aca77c1a82dd9d8ea78a [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 Bataev18095712014-10-10 12:19:54 +000017#include "clang/Basic/SourceLocation.h"
Alexey Bataev9959db52014-05-06 10:08:46 +000018#include "llvm/ADT/DenseMap.h"
Alexey Bataev97720002014-11-11 04:05:39 +000019#include "llvm/ADT/DenseSet.h"
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +000020#include "llvm/ADT/StringMap.h"
Alexey Bataev97720002014-11-11 04:05:39 +000021#include "llvm/IR/ValueHandle.h"
Alexey Bataev18095712014-10-10 12:19:54 +000022
23namespace llvm {
24class ArrayType;
25class Constant;
26class Function;
27class FunctionType;
Alexey Bataev97720002014-11-11 04:05:39 +000028class GlobalVariable;
Alexey Bataev18095712014-10-10 12:19:54 +000029class StructType;
30class Type;
31class Value;
32} // namespace llvm
Alexey Bataev9959db52014-05-06 10:08:46 +000033
Alexey Bataev9959db52014-05-06 10:08:46 +000034namespace clang {
Alexey Bataevcc37cc12014-11-20 04:34:54 +000035class Expr;
Alexey Bataev18095712014-10-10 12:19:54 +000036class OMPExecutableDirective;
37class VarDecl;
38
Alexey Bataev9959db52014-05-06 10:08:46 +000039namespace CodeGen {
40
Alexey Bataev18095712014-10-10 12:19:54 +000041class CodeGenFunction;
42class CodeGenModule;
Alexey Bataev9959db52014-05-06 10:08:46 +000043
44class CGOpenMPRuntime {
45public:
Alexey Bataev8f7c1b02014-12-05 04:09:23 +000046
47private:
48 enum OpenMPRTLFunction {
49 /// \brief Call to void __kmpc_fork_call(ident_t *loc, kmp_int32 argc,
50 /// kmpc_micro microtask, ...);
51 OMPRTL__kmpc_fork_call,
52 /// \brief Call to void *__kmpc_threadprivate_cached(ident_t *loc,
53 /// kmp_int32 global_tid, void *data, size_t size, void ***cache);
54 OMPRTL__kmpc_threadprivate_cached,
55 /// \brief Call to void __kmpc_threadprivate_register( ident_t *,
56 /// void *data, kmpc_ctor ctor, kmpc_cctor cctor, kmpc_dtor dtor);
57 OMPRTL__kmpc_threadprivate_register,
58 // Call to __kmpc_int32 kmpc_global_thread_num(ident_t *loc);
59 OMPRTL__kmpc_global_thread_num,
60 // Call to void __kmpc_critical(ident_t *loc, kmp_int32 global_tid,
61 // kmp_critical_name *crit);
62 OMPRTL__kmpc_critical,
63 // Call to void __kmpc_end_critical(ident_t *loc, kmp_int32 global_tid,
64 // kmp_critical_name *crit);
65 OMPRTL__kmpc_end_critical,
66 // Call to kmp_int32 __kmpc_cancel_barrier(ident_t *loc, kmp_int32
67 // global_tid);
68 OMPRTL__kmpc_cancel_barrier,
69 // Call to void __kmpc_serialized_parallel(ident_t *loc, kmp_int32
70 // global_tid);
71 OMPRTL__kmpc_serialized_parallel,
72 // Call to void __kmpc_end_serialized_parallel(ident_t *loc, kmp_int32
73 // global_tid);
74 OMPRTL__kmpc_end_serialized_parallel,
75 // Call to void __kmpc_push_num_threads(ident_t *loc, kmp_int32 global_tid,
76 // kmp_int32 num_threads);
77 OMPRTL__kmpc_push_num_threads,
78 // Call to void __kmpc_flush(ident_t *loc, ...);
79 OMPRTL__kmpc_flush,
80 // Call to kmp_int32 __kmpc_master(ident_t *, kmp_int32 global_tid);
81 OMPRTL__kmpc_master,
82 // Call to void __kmpc_end_master(ident_t *, kmp_int32 global_tid);
83 OMPRTL__kmpc_end_master,
84 };
85
Alexey Bataev9959db52014-05-06 10:08:46 +000086 /// \brief Values for bit flags used in the ident_t to describe the fields.
87 /// All enumeric elements are named and described in accordance with the code
88 /// from http://llvm.org/svn/llvm-project/openmp/trunk/runtime/src/kmp.h
89 enum OpenMPLocationFlags {
90 /// \brief Use trampoline for internal microtask.
91 OMP_IDENT_IMD = 0x01,
92 /// \brief Use c-style ident structure.
93 OMP_IDENT_KMPC = 0x02,
94 /// \brief Atomic reduction option for kmpc_reduce.
95 OMP_ATOMIC_REDUCE = 0x10,
96 /// \brief Explicit 'barrier' directive.
97 OMP_IDENT_BARRIER_EXPL = 0x20,
98 /// \brief Implicit barrier in code.
99 OMP_IDENT_BARRIER_IMPL = 0x40,
100 /// \brief Implicit barrier in 'for' directive.
101 OMP_IDENT_BARRIER_IMPL_FOR = 0x40,
102 /// \brief Implicit barrier in 'sections' directive.
103 OMP_IDENT_BARRIER_IMPL_SECTIONS = 0xC0,
104 /// \brief Implicit barrier in 'single' directive.
105 OMP_IDENT_BARRIER_IMPL_SINGLE = 0x140
106 };
Alexey Bataev9959db52014-05-06 10:08:46 +0000107 CodeGenModule &CGM;
108 /// \brief Default const ident_t object used for initialization of all other
109 /// ident_t objects.
110 llvm::Constant *DefaultOpenMPPSource;
Alexey Bataev18095712014-10-10 12:19:54 +0000111 /// \brief Map of flags and corresponding default locations.
Alexey Bataev15007ba2014-05-07 06:18:01 +0000112 typedef llvm::DenseMap<unsigned, llvm::Value *> OpenMPDefaultLocMapTy;
113 OpenMPDefaultLocMapTy OpenMPDefaultLocMap;
Alexey Bataev9959db52014-05-06 10:08:46 +0000114 llvm::Value *GetOrCreateDefaultOpenMPLocation(OpenMPLocationFlags Flags);
115 /// \brief Describes ident structure that describes a source location.
116 /// All descriptions are taken from
117 /// http://llvm.org/svn/llvm-project/openmp/trunk/runtime/src/kmp.h
118 /// Original structure:
119 /// typedef struct ident {
120 /// kmp_int32 reserved_1; /**< might be used in Fortran;
121 /// see above */
122 /// kmp_int32 flags; /**< also f.flags; KMP_IDENT_xxx flags;
123 /// KMP_IDENT_KMPC identifies this union
124 /// member */
125 /// kmp_int32 reserved_2; /**< not really used in Fortran any more;
126 /// see above */
127 ///#if USE_ITT_BUILD
128 /// /* but currently used for storing
129 /// region-specific ITT */
130 /// /* contextual information. */
131 ///#endif /* USE_ITT_BUILD */
132 /// kmp_int32 reserved_3; /**< source[4] in Fortran, do not use for
133 /// C++ */
134 /// char const *psource; /**< String describing the source location.
135 /// The string is composed of semi-colon separated
136 // fields which describe the source file,
137 /// the function and a pair of line numbers that
138 /// delimit the construct.
139 /// */
140 /// } ident_t;
141 enum IdentFieldIndex {
142 /// \brief might be used in Fortran
143 IdentField_Reserved_1,
144 /// \brief OMP_IDENT_xxx flags; OMP_IDENT_KMPC identifies this union member.
145 IdentField_Flags,
146 /// \brief Not really used in Fortran any more
147 IdentField_Reserved_2,
148 /// \brief Source[4] in Fortran, do not use for C++
149 IdentField_Reserved_3,
150 /// \brief String describing the source location. The string is composed of
151 /// semi-colon separated fields which describe the source file, the function
152 /// and a pair of line numbers that delimit the construct.
153 IdentField_PSource
154 };
155 llvm::StructType *IdentTy;
Alexey Bataev18095712014-10-10 12:19:54 +0000156 /// \brief Map for SourceLocation and OpenMP runtime library debug locations.
Alexey Bataevf002aca2014-05-30 05:48:40 +0000157 typedef llvm::DenseMap<unsigned, llvm::Value *> OpenMPDebugLocMapTy;
158 OpenMPDebugLocMapTy OpenMPDebugLocMap;
Alexey Bataev9959db52014-05-06 10:08:46 +0000159 /// \brief The type for a microtask which gets passed to __kmpc_fork_call().
160 /// Original representation is:
161 /// typedef void (kmpc_micro)(kmp_int32 global_tid, kmp_int32 bound_tid,...);
162 llvm::FunctionType *Kmpc_MicroTy;
Alexey Bataev18095712014-10-10 12:19:54 +0000163 /// \brief Stores debug location and ThreadID for the function.
164 struct DebugLocThreadIdTy {
165 llvm::Value *DebugLoc;
166 llvm::Value *ThreadID;
167 };
168 /// \brief Map of local debug location, ThreadId and functions.
169 typedef llvm::DenseMap<llvm::Function *, DebugLocThreadIdTy>
170 OpenMPLocThreadIDMapTy;
171 OpenMPLocThreadIDMapTy OpenMPLocThreadIDMap;
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +0000172 /// \brief Type kmp_critical_name, originally defined as typedef kmp_int32
173 /// kmp_critical_name[8];
174 llvm::ArrayType *KmpCriticalNameTy;
Alexey Bataev97720002014-11-11 04:05:39 +0000175 /// \brief An ordered map of auto-generated variables to their unique names.
176 /// It stores variables with the following names: 1) ".gomp_critical_user_" +
177 /// <critical_section_name> + ".var" for "omp critical" directives; 2)
178 /// <mangled_name_for_global_var> + ".cache." for cache for threadprivate
179 /// variables.
180 llvm::StringMap<llvm::AssertingVH<llvm::Constant>, llvm::BumpPtrAllocator>
181 InternalVars;
Alexey Bataev9959db52014-05-06 10:08:46 +0000182
Alexey Bataev9959db52014-05-06 10:08:46 +0000183 /// \brief Emits object of ident_t type with info for source location.
Alexey Bataev9959db52014-05-06 10:08:46 +0000184 /// \param Flags Flags for OpenMP location.
185 ///
186 llvm::Value *
187 EmitOpenMPUpdateLocation(CodeGenFunction &CGF, SourceLocation Loc,
188 OpenMPLocationFlags Flags = OMP_IDENT_KMPC);
189
Alexey Bataevd74d0602014-10-13 06:02:40 +0000190 /// \brief Returns pointer to ident_t type.
Alexey Bataev9959db52014-05-06 10:08:46 +0000191 llvm::Type *getIdentTyPointerTy();
192
Alexey Bataevd74d0602014-10-13 06:02:40 +0000193 /// \brief Returns pointer to kmpc_micro type.
Alexey Bataev9959db52014-05-06 10:08:46 +0000194 llvm::Type *getKmpc_MicroPointerTy();
195
196 /// \brief Returns specified OpenMP runtime function.
197 /// \param Function OpenMP runtime function.
198 /// \return Specified function.
199 llvm::Constant *CreateRuntimeFunction(OpenMPRTLFunction Function);
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +0000200
Alexey Bataev97720002014-11-11 04:05:39 +0000201 /// \brief If the specified mangled name is not in the module, create and
202 /// return threadprivate cache object. This object is a pointer's worth of
203 /// storage that's reserved for use by the OpenMP runtime.
NAKAMURA Takumicdcbfba2014-11-11 07:58:06 +0000204 /// \param VD Threadprivate variable.
Alexey Bataev97720002014-11-11 04:05:39 +0000205 /// \return Cache variable for the specified threadprivate.
206 llvm::Constant *getOrCreateThreadPrivateCache(const VarDecl *VD);
207
Alexey Bataevd74d0602014-10-13 06:02:40 +0000208 /// \brief Emits address of the word in a memory where current thread id is
209 /// stored.
210 virtual llvm::Value *EmitThreadIDAddress(CodeGenFunction &CGF,
211 SourceLocation Loc);
212
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000213 /// \brief Gets thread id value for the current thread.
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000214 ///
215 llvm::Value *GetOpenMPThreadID(CodeGenFunction &CGF, SourceLocation Loc);
216
Alexey Bataev97720002014-11-11 04:05:39 +0000217 /// \brief Gets (if variable with the given name already exist) or creates
218 /// internal global variable with the specified Name. The created variable has
219 /// linkage CommonLinkage by default and is initialized by null value.
220 /// \param Ty Type of the global variable. If it is exist already the type
221 /// must be the same.
222 /// \param Name Name of the variable.
223 llvm::Constant *GetOrCreateInternalVariable(llvm::Type *Ty,
224 const llvm::Twine &Name);
225
226 /// \brief Set of threadprivate variables with the generated initializer.
227 llvm::DenseSet<const VarDecl *> ThreadPrivateWithDefinition;
228
229 /// \brief Emits initialization code for the threadprivate variables.
230 /// \param VDAddr Address of the global variable \a VD.
231 /// \param Ctor Pointer to a global init function for \a VD.
232 /// \param CopyCtor Pointer to a global copy function for \a VD.
233 /// \param Dtor Pointer to a global destructor function for \a VD.
234 /// \param Loc Location of threadprivate declaration.
235 void EmitOMPThreadPrivateVarInit(CodeGenFunction &CGF, llvm::Value *VDAddr,
236 llvm::Value *Ctor, llvm::Value *CopyCtor,
237 llvm::Value *Dtor, SourceLocation Loc);
238
Alexey Bataev75ddfab2014-12-01 11:32:38 +0000239 /// \brief Returns corresponding lock object for the specified critical region
240 /// name. If the lock object does not exist it is created, otherwise the
241 /// reference to the existing copy is returned.
242 /// \param CriticalName Name of the critical region.
243 ///
244 llvm::Value *GetCriticalRegionLock(StringRef CriticalName);
245
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000246public:
247 explicit CGOpenMPRuntime(CodeGenModule &CGM);
248 virtual ~CGOpenMPRuntime() {}
249
Alexey Bataev18095712014-10-10 12:19:54 +0000250 /// \brief Emits outlined function for the specified OpenMP directive \a D
251 /// (required for parallel and task directives). This outlined function has
252 /// type void(*)(kmp_int32 /*ThreadID*/, kmp_int32 /*BoundID*/, struct
253 /// context_vars*).
254 /// \param D OpenMP directive.
255 /// \param ThreadIDVar Variable for thread id in the current OpenMP region.
256 ///
257 virtual llvm::Value *
258 EmitOpenMPOutlinedFunction(const OMPExecutableDirective &D,
259 const VarDecl *ThreadIDVar);
260
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000261 /// \brief Cleans up references to the objects in finished function.
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000262 ///
263 void FunctionFinished(CodeGenFunction &CGF);
264
265 /// \brief Emits code for parallel call of the \a OutlinedFn with variables
266 /// captured in a record which address is stored in \a CapturedStruct.
Alexey Bataev18095712014-10-10 12:19:54 +0000267 /// \param OutlinedFn Outlined function to be run in parallel threads. Type of
268 /// this function is void(*)(kmp_int32, kmp_int32, struct context_vars*).
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000269 /// \param CapturedStruct A pointer to the record with the references to
270 /// variables used in \a OutlinedFn function.
271 ///
272 virtual void EmitOMPParallelCall(CodeGenFunction &CGF, SourceLocation Loc,
273 llvm::Value *OutlinedFn,
274 llvm::Value *CapturedStruct);
275
Alexey Bataevd74d0602014-10-13 06:02:40 +0000276 /// \brief Emits code for serial call of the \a OutlinedFn with variables
277 /// captured in a record which address is stored in \a CapturedStruct.
278 /// \param OutlinedFn Outlined function to be run in serial mode.
279 /// \param CapturedStruct A pointer to the record with the references to
280 /// variables used in \a OutlinedFn function.
281 ///
282 virtual void EmitOMPSerialCall(CodeGenFunction &CGF, SourceLocation Loc,
283 llvm::Value *OutlinedFn,
284 llvm::Value *CapturedStruct);
285
Alexey Bataev75ddfab2014-12-01 11:32:38 +0000286 /// \brief Emits a critical region.
Alexey Bataev18095712014-10-10 12:19:54 +0000287 /// \param CriticalName Name of the critical region.
Alexey Bataev75ddfab2014-12-01 11:32:38 +0000288 /// \param CriticalOpGen Generator for the statement associated with the given
289 /// critical region.
290 virtual void EmitOMPCriticalRegion(CodeGenFunction &CGF,
291 StringRef CriticalName,
292 const std::function<void()> &CriticalOpGen,
293 SourceLocation Loc);
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000294
Alexey Bataev8d690652014-12-04 07:23:53 +0000295 /// \brief Emits a master region.
296 /// \param MasterOpGen Generator for the statement associated with the given
297 /// master region.
298 virtual void EmitOMPMasterRegion(CodeGenFunction &CGF,
299 const std::function<void()> &MasterOpGen,
300 SourceLocation Loc);
301
Alexey Bataev8f7c1b02014-12-05 04:09:23 +0000302 /// \brief Emits explicit barrier for OpenMP threads.
303 /// \param IsExplicit true, if it is explicitly specified barrier.
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000304 ///
305 virtual void EmitOMPBarrierCall(CodeGenFunction &CGF, SourceLocation Loc,
Alexey Bataev8f7c1b02014-12-05 04:09:23 +0000306 bool IsExplicit = true);
Alexey Bataevb2059782014-10-13 08:23:51 +0000307
308 /// \brief Emits call to void __kmpc_push_num_threads(ident_t *loc, kmp_int32
309 /// global_tid, kmp_int32 num_threads) to generate code for 'num_threads'
310 /// clause.
311 /// \param NumThreads An integer value of threads.
312 virtual void EmitOMPNumThreadsClause(CodeGenFunction &CGF,
313 llvm::Value *NumThreads,
314 SourceLocation Loc);
Alexey Bataev97720002014-11-11 04:05:39 +0000315
316 /// \brief Returns address of the threadprivate variable for the current
317 /// thread.
NAKAMURA Takumicdcbfba2014-11-11 07:58:06 +0000318 /// \param VD Threadprivate variable.
Alexey Bataev97720002014-11-11 04:05:39 +0000319 /// \param VDAddr Address of the global variable \a VD.
320 /// \param Loc Location of the reference to threadprivate var.
321 /// \return Address of the threadprivate variable for the current thread.
322 virtual llvm::Value *getOMPAddrOfThreadPrivate(CodeGenFunction &CGF,
323 const VarDecl *VD,
324 llvm::Value *VDAddr,
325 SourceLocation Loc);
326
327 /// \brief Emit a code for initialization of threadprivate variable. It emits
328 /// a call to runtime library which adds initial value to the newly created
329 /// threadprivate variable (if it is not constant) and registers destructor
330 /// for the variable (if any).
331 /// \param VD Threadprivate variable.
332 /// \param VDAddr Address of the global variable \a VD.
333 /// \param Loc Location of threadprivate declaration.
334 /// \param PerformInit true if initialization expression is not constant.
335 virtual llvm::Function *
336 EmitOMPThreadPrivateVarDefinition(const VarDecl *VD, llvm::Value *VDAddr,
337 SourceLocation Loc, bool PerformInit,
338 CodeGenFunction *CGF = nullptr);
Alexey Bataevcc37cc12014-11-20 04:34:54 +0000339
340 /// \brief Emit flush of the variables specified in 'omp flush' directive.
341 /// \param Vars List of variables to flush.
342 virtual void EmitOMPFlush(CodeGenFunction &CGF, ArrayRef<const Expr *> Vars,
343 SourceLocation Loc);
Alexey Bataev9959db52014-05-06 10:08:46 +0000344};
Alexey Bataev23b69422014-06-18 07:08:49 +0000345} // namespace CodeGen
346} // namespace clang
Alexey Bataev9959db52014-05-06 10:08:46 +0000347
348#endif