blob: d32c43a8b03b8dff22e66f194d551c8ef6ee6b71 [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
17#include "clang/AST/Type.h"
18#include "llvm/ADT/DenseMap.h"
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +000019#include "llvm/ADT/StringMap.h"
Alexey Bataev9959db52014-05-06 10:08:46 +000020#include "llvm/IR/Type.h"
21#include "llvm/IR/Value.h"
22
23namespace llvm {
24class AllocaInst;
25class CallInst;
26class GlobalVariable;
27class Constant;
28class Function;
29class Module;
30class StructLayout;
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +000031class ArrayType;
Alexey Bataev9959db52014-05-06 10:08:46 +000032class FunctionType;
33class StructType;
34class Type;
35class Value;
Alexey Bataev23b69422014-06-18 07:08:49 +000036} // namespace llvm
Alexey Bataev9959db52014-05-06 10:08:46 +000037
38namespace clang {
39
40namespace CodeGen {
41
42class CodeGenFunction;
43class CodeGenModule;
44
45class CGOpenMPRuntime {
46public:
47 /// \brief Values for bit flags used in the ident_t to describe the fields.
48 /// All enumeric elements are named and described in accordance with the code
49 /// from http://llvm.org/svn/llvm-project/openmp/trunk/runtime/src/kmp.h
50 enum OpenMPLocationFlags {
51 /// \brief Use trampoline for internal microtask.
52 OMP_IDENT_IMD = 0x01,
53 /// \brief Use c-style ident structure.
54 OMP_IDENT_KMPC = 0x02,
55 /// \brief Atomic reduction option for kmpc_reduce.
56 OMP_ATOMIC_REDUCE = 0x10,
57 /// \brief Explicit 'barrier' directive.
58 OMP_IDENT_BARRIER_EXPL = 0x20,
59 /// \brief Implicit barrier in code.
60 OMP_IDENT_BARRIER_IMPL = 0x40,
61 /// \brief Implicit barrier in 'for' directive.
62 OMP_IDENT_BARRIER_IMPL_FOR = 0x40,
63 /// \brief Implicit barrier in 'sections' directive.
64 OMP_IDENT_BARRIER_IMPL_SECTIONS = 0xC0,
65 /// \brief Implicit barrier in 'single' directive.
66 OMP_IDENT_BARRIER_IMPL_SINGLE = 0x140
67 };
68 enum OpenMPRTLFunction {
69 // Call to void __kmpc_fork_call(ident_t *loc, kmp_int32 argc, kmpc_micro
70 // microtask, ...);
71 OMPRTL__kmpc_fork_call,
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +000072 // Call to __kmpc_int32 kmpc_global_thread_num(ident_t *loc);
73 OMPRTL__kmpc_global_thread_num,
74 // Call to void __kmpc_critical(ident_t ∗loc, kmp_int32 global_tid,
75 // kmp_critical_name ∗crit);
76 OMPRTL__kmpc_critical,
77 // Call to void __kmpc_end_critical(ident_t ∗loc, kmp_int32 global_tid,
78 // kmp_critical_name ∗crit);
79 OMPRTL__kmpc_end_critical
Alexey Bataev9959db52014-05-06 10:08:46 +000080 };
81
82private:
83 CodeGenModule &CGM;
84 /// \brief Default const ident_t object used for initialization of all other
85 /// ident_t objects.
86 llvm::Constant *DefaultOpenMPPSource;
Alexey Bataev15007ba2014-05-07 06:18:01 +000087 /// \brief Map of flags and corrsponding default locations.
88 typedef llvm::DenseMap<unsigned, llvm::Value *> OpenMPDefaultLocMapTy;
89 OpenMPDefaultLocMapTy OpenMPDefaultLocMap;
Alexey Bataev9959db52014-05-06 10:08:46 +000090 llvm::Value *GetOrCreateDefaultOpenMPLocation(OpenMPLocationFlags Flags);
91 /// \brief Describes ident structure that describes a source location.
92 /// All descriptions are taken from
93 /// http://llvm.org/svn/llvm-project/openmp/trunk/runtime/src/kmp.h
94 /// Original structure:
95 /// typedef struct ident {
96 /// kmp_int32 reserved_1; /**< might be used in Fortran;
97 /// see above */
98 /// kmp_int32 flags; /**< also f.flags; KMP_IDENT_xxx flags;
99 /// KMP_IDENT_KMPC identifies this union
100 /// member */
101 /// kmp_int32 reserved_2; /**< not really used in Fortran any more;
102 /// see above */
103 ///#if USE_ITT_BUILD
104 /// /* but currently used for storing
105 /// region-specific ITT */
106 /// /* contextual information. */
107 ///#endif /* USE_ITT_BUILD */
108 /// kmp_int32 reserved_3; /**< source[4] in Fortran, do not use for
109 /// C++ */
110 /// char const *psource; /**< String describing the source location.
111 /// The string is composed of semi-colon separated
112 // fields which describe the source file,
113 /// the function and a pair of line numbers that
114 /// delimit the construct.
115 /// */
116 /// } ident_t;
117 enum IdentFieldIndex {
118 /// \brief might be used in Fortran
119 IdentField_Reserved_1,
120 /// \brief OMP_IDENT_xxx flags; OMP_IDENT_KMPC identifies this union member.
121 IdentField_Flags,
122 /// \brief Not really used in Fortran any more
123 IdentField_Reserved_2,
124 /// \brief Source[4] in Fortran, do not use for C++
125 IdentField_Reserved_3,
126 /// \brief String describing the source location. The string is composed of
127 /// semi-colon separated fields which describe the source file, the function
128 /// and a pair of line numbers that delimit the construct.
129 IdentField_PSource
130 };
131 llvm::StructType *IdentTy;
Alexey Bataevf002aca2014-05-30 05:48:40 +0000132 /// \brief Map for Sourcelocation and OpenMP runtime library debug locations.
133 typedef llvm::DenseMap<unsigned, llvm::Value *> OpenMPDebugLocMapTy;
134 OpenMPDebugLocMapTy OpenMPDebugLocMap;
Alexey Bataev9959db52014-05-06 10:08:46 +0000135 /// \brief The type for a microtask which gets passed to __kmpc_fork_call().
136 /// Original representation is:
137 /// typedef void (kmpc_micro)(kmp_int32 global_tid, kmp_int32 bound_tid,...);
138 llvm::FunctionType *Kmpc_MicroTy;
139 /// \brief Map of local debug location and functions.
140 typedef llvm::DenseMap<llvm::Function *, llvm::Value *> OpenMPLocMapTy;
141 OpenMPLocMapTy OpenMPLocMap;
142 /// \brief Map of local gtid and functions.
143 typedef llvm::DenseMap<llvm::Function *, llvm::Value *> OpenMPGtidMapTy;
144 OpenMPGtidMapTy OpenMPGtidMap;
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +0000145 /// \brief Type kmp_critical_name, originally defined as typedef kmp_int32
146 /// kmp_critical_name[8];
147 llvm::ArrayType *KmpCriticalNameTy;
148 /// \brief Map of critical regions names and the corresponding lock objects.
149 llvm::StringMap<llvm::Value *, llvm::BumpPtrAllocator> CriticalRegionVarNames;
Alexey Bataev9959db52014-05-06 10:08:46 +0000150
151public:
Alexey Bataev23b69422014-06-18 07:08:49 +0000152 explicit CGOpenMPRuntime(CodeGenModule &CGM);
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +0000153 virtual ~CGOpenMPRuntime() {}
Alexey Bataev9959db52014-05-06 10:08:46 +0000154
155 /// \brief Cleans up references to the objects in finished function.
156 /// \param CGF Reference to finished CodeGenFunction.
157 ///
158 void FunctionFinished(CodeGenFunction &CGF);
159
160 /// \brief Emits object of ident_t type with info for source location.
161 /// \param CGF Reference to current CodeGenFunction.
162 /// \param Loc Clang source location.
163 /// \param Flags Flags for OpenMP location.
164 ///
165 llvm::Value *
166 EmitOpenMPUpdateLocation(CodeGenFunction &CGF, SourceLocation Loc,
167 OpenMPLocationFlags Flags = OMP_IDENT_KMPC);
168
169 /// \brief Generates global thread number value.
170 /// \param CGF Reference to current CodeGenFunction.
171 /// \param Loc Clang source location.
172 ///
173 llvm::Value *GetOpenMPGlobalThreadNum(CodeGenFunction &CGF,
174 SourceLocation Loc);
175
176 /// \brief Returns pointer to ident_t type;
177 llvm::Type *getIdentTyPointerTy();
178
179 /// \brief Returns pointer to kmpc_micro type;
180 llvm::Type *getKmpc_MicroPointerTy();
181
182 /// \brief Returns specified OpenMP runtime function.
183 /// \param Function OpenMP runtime function.
184 /// \return Specified function.
185 llvm::Constant *CreateRuntimeFunction(OpenMPRTLFunction Function);
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +0000186
187 /// \brief Returns corresponding lock object for the specified critical region
188 /// name. If the lock object does not exist it is created, otherwise the
189 /// reference to the existing copy is returned.
190 llvm::Value *GetCriticalRegionLock(StringRef CriticalName);
191
192 /// \brief Emits start of the critical region by calling void
193 /// __kmpc_critical(ident_t ∗loc, kmp_int32 global_tid, kmp_critical_name
194 /// ∗\a RegionLock)
195 /// \param CGF Reference to current CodeGenFunction.
196 /// \param RegionLock The lock object for critical region.
197 /// \param Loc Location of the construct.
198 virtual void EmitOMPCriticalRegionStart(CodeGenFunction &CGF,
199 llvm::Value *RegionLock,
200 SourceLocation Loc);
201
202 /// \brief Emits end of the critical region by calling void
203 /// __kmpc_end_critical(ident_t ∗loc, kmp_int32 global_tid, kmp_critical_name
204 /// ∗\a RegionLock)
205 /// \param CGF Reference to current CodeGenFunction.
206 /// \param RegionLock The lock object for critical region.
207 /// \param Loc Location of the construct.
208 virtual void EmitOMPCriticalRegionEnd(CodeGenFunction &CGF,
209 llvm::Value *RegionLock,
210 SourceLocation Loc);
Alexey Bataev9959db52014-05-06 10:08:46 +0000211};
Alexey Bataev23b69422014-06-18 07:08:49 +0000212} // namespace CodeGen
213} // namespace clang
Alexey Bataev9959db52014-05-06 10:08:46 +0000214
215#endif