blob: 033988f4886803582bc20255c2042ddd1fc43fd3 [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 Bataev97720002014-11-11 04:05:39 +000035class VarDecl;
Alexey Bataev9959db52014-05-06 10:08:46 +000036
Alexey Bataev18095712014-10-10 12:19:54 +000037class OMPExecutableDirective;
38class VarDecl;
39
Alexey Bataev9959db52014-05-06 10:08:46 +000040namespace CodeGen {
41
Alexey Bataev18095712014-10-10 12:19:54 +000042class CodeGenFunction;
43class CodeGenModule;
Alexey Bataev9959db52014-05-06 10:08:46 +000044
45class CGOpenMPRuntime {
46public:
47 /// \brief Values for bit flags used in the ident_t to describe the fields.
48 /// All enumeric elements are named and described in accordance with the code
49 /// from http://llvm.org/svn/llvm-project/openmp/trunk/runtime/src/kmp.h
50 enum OpenMPLocationFlags {
51 /// \brief Use trampoline for internal microtask.
52 OMP_IDENT_IMD = 0x01,
53 /// \brief Use c-style ident structure.
54 OMP_IDENT_KMPC = 0x02,
55 /// \brief Atomic reduction option for kmpc_reduce.
56 OMP_ATOMIC_REDUCE = 0x10,
57 /// \brief Explicit 'barrier' directive.
58 OMP_IDENT_BARRIER_EXPL = 0x20,
59 /// \brief Implicit barrier in code.
60 OMP_IDENT_BARRIER_IMPL = 0x40,
61 /// \brief Implicit barrier in 'for' directive.
62 OMP_IDENT_BARRIER_IMPL_FOR = 0x40,
63 /// \brief Implicit barrier in 'sections' directive.
64 OMP_IDENT_BARRIER_IMPL_SECTIONS = 0xC0,
65 /// \brief Implicit barrier in 'single' directive.
66 OMP_IDENT_BARRIER_IMPL_SINGLE = 0x140
67 };
68 enum OpenMPRTLFunction {
Alexey Bataev97720002014-11-11 04:05:39 +000069 /// \brief Call to void __kmpc_fork_call(ident_t *loc, kmp_int32 argc,
70 /// kmpc_micro microtask, ...);
Alexey Bataev9959db52014-05-06 10:08:46 +000071 OMPRTL__kmpc_fork_call,
Alexey Bataev97720002014-11-11 04:05:39 +000072 /// \brief Call to void *__kmpc_threadprivate_cached(ident_t *loc,
73 /// kmp_int32 global_tid, void *data, size_t size, void ***cache);
74 OMPRTL__kmpc_threadprivate_cached,
75 /// \brief Call to void __kmpc_threadprivate_register( ident_t *,
76 /// void *data, kmpc_ctor ctor, kmpc_cctor cctor, kmpc_dtor dtor);
77 OMPRTL__kmpc_threadprivate_register,
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +000078 // Call to __kmpc_int32 kmpc_global_thread_num(ident_t *loc);
79 OMPRTL__kmpc_global_thread_num,
Alexey Bataevf9472182014-09-22 12:32:31 +000080 // Call to void __kmpc_critical(ident_t *loc, kmp_int32 global_tid,
81 // kmp_critical_name *crit);
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +000082 OMPRTL__kmpc_critical,
Alexey Bataevf9472182014-09-22 12:32:31 +000083 // Call to void __kmpc_end_critical(ident_t *loc, kmp_int32 global_tid,
84 // kmp_critical_name *crit);
Alexey Bataev4a5bb772014-10-08 14:01:46 +000085 OMPRTL__kmpc_end_critical,
86 // Call to void __kmpc_barrier(ident_t *loc, kmp_int32 global_tid);
Alexey Bataevd74d0602014-10-13 06:02:40 +000087 OMPRTL__kmpc_barrier,
88 // Call to void __kmpc_serialized_parallel(ident_t *loc, kmp_int32
89 // global_tid);
90 OMPRTL__kmpc_serialized_parallel,
91 // Call to void __kmpc_end_serialized_parallel(ident_t *loc, kmp_int32
92 // global_tid);
Alexey Bataevb2059782014-10-13 08:23:51 +000093 OMPRTL__kmpc_end_serialized_parallel,
94 // Call to void __kmpc_push_num_threads(ident_t *loc, kmp_int32 global_tid,
95 // kmp_int32 num_threads);
96 OMPRTL__kmpc_push_num_threads
Alexey Bataev9959db52014-05-06 10:08:46 +000097 };
98
99private:
100 CodeGenModule &CGM;
101 /// \brief Default const ident_t object used for initialization of all other
102 /// ident_t objects.
103 llvm::Constant *DefaultOpenMPPSource;
Alexey Bataev18095712014-10-10 12:19:54 +0000104 /// \brief Map of flags and corresponding default locations.
Alexey Bataev15007ba2014-05-07 06:18:01 +0000105 typedef llvm::DenseMap<unsigned, llvm::Value *> OpenMPDefaultLocMapTy;
106 OpenMPDefaultLocMapTy OpenMPDefaultLocMap;
Alexey Bataev9959db52014-05-06 10:08:46 +0000107 llvm::Value *GetOrCreateDefaultOpenMPLocation(OpenMPLocationFlags Flags);
108 /// \brief Describes ident structure that describes a source location.
109 /// All descriptions are taken from
110 /// http://llvm.org/svn/llvm-project/openmp/trunk/runtime/src/kmp.h
111 /// Original structure:
112 /// typedef struct ident {
113 /// kmp_int32 reserved_1; /**< might be used in Fortran;
114 /// see above */
115 /// kmp_int32 flags; /**< also f.flags; KMP_IDENT_xxx flags;
116 /// KMP_IDENT_KMPC identifies this union
117 /// member */
118 /// kmp_int32 reserved_2; /**< not really used in Fortran any more;
119 /// see above */
120 ///#if USE_ITT_BUILD
121 /// /* but currently used for storing
122 /// region-specific ITT */
123 /// /* contextual information. */
124 ///#endif /* USE_ITT_BUILD */
125 /// kmp_int32 reserved_3; /**< source[4] in Fortran, do not use for
126 /// C++ */
127 /// char const *psource; /**< String describing the source location.
128 /// The string is composed of semi-colon separated
129 // fields which describe the source file,
130 /// the function and a pair of line numbers that
131 /// delimit the construct.
132 /// */
133 /// } ident_t;
134 enum IdentFieldIndex {
135 /// \brief might be used in Fortran
136 IdentField_Reserved_1,
137 /// \brief OMP_IDENT_xxx flags; OMP_IDENT_KMPC identifies this union member.
138 IdentField_Flags,
139 /// \brief Not really used in Fortran any more
140 IdentField_Reserved_2,
141 /// \brief Source[4] in Fortran, do not use for C++
142 IdentField_Reserved_3,
143 /// \brief String describing the source location. The string is composed of
144 /// semi-colon separated fields which describe the source file, the function
145 /// and a pair of line numbers that delimit the construct.
146 IdentField_PSource
147 };
148 llvm::StructType *IdentTy;
Alexey Bataev18095712014-10-10 12:19:54 +0000149 /// \brief Map for SourceLocation and OpenMP runtime library debug locations.
Alexey Bataevf002aca2014-05-30 05:48:40 +0000150 typedef llvm::DenseMap<unsigned, llvm::Value *> OpenMPDebugLocMapTy;
151 OpenMPDebugLocMapTy OpenMPDebugLocMap;
Alexey Bataev9959db52014-05-06 10:08:46 +0000152 /// \brief The type for a microtask which gets passed to __kmpc_fork_call().
153 /// Original representation is:
154 /// typedef void (kmpc_micro)(kmp_int32 global_tid, kmp_int32 bound_tid,...);
155 llvm::FunctionType *Kmpc_MicroTy;
Alexey Bataev18095712014-10-10 12:19:54 +0000156 /// \brief Stores debug location and ThreadID for the function.
157 struct DebugLocThreadIdTy {
158 llvm::Value *DebugLoc;
159 llvm::Value *ThreadID;
160 };
161 /// \brief Map of local debug location, ThreadId and functions.
162 typedef llvm::DenseMap<llvm::Function *, DebugLocThreadIdTy>
163 OpenMPLocThreadIDMapTy;
164 OpenMPLocThreadIDMapTy OpenMPLocThreadIDMap;
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +0000165 /// \brief Type kmp_critical_name, originally defined as typedef kmp_int32
166 /// kmp_critical_name[8];
167 llvm::ArrayType *KmpCriticalNameTy;
Alexey Bataev97720002014-11-11 04:05:39 +0000168 /// \brief An ordered map of auto-generated variables to their unique names.
169 /// It stores variables with the following names: 1) ".gomp_critical_user_" +
170 /// <critical_section_name> + ".var" for "omp critical" directives; 2)
171 /// <mangled_name_for_global_var> + ".cache." for cache for threadprivate
172 /// variables.
173 llvm::StringMap<llvm::AssertingVH<llvm::Constant>, llvm::BumpPtrAllocator>
174 InternalVars;
Alexey Bataev9959db52014-05-06 10:08:46 +0000175
Alexey Bataev9959db52014-05-06 10:08:46 +0000176 /// \brief Emits object of ident_t type with info for source location.
Alexey Bataev9959db52014-05-06 10:08:46 +0000177 /// \param Flags Flags for OpenMP location.
178 ///
179 llvm::Value *
180 EmitOpenMPUpdateLocation(CodeGenFunction &CGF, SourceLocation Loc,
181 OpenMPLocationFlags Flags = OMP_IDENT_KMPC);
182
Alexey Bataevd74d0602014-10-13 06:02:40 +0000183 /// \brief Returns pointer to ident_t type.
Alexey Bataev9959db52014-05-06 10:08:46 +0000184 llvm::Type *getIdentTyPointerTy();
185
Alexey Bataevd74d0602014-10-13 06:02:40 +0000186 /// \brief Returns pointer to kmpc_micro type.
Alexey Bataev9959db52014-05-06 10:08:46 +0000187 llvm::Type *getKmpc_MicroPointerTy();
188
189 /// \brief Returns specified OpenMP runtime function.
190 /// \param Function OpenMP runtime function.
191 /// \return Specified function.
192 llvm::Constant *CreateRuntimeFunction(OpenMPRTLFunction Function);
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +0000193
Alexey Bataev97720002014-11-11 04:05:39 +0000194 /// \brief If the specified mangled name is not in the module, create and
195 /// return threadprivate cache object. This object is a pointer's worth of
196 /// storage that's reserved for use by the OpenMP runtime.
NAKAMURA Takumicdcbfba2014-11-11 07:58:06 +0000197 /// \param VD Threadprivate variable.
Alexey Bataev97720002014-11-11 04:05:39 +0000198 /// \return Cache variable for the specified threadprivate.
199 llvm::Constant *getOrCreateThreadPrivateCache(const VarDecl *VD);
200
Alexey Bataevd74d0602014-10-13 06:02:40 +0000201 /// \brief Emits address of the word in a memory where current thread id is
202 /// stored.
203 virtual llvm::Value *EmitThreadIDAddress(CodeGenFunction &CGF,
204 SourceLocation Loc);
205
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000206 /// \brief Gets thread id value for the current thread.
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000207 ///
208 llvm::Value *GetOpenMPThreadID(CodeGenFunction &CGF, SourceLocation Loc);
209
Alexey Bataev97720002014-11-11 04:05:39 +0000210 /// \brief Gets (if variable with the given name already exist) or creates
211 /// internal global variable with the specified Name. The created variable has
212 /// linkage CommonLinkage by default and is initialized by null value.
213 /// \param Ty Type of the global variable. If it is exist already the type
214 /// must be the same.
215 /// \param Name Name of the variable.
216 llvm::Constant *GetOrCreateInternalVariable(llvm::Type *Ty,
217 const llvm::Twine &Name);
218
219 /// \brief Set of threadprivate variables with the generated initializer.
220 llvm::DenseSet<const VarDecl *> ThreadPrivateWithDefinition;
221
222 /// \brief Emits initialization code for the threadprivate variables.
223 /// \param VDAddr Address of the global variable \a VD.
224 /// \param Ctor Pointer to a global init function for \a VD.
225 /// \param CopyCtor Pointer to a global copy function for \a VD.
226 /// \param Dtor Pointer to a global destructor function for \a VD.
227 /// \param Loc Location of threadprivate declaration.
228 void EmitOMPThreadPrivateVarInit(CodeGenFunction &CGF, llvm::Value *VDAddr,
229 llvm::Value *Ctor, llvm::Value *CopyCtor,
230 llvm::Value *Dtor, SourceLocation Loc);
231
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000232public:
233 explicit CGOpenMPRuntime(CodeGenModule &CGM);
234 virtual ~CGOpenMPRuntime() {}
235
Alexey Bataev18095712014-10-10 12:19:54 +0000236 /// \brief Emits outlined function for the specified OpenMP directive \a D
237 /// (required for parallel and task directives). This outlined function has
238 /// type void(*)(kmp_int32 /*ThreadID*/, kmp_int32 /*BoundID*/, struct
239 /// context_vars*).
240 /// \param D OpenMP directive.
241 /// \param ThreadIDVar Variable for thread id in the current OpenMP region.
242 ///
243 virtual llvm::Value *
244 EmitOpenMPOutlinedFunction(const OMPExecutableDirective &D,
245 const VarDecl *ThreadIDVar);
246
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000247 /// \brief Cleans up references to the objects in finished function.
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000248 ///
249 void FunctionFinished(CodeGenFunction &CGF);
250
251 /// \brief Emits code for parallel call of the \a OutlinedFn with variables
252 /// captured in a record which address is stored in \a CapturedStruct.
Alexey Bataev18095712014-10-10 12:19:54 +0000253 /// \param OutlinedFn Outlined function to be run in parallel threads. Type of
254 /// this function is void(*)(kmp_int32, kmp_int32, struct context_vars*).
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000255 /// \param CapturedStruct A pointer to the record with the references to
256 /// variables used in \a OutlinedFn function.
257 ///
258 virtual void EmitOMPParallelCall(CodeGenFunction &CGF, SourceLocation Loc,
259 llvm::Value *OutlinedFn,
260 llvm::Value *CapturedStruct);
261
Alexey Bataevd74d0602014-10-13 06:02:40 +0000262 /// \brief Emits code for serial call of the \a OutlinedFn with variables
263 /// captured in a record which address is stored in \a CapturedStruct.
264 /// \param OutlinedFn Outlined function to be run in serial mode.
265 /// \param CapturedStruct A pointer to the record with the references to
266 /// variables used in \a OutlinedFn function.
267 ///
268 virtual void EmitOMPSerialCall(CodeGenFunction &CGF, SourceLocation Loc,
269 llvm::Value *OutlinedFn,
270 llvm::Value *CapturedStruct);
271
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +0000272 /// \brief Returns corresponding lock object for the specified critical region
273 /// name. If the lock object does not exist it is created, otherwise the
274 /// reference to the existing copy is returned.
Alexey Bataev18095712014-10-10 12:19:54 +0000275 /// \param CriticalName Name of the critical region.
276 ///
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +0000277 llvm::Value *GetCriticalRegionLock(StringRef CriticalName);
278
279 /// \brief Emits start of the critical region by calling void
Alexey Bataevf9472182014-09-22 12:32:31 +0000280 /// __kmpc_critical(ident_t *loc, kmp_int32 global_tid, kmp_critical_name
281 /// * \a RegionLock)
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +0000282 /// \param RegionLock The lock object for critical region.
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +0000283 virtual void EmitOMPCriticalRegionStart(CodeGenFunction &CGF,
284 llvm::Value *RegionLock,
285 SourceLocation Loc);
286
287 /// \brief Emits end of the critical region by calling void
Alexey Bataevf9472182014-09-22 12:32:31 +0000288 /// __kmpc_end_critical(ident_t *loc, kmp_int32 global_tid, kmp_critical_name
289 /// * \a RegionLock)
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +0000290 /// \param RegionLock The lock object for critical region.
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +0000291 virtual void EmitOMPCriticalRegionEnd(CodeGenFunction &CGF,
292 llvm::Value *RegionLock,
293 SourceLocation Loc);
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000294
295 /// \brief Emits a barrier for OpenMP threads.
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000296 /// \param Flags Flags for the barrier.
297 ///
298 virtual void EmitOMPBarrierCall(CodeGenFunction &CGF, SourceLocation Loc,
299 OpenMPLocationFlags Flags);
Alexey Bataevb2059782014-10-13 08:23:51 +0000300
301 /// \brief Emits call to void __kmpc_push_num_threads(ident_t *loc, kmp_int32
302 /// global_tid, kmp_int32 num_threads) to generate code for 'num_threads'
303 /// clause.
304 /// \param NumThreads An integer value of threads.
305 virtual void EmitOMPNumThreadsClause(CodeGenFunction &CGF,
306 llvm::Value *NumThreads,
307 SourceLocation Loc);
Alexey Bataev97720002014-11-11 04:05:39 +0000308
309 /// \brief Returns address of the threadprivate variable for the current
310 /// thread.
NAKAMURA Takumicdcbfba2014-11-11 07:58:06 +0000311 /// \param VD Threadprivate variable.
Alexey Bataev97720002014-11-11 04:05:39 +0000312 /// \param VDAddr Address of the global variable \a VD.
313 /// \param Loc Location of the reference to threadprivate var.
314 /// \return Address of the threadprivate variable for the current thread.
315 virtual llvm::Value *getOMPAddrOfThreadPrivate(CodeGenFunction &CGF,
316 const VarDecl *VD,
317 llvm::Value *VDAddr,
318 SourceLocation Loc);
319
320 /// \brief Emit a code for initialization of threadprivate variable. It emits
321 /// a call to runtime library which adds initial value to the newly created
322 /// threadprivate variable (if it is not constant) and registers destructor
323 /// for the variable (if any).
324 /// \param VD Threadprivate variable.
325 /// \param VDAddr Address of the global variable \a VD.
326 /// \param Loc Location of threadprivate declaration.
327 /// \param PerformInit true if initialization expression is not constant.
328 virtual llvm::Function *
329 EmitOMPThreadPrivateVarDefinition(const VarDecl *VD, llvm::Value *VDAddr,
330 SourceLocation Loc, bool PerformInit,
331 CodeGenFunction *CGF = nullptr);
Alexey Bataev9959db52014-05-06 10:08:46 +0000332};
Alexey Bataev23b69422014-06-18 07:08:49 +0000333} // namespace CodeGen
334} // namespace clang
Alexey Bataev9959db52014-05-06 10:08:46 +0000335
336#endif