Home | History | Annotate | Download | only in CodeGen
      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 "clang/AST/Type.h"
     18 #include "clang/Basic/OpenMPKinds.h"
     19 #include "clang/Basic/SourceLocation.h"
     20 #include "llvm/ADT/DenseMap.h"
     21 #include "llvm/ADT/DenseSet.h"
     22 #include "llvm/ADT/StringMap.h"
     23 #include "llvm/IR/ValueHandle.h"
     24 
     25 namespace llvm {
     26 class ArrayType;
     27 class Constant;
     28 class Function;
     29 class FunctionType;
     30 class GlobalVariable;
     31 class StructType;
     32 class Type;
     33 class Value;
     34 } // namespace llvm
     35 
     36 namespace clang {
     37 class Expr;
     38 class OMPExecutableDirective;
     39 class VarDecl;
     40 
     41 namespace CodeGen {
     42 class Address;
     43 class CodeGenFunction;
     44 class CodeGenModule;
     45 
     46 typedef llvm::function_ref<void(CodeGenFunction &)> RegionCodeGenTy;
     47 
     48 class CGOpenMPRuntime {
     49 private:
     50   enum OpenMPRTLFunction {
     51     /// \brief Call to void __kmpc_fork_call(ident_t *loc, kmp_int32 argc,
     52     /// kmpc_micro microtask, ...);
     53     OMPRTL__kmpc_fork_call,
     54     /// \brief Call to void *__kmpc_threadprivate_cached(ident_t *loc,
     55     /// kmp_int32 global_tid, void *data, size_t size, void ***cache);
     56     OMPRTL__kmpc_threadprivate_cached,
     57     /// \brief Call to void __kmpc_threadprivate_register( ident_t *,
     58     /// void *data, kmpc_ctor ctor, kmpc_cctor cctor, kmpc_dtor dtor);
     59     OMPRTL__kmpc_threadprivate_register,
     60     // Call to __kmpc_int32 kmpc_global_thread_num(ident_t *loc);
     61     OMPRTL__kmpc_global_thread_num,
     62     // Call to void __kmpc_critical(ident_t *loc, kmp_int32 global_tid,
     63     // kmp_critical_name *crit);
     64     OMPRTL__kmpc_critical,
     65     // Call to void __kmpc_critical_with_hint(ident_t *loc, kmp_int32
     66     // global_tid, kmp_critical_name *crit, uintptr_t hint);
     67     OMPRTL__kmpc_critical_with_hint,
     68     // Call to void __kmpc_end_critical(ident_t *loc, kmp_int32 global_tid,
     69     // kmp_critical_name *crit);
     70     OMPRTL__kmpc_end_critical,
     71     // Call to kmp_int32 __kmpc_cancel_barrier(ident_t *loc, kmp_int32
     72     // global_tid);
     73     OMPRTL__kmpc_cancel_barrier,
     74     // Call to void __kmpc_barrier(ident_t *loc, kmp_int32 global_tid);
     75     OMPRTL__kmpc_barrier,
     76     // Call to void __kmpc_for_static_fini(ident_t *loc, kmp_int32 global_tid);
     77     OMPRTL__kmpc_for_static_fini,
     78     // Call to void __kmpc_serialized_parallel(ident_t *loc, kmp_int32
     79     // global_tid);
     80     OMPRTL__kmpc_serialized_parallel,
     81     // Call to void __kmpc_end_serialized_parallel(ident_t *loc, kmp_int32
     82     // global_tid);
     83     OMPRTL__kmpc_end_serialized_parallel,
     84     // Call to void __kmpc_push_num_threads(ident_t *loc, kmp_int32 global_tid,
     85     // kmp_int32 num_threads);
     86     OMPRTL__kmpc_push_num_threads,
     87     // Call to void __kmpc_flush(ident_t *loc);
     88     OMPRTL__kmpc_flush,
     89     // Call to kmp_int32 __kmpc_master(ident_t *, kmp_int32 global_tid);
     90     OMPRTL__kmpc_master,
     91     // Call to void __kmpc_end_master(ident_t *, kmp_int32 global_tid);
     92     OMPRTL__kmpc_end_master,
     93     // Call to kmp_int32 __kmpc_omp_taskyield(ident_t *, kmp_int32 global_tid,
     94     // int end_part);
     95     OMPRTL__kmpc_omp_taskyield,
     96     // Call to kmp_int32 __kmpc_single(ident_t *, kmp_int32 global_tid);
     97     OMPRTL__kmpc_single,
     98     // Call to void __kmpc_end_single(ident_t *, kmp_int32 global_tid);
     99     OMPRTL__kmpc_end_single,
    100     // Call to kmp_task_t * __kmpc_omp_task_alloc(ident_t *, kmp_int32 gtid,
    101     // kmp_int32 flags, size_t sizeof_kmp_task_t, size_t sizeof_shareds,
    102     // kmp_routine_entry_t *task_entry);
    103     OMPRTL__kmpc_omp_task_alloc,
    104     // Call to kmp_int32 __kmpc_omp_task(ident_t *, kmp_int32 gtid, kmp_task_t *
    105     // new_task);
    106     OMPRTL__kmpc_omp_task,
    107     // Call to void __kmpc_copyprivate(ident_t *loc, kmp_int32 global_tid,
    108     // size_t cpy_size, void *cpy_data, void(*cpy_func)(void *, void *),
    109     // kmp_int32 didit);
    110     OMPRTL__kmpc_copyprivate,
    111     // Call to kmp_int32 __kmpc_reduce(ident_t *loc, kmp_int32 global_tid,
    112     // kmp_int32 num_vars, size_t reduce_size, void *reduce_data, void
    113     // (*reduce_func)(void *lhs_data, void *rhs_data), kmp_critical_name *lck);
    114     OMPRTL__kmpc_reduce,
    115     // Call to kmp_int32 __kmpc_reduce_nowait(ident_t *loc, kmp_int32
    116     // global_tid, kmp_int32 num_vars, size_t reduce_size, void *reduce_data,
    117     // void (*reduce_func)(void *lhs_data, void *rhs_data), kmp_critical_name
    118     // *lck);
    119     OMPRTL__kmpc_reduce_nowait,
    120     // Call to void __kmpc_end_reduce(ident_t *loc, kmp_int32 global_tid,
    121     // kmp_critical_name *lck);
    122     OMPRTL__kmpc_end_reduce,
    123     // Call to void __kmpc_end_reduce_nowait(ident_t *loc, kmp_int32 global_tid,
    124     // kmp_critical_name *lck);
    125     OMPRTL__kmpc_end_reduce_nowait,
    126     // Call to void __kmpc_omp_task_begin_if0(ident_t *, kmp_int32 gtid,
    127     // kmp_task_t * new_task);
    128     OMPRTL__kmpc_omp_task_begin_if0,
    129     // Call to void __kmpc_omp_task_complete_if0(ident_t *, kmp_int32 gtid,
    130     // kmp_task_t * new_task);
    131     OMPRTL__kmpc_omp_task_complete_if0,
    132     // Call to void __kmpc_ordered(ident_t *loc, kmp_int32 global_tid);
    133     OMPRTL__kmpc_ordered,
    134     // Call to void __kmpc_end_ordered(ident_t *loc, kmp_int32 global_tid);
    135     OMPRTL__kmpc_end_ordered,
    136     // Call to kmp_int32 __kmpc_omp_taskwait(ident_t *loc, kmp_int32
    137     // global_tid);
    138     OMPRTL__kmpc_omp_taskwait,
    139     // Call to void __kmpc_taskgroup(ident_t *loc, kmp_int32 global_tid);
    140     OMPRTL__kmpc_taskgroup,
    141     // Call to void __kmpc_end_taskgroup(ident_t *loc, kmp_int32 global_tid);
    142     OMPRTL__kmpc_end_taskgroup,
    143     // Call to void __kmpc_push_proc_bind(ident_t *loc, kmp_int32 global_tid,
    144     // int proc_bind);
    145     OMPRTL__kmpc_push_proc_bind,
    146     // Call to kmp_int32 __kmpc_omp_task_with_deps(ident_t *loc_ref, kmp_int32
    147     // gtid, kmp_task_t * new_task, kmp_int32 ndeps, kmp_depend_info_t
    148     // *dep_list, kmp_int32 ndeps_noalias, kmp_depend_info_t *noalias_dep_list);
    149     OMPRTL__kmpc_omp_task_with_deps,
    150     // Call to void __kmpc_omp_wait_deps(ident_t *loc_ref, kmp_int32
    151     // gtid, kmp_int32 ndeps, kmp_depend_info_t *dep_list, kmp_int32
    152     // ndeps_noalias, kmp_depend_info_t *noalias_dep_list);
    153     OMPRTL__kmpc_omp_wait_deps,
    154     // Call to kmp_int32 __kmpc_cancellationpoint(ident_t *loc, kmp_int32
    155     // global_tid, kmp_int32 cncl_kind);
    156     OMPRTL__kmpc_cancellationpoint,
    157     // Call to kmp_int32 __kmpc_cancel(ident_t *loc, kmp_int32 global_tid,
    158     // kmp_int32 cncl_kind);
    159     OMPRTL__kmpc_cancel,
    160 
    161     //
    162     // Offloading related calls
    163     //
    164     // Call to int32_t __tgt_target(int32_t device_id, void *host_ptr, int32_t
    165     // arg_num, void** args_base, void **args, size_t *arg_sizes, int32_t
    166     // *arg_types);
    167     OMPRTL__tgt_target,
    168   };
    169 
    170   /// \brief Values for bit flags used in the ident_t to describe the fields.
    171   /// All enumeric elements are named and described in accordance with the code
    172   /// from http://llvm.org/svn/llvm-project/openmp/trunk/runtime/src/kmp.h
    173   enum OpenMPLocationFlags {
    174     /// \brief Use trampoline for internal microtask.
    175     OMP_IDENT_IMD = 0x01,
    176     /// \brief Use c-style ident structure.
    177     OMP_IDENT_KMPC = 0x02,
    178     /// \brief Atomic reduction option for kmpc_reduce.
    179     OMP_ATOMIC_REDUCE = 0x10,
    180     /// \brief Explicit 'barrier' directive.
    181     OMP_IDENT_BARRIER_EXPL = 0x20,
    182     /// \brief Implicit barrier in code.
    183     OMP_IDENT_BARRIER_IMPL = 0x40,
    184     /// \brief Implicit barrier in 'for' directive.
    185     OMP_IDENT_BARRIER_IMPL_FOR = 0x40,
    186     /// \brief Implicit barrier in 'sections' directive.
    187     OMP_IDENT_BARRIER_IMPL_SECTIONS = 0xC0,
    188     /// \brief Implicit barrier in 'single' directive.
    189     OMP_IDENT_BARRIER_IMPL_SINGLE = 0x140
    190   };
    191   CodeGenModule &CGM;
    192   /// \brief Default const ident_t object used for initialization of all other
    193   /// ident_t objects.
    194   llvm::Constant *DefaultOpenMPPSource;
    195   /// \brief Map of flags and corresponding default locations.
    196   typedef llvm::DenseMap<unsigned, llvm::Value *> OpenMPDefaultLocMapTy;
    197   OpenMPDefaultLocMapTy OpenMPDefaultLocMap;
    198   Address getOrCreateDefaultLocation(OpenMPLocationFlags Flags);
    199 
    200 public:
    201   /// \brief Describes ident structure that describes a source location.
    202   /// All descriptions are taken from
    203   /// http://llvm.org/svn/llvm-project/openmp/trunk/runtime/src/kmp.h
    204   /// Original structure:
    205   /// typedef struct ident {
    206   ///    kmp_int32 reserved_1;   /**<  might be used in Fortran;
    207   ///                                  see above  */
    208   ///    kmp_int32 flags;        /**<  also f.flags; KMP_IDENT_xxx flags;
    209   ///                                  KMP_IDENT_KMPC identifies this union
    210   ///                                  member  */
    211   ///    kmp_int32 reserved_2;   /**<  not really used in Fortran any more;
    212   ///                                  see above */
    213   ///#if USE_ITT_BUILD
    214   ///                            /*  but currently used for storing
    215   ///                                region-specific ITT */
    216   ///                            /*  contextual information. */
    217   ///#endif /* USE_ITT_BUILD */
    218   ///    kmp_int32 reserved_3;   /**< source[4] in Fortran, do not use for
    219   ///                                 C++  */
    220   ///    char const *psource;    /**< String describing the source location.
    221   ///                            The string is composed of semi-colon separated
    222   //                             fields which describe the source file,
    223   ///                            the function and a pair of line numbers that
    224   ///                            delimit the construct.
    225   ///                             */
    226   /// } ident_t;
    227   enum IdentFieldIndex {
    228     /// \brief might be used in Fortran
    229     IdentField_Reserved_1,
    230     /// \brief OMP_IDENT_xxx flags; OMP_IDENT_KMPC identifies this union member.
    231     IdentField_Flags,
    232     /// \brief Not really used in Fortran any more
    233     IdentField_Reserved_2,
    234     /// \brief Source[4] in Fortran, do not use for C++
    235     IdentField_Reserved_3,
    236     /// \brief String describing the source location. The string is composed of
    237     /// semi-colon separated fields which describe the source file, the function
    238     /// and a pair of line numbers that delimit the construct.
    239     IdentField_PSource
    240   };
    241 private:
    242   llvm::StructType *IdentTy;
    243   /// \brief Map for SourceLocation and OpenMP runtime library debug locations.
    244   typedef llvm::DenseMap<unsigned, llvm::Value *> OpenMPDebugLocMapTy;
    245   OpenMPDebugLocMapTy OpenMPDebugLocMap;
    246   /// \brief The type for a microtask which gets passed to __kmpc_fork_call().
    247   /// Original representation is:
    248   /// typedef void (kmpc_micro)(kmp_int32 global_tid, kmp_int32 bound_tid,...);
    249   llvm::FunctionType *Kmpc_MicroTy;
    250   /// \brief Stores debug location and ThreadID for the function.
    251   struct DebugLocThreadIdTy {
    252     llvm::Value *DebugLoc;
    253     llvm::Value *ThreadID;
    254   };
    255   /// \brief Map of local debug location, ThreadId and functions.
    256   typedef llvm::DenseMap<llvm::Function *, DebugLocThreadIdTy>
    257       OpenMPLocThreadIDMapTy;
    258   OpenMPLocThreadIDMapTy OpenMPLocThreadIDMap;
    259   /// \brief Type kmp_critical_name, originally defined as typedef kmp_int32
    260   /// kmp_critical_name[8];
    261   llvm::ArrayType *KmpCriticalNameTy;
    262   /// \brief An ordered map of auto-generated variables to their unique names.
    263   /// It stores variables with the following names: 1) ".gomp_critical_user_" +
    264   /// <critical_section_name> + ".var" for "omp critical" directives; 2)
    265   /// <mangled_name_for_global_var> + ".cache." for cache for threadprivate
    266   /// variables.
    267   llvm::StringMap<llvm::AssertingVH<llvm::Constant>, llvm::BumpPtrAllocator>
    268       InternalVars;
    269   /// \brief Type typedef kmp_int32 (* kmp_routine_entry_t)(kmp_int32, void *);
    270   llvm::Type *KmpRoutineEntryPtrTy;
    271   QualType KmpRoutineEntryPtrQTy;
    272   /// \brief Type typedef struct kmp_task {
    273   ///    void *              shareds; /**< pointer to block of pointers to
    274   ///    shared vars   */
    275   ///    kmp_routine_entry_t routine; /**< pointer to routine to call for
    276   ///    executing task */
    277   ///    kmp_int32           part_id; /**< part id for the task */
    278   ///    kmp_routine_entry_t destructors; /* pointer to function to invoke
    279   ///    deconstructors of firstprivate C++ objects */
    280   /// } kmp_task_t;
    281   QualType KmpTaskTQTy;
    282   /// \brief Type typedef struct kmp_depend_info {
    283   ///    kmp_intptr_t               base_addr;
    284   ///    size_t                     len;
    285   ///    struct {
    286   ///             bool                   in:1;
    287   ///             bool                   out:1;
    288   ///    } flags;
    289   /// } kmp_depend_info_t;
    290   QualType KmpDependInfoTy;
    291 
    292 
    293   /// \brief Build type kmp_routine_entry_t (if not built yet).
    294   void emitKmpRoutineEntryT(QualType KmpInt32Ty);
    295 
    296   /// \brief Emits object of ident_t type with info for source location.
    297   /// \param Flags Flags for OpenMP location.
    298   ///
    299   llvm::Value *emitUpdateLocation(CodeGenFunction &CGF, SourceLocation Loc,
    300                                   OpenMPLocationFlags Flags = OMP_IDENT_KMPC);
    301 
    302   /// \brief Returns pointer to ident_t type.
    303   llvm::Type *getIdentTyPointerTy();
    304 
    305   /// \brief Returns pointer to kmpc_micro type.
    306   llvm::Type *getKmpc_MicroPointerTy();
    307 
    308   /// \brief Returns specified OpenMP runtime function.
    309   /// \param Function OpenMP runtime function.
    310   /// \return Specified function.
    311   llvm::Constant *createRuntimeFunction(OpenMPRTLFunction Function);
    312 
    313   /// \brief Returns __kmpc_for_static_init_* runtime function for the specified
    314   /// size \a IVSize and sign \a IVSigned.
    315   llvm::Constant *createForStaticInitFunction(unsigned IVSize, bool IVSigned);
    316 
    317   /// \brief Returns __kmpc_dispatch_init_* runtime function for the specified
    318   /// size \a IVSize and sign \a IVSigned.
    319   llvm::Constant *createDispatchInitFunction(unsigned IVSize, bool IVSigned);
    320 
    321   /// \brief Returns __kmpc_dispatch_next_* runtime function for the specified
    322   /// size \a IVSize and sign \a IVSigned.
    323   llvm::Constant *createDispatchNextFunction(unsigned IVSize, bool IVSigned);
    324 
    325   /// \brief Returns __kmpc_dispatch_fini_* runtime function for the specified
    326   /// size \a IVSize and sign \a IVSigned.
    327   llvm::Constant *createDispatchFiniFunction(unsigned IVSize, bool IVSigned);
    328 
    329   /// \brief If the specified mangled name is not in the module, create and
    330   /// return threadprivate cache object. This object is a pointer's worth of
    331   /// storage that's reserved for use by the OpenMP runtime.
    332   /// \param VD Threadprivate variable.
    333   /// \return Cache variable for the specified threadprivate.
    334   llvm::Constant *getOrCreateThreadPrivateCache(const VarDecl *VD);
    335 
    336   /// \brief Emits address of the word in a memory where current thread id is
    337   /// stored.
    338   virtual Address emitThreadIDAddress(CodeGenFunction &CGF, SourceLocation Loc);
    339 
    340   /// \brief Gets thread id value for the current thread.
    341   ///
    342   llvm::Value *getThreadID(CodeGenFunction &CGF, SourceLocation Loc);
    343 
    344   /// \brief Gets (if variable with the given name already exist) or creates
    345   /// internal global variable with the specified Name. The created variable has
    346   /// linkage CommonLinkage by default and is initialized by null value.
    347   /// \param Ty Type of the global variable. If it is exist already the type
    348   /// must be the same.
    349   /// \param Name Name of the variable.
    350   llvm::Constant *getOrCreateInternalVariable(llvm::Type *Ty,
    351                                               const llvm::Twine &Name);
    352 
    353   /// \brief Set of threadprivate variables with the generated initializer.
    354   llvm::DenseSet<const VarDecl *> ThreadPrivateWithDefinition;
    355 
    356   /// \brief Emits initialization code for the threadprivate variables.
    357   /// \param VDAddr Address of the global variable \a VD.
    358   /// \param Ctor Pointer to a global init function for \a VD.
    359   /// \param CopyCtor Pointer to a global copy function for \a VD.
    360   /// \param Dtor Pointer to a global destructor function for \a VD.
    361   /// \param Loc Location of threadprivate declaration.
    362   void emitThreadPrivateVarInit(CodeGenFunction &CGF, Address VDAddr,
    363                                 llvm::Value *Ctor, llvm::Value *CopyCtor,
    364                                 llvm::Value *Dtor, SourceLocation Loc);
    365 
    366   /// \brief Returns corresponding lock object for the specified critical region
    367   /// name. If the lock object does not exist it is created, otherwise the
    368   /// reference to the existing copy is returned.
    369   /// \param CriticalName Name of the critical region.
    370   ///
    371   llvm::Value *getCriticalRegionLock(StringRef CriticalName);
    372 
    373 public:
    374   explicit CGOpenMPRuntime(CodeGenModule &CGM);
    375   virtual ~CGOpenMPRuntime() {}
    376   virtual void clear();
    377 
    378   /// \brief Emits outlined function for the specified OpenMP parallel directive
    379   /// \a D. This outlined function has type void(*)(kmp_int32 *ThreadID,
    380   /// kmp_int32 BoundID, struct context_vars*).
    381   /// \param D OpenMP directive.
    382   /// \param ThreadIDVar Variable for thread id in the current OpenMP region.
    383   /// \param InnermostKind Kind of innermost directive (for simple directives it
    384   /// is a directive itself, for combined - its innermost directive).
    385   /// \param CodeGen Code generation sequence for the \a D directive.
    386   virtual llvm::Value *emitParallelOutlinedFunction(
    387       const OMPExecutableDirective &D, const VarDecl *ThreadIDVar,
    388       OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen);
    389 
    390   /// \brief Emits outlined function for the OpenMP task directive \a D. This
    391   /// outlined function has type void(*)(kmp_int32 ThreadID, kmp_int32
    392   /// PartID, struct context_vars*).
    393   /// \param D OpenMP directive.
    394   /// \param ThreadIDVar Variable for thread id in the current OpenMP region.
    395   /// \param InnermostKind Kind of innermost directive (for simple directives it
    396   /// is a directive itself, for combined - its innermost directive).
    397   /// \param CodeGen Code generation sequence for the \a D directive.
    398   ///
    399   virtual llvm::Value *emitTaskOutlinedFunction(
    400       const OMPExecutableDirective &D, const VarDecl *ThreadIDVar,
    401       OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen);
    402 
    403   /// \brief Cleans up references to the objects in finished function.
    404   ///
    405   void functionFinished(CodeGenFunction &CGF);
    406 
    407   /// \brief Emits code for parallel or serial call of the \a OutlinedFn with
    408   /// variables captured in a record which address is stored in \a
    409   /// CapturedStruct.
    410   /// \param OutlinedFn Outlined function to be run in parallel threads. Type of
    411   /// this function is void(*)(kmp_int32 *, kmp_int32, struct context_vars*).
    412   /// \param CapturedVars A pointer to the record with the references to
    413   /// variables used in \a OutlinedFn function.
    414   /// \param IfCond Condition in the associated 'if' clause, if it was
    415   /// specified, nullptr otherwise.
    416   ///
    417   virtual void emitParallelCall(CodeGenFunction &CGF, SourceLocation Loc,
    418                                 llvm::Value *OutlinedFn,
    419                                 ArrayRef<llvm::Value *> CapturedVars,
    420                                 const Expr *IfCond);
    421 
    422   /// \brief Emits a critical region.
    423   /// \param CriticalName Name of the critical region.
    424   /// \param CriticalOpGen Generator for the statement associated with the given
    425   /// critical region.
    426   /// \param Hint Value of the 'hint' clause (optional).
    427   virtual void emitCriticalRegion(CodeGenFunction &CGF, StringRef CriticalName,
    428                                   const RegionCodeGenTy &CriticalOpGen,
    429                                   SourceLocation Loc,
    430                                   const Expr *Hint = nullptr);
    431 
    432   /// \brief Emits a master region.
    433   /// \param MasterOpGen Generator for the statement associated with the given
    434   /// master region.
    435   virtual void emitMasterRegion(CodeGenFunction &CGF,
    436                                 const RegionCodeGenTy &MasterOpGen,
    437                                 SourceLocation Loc);
    438 
    439   /// \brief Emits code for a taskyield directive.
    440   virtual void emitTaskyieldCall(CodeGenFunction &CGF, SourceLocation Loc);
    441 
    442   /// \brief Emit a taskgroup region.
    443   /// \param TaskgroupOpGen Generator for the statement associated with the
    444   /// given taskgroup region.
    445   virtual void emitTaskgroupRegion(CodeGenFunction &CGF,
    446                                    const RegionCodeGenTy &TaskgroupOpGen,
    447                                    SourceLocation Loc);
    448 
    449   /// \brief Emits a single region.
    450   /// \param SingleOpGen Generator for the statement associated with the given
    451   /// single region.
    452   virtual void emitSingleRegion(CodeGenFunction &CGF,
    453                                 const RegionCodeGenTy &SingleOpGen,
    454                                 SourceLocation Loc,
    455                                 ArrayRef<const Expr *> CopyprivateVars,
    456                                 ArrayRef<const Expr *> DestExprs,
    457                                 ArrayRef<const Expr *> SrcExprs,
    458                                 ArrayRef<const Expr *> AssignmentOps);
    459 
    460   /// \brief Emit an ordered region.
    461   /// \param OrderedOpGen Generator for the statement associated with the given
    462   /// ordered region.
    463   virtual void emitOrderedRegion(CodeGenFunction &CGF,
    464                                  const RegionCodeGenTy &OrderedOpGen,
    465                                  SourceLocation Loc, bool IsThreads);
    466 
    467   /// \brief Emit an implicit/explicit barrier for OpenMP threads.
    468   /// \param Kind Directive for which this implicit barrier call must be
    469   /// generated. Must be OMPD_barrier for explicit barrier generation.
    470   /// \param EmitChecks true if need to emit checks for cancellation barriers.
    471   /// \param ForceSimpleCall true simple barrier call must be emitted, false if
    472   /// runtime class decides which one to emit (simple or with cancellation
    473   /// checks).
    474   ///
    475   virtual void emitBarrierCall(CodeGenFunction &CGF, SourceLocation Loc,
    476                                OpenMPDirectiveKind Kind,
    477                                bool EmitChecks = true,
    478                                bool ForceSimpleCall = false);
    479 
    480   /// \brief Check if the specified \a ScheduleKind is static non-chunked.
    481   /// This kind of worksharing directive is emitted without outer loop.
    482   /// \param ScheduleKind Schedule kind specified in the 'schedule' clause.
    483   /// \param Chunked True if chunk is specified in the clause.
    484   ///
    485   virtual bool isStaticNonchunked(OpenMPScheduleClauseKind ScheduleKind,
    486                                   bool Chunked) const;
    487 
    488   /// \brief Check if the specified \a ScheduleKind is dynamic.
    489   /// This kind of worksharing directive is emitted without outer loop.
    490   /// \param ScheduleKind Schedule Kind specified in the 'schedule' clause.
    491   ///
    492   virtual bool isDynamic(OpenMPScheduleClauseKind ScheduleKind) const;
    493 
    494   virtual void emitForDispatchInit(CodeGenFunction &CGF, SourceLocation Loc,
    495                                    OpenMPScheduleClauseKind SchedKind,
    496                                    unsigned IVSize, bool IVSigned,
    497                                    bool Ordered, llvm::Value *UB,
    498                                    llvm::Value *Chunk = nullptr);
    499 
    500   /// \brief Call the appropriate runtime routine to initialize it before start
    501   /// of loop.
    502   ///
    503   /// Depending on the loop schedule, it is nesessary to call some runtime
    504   /// routine before start of the OpenMP loop to get the loop upper / lower
    505   /// bounds \a LB and \a UB and stride \a ST.
    506   ///
    507   /// \param CGF Reference to current CodeGenFunction.
    508   /// \param Loc Clang source location.
    509   /// \param SchedKind Schedule kind, specified by the 'schedule' clause.
    510   /// \param IVSize Size of the iteration variable in bits.
    511   /// \param IVSigned Sign of the interation variable.
    512   /// \param Ordered true if loop is ordered, false otherwise.
    513   /// \param IL Address of the output variable in which the flag of the
    514   /// last iteration is returned.
    515   /// \param LB Address of the output variable in which the lower iteration
    516   /// number is returned.
    517   /// \param UB Address of the output variable in which the upper iteration
    518   /// number is returned.
    519   /// \param ST Address of the output variable in which the stride value is
    520   /// returned nesessary to generated the static_chunked scheduled loop.
    521   /// \param Chunk Value of the chunk for the static_chunked scheduled loop.
    522   /// For the default (nullptr) value, the chunk 1 will be used.
    523   ///
    524   virtual void emitForStaticInit(CodeGenFunction &CGF, SourceLocation Loc,
    525                                  OpenMPScheduleClauseKind SchedKind,
    526                                  unsigned IVSize, bool IVSigned, bool Ordered,
    527                                  Address IL, Address LB,
    528                                  Address UB, Address ST,
    529                                  llvm::Value *Chunk = nullptr);
    530 
    531   /// \brief Call the appropriate runtime routine to notify that we finished
    532   /// iteration of the ordered loop with the dynamic scheduling.
    533   ///
    534   /// \param CGF Reference to current CodeGenFunction.
    535   /// \param Loc Clang source location.
    536   /// \param IVSize Size of the iteration variable in bits.
    537   /// \param IVSigned Sign of the interation variable.
    538   ///
    539   virtual void emitForOrderedIterationEnd(CodeGenFunction &CGF,
    540                                           SourceLocation Loc, unsigned IVSize,
    541                                           bool IVSigned);
    542 
    543   /// \brief Call the appropriate runtime routine to notify that we finished
    544   /// all the work with current loop.
    545   ///
    546   /// \param CGF Reference to current CodeGenFunction.
    547   /// \param Loc Clang source location.
    548   ///
    549   virtual void emitForStaticFinish(CodeGenFunction &CGF, SourceLocation Loc);
    550 
    551   /// Call __kmpc_dispatch_next(
    552   ///          ident_t *loc, kmp_int32 tid, kmp_int32 *p_lastiter,
    553   ///          kmp_int[32|64] *p_lower, kmp_int[32|64] *p_upper,
    554   ///          kmp_int[32|64] *p_stride);
    555   /// \param IVSize Size of the iteration variable in bits.
    556   /// \param IVSigned Sign of the interation variable.
    557   /// \param IL Address of the output variable in which the flag of the
    558   /// last iteration is returned.
    559   /// \param LB Address of the output variable in which the lower iteration
    560   /// number is returned.
    561   /// \param UB Address of the output variable in which the upper iteration
    562   /// number is returned.
    563   /// \param ST Address of the output variable in which the stride value is
    564   /// returned.
    565   virtual llvm::Value *emitForNext(CodeGenFunction &CGF, SourceLocation Loc,
    566                                    unsigned IVSize, bool IVSigned,
    567                                    Address IL, Address LB,
    568                                    Address UB, Address ST);
    569 
    570   /// \brief Emits call to void __kmpc_push_num_threads(ident_t *loc, kmp_int32
    571   /// global_tid, kmp_int32 num_threads) to generate code for 'num_threads'
    572   /// clause.
    573   /// \param NumThreads An integer value of threads.
    574   virtual void emitNumThreadsClause(CodeGenFunction &CGF,
    575                                     llvm::Value *NumThreads,
    576                                     SourceLocation Loc);
    577 
    578   /// \brief Emit call to void __kmpc_push_proc_bind(ident_t *loc, kmp_int32
    579   /// global_tid, int proc_bind) to generate code for 'proc_bind' clause.
    580   virtual void emitProcBindClause(CodeGenFunction &CGF,
    581                                   OpenMPProcBindClauseKind ProcBind,
    582                                   SourceLocation Loc);
    583 
    584   /// \brief Returns address of the threadprivate variable for the current
    585   /// thread.
    586   /// \param VD Threadprivate variable.
    587   /// \param VDAddr Address of the global variable \a VD.
    588   /// \param Loc Location of the reference to threadprivate var.
    589   /// \return Address of the threadprivate variable for the current thread.
    590   virtual Address getAddrOfThreadPrivate(CodeGenFunction &CGF,
    591                                          const VarDecl *VD,
    592                                          Address VDAddr,
    593                                          SourceLocation Loc);
    594 
    595   /// \brief Emit a code for initialization of threadprivate variable. It emits
    596   /// a call to runtime library which adds initial value to the newly created
    597   /// threadprivate variable (if it is not constant) and registers destructor
    598   /// for the variable (if any).
    599   /// \param VD Threadprivate variable.
    600   /// \param VDAddr Address of the global variable \a VD.
    601   /// \param Loc Location of threadprivate declaration.
    602   /// \param PerformInit true if initialization expression is not constant.
    603   virtual llvm::Function *
    604   emitThreadPrivateVarDefinition(const VarDecl *VD, Address VDAddr,
    605                                  SourceLocation Loc, bool PerformInit,
    606                                  CodeGenFunction *CGF = nullptr);
    607 
    608   /// \brief Emit flush of the variables specified in 'omp flush' directive.
    609   /// \param Vars List of variables to flush.
    610   virtual void emitFlush(CodeGenFunction &CGF, ArrayRef<const Expr *> Vars,
    611                          SourceLocation Loc);
    612 
    613   /// \brief Emit task region for the task directive. The task region is
    614   /// emitted in several steps:
    615   /// 1. Emit a call to kmp_task_t *__kmpc_omp_task_alloc(ident_t *, kmp_int32
    616   /// gtid, kmp_int32 flags, size_t sizeof_kmp_task_t, size_t sizeof_shareds,
    617   /// kmp_routine_entry_t *task_entry). Here task_entry is a pointer to the
    618   /// function:
    619   /// kmp_int32 .omp_task_entry.(kmp_int32 gtid, kmp_task_t *tt) {
    620   ///   TaskFunction(gtid, tt->part_id, tt->shareds);
    621   ///   return 0;
    622   /// }
    623   /// 2. Copy a list of shared variables to field shareds of the resulting
    624   /// structure kmp_task_t returned by the previous call (if any).
    625   /// 3. Copy a pointer to destructions function to field destructions of the
    626   /// resulting structure kmp_task_t.
    627   /// 4. Emit a call to kmp_int32 __kmpc_omp_task(ident_t *, kmp_int32 gtid,
    628   /// kmp_task_t *new_task), where new_task is a resulting structure from
    629   /// previous items.
    630   /// \param D Current task directive.
    631   /// \param Tied true if the task is tied (the task is tied to the thread that
    632   /// can suspend its task region), false - untied (the task is not tied to any
    633   /// thread).
    634   /// \param Final Contains either constant bool value, or llvm::Value * of i1
    635   /// type for final clause. If the value is true, the task forces all of its
    636   /// child tasks to become final and included tasks.
    637   /// \param TaskFunction An LLVM function with type void (*)(i32 /*gtid*/, i32
    638   /// /*part_id*/, captured_struct */*__context*/);
    639   /// \param SharedsTy A type which contains references the shared variables.
    640   /// \param Shareds Context with the list of shared variables from the \p
    641   /// TaskFunction.
    642   /// \param IfCond Not a nullptr if 'if' clause was specified, nullptr
    643   /// otherwise.
    644   /// \param PrivateVars List of references to private variables for the task
    645   /// directive.
    646   /// \param PrivateCopies List of private copies for each private variable in
    647   /// \p PrivateVars.
    648   /// \param FirstprivateVars List of references to private variables for the
    649   /// task directive.
    650   /// \param FirstprivateCopies List of private copies for each private variable
    651   /// in \p FirstprivateVars.
    652   /// \param FirstprivateInits List of references to auto generated variables
    653   /// used for initialization of a single array element. Used if firstprivate
    654   /// variable is of array type.
    655   /// \param Dependences List of dependences for the 'task' construct, including
    656   /// original expression and dependency type.
    657   virtual void emitTaskCall(
    658       CodeGenFunction &CGF, SourceLocation Loc, const OMPExecutableDirective &D,
    659       bool Tied, llvm::PointerIntPair<llvm::Value *, 1, bool> Final,
    660       llvm::Value *TaskFunction, QualType SharedsTy, Address Shareds,
    661       const Expr *IfCond, ArrayRef<const Expr *> PrivateVars,
    662       ArrayRef<const Expr *> PrivateCopies,
    663       ArrayRef<const Expr *> FirstprivateVars,
    664       ArrayRef<const Expr *> FirstprivateCopies,
    665       ArrayRef<const Expr *> FirstprivateInits,
    666       ArrayRef<std::pair<OpenMPDependClauseKind, const Expr *>> Dependences);
    667 
    668   /// \brief Emit code for the directive that does not require outlining.
    669   ///
    670   /// \param InnermostKind Kind of innermost directive (for simple directives it
    671   /// is a directive itself, for combined - its innermost directive).
    672   /// \param CodeGen Code generation sequence for the \a D directive.
    673   /// \param HasCancel true if region has inner cancel directive, false
    674   /// otherwise.
    675   virtual void emitInlinedDirective(CodeGenFunction &CGF,
    676                                     OpenMPDirectiveKind InnermostKind,
    677                                     const RegionCodeGenTy &CodeGen,
    678                                     bool HasCancel = false);
    679   /// \brief Emit a code for reduction clause. Next code should be emitted for
    680   /// reduction:
    681   /// \code
    682   ///
    683   /// static kmp_critical_name lock = { 0 };
    684   ///
    685   /// void reduce_func(void *lhs[<n>], void *rhs[<n>]) {
    686   ///  ...
    687   ///  *(Type<i>*)lhs[i] = RedOp<i>(*(Type<i>*)lhs[i], *(Type<i>*)rhs[i]);
    688   ///  ...
    689   /// }
    690   ///
    691   /// ...
    692   /// void *RedList[<n>] = {&<RHSExprs>[0], ..., &<RHSExprs>[<n>-1]};
    693   /// switch (__kmpc_reduce{_nowait}(<loc>, <gtid>, <n>, sizeof(RedList),
    694   /// RedList, reduce_func, &<lock>)) {
    695   /// case 1:
    696   ///  ...
    697   ///  <LHSExprs>[i] = RedOp<i>(*<LHSExprs>[i], *<RHSExprs>[i]);
    698   ///  ...
    699   /// __kmpc_end_reduce{_nowait}(<loc>, <gtid>, &<lock>);
    700   /// break;
    701   /// case 2:
    702   ///  ...
    703   ///  Atomic(<LHSExprs>[i] = RedOp<i>(*<LHSExprs>[i], *<RHSExprs>[i]));
    704   ///  ...
    705   /// break;
    706   /// default:;
    707   /// }
    708   /// \endcode
    709   ///
    710   /// \param Privates List of private copies for original reduction arguments.
    711   /// \param LHSExprs List of LHS in \a ReductionOps reduction operations.
    712   /// \param RHSExprs List of RHS in \a ReductionOps reduction operations.
    713   /// \param ReductionOps List of reduction operations in form 'LHS binop RHS'
    714   /// or 'operator binop(LHS, RHS)'.
    715   /// \param WithNowait true if parent directive has also nowait clause, false
    716   /// otherwise.
    717   virtual void emitReduction(CodeGenFunction &CGF, SourceLocation Loc,
    718                              ArrayRef<const Expr *> Privates,
    719                              ArrayRef<const Expr *> LHSExprs,
    720                              ArrayRef<const Expr *> RHSExprs,
    721                              ArrayRef<const Expr *> ReductionOps,
    722                              bool WithNowait, bool SimpleReduction);
    723 
    724   /// \brief Emit code for 'taskwait' directive.
    725   virtual void emitTaskwaitCall(CodeGenFunction &CGF, SourceLocation Loc);
    726 
    727   /// \brief Emit code for 'cancellation point' construct.
    728   /// \param CancelRegion Region kind for which the cancellation point must be
    729   /// emitted.
    730   ///
    731   virtual void emitCancellationPointCall(CodeGenFunction &CGF,
    732                                          SourceLocation Loc,
    733                                          OpenMPDirectiveKind CancelRegion);
    734 
    735   /// \brief Emit code for 'cancel' construct.
    736   /// \param IfCond Condition in the associated 'if' clause, if it was
    737   /// specified, nullptr otherwise.
    738   /// \param CancelRegion Region kind for which the cancel must be emitted.
    739   ///
    740   virtual void emitCancelCall(CodeGenFunction &CGF, SourceLocation Loc,
    741                               const Expr *IfCond,
    742                               OpenMPDirectiveKind CancelRegion);
    743 
    744   /// \brief Emit outilined function for 'target' directive.
    745   /// \param D Directive to emit.
    746   /// \param CodeGen Code generation sequence for the \a D directive.
    747   virtual llvm::Value *
    748   emitTargetOutlinedFunction(const OMPExecutableDirective &D,
    749                              const RegionCodeGenTy &CodeGen);
    750 
    751   /// \brief Emit the target offloading code associated with \a D. The emitted
    752   /// code attempts offloading the execution to the device, an the event of
    753   /// a failure it executes the host version outlined in \a OutlinedFn.
    754   /// \param D Directive to emit.
    755   /// \param OutlinedFn Host version of the code to be offloaded.
    756   /// \param IfCond Expression evaluated in if clause associated with the target
    757   /// directive, or null if no if clause is used.
    758   /// \param Device Expression evaluated in device clause associated with the
    759   /// target directive, or null if no device clause is used.
    760   /// \param CapturedVars Values captured in the current region.
    761   virtual void emitTargetCall(CodeGenFunction &CGF,
    762                               const OMPExecutableDirective &D,
    763                               llvm::Value *OutlinedFn, const Expr *IfCond,
    764                               const Expr *Device,
    765                               ArrayRef<llvm::Value *> CapturedVars);
    766 };
    767 
    768 } // namespace CodeGen
    769 } // namespace clang
    770 
    771 #endif
    772