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