blob: 65348c9c3a107d42ed936bd834c327ec97890ff1 [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 };
Alexey Bataev1e4b7132014-12-03 12:11:24 +000067
68private:
Alexey Bataev9959db52014-05-06 10:08:46 +000069 enum OpenMPRTLFunction {
Alexey Bataev97720002014-11-11 04:05:39 +000070 /// \brief Call to void __kmpc_fork_call(ident_t *loc, kmp_int32 argc,
71 /// kmpc_micro microtask, ...);
Alexey Bataev9959db52014-05-06 10:08:46 +000072 OMPRTL__kmpc_fork_call,
Alexey Bataev97720002014-11-11 04:05:39 +000073 /// \brief Call to void *__kmpc_threadprivate_cached(ident_t *loc,
74 /// kmp_int32 global_tid, void *data, size_t size, void ***cache);
75 OMPRTL__kmpc_threadprivate_cached,
76 /// \brief Call to void __kmpc_threadprivate_register( ident_t *,
77 /// void *data, kmpc_ctor ctor, kmpc_cctor cctor, kmpc_dtor dtor);
78 OMPRTL__kmpc_threadprivate_register,
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +000079 // Call to __kmpc_int32 kmpc_global_thread_num(ident_t *loc);
80 OMPRTL__kmpc_global_thread_num,
Alexey Bataevf9472182014-09-22 12:32:31 +000081 // Call to void __kmpc_critical(ident_t *loc, kmp_int32 global_tid,
82 // kmp_critical_name *crit);
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +000083 OMPRTL__kmpc_critical,
Alexey Bataevf9472182014-09-22 12:32:31 +000084 // Call to void __kmpc_end_critical(ident_t *loc, kmp_int32 global_tid,
85 // kmp_critical_name *crit);
Alexey Bataev4a5bb772014-10-08 14:01:46 +000086 OMPRTL__kmpc_end_critical,
87 // Call to void __kmpc_barrier(ident_t *loc, kmp_int32 global_tid);
Alexey Bataevd74d0602014-10-13 06:02:40 +000088 OMPRTL__kmpc_barrier,
89 // Call to void __kmpc_serialized_parallel(ident_t *loc, kmp_int32
90 // global_tid);
91 OMPRTL__kmpc_serialized_parallel,
92 // Call to void __kmpc_end_serialized_parallel(ident_t *loc, kmp_int32
93 // global_tid);
Alexey Bataevb2059782014-10-13 08:23:51 +000094 OMPRTL__kmpc_end_serialized_parallel,
95 // Call to void __kmpc_push_num_threads(ident_t *loc, kmp_int32 global_tid,
96 // kmp_int32 num_threads);
Alexey Bataevcc37cc12014-11-20 04:34:54 +000097 OMPRTL__kmpc_push_num_threads,
98 // Call to void __kmpc_flush(ident_t *loc, ...);
Alexey Bataev8d690652014-12-04 07:23:53 +000099 OMPRTL__kmpc_flush,
100 // Call to kmp_int32 __kmpc_master(ident_t *, kmp_int32 global_tid);
101 OMPRTL__kmpc_master,
102 // Call to void __kmpc_end_master(ident_t *, kmp_int32 global_tid);
103 OMPRTL__kmpc_end_master,
Alexey Bataev9959db52014-05-06 10:08:46 +0000104 };
105
Alexey Bataev9959db52014-05-06 10:08:46 +0000106 CodeGenModule &CGM;
107 /// \brief Default const ident_t object used for initialization of all other
108 /// ident_t objects.
109 llvm::Constant *DefaultOpenMPPSource;
Alexey Bataev18095712014-10-10 12:19:54 +0000110 /// \brief Map of flags and corresponding default locations.
Alexey Bataev15007ba2014-05-07 06:18:01 +0000111 typedef llvm::DenseMap<unsigned, llvm::Value *> OpenMPDefaultLocMapTy;
112 OpenMPDefaultLocMapTy OpenMPDefaultLocMap;
Alexey Bataev9959db52014-05-06 10:08:46 +0000113 llvm::Value *GetOrCreateDefaultOpenMPLocation(OpenMPLocationFlags Flags);
114 /// \brief Describes ident structure that describes a source location.
115 /// All descriptions are taken from
116 /// http://llvm.org/svn/llvm-project/openmp/trunk/runtime/src/kmp.h
117 /// Original structure:
118 /// typedef struct ident {
119 /// kmp_int32 reserved_1; /**< might be used in Fortran;
120 /// see above */
121 /// kmp_int32 flags; /**< also f.flags; KMP_IDENT_xxx flags;
122 /// KMP_IDENT_KMPC identifies this union
123 /// member */
124 /// kmp_int32 reserved_2; /**< not really used in Fortran any more;
125 /// see above */
126 ///#if USE_ITT_BUILD
127 /// /* but currently used for storing
128 /// region-specific ITT */
129 /// /* contextual information. */
130 ///#endif /* USE_ITT_BUILD */
131 /// kmp_int32 reserved_3; /**< source[4] in Fortran, do not use for
132 /// C++ */
133 /// char const *psource; /**< String describing the source location.
134 /// The string is composed of semi-colon separated
135 // fields which describe the source file,
136 /// the function and a pair of line numbers that
137 /// delimit the construct.
138 /// */
139 /// } ident_t;
140 enum IdentFieldIndex {
141 /// \brief might be used in Fortran
142 IdentField_Reserved_1,
143 /// \brief OMP_IDENT_xxx flags; OMP_IDENT_KMPC identifies this union member.
144 IdentField_Flags,
145 /// \brief Not really used in Fortran any more
146 IdentField_Reserved_2,
147 /// \brief Source[4] in Fortran, do not use for C++
148 IdentField_Reserved_3,
149 /// \brief String describing the source location. The string is composed of
150 /// semi-colon separated fields which describe the source file, the function
151 /// and a pair of line numbers that delimit the construct.
152 IdentField_PSource
153 };
154 llvm::StructType *IdentTy;
Alexey Bataev18095712014-10-10 12:19:54 +0000155 /// \brief Map for SourceLocation and OpenMP runtime library debug locations.
Alexey Bataevf002aca2014-05-30 05:48:40 +0000156 typedef llvm::DenseMap<unsigned, llvm::Value *> OpenMPDebugLocMapTy;
157 OpenMPDebugLocMapTy OpenMPDebugLocMap;
Alexey Bataev9959db52014-05-06 10:08:46 +0000158 /// \brief The type for a microtask which gets passed to __kmpc_fork_call().
159 /// Original representation is:
160 /// typedef void (kmpc_micro)(kmp_int32 global_tid, kmp_int32 bound_tid,...);
161 llvm::FunctionType *Kmpc_MicroTy;
Alexey Bataev18095712014-10-10 12:19:54 +0000162 /// \brief Stores debug location and ThreadID for the function.
163 struct DebugLocThreadIdTy {
164 llvm::Value *DebugLoc;
165 llvm::Value *ThreadID;
166 };
167 /// \brief Map of local debug location, ThreadId and functions.
168 typedef llvm::DenseMap<llvm::Function *, DebugLocThreadIdTy>
169 OpenMPLocThreadIDMapTy;
170 OpenMPLocThreadIDMapTy OpenMPLocThreadIDMap;
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +0000171 /// \brief Type kmp_critical_name, originally defined as typedef kmp_int32
172 /// kmp_critical_name[8];
173 llvm::ArrayType *KmpCriticalNameTy;
Alexey Bataev97720002014-11-11 04:05:39 +0000174 /// \brief An ordered map of auto-generated variables to their unique names.
175 /// It stores variables with the following names: 1) ".gomp_critical_user_" +
176 /// <critical_section_name> + ".var" for "omp critical" directives; 2)
177 /// <mangled_name_for_global_var> + ".cache." for cache for threadprivate
178 /// variables.
179 llvm::StringMap<llvm::AssertingVH<llvm::Constant>, llvm::BumpPtrAllocator>
180 InternalVars;
Alexey Bataev9959db52014-05-06 10:08:46 +0000181
Alexey Bataev9959db52014-05-06 10:08:46 +0000182 /// \brief Emits object of ident_t type with info for source location.
Alexey Bataev9959db52014-05-06 10:08:46 +0000183 /// \param Flags Flags for OpenMP location.
184 ///
185 llvm::Value *
186 EmitOpenMPUpdateLocation(CodeGenFunction &CGF, SourceLocation Loc,
187 OpenMPLocationFlags Flags = OMP_IDENT_KMPC);
188
Alexey Bataevd74d0602014-10-13 06:02:40 +0000189 /// \brief Returns pointer to ident_t type.
Alexey Bataev9959db52014-05-06 10:08:46 +0000190 llvm::Type *getIdentTyPointerTy();
191
Alexey Bataevd74d0602014-10-13 06:02:40 +0000192 /// \brief Returns pointer to kmpc_micro type.
Alexey Bataev9959db52014-05-06 10:08:46 +0000193 llvm::Type *getKmpc_MicroPointerTy();
194
195 /// \brief Returns specified OpenMP runtime function.
196 /// \param Function OpenMP runtime function.
197 /// \return Specified function.
198 llvm::Constant *CreateRuntimeFunction(OpenMPRTLFunction Function);
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +0000199
Alexey Bataev97720002014-11-11 04:05:39 +0000200 /// \brief If the specified mangled name is not in the module, create and
201 /// return threadprivate cache object. This object is a pointer's worth of
202 /// storage that's reserved for use by the OpenMP runtime.
NAKAMURA Takumicdcbfba2014-11-11 07:58:06 +0000203 /// \param VD Threadprivate variable.
Alexey Bataev97720002014-11-11 04:05:39 +0000204 /// \return Cache variable for the specified threadprivate.
205 llvm::Constant *getOrCreateThreadPrivateCache(const VarDecl *VD);
206
Alexey Bataevd74d0602014-10-13 06:02:40 +0000207 /// \brief Emits address of the word in a memory where current thread id is
208 /// stored.
209 virtual llvm::Value *EmitThreadIDAddress(CodeGenFunction &CGF,
210 SourceLocation Loc);
211
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000212 /// \brief Gets thread id value for the current thread.
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000213 ///
214 llvm::Value *GetOpenMPThreadID(CodeGenFunction &CGF, SourceLocation Loc);
215
Alexey Bataev97720002014-11-11 04:05:39 +0000216 /// \brief Gets (if variable with the given name already exist) or creates
217 /// internal global variable with the specified Name. The created variable has
218 /// linkage CommonLinkage by default and is initialized by null value.
219 /// \param Ty Type of the global variable. If it is exist already the type
220 /// must be the same.
221 /// \param Name Name of the variable.
222 llvm::Constant *GetOrCreateInternalVariable(llvm::Type *Ty,
223 const llvm::Twine &Name);
224
225 /// \brief Set of threadprivate variables with the generated initializer.
226 llvm::DenseSet<const VarDecl *> ThreadPrivateWithDefinition;
227
228 /// \brief Emits initialization code for the threadprivate variables.
229 /// \param VDAddr Address of the global variable \a VD.
230 /// \param Ctor Pointer to a global init function for \a VD.
231 /// \param CopyCtor Pointer to a global copy function for \a VD.
232 /// \param Dtor Pointer to a global destructor function for \a VD.
233 /// \param Loc Location of threadprivate declaration.
234 void EmitOMPThreadPrivateVarInit(CodeGenFunction &CGF, llvm::Value *VDAddr,
235 llvm::Value *Ctor, llvm::Value *CopyCtor,
236 llvm::Value *Dtor, SourceLocation Loc);
237
Alexey Bataev75ddfab2014-12-01 11:32:38 +0000238 /// \brief Returns corresponding lock object for the specified critical region
239 /// name. If the lock object does not exist it is created, otherwise the
240 /// reference to the existing copy is returned.
241 /// \param CriticalName Name of the critical region.
242 ///
243 llvm::Value *GetCriticalRegionLock(StringRef CriticalName);
244
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000245public:
246 explicit CGOpenMPRuntime(CodeGenModule &CGM);
247 virtual ~CGOpenMPRuntime() {}
248
Alexey Bataev18095712014-10-10 12:19:54 +0000249 /// \brief Emits outlined function for the specified OpenMP directive \a D
250 /// (required for parallel and task directives). This outlined function has
251 /// type void(*)(kmp_int32 /*ThreadID*/, kmp_int32 /*BoundID*/, struct
252 /// context_vars*).
253 /// \param D OpenMP directive.
254 /// \param ThreadIDVar Variable for thread id in the current OpenMP region.
255 ///
256 virtual llvm::Value *
257 EmitOpenMPOutlinedFunction(const OMPExecutableDirective &D,
258 const VarDecl *ThreadIDVar);
259
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000260 /// \brief Cleans up references to the objects in finished function.
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000261 ///
262 void FunctionFinished(CodeGenFunction &CGF);
263
264 /// \brief Emits code for parallel call of the \a OutlinedFn with variables
265 /// captured in a record which address is stored in \a CapturedStruct.
Alexey Bataev18095712014-10-10 12:19:54 +0000266 /// \param OutlinedFn Outlined function to be run in parallel threads. Type of
267 /// this function is void(*)(kmp_int32, kmp_int32, struct context_vars*).
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000268 /// \param CapturedStruct A pointer to the record with the references to
269 /// variables used in \a OutlinedFn function.
270 ///
271 virtual void EmitOMPParallelCall(CodeGenFunction &CGF, SourceLocation Loc,
272 llvm::Value *OutlinedFn,
273 llvm::Value *CapturedStruct);
274
Alexey Bataevd74d0602014-10-13 06:02:40 +0000275 /// \brief Emits code for serial call of the \a OutlinedFn with variables
276 /// captured in a record which address is stored in \a CapturedStruct.
277 /// \param OutlinedFn Outlined function to be run in serial mode.
278 /// \param CapturedStruct A pointer to the record with the references to
279 /// variables used in \a OutlinedFn function.
280 ///
281 virtual void EmitOMPSerialCall(CodeGenFunction &CGF, SourceLocation Loc,
282 llvm::Value *OutlinedFn,
283 llvm::Value *CapturedStruct);
284
Alexey Bataev75ddfab2014-12-01 11:32:38 +0000285 /// \brief Emits a critical region.
Alexey Bataev18095712014-10-10 12:19:54 +0000286 /// \param CriticalName Name of the critical region.
Alexey Bataev75ddfab2014-12-01 11:32:38 +0000287 /// \param CriticalOpGen Generator for the statement associated with the given
288 /// critical region.
289 virtual void EmitOMPCriticalRegion(CodeGenFunction &CGF,
290 StringRef CriticalName,
291 const std::function<void()> &CriticalOpGen,
292 SourceLocation Loc);
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000293
Alexey Bataev8d690652014-12-04 07:23:53 +0000294 /// \brief Emits a master region.
295 /// \param MasterOpGen Generator for the statement associated with the given
296 /// master region.
297 virtual void EmitOMPMasterRegion(CodeGenFunction &CGF,
298 const std::function<void()> &MasterOpGen,
299 SourceLocation Loc);
300
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000301 /// \brief Emits a barrier for OpenMP threads.
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000302 /// \param Flags Flags for the barrier.
303 ///
304 virtual void EmitOMPBarrierCall(CodeGenFunction &CGF, SourceLocation Loc,
305 OpenMPLocationFlags Flags);
Alexey Bataevb2059782014-10-13 08:23:51 +0000306
307 /// \brief Emits call to void __kmpc_push_num_threads(ident_t *loc, kmp_int32
308 /// global_tid, kmp_int32 num_threads) to generate code for 'num_threads'
309 /// clause.
310 /// \param NumThreads An integer value of threads.
311 virtual void EmitOMPNumThreadsClause(CodeGenFunction &CGF,
312 llvm::Value *NumThreads,
313 SourceLocation Loc);
Alexey Bataev97720002014-11-11 04:05:39 +0000314
315 /// \brief Returns address of the threadprivate variable for the current
316 /// thread.
NAKAMURA Takumicdcbfba2014-11-11 07:58:06 +0000317 /// \param VD Threadprivate variable.
Alexey Bataev97720002014-11-11 04:05:39 +0000318 /// \param VDAddr Address of the global variable \a VD.
319 /// \param Loc Location of the reference to threadprivate var.
320 /// \return Address of the threadprivate variable for the current thread.
321 virtual llvm::Value *getOMPAddrOfThreadPrivate(CodeGenFunction &CGF,
322 const VarDecl *VD,
323 llvm::Value *VDAddr,
324 SourceLocation Loc);
325
326 /// \brief Emit a code for initialization of threadprivate variable. It emits
327 /// a call to runtime library which adds initial value to the newly created
328 /// threadprivate variable (if it is not constant) and registers destructor
329 /// for the variable (if any).
330 /// \param VD Threadprivate variable.
331 /// \param VDAddr Address of the global variable \a VD.
332 /// \param Loc Location of threadprivate declaration.
333 /// \param PerformInit true if initialization expression is not constant.
334 virtual llvm::Function *
335 EmitOMPThreadPrivateVarDefinition(const VarDecl *VD, llvm::Value *VDAddr,
336 SourceLocation Loc, bool PerformInit,
337 CodeGenFunction *CGF = nullptr);
Alexey Bataevcc37cc12014-11-20 04:34:54 +0000338
339 /// \brief Emit flush of the variables specified in 'omp flush' directive.
340 /// \param Vars List of variables to flush.
341 virtual void EmitOMPFlush(CodeGenFunction &CGF, ArrayRef<const Expr *> Vars,
342 SourceLocation Loc);
Alexey Bataev9959db52014-05-06 10:08:46 +0000343};
Alexey Bataev23b69422014-06-18 07:08:49 +0000344} // namespace CodeGen
345} // namespace clang
Alexey Bataev9959db52014-05-06 10:08:46 +0000346
347#endif