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