LLVM 17.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
19#include "llvm/IR/DebugLoc.h"
20#include "llvm/IR/IRBuilder.h"
22#include <forward_list>
23#include <map>
24#include <optional>
25
26namespace llvm {
27class CanonicalLoopInfo;
28struct TargetRegionEntryInfo;
29class OffloadEntriesInfoManager;
30class OpenMPIRBuilder;
31
32/// Move the instruction after an InsertPoint to the beginning of another
33/// BasicBlock.
34///
35/// The instructions after \p IP are moved to the beginning of \p New which must
36/// not have any PHINodes. If \p CreateBranch is true, a branch instruction to
37/// \p New will be added such that there is no semantic change. Otherwise, the
38/// \p IP insert block remains degenerate and it is up to the caller to insert a
39/// terminator.
40void spliceBB(IRBuilderBase::InsertPoint IP, BasicBlock *New,
41 bool CreateBranch);
42
43/// Splice a BasicBlock at an IRBuilder's current insertion point. Its new
44/// insert location will stick to after the instruction before the insertion
45/// point (instead of moving with the instruction the InsertPoint stores
46/// internally).
47void spliceBB(IRBuilder<> &Builder, BasicBlock *New, bool CreateBranch);
48
49/// Split a BasicBlock at an InsertPoint, even if the block is degenerate
50/// (missing the terminator).
51///
52/// llvm::SplitBasicBlock and BasicBlock::splitBasicBlock require a well-formed
53/// BasicBlock. \p Name is used for the new successor block. If \p CreateBranch
54/// is true, a branch to the new successor will new created such that
55/// semantically there is no change; otherwise the block of the insertion point
56/// remains degenerate and it is the caller's responsibility to insert a
57/// terminator. Returns the new successor block.
58BasicBlock *splitBB(IRBuilderBase::InsertPoint IP, bool CreateBranch,
59 llvm::Twine Name = {});
60
61/// Split a BasicBlock at \p Builder's insertion point, even if the block is
62/// degenerate (missing the terminator). Its new insert location will stick to
63/// after the instruction before the insertion point (instead of moving with the
64/// instruction the InsertPoint stores internally).
65BasicBlock *splitBB(IRBuilderBase &Builder, bool CreateBranch,
66 llvm::Twine Name = {});
67
68/// Split a BasicBlock at \p Builder's insertion point, even if the block is
69/// degenerate (missing the terminator). Its new insert location will stick to
70/// after the instruction before the insertion point (instead of moving with the
71/// instruction the InsertPoint stores internally).
72BasicBlock *splitBB(IRBuilder<> &Builder, bool CreateBranch, llvm::Twine Name);
73
74/// Like splitBB, but reuses the current block's name for the new name.
75BasicBlock *splitBBWithSuffix(IRBuilderBase &Builder, bool CreateBranch,
76 llvm::Twine Suffix = ".split");
77
78/// Captures attributes that affect generating LLVM-IR using the
79/// OpenMPIRBuilder and related classes. Note that not all attributes are
80/// required for all classes or functions. In some use cases the configuration
81/// is not necessary at all, because because the only functions that are called
82/// are ones that are not dependent on the configuration.
84public:
85 /// Flag for specifying if the compilation is done for embedded device code
86 /// or host code.
87 std::optional<bool> IsEmbedded;
88
89 /// Flag for specifying if the compilation is done for an offloading target,
90 /// like GPU.
91 std::optional<bool> IsTargetCodegen;
92
93 /// Flag for specifying weather a requires unified_shared_memory
94 /// directive is present or not.
95 std::optional<bool> HasRequiresUnifiedSharedMemory;
96
97 // Flag for specifying if offloading is mandatory.
98 std::optional<bool> OpenMPOffloadMandatory;
99
100 /// First separator used between the initial two parts of a name.
101 std::optional<StringRef> FirstSeparator;
102 /// Separator used between all of the rest consecutive parts of s name
103 std::optional<StringRef> Separator;
104
112
113 // Getters functions that assert if the required values are not present.
114 bool isEmbedded() const {
115 assert(IsEmbedded.has_value() && "IsEmbedded is not set");
116 return *IsEmbedded;
117 }
118
119 bool isTargetCodegen() const {
120 assert(IsTargetCodegen.has_value() && "IsTargetCodegen is not set");
121 return *IsTargetCodegen;
122 }
123
126 "HasUnifiedSharedMemory is not set");
128 }
129
131 assert(OpenMPOffloadMandatory.has_value() &&
132 "OpenMPOffloadMandatory is not set");
134 }
135 // Returns the FirstSeparator if set, otherwise use the default
136 // separator depending on isTargetCodegen
138 if (FirstSeparator.has_value())
139 return *FirstSeparator;
140 if (isTargetCodegen())
141 return "_";
142 return ".";
143 }
144
145 // Returns the Separator if set, otherwise use the default
146 // separator depending on isTargetCodegen
148 if (Separator.has_value())
149 return *Separator;
150 if (isTargetCodegen())
151 return "$";
152 return ".";
153 }
154
159 }
162};
163
164/// Data structure to contain the information needed to uniquely identify
165/// a target entry.
167 std::string ParentName;
168 unsigned DeviceID;
169 unsigned FileID;
170 unsigned Line;
171 unsigned Count;
172
174 : ParentName(""), DeviceID(0), FileID(0), Line(0), Count(0) {}
176 unsigned FileID, unsigned Line, unsigned Count = 0)
178 Count(Count) {}
179
182 unsigned DeviceID, unsigned FileID,
183 unsigned Line, unsigned Count);
184
186 return std::make_tuple(ParentName, DeviceID, FileID, Line, Count) <
187 std::make_tuple(RHS.ParentName, RHS.DeviceID, RHS.FileID, RHS.Line,
188 RHS.Count);
189 }
190};
191
192/// Class that manages information about offload code regions and data
194 /// Number of entries registered so far.
195 OpenMPIRBuilder *OMPBuilder;
196 unsigned OffloadingEntriesNum = 0;
197
198public:
199 /// Base class of the entries info.
201 public:
202 /// Kind of a given entry.
203 enum OffloadingEntryInfoKinds : unsigned {
204 /// Entry is a target region.
206 /// Entry is a declare target variable.
208 /// Invalid entry info.
210 };
211
212 protected:
214 explicit OffloadEntryInfo(OffloadingEntryInfoKinds Kind) : Kind(Kind) {}
215 explicit OffloadEntryInfo(OffloadingEntryInfoKinds Kind, unsigned Order,
216 uint32_t Flags)
217 : Flags(Flags), Order(Order), Kind(Kind) {}
218 ~OffloadEntryInfo() = default;
219
220 public:
221 bool isValid() const { return Order != ~0u; }
222 unsigned getOrder() const { return Order; }
223 OffloadingEntryInfoKinds getKind() const { return Kind; }
224 uint32_t getFlags() const { return Flags; }
225 void setFlags(uint32_t NewFlags) { Flags = NewFlags; }
226 Constant *getAddress() const { return cast_or_null<Constant>(Addr); }
228 assert(!Addr.pointsToAliveValue() && "Address has been set before!");
229 Addr = V;
230 }
231 static bool classof(const OffloadEntryInfo *Info) { return true; }
232
233 private:
234 /// Address of the entity that has to be mapped for offloading.
235 WeakTrackingVH Addr;
236
237 /// Flags associated with the device global.
238 uint32_t Flags = 0u;
239
240 /// Order this entry was emitted.
241 unsigned Order = ~0u;
242
244 };
245
246 /// Return true if a there are no entries defined.
247 bool empty() const;
248 /// Return number of entries defined so far.
249 unsigned size() const { return OffloadingEntriesNum; }
250
252
253 //
254 // Target region entries related.
255 //
256
257 /// Kind of the target registry entry.
259 /// Mark the entry as target region.
261 /// Mark the entry as a global constructor.
263 /// Mark the entry as a global destructor.
265 };
266
267 /// Target region entries info.
269 /// Address that can be used as the ID of the entry.
270 Constant *ID = nullptr;
271
272 public:
275 explicit OffloadEntryInfoTargetRegion(unsigned Order, Constant *Addr,
276 Constant *ID,
279 ID(ID) {
281 }
282
283 Constant *getID() const { return ID; }
284 void setID(Constant *V) {
285 assert(!ID && "ID has been set before!");
286 ID = V;
287 }
288 static bool classof(const OffloadEntryInfo *Info) {
289 return Info->getKind() == OffloadingEntryInfoTargetRegion;
290 }
291 };
292
293 /// Initialize target region entry.
294 /// This is ONLY needed for DEVICE compilation.
296 unsigned Order);
297 /// Register target region entry.
301 /// Return true if a target region entry with the provided information
302 /// exists.
304 bool IgnoreAddressId = false) const;
305
306 // Return the Name based on \a EntryInfo using the next available Count.
308 const TargetRegionEntryInfo &EntryInfo);
309
310 /// brief Applies action \a Action on all registered entries.
311 typedef function_ref<void(const TargetRegionEntryInfo &EntryInfo,
312 const OffloadEntryInfoTargetRegion &)>
314 void
316
317 //
318 // Device global variable entries related.
319 //
320
321 /// Kind of the global variable entry..
323 /// Mark the entry as a to declare target.
325 /// Mark the entry as a to declare target link.
327 };
328
329 /// Device global variable entries info.
331 /// Type of the global variable.
332 int64_t VarSize;
334
335 public:
338 explicit OffloadEntryInfoDeviceGlobalVar(unsigned Order,
341 explicit OffloadEntryInfoDeviceGlobalVar(unsigned Order, Constant *Addr,
342 int64_t VarSize,
346 VarSize(VarSize), Linkage(Linkage) {
348 }
349
350 int64_t getVarSize() const { return VarSize; }
351 void setVarSize(int64_t Size) { VarSize = Size; }
352 GlobalValue::LinkageTypes getLinkage() const { return Linkage; }
353 void setLinkage(GlobalValue::LinkageTypes LT) { Linkage = LT; }
354 static bool classof(const OffloadEntryInfo *Info) {
355 return Info->getKind() == OffloadingEntryInfoDeviceGlobalVar;
356 }
357 };
358
359 /// Initialize device global variable entry.
360 /// This is ONLY used for DEVICE compilation.
363 unsigned Order);
364
365 /// Register device global variable entry.
367 int64_t VarSize,
370 /// Checks if the variable with the given name has been registered already.
372 return OffloadEntriesDeviceGlobalVar.count(VarName) > 0;
373 }
374 /// Applies action \a Action on all registered entries.
375 typedef function_ref<void(StringRef, const OffloadEntryInfoDeviceGlobalVar &)>
379
380private:
381 /// Return the count of entries at a particular source location.
382 unsigned
383 getTargetRegionEntryInfoCount(const TargetRegionEntryInfo &EntryInfo) const;
384
385 /// Update the count of entries at a particular source location.
386 void
387 incrementTargetRegionEntryInfoCount(const TargetRegionEntryInfo &EntryInfo);
388
390 getTargetRegionEntryCountKey(const TargetRegionEntryInfo &EntryInfo) {
391 return TargetRegionEntryInfo(EntryInfo.ParentName, EntryInfo.DeviceID,
392 EntryInfo.FileID, EntryInfo.Line, 0);
393 }
394
395 // Count of entries at a location.
396 std::map<TargetRegionEntryInfo, unsigned> OffloadEntriesTargetRegionCount;
397
398 // Storage for target region entries kind.
399 typedef std::map<TargetRegionEntryInfo, OffloadEntryInfoTargetRegion>
400 OffloadEntriesTargetRegionTy;
401 OffloadEntriesTargetRegionTy OffloadEntriesTargetRegion;
402 /// Storage for device global variable entries kind. The storage is to be
403 /// indexed by mangled name.
405 OffloadEntriesDeviceGlobalVarTy;
406 OffloadEntriesDeviceGlobalVarTy OffloadEntriesDeviceGlobalVar;
407};
408
409/// An interface to create LLVM-IR for OpenMP directives.
410///
411/// Each OpenMP directive has a corresponding public generator method.
413public:
414 /// Create a new OpenMPIRBuilder operating on the given module \p M. This will
415 /// not have an effect on \p M (see initialize)
417 : M(M), Builder(M.getContext()), OffloadInfoManager(this) {}
419
420 /// Initialize the internal state, this will put structures types and
421 /// potentially other helpers into the underlying module. Must be called
422 /// before any other method and only once!
423 void initialize();
424
426
427 /// Finalize the underlying module, e.g., by outlining regions.
428 /// \param Fn The function to be finalized. If not used,
429 /// all functions are finalized.
430 void finalize(Function *Fn = nullptr);
431
432 /// Add attributes known for \p FnID to \p Fn.
434
435 /// Type used throughout for insertion points.
437
438 /// Get the create a name using the platform specific separators.
439 /// \param Parts parts of the final name that needs separation
440 /// The created name has a first separator between the first and second part
441 /// and a second separator between all other parts.
442 /// E.g. with FirstSeparator "$" and Separator "." and
443 /// parts: "p1", "p2", "p3", "p4"
444 /// The resulting name is "p1$p2.p3.p4"
445 /// The separators are retrieved from the OpenMPIRBuilderConfig.
446 std::string createPlatformSpecificName(ArrayRef<StringRef> Parts) const;
447
448 /// Callback type for variable finalization (think destructors).
449 ///
450 /// \param CodeGenIP is the insertion point at which the finalization code
451 /// should be placed.
452 ///
453 /// A finalize callback knows about all objects that need finalization, e.g.
454 /// destruction, when the scope of the currently generated construct is left
455 /// at the time, and location, the callback is invoked.
456 using FinalizeCallbackTy = std::function<void(InsertPointTy CodeGenIP)>;
457
459 /// The finalization callback provided by the last in-flight invocation of
460 /// createXXXX for the directive of kind DK.
462
463 /// The directive kind of the innermost directive that has an associated
464 /// region which might require finalization when it is left.
465 omp::Directive DK;
466
467 /// Flag to indicate if the directive is cancellable.
469 };
470
471 /// Push a finalization callback on the finalization stack.
472 ///
473 /// NOTE: Temporary solution until Clang CG is gone.
475 FinalizationStack.push_back(FI);
476 }
477
478 /// Pop the last finalization callback from the finalization stack.
479 ///
480 /// NOTE: Temporary solution until Clang CG is gone.
482
483 /// Callback type for body (=inner region) code generation
484 ///
485 /// The callback takes code locations as arguments, each describing a
486 /// location where additional instructions can be inserted.
487 ///
488 /// The CodeGenIP may be in the middle of a basic block or point to the end of
489 /// it. The basic block may have a terminator or be degenerate. The callback
490 /// function may just insert instructions at that position, but also split the
491 /// block (without the Before argument of BasicBlock::splitBasicBlock such
492 /// that the identify of the split predecessor block is preserved) and insert
493 /// additional control flow, including branches that do not lead back to what
494 /// follows the CodeGenIP. Note that since the callback is allowed to split
495 /// the block, callers must assume that InsertPoints to positions in the
496 /// BasicBlock after CodeGenIP including CodeGenIP itself are invalidated. If
497 /// such InsertPoints need to be preserved, it can split the block itself
498 /// before calling the callback.
499 ///
500 /// AllocaIP and CodeGenIP must not point to the same position.
501 ///
502 /// \param AllocaIP is the insertion point at which new alloca instructions
503 /// should be placed. The BasicBlock it is pointing to must
504 /// not be split.
505 /// \param CodeGenIP is the insertion point at which the body code should be
506 /// placed.
508 function_ref<void(InsertPointTy AllocaIP, InsertPointTy CodeGenIP)>;
509
510 // This is created primarily for sections construct as llvm::function_ref
511 // (BodyGenCallbackTy) is not storable (as described in the comments of
512 // function_ref class - function_ref contains non-ownable reference
513 // to the callable.
515 std::function<void(InsertPointTy AllocaIP, InsertPointTy CodeGenIP)>;
516
517 /// Callback type for loop body code generation.
518 ///
519 /// \param CodeGenIP is the insertion point where the loop's body code must be
520 /// placed. This will be a dedicated BasicBlock with a
521 /// conditional branch from the loop condition check and
522 /// terminated with an unconditional branch to the loop
523 /// latch.
524 /// \param IndVar is the induction variable usable at the insertion point.
526 function_ref<void(InsertPointTy CodeGenIP, Value *IndVar)>;
527
528 /// Callback type for variable privatization (think copy & default
529 /// constructor).
530 ///
531 /// \param AllocaIP is the insertion point at which new alloca instructions
532 /// should be placed.
533 /// \param CodeGenIP is the insertion point at which the privatization code
534 /// should be placed.
535 /// \param Original The value being copied/created, should not be used in the
536 /// generated IR.
537 /// \param Inner The equivalent of \p Original that should be used in the
538 /// generated IR; this is equal to \p Original if the value is
539 /// a pointer and can thus be passed directly, otherwise it is
540 /// an equivalent but different value.
541 /// \param ReplVal The replacement value, thus a copy or new created version
542 /// of \p Inner.
543 ///
544 /// \returns The new insertion point where code generation continues and
545 /// \p ReplVal the replacement value.
547 InsertPointTy AllocaIP, InsertPointTy CodeGenIP, Value &Original,
548 Value &Inner, Value *&ReplVal)>;
549
550 /// Description of a LLVM-IR insertion point (IP) and a debug/source location
551 /// (filename, line, column, ...).
554 : IP(IRB.saveIP()), DL(IRB.getCurrentDebugLocation()) {}
557 : IP(IP), DL(DL) {}
560 };
561
562 /// Emitter methods for OpenMP directives.
563 ///
564 ///{
565
566 /// Generator for '#omp barrier'
567 ///
568 /// \param Loc The location where the barrier directive was encountered.
569 /// \param DK The kind of directive that caused the barrier.
570 /// \param ForceSimpleCall Flag to force a simple (=non-cancellation) barrier.
571 /// \param CheckCancelFlag Flag to indicate a cancel barrier return value
572 /// should be checked and acted upon.
573 ///
574 /// \returns The insertion point after the barrier.
575 InsertPointTy createBarrier(const LocationDescription &Loc, omp::Directive DK,
576 bool ForceSimpleCall = false,
577 bool CheckCancelFlag = true);
578
579 /// Generator for '#omp cancel'
580 ///
581 /// \param Loc The location where the directive was encountered.
582 /// \param IfCondition The evaluated 'if' clause expression, if any.
583 /// \param CanceledDirective The kind of directive that is cancled.
584 ///
585 /// \returns The insertion point after the barrier.
586 InsertPointTy createCancel(const LocationDescription &Loc, Value *IfCondition,
587 omp::Directive CanceledDirective);
588
589 /// Generator for '#omp parallel'
590 ///
591 /// \param Loc The insert and source location description.
592 /// \param AllocaIP The insertion points to be used for alloca instructions.
593 /// \param BodyGenCB Callback that will generate the region code.
594 /// \param PrivCB Callback to copy a given variable (think copy constructor).
595 /// \param FiniCB Callback to finalize variable copies.
596 /// \param IfCondition The evaluated 'if' clause expression, if any.
597 /// \param NumThreads The evaluated 'num_threads' clause expression, if any.
598 /// \param ProcBind The value of the 'proc_bind' clause (see ProcBindKind).
599 /// \param IsCancellable Flag to indicate a cancellable parallel region.
600 ///
601 /// \returns The insertion position *after* the parallel.
604 BodyGenCallbackTy BodyGenCB, PrivatizeCallbackTy PrivCB,
605 FinalizeCallbackTy FiniCB, Value *IfCondition,
606 Value *NumThreads, omp::ProcBindKind ProcBind,
607 bool IsCancellable);
608
609 /// Generator for the control flow structure of an OpenMP canonical loop.
610 ///
611 /// This generator operates on the logical iteration space of the loop, i.e.
612 /// the caller only has to provide a loop trip count of the loop as defined by
613 /// base language semantics. The trip count is interpreted as an unsigned
614 /// integer. The induction variable passed to \p BodyGenCB will be of the same
615 /// type and run from 0 to \p TripCount - 1. It is up to the callback to
616 /// convert the logical iteration variable to the loop counter variable in the
617 /// loop body.
618 ///
619 /// \param Loc The insert and source location description. The insert
620 /// location can be between two instructions or the end of a
621 /// degenerate block (e.g. a BB under construction).
622 /// \param BodyGenCB Callback that will generate the loop body code.
623 /// \param TripCount Number of iterations the loop body is executed.
624 /// \param Name Base name used to derive BB and instruction names.
625 ///
626 /// \returns An object representing the created control flow structure which
627 /// can be used for loop-associated directives.
629 LoopBodyGenCallbackTy BodyGenCB,
630 Value *TripCount,
631 const Twine &Name = "loop");
632
633 /// Generator for the control flow structure of an OpenMP canonical loop.
634 ///
635 /// Instead of a logical iteration space, this allows specifying user-defined
636 /// loop counter values using increment, upper- and lower bounds. To
637 /// disambiguate the terminology when counting downwards, instead of lower
638 /// bounds we use \p Start for the loop counter value in the first body
639 /// iteration.
640 ///
641 /// Consider the following limitations:
642 ///
643 /// * A loop counter space over all integer values of its bit-width cannot be
644 /// represented. E.g using uint8_t, its loop trip count of 256 cannot be
645 /// stored into an 8 bit integer):
646 ///
647 /// DO I = 0, 255, 1
648 ///
649 /// * Unsigned wrapping is only supported when wrapping only "once"; E.g.
650 /// effectively counting downwards:
651 ///
652 /// for (uint8_t i = 100u; i > 0; i += 127u)
653 ///
654 ///
655 /// TODO: May need to add additional parameters to represent:
656 ///
657 /// * Allow representing downcounting with unsigned integers.
658 ///
659 /// * Sign of the step and the comparison operator might disagree:
660 ///
661 /// for (int i = 0; i < 42; i -= 1u)
662 ///
663 //
664 /// \param Loc The insert and source location description.
665 /// \param BodyGenCB Callback that will generate the loop body code.
666 /// \param Start Value of the loop counter for the first iterations.
667 /// \param Stop Loop counter values past this will stop the loop.
668 /// \param Step Loop counter increment after each iteration; negative
669 /// means counting down.
670 /// \param IsSigned Whether Start, Stop and Step are signed integers.
671 /// \param InclusiveStop Whether \p Stop itself is a valid value for the loop
672 /// counter.
673 /// \param ComputeIP Insertion point for instructions computing the trip
674 /// count. Can be used to ensure the trip count is available
675 /// at the outermost loop of a loop nest. If not set,
676 /// defaults to the preheader of the generated loop.
677 /// \param Name Base name used to derive BB and instruction names.
678 ///
679 /// \returns An object representing the created control flow structure which
680 /// can be used for loop-associated directives.
682 LoopBodyGenCallbackTy BodyGenCB,
683 Value *Start, Value *Stop, Value *Step,
684 bool IsSigned, bool InclusiveStop,
685 InsertPointTy ComputeIP = {},
686 const Twine &Name = "loop");
687
688 /// Collapse a loop nest into a single loop.
689 ///
690 /// Merges loops of a loop nest into a single CanonicalLoopNest representation
691 /// that has the same number of innermost loop iterations as the origin loop
692 /// nest. The induction variables of the input loops are derived from the
693 /// collapsed loop's induction variable. This is intended to be used to
694 /// implement OpenMP's collapse clause. Before applying a directive,
695 /// collapseLoops normalizes a loop nest to contain only a single loop and the
696 /// directive's implementation does not need to handle multiple loops itself.
697 /// This does not remove the need to handle all loop nest handling by
698 /// directives, such as the ordered(<n>) clause or the simd schedule-clause
699 /// modifier of the worksharing-loop directive.
700 ///
701 /// Example:
702 /// \code
703 /// for (int i = 0; i < 7; ++i) // Canonical loop "i"
704 /// for (int j = 0; j < 9; ++j) // Canonical loop "j"
705 /// body(i, j);
706 /// \endcode
707 ///
708 /// After collapsing with Loops={i,j}, the loop is changed to
709 /// \code
710 /// for (int ij = 0; ij < 63; ++ij) {
711 /// int i = ij / 9;
712 /// int j = ij % 9;
713 /// body(i, j);
714 /// }
715 /// \endcode
716 ///
717 /// In the current implementation, the following limitations apply:
718 ///
719 /// * All input loops have an induction variable of the same type.
720 ///
721 /// * The collapsed loop will have the same trip count integer type as the
722 /// input loops. Therefore it is possible that the collapsed loop cannot
723 /// represent all iterations of the input loops. For instance, assuming a
724 /// 32 bit integer type, and two input loops both iterating 2^16 times, the
725 /// theoretical trip count of the collapsed loop would be 2^32 iteration,
726 /// which cannot be represented in an 32-bit integer. Behavior is undefined
727 /// in this case.
728 ///
729 /// * The trip counts of every input loop must be available at \p ComputeIP.
730 /// Non-rectangular loops are not yet supported.
731 ///
732 /// * At each nest level, code between a surrounding loop and its nested loop
733 /// is hoisted into the loop body, and such code will be executed more
734 /// often than before collapsing (or not at all if any inner loop iteration
735 /// has a trip count of 0). This is permitted by the OpenMP specification.
736 ///
737 /// \param DL Debug location for instructions added for collapsing,
738 /// such as instructions to compute/derive the input loop's
739 /// induction variables.
740 /// \param Loops Loops in the loop nest to collapse. Loops are specified
741 /// from outermost-to-innermost and every control flow of a
742 /// loop's body must pass through its directly nested loop.
743 /// \param ComputeIP Where additional instruction that compute the collapsed
744 /// trip count. If not set, defaults to before the generated
745 /// loop.
746 ///
747 /// \returns The CanonicalLoopInfo object representing the collapsed loop.
750 InsertPointTy ComputeIP);
751
752 /// Get the default alignment value for given target
753 ///
754 /// \param TargetTriple Target triple
755 /// \param Features StringMap which describes extra CPU features
756 static unsigned getOpenMPDefaultSimdAlign(const Triple &TargetTriple,
757 const StringMap<bool> &Features);
758
759private:
760 /// Modifies the canonical loop to be a statically-scheduled workshare loop.
761 ///
762 /// This takes a \p LoopInfo representing a canonical loop, such as the one
763 /// created by \p createCanonicalLoop and emits additional instructions to
764 /// turn it into a workshare loop. In particular, it calls to an OpenMP
765 /// runtime function in the preheader to obtain the loop bounds to be used in
766 /// the current thread, updates the relevant instructions in the canonical
767 /// loop and calls to an OpenMP runtime finalization function after the loop.
768 ///
769 /// \param DL Debug location for instructions added for the
770 /// workshare-loop construct itself.
771 /// \param CLI A descriptor of the canonical loop to workshare.
772 /// \param AllocaIP An insertion point for Alloca instructions usable in the
773 /// preheader of the loop.
774 /// \param NeedsBarrier Indicates whether a barrier must be inserted after
775 /// the loop.
776 ///
777 /// \returns Point where to insert code after the workshare construct.
778 InsertPointTy applyStaticWorkshareLoop(DebugLoc DL, CanonicalLoopInfo *CLI,
779 InsertPointTy AllocaIP,
780 bool NeedsBarrier);
781
782 /// Modifies the canonical loop a statically-scheduled workshare loop with a
783 /// user-specified chunk size.
784 ///
785 /// \param DL Debug location for instructions added for the
786 /// workshare-loop construct itself.
787 /// \param CLI A descriptor of the canonical loop to workshare.
788 /// \param AllocaIP An insertion point for Alloca instructions usable in
789 /// the preheader of the loop.
790 /// \param NeedsBarrier Indicates whether a barrier must be inserted after the
791 /// loop.
792 /// \param ChunkSize The user-specified chunk size.
793 ///
794 /// \returns Point where to insert code after the workshare construct.
795 InsertPointTy applyStaticChunkedWorkshareLoop(DebugLoc DL,
797 InsertPointTy AllocaIP,
798 bool NeedsBarrier,
799 Value *ChunkSize);
800
801 /// Modifies the canonical loop to be a dynamically-scheduled workshare loop.
802 ///
803 /// This takes a \p LoopInfo representing a canonical loop, such as the one
804 /// created by \p createCanonicalLoop and emits additional instructions to
805 /// turn it into a workshare loop. In particular, it calls to an OpenMP
806 /// runtime function in the preheader to obtain, and then in each iteration
807 /// to update the loop counter.
808 ///
809 /// \param DL Debug location for instructions added for the
810 /// workshare-loop construct itself.
811 /// \param CLI A descriptor of the canonical loop to workshare.
812 /// \param AllocaIP An insertion point for Alloca instructions usable in the
813 /// preheader of the loop.
814 /// \param SchedType Type of scheduling to be passed to the init function.
815 /// \param NeedsBarrier Indicates whether a barrier must be insterted after
816 /// the loop.
817 /// \param Chunk The size of loop chunk considered as a unit when
818 /// scheduling. If \p nullptr, defaults to 1.
819 ///
820 /// \returns Point where to insert code after the workshare construct.
821 InsertPointTy applyDynamicWorkshareLoop(DebugLoc DL, CanonicalLoopInfo *CLI,
822 InsertPointTy AllocaIP,
823 omp::OMPScheduleType SchedType,
824 bool NeedsBarrier,
825 Value *Chunk = nullptr);
826
827 /// Create alternative version of the loop to support if clause
828 ///
829 /// OpenMP if clause can require to generate second loop. This loop
830 /// will be executed when if clause condition is not met. createIfVersion
831 /// adds branch instruction to the copied loop if \p ifCond is not met.
832 ///
833 /// \param Loop Original loop which should be versioned.
834 /// \param IfCond Value which corresponds to if clause condition
835 /// \param VMap Value to value map to define relation between
836 /// original and copied loop values and loop blocks.
837 /// \param NamePrefix Optional name prefix for if.then if.else blocks.
838 void createIfVersion(CanonicalLoopInfo *Loop, Value *IfCond,
839 ValueToValueMapTy &VMap, const Twine &NamePrefix = "");
840
841public:
842 /// Modifies the canonical loop to be a workshare loop.
843 ///
844 /// This takes a \p LoopInfo representing a canonical loop, such as the one
845 /// created by \p createCanonicalLoop and emits additional instructions to
846 /// turn it into a workshare loop. In particular, it calls to an OpenMP
847 /// runtime function in the preheader to obtain the loop bounds to be used in
848 /// the current thread, updates the relevant instructions in the canonical
849 /// loop and calls to an OpenMP runtime finalization function after the loop.
850 ///
851 /// The concrete transformation is done by applyStaticWorkshareLoop,
852 /// applyStaticChunkedWorkshareLoop, or applyDynamicWorkshareLoop, depending
853 /// on the value of \p SchedKind and \p ChunkSize.
854 ///
855 /// \param DL Debug location for instructions added for the
856 /// workshare-loop construct itself.
857 /// \param CLI A descriptor of the canonical loop to workshare.
858 /// \param AllocaIP An insertion point for Alloca instructions usable in the
859 /// preheader of the loop.
860 /// \param NeedsBarrier Indicates whether a barrier must be insterted after
861 /// the loop.
862 /// \param SchedKind Scheduling algorithm to use.
863 /// \param ChunkSize The chunk size for the inner loop.
864 /// \param HasSimdModifier Whether the simd modifier is present in the
865 /// schedule clause.
866 /// \param HasMonotonicModifier Whether the monotonic modifier is present in
867 /// the schedule clause.
868 /// \param HasNonmonotonicModifier Whether the nonmonotonic modifier is
869 /// present in the schedule clause.
870 /// \param HasOrderedClause Whether the (parameterless) ordered clause is
871 /// present.
872 ///
873 /// \returns Point where to insert code after the workshare construct.
876 bool NeedsBarrier,
877 llvm::omp::ScheduleKind SchedKind = llvm::omp::OMP_SCHEDULE_Default,
878 Value *ChunkSize = nullptr, bool HasSimdModifier = false,
879 bool HasMonotonicModifier = false, bool HasNonmonotonicModifier = false,
880 bool HasOrderedClause = false);
881
882 /// Tile a loop nest.
883 ///
884 /// Tiles the loops of \p Loops by the tile sizes in \p TileSizes. Loops in
885 /// \p/ Loops must be perfectly nested, from outermost to innermost loop
886 /// (i.e. Loops.front() is the outermost loop). The trip count llvm::Value
887 /// of every loop and every tile sizes must be usable in the outermost
888 /// loop's preheader. This implies that the loop nest is rectangular.
889 ///
890 /// Example:
891 /// \code
892 /// for (int i = 0; i < 15; ++i) // Canonical loop "i"
893 /// for (int j = 0; j < 14; ++j) // Canonical loop "j"
894 /// body(i, j);
895 /// \endcode
896 ///
897 /// After tiling with Loops={i,j} and TileSizes={5,7}, the loop is changed to
898 /// \code
899 /// for (int i1 = 0; i1 < 3; ++i1)
900 /// for (int j1 = 0; j1 < 2; ++j1)
901 /// for (int i2 = 0; i2 < 5; ++i2)
902 /// for (int j2 = 0; j2 < 7; ++j2)
903 /// body(i1*3+i2, j1*3+j2);
904 /// \endcode
905 ///
906 /// The returned vector are the loops {i1,j1,i2,j2}. The loops i1 and j1 are
907 /// referred to the floor, and the loops i2 and j2 are the tiles. Tiling also
908 /// handles non-constant trip counts, non-constant tile sizes and trip counts
909 /// that are not multiples of the tile size. In the latter case the tile loop
910 /// of the last floor-loop iteration will have fewer iterations than specified
911 /// as its tile size.
912 ///
913 ///
914 /// @param DL Debug location for instructions added by tiling, for
915 /// instance the floor- and tile trip count computation.
916 /// @param Loops Loops to tile. The CanonicalLoopInfo objects are
917 /// invalidated by this method, i.e. should not used after
918 /// tiling.
919 /// @param TileSizes For each loop in \p Loops, the tile size for that
920 /// dimensions.
921 ///
922 /// \returns A list of generated loops. Contains twice as many loops as the
923 /// input loop nest; the first half are the floor loops and the
924 /// second half are the tile loops.
925 std::vector<CanonicalLoopInfo *>
927 ArrayRef<Value *> TileSizes);
928
929 /// Fully unroll a loop.
930 ///
931 /// Instead of unrolling the loop immediately (and duplicating its body
932 /// instructions), it is deferred to LLVM's LoopUnrollPass by adding loop
933 /// metadata.
934 ///
935 /// \param DL Debug location for instructions added by unrolling.
936 /// \param Loop The loop to unroll. The loop will be invalidated.
938
939 /// Fully or partially unroll a loop. How the loop is unrolled is determined
940 /// using LLVM's LoopUnrollPass.
941 ///
942 /// \param DL Debug location for instructions added by unrolling.
943 /// \param Loop The loop to unroll. The loop will be invalidated.
945
946 /// Partially unroll a loop.
947 ///
948 /// The CanonicalLoopInfo of the unrolled loop for use with chained
949 /// loop-associated directive can be requested using \p UnrolledCLI. Not
950 /// needing the CanonicalLoopInfo allows more efficient code generation by
951 /// deferring the actual unrolling to the LoopUnrollPass using loop metadata.
952 /// A loop-associated directive applied to the unrolled loop needs to know the
953 /// new trip count which means that if using a heuristically determined unroll
954 /// factor (\p Factor == 0), that factor must be computed immediately. We are
955 /// using the same logic as the LoopUnrollPass to derived the unroll factor,
956 /// but which assumes that some canonicalization has taken place (e.g.
957 /// Mem2Reg, LICM, GVN, Inlining, etc.). That is, the heuristic will perform
958 /// better when the unrolled loop's CanonicalLoopInfo is not needed.
959 ///
960 /// \param DL Debug location for instructions added by unrolling.
961 /// \param Loop The loop to unroll. The loop will be invalidated.
962 /// \param Factor The factor to unroll the loop by. A factor of 0
963 /// indicates that a heuristic should be used to determine
964 /// the unroll-factor.
965 /// \param UnrolledCLI If non-null, receives the CanonicalLoopInfo of the
966 /// partially unrolled loop. Otherwise, uses loop metadata
967 /// to defer unrolling to the LoopUnrollPass.
968 void unrollLoopPartial(DebugLoc DL, CanonicalLoopInfo *Loop, int32_t Factor,
969 CanonicalLoopInfo **UnrolledCLI);
970
971 /// Add metadata to simd-ize a loop. If IfCond is not nullptr, the loop
972 /// is cloned. The metadata which prevents vectorization is added to
973 /// to the cloned loop. The cloned loop is executed when ifCond is evaluated
974 /// to false.
975 ///
976 /// \param Loop The loop to simd-ize.
977 /// \param AlignedVars The map which containts pairs of the pointer
978 /// and its corresponding alignment.
979 /// \param IfCond The value which corresponds to the if clause
980 /// condition.
981 /// \param Order The enum to map order clause.
982 /// \param Simdlen The Simdlen length to apply to the simd loop.
983 /// \param Safelen The Safelen length to apply to the simd loop.
985 MapVector<Value *, Value *> AlignedVars, Value *IfCond,
986 omp::OrderKind Order, ConstantInt *Simdlen,
987 ConstantInt *Safelen);
988
989 /// Generator for '#omp flush'
990 ///
991 /// \param Loc The location where the flush directive was encountered
992 void createFlush(const LocationDescription &Loc);
993
994 /// Generator for '#omp taskwait'
995 ///
996 /// \param Loc The location where the taskwait directive was encountered.
997 void createTaskwait(const LocationDescription &Loc);
998
999 /// Generator for '#omp taskyield'
1000 ///
1001 /// \param Loc The location where the taskyield directive was encountered.
1002 void createTaskyield(const LocationDescription &Loc);
1003
1004 /// A struct to pack the relevant information for an OpenMP depend clause.
1005 struct DependData {
1009 explicit DependData() = default;
1011 Value *DepVal)
1013 };
1014
1015 /// Generator for `#omp task`
1016 ///
1017 /// \param Loc The location where the task construct was encountered.
1018 /// \param AllocaIP The insertion point to be used for alloca instructions.
1019 /// \param BodyGenCB Callback that will generate the region code.
1020 /// \param Tied True if the task is tied, false if the task is untied.
1021 /// \param Final i1 value which is `true` if the task is final, `false` if the
1022 /// task is not final.
1023 /// \param IfCondition i1 value. If it evaluates to `false`, an undeferred
1024 /// task is generated, and the encountering thread must
1025 /// suspend the current task region, for which execution
1026 /// cannot be resumed until execution of the structured
1027 /// block that is associated with the generated task is
1028 /// completed.
1029 InsertPointTy createTask(const LocationDescription &Loc,
1030 InsertPointTy AllocaIP, BodyGenCallbackTy BodyGenCB,
1031 bool Tied = true, Value *Final = nullptr,
1032 Value *IfCondition = nullptr,
1033 SmallVector<DependData> Dependencies = {});
1034
1035 /// Generator for the taskgroup construct
1036 ///
1037 /// \param Loc The location where the taskgroup construct was encountered.
1038 /// \param AllocaIP The insertion point to be used for alloca instructions.
1039 /// \param BodyGenCB Callback that will generate the region code.
1040 InsertPointTy createTaskgroup(const LocationDescription &Loc,
1041 InsertPointTy AllocaIP,
1042 BodyGenCallbackTy BodyGenCB);
1043
1044 /// Functions used to generate reductions. Such functions take two Values
1045 /// representing LHS and RHS of the reduction, respectively, and a reference
1046 /// to the value that is updated to refer to the reduction result.
1049
1050 /// Functions used to generate atomic reductions. Such functions take two
1051 /// Values representing pointers to LHS and RHS of the reduction, as well as
1052 /// the element type of these pointers. They are expected to atomically
1053 /// update the LHS to the reduced value.
1056
1057 /// Information about an OpenMP reduction.
1065 assert(cast<PointerType>(Variable->getType())
1066 ->isOpaqueOrPointeeTypeMatches(ElementType) && "Invalid elem type");
1067 }
1068
1069 /// Reduction element type, must match pointee type of variable.
1071
1072 /// Reduction variable of pointer type.
1074
1075 /// Thread-private partial reduction variable.
1077
1078 /// Callback for generating the reduction body. The IR produced by this will
1079 /// be used to combine two values in a thread-safe context, e.g., under
1080 /// lock or within the same thread, and therefore need not be atomic.
1082
1083 /// Callback for generating the atomic reduction body, may be null. The IR
1084 /// produced by this will be used to atomically combine two values during
1085 /// reduction. If null, the implementation will use the non-atomic version
1086 /// along with the appropriate synchronization mechanisms.
1088 };
1089
1090 // TODO: provide atomic and non-atomic reduction generators for reduction
1091 // operators defined by the OpenMP specification.
1092
1093 /// Generator for '#omp reduction'.
1094 ///
1095 /// Emits the IR instructing the runtime to perform the specific kind of
1096 /// reductions. Expects reduction variables to have been privatized and
1097 /// initialized to reduction-neutral values separately. Emits the calls to
1098 /// runtime functions as well as the reduction function and the basic blocks
1099 /// performing the reduction atomically and non-atomically.
1100 ///
1101 /// The code emitted for the following:
1102 ///
1103 /// \code
1104 /// type var_1;
1105 /// type var_2;
1106 /// #pragma omp <directive> reduction(reduction-op:var_1,var_2)
1107 /// /* body */;
1108 /// \endcode
1109 ///
1110 /// corresponds to the following sketch.
1111 ///
1112 /// \code
1113 /// void _outlined_par() {
1114 /// // N is the number of different reductions.
1115 /// void *red_array[] = {privatized_var_1, privatized_var_2, ...};
1116 /// switch(__kmpc_reduce(..., N, /*size of data in red array*/, red_array,
1117 /// _omp_reduction_func,
1118 /// _gomp_critical_user.reduction.var)) {
1119 /// case 1: {
1120 /// var_1 = var_1 <reduction-op> privatized_var_1;
1121 /// var_2 = var_2 <reduction-op> privatized_var_2;
1122 /// // ...
1123 /// __kmpc_end_reduce(...);
1124 /// break;
1125 /// }
1126 /// case 2: {
1127 /// _Atomic<ReductionOp>(var_1, privatized_var_1);
1128 /// _Atomic<ReductionOp>(var_2, privatized_var_2);
1129 /// // ...
1130 /// break;
1131 /// }
1132 /// default: break;
1133 /// }
1134 /// }
1135 ///
1136 /// void _omp_reduction_func(void **lhs, void **rhs) {
1137 /// *(type *)lhs[0] = *(type *)lhs[0] <reduction-op> *(type *)rhs[0];
1138 /// *(type *)lhs[1] = *(type *)lhs[1] <reduction-op> *(type *)rhs[1];
1139 /// // ...
1140 /// }
1141 /// \endcode
1142 ///
1143 /// \param Loc The location where the reduction was
1144 /// encountered. Must be within the associate
1145 /// directive and after the last local access to the
1146 /// reduction variables.
1147 /// \param AllocaIP An insertion point suitable for allocas usable
1148 /// in reductions.
1149 /// \param ReductionInfos A list of info on each reduction variable.
1150 /// \param IsNoWait A flag set if the reduction is marked as nowait.
1152 InsertPointTy AllocaIP,
1153 ArrayRef<ReductionInfo> ReductionInfos,
1154 bool IsNoWait = false);
1155
1156 ///}
1157
1158 /// Return the insertion point used by the underlying IRBuilder.
1160
1161 /// Update the internal location to \p Loc.
1163 Builder.restoreIP(Loc.IP);
1165 return Loc.IP.getBlock() != nullptr;
1166 }
1167
1168 /// Return the function declaration for the runtime function with \p FnID.
1171
1173
1174 /// Return the (LLVM-IR) string describing the source location \p LocStr.
1175 Constant *getOrCreateSrcLocStr(StringRef LocStr, uint32_t &SrcLocStrSize);
1176
1177 /// Return the (LLVM-IR) string describing the default source location.
1179
1180 /// Return the (LLVM-IR) string describing the source location identified by
1181 /// the arguments.
1182 Constant *getOrCreateSrcLocStr(StringRef FunctionName, StringRef FileName,
1183 unsigned Line, unsigned Column,
1184 uint32_t &SrcLocStrSize);
1185
1186 /// Return the (LLVM-IR) string describing the DebugLoc \p DL. Use \p F as
1187 /// fallback if \p DL does not specify the function name.
1189 Function *F = nullptr);
1190
1191 /// Return the (LLVM-IR) string describing the source location \p Loc.
1192 Constant *getOrCreateSrcLocStr(const LocationDescription &Loc,
1193 uint32_t &SrcLocStrSize);
1194
1195 /// Return an ident_t* encoding the source location \p SrcLocStr and \p Flags.
1196 /// TODO: Create a enum class for the Reserve2Flags
1197 Constant *getOrCreateIdent(Constant *SrcLocStr, uint32_t SrcLocStrSize,
1199 unsigned Reserve2Flags = 0);
1200
1201 /// Create a hidden global flag \p Name in the module with initial value \p
1202 /// Value.
1204
1205 /// Create an offloading section struct used to register this global at
1206 /// runtime.
1207 ///
1208 /// Type struct __tgt_offload_entry{
1209 /// void *addr; // Pointer to the offload entry info.
1210 /// // (function or global)
1211 /// char *name; // Name of the function or global.
1212 /// size_t size; // Size of the entry info (0 if it a function).
1213 /// int32_t flags;
1214 /// int32_t reserved;
1215 /// };
1216 ///
1217 /// \param Addr The pointer to the global being registered.
1218 /// \param Name The symbol name associated with the global.
1219 /// \param Size The size in bytes of the global (0 for functions).
1220 /// \param Flags Flags associated with the entry.
1221 /// \param SectionName The section this entry will be placed at.
1223 int32_t Flags,
1224 StringRef SectionName = "omp_offloading_entries");
1225
1226 /// Generate control flow and cleanup for cancellation.
1227 ///
1228 /// \param CancelFlag Flag indicating if the cancellation is performed.
1229 /// \param CanceledDirective The kind of directive that is cancled.
1230 /// \param ExitCB Extra code to be generated in the exit block.
1231 void emitCancelationCheckImpl(Value *CancelFlag,
1232 omp::Directive CanceledDirective,
1233 FinalizeCallbackTy ExitCB = {});
1234
1235 /// Generate a target region entry call.
1236 ///
1237 /// \param Loc The location at which the request originated and is fulfilled.
1238 /// \param AllocaIP The insertion point to be used for alloca instructions.
1239 /// \param Return Return value of the created function returned by reference.
1240 /// \param DeviceID Identifier for the device via the 'device' clause.
1241 /// \param NumTeams Numer of teams for the region via the 'num_teams' clause
1242 /// or 0 if unspecified and -1 if there is no 'teams' clause.
1243 /// \param NumThreads Number of threads via the 'thread_limit' clause.
1244 /// \param HostPtr Pointer to the host-side pointer of the target kernel.
1245 /// \param KernelArgs Array of arguments to the kernel.
1246 InsertPointTy emitTargetKernel(const LocationDescription &Loc,
1247 InsertPointTy AllocaIP, Value *&Return,
1248 Value *Ident, Value *DeviceID, Value *NumTeams,
1249 Value *NumThreads, Value *HostPtr,
1250 ArrayRef<Value *> KernelArgs);
1251
1252 /// Generate a barrier runtime call.
1253 ///
1254 /// \param Loc The location at which the request originated and is fulfilled.
1255 /// \param DK The directive which caused the barrier
1256 /// \param ForceSimpleCall Flag to force a simple (=non-cancellation) barrier.
1257 /// \param CheckCancelFlag Flag to indicate a cancel barrier return value
1258 /// should be checked and acted upon.
1259 ///
1260 /// \returns The insertion point after the barrier.
1261 InsertPointTy emitBarrierImpl(const LocationDescription &Loc,
1262 omp::Directive DK, bool ForceSimpleCall,
1263 bool CheckCancelFlag);
1264
1265 /// Generate a flush runtime call.
1266 ///
1267 /// \param Loc The location at which the request originated and is fulfilled.
1268 void emitFlush(const LocationDescription &Loc);
1269
1270 /// The finalization stack made up of finalize callbacks currently in-flight,
1271 /// wrapped into FinalizationInfo objects that reference also the finalization
1272 /// target block and the kind of cancellable directive.
1274
1275 /// Return true if the last entry in the finalization stack is of kind \p DK
1276 /// and cancellable.
1277 bool isLastFinalizationInfoCancellable(omp::Directive DK) {
1278 return !FinalizationStack.empty() &&
1279 FinalizationStack.back().IsCancellable &&
1280 FinalizationStack.back().DK == DK;
1281 }
1282
1283 /// Generate a taskwait runtime call.
1284 ///
1285 /// \param Loc The location at which the request originated and is fulfilled.
1286 void emitTaskwaitImpl(const LocationDescription &Loc);
1287
1288 /// Generate a taskyield runtime call.
1289 ///
1290 /// \param Loc The location at which the request originated and is fulfilled.
1291 void emitTaskyieldImpl(const LocationDescription &Loc);
1292
1293 /// Return the current thread ID.
1294 ///
1295 /// \param Ident The ident (ident_t*) describing the query origin.
1297
1298 /// The OpenMPIRBuilder Configuration
1300
1301 /// The underlying LLVM-IR module
1303
1304 /// The LLVM-IR Builder used to create IR.
1306
1307 /// Map to remember source location strings
1309
1310 /// Map to remember existing ident_t*.
1312
1313 /// Info manager to keep track of target regions.
1315
1316 /// Helper that contains information about regions we need to outline
1317 /// during finalization.
1319 using PostOutlineCBTy = std::function<void(Function &)>;
1323
1324 /// Collect all blocks in between EntryBB and ExitBB in both the given
1325 /// vector and set.
1327 SmallVectorImpl<BasicBlock *> &BlockVector);
1328
1329 /// Return the function that contains the region to be outlined.
1330 Function *getFunction() const { return EntryBB->getParent(); }
1331 };
1332
1333 /// Collection of regions that need to be outlined during finalization.
1335
1336 /// Collection of owned canonical loop objects that eventually need to be
1337 /// free'd.
1338 std::forward_list<CanonicalLoopInfo> LoopInfos;
1339
1340 /// Add a new region that will be outlined later.
1341 void addOutlineInfo(OutlineInfo &&OI) { OutlineInfos.emplace_back(OI); }
1342
1343 /// An ordered map of auto-generated variables to their unique names.
1344 /// It stores variables with the following names: 1) ".gomp_critical_user_" +
1345 /// <critical_section_name> + ".var" for "omp critical" directives; 2)
1346 /// <mangled_name_for_global_var> + ".cache." for cache for threadprivate
1347 /// variables.
1349
1350 /// Computes the size of type in bytes.
1351 Value *getSizeInBytes(Value *BasePtr);
1352
1353 /// Create the global variable holding the offload mappings information.
1355 std::string VarName);
1356
1357 /// Create the global variable holding the offload names information.
1360 std::string VarName);
1361
1364 AllocaInst *Args = nullptr;
1366 };
1367
1368 /// Create the allocas instruction used in call to mapper functions.
1370 InsertPointTy AllocaIP, unsigned NumOperands,
1372
1373 /// Create the call for the target mapper function.
1374 /// \param Loc The source location description.
1375 /// \param MapperFunc Function to be called.
1376 /// \param SrcLocInfo Source location information global.
1377 /// \param MaptypesArg The argument types.
1378 /// \param MapnamesArg The argument names.
1379 /// \param MapperAllocas The AllocaInst used for the call.
1380 /// \param DeviceID Device ID for the call.
1381 /// \param NumOperands Number of operands in the call.
1382 void emitMapperCall(const LocationDescription &Loc, Function *MapperFunc,
1383 Value *SrcLocInfo, Value *MaptypesArg, Value *MapnamesArg,
1384 struct MapperAllocas &MapperAllocas, int64_t DeviceID,
1385 unsigned NumOperands);
1386
1387 /// Container for the arguments used to pass data to the runtime library.
1389 explicit TargetDataRTArgs() {}
1390 /// The array of base pointer passed to the runtime library.
1392 /// The array of section pointers passed to the runtime library.
1394 /// The array of sizes passed to the runtime library.
1395 Value *SizesArray = nullptr;
1396 /// The array of map types passed to the runtime library for the beginning
1397 /// of the region or for the entire region if there are no separate map
1398 /// types for the region end.
1400 /// The array of map types passed to the runtime library for the end of the
1401 /// region, or nullptr if there are no separate map types for the region
1402 /// end.
1404 /// The array of user-defined mappers passed to the runtime library.
1406 /// The array of original declaration names of mapped pointers sent to the
1407 /// runtime library for debugging
1409 };
1410
1411 /// Struct that keeps the information that should be kept throughout
1412 /// a 'target data' region.
1414 /// Set to true if device pointer information have to be obtained.
1415 bool RequiresDevicePointerInfo = false;
1416 /// Set to true if Clang emits separate runtime calls for the beginning and
1417 /// end of the region. These calls might have separate map type arrays.
1418 bool SeparateBeginEndCalls = false;
1419
1420 public:
1422
1423 /// Indicate whether any user-defined mapper exists.
1424 bool HasMapper = false;
1425 /// The total number of pointers passed to the runtime library.
1426 unsigned NumberOfPtrs = 0u;
1427
1428 explicit TargetDataInfo() {}
1429 explicit TargetDataInfo(bool RequiresDevicePointerInfo,
1430 bool SeparateBeginEndCalls)
1431 : RequiresDevicePointerInfo(RequiresDevicePointerInfo),
1432 SeparateBeginEndCalls(SeparateBeginEndCalls) {}
1433 /// Clear information about the data arrays.
1436 HasMapper = false;
1437 NumberOfPtrs = 0u;
1438 }
1439 /// Return true if the current target data information has valid arrays.
1440 bool isValid() {
1444 }
1445 bool requiresDevicePointerInfo() { return RequiresDevicePointerInfo; }
1446 bool separateBeginEndCalls() { return SeparateBeginEndCalls; }
1447 };
1448
1449 /// Emit the arguments to be passed to the runtime library based on the
1450 /// arrays of base pointers, pointers, sizes, map types, and mappers. If
1451 /// ForEndCall, emit map types to be passed for the end of the region instead
1452 /// of the beginning.
1456 bool EmitDebug = false,
1457 bool ForEndCall = false);
1458
1459 /// Creates offloading entry for the provided entry ID \a ID, address \a
1460 /// Addr, size \a Size, and flags \a Flags.
1463
1464 /// The kind of errors that can occur when emitting the offload entries and
1465 /// metadata.
1471
1472 /// Callback function type
1474 std::function<void(EmitMetadataErrorKind, TargetRegionEntryInfo)>;
1475
1476 // Emit the offloading entries and metadata so that the device codegen side
1477 // can easily figure out what to emit. The produced metadata looks like
1478 // this:
1479 //
1480 // !omp_offload.info = !{!1, ...}
1481 //
1482 // We only generate metadata for function that contain target regions.
1484 EmitMetadataErrorReportFunctionTy &ErrorReportFunction);
1485
1486public:
1487 /// Generator for __kmpc_copyprivate
1488 ///
1489 /// \param Loc The source location description.
1490 /// \param BufSize Number of elements in the buffer.
1491 /// \param CpyBuf List of pointers to data to be copied.
1492 /// \param CpyFn function to call for copying data.
1493 /// \param DidIt flag variable; 1 for 'single' thread, 0 otherwise.
1494 ///
1495 /// \return The insertion position *after* the CopyPrivate call.
1496
1498 llvm::Value *BufSize, llvm::Value *CpyBuf,
1499 llvm::Value *CpyFn, llvm::Value *DidIt);
1500
1501 /// Generator for '#omp single'
1502 ///
1503 /// \param Loc The source location description.
1504 /// \param BodyGenCB Callback that will generate the region code.
1505 /// \param FiniCB Callback to finalize variable copies.
1506 /// \param IsNowait If false, a barrier is emitted.
1507 /// \param DidIt Local variable used as a flag to indicate 'single' thread
1508 ///
1509 /// \returns The insertion position *after* the single call.
1511 BodyGenCallbackTy BodyGenCB,
1512 FinalizeCallbackTy FiniCB, bool IsNowait,
1513 llvm::Value *DidIt);
1514
1515 /// Generator for '#omp master'
1516 ///
1517 /// \param Loc The insert and source location description.
1518 /// \param BodyGenCB Callback that will generate the region code.
1519 /// \param FiniCB Callback to finalize variable copies.
1520 ///
1521 /// \returns The insertion position *after* the master.
1523 BodyGenCallbackTy BodyGenCB,
1524 FinalizeCallbackTy FiniCB);
1525
1526 /// Generator for '#omp masked'
1527 ///
1528 /// \param Loc The insert and source location description.
1529 /// \param BodyGenCB Callback that will generate the region code.
1530 /// \param FiniCB Callback to finialize variable copies.
1531 ///
1532 /// \returns The insertion position *after* the masked.
1534 BodyGenCallbackTy BodyGenCB,
1536
1537 /// Generator for '#omp critical'
1538 ///
1539 /// \param Loc The insert and source location description.
1540 /// \param BodyGenCB Callback that will generate the region body code.
1541 /// \param FiniCB Callback to finalize variable copies.
1542 /// \param CriticalName name of the lock used by the critical directive
1543 /// \param HintInst Hint Instruction for hint clause associated with critical
1544 ///
1545 /// \returns The insertion position *after* the critical.
1547 BodyGenCallbackTy BodyGenCB,
1548 FinalizeCallbackTy FiniCB,
1549 StringRef CriticalName, Value *HintInst);
1550
1551 /// Generator for '#omp ordered depend (source | sink)'
1552 ///
1553 /// \param Loc The insert and source location description.
1554 /// \param AllocaIP The insertion point to be used for alloca instructions.
1555 /// \param NumLoops The number of loops in depend clause.
1556 /// \param StoreValues The value will be stored in vector address.
1557 /// \param Name The name of alloca instruction.
1558 /// \param IsDependSource If true, depend source; otherwise, depend sink.
1559 ///
1560 /// \return The insertion position *after* the ordered.
1562 InsertPointTy AllocaIP, unsigned NumLoops,
1563 ArrayRef<llvm::Value *> StoreValues,
1564 const Twine &Name, bool IsDependSource);
1565
1566 /// Generator for '#omp ordered [threads | simd]'
1567 ///
1568 /// \param Loc The insert and source location description.
1569 /// \param BodyGenCB Callback that will generate the region code.
1570 /// \param FiniCB Callback to finalize variable copies.
1571 /// \param IsThreads If true, with threads clause or without clause;
1572 /// otherwise, with simd clause;
1573 ///
1574 /// \returns The insertion position *after* the ordered.
1576 BodyGenCallbackTy BodyGenCB,
1577 FinalizeCallbackTy FiniCB,
1578 bool IsThreads);
1579
1580 /// Generator for '#omp sections'
1581 ///
1582 /// \param Loc The insert and source location description.
1583 /// \param AllocaIP The insertion points to be used for alloca instructions.
1584 /// \param SectionCBs Callbacks that will generate body of each section.
1585 /// \param PrivCB Callback to copy a given variable (think copy constructor).
1586 /// \param FiniCB Callback to finalize variable copies.
1587 /// \param IsCancellable Flag to indicate a cancellable parallel region.
1588 /// \param IsNowait If true, barrier - to ensure all sections are executed
1589 /// before moving forward will not be generated.
1590 /// \returns The insertion position *after* the sections.
1592 InsertPointTy AllocaIP,
1594 PrivatizeCallbackTy PrivCB,
1595 FinalizeCallbackTy FiniCB, bool IsCancellable,
1596 bool IsNowait);
1597
1598 /// Generator for '#omp section'
1599 ///
1600 /// \param Loc The insert and source location description.
1601 /// \param BodyGenCB Callback that will generate the region body code.
1602 /// \param FiniCB Callback to finalize variable copies.
1603 /// \returns The insertion position *after* the section.
1605 BodyGenCallbackTy BodyGenCB,
1606 FinalizeCallbackTy FiniCB);
1607
1608 /// Generate conditional branch and relevant BasicBlocks through which private
1609 /// threads copy the 'copyin' variables from Master copy to threadprivate
1610 /// copies.
1611 ///
1612 /// \param IP insertion block for copyin conditional
1613 /// \param MasterVarPtr a pointer to the master variable
1614 /// \param PrivateVarPtr a pointer to the threadprivate variable
1615 /// \param IntPtrTy Pointer size type
1616 /// \param BranchtoEnd Create a branch between the copyin.not.master blocks
1617 // and copy.in.end block
1618 ///
1619 /// \returns The insertion point where copying operation to be emitted.
1621 Value *PrivateAddr,
1622 llvm::IntegerType *IntPtrTy,
1623 bool BranchtoEnd = true);
1624
1625 /// Create a runtime call for kmpc_Alloc
1626 ///
1627 /// \param Loc The insert and source location description.
1628 /// \param Size Size of allocated memory space
1629 /// \param Allocator Allocator information instruction
1630 /// \param Name Name of call Instruction for OMP_alloc
1631 ///
1632 /// \returns CallInst to the OMP_Alloc call
1634 Value *Allocator, std::string Name = "");
1635
1636 /// Create a runtime call for kmpc_free
1637 ///
1638 /// \param Loc The insert and source location description.
1639 /// \param Addr Address of memory space to be freed
1640 /// \param Allocator Allocator information instruction
1641 /// \param Name Name of call Instruction for OMP_Free
1642 ///
1643 /// \returns CallInst to the OMP_Free call
1645 Value *Allocator, std::string Name = "");
1646
1647 /// Create a runtime call for kmpc_threadprivate_cached
1648 ///
1649 /// \param Loc The insert and source location description.
1650 /// \param Pointer pointer to data to be cached
1651 /// \param Size size of data to be cached
1652 /// \param Name Name of call Instruction for callinst
1653 ///
1654 /// \returns CallInst to the thread private cache call.
1656 llvm::Value *Pointer,
1658 const llvm::Twine &Name = Twine(""));
1659
1660 /// Create a runtime call for __tgt_interop_init
1661 ///
1662 /// \param Loc The insert and source location description.
1663 /// \param InteropVar variable to be allocated
1664 /// \param InteropType type of interop operation
1665 /// \param Device devide to which offloading will occur
1666 /// \param NumDependences number of dependence variables
1667 /// \param DependenceAddress pointer to dependence variables
1668 /// \param HaveNowaitClause does nowait clause exist
1669 ///
1670 /// \returns CallInst to the __tgt_interop_init call
1672 Value *InteropVar,
1673 omp::OMPInteropType InteropType, Value *Device,
1674 Value *NumDependences,
1675 Value *DependenceAddress,
1676 bool HaveNowaitClause);
1677
1678 /// Create a runtime call for __tgt_interop_destroy
1679 ///
1680 /// \param Loc The insert and source location description.
1681 /// \param InteropVar variable to be allocated
1682 /// \param Device devide to which offloading will occur
1683 /// \param NumDependences number of dependence variables
1684 /// \param DependenceAddress pointer to dependence variables
1685 /// \param HaveNowaitClause does nowait clause exist
1686 ///
1687 /// \returns CallInst to the __tgt_interop_destroy call
1689 Value *InteropVar, Value *Device,
1690 Value *NumDependences,
1691 Value *DependenceAddress,
1692 bool HaveNowaitClause);
1693
1694 /// Create a runtime call for __tgt_interop_use
1695 ///
1696 /// \param Loc The insert and source location description.
1697 /// \param InteropVar variable to be allocated
1698 /// \param Device devide to which offloading will occur
1699 /// \param NumDependences number of dependence variables
1700 /// \param DependenceAddress pointer to dependence variables
1701 /// \param HaveNowaitClause does nowait clause exist
1702 ///
1703 /// \returns CallInst to the __tgt_interop_use call
1705 Value *InteropVar, Value *Device,
1706 Value *NumDependences, Value *DependenceAddress,
1707 bool HaveNowaitClause);
1708
1709 /// The `omp target` interface
1710 ///
1711 /// For more information about the usage of this interface,
1712 /// \see openmp/libomptarget/deviceRTLs/common/include/target.h
1713 ///
1714 ///{
1715
1716 /// Create a runtime call for kmpc_target_init
1717 ///
1718 /// \param Loc The insert and source location description.
1719 /// \param IsSPMD Flag to indicate if the kernel is an SPMD kernel or not.
1720 InsertPointTy createTargetInit(const LocationDescription &Loc, bool IsSPMD);
1721
1722 /// Create a runtime call for kmpc_target_deinit
1723 ///
1724 /// \param Loc The insert and source location description.
1725 /// \param IsSPMD Flag to indicate if the kernel is an SPMD kernel or not.
1726 void createTargetDeinit(const LocationDescription &Loc, bool IsSPMD);
1727
1728 ///}
1729
1730private:
1731 // Sets the function attributes expected for the outlined function
1732 void setOutlinedTargetRegionFunctionAttributes(Function *OutlinedFn,
1733 int32_t NumTeams,
1734 int32_t NumThreads);
1735
1736 // Creates the function ID/Address for the given outlined function.
1737 // In the case of an embedded device function the address of the function is
1738 // used, in the case of a non-offload function a constant is created.
1739 Constant *createOutlinedFunctionID(Function *OutlinedFn,
1740 StringRef EntryFnIDName);
1741
1742 // Creates the region entry address for the outlined function
1743 Constant *createTargetRegionEntryAddr(Function *OutlinedFunction,
1744 StringRef EntryFnName);
1745
1746public:
1747 /// Functions used to generate a function with the given name.
1748 using FunctionGenCallback = std::function<Function *(StringRef FunctionName)>;
1749
1750 /// Create a unique name for the entry function using the source location
1751 /// information of the current target region. The name will be something like:
1752 ///
1753 /// __omp_offloading_DD_FFFF_PP_lBB[_CC]
1754 ///
1755 /// where DD_FFFF is an ID unique to the file (device and file IDs), PP is the
1756 /// mangled name of the function that encloses the target region and BB is the
1757 /// line number of the target region. CC is a count added when more than one
1758 /// region is located at the same location.
1759 ///
1760 /// If this target outline function is not an offload entry, we don't need to
1761 /// register it. This may happen if it is guarded by an if clause that is
1762 /// false at compile time, or no target archs have been specified.
1763 ///
1764 /// The created target region ID is used by the runtime library to identify
1765 /// the current target region, so it only has to be unique and not
1766 /// necessarily point to anything. It could be the pointer to the outlined
1767 /// function that implements the target region, but we aren't using that so
1768 /// that the compiler doesn't need to keep that, and could therefore inline
1769 /// the host function if proven worthwhile during optimization. In the other
1770 /// hand, if emitting code for the device, the ID has to be the function
1771 /// address so that it can retrieved from the offloading entry and launched
1772 /// by the runtime library. We also mark the outlined function to have
1773 /// external linkage in case we are emitting code for the device, because
1774 /// these functions will be entry points to the device.
1775 ///
1776 /// \param InfoManager The info manager keeping track of the offload entries
1777 /// \param EntryInfo The entry information about the function
1778 /// \param GenerateFunctionCallback The callback function to generate the code
1779 /// \param NumTeams Number default teams
1780 /// \param NumThreads Number default threads
1781 /// \param OutlinedFunction Pointer to the outlined function
1782 /// \param EntryFnIDName Name of the ID o be created
1784 FunctionGenCallback &GenerateFunctionCallback,
1785 int32_t NumTeams, int32_t NumThreads,
1786 bool IsOffloadEntry, Function *&OutlinedFn,
1787 Constant *&OutlinedFnID);
1788
1789 /// Registers the given function and sets up the attribtues of the function
1790 /// Returns the FunctionID.
1791 ///
1792 /// \param InfoManager The info manager keeping track of the offload entries
1793 /// \param EntryInfo The entry information about the function
1794 /// \param OutlinedFunction Pointer to the outlined function
1795 /// \param EntryFnName Name of the outlined function
1796 /// \param EntryFnIDName Name of the ID o be created
1797 /// \param NumTeams Number default teams
1798 /// \param NumThreads Number default threads
1800 Function *OutlinedFunction,
1801 StringRef EntryFnName,
1802 StringRef EntryFnIDName,
1803 int32_t NumTeams, int32_t NumThreads);
1804
1805 /// Generator for '#omp target data'
1806 ///
1807 /// \param Loc The location where the target data construct was encountered.
1808 /// \param CodeGenIP The insertion point at which the target directive code
1809 /// should be placed.
1810 /// \param MapTypeFlags BitVector storing the mapType flags for the
1811 /// mapOperands.
1812 /// \param MapNames Names for the mapOperands.
1813 /// \param MapperAllocas Pointers to the AllocInsts for the map clause.
1814 /// \param IsBegin If true then emits begin mapper call otherwise emits
1815 /// end mapper call.
1816 /// \param DeviceID Stores the DeviceID from the device clause.
1817 /// \param IfCond Value which corresponds to the if clause condition.
1818 /// \param ProcessMapOpCB Callback that generates code for the map clause.
1819 /// \param BodyGenCB Callback that will generate the region code.
1822 SmallVectorImpl<uint64_t> &MapTypeFlags,
1824 struct MapperAllocas &MapperAllocas, bool IsBegin, int64_t DeviceID,
1825 Value *IfCond, BodyGenCallbackTy ProcessMapOpCB,
1826 BodyGenCallbackTy BodyGenCB = {});
1827
1828 /// Declarations for LLVM-IR types (simple, array, function and structure) are
1829 /// generated below. Their names are defined and used in OpenMPKinds.def. Here
1830 /// we provide the declarations, the initializeTypes function will provide the
1831 /// values.
1832 ///
1833 ///{
1834#define OMP_TYPE(VarName, InitValue) Type *VarName = nullptr;
1835#define OMP_ARRAY_TYPE(VarName, ElemTy, ArraySize) \
1836 ArrayType *VarName##Ty = nullptr; \
1837 PointerType *VarName##PtrTy = nullptr;
1838#define OMP_FUNCTION_TYPE(VarName, IsVarArg, ReturnType, ...) \
1839 FunctionType *VarName = nullptr; \
1840 PointerType *VarName##Ptr = nullptr;
1841#define OMP_STRUCT_TYPE(VarName, StrName, ...) \
1842 StructType *VarName = nullptr; \
1843 PointerType *VarName##Ptr = nullptr;
1844#include "llvm/Frontend/OpenMP/OMPKinds.def"
1845
1846 ///}
1847
1848private:
1849 /// Create all simple and struct types exposed by the runtime and remember
1850 /// the llvm::PointerTypes of them for easy access later.
1851 void initializeTypes(Module &M);
1852
1853 /// Common interface for generating entry calls for OMP Directives.
1854 /// if the directive has a region/body, It will set the insertion
1855 /// point to the body
1856 ///
1857 /// \param OMPD Directive to generate entry blocks for
1858 /// \param EntryCall Call to the entry OMP Runtime Function
1859 /// \param ExitBB block where the region ends.
1860 /// \param Conditional indicate if the entry call result will be used
1861 /// to evaluate a conditional of whether a thread will execute
1862 /// body code or not.
1863 ///
1864 /// \return The insertion position in exit block
1865 InsertPointTy emitCommonDirectiveEntry(omp::Directive OMPD, Value *EntryCall,
1866 BasicBlock *ExitBB,
1867 bool Conditional = false);
1868
1869 /// Common interface to finalize the region
1870 ///
1871 /// \param OMPD Directive to generate exiting code for
1872 /// \param FinIP Insertion point for emitting Finalization code and exit call
1873 /// \param ExitCall Call to the ending OMP Runtime Function
1874 /// \param HasFinalize indicate if the directive will require finalization
1875 /// and has a finalization callback in the stack that
1876 /// should be called.
1877 ///
1878 /// \return The insertion position in exit block
1879 InsertPointTy emitCommonDirectiveExit(omp::Directive OMPD,
1880 InsertPointTy FinIP,
1881 Instruction *ExitCall,
1882 bool HasFinalize = true);
1883
1884 /// Common Interface to generate OMP inlined regions
1885 ///
1886 /// \param OMPD Directive to generate inlined region for
1887 /// \param EntryCall Call to the entry OMP Runtime Function
1888 /// \param ExitCall Call to the ending OMP Runtime Function
1889 /// \param BodyGenCB Body code generation callback.
1890 /// \param FiniCB Finalization Callback. Will be called when finalizing region
1891 /// \param Conditional indicate if the entry call result will be used
1892 /// to evaluate a conditional of whether a thread will execute
1893 /// body code or not.
1894 /// \param HasFinalize indicate if the directive will require finalization
1895 /// and has a finalization callback in the stack that
1896 /// should be called.
1897 /// \param IsCancellable if HasFinalize is set to true, indicate if the
1898 /// the directive should be cancellable.
1899 /// \return The insertion point after the region
1900
1902 EmitOMPInlinedRegion(omp::Directive OMPD, Instruction *EntryCall,
1903 Instruction *ExitCall, BodyGenCallbackTy BodyGenCB,
1904 FinalizeCallbackTy FiniCB, bool Conditional = false,
1905 bool HasFinalize = true, bool IsCancellable = false);
1906
1907 /// Get the platform-specific name separator.
1908 /// \param Parts different parts of the final name that needs separation
1909 /// \param FirstSeparator First separator used between the initial two
1910 /// parts of the name.
1911 /// \param Separator separator used between all of the rest consecutive
1912 /// parts of the name
1913 static std::string getNameWithSeparators(ArrayRef<StringRef> Parts,
1914 StringRef FirstSeparator,
1915 StringRef Separator);
1916
1917 /// Returns corresponding lock object for the specified critical region
1918 /// name. If the lock object does not exist it is created, otherwise the
1919 /// reference to the existing copy is returned.
1920 /// \param CriticalName Name of the critical region.
1921 ///
1922 Value *getOMPCriticalRegionLock(StringRef CriticalName);
1923
1924 /// Callback type for Atomic Expression update
1925 /// ex:
1926 /// \code{.cpp}
1927 /// unsigned x = 0;
1928 /// #pragma omp atomic update
1929 /// x = Expr(x_old); //Expr() is any legal operation
1930 /// \endcode
1931 ///
1932 /// \param XOld the value of the atomic memory address to use for update
1933 /// \param IRB reference to the IRBuilder to use
1934 ///
1935 /// \returns Value to update X to.
1936 using AtomicUpdateCallbackTy =
1937 const function_ref<Value *(Value *XOld, IRBuilder<> &IRB)>;
1938
1939private:
1940 enum AtomicKind { Read, Write, Update, Capture, Compare };
1941
1942 /// Determine whether to emit flush or not
1943 ///
1944 /// \param Loc The insert and source location description.
1945 /// \param AO The required atomic ordering
1946 /// \param AK The OpenMP atomic operation kind used.
1947 ///
1948 /// \returns wether a flush was emitted or not
1949 bool checkAndEmitFlushAfterAtomic(const LocationDescription &Loc,
1950 AtomicOrdering AO, AtomicKind AK);
1951
1952 /// Emit atomic update for constructs: X = X BinOp Expr ,or X = Expr BinOp X
1953 /// For complex Operations: X = UpdateOp(X) => CmpExch X, old_X, UpdateOp(X)
1954 /// Only Scalar data types.
1955 ///
1956 /// \param AllocaIP The insertion point to be used for alloca
1957 /// instructions.
1958 /// \param X The target atomic pointer to be updated
1959 /// \param XElemTy The element type of the atomic pointer.
1960 /// \param Expr The value to update X with.
1961 /// \param AO Atomic ordering of the generated atomic
1962 /// instructions.
1963 /// \param RMWOp The binary operation used for update. If
1964 /// operation is not supported by atomicRMW,
1965 /// or belong to {FADD, FSUB, BAD_BINOP}.
1966 /// Then a `cmpExch` based atomic will be generated.
1967 /// \param UpdateOp Code generator for complex expressions that cannot be
1968 /// expressed through atomicrmw instruction.
1969 /// \param VolatileX true if \a X volatile?
1970 /// \param IsXBinopExpr true if \a X is Left H.S. in Right H.S. part of the
1971 /// update expression, false otherwise.
1972 /// (e.g. true for X = X BinOp Expr)
1973 ///
1974 /// \returns A pair of the old value of X before the update, and the value
1975 /// used for the update.
1976 std::pair<Value *, Value *>
1977 emitAtomicUpdate(InsertPointTy AllocaIP, Value *X, Type *XElemTy, Value *Expr,
1979 AtomicUpdateCallbackTy &UpdateOp, bool VolatileX,
1980 bool IsXBinopExpr);
1981
1982 /// Emit the binary op. described by \p RMWOp, using \p Src1 and \p Src2 .
1983 ///
1984 /// \Return The instruction
1985 Value *emitRMWOpAsInstruction(Value *Src1, Value *Src2,
1986 AtomicRMWInst::BinOp RMWOp);
1987
1988public:
1989 /// a struct to pack relevant information while generating atomic Ops
1991 Value *Var = nullptr;
1992 Type *ElemTy = nullptr;
1993 bool IsSigned = false;
1994 bool IsVolatile = false;
1995 };
1996
1997 /// Emit atomic Read for : V = X --- Only Scalar data types.
1998 ///
1999 /// \param Loc The insert and source location description.
2000 /// \param X The target pointer to be atomically read
2001 /// \param V Memory address where to store atomically read
2002 /// value
2003 /// \param AO Atomic ordering of the generated atomic
2004 /// instructions.
2005 ///
2006 /// \return Insertion point after generated atomic read IR.
2009 AtomicOrdering AO);
2010
2011 /// Emit atomic write for : X = Expr --- Only Scalar data types.
2012 ///
2013 /// \param Loc The insert and source location description.
2014 /// \param X The target pointer to be atomically written to
2015 /// \param Expr The value to store.
2016 /// \param AO Atomic ordering of the generated atomic
2017 /// instructions.
2018 ///
2019 /// \return Insertion point after generated atomic Write IR.
2021 AtomicOpValue &X, Value *Expr,
2022 AtomicOrdering AO);
2023
2024 /// Emit atomic update for constructs: X = X BinOp Expr ,or X = Expr BinOp X
2025 /// For complex Operations: X = UpdateOp(X) => CmpExch X, old_X, UpdateOp(X)
2026 /// Only Scalar data types.
2027 ///
2028 /// \param Loc The insert and source location description.
2029 /// \param AllocaIP The insertion point to be used for alloca instructions.
2030 /// \param X The target atomic pointer to be updated
2031 /// \param Expr The value to update X with.
2032 /// \param AO Atomic ordering of the generated atomic instructions.
2033 /// \param RMWOp The binary operation used for update. If operation
2034 /// is not supported by atomicRMW, or belong to
2035 /// {FADD, FSUB, BAD_BINOP}. Then a `cmpExch` based
2036 /// atomic will be generated.
2037 /// \param UpdateOp Code generator for complex expressions that cannot be
2038 /// expressed through atomicrmw instruction.
2039 /// \param IsXBinopExpr true if \a X is Left H.S. in Right H.S. part of the
2040 /// update expression, false otherwise.
2041 /// (e.g. true for X = X BinOp Expr)
2042 ///
2043 /// \return Insertion point after generated atomic update IR.
2045 InsertPointTy AllocaIP, AtomicOpValue &X,
2046 Value *Expr, AtomicOrdering AO,
2048 AtomicUpdateCallbackTy &UpdateOp,
2049 bool IsXBinopExpr);
2050
2051 /// Emit atomic update for constructs: --- Only Scalar data types
2052 /// V = X; X = X BinOp Expr ,
2053 /// X = X BinOp Expr; V = X,
2054 /// V = X; X = Expr BinOp X,
2055 /// X = Expr BinOp X; V = X,
2056 /// V = X; X = UpdateOp(X),
2057 /// X = UpdateOp(X); V = X,
2058 ///
2059 /// \param Loc The insert and source location description.
2060 /// \param AllocaIP The insertion point to be used for alloca instructions.
2061 /// \param X The target atomic pointer to be updated
2062 /// \param V Memory address where to store captured value
2063 /// \param Expr The value to update X with.
2064 /// \param AO Atomic ordering of the generated atomic instructions
2065 /// \param RMWOp The binary operation used for update. If
2066 /// operation is not supported by atomicRMW, or belong to
2067 /// {FADD, FSUB, BAD_BINOP}. Then a cmpExch based
2068 /// atomic will be generated.
2069 /// \param UpdateOp Code generator for complex expressions that cannot be
2070 /// expressed through atomicrmw instruction.
2071 /// \param UpdateExpr true if X is an in place update of the form
2072 /// X = X BinOp Expr or X = Expr BinOp X
2073 /// \param IsXBinopExpr true if X is Left H.S. in Right H.S. part of the
2074 /// update expression, false otherwise.
2075 /// (e.g. true for X = X BinOp Expr)
2076 /// \param IsPostfixUpdate true if original value of 'x' must be stored in
2077 /// 'v', not an updated one.
2078 ///
2079 /// \return Insertion point after generated atomic capture IR.
2082 AtomicOpValue &X, AtomicOpValue &V, Value *Expr,
2084 AtomicUpdateCallbackTy &UpdateOp, bool UpdateExpr,
2085 bool IsPostfixUpdate, bool IsXBinopExpr);
2086
2087 /// Emit atomic compare for constructs: --- Only scalar data types
2088 /// cond-expr-stmt:
2089 /// x = x ordop expr ? expr : x;
2090 /// x = expr ordop x ? expr : x;
2091 /// x = x == e ? d : x;
2092 /// x = e == x ? d : x; (this one is not in the spec)
2093 /// cond-update-stmt:
2094 /// if (x ordop expr) { x = expr; }
2095 /// if (expr ordop x) { x = expr; }
2096 /// if (x == e) { x = d; }
2097 /// if (e == x) { x = d; } (this one is not in the spec)
2098 /// conditional-update-capture-atomic:
2099 /// v = x; cond-update-stmt; (IsPostfixUpdate=true, IsFailOnly=false)
2100 /// cond-update-stmt; v = x; (IsPostfixUpdate=false, IsFailOnly=false)
2101 /// if (x == e) { x = d; } else { v = x; } (IsPostfixUpdate=false,
2102 /// IsFailOnly=true)
2103 /// r = x == e; if (r) { x = d; } (IsPostfixUpdate=false, IsFailOnly=false)
2104 /// r = x == e; if (r) { x = d; } else { v = x; } (IsPostfixUpdate=false,
2105 /// IsFailOnly=true)
2106 ///
2107 /// \param Loc The insert and source location description.
2108 /// \param X The target atomic pointer to be updated.
2109 /// \param V Memory address where to store captured value (for
2110 /// compare capture only).
2111 /// \param R Memory address where to store comparison result
2112 /// (for compare capture with '==' only).
2113 /// \param E The expected value ('e') for forms that use an
2114 /// equality comparison or an expression ('expr') for
2115 /// forms that use 'ordop' (logically an atomic maximum or
2116 /// minimum).
2117 /// \param D The desired value for forms that use an equality
2118 /// comparison. If forms that use 'ordop', it should be
2119 /// \p nullptr.
2120 /// \param AO Atomic ordering of the generated atomic instructions.
2121 /// \param Op Atomic compare operation. It can only be ==, <, or >.
2122 /// \param IsXBinopExpr True if the conditional statement is in the form where
2123 /// x is on LHS. It only matters for < or >.
2124 /// \param IsPostfixUpdate True if original value of 'x' must be stored in
2125 /// 'v', not an updated one (for compare capture
2126 /// only).
2127 /// \param IsFailOnly True if the original value of 'x' is stored to 'v'
2128 /// only when the comparison fails. This is only valid for
2129 /// the case the comparison is '=='.
2130 ///
2131 /// \return Insertion point after generated atomic capture IR.
2136 bool IsXBinopExpr, bool IsPostfixUpdate, bool IsFailOnly);
2137
2138 /// Create the control flow structure of a canonical OpenMP loop.
2139 ///
2140 /// The emitted loop will be disconnected, i.e. no edge to the loop's
2141 /// preheader and no terminator in the AfterBB. The OpenMPIRBuilder's
2142 /// IRBuilder location is not preserved.
2143 ///
2144 /// \param DL DebugLoc used for the instructions in the skeleton.
2145 /// \param TripCount Value to be used for the trip count.
2146 /// \param F Function in which to insert the BasicBlocks.
2147 /// \param PreInsertBefore Where to insert BBs that execute before the body,
2148 /// typically the body itself.
2149 /// \param PostInsertBefore Where to insert BBs that execute after the body.
2150 /// \param Name Base name used to derive BB
2151 /// and instruction names.
2152 ///
2153 /// \returns The CanonicalLoopInfo that represents the emitted loop.
2155 Function *F,
2156 BasicBlock *PreInsertBefore,
2157 BasicBlock *PostInsertBefore,
2158 const Twine &Name = {});
2159 /// OMP Offload Info Metadata name string
2160 const std::string ompOffloadInfoName = "omp_offload.info";
2161
2162 /// Loads all the offload entries information from the host IR
2163 /// metadata. This function is only meant to be used with device code
2164 /// generation.
2165 ///
2166 /// \param M Module to load Metadata info from. Module passed maybe
2167 /// loaded from bitcode file, i.e, different from OpenMPIRBuilder::M module.
2169
2170 /// Gets (if variable with the given name already exist) or creates
2171 /// internal global variable with the specified Name. The created variable has
2172 /// linkage CommonLinkage by default and is initialized by null value.
2173 /// \param Ty Type of the global variable. If it is exist already the type
2174 /// must be the same.
2175 /// \param Name Name of the variable.
2177 unsigned AddressSpace = 0);
2178};
2179
2180/// Class to represented the control flow structure of an OpenMP canonical loop.
2181///
2182/// The control-flow structure is standardized for easy consumption by
2183/// directives associated with loops. For instance, the worksharing-loop
2184/// construct may change this control flow such that each loop iteration is
2185/// executed on only one thread. The constraints of a canonical loop in brief
2186/// are:
2187///
2188/// * The number of loop iterations must have been computed before entering the
2189/// loop.
2190///
2191/// * Has an (unsigned) logical induction variable that starts at zero and
2192/// increments by one.
2193///
2194/// * The loop's CFG itself has no side-effects. The OpenMP specification
2195/// itself allows side-effects, but the order in which they happen, including
2196/// how often or whether at all, is unspecified. We expect that the frontend
2197/// will emit those side-effect instructions somewhere (e.g. before the loop)
2198/// such that the CanonicalLoopInfo itself can be side-effect free.
2199///
2200/// Keep in mind that CanonicalLoopInfo is meant to only describe a repeated
2201/// execution of a loop body that satifies these constraints. It does NOT
2202/// represent arbitrary SESE regions that happen to contain a loop. Do not use
2203/// CanonicalLoopInfo for such purposes.
2204///
2205/// The control flow can be described as follows:
2206///
2207/// Preheader
2208/// |
2209/// /-> Header
2210/// | |
2211/// | Cond---\
2212/// | | |
2213/// | Body |
2214/// | | | |
2215/// | <...> |
2216/// | | | |
2217/// \--Latch |
2218/// |
2219/// Exit
2220/// |
2221/// After
2222///
2223/// The loop is thought to start at PreheaderIP (at the Preheader's terminator,
2224/// including) and end at AfterIP (at the After's first instruction, excluding).
2225/// That is, instructions in the Preheader and After blocks (except the
2226/// Preheader's terminator) are out of CanonicalLoopInfo's control and may have
2227/// side-effects. Typically, the Preheader is used to compute the loop's trip
2228/// count. The instructions from BodyIP (at the Body block's first instruction,
2229/// excluding) until the Latch are also considered outside CanonicalLoopInfo's
2230/// control and thus can have side-effects. The body block is the single entry
2231/// point into the loop body, which may contain arbitrary control flow as long
2232/// as all control paths eventually branch to the Latch block.
2233///
2234/// TODO: Consider adding another standardized BasicBlock between Body CFG and
2235/// Latch to guarantee that there is only a single edge to the latch. It would
2236/// make loop transformations easier to not needing to consider multiple
2237/// predecessors of the latch (See redirectAllPredecessorsTo) and would give us
2238/// an equivalant to PreheaderIP, AfterIP and BodyIP for inserting code that
2239/// executes after each body iteration.
2240///
2241/// There must be no loop-carried dependencies through llvm::Values. This is
2242/// equivalant to that the Latch has no PHINode and the Header's only PHINode is
2243/// for the induction variable.
2244///
2245/// All code in Header, Cond, Latch and Exit (plus the terminator of the
2246/// Preheader) are CanonicalLoopInfo's responsibility and their build-up checked
2247/// by assertOK(). They are expected to not be modified unless explicitly
2248/// modifying the CanonicalLoopInfo through a methods that applies a OpenMP
2249/// loop-associated construct such as applyWorkshareLoop, tileLoops, unrollLoop,
2250/// etc. These methods usually invalidate the CanonicalLoopInfo and re-use its
2251/// basic blocks. After invalidation, the CanonicalLoopInfo must not be used
2252/// anymore as its underlying control flow may not exist anymore.
2253/// Loop-transformation methods such as tileLoops, collapseLoops and unrollLoop
2254/// may also return a new CanonicalLoopInfo that can be passed to other
2255/// loop-associated construct implementing methods. These loop-transforming
2256/// methods may either create a new CanonicalLoopInfo usually using
2257/// createLoopSkeleton and invalidate the input CanonicalLoopInfo, or reuse and
2258/// modify one of the input CanonicalLoopInfo and return it as representing the
2259/// modified loop. What is done is an implementation detail of
2260/// transformation-implementing method and callers should always assume that the
2261/// CanonicalLoopInfo passed to it is invalidated and a new object is returned.
2262/// Returned CanonicalLoopInfo have the same structure and guarantees as the one
2263/// created by createCanonicalLoop, such that transforming methods do not have
2264/// to special case where the CanonicalLoopInfo originated from.
2265///
2266/// Generally, methods consuming CanonicalLoopInfo do not need an
2267/// OpenMPIRBuilder::InsertPointTy as argument, but use the locations of the
2268/// CanonicalLoopInfo to insert new or modify existing instructions. Unless
2269/// documented otherwise, methods consuming CanonicalLoopInfo do not invalidate
2270/// any InsertPoint that is outside CanonicalLoopInfo's control. Specifically,
2271/// any InsertPoint in the Preheader, After or Block can still be used after
2272/// calling such a method.
2273///
2274/// TODO: Provide mechanisms for exception handling and cancellation points.
2275///
2276/// Defined outside OpenMPIRBuilder because nested classes cannot be
2277/// forward-declared, e.g. to avoid having to include the entire OMPIRBuilder.h.
2279 friend class OpenMPIRBuilder;
2280
2281private:
2282 BasicBlock *Header = nullptr;
2283 BasicBlock *Cond = nullptr;
2284 BasicBlock *Latch = nullptr;
2285 BasicBlock *Exit = nullptr;
2286
2287 /// Add the control blocks of this loop to \p BBs.
2288 ///
2289 /// This does not include any block from the body, including the one returned
2290 /// by getBody().
2291 ///
2292 /// FIXME: This currently includes the Preheader and After blocks even though
2293 /// their content is (mostly) not under CanonicalLoopInfo's control.
2294 /// Re-evaluated whether this makes sense.
2295 void collectControlBlocks(SmallVectorImpl<BasicBlock *> &BBs);
2296
2297 /// Sets the number of loop iterations to the given value. This value must be
2298 /// valid in the condition block (i.e., defined in the preheader) and is
2299 /// interpreted as an unsigned integer.
2300 void setTripCount(Value *TripCount);
2301
2302 /// Replace all uses of the canonical induction variable in the loop body with
2303 /// a new one.
2304 ///
2305 /// The intended use case is to update the induction variable for an updated
2306 /// iteration space such that it can stay normalized in the 0...tripcount-1
2307 /// range.
2308 ///
2309 /// The \p Updater is called with the (presumable updated) current normalized
2310 /// induction variable and is expected to return the value that uses of the
2311 /// pre-updated induction values should use instead, typically dependent on
2312 /// the new induction variable. This is a lambda (instead of e.g. just passing
2313 /// the new value) to be able to distinguish the uses of the pre-updated
2314 /// induction variable and uses of the induction varible to compute the
2315 /// updated induction variable value.
2316 void mapIndVar(llvm::function_ref<Value *(Instruction *)> Updater);
2317
2318public:
2319 /// Returns whether this object currently represents the IR of a loop. If
2320 /// returning false, it may have been consumed by a loop transformation or not
2321 /// been intialized. Do not use in this case;
2322 bool isValid() const { return Header; }
2323
2324 /// The preheader ensures that there is only a single edge entering the loop.
2325 /// Code that must be execute before any loop iteration can be emitted here,
2326 /// such as computing the loop trip count and begin lifetime markers. Code in
2327 /// the preheader is not considered part of the canonical loop.
2328 BasicBlock *getPreheader() const;
2329
2330 /// The header is the entry for each iteration. In the canonical control flow,
2331 /// it only contains the PHINode for the induction variable.
2333 assert(isValid() && "Requires a valid canonical loop");
2334 return Header;
2335 }
2336
2337 /// The condition block computes whether there is another loop iteration. If
2338 /// yes, branches to the body; otherwise to the exit block.
2340 assert(isValid() && "Requires a valid canonical loop");
2341 return Cond;
2342 }
2343
2344 /// The body block is the single entry for a loop iteration and not controlled
2345 /// by CanonicalLoopInfo. It can contain arbitrary control flow but must
2346 /// eventually branch to the \p Latch block.
2348 assert(isValid() && "Requires a valid canonical loop");
2349 return cast<BranchInst>(Cond->getTerminator())->getSuccessor(0);
2350 }
2351
2352 /// Reaching the latch indicates the end of the loop body code. In the
2353 /// canonical control flow, it only contains the increment of the induction
2354 /// variable.
2356 assert(isValid() && "Requires a valid canonical loop");
2357 return Latch;
2358 }
2359
2360 /// Reaching the exit indicates no more iterations are being executed.
2362 assert(isValid() && "Requires a valid canonical loop");
2363 return Exit;
2364 }
2365
2366 /// The after block is intended for clean-up code such as lifetime end
2367 /// markers. It is separate from the exit block to ensure, analogous to the
2368 /// preheader, it having just a single entry edge and being free from PHI
2369 /// nodes should there be multiple loop exits (such as from break
2370 /// statements/cancellations).
2372 assert(isValid() && "Requires a valid canonical loop");
2373 return Exit->getSingleSuccessor();
2374 }
2375
2376 /// Returns the llvm::Value containing the number of loop iterations. It must
2377 /// be valid in the preheader and always interpreted as an unsigned integer of
2378 /// any bit-width.
2380 assert(isValid() && "Requires a valid canonical loop");
2381 Instruction *CmpI = &Cond->front();
2382 assert(isa<CmpInst>(CmpI) && "First inst must compare IV with TripCount");
2383 return CmpI->getOperand(1);
2384 }
2385
2386 /// Returns the instruction representing the current logical induction
2387 /// variable. Always unsigned, always starting at 0 with an increment of one.
2389 assert(isValid() && "Requires a valid canonical loop");
2390 Instruction *IndVarPHI = &Header->front();
2391 assert(isa<PHINode>(IndVarPHI) && "First inst must be the IV PHI");
2392 return IndVarPHI;
2393 }
2394
2395 /// Return the type of the induction variable (and the trip count).
2397 assert(isValid() && "Requires a valid canonical loop");
2398 return getIndVar()->getType();
2399 }
2400
2401 /// Return the insertion point for user code before the loop.
2403 assert(isValid() && "Requires a valid canonical loop");
2404 BasicBlock *Preheader = getPreheader();
2405 return {Preheader, std::prev(Preheader->end())};
2406 };
2407
2408 /// Return the insertion point for user code in the body.
2410 assert(isValid() && "Requires a valid canonical loop");
2411 BasicBlock *Body = getBody();
2412 return {Body, Body->begin()};
2413 };
2414
2415 /// Return the insertion point for user code after the loop.
2417 assert(isValid() && "Requires a valid canonical loop");
2418 BasicBlock *After = getAfter();
2419 return {After, After->begin()};
2420 };
2421
2423 assert(isValid() && "Requires a valid canonical loop");
2424 return Header->getParent();
2425 }
2426
2427 /// Consistency self-check.
2428 void assertOK() const;
2429
2430 /// Invalidate this loop. That is, the underlying IR does not fulfill the
2431 /// requirements of an OpenMP canonical loop anymore.
2432 void invalidate();
2433};
2434
2435} // end namespace llvm
2436
2437#endif // LLVM_FRONTEND_OPENMP_OMPIRBUILDER_H
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
arc branch finalize
This file defines the BumpPtrAllocator interface.
assume builder
SmallVector< MachineOperand, 4 > Cond
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
uint64_t Addr
std::string Name
uint64_t Size
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
Hexagon Hardware Loops
Inject TLI Mappings
#define F(x, y, z)
Definition: MD5.cpp:55
This file defines constans and helpers used when dealing with OpenMP.
Basic Register Allocator
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
@ Flags
Definition: TextStubV5.cpp:93
@ Names
Definition: TextStubV5.cpp:106
Value * RHS
an instruction to allocate memory on the stack
Definition: Instructions.h:58
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
BinOp
This enumeration lists the possible modifications atomicrmw can make.
Definition: Instructions.h:730
LLVM Basic Block Representation.
Definition: BasicBlock.h:56
iterator end()
Definition: BasicBlock.h:325
iterator begin()
Instruction iterator methods.
Definition: BasicBlock.h:323
const BasicBlock * getSingleSuccessor() const
Return the successor of this block if it has a single successor.
Definition: BasicBlock.cpp:323
const Function * getParent() const
Return the enclosing method, or null if none.
Definition: BasicBlock.h:112
This class represents a function call, abstracting a target machine's calling convention.
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:78
This is an important base class in LLVM.
Definition: Constant.h:41
A debug info location.
Definition: DebugLoc.h:33
A handy container for a FunctionType+Callee-pointer pair, which can be passed around as a single enti...
Definition: DerivedTypes.h:165
LinkageTypes
An enumeration for the kinds of linkage for global values.
Definition: GlobalValue.h:47
InsertPoint - A saved insertion point.
Definition: IRBuilder.h:243
BasicBlock * getBlock() const
Definition: IRBuilder.h:258
Common base class shared among various IRBuilders.
Definition: IRBuilder.h:94
void SetCurrentDebugLocation(DebugLoc L)
Set location information used by debugging information.
Definition: IRBuilder.h:212
InsertPoint saveIP() const
Returns the current insert point.
Definition: IRBuilder.h:263
void restoreIP(InsertPoint IP)
Sets the current insert point to a previously-saved location.
Definition: IRBuilder.h:275
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition: IRBuilder.h:2564
Class to represent integer types.
Definition: DerivedTypes.h:40
Represents a single loop in the control flow graph.
Definition: LoopInfo.h:547
This class implements a map that also provides access to all stored values in a deterministic order.
Definition: MapVector.h:37
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:338
OffloadEntryInfoDeviceGlobalVar(unsigned Order, Constant *Addr, int64_t VarSize, OMPTargetGlobalVarEntryKind Flags, GlobalValue::LinkageTypes Linkage)
Definition: OMPIRBuilder.h:341
static bool classof(const OffloadEntryInfo *Info)
Definition: OMPIRBuilder.h:354
static bool classof(const OffloadEntryInfo *Info)
Definition: OMPIRBuilder.h:288
OffloadEntryInfoTargetRegion(unsigned Order, Constant *Addr, Constant *ID, OMPTargetRegionEntryKind Flags)
Definition: OMPIRBuilder.h:275
@ OffloadingEntryInfoTargetRegion
Entry is a target region.
Definition: OMPIRBuilder.h:205
@ OffloadingEntryInfoDeviceGlobalVar
Entry is a declare target variable.
Definition: OMPIRBuilder.h:207
OffloadingEntryInfoKinds getKind() const
Definition: OMPIRBuilder.h:223
OffloadEntryInfo(OffloadingEntryInfoKinds Kind)
Definition: OMPIRBuilder.h:214
static bool classof(const OffloadEntryInfo *Info)
Definition: OMPIRBuilder.h:231
OffloadEntryInfo(OffloadingEntryInfoKinds Kind, unsigned Order, uint32_t Flags)
Definition: OMPIRBuilder.h:215
Class that manages information about offload code regions and data.
Definition: OMPIRBuilder.h:193
function_ref< void(StringRef, const OffloadEntryInfoDeviceGlobalVar &)> OffloadDeviceGlobalVarEntryInfoActTy
Applies action Action on all registered entries.
Definition: OMPIRBuilder.h:376
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:258
@ OMPTargetRegionEntryTargetRegion
Mark the entry as target region.
Definition: OMPIRBuilder.h:260
@ OMPTargetRegionEntryDtor
Mark the entry as a global destructor.
Definition: OMPIRBuilder.h:264
@ OMPTargetRegionEntryCtor
Mark the entry as a global constructor.
Definition: OMPIRBuilder.h:262
OffloadEntriesInfoManager(OpenMPIRBuilder *builder)
Definition: OMPIRBuilder.h:251
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:249
void initializeTargetRegionEntryInfo(const TargetRegionEntryInfo &EntryInfo, unsigned Order)
Initialize target region entry.
OMPTargetGlobalVarEntryKind
Kind of the global variable entry..
Definition: OMPIRBuilder.h:322
@ OMPTargetGlobalVarEntryLink
Mark the entry as a to declare target link.
Definition: OMPIRBuilder.h:326
@ OMPTargetGlobalVarEntryTo
Mark the entry as a to declare target.
Definition: OMPIRBuilder.h:324
function_ref< void(const TargetRegionEntryInfo &EntryInfo, const OffloadEntryInfoTargetRegion &)> OffloadTargetRegionEntryInfoActTy
brief Applies action Action on all registered entries.
Definition: OMPIRBuilder.h:313
bool hasDeviceGlobalVarEntryInfo(StringRef VarName) const
Checks if the variable with the given name has been registered already.
Definition: OMPIRBuilder.h:371
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:83
std::optional< StringRef > FirstSeparator
First separator used between the initial two parts of a name.
Definition: OMPIRBuilder.h:101
StringRef separator() const
Definition: OMPIRBuilder.h:147
void setFirstSeparator(StringRef FS)
Definition: OMPIRBuilder.h:160
StringRef firstSeparator() const
Definition: OMPIRBuilder.h:137
std::optional< bool > IsTargetCodegen
Flag for specifying if the compilation is done for an offloading target, like GPU.
Definition: OMPIRBuilder.h:91
std::optional< bool > OpenMPOffloadMandatory
Definition: OMPIRBuilder.h:98
bool hasRequiresUnifiedSharedMemory() const
Definition: OMPIRBuilder.h:124
OpenMPIRBuilderConfig(bool IsEmbedded, bool IsTargetCodegen, bool HasRequiresUnifiedSharedMemory, bool OpenMPOffloadMandatory)
Definition: OMPIRBuilder.h:106
std::optional< bool > HasRequiresUnifiedSharedMemory
Flag for specifying weather a requires unified_shared_memory directive is present or not.
Definition: OMPIRBuilder.h:95
void setIsEmbedded(bool Value)
Definition: OMPIRBuilder.h:155
void setHasRequiresUnifiedSharedMemory(bool Value)
Definition: OMPIRBuilder.h:157
std::optional< StringRef > Separator
Separator used between all of the rest consecutive parts of s name.
Definition: OMPIRBuilder.h:103
bool openMPOffloadMandatory() const
Definition: OMPIRBuilder.h:130
std::optional< bool > IsEmbedded
Flag for specifying if the compilation is done for embedded device code or host code.
Definition: OMPIRBuilder.h:87
void setIsTargetCodegen(bool Value)
Definition: OMPIRBuilder.h:156
void setSeparator(StringRef S)
Definition: OMPIRBuilder.h:161
Struct that keeps the information that should be kept throughout a 'target data' region.
TargetDataInfo(bool RequiresDevicePointerInfo, bool SeparateBeginEndCalls)
void clearArrayInfo()
Clear information about the data arrays.
unsigned NumberOfPtrs
The total number of pointers passed to the runtime library.
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:412
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.
std::function< void(InsertPointTy CodeGenIP)> FinalizeCallbackTy
Callback type for variable finalization (think destructors).
Definition: OMPIRBuilder.h:456
CanonicalLoopInfo * collapseLoops(DebugLoc DL, ArrayRef< CanonicalLoopInfo * > Loops, InsertPointTy ComputeIP)
Collapse a loop nest into a single loop.
function_ref< void(InsertPointTy AllocaIP, InsertPointTy CodeGenIP)> BodyGenCallbackTy
Callback type for body (=inner region) code generation.
Definition: OMPIRBuilder.h:508
void createTaskyield(const LocationDescription &Loc)
Generator for '#omp taskyield'.
InsertPointTy createReductions(const LocationDescription &Loc, InsertPointTy AllocaIP, ArrayRef< ReductionInfo > ReductionInfos, bool IsNoWait=false)
Generator for '#omp reduction'.
InsertPointTy createAtomicWrite(const LocationDescription &Loc, AtomicOpValue &X, Value *Expr, AtomicOrdering AO)
Emit atomic write for : X = Expr — Only Scalar data types.
InsertPointTy createCritical(const LocationDescription &Loc, BodyGenCallbackTy BodyGenCB, FinalizeCallbackTy FiniCB, StringRef CriticalName, Value *HintInst)
Generator for '#omp critical'.
void emitTaskwaitImpl(const LocationDescription &Loc)
Generate a taskwait runtime call.
InsertPointTy 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 ,...
void initialize()
Initialize the internal state, this will put structures types and potentially other helpers into the ...
std::function< void(InsertPointTy AllocaIP, InsertPointTy CodeGenIP)> StorableBodyGenCallbackTy
Definition: OMPIRBuilder.h:515
CanonicalLoopInfo * createCanonicalLoop(const LocationDescription &Loc, LoopBodyGenCallbackTy BodyGenCB, Value *TripCount, const Twine &Name="loop")
Generator for the control flow structure of an OpenMP canonical loop.
void loadOffloadInfoMetadata(Module &M)
Loads all the offload entries information from the host IR metadata.
InsertPointTy 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 = ...
InsertPointTy emitBarrierImpl(const LocationDescription &Loc, omp::Directive DK, bool ForceSimpleCall, bool CheckCancelFlag)
Generate a barrier runtime call.
void unrollLoopFull(DebugLoc DL, CanonicalLoopInfo *Loop)
Fully unroll a loop.
void emitFlush(const LocationDescription &Loc)
Generate a flush runtime call.
InsertPointTy createCancel(const LocationDescription &Loc, Value *IfCondition, omp::Directive CanceledDirective)
Generator for '#omp cancel'.
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.
std::function< void(EmitMetadataErrorKind, TargetRegionEntryInfo)> EmitMetadataErrorReportFunctionTy
Callback function type.
void setConfig(OpenMPIRBuilderConfig C)
Definition: OMPIRBuilder.h:425
InsertPointTy createOrderedThreadsSimd(const LocationDescription &Loc, BodyGenCallbackTy BodyGenCB, FinalizeCallbackTy FiniCB, bool IsThreads)
Generator for '#omp ordered [threads | simd]'.
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.
InsertPointTy createBarrier(const LocationDescription &Loc, omp::Directive DK, bool ForceSimpleCall=false, bool CheckCancelFlag=true)
Emitter methods for OpenMP directives.
std::string createPlatformSpecificName(ArrayRef< StringRef > Parts) const
Get the create a name using the platform specific separators.
void unrollLoopHeuristic(DebugLoc DL, CanonicalLoopInfo *Loop)
Fully or partially unroll a loop.
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.
void createOffloadEntry(Constant *ID, Constant *Addr, uint64_t Size, int32_t Flags, GlobalValue::LinkageTypes)
Creates offloading entry for the provided entry ID ID, address Addr, size Size, and flags Flags.
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.
void emitOffloadingArraysArgument(IRBuilderBase &Builder, OpenMPIRBuilder::TargetDataRTArgs &RTArgs, OpenMPIRBuilder::TargetDataInfo &Info, bool EmitDebug=false, bool ForEndCall=false)
Emit the arguments to be passed to the runtime library based on the arrays of base pointers,...
void unrollLoopPartial(DebugLoc DL, CanonicalLoopInfo *Loop, int32_t Factor, CanonicalLoopInfo **UnrolledCLI)
Partially unroll a loop.
InsertPointTy createSections(const LocationDescription &Loc, InsertPointTy AllocaIP, ArrayRef< StorableBodyGenCallbackTy > SectionCBs, PrivatizeCallbackTy PrivCB, FinalizeCallbackTy FiniCB, bool IsCancellable, bool IsNowait)
Generator for '#omp sections'.
void createTargetDeinit(const LocationDescription &Loc, bool IsSPMD)
Create a runtime call for kmpc_target_deinit.
InsertPointTy createTask(const LocationDescription &Loc, InsertPointTy AllocaIP, BodyGenCallbackTy BodyGenCB, bool Tied=true, Value *Final=nullptr, Value *IfCondition=nullptr, SmallVector< DependData > Dependencies={})
Generator for #omp task
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.
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 ?...
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 emitOffloadingEntry(Constant *Addr, StringRef Name, uint64_t Size, int32_t Flags, StringRef SectionName="omp_offloading_entries")
Create an offloading section struct used to register this global at runtime.
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.
InsertPointTy createSingle(const LocationDescription &Loc, BodyGenCallbackTy BodyGenCB, FinalizeCallbackTy FiniCB, bool IsNowait, llvm::Value *DidIt)
Generator for '#omp single'.
SmallVector< OutlineInfo, 16 > OutlineInfos
Collection of regions that need to be outlined during finalization.
Function * getOrCreateRuntimeFunctionPtr(omp::RuntimeFunction FnID)
StringMap< Constant *, BumpPtrAllocator > InternalVars
An ordered map of auto-generated variables to their unique names.
InsertPointTy 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)
Modifies the canonical loop to be a workshare loop.
void emitTargetRegionFunction(TargetRegionEntryInfo &EntryInfo, FunctionGenCallback &GenerateFunctionCallback, int32_t NumTeams, int32_t NumThreads, bool IsOffloadEntry, Function *&OutlinedFn, Constant *&OutlinedFnID)
Create a unique name for the entry function using the source location information of the current targ...
InsertPointTy createTargetInit(const LocationDescription &Loc, bool IsSPMD)
The omp target interface.
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.
CallInst * createOMPAlloc(const LocationDescription &Loc, Value *Size, Value *Allocator, std::string Name="")
Create a runtime call for kmpc_Alloc.
InsertPointTy createSection(const LocationDescription &Loc, BodyGenCallbackTy BodyGenCB, FinalizeCallbackTy FiniCB)
Generator for '#omp section'.
OpenMPIRBuilder::InsertPointTy createTargetData(const LocationDescription &Loc, OpenMPIRBuilder::InsertPointTy CodeGenIP, SmallVectorImpl< uint64_t > &MapTypeFlags, SmallVectorImpl< Constant * > &MapNames, struct MapperAllocas &MapperAllocas, bool IsBegin, int64_t DeviceID, Value *IfCond, BodyGenCallbackTy ProcessMapOpCB, BodyGenCallbackTy BodyGenCB={})
Generator for '#omp target data'.
Value * getOrCreateThreadID(Value *Ident)
Return the current thread ID.
InsertPointTy 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:474
InsertPointTy getInsertionPoint()
}
IRBuilder ::InsertPoint 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'.
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...
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:436
GlobalVariable * createOffloadMapnames(SmallVectorImpl< llvm::Constant * > &Names, std::string VarName)
Create the global variable holding the offload names information.
Constant * registerTargetRegionFunction(TargetRegionEntryInfo &EntryInfo, Function *OutlinedFunction, StringRef EntryFnName, StringRef EntryFnIDName, int32_t NumTeams, int32_t NumThreads)
Registers the given function and sets up the attribtues of the function Returns the FunctionID.
std::function< Function *(StringRef FunctionName)> FunctionGenCallback
Functions used to generate a function with the given name.
void emitCancelationCheckImpl(Value *CancelFlag, omp::Directive CanceledDirective, FinalizeCallbackTy ExitCB={})
Generate control flow and cleanup for cancellation.
Constant * getOrCreateDefaultSrcLocStr(uint32_t &SrcLocStrSize)
Return the (LLVM-IR) string describing the default source location.
InsertPointTy createMasked(const LocationDescription &Loc, BodyGenCallbackTy BodyGenCB, FinalizeCallbackTy FiniCB, Value *Filter)
Generator for '#omp masked'.
static unsigned getOpenMPDefaultSimdAlign(const Triple &TargetTriple, const StringMap< bool > &Features)
Get the default alignment value for given target.
InsertPointTy createTaskgroup(const LocationDescription &Loc, InsertPointTy AllocaIP, BodyGenCallbackTy BodyGenCB)
Generator for the taskgroup construct.
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.
Value * getSizeInBytes(Value *BasePtr)
Computes the size of type in bytes.
OpenMPIRBuilder(Module &M)
Create a new OpenMPIRBuilder operating on the given module M.
Definition: OMPIRBuilder.h:416
OffloadEntriesInfoManager OffloadInfoManager
Info manager to keep track of target regions.
const std::string ompOffloadInfoName
OMP Offload Info Metadata name string.
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:481
bool updateToLocation(const LocationDescription &Loc)
Update the internal location to Loc.
void createFlush(const LocationDescription &Loc)
Generator for '#omp flush'.
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:344
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: SmallVector.h:577
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1200
StringMap - This is an unconventional map that is specialized for handling keys that are "strings",...
Definition: StringMap.h:111
size_type count(StringRef Key) const
count - Return 1 if the element is in the map, 0 otherwise.
Definition: StringMap.h:256
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
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:169
LLVM Value Representation.
Definition: Value.h:74
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:255
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
IdentFlag
IDs for all omp runtime library ident_t flag encodings (see their defintion in openmp/runtime/src/kmp...
Definition: OMPConstants.h:66
RTLDependenceKindTy
Dependence kind for RTL.
Definition: OMPConstants.h:268
RuntimeFunction
IDs for all omp runtime library (RTL) functions.
Definition: OMPConstants.h:46
OMPAtomicCompareOp
Atomic compare operations. Currently OpenMP only supports ==, >, and <.
Definition: OMPConstants.h:262
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.
AddressSpace
Definition: NVPTXBaseInfo.h:21
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.
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:468
FinalizeCallbackTy FiniCB
The finalization callback provided by the last in-flight invocation of createXXXX for the directive o...
Definition: OMPIRBuilder.h:461
omp::Directive DK
The directive kind of the innermost directive that has an associated region which might require final...
Definition: OMPIRBuilder.h:465
Description of a LLVM-IR insertion point (IP) and a debug/source location (filename,...
Definition: OMPIRBuilder.h:552
LocationDescription(const InsertPointTy &IP)
Definition: OMPIRBuilder.h:555
LocationDescription(const InsertPointTy &IP, const DebugLoc &DL)
Definition: OMPIRBuilder.h:556
LocationDescription(const IRBuilderBase &IRB)
Definition: OMPIRBuilder.h:553
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.
AtomicReductionGenTy AtomicReductionGen
Callback for generating the atomic reduction body, may be null.
Value * Variable
Reduction variable of pointer type.
Value * PrivateVariable
Thread-private partial reduction variable.
ReductionGenTy ReductionGen
Callback for generating the reduction body.
Type * ElementType
Reduction element type, must match pointee type of variable.
ReductionInfo(Type *ElementType, Value *Variable, Value *PrivateVariable, ReductionGenTy ReductionGen, AtomicReductionGenTy AtomicReductionGen)
Container for the arguments used to pass data to the runtime library.
Value * SizesArray
The array of sizes passed to the runtime library.
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 to contain the information needed to uniquely identify a target entry.
Definition: OMPIRBuilder.h:166
static void getTargetRegionEntryFnName(SmallVectorImpl< char > &Name, StringRef ParentName, unsigned DeviceID, unsigned FileID, unsigned Line, unsigned Count)
TargetRegionEntryInfo(StringRef ParentName, unsigned DeviceID, unsigned FileID, unsigned Line, unsigned Count=0)
Definition: OMPIRBuilder.h:175
bool operator<(const TargetRegionEntryInfo RHS) const
Definition: OMPIRBuilder.h:185