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