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