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