clang  10.0.0git
CGOpenMPRuntime.h
Go to the documentation of this file.
1 //===----- CGOpenMPRuntime.h - Interface to OpenMP Runtimes -----*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This provides a class for OpenMP runtime code generation.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef LLVM_CLANG_LIB_CODEGEN_CGOPENMPRUNTIME_H
14 #define LLVM_CLANG_LIB_CODEGEN_CGOPENMPRUNTIME_H
15 
16 #include "CGValue.h"
17 #include "clang/AST/DeclOpenMP.h"
18 #include "clang/AST/GlobalDecl.h"
19 #include "clang/AST/Type.h"
22 #include "llvm/ADT/DenseMap.h"
23 #include "llvm/ADT/SmallPtrSet.h"
24 #include "llvm/ADT/StringMap.h"
25 #include "llvm/ADT/StringSet.h"
26 #include "llvm/Frontend/OpenMP/OMPConstants.h"
27 #include "llvm/IR/Function.h"
28 #include "llvm/IR/ValueHandle.h"
29 
30 namespace llvm {
31 class ArrayType;
32 class Constant;
33 class FunctionType;
34 class GlobalVariable;
35 class StructType;
36 class Type;
37 class Value;
38 } // namespace llvm
39 
40 namespace clang {
41 class Expr;
42 class OMPDependClause;
43 class OMPExecutableDirective;
44 class OMPLoopDirective;
45 class VarDecl;
46 class OMPDeclareReductionDecl;
47 class IdentifierInfo;
48 
49 namespace CodeGen {
50 class Address;
51 class CodeGenFunction;
52 class CodeGenModule;
53 
54 /// A basic class for pre|post-action for advanced codegen sequence for OpenMP
55 /// region.
57 public:
58  explicit PrePostActionTy() {}
59  virtual void Enter(CodeGenFunction &CGF) {}
60  virtual void Exit(CodeGenFunction &CGF) {}
61  virtual ~PrePostActionTy() {}
62 };
63 
64 /// Class provides a way to call simple version of codegen for OpenMP region, or
65 /// an advanced with possible pre|post-actions in codegen.
66 class RegionCodeGenTy final {
67  intptr_t CodeGen;
68  typedef void (*CodeGenTy)(intptr_t, CodeGenFunction &, PrePostActionTy &);
69  CodeGenTy Callback;
70  mutable PrePostActionTy *PrePostAction;
71  RegionCodeGenTy() = delete;
72  RegionCodeGenTy &operator=(const RegionCodeGenTy &) = delete;
73  template <typename Callable>
74  static void CallbackFn(intptr_t CodeGen, CodeGenFunction &CGF,
75  PrePostActionTy &Action) {
76  return (*reinterpret_cast<Callable *>(CodeGen))(CGF, Action);
77  }
78 
79 public:
80  template <typename Callable>
82  Callable &&CodeGen,
83  typename std::enable_if<
84  !std::is_same<typename std::remove_reference<Callable>::type,
85  RegionCodeGenTy>::value>::type * = nullptr)
86  : CodeGen(reinterpret_cast<intptr_t>(&CodeGen)),
87  Callback(CallbackFn<typename std::remove_reference<Callable>::type>),
88  PrePostAction(nullptr) {}
89  void setAction(PrePostActionTy &Action) const { PrePostAction = &Action; }
90  void operator()(CodeGenFunction &CGF) const;
91 };
92 
93 struct OMPTaskDataTy final {
105  llvm::PointerIntPair<llvm::Value *, 1, bool> Final;
106  llvm::PointerIntPair<llvm::Value *, 1, bool> Schedule;
107  llvm::PointerIntPair<llvm::Value *, 1, bool> Priority;
108  llvm::Value *Reductions = nullptr;
109  unsigned NumberOfParts = 0;
110  bool Tied = true;
111  bool Nogroup = false;
112 };
113 
114 /// Class intended to support codegen of all kind of the reduction clauses.
116 private:
117  /// Data required for codegen of reduction clauses.
118  struct ReductionData {
119  /// Reference to the original shared item.
120  const Expr *Ref = nullptr;
121  /// Helper expression for generation of private copy.
122  const Expr *Private = nullptr;
123  /// Helper expression for generation reduction operation.
124  const Expr *ReductionOp = nullptr;
125  ReductionData(const Expr *Ref, const Expr *Private, const Expr *ReductionOp)
126  : Ref(Ref), Private(Private), ReductionOp(ReductionOp) {}
127  };
128  /// List of reduction-based clauses.
129  SmallVector<ReductionData, 4> ClausesData;
130 
131  /// List of addresses of original shared variables/expressions.
132  SmallVector<std::pair<LValue, LValue>, 4> SharedAddresses;
133  /// Sizes of the reduction items in chars.
135  /// Base declarations for the reduction items.
137 
138  /// Emits lvalue for shared expression.
139  LValue emitSharedLValue(CodeGenFunction &CGF, const Expr *E);
140  /// Emits upper bound for shared expression (if array section).
141  LValue emitSharedLValueUB(CodeGenFunction &CGF, const Expr *E);
142  /// Performs aggregate initialization.
143  /// \param N Number of reduction item in the common list.
144  /// \param PrivateAddr Address of the corresponding private item.
145  /// \param SharedLVal Address of the original shared variable.
146  /// \param DRD Declare reduction construct used for reduction item.
147  void emitAggregateInitialization(CodeGenFunction &CGF, unsigned N,
148  Address PrivateAddr, LValue SharedLVal,
149  const OMPDeclareReductionDecl *DRD);
150 
151 public:
154  ArrayRef<const Expr *> ReductionOps);
155  /// Emits lvalue for a reduction item.
156  /// \param N Number of the reduction item.
157  void emitSharedLValue(CodeGenFunction &CGF, unsigned N);
158  /// Emits the code for the variable-modified type, if required.
159  /// \param N Number of the reduction item.
160  void emitAggregateType(CodeGenFunction &CGF, unsigned N);
161  /// Emits the code for the variable-modified type, if required.
162  /// \param N Number of the reduction item.
163  /// \param Size Size of the type in chars.
164  void emitAggregateType(CodeGenFunction &CGF, unsigned N, llvm::Value *Size);
165  /// Performs initialization of the private copy for the reduction item.
166  /// \param N Number of the reduction item.
167  /// \param PrivateAddr Address of the corresponding private item.
168  /// \param DefaultInit Default initialization sequence that should be
169  /// performed if no reduction specific initialization is found.
170  /// \param SharedLVal Address of the original shared variable.
171  void
172  emitInitialization(CodeGenFunction &CGF, unsigned N, Address PrivateAddr,
173  LValue SharedLVal,
174  llvm::function_ref<bool(CodeGenFunction &)> DefaultInit);
175  /// Returns true if the private copy requires cleanups.
176  bool needCleanups(unsigned N);
177  /// Emits cleanup code for the reduction item.
178  /// \param N Number of the reduction item.
179  /// \param PrivateAddr Address of the corresponding private item.
180  void emitCleanups(CodeGenFunction &CGF, unsigned N, Address PrivateAddr);
181  /// Adjusts \p PrivatedAddr for using instead of the original variable
182  /// address in normal operations.
183  /// \param N Number of the reduction item.
184  /// \param PrivateAddr Address of the corresponding private item.
185  Address adjustPrivateAddress(CodeGenFunction &CGF, unsigned N,
186  Address PrivateAddr);
187  /// Returns LValue for the reduction item.
188  LValue getSharedLValue(unsigned N) const { return SharedAddresses[N].first; }
189  /// Returns the size of the reduction item (in chars and total number of
190  /// elements in the item), or nullptr, if the size is a constant.
191  std::pair<llvm::Value *, llvm::Value *> getSizes(unsigned N) const {
192  return Sizes[N];
193  }
194  /// Returns the base declaration of the reduction item.
195  const VarDecl *getBaseDecl(unsigned N) const { return BaseDecls[N]; }
196  /// Returns the base declaration of the reduction item.
197  const Expr *getRefExpr(unsigned N) const { return ClausesData[N].Ref; }
198  /// Returns true if the initialization of the reduction item uses initializer
199  /// from declare reduction construct.
200  bool usesReductionInitializer(unsigned N) const;
201 };
202 
204 public:
205  /// Allows to disable automatic handling of functions used in target regions
206  /// as those marked as `omp declare target`.
208  CodeGenModule &CGM;
209  bool SavedShouldMarkAsGlobal;
210 
211  public:
214  };
215 
216  /// Manages list of nontemporal decls for the specified directive.
218  CodeGenModule &CGM;
219  const bool NeedToPush;
220 
221  public:
224  };
225 
226  /// Maps the expression for the lastprivate variable to the global copy used
227  /// to store new value because original variables are not mapped in inner
228  /// parallel regions. Only private copies are captured but we need also to
229  /// store private copy in shared address.
230  /// Also, stores the expression for the private loop counter and it
231  /// threaprivate name.
233  llvm::SmallDenseMap<CanonicalDeclPtr<const Decl>, SmallString<16>>
237  /// True if original lvalue for loop counter can be used in codegen (simd
238  /// region or simd only mode) and no need to create threadprivate
239  /// references.
240  bool UseOriginalIV = false;
241  };
242  /// Manages list of lastprivate conditional decls for the specified directive.
244  CodeGenModule &CGM;
245  const bool NeedToPush;
246 
247  public:
249  const OMPExecutableDirective &S, LValue IVLVal);
251  };
252 
253 protected:
255  StringRef FirstSeparator, Separator;
256 
257  /// Constructor allowing to redefine the name separator for the variables.
258  explicit CGOpenMPRuntime(CodeGenModule &CGM, StringRef FirstSeparator,
259  StringRef Separator);
260 
261  /// Creates offloading entry for the provided entry ID \a ID,
262  /// address \a Addr, size \a Size, and flags \a Flags.
263  virtual void createOffloadEntry(llvm::Constant *ID, llvm::Constant *Addr,
264  uint64_t Size, int32_t Flags,
265  llvm::GlobalValue::LinkageTypes Linkage);
266 
267  /// Helper to emit outlined function for 'target' directive.
268  /// \param D Directive to emit.
269  /// \param ParentName Name of the function that encloses the target region.
270  /// \param OutlinedFn Outlined function value to be defined by this call.
271  /// \param OutlinedFnID Outlined function ID value to be defined by this call.
272  /// \param IsOffloadEntry True if the outlined function is an offload entry.
273  /// \param CodeGen Lambda codegen specific to an accelerator device.
274  /// An outlined function may not be an entry if, e.g. the if clause always
275  /// evaluates to false.
276  virtual void emitTargetOutlinedFunctionHelper(const OMPExecutableDirective &D,
277  StringRef ParentName,
278  llvm::Function *&OutlinedFn,
279  llvm::Constant *&OutlinedFnID,
280  bool IsOffloadEntry,
281  const RegionCodeGenTy &CodeGen);
282 
283  /// Emits object of ident_t type with info for source location.
284  /// \param Flags Flags for OpenMP location.
285  ///
286  llvm::Value *emitUpdateLocation(CodeGenFunction &CGF, SourceLocation Loc,
287  unsigned Flags = 0);
288 
289  /// Returns pointer to ident_t type.
290  llvm::Type *getIdentTyPointerTy();
291 
292  /// Gets thread id value for the current thread.
293  ///
294  llvm::Value *getThreadID(CodeGenFunction &CGF, SourceLocation Loc);
295 
296  /// Get the function name of an outlined region.
297  // The name can be customized depending on the target.
298  //
299  virtual StringRef getOutlinedHelperName() const { return ".omp_outlined."; }
300 
301  /// Emits \p Callee function call with arguments \p Args with location \p Loc.
302  void emitCall(CodeGenFunction &CGF, SourceLocation Loc,
303  llvm::FunctionCallee Callee,
304  ArrayRef<llvm::Value *> Args = llvm::None) const;
305 
306  /// Emits address of the word in a memory where current thread id is
307  /// stored.
308  virtual Address emitThreadIDAddress(CodeGenFunction &CGF, SourceLocation Loc);
309 
310  void setLocThreadIdInsertPt(CodeGenFunction &CGF,
311  bool AtCurrentPoint = false);
312  void clearLocThreadIdInsertPt(CodeGenFunction &CGF);
313 
314  /// Check if the default location must be constant.
315  /// Default is false to support OMPT/OMPD.
316  virtual bool isDefaultLocationConstant() const { return false; }
317 
318  /// Returns additional flags that can be stored in reserved_2 field of the
319  /// default location.
320  virtual unsigned getDefaultLocationReserved2Flags() const { return 0; }
321 
322  /// Tries to emit declare variant function for \p OldGD from \p NewGD.
323  /// \param OrigAddr LLVM IR value for \p OldGD.
324  /// \param IsForDefinition true, if requested emission for the definition of
325  /// \p OldGD.
326  /// \returns true, was able to emit a definition function for \p OldGD, which
327  /// points to \p NewGD.
328  virtual bool tryEmitDeclareVariant(const GlobalDecl &NewGD,
329  const GlobalDecl &OldGD,
330  llvm::GlobalValue *OrigAddr,
331  bool IsForDefinition);
332 
333  /// Returns default flags for the barriers depending on the directive, for
334  /// which this barier is going to be emitted.
335  static unsigned getDefaultFlagsForBarriers(OpenMPDirectiveKind Kind);
336 
337  /// Get the LLVM type for the critical name.
338  llvm::ArrayType *getKmpCriticalNameTy() const {return KmpCriticalNameTy;}
339 
340  /// Returns corresponding lock object for the specified critical region
341  /// name. If the lock object does not exist it is created, otherwise the
342  /// reference to the existing copy is returned.
343  /// \param CriticalName Name of the critical region.
344  ///
345  llvm::Value *getCriticalRegionLock(StringRef CriticalName);
346 
347 private:
348  /// Default const ident_t object used for initialization of all other
349  /// ident_t objects.
350  llvm::Constant *DefaultOpenMPPSource = nullptr;
351  using FlagsTy = std::pair<unsigned, unsigned>;
352  /// Map of flags and corresponding default locations.
353  using OpenMPDefaultLocMapTy = llvm::DenseMap<FlagsTy, llvm::Value *>;
354  OpenMPDefaultLocMapTy OpenMPDefaultLocMap;
355  Address getOrCreateDefaultLocation(unsigned Flags);
356 
357  QualType IdentQTy;
358  llvm::StructType *IdentTy = nullptr;
359  /// Map for SourceLocation and OpenMP runtime library debug locations.
360  typedef llvm::DenseMap<unsigned, llvm::Value *> OpenMPDebugLocMapTy;
361  OpenMPDebugLocMapTy OpenMPDebugLocMap;
362  /// The type for a microtask which gets passed to __kmpc_fork_call().
363  /// Original representation is:
364  /// typedef void (kmpc_micro)(kmp_int32 global_tid, kmp_int32 bound_tid,...);
365  llvm::FunctionType *Kmpc_MicroTy = nullptr;
366  /// Stores debug location and ThreadID for the function.
367  struct DebugLocThreadIdTy {
368  llvm::Value *DebugLoc;
369  llvm::Value *ThreadID;
370  /// Insert point for the service instructions.
371  llvm::AssertingVH<llvm::Instruction> ServiceInsertPt = nullptr;
372  };
373  /// Map of local debug location, ThreadId and functions.
374  typedef llvm::DenseMap<llvm::Function *, DebugLocThreadIdTy>
375  OpenMPLocThreadIDMapTy;
376  OpenMPLocThreadIDMapTy OpenMPLocThreadIDMap;
377  /// Map of UDRs and corresponding combiner/initializer.
378  typedef llvm::DenseMap<const OMPDeclareReductionDecl *,
379  std::pair<llvm::Function *, llvm::Function *>>
380  UDRMapTy;
381  UDRMapTy UDRMap;
382  /// Map of functions and locally defined UDRs.
383  typedef llvm::DenseMap<llvm::Function *,
385  FunctionUDRMapTy;
386  FunctionUDRMapTy FunctionUDRMap;
387  /// Map from the user-defined mapper declaration to its corresponding
388  /// functions.
389  llvm::DenseMap<const OMPDeclareMapperDecl *, llvm::Function *> UDMMap;
390  /// Map of functions and their local user-defined mappers.
391  using FunctionUDMMapTy =
392  llvm::DenseMap<llvm::Function *,
394  FunctionUDMMapTy FunctionUDMMap;
395  /// Type kmp_critical_name, originally defined as typedef kmp_int32
396  /// kmp_critical_name[8];
397  llvm::ArrayType *KmpCriticalNameTy;
398  /// An ordered map of auto-generated variables to their unique names.
399  /// It stores variables with the following names: 1) ".gomp_critical_user_" +
400  /// <critical_section_name> + ".var" for "omp critical" directives; 2)
401  /// <mangled_name_for_global_var> + ".cache." for cache for threadprivate
402  /// variables.
403  llvm::StringMap<llvm::AssertingVH<llvm::Constant>, llvm::BumpPtrAllocator>
404  InternalVars;
405  /// Type typedef kmp_int32 (* kmp_routine_entry_t)(kmp_int32, void *);
406  llvm::Type *KmpRoutineEntryPtrTy = nullptr;
407  QualType KmpRoutineEntryPtrQTy;
408  /// Type typedef struct kmp_task {
409  /// void * shareds; /**< pointer to block of pointers to
410  /// shared vars */
411  /// kmp_routine_entry_t routine; /**< pointer to routine to call for
412  /// executing task */
413  /// kmp_int32 part_id; /**< part id for the task */
414  /// kmp_routine_entry_t destructors; /* pointer to function to invoke
415  /// deconstructors of firstprivate C++ objects */
416  /// } kmp_task_t;
418  /// Saved kmp_task_t for task directive.
420  /// Saved kmp_task_t for taskloop-based directive.
422  /// Type typedef struct kmp_depend_info {
423  /// kmp_intptr_t base_addr;
424  /// size_t len;
425  /// struct {
426  /// bool in:1;
427  /// bool out:1;
428  /// } flags;
429  /// } kmp_depend_info_t;
431  /// struct kmp_dim { // loop bounds info casted to kmp_int64
432  /// kmp_int64 lo; // lower
433  /// kmp_int64 up; // upper
434  /// kmp_int64 st; // stride
435  /// };
437  /// Type struct __tgt_offload_entry{
438  /// void *addr; // Pointer to the offload entry info.
439  /// // (function or global)
440  /// char *name; // Name of the function or global.
441  /// size_t size; // Size of the entry info (0 if it a function).
442  /// int32_t flags;
443  /// int32_t reserved;
444  /// };
446  /// Entity that registers the offloading constants that were emitted so
447  /// far.
449  CodeGenModule &CGM;
450 
451  /// Number of entries registered so far.
452  unsigned OffloadingEntriesNum = 0;
453 
454  public:
455  /// Base class of the entries info.
457  public:
458  /// Kind of a given entry.
459  enum OffloadingEntryInfoKinds : unsigned {
460  /// Entry is a target region.
461  OffloadingEntryInfoTargetRegion = 0,
462  /// Entry is a declare target variable.
463  OffloadingEntryInfoDeviceGlobalVar = 1,
464  /// Invalid entry info.
465  OffloadingEntryInfoInvalid = ~0u
466  };
467 
468  protected:
469  OffloadEntryInfo() = delete;
470  explicit OffloadEntryInfo(OffloadingEntryInfoKinds Kind) : Kind(Kind) {}
471  explicit OffloadEntryInfo(OffloadingEntryInfoKinds Kind, unsigned Order,
472  uint32_t Flags)
473  : Flags(Flags), Order(Order), Kind(Kind) {}
474  ~OffloadEntryInfo() = default;
475 
476  public:
477  bool isValid() const { return Order != ~0u; }
478  unsigned getOrder() const { return Order; }
480  uint32_t getFlags() const { return Flags; }
481  void setFlags(uint32_t NewFlags) { Flags = NewFlags; }
482  llvm::Constant *getAddress() const {
483  return cast_or_null<llvm::Constant>(Addr);
484  }
485  void setAddress(llvm::Constant *V) {
486  assert(!Addr.pointsToAliveValue() && "Address has been set before!");
487  Addr = V;
488  }
489  static bool classof(const OffloadEntryInfo *Info) { return true; }
490 
491  private:
492  /// Address of the entity that has to be mapped for offloading.
493  llvm::WeakTrackingVH Addr;
494 
495  /// Flags associated with the device global.
496  uint32_t Flags = 0u;
497 
498  /// Order this entry was emitted.
499  unsigned Order = ~0u;
500 
501  OffloadingEntryInfoKinds Kind = OffloadingEntryInfoInvalid;
502  };
503 
504  /// Return true if a there are no entries defined.
505  bool empty() const;
506  /// Return number of entries defined so far.
507  unsigned size() const { return OffloadingEntriesNum; }
509 
510  //
511  // Target region entries related.
512  //
513 
514  /// Kind of the target registry entry.
515  enum OMPTargetRegionEntryKind : uint32_t {
516  /// Mark the entry as target region.
517  OMPTargetRegionEntryTargetRegion = 0x0,
518  /// Mark the entry as a global constructor.
519  OMPTargetRegionEntryCtor = 0x02,
520  /// Mark the entry as a global destructor.
521  OMPTargetRegionEntryDtor = 0x04,
522  };
523 
524  /// Target region entries info.
526  /// Address that can be used as the ID of the entry.
527  llvm::Constant *ID = nullptr;
528 
529  public:
531  : OffloadEntryInfo(OffloadingEntryInfoTargetRegion) {}
532  explicit OffloadEntryInfoTargetRegion(unsigned Order,
533  llvm::Constant *Addr,
534  llvm::Constant *ID,
536  : OffloadEntryInfo(OffloadingEntryInfoTargetRegion, Order, Flags),
537  ID(ID) {
538  setAddress(Addr);
539  }
540 
541  llvm::Constant *getID() const { return ID; }
542  void setID(llvm::Constant *V) {
543  assert(!ID && "ID has been set before!");
544  ID = V;
545  }
546  static bool classof(const OffloadEntryInfo *Info) {
547  return Info->getKind() == OffloadingEntryInfoTargetRegion;
548  }
549  };
550 
551  /// Initialize target region entry.
552  void initializeTargetRegionEntryInfo(unsigned DeviceID, unsigned FileID,
553  StringRef ParentName, unsigned LineNum,
554  unsigned Order);
555  /// Register target region entry.
556  void registerTargetRegionEntryInfo(unsigned DeviceID, unsigned FileID,
557  StringRef ParentName, unsigned LineNum,
558  llvm::Constant *Addr, llvm::Constant *ID,
560  /// Return true if a target region entry with the provided information
561  /// exists.
562  bool hasTargetRegionEntryInfo(unsigned DeviceID, unsigned FileID,
563  StringRef ParentName, unsigned LineNum) const;
564  /// brief Applies action \a Action on all registered entries.
565  typedef llvm::function_ref<void(unsigned, unsigned, StringRef, unsigned,
568  void actOnTargetRegionEntriesInfo(
569  const OffloadTargetRegionEntryInfoActTy &Action);
570 
571  //
572  // Device global variable entries related.
573  //
574 
575  /// Kind of the global variable entry..
576  enum OMPTargetGlobalVarEntryKind : uint32_t {
577  /// Mark the entry as a to declare target.
578  OMPTargetGlobalVarEntryTo = 0x0,
579  /// Mark the entry as a to declare target link.
580  OMPTargetGlobalVarEntryLink = 0x1,
581  };
582 
583  /// Device global variable entries info.
585  /// Type of the global variable.
586  CharUnits VarSize;
587  llvm::GlobalValue::LinkageTypes Linkage;
588 
589  public:
591  : OffloadEntryInfo(OffloadingEntryInfoDeviceGlobalVar) {}
592  explicit OffloadEntryInfoDeviceGlobalVar(unsigned Order,
594  : OffloadEntryInfo(OffloadingEntryInfoDeviceGlobalVar, Order, Flags) {}
596  unsigned Order, llvm::Constant *Addr, CharUnits VarSize,
598  llvm::GlobalValue::LinkageTypes Linkage)
599  : OffloadEntryInfo(OffloadingEntryInfoDeviceGlobalVar, Order, Flags),
600  VarSize(VarSize), Linkage(Linkage) {
601  setAddress(Addr);
602  }
603 
604  CharUnits getVarSize() const { return VarSize; }
605  void setVarSize(CharUnits Size) { VarSize = Size; }
606  llvm::GlobalValue::LinkageTypes getLinkage() const { return Linkage; }
607  void setLinkage(llvm::GlobalValue::LinkageTypes LT) { Linkage = LT; }
608  static bool classof(const OffloadEntryInfo *Info) {
609  return Info->getKind() == OffloadingEntryInfoDeviceGlobalVar;
610  }
611  };
612 
613  /// Initialize device global variable entry.
614  void initializeDeviceGlobalVarEntryInfo(StringRef Name,
616  unsigned Order);
617 
618  /// Register device global variable entry.
619  void
620  registerDeviceGlobalVarEntryInfo(StringRef VarName, llvm::Constant *Addr,
621  CharUnits VarSize,
623  llvm::GlobalValue::LinkageTypes Linkage);
624  /// Checks if the variable with the given name has been registered already.
625  bool hasDeviceGlobalVarEntryInfo(StringRef VarName) const {
626  return OffloadEntriesDeviceGlobalVar.count(VarName) > 0;
627  }
628  /// Applies action \a Action on all registered entries.
629  typedef llvm::function_ref<void(StringRef,
632  void actOnDeviceGlobalVarEntriesInfo(
634 
635  private:
636  // Storage for target region entries kind. The storage is to be indexed by
637  // file ID, device ID, parent function name and line number.
638  typedef llvm::DenseMap<unsigned, OffloadEntryInfoTargetRegion>
639  OffloadEntriesTargetRegionPerLine;
640  typedef llvm::StringMap<OffloadEntriesTargetRegionPerLine>
641  OffloadEntriesTargetRegionPerParentName;
642  typedef llvm::DenseMap<unsigned, OffloadEntriesTargetRegionPerParentName>
643  OffloadEntriesTargetRegionPerFile;
644  typedef llvm::DenseMap<unsigned, OffloadEntriesTargetRegionPerFile>
645  OffloadEntriesTargetRegionPerDevice;
646  typedef OffloadEntriesTargetRegionPerDevice OffloadEntriesTargetRegionTy;
647  OffloadEntriesTargetRegionTy OffloadEntriesTargetRegion;
648  /// Storage for device global variable entries kind. The storage is to be
649  /// indexed by mangled name.
650  typedef llvm::StringMap<OffloadEntryInfoDeviceGlobalVar>
651  OffloadEntriesDeviceGlobalVarTy;
652  OffloadEntriesDeviceGlobalVarTy OffloadEntriesDeviceGlobalVar;
653  };
655 
656  bool ShouldMarkAsGlobal = true;
657  /// List of the emitted declarations.
659  /// List of the global variables with their addresses that should not be
660  /// emitted for the target.
661  llvm::StringMap<llvm::WeakTrackingVH> EmittedNonTargetVariables;
662 
663  /// List of variables that can become declare target implicitly and, thus,
664  /// must be emitted.
665  llvm::SmallDenseSet<const VarDecl *> DeferredGlobalVariables;
666 
667  /// Mapping of the original functions to their variants and original global
668  /// decl.
669  llvm::MapVector<CanonicalDeclPtr<const FunctionDecl>,
670  std::pair<GlobalDecl, GlobalDecl>>
672 
673  using NontemporalDeclsSet = llvm::SmallDenseSet<CanonicalDeclPtr<const Decl>>;
674  /// Stack for list of declarations in current context marked as nontemporal.
675  /// The set is the union of all current stack elements.
677 
678  /// Stack for list of addresses of declarations in current context marked as
679  /// lastprivate conditional. The set is the union of all current stack
680  /// elements.
682 
683  /// Flag for keeping track of weather a requires unified_shared_memory
684  /// directive is present.
686 
687  /// Flag for keeping track of weather a target region has been emitted.
689 
690  /// Flag for keeping track of weather a device routine has been emitted.
691  /// Device routines are specific to the
693 
694  /// Loads all the offload entries information from the host IR
695  /// metadata.
697 
698  /// Returns __tgt_offload_entry type.
700 
701  /// Start scanning from statement \a S and and emit all target regions
702  /// found along the way.
703  /// \param S Starting statement.
704  /// \param ParentName Name of the function declaration that is being scanned.
705  void scanForTargetRegionsFunctions(const Stmt *S, StringRef ParentName);
706 
707  /// Build type kmp_routine_entry_t (if not built yet).
708  void emitKmpRoutineEntryT(QualType KmpInt32Ty);
709 
710  /// Returns pointer to kmpc_micro type.
711  llvm::Type *getKmpc_MicroPointerTy();
712 
713  /// Returns specified OpenMP runtime function.
714  /// \param Function OpenMP runtime function.
715  /// \return Specified function.
716  llvm::FunctionCallee createRuntimeFunction(unsigned Function);
717 
718  /// Returns __kmpc_for_static_init_* runtime function for the specified
719  /// size \a IVSize and sign \a IVSigned.
720  llvm::FunctionCallee createForStaticInitFunction(unsigned IVSize,
721  bool IVSigned);
722 
723  /// Returns __kmpc_dispatch_init_* runtime function for the specified
724  /// size \a IVSize and sign \a IVSigned.
725  llvm::FunctionCallee createDispatchInitFunction(unsigned IVSize,
726  bool IVSigned);
727 
728  /// Returns __kmpc_dispatch_next_* runtime function for the specified
729  /// size \a IVSize and sign \a IVSigned.
730  llvm::FunctionCallee createDispatchNextFunction(unsigned IVSize,
731  bool IVSigned);
732 
733  /// Returns __kmpc_dispatch_fini_* runtime function for the specified
734  /// size \a IVSize and sign \a IVSigned.
735  llvm::FunctionCallee createDispatchFiniFunction(unsigned IVSize,
736  bool IVSigned);
737 
738  /// If the specified mangled name is not in the module, create and
739  /// return threadprivate cache object. This object is a pointer's worth of
740  /// storage that's reserved for use by the OpenMP runtime.
741  /// \param VD Threadprivate variable.
742  /// \return Cache variable for the specified threadprivate.
743  llvm::Constant *getOrCreateThreadPrivateCache(const VarDecl *VD);
744 
745  /// Gets (if variable with the given name already exist) or creates
746  /// internal global variable with the specified Name. The created variable has
747  /// linkage CommonLinkage by default and is initialized by null value.
748  /// \param Ty Type of the global variable. If it is exist already the type
749  /// must be the same.
750  /// \param Name Name of the variable.
751  llvm::Constant *getOrCreateInternalVariable(llvm::Type *Ty,
752  const llvm::Twine &Name,
753  unsigned AddressSpace = 0);
754 
755  /// Set of threadprivate variables with the generated initializer.
756  llvm::StringSet<> ThreadPrivateWithDefinition;
757 
758  /// Set of declare target variables with the generated initializer.
759  llvm::StringSet<> DeclareTargetWithDefinition;
760 
761  /// Emits initialization code for the threadprivate variables.
762  /// \param VDAddr Address of the global variable \a VD.
763  /// \param Ctor Pointer to a global init function for \a VD.
764  /// \param CopyCtor Pointer to a global copy function for \a VD.
765  /// \param Dtor Pointer to a global destructor function for \a VD.
766  /// \param Loc Location of threadprivate declaration.
768  llvm::Value *Ctor, llvm::Value *CopyCtor,
769  llvm::Value *Dtor, SourceLocation Loc);
770 
771  /// Emit the array initialization or deletion portion for user-defined mapper
772  /// code generation.
774  llvm::Value *Handle, llvm::Value *BasePtr,
775  llvm::Value *Ptr, llvm::Value *Size,
776  llvm::Value *MapType, CharUnits ElementSize,
777  llvm::BasicBlock *ExitBB, bool IsInit);
778 
779  struct TaskResultTy {
780  llvm::Value *NewTask = nullptr;
781  llvm::Function *TaskEntry = nullptr;
782  llvm::Value *NewTaskNewTaskTTy = nullptr;
784  const RecordDecl *KmpTaskTQTyRD = nullptr;
785  llvm::Value *TaskDupFn = nullptr;
786  };
787  /// Emit task region for the task directive. The task region is emitted in
788  /// several steps:
789  /// 1. Emit a call to kmp_task_t *__kmpc_omp_task_alloc(ident_t *, kmp_int32
790  /// gtid, kmp_int32 flags, size_t sizeof_kmp_task_t, size_t sizeof_shareds,
791  /// kmp_routine_entry_t *task_entry). Here task_entry is a pointer to the
792  /// function:
793  /// kmp_int32 .omp_task_entry.(kmp_int32 gtid, kmp_task_t *tt) {
794  /// TaskFunction(gtid, tt->part_id, tt->shareds);
795  /// return 0;
796  /// }
797  /// 2. Copy a list of shared variables to field shareds of the resulting
798  /// structure kmp_task_t returned by the previous call (if any).
799  /// 3. Copy a pointer to destructions function to field destructions of the
800  /// resulting structure kmp_task_t.
801  /// \param D Current task directive.
802  /// \param TaskFunction An LLVM function with type void (*)(i32 /*gtid*/, i32
803  /// /*part_id*/, captured_struct */*__context*/);
804  /// \param SharedsTy A type which contains references the shared variables.
805  /// \param Shareds Context with the list of shared variables from the \p
806  /// TaskFunction.
807  /// \param Data Additional data for task generation like tiednsee, final
808  /// state, list of privates etc.
810  const OMPExecutableDirective &D,
811  llvm::Function *TaskFunction, QualType SharedsTy,
812  Address Shareds, const OMPTaskDataTy &Data);
813 
814  /// Returns default address space for the constant firstprivates, 0 by
815  /// default.
816  virtual unsigned getDefaultFirstprivateAddressSpace() const { return 0; }
817 
818  /// Emit code that pushes the trip count of loops associated with constructs
819  /// 'target teams distribute' and 'teams distribute parallel for'.
820  /// \param SizeEmitter Emits the int64 value for the number of iterations of
821  /// the associated loop.
824  llvm::Value *DeviceID,
825  llvm::function_ref<llvm::Value *(CodeGenFunction &CGF,
826  const OMPLoopDirective &D)>
827  SizeEmitter);
828 
829 public:
831  : CGOpenMPRuntime(CGM, ".", ".") {}
832  virtual ~CGOpenMPRuntime() {}
833  virtual void clear();
834 
835  /// Emits code for OpenMP 'if' clause using specified \a CodeGen
836  /// function. Here is the logic:
837  /// if (Cond) {
838  /// ThenGen();
839  /// } else {
840  /// ElseGen();
841  /// }
842  void emitIfClause(CodeGenFunction &CGF, const Expr *Cond,
843  const RegionCodeGenTy &ThenGen,
844  const RegionCodeGenTy &ElseGen);
845 
846  /// Checks if the \p Body is the \a CompoundStmt and returns its child
847  /// statement iff there is only one that is not evaluatable at the compile
848  /// time.
849  static const Stmt *getSingleCompoundChild(ASTContext &Ctx, const Stmt *Body);
850 
851  /// Get the platform-specific name separator.
852  std::string getName(ArrayRef<StringRef> Parts) const;
853 
854  /// Emit code for the specified user defined reduction construct.
855  virtual void emitUserDefinedReduction(CodeGenFunction *CGF,
856  const OMPDeclareReductionDecl *D);
857  /// Get combiner/initializer for the specified user-defined reduction, if any.
858  virtual std::pair<llvm::Function *, llvm::Function *>
859  getUserDefinedReduction(const OMPDeclareReductionDecl *D);
860 
861  /// Emit the function for the user defined mapper construct.
863  CodeGenFunction *CGF = nullptr);
864 
865  /// Emits outlined function for the specified OpenMP parallel directive
866  /// \a D. This outlined function has type void(*)(kmp_int32 *ThreadID,
867  /// kmp_int32 BoundID, struct context_vars*).
868  /// \param D OpenMP directive.
869  /// \param ThreadIDVar Variable for thread id in the current OpenMP region.
870  /// \param InnermostKind Kind of innermost directive (for simple directives it
871  /// is a directive itself, for combined - its innermost directive).
872  /// \param CodeGen Code generation sequence for the \a D directive.
873  virtual llvm::Function *emitParallelOutlinedFunction(
874  const OMPExecutableDirective &D, const VarDecl *ThreadIDVar,
875  OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen);
876 
877  /// Emits outlined function for the specified OpenMP teams directive
878  /// \a D. This outlined function has type void(*)(kmp_int32 *ThreadID,
879  /// kmp_int32 BoundID, struct context_vars*).
880  /// \param D OpenMP directive.
881  /// \param ThreadIDVar Variable for thread id in the current OpenMP region.
882  /// \param InnermostKind Kind of innermost directive (for simple directives it
883  /// is a directive itself, for combined - its innermost directive).
884  /// \param CodeGen Code generation sequence for the \a D directive.
885  virtual llvm::Function *emitTeamsOutlinedFunction(
886  const OMPExecutableDirective &D, const VarDecl *ThreadIDVar,
887  OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen);
888 
889  /// Emits outlined function for the OpenMP task directive \a D. This
890  /// outlined function has type void(*)(kmp_int32 ThreadID, struct task_t*
891  /// TaskT).
892  /// \param D OpenMP directive.
893  /// \param ThreadIDVar Variable for thread id in the current OpenMP region.
894  /// \param PartIDVar Variable for partition id in the current OpenMP untied
895  /// task region.
896  /// \param TaskTVar Variable for task_t argument.
897  /// \param InnermostKind Kind of innermost directive (for simple directives it
898  /// is a directive itself, for combined - its innermost directive).
899  /// \param CodeGen Code generation sequence for the \a D directive.
900  /// \param Tied true if task is generated for tied task, false otherwise.
901  /// \param NumberOfParts Number of parts in untied task. Ignored for tied
902  /// tasks.
903  ///
904  virtual llvm::Function *emitTaskOutlinedFunction(
905  const OMPExecutableDirective &D, const VarDecl *ThreadIDVar,
906  const VarDecl *PartIDVar, const VarDecl *TaskTVar,
907  OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen,
908  bool Tied, unsigned &NumberOfParts);
909 
910  /// Cleans up references to the objects in finished function.
911  ///
912  virtual void functionFinished(CodeGenFunction &CGF);
913 
914  /// Emits code for parallel or serial call of the \a OutlinedFn with
915  /// variables captured in a record which address is stored in \a
916  /// CapturedStruct.
917  /// \param OutlinedFn Outlined function to be run in parallel threads. Type of
918  /// this function is void(*)(kmp_int32 *, kmp_int32, struct context_vars*).
919  /// \param CapturedVars A pointer to the record with the references to
920  /// variables used in \a OutlinedFn function.
921  /// \param IfCond Condition in the associated 'if' clause, if it was
922  /// specified, nullptr otherwise.
923  ///
924  virtual void emitParallelCall(CodeGenFunction &CGF, SourceLocation Loc,
925  llvm::Function *OutlinedFn,
926  ArrayRef<llvm::Value *> CapturedVars,
927  const Expr *IfCond);
928 
929  /// Emits a critical region.
930  /// \param CriticalName Name of the critical region.
931  /// \param CriticalOpGen Generator for the statement associated with the given
932  /// critical region.
933  /// \param Hint Value of the 'hint' clause (optional).
934  virtual void emitCriticalRegion(CodeGenFunction &CGF, StringRef CriticalName,
935  const RegionCodeGenTy &CriticalOpGen,
936  SourceLocation Loc,
937  const Expr *Hint = nullptr);
938 
939  /// Emits a master region.
940  /// \param MasterOpGen Generator for the statement associated with the given
941  /// master region.
942  virtual void emitMasterRegion(CodeGenFunction &CGF,
943  const RegionCodeGenTy &MasterOpGen,
944  SourceLocation Loc);
945 
946  /// Emits code for a taskyield directive.
947  virtual void emitTaskyieldCall(CodeGenFunction &CGF, SourceLocation Loc);
948 
949  /// Emit a taskgroup region.
950  /// \param TaskgroupOpGen Generator for the statement associated with the
951  /// given taskgroup region.
952  virtual void emitTaskgroupRegion(CodeGenFunction &CGF,
953  const RegionCodeGenTy &TaskgroupOpGen,
954  SourceLocation Loc);
955 
956  /// Emits a single region.
957  /// \param SingleOpGen Generator for the statement associated with the given
958  /// single region.
959  virtual void emitSingleRegion(CodeGenFunction &CGF,
960  const RegionCodeGenTy &SingleOpGen,
961  SourceLocation Loc,
962  ArrayRef<const Expr *> CopyprivateVars,
963  ArrayRef<const Expr *> DestExprs,
964  ArrayRef<const Expr *> SrcExprs,
965  ArrayRef<const Expr *> AssignmentOps);
966 
967  /// Emit an ordered region.
968  /// \param OrderedOpGen Generator for the statement associated with the given
969  /// ordered region.
970  virtual void emitOrderedRegion(CodeGenFunction &CGF,
971  const RegionCodeGenTy &OrderedOpGen,
972  SourceLocation Loc, bool IsThreads);
973 
974  /// Emit an implicit/explicit barrier for OpenMP threads.
975  /// \param Kind Directive for which this implicit barrier call must be
976  /// generated. Must be OMPD_barrier for explicit barrier generation.
977  /// \param EmitChecks true if need to emit checks for cancellation barriers.
978  /// \param ForceSimpleCall true simple barrier call must be emitted, false if
979  /// runtime class decides which one to emit (simple or with cancellation
980  /// checks).
981  ///
982  virtual void emitBarrierCall(CodeGenFunction &CGF, SourceLocation Loc,
983  OpenMPDirectiveKind Kind,
984  bool EmitChecks = true,
985  bool ForceSimpleCall = false);
986 
987  /// Check if the specified \a ScheduleKind is static non-chunked.
988  /// This kind of worksharing directive is emitted without outer loop.
989  /// \param ScheduleKind Schedule kind specified in the 'schedule' clause.
990  /// \param Chunked True if chunk is specified in the clause.
991  ///
992  virtual bool isStaticNonchunked(OpenMPScheduleClauseKind ScheduleKind,
993  bool Chunked) const;
994 
995  /// Check if the specified \a ScheduleKind is static non-chunked.
996  /// This kind of distribute directive is emitted without outer loop.
997  /// \param ScheduleKind Schedule kind specified in the 'dist_schedule' clause.
998  /// \param Chunked True if chunk is specified in the clause.
999  ///
1000  virtual bool isStaticNonchunked(OpenMPDistScheduleClauseKind ScheduleKind,
1001  bool Chunked) const;
1002 
1003  /// Check if the specified \a ScheduleKind is static chunked.
1004  /// \param ScheduleKind Schedule kind specified in the 'schedule' clause.
1005  /// \param Chunked True if chunk is specified in the clause.
1006  ///
1007  virtual bool isStaticChunked(OpenMPScheduleClauseKind ScheduleKind,
1008  bool Chunked) const;
1009 
1010  /// Check if the specified \a ScheduleKind is static non-chunked.
1011  /// \param ScheduleKind Schedule kind specified in the 'dist_schedule' clause.
1012  /// \param Chunked True if chunk is specified in the clause.
1013  ///
1014  virtual bool isStaticChunked(OpenMPDistScheduleClauseKind ScheduleKind,
1015  bool Chunked) const;
1016 
1017  /// Check if the specified \a ScheduleKind is dynamic.
1018  /// This kind of worksharing directive is emitted without outer loop.
1019  /// \param ScheduleKind Schedule Kind specified in the 'schedule' clause.
1020  ///
1021  virtual bool isDynamic(OpenMPScheduleClauseKind ScheduleKind) const;
1022 
1023  /// struct with the values to be passed to the dispatch runtime function
1025  /// Loop lower bound
1026  llvm::Value *LB = nullptr;
1027  /// Loop upper bound
1028  llvm::Value *UB = nullptr;
1029  /// Chunk size specified using 'schedule' clause (nullptr if chunk
1030  /// was not specified)
1031  llvm::Value *Chunk = nullptr;
1032  DispatchRTInput() = default;
1034  : LB(LB), UB(UB), Chunk(Chunk) {}
1035  };
1036 
1037  /// Call the appropriate runtime routine to initialize it before start
1038  /// of loop.
1039 
1040  /// This is used for non static scheduled types and when the ordered
1041  /// clause is present on the loop construct.
1042  /// Depending on the loop schedule, it is necessary to call some runtime
1043  /// routine before start of the OpenMP loop to get the loop upper / lower
1044  /// bounds \a LB and \a UB and stride \a ST.
1045  ///
1046  /// \param CGF Reference to current CodeGenFunction.
1047  /// \param Loc Clang source location.
1048  /// \param ScheduleKind Schedule kind, specified by the 'schedule' clause.
1049  /// \param IVSize Size of the iteration variable in bits.
1050  /// \param IVSigned Sign of the iteration variable.
1051  /// \param Ordered true if loop is ordered, false otherwise.
1052  /// \param DispatchValues struct containing llvm values for lower bound, upper
1053  /// bound, and chunk expression.
1054  /// For the default (nullptr) value, the chunk 1 will be used.
1055  ///
1056  virtual void emitForDispatchInit(CodeGenFunction &CGF, SourceLocation Loc,
1057  const OpenMPScheduleTy &ScheduleKind,
1058  unsigned IVSize, bool IVSigned, bool Ordered,
1059  const DispatchRTInput &DispatchValues);
1060 
1061  /// Struct with the values to be passed to the static runtime function
1062  struct StaticRTInput {
1063  /// Size of the iteration variable in bits.
1064  unsigned IVSize = 0;
1065  /// Sign of the iteration variable.
1066  bool IVSigned = false;
1067  /// true if loop is ordered, false otherwise.
1068  bool Ordered = false;
1069  /// Address of the output variable in which the flag of the last iteration
1070  /// is returned.
1071  Address IL = Address::invalid();
1072  /// Address of the output variable in which the lower iteration number is
1073  /// returned.
1074  Address LB = Address::invalid();
1075  /// Address of the output variable in which the upper iteration number is
1076  /// returned.
1077  Address UB = Address::invalid();
1078  /// Address of the output variable in which the stride value is returned
1079  /// necessary to generated the static_chunked scheduled loop.
1080  Address ST = Address::invalid();
1081  /// Value of the chunk for the static_chunked scheduled loop. For the
1082  /// default (nullptr) value, the chunk 1 will be used.
1083  llvm::Value *Chunk = nullptr;
1084  StaticRTInput(unsigned IVSize, bool IVSigned, bool Ordered, Address IL,
1085  Address LB, Address UB, Address ST,
1086  llvm::Value *Chunk = nullptr)
1087  : IVSize(IVSize), IVSigned(IVSigned), Ordered(Ordered), IL(IL), LB(LB),
1088  UB(UB), ST(ST), Chunk(Chunk) {}
1089  };
1090  /// Call the appropriate runtime routine to initialize it before start
1091  /// of loop.
1092  ///
1093  /// This is used only in case of static schedule, when the user did not
1094  /// specify a ordered clause on the loop construct.
1095  /// Depending on the loop schedule, it is necessary to call some runtime
1096  /// routine before start of the OpenMP loop to get the loop upper / lower
1097  /// bounds LB and UB and stride ST.
1098  ///
1099  /// \param CGF Reference to current CodeGenFunction.
1100  /// \param Loc Clang source location.
1101  /// \param DKind Kind of the directive.
1102  /// \param ScheduleKind Schedule kind, specified by the 'schedule' clause.
1103  /// \param Values Input arguments for the construct.
1104  ///
1105  virtual void emitForStaticInit(CodeGenFunction &CGF, SourceLocation Loc,
1106  OpenMPDirectiveKind DKind,
1107  const OpenMPScheduleTy &ScheduleKind,
1108  const StaticRTInput &Values);
1109 
1110  ///
1111  /// \param CGF Reference to current CodeGenFunction.
1112  /// \param Loc Clang source location.
1113  /// \param SchedKind Schedule kind, specified by the 'dist_schedule' clause.
1114  /// \param Values Input arguments for the construct.
1115  ///
1116  virtual void emitDistributeStaticInit(CodeGenFunction &CGF,
1117  SourceLocation Loc,
1118  OpenMPDistScheduleClauseKind SchedKind,
1119  const StaticRTInput &Values);
1120 
1121  /// Call the appropriate runtime routine to notify that we finished
1122  /// iteration of the ordered loop with the dynamic scheduling.
1123  ///
1124  /// \param CGF Reference to current CodeGenFunction.
1125  /// \param Loc Clang source location.
1126  /// \param IVSize Size of the iteration variable in bits.
1127  /// \param IVSigned Sign of the iteration variable.
1128  ///
1129  virtual void emitForOrderedIterationEnd(CodeGenFunction &CGF,
1130  SourceLocation Loc, unsigned IVSize,
1131  bool IVSigned);
1132 
1133  /// Call the appropriate runtime routine to notify that we finished
1134  /// all the work with current loop.
1135  ///
1136  /// \param CGF Reference to current CodeGenFunction.
1137  /// \param Loc Clang source location.
1138  /// \param DKind Kind of the directive for which the static finish is emitted.
1139  ///
1140  virtual void emitForStaticFinish(CodeGenFunction &CGF, SourceLocation Loc,
1141  OpenMPDirectiveKind DKind);
1142 
1143  /// Call __kmpc_dispatch_next(
1144  /// ident_t *loc, kmp_int32 tid, kmp_int32 *p_lastiter,
1145  /// kmp_int[32|64] *p_lower, kmp_int[32|64] *p_upper,
1146  /// kmp_int[32|64] *p_stride);
1147  /// \param IVSize Size of the iteration variable in bits.
1148  /// \param IVSigned Sign of the iteration variable.
1149  /// \param IL Address of the output variable in which the flag of the
1150  /// last iteration is returned.
1151  /// \param LB Address of the output variable in which the lower iteration
1152  /// number is returned.
1153  /// \param UB Address of the output variable in which the upper iteration
1154  /// number is returned.
1155  /// \param ST Address of the output variable in which the stride value is
1156  /// returned.
1158  unsigned IVSize, bool IVSigned,
1159  Address IL, Address LB,
1160  Address UB, Address ST);
1161 
1162  /// Emits call to void __kmpc_push_num_threads(ident_t *loc, kmp_int32
1163  /// global_tid, kmp_int32 num_threads) to generate code for 'num_threads'
1164  /// clause.
1165  /// \param NumThreads An integer value of threads.
1166  virtual void emitNumThreadsClause(CodeGenFunction &CGF,
1167  llvm::Value *NumThreads,
1168  SourceLocation Loc);
1169 
1170  /// Emit call to void __kmpc_push_proc_bind(ident_t *loc, kmp_int32
1171  /// global_tid, int proc_bind) to generate code for 'proc_bind' clause.
1172  virtual void emitProcBindClause(CodeGenFunction &CGF,
1173  llvm::omp::ProcBindKind ProcBind,
1174  SourceLocation Loc);
1175 
1176  /// Returns address of the threadprivate variable for the current
1177  /// thread.
1178  /// \param VD Threadprivate variable.
1179  /// \param VDAddr Address of the global variable \a VD.
1180  /// \param Loc Location of the reference to threadprivate var.
1181  /// \return Address of the threadprivate variable for the current thread.
1183  const VarDecl *VD,
1184  Address VDAddr,
1185  SourceLocation Loc);
1186 
1187  /// Returns the address of the variable marked as declare target with link
1188  /// clause OR as declare target with to clause and unified memory.
1189  virtual Address getAddrOfDeclareTargetVar(const VarDecl *VD);
1190 
1191  /// Emit a code for initialization of threadprivate variable. It emits
1192  /// a call to runtime library which adds initial value to the newly created
1193  /// threadprivate variable (if it is not constant) and registers destructor
1194  /// for the variable (if any).
1195  /// \param VD Threadprivate variable.
1196  /// \param VDAddr Address of the global variable \a VD.
1197  /// \param Loc Location of threadprivate declaration.
1198  /// \param PerformInit true if initialization expression is not constant.
1199  virtual llvm::Function *
1201  SourceLocation Loc, bool PerformInit,
1202  CodeGenFunction *CGF = nullptr);
1203 
1204  /// Emit a code for initialization of declare target variable.
1205  /// \param VD Declare target variable.
1206  /// \param Addr Address of the global variable \a VD.
1207  /// \param PerformInit true if initialization expression is not constant.
1208  virtual bool emitDeclareTargetVarDefinition(const VarDecl *VD,
1209  llvm::GlobalVariable *Addr,
1210  bool PerformInit);
1211 
1212  /// Creates artificial threadprivate variable with name \p Name and type \p
1213  /// VarType.
1214  /// \param VarType Type of the artificial threadprivate variable.
1215  /// \param Name Name of the artificial threadprivate variable.
1217  QualType VarType,
1218  StringRef Name);
1219 
1220  /// Emit flush of the variables specified in 'omp flush' directive.
1221  /// \param Vars List of variables to flush.
1222  virtual void emitFlush(CodeGenFunction &CGF, ArrayRef<const Expr *> Vars,
1223  SourceLocation Loc);
1224 
1225  /// Emit task region for the task directive. The task region is
1226  /// emitted in several steps:
1227  /// 1. Emit a call to kmp_task_t *__kmpc_omp_task_alloc(ident_t *, kmp_int32
1228  /// gtid, kmp_int32 flags, size_t sizeof_kmp_task_t, size_t sizeof_shareds,
1229  /// kmp_routine_entry_t *task_entry). Here task_entry is a pointer to the
1230  /// function:
1231  /// kmp_int32 .omp_task_entry.(kmp_int32 gtid, kmp_task_t *tt) {
1232  /// TaskFunction(gtid, tt->part_id, tt->shareds);
1233  /// return 0;
1234  /// }
1235  /// 2. Copy a list of shared variables to field shareds of the resulting
1236  /// structure kmp_task_t returned by the previous call (if any).
1237  /// 3. Copy a pointer to destructions function to field destructions of the
1238  /// resulting structure kmp_task_t.
1239  /// 4. Emit a call to kmp_int32 __kmpc_omp_task(ident_t *, kmp_int32 gtid,
1240  /// kmp_task_t *new_task), where new_task is a resulting structure from
1241  /// previous items.
1242  /// \param D Current task directive.
1243  /// \param TaskFunction An LLVM function with type void (*)(i32 /*gtid*/, i32
1244  /// /*part_id*/, captured_struct */*__context*/);
1245  /// \param SharedsTy A type which contains references the shared variables.
1246  /// \param Shareds Context with the list of shared variables from the \p
1247  /// TaskFunction.
1248  /// \param IfCond Not a nullptr if 'if' clause was specified, nullptr
1249  /// otherwise.
1250  /// \param Data Additional data for task generation like tiednsee, final
1251  /// state, list of privates etc.
1252  virtual void emitTaskCall(CodeGenFunction &CGF, SourceLocation Loc,
1253  const OMPExecutableDirective &D,
1254  llvm::Function *TaskFunction, QualType SharedsTy,
1255  Address Shareds, const Expr *IfCond,
1256  const OMPTaskDataTy &Data);
1257 
1258  /// Emit task region for the taskloop directive. The taskloop region is
1259  /// emitted in several steps:
1260  /// 1. Emit a call to kmp_task_t *__kmpc_omp_task_alloc(ident_t *, kmp_int32
1261  /// gtid, kmp_int32 flags, size_t sizeof_kmp_task_t, size_t sizeof_shareds,
1262  /// kmp_routine_entry_t *task_entry). Here task_entry is a pointer to the
1263  /// function:
1264  /// kmp_int32 .omp_task_entry.(kmp_int32 gtid, kmp_task_t *tt) {
1265  /// TaskFunction(gtid, tt->part_id, tt->shareds);
1266  /// return 0;
1267  /// }
1268  /// 2. Copy a list of shared variables to field shareds of the resulting
1269  /// structure kmp_task_t returned by the previous call (if any).
1270  /// 3. Copy a pointer to destructions function to field destructions of the
1271  /// resulting structure kmp_task_t.
1272  /// 4. Emit a call to void __kmpc_taskloop(ident_t *loc, int gtid, kmp_task_t
1273  /// *task, int if_val, kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st, int
1274  /// nogroup, int sched, kmp_uint64 grainsize, void *task_dup ), where new_task
1275  /// is a resulting structure from
1276  /// previous items.
1277  /// \param D Current task directive.
1278  /// \param TaskFunction An LLVM function with type void (*)(i32 /*gtid*/, i32
1279  /// /*part_id*/, captured_struct */*__context*/);
1280  /// \param SharedsTy A type which contains references the shared variables.
1281  /// \param Shareds Context with the list of shared variables from the \p
1282  /// TaskFunction.
1283  /// \param IfCond Not a nullptr if 'if' clause was specified, nullptr
1284  /// otherwise.
1285  /// \param Data Additional data for task generation like tiednsee, final
1286  /// state, list of privates etc.
1287  virtual void emitTaskLoopCall(CodeGenFunction &CGF, SourceLocation Loc,
1288  const OMPLoopDirective &D,
1289  llvm::Function *TaskFunction,
1290  QualType SharedsTy, Address Shareds,
1291  const Expr *IfCond, const OMPTaskDataTy &Data);
1292 
1293  /// Emit code for the directive that does not require outlining.
1294  ///
1295  /// \param InnermostKind Kind of innermost directive (for simple directives it
1296  /// is a directive itself, for combined - its innermost directive).
1297  /// \param CodeGen Code generation sequence for the \a D directive.
1298  /// \param HasCancel true if region has inner cancel directive, false
1299  /// otherwise.
1300  virtual void emitInlinedDirective(CodeGenFunction &CGF,
1301  OpenMPDirectiveKind InnermostKind,
1302  const RegionCodeGenTy &CodeGen,
1303  bool HasCancel = false);
1304 
1305  /// Emits reduction function.
1306  /// \param ArgsType Array type containing pointers to reduction variables.
1307  /// \param Privates List of private copies for original reduction arguments.
1308  /// \param LHSExprs List of LHS in \a ReductionOps reduction operations.
1309  /// \param RHSExprs List of RHS in \a ReductionOps reduction operations.
1310  /// \param ReductionOps List of reduction operations in form 'LHS binop RHS'
1311  /// or 'operator binop(LHS, RHS)'.
1312  llvm::Function *emitReductionFunction(SourceLocation Loc,
1313  llvm::Type *ArgsType,
1315  ArrayRef<const Expr *> LHSExprs,
1316  ArrayRef<const Expr *> RHSExprs,
1317  ArrayRef<const Expr *> ReductionOps);
1318 
1319  /// Emits single reduction combiner
1321  const Expr *ReductionOp,
1322  const Expr *PrivateRef,
1323  const DeclRefExpr *LHS,
1324  const DeclRefExpr *RHS);
1325 
1330  };
1331  /// Emit a code for reduction clause. Next code should be emitted for
1332  /// reduction:
1333  /// \code
1334  ///
1335  /// static kmp_critical_name lock = { 0 };
1336  ///
1337  /// void reduce_func(void *lhs[<n>], void *rhs[<n>]) {
1338  /// ...
1339  /// *(Type<i>*)lhs[i] = RedOp<i>(*(Type<i>*)lhs[i], *(Type<i>*)rhs[i]);
1340  /// ...
1341  /// }
1342  ///
1343  /// ...
1344  /// void *RedList[<n>] = {&<RHSExprs>[0], ..., &<RHSExprs>[<n>-1]};
1345  /// switch (__kmpc_reduce{_nowait}(<loc>, <gtid>, <n>, sizeof(RedList),
1346  /// RedList, reduce_func, &<lock>)) {
1347  /// case 1:
1348  /// ...
1349  /// <LHSExprs>[i] = RedOp<i>(*<LHSExprs>[i], *<RHSExprs>[i]);
1350  /// ...
1351  /// __kmpc_end_reduce{_nowait}(<loc>, <gtid>, &<lock>);
1352  /// break;
1353  /// case 2:
1354  /// ...
1355  /// Atomic(<LHSExprs>[i] = RedOp<i>(*<LHSExprs>[i], *<RHSExprs>[i]));
1356  /// ...
1357  /// break;
1358  /// default:;
1359  /// }
1360  /// \endcode
1361  ///
1362  /// \param Privates List of private copies for original reduction arguments.
1363  /// \param LHSExprs List of LHS in \a ReductionOps reduction operations.
1364  /// \param RHSExprs List of RHS in \a ReductionOps reduction operations.
1365  /// \param ReductionOps List of reduction operations in form 'LHS binop RHS'
1366  /// or 'operator binop(LHS, RHS)'.
1367  /// \param Options List of options for reduction codegen:
1368  /// WithNowait true if parent directive has also nowait clause, false
1369  /// otherwise.
1370  /// SimpleReduction Emit reduction operation only. Used for omp simd
1371  /// directive on the host.
1372  /// ReductionKind The kind of reduction to perform.
1373  virtual void emitReduction(CodeGenFunction &CGF, SourceLocation Loc,
1375  ArrayRef<const Expr *> LHSExprs,
1376  ArrayRef<const Expr *> RHSExprs,
1377  ArrayRef<const Expr *> ReductionOps,
1378  ReductionOptionsTy Options);
1379 
1380  /// Emit a code for initialization of task reduction clause. Next code
1381  /// should be emitted for reduction:
1382  /// \code
1383  ///
1384  /// _task_red_item_t red_data[n];
1385  /// ...
1386  /// red_data[i].shar = &origs[i];
1387  /// red_data[i].size = sizeof(origs[i]);
1388  /// red_data[i].f_init = (void*)RedInit<i>;
1389  /// red_data[i].f_fini = (void*)RedDest<i>;
1390  /// red_data[i].f_comb = (void*)RedOp<i>;
1391  /// red_data[i].flags = <Flag_i>;
1392  /// ...
1393  /// void* tg1 = __kmpc_task_reduction_init(gtid, n, red_data);
1394  /// \endcode
1395  ///
1396  /// \param LHSExprs List of LHS in \a Data.ReductionOps reduction operations.
1397  /// \param RHSExprs List of RHS in \a Data.ReductionOps reduction operations.
1398  /// \param Data Additional data for task generation like tiedness, final
1399  /// state, list of privates, reductions etc.
1401  SourceLocation Loc,
1402  ArrayRef<const Expr *> LHSExprs,
1403  ArrayRef<const Expr *> RHSExprs,
1404  const OMPTaskDataTy &Data);
1405 
1406  /// Required to resolve existing problems in the runtime. Emits threadprivate
1407  /// variables to store the size of the VLAs/array sections for
1408  /// initializer/combiner/finalizer functions + emits threadprivate variable to
1409  /// store the pointer to the original reduction item for the custom
1410  /// initializer defined by declare reduction construct.
1411  /// \param RCG Allows to reuse an existing data for the reductions.
1412  /// \param N Reduction item for which fixups must be emitted.
1414  ReductionCodeGen &RCG, unsigned N);
1415 
1416  /// Get the address of `void *` type of the privatue copy of the reduction
1417  /// item specified by the \p SharedLVal.
1418  /// \param ReductionsPtr Pointer to the reduction data returned by the
1419  /// emitTaskReductionInit function.
1420  /// \param SharedLVal Address of the original reduction item.
1422  llvm::Value *ReductionsPtr,
1423  LValue SharedLVal);
1424 
1425  /// Emit code for 'taskwait' directive.
1426  virtual void emitTaskwaitCall(CodeGenFunction &CGF, SourceLocation Loc);
1427 
1428  /// Emit code for 'cancellation point' construct.
1429  /// \param CancelRegion Region kind for which the cancellation point must be
1430  /// emitted.
1431  ///
1432  virtual void emitCancellationPointCall(CodeGenFunction &CGF,
1433  SourceLocation Loc,
1434  OpenMPDirectiveKind CancelRegion);
1435 
1436  /// Emit code for 'cancel' construct.
1437  /// \param IfCond Condition in the associated 'if' clause, if it was
1438  /// specified, nullptr otherwise.
1439  /// \param CancelRegion Region kind for which the cancel must be emitted.
1440  ///
1441  virtual void emitCancelCall(CodeGenFunction &CGF, SourceLocation Loc,
1442  const Expr *IfCond,
1443  OpenMPDirectiveKind CancelRegion);
1444 
1445  /// Emit outilined function for 'target' directive.
1446  /// \param D Directive to emit.
1447  /// \param ParentName Name of the function that encloses the target region.
1448  /// \param OutlinedFn Outlined function value to be defined by this call.
1449  /// \param OutlinedFnID Outlined function ID value to be defined by this call.
1450  /// \param IsOffloadEntry True if the outlined function is an offload entry.
1451  /// \param CodeGen Code generation sequence for the \a D directive.
1452  /// An outlined function may not be an entry if, e.g. the if clause always
1453  /// evaluates to false.
1454  virtual void emitTargetOutlinedFunction(const OMPExecutableDirective &D,
1455  StringRef ParentName,
1456  llvm::Function *&OutlinedFn,
1457  llvm::Constant *&OutlinedFnID,
1458  bool IsOffloadEntry,
1459  const RegionCodeGenTy &CodeGen);
1460 
1461  /// Emit the target offloading code associated with \a D. The emitted
1462  /// code attempts offloading the execution to the device, an the event of
1463  /// a failure it executes the host version outlined in \a OutlinedFn.
1464  /// \param D Directive to emit.
1465  /// \param OutlinedFn Host version of the code to be offloaded.
1466  /// \param OutlinedFnID ID of host version of the code to be offloaded.
1467  /// \param IfCond Expression evaluated in if clause associated with the target
1468  /// directive, or null if no if clause is used.
1469  /// \param Device Expression evaluated in device clause associated with the
1470  /// target directive, or null if no device clause is used.
1471  /// \param SizeEmitter Callback to emit number of iterations for loop-based
1472  /// directives.
1473  virtual void
1475  llvm::Function *OutlinedFn, llvm::Value *OutlinedFnID,
1476  const Expr *IfCond, const Expr *Device,
1477  llvm::function_ref<llvm::Value *(CodeGenFunction &CGF,
1478  const OMPLoopDirective &D)>
1479  SizeEmitter);
1480 
1481  /// Emit the target regions enclosed in \a GD function definition or
1482  /// the function itself in case it is a valid device function. Returns true if
1483  /// \a GD was dealt with successfully.
1484  /// \param GD Function to scan.
1485  virtual bool emitTargetFunctions(GlobalDecl GD);
1486 
1487  /// Emit the global variable if it is a valid device global variable.
1488  /// Returns true if \a GD was dealt with successfully.
1489  /// \param GD Variable declaration to emit.
1490  virtual bool emitTargetGlobalVariable(GlobalDecl GD);
1491 
1492  /// Checks if the provided global decl \a GD is a declare target variable and
1493  /// registers it when emitting code for the host.
1494  virtual void registerTargetGlobalVariable(const VarDecl *VD,
1495  llvm::Constant *Addr);
1496 
1497  /// Registers provided target firstprivate variable as global on the
1498  /// target.
1499  llvm::Constant *registerTargetFirstprivateCopy(CodeGenFunction &CGF,
1500  const VarDecl *VD);
1501 
1502  /// Emit the global \a GD if it is meaningful for the target. Returns
1503  /// if it was emitted successfully.
1504  /// \param GD Global to scan.
1505  virtual bool emitTargetGlobal(GlobalDecl GD);
1506 
1507  /// Creates and returns a registration function for when at least one
1508  /// requires directives was used in the current module.
1509  llvm::Function *emitRequiresDirectiveRegFun();
1510 
1511  /// Creates all the offload entries in the current compilation unit
1512  /// along with the associated metadata.
1514 
1515  /// Emits code for teams call of the \a OutlinedFn with
1516  /// variables captured in a record which address is stored in \a
1517  /// CapturedStruct.
1518  /// \param OutlinedFn Outlined function to be run by team masters. Type of
1519  /// this function is void(*)(kmp_int32 *, kmp_int32, struct context_vars*).
1520  /// \param CapturedVars A pointer to the record with the references to
1521  /// variables used in \a OutlinedFn function.
1522  ///
1523  virtual void emitTeamsCall(CodeGenFunction &CGF,
1524  const OMPExecutableDirective &D,
1525  SourceLocation Loc, llvm::Function *OutlinedFn,
1526  ArrayRef<llvm::Value *> CapturedVars);
1527 
1528  /// Emits call to void __kmpc_push_num_teams(ident_t *loc, kmp_int32
1529  /// global_tid, kmp_int32 num_teams, kmp_int32 thread_limit) to generate code
1530  /// for num_teams clause.
1531  /// \param NumTeams An integer expression of teams.
1532  /// \param ThreadLimit An integer expression of threads.
1533  virtual void emitNumTeamsClause(CodeGenFunction &CGF, const Expr *NumTeams,
1534  const Expr *ThreadLimit, SourceLocation Loc);
1535 
1536  /// Struct that keeps all the relevant information that should be kept
1537  /// throughout a 'target data' region.
1539  /// Set to true if device pointer information have to be obtained.
1540  bool RequiresDevicePointerInfo = false;
1541 
1542  public:
1543  /// The array of base pointer passed to the runtime library.
1544  llvm::Value *BasePointersArray = nullptr;
1545  /// The array of section pointers passed to the runtime library.
1546  llvm::Value *PointersArray = nullptr;
1547  /// The array of sizes passed to the runtime library.
1548  llvm::Value *SizesArray = nullptr;
1549  /// The array of map types passed to the runtime library.
1550  llvm::Value *MapTypesArray = nullptr;
1551  /// The total number of pointers passed to the runtime library.
1552  unsigned NumberOfPtrs = 0u;
1553  /// Map between the a declaration of a capture and the corresponding base
1554  /// pointer address where the runtime returns the device pointers.
1555  llvm::DenseMap<const ValueDecl *, Address> CaptureDeviceAddrMap;
1556 
1557  explicit TargetDataInfo() {}
1558  explicit TargetDataInfo(bool RequiresDevicePointerInfo)
1559  : RequiresDevicePointerInfo(RequiresDevicePointerInfo) {}
1560  /// Clear information about the data arrays.
1562  BasePointersArray = nullptr;
1563  PointersArray = nullptr;
1564  SizesArray = nullptr;
1565  MapTypesArray = nullptr;
1566  NumberOfPtrs = 0u;
1567  }
1568  /// Return true if the current target data information has valid arrays.
1569  bool isValid() {
1570  return BasePointersArray && PointersArray && SizesArray &&
1571  MapTypesArray && NumberOfPtrs;
1572  }
1573  bool requiresDevicePointerInfo() { return RequiresDevicePointerInfo; }
1574  };
1575 
1576  /// Emit the target data mapping code associated with \a D.
1577  /// \param D Directive to emit.
1578  /// \param IfCond Expression evaluated in if clause associated with the
1579  /// target directive, or null if no device clause is used.
1580  /// \param Device Expression evaluated in device clause associated with the
1581  /// target directive, or null if no device clause is used.
1582  /// \param Info A record used to store information that needs to be preserved
1583  /// until the region is closed.
1584  virtual void emitTargetDataCalls(CodeGenFunction &CGF,
1585  const OMPExecutableDirective &D,
1586  const Expr *IfCond, const Expr *Device,
1587  const RegionCodeGenTy &CodeGen,
1588  TargetDataInfo &Info);
1589 
1590  /// Emit the data mapping/movement code associated with the directive
1591  /// \a D that should be of the form 'target [{enter|exit} data | update]'.
1592  /// \param D Directive to emit.
1593  /// \param IfCond Expression evaluated in if clause associated with the target
1594  /// directive, or null if no if clause is used.
1595  /// \param Device Expression evaluated in device clause associated with the
1596  /// target directive, or null if no device clause is used.
1598  const OMPExecutableDirective &D,
1599  const Expr *IfCond,
1600  const Expr *Device);
1601 
1602  /// Marks function \a Fn with properly mangled versions of vector functions.
1603  /// \param FD Function marked as 'declare simd'.
1604  /// \param Fn LLVM function that must be marked with 'declare simd'
1605  /// attributes.
1606  virtual void emitDeclareSimdFunction(const FunctionDecl *FD,
1607  llvm::Function *Fn);
1608 
1609  /// Emit initialization for doacross loop nesting support.
1610  /// \param D Loop-based construct used in doacross nesting construct.
1611  virtual void emitDoacrossInit(CodeGenFunction &CGF, const OMPLoopDirective &D,
1612  ArrayRef<Expr *> NumIterations);
1613 
1614  /// Emit code for doacross ordered directive with 'depend' clause.
1615  /// \param C 'depend' clause with 'sink|source' dependency kind.
1616  virtual void emitDoacrossOrdered(CodeGenFunction &CGF,
1617  const OMPDependClause *C);
1618 
1619  /// Translates the native parameter of outlined function if this is required
1620  /// for target.
1621  /// \param FD Field decl from captured record for the parameter.
1622  /// \param NativeParam Parameter itself.
1623  virtual const VarDecl *translateParameter(const FieldDecl *FD,
1624  const VarDecl *NativeParam) const {
1625  return NativeParam;
1626  }
1627 
1628  /// Gets the address of the native argument basing on the address of the
1629  /// target-specific parameter.
1630  /// \param NativeParam Parameter itself.
1631  /// \param TargetParam Corresponding target-specific parameter.
1633  const VarDecl *NativeParam,
1634  const VarDecl *TargetParam) const;
1635 
1636  /// Choose default schedule type and chunk value for the
1637  /// dist_schedule clause.
1639  const OMPLoopDirective &S, OpenMPDistScheduleClauseKind &ScheduleKind,
1640  llvm::Value *&Chunk) const {}
1641 
1642  /// Choose default schedule type and chunk value for the
1643  /// schedule clause.
1644  virtual void getDefaultScheduleAndChunk(CodeGenFunction &CGF,
1645  const OMPLoopDirective &S, OpenMPScheduleClauseKind &ScheduleKind,
1646  const Expr *&ChunkExpr) const;
1647 
1648  /// Emits call of the outlined function with the provided arguments,
1649  /// translating these arguments to correct target-specific arguments.
1650  virtual void
1652  llvm::FunctionCallee OutlinedFn,
1653  ArrayRef<llvm::Value *> Args = llvm::None) const;
1654 
1655  /// Emits OpenMP-specific function prolog.
1656  /// Required for device constructs.
1657  virtual void emitFunctionProlog(CodeGenFunction &CGF, const Decl *D);
1658 
1659  /// Gets the OpenMP-specific address of the local variable.
1661  const VarDecl *VD);
1662 
1663  /// Marks the declaration as already emitted for the device code and returns
1664  /// true, if it was marked already, and false, otherwise.
1665  bool markAsGlobalTarget(GlobalDecl GD);
1666 
1667  /// Emit deferred declare target variables marked for deferred emission.
1668  void emitDeferredTargetDecls() const;
1669 
1670  /// Adjust some parameters for the target-based directives, like addresses of
1671  /// the variables captured by reference in lambdas.
1672  virtual void
1674  const OMPExecutableDirective &D) const;
1675 
1676  /// Perform check on requires decl to ensure that target architecture
1677  /// supports unified addressing
1678  virtual void checkArchForUnifiedAddressing(const OMPRequiresDecl *D);
1679 
1680  /// Checks if the variable has associated OMPAllocateDeclAttr attribute with
1681  /// the predefined allocator and translates it into the corresponding address
1682  /// space.
1683  virtual bool hasAllocateAttributeForGlobalVar(const VarDecl *VD, LangAS &AS);
1684 
1685  /// Return whether the unified_shared_memory has been specified.
1686  bool hasRequiresUnifiedSharedMemory() const;
1687 
1688  /// Emits the definition of the declare variant function.
1689  virtual bool emitDeclareVariant(GlobalDecl GD, bool IsForDefinition);
1690 
1691  /// Checks if the \p VD variable is marked as nontemporal declaration in
1692  /// current context.
1693  bool isNontemporalDecl(const ValueDecl *VD) const;
1694 
1695  /// Initializes global counter for lastprivate conditional.
1696  virtual void
1698  const OMPExecutableDirective &S);
1699 
1700  /// Checks if the provided \p LVal is lastprivate conditional and emits the
1701  /// code to update the value of the original variable.
1702  /// \code
1703  /// lastprivate(conditional: a)
1704  /// ...
1705  /// <type> a;
1706  /// lp_a = ...;
1707  /// #pragma omp critical(a)
1708  /// if (last_iv_a <= iv) {
1709  /// last_iv_a = iv;
1710  /// global_a = lp_a;
1711  /// }
1712  /// \endcode
1714  const Expr *LHS);
1715 
1716  /// Gets the address of the global copy used for lastprivate conditional
1717  /// update, if any.
1718  /// \param PrivLVal LValue for the private copy.
1719  /// \param VD Original lastprivate declaration.
1721  LValue PrivLVal,
1722  const VarDecl *VD,
1723  SourceLocation Loc);
1724 };
1725 
1726 /// Class supports emissionof SIMD-only code.
1727 class CGOpenMPSIMDRuntime final : public CGOpenMPRuntime {
1728 public:
1729  explicit CGOpenMPSIMDRuntime(CodeGenModule &CGM) : CGOpenMPRuntime(CGM) {}
1730  ~CGOpenMPSIMDRuntime() override {}
1731 
1732  /// Emits outlined function for the specified OpenMP parallel directive
1733  /// \a D. This outlined function has type void(*)(kmp_int32 *ThreadID,
1734  /// kmp_int32 BoundID, struct context_vars*).
1735  /// \param D OpenMP directive.
1736  /// \param ThreadIDVar Variable for thread id in the current OpenMP region.
1737  /// \param InnermostKind Kind of innermost directive (for simple directives it
1738  /// is a directive itself, for combined - its innermost directive).
1739  /// \param CodeGen Code generation sequence for the \a D directive.
1740  llvm::Function *
1742  const VarDecl *ThreadIDVar,
1743  OpenMPDirectiveKind InnermostKind,
1744  const RegionCodeGenTy &CodeGen) override;
1745 
1746  /// Emits outlined function for the specified OpenMP teams directive
1747  /// \a D. This outlined function has type void(*)(kmp_int32 *ThreadID,
1748  /// kmp_int32 BoundID, struct context_vars*).
1749  /// \param D OpenMP directive.
1750  /// \param ThreadIDVar Variable for thread id in the current OpenMP region.
1751  /// \param InnermostKind Kind of innermost directive (for simple directives it
1752  /// is a directive itself, for combined - its innermost directive).
1753  /// \param CodeGen Code generation sequence for the \a D directive.
1754  llvm::Function *
1756  const VarDecl *ThreadIDVar,
1757  OpenMPDirectiveKind InnermostKind,
1758  const RegionCodeGenTy &CodeGen) override;
1759 
1760  /// Emits outlined function for the OpenMP task directive \a D. This
1761  /// outlined function has type void(*)(kmp_int32 ThreadID, struct task_t*
1762  /// TaskT).
1763  /// \param D OpenMP directive.
1764  /// \param ThreadIDVar Variable for thread id in the current OpenMP region.
1765  /// \param PartIDVar Variable for partition id in the current OpenMP untied
1766  /// task region.
1767  /// \param TaskTVar Variable for task_t argument.
1768  /// \param InnermostKind Kind of innermost directive (for simple directives it
1769  /// is a directive itself, for combined - its innermost directive).
1770  /// \param CodeGen Code generation sequence for the \a D directive.
1771  /// \param Tied true if task is generated for tied task, false otherwise.
1772  /// \param NumberOfParts Number of parts in untied task. Ignored for tied
1773  /// tasks.
1774  ///
1775  llvm::Function *emitTaskOutlinedFunction(
1776  const OMPExecutableDirective &D, const VarDecl *ThreadIDVar,
1777  const VarDecl *PartIDVar, const VarDecl *TaskTVar,
1778  OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen,
1779  bool Tied, unsigned &NumberOfParts) override;
1780 
1781  /// Emits code for parallel or serial call of the \a OutlinedFn with
1782  /// variables captured in a record which address is stored in \a
1783  /// CapturedStruct.
1784  /// \param OutlinedFn Outlined function to be run in parallel threads. Type of
1785  /// this function is void(*)(kmp_int32 *, kmp_int32, struct context_vars*).
1786  /// \param CapturedVars A pointer to the record with the references to
1787  /// variables used in \a OutlinedFn function.
1788  /// \param IfCond Condition in the associated 'if' clause, if it was
1789  /// specified, nullptr otherwise.
1790  ///
1791  void emitParallelCall(CodeGenFunction &CGF, SourceLocation Loc,
1792  llvm::Function *OutlinedFn,
1793  ArrayRef<llvm::Value *> CapturedVars,
1794  const Expr *IfCond) override;
1795 
1796  /// Emits a critical region.
1797  /// \param CriticalName Name of the critical region.
1798  /// \param CriticalOpGen Generator for the statement associated with the given
1799  /// critical region.
1800  /// \param Hint Value of the 'hint' clause (optional).
1801  void emitCriticalRegion(CodeGenFunction &CGF, StringRef CriticalName,
1802  const RegionCodeGenTy &CriticalOpGen,
1803  SourceLocation Loc,
1804  const Expr *Hint = nullptr) override;
1805 
1806  /// Emits a master region.
1807  /// \param MasterOpGen Generator for the statement associated with the given
1808  /// master region.
1809  void emitMasterRegion(CodeGenFunction &CGF,
1810  const RegionCodeGenTy &MasterOpGen,
1811  SourceLocation Loc) override;
1812 
1813  /// Emits code for a taskyield directive.
1814  void emitTaskyieldCall(CodeGenFunction &CGF, SourceLocation Loc) override;
1815 
1816  /// Emit a taskgroup region.
1817  /// \param TaskgroupOpGen Generator for the statement associated with the
1818  /// given taskgroup region.
1819  void emitTaskgroupRegion(CodeGenFunction &CGF,
1820  const RegionCodeGenTy &TaskgroupOpGen,
1821  SourceLocation Loc) override;
1822 
1823  /// Emits a single region.
1824  /// \param SingleOpGen Generator for the statement associated with the given
1825  /// single region.
1826  void emitSingleRegion(CodeGenFunction &CGF,
1827  const RegionCodeGenTy &SingleOpGen, SourceLocation Loc,
1828  ArrayRef<const Expr *> CopyprivateVars,
1829  ArrayRef<const Expr *> DestExprs,
1830  ArrayRef<const Expr *> SrcExprs,
1831  ArrayRef<const Expr *> AssignmentOps) override;
1832 
1833  /// Emit an ordered region.
1834  /// \param OrderedOpGen Generator for the statement associated with the given
1835  /// ordered region.
1836  void emitOrderedRegion(CodeGenFunction &CGF,
1837  const RegionCodeGenTy &OrderedOpGen,
1838  SourceLocation Loc, bool IsThreads) override;
1839 
1840  /// Emit an implicit/explicit barrier for OpenMP threads.
1841  /// \param Kind Directive for which this implicit barrier call must be
1842  /// generated. Must be OMPD_barrier for explicit barrier generation.
1843  /// \param EmitChecks true if need to emit checks for cancellation barriers.
1844  /// \param ForceSimpleCall true simple barrier call must be emitted, false if
1845  /// runtime class decides which one to emit (simple or with cancellation
1846  /// checks).
1847  ///
1848  void emitBarrierCall(CodeGenFunction &CGF, SourceLocation Loc,
1849  OpenMPDirectiveKind Kind, bool EmitChecks = true,
1850  bool ForceSimpleCall = false) override;
1851 
1852  /// This is used for non static scheduled types and when the ordered
1853  /// clause is present on the loop construct.
1854  /// Depending on the loop schedule, it is necessary to call some runtime
1855  /// routine before start of the OpenMP loop to get the loop upper / lower
1856  /// bounds \a LB and \a UB and stride \a ST.
1857  ///
1858  /// \param CGF Reference to current CodeGenFunction.
1859  /// \param Loc Clang source location.
1860  /// \param ScheduleKind Schedule kind, specified by the 'schedule' clause.
1861  /// \param IVSize Size of the iteration variable in bits.
1862  /// \param IVSigned Sign of the iteration variable.
1863  /// \param Ordered true if loop is ordered, false otherwise.
1864  /// \param DispatchValues struct containing llvm values for lower bound, upper
1865  /// bound, and chunk expression.
1866  /// For the default (nullptr) value, the chunk 1 will be used.
1867  ///
1868  void emitForDispatchInit(CodeGenFunction &CGF, SourceLocation Loc,
1869  const OpenMPScheduleTy &ScheduleKind,
1870  unsigned IVSize, bool IVSigned, bool Ordered,
1871  const DispatchRTInput &DispatchValues) override;
1872 
1873  /// Call the appropriate runtime routine to initialize it before start
1874  /// of loop.
1875  ///
1876  /// This is used only in case of static schedule, when the user did not
1877  /// specify a ordered clause on the loop construct.
1878  /// Depending on the loop schedule, it is necessary to call some runtime
1879  /// routine before start of the OpenMP loop to get the loop upper / lower
1880  /// bounds LB and UB and stride ST.
1881  ///
1882  /// \param CGF Reference to current CodeGenFunction.
1883  /// \param Loc Clang source location.
1884  /// \param DKind Kind of the directive.
1885  /// \param ScheduleKind Schedule kind, specified by the 'schedule' clause.
1886  /// \param Values Input arguments for the construct.
1887  ///
1888  void emitForStaticInit(CodeGenFunction &CGF, SourceLocation Loc,
1889  OpenMPDirectiveKind DKind,
1890  const OpenMPScheduleTy &ScheduleKind,
1891  const StaticRTInput &Values) override;
1892 
1893  ///
1894  /// \param CGF Reference to current CodeGenFunction.
1895  /// \param Loc Clang source location.
1896  /// \param SchedKind Schedule kind, specified by the 'dist_schedule' clause.
1897  /// \param Values Input arguments for the construct.
1898  ///
1899  void emitDistributeStaticInit(CodeGenFunction &CGF, SourceLocation Loc,
1900  OpenMPDistScheduleClauseKind SchedKind,
1901  const StaticRTInput &Values) override;
1902 
1903  /// Call the appropriate runtime routine to notify that we finished
1904  /// iteration of the ordered loop with the dynamic scheduling.
1905  ///
1906  /// \param CGF Reference to current CodeGenFunction.
1907  /// \param Loc Clang source location.
1908  /// \param IVSize Size of the iteration variable in bits.
1909  /// \param IVSigned Sign of the iteration variable.
1910  ///
1911  void emitForOrderedIterationEnd(CodeGenFunction &CGF, SourceLocation Loc,
1912  unsigned IVSize, bool IVSigned) override;
1913 
1914  /// Call the appropriate runtime routine to notify that we finished
1915  /// all the work with current loop.
1916  ///
1917  /// \param CGF Reference to current CodeGenFunction.
1918  /// \param Loc Clang source location.
1919  /// \param DKind Kind of the directive for which the static finish is emitted.
1920  ///
1921  void emitForStaticFinish(CodeGenFunction &CGF, SourceLocation Loc,
1922  OpenMPDirectiveKind DKind) override;
1923 
1924  /// Call __kmpc_dispatch_next(
1925  /// ident_t *loc, kmp_int32 tid, kmp_int32 *p_lastiter,
1926  /// kmp_int[32|64] *p_lower, kmp_int[32|64] *p_upper,
1927  /// kmp_int[32|64] *p_stride);
1928  /// \param IVSize Size of the iteration variable in bits.
1929  /// \param IVSigned Sign of the iteration variable.
1930  /// \param IL Address of the output variable in which the flag of the
1931  /// last iteration is returned.
1932  /// \param LB Address of the output variable in which the lower iteration
1933  /// number is returned.
1934  /// \param UB Address of the output variable in which the upper iteration
1935  /// number is returned.
1936  /// \param ST Address of the output variable in which the stride value is
1937  /// returned.
1938  llvm::Value *emitForNext(CodeGenFunction &CGF, SourceLocation Loc,
1939  unsigned IVSize, bool IVSigned, Address IL,
1940  Address LB, Address UB, Address ST) override;
1941 
1942  /// Emits call to void __kmpc_push_num_threads(ident_t *loc, kmp_int32
1943  /// global_tid, kmp_int32 num_threads) to generate code for 'num_threads'
1944  /// clause.
1945  /// \param NumThreads An integer value of threads.
1946  void emitNumThreadsClause(CodeGenFunction &CGF, llvm::Value *NumThreads,
1947  SourceLocation Loc) override;
1948 
1949  /// Emit call to void __kmpc_push_proc_bind(ident_t *loc, kmp_int32
1950  /// global_tid, int proc_bind) to generate code for 'proc_bind' clause.
1951  void emitProcBindClause(CodeGenFunction &CGF,
1952  llvm::omp::ProcBindKind ProcBind,
1953  SourceLocation Loc) override;
1954 
1955  /// Returns address of the threadprivate variable for the current
1956  /// thread.
1957  /// \param VD Threadprivate variable.
1958  /// \param VDAddr Address of the global variable \a VD.
1959  /// \param Loc Location of the reference to threadprivate var.
1960  /// \return Address of the threadprivate variable for the current thread.
1961  Address getAddrOfThreadPrivate(CodeGenFunction &CGF, const VarDecl *VD,
1962  Address VDAddr, SourceLocation Loc) override;
1963 
1964  /// Emit a code for initialization of threadprivate variable. It emits
1965  /// a call to runtime library which adds initial value to the newly created
1966  /// threadprivate variable (if it is not constant) and registers destructor
1967  /// for the variable (if any).
1968  /// \param VD Threadprivate variable.
1969  /// \param VDAddr Address of the global variable \a VD.
1970  /// \param Loc Location of threadprivate declaration.
1971  /// \param PerformInit true if initialization expression is not constant.
1972  llvm::Function *
1973  emitThreadPrivateVarDefinition(const VarDecl *VD, Address VDAddr,
1974  SourceLocation Loc, bool PerformInit,
1975  CodeGenFunction *CGF = nullptr) override;
1976 
1977  /// Creates artificial threadprivate variable with name \p Name and type \p
1978  /// VarType.
1979  /// \param VarType Type of the artificial threadprivate variable.
1980  /// \param Name Name of the artificial threadprivate variable.
1981  Address getAddrOfArtificialThreadPrivate(CodeGenFunction &CGF,
1982  QualType VarType,
1983  StringRef Name) override;
1984 
1985  /// Emit flush of the variables specified in 'omp flush' directive.
1986  /// \param Vars List of variables to flush.
1987  void emitFlush(CodeGenFunction &CGF, ArrayRef<const Expr *> Vars,
1988  SourceLocation Loc) override;
1989 
1990  /// Emit task region for the task directive. The task region is
1991  /// emitted in several steps:
1992  /// 1. Emit a call to kmp_task_t *__kmpc_omp_task_alloc(ident_t *, kmp_int32
1993  /// gtid, kmp_int32 flags, size_t sizeof_kmp_task_t, size_t sizeof_shareds,
1994  /// kmp_routine_entry_t *task_entry). Here task_entry is a pointer to the
1995  /// function:
1996  /// kmp_int32 .omp_task_entry.(kmp_int32 gtid, kmp_task_t *tt) {
1997  /// TaskFunction(gtid, tt->part_id, tt->shareds);
1998  /// return 0;
1999  /// }
2000  /// 2. Copy a list of shared variables to field shareds of the resulting
2001  /// structure kmp_task_t returned by the previous call (if any).
2002  /// 3. Copy a pointer to destructions function to field destructions of the
2003  /// resulting structure kmp_task_t.
2004  /// 4. Emit a call to kmp_int32 __kmpc_omp_task(ident_t *, kmp_int32 gtid,
2005  /// kmp_task_t *new_task), where new_task is a resulting structure from
2006  /// previous items.
2007  /// \param D Current task directive.
2008  /// \param TaskFunction An LLVM function with type void (*)(i32 /*gtid*/, i32
2009  /// /*part_id*/, captured_struct */*__context*/);
2010  /// \param SharedsTy A type which contains references the shared variables.
2011  /// \param Shareds Context with the list of shared variables from the \p
2012  /// TaskFunction.
2013  /// \param IfCond Not a nullptr if 'if' clause was specified, nullptr
2014  /// otherwise.
2015  /// \param Data Additional data for task generation like tiednsee, final
2016  /// state, list of privates etc.
2017  void emitTaskCall(CodeGenFunction &CGF, SourceLocation Loc,
2018  const OMPExecutableDirective &D,
2019  llvm::Function *TaskFunction, QualType SharedsTy,
2020  Address Shareds, const Expr *IfCond,
2021  const OMPTaskDataTy &Data) override;
2022 
2023  /// Emit task region for the taskloop directive. The taskloop region is
2024  /// emitted in several steps:
2025  /// 1. Emit a call to kmp_task_t *__kmpc_omp_task_alloc(ident_t *, kmp_int32
2026  /// gtid, kmp_int32 flags, size_t sizeof_kmp_task_t, size_t sizeof_shareds,
2027  /// kmp_routine_entry_t *task_entry). Here task_entry is a pointer to the
2028  /// function:
2029  /// kmp_int32 .omp_task_entry.(kmp_int32 gtid, kmp_task_t *tt) {
2030  /// TaskFunction(gtid, tt->part_id, tt->shareds);
2031  /// return 0;
2032  /// }
2033  /// 2. Copy a list of shared variables to field shareds of the resulting
2034  /// structure kmp_task_t returned by the previous call (if any).
2035  /// 3. Copy a pointer to destructions function to field destructions of the
2036  /// resulting structure kmp_task_t.
2037  /// 4. Emit a call to void __kmpc_taskloop(ident_t *loc, int gtid, kmp_task_t
2038  /// *task, int if_val, kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st, int
2039  /// nogroup, int sched, kmp_uint64 grainsize, void *task_dup ), where new_task
2040  /// is a resulting structure from
2041  /// previous items.
2042  /// \param D Current task directive.
2043  /// \param TaskFunction An LLVM function with type void (*)(i32 /*gtid*/, i32
2044  /// /*part_id*/, captured_struct */*__context*/);
2045  /// \param SharedsTy A type which contains references the shared variables.
2046  /// \param Shareds Context with the list of shared variables from the \p
2047  /// TaskFunction.
2048  /// \param IfCond Not a nullptr if 'if' clause was specified, nullptr
2049  /// otherwise.
2050  /// \param Data Additional data for task generation like tiednsee, final
2051  /// state, list of privates etc.
2052  void emitTaskLoopCall(CodeGenFunction &CGF, SourceLocation Loc,
2053  const OMPLoopDirective &D, llvm::Function *TaskFunction,
2054  QualType SharedsTy, Address Shareds, const Expr *IfCond,
2055  const OMPTaskDataTy &Data) override;
2056 
2057  /// Emit a code for reduction clause. Next code should be emitted for
2058  /// reduction:
2059  /// \code
2060  ///
2061  /// static kmp_critical_name lock = { 0 };
2062  ///
2063  /// void reduce_func(void *lhs[<n>], void *rhs[<n>]) {
2064  /// ...
2065  /// *(Type<i>*)lhs[i] = RedOp<i>(*(Type<i>*)lhs[i], *(Type<i>*)rhs[i]);
2066  /// ...
2067  /// }
2068  ///
2069  /// ...
2070  /// void *RedList[<n>] = {&<RHSExprs>[0], ..., &<RHSExprs>[<n>-1]};
2071  /// switch (__kmpc_reduce{_nowait}(<loc>, <gtid>, <n>, sizeof(RedList),
2072  /// RedList, reduce_func, &<lock>)) {
2073  /// case 1:
2074  /// ...
2075  /// <LHSExprs>[i] = RedOp<i>(*<LHSExprs>[i], *<RHSExprs>[i]);
2076  /// ...
2077  /// __kmpc_end_reduce{_nowait}(<loc>, <gtid>, &<lock>);
2078  /// break;
2079  /// case 2:
2080  /// ...
2081  /// Atomic(<LHSExprs>[i] = RedOp<i>(*<LHSExprs>[i], *<RHSExprs>[i]));
2082  /// ...
2083  /// break;
2084  /// default:;
2085  /// }
2086  /// \endcode
2087  ///
2088  /// \param Privates List of private copies for original reduction arguments.
2089  /// \param LHSExprs List of LHS in \a ReductionOps reduction operations.
2090  /// \param RHSExprs List of RHS in \a ReductionOps reduction operations.
2091  /// \param ReductionOps List of reduction operations in form 'LHS binop RHS'
2092  /// or 'operator binop(LHS, RHS)'.
2093  /// \param Options List of options for reduction codegen:
2094  /// WithNowait true if parent directive has also nowait clause, false
2095  /// otherwise.
2096  /// SimpleReduction Emit reduction operation only. Used for omp simd
2097  /// directive on the host.
2098  /// ReductionKind The kind of reduction to perform.
2099  void emitReduction(CodeGenFunction &CGF, SourceLocation Loc,
2101  ArrayRef<const Expr *> LHSExprs,
2102  ArrayRef<const Expr *> RHSExprs,
2103  ArrayRef<const Expr *> ReductionOps,
2104  ReductionOptionsTy Options) override;
2105 
2106  /// Emit a code for initialization of task reduction clause. Next code
2107  /// should be emitted for reduction:
2108  /// \code
2109  ///
2110  /// _task_red_item_t red_data[n];
2111  /// ...
2112  /// red_data[i].shar = &origs[i];
2113  /// red_data[i].size = sizeof(origs[i]);
2114  /// red_data[i].f_init = (void*)RedInit<i>;
2115  /// red_data[i].f_fini = (void*)RedDest<i>;
2116  /// red_data[i].f_comb = (void*)RedOp<i>;
2117  /// red_data[i].flags = <Flag_i>;
2118  /// ...
2119  /// void* tg1 = __kmpc_task_reduction_init(gtid, n, red_data);
2120  /// \endcode
2121  ///
2122  /// \param LHSExprs List of LHS in \a Data.ReductionOps reduction operations.
2123  /// \param RHSExprs List of RHS in \a Data.ReductionOps reduction operations.
2124  /// \param Data Additional data for task generation like tiedness, final
2125  /// state, list of privates, reductions etc.
2126  llvm::Value *emitTaskReductionInit(CodeGenFunction &CGF, SourceLocation Loc,
2127  ArrayRef<const Expr *> LHSExprs,
2128  ArrayRef<const Expr *> RHSExprs,
2129  const OMPTaskDataTy &Data) override;
2130 
2131  /// Required to resolve existing problems in the runtime. Emits threadprivate
2132  /// variables to store the size of the VLAs/array sections for
2133  /// initializer/combiner/finalizer functions + emits threadprivate variable to
2134  /// store the pointer to the original reduction item for the custom
2135  /// initializer defined by declare reduction construct.
2136  /// \param RCG Allows to reuse an existing data for the reductions.
2137  /// \param N Reduction item for which fixups must be emitted.
2138  void emitTaskReductionFixups(CodeGenFunction &CGF, SourceLocation Loc,
2139  ReductionCodeGen &RCG, unsigned N) override;
2140 
2141  /// Get the address of `void *` type of the privatue copy of the reduction
2142  /// item specified by the \p SharedLVal.
2143  /// \param ReductionsPtr Pointer to the reduction data returned by the
2144  /// emitTaskReductionInit function.
2145  /// \param SharedLVal Address of the original reduction item.
2146  Address getTaskReductionItem(CodeGenFunction &CGF, SourceLocation Loc,
2147  llvm::Value *ReductionsPtr,
2148  LValue SharedLVal) override;
2149 
2150  /// Emit code for 'taskwait' directive.
2151  void emitTaskwaitCall(CodeGenFunction &CGF, SourceLocation Loc) override;
2152 
2153  /// Emit code for 'cancellation point' construct.
2154  /// \param CancelRegion Region kind for which the cancellation point must be
2155  /// emitted.
2156  ///
2157  void emitCancellationPointCall(CodeGenFunction &CGF, SourceLocation Loc,
2158  OpenMPDirectiveKind CancelRegion) override;
2159 
2160  /// Emit code for 'cancel' construct.
2161  /// \param IfCond Condition in the associated 'if' clause, if it was
2162  /// specified, nullptr otherwise.
2163  /// \param CancelRegion Region kind for which the cancel must be emitted.
2164  ///
2165  void emitCancelCall(CodeGenFunction &CGF, SourceLocation Loc,
2166  const Expr *IfCond,
2167  OpenMPDirectiveKind CancelRegion) override;
2168 
2169  /// Emit outilined function for 'target' directive.
2170  /// \param D Directive to emit.
2171  /// \param ParentName Name of the function that encloses the target region.
2172  /// \param OutlinedFn Outlined function value to be defined by this call.
2173  /// \param OutlinedFnID Outlined function ID value to be defined by this call.
2174  /// \param IsOffloadEntry True if the outlined function is an offload entry.
2175  /// \param CodeGen Code generation sequence for the \a D directive.
2176  /// An outlined function may not be an entry if, e.g. the if clause always
2177  /// evaluates to false.
2179  StringRef ParentName,
2180  llvm::Function *&OutlinedFn,
2181  llvm::Constant *&OutlinedFnID,
2182  bool IsOffloadEntry,
2183  const RegionCodeGenTy &CodeGen) override;
2184 
2185  /// Emit the target offloading code associated with \a D. The emitted
2186  /// code attempts offloading the execution to the device, an the event of
2187  /// a failure it executes the host version outlined in \a OutlinedFn.
2188  /// \param D Directive to emit.
2189  /// \param OutlinedFn Host version of the code to be offloaded.
2190  /// \param OutlinedFnID ID of host version of the code to be offloaded.
2191  /// \param IfCond Expression evaluated in if clause associated with the target
2192  /// directive, or null if no if clause is used.
2193  /// \param Device Expression evaluated in device clause associated with the
2194  /// target directive, or null if no device clause is used.
2195  void
2196  emitTargetCall(CodeGenFunction &CGF, const OMPExecutableDirective &D,
2197  llvm::Function *OutlinedFn, llvm::Value *OutlinedFnID,
2198  const Expr *IfCond, const Expr *Device,
2199  llvm::function_ref<llvm::Value *(CodeGenFunction &CGF,
2200  const OMPLoopDirective &D)>
2201  SizeEmitter) override;
2202 
2203  /// Emit the target regions enclosed in \a GD function definition or
2204  /// the function itself in case it is a valid device function. Returns true if
2205  /// \a GD was dealt with successfully.
2206  /// \param GD Function to scan.
2207  bool emitTargetFunctions(GlobalDecl GD) override;
2208 
2209  /// Emit the global variable if it is a valid device global variable.
2210  /// Returns true if \a GD was dealt with successfully.
2211  /// \param GD Variable declaration to emit.
2212  bool emitTargetGlobalVariable(GlobalDecl GD) override;
2213 
2214  /// Emit the global \a GD if it is meaningful for the target. Returns
2215  /// if it was emitted successfully.
2216  /// \param GD Global to scan.
2217  bool emitTargetGlobal(GlobalDecl GD) override;
2218 
2219  /// Emits code for teams call of the \a OutlinedFn with
2220  /// variables captured in a record which address is stored in \a
2221  /// CapturedStruct.
2222  /// \param OutlinedFn Outlined function to be run by team masters. Type of
2223  /// this function is void(*)(kmp_int32 *, kmp_int32, struct context_vars*).
2224  /// \param CapturedVars A pointer to the record with the references to
2225  /// variables used in \a OutlinedFn function.
2226  ///
2227  void emitTeamsCall(CodeGenFunction &CGF, const OMPExecutableDirective &D,
2228  SourceLocation Loc, llvm::Function *OutlinedFn,
2229  ArrayRef<llvm::Value *> CapturedVars) override;
2230 
2231  /// Emits call to void __kmpc_push_num_teams(ident_t *loc, kmp_int32
2232  /// global_tid, kmp_int32 num_teams, kmp_int32 thread_limit) to generate code
2233  /// for num_teams clause.
2234  /// \param NumTeams An integer expression of teams.
2235  /// \param ThreadLimit An integer expression of threads.
2236  void emitNumTeamsClause(CodeGenFunction &CGF, const Expr *NumTeams,
2237  const Expr *ThreadLimit, SourceLocation Loc) override;
2238 
2239  /// Emit the target data mapping code associated with \a D.
2240  /// \param D Directive to emit.
2241  /// \param IfCond Expression evaluated in if clause associated with the
2242  /// target directive, or null if no device clause is used.
2243  /// \param Device Expression evaluated in device clause associated with the
2244  /// target directive, or null if no device clause is used.
2245  /// \param Info A record used to store information that needs to be preserved
2246  /// until the region is closed.
2247  void emitTargetDataCalls(CodeGenFunction &CGF,
2248  const OMPExecutableDirective &D, const Expr *IfCond,
2249  const Expr *Device, const RegionCodeGenTy &CodeGen,
2250  TargetDataInfo &Info) override;
2251 
2252  /// Emit the data mapping/movement code associated with the directive
2253  /// \a D that should be of the form 'target [{enter|exit} data | update]'.
2254  /// \param D Directive to emit.
2255  /// \param IfCond Expression evaluated in if clause associated with the target
2256  /// directive, or null if no if clause is used.
2257  /// \param Device Expression evaluated in device clause associated with the
2258  /// target directive, or null if no device clause is used.
2259  void emitTargetDataStandAloneCall(CodeGenFunction &CGF,
2260  const OMPExecutableDirective &D,
2261  const Expr *IfCond,
2262  const Expr *Device) override;
2263 
2264  /// Emit initialization for doacross loop nesting support.
2265  /// \param D Loop-based construct used in doacross nesting construct.
2266  void emitDoacrossInit(CodeGenFunction &CGF, const OMPLoopDirective &D,
2267  ArrayRef<Expr *> NumIterations) override;
2268 
2269  /// Emit code for doacross ordered directive with 'depend' clause.
2270  /// \param C 'depend' clause with 'sink|source' dependency kind.
2271  void emitDoacrossOrdered(CodeGenFunction &CGF,
2272  const OMPDependClause *C) override;
2273 
2274  /// Translates the native parameter of outlined function if this is required
2275  /// for target.
2276  /// \param FD Field decl from captured record for the parameter.
2277  /// \param NativeParam Parameter itself.
2278  const VarDecl *translateParameter(const FieldDecl *FD,
2279  const VarDecl *NativeParam) const override;
2280 
2281  /// Gets the address of the native argument basing on the address of the
2282  /// target-specific parameter.
2283  /// \param NativeParam Parameter itself.
2284  /// \param TargetParam Corresponding target-specific parameter.
2285  Address getParameterAddress(CodeGenFunction &CGF, const VarDecl *NativeParam,
2286  const VarDecl *TargetParam) const override;
2287 
2288  /// Gets the OpenMP-specific address of the local variable.
2289  Address getAddressOfLocalVariable(CodeGenFunction &CGF,
2290  const VarDecl *VD) override {
2291  return Address::invalid();
2292  }
2293 };
2294 
2295 } // namespace CodeGen
2296 } // namespace clang
2297 
2298 #endif
llvm::SmallVector< NontemporalDeclsSet, 4 > NontemporalDeclsStack
Stack for list of declarations in current context marked as nontemporal.
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:1783
OMPTargetRegionEntryKind
Kind of the target registry entry.
bool hasRequiresUnifiedSharedMemory() const
Return whether the unified_shared_memory has been specified.
virtual Address getAddressOfLocalVariable(CodeGenFunction &CGF, const VarDecl *VD)
Gets the OpenMP-specific address of the local variable.
llvm::SmallVector< LastprivateConditionalData, 4 > LastprivateConditionalStack
Stack for list of addresses of declarations in current context marked as lastprivate conditional...
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::Function *TaskFunction, QualType SharedsTy, Address Shareds, const OMPTaskDataTy &Data)
Scheduling data for loop-based OpenMP directives.
Definition: OpenMPKinds.h:198
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...
llvm::Function * emitReductionFunction(SourceLocation Loc, llvm::Type *ArgsType, ArrayRef< const Expr *> Privates, ArrayRef< const Expr *> LHSExprs, ArrayRef< const Expr *> RHSExprs, ArrayRef< const Expr *> ReductionOps)
Emits reduction function.
void emitSingleReductionCombiner(CodeGenFunction &CGF, const Expr *ReductionOp, const Expr *PrivateRef, const DeclRefExpr *LHS, const DeclRefExpr *RHS)
Emits single reduction combiner.
Specialize PointerLikeTypeTraits to allow LazyGenerationalUpdatePtr to be placed into a PointerUnion...
Definition: Dominators.h:30
Manages list of lastprivate conditional decls for the specified directive.
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.
SmallVector< std::pair< OpenMPDependClauseKind, const Expr * >, 4 > Dependences
virtual void initLastprivateConditionalCounter(CodeGenFunction &CGF, const OMPExecutableDirective &S)
Initializes global counter for lastprivate conditional.
SmallVector< const Expr *, 4 > LastprivateCopies
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:88
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.
llvm::SmallDenseSet< const VarDecl * > DeferredGlobalVariables
List of variables that can become declare target implicitly and, thus, must be emitted.
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 already emitted for the device code and returns true, if it was marked alrea...
struct with the values to be passed to the dispatch runtime function
void emitIfClause(CodeGenFunction &CGF, const Expr *Cond, const RegionCodeGenTy &ThenGen, const RegionCodeGenTy &ElseGen)
Emits code for OpenMP &#39;if&#39; clause using specified CodeGen function.
OffloadEntryInfoDeviceGlobalVar(unsigned Order, OMPTargetGlobalVarEntryKind Flags)
virtual void checkArchForUnifiedAddressing(const OMPRequiresDecl *D)
Perform check on requires decl to ensure that target architecture supports unified addressing...
void scanForTargetRegionsFunctions(const Stmt *S, StringRef ParentName)
Start scanning from statement S and and emit all target regions found along the way.
SmallVector< const Expr *, 4 > ReductionCopies
static const Stmt * getSingleCompoundChild(ASTContext &Ctx, const Stmt *Body)
Checks if the Body is the CompoundStmt and returns its child statement iff there is only one that is ...
DispatchRTInput(llvm::Value *LB, llvm::Value *UB, llvm::Value *Chunk)
Represents a variable declaration or definition.
Definition: Decl.h:820
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...
LangAS
Defines the address space values used by the address space qualifier of QualType. ...
Definition: AddressSpaces.h:25
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:23
void createOffloadEntriesAndInfoMetadata()
Creates all the offload entries in the current compilation unit along with the associated metadata...
llvm::FunctionCallee createForStaticInitFunction(unsigned IVSize, bool IVSigned)
Returns __kmpc_for_static_init_* runtime function for the specified size IVSize and sign IVSigned...
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:3748
llvm::StringMap< llvm::WeakTrackingVH > EmittedNonTargetVariables
List of the global variables with their addresses that should not be emitted for the target...
bool HasEmittedTargetRegion
Flag for keeping track of weather a target region has been emitted.
virtual llvm::Function * emitParallelOutlinedFunction(const OMPExecutableDirective &D, const VarDecl *ThreadIDVar, OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen)
Emits outlined function for the specified OpenMP parallel directive 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 emitTaskLoopCall(CodeGenFunction &CGF, SourceLocation Loc, const OMPLoopDirective &D, llvm::Function *TaskFunction, QualType SharedsTy, Address Shareds, const Expr *IfCond, const OMPTaskDataTy &Data)
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.
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:168
virtual void emitTargetCall(CodeGenFunction &CGF, const OMPExecutableDirective &D, llvm::Function *OutlinedFn, llvm::Value *OutlinedFnID, const Expr *IfCond, const Expr *Device, llvm::function_ref< llvm::Value *(CodeGenFunction &CGF, const OMPLoopDirective &D)> SizeEmitter)
Emit the target offloading code associated with D.
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:2729
llvm::SmallDenseMap< CanonicalDeclPtr< const Decl >, SmallString< 16 > > DeclToUniqeName
Definition: Format.h:2445
bool isNontemporalDecl(const ValueDecl *VD) const
Checks if the VD variable is marked as nontemporal declaration in current context.
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:420
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:151
virtual void emitDoacrossInit(CodeGenFunction &CGF, const OMPLoopDirective &D, ArrayRef< Expr *> NumIterations)
Emit initialization for doacross loop nesting support.
void emitUserDefinedMapper(const OMPDeclareMapperDecl *D, CodeGenFunction *CGF=nullptr)
Emit the function for the user defined mapper construct.
QualType TgtOffloadEntryQTy
Type struct __tgt_offload_entry{ void *addr; // Pointer to the offload entry info.
Manages list of nontemporal decls for the specified directive.
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.
llvm::ArrayType * getKmpCriticalNameTy() const
Get the LLVM type for the critical name.
llvm::StringSet ThreadPrivateWithDefinition
Set of threadprivate variables with the generated initializer.
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.
SmallVector< const Expr *, 4 > FirstprivateCopies
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.
SmallVector< const Expr *, 4 > ReductionOps
llvm::omp::Directive OpenMPDirectiveKind
OpenMP directives.
Definition: OpenMPKinds.h:62
SmallVector< const Expr *, 4 > ReductionVars
virtual bool hasAllocateAttributeForGlobalVar(const VarDecl *VD, LangAS &AS)
Checks if the variable has associated OMPAllocateDeclAttr attribute with the predefined allocator and...
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 bool emitDeclareVariant(GlobalDecl GD, bool IsForDefinition)
Emits the definition of the declare variant function.
void emitTargetNumIterationsCall(CodeGenFunction &CGF, const OMPExecutableDirective &D, llvm::Value *DeviceID, llvm::function_ref< llvm::Value *(CodeGenFunction &CGF, const OMPLoopDirective &D)> SizeEmitter)
Emit code that pushes the trip count of loops associated with constructs &#39;target teams distribute&#39; an...
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...
TargetDataInfo(bool RequiresDevicePointerInfo)
Class intended to support codegen of all kind of the reduction clauses.
llvm::FunctionCallee createRuntimeFunction(unsigned Function)
Returns specified OpenMP runtime function.
This represents &#39;#pragma omp requires...&#39; directive.
Definition: DeclOpenMP.h:345
This represents implicit clause &#39;depend&#39; for the &#39;#pragma omp task&#39; directive.
virtual unsigned getDefaultFirstprivateAddressSpace() const
Returns default address space for the constant firstprivates, 0 by default.
void emitDeferredTargetDecls() const
Emit deferred declare target variables marked for deferred emission.
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)
Represent the declaration of a variable (in which case it is an lvalue) a function (in which case it ...
Definition: Decl.h:619
This represents one expression.
Definition: Expr.h:108
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).
#define V(N, I)
Definition: ASTContext.h:2941
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.
llvm::FunctionCallee createDispatchNextFunction(unsigned IVSize, bool IVSigned)
Returns __kmpc_dispatch_next_* runtime function for the specified size IVSize and sign IVSigned...
CGOpenMPRuntime(CodeGenModule &CGM)
void clearArrayInfo()
Clear information about the data arrays.
llvm::SmallDenseSet< CanonicalDeclPtr< const Decl > > NontemporalDeclsSet
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.
virtual bool isStaticChunked(OpenMPDistScheduleClauseKind ScheduleKind, bool Chunked) const
Check if the specified ScheduleKind is static non-chunked.
virtual void emitProcBindClause(CodeGenFunction &CGF, llvm::omp::ProcBindKind ProcBind, SourceLocation Loc)
Emit call to void __kmpc_push_proc_bind(ident_t *loc, kmp_int32 global_tid, int proc_bind) to generat...
*QualType KmpTaskTQTy
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:40
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...
virtual Address getAddrOfDeclareTargetVar(const VarDecl *VD)
Returns the address of the variable marked as declare target with link clause OR as declare target wi...
Address getAddressOfLocalVariable(CodeGenFunction &CGF, const VarDecl *VD) override
Gets the OpenMP-specific address of the local variable.
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...
__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-base.h:55
Kind
llvm::Function * emitRequiresDirectiveRegFun()
Creates and returns a registration function for when at least one requires directives was used in the...
Encodes a location in the source.
llvm::FunctionCallee createDispatchFiniFunction(unsigned IVSize, bool IVSigned)
Returns __kmpc_dispatch_fini_* runtime function for the specified size IVSize and sign IVSigned...
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.
virtual llvm::Function * emitThreadPrivateVarDefinition(const VarDecl *VD, Address VDAddr, SourceLocation Loc, bool PerformInit, CodeGenFunction *CGF=nullptr)
Emit a code for initialization of threadprivate variable.
This file defines OpenMP nodes for declarative directives.
virtual void emitParallelCall(CodeGenFunction &CGF, SourceLocation Loc, llvm::Function *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...
virtual void emitLastprivateConditionalFinalUpdate(CodeGenFunction &CGF, LValue PrivLVal, const VarDecl *VD, SourceLocation Loc)
Gets the address of the global copy used for lastprivate conditional update, if any.
Maps the expression for the lastprivate variable to the global copy used to store new value because o...
unsigned size() const
Return number of entries defined so far.
virtual void Enter(CodeGenFunction &CGF)
An aligned address.
Definition: Address.h:24
llvm::FunctionCallee createDispatchInitFunction(unsigned IVSize, bool IVSigned)
Returns __kmpc_dispatch_init_* runtime function for the specified size IVSize and sign IVSigned...
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 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.
virtual void emitOutlinedFunctionCall(CodeGenFunction &CGF, SourceLocation Loc, llvm::FunctionCallee OutlinedFn, ArrayRef< llvm::Value *> Args=llvm::None) const
Emits call of the outlined function with the provided arguments, translating these arguments to corre...
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.
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.
Optional< types::ID > Type
Dataflow Directional Tag Classes.
Class provides a way to call simple version of codegen for OpenMP region, or an advanced with possibl...
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::Function *TaskFunction, QualType SharedsTy, Address Shareds, const Expr *IfCond, const OMPTaskDataTy &Data)
static std::string getName(const CallEvent &Call)
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.
llvm::PointerIntPair< llvm::Value *, 1, bool > Priority
void emitUDMapperArrayInitOrDel(CodeGenFunction &MapperCGF, llvm::Value *Handle, llvm::Value *BasePtr, llvm::Value *Ptr, llvm::Value *Size, llvm::Value *MapType, CharUnits ElementSize, llvm::BasicBlock *ExitBB, bool IsInit)
Emit the array initialization or deletion portion for user-defined mapper code generation.
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.
virtual bool isDefaultLocationConstant() const
Check if the default location must be constant.
OffloadEntriesInfoManagerTy OffloadEntriesInfoManager
llvm::DenseSet< CanonicalDeclPtr< const Decl > > AlreadyEmittedTargetDecls
List of the emitted declarations.
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 emitTeamsCall(CodeGenFunction &CGF, const OMPExecutableDirective &D, SourceLocation Loc, llvm::Function *OutlinedFn, ArrayRef< llvm::Value *> CapturedVars)
Emits code for teams call of the OutlinedFn with variables captured in a record which address is stor...
virtual void emitDistributeStaticInit(CodeGenFunction &CGF, SourceLocation Loc, OpenMPDistScheduleClauseKind SchedKind, const StaticRTInput &Values)
virtual StringRef getOutlinedHelperName() const
Get the function name of an outlined region.
bool HasEmittedDeclareTargetRegion
Flag for keeping track of weather a device routine has been emitted.
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 checkAndEmitLastprivateConditional(CodeGenFunction &CGF, const Expr *LHS)
Checks if the provided LVal is lastprivate conditional and emits the code to update the value of the ...
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:85
Entity that registers the offloading constants that were emitted so far.
llvm::Constant * getOrCreateInternalVariable(llvm::Type *Ty, const llvm::Twine &Name, unsigned AddressSpace=0)
Gets (if variable with the given name already exist) or creates internal global variable with the spe...
llvm::function_ref< void(StringRef, const OffloadEntryInfoDeviceGlobalVar &)> OffloadDeviceGlobalVarEntryInfoActTy
Applies action Action on all registered entries.
llvm::Constant * registerTargetFirstprivateCopy(CodeGenFunction &CGF, const VarDecl *VD)
Registers provided target firstprivate variable as global on the target.
virtual void emitTaskgroupRegion(CodeGenFunction &CGF, const RegionCodeGenTy &TaskgroupOpGen, SourceLocation Loc)
Emit a taskgroup region.
virtual void adjustTargetSpecificDataForLambdas(CodeGenFunction &CGF, const OMPExecutableDirective &D) const
Adjust some parameters for the target-based directives, like addresses of the variables captured by r...
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:150
virtual llvm::Function * emitTeamsOutlinedFunction(const OMPExecutableDirective &D, const VarDecl *ThreadIDVar, OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen)
Emits outlined function for the specified OpenMP teams directive D.
ASTImporterLookupTable & LT
This represents &#39;#pragma omp declare mapper ...&#39; directive.
Definition: DeclOpenMP.h:217
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.
llvm::MapVector< CanonicalDeclPtr< const FunctionDecl >, std::pair< GlobalDecl, GlobalDecl > > DeferredVariantFunction
Mapping of the original functions to their variants and original global decl.
A reference to a declared variable, function, enum, etc.
Definition: Expr.h:1171
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.
bool HasRequiresUnifiedSharedMemory
Flag for keeping track of weather a requires unified_shared_memory directive is present.
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
virtual unsigned getDefaultLocationReserved2Flags() const
Returns additional flags that can be stored in reserved_2 field of the default location.
virtual void getDefaultDistScheduleAndChunk(CodeGenFunction &CGF, const OMPLoopDirective &S, OpenMPDistScheduleClauseKind &ScheduleKind, llvm::Value *&Chunk) const
Choose default schedule type and chunk value for the dist_schedule clause.
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...
virtual void getDefaultScheduleAndChunk(CodeGenFunction &CGF, const OMPLoopDirective &S, OpenMPScheduleClauseKind &ScheduleKind, const Expr *&ChunkExpr) const
Choose default schedule type and chunk value for the schedule clause.
llvm::StringSet DeclareTargetWithDefinition
Set of declare target variables with the generated initializer.
virtual llvm::Function * 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.
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.