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