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/Basic/OpenMPKinds.h" 18 #include "clang/Basic/SourceLocation.h" 19 #include "llvm/ADT/DenseMap.h" 20 #include "llvm/ADT/DenseSet.h" 21 #include "llvm/ADT/StringMap.h" 22 #include "llvm/IR/ValueHandle.h" 23 24 namespace llvm { 25 class ArrayType; 26 class Constant; 27 class Function; 28 class FunctionType; 29 class GlobalVariable; 30 class StructType; 31 class Type; 32 class Value; 33 } // namespace llvm 34 35 namespace clang { 36 class Expr; 37 class OMPExecutableDirective; 38 class VarDecl; 39 40 namespace CodeGen { 41 42 class CodeGenFunction; 43 class CodeGenModule; 44 45 class CGOpenMPRuntime { 46 public: 47 48 private: 49 enum OpenMPRTLFunction { 50 /// \brief Call to void __kmpc_fork_call(ident_t *loc, kmp_int32 argc, 51 /// kmpc_micro microtask, ...); 52 OMPRTL__kmpc_fork_call, 53 /// \brief Call to void *__kmpc_threadprivate_cached(ident_t *loc, 54 /// kmp_int32 global_tid, void *data, size_t size, void ***cache); 55 OMPRTL__kmpc_threadprivate_cached, 56 /// \brief Call to void __kmpc_threadprivate_register( ident_t *, 57 /// void *data, kmpc_ctor ctor, kmpc_cctor cctor, kmpc_dtor dtor); 58 OMPRTL__kmpc_threadprivate_register, 59 // Call to __kmpc_int32 kmpc_global_thread_num(ident_t *loc); 60 OMPRTL__kmpc_global_thread_num, 61 // Call to void __kmpc_critical(ident_t *loc, kmp_int32 global_tid, 62 // kmp_critical_name *crit); 63 OMPRTL__kmpc_critical, 64 // Call to void __kmpc_end_critical(ident_t *loc, kmp_int32 global_tid, 65 // kmp_critical_name *crit); 66 OMPRTL__kmpc_end_critical, 67 // Call to kmp_int32 __kmpc_cancel_barrier(ident_t *loc, kmp_int32 68 // global_tid); 69 OMPRTL__kmpc_cancel_barrier, 70 // Calls for static scheduling 'omp for' loops. 71 OMPRTL__kmpc_for_static_init_4, 72 OMPRTL__kmpc_for_static_init_4u, 73 OMPRTL__kmpc_for_static_init_8, 74 OMPRTL__kmpc_for_static_init_8u, 75 OMPRTL__kmpc_for_static_fini, 76 // Call to void __kmpc_serialized_parallel(ident_t *loc, kmp_int32 77 // global_tid); 78 OMPRTL__kmpc_serialized_parallel, 79 // Call to void __kmpc_end_serialized_parallel(ident_t *loc, kmp_int32 80 // global_tid); 81 OMPRTL__kmpc_end_serialized_parallel, 82 // Call to void __kmpc_push_num_threads(ident_t *loc, kmp_int32 global_tid, 83 // kmp_int32 num_threads); 84 OMPRTL__kmpc_push_num_threads, 85 // Call to void __kmpc_flush(ident_t *loc, ...); 86 OMPRTL__kmpc_flush, 87 // Call to kmp_int32 __kmpc_master(ident_t *, kmp_int32 global_tid); 88 OMPRTL__kmpc_master, 89 // Call to void __kmpc_end_master(ident_t *, kmp_int32 global_tid); 90 OMPRTL__kmpc_end_master, 91 }; 92 93 /// \brief Values for bit flags used in the ident_t to describe the fields. 94 /// All enumeric elements are named and described in accordance with the code 95 /// from http://llvm.org/svn/llvm-project/openmp/trunk/runtime/src/kmp.h 96 enum OpenMPLocationFlags { 97 /// \brief Use trampoline for internal microtask. 98 OMP_IDENT_IMD = 0x01, 99 /// \brief Use c-style ident structure. 100 OMP_IDENT_KMPC = 0x02, 101 /// \brief Atomic reduction option for kmpc_reduce. 102 OMP_ATOMIC_REDUCE = 0x10, 103 /// \brief Explicit 'barrier' directive. 104 OMP_IDENT_BARRIER_EXPL = 0x20, 105 /// \brief Implicit barrier in code. 106 OMP_IDENT_BARRIER_IMPL = 0x40, 107 /// \brief Implicit barrier in 'for' directive. 108 OMP_IDENT_BARRIER_IMPL_FOR = 0x40, 109 /// \brief Implicit barrier in 'sections' directive. 110 OMP_IDENT_BARRIER_IMPL_SECTIONS = 0xC0, 111 /// \brief Implicit barrier in 'single' directive. 112 OMP_IDENT_BARRIER_IMPL_SINGLE = 0x140 113 }; 114 CodeGenModule &CGM; 115 /// \brief Default const ident_t object used for initialization of all other 116 /// ident_t objects. 117 llvm::Constant *DefaultOpenMPPSource; 118 /// \brief Map of flags and corresponding default locations. 119 typedef llvm::DenseMap<unsigned, llvm::Value *> OpenMPDefaultLocMapTy; 120 OpenMPDefaultLocMapTy OpenMPDefaultLocMap; 121 llvm::Value *GetOrCreateDefaultOpenMPLocation(OpenMPLocationFlags Flags); 122 /// \brief Describes ident structure that describes a source location. 123 /// All descriptions are taken from 124 /// http://llvm.org/svn/llvm-project/openmp/trunk/runtime/src/kmp.h 125 /// Original structure: 126 /// typedef struct ident { 127 /// kmp_int32 reserved_1; /**< might be used in Fortran; 128 /// see above */ 129 /// kmp_int32 flags; /**< also f.flags; KMP_IDENT_xxx flags; 130 /// KMP_IDENT_KMPC identifies this union 131 /// member */ 132 /// kmp_int32 reserved_2; /**< not really used in Fortran any more; 133 /// see above */ 134 ///#if USE_ITT_BUILD 135 /// /* but currently used for storing 136 /// region-specific ITT */ 137 /// /* contextual information. */ 138 ///#endif /* USE_ITT_BUILD */ 139 /// kmp_int32 reserved_3; /**< source[4] in Fortran, do not use for 140 /// C++ */ 141 /// char const *psource; /**< String describing the source location. 142 /// The string is composed of semi-colon separated 143 // fields which describe the source file, 144 /// the function and a pair of line numbers that 145 /// delimit the construct. 146 /// */ 147 /// } ident_t; 148 enum IdentFieldIndex { 149 /// \brief might be used in Fortran 150 IdentField_Reserved_1, 151 /// \brief OMP_IDENT_xxx flags; OMP_IDENT_KMPC identifies this union member. 152 IdentField_Flags, 153 /// \brief Not really used in Fortran any more 154 IdentField_Reserved_2, 155 /// \brief Source[4] in Fortran, do not use for C++ 156 IdentField_Reserved_3, 157 /// \brief String describing the source location. The string is composed of 158 /// semi-colon separated fields which describe the source file, the function 159 /// and a pair of line numbers that delimit the construct. 160 IdentField_PSource 161 }; 162 llvm::StructType *IdentTy; 163 /// \brief Map for SourceLocation and OpenMP runtime library debug locations. 164 typedef llvm::DenseMap<unsigned, llvm::Value *> OpenMPDebugLocMapTy; 165 OpenMPDebugLocMapTy OpenMPDebugLocMap; 166 /// \brief The type for a microtask which gets passed to __kmpc_fork_call(). 167 /// Original representation is: 168 /// typedef void (kmpc_micro)(kmp_int32 global_tid, kmp_int32 bound_tid,...); 169 llvm::FunctionType *Kmpc_MicroTy; 170 /// \brief Stores debug location and ThreadID for the function. 171 struct DebugLocThreadIdTy { 172 llvm::Value *DebugLoc; 173 llvm::Value *ThreadID; 174 }; 175 /// \brief Map of local debug location, ThreadId and functions. 176 typedef llvm::DenseMap<llvm::Function *, DebugLocThreadIdTy> 177 OpenMPLocThreadIDMapTy; 178 OpenMPLocThreadIDMapTy OpenMPLocThreadIDMap; 179 /// \brief Type kmp_critical_name, originally defined as typedef kmp_int32 180 /// kmp_critical_name[8]; 181 llvm::ArrayType *KmpCriticalNameTy; 182 /// \brief An ordered map of auto-generated variables to their unique names. 183 /// It stores variables with the following names: 1) ".gomp_critical_user_" + 184 /// <critical_section_name> + ".var" for "omp critical" directives; 2) 185 /// <mangled_name_for_global_var> + ".cache." for cache for threadprivate 186 /// variables. 187 llvm::StringMap<llvm::AssertingVH<llvm::Constant>, llvm::BumpPtrAllocator> 188 InternalVars; 189 190 /// \brief Emits object of ident_t type with info for source location. 191 /// \param Flags Flags for OpenMP location. 192 /// 193 llvm::Value * 194 EmitOpenMPUpdateLocation(CodeGenFunction &CGF, SourceLocation Loc, 195 OpenMPLocationFlags Flags = OMP_IDENT_KMPC); 196 197 /// \brief Returns pointer to ident_t type. 198 llvm::Type *getIdentTyPointerTy(); 199 200 /// \brief Returns pointer to kmpc_micro type. 201 llvm::Type *getKmpc_MicroPointerTy(); 202 203 /// \brief Returns specified OpenMP runtime function. 204 /// \param Function OpenMP runtime function. 205 /// \return Specified function. 206 llvm::Constant *CreateRuntimeFunction(OpenMPRTLFunction Function); 207 208 /// \brief If the specified mangled name is not in the module, create and 209 /// return threadprivate cache object. This object is a pointer's worth of 210 /// storage that's reserved for use by the OpenMP runtime. 211 /// \param VD Threadprivate variable. 212 /// \return Cache variable for the specified threadprivate. 213 llvm::Constant *getOrCreateThreadPrivateCache(const VarDecl *VD); 214 215 /// \brief Emits address of the word in a memory where current thread id is 216 /// stored. 217 virtual llvm::Value *EmitThreadIDAddress(CodeGenFunction &CGF, 218 SourceLocation Loc); 219 220 /// \brief Gets thread id value for the current thread. 221 /// 222 llvm::Value *GetOpenMPThreadID(CodeGenFunction &CGF, SourceLocation Loc); 223 224 /// \brief Gets (if variable with the given name already exist) or creates 225 /// internal global variable with the specified Name. The created variable has 226 /// linkage CommonLinkage by default and is initialized by null value. 227 /// \param Ty Type of the global variable. If it is exist already the type 228 /// must be the same. 229 /// \param Name Name of the variable. 230 llvm::Constant *GetOrCreateInternalVariable(llvm::Type *Ty, 231 const llvm::Twine &Name); 232 233 /// \brief Set of threadprivate variables with the generated initializer. 234 llvm::DenseSet<const VarDecl *> ThreadPrivateWithDefinition; 235 236 /// \brief Emits initialization code for the threadprivate variables. 237 /// \param VDAddr Address of the global variable \a VD. 238 /// \param Ctor Pointer to a global init function for \a VD. 239 /// \param CopyCtor Pointer to a global copy function for \a VD. 240 /// \param Dtor Pointer to a global destructor function for \a VD. 241 /// \param Loc Location of threadprivate declaration. 242 void EmitOMPThreadPrivateVarInit(CodeGenFunction &CGF, llvm::Value *VDAddr, 243 llvm::Value *Ctor, llvm::Value *CopyCtor, 244 llvm::Value *Dtor, SourceLocation Loc); 245 246 /// \brief Returns corresponding lock object for the specified critical region 247 /// name. If the lock object does not exist it is created, otherwise the 248 /// reference to the existing copy is returned. 249 /// \param CriticalName Name of the critical region. 250 /// 251 llvm::Value *GetCriticalRegionLock(StringRef CriticalName); 252 253 public: 254 explicit CGOpenMPRuntime(CodeGenModule &CGM); ~CGOpenMPRuntime()255 virtual ~CGOpenMPRuntime() {} 256 257 /// \brief Emits outlined function for the specified OpenMP directive \a D 258 /// (required for parallel and task directives). This outlined function has 259 /// type void(*)(kmp_int32 /*ThreadID*/, kmp_int32 /*BoundID*/, struct 260 /// context_vars*). 261 /// \param D OpenMP directive. 262 /// \param ThreadIDVar Variable for thread id in the current OpenMP region. 263 /// 264 virtual llvm::Value * 265 EmitOpenMPOutlinedFunction(const OMPExecutableDirective &D, 266 const VarDecl *ThreadIDVar); 267 268 /// \brief Cleans up references to the objects in finished function. 269 /// 270 void FunctionFinished(CodeGenFunction &CGF); 271 272 /// \brief Emits code for parallel call of the \a OutlinedFn with variables 273 /// captured in a record which address is stored in \a CapturedStruct. 274 /// \param OutlinedFn Outlined function to be run in parallel threads. Type of 275 /// this function is void(*)(kmp_int32, kmp_int32, struct context_vars*). 276 /// \param CapturedStruct A pointer to the record with the references to 277 /// variables used in \a OutlinedFn function. 278 /// 279 virtual void EmitOMPParallelCall(CodeGenFunction &CGF, SourceLocation Loc, 280 llvm::Value *OutlinedFn, 281 llvm::Value *CapturedStruct); 282 283 /// \brief Emits code for serial call of the \a OutlinedFn with variables 284 /// captured in a record which address is stored in \a CapturedStruct. 285 /// \param OutlinedFn Outlined function to be run in serial mode. 286 /// \param CapturedStruct A pointer to the record with the references to 287 /// variables used in \a OutlinedFn function. 288 /// 289 virtual void EmitOMPSerialCall(CodeGenFunction &CGF, SourceLocation Loc, 290 llvm::Value *OutlinedFn, 291 llvm::Value *CapturedStruct); 292 293 /// \brief Emits a critical region. 294 /// \param CriticalName Name of the critical region. 295 /// \param CriticalOpGen Generator for the statement associated with the given 296 /// critical region. 297 virtual void EmitOMPCriticalRegion(CodeGenFunction &CGF, 298 StringRef CriticalName, 299 const std::function<void()> &CriticalOpGen, 300 SourceLocation Loc); 301 302 /// \brief Emits a master region. 303 /// \param MasterOpGen Generator for the statement associated with the given 304 /// master region. 305 virtual void EmitOMPMasterRegion(CodeGenFunction &CGF, 306 const std::function<void()> &MasterOpGen, 307 SourceLocation Loc); 308 309 /// \brief Emits explicit barrier for OpenMP threads. 310 /// \param IsExplicit true, if it is explicitly specified barrier. 311 /// 312 virtual void EmitOMPBarrierCall(CodeGenFunction &CGF, SourceLocation Loc, 313 bool IsExplicit = true); 314 315 /// \brief Check if the specified \a ScheduleKind is static non-chunked. 316 /// This kind of worksharing directive is emitted without outer loop. 317 /// \param ScheduleKind Schedule kind specified in the 'schedule' clause. 318 /// \param Chunked True if chunk is specified in the clause. 319 /// 320 virtual bool isStaticNonchunked(OpenMPScheduleClauseKind ScheduleKind, 321 bool Chunked) const; 322 323 /// \brief Call the appropriate runtime routine to initialize it before start 324 /// of loop. 325 /// 326 /// Depending on the loop schedule, it is nesessary to call some runtime 327 /// routine before start of the OpenMP loop to get the loop upper / lower 328 /// bounds \a LB and \a UB and stride \a ST. 329 /// 330 /// \param CGF Reference to current CodeGenFunction. 331 /// \param Loc Clang source location. 332 /// \param SchedKind Schedule kind, specified by the 'schedule' clause. 333 /// \param IVSize Size of the iteration variable in bits. 334 /// \param IVSigned Sign of the interation variable. 335 /// \param IL Address of the output variable in which the flag of the 336 /// last iteration is returned. 337 /// \param LB Address of the output variable in which the lower iteration 338 /// number is returned. 339 /// \param UB Address of the output variable in which the upper iteration 340 /// number is returned. 341 /// \param ST Address of the output variable in which the stride value is 342 /// returned nesessary to generated the static_chunked scheduled loop. 343 /// \param Chunk Value of the chunk for the static_chunked scheduled loop. 344 /// For the default (nullptr) value, the chunk 1 will be used. 345 /// 346 virtual void EmitOMPForInit(CodeGenFunction &CGF, SourceLocation Loc, 347 OpenMPScheduleClauseKind SchedKind, 348 unsigned IVSize, bool IVSigned, llvm::Value *IL, 349 llvm::Value *LB, llvm::Value *UB, llvm::Value *ST, 350 llvm::Value *Chunk = nullptr); 351 352 /// \brief Call the appropriate runtime routine to notify that we finished 353 /// all the work with current loop. 354 /// 355 /// \param CGF Reference to current CodeGenFunction. 356 /// \param Loc Clang source location. 357 /// \param ScheduleKind Schedule kind, specified by the 'schedule' clause. 358 /// 359 virtual void EmitOMPForFinish(CodeGenFunction &CGF, SourceLocation Loc, 360 OpenMPScheduleClauseKind ScheduleKind); 361 362 /// \brief Emits call to void __kmpc_push_num_threads(ident_t *loc, kmp_int32 363 /// global_tid, kmp_int32 num_threads) to generate code for 'num_threads' 364 /// clause. 365 /// \param NumThreads An integer value of threads. 366 virtual void EmitOMPNumThreadsClause(CodeGenFunction &CGF, 367 llvm::Value *NumThreads, 368 SourceLocation Loc); 369 370 /// \brief Returns address of the threadprivate variable for the current 371 /// thread. 372 /// \param VD Threadprivate variable. 373 /// \param VDAddr Address of the global variable \a VD. 374 /// \param Loc Location of the reference to threadprivate var. 375 /// \return Address of the threadprivate variable for the current thread. 376 virtual llvm::Value *getOMPAddrOfThreadPrivate(CodeGenFunction &CGF, 377 const VarDecl *VD, 378 llvm::Value *VDAddr, 379 SourceLocation Loc); 380 381 /// \brief Emit a code for initialization of threadprivate variable. It emits 382 /// a call to runtime library which adds initial value to the newly created 383 /// threadprivate variable (if it is not constant) and registers destructor 384 /// for the variable (if any). 385 /// \param VD Threadprivate variable. 386 /// \param VDAddr Address of the global variable \a VD. 387 /// \param Loc Location of threadprivate declaration. 388 /// \param PerformInit true if initialization expression is not constant. 389 virtual llvm::Function * 390 EmitOMPThreadPrivateVarDefinition(const VarDecl *VD, llvm::Value *VDAddr, 391 SourceLocation Loc, bool PerformInit, 392 CodeGenFunction *CGF = nullptr); 393 394 /// \brief Emit flush of the variables specified in 'omp flush' directive. 395 /// \param Vars List of variables to flush. 396 virtual void EmitOMPFlush(CodeGenFunction &CGF, ArrayRef<const Expr *> Vars, 397 SourceLocation Loc); 398 }; 399 } // namespace CodeGen 400 } // namespace clang 401 402 #endif 403