]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/CodeGen/CGOpenMPRuntime.h
Vendor import of clang trunk r308421:
[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 expresion.
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 true if the initialization of the reduction item uses initializer
195   /// from declare reduction construct.
196   bool usesReductionInitializer(unsigned N) const;
197 };
198
199 class CGOpenMPRuntime {
200 protected:
201   CodeGenModule &CGM;
202
203   /// \brief Creates offloading entry for the provided entry ID \a ID,
204   /// address \a Addr, size \a Size, and flags \a Flags.
205   virtual void createOffloadEntry(llvm::Constant *ID, llvm::Constant *Addr,
206                                   uint64_t Size, int32_t Flags = 0);
207
208   /// \brief Helper to emit outlined function for 'target' directive.
209   /// \param D Directive to emit.
210   /// \param ParentName Name of the function that encloses the target region.
211   /// \param OutlinedFn Outlined function value to be defined by this call.
212   /// \param OutlinedFnID Outlined function ID value to be defined by this call.
213   /// \param IsOffloadEntry True if the outlined function is an offload entry.
214   /// \param CodeGen Lambda codegen specific to an accelerator device.
215   /// An outlined function may not be an entry if, e.g. the if clause always
216   /// evaluates to false.
217   virtual void emitTargetOutlinedFunctionHelper(const OMPExecutableDirective &D,
218                                                 StringRef ParentName,
219                                                 llvm::Function *&OutlinedFn,
220                                                 llvm::Constant *&OutlinedFnID,
221                                                 bool IsOffloadEntry,
222                                                 const RegionCodeGenTy &CodeGen);
223
224   /// \brief Emits code for OpenMP 'if' clause using specified \a CodeGen
225   /// function. Here is the logic:
226   /// if (Cond) {
227   ///   ThenGen();
228   /// } else {
229   ///   ElseGen();
230   /// }
231   void emitOMPIfClause(CodeGenFunction &CGF, const Expr *Cond,
232                        const RegionCodeGenTy &ThenGen,
233                        const RegionCodeGenTy &ElseGen);
234
235   /// \brief Emits object of ident_t type with info for source location.
236   /// \param Flags Flags for OpenMP location.
237   ///
238   llvm::Value *emitUpdateLocation(CodeGenFunction &CGF, SourceLocation Loc,
239                                   unsigned Flags = 0);
240
241   /// \brief Returns pointer to ident_t type.
242   llvm::Type *getIdentTyPointerTy();
243
244   /// \brief Gets thread id value for the current thread.
245   ///
246   llvm::Value *getThreadID(CodeGenFunction &CGF, SourceLocation Loc);
247
248   /// \brief Get the function name of an outlined region.
249   //  The name can be customized depending on the target.
250   //
251   virtual StringRef getOutlinedHelperName() const { return ".omp_outlined."; }
252
253 private:
254   /// \brief Default const ident_t object used for initialization of all other
255   /// ident_t objects.
256   llvm::Constant *DefaultOpenMPPSource = nullptr;
257   /// \brief Map of flags and corresponding default locations.
258   typedef llvm::DenseMap<unsigned, llvm::Value *> OpenMPDefaultLocMapTy;
259   OpenMPDefaultLocMapTy OpenMPDefaultLocMap;
260   Address getOrCreateDefaultLocation(unsigned Flags);
261
262   llvm::StructType *IdentTy = nullptr;
263   /// \brief Map for SourceLocation and OpenMP runtime library debug locations.
264   typedef llvm::DenseMap<unsigned, llvm::Value *> OpenMPDebugLocMapTy;
265   OpenMPDebugLocMapTy OpenMPDebugLocMap;
266   /// \brief The type for a microtask which gets passed to __kmpc_fork_call().
267   /// Original representation is:
268   /// typedef void (kmpc_micro)(kmp_int32 global_tid, kmp_int32 bound_tid,...);
269   llvm::FunctionType *Kmpc_MicroTy = nullptr;
270   /// \brief Stores debug location and ThreadID for the function.
271   struct DebugLocThreadIdTy {
272     llvm::Value *DebugLoc;
273     llvm::Value *ThreadID;
274   };
275   /// \brief Map of local debug location, ThreadId and functions.
276   typedef llvm::DenseMap<llvm::Function *, DebugLocThreadIdTy>
277       OpenMPLocThreadIDMapTy;
278   OpenMPLocThreadIDMapTy OpenMPLocThreadIDMap;
279   /// Map of UDRs and corresponding combiner/initializer.
280   typedef llvm::DenseMap<const OMPDeclareReductionDecl *,
281                          std::pair<llvm::Function *, llvm::Function *>>
282       UDRMapTy;
283   UDRMapTy UDRMap;
284   /// Map of functions and locally defined UDRs.
285   typedef llvm::DenseMap<llvm::Function *,
286                          SmallVector<const OMPDeclareReductionDecl *, 4>>
287       FunctionUDRMapTy;
288   FunctionUDRMapTy FunctionUDRMap;
289   IdentifierInfo *In = nullptr;
290   IdentifierInfo *Out = nullptr;
291   IdentifierInfo *Priv = nullptr;
292   IdentifierInfo *Orig = nullptr;
293   /// \brief Type kmp_critical_name, originally defined as typedef kmp_int32
294   /// kmp_critical_name[8];
295   llvm::ArrayType *KmpCriticalNameTy;
296   /// \brief An ordered map of auto-generated variables to their unique names.
297   /// It stores variables with the following names: 1) ".gomp_critical_user_" +
298   /// <critical_section_name> + ".var" for "omp critical" directives; 2)
299   /// <mangled_name_for_global_var> + ".cache." for cache for threadprivate
300   /// variables.
301   llvm::StringMap<llvm::AssertingVH<llvm::Constant>, llvm::BumpPtrAllocator>
302       InternalVars;
303   /// \brief Type typedef kmp_int32 (* kmp_routine_entry_t)(kmp_int32, void *);
304   llvm::Type *KmpRoutineEntryPtrTy = nullptr;
305   QualType KmpRoutineEntryPtrQTy;
306   /// \brief Type typedef struct kmp_task {
307   ///    void *              shareds; /**< pointer to block of pointers to
308   ///    shared vars   */
309   ///    kmp_routine_entry_t routine; /**< pointer to routine to call for
310   ///    executing task */
311   ///    kmp_int32           part_id; /**< part id for the task */
312   ///    kmp_routine_entry_t destructors; /* pointer to function to invoke
313   ///    deconstructors of firstprivate C++ objects */
314   /// } kmp_task_t;
315   QualType KmpTaskTQTy;
316   /// \brief Type typedef struct kmp_depend_info {
317   ///    kmp_intptr_t               base_addr;
318   ///    size_t                     len;
319   ///    struct {
320   ///             bool                   in:1;
321   ///             bool                   out:1;
322   ///    } flags;
323   /// } kmp_depend_info_t;
324   QualType KmpDependInfoTy;
325   /// struct kmp_dim {  // loop bounds info casted to kmp_int64
326   ///  kmp_int64 lo; // lower
327   ///  kmp_int64 up; // upper
328   ///  kmp_int64 st; // stride
329   /// };
330   QualType KmpDimTy;
331   /// \brief Type struct __tgt_offload_entry{
332   ///   void      *addr;       // Pointer to the offload entry info.
333   ///                          // (function or global)
334   ///   char      *name;       // Name of the function or global.
335   ///   size_t     size;       // Size of the entry info (0 if it a function).
336   /// };
337   QualType TgtOffloadEntryQTy;
338   /// struct __tgt_device_image{
339   /// void   *ImageStart;       // Pointer to the target code start.
340   /// void   *ImageEnd;         // Pointer to the target code end.
341   /// // We also add the host entries to the device image, as it may be useful
342   /// // for the target runtime to have access to that information.
343   /// __tgt_offload_entry  *EntriesBegin;   // Begin of the table with all
344   ///                                       // the entries.
345   /// __tgt_offload_entry  *EntriesEnd;     // End of the table with all the
346   ///                                       // entries (non inclusive).
347   /// };
348   QualType TgtDeviceImageQTy;
349   /// struct __tgt_bin_desc{
350   ///   int32_t              NumDevices;      // Number of devices supported.
351   ///   __tgt_device_image   *DeviceImages;   // Arrays of device images
352   ///                                         // (one per device).
353   ///   __tgt_offload_entry  *EntriesBegin;   // Begin of the table with all the
354   ///                                         // entries.
355   ///   __tgt_offload_entry  *EntriesEnd;     // End of the table with all the
356   ///                                         // entries (non inclusive).
357   /// };
358   QualType TgtBinaryDescriptorQTy;
359   /// \brief Entity that registers the offloading constants that were emitted so
360   /// far.
361   class OffloadEntriesInfoManagerTy {
362     CodeGenModule &CGM;
363
364     /// \brief Number of entries registered so far.
365     unsigned OffloadingEntriesNum;
366
367   public:
368     /// Base class of the entries info.
369     class OffloadEntryInfo {
370     public:
371       /// Kind of a given entry. Currently, only target regions are
372       /// supported.
373       enum OffloadingEntryInfoKinds : unsigned {
374         // Entry is a target region.
375         OFFLOAD_ENTRY_INFO_TARGET_REGION = 0,
376         // Invalid entry info.
377         OFFLOAD_ENTRY_INFO_INVALID = ~0u
378       };
379
380       OffloadEntryInfo()
381           : Flags(0), Order(~0u), Kind(OFFLOAD_ENTRY_INFO_INVALID) {}
382       explicit OffloadEntryInfo(OffloadingEntryInfoKinds Kind, unsigned Order,
383                                 int32_t Flags)
384           : Flags(Flags), Order(Order), Kind(Kind) {}
385
386       bool isValid() const { return Order != ~0u; }
387       unsigned getOrder() const { return Order; }
388       OffloadingEntryInfoKinds getKind() const { return Kind; }
389       int32_t getFlags() const { return Flags; }
390       void setFlags(int32_t NewFlags) { Flags = NewFlags; }
391       static bool classof(const OffloadEntryInfo *Info) { return true; }
392
393     private:
394       /// Flags associated with the device global.
395       int32_t Flags;
396
397       /// Order this entry was emitted.
398       unsigned Order;
399
400       OffloadingEntryInfoKinds Kind;
401     };
402
403     /// \brief Return true if a there are no entries defined.
404     bool empty() const;
405     /// \brief Return number of entries defined so far.
406     unsigned size() const { return OffloadingEntriesNum; }
407     OffloadEntriesInfoManagerTy(CodeGenModule &CGM)
408         : CGM(CGM), OffloadingEntriesNum(0) {}
409
410     ///
411     /// Target region entries related.
412     ///
413     /// \brief Target region entries info.
414     class OffloadEntryInfoTargetRegion : public OffloadEntryInfo {
415       // \brief Address of the entity that has to be mapped for offloading.
416       llvm::Constant *Addr;
417       // \brief Address that can be used as the ID of the entry.
418       llvm::Constant *ID;
419
420     public:
421       OffloadEntryInfoTargetRegion()
422           : OffloadEntryInfo(OFFLOAD_ENTRY_INFO_TARGET_REGION, ~0u,
423                              /*Flags=*/0),
424             Addr(nullptr), ID(nullptr) {}
425       explicit OffloadEntryInfoTargetRegion(unsigned Order,
426                                             llvm::Constant *Addr,
427                                             llvm::Constant *ID, int32_t Flags)
428           : OffloadEntryInfo(OFFLOAD_ENTRY_INFO_TARGET_REGION, Order, Flags),
429             Addr(Addr), ID(ID) {}
430
431       llvm::Constant *getAddress() const { return Addr; }
432       llvm::Constant *getID() const { return ID; }
433       void setAddress(llvm::Constant *V) {
434         assert(!Addr && "Address as been set before!");
435         Addr = V;
436       }
437       void setID(llvm::Constant *V) {
438         assert(!ID && "ID as been set before!");
439         ID = V;
440       }
441       static bool classof(const OffloadEntryInfo *Info) {
442         return Info->getKind() == OFFLOAD_ENTRY_INFO_TARGET_REGION;
443       }
444     };
445     /// \brief Initialize target region entry.
446     void initializeTargetRegionEntryInfo(unsigned DeviceID, unsigned FileID,
447                                          StringRef ParentName, unsigned LineNum,
448                                          unsigned Order);
449     /// \brief Register target region entry.
450     void registerTargetRegionEntryInfo(unsigned DeviceID, unsigned FileID,
451                                        StringRef ParentName, unsigned LineNum,
452                                        llvm::Constant *Addr, llvm::Constant *ID,
453                                        int32_t Flags);
454     /// \brief Return true if a target region entry with the provided
455     /// information exists.
456     bool hasTargetRegionEntryInfo(unsigned DeviceID, unsigned FileID,
457                                   StringRef ParentName, unsigned LineNum) const;
458     /// brief Applies action \a Action on all registered entries.
459     typedef llvm::function_ref<void(unsigned, unsigned, StringRef, unsigned,
460                                     OffloadEntryInfoTargetRegion &)>
461         OffloadTargetRegionEntryInfoActTy;
462     void actOnTargetRegionEntriesInfo(
463         const OffloadTargetRegionEntryInfoActTy &Action);
464
465   private:
466     // Storage for target region entries kind. The storage is to be indexed by
467     // file ID, device ID, parent function name and line number.
468     typedef llvm::DenseMap<unsigned, OffloadEntryInfoTargetRegion>
469         OffloadEntriesTargetRegionPerLine;
470     typedef llvm::StringMap<OffloadEntriesTargetRegionPerLine>
471         OffloadEntriesTargetRegionPerParentName;
472     typedef llvm::DenseMap<unsigned, OffloadEntriesTargetRegionPerParentName>
473         OffloadEntriesTargetRegionPerFile;
474     typedef llvm::DenseMap<unsigned, OffloadEntriesTargetRegionPerFile>
475         OffloadEntriesTargetRegionPerDevice;
476     typedef OffloadEntriesTargetRegionPerDevice OffloadEntriesTargetRegionTy;
477     OffloadEntriesTargetRegionTy OffloadEntriesTargetRegion;
478   };
479   OffloadEntriesInfoManagerTy OffloadEntriesInfoManager;
480
481   /// \brief Creates and registers offloading binary descriptor for the current
482   /// compilation unit. The function that does the registration is returned.
483   llvm::Function *createOffloadingBinaryDescriptorRegistration();
484
485   /// \brief Creates all the offload entries in the current compilation unit
486   /// along with the associated metadata.
487   void createOffloadEntriesAndInfoMetadata();
488
489   /// \brief Loads all the offload entries information from the host IR
490   /// metadata.
491   void loadOffloadInfoMetadata();
492
493   /// \brief Returns __tgt_offload_entry type.
494   QualType getTgtOffloadEntryQTy();
495
496   /// \brief Returns __tgt_device_image type.
497   QualType getTgtDeviceImageQTy();
498
499   /// \brief Returns __tgt_bin_desc type.
500   QualType getTgtBinaryDescriptorQTy();
501
502   /// \brief Start scanning from statement \a S and and emit all target regions
503   /// found along the way.
504   /// \param S Starting statement.
505   /// \param ParentName Name of the function declaration that is being scanned.
506   void scanForTargetRegionsFunctions(const Stmt *S, StringRef ParentName);
507
508   /// \brief Build type kmp_routine_entry_t (if not built yet).
509   void emitKmpRoutineEntryT(QualType KmpInt32Ty);
510
511   /// \brief Returns pointer to kmpc_micro type.
512   llvm::Type *getKmpc_MicroPointerTy();
513
514   /// \brief Returns specified OpenMP runtime function.
515   /// \param Function OpenMP runtime function.
516   /// \return Specified function.
517   llvm::Constant *createRuntimeFunction(unsigned Function);
518
519   /// \brief Returns __kmpc_for_static_init_* runtime function for the specified
520   /// size \a IVSize and sign \a IVSigned.
521   llvm::Constant *createForStaticInitFunction(unsigned IVSize, bool IVSigned);
522
523   /// \brief Returns __kmpc_dispatch_init_* runtime function for the specified
524   /// size \a IVSize and sign \a IVSigned.
525   llvm::Constant *createDispatchInitFunction(unsigned IVSize, bool IVSigned);
526
527   /// \brief Returns __kmpc_dispatch_next_* runtime function for the specified
528   /// size \a IVSize and sign \a IVSigned.
529   llvm::Constant *createDispatchNextFunction(unsigned IVSize, bool IVSigned);
530
531   /// \brief Returns __kmpc_dispatch_fini_* runtime function for the specified
532   /// size \a IVSize and sign \a IVSigned.
533   llvm::Constant *createDispatchFiniFunction(unsigned IVSize, bool IVSigned);
534
535   /// \brief If the specified mangled name is not in the module, create and
536   /// return threadprivate cache object. This object is a pointer's worth of
537   /// storage that's reserved for use by the OpenMP runtime.
538   /// \param VD Threadprivate variable.
539   /// \return Cache variable for the specified threadprivate.
540   llvm::Constant *getOrCreateThreadPrivateCache(const VarDecl *VD);
541
542   /// \brief Emits address of the word in a memory where current thread id is
543   /// stored.
544   virtual Address emitThreadIDAddress(CodeGenFunction &CGF, SourceLocation Loc);
545
546   /// \brief Gets (if variable with the given name already exist) or creates
547   /// internal global variable with the specified Name. The created variable has
548   /// linkage CommonLinkage by default and is initialized by null value.
549   /// \param Ty Type of the global variable. If it is exist already the type
550   /// must be the same.
551   /// \param Name Name of the variable.
552   llvm::Constant *getOrCreateInternalVariable(llvm::Type *Ty,
553                                               const llvm::Twine &Name);
554
555   /// \brief Set of threadprivate variables with the generated initializer.
556   llvm::SmallPtrSet<const VarDecl *, 4> ThreadPrivateWithDefinition;
557
558   /// \brief Emits initialization code for the threadprivate variables.
559   /// \param VDAddr Address of the global variable \a VD.
560   /// \param Ctor Pointer to a global init function for \a VD.
561   /// \param CopyCtor Pointer to a global copy function for \a VD.
562   /// \param Dtor Pointer to a global destructor function for \a VD.
563   /// \param Loc Location of threadprivate declaration.
564   void emitThreadPrivateVarInit(CodeGenFunction &CGF, Address VDAddr,
565                                 llvm::Value *Ctor, llvm::Value *CopyCtor,
566                                 llvm::Value *Dtor, SourceLocation Loc);
567
568   /// \brief Returns corresponding lock object for the specified critical region
569   /// name. If the lock object does not exist it is created, otherwise the
570   /// reference to the existing copy is returned.
571   /// \param CriticalName Name of the critical region.
572   ///
573   llvm::Value *getCriticalRegionLock(StringRef CriticalName);
574
575   struct TaskResultTy {
576     llvm::Value *NewTask = nullptr;
577     llvm::Value *TaskEntry = nullptr;
578     llvm::Value *NewTaskNewTaskTTy = nullptr;
579     LValue TDBase;
580     RecordDecl *KmpTaskTQTyRD = nullptr;
581     llvm::Value *TaskDupFn = nullptr;
582   };
583   /// Emit task region for the task directive. The task region is emitted in
584   /// several steps:
585   /// 1. Emit a call to kmp_task_t *__kmpc_omp_task_alloc(ident_t *, kmp_int32
586   /// gtid, kmp_int32 flags, size_t sizeof_kmp_task_t, size_t sizeof_shareds,
587   /// kmp_routine_entry_t *task_entry). Here task_entry is a pointer to the
588   /// function:
589   /// kmp_int32 .omp_task_entry.(kmp_int32 gtid, kmp_task_t *tt) {
590   ///   TaskFunction(gtid, tt->part_id, tt->shareds);
591   ///   return 0;
592   /// }
593   /// 2. Copy a list of shared variables to field shareds of the resulting
594   /// structure kmp_task_t returned by the previous call (if any).
595   /// 3. Copy a pointer to destructions function to field destructions of the
596   /// resulting structure kmp_task_t.
597   /// \param D Current task directive.
598   /// \param TaskFunction An LLVM function with type void (*)(i32 /*gtid*/, i32
599   /// /*part_id*/, captured_struct */*__context*/);
600   /// \param SharedsTy A type which contains references the shared variables.
601   /// \param Shareds Context with the list of shared variables from the \p
602   /// TaskFunction.
603   /// \param Data Additional data for task generation like tiednsee, final
604   /// state, list of privates etc.
605   TaskResultTy emitTaskInit(CodeGenFunction &CGF, SourceLocation Loc,
606                             const OMPExecutableDirective &D,
607                             llvm::Value *TaskFunction, QualType SharedsTy,
608                             Address Shareds, const OMPTaskDataTy &Data);
609
610 public:
611   explicit CGOpenMPRuntime(CodeGenModule &CGM);
612   virtual ~CGOpenMPRuntime() {}
613   virtual void clear();
614
615   /// Emit code for the specified user defined reduction construct.
616   virtual void emitUserDefinedReduction(CodeGenFunction *CGF,
617                                         const OMPDeclareReductionDecl *D);
618   /// Get combiner/initializer for the specified user-defined reduction, if any.
619   virtual std::pair<llvm::Function *, llvm::Function *>
620   getUserDefinedReduction(const OMPDeclareReductionDecl *D);
621
622   /// \brief Emits outlined function for the specified OpenMP parallel directive
623   /// \a D. This outlined function has type void(*)(kmp_int32 *ThreadID,
624   /// kmp_int32 BoundID, struct context_vars*).
625   /// \param D OpenMP directive.
626   /// \param ThreadIDVar Variable for thread id in the current OpenMP region.
627   /// \param InnermostKind Kind of innermost directive (for simple directives it
628   /// is a directive itself, for combined - its innermost directive).
629   /// \param CodeGen Code generation sequence for the \a D directive.
630   virtual llvm::Value *emitParallelOutlinedFunction(
631       const OMPExecutableDirective &D, const VarDecl *ThreadIDVar,
632       OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen);
633
634   /// \brief Emits outlined function for the specified OpenMP teams directive
635   /// \a D. This outlined function has type void(*)(kmp_int32 *ThreadID,
636   /// kmp_int32 BoundID, struct context_vars*).
637   /// \param D OpenMP directive.
638   /// \param ThreadIDVar Variable for thread id in the current OpenMP region.
639   /// \param InnermostKind Kind of innermost directive (for simple directives it
640   /// is a directive itself, for combined - its innermost directive).
641   /// \param CodeGen Code generation sequence for the \a D directive.
642   virtual llvm::Value *emitTeamsOutlinedFunction(
643       const OMPExecutableDirective &D, const VarDecl *ThreadIDVar,
644       OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen);
645
646   /// \brief Emits outlined function for the OpenMP task directive \a D. This
647   /// outlined function has type void(*)(kmp_int32 ThreadID, struct task_t*
648   /// TaskT).
649   /// \param D OpenMP directive.
650   /// \param ThreadIDVar Variable for thread id in the current OpenMP region.
651   /// \param PartIDVar Variable for partition id in the current OpenMP untied
652   /// task region.
653   /// \param TaskTVar Variable for task_t argument.
654   /// \param InnermostKind Kind of innermost directive (for simple directives it
655   /// is a directive itself, for combined - its innermost directive).
656   /// \param CodeGen Code generation sequence for the \a D directive.
657   /// \param Tied true if task is generated for tied task, false otherwise.
658   /// \param NumberOfParts Number of parts in untied task. Ignored for tied
659   /// tasks.
660   ///
661   virtual llvm::Value *emitTaskOutlinedFunction(
662       const OMPExecutableDirective &D, const VarDecl *ThreadIDVar,
663       const VarDecl *PartIDVar, const VarDecl *TaskTVar,
664       OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen,
665       bool Tied, unsigned &NumberOfParts);
666
667   /// \brief Cleans up references to the objects in finished function.
668   ///
669   void functionFinished(CodeGenFunction &CGF);
670
671   /// \brief Emits code for parallel or serial call of the \a OutlinedFn with
672   /// variables captured in a record which address is stored in \a
673   /// CapturedStruct.
674   /// \param OutlinedFn Outlined function to be run in parallel threads. Type of
675   /// this function is void(*)(kmp_int32 *, kmp_int32, struct context_vars*).
676   /// \param CapturedVars A pointer to the record with the references to
677   /// variables used in \a OutlinedFn function.
678   /// \param IfCond Condition in the associated 'if' clause, if it was
679   /// specified, nullptr otherwise.
680   ///
681   virtual void emitParallelCall(CodeGenFunction &CGF, SourceLocation Loc,
682                                 llvm::Value *OutlinedFn,
683                                 ArrayRef<llvm::Value *> CapturedVars,
684                                 const Expr *IfCond);
685
686   /// \brief Emits a critical region.
687   /// \param CriticalName Name of the critical region.
688   /// \param CriticalOpGen Generator for the statement associated with the given
689   /// critical region.
690   /// \param Hint Value of the 'hint' clause (optional).
691   virtual void emitCriticalRegion(CodeGenFunction &CGF, StringRef CriticalName,
692                                   const RegionCodeGenTy &CriticalOpGen,
693                                   SourceLocation Loc,
694                                   const Expr *Hint = nullptr);
695
696   /// \brief Emits a master region.
697   /// \param MasterOpGen Generator for the statement associated with the given
698   /// master region.
699   virtual void emitMasterRegion(CodeGenFunction &CGF,
700                                 const RegionCodeGenTy &MasterOpGen,
701                                 SourceLocation Loc);
702
703   /// \brief Emits code for a taskyield directive.
704   virtual void emitTaskyieldCall(CodeGenFunction &CGF, SourceLocation Loc);
705
706   /// \brief Emit a taskgroup region.
707   /// \param TaskgroupOpGen Generator for the statement associated with the
708   /// given taskgroup region.
709   virtual void emitTaskgroupRegion(CodeGenFunction &CGF,
710                                    const RegionCodeGenTy &TaskgroupOpGen,
711                                    SourceLocation Loc);
712
713   /// \brief Emits a single region.
714   /// \param SingleOpGen Generator for the statement associated with the given
715   /// single region.
716   virtual void emitSingleRegion(CodeGenFunction &CGF,
717                                 const RegionCodeGenTy &SingleOpGen,
718                                 SourceLocation Loc,
719                                 ArrayRef<const Expr *> CopyprivateVars,
720                                 ArrayRef<const Expr *> DestExprs,
721                                 ArrayRef<const Expr *> SrcExprs,
722                                 ArrayRef<const Expr *> AssignmentOps);
723
724   /// \brief Emit an ordered region.
725   /// \param OrderedOpGen Generator for the statement associated with the given
726   /// ordered region.
727   virtual void emitOrderedRegion(CodeGenFunction &CGF,
728                                  const RegionCodeGenTy &OrderedOpGen,
729                                  SourceLocation Loc, bool IsThreads);
730
731   /// \brief Emit an implicit/explicit barrier for OpenMP threads.
732   /// \param Kind Directive for which this implicit barrier call must be
733   /// generated. Must be OMPD_barrier for explicit barrier generation.
734   /// \param EmitChecks true if need to emit checks for cancellation barriers.
735   /// \param ForceSimpleCall true simple barrier call must be emitted, false if
736   /// runtime class decides which one to emit (simple or with cancellation
737   /// checks).
738   ///
739   virtual void emitBarrierCall(CodeGenFunction &CGF, SourceLocation Loc,
740                                OpenMPDirectiveKind Kind,
741                                bool EmitChecks = true,
742                                bool ForceSimpleCall = false);
743
744   /// \brief Check if the specified \a ScheduleKind is static non-chunked.
745   /// This kind of worksharing directive is emitted without outer loop.
746   /// \param ScheduleKind Schedule kind specified in the 'schedule' clause.
747   /// \param Chunked True if chunk is specified in the clause.
748   ///
749   virtual bool isStaticNonchunked(OpenMPScheduleClauseKind ScheduleKind,
750                                   bool Chunked) const;
751
752   /// \brief Check if the specified \a ScheduleKind is static non-chunked.
753   /// This kind of distribute directive is emitted without outer loop.
754   /// \param ScheduleKind Schedule kind specified in the 'dist_schedule' clause.
755   /// \param Chunked True if chunk is specified in the clause.
756   ///
757   virtual bool isStaticNonchunked(OpenMPDistScheduleClauseKind ScheduleKind,
758                                   bool Chunked) const;
759
760   /// \brief Check if the specified \a ScheduleKind is dynamic.
761   /// This kind of worksharing directive is emitted without outer loop.
762   /// \param ScheduleKind Schedule Kind specified in the 'schedule' clause.
763   ///
764   virtual bool isDynamic(OpenMPScheduleClauseKind ScheduleKind) const;
765
766   /// struct with the values to be passed to the dispatch runtime function
767   struct DispatchRTInput {
768     /// Loop lower bound
769     llvm::Value *LB = nullptr;
770     /// Loop upper bound
771     llvm::Value *UB = nullptr;
772     /// Chunk size specified using 'schedule' clause (nullptr if chunk
773     /// was not specified)
774     llvm::Value *Chunk = nullptr;
775     DispatchRTInput() = default;
776     DispatchRTInput(llvm::Value *LB, llvm::Value *UB, llvm::Value *Chunk)
777         : LB(LB), UB(UB), Chunk(Chunk) {}
778   };
779
780   /// Call the appropriate runtime routine to initialize it before start
781   /// of loop.
782
783   /// This is used for non static scheduled types and when the ordered
784   /// clause is present on the loop construct.
785   /// Depending on the loop schedule, it is necessary to call some runtime
786   /// routine before start of the OpenMP loop to get the loop upper / lower
787   /// bounds \a LB and \a UB and stride \a ST.
788   ///
789   /// \param CGF Reference to current CodeGenFunction.
790   /// \param Loc Clang source location.
791   /// \param ScheduleKind Schedule kind, specified by the 'schedule' clause.
792   /// \param IVSize Size of the iteration variable in bits.
793   /// \param IVSigned Sign of the iteration variable.
794   /// \param Ordered true if loop is ordered, false otherwise.
795   /// \param DispatchValues struct containing llvm values for lower bound, upper
796   /// bound, and chunk expression.
797   /// For the default (nullptr) value, the chunk 1 will be used.
798   ///
799   virtual void emitForDispatchInit(CodeGenFunction &CGF, SourceLocation Loc,
800                                    const OpenMPScheduleTy &ScheduleKind,
801                                    unsigned IVSize, bool IVSigned, bool Ordered,
802                                    const DispatchRTInput &DispatchValues);
803
804   /// \brief Call the appropriate runtime routine to initialize it before start
805   /// of loop.
806   ///
807   /// This is used only in case of static schedule, when the user did not
808   /// specify a ordered clause on the loop construct.
809   /// Depending on the loop schedule, it is necessary to call some runtime
810   /// routine before start of the OpenMP loop to get the loop upper / lower
811   /// bounds \a LB and \a UB and stride \a ST.
812   ///
813   /// \param CGF Reference to current CodeGenFunction.
814   /// \param Loc Clang source location.
815   /// \param ScheduleKind Schedule kind, specified by the 'schedule' clause.
816   /// \param IVSize Size of the iteration variable in bits.
817   /// \param IVSigned Sign of the iteration variable.
818   /// \param Ordered true if loop is ordered, false otherwise.
819   /// \param IL Address of the output variable in which the flag of the
820   /// last iteration is returned.
821   /// \param LB Address of the output variable in which the lower iteration
822   /// number is returned.
823   /// \param UB Address of the output variable in which the upper iteration
824   /// number is returned.
825   /// \param ST Address of the output variable in which the stride value is
826   /// returned necessary to generated the static_chunked scheduled loop.
827   /// \param Chunk Value of the chunk for the static_chunked scheduled loop.
828   /// For the default (nullptr) value, the chunk 1 will be used.
829   ///
830   virtual void emitForStaticInit(CodeGenFunction &CGF, SourceLocation Loc,
831                                  const OpenMPScheduleTy &ScheduleKind,
832                                  unsigned IVSize, bool IVSigned, bool Ordered,
833                                  Address IL, Address LB, Address UB, Address ST,
834                                  llvm::Value *Chunk = nullptr);
835
836   ///
837   /// \param CGF Reference to current CodeGenFunction.
838   /// \param Loc Clang source location.
839   /// \param SchedKind Schedule kind, specified by the 'dist_schedule' clause.
840   /// \param IVSize Size of the iteration variable in bits.
841   /// \param IVSigned Sign of the iteration variable.
842   /// \param Ordered true if loop is ordered, false otherwise.
843   /// \param IL Address of the output variable in which the flag of the
844   /// last iteration is returned.
845   /// \param LB Address of the output variable in which the lower iteration
846   /// number is returned.
847   /// \param UB Address of the output variable in which the upper iteration
848   /// number is returned.
849   /// \param ST Address of the output variable in which the stride value is
850   /// returned necessary to generated the static_chunked scheduled loop.
851   /// \param Chunk Value of the chunk for the static_chunked scheduled loop.
852   /// For the default (nullptr) value, the chunk 1 will be used.
853   ///
854   virtual void emitDistributeStaticInit(CodeGenFunction &CGF, SourceLocation Loc,
855                                         OpenMPDistScheduleClauseKind SchedKind,
856                                         unsigned IVSize, bool IVSigned,
857                                         bool Ordered, Address IL, Address LB,
858                                         Address UB, Address ST,
859                                         llvm::Value *Chunk = nullptr);
860
861   /// \brief Call the appropriate runtime routine to notify that we finished
862   /// iteration of the ordered loop with the dynamic scheduling.
863   ///
864   /// \param CGF Reference to current CodeGenFunction.
865   /// \param Loc Clang source location.
866   /// \param IVSize Size of the iteration variable in bits.
867   /// \param IVSigned Sign of the iteration variable.
868   ///
869   virtual void emitForOrderedIterationEnd(CodeGenFunction &CGF,
870                                           SourceLocation Loc, unsigned IVSize,
871                                           bool IVSigned);
872
873   /// \brief Call the appropriate runtime routine to notify that we finished
874   /// all the work with current loop.
875   ///
876   /// \param CGF Reference to current CodeGenFunction.
877   /// \param Loc Clang source location.
878   ///
879   virtual void emitForStaticFinish(CodeGenFunction &CGF, SourceLocation Loc);
880
881   /// Call __kmpc_dispatch_next(
882   ///          ident_t *loc, kmp_int32 tid, kmp_int32 *p_lastiter,
883   ///          kmp_int[32|64] *p_lower, kmp_int[32|64] *p_upper,
884   ///          kmp_int[32|64] *p_stride);
885   /// \param IVSize Size of the iteration variable in bits.
886   /// \param IVSigned Sign of the iteration variable.
887   /// \param IL Address of the output variable in which the flag of the
888   /// last iteration is returned.
889   /// \param LB Address of the output variable in which the lower iteration
890   /// number is returned.
891   /// \param UB Address of the output variable in which the upper iteration
892   /// number is returned.
893   /// \param ST Address of the output variable in which the stride value is
894   /// returned.
895   virtual llvm::Value *emitForNext(CodeGenFunction &CGF, SourceLocation Loc,
896                                    unsigned IVSize, bool IVSigned,
897                                    Address IL, Address LB,
898                                    Address UB, Address ST);
899
900   /// \brief Emits call to void __kmpc_push_num_threads(ident_t *loc, kmp_int32
901   /// global_tid, kmp_int32 num_threads) to generate code for 'num_threads'
902   /// clause.
903   /// \param NumThreads An integer value of threads.
904   virtual void emitNumThreadsClause(CodeGenFunction &CGF,
905                                     llvm::Value *NumThreads,
906                                     SourceLocation Loc);
907
908   /// \brief Emit call to void __kmpc_push_proc_bind(ident_t *loc, kmp_int32
909   /// global_tid, int proc_bind) to generate code for 'proc_bind' clause.
910   virtual void emitProcBindClause(CodeGenFunction &CGF,
911                                   OpenMPProcBindClauseKind ProcBind,
912                                   SourceLocation Loc);
913
914   /// \brief Returns address of the threadprivate variable for the current
915   /// thread.
916   /// \param VD Threadprivate variable.
917   /// \param VDAddr Address of the global variable \a VD.
918   /// \param Loc Location of the reference to threadprivate var.
919   /// \return Address of the threadprivate variable for the current thread.
920   virtual Address getAddrOfThreadPrivate(CodeGenFunction &CGF,
921                                          const VarDecl *VD,
922                                          Address VDAddr,
923                                          SourceLocation Loc);
924
925   /// \brief Emit a code for initialization of threadprivate variable. It emits
926   /// a call to runtime library which adds initial value to the newly created
927   /// threadprivate variable (if it is not constant) and registers destructor
928   /// for the variable (if any).
929   /// \param VD Threadprivate variable.
930   /// \param VDAddr Address of the global variable \a VD.
931   /// \param Loc Location of threadprivate declaration.
932   /// \param PerformInit true if initialization expression is not constant.
933   virtual llvm::Function *
934   emitThreadPrivateVarDefinition(const VarDecl *VD, Address VDAddr,
935                                  SourceLocation Loc, bool PerformInit,
936                                  CodeGenFunction *CGF = nullptr);
937
938   /// Creates artificial threadprivate variable with name \p Name and type \p
939   /// VarType.
940   /// \param VarType Type of the artificial threadprivate variable.
941   /// \param Name Name of the artificial threadprivate variable.
942   virtual Address getAddrOfArtificialThreadPrivate(CodeGenFunction &CGF,
943                                                    QualType VarType,
944                                                    StringRef Name);
945
946   /// \brief Emit flush of the variables specified in 'omp flush' directive.
947   /// \param Vars List of variables to flush.
948   virtual void emitFlush(CodeGenFunction &CGF, ArrayRef<const Expr *> Vars,
949                          SourceLocation Loc);
950
951   /// \brief Emit task region for the task directive. The task region is
952   /// emitted in several steps:
953   /// 1. Emit a call to kmp_task_t *__kmpc_omp_task_alloc(ident_t *, kmp_int32
954   /// gtid, kmp_int32 flags, size_t sizeof_kmp_task_t, size_t sizeof_shareds,
955   /// kmp_routine_entry_t *task_entry). Here task_entry is a pointer to the
956   /// function:
957   /// kmp_int32 .omp_task_entry.(kmp_int32 gtid, kmp_task_t *tt) {
958   ///   TaskFunction(gtid, tt->part_id, tt->shareds);
959   ///   return 0;
960   /// }
961   /// 2. Copy a list of shared variables to field shareds of the resulting
962   /// structure kmp_task_t returned by the previous call (if any).
963   /// 3. Copy a pointer to destructions function to field destructions of the
964   /// resulting structure kmp_task_t.
965   /// 4. Emit a call to kmp_int32 __kmpc_omp_task(ident_t *, kmp_int32 gtid,
966   /// kmp_task_t *new_task), where new_task is a resulting structure from
967   /// previous items.
968   /// \param D Current task directive.
969   /// \param TaskFunction An LLVM function with type void (*)(i32 /*gtid*/, i32
970   /// /*part_id*/, captured_struct */*__context*/);
971   /// \param SharedsTy A type which contains references the shared variables.
972   /// \param Shareds Context with the list of shared variables from the \p
973   /// TaskFunction.
974   /// \param IfCond Not a nullptr if 'if' clause was specified, nullptr
975   /// otherwise.
976   /// \param Data Additional data for task generation like tiednsee, final
977   /// state, list of privates etc.
978   virtual void emitTaskCall(CodeGenFunction &CGF, SourceLocation Loc,
979                             const OMPExecutableDirective &D,
980                             llvm::Value *TaskFunction, QualType SharedsTy,
981                             Address Shareds, const Expr *IfCond,
982                             const OMPTaskDataTy &Data);
983
984   /// Emit task region for the taskloop directive. The taskloop region is
985   /// emitted in several steps:
986   /// 1. Emit a call to kmp_task_t *__kmpc_omp_task_alloc(ident_t *, kmp_int32
987   /// gtid, kmp_int32 flags, size_t sizeof_kmp_task_t, size_t sizeof_shareds,
988   /// kmp_routine_entry_t *task_entry). Here task_entry is a pointer to the
989   /// function:
990   /// kmp_int32 .omp_task_entry.(kmp_int32 gtid, kmp_task_t *tt) {
991   ///   TaskFunction(gtid, tt->part_id, tt->shareds);
992   ///   return 0;
993   /// }
994   /// 2. Copy a list of shared variables to field shareds of the resulting
995   /// structure kmp_task_t returned by the previous call (if any).
996   /// 3. Copy a pointer to destructions function to field destructions of the
997   /// resulting structure kmp_task_t.
998   /// 4. Emit a call to void __kmpc_taskloop(ident_t *loc, int gtid, kmp_task_t
999   /// *task, int if_val, kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st, int
1000   /// nogroup, int sched, kmp_uint64 grainsize, void *task_dup ), where new_task
1001   /// is a resulting structure from
1002   /// previous items.
1003   /// \param D Current task directive.
1004   /// \param TaskFunction An LLVM function with type void (*)(i32 /*gtid*/, i32
1005   /// /*part_id*/, captured_struct */*__context*/);
1006   /// \param SharedsTy A type which contains references the shared variables.
1007   /// \param Shareds Context with the list of shared variables from the \p
1008   /// TaskFunction.
1009   /// \param IfCond Not a nullptr if 'if' clause was specified, nullptr
1010   /// otherwise.
1011   /// \param Data Additional data for task generation like tiednsee, final
1012   /// state, list of privates etc.
1013   virtual void emitTaskLoopCall(
1014       CodeGenFunction &CGF, SourceLocation Loc, const OMPLoopDirective &D,
1015       llvm::Value *TaskFunction, QualType SharedsTy, Address Shareds,
1016       const Expr *IfCond, const OMPTaskDataTy &Data);
1017
1018   /// \brief Emit code for the directive that does not require outlining.
1019   ///
1020   /// \param InnermostKind Kind of innermost directive (for simple directives it
1021   /// is a directive itself, for combined - its innermost directive).
1022   /// \param CodeGen Code generation sequence for the \a D directive.
1023   /// \param HasCancel true if region has inner cancel directive, false
1024   /// otherwise.
1025   virtual void emitInlinedDirective(CodeGenFunction &CGF,
1026                                     OpenMPDirectiveKind InnermostKind,
1027                                     const RegionCodeGenTy &CodeGen,
1028                                     bool HasCancel = false);
1029
1030   /// Emits reduction function.
1031   /// \param ArgsType Array type containing pointers to reduction variables.
1032   /// \param Privates List of private copies for original reduction arguments.
1033   /// \param LHSExprs List of LHS in \a ReductionOps reduction operations.
1034   /// \param RHSExprs List of RHS in \a ReductionOps reduction operations.
1035   /// \param ReductionOps List of reduction operations in form 'LHS binop RHS'
1036   /// or 'operator binop(LHS, RHS)'.
1037   llvm::Value *emitReductionFunction(CodeGenModule &CGM, llvm::Type *ArgsType,
1038                                      ArrayRef<const Expr *> Privates,
1039                                      ArrayRef<const Expr *> LHSExprs,
1040                                      ArrayRef<const Expr *> RHSExprs,
1041                                      ArrayRef<const Expr *> ReductionOps);
1042
1043   /// Emits single reduction combiner
1044   void emitSingleReductionCombiner(CodeGenFunction &CGF,
1045                                    const Expr *ReductionOp,
1046                                    const Expr *PrivateRef,
1047                                    const DeclRefExpr *LHS,
1048                                    const DeclRefExpr *RHS);
1049
1050   struct ReductionOptionsTy {
1051     bool WithNowait;
1052     bool SimpleReduction;
1053     OpenMPDirectiveKind ReductionKind;
1054   };
1055   /// \brief Emit a code for reduction clause. Next code should be emitted for
1056   /// reduction:
1057   /// \code
1058   ///
1059   /// static kmp_critical_name lock = { 0 };
1060   ///
1061   /// void reduce_func(void *lhs[<n>], void *rhs[<n>]) {
1062   ///  ...
1063   ///  *(Type<i>*)lhs[i] = RedOp<i>(*(Type<i>*)lhs[i], *(Type<i>*)rhs[i]);
1064   ///  ...
1065   /// }
1066   ///
1067   /// ...
1068   /// void *RedList[<n>] = {&<RHSExprs>[0], ..., &<RHSExprs>[<n>-1]};
1069   /// switch (__kmpc_reduce{_nowait}(<loc>, <gtid>, <n>, sizeof(RedList),
1070   /// RedList, reduce_func, &<lock>)) {
1071   /// case 1:
1072   ///  ...
1073   ///  <LHSExprs>[i] = RedOp<i>(*<LHSExprs>[i], *<RHSExprs>[i]);
1074   ///  ...
1075   /// __kmpc_end_reduce{_nowait}(<loc>, <gtid>, &<lock>);
1076   /// break;
1077   /// case 2:
1078   ///  ...
1079   ///  Atomic(<LHSExprs>[i] = RedOp<i>(*<LHSExprs>[i], *<RHSExprs>[i]));
1080   ///  ...
1081   /// break;
1082   /// default:;
1083   /// }
1084   /// \endcode
1085   ///
1086   /// \param Privates List of private copies for original reduction arguments.
1087   /// \param LHSExprs List of LHS in \a ReductionOps reduction operations.
1088   /// \param RHSExprs List of RHS in \a ReductionOps reduction operations.
1089   /// \param ReductionOps List of reduction operations in form 'LHS binop RHS'
1090   /// or 'operator binop(LHS, RHS)'.
1091   /// \param Options List of options for reduction codegen:
1092   ///     WithNowait true if parent directive has also nowait clause, false
1093   ///     otherwise.
1094   ///     SimpleReduction Emit reduction operation only. Used for omp simd
1095   ///     directive on the host.
1096   ///     ReductionKind The kind of reduction to perform.
1097   virtual void emitReduction(CodeGenFunction &CGF, SourceLocation Loc,
1098                              ArrayRef<const Expr *> Privates,
1099                              ArrayRef<const Expr *> LHSExprs,
1100                              ArrayRef<const Expr *> RHSExprs,
1101                              ArrayRef<const Expr *> ReductionOps,
1102                              ReductionOptionsTy Options);
1103
1104   /// Emit a code for initialization of task reduction clause. Next code
1105   /// should be emitted for reduction:
1106   /// \code
1107   ///
1108   /// _task_red_item_t red_data[n];
1109   /// ...
1110   /// red_data[i].shar = &origs[i];
1111   /// red_data[i].size = sizeof(origs[i]);
1112   /// red_data[i].f_init = (void*)RedInit<i>;
1113   /// red_data[i].f_fini = (void*)RedDest<i>;
1114   /// red_data[i].f_comb = (void*)RedOp<i>;
1115   /// red_data[i].flags = <Flag_i>;
1116   /// ...
1117   /// void* tg1 = __kmpc_task_reduction_init(gtid, n, red_data);
1118   /// \endcode
1119   ///
1120   /// \param LHSExprs List of LHS in \a Data.ReductionOps reduction operations.
1121   /// \param RHSExprs List of RHS in \a Data.ReductionOps reduction operations.
1122   /// \param Data Additional data for task generation like tiedness, final
1123   /// state, list of privates, reductions etc.
1124   virtual llvm::Value *emitTaskReductionInit(CodeGenFunction &CGF,
1125                                              SourceLocation Loc,
1126                                              ArrayRef<const Expr *> LHSExprs,
1127                                              ArrayRef<const Expr *> RHSExprs,
1128                                              const OMPTaskDataTy &Data);
1129
1130   /// Required to resolve existing problems in the runtime. Emits threadprivate
1131   /// variables to store the size of the VLAs/array sections for
1132   /// initializer/combiner/finalizer functions + emits threadprivate variable to
1133   /// store the pointer to the original reduction item for the custom
1134   /// initializer defined by declare reduction construct.
1135   /// \param RCG Allows to reuse an existing data for the reductions.
1136   /// \param N Reduction item for which fixups must be emitted.
1137   virtual void emitTaskReductionFixups(CodeGenFunction &CGF, SourceLocation Loc,
1138                                        ReductionCodeGen &RCG, unsigned N);
1139
1140   /// Get the address of `void *` type of the privatue copy of the reduction
1141   /// item specified by the \p SharedLVal.
1142   /// \param ReductionsPtr Pointer to the reduction data returned by the
1143   /// emitTaskReductionInit function.
1144   /// \param SharedLVal Address of the original reduction item.
1145   virtual Address getTaskReductionItem(CodeGenFunction &CGF, SourceLocation Loc,
1146                                        llvm::Value *ReductionsPtr,
1147                                        LValue SharedLVal);
1148
1149   /// \brief Emit code for 'taskwait' directive.
1150   virtual void emitTaskwaitCall(CodeGenFunction &CGF, SourceLocation Loc);
1151
1152   /// \brief Emit code for 'cancellation point' construct.
1153   /// \param CancelRegion Region kind for which the cancellation point must be
1154   /// emitted.
1155   ///
1156   virtual void emitCancellationPointCall(CodeGenFunction &CGF,
1157                                          SourceLocation Loc,
1158                                          OpenMPDirectiveKind CancelRegion);
1159
1160   /// \brief Emit code for 'cancel' construct.
1161   /// \param IfCond Condition in the associated 'if' clause, if it was
1162   /// specified, nullptr otherwise.
1163   /// \param CancelRegion Region kind for which the cancel must be emitted.
1164   ///
1165   virtual void emitCancelCall(CodeGenFunction &CGF, SourceLocation Loc,
1166                               const Expr *IfCond,
1167                               OpenMPDirectiveKind CancelRegion);
1168
1169   /// \brief Emit outilined function for 'target' directive.
1170   /// \param D Directive to emit.
1171   /// \param ParentName Name of the function that encloses the target region.
1172   /// \param OutlinedFn Outlined function value to be defined by this call.
1173   /// \param OutlinedFnID Outlined function ID value to be defined by this call.
1174   /// \param IsOffloadEntry True if the outlined function is an offload entry.
1175   /// \param CodeGen Code generation sequence for the \a D directive.
1176   /// An outlined function may not be an entry if, e.g. the if clause always
1177   /// evaluates to false.
1178   virtual void emitTargetOutlinedFunction(const OMPExecutableDirective &D,
1179                                           StringRef ParentName,
1180                                           llvm::Function *&OutlinedFn,
1181                                           llvm::Constant *&OutlinedFnID,
1182                                           bool IsOffloadEntry,
1183                                           const RegionCodeGenTy &CodeGen);
1184
1185   /// \brief Emit the target offloading code associated with \a D. The emitted
1186   /// code attempts offloading the execution to the device, an the event of
1187   /// a failure it executes the host version outlined in \a OutlinedFn.
1188   /// \param D Directive to emit.
1189   /// \param OutlinedFn Host version of the code to be offloaded.
1190   /// \param OutlinedFnID ID of host version of the code to be offloaded.
1191   /// \param IfCond Expression evaluated in if clause associated with the target
1192   /// directive, or null if no if clause is used.
1193   /// \param Device Expression evaluated in device clause associated with the
1194   /// target directive, or null if no device clause is used.
1195   /// \param CapturedVars Values captured in the current region.
1196   virtual void emitTargetCall(CodeGenFunction &CGF,
1197                               const OMPExecutableDirective &D,
1198                               llvm::Value *OutlinedFn,
1199                               llvm::Value *OutlinedFnID, const Expr *IfCond,
1200                               const Expr *Device,
1201                               ArrayRef<llvm::Value *> CapturedVars);
1202
1203   /// \brief Emit the target regions enclosed in \a GD function definition or
1204   /// the function itself in case it is a valid device function. Returns true if
1205   /// \a GD was dealt with successfully.
1206   /// \param GD Function to scan.
1207   virtual bool emitTargetFunctions(GlobalDecl GD);
1208
1209   /// \brief Emit the global variable if it is a valid device global variable.
1210   /// Returns true if \a GD was dealt with successfully.
1211   /// \param GD Variable declaration to emit.
1212   virtual bool emitTargetGlobalVariable(GlobalDecl GD);
1213
1214   /// \brief Emit the global \a GD if it is meaningful for the target. Returns
1215   /// if it was emitted successfully.
1216   /// \param GD Global to scan.
1217   virtual bool emitTargetGlobal(GlobalDecl GD);
1218
1219   /// \brief Creates the offloading descriptor in the event any target region
1220   /// was emitted in the current module and return the function that registers
1221   /// it.
1222   virtual llvm::Function *emitRegistrationFunction();
1223
1224   /// \brief Emits code for teams call of the \a OutlinedFn with
1225   /// variables captured in a record which address is stored in \a
1226   /// CapturedStruct.
1227   /// \param OutlinedFn Outlined function to be run by team masters. Type of
1228   /// this function is void(*)(kmp_int32 *, kmp_int32, struct context_vars*).
1229   /// \param CapturedVars A pointer to the record with the references to
1230   /// variables used in \a OutlinedFn function.
1231   ///
1232   virtual void emitTeamsCall(CodeGenFunction &CGF,
1233                              const OMPExecutableDirective &D,
1234                              SourceLocation Loc, llvm::Value *OutlinedFn,
1235                              ArrayRef<llvm::Value *> CapturedVars);
1236
1237   /// \brief Emits call to void __kmpc_push_num_teams(ident_t *loc, kmp_int32
1238   /// global_tid, kmp_int32 num_teams, kmp_int32 thread_limit) to generate code
1239   /// for num_teams clause.
1240   /// \param NumTeams An integer expression of teams.
1241   /// \param ThreadLimit An integer expression of threads.
1242   virtual void emitNumTeamsClause(CodeGenFunction &CGF, const Expr *NumTeams,
1243                                   const Expr *ThreadLimit, SourceLocation Loc);
1244
1245   /// Struct that keeps all the relevant information that should be kept
1246   /// throughout a 'target data' region.
1247   class TargetDataInfo {
1248     /// Set to true if device pointer information have to be obtained.
1249     bool RequiresDevicePointerInfo = false;
1250
1251   public:
1252     /// The array of base pointer passed to the runtime library.
1253     llvm::Value *BasePointersArray = nullptr;
1254     /// The array of section pointers passed to the runtime library.
1255     llvm::Value *PointersArray = nullptr;
1256     /// The array of sizes passed to the runtime library.
1257     llvm::Value *SizesArray = nullptr;
1258     /// The array of map types passed to the runtime library.
1259     llvm::Value *MapTypesArray = nullptr;
1260     /// The total number of pointers passed to the runtime library.
1261     unsigned NumberOfPtrs = 0u;
1262     /// Map between the a declaration of a capture and the corresponding base
1263     /// pointer address where the runtime returns the device pointers.
1264     llvm::DenseMap<const ValueDecl *, Address> CaptureDeviceAddrMap;
1265
1266     explicit TargetDataInfo() {}
1267     explicit TargetDataInfo(bool RequiresDevicePointerInfo)
1268         : RequiresDevicePointerInfo(RequiresDevicePointerInfo) {}
1269     /// Clear information about the data arrays.
1270     void clearArrayInfo() {
1271       BasePointersArray = nullptr;
1272       PointersArray = nullptr;
1273       SizesArray = nullptr;
1274       MapTypesArray = nullptr;
1275       NumberOfPtrs = 0u;
1276     }
1277     /// Return true if the current target data information has valid arrays.
1278     bool isValid() {
1279       return BasePointersArray && PointersArray && SizesArray &&
1280              MapTypesArray && NumberOfPtrs;
1281     }
1282     bool requiresDevicePointerInfo() { return RequiresDevicePointerInfo; }
1283   };
1284
1285   /// \brief Emit the target data mapping code associated with \a D.
1286   /// \param D Directive to emit.
1287   /// \param IfCond Expression evaluated in if clause associated with the
1288   /// target directive, or null if no device clause is used.
1289   /// \param Device Expression evaluated in device clause associated with the
1290   /// target directive, or null if no device clause is used.
1291   /// \param Info A record used to store information that needs to be preserved
1292   /// until the region is closed.
1293   virtual void emitTargetDataCalls(CodeGenFunction &CGF,
1294                                    const OMPExecutableDirective &D,
1295                                    const Expr *IfCond, const Expr *Device,
1296                                    const RegionCodeGenTy &CodeGen,
1297                                    TargetDataInfo &Info);
1298
1299   /// \brief Emit the data mapping/movement code associated with the directive
1300   /// \a D that should be of the form 'target [{enter|exit} data | update]'.
1301   /// \param D Directive to emit.
1302   /// \param IfCond Expression evaluated in if clause associated with the target
1303   /// directive, or null if no if clause is used.
1304   /// \param Device Expression evaluated in device clause associated with the
1305   /// target directive, or null if no device clause is used.
1306   virtual void emitTargetDataStandAloneCall(CodeGenFunction &CGF,
1307                                             const OMPExecutableDirective &D,
1308                                             const Expr *IfCond,
1309                                             const Expr *Device);
1310
1311   /// Marks function \a Fn with properly mangled versions of vector functions.
1312   /// \param FD Function marked as 'declare simd'.
1313   /// \param Fn LLVM function that must be marked with 'declare simd'
1314   /// attributes.
1315   virtual void emitDeclareSimdFunction(const FunctionDecl *FD,
1316                                        llvm::Function *Fn);
1317
1318   /// Emit initialization for doacross loop nesting support.
1319   /// \param D Loop-based construct used in doacross nesting construct.
1320   virtual void emitDoacrossInit(CodeGenFunction &CGF,
1321                                 const OMPLoopDirective &D);
1322
1323   /// Emit code for doacross ordered directive with 'depend' clause.
1324   /// \param C 'depend' clause with 'sink|source' dependency kind.
1325   virtual void emitDoacrossOrdered(CodeGenFunction &CGF,
1326                                    const OMPDependClause *C);
1327 };
1328
1329 } // namespace CodeGen
1330 } // namespace clang
1331
1332 #endif