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