clang API Documentation

CGOpenMPRuntime.h
Go to the documentation of this file.
00001 //===----- CGOpenMPRuntime.h - Interface to OpenMP Runtimes -----*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This provides a class for OpenMP runtime code generation.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #ifndef LLVM_CLANG_LIB_CODEGEN_CGOPENMPRUNTIME_H
00015 #define LLVM_CLANG_LIB_CODEGEN_CGOPENMPRUNTIME_H
00016 
00017 #include "clang/Basic/SourceLocation.h"
00018 #include "llvm/ADT/DenseMap.h"
00019 #include "llvm/ADT/DenseSet.h"
00020 #include "llvm/ADT/StringMap.h"
00021 #include "llvm/IR/ValueHandle.h"
00022 
00023 namespace llvm {
00024 class ArrayType;
00025 class Constant;
00026 class Function;
00027 class FunctionType;
00028 class GlobalVariable;
00029 class StructType;
00030 class Type;
00031 class Value;
00032 } // namespace llvm
00033 
00034 namespace clang {
00035 class VarDecl;
00036 
00037 class OMPExecutableDirective;
00038 class VarDecl;
00039 
00040 namespace CodeGen {
00041 
00042 class CodeGenFunction;
00043 class CodeGenModule;
00044 
00045 class CGOpenMPRuntime {
00046 public:
00047   /// \brief Values for bit flags used in the ident_t to describe the fields.
00048   /// All enumeric elements are named and described in accordance with the code
00049   /// from http://llvm.org/svn/llvm-project/openmp/trunk/runtime/src/kmp.h
00050   enum OpenMPLocationFlags {
00051     /// \brief Use trampoline for internal microtask.
00052     OMP_IDENT_IMD = 0x01,
00053     /// \brief Use c-style ident structure.
00054     OMP_IDENT_KMPC = 0x02,
00055     /// \brief Atomic reduction option for kmpc_reduce.
00056     OMP_ATOMIC_REDUCE = 0x10,
00057     /// \brief Explicit 'barrier' directive.
00058     OMP_IDENT_BARRIER_EXPL = 0x20,
00059     /// \brief Implicit barrier in code.
00060     OMP_IDENT_BARRIER_IMPL = 0x40,
00061     /// \brief Implicit barrier in 'for' directive.
00062     OMP_IDENT_BARRIER_IMPL_FOR = 0x40,
00063     /// \brief Implicit barrier in 'sections' directive.
00064     OMP_IDENT_BARRIER_IMPL_SECTIONS = 0xC0,
00065     /// \brief Implicit barrier in 'single' directive.
00066     OMP_IDENT_BARRIER_IMPL_SINGLE = 0x140
00067   };
00068   enum OpenMPRTLFunction {
00069     /// \brief Call to void __kmpc_fork_call(ident_t *loc, kmp_int32 argc,
00070     /// kmpc_micro microtask, ...);
00071     OMPRTL__kmpc_fork_call,
00072     /// \brief Call to void *__kmpc_threadprivate_cached(ident_t *loc,
00073     /// kmp_int32 global_tid, void *data, size_t size, void ***cache);
00074     OMPRTL__kmpc_threadprivate_cached,
00075     /// \brief Call to void __kmpc_threadprivate_register( ident_t *,
00076     /// void *data, kmpc_ctor ctor, kmpc_cctor cctor, kmpc_dtor dtor);
00077     OMPRTL__kmpc_threadprivate_register,
00078     // Call to __kmpc_int32 kmpc_global_thread_num(ident_t *loc);
00079     OMPRTL__kmpc_global_thread_num,
00080     // Call to void __kmpc_critical(ident_t *loc, kmp_int32 global_tid,
00081     // kmp_critical_name *crit);
00082     OMPRTL__kmpc_critical,
00083     // Call to void __kmpc_end_critical(ident_t *loc, kmp_int32 global_tid,
00084     // kmp_critical_name *crit);
00085     OMPRTL__kmpc_end_critical,
00086     // Call to void __kmpc_barrier(ident_t *loc, kmp_int32 global_tid);
00087     OMPRTL__kmpc_barrier,
00088     // Call to void __kmpc_serialized_parallel(ident_t *loc, kmp_int32
00089     // global_tid);
00090     OMPRTL__kmpc_serialized_parallel,
00091     // Call to void __kmpc_end_serialized_parallel(ident_t *loc, kmp_int32
00092     // global_tid);
00093     OMPRTL__kmpc_end_serialized_parallel,
00094     // Call to void __kmpc_push_num_threads(ident_t *loc, kmp_int32 global_tid,
00095     // kmp_int32 num_threads);
00096     OMPRTL__kmpc_push_num_threads
00097   };
00098 
00099 private:
00100   CodeGenModule &CGM;
00101   /// \brief Default const ident_t object used for initialization of all other
00102   /// ident_t objects.
00103   llvm::Constant *DefaultOpenMPPSource;
00104   /// \brief Map of flags and corresponding default locations.
00105   typedef llvm::DenseMap<unsigned, llvm::Value *> OpenMPDefaultLocMapTy;
00106   OpenMPDefaultLocMapTy OpenMPDefaultLocMap;
00107   llvm::Value *GetOrCreateDefaultOpenMPLocation(OpenMPLocationFlags Flags);
00108   /// \brief Describes ident structure that describes a source location.
00109   /// All descriptions are taken from
00110   /// http://llvm.org/svn/llvm-project/openmp/trunk/runtime/src/kmp.h
00111   /// Original structure:
00112   /// typedef struct ident {
00113   ///    kmp_int32 reserved_1;   /**<  might be used in Fortran;
00114   ///                                  see above  */
00115   ///    kmp_int32 flags;        /**<  also f.flags; KMP_IDENT_xxx flags;
00116   ///                                  KMP_IDENT_KMPC identifies this union
00117   ///                                  member  */
00118   ///    kmp_int32 reserved_2;   /**<  not really used in Fortran any more;
00119   ///                                  see above */
00120   ///#if USE_ITT_BUILD
00121   ///                            /*  but currently used for storing
00122   ///                                region-specific ITT */
00123   ///                            /*  contextual information. */
00124   ///#endif /* USE_ITT_BUILD */
00125   ///    kmp_int32 reserved_3;   /**< source[4] in Fortran, do not use for
00126   ///                                 C++  */
00127   ///    char const *psource;    /**< String describing the source location.
00128   ///                            The string is composed of semi-colon separated
00129   //                             fields which describe the source file,
00130   ///                            the function and a pair of line numbers that
00131   ///                            delimit the construct.
00132   ///                             */
00133   /// } ident_t;
00134   enum IdentFieldIndex {
00135     /// \brief might be used in Fortran
00136     IdentField_Reserved_1,
00137     /// \brief OMP_IDENT_xxx flags; OMP_IDENT_KMPC identifies this union member.
00138     IdentField_Flags,
00139     /// \brief Not really used in Fortran any more
00140     IdentField_Reserved_2,
00141     /// \brief Source[4] in Fortran, do not use for C++
00142     IdentField_Reserved_3,
00143     /// \brief String describing the source location. The string is composed of
00144     /// semi-colon separated fields which describe the source file, the function
00145     /// and a pair of line numbers that delimit the construct.
00146     IdentField_PSource
00147   };
00148   llvm::StructType *IdentTy;
00149   /// \brief Map for SourceLocation and OpenMP runtime library debug locations.
00150   typedef llvm::DenseMap<unsigned, llvm::Value *> OpenMPDebugLocMapTy;
00151   OpenMPDebugLocMapTy OpenMPDebugLocMap;
00152   /// \brief The type for a microtask which gets passed to __kmpc_fork_call().
00153   /// Original representation is:
00154   /// typedef void (kmpc_micro)(kmp_int32 global_tid, kmp_int32 bound_tid,...);
00155   llvm::FunctionType *Kmpc_MicroTy;
00156   /// \brief Stores debug location and ThreadID for the function.
00157   struct DebugLocThreadIdTy {
00158     llvm::Value *DebugLoc;
00159     llvm::Value *ThreadID;
00160   };
00161   /// \brief Map of local debug location, ThreadId and functions.
00162   typedef llvm::DenseMap<llvm::Function *, DebugLocThreadIdTy>
00163       OpenMPLocThreadIDMapTy;
00164   OpenMPLocThreadIDMapTy OpenMPLocThreadIDMap;
00165   /// \brief Type kmp_critical_name, originally defined as typedef kmp_int32
00166   /// kmp_critical_name[8];
00167   llvm::ArrayType *KmpCriticalNameTy;
00168   /// \brief An ordered map of auto-generated variables to their unique names.
00169   /// It stores variables with the following names: 1) ".gomp_critical_user_" +
00170   /// <critical_section_name> + ".var" for "omp critical" directives; 2)
00171   /// <mangled_name_for_global_var> + ".cache." for cache for threadprivate
00172   /// variables.
00173   llvm::StringMap<llvm::AssertingVH<llvm::Constant>, llvm::BumpPtrAllocator>
00174       InternalVars;
00175 
00176   /// \brief Emits object of ident_t type with info for source location.
00177   /// \param Flags Flags for OpenMP location.
00178   ///
00179   llvm::Value *
00180   EmitOpenMPUpdateLocation(CodeGenFunction &CGF, SourceLocation Loc,
00181                            OpenMPLocationFlags Flags = OMP_IDENT_KMPC);
00182 
00183   /// \brief Returns pointer to ident_t type.
00184   llvm::Type *getIdentTyPointerTy();
00185 
00186   /// \brief Returns pointer to kmpc_micro type.
00187   llvm::Type *getKmpc_MicroPointerTy();
00188 
00189   /// \brief Returns specified OpenMP runtime function.
00190   /// \param Function OpenMP runtime function.
00191   /// \return Specified function.
00192   llvm::Constant *CreateRuntimeFunction(OpenMPRTLFunction Function);
00193 
00194   /// \brief If the specified mangled name is not in the module, create and
00195   /// return threadprivate cache object. This object is a pointer's worth of
00196   /// storage that's reserved for use by the OpenMP runtime.
00197   /// \param VD Threadprivate variable.
00198   /// \return Cache variable for the specified threadprivate.
00199   llvm::Constant *getOrCreateThreadPrivateCache(const VarDecl *VD);
00200 
00201   /// \brief Emits address of the word in a memory where current thread id is
00202   /// stored.
00203   virtual llvm::Value *EmitThreadIDAddress(CodeGenFunction &CGF,
00204                                            SourceLocation Loc);
00205 
00206   /// \brief Gets thread id value for the current thread.
00207   ///
00208   llvm::Value *GetOpenMPThreadID(CodeGenFunction &CGF, SourceLocation Loc);
00209 
00210   /// \brief Gets (if variable with the given name already exist) or creates
00211   /// internal global variable with the specified Name. The created variable has
00212   /// linkage CommonLinkage by default and is initialized by null value.
00213   /// \param Ty Type of the global variable. If it is exist already the type
00214   /// must be the same.
00215   /// \param Name Name of the variable.
00216   llvm::Constant *GetOrCreateInternalVariable(llvm::Type *Ty,
00217                                               const llvm::Twine &Name);
00218 
00219   /// \brief Set of threadprivate variables with the generated initializer.
00220   llvm::DenseSet<const VarDecl *> ThreadPrivateWithDefinition;
00221 
00222   /// \brief Emits initialization code for the threadprivate variables.
00223   /// \param VDAddr Address of the global variable \a VD.
00224   /// \param Ctor Pointer to a global init function for \a VD.
00225   /// \param CopyCtor Pointer to a global copy function for \a VD.
00226   /// \param Dtor Pointer to a global destructor function for \a VD.
00227   /// \param Loc Location of threadprivate declaration.
00228   void EmitOMPThreadPrivateVarInit(CodeGenFunction &CGF, llvm::Value *VDAddr,
00229                                    llvm::Value *Ctor, llvm::Value *CopyCtor,
00230                                    llvm::Value *Dtor, SourceLocation Loc);
00231 
00232 public:
00233   explicit CGOpenMPRuntime(CodeGenModule &CGM);
00234   virtual ~CGOpenMPRuntime() {}
00235 
00236   /// \brief Emits outlined function for the specified OpenMP directive \a D
00237   /// (required for parallel and task directives). This outlined function has
00238   /// type void(*)(kmp_int32 /*ThreadID*/, kmp_int32 /*BoundID*/, struct
00239   /// context_vars*).
00240   /// \param D OpenMP directive.
00241   /// \param ThreadIDVar Variable for thread id in the current OpenMP region.
00242   ///
00243   virtual llvm::Value *
00244   EmitOpenMPOutlinedFunction(const OMPExecutableDirective &D,
00245                              const VarDecl *ThreadIDVar);
00246 
00247   /// \brief Cleans up references to the objects in finished function.
00248   ///
00249   void FunctionFinished(CodeGenFunction &CGF);
00250 
00251   /// \brief Emits code for parallel call of the \a OutlinedFn with variables
00252   /// captured in a record which address is stored in \a CapturedStruct.
00253   /// \param OutlinedFn Outlined function to be run in parallel threads. Type of
00254   /// this function is void(*)(kmp_int32, kmp_int32, struct context_vars*).
00255   /// \param CapturedStruct A pointer to the record with the references to
00256   /// variables used in \a OutlinedFn function.
00257   ///
00258   virtual void EmitOMPParallelCall(CodeGenFunction &CGF, SourceLocation Loc,
00259                                    llvm::Value *OutlinedFn,
00260                                    llvm::Value *CapturedStruct);
00261 
00262   /// \brief Emits code for serial call of the \a OutlinedFn with variables
00263   /// captured in a record which address is stored in \a CapturedStruct.
00264   /// \param OutlinedFn Outlined function to be run in serial mode.
00265   /// \param CapturedStruct A pointer to the record with the references to
00266   /// variables used in \a OutlinedFn function.
00267   ///
00268   virtual void EmitOMPSerialCall(CodeGenFunction &CGF, SourceLocation Loc,
00269                                  llvm::Value *OutlinedFn,
00270                                  llvm::Value *CapturedStruct);
00271 
00272   /// \brief Returns corresponding lock object for the specified critical region
00273   /// name. If the lock object does not exist it is created, otherwise the
00274   /// reference to the existing copy is returned.
00275   /// \param CriticalName Name of the critical region.
00276   ///
00277   llvm::Value *GetCriticalRegionLock(StringRef CriticalName);
00278 
00279   /// \brief Emits start of the critical region by calling void
00280   /// __kmpc_critical(ident_t *loc, kmp_int32 global_tid, kmp_critical_name
00281   /// * \a RegionLock)
00282   /// \param RegionLock The lock object for critical region.
00283   virtual void EmitOMPCriticalRegionStart(CodeGenFunction &CGF,
00284                                           llvm::Value *RegionLock,
00285                                           SourceLocation Loc);
00286 
00287   /// \brief Emits end of the critical region by calling void
00288   /// __kmpc_end_critical(ident_t *loc, kmp_int32 global_tid, kmp_critical_name
00289   /// * \a RegionLock)
00290   /// \param RegionLock The lock object for critical region.
00291   virtual void EmitOMPCriticalRegionEnd(CodeGenFunction &CGF,
00292                                         llvm::Value *RegionLock,
00293                                         SourceLocation Loc);
00294 
00295   /// \brief Emits a barrier for OpenMP threads.
00296   /// \param Flags Flags for the barrier.
00297   ///
00298   virtual void EmitOMPBarrierCall(CodeGenFunction &CGF, SourceLocation Loc,
00299                                   OpenMPLocationFlags Flags);
00300 
00301   /// \brief Emits call to void __kmpc_push_num_threads(ident_t *loc, kmp_int32
00302   /// global_tid, kmp_int32 num_threads) to generate code for 'num_threads'
00303   /// clause.
00304   /// \param NumThreads An integer value of threads.
00305   virtual void EmitOMPNumThreadsClause(CodeGenFunction &CGF,
00306                                        llvm::Value *NumThreads,
00307                                        SourceLocation Loc);
00308 
00309   /// \brief Returns address of the threadprivate variable for the current
00310   /// thread.
00311   /// \param VD Threadprivate variable.
00312   /// \param VDAddr Address of the global variable \a VD.
00313   /// \param Loc Location of the reference to threadprivate var.
00314   /// \return Address of the threadprivate variable for the current thread.
00315   virtual llvm::Value *getOMPAddrOfThreadPrivate(CodeGenFunction &CGF,
00316                                                  const VarDecl *VD,
00317                                                  llvm::Value *VDAddr,
00318                                                  SourceLocation Loc);
00319 
00320   /// \brief Emit a code for initialization of threadprivate variable. It emits
00321   /// a call to runtime library which adds initial value to the newly created
00322   /// threadprivate variable (if it is not constant) and registers destructor
00323   /// for the variable (if any).
00324   /// \param VD Threadprivate variable.
00325   /// \param VDAddr Address of the global variable \a VD.
00326   /// \param Loc Location of threadprivate declaration.
00327   /// \param PerformInit true if initialization expression is not constant.
00328   virtual llvm::Function *
00329   EmitOMPThreadPrivateVarDefinition(const VarDecl *VD, llvm::Value *VDAddr,
00330                                     SourceLocation Loc, bool PerformInit,
00331                                     CodeGenFunction *CGF = nullptr);
00332 };
00333 } // namespace CodeGen
00334 } // namespace clang
00335 
00336 #endif