blob: b9c1a35434fca21c752049d39e58af999076f3da [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:
46 /// \brief Values for bit flags used in the ident_t to describe the fields.
47 /// All enumeric elements are named and described in accordance with the code
48 /// from http://llvm.org/svn/llvm-project/openmp/trunk/runtime/src/kmp.h
49 enum OpenMPLocationFlags {
50 /// \brief Use trampoline for internal microtask.
51 OMP_IDENT_IMD = 0x01,
52 /// \brief Use c-style ident structure.
53 OMP_IDENT_KMPC = 0x02,
54 /// \brief Atomic reduction option for kmpc_reduce.
55 OMP_ATOMIC_REDUCE = 0x10,
56 /// \brief Explicit 'barrier' directive.
57 OMP_IDENT_BARRIER_EXPL = 0x20,
58 /// \brief Implicit barrier in code.
59 OMP_IDENT_BARRIER_IMPL = 0x40,
60 /// \brief Implicit barrier in 'for' directive.
61 OMP_IDENT_BARRIER_IMPL_FOR = 0x40,
62 /// \brief Implicit barrier in 'sections' directive.
63 OMP_IDENT_BARRIER_IMPL_SECTIONS = 0xC0,
64 /// \brief Implicit barrier in 'single' directive.
65 OMP_IDENT_BARRIER_IMPL_SINGLE = 0x140
66 };
67 enum OpenMPRTLFunction {
Alexey Bataev97720002014-11-11 04:05:39 +000068 /// \brief Call to void __kmpc_fork_call(ident_t *loc, kmp_int32 argc,
69 /// kmpc_micro microtask, ...);
Alexey Bataev9959db52014-05-06 10:08:46 +000070 OMPRTL__kmpc_fork_call,
Alexey Bataev97720002014-11-11 04:05:39 +000071 /// \brief Call to void *__kmpc_threadprivate_cached(ident_t *loc,
72 /// kmp_int32 global_tid, void *data, size_t size, void ***cache);
73 OMPRTL__kmpc_threadprivate_cached,
74 /// \brief Call to void __kmpc_threadprivate_register( ident_t *,
75 /// void *data, kmpc_ctor ctor, kmpc_cctor cctor, kmpc_dtor dtor);
76 OMPRTL__kmpc_threadprivate_register,
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +000077 // Call to __kmpc_int32 kmpc_global_thread_num(ident_t *loc);
78 OMPRTL__kmpc_global_thread_num,
Alexey Bataevf9472182014-09-22 12:32:31 +000079 // Call to void __kmpc_critical(ident_t *loc, kmp_int32 global_tid,
80 // kmp_critical_name *crit);
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +000081 OMPRTL__kmpc_critical,
Alexey Bataevf9472182014-09-22 12:32:31 +000082 // Call to void __kmpc_end_critical(ident_t *loc, kmp_int32 global_tid,
83 // kmp_critical_name *crit);
Alexey Bataev4a5bb772014-10-08 14:01:46 +000084 OMPRTL__kmpc_end_critical,
85 // Call to void __kmpc_barrier(ident_t *loc, kmp_int32 global_tid);
Alexey Bataevd74d0602014-10-13 06:02:40 +000086 OMPRTL__kmpc_barrier,
87 // Call to void __kmpc_serialized_parallel(ident_t *loc, kmp_int32
88 // global_tid);
89 OMPRTL__kmpc_serialized_parallel,
90 // Call to void __kmpc_end_serialized_parallel(ident_t *loc, kmp_int32
91 // global_tid);
Alexey Bataevb2059782014-10-13 08:23:51 +000092 OMPRTL__kmpc_end_serialized_parallel,
93 // Call to void __kmpc_push_num_threads(ident_t *loc, kmp_int32 global_tid,
94 // kmp_int32 num_threads);
Alexey Bataevcc37cc12014-11-20 04:34:54 +000095 OMPRTL__kmpc_push_num_threads,
96 // Call to void __kmpc_flush(ident_t *loc, ...);
97 OMPRTL__kmpc_flush
Alexey Bataev9959db52014-05-06 10:08:46 +000098 };
99
100private:
101 CodeGenModule &CGM;
102 /// \brief Default const ident_t object used for initialization of all other
103 /// ident_t objects.
104 llvm::Constant *DefaultOpenMPPSource;
Alexey Bataev18095712014-10-10 12:19:54 +0000105 /// \brief Map of flags and corresponding default locations.
Alexey Bataev15007ba2014-05-07 06:18:01 +0000106 typedef llvm::DenseMap<unsigned, llvm::Value *> OpenMPDefaultLocMapTy;
107 OpenMPDefaultLocMapTy OpenMPDefaultLocMap;
Alexey Bataev9959db52014-05-06 10:08:46 +0000108 llvm::Value *GetOrCreateDefaultOpenMPLocation(OpenMPLocationFlags Flags);
109 /// \brief Describes ident structure that describes a source location.
110 /// All descriptions are taken from
111 /// http://llvm.org/svn/llvm-project/openmp/trunk/runtime/src/kmp.h
112 /// Original structure:
113 /// typedef struct ident {
114 /// kmp_int32 reserved_1; /**< might be used in Fortran;
115 /// see above */
116 /// kmp_int32 flags; /**< also f.flags; KMP_IDENT_xxx flags;
117 /// KMP_IDENT_KMPC identifies this union
118 /// member */
119 /// kmp_int32 reserved_2; /**< not really used in Fortran any more;
120 /// see above */
121 ///#if USE_ITT_BUILD
122 /// /* but currently used for storing
123 /// region-specific ITT */
124 /// /* contextual information. */
125 ///#endif /* USE_ITT_BUILD */
126 /// kmp_int32 reserved_3; /**< source[4] in Fortran, do not use for
127 /// C++ */
128 /// char const *psource; /**< String describing the source location.
129 /// The string is composed of semi-colon separated
130 // fields which describe the source file,
131 /// the function and a pair of line numbers that
132 /// delimit the construct.
133 /// */
134 /// } ident_t;
135 enum IdentFieldIndex {
136 /// \brief might be used in Fortran
137 IdentField_Reserved_1,
138 /// \brief OMP_IDENT_xxx flags; OMP_IDENT_KMPC identifies this union member.
139 IdentField_Flags,
140 /// \brief Not really used in Fortran any more
141 IdentField_Reserved_2,
142 /// \brief Source[4] in Fortran, do not use for C++
143 IdentField_Reserved_3,
144 /// \brief String describing the source location. The string is composed of
145 /// semi-colon separated fields which describe the source file, the function
146 /// and a pair of line numbers that delimit the construct.
147 IdentField_PSource
148 };
149 llvm::StructType *IdentTy;
Alexey Bataev18095712014-10-10 12:19:54 +0000150 /// \brief Map for SourceLocation and OpenMP runtime library debug locations.
Alexey Bataevf002aca2014-05-30 05:48:40 +0000151 typedef llvm::DenseMap<unsigned, llvm::Value *> OpenMPDebugLocMapTy;
152 OpenMPDebugLocMapTy OpenMPDebugLocMap;
Alexey Bataev9959db52014-05-06 10:08:46 +0000153 /// \brief The type for a microtask which gets passed to __kmpc_fork_call().
154 /// Original representation is:
155 /// typedef void (kmpc_micro)(kmp_int32 global_tid, kmp_int32 bound_tid,...);
156 llvm::FunctionType *Kmpc_MicroTy;
Alexey Bataev18095712014-10-10 12:19:54 +0000157 /// \brief Stores debug location and ThreadID for the function.
158 struct DebugLocThreadIdTy {
159 llvm::Value *DebugLoc;
160 llvm::Value *ThreadID;
161 };
162 /// \brief Map of local debug location, ThreadId and functions.
163 typedef llvm::DenseMap<llvm::Function *, DebugLocThreadIdTy>
164 OpenMPLocThreadIDMapTy;
165 OpenMPLocThreadIDMapTy OpenMPLocThreadIDMap;
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +0000166 /// \brief Type kmp_critical_name, originally defined as typedef kmp_int32
167 /// kmp_critical_name[8];
168 llvm::ArrayType *KmpCriticalNameTy;
Alexey Bataev97720002014-11-11 04:05:39 +0000169 /// \brief An ordered map of auto-generated variables to their unique names.
170 /// It stores variables with the following names: 1) ".gomp_critical_user_" +
171 /// <critical_section_name> + ".var" for "omp critical" directives; 2)
172 /// <mangled_name_for_global_var> + ".cache." for cache for threadprivate
173 /// variables.
174 llvm::StringMap<llvm::AssertingVH<llvm::Constant>, llvm::BumpPtrAllocator>
175 InternalVars;
Alexey Bataev9959db52014-05-06 10:08:46 +0000176
Alexey Bataev9959db52014-05-06 10:08:46 +0000177 /// \brief Emits object of ident_t type with info for source location.
Alexey Bataev9959db52014-05-06 10:08:46 +0000178 /// \param Flags Flags for OpenMP location.
179 ///
180 llvm::Value *
181 EmitOpenMPUpdateLocation(CodeGenFunction &CGF, SourceLocation Loc,
182 OpenMPLocationFlags Flags = OMP_IDENT_KMPC);
183
Alexey Bataevd74d0602014-10-13 06:02:40 +0000184 /// \brief Returns pointer to ident_t type.
Alexey Bataev9959db52014-05-06 10:08:46 +0000185 llvm::Type *getIdentTyPointerTy();
186
Alexey Bataevd74d0602014-10-13 06:02:40 +0000187 /// \brief Returns pointer to kmpc_micro type.
Alexey Bataev9959db52014-05-06 10:08:46 +0000188 llvm::Type *getKmpc_MicroPointerTy();
189
190 /// \brief Returns specified OpenMP runtime function.
191 /// \param Function OpenMP runtime function.
192 /// \return Specified function.
193 llvm::Constant *CreateRuntimeFunction(OpenMPRTLFunction Function);
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +0000194
Alexey Bataev97720002014-11-11 04:05:39 +0000195 /// \brief If the specified mangled name is not in the module, create and
196 /// return threadprivate cache object. This object is a pointer's worth of
197 /// storage that's reserved for use by the OpenMP runtime.
NAKAMURA Takumicdcbfba2014-11-11 07:58:06 +0000198 /// \param VD Threadprivate variable.
Alexey Bataev97720002014-11-11 04:05:39 +0000199 /// \return Cache variable for the specified threadprivate.
200 llvm::Constant *getOrCreateThreadPrivateCache(const VarDecl *VD);
201
Alexey Bataevd74d0602014-10-13 06:02:40 +0000202 /// \brief Emits address of the word in a memory where current thread id is
203 /// stored.
204 virtual llvm::Value *EmitThreadIDAddress(CodeGenFunction &CGF,
205 SourceLocation Loc);
206
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000207 /// \brief Gets thread id value for the current thread.
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000208 ///
209 llvm::Value *GetOpenMPThreadID(CodeGenFunction &CGF, SourceLocation Loc);
210
Alexey Bataev97720002014-11-11 04:05:39 +0000211 /// \brief Gets (if variable with the given name already exist) or creates
212 /// internal global variable with the specified Name. The created variable has
213 /// linkage CommonLinkage by default and is initialized by null value.
214 /// \param Ty Type of the global variable. If it is exist already the type
215 /// must be the same.
216 /// \param Name Name of the variable.
217 llvm::Constant *GetOrCreateInternalVariable(llvm::Type *Ty,
218 const llvm::Twine &Name);
219
220 /// \brief Set of threadprivate variables with the generated initializer.
221 llvm::DenseSet<const VarDecl *> ThreadPrivateWithDefinition;
222
223 /// \brief Emits initialization code for the threadprivate variables.
224 /// \param VDAddr Address of the global variable \a VD.
225 /// \param Ctor Pointer to a global init function for \a VD.
226 /// \param CopyCtor Pointer to a global copy function for \a VD.
227 /// \param Dtor Pointer to a global destructor function for \a VD.
228 /// \param Loc Location of threadprivate declaration.
229 void EmitOMPThreadPrivateVarInit(CodeGenFunction &CGF, llvm::Value *VDAddr,
230 llvm::Value *Ctor, llvm::Value *CopyCtor,
231 llvm::Value *Dtor, SourceLocation Loc);
232
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000233public:
234 explicit CGOpenMPRuntime(CodeGenModule &CGM);
235 virtual ~CGOpenMPRuntime() {}
236
Alexey Bataev18095712014-10-10 12:19:54 +0000237 /// \brief Emits outlined function for the specified OpenMP directive \a D
238 /// (required for parallel and task directives). This outlined function has
239 /// type void(*)(kmp_int32 /*ThreadID*/, kmp_int32 /*BoundID*/, struct
240 /// context_vars*).
241 /// \param D OpenMP directive.
242 /// \param ThreadIDVar Variable for thread id in the current OpenMP region.
243 ///
244 virtual llvm::Value *
245 EmitOpenMPOutlinedFunction(const OMPExecutableDirective &D,
246 const VarDecl *ThreadIDVar);
247
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000248 /// \brief Cleans up references to the objects in finished function.
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000249 ///
250 void FunctionFinished(CodeGenFunction &CGF);
251
252 /// \brief Emits code for parallel call of the \a OutlinedFn with variables
253 /// captured in a record which address is stored in \a CapturedStruct.
Alexey Bataev18095712014-10-10 12:19:54 +0000254 /// \param OutlinedFn Outlined function to be run in parallel threads. Type of
255 /// this function is void(*)(kmp_int32, kmp_int32, struct context_vars*).
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000256 /// \param CapturedStruct A pointer to the record with the references to
257 /// variables used in \a OutlinedFn function.
258 ///
259 virtual void EmitOMPParallelCall(CodeGenFunction &CGF, SourceLocation Loc,
260 llvm::Value *OutlinedFn,
261 llvm::Value *CapturedStruct);
262
Alexey Bataevd74d0602014-10-13 06:02:40 +0000263 /// \brief Emits code for serial call of the \a OutlinedFn with variables
264 /// captured in a record which address is stored in \a CapturedStruct.
265 /// \param OutlinedFn Outlined function to be run in serial mode.
266 /// \param CapturedStruct A pointer to the record with the references to
267 /// variables used in \a OutlinedFn function.
268 ///
269 virtual void EmitOMPSerialCall(CodeGenFunction &CGF, SourceLocation Loc,
270 llvm::Value *OutlinedFn,
271 llvm::Value *CapturedStruct);
272
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +0000273 /// \brief Returns corresponding lock object for the specified critical region
274 /// name. If the lock object does not exist it is created, otherwise the
275 /// reference to the existing copy is returned.
Alexey Bataev18095712014-10-10 12:19:54 +0000276 /// \param CriticalName Name of the critical region.
277 ///
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +0000278 llvm::Value *GetCriticalRegionLock(StringRef CriticalName);
279
280 /// \brief Emits start of the critical region by calling void
Alexey Bataevf9472182014-09-22 12:32:31 +0000281 /// __kmpc_critical(ident_t *loc, kmp_int32 global_tid, kmp_critical_name
282 /// * \a RegionLock)
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +0000283 /// \param RegionLock The lock object for critical region.
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +0000284 virtual void EmitOMPCriticalRegionStart(CodeGenFunction &CGF,
285 llvm::Value *RegionLock,
286 SourceLocation Loc);
287
288 /// \brief Emits end of the critical region by calling void
Alexey Bataevf9472182014-09-22 12:32:31 +0000289 /// __kmpc_end_critical(ident_t *loc, kmp_int32 global_tid, kmp_critical_name
290 /// * \a RegionLock)
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +0000291 /// \param RegionLock The lock object for critical region.
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +0000292 virtual void EmitOMPCriticalRegionEnd(CodeGenFunction &CGF,
293 llvm::Value *RegionLock,
294 SourceLocation Loc);
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000295
296 /// \brief Emits a barrier for OpenMP threads.
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000297 /// \param Flags Flags for the barrier.
298 ///
299 virtual void EmitOMPBarrierCall(CodeGenFunction &CGF, SourceLocation Loc,
300 OpenMPLocationFlags Flags);
Alexey Bataevb2059782014-10-13 08:23:51 +0000301
302 /// \brief Emits call to void __kmpc_push_num_threads(ident_t *loc, kmp_int32
303 /// global_tid, kmp_int32 num_threads) to generate code for 'num_threads'
304 /// clause.
305 /// \param NumThreads An integer value of threads.
306 virtual void EmitOMPNumThreadsClause(CodeGenFunction &CGF,
307 llvm::Value *NumThreads,
308 SourceLocation Loc);
Alexey Bataev97720002014-11-11 04:05:39 +0000309
310 /// \brief Returns address of the threadprivate variable for the current
311 /// thread.
NAKAMURA Takumicdcbfba2014-11-11 07:58:06 +0000312 /// \param VD Threadprivate variable.
Alexey Bataev97720002014-11-11 04:05:39 +0000313 /// \param VDAddr Address of the global variable \a VD.
314 /// \param Loc Location of the reference to threadprivate var.
315 /// \return Address of the threadprivate variable for the current thread.
316 virtual llvm::Value *getOMPAddrOfThreadPrivate(CodeGenFunction &CGF,
317 const VarDecl *VD,
318 llvm::Value *VDAddr,
319 SourceLocation Loc);
320
321 /// \brief Emit a code for initialization of threadprivate variable. It emits
322 /// a call to runtime library which adds initial value to the newly created
323 /// threadprivate variable (if it is not constant) and registers destructor
324 /// for the variable (if any).
325 /// \param VD Threadprivate variable.
326 /// \param VDAddr Address of the global variable \a VD.
327 /// \param Loc Location of threadprivate declaration.
328 /// \param PerformInit true if initialization expression is not constant.
329 virtual llvm::Function *
330 EmitOMPThreadPrivateVarDefinition(const VarDecl *VD, llvm::Value *VDAddr,
331 SourceLocation Loc, bool PerformInit,
332 CodeGenFunction *CGF = nullptr);
Alexey Bataevcc37cc12014-11-20 04:34:54 +0000333
334 /// \brief Emit flush of the variables specified in 'omp flush' directive.
335 /// \param Vars List of variables to flush.
336 virtual void EmitOMPFlush(CodeGenFunction &CGF, ArrayRef<const Expr *> Vars,
337 SourceLocation Loc);
Alexey Bataev9959db52014-05-06 10:08:46 +0000338};
Alexey Bataev23b69422014-06-18 07:08:49 +0000339} // namespace CodeGen
340} // namespace clang
Alexey Bataev9959db52014-05-06 10:08:46 +0000341
342#endif