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