blob: 09e3272e9433a57e2ef18ae3274e37db3aad60c5 [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 Bataev75ddfab2014-12-01 11:32:38 +0000233 /// \brief Returns corresponding lock object for the specified critical region
234 /// name. If the lock object does not exist it is created, otherwise the
235 /// reference to the existing copy is returned.
236 /// \param CriticalName Name of the critical region.
237 ///
238 llvm::Value *GetCriticalRegionLock(StringRef CriticalName);
239
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000240public:
241 explicit CGOpenMPRuntime(CodeGenModule &CGM);
242 virtual ~CGOpenMPRuntime() {}
243
Alexey Bataev18095712014-10-10 12:19:54 +0000244 /// \brief Emits outlined function for the specified OpenMP directive \a D
245 /// (required for parallel and task directives). This outlined function has
246 /// type void(*)(kmp_int32 /*ThreadID*/, kmp_int32 /*BoundID*/, struct
247 /// context_vars*).
248 /// \param D OpenMP directive.
249 /// \param ThreadIDVar Variable for thread id in the current OpenMP region.
250 ///
251 virtual llvm::Value *
252 EmitOpenMPOutlinedFunction(const OMPExecutableDirective &D,
253 const VarDecl *ThreadIDVar);
254
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000255 /// \brief Cleans up references to the objects in finished function.
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000256 ///
257 void FunctionFinished(CodeGenFunction &CGF);
258
259 /// \brief Emits code for parallel call of the \a OutlinedFn with variables
260 /// captured in a record which address is stored in \a CapturedStruct.
Alexey Bataev18095712014-10-10 12:19:54 +0000261 /// \param OutlinedFn Outlined function to be run in parallel threads. Type of
262 /// this function is void(*)(kmp_int32, kmp_int32, struct context_vars*).
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000263 /// \param CapturedStruct A pointer to the record with the references to
264 /// variables used in \a OutlinedFn function.
265 ///
266 virtual void EmitOMPParallelCall(CodeGenFunction &CGF, SourceLocation Loc,
267 llvm::Value *OutlinedFn,
268 llvm::Value *CapturedStruct);
269
Alexey Bataevd74d0602014-10-13 06:02:40 +0000270 /// \brief Emits code for serial call of the \a OutlinedFn with variables
271 /// captured in a record which address is stored in \a CapturedStruct.
272 /// \param OutlinedFn Outlined function to be run in serial mode.
273 /// \param CapturedStruct A pointer to the record with the references to
274 /// variables used in \a OutlinedFn function.
275 ///
276 virtual void EmitOMPSerialCall(CodeGenFunction &CGF, SourceLocation Loc,
277 llvm::Value *OutlinedFn,
278 llvm::Value *CapturedStruct);
279
Alexey Bataev75ddfab2014-12-01 11:32:38 +0000280 /// \brief Emits a critical region.
Alexey Bataev18095712014-10-10 12:19:54 +0000281 /// \param CriticalName Name of the critical region.
Alexey Bataev75ddfab2014-12-01 11:32:38 +0000282 /// \param CriticalOpGen Generator for the statement associated with the given
283 /// critical region.
284 virtual void EmitOMPCriticalRegion(CodeGenFunction &CGF,
285 StringRef CriticalName,
286 const std::function<void()> &CriticalOpGen,
287 SourceLocation Loc);
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000288
289 /// \brief Emits a barrier for OpenMP threads.
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000290 /// \param Flags Flags for the barrier.
291 ///
292 virtual void EmitOMPBarrierCall(CodeGenFunction &CGF, SourceLocation Loc,
293 OpenMPLocationFlags Flags);
Alexey Bataevb2059782014-10-13 08:23:51 +0000294
295 /// \brief Emits call to void __kmpc_push_num_threads(ident_t *loc, kmp_int32
296 /// global_tid, kmp_int32 num_threads) to generate code for 'num_threads'
297 /// clause.
298 /// \param NumThreads An integer value of threads.
299 virtual void EmitOMPNumThreadsClause(CodeGenFunction &CGF,
300 llvm::Value *NumThreads,
301 SourceLocation Loc);
Alexey Bataev97720002014-11-11 04:05:39 +0000302
303 /// \brief Returns address of the threadprivate variable for the current
304 /// thread.
NAKAMURA Takumicdcbfba2014-11-11 07:58:06 +0000305 /// \param VD Threadprivate variable.
Alexey Bataev97720002014-11-11 04:05:39 +0000306 /// \param VDAddr Address of the global variable \a VD.
307 /// \param Loc Location of the reference to threadprivate var.
308 /// \return Address of the threadprivate variable for the current thread.
309 virtual llvm::Value *getOMPAddrOfThreadPrivate(CodeGenFunction &CGF,
310 const VarDecl *VD,
311 llvm::Value *VDAddr,
312 SourceLocation Loc);
313
314 /// \brief Emit a code for initialization of threadprivate variable. It emits
315 /// a call to runtime library which adds initial value to the newly created
316 /// threadprivate variable (if it is not constant) and registers destructor
317 /// for the variable (if any).
318 /// \param VD Threadprivate variable.
319 /// \param VDAddr Address of the global variable \a VD.
320 /// \param Loc Location of threadprivate declaration.
321 /// \param PerformInit true if initialization expression is not constant.
322 virtual llvm::Function *
323 EmitOMPThreadPrivateVarDefinition(const VarDecl *VD, llvm::Value *VDAddr,
324 SourceLocation Loc, bool PerformInit,
325 CodeGenFunction *CGF = nullptr);
Alexey Bataevcc37cc12014-11-20 04:34:54 +0000326
327 /// \brief Emit flush of the variables specified in 'omp flush' directive.
328 /// \param Vars List of variables to flush.
329 virtual void EmitOMPFlush(CodeGenFunction &CGF, ArrayRef<const Expr *> Vars,
330 SourceLocation Loc);
Alexey Bataev9959db52014-05-06 10:08:46 +0000331};
Alexey Bataev23b69422014-06-18 07:08:49 +0000332} // namespace CodeGen
333} // namespace clang
Alexey Bataev9959db52014-05-06 10:08:46 +0000334
335#endif