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 expression.
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 the base declaration of the reduction item.
195  const Expr *getRefExpr(unsigned N) const { return ClausesData[N].Ref; }
196  /// Returns true if the initialization of the reduction item uses initializer
197  /// from declare reduction construct.
198  bool usesReductionInitializer(unsigned N) const;
199 };
200 
202 public:
203  /// Allows to disable automatic handling of functions used in target regions
204  /// as those marked as `omp declare target`.
206  CodeGenModule &CGM;
207  bool SavedShouldMarkAsGlobal;
208 
209  public:
212  };
213 
214 protected:
216  StringRef FirstSeparator, Separator;
217 
218  /// Constructor allowing to redefine the name separator for the variables.
219  explicit CGOpenMPRuntime(CodeGenModule &CGM, StringRef FirstSeparator,
220  StringRef Separator);
221 
222  /// Creates offloading entry for the provided entry ID \a ID,
223  /// address \a Addr, size \a Size, and flags \a Flags.
224  virtual void createOffloadEntry(llvm::Constant *ID, llvm::Constant *Addr,
225  uint64_t Size, int32_t Flags,
226  llvm::GlobalValue::LinkageTypes Linkage);
227 
228  /// Helper to emit outlined function for 'target' directive.
229  /// \param D Directive to emit.
230  /// \param ParentName Name of the function that encloses the target region.
231  /// \param OutlinedFn Outlined function value to be defined by this call.
232  /// \param OutlinedFnID Outlined function ID value to be defined by this call.
233  /// \param IsOffloadEntry True if the outlined function is an offload entry.
234  /// \param CodeGen Lambda codegen specific to an accelerator device.
235  /// An outlined function may not be an entry if, e.g. the if clause always
236  /// evaluates to false.
237  virtual void emitTargetOutlinedFunctionHelper(const OMPExecutableDirective &D,
238  StringRef ParentName,
239  llvm::Function *&OutlinedFn,
240  llvm::Constant *&OutlinedFnID,
241  bool IsOffloadEntry,
242  const RegionCodeGenTy &CodeGen);
243 
244  /// Emits code for OpenMP 'if' clause using specified \a CodeGen
245  /// function. Here is the logic:
246  /// if (Cond) {
247  /// ThenGen();
248  /// } else {
249  /// ElseGen();
250  /// }
251  void emitOMPIfClause(CodeGenFunction &CGF, const Expr *Cond,
252  const RegionCodeGenTy &ThenGen,
253  const RegionCodeGenTy &ElseGen);
254 
255  /// Emits object of ident_t type with info for source location.
256  /// \param Flags Flags for OpenMP location.
257  ///
258  llvm::Value *emitUpdateLocation(CodeGenFunction &CGF, SourceLocation Loc,
259  unsigned Flags = 0);
260 
261  /// Returns pointer to ident_t type.
262  llvm::Type *getIdentTyPointerTy();
263 
264  /// Gets thread id value for the current thread.
265  ///
266  llvm::Value *getThreadID(CodeGenFunction &CGF, SourceLocation Loc);
267 
268  /// Get the function name of an outlined region.
269  // The name can be customized depending on the target.
270  //
271  virtual StringRef getOutlinedHelperName() const { return ".omp_outlined."; }
272 
273  /// Emits \p Callee function call with arguments \p Args with location \p Loc.
274  void emitCall(CodeGenFunction &CGF, SourceLocation Loc, llvm::Value *Callee,
275  ArrayRef<llvm::Value *> Args = llvm::None) const;
276 
277  /// Emits address of the word in a memory where current thread id is
278  /// stored.
279  virtual Address emitThreadIDAddress(CodeGenFunction &CGF, SourceLocation Loc);
280 
281 private:
282  /// Default const ident_t object used for initialization of all other
283  /// ident_t objects.
284  llvm::Constant *DefaultOpenMPPSource = nullptr;
285  /// Map of flags and corresponding default locations.
286  typedef llvm::DenseMap<unsigned, llvm::Value *> OpenMPDefaultLocMapTy;
287  OpenMPDefaultLocMapTy OpenMPDefaultLocMap;
288  Address getOrCreateDefaultLocation(unsigned Flags);
289 
290  QualType IdentQTy;
291  llvm::StructType *IdentTy = nullptr;
292  /// Map for SourceLocation and OpenMP runtime library debug locations.
293  typedef llvm::DenseMap<unsigned, llvm::Value *> OpenMPDebugLocMapTy;
294  OpenMPDebugLocMapTy OpenMPDebugLocMap;
295  /// The type for a microtask which gets passed to __kmpc_fork_call().
296  /// Original representation is:
297  /// typedef void (kmpc_micro)(kmp_int32 global_tid, kmp_int32 bound_tid,...);
298  llvm::FunctionType *Kmpc_MicroTy = nullptr;
299  /// Stores debug location and ThreadID for the function.
300  struct DebugLocThreadIdTy {
301  llvm::Value *DebugLoc;
302  llvm::Value *ThreadID;
303  };
304  /// Map of local debug location, ThreadId and functions.
305  typedef llvm::DenseMap<llvm::Function *, DebugLocThreadIdTy>
306  OpenMPLocThreadIDMapTy;
307  OpenMPLocThreadIDMapTy OpenMPLocThreadIDMap;
308  /// Map of UDRs and corresponding combiner/initializer.
309  typedef llvm::DenseMap<const OMPDeclareReductionDecl *,
310  std::pair<llvm::Function *, llvm::Function *>>
311  UDRMapTy;
312  UDRMapTy UDRMap;
313  /// Map of functions and locally defined UDRs.
314  typedef llvm::DenseMap<llvm::Function *,
316  FunctionUDRMapTy;
317  FunctionUDRMapTy FunctionUDRMap;
318  IdentifierInfo *In = nullptr;
319  IdentifierInfo *Out = nullptr;
320  IdentifierInfo *Priv = nullptr;
321  IdentifierInfo *Orig = nullptr;
322  /// Type kmp_critical_name, originally defined as typedef kmp_int32
323  /// kmp_critical_name[8];
324  llvm::ArrayType *KmpCriticalNameTy;
325  /// An ordered map of auto-generated variables to their unique names.
326  /// It stores variables with the following names: 1) ".gomp_critical_user_" +
327  /// <critical_section_name> + ".var" for "omp critical" directives; 2)
328  /// <mangled_name_for_global_var> + ".cache." for cache for threadprivate
329  /// variables.
330  llvm::StringMap<llvm::AssertingVH<llvm::Constant>, llvm::BumpPtrAllocator>
331  InternalVars;
332  /// Type typedef kmp_int32 (* kmp_routine_entry_t)(kmp_int32, void *);
333  llvm::Type *KmpRoutineEntryPtrTy = nullptr;
334  QualType KmpRoutineEntryPtrQTy;
335  /// Type typedef struct kmp_task {
336  /// void * shareds; /**< pointer to block of pointers to
337  /// shared vars */
338  /// kmp_routine_entry_t routine; /**< pointer to routine to call for
339  /// executing task */
340  /// kmp_int32 part_id; /**< part id for the task */
341  /// kmp_routine_entry_t destructors; /* pointer to function to invoke
342  /// deconstructors of firstprivate C++ objects */
343  /// } kmp_task_t;
345  /// Saved kmp_task_t for task directive.
347  /// Saved kmp_task_t for taskloop-based directive.
349  /// Type typedef struct kmp_depend_info {
350  /// kmp_intptr_t base_addr;
351  /// size_t len;
352  /// struct {
353  /// bool in:1;
354  /// bool out:1;
355  /// } flags;
356  /// } kmp_depend_info_t;
358  /// struct kmp_dim { // loop bounds info casted to kmp_int64
359  /// kmp_int64 lo; // lower
360  /// kmp_int64 up; // upper
361  /// kmp_int64 st; // stride
362  /// };
364  /// Type struct __tgt_offload_entry{
365  /// void *addr; // Pointer to the offload entry info.
366  /// // (function or global)
367  /// char *name; // Name of the function or global.
368  /// size_t size; // Size of the entry info (0 if it a function).
369  /// };
371  /// struct __tgt_device_image{
372  /// void *ImageStart; // Pointer to the target code start.
373  /// void *ImageEnd; // Pointer to the target code end.
374  /// // We also add the host entries to the device image, as it may be useful
375  /// // for the target runtime to have access to that information.
376  /// __tgt_offload_entry *EntriesBegin; // Begin of the table with all
377  /// // the entries.
378  /// __tgt_offload_entry *EntriesEnd; // End of the table with all the
379  /// // entries (non inclusive).
380  /// };
382  /// struct __tgt_bin_desc{
383  /// int32_t NumDevices; // Number of devices supported.
384  /// __tgt_device_image *DeviceImages; // Arrays of device images
385  /// // (one per device).
386  /// __tgt_offload_entry *EntriesBegin; // Begin of the table with all the
387  /// // entries.
388  /// __tgt_offload_entry *EntriesEnd; // End of the table with all the
389  /// // entries (non inclusive).
390  /// };
392  /// Entity that registers the offloading constants that were emitted so
393  /// far.
395  CodeGenModule &CGM;
396 
397  /// Number of entries registered so far.
398  unsigned OffloadingEntriesNum = 0;
399 
400  public:
401  /// Base class of the entries info.
403  public:
404  /// Kind of a given entry.
405  enum OffloadingEntryInfoKinds : unsigned {
406  /// Entry is a target region.
407  OffloadingEntryInfoTargetRegion = 0,
408  /// Entry is a declare target variable.
409  OffloadingEntryInfoDeviceGlobalVar = 1,
410  /// Invalid entry info.
411  OffloadingEntryInfoInvalid = ~0u
412  };
413 
414  protected:
415  OffloadEntryInfo() = delete;
418  uint32_t Flags)
419  : Flags(Flags), Order(Order), Kind(Kind) {}
420  ~OffloadEntryInfo() = default;
421 
422  public:
423  bool isValid() const { return Order != ~0u; }
424  unsigned getOrder() const { return Order; }
426  uint32_t getFlags() const { return Flags; }
427  void setFlags(uint32_t NewFlags) { Flags = NewFlags; }
428  llvm::Constant *getAddress() const {
429  return cast_or_null<llvm::Constant>(Addr);
430  }
431  void setAddress(llvm::Constant *V) {
432  assert(!Addr.pointsToAliveValue() && "Address has been set before!");
433  Addr = V;
434  }
435  static bool classof(const OffloadEntryInfo *Info) { return true; }
436 
437  private:
438  /// Address of the entity that has to be mapped for offloading.
439  llvm::WeakTrackingVH Addr;
440 
441  /// Flags associated with the device global.
442  uint32_t Flags = 0u;
443 
444  /// Order this entry was emitted.
445  unsigned Order = ~0u;
446 
447  OffloadingEntryInfoKinds Kind = OffloadingEntryInfoInvalid;
448  };
449 
450  /// Return true if a there are no entries defined.
451  bool empty() const;
452  /// Return number of entries defined so far.
453  unsigned size() const { return OffloadingEntriesNum; }
455 
456  //
457  // Target region entries related.
458  //
459 
460  /// Kind of the target registry entry.
461  enum OMPTargetRegionEntryKind : uint32_t {
462  /// Mark the entry as target region.
463  OMPTargetRegionEntryTargetRegion = 0x0,
464  /// Mark the entry as a global constructor.
465  OMPTargetRegionEntryCtor = 0x02,
466  /// Mark the entry as a global destructor.
467  OMPTargetRegionEntryDtor = 0x04,
468  };
469 
470  /// Target region entries info.
472  /// Address that can be used as the ID of the entry.
473  llvm::Constant *ID = nullptr;
474 
475  public:
477  : OffloadEntryInfo(OffloadingEntryInfoTargetRegion) {}
478  explicit OffloadEntryInfoTargetRegion(unsigned Order,
479  llvm::Constant *Addr,
480  llvm::Constant *ID,
482  : OffloadEntryInfo(OffloadingEntryInfoTargetRegion, Order, Flags),
483  ID(ID) {
484  setAddress(Addr);
485  }
486 
487  llvm::Constant *getID() const { return ID; }
488  void setID(llvm::Constant *V) {
489  assert(!ID && "ID has been set before!");
490  ID = V;
491  }
492  static bool classof(const OffloadEntryInfo *Info) {
493  return Info->getKind() == OffloadingEntryInfoTargetRegion;
494  }
495  };
496 
497  /// Initialize target region entry.
498  void initializeTargetRegionEntryInfo(unsigned DeviceID, unsigned FileID,
499  StringRef ParentName, unsigned LineNum,
500  unsigned Order);
501  /// Register target region entry.
502  void registerTargetRegionEntryInfo(unsigned DeviceID, unsigned FileID,
503  StringRef ParentName, unsigned LineNum,
504  llvm::Constant *Addr, llvm::Constant *ID,
506  /// Return true if a target region entry with the provided information
507  /// exists.
508  bool hasTargetRegionEntryInfo(unsigned DeviceID, unsigned FileID,
509  StringRef ParentName, unsigned LineNum) const;
510  /// brief Applies action \a Action on all registered entries.
511  typedef llvm::function_ref<void(unsigned, unsigned, StringRef, unsigned,
514  void actOnTargetRegionEntriesInfo(
515  const OffloadTargetRegionEntryInfoActTy &Action);
516 
517  //
518  // Device global variable entries related.
519  //
520 
521  /// Kind of the global variable entry..
522  enum OMPTargetGlobalVarEntryKind : uint32_t {
523  /// Mark the entry as a to declare target.
524  OMPTargetGlobalVarEntryTo = 0x0,
525  };
526 
527  /// Device global variable entries info.
529  /// Type of the global variable.
530  CharUnits VarSize;
531  llvm::GlobalValue::LinkageTypes Linkage;
532 
533  public:
535  : OffloadEntryInfo(OffloadingEntryInfoDeviceGlobalVar) {}
536  explicit OffloadEntryInfoDeviceGlobalVar(unsigned Order,
538  : OffloadEntryInfo(OffloadingEntryInfoDeviceGlobalVar, Order, Flags) {}
540  unsigned Order, llvm::Constant *Addr, CharUnits VarSize,
542  llvm::GlobalValue::LinkageTypes Linkage)
543  : OffloadEntryInfo(OffloadingEntryInfoDeviceGlobalVar, Order, Flags),
544  VarSize(VarSize), Linkage(Linkage) {
545  setAddress(Addr);
546  }
547 
548  CharUnits getVarSize() const { return VarSize; }
549  void setVarSize(CharUnits Size) { VarSize = Size; }
550  llvm::GlobalValue::LinkageTypes getLinkage() const { return Linkage; }
551  void setLinkage(llvm::GlobalValue::LinkageTypes LT) { Linkage = LT; }
552  static bool classof(const OffloadEntryInfo *Info) {
553  return Info->getKind() == OffloadingEntryInfoDeviceGlobalVar;
554  }
555  };
556 
557  /// Initialize device global variable entry.
558  void initializeDeviceGlobalVarEntryInfo(StringRef Name,
560  unsigned Order);
561 
562  /// Register device global variable entry.
563  void
564  registerDeviceGlobalVarEntryInfo(StringRef VarName, llvm::Constant *Addr,
565  CharUnits VarSize,
567  llvm::GlobalValue::LinkageTypes Linkage);
568  /// Checks if the variable with the given name has been registered already.
569  bool hasDeviceGlobalVarEntryInfo(StringRef VarName) const {
570  return OffloadEntriesDeviceGlobalVar.count(VarName) > 0;
571  }
572  /// Applies action \a Action on all registered entries.
573  typedef llvm::function_ref<void(StringRef,
576  void actOnDeviceGlobalVarEntriesInfo(
578 
579  private:
580  // Storage for target region entries kind. The storage is to be indexed by
581  // file ID, device ID, parent function name and line number.
582  typedef llvm::DenseMap<unsigned, OffloadEntryInfoTargetRegion>
583  OffloadEntriesTargetRegionPerLine;
584  typedef llvm::StringMap<OffloadEntriesTargetRegionPerLine>
585  OffloadEntriesTargetRegionPerParentName;
586  typedef llvm::DenseMap<unsigned, OffloadEntriesTargetRegionPerParentName>
587  OffloadEntriesTargetRegionPerFile;
588  typedef llvm::DenseMap<unsigned, OffloadEntriesTargetRegionPerFile>
589  OffloadEntriesTargetRegionPerDevice;
590  typedef OffloadEntriesTargetRegionPerDevice OffloadEntriesTargetRegionTy;
591  OffloadEntriesTargetRegionTy OffloadEntriesTargetRegion;
592  /// Storage for device global variable entries kind. The storage is to be
593  /// indexed by mangled name.
594  typedef llvm::StringMap<OffloadEntryInfoDeviceGlobalVar>
595  OffloadEntriesDeviceGlobalVarTy;
596  OffloadEntriesDeviceGlobalVarTy OffloadEntriesDeviceGlobalVar;
597  };
599 
600  bool ShouldMarkAsGlobal = true;
601  llvm::SmallDenseSet<const FunctionDecl *> AlreadyEmittedTargetFunctions;
602 
603  /// Creates and registers offloading binary descriptor for the current
604  /// compilation unit. The function that does the registration is returned.
606 
607  /// Creates all the offload entries in the current compilation unit
608  /// along with the associated metadata.
610 
611  /// Loads all the offload entries information from the host IR
612  /// metadata.
614 
615  /// Returns __tgt_offload_entry type.
617 
618  /// Returns __tgt_device_image type.
620 
621  /// Returns __tgt_bin_desc type.
623 
624  /// Start scanning from statement \a S and and emit all target regions
625  /// found along the way.
626  /// \param S Starting statement.
627  /// \param ParentName Name of the function declaration that is being scanned.
628  void scanForTargetRegionsFunctions(const Stmt *S, StringRef ParentName);
629 
630  /// Build type kmp_routine_entry_t (if not built yet).
631  void emitKmpRoutineEntryT(QualType KmpInt32Ty);
632 
633  /// Returns pointer to kmpc_micro type.
634  llvm::Type *getKmpc_MicroPointerTy();
635 
636  /// Returns specified OpenMP runtime function.
637  /// \param Function OpenMP runtime function.
638  /// \return Specified function.
639  llvm::Constant *createRuntimeFunction(unsigned Function);
640 
641  /// Returns __kmpc_for_static_init_* runtime function for the specified
642  /// size \a IVSize and sign \a IVSigned.
643  llvm::Constant *createForStaticInitFunction(unsigned IVSize, bool IVSigned);
644 
645  /// Returns __kmpc_dispatch_init_* runtime function for the specified
646  /// size \a IVSize and sign \a IVSigned.
647  llvm::Constant *createDispatchInitFunction(unsigned IVSize, bool IVSigned);
648 
649  /// Returns __kmpc_dispatch_next_* runtime function for the specified
650  /// size \a IVSize and sign \a IVSigned.
651  llvm::Constant *createDispatchNextFunction(unsigned IVSize, bool IVSigned);
652 
653  /// Returns __kmpc_dispatch_fini_* runtime function for the specified
654  /// size \a IVSize and sign \a IVSigned.
655  llvm::Constant *createDispatchFiniFunction(unsigned IVSize, bool IVSigned);
656 
657  /// If the specified mangled name is not in the module, create and
658  /// return threadprivate cache object. This object is a pointer's worth of
659  /// storage that's reserved for use by the OpenMP runtime.
660  /// \param VD Threadprivate variable.
661  /// \return Cache variable for the specified threadprivate.
662  llvm::Constant *getOrCreateThreadPrivateCache(const VarDecl *VD);
663 
664  /// Gets (if variable with the given name already exist) or creates
665  /// internal global variable with the specified Name. The created variable has
666  /// linkage CommonLinkage by default and is initialized by null value.
667  /// \param Ty Type of the global variable. If it is exist already the type
668  /// must be the same.
669  /// \param Name Name of the variable.
670  llvm::Constant *getOrCreateInternalVariable(llvm::Type *Ty,
671  const llvm::Twine &Name);
672 
673  /// Set of threadprivate variables with the generated initializer.
674  llvm::SmallPtrSet<const VarDecl *, 4> ThreadPrivateWithDefinition;
675 
676  /// Set of declare target variables with the generated initializer.
677  llvm::SmallPtrSet<const VarDecl *, 4> DeclareTargetWithDefinition;
678 
679  /// Emits initialization code for the threadprivate variables.
680  /// \param VDAddr Address of the global variable \a VD.
681  /// \param Ctor Pointer to a global init function for \a VD.
682  /// \param CopyCtor Pointer to a global copy function for \a VD.
683  /// \param Dtor Pointer to a global destructor function for \a VD.
684  /// \param Loc Location of threadprivate declaration.
686  llvm::Value *Ctor, llvm::Value *CopyCtor,
687  llvm::Value *Dtor, SourceLocation Loc);
688 
689  /// Returns corresponding lock object for the specified critical region
690  /// name. If the lock object does not exist it is created, otherwise the
691  /// reference to the existing copy is returned.
692  /// \param CriticalName Name of the critical region.
693  ///
694  llvm::Value *getCriticalRegionLock(StringRef CriticalName);
695 
696  struct TaskResultTy {
697  llvm::Value *NewTask = nullptr;
698  llvm::Value *TaskEntry = nullptr;
699  llvm::Value *NewTaskNewTaskTTy = nullptr;
701  const RecordDecl *KmpTaskTQTyRD = nullptr;
702  llvm::Value *TaskDupFn = nullptr;
703  };
704  /// Emit task region for the task directive. The task region is emitted in
705  /// several steps:
706  /// 1. Emit a call to kmp_task_t *__kmpc_omp_task_alloc(ident_t *, kmp_int32
707  /// gtid, kmp_int32 flags, size_t sizeof_kmp_task_t, size_t sizeof_shareds,
708  /// kmp_routine_entry_t *task_entry). Here task_entry is a pointer to the
709  /// function:
710  /// kmp_int32 .omp_task_entry.(kmp_int32 gtid, kmp_task_t *tt) {
711  /// TaskFunction(gtid, tt->part_id, tt->shareds);
712  /// return 0;
713  /// }
714  /// 2. Copy a list of shared variables to field shareds of the resulting
715  /// structure kmp_task_t returned by the previous call (if any).
716  /// 3. Copy a pointer to destructions function to field destructions of the
717  /// resulting structure kmp_task_t.
718  /// \param D Current task directive.
719  /// \param TaskFunction An LLVM function with type void (*)(i32 /*gtid*/, i32
720  /// /*part_id*/, captured_struct */*__context*/);
721  /// \param SharedsTy A type which contains references the shared variables.
722  /// \param Shareds Context with the list of shared variables from the \p
723  /// TaskFunction.
724  /// \param Data Additional data for task generation like tiednsee, final
725  /// state, list of privates etc.
727  const OMPExecutableDirective &D,
728  llvm::Value *TaskFunction, QualType SharedsTy,
729  Address Shareds, const OMPTaskDataTy &Data);
730 
731 public:
733  : CGOpenMPRuntime(CGM, ".", ".") {}
734  virtual ~CGOpenMPRuntime() {}
735  virtual void clear();
736 
737  /// Get the platform-specific name separator.
738  std::string getName(ArrayRef<StringRef> Parts) const;
739 
740  /// Emit code for the specified user defined reduction construct.
741  virtual void emitUserDefinedReduction(CodeGenFunction *CGF,
742  const OMPDeclareReductionDecl *D);
743  /// Get combiner/initializer for the specified user-defined reduction, if any.
744  virtual std::pair<llvm::Function *, llvm::Function *>
745  getUserDefinedReduction(const OMPDeclareReductionDecl *D);
746 
747  /// Emits outlined function for the specified OpenMP parallel directive
748  /// \a D. This outlined function has type void(*)(kmp_int32 *ThreadID,
749  /// kmp_int32 BoundID, struct context_vars*).
750  /// \param D OpenMP directive.
751  /// \param ThreadIDVar Variable for thread id in the current OpenMP region.
752  /// \param InnermostKind Kind of innermost directive (for simple directives it
753  /// is a directive itself, for combined - its innermost directive).
754  /// \param CodeGen Code generation sequence for the \a D directive.
756  const OMPExecutableDirective &D, const VarDecl *ThreadIDVar,
757  OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen);
758 
759  /// Emits outlined function for the specified OpenMP teams directive
760  /// \a D. This outlined function has type void(*)(kmp_int32 *ThreadID,
761  /// kmp_int32 BoundID, struct context_vars*).
762  /// \param D OpenMP directive.
763  /// \param ThreadIDVar Variable for thread id in the current OpenMP region.
764  /// \param InnermostKind Kind of innermost directive (for simple directives it
765  /// is a directive itself, for combined - its innermost directive).
766  /// \param CodeGen Code generation sequence for the \a D directive.
768  const OMPExecutableDirective &D, const VarDecl *ThreadIDVar,
769  OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen);
770 
771  /// Emits outlined function for the OpenMP task directive \a D. This
772  /// outlined function has type void(*)(kmp_int32 ThreadID, struct task_t*
773  /// TaskT).
774  /// \param D OpenMP directive.
775  /// \param ThreadIDVar Variable for thread id in the current OpenMP region.
776  /// \param PartIDVar Variable for partition id in the current OpenMP untied
777  /// task region.
778  /// \param TaskTVar Variable for task_t argument.
779  /// \param InnermostKind Kind of innermost directive (for simple directives it
780  /// is a directive itself, for combined - its innermost directive).
781  /// \param CodeGen Code generation sequence for the \a D directive.
782  /// \param Tied true if task is generated for tied task, false otherwise.
783  /// \param NumberOfParts Number of parts in untied task. Ignored for tied
784  /// tasks.
785  ///
787  const OMPExecutableDirective &D, const VarDecl *ThreadIDVar,
788  const VarDecl *PartIDVar, const VarDecl *TaskTVar,
789  OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen,
790  bool Tied, unsigned &NumberOfParts);
791 
792  /// Cleans up references to the objects in finished function.
793  ///
794  virtual void functionFinished(CodeGenFunction &CGF);
795 
796  /// Emits code for parallel or serial call of the \a OutlinedFn with
797  /// variables captured in a record which address is stored in \a
798  /// CapturedStruct.
799  /// \param OutlinedFn Outlined function to be run in parallel threads. Type of
800  /// this function is void(*)(kmp_int32 *, kmp_int32, struct context_vars*).
801  /// \param CapturedVars A pointer to the record with the references to
802  /// variables used in \a OutlinedFn function.
803  /// \param IfCond Condition in the associated 'if' clause, if it was
804  /// specified, nullptr otherwise.
805  ///
806  virtual void emitParallelCall(CodeGenFunction &CGF, SourceLocation Loc,
807  llvm::Value *OutlinedFn,
808  ArrayRef<llvm::Value *> CapturedVars,
809  const Expr *IfCond);
810 
811  /// Emits a critical region.
812  /// \param CriticalName Name of the critical region.
813  /// \param CriticalOpGen Generator for the statement associated with the given
814  /// critical region.
815  /// \param Hint Value of the 'hint' clause (optional).
816  virtual void emitCriticalRegion(CodeGenFunction &CGF, StringRef CriticalName,
817  const RegionCodeGenTy &CriticalOpGen,
818  SourceLocation Loc,
819  const Expr *Hint = nullptr);
820 
821  /// Emits a master region.
822  /// \param MasterOpGen Generator for the statement associated with the given
823  /// master region.
824  virtual void emitMasterRegion(CodeGenFunction &CGF,
825  const RegionCodeGenTy &MasterOpGen,
826  SourceLocation Loc);
827 
828  /// Emits code for a taskyield directive.
829  virtual void emitTaskyieldCall(CodeGenFunction &CGF, SourceLocation Loc);
830 
831  /// Emit a taskgroup region.
832  /// \param TaskgroupOpGen Generator for the statement associated with the
833  /// given taskgroup region.
834  virtual void emitTaskgroupRegion(CodeGenFunction &CGF,
835  const RegionCodeGenTy &TaskgroupOpGen,
836  SourceLocation Loc);
837 
838  /// Emits a single region.
839  /// \param SingleOpGen Generator for the statement associated with the given
840  /// single region.
841  virtual void emitSingleRegion(CodeGenFunction &CGF,
842  const RegionCodeGenTy &SingleOpGen,
843  SourceLocation Loc,
844  ArrayRef<const Expr *> CopyprivateVars,
845  ArrayRef<const Expr *> DestExprs,
846  ArrayRef<const Expr *> SrcExprs,
847  ArrayRef<const Expr *> AssignmentOps);
848 
849  /// Emit an ordered region.
850  /// \param OrderedOpGen Generator for the statement associated with the given
851  /// ordered region.
852  virtual void emitOrderedRegion(CodeGenFunction &CGF,
853  const RegionCodeGenTy &OrderedOpGen,
854  SourceLocation Loc, bool IsThreads);
855 
856  /// Emit an implicit/explicit barrier for OpenMP threads.
857  /// \param Kind Directive for which this implicit barrier call must be
858  /// generated. Must be OMPD_barrier for explicit barrier generation.
859  /// \param EmitChecks true if need to emit checks for cancellation barriers.
860  /// \param ForceSimpleCall true simple barrier call must be emitted, false if
861  /// runtime class decides which one to emit (simple or with cancellation
862  /// checks).
863  ///
864  virtual void emitBarrierCall(CodeGenFunction &CGF, SourceLocation Loc,
866  bool EmitChecks = true,
867  bool ForceSimpleCall = false);
868 
869  /// Check if the specified \a ScheduleKind is static non-chunked.
870  /// This kind of worksharing directive is emitted without outer loop.
871  /// \param ScheduleKind Schedule kind specified in the 'schedule' clause.
872  /// \param Chunked True if chunk is specified in the clause.
873  ///
874  virtual bool isStaticNonchunked(OpenMPScheduleClauseKind ScheduleKind,
875  bool Chunked) const;
876 
877  /// Check if the specified \a ScheduleKind is static non-chunked.
878  /// This kind of distribute directive is emitted without outer loop.
879  /// \param ScheduleKind Schedule kind specified in the 'dist_schedule' clause.
880  /// \param Chunked True if chunk is specified in the clause.
881  ///
882  virtual bool isStaticNonchunked(OpenMPDistScheduleClauseKind ScheduleKind,
883  bool Chunked) const;
884 
885  /// Check if the specified \a ScheduleKind is dynamic.
886  /// This kind of worksharing directive is emitted without outer loop.
887  /// \param ScheduleKind Schedule Kind specified in the 'schedule' clause.
888  ///
889  virtual bool isDynamic(OpenMPScheduleClauseKind ScheduleKind) const;
890 
891  /// struct with the values to be passed to the dispatch runtime function
893  /// Loop lower bound
894  llvm::Value *LB = nullptr;
895  /// Loop upper bound
896  llvm::Value *UB = nullptr;
897  /// Chunk size specified using 'schedule' clause (nullptr if chunk
898  /// was not specified)
899  llvm::Value *Chunk = nullptr;
900  DispatchRTInput() = default;
902  : LB(LB), UB(UB), Chunk(Chunk) {}
903  };
904 
905  /// Call the appropriate runtime routine to initialize it before start
906  /// of loop.
907 
908  /// This is used for non static scheduled types and when the ordered
909  /// clause is present on the loop construct.
910  /// Depending on the loop schedule, it is necessary to call some runtime
911  /// routine before start of the OpenMP loop to get the loop upper / lower
912  /// bounds \a LB and \a UB and stride \a ST.
913  ///
914  /// \param CGF Reference to current CodeGenFunction.
915  /// \param Loc Clang source location.
916  /// \param ScheduleKind Schedule kind, specified by the 'schedule' clause.
917  /// \param IVSize Size of the iteration variable in bits.
918  /// \param IVSigned Sign of the iteration variable.
919  /// \param Ordered true if loop is ordered, false otherwise.
920  /// \param DispatchValues struct containing llvm values for lower bound, upper
921  /// bound, and chunk expression.
922  /// For the default (nullptr) value, the chunk 1 will be used.
923  ///
924  virtual void emitForDispatchInit(CodeGenFunction &CGF, SourceLocation Loc,
925  const OpenMPScheduleTy &ScheduleKind,
926  unsigned IVSize, bool IVSigned, bool Ordered,
927  const DispatchRTInput &DispatchValues);
928 
929  /// Struct with the values to be passed to the static runtime function
930  struct StaticRTInput {
931  /// Size of the iteration variable in bits.
932  unsigned IVSize = 0;
933  /// Sign of the iteration variable.
934  bool IVSigned = false;
935  /// true if loop is ordered, false otherwise.
936  bool Ordered = false;
937  /// Address of the output variable in which the flag of the last iteration
938  /// is returned.
939  Address IL = Address::invalid();
940  /// Address of the output variable in which the lower iteration number is
941  /// returned.
942  Address LB = Address::invalid();
943  /// Address of the output variable in which the upper iteration number is
944  /// returned.
945  Address UB = Address::invalid();
946  /// Address of the output variable in which the stride value is returned
947  /// necessary to generated the static_chunked scheduled loop.
948  Address ST = Address::invalid();
949  /// Value of the chunk for the static_chunked scheduled loop. For the
950  /// default (nullptr) value, the chunk 1 will be used.
951  llvm::Value *Chunk = nullptr;
952  StaticRTInput(unsigned IVSize, bool IVSigned, bool Ordered, Address IL,
953  Address LB, Address UB, Address ST,
954  llvm::Value *Chunk = nullptr)
955  : IVSize(IVSize), IVSigned(IVSigned), Ordered(Ordered), IL(IL), LB(LB),
956  UB(UB), ST(ST), Chunk(Chunk) {}
957  };
958  /// Call the appropriate runtime routine to initialize it before start
959  /// of loop.
960  ///
961  /// This is used only in case of static schedule, when the user did not
962  /// specify a ordered clause on the loop construct.
963  /// Depending on the loop schedule, it is necessary to call some runtime
964  /// routine before start of the OpenMP loop to get the loop upper / lower
965  /// bounds LB and UB and stride ST.
966  ///
967  /// \param CGF Reference to current CodeGenFunction.
968  /// \param Loc Clang source location.
969  /// \param DKind Kind of the directive.
970  /// \param ScheduleKind Schedule kind, specified by the 'schedule' clause.
971  /// \param Values Input arguments for the construct.
972  ///
973  virtual void emitForStaticInit(CodeGenFunction &CGF, SourceLocation Loc,
974  OpenMPDirectiveKind DKind,
975  const OpenMPScheduleTy &ScheduleKind,
976  const StaticRTInput &Values);
977 
978  ///
979  /// \param CGF Reference to current CodeGenFunction.
980  /// \param Loc Clang source location.
981  /// \param SchedKind Schedule kind, specified by the 'dist_schedule' clause.
982  /// \param Values Input arguments for the construct.
983  ///
984  virtual void emitDistributeStaticInit(CodeGenFunction &CGF,
985  SourceLocation Loc,
987  const StaticRTInput &Values);
988 
989  /// Call the appropriate runtime routine to notify that we finished
990  /// iteration of the ordered loop with the dynamic scheduling.
991  ///
992  /// \param CGF Reference to current CodeGenFunction.
993  /// \param Loc Clang source location.
994  /// \param IVSize Size of the iteration variable in bits.
995  /// \param IVSigned Sign of the iteration variable.
996  ///
998  SourceLocation Loc, unsigned IVSize,
999  bool IVSigned);
1000 
1001  /// Call the appropriate runtime routine to notify that we finished
1002  /// all the work with current loop.
1003  ///
1004  /// \param CGF Reference to current CodeGenFunction.
1005  /// \param Loc Clang source location.
1006  /// \param DKind Kind of the directive for which the static finish is emitted.
1007  ///
1008  virtual void emitForStaticFinish(CodeGenFunction &CGF, SourceLocation Loc,
1009  OpenMPDirectiveKind DKind);
1010 
1011  /// Call __kmpc_dispatch_next(
1012  /// ident_t *loc, kmp_int32 tid, kmp_int32 *p_lastiter,
1013  /// kmp_int[32|64] *p_lower, kmp_int[32|64] *p_upper,
1014  /// kmp_int[32|64] *p_stride);
1015  /// \param IVSize Size of the iteration variable in bits.
1016  /// \param IVSigned Sign of the iteration variable.
1017  /// \param IL Address of the output variable in which the flag of the
1018  /// last iteration is returned.
1019  /// \param LB Address of the output variable in which the lower iteration
1020  /// number is returned.
1021  /// \param UB Address of the output variable in which the upper iteration
1022  /// number is returned.
1023  /// \param ST Address of the output variable in which the stride value is
1024  /// returned.
1026  unsigned IVSize, bool IVSigned,
1027  Address IL, Address LB,
1028  Address UB, Address ST);
1029 
1030  /// Emits call to void __kmpc_push_num_threads(ident_t *loc, kmp_int32
1031  /// global_tid, kmp_int32 num_threads) to generate code for 'num_threads'
1032  /// clause.
1033  /// \param NumThreads An integer value of threads.
1034  virtual void emitNumThreadsClause(CodeGenFunction &CGF,
1035  llvm::Value *NumThreads,
1036  SourceLocation Loc);
1037 
1038  /// Emit call to void __kmpc_push_proc_bind(ident_t *loc, kmp_int32
1039  /// global_tid, int proc_bind) to generate code for 'proc_bind' clause.
1040  virtual void emitProcBindClause(CodeGenFunction &CGF,
1041  OpenMPProcBindClauseKind ProcBind,
1042  SourceLocation Loc);
1043 
1044  /// Returns address of the threadprivate variable for the current
1045  /// thread.
1046  /// \param VD Threadprivate variable.
1047  /// \param VDAddr Address of the global variable \a VD.
1048  /// \param Loc Location of the reference to threadprivate var.
1049  /// \return Address of the threadprivate variable for the current thread.
1051  const VarDecl *VD,
1052  Address VDAddr,
1053  SourceLocation Loc);
1054 
1055  /// Returns the address of the variable marked as declare target with link
1056  /// clause.
1057  virtual Address getAddrOfDeclareTargetLink(const VarDecl *VD);
1058 
1059  /// Emit a code for initialization of threadprivate variable. It emits
1060  /// a call to runtime library which adds initial value to the newly created
1061  /// threadprivate variable (if it is not constant) and registers destructor
1062  /// for the variable (if any).
1063  /// \param VD Threadprivate variable.
1064  /// \param VDAddr Address of the global variable \a VD.
1065  /// \param Loc Location of threadprivate declaration.
1066  /// \param PerformInit true if initialization expression is not constant.
1067  virtual llvm::Function *
1069  SourceLocation Loc, bool PerformInit,
1070  CodeGenFunction *CGF = nullptr);
1071 
1072  /// Emit a code for initialization of declare target variable.
1073  /// \param VD Declare target variable.
1074  /// \param Addr Address of the global variable \a VD.
1075  /// \param PerformInit true if initialization expression is not constant.
1076  virtual bool emitDeclareTargetVarDefinition(const VarDecl *VD,
1077  llvm::GlobalVariable *Addr,
1078  bool PerformInit);
1079 
1080  /// Creates artificial threadprivate variable with name \p Name and type \p
1081  /// VarType.
1082  /// \param VarType Type of the artificial threadprivate variable.
1083  /// \param Name Name of the artificial threadprivate variable.
1085  QualType VarType,
1086  StringRef Name);
1087 
1088  /// Emit flush of the variables specified in 'omp flush' directive.
1089  /// \param Vars List of variables to flush.
1090  virtual void emitFlush(CodeGenFunction &CGF, ArrayRef<const Expr *> Vars,
1091  SourceLocation Loc);
1092 
1093  /// Emit task region for the task directive. The task region is
1094  /// emitted in several steps:
1095  /// 1. Emit a call to kmp_task_t *__kmpc_omp_task_alloc(ident_t *, kmp_int32
1096  /// gtid, kmp_int32 flags, size_t sizeof_kmp_task_t, size_t sizeof_shareds,
1097  /// kmp_routine_entry_t *task_entry). Here task_entry is a pointer to the
1098  /// function:
1099  /// kmp_int32 .omp_task_entry.(kmp_int32 gtid, kmp_task_t *tt) {
1100  /// TaskFunction(gtid, tt->part_id, tt->shareds);
1101  /// return 0;
1102  /// }
1103  /// 2. Copy a list of shared variables to field shareds of the resulting
1104  /// structure kmp_task_t returned by the previous call (if any).
1105  /// 3. Copy a pointer to destructions function to field destructions of the
1106  /// resulting structure kmp_task_t.
1107  /// 4. Emit a call to kmp_int32 __kmpc_omp_task(ident_t *, kmp_int32 gtid,
1108  /// kmp_task_t *new_task), where new_task is a resulting structure from
1109  /// previous items.
1110  /// \param D Current task directive.
1111  /// \param TaskFunction An LLVM function with type void (*)(i32 /*gtid*/, i32
1112  /// /*part_id*/, captured_struct */*__context*/);
1113  /// \param SharedsTy A type which contains references the shared variables.
1114  /// \param Shareds Context with the list of shared variables from the \p
1115  /// TaskFunction.
1116  /// \param IfCond Not a nullptr if 'if' clause was specified, nullptr
1117  /// otherwise.
1118  /// \param Data Additional data for task generation like tiednsee, final
1119  /// state, list of privates etc.
1120  virtual void emitTaskCall(CodeGenFunction &CGF, SourceLocation Loc,
1121  const OMPExecutableDirective &D,
1122  llvm::Value *TaskFunction, QualType SharedsTy,
1123  Address Shareds, const Expr *IfCond,
1124  const OMPTaskDataTy &Data);
1125 
1126  /// Emit task region for the taskloop directive. The taskloop region is
1127  /// emitted in several steps:
1128  /// 1. Emit a call to kmp_task_t *__kmpc_omp_task_alloc(ident_t *, kmp_int32
1129  /// gtid, kmp_int32 flags, size_t sizeof_kmp_task_t, size_t sizeof_shareds,
1130  /// kmp_routine_entry_t *task_entry). Here task_entry is a pointer to the
1131  /// function:
1132  /// kmp_int32 .omp_task_entry.(kmp_int32 gtid, kmp_task_t *tt) {
1133  /// TaskFunction(gtid, tt->part_id, tt->shareds);
1134  /// return 0;
1135  /// }
1136  /// 2. Copy a list of shared variables to field shareds of the resulting
1137  /// structure kmp_task_t returned by the previous call (if any).
1138  /// 3. Copy a pointer to destructions function to field destructions of the
1139  /// resulting structure kmp_task_t.
1140  /// 4. Emit a call to void __kmpc_taskloop(ident_t *loc, int gtid, kmp_task_t
1141  /// *task, int if_val, kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st, int
1142  /// nogroup, int sched, kmp_uint64 grainsize, void *task_dup ), where new_task
1143  /// is a resulting structure from
1144  /// previous items.
1145  /// \param D Current task directive.
1146  /// \param TaskFunction An LLVM function with type void (*)(i32 /*gtid*/, i32
1147  /// /*part_id*/, captured_struct */*__context*/);
1148  /// \param SharedsTy A type which contains references the shared variables.
1149  /// \param Shareds Context with the list of shared variables from the \p
1150  /// TaskFunction.
1151  /// \param IfCond Not a nullptr if 'if' clause was specified, nullptr
1152  /// otherwise.
1153  /// \param Data Additional data for task generation like tiednsee, final
1154  /// state, list of privates etc.
1155  virtual void emitTaskLoopCall(
1156  CodeGenFunction &CGF, SourceLocation Loc, const OMPLoopDirective &D,
1157  llvm::Value *TaskFunction, QualType SharedsTy, Address Shareds,
1158  const Expr *IfCond, const OMPTaskDataTy &Data);
1159 
1160  /// Emit code for the directive that does not require outlining.
1161  ///
1162  /// \param InnermostKind Kind of innermost directive (for simple directives it
1163  /// is a directive itself, for combined - its innermost directive).
1164  /// \param CodeGen Code generation sequence for the \a D directive.
1165  /// \param HasCancel true if region has inner cancel directive, false
1166  /// otherwise.
1167  virtual void emitInlinedDirective(CodeGenFunction &CGF,
1168  OpenMPDirectiveKind InnermostKind,
1169  const RegionCodeGenTy &CodeGen,
1170  bool HasCancel = false);
1171 
1172  /// Emits reduction function.
1173  /// \param ArgsType Array type containing pointers to reduction variables.
1174  /// \param Privates List of private copies for original reduction arguments.
1175  /// \param LHSExprs List of LHS in \a ReductionOps reduction operations.
1176  /// \param RHSExprs List of RHS in \a ReductionOps reduction operations.
1177  /// \param ReductionOps List of reduction operations in form 'LHS binop RHS'
1178  /// or 'operator binop(LHS, RHS)'.
1180  llvm::Type *ArgsType,
1182  ArrayRef<const Expr *> LHSExprs,
1183  ArrayRef<const Expr *> RHSExprs,
1184  ArrayRef<const Expr *> ReductionOps);
1185 
1186  /// Emits single reduction combiner
1188  const Expr *ReductionOp,
1189  const Expr *PrivateRef,
1190  const DeclRefExpr *LHS,
1191  const DeclRefExpr *RHS);
1192 
1197  };
1198  /// Emit a code for reduction clause. Next code should be emitted for
1199  /// reduction:
1200  /// \code
1201  ///
1202  /// static kmp_critical_name lock = { 0 };
1203  ///
1204  /// void reduce_func(void *lhs[<n>], void *rhs[<n>]) {
1205  /// ...
1206  /// *(Type<i>*)lhs[i] = RedOp<i>(*(Type<i>*)lhs[i], *(Type<i>*)rhs[i]);
1207  /// ...
1208  /// }
1209  ///
1210  /// ...
1211  /// void *RedList[<n>] = {&<RHSExprs>[0], ..., &<RHSExprs>[<n>-1]};
1212  /// switch (__kmpc_reduce{_nowait}(<loc>, <gtid>, <n>, sizeof(RedList),
1213  /// RedList, reduce_func, &<lock>)) {
1214  /// case 1:
1215  /// ...
1216  /// <LHSExprs>[i] = RedOp<i>(*<LHSExprs>[i], *<RHSExprs>[i]);
1217  /// ...
1218  /// __kmpc_end_reduce{_nowait}(<loc>, <gtid>, &<lock>);
1219  /// break;
1220  /// case 2:
1221  /// ...
1222  /// Atomic(<LHSExprs>[i] = RedOp<i>(*<LHSExprs>[i], *<RHSExprs>[i]));
1223  /// ...
1224  /// break;
1225  /// default:;
1226  /// }
1227  /// \endcode
1228  ///
1229  /// \param Privates List of private copies for original reduction arguments.
1230  /// \param LHSExprs List of LHS in \a ReductionOps reduction operations.
1231  /// \param RHSExprs List of RHS in \a ReductionOps reduction operations.
1232  /// \param ReductionOps List of reduction operations in form 'LHS binop RHS'
1233  /// or 'operator binop(LHS, RHS)'.
1234  /// \param Options List of options for reduction codegen:
1235  /// WithNowait true if parent directive has also nowait clause, false
1236  /// otherwise.
1237  /// SimpleReduction Emit reduction operation only. Used for omp simd
1238  /// directive on the host.
1239  /// ReductionKind The kind of reduction to perform.
1240  virtual void emitReduction(CodeGenFunction &CGF, SourceLocation Loc,
1242  ArrayRef<const Expr *> LHSExprs,
1243  ArrayRef<const Expr *> RHSExprs,
1244  ArrayRef<const Expr *> ReductionOps,
1245  ReductionOptionsTy Options);
1246 
1247  /// Emit a code for initialization of task reduction clause. Next code
1248  /// should be emitted for reduction:
1249  /// \code
1250  ///
1251  /// _task_red_item_t red_data[n];
1252  /// ...
1253  /// red_data[i].shar = &origs[i];
1254  /// red_data[i].size = sizeof(origs[i]);
1255  /// red_data[i].f_init = (void*)RedInit<i>;
1256  /// red_data[i].f_fini = (void*)RedDest<i>;
1257  /// red_data[i].f_comb = (void*)RedOp<i>;
1258  /// red_data[i].flags = <Flag_i>;
1259  /// ...
1260  /// void* tg1 = __kmpc_task_reduction_init(gtid, n, red_data);
1261  /// \endcode
1262  ///
1263  /// \param LHSExprs List of LHS in \a Data.ReductionOps reduction operations.
1264  /// \param RHSExprs List of RHS in \a Data.ReductionOps reduction operations.
1265  /// \param Data Additional data for task generation like tiedness, final
1266  /// state, list of privates, reductions etc.
1268  SourceLocation Loc,
1269  ArrayRef<const Expr *> LHSExprs,
1270  ArrayRef<const Expr *> RHSExprs,
1271  const OMPTaskDataTy &Data);
1272 
1273  /// Required to resolve existing problems in the runtime. Emits threadprivate
1274  /// variables to store the size of the VLAs/array sections for
1275  /// initializer/combiner/finalizer functions + emits threadprivate variable to
1276  /// store the pointer to the original reduction item for the custom
1277  /// initializer defined by declare reduction construct.
1278  /// \param RCG Allows to reuse an existing data for the reductions.
1279  /// \param N Reduction item for which fixups must be emitted.
1281  ReductionCodeGen &RCG, unsigned N);
1282 
1283  /// Get the address of `void *` type of the privatue copy of the reduction
1284  /// item specified by the \p SharedLVal.
1285  /// \param ReductionsPtr Pointer to the reduction data returned by the
1286  /// emitTaskReductionInit function.
1287  /// \param SharedLVal Address of the original reduction item.
1289  llvm::Value *ReductionsPtr,
1290  LValue SharedLVal);
1291 
1292  /// Emit code for 'taskwait' directive.
1293  virtual void emitTaskwaitCall(CodeGenFunction &CGF, SourceLocation Loc);
1294 
1295  /// Emit code for 'cancellation point' construct.
1296  /// \param CancelRegion Region kind for which the cancellation point must be
1297  /// emitted.
1298  ///
1299  virtual void emitCancellationPointCall(CodeGenFunction &CGF,
1300  SourceLocation Loc,
1301  OpenMPDirectiveKind CancelRegion);
1302 
1303  /// Emit code for 'cancel' construct.
1304  /// \param IfCond Condition in the associated 'if' clause, if it was
1305  /// specified, nullptr otherwise.
1306  /// \param CancelRegion Region kind for which the cancel must be emitted.
1307  ///
1308  virtual void emitCancelCall(CodeGenFunction &CGF, SourceLocation Loc,
1309  const Expr *IfCond,
1310  OpenMPDirectiveKind CancelRegion);
1311 
1312  /// Emit outilined function for 'target' directive.
1313  /// \param D Directive to emit.
1314  /// \param ParentName Name of the function that encloses the target region.
1315  /// \param OutlinedFn Outlined function value to be defined by this call.
1316  /// \param OutlinedFnID Outlined function ID value to be defined by this call.
1317  /// \param IsOffloadEntry True if the outlined function is an offload entry.
1318  /// \param CodeGen Code generation sequence for the \a D directive.
1319  /// An outlined function may not be an entry if, e.g. the if clause always
1320  /// evaluates to false.
1321  virtual void emitTargetOutlinedFunction(const OMPExecutableDirective &D,
1322  StringRef ParentName,
1323  llvm::Function *&OutlinedFn,
1324  llvm::Constant *&OutlinedFnID,
1325  bool IsOffloadEntry,
1326  const RegionCodeGenTy &CodeGen);
1327 
1328  /// Emit the target offloading code associated with \a D. The emitted
1329  /// code attempts offloading the execution to the device, an the event of
1330  /// a failure it executes the host version outlined in \a OutlinedFn.
1331  /// \param D Directive to emit.
1332  /// \param OutlinedFn Host version of the code to be offloaded.
1333  /// \param OutlinedFnID ID of host version of the code to be offloaded.
1334  /// \param IfCond Expression evaluated in if clause associated with the target
1335  /// directive, or null if no if clause is used.
1336  /// \param Device Expression evaluated in device clause associated with the
1337  /// target directive, or null if no device clause is used.
1338  virtual void emitTargetCall(CodeGenFunction &CGF,
1339  const OMPExecutableDirective &D,
1340  llvm::Value *OutlinedFn,
1341  llvm::Value *OutlinedFnID, const Expr *IfCond,
1342  const Expr *Device);
1343 
1344  /// Emit the target regions enclosed in \a GD function definition or
1345  /// the function itself in case it is a valid device function. Returns true if
1346  /// \a GD was dealt with successfully.
1347  /// \param GD Function to scan.
1348  virtual bool emitTargetFunctions(GlobalDecl GD);
1349 
1350  /// Emit the global variable if it is a valid device global variable.
1351  /// Returns true if \a GD was dealt with successfully.
1352  /// \param GD Variable declaration to emit.
1353  virtual bool emitTargetGlobalVariable(GlobalDecl GD);
1354 
1355  /// Checks if the provided global decl \a GD is a declare target variable and
1356  /// registers it when emitting code for the host.
1357  virtual void registerTargetGlobalVariable(const VarDecl *VD,
1358  llvm::Constant *Addr);
1359 
1360  /// Emit the global \a GD if it is meaningful for the target. Returns
1361  /// if it was emitted successfully.
1362  /// \param GD Global to scan.
1363  virtual bool emitTargetGlobal(GlobalDecl GD);
1364 
1365  /// Creates the offloading descriptor in the event any target region
1366  /// was emitted in the current module and return the function that registers
1367  /// it.
1368  virtual llvm::Function *emitRegistrationFunction();
1369 
1370  /// Emits code for teams call of the \a OutlinedFn with
1371  /// variables captured in a record which address is stored in \a
1372  /// CapturedStruct.
1373  /// \param OutlinedFn Outlined function to be run by team masters. Type of
1374  /// this function is void(*)(kmp_int32 *, kmp_int32, struct context_vars*).
1375  /// \param CapturedVars A pointer to the record with the references to
1376  /// variables used in \a OutlinedFn function.
1377  ///
1378  virtual void emitTeamsCall(CodeGenFunction &CGF,
1379  const OMPExecutableDirective &D,
1380  SourceLocation Loc, llvm::Value *OutlinedFn,
1381  ArrayRef<llvm::Value *> CapturedVars);
1382 
1383  /// Emits call to void __kmpc_push_num_teams(ident_t *loc, kmp_int32
1384  /// global_tid, kmp_int32 num_teams, kmp_int32 thread_limit) to generate code
1385  /// for num_teams clause.
1386  /// \param NumTeams An integer expression of teams.
1387  /// \param ThreadLimit An integer expression of threads.
1388  virtual void emitNumTeamsClause(CodeGenFunction &CGF, const Expr *NumTeams,
1389  const Expr *ThreadLimit, SourceLocation Loc);
1390 
1391  /// Struct that keeps all the relevant information that should be kept
1392  /// throughout a 'target data' region.
1394  /// Set to true if device pointer information have to be obtained.
1395  bool RequiresDevicePointerInfo = false;
1396 
1397  public:
1398  /// The array of base pointer passed to the runtime library.
1399  llvm::Value *BasePointersArray = nullptr;
1400  /// The array of section pointers passed to the runtime library.
1401  llvm::Value *PointersArray = nullptr;
1402  /// The array of sizes passed to the runtime library.
1403  llvm::Value *SizesArray = nullptr;
1404  /// The array of map types passed to the runtime library.
1405  llvm::Value *MapTypesArray = nullptr;
1406  /// The total number of pointers passed to the runtime library.
1407  unsigned NumberOfPtrs = 0u;
1408  /// Map between the a declaration of a capture and the corresponding base
1409  /// pointer address where the runtime returns the device pointers.
1410  llvm::DenseMap<const ValueDecl *, Address> CaptureDeviceAddrMap;
1411 
1412  explicit TargetDataInfo() {}
1413  explicit TargetDataInfo(bool RequiresDevicePointerInfo)
1414  : RequiresDevicePointerInfo(RequiresDevicePointerInfo) {}
1415  /// Clear information about the data arrays.
1417  BasePointersArray = nullptr;
1418  PointersArray = nullptr;
1419  SizesArray = nullptr;
1420  MapTypesArray = nullptr;
1421  NumberOfPtrs = 0u;
1422  }
1423  /// Return true if the current target data information has valid arrays.
1424  bool isValid() {
1425  return BasePointersArray && PointersArray && SizesArray &&
1426  MapTypesArray && NumberOfPtrs;
1427  }
1428  bool requiresDevicePointerInfo() { return RequiresDevicePointerInfo; }
1429  };
1430 
1431  /// Emit the target data mapping code associated with \a D.
1432  /// \param D Directive to emit.
1433  /// \param IfCond Expression evaluated in if clause associated with the
1434  /// target directive, or null if no device clause is used.
1435  /// \param Device Expression evaluated in device clause associated with the
1436  /// target directive, or null if no device clause is used.
1437  /// \param Info A record used to store information that needs to be preserved
1438  /// until the region is closed.
1439  virtual void emitTargetDataCalls(CodeGenFunction &CGF,
1440  const OMPExecutableDirective &D,
1441  const Expr *IfCond, const Expr *Device,
1442  const RegionCodeGenTy &CodeGen,
1443  TargetDataInfo &Info);
1444 
1445  /// Emit the data mapping/movement code associated with the directive
1446  /// \a D that should be of the form 'target [{enter|exit} data | update]'.
1447  /// \param D Directive to emit.
1448  /// \param IfCond Expression evaluated in if clause associated with the target
1449  /// directive, or null if no if clause is used.
1450  /// \param Device Expression evaluated in device clause associated with the
1451  /// target directive, or null if no device clause is used.
1453  const OMPExecutableDirective &D,
1454  const Expr *IfCond,
1455  const Expr *Device);
1456 
1457  /// Marks function \a Fn with properly mangled versions of vector functions.
1458  /// \param FD Function marked as 'declare simd'.
1459  /// \param Fn LLVM function that must be marked with 'declare simd'
1460  /// attributes.
1461  virtual void emitDeclareSimdFunction(const FunctionDecl *FD,
1462  llvm::Function *Fn);
1463 
1464  /// Emit initialization for doacross loop nesting support.
1465  /// \param D Loop-based construct used in doacross nesting construct.
1466  virtual void emitDoacrossInit(CodeGenFunction &CGF,
1467  const OMPLoopDirective &D);
1468 
1469  /// Emit code for doacross ordered directive with 'depend' clause.
1470  /// \param C 'depend' clause with 'sink|source' dependency kind.
1471  virtual void emitDoacrossOrdered(CodeGenFunction &CGF,
1472  const OMPDependClause *C);
1473 
1474  /// Translates the native parameter of outlined function if this is required
1475  /// for target.
1476  /// \param FD Field decl from captured record for the parameter.
1477  /// \param NativeParam Parameter itself.
1478  virtual const VarDecl *translateParameter(const FieldDecl *FD,
1479  const VarDecl *NativeParam) const {
1480  return NativeParam;
1481  }
1482 
1483  /// Gets the address of the native argument basing on the address of the
1484  /// target-specific parameter.
1485  /// \param NativeParam Parameter itself.
1486  /// \param TargetParam Corresponding target-specific parameter.
1488  const VarDecl *NativeParam,
1489  const VarDecl *TargetParam) const;
1490 
1491  /// Emits call of the outlined function with the provided arguments,
1492  /// translating these arguments to correct target-specific arguments.
1493  virtual void
1495  llvm::Value *OutlinedFn,
1496  ArrayRef<llvm::Value *> Args = llvm::None) const;
1497 
1498  /// Emits OpenMP-specific function prolog.
1499  /// Required for device constructs.
1500  virtual void emitFunctionProlog(CodeGenFunction &CGF, const Decl *D) {}
1501 
1502  /// Gets the OpenMP-specific address of the local variable.
1504  const VarDecl *VD);
1505 
1506  /// Marks the declaration as alread emitted for the device code and returns
1507  /// true, if it was marked already, and false, otherwise.
1508  bool markAsGlobalTarget(GlobalDecl GD);
1509 
1510 };
1511 
1512 /// Class supports emissionof SIMD-only code.
1513 class CGOpenMPSIMDRuntime final : public CGOpenMPRuntime {
1514 public:
1515  explicit CGOpenMPSIMDRuntime(CodeGenModule &CGM) : CGOpenMPRuntime(CGM) {}
1516  ~CGOpenMPSIMDRuntime() override {}
1517 
1518  /// Emits outlined function for the specified OpenMP parallel directive
1519  /// \a D. This outlined function has type void(*)(kmp_int32 *ThreadID,
1520  /// kmp_int32 BoundID, struct context_vars*).
1521  /// \param D OpenMP directive.
1522  /// \param ThreadIDVar Variable for thread id in the current OpenMP region.
1523  /// \param InnermostKind Kind of innermost directive (for simple directives it
1524  /// is a directive itself, for combined - its innermost directive).
1525  /// \param CodeGen Code generation sequence for the \a D directive.
1526  llvm::Value *
1528  const VarDecl *ThreadIDVar,
1529  OpenMPDirectiveKind InnermostKind,
1530  const RegionCodeGenTy &CodeGen) override;
1531 
1532  /// Emits outlined function for the specified OpenMP teams directive
1533  /// \a D. This outlined function has type void(*)(kmp_int32 *ThreadID,
1534  /// kmp_int32 BoundID, struct context_vars*).
1535  /// \param D OpenMP directive.
1536  /// \param ThreadIDVar Variable for thread id in the current OpenMP region.
1537  /// \param InnermostKind Kind of innermost directive (for simple directives it
1538  /// is a directive itself, for combined - its innermost directive).
1539  /// \param CodeGen Code generation sequence for the \a D directive.
1540  llvm::Value *
1542  const VarDecl *ThreadIDVar,
1543  OpenMPDirectiveKind InnermostKind,
1544  const RegionCodeGenTy &CodeGen) override;
1545 
1546  /// Emits outlined function for the OpenMP task directive \a D. This
1547  /// outlined function has type void(*)(kmp_int32 ThreadID, struct task_t*
1548  /// TaskT).
1549  /// \param D OpenMP directive.
1550  /// \param ThreadIDVar Variable for thread id in the current OpenMP region.
1551  /// \param PartIDVar Variable for partition id in the current OpenMP untied
1552  /// task region.
1553  /// \param TaskTVar Variable for task_t argument.
1554  /// \param InnermostKind Kind of innermost directive (for simple directives it
1555  /// is a directive itself, for combined - its innermost directive).
1556  /// \param CodeGen Code generation sequence for the \a D directive.
1557  /// \param Tied true if task is generated for tied task, false otherwise.
1558  /// \param NumberOfParts Number of parts in untied task. Ignored for tied
1559  /// tasks.
1560  ///
1562  const OMPExecutableDirective &D, const VarDecl *ThreadIDVar,
1563  const VarDecl *PartIDVar, const VarDecl *TaskTVar,
1564  OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen,
1565  bool Tied, unsigned &NumberOfParts) override;
1566 
1567  /// Emits code for parallel or serial call of the \a OutlinedFn with
1568  /// variables captured in a record which address is stored in \a
1569  /// CapturedStruct.
1570  /// \param OutlinedFn Outlined function to be run in parallel threads. Type of
1571  /// this function is void(*)(kmp_int32 *, kmp_int32, struct context_vars*).
1572  /// \param CapturedVars A pointer to the record with the references to
1573  /// variables used in \a OutlinedFn function.
1574  /// \param IfCond Condition in the associated 'if' clause, if it was
1575  /// specified, nullptr otherwise.
1576  ///
1577  void emitParallelCall(CodeGenFunction &CGF, SourceLocation Loc,
1578  llvm::Value *OutlinedFn,
1579  ArrayRef<llvm::Value *> CapturedVars,
1580  const Expr *IfCond) override;
1581 
1582  /// Emits a critical region.
1583  /// \param CriticalName Name of the critical region.
1584  /// \param CriticalOpGen Generator for the statement associated with the given
1585  /// critical region.
1586  /// \param Hint Value of the 'hint' clause (optional).
1587  void emitCriticalRegion(CodeGenFunction &CGF, StringRef CriticalName,
1588  const RegionCodeGenTy &CriticalOpGen,
1589  SourceLocation Loc,
1590  const Expr *Hint = nullptr) override;
1591 
1592  /// Emits a master region.
1593  /// \param MasterOpGen Generator for the statement associated with the given
1594  /// master region.
1595  void emitMasterRegion(CodeGenFunction &CGF,
1596  const RegionCodeGenTy &MasterOpGen,
1597  SourceLocation Loc) override;
1598 
1599  /// Emits code for a taskyield directive.
1600  void emitTaskyieldCall(CodeGenFunction &CGF, SourceLocation Loc) override;
1601 
1602  /// Emit a taskgroup region.
1603  /// \param TaskgroupOpGen Generator for the statement associated with the
1604  /// given taskgroup region.
1605  void emitTaskgroupRegion(CodeGenFunction &CGF,
1606  const RegionCodeGenTy &TaskgroupOpGen,
1607  SourceLocation Loc) override;
1608 
1609  /// Emits a single region.
1610  /// \param SingleOpGen Generator for the statement associated with the given
1611  /// single region.
1612  void emitSingleRegion(CodeGenFunction &CGF,
1613  const RegionCodeGenTy &SingleOpGen, SourceLocation Loc,
1614  ArrayRef<const Expr *> CopyprivateVars,
1615  ArrayRef<const Expr *> DestExprs,
1616  ArrayRef<const Expr *> SrcExprs,
1617  ArrayRef<const Expr *> AssignmentOps) override;
1618 
1619  /// Emit an ordered region.
1620  /// \param OrderedOpGen Generator for the statement associated with the given
1621  /// ordered region.
1622  void emitOrderedRegion(CodeGenFunction &CGF,
1623  const RegionCodeGenTy &OrderedOpGen,
1624  SourceLocation Loc, bool IsThreads) override;
1625 
1626  /// Emit an implicit/explicit barrier for OpenMP threads.
1627  /// \param Kind Directive for which this implicit barrier call must be
1628  /// generated. Must be OMPD_barrier for explicit barrier generation.
1629  /// \param EmitChecks true if need to emit checks for cancellation barriers.
1630  /// \param ForceSimpleCall true simple barrier call must be emitted, false if
1631  /// runtime class decides which one to emit (simple or with cancellation
1632  /// checks).
1633  ///
1634  void emitBarrierCall(CodeGenFunction &CGF, SourceLocation Loc,
1635  OpenMPDirectiveKind Kind, bool EmitChecks = true,
1636  bool ForceSimpleCall = false) override;
1637 
1638  /// This is used for non static scheduled types and when the ordered
1639  /// clause is present on the loop construct.
1640  /// Depending on the loop schedule, it is necessary to call some runtime
1641  /// routine before start of the OpenMP loop to get the loop upper / lower
1642  /// bounds \a LB and \a UB and stride \a ST.
1643  ///
1644  /// \param CGF Reference to current CodeGenFunction.
1645  /// \param Loc Clang source location.
1646  /// \param ScheduleKind Schedule kind, specified by the 'schedule' clause.
1647  /// \param IVSize Size of the iteration variable in bits.
1648  /// \param IVSigned Sign of the iteration variable.
1649  /// \param Ordered true if loop is ordered, false otherwise.
1650  /// \param DispatchValues struct containing llvm values for lower bound, upper
1651  /// bound, and chunk expression.
1652  /// For the default (nullptr) value, the chunk 1 will be used.
1653  ///
1654  void emitForDispatchInit(CodeGenFunction &CGF, SourceLocation Loc,
1655  const OpenMPScheduleTy &ScheduleKind,
1656  unsigned IVSize, bool IVSigned, bool Ordered,
1657  const DispatchRTInput &DispatchValues) override;
1658 
1659  /// Call the appropriate runtime routine to initialize it before start
1660  /// of loop.
1661  ///
1662  /// This is used only in case of static schedule, when the user did not
1663  /// specify a ordered clause on the loop construct.
1664  /// Depending on the loop schedule, it is necessary to call some runtime
1665  /// routine before start of the OpenMP loop to get the loop upper / lower
1666  /// bounds LB and UB and stride ST.
1667  ///
1668  /// \param CGF Reference to current CodeGenFunction.
1669  /// \param Loc Clang source location.
1670  /// \param DKind Kind of the directive.
1671  /// \param ScheduleKind Schedule kind, specified by the 'schedule' clause.
1672  /// \param Values Input arguments for the construct.
1673  ///
1674  void emitForStaticInit(CodeGenFunction &CGF, SourceLocation Loc,
1675  OpenMPDirectiveKind DKind,
1676  const OpenMPScheduleTy &ScheduleKind,
1677  const StaticRTInput &Values) override;
1678 
1679  ///
1680  /// \param CGF Reference to current CodeGenFunction.
1681  /// \param Loc Clang source location.
1682  /// \param SchedKind Schedule kind, specified by the 'dist_schedule' clause.
1683  /// \param Values Input arguments for the construct.
1684  ///
1685  void emitDistributeStaticInit(CodeGenFunction &CGF, SourceLocation Loc,
1686  OpenMPDistScheduleClauseKind SchedKind,
1687  const StaticRTInput &Values) override;
1688 
1689  /// Call the appropriate runtime routine to notify that we finished
1690  /// iteration of the ordered loop with the dynamic scheduling.
1691  ///
1692  /// \param CGF Reference to current CodeGenFunction.
1693  /// \param Loc Clang source location.
1694  /// \param IVSize Size of the iteration variable in bits.
1695  /// \param IVSigned Sign of the iteration variable.
1696  ///
1697  void emitForOrderedIterationEnd(CodeGenFunction &CGF, SourceLocation Loc,
1698  unsigned IVSize, bool IVSigned) override;
1699 
1700  /// Call the appropriate runtime routine to notify that we finished
1701  /// all the work with current loop.
1702  ///
1703  /// \param CGF Reference to current CodeGenFunction.
1704  /// \param Loc Clang source location.
1705  /// \param DKind Kind of the directive for which the static finish is emitted.
1706  ///
1707  void emitForStaticFinish(CodeGenFunction &CGF, SourceLocation Loc,
1708  OpenMPDirectiveKind DKind) override;
1709 
1710  /// Call __kmpc_dispatch_next(
1711  /// ident_t *loc, kmp_int32 tid, kmp_int32 *p_lastiter,
1712  /// kmp_int[32|64] *p_lower, kmp_int[32|64] *p_upper,
1713  /// kmp_int[32|64] *p_stride);
1714  /// \param IVSize Size of the iteration variable in bits.
1715  /// \param IVSigned Sign of the iteration variable.
1716  /// \param IL Address of the output variable in which the flag of the
1717  /// last iteration is returned.
1718  /// \param LB Address of the output variable in which the lower iteration
1719  /// number is returned.
1720  /// \param UB Address of the output variable in which the upper iteration
1721  /// number is returned.
1722  /// \param ST Address of the output variable in which the stride value is
1723  /// returned.
1724  llvm::Value *emitForNext(CodeGenFunction &CGF, SourceLocation Loc,
1725  unsigned IVSize, bool IVSigned, Address IL,
1726  Address LB, Address UB, Address ST) override;
1727 
1728  /// Emits call to void __kmpc_push_num_threads(ident_t *loc, kmp_int32
1729  /// global_tid, kmp_int32 num_threads) to generate code for 'num_threads'
1730  /// clause.
1731  /// \param NumThreads An integer value of threads.
1732  void emitNumThreadsClause(CodeGenFunction &CGF, llvm::Value *NumThreads,
1733  SourceLocation Loc) override;
1734 
1735  /// Emit call to void __kmpc_push_proc_bind(ident_t *loc, kmp_int32
1736  /// global_tid, int proc_bind) to generate code for 'proc_bind' clause.
1737  void emitProcBindClause(CodeGenFunction &CGF,
1738  OpenMPProcBindClauseKind ProcBind,
1739  SourceLocation Loc) override;
1740 
1741  /// Returns address of the threadprivate variable for the current
1742  /// thread.
1743  /// \param VD Threadprivate variable.
1744  /// \param VDAddr Address of the global variable \a VD.
1745  /// \param Loc Location of the reference to threadprivate var.
1746  /// \return Address of the threadprivate variable for the current thread.
1747  Address getAddrOfThreadPrivate(CodeGenFunction &CGF, const VarDecl *VD,
1748  Address VDAddr, SourceLocation Loc) override;
1749 
1750  /// Emit a code for initialization of threadprivate variable. It emits
1751  /// a call to runtime library which adds initial value to the newly created
1752  /// threadprivate variable (if it is not constant) and registers destructor
1753  /// for the variable (if any).
1754  /// \param VD Threadprivate variable.
1755  /// \param VDAddr Address of the global variable \a VD.
1756  /// \param Loc Location of threadprivate declaration.
1757  /// \param PerformInit true if initialization expression is not constant.
1758  llvm::Function *
1759  emitThreadPrivateVarDefinition(const VarDecl *VD, Address VDAddr,
1760  SourceLocation Loc, bool PerformInit,
1761  CodeGenFunction *CGF = nullptr) override;
1762 
1763  /// Creates artificial threadprivate variable with name \p Name and type \p
1764  /// VarType.
1765  /// \param VarType Type of the artificial threadprivate variable.
1766  /// \param Name Name of the artificial threadprivate variable.
1767  Address getAddrOfArtificialThreadPrivate(CodeGenFunction &CGF,
1768  QualType VarType,
1769  StringRef Name) override;
1770 
1771  /// Emit flush of the variables specified in 'omp flush' directive.
1772  /// \param Vars List of variables to flush.
1773  void emitFlush(CodeGenFunction &CGF, ArrayRef<const Expr *> Vars,
1774  SourceLocation Loc) override;
1775 
1776  /// Emit task region for the task directive. The task region is
1777  /// emitted in several steps:
1778  /// 1. Emit a call to kmp_task_t *__kmpc_omp_task_alloc(ident_t *, kmp_int32
1779  /// gtid, kmp_int32 flags, size_t sizeof_kmp_task_t, size_t sizeof_shareds,
1780  /// kmp_routine_entry_t *task_entry). Here task_entry is a pointer to the
1781  /// function:
1782  /// kmp_int32 .omp_task_entry.(kmp_int32 gtid, kmp_task_t *tt) {
1783  /// TaskFunction(gtid, tt->part_id, tt->shareds);
1784  /// return 0;
1785  /// }
1786  /// 2. Copy a list of shared variables to field shareds of the resulting
1787  /// structure kmp_task_t returned by the previous call (if any).
1788  /// 3. Copy a pointer to destructions function to field destructions of the
1789  /// resulting structure kmp_task_t.
1790  /// 4. Emit a call to kmp_int32 __kmpc_omp_task(ident_t *, kmp_int32 gtid,
1791  /// kmp_task_t *new_task), where new_task is a resulting structure from
1792  /// previous items.
1793  /// \param D Current task directive.
1794  /// \param TaskFunction An LLVM function with type void (*)(i32 /*gtid*/, i32
1795  /// /*part_id*/, captured_struct */*__context*/);
1796  /// \param SharedsTy A type which contains references the shared variables.
1797  /// \param Shareds Context with the list of shared variables from the \p
1798  /// TaskFunction.
1799  /// \param IfCond Not a nullptr if 'if' clause was specified, nullptr
1800  /// otherwise.
1801  /// \param Data Additional data for task generation like tiednsee, final
1802  /// state, list of privates etc.
1803  void emitTaskCall(CodeGenFunction &CGF, SourceLocation Loc,
1804  const OMPExecutableDirective &D, llvm::Value *TaskFunction,
1805  QualType SharedsTy, Address Shareds, const Expr *IfCond,
1806  const OMPTaskDataTy &Data) override;
1807 
1808  /// Emit task region for the taskloop directive. The taskloop region is
1809  /// emitted in several steps:
1810  /// 1. Emit a call to kmp_task_t *__kmpc_omp_task_alloc(ident_t *, kmp_int32
1811  /// gtid, kmp_int32 flags, size_t sizeof_kmp_task_t, size_t sizeof_shareds,
1812  /// kmp_routine_entry_t *task_entry). Here task_entry is a pointer to the
1813  /// function:
1814  /// kmp_int32 .omp_task_entry.(kmp_int32 gtid, kmp_task_t *tt) {
1815  /// TaskFunction(gtid, tt->part_id, tt->shareds);
1816  /// return 0;
1817  /// }
1818  /// 2. Copy a list of shared variables to field shareds of the resulting
1819  /// structure kmp_task_t returned by the previous call (if any).
1820  /// 3. Copy a pointer to destructions function to field destructions of the
1821  /// resulting structure kmp_task_t.
1822  /// 4. Emit a call to void __kmpc_taskloop(ident_t *loc, int gtid, kmp_task_t
1823  /// *task, int if_val, kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st, int
1824  /// nogroup, int sched, kmp_uint64 grainsize, void *task_dup ), where new_task
1825  /// is a resulting structure from
1826  /// previous items.
1827  /// \param D Current task directive.
1828  /// \param TaskFunction An LLVM function with type void (*)(i32 /*gtid*/, i32
1829  /// /*part_id*/, captured_struct */*__context*/);
1830  /// \param SharedsTy A type which contains references the shared variables.
1831  /// \param Shareds Context with the list of shared variables from the \p
1832  /// TaskFunction.
1833  /// \param IfCond Not a nullptr if 'if' clause was specified, nullptr
1834  /// otherwise.
1835  /// \param Data Additional data for task generation like tiednsee, final
1836  /// state, list of privates etc.
1837  void emitTaskLoopCall(CodeGenFunction &CGF, SourceLocation Loc,
1838  const OMPLoopDirective &D, llvm::Value *TaskFunction,
1839  QualType SharedsTy, Address Shareds, const Expr *IfCond,
1840  const OMPTaskDataTy &Data) override;
1841 
1842  /// Emit a code for reduction clause. Next code should be emitted for
1843  /// reduction:
1844  /// \code
1845  ///
1846  /// static kmp_critical_name lock = { 0 };
1847  ///
1848  /// void reduce_func(void *lhs[<n>], void *rhs[<n>]) {
1849  /// ...
1850  /// *(Type<i>*)lhs[i] = RedOp<i>(*(Type<i>*)lhs[i], *(Type<i>*)rhs[i]);
1851  /// ...
1852  /// }
1853  ///
1854  /// ...
1855  /// void *RedList[<n>] = {&<RHSExprs>[0], ..., &<RHSExprs>[<n>-1]};
1856  /// switch (__kmpc_reduce{_nowait}(<loc>, <gtid>, <n>, sizeof(RedList),
1857  /// RedList, reduce_func, &<lock>)) {
1858  /// case 1:
1859  /// ...
1860  /// <LHSExprs>[i] = RedOp<i>(*<LHSExprs>[i], *<RHSExprs>[i]);
1861  /// ...
1862  /// __kmpc_end_reduce{_nowait}(<loc>, <gtid>, &<lock>);
1863  /// break;
1864  /// case 2:
1865  /// ...
1866  /// Atomic(<LHSExprs>[i] = RedOp<i>(*<LHSExprs>[i], *<RHSExprs>[i]));
1867  /// ...
1868  /// break;
1869  /// default:;
1870  /// }
1871  /// \endcode
1872  ///
1873  /// \param Privates List of private copies for original reduction arguments.
1874  /// \param LHSExprs List of LHS in \a ReductionOps reduction operations.
1875  /// \param RHSExprs List of RHS in \a ReductionOps reduction operations.
1876  /// \param ReductionOps List of reduction operations in form 'LHS binop RHS'
1877  /// or 'operator binop(LHS, RHS)'.
1878  /// \param Options List of options for reduction codegen:
1879  /// WithNowait true if parent directive has also nowait clause, false
1880  /// otherwise.
1881  /// SimpleReduction Emit reduction operation only. Used for omp simd
1882  /// directive on the host.
1883  /// ReductionKind The kind of reduction to perform.
1884  void emitReduction(CodeGenFunction &CGF, SourceLocation Loc,
1886  ArrayRef<const Expr *> LHSExprs,
1887  ArrayRef<const Expr *> RHSExprs,
1888  ArrayRef<const Expr *> ReductionOps,
1889  ReductionOptionsTy Options) override;
1890 
1891  /// Emit a code for initialization of task reduction clause. Next code
1892  /// should be emitted for reduction:
1893  /// \code
1894  ///
1895  /// _task_red_item_t red_data[n];
1896  /// ...
1897  /// red_data[i].shar = &origs[i];
1898  /// red_data[i].size = sizeof(origs[i]);
1899  /// red_data[i].f_init = (void*)RedInit<i>;
1900  /// red_data[i].f_fini = (void*)RedDest<i>;
1901  /// red_data[i].f_comb = (void*)RedOp<i>;
1902  /// red_data[i].flags = <Flag_i>;
1903  /// ...
1904  /// void* tg1 = __kmpc_task_reduction_init(gtid, n, red_data);
1905  /// \endcode
1906  ///
1907  /// \param LHSExprs List of LHS in \a Data.ReductionOps reduction operations.
1908  /// \param RHSExprs List of RHS in \a Data.ReductionOps reduction operations.
1909  /// \param Data Additional data for task generation like tiedness, final
1910  /// state, list of privates, reductions etc.
1911  llvm::Value *emitTaskReductionInit(CodeGenFunction &CGF, SourceLocation Loc,
1912  ArrayRef<const Expr *> LHSExprs,
1913  ArrayRef<const Expr *> RHSExprs,
1914  const OMPTaskDataTy &Data) override;
1915 
1916  /// Required to resolve existing problems in the runtime. Emits threadprivate
1917  /// variables to store the size of the VLAs/array sections for
1918  /// initializer/combiner/finalizer functions + emits threadprivate variable to
1919  /// store the pointer to the original reduction item for the custom
1920  /// initializer defined by declare reduction construct.
1921  /// \param RCG Allows to reuse an existing data for the reductions.
1922  /// \param N Reduction item for which fixups must be emitted.
1923  void emitTaskReductionFixups(CodeGenFunction &CGF, SourceLocation Loc,
1924  ReductionCodeGen &RCG, unsigned N) override;
1925 
1926  /// Get the address of `void *` type of the privatue copy of the reduction
1927  /// item specified by the \p SharedLVal.
1928  /// \param ReductionsPtr Pointer to the reduction data returned by the
1929  /// emitTaskReductionInit function.
1930  /// \param SharedLVal Address of the original reduction item.
1931  Address getTaskReductionItem(CodeGenFunction &CGF, SourceLocation Loc,
1932  llvm::Value *ReductionsPtr,
1933  LValue SharedLVal) override;
1934 
1935  /// Emit code for 'taskwait' directive.
1936  void emitTaskwaitCall(CodeGenFunction &CGF, SourceLocation Loc) override;
1937 
1938  /// Emit code for 'cancellation point' construct.
1939  /// \param CancelRegion Region kind for which the cancellation point must be
1940  /// emitted.
1941  ///
1942  void emitCancellationPointCall(CodeGenFunction &CGF, SourceLocation Loc,
1943  OpenMPDirectiveKind CancelRegion) override;
1944 
1945  /// Emit code for 'cancel' construct.
1946  /// \param IfCond Condition in the associated 'if' clause, if it was
1947  /// specified, nullptr otherwise.
1948  /// \param CancelRegion Region kind for which the cancel must be emitted.
1949  ///
1950  void emitCancelCall(CodeGenFunction &CGF, SourceLocation Loc,
1951  const Expr *IfCond,
1952  OpenMPDirectiveKind CancelRegion) override;
1953 
1954  /// Emit outilined function for 'target' directive.
1955  /// \param D Directive to emit.
1956  /// \param ParentName Name of the function that encloses the target region.
1957  /// \param OutlinedFn Outlined function value to be defined by this call.
1958  /// \param OutlinedFnID Outlined function ID value to be defined by this call.
1959  /// \param IsOffloadEntry True if the outlined function is an offload entry.
1960  /// \param CodeGen Code generation sequence for the \a D directive.
1961  /// An outlined function may not be an entry if, e.g. the if clause always
1962  /// evaluates to false.
1964  StringRef ParentName,
1965  llvm::Function *&OutlinedFn,
1966  llvm::Constant *&OutlinedFnID,
1967  bool IsOffloadEntry,
1968  const RegionCodeGenTy &CodeGen) override;
1969 
1970  /// Emit the target offloading code associated with \a D. The emitted
1971  /// code attempts offloading the execution to the device, an the event of
1972  /// a failure it executes the host version outlined in \a OutlinedFn.
1973  /// \param D Directive to emit.
1974  /// \param OutlinedFn Host version of the code to be offloaded.
1975  /// \param OutlinedFnID ID of host version of the code to be offloaded.
1976  /// \param IfCond Expression evaluated in if clause associated with the target
1977  /// directive, or null if no if clause is used.
1978  /// \param Device Expression evaluated in device clause associated with the
1979  /// target directive, or null if no device clause is used.
1980  void emitTargetCall(CodeGenFunction &CGF, const OMPExecutableDirective &D,
1981  llvm::Value *OutlinedFn, llvm::Value *OutlinedFnID,
1982  const Expr *IfCond, const Expr *Device) override;
1983 
1984  /// Emit the target regions enclosed in \a GD function definition or
1985  /// the function itself in case it is a valid device function. Returns true if
1986  /// \a GD was dealt with successfully.
1987  /// \param GD Function to scan.
1988  bool emitTargetFunctions(GlobalDecl GD) override;
1989 
1990  /// Emit the global variable if it is a valid device global variable.
1991  /// Returns true if \a GD was dealt with successfully.
1992  /// \param GD Variable declaration to emit.
1993  bool emitTargetGlobalVariable(GlobalDecl GD) override;
1994 
1995  /// Emit the global \a GD if it is meaningful for the target. Returns
1996  /// if it was emitted successfully.
1997  /// \param GD Global to scan.
1998  bool emitTargetGlobal(GlobalDecl GD) override;
1999 
2000  /// Creates the offloading descriptor in the event any target region
2001  /// was emitted in the current module and return the function that registers
2002  /// it.
2003  llvm::Function *emitRegistrationFunction() override;
2004 
2005  /// Emits code for teams call of the \a OutlinedFn with
2006  /// variables captured in a record which address is stored in \a
2007  /// CapturedStruct.
2008  /// \param OutlinedFn Outlined function to be run by team masters. Type of
2009  /// this function is void(*)(kmp_int32 *, kmp_int32, struct context_vars*).
2010  /// \param CapturedVars A pointer to the record with the references to
2011  /// variables used in \a OutlinedFn function.
2012  ///
2013  void emitTeamsCall(CodeGenFunction &CGF, const OMPExecutableDirective &D,
2014  SourceLocation Loc, llvm::Value *OutlinedFn,
2015  ArrayRef<llvm::Value *> CapturedVars) override;
2016 
2017  /// Emits call to void __kmpc_push_num_teams(ident_t *loc, kmp_int32
2018  /// global_tid, kmp_int32 num_teams, kmp_int32 thread_limit) to generate code
2019  /// for num_teams clause.
2020  /// \param NumTeams An integer expression of teams.
2021  /// \param ThreadLimit An integer expression of threads.
2022  void emitNumTeamsClause(CodeGenFunction &CGF, const Expr *NumTeams,
2023  const Expr *ThreadLimit, SourceLocation Loc) override;
2024 
2025  /// Emit the target data mapping code associated with \a D.
2026  /// \param D Directive to emit.
2027  /// \param IfCond Expression evaluated in if clause associated with the
2028  /// target directive, or null if no device clause is used.
2029  /// \param Device Expression evaluated in device clause associated with the
2030  /// target directive, or null if no device clause is used.
2031  /// \param Info A record used to store information that needs to be preserved
2032  /// until the region is closed.
2033  void emitTargetDataCalls(CodeGenFunction &CGF,
2034  const OMPExecutableDirective &D, const Expr *IfCond,
2035  const Expr *Device, const RegionCodeGenTy &CodeGen,
2036  TargetDataInfo &Info) override;
2037 
2038  /// Emit the data mapping/movement code associated with the directive
2039  /// \a D that should be of the form 'target [{enter|exit} data | update]'.
2040  /// \param D Directive to emit.
2041  /// \param IfCond Expression evaluated in if clause associated with the target
2042  /// directive, or null if no if clause is used.
2043  /// \param Device Expression evaluated in device clause associated with the
2044  /// target directive, or null if no device clause is used.
2045  void emitTargetDataStandAloneCall(CodeGenFunction &CGF,
2046  const OMPExecutableDirective &D,
2047  const Expr *IfCond,
2048  const Expr *Device) override;
2049 
2050  /// Emit initialization for doacross loop nesting support.
2051  /// \param D Loop-based construct used in doacross nesting construct.
2052  void emitDoacrossInit(CodeGenFunction &CGF,
2053  const OMPLoopDirective &D) override;
2054 
2055  /// Emit code for doacross ordered directive with 'depend' clause.
2056  /// \param C 'depend' clause with 'sink|source' dependency kind.
2057  void emitDoacrossOrdered(CodeGenFunction &CGF,
2058  const OMPDependClause *C) override;
2059 
2060  /// Translates the native parameter of outlined function if this is required
2061  /// for target.
2062  /// \param FD Field decl from captured record for the parameter.
2063  /// \param NativeParam Parameter itself.
2064  const VarDecl *translateParameter(const FieldDecl *FD,
2065  const VarDecl *NativeParam) const override;
2066 
2067  /// Gets the address of the native argument basing on the address of the
2068  /// target-specific parameter.
2069  /// \param NativeParam Parameter itself.
2070  /// \param TargetParam Corresponding target-specific parameter.
2071  Address getParameterAddress(CodeGenFunction &CGF, const VarDecl *NativeParam,
2072  const VarDecl *TargetParam) const override;
2073 };
2074 
2075 } // namespace CodeGen
2076 } // namespace clang
2077 
2078 #endif
const internal::VariadicAllOfMatcher< Type > type
Matches Types in the clang AST.
llvm::function_ref< void(unsigned, unsigned, StringRef, unsigned, const OffloadEntryInfoTargetRegion &)> OffloadTargetRegionEntryInfoActTy
brief Applies action Action on all registered entries.
Represents a function declaration or definition.
Definition: Decl.h:1714
OMPTargetRegionEntryKind
Kind of the target registry entry.
virtual Address getAddressOfLocalVariable(CodeGenFunction &CGF, const VarDecl *VD)
Gets the OpenMP-specific address of the local variable.
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:654
Allows to disable automatic handling of functions used in target regions as those marked as omp decla...
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:30
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
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:86
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()
bool markAsGlobalTarget(GlobalDecl GD)
Marks the declaration as alread emitted for the device code and returns true, if it was marked alread...
struct with the values to be passed to the dispatch runtime function
OffloadEntryInfoDeviceGlobalVar(unsigned Order, OMPTargetGlobalVarEntryKind Flags)
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)
Linkage
Describes the different kinds of linkage (C++ [basic.link], C99 6.2.2) that an entity may have...
Definition: Linkage.h:24
void createOffloadEntriesAndInfoMetadata()
Creates all the offload entries in the current compilation unit along with the associated metadata...
std::string getName(ArrayRef< StringRef > Parts) const
Get the platform-specific name separator.
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:3548
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:2521
Definition: Format.h:2022
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)...
OMPTargetGlobalVarEntryKind
Kind of the global variable entry..
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.
CharUnits - This is an opaque type for sizes expressed in character units.
Definition: CharUnits.h:38
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.
virtual void emitFunctionProlog(CodeGenFunction &CGF, const Decl *D)
Emits OpenMP-specific function prolog.
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
llvm::SmallDenseSet< const FunctionDecl * > AlreadyEmittedTargetFunctions
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 getAddrOfDeclareTargetLink(const VarDecl *VD)
Returns the address of the variable marked as declare target with link clause.
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.
OffloadEntryInfo(OffloadingEntryInfoKinds Kind, unsigned Order, uint32_t Flags)
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.
CGOpenMPRuntime(CodeGenModule &CGM)
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.
*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.
virtual void registerTargetGlobalVariable(const VarDecl *VD, llvm::Constant *Addr)
Checks if the provided global decl GD is a declare target variable and registers it when emitting cod...
Kind
Encodes a location in the source.
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...
bool hasDeviceGlobalVarEntryInfo(StringRef VarName) const
Checks if the variable with the given name has been registered already.
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.
OffloadEntryInfoDeviceGlobalVar(unsigned Order, llvm::Constant *Addr, CharUnits VarSize, OMPTargetGlobalVarEntryKind Flags, llvm::GlobalValue::LinkageTypes Linkage)
virtual void functionFinished(CodeGenFunction &CGF)
Cleans up references to the objects in finished function.
const Expr * getRefExpr(unsigned N) const
Returns the base declaration of the reduction item.
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)
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...
OffloadEntryInfoTargetRegion(unsigned Order, llvm::Constant *Addr, llvm::Constant *ID, OMPTargetRegionEntryKind Flags)
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 bool emitDeclareTargetVarDefinition(const VarDecl *VD, llvm::GlobalVariable *Addr, bool PerformInit)
Emit a code for initialization of declare target variable.
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::function_ref< void(StringRef, const OffloadEntryInfoDeviceGlobalVar &)> OffloadDeviceGlobalVarEntryInfoActTy
Applies action Action on all registered entries.
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:141
llvm::SmallPtrSet< const VarDecl *, 4 > DeclareTargetWithDefinition
Set of declare target variables with the generated initializer.
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:972
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.