clang  7.0.0svn
CGOpenMPRuntime.h
Go to the documentation of this file.
1 //===----- 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 
14 #ifndef LLVM_CLANG_LIB_CODEGEN_CGOPENMPRUNTIME_H
15 #define LLVM_CLANG_LIB_CODEGEN_CGOPENMPRUNTIME_H
16 
17 #include "CGValue.h"
18 #include "clang/AST/Type.h"
21 #include "llvm/ADT/DenseMap.h"
22 #include "llvm/ADT/SmallPtrSet.h"
23 #include "llvm/ADT/StringMap.h"
24 #include "llvm/IR/Function.h"
25 #include "llvm/IR/ValueHandle.h"
26 
27 namespace llvm {
28 class ArrayType;
29 class Constant;
30 class FunctionType;
31 class GlobalVariable;
32 class StructType;
33 class Type;
34 class Value;
35 } // namespace llvm
36 
37 namespace clang {
38 class Expr;
39 class GlobalDecl;
40 class OMPDependClause;
41 class OMPExecutableDirective;
42 class OMPLoopDirective;
43 class VarDecl;
44 class OMPDeclareReductionDecl;
45 class IdentifierInfo;
46 
47 namespace CodeGen {
48 class Address;
49 class CodeGenFunction;
50 class CodeGenModule;
51 
52 /// A basic class for pre|post-action for advanced codegen sequence for OpenMP
53 /// region.
55 public:
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.
64 class 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 
77 public:
78  template <typename Callable>
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 };
90 
91 struct OMPTaskDataTy final {
103  llvm::PointerIntPair<llvm::Value *, 1, bool> Final;
104  llvm::PointerIntPair<llvm::Value *, 1, bool> Schedule;
105  llvm::PointerIntPair<llvm::Value *, 1, bool> Priority;
106  llvm::Value *Reductions = nullptr;
107  unsigned NumberOfParts = 0;
108  bool Tied = true;
109  bool Nogroup = false;
110 };
111 
112 /// Class intended to support codegen of all kind of the reduction clauses.
114 private:
115  /// Data required for codegen of reduction clauses.
116  struct ReductionData {
117  /// Reference to the original shared item.
118  const Expr *Ref = nullptr;
119  /// Helper expression for generation of private copy.
120  const Expr *Private = nullptr;
121  /// Helper expression for generation reduction operation.
122  const Expr *ReductionOp = nullptr;
123  ReductionData(const Expr *Ref, const Expr *Private, const Expr *ReductionOp)
124  : Ref(Ref), Private(Private), ReductionOp(ReductionOp) {}
125  };
126  /// List of reduction-based clauses.
127  SmallVector<ReductionData, 4> ClausesData;
128 
129  /// List of addresses of original shared variables/expressions.
130  SmallVector<std::pair<LValue, LValue>, 4> SharedAddresses;
131  /// Sizes of the reduction items in chars.
133  /// Base declarations for the reduction items.
135 
136  /// Emits lvalue for shared expresion.
137  LValue emitSharedLValue(CodeGenFunction &CGF, const Expr *E);
138  /// Emits upper bound for shared expression (if array section).
139  LValue emitSharedLValueUB(CodeGenFunction &CGF, const Expr *E);
140  /// Performs aggregate initialization.
141  /// \param N Number of reduction item in the common list.
142  /// \param PrivateAddr Address of the corresponding private item.
143  /// \param SharedLVal Address of the original shared variable.
144  /// \param DRD Declare reduction construct used for reduction item.
145  void emitAggregateInitialization(CodeGenFunction &CGF, unsigned N,
146  Address PrivateAddr, LValue SharedLVal,
147  const OMPDeclareReductionDecl *DRD);
148 
149 public:
152  ArrayRef<const Expr *> ReductionOps);
153  /// Emits lvalue for a reduction item.
154  /// \param N Number of the reduction item.
155  void emitSharedLValue(CodeGenFunction &CGF, unsigned N);
156  /// Emits the code for the variable-modified type, if required.
157  /// \param N Number of the reduction item.
158  void emitAggregateType(CodeGenFunction &CGF, unsigned N);
159  /// Emits the code for the variable-modified type, if required.
160  /// \param N Number of the reduction item.
161  /// \param Size Size of the type in chars.
162  void emitAggregateType(CodeGenFunction &CGF, unsigned N, llvm::Value *Size);
163  /// Performs initialization of the private copy for the reduction item.
164  /// \param N Number of the reduction item.
165  /// \param PrivateAddr Address of the corresponding private item.
166  /// \param DefaultInit Default initialization sequence that should be
167  /// performed if no reduction specific initialization is found.
168  /// \param SharedLVal Address of the original shared variable.
169  void
170  emitInitialization(CodeGenFunction &CGF, unsigned N, Address PrivateAddr,
171  LValue SharedLVal,
172  llvm::function_ref<bool(CodeGenFunction &)> DefaultInit);
173  /// Returns true if the private copy requires cleanups.
174  bool needCleanups(unsigned N);
175  /// Emits cleanup code for the reduction item.
176  /// \param N Number of the reduction item.
177  /// \param PrivateAddr Address of the corresponding private item.
178  void emitCleanups(CodeGenFunction &CGF, unsigned N, Address PrivateAddr);
179  /// Adjusts \p PrivatedAddr for using instead of the original variable
180  /// address in normal operations.
181  /// \param N Number of the reduction item.
182  /// \param PrivateAddr Address of the corresponding private item.
183  Address adjustPrivateAddress(CodeGenFunction &CGF, unsigned N,
184  Address PrivateAddr);
185  /// Returns LValue for the reduction item.
186  LValue getSharedLValue(unsigned N) const { return SharedAddresses[N].first; }
187  /// Returns the size of the reduction item (in chars and total number of
188  /// elements in the item), or nullptr, if the size is a constant.
189  std::pair<llvm::Value *, llvm::Value *> getSizes(unsigned N) const {
190  return Sizes[N];
191  }
192  /// Returns the base declaration of the reduction item.
193  const VarDecl *getBaseDecl(unsigned N) const { return BaseDecls[N]; }
194  /// Returns true if the initialization of the reduction item uses initializer
195  /// from declare reduction construct.
196  bool usesReductionInitializer(unsigned N) const;
197 };
198 
200 protected:
202 
203  /// \brief Creates offloading entry for the provided entry ID \a ID,
204  /// address \a Addr, size \a Size, and flags \a Flags.
205  virtual void createOffloadEntry(llvm::Constant *ID, llvm::Constant *Addr,
206  uint64_t Size, int32_t Flags = 0);
207 
208  /// \brief Helper to emit outlined function for 'target' directive.
209  /// \param D Directive to emit.
210  /// \param ParentName Name of the function that encloses the target region.
211  /// \param OutlinedFn Outlined function value to be defined by this call.
212  /// \param OutlinedFnID Outlined function ID value to be defined by this call.
213  /// \param IsOffloadEntry True if the outlined function is an offload entry.
214  /// \param CodeGen Lambda codegen specific to an accelerator device.
215  /// An outlined function may not be an entry if, e.g. the if clause always
216  /// evaluates to false.
217  virtual void emitTargetOutlinedFunctionHelper(const OMPExecutableDirective &D,
218  StringRef ParentName,
219  llvm::Function *&OutlinedFn,
220  llvm::Constant *&OutlinedFnID,
221  bool IsOffloadEntry,
222  const RegionCodeGenTy &CodeGen);
223 
224  /// \brief Emits code for OpenMP 'if' clause using specified \a CodeGen
225  /// function. Here is the logic:
226  /// if (Cond) {
227  /// ThenGen();
228  /// } else {
229  /// ElseGen();
230  /// }
231  void emitOMPIfClause(CodeGenFunction &CGF, const Expr *Cond,
232  const RegionCodeGenTy &ThenGen,
233  const RegionCodeGenTy &ElseGen);
234 
235  /// \brief Emits object of ident_t type with info for source location.
236  /// \param Flags Flags for OpenMP location.
237  ///
238  llvm::Value *emitUpdateLocation(CodeGenFunction &CGF, SourceLocation Loc,
239  unsigned Flags = 0);
240 
241  /// \brief Returns pointer to ident_t type.
242  llvm::Type *getIdentTyPointerTy();
243 
244  /// \brief Gets thread id value for the current thread.
245  ///
246  llvm::Value *getThreadID(CodeGenFunction &CGF, SourceLocation Loc);
247 
248  /// \brief Get the function name of an outlined region.
249  // The name can be customized depending on the target.
250  //
251  virtual StringRef getOutlinedHelperName() const { return ".omp_outlined."; }
252 
253  /// Emits \p Callee function call with arguments \p Args with location \p Loc.
254  void emitCall(CodeGenFunction &CGF, llvm::Value *Callee,
255  ArrayRef<llvm::Value *> Args = llvm::None,
256  SourceLocation Loc = SourceLocation()) const;
257 
258 private:
259  /// \brief Default const ident_t object used for initialization of all other
260  /// ident_t objects.
261  llvm::Constant *DefaultOpenMPPSource = nullptr;
262  /// \brief Map of flags and corresponding default locations.
263  typedef llvm::DenseMap<unsigned, llvm::Value *> OpenMPDefaultLocMapTy;
264  OpenMPDefaultLocMapTy OpenMPDefaultLocMap;
265  Address getOrCreateDefaultLocation(unsigned Flags);
266 
267  llvm::StructType *IdentTy = nullptr;
268  /// \brief Map for SourceLocation and OpenMP runtime library debug locations.
269  typedef llvm::DenseMap<unsigned, llvm::Value *> OpenMPDebugLocMapTy;
270  OpenMPDebugLocMapTy OpenMPDebugLocMap;
271  /// \brief The type for a microtask which gets passed to __kmpc_fork_call().
272  /// Original representation is:
273  /// typedef void (kmpc_micro)(kmp_int32 global_tid, kmp_int32 bound_tid,...);
274  llvm::FunctionType *Kmpc_MicroTy = nullptr;
275  /// \brief Stores debug location and ThreadID for the function.
276  struct DebugLocThreadIdTy {
277  llvm::Value *DebugLoc;
278  llvm::Value *ThreadID;
279  };
280  /// \brief Map of local debug location, ThreadId and functions.
281  typedef llvm::DenseMap<llvm::Function *, DebugLocThreadIdTy>
282  OpenMPLocThreadIDMapTy;
283  OpenMPLocThreadIDMapTy OpenMPLocThreadIDMap;
284  /// Map of UDRs and corresponding combiner/initializer.
285  typedef llvm::DenseMap<const OMPDeclareReductionDecl *,
286  std::pair<llvm::Function *, llvm::Function *>>
287  UDRMapTy;
288  UDRMapTy UDRMap;
289  /// Map of functions and locally defined UDRs.
290  typedef llvm::DenseMap<llvm::Function *,
292  FunctionUDRMapTy;
293  FunctionUDRMapTy FunctionUDRMap;
294  IdentifierInfo *In = nullptr;
295  IdentifierInfo *Out = nullptr;
296  IdentifierInfo *Priv = nullptr;
297  IdentifierInfo *Orig = nullptr;
298  /// \brief Type kmp_critical_name, originally defined as typedef kmp_int32
299  /// kmp_critical_name[8];
300  llvm::ArrayType *KmpCriticalNameTy;
301  /// \brief An ordered map of auto-generated variables to their unique names.
302  /// It stores variables with the following names: 1) ".gomp_critical_user_" +
303  /// <critical_section_name> + ".var" for "omp critical" directives; 2)
304  /// <mangled_name_for_global_var> + ".cache." for cache for threadprivate
305  /// variables.
306  llvm::StringMap<llvm::AssertingVH<llvm::Constant>, llvm::BumpPtrAllocator>
307  InternalVars;
308  /// \brief Type typedef kmp_int32 (* kmp_routine_entry_t)(kmp_int32, void *);
309  llvm::Type *KmpRoutineEntryPtrTy = nullptr;
310  QualType KmpRoutineEntryPtrQTy;
311  /// \brief Type typedef struct kmp_task {
312  /// void * shareds; /**< pointer to block of pointers to
313  /// shared vars */
314  /// kmp_routine_entry_t routine; /**< pointer to routine to call for
315  /// executing task */
316  /// kmp_int32 part_id; /**< part id for the task */
317  /// kmp_routine_entry_t destructors; /* pointer to function to invoke
318  /// deconstructors of firstprivate C++ objects */
319  /// } kmp_task_t;
321  /// Saved kmp_task_t for task directive.
323  /// Saved kmp_task_t for taskloop-based directive.
325  /// \brief Type typedef struct kmp_depend_info {
326  /// kmp_intptr_t base_addr;
327  /// size_t len;
328  /// struct {
329  /// bool in:1;
330  /// bool out:1;
331  /// } flags;
332  /// } kmp_depend_info_t;
334  /// struct kmp_dim { // loop bounds info casted to kmp_int64
335  /// kmp_int64 lo; // lower
336  /// kmp_int64 up; // upper
337  /// kmp_int64 st; // stride
338  /// };
340  /// \brief Type struct __tgt_offload_entry{
341  /// void *addr; // Pointer to the offload entry info.
342  /// // (function or global)
343  /// char *name; // Name of the function or global.
344  /// size_t size; // Size of the entry info (0 if it a function).
345  /// };
347  /// struct __tgt_device_image{
348  /// void *ImageStart; // Pointer to the target code start.
349  /// void *ImageEnd; // Pointer to the target code end.
350  /// // We also add the host entries to the device image, as it may be useful
351  /// // for the target runtime to have access to that information.
352  /// __tgt_offload_entry *EntriesBegin; // Begin of the table with all
353  /// // the entries.
354  /// __tgt_offload_entry *EntriesEnd; // End of the table with all the
355  /// // entries (non inclusive).
356  /// };
358  /// struct __tgt_bin_desc{
359  /// int32_t NumDevices; // Number of devices supported.
360  /// __tgt_device_image *DeviceImages; // Arrays of device images
361  /// // (one per device).
362  /// __tgt_offload_entry *EntriesBegin; // Begin of the table with all the
363  /// // entries.
364  /// __tgt_offload_entry *EntriesEnd; // End of the table with all the
365  /// // entries (non inclusive).
366  /// };
368  /// \brief Entity that registers the offloading constants that were emitted so
369  /// far.
371  CodeGenModule &CGM;
372 
373  /// \brief Number of entries registered so far.
374  unsigned OffloadingEntriesNum;
375 
376  public:
377  /// Base class of the entries info.
379  public:
380  /// Kind of a given entry. Currently, only target regions are
381  /// supported.
382  enum OffloadingEntryInfoKinds : unsigned {
383  // Entry is a target region.
384  OFFLOAD_ENTRY_INFO_TARGET_REGION = 0,
385  // Invalid entry info.
386  OFFLOAD_ENTRY_INFO_INVALID = ~0u
387  };
388 
390  : Flags(0), Order(~0u), Kind(OFFLOAD_ENTRY_INFO_INVALID) {}
392  int32_t Flags)
393  : Flags(Flags), Order(Order), Kind(Kind) {}
394 
395  bool isValid() const { return Order != ~0u; }
396  unsigned getOrder() const { return Order; }
398  int32_t getFlags() const { return Flags; }
399  void setFlags(int32_t NewFlags) { Flags = NewFlags; }
400  static bool classof(const OffloadEntryInfo *Info) { return true; }
401 
402  private:
403  /// Flags associated with the device global.
404  int32_t Flags;
405 
406  /// Order this entry was emitted.
407  unsigned Order;
408 
410  };
411 
412  /// \brief Return true if a there are no entries defined.
413  bool empty() const;
414  /// \brief Return number of entries defined so far.
415  unsigned size() const { return OffloadingEntriesNum; }
417  : CGM(CGM), OffloadingEntriesNum(0) {}
418 
419  ///
420  /// Target region entries related.
421  ///
422  /// \brief Target region entries info.
424  // \brief Address of the entity that has to be mapped for offloading.
425  llvm::Constant *Addr;
426  // \brief Address that can be used as the ID of the entry.
427  llvm::Constant *ID;
428 
429  public:
431  : OffloadEntryInfo(OFFLOAD_ENTRY_INFO_TARGET_REGION, ~0u,
432  /*Flags=*/0),
433  Addr(nullptr), ID(nullptr) {}
434  explicit OffloadEntryInfoTargetRegion(unsigned Order,
435  llvm::Constant *Addr,
436  llvm::Constant *ID, int32_t Flags)
437  : OffloadEntryInfo(OFFLOAD_ENTRY_INFO_TARGET_REGION, Order, Flags),
438  Addr(Addr), ID(ID) {}
439 
440  llvm::Constant *getAddress() const { return Addr; }
441  llvm::Constant *getID() const { return ID; }
442  void setAddress(llvm::Constant *V) {
443  assert(!Addr && "Address as been set before!");
444  Addr = V;
445  }
446  void setID(llvm::Constant *V) {
447  assert(!ID && "ID as been set before!");
448  ID = V;
449  }
450  static bool classof(const OffloadEntryInfo *Info) {
451  return Info->getKind() == OFFLOAD_ENTRY_INFO_TARGET_REGION;
452  }
453  };
454  /// \brief Initialize target region entry.
455  void initializeTargetRegionEntryInfo(unsigned DeviceID, unsigned FileID,
456  StringRef ParentName, unsigned LineNum,
457  unsigned Order);
458  /// \brief Register target region entry.
459  void registerTargetRegionEntryInfo(unsigned DeviceID, unsigned FileID,
460  StringRef ParentName, unsigned LineNum,
461  llvm::Constant *Addr, llvm::Constant *ID,
462  int32_t Flags);
463  /// \brief Return true if a target region entry with the provided
464  /// information exists.
465  bool hasTargetRegionEntryInfo(unsigned DeviceID, unsigned FileID,
466  StringRef ParentName, unsigned LineNum) const;
467  /// brief Applies action \a Action on all registered entries.
468  typedef llvm::function_ref<void(unsigned, unsigned, StringRef, unsigned,
471  void actOnTargetRegionEntriesInfo(
472  const OffloadTargetRegionEntryInfoActTy &Action);
473 
474  private:
475  // Storage for target region entries kind. The storage is to be indexed by
476  // file ID, device ID, parent function name and line number.
477  typedef llvm::DenseMap<unsigned, OffloadEntryInfoTargetRegion>
478  OffloadEntriesTargetRegionPerLine;
479  typedef llvm::StringMap<OffloadEntriesTargetRegionPerLine>
480  OffloadEntriesTargetRegionPerParentName;
481  typedef llvm::DenseMap<unsigned, OffloadEntriesTargetRegionPerParentName>
482  OffloadEntriesTargetRegionPerFile;
483  typedef llvm::DenseMap<unsigned, OffloadEntriesTargetRegionPerFile>
484  OffloadEntriesTargetRegionPerDevice;
485  typedef OffloadEntriesTargetRegionPerDevice OffloadEntriesTargetRegionTy;
486  OffloadEntriesTargetRegionTy OffloadEntriesTargetRegion;
487  };
489 
490  /// \brief Creates and registers offloading binary descriptor for the current
491  /// compilation unit. The function that does the registration is returned.
493 
494  /// \brief Creates all the offload entries in the current compilation unit
495  /// along with the associated metadata.
497 
498  /// \brief Loads all the offload entries information from the host IR
499  /// metadata.
501 
502  /// \brief Returns __tgt_offload_entry type.
504 
505  /// \brief Returns __tgt_device_image type.
507 
508  /// \brief Returns __tgt_bin_desc type.
510 
511  /// \brief Start scanning from statement \a S and and emit all target regions
512  /// found along the way.
513  /// \param S Starting statement.
514  /// \param ParentName Name of the function declaration that is being scanned.
515  void scanForTargetRegionsFunctions(const Stmt *S, StringRef ParentName);
516 
517  /// \brief Build type kmp_routine_entry_t (if not built yet).
518  void emitKmpRoutineEntryT(QualType KmpInt32Ty);
519 
520  /// \brief Returns pointer to kmpc_micro type.
521  llvm::Type *getKmpc_MicroPointerTy();
522 
523  /// \brief Returns specified OpenMP runtime function.
524  /// \param Function OpenMP runtime function.
525  /// \return Specified function.
526  llvm::Constant *createRuntimeFunction(unsigned Function);
527 
528  /// \brief Returns __kmpc_for_static_init_* runtime function for the specified
529  /// size \a IVSize and sign \a IVSigned.
530  llvm::Constant *createForStaticInitFunction(unsigned IVSize, bool IVSigned);
531 
532  /// \brief Returns __kmpc_dispatch_init_* runtime function for the specified
533  /// size \a IVSize and sign \a IVSigned.
534  llvm::Constant *createDispatchInitFunction(unsigned IVSize, bool IVSigned);
535 
536  /// \brief Returns __kmpc_dispatch_next_* runtime function for the specified
537  /// size \a IVSize and sign \a IVSigned.
538  llvm::Constant *createDispatchNextFunction(unsigned IVSize, bool IVSigned);
539 
540  /// \brief Returns __kmpc_dispatch_fini_* runtime function for the specified
541  /// size \a IVSize and sign \a IVSigned.
542  llvm::Constant *createDispatchFiniFunction(unsigned IVSize, bool IVSigned);
543 
544  /// \brief If the specified mangled name is not in the module, create and
545  /// return threadprivate cache object. This object is a pointer's worth of
546  /// storage that's reserved for use by the OpenMP runtime.
547  /// \param VD Threadprivate variable.
548  /// \return Cache variable for the specified threadprivate.
549  llvm::Constant *getOrCreateThreadPrivateCache(const VarDecl *VD);
550 
551  /// \brief Emits address of the word in a memory where current thread id is
552  /// stored.
554 
555  /// \brief Gets (if variable with the given name already exist) or creates
556  /// internal global variable with the specified Name. The created variable has
557  /// linkage CommonLinkage by default and is initialized by null value.
558  /// \param Ty Type of the global variable. If it is exist already the type
559  /// must be the same.
560  /// \param Name Name of the variable.
561  llvm::Constant *getOrCreateInternalVariable(llvm::Type *Ty,
562  const llvm::Twine &Name);
563 
564  /// \brief Set of threadprivate variables with the generated initializer.
565  llvm::SmallPtrSet<const VarDecl *, 4> ThreadPrivateWithDefinition;
566 
567  /// \brief Emits initialization code for the threadprivate variables.
568  /// \param VDAddr Address of the global variable \a VD.
569  /// \param Ctor Pointer to a global init function for \a VD.
570  /// \param CopyCtor Pointer to a global copy function for \a VD.
571  /// \param Dtor Pointer to a global destructor function for \a VD.
572  /// \param Loc Location of threadprivate declaration.
574  llvm::Value *Ctor, llvm::Value *CopyCtor,
575  llvm::Value *Dtor, SourceLocation Loc);
576 
577  /// \brief Returns corresponding lock object for the specified critical region
578  /// name. If the lock object does not exist it is created, otherwise the
579  /// reference to the existing copy is returned.
580  /// \param CriticalName Name of the critical region.
581  ///
582  llvm::Value *getCriticalRegionLock(StringRef CriticalName);
583 
584  struct TaskResultTy {
585  llvm::Value *NewTask = nullptr;
586  llvm::Value *TaskEntry = nullptr;
587  llvm::Value *NewTaskNewTaskTTy = nullptr;
589  RecordDecl *KmpTaskTQTyRD = nullptr;
590  llvm::Value *TaskDupFn = nullptr;
591  };
592  /// Emit task region for the task directive. The task region is emitted in
593  /// several steps:
594  /// 1. Emit a call to kmp_task_t *__kmpc_omp_task_alloc(ident_t *, kmp_int32
595  /// gtid, kmp_int32 flags, size_t sizeof_kmp_task_t, size_t sizeof_shareds,
596  /// kmp_routine_entry_t *task_entry). Here task_entry is a pointer to the
597  /// function:
598  /// kmp_int32 .omp_task_entry.(kmp_int32 gtid, kmp_task_t *tt) {
599  /// TaskFunction(gtid, tt->part_id, tt->shareds);
600  /// return 0;
601  /// }
602  /// 2. Copy a list of shared variables to field shareds of the resulting
603  /// structure kmp_task_t returned by the previous call (if any).
604  /// 3. Copy a pointer to destructions function to field destructions of the
605  /// resulting structure kmp_task_t.
606  /// \param D Current task directive.
607  /// \param TaskFunction An LLVM function with type void (*)(i32 /*gtid*/, i32
608  /// /*part_id*/, captured_struct */*__context*/);
609  /// \param SharedsTy A type which contains references the shared variables.
610  /// \param Shareds Context with the list of shared variables from the \p
611  /// TaskFunction.
612  /// \param Data Additional data for task generation like tiednsee, final
613  /// state, list of privates etc.
615  const OMPExecutableDirective &D,
616  llvm::Value *TaskFunction, QualType SharedsTy,
617  Address Shareds, const OMPTaskDataTy &Data);
618 
619 public:
620  explicit CGOpenMPRuntime(CodeGenModule &CGM);
621  virtual ~CGOpenMPRuntime() {}
622  virtual void clear();
623 
624  /// Emit code for the specified user defined reduction construct.
625  virtual void emitUserDefinedReduction(CodeGenFunction *CGF,
626  const OMPDeclareReductionDecl *D);
627  /// Get combiner/initializer for the specified user-defined reduction, if any.
628  virtual std::pair<llvm::Function *, llvm::Function *>
629  getUserDefinedReduction(const OMPDeclareReductionDecl *D);
630 
631  /// \brief Emits outlined function for the specified OpenMP parallel directive
632  /// \a D. This outlined function has type void(*)(kmp_int32 *ThreadID,
633  /// kmp_int32 BoundID, struct context_vars*).
634  /// \param D OpenMP directive.
635  /// \param ThreadIDVar Variable for thread id in the current OpenMP region.
636  /// \param InnermostKind Kind of innermost directive (for simple directives it
637  /// is a directive itself, for combined - its innermost directive).
638  /// \param CodeGen Code generation sequence for the \a D directive.
640  const OMPExecutableDirective &D, const VarDecl *ThreadIDVar,
641  OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen);
642 
643  /// \brief Emits outlined function for the specified OpenMP teams directive
644  /// \a D. This outlined function has type void(*)(kmp_int32 *ThreadID,
645  /// kmp_int32 BoundID, struct context_vars*).
646  /// \param D OpenMP directive.
647  /// \param ThreadIDVar Variable for thread id in the current OpenMP region.
648  /// \param InnermostKind Kind of innermost directive (for simple directives it
649  /// is a directive itself, for combined - its innermost directive).
650  /// \param CodeGen Code generation sequence for the \a D directive.
652  const OMPExecutableDirective &D, const VarDecl *ThreadIDVar,
653  OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen);
654 
655  /// \brief Emits outlined function for the OpenMP task directive \a D. This
656  /// outlined function has type void(*)(kmp_int32 ThreadID, struct task_t*
657  /// TaskT).
658  /// \param D OpenMP directive.
659  /// \param ThreadIDVar Variable for thread id in the current OpenMP region.
660  /// \param PartIDVar Variable for partition id in the current OpenMP untied
661  /// task region.
662  /// \param TaskTVar Variable for task_t argument.
663  /// \param InnermostKind Kind of innermost directive (for simple directives it
664  /// is a directive itself, for combined - its innermost directive).
665  /// \param CodeGen Code generation sequence for the \a D directive.
666  /// \param Tied true if task is generated for tied task, false otherwise.
667  /// \param NumberOfParts Number of parts in untied task. Ignored for tied
668  /// tasks.
669  ///
671  const OMPExecutableDirective &D, const VarDecl *ThreadIDVar,
672  const VarDecl *PartIDVar, const VarDecl *TaskTVar,
673  OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen,
674  bool Tied, unsigned &NumberOfParts);
675 
676  /// \brief Cleans up references to the objects in finished function.
677  ///
679 
680  /// \brief Emits code for parallel or serial call of the \a OutlinedFn with
681  /// variables captured in a record which address is stored in \a
682  /// CapturedStruct.
683  /// \param OutlinedFn Outlined function to be run in parallel threads. Type of
684  /// this function is void(*)(kmp_int32 *, kmp_int32, struct context_vars*).
685  /// \param CapturedVars A pointer to the record with the references to
686  /// variables used in \a OutlinedFn function.
687  /// \param IfCond Condition in the associated 'if' clause, if it was
688  /// specified, nullptr otherwise.
689  ///
690  virtual void emitParallelCall(CodeGenFunction &CGF, SourceLocation Loc,
691  llvm::Value *OutlinedFn,
692  ArrayRef<llvm::Value *> CapturedVars,
693  const Expr *IfCond);
694 
695  /// \brief Emits a critical region.
696  /// \param CriticalName Name of the critical region.
697  /// \param CriticalOpGen Generator for the statement associated with the given
698  /// critical region.
699  /// \param Hint Value of the 'hint' clause (optional).
700  virtual void emitCriticalRegion(CodeGenFunction &CGF, StringRef CriticalName,
701  const RegionCodeGenTy &CriticalOpGen,
702  SourceLocation Loc,
703  const Expr *Hint = nullptr);
704 
705  /// \brief Emits a master region.
706  /// \param MasterOpGen Generator for the statement associated with the given
707  /// master region.
708  virtual void emitMasterRegion(CodeGenFunction &CGF,
709  const RegionCodeGenTy &MasterOpGen,
710  SourceLocation Loc);
711 
712  /// \brief Emits code for a taskyield directive.
713  virtual void emitTaskyieldCall(CodeGenFunction &CGF, SourceLocation Loc);
714 
715  /// \brief Emit a taskgroup region.
716  /// \param TaskgroupOpGen Generator for the statement associated with the
717  /// given taskgroup region.
718  virtual void emitTaskgroupRegion(CodeGenFunction &CGF,
719  const RegionCodeGenTy &TaskgroupOpGen,
720  SourceLocation Loc);
721 
722  /// \brief Emits a single region.
723  /// \param SingleOpGen Generator for the statement associated with the given
724  /// single region.
725  virtual void emitSingleRegion(CodeGenFunction &CGF,
726  const RegionCodeGenTy &SingleOpGen,
727  SourceLocation Loc,
728  ArrayRef<const Expr *> CopyprivateVars,
729  ArrayRef<const Expr *> DestExprs,
730  ArrayRef<const Expr *> SrcExprs,
731  ArrayRef<const Expr *> AssignmentOps);
732 
733  /// \brief Emit an ordered region.
734  /// \param OrderedOpGen Generator for the statement associated with the given
735  /// ordered region.
736  virtual void emitOrderedRegion(CodeGenFunction &CGF,
737  const RegionCodeGenTy &OrderedOpGen,
738  SourceLocation Loc, bool IsThreads);
739 
740  /// \brief Emit an implicit/explicit barrier for OpenMP threads.
741  /// \param Kind Directive for which this implicit barrier call must be
742  /// generated. Must be OMPD_barrier for explicit barrier generation.
743  /// \param EmitChecks true if need to emit checks for cancellation barriers.
744  /// \param ForceSimpleCall true simple barrier call must be emitted, false if
745  /// runtime class decides which one to emit (simple or with cancellation
746  /// checks).
747  ///
748  virtual void emitBarrierCall(CodeGenFunction &CGF, SourceLocation Loc,
750  bool EmitChecks = true,
751  bool ForceSimpleCall = false);
752 
753  /// \brief Check if the specified \a ScheduleKind is static non-chunked.
754  /// This kind of worksharing directive is emitted without outer loop.
755  /// \param ScheduleKind Schedule kind specified in the 'schedule' clause.
756  /// \param Chunked True if chunk is specified in the clause.
757  ///
758  virtual bool isStaticNonchunked(OpenMPScheduleClauseKind ScheduleKind,
759  bool Chunked) const;
760 
761  /// \brief Check if the specified \a ScheduleKind is static non-chunked.
762  /// This kind of distribute directive is emitted without outer loop.
763  /// \param ScheduleKind Schedule kind specified in the 'dist_schedule' clause.
764  /// \param Chunked True if chunk is specified in the clause.
765  ///
766  virtual bool isStaticNonchunked(OpenMPDistScheduleClauseKind ScheduleKind,
767  bool Chunked) const;
768 
769  /// \brief Check if the specified \a ScheduleKind is dynamic.
770  /// This kind of worksharing directive is emitted without outer loop.
771  /// \param ScheduleKind Schedule Kind specified in the 'schedule' clause.
772  ///
773  virtual bool isDynamic(OpenMPScheduleClauseKind ScheduleKind) const;
774 
775  /// struct with the values to be passed to the dispatch runtime function
777  /// Loop lower bound
778  llvm::Value *LB = nullptr;
779  /// Loop upper bound
780  llvm::Value *UB = nullptr;
781  /// Chunk size specified using 'schedule' clause (nullptr if chunk
782  /// was not specified)
783  llvm::Value *Chunk = nullptr;
784  DispatchRTInput() = default;
786  : LB(LB), UB(UB), Chunk(Chunk) {}
787  };
788 
789  /// Call the appropriate runtime routine to initialize it before start
790  /// of loop.
791 
792  /// This is used for non static scheduled types and when the ordered
793  /// clause is present on the loop construct.
794  /// Depending on the loop schedule, it is necessary to call some runtime
795  /// routine before start of the OpenMP loop to get the loop upper / lower
796  /// bounds \a LB and \a UB and stride \a ST.
797  ///
798  /// \param CGF Reference to current CodeGenFunction.
799  /// \param Loc Clang source location.
800  /// \param ScheduleKind Schedule kind, specified by the 'schedule' clause.
801  /// \param IVSize Size of the iteration variable in bits.
802  /// \param IVSigned Sign of the iteration variable.
803  /// \param Ordered true if loop is ordered, false otherwise.
804  /// \param DispatchValues struct containing llvm values for lower bound, upper
805  /// bound, and chunk expression.
806  /// For the default (nullptr) value, the chunk 1 will be used.
807  ///
808  virtual void emitForDispatchInit(CodeGenFunction &CGF, SourceLocation Loc,
809  const OpenMPScheduleTy &ScheduleKind,
810  unsigned IVSize, bool IVSigned, bool Ordered,
811  const DispatchRTInput &DispatchValues);
812 
813  /// Struct with the values to be passed to the static runtime function
814  struct StaticRTInput {
815  /// Size of the iteration variable in bits.
816  unsigned IVSize = 0;
817  /// Sign of the iteration variable.
818  bool IVSigned = false;
819  /// true if loop is ordered, false otherwise.
820  bool Ordered = false;
821  /// Address of the output variable in which the flag of the last iteration
822  /// is returned.
823  Address IL = Address::invalid();
824  /// Address of the output variable in which the lower iteration number is
825  /// returned.
826  Address LB = Address::invalid();
827  /// Address of the output variable in which the upper iteration number is
828  /// returned.
829  Address UB = Address::invalid();
830  /// Address of the output variable in which the stride value is returned
831  /// necessary to generated the static_chunked scheduled loop.
832  Address ST = Address::invalid();
833  /// Value of the chunk for the static_chunked scheduled loop. For the
834  /// default (nullptr) value, the chunk 1 will be used.
835  llvm::Value *Chunk = nullptr;
836  StaticRTInput(unsigned IVSize, bool IVSigned, bool Ordered, Address IL,
837  Address LB, Address UB, Address ST,
838  llvm::Value *Chunk = nullptr)
839  : IVSize(IVSize), IVSigned(IVSigned), Ordered(Ordered), IL(IL), LB(LB),
840  UB(UB), ST(ST), Chunk(Chunk) {}
841  };
842  /// \brief Call the appropriate runtime routine to initialize it before start
843  /// of loop.
844  ///
845  /// This is used only in case of static schedule, when the user did not
846  /// specify a ordered clause on the loop construct.
847  /// Depending on the loop schedule, it is necessary to call some runtime
848  /// routine before start of the OpenMP loop to get the loop upper / lower
849  /// bounds LB and UB and stride ST.
850  ///
851  /// \param CGF Reference to current CodeGenFunction.
852  /// \param Loc Clang source location.
853  /// \param DKind Kind of the directive.
854  /// \param ScheduleKind Schedule kind, specified by the 'schedule' clause.
855  /// \param Values Input arguments for the construct.
856  ///
857  virtual void emitForStaticInit(CodeGenFunction &CGF, SourceLocation Loc,
858  OpenMPDirectiveKind DKind,
859  const OpenMPScheduleTy &ScheduleKind,
860  const StaticRTInput &Values);
861 
862  ///
863  /// \param CGF Reference to current CodeGenFunction.
864  /// \param Loc Clang source location.
865  /// \param SchedKind Schedule kind, specified by the 'dist_schedule' clause.
866  /// \param Values Input arguments for the construct.
867  ///
868  virtual void emitDistributeStaticInit(CodeGenFunction &CGF,
869  SourceLocation Loc,
871  const StaticRTInput &Values);
872 
873  /// \brief Call the appropriate runtime routine to notify that we finished
874  /// iteration of the ordered loop with the dynamic scheduling.
875  ///
876  /// \param CGF Reference to current CodeGenFunction.
877  /// \param Loc Clang source location.
878  /// \param IVSize Size of the iteration variable in bits.
879  /// \param IVSigned Sign of the iteration variable.
880  ///
882  SourceLocation Loc, unsigned IVSize,
883  bool IVSigned);
884 
885  /// \brief Call the appropriate runtime routine to notify that we finished
886  /// all the work with current loop.
887  ///
888  /// \param CGF Reference to current CodeGenFunction.
889  /// \param Loc Clang source location.
890  /// \param DKind Kind of the directive for which the static finish is emitted.
891  ///
892  virtual void emitForStaticFinish(CodeGenFunction &CGF, SourceLocation Loc,
893  OpenMPDirectiveKind DKind);
894 
895  /// Call __kmpc_dispatch_next(
896  /// ident_t *loc, kmp_int32 tid, kmp_int32 *p_lastiter,
897  /// kmp_int[32|64] *p_lower, kmp_int[32|64] *p_upper,
898  /// kmp_int[32|64] *p_stride);
899  /// \param IVSize Size of the iteration variable in bits.
900  /// \param IVSigned Sign of the iteration variable.
901  /// \param IL Address of the output variable in which the flag of the
902  /// last iteration is returned.
903  /// \param LB Address of the output variable in which the lower iteration
904  /// number is returned.
905  /// \param UB Address of the output variable in which the upper iteration
906  /// number is returned.
907  /// \param ST Address of the output variable in which the stride value is
908  /// returned.
910  unsigned IVSize, bool IVSigned,
911  Address IL, Address LB,
912  Address UB, Address ST);
913 
914  /// \brief Emits call to void __kmpc_push_num_threads(ident_t *loc, kmp_int32
915  /// global_tid, kmp_int32 num_threads) to generate code for 'num_threads'
916  /// clause.
917  /// \param NumThreads An integer value of threads.
918  virtual void emitNumThreadsClause(CodeGenFunction &CGF,
919  llvm::Value *NumThreads,
920  SourceLocation Loc);
921 
922  /// \brief Emit call to void __kmpc_push_proc_bind(ident_t *loc, kmp_int32
923  /// global_tid, int proc_bind) to generate code for 'proc_bind' clause.
924  virtual void emitProcBindClause(CodeGenFunction &CGF,
925  OpenMPProcBindClauseKind ProcBind,
926  SourceLocation Loc);
927 
928  /// \brief Returns address of the threadprivate variable for the current
929  /// thread.
930  /// \param VD Threadprivate variable.
931  /// \param VDAddr Address of the global variable \a VD.
932  /// \param Loc Location of the reference to threadprivate var.
933  /// \return Address of the threadprivate variable for the current thread.
935  const VarDecl *VD,
936  Address VDAddr,
937  SourceLocation Loc);
938 
939  /// \brief Emit a code for initialization of threadprivate variable. It emits
940  /// a call to runtime library which adds initial value to the newly created
941  /// threadprivate variable (if it is not constant) and registers destructor
942  /// for the variable (if any).
943  /// \param VD Threadprivate variable.
944  /// \param VDAddr Address of the global variable \a VD.
945  /// \param Loc Location of threadprivate declaration.
946  /// \param PerformInit true if initialization expression is not constant.
947  virtual llvm::Function *
949  SourceLocation Loc, bool PerformInit,
950  CodeGenFunction *CGF = nullptr);
951 
952  /// Creates artificial threadprivate variable with name \p Name and type \p
953  /// VarType.
954  /// \param VarType Type of the artificial threadprivate variable.
955  /// \param Name Name of the artificial threadprivate variable.
957  QualType VarType,
958  StringRef Name);
959 
960  /// \brief Emit flush of the variables specified in 'omp flush' directive.
961  /// \param Vars List of variables to flush.
962  virtual void emitFlush(CodeGenFunction &CGF, ArrayRef<const Expr *> Vars,
963  SourceLocation Loc);
964 
965  /// \brief Emit task region for the task directive. The task region is
966  /// emitted in several steps:
967  /// 1. Emit a call to kmp_task_t *__kmpc_omp_task_alloc(ident_t *, kmp_int32
968  /// gtid, kmp_int32 flags, size_t sizeof_kmp_task_t, size_t sizeof_shareds,
969  /// kmp_routine_entry_t *task_entry). Here task_entry is a pointer to the
970  /// function:
971  /// kmp_int32 .omp_task_entry.(kmp_int32 gtid, kmp_task_t *tt) {
972  /// TaskFunction(gtid, tt->part_id, tt->shareds);
973  /// return 0;
974  /// }
975  /// 2. Copy a list of shared variables to field shareds of the resulting
976  /// structure kmp_task_t returned by the previous call (if any).
977  /// 3. Copy a pointer to destructions function to field destructions of the
978  /// resulting structure kmp_task_t.
979  /// 4. Emit a call to kmp_int32 __kmpc_omp_task(ident_t *, kmp_int32 gtid,
980  /// kmp_task_t *new_task), where new_task is a resulting structure from
981  /// previous items.
982  /// \param D Current task directive.
983  /// \param TaskFunction An LLVM function with type void (*)(i32 /*gtid*/, i32
984  /// /*part_id*/, captured_struct */*__context*/);
985  /// \param SharedsTy A type which contains references the shared variables.
986  /// \param Shareds Context with the list of shared variables from the \p
987  /// TaskFunction.
988  /// \param IfCond Not a nullptr if 'if' clause was specified, nullptr
989  /// otherwise.
990  /// \param Data Additional data for task generation like tiednsee, final
991  /// state, list of privates etc.
992  virtual void emitTaskCall(CodeGenFunction &CGF, SourceLocation Loc,
993  const OMPExecutableDirective &D,
994  llvm::Value *TaskFunction, QualType SharedsTy,
995  Address Shareds, const Expr *IfCond,
996  const OMPTaskDataTy &Data);
997 
998  /// Emit task region for the taskloop directive. The taskloop region is
999  /// emitted in several steps:
1000  /// 1. Emit a call to kmp_task_t *__kmpc_omp_task_alloc(ident_t *, kmp_int32
1001  /// gtid, kmp_int32 flags, size_t sizeof_kmp_task_t, size_t sizeof_shareds,
1002  /// kmp_routine_entry_t *task_entry). Here task_entry is a pointer to the
1003  /// function:
1004  /// kmp_int32 .omp_task_entry.(kmp_int32 gtid, kmp_task_t *tt) {
1005  /// TaskFunction(gtid, tt->part_id, tt->shareds);
1006  /// return 0;
1007  /// }
1008  /// 2. Copy a list of shared variables to field shareds of the resulting
1009  /// structure kmp_task_t returned by the previous call (if any).
1010  /// 3. Copy a pointer to destructions function to field destructions of the
1011  /// resulting structure kmp_task_t.
1012  /// 4. Emit a call to void __kmpc_taskloop(ident_t *loc, int gtid, kmp_task_t
1013  /// *task, int if_val, kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st, int
1014  /// nogroup, int sched, kmp_uint64 grainsize, void *task_dup ), where new_task
1015  /// is a resulting structure from
1016  /// previous items.
1017  /// \param D Current task directive.
1018  /// \param TaskFunction An LLVM function with type void (*)(i32 /*gtid*/, i32
1019  /// /*part_id*/, captured_struct */*__context*/);
1020  /// \param SharedsTy A type which contains references the shared variables.
1021  /// \param Shareds Context with the list of shared variables from the \p
1022  /// TaskFunction.
1023  /// \param IfCond Not a nullptr if 'if' clause was specified, nullptr
1024  /// otherwise.
1025  /// \param Data Additional data for task generation like tiednsee, final
1026  /// state, list of privates etc.
1027  virtual void emitTaskLoopCall(
1028  CodeGenFunction &CGF, SourceLocation Loc, const OMPLoopDirective &D,
1029  llvm::Value *TaskFunction, QualType SharedsTy, Address Shareds,
1030  const Expr *IfCond, const OMPTaskDataTy &Data);
1031 
1032  /// \brief Emit code for the directive that does not require outlining.
1033  ///
1034  /// \param InnermostKind Kind of innermost directive (for simple directives it
1035  /// is a directive itself, for combined - its innermost directive).
1036  /// \param CodeGen Code generation sequence for the \a D directive.
1037  /// \param HasCancel true if region has inner cancel directive, false
1038  /// otherwise.
1039  virtual void emitInlinedDirective(CodeGenFunction &CGF,
1040  OpenMPDirectiveKind InnermostKind,
1041  const RegionCodeGenTy &CodeGen,
1042  bool HasCancel = false);
1043 
1044  /// Emits reduction function.
1045  /// \param ArgsType Array type containing pointers to reduction variables.
1046  /// \param Privates List of private copies for original reduction arguments.
1047  /// \param LHSExprs List of LHS in \a ReductionOps reduction operations.
1048  /// \param RHSExprs List of RHS in \a ReductionOps reduction operations.
1049  /// \param ReductionOps List of reduction operations in form 'LHS binop RHS'
1050  /// or 'operator binop(LHS, RHS)'.
1052  llvm::Type *ArgsType,
1054  ArrayRef<const Expr *> LHSExprs,
1055  ArrayRef<const Expr *> RHSExprs,
1056  ArrayRef<const Expr *> ReductionOps);
1057 
1058  /// Emits single reduction combiner
1060  const Expr *ReductionOp,
1061  const Expr *PrivateRef,
1062  const DeclRefExpr *LHS,
1063  const DeclRefExpr *RHS);
1064 
1069  };
1070  /// \brief Emit a code for reduction clause. Next code should be emitted for
1071  /// reduction:
1072  /// \code
1073  ///
1074  /// static kmp_critical_name lock = { 0 };
1075  ///
1076  /// void reduce_func(void *lhs[<n>], void *rhs[<n>]) {
1077  /// ...
1078  /// *(Type<i>*)lhs[i] = RedOp<i>(*(Type<i>*)lhs[i], *(Type<i>*)rhs[i]);
1079  /// ...
1080  /// }
1081  ///
1082  /// ...
1083  /// void *RedList[<n>] = {&<RHSExprs>[0], ..., &<RHSExprs>[<n>-1]};
1084  /// switch (__kmpc_reduce{_nowait}(<loc>, <gtid>, <n>, sizeof(RedList),
1085  /// RedList, reduce_func, &<lock>)) {
1086  /// case 1:
1087  /// ...
1088  /// <LHSExprs>[i] = RedOp<i>(*<LHSExprs>[i], *<RHSExprs>[i]);
1089  /// ...
1090  /// __kmpc_end_reduce{_nowait}(<loc>, <gtid>, &<lock>);
1091  /// break;
1092  /// case 2:
1093  /// ...
1094  /// Atomic(<LHSExprs>[i] = RedOp<i>(*<LHSExprs>[i], *<RHSExprs>[i]));
1095  /// ...
1096  /// break;
1097  /// default:;
1098  /// }
1099  /// \endcode
1100  ///
1101  /// \param Privates List of private copies for original reduction arguments.
1102  /// \param LHSExprs List of LHS in \a ReductionOps reduction operations.
1103  /// \param RHSExprs List of RHS in \a ReductionOps reduction operations.
1104  /// \param ReductionOps List of reduction operations in form 'LHS binop RHS'
1105  /// or 'operator binop(LHS, RHS)'.
1106  /// \param Options List of options for reduction codegen:
1107  /// WithNowait true if parent directive has also nowait clause, false
1108  /// otherwise.
1109  /// SimpleReduction Emit reduction operation only. Used for omp simd
1110  /// directive on the host.
1111  /// ReductionKind The kind of reduction to perform.
1112  virtual void emitReduction(CodeGenFunction &CGF, SourceLocation Loc,
1114  ArrayRef<const Expr *> LHSExprs,
1115  ArrayRef<const Expr *> RHSExprs,
1116  ArrayRef<const Expr *> ReductionOps,
1117  ReductionOptionsTy Options);
1118 
1119  /// Emit a code for initialization of task reduction clause. Next code
1120  /// should be emitted for reduction:
1121  /// \code
1122  ///
1123  /// _task_red_item_t red_data[n];
1124  /// ...
1125  /// red_data[i].shar = &origs[i];
1126  /// red_data[i].size = sizeof(origs[i]);
1127  /// red_data[i].f_init = (void*)RedInit<i>;
1128  /// red_data[i].f_fini = (void*)RedDest<i>;
1129  /// red_data[i].f_comb = (void*)RedOp<i>;
1130  /// red_data[i].flags = <Flag_i>;
1131  /// ...
1132  /// void* tg1 = __kmpc_task_reduction_init(gtid, n, red_data);
1133  /// \endcode
1134  ///
1135  /// \param LHSExprs List of LHS in \a Data.ReductionOps reduction operations.
1136  /// \param RHSExprs List of RHS in \a Data.ReductionOps reduction operations.
1137  /// \param Data Additional data for task generation like tiedness, final
1138  /// state, list of privates, reductions etc.
1140  SourceLocation Loc,
1141  ArrayRef<const Expr *> LHSExprs,
1142  ArrayRef<const Expr *> RHSExprs,
1143  const OMPTaskDataTy &Data);
1144 
1145  /// Required to resolve existing problems in the runtime. Emits threadprivate
1146  /// variables to store the size of the VLAs/array sections for
1147  /// initializer/combiner/finalizer functions + emits threadprivate variable to
1148  /// store the pointer to the original reduction item for the custom
1149  /// initializer defined by declare reduction construct.
1150  /// \param RCG Allows to reuse an existing data for the reductions.
1151  /// \param N Reduction item for which fixups must be emitted.
1153  ReductionCodeGen &RCG, unsigned N);
1154 
1155  /// Get the address of `void *` type of the privatue copy of the reduction
1156  /// item specified by the \p SharedLVal.
1157  /// \param ReductionsPtr Pointer to the reduction data returned by the
1158  /// emitTaskReductionInit function.
1159  /// \param SharedLVal Address of the original reduction item.
1161  llvm::Value *ReductionsPtr,
1162  LValue SharedLVal);
1163 
1164  /// \brief Emit code for 'taskwait' directive.
1165  virtual void emitTaskwaitCall(CodeGenFunction &CGF, SourceLocation Loc);
1166 
1167  /// \brief Emit code for 'cancellation point' construct.
1168  /// \param CancelRegion Region kind for which the cancellation point must be
1169  /// emitted.
1170  ///
1171  virtual void emitCancellationPointCall(CodeGenFunction &CGF,
1172  SourceLocation Loc,
1173  OpenMPDirectiveKind CancelRegion);
1174 
1175  /// \brief Emit code for 'cancel' construct.
1176  /// \param IfCond Condition in the associated 'if' clause, if it was
1177  /// specified, nullptr otherwise.
1178  /// \param CancelRegion Region kind for which the cancel must be emitted.
1179  ///
1180  virtual void emitCancelCall(CodeGenFunction &CGF, SourceLocation Loc,
1181  const Expr *IfCond,
1182  OpenMPDirectiveKind CancelRegion);
1183 
1184  /// \brief Emit outilined function for 'target' directive.
1185  /// \param D Directive to emit.
1186  /// \param ParentName Name of the function that encloses the target region.
1187  /// \param OutlinedFn Outlined function value to be defined by this call.
1188  /// \param OutlinedFnID Outlined function ID value to be defined by this call.
1189  /// \param IsOffloadEntry True if the outlined function is an offload entry.
1190  /// \param CodeGen Code generation sequence for the \a D directive.
1191  /// An outlined function may not be an entry if, e.g. the if clause always
1192  /// evaluates to false.
1193  virtual void emitTargetOutlinedFunction(const OMPExecutableDirective &D,
1194  StringRef ParentName,
1195  llvm::Function *&OutlinedFn,
1196  llvm::Constant *&OutlinedFnID,
1197  bool IsOffloadEntry,
1198  const RegionCodeGenTy &CodeGen);
1199 
1200  /// \brief Emit the target offloading code associated with \a D. The emitted
1201  /// code attempts offloading the execution to the device, an the event of
1202  /// a failure it executes the host version outlined in \a OutlinedFn.
1203  /// \param D Directive to emit.
1204  /// \param OutlinedFn Host version of the code to be offloaded.
1205  /// \param OutlinedFnID ID of host version of the code to be offloaded.
1206  /// \param IfCond Expression evaluated in if clause associated with the target
1207  /// directive, or null if no if clause is used.
1208  /// \param Device Expression evaluated in device clause associated with the
1209  /// target directive, or null if no device clause is used.
1210  virtual void emitTargetCall(CodeGenFunction &CGF,
1211  const OMPExecutableDirective &D,
1212  llvm::Value *OutlinedFn,
1213  llvm::Value *OutlinedFnID, const Expr *IfCond,
1214  const Expr *Device);
1215 
1216  /// \brief Emit the target regions enclosed in \a GD function definition or
1217  /// the function itself in case it is a valid device function. Returns true if
1218  /// \a GD was dealt with successfully.
1219  /// \param GD Function to scan.
1220  virtual bool emitTargetFunctions(GlobalDecl GD);
1221 
1222  /// \brief Emit the global variable if it is a valid device global variable.
1223  /// Returns true if \a GD was dealt with successfully.
1224  /// \param GD Variable declaration to emit.
1225  virtual bool emitTargetGlobalVariable(GlobalDecl GD);
1226 
1227  /// \brief Emit the global \a GD if it is meaningful for the target. Returns
1228  /// if it was emitted successfully.
1229  /// \param GD Global to scan.
1230  virtual bool emitTargetGlobal(GlobalDecl GD);
1231 
1232  /// \brief Creates the offloading descriptor in the event any target region
1233  /// was emitted in the current module and return the function that registers
1234  /// it.
1235  virtual llvm::Function *emitRegistrationFunction();
1236 
1237  /// \brief Emits code for teams call of the \a OutlinedFn with
1238  /// variables captured in a record which address is stored in \a
1239  /// CapturedStruct.
1240  /// \param OutlinedFn Outlined function to be run by team masters. Type of
1241  /// this function is void(*)(kmp_int32 *, kmp_int32, struct context_vars*).
1242  /// \param CapturedVars A pointer to the record with the references to
1243  /// variables used in \a OutlinedFn function.
1244  ///
1245  virtual void emitTeamsCall(CodeGenFunction &CGF,
1246  const OMPExecutableDirective &D,
1247  SourceLocation Loc, llvm::Value *OutlinedFn,
1248  ArrayRef<llvm::Value *> CapturedVars);
1249 
1250  /// \brief Emits call to void __kmpc_push_num_teams(ident_t *loc, kmp_int32
1251  /// global_tid, kmp_int32 num_teams, kmp_int32 thread_limit) to generate code
1252  /// for num_teams clause.
1253  /// \param NumTeams An integer expression of teams.
1254  /// \param ThreadLimit An integer expression of threads.
1255  virtual void emitNumTeamsClause(CodeGenFunction &CGF, const Expr *NumTeams,
1256  const Expr *ThreadLimit, SourceLocation Loc);
1257 
1258  /// Struct that keeps all the relevant information that should be kept
1259  /// throughout a 'target data' region.
1261  /// Set to true if device pointer information have to be obtained.
1262  bool RequiresDevicePointerInfo = false;
1263 
1264  public:
1265  /// The array of base pointer passed to the runtime library.
1266  llvm::Value *BasePointersArray = nullptr;
1267  /// The array of section pointers passed to the runtime library.
1268  llvm::Value *PointersArray = nullptr;
1269  /// The array of sizes passed to the runtime library.
1270  llvm::Value *SizesArray = nullptr;
1271  /// The array of map types passed to the runtime library.
1272  llvm::Value *MapTypesArray = nullptr;
1273  /// The total number of pointers passed to the runtime library.
1274  unsigned NumberOfPtrs = 0u;
1275  /// Map between the a declaration of a capture and the corresponding base
1276  /// pointer address where the runtime returns the device pointers.
1277  llvm::DenseMap<const ValueDecl *, Address> CaptureDeviceAddrMap;
1278 
1279  explicit TargetDataInfo() {}
1280  explicit TargetDataInfo(bool RequiresDevicePointerInfo)
1281  : RequiresDevicePointerInfo(RequiresDevicePointerInfo) {}
1282  /// Clear information about the data arrays.
1284  BasePointersArray = nullptr;
1285  PointersArray = nullptr;
1286  SizesArray = nullptr;
1287  MapTypesArray = nullptr;
1288  NumberOfPtrs = 0u;
1289  }
1290  /// Return true if the current target data information has valid arrays.
1291  bool isValid() {
1292  return BasePointersArray && PointersArray && SizesArray &&
1293  MapTypesArray && NumberOfPtrs;
1294  }
1295  bool requiresDevicePointerInfo() { return RequiresDevicePointerInfo; }
1296  };
1297 
1298  /// \brief Emit the target data mapping code associated with \a D.
1299  /// \param D Directive to emit.
1300  /// \param IfCond Expression evaluated in if clause associated with the
1301  /// target directive, or null if no device clause is used.
1302  /// \param Device Expression evaluated in device clause associated with the
1303  /// target directive, or null if no device clause is used.
1304  /// \param Info A record used to store information that needs to be preserved
1305  /// until the region is closed.
1306  virtual void emitTargetDataCalls(CodeGenFunction &CGF,
1307  const OMPExecutableDirective &D,
1308  const Expr *IfCond, const Expr *Device,
1309  const RegionCodeGenTy &CodeGen,
1310  TargetDataInfo &Info);
1311 
1312  /// \brief Emit the data mapping/movement code associated with the directive
1313  /// \a D that should be of the form 'target [{enter|exit} data | update]'.
1314  /// \param D Directive to emit.
1315  /// \param IfCond Expression evaluated in if clause associated with the target
1316  /// directive, or null if no if clause is used.
1317  /// \param Device Expression evaluated in device clause associated with the
1318  /// target directive, or null if no device clause is used.
1320  const OMPExecutableDirective &D,
1321  const Expr *IfCond,
1322  const Expr *Device);
1323 
1324  /// Marks function \a Fn with properly mangled versions of vector functions.
1325  /// \param FD Function marked as 'declare simd'.
1326  /// \param Fn LLVM function that must be marked with 'declare simd'
1327  /// attributes.
1328  virtual void emitDeclareSimdFunction(const FunctionDecl *FD,
1329  llvm::Function *Fn);
1330 
1331  /// Emit initialization for doacross loop nesting support.
1332  /// \param D Loop-based construct used in doacross nesting construct.
1333  virtual void emitDoacrossInit(CodeGenFunction &CGF,
1334  const OMPLoopDirective &D);
1335 
1336  /// Emit code for doacross ordered directive with 'depend' clause.
1337  /// \param C 'depend' clause with 'sink|source' dependency kind.
1338  virtual void emitDoacrossOrdered(CodeGenFunction &CGF,
1339  const OMPDependClause *C);
1340 
1341  /// Translates the native parameter of outlined function if this is required
1342  /// for target.
1343  /// \param FD Field decl from captured record for the paramater.
1344  /// \param NativeParam Parameter itself.
1345  virtual const VarDecl *translateParameter(const FieldDecl *FD,
1346  const VarDecl *NativeParam) const {
1347  return NativeParam;
1348  }
1349 
1350  /// Gets the address of the native argument basing on the address of the
1351  /// target-specific parameter.
1352  /// \param NativeParam Parameter itself.
1353  /// \param TargetParam Corresponding target-specific parameter.
1355  const VarDecl *NativeParam,
1356  const VarDecl *TargetParam) const;
1357 
1358  /// Emits call of the outlined function with the provided arguments,
1359  /// translating these arguments to correct target-specific arguments.
1360  virtual void
1362  llvm::Value *OutlinedFn,
1363  ArrayRef<llvm::Value *> Args = llvm::None) const;
1364 };
1365 
1366 /// Class supports emissionof SIMD-only code.
1367 class CGOpenMPSIMDRuntime final : public CGOpenMPRuntime {
1368 public:
1369  explicit CGOpenMPSIMDRuntime(CodeGenModule &CGM) : CGOpenMPRuntime(CGM) {}
1370  ~CGOpenMPSIMDRuntime() override {}
1371 
1372  /// \brief Emits outlined function for the specified OpenMP parallel directive
1373  /// \a D. This outlined function has type void(*)(kmp_int32 *ThreadID,
1374  /// kmp_int32 BoundID, struct context_vars*).
1375  /// \param D OpenMP directive.
1376  /// \param ThreadIDVar Variable for thread id in the current OpenMP region.
1377  /// \param InnermostKind Kind of innermost directive (for simple directives it
1378  /// is a directive itself, for combined - its innermost directive).
1379  /// \param CodeGen Code generation sequence for the \a D directive.
1380  llvm::Value *
1382  const VarDecl *ThreadIDVar,
1383  OpenMPDirectiveKind InnermostKind,
1384  const RegionCodeGenTy &CodeGen) override;
1385 
1386  /// \brief Emits outlined function for the specified OpenMP teams directive
1387  /// \a D. This outlined function has type void(*)(kmp_int32 *ThreadID,
1388  /// kmp_int32 BoundID, struct context_vars*).
1389  /// \param D OpenMP directive.
1390  /// \param ThreadIDVar Variable for thread id in the current OpenMP region.
1391  /// \param InnermostKind Kind of innermost directive (for simple directives it
1392  /// is a directive itself, for combined - its innermost directive).
1393  /// \param CodeGen Code generation sequence for the \a D directive.
1394  llvm::Value *
1396  const VarDecl *ThreadIDVar,
1397  OpenMPDirectiveKind InnermostKind,
1398  const RegionCodeGenTy &CodeGen) override;
1399 
1400  /// \brief Emits outlined function for the OpenMP task directive \a D. This
1401  /// outlined function has type void(*)(kmp_int32 ThreadID, struct task_t*
1402  /// TaskT).
1403  /// \param D OpenMP directive.
1404  /// \param ThreadIDVar Variable for thread id in the current OpenMP region.
1405  /// \param PartIDVar Variable for partition id in the current OpenMP untied
1406  /// task region.
1407  /// \param TaskTVar Variable for task_t argument.
1408  /// \param InnermostKind Kind of innermost directive (for simple directives it
1409  /// is a directive itself, for combined - its innermost directive).
1410  /// \param CodeGen Code generation sequence for the \a D directive.
1411  /// \param Tied true if task is generated for tied task, false otherwise.
1412  /// \param NumberOfParts Number of parts in untied task. Ignored for tied
1413  /// tasks.
1414  ///
1416  const OMPExecutableDirective &D, const VarDecl *ThreadIDVar,
1417  const VarDecl *PartIDVar, const VarDecl *TaskTVar,
1418  OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen,
1419  bool Tied, unsigned &NumberOfParts) override;
1420 
1421  /// \brief Emits code for parallel or serial call of the \a OutlinedFn with
1422  /// variables captured in a record which address is stored in \a
1423  /// CapturedStruct.
1424  /// \param OutlinedFn Outlined function to be run in parallel threads. Type of
1425  /// this function is void(*)(kmp_int32 *, kmp_int32, struct context_vars*).
1426  /// \param CapturedVars A pointer to the record with the references to
1427  /// variables used in \a OutlinedFn function.
1428  /// \param IfCond Condition in the associated 'if' clause, if it was
1429  /// specified, nullptr otherwise.
1430  ///
1431  void emitParallelCall(CodeGenFunction &CGF, SourceLocation Loc,
1432  llvm::Value *OutlinedFn,
1433  ArrayRef<llvm::Value *> CapturedVars,
1434  const Expr *IfCond) override;
1435 
1436  /// \brief Emits a critical region.
1437  /// \param CriticalName Name of the critical region.
1438  /// \param CriticalOpGen Generator for the statement associated with the given
1439  /// critical region.
1440  /// \param Hint Value of the 'hint' clause (optional).
1441  void emitCriticalRegion(CodeGenFunction &CGF, StringRef CriticalName,
1442  const RegionCodeGenTy &CriticalOpGen,
1443  SourceLocation Loc,
1444  const Expr *Hint = nullptr) override;
1445 
1446  /// \brief Emits a master region.
1447  /// \param MasterOpGen Generator for the statement associated with the given
1448  /// master region.
1449  void emitMasterRegion(CodeGenFunction &CGF,
1450  const RegionCodeGenTy &MasterOpGen,
1451  SourceLocation Loc) override;
1452 
1453  /// \brief Emits code for a taskyield directive.
1454  void emitTaskyieldCall(CodeGenFunction &CGF, SourceLocation Loc) override;
1455 
1456  /// \brief Emit a taskgroup region.
1457  /// \param TaskgroupOpGen Generator for the statement associated with the
1458  /// given taskgroup region.
1459  void emitTaskgroupRegion(CodeGenFunction &CGF,
1460  const RegionCodeGenTy &TaskgroupOpGen,
1461  SourceLocation Loc) override;
1462 
1463  /// \brief Emits a single region.
1464  /// \param SingleOpGen Generator for the statement associated with the given
1465  /// single region.
1466  void emitSingleRegion(CodeGenFunction &CGF,
1467  const RegionCodeGenTy &SingleOpGen, SourceLocation Loc,
1468  ArrayRef<const Expr *> CopyprivateVars,
1469  ArrayRef<const Expr *> DestExprs,
1470  ArrayRef<const Expr *> SrcExprs,
1471  ArrayRef<const Expr *> AssignmentOps) override;
1472 
1473  /// \brief Emit an ordered region.
1474  /// \param OrderedOpGen Generator for the statement associated with the given
1475  /// ordered region.
1476  void emitOrderedRegion(CodeGenFunction &CGF,
1477  const RegionCodeGenTy &OrderedOpGen,
1478  SourceLocation Loc, bool IsThreads) override;
1479 
1480  /// \brief Emit an implicit/explicit barrier for OpenMP threads.
1481  /// \param Kind Directive for which this implicit barrier call must be
1482  /// generated. Must be OMPD_barrier for explicit barrier generation.
1483  /// \param EmitChecks true if need to emit checks for cancellation barriers.
1484  /// \param ForceSimpleCall true simple barrier call must be emitted, false if
1485  /// runtime class decides which one to emit (simple or with cancellation
1486  /// checks).
1487  ///
1488  void emitBarrierCall(CodeGenFunction &CGF, SourceLocation Loc,
1489  OpenMPDirectiveKind Kind, bool EmitChecks = true,
1490  bool ForceSimpleCall = false) override;
1491 
1492  /// This is used for non static scheduled types and when the ordered
1493  /// clause is present on the loop construct.
1494  /// Depending on the loop schedule, it is necessary to call some runtime
1495  /// routine before start of the OpenMP loop to get the loop upper / lower
1496  /// bounds \a LB and \a UB and stride \a ST.
1497  ///
1498  /// \param CGF Reference to current CodeGenFunction.
1499  /// \param Loc Clang source location.
1500  /// \param ScheduleKind Schedule kind, specified by the 'schedule' clause.
1501  /// \param IVSize Size of the iteration variable in bits.
1502  /// \param IVSigned Sign of the iteration variable.
1503  /// \param Ordered true if loop is ordered, false otherwise.
1504  /// \param DispatchValues struct containing llvm values for lower bound, upper
1505  /// bound, and chunk expression.
1506  /// For the default (nullptr) value, the chunk 1 will be used.
1507  ///
1508  void emitForDispatchInit(CodeGenFunction &CGF, SourceLocation Loc,
1509  const OpenMPScheduleTy &ScheduleKind,
1510  unsigned IVSize, bool IVSigned, bool Ordered,
1511  const DispatchRTInput &DispatchValues) override;
1512 
1513  /// \brief Call the appropriate runtime routine to initialize it before start
1514  /// of loop.
1515  ///
1516  /// This is used only in case of static schedule, when the user did not
1517  /// specify a ordered clause on the loop construct.
1518  /// Depending on the loop schedule, it is necessary to call some runtime
1519  /// routine before start of the OpenMP loop to get the loop upper / lower
1520  /// bounds LB and UB and stride ST.
1521  ///
1522  /// \param CGF Reference to current CodeGenFunction.
1523  /// \param Loc Clang source location.
1524  /// \param DKind Kind of the directive.
1525  /// \param ScheduleKind Schedule kind, specified by the 'schedule' clause.
1526  /// \param Values Input arguments for the construct.
1527  ///
1528  void emitForStaticInit(CodeGenFunction &CGF, SourceLocation Loc,
1529  OpenMPDirectiveKind DKind,
1530  const OpenMPScheduleTy &ScheduleKind,
1531  const StaticRTInput &Values) override;
1532 
1533  ///
1534  /// \param CGF Reference to current CodeGenFunction.
1535  /// \param Loc Clang source location.
1536  /// \param SchedKind Schedule kind, specified by the 'dist_schedule' clause.
1537  /// \param Values Input arguments for the construct.
1538  ///
1539  void emitDistributeStaticInit(CodeGenFunction &CGF, SourceLocation Loc,
1540  OpenMPDistScheduleClauseKind SchedKind,
1541  const StaticRTInput &Values) override;
1542 
1543  /// \brief Call the appropriate runtime routine to notify that we finished
1544  /// iteration of the ordered loop with the dynamic scheduling.
1545  ///
1546  /// \param CGF Reference to current CodeGenFunction.
1547  /// \param Loc Clang source location.
1548  /// \param IVSize Size of the iteration variable in bits.
1549  /// \param IVSigned Sign of the iteration variable.
1550  ///
1551  void emitForOrderedIterationEnd(CodeGenFunction &CGF, SourceLocation Loc,
1552  unsigned IVSize, bool IVSigned) override;
1553 
1554  /// \brief Call the appropriate runtime routine to notify that we finished
1555  /// all the work with current loop.
1556  ///
1557  /// \param CGF Reference to current CodeGenFunction.
1558  /// \param Loc Clang source location.
1559  /// \param DKind Kind of the directive for which the static finish is emitted.
1560  ///
1561  void emitForStaticFinish(CodeGenFunction &CGF, SourceLocation Loc,
1562  OpenMPDirectiveKind DKind) override;
1563 
1564  /// Call __kmpc_dispatch_next(
1565  /// ident_t *loc, kmp_int32 tid, kmp_int32 *p_lastiter,
1566  /// kmp_int[32|64] *p_lower, kmp_int[32|64] *p_upper,
1567  /// kmp_int[32|64] *p_stride);
1568  /// \param IVSize Size of the iteration variable in bits.
1569  /// \param IVSigned Sign of the iteration variable.
1570  /// \param IL Address of the output variable in which the flag of the
1571  /// last iteration is returned.
1572  /// \param LB Address of the output variable in which the lower iteration
1573  /// number is returned.
1574  /// \param UB Address of the output variable in which the upper iteration
1575  /// number is returned.
1576  /// \param ST Address of the output variable in which the stride value is
1577  /// returned.
1578  llvm::Value *emitForNext(CodeGenFunction &CGF, SourceLocation Loc,
1579  unsigned IVSize, bool IVSigned, Address IL,
1580  Address LB, Address UB, Address ST) override;
1581 
1582  /// \brief Emits call to void __kmpc_push_num_threads(ident_t *loc, kmp_int32
1583  /// global_tid, kmp_int32 num_threads) to generate code for 'num_threads'
1584  /// clause.
1585  /// \param NumThreads An integer value of threads.
1586  void emitNumThreadsClause(CodeGenFunction &CGF, llvm::Value *NumThreads,
1587  SourceLocation Loc) override;
1588 
1589  /// \brief Emit call to void __kmpc_push_proc_bind(ident_t *loc, kmp_int32
1590  /// global_tid, int proc_bind) to generate code for 'proc_bind' clause.
1591  void emitProcBindClause(CodeGenFunction &CGF,
1592  OpenMPProcBindClauseKind ProcBind,
1593  SourceLocation Loc) override;
1594 
1595  /// \brief Returns address of the threadprivate variable for the current
1596  /// thread.
1597  /// \param VD Threadprivate variable.
1598  /// \param VDAddr Address of the global variable \a VD.
1599  /// \param Loc Location of the reference to threadprivate var.
1600  /// \return Address of the threadprivate variable for the current thread.
1601  Address getAddrOfThreadPrivate(CodeGenFunction &CGF, const VarDecl *VD,
1602  Address VDAddr, SourceLocation Loc) override;
1603 
1604  /// \brief Emit a code for initialization of threadprivate variable. It emits
1605  /// a call to runtime library which adds initial value to the newly created
1606  /// threadprivate variable (if it is not constant) and registers destructor
1607  /// for the variable (if any).
1608  /// \param VD Threadprivate variable.
1609  /// \param VDAddr Address of the global variable \a VD.
1610  /// \param Loc Location of threadprivate declaration.
1611  /// \param PerformInit true if initialization expression is not constant.
1612  llvm::Function *
1613  emitThreadPrivateVarDefinition(const VarDecl *VD, Address VDAddr,
1614  SourceLocation Loc, bool PerformInit,
1615  CodeGenFunction *CGF = nullptr) override;
1616 
1617  /// Creates artificial threadprivate variable with name \p Name and type \p
1618  /// VarType.
1619  /// \param VarType Type of the artificial threadprivate variable.
1620  /// \param Name Name of the artificial threadprivate variable.
1621  Address getAddrOfArtificialThreadPrivate(CodeGenFunction &CGF,
1622  QualType VarType,
1623  StringRef Name) override;
1624 
1625  /// \brief Emit flush of the variables specified in 'omp flush' directive.
1626  /// \param Vars List of variables to flush.
1627  void emitFlush(CodeGenFunction &CGF, ArrayRef<const Expr *> Vars,
1628  SourceLocation Loc) override;
1629 
1630  /// \brief Emit task region for the task directive. The task region is
1631  /// emitted in several steps:
1632  /// 1. Emit a call to kmp_task_t *__kmpc_omp_task_alloc(ident_t *, kmp_int32
1633  /// gtid, kmp_int32 flags, size_t sizeof_kmp_task_t, size_t sizeof_shareds,
1634  /// kmp_routine_entry_t *task_entry). Here task_entry is a pointer to the
1635  /// function:
1636  /// kmp_int32 .omp_task_entry.(kmp_int32 gtid, kmp_task_t *tt) {
1637  /// TaskFunction(gtid, tt->part_id, tt->shareds);
1638  /// return 0;
1639  /// }
1640  /// 2. Copy a list of shared variables to field shareds of the resulting
1641  /// structure kmp_task_t returned by the previous call (if any).
1642  /// 3. Copy a pointer to destructions function to field destructions of the
1643  /// resulting structure kmp_task_t.
1644  /// 4. Emit a call to kmp_int32 __kmpc_omp_task(ident_t *, kmp_int32 gtid,
1645  /// kmp_task_t *new_task), where new_task is a resulting structure from
1646  /// previous items.
1647  /// \param D Current task directive.
1648  /// \param TaskFunction An LLVM function with type void (*)(i32 /*gtid*/, i32
1649  /// /*part_id*/, captured_struct */*__context*/);
1650  /// \param SharedsTy A type which contains references the shared variables.
1651  /// \param Shareds Context with the list of shared variables from the \p
1652  /// TaskFunction.
1653  /// \param IfCond Not a nullptr if 'if' clause was specified, nullptr
1654  /// otherwise.
1655  /// \param Data Additional data for task generation like tiednsee, final
1656  /// state, list of privates etc.
1657  void emitTaskCall(CodeGenFunction &CGF, SourceLocation Loc,
1658  const OMPExecutableDirective &D, llvm::Value *TaskFunction,
1659  QualType SharedsTy, Address Shareds, const Expr *IfCond,
1660  const OMPTaskDataTy &Data) override;
1661 
1662  /// Emit task region for the taskloop directive. The taskloop region is
1663  /// emitted in several steps:
1664  /// 1. Emit a call to kmp_task_t *__kmpc_omp_task_alloc(ident_t *, kmp_int32
1665  /// gtid, kmp_int32 flags, size_t sizeof_kmp_task_t, size_t sizeof_shareds,
1666  /// kmp_routine_entry_t *task_entry). Here task_entry is a pointer to the
1667  /// function:
1668  /// kmp_int32 .omp_task_entry.(kmp_int32 gtid, kmp_task_t *tt) {
1669  /// TaskFunction(gtid, tt->part_id, tt->shareds);
1670  /// return 0;
1671  /// }
1672  /// 2. Copy a list of shared variables to field shareds of the resulting
1673  /// structure kmp_task_t returned by the previous call (if any).
1674  /// 3. Copy a pointer to destructions function to field destructions of the
1675  /// resulting structure kmp_task_t.
1676  /// 4. Emit a call to void __kmpc_taskloop(ident_t *loc, int gtid, kmp_task_t
1677  /// *task, int if_val, kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st, int
1678  /// nogroup, int sched, kmp_uint64 grainsize, void *task_dup ), where new_task
1679  /// is a resulting structure from
1680  /// previous items.
1681  /// \param D Current task directive.
1682  /// \param TaskFunction An LLVM function with type void (*)(i32 /*gtid*/, i32
1683  /// /*part_id*/, captured_struct */*__context*/);
1684  /// \param SharedsTy A type which contains references the shared variables.
1685  /// \param Shareds Context with the list of shared variables from the \p
1686  /// TaskFunction.
1687  /// \param IfCond Not a nullptr if 'if' clause was specified, nullptr
1688  /// otherwise.
1689  /// \param Data Additional data for task generation like tiednsee, final
1690  /// state, list of privates etc.
1691  void emitTaskLoopCall(CodeGenFunction &CGF, SourceLocation Loc,
1692  const OMPLoopDirective &D, llvm::Value *TaskFunction,
1693  QualType SharedsTy, Address Shareds, const Expr *IfCond,
1694  const OMPTaskDataTy &Data) override;
1695 
1696  /// \brief Emit a code for reduction clause. Next code should be emitted for
1697  /// reduction:
1698  /// \code
1699  ///
1700  /// static kmp_critical_name lock = { 0 };
1701  ///
1702  /// void reduce_func(void *lhs[<n>], void *rhs[<n>]) {
1703  /// ...
1704  /// *(Type<i>*)lhs[i] = RedOp<i>(*(Type<i>*)lhs[i], *(Type<i>*)rhs[i]);
1705  /// ...
1706  /// }
1707  ///
1708  /// ...
1709  /// void *RedList[<n>] = {&<RHSExprs>[0], ..., &<RHSExprs>[<n>-1]};
1710  /// switch (__kmpc_reduce{_nowait}(<loc>, <gtid>, <n>, sizeof(RedList),
1711  /// RedList, reduce_func, &<lock>)) {
1712  /// case 1:
1713  /// ...
1714  /// <LHSExprs>[i] = RedOp<i>(*<LHSExprs>[i], *<RHSExprs>[i]);
1715  /// ...
1716  /// __kmpc_end_reduce{_nowait}(<loc>, <gtid>, &<lock>);
1717  /// break;
1718  /// case 2:
1719  /// ...
1720  /// Atomic(<LHSExprs>[i] = RedOp<i>(*<LHSExprs>[i], *<RHSExprs>[i]));
1721  /// ...
1722  /// break;
1723  /// default:;
1724  /// }
1725  /// \endcode
1726  ///
1727  /// \param Privates List of private copies for original reduction arguments.
1728  /// \param LHSExprs List of LHS in \a ReductionOps reduction operations.
1729  /// \param RHSExprs List of RHS in \a ReductionOps reduction operations.
1730  /// \param ReductionOps List of reduction operations in form 'LHS binop RHS'
1731  /// or 'operator binop(LHS, RHS)'.
1732  /// \param Options List of options for reduction codegen:
1733  /// WithNowait true if parent directive has also nowait clause, false
1734  /// otherwise.
1735  /// SimpleReduction Emit reduction operation only. Used for omp simd
1736  /// directive on the host.
1737  /// ReductionKind The kind of reduction to perform.
1738  void emitReduction(CodeGenFunction &CGF, SourceLocation Loc,
1740  ArrayRef<const Expr *> LHSExprs,
1741  ArrayRef<const Expr *> RHSExprs,
1742  ArrayRef<const Expr *> ReductionOps,
1743  ReductionOptionsTy Options) override;
1744 
1745  /// Emit a code for initialization of task reduction clause. Next code
1746  /// should be emitted for reduction:
1747  /// \code
1748  ///
1749  /// _task_red_item_t red_data[n];
1750  /// ...
1751  /// red_data[i].shar = &origs[i];
1752  /// red_data[i].size = sizeof(origs[i]);
1753  /// red_data[i].f_init = (void*)RedInit<i>;
1754  /// red_data[i].f_fini = (void*)RedDest<i>;
1755  /// red_data[i].f_comb = (void*)RedOp<i>;
1756  /// red_data[i].flags = <Flag_i>;
1757  /// ...
1758  /// void* tg1 = __kmpc_task_reduction_init(gtid, n, red_data);
1759  /// \endcode
1760  ///
1761  /// \param LHSExprs List of LHS in \a Data.ReductionOps reduction operations.
1762  /// \param RHSExprs List of RHS in \a Data.ReductionOps reduction operations.
1763  /// \param Data Additional data for task generation like tiedness, final
1764  /// state, list of privates, reductions etc.
1765  llvm::Value *emitTaskReductionInit(CodeGenFunction &CGF, SourceLocation Loc,
1766  ArrayRef<const Expr *> LHSExprs,
1767  ArrayRef<const Expr *> RHSExprs,
1768  const OMPTaskDataTy &Data) override;
1769 
1770  /// Required to resolve existing problems in the runtime. Emits threadprivate
1771  /// variables to store the size of the VLAs/array sections for
1772  /// initializer/combiner/finalizer functions + emits threadprivate variable to
1773  /// store the pointer to the original reduction item for the custom
1774  /// initializer defined by declare reduction construct.
1775  /// \param RCG Allows to reuse an existing data for the reductions.
1776  /// \param N Reduction item for which fixups must be emitted.
1777  void emitTaskReductionFixups(CodeGenFunction &CGF, SourceLocation Loc,
1778  ReductionCodeGen &RCG, unsigned N) override;
1779 
1780  /// Get the address of `void *` type of the privatue copy of the reduction
1781  /// item specified by the \p SharedLVal.
1782  /// \param ReductionsPtr Pointer to the reduction data returned by the
1783  /// emitTaskReductionInit function.
1784  /// \param SharedLVal Address of the original reduction item.
1785  Address getTaskReductionItem(CodeGenFunction &CGF, SourceLocation Loc,
1786  llvm::Value *ReductionsPtr,
1787  LValue SharedLVal) override;
1788 
1789  /// \brief Emit code for 'taskwait' directive.
1790  void emitTaskwaitCall(CodeGenFunction &CGF, SourceLocation Loc) override;
1791 
1792  /// \brief Emit code for 'cancellation point' construct.
1793  /// \param CancelRegion Region kind for which the cancellation point must be
1794  /// emitted.
1795  ///
1796  void emitCancellationPointCall(CodeGenFunction &CGF, SourceLocation Loc,
1797  OpenMPDirectiveKind CancelRegion) override;
1798 
1799  /// \brief Emit code for 'cancel' construct.
1800  /// \param IfCond Condition in the associated 'if' clause, if it was
1801  /// specified, nullptr otherwise.
1802  /// \param CancelRegion Region kind for which the cancel must be emitted.
1803  ///
1804  void emitCancelCall(CodeGenFunction &CGF, SourceLocation Loc,
1805  const Expr *IfCond,
1806  OpenMPDirectiveKind CancelRegion) override;
1807 
1808  /// \brief Emit outilined function for 'target' directive.
1809  /// \param D Directive to emit.
1810  /// \param ParentName Name of the function that encloses the target region.
1811  /// \param OutlinedFn Outlined function value to be defined by this call.
1812  /// \param OutlinedFnID Outlined function ID value to be defined by this call.
1813  /// \param IsOffloadEntry True if the outlined function is an offload entry.
1814  /// \param CodeGen Code generation sequence for the \a D directive.
1815  /// An outlined function may not be an entry if, e.g. the if clause always
1816  /// evaluates to false.
1818  StringRef ParentName,
1819  llvm::Function *&OutlinedFn,
1820  llvm::Constant *&OutlinedFnID,
1821  bool IsOffloadEntry,
1822  const RegionCodeGenTy &CodeGen) override;
1823 
1824  /// \brief Emit the target offloading code associated with \a D. The emitted
1825  /// code attempts offloading the execution to the device, an the event of
1826  /// a failure it executes the host version outlined in \a OutlinedFn.
1827  /// \param D Directive to emit.
1828  /// \param OutlinedFn Host version of the code to be offloaded.
1829  /// \param OutlinedFnID ID of host version of the code to be offloaded.
1830  /// \param IfCond Expression evaluated in if clause associated with the target
1831  /// directive, or null if no if clause is used.
1832  /// \param Device Expression evaluated in device clause associated with the
1833  /// target directive, or null if no device clause is used.
1834  void emitTargetCall(CodeGenFunction &CGF, const OMPExecutableDirective &D,
1835  llvm::Value *OutlinedFn, llvm::Value *OutlinedFnID,
1836  const Expr *IfCond, const Expr *Device) override;
1837 
1838  /// \brief Emit the target regions enclosed in \a GD function definition or
1839  /// the function itself in case it is a valid device function. Returns true if
1840  /// \a GD was dealt with successfully.
1841  /// \param GD Function to scan.
1842  bool emitTargetFunctions(GlobalDecl GD) override;
1843 
1844  /// \brief Emit the global variable if it is a valid device global variable.
1845  /// Returns true if \a GD was dealt with successfully.
1846  /// \param GD Variable declaration to emit.
1847  bool emitTargetGlobalVariable(GlobalDecl GD) override;
1848 
1849  /// \brief Emit the global \a GD if it is meaningful for the target. Returns
1850  /// if it was emitted successfully.
1851  /// \param GD Global to scan.
1852  bool emitTargetGlobal(GlobalDecl GD) override;
1853 
1854  /// \brief Creates the offloading descriptor in the event any target region
1855  /// was emitted in the current module and return the function that registers
1856  /// it.
1857  llvm::Function *emitRegistrationFunction() override;
1858 
1859  /// \brief Emits code for teams call of the \a OutlinedFn with
1860  /// variables captured in a record which address is stored in \a
1861  /// CapturedStruct.
1862  /// \param OutlinedFn Outlined function to be run by team masters. Type of
1863  /// this function is void(*)(kmp_int32 *, kmp_int32, struct context_vars*).
1864  /// \param CapturedVars A pointer to the record with the references to
1865  /// variables used in \a OutlinedFn function.
1866  ///
1867  void emitTeamsCall(CodeGenFunction &CGF, const OMPExecutableDirective &D,
1868  SourceLocation Loc, llvm::Value *OutlinedFn,
1869  ArrayRef<llvm::Value *> CapturedVars) override;
1870 
1871  /// \brief Emits call to void __kmpc_push_num_teams(ident_t *loc, kmp_int32
1872  /// global_tid, kmp_int32 num_teams, kmp_int32 thread_limit) to generate code
1873  /// for num_teams clause.
1874  /// \param NumTeams An integer expression of teams.
1875  /// \param ThreadLimit An integer expression of threads.
1876  void emitNumTeamsClause(CodeGenFunction &CGF, const Expr *NumTeams,
1877  const Expr *ThreadLimit, SourceLocation Loc) override;
1878 
1879  /// \brief Emit the target data mapping code associated with \a D.
1880  /// \param D Directive to emit.
1881  /// \param IfCond Expression evaluated in if clause associated with the
1882  /// target directive, or null if no device clause is used.
1883  /// \param Device Expression evaluated in device clause associated with the
1884  /// target directive, or null if no device clause is used.
1885  /// \param Info A record used to store information that needs to be preserved
1886  /// until the region is closed.
1887  void emitTargetDataCalls(CodeGenFunction &CGF,
1888  const OMPExecutableDirective &D, const Expr *IfCond,
1889  const Expr *Device, const RegionCodeGenTy &CodeGen,
1890  TargetDataInfo &Info) override;
1891 
1892  /// \brief Emit the data mapping/movement code associated with the directive
1893  /// \a D that should be of the form 'target [{enter|exit} data | update]'.
1894  /// \param D Directive to emit.
1895  /// \param IfCond Expression evaluated in if clause associated with the target
1896  /// directive, or null if no if clause is used.
1897  /// \param Device Expression evaluated in device clause associated with the
1898  /// target directive, or null if no device clause is used.
1899  void emitTargetDataStandAloneCall(CodeGenFunction &CGF,
1900  const OMPExecutableDirective &D,
1901  const Expr *IfCond,
1902  const Expr *Device) override;
1903 
1904  /// Emit initialization for doacross loop nesting support.
1905  /// \param D Loop-based construct used in doacross nesting construct.
1906  void emitDoacrossInit(CodeGenFunction &CGF,
1907  const OMPLoopDirective &D) override;
1908 
1909  /// Emit code for doacross ordered directive with 'depend' clause.
1910  /// \param C 'depend' clause with 'sink|source' dependency kind.
1911  void emitDoacrossOrdered(CodeGenFunction &CGF,
1912  const OMPDependClause *C) override;
1913 
1914  /// Translates the native parameter of outlined function if this is required
1915  /// for target.
1916  /// \param FD Field decl from captured record for the paramater.
1917  /// \param NativeParam Parameter itself.
1918  const VarDecl *translateParameter(const FieldDecl *FD,
1919  const VarDecl *NativeParam) const override;
1920 
1921  /// Gets the address of the native argument basing on the address of the
1922  /// target-specific parameter.
1923  /// \param NativeParam Parameter itself.
1924  /// \param TargetParam Corresponding target-specific parameter.
1925  Address getParameterAddress(CodeGenFunction &CGF, const VarDecl *NativeParam,
1926  const VarDecl *TargetParam) const override;
1927 };
1928 
1929 } // namespace CodeGen
1930 } // namespace clang
1931 
1932 #endif
const internal::VariadicAllOfMatcher< Type > type
Matches Types in the clang AST.
Represents a function declaration or definition.
Definition: Decl.h:1696
QualType TgtDeviceImageQTy
struct __tgt_device_image{ void *ImageStart; // Pointer to the target code start. ...
Scheduling data for loop-based OpenMP directives.
Definition: OpenMPKinds.h:124
A (possibly-)qualified type.
Definition: Type.h:653
OffloadEntryInfoTargetRegion(unsigned Order, llvm::Constant *Addr, llvm::Constant *ID, int32_t Flags)
void emitSingleReductionCombiner(CodeGenFunction &CGF, const Expr *ReductionOp, const Expr *PrivateRef, const DeclRefExpr *LHS, const DeclRefExpr *RHS)
Emits single reduction combiner.
DominatorTree GraphTraits specialization so the DominatorTree can be iterable by generic graph iterat...
Definition: Dominators.h:26
llvm::SmallPtrSet< const VarDecl *, 4 > ThreadPrivateWithDefinition
Set of threadprivate variables with the generated initializer.
Stmt - This represents one statement.
Definition: Stmt.h:66
virtual void emitUserDefinedReduction(CodeGenFunction *CGF, const OMPDeclareReductionDecl *D)
Emit code for the specified user defined reduction construct.
C Language Family Type Representation.
QualType getTgtBinaryDescriptorQTy()
Returns __tgt_bin_desc type.
SmallVector< std::pair< OpenMPDependClauseKind, const Expr * >, 4 > Dependences
SmallVector< const Expr *, 4 > LastprivateCopies
llvm::Constant * getOrCreateInternalVariable(llvm::Type *Ty, const llvm::Twine &Name)
Gets (if variable with the given name already exist) or creates internal global variable with the spe...
virtual llvm::Value * emitTaskReductionInit(CodeGenFunction &CGF, SourceLocation Loc, ArrayRef< const Expr *> LHSExprs, ArrayRef< const Expr *> RHSExprs, const OMPTaskDataTy &Data)
Emit a code for initialization of task reduction clause.
virtual Address getAddrOfArtificialThreadPrivate(CodeGenFunction &CGF, QualType VarType, StringRef Name)
Creates artificial threadprivate variable with name Name and type VarType.
virtual void clear()
struct with the values to be passed to the dispatch runtime function
void scanForTargetRegionsFunctions(const Stmt *S, StringRef ParentName)
Start scanning from statement S and and emit all target regions found along the way.
llvm::Value * getCriticalRegionLock(StringRef CriticalName)
Returns corresponding lock object for the specified critical region name.
SmallVector< const Expr *, 4 > ReductionCopies
DispatchRTInput(llvm::Value *LB, llvm::Value *UB, llvm::Value *Chunk)
Represents a variable declaration or definition.
Definition: Decl.h:812
virtual void emitOutlinedFunctionCall(CodeGenFunction &CGF, SourceLocation Loc, llvm::Value *OutlinedFn, ArrayRef< llvm::Value *> Args=llvm::None) const
Emits call of the outlined function with the provided arguments, translating these arguments to corre...
bool isValid()
Return true if the current target data information has valid arrays.
llvm::DenseMap< const ValueDecl *, Address > CaptureDeviceAddrMap
Map between the a declaration of a capture and the corresponding base pointer address where the runti...
Class supports emissionof SIMD-only code.
virtual llvm::Value * emitForNext(CodeGenFunction &CGF, SourceLocation Loc, unsigned IVSize, bool IVSigned, Address IL, Address LB, Address UB, Address ST)
Call __kmpc_dispatch_next( ident_t *loc, kmp_int32 tid, kmp_int32 *p_lastiter, kmp_int[32|64] *p_lowe...
virtual void Exit(CodeGenFunction &CGF)
void createOffloadEntriesAndInfoMetadata()
Creates all the offload entries in the current compilation unit along with the associated metadata...
Struct that keeps all the relevant information that should be kept throughout a &#39;target data&#39; region...
QualType getTgtOffloadEntryQTy()
Returns __tgt_offload_entry type.
virtual void emitTargetDataStandAloneCall(CodeGenFunction &CGF, const OMPExecutableDirective &D, const Expr *IfCond, const Expr *Device)
Emit the data mapping/movement code associated with the directive D that should be of the form &#39;targe...
SmallVector< const Expr *, 4 > PrivateVars
Represents a struct/union/class.
Definition: Decl.h:3510
OffloadEntryInfo(OffloadingEntryInfoKinds Kind, unsigned Order, int32_t Flags)
One of these records is kept for each identifier that is lexed.
CodeGenFunction - This class organizes the per-function state that is used while generating LLVM code...
virtual void emitMasterRegion(CodeGenFunction &CGF, const RegionCodeGenTy &MasterOpGen, SourceLocation Loc)
Emits a master region.
virtual void emitForOrderedIterationEnd(CodeGenFunction &CGF, SourceLocation Loc, unsigned IVSize, bool IVSigned)
Call the appropriate runtime routine to notify that we finished iteration of the ordered loop with th...
SmallVector< const Expr *, 4 > LastprivateVars
virtual void emitDoacrossOrdered(CodeGenFunction &CGF, const OMPDependClause *C)
Emit code for doacross ordered directive with &#39;depend&#39; clause.
Represents a member of a struct/union/class.
Definition: Decl.h:2488
Definition: Format.h:2007
std::pair< llvm::Value *, llvm::Value * > getSizes(unsigned N) const
Returns the size of the reduction item (in chars and total number of elements in the item)...
virtual bool isDynamic(OpenMPScheduleClauseKind ScheduleKind) const
Check if the specified ScheduleKind is dynamic.
Defines some OpenMP-specific enums and functions.
llvm::Type * getKmpc_MicroPointerTy()
Returns pointer to kmpc_micro type.
This is a common base class for loop directives (&#39;omp simd&#39;, &#39;omp for&#39;, &#39;omp for simd&#39; etc...
Definition: StmtOpenMP.h:336
static bool classof(const OffloadEntryInfo *Info)
virtual bool emitTargetFunctions(GlobalDecl GD)
Emit the target regions enclosed in GD function definition or the function itself in case it is a val...
OpenMPDistScheduleClauseKind
OpenMP attributes for &#39;dist_schedule&#39; clause.
Definition: OpenMPKinds.h:100
virtual void emitTargetCall(CodeGenFunction &CGF, const OMPExecutableDirective &D, llvm::Value *OutlinedFn, llvm::Value *OutlinedFnID, const Expr *IfCond, const Expr *Device)
Emit the target offloading code associated with D.
QualType TgtOffloadEntryQTy
Type struct __tgt_offload_entry{ void *addr; // Pointer to the offload entry info.
virtual void emitTargetDataCalls(CodeGenFunction &CGF, const OMPExecutableDirective &D, const Expr *IfCond, const Expr *Device, const RegionCodeGenTy &CodeGen, TargetDataInfo &Info)
Emit the target data mapping code associated with D.
i32 captured_struct **param SharedsTy A type which contains references the shared variables *param Shareds Context with the list of shared variables from the p *TaskFunction *param IfCond Not a nullptr if if clause was nullptr *otherwise *param Data Additional data for task generation like final list of privates etc *virtual void emitTaskCall(CodeGenFunction &CGF, SourceLocation Loc, const OMPExecutableDirective &D, llvm::Value *TaskFunction, QualType SharedsTy, Address Shareds, const Expr *IfCond, const OMPTaskDataTy &Data)
virtual std::pair< llvm::Function *, llvm::Function * > getUserDefinedReduction(const OMPDeclareReductionDecl *D)
Get combiner/initializer for the specified user-defined reduction, if any.
SmallVector< const Expr *, 4 > PrivateCopies
virtual void emitDeclareSimdFunction(const FunctionDecl *FD, llvm::Function *Fn)
Marks function Fn with properly mangled versions of vector functions.
virtual void emitTeamsCall(CodeGenFunction &CGF, const OMPExecutableDirective &D, SourceLocation Loc, llvm::Value *OutlinedFn, ArrayRef< llvm::Value *> CapturedVars)
Emits code for teams call of the OutlinedFn with variables captured in a record which address is stor...
SmallVector< const Expr *, 4 > FirstprivateCopies
llvm::Value * emitReductionFunction(CodeGenModule &CGM, SourceLocation Loc, llvm::Type *ArgsType, ArrayRef< const Expr *> Privates, ArrayRef< const Expr *> LHSExprs, ArrayRef< const Expr *> RHSExprs, ArrayRef< const Expr *> ReductionOps)
Emits reduction function.
const VarDecl * getBaseDecl(unsigned N) const
Returns the base declaration of the reduction item.
__INTPTR_TYPE__ intptr_t
A signed integer type with the property that any valid pointer to void can be converted to this type...
Definition: opencl-c.h:75
virtual llvm::Value * emitTeamsOutlinedFunction(const OMPExecutableDirective &D, const VarDecl *ThreadIDVar, OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen)
Emits outlined function for the specified OpenMP teams directive D.
SmallVector< const Expr *, 4 > ReductionOps
SmallVector< const Expr *, 4 > ReductionVars
virtual void emitForStaticFinish(CodeGenFunction &CGF, SourceLocation Loc, OpenMPDirectiveKind DKind)
Call the appropriate runtime routine to notify that we finished all the work with current loop...
virtual Address getParameterAddress(CodeGenFunction &CGF, const VarDecl *NativeParam, const VarDecl *TargetParam) const
Gets the address of the native argument basing on the address of the target-specific parameter...
llvm::Constant * createForStaticInitFunction(unsigned IVSize, bool IVSigned)
Returns __kmpc_for_static_init_* runtime function for the specified size IVSize and sign IVSigned...
TargetDataInfo(bool RequiresDevicePointerInfo)
Class intended to support codegen of all kind of the reduction clauses.
llvm::Constant * createDispatchFiniFunction(unsigned IVSize, bool IVSigned)
Returns __kmpc_dispatch_fini_* runtime function for the specified size IVSize and sign IVSigned...
QualType getTgtDeviceImageQTy()
Returns __tgt_device_image type.
This represents implicit clause &#39;depend&#39; for the &#39;#pragma omp task&#39; directive.
virtual void emitTaskReductionFixups(CodeGenFunction &CGF, SourceLocation Loc, ReductionCodeGen &RCG, unsigned N)
Required to resolve existing problems in the runtime.
virtual void emitFlush(CodeGenFunction &CGF, ArrayRef< const Expr *> Vars, SourceLocation Loc)
Emit flush of the variables specified in &#39;omp flush&#39; directive.
RegionCodeGenTy(Callable &&CodeGen, typename std::enable_if< !std::is_same< typename std::remove_reference< Callable >::type, RegionCodeGenTy >::value >::type *=nullptr)
Expr - This represents one expression.
Definition: Expr.h:106
virtual llvm::Value * emitParallelOutlinedFunction(const OMPExecutableDirective &D, const VarDecl *ThreadIDVar, OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen)
Emits outlined function for the specified OpenMP parallel directive D.
void loadOffloadInfoMetadata()
Loads all the offload entries information from the host IR metadata.
void emitKmpRoutineEntryT(QualType KmpInt32Ty)
Build type kmp_routine_entry_t (if not built yet).
virtual const VarDecl * translateParameter(const FieldDecl *FD, const VarDecl *NativeParam) const
Translates the native parameter of outlined function if this is required for target.
QualType SavedKmpTaskloopTQTy
Saved kmp_task_t for taskloop-based directive.
virtual ~CGOpenMPRuntime()
virtual bool isStaticNonchunked(OpenMPDistScheduleClauseKind ScheduleKind, bool Chunked) const
Check if the specified ScheduleKind is static non-chunked.
void clearArrayInfo()
Clear information about the data arrays.
SmallVector< const Expr *, 4 > FirstprivateVars
virtual Address getAddrOfThreadPrivate(CodeGenFunction &CGF, const VarDecl *VD, Address VDAddr, SourceLocation Loc)
Returns address of the threadprivate variable for the current thread.
CGOpenMPRuntime(CodeGenModule &CGM)
*QualType KmpTaskTQTy
OpenMPProcBindClauseKind
OpenMP attributes for &#39;proc_bind&#39; clause.
Definition: OpenMPKinds.h:51
llvm::Constant * createDispatchInitFunction(unsigned IVSize, bool IVSigned)
Returns __kmpc_dispatch_init_* runtime function for the specified size IVSize and sign IVSigned...
virtual void emitCriticalRegion(CodeGenFunction &CGF, StringRef CriticalName, const RegionCodeGenTy &CriticalOpGen, SourceLocation Loc, const Expr *Hint=nullptr)
Emits a critical region.
GlobalDecl - represents a global declaration.
Definition: GlobalDecl.h:35
virtual bool emitTargetGlobalVariable(GlobalDecl GD)
Emit the global variable if it is a valid device global variable.
QualType KmpDependInfoTy
Type typedef struct kmp_depend_info { kmp_intptr_t base_addr; size_t len; struct { bool in:1; bool ou...
QualType TgtBinaryDescriptorQTy
struct __tgt_bin_desc{ int32_t NumDevices; // Number of devices supported.
The l-value was considered opaque, so the alignment was determined from a type.
Kind
void functionFinished(CodeGenFunction &CGF)
Cleans up references to the objects in finished function.
Encodes a location in the source.
llvm::function_ref< void(unsigned, unsigned, StringRef, unsigned, OffloadEntryInfoTargetRegion &)> OffloadTargetRegionEntryInfoActTy
brief Applies action Action on all registered entries.
This represents &#39;#pragma omp declare reduction ...&#39; directive.
Definition: DeclOpenMP.h:102
llvm::PointerIntPair< llvm::Value *, 1, bool > Final
virtual void emitSingleRegion(CodeGenFunction &CGF, const RegionCodeGenTy &SingleOpGen, SourceLocation Loc, ArrayRef< const Expr *> CopyprivateVars, ArrayRef< const Expr *> DestExprs, ArrayRef< const Expr *> SrcExprs, ArrayRef< const Expr *> AssignmentOps)
Emits a single region.
virtual void emitNumTeamsClause(CodeGenFunction &CGF, const Expr *NumTeams, const Expr *ThreadLimit, SourceLocation Loc)
Emits call to void __kmpc_push_num_teams(ident_t *loc, kmp_int32 global_tid, kmp_int32 num_teams...
This is a basic class for representing single OpenMP executable directive.
Definition: StmtOpenMP.h:33
virtual void emitCancellationPointCall(CodeGenFunction &CGF, SourceLocation Loc, OpenMPDirectiveKind CancelRegion)
Emit code for &#39;cancellation point&#39; construct.
OpenMPDirectiveKind
OpenMP directives.
Definition: OpenMPKinds.h:23
virtual llvm::Function * emitThreadPrivateVarDefinition(const VarDecl *VD, Address VDAddr, SourceLocation Loc, bool PerformInit, CodeGenFunction *CGF=nullptr)
Emit a code for initialization of threadprivate variable.
virtual void emitDoacrossInit(CodeGenFunction &CGF, const OMPLoopDirective &D)
Emit initialization for doacross loop nesting support.
virtual llvm::Value * emitTaskOutlinedFunction(const OMPExecutableDirective &D, const VarDecl *ThreadIDVar, const VarDecl *PartIDVar, const VarDecl *TaskTVar, OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen, bool Tied, unsigned &NumberOfParts)
Emits outlined function for the OpenMP task directive D.
unsigned size() const
Return number of entries defined so far.
virtual void Enter(CodeGenFunction &CGF)
An aligned address.
Definition: Address.h:25
virtual void emitParallelCall(CodeGenFunction &CGF, SourceLocation Loc, llvm::Value *OutlinedFn, ArrayRef< llvm::Value *> CapturedVars, const Expr *IfCond)
Emits code for parallel or serial call of the OutlinedFn with variables captured in a record which ad...
void emitThreadPrivateVarInit(CodeGenFunction &CGF, Address VDAddr, llvm::Value *Ctor, llvm::Value *CopyCtor, llvm::Value *Dtor, SourceLocation Loc)
Emits initialization code for the threadprivate variables.
virtual void emitProcBindClause(CodeGenFunction &CGF, OpenMPProcBindClauseKind ProcBind, SourceLocation Loc)
Emit call to void __kmpc_push_proc_bind(ident_t *loc, kmp_int32 global_tid, int proc_bind) to generat...
virtual void emitTaskyieldCall(CodeGenFunction &CGF, SourceLocation Loc)
Emits code for a taskyield directive.
Struct with the values to be passed to the static runtime function.
i32 captured_struct **param SharedsTy A type which contains references the shared variables *param Shareds Context with the list of shared variables from the p *TaskFunction *param Data Additional data for task generation like final list of privates etc *TaskResultTy emitTaskInit(CodeGenFunction &CGF, SourceLocation Loc, const OMPExecutableDirective &D, llvm::Value *TaskFunction, QualType SharedsTy, Address Shareds, const OMPTaskDataTy &Data)
virtual Address emitThreadIDAddress(CodeGenFunction &CGF, SourceLocation Loc)
Emits address of the word in a memory where current thread id is stored.
An opaque identifier used by SourceManager which refers to a source file (MemoryBuffer) along with it...
void setAction(PrePostActionTy &Action) const
This class organizes the cross-function state that is used while generating LLVM code.
Dataflow Directional Tag Classes.
Class provides a way to call simple version of codegen for OpenMP region, or an advanced with possibl...
virtual bool emitTargetGlobal(GlobalDecl GD)
Emit the global GD if it is meaningful for the target.
virtual void emitCancelCall(CodeGenFunction &CGF, SourceLocation Loc, const Expr *IfCond, OpenMPDirectiveKind CancelRegion)
Emit code for &#39;cancel&#39; construct.
A basic class for pre|post-action for advanced codegen sequence for OpenMP region.
LValue getSharedLValue(unsigned N) const
Returns LValue for the reduction item.
i32 captured_struct **param SharedsTy A type which contains references the shared variables *param Shareds Context with the list of shared variables from the p *TaskFunction *param IfCond Not a nullptr if if clause was nullptr *otherwise *param Data Additional data for task generation like final list of privates etc *virtual void emitTaskLoopCall(CodeGenFunction &CGF, SourceLocation Loc, const OMPLoopDirective &D, llvm::Value *TaskFunction, QualType SharedsTy, Address Shareds, const Expr *IfCond, const OMPTaskDataTy &Data)
llvm::Function * createOffloadingBinaryDescriptorRegistration()
Creates and registers offloading binary descriptor for the current compilation unit.
llvm::PointerIntPair< llvm::Value *, 1, bool > Priority
virtual void emitTargetOutlinedFunction(const OMPExecutableDirective &D, StringRef ParentName, llvm::Function *&OutlinedFn, llvm::Constant *&OutlinedFnID, bool IsOffloadEntry, const RegionCodeGenTy &CodeGen)
Emit outilined function for &#39;target&#39; directive.
OffloadEntriesInfoManagerTy OffloadEntriesInfoManager
CGOpenMPSIMDRuntime(CodeGenModule &CGM)
virtual void emitDistributeStaticInit(CodeGenFunction &CGF, SourceLocation Loc, OpenMPDistScheduleClauseKind SchedKind, const StaticRTInput &Values)
virtual StringRef getOutlinedHelperName() const
Get the function name of an outlined region.
virtual void emitOrderedRegion(CodeGenFunction &CGF, const RegionCodeGenTy &OrderedOpGen, SourceLocation Loc, bool IsThreads)
Emit an ordered region.
StaticRTInput(unsigned IVSize, bool IVSigned, bool Ordered, Address IL, Address LB, Address UB, Address ST, llvm::Value *Chunk=nullptr)
virtual void emitInlinedDirective(CodeGenFunction &CGF, OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen, bool HasCancel=false)
Emit code for the directive that does not require outlining.
OpenMPScheduleClauseKind
OpenMP attributes for &#39;schedule&#39; clause.
Definition: OpenMPKinds.h:59
Entity that registers the offloading constants that were emitted so far.
llvm::Constant * createDispatchNextFunction(unsigned IVSize, bool IVSigned)
Returns __kmpc_dispatch_next_* runtime function for the specified size IVSize and sign IVSigned...
virtual void emitTaskgroupRegion(CodeGenFunction &CGF, const RegionCodeGenTy &TaskgroupOpGen, SourceLocation Loc)
Emit a taskgroup region.
virtual llvm::Function * emitRegistrationFunction()
Creates the offloading descriptor in the event any target region was emitted in the current module an...
virtual void emitReduction(CodeGenFunction &CGF, SourceLocation Loc, ArrayRef< const Expr *> Privates, ArrayRef< const Expr *> LHSExprs, ArrayRef< const Expr *> RHSExprs, ArrayRef< const Expr *> ReductionOps, ReductionOptionsTy Options)
Emit a code for reduction clause.
Defines the clang::SourceLocation class and associated facilities.
llvm::PointerIntPair< llvm::Value *, 1, bool > Schedule
Privates[]
Gets the list of initial values for linear variables.
Definition: OpenMPClause.h:140
virtual void emitBarrierCall(CodeGenFunction &CGF, SourceLocation Loc, OpenMPDirectiveKind Kind, bool EmitChecks=true, bool ForceSimpleCall=false)
Emit an implicit/explicit barrier for OpenMP threads.
virtual void emitForDispatchInit(CodeGenFunction &CGF, SourceLocation Loc, const OpenMPScheduleTy &ScheduleKind, unsigned IVSize, bool IVSigned, bool Ordered, const DispatchRTInput &DispatchValues)
Call the appropriate runtime routine to initialize it before start of loop.
A reference to a declared variable, function, enum, etc.
Definition: Expr.h:956
virtual void emitForStaticInit(CodeGenFunction &CGF, SourceLocation Loc, OpenMPDirectiveKind DKind, const OpenMPScheduleTy &ScheduleKind, const StaticRTInput &Values)
Call the appropriate runtime routine to initialize it before start of loop.
QualType KmpDimTy
struct kmp_dim { // loop bounds info casted to kmp_int64 kmp_int64 lo; // lower kmp_int64 up; // uppe...
LValue - This represents an lvalue references.
Definition: CGValue.h:167
QualType SavedKmpTaskTQTy
Saved kmp_task_t for task directive.
virtual Address getTaskReductionItem(CodeGenFunction &CGF, SourceLocation Loc, llvm::Value *ReductionsPtr, LValue SharedLVal)
Get the address of void * type of the privatue copy of the reduction item specified by the SharedLVal...
llvm::Constant * createRuntimeFunction(unsigned Function)
Returns specified OpenMP runtime function.
virtual void emitNumThreadsClause(CodeGenFunction &CGF, llvm::Value *NumThreads, SourceLocation Loc)
Emits call to void __kmpc_push_num_threads(ident_t *loc, kmp_int32 global_tid, kmp_int32 num_threads)...
SmallVector< const Expr *, 4 > FirstprivateInits
llvm::Constant * getOrCreateThreadPrivateCache(const VarDecl *VD)
If the specified mangled name is not in the module, create and return threadprivate cache object...
virtual void emitTaskwaitCall(CodeGenFunction &CGF, SourceLocation Loc)
Emit code for &#39;taskwait&#39; directive.