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