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
18 #include "clang/AST/Type.h"
19 #include "clang/Basic/OpenMPKinds.h"
20 #include "clang/Basic/SourceLocation.h"
21 #include "llvm/ADT/DenseMap.h"
22 #include "llvm/ADT/SmallPtrSet.h"
23 #include "llvm/ADT/StringMap.h"
24 #include "llvm/IR/Function.h"
25 #include "llvm/IR/ValueHandle.h"
40 class OMPDependClause;
41 class OMPExecutableDirective;
42 class OMPLoopDirective;
44 class OMPDeclareReductionDecl;
49 class CodeGenFunction;
52 /// A basic class for pre|post-action for advanced codegen sequence for OpenMP
54 class PrePostActionTy {
56 explicit PrePostActionTy() {}
57 virtual void Enter(CodeGenFunction &CGF) {}
58 virtual void Exit(CodeGenFunction &CGF) {}
59 virtual ~PrePostActionTy() {}
62 /// Class provides a way to call simple version of codegen for OpenMP region, or
63 /// an advanced with possible pre|post-actions in codegen.
64 class RegionCodeGenTy final {
66 typedef void (*CodeGenTy)(intptr_t, CodeGenFunction &, PrePostActionTy &);
68 mutable PrePostActionTy *PrePostAction;
69 RegionCodeGenTy() = delete;
70 RegionCodeGenTy &operator=(const RegionCodeGenTy &) = delete;
71 template <typename Callable>
72 static void CallbackFn(intptr_t CodeGen, CodeGenFunction &CGF,
73 PrePostActionTy &Action) {
74 return (*reinterpret_cast<Callable *>(CodeGen))(CGF, Action);
78 template <typename Callable>
81 typename std::enable_if<
82 !std::is_same<typename std::remove_reference<Callable>::type,
83 RegionCodeGenTy>::value>::type * = nullptr)
84 : CodeGen(reinterpret_cast<intptr_t>(&CodeGen)),
85 Callback(CallbackFn<typename std::remove_reference<Callable>::type>),
86 PrePostAction(nullptr) {}
87 void setAction(PrePostActionTy &Action) const { PrePostAction = &Action; }
88 void operator()(CodeGenFunction &CGF) const;
91 struct OMPTaskDataTy final {
92 SmallVector<const Expr *, 4> PrivateVars;
93 SmallVector<const Expr *, 4> PrivateCopies;
94 SmallVector<const Expr *, 4> FirstprivateVars;
95 SmallVector<const Expr *, 4> FirstprivateCopies;
96 SmallVector<const Expr *, 4> FirstprivateInits;
97 SmallVector<const Expr *, 4> LastprivateVars;
98 SmallVector<const Expr *, 4> LastprivateCopies;
99 SmallVector<const Expr *, 4> ReductionVars;
100 SmallVector<const Expr *, 4> ReductionCopies;
101 SmallVector<const Expr *, 4> ReductionOps;
102 SmallVector<std::pair<OpenMPDependClauseKind, const Expr *>, 4> Dependences;
103 llvm::PointerIntPair<llvm::Value *, 1, bool> Final;
104 llvm::PointerIntPair<llvm::Value *, 1, bool> Schedule;
105 llvm::PointerIntPair<llvm::Value *, 1, bool> Priority;
106 llvm::Value *Reductions = nullptr;
107 unsigned NumberOfParts = 0;
109 bool Nogroup = false;
112 /// Class intended to support codegen of all kind of the reduction clauses.
113 class ReductionCodeGen {
115 /// Data required for codegen of reduction clauses.
116 struct ReductionData {
117 /// Reference to the original shared item.
118 const Expr *Ref = nullptr;
119 /// Helper expression for generation of private copy.
120 const Expr *Private = nullptr;
121 /// Helper expression for generation reduction operation.
122 const Expr *ReductionOp = nullptr;
123 ReductionData(const Expr *Ref, const Expr *Private, const Expr *ReductionOp)
124 : Ref(Ref), Private(Private), ReductionOp(ReductionOp) {}
126 /// List of reduction-based clauses.
127 SmallVector<ReductionData, 4> ClausesData;
129 /// List of addresses of original shared variables/expressions.
130 SmallVector<std::pair<LValue, LValue>, 4> SharedAddresses;
131 /// Sizes of the reduction items in chars.
132 SmallVector<std::pair<llvm::Value *, llvm::Value *>, 4> Sizes;
133 /// Base declarations for the reduction items.
134 SmallVector<const VarDecl *, 4> BaseDecls;
136 /// Emits lvalue for shared expresion.
137 LValue emitSharedLValue(CodeGenFunction &CGF, const Expr *E);
138 /// Emits upper bound for shared expression (if array section).
139 LValue emitSharedLValueUB(CodeGenFunction &CGF, const Expr *E);
140 /// Performs aggregate initialization.
141 /// \param N Number of reduction item in the common list.
142 /// \param PrivateAddr Address of the corresponding private item.
143 /// \param SharedLVal Address of the original shared variable.
144 /// \param DRD Declare reduction construct used for reduction item.
145 void emitAggregateInitialization(CodeGenFunction &CGF, unsigned N,
146 Address PrivateAddr, LValue SharedLVal,
147 const OMPDeclareReductionDecl *DRD);
150 ReductionCodeGen(ArrayRef<const Expr *> Shareds,
151 ArrayRef<const Expr *> Privates,
152 ArrayRef<const Expr *> ReductionOps);
153 /// Emits lvalue for a reduction item.
154 /// \param N Number of the reduction item.
155 void emitSharedLValue(CodeGenFunction &CGF, unsigned N);
156 /// Emits the code for the variable-modified type, if required.
157 /// \param N Number of the reduction item.
158 void emitAggregateType(CodeGenFunction &CGF, unsigned N);
159 /// Emits the code for the variable-modified type, if required.
160 /// \param N Number of the reduction item.
161 /// \param Size Size of the type in chars.
162 void emitAggregateType(CodeGenFunction &CGF, unsigned N, llvm::Value *Size);
163 /// Performs initialization of the private copy for the reduction item.
164 /// \param N Number of the reduction item.
165 /// \param PrivateAddr Address of the corresponding private item.
166 /// \param DefaultInit Default initialization sequence that should be
167 /// performed if no reduction specific initialization is found.
168 /// \param SharedLVal Address of the original shared variable.
170 emitInitialization(CodeGenFunction &CGF, unsigned N, Address PrivateAddr,
172 llvm::function_ref<bool(CodeGenFunction &)> DefaultInit);
173 /// Returns true if the private copy requires cleanups.
174 bool needCleanups(unsigned N);
175 /// Emits cleanup code for the reduction item.
176 /// \param N Number of the reduction item.
177 /// \param PrivateAddr Address of the corresponding private item.
178 void emitCleanups(CodeGenFunction &CGF, unsigned N, Address PrivateAddr);
179 /// Adjusts \p PrivatedAddr for using instead of the original variable
180 /// address in normal operations.
181 /// \param N Number of the reduction item.
182 /// \param PrivateAddr Address of the corresponding private item.
183 Address adjustPrivateAddress(CodeGenFunction &CGF, unsigned N,
184 Address PrivateAddr);
185 /// Returns LValue for the reduction item.
186 LValue getSharedLValue(unsigned N) const { return SharedAddresses[N].first; }
187 /// Returns the size of the reduction item (in chars and total number of
188 /// elements in the item), or nullptr, if the size is a constant.
189 std::pair<llvm::Value *, llvm::Value *> getSizes(unsigned N) const {
192 /// Returns the base declaration of the reduction item.
193 const VarDecl *getBaseDecl(unsigned N) const { return BaseDecls[N]; }
194 /// Returns true if the initialization of the reduction item uses initializer
195 /// from declare reduction construct.
196 bool usesReductionInitializer(unsigned N) const;
199 class CGOpenMPRuntime {
203 /// \brief Creates offloading entry for the provided entry ID \a ID,
204 /// address \a Addr, size \a Size, and flags \a Flags.
205 virtual void createOffloadEntry(llvm::Constant *ID, llvm::Constant *Addr,
206 uint64_t Size, int32_t Flags = 0);
208 /// \brief Helper to emit outlined function for 'target' directive.
209 /// \param D Directive to emit.
210 /// \param ParentName Name of the function that encloses the target region.
211 /// \param OutlinedFn Outlined function value to be defined by this call.
212 /// \param OutlinedFnID Outlined function ID value to be defined by this call.
213 /// \param IsOffloadEntry True if the outlined function is an offload entry.
214 /// \param CodeGen Lambda codegen specific to an accelerator device.
215 /// An outlined function may not be an entry if, e.g. the if clause always
216 /// evaluates to false.
217 virtual void emitTargetOutlinedFunctionHelper(const OMPExecutableDirective &D,
218 StringRef ParentName,
219 llvm::Function *&OutlinedFn,
220 llvm::Constant *&OutlinedFnID,
222 const RegionCodeGenTy &CodeGen);
224 /// \brief Emits code for OpenMP 'if' clause using specified \a CodeGen
225 /// function. Here is the logic:
231 void emitOMPIfClause(CodeGenFunction &CGF, const Expr *Cond,
232 const RegionCodeGenTy &ThenGen,
233 const RegionCodeGenTy &ElseGen);
235 /// \brief Emits object of ident_t type with info for source location.
236 /// \param Flags Flags for OpenMP location.
238 llvm::Value *emitUpdateLocation(CodeGenFunction &CGF, SourceLocation Loc,
241 /// \brief Returns pointer to ident_t type.
242 llvm::Type *getIdentTyPointerTy();
244 /// \brief Gets thread id value for the current thread.
246 llvm::Value *getThreadID(CodeGenFunction &CGF, SourceLocation Loc);
248 /// \brief Get the function name of an outlined region.
249 // The name can be customized depending on the target.
251 virtual StringRef getOutlinedHelperName() const { return ".omp_outlined."; }
253 /// Emits \p Callee function call with arguments \p Args with location \p Loc.
254 void emitCall(CodeGenFunction &CGF, llvm::Value *Callee,
255 ArrayRef<llvm::Value *> Args = llvm::None,
256 SourceLocation Loc = SourceLocation()) const;
259 /// \brief Default const ident_t object used for initialization of all other
261 llvm::Constant *DefaultOpenMPPSource = nullptr;
262 /// \brief Map of flags and corresponding default locations.
263 typedef llvm::DenseMap<unsigned, llvm::Value *> OpenMPDefaultLocMapTy;
264 OpenMPDefaultLocMapTy OpenMPDefaultLocMap;
265 Address getOrCreateDefaultLocation(unsigned Flags);
267 llvm::StructType *IdentTy = nullptr;
268 /// \brief Map for SourceLocation and OpenMP runtime library debug locations.
269 typedef llvm::DenseMap<unsigned, llvm::Value *> OpenMPDebugLocMapTy;
270 OpenMPDebugLocMapTy OpenMPDebugLocMap;
271 /// \brief The type for a microtask which gets passed to __kmpc_fork_call().
272 /// Original representation is:
273 /// typedef void (kmpc_micro)(kmp_int32 global_tid, kmp_int32 bound_tid,...);
274 llvm::FunctionType *Kmpc_MicroTy = nullptr;
275 /// \brief Stores debug location and ThreadID for the function.
276 struct DebugLocThreadIdTy {
277 llvm::Value *DebugLoc;
278 llvm::Value *ThreadID;
280 /// \brief Map of local debug location, ThreadId and functions.
281 typedef llvm::DenseMap<llvm::Function *, DebugLocThreadIdTy>
282 OpenMPLocThreadIDMapTy;
283 OpenMPLocThreadIDMapTy OpenMPLocThreadIDMap;
284 /// Map of UDRs and corresponding combiner/initializer.
285 typedef llvm::DenseMap<const OMPDeclareReductionDecl *,
286 std::pair<llvm::Function *, llvm::Function *>>
289 /// Map of functions and locally defined UDRs.
290 typedef llvm::DenseMap<llvm::Function *,
291 SmallVector<const OMPDeclareReductionDecl *, 4>>
293 FunctionUDRMapTy FunctionUDRMap;
294 IdentifierInfo *In = nullptr;
295 IdentifierInfo *Out = nullptr;
296 IdentifierInfo *Priv = nullptr;
297 IdentifierInfo *Orig = nullptr;
298 /// \brief Type kmp_critical_name, originally defined as typedef kmp_int32
299 /// kmp_critical_name[8];
300 llvm::ArrayType *KmpCriticalNameTy;
301 /// \brief An ordered map of auto-generated variables to their unique names.
302 /// It stores variables with the following names: 1) ".gomp_critical_user_" +
303 /// <critical_section_name> + ".var" for "omp critical" directives; 2)
304 /// <mangled_name_for_global_var> + ".cache." for cache for threadprivate
306 llvm::StringMap<llvm::AssertingVH<llvm::Constant>, llvm::BumpPtrAllocator>
308 /// \brief Type typedef kmp_int32 (* kmp_routine_entry_t)(kmp_int32, void *);
309 llvm::Type *KmpRoutineEntryPtrTy = nullptr;
310 QualType KmpRoutineEntryPtrQTy;
311 /// \brief Type typedef struct kmp_task {
312 /// void * shareds; /**< pointer to block of pointers to
314 /// kmp_routine_entry_t routine; /**< pointer to routine to call for
315 /// executing task */
316 /// kmp_int32 part_id; /**< part id for the task */
317 /// kmp_routine_entry_t destructors; /* pointer to function to invoke
318 /// deconstructors of firstprivate C++ objects */
320 QualType KmpTaskTQTy;
321 /// Saved kmp_task_t for task directive.
322 QualType SavedKmpTaskTQTy;
323 /// Saved kmp_task_t for taskloop-based directive.
324 QualType SavedKmpTaskloopTQTy;
325 /// \brief Type typedef struct kmp_depend_info {
326 /// kmp_intptr_t base_addr;
332 /// } kmp_depend_info_t;
333 QualType KmpDependInfoTy;
334 /// struct kmp_dim { // loop bounds info casted to kmp_int64
335 /// kmp_int64 lo; // lower
336 /// kmp_int64 up; // upper
337 /// kmp_int64 st; // stride
340 /// \brief Type struct __tgt_offload_entry{
341 /// void *addr; // Pointer to the offload entry info.
342 /// // (function or global)
343 /// char *name; // Name of the function or global.
344 /// size_t size; // Size of the entry info (0 if it a function).
346 QualType TgtOffloadEntryQTy;
347 /// struct __tgt_device_image{
348 /// void *ImageStart; // Pointer to the target code start.
349 /// void *ImageEnd; // Pointer to the target code end.
350 /// // We also add the host entries to the device image, as it may be useful
351 /// // for the target runtime to have access to that information.
352 /// __tgt_offload_entry *EntriesBegin; // Begin of the table with all
354 /// __tgt_offload_entry *EntriesEnd; // End of the table with all the
355 /// // entries (non inclusive).
357 QualType TgtDeviceImageQTy;
358 /// struct __tgt_bin_desc{
359 /// int32_t NumDevices; // Number of devices supported.
360 /// __tgt_device_image *DeviceImages; // Arrays of device images
361 /// // (one per device).
362 /// __tgt_offload_entry *EntriesBegin; // Begin of the table with all the
364 /// __tgt_offload_entry *EntriesEnd; // End of the table with all the
365 /// // entries (non inclusive).
367 QualType TgtBinaryDescriptorQTy;
368 /// \brief Entity that registers the offloading constants that were emitted so
370 class OffloadEntriesInfoManagerTy {
373 /// \brief Number of entries registered so far.
374 unsigned OffloadingEntriesNum;
377 /// Base class of the entries info.
378 class OffloadEntryInfo {
380 /// Kind of a given entry. Currently, only target regions are
382 enum OffloadingEntryInfoKinds : unsigned {
383 // Entry is a target region.
384 OFFLOAD_ENTRY_INFO_TARGET_REGION = 0,
385 // Invalid entry info.
386 OFFLOAD_ENTRY_INFO_INVALID = ~0u
390 : Flags(0), Order(~0u), Kind(OFFLOAD_ENTRY_INFO_INVALID) {}
391 explicit OffloadEntryInfo(OffloadingEntryInfoKinds Kind, unsigned Order,
393 : Flags(Flags), Order(Order), Kind(Kind) {}
395 bool isValid() const { return Order != ~0u; }
396 unsigned getOrder() const { return Order; }
397 OffloadingEntryInfoKinds getKind() const { return Kind; }
398 int32_t getFlags() const { return Flags; }
399 void setFlags(int32_t NewFlags) { Flags = NewFlags; }
400 static bool classof(const OffloadEntryInfo *Info) { return true; }
403 /// Flags associated with the device global.
406 /// Order this entry was emitted.
409 OffloadingEntryInfoKinds Kind;
412 /// \brief Return true if a there are no entries defined.
414 /// \brief Return number of entries defined so far.
415 unsigned size() const { return OffloadingEntriesNum; }
416 OffloadEntriesInfoManagerTy(CodeGenModule &CGM)
417 : CGM(CGM), OffloadingEntriesNum(0) {}
420 /// Target region entries related.
422 /// \brief Target region entries info.
423 class OffloadEntryInfoTargetRegion : public OffloadEntryInfo {
424 // \brief Address of the entity that has to be mapped for offloading.
425 llvm::Constant *Addr;
426 // \brief Address that can be used as the ID of the entry.
430 OffloadEntryInfoTargetRegion()
431 : OffloadEntryInfo(OFFLOAD_ENTRY_INFO_TARGET_REGION, ~0u,
433 Addr(nullptr), ID(nullptr) {}
434 explicit OffloadEntryInfoTargetRegion(unsigned Order,
435 llvm::Constant *Addr,
436 llvm::Constant *ID, int32_t Flags)
437 : OffloadEntryInfo(OFFLOAD_ENTRY_INFO_TARGET_REGION, Order, Flags),
438 Addr(Addr), ID(ID) {}
440 llvm::Constant *getAddress() const { return Addr; }
441 llvm::Constant *getID() const { return ID; }
442 void setAddress(llvm::Constant *V) {
443 assert(!Addr && "Address as been set before!");
446 void setID(llvm::Constant *V) {
447 assert(!ID && "ID as been set before!");
450 static bool classof(const OffloadEntryInfo *Info) {
451 return Info->getKind() == OFFLOAD_ENTRY_INFO_TARGET_REGION;
454 /// \brief Initialize target region entry.
455 void initializeTargetRegionEntryInfo(unsigned DeviceID, unsigned FileID,
456 StringRef ParentName, unsigned LineNum,
458 /// \brief Register target region entry.
459 void registerTargetRegionEntryInfo(unsigned DeviceID, unsigned FileID,
460 StringRef ParentName, unsigned LineNum,
461 llvm::Constant *Addr, llvm::Constant *ID,
463 /// \brief Return true if a target region entry with the provided
464 /// information exists.
465 bool hasTargetRegionEntryInfo(unsigned DeviceID, unsigned FileID,
466 StringRef ParentName, unsigned LineNum) const;
467 /// brief Applies action \a Action on all registered entries.
468 typedef llvm::function_ref<void(unsigned, unsigned, StringRef, unsigned,
469 OffloadEntryInfoTargetRegion &)>
470 OffloadTargetRegionEntryInfoActTy;
471 void actOnTargetRegionEntriesInfo(
472 const OffloadTargetRegionEntryInfoActTy &Action);
475 // Storage for target region entries kind. The storage is to be indexed by
476 // file ID, device ID, parent function name and line number.
477 typedef llvm::DenseMap<unsigned, OffloadEntryInfoTargetRegion>
478 OffloadEntriesTargetRegionPerLine;
479 typedef llvm::StringMap<OffloadEntriesTargetRegionPerLine>
480 OffloadEntriesTargetRegionPerParentName;
481 typedef llvm::DenseMap<unsigned, OffloadEntriesTargetRegionPerParentName>
482 OffloadEntriesTargetRegionPerFile;
483 typedef llvm::DenseMap<unsigned, OffloadEntriesTargetRegionPerFile>
484 OffloadEntriesTargetRegionPerDevice;
485 typedef OffloadEntriesTargetRegionPerDevice OffloadEntriesTargetRegionTy;
486 OffloadEntriesTargetRegionTy OffloadEntriesTargetRegion;
488 OffloadEntriesInfoManagerTy OffloadEntriesInfoManager;
490 /// \brief Creates and registers offloading binary descriptor for the current
491 /// compilation unit. The function that does the registration is returned.
492 llvm::Function *createOffloadingBinaryDescriptorRegistration();
494 /// \brief Creates all the offload entries in the current compilation unit
495 /// along with the associated metadata.
496 void createOffloadEntriesAndInfoMetadata();
498 /// \brief Loads all the offload entries information from the host IR
500 void loadOffloadInfoMetadata();
502 /// \brief Returns __tgt_offload_entry type.
503 QualType getTgtOffloadEntryQTy();
505 /// \brief Returns __tgt_device_image type.
506 QualType getTgtDeviceImageQTy();
508 /// \brief Returns __tgt_bin_desc type.
509 QualType getTgtBinaryDescriptorQTy();
511 /// \brief Start scanning from statement \a S and and emit all target regions
512 /// found along the way.
513 /// \param S Starting statement.
514 /// \param ParentName Name of the function declaration that is being scanned.
515 void scanForTargetRegionsFunctions(const Stmt *S, StringRef ParentName);
517 /// \brief Build type kmp_routine_entry_t (if not built yet).
518 void emitKmpRoutineEntryT(QualType KmpInt32Ty);
520 /// \brief Returns pointer to kmpc_micro type.
521 llvm::Type *getKmpc_MicroPointerTy();
523 /// \brief Returns specified OpenMP runtime function.
524 /// \param Function OpenMP runtime function.
525 /// \return Specified function.
526 llvm::Constant *createRuntimeFunction(unsigned Function);
528 /// \brief Returns __kmpc_for_static_init_* runtime function for the specified
529 /// size \a IVSize and sign \a IVSigned.
530 llvm::Constant *createForStaticInitFunction(unsigned IVSize, bool IVSigned);
532 /// \brief Returns __kmpc_dispatch_init_* runtime function for the specified
533 /// size \a IVSize and sign \a IVSigned.
534 llvm::Constant *createDispatchInitFunction(unsigned IVSize, bool IVSigned);
536 /// \brief Returns __kmpc_dispatch_next_* runtime function for the specified
537 /// size \a IVSize and sign \a IVSigned.
538 llvm::Constant *createDispatchNextFunction(unsigned IVSize, bool IVSigned);
540 /// \brief Returns __kmpc_dispatch_fini_* runtime function for the specified
541 /// size \a IVSize and sign \a IVSigned.
542 llvm::Constant *createDispatchFiniFunction(unsigned IVSize, bool IVSigned);
544 /// \brief If the specified mangled name is not in the module, create and
545 /// return threadprivate cache object. This object is a pointer's worth of
546 /// storage that's reserved for use by the OpenMP runtime.
547 /// \param VD Threadprivate variable.
548 /// \return Cache variable for the specified threadprivate.
549 llvm::Constant *getOrCreateThreadPrivateCache(const VarDecl *VD);
551 /// \brief Emits address of the word in a memory where current thread id is
553 virtual Address emitThreadIDAddress(CodeGenFunction &CGF, SourceLocation Loc);
555 /// \brief Gets (if variable with the given name already exist) or creates
556 /// internal global variable with the specified Name. The created variable has
557 /// linkage CommonLinkage by default and is initialized by null value.
558 /// \param Ty Type of the global variable. If it is exist already the type
559 /// must be the same.
560 /// \param Name Name of the variable.
561 llvm::Constant *getOrCreateInternalVariable(llvm::Type *Ty,
562 const llvm::Twine &Name);
564 /// \brief Set of threadprivate variables with the generated initializer.
565 llvm::SmallPtrSet<const VarDecl *, 4> ThreadPrivateWithDefinition;
567 /// \brief Emits initialization code for the threadprivate variables.
568 /// \param VDAddr Address of the global variable \a VD.
569 /// \param Ctor Pointer to a global init function for \a VD.
570 /// \param CopyCtor Pointer to a global copy function for \a VD.
571 /// \param Dtor Pointer to a global destructor function for \a VD.
572 /// \param Loc Location of threadprivate declaration.
573 void emitThreadPrivateVarInit(CodeGenFunction &CGF, Address VDAddr,
574 llvm::Value *Ctor, llvm::Value *CopyCtor,
575 llvm::Value *Dtor, SourceLocation Loc);
577 /// \brief Returns corresponding lock object for the specified critical region
578 /// name. If the lock object does not exist it is created, otherwise the
579 /// reference to the existing copy is returned.
580 /// \param CriticalName Name of the critical region.
582 llvm::Value *getCriticalRegionLock(StringRef CriticalName);
584 struct TaskResultTy {
585 llvm::Value *NewTask = nullptr;
586 llvm::Value *TaskEntry = nullptr;
587 llvm::Value *NewTaskNewTaskTTy = nullptr;
589 RecordDecl *KmpTaskTQTyRD = nullptr;
590 llvm::Value *TaskDupFn = nullptr;
592 /// Emit task region for the task directive. The task region is emitted in
594 /// 1. Emit a call to kmp_task_t *__kmpc_omp_task_alloc(ident_t *, kmp_int32
595 /// gtid, kmp_int32 flags, size_t sizeof_kmp_task_t, size_t sizeof_shareds,
596 /// kmp_routine_entry_t *task_entry). Here task_entry is a pointer to the
598 /// kmp_int32 .omp_task_entry.(kmp_int32 gtid, kmp_task_t *tt) {
599 /// TaskFunction(gtid, tt->part_id, tt->shareds);
602 /// 2. Copy a list of shared variables to field shareds of the resulting
603 /// structure kmp_task_t returned by the previous call (if any).
604 /// 3. Copy a pointer to destructions function to field destructions of the
605 /// resulting structure kmp_task_t.
606 /// \param D Current task directive.
607 /// \param TaskFunction An LLVM function with type void (*)(i32 /*gtid*/, i32
608 /// /*part_id*/, captured_struct */*__context*/);
609 /// \param SharedsTy A type which contains references the shared variables.
610 /// \param Shareds Context with the list of shared variables from the \p
612 /// \param Data Additional data for task generation like tiednsee, final
613 /// state, list of privates etc.
614 TaskResultTy emitTaskInit(CodeGenFunction &CGF, SourceLocation Loc,
615 const OMPExecutableDirective &D,
616 llvm::Value *TaskFunction, QualType SharedsTy,
617 Address Shareds, const OMPTaskDataTy &Data);
620 explicit CGOpenMPRuntime(CodeGenModule &CGM);
621 virtual ~CGOpenMPRuntime() {}
622 virtual void clear();
624 /// Emit code for the specified user defined reduction construct.
625 virtual void emitUserDefinedReduction(CodeGenFunction *CGF,
626 const OMPDeclareReductionDecl *D);
627 /// Get combiner/initializer for the specified user-defined reduction, if any.
628 virtual std::pair<llvm::Function *, llvm::Function *>
629 getUserDefinedReduction(const OMPDeclareReductionDecl *D);
631 /// \brief Emits outlined function for the specified OpenMP parallel directive
632 /// \a D. This outlined function has type void(*)(kmp_int32 *ThreadID,
633 /// kmp_int32 BoundID, struct context_vars*).
634 /// \param D OpenMP directive.
635 /// \param ThreadIDVar Variable for thread id in the current OpenMP region.
636 /// \param InnermostKind Kind of innermost directive (for simple directives it
637 /// is a directive itself, for combined - its innermost directive).
638 /// \param CodeGen Code generation sequence for the \a D directive.
639 virtual llvm::Value *emitParallelOutlinedFunction(
640 const OMPExecutableDirective &D, const VarDecl *ThreadIDVar,
641 OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen);
643 /// \brief Emits outlined function for the specified OpenMP teams directive
644 /// \a D. This outlined function has type void(*)(kmp_int32 *ThreadID,
645 /// kmp_int32 BoundID, struct context_vars*).
646 /// \param D OpenMP directive.
647 /// \param ThreadIDVar Variable for thread id in the current OpenMP region.
648 /// \param InnermostKind Kind of innermost directive (for simple directives it
649 /// is a directive itself, for combined - its innermost directive).
650 /// \param CodeGen Code generation sequence for the \a D directive.
651 virtual llvm::Value *emitTeamsOutlinedFunction(
652 const OMPExecutableDirective &D, const VarDecl *ThreadIDVar,
653 OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen);
655 /// \brief Emits outlined function for the OpenMP task directive \a D. This
656 /// outlined function has type void(*)(kmp_int32 ThreadID, struct task_t*
658 /// \param D OpenMP directive.
659 /// \param ThreadIDVar Variable for thread id in the current OpenMP region.
660 /// \param PartIDVar Variable for partition id in the current OpenMP untied
662 /// \param TaskTVar Variable for task_t argument.
663 /// \param InnermostKind Kind of innermost directive (for simple directives it
664 /// is a directive itself, for combined - its innermost directive).
665 /// \param CodeGen Code generation sequence for the \a D directive.
666 /// \param Tied true if task is generated for tied task, false otherwise.
667 /// \param NumberOfParts Number of parts in untied task. Ignored for tied
670 virtual llvm::Value *emitTaskOutlinedFunction(
671 const OMPExecutableDirective &D, const VarDecl *ThreadIDVar,
672 const VarDecl *PartIDVar, const VarDecl *TaskTVar,
673 OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen,
674 bool Tied, unsigned &NumberOfParts);
676 /// \brief Cleans up references to the objects in finished function.
678 void functionFinished(CodeGenFunction &CGF);
680 /// \brief Emits code for parallel or serial call of the \a OutlinedFn with
681 /// variables captured in a record which address is stored in \a
683 /// \param OutlinedFn Outlined function to be run in parallel threads. Type of
684 /// this function is void(*)(kmp_int32 *, kmp_int32, struct context_vars*).
685 /// \param CapturedVars A pointer to the record with the references to
686 /// variables used in \a OutlinedFn function.
687 /// \param IfCond Condition in the associated 'if' clause, if it was
688 /// specified, nullptr otherwise.
690 virtual void emitParallelCall(CodeGenFunction &CGF, SourceLocation Loc,
691 llvm::Value *OutlinedFn,
692 ArrayRef<llvm::Value *> CapturedVars,
695 /// \brief Emits a critical region.
696 /// \param CriticalName Name of the critical region.
697 /// \param CriticalOpGen Generator for the statement associated with the given
699 /// \param Hint Value of the 'hint' clause (optional).
700 virtual void emitCriticalRegion(CodeGenFunction &CGF, StringRef CriticalName,
701 const RegionCodeGenTy &CriticalOpGen,
703 const Expr *Hint = nullptr);
705 /// \brief Emits a master region.
706 /// \param MasterOpGen Generator for the statement associated with the given
708 virtual void emitMasterRegion(CodeGenFunction &CGF,
709 const RegionCodeGenTy &MasterOpGen,
712 /// \brief Emits code for a taskyield directive.
713 virtual void emitTaskyieldCall(CodeGenFunction &CGF, SourceLocation Loc);
715 /// \brief Emit a taskgroup region.
716 /// \param TaskgroupOpGen Generator for the statement associated with the
717 /// given taskgroup region.
718 virtual void emitTaskgroupRegion(CodeGenFunction &CGF,
719 const RegionCodeGenTy &TaskgroupOpGen,
722 /// \brief Emits a single region.
723 /// \param SingleOpGen Generator for the statement associated with the given
725 virtual void emitSingleRegion(CodeGenFunction &CGF,
726 const RegionCodeGenTy &SingleOpGen,
728 ArrayRef<const Expr *> CopyprivateVars,
729 ArrayRef<const Expr *> DestExprs,
730 ArrayRef<const Expr *> SrcExprs,
731 ArrayRef<const Expr *> AssignmentOps);
733 /// \brief Emit an ordered region.
734 /// \param OrderedOpGen Generator for the statement associated with the given
736 virtual void emitOrderedRegion(CodeGenFunction &CGF,
737 const RegionCodeGenTy &OrderedOpGen,
738 SourceLocation Loc, bool IsThreads);
740 /// \brief Emit an implicit/explicit barrier for OpenMP threads.
741 /// \param Kind Directive for which this implicit barrier call must be
742 /// generated. Must be OMPD_barrier for explicit barrier generation.
743 /// \param EmitChecks true if need to emit checks for cancellation barriers.
744 /// \param ForceSimpleCall true simple barrier call must be emitted, false if
745 /// runtime class decides which one to emit (simple or with cancellation
748 virtual void emitBarrierCall(CodeGenFunction &CGF, SourceLocation Loc,
749 OpenMPDirectiveKind Kind,
750 bool EmitChecks = true,
751 bool ForceSimpleCall = false);
753 /// \brief Check if the specified \a ScheduleKind is static non-chunked.
754 /// This kind of worksharing directive is emitted without outer loop.
755 /// \param ScheduleKind Schedule kind specified in the 'schedule' clause.
756 /// \param Chunked True if chunk is specified in the clause.
758 virtual bool isStaticNonchunked(OpenMPScheduleClauseKind ScheduleKind,
761 /// \brief Check if the specified \a ScheduleKind is static non-chunked.
762 /// This kind of distribute directive is emitted without outer loop.
763 /// \param ScheduleKind Schedule kind specified in the 'dist_schedule' clause.
764 /// \param Chunked True if chunk is specified in the clause.
766 virtual bool isStaticNonchunked(OpenMPDistScheduleClauseKind ScheduleKind,
769 /// \brief Check if the specified \a ScheduleKind is dynamic.
770 /// This kind of worksharing directive is emitted without outer loop.
771 /// \param ScheduleKind Schedule Kind specified in the 'schedule' clause.
773 virtual bool isDynamic(OpenMPScheduleClauseKind ScheduleKind) const;
775 /// struct with the values to be passed to the dispatch runtime function
776 struct DispatchRTInput {
778 llvm::Value *LB = nullptr;
780 llvm::Value *UB = nullptr;
781 /// Chunk size specified using 'schedule' clause (nullptr if chunk
782 /// was not specified)
783 llvm::Value *Chunk = nullptr;
784 DispatchRTInput() = default;
785 DispatchRTInput(llvm::Value *LB, llvm::Value *UB, llvm::Value *Chunk)
786 : LB(LB), UB(UB), Chunk(Chunk) {}
789 /// Call the appropriate runtime routine to initialize it before start
792 /// This is used for non static scheduled types and when the ordered
793 /// clause is present on the loop construct.
794 /// Depending on the loop schedule, it is necessary to call some runtime
795 /// routine before start of the OpenMP loop to get the loop upper / lower
796 /// bounds \a LB and \a UB and stride \a ST.
798 /// \param CGF Reference to current CodeGenFunction.
799 /// \param Loc Clang source location.
800 /// \param ScheduleKind Schedule kind, specified by the 'schedule' clause.
801 /// \param IVSize Size of the iteration variable in bits.
802 /// \param IVSigned Sign of the iteration variable.
803 /// \param Ordered true if loop is ordered, false otherwise.
804 /// \param DispatchValues struct containing llvm values for lower bound, upper
805 /// bound, and chunk expression.
806 /// For the default (nullptr) value, the chunk 1 will be used.
808 virtual void emitForDispatchInit(CodeGenFunction &CGF, SourceLocation Loc,
809 const OpenMPScheduleTy &ScheduleKind,
810 unsigned IVSize, bool IVSigned, bool Ordered,
811 const DispatchRTInput &DispatchValues);
813 /// Struct with the values to be passed to the static runtime function
814 struct StaticRTInput {
815 /// Size of the iteration variable in bits.
817 /// Sign of the iteration variable.
818 bool IVSigned = false;
819 /// true if loop is ordered, false otherwise.
820 bool Ordered = false;
821 /// Address of the output variable in which the flag of the last iteration
823 Address IL = Address::invalid();
824 /// Address of the output variable in which the lower iteration number is
826 Address LB = Address::invalid();
827 /// Address of the output variable in which the upper iteration number is
829 Address UB = Address::invalid();
830 /// Address of the output variable in which the stride value is returned
831 /// necessary to generated the static_chunked scheduled loop.
832 Address ST = Address::invalid();
833 /// Value of the chunk for the static_chunked scheduled loop. For the
834 /// default (nullptr) value, the chunk 1 will be used.
835 llvm::Value *Chunk = nullptr;
836 StaticRTInput(unsigned IVSize, bool IVSigned, bool Ordered, Address IL,
837 Address LB, Address UB, Address ST,
838 llvm::Value *Chunk = nullptr)
839 : IVSize(IVSize), IVSigned(IVSigned), Ordered(Ordered), IL(IL), LB(LB),
840 UB(UB), ST(ST), Chunk(Chunk) {}
842 /// \brief Call the appropriate runtime routine to initialize it before start
845 /// This is used only in case of static schedule, when the user did not
846 /// specify a ordered clause on the loop construct.
847 /// Depending on the loop schedule, it is necessary to call some runtime
848 /// routine before start of the OpenMP loop to get the loop upper / lower
849 /// bounds LB and UB and stride ST.
851 /// \param CGF Reference to current CodeGenFunction.
852 /// \param Loc Clang source location.
853 /// \param DKind Kind of the directive.
854 /// \param ScheduleKind Schedule kind, specified by the 'schedule' clause.
855 /// \param Values Input arguments for the construct.
857 virtual void emitForStaticInit(CodeGenFunction &CGF, SourceLocation Loc,
858 OpenMPDirectiveKind DKind,
859 const OpenMPScheduleTy &ScheduleKind,
860 const StaticRTInput &Values);
863 /// \param CGF Reference to current CodeGenFunction.
864 /// \param Loc Clang source location.
865 /// \param SchedKind Schedule kind, specified by the 'dist_schedule' clause.
866 /// \param Values Input arguments for the construct.
868 virtual void emitDistributeStaticInit(CodeGenFunction &CGF,
870 OpenMPDistScheduleClauseKind SchedKind,
871 const StaticRTInput &Values);
873 /// \brief Call the appropriate runtime routine to notify that we finished
874 /// iteration of the ordered loop with the dynamic scheduling.
876 /// \param CGF Reference to current CodeGenFunction.
877 /// \param Loc Clang source location.
878 /// \param IVSize Size of the iteration variable in bits.
879 /// \param IVSigned Sign of the iteration variable.
881 virtual void emitForOrderedIterationEnd(CodeGenFunction &CGF,
882 SourceLocation Loc, unsigned IVSize,
885 /// \brief Call the appropriate runtime routine to notify that we finished
886 /// all the work with current loop.
888 /// \param CGF Reference to current CodeGenFunction.
889 /// \param Loc Clang source location.
890 /// \param DKind Kind of the directive for which the static finish is emitted.
892 virtual void emitForStaticFinish(CodeGenFunction &CGF, SourceLocation Loc,
893 OpenMPDirectiveKind DKind);
895 /// Call __kmpc_dispatch_next(
896 /// ident_t *loc, kmp_int32 tid, kmp_int32 *p_lastiter,
897 /// kmp_int[32|64] *p_lower, kmp_int[32|64] *p_upper,
898 /// kmp_int[32|64] *p_stride);
899 /// \param IVSize Size of the iteration variable in bits.
900 /// \param IVSigned Sign of the iteration variable.
901 /// \param IL Address of the output variable in which the flag of the
902 /// last iteration is returned.
903 /// \param LB Address of the output variable in which the lower iteration
904 /// number is returned.
905 /// \param UB Address of the output variable in which the upper iteration
906 /// number is returned.
907 /// \param ST Address of the output variable in which the stride value is
909 virtual llvm::Value *emitForNext(CodeGenFunction &CGF, SourceLocation Loc,
910 unsigned IVSize, bool IVSigned,
911 Address IL, Address LB,
912 Address UB, Address ST);
914 /// \brief Emits call to void __kmpc_push_num_threads(ident_t *loc, kmp_int32
915 /// global_tid, kmp_int32 num_threads) to generate code for 'num_threads'
917 /// \param NumThreads An integer value of threads.
918 virtual void emitNumThreadsClause(CodeGenFunction &CGF,
919 llvm::Value *NumThreads,
922 /// \brief Emit call to void __kmpc_push_proc_bind(ident_t *loc, kmp_int32
923 /// global_tid, int proc_bind) to generate code for 'proc_bind' clause.
924 virtual void emitProcBindClause(CodeGenFunction &CGF,
925 OpenMPProcBindClauseKind ProcBind,
928 /// \brief Returns address of the threadprivate variable for the current
930 /// \param VD Threadprivate variable.
931 /// \param VDAddr Address of the global variable \a VD.
932 /// \param Loc Location of the reference to threadprivate var.
933 /// \return Address of the threadprivate variable for the current thread.
934 virtual Address getAddrOfThreadPrivate(CodeGenFunction &CGF,
939 /// \brief Emit a code for initialization of threadprivate variable. It emits
940 /// a call to runtime library which adds initial value to the newly created
941 /// threadprivate variable (if it is not constant) and registers destructor
942 /// for the variable (if any).
943 /// \param VD Threadprivate variable.
944 /// \param VDAddr Address of the global variable \a VD.
945 /// \param Loc Location of threadprivate declaration.
946 /// \param PerformInit true if initialization expression is not constant.
947 virtual llvm::Function *
948 emitThreadPrivateVarDefinition(const VarDecl *VD, Address VDAddr,
949 SourceLocation Loc, bool PerformInit,
950 CodeGenFunction *CGF = nullptr);
952 /// Creates artificial threadprivate variable with name \p Name and type \p
954 /// \param VarType Type of the artificial threadprivate variable.
955 /// \param Name Name of the artificial threadprivate variable.
956 virtual Address getAddrOfArtificialThreadPrivate(CodeGenFunction &CGF,
960 /// \brief Emit flush of the variables specified in 'omp flush' directive.
961 /// \param Vars List of variables to flush.
962 virtual void emitFlush(CodeGenFunction &CGF, ArrayRef<const Expr *> Vars,
965 /// \brief Emit task region for the task directive. The task region is
966 /// emitted in several steps:
967 /// 1. Emit a call to kmp_task_t *__kmpc_omp_task_alloc(ident_t *, kmp_int32
968 /// gtid, kmp_int32 flags, size_t sizeof_kmp_task_t, size_t sizeof_shareds,
969 /// kmp_routine_entry_t *task_entry). Here task_entry is a pointer to the
971 /// kmp_int32 .omp_task_entry.(kmp_int32 gtid, kmp_task_t *tt) {
972 /// TaskFunction(gtid, tt->part_id, tt->shareds);
975 /// 2. Copy a list of shared variables to field shareds of the resulting
976 /// structure kmp_task_t returned by the previous call (if any).
977 /// 3. Copy a pointer to destructions function to field destructions of the
978 /// resulting structure kmp_task_t.
979 /// 4. Emit a call to kmp_int32 __kmpc_omp_task(ident_t *, kmp_int32 gtid,
980 /// kmp_task_t *new_task), where new_task is a resulting structure from
982 /// \param D Current task directive.
983 /// \param TaskFunction An LLVM function with type void (*)(i32 /*gtid*/, i32
984 /// /*part_id*/, captured_struct */*__context*/);
985 /// \param SharedsTy A type which contains references the shared variables.
986 /// \param Shareds Context with the list of shared variables from the \p
988 /// \param IfCond Not a nullptr if 'if' clause was specified, nullptr
990 /// \param Data Additional data for task generation like tiednsee, final
991 /// state, list of privates etc.
992 virtual void emitTaskCall(CodeGenFunction &CGF, SourceLocation Loc,
993 const OMPExecutableDirective &D,
994 llvm::Value *TaskFunction, QualType SharedsTy,
995 Address Shareds, const Expr *IfCond,
996 const OMPTaskDataTy &Data);
998 /// Emit task region for the taskloop directive. The taskloop region is
999 /// emitted in several steps:
1000 /// 1. Emit a call to kmp_task_t *__kmpc_omp_task_alloc(ident_t *, kmp_int32
1001 /// gtid, kmp_int32 flags, size_t sizeof_kmp_task_t, size_t sizeof_shareds,
1002 /// kmp_routine_entry_t *task_entry). Here task_entry is a pointer to the
1004 /// kmp_int32 .omp_task_entry.(kmp_int32 gtid, kmp_task_t *tt) {
1005 /// TaskFunction(gtid, tt->part_id, tt->shareds);
1008 /// 2. Copy a list of shared variables to field shareds of the resulting
1009 /// structure kmp_task_t returned by the previous call (if any).
1010 /// 3. Copy a pointer to destructions function to field destructions of the
1011 /// resulting structure kmp_task_t.
1012 /// 4. Emit a call to void __kmpc_taskloop(ident_t *loc, int gtid, kmp_task_t
1013 /// *task, int if_val, kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st, int
1014 /// nogroup, int sched, kmp_uint64 grainsize, void *task_dup ), where new_task
1015 /// is a resulting structure from
1017 /// \param D Current task directive.
1018 /// \param TaskFunction An LLVM function with type void (*)(i32 /*gtid*/, i32
1019 /// /*part_id*/, captured_struct */*__context*/);
1020 /// \param SharedsTy A type which contains references the shared variables.
1021 /// \param Shareds Context with the list of shared variables from the \p
1023 /// \param IfCond Not a nullptr if 'if' clause was specified, nullptr
1025 /// \param Data Additional data for task generation like tiednsee, final
1026 /// state, list of privates etc.
1027 virtual void emitTaskLoopCall(
1028 CodeGenFunction &CGF, SourceLocation Loc, const OMPLoopDirective &D,
1029 llvm::Value *TaskFunction, QualType SharedsTy, Address Shareds,
1030 const Expr *IfCond, const OMPTaskDataTy &Data);
1032 /// \brief Emit code for the directive that does not require outlining.
1034 /// \param InnermostKind Kind of innermost directive (for simple directives it
1035 /// is a directive itself, for combined - its innermost directive).
1036 /// \param CodeGen Code generation sequence for the \a D directive.
1037 /// \param HasCancel true if region has inner cancel directive, false
1039 virtual void emitInlinedDirective(CodeGenFunction &CGF,
1040 OpenMPDirectiveKind InnermostKind,
1041 const RegionCodeGenTy &CodeGen,
1042 bool HasCancel = false);
1044 /// Emits reduction function.
1045 /// \param ArgsType Array type containing pointers to reduction variables.
1046 /// \param Privates List of private copies for original reduction arguments.
1047 /// \param LHSExprs List of LHS in \a ReductionOps reduction operations.
1048 /// \param RHSExprs List of RHS in \a ReductionOps reduction operations.
1049 /// \param ReductionOps List of reduction operations in form 'LHS binop RHS'
1050 /// or 'operator binop(LHS, RHS)'.
1051 llvm::Value *emitReductionFunction(CodeGenModule &CGM, llvm::Type *ArgsType,
1052 ArrayRef<const Expr *> Privates,
1053 ArrayRef<const Expr *> LHSExprs,
1054 ArrayRef<const Expr *> RHSExprs,
1055 ArrayRef<const Expr *> ReductionOps);
1057 /// Emits single reduction combiner
1058 void emitSingleReductionCombiner(CodeGenFunction &CGF,
1059 const Expr *ReductionOp,
1060 const Expr *PrivateRef,
1061 const DeclRefExpr *LHS,
1062 const DeclRefExpr *RHS);
1064 struct ReductionOptionsTy {
1066 bool SimpleReduction;
1067 OpenMPDirectiveKind ReductionKind;
1069 /// \brief Emit a code for reduction clause. Next code should be emitted for
1073 /// static kmp_critical_name lock = { 0 };
1075 /// void reduce_func(void *lhs[<n>], void *rhs[<n>]) {
1077 /// *(Type<i>*)lhs[i] = RedOp<i>(*(Type<i>*)lhs[i], *(Type<i>*)rhs[i]);
1082 /// void *RedList[<n>] = {&<RHSExprs>[0], ..., &<RHSExprs>[<n>-1]};
1083 /// switch (__kmpc_reduce{_nowait}(<loc>, <gtid>, <n>, sizeof(RedList),
1084 /// RedList, reduce_func, &<lock>)) {
1087 /// <LHSExprs>[i] = RedOp<i>(*<LHSExprs>[i], *<RHSExprs>[i]);
1089 /// __kmpc_end_reduce{_nowait}(<loc>, <gtid>, &<lock>);
1093 /// Atomic(<LHSExprs>[i] = RedOp<i>(*<LHSExprs>[i], *<RHSExprs>[i]));
1100 /// \param Privates List of private copies for original reduction arguments.
1101 /// \param LHSExprs List of LHS in \a ReductionOps reduction operations.
1102 /// \param RHSExprs List of RHS in \a ReductionOps reduction operations.
1103 /// \param ReductionOps List of reduction operations in form 'LHS binop RHS'
1104 /// or 'operator binop(LHS, RHS)'.
1105 /// \param Options List of options for reduction codegen:
1106 /// WithNowait true if parent directive has also nowait clause, false
1108 /// SimpleReduction Emit reduction operation only. Used for omp simd
1109 /// directive on the host.
1110 /// ReductionKind The kind of reduction to perform.
1111 virtual void emitReduction(CodeGenFunction &CGF, SourceLocation Loc,
1112 ArrayRef<const Expr *> Privates,
1113 ArrayRef<const Expr *> LHSExprs,
1114 ArrayRef<const Expr *> RHSExprs,
1115 ArrayRef<const Expr *> ReductionOps,
1116 ReductionOptionsTy Options);
1118 /// Emit a code for initialization of task reduction clause. Next code
1119 /// should be emitted for reduction:
1122 /// _task_red_item_t red_data[n];
1124 /// red_data[i].shar = &origs[i];
1125 /// red_data[i].size = sizeof(origs[i]);
1126 /// red_data[i].f_init = (void*)RedInit<i>;
1127 /// red_data[i].f_fini = (void*)RedDest<i>;
1128 /// red_data[i].f_comb = (void*)RedOp<i>;
1129 /// red_data[i].flags = <Flag_i>;
1131 /// void* tg1 = __kmpc_task_reduction_init(gtid, n, red_data);
1134 /// \param LHSExprs List of LHS in \a Data.ReductionOps reduction operations.
1135 /// \param RHSExprs List of RHS in \a Data.ReductionOps reduction operations.
1136 /// \param Data Additional data for task generation like tiedness, final
1137 /// state, list of privates, reductions etc.
1138 virtual llvm::Value *emitTaskReductionInit(CodeGenFunction &CGF,
1140 ArrayRef<const Expr *> LHSExprs,
1141 ArrayRef<const Expr *> RHSExprs,
1142 const OMPTaskDataTy &Data);
1144 /// Required to resolve existing problems in the runtime. Emits threadprivate
1145 /// variables to store the size of the VLAs/array sections for
1146 /// initializer/combiner/finalizer functions + emits threadprivate variable to
1147 /// store the pointer to the original reduction item for the custom
1148 /// initializer defined by declare reduction construct.
1149 /// \param RCG Allows to reuse an existing data for the reductions.
1150 /// \param N Reduction item for which fixups must be emitted.
1151 virtual void emitTaskReductionFixups(CodeGenFunction &CGF, SourceLocation Loc,
1152 ReductionCodeGen &RCG, unsigned N);
1154 /// Get the address of `void *` type of the privatue copy of the reduction
1155 /// item specified by the \p SharedLVal.
1156 /// \param ReductionsPtr Pointer to the reduction data returned by the
1157 /// emitTaskReductionInit function.
1158 /// \param SharedLVal Address of the original reduction item.
1159 virtual Address getTaskReductionItem(CodeGenFunction &CGF, SourceLocation Loc,
1160 llvm::Value *ReductionsPtr,
1163 /// \brief Emit code for 'taskwait' directive.
1164 virtual void emitTaskwaitCall(CodeGenFunction &CGF, SourceLocation Loc);
1166 /// \brief Emit code for 'cancellation point' construct.
1167 /// \param CancelRegion Region kind for which the cancellation point must be
1170 virtual void emitCancellationPointCall(CodeGenFunction &CGF,
1172 OpenMPDirectiveKind CancelRegion);
1174 /// \brief Emit code for 'cancel' construct.
1175 /// \param IfCond Condition in the associated 'if' clause, if it was
1176 /// specified, nullptr otherwise.
1177 /// \param CancelRegion Region kind for which the cancel must be emitted.
1179 virtual void emitCancelCall(CodeGenFunction &CGF, SourceLocation Loc,
1181 OpenMPDirectiveKind CancelRegion);
1183 /// \brief Emit outilined function for 'target' directive.
1184 /// \param D Directive to emit.
1185 /// \param ParentName Name of the function that encloses the target region.
1186 /// \param OutlinedFn Outlined function value to be defined by this call.
1187 /// \param OutlinedFnID Outlined function ID value to be defined by this call.
1188 /// \param IsOffloadEntry True if the outlined function is an offload entry.
1189 /// \param CodeGen Code generation sequence for the \a D directive.
1190 /// An outlined function may not be an entry if, e.g. the if clause always
1191 /// evaluates to false.
1192 virtual void emitTargetOutlinedFunction(const OMPExecutableDirective &D,
1193 StringRef ParentName,
1194 llvm::Function *&OutlinedFn,
1195 llvm::Constant *&OutlinedFnID,
1196 bool IsOffloadEntry,
1197 const RegionCodeGenTy &CodeGen);
1199 /// \brief Emit the target offloading code associated with \a D. The emitted
1200 /// code attempts offloading the execution to the device, an the event of
1201 /// a failure it executes the host version outlined in \a OutlinedFn.
1202 /// \param D Directive to emit.
1203 /// \param OutlinedFn Host version of the code to be offloaded.
1204 /// \param OutlinedFnID ID of host version of the code to be offloaded.
1205 /// \param IfCond Expression evaluated in if clause associated with the target
1206 /// directive, or null if no if clause is used.
1207 /// \param Device Expression evaluated in device clause associated with the
1208 /// target directive, or null if no device clause is used.
1209 /// \param CapturedVars Values captured in the current region.
1210 virtual void emitTargetCall(CodeGenFunction &CGF,
1211 const OMPExecutableDirective &D,
1212 llvm::Value *OutlinedFn,
1213 llvm::Value *OutlinedFnID, const Expr *IfCond,
1215 ArrayRef<llvm::Value *> CapturedVars);
1217 /// \brief Emit the target regions enclosed in \a GD function definition or
1218 /// the function itself in case it is a valid device function. Returns true if
1219 /// \a GD was dealt with successfully.
1220 /// \param GD Function to scan.
1221 virtual bool emitTargetFunctions(GlobalDecl GD);
1223 /// \brief Emit the global variable if it is a valid device global variable.
1224 /// Returns true if \a GD was dealt with successfully.
1225 /// \param GD Variable declaration to emit.
1226 virtual bool emitTargetGlobalVariable(GlobalDecl GD);
1228 /// \brief Emit the global \a GD if it is meaningful for the target. Returns
1229 /// if it was emitted successfully.
1230 /// \param GD Global to scan.
1231 virtual bool emitTargetGlobal(GlobalDecl GD);
1233 /// \brief Creates the offloading descriptor in the event any target region
1234 /// was emitted in the current module and return the function that registers
1236 virtual llvm::Function *emitRegistrationFunction();
1238 /// \brief Emits code for teams call of the \a OutlinedFn with
1239 /// variables captured in a record which address is stored in \a
1241 /// \param OutlinedFn Outlined function to be run by team masters. Type of
1242 /// this function is void(*)(kmp_int32 *, kmp_int32, struct context_vars*).
1243 /// \param CapturedVars A pointer to the record with the references to
1244 /// variables used in \a OutlinedFn function.
1246 virtual void emitTeamsCall(CodeGenFunction &CGF,
1247 const OMPExecutableDirective &D,
1248 SourceLocation Loc, llvm::Value *OutlinedFn,
1249 ArrayRef<llvm::Value *> CapturedVars);
1251 /// \brief Emits call to void __kmpc_push_num_teams(ident_t *loc, kmp_int32
1252 /// global_tid, kmp_int32 num_teams, kmp_int32 thread_limit) to generate code
1253 /// for num_teams clause.
1254 /// \param NumTeams An integer expression of teams.
1255 /// \param ThreadLimit An integer expression of threads.
1256 virtual void emitNumTeamsClause(CodeGenFunction &CGF, const Expr *NumTeams,
1257 const Expr *ThreadLimit, SourceLocation Loc);
1259 /// Struct that keeps all the relevant information that should be kept
1260 /// throughout a 'target data' region.
1261 class TargetDataInfo {
1262 /// Set to true if device pointer information have to be obtained.
1263 bool RequiresDevicePointerInfo = false;
1266 /// The array of base pointer passed to the runtime library.
1267 llvm::Value *BasePointersArray = nullptr;
1268 /// The array of section pointers passed to the runtime library.
1269 llvm::Value *PointersArray = nullptr;
1270 /// The array of sizes passed to the runtime library.
1271 llvm::Value *SizesArray = nullptr;
1272 /// The array of map types passed to the runtime library.
1273 llvm::Value *MapTypesArray = nullptr;
1274 /// The total number of pointers passed to the runtime library.
1275 unsigned NumberOfPtrs = 0u;
1276 /// Map between the a declaration of a capture and the corresponding base
1277 /// pointer address where the runtime returns the device pointers.
1278 llvm::DenseMap<const ValueDecl *, Address> CaptureDeviceAddrMap;
1280 explicit TargetDataInfo() {}
1281 explicit TargetDataInfo(bool RequiresDevicePointerInfo)
1282 : RequiresDevicePointerInfo(RequiresDevicePointerInfo) {}
1283 /// Clear information about the data arrays.
1284 void clearArrayInfo() {
1285 BasePointersArray = nullptr;
1286 PointersArray = nullptr;
1287 SizesArray = nullptr;
1288 MapTypesArray = nullptr;
1291 /// Return true if the current target data information has valid arrays.
1293 return BasePointersArray && PointersArray && SizesArray &&
1294 MapTypesArray && NumberOfPtrs;
1296 bool requiresDevicePointerInfo() { return RequiresDevicePointerInfo; }
1299 /// \brief Emit the target data mapping code associated with \a D.
1300 /// \param D Directive to emit.
1301 /// \param IfCond Expression evaluated in if clause associated with the
1302 /// target directive, or null if no device clause is used.
1303 /// \param Device Expression evaluated in device clause associated with the
1304 /// target directive, or null if no device clause is used.
1305 /// \param Info A record used to store information that needs to be preserved
1306 /// until the region is closed.
1307 virtual void emitTargetDataCalls(CodeGenFunction &CGF,
1308 const OMPExecutableDirective &D,
1309 const Expr *IfCond, const Expr *Device,
1310 const RegionCodeGenTy &CodeGen,
1311 TargetDataInfo &Info);
1313 /// \brief Emit the data mapping/movement code associated with the directive
1314 /// \a D that should be of the form 'target [{enter|exit} data | update]'.
1315 /// \param D Directive to emit.
1316 /// \param IfCond Expression evaluated in if clause associated with the target
1317 /// directive, or null if no if clause is used.
1318 /// \param Device Expression evaluated in device clause associated with the
1319 /// target directive, or null if no device clause is used.
1320 virtual void emitTargetDataStandAloneCall(CodeGenFunction &CGF,
1321 const OMPExecutableDirective &D,
1323 const Expr *Device);
1325 /// Marks function \a Fn with properly mangled versions of vector functions.
1326 /// \param FD Function marked as 'declare simd'.
1327 /// \param Fn LLVM function that must be marked with 'declare simd'
1329 virtual void emitDeclareSimdFunction(const FunctionDecl *FD,
1330 llvm::Function *Fn);
1332 /// Emit initialization for doacross loop nesting support.
1333 /// \param D Loop-based construct used in doacross nesting construct.
1334 virtual void emitDoacrossInit(CodeGenFunction &CGF,
1335 const OMPLoopDirective &D);
1337 /// Emit code for doacross ordered directive with 'depend' clause.
1338 /// \param C 'depend' clause with 'sink|source' dependency kind.
1339 virtual void emitDoacrossOrdered(CodeGenFunction &CGF,
1340 const OMPDependClause *C);
1342 /// Translates the native parameter of outlined function if this is required
1344 /// \param FD Field decl from captured record for the paramater.
1345 /// \param NativeParam Parameter itself.
1346 virtual const VarDecl *translateParameter(const FieldDecl *FD,
1347 const VarDecl *NativeParam) const {
1351 /// Gets the address of the native argument basing on the address of the
1352 /// target-specific parameter.
1353 /// \param NativeParam Parameter itself.
1354 /// \param TargetParam Corresponding target-specific parameter.
1355 virtual Address getParameterAddress(CodeGenFunction &CGF,
1356 const VarDecl *NativeParam,
1357 const VarDecl *TargetParam) const;
1359 /// Emits call of the outlined function with the provided arguments,
1360 /// translating these arguments to correct target-specific arguments.
1362 emitOutlinedFunctionCall(CodeGenFunction &CGF, SourceLocation Loc,
1363 llvm::Value *OutlinedFn,
1364 ArrayRef<llvm::Value *> Args = llvm::None) const;
1367 /// Class supports emissionof SIMD-only code.
1368 class CGOpenMPSIMDRuntime final : public CGOpenMPRuntime {
1370 explicit CGOpenMPSIMDRuntime(CodeGenModule &CGM) : CGOpenMPRuntime(CGM) {}
1371 ~CGOpenMPSIMDRuntime() override {}
1373 /// \brief Emits outlined function for the specified OpenMP parallel directive
1374 /// \a D. This outlined function has type void(*)(kmp_int32 *ThreadID,
1375 /// kmp_int32 BoundID, struct context_vars*).
1376 /// \param D OpenMP directive.
1377 /// \param ThreadIDVar Variable for thread id in the current OpenMP region.
1378 /// \param InnermostKind Kind of innermost directive (for simple directives it
1379 /// is a directive itself, for combined - its innermost directive).
1380 /// \param CodeGen Code generation sequence for the \a D directive.
1382 emitParallelOutlinedFunction(const OMPExecutableDirective &D,
1383 const VarDecl *ThreadIDVar,
1384 OpenMPDirectiveKind InnermostKind,
1385 const RegionCodeGenTy &CodeGen) override;
1387 /// \brief Emits outlined function for the specified OpenMP teams directive
1388 /// \a D. This outlined function has type void(*)(kmp_int32 *ThreadID,
1389 /// kmp_int32 BoundID, struct context_vars*).
1390 /// \param D OpenMP directive.
1391 /// \param ThreadIDVar Variable for thread id in the current OpenMP region.
1392 /// \param InnermostKind Kind of innermost directive (for simple directives it
1393 /// is a directive itself, for combined - its innermost directive).
1394 /// \param CodeGen Code generation sequence for the \a D directive.
1396 emitTeamsOutlinedFunction(const OMPExecutableDirective &D,
1397 const VarDecl *ThreadIDVar,
1398 OpenMPDirectiveKind InnermostKind,
1399 const RegionCodeGenTy &CodeGen) override;
1401 /// \brief Emits outlined function for the OpenMP task directive \a D. This
1402 /// outlined function has type void(*)(kmp_int32 ThreadID, struct task_t*
1404 /// \param D OpenMP directive.
1405 /// \param ThreadIDVar Variable for thread id in the current OpenMP region.
1406 /// \param PartIDVar Variable for partition id in the current OpenMP untied
1408 /// \param TaskTVar Variable for task_t argument.
1409 /// \param InnermostKind Kind of innermost directive (for simple directives it
1410 /// is a directive itself, for combined - its innermost directive).
1411 /// \param CodeGen Code generation sequence for the \a D directive.
1412 /// \param Tied true if task is generated for tied task, false otherwise.
1413 /// \param NumberOfParts Number of parts in untied task. Ignored for tied
1416 llvm::Value *emitTaskOutlinedFunction(
1417 const OMPExecutableDirective &D, const VarDecl *ThreadIDVar,
1418 const VarDecl *PartIDVar, const VarDecl *TaskTVar,
1419 OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen,
1420 bool Tied, unsigned &NumberOfParts) override;
1422 /// \brief Emits code for parallel or serial call of the \a OutlinedFn with
1423 /// variables captured in a record which address is stored in \a
1425 /// \param OutlinedFn Outlined function to be run in parallel threads. Type of
1426 /// this function is void(*)(kmp_int32 *, kmp_int32, struct context_vars*).
1427 /// \param CapturedVars A pointer to the record with the references to
1428 /// variables used in \a OutlinedFn function.
1429 /// \param IfCond Condition in the associated 'if' clause, if it was
1430 /// specified, nullptr otherwise.
1432 void emitParallelCall(CodeGenFunction &CGF, SourceLocation Loc,
1433 llvm::Value *OutlinedFn,
1434 ArrayRef<llvm::Value *> CapturedVars,
1435 const Expr *IfCond) override;
1437 /// \brief Emits a critical region.
1438 /// \param CriticalName Name of the critical region.
1439 /// \param CriticalOpGen Generator for the statement associated with the given
1440 /// critical region.
1441 /// \param Hint Value of the 'hint' clause (optional).
1442 void emitCriticalRegion(CodeGenFunction &CGF, StringRef CriticalName,
1443 const RegionCodeGenTy &CriticalOpGen,
1445 const Expr *Hint = nullptr) override;
1447 /// \brief Emits a master region.
1448 /// \param MasterOpGen Generator for the statement associated with the given
1450 void emitMasterRegion(CodeGenFunction &CGF,
1451 const RegionCodeGenTy &MasterOpGen,
1452 SourceLocation Loc) override;
1454 /// \brief Emits code for a taskyield directive.
1455 void emitTaskyieldCall(CodeGenFunction &CGF, SourceLocation Loc) override;
1457 /// \brief Emit a taskgroup region.
1458 /// \param TaskgroupOpGen Generator for the statement associated with the
1459 /// given taskgroup region.
1460 void emitTaskgroupRegion(CodeGenFunction &CGF,
1461 const RegionCodeGenTy &TaskgroupOpGen,
1462 SourceLocation Loc) override;
1464 /// \brief Emits a single region.
1465 /// \param SingleOpGen Generator for the statement associated with the given
1467 void emitSingleRegion(CodeGenFunction &CGF,
1468 const RegionCodeGenTy &SingleOpGen, SourceLocation Loc,
1469 ArrayRef<const Expr *> CopyprivateVars,
1470 ArrayRef<const Expr *> DestExprs,
1471 ArrayRef<const Expr *> SrcExprs,
1472 ArrayRef<const Expr *> AssignmentOps) override;
1474 /// \brief Emit an ordered region.
1475 /// \param OrderedOpGen Generator for the statement associated with the given
1477 void emitOrderedRegion(CodeGenFunction &CGF,
1478 const RegionCodeGenTy &OrderedOpGen,
1479 SourceLocation Loc, bool IsThreads) override;
1481 /// \brief Emit an implicit/explicit barrier for OpenMP threads.
1482 /// \param Kind Directive for which this implicit barrier call must be
1483 /// generated. Must be OMPD_barrier for explicit barrier generation.
1484 /// \param EmitChecks true if need to emit checks for cancellation barriers.
1485 /// \param ForceSimpleCall true simple barrier call must be emitted, false if
1486 /// runtime class decides which one to emit (simple or with cancellation
1489 void emitBarrierCall(CodeGenFunction &CGF, SourceLocation Loc,
1490 OpenMPDirectiveKind Kind, bool EmitChecks = true,
1491 bool ForceSimpleCall = false) override;
1493 /// This is used for non static scheduled types and when the ordered
1494 /// clause is present on the loop construct.
1495 /// Depending on the loop schedule, it is necessary to call some runtime
1496 /// routine before start of the OpenMP loop to get the loop upper / lower
1497 /// bounds \a LB and \a UB and stride \a ST.
1499 /// \param CGF Reference to current CodeGenFunction.
1500 /// \param Loc Clang source location.
1501 /// \param ScheduleKind Schedule kind, specified by the 'schedule' clause.
1502 /// \param IVSize Size of the iteration variable in bits.
1503 /// \param IVSigned Sign of the iteration variable.
1504 /// \param Ordered true if loop is ordered, false otherwise.
1505 /// \param DispatchValues struct containing llvm values for lower bound, upper
1506 /// bound, and chunk expression.
1507 /// For the default (nullptr) value, the chunk 1 will be used.
1509 void emitForDispatchInit(CodeGenFunction &CGF, SourceLocation Loc,
1510 const OpenMPScheduleTy &ScheduleKind,
1511 unsigned IVSize, bool IVSigned, bool Ordered,
1512 const DispatchRTInput &DispatchValues) override;
1514 /// \brief Call the appropriate runtime routine to initialize it before start
1517 /// This is used only in case of static schedule, when the user did not
1518 /// specify a ordered clause on the loop construct.
1519 /// Depending on the loop schedule, it is necessary to call some runtime
1520 /// routine before start of the OpenMP loop to get the loop upper / lower
1521 /// bounds LB and UB and stride ST.
1523 /// \param CGF Reference to current CodeGenFunction.
1524 /// \param Loc Clang source location.
1525 /// \param DKind Kind of the directive.
1526 /// \param ScheduleKind Schedule kind, specified by the 'schedule' clause.
1527 /// \param Values Input arguments for the construct.
1529 void emitForStaticInit(CodeGenFunction &CGF, SourceLocation Loc,
1530 OpenMPDirectiveKind DKind,
1531 const OpenMPScheduleTy &ScheduleKind,
1532 const StaticRTInput &Values) override;
1535 /// \param CGF Reference to current CodeGenFunction.
1536 /// \param Loc Clang source location.
1537 /// \param SchedKind Schedule kind, specified by the 'dist_schedule' clause.
1538 /// \param Values Input arguments for the construct.
1540 void emitDistributeStaticInit(CodeGenFunction &CGF, SourceLocation Loc,
1541 OpenMPDistScheduleClauseKind SchedKind,
1542 const StaticRTInput &Values) override;
1544 /// \brief Call the appropriate runtime routine to notify that we finished
1545 /// iteration of the ordered loop with the dynamic scheduling.
1547 /// \param CGF Reference to current CodeGenFunction.
1548 /// \param Loc Clang source location.
1549 /// \param IVSize Size of the iteration variable in bits.
1550 /// \param IVSigned Sign of the iteration variable.
1552 void emitForOrderedIterationEnd(CodeGenFunction &CGF, SourceLocation Loc,
1553 unsigned IVSize, bool IVSigned) override;
1555 /// \brief Call the appropriate runtime routine to notify that we finished
1556 /// all the work with current loop.
1558 /// \param CGF Reference to current CodeGenFunction.
1559 /// \param Loc Clang source location.
1560 /// \param DKind Kind of the directive for which the static finish is emitted.
1562 void emitForStaticFinish(CodeGenFunction &CGF, SourceLocation Loc,
1563 OpenMPDirectiveKind DKind) override;
1565 /// Call __kmpc_dispatch_next(
1566 /// ident_t *loc, kmp_int32 tid, kmp_int32 *p_lastiter,
1567 /// kmp_int[32|64] *p_lower, kmp_int[32|64] *p_upper,
1568 /// kmp_int[32|64] *p_stride);
1569 /// \param IVSize Size of the iteration variable in bits.
1570 /// \param IVSigned Sign of the iteration variable.
1571 /// \param IL Address of the output variable in which the flag of the
1572 /// last iteration is returned.
1573 /// \param LB Address of the output variable in which the lower iteration
1574 /// number is returned.
1575 /// \param UB Address of the output variable in which the upper iteration
1576 /// number is returned.
1577 /// \param ST Address of the output variable in which the stride value is
1579 llvm::Value *emitForNext(CodeGenFunction &CGF, SourceLocation Loc,
1580 unsigned IVSize, bool IVSigned, Address IL,
1581 Address LB, Address UB, Address ST) override;
1583 /// \brief Emits call to void __kmpc_push_num_threads(ident_t *loc, kmp_int32
1584 /// global_tid, kmp_int32 num_threads) to generate code for 'num_threads'
1586 /// \param NumThreads An integer value of threads.
1587 void emitNumThreadsClause(CodeGenFunction &CGF, llvm::Value *NumThreads,
1588 SourceLocation Loc) override;
1590 /// \brief Emit call to void __kmpc_push_proc_bind(ident_t *loc, kmp_int32
1591 /// global_tid, int proc_bind) to generate code for 'proc_bind' clause.
1592 void emitProcBindClause(CodeGenFunction &CGF,
1593 OpenMPProcBindClauseKind ProcBind,
1594 SourceLocation Loc) override;
1596 /// \brief Returns address of the threadprivate variable for the current
1598 /// \param VD Threadprivate variable.
1599 /// \param VDAddr Address of the global variable \a VD.
1600 /// \param Loc Location of the reference to threadprivate var.
1601 /// \return Address of the threadprivate variable for the current thread.
1602 Address getAddrOfThreadPrivate(CodeGenFunction &CGF, const VarDecl *VD,
1603 Address VDAddr, SourceLocation Loc) override;
1605 /// \brief Emit a code for initialization of threadprivate variable. It emits
1606 /// a call to runtime library which adds initial value to the newly created
1607 /// threadprivate variable (if it is not constant) and registers destructor
1608 /// for the variable (if any).
1609 /// \param VD Threadprivate variable.
1610 /// \param VDAddr Address of the global variable \a VD.
1611 /// \param Loc Location of threadprivate declaration.
1612 /// \param PerformInit true if initialization expression is not constant.
1614 emitThreadPrivateVarDefinition(const VarDecl *VD, Address VDAddr,
1615 SourceLocation Loc, bool PerformInit,
1616 CodeGenFunction *CGF = nullptr) override;
1618 /// Creates artificial threadprivate variable with name \p Name and type \p
1620 /// \param VarType Type of the artificial threadprivate variable.
1621 /// \param Name Name of the artificial threadprivate variable.
1622 Address getAddrOfArtificialThreadPrivate(CodeGenFunction &CGF,
1624 StringRef Name) override;
1626 /// \brief Emit flush of the variables specified in 'omp flush' directive.
1627 /// \param Vars List of variables to flush.
1628 void emitFlush(CodeGenFunction &CGF, ArrayRef<const Expr *> Vars,
1629 SourceLocation Loc) override;
1631 /// \brief Emit task region for the task directive. The task region is
1632 /// emitted in several steps:
1633 /// 1. Emit a call to kmp_task_t *__kmpc_omp_task_alloc(ident_t *, kmp_int32
1634 /// gtid, kmp_int32 flags, size_t sizeof_kmp_task_t, size_t sizeof_shareds,
1635 /// kmp_routine_entry_t *task_entry). Here task_entry is a pointer to the
1637 /// kmp_int32 .omp_task_entry.(kmp_int32 gtid, kmp_task_t *tt) {
1638 /// TaskFunction(gtid, tt->part_id, tt->shareds);
1641 /// 2. Copy a list of shared variables to field shareds of the resulting
1642 /// structure kmp_task_t returned by the previous call (if any).
1643 /// 3. Copy a pointer to destructions function to field destructions of the
1644 /// resulting structure kmp_task_t.
1645 /// 4. Emit a call to kmp_int32 __kmpc_omp_task(ident_t *, kmp_int32 gtid,
1646 /// kmp_task_t *new_task), where new_task is a resulting structure from
1648 /// \param D Current task directive.
1649 /// \param TaskFunction An LLVM function with type void (*)(i32 /*gtid*/, i32
1650 /// /*part_id*/, captured_struct */*__context*/);
1651 /// \param SharedsTy A type which contains references the shared variables.
1652 /// \param Shareds Context with the list of shared variables from the \p
1654 /// \param IfCond Not a nullptr if 'if' clause was specified, nullptr
1656 /// \param Data Additional data for task generation like tiednsee, final
1657 /// state, list of privates etc.
1658 void emitTaskCall(CodeGenFunction &CGF, SourceLocation Loc,
1659 const OMPExecutableDirective &D, llvm::Value *TaskFunction,
1660 QualType SharedsTy, Address Shareds, const Expr *IfCond,
1661 const OMPTaskDataTy &Data) override;
1663 /// Emit task region for the taskloop directive. The taskloop region is
1664 /// emitted in several steps:
1665 /// 1. Emit a call to kmp_task_t *__kmpc_omp_task_alloc(ident_t *, kmp_int32
1666 /// gtid, kmp_int32 flags, size_t sizeof_kmp_task_t, size_t sizeof_shareds,
1667 /// kmp_routine_entry_t *task_entry). Here task_entry is a pointer to the
1669 /// kmp_int32 .omp_task_entry.(kmp_int32 gtid, kmp_task_t *tt) {
1670 /// TaskFunction(gtid, tt->part_id, tt->shareds);
1673 /// 2. Copy a list of shared variables to field shareds of the resulting
1674 /// structure kmp_task_t returned by the previous call (if any).
1675 /// 3. Copy a pointer to destructions function to field destructions of the
1676 /// resulting structure kmp_task_t.
1677 /// 4. Emit a call to void __kmpc_taskloop(ident_t *loc, int gtid, kmp_task_t
1678 /// *task, int if_val, kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st, int
1679 /// nogroup, int sched, kmp_uint64 grainsize, void *task_dup ), where new_task
1680 /// is a resulting structure from
1682 /// \param D Current task directive.
1683 /// \param TaskFunction An LLVM function with type void (*)(i32 /*gtid*/, i32
1684 /// /*part_id*/, captured_struct */*__context*/);
1685 /// \param SharedsTy A type which contains references the shared variables.
1686 /// \param Shareds Context with the list of shared variables from the \p
1688 /// \param IfCond Not a nullptr if 'if' clause was specified, nullptr
1690 /// \param Data Additional data for task generation like tiednsee, final
1691 /// state, list of privates etc.
1692 void emitTaskLoopCall(CodeGenFunction &CGF, SourceLocation Loc,
1693 const OMPLoopDirective &D, llvm::Value *TaskFunction,
1694 QualType SharedsTy, Address Shareds, const Expr *IfCond,
1695 const OMPTaskDataTy &Data) override;
1697 /// \brief Emit a code for reduction clause. Next code should be emitted for
1701 /// static kmp_critical_name lock = { 0 };
1703 /// void reduce_func(void *lhs[<n>], void *rhs[<n>]) {
1705 /// *(Type<i>*)lhs[i] = RedOp<i>(*(Type<i>*)lhs[i], *(Type<i>*)rhs[i]);
1710 /// void *RedList[<n>] = {&<RHSExprs>[0], ..., &<RHSExprs>[<n>-1]};
1711 /// switch (__kmpc_reduce{_nowait}(<loc>, <gtid>, <n>, sizeof(RedList),
1712 /// RedList, reduce_func, &<lock>)) {
1715 /// <LHSExprs>[i] = RedOp<i>(*<LHSExprs>[i], *<RHSExprs>[i]);
1717 /// __kmpc_end_reduce{_nowait}(<loc>, <gtid>, &<lock>);
1721 /// Atomic(<LHSExprs>[i] = RedOp<i>(*<LHSExprs>[i], *<RHSExprs>[i]));
1728 /// \param Privates List of private copies for original reduction arguments.
1729 /// \param LHSExprs List of LHS in \a ReductionOps reduction operations.
1730 /// \param RHSExprs List of RHS in \a ReductionOps reduction operations.
1731 /// \param ReductionOps List of reduction operations in form 'LHS binop RHS'
1732 /// or 'operator binop(LHS, RHS)'.
1733 /// \param Options List of options for reduction codegen:
1734 /// WithNowait true if parent directive has also nowait clause, false
1736 /// SimpleReduction Emit reduction operation only. Used for omp simd
1737 /// directive on the host.
1738 /// ReductionKind The kind of reduction to perform.
1739 void emitReduction(CodeGenFunction &CGF, SourceLocation Loc,
1740 ArrayRef<const Expr *> Privates,
1741 ArrayRef<const Expr *> LHSExprs,
1742 ArrayRef<const Expr *> RHSExprs,
1743 ArrayRef<const Expr *> ReductionOps,
1744 ReductionOptionsTy Options) override;
1746 /// Emit a code for initialization of task reduction clause. Next code
1747 /// should be emitted for reduction:
1750 /// _task_red_item_t red_data[n];
1752 /// red_data[i].shar = &origs[i];
1753 /// red_data[i].size = sizeof(origs[i]);
1754 /// red_data[i].f_init = (void*)RedInit<i>;
1755 /// red_data[i].f_fini = (void*)RedDest<i>;
1756 /// red_data[i].f_comb = (void*)RedOp<i>;
1757 /// red_data[i].flags = <Flag_i>;
1759 /// void* tg1 = __kmpc_task_reduction_init(gtid, n, red_data);
1762 /// \param LHSExprs List of LHS in \a Data.ReductionOps reduction operations.
1763 /// \param RHSExprs List of RHS in \a Data.ReductionOps reduction operations.
1764 /// \param Data Additional data for task generation like tiedness, final
1765 /// state, list of privates, reductions etc.
1766 llvm::Value *emitTaskReductionInit(CodeGenFunction &CGF, SourceLocation Loc,
1767 ArrayRef<const Expr *> LHSExprs,
1768 ArrayRef<const Expr *> RHSExprs,
1769 const OMPTaskDataTy &Data) override;
1771 /// Required to resolve existing problems in the runtime. Emits threadprivate
1772 /// variables to store the size of the VLAs/array sections for
1773 /// initializer/combiner/finalizer functions + emits threadprivate variable to
1774 /// store the pointer to the original reduction item for the custom
1775 /// initializer defined by declare reduction construct.
1776 /// \param RCG Allows to reuse an existing data for the reductions.
1777 /// \param N Reduction item for which fixups must be emitted.
1778 void emitTaskReductionFixups(CodeGenFunction &CGF, SourceLocation Loc,
1779 ReductionCodeGen &RCG, unsigned N) override;
1781 /// Get the address of `void *` type of the privatue copy of the reduction
1782 /// item specified by the \p SharedLVal.
1783 /// \param ReductionsPtr Pointer to the reduction data returned by the
1784 /// emitTaskReductionInit function.
1785 /// \param SharedLVal Address of the original reduction item.
1786 Address getTaskReductionItem(CodeGenFunction &CGF, SourceLocation Loc,
1787 llvm::Value *ReductionsPtr,
1788 LValue SharedLVal) override;
1790 /// \brief Emit code for 'taskwait' directive.
1791 void emitTaskwaitCall(CodeGenFunction &CGF, SourceLocation Loc) override;
1793 /// \brief Emit code for 'cancellation point' construct.
1794 /// \param CancelRegion Region kind for which the cancellation point must be
1797 void emitCancellationPointCall(CodeGenFunction &CGF, SourceLocation Loc,
1798 OpenMPDirectiveKind CancelRegion) override;
1800 /// \brief Emit code for 'cancel' construct.
1801 /// \param IfCond Condition in the associated 'if' clause, if it was
1802 /// specified, nullptr otherwise.
1803 /// \param CancelRegion Region kind for which the cancel must be emitted.
1805 void emitCancelCall(CodeGenFunction &CGF, SourceLocation Loc,
1807 OpenMPDirectiveKind CancelRegion) override;
1809 /// \brief Emit outilined function for 'target' directive.
1810 /// \param D Directive to emit.
1811 /// \param ParentName Name of the function that encloses the target region.
1812 /// \param OutlinedFn Outlined function value to be defined by this call.
1813 /// \param OutlinedFnID Outlined function ID value to be defined by this call.
1814 /// \param IsOffloadEntry True if the outlined function is an offload entry.
1815 /// \param CodeGen Code generation sequence for the \a D directive.
1816 /// An outlined function may not be an entry if, e.g. the if clause always
1817 /// evaluates to false.
1818 void emitTargetOutlinedFunction(const OMPExecutableDirective &D,
1819 StringRef ParentName,
1820 llvm::Function *&OutlinedFn,
1821 llvm::Constant *&OutlinedFnID,
1822 bool IsOffloadEntry,
1823 const RegionCodeGenTy &CodeGen) override;
1825 /// \brief Emit the target offloading code associated with \a D. The emitted
1826 /// code attempts offloading the execution to the device, an the event of
1827 /// a failure it executes the host version outlined in \a OutlinedFn.
1828 /// \param D Directive to emit.
1829 /// \param OutlinedFn Host version of the code to be offloaded.
1830 /// \param OutlinedFnID ID of host version of the code to be offloaded.
1831 /// \param IfCond Expression evaluated in if clause associated with the target
1832 /// directive, or null if no if clause is used.
1833 /// \param Device Expression evaluated in device clause associated with the
1834 /// target directive, or null if no device clause is used.
1835 /// \param CapturedVars Values captured in the current region.
1836 void emitTargetCall(CodeGenFunction &CGF, const OMPExecutableDirective &D,
1837 llvm::Value *OutlinedFn, llvm::Value *OutlinedFnID,
1838 const Expr *IfCond, const Expr *Device,
1839 ArrayRef<llvm::Value *> CapturedVars) override;
1841 /// \brief Emit the target regions enclosed in \a GD function definition or
1842 /// the function itself in case it is a valid device function. Returns true if
1843 /// \a GD was dealt with successfully.
1844 /// \param GD Function to scan.
1845 bool emitTargetFunctions(GlobalDecl GD) override;
1847 /// \brief Emit the global variable if it is a valid device global variable.
1848 /// Returns true if \a GD was dealt with successfully.
1849 /// \param GD Variable declaration to emit.
1850 bool emitTargetGlobalVariable(GlobalDecl GD) override;
1852 /// \brief Emit the global \a GD if it is meaningful for the target. Returns
1853 /// if it was emitted successfully.
1854 /// \param GD Global to scan.
1855 bool emitTargetGlobal(GlobalDecl GD) override;
1857 /// \brief Creates the offloading descriptor in the event any target region
1858 /// was emitted in the current module and return the function that registers
1860 llvm::Function *emitRegistrationFunction() override;
1862 /// \brief Emits code for teams call of the \a OutlinedFn with
1863 /// variables captured in a record which address is stored in \a
1865 /// \param OutlinedFn Outlined function to be run by team masters. Type of
1866 /// this function is void(*)(kmp_int32 *, kmp_int32, struct context_vars*).
1867 /// \param CapturedVars A pointer to the record with the references to
1868 /// variables used in \a OutlinedFn function.
1870 void emitTeamsCall(CodeGenFunction &CGF, const OMPExecutableDirective &D,
1871 SourceLocation Loc, llvm::Value *OutlinedFn,
1872 ArrayRef<llvm::Value *> CapturedVars) override;
1874 /// \brief Emits call to void __kmpc_push_num_teams(ident_t *loc, kmp_int32
1875 /// global_tid, kmp_int32 num_teams, kmp_int32 thread_limit) to generate code
1876 /// for num_teams clause.
1877 /// \param NumTeams An integer expression of teams.
1878 /// \param ThreadLimit An integer expression of threads.
1879 void emitNumTeamsClause(CodeGenFunction &CGF, const Expr *NumTeams,
1880 const Expr *ThreadLimit, SourceLocation Loc) override;
1882 /// \brief Emit the target data mapping code associated with \a D.
1883 /// \param D Directive to emit.
1884 /// \param IfCond Expression evaluated in if clause associated with the
1885 /// target directive, or null if no device clause is used.
1886 /// \param Device Expression evaluated in device clause associated with the
1887 /// target directive, or null if no device clause is used.
1888 /// \param Info A record used to store information that needs to be preserved
1889 /// until the region is closed.
1890 void emitTargetDataCalls(CodeGenFunction &CGF,
1891 const OMPExecutableDirective &D, const Expr *IfCond,
1892 const Expr *Device, const RegionCodeGenTy &CodeGen,
1893 TargetDataInfo &Info) override;
1895 /// \brief Emit the data mapping/movement code associated with the directive
1896 /// \a D that should be of the form 'target [{enter|exit} data | update]'.
1897 /// \param D Directive to emit.
1898 /// \param IfCond Expression evaluated in if clause associated with the target
1899 /// directive, or null if no if clause is used.
1900 /// \param Device Expression evaluated in device clause associated with the
1901 /// target directive, or null if no device clause is used.
1902 void emitTargetDataStandAloneCall(CodeGenFunction &CGF,
1903 const OMPExecutableDirective &D,
1905 const Expr *Device) override;
1907 /// Emit initialization for doacross loop nesting support.
1908 /// \param D Loop-based construct used in doacross nesting construct.
1909 void emitDoacrossInit(CodeGenFunction &CGF,
1910 const OMPLoopDirective &D) override;
1912 /// Emit code for doacross ordered directive with 'depend' clause.
1913 /// \param C 'depend' clause with 'sink|source' dependency kind.
1914 void emitDoacrossOrdered(CodeGenFunction &CGF,
1915 const OMPDependClause *C) override;
1917 /// Translates the native parameter of outlined function if this is required
1919 /// \param FD Field decl from captured record for the paramater.
1920 /// \param NativeParam Parameter itself.
1921 const VarDecl *translateParameter(const FieldDecl *FD,
1922 const VarDecl *NativeParam) const override;
1924 /// Gets the address of the native argument basing on the address of the
1925 /// target-specific parameter.
1926 /// \param NativeParam Parameter itself.
1927 /// \param TargetParam Corresponding target-specific parameter.
1928 Address getParameterAddress(CodeGenFunction &CGF, const VarDecl *NativeParam,
1929 const VarDecl *TargetParam) const override;
1932 } // namespace CodeGen
1933 } // namespace clang