LLVM  15.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 
18 #include "llvm/IR/DebugLoc.h"
19 #include "llvm/IR/IRBuilder.h"
20 #include "llvm/Support/Allocator.h"
21 #include <forward_list>
22 
23 namespace llvm {
24 class CanonicalLoopInfo;
25 
26 /// Move the instruction after an InsertPoint to the beginning of another
27 /// BasicBlock.
28 ///
29 /// The instructions after \p IP are moved to the beginning of \p New which must
30 /// not have any PHINodes. If \p CreateBranch is true, a branch instruction to
31 /// \p New will be added such that there is no semantic change. Otherwise, the
32 /// \p IP insert block remains degenerate and it is up to the caller to insert a
33 /// terminator.
34 void spliceBB(IRBuilderBase::InsertPoint IP, BasicBlock *New,
35  bool CreateBranch);
36 
37 /// Splice a BasicBlock at an IRBuilder's current insertion point. Its new
38 /// insert location will stick to after the instruction before the insertion
39 /// point (instead of moving with the instruction the InsertPoint stores
40 /// internally).
41 void spliceBB(IRBuilder<> &Builder, BasicBlock *New, bool CreateBranch);
42 
43 /// Split a BasicBlock at an InsertPoint, even if the block is degenerate
44 /// (missing the terminator).
45 ///
46 /// llvm::SplitBasicBlock and BasicBlock::splitBasicBlock require a well-formed
47 /// BasicBlock. \p Name is used for the new successor block. If \p CreateBranch
48 /// is true, a branch to the new successor will new created such that
49 /// semantically there is no change; otherwise the block of the insertion point
50 /// remains degenerate and it is the caller's responsibility to insert a
51 /// terminator. Returns the new successor block.
52 BasicBlock *splitBB(IRBuilderBase::InsertPoint IP, bool CreateBranch,
53  llvm::Twine Name = {});
54 
55 /// Split a BasicBlock at \p Builder's insertion point, even if the block is
56 /// degenerate (missing the terminator). Its new insert location will stick to
57 /// after the instruction before the insertion point (instead of moving with the
58 /// instruction the InsertPoint stores internally).
59 BasicBlock *splitBB(IRBuilderBase &Builder, bool CreateBranch,
60  llvm::Twine Name = {});
61 
62 /// Split a BasicBlock at \p Builder's insertion point, even if the block is
63 /// degenerate (missing the terminator). Its new insert location will stick to
64 /// after the instruction before the insertion point (instead of moving with the
65 /// instruction the InsertPoint stores internally).
66 BasicBlock *splitBB(IRBuilder<> &Builder, bool CreateBranch, llvm::Twine Name);
67 
68 /// Like splitBB, but reuses the current block's name for the new name.
69 BasicBlock *splitBBWithSuffix(IRBuilderBase &Builder, bool CreateBranch,
70  llvm::Twine Suffix = ".split");
71 
72 /// An interface to create LLVM-IR for OpenMP directives.
73 ///
74 /// Each OpenMP directive has a corresponding public generator method.
76 public:
77  /// Create a new OpenMPIRBuilder operating on the given module \p M. This will
78  /// not have an effect on \p M (see initialize).
79  OpenMPIRBuilder(Module &M) : M(M), Builder(M.getContext()) {}
81 
82  /// Initialize the internal state, this will put structures types and
83  /// potentially other helpers into the underlying module. Must be called
84  /// before any other method and only once!
85  void initialize();
86 
87  /// Finalize the underlying module, e.g., by outlining regions.
88  /// \param Fn The function to be finalized. If not used,
89  /// all functions are finalized.
90  void finalize(Function *Fn = nullptr);
91 
92  /// Add attributes known for \p FnID to \p Fn.
94 
95  /// Type used throughout for insertion points.
97 
98  /// Callback type for variable finalization (think destructors).
99  ///
100  /// \param CodeGenIP is the insertion point at which the finalization code
101  /// should be placed.
102  ///
103  /// A finalize callback knows about all objects that need finalization, e.g.
104  /// destruction, when the scope of the currently generated construct is left
105  /// at the time, and location, the callback is invoked.
107 
109  /// The finalization callback provided by the last in-flight invocation of
110  /// createXXXX for the directive of kind DK.
112 
113  /// The directive kind of the innermost directive that has an associated
114  /// region which might require finalization when it is left.
115  omp::Directive DK;
116 
117  /// Flag to indicate if the directive is cancellable.
119  };
120 
121  /// Push a finalization callback on the finalization stack.
122  ///
123  /// NOTE: Temporary solution until Clang CG is gone.
125  FinalizationStack.push_back(FI);
126  }
127 
128  /// Pop the last finalization callback from the finalization stack.
129  ///
130  /// NOTE: Temporary solution until Clang CG is gone.
131  void popFinalizationCB() { FinalizationStack.pop_back(); }
132 
133  /// Callback type for body (=inner region) code generation
134  ///
135  /// The callback takes code locations as arguments, each describing a
136  /// location where additional instructions can be inserted.
137  ///
138  /// The CodeGenIP may be in the middle of a basic block or point to the end of
139  /// it. The basic block may have a terminator or be degenerate. The callback
140  /// function may just insert instructions at that position, but also split the
141  /// block (without the Before argument of BasicBlock::splitBasicBlock such
142  /// that the identify of the split predecessor block is preserved) and insert
143  /// additional control flow, including branches that do not lead back to what
144  /// follows the CodeGenIP. Note that since the callback is allowed to split
145  /// the block, callers must assume that InsertPoints to positions in the
146  /// BasicBlock after CodeGenIP including CodeGenIP itself are invalidated. If
147  /// such InsertPoints need to be preserved, it can split the block itself
148  /// before calling the callback.
149  ///
150  /// AllocaIP and CodeGenIP must not point to the same position.
151  ///
152  /// \param AllocaIP is the insertion point at which new alloca instructions
153  /// should be placed. The BasicBlock it is pointing to must
154  /// not be split.
155  /// \param CodeGenIP is the insertion point at which the body code should be
156  /// placed.
157  using BodyGenCallbackTy =
158  function_ref<void(InsertPointTy AllocaIP, InsertPointTy CodeGenIP)>;
159 
160  // This is created primarily for sections construct as llvm::function_ref
161  // (BodyGenCallbackTy) is not storable (as described in the comments of
162  // function_ref class - function_ref contains non-ownable reference
163  // to the callable.
165  std::function<void(InsertPointTy AllocaIP, InsertPointTy CodeGenIP)>;
166 
167  /// Callback type for loop body code generation.
168  ///
169  /// \param CodeGenIP is the insertion point where the loop's body code must be
170  /// placed. This will be a dedicated BasicBlock with a
171  /// conditional branch from the loop condition check and
172  /// terminated with an unconditional branch to the loop
173  /// latch.
174  /// \param IndVar is the induction variable usable at the insertion point.
175  using LoopBodyGenCallbackTy =
176  function_ref<void(InsertPointTy CodeGenIP, Value *IndVar)>;
177 
178  /// Callback type for variable privatization (think copy & default
179  /// constructor).
180  ///
181  /// \param AllocaIP is the insertion point at which new alloca instructions
182  /// should be placed.
183  /// \param CodeGenIP is the insertion point at which the privatization code
184  /// should be placed.
185  /// \param Original The value being copied/created, should not be used in the
186  /// generated IR.
187  /// \param Inner The equivalent of \p Original that should be used in the
188  /// generated IR; this is equal to \p Original if the value is
189  /// a pointer and can thus be passed directly, otherwise it is
190  /// an equivalent but different value.
191  /// \param ReplVal The replacement value, thus a copy or new created version
192  /// of \p Inner.
193  ///
194  /// \returns The new insertion point where code generation continues and
195  /// \p ReplVal the replacement value.
197  InsertPointTy AllocaIP, InsertPointTy CodeGenIP, Value &Original,
198  Value &Inner, Value *&ReplVal)>;
199 
200  /// Description of a LLVM-IR insertion point (IP) and a debug/source location
201  /// (filename, line, column, ...).
204  : IP(IRB.saveIP()), DL(IRB.getCurrentDebugLocation()) {}
207  : IP(IP), DL(DL) {}
210  };
211 
212  /// Emitter methods for OpenMP directives.
213  ///
214  ///{
215 
216  /// Generator for '#omp barrier'
217  ///
218  /// \param Loc The location where the barrier directive was encountered.
219  /// \param DK The kind of directive that caused the barrier.
220  /// \param ForceSimpleCall Flag to force a simple (=non-cancellation) barrier.
221  /// \param CheckCancelFlag Flag to indicate a cancel barrier return value
222  /// should be checked and acted upon.
223  ///
224  /// \returns The insertion point after the barrier.
225  InsertPointTy createBarrier(const LocationDescription &Loc, omp::Directive DK,
226  bool ForceSimpleCall = false,
227  bool CheckCancelFlag = true);
228 
229  /// Generator for '#omp cancel'
230  ///
231  /// \param Loc The location where the directive was encountered.
232  /// \param IfCondition The evaluated 'if' clause expression, if any.
233  /// \param CanceledDirective The kind of directive that is cancled.
234  ///
235  /// \returns The insertion point after the barrier.
236  InsertPointTy createCancel(const LocationDescription &Loc, Value *IfCondition,
237  omp::Directive CanceledDirective);
238 
239  /// Generator for '#omp parallel'
240  ///
241  /// \param Loc The insert and source location description.
242  /// \param AllocaIP The insertion points to be used for alloca instructions.
243  /// \param BodyGenCB Callback that will generate the region code.
244  /// \param PrivCB Callback to copy a given variable (think copy constructor).
245  /// \param FiniCB Callback to finalize variable copies.
246  /// \param IfCondition The evaluated 'if' clause expression, if any.
247  /// \param NumThreads The evaluated 'num_threads' clause expression, if any.
248  /// \param ProcBind The value of the 'proc_bind' clause (see ProcBindKind).
249  /// \param IsCancellable Flag to indicate a cancellable parallel region.
250  ///
251  /// \returns The insertion position *after* the parallel.
253  createParallel(const LocationDescription &Loc, InsertPointTy AllocaIP,
254  BodyGenCallbackTy BodyGenCB, PrivatizeCallbackTy PrivCB,
255  FinalizeCallbackTy FiniCB, Value *IfCondition,
256  Value *NumThreads, omp::ProcBindKind ProcBind,
257  bool IsCancellable);
258 
259  /// Generator for the control flow structure of an OpenMP canonical loop.
260  ///
261  /// This generator operates on the logical iteration space of the loop, i.e.
262  /// the caller only has to provide a loop trip count of the loop as defined by
263  /// base language semantics. The trip count is interpreted as an unsigned
264  /// integer. The induction variable passed to \p BodyGenCB will be of the same
265  /// type and run from 0 to \p TripCount - 1. It is up to the callback to
266  /// convert the logical iteration variable to the loop counter variable in the
267  /// loop body.
268  ///
269  /// \param Loc The insert and source location description. The insert
270  /// location can be between two instructions or the end of a
271  /// degenerate block (e.g. a BB under construction).
272  /// \param BodyGenCB Callback that will generate the loop body code.
273  /// \param TripCount Number of iterations the loop body is executed.
274  /// \param Name Base name used to derive BB and instruction names.
275  ///
276  /// \returns An object representing the created control flow structure which
277  /// can be used for loop-associated directives.
279  LoopBodyGenCallbackTy BodyGenCB,
280  Value *TripCount,
281  const Twine &Name = "loop");
282 
283  /// Generator for the control flow structure of an OpenMP canonical loop.
284  ///
285  /// Instead of a logical iteration space, this allows specifying user-defined
286  /// loop counter values using increment, upper- and lower bounds. To
287  /// disambiguate the terminology when counting downwards, instead of lower
288  /// bounds we use \p Start for the loop counter value in the first body
289  /// iteration.
290  ///
291  /// Consider the following limitations:
292  ///
293  /// * A loop counter space over all integer values of its bit-width cannot be
294  /// represented. E.g using uint8_t, its loop trip count of 256 cannot be
295  /// stored into an 8 bit integer):
296  ///
297  /// DO I = 0, 255, 1
298  ///
299  /// * Unsigned wrapping is only supported when wrapping only "once"; E.g.
300  /// effectively counting downwards:
301  ///
302  /// for (uint8_t i = 100u; i > 0; i += 127u)
303  ///
304  ///
305  /// TODO: May need to add additional parameters to represent:
306  ///
307  /// * Allow representing downcounting with unsigned integers.
308  ///
309  /// * Sign of the step and the comparison operator might disagree:
310  ///
311  /// for (int i = 0; i < 42; i -= 1u)
312  ///
313  //
314  /// \param Loc The insert and source location description.
315  /// \param BodyGenCB Callback that will generate the loop body code.
316  /// \param Start Value of the loop counter for the first iterations.
317  /// \param Stop Loop counter values past this will stop the loop.
318  /// \param Step Loop counter increment after each iteration; negative
319  /// means counting down.
320  /// \param IsSigned Whether Start, Stop and Step are signed integers.
321  /// \param InclusiveStop Whether \p Stop itself is a valid value for the loop
322  /// counter.
323  /// \param ComputeIP Insertion point for instructions computing the trip
324  /// count. Can be used to ensure the trip count is available
325  /// at the outermost loop of a loop nest. If not set,
326  /// defaults to the preheader of the generated loop.
327  /// \param Name Base name used to derive BB and instruction names.
328  ///
329  /// \returns An object representing the created control flow structure which
330  /// can be used for loop-associated directives.
332  LoopBodyGenCallbackTy BodyGenCB,
333  Value *Start, Value *Stop, Value *Step,
334  bool IsSigned, bool InclusiveStop,
335  InsertPointTy ComputeIP = {},
336  const Twine &Name = "loop");
337 
338  /// Collapse a loop nest into a single loop.
339  ///
340  /// Merges loops of a loop nest into a single CanonicalLoopNest representation
341  /// that has the same number of innermost loop iterations as the origin loop
342  /// nest. The induction variables of the input loops are derived from the
343  /// collapsed loop's induction variable. This is intended to be used to
344  /// implement OpenMP's collapse clause. Before applying a directive,
345  /// collapseLoops normalizes a loop nest to contain only a single loop and the
346  /// directive's implementation does not need to handle multiple loops itself.
347  /// This does not remove the need to handle all loop nest handling by
348  /// directives, such as the ordered(<n>) clause or the simd schedule-clause
349  /// modifier of the worksharing-loop directive.
350  ///
351  /// Example:
352  /// \code
353  /// for (int i = 0; i < 7; ++i) // Canonical loop "i"
354  /// for (int j = 0; j < 9; ++j) // Canonical loop "j"
355  /// body(i, j);
356  /// \endcode
357  ///
358  /// After collapsing with Loops={i,j}, the loop is changed to
359  /// \code
360  /// for (int ij = 0; ij < 63; ++ij) {
361  /// int i = ij / 9;
362  /// int j = ij % 9;
363  /// body(i, j);
364  /// }
365  /// \endcode
366  ///
367  /// In the current implementation, the following limitations apply:
368  ///
369  /// * All input loops have an induction variable of the same type.
370  ///
371  /// * The collapsed loop will have the same trip count integer type as the
372  /// input loops. Therefore it is possible that the collapsed loop cannot
373  /// represent all iterations of the input loops. For instance, assuming a
374  /// 32 bit integer type, and two input loops both iterating 2^16 times, the
375  /// theoretical trip count of the collapsed loop would be 2^32 iteration,
376  /// which cannot be represented in an 32-bit integer. Behavior is undefined
377  /// in this case.
378  ///
379  /// * The trip counts of every input loop must be available at \p ComputeIP.
380  /// Non-rectangular loops are not yet supported.
381  ///
382  /// * At each nest level, code between a surrounding loop and its nested loop
383  /// is hoisted into the loop body, and such code will be executed more
384  /// often than before collapsing (or not at all if any inner loop iteration
385  /// has a trip count of 0). This is permitted by the OpenMP specification.
386  ///
387  /// \param DL Debug location for instructions added for collapsing,
388  /// such as instructions to compute/derive the input loop's
389  /// induction variables.
390  /// \param Loops Loops in the loop nest to collapse. Loops are specified
391  /// from outermost-to-innermost and every control flow of a
392  /// loop's body must pass through its directly nested loop.
393  /// \param ComputeIP Where additional instruction that compute the collapsed
394  /// trip count. If not set, defaults to before the generated
395  /// loop.
396  ///
397  /// \returns The CanonicalLoopInfo object representing the collapsed loop.
400  InsertPointTy ComputeIP);
401 
402 private:
403  /// Modifies the canonical loop to be a statically-scheduled workshare loop.
404  ///
405  /// This takes a \p LoopInfo representing a canonical loop, such as the one
406  /// created by \p createCanonicalLoop and emits additional instructions to
407  /// turn it into a workshare loop. In particular, it calls to an OpenMP
408  /// runtime function in the preheader to obtain the loop bounds to be used in
409  /// the current thread, updates the relevant instructions in the canonical
410  /// loop and calls to an OpenMP runtime finalization function after the loop.
411  ///
412  /// \param DL Debug location for instructions added for the
413  /// workshare-loop construct itself.
414  /// \param CLI A descriptor of the canonical loop to workshare.
415  /// \param AllocaIP An insertion point for Alloca instructions usable in the
416  /// preheader of the loop.
417  /// \param NeedsBarrier Indicates whether a barrier must be inserted after
418  /// the loop.
419  ///
420  /// \returns Point where to insert code after the workshare construct.
421  InsertPointTy applyStaticWorkshareLoop(DebugLoc DL, CanonicalLoopInfo *CLI,
422  InsertPointTy AllocaIP,
423  bool NeedsBarrier);
424 
425  /// Modifies the canonical loop a statically-scheduled workshare loop with a
426  /// user-specified chunk size.
427  ///
428  /// \param DL Debug location for instructions added for the
429  /// workshare-loop construct itself.
430  /// \param CLI A descriptor of the canonical loop to workshare.
431  /// \param AllocaIP An insertion point for Alloca instructions usable in
432  /// the preheader of the loop.
433  /// \param NeedsBarrier Indicates whether a barrier must be inserted after the
434  /// loop.
435  /// \param ChunkSize The user-specified chunk size.
436  ///
437  /// \returns Point where to insert code after the workshare construct.
438  InsertPointTy applyStaticChunkedWorkshareLoop(DebugLoc DL,
439  CanonicalLoopInfo *CLI,
440  InsertPointTy AllocaIP,
441  bool NeedsBarrier,
442  Value *ChunkSize);
443 
444  /// Modifies the canonical loop to be a dynamically-scheduled workshare loop.
445  ///
446  /// This takes a \p LoopInfo representing a canonical loop, such as the one
447  /// created by \p createCanonicalLoop and emits additional instructions to
448  /// turn it into a workshare loop. In particular, it calls to an OpenMP
449  /// runtime function in the preheader to obtain, and then in each iteration
450  /// to update the loop counter.
451  ///
452  /// \param DL Debug location for instructions added for the
453  /// workshare-loop construct itself.
454  /// \param CLI A descriptor of the canonical loop to workshare.
455  /// \param AllocaIP An insertion point for Alloca instructions usable in the
456  /// preheader of the loop.
457  /// \param SchedType Type of scheduling to be passed to the init function.
458  /// \param NeedsBarrier Indicates whether a barrier must be insterted after
459  /// the loop.
460  /// \param Chunk The size of loop chunk considered as a unit when
461  /// scheduling. If \p nullptr, defaults to 1.
462  ///
463  /// \returns Point where to insert code after the workshare construct.
464  InsertPointTy applyDynamicWorkshareLoop(DebugLoc DL, CanonicalLoopInfo *CLI,
465  InsertPointTy AllocaIP,
466  omp::OMPScheduleType SchedType,
467  bool NeedsBarrier,
468  Value *Chunk = nullptr);
469 
470 public:
471  /// Modifies the canonical loop to be a workshare loop.
472  ///
473  /// This takes a \p LoopInfo representing a canonical loop, such as the one
474  /// created by \p createCanonicalLoop and emits additional instructions to
475  /// turn it into a workshare loop. In particular, it calls to an OpenMP
476  /// runtime function in the preheader to obtain the loop bounds to be used in
477  /// the current thread, updates the relevant instructions in the canonical
478  /// loop and calls to an OpenMP runtime finalization function after the loop.
479  ///
480  /// The concrete transformation is done by applyStaticWorkshareLoop,
481  /// applyStaticChunkedWorkshareLoop, or applyDynamicWorkshareLoop, depending
482  /// on the value of \p SchedKind and \p ChunkSize.
483  ///
484  /// \param DL Debug location for instructions added for the
485  /// workshare-loop construct itself.
486  /// \param CLI A descriptor of the canonical loop to workshare.
487  /// \param AllocaIP An insertion point for Alloca instructions usable in the
488  /// preheader of the loop.
489  /// \param NeedsBarrier Indicates whether a barrier must be insterted after
490  /// the loop.
491  /// \param SchedKind Scheduling algorithm to use.
492  /// \param ChunkSize The chunk size for the inner loop.
493  /// \param HasSimdModifier Whether the simd modifier is present in the
494  /// schedule clause.
495  /// \param HasMonotonicModifier Whether the monotonic modifier is present in
496  /// the schedule clause.
497  /// \param HasNonmonotonicModifier Whether the nonmonotonic modifier is
498  /// present in the schedule clause.
499  /// \param HasOrderedClause Whether the (parameterless) ordered clause is
500  /// present.
501  ///
502  /// \returns Point where to insert code after the workshare construct.
504  DebugLoc DL, CanonicalLoopInfo *CLI, InsertPointTy AllocaIP,
505  bool NeedsBarrier,
506  llvm::omp::ScheduleKind SchedKind = llvm::omp::OMP_SCHEDULE_Default,
507  Value *ChunkSize = nullptr, bool HasSimdModifier = false,
508  bool HasMonotonicModifier = false, bool HasNonmonotonicModifier = false,
509  bool HasOrderedClause = false);
510 
511  /// Tile a loop nest.
512  ///
513  /// Tiles the loops of \p Loops by the tile sizes in \p TileSizes. Loops in
514  /// \p/ Loops must be perfectly nested, from outermost to innermost loop
515  /// (i.e. Loops.front() is the outermost loop). The trip count llvm::Value
516  /// of every loop and every tile sizes must be usable in the outermost
517  /// loop's preheader. This implies that the loop nest is rectangular.
518  ///
519  /// Example:
520  /// \code
521  /// for (int i = 0; i < 15; ++i) // Canonical loop "i"
522  /// for (int j = 0; j < 14; ++j) // Canonical loop "j"
523  /// body(i, j);
524  /// \endcode
525  ///
526  /// After tiling with Loops={i,j} and TileSizes={5,7}, the loop is changed to
527  /// \code
528  /// for (int i1 = 0; i1 < 3; ++i1)
529  /// for (int j1 = 0; j1 < 2; ++j1)
530  /// for (int i2 = 0; i2 < 5; ++i2)
531  /// for (int j2 = 0; j2 < 7; ++j2)
532  /// body(i1*3+i2, j1*3+j2);
533  /// \endcode
534  ///
535  /// The returned vector are the loops {i1,j1,i2,j2}. The loops i1 and j1 are
536  /// referred to the floor, and the loops i2 and j2 are the tiles. Tiling also
537  /// handles non-constant trip counts, non-constant tile sizes and trip counts
538  /// that are not multiples of the tile size. In the latter case the tile loop
539  /// of the last floor-loop iteration will have fewer iterations than specified
540  /// as its tile size.
541  ///
542  ///
543  /// @param DL Debug location for instructions added by tiling, for
544  /// instance the floor- and tile trip count computation.
545  /// @param Loops Loops to tile. The CanonicalLoopInfo objects are
546  /// invalidated by this method, i.e. should not used after
547  /// tiling.
548  /// @param TileSizes For each loop in \p Loops, the tile size for that
549  /// dimensions.
550  ///
551  /// \returns A list of generated loops. Contains twice as many loops as the
552  /// input loop nest; the first half are the floor loops and the
553  /// second half are the tile loops.
554  std::vector<CanonicalLoopInfo *>
556  ArrayRef<Value *> TileSizes);
557 
558  /// Fully unroll a loop.
559  ///
560  /// Instead of unrolling the loop immediately (and duplicating its body
561  /// instructions), it is deferred to LLVM's LoopUnrollPass by adding loop
562  /// metadata.
563  ///
564  /// \param DL Debug location for instructions added by unrolling.
565  /// \param Loop The loop to unroll. The loop will be invalidated.
567 
568  /// Fully or partially unroll a loop. How the loop is unrolled is determined
569  /// using LLVM's LoopUnrollPass.
570  ///
571  /// \param DL Debug location for instructions added by unrolling.
572  /// \param Loop The loop to unroll. The loop will be invalidated.
574 
575  /// Partially unroll a loop.
576  ///
577  /// The CanonicalLoopInfo of the unrolled loop for use with chained
578  /// loop-associated directive can be requested using \p UnrolledCLI. Not
579  /// needing the CanonicalLoopInfo allows more efficient code generation by
580  /// deferring the actual unrolling to the LoopUnrollPass using loop metadata.
581  /// A loop-associated directive applied to the unrolled loop needs to know the
582  /// new trip count which means that if using a heuristically determined unroll
583  /// factor (\p Factor == 0), that factor must be computed immediately. We are
584  /// using the same logic as the LoopUnrollPass to derived the unroll factor,
585  /// but which assumes that some canonicalization has taken place (e.g.
586  /// Mem2Reg, LICM, GVN, Inlining, etc.). That is, the heuristic will perform
587  /// better when the unrolled loop's CanonicalLoopInfo is not needed.
588  ///
589  /// \param DL Debug location for instructions added by unrolling.
590  /// \param Loop The loop to unroll. The loop will be invalidated.
591  /// \param Factor The factor to unroll the loop by. A factor of 0
592  /// indicates that a heuristic should be used to determine
593  /// the unroll-factor.
594  /// \param UnrolledCLI If non-null, receives the CanonicalLoopInfo of the
595  /// partially unrolled loop. Otherwise, uses loop metadata
596  /// to defer unrolling to the LoopUnrollPass.
597  void unrollLoopPartial(DebugLoc DL, CanonicalLoopInfo *Loop, int32_t Factor,
598  CanonicalLoopInfo **UnrolledCLI);
599 
600  /// Add metadata to simd-ize a loop.
601  ///
602  /// \param DL Debug location for instructions added by unrolling.
603  /// \param Loop The loop to simd-ize.
605 
606  /// Generator for '#omp flush'
607  ///
608  /// \param Loc The location where the flush directive was encountered
609  void createFlush(const LocationDescription &Loc);
610 
611  /// Generator for '#omp taskwait'
612  ///
613  /// \param Loc The location where the taskwait directive was encountered.
614  void createTaskwait(const LocationDescription &Loc);
615 
616  /// Generator for '#omp taskyield'
617  ///
618  /// \param Loc The location where the taskyield directive was encountered.
619  void createTaskyield(const LocationDescription &Loc);
620 
621  /// Generator for `#omp task`
622  ///
623  /// \param Loc The location where the task construct was encountered.
624  /// \param AllocaIP The insertion point to be used for alloca instructions.
625  /// \param BodyGenCB Callback that will generate the region code.
626  /// \param Tied True if the task is tied, false if the task is untied.
627  InsertPointTy createTask(const LocationDescription &Loc,
628  InsertPointTy AllocaIP, BodyGenCallbackTy BodyGenCB,
629  bool Tied = true);
630 
631  /// Functions used to generate reductions. Such functions take two Values
632  /// representing LHS and RHS of the reduction, respectively, and a reference
633  /// to the value that is updated to refer to the reduction result.
634  using ReductionGenTy =
636 
637  /// Functions used to generate atomic reductions. Such functions take two
638  /// Values representing pointers to LHS and RHS of the reduction, as well as
639  /// the element type of these pointers. They are expected to atomically
640  /// update the LHS to the reduced value.
641  using AtomicReductionGenTy =
643 
644  /// Information about an OpenMP reduction.
645  struct ReductionInfo {
652  assert(cast<PointerType>(Variable->getType())
653  ->isOpaqueOrPointeeTypeMatches(ElementType) && "Invalid elem type");
654  }
655 
656  /// Reduction element type, must match pointee type of variable.
658 
659  /// Reduction variable of pointer type.
661 
662  /// Thread-private partial reduction variable.
664 
665  /// Callback for generating the reduction body. The IR produced by this will
666  /// be used to combine two values in a thread-safe context, e.g., under
667  /// lock or within the same thread, and therefore need not be atomic.
669 
670  /// Callback for generating the atomic reduction body, may be null. The IR
671  /// produced by this will be used to atomically combine two values during
672  /// reduction. If null, the implementation will use the non-atomic version
673  /// along with the appropriate synchronization mechanisms.
675  };
676 
677  // TODO: provide atomic and non-atomic reduction generators for reduction
678  // operators defined by the OpenMP specification.
679 
680  /// Generator for '#omp reduction'.
681  ///
682  /// Emits the IR instructing the runtime to perform the specific kind of
683  /// reductions. Expects reduction variables to have been privatized and
684  /// initialized to reduction-neutral values separately. Emits the calls to
685  /// runtime functions as well as the reduction function and the basic blocks
686  /// performing the reduction atomically and non-atomically.
687  ///
688  /// The code emitted for the following:
689  ///
690  /// \code
691  /// type var_1;
692  /// type var_2;
693  /// #pragma omp <directive> reduction(reduction-op:var_1,var_2)
694  /// /* body */;
695  /// \endcode
696  ///
697  /// corresponds to the following sketch.
698  ///
699  /// \code
700  /// void _outlined_par() {
701  /// // N is the number of different reductions.
702  /// void *red_array[] = {privatized_var_1, privatized_var_2, ...};
703  /// switch(__kmpc_reduce(..., N, /*size of data in red array*/, red_array,
704  /// _omp_reduction_func,
705  /// _gomp_critical_user.reduction.var)) {
706  /// case 1: {
707  /// var_1 = var_1 <reduction-op> privatized_var_1;
708  /// var_2 = var_2 <reduction-op> privatized_var_2;
709  /// // ...
710  /// __kmpc_end_reduce(...);
711  /// break;
712  /// }
713  /// case 2: {
714  /// _Atomic<ReductionOp>(var_1, privatized_var_1);
715  /// _Atomic<ReductionOp>(var_2, privatized_var_2);
716  /// // ...
717  /// break;
718  /// }
719  /// default: break;
720  /// }
721  /// }
722  ///
723  /// void _omp_reduction_func(void **lhs, void **rhs) {
724  /// *(type *)lhs[0] = *(type *)lhs[0] <reduction-op> *(type *)rhs[0];
725  /// *(type *)lhs[1] = *(type *)lhs[1] <reduction-op> *(type *)rhs[1];
726  /// // ...
727  /// }
728  /// \endcode
729  ///
730  /// \param Loc The location where the reduction was
731  /// encountered. Must be within the associate
732  /// directive and after the last local access to the
733  /// reduction variables.
734  /// \param AllocaIP An insertion point suitable for allocas usable
735  /// in reductions.
736  /// \param ReductionInfos A list of info on each reduction variable.
737  /// \param IsNoWait A flag set if the reduction is marked as nowait.
739  InsertPointTy AllocaIP,
740  ArrayRef<ReductionInfo> ReductionInfos,
741  bool IsNoWait = false);
742 
743  ///}
744 
745  /// Return the insertion point used by the underlying IRBuilder.
747 
748  /// Update the internal location to \p Loc.
750  Builder.restoreIP(Loc.IP);
752  return Loc.IP.getBlock() != nullptr;
753  }
754 
755  /// Return the function declaration for the runtime function with \p FnID.
757  omp::RuntimeFunction FnID);
758 
760 
761  /// Return the (LLVM-IR) string describing the source location \p LocStr.
762  Constant *getOrCreateSrcLocStr(StringRef LocStr, uint32_t &SrcLocStrSize);
763 
764  /// Return the (LLVM-IR) string describing the default source location.
766 
767  /// Return the (LLVM-IR) string describing the source location identified by
768  /// the arguments.
769  Constant *getOrCreateSrcLocStr(StringRef FunctionName, StringRef FileName,
770  unsigned Line, unsigned Column,
771  uint32_t &SrcLocStrSize);
772 
773  /// Return the (LLVM-IR) string describing the DebugLoc \p DL. Use \p F as
774  /// fallback if \p DL does not specify the function name.
776  Function *F = nullptr);
777 
778  /// Return the (LLVM-IR) string describing the source location \p Loc.
779  Constant *getOrCreateSrcLocStr(const LocationDescription &Loc,
780  uint32_t &SrcLocStrSize);
781 
782  /// Return an ident_t* encoding the source location \p SrcLocStr and \p Flags.
783  /// TODO: Create a enum class for the Reserve2Flags
784  Constant *getOrCreateIdent(Constant *SrcLocStr, uint32_t SrcLocStrSize,
785  omp::IdentFlag Flags = omp::IdentFlag(0),
786  unsigned Reserve2Flags = 0);
787 
788  /// Create a hidden global flag \p Name in the module with initial value \p
789  /// Value.
791 
792  /// Create an offloading section struct used to register this global at
793  /// runtime.
794  ///
795  /// Type struct __tgt_offload_entry{
796  /// void *addr; // Pointer to the offload entry info.
797  /// // (function or global)
798  /// char *name; // Name of the function or global.
799  /// size_t size; // Size of the entry info (0 if it a function).
800  /// int32_t flags;
801  /// int32_t reserved;
802  /// };
803  ///
804  /// \param Addr The pointer to the global being registered.
805  /// \param Name The symbol name associated with the global.
806  /// \param Size The size in bytes of the global (0 for functions).
807  /// \param Flags Flags associated with the entry.
808  /// \param SectionName The section this entry will be placed at.
810  int32_t Flags,
811  StringRef SectionName = "omp_offloading_entries");
812 
813  /// Generate control flow and cleanup for cancellation.
814  ///
815  /// \param CancelFlag Flag indicating if the cancellation is performed.
816  /// \param CanceledDirective The kind of directive that is cancled.
817  /// \param ExitCB Extra code to be generated in the exit block.
818  void emitCancelationCheckImpl(Value *CancelFlag,
819  omp::Directive CanceledDirective,
820  FinalizeCallbackTy ExitCB = {});
821 
822  /// Generate a barrier runtime call.
823  ///
824  /// \param Loc The location at which the request originated and is fulfilled.
825  /// \param DK The directive which caused the barrier
826  /// \param ForceSimpleCall Flag to force a simple (=non-cancellation) barrier.
827  /// \param CheckCancelFlag Flag to indicate a cancel barrier return value
828  /// should be checked and acted upon.
829  ///
830  /// \returns The insertion point after the barrier.
831  InsertPointTy emitBarrierImpl(const LocationDescription &Loc,
832  omp::Directive DK, bool ForceSimpleCall,
833  bool CheckCancelFlag);
834 
835  /// Generate a flush runtime call.
836  ///
837  /// \param Loc The location at which the request originated and is fulfilled.
838  void emitFlush(const LocationDescription &Loc);
839 
840  /// The finalization stack made up of finalize callbacks currently in-flight,
841  /// wrapped into FinalizationInfo objects that reference also the finalization
842  /// target block and the kind of cancellable directive.
844 
845  /// Return true if the last entry in the finalization stack is of kind \p DK
846  /// and cancellable.
847  bool isLastFinalizationInfoCancellable(omp::Directive DK) {
848  return !FinalizationStack.empty() &&
849  FinalizationStack.back().IsCancellable &&
850  FinalizationStack.back().DK == DK;
851  }
852 
853  /// Generate a taskwait runtime call.
854  ///
855  /// \param Loc The location at which the request originated and is fulfilled.
856  void emitTaskwaitImpl(const LocationDescription &Loc);
857 
858  /// Generate a taskyield runtime call.
859  ///
860  /// \param Loc The location at which the request originated and is fulfilled.
861  void emitTaskyieldImpl(const LocationDescription &Loc);
862 
863  /// Return the current thread ID.
864  ///
865  /// \param Ident The ident (ident_t*) describing the query origin.
867 
868  /// The underlying LLVM-IR module
870 
871  /// The LLVM-IR Builder used to create IR.
873 
874  /// Map to remember source location strings
876 
877  /// Map to remember existing ident_t*.
879 
880  /// Helper that contains information about regions we need to outline
881  /// during finalization.
882  struct OutlineInfo {
887 
888  /// Collect all blocks in between EntryBB and ExitBB in both the given
889  /// vector and set.
891  SmallVectorImpl<BasicBlock *> &BlockVector);
892 
893  /// Return the function that contains the region to be outlined.
894  Function *getFunction() const { return EntryBB->getParent(); }
895  };
896 
897  /// Collection of regions that need to be outlined during finalization.
899 
900  /// Collection of owned canonical loop objects that eventually need to be
901  /// free'd.
902  std::forward_list<CanonicalLoopInfo> LoopInfos;
903 
904  /// Add a new region that will be outlined later.
905  void addOutlineInfo(OutlineInfo &&OI) { OutlineInfos.emplace_back(OI); }
906 
907  /// An ordered map of auto-generated variables to their unique names.
908  /// It stores variables with the following names: 1) ".gomp_critical_user_" +
909  /// <critical_section_name> + ".var" for "omp critical" directives; 2)
910  /// <mangled_name_for_global_var> + ".cache." for cache for threadprivate
911  /// variables.
913 
914  /// Create the global variable holding the offload mappings information.
916  std::string VarName);
917 
918  /// Create the global variable holding the offload names information.
921  std::string VarName);
922 
923  struct MapperAllocas {
924  AllocaInst *ArgsBase = nullptr;
925  AllocaInst *Args = nullptr;
926  AllocaInst *ArgSizes = nullptr;
927  };
928 
929  /// Create the allocas instruction used in call to mapper functions.
931  InsertPointTy AllocaIP, unsigned NumOperands,
932  struct MapperAllocas &MapperAllocas);
933 
934  /// Create the call for the target mapper function.
935  /// \param Loc The source location description.
936  /// \param MapperFunc Function to be called.
937  /// \param SrcLocInfo Source location information global.
938  /// \param MaptypesArg The argument types.
939  /// \param MapnamesArg The argument names.
940  /// \param MapperAllocas The AllocaInst used for the call.
941  /// \param DeviceID Device ID for the call.
942  /// \param NumOperands Number of operands in the call.
943  void emitMapperCall(const LocationDescription &Loc, Function *MapperFunc,
944  Value *SrcLocInfo, Value *MaptypesArg, Value *MapnamesArg,
945  struct MapperAllocas &MapperAllocas, int64_t DeviceID,
946  unsigned NumOperands);
947 
948 public:
949  /// Generator for __kmpc_copyprivate
950  ///
951  /// \param Loc The source location description.
952  /// \param BufSize Number of elements in the buffer.
953  /// \param CpyBuf List of pointers to data to be copied.
954  /// \param CpyFn function to call for copying data.
955  /// \param DidIt flag variable; 1 for 'single' thread, 0 otherwise.
956  ///
957  /// \return The insertion position *after* the CopyPrivate call.
958 
960  llvm::Value *BufSize, llvm::Value *CpyBuf,
961  llvm::Value *CpyFn, llvm::Value *DidIt);
962 
963  /// Generator for '#omp single'
964  ///
965  /// \param Loc The source location description.
966  /// \param BodyGenCB Callback that will generate the region code.
967  /// \param FiniCB Callback to finalize variable copies.
968  /// \param IsNowait If false, a barrier is emitted.
969  /// \param DidIt Local variable used as a flag to indicate 'single' thread
970  ///
971  /// \returns The insertion position *after* the single call.
973  BodyGenCallbackTy BodyGenCB,
974  FinalizeCallbackTy FiniCB, bool IsNowait,
975  llvm::Value *DidIt);
976 
977  /// Generator for '#omp master'
978  ///
979  /// \param Loc The insert and source location description.
980  /// \param BodyGenCB Callback that will generate the region code.
981  /// \param FiniCB Callback to finalize variable copies.
982  ///
983  /// \returns The insertion position *after* the master.
985  BodyGenCallbackTy BodyGenCB,
986  FinalizeCallbackTy FiniCB);
987 
988  /// Generator for '#omp masked'
989  ///
990  /// \param Loc The insert and source location description.
991  /// \param BodyGenCB Callback that will generate the region code.
992  /// \param FiniCB Callback to finialize variable copies.
993  ///
994  /// \returns The insertion position *after* the masked.
996  BodyGenCallbackTy BodyGenCB,
997  FinalizeCallbackTy FiniCB, Value *Filter);
998 
999  /// Generator for '#omp critical'
1000  ///
1001  /// \param Loc The insert and source location description.
1002  /// \param BodyGenCB Callback that will generate the region body code.
1003  /// \param FiniCB Callback to finalize variable copies.
1004  /// \param CriticalName name of the lock used by the critical directive
1005  /// \param HintInst Hint Instruction for hint clause associated with critical
1006  ///
1007  /// \returns The insertion position *after* the critical.
1009  BodyGenCallbackTy BodyGenCB,
1010  FinalizeCallbackTy FiniCB,
1011  StringRef CriticalName, Value *HintInst);
1012 
1013  /// Generator for '#omp ordered depend (source | sink)'
1014  ///
1015  /// \param Loc The insert and source location description.
1016  /// \param AllocaIP The insertion point to be used for alloca instructions.
1017  /// \param NumLoops The number of loops in depend clause.
1018  /// \param StoreValues The value will be stored in vector address.
1019  /// \param Name The name of alloca instruction.
1020  /// \param IsDependSource If true, depend source; otherwise, depend sink.
1021  ///
1022  /// \return The insertion position *after* the ordered.
1024  InsertPointTy AllocaIP, unsigned NumLoops,
1025  ArrayRef<llvm::Value *> StoreValues,
1026  const Twine &Name, bool IsDependSource);
1027 
1028  /// Generator for '#omp ordered [threads | simd]'
1029  ///
1030  /// \param Loc The insert and source location description.
1031  /// \param BodyGenCB Callback that will generate the region code.
1032  /// \param FiniCB Callback to finalize variable copies.
1033  /// \param IsThreads If true, with threads clause or without clause;
1034  /// otherwise, with simd clause;
1035  ///
1036  /// \returns The insertion position *after* the ordered.
1038  BodyGenCallbackTy BodyGenCB,
1039  FinalizeCallbackTy FiniCB,
1040  bool IsThreads);
1041 
1042  /// Generator for '#omp sections'
1043  ///
1044  /// \param Loc The insert and source location description.
1045  /// \param AllocaIP The insertion points to be used for alloca instructions.
1046  /// \param SectionCBs Callbacks that will generate body of each section.
1047  /// \param PrivCB Callback to copy a given variable (think copy constructor).
1048  /// \param FiniCB Callback to finalize variable copies.
1049  /// \param IsCancellable Flag to indicate a cancellable parallel region.
1050  /// \param IsNowait If true, barrier - to ensure all sections are executed
1051  /// before moving forward will not be generated.
1052  /// \returns The insertion position *after* the sections.
1054  InsertPointTy AllocaIP,
1056  PrivatizeCallbackTy PrivCB,
1057  FinalizeCallbackTy FiniCB, bool IsCancellable,
1058  bool IsNowait);
1059 
1060  /// Generator for '#omp section'
1061  ///
1062  /// \param Loc The insert and source location description.
1063  /// \param BodyGenCB Callback that will generate the region body code.
1064  /// \param FiniCB Callback to finalize variable copies.
1065  /// \returns The insertion position *after* the section.
1067  BodyGenCallbackTy BodyGenCB,
1068  FinalizeCallbackTy FiniCB);
1069 
1070  /// Generate conditional branch and relevant BasicBlocks through which private
1071  /// threads copy the 'copyin' variables from Master copy to threadprivate
1072  /// copies.
1073  ///
1074  /// \param IP insertion block for copyin conditional
1075  /// \param MasterVarPtr a pointer to the master variable
1076  /// \param PrivateVarPtr a pointer to the threadprivate variable
1077  /// \param IntPtrTy Pointer size type
1078  /// \param BranchtoEnd Create a branch between the copyin.not.master blocks
1079  // and copy.in.end block
1080  ///
1081  /// \returns The insertion point where copying operation to be emitted.
1083  Value *PrivateAddr,
1084  llvm::IntegerType *IntPtrTy,
1085  bool BranchtoEnd = true);
1086 
1087  /// Create a runtime call for kmpc_Alloc
1088  ///
1089  /// \param Loc The insert and source location description.
1090  /// \param Size Size of allocated memory space
1091  /// \param Allocator Allocator information instruction
1092  /// \param Name Name of call Instruction for OMP_alloc
1093  ///
1094  /// \returns CallInst to the OMP_Alloc call
1095  CallInst *createOMPAlloc(const LocationDescription &Loc, Value *Size,
1096  Value *Allocator, std::string Name = "");
1097 
1098  /// Create a runtime call for kmpc_free
1099  ///
1100  /// \param Loc The insert and source location description.
1101  /// \param Addr Address of memory space to be freed
1102  /// \param Allocator Allocator information instruction
1103  /// \param Name Name of call Instruction for OMP_Free
1104  ///
1105  /// \returns CallInst to the OMP_Free call
1107  Value *Allocator, std::string Name = "");
1108 
1109  /// Create a runtime call for kmpc_threadprivate_cached
1110  ///
1111  /// \param Loc The insert and source location description.
1112  /// \param Pointer pointer to data to be cached
1113  /// \param Size size of data to be cached
1114  /// \param Name Name of call Instruction for callinst
1115  ///
1116  /// \returns CallInst to the thread private cache call.
1118  llvm::Value *Pointer,
1119  llvm::ConstantInt *Size,
1120  const llvm::Twine &Name = Twine(""));
1121 
1122  /// Create a runtime call for __tgt_interop_init
1123  ///
1124  /// \param Loc The insert and source location description.
1125  /// \param InteropVar variable to be allocated
1126  /// \param InteropType type of interop operation
1127  /// \param Device devide to which offloading will occur
1128  /// \param NumDependences number of dependence variables
1129  /// \param DependenceAddress pointer to dependence variables
1130  /// \param HaveNowaitClause does nowait clause exist
1131  ///
1132  /// \returns CallInst to the __tgt_interop_init call
1134  Value *InteropVar,
1135  omp::OMPInteropType InteropType, Value *Device,
1136  Value *NumDependences,
1137  Value *DependenceAddress,
1138  bool HaveNowaitClause);
1139 
1140  /// Create a runtime call for __tgt_interop_destroy
1141  ///
1142  /// \param Loc The insert and source location description.
1143  /// \param InteropVar variable to be allocated
1144  /// \param Device devide to which offloading will occur
1145  /// \param NumDependences number of dependence variables
1146  /// \param DependenceAddress pointer to dependence variables
1147  /// \param HaveNowaitClause does nowait clause exist
1148  ///
1149  /// \returns CallInst to the __tgt_interop_destroy call
1151  Value *InteropVar, Value *Device,
1152  Value *NumDependences,
1153  Value *DependenceAddress,
1154  bool HaveNowaitClause);
1155 
1156  /// Create a runtime call for __tgt_interop_use
1157  ///
1158  /// \param Loc The insert and source location description.
1159  /// \param InteropVar variable to be allocated
1160  /// \param Device devide to which offloading will occur
1161  /// \param NumDependences number of dependence variables
1162  /// \param DependenceAddress pointer to dependence variables
1163  /// \param HaveNowaitClause does nowait clause exist
1164  ///
1165  /// \returns CallInst to the __tgt_interop_use call
1167  Value *InteropVar, Value *Device,
1168  Value *NumDependences, Value *DependenceAddress,
1169  bool HaveNowaitClause);
1170 
1171  /// The `omp target` interface
1172  ///
1173  /// For more information about the usage of this interface,
1174  /// \see openmp/libomptarget/deviceRTLs/common/include/target.h
1175  ///
1176  ///{
1177 
1178  /// Create a runtime call for kmpc_target_init
1179  ///
1180  /// \param Loc The insert and source location description.
1181  /// \param IsSPMD Flag to indicate if the kernel is an SPMD kernel or not.
1182  /// \param RequiresFullRuntime Indicate if a full device runtime is necessary.
1183  InsertPointTy createTargetInit(const LocationDescription &Loc, bool IsSPMD,
1184  bool RequiresFullRuntime);
1185 
1186  /// Create a runtime call for kmpc_target_deinit
1187  ///
1188  /// \param Loc The insert and source location description.
1189  /// \param IsSPMD Flag to indicate if the kernel is an SPMD kernel or not.
1190  /// \param RequiresFullRuntime Indicate if a full device runtime is necessary.
1191  void createTargetDeinit(const LocationDescription &Loc, bool IsSPMD,
1192  bool RequiresFullRuntime);
1193 
1194  ///}
1195 
1196  /// Declarations for LLVM-IR types (simple, array, function and structure) are
1197  /// generated below. Their names are defined and used in OpenMPKinds.def. Here
1198  /// we provide the declarations, the initializeTypes function will provide the
1199  /// values.
1200  ///
1201  ///{
1202 #define OMP_TYPE(VarName, InitValue) Type *VarName = nullptr;
1203 #define OMP_ARRAY_TYPE(VarName, ElemTy, ArraySize) \
1204  ArrayType *VarName##Ty = nullptr; \
1205  PointerType *VarName##PtrTy = nullptr;
1206 #define OMP_FUNCTION_TYPE(VarName, IsVarArg, ReturnType, ...) \
1207  FunctionType *VarName = nullptr; \
1208  PointerType *VarName##Ptr = nullptr;
1209 #define OMP_STRUCT_TYPE(VarName, StrName, ...) \
1210  StructType *VarName = nullptr; \
1211  PointerType *VarName##Ptr = nullptr;
1212 #include "llvm/Frontend/OpenMP/OMPKinds.def"
1213 
1214  ///}
1215 
1216 private:
1217  /// Create all simple and struct types exposed by the runtime and remember
1218  /// the llvm::PointerTypes of them for easy access later.
1219  void initializeTypes(Module &M);
1220 
1221  /// Common interface for generating entry calls for OMP Directives.
1222  /// if the directive has a region/body, It will set the insertion
1223  /// point to the body
1224  ///
1225  /// \param OMPD Directive to generate entry blocks for
1226  /// \param EntryCall Call to the entry OMP Runtime Function
1227  /// \param ExitBB block where the region ends.
1228  /// \param Conditional indicate if the entry call result will be used
1229  /// to evaluate a conditional of whether a thread will execute
1230  /// body code or not.
1231  ///
1232  /// \return The insertion position in exit block
1233  InsertPointTy emitCommonDirectiveEntry(omp::Directive OMPD, Value *EntryCall,
1234  BasicBlock *ExitBB,
1235  bool Conditional = false);
1236 
1237  /// Common interface to finalize the region
1238  ///
1239  /// \param OMPD Directive to generate exiting code for
1240  /// \param FinIP Insertion point for emitting Finalization code and exit call
1241  /// \param ExitCall Call to the ending OMP Runtime Function
1242  /// \param HasFinalize indicate if the directive will require finalization
1243  /// and has a finalization callback in the stack that
1244  /// should be called.
1245  ///
1246  /// \return The insertion position in exit block
1247  InsertPointTy emitCommonDirectiveExit(omp::Directive OMPD,
1248  InsertPointTy FinIP,
1249  Instruction *ExitCall,
1250  bool HasFinalize = true);
1251 
1252  /// Common Interface to generate OMP inlined regions
1253  ///
1254  /// \param OMPD Directive to generate inlined region for
1255  /// \param EntryCall Call to the entry OMP Runtime Function
1256  /// \param ExitCall Call to the ending OMP Runtime Function
1257  /// \param BodyGenCB Body code generation callback.
1258  /// \param FiniCB Finalization Callback. Will be called when finalizing region
1259  /// \param Conditional indicate if the entry call result will be used
1260  /// to evaluate a conditional of whether a thread will execute
1261  /// body code or not.
1262  /// \param HasFinalize indicate if the directive will require finalization
1263  /// and has a finalization callback in the stack that
1264  /// should be called.
1265  /// \param IsCancellable if HasFinalize is set to true, indicate if the
1266  /// the directive should be cancellable.
1267  /// \return The insertion point after the region
1268 
1270  EmitOMPInlinedRegion(omp::Directive OMPD, Instruction *EntryCall,
1271  Instruction *ExitCall, BodyGenCallbackTy BodyGenCB,
1272  FinalizeCallbackTy FiniCB, bool Conditional = false,
1273  bool HasFinalize = true, bool IsCancellable = false);
1274 
1275  /// Get the platform-specific name separator.
1276  /// \param Parts different parts of the final name that needs separation
1277  /// \param FirstSeparator First separator used between the initial two
1278  /// parts of the name.
1279  /// \param Separator separator used between all of the rest consecutive
1280  /// parts of the name
1281  static std::string getNameWithSeparators(ArrayRef<StringRef> Parts,
1282  StringRef FirstSeparator,
1283  StringRef Separator);
1284 
1285  /// Gets (if variable with the given name already exist) or creates
1286  /// internal global variable with the specified Name. The created variable has
1287  /// linkage CommonLinkage by default and is initialized by null value.
1288  /// \param Ty Type of the global variable. If it is exist already the type
1289  /// must be the same.
1290  /// \param Name Name of the variable.
1291  Constant *getOrCreateOMPInternalVariable(Type *Ty, const Twine &Name,
1292  unsigned AddressSpace = 0);
1293 
1294  /// Returns corresponding lock object for the specified critical region
1295  /// name. If the lock object does not exist it is created, otherwise the
1296  /// reference to the existing copy is returned.
1297  /// \param CriticalName Name of the critical region.
1298  ///
1299  Value *getOMPCriticalRegionLock(StringRef CriticalName);
1300 
1301  /// Callback type for Atomic Expression update
1302  /// ex:
1303  /// \code{.cpp}
1304  /// unsigned x = 0;
1305  /// #pragma omp atomic update
1306  /// x = Expr(x_old); //Expr() is any legal operation
1307  /// \endcode
1308  ///
1309  /// \param XOld the value of the atomic memory address to use for update
1310  /// \param IRB reference to the IRBuilder to use
1311  ///
1312  /// \returns Value to update X to.
1313  using AtomicUpdateCallbackTy =
1314  const function_ref<Value *(Value *XOld, IRBuilder<> &IRB)>;
1315 
1316 private:
1317  enum AtomicKind { Read, Write, Update, Capture, Compare };
1318 
1319  /// Determine whether to emit flush or not
1320  ///
1321  /// \param Loc The insert and source location description.
1322  /// \param AO The required atomic ordering
1323  /// \param AK The OpenMP atomic operation kind used.
1324  ///
1325  /// \returns wether a flush was emitted or not
1326  bool checkAndEmitFlushAfterAtomic(const LocationDescription &Loc,
1327  AtomicOrdering AO, AtomicKind AK);
1328 
1329  /// Emit atomic update for constructs: X = X BinOp Expr ,or X = Expr BinOp X
1330  /// For complex Operations: X = UpdateOp(X) => CmpExch X, old_X, UpdateOp(X)
1331  /// Only Scalar data types.
1332  ///
1333  /// \param AllocaIP The insertion point to be used for alloca
1334  /// instructions.
1335  /// \param X The target atomic pointer to be updated
1336  /// \param XElemTy The element type of the atomic pointer.
1337  /// \param Expr The value to update X with.
1338  /// \param AO Atomic ordering of the generated atomic
1339  /// instructions.
1340  /// \param RMWOp The binary operation used for update. If
1341  /// operation is not supported by atomicRMW,
1342  /// or belong to {FADD, FSUB, BAD_BINOP}.
1343  /// Then a `cmpExch` based atomic will be generated.
1344  /// \param UpdateOp Code generator for complex expressions that cannot be
1345  /// expressed through atomicrmw instruction.
1346  /// \param VolatileX true if \a X volatile?
1347  /// \param IsXBinopExpr true if \a X is Left H.S. in Right H.S. part of the
1348  /// update expression, false otherwise.
1349  /// (e.g. true for X = X BinOp Expr)
1350  ///
1351  /// \returns A pair of the old value of X before the update, and the value
1352  /// used for the update.
1353  std::pair<Value *, Value *>
1354  emitAtomicUpdate(InsertPointTy AllocaIP, Value *X, Type *XElemTy, Value *Expr,
1356  AtomicUpdateCallbackTy &UpdateOp, bool VolatileX,
1357  bool IsXBinopExpr);
1358 
1359  /// Emit the binary op. described by \p RMWOp, using \p Src1 and \p Src2 .
1360  ///
1361  /// \Return The instruction
1362  Value *emitRMWOpAsInstruction(Value *Src1, Value *Src2,
1363  AtomicRMWInst::BinOp RMWOp);
1364 
1365 public:
1366  /// a struct to pack relevant information while generating atomic Ops
1367  struct AtomicOpValue {
1368  Value *Var = nullptr;
1369  Type *ElemTy = nullptr;
1370  bool IsSigned = false;
1371  bool IsVolatile = false;
1372  };
1373 
1374  /// Emit atomic Read for : V = X --- Only Scalar data types.
1375  ///
1376  /// \param Loc The insert and source location description.
1377  /// \param X The target pointer to be atomically read
1378  /// \param V Memory address where to store atomically read
1379  /// value
1380  /// \param AO Atomic ordering of the generated atomic
1381  /// instructions.
1382  ///
1383  /// \return Insertion point after generated atomic read IR.
1386  AtomicOrdering AO);
1387 
1388  /// Emit atomic write for : X = Expr --- Only Scalar data types.
1389  ///
1390  /// \param Loc The insert and source location description.
1391  /// \param X The target pointer to be atomically written to
1392  /// \param Expr The value to store.
1393  /// \param AO Atomic ordering of the generated atomic
1394  /// instructions.
1395  ///
1396  /// \return Insertion point after generated atomic Write IR.
1398  AtomicOpValue &X, Value *Expr,
1399  AtomicOrdering AO);
1400 
1401  /// Emit atomic update for constructs: X = X BinOp Expr ,or X = Expr BinOp X
1402  /// For complex Operations: X = UpdateOp(X) => CmpExch X, old_X, UpdateOp(X)
1403  /// Only Scalar data types.
1404  ///
1405  /// \param Loc The insert and source location description.
1406  /// \param AllocaIP The insertion point to be used for alloca instructions.
1407  /// \param X The target atomic pointer to be updated
1408  /// \param Expr The value to update X with.
1409  /// \param AO Atomic ordering of the generated atomic instructions.
1410  /// \param RMWOp The binary operation used for update. If operation
1411  /// is not supported by atomicRMW, or belong to
1412  /// {FADD, FSUB, BAD_BINOP}. Then a `cmpExch` based
1413  /// atomic will be generated.
1414  /// \param UpdateOp Code generator for complex expressions that cannot be
1415  /// expressed through atomicrmw instruction.
1416  /// \param IsXBinopExpr true if \a X is Left H.S. in Right H.S. part of the
1417  /// update expression, false otherwise.
1418  /// (e.g. true for X = X BinOp Expr)
1419  ///
1420  /// \return Insertion point after generated atomic update IR.
1422  InsertPointTy AllocaIP, AtomicOpValue &X,
1423  Value *Expr, AtomicOrdering AO,
1424  AtomicRMWInst::BinOp RMWOp,
1425  AtomicUpdateCallbackTy &UpdateOp,
1426  bool IsXBinopExpr);
1427 
1428  /// Emit atomic update for constructs: --- Only Scalar data types
1429  /// V = X; X = X BinOp Expr ,
1430  /// X = X BinOp Expr; V = X,
1431  /// V = X; X = Expr BinOp X,
1432  /// X = Expr BinOp X; V = X,
1433  /// V = X; X = UpdateOp(X),
1434  /// X = UpdateOp(X); V = X,
1435  ///
1436  /// \param Loc The insert and source location description.
1437  /// \param AllocaIP The insertion point to be used for alloca instructions.
1438  /// \param X The target atomic pointer to be updated
1439  /// \param V Memory address where to store captured value
1440  /// \param Expr The value to update X with.
1441  /// \param AO Atomic ordering of the generated atomic instructions
1442  /// \param RMWOp The binary operation used for update. If
1443  /// operation is not supported by atomicRMW, or belong to
1444  /// {FADD, FSUB, BAD_BINOP}. Then a cmpExch based
1445  /// atomic will be generated.
1446  /// \param UpdateOp Code generator for complex expressions that cannot be
1447  /// expressed through atomicrmw instruction.
1448  /// \param UpdateExpr true if X is an in place update of the form
1449  /// X = X BinOp Expr or X = Expr BinOp X
1450  /// \param IsXBinopExpr true if X is Left H.S. in Right H.S. part of the
1451  /// update expression, false otherwise.
1452  /// (e.g. true for X = X BinOp Expr)
1453  /// \param IsPostfixUpdate true if original value of 'x' must be stored in
1454  /// 'v', not an updated one.
1455  ///
1456  /// \return Insertion point after generated atomic capture IR.
1459  AtomicOpValue &X, AtomicOpValue &V, Value *Expr,
1461  AtomicUpdateCallbackTy &UpdateOp, bool UpdateExpr,
1462  bool IsPostfixUpdate, bool IsXBinopExpr);
1463 
1464  /// Emit atomic compare for constructs: --- Only scalar data types
1465  /// cond-update-atomic:
1466  /// x = x ordop expr ? expr : x;
1467  /// x = expr ordop x ? expr : x;
1468  /// x = x == e ? d : x;
1469  /// x = e == x ? d : x; (this one is not in the spec)
1470  /// cond-update-stmt:
1471  /// if (x ordop expr) { x = expr; }
1472  /// if (expr ordop x) { x = expr; }
1473  /// if (x == e) { x = d; }
1474  /// if (e == x) { x = d; } (this one is not in the spec)
1475  ///
1476  /// \param Loc The insert and source location description.
1477  /// \param X The target atomic pointer to be updated.
1478  /// \param E The expected value ('e') for forms that use an
1479  /// equality comparison or an expression ('expr') for
1480  /// forms that use 'ordop' (logically an atomic maximum or
1481  /// minimum).
1482  /// \param D The desired value for forms that use an equality
1483  /// comparison. If forms that use 'ordop', it should be
1484  /// \p nullptr.
1485  /// \param AO Atomic ordering of the generated atomic instructions.
1486  /// \param Op Atomic compare operation. It can only be ==, <, or >.
1487  /// \param IsXBinopExpr True if the conditional statement is in the form where
1488  /// x is on LHS. It only matters for < or >.
1489  ///
1490  /// \return Insertion point after generated atomic capture IR.
1492  AtomicOpValue &X, Value *E, Value *D,
1493  AtomicOrdering AO,
1495  bool IsXBinopExpr);
1496 
1497  /// Create the control flow structure of a canonical OpenMP loop.
1498  ///
1499  /// The emitted loop will be disconnected, i.e. no edge to the loop's
1500  /// preheader and no terminator in the AfterBB. The OpenMPIRBuilder's
1501  /// IRBuilder location is not preserved.
1502  ///
1503  /// \param DL DebugLoc used for the instructions in the skeleton.
1504  /// \param TripCount Value to be used for the trip count.
1505  /// \param F Function in which to insert the BasicBlocks.
1506  /// \param PreInsertBefore Where to insert BBs that execute before the body,
1507  /// typically the body itself.
1508  /// \param PostInsertBefore Where to insert BBs that execute after the body.
1509  /// \param Name Base name used to derive BB
1510  /// and instruction names.
1511  ///
1512  /// \returns The CanonicalLoopInfo that represents the emitted loop.
1514  Function *F,
1515  BasicBlock *PreInsertBefore,
1516  BasicBlock *PostInsertBefore,
1517  const Twine &Name = {});
1518 };
1519 
1520 /// Class to represented the control flow structure of an OpenMP canonical loop.
1521 ///
1522 /// The control-flow structure is standardized for easy consumption by
1523 /// directives associated with loops. For instance, the worksharing-loop
1524 /// construct may change this control flow such that each loop iteration is
1525 /// executed on only one thread. The constraints of a canonical loop in brief
1526 /// are:
1527 ///
1528 /// * The number of loop iterations must have been computed before entering the
1529 /// loop.
1530 ///
1531 /// * Has an (unsigned) logical induction variable that starts at zero and
1532 /// increments by one.
1533 ///
1534 /// * The loop's CFG itself has no side-effects. The OpenMP specification
1535 /// itself allows side-effects, but the order in which they happen, including
1536 /// how often or whether at all, is unspecified. We expect that the frontend
1537 /// will emit those side-effect instructions somewhere (e.g. before the loop)
1538 /// such that the CanonicalLoopInfo itself can be side-effect free.
1539 ///
1540 /// Keep in mind that CanonicalLoopInfo is meant to only describe a repeated
1541 /// execution of a loop body that satifies these constraints. It does NOT
1542 /// represent arbitrary SESE regions that happen to contain a loop. Do not use
1543 /// CanonicalLoopInfo for such purposes.
1544 ///
1545 /// The control flow can be described as follows:
1546 ///
1547 /// Preheader
1548 /// |
1549 /// /-> Header
1550 /// | |
1551 /// | Cond---\
1552 /// | | |
1553 /// | Body |
1554 /// | | | |
1555 /// | <...> |
1556 /// | | | |
1557 /// \--Latch |
1558 /// |
1559 /// Exit
1560 /// |
1561 /// After
1562 ///
1563 /// The loop is thought to start at PreheaderIP (at the Preheader's terminator,
1564 /// including) and end at AfterIP (at the After's first instruction, excluding).
1565 /// That is, instructions in the Preheader and After blocks (except the
1566 /// Preheader's terminator) are out of CanonicalLoopInfo's control and may have
1567 /// side-effects. Typically, the Preheader is used to compute the loop's trip
1568 /// count. The instructions from BodyIP (at the Body block's first instruction,
1569 /// excluding) until the Latch are also considered outside CanonicalLoopInfo's
1570 /// control and thus can have side-effects. The body block is the single entry
1571 /// point into the loop body, which may contain arbitrary control flow as long
1572 /// as all control paths eventually branch to the Latch block.
1573 ///
1574 /// TODO: Consider adding another standardized BasicBlock between Body CFG and
1575 /// Latch to guarantee that there is only a single edge to the latch. It would
1576 /// make loop transformations easier to not needing to consider multiple
1577 /// predecessors of the latch (See redirectAllPredecessorsTo) and would give us
1578 /// an equivalant to PreheaderIP, AfterIP and BodyIP for inserting code that
1579 /// executes after each body iteration.
1580 ///
1581 /// There must be no loop-carried dependencies through llvm::Values. This is
1582 /// equivalant to that the Latch has no PHINode and the Header's only PHINode is
1583 /// for the induction variable.
1584 ///
1585 /// All code in Header, Cond, Latch and Exit (plus the terminator of the
1586 /// Preheader) are CanonicalLoopInfo's responsibility and their build-up checked
1587 /// by assertOK(). They are expected to not be modified unless explicitly
1588 /// modifying the CanonicalLoopInfo through a methods that applies a OpenMP
1589 /// loop-associated construct such as applyWorkshareLoop, tileLoops, unrollLoop,
1590 /// etc. These methods usually invalidate the CanonicalLoopInfo and re-use its
1591 /// basic blocks. After invalidation, the CanonicalLoopInfo must not be used
1592 /// anymore as its underlying control flow may not exist anymore.
1593 /// Loop-transformation methods such as tileLoops, collapseLoops and unrollLoop
1594 /// may also return a new CanonicalLoopInfo that can be passed to other
1595 /// loop-associated construct implementing methods. These loop-transforming
1596 /// methods may either create a new CanonicalLoopInfo usually using
1597 /// createLoopSkeleton and invalidate the input CanonicalLoopInfo, or reuse and
1598 /// modify one of the input CanonicalLoopInfo and return it as representing the
1599 /// modified loop. What is done is an implementation detail of
1600 /// transformation-implementing method and callers should always assume that the
1601 /// CanonicalLoopInfo passed to it is invalidated and a new object is returned.
1602 /// Returned CanonicalLoopInfo have the same structure and guarantees as the one
1603 /// created by createCanonicalLoop, such that transforming methods do not have
1604 /// to special case where the CanonicalLoopInfo originated from.
1605 ///
1606 /// Generally, methods consuming CanonicalLoopInfo do not need an
1607 /// OpenMPIRBuilder::InsertPointTy as argument, but use the locations of the
1608 /// CanonicalLoopInfo to insert new or modify existing instructions. Unless
1609 /// documented otherwise, methods consuming CanonicalLoopInfo do not invalidate
1610 /// any InsertPoint that is outside CanonicalLoopInfo's control. Specifically,
1611 /// any InsertPoint in the Preheader, After or Block can still be used after
1612 /// calling such a method.
1613 ///
1614 /// TODO: Provide mechanisms for exception handling and cancellation points.
1615 ///
1616 /// Defined outside OpenMPIRBuilder because nested classes cannot be
1617 /// forward-declared, e.g. to avoid having to include the entire OMPIRBuilder.h.
1619  friend class OpenMPIRBuilder;
1620 
1621 private:
1622  BasicBlock *Header = nullptr;
1623  BasicBlock *Cond = nullptr;
1624  BasicBlock *Latch = nullptr;
1625  BasicBlock *Exit = nullptr;
1626 
1627  /// Add the control blocks of this loop to \p BBs.
1628  ///
1629  /// This does not include any block from the body, including the one returned
1630  /// by getBody().
1631  ///
1632  /// FIXME: This currently includes the Preheader and After blocks even though
1633  /// their content is (mostly) not under CanonicalLoopInfo's control.
1634  /// Re-evaluated whether this makes sense.
1635  void collectControlBlocks(SmallVectorImpl<BasicBlock *> &BBs);
1636 
1637  /// Sets the number of loop iterations to the given value. This value must be
1638  /// valid in the condition block (i.e., defined in the preheader) and is
1639  /// interpreted as an unsigned integer.
1640  void setTripCount(Value *TripCount);
1641 
1642  /// Replace all uses of the canonical induction variable in the loop body with
1643  /// a new one.
1644  ///
1645  /// The intended use case is to update the induction variable for an updated
1646  /// iteration space such that it can stay normalized in the 0...tripcount-1
1647  /// range.
1648  ///
1649  /// The \p Updater is called with the (presumable updated) current normalized
1650  /// induction variable and is expected to return the value that uses of the
1651  /// pre-updated induction values should use instead, typically dependent on
1652  /// the new induction variable. This is a lambda (instead of e.g. just passing
1653  /// the new value) to be able to distinguish the uses of the pre-updated
1654  /// induction variable and uses of the induction varible to compute the
1655  /// updated induction variable value.
1656  void mapIndVar(llvm::function_ref<Value *(Instruction *)> Updater);
1657 
1658 public:
1659  /// Returns whether this object currently represents the IR of a loop. If
1660  /// returning false, it may have been consumed by a loop transformation or not
1661  /// been intialized. Do not use in this case;
1662  bool isValid() const { return Header; }
1663 
1664  /// The preheader ensures that there is only a single edge entering the loop.
1665  /// Code that must be execute before any loop iteration can be emitted here,
1666  /// such as computing the loop trip count and begin lifetime markers. Code in
1667  /// the preheader is not considered part of the canonical loop.
1668  BasicBlock *getPreheader() const;
1669 
1670  /// The header is the entry for each iteration. In the canonical control flow,
1671  /// it only contains the PHINode for the induction variable.
1673  assert(isValid() && "Requires a valid canonical loop");
1674  return Header;
1675  }
1676 
1677  /// The condition block computes whether there is another loop iteration. If
1678  /// yes, branches to the body; otherwise to the exit block.
1679  BasicBlock *getCond() const {
1680  assert(isValid() && "Requires a valid canonical loop");
1681  return Cond;
1682  }
1683 
1684  /// The body block is the single entry for a loop iteration and not controlled
1685  /// by CanonicalLoopInfo. It can contain arbitrary control flow but must
1686  /// eventually branch to the \p Latch block.
1687  BasicBlock *getBody() const {
1688  assert(isValid() && "Requires a valid canonical loop");
1689  return cast<BranchInst>(Cond->getTerminator())->getSuccessor(0);
1690  }
1691 
1692  /// Reaching the latch indicates the end of the loop body code. In the
1693  /// canonical control flow, it only contains the increment of the induction
1694  /// variable.
1696  assert(isValid() && "Requires a valid canonical loop");
1697  return Latch;
1698  }
1699 
1700  /// Reaching the exit indicates no more iterations are being executed.
1701  BasicBlock *getExit() const {
1702  assert(isValid() && "Requires a valid canonical loop");
1703  return Exit;
1704  }
1705 
1706  /// The after block is intended for clean-up code such as lifetime end
1707  /// markers. It is separate from the exit block to ensure, analogous to the
1708  /// preheader, it having just a single entry edge and being free from PHI
1709  /// nodes should there be multiple loop exits (such as from break
1710  /// statements/cancellations).
1712  assert(isValid() && "Requires a valid canonical loop");
1713  return Exit->getSingleSuccessor();
1714  }
1715 
1716  /// Returns the llvm::Value containing the number of loop iterations. It must
1717  /// be valid in the preheader and always interpreted as an unsigned integer of
1718  /// any bit-width.
1719  Value *getTripCount() const {
1720  assert(isValid() && "Requires a valid canonical loop");
1721  Instruction *CmpI = &Cond->front();
1722  assert(isa<CmpInst>(CmpI) && "First inst must compare IV with TripCount");
1723  return CmpI->getOperand(1);
1724  }
1725 
1726  /// Returns the instruction representing the current logical induction
1727  /// variable. Always unsigned, always starting at 0 with an increment of one.
1729  assert(isValid() && "Requires a valid canonical loop");
1730  Instruction *IndVarPHI = &Header->front();
1731  assert(isa<PHINode>(IndVarPHI) && "First inst must be the IV PHI");
1732  return IndVarPHI;
1733  }
1734 
1735  /// Return the type of the induction variable (and the trip count).
1736  Type *getIndVarType() const {
1737  assert(isValid() && "Requires a valid canonical loop");
1738  return getIndVar()->getType();
1739  }
1740 
1741  /// Return the insertion point for user code before the loop.
1743  assert(isValid() && "Requires a valid canonical loop");
1744  BasicBlock *Preheader = getPreheader();
1745  return {Preheader, std::prev(Preheader->end())};
1746  };
1747 
1748  /// Return the insertion point for user code in the body.
1750  assert(isValid() && "Requires a valid canonical loop");
1751  BasicBlock *Body = getBody();
1752  return {Body, Body->begin()};
1753  };
1754 
1755  /// Return the insertion point for user code after the loop.
1757  assert(isValid() && "Requires a valid canonical loop");
1758  BasicBlock *After = getAfter();
1759  return {After, After->begin()};
1760  };
1761 
1763  assert(isValid() && "Requires a valid canonical loop");
1764  return Header->getParent();
1765  }
1766 
1767  /// Consistency self-check.
1768  void assertOK() const;
1769 
1770  /// Invalidate this loop. That is, the underlying IR does not fulfill the
1771  /// requirements of an OpenMP canonical loop anymore.
1772  void invalidate();
1773 };
1774 
1775 } // end namespace llvm
1776 
1777 #endif // LLVM_FRONTEND_OPENMP_OMPIRBUILDER_H
llvm::CanonicalLoopInfo::getPreheaderIP
OpenMPIRBuilder::InsertPointTy getPreheaderIP() const
Return the insertion point for user code before the loop.
Definition: OMPIRBuilder.h:1742
llvm::OpenMPIRBuilder::createCachedThreadPrivate
CallInst * createCachedThreadPrivate(const LocationDescription &Loc, llvm::Value *Pointer, llvm::ConstantInt *Size, const llvm::Twine &Name=Twine(""))
Create a runtime call for kmpc_threadprivate_cached.
Definition: OMPIRBuilder.cpp:3567
llvm::BasicBlock::end
iterator end()
Definition: BasicBlock.h:299
llvm::IRBuilderBase::restoreIP
void restoreIP(InsertPoint IP)
Sets the current insert point to a previously-saved location.
Definition: IRBuilder.h:266
llvm::OpenMPIRBuilder::LocationDescription
Description of a LLVM-IR insertion point (IP) and a debug/source location (filename,...
Definition: OMPIRBuilder.h:202
llvm::OpenMPIRBuilder::OutlineInfo::ExitBB
BasicBlock * ExitBB
Definition: OMPIRBuilder.h:885
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:17
llvm::OpenMPIRBuilder::createSection
InsertPointTy createSection(const LocationDescription &Loc, BodyGenCallbackTy BodyGenCB, FinalizeCallbackTy FiniCB)
Generator for '#omp section'.
Definition: OMPIRBuilder.cpp:1517
llvm::OpenMPIRBuilder::createCritical
InsertPointTy createCritical(const LocationDescription &Loc, BodyGenCallbackTy BodyGenCB, FinalizeCallbackTy FiniCB, StringRef CriticalName, Value *HintInst)
Generator for '#omp critical'.
Definition: OMPIRBuilder.cpp:3179
llvm::CanonicalLoopInfo::getAfter
BasicBlock * getAfter() const
The after block is intended for clean-up code such as lifetime end markers.
Definition: OMPIRBuilder.h:1711
llvm::OpenMPIRBuilder::OutlineInfo::ExcludeArgsFromAggregate
SmallVector< Value *, 2 > ExcludeArgsFromAggregate
Definition: OMPIRBuilder.h:886
llvm::BasicBlock::getParent
const Function * getParent() const
Return the enclosing method, or null if none.
Definition: BasicBlock.h:104
llvm::OpenMPIRBuilder::createLoopSkeleton
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.
Definition: OMPIRBuilder.cpp:1776
llvm::CanonicalLoopInfo::getPreheader
BasicBlock * getPreheader() const
The preheader ensures that there is only a single edge entering the loop.
Definition: OMPIRBuilder.cpp:4224
llvm::OpenMPIRBuilder::pushFinalizationCB
void pushFinalizationCB(const FinalizationInfo &FI)
Push a finalization callback on the finalization stack.
Definition: OMPIRBuilder.h:124
llvm::Function
Definition: Function.h:60
llvm::Loop
Represents a single loop in the control flow graph.
Definition: LoopInfo.h:530
llvm::OpenMPIRBuilder::createOMPInteropUse
CallInst * createOMPInteropUse(const LocationDescription &Loc, Value *InteropVar, Value *Device, Value *NumDependences, Value *DependenceAddress, bool HaveNowaitClause)
Create a runtime call for __tgt_interop_use.
Definition: OMPIRBuilder.cpp:3539
Loops
Hexagon Hardware Loops
Definition: HexagonHardwareLoops.cpp:372
llvm::AtomicRMWInst::BinOp
BinOp
This enumeration lists the possible modifications atomicrmw can make.
Definition: Instructions.h:739
llvm::SmallVector
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1185
llvm::OpenMPIRBuilder::tileLoops
std::vector< CanonicalLoopInfo * > tileLoops(DebugLoc DL, ArrayRef< CanonicalLoopInfo * > Loops, ArrayRef< Value * > TileSizes)
Tile a loop nest.
Definition: OMPIRBuilder.cpp:2584
llvm::OpenMPIRBuilder::InsertPointTy
IRBuilder<>::InsertPoint InsertPointTy
Type used throughout for insertion points.
Definition: OMPIRBuilder.h:96
llvm::IRBuilder<>
llvm::OpenMPIRBuilder::createAtomicCompare
InsertPointTy createAtomicCompare(const LocationDescription &Loc, AtomicOpValue &X, Value *E, Value *D, AtomicOrdering AO, omp::OMPAtomicCompareOp Op, bool IsXBinopExpr)
Emit atomic compare for constructs: — Only scalar data types cond-update-atomic: x = x ordop expr ?...
Definition: OMPIRBuilder.cpp:4110
llvm::GlobalVariable
Definition: GlobalVariable.h:39
llvm::OpenMPIRBuilder::getOrCreateThreadID
Value * getOrCreateThreadID(Value *Ident)
Return the current thread ID.
Definition: OMPIRBuilder.cpp:641
Allocator.h
llvm::OpenMPIRBuilder::createAtomicUpdate
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 = ...
Definition: OMPIRBuilder.cpp:3907
llvm::tgtok::VarName
@ VarName
Definition: TGLexer.h:71
llvm::OpenMPIRBuilder::FinalizationInfo::IsCancellable
bool IsCancellable
Flag to indicate if the directive is cancellable.
Definition: OMPIRBuilder.h:118
llvm::Type
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
llvm::OpenMPIRBuilder::StorableBodyGenCallbackTy
std::function< void(InsertPointTy AllocaIP, InsertPointTy CodeGenIP)> StorableBodyGenCallbackTy
Definition: OMPIRBuilder.h:165
llvm::OpenMPIRBuilder::AtomicOpValue
a struct to pack relevant information while generating atomic Ops
Definition: OMPIRBuilder.h:1367
llvm::OpenMPIRBuilder::FinalizationInfo::FiniCB
FinalizeCallbackTy FiniCB
The finalization callback provided by the last in-flight invocation of createXXXX for the directive o...
Definition: OMPIRBuilder.h:111
llvm::CanonicalLoopInfo::getAfterIP
OpenMPIRBuilder::InsertPointTy getAfterIP() const
Return the insertion point for user code after the loop.
Definition: OMPIRBuilder.h:1756
llvm::BasicBlock::getSingleSuccessor
const BasicBlock * getSingleSuccessor() const
Return the successor of this block if it has a single successor.
Definition: BasicBlock.cpp:291
llvm::spliceBB
void spliceBB(IRBuilderBase::InsertPoint IP, BasicBlock *New, bool CreateBranch)
Move the instruction after an InsertPoint to the beginning of another BasicBlock.
Definition: OMPIRBuilder.cpp:255
llvm::OpenMPIRBuilder::createSingle
InsertPointTy createSingle(const LocationDescription &Loc, BodyGenCallbackTy BodyGenCB, FinalizeCallbackTy FiniCB, bool IsNowait, llvm::Value *DidIt)
Generator for '#omp single'.
Definition: OMPIRBuilder.cpp:3137
llvm::OpenMPIRBuilder::ReductionInfo::Variable
Value * Variable
Reduction variable of pointer type.
Definition: OMPIRBuilder.h:660
llvm::OpenMPIRBuilder::FinalizeCallbackTy
std::function< void(InsertPointTy CodeGenIP)> FinalizeCallbackTy
Callback type for variable finalization (think destructors).
Definition: OMPIRBuilder.h:106
llvm::CanonicalLoopInfo::getFunction
Function * getFunction() const
Definition: OMPIRBuilder.h:1762
llvm::OpenMPIRBuilder::AtomicOpValue::Var
Value * Var
Definition: OMPIRBuilder.h:1368
llvm::OpenMPIRBuilder::emitTaskyieldImpl
void emitTaskyieldImpl(const LocationDescription &Loc)
Generate a taskyield runtime call.
Definition: OMPIRBuilder.cpp:1238
llvm::OpenMPIRBuilder::createReductions
InsertPointTy createReductions(const LocationDescription &Loc, InsertPointTy AllocaIP, ArrayRef< ReductionInfo > ReductionInfos, bool IsNoWait=false)
Generator for '#omp reduction'.
Definition: OMPIRBuilder.cpp:1563
llvm::OpenMPIRBuilder::collapseLoops
CanonicalLoopInfo * collapseLoops(DebugLoc DL, ArrayRef< CanonicalLoopInfo * > Loops, InsertPointTy ComputeIP)
Collapse a loop nest into a single loop.
Definition: OMPIRBuilder.cpp:2456
F
#define F(x, y, z)
Definition: MD5.cpp:55
llvm::CanonicalLoopInfo::getIndVar
Instruction * getIndVar() const
Returns the instruction representing the current logical induction variable.
Definition: OMPIRBuilder.h:1728
llvm::BasicBlock
LLVM Basic Block Representation.
Definition: BasicBlock.h:55
llvm::OpenMPIRBuilder::createOrderedDepend
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)'.
Definition: OMPIRBuilder.cpp:3214
Mappings
Inject TLI Mappings
Definition: InjectTLIMappings.cpp:171
llvm::ConstantInt
This is the shared class of boolean and integer constants.
Definition: Constants.h:79
llvm::omp::OMPInteropType
OMPInteropType
Definition: OMPConstants.h:205
llvm::OpenMPIRBuilder::popFinalizationCB
void popFinalizationCB()
Pop the last finalization callback from the finalization stack.
Definition: OMPIRBuilder.h:131
llvm::OpenMPIRBuilder::emitMapperCall
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.
Definition: OMPIRBuilder.cpp:3744
llvm::OpenMPIRBuilder::M
Module & M
The underlying LLVM-IR module.
Definition: OMPIRBuilder.h:869
E
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
llvm::CanonicalLoopInfo::assertOK
void assertOK() const
Consistency self-check.
Definition: OMPIRBuilder.cpp:4278
llvm::CanonicalLoopInfo::getCond
BasicBlock * getCond() const
The condition block computes whether there is another loop iteration.
Definition: OMPIRBuilder.h:1679
llvm::OpenMPIRBuilder
An interface to create LLVM-IR for OpenMP directives.
Definition: OMPIRBuilder.h:75
llvm::OpenMPIRBuilder::OutlineInfos
SmallVector< OutlineInfo, 16 > OutlineInfos
Collection of regions that need to be outlined during finalization.
Definition: OMPIRBuilder.h:898
llvm::OpenMPIRBuilder::createOMPFree
CallInst * createOMPFree(const LocationDescription &Loc, Value *Addr, Value *Allocator, std::string Name="")
Create a runtime call for kmpc_free.
Definition: OMPIRBuilder.cpp:3468
llvm::OpenMPIRBuilder::getOrCreateDefaultSrcLocStr
Constant * getOrCreateDefaultSrcLocStr(uint32_t &SrcLocStrSize)
Return the (LLVM-IR) string describing the default source location.
Definition: OMPIRBuilder.cpp:613
llvm::OpenMPIRBuilder::emitOffloadingEntry
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.
Definition: OMPIRBuilder.cpp:756
llvm::BasicBlock::begin
iterator begin()
Instruction iterator methods.
Definition: BasicBlock.h:297
llvm::OpenMPIRBuilder::createMapperAllocas
void createMapperAllocas(const LocationDescription &Loc, InsertPointTy AllocaIP, unsigned NumOperands, struct MapperAllocas &MapperAllocas)
Create the allocas instruction used in call to mapper functions.
Definition: OMPIRBuilder.cpp:3725
llvm::OpenMPIRBuilder::createBarrier
InsertPointTy createBarrier(const LocationDescription &Loc, omp::Directive DK, bool ForceSimpleCall=false, bool CheckCancelFlag=true)
Emitter methods for OpenMP directives.
Definition: OMPIRBuilder.cpp:648
llvm::OpenMPIRBuilder::createAtomicRead
InsertPointTy createAtomicRead(const LocationDescription &Loc, AtomicOpValue &X, AtomicOpValue &V, AtomicOrdering AO)
Emit atomic Read for : V = X — Only Scalar data types.
Definition: OMPIRBuilder.cpp:3832
llvm::OpenMPIRBuilder::IdentMap
DenseMap< std::pair< Constant *, uint64_t >, Constant * > IdentMap
Map to remember existing ident_t*.
Definition: OMPIRBuilder.h:878
IP
Definition: NVPTXLowerArgs.cpp:167
llvm::OpenMPIRBuilder::getInsertionPoint
InsertPointTy getInsertionPoint()
}
Definition: OMPIRBuilder.h:746
llvm::IRBuilderBase::SetCurrentDebugLocation
void SetCurrentDebugLocation(DebugLoc L)
Set location information used by debugging information.
Definition: IRBuilder.h:203
llvm::OpenMPIRBuilder::~OpenMPIRBuilder
~OpenMPIRBuilder()
Definition: OMPIRBuilder.cpp:518
llvm::IntegerType
Class to represent integer types.
Definition: DerivedTypes.h:40
llvm::CanonicalLoopInfo::isValid
bool isValid() const
Returns whether this object currently represents the IR of a loop.
Definition: OMPIRBuilder.h:1662
llvm::OpenMPIRBuilder::getOrCreateSrcLocStr
Constant * getOrCreateSrcLocStr(StringRef LocStr, uint32_t &SrcLocStrSize)
Return the (LLVM-IR) string describing the source location LocStr.
Definition: OMPIRBuilder.cpp:573
llvm::OpenMPIRBuilder::AtomicOpValue::IsSigned
bool IsSigned
Definition: OMPIRBuilder.h:1370
llvm::OpenMPIRBuilder::LocationDescription::LocationDescription
LocationDescription(const InsertPointTy &IP)
Definition: OMPIRBuilder.h:205
llvm::Instruction
Definition: Instruction.h:42
llvm::splitBBWithSuffix
BasicBlock * splitBBWithSuffix(IRBuilderBase &Builder, bool CreateBranch, llvm::Twine Suffix=".split")
Like splitBB, but reuses the current block's name for the new name.
Definition: OMPIRBuilder.cpp:323
llvm::OpenMPIRBuilder::applySimd
void applySimd(DebugLoc DL, CanonicalLoopInfo *Loop)
Add metadata to simd-ize a loop.
Definition: OMPIRBuilder.cpp:2830
llvm::OpenMPIRBuilder::LocationDescription::LocationDescription
LocationDescription(const InsertPointTy &IP, const DebugLoc &DL)
Definition: OMPIRBuilder.h:206
llvm::CanonicalLoopInfo::getBodyIP
OpenMPIRBuilder::InsertPointTy getBodyIP() const
Return the insertion point for user code in the body.
Definition: OMPIRBuilder.h:1749
DebugLoc.h
llvm::OpenMPIRBuilder::createTask
InsertPointTy createTask(const LocationDescription &Loc, InsertPointTy AllocaIP, BodyGenCallbackTy BodyGenCB, bool Tied=true)
Generator for #omp task
Definition: OMPIRBuilder.cpp:1257
llvm::OpenMPIRBuilder::MapperAllocas::Args
AllocaInst * Args
Definition: OMPIRBuilder.h:925
llvm::omp::IdentFlag
IdentFlag
IDs for all omp runtime library ident_t flag encodings (see their defintion in openmp/runtime/src/kmp...
Definition: OMPConstants.h:66
llvm::OpenMPIRBuilder::OpenMPIRBuilder
OpenMPIRBuilder(Module &M)
Create a new OpenMPIRBuilder operating on the given module M.
Definition: OMPIRBuilder.h:79
llvm::OpenMPIRBuilder::unrollLoopFull
void unrollLoopFull(DebugLoc DL, CanonicalLoopInfo *Loop)
Fully unroll a loop.
Definition: OMPIRBuilder.cpp:2815
llvm::OpenMPIRBuilder::emitTaskwaitImpl
void emitTaskwaitImpl(const LocationDescription &Loc)
Generate a taskwait runtime call.
Definition: OMPIRBuilder.cpp:1219
llvm::AddressSpace
AddressSpace
Definition: NVPTXBaseInfo.h:21
llvm::OpenMPIRBuilder::createOMPAlloc
CallInst * createOMPAlloc(const LocationDescription &Loc, Value *Size, Value *Allocator, std::string Name="")
Create a runtime call for kmpc_Alloc.
Definition: OMPIRBuilder.cpp:3451
llvm::OpenMPIRBuilder::addAttributes
void addAttributes(omp::RuntimeFunction FnID, Function &Fn)
Add attributes known for FnID to Fn.
Definition: OMPIRBuilder.cpp:329
X
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
llvm::StringMap
StringMap - This is an unconventional map that is specialized for handling keys that are "strings",...
Definition: StringMap.h:110
llvm::OpenMPIRBuilder::OutlineInfo::EntryBB
BasicBlock * EntryBB
Definition: OMPIRBuilder.h:885
llvm::OpenMPIRBuilder::Builder
IRBuilder Builder
The LLVM-IR Builder used to create IR.
Definition: OMPIRBuilder.h:872
llvm::function_ref
An efficient, type-erasing, non-owning reference to a callable.
Definition: STLFunctionalExtras.h:36
llvm::omp::RuntimeFunction
RuntimeFunction
IDs for all omp runtime library (RTL) functions.
Definition: OMPConstants.h:46
llvm::AtomicOrdering
AtomicOrdering
Atomic ordering for LLVM's memory model.
Definition: AtomicOrdering.h:56
llvm::OpenMPIRBuilder::createTaskyield
void createTaskyield(const LocationDescription &Loc)
Generator for '#omp taskyield'.
Definition: OMPIRBuilder.cpp:1250
llvm::OpenMPIRBuilder::createOffloadMaptypes
GlobalVariable * createOffloadMaptypes(SmallVectorImpl< uint64_t > &Mappings, std::string VarName)
Create the global variable holding the offload mappings information.
Definition: OMPIRBuilder.cpp:3713
llvm::OpenMPIRBuilder::AtomicOpValue::IsVolatile
bool IsVolatile
Definition: OMPIRBuilder.h:1371
llvm::OpenMPIRBuilder::createSections
InsertPointTy createSections(const LocationDescription &Loc, InsertPointTy AllocaIP, ArrayRef< StorableBodyGenCallbackTy > SectionCBs, PrivatizeCallbackTy PrivCB, FinalizeCallbackTy FiniCB, bool IsCancellable, bool IsNowait)
Generator for '#omp sections'.
Definition: OMPIRBuilder.cpp:1422
llvm::OpenMPIRBuilder::MapperAllocas::ArgsBase
AllocaInst * ArgsBase
Definition: OMPIRBuilder.h:924
llvm::GlobalValue
Definition: GlobalValue.h:44
llvm::OpenMPIRBuilder::createOMPInteropDestroy
CallInst * createOMPInteropDestroy(const LocationDescription &Loc, Value *InteropVar, Value *Device, Value *NumDependences, Value *DependenceAddress, bool HaveNowaitClause)
Create a runtime call for __tgt_interop_destroy.
Definition: OMPIRBuilder.cpp:3512
llvm::Constant
This is an important base class in LLVM.
Definition: Constant.h:41
llvm::OpenMPIRBuilder::ReductionInfo::PrivateVariable
Value * PrivateVariable
Thread-private partial reduction variable.
Definition: OMPIRBuilder.h:663
llvm::OpenMPIRBuilder::createOrderedThreadsSimd
InsertPointTy createOrderedThreadsSimd(const LocationDescription &Loc, BodyGenCallbackTy BodyGenCB, FinalizeCallbackTy FiniCB, bool IsThreads)
Generator for '#omp ordered [threads | simd]'.
Definition: OMPIRBuilder.cpp:3259
llvm::OpenMPIRBuilder::OutlineInfo::getFunction
Function * getFunction() const
Return the function that contains the region to be outlined.
Definition: OMPIRBuilder.h:894
uint64_t
D
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
Addr
uint64_t Addr
Definition: ELFObjHandler.cpp:78
llvm::OpenMPIRBuilder::LocationDescription::LocationDescription
LocationDescription(const IRBuilderBase &IRB)
Definition: OMPIRBuilder.h:203
llvm::BumpPtrAllocatorImpl
Allocate memory in an ever growing pool, as if by bump-pointer.
Definition: Allocator.h:63
llvm::OpenMPIRBuilder::isLastFinalizationInfoCancellable
bool isLastFinalizationInfoCancellable(omp::Directive DK)
Return true if the last entry in the finalization stack is of kind DK and cancellable.
Definition: OMPIRBuilder.h:847
llvm::DenseMap
Definition: DenseMap.h:716
llvm::OpenMPIRBuilder::FinalizationInfo::DK
omp::Directive DK
The directive kind of the innermost directive that has an associated region which might require final...
Definition: OMPIRBuilder.h:115
llvm::OpenMPIRBuilder::emitCancelationCheckImpl
void emitCancelationCheckImpl(Value *CancelFlag, omp::Directive CanceledDirective, FinalizeCallbackTy ExitCB={})
Generate control flow and cleanup for cancellation.
Definition: OMPIRBuilder.cpp:794
llvm::OpenMPIRBuilder::createCanonicalLoop
CanonicalLoopInfo * createCanonicalLoop(const LocationDescription &Loc, LoopBodyGenCallbackTy BodyGenCB, Value *TripCount, const Twine &Name="loop")
Generator for the control flow structure of an OpenMP canonical loop.
Definition: OMPIRBuilder.cpp:1843
llvm::OpenMPIRBuilder::createTargetInit
InsertPointTy createTargetInit(const LocationDescription &Loc, bool IsSPMD, bool RequiresFullRuntime)
The omp target interface.
Definition: OMPIRBuilder.cpp:3588
llvm::OpenMPIRBuilder::getOrCreateIdent
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.
Definition: OMPIRBuilder.cpp:533
llvm::OpenMPIRBuilder::createParallel
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'.
Definition: OMPIRBuilder.cpp:833
llvm::OpenMPIRBuilder::OutlineInfo::OuterAllocaBB
BasicBlock * OuterAllocaBB
Definition: OMPIRBuilder.h:885
llvm::OpenMPIRBuilder::finalize
void finalize(Function *Fn=nullptr)
Finalize the underlying module, e.g., by outlining regions.
Definition: OMPIRBuilder.cpp:429
IRBuilder.h
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::OpenMPIRBuilder::ReductionGenTy
function_ref< InsertPointTy(InsertPointTy, Value *, Value *, Value *&)> ReductionGenTy
Functions used to generate reductions.
Definition: OMPIRBuilder.h:635
llvm::ISD::BasicBlock
@ BasicBlock
Various leaf nodes.
Definition: ISDOpcodes.h:71
llvm::OpenMPIRBuilder::LocationDescription::DL
DebugLoc DL
Definition: OMPIRBuilder.h:209
function
print Print MemDeps of function
Definition: MemDepPrinter.cpp:82
llvm::OpenMPIRBuilder::unrollLoopHeuristic
void unrollLoopHeuristic(DebugLoc DL, CanonicalLoopInfo *Loop)
Fully or partially unroll a loop.
Definition: OMPIRBuilder.cpp:2822
llvm::OpenMPIRBuilder::getOrCreateRuntimeFunctionPtr
Function * getOrCreateRuntimeFunctionPtr(omp::RuntimeFunction FnID)
Definition: OMPIRBuilder.cpp:420
llvm::Module
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
llvm::OpenMPIRBuilder::createMaster
InsertPointTy createMaster(const LocationDescription &Loc, BodyGenCallbackTy BodyGenCB, FinalizeCallbackTy FiniCB)
Generator for '#omp master'.
Definition: OMPIRBuilder.cpp:1727
llvm::OpenMPIRBuilder::createFlush
void createFlush(const LocationDescription &Loc)
Generator for '#omp flush'.
Definition: OMPIRBuilder.cpp:1213
Builder
assume Assume Builder
Definition: AssumeBundleBuilder.cpp:651
llvm::OpenMPIRBuilder::OutlineInfo
Helper that contains information about regions we need to outline during finalization.
Definition: OMPIRBuilder.h:882
llvm::CanonicalLoopInfo
Class to represented the control flow structure of an OpenMP canonical loop.
Definition: OMPIRBuilder.h:1618
llvm::OpenMPIRBuilder::createGlobalFlag
GlobalValue * createGlobalFlag(unsigned Value, StringRef Name)
Create a hidden global flag Name in the module with initial value Value.
Definition: OMPIRBuilder.cpp:522
llvm::OpenMPIRBuilder::createOMPInteropInit
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.
Definition: OMPIRBuilder.cpp:3483
llvm::omp::OMPAtomicCompareOp
OMPAtomicCompareOp
Atomic compare operations. Currently OpenMP only supports ==, >, and <.
Definition: OMPConstants.h:208
llvm::ArrayRef
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: APInt.h:32
Cond
SmallVector< MachineOperand, 4 > Cond
Definition: BasicBlockSections.cpp:178
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
llvm::Value::getType
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:255
llvm::OpenMPIRBuilder::createOffloadMapnames
GlobalVariable * createOffloadMapnames(SmallVectorImpl< llvm::Constant * > &Names, std::string VarName)
Create the global variable holding the offload names information.
Definition: OMPIRBuilder.cpp:4163
uint32_t
llvm::IRBuilderBase
Common base class shared among various IRBuilders.
Definition: IRBuilder.h:93
DL
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
Definition: AArch64SLSHardening.cpp:76
Module
Machine Check Debug Module
Definition: MachineCheckDebugify.cpp:122
llvm::splitBB
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).
Definition: OMPIRBuilder.cpp:284
llvm::OpenMPIRBuilder::MapperAllocas::ArgSizes
AllocaInst * ArgSizes
Definition: OMPIRBuilder.h:926
llvm::OpenMPIRBuilder::OutlineInfo::PostOutlineCB
PostOutlineCBTy PostOutlineCB
Definition: OMPIRBuilder.h:884
llvm::OpenMPIRBuilder::unrollLoopPartial
void unrollLoopPartial(DebugLoc DL, CanonicalLoopInfo *Loop, int32_t Factor, CanonicalLoopInfo **UnrolledCLI)
Partially unroll a loop.
Definition: OMPIRBuilder.cpp:3047
llvm::OpenMPIRBuilder::addOutlineInfo
void addOutlineInfo(OutlineInfo &&OI)
Add a new region that will be outlined later.
Definition: OMPIRBuilder.h:905
llvm::OpenMPIRBuilder::createMasked
InsertPointTy createMasked(const LocationDescription &Loc, BodyGenCallbackTy BodyGenCB, FinalizeCallbackTy FiniCB, Value *Filter)
Generator for '#omp masked'.
Definition: OMPIRBuilder.cpp:1752
llvm::OpenMPIRBuilder::updateToLocation
bool updateToLocation(const LocationDescription &Loc)
Update the internal location to Loc.
Definition: OMPIRBuilder.h:749
llvm::OpenMPIRBuilder::FinalizationStack
SmallVector< FinalizationInfo, 8 > FinalizationStack
The finalization stack made up of finalize callbacks currently in-flight, wrapped into FinalizationIn...
Definition: OMPIRBuilder.h:843
llvm::BasicBlock::front
const Instruction & front() const
Definition: BasicBlock.h:309
llvm::CanonicalLoopInfo::getExit
BasicBlock * getExit() const
Reaching the exit indicates no more iterations are being executed.
Definition: OMPIRBuilder.h:1701
llvm::OpenMPIRBuilder::LoopInfos
std::forward_list< CanonicalLoopInfo > LoopInfos
Collection of owned canonical loop objects that eventually need to be free'd.
Definition: OMPIRBuilder.h:902
llvm::OpenMPIRBuilder::createTaskwait
void createTaskwait(const LocationDescription &Loc)
Generator for '#omp taskwait'.
Definition: OMPIRBuilder.cpp:1232
llvm::Twine
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:83
llvm::OpenMPIRBuilder::ReductionInfo::ReductionGen
ReductionGenTy ReductionGen
Callback for generating the reduction body.
Definition: OMPIRBuilder.h:668
llvm::OpenMPIRBuilder::AtomicOpValue::ElemTy
Type * ElemTy
Definition: OMPIRBuilder.h:1369
llvm::GraphProgram::Name
Name
Definition: GraphWriter.h:50
llvm::AMDGPU::SendMsg::Op
Op
Definition: SIDefines.h:341
llvm::OpenMPIRBuilder::ReductionInfo::ElementType
Type * ElementType
Reduction element type, must match pointee type of variable.
Definition: OMPIRBuilder.h:657
llvm::OpenMPIRBuilder::createAtomicWrite
InsertPointTy createAtomicWrite(const LocationDescription &Loc, AtomicOpValue &X, Value *Expr, AtomicOrdering AO)
Emit atomic write for : X = Expr — Only Scalar data types.
Definition: OMPIRBuilder.cpp:3874
llvm::SectionName
Definition: DWARFSection.h:21
llvm::OpenMPIRBuilder::createAtomicCapture
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 ,...
Definition: OMPIRBuilder.cpp:4076
llvm::CanonicalLoopInfo::invalidate
void invalidate()
Invalidate this loop.
Definition: OMPIRBuilder.cpp:4372
llvm::OpenMPIRBuilder::createCancel
InsertPointTy createCancel(const LocationDescription &Loc, Value *IfCondition, omp::Directive CanceledDirective)
Generator for '#omp cancel'.
Definition: OMPIRBuilder.cpp:705
llvm::TargetStackID::Value
Value
Definition: TargetFrameLowering.h:27
llvm::OpenMPIRBuilder::OutlineInfo::PostOutlineCBTy
std::function< void(Function &)> PostOutlineCBTy
Definition: OMPIRBuilder.h:883
llvm::CanonicalLoopInfo::getHeader
BasicBlock * getHeader() const
The header is the entry for each iteration.
Definition: OMPIRBuilder.h:1672
llvm::OpenMPIRBuilder::applyWorkshareLoop
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.
Definition: OMPIRBuilder.cpp:2190
OMPConstants.h
llvm::OpenMPIRBuilder::createTargetDeinit
void createTargetDeinit(const LocationDescription &Loc, bool IsSPMD, bool RequiresFullRuntime)
Create a runtime call for kmpc_target_deinit.
Definition: OMPIRBuilder.cpp:3641
llvm::OpenMPIRBuilder::ReductionInfo
Information about an OpenMP reduction.
Definition: OMPIRBuilder.h:645
llvm::IRBuilderBase::saveIP
InsertPoint saveIP() const
Returns the current insert point.
Definition: IRBuilder.h:254
llvm::OpenMPIRBuilder::initialize
void initialize()
Initialize the internal state, this will put structures types and potentially other helpers into the ...
Definition: OMPIRBuilder.cpp:427
llvm::CanonicalLoopInfo::getLatch
BasicBlock * getLatch() const
Reaching the latch indicates the end of the loop body code.
Definition: OMPIRBuilder.h:1695
llvm::OpenMPIRBuilder::BodyGenCallbackTy
function_ref< void(InsertPointTy AllocaIP, InsertPointTy CodeGenIP)> BodyGenCallbackTy
Callback type for body (=inner region) code generation.
Definition: OMPIRBuilder.h:158
llvm::OpenMPIRBuilder::FinalizationInfo
Definition: OMPIRBuilder.h:108
Allocator
Basic Register Allocator
Definition: RegAllocBasic.cpp:142
llvm::CanonicalLoopInfo::getIndVarType
Type * getIndVarType() const
Return the type of the induction variable (and the trip count).
Definition: OMPIRBuilder.h:1736
llvm::OpenMPIRBuilder::SrcLocStrMap
StringMap< Constant * > SrcLocStrMap
Map to remember source location strings.
Definition: OMPIRBuilder.h:875
llvm::OpenMPIRBuilder::emitBarrierImpl
InsertPointTy emitBarrierImpl(const LocationDescription &Loc, omp::Directive DK, bool ForceSimpleCall, bool CheckCancelFlag)
Generate a barrier runtime call.
Definition: OMPIRBuilder.cpp:656
llvm::OpenMPIRBuilder::InternalVars
StringMap< AssertingVH< Constant >, BumpPtrAllocator > InternalVars
An ordered map of auto-generated variables to their unique names.
Definition: OMPIRBuilder.h:912
llvm::FunctionCallee
A handy container for a FunctionType+Callee-pointer pair, which can be passed around as a single enti...
Definition: DerivedTypes.h:165
llvm::SmallVectorImpl< BasicBlock * >
llvm::CanonicalLoopInfo::getTripCount
Value * getTripCount() const
Returns the llvm::Value containing the number of loop iterations.
Definition: OMPIRBuilder.h:1719
llvm::SmallPtrSetImpl
A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...
Definition: SmallPtrSet.h:344
llvm::OpenMPIRBuilder::emitFlush
void emitFlush(const LocationDescription &Loc)
Generate a flush runtime call.
Definition: OMPIRBuilder.cpp:1204
llvm::CallInst
This class represents a function call, abstracting a target machine's calling convention.
Definition: Instructions.h:1474
llvm::OpenMPIRBuilder::OutlineInfo::collectBlocks
void collectBlocks(SmallPtrSetImpl< BasicBlock * > &BlockSet, SmallVectorImpl< BasicBlock * > &BlockVector)
Collect all blocks in between EntryBB and ExitBB in both the given vector and set.
Definition: OMPIRBuilder.cpp:4197
llvm::OpenMPIRBuilder::MapperAllocas
Definition: OMPIRBuilder.h:923
llvm::OpenMPIRBuilder::ReductionInfo::ReductionInfo
ReductionInfo(Type *ElementType, Value *Variable, Value *PrivateVariable, ReductionGenTy ReductionGen, AtomicReductionGenTy AtomicReductionGen)
Definition: OMPIRBuilder.h:646
llvm::DebugLoc
A debug info location.
Definition: DebugLoc.h:33
llvm::AllocaInst
an instruction to allocate memory on the stack
Definition: Instructions.h:58
llvm::User::getOperand
Value * getOperand(unsigned i) const
Definition: User.h:169
llvm::OpenMPIRBuilder::createCopyinClauseBlocks
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' ...
Definition: OMPIRBuilder.cpp:3401
llvm::OpenMPIRBuilder::createCopyPrivate
InsertPointTy createCopyPrivate(const LocationDescription &Loc, llvm::Value *BufSize, llvm::Value *CpyBuf, llvm::Value *CpyFn, llvm::Value *DidIt)
Generator for __kmpc_copyprivate.
Definition: OMPIRBuilder.cpp:3116
llvm::omp::OMPScheduleType
OMPScheduleType
Definition: OMPConstants.h:77
llvm::OpenMPIRBuilder::getOrCreateRuntimeFunction
FunctionCallee getOrCreateRuntimeFunction(Module &M, omp::RuntimeFunction FnID)
Return the function declaration for the runtime function with FnID.
Definition: OMPIRBuilder.cpp:362
llvm::OpenMPIRBuilder::ReductionInfo::AtomicReductionGen
AtomicReductionGenTy AtomicReductionGen
Callback for generating the atomic reduction body, may be null.
Definition: OMPIRBuilder.h:674
llvm::CanonicalLoopInfo::getBody
BasicBlock * getBody() const
The body block is the single entry for a loop iteration and not controlled by CanonicalLoopInfo.
Definition: OMPIRBuilder.h:1687
llvm::Value
LLVM Value Representation.
Definition: Value.h:74
llvm::OpenMPIRBuilder::LocationDescription::IP
InsertPointTy IP
Definition: OMPIRBuilder.h:208