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