blob: 9057e5ec4c14475247ab2904ad46f3e8330b3d64 [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 Bataev7292c292016-04-25 12:22:29 +000017#include "CGValue.h"
Alexey Bataev62b63b12015-03-10 07:28:44 +000018#include "clang/AST/Type.h"
Alexander Musmanc6388682014-12-15 07:07:06 +000019#include "clang/Basic/OpenMPKinds.h"
Chandler Carruth0d9593d2015-01-14 11:29:14 +000020#include "clang/Basic/SourceLocation.h"
Alexey Bataev9959db52014-05-06 10:08:46 +000021#include "llvm/ADT/DenseMap.h"
Benjamin Kramer8fdba912016-02-02 14:24:21 +000022#include "llvm/ADT/SmallPtrSet.h"
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +000023#include "llvm/ADT/StringMap.h"
Benjamin Kramer8fdba912016-02-02 14:24:21 +000024#include "llvm/IR/Function.h"
Alexey Bataev97720002014-11-11 04:05:39 +000025#include "llvm/IR/ValueHandle.h"
Alexey Bataev18095712014-10-10 12:19:54 +000026
27namespace llvm {
28class ArrayType;
29class Constant;
Alexey Bataev18095712014-10-10 12:19:54 +000030class FunctionType;
Alexey Bataev97720002014-11-11 04:05:39 +000031class GlobalVariable;
Alexey Bataev18095712014-10-10 12:19:54 +000032class StructType;
33class Type;
34class Value;
35} // namespace llvm
Alexey Bataev9959db52014-05-06 10:08:46 +000036
Alexey Bataev9959db52014-05-06 10:08:46 +000037namespace clang {
Alexey Bataevcc37cc12014-11-20 04:34:54 +000038class Expr;
Samuel Antaoee8fb302016-01-06 13:42:12 +000039class GlobalDecl;
Alexey Bataev8b427062016-05-25 12:36:08 +000040class OMPDependClause;
Alexey Bataev18095712014-10-10 12:19:54 +000041class OMPExecutableDirective;
Alexey Bataev7292c292016-04-25 12:22:29 +000042class OMPLoopDirective;
Alexey Bataev18095712014-10-10 12:19:54 +000043class VarDecl;
Alexey Bataevc5b1d322016-03-04 09:22:22 +000044class OMPDeclareReductionDecl;
45class IdentifierInfo;
Alexey Bataev18095712014-10-10 12:19:54 +000046
Alexey Bataev9959db52014-05-06 10:08:46 +000047namespace CodeGen {
John McCall7f416cc2015-09-08 08:05:57 +000048class Address;
Alexey Bataev18095712014-10-10 12:19:54 +000049class CodeGenFunction;
50class CodeGenModule;
Alexey Bataev9959db52014-05-06 10:08:46 +000051
Alexey Bataev14fa1c62016-03-29 05:34:15 +000052/// A basic class for pre|post-action for advanced codegen sequence for OpenMP
53/// region.
54class PrePostActionTy {
55public:
56 explicit PrePostActionTy() {}
57 virtual void Enter(CodeGenFunction &CGF) {}
58 virtual void Exit(CodeGenFunction &CGF) {}
59 virtual ~PrePostActionTy() {}
60};
61
62/// Class provides a way to call simple version of codegen for OpenMP region, or
63/// an advanced with possible pre|post-actions in codegen.
64class RegionCodeGenTy final {
65 intptr_t CodeGen;
66 typedef void (*CodeGenTy)(intptr_t, CodeGenFunction &, PrePostActionTy &);
67 CodeGenTy Callback;
68 mutable PrePostActionTy *PrePostAction;
69 RegionCodeGenTy() = delete;
70 RegionCodeGenTy &operator=(const RegionCodeGenTy &) = delete;
71 template <typename Callable>
72 static void CallbackFn(intptr_t CodeGen, CodeGenFunction &CGF,
73 PrePostActionTy &Action) {
74 return (*reinterpret_cast<Callable *>(CodeGen))(CGF, Action);
75 }
76
77public:
78 template <typename Callable>
79 RegionCodeGenTy(
80 Callable &&CodeGen,
81 typename std::enable_if<
82 !std::is_same<typename std::remove_reference<Callable>::type,
83 RegionCodeGenTy>::value>::type * = nullptr)
84 : CodeGen(reinterpret_cast<intptr_t>(&CodeGen)),
85 Callback(CallbackFn<typename std::remove_reference<Callable>::type>),
86 PrePostAction(nullptr) {}
87 void setAction(PrePostActionTy &Action) const { PrePostAction = &Action; }
88 void operator()(CodeGenFunction &CGF) const;
89};
Alexey Bataev6f1ffc02015-04-10 04:50:10 +000090
Alexey Bataev24b5bae2016-04-28 09:23:51 +000091struct OMPTaskDataTy final {
92 SmallVector<const Expr *, 4> PrivateVars;
93 SmallVector<const Expr *, 4> PrivateCopies;
94 SmallVector<const Expr *, 4> FirstprivateVars;
95 SmallVector<const Expr *, 4> FirstprivateCopies;
96 SmallVector<const Expr *, 4> FirstprivateInits;
Alexey Bataevf93095a2016-05-05 08:46:22 +000097 SmallVector<const Expr *, 4> LastprivateVars;
98 SmallVector<const Expr *, 4> LastprivateCopies;
Alexey Bataev24b5bae2016-04-28 09:23:51 +000099 SmallVector<std::pair<OpenMPDependClauseKind, const Expr *>, 4> Dependences;
100 llvm::PointerIntPair<llvm::Value *, 1, bool> Final;
101 llvm::PointerIntPair<llvm::Value *, 1, bool> Schedule;
Alexey Bataev1e1e2862016-05-10 12:21:02 +0000102 llvm::PointerIntPair<llvm::Value *, 1, bool> Priority;
Alexey Bataev24b5bae2016-04-28 09:23:51 +0000103 unsigned NumberOfParts = 0;
104 bool Tied = true;
105 bool Nogroup = false;
106};
107
Alexey Bataev9959db52014-05-06 10:08:46 +0000108class CGOpenMPRuntime {
Arpith Chacko Jacob5c309e42016-03-22 01:48:56 +0000109protected:
Alexey Bataev9959db52014-05-06 10:08:46 +0000110 CodeGenModule &CGM;
Arpith Chacko Jacob5c309e42016-03-22 01:48:56 +0000111
112 /// \brief Creates offloading entry for the provided entry ID \a ID,
113 /// address \a Addr and size \a Size.
114 virtual void createOffloadEntry(llvm::Constant *ID, llvm::Constant *Addr,
115 uint64_t Size);
116
117 /// \brief Helper to emit outlined function for 'target' directive.
118 /// \param D Directive to emit.
119 /// \param ParentName Name of the function that encloses the target region.
120 /// \param OutlinedFn Outlined function value to be defined by this call.
121 /// \param OutlinedFnID Outlined function ID value to be defined by this call.
122 /// \param IsOffloadEntry True if the outlined function is an offload entry.
123 /// \param CodeGen Lambda codegen specific to an accelerator device.
124 /// An oulined function may not be an entry if, e.g. the if clause always
125 /// evaluates to false.
126 virtual void emitTargetOutlinedFunctionHelper(const OMPExecutableDirective &D,
127 StringRef ParentName,
128 llvm::Function *&OutlinedFn,
129 llvm::Constant *&OutlinedFnID,
130 bool IsOffloadEntry,
131 const RegionCodeGenTy &CodeGen);
132
133private:
Alexey Bataev9959db52014-05-06 10:08:46 +0000134 /// \brief Default const ident_t object used for initialization of all other
135 /// ident_t objects.
Alexey Bataevc5b1d322016-03-04 09:22:22 +0000136 llvm::Constant *DefaultOpenMPPSource = nullptr;
Alexey Bataev18095712014-10-10 12:19:54 +0000137 /// \brief Map of flags and corresponding default locations.
Alexey Bataev15007ba2014-05-07 06:18:01 +0000138 typedef llvm::DenseMap<unsigned, llvm::Value *> OpenMPDefaultLocMapTy;
139 OpenMPDefaultLocMapTy OpenMPDefaultLocMap;
Alexey Bataev50b3c952016-02-19 10:38:26 +0000140 Address getOrCreateDefaultLocation(unsigned Flags);
John McCall7f416cc2015-09-08 08:05:57 +0000141
Alexey Bataev14fa1c62016-03-29 05:34:15 +0000142 llvm::StructType *IdentTy = nullptr;
Alexey Bataev18095712014-10-10 12:19:54 +0000143 /// \brief Map for SourceLocation and OpenMP runtime library debug locations.
Alexey Bataevf002aca2014-05-30 05:48:40 +0000144 typedef llvm::DenseMap<unsigned, llvm::Value *> OpenMPDebugLocMapTy;
145 OpenMPDebugLocMapTy OpenMPDebugLocMap;
Alexey Bataev9959db52014-05-06 10:08:46 +0000146 /// \brief The type for a microtask which gets passed to __kmpc_fork_call().
147 /// Original representation is:
148 /// typedef void (kmpc_micro)(kmp_int32 global_tid, kmp_int32 bound_tid,...);
Alexey Bataev14fa1c62016-03-29 05:34:15 +0000149 llvm::FunctionType *Kmpc_MicroTy = nullptr;
Alexey Bataev18095712014-10-10 12:19:54 +0000150 /// \brief Stores debug location and ThreadID for the function.
151 struct DebugLocThreadIdTy {
152 llvm::Value *DebugLoc;
153 llvm::Value *ThreadID;
154 };
155 /// \brief Map of local debug location, ThreadId and functions.
156 typedef llvm::DenseMap<llvm::Function *, DebugLocThreadIdTy>
157 OpenMPLocThreadIDMapTy;
158 OpenMPLocThreadIDMapTy OpenMPLocThreadIDMap;
Alexey Bataevc5b1d322016-03-04 09:22:22 +0000159 /// Map of UDRs and corresponding combiner/initializer.
160 typedef llvm::DenseMap<const OMPDeclareReductionDecl *,
161 std::pair<llvm::Function *, llvm::Function *>>
162 UDRMapTy;
163 UDRMapTy UDRMap;
164 /// Map of functions and locally defined UDRs.
165 typedef llvm::DenseMap<llvm::Function *,
166 SmallVector<const OMPDeclareReductionDecl *, 4>>
167 FunctionUDRMapTy;
168 FunctionUDRMapTy FunctionUDRMap;
169 IdentifierInfo *In = nullptr;
170 IdentifierInfo *Out = nullptr;
171 IdentifierInfo *Priv = nullptr;
172 IdentifierInfo *Orig = nullptr;
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +0000173 /// \brief Type kmp_critical_name, originally defined as typedef kmp_int32
174 /// kmp_critical_name[8];
175 llvm::ArrayType *KmpCriticalNameTy;
Alexey Bataev97720002014-11-11 04:05:39 +0000176 /// \brief An ordered map of auto-generated variables to their unique names.
177 /// It stores variables with the following names: 1) ".gomp_critical_user_" +
178 /// <critical_section_name> + ".var" for "omp critical" directives; 2)
179 /// <mangled_name_for_global_var> + ".cache." for cache for threadprivate
180 /// variables.
181 llvm::StringMap<llvm::AssertingVH<llvm::Constant>, llvm::BumpPtrAllocator>
182 InternalVars;
Alexey Bataev62b63b12015-03-10 07:28:44 +0000183 /// \brief Type typedef kmp_int32 (* kmp_routine_entry_t)(kmp_int32, void *);
Alexey Bataevc5b1d322016-03-04 09:22:22 +0000184 llvm::Type *KmpRoutineEntryPtrTy = nullptr;
Alexey Bataev62b63b12015-03-10 07:28:44 +0000185 QualType KmpRoutineEntryPtrQTy;
Alexey Bataev8fc69dc2015-05-18 07:54:53 +0000186 /// \brief Type typedef struct kmp_task {
187 /// void * shareds; /**< pointer to block of pointers to
188 /// shared vars */
189 /// kmp_routine_entry_t routine; /**< pointer to routine to call for
190 /// executing task */
191 /// kmp_int32 part_id; /**< part id for the task */
192 /// kmp_routine_entry_t destructors; /* pointer to function to invoke
193 /// deconstructors of firstprivate C++ objects */
194 /// } kmp_task_t;
195 QualType KmpTaskTQTy;
Alexey Bataev1d2353d2015-06-24 11:01:36 +0000196 /// \brief Type typedef struct kmp_depend_info {
197 /// kmp_intptr_t base_addr;
198 /// size_t len;
199 /// struct {
200 /// bool in:1;
201 /// bool out:1;
202 /// } flags;
203 /// } kmp_depend_info_t;
204 QualType KmpDependInfoTy;
Alexey Bataev8b427062016-05-25 12:36:08 +0000205 /// struct kmp_dim { // loop bounds info casted to kmp_int64
206 /// kmp_int64 lo; // lower
207 /// kmp_int64 up; // upper
208 /// kmp_int64 st; // stride
209 /// };
210 QualType KmpDimTy;
Samuel Antaoee8fb302016-01-06 13:42:12 +0000211 /// \brief Type struct __tgt_offload_entry{
212 /// void *addr; // Pointer to the offload entry info.
213 /// // (function or global)
214 /// char *name; // Name of the function or global.
215 /// size_t size; // Size of the entry info (0 if it a function).
216 /// };
217 QualType TgtOffloadEntryQTy;
218 /// struct __tgt_device_image{
219 /// void *ImageStart; // Pointer to the target code start.
220 /// void *ImageEnd; // Pointer to the target code end.
221 /// // We also add the host entries to the device image, as it may be useful
222 /// // for the target runtime to have access to that information.
223 /// __tgt_offload_entry *EntriesBegin; // Begin of the table with all
224 /// // the entries.
225 /// __tgt_offload_entry *EntriesEnd; // End of the table with all the
226 /// // entries (non inclusive).
227 /// };
228 QualType TgtDeviceImageQTy;
229 /// struct __tgt_bin_desc{
230 /// int32_t NumDevices; // Number of devices supported.
231 /// __tgt_device_image *DeviceImages; // Arrays of device images
232 /// // (one per device).
233 /// __tgt_offload_entry *EntriesBegin; // Begin of the table with all the
234 /// // entries.
235 /// __tgt_offload_entry *EntriesEnd; // End of the table with all the
236 /// // entries (non inclusive).
237 /// };
238 QualType TgtBinaryDescriptorQTy;
239 /// \brief Entity that registers the offloading constants that were emitted so
240 /// far.
241 class OffloadEntriesInfoManagerTy {
242 CodeGenModule &CGM;
Alexey Bataev1d2353d2015-06-24 11:01:36 +0000243
Samuel Antaoee8fb302016-01-06 13:42:12 +0000244 /// \brief Number of entries registered so far.
245 unsigned OffloadingEntriesNum;
246
247 public:
248 /// \brief Base class of the entries info.
249 class OffloadEntryInfo {
250 public:
251 /// \brief Kind of a given entry. Currently, only target regions are
252 /// supported.
Reid Klecknerdc78f952016-01-11 20:55:16 +0000253 enum OffloadingEntryInfoKinds : unsigned {
Samuel Antaoee8fb302016-01-06 13:42:12 +0000254 // Entry is a target region.
255 OFFLOAD_ENTRY_INFO_TARGET_REGION = 0,
256 // Invalid entry info.
257 OFFLOAD_ENTRY_INFO_INVALID = ~0u
258 };
259
260 OffloadEntryInfo() : Order(~0u), Kind(OFFLOAD_ENTRY_INFO_INVALID) {}
261 explicit OffloadEntryInfo(OffloadingEntryInfoKinds Kind, unsigned Order)
262 : Order(Order), Kind(Kind) {}
263
264 bool isValid() const { return Order != ~0u; }
265 unsigned getOrder() const { return Order; }
266 OffloadingEntryInfoKinds getKind() const { return Kind; }
267 static bool classof(const OffloadEntryInfo *Info) { return true; }
268
269 protected:
270 // \brief Order this entry was emitted.
271 unsigned Order;
272
273 OffloadingEntryInfoKinds Kind;
274 };
275
276 /// \brief Return true if a there are no entries defined.
277 bool empty() const;
278 /// \brief Return number of entries defined so far.
279 unsigned size() const { return OffloadingEntriesNum; }
280 OffloadEntriesInfoManagerTy(CodeGenModule &CGM)
281 : CGM(CGM), OffloadingEntriesNum(0) {}
282
283 ///
284 /// Target region entries related.
285 ///
286 /// \brief Target region entries info.
287 class OffloadEntryInfoTargetRegion : public OffloadEntryInfo {
288 // \brief Address of the entity that has to be mapped for offloading.
289 llvm::Constant *Addr;
290 // \brief Address that can be used as the ID of the entry.
291 llvm::Constant *ID;
292
293 public:
294 OffloadEntryInfoTargetRegion()
295 : OffloadEntryInfo(OFFLOAD_ENTRY_INFO_TARGET_REGION, ~0u),
296 Addr(nullptr), ID(nullptr) {}
297 explicit OffloadEntryInfoTargetRegion(unsigned Order,
298 llvm::Constant *Addr,
299 llvm::Constant *ID)
300 : OffloadEntryInfo(OFFLOAD_ENTRY_INFO_TARGET_REGION, Order),
301 Addr(Addr), ID(ID) {}
302
303 llvm::Constant *getAddress() const { return Addr; }
304 llvm::Constant *getID() const { return ID; }
305 void setAddress(llvm::Constant *V) {
306 assert(!Addr && "Address as been set before!");
307 Addr = V;
308 }
309 void setID(llvm::Constant *V) {
310 assert(!ID && "ID as been set before!");
311 ID = V;
312 }
313 static bool classof(const OffloadEntryInfo *Info) {
314 return Info->getKind() == OFFLOAD_ENTRY_INFO_TARGET_REGION;
315 }
316 };
317 /// \brief Initialize target region entry.
318 void initializeTargetRegionEntryInfo(unsigned DeviceID, unsigned FileID,
319 StringRef ParentName, unsigned LineNum,
Samuel Antao2de62b02016-02-13 23:35:10 +0000320 unsigned Order);
Samuel Antaoee8fb302016-01-06 13:42:12 +0000321 /// \brief Register target region entry.
322 void registerTargetRegionEntryInfo(unsigned DeviceID, unsigned FileID,
323 StringRef ParentName, unsigned LineNum,
Samuel Antao2de62b02016-02-13 23:35:10 +0000324 llvm::Constant *Addr,
Samuel Antaoee8fb302016-01-06 13:42:12 +0000325 llvm::Constant *ID);
326 /// \brief Return true if a target region entry with the provided
327 /// information exists.
328 bool hasTargetRegionEntryInfo(unsigned DeviceID, unsigned FileID,
Samuel Antao2de62b02016-02-13 23:35:10 +0000329 StringRef ParentName, unsigned LineNum) const;
Samuel Antaoee8fb302016-01-06 13:42:12 +0000330 /// brief Applies action \a Action on all registered entries.
331 typedef llvm::function_ref<void(unsigned, unsigned, StringRef, unsigned,
Samuel Antao2de62b02016-02-13 23:35:10 +0000332 OffloadEntryInfoTargetRegion &)>
Samuel Antaoee8fb302016-01-06 13:42:12 +0000333 OffloadTargetRegionEntryInfoActTy;
334 void actOnTargetRegionEntriesInfo(
335 const OffloadTargetRegionEntryInfoActTy &Action);
336
337 private:
338 // Storage for target region entries kind. The storage is to be indexed by
Samuel Antao2de62b02016-02-13 23:35:10 +0000339 // file ID, device ID, parent function name and line number.
Samuel Antaoee8fb302016-01-06 13:42:12 +0000340 typedef llvm::DenseMap<unsigned, OffloadEntryInfoTargetRegion>
Samuel Antaoee8fb302016-01-06 13:42:12 +0000341 OffloadEntriesTargetRegionPerLine;
342 typedef llvm::StringMap<OffloadEntriesTargetRegionPerLine>
343 OffloadEntriesTargetRegionPerParentName;
344 typedef llvm::DenseMap<unsigned, OffloadEntriesTargetRegionPerParentName>
345 OffloadEntriesTargetRegionPerFile;
346 typedef llvm::DenseMap<unsigned, OffloadEntriesTargetRegionPerFile>
347 OffloadEntriesTargetRegionPerDevice;
348 typedef OffloadEntriesTargetRegionPerDevice OffloadEntriesTargetRegionTy;
349 OffloadEntriesTargetRegionTy OffloadEntriesTargetRegion;
350 };
351 OffloadEntriesInfoManagerTy OffloadEntriesInfoManager;
352
353 /// \brief Creates and registers offloading binary descriptor for the current
354 /// compilation unit. The function that does the registration is returned.
355 llvm::Function *createOffloadingBinaryDescriptorRegistration();
356
Samuel Antaoee8fb302016-01-06 13:42:12 +0000357 /// \brief Creates all the offload entries in the current compilation unit
358 /// along with the associated metadata.
359 void createOffloadEntriesAndInfoMetadata();
360
361 /// \brief Loads all the offload entries information from the host IR
362 /// metadata.
363 void loadOffloadInfoMetadata();
364
365 /// \brief Returns __tgt_offload_entry type.
366 QualType getTgtOffloadEntryQTy();
367
368 /// \brief Returns __tgt_device_image type.
369 QualType getTgtDeviceImageQTy();
370
371 /// \brief Returns __tgt_bin_desc type.
372 QualType getTgtBinaryDescriptorQTy();
373
374 /// \brief Start scanning from statement \a S and and emit all target regions
375 /// found along the way.
376 /// \param S Starting statement.
377 /// \param ParentName Name of the function declaration that is being scanned.
378 void scanForTargetRegionsFunctions(const Stmt *S, StringRef ParentName);
Alexey Bataev62b63b12015-03-10 07:28:44 +0000379
380 /// \brief Build type kmp_routine_entry_t (if not built yet).
381 void emitKmpRoutineEntryT(QualType KmpInt32Ty);
Alexey Bataev9959db52014-05-06 10:08:46 +0000382
Alexey Bataev9959db52014-05-06 10:08:46 +0000383 /// \brief Emits object of ident_t type with info for source location.
Alexey Bataev9959db52014-05-06 10:08:46 +0000384 /// \param Flags Flags for OpenMP location.
385 ///
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000386 llvm::Value *emitUpdateLocation(CodeGenFunction &CGF, SourceLocation Loc,
Alexey Bataev50b3c952016-02-19 10:38:26 +0000387 unsigned Flags = 0);
Alexey Bataev9959db52014-05-06 10:08:46 +0000388
Alexey Bataevd74d0602014-10-13 06:02:40 +0000389 /// \brief Returns pointer to ident_t type.
Alexey Bataev9959db52014-05-06 10:08:46 +0000390 llvm::Type *getIdentTyPointerTy();
391
Alexey Bataevd74d0602014-10-13 06:02:40 +0000392 /// \brief Returns pointer to kmpc_micro type.
Alexey Bataev9959db52014-05-06 10:08:46 +0000393 llvm::Type *getKmpc_MicroPointerTy();
394
395 /// \brief Returns specified OpenMP runtime function.
396 /// \param Function OpenMP runtime function.
397 /// \return Specified function.
Alexey Bataev50b3c952016-02-19 10:38:26 +0000398 llvm::Constant *createRuntimeFunction(unsigned Function);
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +0000399
Alexander Musman21212e42015-03-13 10:38:23 +0000400 /// \brief Returns __kmpc_for_static_init_* runtime function for the specified
401 /// size \a IVSize and sign \a IVSigned.
402 llvm::Constant *createForStaticInitFunction(unsigned IVSize, bool IVSigned);
403
Alexander Musman92bdaab2015-03-12 13:37:50 +0000404 /// \brief Returns __kmpc_dispatch_init_* runtime function for the specified
405 /// size \a IVSize and sign \a IVSigned.
406 llvm::Constant *createDispatchInitFunction(unsigned IVSize, bool IVSigned);
407
408 /// \brief Returns __kmpc_dispatch_next_* runtime function for the specified
409 /// size \a IVSize and sign \a IVSigned.
410 llvm::Constant *createDispatchNextFunction(unsigned IVSize, bool IVSigned);
411
Alexey Bataev98eb6e32015-04-22 11:15:40 +0000412 /// \brief Returns __kmpc_dispatch_fini_* runtime function for the specified
413 /// size \a IVSize and sign \a IVSigned.
414 llvm::Constant *createDispatchFiniFunction(unsigned IVSize, bool IVSigned);
415
Alexey Bataev97720002014-11-11 04:05:39 +0000416 /// \brief If the specified mangled name is not in the module, create and
417 /// return threadprivate cache object. This object is a pointer's worth of
418 /// storage that's reserved for use by the OpenMP runtime.
NAKAMURA Takumicdcbfba2014-11-11 07:58:06 +0000419 /// \param VD Threadprivate variable.
Alexey Bataev97720002014-11-11 04:05:39 +0000420 /// \return Cache variable for the specified threadprivate.
421 llvm::Constant *getOrCreateThreadPrivateCache(const VarDecl *VD);
422
Alexey Bataevd74d0602014-10-13 06:02:40 +0000423 /// \brief Emits address of the word in a memory where current thread id is
424 /// stored.
John McCall7f416cc2015-09-08 08:05:57 +0000425 virtual Address emitThreadIDAddress(CodeGenFunction &CGF, SourceLocation Loc);
Alexey Bataevd74d0602014-10-13 06:02:40 +0000426
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000427 /// \brief Gets thread id value for the current thread.
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000428 ///
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000429 llvm::Value *getThreadID(CodeGenFunction &CGF, SourceLocation Loc);
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000430
Alexey Bataev97720002014-11-11 04:05:39 +0000431 /// \brief Gets (if variable with the given name already exist) or creates
432 /// internal global variable with the specified Name. The created variable has
433 /// linkage CommonLinkage by default and is initialized by null value.
434 /// \param Ty Type of the global variable. If it is exist already the type
435 /// must be the same.
436 /// \param Name Name of the variable.
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000437 llvm::Constant *getOrCreateInternalVariable(llvm::Type *Ty,
Alexey Bataev97720002014-11-11 04:05:39 +0000438 const llvm::Twine &Name);
439
440 /// \brief Set of threadprivate variables with the generated initializer.
Benjamin Kramer8fdba912016-02-02 14:24:21 +0000441 llvm::SmallPtrSet<const VarDecl *, 4> ThreadPrivateWithDefinition;
Alexey Bataev97720002014-11-11 04:05:39 +0000442
443 /// \brief Emits initialization code for the threadprivate variables.
444 /// \param VDAddr Address of the global variable \a VD.
445 /// \param Ctor Pointer to a global init function for \a VD.
446 /// \param CopyCtor Pointer to a global copy function for \a VD.
447 /// \param Dtor Pointer to a global destructor function for \a VD.
448 /// \param Loc Location of threadprivate declaration.
John McCall7f416cc2015-09-08 08:05:57 +0000449 void emitThreadPrivateVarInit(CodeGenFunction &CGF, Address VDAddr,
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000450 llvm::Value *Ctor, llvm::Value *CopyCtor,
451 llvm::Value *Dtor, SourceLocation Loc);
Alexey Bataev97720002014-11-11 04:05:39 +0000452
Alexey Bataev75ddfab2014-12-01 11:32:38 +0000453 /// \brief Returns corresponding lock object for the specified critical region
454 /// name. If the lock object does not exist it is created, otherwise the
455 /// reference to the existing copy is returned.
456 /// \param CriticalName Name of the critical region.
457 ///
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000458 llvm::Value *getCriticalRegionLock(StringRef CriticalName);
Alexey Bataev75ddfab2014-12-01 11:32:38 +0000459
Alexey Bataev24b5bae2016-04-28 09:23:51 +0000460 struct TaskResultTy {
461 llvm::Value *NewTask = nullptr;
462 llvm::Value *TaskEntry = nullptr;
463 llvm::Value *NewTaskNewTaskTTy = nullptr;
Alexey Bataev7292c292016-04-25 12:22:29 +0000464 LValue TDBase;
Alexey Bataev24b5bae2016-04-28 09:23:51 +0000465 RecordDecl *KmpTaskTQTyRD = nullptr;
Alexey Bataevf93095a2016-05-05 08:46:22 +0000466 llvm::Value *TaskDupFn = nullptr;
Alexey Bataev7292c292016-04-25 12:22:29 +0000467 };
468 /// Emit task region for the task directive. The task region is emitted in
469 /// several steps:
470 /// 1. Emit a call to kmp_task_t *__kmpc_omp_task_alloc(ident_t *, kmp_int32
471 /// gtid, kmp_int32 flags, size_t sizeof_kmp_task_t, size_t sizeof_shareds,
472 /// kmp_routine_entry_t *task_entry). Here task_entry is a pointer to the
473 /// function:
474 /// kmp_int32 .omp_task_entry.(kmp_int32 gtid, kmp_task_t *tt) {
475 /// TaskFunction(gtid, tt->part_id, tt->shareds);
476 /// return 0;
477 /// }
478 /// 2. Copy a list of shared variables to field shareds of the resulting
479 /// structure kmp_task_t returned by the previous call (if any).
480 /// 3. Copy a pointer to destructions function to field destructions of the
481 /// resulting structure kmp_task_t.
482 /// \param D Current task directive.
Alexey Bataev7292c292016-04-25 12:22:29 +0000483 /// \param TaskFunction An LLVM function with type void (*)(i32 /*gtid*/, i32
484 /// /*part_id*/, captured_struct */*__context*/);
485 /// \param SharedsTy A type which contains references the shared variables.
486 /// \param Shareds Context with the list of shared variables from the \p
487 /// TaskFunction.
Alexey Bataev24b5bae2016-04-28 09:23:51 +0000488 /// \param Data Additional data for task generation like tiednsee, final
489 /// state, list of privates etc.
490 TaskResultTy emitTaskInit(CodeGenFunction &CGF, SourceLocation Loc,
491 const OMPExecutableDirective &D,
492 llvm::Value *TaskFunction, QualType SharedsTy,
493 Address Shareds, const OMPTaskDataTy &Data);
Alexey Bataev7292c292016-04-25 12:22:29 +0000494
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000495public:
496 explicit CGOpenMPRuntime(CodeGenModule &CGM);
Angel Garcia Gomez637d1e62015-10-20 13:23:58 +0000497 virtual ~CGOpenMPRuntime() {}
Alexey Bataev91797552015-03-18 04:13:55 +0000498 virtual void clear();
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000499
Alexey Bataevc5b1d322016-03-04 09:22:22 +0000500 /// Emit code for the specified user defined reduction construct.
501 virtual void emitUserDefinedReduction(CodeGenFunction *CGF,
502 const OMPDeclareReductionDecl *D);
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000503 /// Get combiner/initializer for the specified user-defined reduction, if any.
504 virtual std::pair<llvm::Function *, llvm::Function *>
505 getUserDefinedReduction(const OMPDeclareReductionDecl *D);
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000506 /// \brief Emits outlined function for the specified OpenMP parallel directive
507 /// \a D. This outlined function has type void(*)(kmp_int32 *ThreadID,
508 /// kmp_int32 BoundID, struct context_vars*).
Alexey Bataev18095712014-10-10 12:19:54 +0000509 /// \param D OpenMP directive.
510 /// \param ThreadIDVar Variable for thread id in the current OpenMP region.
Alexey Bataev81c7ea02015-07-03 09:56:58 +0000511 /// \param InnermostKind Kind of innermost directive (for simple directives it
512 /// is a directive itself, for combined - its innermost directive).
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000513 /// \param CodeGen Code generation sequence for the \a D directive.
Carlo Bertolli430d8ec2016-03-03 20:34:23 +0000514 virtual llvm::Value *emitParallelOrTeamsOutlinedFunction(
Alexey Bataev81c7ea02015-07-03 09:56:58 +0000515 const OMPExecutableDirective &D, const VarDecl *ThreadIDVar,
516 OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen);
Alexey Bataev18095712014-10-10 12:19:54 +0000517
Alexey Bataev62b63b12015-03-10 07:28:44 +0000518 /// \brief Emits outlined function for the OpenMP task directive \a D. This
Alexey Bataev48591dd2016-04-20 04:01:36 +0000519 /// outlined function has type void(*)(kmp_int32 ThreadID, struct task_t*
520 /// TaskT).
Alexey Bataev62b63b12015-03-10 07:28:44 +0000521 /// \param D OpenMP directive.
522 /// \param ThreadIDVar Variable for thread id in the current OpenMP region.
Alexey Bataev48591dd2016-04-20 04:01:36 +0000523 /// \param PartIDVar Variable for partition id in the current OpenMP untied
524 /// task region.
525 /// \param TaskTVar Variable for task_t argument.
Alexey Bataev81c7ea02015-07-03 09:56:58 +0000526 /// \param InnermostKind Kind of innermost directive (for simple directives it
527 /// is a directive itself, for combined - its innermost directive).
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000528 /// \param CodeGen Code generation sequence for the \a D directive.
Alexey Bataev48591dd2016-04-20 04:01:36 +0000529 /// \param Tied true if task is generated for tied task, false otherwise.
530 /// \param NumberOfParts Number of parts in untied task. Ignored for tied
531 /// tasks.
Alexey Bataev62b63b12015-03-10 07:28:44 +0000532 ///
Alexey Bataev81c7ea02015-07-03 09:56:58 +0000533 virtual llvm::Value *emitTaskOutlinedFunction(
534 const OMPExecutableDirective &D, const VarDecl *ThreadIDVar,
Alexey Bataev48591dd2016-04-20 04:01:36 +0000535 const VarDecl *PartIDVar, const VarDecl *TaskTVar,
536 OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen,
537 bool Tied, unsigned &NumberOfParts);
Alexey Bataev62b63b12015-03-10 07:28:44 +0000538
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000539 /// \brief Cleans up references to the objects in finished function.
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000540 ///
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000541 void functionFinished(CodeGenFunction &CGF);
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000542
Alexey Bataev1d677132015-04-22 13:57:31 +0000543 /// \brief Emits code for parallel or serial call of the \a OutlinedFn with
544 /// variables captured in a record which address is stored in \a
545 /// CapturedStruct.
Alexey Bataev18095712014-10-10 12:19:54 +0000546 /// \param OutlinedFn Outlined function to be run in parallel threads. Type of
Alexey Bataev62b63b12015-03-10 07:28:44 +0000547 /// this function is void(*)(kmp_int32 *, kmp_int32, struct context_vars*).
NAKAMURA Takumi62f0eb52015-09-11 08:13:32 +0000548 /// \param CapturedVars A pointer to the record with the references to
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000549 /// variables used in \a OutlinedFn function.
Alexey Bataev1d677132015-04-22 13:57:31 +0000550 /// \param IfCond Condition in the associated 'if' clause, if it was
551 /// specified, nullptr otherwise.
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000552 ///
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000553 virtual void emitParallelCall(CodeGenFunction &CGF, SourceLocation Loc,
554 llvm::Value *OutlinedFn,
Alexey Bataev2377fe92015-09-10 08:12:02 +0000555 ArrayRef<llvm::Value *> CapturedVars,
556 const Expr *IfCond);
Alexey Bataevd74d0602014-10-13 06:02:40 +0000557
Alexey Bataev75ddfab2014-12-01 11:32:38 +0000558 /// \brief Emits a critical region.
Alexey Bataev18095712014-10-10 12:19:54 +0000559 /// \param CriticalName Name of the critical region.
Alexey Bataev75ddfab2014-12-01 11:32:38 +0000560 /// \param CriticalOpGen Generator for the statement associated with the given
561 /// critical region.
Alexey Bataevfc57d162015-12-15 10:55:09 +0000562 /// \param Hint Value of the 'hint' clause (optional).
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000563 virtual void emitCriticalRegion(CodeGenFunction &CGF, StringRef CriticalName,
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000564 const RegionCodeGenTy &CriticalOpGen,
Alexey Bataevfc57d162015-12-15 10:55:09 +0000565 SourceLocation Loc,
566 const Expr *Hint = nullptr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000567
Alexey Bataev8d690652014-12-04 07:23:53 +0000568 /// \brief Emits a master region.
569 /// \param MasterOpGen Generator for the statement associated with the given
570 /// master region.
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000571 virtual void emitMasterRegion(CodeGenFunction &CGF,
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000572 const RegionCodeGenTy &MasterOpGen,
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000573 SourceLocation Loc);
Alexey Bataev8d690652014-12-04 07:23:53 +0000574
Alexey Bataev9f797f32015-02-05 05:57:51 +0000575 /// \brief Emits code for a taskyield directive.
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000576 virtual void emitTaskyieldCall(CodeGenFunction &CGF, SourceLocation Loc);
Alexey Bataev9f797f32015-02-05 05:57:51 +0000577
Alexey Bataevc30dd2d2015-06-18 12:14:09 +0000578 /// \brief Emit a taskgroup region.
579 /// \param TaskgroupOpGen Generator for the statement associated with the
580 /// given taskgroup region.
581 virtual void emitTaskgroupRegion(CodeGenFunction &CGF,
582 const RegionCodeGenTy &TaskgroupOpGen,
583 SourceLocation Loc);
584
Alexey Bataev6956e2e2015-02-05 06:35:41 +0000585 /// \brief Emits a single region.
586 /// \param SingleOpGen Generator for the statement associated with the given
587 /// single region.
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000588 virtual void emitSingleRegion(CodeGenFunction &CGF,
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000589 const RegionCodeGenTy &SingleOpGen,
Alexey Bataeva63048e2015-03-23 06:18:07 +0000590 SourceLocation Loc,
591 ArrayRef<const Expr *> CopyprivateVars,
Alexey Bataev420d45b2015-04-14 05:11:24 +0000592 ArrayRef<const Expr *> DestExprs,
Alexey Bataeva63048e2015-03-23 06:18:07 +0000593 ArrayRef<const Expr *> SrcExprs,
Alexey Bataeva63048e2015-03-23 06:18:07 +0000594 ArrayRef<const Expr *> AssignmentOps);
Alexey Bataev6956e2e2015-02-05 06:35:41 +0000595
Alexey Bataev98eb6e32015-04-22 11:15:40 +0000596 /// \brief Emit an ordered region.
597 /// \param OrderedOpGen Generator for the statement associated with the given
Alexey Bataevc30dd2d2015-06-18 12:14:09 +0000598 /// ordered region.
Alexey Bataev98eb6e32015-04-22 11:15:40 +0000599 virtual void emitOrderedRegion(CodeGenFunction &CGF,
600 const RegionCodeGenTy &OrderedOpGen,
Alexey Bataev5f600d62015-09-29 03:48:57 +0000601 SourceLocation Loc, bool IsThreads);
Alexey Bataev98eb6e32015-04-22 11:15:40 +0000602
Alexey Bataevf2685682015-03-30 04:30:22 +0000603 /// \brief Emit an implicit/explicit barrier for OpenMP threads.
604 /// \param Kind Directive for which this implicit barrier call must be
605 /// generated. Must be OMPD_barrier for explicit barrier generation.
Alexey Bataev25e5b442015-09-15 12:52:43 +0000606 /// \param EmitChecks true if need to emit checks for cancellation barriers.
607 /// \param ForceSimpleCall true simple barrier call must be emitted, false if
608 /// runtime class decides which one to emit (simple or with cancellation
609 /// checks).
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000610 ///
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000611 virtual void emitBarrierCall(CodeGenFunction &CGF, SourceLocation Loc,
Alexey Bataev81c7ea02015-07-03 09:56:58 +0000612 OpenMPDirectiveKind Kind,
Alexey Bataev25e5b442015-09-15 12:52:43 +0000613 bool EmitChecks = true,
614 bool ForceSimpleCall = false);
Alexey Bataevb2059782014-10-13 08:23:51 +0000615
Alexander Musmanc6388682014-12-15 07:07:06 +0000616 /// \brief Check if the specified \a ScheduleKind is static non-chunked.
617 /// This kind of worksharing directive is emitted without outer loop.
618 /// \param ScheduleKind Schedule kind specified in the 'schedule' clause.
619 /// \param Chunked True if chunk is specified in the clause.
620 ///
621 virtual bool isStaticNonchunked(OpenMPScheduleClauseKind ScheduleKind,
622 bool Chunked) const;
623
Carlo Bertollifc35ad22016-03-07 16:04:49 +0000624 /// \brief Check if the specified \a ScheduleKind is static non-chunked.
625 /// This kind of distribute directive is emitted without outer loop.
626 /// \param ScheduleKind Schedule kind specified in the 'dist_schedule' clause.
627 /// \param Chunked True if chunk is specified in the clause.
628 ///
629 virtual bool isStaticNonchunked(OpenMPDistScheduleClauseKind ScheduleKind,
630 bool Chunked) const;
631
Alexander Musmandf7a8e22015-01-22 08:49:35 +0000632 /// \brief Check if the specified \a ScheduleKind is dynamic.
633 /// This kind of worksharing directive is emitted without outer loop.
634 /// \param ScheduleKind Schedule Kind specified in the 'schedule' clause.
635 ///
636 virtual bool isDynamic(OpenMPScheduleClauseKind ScheduleKind) const;
637
NAKAMURA Takumiff7a9252015-09-08 09:42:41 +0000638 virtual void emitForDispatchInit(CodeGenFunction &CGF, SourceLocation Loc,
Alexey Bataev9ebd7422016-05-10 09:57:36 +0000639 const OpenMPScheduleTy &ScheduleKind,
640 unsigned IVSize, bool IVSigned, bool Ordered,
641 llvm::Value *UB,
NAKAMURA Takumiff7a9252015-09-08 09:42:41 +0000642 llvm::Value *Chunk = nullptr);
643
Alexander Musmanc6388682014-12-15 07:07:06 +0000644 /// \brief Call the appropriate runtime routine to initialize it before start
645 /// of loop.
646 ///
647 /// Depending on the loop schedule, it is nesessary to call some runtime
648 /// routine before start of the OpenMP loop to get the loop upper / lower
649 /// bounds \a LB and \a UB and stride \a ST.
650 ///
651 /// \param CGF Reference to current CodeGenFunction.
652 /// \param Loc Clang source location.
Alexey Bataev9ebd7422016-05-10 09:57:36 +0000653 /// \param ScheduleKind Schedule kind, specified by the 'schedule' clause.
Alexander Musmanc6388682014-12-15 07:07:06 +0000654 /// \param IVSize Size of the iteration variable in bits.
655 /// \param IVSigned Sign of the interation variable.
Alexey Bataevd7589ffe2015-05-20 13:12:48 +0000656 /// \param Ordered true if loop is ordered, false otherwise.
Alexander Musmanc6388682014-12-15 07:07:06 +0000657 /// \param IL Address of the output variable in which the flag of the
658 /// last iteration is returned.
659 /// \param LB Address of the output variable in which the lower iteration
660 /// number is returned.
661 /// \param UB Address of the output variable in which the upper iteration
662 /// number is returned.
663 /// \param ST Address of the output variable in which the stride value is
664 /// returned nesessary to generated the static_chunked scheduled loop.
665 /// \param Chunk Value of the chunk for the static_chunked scheduled loop.
666 /// For the default (nullptr) value, the chunk 1 will be used.
667 ///
John McCall7f416cc2015-09-08 08:05:57 +0000668 virtual void emitForStaticInit(CodeGenFunction &CGF, SourceLocation Loc,
Alexey Bataev9ebd7422016-05-10 09:57:36 +0000669 const OpenMPScheduleTy &ScheduleKind,
John McCall7f416cc2015-09-08 08:05:57 +0000670 unsigned IVSize, bool IVSigned, bool Ordered,
Alexey Bataev9ebd7422016-05-10 09:57:36 +0000671 Address IL, Address LB, Address UB, Address ST,
John McCall7f416cc2015-09-08 08:05:57 +0000672 llvm::Value *Chunk = nullptr);
Alexander Musmanc6388682014-12-15 07:07:06 +0000673
Carlo Bertollifc35ad22016-03-07 16:04:49 +0000674 ///
675 /// \param CGF Reference to current CodeGenFunction.
676 /// \param Loc Clang source location.
677 /// \param SchedKind Schedule kind, specified by the 'dist_schedule' clause.
678 /// \param IVSize Size of the iteration variable in bits.
679 /// \param IVSigned Sign of the interation variable.
680 /// \param Ordered true if loop is ordered, false otherwise.
681 /// \param IL Address of the output variable in which the flag of the
682 /// last iteration is returned.
683 /// \param LB Address of the output variable in which the lower iteration
684 /// number is returned.
685 /// \param UB Address of the output variable in which the upper iteration
686 /// number is returned.
687 /// \param ST Address of the output variable in which the stride value is
688 /// returned nesessary to generated the static_chunked scheduled loop.
689 /// \param Chunk Value of the chunk for the static_chunked scheduled loop.
690 /// For the default (nullptr) value, the chunk 1 will be used.
691 ///
692 virtual void emitDistributeStaticInit(CodeGenFunction &CGF, SourceLocation Loc,
693 OpenMPDistScheduleClauseKind SchedKind,
694 unsigned IVSize, bool IVSigned,
695 bool Ordered, Address IL, Address LB,
696 Address UB, Address ST,
697 llvm::Value *Chunk = nullptr);
698
Alexander Musmanc6388682014-12-15 07:07:06 +0000699 /// \brief Call the appropriate runtime routine to notify that we finished
Alexey Bataev98eb6e32015-04-22 11:15:40 +0000700 /// iteration of the ordered loop with the dynamic scheduling.
701 ///
702 /// \param CGF Reference to current CodeGenFunction.
703 /// \param Loc Clang source location.
704 /// \param IVSize Size of the iteration variable in bits.
705 /// \param IVSigned Sign of the interation variable.
706 ///
Alexey Bataevd7589ffe2015-05-20 13:12:48 +0000707 virtual void emitForOrderedIterationEnd(CodeGenFunction &CGF,
708 SourceLocation Loc, unsigned IVSize,
709 bool IVSigned);
Alexey Bataev98eb6e32015-04-22 11:15:40 +0000710
711 /// \brief Call the appropriate runtime routine to notify that we finished
Alexander Musmanc6388682014-12-15 07:07:06 +0000712 /// all the work with current loop.
713 ///
714 /// \param CGF Reference to current CodeGenFunction.
715 /// \param Loc Clang source location.
Alexander Musmanc6388682014-12-15 07:07:06 +0000716 ///
Alexey Bataev98eb6e32015-04-22 11:15:40 +0000717 virtual void emitForStaticFinish(CodeGenFunction &CGF, SourceLocation Loc);
Alexander Musmanc6388682014-12-15 07:07:06 +0000718
Alexander Musman92bdaab2015-03-12 13:37:50 +0000719 /// Call __kmpc_dispatch_next(
720 /// ident_t *loc, kmp_int32 tid, kmp_int32 *p_lastiter,
721 /// kmp_int[32|64] *p_lower, kmp_int[32|64] *p_upper,
722 /// kmp_int[32|64] *p_stride);
723 /// \param IVSize Size of the iteration variable in bits.
724 /// \param IVSigned Sign of the interation variable.
725 /// \param IL Address of the output variable in which the flag of the
726 /// last iteration is returned.
727 /// \param LB Address of the output variable in which the lower iteration
728 /// number is returned.
729 /// \param UB Address of the output variable in which the upper iteration
730 /// number is returned.
731 /// \param ST Address of the output variable in which the stride value is
732 /// returned.
733 virtual llvm::Value *emitForNext(CodeGenFunction &CGF, SourceLocation Loc,
734 unsigned IVSize, bool IVSigned,
John McCall7f416cc2015-09-08 08:05:57 +0000735 Address IL, Address LB,
736 Address UB, Address ST);
Alexander Musman92bdaab2015-03-12 13:37:50 +0000737
Alexey Bataevb2059782014-10-13 08:23:51 +0000738 /// \brief Emits call to void __kmpc_push_num_threads(ident_t *loc, kmp_int32
739 /// global_tid, kmp_int32 num_threads) to generate code for 'num_threads'
740 /// clause.
741 /// \param NumThreads An integer value of threads.
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000742 virtual void emitNumThreadsClause(CodeGenFunction &CGF,
743 llvm::Value *NumThreads,
744 SourceLocation Loc);
Alexey Bataev97720002014-11-11 04:05:39 +0000745
Alexey Bataev7f210c62015-06-18 13:40:03 +0000746 /// \brief Emit call to void __kmpc_push_proc_bind(ident_t *loc, kmp_int32
747 /// global_tid, int proc_bind) to generate code for 'proc_bind' clause.
748 virtual void emitProcBindClause(CodeGenFunction &CGF,
749 OpenMPProcBindClauseKind ProcBind,
750 SourceLocation Loc);
751
Alexey Bataev97720002014-11-11 04:05:39 +0000752 /// \brief Returns address of the threadprivate variable for the current
753 /// thread.
NAKAMURA Takumicdcbfba2014-11-11 07:58:06 +0000754 /// \param VD Threadprivate variable.
Alexey Bataev97720002014-11-11 04:05:39 +0000755 /// \param VDAddr Address of the global variable \a VD.
756 /// \param Loc Location of the reference to threadprivate var.
757 /// \return Address of the threadprivate variable for the current thread.
John McCall7f416cc2015-09-08 08:05:57 +0000758 virtual Address getAddrOfThreadPrivate(CodeGenFunction &CGF,
759 const VarDecl *VD,
760 Address VDAddr,
761 SourceLocation Loc);
Alexey Bataev97720002014-11-11 04:05:39 +0000762
763 /// \brief Emit a code for initialization of threadprivate variable. It emits
764 /// a call to runtime library which adds initial value to the newly created
765 /// threadprivate variable (if it is not constant) and registers destructor
766 /// for the variable (if any).
767 /// \param VD Threadprivate variable.
768 /// \param VDAddr Address of the global variable \a VD.
769 /// \param Loc Location of threadprivate declaration.
770 /// \param PerformInit true if initialization expression is not constant.
771 virtual llvm::Function *
John McCall7f416cc2015-09-08 08:05:57 +0000772 emitThreadPrivateVarDefinition(const VarDecl *VD, Address VDAddr,
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000773 SourceLocation Loc, bool PerformInit,
774 CodeGenFunction *CGF = nullptr);
Alexey Bataevcc37cc12014-11-20 04:34:54 +0000775
776 /// \brief Emit flush of the variables specified in 'omp flush' directive.
777 /// \param Vars List of variables to flush.
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000778 virtual void emitFlush(CodeGenFunction &CGF, ArrayRef<const Expr *> Vars,
779 SourceLocation Loc);
Alexey Bataev62b63b12015-03-10 07:28:44 +0000780
781 /// \brief Emit task region for the task directive. The task region is
Nico Weber20b0ce32015-04-28 18:19:18 +0000782 /// emitted in several steps:
Alexey Bataev62b63b12015-03-10 07:28:44 +0000783 /// 1. Emit a call to kmp_task_t *__kmpc_omp_task_alloc(ident_t *, kmp_int32
784 /// gtid, kmp_int32 flags, size_t sizeof_kmp_task_t, size_t sizeof_shareds,
785 /// kmp_routine_entry_t *task_entry). Here task_entry is a pointer to the
786 /// function:
787 /// kmp_int32 .omp_task_entry.(kmp_int32 gtid, kmp_task_t *tt) {
788 /// TaskFunction(gtid, tt->part_id, tt->shareds);
789 /// return 0;
790 /// }
791 /// 2. Copy a list of shared variables to field shareds of the resulting
792 /// structure kmp_task_t returned by the previous call (if any).
793 /// 3. Copy a pointer to destructions function to field destructions of the
794 /// resulting structure kmp_task_t.
795 /// 4. Emit a call to kmp_int32 __kmpc_omp_task(ident_t *, kmp_int32 gtid,
796 /// kmp_task_t *new_task), where new_task is a resulting structure from
797 /// previous items.
Alexey Bataev36c1eb92015-04-30 06:51:57 +0000798 /// \param D Current task directive.
Alexey Bataev62b63b12015-03-10 07:28:44 +0000799 /// \param TaskFunction An LLVM function with type void (*)(i32 /*gtid*/, i32
800 /// /*part_id*/, captured_struct */*__context*/);
801 /// \param SharedsTy A type which contains references the shared variables.
Alexey Bataev1d2353d2015-06-24 11:01:36 +0000802 /// \param Shareds Context with the list of shared variables from the \p
Alexey Bataev62b63b12015-03-10 07:28:44 +0000803 /// TaskFunction.
Alexey Bataev1d677132015-04-22 13:57:31 +0000804 /// \param IfCond Not a nullptr if 'if' clause was specified, nullptr
805 /// otherwise.
Alexey Bataev24b5bae2016-04-28 09:23:51 +0000806 /// \param Data Additional data for task generation like tiednsee, final
807 /// state, list of privates etc.
808 virtual void emitTaskCall(CodeGenFunction &CGF, SourceLocation Loc,
809 const OMPExecutableDirective &D,
810 llvm::Value *TaskFunction, QualType SharedsTy,
811 Address Shareds, const Expr *IfCond,
812 const OMPTaskDataTy &Data);
Alexey Bataev794ba0d2015-04-10 10:43:45 +0000813
Alexey Bataev7292c292016-04-25 12:22:29 +0000814 /// Emit task region for the taskloop directive. The taskloop region is
815 /// emitted in several steps:
816 /// 1. Emit a call to kmp_task_t *__kmpc_omp_task_alloc(ident_t *, kmp_int32
817 /// gtid, kmp_int32 flags, size_t sizeof_kmp_task_t, size_t sizeof_shareds,
818 /// kmp_routine_entry_t *task_entry). Here task_entry is a pointer to the
819 /// function:
820 /// kmp_int32 .omp_task_entry.(kmp_int32 gtid, kmp_task_t *tt) {
821 /// TaskFunction(gtid, tt->part_id, tt->shareds);
822 /// return 0;
823 /// }
824 /// 2. Copy a list of shared variables to field shareds of the resulting
825 /// structure kmp_task_t returned by the previous call (if any).
826 /// 3. Copy a pointer to destructions function to field destructions of the
827 /// resulting structure kmp_task_t.
828 /// 4. Emit a call to void __kmpc_taskloop(ident_t *loc, int gtid, kmp_task_t
829 /// *task, int if_val, kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st, int
830 /// nogroup, int sched, kmp_uint64 grainsize, void *task_dup ), where new_task
831 /// is a resulting structure from
832 /// previous items.
833 /// \param D Current task directive.
Alexey Bataev7292c292016-04-25 12:22:29 +0000834 /// \param TaskFunction An LLVM function with type void (*)(i32 /*gtid*/, i32
835 /// /*part_id*/, captured_struct */*__context*/);
836 /// \param SharedsTy A type which contains references the shared variables.
837 /// \param Shareds Context with the list of shared variables from the \p
838 /// TaskFunction.
839 /// \param IfCond Not a nullptr if 'if' clause was specified, nullptr
840 /// otherwise.
Alexey Bataev24b5bae2016-04-28 09:23:51 +0000841 /// \param Data Additional data for task generation like tiednsee, final
842 /// state, list of privates etc.
Alexey Bataev7292c292016-04-25 12:22:29 +0000843 virtual void emitTaskLoopCall(
844 CodeGenFunction &CGF, SourceLocation Loc, const OMPLoopDirective &D,
Alexey Bataev24b5bae2016-04-28 09:23:51 +0000845 llvm::Value *TaskFunction, QualType SharedsTy, Address Shareds,
846 const Expr *IfCond, const OMPTaskDataTy &Data);
Alexey Bataev7292c292016-04-25 12:22:29 +0000847
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000848 /// \brief Emit code for the directive that does not require outlining.
849 ///
Alexey Bataev81c7ea02015-07-03 09:56:58 +0000850 /// \param InnermostKind Kind of innermost directive (for simple directives it
851 /// is a directive itself, for combined - its innermost directive).
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000852 /// \param CodeGen Code generation sequence for the \a D directive.
Alexey Bataev25e5b442015-09-15 12:52:43 +0000853 /// \param HasCancel true if region has inner cancel directive, false
854 /// otherwise.
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000855 virtual void emitInlinedDirective(CodeGenFunction &CGF,
Alexey Bataev81c7ea02015-07-03 09:56:58 +0000856 OpenMPDirectiveKind InnermostKind,
Alexey Bataev25e5b442015-09-15 12:52:43 +0000857 const RegionCodeGenTy &CodeGen,
858 bool HasCancel = false);
Alexey Bataev794ba0d2015-04-10 10:43:45 +0000859 /// \brief Emit a code for reduction clause. Next code should be emitted for
860 /// reduction:
861 /// \code
862 ///
863 /// static kmp_critical_name lock = { 0 };
864 ///
865 /// void reduce_func(void *lhs[<n>], void *rhs[<n>]) {
866 /// ...
867 /// *(Type<i>*)lhs[i] = RedOp<i>(*(Type<i>*)lhs[i], *(Type<i>*)rhs[i]);
868 /// ...
869 /// }
870 ///
871 /// ...
872 /// void *RedList[<n>] = {&<RHSExprs>[0], ..., &<RHSExprs>[<n>-1]};
873 /// switch (__kmpc_reduce{_nowait}(<loc>, <gtid>, <n>, sizeof(RedList),
874 /// RedList, reduce_func, &<lock>)) {
875 /// case 1:
876 /// ...
877 /// <LHSExprs>[i] = RedOp<i>(*<LHSExprs>[i], *<RHSExprs>[i]);
878 /// ...
879 /// __kmpc_end_reduce{_nowait}(<loc>, <gtid>, &<lock>);
880 /// break;
881 /// case 2:
882 /// ...
883 /// Atomic(<LHSExprs>[i] = RedOp<i>(*<LHSExprs>[i], *<RHSExprs>[i]));
884 /// ...
885 /// break;
886 /// default:;
887 /// }
888 /// \endcode
889 ///
Alexey Bataevf24e7b12015-10-08 09:10:53 +0000890 /// \param Privates List of private copies for original reduction arguments.
Alexey Bataev794ba0d2015-04-10 10:43:45 +0000891 /// \param LHSExprs List of LHS in \a ReductionOps reduction operations.
892 /// \param RHSExprs List of RHS in \a ReductionOps reduction operations.
893 /// \param ReductionOps List of reduction operations in form 'LHS binop RHS'
894 /// or 'operator binop(LHS, RHS)'.
895 /// \param WithNowait true if parent directive has also nowait clause, false
896 /// otherwise.
897 virtual void emitReduction(CodeGenFunction &CGF, SourceLocation Loc,
Alexey Bataevf24e7b12015-10-08 09:10:53 +0000898 ArrayRef<const Expr *> Privates,
Alexey Bataev794ba0d2015-04-10 10:43:45 +0000899 ArrayRef<const Expr *> LHSExprs,
900 ArrayRef<const Expr *> RHSExprs,
901 ArrayRef<const Expr *> ReductionOps,
Alexey Bataev89e7e8e2015-06-17 06:21:39 +0000902 bool WithNowait, bool SimpleReduction);
Alexey Bataev8b8e2022015-04-27 05:22:09 +0000903
904 /// \brief Emit code for 'taskwait' directive.
905 virtual void emitTaskwaitCall(CodeGenFunction &CGF, SourceLocation Loc);
Alexey Bataev0f34da12015-07-02 04:17:07 +0000906
907 /// \brief Emit code for 'cancellation point' construct.
908 /// \param CancelRegion Region kind for which the cancellation point must be
909 /// emitted.
910 ///
911 virtual void emitCancellationPointCall(CodeGenFunction &CGF,
912 SourceLocation Loc,
913 OpenMPDirectiveKind CancelRegion);
Alexey Bataev7d5d33e2015-07-06 05:50:32 +0000914
915 /// \brief Emit code for 'cancel' construct.
Alexey Bataev87933c72015-09-18 08:07:34 +0000916 /// \param IfCond Condition in the associated 'if' clause, if it was
917 /// specified, nullptr otherwise.
Alexey Bataev7d5d33e2015-07-06 05:50:32 +0000918 /// \param CancelRegion Region kind for which the cancel must be emitted.
919 ///
920 virtual void emitCancelCall(CodeGenFunction &CGF, SourceLocation Loc,
Alexey Bataev87933c72015-09-18 08:07:34 +0000921 const Expr *IfCond,
Alexey Bataev7d5d33e2015-07-06 05:50:32 +0000922 OpenMPDirectiveKind CancelRegion);
Samuel Antaobed3c462015-10-02 16:14:20 +0000923
924 /// \brief Emit outilined function for 'target' directive.
925 /// \param D Directive to emit.
Samuel Antaoee8fb302016-01-06 13:42:12 +0000926 /// \param ParentName Name of the function that encloses the target region.
927 /// \param OutlinedFn Outlined function value to be defined by this call.
928 /// \param OutlinedFnID Outlined function ID value to be defined by this call.
929 /// \param IsOffloadEntry True if the outlined function is an offload entry.
Alexey Bataev14fa1c62016-03-29 05:34:15 +0000930 /// \param CodeGen Code generation sequence for the \a D directive.
Samuel Antaoee8fb302016-01-06 13:42:12 +0000931 /// An oulined function may not be an entry if, e.g. the if clause always
932 /// evaluates to false.
933 virtual void emitTargetOutlinedFunction(const OMPExecutableDirective &D,
934 StringRef ParentName,
935 llvm::Function *&OutlinedFn,
936 llvm::Constant *&OutlinedFnID,
Alexey Bataev14fa1c62016-03-29 05:34:15 +0000937 bool IsOffloadEntry,
938 const RegionCodeGenTy &CodeGen);
Samuel Antaobed3c462015-10-02 16:14:20 +0000939
940 /// \brief Emit the target offloading code associated with \a D. The emitted
941 /// code attempts offloading the execution to the device, an the event of
942 /// a failure it executes the host version outlined in \a OutlinedFn.
943 /// \param D Directive to emit.
944 /// \param OutlinedFn Host version of the code to be offloaded.
Samuel Antaoee8fb302016-01-06 13:42:12 +0000945 /// \param OutlinedFnID ID of host version of the code to be offloaded.
Samuel Antaobed3c462015-10-02 16:14:20 +0000946 /// \param IfCond Expression evaluated in if clause associated with the target
947 /// directive, or null if no if clause is used.
948 /// \param Device Expression evaluated in device clause associated with the
949 /// target directive, or null if no device clause is used.
950 /// \param CapturedVars Values captured in the current region.
951 virtual void emitTargetCall(CodeGenFunction &CGF,
952 const OMPExecutableDirective &D,
Samuel Antaoee8fb302016-01-06 13:42:12 +0000953 llvm::Value *OutlinedFn,
954 llvm::Value *OutlinedFnID, const Expr *IfCond,
Samuel Antaobed3c462015-10-02 16:14:20 +0000955 const Expr *Device,
956 ArrayRef<llvm::Value *> CapturedVars);
Samuel Antaoee8fb302016-01-06 13:42:12 +0000957
958 /// \brief Emit the target regions enclosed in \a GD function definition or
959 /// the function itself in case it is a valid device function. Returns true if
960 /// \a GD was dealt with successfully.
Nico Webera2abe8c2016-01-06 19:13:49 +0000961 /// \param GD Function to scan.
Samuel Antaoee8fb302016-01-06 13:42:12 +0000962 virtual bool emitTargetFunctions(GlobalDecl GD);
963
964 /// \brief Emit the global variable if it is a valid device global variable.
965 /// Returns true if \a GD was dealt with successfully.
966 /// \param GD Variable declaration to emit.
967 virtual bool emitTargetGlobalVariable(GlobalDecl GD);
968
969 /// \brief Emit the global \a GD if it is meaningful for the target. Returns
970 /// if it was emitted succesfully.
971 /// \param GD Global to scan.
972 virtual bool emitTargetGlobal(GlobalDecl GD);
973
974 /// \brief Creates the offloading descriptor in the event any target region
975 /// was emitted in the current module and return the function that registers
976 /// it.
977 virtual llvm::Function *emitRegistrationFunction();
Carlo Bertolli430d8ec2016-03-03 20:34:23 +0000978
979 /// \brief Emits code for teams call of the \a OutlinedFn with
980 /// variables captured in a record which address is stored in \a
981 /// CapturedStruct.
982 /// \param OutlinedFn Outlined function to be run by team masters. Type of
983 /// this function is void(*)(kmp_int32 *, kmp_int32, struct context_vars*).
984 /// \param CapturedVars A pointer to the record with the references to
985 /// variables used in \a OutlinedFn function.
986 ///
987 virtual void emitTeamsCall(CodeGenFunction &CGF,
988 const OMPExecutableDirective &D,
989 SourceLocation Loc, llvm::Value *OutlinedFn,
990 ArrayRef<llvm::Value *> CapturedVars);
991
992 /// \brief Emits call to void __kmpc_push_num_teams(ident_t *loc, kmp_int32
993 /// global_tid, kmp_int32 num_teams, kmp_int32 thread_limit) to generate code
994 /// for num_teams clause.
Carlo Bertollic6872252016-04-04 15:55:02 +0000995 /// \param NumTeams An integer expression of teams.
996 /// \param ThreadLimit An integer expression of threads.
997 virtual void emitNumTeamsClause(CodeGenFunction &CGF, const Expr *NumTeams,
998 const Expr *ThreadLimit, SourceLocation Loc);
Samuel Antaodf158d52016-04-27 22:58:19 +0000999
Samuel Antaocc10b852016-07-28 14:23:26 +00001000 /// Struct that keeps all the relevant information that should be kept
1001 /// throughout a 'target data' region.
1002 class TargetDataInfo {
1003 /// Set to true if device pointer information have to be obtained.
1004 bool RequiresDevicePointerInfo = false;
1005
1006 public:
1007 /// The array of base pointer passed to the runtime library.
1008 llvm::Value *BasePointersArray = nullptr;
1009 /// The array of section pointers passed to the runtime library.
1010 llvm::Value *PointersArray = nullptr;
1011 /// The array of sizes passed to the runtime library.
1012 llvm::Value *SizesArray = nullptr;
1013 /// The array of map types passed to the runtime library.
1014 llvm::Value *MapTypesArray = nullptr;
1015 /// The total number of pointers passed to the runtime library.
1016 unsigned NumberOfPtrs = 0u;
1017 /// Map between the a declaration of a capture and the corresponding base
1018 /// pointer address where the runtime returns the device pointers.
1019 llvm::DenseMap<const ValueDecl *, Address> CaptureDeviceAddrMap;
1020
1021 explicit TargetDataInfo() {}
1022 explicit TargetDataInfo(bool RequiresDevicePointerInfo)
1023 : RequiresDevicePointerInfo(RequiresDevicePointerInfo) {}
1024 /// Clear information about the data arrays.
1025 void clearArrayInfo() {
1026 BasePointersArray = nullptr;
1027 PointersArray = nullptr;
1028 SizesArray = nullptr;
1029 MapTypesArray = nullptr;
1030 NumberOfPtrs = 0u;
1031 }
1032 /// Return true if the current target data information has valid arrays.
1033 bool isValid() {
1034 return BasePointersArray && PointersArray && SizesArray &&
1035 MapTypesArray && NumberOfPtrs;
1036 }
1037 bool requiresDevicePointerInfo() { return RequiresDevicePointerInfo; }
1038 };
1039
Samuel Antaodf158d52016-04-27 22:58:19 +00001040 /// \brief Emit the target data mapping code associated with \a D.
1041 /// \param D Directive to emit.
Samuel Antaocc10b852016-07-28 14:23:26 +00001042 /// \param IfCond Expression evaluated in if clause associated with the
1043 /// target directive, or null if no device clause is used.
Samuel Antaodf158d52016-04-27 22:58:19 +00001044 /// \param Device Expression evaluated in device clause associated with the
1045 /// target directive, or null if no device clause is used.
Samuel Antaocc10b852016-07-28 14:23:26 +00001046 /// \param Info A record used to store information that needs to be preserved
1047 /// until the region is closed.
Samuel Antaodf158d52016-04-27 22:58:19 +00001048 virtual void emitTargetDataCalls(CodeGenFunction &CGF,
1049 const OMPExecutableDirective &D,
1050 const Expr *IfCond, const Expr *Device,
Samuel Antaocc10b852016-07-28 14:23:26 +00001051 const RegionCodeGenTy &CodeGen,
1052 TargetDataInfo &Info);
Samuel Antaobd0ae2e2016-04-27 23:07:29 +00001053
Samuel Antao8d2d7302016-05-26 18:30:22 +00001054 /// \brief Emit the data mapping/movement code associated with the directive
1055 /// \a D that should be of the form 'target [{enter|exit} data | update]'.
Samuel Antaobd0ae2e2016-04-27 23:07:29 +00001056 /// \param D Directive to emit.
1057 /// \param IfCond Expression evaluated in if clause associated with the target
1058 /// directive, or null if no if clause is used.
1059 /// \param Device Expression evaluated in device clause associated with the
1060 /// target directive, or null if no device clause is used.
Samuel Antao8d2d7302016-05-26 18:30:22 +00001061 virtual void emitTargetDataStandAloneCall(CodeGenFunction &CGF,
1062 const OMPExecutableDirective &D,
1063 const Expr *IfCond,
1064 const Expr *Device);
Alexey Bataevc7a82b42016-05-06 09:40:08 +00001065
1066 /// Marks function \a Fn with properly mangled versions of vector functions.
1067 /// \param FD Function marked as 'declare simd'.
1068 /// \param Fn LLVM function that must be marked with 'declare simd'
1069 /// attributes.
1070 virtual void emitDeclareSimdFunction(const FunctionDecl *FD,
1071 llvm::Function *Fn);
Alexey Bataev8b427062016-05-25 12:36:08 +00001072
1073 /// Emit initialization for doacross loop nesting support.
1074 /// \param D Loop-based construct used in doacross nesting construct.
1075 virtual void emitDoacrossInit(CodeGenFunction &CGF,
1076 const OMPLoopDirective &D);
1077
1078 /// Emit code for doacross ordered directive with 'depend' clause.
1079 /// \param C 'depend' clause with 'sink|source' dependency kind.
1080 virtual void emitDoacrossOrdered(CodeGenFunction &CGF,
1081 const OMPDependClause *C);
Alexey Bataev9959db52014-05-06 10:08:46 +00001082};
Alexey Bataev8cbe0a62015-02-26 10:27:34 +00001083
Alexey Bataev23b69422014-06-18 07:08:49 +00001084} // namespace CodeGen
1085} // namespace clang
Alexey Bataev9959db52014-05-06 10:08:46 +00001086
1087#endif