blob: 1935e93e20633ef5219cc5347b8903a6aa94bac2 [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 Bataev62b63b12015-03-10 07:28:44 +000017#include "clang/AST/Type.h"
Alexander Musmanc6388682014-12-15 07:07:06 +000018#include "clang/Basic/OpenMPKinds.h"
Chandler Carruth0d9593d2015-01-14 11:29:14 +000019#include "clang/Basic/SourceLocation.h"
Alexey Bataev9959db52014-05-06 10:08:46 +000020#include "llvm/ADT/DenseMap.h"
Benjamin Kramer8fdba912016-02-02 14:24:21 +000021#include "llvm/ADT/SmallPtrSet.h"
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +000022#include "llvm/ADT/StringMap.h"
Benjamin Kramer8fdba912016-02-02 14:24:21 +000023#include "llvm/IR/Function.h"
Alexey Bataev97720002014-11-11 04:05:39 +000024#include "llvm/IR/ValueHandle.h"
Alexey Bataev18095712014-10-10 12:19:54 +000025
26namespace llvm {
27class ArrayType;
28class Constant;
Alexey Bataev18095712014-10-10 12:19:54 +000029class FunctionType;
Alexey Bataev97720002014-11-11 04:05:39 +000030class GlobalVariable;
Alexey Bataev18095712014-10-10 12:19:54 +000031class StructType;
32class Type;
33class Value;
34} // namespace llvm
Alexey Bataev9959db52014-05-06 10:08:46 +000035
Alexey Bataev9959db52014-05-06 10:08:46 +000036namespace clang {
Alexey Bataevcc37cc12014-11-20 04:34:54 +000037class Expr;
Samuel Antaoee8fb302016-01-06 13:42:12 +000038class GlobalDecl;
Alexey Bataev18095712014-10-10 12:19:54 +000039class OMPExecutableDirective;
40class VarDecl;
Alexey Bataevc5b1d322016-03-04 09:22:22 +000041class OMPDeclareReductionDecl;
42class IdentifierInfo;
Alexey Bataev18095712014-10-10 12:19:54 +000043
Alexey Bataev9959db52014-05-06 10:08:46 +000044namespace CodeGen {
John McCall7f416cc2015-09-08 08:05:57 +000045class Address;
Alexey Bataev18095712014-10-10 12:19:54 +000046class CodeGenFunction;
47class CodeGenModule;
Alexey Bataev9959db52014-05-06 10:08:46 +000048
Alexey Bataevf539faa2016-03-28 12:58:34 +000049typedef llvm::function_ref<void(CodeGenFunction &)> RegionCodeGenTy;
Alexey Bataev6f1ffc02015-04-10 04:50:10 +000050
Alexey Bataev9959db52014-05-06 10:08:46 +000051class CGOpenMPRuntime {
Arpith Chacko Jacob5c309e42016-03-22 01:48:56 +000052protected:
Alexey Bataev9959db52014-05-06 10:08:46 +000053 CodeGenModule &CGM;
Arpith Chacko Jacob5c309e42016-03-22 01:48:56 +000054
55 /// \brief Creates offloading entry for the provided entry ID \a ID,
56 /// address \a Addr and size \a Size.
57 virtual void createOffloadEntry(llvm::Constant *ID, llvm::Constant *Addr,
58 uint64_t Size);
59
60 /// \brief Helper to emit outlined function for 'target' directive.
61 /// \param D Directive to emit.
62 /// \param ParentName Name of the function that encloses the target region.
63 /// \param OutlinedFn Outlined function value to be defined by this call.
64 /// \param OutlinedFnID Outlined function ID value to be defined by this call.
65 /// \param IsOffloadEntry True if the outlined function is an offload entry.
66 /// \param CodeGen Lambda codegen specific to an accelerator device.
67 /// An oulined function may not be an entry if, e.g. the if clause always
68 /// evaluates to false.
69 virtual void emitTargetOutlinedFunctionHelper(const OMPExecutableDirective &D,
70 StringRef ParentName,
71 llvm::Function *&OutlinedFn,
72 llvm::Constant *&OutlinedFnID,
73 bool IsOffloadEntry,
74 const RegionCodeGenTy &CodeGen);
75
76private:
Alexey Bataev9959db52014-05-06 10:08:46 +000077 /// \brief Default const ident_t object used for initialization of all other
78 /// ident_t objects.
Alexey Bataevc5b1d322016-03-04 09:22:22 +000079 llvm::Constant *DefaultOpenMPPSource = nullptr;
Alexey Bataev18095712014-10-10 12:19:54 +000080 /// \brief Map of flags and corresponding default locations.
Alexey Bataev15007ba2014-05-07 06:18:01 +000081 typedef llvm::DenseMap<unsigned, llvm::Value *> OpenMPDefaultLocMapTy;
82 OpenMPDefaultLocMapTy OpenMPDefaultLocMap;
Alexey Bataev50b3c952016-02-19 10:38:26 +000083 Address getOrCreateDefaultLocation(unsigned Flags);
John McCall7f416cc2015-09-08 08:05:57 +000084
Alexey Bataevf539faa2016-03-28 12:58:34 +000085 llvm::StructType *IdentTy;
Alexey Bataev18095712014-10-10 12:19:54 +000086 /// \brief Map for SourceLocation and OpenMP runtime library debug locations.
Alexey Bataevf002aca2014-05-30 05:48:40 +000087 typedef llvm::DenseMap<unsigned, llvm::Value *> OpenMPDebugLocMapTy;
88 OpenMPDebugLocMapTy OpenMPDebugLocMap;
Alexey Bataev9959db52014-05-06 10:08:46 +000089 /// \brief The type for a microtask which gets passed to __kmpc_fork_call().
90 /// Original representation is:
91 /// typedef void (kmpc_micro)(kmp_int32 global_tid, kmp_int32 bound_tid,...);
Alexey Bataevf539faa2016-03-28 12:58:34 +000092 llvm::FunctionType *Kmpc_MicroTy;
Alexey Bataev18095712014-10-10 12:19:54 +000093 /// \brief Stores debug location and ThreadID for the function.
94 struct DebugLocThreadIdTy {
95 llvm::Value *DebugLoc;
96 llvm::Value *ThreadID;
97 };
98 /// \brief Map of local debug location, ThreadId and functions.
99 typedef llvm::DenseMap<llvm::Function *, DebugLocThreadIdTy>
100 OpenMPLocThreadIDMapTy;
101 OpenMPLocThreadIDMapTy OpenMPLocThreadIDMap;
Alexey Bataevc5b1d322016-03-04 09:22:22 +0000102 /// Map of UDRs and corresponding combiner/initializer.
103 typedef llvm::DenseMap<const OMPDeclareReductionDecl *,
104 std::pair<llvm::Function *, llvm::Function *>>
105 UDRMapTy;
106 UDRMapTy UDRMap;
107 /// Map of functions and locally defined UDRs.
108 typedef llvm::DenseMap<llvm::Function *,
109 SmallVector<const OMPDeclareReductionDecl *, 4>>
110 FunctionUDRMapTy;
111 FunctionUDRMapTy FunctionUDRMap;
112 IdentifierInfo *In = nullptr;
113 IdentifierInfo *Out = nullptr;
114 IdentifierInfo *Priv = nullptr;
115 IdentifierInfo *Orig = nullptr;
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +0000116 /// \brief Type kmp_critical_name, originally defined as typedef kmp_int32
117 /// kmp_critical_name[8];
118 llvm::ArrayType *KmpCriticalNameTy;
Alexey Bataev97720002014-11-11 04:05:39 +0000119 /// \brief An ordered map of auto-generated variables to their unique names.
120 /// It stores variables with the following names: 1) ".gomp_critical_user_" +
121 /// <critical_section_name> + ".var" for "omp critical" directives; 2)
122 /// <mangled_name_for_global_var> + ".cache." for cache for threadprivate
123 /// variables.
124 llvm::StringMap<llvm::AssertingVH<llvm::Constant>, llvm::BumpPtrAllocator>
125 InternalVars;
Alexey Bataev62b63b12015-03-10 07:28:44 +0000126 /// \brief Type typedef kmp_int32 (* kmp_routine_entry_t)(kmp_int32, void *);
Alexey Bataevc5b1d322016-03-04 09:22:22 +0000127 llvm::Type *KmpRoutineEntryPtrTy = nullptr;
Alexey Bataev62b63b12015-03-10 07:28:44 +0000128 QualType KmpRoutineEntryPtrQTy;
Alexey Bataev8fc69dc2015-05-18 07:54:53 +0000129 /// \brief Type typedef struct kmp_task {
130 /// void * shareds; /**< pointer to block of pointers to
131 /// shared vars */
132 /// kmp_routine_entry_t routine; /**< pointer to routine to call for
133 /// executing task */
134 /// kmp_int32 part_id; /**< part id for the task */
135 /// kmp_routine_entry_t destructors; /* pointer to function to invoke
136 /// deconstructors of firstprivate C++ objects */
137 /// } kmp_task_t;
138 QualType KmpTaskTQTy;
Alexey Bataev1d2353d2015-06-24 11:01:36 +0000139 /// \brief Type typedef struct kmp_depend_info {
140 /// kmp_intptr_t base_addr;
141 /// size_t len;
142 /// struct {
143 /// bool in:1;
144 /// bool out:1;
145 /// } flags;
146 /// } kmp_depend_info_t;
147 QualType KmpDependInfoTy;
Samuel Antaoee8fb302016-01-06 13:42:12 +0000148 /// \brief Type struct __tgt_offload_entry{
149 /// void *addr; // Pointer to the offload entry info.
150 /// // (function or global)
151 /// char *name; // Name of the function or global.
152 /// size_t size; // Size of the entry info (0 if it a function).
153 /// };
154 QualType TgtOffloadEntryQTy;
155 /// struct __tgt_device_image{
156 /// void *ImageStart; // Pointer to the target code start.
157 /// void *ImageEnd; // Pointer to the target code end.
158 /// // We also add the host entries to the device image, as it may be useful
159 /// // for the target runtime to have access to that information.
160 /// __tgt_offload_entry *EntriesBegin; // Begin of the table with all
161 /// // the entries.
162 /// __tgt_offload_entry *EntriesEnd; // End of the table with all the
163 /// // entries (non inclusive).
164 /// };
165 QualType TgtDeviceImageQTy;
166 /// struct __tgt_bin_desc{
167 /// int32_t NumDevices; // Number of devices supported.
168 /// __tgt_device_image *DeviceImages; // Arrays of device images
169 /// // (one per device).
170 /// __tgt_offload_entry *EntriesBegin; // Begin of the table with all the
171 /// // entries.
172 /// __tgt_offload_entry *EntriesEnd; // End of the table with all the
173 /// // entries (non inclusive).
174 /// };
175 QualType TgtBinaryDescriptorQTy;
176 /// \brief Entity that registers the offloading constants that were emitted so
177 /// far.
178 class OffloadEntriesInfoManagerTy {
179 CodeGenModule &CGM;
Alexey Bataev1d2353d2015-06-24 11:01:36 +0000180
Samuel Antaoee8fb302016-01-06 13:42:12 +0000181 /// \brief Number of entries registered so far.
182 unsigned OffloadingEntriesNum;
183
184 public:
185 /// \brief Base class of the entries info.
186 class OffloadEntryInfo {
187 public:
188 /// \brief Kind of a given entry. Currently, only target regions are
189 /// supported.
Reid Klecknerdc78f952016-01-11 20:55:16 +0000190 enum OffloadingEntryInfoKinds : unsigned {
Samuel Antaoee8fb302016-01-06 13:42:12 +0000191 // Entry is a target region.
192 OFFLOAD_ENTRY_INFO_TARGET_REGION = 0,
193 // Invalid entry info.
194 OFFLOAD_ENTRY_INFO_INVALID = ~0u
195 };
196
197 OffloadEntryInfo() : Order(~0u), Kind(OFFLOAD_ENTRY_INFO_INVALID) {}
198 explicit OffloadEntryInfo(OffloadingEntryInfoKinds Kind, unsigned Order)
199 : Order(Order), Kind(Kind) {}
200
201 bool isValid() const { return Order != ~0u; }
202 unsigned getOrder() const { return Order; }
203 OffloadingEntryInfoKinds getKind() const { return Kind; }
204 static bool classof(const OffloadEntryInfo *Info) { return true; }
205
206 protected:
207 // \brief Order this entry was emitted.
208 unsigned Order;
209
210 OffloadingEntryInfoKinds Kind;
211 };
212
213 /// \brief Return true if a there are no entries defined.
214 bool empty() const;
215 /// \brief Return number of entries defined so far.
216 unsigned size() const { return OffloadingEntriesNum; }
217 OffloadEntriesInfoManagerTy(CodeGenModule &CGM)
218 : CGM(CGM), OffloadingEntriesNum(0) {}
219
220 ///
221 /// Target region entries related.
222 ///
223 /// \brief Target region entries info.
224 class OffloadEntryInfoTargetRegion : public OffloadEntryInfo {
225 // \brief Address of the entity that has to be mapped for offloading.
226 llvm::Constant *Addr;
227 // \brief Address that can be used as the ID of the entry.
228 llvm::Constant *ID;
229
230 public:
231 OffloadEntryInfoTargetRegion()
232 : OffloadEntryInfo(OFFLOAD_ENTRY_INFO_TARGET_REGION, ~0u),
233 Addr(nullptr), ID(nullptr) {}
234 explicit OffloadEntryInfoTargetRegion(unsigned Order,
235 llvm::Constant *Addr,
236 llvm::Constant *ID)
237 : OffloadEntryInfo(OFFLOAD_ENTRY_INFO_TARGET_REGION, Order),
238 Addr(Addr), ID(ID) {}
239
240 llvm::Constant *getAddress() const { return Addr; }
241 llvm::Constant *getID() const { return ID; }
242 void setAddress(llvm::Constant *V) {
243 assert(!Addr && "Address as been set before!");
244 Addr = V;
245 }
246 void setID(llvm::Constant *V) {
247 assert(!ID && "ID as been set before!");
248 ID = V;
249 }
250 static bool classof(const OffloadEntryInfo *Info) {
251 return Info->getKind() == OFFLOAD_ENTRY_INFO_TARGET_REGION;
252 }
253 };
254 /// \brief Initialize target region entry.
255 void initializeTargetRegionEntryInfo(unsigned DeviceID, unsigned FileID,
256 StringRef ParentName, unsigned LineNum,
Samuel Antao2de62b02016-02-13 23:35:10 +0000257 unsigned Order);
Samuel Antaoee8fb302016-01-06 13:42:12 +0000258 /// \brief Register target region entry.
259 void registerTargetRegionEntryInfo(unsigned DeviceID, unsigned FileID,
260 StringRef ParentName, unsigned LineNum,
Samuel Antao2de62b02016-02-13 23:35:10 +0000261 llvm::Constant *Addr,
Samuel Antaoee8fb302016-01-06 13:42:12 +0000262 llvm::Constant *ID);
263 /// \brief Return true if a target region entry with the provided
264 /// information exists.
265 bool hasTargetRegionEntryInfo(unsigned DeviceID, unsigned FileID,
Samuel Antao2de62b02016-02-13 23:35:10 +0000266 StringRef ParentName, unsigned LineNum) const;
Samuel Antaoee8fb302016-01-06 13:42:12 +0000267 /// brief Applies action \a Action on all registered entries.
268 typedef llvm::function_ref<void(unsigned, unsigned, StringRef, unsigned,
Samuel Antao2de62b02016-02-13 23:35:10 +0000269 OffloadEntryInfoTargetRegion &)>
Samuel Antaoee8fb302016-01-06 13:42:12 +0000270 OffloadTargetRegionEntryInfoActTy;
271 void actOnTargetRegionEntriesInfo(
272 const OffloadTargetRegionEntryInfoActTy &Action);
273
274 private:
275 // Storage for target region entries kind. The storage is to be indexed by
Samuel Antao2de62b02016-02-13 23:35:10 +0000276 // file ID, device ID, parent function name and line number.
Samuel Antaoee8fb302016-01-06 13:42:12 +0000277 typedef llvm::DenseMap<unsigned, OffloadEntryInfoTargetRegion>
Samuel Antaoee8fb302016-01-06 13:42:12 +0000278 OffloadEntriesTargetRegionPerLine;
279 typedef llvm::StringMap<OffloadEntriesTargetRegionPerLine>
280 OffloadEntriesTargetRegionPerParentName;
281 typedef llvm::DenseMap<unsigned, OffloadEntriesTargetRegionPerParentName>
282 OffloadEntriesTargetRegionPerFile;
283 typedef llvm::DenseMap<unsigned, OffloadEntriesTargetRegionPerFile>
284 OffloadEntriesTargetRegionPerDevice;
285 typedef OffloadEntriesTargetRegionPerDevice OffloadEntriesTargetRegionTy;
286 OffloadEntriesTargetRegionTy OffloadEntriesTargetRegion;
287 };
288 OffloadEntriesInfoManagerTy OffloadEntriesInfoManager;
289
290 /// \brief Creates and registers offloading binary descriptor for the current
291 /// compilation unit. The function that does the registration is returned.
292 llvm::Function *createOffloadingBinaryDescriptorRegistration();
293
Samuel Antaoee8fb302016-01-06 13:42:12 +0000294 /// \brief Creates all the offload entries in the current compilation unit
295 /// along with the associated metadata.
296 void createOffloadEntriesAndInfoMetadata();
297
298 /// \brief Loads all the offload entries information from the host IR
299 /// metadata.
300 void loadOffloadInfoMetadata();
301
302 /// \brief Returns __tgt_offload_entry type.
303 QualType getTgtOffloadEntryQTy();
304
305 /// \brief Returns __tgt_device_image type.
306 QualType getTgtDeviceImageQTy();
307
308 /// \brief Returns __tgt_bin_desc type.
309 QualType getTgtBinaryDescriptorQTy();
310
311 /// \brief Start scanning from statement \a S and and emit all target regions
312 /// found along the way.
313 /// \param S Starting statement.
314 /// \param ParentName Name of the function declaration that is being scanned.
315 void scanForTargetRegionsFunctions(const Stmt *S, StringRef ParentName);
Alexey Bataev62b63b12015-03-10 07:28:44 +0000316
317 /// \brief Build type kmp_routine_entry_t (if not built yet).
318 void emitKmpRoutineEntryT(QualType KmpInt32Ty);
Alexey Bataev9959db52014-05-06 10:08:46 +0000319
Alexey Bataev9959db52014-05-06 10:08:46 +0000320 /// \brief Emits object of ident_t type with info for source location.
Alexey Bataev9959db52014-05-06 10:08:46 +0000321 /// \param Flags Flags for OpenMP location.
322 ///
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000323 llvm::Value *emitUpdateLocation(CodeGenFunction &CGF, SourceLocation Loc,
Alexey Bataev50b3c952016-02-19 10:38:26 +0000324 unsigned Flags = 0);
Alexey Bataev9959db52014-05-06 10:08:46 +0000325
Alexey Bataevd74d0602014-10-13 06:02:40 +0000326 /// \brief Returns pointer to ident_t type.
Alexey Bataev9959db52014-05-06 10:08:46 +0000327 llvm::Type *getIdentTyPointerTy();
328
Alexey Bataevd74d0602014-10-13 06:02:40 +0000329 /// \brief Returns pointer to kmpc_micro type.
Alexey Bataev9959db52014-05-06 10:08:46 +0000330 llvm::Type *getKmpc_MicroPointerTy();
331
332 /// \brief Returns specified OpenMP runtime function.
333 /// \param Function OpenMP runtime function.
334 /// \return Specified function.
Alexey Bataev50b3c952016-02-19 10:38:26 +0000335 llvm::Constant *createRuntimeFunction(unsigned Function);
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +0000336
Alexander Musman21212e42015-03-13 10:38:23 +0000337 /// \brief Returns __kmpc_for_static_init_* runtime function for the specified
338 /// size \a IVSize and sign \a IVSigned.
339 llvm::Constant *createForStaticInitFunction(unsigned IVSize, bool IVSigned);
340
Alexander Musman92bdaab2015-03-12 13:37:50 +0000341 /// \brief Returns __kmpc_dispatch_init_* runtime function for the specified
342 /// size \a IVSize and sign \a IVSigned.
343 llvm::Constant *createDispatchInitFunction(unsigned IVSize, bool IVSigned);
344
345 /// \brief Returns __kmpc_dispatch_next_* runtime function for the specified
346 /// size \a IVSize and sign \a IVSigned.
347 llvm::Constant *createDispatchNextFunction(unsigned IVSize, bool IVSigned);
348
Alexey Bataev98eb6e32015-04-22 11:15:40 +0000349 /// \brief Returns __kmpc_dispatch_fini_* runtime function for the specified
350 /// size \a IVSize and sign \a IVSigned.
351 llvm::Constant *createDispatchFiniFunction(unsigned IVSize, bool IVSigned);
352
Alexey Bataev97720002014-11-11 04:05:39 +0000353 /// \brief If the specified mangled name is not in the module, create and
354 /// return threadprivate cache object. This object is a pointer's worth of
355 /// storage that's reserved for use by the OpenMP runtime.
NAKAMURA Takumicdcbfba2014-11-11 07:58:06 +0000356 /// \param VD Threadprivate variable.
Alexey Bataev97720002014-11-11 04:05:39 +0000357 /// \return Cache variable for the specified threadprivate.
358 llvm::Constant *getOrCreateThreadPrivateCache(const VarDecl *VD);
359
Alexey Bataevd74d0602014-10-13 06:02:40 +0000360 /// \brief Emits address of the word in a memory where current thread id is
361 /// stored.
John McCall7f416cc2015-09-08 08:05:57 +0000362 virtual Address emitThreadIDAddress(CodeGenFunction &CGF, SourceLocation Loc);
Alexey Bataevd74d0602014-10-13 06:02:40 +0000363
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000364 /// \brief Gets thread id value for the current thread.
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000365 ///
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000366 llvm::Value *getThreadID(CodeGenFunction &CGF, SourceLocation Loc);
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000367
Alexey Bataev97720002014-11-11 04:05:39 +0000368 /// \brief Gets (if variable with the given name already exist) or creates
369 /// internal global variable with the specified Name. The created variable has
370 /// linkage CommonLinkage by default and is initialized by null value.
371 /// \param Ty Type of the global variable. If it is exist already the type
372 /// must be the same.
373 /// \param Name Name of the variable.
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000374 llvm::Constant *getOrCreateInternalVariable(llvm::Type *Ty,
Alexey Bataev97720002014-11-11 04:05:39 +0000375 const llvm::Twine &Name);
376
377 /// \brief Set of threadprivate variables with the generated initializer.
Benjamin Kramer8fdba912016-02-02 14:24:21 +0000378 llvm::SmallPtrSet<const VarDecl *, 4> ThreadPrivateWithDefinition;
Alexey Bataev97720002014-11-11 04:05:39 +0000379
380 /// \brief Emits initialization code for the threadprivate variables.
381 /// \param VDAddr Address of the global variable \a VD.
382 /// \param Ctor Pointer to a global init function for \a VD.
383 /// \param CopyCtor Pointer to a global copy function for \a VD.
384 /// \param Dtor Pointer to a global destructor function for \a VD.
385 /// \param Loc Location of threadprivate declaration.
John McCall7f416cc2015-09-08 08:05:57 +0000386 void emitThreadPrivateVarInit(CodeGenFunction &CGF, Address VDAddr,
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000387 llvm::Value *Ctor, llvm::Value *CopyCtor,
388 llvm::Value *Dtor, SourceLocation Loc);
Alexey Bataev97720002014-11-11 04:05:39 +0000389
Alexey Bataev75ddfab2014-12-01 11:32:38 +0000390 /// \brief Returns corresponding lock object for the specified critical region
391 /// name. If the lock object does not exist it is created, otherwise the
392 /// reference to the existing copy is returned.
393 /// \param CriticalName Name of the critical region.
394 ///
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000395 llvm::Value *getCriticalRegionLock(StringRef CriticalName);
Alexey Bataev75ddfab2014-12-01 11:32:38 +0000396
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000397public:
398 explicit CGOpenMPRuntime(CodeGenModule &CGM);
Angel Garcia Gomez637d1e62015-10-20 13:23:58 +0000399 virtual ~CGOpenMPRuntime() {}
Alexey Bataev91797552015-03-18 04:13:55 +0000400 virtual void clear();
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000401
Alexey Bataevc5b1d322016-03-04 09:22:22 +0000402 /// Emit code for the specified user defined reduction construct.
403 virtual void emitUserDefinedReduction(CodeGenFunction *CGF,
404 const OMPDeclareReductionDecl *D);
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000405 /// Get combiner/initializer for the specified user-defined reduction, if any.
406 virtual std::pair<llvm::Function *, llvm::Function *>
407 getUserDefinedReduction(const OMPDeclareReductionDecl *D);
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000408 /// \brief Emits outlined function for the specified OpenMP parallel directive
409 /// \a D. This outlined function has type void(*)(kmp_int32 *ThreadID,
410 /// kmp_int32 BoundID, struct context_vars*).
Alexey Bataev18095712014-10-10 12:19:54 +0000411 /// \param D OpenMP directive.
412 /// \param ThreadIDVar Variable for thread id in the current OpenMP region.
Alexey Bataev81c7ea02015-07-03 09:56:58 +0000413 /// \param InnermostKind Kind of innermost directive (for simple directives it
414 /// is a directive itself, for combined - its innermost directive).
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000415 /// \param CodeGen Code generation sequence for the \a D directive.
Carlo Bertolli430d8ec2016-03-03 20:34:23 +0000416 virtual llvm::Value *emitParallelOrTeamsOutlinedFunction(
Alexey Bataev81c7ea02015-07-03 09:56:58 +0000417 const OMPExecutableDirective &D, const VarDecl *ThreadIDVar,
418 OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen);
Alexey Bataev18095712014-10-10 12:19:54 +0000419
Alexey Bataev62b63b12015-03-10 07:28:44 +0000420 /// \brief Emits outlined function for the OpenMP task directive \a D. This
421 /// outlined function has type void(*)(kmp_int32 ThreadID, kmp_int32
422 /// PartID, struct context_vars*).
423 /// \param D OpenMP directive.
424 /// \param ThreadIDVar Variable for thread id in the current OpenMP region.
Alexey Bataev81c7ea02015-07-03 09:56:58 +0000425 /// \param InnermostKind Kind of innermost directive (for simple directives it
426 /// is a directive itself, for combined - its innermost directive).
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000427 /// \param CodeGen Code generation sequence for the \a D directive.
Alexey Bataev62b63b12015-03-10 07:28:44 +0000428 ///
Alexey Bataev81c7ea02015-07-03 09:56:58 +0000429 virtual llvm::Value *emitTaskOutlinedFunction(
430 const OMPExecutableDirective &D, const VarDecl *ThreadIDVar,
431 OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen);
Alexey Bataev62b63b12015-03-10 07:28:44 +0000432
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000433 /// \brief Cleans up references to the objects in finished function.
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000434 ///
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000435 void functionFinished(CodeGenFunction &CGF);
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000436
Alexey Bataev1d677132015-04-22 13:57:31 +0000437 /// \brief Emits code for parallel or serial call of the \a OutlinedFn with
438 /// variables captured in a record which address is stored in \a
439 /// CapturedStruct.
Alexey Bataev18095712014-10-10 12:19:54 +0000440 /// \param OutlinedFn Outlined function to be run in parallel threads. Type of
Alexey Bataev62b63b12015-03-10 07:28:44 +0000441 /// this function is void(*)(kmp_int32 *, kmp_int32, struct context_vars*).
NAKAMURA Takumi62f0eb52015-09-11 08:13:32 +0000442 /// \param CapturedVars A pointer to the record with the references to
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000443 /// variables used in \a OutlinedFn function.
Alexey Bataev1d677132015-04-22 13:57:31 +0000444 /// \param IfCond Condition in the associated 'if' clause, if it was
445 /// specified, nullptr otherwise.
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000446 ///
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000447 virtual void emitParallelCall(CodeGenFunction &CGF, SourceLocation Loc,
448 llvm::Value *OutlinedFn,
Alexey Bataev2377fe92015-09-10 08:12:02 +0000449 ArrayRef<llvm::Value *> CapturedVars,
450 const Expr *IfCond);
Alexey Bataevd74d0602014-10-13 06:02:40 +0000451
Alexey Bataev75ddfab2014-12-01 11:32:38 +0000452 /// \brief Emits a critical region.
Alexey Bataev18095712014-10-10 12:19:54 +0000453 /// \param CriticalName Name of the critical region.
Alexey Bataev75ddfab2014-12-01 11:32:38 +0000454 /// \param CriticalOpGen Generator for the statement associated with the given
455 /// critical region.
Alexey Bataevfc57d162015-12-15 10:55:09 +0000456 /// \param Hint Value of the 'hint' clause (optional).
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000457 virtual void emitCriticalRegion(CodeGenFunction &CGF, StringRef CriticalName,
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000458 const RegionCodeGenTy &CriticalOpGen,
Alexey Bataevfc57d162015-12-15 10:55:09 +0000459 SourceLocation Loc,
460 const Expr *Hint = nullptr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000461
Alexey Bataev8d690652014-12-04 07:23:53 +0000462 /// \brief Emits a master region.
463 /// \param MasterOpGen Generator for the statement associated with the given
464 /// master region.
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000465 virtual void emitMasterRegion(CodeGenFunction &CGF,
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000466 const RegionCodeGenTy &MasterOpGen,
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000467 SourceLocation Loc);
Alexey Bataev8d690652014-12-04 07:23:53 +0000468
Alexey Bataev9f797f32015-02-05 05:57:51 +0000469 /// \brief Emits code for a taskyield directive.
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000470 virtual void emitTaskyieldCall(CodeGenFunction &CGF, SourceLocation Loc);
Alexey Bataev9f797f32015-02-05 05:57:51 +0000471
Alexey Bataevc30dd2d2015-06-18 12:14:09 +0000472 /// \brief Emit a taskgroup region.
473 /// \param TaskgroupOpGen Generator for the statement associated with the
474 /// given taskgroup region.
475 virtual void emitTaskgroupRegion(CodeGenFunction &CGF,
476 const RegionCodeGenTy &TaskgroupOpGen,
477 SourceLocation Loc);
478
Alexey Bataev6956e2e2015-02-05 06:35:41 +0000479 /// \brief Emits a single region.
480 /// \param SingleOpGen Generator for the statement associated with the given
481 /// single region.
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000482 virtual void emitSingleRegion(CodeGenFunction &CGF,
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000483 const RegionCodeGenTy &SingleOpGen,
Alexey Bataeva63048e2015-03-23 06:18:07 +0000484 SourceLocation Loc,
485 ArrayRef<const Expr *> CopyprivateVars,
Alexey Bataev420d45b2015-04-14 05:11:24 +0000486 ArrayRef<const Expr *> DestExprs,
Alexey Bataeva63048e2015-03-23 06:18:07 +0000487 ArrayRef<const Expr *> SrcExprs,
Alexey Bataeva63048e2015-03-23 06:18:07 +0000488 ArrayRef<const Expr *> AssignmentOps);
Alexey Bataev6956e2e2015-02-05 06:35:41 +0000489
Alexey Bataev98eb6e32015-04-22 11:15:40 +0000490 /// \brief Emit an ordered region.
491 /// \param OrderedOpGen Generator for the statement associated with the given
Alexey Bataevc30dd2d2015-06-18 12:14:09 +0000492 /// ordered region.
Alexey Bataev98eb6e32015-04-22 11:15:40 +0000493 virtual void emitOrderedRegion(CodeGenFunction &CGF,
494 const RegionCodeGenTy &OrderedOpGen,
Alexey Bataev5f600d62015-09-29 03:48:57 +0000495 SourceLocation Loc, bool IsThreads);
Alexey Bataev98eb6e32015-04-22 11:15:40 +0000496
Alexey Bataevf2685682015-03-30 04:30:22 +0000497 /// \brief Emit an implicit/explicit barrier for OpenMP threads.
498 /// \param Kind Directive for which this implicit barrier call must be
499 /// generated. Must be OMPD_barrier for explicit barrier generation.
Alexey Bataev25e5b442015-09-15 12:52:43 +0000500 /// \param EmitChecks true if need to emit checks for cancellation barriers.
501 /// \param ForceSimpleCall true simple barrier call must be emitted, false if
502 /// runtime class decides which one to emit (simple or with cancellation
503 /// checks).
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000504 ///
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000505 virtual void emitBarrierCall(CodeGenFunction &CGF, SourceLocation Loc,
Alexey Bataev81c7ea02015-07-03 09:56:58 +0000506 OpenMPDirectiveKind Kind,
Alexey Bataev25e5b442015-09-15 12:52:43 +0000507 bool EmitChecks = true,
508 bool ForceSimpleCall = false);
Alexey Bataevb2059782014-10-13 08:23:51 +0000509
Alexander Musmanc6388682014-12-15 07:07:06 +0000510 /// \brief Check if the specified \a ScheduleKind is static non-chunked.
511 /// This kind of worksharing directive is emitted without outer loop.
512 /// \param ScheduleKind Schedule kind specified in the 'schedule' clause.
513 /// \param Chunked True if chunk is specified in the clause.
514 ///
515 virtual bool isStaticNonchunked(OpenMPScheduleClauseKind ScheduleKind,
516 bool Chunked) const;
517
Carlo Bertollifc35ad22016-03-07 16:04:49 +0000518 /// \brief Check if the specified \a ScheduleKind is static non-chunked.
519 /// This kind of distribute directive is emitted without outer loop.
520 /// \param ScheduleKind Schedule kind specified in the 'dist_schedule' clause.
521 /// \param Chunked True if chunk is specified in the clause.
522 ///
523 virtual bool isStaticNonchunked(OpenMPDistScheduleClauseKind ScheduleKind,
524 bool Chunked) const;
525
Alexander Musmandf7a8e22015-01-22 08:49:35 +0000526 /// \brief Check if the specified \a ScheduleKind is dynamic.
527 /// This kind of worksharing directive is emitted without outer loop.
528 /// \param ScheduleKind Schedule Kind specified in the 'schedule' clause.
529 ///
530 virtual bool isDynamic(OpenMPScheduleClauseKind ScheduleKind) const;
531
NAKAMURA Takumiff7a9252015-09-08 09:42:41 +0000532 virtual void emitForDispatchInit(CodeGenFunction &CGF, SourceLocation Loc,
533 OpenMPScheduleClauseKind SchedKind,
534 unsigned IVSize, bool IVSigned,
535 bool Ordered, llvm::Value *UB,
536 llvm::Value *Chunk = nullptr);
537
Alexander Musmanc6388682014-12-15 07:07:06 +0000538 /// \brief Call the appropriate runtime routine to initialize it before start
539 /// of loop.
540 ///
541 /// Depending on the loop schedule, it is nesessary to call some runtime
542 /// routine before start of the OpenMP loop to get the loop upper / lower
543 /// bounds \a LB and \a UB and stride \a ST.
544 ///
545 /// \param CGF Reference to current CodeGenFunction.
546 /// \param Loc Clang source location.
NAKAMURA Takumieca08382014-12-17 14:47:06 +0000547 /// \param SchedKind Schedule kind, specified by the 'schedule' clause.
Alexander Musmanc6388682014-12-15 07:07:06 +0000548 /// \param IVSize Size of the iteration variable in bits.
549 /// \param IVSigned Sign of the interation variable.
Alexey Bataevd7589ffe2015-05-20 13:12:48 +0000550 /// \param Ordered true if loop is ordered, false otherwise.
Alexander Musmanc6388682014-12-15 07:07:06 +0000551 /// \param IL Address of the output variable in which the flag of the
552 /// last iteration is returned.
553 /// \param LB Address of the output variable in which the lower iteration
554 /// number is returned.
555 /// \param UB Address of the output variable in which the upper iteration
556 /// number is returned.
557 /// \param ST Address of the output variable in which the stride value is
558 /// returned nesessary to generated the static_chunked scheduled loop.
559 /// \param Chunk Value of the chunk for the static_chunked scheduled loop.
560 /// For the default (nullptr) value, the chunk 1 will be used.
561 ///
John McCall7f416cc2015-09-08 08:05:57 +0000562 virtual void emitForStaticInit(CodeGenFunction &CGF, SourceLocation Loc,
563 OpenMPScheduleClauseKind SchedKind,
564 unsigned IVSize, bool IVSigned, bool Ordered,
565 Address IL, Address LB,
566 Address UB, Address ST,
567 llvm::Value *Chunk = nullptr);
Alexander Musmanc6388682014-12-15 07:07:06 +0000568
Carlo Bertollifc35ad22016-03-07 16:04:49 +0000569 ///
570 /// \param CGF Reference to current CodeGenFunction.
571 /// \param Loc Clang source location.
572 /// \param SchedKind Schedule kind, specified by the 'dist_schedule' clause.
573 /// \param IVSize Size of the iteration variable in bits.
574 /// \param IVSigned Sign of the interation variable.
575 /// \param Ordered true if loop is ordered, false otherwise.
576 /// \param IL Address of the output variable in which the flag of the
577 /// last iteration is returned.
578 /// \param LB Address of the output variable in which the lower iteration
579 /// number is returned.
580 /// \param UB Address of the output variable in which the upper iteration
581 /// number is returned.
582 /// \param ST Address of the output variable in which the stride value is
583 /// returned nesessary to generated the static_chunked scheduled loop.
584 /// \param Chunk Value of the chunk for the static_chunked scheduled loop.
585 /// For the default (nullptr) value, the chunk 1 will be used.
586 ///
587 virtual void emitDistributeStaticInit(CodeGenFunction &CGF, SourceLocation Loc,
588 OpenMPDistScheduleClauseKind SchedKind,
589 unsigned IVSize, bool IVSigned,
590 bool Ordered, Address IL, Address LB,
591 Address UB, Address ST,
592 llvm::Value *Chunk = nullptr);
593
Alexander Musmanc6388682014-12-15 07:07:06 +0000594 /// \brief Call the appropriate runtime routine to notify that we finished
Alexey Bataev98eb6e32015-04-22 11:15:40 +0000595 /// iteration of the ordered loop with the dynamic scheduling.
596 ///
597 /// \param CGF Reference to current CodeGenFunction.
598 /// \param Loc Clang source location.
599 /// \param IVSize Size of the iteration variable in bits.
600 /// \param IVSigned Sign of the interation variable.
601 ///
Alexey Bataevd7589ffe2015-05-20 13:12:48 +0000602 virtual void emitForOrderedIterationEnd(CodeGenFunction &CGF,
603 SourceLocation Loc, unsigned IVSize,
604 bool IVSigned);
Alexey Bataev98eb6e32015-04-22 11:15:40 +0000605
606 /// \brief Call the appropriate runtime routine to notify that we finished
Alexander Musmanc6388682014-12-15 07:07:06 +0000607 /// all the work with current loop.
608 ///
609 /// \param CGF Reference to current CodeGenFunction.
610 /// \param Loc Clang source location.
Alexander Musmanc6388682014-12-15 07:07:06 +0000611 ///
Alexey Bataev98eb6e32015-04-22 11:15:40 +0000612 virtual void emitForStaticFinish(CodeGenFunction &CGF, SourceLocation Loc);
Alexander Musmanc6388682014-12-15 07:07:06 +0000613
Alexander Musman92bdaab2015-03-12 13:37:50 +0000614 /// Call __kmpc_dispatch_next(
615 /// ident_t *loc, kmp_int32 tid, kmp_int32 *p_lastiter,
616 /// kmp_int[32|64] *p_lower, kmp_int[32|64] *p_upper,
617 /// kmp_int[32|64] *p_stride);
618 /// \param IVSize Size of the iteration variable in bits.
619 /// \param IVSigned Sign of the interation variable.
620 /// \param IL Address of the output variable in which the flag of the
621 /// last iteration is returned.
622 /// \param LB Address of the output variable in which the lower iteration
623 /// number is returned.
624 /// \param UB Address of the output variable in which the upper iteration
625 /// number is returned.
626 /// \param ST Address of the output variable in which the stride value is
627 /// returned.
628 virtual llvm::Value *emitForNext(CodeGenFunction &CGF, SourceLocation Loc,
629 unsigned IVSize, bool IVSigned,
John McCall7f416cc2015-09-08 08:05:57 +0000630 Address IL, Address LB,
631 Address UB, Address ST);
Alexander Musman92bdaab2015-03-12 13:37:50 +0000632
Alexey Bataevb2059782014-10-13 08:23:51 +0000633 /// \brief Emits call to void __kmpc_push_num_threads(ident_t *loc, kmp_int32
634 /// global_tid, kmp_int32 num_threads) to generate code for 'num_threads'
635 /// clause.
636 /// \param NumThreads An integer value of threads.
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000637 virtual void emitNumThreadsClause(CodeGenFunction &CGF,
638 llvm::Value *NumThreads,
639 SourceLocation Loc);
Alexey Bataev97720002014-11-11 04:05:39 +0000640
Alexey Bataev7f210c62015-06-18 13:40:03 +0000641 /// \brief Emit call to void __kmpc_push_proc_bind(ident_t *loc, kmp_int32
642 /// global_tid, int proc_bind) to generate code for 'proc_bind' clause.
643 virtual void emitProcBindClause(CodeGenFunction &CGF,
644 OpenMPProcBindClauseKind ProcBind,
645 SourceLocation Loc);
646
Alexey Bataev97720002014-11-11 04:05:39 +0000647 /// \brief Returns address of the threadprivate variable for the current
648 /// thread.
NAKAMURA Takumicdcbfba2014-11-11 07:58:06 +0000649 /// \param VD Threadprivate variable.
Alexey Bataev97720002014-11-11 04:05:39 +0000650 /// \param VDAddr Address of the global variable \a VD.
651 /// \param Loc Location of the reference to threadprivate var.
652 /// \return Address of the threadprivate variable for the current thread.
John McCall7f416cc2015-09-08 08:05:57 +0000653 virtual Address getAddrOfThreadPrivate(CodeGenFunction &CGF,
654 const VarDecl *VD,
655 Address VDAddr,
656 SourceLocation Loc);
Alexey Bataev97720002014-11-11 04:05:39 +0000657
658 /// \brief Emit a code for initialization of threadprivate variable. It emits
659 /// a call to runtime library which adds initial value to the newly created
660 /// threadprivate variable (if it is not constant) and registers destructor
661 /// for the variable (if any).
662 /// \param VD Threadprivate variable.
663 /// \param VDAddr Address of the global variable \a VD.
664 /// \param Loc Location of threadprivate declaration.
665 /// \param PerformInit true if initialization expression is not constant.
666 virtual llvm::Function *
John McCall7f416cc2015-09-08 08:05:57 +0000667 emitThreadPrivateVarDefinition(const VarDecl *VD, Address VDAddr,
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000668 SourceLocation Loc, bool PerformInit,
669 CodeGenFunction *CGF = nullptr);
Alexey Bataevcc37cc12014-11-20 04:34:54 +0000670
671 /// \brief Emit flush of the variables specified in 'omp flush' directive.
672 /// \param Vars List of variables to flush.
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000673 virtual void emitFlush(CodeGenFunction &CGF, ArrayRef<const Expr *> Vars,
674 SourceLocation Loc);
Alexey Bataev62b63b12015-03-10 07:28:44 +0000675
676 /// \brief Emit task region for the task directive. The task region is
Nico Weber20b0ce32015-04-28 18:19:18 +0000677 /// emitted in several steps:
Alexey Bataev62b63b12015-03-10 07:28:44 +0000678 /// 1. Emit a call to kmp_task_t *__kmpc_omp_task_alloc(ident_t *, kmp_int32
679 /// gtid, kmp_int32 flags, size_t sizeof_kmp_task_t, size_t sizeof_shareds,
680 /// kmp_routine_entry_t *task_entry). Here task_entry is a pointer to the
681 /// function:
682 /// kmp_int32 .omp_task_entry.(kmp_int32 gtid, kmp_task_t *tt) {
683 /// TaskFunction(gtid, tt->part_id, tt->shareds);
684 /// return 0;
685 /// }
686 /// 2. Copy a list of shared variables to field shareds of the resulting
687 /// structure kmp_task_t returned by the previous call (if any).
688 /// 3. Copy a pointer to destructions function to field destructions of the
689 /// resulting structure kmp_task_t.
690 /// 4. Emit a call to kmp_int32 __kmpc_omp_task(ident_t *, kmp_int32 gtid,
691 /// kmp_task_t *new_task), where new_task is a resulting structure from
692 /// previous items.
Alexey Bataev36c1eb92015-04-30 06:51:57 +0000693 /// \param D Current task directive.
Alexey Bataev62b63b12015-03-10 07:28:44 +0000694 /// \param Tied true if the task is tied (the task is tied to the thread that
695 /// can suspend its task region), false - untied (the task is not tied to any
696 /// thread).
697 /// \param Final Contains either constant bool value, or llvm::Value * of i1
698 /// type for final clause. If the value is true, the task forces all of its
699 /// child tasks to become final and included tasks.
700 /// \param TaskFunction An LLVM function with type void (*)(i32 /*gtid*/, i32
701 /// /*part_id*/, captured_struct */*__context*/);
702 /// \param SharedsTy A type which contains references the shared variables.
Alexey Bataev1d2353d2015-06-24 11:01:36 +0000703 /// \param Shareds Context with the list of shared variables from the \p
Alexey Bataev62b63b12015-03-10 07:28:44 +0000704 /// TaskFunction.
Alexey Bataev1d677132015-04-22 13:57:31 +0000705 /// \param IfCond Not a nullptr if 'if' clause was specified, nullptr
706 /// otherwise.
Alexey Bataev36c1eb92015-04-30 06:51:57 +0000707 /// \param PrivateVars List of references to private variables for the task
708 /// directive.
709 /// \param PrivateCopies List of private copies for each private variable in
Alexey Bataev9e034042015-05-05 04:05:12 +0000710 /// \p PrivateVars.
711 /// \param FirstprivateVars List of references to private variables for the
712 /// task directive.
713 /// \param FirstprivateCopies List of private copies for each private variable
714 /// in \p FirstprivateVars.
715 /// \param FirstprivateInits List of references to auto generated variables
716 /// used for initialization of a single array element. Used if firstprivate
717 /// variable is of array type.
Alexey Bataev1d2353d2015-06-24 11:01:36 +0000718 /// \param Dependences List of dependences for the 'task' construct, including
719 /// original expression and dependency type.
720 virtual void emitTaskCall(
721 CodeGenFunction &CGF, SourceLocation Loc, const OMPExecutableDirective &D,
722 bool Tied, llvm::PointerIntPair<llvm::Value *, 1, bool> Final,
John McCall7f416cc2015-09-08 08:05:57 +0000723 llvm::Value *TaskFunction, QualType SharedsTy, Address Shareds,
Alexey Bataev1d2353d2015-06-24 11:01:36 +0000724 const Expr *IfCond, ArrayRef<const Expr *> PrivateVars,
725 ArrayRef<const Expr *> PrivateCopies,
726 ArrayRef<const Expr *> FirstprivateVars,
727 ArrayRef<const Expr *> FirstprivateCopies,
728 ArrayRef<const Expr *> FirstprivateInits,
729 ArrayRef<std::pair<OpenMPDependClauseKind, const Expr *>> Dependences);
Alexey Bataev794ba0d2015-04-10 10:43:45 +0000730
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000731 /// \brief Emit code for the directive that does not require outlining.
732 ///
Alexey Bataev81c7ea02015-07-03 09:56:58 +0000733 /// \param InnermostKind Kind of innermost directive (for simple directives it
734 /// is a directive itself, for combined - its innermost directive).
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000735 /// \param CodeGen Code generation sequence for the \a D directive.
Alexey Bataev25e5b442015-09-15 12:52:43 +0000736 /// \param HasCancel true if region has inner cancel directive, false
737 /// otherwise.
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000738 virtual void emitInlinedDirective(CodeGenFunction &CGF,
Alexey Bataev81c7ea02015-07-03 09:56:58 +0000739 OpenMPDirectiveKind InnermostKind,
Alexey Bataev25e5b442015-09-15 12:52:43 +0000740 const RegionCodeGenTy &CodeGen,
741 bool HasCancel = false);
Alexey Bataev794ba0d2015-04-10 10:43:45 +0000742 /// \brief Emit a code for reduction clause. Next code should be emitted for
743 /// reduction:
744 /// \code
745 ///
746 /// static kmp_critical_name lock = { 0 };
747 ///
748 /// void reduce_func(void *lhs[<n>], void *rhs[<n>]) {
749 /// ...
750 /// *(Type<i>*)lhs[i] = RedOp<i>(*(Type<i>*)lhs[i], *(Type<i>*)rhs[i]);
751 /// ...
752 /// }
753 ///
754 /// ...
755 /// void *RedList[<n>] = {&<RHSExprs>[0], ..., &<RHSExprs>[<n>-1]};
756 /// switch (__kmpc_reduce{_nowait}(<loc>, <gtid>, <n>, sizeof(RedList),
757 /// RedList, reduce_func, &<lock>)) {
758 /// case 1:
759 /// ...
760 /// <LHSExprs>[i] = RedOp<i>(*<LHSExprs>[i], *<RHSExprs>[i]);
761 /// ...
762 /// __kmpc_end_reduce{_nowait}(<loc>, <gtid>, &<lock>);
763 /// break;
764 /// case 2:
765 /// ...
766 /// Atomic(<LHSExprs>[i] = RedOp<i>(*<LHSExprs>[i], *<RHSExprs>[i]));
767 /// ...
768 /// break;
769 /// default:;
770 /// }
771 /// \endcode
772 ///
Alexey Bataevf24e7b12015-10-08 09:10:53 +0000773 /// \param Privates List of private copies for original reduction arguments.
Alexey Bataev794ba0d2015-04-10 10:43:45 +0000774 /// \param LHSExprs List of LHS in \a ReductionOps reduction operations.
775 /// \param RHSExprs List of RHS in \a ReductionOps reduction operations.
776 /// \param ReductionOps List of reduction operations in form 'LHS binop RHS'
777 /// or 'operator binop(LHS, RHS)'.
778 /// \param WithNowait true if parent directive has also nowait clause, false
779 /// otherwise.
780 virtual void emitReduction(CodeGenFunction &CGF, SourceLocation Loc,
Alexey Bataevf24e7b12015-10-08 09:10:53 +0000781 ArrayRef<const Expr *> Privates,
Alexey Bataev794ba0d2015-04-10 10:43:45 +0000782 ArrayRef<const Expr *> LHSExprs,
783 ArrayRef<const Expr *> RHSExprs,
784 ArrayRef<const Expr *> ReductionOps,
Alexey Bataev89e7e8e2015-06-17 06:21:39 +0000785 bool WithNowait, bool SimpleReduction);
Alexey Bataev8b8e2022015-04-27 05:22:09 +0000786
787 /// \brief Emit code for 'taskwait' directive.
788 virtual void emitTaskwaitCall(CodeGenFunction &CGF, SourceLocation Loc);
Alexey Bataev0f34da12015-07-02 04:17:07 +0000789
790 /// \brief Emit code for 'cancellation point' construct.
791 /// \param CancelRegion Region kind for which the cancellation point must be
792 /// emitted.
793 ///
794 virtual void emitCancellationPointCall(CodeGenFunction &CGF,
795 SourceLocation Loc,
796 OpenMPDirectiveKind CancelRegion);
Alexey Bataev7d5d33e2015-07-06 05:50:32 +0000797
798 /// \brief Emit code for 'cancel' construct.
Alexey Bataev87933c72015-09-18 08:07:34 +0000799 /// \param IfCond Condition in the associated 'if' clause, if it was
800 /// specified, nullptr otherwise.
Alexey Bataev7d5d33e2015-07-06 05:50:32 +0000801 /// \param CancelRegion Region kind for which the cancel must be emitted.
802 ///
803 virtual void emitCancelCall(CodeGenFunction &CGF, SourceLocation Loc,
Alexey Bataev87933c72015-09-18 08:07:34 +0000804 const Expr *IfCond,
Alexey Bataev7d5d33e2015-07-06 05:50:32 +0000805 OpenMPDirectiveKind CancelRegion);
Samuel Antaobed3c462015-10-02 16:14:20 +0000806
807 /// \brief Emit outilined function for 'target' directive.
808 /// \param D Directive to emit.
Samuel Antaoee8fb302016-01-06 13:42:12 +0000809 /// \param ParentName Name of the function that encloses the target region.
810 /// \param OutlinedFn Outlined function value to be defined by this call.
811 /// \param OutlinedFnID Outlined function ID value to be defined by this call.
812 /// \param IsOffloadEntry True if the outlined function is an offload entry.
813 /// An oulined function may not be an entry if, e.g. the if clause always
814 /// evaluates to false.
815 virtual void emitTargetOutlinedFunction(const OMPExecutableDirective &D,
816 StringRef ParentName,
817 llvm::Function *&OutlinedFn,
818 llvm::Constant *&OutlinedFnID,
Alexey Bataevf539faa2016-03-28 12:58:34 +0000819 bool IsOffloadEntry);
Samuel Antaobed3c462015-10-02 16:14:20 +0000820
821 /// \brief Emit the target offloading code associated with \a D. The emitted
822 /// code attempts offloading the execution to the device, an the event of
823 /// a failure it executes the host version outlined in \a OutlinedFn.
824 /// \param D Directive to emit.
825 /// \param OutlinedFn Host version of the code to be offloaded.
Samuel Antaoee8fb302016-01-06 13:42:12 +0000826 /// \param OutlinedFnID ID of host version of the code to be offloaded.
Samuel Antaobed3c462015-10-02 16:14:20 +0000827 /// \param IfCond Expression evaluated in if clause associated with the target
828 /// directive, or null if no if clause is used.
829 /// \param Device Expression evaluated in device clause associated with the
830 /// target directive, or null if no device clause is used.
831 /// \param CapturedVars Values captured in the current region.
832 virtual void emitTargetCall(CodeGenFunction &CGF,
833 const OMPExecutableDirective &D,
Samuel Antaoee8fb302016-01-06 13:42:12 +0000834 llvm::Value *OutlinedFn,
835 llvm::Value *OutlinedFnID, const Expr *IfCond,
Samuel Antaobed3c462015-10-02 16:14:20 +0000836 const Expr *Device,
837 ArrayRef<llvm::Value *> CapturedVars);
Samuel Antaoee8fb302016-01-06 13:42:12 +0000838
839 /// \brief Emit the target regions enclosed in \a GD function definition or
840 /// the function itself in case it is a valid device function. Returns true if
841 /// \a GD was dealt with successfully.
Nico Webera2abe8c2016-01-06 19:13:49 +0000842 /// \param GD Function to scan.
Samuel Antaoee8fb302016-01-06 13:42:12 +0000843 virtual bool emitTargetFunctions(GlobalDecl GD);
844
845 /// \brief Emit the global variable if it is a valid device global variable.
846 /// Returns true if \a GD was dealt with successfully.
847 /// \param GD Variable declaration to emit.
848 virtual bool emitTargetGlobalVariable(GlobalDecl GD);
849
850 /// \brief Emit the global \a GD if it is meaningful for the target. Returns
851 /// if it was emitted succesfully.
852 /// \param GD Global to scan.
853 virtual bool emitTargetGlobal(GlobalDecl GD);
854
855 /// \brief Creates the offloading descriptor in the event any target region
856 /// was emitted in the current module and return the function that registers
857 /// it.
858 virtual llvm::Function *emitRegistrationFunction();
Carlo Bertolli430d8ec2016-03-03 20:34:23 +0000859
860 /// \brief Emits code for teams call of the \a OutlinedFn with
861 /// variables captured in a record which address is stored in \a
862 /// CapturedStruct.
863 /// \param OutlinedFn Outlined function to be run by team masters. Type of
864 /// this function is void(*)(kmp_int32 *, kmp_int32, struct context_vars*).
865 /// \param CapturedVars A pointer to the record with the references to
866 /// variables used in \a OutlinedFn function.
867 ///
868 virtual void emitTeamsCall(CodeGenFunction &CGF,
869 const OMPExecutableDirective &D,
870 SourceLocation Loc, llvm::Value *OutlinedFn,
871 ArrayRef<llvm::Value *> CapturedVars);
872
873 /// \brief Emits call to void __kmpc_push_num_teams(ident_t *loc, kmp_int32
874 /// global_tid, kmp_int32 num_teams, kmp_int32 thread_limit) to generate code
875 /// for num_teams clause.
876 /// \param NumTeams An integer value of teams.
Simon Pilgrim1feae0d2016-03-05 22:35:55 +0000877 /// \param ThreadLimit An integer value of threads.
Carlo Bertolli430d8ec2016-03-03 20:34:23 +0000878 virtual void emitNumTeamsClause(CodeGenFunction &CGF, llvm::Value *NumTeams,
879 llvm::Value *ThreadLimit, SourceLocation Loc);
880
Alexey Bataev9959db52014-05-06 10:08:46 +0000881};
Alexey Bataev8cbe0a62015-02-26 10:27:34 +0000882
Alexey Bataev23b69422014-06-18 07:08:49 +0000883} // namespace CodeGen
884} // namespace clang
Alexey Bataev9959db52014-05-06 10:08:46 +0000885
886#endif