1 //===----- CGOpenMPRuntime.h - Interface to OpenMP Runtimes -----*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This provides a class for OpenMP runtime code generation.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_CLANG_LIB_CODEGEN_CGOPENMPRUNTIME_H
15 #define LLVM_CLANG_LIB_CODEGEN_CGOPENMPRUNTIME_H
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"
39 class OMPExecutableDirective;
44 class CodeGenFunction;
47 typedef llvm::function_ref<void(CodeGenFunction &)> RegionCodeGenTy;
49 class CGOpenMPRuntime {
51 enum OpenMPRTLFunction {
52 /// \brief Call to void __kmpc_fork_call(ident_t *loc, kmp_int32 argc,
53 /// kmpc_micro microtask, ...);
54 OMPRTL__kmpc_fork_call,
55 /// \brief Call to void *__kmpc_threadprivate_cached(ident_t *loc,
56 /// kmp_int32 global_tid, void *data, size_t size, void ***cache);
57 OMPRTL__kmpc_threadprivate_cached,
58 /// \brief Call to void __kmpc_threadprivate_register( ident_t *,
59 /// void *data, kmpc_ctor ctor, kmpc_cctor cctor, kmpc_dtor dtor);
60 OMPRTL__kmpc_threadprivate_register,
61 // Call to __kmpc_int32 kmpc_global_thread_num(ident_t *loc);
62 OMPRTL__kmpc_global_thread_num,
63 // Call to void __kmpc_critical(ident_t *loc, kmp_int32 global_tid,
64 // kmp_critical_name *crit);
65 OMPRTL__kmpc_critical,
66 // Call to void __kmpc_critical_with_hint(ident_t *loc, kmp_int32
67 // global_tid, kmp_critical_name *crit, uintptr_t hint);
68 OMPRTL__kmpc_critical_with_hint,
69 // Call to void __kmpc_end_critical(ident_t *loc, kmp_int32 global_tid,
70 // kmp_critical_name *crit);
71 OMPRTL__kmpc_end_critical,
72 // Call to kmp_int32 __kmpc_cancel_barrier(ident_t *loc, kmp_int32
74 OMPRTL__kmpc_cancel_barrier,
75 // Call to void __kmpc_barrier(ident_t *loc, kmp_int32 global_tid);
77 // Call to void __kmpc_for_static_fini(ident_t *loc, kmp_int32 global_tid);
78 OMPRTL__kmpc_for_static_fini,
79 // Call to void __kmpc_serialized_parallel(ident_t *loc, kmp_int32
81 OMPRTL__kmpc_serialized_parallel,
82 // Call to void __kmpc_end_serialized_parallel(ident_t *loc, kmp_int32
84 OMPRTL__kmpc_end_serialized_parallel,
85 // Call to void __kmpc_push_num_threads(ident_t *loc, kmp_int32 global_tid,
86 // kmp_int32 num_threads);
87 OMPRTL__kmpc_push_num_threads,
88 // Call to void __kmpc_flush(ident_t *loc);
90 // Call to kmp_int32 __kmpc_master(ident_t *, kmp_int32 global_tid);
92 // Call to void __kmpc_end_master(ident_t *, kmp_int32 global_tid);
93 OMPRTL__kmpc_end_master,
94 // Call to kmp_int32 __kmpc_omp_taskyield(ident_t *, kmp_int32 global_tid,
96 OMPRTL__kmpc_omp_taskyield,
97 // Call to kmp_int32 __kmpc_single(ident_t *, kmp_int32 global_tid);
99 // Call to void __kmpc_end_single(ident_t *, kmp_int32 global_tid);
100 OMPRTL__kmpc_end_single,
101 // Call to kmp_task_t * __kmpc_omp_task_alloc(ident_t *, kmp_int32 gtid,
102 // kmp_int32 flags, size_t sizeof_kmp_task_t, size_t sizeof_shareds,
103 // kmp_routine_entry_t *task_entry);
104 OMPRTL__kmpc_omp_task_alloc,
105 // Call to kmp_int32 __kmpc_omp_task(ident_t *, kmp_int32 gtid, kmp_task_t *
107 OMPRTL__kmpc_omp_task,
108 // Call to void __kmpc_copyprivate(ident_t *loc, kmp_int32 global_tid,
109 // size_t cpy_size, void *cpy_data, void(*cpy_func)(void *, void *),
111 OMPRTL__kmpc_copyprivate,
112 // Call to kmp_int32 __kmpc_reduce(ident_t *loc, kmp_int32 global_tid,
113 // kmp_int32 num_vars, size_t reduce_size, void *reduce_data, void
114 // (*reduce_func)(void *lhs_data, void *rhs_data), kmp_critical_name *lck);
116 // Call to kmp_int32 __kmpc_reduce_nowait(ident_t *loc, kmp_int32
117 // global_tid, kmp_int32 num_vars, size_t reduce_size, void *reduce_data,
118 // void (*reduce_func)(void *lhs_data, void *rhs_data), kmp_critical_name
120 OMPRTL__kmpc_reduce_nowait,
121 // Call to void __kmpc_end_reduce(ident_t *loc, kmp_int32 global_tid,
122 // kmp_critical_name *lck);
123 OMPRTL__kmpc_end_reduce,
124 // Call to void __kmpc_end_reduce_nowait(ident_t *loc, kmp_int32 global_tid,
125 // kmp_critical_name *lck);
126 OMPRTL__kmpc_end_reduce_nowait,
127 // Call to void __kmpc_omp_task_begin_if0(ident_t *, kmp_int32 gtid,
128 // kmp_task_t * new_task);
129 OMPRTL__kmpc_omp_task_begin_if0,
130 // Call to void __kmpc_omp_task_complete_if0(ident_t *, kmp_int32 gtid,
131 // kmp_task_t * new_task);
132 OMPRTL__kmpc_omp_task_complete_if0,
133 // Call to void __kmpc_ordered(ident_t *loc, kmp_int32 global_tid);
134 OMPRTL__kmpc_ordered,
135 // Call to void __kmpc_end_ordered(ident_t *loc, kmp_int32 global_tid);
136 OMPRTL__kmpc_end_ordered,
137 // Call to kmp_int32 __kmpc_omp_taskwait(ident_t *loc, kmp_int32
139 OMPRTL__kmpc_omp_taskwait,
140 // Call to void __kmpc_taskgroup(ident_t *loc, kmp_int32 global_tid);
141 OMPRTL__kmpc_taskgroup,
142 // Call to void __kmpc_end_taskgroup(ident_t *loc, kmp_int32 global_tid);
143 OMPRTL__kmpc_end_taskgroup,
144 // Call to void __kmpc_push_proc_bind(ident_t *loc, kmp_int32 global_tid,
146 OMPRTL__kmpc_push_proc_bind,
147 // Call to kmp_int32 __kmpc_omp_task_with_deps(ident_t *loc_ref, kmp_int32
148 // gtid, kmp_task_t * new_task, kmp_int32 ndeps, kmp_depend_info_t
149 // *dep_list, kmp_int32 ndeps_noalias, kmp_depend_info_t *noalias_dep_list);
150 OMPRTL__kmpc_omp_task_with_deps,
151 // Call to void __kmpc_omp_wait_deps(ident_t *loc_ref, kmp_int32
152 // gtid, kmp_int32 ndeps, kmp_depend_info_t *dep_list, kmp_int32
153 // ndeps_noalias, kmp_depend_info_t *noalias_dep_list);
154 OMPRTL__kmpc_omp_wait_deps,
155 // Call to kmp_int32 __kmpc_cancellationpoint(ident_t *loc, kmp_int32
156 // global_tid, kmp_int32 cncl_kind);
157 OMPRTL__kmpc_cancellationpoint,
158 // Call to kmp_int32 __kmpc_cancel(ident_t *loc, kmp_int32 global_tid,
159 // kmp_int32 cncl_kind);
163 // Offloading related calls
165 // Call to int32_t __tgt_target(int32_t device_id, void *host_ptr, int32_t
166 // arg_num, void** args_base, void **args, size_t *arg_sizes, int32_t
169 // Call to void __tgt_register_lib(__tgt_bin_desc *desc);
170 OMPRTL__tgt_register_lib,
171 // Call to void __tgt_unregister_lib(__tgt_bin_desc *desc);
172 OMPRTL__tgt_unregister_lib,
175 /// \brief Values for bit flags used in the ident_t to describe the fields.
176 /// All enumeric elements are named and described in accordance with the code
177 /// from http://llvm.org/svn/llvm-project/openmp/trunk/runtime/src/kmp.h
178 enum OpenMPLocationFlags {
179 /// \brief Use trampoline for internal microtask.
180 OMP_IDENT_IMD = 0x01,
181 /// \brief Use c-style ident structure.
182 OMP_IDENT_KMPC = 0x02,
183 /// \brief Atomic reduction option for kmpc_reduce.
184 OMP_ATOMIC_REDUCE = 0x10,
185 /// \brief Explicit 'barrier' directive.
186 OMP_IDENT_BARRIER_EXPL = 0x20,
187 /// \brief Implicit barrier in code.
188 OMP_IDENT_BARRIER_IMPL = 0x40,
189 /// \brief Implicit barrier in 'for' directive.
190 OMP_IDENT_BARRIER_IMPL_FOR = 0x40,
191 /// \brief Implicit barrier in 'sections' directive.
192 OMP_IDENT_BARRIER_IMPL_SECTIONS = 0xC0,
193 /// \brief Implicit barrier in 'single' directive.
194 OMP_IDENT_BARRIER_IMPL_SINGLE = 0x140
197 /// \brief Default const ident_t object used for initialization of all other
199 llvm::Constant *DefaultOpenMPPSource;
200 /// \brief Map of flags and corresponding default locations.
201 typedef llvm::DenseMap<unsigned, llvm::Value *> OpenMPDefaultLocMapTy;
202 OpenMPDefaultLocMapTy OpenMPDefaultLocMap;
203 Address getOrCreateDefaultLocation(OpenMPLocationFlags Flags);
206 /// \brief Describes ident structure that describes a source location.
207 /// All descriptions are taken from
208 /// http://llvm.org/svn/llvm-project/openmp/trunk/runtime/src/kmp.h
209 /// Original structure:
210 /// typedef struct ident {
211 /// kmp_int32 reserved_1; /**< might be used in Fortran;
213 /// kmp_int32 flags; /**< also f.flags; KMP_IDENT_xxx flags;
214 /// KMP_IDENT_KMPC identifies this union
216 /// kmp_int32 reserved_2; /**< not really used in Fortran any more;
219 /// /* but currently used for storing
220 /// region-specific ITT */
221 /// /* contextual information. */
222 ///#endif /* USE_ITT_BUILD */
223 /// kmp_int32 reserved_3; /**< source[4] in Fortran, do not use for
225 /// char const *psource; /**< String describing the source location.
226 /// The string is composed of semi-colon separated
227 // fields which describe the source file,
228 /// the function and a pair of line numbers that
229 /// delimit the construct.
232 enum IdentFieldIndex {
233 /// \brief might be used in Fortran
234 IdentField_Reserved_1,
235 /// \brief OMP_IDENT_xxx flags; OMP_IDENT_KMPC identifies this union member.
237 /// \brief Not really used in Fortran any more
238 IdentField_Reserved_2,
239 /// \brief Source[4] in Fortran, do not use for C++
240 IdentField_Reserved_3,
241 /// \brief String describing the source location. The string is composed of
242 /// semi-colon separated fields which describe the source file, the function
243 /// and a pair of line numbers that delimit the construct.
247 llvm::StructType *IdentTy;
248 /// \brief Map for SourceLocation and OpenMP runtime library debug locations.
249 typedef llvm::DenseMap<unsigned, llvm::Value *> OpenMPDebugLocMapTy;
250 OpenMPDebugLocMapTy OpenMPDebugLocMap;
251 /// \brief The type for a microtask which gets passed to __kmpc_fork_call().
252 /// Original representation is:
253 /// typedef void (kmpc_micro)(kmp_int32 global_tid, kmp_int32 bound_tid,...);
254 llvm::FunctionType *Kmpc_MicroTy;
255 /// \brief Stores debug location and ThreadID for the function.
256 struct DebugLocThreadIdTy {
257 llvm::Value *DebugLoc;
258 llvm::Value *ThreadID;
260 /// \brief Map of local debug location, ThreadId and functions.
261 typedef llvm::DenseMap<llvm::Function *, DebugLocThreadIdTy>
262 OpenMPLocThreadIDMapTy;
263 OpenMPLocThreadIDMapTy OpenMPLocThreadIDMap;
264 /// \brief Type kmp_critical_name, originally defined as typedef kmp_int32
265 /// kmp_critical_name[8];
266 llvm::ArrayType *KmpCriticalNameTy;
267 /// \brief An ordered map of auto-generated variables to their unique names.
268 /// It stores variables with the following names: 1) ".gomp_critical_user_" +
269 /// <critical_section_name> + ".var" for "omp critical" directives; 2)
270 /// <mangled_name_for_global_var> + ".cache." for cache for threadprivate
272 llvm::StringMap<llvm::AssertingVH<llvm::Constant>, llvm::BumpPtrAllocator>
274 /// \brief Type typedef kmp_int32 (* kmp_routine_entry_t)(kmp_int32, void *);
275 llvm::Type *KmpRoutineEntryPtrTy;
276 QualType KmpRoutineEntryPtrQTy;
277 /// \brief Type typedef struct kmp_task {
278 /// void * shareds; /**< pointer to block of pointers to
280 /// kmp_routine_entry_t routine; /**< pointer to routine to call for
281 /// executing task */
282 /// kmp_int32 part_id; /**< part id for the task */
283 /// kmp_routine_entry_t destructors; /* pointer to function to invoke
284 /// deconstructors of firstprivate C++ objects */
286 QualType KmpTaskTQTy;
287 /// \brief Type typedef struct kmp_depend_info {
288 /// kmp_intptr_t base_addr;
294 /// } kmp_depend_info_t;
295 QualType KmpDependInfoTy;
296 /// \brief Type struct __tgt_offload_entry{
297 /// void *addr; // Pointer to the offload entry info.
298 /// // (function or global)
299 /// char *name; // Name of the function or global.
300 /// size_t size; // Size of the entry info (0 if it a function).
302 QualType TgtOffloadEntryQTy;
303 /// struct __tgt_device_image{
304 /// void *ImageStart; // Pointer to the target code start.
305 /// void *ImageEnd; // Pointer to the target code end.
306 /// // We also add the host entries to the device image, as it may be useful
307 /// // for the target runtime to have access to that information.
308 /// __tgt_offload_entry *EntriesBegin; // Begin of the table with all
310 /// __tgt_offload_entry *EntriesEnd; // End of the table with all the
311 /// // entries (non inclusive).
313 QualType TgtDeviceImageQTy;
314 /// struct __tgt_bin_desc{
315 /// int32_t NumDevices; // Number of devices supported.
316 /// __tgt_device_image *DeviceImages; // Arrays of device images
317 /// // (one per device).
318 /// __tgt_offload_entry *EntriesBegin; // Begin of the table with all the
320 /// __tgt_offload_entry *EntriesEnd; // End of the table with all the
321 /// // entries (non inclusive).
323 QualType TgtBinaryDescriptorQTy;
324 /// \brief Entity that registers the offloading constants that were emitted so
326 class OffloadEntriesInfoManagerTy {
329 /// \brief Number of entries registered so far.
330 unsigned OffloadingEntriesNum;
333 /// \brief Base class of the entries info.
334 class OffloadEntryInfo {
336 /// \brief Kind of a given entry. Currently, only target regions are
338 enum OffloadingEntryInfoKinds : unsigned {
339 // Entry is a target region.
340 OFFLOAD_ENTRY_INFO_TARGET_REGION = 0,
341 // Invalid entry info.
342 OFFLOAD_ENTRY_INFO_INVALID = ~0u
345 OffloadEntryInfo() : Order(~0u), Kind(OFFLOAD_ENTRY_INFO_INVALID) {}
346 explicit OffloadEntryInfo(OffloadingEntryInfoKinds Kind, unsigned Order)
347 : Order(Order), Kind(Kind) {}
349 bool isValid() const { return Order != ~0u; }
350 unsigned getOrder() const { return Order; }
351 OffloadingEntryInfoKinds getKind() const { return Kind; }
352 static bool classof(const OffloadEntryInfo *Info) { return true; }
355 // \brief Order this entry was emitted.
358 OffloadingEntryInfoKinds Kind;
361 /// \brief Return true if a there are no entries defined.
363 /// \brief Return number of entries defined so far.
364 unsigned size() const { return OffloadingEntriesNum; }
365 OffloadEntriesInfoManagerTy(CodeGenModule &CGM)
366 : CGM(CGM), OffloadingEntriesNum(0) {}
369 /// Target region entries related.
371 /// \brief Target region entries info.
372 class OffloadEntryInfoTargetRegion : public OffloadEntryInfo {
373 // \brief Address of the entity that has to be mapped for offloading.
374 llvm::Constant *Addr;
375 // \brief Address that can be used as the ID of the entry.
379 OffloadEntryInfoTargetRegion()
380 : OffloadEntryInfo(OFFLOAD_ENTRY_INFO_TARGET_REGION, ~0u),
381 Addr(nullptr), ID(nullptr) {}
382 explicit OffloadEntryInfoTargetRegion(unsigned Order,
383 llvm::Constant *Addr,
385 : OffloadEntryInfo(OFFLOAD_ENTRY_INFO_TARGET_REGION, Order),
386 Addr(Addr), ID(ID) {}
388 llvm::Constant *getAddress() const { return Addr; }
389 llvm::Constant *getID() const { return ID; }
390 void setAddress(llvm::Constant *V) {
391 assert(!Addr && "Address as been set before!");
394 void setID(llvm::Constant *V) {
395 assert(!ID && "ID as been set before!");
398 static bool classof(const OffloadEntryInfo *Info) {
399 return Info->getKind() == OFFLOAD_ENTRY_INFO_TARGET_REGION;
402 /// \brief Initialize target region entry.
403 void initializeTargetRegionEntryInfo(unsigned DeviceID, unsigned FileID,
404 StringRef ParentName, unsigned LineNum,
405 unsigned ColNum, unsigned Order);
406 /// \brief Register target region entry.
407 void registerTargetRegionEntryInfo(unsigned DeviceID, unsigned FileID,
408 StringRef ParentName, unsigned LineNum,
409 unsigned ColNum, llvm::Constant *Addr,
411 /// \brief Return true if a target region entry with the provided
412 /// information exists.
413 bool hasTargetRegionEntryInfo(unsigned DeviceID, unsigned FileID,
414 StringRef ParentName, unsigned LineNum,
415 unsigned ColNum) const;
416 /// brief Applies action \a Action on all registered entries.
417 typedef llvm::function_ref<void(unsigned, unsigned, StringRef, unsigned,
418 unsigned, OffloadEntryInfoTargetRegion &)>
419 OffloadTargetRegionEntryInfoActTy;
420 void actOnTargetRegionEntriesInfo(
421 const OffloadTargetRegionEntryInfoActTy &Action);
424 // Storage for target region entries kind. The storage is to be indexed by
425 // file ID, device ID, parent function name, lane number, and column number.
426 typedef llvm::DenseMap<unsigned, OffloadEntryInfoTargetRegion>
427 OffloadEntriesTargetRegionPerColumn;
428 typedef llvm::DenseMap<unsigned, OffloadEntriesTargetRegionPerColumn>
429 OffloadEntriesTargetRegionPerLine;
430 typedef llvm::StringMap<OffloadEntriesTargetRegionPerLine>
431 OffloadEntriesTargetRegionPerParentName;
432 typedef llvm::DenseMap<unsigned, OffloadEntriesTargetRegionPerParentName>
433 OffloadEntriesTargetRegionPerFile;
434 typedef llvm::DenseMap<unsigned, OffloadEntriesTargetRegionPerFile>
435 OffloadEntriesTargetRegionPerDevice;
436 typedef OffloadEntriesTargetRegionPerDevice OffloadEntriesTargetRegionTy;
437 OffloadEntriesTargetRegionTy OffloadEntriesTargetRegion;
439 OffloadEntriesInfoManagerTy OffloadEntriesInfoManager;
441 /// \brief Creates and registers offloading binary descriptor for the current
442 /// compilation unit. The function that does the registration is returned.
443 llvm::Function *createOffloadingBinaryDescriptorRegistration();
445 /// \brief Creates offloading entry for the provided address \a Addr,
446 /// name \a Name and size \a Size.
447 void createOffloadEntry(llvm::Constant *Addr, StringRef Name, uint64_t Size);
449 /// \brief Creates all the offload entries in the current compilation unit
450 /// along with the associated metadata.
451 void createOffloadEntriesAndInfoMetadata();
453 /// \brief Loads all the offload entries information from the host IR
455 void loadOffloadInfoMetadata();
457 /// \brief Returns __tgt_offload_entry type.
458 QualType getTgtOffloadEntryQTy();
460 /// \brief Returns __tgt_device_image type.
461 QualType getTgtDeviceImageQTy();
463 /// \brief Returns __tgt_bin_desc type.
464 QualType getTgtBinaryDescriptorQTy();
466 /// \brief Start scanning from statement \a S and and emit all target regions
467 /// found along the way.
468 /// \param S Starting statement.
469 /// \param ParentName Name of the function declaration that is being scanned.
470 void scanForTargetRegionsFunctions(const Stmt *S, StringRef ParentName);
472 /// \brief Build type kmp_routine_entry_t (if not built yet).
473 void emitKmpRoutineEntryT(QualType KmpInt32Ty);
475 /// \brief Emits object of ident_t type with info for source location.
476 /// \param Flags Flags for OpenMP location.
478 llvm::Value *emitUpdateLocation(CodeGenFunction &CGF, SourceLocation Loc,
479 OpenMPLocationFlags Flags = OMP_IDENT_KMPC);
481 /// \brief Returns pointer to ident_t type.
482 llvm::Type *getIdentTyPointerTy();
484 /// \brief Returns pointer to kmpc_micro type.
485 llvm::Type *getKmpc_MicroPointerTy();
487 /// \brief Returns specified OpenMP runtime function.
488 /// \param Function OpenMP runtime function.
489 /// \return Specified function.
490 llvm::Constant *createRuntimeFunction(OpenMPRTLFunction Function);
492 /// \brief Returns __kmpc_for_static_init_* runtime function for the specified
493 /// size \a IVSize and sign \a IVSigned.
494 llvm::Constant *createForStaticInitFunction(unsigned IVSize, bool IVSigned);
496 /// \brief Returns __kmpc_dispatch_init_* runtime function for the specified
497 /// size \a IVSize and sign \a IVSigned.
498 llvm::Constant *createDispatchInitFunction(unsigned IVSize, bool IVSigned);
500 /// \brief Returns __kmpc_dispatch_next_* runtime function for the specified
501 /// size \a IVSize and sign \a IVSigned.
502 llvm::Constant *createDispatchNextFunction(unsigned IVSize, bool IVSigned);
504 /// \brief Returns __kmpc_dispatch_fini_* runtime function for the specified
505 /// size \a IVSize and sign \a IVSigned.
506 llvm::Constant *createDispatchFiniFunction(unsigned IVSize, bool IVSigned);
508 /// \brief If the specified mangled name is not in the module, create and
509 /// return threadprivate cache object. This object is a pointer's worth of
510 /// storage that's reserved for use by the OpenMP runtime.
511 /// \param VD Threadprivate variable.
512 /// \return Cache variable for the specified threadprivate.
513 llvm::Constant *getOrCreateThreadPrivateCache(const VarDecl *VD);
515 /// \brief Emits address of the word in a memory where current thread id is
517 virtual Address emitThreadIDAddress(CodeGenFunction &CGF, SourceLocation Loc);
519 /// \brief Gets thread id value for the current thread.
521 llvm::Value *getThreadID(CodeGenFunction &CGF, SourceLocation Loc);
523 /// \brief Gets (if variable with the given name already exist) or creates
524 /// internal global variable with the specified Name. The created variable has
525 /// linkage CommonLinkage by default and is initialized by null value.
526 /// \param Ty Type of the global variable. If it is exist already the type
527 /// must be the same.
528 /// \param Name Name of the variable.
529 llvm::Constant *getOrCreateInternalVariable(llvm::Type *Ty,
530 const llvm::Twine &Name);
532 /// \brief Set of threadprivate variables with the generated initializer.
533 llvm::DenseSet<const VarDecl *> ThreadPrivateWithDefinition;
535 /// \brief Emits initialization code for the threadprivate variables.
536 /// \param VDAddr Address of the global variable \a VD.
537 /// \param Ctor Pointer to a global init function for \a VD.
538 /// \param CopyCtor Pointer to a global copy function for \a VD.
539 /// \param Dtor Pointer to a global destructor function for \a VD.
540 /// \param Loc Location of threadprivate declaration.
541 void emitThreadPrivateVarInit(CodeGenFunction &CGF, Address VDAddr,
542 llvm::Value *Ctor, llvm::Value *CopyCtor,
543 llvm::Value *Dtor, SourceLocation Loc);
545 /// \brief Returns corresponding lock object for the specified critical region
546 /// name. If the lock object does not exist it is created, otherwise the
547 /// reference to the existing copy is returned.
548 /// \param CriticalName Name of the critical region.
550 llvm::Value *getCriticalRegionLock(StringRef CriticalName);
553 explicit CGOpenMPRuntime(CodeGenModule &CGM);
554 virtual ~CGOpenMPRuntime() {}
555 virtual void clear();
557 /// \brief Emits outlined function for the specified OpenMP parallel directive
558 /// \a D. This outlined function has type void(*)(kmp_int32 *ThreadID,
559 /// kmp_int32 BoundID, struct context_vars*).
560 /// \param D OpenMP directive.
561 /// \param ThreadIDVar Variable for thread id in the current OpenMP region.
562 /// \param InnermostKind Kind of innermost directive (for simple directives it
563 /// is a directive itself, for combined - its innermost directive).
564 /// \param CodeGen Code generation sequence for the \a D directive.
565 virtual llvm::Value *emitParallelOutlinedFunction(
566 const OMPExecutableDirective &D, const VarDecl *ThreadIDVar,
567 OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen);
569 /// \brief Emits outlined function for the OpenMP task directive \a D. This
570 /// outlined function has type void(*)(kmp_int32 ThreadID, kmp_int32
571 /// PartID, struct context_vars*).
572 /// \param D OpenMP directive.
573 /// \param ThreadIDVar Variable for thread id in the current OpenMP region.
574 /// \param InnermostKind Kind of innermost directive (for simple directives it
575 /// is a directive itself, for combined - its innermost directive).
576 /// \param CodeGen Code generation sequence for the \a D directive.
578 virtual llvm::Value *emitTaskOutlinedFunction(
579 const OMPExecutableDirective &D, const VarDecl *ThreadIDVar,
580 OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen);
582 /// \brief Cleans up references to the objects in finished function.
584 void functionFinished(CodeGenFunction &CGF);
586 /// \brief Emits code for parallel or serial call of the \a OutlinedFn with
587 /// variables captured in a record which address is stored in \a
589 /// \param OutlinedFn Outlined function to be run in parallel threads. Type of
590 /// this function is void(*)(kmp_int32 *, kmp_int32, struct context_vars*).
591 /// \param CapturedVars A pointer to the record with the references to
592 /// variables used in \a OutlinedFn function.
593 /// \param IfCond Condition in the associated 'if' clause, if it was
594 /// specified, nullptr otherwise.
596 virtual void emitParallelCall(CodeGenFunction &CGF, SourceLocation Loc,
597 llvm::Value *OutlinedFn,
598 ArrayRef<llvm::Value *> CapturedVars,
601 /// \brief Emits a critical region.
602 /// \param CriticalName Name of the critical region.
603 /// \param CriticalOpGen Generator for the statement associated with the given
605 /// \param Hint Value of the 'hint' clause (optional).
606 virtual void emitCriticalRegion(CodeGenFunction &CGF, StringRef CriticalName,
607 const RegionCodeGenTy &CriticalOpGen,
609 const Expr *Hint = nullptr);
611 /// \brief Emits a master region.
612 /// \param MasterOpGen Generator for the statement associated with the given
614 virtual void emitMasterRegion(CodeGenFunction &CGF,
615 const RegionCodeGenTy &MasterOpGen,
618 /// \brief Emits code for a taskyield directive.
619 virtual void emitTaskyieldCall(CodeGenFunction &CGF, SourceLocation Loc);
621 /// \brief Emit a taskgroup region.
622 /// \param TaskgroupOpGen Generator for the statement associated with the
623 /// given taskgroup region.
624 virtual void emitTaskgroupRegion(CodeGenFunction &CGF,
625 const RegionCodeGenTy &TaskgroupOpGen,
628 /// \brief Emits a single region.
629 /// \param SingleOpGen Generator for the statement associated with the given
631 virtual void emitSingleRegion(CodeGenFunction &CGF,
632 const RegionCodeGenTy &SingleOpGen,
634 ArrayRef<const Expr *> CopyprivateVars,
635 ArrayRef<const Expr *> DestExprs,
636 ArrayRef<const Expr *> SrcExprs,
637 ArrayRef<const Expr *> AssignmentOps);
639 /// \brief Emit an ordered region.
640 /// \param OrderedOpGen Generator for the statement associated with the given
642 virtual void emitOrderedRegion(CodeGenFunction &CGF,
643 const RegionCodeGenTy &OrderedOpGen,
644 SourceLocation Loc, bool IsThreads);
646 /// \brief Emit an implicit/explicit barrier for OpenMP threads.
647 /// \param Kind Directive for which this implicit barrier call must be
648 /// generated. Must be OMPD_barrier for explicit barrier generation.
649 /// \param EmitChecks true if need to emit checks for cancellation barriers.
650 /// \param ForceSimpleCall true simple barrier call must be emitted, false if
651 /// runtime class decides which one to emit (simple or with cancellation
654 virtual void emitBarrierCall(CodeGenFunction &CGF, SourceLocation Loc,
655 OpenMPDirectiveKind Kind,
656 bool EmitChecks = true,
657 bool ForceSimpleCall = false);
659 /// \brief Check if the specified \a ScheduleKind is static non-chunked.
660 /// This kind of worksharing directive is emitted without outer loop.
661 /// \param ScheduleKind Schedule kind specified in the 'schedule' clause.
662 /// \param Chunked True if chunk is specified in the clause.
664 virtual bool isStaticNonchunked(OpenMPScheduleClauseKind ScheduleKind,
667 /// \brief Check if the specified \a ScheduleKind is dynamic.
668 /// This kind of worksharing directive is emitted without outer loop.
669 /// \param ScheduleKind Schedule Kind specified in the 'schedule' clause.
671 virtual bool isDynamic(OpenMPScheduleClauseKind ScheduleKind) const;
673 virtual void emitForDispatchInit(CodeGenFunction &CGF, SourceLocation Loc,
674 OpenMPScheduleClauseKind SchedKind,
675 unsigned IVSize, bool IVSigned,
676 bool Ordered, llvm::Value *UB,
677 llvm::Value *Chunk = nullptr);
679 /// \brief Call the appropriate runtime routine to initialize it before start
682 /// Depending on the loop schedule, it is nesessary to call some runtime
683 /// routine before start of the OpenMP loop to get the loop upper / lower
684 /// bounds \a LB and \a UB and stride \a ST.
686 /// \param CGF Reference to current CodeGenFunction.
687 /// \param Loc Clang source location.
688 /// \param SchedKind Schedule kind, specified by the 'schedule' clause.
689 /// \param IVSize Size of the iteration variable in bits.
690 /// \param IVSigned Sign of the interation variable.
691 /// \param Ordered true if loop is ordered, false otherwise.
692 /// \param IL Address of the output variable in which the flag of the
693 /// last iteration is returned.
694 /// \param LB Address of the output variable in which the lower iteration
695 /// number is returned.
696 /// \param UB Address of the output variable in which the upper iteration
697 /// number is returned.
698 /// \param ST Address of the output variable in which the stride value is
699 /// returned nesessary to generated the static_chunked scheduled loop.
700 /// \param Chunk Value of the chunk for the static_chunked scheduled loop.
701 /// For the default (nullptr) value, the chunk 1 will be used.
703 virtual void emitForStaticInit(CodeGenFunction &CGF, SourceLocation Loc,
704 OpenMPScheduleClauseKind SchedKind,
705 unsigned IVSize, bool IVSigned, bool Ordered,
706 Address IL, Address LB,
707 Address UB, Address ST,
708 llvm::Value *Chunk = nullptr);
710 /// \brief Call the appropriate runtime routine to notify that we finished
711 /// iteration of the ordered loop with the dynamic scheduling.
713 /// \param CGF Reference to current CodeGenFunction.
714 /// \param Loc Clang source location.
715 /// \param IVSize Size of the iteration variable in bits.
716 /// \param IVSigned Sign of the interation variable.
718 virtual void emitForOrderedIterationEnd(CodeGenFunction &CGF,
719 SourceLocation Loc, unsigned IVSize,
722 /// \brief Call the appropriate runtime routine to notify that we finished
723 /// all the work with current loop.
725 /// \param CGF Reference to current CodeGenFunction.
726 /// \param Loc Clang source location.
728 virtual void emitForStaticFinish(CodeGenFunction &CGF, SourceLocation Loc);
730 /// Call __kmpc_dispatch_next(
731 /// ident_t *loc, kmp_int32 tid, kmp_int32 *p_lastiter,
732 /// kmp_int[32|64] *p_lower, kmp_int[32|64] *p_upper,
733 /// kmp_int[32|64] *p_stride);
734 /// \param IVSize Size of the iteration variable in bits.
735 /// \param IVSigned Sign of the interation variable.
736 /// \param IL Address of the output variable in which the flag of the
737 /// last iteration is returned.
738 /// \param LB Address of the output variable in which the lower iteration
739 /// number is returned.
740 /// \param UB Address of the output variable in which the upper iteration
741 /// number is returned.
742 /// \param ST Address of the output variable in which the stride value is
744 virtual llvm::Value *emitForNext(CodeGenFunction &CGF, SourceLocation Loc,
745 unsigned IVSize, bool IVSigned,
746 Address IL, Address LB,
747 Address UB, Address ST);
749 /// \brief Emits call to void __kmpc_push_num_threads(ident_t *loc, kmp_int32
750 /// global_tid, kmp_int32 num_threads) to generate code for 'num_threads'
752 /// \param NumThreads An integer value of threads.
753 virtual void emitNumThreadsClause(CodeGenFunction &CGF,
754 llvm::Value *NumThreads,
757 /// \brief Emit call to void __kmpc_push_proc_bind(ident_t *loc, kmp_int32
758 /// global_tid, int proc_bind) to generate code for 'proc_bind' clause.
759 virtual void emitProcBindClause(CodeGenFunction &CGF,
760 OpenMPProcBindClauseKind ProcBind,
763 /// \brief Returns address of the threadprivate variable for the current
765 /// \param VD Threadprivate variable.
766 /// \param VDAddr Address of the global variable \a VD.
767 /// \param Loc Location of the reference to threadprivate var.
768 /// \return Address of the threadprivate variable for the current thread.
769 virtual Address getAddrOfThreadPrivate(CodeGenFunction &CGF,
774 /// \brief Emit a code for initialization of threadprivate variable. It emits
775 /// a call to runtime library which adds initial value to the newly created
776 /// threadprivate variable (if it is not constant) and registers destructor
777 /// for the variable (if any).
778 /// \param VD Threadprivate variable.
779 /// \param VDAddr Address of the global variable \a VD.
780 /// \param Loc Location of threadprivate declaration.
781 /// \param PerformInit true if initialization expression is not constant.
782 virtual llvm::Function *
783 emitThreadPrivateVarDefinition(const VarDecl *VD, Address VDAddr,
784 SourceLocation Loc, bool PerformInit,
785 CodeGenFunction *CGF = nullptr);
787 /// \brief Emit flush of the variables specified in 'omp flush' directive.
788 /// \param Vars List of variables to flush.
789 virtual void emitFlush(CodeGenFunction &CGF, ArrayRef<const Expr *> Vars,
792 /// \brief Emit task region for the task directive. The task region is
793 /// emitted in several steps:
794 /// 1. Emit a call to kmp_task_t *__kmpc_omp_task_alloc(ident_t *, kmp_int32
795 /// gtid, kmp_int32 flags, size_t sizeof_kmp_task_t, size_t sizeof_shareds,
796 /// kmp_routine_entry_t *task_entry). Here task_entry is a pointer to the
798 /// kmp_int32 .omp_task_entry.(kmp_int32 gtid, kmp_task_t *tt) {
799 /// TaskFunction(gtid, tt->part_id, tt->shareds);
802 /// 2. Copy a list of shared variables to field shareds of the resulting
803 /// structure kmp_task_t returned by the previous call (if any).
804 /// 3. Copy a pointer to destructions function to field destructions of the
805 /// resulting structure kmp_task_t.
806 /// 4. Emit a call to kmp_int32 __kmpc_omp_task(ident_t *, kmp_int32 gtid,
807 /// kmp_task_t *new_task), where new_task is a resulting structure from
809 /// \param D Current task directive.
810 /// \param Tied true if the task is tied (the task is tied to the thread that
811 /// can suspend its task region), false - untied (the task is not tied to any
813 /// \param Final Contains either constant bool value, or llvm::Value * of i1
814 /// type for final clause. If the value is true, the task forces all of its
815 /// child tasks to become final and included tasks.
816 /// \param TaskFunction An LLVM function with type void (*)(i32 /*gtid*/, i32
817 /// /*part_id*/, captured_struct */*__context*/);
818 /// \param SharedsTy A type which contains references the shared variables.
819 /// \param Shareds Context with the list of shared variables from the \p
821 /// \param IfCond Not a nullptr if 'if' clause was specified, nullptr
823 /// \param PrivateVars List of references to private variables for the task
825 /// \param PrivateCopies List of private copies for each private variable in
827 /// \param FirstprivateVars List of references to private variables for the
829 /// \param FirstprivateCopies List of private copies for each private variable
830 /// in \p FirstprivateVars.
831 /// \param FirstprivateInits List of references to auto generated variables
832 /// used for initialization of a single array element. Used if firstprivate
833 /// variable is of array type.
834 /// \param Dependences List of dependences for the 'task' construct, including
835 /// original expression and dependency type.
836 virtual void emitTaskCall(
837 CodeGenFunction &CGF, SourceLocation Loc, const OMPExecutableDirective &D,
838 bool Tied, llvm::PointerIntPair<llvm::Value *, 1, bool> Final,
839 llvm::Value *TaskFunction, QualType SharedsTy, Address Shareds,
840 const Expr *IfCond, ArrayRef<const Expr *> PrivateVars,
841 ArrayRef<const Expr *> PrivateCopies,
842 ArrayRef<const Expr *> FirstprivateVars,
843 ArrayRef<const Expr *> FirstprivateCopies,
844 ArrayRef<const Expr *> FirstprivateInits,
845 ArrayRef<std::pair<OpenMPDependClauseKind, const Expr *>> Dependences);
847 /// \brief Emit code for the directive that does not require outlining.
849 /// \param InnermostKind Kind of innermost directive (for simple directives it
850 /// is a directive itself, for combined - its innermost directive).
851 /// \param CodeGen Code generation sequence for the \a D directive.
852 /// \param HasCancel true if region has inner cancel directive, false
854 virtual void emitInlinedDirective(CodeGenFunction &CGF,
855 OpenMPDirectiveKind InnermostKind,
856 const RegionCodeGenTy &CodeGen,
857 bool HasCancel = false);
858 /// \brief Emit a code for reduction clause. Next code should be emitted for
862 /// static kmp_critical_name lock = { 0 };
864 /// void reduce_func(void *lhs[<n>], void *rhs[<n>]) {
866 /// *(Type<i>*)lhs[i] = RedOp<i>(*(Type<i>*)lhs[i], *(Type<i>*)rhs[i]);
871 /// void *RedList[<n>] = {&<RHSExprs>[0], ..., &<RHSExprs>[<n>-1]};
872 /// switch (__kmpc_reduce{_nowait}(<loc>, <gtid>, <n>, sizeof(RedList),
873 /// RedList, reduce_func, &<lock>)) {
876 /// <LHSExprs>[i] = RedOp<i>(*<LHSExprs>[i], *<RHSExprs>[i]);
878 /// __kmpc_end_reduce{_nowait}(<loc>, <gtid>, &<lock>);
882 /// Atomic(<LHSExprs>[i] = RedOp<i>(*<LHSExprs>[i], *<RHSExprs>[i]));
889 /// \param Privates List of private copies for original reduction arguments.
890 /// \param LHSExprs List of LHS in \a ReductionOps reduction operations.
891 /// \param RHSExprs List of RHS in \a ReductionOps reduction operations.
892 /// \param ReductionOps List of reduction operations in form 'LHS binop RHS'
893 /// or 'operator binop(LHS, RHS)'.
894 /// \param WithNowait true if parent directive has also nowait clause, false
896 virtual void emitReduction(CodeGenFunction &CGF, SourceLocation Loc,
897 ArrayRef<const Expr *> Privates,
898 ArrayRef<const Expr *> LHSExprs,
899 ArrayRef<const Expr *> RHSExprs,
900 ArrayRef<const Expr *> ReductionOps,
901 bool WithNowait, bool SimpleReduction);
903 /// \brief Emit code for 'taskwait' directive.
904 virtual void emitTaskwaitCall(CodeGenFunction &CGF, SourceLocation Loc);
906 /// \brief Emit code for 'cancellation point' construct.
907 /// \param CancelRegion Region kind for which the cancellation point must be
910 virtual void emitCancellationPointCall(CodeGenFunction &CGF,
912 OpenMPDirectiveKind CancelRegion);
914 /// \brief Emit code for 'cancel' construct.
915 /// \param IfCond Condition in the associated 'if' clause, if it was
916 /// specified, nullptr otherwise.
917 /// \param CancelRegion Region kind for which the cancel must be emitted.
919 virtual void emitCancelCall(CodeGenFunction &CGF, SourceLocation Loc,
921 OpenMPDirectiveKind CancelRegion);
923 /// \brief Emit outilined function for 'target' directive.
924 /// \param D Directive to emit.
925 /// \param ParentName Name of the function that encloses the target region.
926 /// \param OutlinedFn Outlined function value to be defined by this call.
927 /// \param OutlinedFnID Outlined function ID value to be defined by this call.
928 /// \param IsOffloadEntry True if the outlined function is an offload entry.
929 /// An oulined function may not be an entry if, e.g. the if clause always
930 /// evaluates to false.
931 virtual void emitTargetOutlinedFunction(const OMPExecutableDirective &D,
932 StringRef ParentName,
933 llvm::Function *&OutlinedFn,
934 llvm::Constant *&OutlinedFnID,
935 bool IsOffloadEntry);
937 /// \brief Emit the target offloading code associated with \a D. The emitted
938 /// code attempts offloading the execution to the device, an the event of
939 /// a failure it executes the host version outlined in \a OutlinedFn.
940 /// \param D Directive to emit.
941 /// \param OutlinedFn Host version of the code to be offloaded.
942 /// \param OutlinedFnID ID of host version of the code to be offloaded.
943 /// \param IfCond Expression evaluated in if clause associated with the target
944 /// directive, or null if no if clause is used.
945 /// \param Device Expression evaluated in device clause associated with the
946 /// target directive, or null if no device clause is used.
947 /// \param CapturedVars Values captured in the current region.
948 virtual void emitTargetCall(CodeGenFunction &CGF,
949 const OMPExecutableDirective &D,
950 llvm::Value *OutlinedFn,
951 llvm::Value *OutlinedFnID, const Expr *IfCond,
953 ArrayRef<llvm::Value *> CapturedVars);
955 /// \brief Emit the target regions enclosed in \a GD function definition or
956 /// the function itself in case it is a valid device function. Returns true if
957 /// \a GD was dealt with successfully.
958 /// \param GD Function to scan.
959 virtual bool emitTargetFunctions(GlobalDecl GD);
961 /// \brief Emit the global variable if it is a valid device global variable.
962 /// Returns true if \a GD was dealt with successfully.
963 /// \param GD Variable declaration to emit.
964 virtual bool emitTargetGlobalVariable(GlobalDecl GD);
966 /// \brief Emit the global \a GD if it is meaningful for the target. Returns
967 /// if it was emitted succesfully.
968 /// \param GD Global to scan.
969 virtual bool emitTargetGlobal(GlobalDecl GD);
971 /// \brief Creates the offloading descriptor in the event any target region
972 /// was emitted in the current module and return the function that registers
974 virtual llvm::Function *emitRegistrationFunction();
977 } // namespace CodeGen