blob: c4da375acf5cd5b0d2b0719ce9204a9bca01553a [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 Bataev3a3bf0b2014-09-22 10:01:53 +000019#include "llvm/ADT/StringMap.h"
Alexey Bataev18095712014-10-10 12:19:54 +000020
21namespace llvm {
22class ArrayType;
23class Constant;
24class Function;
25class FunctionType;
26class StructType;
27class Type;
28class Value;
29} // namespace llvm
Alexey Bataev9959db52014-05-06 10:08:46 +000030
Alexey Bataev9959db52014-05-06 10:08:46 +000031namespace clang {
32
Alexey Bataev18095712014-10-10 12:19:54 +000033class OMPExecutableDirective;
34class VarDecl;
35
Alexey Bataev9959db52014-05-06 10:08:46 +000036namespace CodeGen {
37
Alexey Bataev18095712014-10-10 12:19:54 +000038class CodeGenFunction;
39class CodeGenModule;
Alexey Bataev9959db52014-05-06 10:08:46 +000040
41class CGOpenMPRuntime {
42public:
43 /// \brief Values for bit flags used in the ident_t to describe the fields.
44 /// All enumeric elements are named and described in accordance with the code
45 /// from http://llvm.org/svn/llvm-project/openmp/trunk/runtime/src/kmp.h
46 enum OpenMPLocationFlags {
47 /// \brief Use trampoline for internal microtask.
48 OMP_IDENT_IMD = 0x01,
49 /// \brief Use c-style ident structure.
50 OMP_IDENT_KMPC = 0x02,
51 /// \brief Atomic reduction option for kmpc_reduce.
52 OMP_ATOMIC_REDUCE = 0x10,
53 /// \brief Explicit 'barrier' directive.
54 OMP_IDENT_BARRIER_EXPL = 0x20,
55 /// \brief Implicit barrier in code.
56 OMP_IDENT_BARRIER_IMPL = 0x40,
57 /// \brief Implicit barrier in 'for' directive.
58 OMP_IDENT_BARRIER_IMPL_FOR = 0x40,
59 /// \brief Implicit barrier in 'sections' directive.
60 OMP_IDENT_BARRIER_IMPL_SECTIONS = 0xC0,
61 /// \brief Implicit barrier in 'single' directive.
62 OMP_IDENT_BARRIER_IMPL_SINGLE = 0x140
63 };
64 enum OpenMPRTLFunction {
65 // Call to void __kmpc_fork_call(ident_t *loc, kmp_int32 argc, kmpc_micro
66 // microtask, ...);
67 OMPRTL__kmpc_fork_call,
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +000068 // Call to __kmpc_int32 kmpc_global_thread_num(ident_t *loc);
69 OMPRTL__kmpc_global_thread_num,
Alexey Bataevf9472182014-09-22 12:32:31 +000070 // Call to void __kmpc_critical(ident_t *loc, kmp_int32 global_tid,
71 // kmp_critical_name *crit);
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +000072 OMPRTL__kmpc_critical,
Alexey Bataevf9472182014-09-22 12:32:31 +000073 // Call to void __kmpc_end_critical(ident_t *loc, kmp_int32 global_tid,
74 // kmp_critical_name *crit);
Alexey Bataev4a5bb772014-10-08 14:01:46 +000075 OMPRTL__kmpc_end_critical,
76 // Call to void __kmpc_barrier(ident_t *loc, kmp_int32 global_tid);
Alexey Bataevd74d0602014-10-13 06:02:40 +000077 OMPRTL__kmpc_barrier,
78 // Call to void __kmpc_serialized_parallel(ident_t *loc, kmp_int32
79 // global_tid);
80 OMPRTL__kmpc_serialized_parallel,
81 // Call to void __kmpc_end_serialized_parallel(ident_t *loc, kmp_int32
82 // global_tid);
Alexey Bataevb2059782014-10-13 08:23:51 +000083 OMPRTL__kmpc_end_serialized_parallel,
84 // Call to void __kmpc_push_num_threads(ident_t *loc, kmp_int32 global_tid,
85 // kmp_int32 num_threads);
86 OMPRTL__kmpc_push_num_threads
Alexey Bataev9959db52014-05-06 10:08:46 +000087 };
88
89private:
90 CodeGenModule &CGM;
91 /// \brief Default const ident_t object used for initialization of all other
92 /// ident_t objects.
93 llvm::Constant *DefaultOpenMPPSource;
Alexey Bataev18095712014-10-10 12:19:54 +000094 /// \brief Map of flags and corresponding default locations.
Alexey Bataev15007ba2014-05-07 06:18:01 +000095 typedef llvm::DenseMap<unsigned, llvm::Value *> OpenMPDefaultLocMapTy;
96 OpenMPDefaultLocMapTy OpenMPDefaultLocMap;
Alexey Bataev9959db52014-05-06 10:08:46 +000097 llvm::Value *GetOrCreateDefaultOpenMPLocation(OpenMPLocationFlags Flags);
98 /// \brief Describes ident structure that describes a source location.
99 /// All descriptions are taken from
100 /// http://llvm.org/svn/llvm-project/openmp/trunk/runtime/src/kmp.h
101 /// Original structure:
102 /// typedef struct ident {
103 /// kmp_int32 reserved_1; /**< might be used in Fortran;
104 /// see above */
105 /// kmp_int32 flags; /**< also f.flags; KMP_IDENT_xxx flags;
106 /// KMP_IDENT_KMPC identifies this union
107 /// member */
108 /// kmp_int32 reserved_2; /**< not really used in Fortran any more;
109 /// see above */
110 ///#if USE_ITT_BUILD
111 /// /* but currently used for storing
112 /// region-specific ITT */
113 /// /* contextual information. */
114 ///#endif /* USE_ITT_BUILD */
115 /// kmp_int32 reserved_3; /**< source[4] in Fortran, do not use for
116 /// C++ */
117 /// char const *psource; /**< String describing the source location.
118 /// The string is composed of semi-colon separated
119 // fields which describe the source file,
120 /// the function and a pair of line numbers that
121 /// delimit the construct.
122 /// */
123 /// } ident_t;
124 enum IdentFieldIndex {
125 /// \brief might be used in Fortran
126 IdentField_Reserved_1,
127 /// \brief OMP_IDENT_xxx flags; OMP_IDENT_KMPC identifies this union member.
128 IdentField_Flags,
129 /// \brief Not really used in Fortran any more
130 IdentField_Reserved_2,
131 /// \brief Source[4] in Fortran, do not use for C++
132 IdentField_Reserved_3,
133 /// \brief String describing the source location. The string is composed of
134 /// semi-colon separated fields which describe the source file, the function
135 /// and a pair of line numbers that delimit the construct.
136 IdentField_PSource
137 };
138 llvm::StructType *IdentTy;
Alexey Bataev18095712014-10-10 12:19:54 +0000139 /// \brief Map for SourceLocation and OpenMP runtime library debug locations.
Alexey Bataevf002aca2014-05-30 05:48:40 +0000140 typedef llvm::DenseMap<unsigned, llvm::Value *> OpenMPDebugLocMapTy;
141 OpenMPDebugLocMapTy OpenMPDebugLocMap;
Alexey Bataev9959db52014-05-06 10:08:46 +0000142 /// \brief The type for a microtask which gets passed to __kmpc_fork_call().
143 /// Original representation is:
144 /// typedef void (kmpc_micro)(kmp_int32 global_tid, kmp_int32 bound_tid,...);
145 llvm::FunctionType *Kmpc_MicroTy;
Alexey Bataev18095712014-10-10 12:19:54 +0000146 /// \brief Stores debug location and ThreadID for the function.
147 struct DebugLocThreadIdTy {
148 llvm::Value *DebugLoc;
149 llvm::Value *ThreadID;
150 };
151 /// \brief Map of local debug location, ThreadId and functions.
152 typedef llvm::DenseMap<llvm::Function *, DebugLocThreadIdTy>
153 OpenMPLocThreadIDMapTy;
154 OpenMPLocThreadIDMapTy OpenMPLocThreadIDMap;
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +0000155 /// \brief Type kmp_critical_name, originally defined as typedef kmp_int32
156 /// kmp_critical_name[8];
157 llvm::ArrayType *KmpCriticalNameTy;
158 /// \brief Map of critical regions names and the corresponding lock objects.
159 llvm::StringMap<llvm::Value *, llvm::BumpPtrAllocator> CriticalRegionVarNames;
Alexey Bataev9959db52014-05-06 10:08:46 +0000160
Alexey Bataev9959db52014-05-06 10:08:46 +0000161 /// \brief Emits object of ident_t type with info for source location.
Alexey Bataev9959db52014-05-06 10:08:46 +0000162 /// \param Flags Flags for OpenMP location.
163 ///
164 llvm::Value *
165 EmitOpenMPUpdateLocation(CodeGenFunction &CGF, SourceLocation Loc,
166 OpenMPLocationFlags Flags = OMP_IDENT_KMPC);
167
Alexey Bataevd74d0602014-10-13 06:02:40 +0000168 /// \brief Returns pointer to ident_t type.
Alexey Bataev9959db52014-05-06 10:08:46 +0000169 llvm::Type *getIdentTyPointerTy();
170
Alexey Bataevd74d0602014-10-13 06:02:40 +0000171 /// \brief Returns pointer to kmpc_micro type.
Alexey Bataev9959db52014-05-06 10:08:46 +0000172 llvm::Type *getKmpc_MicroPointerTy();
173
174 /// \brief Returns specified OpenMP runtime function.
175 /// \param Function OpenMP runtime function.
176 /// \return Specified function.
177 llvm::Constant *CreateRuntimeFunction(OpenMPRTLFunction Function);
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +0000178
Alexey Bataevd74d0602014-10-13 06:02:40 +0000179 /// \brief Emits address of the word in a memory where current thread id is
180 /// stored.
181 virtual llvm::Value *EmitThreadIDAddress(CodeGenFunction &CGF,
182 SourceLocation Loc);
183
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000184 /// \brief Gets thread id value for the current thread.
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000185 ///
186 llvm::Value *GetOpenMPThreadID(CodeGenFunction &CGF, SourceLocation Loc);
187
188public:
189 explicit CGOpenMPRuntime(CodeGenModule &CGM);
190 virtual ~CGOpenMPRuntime() {}
191
Alexey Bataev18095712014-10-10 12:19:54 +0000192 /// \brief Emits outlined function for the specified OpenMP directive \a D
193 /// (required for parallel and task directives). This outlined function has
194 /// type void(*)(kmp_int32 /*ThreadID*/, kmp_int32 /*BoundID*/, struct
195 /// context_vars*).
196 /// \param D OpenMP directive.
197 /// \param ThreadIDVar Variable for thread id in the current OpenMP region.
198 ///
199 virtual llvm::Value *
200 EmitOpenMPOutlinedFunction(const OMPExecutableDirective &D,
201 const VarDecl *ThreadIDVar);
202
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000203 /// \brief Cleans up references to the objects in finished function.
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000204 ///
205 void FunctionFinished(CodeGenFunction &CGF);
206
207 /// \brief Emits code for parallel call of the \a OutlinedFn with variables
208 /// captured in a record which address is stored in \a CapturedStruct.
Alexey Bataev18095712014-10-10 12:19:54 +0000209 /// \param OutlinedFn Outlined function to be run in parallel threads. Type of
210 /// this function is void(*)(kmp_int32, kmp_int32, struct context_vars*).
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000211 /// \param CapturedStruct A pointer to the record with the references to
212 /// variables used in \a OutlinedFn function.
213 ///
214 virtual void EmitOMPParallelCall(CodeGenFunction &CGF, SourceLocation Loc,
215 llvm::Value *OutlinedFn,
216 llvm::Value *CapturedStruct);
217
Alexey Bataevd74d0602014-10-13 06:02:40 +0000218 /// \brief Emits code for serial call of the \a OutlinedFn with variables
219 /// captured in a record which address is stored in \a CapturedStruct.
220 /// \param OutlinedFn Outlined function to be run in serial mode.
221 /// \param CapturedStruct A pointer to the record with the references to
222 /// variables used in \a OutlinedFn function.
223 ///
224 virtual void EmitOMPSerialCall(CodeGenFunction &CGF, SourceLocation Loc,
225 llvm::Value *OutlinedFn,
226 llvm::Value *CapturedStruct);
227
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +0000228 /// \brief Returns corresponding lock object for the specified critical region
229 /// name. If the lock object does not exist it is created, otherwise the
230 /// reference to the existing copy is returned.
Alexey Bataev18095712014-10-10 12:19:54 +0000231 /// \param CriticalName Name of the critical region.
232 ///
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +0000233 llvm::Value *GetCriticalRegionLock(StringRef CriticalName);
234
235 /// \brief Emits start of the critical region by calling void
Alexey Bataevf9472182014-09-22 12:32:31 +0000236 /// __kmpc_critical(ident_t *loc, kmp_int32 global_tid, kmp_critical_name
237 /// * \a RegionLock)
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +0000238 /// \param RegionLock The lock object for critical region.
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +0000239 virtual void EmitOMPCriticalRegionStart(CodeGenFunction &CGF,
240 llvm::Value *RegionLock,
241 SourceLocation Loc);
242
243 /// \brief Emits end of the critical region by calling void
Alexey Bataevf9472182014-09-22 12:32:31 +0000244 /// __kmpc_end_critical(ident_t *loc, kmp_int32 global_tid, kmp_critical_name
245 /// * \a RegionLock)
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +0000246 /// \param RegionLock The lock object for critical region.
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +0000247 virtual void EmitOMPCriticalRegionEnd(CodeGenFunction &CGF,
248 llvm::Value *RegionLock,
249 SourceLocation Loc);
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000250
251 /// \brief Emits a barrier for OpenMP threads.
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000252 /// \param Flags Flags for the barrier.
253 ///
254 virtual void EmitOMPBarrierCall(CodeGenFunction &CGF, SourceLocation Loc,
255 OpenMPLocationFlags Flags);
Alexey Bataevb2059782014-10-13 08:23:51 +0000256
257 /// \brief Emits call to void __kmpc_push_num_threads(ident_t *loc, kmp_int32
258 /// global_tid, kmp_int32 num_threads) to generate code for 'num_threads'
259 /// clause.
260 /// \param NumThreads An integer value of threads.
261 virtual void EmitOMPNumThreadsClause(CodeGenFunction &CGF,
262 llvm::Value *NumThreads,
263 SourceLocation Loc);
Alexey Bataev9959db52014-05-06 10:08:46 +0000264};
Alexey Bataev23b69422014-06-18 07:08:49 +0000265} // namespace CodeGen
266} // namespace clang
Alexey Bataev9959db52014-05-06 10:08:46 +0000267
268#endif