blob: ec6115fb0948c97533fab98a868310953f3360b3 [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 Bataev4a5bb772014-10-08 14:01:46 +000017#include "CodeGenFunction.h"
18#include "clang/AST/StmtOpenMP.h"
Alexey Bataev9959db52014-05-06 10:08:46 +000019#include "clang/AST/Type.h"
20#include "llvm/ADT/DenseMap.h"
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +000021#include "llvm/ADT/StringMap.h"
Alexey Bataev9959db52014-05-06 10:08:46 +000022#include "llvm/IR/Type.h"
23#include "llvm/IR/Value.h"
24
Alexey Bataev9959db52014-05-06 10:08:46 +000025namespace clang {
26
27namespace CodeGen {
28
Alexey Bataev4a5bb772014-10-08 14:01:46 +000029/// \brief API for captured statement code generation in OpenMP constructs.
30class CGOpenMPRegionInfo : public CodeGenFunction::CGCapturedStmtInfo {
31public:
32 CGOpenMPRegionInfo(const OMPExecutableDirective &D, const CapturedStmt &S,
33 const VarDecl *ThreadIDVar)
34 : CGCapturedStmtInfo(S, CR_OpenMP), ThreadIDVar(ThreadIDVar),
35 Directive(D) {}
36
37 virtual ~CGOpenMPRegionInfo() override{};
38
39 /// \brief Gets a variable or parameter for storing global thread id
40 /// inside OpenMP construct.
41 const VarDecl *getThreadIDVariable() const { return ThreadIDVar; }
42
43 static bool classof(const CGCapturedStmtInfo *Info) {
44 return Info->getKind() == CR_OpenMP;
45 }
46
47 /// \brief Emit the captured statement body.
48 virtual void EmitBody(CodeGenFunction &CGF, Stmt *S) override;
49
50 /// \brief Get the name of the capture helper.
51 virtual StringRef getHelperName() const override { return ".omp_outlined."; }
52
53private:
54 /// \brief A variable or parameter storing global thread id for OpenMP
55 /// constructs.
56 const VarDecl *ThreadIDVar;
57 /// \brief OpenMP executable directive associated with the region.
58 const OMPExecutableDirective &Directive;
59};
Alexey Bataev9959db52014-05-06 10:08:46 +000060
61class CGOpenMPRuntime {
62public:
63 /// \brief Values for bit flags used in the ident_t to describe the fields.
64 /// All enumeric elements are named and described in accordance with the code
65 /// from http://llvm.org/svn/llvm-project/openmp/trunk/runtime/src/kmp.h
66 enum OpenMPLocationFlags {
67 /// \brief Use trampoline for internal microtask.
68 OMP_IDENT_IMD = 0x01,
69 /// \brief Use c-style ident structure.
70 OMP_IDENT_KMPC = 0x02,
71 /// \brief Atomic reduction option for kmpc_reduce.
72 OMP_ATOMIC_REDUCE = 0x10,
73 /// \brief Explicit 'barrier' directive.
74 OMP_IDENT_BARRIER_EXPL = 0x20,
75 /// \brief Implicit barrier in code.
76 OMP_IDENT_BARRIER_IMPL = 0x40,
77 /// \brief Implicit barrier in 'for' directive.
78 OMP_IDENT_BARRIER_IMPL_FOR = 0x40,
79 /// \brief Implicit barrier in 'sections' directive.
80 OMP_IDENT_BARRIER_IMPL_SECTIONS = 0xC0,
81 /// \brief Implicit barrier in 'single' directive.
82 OMP_IDENT_BARRIER_IMPL_SINGLE = 0x140
83 };
84 enum OpenMPRTLFunction {
85 // Call to void __kmpc_fork_call(ident_t *loc, kmp_int32 argc, kmpc_micro
86 // microtask, ...);
87 OMPRTL__kmpc_fork_call,
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +000088 // Call to __kmpc_int32 kmpc_global_thread_num(ident_t *loc);
89 OMPRTL__kmpc_global_thread_num,
Alexey Bataevf9472182014-09-22 12:32:31 +000090 // Call to void __kmpc_critical(ident_t *loc, kmp_int32 global_tid,
91 // kmp_critical_name *crit);
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +000092 OMPRTL__kmpc_critical,
Alexey Bataevf9472182014-09-22 12:32:31 +000093 // Call to void __kmpc_end_critical(ident_t *loc, kmp_int32 global_tid,
94 // kmp_critical_name *crit);
Alexey Bataev4a5bb772014-10-08 14:01:46 +000095 OMPRTL__kmpc_end_critical,
96 // Call to void __kmpc_barrier(ident_t *loc, kmp_int32 global_tid);
97 OMPRTL__kmpc_barrier
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 Bataev15007ba2014-05-07 06:18:01 +0000105 /// \brief Map of flags and corrsponding default locations.
106 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 Bataevf002aca2014-05-30 05:48:40 +0000150 /// \brief Map for Sourcelocation and OpenMP runtime library debug locations.
151 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;
157 /// \brief Map of local debug location and functions.
158 typedef llvm::DenseMap<llvm::Function *, llvm::Value *> OpenMPLocMapTy;
159 OpenMPLocMapTy OpenMPLocMap;
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000160 /// \brief Map of local ThreadID and functions.
161 typedef llvm::DenseMap<llvm::Function *, llvm::Value *> OpenMPThreadIDMapTy;
162 OpenMPThreadIDMapTy OpenMPThreadIDMap;
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +0000163 /// \brief Type kmp_critical_name, originally defined as typedef kmp_int32
164 /// kmp_critical_name[8];
165 llvm::ArrayType *KmpCriticalNameTy;
166 /// \brief Map of critical regions names and the corresponding lock objects.
167 llvm::StringMap<llvm::Value *, llvm::BumpPtrAllocator> CriticalRegionVarNames;
Alexey Bataev9959db52014-05-06 10:08:46 +0000168
Alexey Bataev9959db52014-05-06 10:08:46 +0000169 /// \brief Emits object of ident_t type with info for source location.
170 /// \param CGF Reference to current CodeGenFunction.
171 /// \param Loc Clang source location.
172 /// \param Flags Flags for OpenMP location.
173 ///
174 llvm::Value *
175 EmitOpenMPUpdateLocation(CodeGenFunction &CGF, SourceLocation Loc,
176 OpenMPLocationFlags Flags = OMP_IDENT_KMPC);
177
Alexey Bataev9959db52014-05-06 10:08:46 +0000178 /// \brief Returns pointer to ident_t type;
179 llvm::Type *getIdentTyPointerTy();
180
181 /// \brief Returns pointer to kmpc_micro type;
182 llvm::Type *getKmpc_MicroPointerTy();
183
184 /// \brief Returns specified OpenMP runtime function.
185 /// \param Function OpenMP runtime function.
186 /// \return Specified function.
187 llvm::Constant *CreateRuntimeFunction(OpenMPRTLFunction Function);
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +0000188
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000189 /// \brief Gets thread id value for the current thread.
190 /// \param CGF Reference to current CodeGenFunction.
191 /// \param Loc Clang source location.
192 ///
193 llvm::Value *GetOpenMPThreadID(CodeGenFunction &CGF, SourceLocation Loc);
194
195public:
196 explicit CGOpenMPRuntime(CodeGenModule &CGM);
197 virtual ~CGOpenMPRuntime() {}
198
199 /// \brief Cleans up references to the objects in finished function.
200 /// \param CGF Reference to finished CodeGenFunction.
201 ///
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.
206 /// \param CGF Reference to current CodeGenFunction.
207 /// \param Loc Clang source location.
208 /// \param OutlinedFn Outlined function to be run in parallel threads.
209 /// \param CapturedStruct A pointer to the record with the references to
210 /// variables used in \a OutlinedFn function.
211 ///
212 virtual void EmitOMPParallelCall(CodeGenFunction &CGF, SourceLocation Loc,
213 llvm::Value *OutlinedFn,
214 llvm::Value *CapturedStruct);
215
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +0000216 /// \brief Returns corresponding lock object for the specified critical region
217 /// name. If the lock object does not exist it is created, otherwise the
218 /// reference to the existing copy is returned.
219 llvm::Value *GetCriticalRegionLock(StringRef CriticalName);
220
221 /// \brief Emits start of the critical region by calling void
Alexey Bataevf9472182014-09-22 12:32:31 +0000222 /// __kmpc_critical(ident_t *loc, kmp_int32 global_tid, kmp_critical_name
223 /// * \a RegionLock)
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +0000224 /// \param CGF Reference to current CodeGenFunction.
225 /// \param RegionLock The lock object for critical region.
226 /// \param Loc Location of the construct.
227 virtual void EmitOMPCriticalRegionStart(CodeGenFunction &CGF,
228 llvm::Value *RegionLock,
229 SourceLocation Loc);
230
231 /// \brief Emits end of the critical region by calling void
Alexey Bataevf9472182014-09-22 12:32:31 +0000232 /// __kmpc_end_critical(ident_t *loc, kmp_int32 global_tid, kmp_critical_name
233 /// * \a RegionLock)
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +0000234 /// \param CGF Reference to current CodeGenFunction.
235 /// \param RegionLock The lock object for critical region.
236 /// \param Loc Location of the construct.
237 virtual void EmitOMPCriticalRegionEnd(CodeGenFunction &CGF,
238 llvm::Value *RegionLock,
239 SourceLocation Loc);
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000240
241 /// \brief Emits a barrier for OpenMP threads.
242 /// \param CGF Reference to current CodeGenFunction.
243 /// \param Loc Clang source location.
244 /// \param Flags Flags for the barrier.
245 ///
246 virtual void EmitOMPBarrierCall(CodeGenFunction &CGF, SourceLocation Loc,
247 OpenMPLocationFlags Flags);
Alexey Bataev9959db52014-05-06 10:08:46 +0000248};
Alexey Bataev23b69422014-06-18 07:08:49 +0000249} // namespace CodeGen
250} // namespace clang
Alexey Bataev9959db52014-05-06 10:08:46 +0000251
252#endif