blob: 04378821d7a6caa5a60de11a9b6f4032bcaf5422 [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);
83 OMPRTL__kmpc_end_serialized_parallel
Alexey Bataev9959db52014-05-06 10:08:46 +000084 };
85
86private:
87 CodeGenModule &CGM;
88 /// \brief Default const ident_t object used for initialization of all other
89 /// ident_t objects.
90 llvm::Constant *DefaultOpenMPPSource;
Alexey Bataev18095712014-10-10 12:19:54 +000091 /// \brief Map of flags and corresponding default locations.
Alexey Bataev15007ba2014-05-07 06:18:01 +000092 typedef llvm::DenseMap<unsigned, llvm::Value *> OpenMPDefaultLocMapTy;
93 OpenMPDefaultLocMapTy OpenMPDefaultLocMap;
Alexey Bataev9959db52014-05-06 10:08:46 +000094 llvm::Value *GetOrCreateDefaultOpenMPLocation(OpenMPLocationFlags Flags);
95 /// \brief Describes ident structure that describes a source location.
96 /// All descriptions are taken from
97 /// http://llvm.org/svn/llvm-project/openmp/trunk/runtime/src/kmp.h
98 /// Original structure:
99 /// typedef struct ident {
100 /// kmp_int32 reserved_1; /**< might be used in Fortran;
101 /// see above */
102 /// kmp_int32 flags; /**< also f.flags; KMP_IDENT_xxx flags;
103 /// KMP_IDENT_KMPC identifies this union
104 /// member */
105 /// kmp_int32 reserved_2; /**< not really used in Fortran any more;
106 /// see above */
107 ///#if USE_ITT_BUILD
108 /// /* but currently used for storing
109 /// region-specific ITT */
110 /// /* contextual information. */
111 ///#endif /* USE_ITT_BUILD */
112 /// kmp_int32 reserved_3; /**< source[4] in Fortran, do not use for
113 /// C++ */
114 /// char const *psource; /**< String describing the source location.
115 /// The string is composed of semi-colon separated
116 // fields which describe the source file,
117 /// the function and a pair of line numbers that
118 /// delimit the construct.
119 /// */
120 /// } ident_t;
121 enum IdentFieldIndex {
122 /// \brief might be used in Fortran
123 IdentField_Reserved_1,
124 /// \brief OMP_IDENT_xxx flags; OMP_IDENT_KMPC identifies this union member.
125 IdentField_Flags,
126 /// \brief Not really used in Fortran any more
127 IdentField_Reserved_2,
128 /// \brief Source[4] in Fortran, do not use for C++
129 IdentField_Reserved_3,
130 /// \brief String describing the source location. The string is composed of
131 /// semi-colon separated fields which describe the source file, the function
132 /// and a pair of line numbers that delimit the construct.
133 IdentField_PSource
134 };
135 llvm::StructType *IdentTy;
Alexey Bataev18095712014-10-10 12:19:54 +0000136 /// \brief Map for SourceLocation and OpenMP runtime library debug locations.
Alexey Bataevf002aca2014-05-30 05:48:40 +0000137 typedef llvm::DenseMap<unsigned, llvm::Value *> OpenMPDebugLocMapTy;
138 OpenMPDebugLocMapTy OpenMPDebugLocMap;
Alexey Bataev9959db52014-05-06 10:08:46 +0000139 /// \brief The type for a microtask which gets passed to __kmpc_fork_call().
140 /// Original representation is:
141 /// typedef void (kmpc_micro)(kmp_int32 global_tid, kmp_int32 bound_tid,...);
142 llvm::FunctionType *Kmpc_MicroTy;
Alexey Bataev18095712014-10-10 12:19:54 +0000143 /// \brief Stores debug location and ThreadID for the function.
144 struct DebugLocThreadIdTy {
145 llvm::Value *DebugLoc;
146 llvm::Value *ThreadID;
147 };
148 /// \brief Map of local debug location, ThreadId and functions.
149 typedef llvm::DenseMap<llvm::Function *, DebugLocThreadIdTy>
150 OpenMPLocThreadIDMapTy;
151 OpenMPLocThreadIDMapTy OpenMPLocThreadIDMap;
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +0000152 /// \brief Type kmp_critical_name, originally defined as typedef kmp_int32
153 /// kmp_critical_name[8];
154 llvm::ArrayType *KmpCriticalNameTy;
155 /// \brief Map of critical regions names and the corresponding lock objects.
156 llvm::StringMap<llvm::Value *, llvm::BumpPtrAllocator> CriticalRegionVarNames;
Alexey Bataev9959db52014-05-06 10:08:46 +0000157
Alexey Bataev9959db52014-05-06 10:08:46 +0000158 /// \brief Emits object of ident_t type with info for source location.
Alexey Bataev9959db52014-05-06 10:08:46 +0000159 /// \param Flags Flags for OpenMP location.
160 ///
161 llvm::Value *
162 EmitOpenMPUpdateLocation(CodeGenFunction &CGF, SourceLocation Loc,
163 OpenMPLocationFlags Flags = OMP_IDENT_KMPC);
164
Alexey Bataevd74d0602014-10-13 06:02:40 +0000165 /// \brief Returns pointer to ident_t type.
Alexey Bataev9959db52014-05-06 10:08:46 +0000166 llvm::Type *getIdentTyPointerTy();
167
Alexey Bataevd74d0602014-10-13 06:02:40 +0000168 /// \brief Returns pointer to kmpc_micro type.
Alexey Bataev9959db52014-05-06 10:08:46 +0000169 llvm::Type *getKmpc_MicroPointerTy();
170
171 /// \brief Returns specified OpenMP runtime function.
172 /// \param Function OpenMP runtime function.
173 /// \return Specified function.
174 llvm::Constant *CreateRuntimeFunction(OpenMPRTLFunction Function);
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +0000175
Alexey Bataevd74d0602014-10-13 06:02:40 +0000176 /// \brief Emits address of the word in a memory where current thread id is
177 /// stored.
178 virtual llvm::Value *EmitThreadIDAddress(CodeGenFunction &CGF,
179 SourceLocation Loc);
180
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000181 /// \brief Gets thread id value for the current thread.
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000182 ///
183 llvm::Value *GetOpenMPThreadID(CodeGenFunction &CGF, SourceLocation Loc);
184
185public:
186 explicit CGOpenMPRuntime(CodeGenModule &CGM);
187 virtual ~CGOpenMPRuntime() {}
188
Alexey Bataev18095712014-10-10 12:19:54 +0000189 /// \brief Emits outlined function for the specified OpenMP directive \a D
190 /// (required for parallel and task directives). This outlined function has
191 /// type void(*)(kmp_int32 /*ThreadID*/, kmp_int32 /*BoundID*/, struct
192 /// context_vars*).
193 /// \param D OpenMP directive.
194 /// \param ThreadIDVar Variable for thread id in the current OpenMP region.
195 ///
196 virtual llvm::Value *
197 EmitOpenMPOutlinedFunction(const OMPExecutableDirective &D,
198 const VarDecl *ThreadIDVar);
199
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000200 /// \brief Cleans up references to the objects in finished function.
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000201 ///
202 void FunctionFinished(CodeGenFunction &CGF);
203
204 /// \brief Emits code for parallel call of the \a OutlinedFn with variables
205 /// captured in a record which address is stored in \a CapturedStruct.
Alexey Bataev18095712014-10-10 12:19:54 +0000206 /// \param OutlinedFn Outlined function to be run in parallel threads. Type of
207 /// this function is void(*)(kmp_int32, kmp_int32, struct context_vars*).
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000208 /// \param CapturedStruct A pointer to the record with the references to
209 /// variables used in \a OutlinedFn function.
210 ///
211 virtual void EmitOMPParallelCall(CodeGenFunction &CGF, SourceLocation Loc,
212 llvm::Value *OutlinedFn,
213 llvm::Value *CapturedStruct);
214
Alexey Bataevd74d0602014-10-13 06:02:40 +0000215 /// \brief Emits code for serial call of the \a OutlinedFn with variables
216 /// captured in a record which address is stored in \a CapturedStruct.
217 /// \param OutlinedFn Outlined function to be run in serial mode.
218 /// \param CapturedStruct A pointer to the record with the references to
219 /// variables used in \a OutlinedFn function.
220 ///
221 virtual void EmitOMPSerialCall(CodeGenFunction &CGF, SourceLocation Loc,
222 llvm::Value *OutlinedFn,
223 llvm::Value *CapturedStruct);
224
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +0000225 /// \brief Returns corresponding lock object for the specified critical region
226 /// name. If the lock object does not exist it is created, otherwise the
227 /// reference to the existing copy is returned.
Alexey Bataev18095712014-10-10 12:19:54 +0000228 /// \param CriticalName Name of the critical region.
229 ///
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +0000230 llvm::Value *GetCriticalRegionLock(StringRef CriticalName);
231
232 /// \brief Emits start of the critical region by calling void
Alexey Bataevf9472182014-09-22 12:32:31 +0000233 /// __kmpc_critical(ident_t *loc, kmp_int32 global_tid, kmp_critical_name
234 /// * \a RegionLock)
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +0000235 /// \param RegionLock The lock object for critical region.
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +0000236 virtual void EmitOMPCriticalRegionStart(CodeGenFunction &CGF,
237 llvm::Value *RegionLock,
238 SourceLocation Loc);
239
240 /// \brief Emits end of the critical region by calling void
Alexey Bataevf9472182014-09-22 12:32:31 +0000241 /// __kmpc_end_critical(ident_t *loc, kmp_int32 global_tid, kmp_critical_name
242 /// * \a RegionLock)
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +0000243 /// \param RegionLock The lock object for critical region.
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +0000244 virtual void EmitOMPCriticalRegionEnd(CodeGenFunction &CGF,
245 llvm::Value *RegionLock,
246 SourceLocation Loc);
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000247
248 /// \brief Emits a barrier for OpenMP threads.
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000249 /// \param Flags Flags for the barrier.
250 ///
251 virtual void EmitOMPBarrierCall(CodeGenFunction &CGF, SourceLocation Loc,
252 OpenMPLocationFlags Flags);
Alexey Bataev9959db52014-05-06 10:08:46 +0000253};
Alexey Bataev23b69422014-06-18 07:08:49 +0000254} // namespace CodeGen
255} // namespace clang
Alexey Bataev9959db52014-05-06 10:08:46 +0000256
257#endif