LLVM 20.0.0git
OMPIRBuilder.h
Go to the documentation of this file.
1//===- IR/OpenMPIRBuilder.h - OpenMP encoding builder for LLVM IR - C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file defines the OpenMPIRBuilder class and helpers used as a convenient
10// way to create LLVM instructions for OpenMP directives.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_FRONTEND_OPENMP_OMPIRBUILDER_H
15#define LLVM_FRONTEND_OPENMP_OMPIRBUILDER_H
16
21#include "llvm/IR/DebugLoc.h"
22#include "llvm/IR/IRBuilder.h"
23#include "llvm/IR/Module.h"
26#include <forward_list>
27#include <map>
28#include <optional>
29
30namespace llvm {
31class CanonicalLoopInfo;
32struct TargetRegionEntryInfo;
33class OffloadEntriesInfoManager;
34class OpenMPIRBuilder;
35
36/// Move the instruction after an InsertPoint to the beginning of another
37/// BasicBlock.
38///
39/// The instructions after \p IP are moved to the beginning of \p New which must
40/// not have any PHINodes. If \p CreateBranch is true, a branch instruction to
41/// \p New will be added such that there is no semantic change. Otherwise, the
42/// \p IP insert block remains degenerate and it is up to the caller to insert a
43/// terminator.
44void spliceBB(IRBuilderBase::InsertPoint IP, BasicBlock *New,
45 bool CreateBranch);
46
47/// Splice a BasicBlock at an IRBuilder's current insertion point. Its new
48/// insert location will stick to after the instruction before the insertion
49/// point (instead of moving with the instruction the InsertPoint stores
50/// internally).
51void spliceBB(IRBuilder<> &Builder, BasicBlock *New, bool CreateBranch);
52
53/// Split a BasicBlock at an InsertPoint, even if the block is degenerate
54/// (missing the terminator).
55///
56/// llvm::SplitBasicBlock and BasicBlock::splitBasicBlock require a well-formed
57/// BasicBlock. \p Name is used for the new successor block. If \p CreateBranch
58/// is true, a branch to the new successor will new created such that
59/// semantically there is no change; otherwise the block of the insertion point
60/// remains degenerate and it is the caller's responsibility to insert a
61/// terminator. Returns the new successor block.
62BasicBlock *splitBB(IRBuilderBase::InsertPoint IP, bool CreateBranch,
63 llvm::Twine Name = {});
64
65/// Split a BasicBlock at \p Builder's insertion point, even if the block is
66/// degenerate (missing the terminator). Its new insert location will stick to
67/// after the instruction before the insertion point (instead of moving with the
68/// instruction the InsertPoint stores internally).
69BasicBlock *splitBB(IRBuilderBase &Builder, bool CreateBranch,
70 llvm::Twine Name = {});
71
72/// Split a BasicBlock at \p Builder's insertion point, even if the block is
73/// degenerate (missing the terminator). Its new insert location will stick to
74/// after the instruction before the insertion point (instead of moving with the
75/// instruction the InsertPoint stores internally).
76BasicBlock *splitBB(IRBuilder<> &Builder, bool CreateBranch, llvm::Twine Name);
77
78/// Like splitBB, but reuses the current block's name for the new name.
79BasicBlock *splitBBWithSuffix(IRBuilderBase &Builder, bool CreateBranch,
80 llvm::Twine Suffix = ".split");
81
82/// Captures attributes that affect generating LLVM-IR using the
83/// OpenMPIRBuilder and related classes. Note that not all attributes are
84/// required for all classes or functions. In some use cases the configuration
85/// is not necessary at all, because because the only functions that are called
86/// are ones that are not dependent on the configuration.
88public:
89 /// Flag to define whether to generate code for the role of the OpenMP host
90 /// (if set to false) or device (if set to true) in an offloading context. It
91 /// is set when the -fopenmp-is-target-device compiler frontend option is
92 /// specified.
93 std::optional<bool> IsTargetDevice;
94
95 /// Flag for specifying if the compilation is done for an accelerator. It is
96 /// set according to the architecture of the target triple and currently only
97 /// true when targeting AMDGPU or NVPTX. Today, these targets can only perform
98 /// the role of an OpenMP target device, so `IsTargetDevice` must also be true
99 /// if `IsGPU` is true. This restriction might be lifted if an accelerator-
100 /// like target with the ability to work as the OpenMP host is added, or if
101 /// the capabilities of the currently supported GPU architectures are
102 /// expanded.
103 std::optional<bool> IsGPU;
104
105 /// Flag for specifying if LLVMUsed information should be emitted.
106 std::optional<bool> EmitLLVMUsedMetaInfo;
107
108 /// Flag for specifying if offloading is mandatory.
109 std::optional<bool> OpenMPOffloadMandatory;
110
111 /// First separator used between the initial two parts of a name.
112 std::optional<StringRef> FirstSeparator;
113 /// Separator used between all of the rest consecutive parts of s name
114 std::optional<StringRef> Separator;
115
116 // Grid Value for the GPU target
117 std::optional<omp::GV> GridValue;
118
119 /// When compilation is being done for the OpenMP host (i.e. `IsTargetDevice =
120 /// false`), this contains the list of offloading triples associated, if any.
122
126 bool HasRequiresReverseOffload,
127 bool HasRequiresUnifiedAddress,
128 bool HasRequiresUnifiedSharedMemory,
129 bool HasRequiresDynamicAllocators);
130
131 // Getters functions that assert if the required values are not present.
132 bool isTargetDevice() const {
133 assert(IsTargetDevice.has_value() && "IsTargetDevice is not set");
134 return *IsTargetDevice;
135 }
136
137 bool isGPU() const {
138 assert(IsGPU.has_value() && "IsGPU is not set");
139 return *IsGPU;
140 }
141
143 assert(OpenMPOffloadMandatory.has_value() &&
144 "OpenMPOffloadMandatory is not set");
146 }
147
149 assert(GridValue.has_value() && "GridValue is not set");
150 return *GridValue;
151 }
152
153 bool hasRequiresFlags() const { return RequiresFlags; }
154 bool hasRequiresReverseOffload() const;
155 bool hasRequiresUnifiedAddress() const;
157 bool hasRequiresDynamicAllocators() const;
158
159 /// Returns requires directive clauses as flags compatible with those expected
160 /// by libomptarget.
161 int64_t getRequiresFlags() const;
162
163 // Returns the FirstSeparator if set, otherwise use the default separator
164 // depending on isGPU
166 if (FirstSeparator.has_value())
167 return *FirstSeparator;
168 if (isGPU())
169 return "_";
170 return ".";
171 }
172
173 // Returns the Separator if set, otherwise use the default separator depending
174 // on isGPU
176 if (Separator.has_value())
177 return *Separator;
178 if (isGPU())
179 return "$";
180 return ".";
181 }
182
184 void setIsGPU(bool Value) { IsGPU = Value; }
190
195
196private:
197 /// Flags for specifying which requires directive clauses are present.
198 int64_t RequiresFlags;
199};
200
201/// Data structure to contain the information needed to uniquely identify
202/// a target entry.
204 /// The prefix used for kernel names.
205 static constexpr const char *KernelNamePrefix = "__omp_offloading_";
206
207 std::string ParentName;
208 unsigned DeviceID;
209 unsigned FileID;
210 unsigned Line;
211 unsigned Count;
212
215 unsigned FileID, unsigned Line, unsigned Count = 0)
217 Count(Count) {}
218
221 unsigned DeviceID, unsigned FileID,
222 unsigned Line, unsigned Count);
223
225 return std::make_tuple(ParentName, DeviceID, FileID, Line, Count) <
226 std::make_tuple(RHS.ParentName, RHS.DeviceID, RHS.FileID, RHS.Line,
227 RHS.Count);
228 }
229};
230
231/// Class that manages information about offload code regions and data
233 /// Number of entries registered so far.
234 OpenMPIRBuilder *OMPBuilder;
235 unsigned OffloadingEntriesNum = 0;
236
237public:
238 /// Base class of the entries info.
240 public:
241 /// Kind of a given entry.
242 enum OffloadingEntryInfoKinds : unsigned {
243 /// Entry is a target region.
245 /// Entry is a declare target variable.
247 /// Invalid entry info.
249 };
250
251 protected:
253 explicit OffloadEntryInfo(OffloadingEntryInfoKinds Kind) : Kind(Kind) {}
254 explicit OffloadEntryInfo(OffloadingEntryInfoKinds Kind, unsigned Order,
255 uint32_t Flags)
256 : Flags(Flags), Order(Order), Kind(Kind) {}
257 ~OffloadEntryInfo() = default;
258
259 public:
260 bool isValid() const { return Order != ~0u; }
261 unsigned getOrder() const { return Order; }
262 OffloadingEntryInfoKinds getKind() const { return Kind; }
263 uint32_t getFlags() const { return Flags; }
264 void setFlags(uint32_t NewFlags) { Flags = NewFlags; }
265 Constant *getAddress() const { return cast_or_null<Constant>(Addr); }
267 assert(!Addr.pointsToAliveValue() && "Address has been set before!");
268 Addr = V;
269 }
270 static bool classof(const OffloadEntryInfo *Info) { return true; }
271
272 private:
273 /// Address of the entity that has to be mapped for offloading.
274 WeakTrackingVH Addr;
275
276 /// Flags associated with the device global.
277 uint32_t Flags = 0u;
278
279 /// Order this entry was emitted.
280 unsigned Order = ~0u;
281
283 };
284
285 /// Return true if a there are no entries defined.
286 bool empty() const;
287 /// Return number of entries defined so far.
288 unsigned size() const { return OffloadingEntriesNum; }
289
290 OffloadEntriesInfoManager(OpenMPIRBuilder *builder) : OMPBuilder(builder) {}
291
292 //
293 // Target region entries related.
294 //
295
296 /// Kind of the target registry entry.
298 /// Mark the entry as target region.
300 };
301
302 /// Target region entries info.
304 /// Address that can be used as the ID of the entry.
305 Constant *ID = nullptr;
306
307 public:
310 explicit OffloadEntryInfoTargetRegion(unsigned Order, Constant *Addr,
311 Constant *ID,
314 ID(ID) {
316 }
317
318 Constant *getID() const { return ID; }
319 void setID(Constant *V) {
320 assert(!ID && "ID has been set before!");
321 ID = V;
322 }
323 static bool classof(const OffloadEntryInfo *Info) {
324 return Info->getKind() == OffloadingEntryInfoTargetRegion;
325 }
326 };
327
328 /// Initialize target region entry.
329 /// This is ONLY needed for DEVICE compilation.
331 unsigned Order);
332 /// Register target region entry.
336 /// Return true if a target region entry with the provided information
337 /// exists.
339 bool IgnoreAddressId = false) const;
340
341 // Return the Name based on \a EntryInfo using the next available Count.
343 const TargetRegionEntryInfo &EntryInfo);
344
345 /// brief Applies action \a Action on all registered entries.
346 typedef function_ref<void(const TargetRegionEntryInfo &EntryInfo,
347 const OffloadEntryInfoTargetRegion &)>
349 void
351
352 //
353 // Device global variable entries related.
354 //
355
356 /// Kind of the global variable entry..
358 /// Mark the entry as a to declare target.
360 /// Mark the entry as a to declare target link.
362 /// Mark the entry as a declare target enter.
364 /// Mark the entry as having no declare target entry kind.
366 /// Mark the entry as a declare target indirect global.
368 /// Mark the entry as a register requires global.
370 };
371
372 /// Kind of device clause for declare target variables
373 /// and functions
374 /// NOTE: Currently not used as a part of a variable entry
375 /// used for Flang and Clang to interface with the variable
376 /// related registration functions
378 /// The target is marked for all devices
380 /// The target is marked for non-host devices
382 /// The target is marked for host devices
384 /// The target is marked as having no clause
386 };
387
388 /// Device global variable entries info.
390 /// Type of the global variable.
391 int64_t VarSize;
393 const std::string VarName;
394
395 public:
398 explicit OffloadEntryInfoDeviceGlobalVar(unsigned Order,
401 explicit OffloadEntryInfoDeviceGlobalVar(unsigned Order, Constant *Addr,
402 int64_t VarSize,
405 const std::string &VarName)
407 VarSize(VarSize), Linkage(Linkage), VarName(VarName) {
409 }
410
411 int64_t getVarSize() const { return VarSize; }
412 StringRef getVarName() const { return VarName; }
413 void setVarSize(int64_t Size) { VarSize = Size; }
414 GlobalValue::LinkageTypes getLinkage() const { return Linkage; }
415 void setLinkage(GlobalValue::LinkageTypes LT) { Linkage = LT; }
416 static bool classof(const OffloadEntryInfo *Info) {
417 return Info->getKind() == OffloadingEntryInfoDeviceGlobalVar;
418 }
419 };
420
421 /// Initialize device global variable entry.
422 /// This is ONLY used for DEVICE compilation.
425 unsigned Order);
426
427 /// Register device global variable entry.
429 int64_t VarSize,
432 /// Checks if the variable with the given name has been registered already.
434 return OffloadEntriesDeviceGlobalVar.count(VarName) > 0;
435 }
436 /// Applies action \a Action on all registered entries.
437 typedef function_ref<void(StringRef, const OffloadEntryInfoDeviceGlobalVar &)>
441
442private:
443 /// Return the count of entries at a particular source location.
444 unsigned
445 getTargetRegionEntryInfoCount(const TargetRegionEntryInfo &EntryInfo) const;
446
447 /// Update the count of entries at a particular source location.
448 void
449 incrementTargetRegionEntryInfoCount(const TargetRegionEntryInfo &EntryInfo);
450
452 getTargetRegionEntryCountKey(const TargetRegionEntryInfo &EntryInfo) {
453 return TargetRegionEntryInfo(EntryInfo.ParentName, EntryInfo.DeviceID,
454 EntryInfo.FileID, EntryInfo.Line, 0);
455 }
456
457 // Count of entries at a location.
458 std::map<TargetRegionEntryInfo, unsigned> OffloadEntriesTargetRegionCount;
459
460 // Storage for target region entries kind.
461 typedef std::map<TargetRegionEntryInfo, OffloadEntryInfoTargetRegion>
462 OffloadEntriesTargetRegionTy;
463 OffloadEntriesTargetRegionTy OffloadEntriesTargetRegion;
464 /// Storage for device global variable entries kind. The storage is to be
465 /// indexed by mangled name.
467 OffloadEntriesDeviceGlobalVarTy;
468 OffloadEntriesDeviceGlobalVarTy OffloadEntriesDeviceGlobalVar;
469};
470
471/// An interface to create LLVM-IR for OpenMP directives.
472///
473/// Each OpenMP directive has a corresponding public generator method.
475public:
476 /// Create a new OpenMPIRBuilder operating on the given module \p M. This will
477 /// not have an effect on \p M (see initialize)
479 : M(M), Builder(M.getContext()), OffloadInfoManager(this),
480 T(Triple(M.getTargetTriple())) {}
482
484 llvm::Value *AtomicVar;
485
486 public:
492 AtomicVar(AtomicVar) {}
493
494 llvm::Value *getAtomicPointer() const override { return AtomicVar; }
497 const llvm::Twine &Name) const override {
498 llvm::AllocaInst *allocaInst = Builder->CreateAlloca(Ty);
499 allocaInst->setName(Name);
500 return allocaInst;
501 }
502 };
503 /// Initialize the internal state, this will put structures types and
504 /// potentially other helpers into the underlying module. Must be called
505 /// before any other method and only once! This internal state includes types
506 /// used in the OpenMPIRBuilder generated from OMPKinds.def.
507 void initialize();
508
510
511 /// Finalize the underlying module, e.g., by outlining regions.
512 /// \param Fn The function to be finalized. If not used,
513 /// all functions are finalized.
514 void finalize(Function *Fn = nullptr);
515
516 /// Add attributes known for \p FnID to \p Fn.
518
519 /// Type used throughout for insertion points.
521
522 /// Type used to represent an insertion point or an error value.
524
525 /// Get the create a name using the platform specific separators.
526 /// \param Parts parts of the final name that needs separation
527 /// The created name has a first separator between the first and second part
528 /// and a second separator between all other parts.
529 /// E.g. with FirstSeparator "$" and Separator "." and
530 /// parts: "p1", "p2", "p3", "p4"
531 /// The resulting name is "p1$p2.p3.p4"
532 /// The separators are retrieved from the OpenMPIRBuilderConfig.
533 std::string createPlatformSpecificName(ArrayRef<StringRef> Parts) const;
534
535 /// Callback type for variable finalization (think destructors).
536 ///
537 /// \param CodeGenIP is the insertion point at which the finalization code
538 /// should be placed.
539 ///
540 /// A finalize callback knows about all objects that need finalization, e.g.
541 /// destruction, when the scope of the currently generated construct is left
542 /// at the time, and location, the callback is invoked.
543 using FinalizeCallbackTy = std::function<Error(InsertPointTy CodeGenIP)>;
544
546 /// The finalization callback provided by the last in-flight invocation of
547 /// createXXXX for the directive of kind DK.
549
550 /// The directive kind of the innermost directive that has an associated
551 /// region which might require finalization when it is left.
552 omp::Directive DK;
553
554 /// Flag to indicate if the directive is cancellable.
556 };
557
558 /// Push a finalization callback on the finalization stack.
559 ///
560 /// NOTE: Temporary solution until Clang CG is gone.
562 FinalizationStack.push_back(FI);
563 }
564
565 /// Pop the last finalization callback from the finalization stack.
566 ///
567 /// NOTE: Temporary solution until Clang CG is gone.
569
570 /// Callback type for body (=inner region) code generation
571 ///
572 /// The callback takes code locations as arguments, each describing a
573 /// location where additional instructions can be inserted.
574 ///
575 /// The CodeGenIP may be in the middle of a basic block or point to the end of
576 /// it. The basic block may have a terminator or be degenerate. The callback
577 /// function may just insert instructions at that position, but also split the
578 /// block (without the Before argument of BasicBlock::splitBasicBlock such
579 /// that the identify of the split predecessor block is preserved) and insert
580 /// additional control flow, including branches that do not lead back to what
581 /// follows the CodeGenIP. Note that since the callback is allowed to split
582 /// the block, callers must assume that InsertPoints to positions in the
583 /// BasicBlock after CodeGenIP including CodeGenIP itself are invalidated. If
584 /// such InsertPoints need to be preserved, it can split the block itself
585 /// before calling the callback.
586 ///
587 /// AllocaIP and CodeGenIP must not point to the same position.
588 ///
589 /// \param AllocaIP is the insertion point at which new alloca instructions
590 /// should be placed. The BasicBlock it is pointing to must
591 /// not be split.
592 /// \param CodeGenIP is the insertion point at which the body code should be
593 /// placed.
594 ///
595 /// \return an error, if any were triggered during execution.
597 function_ref<Error(InsertPointTy AllocaIP, InsertPointTy CodeGenIP)>;
598
599 // This is created primarily for sections construct as llvm::function_ref
600 // (BodyGenCallbackTy) is not storable (as described in the comments of
601 // function_ref class - function_ref contains non-ownable reference
602 // to the callable.
603 ///
604 /// \return an error, if any were triggered during execution.
606 std::function<Error(InsertPointTy AllocaIP, InsertPointTy CodeGenIP)>;
607
608 /// Callback type for loop body code generation.
609 ///
610 /// \param CodeGenIP is the insertion point where the loop's body code must be
611 /// placed. This will be a dedicated BasicBlock with a
612 /// conditional branch from the loop condition check and
613 /// terminated with an unconditional branch to the loop
614 /// latch.
615 /// \param IndVar is the induction variable usable at the insertion point.
616 ///
617 /// \return an error, if any were triggered during execution.
619 function_ref<Error(InsertPointTy CodeGenIP, Value *IndVar)>;
620
621 /// Callback type for variable privatization (think copy & default
622 /// constructor).
623 ///
624 /// \param AllocaIP is the insertion point at which new alloca instructions
625 /// should be placed.
626 /// \param CodeGenIP is the insertion point at which the privatization code
627 /// should be placed.
628 /// \param Original The value being copied/created, should not be used in the
629 /// generated IR.
630 /// \param Inner The equivalent of \p Original that should be used in the
631 /// generated IR; this is equal to \p Original if the value is
632 /// a pointer and can thus be passed directly, otherwise it is
633 /// an equivalent but different value.
634 /// \param ReplVal The replacement value, thus a copy or new created version
635 /// of \p Inner.
636 ///
637 /// \returns The new insertion point where code generation continues and
638 /// \p ReplVal the replacement value.
640 InsertPointTy AllocaIP, InsertPointTy CodeGenIP, Value &Original,
641 Value &Inner, Value *&ReplVal)>;
642
643 /// Description of a LLVM-IR insertion point (IP) and a debug/source location
644 /// (filename, line, column, ...).
647 : IP(IRB.saveIP()), DL(IRB.getCurrentDebugLocation()) {}
650 : IP(IP), DL(DL) {}
653 };
654
655 /// Emitter methods for OpenMP directives.
656 ///
657 ///{
658
659 /// Generator for '#omp barrier'
660 ///
661 /// \param Loc The location where the barrier directive was encountered.
662 /// \param Kind The kind of directive that caused the barrier.
663 /// \param ForceSimpleCall Flag to force a simple (=non-cancellation) barrier.
664 /// \param CheckCancelFlag Flag to indicate a cancel barrier return value
665 /// should be checked and acted upon.
666 /// \param ThreadID Optional parameter to pass in any existing ThreadID value.
667 ///
668 /// \returns The insertion point after the barrier.
670 omp::Directive Kind,
671 bool ForceSimpleCall = false,
672 bool CheckCancelFlag = true);
673
674 /// Generator for '#omp cancel'
675 ///
676 /// \param Loc The location where the directive was encountered.
677 /// \param IfCondition The evaluated 'if' clause expression, if any.
678 /// \param CanceledDirective The kind of directive that is cancled.
679 ///
680 /// \returns The insertion point after the barrier.
682 Value *IfCondition,
683 omp::Directive CanceledDirective);
684
685 /// Generator for '#omp parallel'
686 ///
687 /// \param Loc The insert and source location description.
688 /// \param AllocaIP The insertion points to be used for alloca instructions.
689 /// \param BodyGenCB Callback that will generate the region code.
690 /// \param PrivCB Callback to copy a given variable (think copy constructor).
691 /// \param FiniCB Callback to finalize variable copies.
692 /// \param IfCondition The evaluated 'if' clause expression, if any.
693 /// \param NumThreads The evaluated 'num_threads' clause expression, if any.
694 /// \param ProcBind The value of the 'proc_bind' clause (see ProcBindKind).
695 /// \param IsCancellable Flag to indicate a cancellable parallel region.
696 ///
697 /// \returns The insertion position *after* the parallel.
700 BodyGenCallbackTy BodyGenCB, PrivatizeCallbackTy PrivCB,
701 FinalizeCallbackTy FiniCB, Value *IfCondition,
702 Value *NumThreads, omp::ProcBindKind ProcBind,
703 bool IsCancellable);
704
705 /// Generator for the control flow structure of an OpenMP canonical loop.
706 ///
707 /// This generator operates on the logical iteration space of the loop, i.e.
708 /// the caller only has to provide a loop trip count of the loop as defined by
709 /// base language semantics. The trip count is interpreted as an unsigned
710 /// integer. The induction variable passed to \p BodyGenCB will be of the same
711 /// type and run from 0 to \p TripCount - 1. It is up to the callback to
712 /// convert the logical iteration variable to the loop counter variable in the
713 /// loop body.
714 ///
715 /// \param Loc The insert and source location description. The insert
716 /// location can be between two instructions or the end of a
717 /// degenerate block (e.g. a BB under construction).
718 /// \param BodyGenCB Callback that will generate the loop body code.
719 /// \param TripCount Number of iterations the loop body is executed.
720 /// \param Name Base name used to derive BB and instruction names.
721 ///
722 /// \returns An object representing the created control flow structure which
723 /// can be used for loop-associated directives.
726 LoopBodyGenCallbackTy BodyGenCB, Value *TripCount,
727 const Twine &Name = "loop");
728
729 /// Generator for the control flow structure of an OpenMP canonical loop.
730 ///
731 /// Instead of a logical iteration space, this allows specifying user-defined
732 /// loop counter values using increment, upper- and lower bounds. To
733 /// disambiguate the terminology when counting downwards, instead of lower
734 /// bounds we use \p Start for the loop counter value in the first body
735 /// iteration.
736 ///
737 /// Consider the following limitations:
738 ///
739 /// * A loop counter space over all integer values of its bit-width cannot be
740 /// represented. E.g using uint8_t, its loop trip count of 256 cannot be
741 /// stored into an 8 bit integer):
742 ///
743 /// DO I = 0, 255, 1
744 ///
745 /// * Unsigned wrapping is only supported when wrapping only "once"; E.g.
746 /// effectively counting downwards:
747 ///
748 /// for (uint8_t i = 100u; i > 0; i += 127u)
749 ///
750 ///
751 /// TODO: May need to add additional parameters to represent:
752 ///
753 /// * Allow representing downcounting with unsigned integers.
754 ///
755 /// * Sign of the step and the comparison operator might disagree:
756 ///
757 /// for (int i = 0; i < 42; i -= 1u)
758 ///
759 //
760 /// \param Loc The insert and source location description.
761 /// \param BodyGenCB Callback that will generate the loop body code.
762 /// \param Start Value of the loop counter for the first iterations.
763 /// \param Stop Loop counter values past this will stop the loop.
764 /// \param Step Loop counter increment after each iteration; negative
765 /// means counting down.
766 /// \param IsSigned Whether Start, Stop and Step are signed integers.
767 /// \param InclusiveStop Whether \p Stop itself is a valid value for the loop
768 /// counter.
769 /// \param ComputeIP Insertion point for instructions computing the trip
770 /// count. Can be used to ensure the trip count is available
771 /// at the outermost loop of a loop nest. If not set,
772 /// defaults to the preheader of the generated loop.
773 /// \param Name Base name used to derive BB and instruction names.
774 ///
775 /// \returns An object representing the created control flow structure which
776 /// can be used for loop-associated directives.
778 const LocationDescription &Loc, LoopBodyGenCallbackTy BodyGenCB,
779 Value *Start, Value *Stop, Value *Step, bool IsSigned, bool InclusiveStop,
780 InsertPointTy ComputeIP = {}, const Twine &Name = "loop");
781
782 /// Collapse a loop nest into a single loop.
783 ///
784 /// Merges loops of a loop nest into a single CanonicalLoopNest representation
785 /// that has the same number of innermost loop iterations as the origin loop
786 /// nest. The induction variables of the input loops are derived from the
787 /// collapsed loop's induction variable. This is intended to be used to
788 /// implement OpenMP's collapse clause. Before applying a directive,
789 /// collapseLoops normalizes a loop nest to contain only a single loop and the
790 /// directive's implementation does not need to handle multiple loops itself.
791 /// This does not remove the need to handle all loop nest handling by
792 /// directives, such as the ordered(<n>) clause or the simd schedule-clause
793 /// modifier of the worksharing-loop directive.
794 ///
795 /// Example:
796 /// \code
797 /// for (int i = 0; i < 7; ++i) // Canonical loop "i"
798 /// for (int j = 0; j < 9; ++j) // Canonical loop "j"
799 /// body(i, j);
800 /// \endcode
801 ///
802 /// After collapsing with Loops={i,j}, the loop is changed to
803 /// \code
804 /// for (int ij = 0; ij < 63; ++ij) {
805 /// int i = ij / 9;
806 /// int j = ij % 9;
807 /// body(i, j);
808 /// }
809 /// \endcode
810 ///
811 /// In the current implementation, the following limitations apply:
812 ///
813 /// * All input loops have an induction variable of the same type.
814 ///
815 /// * The collapsed loop will have the same trip count integer type as the
816 /// input loops. Therefore it is possible that the collapsed loop cannot
817 /// represent all iterations of the input loops. For instance, assuming a
818 /// 32 bit integer type, and two input loops both iterating 2^16 times, the
819 /// theoretical trip count of the collapsed loop would be 2^32 iteration,
820 /// which cannot be represented in an 32-bit integer. Behavior is undefined
821 /// in this case.
822 ///
823 /// * The trip counts of every input loop must be available at \p ComputeIP.
824 /// Non-rectangular loops are not yet supported.
825 ///
826 /// * At each nest level, code between a surrounding loop and its nested loop
827 /// is hoisted into the loop body, and such code will be executed more
828 /// often than before collapsing (or not at all if any inner loop iteration
829 /// has a trip count of 0). This is permitted by the OpenMP specification.
830 ///
831 /// \param DL Debug location for instructions added for collapsing,
832 /// such as instructions to compute/derive the input loop's
833 /// induction variables.
834 /// \param Loops Loops in the loop nest to collapse. Loops are specified
835 /// from outermost-to-innermost and every control flow of a
836 /// loop's body must pass through its directly nested loop.
837 /// \param ComputeIP Where additional instruction that compute the collapsed
838 /// trip count. If not set, defaults to before the generated
839 /// loop.
840 ///
841 /// \returns The CanonicalLoopInfo object representing the collapsed loop.
844 InsertPointTy ComputeIP);
845
846 /// Get the default alignment value for given target
847 ///
848 /// \param TargetTriple Target triple
849 /// \param Features StringMap which describes extra CPU features
850 static unsigned getOpenMPDefaultSimdAlign(const Triple &TargetTriple,
851 const StringMap<bool> &Features);
852
853 /// Retrieve (or create if non-existent) the address of a declare
854 /// target variable, used in conjunction with registerTargetGlobalVariable
855 /// to create declare target global variables.
856 ///
857 /// \param CaptureClause - enumerator corresponding to the OpenMP capture
858 /// clause used in conjunction with the variable being registered (link,
859 /// to, enter).
860 /// \param DeviceClause - enumerator corresponding to the OpenMP capture
861 /// clause used in conjunction with the variable being registered (nohost,
862 /// host, any)
863 /// \param IsDeclaration - boolean stating if the variable being registered
864 /// is a declaration-only and not a definition
865 /// \param IsExternallyVisible - boolean stating if the variable is externally
866 /// visible
867 /// \param EntryInfo - Unique entry information for the value generated
868 /// using getTargetEntryUniqueInfo, used to name generated pointer references
869 /// to the declare target variable
870 /// \param MangledName - the mangled name of the variable being registered
871 /// \param GeneratedRefs - references generated by invocations of
872 /// registerTargetGlobalVariable invoked from getAddrOfDeclareTargetVar,
873 /// these are required by Clang for book keeping.
874 /// \param OpenMPSIMD - if OpenMP SIMD mode is currently enabled
875 /// \param TargetTriple - The OpenMP device target triple we are compiling
876 /// for
877 /// \param LlvmPtrTy - The type of the variable we are generating or
878 /// retrieving an address for
879 /// \param GlobalInitializer - a lambda function which creates a constant
880 /// used for initializing a pointer reference to the variable in certain
881 /// cases. If a nullptr is passed, it will default to utilising the original
882 /// variable to initialize the pointer reference.
883 /// \param VariableLinkage - a lambda function which returns the variables
884 /// linkage type, if unspecified and a nullptr is given, it will instead
885 /// utilise the linkage stored on the existing global variable in the
886 /// LLVMModule.
890 bool IsDeclaration, bool IsExternallyVisible,
891 TargetRegionEntryInfo EntryInfo, StringRef MangledName,
892 std::vector<GlobalVariable *> &GeneratedRefs, bool OpenMPSIMD,
893 std::vector<Triple> TargetTriple, Type *LlvmPtrTy,
894 std::function<Constant *()> GlobalInitializer,
895 std::function<GlobalValue::LinkageTypes()> VariableLinkage);
896
897 /// Registers a target variable for device or host.
898 ///
899 /// \param CaptureClause - enumerator corresponding to the OpenMP capture
900 /// clause used in conjunction with the variable being registered (link,
901 /// to, enter).
902 /// \param DeviceClause - enumerator corresponding to the OpenMP capture
903 /// clause used in conjunction with the variable being registered (nohost,
904 /// host, any)
905 /// \param IsDeclaration - boolean stating if the variable being registered
906 /// is a declaration-only and not a definition
907 /// \param IsExternallyVisible - boolean stating if the variable is externally
908 /// visible
909 /// \param EntryInfo - Unique entry information for the value generated
910 /// using getTargetEntryUniqueInfo, used to name generated pointer references
911 /// to the declare target variable
912 /// \param MangledName - the mangled name of the variable being registered
913 /// \param GeneratedRefs - references generated by invocations of
914 /// registerTargetGlobalVariable these are required by Clang for book
915 /// keeping.
916 /// \param OpenMPSIMD - if OpenMP SIMD mode is currently enabled
917 /// \param TargetTriple - The OpenMP device target triple we are compiling
918 /// for
919 /// \param GlobalInitializer - a lambda function which creates a constant
920 /// used for initializing a pointer reference to the variable in certain
921 /// cases. If a nullptr is passed, it will default to utilising the original
922 /// variable to initialize the pointer reference.
923 /// \param VariableLinkage - a lambda function which returns the variables
924 /// linkage type, if unspecified and a nullptr is given, it will instead
925 /// utilise the linkage stored on the existing global variable in the
926 /// LLVMModule.
927 /// \param LlvmPtrTy - The type of the variable we are generating or
928 /// retrieving an address for
929 /// \param Addr - the original llvm value (addr) of the variable to be
930 /// registered
934 bool IsDeclaration, bool IsExternallyVisible,
935 TargetRegionEntryInfo EntryInfo, StringRef MangledName,
936 std::vector<GlobalVariable *> &GeneratedRefs, bool OpenMPSIMD,
937 std::vector<Triple> TargetTriple,
938 std::function<Constant *()> GlobalInitializer,
939 std::function<GlobalValue::LinkageTypes()> VariableLinkage,
940 Type *LlvmPtrTy, Constant *Addr);
941
942 /// Get the offset of the OMP_MAP_MEMBER_OF field.
943 unsigned getFlagMemberOffset();
944
945 /// Get OMP_MAP_MEMBER_OF flag with extra bits reserved based on
946 /// the position given.
947 /// \param Position - A value indicating the position of the parent
948 /// of the member in the kernel argument structure, often retrieved
949 /// by the parents position in the combined information vectors used
950 /// to generate the structure itself. Multiple children (member's of)
951 /// with the same parent will use the same returned member flag.
953
954 /// Given an initial flag set, this function modifies it to contain
955 /// the passed in MemberOfFlag generated from the getMemberOfFlag
956 /// function. The results are dependent on the existing flag bits
957 /// set in the original flag set.
958 /// \param Flags - The original set of flags to be modified with the
959 /// passed in MemberOfFlag.
960 /// \param MemberOfFlag - A modified OMP_MAP_MEMBER_OF flag, adjusted
961 /// slightly based on the getMemberOfFlag which adjusts the flag bits
962 /// based on the members position in its parent.
964 omp::OpenMPOffloadMappingFlags MemberOfFlag);
965
966private:
967 /// Modifies the canonical loop to be a statically-scheduled workshare loop
968 /// which is executed on the device
969 ///
970 /// This takes a \p CLI representing a canonical loop, such as the one
971 /// created by \see createCanonicalLoop and emits additional instructions to
972 /// turn it into a workshare loop. In particular, it calls to an OpenMP
973 /// runtime function in the preheader to call OpenMP device rtl function
974 /// which handles worksharing of loop body interations.
975 ///
976 /// \param DL Debug location for instructions added for the
977 /// workshare-loop construct itself.
978 /// \param CLI A descriptor of the canonical loop to workshare.
979 /// \param AllocaIP An insertion point for Alloca instructions usable in the
980 /// preheader of the loop.
981 /// \param LoopType Information about type of loop worksharing.
982 /// It corresponds to type of loop workshare OpenMP pragma.
983 ///
984 /// \returns Point where to insert code after the workshare construct.
985 InsertPointTy applyWorkshareLoopTarget(DebugLoc DL, CanonicalLoopInfo *CLI,
986 InsertPointTy AllocaIP,
987 omp::WorksharingLoopType LoopType);
988
989 /// Modifies the canonical loop to be a statically-scheduled workshare loop.
990 ///
991 /// This takes a \p LoopInfo representing a canonical loop, such as the one
992 /// created by \p createCanonicalLoop and emits additional instructions to
993 /// turn it into a workshare loop. In particular, it calls to an OpenMP
994 /// runtime function in the preheader to obtain the loop bounds to be used in
995 /// the current thread, updates the relevant instructions in the canonical
996 /// loop and calls to an OpenMP runtime finalization function after the loop.
997 ///
998 /// \param DL Debug location for instructions added for the
999 /// workshare-loop construct itself.
1000 /// \param CLI A descriptor of the canonical loop to workshare.
1001 /// \param AllocaIP An insertion point for Alloca instructions usable in the
1002 /// preheader of the loop.
1003 /// \param NeedsBarrier Indicates whether a barrier must be inserted after
1004 /// the loop.
1005 ///
1006 /// \returns Point where to insert code after the workshare construct.
1007 InsertPointOrErrorTy applyStaticWorkshareLoop(DebugLoc DL,
1008 CanonicalLoopInfo *CLI,
1009 InsertPointTy AllocaIP,
1010 bool NeedsBarrier);
1011
1012 /// Modifies the canonical loop a statically-scheduled workshare loop with a
1013 /// user-specified chunk size.
1014 ///
1015 /// \param DL Debug location for instructions added for the
1016 /// workshare-loop construct itself.
1017 /// \param CLI A descriptor of the canonical loop to workshare.
1018 /// \param AllocaIP An insertion point for Alloca instructions usable in
1019 /// the preheader of the loop.
1020 /// \param NeedsBarrier Indicates whether a barrier must be inserted after the
1021 /// loop.
1022 /// \param ChunkSize The user-specified chunk size.
1023 ///
1024 /// \returns Point where to insert code after the workshare construct.
1025 InsertPointOrErrorTy applyStaticChunkedWorkshareLoop(DebugLoc DL,
1026 CanonicalLoopInfo *CLI,
1027 InsertPointTy AllocaIP,
1028 bool NeedsBarrier,
1029 Value *ChunkSize);
1030
1031 /// Modifies the canonical loop to be a dynamically-scheduled workshare loop.
1032 ///
1033 /// This takes a \p LoopInfo representing a canonical loop, such as the one
1034 /// created by \p createCanonicalLoop and emits additional instructions to
1035 /// turn it into a workshare loop. In particular, it calls to an OpenMP
1036 /// runtime function in the preheader to obtain, and then in each iteration
1037 /// to update the loop counter.
1038 ///
1039 /// \param DL Debug location for instructions added for the
1040 /// workshare-loop construct itself.
1041 /// \param CLI A descriptor of the canonical loop to workshare.
1042 /// \param AllocaIP An insertion point for Alloca instructions usable in the
1043 /// preheader of the loop.
1044 /// \param SchedType Type of scheduling to be passed to the init function.
1045 /// \param NeedsBarrier Indicates whether a barrier must be insterted after
1046 /// the loop.
1047 /// \param Chunk The size of loop chunk considered as a unit when
1048 /// scheduling. If \p nullptr, defaults to 1.
1049 ///
1050 /// \returns Point where to insert code after the workshare construct.
1051 InsertPointOrErrorTy applyDynamicWorkshareLoop(DebugLoc DL,
1052 CanonicalLoopInfo *CLI,
1053 InsertPointTy AllocaIP,
1054 omp::OMPScheduleType SchedType,
1055 bool NeedsBarrier,
1056 Value *Chunk = nullptr);
1057
1058 /// Create alternative version of the loop to support if clause
1059 ///
1060 /// OpenMP if clause can require to generate second loop. This loop
1061 /// will be executed when if clause condition is not met. createIfVersion
1062 /// adds branch instruction to the copied loop if \p ifCond is not met.
1063 ///
1064 /// \param Loop Original loop which should be versioned.
1065 /// \param IfCond Value which corresponds to if clause condition
1066 /// \param VMap Value to value map to define relation between
1067 /// original and copied loop values and loop blocks.
1068 /// \param NamePrefix Optional name prefix for if.then if.else blocks.
1069 void createIfVersion(CanonicalLoopInfo *Loop, Value *IfCond,
1070 ValueToValueMapTy &VMap, const Twine &NamePrefix = "");
1071
1072public:
1073 /// Modifies the canonical loop to be a workshare loop.
1074 ///
1075 /// This takes a \p LoopInfo representing a canonical loop, such as the one
1076 /// created by \p createCanonicalLoop and emits additional instructions to
1077 /// turn it into a workshare loop. In particular, it calls to an OpenMP
1078 /// runtime function in the preheader to obtain the loop bounds to be used in
1079 /// the current thread, updates the relevant instructions in the canonical
1080 /// loop and calls to an OpenMP runtime finalization function after the loop.
1081 ///
1082 /// The concrete transformation is done by applyStaticWorkshareLoop,
1083 /// applyStaticChunkedWorkshareLoop, or applyDynamicWorkshareLoop, depending
1084 /// on the value of \p SchedKind and \p ChunkSize.
1085 ///
1086 /// \param DL Debug location for instructions added for the
1087 /// workshare-loop construct itself.
1088 /// \param CLI A descriptor of the canonical loop to workshare.
1089 /// \param AllocaIP An insertion point for Alloca instructions usable in the
1090 /// preheader of the loop.
1091 /// \param NeedsBarrier Indicates whether a barrier must be insterted after
1092 /// the loop.
1093 /// \param SchedKind Scheduling algorithm to use.
1094 /// \param ChunkSize The chunk size for the inner loop.
1095 /// \param HasSimdModifier Whether the simd modifier is present in the
1096 /// schedule clause.
1097 /// \param HasMonotonicModifier Whether the monotonic modifier is present in
1098 /// the schedule clause.
1099 /// \param HasNonmonotonicModifier Whether the nonmonotonic modifier is
1100 /// present in the schedule clause.
1101 /// \param HasOrderedClause Whether the (parameterless) ordered clause is
1102 /// present.
1103 /// \param LoopType Information about type of loop worksharing.
1104 /// It corresponds to type of loop workshare OpenMP pragma.
1105 ///
1106 /// \returns Point where to insert code after the workshare construct.
1109 bool NeedsBarrier,
1110 llvm::omp::ScheduleKind SchedKind = llvm::omp::OMP_SCHEDULE_Default,
1111 Value *ChunkSize = nullptr, bool HasSimdModifier = false,
1112 bool HasMonotonicModifier = false, bool HasNonmonotonicModifier = false,
1113 bool HasOrderedClause = false,
1114 omp::WorksharingLoopType LoopType =
1116
1117 /// Tile a loop nest.
1118 ///
1119 /// Tiles the loops of \p Loops by the tile sizes in \p TileSizes. Loops in
1120 /// \p/ Loops must be perfectly nested, from outermost to innermost loop
1121 /// (i.e. Loops.front() is the outermost loop). The trip count llvm::Value
1122 /// of every loop and every tile sizes must be usable in the outermost
1123 /// loop's preheader. This implies that the loop nest is rectangular.
1124 ///
1125 /// Example:
1126 /// \code
1127 /// for (int i = 0; i < 15; ++i) // Canonical loop "i"
1128 /// for (int j = 0; j < 14; ++j) // Canonical loop "j"
1129 /// body(i, j);
1130 /// \endcode
1131 ///
1132 /// After tiling with Loops={i,j} and TileSizes={5,7}, the loop is changed to
1133 /// \code
1134 /// for (int i1 = 0; i1 < 3; ++i1)
1135 /// for (int j1 = 0; j1 < 2; ++j1)
1136 /// for (int i2 = 0; i2 < 5; ++i2)
1137 /// for (int j2 = 0; j2 < 7; ++j2)
1138 /// body(i1*3+i2, j1*3+j2);
1139 /// \endcode
1140 ///
1141 /// The returned vector are the loops {i1,j1,i2,j2}. The loops i1 and j1 are
1142 /// referred to the floor, and the loops i2 and j2 are the tiles. Tiling also
1143 /// handles non-constant trip counts, non-constant tile sizes and trip counts
1144 /// that are not multiples of the tile size. In the latter case the tile loop
1145 /// of the last floor-loop iteration will have fewer iterations than specified
1146 /// as its tile size.
1147 ///
1148 ///
1149 /// @param DL Debug location for instructions added by tiling, for
1150 /// instance the floor- and tile trip count computation.
1151 /// @param Loops Loops to tile. The CanonicalLoopInfo objects are
1152 /// invalidated by this method, i.e. should not used after
1153 /// tiling.
1154 /// @param TileSizes For each loop in \p Loops, the tile size for that
1155 /// dimensions.
1156 ///
1157 /// \returns A list of generated loops. Contains twice as many loops as the
1158 /// input loop nest; the first half are the floor loops and the
1159 /// second half are the tile loops.
1160 std::vector<CanonicalLoopInfo *>
1162 ArrayRef<Value *> TileSizes);
1163
1164 /// Fully unroll a loop.
1165 ///
1166 /// Instead of unrolling the loop immediately (and duplicating its body
1167 /// instructions), it is deferred to LLVM's LoopUnrollPass by adding loop
1168 /// metadata.
1169 ///
1170 /// \param DL Debug location for instructions added by unrolling.
1171 /// \param Loop The loop to unroll. The loop will be invalidated.
1173
1174 /// Fully or partially unroll a loop. How the loop is unrolled is determined
1175 /// using LLVM's LoopUnrollPass.
1176 ///
1177 /// \param DL Debug location for instructions added by unrolling.
1178 /// \param Loop The loop to unroll. The loop will be invalidated.
1180
1181 /// Partially unroll a loop.
1182 ///
1183 /// The CanonicalLoopInfo of the unrolled loop for use with chained
1184 /// loop-associated directive can be requested using \p UnrolledCLI. Not
1185 /// needing the CanonicalLoopInfo allows more efficient code generation by
1186 /// deferring the actual unrolling to the LoopUnrollPass using loop metadata.
1187 /// A loop-associated directive applied to the unrolled loop needs to know the
1188 /// new trip count which means that if using a heuristically determined unroll
1189 /// factor (\p Factor == 0), that factor must be computed immediately. We are
1190 /// using the same logic as the LoopUnrollPass to derived the unroll factor,
1191 /// but which assumes that some canonicalization has taken place (e.g.
1192 /// Mem2Reg, LICM, GVN, Inlining, etc.). That is, the heuristic will perform
1193 /// better when the unrolled loop's CanonicalLoopInfo is not needed.
1194 ///
1195 /// \param DL Debug location for instructions added by unrolling.
1196 /// \param Loop The loop to unroll. The loop will be invalidated.
1197 /// \param Factor The factor to unroll the loop by. A factor of 0
1198 /// indicates that a heuristic should be used to determine
1199 /// the unroll-factor.
1200 /// \param UnrolledCLI If non-null, receives the CanonicalLoopInfo of the
1201 /// partially unrolled loop. Otherwise, uses loop metadata
1202 /// to defer unrolling to the LoopUnrollPass.
1203 void unrollLoopPartial(DebugLoc DL, CanonicalLoopInfo *Loop, int32_t Factor,
1204 CanonicalLoopInfo **UnrolledCLI);
1205
1206 /// Add metadata to simd-ize a loop. If IfCond is not nullptr, the loop
1207 /// is cloned. The metadata which prevents vectorization is added to
1208 /// to the cloned loop. The cloned loop is executed when ifCond is evaluated
1209 /// to false.
1210 ///
1211 /// \param Loop The loop to simd-ize.
1212 /// \param AlignedVars The map which containts pairs of the pointer
1213 /// and its corresponding alignment.
1214 /// \param IfCond The value which corresponds to the if clause
1215 /// condition.
1216 /// \param Order The enum to map order clause.
1217 /// \param Simdlen The Simdlen length to apply to the simd loop.
1218 /// \param Safelen The Safelen length to apply to the simd loop.
1220 MapVector<Value *, Value *> AlignedVars, Value *IfCond,
1221 omp::OrderKind Order, ConstantInt *Simdlen,
1222 ConstantInt *Safelen);
1223
1224 /// Generator for '#omp flush'
1225 ///
1226 /// \param Loc The location where the flush directive was encountered
1227 void createFlush(const LocationDescription &Loc);
1228
1229 /// Generator for '#omp taskwait'
1230 ///
1231 /// \param Loc The location where the taskwait directive was encountered.
1232 void createTaskwait(const LocationDescription &Loc);
1233
1234 /// Generator for '#omp taskyield'
1235 ///
1236 /// \param Loc The location where the taskyield directive was encountered.
1237 void createTaskyield(const LocationDescription &Loc);
1238
1239 /// A struct to pack the relevant information for an OpenMP depend clause.
1240 struct DependData {
1244 explicit DependData() = default;
1246 Value *DepVal)
1248 };
1249
1250 /// Generator for `#omp task`
1251 ///
1252 /// \param Loc The location where the task construct was encountered.
1253 /// \param AllocaIP The insertion point to be used for alloca instructions.
1254 /// \param BodyGenCB Callback that will generate the region code.
1255 /// \param Tied True if the task is tied, false if the task is untied.
1256 /// \param Final i1 value which is `true` if the task is final, `false` if the
1257 /// task is not final.
1258 /// \param IfCondition i1 value. If it evaluates to `false`, an undeferred
1259 /// task is generated, and the encountering thread must
1260 /// suspend the current task region, for which execution
1261 /// cannot be resumed until execution of the structured
1262 /// block that is associated with the generated task is
1263 /// completed.
1264 /// \param EventHandle If present, signifies the event handle as part of
1265 /// the detach clause
1266 /// \param Mergeable If the given task is `mergeable`
1268 createTask(const LocationDescription &Loc, InsertPointTy AllocaIP,
1269 BodyGenCallbackTy BodyGenCB, bool Tied = true,
1270 Value *Final = nullptr, Value *IfCondition = nullptr,
1271 SmallVector<DependData> Dependencies = {}, bool Mergeable = false,
1272 Value *EventHandle = nullptr);
1273
1274 /// Generator for the taskgroup construct
1275 ///
1276 /// \param Loc The location where the taskgroup construct was encountered.
1277 /// \param AllocaIP The insertion point to be used for alloca instructions.
1278 /// \param BodyGenCB Callback that will generate the region code.
1279 InsertPointOrErrorTy createTaskgroup(const LocationDescription &Loc,
1280 InsertPointTy AllocaIP,
1281 BodyGenCallbackTy BodyGenCB);
1282
1284 std::function<std::tuple<std::string, uint64_t>()>;
1285
1286 /// Creates a unique info for a target entry when provided a filename and
1287 /// line number from.
1288 ///
1289 /// \param CallBack A callback function which should return filename the entry
1290 /// resides in as well as the line number for the target entry
1291 /// \param ParentName The name of the parent the target entry resides in, if
1292 /// any.
1295 StringRef ParentName = "");
1296
1297 /// Enum class for the RedctionGen CallBack type to be used.
1299
1300 /// ReductionGen CallBack for Clang
1301 ///
1302 /// \param CodeGenIP InsertPoint for CodeGen.
1303 /// \param Index Index of the ReductionInfo to generate code for.
1304 /// \param LHSPtr Optionally used by Clang to return the LHSPtr it used for
1305 /// codegen, used for fixup later.
1306 /// \param RHSPtr Optionally used by Clang to
1307 /// return the RHSPtr it used for codegen, used for fixup later.
1308 /// \param CurFn Optionally used by Clang to pass in the Current Function as
1309 /// Clang context may be old.
1311 std::function<InsertPointTy(InsertPointTy CodeGenIP, unsigned Index,
1312 Value **LHS, Value **RHS, Function *CurFn)>;
1313
1314 /// ReductionGen CallBack for MLIR
1315 ///
1316 /// \param CodeGenIP InsertPoint for CodeGen.
1317 /// \param LHS Pass in the LHS Value to be used for CodeGen.
1318 /// \param RHS Pass in the RHS Value to be used for CodeGen.
1320 InsertPointTy CodeGenIP, Value *LHS, Value *RHS, Value *&Res)>;
1321
1322 /// Functions used to generate atomic reductions. Such functions take two
1323 /// Values representing pointers to LHS and RHS of the reduction, as well as
1324 /// the element type of these pointers. They are expected to atomically
1325 /// update the LHS to the reduced value.
1327 InsertPointTy, Type *, Value *, Value *)>;
1328
1329 /// Enum class for reduction evaluation types scalar, complex and aggregate.
1331
1332 /// Information about an OpenMP reduction.
1343 : ElementType(nullptr), Variable(nullptr),
1346
1347 /// Reduction element type, must match pointee type of variable.
1349
1350 /// Reduction variable of pointer type.
1352
1353 /// Thread-private partial reduction variable.
1355
1356 /// Reduction evaluation kind - scalar, complex or aggregate.
1358
1359 /// Callback for generating the reduction body. The IR produced by this will
1360 /// be used to combine two values in a thread-safe context, e.g., under
1361 /// lock or within the same thread, and therefore need not be atomic.
1363
1364 /// Clang callback for generating the reduction body. The IR produced by
1365 /// this will be used to combine two values in a thread-safe context, e.g.,
1366 /// under lock or within the same thread, and therefore need not be atomic.
1368
1369 /// Callback for generating the atomic reduction body, may be null. The IR
1370 /// produced by this will be used to atomically combine two values during
1371 /// reduction. If null, the implementation will use the non-atomic version
1372 /// along with the appropriate synchronization mechanisms.
1374 };
1375
1376 enum class CopyAction : unsigned {
1377 // RemoteLaneToThread: Copy over a Reduce list from a remote lane in
1378 // the warp using shuffle instructions.
1380 // ThreadCopy: Make a copy of a Reduce list on the thread's stack.
1381 ThreadCopy,
1382 };
1383
1388 };
1389
1390 /// Supporting functions for Reductions CodeGen.
1391private:
1392 /// Get the id of the current thread on the GPU.
1393 Value *getGPUThreadID();
1394
1395 /// Get the GPU warp size.
1396 Value *getGPUWarpSize();
1397
1398 /// Get the id of the warp in the block.
1399 /// We assume that the warp size is 32, which is always the case
1400 /// on the NVPTX device, to generate more efficient code.
1401 Value *getNVPTXWarpID();
1402
1403 /// Get the id of the current lane in the Warp.
1404 /// We assume that the warp size is 32, which is always the case
1405 /// on the NVPTX device, to generate more efficient code.
1406 Value *getNVPTXLaneID();
1407
1408 /// Cast value to the specified type.
1409 Value *castValueToType(InsertPointTy AllocaIP, Value *From, Type *ToType);
1410
1411 /// This function creates calls to one of two shuffle functions to copy
1412 /// variables between lanes in a warp.
1413 Value *createRuntimeShuffleFunction(InsertPointTy AllocaIP, Value *Element,
1414 Type *ElementType, Value *Offset);
1415
1416 /// Function to shuffle over the value from the remote lane.
1417 void shuffleAndStore(InsertPointTy AllocaIP, Value *SrcAddr, Value *DstAddr,
1418 Type *ElementType, Value *Offset,
1419 Type *ReductionArrayTy);
1420
1421 /// Emit instructions to copy a Reduce list, which contains partially
1422 /// aggregated values, in the specified direction.
1423 void emitReductionListCopy(
1424 InsertPointTy AllocaIP, CopyAction Action, Type *ReductionArrayTy,
1425 ArrayRef<ReductionInfo> ReductionInfos, Value *SrcBase, Value *DestBase,
1426 CopyOptionsTy CopyOptions = {nullptr, nullptr, nullptr});
1427
1428 /// Emit a helper that reduces data across two OpenMP threads (lanes)
1429 /// in the same warp. It uses shuffle instructions to copy over data from
1430 /// a remote lane's stack. The reduction algorithm performed is specified
1431 /// by the fourth parameter.
1432 ///
1433 /// Algorithm Versions.
1434 /// Full Warp Reduce (argument value 0):
1435 /// This algorithm assumes that all 32 lanes are active and gathers
1436 /// data from these 32 lanes, producing a single resultant value.
1437 /// Contiguous Partial Warp Reduce (argument value 1):
1438 /// This algorithm assumes that only a *contiguous* subset of lanes
1439 /// are active. This happens for the last warp in a parallel region
1440 /// when the user specified num_threads is not an integer multiple of
1441 /// 32. This contiguous subset always starts with the zeroth lane.
1442 /// Partial Warp Reduce (argument value 2):
1443 /// This algorithm gathers data from any number of lanes at any position.
1444 /// All reduced values are stored in the lowest possible lane. The set
1445 /// of problems every algorithm addresses is a super set of those
1446 /// addressable by algorithms with a lower version number. Overhead
1447 /// increases as algorithm version increases.
1448 ///
1449 /// Terminology
1450 /// Reduce element:
1451 /// Reduce element refers to the individual data field with primitive
1452 /// data types to be combined and reduced across threads.
1453 /// Reduce list:
1454 /// Reduce list refers to a collection of local, thread-private
1455 /// reduce elements.
1456 /// Remote Reduce list:
1457 /// Remote Reduce list refers to a collection of remote (relative to
1458 /// the current thread) reduce elements.
1459 ///
1460 /// We distinguish between three states of threads that are important to
1461 /// the implementation of this function.
1462 /// Alive threads:
1463 /// Threads in a warp executing the SIMT instruction, as distinguished from
1464 /// threads that are inactive due to divergent control flow.
1465 /// Active threads:
1466 /// The minimal set of threads that has to be alive upon entry to this
1467 /// function. The computation is correct iff active threads are alive.
1468 /// Some threads are alive but they are not active because they do not
1469 /// contribute to the computation in any useful manner. Turning them off
1470 /// may introduce control flow overheads without any tangible benefits.
1471 /// Effective threads:
1472 /// In order to comply with the argument requirements of the shuffle
1473 /// function, we must keep all lanes holding data alive. But at most
1474 /// half of them perform value aggregation; we refer to this half of
1475 /// threads as effective. The other half is simply handing off their
1476 /// data.
1477 ///
1478 /// Procedure
1479 /// Value shuffle:
1480 /// In this step active threads transfer data from higher lane positions
1481 /// in the warp to lower lane positions, creating Remote Reduce list.
1482 /// Value aggregation:
1483 /// In this step, effective threads combine their thread local Reduce list
1484 /// with Remote Reduce list and store the result in the thread local
1485 /// Reduce list.
1486 /// Value copy:
1487 /// In this step, we deal with the assumption made by algorithm 2
1488 /// (i.e. contiguity assumption). When we have an odd number of lanes
1489 /// active, say 2k+1, only k threads will be effective and therefore k
1490 /// new values will be produced. However, the Reduce list owned by the
1491 /// (2k+1)th thread is ignored in the value aggregation. Therefore
1492 /// we copy the Reduce list from the (2k+1)th lane to (k+1)th lane so
1493 /// that the contiguity assumption still holds.
1494 ///
1495 /// \param ReductionInfos Array type containing the ReductionOps.
1496 /// \param ReduceFn The reduction function.
1497 /// \param FuncAttrs Optional param to specify any function attributes that
1498 /// need to be copied to the new function.
1499 ///
1500 /// \return The ShuffleAndReduce function.
1501 Function *emitShuffleAndReduceFunction(
1503 Function *ReduceFn, AttributeList FuncAttrs);
1504
1505 /// This function emits a helper that gathers Reduce lists from the first
1506 /// lane of every active warp to lanes in the first warp.
1507 ///
1508 /// void inter_warp_copy_func(void* reduce_data, num_warps)
1509 /// shared smem[warp_size];
1510 /// For all data entries D in reduce_data:
1511 /// sync
1512 /// If (I am the first lane in each warp)
1513 /// Copy my local D to smem[warp_id]
1514 /// sync
1515 /// if (I am the first warp)
1516 /// Copy smem[thread_id] to my local D
1517 ///
1518 /// \param Loc The insert and source location description.
1519 /// \param ReductionInfos Array type containing the ReductionOps.
1520 /// \param FuncAttrs Optional param to specify any function attributes that
1521 /// need to be copied to the new function.
1522 ///
1523 /// \return The InterWarpCopy function.
1525 emitInterWarpCopyFunction(const LocationDescription &Loc,
1526 ArrayRef<ReductionInfo> ReductionInfos,
1527 AttributeList FuncAttrs);
1528
1529 /// This function emits a helper that copies all the reduction variables from
1530 /// the team into the provided global buffer for the reduction variables.
1531 ///
1532 /// void list_to_global_copy_func(void *buffer, int Idx, void *reduce_data)
1533 /// For all data entries D in reduce_data:
1534 /// Copy local D to buffer.D[Idx]
1535 ///
1536 /// \param ReductionInfos Array type containing the ReductionOps.
1537 /// \param ReductionsBufferTy The StructTy for the reductions buffer.
1538 /// \param FuncAttrs Optional param to specify any function attributes that
1539 /// need to be copied to the new function.
1540 ///
1541 /// \return The ListToGlobalCopy function.
1542 Function *emitListToGlobalCopyFunction(ArrayRef<ReductionInfo> ReductionInfos,
1543 Type *ReductionsBufferTy,
1544 AttributeList FuncAttrs);
1545
1546 /// This function emits a helper that copies all the reduction variables from
1547 /// the team into the provided global buffer for the reduction variables.
1548 ///
1549 /// void list_to_global_copy_func(void *buffer, int Idx, void *reduce_data)
1550 /// For all data entries D in reduce_data:
1551 /// Copy buffer.D[Idx] to local D;
1552 ///
1553 /// \param ReductionInfos Array type containing the ReductionOps.
1554 /// \param ReductionsBufferTy The StructTy for the reductions buffer.
1555 /// \param FuncAttrs Optional param to specify any function attributes that
1556 /// need to be copied to the new function.
1557 ///
1558 /// \return The GlobalToList function.
1559 Function *emitGlobalToListCopyFunction(ArrayRef<ReductionInfo> ReductionInfos,
1560 Type *ReductionsBufferTy,
1561 AttributeList FuncAttrs);
1562
1563 /// This function emits a helper that reduces all the reduction variables from
1564 /// the team into the provided global buffer for the reduction variables.
1565 ///
1566 /// void list_to_global_reduce_func(void *buffer, int Idx, void *reduce_data)
1567 /// void *GlobPtrs[];
1568 /// GlobPtrs[0] = (void*)&buffer.D0[Idx];
1569 /// ...
1570 /// GlobPtrs[N] = (void*)&buffer.DN[Idx];
1571 /// reduce_function(GlobPtrs, reduce_data);
1572 ///
1573 /// \param ReductionInfos Array type containing the ReductionOps.
1574 /// \param ReduceFn The reduction function.
1575 /// \param ReductionsBufferTy The StructTy for the reductions buffer.
1576 /// \param FuncAttrs Optional param to specify any function attributes that
1577 /// need to be copied to the new function.
1578 ///
1579 /// \return The ListToGlobalReduce function.
1580 Function *
1581 emitListToGlobalReduceFunction(ArrayRef<ReductionInfo> ReductionInfos,
1582 Function *ReduceFn, Type *ReductionsBufferTy,
1583 AttributeList FuncAttrs);
1584
1585 /// This function emits a helper that reduces all the reduction variables from
1586 /// the team into the provided global buffer for the reduction variables.
1587 ///
1588 /// void global_to_list_reduce_func(void *buffer, int Idx, void *reduce_data)
1589 /// void *GlobPtrs[];
1590 /// GlobPtrs[0] = (void*)&buffer.D0[Idx];
1591 /// ...
1592 /// GlobPtrs[N] = (void*)&buffer.DN[Idx];
1593 /// reduce_function(reduce_data, GlobPtrs);
1594 ///
1595 /// \param ReductionInfos Array type containing the ReductionOps.
1596 /// \param ReduceFn The reduction function.
1597 /// \param ReductionsBufferTy The StructTy for the reductions buffer.
1598 /// \param FuncAttrs Optional param to specify any function attributes that
1599 /// need to be copied to the new function.
1600 ///
1601 /// \return The GlobalToListReduce function.
1602 Function *
1603 emitGlobalToListReduceFunction(ArrayRef<ReductionInfo> ReductionInfos,
1604 Function *ReduceFn, Type *ReductionsBufferTy,
1605 AttributeList FuncAttrs);
1606
1607 /// Get the function name of a reduction function.
1608 std::string getReductionFuncName(StringRef Name) const;
1609
1610 /// Emits reduction function.
1611 /// \param ReducerName Name of the function calling the reduction.
1612 /// \param ReductionInfos Array type containing the ReductionOps.
1613 /// \param ReductionGenCBKind Optional param to specify Clang or MLIR
1614 /// CodeGenCB kind.
1615 /// \param FuncAttrs Optional param to specify any function attributes that
1616 /// need to be copied to the new function.
1617 ///
1618 /// \return The reduction function.
1619 Expected<Function *> createReductionFunction(
1620 StringRef ReducerName, ArrayRef<ReductionInfo> ReductionInfos,
1622 AttributeList FuncAttrs = {});
1623
1624public:
1625 ///
1626 /// Design of OpenMP reductions on the GPU
1627 ///
1628 /// Consider a typical OpenMP program with one or more reduction
1629 /// clauses:
1630 ///
1631 /// float foo;
1632 /// double bar;
1633 /// #pragma omp target teams distribute parallel for \
1634 /// reduction(+:foo) reduction(*:bar)
1635 /// for (int i = 0; i < N; i++) {
1636 /// foo += A[i]; bar *= B[i];
1637 /// }
1638 ///
1639 /// where 'foo' and 'bar' are reduced across all OpenMP threads in
1640 /// all teams. In our OpenMP implementation on the NVPTX device an
1641 /// OpenMP team is mapped to a CUDA threadblock and OpenMP threads
1642 /// within a team are mapped to CUDA threads within a threadblock.
1643 /// Our goal is to efficiently aggregate values across all OpenMP
1644 /// threads such that:
1645 ///
1646 /// - the compiler and runtime are logically concise, and
1647 /// - the reduction is performed efficiently in a hierarchical
1648 /// manner as follows: within OpenMP threads in the same warp,
1649 /// across warps in a threadblock, and finally across teams on
1650 /// the NVPTX device.
1651 ///
1652 /// Introduction to Decoupling
1653 ///
1654 /// We would like to decouple the compiler and the runtime so that the
1655 /// latter is ignorant of the reduction variables (number, data types)
1656 /// and the reduction operators. This allows a simpler interface
1657 /// and implementation while still attaining good performance.
1658 ///
1659 /// Pseudocode for the aforementioned OpenMP program generated by the
1660 /// compiler is as follows:
1661 ///
1662 /// 1. Create private copies of reduction variables on each OpenMP
1663 /// thread: 'foo_private', 'bar_private'
1664 /// 2. Each OpenMP thread reduces the chunk of 'A' and 'B' assigned
1665 /// to it and writes the result in 'foo_private' and 'bar_private'
1666 /// respectively.
1667 /// 3. Call the OpenMP runtime on the GPU to reduce within a team
1668 /// and store the result on the team master:
1669 ///
1670 /// __kmpc_nvptx_parallel_reduce_nowait_v2(...,
1671 /// reduceData, shuffleReduceFn, interWarpCpyFn)
1672 ///
1673 /// where:
1674 /// struct ReduceData {
1675 /// double *foo;
1676 /// double *bar;
1677 /// } reduceData
1678 /// reduceData.foo = &foo_private
1679 /// reduceData.bar = &bar_private
1680 ///
1681 /// 'shuffleReduceFn' and 'interWarpCpyFn' are pointers to two
1682 /// auxiliary functions generated by the compiler that operate on
1683 /// variables of type 'ReduceData'. They aid the runtime perform
1684 /// algorithmic steps in a data agnostic manner.
1685 ///
1686 /// 'shuffleReduceFn' is a pointer to a function that reduces data
1687 /// of type 'ReduceData' across two OpenMP threads (lanes) in the
1688 /// same warp. It takes the following arguments as input:
1689 ///
1690 /// a. variable of type 'ReduceData' on the calling lane,
1691 /// b. its lane_id,
1692 /// c. an offset relative to the current lane_id to generate a
1693 /// remote_lane_id. The remote lane contains the second
1694 /// variable of type 'ReduceData' that is to be reduced.
1695 /// d. an algorithm version parameter determining which reduction
1696 /// algorithm to use.
1697 ///
1698 /// 'shuffleReduceFn' retrieves data from the remote lane using
1699 /// efficient GPU shuffle intrinsics and reduces, using the
1700 /// algorithm specified by the 4th parameter, the two operands
1701 /// element-wise. The result is written to the first operand.
1702 ///
1703 /// Different reduction algorithms are implemented in different
1704 /// runtime functions, all calling 'shuffleReduceFn' to perform
1705 /// the essential reduction step. Therefore, based on the 4th
1706 /// parameter, this function behaves slightly differently to
1707 /// cooperate with the runtime to ensure correctness under
1708 /// different circumstances.
1709 ///
1710 /// 'InterWarpCpyFn' is a pointer to a function that transfers
1711 /// reduced variables across warps. It tunnels, through CUDA
1712 /// shared memory, the thread-private data of type 'ReduceData'
1713 /// from lane 0 of each warp to a lane in the first warp.
1714 /// 4. Call the OpenMP runtime on the GPU to reduce across teams.
1715 /// The last team writes the global reduced value to memory.
1716 ///
1717 /// ret = __kmpc_nvptx_teams_reduce_nowait(...,
1718 /// reduceData, shuffleReduceFn, interWarpCpyFn,
1719 /// scratchpadCopyFn, loadAndReduceFn)
1720 ///
1721 /// 'scratchpadCopyFn' is a helper that stores reduced
1722 /// data from the team master to a scratchpad array in
1723 /// global memory.
1724 ///
1725 /// 'loadAndReduceFn' is a helper that loads data from
1726 /// the scratchpad array and reduces it with the input
1727 /// operand.
1728 ///
1729 /// These compiler generated functions hide address
1730 /// calculation and alignment information from the runtime.
1731 /// 5. if ret == 1:
1732 /// The team master of the last team stores the reduced
1733 /// result to the globals in memory.
1734 /// foo += reduceData.foo; bar *= reduceData.bar
1735 ///
1736 ///
1737 /// Warp Reduction Algorithms
1738 ///
1739 /// On the warp level, we have three algorithms implemented in the
1740 /// OpenMP runtime depending on the number of active lanes:
1741 ///
1742 /// Full Warp Reduction
1743 ///
1744 /// The reduce algorithm within a warp where all lanes are active
1745 /// is implemented in the runtime as follows:
1746 ///
1747 /// full_warp_reduce(void *reduce_data,
1748 /// kmp_ShuffleReductFctPtr ShuffleReduceFn) {
1749 /// for (int offset = WARPSIZE/2; offset > 0; offset /= 2)
1750 /// ShuffleReduceFn(reduce_data, 0, offset, 0);
1751 /// }
1752 ///
1753 /// The algorithm completes in log(2, WARPSIZE) steps.
1754 ///
1755 /// 'ShuffleReduceFn' is used here with lane_id set to 0 because it is
1756 /// not used therefore we save instructions by not retrieving lane_id
1757 /// from the corresponding special registers. The 4th parameter, which
1758 /// represents the version of the algorithm being used, is set to 0 to
1759 /// signify full warp reduction.
1760 ///
1761 /// In this version, 'ShuffleReduceFn' behaves, per element, as follows:
1762 ///
1763 /// #reduce_elem refers to an element in the local lane's data structure
1764 /// #remote_elem is retrieved from a remote lane
1765 /// remote_elem = shuffle_down(reduce_elem, offset, WARPSIZE);
1766 /// reduce_elem = reduce_elem REDUCE_OP remote_elem;
1767 ///
1768 /// Contiguous Partial Warp Reduction
1769 ///
1770 /// This reduce algorithm is used within a warp where only the first
1771 /// 'n' (n <= WARPSIZE) lanes are active. It is typically used when the
1772 /// number of OpenMP threads in a parallel region is not a multiple of
1773 /// WARPSIZE. The algorithm is implemented in the runtime as follows:
1774 ///
1775 /// void
1776 /// contiguous_partial_reduce(void *reduce_data,
1777 /// kmp_ShuffleReductFctPtr ShuffleReduceFn,
1778 /// int size, int lane_id) {
1779 /// int curr_size;
1780 /// int offset;
1781 /// curr_size = size;
1782 /// mask = curr_size/2;
1783 /// while (offset>0) {
1784 /// ShuffleReduceFn(reduce_data, lane_id, offset, 1);
1785 /// curr_size = (curr_size+1)/2;
1786 /// offset = curr_size/2;
1787 /// }
1788 /// }
1789 ///
1790 /// In this version, 'ShuffleReduceFn' behaves, per element, as follows:
1791 ///
1792 /// remote_elem = shuffle_down(reduce_elem, offset, WARPSIZE);
1793 /// if (lane_id < offset)
1794 /// reduce_elem = reduce_elem REDUCE_OP remote_elem
1795 /// else
1796 /// reduce_elem = remote_elem
1797 ///
1798 /// This algorithm assumes that the data to be reduced are located in a
1799 /// contiguous subset of lanes starting from the first. When there is
1800 /// an odd number of active lanes, the data in the last lane is not
1801 /// aggregated with any other lane's dat but is instead copied over.
1802 ///
1803 /// Dispersed Partial Warp Reduction
1804 ///
1805 /// This algorithm is used within a warp when any discontiguous subset of
1806 /// lanes are active. It is used to implement the reduction operation
1807 /// across lanes in an OpenMP simd region or in a nested parallel region.
1808 ///
1809 /// void
1810 /// dispersed_partial_reduce(void *reduce_data,
1811 /// kmp_ShuffleReductFctPtr ShuffleReduceFn) {
1812 /// int size, remote_id;
1813 /// int logical_lane_id = number_of_active_lanes_before_me() * 2;
1814 /// do {
1815 /// remote_id = next_active_lane_id_right_after_me();
1816 /// # the above function returns 0 of no active lane
1817 /// # is present right after the current lane.
1818 /// size = number_of_active_lanes_in_this_warp();
1819 /// logical_lane_id /= 2;
1820 /// ShuffleReduceFn(reduce_data, logical_lane_id,
1821 /// remote_id-1-threadIdx.x, 2);
1822 /// } while (logical_lane_id % 2 == 0 && size > 1);
1823 /// }
1824 ///
1825 /// There is no assumption made about the initial state of the reduction.
1826 /// Any number of lanes (>=1) could be active at any position. The reduction
1827 /// result is returned in the first active lane.
1828 ///
1829 /// In this version, 'ShuffleReduceFn' behaves, per element, as follows:
1830 ///
1831 /// remote_elem = shuffle_down(reduce_elem, offset, WARPSIZE);
1832 /// if (lane_id % 2 == 0 && offset > 0)
1833 /// reduce_elem = reduce_elem REDUCE_OP remote_elem
1834 /// else
1835 /// reduce_elem = remote_elem
1836 ///
1837 ///
1838 /// Intra-Team Reduction
1839 ///
1840 /// This function, as implemented in the runtime call
1841 /// '__kmpc_nvptx_parallel_reduce_nowait_v2', aggregates data across OpenMP
1842 /// threads in a team. It first reduces within a warp using the
1843 /// aforementioned algorithms. We then proceed to gather all such
1844 /// reduced values at the first warp.
1845 ///
1846 /// The runtime makes use of the function 'InterWarpCpyFn', which copies
1847 /// data from each of the "warp master" (zeroth lane of each warp, where
1848 /// warp-reduced data is held) to the zeroth warp. This step reduces (in
1849 /// a mathematical sense) the problem of reduction across warp masters in
1850 /// a block to the problem of warp reduction.
1851 ///
1852 ///
1853 /// Inter-Team Reduction
1854 ///
1855 /// Once a team has reduced its data to a single value, it is stored in
1856 /// a global scratchpad array. Since each team has a distinct slot, this
1857 /// can be done without locking.
1858 ///
1859 /// The last team to write to the scratchpad array proceeds to reduce the
1860 /// scratchpad array. One or more workers in the last team use the helper
1861 /// 'loadAndReduceDataFn' to load and reduce values from the array, i.e.,
1862 /// the k'th worker reduces every k'th element.
1863 ///
1864 /// Finally, a call is made to '__kmpc_nvptx_parallel_reduce_nowait_v2' to
1865 /// reduce across workers and compute a globally reduced value.
1866 ///
1867 /// \param Loc The location where the reduction was
1868 /// encountered. Must be within the associate
1869 /// directive and after the last local access to the
1870 /// reduction variables.
1871 /// \param AllocaIP An insertion point suitable for allocas usable
1872 /// in reductions.
1873 /// \param CodeGenIP An insertion point suitable for code
1874 /// generation. \param ReductionInfos A list of info on each reduction
1875 /// variable. \param IsNoWait Optional flag set if the reduction is
1876 /// marked as
1877 /// nowait.
1878 /// \param IsTeamsReduction Optional flag set if it is a teams
1879 /// reduction.
1880 /// \param HasDistribute Optional flag set if it is a
1881 /// distribute reduction.
1882 /// \param GridValue Optional GPU grid value.
1883 /// \param ReductionBufNum Optional OpenMPCUDAReductionBufNumValue to be
1884 /// used for teams reduction.
1885 /// \param SrcLocInfo Source location information global.
1887 const LocationDescription &Loc, InsertPointTy AllocaIP,
1888 InsertPointTy CodeGenIP, ArrayRef<ReductionInfo> ReductionInfos,
1889 bool IsNoWait = false, bool IsTeamsReduction = false,
1890 bool HasDistribute = false,
1892 std::optional<omp::GV> GridValue = {}, unsigned ReductionBufNum = 1024,
1893 Value *SrcLocInfo = nullptr);
1894
1895 // TODO: provide atomic and non-atomic reduction generators for reduction
1896 // operators defined by the OpenMP specification.
1897
1898 /// Generator for '#omp reduction'.
1899 ///
1900 /// Emits the IR instructing the runtime to perform the specific kind of
1901 /// reductions. Expects reduction variables to have been privatized and
1902 /// initialized to reduction-neutral values separately. Emits the calls to
1903 /// runtime functions as well as the reduction function and the basic blocks
1904 /// performing the reduction atomically and non-atomically.
1905 ///
1906 /// The code emitted for the following:
1907 ///
1908 /// \code
1909 /// type var_1;
1910 /// type var_2;
1911 /// #pragma omp <directive> reduction(reduction-op:var_1,var_2)
1912 /// /* body */;
1913 /// \endcode
1914 ///
1915 /// corresponds to the following sketch.
1916 ///
1917 /// \code
1918 /// void _outlined_par() {
1919 /// // N is the number of different reductions.
1920 /// void *red_array[] = {privatized_var_1, privatized_var_2, ...};
1921 /// switch(__kmpc_reduce(..., N, /*size of data in red array*/, red_array,
1922 /// _omp_reduction_func,
1923 /// _gomp_critical_user.reduction.var)) {
1924 /// case 1: {
1925 /// var_1 = var_1 <reduction-op> privatized_var_1;
1926 /// var_2 = var_2 <reduction-op> privatized_var_2;
1927 /// // ...
1928 /// __kmpc_end_reduce(...);
1929 /// break;
1930 /// }
1931 /// case 2: {
1932 /// _Atomic<ReductionOp>(var_1, privatized_var_1);
1933 /// _Atomic<ReductionOp>(var_2, privatized_var_2);
1934 /// // ...
1935 /// break;
1936 /// }
1937 /// default: break;
1938 /// }
1939 /// }
1940 ///
1941 /// void _omp_reduction_func(void **lhs, void **rhs) {
1942 /// *(type *)lhs[0] = *(type *)lhs[0] <reduction-op> *(type *)rhs[0];
1943 /// *(type *)lhs[1] = *(type *)lhs[1] <reduction-op> *(type *)rhs[1];
1944 /// // ...
1945 /// }
1946 /// \endcode
1947 ///
1948 /// \param Loc The location where the reduction was
1949 /// encountered. Must be within the associate
1950 /// directive and after the last local access to the
1951 /// reduction variables.
1952 /// \param AllocaIP An insertion point suitable for allocas usable
1953 /// in reductions.
1954 /// \param ReductionInfos A list of info on each reduction variable.
1955 /// \param IsNoWait A flag set if the reduction is marked as nowait.
1956 /// \param IsByRef A flag set if the reduction is using reference
1957 /// or direct value.
1958 InsertPointOrErrorTy createReductions(const LocationDescription &Loc,
1959 InsertPointTy AllocaIP,
1960 ArrayRef<ReductionInfo> ReductionInfos,
1961 ArrayRef<bool> IsByRef,
1962 bool IsNoWait = false);
1963
1964 ///}
1965
1966 /// Return the insertion point used by the underlying IRBuilder.
1968
1969 /// Update the internal location to \p Loc.
1971 Builder.restoreIP(Loc.IP);
1973 return Loc.IP.getBlock() != nullptr;
1974 }
1975
1976 /// Return the function declaration for the runtime function with \p FnID.
1979
1981
1982 /// Return the (LLVM-IR) string describing the source location \p LocStr.
1983 Constant *getOrCreateSrcLocStr(StringRef LocStr, uint32_t &SrcLocStrSize);
1984
1985 /// Return the (LLVM-IR) string describing the default source location.
1987
1988 /// Return the (LLVM-IR) string describing the source location identified by
1989 /// the arguments.
1990 Constant *getOrCreateSrcLocStr(StringRef FunctionName, StringRef FileName,
1991 unsigned Line, unsigned Column,
1992 uint32_t &SrcLocStrSize);
1993
1994 /// Return the (LLVM-IR) string describing the DebugLoc \p DL. Use \p F as
1995 /// fallback if \p DL does not specify the function name.
1997 Function *F = nullptr);
1998
1999 /// Return the (LLVM-IR) string describing the source location \p Loc.
2000 Constant *getOrCreateSrcLocStr(const LocationDescription &Loc,
2001 uint32_t &SrcLocStrSize);
2002
2003 /// Return an ident_t* encoding the source location \p SrcLocStr and \p Flags.
2004 /// TODO: Create a enum class for the Reserve2Flags
2005 Constant *getOrCreateIdent(Constant *SrcLocStr, uint32_t SrcLocStrSize,
2006 omp::IdentFlag Flags = omp::IdentFlag(0),
2007 unsigned Reserve2Flags = 0);
2008
2009 /// Create a hidden global flag \p Name in the module with initial value \p
2010 /// Value.
2012
2013 /// Emit the llvm.used metadata.
2015
2016 /// Emit the kernel execution mode.
2019
2020 /// Generate control flow and cleanup for cancellation.
2021 ///
2022 /// \param CancelFlag Flag indicating if the cancellation is performed.
2023 /// \param CanceledDirective The kind of directive that is cancled.
2024 /// \param ExitCB Extra code to be generated in the exit block.
2025 ///
2026 /// \return an error, if any were triggered during execution.
2028 omp::Directive CanceledDirective,
2029 FinalizeCallbackTy ExitCB = {});
2030
2031 /// Generate a target region entry call.
2032 ///
2033 /// \param Loc The location at which the request originated and is fulfilled.
2034 /// \param AllocaIP The insertion point to be used for alloca instructions.
2035 /// \param Return Return value of the created function returned by reference.
2036 /// \param DeviceID Identifier for the device via the 'device' clause.
2037 /// \param NumTeams Numer of teams for the region via the 'num_teams' clause
2038 /// or 0 if unspecified and -1 if there is no 'teams' clause.
2039 /// \param NumThreads Number of threads via the 'thread_limit' clause.
2040 /// \param HostPtr Pointer to the host-side pointer of the target kernel.
2041 /// \param KernelArgs Array of arguments to the kernel.
2042 InsertPointTy emitTargetKernel(const LocationDescription &Loc,
2043 InsertPointTy AllocaIP, Value *&Return,
2044 Value *Ident, Value *DeviceID, Value *NumTeams,
2045 Value *NumThreads, Value *HostPtr,
2046 ArrayRef<Value *> KernelArgs);
2047
2048 /// Generate a flush runtime call.
2049 ///
2050 /// \param Loc The location at which the request originated and is fulfilled.
2051 void emitFlush(const LocationDescription &Loc);
2052
2053 /// The finalization stack made up of finalize callbacks currently in-flight,
2054 /// wrapped into FinalizationInfo objects that reference also the finalization
2055 /// target block and the kind of cancellable directive.
2057
2058 /// Return true if the last entry in the finalization stack is of kind \p DK
2059 /// and cancellable.
2060 bool isLastFinalizationInfoCancellable(omp::Directive DK) {
2061 return !FinalizationStack.empty() &&
2062 FinalizationStack.back().IsCancellable &&
2063 FinalizationStack.back().DK == DK;
2064 }
2065
2066 /// Generate a taskwait runtime call.
2067 ///
2068 /// \param Loc The location at which the request originated and is fulfilled.
2069 void emitTaskwaitImpl(const LocationDescription &Loc);
2070
2071 /// Generate a taskyield runtime call.
2072 ///
2073 /// \param Loc The location at which the request originated and is fulfilled.
2074 void emitTaskyieldImpl(const LocationDescription &Loc);
2075
2076 /// Return the current thread ID.
2077 ///
2078 /// \param Ident The ident (ident_t*) describing the query origin.
2080
2081 /// The OpenMPIRBuilder Configuration
2083
2084 /// The underlying LLVM-IR module
2086
2087 /// The LLVM-IR Builder used to create IR.
2089
2090 /// Map to remember source location strings
2092
2093 /// Map to remember existing ident_t*.
2095
2096 /// Info manager to keep track of target regions.
2098
2099 /// The target triple of the underlying module.
2100 const Triple T;
2101
2102 /// Helper that contains information about regions we need to outline
2103 /// during finalization.
2105 using PostOutlineCBTy = std::function<void(Function &)>;
2109
2110 /// Collect all blocks in between EntryBB and ExitBB in both the given
2111 /// vector and set.
2113 SmallVectorImpl<BasicBlock *> &BlockVector);
2114
2115 /// Return the function that contains the region to be outlined.
2116 Function *getFunction() const { return EntryBB->getParent(); }
2117 };
2118
2119 /// Collection of regions that need to be outlined during finalization.
2121
2122 /// A collection of candidate target functions that's constant allocas will
2123 /// attempt to be raised on a call of finalize after all currently enqueued
2124 /// outline info's have been processed.
2126
2127 /// Collection of owned canonical loop objects that eventually need to be
2128 /// free'd.
2129 std::forward_list<CanonicalLoopInfo> LoopInfos;
2130
2131 /// Add a new region that will be outlined later.
2132 void addOutlineInfo(OutlineInfo &&OI) { OutlineInfos.emplace_back(OI); }
2133
2134 /// An ordered map of auto-generated variables to their unique names.
2135 /// It stores variables with the following names: 1) ".gomp_critical_user_" +
2136 /// <critical_section_name> + ".var" for "omp critical" directives; 2)
2137 /// <mangled_name_for_global_var> + ".cache." for cache for threadprivate
2138 /// variables.
2140
2141 /// Computes the size of type in bytes.
2142 Value *getSizeInBytes(Value *BasePtr);
2143
2144 // Emit a branch from the current block to the Target block only if
2145 // the current block has a terminator.
2147
2148 // If BB has no use then delete it and return. Else place BB after the current
2149 // block, if possible, or else at the end of the function. Also add a branch
2150 // from current block to BB if current block does not have a terminator.
2151 void emitBlock(BasicBlock *BB, Function *CurFn, bool IsFinished = false);
2152
2153 /// Emits code for OpenMP 'if' clause using specified \a BodyGenCallbackTy
2154 /// Here is the logic:
2155 /// if (Cond) {
2156 /// ThenGen();
2157 /// } else {
2158 /// ElseGen();
2159 /// }
2160 ///
2161 /// \return an error, if any were triggered during execution.
2163 BodyGenCallbackTy ElseGen, InsertPointTy AllocaIP = {});
2164
2165 /// Create the global variable holding the offload mappings information.
2167 std::string VarName);
2168
2169 /// Create the global variable holding the offload names information.
2172 std::string VarName);
2173
2176 AllocaInst *Args = nullptr;
2178 };
2179
2180 /// Create the allocas instruction used in call to mapper functions.
2182 InsertPointTy AllocaIP, unsigned NumOperands,
2184
2185 /// Create the call for the target mapper function.
2186 /// \param Loc The source location description.
2187 /// \param MapperFunc Function to be called.
2188 /// \param SrcLocInfo Source location information global.
2189 /// \param MaptypesArg The argument types.
2190 /// \param MapnamesArg The argument names.
2191 /// \param MapperAllocas The AllocaInst used for the call.
2192 /// \param DeviceID Device ID for the call.
2193 /// \param NumOperands Number of operands in the call.
2194 void emitMapperCall(const LocationDescription &Loc, Function *MapperFunc,
2195 Value *SrcLocInfo, Value *MaptypesArg, Value *MapnamesArg,
2196 struct MapperAllocas &MapperAllocas, int64_t DeviceID,
2197 unsigned NumOperands);
2198
2199 /// Container for the arguments used to pass data to the runtime library.
2201 /// The array of base pointer passed to the runtime library.
2203 /// The array of section pointers passed to the runtime library.
2205 /// The array of sizes passed to the runtime library.
2206 Value *SizesArray = nullptr;
2207 /// The array of map types passed to the runtime library for the beginning
2208 /// of the region or for the entire region if there are no separate map
2209 /// types for the region end.
2211 /// The array of map types passed to the runtime library for the end of the
2212 /// region, or nullptr if there are no separate map types for the region
2213 /// end.
2215 /// The array of user-defined mappers passed to the runtime library.
2217 /// The array of original declaration names of mapped pointers sent to the
2218 /// runtime library for debugging
2220
2221 explicit TargetDataRTArgs() {}
2230 };
2231
2232 /// Container to pass the default attributes with which a kernel must be
2233 /// launched, used to set kernel attributes and populate associated static
2234 /// structures.
2235 ///
2236 /// For max values, < 0 means unset, == 0 means set but unknown at compile
2237 /// time. The number of max values will be 1 except for the case where
2238 /// ompx_bare is set.
2243 int32_t MinTeams = 1;
2245 int32_t MinThreads = 1;
2246 };
2247
2248 /// Container to pass LLVM IR runtime values or constants related to the
2249 /// number of teams and threads with which the kernel must be launched, as
2250 /// well as the trip count of the loop, if it is an SPMD or Generic-SPMD
2251 /// kernel. These must be defined in the host prior to the call to the kernel
2252 /// launch OpenMP RTL function.
2255 Value *MinTeams = nullptr;
2258
2259 /// 'parallel' construct 'num_threads' clause value, if present and it is an
2260 /// SPMD kernel.
2261 Value *MaxThreads = nullptr;
2262
2263 /// Total number of iterations of the SPMD or Generic-SPMD kernel or null if
2264 /// it is a generic kernel.
2266 };
2267
2268 /// Data structure that contains the needed information to construct the
2269 /// kernel args vector.
2271 /// Number of arguments passed to the runtime library.
2272 unsigned NumTargetItems = 0;
2273 /// Arguments passed to the runtime library
2275 /// The number of iterations
2277 /// The number of teams.
2279 /// The number of threads.
2281 /// The size of the dynamic shared memory.
2283 /// True if the kernel has 'no wait' clause.
2284 bool HasNoWait = false;
2285
2286 // Constructors for TargetKernelArgs.
2291 bool HasNoWait)
2296 };
2297
2298 /// Create the kernel args vector used by emitTargetKernel. This function
2299 /// creates various constant values that are used in the resulting args
2300 /// vector.
2301 static void getKernelArgsVector(TargetKernelArgs &KernelArgs,
2303 SmallVector<Value *> &ArgsVector);
2304
2305 /// Struct that keeps the information that should be kept throughout
2306 /// a 'target data' region.
2308 /// Set to true if device pointer information have to be obtained.
2309 bool RequiresDevicePointerInfo = false;
2310 /// Set to true if Clang emits separate runtime calls for the beginning and
2311 /// end of the region. These calls might have separate map type arrays.
2312 bool SeparateBeginEndCalls = false;
2313
2314 public:
2316
2319
2320 /// Indicate whether any user-defined mapper exists.
2321 bool HasMapper = false;
2322 /// The total number of pointers passed to the runtime library.
2323 unsigned NumberOfPtrs = 0u;
2324
2325 bool EmitDebug = false;
2326
2327 /// Whether the `target ... data` directive has a `nowait` clause.
2328 bool HasNoWait = false;
2329
2330 explicit TargetDataInfo() {}
2331 explicit TargetDataInfo(bool RequiresDevicePointerInfo,
2332 bool SeparateBeginEndCalls)
2333 : RequiresDevicePointerInfo(RequiresDevicePointerInfo),
2334 SeparateBeginEndCalls(SeparateBeginEndCalls) {}
2335 /// Clear information about the data arrays.
2338 HasMapper = false;
2339 NumberOfPtrs = 0u;
2340 }
2341 /// Return true if the current target data information has valid arrays.
2342 bool isValid() {
2346 }
2347 bool requiresDevicePointerInfo() { return RequiresDevicePointerInfo; }
2348 bool separateBeginEndCalls() { return SeparateBeginEndCalls; }
2349 };
2350
2358
2359 /// This structure contains combined information generated for mappable
2360 /// clauses, including base pointers, pointers, sizes, map types, user-defined
2361 /// mappers, and non-contiguous information.
2362 struct MapInfosTy {
2364 bool IsNonContiguous = false;
2369 };
2377
2378 /// Append arrays in \a CurInfo.
2379 void append(MapInfosTy &CurInfo) {
2381 CurInfo.BasePointers.end());
2382 Pointers.append(CurInfo.Pointers.begin(), CurInfo.Pointers.end());
2384 CurInfo.DevicePointers.end());
2385 Sizes.append(CurInfo.Sizes.begin(), CurInfo.Sizes.end());
2386 Types.append(CurInfo.Types.begin(), CurInfo.Types.end());
2387 Names.append(CurInfo.Names.begin(), CurInfo.Names.end());
2389 CurInfo.NonContigInfo.Dims.end());
2391 CurInfo.NonContigInfo.Offsets.end());
2393 CurInfo.NonContigInfo.Counts.end());
2395 CurInfo.NonContigInfo.Strides.end());
2396 }
2397 };
2398
2399 /// Callback function type for functions emitting the host fallback code that
2400 /// is executed when the kernel launch fails. It takes an insertion point as
2401 /// parameter where the code should be emitted. It returns an insertion point
2402 /// that points right after after the emitted code.
2405
2406 /// Generate a target region entry call and host fallback call.
2407 ///
2408 /// \param Loc The location at which the request originated and is fulfilled.
2409 /// \param OutlinedFnID The ooulined function ID.
2410 /// \param EmitTargetCallFallbackCB Call back function to generate host
2411 /// fallback code.
2412 /// \param Args Data structure holding information about the kernel arguments.
2413 /// \param DeviceID Identifier for the device via the 'device' clause.
2414 /// \param RTLoc Source location identifier
2415 /// \param AllocaIP The insertion point to be used for alloca instructions.
2417 emitKernelLaunch(const LocationDescription &Loc, Value *OutlinedFnID,
2418 EmitFallbackCallbackTy EmitTargetCallFallbackCB,
2419 TargetKernelArgs &Args, Value *DeviceID, Value *RTLoc,
2420 InsertPointTy AllocaIP);
2421
2422 /// Callback type for generating the bodies of device directives that require
2423 /// outer target tasks (e.g. in case of having `nowait` or `depend` clauses).
2424 ///
2425 /// \param DeviceID The ID of the device on which the target region will
2426 /// execute.
2427 /// \param RTLoc Source location identifier
2428 /// \Param TargetTaskAllocaIP Insertion point for the alloca block of the
2429 /// generated task.
2430 ///
2431 /// \return an error, if any were triggered during execution.
2433 function_ref<Error(Value *DeviceID, Value *RTLoc,
2434 IRBuilderBase::InsertPoint TargetTaskAllocaIP)>;
2435
2436 /// Generate a target-task for the target construct
2437 ///
2438 /// \param TaskBodyCB Callback to generate the actual body of the target task.
2439 /// \param DeviceID Identifier for the device via the 'device' clause.
2440 /// \param RTLoc Source location identifier
2441 /// \param AllocaIP The insertion point to be used for alloca instructions.
2442 /// \param Dependencies Vector of DependData objects holding information of
2443 /// dependencies as specified by the 'depend' clause.
2444 /// \param HasNoWait True if the target construct had 'nowait' on it, false
2445 /// otherwise
2447 TargetTaskBodyCallbackTy TaskBodyCB, Value *DeviceID, Value *RTLoc,
2450 bool HasNoWait);
2451
2452 /// Emit the arguments to be passed to the runtime library based on the
2453 /// arrays of base pointers, pointers, sizes, map types, and mappers. If
2454 /// ForEndCall, emit map types to be passed for the end of the region instead
2455 /// of the beginning.
2459 bool ForEndCall = false);
2460
2461 /// Emit an array of struct descriptors to be assigned to the offload args.
2463 InsertPointTy CodeGenIP,
2464 MapInfosTy &CombinedInfo,
2466
2467 /// Emit the arrays used to pass the captures and map information to the
2468 /// offloading runtime library. If there is no map or capture information,
2469 /// return nullptr by reference. Accepts a reference to a MapInfosTy object
2470 /// that contains information generated for mappable clauses,
2471 /// including base pointers, pointers, sizes, map types, user-defined mappers.
2473 InsertPointTy AllocaIP, InsertPointTy CodeGenIP, MapInfosTy &CombinedInfo,
2474 TargetDataInfo &Info, bool IsNonContiguous = false,
2475 function_ref<void(unsigned int, Value *)> DeviceAddrCB = nullptr,
2476 function_ref<Value *(unsigned int)> CustomMapperCB = nullptr);
2477
2478 /// Allocates memory for and populates the arrays required for offloading
2479 /// (offload_{baseptrs|ptrs|mappers|sizes|maptypes|mapnames}). Then, it
2480 /// emits their base addresses as arguments to be passed to the runtime
2481 /// library. In essence, this function is a combination of
2482 /// emitOffloadingArrays and emitOffloadingArraysArgument and should arguably
2483 /// be preferred by clients of OpenMPIRBuilder.
2485 InsertPointTy AllocaIP, InsertPointTy CodeGenIP, TargetDataInfo &Info,
2486 TargetDataRTArgs &RTArgs, MapInfosTy &CombinedInfo,
2487 bool IsNonContiguous = false, bool ForEndCall = false,
2488 function_ref<void(unsigned int, Value *)> DeviceAddrCB = nullptr,
2489 function_ref<Value *(unsigned int)> CustomMapperCB = nullptr);
2490
2491 /// Creates offloading entry for the provided entry ID \a ID, address \a
2492 /// Addr, size \a Size, and flags \a Flags.
2494 int32_t Flags, GlobalValue::LinkageTypes,
2495 StringRef Name = "");
2496
2497 /// The kind of errors that can occur when emitting the offload entries and
2498 /// metadata.
2504
2505 /// Callback function type
2507 std::function<void(EmitMetadataErrorKind, TargetRegionEntryInfo)>;
2508
2509 // Emit the offloading entries and metadata so that the device codegen side
2510 // can easily figure out what to emit. The produced metadata looks like
2511 // this:
2512 //
2513 // !omp_offload.info = !{!1, ...}
2514 //
2515 // We only generate metadata for function that contain target regions.
2517 EmitMetadataErrorReportFunctionTy &ErrorReportFunction);
2518
2519public:
2520 /// Generator for __kmpc_copyprivate
2521 ///
2522 /// \param Loc The source location description.
2523 /// \param BufSize Number of elements in the buffer.
2524 /// \param CpyBuf List of pointers to data to be copied.
2525 /// \param CpyFn function to call for copying data.
2526 /// \param DidIt flag variable; 1 for 'single' thread, 0 otherwise.
2527 ///
2528 /// \return The insertion position *after* the CopyPrivate call.
2529
2531 llvm::Value *BufSize, llvm::Value *CpyBuf,
2532 llvm::Value *CpyFn, llvm::Value *DidIt);
2533
2534 /// Generator for '#omp single'
2535 ///
2536 /// \param Loc The source location description.
2537 /// \param BodyGenCB Callback that will generate the region code.
2538 /// \param FiniCB Callback to finalize variable copies.
2539 /// \param IsNowait If false, a barrier is emitted.
2540 /// \param CPVars copyprivate variables.
2541 /// \param CPFuncs copy functions to use for each copyprivate variable.
2542 ///
2543 /// \returns The insertion position *after* the single call.
2545 BodyGenCallbackTy BodyGenCB,
2546 FinalizeCallbackTy FiniCB, bool IsNowait,
2547 ArrayRef<llvm::Value *> CPVars = {},
2548 ArrayRef<llvm::Function *> CPFuncs = {});
2549
2550 /// Generator for '#omp master'
2551 ///
2552 /// \param Loc The insert and source location description.
2553 /// \param BodyGenCB Callback that will generate the region code.
2554 /// \param FiniCB Callback to finalize variable copies.
2555 ///
2556 /// \returns The insertion position *after* the master.
2557 InsertPointOrErrorTy createMaster(const LocationDescription &Loc,
2558 BodyGenCallbackTy BodyGenCB,
2559 FinalizeCallbackTy FiniCB);
2560
2561 /// Generator for '#omp masked'
2562 ///
2563 /// \param Loc The insert and source location description.
2564 /// \param BodyGenCB Callback that will generate the region code.
2565 /// \param FiniCB Callback to finialize variable copies.
2566 ///
2567 /// \returns The insertion position *after* the masked.
2568 InsertPointOrErrorTy createMasked(const LocationDescription &Loc,
2569 BodyGenCallbackTy BodyGenCB,
2570 FinalizeCallbackTy FiniCB, Value *Filter);
2571
2572 /// Generator for '#omp critical'
2573 ///
2574 /// \param Loc The insert and source location description.
2575 /// \param BodyGenCB Callback that will generate the region body code.
2576 /// \param FiniCB Callback to finalize variable copies.
2577 /// \param CriticalName name of the lock used by the critical directive
2578 /// \param HintInst Hint Instruction for hint clause associated with critical
2579 ///
2580 /// \returns The insertion position *after* the critical.
2581 InsertPointOrErrorTy createCritical(const LocationDescription &Loc,
2582 BodyGenCallbackTy BodyGenCB,
2583 FinalizeCallbackTy FiniCB,
2584 StringRef CriticalName, Value *HintInst);
2585
2586 /// Generator for '#omp ordered depend (source | sink)'
2587 ///
2588 /// \param Loc The insert and source location description.
2589 /// \param AllocaIP The insertion point to be used for alloca instructions.
2590 /// \param NumLoops The number of loops in depend clause.
2591 /// \param StoreValues The value will be stored in vector address.
2592 /// \param Name The name of alloca instruction.
2593 /// \param IsDependSource If true, depend source; otherwise, depend sink.
2594 ///
2595 /// \return The insertion position *after* the ordered.
2596 InsertPointTy createOrderedDepend(const LocationDescription &Loc,
2597 InsertPointTy AllocaIP, unsigned NumLoops,
2598 ArrayRef<llvm::Value *> StoreValues,
2599 const Twine &Name, bool IsDependSource);
2600
2601 /// Generator for '#omp ordered [threads | simd]'
2602 ///
2603 /// \param Loc The insert and source location description.
2604 /// \param BodyGenCB Callback that will generate the region code.
2605 /// \param FiniCB Callback to finalize variable copies.
2606 /// \param IsThreads If true, with threads clause or without clause;
2607 /// otherwise, with simd clause;
2608 ///
2609 /// \returns The insertion position *after* the ordered.
2610 InsertPointOrErrorTy createOrderedThreadsSimd(const LocationDescription &Loc,
2611 BodyGenCallbackTy BodyGenCB,
2612 FinalizeCallbackTy FiniCB,
2613 bool IsThreads);
2614
2615 /// Generator for '#omp sections'
2616 ///
2617 /// \param Loc The insert and source location description.
2618 /// \param AllocaIP The insertion points to be used for alloca instructions.
2619 /// \param SectionCBs Callbacks that will generate body of each section.
2620 /// \param PrivCB Callback to copy a given variable (think copy constructor).
2621 /// \param FiniCB Callback to finalize variable copies.
2622 /// \param IsCancellable Flag to indicate a cancellable parallel region.
2623 /// \param IsNowait If true, barrier - to ensure all sections are executed
2624 /// before moving forward will not be generated.
2625 /// \returns The insertion position *after* the sections.
2627 createSections(const LocationDescription &Loc, InsertPointTy AllocaIP,
2628 ArrayRef<StorableBodyGenCallbackTy> SectionCBs,
2630 bool IsCancellable, bool IsNowait);
2631
2632 /// Generator for '#omp section'
2633 ///
2634 /// \param Loc The insert and source location description.
2635 /// \param BodyGenCB Callback that will generate the region body code.
2636 /// \param FiniCB Callback to finalize variable copies.
2637 /// \returns The insertion position *after* the section.
2638 InsertPointOrErrorTy createSection(const LocationDescription &Loc,
2639 BodyGenCallbackTy BodyGenCB,
2640 FinalizeCallbackTy FiniCB);
2641
2642 /// Generator for `#omp teams`
2643 ///
2644 /// \param Loc The location where the teams construct was encountered.
2645 /// \param BodyGenCB Callback that will generate the region code.
2646 /// \param NumTeamsLower Lower bound on number of teams. If this is nullptr,
2647 /// it is as if lower bound is specified as equal to upperbound. If
2648 /// this is non-null, then upperbound must also be non-null.
2649 /// \param NumTeamsUpper Upper bound on the number of teams.
2650 /// \param ThreadLimit on the number of threads that may participate in a
2651 /// contention group created by each team.
2652 /// \param IfExpr is the integer argument value of the if condition on the
2653 /// teams clause.
2655 createTeams(const LocationDescription &Loc, BodyGenCallbackTy BodyGenCB,
2656 Value *NumTeamsLower = nullptr, Value *NumTeamsUpper = nullptr,
2657 Value *ThreadLimit = nullptr, Value *IfExpr = nullptr);
2658
2659 /// Generate conditional branch and relevant BasicBlocks through which private
2660 /// threads copy the 'copyin' variables from Master copy to threadprivate
2661 /// copies.
2662 ///
2663 /// \param IP insertion block for copyin conditional
2664 /// \param MasterVarPtr a pointer to the master variable
2665 /// \param PrivateVarPtr a pointer to the threadprivate variable
2666 /// \param IntPtrTy Pointer size type
2667 /// \param BranchtoEnd Create a branch between the copyin.not.master blocks
2668 // and copy.in.end block
2669 ///
2670 /// \returns The insertion point where copying operation to be emitted.
2672 Value *PrivateAddr,
2673 llvm::IntegerType *IntPtrTy,
2674 bool BranchtoEnd = true);
2675
2676 /// Create a runtime call for kmpc_Alloc
2677 ///
2678 /// \param Loc The insert and source location description.
2679 /// \param Size Size of allocated memory space
2680 /// \param Allocator Allocator information instruction
2681 /// \param Name Name of call Instruction for OMP_alloc
2682 ///
2683 /// \returns CallInst to the OMP_Alloc call
2684 CallInst *createOMPAlloc(const LocationDescription &Loc, Value *Size,
2685 Value *Allocator, std::string Name = "");
2686
2687 /// Create a runtime call for kmpc_free
2688 ///
2689 /// \param Loc The insert and source location description.
2690 /// \param Addr Address of memory space to be freed
2691 /// \param Allocator Allocator information instruction
2692 /// \param Name Name of call Instruction for OMP_Free
2693 ///
2694 /// \returns CallInst to the OMP_Free call
2695 CallInst *createOMPFree(const LocationDescription &Loc, Value *Addr,
2696 Value *Allocator, std::string Name = "");
2697
2698 /// Create a runtime call for kmpc_threadprivate_cached
2699 ///
2700 /// \param Loc The insert and source location description.
2701 /// \param Pointer pointer to data to be cached
2702 /// \param Size size of data to be cached
2703 /// \param Name Name of call Instruction for callinst
2704 ///
2705 /// \returns CallInst to the thread private cache call.
2706 CallInst *createCachedThreadPrivate(const LocationDescription &Loc,
2709 const llvm::Twine &Name = Twine(""));
2710
2711 /// Create a runtime call for __tgt_interop_init
2712 ///
2713 /// \param Loc The insert and source location description.
2714 /// \param InteropVar variable to be allocated
2715 /// \param InteropType type of interop operation
2716 /// \param Device devide to which offloading will occur
2717 /// \param NumDependences number of dependence variables
2718 /// \param DependenceAddress pointer to dependence variables
2719 /// \param HaveNowaitClause does nowait clause exist
2720 ///
2721 /// \returns CallInst to the __tgt_interop_init call
2722 CallInst *createOMPInteropInit(const LocationDescription &Loc,
2723 Value *InteropVar,
2724 omp::OMPInteropType InteropType, Value *Device,
2725 Value *NumDependences,
2726 Value *DependenceAddress,
2727 bool HaveNowaitClause);
2728
2729 /// Create a runtime call for __tgt_interop_destroy
2730 ///
2731 /// \param Loc The insert and source location description.
2732 /// \param InteropVar variable to be allocated
2733 /// \param Device devide to which offloading will occur
2734 /// \param NumDependences number of dependence variables
2735 /// \param DependenceAddress pointer to dependence variables
2736 /// \param HaveNowaitClause does nowait clause exist
2737 ///
2738 /// \returns CallInst to the __tgt_interop_destroy call
2739 CallInst *createOMPInteropDestroy(const LocationDescription &Loc,
2740 Value *InteropVar, Value *Device,
2741 Value *NumDependences,
2742 Value *DependenceAddress,
2743 bool HaveNowaitClause);
2744
2745 /// Create a runtime call for __tgt_interop_use
2746 ///
2747 /// \param Loc The insert and source location description.
2748 /// \param InteropVar variable to be allocated
2749 /// \param Device devide to which offloading will occur
2750 /// \param NumDependences number of dependence variables
2751 /// \param DependenceAddress pointer to dependence variables
2752 /// \param HaveNowaitClause does nowait clause exist
2753 ///
2754 /// \returns CallInst to the __tgt_interop_use call
2755 CallInst *createOMPInteropUse(const LocationDescription &Loc,
2756 Value *InteropVar, Value *Device,
2757 Value *NumDependences, Value *DependenceAddress,
2758 bool HaveNowaitClause);
2759
2760 /// The `omp target` interface
2761 ///
2762 /// For more information about the usage of this interface,
2763 /// \see openmp/libomptarget/deviceRTLs/common/include/target.h
2764 ///
2765 ///{
2766
2767 /// Create a runtime call for kmpc_target_init
2768 ///
2769 /// \param Loc The insert and source location description.
2770 /// \param Attrs Structure containing the default attributes, including
2771 /// numbers of threads and teams to launch the kernel with.
2773 const LocationDescription &Loc,
2775
2776 /// Create a runtime call for kmpc_target_deinit
2777 ///
2778 /// \param Loc The insert and source location description.
2779 /// \param TeamsReductionDataSize The maximal size of all the reduction data
2780 /// for teams reduction.
2781 /// \param TeamsReductionBufferLength The number of elements (each of up to
2782 /// \p TeamsReductionDataSize size), in the teams reduction buffer.
2783 void createTargetDeinit(const LocationDescription &Loc,
2784 int32_t TeamsReductionDataSize = 0,
2785 int32_t TeamsReductionBufferLength = 1024);
2786
2787 ///}
2788
2789 /// Helpers to read/write kernel annotations from the IR.
2790 ///
2791 ///{
2792
2793 /// Read/write a bounds on threads for \p Kernel. Read will return 0 if none
2794 /// is set.
2795 static std::pair<int32_t, int32_t>
2796 readThreadBoundsForKernel(const Triple &T, Function &Kernel);
2797 static void writeThreadBoundsForKernel(const Triple &T, Function &Kernel,
2798 int32_t LB, int32_t UB);
2799
2800 /// Read/write a bounds on teams for \p Kernel. Read will return 0 if none
2801 /// is set.
2802 static std::pair<int32_t, int32_t> readTeamBoundsForKernel(const Triple &T,
2803 Function &Kernel);
2804 static void writeTeamsForKernel(const Triple &T, Function &Kernel, int32_t LB,
2805 int32_t UB);
2806 ///}
2807
2808private:
2809 // Sets the function attributes expected for the outlined function
2810 void setOutlinedTargetRegionFunctionAttributes(Function *OutlinedFn);
2811
2812 // Creates the function ID/Address for the given outlined function.
2813 // In the case of an embedded device function the address of the function is
2814 // used, in the case of a non-offload function a constant is created.
2815 Constant *createOutlinedFunctionID(Function *OutlinedFn,
2816 StringRef EntryFnIDName);
2817
2818 // Creates the region entry address for the outlined function
2819 Constant *createTargetRegionEntryAddr(Function *OutlinedFunction,
2820 StringRef EntryFnName);
2821
2822public:
2823 /// Functions used to generate a function with the given name.
2825 std::function<Expected<Function *>(StringRef FunctionName)>;
2826
2827 /// Create a unique name for the entry function using the source location
2828 /// information of the current target region. The name will be something like:
2829 ///
2830 /// __omp_offloading_DD_FFFF_PP_lBB[_CC]
2831 ///
2832 /// where DD_FFFF is an ID unique to the file (device and file IDs), PP is the
2833 /// mangled name of the function that encloses the target region and BB is the
2834 /// line number of the target region. CC is a count added when more than one
2835 /// region is located at the same location.
2836 ///
2837 /// If this target outline function is not an offload entry, we don't need to
2838 /// register it. This may happen if it is guarded by an if clause that is
2839 /// false at compile time, or no target archs have been specified.
2840 ///
2841 /// The created target region ID is used by the runtime library to identify
2842 /// the current target region, so it only has to be unique and not
2843 /// necessarily point to anything. It could be the pointer to the outlined
2844 /// function that implements the target region, but we aren't using that so
2845 /// that the compiler doesn't need to keep that, and could therefore inline
2846 /// the host function if proven worthwhile during optimization. In the other
2847 /// hand, if emitting code for the device, the ID has to be the function
2848 /// address so that it can retrieved from the offloading entry and launched
2849 /// by the runtime library. We also mark the outlined function to have
2850 /// external linkage in case we are emitting code for the device, because
2851 /// these functions will be entry points to the device.
2852 ///
2853 /// \param InfoManager The info manager keeping track of the offload entries
2854 /// \param EntryInfo The entry information about the function
2855 /// \param GenerateFunctionCallback The callback function to generate the code
2856 /// \param OutlinedFunction Pointer to the outlined function
2857 /// \param EntryFnIDName Name of the ID o be created
2859 FunctionGenCallback &GenerateFunctionCallback,
2860 bool IsOffloadEntry, Function *&OutlinedFn,
2861 Constant *&OutlinedFnID);
2862
2863 /// Registers the given function and sets up the attribtues of the function
2864 /// Returns the FunctionID.
2865 ///
2866 /// \param InfoManager The info manager keeping track of the offload entries
2867 /// \param EntryInfo The entry information about the function
2868 /// \param OutlinedFunction Pointer to the outlined function
2869 /// \param EntryFnName Name of the outlined function
2870 /// \param EntryFnIDName Name of the ID o be created
2872 Function *OutlinedFunction,
2873 StringRef EntryFnName,
2874 StringRef EntryFnIDName);
2875
2876 /// Type of BodyGen to use for region codegen
2877 ///
2878 /// Priv: If device pointer privatization is required, emit the body of the
2879 /// region here. It will have to be duplicated: with and without
2880 /// privatization.
2881 /// DupNoPriv: If we need device pointer privatization, we need
2882 /// to emit the body of the region with no privatization in the 'else' branch
2883 /// of the conditional.
2884 /// NoPriv: If we don't require privatization of device
2885 /// pointers, we emit the body in between the runtime calls. This avoids
2886 /// duplicating the body code.
2888
2889 /// Callback type for creating the map infos for the kernel parameters.
2890 /// \param CodeGenIP is the insertion point where code should be generated,
2891 /// if any.
2894
2895private:
2896 /// Emit the array initialization or deletion portion for user-defined mapper
2897 /// code generation. First, it evaluates whether an array section is mapped
2898 /// and whether the \a MapType instructs to delete this section. If \a IsInit
2899 /// is true, and \a MapType indicates to not delete this array, array
2900 /// initialization code is generated. If \a IsInit is false, and \a MapType
2901 /// indicates to delete this array, array deletion code is generated.
2902 void emitUDMapperArrayInitOrDel(Function *MapperFn, llvm::Value *MapperHandle,
2903 llvm::Value *Base, llvm::Value *Begin,
2904 llvm::Value *Size, llvm::Value *MapType,
2905 llvm::Value *MapName, TypeSize ElementSize,
2906 llvm::BasicBlock *ExitBB, bool IsInit);
2907
2908public:
2909 /// Emit the user-defined mapper function. The code generation follows the
2910 /// pattern in the example below.
2911 /// \code
2912 /// void .omp_mapper.<type_name>.<mapper_id>.(void *rt_mapper_handle,
2913 /// void *base, void *begin,
2914 /// int64_t size, int64_t type,
2915 /// void *name = nullptr) {
2916 /// // Allocate space for an array section first or add a base/begin for
2917 /// // pointer dereference.
2918 /// if ((size > 1 || (base != begin && maptype.IsPtrAndObj)) &&
2919 /// !maptype.IsDelete)
2920 /// __tgt_push_mapper_component(rt_mapper_handle, base, begin,
2921 /// size*sizeof(Ty), clearToFromMember(type));
2922 /// // Map members.
2923 /// for (unsigned i = 0; i < size; i++) {
2924 /// // For each component specified by this mapper:
2925 /// for (auto c : begin[i]->all_components) {
2926 /// if (c.hasMapper())
2927 /// (*c.Mapper())(rt_mapper_handle, c.arg_base, c.arg_begin,
2928 /// c.arg_size,
2929 /// c.arg_type, c.arg_name);
2930 /// else
2931 /// __tgt_push_mapper_component(rt_mapper_handle, c.arg_base,
2932 /// c.arg_begin, c.arg_size, c.arg_type,
2933 /// c.arg_name);
2934 /// }
2935 /// }
2936 /// // Delete the array section.
2937 /// if (size > 1 && maptype.IsDelete)
2938 /// __tgt_push_mapper_component(rt_mapper_handle, base, begin,
2939 /// size*sizeof(Ty), clearToFromMember(type));
2940 /// }
2941 /// \endcode
2942 ///
2943 /// \param PrivAndGenMapInfoCB Callback that privatizes code and populates the
2944 /// MapInfos and returns.
2945 /// \param ElemTy DeclareMapper element type.
2946 /// \param FuncName Optional param to specify mapper function name.
2947 /// \param CustomMapperCB Optional callback to generate code related to
2948 /// custom mappers.
2950 function_ref<MapInfosTy &(InsertPointTy CodeGenIP, llvm::Value *PtrPHI,
2951 llvm::Value *BeginArg)>
2952 PrivAndGenMapInfoCB,
2953 llvm::Type *ElemTy, StringRef FuncName,
2954 function_ref<bool(unsigned int, Function **)> CustomMapperCB = nullptr);
2955
2956 /// Generator for '#omp target data'
2957 ///
2958 /// \param Loc The location where the target data construct was encountered.
2959 /// \param AllocaIP The insertion points to be used for alloca instructions.
2960 /// \param CodeGenIP The insertion point at which the target directive code
2961 /// should be placed.
2962 /// \param IsBegin If true then emits begin mapper call otherwise emits
2963 /// end mapper call.
2964 /// \param DeviceID Stores the DeviceID from the device clause.
2965 /// \param IfCond Value which corresponds to the if clause condition.
2966 /// \param Info Stores all information realted to the Target Data directive.
2967 /// \param GenMapInfoCB Callback that populates the MapInfos and returns.
2968 /// \param BodyGenCB Optional Callback to generate the region code.
2969 /// \param DeviceAddrCB Optional callback to generate code related to
2970 /// use_device_ptr and use_device_addr.
2971 /// \param CustomMapperCB Optional callback to generate code related to
2972 /// custom mappers.
2974 const LocationDescription &Loc, InsertPointTy AllocaIP,
2975 InsertPointTy CodeGenIP, Value *DeviceID, Value *IfCond,
2977 omp::RuntimeFunction *MapperFunc = nullptr,
2979 BodyGenTy BodyGenType)>
2980 BodyGenCB = nullptr,
2981 function_ref<void(unsigned int, Value *)> DeviceAddrCB = nullptr,
2982 function_ref<Value *(unsigned int)> CustomMapperCB = nullptr,
2983 Value *SrcLocInfo = nullptr);
2984
2986 InsertPointTy AllocaIP, InsertPointTy CodeGenIP)>;
2987
2989 Argument &Arg, Value *Input, Value *&RetVal, InsertPointTy AllocaIP,
2990 InsertPointTy CodeGenIP)>;
2991
2992 /// Generator for '#omp target'
2993 ///
2994 /// \param Loc where the target data construct was encountered.
2995 /// \param IsOffloadEntry whether it is an offload entry.
2996 /// \param CodeGenIP The insertion point where the call to the outlined
2997 /// function should be emitted.
2998 /// \param EntryInfo The entry information about the function.
2999 /// \param DefaultAttrs Structure containing the default attributes, including
3000 /// numbers of threads and teams to launch the kernel with.
3001 /// \param RuntimeAttrs Structure containing the runtime numbers of threads
3002 /// and teams to launch the kernel with.
3003 /// \param Inputs The input values to the region that will be passed.
3004 /// as arguments to the outlined function.
3005 /// \param BodyGenCB Callback that will generate the region code.
3006 /// \param ArgAccessorFuncCB Callback that will generate accessors
3007 /// instructions for passed in target arguments where neccessary
3008 /// \param Dependencies A vector of DependData objects that carry
3009 // dependency information as passed in the depend clause
3010 // \param HasNowait Whether the target construct has a `nowait` clause or not.
3012 const LocationDescription &Loc, bool IsOffloadEntry,
3015 TargetRegionEntryInfo &EntryInfo,
3016 const TargetKernelDefaultAttrs &DefaultAttrs,
3017 const TargetKernelRuntimeAttrs &RuntimeAttrs,
3018 SmallVectorImpl<Value *> &Inputs, GenMapInfoCallbackTy GenMapInfoCB,
3019 TargetBodyGenCallbackTy BodyGenCB,
3020 TargetGenArgAccessorsCallbackTy ArgAccessorFuncCB,
3021 SmallVector<DependData> Dependencies = {}, bool HasNowait = false);
3022
3023 /// Returns __kmpc_for_static_init_* runtime function for the specified
3024 /// size \a IVSize and sign \a IVSigned. Will create a distribute call
3025 /// __kmpc_distribute_static_init* if \a IsGPUDistribute is set.
3026 FunctionCallee createForStaticInitFunction(unsigned IVSize, bool IVSigned,
3027 bool IsGPUDistribute);
3028
3029 /// Returns __kmpc_dispatch_init_* runtime function for the specified
3030 /// size \a IVSize and sign \a IVSigned.
3031 FunctionCallee createDispatchInitFunction(unsigned IVSize, bool IVSigned);
3032
3033 /// Returns __kmpc_dispatch_next_* runtime function for the specified
3034 /// size \a IVSize and sign \a IVSigned.
3035 FunctionCallee createDispatchNextFunction(unsigned IVSize, bool IVSigned);
3036
3037 /// Returns __kmpc_dispatch_fini_* runtime function for the specified
3038 /// size \a IVSize and sign \a IVSigned.
3039 FunctionCallee createDispatchFiniFunction(unsigned IVSize, bool IVSigned);
3040
3041 /// Returns __kmpc_dispatch_deinit runtime function.
3043
3044 /// Declarations for LLVM-IR types (simple, array, function and structure) are
3045 /// generated below. Their names are defined and used in OpenMPKinds.def. Here
3046 /// we provide the declarations, the initializeTypes function will provide the
3047 /// values.
3048 ///
3049 ///{
3050#define OMP_TYPE(VarName, InitValue) Type *VarName = nullptr;
3051#define OMP_ARRAY_TYPE(VarName, ElemTy, ArraySize) \
3052 ArrayType *VarName##Ty = nullptr; \
3053 PointerType *VarName##PtrTy = nullptr;
3054#define OMP_FUNCTION_TYPE(VarName, IsVarArg, ReturnType, ...) \
3055 FunctionType *VarName = nullptr; \
3056 PointerType *VarName##Ptr = nullptr;
3057#define OMP_STRUCT_TYPE(VarName, StrName, ...) \
3058 StructType *VarName = nullptr; \
3059 PointerType *VarName##Ptr = nullptr;
3060#include "llvm/Frontend/OpenMP/OMPKinds.def"
3061
3062 ///}
3063
3064private:
3065 /// Create all simple and struct types exposed by the runtime and remember
3066 /// the llvm::PointerTypes of them for easy access later.
3067 void initializeTypes(Module &M);
3068
3069 /// Common interface for generating entry calls for OMP Directives.
3070 /// if the directive has a region/body, It will set the insertion
3071 /// point to the body
3072 ///
3073 /// \param OMPD Directive to generate entry blocks for
3074 /// \param EntryCall Call to the entry OMP Runtime Function
3075 /// \param ExitBB block where the region ends.
3076 /// \param Conditional indicate if the entry call result will be used
3077 /// to evaluate a conditional of whether a thread will execute
3078 /// body code or not.
3079 ///
3080 /// \return The insertion position in exit block
3081 InsertPointTy emitCommonDirectiveEntry(omp::Directive OMPD, Value *EntryCall,
3082 BasicBlock *ExitBB,
3083 bool Conditional = false);
3084
3085 /// Common interface to finalize the region
3086 ///
3087 /// \param OMPD Directive to generate exiting code for
3088 /// \param FinIP Insertion point for emitting Finalization code and exit call
3089 /// \param ExitCall Call to the ending OMP Runtime Function
3090 /// \param HasFinalize indicate if the directive will require finalization
3091 /// and has a finalization callback in the stack that
3092 /// should be called.
3093 ///
3094 /// \return The insertion position in exit block
3095 InsertPointOrErrorTy emitCommonDirectiveExit(omp::Directive OMPD,
3096 InsertPointTy FinIP,
3097 Instruction *ExitCall,
3098 bool HasFinalize = true);
3099
3100 /// Common Interface to generate OMP inlined regions
3101 ///
3102 /// \param OMPD Directive to generate inlined region for
3103 /// \param EntryCall Call to the entry OMP Runtime Function
3104 /// \param ExitCall Call to the ending OMP Runtime Function
3105 /// \param BodyGenCB Body code generation callback.
3106 /// \param FiniCB Finalization Callback. Will be called when finalizing region
3107 /// \param Conditional indicate if the entry call result will be used
3108 /// to evaluate a conditional of whether a thread will execute
3109 /// body code or not.
3110 /// \param HasFinalize indicate if the directive will require finalization
3111 /// and has a finalization callback in the stack that
3112 /// should be called.
3113 /// \param IsCancellable if HasFinalize is set to true, indicate if the
3114 /// the directive should be cancellable.
3115 /// \return The insertion point after the region
3117 EmitOMPInlinedRegion(omp::Directive OMPD, Instruction *EntryCall,
3118 Instruction *ExitCall, BodyGenCallbackTy BodyGenCB,
3119 FinalizeCallbackTy FiniCB, bool Conditional = false,
3120 bool HasFinalize = true, bool IsCancellable = false);
3121
3122 /// Get the platform-specific name separator.
3123 /// \param Parts different parts of the final name that needs separation
3124 /// \param FirstSeparator First separator used between the initial two
3125 /// parts of the name.
3126 /// \param Separator separator used between all of the rest consecutive
3127 /// parts of the name
3128 static std::string getNameWithSeparators(ArrayRef<StringRef> Parts,
3129 StringRef FirstSeparator,
3130 StringRef Separator);
3131
3132 /// Returns corresponding lock object for the specified critical region
3133 /// name. If the lock object does not exist it is created, otherwise the
3134 /// reference to the existing copy is returned.
3135 /// \param CriticalName Name of the critical region.
3136 ///
3137 Value *getOMPCriticalRegionLock(StringRef CriticalName);
3138
3139 /// Callback type for Atomic Expression update
3140 /// ex:
3141 /// \code{.cpp}
3142 /// unsigned x = 0;
3143 /// #pragma omp atomic update
3144 /// x = Expr(x_old); //Expr() is any legal operation
3145 /// \endcode
3146 ///
3147 /// \param XOld the value of the atomic memory address to use for update
3148 /// \param IRB reference to the IRBuilder to use
3149 ///
3150 /// \returns Value to update X to.
3151 using AtomicUpdateCallbackTy =
3152 const function_ref<Expected<Value *>(Value *XOld, IRBuilder<> &IRB)>;
3153
3154private:
3155 enum AtomicKind { Read, Write, Update, Capture, Compare };
3156
3157 /// Determine whether to emit flush or not
3158 ///
3159 /// \param Loc The insert and source location description.
3160 /// \param AO The required atomic ordering
3161 /// \param AK The OpenMP atomic operation kind used.
3162 ///
3163 /// \returns wether a flush was emitted or not
3164 bool checkAndEmitFlushAfterAtomic(const LocationDescription &Loc,
3165 AtomicOrdering AO, AtomicKind AK);
3166
3167 /// Emit atomic update for constructs: X = X BinOp Expr ,or X = Expr BinOp X
3168 /// For complex Operations: X = UpdateOp(X) => CmpExch X, old_X, UpdateOp(X)
3169 /// Only Scalar data types.
3170 ///
3171 /// \param AllocaIP The insertion point to be used for alloca
3172 /// instructions.
3173 /// \param X The target atomic pointer to be updated
3174 /// \param XElemTy The element type of the atomic pointer.
3175 /// \param Expr The value to update X with.
3176 /// \param AO Atomic ordering of the generated atomic
3177 /// instructions.
3178 /// \param RMWOp The binary operation used for update. If
3179 /// operation is not supported by atomicRMW,
3180 /// or belong to {FADD, FSUB, BAD_BINOP}.
3181 /// Then a `cmpExch` based atomic will be generated.
3182 /// \param UpdateOp Code generator for complex expressions that cannot be
3183 /// expressed through atomicrmw instruction.
3184 /// \param VolatileX true if \a X volatile?
3185 /// \param IsXBinopExpr true if \a X is Left H.S. in Right H.S. part of the
3186 /// update expression, false otherwise.
3187 /// (e.g. true for X = X BinOp Expr)
3188 ///
3189 /// \returns A pair of the old value of X before the update, and the value
3190 /// used for the update.
3191 Expected<std::pair<Value *, Value *>>
3192 emitAtomicUpdate(InsertPointTy AllocaIP, Value *X, Type *XElemTy, Value *Expr,
3194 AtomicUpdateCallbackTy &UpdateOp, bool VolatileX,
3195 bool IsXBinopExpr);
3196
3197 /// Emit the binary op. described by \p RMWOp, using \p Src1 and \p Src2 .
3198 ///
3199 /// \Return The instruction
3200 Value *emitRMWOpAsInstruction(Value *Src1, Value *Src2,
3201 AtomicRMWInst::BinOp RMWOp);
3202
3203public:
3204 /// a struct to pack relevant information while generating atomic Ops
3206 Value *Var = nullptr;
3207 Type *ElemTy = nullptr;
3208 bool IsSigned = false;
3209 bool IsVolatile = false;
3210 };
3211
3212 /// Emit atomic Read for : V = X --- Only Scalar data types.
3213 ///
3214 /// \param Loc The insert and source location description.
3215 /// \param X The target pointer to be atomically read
3216 /// \param V Memory address where to store atomically read
3217 /// value
3218 /// \param AO Atomic ordering of the generated atomic
3219 /// instructions.
3220 ///
3221 /// \return Insertion point after generated atomic read IR.
3224 AtomicOrdering AO);
3225
3226 /// Emit atomic write for : X = Expr --- Only Scalar data types.
3227 ///
3228 /// \param Loc The insert and source location description.
3229 /// \param X The target pointer to be atomically written to
3230 /// \param Expr The value to store.
3231 /// \param AO Atomic ordering of the generated atomic
3232 /// instructions.
3233 ///
3234 /// \return Insertion point after generated atomic Write IR.
3236 AtomicOpValue &X, Value *Expr,
3237 AtomicOrdering AO);
3238
3239 /// Emit atomic update for constructs: X = X BinOp Expr ,or X = Expr BinOp X
3240 /// For complex Operations: X = UpdateOp(X) => CmpExch X, old_X, UpdateOp(X)
3241 /// Only Scalar data types.
3242 ///
3243 /// \param Loc The insert and source location description.
3244 /// \param AllocaIP The insertion point to be used for alloca instructions.
3245 /// \param X The target atomic pointer to be updated
3246 /// \param Expr The value to update X with.
3247 /// \param AO Atomic ordering of the generated atomic instructions.
3248 /// \param RMWOp The binary operation used for update. If operation
3249 /// is not supported by atomicRMW, or belong to
3250 /// {FADD, FSUB, BAD_BINOP}. Then a `cmpExch` based
3251 /// atomic will be generated.
3252 /// \param UpdateOp Code generator for complex expressions that cannot be
3253 /// expressed through atomicrmw instruction.
3254 /// \param IsXBinopExpr true if \a X is Left H.S. in Right H.S. part of the
3255 /// update expression, false otherwise.
3256 /// (e.g. true for X = X BinOp Expr)
3257 ///
3258 /// \return Insertion point after generated atomic update IR.
3261 AtomicOpValue &X, Value *Expr, AtomicOrdering AO,
3263 AtomicUpdateCallbackTy &UpdateOp, bool IsXBinopExpr);
3264
3265 /// Emit atomic update for constructs: --- Only Scalar data types
3266 /// V = X; X = X BinOp Expr ,
3267 /// X = X BinOp Expr; V = X,
3268 /// V = X; X = Expr BinOp X,
3269 /// X = Expr BinOp X; V = X,
3270 /// V = X; X = UpdateOp(X),
3271 /// X = UpdateOp(X); V = X,
3272 ///
3273 /// \param Loc The insert and source location description.
3274 /// \param AllocaIP The insertion point to be used for alloca instructions.
3275 /// \param X The target atomic pointer to be updated
3276 /// \param V Memory address where to store captured value
3277 /// \param Expr The value to update X with.
3278 /// \param AO Atomic ordering of the generated atomic instructions
3279 /// \param RMWOp The binary operation used for update. If
3280 /// operation is not supported by atomicRMW, or belong to
3281 /// {FADD, FSUB, BAD_BINOP}. Then a cmpExch based
3282 /// atomic will be generated.
3283 /// \param UpdateOp Code generator for complex expressions that cannot be
3284 /// expressed through atomicrmw instruction.
3285 /// \param UpdateExpr true if X is an in place update of the form
3286 /// X = X BinOp Expr or X = Expr BinOp X
3287 /// \param IsXBinopExpr true if X is Left H.S. in Right H.S. part of the
3288 /// update expression, false otherwise.
3289 /// (e.g. true for X = X BinOp Expr)
3290 /// \param IsPostfixUpdate true if original value of 'x' must be stored in
3291 /// 'v', not an updated one.
3292 ///
3293 /// \return Insertion point after generated atomic capture IR.
3296 AtomicOpValue &X, AtomicOpValue &V, Value *Expr,
3298 AtomicUpdateCallbackTy &UpdateOp, bool UpdateExpr,
3299 bool IsPostfixUpdate, bool IsXBinopExpr);
3300
3301 /// Emit atomic compare for constructs: --- Only scalar data types
3302 /// cond-expr-stmt:
3303 /// x = x ordop expr ? expr : x;
3304 /// x = expr ordop x ? expr : x;
3305 /// x = x == e ? d : x;
3306 /// x = e == x ? d : x; (this one is not in the spec)
3307 /// cond-update-stmt:
3308 /// if (x ordop expr) { x = expr; }
3309 /// if (expr ordop x) { x = expr; }
3310 /// if (x == e) { x = d; }
3311 /// if (e == x) { x = d; } (this one is not in the spec)
3312 /// conditional-update-capture-atomic:
3313 /// v = x; cond-update-stmt; (IsPostfixUpdate=true, IsFailOnly=false)
3314 /// cond-update-stmt; v = x; (IsPostfixUpdate=false, IsFailOnly=false)
3315 /// if (x == e) { x = d; } else { v = x; } (IsPostfixUpdate=false,
3316 /// IsFailOnly=true)
3317 /// r = x == e; if (r) { x = d; } (IsPostfixUpdate=false, IsFailOnly=false)
3318 /// r = x == e; if (r) { x = d; } else { v = x; } (IsPostfixUpdate=false,
3319 /// IsFailOnly=true)
3320 ///
3321 /// \param Loc The insert and source location description.
3322 /// \param X The target atomic pointer to be updated.
3323 /// \param V Memory address where to store captured value (for
3324 /// compare capture only).
3325 /// \param R Memory address where to store comparison result
3326 /// (for compare capture with '==' only).
3327 /// \param E The expected value ('e') for forms that use an
3328 /// equality comparison or an expression ('expr') for
3329 /// forms that use 'ordop' (logically an atomic maximum or
3330 /// minimum).
3331 /// \param D The desired value for forms that use an equality
3332 /// comparison. If forms that use 'ordop', it should be
3333 /// \p nullptr.
3334 /// \param AO Atomic ordering of the generated atomic instructions.
3335 /// \param Op Atomic compare operation. It can only be ==, <, or >.
3336 /// \param IsXBinopExpr True if the conditional statement is in the form where
3337 /// x is on LHS. It only matters for < or >.
3338 /// \param IsPostfixUpdate True if original value of 'x' must be stored in
3339 /// 'v', not an updated one (for compare capture
3340 /// only).
3341 /// \param IsFailOnly True if the original value of 'x' is stored to 'v'
3342 /// only when the comparison fails. This is only valid for
3343 /// the case the comparison is '=='.
3344 ///
3345 /// \return Insertion point after generated atomic capture IR.
3350 bool IsXBinopExpr, bool IsPostfixUpdate, bool IsFailOnly);
3353 AtomicOpValue &R, Value *E, Value *D,
3354 AtomicOrdering AO,
3356 bool IsXBinopExpr, bool IsPostfixUpdate,
3357 bool IsFailOnly, AtomicOrdering Failure);
3358
3359 /// Create the control flow structure of a canonical OpenMP loop.
3360 ///
3361 /// The emitted loop will be disconnected, i.e. no edge to the loop's
3362 /// preheader and no terminator in the AfterBB. The OpenMPIRBuilder's
3363 /// IRBuilder location is not preserved.
3364 ///
3365 /// \param DL DebugLoc used for the instructions in the skeleton.
3366 /// \param TripCount Value to be used for the trip count.
3367 /// \param F Function in which to insert the BasicBlocks.
3368 /// \param PreInsertBefore Where to insert BBs that execute before the body,
3369 /// typically the body itself.
3370 /// \param PostInsertBefore Where to insert BBs that execute after the body.
3371 /// \param Name Base name used to derive BB
3372 /// and instruction names.
3373 ///
3374 /// \returns The CanonicalLoopInfo that represents the emitted loop.
3376 Function *F,
3377 BasicBlock *PreInsertBefore,
3378 BasicBlock *PostInsertBefore,
3379 const Twine &Name = {});
3380 /// OMP Offload Info Metadata name string
3381 const std::string ompOffloadInfoName = "omp_offload.info";
3382
3383 /// Loads all the offload entries information from the host IR
3384 /// metadata. This function is only meant to be used with device code
3385 /// generation.
3386 ///
3387 /// \param M Module to load Metadata info from. Module passed maybe
3388 /// loaded from bitcode file, i.e, different from OpenMPIRBuilder::M module.
3390
3391 /// Loads all the offload entries information from the host IR
3392 /// metadata read from the file passed in as the HostFilePath argument. This
3393 /// function is only meant to be used with device code generation.
3394 ///
3395 /// \param HostFilePath The path to the host IR file,
3396 /// used to load in offload metadata for the device, allowing host and device
3397 /// to maintain the same metadata mapping.
3398 void loadOffloadInfoMetadata(StringRef HostFilePath);
3399
3400 /// Gets (if variable with the given name already exist) or creates
3401 /// internal global variable with the specified Name. The created variable has
3402 /// linkage CommonLinkage by default and is initialized by null value.
3403 /// \param Ty Type of the global variable. If it is exist already the type
3404 /// must be the same.
3405 /// \param Name Name of the variable.
3407 unsigned AddressSpace = 0);
3408};
3409
3410/// Class to represented the control flow structure of an OpenMP canonical loop.
3411///
3412/// The control-flow structure is standardized for easy consumption by
3413/// directives associated with loops. For instance, the worksharing-loop
3414/// construct may change this control flow such that each loop iteration is
3415/// executed on only one thread. The constraints of a canonical loop in brief
3416/// are:
3417///
3418/// * The number of loop iterations must have been computed before entering the
3419/// loop.
3420///
3421/// * Has an (unsigned) logical induction variable that starts at zero and
3422/// increments by one.
3423///
3424/// * The loop's CFG itself has no side-effects. The OpenMP specification
3425/// itself allows side-effects, but the order in which they happen, including
3426/// how often or whether at all, is unspecified. We expect that the frontend
3427/// will emit those side-effect instructions somewhere (e.g. before the loop)
3428/// such that the CanonicalLoopInfo itself can be side-effect free.
3429///
3430/// Keep in mind that CanonicalLoopInfo is meant to only describe a repeated
3431/// execution of a loop body that satifies these constraints. It does NOT
3432/// represent arbitrary SESE regions that happen to contain a loop. Do not use
3433/// CanonicalLoopInfo for such purposes.
3434///
3435/// The control flow can be described as follows:
3436///
3437/// Preheader
3438/// |
3439/// /-> Header
3440/// | |
3441/// | Cond---\
3442/// | | |
3443/// | Body |
3444/// | | | |
3445/// | <...> |
3446/// | | | |
3447/// \--Latch |
3448/// |
3449/// Exit
3450/// |
3451/// After
3452///
3453/// The loop is thought to start at PreheaderIP (at the Preheader's terminator,
3454/// including) and end at AfterIP (at the After's first instruction, excluding).
3455/// That is, instructions in the Preheader and After blocks (except the
3456/// Preheader's terminator) are out of CanonicalLoopInfo's control and may have
3457/// side-effects. Typically, the Preheader is used to compute the loop's trip
3458/// count. The instructions from BodyIP (at the Body block's first instruction,
3459/// excluding) until the Latch are also considered outside CanonicalLoopInfo's
3460/// control and thus can have side-effects. The body block is the single entry
3461/// point into the loop body, which may contain arbitrary control flow as long
3462/// as all control paths eventually branch to the Latch block.
3463///
3464/// TODO: Consider adding another standardized BasicBlock between Body CFG and
3465/// Latch to guarantee that there is only a single edge to the latch. It would
3466/// make loop transformations easier to not needing to consider multiple
3467/// predecessors of the latch (See redirectAllPredecessorsTo) and would give us
3468/// an equivalant to PreheaderIP, AfterIP and BodyIP for inserting code that
3469/// executes after each body iteration.
3470///
3471/// There must be no loop-carried dependencies through llvm::Values. This is
3472/// equivalant to that the Latch has no PHINode and the Header's only PHINode is
3473/// for the induction variable.
3474///
3475/// All code in Header, Cond, Latch and Exit (plus the terminator of the
3476/// Preheader) are CanonicalLoopInfo's responsibility and their build-up checked
3477/// by assertOK(). They are expected to not be modified unless explicitly
3478/// modifying the CanonicalLoopInfo through a methods that applies a OpenMP
3479/// loop-associated construct such as applyWorkshareLoop, tileLoops, unrollLoop,
3480/// etc. These methods usually invalidate the CanonicalLoopInfo and re-use its
3481/// basic blocks. After invalidation, the CanonicalLoopInfo must not be used
3482/// anymore as its underlying control flow may not exist anymore.
3483/// Loop-transformation methods such as tileLoops, collapseLoops and unrollLoop
3484/// may also return a new CanonicalLoopInfo that can be passed to other
3485/// loop-associated construct implementing methods. These loop-transforming
3486/// methods may either create a new CanonicalLoopInfo usually using
3487/// createLoopSkeleton and invalidate the input CanonicalLoopInfo, or reuse and
3488/// modify one of the input CanonicalLoopInfo and return it as representing the
3489/// modified loop. What is done is an implementation detail of
3490/// transformation-implementing method and callers should always assume that the
3491/// CanonicalLoopInfo passed to it is invalidated and a new object is returned.
3492/// Returned CanonicalLoopInfo have the same structure and guarantees as the one
3493/// created by createCanonicalLoop, such that transforming methods do not have
3494/// to special case where the CanonicalLoopInfo originated from.
3495///
3496/// Generally, methods consuming CanonicalLoopInfo do not need an
3497/// OpenMPIRBuilder::InsertPointTy as argument, but use the locations of the
3498/// CanonicalLoopInfo to insert new or modify existing instructions. Unless
3499/// documented otherwise, methods consuming CanonicalLoopInfo do not invalidate
3500/// any InsertPoint that is outside CanonicalLoopInfo's control. Specifically,
3501/// any InsertPoint in the Preheader, After or Block can still be used after
3502/// calling such a method.
3503///
3504/// TODO: Provide mechanisms for exception handling and cancellation points.
3505///
3506/// Defined outside OpenMPIRBuilder because nested classes cannot be
3507/// forward-declared, e.g. to avoid having to include the entire OMPIRBuilder.h.
3509 friend class OpenMPIRBuilder;
3510
3511private:
3512 BasicBlock *Header = nullptr;
3513 BasicBlock *Cond = nullptr;
3514 BasicBlock *Latch = nullptr;
3515 BasicBlock *Exit = nullptr;
3516
3517 /// Add the control blocks of this loop to \p BBs.
3518 ///
3519 /// This does not include any block from the body, including the one returned
3520 /// by getBody().
3521 ///
3522 /// FIXME: This currently includes the Preheader and After blocks even though
3523 /// their content is (mostly) not under CanonicalLoopInfo's control.
3524 /// Re-evaluated whether this makes sense.
3525 void collectControlBlocks(SmallVectorImpl<BasicBlock *> &BBs);
3526
3527 /// Sets the number of loop iterations to the given value. This value must be
3528 /// valid in the condition block (i.e., defined in the preheader) and is
3529 /// interpreted as an unsigned integer.
3530 void setTripCount(Value *TripCount);
3531
3532 /// Replace all uses of the canonical induction variable in the loop body with
3533 /// a new one.
3534 ///
3535 /// The intended use case is to update the induction variable for an updated
3536 /// iteration space such that it can stay normalized in the 0...tripcount-1
3537 /// range.
3538 ///
3539 /// The \p Updater is called with the (presumable updated) current normalized
3540 /// induction variable and is expected to return the value that uses of the
3541 /// pre-updated induction values should use instead, typically dependent on
3542 /// the new induction variable. This is a lambda (instead of e.g. just passing
3543 /// the new value) to be able to distinguish the uses of the pre-updated
3544 /// induction variable and uses of the induction varible to compute the
3545 /// updated induction variable value.
3546 void mapIndVar(llvm::function_ref<Value *(Instruction *)> Updater);
3547
3548public:
3549 /// Returns whether this object currently represents the IR of a loop. If
3550 /// returning false, it may have been consumed by a loop transformation or not
3551 /// been intialized. Do not use in this case;
3552 bool isValid() const { return Header; }
3553
3554 /// The preheader ensures that there is only a single edge entering the loop.
3555 /// Code that must be execute before any loop iteration can be emitted here,
3556 /// such as computing the loop trip count and begin lifetime markers. Code in
3557 /// the preheader is not considered part of the canonical loop.
3558 BasicBlock *getPreheader() const;
3559
3560 /// The header is the entry for each iteration. In the canonical control flow,
3561 /// it only contains the PHINode for the induction variable.
3563 assert(isValid() && "Requires a valid canonical loop");
3564 return Header;
3565 }
3566
3567 /// The condition block computes whether there is another loop iteration. If
3568 /// yes, branches to the body; otherwise to the exit block.
3570 assert(isValid() && "Requires a valid canonical loop");
3571 return Cond;
3572 }
3573
3574 /// The body block is the single entry for a loop iteration and not controlled
3575 /// by CanonicalLoopInfo. It can contain arbitrary control flow but must
3576 /// eventually branch to the \p Latch block.
3578 assert(isValid() && "Requires a valid canonical loop");
3579 return cast<BranchInst>(Cond->getTerminator())->getSuccessor(0);
3580 }
3581
3582 /// Reaching the latch indicates the end of the loop body code. In the
3583 /// canonical control flow, it only contains the increment of the induction
3584 /// variable.
3586 assert(isValid() && "Requires a valid canonical loop");
3587 return Latch;
3588 }
3589
3590 /// Reaching the exit indicates no more iterations are being executed.
3592 assert(isValid() && "Requires a valid canonical loop");
3593 return Exit;
3594 }
3595
3596 /// The after block is intended for clean-up code such as lifetime end
3597 /// markers. It is separate from the exit block to ensure, analogous to the
3598 /// preheader, it having just a single entry edge and being free from PHI
3599 /// nodes should there be multiple loop exits (such as from break
3600 /// statements/cancellations).
3602 assert(isValid() && "Requires a valid canonical loop");
3603 return Exit->getSingleSuccessor();
3604 }
3605
3606 /// Returns the llvm::Value containing the number of loop iterations. It must
3607 /// be valid in the preheader and always interpreted as an unsigned integer of
3608 /// any bit-width.
3610 assert(isValid() && "Requires a valid canonical loop");
3611 Instruction *CmpI = &Cond->front();
3612 assert(isa<CmpInst>(CmpI) && "First inst must compare IV with TripCount");
3613 return CmpI->getOperand(1);
3614 }
3615
3616 /// Returns the instruction representing the current logical induction
3617 /// variable. Always unsigned, always starting at 0 with an increment of one.
3619 assert(isValid() && "Requires a valid canonical loop");
3620 Instruction *IndVarPHI = &Header->front();
3621 assert(isa<PHINode>(IndVarPHI) && "First inst must be the IV PHI");
3622 return IndVarPHI;
3623 }
3624
3625 /// Return the type of the induction variable (and the trip count).
3627 assert(isValid() && "Requires a valid canonical loop");
3628 return getIndVar()->getType();
3629 }
3630
3631 /// Return the insertion point for user code before the loop.
3633 assert(isValid() && "Requires a valid canonical loop");
3634 BasicBlock *Preheader = getPreheader();
3635 return {Preheader, std::prev(Preheader->end())};
3636 };
3637
3638 /// Return the insertion point for user code in the body.
3640 assert(isValid() && "Requires a valid canonical loop");
3641 BasicBlock *Body = getBody();
3642 return {Body, Body->begin()};
3643 };
3644
3645 /// Return the insertion point for user code after the loop.
3647 assert(isValid() && "Requires a valid canonical loop");
3649 return {After, After->begin()};
3650 };
3651
3653 assert(isValid() && "Requires a valid canonical loop");
3654 return Header->getParent();
3655 }
3656
3657 /// Consistency self-check.
3658 void assertOK() const;
3659
3660 /// Invalidate this loop. That is, the underlying IR does not fulfill the
3661 /// requirements of an OpenMP canonical loop anymore.
3662 void invalidate();
3663};
3664
3665} // end namespace llvm
3666
3667#endif // LLVM_FRONTEND_OPENMP_OMPIRBUILDER_H
arc branch finalize
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
This file defines the BumpPtrAllocator interface.
BlockVerifier::State From
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
Analysis containing CSE Info
Definition: CSEInfo.cpp:27
DXIL Finalize Linkage
uint64_t Addr
std::string Name
uint32_t Index
uint64_t Size
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
Hexagon Hardware Loops
Module.h This file contains the declarations for the Module class.
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
#define G(x, y, z)
Definition: MD5.cpp:56
This file defines constans and helpers used when dealing with OpenMP.
Provides definitions for Target specific Grid Values.
const SmallVectorImpl< MachineOperand > & Cond
Basic Register Allocator
static cl::opt< RegAllocEvictionAdvisorAnalysis::AdvisorMode > Mode("regalloc-enable-advisor", cl::Hidden, cl::init(RegAllocEvictionAdvisorAnalysis::AdvisorMode::Default), cl::desc("Enable regalloc advisor mode"), cl::values(clEnumValN(RegAllocEvictionAdvisorAnalysis::AdvisorMode::Default, "default", "Default"), clEnumValN(RegAllocEvictionAdvisorAnalysis::AdvisorMode::Release, "release", "precompiled"), clEnumValN(RegAllocEvictionAdvisorAnalysis::AdvisorMode::Development, "development", "for training")))
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
Value * RHS
Value * LHS
an instruction to allocate memory on the stack
Definition: Instructions.h:63
This class represents an incoming formal argument to a Function.
Definition: Argument.h:31
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
Align AtomicAlign
Definition: Atomic.h:22
bool UseLibcall
Definition: Atomic.h:24
IRBuilderBase * Builder
Definition: Atomic.h:18
uint64_t AtomicSizeInBits
Definition: Atomic.h:20
uint64_t ValueSizeInBits
Definition: Atomic.h:21
Align ValueAlign
Definition: Atomic.h:23
Type * Ty
Definition: Atomic.h:19
BinOp
This enumeration lists the possible modifications atomicrmw can make.
Definition: Instructions.h:716
LLVM Basic Block Representation.
Definition: BasicBlock.h:61
iterator end()
Definition: BasicBlock.h:461
iterator begin()
Instruction iterator methods.
Definition: BasicBlock.h:448
const Function * getParent() const
Return the enclosing method, or null if none.
Definition: BasicBlock.h:219
Class to represented the control flow structure of an OpenMP canonical loop.
Value * getTripCount() const
Returns the llvm::Value containing the number of loop iterations.
BasicBlock * getHeader() const
The header is the entry for each iteration.
void assertOK() const
Consistency self-check.
Type * getIndVarType() const
Return the type of the induction variable (and the trip count).
BasicBlock * getBody() const
The body block is the single entry for a loop iteration and not controlled by CanonicalLoopInfo.
bool isValid() const
Returns whether this object currently represents the IR of a loop.
OpenMPIRBuilder::InsertPointTy getAfterIP() const
Return the insertion point for user code after the loop.
OpenMPIRBuilder::InsertPointTy getBodyIP() const
Return the insertion point for user code in the body.
BasicBlock * getAfter() const
The after block is intended for clean-up code such as lifetime end markers.
Function * getFunction() const
void invalidate()
Invalidate this loop.
BasicBlock * getLatch() const
Reaching the latch indicates the end of the loop body code.
OpenMPIRBuilder::InsertPointTy getPreheaderIP() const
Return the insertion point for user code before the loop.
BasicBlock * getCond() const
The condition block computes whether there is another loop iteration.
BasicBlock * getExit() const
Reaching the exit indicates no more iterations are being executed.
BasicBlock * getPreheader() const
The preheader ensures that there is only a single edge entering the loop.
Instruction * getIndVar() const
Returns the instruction representing the current logical induction variable.
This is the shared class of boolean and integer constants.
Definition: Constants.h:83
This is an important base class in LLVM.
Definition: Constant.h:42
This class represents an Operation in the Expression.
A debug info location.
Definition: DebugLoc.h:33
Lightweight error class with error context and mandatory checking.
Definition: Error.h:160
Tagged union holding either a T or a Error.
Definition: Error.h:481
A handy container for a FunctionType+Callee-pointer pair, which can be passed around as a single enti...
Definition: DerivedTypes.h:170
LinkageTypes
An enumeration for the kinds of linkage for global values.
Definition: GlobalValue.h:51
InsertPoint - A saved insertion point.
Definition: IRBuilder.h:276
BasicBlock * getBlock() const
Definition: IRBuilder.h:291
Common base class shared among various IRBuilders.
Definition: IRBuilder.h:113
AllocaInst * CreateAlloca(Type *Ty, unsigned AddrSpace, Value *ArraySize=nullptr, const Twine &Name="")
Definition: IRBuilder.h:1780
void SetCurrentDebugLocation(DebugLoc L)
Set location information used by debugging information.
Definition: IRBuilder.h:239
InsertPoint saveIP() const
Returns the current insert point.
Definition: IRBuilder.h:296
void restoreIP(InsertPoint IP)
Sets the current insert point to a previously-saved location.
Definition: IRBuilder.h:308
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition: IRBuilder.h:2704
Class to represent integer types.
Definition: DerivedTypes.h:42
Represents a single loop in the control flow graph.
Definition: LoopInfo.h:39
This class implements a map that also provides access to all stored values in a deterministic order.
Definition: MapVector.h:36
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
OffloadEntryInfoDeviceGlobalVar(unsigned Order, OMPTargetGlobalVarEntryKind Flags)
Definition: OMPIRBuilder.h:398
OffloadEntryInfoDeviceGlobalVar(unsigned Order, Constant *Addr, int64_t VarSize, OMPTargetGlobalVarEntryKind Flags, GlobalValue::LinkageTypes Linkage, const std::string &VarName)
Definition: OMPIRBuilder.h:401
static bool classof(const OffloadEntryInfo *Info)
Definition: OMPIRBuilder.h:416
static bool classof(const OffloadEntryInfo *Info)
Definition: OMPIRBuilder.h:323
OffloadEntryInfoTargetRegion(unsigned Order, Constant *Addr, Constant *ID, OMPTargetRegionEntryKind Flags)
Definition: OMPIRBuilder.h:310
@ OffloadingEntryInfoTargetRegion
Entry is a target region.
Definition: OMPIRBuilder.h:244
@ OffloadingEntryInfoDeviceGlobalVar
Entry is a declare target variable.
Definition: OMPIRBuilder.h:246
OffloadingEntryInfoKinds getKind() const
Definition: OMPIRBuilder.h:262
OffloadEntryInfo(OffloadingEntryInfoKinds Kind)
Definition: OMPIRBuilder.h:253
static bool classof(const OffloadEntryInfo *Info)
Definition: OMPIRBuilder.h:270
OffloadEntryInfo(OffloadingEntryInfoKinds Kind, unsigned Order, uint32_t Flags)
Definition: OMPIRBuilder.h:254
Class that manages information about offload code regions and data.
Definition: OMPIRBuilder.h:232
function_ref< void(StringRef, const OffloadEntryInfoDeviceGlobalVar &)> OffloadDeviceGlobalVarEntryInfoActTy
Applies action Action on all registered entries.
Definition: OMPIRBuilder.h:438
OMPTargetDeviceClauseKind
Kind of device clause for declare target variables and functions NOTE: Currently not used as a part o...
Definition: OMPIRBuilder.h:377
@ OMPTargetDeviceClauseNoHost
The target is marked for non-host devices.
Definition: OMPIRBuilder.h:381
@ OMPTargetDeviceClauseAny
The target is marked for all devices.
Definition: OMPIRBuilder.h:379
@ OMPTargetDeviceClauseNone
The target is marked as having no clause.
Definition: OMPIRBuilder.h:385
@ OMPTargetDeviceClauseHost
The target is marked for host devices.
Definition: OMPIRBuilder.h:383
void registerDeviceGlobalVarEntryInfo(StringRef VarName, Constant *Addr, int64_t VarSize, OMPTargetGlobalVarEntryKind Flags, GlobalValue::LinkageTypes Linkage)
Register device global variable entry.
void initializeDeviceGlobalVarEntryInfo(StringRef Name, OMPTargetGlobalVarEntryKind Flags, unsigned Order)
Initialize device global variable entry.
void actOnDeviceGlobalVarEntriesInfo(const OffloadDeviceGlobalVarEntryInfoActTy &Action)
OMPTargetRegionEntryKind
Kind of the target registry entry.
Definition: OMPIRBuilder.h:297
@ OMPTargetRegionEntryTargetRegion
Mark the entry as target region.
Definition: OMPIRBuilder.h:299
OffloadEntriesInfoManager(OpenMPIRBuilder *builder)
Definition: OMPIRBuilder.h:290
void getTargetRegionEntryFnName(SmallVectorImpl< char > &Name, const TargetRegionEntryInfo &EntryInfo)
bool hasTargetRegionEntryInfo(TargetRegionEntryInfo EntryInfo, bool IgnoreAddressId=false) const
Return true if a target region entry with the provided information exists.
void registerTargetRegionEntryInfo(TargetRegionEntryInfo EntryInfo, Constant *Addr, Constant *ID, OMPTargetRegionEntryKind Flags)
Register target region entry.
void actOnTargetRegionEntriesInfo(const OffloadTargetRegionEntryInfoActTy &Action)
unsigned size() const
Return number of entries defined so far.
Definition: OMPIRBuilder.h:288
void initializeTargetRegionEntryInfo(const TargetRegionEntryInfo &EntryInfo, unsigned Order)
Initialize target region entry.
OMPTargetGlobalVarEntryKind
Kind of the global variable entry..
Definition: OMPIRBuilder.h:357
@ OMPTargetGlobalVarEntryEnter
Mark the entry as a declare target enter.
Definition: OMPIRBuilder.h:363
@ OMPTargetGlobalVarEntryNone
Mark the entry as having no declare target entry kind.
Definition: OMPIRBuilder.h:365
@ OMPTargetGlobalRegisterRequires
Mark the entry as a register requires global.
Definition: OMPIRBuilder.h:369
@ OMPTargetGlobalVarEntryIndirect
Mark the entry as a declare target indirect global.
Definition: OMPIRBuilder.h:367
@ OMPTargetGlobalVarEntryLink
Mark the entry as a to declare target link.
Definition: OMPIRBuilder.h:361
@ OMPTargetGlobalVarEntryTo
Mark the entry as a to declare target.
Definition: OMPIRBuilder.h:359
function_ref< void(const TargetRegionEntryInfo &EntryInfo, const OffloadEntryInfoTargetRegion &)> OffloadTargetRegionEntryInfoActTy
brief Applies action Action on all registered entries.
Definition: OMPIRBuilder.h:348
bool hasDeviceGlobalVarEntryInfo(StringRef VarName) const
Checks if the variable with the given name has been registered already.
Definition: OMPIRBuilder.h:433
bool empty() const
Return true if a there are no entries defined.
Captures attributes that affect generating LLVM-IR using the OpenMPIRBuilder and related classes.
Definition: OMPIRBuilder.h:87
void setIsGPU(bool Value)
Definition: OMPIRBuilder.h:184
std::optional< bool > IsTargetDevice
Flag to define whether to generate code for the role of the OpenMP host (if set to false) or device (...
Definition: OMPIRBuilder.h:93
std::optional< bool > IsGPU
Flag for specifying if the compilation is done for an accelerator.
Definition: OMPIRBuilder.h:103
void setGridValue(omp::GV G)
Definition: OMPIRBuilder.h:189
std::optional< StringRef > FirstSeparator
First separator used between the initial two parts of a name.
Definition: OMPIRBuilder.h:112
StringRef separator() const
Definition: OMPIRBuilder.h:175
int64_t getRequiresFlags() const
Returns requires directive clauses as flags compatible with those expected by libomptarget.
void setFirstSeparator(StringRef FS)
Definition: OMPIRBuilder.h:187
StringRef firstSeparator() const
Definition: OMPIRBuilder.h:165
std::optional< bool > OpenMPOffloadMandatory
Flag for specifying if offloading is mandatory.
Definition: OMPIRBuilder.h:109
std::optional< bool > EmitLLVMUsedMetaInfo
Flag for specifying if LLVMUsed information should be emitted.
Definition: OMPIRBuilder.h:106
omp::GV getGridValue() const
Definition: OMPIRBuilder.h:148
SmallVector< Triple > TargetTriples
When compilation is being done for the OpenMP host (i.e.
Definition: OMPIRBuilder.h:121
void setHasRequiresReverseOffload(bool Value)
bool hasRequiresUnifiedSharedMemory() const
void setHasRequiresUnifiedSharedMemory(bool Value)
std::optional< StringRef > Separator
Separator used between all of the rest consecutive parts of s name.
Definition: OMPIRBuilder.h:114
bool hasRequiresDynamicAllocators() const
bool openMPOffloadMandatory() const
Definition: OMPIRBuilder.h:142
void setHasRequiresUnifiedAddress(bool Value)
void setOpenMPOffloadMandatory(bool Value)
Definition: OMPIRBuilder.h:186
void setIsTargetDevice(bool Value)
Definition: OMPIRBuilder.h:183
void setSeparator(StringRef S)
Definition: OMPIRBuilder.h:188
void setHasRequiresDynamicAllocators(bool Value)
void setEmitLLVMUsed(bool Value=true)
Definition: OMPIRBuilder.h:185
std::optional< omp::GV > GridValue
Definition: OMPIRBuilder.h:117
bool hasRequiresReverseOffload() const
bool hasRequiresUnifiedAddress() const
llvm::AllocaInst * CreateAlloca(llvm::Type *Ty, const llvm::Twine &Name) const override
Definition: OMPIRBuilder.h:496
AtomicInfo(IRBuilder<> *Builder, llvm::Type *Ty, uint64_t AtomicSizeInBits, uint64_t ValueSizeInBits, llvm::Align AtomicAlign, llvm::Align ValueAlign, bool UseLibcall, llvm::Value *AtomicVar)
Definition: OMPIRBuilder.h:487
void decorateWithTBAA(llvm::Instruction *I) override
Definition: OMPIRBuilder.h:495
llvm::Value * getAtomicPointer() const override
Definition: OMPIRBuilder.h:494
Struct that keeps the information that should be kept throughout a 'target data' region.
TargetDataInfo(bool RequiresDevicePointerInfo, bool SeparateBeginEndCalls)
SmallMapVector< const Value *, std::pair< Value *, Value * >, 4 > DevicePtrInfoMap
void clearArrayInfo()
Clear information about the data arrays.
unsigned NumberOfPtrs
The total number of pointers passed to the runtime library.
bool HasNoWait
Whether the target ... data directive has a nowait clause.
bool isValid()
Return true if the current target data information has valid arrays.
bool HasMapper
Indicate whether any user-defined mapper exists.
An interface to create LLVM-IR for OpenMP directives.
Definition: OMPIRBuilder.h:474
InsertPointOrErrorTy createOrderedThreadsSimd(const LocationDescription &Loc, BodyGenCallbackTy BodyGenCB, FinalizeCallbackTy FiniCB, bool IsThreads)
Generator for '#omp ordered [threads | simd]'.
Constant * getOrCreateIdent(Constant *SrcLocStr, uint32_t SrcLocStrSize, omp::IdentFlag Flags=omp::IdentFlag(0), unsigned Reserve2Flags=0)
Return an ident_t* encoding the source location SrcLocStr and Flags.
FunctionCallee getOrCreateRuntimeFunction(Module &M, omp::RuntimeFunction FnID)
Return the function declaration for the runtime function with FnID.
InsertPointOrErrorTy createCancel(const LocationDescription &Loc, Value *IfCondition, omp::Directive CanceledDirective)
Generator for '#omp cancel'.
ReductionGenCBKind
Enum class for the RedctionGen CallBack type to be used.
CanonicalLoopInfo * collapseLoops(DebugLoc DL, ArrayRef< CanonicalLoopInfo * > Loops, InsertPointTy ComputeIP)
Collapse a loop nest into a single loop.
void createTaskyield(const LocationDescription &Loc)
Generator for '#omp taskyield'.
std::function< Error(InsertPointTy CodeGenIP)> FinalizeCallbackTy
Callback type for variable finalization (think destructors).
Definition: OMPIRBuilder.h:543
void emitBranch(BasicBlock *Target)
InsertPointTy createAtomicWrite(const LocationDescription &Loc, AtomicOpValue &X, Value *Expr, AtomicOrdering AO)
Emit atomic write for : X = Expr — Only Scalar data types.
static void writeThreadBoundsForKernel(const Triple &T, Function &Kernel, int32_t LB, int32_t UB)
EvalKind
Enum class for reduction evaluation types scalar, complex and aggregate.
static TargetRegionEntryInfo getTargetEntryUniqueInfo(FileIdentifierInfoCallbackTy CallBack, StringRef ParentName="")
Creates a unique info for a target entry when provided a filename and line number from.
void emitTaskwaitImpl(const LocationDescription &Loc)
Generate a taskwait runtime call.
Constant * registerTargetRegionFunction(TargetRegionEntryInfo &EntryInfo, Function *OutlinedFunction, StringRef EntryFnName, StringRef EntryFnIDName)
Registers the given function and sets up the attribtues of the function Returns the FunctionID.
GlobalVariable * emitKernelExecutionMode(StringRef KernelName, omp::OMPTgtExecModeFlags Mode)
Emit the kernel execution mode.
void initialize()
Initialize the internal state, this will put structures types and potentially other helpers into the ...
void createTargetDeinit(const LocationDescription &Loc, int32_t TeamsReductionDataSize=0, int32_t TeamsReductionBufferLength=1024)
Create a runtime call for kmpc_target_deinit.
InsertPointOrErrorTy createTaskgroup(const LocationDescription &Loc, InsertPointTy AllocaIP, BodyGenCallbackTy BodyGenCB)
Generator for the taskgroup construct.
void loadOffloadInfoMetadata(Module &M)
Loads all the offload entries information from the host IR metadata.
std::function< InsertPointOrErrorTy(InsertPointTy CodeGenIP, Value *LHS, Value *RHS, Value *&Res)> ReductionGenCBTy
ReductionGen CallBack for MLIR.
InsertPointOrErrorTy emitTargetTask(TargetTaskBodyCallbackTy TaskBodyCB, Value *DeviceID, Value *RTLoc, OpenMPIRBuilder::InsertPointTy AllocaIP, const SmallVector< llvm::OpenMPIRBuilder::DependData > &Dependencies, bool HasNoWait)
Generate a target-task for the target construct.
void unrollLoopFull(DebugLoc DL, CanonicalLoopInfo *Loop)
Fully unroll a loop.
void emitFlush(const LocationDescription &Loc)
Generate a flush runtime call.
static std::pair< int32_t, int32_t > readThreadBoundsForKernel(const Triple &T, Function &Kernel)
}
OpenMPIRBuilderConfig Config
The OpenMPIRBuilder Configuration.
CallInst * createOMPInteropDestroy(const LocationDescription &Loc, Value *InteropVar, Value *Device, Value *NumDependences, Value *DependenceAddress, bool HaveNowaitClause)
Create a runtime call for __tgt_interop_destroy.
InsertPointTy createAtomicRead(const LocationDescription &Loc, AtomicOpValue &X, AtomicOpValue &V, AtomicOrdering AO)
Emit atomic Read for : V = X — Only Scalar data types.
Error emitIfClause(Value *Cond, BodyGenCallbackTy ThenGen, BodyGenCallbackTy ElseGen, InsertPointTy AllocaIP={})
Emits code for OpenMP 'if' clause using specified BodyGenCallbackTy Here is the logic: if (Cond) { Th...
std::function< void(EmitMetadataErrorKind, TargetRegionEntryInfo)> EmitMetadataErrorReportFunctionTy
Callback function type.
void emitUsed(StringRef Name, ArrayRef< llvm::WeakTrackingVH > List)
Emit the llvm.used metadata.
void setConfig(OpenMPIRBuilderConfig C)
Definition: OMPIRBuilder.h:509
InsertPointOrErrorTy createSingle(const LocationDescription &Loc, BodyGenCallbackTy BodyGenCB, FinalizeCallbackTy FiniCB, bool IsNowait, ArrayRef< llvm::Value * > CPVars={}, ArrayRef< llvm::Function * > CPFuncs={})
Generator for '#omp single'.
InsertPointOrErrorTy createTeams(const LocationDescription &Loc, BodyGenCallbackTy BodyGenCB, Value *NumTeamsLower=nullptr, Value *NumTeamsUpper=nullptr, Value *ThreadLimit=nullptr, Value *IfExpr=nullptr)
Generator for #omp teams
std::forward_list< CanonicalLoopInfo > LoopInfos
Collection of owned canonical loop objects that eventually need to be free'd.
void createTaskwait(const LocationDescription &Loc)
Generator for '#omp taskwait'.
CanonicalLoopInfo * createLoopSkeleton(DebugLoc DL, Value *TripCount, Function *F, BasicBlock *PreInsertBefore, BasicBlock *PostInsertBefore, const Twine &Name={})
Create the control flow structure of a canonical OpenMP loop.
std::string createPlatformSpecificName(ArrayRef< StringRef > Parts) const
Get the create a name using the platform specific separators.
FunctionCallee createDispatchNextFunction(unsigned IVSize, bool IVSigned)
Returns __kmpc_dispatch_next_* runtime function for the specified size IVSize and sign IVSigned.
static void getKernelArgsVector(TargetKernelArgs &KernelArgs, IRBuilderBase &Builder, SmallVector< Value * > &ArgsVector)
Create the kernel args vector used by emitTargetKernel.
void unrollLoopHeuristic(DebugLoc DL, CanonicalLoopInfo *Loop)
Fully or partially unroll a loop.
InsertPointOrErrorTy createParallel(const LocationDescription &Loc, InsertPointTy AllocaIP, BodyGenCallbackTy BodyGenCB, PrivatizeCallbackTy PrivCB, FinalizeCallbackTy FiniCB, Value *IfCondition, Value *NumThreads, omp::ProcBindKind ProcBind, bool IsCancellable)
Generator for '#omp parallel'.
omp::OpenMPOffloadMappingFlags getMemberOfFlag(unsigned Position)
Get OMP_MAP_MEMBER_OF flag with extra bits reserved based on the position given.
void addAttributes(omp::RuntimeFunction FnID, Function &Fn)
Add attributes known for FnID to Fn.
Module & M
The underlying LLVM-IR module.
StringMap< Constant * > SrcLocStrMap
Map to remember source location strings.
void createMapperAllocas(const LocationDescription &Loc, InsertPointTy AllocaIP, unsigned NumOperands, struct MapperAllocas &MapperAllocas)
Create the allocas instruction used in call to mapper functions.
Constant * getOrCreateSrcLocStr(StringRef LocStr, uint32_t &SrcLocStrSize)
Return the (LLVM-IR) string describing the source location LocStr.
void addOutlineInfo(OutlineInfo &&OI)
Add a new region that will be outlined later.
Error emitTargetRegionFunction(TargetRegionEntryInfo &EntryInfo, FunctionGenCallback &GenerateFunctionCallback, bool IsOffloadEntry, Function *&OutlinedFn, Constant *&OutlinedFnID)
Create a unique name for the entry function using the source location information of the current targ...
FunctionCallee createDispatchFiniFunction(unsigned IVSize, bool IVSigned)
Returns __kmpc_dispatch_fini_* runtime function for the specified size IVSize and sign IVSigned.
void unrollLoopPartial(DebugLoc DL, CanonicalLoopInfo *Loop, int32_t Factor, CanonicalLoopInfo **UnrolledCLI)
Partially unroll a loop.
void emitTaskyieldImpl(const LocationDescription &Loc)
Generate a taskyield runtime call.
void emitMapperCall(const LocationDescription &Loc, Function *MapperFunc, Value *SrcLocInfo, Value *MaptypesArg, Value *MapnamesArg, struct MapperAllocas &MapperAllocas, int64_t DeviceID, unsigned NumOperands)
Create the call for the target mapper function.
std::function< Error(InsertPointTy AllocaIP, InsertPointTy CodeGenIP)> StorableBodyGenCallbackTy
Definition: OMPIRBuilder.h:606
InsertPointTy createAtomicCompare(const LocationDescription &Loc, AtomicOpValue &X, AtomicOpValue &V, AtomicOpValue &R, Value *E, Value *D, AtomicOrdering AO, omp::OMPAtomicCompareOp Op, bool IsXBinopExpr, bool IsPostfixUpdate, bool IsFailOnly)
Emit atomic compare for constructs: — Only scalar data types cond-expr-stmt: x = x ordop expr ?...
InsertPointOrErrorTy createAtomicCapture(const LocationDescription &Loc, InsertPointTy AllocaIP, AtomicOpValue &X, AtomicOpValue &V, Value *Expr, AtomicOrdering AO, AtomicRMWInst::BinOp RMWOp, AtomicUpdateCallbackTy &UpdateOp, bool UpdateExpr, bool IsPostfixUpdate, bool IsXBinopExpr)
Emit atomic update for constructs: — Only Scalar data types V = X; X = X BinOp Expr ,...
InsertPointTy createOrderedDepend(const LocationDescription &Loc, InsertPointTy AllocaIP, unsigned NumLoops, ArrayRef< llvm::Value * > StoreValues, const Twine &Name, bool IsDependSource)
Generator for '#omp ordered depend (source | sink)'.
InsertPointTy createCopyinClauseBlocks(InsertPointTy IP, Value *MasterAddr, Value *PrivateAddr, llvm::IntegerType *IntPtrTy, bool BranchtoEnd=true)
Generate conditional branch and relevant BasicBlocks through which private threads copy the 'copyin' ...
void emitOffloadingArrays(InsertPointTy AllocaIP, InsertPointTy CodeGenIP, MapInfosTy &CombinedInfo, TargetDataInfo &Info, bool IsNonContiguous=false, function_ref< void(unsigned int, Value *)> DeviceAddrCB=nullptr, function_ref< Value *(unsigned int)> CustomMapperCB=nullptr)
Emit the arrays used to pass the captures and map information to the offloading runtime library.
SmallVector< FinalizationInfo, 8 > FinalizationStack
The finalization stack made up of finalize callbacks currently in-flight, wrapped into FinalizationIn...
std::vector< CanonicalLoopInfo * > tileLoops(DebugLoc DL, ArrayRef< CanonicalLoopInfo * > Loops, ArrayRef< Value * > TileSizes)
Tile a loop nest.
CallInst * createOMPInteropInit(const LocationDescription &Loc, Value *InteropVar, omp::OMPInteropType InteropType, Value *Device, Value *NumDependences, Value *DependenceAddress, bool HaveNowaitClause)
Create a runtime call for __tgt_interop_init.
SmallVector< OutlineInfo, 16 > OutlineInfos
Collection of regions that need to be outlined during finalization.
Function * getOrCreateRuntimeFunctionPtr(omp::RuntimeFunction FnID)
std::function< InsertPointOrErrorTy(InsertPointTy, Type *, Value *, Value *)> ReductionGenAtomicCBTy
Functions used to generate atomic reductions.
InsertPointTy createTargetInit(const LocationDescription &Loc, const llvm::OpenMPIRBuilder::TargetKernelDefaultAttrs &Attrs)
The omp target interface.
const Triple T
The target triple of the underlying module.
DenseMap< std::pair< Constant *, uint64_t >, Constant * > IdentMap
Map to remember existing ident_t*.
CallInst * createOMPFree(const LocationDescription &Loc, Value *Addr, Value *Allocator, std::string Name="")
Create a runtime call for kmpc_free.
FunctionCallee createForStaticInitFunction(unsigned IVSize, bool IVSigned, bool IsGPUDistribute)
Returns __kmpc_for_static_init_* runtime function for the specified size IVSize and sign IVSigned.
CallInst * createOMPAlloc(const LocationDescription &Loc, Value *Size, Value *Allocator, std::string Name="")
Create a runtime call for kmpc_Alloc.
void emitNonContiguousDescriptor(InsertPointTy AllocaIP, InsertPointTy CodeGenIP, MapInfosTy &CombinedInfo, TargetDataInfo &Info)
Emit an array of struct descriptors to be assigned to the offload args.
InsertPointOrErrorTy createSection(const LocationDescription &Loc, BodyGenCallbackTy BodyGenCB, FinalizeCallbackTy FiniCB)
Generator for '#omp section'.
std::function< InsertPointTy(InsertPointTy CodeGenIP, unsigned Index, Value **LHS, Value **RHS, Function *CurFn)> ReductionGenClangCBTy
ReductionGen CallBack for Clang.
void emitBlock(BasicBlock *BB, Function *CurFn, bool IsFinished=false)
Value * getOrCreateThreadID(Value *Ident)
Return the current thread ID.
void emitOffloadingArraysAndArgs(InsertPointTy AllocaIP, InsertPointTy CodeGenIP, TargetDataInfo &Info, TargetDataRTArgs &RTArgs, MapInfosTy &CombinedInfo, bool IsNonContiguous=false, bool ForEndCall=false, function_ref< void(unsigned int, Value *)> DeviceAddrCB=nullptr, function_ref< Value *(unsigned int)> CustomMapperCB=nullptr)
Allocates memory for and populates the arrays required for offloading (offload_{baseptrs|ptrs|mappers...
InsertPointOrErrorTy createMaster(const LocationDescription &Loc, BodyGenCallbackTy BodyGenCB, FinalizeCallbackTy FiniCB)
Generator for '#omp master'.
void pushFinalizationCB(const FinalizationInfo &FI)
Push a finalization callback on the finalization stack.
Definition: OMPIRBuilder.h:561
Error emitCancelationCheckImpl(Value *CancelFlag, omp::Directive CanceledDirective, FinalizeCallbackTy ExitCB={})
Generate control flow and cleanup for cancellation.
InsertPointOrErrorTy emitKernelLaunch(const LocationDescription &Loc, Value *OutlinedFnID, EmitFallbackCallbackTy EmitTargetCallFallbackCB, TargetKernelArgs &Args, Value *DeviceID, Value *RTLoc, InsertPointTy AllocaIP)
Generate a target region entry call and host fallback call.
InsertPointTy getInsertionPoint()
}
StringMap< GlobalVariable *, BumpPtrAllocator > InternalVars
An ordered map of auto-generated variables to their unique names.
GlobalVariable * getOrCreateInternalVariable(Type *Ty, const StringRef &Name, unsigned AddressSpace=0)
Gets (if variable with the given name already exist) or creates internal global variable with the spe...
InsertPointOrErrorTy createReductionsGPU(const LocationDescription &Loc, InsertPointTy AllocaIP, InsertPointTy CodeGenIP, ArrayRef< ReductionInfo > ReductionInfos, bool IsNoWait=false, bool IsTeamsReduction=false, bool HasDistribute=false, ReductionGenCBKind ReductionGenCBKind=ReductionGenCBKind::MLIR, std::optional< omp::GV > GridValue={}, unsigned ReductionBufNum=1024, Value *SrcLocInfo=nullptr)
Design of OpenMP reductions on the GPU.
FunctionCallee createDispatchInitFunction(unsigned IVSize, bool IVSigned)
Returns __kmpc_dispatch_init_* runtime function for the specified size IVSize and sign IVSigned.
Function * emitUserDefinedMapper(function_ref< MapInfosTy &(InsertPointTy CodeGenIP, llvm::Value *PtrPHI, llvm::Value *BeginArg)> PrivAndGenMapInfoCB, llvm::Type *ElemTy, StringRef FuncName, function_ref< bool(unsigned int, Function **)> CustomMapperCB=nullptr)
Emit the user-defined mapper function.
CallInst * createOMPInteropUse(const LocationDescription &Loc, Value *InteropVar, Value *Device, Value *NumDependences, Value *DependenceAddress, bool HaveNowaitClause)
Create a runtime call for __tgt_interop_use.
IRBuilder<>::InsertPoint InsertPointTy
Type used throughout for insertion points.
Definition: OMPIRBuilder.h:520
InsertPointOrErrorTy createReductions(const LocationDescription &Loc, InsertPointTy AllocaIP, ArrayRef< ReductionInfo > ReductionInfos, ArrayRef< bool > IsByRef, bool IsNoWait=false)
Generator for '#omp reduction'.
GlobalVariable * createOffloadMapnames(SmallVectorImpl< llvm::Constant * > &Names, std::string VarName)
Create the global variable holding the offload names information.
InsertPointOrErrorTy createTask(const LocationDescription &Loc, InsertPointTy AllocaIP, BodyGenCallbackTy BodyGenCB, bool Tied=true, Value *Final=nullptr, Value *IfCondition=nullptr, SmallVector< DependData > Dependencies={}, bool Mergeable=false, Value *EventHandle=nullptr)
Generator for #omp task
std::function< Expected< Function * >(StringRef FunctionName)> FunctionGenCallback
Functions used to generate a function with the given name.
static void writeTeamsForKernel(const Triple &T, Function &Kernel, int32_t LB, int32_t UB)
InsertPointOrErrorTy createBarrier(const LocationDescription &Loc, omp::Directive Kind, bool ForceSimpleCall=false, bool CheckCancelFlag=true)
Emitter methods for OpenMP directives.
void setCorrectMemberOfFlag(omp::OpenMPOffloadMappingFlags &Flags, omp::OpenMPOffloadMappingFlags MemberOfFlag)
Given an initial flag set, this function modifies it to contain the passed in MemberOfFlag generated ...
Constant * getOrCreateDefaultSrcLocStr(uint32_t &SrcLocStrSize)
Return the (LLVM-IR) string describing the default source location.
InsertPointOrErrorTy createCritical(const LocationDescription &Loc, BodyGenCallbackTy BodyGenCB, FinalizeCallbackTy FiniCB, StringRef CriticalName, Value *HintInst)
Generator for '#omp critical'.
void createOffloadEntry(Constant *ID, Constant *Addr, uint64_t Size, int32_t Flags, GlobalValue::LinkageTypes, StringRef Name="")
Creates offloading entry for the provided entry ID ID, address Addr, size Size, and flags Flags.
static unsigned getOpenMPDefaultSimdAlign(const Triple &TargetTriple, const StringMap< bool > &Features)
Get the default alignment value for given target.
unsigned getFlagMemberOffset()
Get the offset of the OMP_MAP_MEMBER_OF field.
void createOffloadEntriesAndInfoMetadata(EmitMetadataErrorReportFunctionTy &ErrorReportFunction)
void applySimd(CanonicalLoopInfo *Loop, MapVector< Value *, Value * > AlignedVars, Value *IfCond, omp::OrderKind Order, ConstantInt *Simdlen, ConstantInt *Safelen)
Add metadata to simd-ize a loop.
bool isLastFinalizationInfoCancellable(omp::Directive DK)
Return true if the last entry in the finalization stack is of kind DK and cancellable.
InsertPointTy emitTargetKernel(const LocationDescription &Loc, InsertPointTy AllocaIP, Value *&Return, Value *Ident, Value *DeviceID, Value *NumTeams, Value *NumThreads, Value *HostPtr, ArrayRef< Value * > KernelArgs)
Generate a target region entry call.
GlobalVariable * createOffloadMaptypes(SmallVectorImpl< uint64_t > &Mappings, std::string VarName)
Create the global variable holding the offload mappings information.
CallInst * createCachedThreadPrivate(const LocationDescription &Loc, llvm::Value *Pointer, llvm::ConstantInt *Size, const llvm::Twine &Name=Twine(""))
Create a runtime call for kmpc_threadprivate_cached.
IRBuilder Builder
The LLVM-IR Builder used to create IR.
GlobalValue * createGlobalFlag(unsigned Value, StringRef Name)
Create a hidden global flag Name in the module with initial value Value.
InsertPointOrErrorTy applyWorkshareLoop(DebugLoc DL, CanonicalLoopInfo *CLI, InsertPointTy AllocaIP, bool NeedsBarrier, llvm::omp::ScheduleKind SchedKind=llvm::omp::OMP_SCHEDULE_Default, Value *ChunkSize=nullptr, bool HasSimdModifier=false, bool HasMonotonicModifier=false, bool HasNonmonotonicModifier=false, bool HasOrderedClause=false, omp::WorksharingLoopType LoopType=omp::WorksharingLoopType::ForStaticLoop)
Modifies the canonical loop to be a workshare loop.
void emitOffloadingArraysArgument(IRBuilderBase &Builder, OpenMPIRBuilder::TargetDataRTArgs &RTArgs, OpenMPIRBuilder::TargetDataInfo &Info, bool ForEndCall=false)
Emit the arguments to be passed to the runtime library based on the arrays of base pointers,...
InsertPointOrErrorTy createMasked(const LocationDescription &Loc, BodyGenCallbackTy BodyGenCB, FinalizeCallbackTy FiniCB, Value *Filter)
Generator for '#omp masked'.
Expected< CanonicalLoopInfo * > createCanonicalLoop(const LocationDescription &Loc, LoopBodyGenCallbackTy BodyGenCB, Value *TripCount, const Twine &Name="loop")
Generator for the control flow structure of an OpenMP canonical loop.
Value * getSizeInBytes(Value *BasePtr)
Computes the size of type in bytes.
function_ref< InsertPointOrErrorTy(InsertPointTy AllocaIP, InsertPointTy CodeGenIP, Value &Original, Value &Inner, Value *&ReplVal)> PrivatizeCallbackTy
Callback type for variable privatization (think copy & default constructor).
Definition: OMPIRBuilder.h:641
OpenMPIRBuilder(Module &M)
Create a new OpenMPIRBuilder operating on the given module M.
Definition: OMPIRBuilder.h:478
FunctionCallee createDispatchDeinitFunction()
Returns __kmpc_dispatch_deinit runtime function.
void registerTargetGlobalVariable(OffloadEntriesInfoManager::OMPTargetGlobalVarEntryKind CaptureClause, OffloadEntriesInfoManager::OMPTargetDeviceClauseKind DeviceClause, bool IsDeclaration, bool IsExternallyVisible, TargetRegionEntryInfo EntryInfo, StringRef MangledName, std::vector< GlobalVariable * > &GeneratedRefs, bool OpenMPSIMD, std::vector< Triple > TargetTriple, std::function< Constant *()> GlobalInitializer, std::function< GlobalValue::LinkageTypes()> VariableLinkage, Type *LlvmPtrTy, Constant *Addr)
Registers a target variable for device or host.
InsertPointOrErrorTy createTargetData(const LocationDescription &Loc, InsertPointTy AllocaIP, InsertPointTy CodeGenIP, Value *DeviceID, Value *IfCond, TargetDataInfo &Info, GenMapInfoCallbackTy GenMapInfoCB, omp::RuntimeFunction *MapperFunc=nullptr, function_ref< InsertPointOrErrorTy(InsertPointTy CodeGenIP, BodyGenTy BodyGenType)> BodyGenCB=nullptr, function_ref< void(unsigned int, Value *)> DeviceAddrCB=nullptr, function_ref< Value *(unsigned int)> CustomMapperCB=nullptr, Value *SrcLocInfo=nullptr)
Generator for '#omp target data'.
BodyGenTy
Type of BodyGen to use for region codegen.
InsertPointOrErrorTy createAtomicUpdate(const LocationDescription &Loc, InsertPointTy AllocaIP, AtomicOpValue &X, Value *Expr, AtomicOrdering AO, AtomicRMWInst::BinOp RMWOp, AtomicUpdateCallbackTy &UpdateOp, bool IsXBinopExpr)
Emit atomic update for constructs: X = X BinOp Expr ,or X = Expr BinOp X For complex Operations: X = ...
InsertPointOrErrorTy createTarget(const LocationDescription &Loc, bool IsOffloadEntry, OpenMPIRBuilder::InsertPointTy AllocaIP, OpenMPIRBuilder::InsertPointTy CodeGenIP, TargetRegionEntryInfo &EntryInfo, const TargetKernelDefaultAttrs &DefaultAttrs, const TargetKernelRuntimeAttrs &RuntimeAttrs, SmallVectorImpl< Value * > &Inputs, GenMapInfoCallbackTy GenMapInfoCB, TargetBodyGenCallbackTy BodyGenCB, TargetGenArgAccessorsCallbackTy ArgAccessorFuncCB, SmallVector< DependData > Dependencies={}, bool HasNowait=false)
Generator for '#omp target'.
SmallVector< llvm::Function *, 16 > ConstantAllocaRaiseCandidates
A collection of candidate target functions that's constant allocas will attempt to be raised on a cal...
OffloadEntriesInfoManager OffloadInfoManager
Info manager to keep track of target regions.
static std::pair< int32_t, int32_t > readTeamBoundsForKernel(const Triple &T, Function &Kernel)
Read/write a bounds on teams for Kernel.
std::function< std::tuple< std::string, uint64_t >()> FileIdentifierInfoCallbackTy
const std::string ompOffloadInfoName
OMP Offload Info Metadata name string.
Expected< InsertPointTy > InsertPointOrErrorTy
Type used to represent an insertion point or an error value.
Definition: OMPIRBuilder.h:523
InsertPointTy createCopyPrivate(const LocationDescription &Loc, llvm::Value *BufSize, llvm::Value *CpyBuf, llvm::Value *CpyFn, llvm::Value *DidIt)
Generator for __kmpc_copyprivate.
void popFinalizationCB()
Pop the last finalization callback from the finalization stack.
Definition: OMPIRBuilder.h:568
InsertPointOrErrorTy createSections(const LocationDescription &Loc, InsertPointTy AllocaIP, ArrayRef< StorableBodyGenCallbackTy > SectionCBs, PrivatizeCallbackTy PrivCB, FinalizeCallbackTy FiniCB, bool IsCancellable, bool IsNowait)
Generator for '#omp sections'.
function_ref< Error(InsertPointTy AllocaIP, InsertPointTy CodeGenIP)> BodyGenCallbackTy
Callback type for body (=inner region) code generation.
Definition: OMPIRBuilder.h:597
bool updateToLocation(const LocationDescription &Loc)
Update the internal location to Loc.
void createFlush(const LocationDescription &Loc)
Generator for '#omp flush'.
Constant * getAddrOfDeclareTargetVar(OffloadEntriesInfoManager::OMPTargetGlobalVarEntryKind CaptureClause, OffloadEntriesInfoManager::OMPTargetDeviceClauseKind DeviceClause, bool IsDeclaration, bool IsExternallyVisible, TargetRegionEntryInfo EntryInfo, StringRef MangledName, std::vector< GlobalVariable * > &GeneratedRefs, bool OpenMPSIMD, std::vector< Triple > TargetTriple, Type *LlvmPtrTy, std::function< Constant *()> GlobalInitializer, std::function< GlobalValue::LinkageTypes()> VariableLinkage)
Retrieve (or create if non-existent) the address of a declare target variable, used in conjunction wi...
EmitMetadataErrorKind
The kind of errors that can occur when emitting the offload entries and metadata.
A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...
Definition: SmallPtrSet.h:363
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: SmallVector.h:573
void append(ItTy in_start, ItTy in_end)
Add the specified range to the end of the SmallVector.
Definition: SmallVector.h:683
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1196
StringMap - This is an unconventional map that is specialized for handling keys that are "strings",...
Definition: StringMap.h:128
size_type count(StringRef Key) const
count - Return 1 if the element is in the map, 0 otherwise.
Definition: StringMap.h:276
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:51
Target - Wrapper for Target specific information.
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
Value * getOperand(unsigned i) const
Definition: User.h:228
LLVM Value Representation.
Definition: Value.h:74
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:255
void setName(const Twine &Name)
Change the name of the value.
Definition: Value.cpp:377
Value handle that is nullable, but tries to track the Value.
Definition: ValueHandle.h:204
bool pointsToAliveValue() const
Definition: ValueHandle.h:224
An efficient, type-erasing, non-owning reference to a callable.
@ C
The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
@ BasicBlock
Various leaf nodes.
Definition: ISDOpcodes.h:71
OpenMPOffloadMappingFlags
Values for bit flags used to specify the mapping type for offloading.
Definition: OMPConstants.h:195
IdentFlag
IDs for all omp runtime library ident_t flag encodings (see their defintion in openmp/runtime/src/kmp...
Definition: OMPConstants.h:65
RTLDependenceKindTy
Dependence kind for RTL.
Definition: OMPConstants.h:273
RuntimeFunction
IDs for all omp runtime library (RTL) functions.
Definition: OMPConstants.h:45
WorksharingLoopType
A type of worksharing loop construct.
Definition: OMPConstants.h:283
OMPAtomicCompareOp
Atomic compare operations. Currently OpenMP only supports ==, >, and <.
Definition: OMPConstants.h:267
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
BasicBlock * splitBBWithSuffix(IRBuilderBase &Builder, bool CreateBranch, llvm::Twine Suffix=".split")
Like splitBB, but reuses the current block's name for the new name.
@ Offset
Definition: DWP.cpp:480
void spliceBB(IRBuilderBase::InsertPoint IP, BasicBlock *New, bool CreateBranch)
Move the instruction after an InsertPoint to the beginning of another BasicBlock.
BasicBlock * splitBB(IRBuilderBase::InsertPoint IP, bool CreateBranch, llvm::Twine Name={})
Split a BasicBlock at an InsertPoint, even if the block is degenerate (missing the terminator).
AtomicOrdering
Atomic ordering for LLVM's memory model.
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition: Alignment.h:39
a struct to pack relevant information while generating atomic Ops
A struct to pack the relevant information for an OpenMP depend clause.
DependData(omp::RTLDependenceKindTy DepKind, Type *DepValueType, Value *DepVal)
omp::RTLDependenceKindTy DepKind
bool IsCancellable
Flag to indicate if the directive is cancellable.
Definition: OMPIRBuilder.h:555
FinalizeCallbackTy FiniCB
The finalization callback provided by the last in-flight invocation of createXXXX for the directive o...
Definition: OMPIRBuilder.h:548
omp::Directive DK
The directive kind of the innermost directive that has an associated region which might require final...
Definition: OMPIRBuilder.h:552
Description of a LLVM-IR insertion point (IP) and a debug/source location (filename,...
Definition: OMPIRBuilder.h:645
LocationDescription(const InsertPointTy &IP)
Definition: OMPIRBuilder.h:648
LocationDescription(const InsertPointTy &IP, const DebugLoc &DL)
Definition: OMPIRBuilder.h:649
LocationDescription(const IRBuilderBase &IRB)
Definition: OMPIRBuilder.h:646
This structure contains combined information generated for mappable clauses, including base pointers,...
void append(MapInfosTy &CurInfo)
Append arrays in CurInfo.
MapDeviceInfoArrayTy DevicePointers
StructNonContiguousInfo NonContigInfo
Helper that contains information about regions we need to outline during finalization.
void collectBlocks(SmallPtrSetImpl< BasicBlock * > &BlockSet, SmallVectorImpl< BasicBlock * > &BlockVector)
Collect all blocks in between EntryBB and ExitBB in both the given vector and set.
Function * getFunction() const
Return the function that contains the region to be outlined.
SmallVector< Value *, 2 > ExcludeArgsFromAggregate
std::function< void(Function &)> PostOutlineCBTy
Information about an OpenMP reduction.
EvalKind EvaluationKind
Reduction evaluation kind - scalar, complex or aggregate.
ReductionGenAtomicCBTy AtomicReductionGen
Callback for generating the atomic reduction body, may be null.
ReductionGenCBTy ReductionGen
Callback for generating the reduction body.
ReductionInfo(Value *PrivateVariable)
Value * Variable
Reduction variable of pointer type.
Value * PrivateVariable
Thread-private partial reduction variable.
ReductionInfo(Type *ElementType, Value *Variable, Value *PrivateVariable, EvalKind EvaluationKind, ReductionGenCBTy ReductionGen, ReductionGenClangCBTy ReductionGenClang, ReductionGenAtomicCBTy AtomicReductionGen)
ReductionGenClangCBTy ReductionGenClang
Clang callback for generating the reduction body.
Type * ElementType
Reduction element type, must match pointee type of variable.
Container for the arguments used to pass data to the runtime library.
Value * SizesArray
The array of sizes passed to the runtime library.
TargetDataRTArgs(Value *BasePointersArray, Value *PointersArray, Value *SizesArray, Value *MapTypesArray, Value *MapTypesArrayEnd, Value *MappersArray, Value *MapNamesArray)
Value * PointersArray
The array of section pointers passed to the runtime library.
Value * MappersArray
The array of user-defined mappers passed to the runtime library.
Value * MapTypesArrayEnd
The array of map types passed to the runtime library for the end of the region, or nullptr if there a...
Value * BasePointersArray
The array of base pointer passed to the runtime library.
Value * MapTypesArray
The array of map types passed to the runtime library for the beginning of the region or for the entir...
Value * MapNamesArray
The array of original declaration names of mapped pointers sent to the runtime library for debugging.
Data structure that contains the needed information to construct the kernel args vector.
TargetKernelArgs(unsigned NumTargetItems, TargetDataRTArgs RTArgs, Value *NumIterations, ArrayRef< Value * > NumTeams, ArrayRef< Value * > NumThreads, Value *DynCGGroupMem, bool HasNoWait)
Value * DynCGGroupMem
The size of the dynamic shared memory.
ArrayRef< Value * > NumThreads
The number of threads.
TargetDataRTArgs RTArgs
Arguments passed to the runtime library.
Value * NumIterations
The number of iterations.
unsigned NumTargetItems
Number of arguments passed to the runtime library.
bool HasNoWait
True if the kernel has 'no wait' clause.
ArrayRef< Value * > NumTeams
The number of teams.
Container to pass the default attributes with which a kernel must be launched, used to set kernel att...
Container to pass LLVM IR runtime values or constants related to the number of teams and threads with...
Value * MaxThreads
'parallel' construct 'num_threads' clause value, if present and it is an SPMD kernel.
Value * LoopTripCount
Total number of iterations of the SPMD or Generic-SPMD kernel or null if it is a generic kernel.
A MapVector that performs no allocations if smaller than a certain size.
Definition: MapVector.h:254
Data structure to contain the information needed to uniquely identify a target entry.
Definition: OMPIRBuilder.h:203
static void getTargetRegionEntryFnName(SmallVectorImpl< char > &Name, StringRef ParentName, unsigned DeviceID, unsigned FileID, unsigned Line, unsigned Count)
static constexpr const char * KernelNamePrefix
The prefix used for kernel names.
Definition: OMPIRBuilder.h:205
bool operator<(const TargetRegionEntryInfo &RHS) const
Definition: OMPIRBuilder.h:224
TargetRegionEntryInfo(StringRef ParentName, unsigned DeviceID, unsigned FileID, unsigned Line, unsigned Count=0)
Definition: OMPIRBuilder.h:214
Defines various target-specific GPU grid values that must be consistent between host RTL (plugin),...
Definition: OMPGridValues.h:57