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