blob: ce822ea7d1b8c25e53ae293b44be7ad28fd32f2f [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);
77 OMPRTL__kmpc_barrier
Alexey Bataev9959db52014-05-06 10:08:46 +000078 };
79
80private:
81 CodeGenModule &CGM;
82 /// \brief Default const ident_t object used for initialization of all other
83 /// ident_t objects.
84 llvm::Constant *DefaultOpenMPPSource;
Alexey Bataev18095712014-10-10 12:19:54 +000085 /// \brief Map of flags and corresponding default locations.
Alexey Bataev15007ba2014-05-07 06:18:01 +000086 typedef llvm::DenseMap<unsigned, llvm::Value *> OpenMPDefaultLocMapTy;
87 OpenMPDefaultLocMapTy OpenMPDefaultLocMap;
Alexey Bataev9959db52014-05-06 10:08:46 +000088 llvm::Value *GetOrCreateDefaultOpenMPLocation(OpenMPLocationFlags Flags);
89 /// \brief Describes ident structure that describes a source location.
90 /// All descriptions are taken from
91 /// http://llvm.org/svn/llvm-project/openmp/trunk/runtime/src/kmp.h
92 /// Original structure:
93 /// typedef struct ident {
94 /// kmp_int32 reserved_1; /**< might be used in Fortran;
95 /// see above */
96 /// kmp_int32 flags; /**< also f.flags; KMP_IDENT_xxx flags;
97 /// KMP_IDENT_KMPC identifies this union
98 /// member */
99 /// kmp_int32 reserved_2; /**< not really used in Fortran any more;
100 /// see above */
101 ///#if USE_ITT_BUILD
102 /// /* but currently used for storing
103 /// region-specific ITT */
104 /// /* contextual information. */
105 ///#endif /* USE_ITT_BUILD */
106 /// kmp_int32 reserved_3; /**< source[4] in Fortran, do not use for
107 /// C++ */
108 /// char const *psource; /**< String describing the source location.
109 /// The string is composed of semi-colon separated
110 // fields which describe the source file,
111 /// the function and a pair of line numbers that
112 /// delimit the construct.
113 /// */
114 /// } ident_t;
115 enum IdentFieldIndex {
116 /// \brief might be used in Fortran
117 IdentField_Reserved_1,
118 /// \brief OMP_IDENT_xxx flags; OMP_IDENT_KMPC identifies this union member.
119 IdentField_Flags,
120 /// \brief Not really used in Fortran any more
121 IdentField_Reserved_2,
122 /// \brief Source[4] in Fortran, do not use for C++
123 IdentField_Reserved_3,
124 /// \brief String describing the source location. The string is composed of
125 /// semi-colon separated fields which describe the source file, the function
126 /// and a pair of line numbers that delimit the construct.
127 IdentField_PSource
128 };
129 llvm::StructType *IdentTy;
Alexey Bataev18095712014-10-10 12:19:54 +0000130 /// \brief Map for SourceLocation and OpenMP runtime library debug locations.
Alexey Bataevf002aca2014-05-30 05:48:40 +0000131 typedef llvm::DenseMap<unsigned, llvm::Value *> OpenMPDebugLocMapTy;
132 OpenMPDebugLocMapTy OpenMPDebugLocMap;
Alexey Bataev9959db52014-05-06 10:08:46 +0000133 /// \brief The type for a microtask which gets passed to __kmpc_fork_call().
134 /// Original representation is:
135 /// typedef void (kmpc_micro)(kmp_int32 global_tid, kmp_int32 bound_tid,...);
136 llvm::FunctionType *Kmpc_MicroTy;
Alexey Bataev18095712014-10-10 12:19:54 +0000137 /// \brief Stores debug location and ThreadID for the function.
138 struct DebugLocThreadIdTy {
139 llvm::Value *DebugLoc;
140 llvm::Value *ThreadID;
141 };
142 /// \brief Map of local debug location, ThreadId and functions.
143 typedef llvm::DenseMap<llvm::Function *, DebugLocThreadIdTy>
144 OpenMPLocThreadIDMapTy;
145 OpenMPLocThreadIDMapTy OpenMPLocThreadIDMap;
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +0000146 /// \brief Type kmp_critical_name, originally defined as typedef kmp_int32
147 /// kmp_critical_name[8];
148 llvm::ArrayType *KmpCriticalNameTy;
149 /// \brief Map of critical regions names and the corresponding lock objects.
150 llvm::StringMap<llvm::Value *, llvm::BumpPtrAllocator> CriticalRegionVarNames;
Alexey Bataev9959db52014-05-06 10:08:46 +0000151
Alexey Bataev9959db52014-05-06 10:08:46 +0000152 /// \brief Emits object of ident_t type with info for source location.
Alexey Bataev9959db52014-05-06 10:08:46 +0000153 /// \param Flags Flags for OpenMP location.
154 ///
155 llvm::Value *
156 EmitOpenMPUpdateLocation(CodeGenFunction &CGF, SourceLocation Loc,
157 OpenMPLocationFlags Flags = OMP_IDENT_KMPC);
158
Alexey Bataev9959db52014-05-06 10:08:46 +0000159 /// \brief Returns pointer to ident_t type;
160 llvm::Type *getIdentTyPointerTy();
161
162 /// \brief Returns pointer to kmpc_micro type;
163 llvm::Type *getKmpc_MicroPointerTy();
164
165 /// \brief Returns specified OpenMP runtime function.
166 /// \param Function OpenMP runtime function.
167 /// \return Specified function.
168 llvm::Constant *CreateRuntimeFunction(OpenMPRTLFunction Function);
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +0000169
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000170 /// \brief Gets thread id value for the current thread.
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000171 ///
172 llvm::Value *GetOpenMPThreadID(CodeGenFunction &CGF, SourceLocation Loc);
173
174public:
175 explicit CGOpenMPRuntime(CodeGenModule &CGM);
176 virtual ~CGOpenMPRuntime() {}
177
Alexey Bataev18095712014-10-10 12:19:54 +0000178 /// \brief Emits outlined function for the specified OpenMP directive \a D
179 /// (required for parallel and task directives). This outlined function has
180 /// type void(*)(kmp_int32 /*ThreadID*/, kmp_int32 /*BoundID*/, struct
181 /// context_vars*).
182 /// \param D OpenMP directive.
183 /// \param ThreadIDVar Variable for thread id in the current OpenMP region.
184 ///
185 virtual llvm::Value *
186 EmitOpenMPOutlinedFunction(const OMPExecutableDirective &D,
187 const VarDecl *ThreadIDVar);
188
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000189 /// \brief Cleans up references to the objects in finished function.
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000190 ///
191 void FunctionFinished(CodeGenFunction &CGF);
192
193 /// \brief Emits code for parallel call of the \a OutlinedFn with variables
194 /// captured in a record which address is stored in \a CapturedStruct.
Alexey Bataev18095712014-10-10 12:19:54 +0000195 /// \param OutlinedFn Outlined function to be run in parallel threads. Type of
196 /// this function is void(*)(kmp_int32, kmp_int32, struct context_vars*).
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000197 /// \param CapturedStruct A pointer to the record with the references to
198 /// variables used in \a OutlinedFn function.
199 ///
200 virtual void EmitOMPParallelCall(CodeGenFunction &CGF, SourceLocation Loc,
201 llvm::Value *OutlinedFn,
202 llvm::Value *CapturedStruct);
203
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +0000204 /// \brief Returns corresponding lock object for the specified critical region
205 /// name. If the lock object does not exist it is created, otherwise the
206 /// reference to the existing copy is returned.
Alexey Bataev18095712014-10-10 12:19:54 +0000207 /// \param CriticalName Name of the critical region.
208 ///
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +0000209 llvm::Value *GetCriticalRegionLock(StringRef CriticalName);
210
211 /// \brief Emits start of the critical region by calling void
Alexey Bataevf9472182014-09-22 12:32:31 +0000212 /// __kmpc_critical(ident_t *loc, kmp_int32 global_tid, kmp_critical_name
213 /// * \a RegionLock)
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +0000214 /// \param RegionLock The lock object for critical region.
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +0000215 virtual void EmitOMPCriticalRegionStart(CodeGenFunction &CGF,
216 llvm::Value *RegionLock,
217 SourceLocation Loc);
218
219 /// \brief Emits end of the critical region by calling void
Alexey Bataevf9472182014-09-22 12:32:31 +0000220 /// __kmpc_end_critical(ident_t *loc, kmp_int32 global_tid, kmp_critical_name
221 /// * \a RegionLock)
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +0000222 /// \param RegionLock The lock object for critical region.
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +0000223 virtual void EmitOMPCriticalRegionEnd(CodeGenFunction &CGF,
224 llvm::Value *RegionLock,
225 SourceLocation Loc);
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000226
227 /// \brief Emits a barrier for OpenMP threads.
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000228 /// \param Flags Flags for the barrier.
229 ///
230 virtual void EmitOMPBarrierCall(CodeGenFunction &CGF, SourceLocation Loc,
231 OpenMPLocationFlags Flags);
Alexey Bataev9959db52014-05-06 10:08:46 +0000232};
Alexey Bataev23b69422014-06-18 07:08:49 +0000233} // namespace CodeGen
234} // namespace clang
Alexey Bataev9959db52014-05-06 10:08:46 +0000235
236#endif