blob: fb77243baec2e5a0ae2c3064224dd1f739dd0041 [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 Bataev5c40bec2017-07-13 13:36:14 +0000108/// Class intended to support codegen of all kind of the reduction clauses.
109class ReductionCodeGen {
110private:
Simon Pilgrim6c0eeff2017-07-13 17:34:44 +0000111 /// Data required for codegen of reduction clauses.
Alexey Bataev5c40bec2017-07-13 13:36:14 +0000112 struct ReductionData {
113 /// Reference to the original shared item.
114 const Expr *Ref = nullptr;
115 /// Helper expression for generation of private copy.
116 const Expr *Private = nullptr;
117 /// Helper expression for generation reduction operation.
118 const Expr *ReductionOp = nullptr;
119 ReductionData(const Expr *Ref, const Expr *Private, const Expr *ReductionOp)
120 : Ref(Ref), Private(Private), ReductionOp(ReductionOp) {}
121 };
122 /// List of reduction-based clauses.
123 SmallVector<ReductionData, 4> ClausesData;
124
125 /// List of addresses of original shared variables/expressions.
126 SmallVector<std::pair<LValue, LValue>, 4> SharedAddresses;
127 /// Sizes of the reduction items in chars.
128 SmallVector<llvm::Value *, 4> Sizes;
129 /// Base declarations for the reduction items.
130 SmallVector<const VarDecl *, 4> BaseDecls;
131 /// Emits lvalue for shared expresion.
132 LValue emitSharedLValue(CodeGenFunction &CGF, const Expr *E);
133 /// Emits upper bound for shared expression (if array section).
134 LValue emitSharedLValueUB(CodeGenFunction &CGF, const Expr *E);
135 /// Performs aggregate initialization.
136 /// \param N Number of reduction item in the common list.
137 /// \param PrivateAddr Address of the corresponding private item.
138 /// \param SharedLVal Addreiss of the original shared variable.
139 void emitAggregateInitialization(CodeGenFunction &CGF, unsigned N,
140 Address PrivateAddr, LValue SharedLVal);
141
142public:
143 ReductionCodeGen(ArrayRef<const Expr *> Shareds,
144 ArrayRef<const Expr *> Privates,
145 ArrayRef<const Expr *> ReductionOps);
146 /// Emits lvalue for a reduction item.
147 /// \param N Number of the reduction item.
148 void emitSharedLValue(CodeGenFunction &CGF, unsigned N);
149 /// Emits the code for the variable-modified type, if required.
150 /// \param N Number of the reduction item.
151 void emitAggregateType(CodeGenFunction &CGF, unsigned N);
152 /// Emits the code for the variable-modified type, if required.
153 /// \param N Number of the reduction item.
154 /// \param Size Size of the type in chars.
155 void emitAggregateType(CodeGenFunction &CGF, unsigned N, llvm::Value *Size);
156 /// Performs initialization of the private copy for the reduction item.
157 /// \param N Number of the reduction item.
158 /// \param PrivateAddr Address of the corresponding private item.
159 /// \param DefaultInit Default initialization sequence that should be
160 /// performed if no reduction specific initialization is found.
Simon Pilgrim6c0eeff2017-07-13 17:34:44 +0000161 /// \param SharedLVal Address of the original shared variable.
Alexey Bataev5c40bec2017-07-13 13:36:14 +0000162 void
163 emitInitialization(CodeGenFunction &CGF, unsigned N, Address PrivateAddr,
164 LValue SharedLVal,
165 llvm::function_ref<bool(CodeGenFunction &)> DefaultInit);
Simon Pilgrim6c0eeff2017-07-13 17:34:44 +0000166 /// Returns true if the private copy requires cleanups.
Alexey Bataev5c40bec2017-07-13 13:36:14 +0000167 bool needCleanups(unsigned N);
Simon Pilgrim6c0eeff2017-07-13 17:34:44 +0000168 /// Emits cleanup code for the reduction item.
Alexey Bataev5c40bec2017-07-13 13:36:14 +0000169 /// \param N Number of the reduction item.
170 /// \param PrivateAddr Address of the corresponding private item.
171 void emitCleanups(CodeGenFunction &CGF, unsigned N, Address PrivateAddr);
Simon Pilgrim6c0eeff2017-07-13 17:34:44 +0000172 /// Adjusts \p PrivatedAddr for using instead of the original variable
Alexey Bataev5c40bec2017-07-13 13:36:14 +0000173 /// address in normal operations.
174 /// \param N Number of the reduction item.
175 /// \param PrivateAddr Address of the corresponding private item.
176 Address adjustPrivateAddress(CodeGenFunction &CGF, unsigned N,
177 Address PrivateAddr);
178 /// Returns LValue for the reduction item.
179 LValue getSharedLValue(unsigned N) const { return SharedAddresses[N].first; }
180 /// Returns the size of the reduction item in chars, or nullptr, if the size
181 /// is a constant.
182 llvm::Value *getSizeInChars(unsigned N) const { return Sizes[N]; }
183 /// Returns the base declaration of the reduction item.
184 const VarDecl *getBaseDecl(unsigned N) const { return BaseDecls[N]; }
185};
186
Alexey Bataev9959db52014-05-06 10:08:46 +0000187class CGOpenMPRuntime {
Arpith Chacko Jacob5c309e42016-03-22 01:48:56 +0000188protected:
Alexey Bataev9959db52014-05-06 10:08:46 +0000189 CodeGenModule &CGM;
Arpith Chacko Jacob5c309e42016-03-22 01:48:56 +0000190
191 /// \brief Creates offloading entry for the provided entry ID \a ID,
Samuel Antaof83efdb2017-01-05 16:02:49 +0000192 /// address \a Addr, size \a Size, and flags \a Flags.
Arpith Chacko Jacob5c309e42016-03-22 01:48:56 +0000193 virtual void createOffloadEntry(llvm::Constant *ID, llvm::Constant *Addr,
Samuel Antaof83efdb2017-01-05 16:02:49 +0000194 uint64_t Size, int32_t Flags = 0);
Arpith Chacko Jacob5c309e42016-03-22 01:48:56 +0000195
196 /// \brief Helper to emit outlined function for 'target' directive.
197 /// \param D Directive to emit.
198 /// \param ParentName Name of the function that encloses the target region.
199 /// \param OutlinedFn Outlined function value to be defined by this call.
200 /// \param OutlinedFnID Outlined function ID value to be defined by this call.
201 /// \param IsOffloadEntry True if the outlined function is an offload entry.
202 /// \param CodeGen Lambda codegen specific to an accelerator device.
Simon Pilgrim6c0eeff2017-07-13 17:34:44 +0000203 /// An outlined function may not be an entry if, e.g. the if clause always
Arpith Chacko Jacob5c309e42016-03-22 01:48:56 +0000204 /// evaluates to false.
205 virtual void emitTargetOutlinedFunctionHelper(const OMPExecutableDirective &D,
206 StringRef ParentName,
207 llvm::Function *&OutlinedFn,
208 llvm::Constant *&OutlinedFnID,
209 bool IsOffloadEntry,
210 const RegionCodeGenTy &CodeGen);
211
Arpith Chacko Jacobbb36fe82017-01-10 15:42:51 +0000212 /// \brief Emits code for OpenMP 'if' clause using specified \a CodeGen
213 /// function. Here is the logic:
214 /// if (Cond) {
215 /// ThenGen();
216 /// } else {
217 /// ElseGen();
218 /// }
219 void emitOMPIfClause(CodeGenFunction &CGF, const Expr *Cond,
220 const RegionCodeGenTy &ThenGen,
221 const RegionCodeGenTy &ElseGen);
222
223 /// \brief Emits object of ident_t type with info for source location.
224 /// \param Flags Flags for OpenMP location.
225 ///
226 llvm::Value *emitUpdateLocation(CodeGenFunction &CGF, SourceLocation Loc,
227 unsigned Flags = 0);
228
229 /// \brief Returns pointer to ident_t type.
230 llvm::Type *getIdentTyPointerTy();
231
232 /// \brief Gets thread id value for the current thread.
233 ///
234 llvm::Value *getThreadID(CodeGenFunction &CGF, SourceLocation Loc);
235
236 /// \brief Get the function name of an outlined region.
237 // The name can be customized depending on the target.
238 //
239 virtual StringRef getOutlinedHelperName() const { return ".omp_outlined."; }
240
Arpith Chacko Jacob5c309e42016-03-22 01:48:56 +0000241private:
Alexey Bataev9959db52014-05-06 10:08:46 +0000242 /// \brief Default const ident_t object used for initialization of all other
243 /// ident_t objects.
Alexey Bataevc5b1d322016-03-04 09:22:22 +0000244 llvm::Constant *DefaultOpenMPPSource = nullptr;
Alexey Bataev18095712014-10-10 12:19:54 +0000245 /// \brief Map of flags and corresponding default locations.
Alexey Bataev15007ba2014-05-07 06:18:01 +0000246 typedef llvm::DenseMap<unsigned, llvm::Value *> OpenMPDefaultLocMapTy;
247 OpenMPDefaultLocMapTy OpenMPDefaultLocMap;
Alexey Bataev50b3c952016-02-19 10:38:26 +0000248 Address getOrCreateDefaultLocation(unsigned Flags);
John McCall7f416cc2015-09-08 08:05:57 +0000249
Alexey Bataev14fa1c62016-03-29 05:34:15 +0000250 llvm::StructType *IdentTy = nullptr;
Alexey Bataev18095712014-10-10 12:19:54 +0000251 /// \brief Map for SourceLocation and OpenMP runtime library debug locations.
Alexey Bataevf002aca2014-05-30 05:48:40 +0000252 typedef llvm::DenseMap<unsigned, llvm::Value *> OpenMPDebugLocMapTy;
253 OpenMPDebugLocMapTy OpenMPDebugLocMap;
Alexey Bataev9959db52014-05-06 10:08:46 +0000254 /// \brief The type for a microtask which gets passed to __kmpc_fork_call().
255 /// Original representation is:
256 /// typedef void (kmpc_micro)(kmp_int32 global_tid, kmp_int32 bound_tid,...);
Alexey Bataev14fa1c62016-03-29 05:34:15 +0000257 llvm::FunctionType *Kmpc_MicroTy = nullptr;
Alexey Bataev18095712014-10-10 12:19:54 +0000258 /// \brief Stores debug location and ThreadID for the function.
259 struct DebugLocThreadIdTy {
260 llvm::Value *DebugLoc;
261 llvm::Value *ThreadID;
262 };
263 /// \brief Map of local debug location, ThreadId and functions.
264 typedef llvm::DenseMap<llvm::Function *, DebugLocThreadIdTy>
265 OpenMPLocThreadIDMapTy;
266 OpenMPLocThreadIDMapTy OpenMPLocThreadIDMap;
Alexey Bataevc5b1d322016-03-04 09:22:22 +0000267 /// Map of UDRs and corresponding combiner/initializer.
268 typedef llvm::DenseMap<const OMPDeclareReductionDecl *,
269 std::pair<llvm::Function *, llvm::Function *>>
270 UDRMapTy;
271 UDRMapTy UDRMap;
272 /// Map of functions and locally defined UDRs.
273 typedef llvm::DenseMap<llvm::Function *,
274 SmallVector<const OMPDeclareReductionDecl *, 4>>
275 FunctionUDRMapTy;
276 FunctionUDRMapTy FunctionUDRMap;
277 IdentifierInfo *In = nullptr;
278 IdentifierInfo *Out = nullptr;
279 IdentifierInfo *Priv = nullptr;
280 IdentifierInfo *Orig = nullptr;
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +0000281 /// \brief Type kmp_critical_name, originally defined as typedef kmp_int32
282 /// kmp_critical_name[8];
283 llvm::ArrayType *KmpCriticalNameTy;
Alexey Bataev97720002014-11-11 04:05:39 +0000284 /// \brief An ordered map of auto-generated variables to their unique names.
285 /// It stores variables with the following names: 1) ".gomp_critical_user_" +
286 /// <critical_section_name> + ".var" for "omp critical" directives; 2)
287 /// <mangled_name_for_global_var> + ".cache." for cache for threadprivate
288 /// variables.
289 llvm::StringMap<llvm::AssertingVH<llvm::Constant>, llvm::BumpPtrAllocator>
290 InternalVars;
Alexey Bataev62b63b12015-03-10 07:28:44 +0000291 /// \brief Type typedef kmp_int32 (* kmp_routine_entry_t)(kmp_int32, void *);
Alexey Bataevc5b1d322016-03-04 09:22:22 +0000292 llvm::Type *KmpRoutineEntryPtrTy = nullptr;
Alexey Bataev62b63b12015-03-10 07:28:44 +0000293 QualType KmpRoutineEntryPtrQTy;
Alexey Bataev8fc69dc2015-05-18 07:54:53 +0000294 /// \brief Type typedef struct kmp_task {
295 /// void * shareds; /**< pointer to block of pointers to
296 /// shared vars */
297 /// kmp_routine_entry_t routine; /**< pointer to routine to call for
298 /// executing task */
299 /// kmp_int32 part_id; /**< part id for the task */
300 /// kmp_routine_entry_t destructors; /* pointer to function to invoke
301 /// deconstructors of firstprivate C++ objects */
302 /// } kmp_task_t;
303 QualType KmpTaskTQTy;
Alexey Bataev1d2353d2015-06-24 11:01:36 +0000304 /// \brief Type typedef struct kmp_depend_info {
305 /// kmp_intptr_t base_addr;
306 /// size_t len;
307 /// struct {
308 /// bool in:1;
309 /// bool out:1;
310 /// } flags;
311 /// } kmp_depend_info_t;
312 QualType KmpDependInfoTy;
Alexey Bataev8b427062016-05-25 12:36:08 +0000313 /// struct kmp_dim { // loop bounds info casted to kmp_int64
314 /// kmp_int64 lo; // lower
315 /// kmp_int64 up; // upper
316 /// kmp_int64 st; // stride
317 /// };
318 QualType KmpDimTy;
Samuel Antaoee8fb302016-01-06 13:42:12 +0000319 /// \brief Type struct __tgt_offload_entry{
320 /// void *addr; // Pointer to the offload entry info.
321 /// // (function or global)
322 /// char *name; // Name of the function or global.
323 /// size_t size; // Size of the entry info (0 if it a function).
324 /// };
325 QualType TgtOffloadEntryQTy;
326 /// struct __tgt_device_image{
327 /// void *ImageStart; // Pointer to the target code start.
328 /// void *ImageEnd; // Pointer to the target code end.
329 /// // We also add the host entries to the device image, as it may be useful
330 /// // for the target runtime to have access to that information.
331 /// __tgt_offload_entry *EntriesBegin; // Begin of the table with all
332 /// // the entries.
333 /// __tgt_offload_entry *EntriesEnd; // End of the table with all the
334 /// // entries (non inclusive).
335 /// };
336 QualType TgtDeviceImageQTy;
337 /// struct __tgt_bin_desc{
338 /// int32_t NumDevices; // Number of devices supported.
339 /// __tgt_device_image *DeviceImages; // Arrays of device images
340 /// // (one per device).
341 /// __tgt_offload_entry *EntriesBegin; // Begin of the table with all the
342 /// // entries.
343 /// __tgt_offload_entry *EntriesEnd; // End of the table with all the
344 /// // entries (non inclusive).
345 /// };
346 QualType TgtBinaryDescriptorQTy;
347 /// \brief Entity that registers the offloading constants that were emitted so
348 /// far.
349 class OffloadEntriesInfoManagerTy {
350 CodeGenModule &CGM;
Alexey Bataev1d2353d2015-06-24 11:01:36 +0000351
Samuel Antaoee8fb302016-01-06 13:42:12 +0000352 /// \brief Number of entries registered so far.
353 unsigned OffloadingEntriesNum;
354
355 public:
Samuel Antaof83efdb2017-01-05 16:02:49 +0000356 /// Base class of the entries info.
Samuel Antaoee8fb302016-01-06 13:42:12 +0000357 class OffloadEntryInfo {
358 public:
Samuel Antaof83efdb2017-01-05 16:02:49 +0000359 /// Kind of a given entry. Currently, only target regions are
Samuel Antaoee8fb302016-01-06 13:42:12 +0000360 /// supported.
Reid Klecknerdc78f952016-01-11 20:55:16 +0000361 enum OffloadingEntryInfoKinds : unsigned {
Samuel Antaoee8fb302016-01-06 13:42:12 +0000362 // Entry is a target region.
363 OFFLOAD_ENTRY_INFO_TARGET_REGION = 0,
364 // Invalid entry info.
365 OFFLOAD_ENTRY_INFO_INVALID = ~0u
366 };
367
Samuel Antaof83efdb2017-01-05 16:02:49 +0000368 OffloadEntryInfo()
369 : Flags(0), Order(~0u), Kind(OFFLOAD_ENTRY_INFO_INVALID) {}
370 explicit OffloadEntryInfo(OffloadingEntryInfoKinds Kind, unsigned Order,
371 int32_t Flags)
372 : Flags(Flags), Order(Order), Kind(Kind) {}
Samuel Antaoee8fb302016-01-06 13:42:12 +0000373
374 bool isValid() const { return Order != ~0u; }
375 unsigned getOrder() const { return Order; }
376 OffloadingEntryInfoKinds getKind() const { return Kind; }
Samuel Antaof83efdb2017-01-05 16:02:49 +0000377 int32_t getFlags() const { return Flags; }
378 void setFlags(int32_t NewFlags) { Flags = NewFlags; }
Samuel Antaoee8fb302016-01-06 13:42:12 +0000379 static bool classof(const OffloadEntryInfo *Info) { return true; }
380
Samuel Antaof83efdb2017-01-05 16:02:49 +0000381 private:
382 /// Flags associated with the device global.
383 int32_t Flags;
384
385 /// Order this entry was emitted.
Samuel Antaoee8fb302016-01-06 13:42:12 +0000386 unsigned Order;
387
388 OffloadingEntryInfoKinds Kind;
389 };
390
391 /// \brief Return true if a there are no entries defined.
392 bool empty() const;
393 /// \brief Return number of entries defined so far.
394 unsigned size() const { return OffloadingEntriesNum; }
395 OffloadEntriesInfoManagerTy(CodeGenModule &CGM)
396 : CGM(CGM), OffloadingEntriesNum(0) {}
397
398 ///
399 /// Target region entries related.
400 ///
401 /// \brief Target region entries info.
402 class OffloadEntryInfoTargetRegion : public OffloadEntryInfo {
403 // \brief Address of the entity that has to be mapped for offloading.
404 llvm::Constant *Addr;
405 // \brief Address that can be used as the ID of the entry.
406 llvm::Constant *ID;
407
408 public:
409 OffloadEntryInfoTargetRegion()
Samuel Antaof83efdb2017-01-05 16:02:49 +0000410 : OffloadEntryInfo(OFFLOAD_ENTRY_INFO_TARGET_REGION, ~0u,
411 /*Flags=*/0),
Samuel Antaoee8fb302016-01-06 13:42:12 +0000412 Addr(nullptr), ID(nullptr) {}
413 explicit OffloadEntryInfoTargetRegion(unsigned Order,
414 llvm::Constant *Addr,
Samuel Antaof83efdb2017-01-05 16:02:49 +0000415 llvm::Constant *ID, int32_t Flags)
416 : OffloadEntryInfo(OFFLOAD_ENTRY_INFO_TARGET_REGION, Order, Flags),
Samuel Antaoee8fb302016-01-06 13:42:12 +0000417 Addr(Addr), ID(ID) {}
418
419 llvm::Constant *getAddress() const { return Addr; }
420 llvm::Constant *getID() const { return ID; }
421 void setAddress(llvm::Constant *V) {
422 assert(!Addr && "Address as been set before!");
423 Addr = V;
424 }
425 void setID(llvm::Constant *V) {
426 assert(!ID && "ID as been set before!");
427 ID = V;
428 }
429 static bool classof(const OffloadEntryInfo *Info) {
430 return Info->getKind() == OFFLOAD_ENTRY_INFO_TARGET_REGION;
431 }
432 };
433 /// \brief Initialize target region entry.
434 void initializeTargetRegionEntryInfo(unsigned DeviceID, unsigned FileID,
435 StringRef ParentName, unsigned LineNum,
Samuel Antao2de62b02016-02-13 23:35:10 +0000436 unsigned Order);
Samuel Antaoee8fb302016-01-06 13:42:12 +0000437 /// \brief Register target region entry.
438 void registerTargetRegionEntryInfo(unsigned DeviceID, unsigned FileID,
439 StringRef ParentName, unsigned LineNum,
Samuel Antaof83efdb2017-01-05 16:02:49 +0000440 llvm::Constant *Addr, llvm::Constant *ID,
441 int32_t Flags);
Samuel Antaoee8fb302016-01-06 13:42:12 +0000442 /// \brief Return true if a target region entry with the provided
443 /// information exists.
444 bool hasTargetRegionEntryInfo(unsigned DeviceID, unsigned FileID,
Samuel Antao2de62b02016-02-13 23:35:10 +0000445 StringRef ParentName, unsigned LineNum) const;
Samuel Antaoee8fb302016-01-06 13:42:12 +0000446 /// brief Applies action \a Action on all registered entries.
447 typedef llvm::function_ref<void(unsigned, unsigned, StringRef, unsigned,
Samuel Antao2de62b02016-02-13 23:35:10 +0000448 OffloadEntryInfoTargetRegion &)>
Samuel Antaoee8fb302016-01-06 13:42:12 +0000449 OffloadTargetRegionEntryInfoActTy;
450 void actOnTargetRegionEntriesInfo(
451 const OffloadTargetRegionEntryInfoActTy &Action);
452
453 private:
454 // Storage for target region entries kind. The storage is to be indexed by
Samuel Antao2de62b02016-02-13 23:35:10 +0000455 // file ID, device ID, parent function name and line number.
Samuel Antaoee8fb302016-01-06 13:42:12 +0000456 typedef llvm::DenseMap<unsigned, OffloadEntryInfoTargetRegion>
Samuel Antaoee8fb302016-01-06 13:42:12 +0000457 OffloadEntriesTargetRegionPerLine;
458 typedef llvm::StringMap<OffloadEntriesTargetRegionPerLine>
459 OffloadEntriesTargetRegionPerParentName;
460 typedef llvm::DenseMap<unsigned, OffloadEntriesTargetRegionPerParentName>
461 OffloadEntriesTargetRegionPerFile;
462 typedef llvm::DenseMap<unsigned, OffloadEntriesTargetRegionPerFile>
463 OffloadEntriesTargetRegionPerDevice;
464 typedef OffloadEntriesTargetRegionPerDevice OffloadEntriesTargetRegionTy;
465 OffloadEntriesTargetRegionTy OffloadEntriesTargetRegion;
466 };
467 OffloadEntriesInfoManagerTy OffloadEntriesInfoManager;
468
469 /// \brief Creates and registers offloading binary descriptor for the current
470 /// compilation unit. The function that does the registration is returned.
471 llvm::Function *createOffloadingBinaryDescriptorRegistration();
472
Samuel Antaoee8fb302016-01-06 13:42:12 +0000473 /// \brief Creates all the offload entries in the current compilation unit
474 /// along with the associated metadata.
475 void createOffloadEntriesAndInfoMetadata();
476
477 /// \brief Loads all the offload entries information from the host IR
478 /// metadata.
479 void loadOffloadInfoMetadata();
480
481 /// \brief Returns __tgt_offload_entry type.
482 QualType getTgtOffloadEntryQTy();
483
484 /// \brief Returns __tgt_device_image type.
485 QualType getTgtDeviceImageQTy();
486
487 /// \brief Returns __tgt_bin_desc type.
488 QualType getTgtBinaryDescriptorQTy();
489
490 /// \brief Start scanning from statement \a S and and emit all target regions
491 /// found along the way.
492 /// \param S Starting statement.
493 /// \param ParentName Name of the function declaration that is being scanned.
494 void scanForTargetRegionsFunctions(const Stmt *S, StringRef ParentName);
Alexey Bataev62b63b12015-03-10 07:28:44 +0000495
496 /// \brief Build type kmp_routine_entry_t (if not built yet).
497 void emitKmpRoutineEntryT(QualType KmpInt32Ty);
Alexey Bataev9959db52014-05-06 10:08:46 +0000498
Alexey Bataevd74d0602014-10-13 06:02:40 +0000499 /// \brief Returns pointer to kmpc_micro type.
Alexey Bataev9959db52014-05-06 10:08:46 +0000500 llvm::Type *getKmpc_MicroPointerTy();
501
502 /// \brief Returns specified OpenMP runtime function.
503 /// \param Function OpenMP runtime function.
504 /// \return Specified function.
Alexey Bataev50b3c952016-02-19 10:38:26 +0000505 llvm::Constant *createRuntimeFunction(unsigned Function);
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +0000506
Alexander Musman21212e42015-03-13 10:38:23 +0000507 /// \brief Returns __kmpc_for_static_init_* runtime function for the specified
508 /// size \a IVSize and sign \a IVSigned.
509 llvm::Constant *createForStaticInitFunction(unsigned IVSize, bool IVSigned);
510
Alexander Musman92bdaab2015-03-12 13:37:50 +0000511 /// \brief Returns __kmpc_dispatch_init_* runtime function for the specified
512 /// size \a IVSize and sign \a IVSigned.
513 llvm::Constant *createDispatchInitFunction(unsigned IVSize, bool IVSigned);
514
515 /// \brief Returns __kmpc_dispatch_next_* runtime function for the specified
516 /// size \a IVSize and sign \a IVSigned.
517 llvm::Constant *createDispatchNextFunction(unsigned IVSize, bool IVSigned);
518
Alexey Bataev98eb6e32015-04-22 11:15:40 +0000519 /// \brief Returns __kmpc_dispatch_fini_* runtime function for the specified
520 /// size \a IVSize and sign \a IVSigned.
521 llvm::Constant *createDispatchFiniFunction(unsigned IVSize, bool IVSigned);
522
Alexey Bataev97720002014-11-11 04:05:39 +0000523 /// \brief If the specified mangled name is not in the module, create and
524 /// return threadprivate cache object. This object is a pointer's worth of
525 /// storage that's reserved for use by the OpenMP runtime.
NAKAMURA Takumicdcbfba2014-11-11 07:58:06 +0000526 /// \param VD Threadprivate variable.
Alexey Bataev97720002014-11-11 04:05:39 +0000527 /// \return Cache variable for the specified threadprivate.
528 llvm::Constant *getOrCreateThreadPrivateCache(const VarDecl *VD);
529
Alexey Bataevd74d0602014-10-13 06:02:40 +0000530 /// \brief Emits address of the word in a memory where current thread id is
531 /// stored.
John McCall7f416cc2015-09-08 08:05:57 +0000532 virtual Address emitThreadIDAddress(CodeGenFunction &CGF, SourceLocation Loc);
Alexey Bataevd74d0602014-10-13 06:02:40 +0000533
Alexey Bataev97720002014-11-11 04:05:39 +0000534 /// \brief Gets (if variable with the given name already exist) or creates
535 /// internal global variable with the specified Name. The created variable has
536 /// linkage CommonLinkage by default and is initialized by null value.
537 /// \param Ty Type of the global variable. If it is exist already the type
538 /// must be the same.
539 /// \param Name Name of the variable.
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000540 llvm::Constant *getOrCreateInternalVariable(llvm::Type *Ty,
Alexey Bataev97720002014-11-11 04:05:39 +0000541 const llvm::Twine &Name);
542
543 /// \brief Set of threadprivate variables with the generated initializer.
Benjamin Kramer8fdba912016-02-02 14:24:21 +0000544 llvm::SmallPtrSet<const VarDecl *, 4> ThreadPrivateWithDefinition;
Alexey Bataev97720002014-11-11 04:05:39 +0000545
546 /// \brief Emits initialization code for the threadprivate variables.
547 /// \param VDAddr Address of the global variable \a VD.
548 /// \param Ctor Pointer to a global init function for \a VD.
549 /// \param CopyCtor Pointer to a global copy function for \a VD.
550 /// \param Dtor Pointer to a global destructor function for \a VD.
551 /// \param Loc Location of threadprivate declaration.
John McCall7f416cc2015-09-08 08:05:57 +0000552 void emitThreadPrivateVarInit(CodeGenFunction &CGF, Address VDAddr,
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000553 llvm::Value *Ctor, llvm::Value *CopyCtor,
554 llvm::Value *Dtor, SourceLocation Loc);
Alexey Bataev97720002014-11-11 04:05:39 +0000555
Alexey Bataev75ddfab2014-12-01 11:32:38 +0000556 /// \brief Returns corresponding lock object for the specified critical region
557 /// name. If the lock object does not exist it is created, otherwise the
558 /// reference to the existing copy is returned.
559 /// \param CriticalName Name of the critical region.
560 ///
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000561 llvm::Value *getCriticalRegionLock(StringRef CriticalName);
Alexey Bataev75ddfab2014-12-01 11:32:38 +0000562
Alexey Bataev24b5bae2016-04-28 09:23:51 +0000563 struct TaskResultTy {
564 llvm::Value *NewTask = nullptr;
565 llvm::Value *TaskEntry = nullptr;
566 llvm::Value *NewTaskNewTaskTTy = nullptr;
Alexey Bataev7292c292016-04-25 12:22:29 +0000567 LValue TDBase;
Alexey Bataev24b5bae2016-04-28 09:23:51 +0000568 RecordDecl *KmpTaskTQTyRD = nullptr;
Alexey Bataevf93095a2016-05-05 08:46:22 +0000569 llvm::Value *TaskDupFn = nullptr;
Alexey Bataev7292c292016-04-25 12:22:29 +0000570 };
571 /// Emit task region for the task directive. The task region is emitted in
572 /// several steps:
573 /// 1. Emit a call to kmp_task_t *__kmpc_omp_task_alloc(ident_t *, kmp_int32
574 /// gtid, kmp_int32 flags, size_t sizeof_kmp_task_t, size_t sizeof_shareds,
575 /// kmp_routine_entry_t *task_entry). Here task_entry is a pointer to the
576 /// function:
577 /// kmp_int32 .omp_task_entry.(kmp_int32 gtid, kmp_task_t *tt) {
578 /// TaskFunction(gtid, tt->part_id, tt->shareds);
579 /// return 0;
580 /// }
581 /// 2. Copy a list of shared variables to field shareds of the resulting
582 /// structure kmp_task_t returned by the previous call (if any).
583 /// 3. Copy a pointer to destructions function to field destructions of the
584 /// resulting structure kmp_task_t.
585 /// \param D Current task directive.
Alexey Bataev7292c292016-04-25 12:22:29 +0000586 /// \param TaskFunction An LLVM function with type void (*)(i32 /*gtid*/, i32
587 /// /*part_id*/, captured_struct */*__context*/);
588 /// \param SharedsTy A type which contains references the shared variables.
589 /// \param Shareds Context with the list of shared variables from the \p
590 /// TaskFunction.
Alexey Bataev24b5bae2016-04-28 09:23:51 +0000591 /// \param Data Additional data for task generation like tiednsee, final
592 /// state, list of privates etc.
593 TaskResultTy emitTaskInit(CodeGenFunction &CGF, SourceLocation Loc,
594 const OMPExecutableDirective &D,
595 llvm::Value *TaskFunction, QualType SharedsTy,
596 Address Shareds, const OMPTaskDataTy &Data);
Alexey Bataev7292c292016-04-25 12:22:29 +0000597
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000598public:
599 explicit CGOpenMPRuntime(CodeGenModule &CGM);
Angel Garcia Gomez637d1e62015-10-20 13:23:58 +0000600 virtual ~CGOpenMPRuntime() {}
Alexey Bataev91797552015-03-18 04:13:55 +0000601 virtual void clear();
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000602
Alexey Bataevc5b1d322016-03-04 09:22:22 +0000603 /// Emit code for the specified user defined reduction construct.
604 virtual void emitUserDefinedReduction(CodeGenFunction *CGF,
605 const OMPDeclareReductionDecl *D);
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000606 /// Get combiner/initializer for the specified user-defined reduction, if any.
607 virtual std::pair<llvm::Function *, llvm::Function *>
608 getUserDefinedReduction(const OMPDeclareReductionDecl *D);
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +0000609
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000610 /// \brief Emits outlined function for the specified OpenMP parallel directive
611 /// \a D. This outlined function has type void(*)(kmp_int32 *ThreadID,
612 /// kmp_int32 BoundID, struct context_vars*).
Alexey Bataev18095712014-10-10 12:19:54 +0000613 /// \param D OpenMP directive.
614 /// \param ThreadIDVar Variable for thread id in the current OpenMP region.
Alexey Bataev81c7ea02015-07-03 09:56:58 +0000615 /// \param InnermostKind Kind of innermost directive (for simple directives it
616 /// is a directive itself, for combined - its innermost directive).
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000617 /// \param CodeGen Code generation sequence for the \a D directive.
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +0000618 virtual llvm::Value *emitParallelOutlinedFunction(
619 const OMPExecutableDirective &D, const VarDecl *ThreadIDVar,
620 OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen);
621
622 /// \brief Emits outlined function for the specified OpenMP teams directive
623 /// \a D. This outlined function has type void(*)(kmp_int32 *ThreadID,
624 /// kmp_int32 BoundID, struct context_vars*).
625 /// \param D OpenMP directive.
626 /// \param ThreadIDVar Variable for thread id in the current OpenMP region.
627 /// \param InnermostKind Kind of innermost directive (for simple directives it
628 /// is a directive itself, for combined - its innermost directive).
629 /// \param CodeGen Code generation sequence for the \a D directive.
630 virtual llvm::Value *emitTeamsOutlinedFunction(
Alexey Bataev81c7ea02015-07-03 09:56:58 +0000631 const OMPExecutableDirective &D, const VarDecl *ThreadIDVar,
632 OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen);
Alexey Bataev18095712014-10-10 12:19:54 +0000633
Alexey Bataev62b63b12015-03-10 07:28:44 +0000634 /// \brief Emits outlined function for the OpenMP task directive \a D. This
Alexey Bataev48591dd2016-04-20 04:01:36 +0000635 /// outlined function has type void(*)(kmp_int32 ThreadID, struct task_t*
636 /// TaskT).
Alexey Bataev62b63b12015-03-10 07:28:44 +0000637 /// \param D OpenMP directive.
638 /// \param ThreadIDVar Variable for thread id in the current OpenMP region.
Alexey Bataev48591dd2016-04-20 04:01:36 +0000639 /// \param PartIDVar Variable for partition id in the current OpenMP untied
640 /// task region.
641 /// \param TaskTVar Variable for task_t argument.
Alexey Bataev81c7ea02015-07-03 09:56:58 +0000642 /// \param InnermostKind Kind of innermost directive (for simple directives it
643 /// is a directive itself, for combined - its innermost directive).
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000644 /// \param CodeGen Code generation sequence for the \a D directive.
Alexey Bataev48591dd2016-04-20 04:01:36 +0000645 /// \param Tied true if task is generated for tied task, false otherwise.
646 /// \param NumberOfParts Number of parts in untied task. Ignored for tied
647 /// tasks.
Alexey Bataev62b63b12015-03-10 07:28:44 +0000648 ///
Alexey Bataev81c7ea02015-07-03 09:56:58 +0000649 virtual llvm::Value *emitTaskOutlinedFunction(
650 const OMPExecutableDirective &D, const VarDecl *ThreadIDVar,
Alexey Bataev48591dd2016-04-20 04:01:36 +0000651 const VarDecl *PartIDVar, const VarDecl *TaskTVar,
652 OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen,
653 bool Tied, unsigned &NumberOfParts);
Alexey Bataev62b63b12015-03-10 07:28:44 +0000654
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000655 /// \brief Cleans up references to the objects in finished function.
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000656 ///
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000657 void functionFinished(CodeGenFunction &CGF);
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000658
Alexey Bataev1d677132015-04-22 13:57:31 +0000659 /// \brief Emits code for parallel or serial call of the \a OutlinedFn with
660 /// variables captured in a record which address is stored in \a
661 /// CapturedStruct.
Alexey Bataev18095712014-10-10 12:19:54 +0000662 /// \param OutlinedFn Outlined function to be run in parallel threads. Type of
Alexey Bataev62b63b12015-03-10 07:28:44 +0000663 /// this function is void(*)(kmp_int32 *, kmp_int32, struct context_vars*).
NAKAMURA Takumi62f0eb52015-09-11 08:13:32 +0000664 /// \param CapturedVars A pointer to the record with the references to
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000665 /// variables used in \a OutlinedFn function.
Alexey Bataev1d677132015-04-22 13:57:31 +0000666 /// \param IfCond Condition in the associated 'if' clause, if it was
667 /// specified, nullptr otherwise.
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000668 ///
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000669 virtual void emitParallelCall(CodeGenFunction &CGF, SourceLocation Loc,
670 llvm::Value *OutlinedFn,
Alexey Bataev2377fe92015-09-10 08:12:02 +0000671 ArrayRef<llvm::Value *> CapturedVars,
672 const Expr *IfCond);
Alexey Bataevd74d0602014-10-13 06:02:40 +0000673
Alexey Bataev75ddfab2014-12-01 11:32:38 +0000674 /// \brief Emits a critical region.
Alexey Bataev18095712014-10-10 12:19:54 +0000675 /// \param CriticalName Name of the critical region.
Alexey Bataev75ddfab2014-12-01 11:32:38 +0000676 /// \param CriticalOpGen Generator for the statement associated with the given
677 /// critical region.
Alexey Bataevfc57d162015-12-15 10:55:09 +0000678 /// \param Hint Value of the 'hint' clause (optional).
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000679 virtual void emitCriticalRegion(CodeGenFunction &CGF, StringRef CriticalName,
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000680 const RegionCodeGenTy &CriticalOpGen,
Alexey Bataevfc57d162015-12-15 10:55:09 +0000681 SourceLocation Loc,
682 const Expr *Hint = nullptr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000683
Alexey Bataev8d690652014-12-04 07:23:53 +0000684 /// \brief Emits a master region.
685 /// \param MasterOpGen Generator for the statement associated with the given
686 /// master region.
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000687 virtual void emitMasterRegion(CodeGenFunction &CGF,
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000688 const RegionCodeGenTy &MasterOpGen,
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000689 SourceLocation Loc);
Alexey Bataev8d690652014-12-04 07:23:53 +0000690
Alexey Bataev9f797f32015-02-05 05:57:51 +0000691 /// \brief Emits code for a taskyield directive.
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000692 virtual void emitTaskyieldCall(CodeGenFunction &CGF, SourceLocation Loc);
Alexey Bataev9f797f32015-02-05 05:57:51 +0000693
Alexey Bataevc30dd2d2015-06-18 12:14:09 +0000694 /// \brief Emit a taskgroup region.
695 /// \param TaskgroupOpGen Generator for the statement associated with the
696 /// given taskgroup region.
697 virtual void emitTaskgroupRegion(CodeGenFunction &CGF,
698 const RegionCodeGenTy &TaskgroupOpGen,
699 SourceLocation Loc);
700
Alexey Bataev6956e2e2015-02-05 06:35:41 +0000701 /// \brief Emits a single region.
702 /// \param SingleOpGen Generator for the statement associated with the given
703 /// single region.
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000704 virtual void emitSingleRegion(CodeGenFunction &CGF,
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000705 const RegionCodeGenTy &SingleOpGen,
Alexey Bataeva63048e2015-03-23 06:18:07 +0000706 SourceLocation Loc,
707 ArrayRef<const Expr *> CopyprivateVars,
Alexey Bataev420d45b2015-04-14 05:11:24 +0000708 ArrayRef<const Expr *> DestExprs,
Alexey Bataeva63048e2015-03-23 06:18:07 +0000709 ArrayRef<const Expr *> SrcExprs,
Alexey Bataeva63048e2015-03-23 06:18:07 +0000710 ArrayRef<const Expr *> AssignmentOps);
Alexey Bataev6956e2e2015-02-05 06:35:41 +0000711
Alexey Bataev98eb6e32015-04-22 11:15:40 +0000712 /// \brief Emit an ordered region.
713 /// \param OrderedOpGen Generator for the statement associated with the given
Alexey Bataevc30dd2d2015-06-18 12:14:09 +0000714 /// ordered region.
Alexey Bataev98eb6e32015-04-22 11:15:40 +0000715 virtual void emitOrderedRegion(CodeGenFunction &CGF,
716 const RegionCodeGenTy &OrderedOpGen,
Alexey Bataev5f600d62015-09-29 03:48:57 +0000717 SourceLocation Loc, bool IsThreads);
Alexey Bataev98eb6e32015-04-22 11:15:40 +0000718
Alexey Bataevf2685682015-03-30 04:30:22 +0000719 /// \brief Emit an implicit/explicit barrier for OpenMP threads.
720 /// \param Kind Directive for which this implicit barrier call must be
721 /// generated. Must be OMPD_barrier for explicit barrier generation.
Alexey Bataev25e5b442015-09-15 12:52:43 +0000722 /// \param EmitChecks true if need to emit checks for cancellation barriers.
723 /// \param ForceSimpleCall true simple barrier call must be emitted, false if
724 /// runtime class decides which one to emit (simple or with cancellation
725 /// checks).
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000726 ///
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000727 virtual void emitBarrierCall(CodeGenFunction &CGF, SourceLocation Loc,
Alexey Bataev81c7ea02015-07-03 09:56:58 +0000728 OpenMPDirectiveKind Kind,
Alexey Bataev25e5b442015-09-15 12:52:43 +0000729 bool EmitChecks = true,
730 bool ForceSimpleCall = false);
Alexey Bataevb2059782014-10-13 08:23:51 +0000731
Alexander Musmanc6388682014-12-15 07:07:06 +0000732 /// \brief Check if the specified \a ScheduleKind is static non-chunked.
733 /// This kind of worksharing directive is emitted without outer loop.
734 /// \param ScheduleKind Schedule kind specified in the 'schedule' clause.
735 /// \param Chunked True if chunk is specified in the clause.
736 ///
737 virtual bool isStaticNonchunked(OpenMPScheduleClauseKind ScheduleKind,
738 bool Chunked) const;
739
Carlo Bertollifc35ad22016-03-07 16:04:49 +0000740 /// \brief Check if the specified \a ScheduleKind is static non-chunked.
741 /// This kind of distribute directive is emitted without outer loop.
742 /// \param ScheduleKind Schedule kind specified in the 'dist_schedule' clause.
743 /// \param Chunked True if chunk is specified in the clause.
744 ///
745 virtual bool isStaticNonchunked(OpenMPDistScheduleClauseKind ScheduleKind,
746 bool Chunked) const;
747
Alexander Musmandf7a8e22015-01-22 08:49:35 +0000748 /// \brief Check if the specified \a ScheduleKind is dynamic.
749 /// This kind of worksharing directive is emitted without outer loop.
750 /// \param ScheduleKind Schedule Kind specified in the 'schedule' clause.
751 ///
752 virtual bool isDynamic(OpenMPScheduleClauseKind ScheduleKind) const;
753
Carlo Bertollib0ff0a62017-04-25 17:52:12 +0000754 /// struct with the values to be passed to the dispatch runtime function
755 struct DispatchRTInput {
756 /// Loop lower bound
757 llvm::Value *LB = nullptr;
758 /// Loop upper bound
759 llvm::Value *UB = nullptr;
760 /// Chunk size specified using 'schedule' clause (nullptr if chunk
761 /// was not specified)
762 llvm::Value *Chunk = nullptr;
763 DispatchRTInput() = default;
764 DispatchRTInput(llvm::Value *LB, llvm::Value *UB, llvm::Value *Chunk)
765 : LB(LB), UB(UB), Chunk(Chunk) {}
766 };
767
768 /// Call the appropriate runtime routine to initialize it before start
769 /// of loop.
770
771 /// This is used for non static scheduled types and when the ordered
772 /// clause is present on the loop construct.
773 /// Depending on the loop schedule, it is necessary to call some runtime
774 /// routine before start of the OpenMP loop to get the loop upper / lower
775 /// bounds \a LB and \a UB and stride \a ST.
776 ///
777 /// \param CGF Reference to current CodeGenFunction.
778 /// \param Loc Clang source location.
779 /// \param ScheduleKind Schedule kind, specified by the 'schedule' clause.
780 /// \param IVSize Size of the iteration variable in bits.
Simon Pilgrim6c0eeff2017-07-13 17:34:44 +0000781 /// \param IVSigned Sign of the iteration variable.
Carlo Bertollib0ff0a62017-04-25 17:52:12 +0000782 /// \param Ordered true if loop is ordered, false otherwise.
783 /// \param DispatchValues struct containing llvm values for lower bound, upper
784 /// bound, and chunk expression.
785 /// For the default (nullptr) value, the chunk 1 will be used.
786 ///
NAKAMURA Takumiff7a9252015-09-08 09:42:41 +0000787 virtual void emitForDispatchInit(CodeGenFunction &CGF, SourceLocation Loc,
Alexey Bataev9ebd7422016-05-10 09:57:36 +0000788 const OpenMPScheduleTy &ScheduleKind,
789 unsigned IVSize, bool IVSigned, bool Ordered,
Carlo Bertollib0ff0a62017-04-25 17:52:12 +0000790 const DispatchRTInput &DispatchValues);
NAKAMURA Takumiff7a9252015-09-08 09:42:41 +0000791
Alexander Musmanc6388682014-12-15 07:07:06 +0000792 /// \brief Call the appropriate runtime routine to initialize it before start
793 /// of loop.
794 ///
Carlo Bertollib0ff0a62017-04-25 17:52:12 +0000795 /// This is used only in case of static schedule, when the user did not
796 /// specify a ordered clause on the loop construct.
797 /// Depending on the loop schedule, it is necessary to call some runtime
Alexander Musmanc6388682014-12-15 07:07:06 +0000798 /// routine before start of the OpenMP loop to get the loop upper / lower
799 /// bounds \a LB and \a UB and stride \a ST.
800 ///
801 /// \param CGF Reference to current CodeGenFunction.
802 /// \param Loc Clang source location.
Alexey Bataev9ebd7422016-05-10 09:57:36 +0000803 /// \param ScheduleKind Schedule kind, specified by the 'schedule' clause.
Alexander Musmanc6388682014-12-15 07:07:06 +0000804 /// \param IVSize Size of the iteration variable in bits.
Simon Pilgrim6c0eeff2017-07-13 17:34:44 +0000805 /// \param IVSigned Sign of the iteration variable.
Alexey Bataevd7589ffe2015-05-20 13:12:48 +0000806 /// \param Ordered true if loop is ordered, false otherwise.
Alexander Musmanc6388682014-12-15 07:07:06 +0000807 /// \param IL Address of the output variable in which the flag of the
808 /// last iteration is returned.
809 /// \param LB Address of the output variable in which the lower iteration
810 /// number is returned.
811 /// \param UB Address of the output variable in which the upper iteration
812 /// number is returned.
813 /// \param ST Address of the output variable in which the stride value is
Simon Pilgrim6c0eeff2017-07-13 17:34:44 +0000814 /// returned necessary to generated the static_chunked scheduled loop.
Alexander Musmanc6388682014-12-15 07:07:06 +0000815 /// \param Chunk Value of the chunk for the static_chunked scheduled loop.
816 /// For the default (nullptr) value, the chunk 1 will be used.
817 ///
John McCall7f416cc2015-09-08 08:05:57 +0000818 virtual void emitForStaticInit(CodeGenFunction &CGF, SourceLocation Loc,
Alexey Bataev9ebd7422016-05-10 09:57:36 +0000819 const OpenMPScheduleTy &ScheduleKind,
John McCall7f416cc2015-09-08 08:05:57 +0000820 unsigned IVSize, bool IVSigned, bool Ordered,
Alexey Bataev9ebd7422016-05-10 09:57:36 +0000821 Address IL, Address LB, Address UB, Address ST,
John McCall7f416cc2015-09-08 08:05:57 +0000822 llvm::Value *Chunk = nullptr);
Alexander Musmanc6388682014-12-15 07:07:06 +0000823
Carlo Bertollifc35ad22016-03-07 16:04:49 +0000824 ///
825 /// \param CGF Reference to current CodeGenFunction.
826 /// \param Loc Clang source location.
827 /// \param SchedKind Schedule kind, specified by the 'dist_schedule' clause.
828 /// \param IVSize Size of the iteration variable in bits.
Simon Pilgrim6c0eeff2017-07-13 17:34:44 +0000829 /// \param IVSigned Sign of the iteration variable.
Carlo Bertollifc35ad22016-03-07 16:04:49 +0000830 /// \param Ordered true if loop is ordered, false otherwise.
831 /// \param IL Address of the output variable in which the flag of the
832 /// last iteration is returned.
833 /// \param LB Address of the output variable in which the lower iteration
834 /// number is returned.
835 /// \param UB Address of the output variable in which the upper iteration
836 /// number is returned.
837 /// \param ST Address of the output variable in which the stride value is
Simon Pilgrim6c0eeff2017-07-13 17:34:44 +0000838 /// returned necessary to generated the static_chunked scheduled loop.
Carlo Bertollifc35ad22016-03-07 16:04:49 +0000839 /// \param Chunk Value of the chunk for the static_chunked scheduled loop.
840 /// For the default (nullptr) value, the chunk 1 will be used.
841 ///
842 virtual void emitDistributeStaticInit(CodeGenFunction &CGF, SourceLocation Loc,
843 OpenMPDistScheduleClauseKind SchedKind,
844 unsigned IVSize, bool IVSigned,
845 bool Ordered, Address IL, Address LB,
846 Address UB, Address ST,
847 llvm::Value *Chunk = nullptr);
848
Alexander Musmanc6388682014-12-15 07:07:06 +0000849 /// \brief Call the appropriate runtime routine to notify that we finished
Alexey Bataev98eb6e32015-04-22 11:15:40 +0000850 /// iteration of the ordered loop with the dynamic scheduling.
851 ///
852 /// \param CGF Reference to current CodeGenFunction.
853 /// \param Loc Clang source location.
854 /// \param IVSize Size of the iteration variable in bits.
Simon Pilgrim6c0eeff2017-07-13 17:34:44 +0000855 /// \param IVSigned Sign of the iteration variable.
Alexey Bataev98eb6e32015-04-22 11:15:40 +0000856 ///
Alexey Bataevd7589ffe2015-05-20 13:12:48 +0000857 virtual void emitForOrderedIterationEnd(CodeGenFunction &CGF,
858 SourceLocation Loc, unsigned IVSize,
859 bool IVSigned);
Alexey Bataev98eb6e32015-04-22 11:15:40 +0000860
861 /// \brief Call the appropriate runtime routine to notify that we finished
Alexander Musmanc6388682014-12-15 07:07:06 +0000862 /// all the work with current loop.
863 ///
864 /// \param CGF Reference to current CodeGenFunction.
865 /// \param Loc Clang source location.
Alexander Musmanc6388682014-12-15 07:07:06 +0000866 ///
Alexey Bataev98eb6e32015-04-22 11:15:40 +0000867 virtual void emitForStaticFinish(CodeGenFunction &CGF, SourceLocation Loc);
Alexander Musmanc6388682014-12-15 07:07:06 +0000868
Alexander Musman92bdaab2015-03-12 13:37:50 +0000869 /// Call __kmpc_dispatch_next(
870 /// ident_t *loc, kmp_int32 tid, kmp_int32 *p_lastiter,
871 /// kmp_int[32|64] *p_lower, kmp_int[32|64] *p_upper,
872 /// kmp_int[32|64] *p_stride);
873 /// \param IVSize Size of the iteration variable in bits.
Simon Pilgrim6c0eeff2017-07-13 17:34:44 +0000874 /// \param IVSigned Sign of the iteration variable.
Alexander Musman92bdaab2015-03-12 13:37:50 +0000875 /// \param IL Address of the output variable in which the flag of the
876 /// last iteration is returned.
877 /// \param LB Address of the output variable in which the lower iteration
878 /// number is returned.
879 /// \param UB Address of the output variable in which the upper iteration
880 /// number is returned.
881 /// \param ST Address of the output variable in which the stride value is
882 /// returned.
883 virtual llvm::Value *emitForNext(CodeGenFunction &CGF, SourceLocation Loc,
884 unsigned IVSize, bool IVSigned,
John McCall7f416cc2015-09-08 08:05:57 +0000885 Address IL, Address LB,
886 Address UB, Address ST);
Alexander Musman92bdaab2015-03-12 13:37:50 +0000887
Alexey Bataevb2059782014-10-13 08:23:51 +0000888 /// \brief Emits call to void __kmpc_push_num_threads(ident_t *loc, kmp_int32
889 /// global_tid, kmp_int32 num_threads) to generate code for 'num_threads'
890 /// clause.
891 /// \param NumThreads An integer value of threads.
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000892 virtual void emitNumThreadsClause(CodeGenFunction &CGF,
893 llvm::Value *NumThreads,
894 SourceLocation Loc);
Alexey Bataev97720002014-11-11 04:05:39 +0000895
Alexey Bataev7f210c62015-06-18 13:40:03 +0000896 /// \brief Emit call to void __kmpc_push_proc_bind(ident_t *loc, kmp_int32
897 /// global_tid, int proc_bind) to generate code for 'proc_bind' clause.
898 virtual void emitProcBindClause(CodeGenFunction &CGF,
899 OpenMPProcBindClauseKind ProcBind,
900 SourceLocation Loc);
901
Alexey Bataev97720002014-11-11 04:05:39 +0000902 /// \brief Returns address of the threadprivate variable for the current
903 /// thread.
NAKAMURA Takumicdcbfba2014-11-11 07:58:06 +0000904 /// \param VD Threadprivate variable.
Alexey Bataev97720002014-11-11 04:05:39 +0000905 /// \param VDAddr Address of the global variable \a VD.
906 /// \param Loc Location of the reference to threadprivate var.
907 /// \return Address of the threadprivate variable for the current thread.
John McCall7f416cc2015-09-08 08:05:57 +0000908 virtual Address getAddrOfThreadPrivate(CodeGenFunction &CGF,
909 const VarDecl *VD,
910 Address VDAddr,
911 SourceLocation Loc);
Alexey Bataev97720002014-11-11 04:05:39 +0000912
913 /// \brief Emit a code for initialization of threadprivate variable. It emits
914 /// a call to runtime library which adds initial value to the newly created
915 /// threadprivate variable (if it is not constant) and registers destructor
916 /// for the variable (if any).
917 /// \param VD Threadprivate variable.
918 /// \param VDAddr Address of the global variable \a VD.
919 /// \param Loc Location of threadprivate declaration.
920 /// \param PerformInit true if initialization expression is not constant.
921 virtual llvm::Function *
John McCall7f416cc2015-09-08 08:05:57 +0000922 emitThreadPrivateVarDefinition(const VarDecl *VD, Address VDAddr,
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000923 SourceLocation Loc, bool PerformInit,
924 CodeGenFunction *CGF = nullptr);
Alexey Bataevcc37cc12014-11-20 04:34:54 +0000925
926 /// \brief Emit flush of the variables specified in 'omp flush' directive.
927 /// \param Vars List of variables to flush.
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000928 virtual void emitFlush(CodeGenFunction &CGF, ArrayRef<const Expr *> Vars,
929 SourceLocation Loc);
Alexey Bataev62b63b12015-03-10 07:28:44 +0000930
931 /// \brief Emit task region for the task directive. The task region is
Nico Weber20b0ce32015-04-28 18:19:18 +0000932 /// emitted in several steps:
Alexey Bataev62b63b12015-03-10 07:28:44 +0000933 /// 1. Emit a call to kmp_task_t *__kmpc_omp_task_alloc(ident_t *, kmp_int32
934 /// gtid, kmp_int32 flags, size_t sizeof_kmp_task_t, size_t sizeof_shareds,
935 /// kmp_routine_entry_t *task_entry). Here task_entry is a pointer to the
936 /// function:
937 /// kmp_int32 .omp_task_entry.(kmp_int32 gtid, kmp_task_t *tt) {
938 /// TaskFunction(gtid, tt->part_id, tt->shareds);
939 /// return 0;
940 /// }
941 /// 2. Copy a list of shared variables to field shareds of the resulting
942 /// structure kmp_task_t returned by the previous call (if any).
943 /// 3. Copy a pointer to destructions function to field destructions of the
944 /// resulting structure kmp_task_t.
945 /// 4. Emit a call to kmp_int32 __kmpc_omp_task(ident_t *, kmp_int32 gtid,
946 /// kmp_task_t *new_task), where new_task is a resulting structure from
947 /// previous items.
Alexey Bataev36c1eb92015-04-30 06:51:57 +0000948 /// \param D Current task directive.
Alexey Bataev62b63b12015-03-10 07:28:44 +0000949 /// \param TaskFunction An LLVM function with type void (*)(i32 /*gtid*/, i32
950 /// /*part_id*/, captured_struct */*__context*/);
951 /// \param SharedsTy A type which contains references the shared variables.
Alexey Bataev1d2353d2015-06-24 11:01:36 +0000952 /// \param Shareds Context with the list of shared variables from the \p
Alexey Bataev62b63b12015-03-10 07:28:44 +0000953 /// TaskFunction.
Alexey Bataev1d677132015-04-22 13:57:31 +0000954 /// \param IfCond Not a nullptr if 'if' clause was specified, nullptr
955 /// otherwise.
Alexey Bataev24b5bae2016-04-28 09:23:51 +0000956 /// \param Data Additional data for task generation like tiednsee, final
957 /// state, list of privates etc.
958 virtual void emitTaskCall(CodeGenFunction &CGF, SourceLocation Loc,
959 const OMPExecutableDirective &D,
960 llvm::Value *TaskFunction, QualType SharedsTy,
961 Address Shareds, const Expr *IfCond,
962 const OMPTaskDataTy &Data);
Alexey Bataev794ba0d2015-04-10 10:43:45 +0000963
Alexey Bataev7292c292016-04-25 12:22:29 +0000964 /// Emit task region for the taskloop directive. The taskloop region is
965 /// emitted in several steps:
966 /// 1. Emit a call to kmp_task_t *__kmpc_omp_task_alloc(ident_t *, kmp_int32
967 /// gtid, kmp_int32 flags, size_t sizeof_kmp_task_t, size_t sizeof_shareds,
968 /// kmp_routine_entry_t *task_entry). Here task_entry is a pointer to the
969 /// function:
970 /// kmp_int32 .omp_task_entry.(kmp_int32 gtid, kmp_task_t *tt) {
971 /// TaskFunction(gtid, tt->part_id, tt->shareds);
972 /// return 0;
973 /// }
974 /// 2. Copy a list of shared variables to field shareds of the resulting
975 /// structure kmp_task_t returned by the previous call (if any).
976 /// 3. Copy a pointer to destructions function to field destructions of the
977 /// resulting structure kmp_task_t.
978 /// 4. Emit a call to void __kmpc_taskloop(ident_t *loc, int gtid, kmp_task_t
979 /// *task, int if_val, kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st, int
980 /// nogroup, int sched, kmp_uint64 grainsize, void *task_dup ), where new_task
981 /// is a resulting structure from
982 /// previous items.
983 /// \param D Current task directive.
Alexey Bataev7292c292016-04-25 12:22:29 +0000984 /// \param TaskFunction An LLVM function with type void (*)(i32 /*gtid*/, i32
985 /// /*part_id*/, captured_struct */*__context*/);
986 /// \param SharedsTy A type which contains references the shared variables.
987 /// \param Shareds Context with the list of shared variables from the \p
988 /// TaskFunction.
989 /// \param IfCond Not a nullptr if 'if' clause was specified, nullptr
990 /// otherwise.
Alexey Bataev24b5bae2016-04-28 09:23:51 +0000991 /// \param Data Additional data for task generation like tiednsee, final
992 /// state, list of privates etc.
Alexey Bataev7292c292016-04-25 12:22:29 +0000993 virtual void emitTaskLoopCall(
994 CodeGenFunction &CGF, SourceLocation Loc, const OMPLoopDirective &D,
Alexey Bataev24b5bae2016-04-28 09:23:51 +0000995 llvm::Value *TaskFunction, QualType SharedsTy, Address Shareds,
996 const Expr *IfCond, const OMPTaskDataTy &Data);
Alexey Bataev7292c292016-04-25 12:22:29 +0000997
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000998 /// \brief Emit code for the directive that does not require outlining.
999 ///
Alexey Bataev81c7ea02015-07-03 09:56:58 +00001000 /// \param InnermostKind Kind of innermost directive (for simple directives it
1001 /// is a directive itself, for combined - its innermost directive).
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001002 /// \param CodeGen Code generation sequence for the \a D directive.
Alexey Bataev25e5b442015-09-15 12:52:43 +00001003 /// \param HasCancel true if region has inner cancel directive, false
1004 /// otherwise.
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001005 virtual void emitInlinedDirective(CodeGenFunction &CGF,
Alexey Bataev81c7ea02015-07-03 09:56:58 +00001006 OpenMPDirectiveKind InnermostKind,
Alexey Bataev25e5b442015-09-15 12:52:43 +00001007 const RegionCodeGenTy &CodeGen,
1008 bool HasCancel = false);
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00001009
1010 /// Emits reduction function.
1011 /// \param ArgsType Array type containing pointers to reduction variables.
1012 /// \param Privates List of private copies for original reduction arguments.
1013 /// \param LHSExprs List of LHS in \a ReductionOps reduction operations.
1014 /// \param RHSExprs List of RHS in \a ReductionOps reduction operations.
1015 /// \param ReductionOps List of reduction operations in form 'LHS binop RHS'
1016 /// or 'operator binop(LHS, RHS)'.
1017 llvm::Value *emitReductionFunction(CodeGenModule &CGM, llvm::Type *ArgsType,
1018 ArrayRef<const Expr *> Privates,
1019 ArrayRef<const Expr *> LHSExprs,
1020 ArrayRef<const Expr *> RHSExprs,
1021 ArrayRef<const Expr *> ReductionOps);
1022
1023 /// Emits single reduction combiner
1024 void emitSingleReductionCombiner(CodeGenFunction &CGF,
1025 const Expr *ReductionOp,
1026 const Expr *PrivateRef,
1027 const DeclRefExpr *LHS,
1028 const DeclRefExpr *RHS);
1029
1030 struct ReductionOptionsTy {
1031 bool WithNowait;
1032 bool SimpleReduction;
1033 OpenMPDirectiveKind ReductionKind;
1034 };
Alexey Bataev794ba0d2015-04-10 10:43:45 +00001035 /// \brief Emit a code for reduction clause. Next code should be emitted for
1036 /// reduction:
1037 /// \code
1038 ///
1039 /// static kmp_critical_name lock = { 0 };
1040 ///
1041 /// void reduce_func(void *lhs[<n>], void *rhs[<n>]) {
1042 /// ...
1043 /// *(Type<i>*)lhs[i] = RedOp<i>(*(Type<i>*)lhs[i], *(Type<i>*)rhs[i]);
1044 /// ...
1045 /// }
1046 ///
1047 /// ...
1048 /// void *RedList[<n>] = {&<RHSExprs>[0], ..., &<RHSExprs>[<n>-1]};
1049 /// switch (__kmpc_reduce{_nowait}(<loc>, <gtid>, <n>, sizeof(RedList),
1050 /// RedList, reduce_func, &<lock>)) {
1051 /// case 1:
1052 /// ...
1053 /// <LHSExprs>[i] = RedOp<i>(*<LHSExprs>[i], *<RHSExprs>[i]);
1054 /// ...
1055 /// __kmpc_end_reduce{_nowait}(<loc>, <gtid>, &<lock>);
1056 /// break;
1057 /// case 2:
1058 /// ...
1059 /// Atomic(<LHSExprs>[i] = RedOp<i>(*<LHSExprs>[i], *<RHSExprs>[i]));
1060 /// ...
1061 /// break;
1062 /// default:;
1063 /// }
1064 /// \endcode
1065 ///
Alexey Bataevf24e7b12015-10-08 09:10:53 +00001066 /// \param Privates List of private copies for original reduction arguments.
Alexey Bataev794ba0d2015-04-10 10:43:45 +00001067 /// \param LHSExprs List of LHS in \a ReductionOps reduction operations.
1068 /// \param RHSExprs List of RHS in \a ReductionOps reduction operations.
1069 /// \param ReductionOps List of reduction operations in form 'LHS binop RHS'
1070 /// or 'operator binop(LHS, RHS)'.
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00001071 /// \param Options List of options for reduction codegen:
1072 /// WithNowait true if parent directive has also nowait clause, false
1073 /// otherwise.
1074 /// SimpleReduction Emit reduction operation only. Used for omp simd
1075 /// directive on the host.
1076 /// ReductionKind The kind of reduction to perform.
Alexey Bataev794ba0d2015-04-10 10:43:45 +00001077 virtual void emitReduction(CodeGenFunction &CGF, SourceLocation Loc,
Alexey Bataevf24e7b12015-10-08 09:10:53 +00001078 ArrayRef<const Expr *> Privates,
Alexey Bataev794ba0d2015-04-10 10:43:45 +00001079 ArrayRef<const Expr *> LHSExprs,
1080 ArrayRef<const Expr *> RHSExprs,
1081 ArrayRef<const Expr *> ReductionOps,
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00001082 ReductionOptionsTy Options);
Alexey Bataev8b8e2022015-04-27 05:22:09 +00001083
1084 /// \brief Emit code for 'taskwait' directive.
1085 virtual void emitTaskwaitCall(CodeGenFunction &CGF, SourceLocation Loc);
Alexey Bataev0f34da12015-07-02 04:17:07 +00001086
1087 /// \brief Emit code for 'cancellation point' construct.
1088 /// \param CancelRegion Region kind for which the cancellation point must be
1089 /// emitted.
1090 ///
1091 virtual void emitCancellationPointCall(CodeGenFunction &CGF,
1092 SourceLocation Loc,
1093 OpenMPDirectiveKind CancelRegion);
Alexey Bataev7d5d33e2015-07-06 05:50:32 +00001094
1095 /// \brief Emit code for 'cancel' construct.
Alexey Bataev87933c72015-09-18 08:07:34 +00001096 /// \param IfCond Condition in the associated 'if' clause, if it was
1097 /// specified, nullptr otherwise.
Alexey Bataev7d5d33e2015-07-06 05:50:32 +00001098 /// \param CancelRegion Region kind for which the cancel must be emitted.
1099 ///
1100 virtual void emitCancelCall(CodeGenFunction &CGF, SourceLocation Loc,
Alexey Bataev87933c72015-09-18 08:07:34 +00001101 const Expr *IfCond,
Alexey Bataev7d5d33e2015-07-06 05:50:32 +00001102 OpenMPDirectiveKind CancelRegion);
Samuel Antaobed3c462015-10-02 16:14:20 +00001103
1104 /// \brief Emit outilined function for 'target' directive.
1105 /// \param D Directive to emit.
Samuel Antaoee8fb302016-01-06 13:42:12 +00001106 /// \param ParentName Name of the function that encloses the target region.
1107 /// \param OutlinedFn Outlined function value to be defined by this call.
1108 /// \param OutlinedFnID Outlined function ID value to be defined by this call.
1109 /// \param IsOffloadEntry True if the outlined function is an offload entry.
Alexey Bataev14fa1c62016-03-29 05:34:15 +00001110 /// \param CodeGen Code generation sequence for the \a D directive.
Simon Pilgrim6c0eeff2017-07-13 17:34:44 +00001111 /// An outlined function may not be an entry if, e.g. the if clause always
Samuel Antaoee8fb302016-01-06 13:42:12 +00001112 /// evaluates to false.
1113 virtual void emitTargetOutlinedFunction(const OMPExecutableDirective &D,
1114 StringRef ParentName,
1115 llvm::Function *&OutlinedFn,
1116 llvm::Constant *&OutlinedFnID,
Alexey Bataev14fa1c62016-03-29 05:34:15 +00001117 bool IsOffloadEntry,
1118 const RegionCodeGenTy &CodeGen);
Samuel Antaobed3c462015-10-02 16:14:20 +00001119
1120 /// \brief Emit the target offloading code associated with \a D. The emitted
1121 /// code attempts offloading the execution to the device, an the event of
1122 /// a failure it executes the host version outlined in \a OutlinedFn.
1123 /// \param D Directive to emit.
1124 /// \param OutlinedFn Host version of the code to be offloaded.
Samuel Antaoee8fb302016-01-06 13:42:12 +00001125 /// \param OutlinedFnID ID of host version of the code to be offloaded.
Samuel Antaobed3c462015-10-02 16:14:20 +00001126 /// \param IfCond Expression evaluated in if clause associated with the target
1127 /// directive, or null if no if clause is used.
1128 /// \param Device Expression evaluated in device clause associated with the
1129 /// target directive, or null if no device clause is used.
1130 /// \param CapturedVars Values captured in the current region.
1131 virtual void emitTargetCall(CodeGenFunction &CGF,
1132 const OMPExecutableDirective &D,
Samuel Antaoee8fb302016-01-06 13:42:12 +00001133 llvm::Value *OutlinedFn,
1134 llvm::Value *OutlinedFnID, const Expr *IfCond,
Samuel Antaobed3c462015-10-02 16:14:20 +00001135 const Expr *Device,
1136 ArrayRef<llvm::Value *> CapturedVars);
Samuel Antaoee8fb302016-01-06 13:42:12 +00001137
1138 /// \brief Emit the target regions enclosed in \a GD function definition or
1139 /// the function itself in case it is a valid device function. Returns true if
1140 /// \a GD was dealt with successfully.
Nico Webera2abe8c2016-01-06 19:13:49 +00001141 /// \param GD Function to scan.
Samuel Antaoee8fb302016-01-06 13:42:12 +00001142 virtual bool emitTargetFunctions(GlobalDecl GD);
1143
1144 /// \brief Emit the global variable if it is a valid device global variable.
1145 /// Returns true if \a GD was dealt with successfully.
1146 /// \param GD Variable declaration to emit.
1147 virtual bool emitTargetGlobalVariable(GlobalDecl GD);
1148
1149 /// \brief Emit the global \a GD if it is meaningful for the target. Returns
Simon Pilgrim2c518802017-03-30 14:13:19 +00001150 /// if it was emitted successfully.
Samuel Antaoee8fb302016-01-06 13:42:12 +00001151 /// \param GD Global to scan.
1152 virtual bool emitTargetGlobal(GlobalDecl GD);
1153
1154 /// \brief Creates the offloading descriptor in the event any target region
1155 /// was emitted in the current module and return the function that registers
1156 /// it.
1157 virtual llvm::Function *emitRegistrationFunction();
Carlo Bertolli430d8ec2016-03-03 20:34:23 +00001158
1159 /// \brief Emits code for teams call of the \a OutlinedFn with
1160 /// variables captured in a record which address is stored in \a
1161 /// CapturedStruct.
1162 /// \param OutlinedFn Outlined function to be run by team masters. Type of
1163 /// this function is void(*)(kmp_int32 *, kmp_int32, struct context_vars*).
1164 /// \param CapturedVars A pointer to the record with the references to
1165 /// variables used in \a OutlinedFn function.
1166 ///
1167 virtual void emitTeamsCall(CodeGenFunction &CGF,
1168 const OMPExecutableDirective &D,
1169 SourceLocation Loc, llvm::Value *OutlinedFn,
1170 ArrayRef<llvm::Value *> CapturedVars);
1171
1172 /// \brief Emits call to void __kmpc_push_num_teams(ident_t *loc, kmp_int32
1173 /// global_tid, kmp_int32 num_teams, kmp_int32 thread_limit) to generate code
1174 /// for num_teams clause.
Carlo Bertollic6872252016-04-04 15:55:02 +00001175 /// \param NumTeams An integer expression of teams.
1176 /// \param ThreadLimit An integer expression of threads.
1177 virtual void emitNumTeamsClause(CodeGenFunction &CGF, const Expr *NumTeams,
1178 const Expr *ThreadLimit, SourceLocation Loc);
Samuel Antaodf158d52016-04-27 22:58:19 +00001179
Samuel Antaocc10b852016-07-28 14:23:26 +00001180 /// Struct that keeps all the relevant information that should be kept
1181 /// throughout a 'target data' region.
1182 class TargetDataInfo {
1183 /// Set to true if device pointer information have to be obtained.
1184 bool RequiresDevicePointerInfo = false;
1185
1186 public:
1187 /// The array of base pointer passed to the runtime library.
1188 llvm::Value *BasePointersArray = nullptr;
1189 /// The array of section pointers passed to the runtime library.
1190 llvm::Value *PointersArray = nullptr;
1191 /// The array of sizes passed to the runtime library.
1192 llvm::Value *SizesArray = nullptr;
1193 /// The array of map types passed to the runtime library.
1194 llvm::Value *MapTypesArray = nullptr;
1195 /// The total number of pointers passed to the runtime library.
1196 unsigned NumberOfPtrs = 0u;
1197 /// Map between the a declaration of a capture and the corresponding base
1198 /// pointer address where the runtime returns the device pointers.
1199 llvm::DenseMap<const ValueDecl *, Address> CaptureDeviceAddrMap;
1200
1201 explicit TargetDataInfo() {}
1202 explicit TargetDataInfo(bool RequiresDevicePointerInfo)
1203 : RequiresDevicePointerInfo(RequiresDevicePointerInfo) {}
1204 /// Clear information about the data arrays.
1205 void clearArrayInfo() {
1206 BasePointersArray = nullptr;
1207 PointersArray = nullptr;
1208 SizesArray = nullptr;
1209 MapTypesArray = nullptr;
1210 NumberOfPtrs = 0u;
1211 }
1212 /// Return true if the current target data information has valid arrays.
1213 bool isValid() {
1214 return BasePointersArray && PointersArray && SizesArray &&
1215 MapTypesArray && NumberOfPtrs;
1216 }
1217 bool requiresDevicePointerInfo() { return RequiresDevicePointerInfo; }
1218 };
1219
Samuel Antaodf158d52016-04-27 22:58:19 +00001220 /// \brief Emit the target data mapping code associated with \a D.
1221 /// \param D Directive to emit.
Samuel Antaocc10b852016-07-28 14:23:26 +00001222 /// \param IfCond Expression evaluated in if clause associated with the
1223 /// target directive, or null if no device clause is used.
Samuel Antaodf158d52016-04-27 22:58:19 +00001224 /// \param Device Expression evaluated in device clause associated with the
1225 /// target directive, or null if no device clause is used.
Samuel Antaocc10b852016-07-28 14:23:26 +00001226 /// \param Info A record used to store information that needs to be preserved
1227 /// until the region is closed.
Samuel Antaodf158d52016-04-27 22:58:19 +00001228 virtual void emitTargetDataCalls(CodeGenFunction &CGF,
1229 const OMPExecutableDirective &D,
1230 const Expr *IfCond, const Expr *Device,
Samuel Antaocc10b852016-07-28 14:23:26 +00001231 const RegionCodeGenTy &CodeGen,
1232 TargetDataInfo &Info);
Samuel Antaobd0ae2e2016-04-27 23:07:29 +00001233
Samuel Antao8d2d7302016-05-26 18:30:22 +00001234 /// \brief Emit the data mapping/movement code associated with the directive
1235 /// \a D that should be of the form 'target [{enter|exit} data | update]'.
Samuel Antaobd0ae2e2016-04-27 23:07:29 +00001236 /// \param D Directive to emit.
1237 /// \param IfCond Expression evaluated in if clause associated with the target
1238 /// directive, or null if no if clause is used.
1239 /// \param Device Expression evaluated in device clause associated with the
1240 /// target directive, or null if no device clause is used.
Samuel Antao8d2d7302016-05-26 18:30:22 +00001241 virtual void emitTargetDataStandAloneCall(CodeGenFunction &CGF,
1242 const OMPExecutableDirective &D,
1243 const Expr *IfCond,
1244 const Expr *Device);
Alexey Bataevc7a82b42016-05-06 09:40:08 +00001245
1246 /// Marks function \a Fn with properly mangled versions of vector functions.
1247 /// \param FD Function marked as 'declare simd'.
1248 /// \param Fn LLVM function that must be marked with 'declare simd'
1249 /// attributes.
1250 virtual void emitDeclareSimdFunction(const FunctionDecl *FD,
1251 llvm::Function *Fn);
Alexey Bataev8b427062016-05-25 12:36:08 +00001252
1253 /// Emit initialization for doacross loop nesting support.
1254 /// \param D Loop-based construct used in doacross nesting construct.
1255 virtual void emitDoacrossInit(CodeGenFunction &CGF,
1256 const OMPLoopDirective &D);
1257
1258 /// Emit code for doacross ordered directive with 'depend' clause.
1259 /// \param C 'depend' clause with 'sink|source' dependency kind.
1260 virtual void emitDoacrossOrdered(CodeGenFunction &CGF,
1261 const OMPDependClause *C);
Alexey Bataev9959db52014-05-06 10:08:46 +00001262};
Alexey Bataev8cbe0a62015-02-26 10:27:34 +00001263
Alexey Bataev23b69422014-06-18 07:08:49 +00001264} // namespace CodeGen
1265} // namespace clang
Alexey Bataev9959db52014-05-06 10:08:46 +00001266
1267#endif