1 //===----- CGOpenMPRuntime.cpp - Interface to OpenMP Runtimes -------------===//
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 provides a class for OpenMP runtime code generation.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "CGOpenMPRuntime.h"
14 #include "CGCXXABI.h"
15 #include "CGCleanup.h"
16 #include "CGRecordLayout.h"
17 #include "CodeGenFunction.h"
18 #include "clang/AST/Attr.h"
19 #include "clang/AST/Decl.h"
20 #include "clang/AST/OpenMPClause.h"
21 #include "clang/AST/StmtOpenMP.h"
22 #include "clang/AST/StmtVisitor.h"
23 #include "clang/Basic/BitmaskEnum.h"
24 #include "clang/Basic/FileManager.h"
25 #include "clang/Basic/OpenMPKinds.h"
26 #include "clang/Basic/SourceManager.h"
27 #include "clang/CodeGen/ConstantInitBuilder.h"
28 #include "llvm/ADT/ArrayRef.h"
29 #include "llvm/ADT/SetOperations.h"
30 #include "llvm/ADT/StringExtras.h"
31 #include "llvm/Bitcode/BitcodeReader.h"
32 #include "llvm/Frontend/OpenMP/OMPIRBuilder.h"
33 #include "llvm/IR/Constants.h"
34 #include "llvm/IR/DerivedTypes.h"
35 #include "llvm/IR/GlobalValue.h"
36 #include "llvm/IR/Value.h"
37 #include "llvm/Support/AtomicOrdering.h"
38 #include "llvm/Support/Format.h"
39 #include "llvm/Support/raw_ostream.h"
40 #include <cassert>
41 #include <numeric>
42 
43 using namespace clang;
44 using namespace CodeGen;
45 using namespace llvm::omp;
46 
47 namespace {
48 /// Base class for handling code generation inside OpenMP regions.
49 class CGOpenMPRegionInfo : public CodeGenFunction::CGCapturedStmtInfo {
50 public:
51   /// Kinds of OpenMP regions used in codegen.
52   enum CGOpenMPRegionKind {
53     /// Region with outlined function for standalone 'parallel'
54     /// directive.
55     ParallelOutlinedRegion,
56     /// Region with outlined function for standalone 'task' directive.
57     TaskOutlinedRegion,
58     /// Region for constructs that do not require function outlining,
59     /// like 'for', 'sections', 'atomic' etc. directives.
60     InlinedRegion,
61     /// Region with outlined function for standalone 'target' directive.
62     TargetRegion,
63   };
64 
65   CGOpenMPRegionInfo(const CapturedStmt &CS,
66                      const CGOpenMPRegionKind RegionKind,
67                      const RegionCodeGenTy &CodeGen, OpenMPDirectiveKind Kind,
68                      bool HasCancel)
69       : CGCapturedStmtInfo(CS, CR_OpenMP), RegionKind(RegionKind),
70         CodeGen(CodeGen), Kind(Kind), HasCancel(HasCancel) {}
71 
72   CGOpenMPRegionInfo(const CGOpenMPRegionKind RegionKind,
73                      const RegionCodeGenTy &CodeGen, OpenMPDirectiveKind Kind,
74                      bool HasCancel)
75       : CGCapturedStmtInfo(CR_OpenMP), RegionKind(RegionKind), CodeGen(CodeGen),
76         Kind(Kind), HasCancel(HasCancel) {}
77 
78   /// Get a variable or parameter for storing global thread id
79   /// inside OpenMP construct.
80   virtual const VarDecl *getThreadIDVariable() const = 0;
81 
82   /// Emit the captured statement body.
83   void EmitBody(CodeGenFunction &CGF, const Stmt *S) override;
84 
85   /// Get an LValue for the current ThreadID variable.
86   /// \return LValue for thread id variable. This LValue always has type int32*.
87   virtual LValue getThreadIDVariableLValue(CodeGenFunction &CGF);
88 
89   virtual void emitUntiedSwitch(CodeGenFunction & /*CGF*/) {}
90 
91   CGOpenMPRegionKind getRegionKind() const { return RegionKind; }
92 
93   OpenMPDirectiveKind getDirectiveKind() const { return Kind; }
94 
95   bool hasCancel() const { return HasCancel; }
96 
97   static bool classof(const CGCapturedStmtInfo *Info) {
98     return Info->getKind() == CR_OpenMP;
99   }
100 
101   ~CGOpenMPRegionInfo() override = default;
102 
103 protected:
104   CGOpenMPRegionKind RegionKind;
105   RegionCodeGenTy CodeGen;
106   OpenMPDirectiveKind Kind;
107   bool HasCancel;
108 };
109 
110 /// API for captured statement code generation in OpenMP constructs.
111 class CGOpenMPOutlinedRegionInfo final : public CGOpenMPRegionInfo {
112 public:
113   CGOpenMPOutlinedRegionInfo(const CapturedStmt &CS, const VarDecl *ThreadIDVar,
114                              const RegionCodeGenTy &CodeGen,
115                              OpenMPDirectiveKind Kind, bool HasCancel,
116                              StringRef HelperName)
117       : CGOpenMPRegionInfo(CS, ParallelOutlinedRegion, CodeGen, Kind,
118                            HasCancel),
119         ThreadIDVar(ThreadIDVar), HelperName(HelperName) {
120     assert(ThreadIDVar != nullptr && "No ThreadID in OpenMP region.");
121   }
122 
123   /// Get a variable or parameter for storing global thread id
124   /// inside OpenMP construct.
125   const VarDecl *getThreadIDVariable() const override { return ThreadIDVar; }
126 
127   /// Get the name of the capture helper.
128   StringRef getHelperName() const override { return HelperName; }
129 
130   static bool classof(const CGCapturedStmtInfo *Info) {
131     return CGOpenMPRegionInfo::classof(Info) &&
132            cast<CGOpenMPRegionInfo>(Info)->getRegionKind() ==
133                ParallelOutlinedRegion;
134   }
135 
136 private:
137   /// A variable or parameter storing global thread id for OpenMP
138   /// constructs.
139   const VarDecl *ThreadIDVar;
140   StringRef HelperName;
141 };
142 
143 /// API for captured statement code generation in OpenMP constructs.
144 class CGOpenMPTaskOutlinedRegionInfo final : public CGOpenMPRegionInfo {
145 public:
146   class UntiedTaskActionTy final : public PrePostActionTy {
147     bool Untied;
148     const VarDecl *PartIDVar;
149     const RegionCodeGenTy UntiedCodeGen;
150     llvm::SwitchInst *UntiedSwitch = nullptr;
151 
152   public:
153     UntiedTaskActionTy(bool Tied, const VarDecl *PartIDVar,
154                        const RegionCodeGenTy &UntiedCodeGen)
155         : Untied(!Tied), PartIDVar(PartIDVar), UntiedCodeGen(UntiedCodeGen) {}
156     void Enter(CodeGenFunction &CGF) override {
157       if (Untied) {
158         // Emit task switching point.
159         LValue PartIdLVal = CGF.EmitLoadOfPointerLValue(
160             CGF.GetAddrOfLocalVar(PartIDVar),
161             PartIDVar->getType()->castAs<PointerType>());
162         llvm::Value *Res =
163             CGF.EmitLoadOfScalar(PartIdLVal, PartIDVar->getLocation());
164         llvm::BasicBlock *DoneBB = CGF.createBasicBlock(".untied.done.");
165         UntiedSwitch = CGF.Builder.CreateSwitch(Res, DoneBB);
166         CGF.EmitBlock(DoneBB);
167         CGF.EmitBranchThroughCleanup(CGF.ReturnBlock);
168         CGF.EmitBlock(CGF.createBasicBlock(".untied.jmp."));
169         UntiedSwitch->addCase(CGF.Builder.getInt32(0),
170                               CGF.Builder.GetInsertBlock());
171         emitUntiedSwitch(CGF);
172       }
173     }
174     void emitUntiedSwitch(CodeGenFunction &CGF) const {
175       if (Untied) {
176         LValue PartIdLVal = CGF.EmitLoadOfPointerLValue(
177             CGF.GetAddrOfLocalVar(PartIDVar),
178             PartIDVar->getType()->castAs<PointerType>());
179         CGF.EmitStoreOfScalar(CGF.Builder.getInt32(UntiedSwitch->getNumCases()),
180                               PartIdLVal);
181         UntiedCodeGen(CGF);
182         CodeGenFunction::JumpDest CurPoint =
183             CGF.getJumpDestInCurrentScope(".untied.next.");
184         CGF.EmitBranchThroughCleanup(CGF.ReturnBlock);
185         CGF.EmitBlock(CGF.createBasicBlock(".untied.jmp."));
186         UntiedSwitch->addCase(CGF.Builder.getInt32(UntiedSwitch->getNumCases()),
187                               CGF.Builder.GetInsertBlock());
188         CGF.EmitBranchThroughCleanup(CurPoint);
189         CGF.EmitBlock(CurPoint.getBlock());
190       }
191     }
192     unsigned getNumberOfParts() const { return UntiedSwitch->getNumCases(); }
193   };
194   CGOpenMPTaskOutlinedRegionInfo(const CapturedStmt &CS,
195                                  const VarDecl *ThreadIDVar,
196                                  const RegionCodeGenTy &CodeGen,
197                                  OpenMPDirectiveKind Kind, bool HasCancel,
198                                  const UntiedTaskActionTy &Action)
199       : CGOpenMPRegionInfo(CS, TaskOutlinedRegion, CodeGen, Kind, HasCancel),
200         ThreadIDVar(ThreadIDVar), Action(Action) {
201     assert(ThreadIDVar != nullptr && "No ThreadID in OpenMP region.");
202   }
203 
204   /// Get a variable or parameter for storing global thread id
205   /// inside OpenMP construct.
206   const VarDecl *getThreadIDVariable() const override { return ThreadIDVar; }
207 
208   /// Get an LValue for the current ThreadID variable.
209   LValue getThreadIDVariableLValue(CodeGenFunction &CGF) override;
210 
211   /// Get the name of the capture helper.
212   StringRef getHelperName() const override { return ".omp_outlined."; }
213 
214   void emitUntiedSwitch(CodeGenFunction &CGF) override {
215     Action.emitUntiedSwitch(CGF);
216   }
217 
218   static bool classof(const CGCapturedStmtInfo *Info) {
219     return CGOpenMPRegionInfo::classof(Info) &&
220            cast<CGOpenMPRegionInfo>(Info)->getRegionKind() ==
221                TaskOutlinedRegion;
222   }
223 
224 private:
225   /// A variable or parameter storing global thread id for OpenMP
226   /// constructs.
227   const VarDecl *ThreadIDVar;
228   /// Action for emitting code for untied tasks.
229   const UntiedTaskActionTy &Action;
230 };
231 
232 /// API for inlined captured statement code generation in OpenMP
233 /// constructs.
234 class CGOpenMPInlinedRegionInfo : public CGOpenMPRegionInfo {
235 public:
236   CGOpenMPInlinedRegionInfo(CodeGenFunction::CGCapturedStmtInfo *OldCSI,
237                             const RegionCodeGenTy &CodeGen,
238                             OpenMPDirectiveKind Kind, bool HasCancel)
239       : CGOpenMPRegionInfo(InlinedRegion, CodeGen, Kind, HasCancel),
240         OldCSI(OldCSI),
241         OuterRegionInfo(dyn_cast_or_null<CGOpenMPRegionInfo>(OldCSI)) {}
242 
243   // Retrieve the value of the context parameter.
244   llvm::Value *getContextValue() const override {
245     if (OuterRegionInfo)
246       return OuterRegionInfo->getContextValue();
247     llvm_unreachable("No context value for inlined OpenMP region");
248   }
249 
250   void setContextValue(llvm::Value *V) override {
251     if (OuterRegionInfo) {
252       OuterRegionInfo->setContextValue(V);
253       return;
254     }
255     llvm_unreachable("No context value for inlined OpenMP region");
256   }
257 
258   /// Lookup the captured field decl for a variable.
259   const FieldDecl *lookup(const VarDecl *VD) const override {
260     if (OuterRegionInfo)
261       return OuterRegionInfo->lookup(VD);
262     // If there is no outer outlined region,no need to lookup in a list of
263     // captured variables, we can use the original one.
264     return nullptr;
265   }
266 
267   FieldDecl *getThisFieldDecl() const override {
268     if (OuterRegionInfo)
269       return OuterRegionInfo->getThisFieldDecl();
270     return nullptr;
271   }
272 
273   /// Get a variable or parameter for storing global thread id
274   /// inside OpenMP construct.
275   const VarDecl *getThreadIDVariable() const override {
276     if (OuterRegionInfo)
277       return OuterRegionInfo->getThreadIDVariable();
278     return nullptr;
279   }
280 
281   /// Get an LValue for the current ThreadID variable.
282   LValue getThreadIDVariableLValue(CodeGenFunction &CGF) override {
283     if (OuterRegionInfo)
284       return OuterRegionInfo->getThreadIDVariableLValue(CGF);
285     llvm_unreachable("No LValue for inlined OpenMP construct");
286   }
287 
288   /// Get the name of the capture helper.
289   StringRef getHelperName() const override {
290     if (auto *OuterRegionInfo = getOldCSI())
291       return OuterRegionInfo->getHelperName();
292     llvm_unreachable("No helper name for inlined OpenMP construct");
293   }
294 
295   void emitUntiedSwitch(CodeGenFunction &CGF) override {
296     if (OuterRegionInfo)
297       OuterRegionInfo->emitUntiedSwitch(CGF);
298   }
299 
300   CodeGenFunction::CGCapturedStmtInfo *getOldCSI() const { return OldCSI; }
301 
302   static bool classof(const CGCapturedStmtInfo *Info) {
303     return CGOpenMPRegionInfo::classof(Info) &&
304            cast<CGOpenMPRegionInfo>(Info)->getRegionKind() == InlinedRegion;
305   }
306 
307   ~CGOpenMPInlinedRegionInfo() override = default;
308 
309 private:
310   /// CodeGen info about outer OpenMP region.
311   CodeGenFunction::CGCapturedStmtInfo *OldCSI;
312   CGOpenMPRegionInfo *OuterRegionInfo;
313 };
314 
315 /// API for captured statement code generation in OpenMP target
316 /// constructs. For this captures, implicit parameters are used instead of the
317 /// captured fields. The name of the target region has to be unique in a given
318 /// application so it is provided by the client, because only the client has
319 /// the information to generate that.
320 class CGOpenMPTargetRegionInfo final : public CGOpenMPRegionInfo {
321 public:
322   CGOpenMPTargetRegionInfo(const CapturedStmt &CS,
323                            const RegionCodeGenTy &CodeGen, StringRef HelperName)
324       : CGOpenMPRegionInfo(CS, TargetRegion, CodeGen, OMPD_target,
325                            /*HasCancel=*/false),
326         HelperName(HelperName) {}
327 
328   /// This is unused for target regions because each starts executing
329   /// with a single thread.
330   const VarDecl *getThreadIDVariable() const override { return nullptr; }
331 
332   /// Get the name of the capture helper.
333   StringRef getHelperName() const override { return HelperName; }
334 
335   static bool classof(const CGCapturedStmtInfo *Info) {
336     return CGOpenMPRegionInfo::classof(Info) &&
337            cast<CGOpenMPRegionInfo>(Info)->getRegionKind() == TargetRegion;
338   }
339 
340 private:
341   StringRef HelperName;
342 };
343 
344 static void EmptyCodeGen(CodeGenFunction &, PrePostActionTy &) {
345   llvm_unreachable("No codegen for expressions");
346 }
347 /// API for generation of expressions captured in a innermost OpenMP
348 /// region.
349 class CGOpenMPInnerExprInfo final : public CGOpenMPInlinedRegionInfo {
350 public:
351   CGOpenMPInnerExprInfo(CodeGenFunction &CGF, const CapturedStmt &CS)
352       : CGOpenMPInlinedRegionInfo(CGF.CapturedStmtInfo, EmptyCodeGen,
353                                   OMPD_unknown,
354                                   /*HasCancel=*/false),
355         PrivScope(CGF) {
356     // Make sure the globals captured in the provided statement are local by
357     // using the privatization logic. We assume the same variable is not
358     // captured more than once.
359     for (const auto &C : CS.captures()) {
360       if (!C.capturesVariable() && !C.capturesVariableByCopy())
361         continue;
362 
363       const VarDecl *VD = C.getCapturedVar();
364       if (VD->isLocalVarDeclOrParm())
365         continue;
366 
367       DeclRefExpr DRE(CGF.getContext(), const_cast<VarDecl *>(VD),
368                       /*RefersToEnclosingVariableOrCapture=*/false,
369                       VD->getType().getNonReferenceType(), VK_LValue,
370                       C.getLocation());
371       PrivScope.addPrivate(
372           VD, [&CGF, &DRE]() { return CGF.EmitLValue(&DRE).getAddress(CGF); });
373     }
374     (void)PrivScope.Privatize();
375   }
376 
377   /// Lookup the captured field decl for a variable.
378   const FieldDecl *lookup(const VarDecl *VD) const override {
379     if (const FieldDecl *FD = CGOpenMPInlinedRegionInfo::lookup(VD))
380       return FD;
381     return nullptr;
382   }
383 
384   /// Emit the captured statement body.
385   void EmitBody(CodeGenFunction &CGF, const Stmt *S) override {
386     llvm_unreachable("No body for expressions");
387   }
388 
389   /// Get a variable or parameter for storing global thread id
390   /// inside OpenMP construct.
391   const VarDecl *getThreadIDVariable() const override {
392     llvm_unreachable("No thread id for expressions");
393   }
394 
395   /// Get the name of the capture helper.
396   StringRef getHelperName() const override {
397     llvm_unreachable("No helper name for expressions");
398   }
399 
400   static bool classof(const CGCapturedStmtInfo *Info) { return false; }
401 
402 private:
403   /// Private scope to capture global variables.
404   CodeGenFunction::OMPPrivateScope PrivScope;
405 };
406 
407 /// RAII for emitting code of OpenMP constructs.
408 class InlinedOpenMPRegionRAII {
409   CodeGenFunction &CGF;
410   llvm::DenseMap<const VarDecl *, FieldDecl *> LambdaCaptureFields;
411   FieldDecl *LambdaThisCaptureField = nullptr;
412   const CodeGen::CGBlockInfo *BlockInfo = nullptr;
413 
414 public:
415   /// Constructs region for combined constructs.
416   /// \param CodeGen Code generation sequence for combined directives. Includes
417   /// a list of functions used for code generation of implicitly inlined
418   /// regions.
419   InlinedOpenMPRegionRAII(CodeGenFunction &CGF, const RegionCodeGenTy &CodeGen,
420                           OpenMPDirectiveKind Kind, bool HasCancel)
421       : CGF(CGF) {
422     // Start emission for the construct.
423     CGF.CapturedStmtInfo = new CGOpenMPInlinedRegionInfo(
424         CGF.CapturedStmtInfo, CodeGen, Kind, HasCancel);
425     std::swap(CGF.LambdaCaptureFields, LambdaCaptureFields);
426     LambdaThisCaptureField = CGF.LambdaThisCaptureField;
427     CGF.LambdaThisCaptureField = nullptr;
428     BlockInfo = CGF.BlockInfo;
429     CGF.BlockInfo = nullptr;
430   }
431 
432   ~InlinedOpenMPRegionRAII() {
433     // Restore original CapturedStmtInfo only if we're done with code emission.
434     auto *OldCSI =
435         cast<CGOpenMPInlinedRegionInfo>(CGF.CapturedStmtInfo)->getOldCSI();
436     delete CGF.CapturedStmtInfo;
437     CGF.CapturedStmtInfo = OldCSI;
438     std::swap(CGF.LambdaCaptureFields, LambdaCaptureFields);
439     CGF.LambdaThisCaptureField = LambdaThisCaptureField;
440     CGF.BlockInfo = BlockInfo;
441   }
442 };
443 
444 /// Values for bit flags used in the ident_t to describe the fields.
445 /// All enumeric elements are named and described in accordance with the code
446 /// from https://github.com/llvm/llvm-project/blob/master/openmp/runtime/src/kmp.h
447 enum OpenMPLocationFlags : unsigned {
448   /// Use trampoline for internal microtask.
449   OMP_IDENT_IMD = 0x01,
450   /// Use c-style ident structure.
451   OMP_IDENT_KMPC = 0x02,
452   /// Atomic reduction option for kmpc_reduce.
453   OMP_ATOMIC_REDUCE = 0x10,
454   /// Explicit 'barrier' directive.
455   OMP_IDENT_BARRIER_EXPL = 0x20,
456   /// Implicit barrier in code.
457   OMP_IDENT_BARRIER_IMPL = 0x40,
458   /// Implicit barrier in 'for' directive.
459   OMP_IDENT_BARRIER_IMPL_FOR = 0x40,
460   /// Implicit barrier in 'sections' directive.
461   OMP_IDENT_BARRIER_IMPL_SECTIONS = 0xC0,
462   /// Implicit barrier in 'single' directive.
463   OMP_IDENT_BARRIER_IMPL_SINGLE = 0x140,
464   /// Call of __kmp_for_static_init for static loop.
465   OMP_IDENT_WORK_LOOP = 0x200,
466   /// Call of __kmp_for_static_init for sections.
467   OMP_IDENT_WORK_SECTIONS = 0x400,
468   /// Call of __kmp_for_static_init for distribute.
469   OMP_IDENT_WORK_DISTRIBUTE = 0x800,
470   LLVM_MARK_AS_BITMASK_ENUM(/*LargestValue=*/OMP_IDENT_WORK_DISTRIBUTE)
471 };
472 
473 namespace {
474 LLVM_ENABLE_BITMASK_ENUMS_IN_NAMESPACE();
475 /// Values for bit flags for marking which requires clauses have been used.
476 enum OpenMPOffloadingRequiresDirFlags : int64_t {
477   /// flag undefined.
478   OMP_REQ_UNDEFINED               = 0x000,
479   /// no requires clause present.
480   OMP_REQ_NONE                    = 0x001,
481   /// reverse_offload clause.
482   OMP_REQ_REVERSE_OFFLOAD         = 0x002,
483   /// unified_address clause.
484   OMP_REQ_UNIFIED_ADDRESS         = 0x004,
485   /// unified_shared_memory clause.
486   OMP_REQ_UNIFIED_SHARED_MEMORY   = 0x008,
487   /// dynamic_allocators clause.
488   OMP_REQ_DYNAMIC_ALLOCATORS      = 0x010,
489   LLVM_MARK_AS_BITMASK_ENUM(/*LargestValue=*/OMP_REQ_DYNAMIC_ALLOCATORS)
490 };
491 
492 enum OpenMPOffloadingReservedDeviceIDs {
493   /// Device ID if the device was not defined, runtime should get it
494   /// from environment variables in the spec.
495   OMP_DEVICEID_UNDEF = -1,
496 };
497 } // anonymous namespace
498 
499 /// Describes ident structure that describes a source location.
500 /// All descriptions are taken from
501 /// https://github.com/llvm/llvm-project/blob/master/openmp/runtime/src/kmp.h
502 /// Original structure:
503 /// typedef struct ident {
504 ///    kmp_int32 reserved_1;   /**<  might be used in Fortran;
505 ///                                  see above  */
506 ///    kmp_int32 flags;        /**<  also f.flags; KMP_IDENT_xxx flags;
507 ///                                  KMP_IDENT_KMPC identifies this union
508 ///                                  member  */
509 ///    kmp_int32 reserved_2;   /**<  not really used in Fortran any more;
510 ///                                  see above */
511 ///#if USE_ITT_BUILD
512 ///                            /*  but currently used for storing
513 ///                                region-specific ITT */
514 ///                            /*  contextual information. */
515 ///#endif /* USE_ITT_BUILD */
516 ///    kmp_int32 reserved_3;   /**< source[4] in Fortran, do not use for
517 ///                                 C++  */
518 ///    char const *psource;    /**< String describing the source location.
519 ///                            The string is composed of semi-colon separated
520 //                             fields which describe the source file,
521 ///                            the function and a pair of line numbers that
522 ///                            delimit the construct.
523 ///                             */
524 /// } ident_t;
525 enum IdentFieldIndex {
526   /// might be used in Fortran
527   IdentField_Reserved_1,
528   /// OMP_IDENT_xxx flags; OMP_IDENT_KMPC identifies this union member.
529   IdentField_Flags,
530   /// Not really used in Fortran any more
531   IdentField_Reserved_2,
532   /// Source[4] in Fortran, do not use for C++
533   IdentField_Reserved_3,
534   /// String describing the source location. The string is composed of
535   /// semi-colon separated fields which describe the source file, the function
536   /// and a pair of line numbers that delimit the construct.
537   IdentField_PSource
538 };
539 
540 /// Schedule types for 'omp for' loops (these enumerators are taken from
541 /// the enum sched_type in kmp.h).
542 enum OpenMPSchedType {
543   /// Lower bound for default (unordered) versions.
544   OMP_sch_lower = 32,
545   OMP_sch_static_chunked = 33,
546   OMP_sch_static = 34,
547   OMP_sch_dynamic_chunked = 35,
548   OMP_sch_guided_chunked = 36,
549   OMP_sch_runtime = 37,
550   OMP_sch_auto = 38,
551   /// static with chunk adjustment (e.g., simd)
552   OMP_sch_static_balanced_chunked = 45,
553   /// Lower bound for 'ordered' versions.
554   OMP_ord_lower = 64,
555   OMP_ord_static_chunked = 65,
556   OMP_ord_static = 66,
557   OMP_ord_dynamic_chunked = 67,
558   OMP_ord_guided_chunked = 68,
559   OMP_ord_runtime = 69,
560   OMP_ord_auto = 70,
561   OMP_sch_default = OMP_sch_static,
562   /// dist_schedule types
563   OMP_dist_sch_static_chunked = 91,
564   OMP_dist_sch_static = 92,
565   /// Support for OpenMP 4.5 monotonic and nonmonotonic schedule modifiers.
566   /// Set if the monotonic schedule modifier was present.
567   OMP_sch_modifier_monotonic = (1 << 29),
568   /// Set if the nonmonotonic schedule modifier was present.
569   OMP_sch_modifier_nonmonotonic = (1 << 30),
570 };
571 
572 /// A basic class for pre|post-action for advanced codegen sequence for OpenMP
573 /// region.
574 class CleanupTy final : public EHScopeStack::Cleanup {
575   PrePostActionTy *Action;
576 
577 public:
578   explicit CleanupTy(PrePostActionTy *Action) : Action(Action) {}
579   void Emit(CodeGenFunction &CGF, Flags /*flags*/) override {
580     if (!CGF.HaveInsertPoint())
581       return;
582     Action->Exit(CGF);
583   }
584 };
585 
586 } // anonymous namespace
587 
588 void RegionCodeGenTy::operator()(CodeGenFunction &CGF) const {
589   CodeGenFunction::RunCleanupsScope Scope(CGF);
590   if (PrePostAction) {
591     CGF.EHStack.pushCleanup<CleanupTy>(NormalAndEHCleanup, PrePostAction);
592     Callback(CodeGen, CGF, *PrePostAction);
593   } else {
594     PrePostActionTy Action;
595     Callback(CodeGen, CGF, Action);
596   }
597 }
598 
599 /// Check if the combiner is a call to UDR combiner and if it is so return the
600 /// UDR decl used for reduction.
601 static const OMPDeclareReductionDecl *
602 getReductionInit(const Expr *ReductionOp) {
603   if (const auto *CE = dyn_cast<CallExpr>(ReductionOp))
604     if (const auto *OVE = dyn_cast<OpaqueValueExpr>(CE->getCallee()))
605       if (const auto *DRE =
606               dyn_cast<DeclRefExpr>(OVE->getSourceExpr()->IgnoreImpCasts()))
607         if (const auto *DRD = dyn_cast<OMPDeclareReductionDecl>(DRE->getDecl()))
608           return DRD;
609   return nullptr;
610 }
611 
612 static void emitInitWithReductionInitializer(CodeGenFunction &CGF,
613                                              const OMPDeclareReductionDecl *DRD,
614                                              const Expr *InitOp,
615                                              Address Private, Address Original,
616                                              QualType Ty) {
617   if (DRD->getInitializer()) {
618     std::pair<llvm::Function *, llvm::Function *> Reduction =
619         CGF.CGM.getOpenMPRuntime().getUserDefinedReduction(DRD);
620     const auto *CE = cast<CallExpr>(InitOp);
621     const auto *OVE = cast<OpaqueValueExpr>(CE->getCallee());
622     const Expr *LHS = CE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
623     const Expr *RHS = CE->getArg(/*Arg=*/1)->IgnoreParenImpCasts();
624     const auto *LHSDRE =
625         cast<DeclRefExpr>(cast<UnaryOperator>(LHS)->getSubExpr());
626     const auto *RHSDRE =
627         cast<DeclRefExpr>(cast<UnaryOperator>(RHS)->getSubExpr());
628     CodeGenFunction::OMPPrivateScope PrivateScope(CGF);
629     PrivateScope.addPrivate(cast<VarDecl>(LHSDRE->getDecl()),
630                             [=]() { return Private; });
631     PrivateScope.addPrivate(cast<VarDecl>(RHSDRE->getDecl()),
632                             [=]() { return Original; });
633     (void)PrivateScope.Privatize();
634     RValue Func = RValue::get(Reduction.second);
635     CodeGenFunction::OpaqueValueMapping Map(CGF, OVE, Func);
636     CGF.EmitIgnoredExpr(InitOp);
637   } else {
638     llvm::Constant *Init = CGF.CGM.EmitNullConstant(Ty);
639     std::string Name = CGF.CGM.getOpenMPRuntime().getName({"init"});
640     auto *GV = new llvm::GlobalVariable(
641         CGF.CGM.getModule(), Init->getType(), /*isConstant=*/true,
642         llvm::GlobalValue::PrivateLinkage, Init, Name);
643     LValue LV = CGF.MakeNaturalAlignAddrLValue(GV, Ty);
644     RValue InitRVal;
645     switch (CGF.getEvaluationKind(Ty)) {
646     case TEK_Scalar:
647       InitRVal = CGF.EmitLoadOfLValue(LV, DRD->getLocation());
648       break;
649     case TEK_Complex:
650       InitRVal =
651           RValue::getComplex(CGF.EmitLoadOfComplex(LV, DRD->getLocation()));
652       break;
653     case TEK_Aggregate:
654       InitRVal = RValue::getAggregate(LV.getAddress(CGF));
655       break;
656     }
657     OpaqueValueExpr OVE(DRD->getLocation(), Ty, VK_RValue);
658     CodeGenFunction::OpaqueValueMapping OpaqueMap(CGF, &OVE, InitRVal);
659     CGF.EmitAnyExprToMem(&OVE, Private, Ty.getQualifiers(),
660                          /*IsInitializer=*/false);
661   }
662 }
663 
664 /// Emit initialization of arrays of complex types.
665 /// \param DestAddr Address of the array.
666 /// \param Type Type of array.
667 /// \param Init Initial expression of array.
668 /// \param SrcAddr Address of the original array.
669 static void EmitOMPAggregateInit(CodeGenFunction &CGF, Address DestAddr,
670                                  QualType Type, bool EmitDeclareReductionInit,
671                                  const Expr *Init,
672                                  const OMPDeclareReductionDecl *DRD,
673                                  Address SrcAddr = Address::invalid()) {
674   // Perform element-by-element initialization.
675   QualType ElementTy;
676 
677   // Drill down to the base element type on both arrays.
678   const ArrayType *ArrayTy = Type->getAsArrayTypeUnsafe();
679   llvm::Value *NumElements = CGF.emitArrayLength(ArrayTy, ElementTy, DestAddr);
680   DestAddr =
681       CGF.Builder.CreateElementBitCast(DestAddr, DestAddr.getElementType());
682   if (DRD)
683     SrcAddr =
684         CGF.Builder.CreateElementBitCast(SrcAddr, DestAddr.getElementType());
685 
686   llvm::Value *SrcBegin = nullptr;
687   if (DRD)
688     SrcBegin = SrcAddr.getPointer();
689   llvm::Value *DestBegin = DestAddr.getPointer();
690   // Cast from pointer to array type to pointer to single element.
691   llvm::Value *DestEnd = CGF.Builder.CreateGEP(DestBegin, NumElements);
692   // The basic structure here is a while-do loop.
693   llvm::BasicBlock *BodyBB = CGF.createBasicBlock("omp.arrayinit.body");
694   llvm::BasicBlock *DoneBB = CGF.createBasicBlock("omp.arrayinit.done");
695   llvm::Value *IsEmpty =
696       CGF.Builder.CreateICmpEQ(DestBegin, DestEnd, "omp.arrayinit.isempty");
697   CGF.Builder.CreateCondBr(IsEmpty, DoneBB, BodyBB);
698 
699   // Enter the loop body, making that address the current address.
700   llvm::BasicBlock *EntryBB = CGF.Builder.GetInsertBlock();
701   CGF.EmitBlock(BodyBB);
702 
703   CharUnits ElementSize = CGF.getContext().getTypeSizeInChars(ElementTy);
704 
705   llvm::PHINode *SrcElementPHI = nullptr;
706   Address SrcElementCurrent = Address::invalid();
707   if (DRD) {
708     SrcElementPHI = CGF.Builder.CreatePHI(SrcBegin->getType(), 2,
709                                           "omp.arraycpy.srcElementPast");
710     SrcElementPHI->addIncoming(SrcBegin, EntryBB);
711     SrcElementCurrent =
712         Address(SrcElementPHI,
713                 SrcAddr.getAlignment().alignmentOfArrayElement(ElementSize));
714   }
715   llvm::PHINode *DestElementPHI = CGF.Builder.CreatePHI(
716       DestBegin->getType(), 2, "omp.arraycpy.destElementPast");
717   DestElementPHI->addIncoming(DestBegin, EntryBB);
718   Address DestElementCurrent =
719       Address(DestElementPHI,
720               DestAddr.getAlignment().alignmentOfArrayElement(ElementSize));
721 
722   // Emit copy.
723   {
724     CodeGenFunction::RunCleanupsScope InitScope(CGF);
725     if (EmitDeclareReductionInit) {
726       emitInitWithReductionInitializer(CGF, DRD, Init, DestElementCurrent,
727                                        SrcElementCurrent, ElementTy);
728     } else
729       CGF.EmitAnyExprToMem(Init, DestElementCurrent, ElementTy.getQualifiers(),
730                            /*IsInitializer=*/false);
731   }
732 
733   if (DRD) {
734     // Shift the address forward by one element.
735     llvm::Value *SrcElementNext = CGF.Builder.CreateConstGEP1_32(
736         SrcElementPHI, /*Idx0=*/1, "omp.arraycpy.dest.element");
737     SrcElementPHI->addIncoming(SrcElementNext, CGF.Builder.GetInsertBlock());
738   }
739 
740   // Shift the address forward by one element.
741   llvm::Value *DestElementNext = CGF.Builder.CreateConstGEP1_32(
742       DestElementPHI, /*Idx0=*/1, "omp.arraycpy.dest.element");
743   // Check whether we've reached the end.
744   llvm::Value *Done =
745       CGF.Builder.CreateICmpEQ(DestElementNext, DestEnd, "omp.arraycpy.done");
746   CGF.Builder.CreateCondBr(Done, DoneBB, BodyBB);
747   DestElementPHI->addIncoming(DestElementNext, CGF.Builder.GetInsertBlock());
748 
749   // Done.
750   CGF.EmitBlock(DoneBB, /*IsFinished=*/true);
751 }
752 
753 LValue ReductionCodeGen::emitSharedLValue(CodeGenFunction &CGF, const Expr *E) {
754   return CGF.EmitOMPSharedLValue(E);
755 }
756 
757 LValue ReductionCodeGen::emitSharedLValueUB(CodeGenFunction &CGF,
758                                             const Expr *E) {
759   if (const auto *OASE = dyn_cast<OMPArraySectionExpr>(E))
760     return CGF.EmitOMPArraySectionExpr(OASE, /*IsLowerBound=*/false);
761   return LValue();
762 }
763 
764 void ReductionCodeGen::emitAggregateInitialization(
765     CodeGenFunction &CGF, unsigned N, Address PrivateAddr, LValue SharedLVal,
766     const OMPDeclareReductionDecl *DRD) {
767   // Emit VarDecl with copy init for arrays.
768   // Get the address of the original variable captured in current
769   // captured region.
770   const auto *PrivateVD =
771       cast<VarDecl>(cast<DeclRefExpr>(ClausesData[N].Private)->getDecl());
772   bool EmitDeclareReductionInit =
773       DRD && (DRD->getInitializer() || !PrivateVD->hasInit());
774   EmitOMPAggregateInit(CGF, PrivateAddr, PrivateVD->getType(),
775                        EmitDeclareReductionInit,
776                        EmitDeclareReductionInit ? ClausesData[N].ReductionOp
777                                                 : PrivateVD->getInit(),
778                        DRD, SharedLVal.getAddress(CGF));
779 }
780 
781 ReductionCodeGen::ReductionCodeGen(ArrayRef<const Expr *> Shareds,
782                                    ArrayRef<const Expr *> Origs,
783                                    ArrayRef<const Expr *> Privates,
784                                    ArrayRef<const Expr *> ReductionOps) {
785   ClausesData.reserve(Shareds.size());
786   SharedAddresses.reserve(Shareds.size());
787   Sizes.reserve(Shareds.size());
788   BaseDecls.reserve(Shareds.size());
789   const auto *IOrig = Origs.begin();
790   const auto *IPriv = Privates.begin();
791   const auto *IRed = ReductionOps.begin();
792   for (const Expr *Ref : Shareds) {
793     ClausesData.emplace_back(Ref, *IOrig, *IPriv, *IRed);
794     std::advance(IOrig, 1);
795     std::advance(IPriv, 1);
796     std::advance(IRed, 1);
797   }
798 }
799 
800 void ReductionCodeGen::emitSharedOrigLValue(CodeGenFunction &CGF, unsigned N) {
801   assert(SharedAddresses.size() == N && OrigAddresses.size() == N &&
802          "Number of generated lvalues must be exactly N.");
803   LValue First = emitSharedLValue(CGF, ClausesData[N].Shared);
804   LValue Second = emitSharedLValueUB(CGF, ClausesData[N].Shared);
805   SharedAddresses.emplace_back(First, Second);
806   if (ClausesData[N].Shared == ClausesData[N].Ref) {
807     OrigAddresses.emplace_back(First, Second);
808   } else {
809     LValue First = emitSharedLValue(CGF, ClausesData[N].Ref);
810     LValue Second = emitSharedLValueUB(CGF, ClausesData[N].Ref);
811     OrigAddresses.emplace_back(First, Second);
812   }
813 }
814 
815 void ReductionCodeGen::emitAggregateType(CodeGenFunction &CGF, unsigned N) {
816   const auto *PrivateVD =
817       cast<VarDecl>(cast<DeclRefExpr>(ClausesData[N].Private)->getDecl());
818   QualType PrivateType = PrivateVD->getType();
819   bool AsArraySection = isa<OMPArraySectionExpr>(ClausesData[N].Ref);
820   if (!PrivateType->isVariablyModifiedType()) {
821     Sizes.emplace_back(
822         CGF.getTypeSize(OrigAddresses[N].first.getType().getNonReferenceType()),
823         nullptr);
824     return;
825   }
826   llvm::Value *Size;
827   llvm::Value *SizeInChars;
828   auto *ElemType =
829       cast<llvm::PointerType>(OrigAddresses[N].first.getPointer(CGF)->getType())
830           ->getElementType();
831   auto *ElemSizeOf = llvm::ConstantExpr::getSizeOf(ElemType);
832   if (AsArraySection) {
833     Size = CGF.Builder.CreatePtrDiff(OrigAddresses[N].second.getPointer(CGF),
834                                      OrigAddresses[N].first.getPointer(CGF));
835     Size = CGF.Builder.CreateNUWAdd(
836         Size, llvm::ConstantInt::get(Size->getType(), /*V=*/1));
837     SizeInChars = CGF.Builder.CreateNUWMul(Size, ElemSizeOf);
838   } else {
839     SizeInChars =
840         CGF.getTypeSize(OrigAddresses[N].first.getType().getNonReferenceType());
841     Size = CGF.Builder.CreateExactUDiv(SizeInChars, ElemSizeOf);
842   }
843   Sizes.emplace_back(SizeInChars, Size);
844   CodeGenFunction::OpaqueValueMapping OpaqueMap(
845       CGF,
846       cast<OpaqueValueExpr>(
847           CGF.getContext().getAsVariableArrayType(PrivateType)->getSizeExpr()),
848       RValue::get(Size));
849   CGF.EmitVariablyModifiedType(PrivateType);
850 }
851 
852 void ReductionCodeGen::emitAggregateType(CodeGenFunction &CGF, unsigned N,
853                                          llvm::Value *Size) {
854   const auto *PrivateVD =
855       cast<VarDecl>(cast<DeclRefExpr>(ClausesData[N].Private)->getDecl());
856   QualType PrivateType = PrivateVD->getType();
857   if (!PrivateType->isVariablyModifiedType()) {
858     assert(!Size && !Sizes[N].second &&
859            "Size should be nullptr for non-variably modified reduction "
860            "items.");
861     return;
862   }
863   CodeGenFunction::OpaqueValueMapping OpaqueMap(
864       CGF,
865       cast<OpaqueValueExpr>(
866           CGF.getContext().getAsVariableArrayType(PrivateType)->getSizeExpr()),
867       RValue::get(Size));
868   CGF.EmitVariablyModifiedType(PrivateType);
869 }
870 
871 void ReductionCodeGen::emitInitialization(
872     CodeGenFunction &CGF, unsigned N, Address PrivateAddr, LValue SharedLVal,
873     llvm::function_ref<bool(CodeGenFunction &)> DefaultInit) {
874   assert(SharedAddresses.size() > N && "No variable was generated");
875   const auto *PrivateVD =
876       cast<VarDecl>(cast<DeclRefExpr>(ClausesData[N].Private)->getDecl());
877   const OMPDeclareReductionDecl *DRD =
878       getReductionInit(ClausesData[N].ReductionOp);
879   QualType PrivateType = PrivateVD->getType();
880   PrivateAddr = CGF.Builder.CreateElementBitCast(
881       PrivateAddr, CGF.ConvertTypeForMem(PrivateType));
882   QualType SharedType = SharedAddresses[N].first.getType();
883   SharedLVal = CGF.MakeAddrLValue(
884       CGF.Builder.CreateElementBitCast(SharedLVal.getAddress(CGF),
885                                        CGF.ConvertTypeForMem(SharedType)),
886       SharedType, SharedAddresses[N].first.getBaseInfo(),
887       CGF.CGM.getTBAAInfoForSubobject(SharedAddresses[N].first, SharedType));
888   if (CGF.getContext().getAsArrayType(PrivateVD->getType())) {
889     if (DRD && DRD->getInitializer())
890       (void)DefaultInit(CGF);
891     emitAggregateInitialization(CGF, N, PrivateAddr, SharedLVal, DRD);
892   } else if (DRD && (DRD->getInitializer() || !PrivateVD->hasInit())) {
893     (void)DefaultInit(CGF);
894     emitInitWithReductionInitializer(CGF, DRD, ClausesData[N].ReductionOp,
895                                      PrivateAddr, SharedLVal.getAddress(CGF),
896                                      SharedLVal.getType());
897   } else if (!DefaultInit(CGF) && PrivateVD->hasInit() &&
898              !CGF.isTrivialInitializer(PrivateVD->getInit())) {
899     CGF.EmitAnyExprToMem(PrivateVD->getInit(), PrivateAddr,
900                          PrivateVD->getType().getQualifiers(),
901                          /*IsInitializer=*/false);
902   }
903 }
904 
905 bool ReductionCodeGen::needCleanups(unsigned N) {
906   const auto *PrivateVD =
907       cast<VarDecl>(cast<DeclRefExpr>(ClausesData[N].Private)->getDecl());
908   QualType PrivateType = PrivateVD->getType();
909   QualType::DestructionKind DTorKind = PrivateType.isDestructedType();
910   return DTorKind != QualType::DK_none;
911 }
912 
913 void ReductionCodeGen::emitCleanups(CodeGenFunction &CGF, unsigned N,
914                                     Address PrivateAddr) {
915   const auto *PrivateVD =
916       cast<VarDecl>(cast<DeclRefExpr>(ClausesData[N].Private)->getDecl());
917   QualType PrivateType = PrivateVD->getType();
918   QualType::DestructionKind DTorKind = PrivateType.isDestructedType();
919   if (needCleanups(N)) {
920     PrivateAddr = CGF.Builder.CreateElementBitCast(
921         PrivateAddr, CGF.ConvertTypeForMem(PrivateType));
922     CGF.pushDestroy(DTorKind, PrivateAddr, PrivateType);
923   }
924 }
925 
926 static LValue loadToBegin(CodeGenFunction &CGF, QualType BaseTy, QualType ElTy,
927                           LValue BaseLV) {
928   BaseTy = BaseTy.getNonReferenceType();
929   while ((BaseTy->isPointerType() || BaseTy->isReferenceType()) &&
930          !CGF.getContext().hasSameType(BaseTy, ElTy)) {
931     if (const auto *PtrTy = BaseTy->getAs<PointerType>()) {
932       BaseLV = CGF.EmitLoadOfPointerLValue(BaseLV.getAddress(CGF), PtrTy);
933     } else {
934       LValue RefLVal = CGF.MakeAddrLValue(BaseLV.getAddress(CGF), BaseTy);
935       BaseLV = CGF.EmitLoadOfReferenceLValue(RefLVal);
936     }
937     BaseTy = BaseTy->getPointeeType();
938   }
939   return CGF.MakeAddrLValue(
940       CGF.Builder.CreateElementBitCast(BaseLV.getAddress(CGF),
941                                        CGF.ConvertTypeForMem(ElTy)),
942       BaseLV.getType(), BaseLV.getBaseInfo(),
943       CGF.CGM.getTBAAInfoForSubobject(BaseLV, BaseLV.getType()));
944 }
945 
946 static Address castToBase(CodeGenFunction &CGF, QualType BaseTy, QualType ElTy,
947                           llvm::Type *BaseLVType, CharUnits BaseLVAlignment,
948                           llvm::Value *Addr) {
949   Address Tmp = Address::invalid();
950   Address TopTmp = Address::invalid();
951   Address MostTopTmp = Address::invalid();
952   BaseTy = BaseTy.getNonReferenceType();
953   while ((BaseTy->isPointerType() || BaseTy->isReferenceType()) &&
954          !CGF.getContext().hasSameType(BaseTy, ElTy)) {
955     Tmp = CGF.CreateMemTemp(BaseTy);
956     if (TopTmp.isValid())
957       CGF.Builder.CreateStore(Tmp.getPointer(), TopTmp);
958     else
959       MostTopTmp = Tmp;
960     TopTmp = Tmp;
961     BaseTy = BaseTy->getPointeeType();
962   }
963   llvm::Type *Ty = BaseLVType;
964   if (Tmp.isValid())
965     Ty = Tmp.getElementType();
966   Addr = CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(Addr, Ty);
967   if (Tmp.isValid()) {
968     CGF.Builder.CreateStore(Addr, Tmp);
969     return MostTopTmp;
970   }
971   return Address(Addr, BaseLVAlignment);
972 }
973 
974 static const VarDecl *getBaseDecl(const Expr *Ref, const DeclRefExpr *&DE) {
975   const VarDecl *OrigVD = nullptr;
976   if (const auto *OASE = dyn_cast<OMPArraySectionExpr>(Ref)) {
977     const Expr *Base = OASE->getBase()->IgnoreParenImpCasts();
978     while (const auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base))
979       Base = TempOASE->getBase()->IgnoreParenImpCasts();
980     while (const auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
981       Base = TempASE->getBase()->IgnoreParenImpCasts();
982     DE = cast<DeclRefExpr>(Base);
983     OrigVD = cast<VarDecl>(DE->getDecl());
984   } else if (const auto *ASE = dyn_cast<ArraySubscriptExpr>(Ref)) {
985     const Expr *Base = ASE->getBase()->IgnoreParenImpCasts();
986     while (const auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
987       Base = TempASE->getBase()->IgnoreParenImpCasts();
988     DE = cast<DeclRefExpr>(Base);
989     OrigVD = cast<VarDecl>(DE->getDecl());
990   }
991   return OrigVD;
992 }
993 
994 Address ReductionCodeGen::adjustPrivateAddress(CodeGenFunction &CGF, unsigned N,
995                                                Address PrivateAddr) {
996   const DeclRefExpr *DE;
997   if (const VarDecl *OrigVD = ::getBaseDecl(ClausesData[N].Ref, DE)) {
998     BaseDecls.emplace_back(OrigVD);
999     LValue OriginalBaseLValue = CGF.EmitLValue(DE);
1000     LValue BaseLValue =
1001         loadToBegin(CGF, OrigVD->getType(), SharedAddresses[N].first.getType(),
1002                     OriginalBaseLValue);
1003     llvm::Value *Adjustment = CGF.Builder.CreatePtrDiff(
1004         BaseLValue.getPointer(CGF), SharedAddresses[N].first.getPointer(CGF));
1005     llvm::Value *PrivatePointer =
1006         CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(
1007             PrivateAddr.getPointer(),
1008             SharedAddresses[N].first.getAddress(CGF).getType());
1009     llvm::Value *Ptr = CGF.Builder.CreateGEP(PrivatePointer, Adjustment);
1010     return castToBase(CGF, OrigVD->getType(),
1011                       SharedAddresses[N].first.getType(),
1012                       OriginalBaseLValue.getAddress(CGF).getType(),
1013                       OriginalBaseLValue.getAlignment(), Ptr);
1014   }
1015   BaseDecls.emplace_back(
1016       cast<VarDecl>(cast<DeclRefExpr>(ClausesData[N].Ref)->getDecl()));
1017   return PrivateAddr;
1018 }
1019 
1020 bool ReductionCodeGen::usesReductionInitializer(unsigned N) const {
1021   const OMPDeclareReductionDecl *DRD =
1022       getReductionInit(ClausesData[N].ReductionOp);
1023   return DRD && DRD->getInitializer();
1024 }
1025 
1026 LValue CGOpenMPRegionInfo::getThreadIDVariableLValue(CodeGenFunction &CGF) {
1027   return CGF.EmitLoadOfPointerLValue(
1028       CGF.GetAddrOfLocalVar(getThreadIDVariable()),
1029       getThreadIDVariable()->getType()->castAs<PointerType>());
1030 }
1031 
1032 void CGOpenMPRegionInfo::EmitBody(CodeGenFunction &CGF, const Stmt * /*S*/) {
1033   if (!CGF.HaveInsertPoint())
1034     return;
1035   // 1.2.2 OpenMP Language Terminology
1036   // Structured block - An executable statement with a single entry at the
1037   // top and a single exit at the bottom.
1038   // The point of exit cannot be a branch out of the structured block.
1039   // longjmp() and throw() must not violate the entry/exit criteria.
1040   CGF.EHStack.pushTerminate();
1041   CodeGen(CGF);
1042   CGF.EHStack.popTerminate();
1043 }
1044 
1045 LValue CGOpenMPTaskOutlinedRegionInfo::getThreadIDVariableLValue(
1046     CodeGenFunction &CGF) {
1047   return CGF.MakeAddrLValue(CGF.GetAddrOfLocalVar(getThreadIDVariable()),
1048                             getThreadIDVariable()->getType(),
1049                             AlignmentSource::Decl);
1050 }
1051 
1052 static FieldDecl *addFieldToRecordDecl(ASTContext &C, DeclContext *DC,
1053                                        QualType FieldTy) {
1054   auto *Field = FieldDecl::Create(
1055       C, DC, SourceLocation(), SourceLocation(), /*Id=*/nullptr, FieldTy,
1056       C.getTrivialTypeSourceInfo(FieldTy, SourceLocation()),
1057       /*BW=*/nullptr, /*Mutable=*/false, /*InitStyle=*/ICIS_NoInit);
1058   Field->setAccess(AS_public);
1059   DC->addDecl(Field);
1060   return Field;
1061 }
1062 
1063 CGOpenMPRuntime::CGOpenMPRuntime(CodeGenModule &CGM, StringRef FirstSeparator,
1064                                  StringRef Separator)
1065     : CGM(CGM), FirstSeparator(FirstSeparator), Separator(Separator),
1066       OMPBuilder(CGM.getModule()), OffloadEntriesInfoManager(CGM) {
1067   ASTContext &C = CGM.getContext();
1068   RecordDecl *RD = C.buildImplicitRecord("ident_t");
1069   QualType KmpInt32Ty = C.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1);
1070   RD->startDefinition();
1071   // reserved_1
1072   addFieldToRecordDecl(C, RD, KmpInt32Ty);
1073   // flags
1074   addFieldToRecordDecl(C, RD, KmpInt32Ty);
1075   // reserved_2
1076   addFieldToRecordDecl(C, RD, KmpInt32Ty);
1077   // reserved_3
1078   addFieldToRecordDecl(C, RD, KmpInt32Ty);
1079   // psource
1080   addFieldToRecordDecl(C, RD, C.VoidPtrTy);
1081   RD->completeDefinition();
1082   IdentQTy = C.getRecordType(RD);
1083   IdentTy = CGM.getTypes().ConvertRecordDeclType(RD);
1084   KmpCriticalNameTy = llvm::ArrayType::get(CGM.Int32Ty, /*NumElements*/ 8);
1085 
1086   // Initialize Types used in OpenMPIRBuilder from OMPKinds.def
1087   OMPBuilder.initialize();
1088   loadOffloadInfoMetadata();
1089 }
1090 
1091 void CGOpenMPRuntime::clear() {
1092   InternalVars.clear();
1093   // Clean non-target variable declarations possibly used only in debug info.
1094   for (const auto &Data : EmittedNonTargetVariables) {
1095     if (!Data.getValue().pointsToAliveValue())
1096       continue;
1097     auto *GV = dyn_cast<llvm::GlobalVariable>(Data.getValue());
1098     if (!GV)
1099       continue;
1100     if (!GV->isDeclaration() || GV->getNumUses() > 0)
1101       continue;
1102     GV->eraseFromParent();
1103   }
1104 }
1105 
1106 std::string CGOpenMPRuntime::getName(ArrayRef<StringRef> Parts) const {
1107   SmallString<128> Buffer;
1108   llvm::raw_svector_ostream OS(Buffer);
1109   StringRef Sep = FirstSeparator;
1110   for (StringRef Part : Parts) {
1111     OS << Sep << Part;
1112     Sep = Separator;
1113   }
1114   return std::string(OS.str());
1115 }
1116 
1117 static llvm::Function *
1118 emitCombinerOrInitializer(CodeGenModule &CGM, QualType Ty,
1119                           const Expr *CombinerInitializer, const VarDecl *In,
1120                           const VarDecl *Out, bool IsCombiner) {
1121   // void .omp_combiner.(Ty *in, Ty *out);
1122   ASTContext &C = CGM.getContext();
1123   QualType PtrTy = C.getPointerType(Ty).withRestrict();
1124   FunctionArgList Args;
1125   ImplicitParamDecl OmpOutParm(C, /*DC=*/nullptr, Out->getLocation(),
1126                                /*Id=*/nullptr, PtrTy, ImplicitParamDecl::Other);
1127   ImplicitParamDecl OmpInParm(C, /*DC=*/nullptr, In->getLocation(),
1128                               /*Id=*/nullptr, PtrTy, ImplicitParamDecl::Other);
1129   Args.push_back(&OmpOutParm);
1130   Args.push_back(&OmpInParm);
1131   const CGFunctionInfo &FnInfo =
1132       CGM.getTypes().arrangeBuiltinFunctionDeclaration(C.VoidTy, Args);
1133   llvm::FunctionType *FnTy = CGM.getTypes().GetFunctionType(FnInfo);
1134   std::string Name = CGM.getOpenMPRuntime().getName(
1135       {IsCombiner ? "omp_combiner" : "omp_initializer", ""});
1136   auto *Fn = llvm::Function::Create(FnTy, llvm::GlobalValue::InternalLinkage,
1137                                     Name, &CGM.getModule());
1138   CGM.SetInternalFunctionAttributes(GlobalDecl(), Fn, FnInfo);
1139   if (CGM.getLangOpts().Optimize) {
1140     Fn->removeFnAttr(llvm::Attribute::NoInline);
1141     Fn->removeFnAttr(llvm::Attribute::OptimizeNone);
1142     Fn->addFnAttr(llvm::Attribute::AlwaysInline);
1143   }
1144   CodeGenFunction CGF(CGM);
1145   // Map "T omp_in;" variable to "*omp_in_parm" value in all expressions.
1146   // Map "T omp_out;" variable to "*omp_out_parm" value in all expressions.
1147   CGF.StartFunction(GlobalDecl(), C.VoidTy, Fn, FnInfo, Args, In->getLocation(),
1148                     Out->getLocation());
1149   CodeGenFunction::OMPPrivateScope Scope(CGF);
1150   Address AddrIn = CGF.GetAddrOfLocalVar(&OmpInParm);
1151   Scope.addPrivate(In, [&CGF, AddrIn, PtrTy]() {
1152     return CGF.EmitLoadOfPointerLValue(AddrIn, PtrTy->castAs<PointerType>())
1153         .getAddress(CGF);
1154   });
1155   Address AddrOut = CGF.GetAddrOfLocalVar(&OmpOutParm);
1156   Scope.addPrivate(Out, [&CGF, AddrOut, PtrTy]() {
1157     return CGF.EmitLoadOfPointerLValue(AddrOut, PtrTy->castAs<PointerType>())
1158         .getAddress(CGF);
1159   });
1160   (void)Scope.Privatize();
1161   if (!IsCombiner && Out->hasInit() &&
1162       !CGF.isTrivialInitializer(Out->getInit())) {
1163     CGF.EmitAnyExprToMem(Out->getInit(), CGF.GetAddrOfLocalVar(Out),
1164                          Out->getType().getQualifiers(),
1165                          /*IsInitializer=*/true);
1166   }
1167   if (CombinerInitializer)
1168     CGF.EmitIgnoredExpr(CombinerInitializer);
1169   Scope.ForceCleanup();
1170   CGF.FinishFunction();
1171   return Fn;
1172 }
1173 
1174 void CGOpenMPRuntime::emitUserDefinedReduction(
1175     CodeGenFunction *CGF, const OMPDeclareReductionDecl *D) {
1176   if (UDRMap.count(D) > 0)
1177     return;
1178   llvm::Function *Combiner = emitCombinerOrInitializer(
1179       CGM, D->getType(), D->getCombiner(),
1180       cast<VarDecl>(cast<DeclRefExpr>(D->getCombinerIn())->getDecl()),
1181       cast<VarDecl>(cast<DeclRefExpr>(D->getCombinerOut())->getDecl()),
1182       /*IsCombiner=*/true);
1183   llvm::Function *Initializer = nullptr;
1184   if (const Expr *Init = D->getInitializer()) {
1185     Initializer = emitCombinerOrInitializer(
1186         CGM, D->getType(),
1187         D->getInitializerKind() == OMPDeclareReductionDecl::CallInit ? Init
1188                                                                      : nullptr,
1189         cast<VarDecl>(cast<DeclRefExpr>(D->getInitOrig())->getDecl()),
1190         cast<VarDecl>(cast<DeclRefExpr>(D->getInitPriv())->getDecl()),
1191         /*IsCombiner=*/false);
1192   }
1193   UDRMap.try_emplace(D, Combiner, Initializer);
1194   if (CGF) {
1195     auto &Decls = FunctionUDRMap.FindAndConstruct(CGF->CurFn);
1196     Decls.second.push_back(D);
1197   }
1198 }
1199 
1200 std::pair<llvm::Function *, llvm::Function *>
1201 CGOpenMPRuntime::getUserDefinedReduction(const OMPDeclareReductionDecl *D) {
1202   auto I = UDRMap.find(D);
1203   if (I != UDRMap.end())
1204     return I->second;
1205   emitUserDefinedReduction(/*CGF=*/nullptr, D);
1206   return UDRMap.lookup(D);
1207 }
1208 
1209 namespace {
1210 // Temporary RAII solution to perform a push/pop stack event on the OpenMP IR
1211 // Builder if one is present.
1212 struct PushAndPopStackRAII {
1213   PushAndPopStackRAII(llvm::OpenMPIRBuilder *OMPBuilder, CodeGenFunction &CGF,
1214                       bool HasCancel)
1215       : OMPBuilder(OMPBuilder) {
1216     if (!OMPBuilder)
1217       return;
1218 
1219     // The following callback is the crucial part of clangs cleanup process.
1220     //
1221     // NOTE:
1222     // Once the OpenMPIRBuilder is used to create parallel regions (and
1223     // similar), the cancellation destination (Dest below) is determined via
1224     // IP. That means if we have variables to finalize we split the block at IP,
1225     // use the new block (=BB) as destination to build a JumpDest (via
1226     // getJumpDestInCurrentScope(BB)) which then is fed to
1227     // EmitBranchThroughCleanup. Furthermore, there will not be the need
1228     // to push & pop an FinalizationInfo object.
1229     // The FiniCB will still be needed but at the point where the
1230     // OpenMPIRBuilder is asked to construct a parallel (or similar) construct.
1231     auto FiniCB = [&CGF](llvm::OpenMPIRBuilder::InsertPointTy IP) {
1232       assert(IP.getBlock()->end() == IP.getPoint() &&
1233              "Clang CG should cause non-terminated block!");
1234       CGBuilderTy::InsertPointGuard IPG(CGF.Builder);
1235       CGF.Builder.restoreIP(IP);
1236       CodeGenFunction::JumpDest Dest =
1237           CGF.getOMPCancelDestination(OMPD_parallel);
1238       CGF.EmitBranchThroughCleanup(Dest);
1239     };
1240 
1241     // TODO: Remove this once we emit parallel regions through the
1242     //       OpenMPIRBuilder as it can do this setup internally.
1243     llvm::OpenMPIRBuilder::FinalizationInfo FI(
1244         {FiniCB, OMPD_parallel, HasCancel});
1245     OMPBuilder->pushFinalizationCB(std::move(FI));
1246   }
1247   ~PushAndPopStackRAII() {
1248     if (OMPBuilder)
1249       OMPBuilder->popFinalizationCB();
1250   }
1251   llvm::OpenMPIRBuilder *OMPBuilder;
1252 };
1253 } // namespace
1254 
1255 static llvm::Function *emitParallelOrTeamsOutlinedFunction(
1256     CodeGenModule &CGM, const OMPExecutableDirective &D, const CapturedStmt *CS,
1257     const VarDecl *ThreadIDVar, OpenMPDirectiveKind InnermostKind,
1258     const StringRef OutlinedHelperName, const RegionCodeGenTy &CodeGen) {
1259   assert(ThreadIDVar->getType()->isPointerType() &&
1260          "thread id variable must be of type kmp_int32 *");
1261   CodeGenFunction CGF(CGM, true);
1262   bool HasCancel = false;
1263   if (const auto *OPD = dyn_cast<OMPParallelDirective>(&D))
1264     HasCancel = OPD->hasCancel();
1265   else if (const auto *OPD = dyn_cast<OMPTargetParallelDirective>(&D))
1266     HasCancel = OPD->hasCancel();
1267   else if (const auto *OPSD = dyn_cast<OMPParallelSectionsDirective>(&D))
1268     HasCancel = OPSD->hasCancel();
1269   else if (const auto *OPFD = dyn_cast<OMPParallelForDirective>(&D))
1270     HasCancel = OPFD->hasCancel();
1271   else if (const auto *OPFD = dyn_cast<OMPTargetParallelForDirective>(&D))
1272     HasCancel = OPFD->hasCancel();
1273   else if (const auto *OPFD = dyn_cast<OMPDistributeParallelForDirective>(&D))
1274     HasCancel = OPFD->hasCancel();
1275   else if (const auto *OPFD =
1276                dyn_cast<OMPTeamsDistributeParallelForDirective>(&D))
1277     HasCancel = OPFD->hasCancel();
1278   else if (const auto *OPFD =
1279                dyn_cast<OMPTargetTeamsDistributeParallelForDirective>(&D))
1280     HasCancel = OPFD->hasCancel();
1281 
1282   // TODO: Temporarily inform the OpenMPIRBuilder, if any, about the new
1283   //       parallel region to make cancellation barriers work properly.
1284   llvm::OpenMPIRBuilder &OMPBuilder = CGM.getOpenMPRuntime().getOMPBuilder();
1285   PushAndPopStackRAII PSR(&OMPBuilder, CGF, HasCancel);
1286   CGOpenMPOutlinedRegionInfo CGInfo(*CS, ThreadIDVar, CodeGen, InnermostKind,
1287                                     HasCancel, OutlinedHelperName);
1288   CodeGenFunction::CGCapturedStmtRAII CapInfoRAII(CGF, &CGInfo);
1289   return CGF.GenerateOpenMPCapturedStmtFunction(*CS, D.getBeginLoc());
1290 }
1291 
1292 llvm::Function *CGOpenMPRuntime::emitParallelOutlinedFunction(
1293     const OMPExecutableDirective &D, const VarDecl *ThreadIDVar,
1294     OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen) {
1295   const CapturedStmt *CS = D.getCapturedStmt(OMPD_parallel);
1296   return emitParallelOrTeamsOutlinedFunction(
1297       CGM, D, CS, ThreadIDVar, InnermostKind, getOutlinedHelperName(), CodeGen);
1298 }
1299 
1300 llvm::Function *CGOpenMPRuntime::emitTeamsOutlinedFunction(
1301     const OMPExecutableDirective &D, const VarDecl *ThreadIDVar,
1302     OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen) {
1303   const CapturedStmt *CS = D.getCapturedStmt(OMPD_teams);
1304   return emitParallelOrTeamsOutlinedFunction(
1305       CGM, D, CS, ThreadIDVar, InnermostKind, getOutlinedHelperName(), CodeGen);
1306 }
1307 
1308 llvm::Function *CGOpenMPRuntime::emitTaskOutlinedFunction(
1309     const OMPExecutableDirective &D, const VarDecl *ThreadIDVar,
1310     const VarDecl *PartIDVar, const VarDecl *TaskTVar,
1311     OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen,
1312     bool Tied, unsigned &NumberOfParts) {
1313   auto &&UntiedCodeGen = [this, &D, TaskTVar](CodeGenFunction &CGF,
1314                                               PrePostActionTy &) {
1315     llvm::Value *ThreadID = getThreadID(CGF, D.getBeginLoc());
1316     llvm::Value *UpLoc = emitUpdateLocation(CGF, D.getBeginLoc());
1317     llvm::Value *TaskArgs[] = {
1318         UpLoc, ThreadID,
1319         CGF.EmitLoadOfPointerLValue(CGF.GetAddrOfLocalVar(TaskTVar),
1320                                     TaskTVar->getType()->castAs<PointerType>())
1321             .getPointer(CGF)};
1322     CGF.EmitRuntimeCall(OMPBuilder.getOrCreateRuntimeFunction(
1323                             CGM.getModule(), OMPRTL___kmpc_omp_task),
1324                         TaskArgs);
1325   };
1326   CGOpenMPTaskOutlinedRegionInfo::UntiedTaskActionTy Action(Tied, PartIDVar,
1327                                                             UntiedCodeGen);
1328   CodeGen.setAction(Action);
1329   assert(!ThreadIDVar->getType()->isPointerType() &&
1330          "thread id variable must be of type kmp_int32 for tasks");
1331   const OpenMPDirectiveKind Region =
1332       isOpenMPTaskLoopDirective(D.getDirectiveKind()) ? OMPD_taskloop
1333                                                       : OMPD_task;
1334   const CapturedStmt *CS = D.getCapturedStmt(Region);
1335   bool HasCancel = false;
1336   if (const auto *TD = dyn_cast<OMPTaskDirective>(&D))
1337     HasCancel = TD->hasCancel();
1338   else if (const auto *TD = dyn_cast<OMPTaskLoopDirective>(&D))
1339     HasCancel = TD->hasCancel();
1340   else if (const auto *TD = dyn_cast<OMPMasterTaskLoopDirective>(&D))
1341     HasCancel = TD->hasCancel();
1342   else if (const auto *TD = dyn_cast<OMPParallelMasterTaskLoopDirective>(&D))
1343     HasCancel = TD->hasCancel();
1344 
1345   CodeGenFunction CGF(CGM, true);
1346   CGOpenMPTaskOutlinedRegionInfo CGInfo(*CS, ThreadIDVar, CodeGen,
1347                                         InnermostKind, HasCancel, Action);
1348   CodeGenFunction::CGCapturedStmtRAII CapInfoRAII(CGF, &CGInfo);
1349   llvm::Function *Res = CGF.GenerateCapturedStmtFunction(*CS);
1350   if (!Tied)
1351     NumberOfParts = Action.getNumberOfParts();
1352   return Res;
1353 }
1354 
1355 static void buildStructValue(ConstantStructBuilder &Fields, CodeGenModule &CGM,
1356                              const RecordDecl *RD, const CGRecordLayout &RL,
1357                              ArrayRef<llvm::Constant *> Data) {
1358   llvm::StructType *StructTy = RL.getLLVMType();
1359   unsigned PrevIdx = 0;
1360   ConstantInitBuilder CIBuilder(CGM);
1361   auto DI = Data.begin();
1362   for (const FieldDecl *FD : RD->fields()) {
1363     unsigned Idx = RL.getLLVMFieldNo(FD);
1364     // Fill the alignment.
1365     for (unsigned I = PrevIdx; I < Idx; ++I)
1366       Fields.add(llvm::Constant::getNullValue(StructTy->getElementType(I)));
1367     PrevIdx = Idx + 1;
1368     Fields.add(*DI);
1369     ++DI;
1370   }
1371 }
1372 
1373 template <class... As>
1374 static llvm::GlobalVariable *
1375 createGlobalStruct(CodeGenModule &CGM, QualType Ty, bool IsConstant,
1376                    ArrayRef<llvm::Constant *> Data, const Twine &Name,
1377                    As &&... Args) {
1378   const auto *RD = cast<RecordDecl>(Ty->getAsTagDecl());
1379   const CGRecordLayout &RL = CGM.getTypes().getCGRecordLayout(RD);
1380   ConstantInitBuilder CIBuilder(CGM);
1381   ConstantStructBuilder Fields = CIBuilder.beginStruct(RL.getLLVMType());
1382   buildStructValue(Fields, CGM, RD, RL, Data);
1383   return Fields.finishAndCreateGlobal(
1384       Name, CGM.getContext().getAlignOfGlobalVarInChars(Ty), IsConstant,
1385       std::forward<As>(Args)...);
1386 }
1387 
1388 template <typename T>
1389 static void
1390 createConstantGlobalStructAndAddToParent(CodeGenModule &CGM, QualType Ty,
1391                                          ArrayRef<llvm::Constant *> Data,
1392                                          T &Parent) {
1393   const auto *RD = cast<RecordDecl>(Ty->getAsTagDecl());
1394   const CGRecordLayout &RL = CGM.getTypes().getCGRecordLayout(RD);
1395   ConstantStructBuilder Fields = Parent.beginStruct(RL.getLLVMType());
1396   buildStructValue(Fields, CGM, RD, RL, Data);
1397   Fields.finishAndAddTo(Parent);
1398 }
1399 
1400 Address CGOpenMPRuntime::getOrCreateDefaultLocation(unsigned Flags) {
1401   CharUnits Align = CGM.getContext().getTypeAlignInChars(IdentQTy);
1402   unsigned Reserved2Flags = getDefaultLocationReserved2Flags();
1403   FlagsTy FlagsKey(Flags, Reserved2Flags);
1404   llvm::Value *Entry = OpenMPDefaultLocMap.lookup(FlagsKey);
1405   if (!Entry) {
1406     if (!DefaultOpenMPPSource) {
1407       // Initialize default location for psource field of ident_t structure of
1408       // all ident_t objects. Format is ";file;function;line;column;;".
1409       // Taken from
1410       // https://github.com/llvm/llvm-project/blob/master/openmp/runtime/src/kmp_str.cpp
1411       DefaultOpenMPPSource =
1412           CGM.GetAddrOfConstantCString(";unknown;unknown;0;0;;").getPointer();
1413       DefaultOpenMPPSource =
1414           llvm::ConstantExpr::getBitCast(DefaultOpenMPPSource, CGM.Int8PtrTy);
1415     }
1416 
1417     llvm::Constant *Data[] = {
1418         llvm::ConstantInt::getNullValue(CGM.Int32Ty),
1419         llvm::ConstantInt::get(CGM.Int32Ty, Flags),
1420         llvm::ConstantInt::get(CGM.Int32Ty, Reserved2Flags),
1421         llvm::ConstantInt::getNullValue(CGM.Int32Ty), DefaultOpenMPPSource};
1422     llvm::GlobalValue *DefaultOpenMPLocation =
1423         createGlobalStruct(CGM, IdentQTy, isDefaultLocationConstant(), Data, "",
1424                            llvm::GlobalValue::PrivateLinkage);
1425     DefaultOpenMPLocation->setUnnamedAddr(
1426         llvm::GlobalValue::UnnamedAddr::Global);
1427 
1428     OpenMPDefaultLocMap[FlagsKey] = Entry = DefaultOpenMPLocation;
1429   }
1430   return Address(Entry, Align);
1431 }
1432 
1433 void CGOpenMPRuntime::setLocThreadIdInsertPt(CodeGenFunction &CGF,
1434                                              bool AtCurrentPoint) {
1435   auto &Elem = OpenMPLocThreadIDMap.FindAndConstruct(CGF.CurFn);
1436   assert(!Elem.second.ServiceInsertPt && "Insert point is set already.");
1437 
1438   llvm::Value *Undef = llvm::UndefValue::get(CGF.Int32Ty);
1439   if (AtCurrentPoint) {
1440     Elem.second.ServiceInsertPt = new llvm::BitCastInst(
1441         Undef, CGF.Int32Ty, "svcpt", CGF.Builder.GetInsertBlock());
1442   } else {
1443     Elem.second.ServiceInsertPt =
1444         new llvm::BitCastInst(Undef, CGF.Int32Ty, "svcpt");
1445     Elem.second.ServiceInsertPt->insertAfter(CGF.AllocaInsertPt);
1446   }
1447 }
1448 
1449 void CGOpenMPRuntime::clearLocThreadIdInsertPt(CodeGenFunction &CGF) {
1450   auto &Elem = OpenMPLocThreadIDMap.FindAndConstruct(CGF.CurFn);
1451   if (Elem.second.ServiceInsertPt) {
1452     llvm::Instruction *Ptr = Elem.second.ServiceInsertPt;
1453     Elem.second.ServiceInsertPt = nullptr;
1454     Ptr->eraseFromParent();
1455   }
1456 }
1457 
1458 llvm::Value *CGOpenMPRuntime::emitUpdateLocation(CodeGenFunction &CGF,
1459                                                  SourceLocation Loc,
1460                                                  unsigned Flags) {
1461   Flags |= OMP_IDENT_KMPC;
1462   // If no debug info is generated - return global default location.
1463   if (CGM.getCodeGenOpts().getDebugInfo() == codegenoptions::NoDebugInfo ||
1464       Loc.isInvalid())
1465     return getOrCreateDefaultLocation(Flags).getPointer();
1466 
1467   assert(CGF.CurFn && "No function in current CodeGenFunction.");
1468 
1469   CharUnits Align = CGM.getContext().getTypeAlignInChars(IdentQTy);
1470   Address LocValue = Address::invalid();
1471   auto I = OpenMPLocThreadIDMap.find(CGF.CurFn);
1472   if (I != OpenMPLocThreadIDMap.end())
1473     LocValue = Address(I->second.DebugLoc, Align);
1474 
1475   // OpenMPLocThreadIDMap may have null DebugLoc and non-null ThreadID, if
1476   // GetOpenMPThreadID was called before this routine.
1477   if (!LocValue.isValid()) {
1478     // Generate "ident_t .kmpc_loc.addr;"
1479     Address AI = CGF.CreateMemTemp(IdentQTy, ".kmpc_loc.addr");
1480     auto &Elem = OpenMPLocThreadIDMap.FindAndConstruct(CGF.CurFn);
1481     Elem.second.DebugLoc = AI.getPointer();
1482     LocValue = AI;
1483 
1484     if (!Elem.second.ServiceInsertPt)
1485       setLocThreadIdInsertPt(CGF);
1486     CGBuilderTy::InsertPointGuard IPG(CGF.Builder);
1487     CGF.Builder.SetInsertPoint(Elem.second.ServiceInsertPt);
1488     CGF.Builder.CreateMemCpy(LocValue, getOrCreateDefaultLocation(Flags),
1489                              CGF.getTypeSize(IdentQTy));
1490   }
1491 
1492   // char **psource = &.kmpc_loc_<flags>.addr.psource;
1493   LValue Base = CGF.MakeAddrLValue(LocValue, IdentQTy);
1494   auto Fields = cast<RecordDecl>(IdentQTy->getAsTagDecl())->field_begin();
1495   LValue PSource =
1496       CGF.EmitLValueForField(Base, *std::next(Fields, IdentField_PSource));
1497 
1498   llvm::Value *OMPDebugLoc = OpenMPDebugLocMap.lookup(Loc.getRawEncoding());
1499   if (OMPDebugLoc == nullptr) {
1500     SmallString<128> Buffer2;
1501     llvm::raw_svector_ostream OS2(Buffer2);
1502     // Build debug location
1503     PresumedLoc PLoc = CGF.getContext().getSourceManager().getPresumedLoc(Loc);
1504     OS2 << ";" << PLoc.getFilename() << ";";
1505     if (const auto *FD = dyn_cast_or_null<FunctionDecl>(CGF.CurFuncDecl))
1506       OS2 << FD->getQualifiedNameAsString();
1507     OS2 << ";" << PLoc.getLine() << ";" << PLoc.getColumn() << ";;";
1508     OMPDebugLoc = CGF.Builder.CreateGlobalStringPtr(OS2.str());
1509     OpenMPDebugLocMap[Loc.getRawEncoding()] = OMPDebugLoc;
1510   }
1511   // *psource = ";<File>;<Function>;<Line>;<Column>;;";
1512   CGF.EmitStoreOfScalar(OMPDebugLoc, PSource);
1513 
1514   // Our callers always pass this to a runtime function, so for
1515   // convenience, go ahead and return a naked pointer.
1516   return LocValue.getPointer();
1517 }
1518 
1519 llvm::Value *CGOpenMPRuntime::getThreadID(CodeGenFunction &CGF,
1520                                           SourceLocation Loc) {
1521   assert(CGF.CurFn && "No function in current CodeGenFunction.");
1522 
1523   llvm::Value *ThreadID = nullptr;
1524   // Check whether we've already cached a load of the thread id in this
1525   // function.
1526   auto I = OpenMPLocThreadIDMap.find(CGF.CurFn);
1527   if (I != OpenMPLocThreadIDMap.end()) {
1528     ThreadID = I->second.ThreadID;
1529     if (ThreadID != nullptr)
1530       return ThreadID;
1531   }
1532   // If exceptions are enabled, do not use parameter to avoid possible crash.
1533   if (auto *OMPRegionInfo =
1534           dyn_cast_or_null<CGOpenMPRegionInfo>(CGF.CapturedStmtInfo)) {
1535     if (OMPRegionInfo->getThreadIDVariable()) {
1536       // Check if this an outlined function with thread id passed as argument.
1537       LValue LVal = OMPRegionInfo->getThreadIDVariableLValue(CGF);
1538       llvm::BasicBlock *TopBlock = CGF.AllocaInsertPt->getParent();
1539       if (!CGF.EHStack.requiresLandingPad() || !CGF.getLangOpts().Exceptions ||
1540           !CGF.getLangOpts().CXXExceptions ||
1541           CGF.Builder.GetInsertBlock() == TopBlock ||
1542           !isa<llvm::Instruction>(LVal.getPointer(CGF)) ||
1543           cast<llvm::Instruction>(LVal.getPointer(CGF))->getParent() ==
1544               TopBlock ||
1545           cast<llvm::Instruction>(LVal.getPointer(CGF))->getParent() ==
1546               CGF.Builder.GetInsertBlock()) {
1547         ThreadID = CGF.EmitLoadOfScalar(LVal, Loc);
1548         // If value loaded in entry block, cache it and use it everywhere in
1549         // function.
1550         if (CGF.Builder.GetInsertBlock() == TopBlock) {
1551           auto &Elem = OpenMPLocThreadIDMap.FindAndConstruct(CGF.CurFn);
1552           Elem.second.ThreadID = ThreadID;
1553         }
1554         return ThreadID;
1555       }
1556     }
1557   }
1558 
1559   // This is not an outlined function region - need to call __kmpc_int32
1560   // kmpc_global_thread_num(ident_t *loc).
1561   // Generate thread id value and cache this value for use across the
1562   // function.
1563   auto &Elem = OpenMPLocThreadIDMap.FindAndConstruct(CGF.CurFn);
1564   if (!Elem.second.ServiceInsertPt)
1565     setLocThreadIdInsertPt(CGF);
1566   CGBuilderTy::InsertPointGuard IPG(CGF.Builder);
1567   CGF.Builder.SetInsertPoint(Elem.second.ServiceInsertPt);
1568   llvm::CallInst *Call = CGF.Builder.CreateCall(
1569       OMPBuilder.getOrCreateRuntimeFunction(CGM.getModule(),
1570                                             OMPRTL___kmpc_global_thread_num),
1571       emitUpdateLocation(CGF, Loc));
1572   Call->setCallingConv(CGF.getRuntimeCC());
1573   Elem.second.ThreadID = Call;
1574   return Call;
1575 }
1576 
1577 void CGOpenMPRuntime::functionFinished(CodeGenFunction &CGF) {
1578   assert(CGF.CurFn && "No function in current CodeGenFunction.");
1579   if (OpenMPLocThreadIDMap.count(CGF.CurFn)) {
1580     clearLocThreadIdInsertPt(CGF);
1581     OpenMPLocThreadIDMap.erase(CGF.CurFn);
1582   }
1583   if (FunctionUDRMap.count(CGF.CurFn) > 0) {
1584     for(const auto *D : FunctionUDRMap[CGF.CurFn])
1585       UDRMap.erase(D);
1586     FunctionUDRMap.erase(CGF.CurFn);
1587   }
1588   auto I = FunctionUDMMap.find(CGF.CurFn);
1589   if (I != FunctionUDMMap.end()) {
1590     for(const auto *D : I->second)
1591       UDMMap.erase(D);
1592     FunctionUDMMap.erase(I);
1593   }
1594   LastprivateConditionalToTypes.erase(CGF.CurFn);
1595 }
1596 
1597 llvm::Type *CGOpenMPRuntime::getIdentTyPointerTy() {
1598   return IdentTy->getPointerTo();
1599 }
1600 
1601 llvm::Type *CGOpenMPRuntime::getKmpc_MicroPointerTy() {
1602   if (!Kmpc_MicroTy) {
1603     // Build void (*kmpc_micro)(kmp_int32 *global_tid, kmp_int32 *bound_tid,...)
1604     llvm::Type *MicroParams[] = {llvm::PointerType::getUnqual(CGM.Int32Ty),
1605                                  llvm::PointerType::getUnqual(CGM.Int32Ty)};
1606     Kmpc_MicroTy = llvm::FunctionType::get(CGM.VoidTy, MicroParams, true);
1607   }
1608   return llvm::PointerType::getUnqual(Kmpc_MicroTy);
1609 }
1610 
1611 llvm::FunctionCallee
1612 CGOpenMPRuntime::createForStaticInitFunction(unsigned IVSize, bool IVSigned) {
1613   assert((IVSize == 32 || IVSize == 64) &&
1614          "IV size is not compatible with the omp runtime");
1615   StringRef Name = IVSize == 32 ? (IVSigned ? "__kmpc_for_static_init_4"
1616                                             : "__kmpc_for_static_init_4u")
1617                                 : (IVSigned ? "__kmpc_for_static_init_8"
1618                                             : "__kmpc_for_static_init_8u");
1619   llvm::Type *ITy = IVSize == 32 ? CGM.Int32Ty : CGM.Int64Ty;
1620   auto *PtrTy = llvm::PointerType::getUnqual(ITy);
1621   llvm::Type *TypeParams[] = {
1622     getIdentTyPointerTy(),                     // loc
1623     CGM.Int32Ty,                               // tid
1624     CGM.Int32Ty,                               // schedtype
1625     llvm::PointerType::getUnqual(CGM.Int32Ty), // p_lastiter
1626     PtrTy,                                     // p_lower
1627     PtrTy,                                     // p_upper
1628     PtrTy,                                     // p_stride
1629     ITy,                                       // incr
1630     ITy                                        // chunk
1631   };
1632   auto *FnTy =
1633       llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false);
1634   return CGM.CreateRuntimeFunction(FnTy, Name);
1635 }
1636 
1637 llvm::FunctionCallee
1638 CGOpenMPRuntime::createDispatchInitFunction(unsigned IVSize, bool IVSigned) {
1639   assert((IVSize == 32 || IVSize == 64) &&
1640          "IV size is not compatible with the omp runtime");
1641   StringRef Name =
1642       IVSize == 32
1643           ? (IVSigned ? "__kmpc_dispatch_init_4" : "__kmpc_dispatch_init_4u")
1644           : (IVSigned ? "__kmpc_dispatch_init_8" : "__kmpc_dispatch_init_8u");
1645   llvm::Type *ITy = IVSize == 32 ? CGM.Int32Ty : CGM.Int64Ty;
1646   llvm::Type *TypeParams[] = { getIdentTyPointerTy(), // loc
1647                                CGM.Int32Ty,           // tid
1648                                CGM.Int32Ty,           // schedtype
1649                                ITy,                   // lower
1650                                ITy,                   // upper
1651                                ITy,                   // stride
1652                                ITy                    // chunk
1653   };
1654   auto *FnTy =
1655       llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false);
1656   return CGM.CreateRuntimeFunction(FnTy, Name);
1657 }
1658 
1659 llvm::FunctionCallee
1660 CGOpenMPRuntime::createDispatchFiniFunction(unsigned IVSize, bool IVSigned) {
1661   assert((IVSize == 32 || IVSize == 64) &&
1662          "IV size is not compatible with the omp runtime");
1663   StringRef Name =
1664       IVSize == 32
1665           ? (IVSigned ? "__kmpc_dispatch_fini_4" : "__kmpc_dispatch_fini_4u")
1666           : (IVSigned ? "__kmpc_dispatch_fini_8" : "__kmpc_dispatch_fini_8u");
1667   llvm::Type *TypeParams[] = {
1668       getIdentTyPointerTy(), // loc
1669       CGM.Int32Ty,           // tid
1670   };
1671   auto *FnTy =
1672       llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false);
1673   return CGM.CreateRuntimeFunction(FnTy, Name);
1674 }
1675 
1676 llvm::FunctionCallee
1677 CGOpenMPRuntime::createDispatchNextFunction(unsigned IVSize, bool IVSigned) {
1678   assert((IVSize == 32 || IVSize == 64) &&
1679          "IV size is not compatible with the omp runtime");
1680   StringRef Name =
1681       IVSize == 32
1682           ? (IVSigned ? "__kmpc_dispatch_next_4" : "__kmpc_dispatch_next_4u")
1683           : (IVSigned ? "__kmpc_dispatch_next_8" : "__kmpc_dispatch_next_8u");
1684   llvm::Type *ITy = IVSize == 32 ? CGM.Int32Ty : CGM.Int64Ty;
1685   auto *PtrTy = llvm::PointerType::getUnqual(ITy);
1686   llvm::Type *TypeParams[] = {
1687     getIdentTyPointerTy(),                     // loc
1688     CGM.Int32Ty,                               // tid
1689     llvm::PointerType::getUnqual(CGM.Int32Ty), // p_lastiter
1690     PtrTy,                                     // p_lower
1691     PtrTy,                                     // p_upper
1692     PtrTy                                      // p_stride
1693   };
1694   auto *FnTy =
1695       llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg*/ false);
1696   return CGM.CreateRuntimeFunction(FnTy, Name);
1697 }
1698 
1699 /// Obtain information that uniquely identifies a target entry. This
1700 /// consists of the file and device IDs as well as line number associated with
1701 /// the relevant entry source location.
1702 static void getTargetEntryUniqueInfo(ASTContext &C, SourceLocation Loc,
1703                                      unsigned &DeviceID, unsigned &FileID,
1704                                      unsigned &LineNum) {
1705   SourceManager &SM = C.getSourceManager();
1706 
1707   // The loc should be always valid and have a file ID (the user cannot use
1708   // #pragma directives in macros)
1709 
1710   assert(Loc.isValid() && "Source location is expected to be always valid.");
1711 
1712   PresumedLoc PLoc = SM.getPresumedLoc(Loc);
1713   assert(PLoc.isValid() && "Source location is expected to be always valid.");
1714 
1715   llvm::sys::fs::UniqueID ID;
1716   if (auto EC = llvm::sys::fs::getUniqueID(PLoc.getFilename(), ID))
1717     SM.getDiagnostics().Report(diag::err_cannot_open_file)
1718         << PLoc.getFilename() << EC.message();
1719 
1720   DeviceID = ID.getDevice();
1721   FileID = ID.getFile();
1722   LineNum = PLoc.getLine();
1723 }
1724 
1725 Address CGOpenMPRuntime::getAddrOfDeclareTargetVar(const VarDecl *VD) {
1726   if (CGM.getLangOpts().OpenMPSimd)
1727     return Address::invalid();
1728   llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res =
1729       OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD);
1730   if (Res && (*Res == OMPDeclareTargetDeclAttr::MT_Link ||
1731               (*Res == OMPDeclareTargetDeclAttr::MT_To &&
1732                HasRequiresUnifiedSharedMemory))) {
1733     SmallString<64> PtrName;
1734     {
1735       llvm::raw_svector_ostream OS(PtrName);
1736       OS << CGM.getMangledName(GlobalDecl(VD));
1737       if (!VD->isExternallyVisible()) {
1738         unsigned DeviceID, FileID, Line;
1739         getTargetEntryUniqueInfo(CGM.getContext(),
1740                                  VD->getCanonicalDecl()->getBeginLoc(),
1741                                  DeviceID, FileID, Line);
1742         OS << llvm::format("_%x", FileID);
1743       }
1744       OS << "_decl_tgt_ref_ptr";
1745     }
1746     llvm::Value *Ptr = CGM.getModule().getNamedValue(PtrName);
1747     if (!Ptr) {
1748       QualType PtrTy = CGM.getContext().getPointerType(VD->getType());
1749       Ptr = getOrCreateInternalVariable(CGM.getTypes().ConvertTypeForMem(PtrTy),
1750                                         PtrName);
1751 
1752       auto *GV = cast<llvm::GlobalVariable>(Ptr);
1753       GV->setLinkage(llvm::GlobalValue::WeakAnyLinkage);
1754 
1755       if (!CGM.getLangOpts().OpenMPIsDevice)
1756         GV->setInitializer(CGM.GetAddrOfGlobal(VD));
1757       registerTargetGlobalVariable(VD, cast<llvm::Constant>(Ptr));
1758     }
1759     return Address(Ptr, CGM.getContext().getDeclAlign(VD));
1760   }
1761   return Address::invalid();
1762 }
1763 
1764 llvm::Constant *
1765 CGOpenMPRuntime::getOrCreateThreadPrivateCache(const VarDecl *VD) {
1766   assert(!CGM.getLangOpts().OpenMPUseTLS ||
1767          !CGM.getContext().getTargetInfo().isTLSSupported());
1768   // Lookup the entry, lazily creating it if necessary.
1769   std::string Suffix = getName({"cache", ""});
1770   return getOrCreateInternalVariable(
1771       CGM.Int8PtrPtrTy, Twine(CGM.getMangledName(VD)).concat(Suffix));
1772 }
1773 
1774 Address CGOpenMPRuntime::getAddrOfThreadPrivate(CodeGenFunction &CGF,
1775                                                 const VarDecl *VD,
1776                                                 Address VDAddr,
1777                                                 SourceLocation Loc) {
1778   if (CGM.getLangOpts().OpenMPUseTLS &&
1779       CGM.getContext().getTargetInfo().isTLSSupported())
1780     return VDAddr;
1781 
1782   llvm::Type *VarTy = VDAddr.getElementType();
1783   llvm::Value *Args[] = {emitUpdateLocation(CGF, Loc), getThreadID(CGF, Loc),
1784                          CGF.Builder.CreatePointerCast(VDAddr.getPointer(),
1785                                                        CGM.Int8PtrTy),
1786                          CGM.getSize(CGM.GetTargetTypeStoreSize(VarTy)),
1787                          getOrCreateThreadPrivateCache(VD)};
1788   return Address(CGF.EmitRuntimeCall(
1789                      OMPBuilder.getOrCreateRuntimeFunction(
1790                          CGM.getModule(), OMPRTL___kmpc_threadprivate_cached),
1791                      Args),
1792                  VDAddr.getAlignment());
1793 }
1794 
1795 void CGOpenMPRuntime::emitThreadPrivateVarInit(
1796     CodeGenFunction &CGF, Address VDAddr, llvm::Value *Ctor,
1797     llvm::Value *CopyCtor, llvm::Value *Dtor, SourceLocation Loc) {
1798   // Call kmp_int32 __kmpc_global_thread_num(&loc) to init OpenMP runtime
1799   // library.
1800   llvm::Value *OMPLoc = emitUpdateLocation(CGF, Loc);
1801   CGF.EmitRuntimeCall(OMPBuilder.getOrCreateRuntimeFunction(
1802                           CGM.getModule(), OMPRTL___kmpc_global_thread_num),
1803                       OMPLoc);
1804   // Call __kmpc_threadprivate_register(&loc, &var, ctor, cctor/*NULL*/, dtor)
1805   // to register constructor/destructor for variable.
1806   llvm::Value *Args[] = {
1807       OMPLoc, CGF.Builder.CreatePointerCast(VDAddr.getPointer(), CGM.VoidPtrTy),
1808       Ctor, CopyCtor, Dtor};
1809   CGF.EmitRuntimeCall(
1810       OMPBuilder.getOrCreateRuntimeFunction(
1811           CGM.getModule(), OMPRTL___kmpc_threadprivate_register),
1812       Args);
1813 }
1814 
1815 llvm::Function *CGOpenMPRuntime::emitThreadPrivateVarDefinition(
1816     const VarDecl *VD, Address VDAddr, SourceLocation Loc,
1817     bool PerformInit, CodeGenFunction *CGF) {
1818   if (CGM.getLangOpts().OpenMPUseTLS &&
1819       CGM.getContext().getTargetInfo().isTLSSupported())
1820     return nullptr;
1821 
1822   VD = VD->getDefinition(CGM.getContext());
1823   if (VD && ThreadPrivateWithDefinition.insert(CGM.getMangledName(VD)).second) {
1824     QualType ASTTy = VD->getType();
1825 
1826     llvm::Value *Ctor = nullptr, *CopyCtor = nullptr, *Dtor = nullptr;
1827     const Expr *Init = VD->getAnyInitializer();
1828     if (CGM.getLangOpts().CPlusPlus && PerformInit) {
1829       // Generate function that re-emits the declaration's initializer into the
1830       // threadprivate copy of the variable VD
1831       CodeGenFunction CtorCGF(CGM);
1832       FunctionArgList Args;
1833       ImplicitParamDecl Dst(CGM.getContext(), /*DC=*/nullptr, Loc,
1834                             /*Id=*/nullptr, CGM.getContext().VoidPtrTy,
1835                             ImplicitParamDecl::Other);
1836       Args.push_back(&Dst);
1837 
1838       const auto &FI = CGM.getTypes().arrangeBuiltinFunctionDeclaration(
1839           CGM.getContext().VoidPtrTy, Args);
1840       llvm::FunctionType *FTy = CGM.getTypes().GetFunctionType(FI);
1841       std::string Name = getName({"__kmpc_global_ctor_", ""});
1842       llvm::Function *Fn =
1843           CGM.CreateGlobalInitOrCleanUpFunction(FTy, Name, FI, Loc);
1844       CtorCGF.StartFunction(GlobalDecl(), CGM.getContext().VoidPtrTy, Fn, FI,
1845                             Args, Loc, Loc);
1846       llvm::Value *ArgVal = CtorCGF.EmitLoadOfScalar(
1847           CtorCGF.GetAddrOfLocalVar(&Dst), /*Volatile=*/false,
1848           CGM.getContext().VoidPtrTy, Dst.getLocation());
1849       Address Arg = Address(ArgVal, VDAddr.getAlignment());
1850       Arg = CtorCGF.Builder.CreateElementBitCast(
1851           Arg, CtorCGF.ConvertTypeForMem(ASTTy));
1852       CtorCGF.EmitAnyExprToMem(Init, Arg, Init->getType().getQualifiers(),
1853                                /*IsInitializer=*/true);
1854       ArgVal = CtorCGF.EmitLoadOfScalar(
1855           CtorCGF.GetAddrOfLocalVar(&Dst), /*Volatile=*/false,
1856           CGM.getContext().VoidPtrTy, Dst.getLocation());
1857       CtorCGF.Builder.CreateStore(ArgVal, CtorCGF.ReturnValue);
1858       CtorCGF.FinishFunction();
1859       Ctor = Fn;
1860     }
1861     if (VD->getType().isDestructedType() != QualType::DK_none) {
1862       // Generate function that emits destructor call for the threadprivate copy
1863       // of the variable VD
1864       CodeGenFunction DtorCGF(CGM);
1865       FunctionArgList Args;
1866       ImplicitParamDecl Dst(CGM.getContext(), /*DC=*/nullptr, Loc,
1867                             /*Id=*/nullptr, CGM.getContext().VoidPtrTy,
1868                             ImplicitParamDecl::Other);
1869       Args.push_back(&Dst);
1870 
1871       const auto &FI = CGM.getTypes().arrangeBuiltinFunctionDeclaration(
1872           CGM.getContext().VoidTy, Args);
1873       llvm::FunctionType *FTy = CGM.getTypes().GetFunctionType(FI);
1874       std::string Name = getName({"__kmpc_global_dtor_", ""});
1875       llvm::Function *Fn =
1876           CGM.CreateGlobalInitOrCleanUpFunction(FTy, Name, FI, Loc);
1877       auto NL = ApplyDebugLocation::CreateEmpty(DtorCGF);
1878       DtorCGF.StartFunction(GlobalDecl(), CGM.getContext().VoidTy, Fn, FI, Args,
1879                             Loc, Loc);
1880       // Create a scope with an artificial location for the body of this function.
1881       auto AL = ApplyDebugLocation::CreateArtificial(DtorCGF);
1882       llvm::Value *ArgVal = DtorCGF.EmitLoadOfScalar(
1883           DtorCGF.GetAddrOfLocalVar(&Dst),
1884           /*Volatile=*/false, CGM.getContext().VoidPtrTy, Dst.getLocation());
1885       DtorCGF.emitDestroy(Address(ArgVal, VDAddr.getAlignment()), ASTTy,
1886                           DtorCGF.getDestroyer(ASTTy.isDestructedType()),
1887                           DtorCGF.needsEHCleanup(ASTTy.isDestructedType()));
1888       DtorCGF.FinishFunction();
1889       Dtor = Fn;
1890     }
1891     // Do not emit init function if it is not required.
1892     if (!Ctor && !Dtor)
1893       return nullptr;
1894 
1895     llvm::Type *CopyCtorTyArgs[] = {CGM.VoidPtrTy, CGM.VoidPtrTy};
1896     auto *CopyCtorTy = llvm::FunctionType::get(CGM.VoidPtrTy, CopyCtorTyArgs,
1897                                                /*isVarArg=*/false)
1898                            ->getPointerTo();
1899     // Copying constructor for the threadprivate variable.
1900     // Must be NULL - reserved by runtime, but currently it requires that this
1901     // parameter is always NULL. Otherwise it fires assertion.
1902     CopyCtor = llvm::Constant::getNullValue(CopyCtorTy);
1903     if (Ctor == nullptr) {
1904       auto *CtorTy = llvm::FunctionType::get(CGM.VoidPtrTy, CGM.VoidPtrTy,
1905                                              /*isVarArg=*/false)
1906                          ->getPointerTo();
1907       Ctor = llvm::Constant::getNullValue(CtorTy);
1908     }
1909     if (Dtor == nullptr) {
1910       auto *DtorTy = llvm::FunctionType::get(CGM.VoidTy, CGM.VoidPtrTy,
1911                                              /*isVarArg=*/false)
1912                          ->getPointerTo();
1913       Dtor = llvm::Constant::getNullValue(DtorTy);
1914     }
1915     if (!CGF) {
1916       auto *InitFunctionTy =
1917           llvm::FunctionType::get(CGM.VoidTy, /*isVarArg*/ false);
1918       std::string Name = getName({"__omp_threadprivate_init_", ""});
1919       llvm::Function *InitFunction = CGM.CreateGlobalInitOrCleanUpFunction(
1920           InitFunctionTy, Name, CGM.getTypes().arrangeNullaryFunction());
1921       CodeGenFunction InitCGF(CGM);
1922       FunctionArgList ArgList;
1923       InitCGF.StartFunction(GlobalDecl(), CGM.getContext().VoidTy, InitFunction,
1924                             CGM.getTypes().arrangeNullaryFunction(), ArgList,
1925                             Loc, Loc);
1926       emitThreadPrivateVarInit(InitCGF, VDAddr, Ctor, CopyCtor, Dtor, Loc);
1927       InitCGF.FinishFunction();
1928       return InitFunction;
1929     }
1930     emitThreadPrivateVarInit(*CGF, VDAddr, Ctor, CopyCtor, Dtor, Loc);
1931   }
1932   return nullptr;
1933 }
1934 
1935 bool CGOpenMPRuntime::emitDeclareTargetVarDefinition(const VarDecl *VD,
1936                                                      llvm::GlobalVariable *Addr,
1937                                                      bool PerformInit) {
1938   if (CGM.getLangOpts().OMPTargetTriples.empty() &&
1939       !CGM.getLangOpts().OpenMPIsDevice)
1940     return false;
1941   Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res =
1942       OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD);
1943   if (!Res || *Res == OMPDeclareTargetDeclAttr::MT_Link ||
1944       (*Res == OMPDeclareTargetDeclAttr::MT_To &&
1945        HasRequiresUnifiedSharedMemory))
1946     return CGM.getLangOpts().OpenMPIsDevice;
1947   VD = VD->getDefinition(CGM.getContext());
1948   assert(VD && "Unknown VarDecl");
1949 
1950   if (!DeclareTargetWithDefinition.insert(CGM.getMangledName(VD)).second)
1951     return CGM.getLangOpts().OpenMPIsDevice;
1952 
1953   QualType ASTTy = VD->getType();
1954   SourceLocation Loc = VD->getCanonicalDecl()->getBeginLoc();
1955 
1956   // Produce the unique prefix to identify the new target regions. We use
1957   // the source location of the variable declaration which we know to not
1958   // conflict with any target region.
1959   unsigned DeviceID;
1960   unsigned FileID;
1961   unsigned Line;
1962   getTargetEntryUniqueInfo(CGM.getContext(), Loc, DeviceID, FileID, Line);
1963   SmallString<128> Buffer, Out;
1964   {
1965     llvm::raw_svector_ostream OS(Buffer);
1966     OS << "__omp_offloading_" << llvm::format("_%x", DeviceID)
1967        << llvm::format("_%x_", FileID) << VD->getName() << "_l" << Line;
1968   }
1969 
1970   const Expr *Init = VD->getAnyInitializer();
1971   if (CGM.getLangOpts().CPlusPlus && PerformInit) {
1972     llvm::Constant *Ctor;
1973     llvm::Constant *ID;
1974     if (CGM.getLangOpts().OpenMPIsDevice) {
1975       // Generate function that re-emits the declaration's initializer into
1976       // the threadprivate copy of the variable VD
1977       CodeGenFunction CtorCGF(CGM);
1978 
1979       const CGFunctionInfo &FI = CGM.getTypes().arrangeNullaryFunction();
1980       llvm::FunctionType *FTy = CGM.getTypes().GetFunctionType(FI);
1981       llvm::Function *Fn = CGM.CreateGlobalInitOrCleanUpFunction(
1982           FTy, Twine(Buffer, "_ctor"), FI, Loc);
1983       auto NL = ApplyDebugLocation::CreateEmpty(CtorCGF);
1984       CtorCGF.StartFunction(GlobalDecl(), CGM.getContext().VoidTy, Fn, FI,
1985                             FunctionArgList(), Loc, Loc);
1986       auto AL = ApplyDebugLocation::CreateArtificial(CtorCGF);
1987       CtorCGF.EmitAnyExprToMem(Init,
1988                                Address(Addr, CGM.getContext().getDeclAlign(VD)),
1989                                Init->getType().getQualifiers(),
1990                                /*IsInitializer=*/true);
1991       CtorCGF.FinishFunction();
1992       Ctor = Fn;
1993       ID = llvm::ConstantExpr::getBitCast(Fn, CGM.Int8PtrTy);
1994       CGM.addUsedGlobal(cast<llvm::GlobalValue>(Ctor));
1995     } else {
1996       Ctor = new llvm::GlobalVariable(
1997           CGM.getModule(), CGM.Int8Ty, /*isConstant=*/true,
1998           llvm::GlobalValue::PrivateLinkage,
1999           llvm::Constant::getNullValue(CGM.Int8Ty), Twine(Buffer, "_ctor"));
2000       ID = Ctor;
2001     }
2002 
2003     // Register the information for the entry associated with the constructor.
2004     Out.clear();
2005     OffloadEntriesInfoManager.registerTargetRegionEntryInfo(
2006         DeviceID, FileID, Twine(Buffer, "_ctor").toStringRef(Out), Line, Ctor,
2007         ID, OffloadEntriesInfoManagerTy::OMPTargetRegionEntryCtor);
2008   }
2009   if (VD->getType().isDestructedType() != QualType::DK_none) {
2010     llvm::Constant *Dtor;
2011     llvm::Constant *ID;
2012     if (CGM.getLangOpts().OpenMPIsDevice) {
2013       // Generate function that emits destructor call for the threadprivate
2014       // copy of the variable VD
2015       CodeGenFunction DtorCGF(CGM);
2016 
2017       const CGFunctionInfo &FI = CGM.getTypes().arrangeNullaryFunction();
2018       llvm::FunctionType *FTy = CGM.getTypes().GetFunctionType(FI);
2019       llvm::Function *Fn = CGM.CreateGlobalInitOrCleanUpFunction(
2020           FTy, Twine(Buffer, "_dtor"), FI, Loc);
2021       auto NL = ApplyDebugLocation::CreateEmpty(DtorCGF);
2022       DtorCGF.StartFunction(GlobalDecl(), CGM.getContext().VoidTy, Fn, FI,
2023                             FunctionArgList(), Loc, Loc);
2024       // Create a scope with an artificial location for the body of this
2025       // function.
2026       auto AL = ApplyDebugLocation::CreateArtificial(DtorCGF);
2027       DtorCGF.emitDestroy(Address(Addr, CGM.getContext().getDeclAlign(VD)),
2028                           ASTTy, DtorCGF.getDestroyer(ASTTy.isDestructedType()),
2029                           DtorCGF.needsEHCleanup(ASTTy.isDestructedType()));
2030       DtorCGF.FinishFunction();
2031       Dtor = Fn;
2032       ID = llvm::ConstantExpr::getBitCast(Fn, CGM.Int8PtrTy);
2033       CGM.addUsedGlobal(cast<llvm::GlobalValue>(Dtor));
2034     } else {
2035       Dtor = new llvm::GlobalVariable(
2036           CGM.getModule(), CGM.Int8Ty, /*isConstant=*/true,
2037           llvm::GlobalValue::PrivateLinkage,
2038           llvm::Constant::getNullValue(CGM.Int8Ty), Twine(Buffer, "_dtor"));
2039       ID = Dtor;
2040     }
2041     // Register the information for the entry associated with the destructor.
2042     Out.clear();
2043     OffloadEntriesInfoManager.registerTargetRegionEntryInfo(
2044         DeviceID, FileID, Twine(Buffer, "_dtor").toStringRef(Out), Line, Dtor,
2045         ID, OffloadEntriesInfoManagerTy::OMPTargetRegionEntryDtor);
2046   }
2047   return CGM.getLangOpts().OpenMPIsDevice;
2048 }
2049 
2050 Address CGOpenMPRuntime::getAddrOfArtificialThreadPrivate(CodeGenFunction &CGF,
2051                                                           QualType VarType,
2052                                                           StringRef Name) {
2053   std::string Suffix = getName({"artificial", ""});
2054   llvm::Type *VarLVType = CGF.ConvertTypeForMem(VarType);
2055   llvm::Value *GAddr =
2056       getOrCreateInternalVariable(VarLVType, Twine(Name).concat(Suffix));
2057   if (CGM.getLangOpts().OpenMP && CGM.getLangOpts().OpenMPUseTLS &&
2058       CGM.getTarget().isTLSSupported()) {
2059     cast<llvm::GlobalVariable>(GAddr)->setThreadLocal(/*Val=*/true);
2060     return Address(GAddr, CGM.getContext().getTypeAlignInChars(VarType));
2061   }
2062   std::string CacheSuffix = getName({"cache", ""});
2063   llvm::Value *Args[] = {
2064       emitUpdateLocation(CGF, SourceLocation()),
2065       getThreadID(CGF, SourceLocation()),
2066       CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(GAddr, CGM.VoidPtrTy),
2067       CGF.Builder.CreateIntCast(CGF.getTypeSize(VarType), CGM.SizeTy,
2068                                 /*isSigned=*/false),
2069       getOrCreateInternalVariable(
2070           CGM.VoidPtrPtrTy, Twine(Name).concat(Suffix).concat(CacheSuffix))};
2071   return Address(
2072       CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(
2073           CGF.EmitRuntimeCall(
2074               OMPBuilder.getOrCreateRuntimeFunction(
2075                   CGM.getModule(), OMPRTL___kmpc_threadprivate_cached),
2076               Args),
2077           VarLVType->getPointerTo(/*AddrSpace=*/0)),
2078       CGM.getContext().getTypeAlignInChars(VarType));
2079 }
2080 
2081 void CGOpenMPRuntime::emitIfClause(CodeGenFunction &CGF, const Expr *Cond,
2082                                    const RegionCodeGenTy &ThenGen,
2083                                    const RegionCodeGenTy &ElseGen) {
2084   CodeGenFunction::LexicalScope ConditionScope(CGF, Cond->getSourceRange());
2085 
2086   // If the condition constant folds and can be elided, try to avoid emitting
2087   // the condition and the dead arm of the if/else.
2088   bool CondConstant;
2089   if (CGF.ConstantFoldsToSimpleInteger(Cond, CondConstant)) {
2090     if (CondConstant)
2091       ThenGen(CGF);
2092     else
2093       ElseGen(CGF);
2094     return;
2095   }
2096 
2097   // Otherwise, the condition did not fold, or we couldn't elide it.  Just
2098   // emit the conditional branch.
2099   llvm::BasicBlock *ThenBlock = CGF.createBasicBlock("omp_if.then");
2100   llvm::BasicBlock *ElseBlock = CGF.createBasicBlock("omp_if.else");
2101   llvm::BasicBlock *ContBlock = CGF.createBasicBlock("omp_if.end");
2102   CGF.EmitBranchOnBoolExpr(Cond, ThenBlock, ElseBlock, /*TrueCount=*/0);
2103 
2104   // Emit the 'then' code.
2105   CGF.EmitBlock(ThenBlock);
2106   ThenGen(CGF);
2107   CGF.EmitBranch(ContBlock);
2108   // Emit the 'else' code if present.
2109   // There is no need to emit line number for unconditional branch.
2110   (void)ApplyDebugLocation::CreateEmpty(CGF);
2111   CGF.EmitBlock(ElseBlock);
2112   ElseGen(CGF);
2113   // There is no need to emit line number for unconditional branch.
2114   (void)ApplyDebugLocation::CreateEmpty(CGF);
2115   CGF.EmitBranch(ContBlock);
2116   // Emit the continuation block for code after the if.
2117   CGF.EmitBlock(ContBlock, /*IsFinished=*/true);
2118 }
2119 
2120 void CGOpenMPRuntime::emitParallelCall(CodeGenFunction &CGF, SourceLocation Loc,
2121                                        llvm::Function *OutlinedFn,
2122                                        ArrayRef<llvm::Value *> CapturedVars,
2123                                        const Expr *IfCond) {
2124   if (!CGF.HaveInsertPoint())
2125     return;
2126   llvm::Value *RTLoc = emitUpdateLocation(CGF, Loc);
2127   auto &M = CGM.getModule();
2128   auto &&ThenGen = [&M, OutlinedFn, CapturedVars, RTLoc,
2129                     this](CodeGenFunction &CGF, PrePostActionTy &) {
2130     // Build call __kmpc_fork_call(loc, n, microtask, var1, .., varn);
2131     CGOpenMPRuntime &RT = CGF.CGM.getOpenMPRuntime();
2132     llvm::Value *Args[] = {
2133         RTLoc,
2134         CGF.Builder.getInt32(CapturedVars.size()), // Number of captured vars
2135         CGF.Builder.CreateBitCast(OutlinedFn, RT.getKmpc_MicroPointerTy())};
2136     llvm::SmallVector<llvm::Value *, 16> RealArgs;
2137     RealArgs.append(std::begin(Args), std::end(Args));
2138     RealArgs.append(CapturedVars.begin(), CapturedVars.end());
2139 
2140     llvm::FunctionCallee RTLFn =
2141         OMPBuilder.getOrCreateRuntimeFunction(M, OMPRTL___kmpc_fork_call);
2142     CGF.EmitRuntimeCall(RTLFn, RealArgs);
2143   };
2144   auto &&ElseGen = [&M, OutlinedFn, CapturedVars, RTLoc, Loc,
2145                     this](CodeGenFunction &CGF, PrePostActionTy &) {
2146     CGOpenMPRuntime &RT = CGF.CGM.getOpenMPRuntime();
2147     llvm::Value *ThreadID = RT.getThreadID(CGF, Loc);
2148     // Build calls:
2149     // __kmpc_serialized_parallel(&Loc, GTid);
2150     llvm::Value *Args[] = {RTLoc, ThreadID};
2151     CGF.EmitRuntimeCall(OMPBuilder.getOrCreateRuntimeFunction(
2152                             M, OMPRTL___kmpc_serialized_parallel),
2153                         Args);
2154 
2155     // OutlinedFn(&GTid, &zero_bound, CapturedStruct);
2156     Address ThreadIDAddr = RT.emitThreadIDAddress(CGF, Loc);
2157     Address ZeroAddrBound =
2158         CGF.CreateDefaultAlignTempAlloca(CGF.Int32Ty,
2159                                          /*Name=*/".bound.zero.addr");
2160     CGF.InitTempAlloca(ZeroAddrBound, CGF.Builder.getInt32(/*C*/ 0));
2161     llvm::SmallVector<llvm::Value *, 16> OutlinedFnArgs;
2162     // ThreadId for serialized parallels is 0.
2163     OutlinedFnArgs.push_back(ThreadIDAddr.getPointer());
2164     OutlinedFnArgs.push_back(ZeroAddrBound.getPointer());
2165     OutlinedFnArgs.append(CapturedVars.begin(), CapturedVars.end());
2166     RT.emitOutlinedFunctionCall(CGF, Loc, OutlinedFn, OutlinedFnArgs);
2167 
2168     // __kmpc_end_serialized_parallel(&Loc, GTid);
2169     llvm::Value *EndArgs[] = {RT.emitUpdateLocation(CGF, Loc), ThreadID};
2170     CGF.EmitRuntimeCall(OMPBuilder.getOrCreateRuntimeFunction(
2171                             M, OMPRTL___kmpc_end_serialized_parallel),
2172                         EndArgs);
2173   };
2174   if (IfCond) {
2175     emitIfClause(CGF, IfCond, ThenGen, ElseGen);
2176   } else {
2177     RegionCodeGenTy ThenRCG(ThenGen);
2178     ThenRCG(CGF);
2179   }
2180 }
2181 
2182 // If we're inside an (outlined) parallel region, use the region info's
2183 // thread-ID variable (it is passed in a first argument of the outlined function
2184 // as "kmp_int32 *gtid"). Otherwise, if we're not inside parallel region, but in
2185 // regular serial code region, get thread ID by calling kmp_int32
2186 // kmpc_global_thread_num(ident_t *loc), stash this thread ID in a temporary and
2187 // return the address of that temp.
2188 Address CGOpenMPRuntime::emitThreadIDAddress(CodeGenFunction &CGF,
2189                                              SourceLocation Loc) {
2190   if (auto *OMPRegionInfo =
2191           dyn_cast_or_null<CGOpenMPRegionInfo>(CGF.CapturedStmtInfo))
2192     if (OMPRegionInfo->getThreadIDVariable())
2193       return OMPRegionInfo->getThreadIDVariableLValue(CGF).getAddress(CGF);
2194 
2195   llvm::Value *ThreadID = getThreadID(CGF, Loc);
2196   QualType Int32Ty =
2197       CGF.getContext().getIntTypeForBitwidth(/*DestWidth*/ 32, /*Signed*/ true);
2198   Address ThreadIDTemp = CGF.CreateMemTemp(Int32Ty, /*Name*/ ".threadid_temp.");
2199   CGF.EmitStoreOfScalar(ThreadID,
2200                         CGF.MakeAddrLValue(ThreadIDTemp, Int32Ty));
2201 
2202   return ThreadIDTemp;
2203 }
2204 
2205 llvm::Constant *CGOpenMPRuntime::getOrCreateInternalVariable(
2206     llvm::Type *Ty, const llvm::Twine &Name, unsigned AddressSpace) {
2207   SmallString<256> Buffer;
2208   llvm::raw_svector_ostream Out(Buffer);
2209   Out << Name;
2210   StringRef RuntimeName = Out.str();
2211   auto &Elem = *InternalVars.try_emplace(RuntimeName, nullptr).first;
2212   if (Elem.second) {
2213     assert(Elem.second->getType()->getPointerElementType() == Ty &&
2214            "OMP internal variable has different type than requested");
2215     return &*Elem.second;
2216   }
2217 
2218   return Elem.second = new llvm::GlobalVariable(
2219              CGM.getModule(), Ty, /*IsConstant*/ false,
2220              llvm::GlobalValue::CommonLinkage, llvm::Constant::getNullValue(Ty),
2221              Elem.first(), /*InsertBefore=*/nullptr,
2222              llvm::GlobalValue::NotThreadLocal, AddressSpace);
2223 }
2224 
2225 llvm::Value *CGOpenMPRuntime::getCriticalRegionLock(StringRef CriticalName) {
2226   std::string Prefix = Twine("gomp_critical_user_", CriticalName).str();
2227   std::string Name = getName({Prefix, "var"});
2228   return getOrCreateInternalVariable(KmpCriticalNameTy, Name);
2229 }
2230 
2231 namespace {
2232 /// Common pre(post)-action for different OpenMP constructs.
2233 class CommonActionTy final : public PrePostActionTy {
2234   llvm::FunctionCallee EnterCallee;
2235   ArrayRef<llvm::Value *> EnterArgs;
2236   llvm::FunctionCallee ExitCallee;
2237   ArrayRef<llvm::Value *> ExitArgs;
2238   bool Conditional;
2239   llvm::BasicBlock *ContBlock = nullptr;
2240 
2241 public:
2242   CommonActionTy(llvm::FunctionCallee EnterCallee,
2243                  ArrayRef<llvm::Value *> EnterArgs,
2244                  llvm::FunctionCallee ExitCallee,
2245                  ArrayRef<llvm::Value *> ExitArgs, bool Conditional = false)
2246       : EnterCallee(EnterCallee), EnterArgs(EnterArgs), ExitCallee(ExitCallee),
2247         ExitArgs(ExitArgs), Conditional(Conditional) {}
2248   void Enter(CodeGenFunction &CGF) override {
2249     llvm::Value *EnterRes = CGF.EmitRuntimeCall(EnterCallee, EnterArgs);
2250     if (Conditional) {
2251       llvm::Value *CallBool = CGF.Builder.CreateIsNotNull(EnterRes);
2252       auto *ThenBlock = CGF.createBasicBlock("omp_if.then");
2253       ContBlock = CGF.createBasicBlock("omp_if.end");
2254       // Generate the branch (If-stmt)
2255       CGF.Builder.CreateCondBr(CallBool, ThenBlock, ContBlock);
2256       CGF.EmitBlock(ThenBlock);
2257     }
2258   }
2259   void Done(CodeGenFunction &CGF) {
2260     // Emit the rest of blocks/branches
2261     CGF.EmitBranch(ContBlock);
2262     CGF.EmitBlock(ContBlock, true);
2263   }
2264   void Exit(CodeGenFunction &CGF) override {
2265     CGF.EmitRuntimeCall(ExitCallee, ExitArgs);
2266   }
2267 };
2268 } // anonymous namespace
2269 
2270 void CGOpenMPRuntime::emitCriticalRegion(CodeGenFunction &CGF,
2271                                          StringRef CriticalName,
2272                                          const RegionCodeGenTy &CriticalOpGen,
2273                                          SourceLocation Loc, const Expr *Hint) {
2274   // __kmpc_critical[_with_hint](ident_t *, gtid, Lock[, hint]);
2275   // CriticalOpGen();
2276   // __kmpc_end_critical(ident_t *, gtid, Lock);
2277   // Prepare arguments and build a call to __kmpc_critical
2278   if (!CGF.HaveInsertPoint())
2279     return;
2280   llvm::Value *Args[] = {emitUpdateLocation(CGF, Loc), getThreadID(CGF, Loc),
2281                          getCriticalRegionLock(CriticalName)};
2282   llvm::SmallVector<llvm::Value *, 4> EnterArgs(std::begin(Args),
2283                                                 std::end(Args));
2284   if (Hint) {
2285     EnterArgs.push_back(CGF.Builder.CreateIntCast(
2286         CGF.EmitScalarExpr(Hint), CGM.Int32Ty, /*isSigned=*/false));
2287   }
2288   CommonActionTy Action(
2289       OMPBuilder.getOrCreateRuntimeFunction(
2290           CGM.getModule(),
2291           Hint ? OMPRTL___kmpc_critical_with_hint : OMPRTL___kmpc_critical),
2292       EnterArgs,
2293       OMPBuilder.getOrCreateRuntimeFunction(CGM.getModule(),
2294                                             OMPRTL___kmpc_end_critical),
2295       Args);
2296   CriticalOpGen.setAction(Action);
2297   emitInlinedDirective(CGF, OMPD_critical, CriticalOpGen);
2298 }
2299 
2300 void CGOpenMPRuntime::emitMasterRegion(CodeGenFunction &CGF,
2301                                        const RegionCodeGenTy &MasterOpGen,
2302                                        SourceLocation Loc) {
2303   if (!CGF.HaveInsertPoint())
2304     return;
2305   // if(__kmpc_master(ident_t *, gtid)) {
2306   //   MasterOpGen();
2307   //   __kmpc_end_master(ident_t *, gtid);
2308   // }
2309   // Prepare arguments and build a call to __kmpc_master
2310   llvm::Value *Args[] = {emitUpdateLocation(CGF, Loc), getThreadID(CGF, Loc)};
2311   CommonActionTy Action(OMPBuilder.getOrCreateRuntimeFunction(
2312                             CGM.getModule(), OMPRTL___kmpc_master),
2313                         Args,
2314                         OMPBuilder.getOrCreateRuntimeFunction(
2315                             CGM.getModule(), OMPRTL___kmpc_end_master),
2316                         Args,
2317                         /*Conditional=*/true);
2318   MasterOpGen.setAction(Action);
2319   emitInlinedDirective(CGF, OMPD_master, MasterOpGen);
2320   Action.Done(CGF);
2321 }
2322 
2323 void CGOpenMPRuntime::emitTaskyieldCall(CodeGenFunction &CGF,
2324                                         SourceLocation Loc) {
2325   if (!CGF.HaveInsertPoint())
2326     return;
2327   if (CGF.CGM.getLangOpts().OpenMPIRBuilder) {
2328     OMPBuilder.CreateTaskyield(CGF.Builder);
2329   } else {
2330     // Build call __kmpc_omp_taskyield(loc, thread_id, 0);
2331     llvm::Value *Args[] = {
2332         emitUpdateLocation(CGF, Loc), getThreadID(CGF, Loc),
2333         llvm::ConstantInt::get(CGM.IntTy, /*V=*/0, /*isSigned=*/true)};
2334     CGF.EmitRuntimeCall(OMPBuilder.getOrCreateRuntimeFunction(
2335                             CGM.getModule(), OMPRTL___kmpc_omp_taskyield),
2336                         Args);
2337   }
2338 
2339   if (auto *Region = dyn_cast_or_null<CGOpenMPRegionInfo>(CGF.CapturedStmtInfo))
2340     Region->emitUntiedSwitch(CGF);
2341 }
2342 
2343 void CGOpenMPRuntime::emitTaskgroupRegion(CodeGenFunction &CGF,
2344                                           const RegionCodeGenTy &TaskgroupOpGen,
2345                                           SourceLocation Loc) {
2346   if (!CGF.HaveInsertPoint())
2347     return;
2348   // __kmpc_taskgroup(ident_t *, gtid);
2349   // TaskgroupOpGen();
2350   // __kmpc_end_taskgroup(ident_t *, gtid);
2351   // Prepare arguments and build a call to __kmpc_taskgroup
2352   llvm::Value *Args[] = {emitUpdateLocation(CGF, Loc), getThreadID(CGF, Loc)};
2353   CommonActionTy Action(OMPBuilder.getOrCreateRuntimeFunction(
2354                             CGM.getModule(), OMPRTL___kmpc_taskgroup),
2355                         Args,
2356                         OMPBuilder.getOrCreateRuntimeFunction(
2357                             CGM.getModule(), OMPRTL___kmpc_end_taskgroup),
2358                         Args);
2359   TaskgroupOpGen.setAction(Action);
2360   emitInlinedDirective(CGF, OMPD_taskgroup, TaskgroupOpGen);
2361 }
2362 
2363 /// Given an array of pointers to variables, project the address of a
2364 /// given variable.
2365 static Address emitAddrOfVarFromArray(CodeGenFunction &CGF, Address Array,
2366                                       unsigned Index, const VarDecl *Var) {
2367   // Pull out the pointer to the variable.
2368   Address PtrAddr = CGF.Builder.CreateConstArrayGEP(Array, Index);
2369   llvm::Value *Ptr = CGF.Builder.CreateLoad(PtrAddr);
2370 
2371   Address Addr = Address(Ptr, CGF.getContext().getDeclAlign(Var));
2372   Addr = CGF.Builder.CreateElementBitCast(
2373       Addr, CGF.ConvertTypeForMem(Var->getType()));
2374   return Addr;
2375 }
2376 
2377 static llvm::Value *emitCopyprivateCopyFunction(
2378     CodeGenModule &CGM, llvm::Type *ArgsType,
2379     ArrayRef<const Expr *> CopyprivateVars, ArrayRef<const Expr *> DestExprs,
2380     ArrayRef<const Expr *> SrcExprs, ArrayRef<const Expr *> AssignmentOps,
2381     SourceLocation Loc) {
2382   ASTContext &C = CGM.getContext();
2383   // void copy_func(void *LHSArg, void *RHSArg);
2384   FunctionArgList Args;
2385   ImplicitParamDecl LHSArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, C.VoidPtrTy,
2386                            ImplicitParamDecl::Other);
2387   ImplicitParamDecl RHSArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, C.VoidPtrTy,
2388                            ImplicitParamDecl::Other);
2389   Args.push_back(&LHSArg);
2390   Args.push_back(&RHSArg);
2391   const auto &CGFI =
2392       CGM.getTypes().arrangeBuiltinFunctionDeclaration(C.VoidTy, Args);
2393   std::string Name =
2394       CGM.getOpenMPRuntime().getName({"omp", "copyprivate", "copy_func"});
2395   auto *Fn = llvm::Function::Create(CGM.getTypes().GetFunctionType(CGFI),
2396                                     llvm::GlobalValue::InternalLinkage, Name,
2397                                     &CGM.getModule());
2398   CGM.SetInternalFunctionAttributes(GlobalDecl(), Fn, CGFI);
2399   Fn->setDoesNotRecurse();
2400   CodeGenFunction CGF(CGM);
2401   CGF.StartFunction(GlobalDecl(), C.VoidTy, Fn, CGFI, Args, Loc, Loc);
2402   // Dest = (void*[n])(LHSArg);
2403   // Src = (void*[n])(RHSArg);
2404   Address LHS(CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(
2405       CGF.Builder.CreateLoad(CGF.GetAddrOfLocalVar(&LHSArg)),
2406       ArgsType), CGF.getPointerAlign());
2407   Address RHS(CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(
2408       CGF.Builder.CreateLoad(CGF.GetAddrOfLocalVar(&RHSArg)),
2409       ArgsType), CGF.getPointerAlign());
2410   // *(Type0*)Dst[0] = *(Type0*)Src[0];
2411   // *(Type1*)Dst[1] = *(Type1*)Src[1];
2412   // ...
2413   // *(Typen*)Dst[n] = *(Typen*)Src[n];
2414   for (unsigned I = 0, E = AssignmentOps.size(); I < E; ++I) {
2415     const auto *DestVar =
2416         cast<VarDecl>(cast<DeclRefExpr>(DestExprs[I])->getDecl());
2417     Address DestAddr = emitAddrOfVarFromArray(CGF, LHS, I, DestVar);
2418 
2419     const auto *SrcVar =
2420         cast<VarDecl>(cast<DeclRefExpr>(SrcExprs[I])->getDecl());
2421     Address SrcAddr = emitAddrOfVarFromArray(CGF, RHS, I, SrcVar);
2422 
2423     const auto *VD = cast<DeclRefExpr>(CopyprivateVars[I])->getDecl();
2424     QualType Type = VD->getType();
2425     CGF.EmitOMPCopy(Type, DestAddr, SrcAddr, DestVar, SrcVar, AssignmentOps[I]);
2426   }
2427   CGF.FinishFunction();
2428   return Fn;
2429 }
2430 
2431 void CGOpenMPRuntime::emitSingleRegion(CodeGenFunction &CGF,
2432                                        const RegionCodeGenTy &SingleOpGen,
2433                                        SourceLocation Loc,
2434                                        ArrayRef<const Expr *> CopyprivateVars,
2435                                        ArrayRef<const Expr *> SrcExprs,
2436                                        ArrayRef<const Expr *> DstExprs,
2437                                        ArrayRef<const Expr *> AssignmentOps) {
2438   if (!CGF.HaveInsertPoint())
2439     return;
2440   assert(CopyprivateVars.size() == SrcExprs.size() &&
2441          CopyprivateVars.size() == DstExprs.size() &&
2442          CopyprivateVars.size() == AssignmentOps.size());
2443   ASTContext &C = CGM.getContext();
2444   // int32 did_it = 0;
2445   // if(__kmpc_single(ident_t *, gtid)) {
2446   //   SingleOpGen();
2447   //   __kmpc_end_single(ident_t *, gtid);
2448   //   did_it = 1;
2449   // }
2450   // call __kmpc_copyprivate(ident_t *, gtid, <buf_size>, <copyprivate list>,
2451   // <copy_func>, did_it);
2452 
2453   Address DidIt = Address::invalid();
2454   if (!CopyprivateVars.empty()) {
2455     // int32 did_it = 0;
2456     QualType KmpInt32Ty =
2457         C.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1);
2458     DidIt = CGF.CreateMemTemp(KmpInt32Ty, ".omp.copyprivate.did_it");
2459     CGF.Builder.CreateStore(CGF.Builder.getInt32(0), DidIt);
2460   }
2461   // Prepare arguments and build a call to __kmpc_single
2462   llvm::Value *Args[] = {emitUpdateLocation(CGF, Loc), getThreadID(CGF, Loc)};
2463   CommonActionTy Action(OMPBuilder.getOrCreateRuntimeFunction(
2464                             CGM.getModule(), OMPRTL___kmpc_single),
2465                         Args,
2466                         OMPBuilder.getOrCreateRuntimeFunction(
2467                             CGM.getModule(), OMPRTL___kmpc_end_single),
2468                         Args,
2469                         /*Conditional=*/true);
2470   SingleOpGen.setAction(Action);
2471   emitInlinedDirective(CGF, OMPD_single, SingleOpGen);
2472   if (DidIt.isValid()) {
2473     // did_it = 1;
2474     CGF.Builder.CreateStore(CGF.Builder.getInt32(1), DidIt);
2475   }
2476   Action.Done(CGF);
2477   // call __kmpc_copyprivate(ident_t *, gtid, <buf_size>, <copyprivate list>,
2478   // <copy_func>, did_it);
2479   if (DidIt.isValid()) {
2480     llvm::APInt ArraySize(/*unsigned int numBits=*/32, CopyprivateVars.size());
2481     QualType CopyprivateArrayTy = C.getConstantArrayType(
2482         C.VoidPtrTy, ArraySize, nullptr, ArrayType::Normal,
2483         /*IndexTypeQuals=*/0);
2484     // Create a list of all private variables for copyprivate.
2485     Address CopyprivateList =
2486         CGF.CreateMemTemp(CopyprivateArrayTy, ".omp.copyprivate.cpr_list");
2487     for (unsigned I = 0, E = CopyprivateVars.size(); I < E; ++I) {
2488       Address Elem = CGF.Builder.CreateConstArrayGEP(CopyprivateList, I);
2489       CGF.Builder.CreateStore(
2490           CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(
2491               CGF.EmitLValue(CopyprivateVars[I]).getPointer(CGF),
2492               CGF.VoidPtrTy),
2493           Elem);
2494     }
2495     // Build function that copies private values from single region to all other
2496     // threads in the corresponding parallel region.
2497     llvm::Value *CpyFn = emitCopyprivateCopyFunction(
2498         CGM, CGF.ConvertTypeForMem(CopyprivateArrayTy)->getPointerTo(),
2499         CopyprivateVars, SrcExprs, DstExprs, AssignmentOps, Loc);
2500     llvm::Value *BufSize = CGF.getTypeSize(CopyprivateArrayTy);
2501     Address CL =
2502       CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(CopyprivateList,
2503                                                       CGF.VoidPtrTy);
2504     llvm::Value *DidItVal = CGF.Builder.CreateLoad(DidIt);
2505     llvm::Value *Args[] = {
2506         emitUpdateLocation(CGF, Loc), // ident_t *<loc>
2507         getThreadID(CGF, Loc),        // i32 <gtid>
2508         BufSize,                      // size_t <buf_size>
2509         CL.getPointer(),              // void *<copyprivate list>
2510         CpyFn,                        // void (*) (void *, void *) <copy_func>
2511         DidItVal                      // i32 did_it
2512     };
2513     CGF.EmitRuntimeCall(OMPBuilder.getOrCreateRuntimeFunction(
2514                             CGM.getModule(), OMPRTL___kmpc_copyprivate),
2515                         Args);
2516   }
2517 }
2518 
2519 void CGOpenMPRuntime::emitOrderedRegion(CodeGenFunction &CGF,
2520                                         const RegionCodeGenTy &OrderedOpGen,
2521                                         SourceLocation Loc, bool IsThreads) {
2522   if (!CGF.HaveInsertPoint())
2523     return;
2524   // __kmpc_ordered(ident_t *, gtid);
2525   // OrderedOpGen();
2526   // __kmpc_end_ordered(ident_t *, gtid);
2527   // Prepare arguments and build a call to __kmpc_ordered
2528   if (IsThreads) {
2529     llvm::Value *Args[] = {emitUpdateLocation(CGF, Loc), getThreadID(CGF, Loc)};
2530     CommonActionTy Action(OMPBuilder.getOrCreateRuntimeFunction(
2531                               CGM.getModule(), OMPRTL___kmpc_ordered),
2532                           Args,
2533                           OMPBuilder.getOrCreateRuntimeFunction(
2534                               CGM.getModule(), OMPRTL___kmpc_end_ordered),
2535                           Args);
2536     OrderedOpGen.setAction(Action);
2537     emitInlinedDirective(CGF, OMPD_ordered, OrderedOpGen);
2538     return;
2539   }
2540   emitInlinedDirective(CGF, OMPD_ordered, OrderedOpGen);
2541 }
2542 
2543 unsigned CGOpenMPRuntime::getDefaultFlagsForBarriers(OpenMPDirectiveKind Kind) {
2544   unsigned Flags;
2545   if (Kind == OMPD_for)
2546     Flags = OMP_IDENT_BARRIER_IMPL_FOR;
2547   else if (Kind == OMPD_sections)
2548     Flags = OMP_IDENT_BARRIER_IMPL_SECTIONS;
2549   else if (Kind == OMPD_single)
2550     Flags = OMP_IDENT_BARRIER_IMPL_SINGLE;
2551   else if (Kind == OMPD_barrier)
2552     Flags = OMP_IDENT_BARRIER_EXPL;
2553   else
2554     Flags = OMP_IDENT_BARRIER_IMPL;
2555   return Flags;
2556 }
2557 
2558 void CGOpenMPRuntime::getDefaultScheduleAndChunk(
2559     CodeGenFunction &CGF, const OMPLoopDirective &S,
2560     OpenMPScheduleClauseKind &ScheduleKind, const Expr *&ChunkExpr) const {
2561   // Check if the loop directive is actually a doacross loop directive. In this
2562   // case choose static, 1 schedule.
2563   if (llvm::any_of(
2564           S.getClausesOfKind<OMPOrderedClause>(),
2565           [](const OMPOrderedClause *C) { return C->getNumForLoops(); })) {
2566     ScheduleKind = OMPC_SCHEDULE_static;
2567     // Chunk size is 1 in this case.
2568     llvm::APInt ChunkSize(32, 1);
2569     ChunkExpr = IntegerLiteral::Create(
2570         CGF.getContext(), ChunkSize,
2571         CGF.getContext().getIntTypeForBitwidth(32, /*Signed=*/0),
2572         SourceLocation());
2573   }
2574 }
2575 
2576 void CGOpenMPRuntime::emitBarrierCall(CodeGenFunction &CGF, SourceLocation Loc,
2577                                       OpenMPDirectiveKind Kind, bool EmitChecks,
2578                                       bool ForceSimpleCall) {
2579   // Check if we should use the OMPBuilder
2580   auto *OMPRegionInfo =
2581       dyn_cast_or_null<CGOpenMPRegionInfo>(CGF.CapturedStmtInfo);
2582   if (CGF.CGM.getLangOpts().OpenMPIRBuilder) {
2583     CGF.Builder.restoreIP(OMPBuilder.CreateBarrier(
2584         CGF.Builder, Kind, ForceSimpleCall, EmitChecks));
2585     return;
2586   }
2587 
2588   if (!CGF.HaveInsertPoint())
2589     return;
2590   // Build call __kmpc_cancel_barrier(loc, thread_id);
2591   // Build call __kmpc_barrier(loc, thread_id);
2592   unsigned Flags = getDefaultFlagsForBarriers(Kind);
2593   // Build call __kmpc_cancel_barrier(loc, thread_id) or __kmpc_barrier(loc,
2594   // thread_id);
2595   llvm::Value *Args[] = {emitUpdateLocation(CGF, Loc, Flags),
2596                          getThreadID(CGF, Loc)};
2597   if (OMPRegionInfo) {
2598     if (!ForceSimpleCall && OMPRegionInfo->hasCancel()) {
2599       llvm::Value *Result = CGF.EmitRuntimeCall(
2600           OMPBuilder.getOrCreateRuntimeFunction(CGM.getModule(),
2601                                                 OMPRTL___kmpc_cancel_barrier),
2602           Args);
2603       if (EmitChecks) {
2604         // if (__kmpc_cancel_barrier()) {
2605         //   exit from construct;
2606         // }
2607         llvm::BasicBlock *ExitBB = CGF.createBasicBlock(".cancel.exit");
2608         llvm::BasicBlock *ContBB = CGF.createBasicBlock(".cancel.continue");
2609         llvm::Value *Cmp = CGF.Builder.CreateIsNotNull(Result);
2610         CGF.Builder.CreateCondBr(Cmp, ExitBB, ContBB);
2611         CGF.EmitBlock(ExitBB);
2612         //   exit from construct;
2613         CodeGenFunction::JumpDest CancelDestination =
2614             CGF.getOMPCancelDestination(OMPRegionInfo->getDirectiveKind());
2615         CGF.EmitBranchThroughCleanup(CancelDestination);
2616         CGF.EmitBlock(ContBB, /*IsFinished=*/true);
2617       }
2618       return;
2619     }
2620   }
2621   CGF.EmitRuntimeCall(OMPBuilder.getOrCreateRuntimeFunction(
2622                           CGM.getModule(), OMPRTL___kmpc_barrier),
2623                       Args);
2624 }
2625 
2626 /// Map the OpenMP loop schedule to the runtime enumeration.
2627 static OpenMPSchedType getRuntimeSchedule(OpenMPScheduleClauseKind ScheduleKind,
2628                                           bool Chunked, bool Ordered) {
2629   switch (ScheduleKind) {
2630   case OMPC_SCHEDULE_static:
2631     return Chunked ? (Ordered ? OMP_ord_static_chunked : OMP_sch_static_chunked)
2632                    : (Ordered ? OMP_ord_static : OMP_sch_static);
2633   case OMPC_SCHEDULE_dynamic:
2634     return Ordered ? OMP_ord_dynamic_chunked : OMP_sch_dynamic_chunked;
2635   case OMPC_SCHEDULE_guided:
2636     return Ordered ? OMP_ord_guided_chunked : OMP_sch_guided_chunked;
2637   case OMPC_SCHEDULE_runtime:
2638     return Ordered ? OMP_ord_runtime : OMP_sch_runtime;
2639   case OMPC_SCHEDULE_auto:
2640     return Ordered ? OMP_ord_auto : OMP_sch_auto;
2641   case OMPC_SCHEDULE_unknown:
2642     assert(!Chunked && "chunk was specified but schedule kind not known");
2643     return Ordered ? OMP_ord_static : OMP_sch_static;
2644   }
2645   llvm_unreachable("Unexpected runtime schedule");
2646 }
2647 
2648 /// Map the OpenMP distribute schedule to the runtime enumeration.
2649 static OpenMPSchedType
2650 getRuntimeSchedule(OpenMPDistScheduleClauseKind ScheduleKind, bool Chunked) {
2651   // only static is allowed for dist_schedule
2652   return Chunked ? OMP_dist_sch_static_chunked : OMP_dist_sch_static;
2653 }
2654 
2655 bool CGOpenMPRuntime::isStaticNonchunked(OpenMPScheduleClauseKind ScheduleKind,
2656                                          bool Chunked) const {
2657   OpenMPSchedType Schedule =
2658       getRuntimeSchedule(ScheduleKind, Chunked, /*Ordered=*/false);
2659   return Schedule == OMP_sch_static;
2660 }
2661 
2662 bool CGOpenMPRuntime::isStaticNonchunked(
2663     OpenMPDistScheduleClauseKind ScheduleKind, bool Chunked) const {
2664   OpenMPSchedType Schedule = getRuntimeSchedule(ScheduleKind, Chunked);
2665   return Schedule == OMP_dist_sch_static;
2666 }
2667 
2668 bool CGOpenMPRuntime::isStaticChunked(OpenMPScheduleClauseKind ScheduleKind,
2669                                       bool Chunked) const {
2670   OpenMPSchedType Schedule =
2671       getRuntimeSchedule(ScheduleKind, Chunked, /*Ordered=*/false);
2672   return Schedule == OMP_sch_static_chunked;
2673 }
2674 
2675 bool CGOpenMPRuntime::isStaticChunked(
2676     OpenMPDistScheduleClauseKind ScheduleKind, bool Chunked) const {
2677   OpenMPSchedType Schedule = getRuntimeSchedule(ScheduleKind, Chunked);
2678   return Schedule == OMP_dist_sch_static_chunked;
2679 }
2680 
2681 bool CGOpenMPRuntime::isDynamic(OpenMPScheduleClauseKind ScheduleKind) const {
2682   OpenMPSchedType Schedule =
2683       getRuntimeSchedule(ScheduleKind, /*Chunked=*/false, /*Ordered=*/false);
2684   assert(Schedule != OMP_sch_static_chunked && "cannot be chunked here");
2685   return Schedule != OMP_sch_static;
2686 }
2687 
2688 static int addMonoNonMonoModifier(CodeGenModule &CGM, OpenMPSchedType Schedule,
2689                                   OpenMPScheduleClauseModifier M1,
2690                                   OpenMPScheduleClauseModifier M2) {
2691   int Modifier = 0;
2692   switch (M1) {
2693   case OMPC_SCHEDULE_MODIFIER_monotonic:
2694     Modifier = OMP_sch_modifier_monotonic;
2695     break;
2696   case OMPC_SCHEDULE_MODIFIER_nonmonotonic:
2697     Modifier = OMP_sch_modifier_nonmonotonic;
2698     break;
2699   case OMPC_SCHEDULE_MODIFIER_simd:
2700     if (Schedule == OMP_sch_static_chunked)
2701       Schedule = OMP_sch_static_balanced_chunked;
2702     break;
2703   case OMPC_SCHEDULE_MODIFIER_last:
2704   case OMPC_SCHEDULE_MODIFIER_unknown:
2705     break;
2706   }
2707   switch (M2) {
2708   case OMPC_SCHEDULE_MODIFIER_monotonic:
2709     Modifier = OMP_sch_modifier_monotonic;
2710     break;
2711   case OMPC_SCHEDULE_MODIFIER_nonmonotonic:
2712     Modifier = OMP_sch_modifier_nonmonotonic;
2713     break;
2714   case OMPC_SCHEDULE_MODIFIER_simd:
2715     if (Schedule == OMP_sch_static_chunked)
2716       Schedule = OMP_sch_static_balanced_chunked;
2717     break;
2718   case OMPC_SCHEDULE_MODIFIER_last:
2719   case OMPC_SCHEDULE_MODIFIER_unknown:
2720     break;
2721   }
2722   // OpenMP 5.0, 2.9.2 Worksharing-Loop Construct, Desription.
2723   // If the static schedule kind is specified or if the ordered clause is
2724   // specified, and if the nonmonotonic modifier is not specified, the effect is
2725   // as if the monotonic modifier is specified. Otherwise, unless the monotonic
2726   // modifier is specified, the effect is as if the nonmonotonic modifier is
2727   // specified.
2728   if (CGM.getLangOpts().OpenMP >= 50 && Modifier == 0) {
2729     if (!(Schedule == OMP_sch_static_chunked || Schedule == OMP_sch_static ||
2730           Schedule == OMP_sch_static_balanced_chunked ||
2731           Schedule == OMP_ord_static_chunked || Schedule == OMP_ord_static ||
2732           Schedule == OMP_dist_sch_static_chunked ||
2733           Schedule == OMP_dist_sch_static))
2734       Modifier = OMP_sch_modifier_nonmonotonic;
2735   }
2736   return Schedule | Modifier;
2737 }
2738 
2739 void CGOpenMPRuntime::emitForDispatchInit(
2740     CodeGenFunction &CGF, SourceLocation Loc,
2741     const OpenMPScheduleTy &ScheduleKind, unsigned IVSize, bool IVSigned,
2742     bool Ordered, const DispatchRTInput &DispatchValues) {
2743   if (!CGF.HaveInsertPoint())
2744     return;
2745   OpenMPSchedType Schedule = getRuntimeSchedule(
2746       ScheduleKind.Schedule, DispatchValues.Chunk != nullptr, Ordered);
2747   assert(Ordered ||
2748          (Schedule != OMP_sch_static && Schedule != OMP_sch_static_chunked &&
2749           Schedule != OMP_ord_static && Schedule != OMP_ord_static_chunked &&
2750           Schedule != OMP_sch_static_balanced_chunked));
2751   // Call __kmpc_dispatch_init(
2752   //          ident_t *loc, kmp_int32 tid, kmp_int32 schedule,
2753   //          kmp_int[32|64] lower, kmp_int[32|64] upper,
2754   //          kmp_int[32|64] stride, kmp_int[32|64] chunk);
2755 
2756   // If the Chunk was not specified in the clause - use default value 1.
2757   llvm::Value *Chunk = DispatchValues.Chunk ? DispatchValues.Chunk
2758                                             : CGF.Builder.getIntN(IVSize, 1);
2759   llvm::Value *Args[] = {
2760       emitUpdateLocation(CGF, Loc),
2761       getThreadID(CGF, Loc),
2762       CGF.Builder.getInt32(addMonoNonMonoModifier(
2763           CGM, Schedule, ScheduleKind.M1, ScheduleKind.M2)), // Schedule type
2764       DispatchValues.LB,                                     // Lower
2765       DispatchValues.UB,                                     // Upper
2766       CGF.Builder.getIntN(IVSize, 1),                        // Stride
2767       Chunk                                                  // Chunk
2768   };
2769   CGF.EmitRuntimeCall(createDispatchInitFunction(IVSize, IVSigned), Args);
2770 }
2771 
2772 static void emitForStaticInitCall(
2773     CodeGenFunction &CGF, llvm::Value *UpdateLocation, llvm::Value *ThreadId,
2774     llvm::FunctionCallee ForStaticInitFunction, OpenMPSchedType Schedule,
2775     OpenMPScheduleClauseModifier M1, OpenMPScheduleClauseModifier M2,
2776     const CGOpenMPRuntime::StaticRTInput &Values) {
2777   if (!CGF.HaveInsertPoint())
2778     return;
2779 
2780   assert(!Values.Ordered);
2781   assert(Schedule == OMP_sch_static || Schedule == OMP_sch_static_chunked ||
2782          Schedule == OMP_sch_static_balanced_chunked ||
2783          Schedule == OMP_ord_static || Schedule == OMP_ord_static_chunked ||
2784          Schedule == OMP_dist_sch_static ||
2785          Schedule == OMP_dist_sch_static_chunked);
2786 
2787   // Call __kmpc_for_static_init(
2788   //          ident_t *loc, kmp_int32 tid, kmp_int32 schedtype,
2789   //          kmp_int32 *p_lastiter, kmp_int[32|64] *p_lower,
2790   //          kmp_int[32|64] *p_upper, kmp_int[32|64] *p_stride,
2791   //          kmp_int[32|64] incr, kmp_int[32|64] chunk);
2792   llvm::Value *Chunk = Values.Chunk;
2793   if (Chunk == nullptr) {
2794     assert((Schedule == OMP_sch_static || Schedule == OMP_ord_static ||
2795             Schedule == OMP_dist_sch_static) &&
2796            "expected static non-chunked schedule");
2797     // If the Chunk was not specified in the clause - use default value 1.
2798     Chunk = CGF.Builder.getIntN(Values.IVSize, 1);
2799   } else {
2800     assert((Schedule == OMP_sch_static_chunked ||
2801             Schedule == OMP_sch_static_balanced_chunked ||
2802             Schedule == OMP_ord_static_chunked ||
2803             Schedule == OMP_dist_sch_static_chunked) &&
2804            "expected static chunked schedule");
2805   }
2806   llvm::Value *Args[] = {
2807       UpdateLocation,
2808       ThreadId,
2809       CGF.Builder.getInt32(addMonoNonMonoModifier(CGF.CGM, Schedule, M1,
2810                                                   M2)), // Schedule type
2811       Values.IL.getPointer(),                           // &isLastIter
2812       Values.LB.getPointer(),                           // &LB
2813       Values.UB.getPointer(),                           // &UB
2814       Values.ST.getPointer(),                           // &Stride
2815       CGF.Builder.getIntN(Values.IVSize, 1),            // Incr
2816       Chunk                                             // Chunk
2817   };
2818   CGF.EmitRuntimeCall(ForStaticInitFunction, Args);
2819 }
2820 
2821 void CGOpenMPRuntime::emitForStaticInit(CodeGenFunction &CGF,
2822                                         SourceLocation Loc,
2823                                         OpenMPDirectiveKind DKind,
2824                                         const OpenMPScheduleTy &ScheduleKind,
2825                                         const StaticRTInput &Values) {
2826   OpenMPSchedType ScheduleNum = getRuntimeSchedule(
2827       ScheduleKind.Schedule, Values.Chunk != nullptr, Values.Ordered);
2828   assert(isOpenMPWorksharingDirective(DKind) &&
2829          "Expected loop-based or sections-based directive.");
2830   llvm::Value *UpdatedLocation = emitUpdateLocation(CGF, Loc,
2831                                              isOpenMPLoopDirective(DKind)
2832                                                  ? OMP_IDENT_WORK_LOOP
2833                                                  : OMP_IDENT_WORK_SECTIONS);
2834   llvm::Value *ThreadId = getThreadID(CGF, Loc);
2835   llvm::FunctionCallee StaticInitFunction =
2836       createForStaticInitFunction(Values.IVSize, Values.IVSigned);
2837   auto DL = ApplyDebugLocation::CreateDefaultArtificial(CGF, Loc);
2838   emitForStaticInitCall(CGF, UpdatedLocation, ThreadId, StaticInitFunction,
2839                         ScheduleNum, ScheduleKind.M1, ScheduleKind.M2, Values);
2840 }
2841 
2842 void CGOpenMPRuntime::emitDistributeStaticInit(
2843     CodeGenFunction &CGF, SourceLocation Loc,
2844     OpenMPDistScheduleClauseKind SchedKind,
2845     const CGOpenMPRuntime::StaticRTInput &Values) {
2846   OpenMPSchedType ScheduleNum =
2847       getRuntimeSchedule(SchedKind, Values.Chunk != nullptr);
2848   llvm::Value *UpdatedLocation =
2849       emitUpdateLocation(CGF, Loc, OMP_IDENT_WORK_DISTRIBUTE);
2850   llvm::Value *ThreadId = getThreadID(CGF, Loc);
2851   llvm::FunctionCallee StaticInitFunction =
2852       createForStaticInitFunction(Values.IVSize, Values.IVSigned);
2853   emitForStaticInitCall(CGF, UpdatedLocation, ThreadId, StaticInitFunction,
2854                         ScheduleNum, OMPC_SCHEDULE_MODIFIER_unknown,
2855                         OMPC_SCHEDULE_MODIFIER_unknown, Values);
2856 }
2857 
2858 void CGOpenMPRuntime::emitForStaticFinish(CodeGenFunction &CGF,
2859                                           SourceLocation Loc,
2860                                           OpenMPDirectiveKind DKind) {
2861   if (!CGF.HaveInsertPoint())
2862     return;
2863   // Call __kmpc_for_static_fini(ident_t *loc, kmp_int32 tid);
2864   llvm::Value *Args[] = {
2865       emitUpdateLocation(CGF, Loc,
2866                          isOpenMPDistributeDirective(DKind)
2867                              ? OMP_IDENT_WORK_DISTRIBUTE
2868                              : isOpenMPLoopDirective(DKind)
2869                                    ? OMP_IDENT_WORK_LOOP
2870                                    : OMP_IDENT_WORK_SECTIONS),
2871       getThreadID(CGF, Loc)};
2872   auto DL = ApplyDebugLocation::CreateDefaultArtificial(CGF, Loc);
2873   CGF.EmitRuntimeCall(OMPBuilder.getOrCreateRuntimeFunction(
2874                           CGM.getModule(), OMPRTL___kmpc_for_static_fini),
2875                       Args);
2876 }
2877 
2878 void CGOpenMPRuntime::emitForOrderedIterationEnd(CodeGenFunction &CGF,
2879                                                  SourceLocation Loc,
2880                                                  unsigned IVSize,
2881                                                  bool IVSigned) {
2882   if (!CGF.HaveInsertPoint())
2883     return;
2884   // Call __kmpc_for_dynamic_fini_(4|8)[u](ident_t *loc, kmp_int32 tid);
2885   llvm::Value *Args[] = {emitUpdateLocation(CGF, Loc), getThreadID(CGF, Loc)};
2886   CGF.EmitRuntimeCall(createDispatchFiniFunction(IVSize, IVSigned), Args);
2887 }
2888 
2889 llvm::Value *CGOpenMPRuntime::emitForNext(CodeGenFunction &CGF,
2890                                           SourceLocation Loc, unsigned IVSize,
2891                                           bool IVSigned, Address IL,
2892                                           Address LB, Address UB,
2893                                           Address ST) {
2894   // Call __kmpc_dispatch_next(
2895   //          ident_t *loc, kmp_int32 tid, kmp_int32 *p_lastiter,
2896   //          kmp_int[32|64] *p_lower, kmp_int[32|64] *p_upper,
2897   //          kmp_int[32|64] *p_stride);
2898   llvm::Value *Args[] = {
2899       emitUpdateLocation(CGF, Loc),
2900       getThreadID(CGF, Loc),
2901       IL.getPointer(), // &isLastIter
2902       LB.getPointer(), // &Lower
2903       UB.getPointer(), // &Upper
2904       ST.getPointer()  // &Stride
2905   };
2906   llvm::Value *Call =
2907       CGF.EmitRuntimeCall(createDispatchNextFunction(IVSize, IVSigned), Args);
2908   return CGF.EmitScalarConversion(
2909       Call, CGF.getContext().getIntTypeForBitwidth(32, /*Signed=*/1),
2910       CGF.getContext().BoolTy, Loc);
2911 }
2912 
2913 void CGOpenMPRuntime::emitNumThreadsClause(CodeGenFunction &CGF,
2914                                            llvm::Value *NumThreads,
2915                                            SourceLocation Loc) {
2916   if (!CGF.HaveInsertPoint())
2917     return;
2918   // Build call __kmpc_push_num_threads(&loc, global_tid, num_threads)
2919   llvm::Value *Args[] = {
2920       emitUpdateLocation(CGF, Loc), getThreadID(CGF, Loc),
2921       CGF.Builder.CreateIntCast(NumThreads, CGF.Int32Ty, /*isSigned*/ true)};
2922   CGF.EmitRuntimeCall(OMPBuilder.getOrCreateRuntimeFunction(
2923                           CGM.getModule(), OMPRTL___kmpc_push_num_threads),
2924                       Args);
2925 }
2926 
2927 void CGOpenMPRuntime::emitProcBindClause(CodeGenFunction &CGF,
2928                                          ProcBindKind ProcBind,
2929                                          SourceLocation Loc) {
2930   if (!CGF.HaveInsertPoint())
2931     return;
2932   assert(ProcBind != OMP_PROC_BIND_unknown && "Unsupported proc_bind value.");
2933   // Build call __kmpc_push_proc_bind(&loc, global_tid, proc_bind)
2934   llvm::Value *Args[] = {
2935       emitUpdateLocation(CGF, Loc), getThreadID(CGF, Loc),
2936       llvm::ConstantInt::get(CGM.IntTy, unsigned(ProcBind), /*isSigned=*/true)};
2937   CGF.EmitRuntimeCall(OMPBuilder.getOrCreateRuntimeFunction(
2938                           CGM.getModule(), OMPRTL___kmpc_push_proc_bind),
2939                       Args);
2940 }
2941 
2942 void CGOpenMPRuntime::emitFlush(CodeGenFunction &CGF, ArrayRef<const Expr *>,
2943                                 SourceLocation Loc, llvm::AtomicOrdering AO) {
2944   if (CGF.CGM.getLangOpts().OpenMPIRBuilder) {
2945     OMPBuilder.CreateFlush(CGF.Builder);
2946   } else {
2947     if (!CGF.HaveInsertPoint())
2948       return;
2949     // Build call void __kmpc_flush(ident_t *loc)
2950     CGF.EmitRuntimeCall(OMPBuilder.getOrCreateRuntimeFunction(
2951                             CGM.getModule(), OMPRTL___kmpc_flush),
2952                         emitUpdateLocation(CGF, Loc));
2953   }
2954 }
2955 
2956 namespace {
2957 /// Indexes of fields for type kmp_task_t.
2958 enum KmpTaskTFields {
2959   /// List of shared variables.
2960   KmpTaskTShareds,
2961   /// Task routine.
2962   KmpTaskTRoutine,
2963   /// Partition id for the untied tasks.
2964   KmpTaskTPartId,
2965   /// Function with call of destructors for private variables.
2966   Data1,
2967   /// Task priority.
2968   Data2,
2969   /// (Taskloops only) Lower bound.
2970   KmpTaskTLowerBound,
2971   /// (Taskloops only) Upper bound.
2972   KmpTaskTUpperBound,
2973   /// (Taskloops only) Stride.
2974   KmpTaskTStride,
2975   /// (Taskloops only) Is last iteration flag.
2976   KmpTaskTLastIter,
2977   /// (Taskloops only) Reduction data.
2978   KmpTaskTReductions,
2979 };
2980 } // anonymous namespace
2981 
2982 bool CGOpenMPRuntime::OffloadEntriesInfoManagerTy::empty() const {
2983   return OffloadEntriesTargetRegion.empty() &&
2984          OffloadEntriesDeviceGlobalVar.empty();
2985 }
2986 
2987 /// Initialize target region entry.
2988 void CGOpenMPRuntime::OffloadEntriesInfoManagerTy::
2989     initializeTargetRegionEntryInfo(unsigned DeviceID, unsigned FileID,
2990                                     StringRef ParentName, unsigned LineNum,
2991                                     unsigned Order) {
2992   assert(CGM.getLangOpts().OpenMPIsDevice && "Initialization of entries is "
2993                                              "only required for the device "
2994                                              "code generation.");
2995   OffloadEntriesTargetRegion[DeviceID][FileID][ParentName][LineNum] =
2996       OffloadEntryInfoTargetRegion(Order, /*Addr=*/nullptr, /*ID=*/nullptr,
2997                                    OMPTargetRegionEntryTargetRegion);
2998   ++OffloadingEntriesNum;
2999 }
3000 
3001 void CGOpenMPRuntime::OffloadEntriesInfoManagerTy::
3002     registerTargetRegionEntryInfo(unsigned DeviceID, unsigned FileID,
3003                                   StringRef ParentName, unsigned LineNum,
3004                                   llvm::Constant *Addr, llvm::Constant *ID,
3005                                   OMPTargetRegionEntryKind Flags) {
3006   // If we are emitting code for a target, the entry is already initialized,
3007   // only has to be registered.
3008   if (CGM.getLangOpts().OpenMPIsDevice) {
3009     if (!hasTargetRegionEntryInfo(DeviceID, FileID, ParentName, LineNum)) {
3010       unsigned DiagID = CGM.getDiags().getCustomDiagID(
3011           DiagnosticsEngine::Error,
3012           "Unable to find target region on line '%0' in the device code.");
3013       CGM.getDiags().Report(DiagID) << LineNum;
3014       return;
3015     }
3016     auto &Entry =
3017         OffloadEntriesTargetRegion[DeviceID][FileID][ParentName][LineNum];
3018     assert(Entry.isValid() && "Entry not initialized!");
3019     Entry.setAddress(Addr);
3020     Entry.setID(ID);
3021     Entry.setFlags(Flags);
3022   } else {
3023     OffloadEntryInfoTargetRegion Entry(OffloadingEntriesNum, Addr, ID, Flags);
3024     OffloadEntriesTargetRegion[DeviceID][FileID][ParentName][LineNum] = Entry;
3025     ++OffloadingEntriesNum;
3026   }
3027 }
3028 
3029 bool CGOpenMPRuntime::OffloadEntriesInfoManagerTy::hasTargetRegionEntryInfo(
3030     unsigned DeviceID, unsigned FileID, StringRef ParentName,
3031     unsigned LineNum) const {
3032   auto PerDevice = OffloadEntriesTargetRegion.find(DeviceID);
3033   if (PerDevice == OffloadEntriesTargetRegion.end())
3034     return false;
3035   auto PerFile = PerDevice->second.find(FileID);
3036   if (PerFile == PerDevice->second.end())
3037     return false;
3038   auto PerParentName = PerFile->second.find(ParentName);
3039   if (PerParentName == PerFile->second.end())
3040     return false;
3041   auto PerLine = PerParentName->second.find(LineNum);
3042   if (PerLine == PerParentName->second.end())
3043     return false;
3044   // Fail if this entry is already registered.
3045   if (PerLine->second.getAddress() || PerLine->second.getID())
3046     return false;
3047   return true;
3048 }
3049 
3050 void CGOpenMPRuntime::OffloadEntriesInfoManagerTy::actOnTargetRegionEntriesInfo(
3051     const OffloadTargetRegionEntryInfoActTy &Action) {
3052   // Scan all target region entries and perform the provided action.
3053   for (const auto &D : OffloadEntriesTargetRegion)
3054     for (const auto &F : D.second)
3055       for (const auto &P : F.second)
3056         for (const auto &L : P.second)
3057           Action(D.first, F.first, P.first(), L.first, L.second);
3058 }
3059 
3060 void CGOpenMPRuntime::OffloadEntriesInfoManagerTy::
3061     initializeDeviceGlobalVarEntryInfo(StringRef Name,
3062                                        OMPTargetGlobalVarEntryKind Flags,
3063                                        unsigned Order) {
3064   assert(CGM.getLangOpts().OpenMPIsDevice && "Initialization of entries is "
3065                                              "only required for the device "
3066                                              "code generation.");
3067   OffloadEntriesDeviceGlobalVar.try_emplace(Name, Order, Flags);
3068   ++OffloadingEntriesNum;
3069 }
3070 
3071 void CGOpenMPRuntime::OffloadEntriesInfoManagerTy::
3072     registerDeviceGlobalVarEntryInfo(StringRef VarName, llvm::Constant *Addr,
3073                                      CharUnits VarSize,
3074                                      OMPTargetGlobalVarEntryKind Flags,
3075                                      llvm::GlobalValue::LinkageTypes Linkage) {
3076   if (CGM.getLangOpts().OpenMPIsDevice) {
3077     auto &Entry = OffloadEntriesDeviceGlobalVar[VarName];
3078     assert(Entry.isValid() && Entry.getFlags() == Flags &&
3079            "Entry not initialized!");
3080     assert((!Entry.getAddress() || Entry.getAddress() == Addr) &&
3081            "Resetting with the new address.");
3082     if (Entry.getAddress() && hasDeviceGlobalVarEntryInfo(VarName)) {
3083       if (Entry.getVarSize().isZero()) {
3084         Entry.setVarSize(VarSize);
3085         Entry.setLinkage(Linkage);
3086       }
3087       return;
3088     }
3089     Entry.setVarSize(VarSize);
3090     Entry.setLinkage(Linkage);
3091     Entry.setAddress(Addr);
3092   } else {
3093     if (hasDeviceGlobalVarEntryInfo(VarName)) {
3094       auto &Entry = OffloadEntriesDeviceGlobalVar[VarName];
3095       assert(Entry.isValid() && Entry.getFlags() == Flags &&
3096              "Entry not initialized!");
3097       assert((!Entry.getAddress() || Entry.getAddress() == Addr) &&
3098              "Resetting with the new address.");
3099       if (Entry.getVarSize().isZero()) {
3100         Entry.setVarSize(VarSize);
3101         Entry.setLinkage(Linkage);
3102       }
3103       return;
3104     }
3105     OffloadEntriesDeviceGlobalVar.try_emplace(
3106         VarName, OffloadingEntriesNum, Addr, VarSize, Flags, Linkage);
3107     ++OffloadingEntriesNum;
3108   }
3109 }
3110 
3111 void CGOpenMPRuntime::OffloadEntriesInfoManagerTy::
3112     actOnDeviceGlobalVarEntriesInfo(
3113         const OffloadDeviceGlobalVarEntryInfoActTy &Action) {
3114   // Scan all target region entries and perform the provided action.
3115   for (const auto &E : OffloadEntriesDeviceGlobalVar)
3116     Action(E.getKey(), E.getValue());
3117 }
3118 
3119 void CGOpenMPRuntime::createOffloadEntry(
3120     llvm::Constant *ID, llvm::Constant *Addr, uint64_t Size, int32_t Flags,
3121     llvm::GlobalValue::LinkageTypes Linkage) {
3122   StringRef Name = Addr->getName();
3123   llvm::Module &M = CGM.getModule();
3124   llvm::LLVMContext &C = M.getContext();
3125 
3126   // Create constant string with the name.
3127   llvm::Constant *StrPtrInit = llvm::ConstantDataArray::getString(C, Name);
3128 
3129   std::string StringName = getName({"omp_offloading", "entry_name"});
3130   auto *Str = new llvm::GlobalVariable(
3131       M, StrPtrInit->getType(), /*isConstant=*/true,
3132       llvm::GlobalValue::InternalLinkage, StrPtrInit, StringName);
3133   Str->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
3134 
3135   llvm::Constant *Data[] = {llvm::ConstantExpr::getBitCast(ID, CGM.VoidPtrTy),
3136                             llvm::ConstantExpr::getBitCast(Str, CGM.Int8PtrTy),
3137                             llvm::ConstantInt::get(CGM.SizeTy, Size),
3138                             llvm::ConstantInt::get(CGM.Int32Ty, Flags),
3139                             llvm::ConstantInt::get(CGM.Int32Ty, 0)};
3140   std::string EntryName = getName({"omp_offloading", "entry", ""});
3141   llvm::GlobalVariable *Entry = createGlobalStruct(
3142       CGM, getTgtOffloadEntryQTy(), /*IsConstant=*/true, Data,
3143       Twine(EntryName).concat(Name), llvm::GlobalValue::WeakAnyLinkage);
3144 
3145   // The entry has to be created in the section the linker expects it to be.
3146   Entry->setSection("omp_offloading_entries");
3147 }
3148 
3149 void CGOpenMPRuntime::createOffloadEntriesAndInfoMetadata() {
3150   // Emit the offloading entries and metadata so that the device codegen side
3151   // can easily figure out what to emit. The produced metadata looks like
3152   // this:
3153   //
3154   // !omp_offload.info = !{!1, ...}
3155   //
3156   // Right now we only generate metadata for function that contain target
3157   // regions.
3158 
3159   // If we are in simd mode or there are no entries, we don't need to do
3160   // anything.
3161   if (CGM.getLangOpts().OpenMPSimd || OffloadEntriesInfoManager.empty())
3162     return;
3163 
3164   llvm::Module &M = CGM.getModule();
3165   llvm::LLVMContext &C = M.getContext();
3166   SmallVector<std::tuple<const OffloadEntriesInfoManagerTy::OffloadEntryInfo *,
3167                          SourceLocation, StringRef>,
3168               16>
3169       OrderedEntries(OffloadEntriesInfoManager.size());
3170   llvm::SmallVector<StringRef, 16> ParentFunctions(
3171       OffloadEntriesInfoManager.size());
3172 
3173   // Auxiliary methods to create metadata values and strings.
3174   auto &&GetMDInt = [this](unsigned V) {
3175     return llvm::ConstantAsMetadata::get(
3176         llvm::ConstantInt::get(CGM.Int32Ty, V));
3177   };
3178 
3179   auto &&GetMDString = [&C](StringRef V) { return llvm::MDString::get(C, V); };
3180 
3181   // Create the offloading info metadata node.
3182   llvm::NamedMDNode *MD = M.getOrInsertNamedMetadata("omp_offload.info");
3183 
3184   // Create function that emits metadata for each target region entry;
3185   auto &&TargetRegionMetadataEmitter =
3186       [this, &C, MD, &OrderedEntries, &ParentFunctions, &GetMDInt,
3187        &GetMDString](
3188           unsigned DeviceID, unsigned FileID, StringRef ParentName,
3189           unsigned Line,
3190           const OffloadEntriesInfoManagerTy::OffloadEntryInfoTargetRegion &E) {
3191         // Generate metadata for target regions. Each entry of this metadata
3192         // contains:
3193         // - Entry 0 -> Kind of this type of metadata (0).
3194         // - Entry 1 -> Device ID of the file where the entry was identified.
3195         // - Entry 2 -> File ID of the file where the entry was identified.
3196         // - Entry 3 -> Mangled name of the function where the entry was
3197         // identified.
3198         // - Entry 4 -> Line in the file where the entry was identified.
3199         // - Entry 5 -> Order the entry was created.
3200         // The first element of the metadata node is the kind.
3201         llvm::Metadata *Ops[] = {GetMDInt(E.getKind()), GetMDInt(DeviceID),
3202                                  GetMDInt(FileID),      GetMDString(ParentName),
3203                                  GetMDInt(Line),        GetMDInt(E.getOrder())};
3204 
3205         SourceLocation Loc;
3206         for (auto I = CGM.getContext().getSourceManager().fileinfo_begin(),
3207                   E = CGM.getContext().getSourceManager().fileinfo_end();
3208              I != E; ++I) {
3209           if (I->getFirst()->getUniqueID().getDevice() == DeviceID &&
3210               I->getFirst()->getUniqueID().getFile() == FileID) {
3211             Loc = CGM.getContext().getSourceManager().translateFileLineCol(
3212                 I->getFirst(), Line, 1);
3213             break;
3214           }
3215         }
3216         // Save this entry in the right position of the ordered entries array.
3217         OrderedEntries[E.getOrder()] = std::make_tuple(&E, Loc, ParentName);
3218         ParentFunctions[E.getOrder()] = ParentName;
3219 
3220         // Add metadata to the named metadata node.
3221         MD->addOperand(llvm::MDNode::get(C, Ops));
3222       };
3223 
3224   OffloadEntriesInfoManager.actOnTargetRegionEntriesInfo(
3225       TargetRegionMetadataEmitter);
3226 
3227   // Create function that emits metadata for each device global variable entry;
3228   auto &&DeviceGlobalVarMetadataEmitter =
3229       [&C, &OrderedEntries, &GetMDInt, &GetMDString,
3230        MD](StringRef MangledName,
3231            const OffloadEntriesInfoManagerTy::OffloadEntryInfoDeviceGlobalVar
3232                &E) {
3233         // Generate metadata for global variables. Each entry of this metadata
3234         // contains:
3235         // - Entry 0 -> Kind of this type of metadata (1).
3236         // - Entry 1 -> Mangled name of the variable.
3237         // - Entry 2 -> Declare target kind.
3238         // - Entry 3 -> Order the entry was created.
3239         // The first element of the metadata node is the kind.
3240         llvm::Metadata *Ops[] = {
3241             GetMDInt(E.getKind()), GetMDString(MangledName),
3242             GetMDInt(E.getFlags()), GetMDInt(E.getOrder())};
3243 
3244         // Save this entry in the right position of the ordered entries array.
3245         OrderedEntries[E.getOrder()] =
3246             std::make_tuple(&E, SourceLocation(), MangledName);
3247 
3248         // Add metadata to the named metadata node.
3249         MD->addOperand(llvm::MDNode::get(C, Ops));
3250       };
3251 
3252   OffloadEntriesInfoManager.actOnDeviceGlobalVarEntriesInfo(
3253       DeviceGlobalVarMetadataEmitter);
3254 
3255   for (const auto &E : OrderedEntries) {
3256     assert(std::get<0>(E) && "All ordered entries must exist!");
3257     if (const auto *CE =
3258             dyn_cast<OffloadEntriesInfoManagerTy::OffloadEntryInfoTargetRegion>(
3259                 std::get<0>(E))) {
3260       if (!CE->getID() || !CE->getAddress()) {
3261         // Do not blame the entry if the parent funtion is not emitted.
3262         StringRef FnName = ParentFunctions[CE->getOrder()];
3263         if (!CGM.GetGlobalValue(FnName))
3264           continue;
3265         unsigned DiagID = CGM.getDiags().getCustomDiagID(
3266             DiagnosticsEngine::Error,
3267             "Offloading entry for target region in %0 is incorrect: either the "
3268             "address or the ID is invalid.");
3269         CGM.getDiags().Report(std::get<1>(E), DiagID) << FnName;
3270         continue;
3271       }
3272       createOffloadEntry(CE->getID(), CE->getAddress(), /*Size=*/0,
3273                          CE->getFlags(), llvm::GlobalValue::WeakAnyLinkage);
3274     } else if (const auto *CE = dyn_cast<OffloadEntriesInfoManagerTy::
3275                                              OffloadEntryInfoDeviceGlobalVar>(
3276                    std::get<0>(E))) {
3277       OffloadEntriesInfoManagerTy::OMPTargetGlobalVarEntryKind Flags =
3278           static_cast<OffloadEntriesInfoManagerTy::OMPTargetGlobalVarEntryKind>(
3279               CE->getFlags());
3280       switch (Flags) {
3281       case OffloadEntriesInfoManagerTy::OMPTargetGlobalVarEntryTo: {
3282         if (CGM.getLangOpts().OpenMPIsDevice &&
3283             CGM.getOpenMPRuntime().hasRequiresUnifiedSharedMemory())
3284           continue;
3285         if (!CE->getAddress()) {
3286           unsigned DiagID = CGM.getDiags().getCustomDiagID(
3287               DiagnosticsEngine::Error, "Offloading entry for declare target "
3288                                         "variable %0 is incorrect: the "
3289                                         "address is invalid.");
3290           CGM.getDiags().Report(std::get<1>(E), DiagID) << std::get<2>(E);
3291           continue;
3292         }
3293         // The vaiable has no definition - no need to add the entry.
3294         if (CE->getVarSize().isZero())
3295           continue;
3296         break;
3297       }
3298       case OffloadEntriesInfoManagerTy::OMPTargetGlobalVarEntryLink:
3299         assert(((CGM.getLangOpts().OpenMPIsDevice && !CE->getAddress()) ||
3300                 (!CGM.getLangOpts().OpenMPIsDevice && CE->getAddress())) &&
3301                "Declaret target link address is set.");
3302         if (CGM.getLangOpts().OpenMPIsDevice)
3303           continue;
3304         if (!CE->getAddress()) {
3305           unsigned DiagID = CGM.getDiags().getCustomDiagID(
3306               DiagnosticsEngine::Error,
3307               "Offloading entry for declare target variable is incorrect: the "
3308               "address is invalid.");
3309           CGM.getDiags().Report(DiagID);
3310           continue;
3311         }
3312         break;
3313       }
3314       createOffloadEntry(CE->getAddress(), CE->getAddress(),
3315                          CE->getVarSize().getQuantity(), Flags,
3316                          CE->getLinkage());
3317     } else {
3318       llvm_unreachable("Unsupported entry kind.");
3319     }
3320   }
3321 }
3322 
3323 /// Loads all the offload entries information from the host IR
3324 /// metadata.
3325 void CGOpenMPRuntime::loadOffloadInfoMetadata() {
3326   // If we are in target mode, load the metadata from the host IR. This code has
3327   // to match the metadaata creation in createOffloadEntriesAndInfoMetadata().
3328 
3329   if (!CGM.getLangOpts().OpenMPIsDevice)
3330     return;
3331 
3332   if (CGM.getLangOpts().OMPHostIRFile.empty())
3333     return;
3334 
3335   auto Buf = llvm::MemoryBuffer::getFile(CGM.getLangOpts().OMPHostIRFile);
3336   if (auto EC = Buf.getError()) {
3337     CGM.getDiags().Report(diag::err_cannot_open_file)
3338         << CGM.getLangOpts().OMPHostIRFile << EC.message();
3339     return;
3340   }
3341 
3342   llvm::LLVMContext C;
3343   auto ME = expectedToErrorOrAndEmitErrors(
3344       C, llvm::parseBitcodeFile(Buf.get()->getMemBufferRef(), C));
3345 
3346   if (auto EC = ME.getError()) {
3347     unsigned DiagID = CGM.getDiags().getCustomDiagID(
3348         DiagnosticsEngine::Error, "Unable to parse host IR file '%0':'%1'");
3349     CGM.getDiags().Report(DiagID)
3350         << CGM.getLangOpts().OMPHostIRFile << EC.message();
3351     return;
3352   }
3353 
3354   llvm::NamedMDNode *MD = ME.get()->getNamedMetadata("omp_offload.info");
3355   if (!MD)
3356     return;
3357 
3358   for (llvm::MDNode *MN : MD->operands()) {
3359     auto &&GetMDInt = [MN](unsigned Idx) {
3360       auto *V = cast<llvm::ConstantAsMetadata>(MN->getOperand(Idx));
3361       return cast<llvm::ConstantInt>(V->getValue())->getZExtValue();
3362     };
3363 
3364     auto &&GetMDString = [MN](unsigned Idx) {
3365       auto *V = cast<llvm::MDString>(MN->getOperand(Idx));
3366       return V->getString();
3367     };
3368 
3369     switch (GetMDInt(0)) {
3370     default:
3371       llvm_unreachable("Unexpected metadata!");
3372       break;
3373     case OffloadEntriesInfoManagerTy::OffloadEntryInfo::
3374         OffloadingEntryInfoTargetRegion:
3375       OffloadEntriesInfoManager.initializeTargetRegionEntryInfo(
3376           /*DeviceID=*/GetMDInt(1), /*FileID=*/GetMDInt(2),
3377           /*ParentName=*/GetMDString(3), /*Line=*/GetMDInt(4),
3378           /*Order=*/GetMDInt(5));
3379       break;
3380     case OffloadEntriesInfoManagerTy::OffloadEntryInfo::
3381         OffloadingEntryInfoDeviceGlobalVar:
3382       OffloadEntriesInfoManager.initializeDeviceGlobalVarEntryInfo(
3383           /*MangledName=*/GetMDString(1),
3384           static_cast<OffloadEntriesInfoManagerTy::OMPTargetGlobalVarEntryKind>(
3385               /*Flags=*/GetMDInt(2)),
3386           /*Order=*/GetMDInt(3));
3387       break;
3388     }
3389   }
3390 }
3391 
3392 void CGOpenMPRuntime::emitKmpRoutineEntryT(QualType KmpInt32Ty) {
3393   if (!KmpRoutineEntryPtrTy) {
3394     // Build typedef kmp_int32 (* kmp_routine_entry_t)(kmp_int32, void *); type.
3395     ASTContext &C = CGM.getContext();
3396     QualType KmpRoutineEntryTyArgs[] = {KmpInt32Ty, C.VoidPtrTy};
3397     FunctionProtoType::ExtProtoInfo EPI;
3398     KmpRoutineEntryPtrQTy = C.getPointerType(
3399         C.getFunctionType(KmpInt32Ty, KmpRoutineEntryTyArgs, EPI));
3400     KmpRoutineEntryPtrTy = CGM.getTypes().ConvertType(KmpRoutineEntryPtrQTy);
3401   }
3402 }
3403 
3404 QualType CGOpenMPRuntime::getTgtOffloadEntryQTy() {
3405   // Make sure the type of the entry is already created. This is the type we
3406   // have to create:
3407   // struct __tgt_offload_entry{
3408   //   void      *addr;       // Pointer to the offload entry info.
3409   //                          // (function or global)
3410   //   char      *name;       // Name of the function or global.
3411   //   size_t     size;       // Size of the entry info (0 if it a function).
3412   //   int32_t    flags;      // Flags associated with the entry, e.g. 'link'.
3413   //   int32_t    reserved;   // Reserved, to use by the runtime library.
3414   // };
3415   if (TgtOffloadEntryQTy.isNull()) {
3416     ASTContext &C = CGM.getContext();
3417     RecordDecl *RD = C.buildImplicitRecord("__tgt_offload_entry");
3418     RD->startDefinition();
3419     addFieldToRecordDecl(C, RD, C.VoidPtrTy);
3420     addFieldToRecordDecl(C, RD, C.getPointerType(C.CharTy));
3421     addFieldToRecordDecl(C, RD, C.getSizeType());
3422     addFieldToRecordDecl(
3423         C, RD, C.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/true));
3424     addFieldToRecordDecl(
3425         C, RD, C.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/true));
3426     RD->completeDefinition();
3427     RD->addAttr(PackedAttr::CreateImplicit(C));
3428     TgtOffloadEntryQTy = C.getRecordType(RD);
3429   }
3430   return TgtOffloadEntryQTy;
3431 }
3432 
3433 namespace {
3434 struct PrivateHelpersTy {
3435   PrivateHelpersTy(const Expr *OriginalRef, const VarDecl *Original,
3436                    const VarDecl *PrivateCopy, const VarDecl *PrivateElemInit)
3437       : OriginalRef(OriginalRef), Original(Original), PrivateCopy(PrivateCopy),
3438         PrivateElemInit(PrivateElemInit) {}
3439   const Expr *OriginalRef = nullptr;
3440   const VarDecl *Original = nullptr;
3441   const VarDecl *PrivateCopy = nullptr;
3442   const VarDecl *PrivateElemInit = nullptr;
3443 };
3444 typedef std::pair<CharUnits /*Align*/, PrivateHelpersTy> PrivateDataTy;
3445 } // anonymous namespace
3446 
3447 static RecordDecl *
3448 createPrivatesRecordDecl(CodeGenModule &CGM, ArrayRef<PrivateDataTy> Privates) {
3449   if (!Privates.empty()) {
3450     ASTContext &C = CGM.getContext();
3451     // Build struct .kmp_privates_t. {
3452     //         /*  private vars  */
3453     //       };
3454     RecordDecl *RD = C.buildImplicitRecord(".kmp_privates.t");
3455     RD->startDefinition();
3456     for (const auto &Pair : Privates) {
3457       const VarDecl *VD = Pair.second.Original;
3458       QualType Type = VD->getType().getNonReferenceType();
3459       FieldDecl *FD = addFieldToRecordDecl(C, RD, Type);
3460       if (VD->hasAttrs()) {
3461         for (specific_attr_iterator<AlignedAttr> I(VD->getAttrs().begin()),
3462              E(VD->getAttrs().end());
3463              I != E; ++I)
3464           FD->addAttr(*I);
3465       }
3466     }
3467     RD->completeDefinition();
3468     return RD;
3469   }
3470   return nullptr;
3471 }
3472 
3473 static RecordDecl *
3474 createKmpTaskTRecordDecl(CodeGenModule &CGM, OpenMPDirectiveKind Kind,
3475                          QualType KmpInt32Ty,
3476                          QualType KmpRoutineEntryPointerQTy) {
3477   ASTContext &C = CGM.getContext();
3478   // Build struct kmp_task_t {
3479   //         void *              shareds;
3480   //         kmp_routine_entry_t routine;
3481   //         kmp_int32           part_id;
3482   //         kmp_cmplrdata_t data1;
3483   //         kmp_cmplrdata_t data2;
3484   // For taskloops additional fields:
3485   //         kmp_uint64          lb;
3486   //         kmp_uint64          ub;
3487   //         kmp_int64           st;
3488   //         kmp_int32           liter;
3489   //         void *              reductions;
3490   //       };
3491   RecordDecl *UD = C.buildImplicitRecord("kmp_cmplrdata_t", TTK_Union);
3492   UD->startDefinition();
3493   addFieldToRecordDecl(C, UD, KmpInt32Ty);
3494   addFieldToRecordDecl(C, UD, KmpRoutineEntryPointerQTy);
3495   UD->completeDefinition();
3496   QualType KmpCmplrdataTy = C.getRecordType(UD);
3497   RecordDecl *RD = C.buildImplicitRecord("kmp_task_t");
3498   RD->startDefinition();
3499   addFieldToRecordDecl(C, RD, C.VoidPtrTy);
3500   addFieldToRecordDecl(C, RD, KmpRoutineEntryPointerQTy);
3501   addFieldToRecordDecl(C, RD, KmpInt32Ty);
3502   addFieldToRecordDecl(C, RD, KmpCmplrdataTy);
3503   addFieldToRecordDecl(C, RD, KmpCmplrdataTy);
3504   if (isOpenMPTaskLoopDirective(Kind)) {
3505     QualType KmpUInt64Ty =
3506         CGM.getContext().getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0);
3507     QualType KmpInt64Ty =
3508         CGM.getContext().getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1);
3509     addFieldToRecordDecl(C, RD, KmpUInt64Ty);
3510     addFieldToRecordDecl(C, RD, KmpUInt64Ty);
3511     addFieldToRecordDecl(C, RD, KmpInt64Ty);
3512     addFieldToRecordDecl(C, RD, KmpInt32Ty);
3513     addFieldToRecordDecl(C, RD, C.VoidPtrTy);
3514   }
3515   RD->completeDefinition();
3516   return RD;
3517 }
3518 
3519 static RecordDecl *
3520 createKmpTaskTWithPrivatesRecordDecl(CodeGenModule &CGM, QualType KmpTaskTQTy,
3521                                      ArrayRef<PrivateDataTy> Privates) {
3522   ASTContext &C = CGM.getContext();
3523   // Build struct kmp_task_t_with_privates {
3524   //         kmp_task_t task_data;
3525   //         .kmp_privates_t. privates;
3526   //       };
3527   RecordDecl *RD = C.buildImplicitRecord("kmp_task_t_with_privates");
3528   RD->startDefinition();
3529   addFieldToRecordDecl(C, RD, KmpTaskTQTy);
3530   if (const RecordDecl *PrivateRD = createPrivatesRecordDecl(CGM, Privates))
3531     addFieldToRecordDecl(C, RD, C.getRecordType(PrivateRD));
3532   RD->completeDefinition();
3533   return RD;
3534 }
3535 
3536 /// Emit a proxy function which accepts kmp_task_t as the second
3537 /// argument.
3538 /// \code
3539 /// kmp_int32 .omp_task_entry.(kmp_int32 gtid, kmp_task_t *tt) {
3540 ///   TaskFunction(gtid, tt->part_id, &tt->privates, task_privates_map, tt,
3541 ///   For taskloops:
3542 ///   tt->task_data.lb, tt->task_data.ub, tt->task_data.st, tt->task_data.liter,
3543 ///   tt->reductions, tt->shareds);
3544 ///   return 0;
3545 /// }
3546 /// \endcode
3547 static llvm::Function *
3548 emitProxyTaskFunction(CodeGenModule &CGM, SourceLocation Loc,
3549                       OpenMPDirectiveKind Kind, QualType KmpInt32Ty,
3550                       QualType KmpTaskTWithPrivatesPtrQTy,
3551                       QualType KmpTaskTWithPrivatesQTy, QualType KmpTaskTQTy,
3552                       QualType SharedsPtrTy, llvm::Function *TaskFunction,
3553                       llvm::Value *TaskPrivatesMap) {
3554   ASTContext &C = CGM.getContext();
3555   FunctionArgList Args;
3556   ImplicitParamDecl GtidArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, KmpInt32Ty,
3557                             ImplicitParamDecl::Other);
3558   ImplicitParamDecl TaskTypeArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr,
3559                                 KmpTaskTWithPrivatesPtrQTy.withRestrict(),
3560                                 ImplicitParamDecl::Other);
3561   Args.push_back(&GtidArg);
3562   Args.push_back(&TaskTypeArg);
3563   const auto &TaskEntryFnInfo =
3564       CGM.getTypes().arrangeBuiltinFunctionDeclaration(KmpInt32Ty, Args);
3565   llvm::FunctionType *TaskEntryTy =
3566       CGM.getTypes().GetFunctionType(TaskEntryFnInfo);
3567   std::string Name = CGM.getOpenMPRuntime().getName({"omp_task_entry", ""});
3568   auto *TaskEntry = llvm::Function::Create(
3569       TaskEntryTy, llvm::GlobalValue::InternalLinkage, Name, &CGM.getModule());
3570   CGM.SetInternalFunctionAttributes(GlobalDecl(), TaskEntry, TaskEntryFnInfo);
3571   TaskEntry->setDoesNotRecurse();
3572   CodeGenFunction CGF(CGM);
3573   CGF.StartFunction(GlobalDecl(), KmpInt32Ty, TaskEntry, TaskEntryFnInfo, Args,
3574                     Loc, Loc);
3575 
3576   // TaskFunction(gtid, tt->task_data.part_id, &tt->privates, task_privates_map,
3577   // tt,
3578   // For taskloops:
3579   // tt->task_data.lb, tt->task_data.ub, tt->task_data.st, tt->task_data.liter,
3580   // tt->task_data.shareds);
3581   llvm::Value *GtidParam = CGF.EmitLoadOfScalar(
3582       CGF.GetAddrOfLocalVar(&GtidArg), /*Volatile=*/false, KmpInt32Ty, Loc);
3583   LValue TDBase = CGF.EmitLoadOfPointerLValue(
3584       CGF.GetAddrOfLocalVar(&TaskTypeArg),
3585       KmpTaskTWithPrivatesPtrQTy->castAs<PointerType>());
3586   const auto *KmpTaskTWithPrivatesQTyRD =
3587       cast<RecordDecl>(KmpTaskTWithPrivatesQTy->getAsTagDecl());
3588   LValue Base =
3589       CGF.EmitLValueForField(TDBase, *KmpTaskTWithPrivatesQTyRD->field_begin());
3590   const auto *KmpTaskTQTyRD = cast<RecordDecl>(KmpTaskTQTy->getAsTagDecl());
3591   auto PartIdFI = std::next(KmpTaskTQTyRD->field_begin(), KmpTaskTPartId);
3592   LValue PartIdLVal = CGF.EmitLValueForField(Base, *PartIdFI);
3593   llvm::Value *PartidParam = PartIdLVal.getPointer(CGF);
3594 
3595   auto SharedsFI = std::next(KmpTaskTQTyRD->field_begin(), KmpTaskTShareds);
3596   LValue SharedsLVal = CGF.EmitLValueForField(Base, *SharedsFI);
3597   llvm::Value *SharedsParam = CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(
3598       CGF.EmitLoadOfScalar(SharedsLVal, Loc),
3599       CGF.ConvertTypeForMem(SharedsPtrTy));
3600 
3601   auto PrivatesFI = std::next(KmpTaskTWithPrivatesQTyRD->field_begin(), 1);
3602   llvm::Value *PrivatesParam;
3603   if (PrivatesFI != KmpTaskTWithPrivatesQTyRD->field_end()) {
3604     LValue PrivatesLVal = CGF.EmitLValueForField(TDBase, *PrivatesFI);
3605     PrivatesParam = CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(
3606         PrivatesLVal.getPointer(CGF), CGF.VoidPtrTy);
3607   } else {
3608     PrivatesParam = llvm::ConstantPointerNull::get(CGF.VoidPtrTy);
3609   }
3610 
3611   llvm::Value *CommonArgs[] = {GtidParam, PartidParam, PrivatesParam,
3612                                TaskPrivatesMap,
3613                                CGF.Builder
3614                                    .CreatePointerBitCastOrAddrSpaceCast(
3615                                        TDBase.getAddress(CGF), CGF.VoidPtrTy)
3616                                    .getPointer()};
3617   SmallVector<llvm::Value *, 16> CallArgs(std::begin(CommonArgs),
3618                                           std::end(CommonArgs));
3619   if (isOpenMPTaskLoopDirective(Kind)) {
3620     auto LBFI = std::next(KmpTaskTQTyRD->field_begin(), KmpTaskTLowerBound);
3621     LValue LBLVal = CGF.EmitLValueForField(Base, *LBFI);
3622     llvm::Value *LBParam = CGF.EmitLoadOfScalar(LBLVal, Loc);
3623     auto UBFI = std::next(KmpTaskTQTyRD->field_begin(), KmpTaskTUpperBound);
3624     LValue UBLVal = CGF.EmitLValueForField(Base, *UBFI);
3625     llvm::Value *UBParam = CGF.EmitLoadOfScalar(UBLVal, Loc);
3626     auto StFI = std::next(KmpTaskTQTyRD->field_begin(), KmpTaskTStride);
3627     LValue StLVal = CGF.EmitLValueForField(Base, *StFI);
3628     llvm::Value *StParam = CGF.EmitLoadOfScalar(StLVal, Loc);
3629     auto LIFI = std::next(KmpTaskTQTyRD->field_begin(), KmpTaskTLastIter);
3630     LValue LILVal = CGF.EmitLValueForField(Base, *LIFI);
3631     llvm::Value *LIParam = CGF.EmitLoadOfScalar(LILVal, Loc);
3632     auto RFI = std::next(KmpTaskTQTyRD->field_begin(), KmpTaskTReductions);
3633     LValue RLVal = CGF.EmitLValueForField(Base, *RFI);
3634     llvm::Value *RParam = CGF.EmitLoadOfScalar(RLVal, Loc);
3635     CallArgs.push_back(LBParam);
3636     CallArgs.push_back(UBParam);
3637     CallArgs.push_back(StParam);
3638     CallArgs.push_back(LIParam);
3639     CallArgs.push_back(RParam);
3640   }
3641   CallArgs.push_back(SharedsParam);
3642 
3643   CGM.getOpenMPRuntime().emitOutlinedFunctionCall(CGF, Loc, TaskFunction,
3644                                                   CallArgs);
3645   CGF.EmitStoreThroughLValue(RValue::get(CGF.Builder.getInt32(/*C=*/0)),
3646                              CGF.MakeAddrLValue(CGF.ReturnValue, KmpInt32Ty));
3647   CGF.FinishFunction();
3648   return TaskEntry;
3649 }
3650 
3651 static llvm::Value *emitDestructorsFunction(CodeGenModule &CGM,
3652                                             SourceLocation Loc,
3653                                             QualType KmpInt32Ty,
3654                                             QualType KmpTaskTWithPrivatesPtrQTy,
3655                                             QualType KmpTaskTWithPrivatesQTy) {
3656   ASTContext &C = CGM.getContext();
3657   FunctionArgList Args;
3658   ImplicitParamDecl GtidArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, KmpInt32Ty,
3659                             ImplicitParamDecl::Other);
3660   ImplicitParamDecl TaskTypeArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr,
3661                                 KmpTaskTWithPrivatesPtrQTy.withRestrict(),
3662                                 ImplicitParamDecl::Other);
3663   Args.push_back(&GtidArg);
3664   Args.push_back(&TaskTypeArg);
3665   const auto &DestructorFnInfo =
3666       CGM.getTypes().arrangeBuiltinFunctionDeclaration(KmpInt32Ty, Args);
3667   llvm::FunctionType *DestructorFnTy =
3668       CGM.getTypes().GetFunctionType(DestructorFnInfo);
3669   std::string Name =
3670       CGM.getOpenMPRuntime().getName({"omp_task_destructor", ""});
3671   auto *DestructorFn =
3672       llvm::Function::Create(DestructorFnTy, llvm::GlobalValue::InternalLinkage,
3673                              Name, &CGM.getModule());
3674   CGM.SetInternalFunctionAttributes(GlobalDecl(), DestructorFn,
3675                                     DestructorFnInfo);
3676   DestructorFn->setDoesNotRecurse();
3677   CodeGenFunction CGF(CGM);
3678   CGF.StartFunction(GlobalDecl(), KmpInt32Ty, DestructorFn, DestructorFnInfo,
3679                     Args, Loc, Loc);
3680 
3681   LValue Base = CGF.EmitLoadOfPointerLValue(
3682       CGF.GetAddrOfLocalVar(&TaskTypeArg),
3683       KmpTaskTWithPrivatesPtrQTy->castAs<PointerType>());
3684   const auto *KmpTaskTWithPrivatesQTyRD =
3685       cast<RecordDecl>(KmpTaskTWithPrivatesQTy->getAsTagDecl());
3686   auto FI = std::next(KmpTaskTWithPrivatesQTyRD->field_begin());
3687   Base = CGF.EmitLValueForField(Base, *FI);
3688   for (const auto *Field :
3689        cast<RecordDecl>(FI->getType()->getAsTagDecl())->fields()) {
3690     if (QualType::DestructionKind DtorKind =
3691             Field->getType().isDestructedType()) {
3692       LValue FieldLValue = CGF.EmitLValueForField(Base, Field);
3693       CGF.pushDestroy(DtorKind, FieldLValue.getAddress(CGF), Field->getType());
3694     }
3695   }
3696   CGF.FinishFunction();
3697   return DestructorFn;
3698 }
3699 
3700 /// Emit a privates mapping function for correct handling of private and
3701 /// firstprivate variables.
3702 /// \code
3703 /// void .omp_task_privates_map.(const .privates. *noalias privs, <ty1>
3704 /// **noalias priv1,...,  <tyn> **noalias privn) {
3705 ///   *priv1 = &.privates.priv1;
3706 ///   ...;
3707 ///   *privn = &.privates.privn;
3708 /// }
3709 /// \endcode
3710 static llvm::Value *
3711 emitTaskPrivateMappingFunction(CodeGenModule &CGM, SourceLocation Loc,
3712                                ArrayRef<const Expr *> PrivateVars,
3713                                ArrayRef<const Expr *> FirstprivateVars,
3714                                ArrayRef<const Expr *> LastprivateVars,
3715                                QualType PrivatesQTy,
3716                                ArrayRef<PrivateDataTy> Privates) {
3717   ASTContext &C = CGM.getContext();
3718   FunctionArgList Args;
3719   ImplicitParamDecl TaskPrivatesArg(
3720       C, /*DC=*/nullptr, Loc, /*Id=*/nullptr,
3721       C.getPointerType(PrivatesQTy).withConst().withRestrict(),
3722       ImplicitParamDecl::Other);
3723   Args.push_back(&TaskPrivatesArg);
3724   llvm::DenseMap<const VarDecl *, unsigned> PrivateVarsPos;
3725   unsigned Counter = 1;
3726   for (const Expr *E : PrivateVars) {
3727     Args.push_back(ImplicitParamDecl::Create(
3728         C, /*DC=*/nullptr, Loc, /*Id=*/nullptr,
3729         C.getPointerType(C.getPointerType(E->getType()))
3730             .withConst()
3731             .withRestrict(),
3732         ImplicitParamDecl::Other));
3733     const auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl());
3734     PrivateVarsPos[VD] = Counter;
3735     ++Counter;
3736   }
3737   for (const Expr *E : FirstprivateVars) {
3738     Args.push_back(ImplicitParamDecl::Create(
3739         C, /*DC=*/nullptr, Loc, /*Id=*/nullptr,
3740         C.getPointerType(C.getPointerType(E->getType()))
3741             .withConst()
3742             .withRestrict(),
3743         ImplicitParamDecl::Other));
3744     const auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl());
3745     PrivateVarsPos[VD] = Counter;
3746     ++Counter;
3747   }
3748   for (const Expr *E : LastprivateVars) {
3749     Args.push_back(ImplicitParamDecl::Create(
3750         C, /*DC=*/nullptr, Loc, /*Id=*/nullptr,
3751         C.getPointerType(C.getPointerType(E->getType()))
3752             .withConst()
3753             .withRestrict(),
3754         ImplicitParamDecl::Other));
3755     const auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl());
3756     PrivateVarsPos[VD] = Counter;
3757     ++Counter;
3758   }
3759   const auto &TaskPrivatesMapFnInfo =
3760       CGM.getTypes().arrangeBuiltinFunctionDeclaration(C.VoidTy, Args);
3761   llvm::FunctionType *TaskPrivatesMapTy =
3762       CGM.getTypes().GetFunctionType(TaskPrivatesMapFnInfo);
3763   std::string Name =
3764       CGM.getOpenMPRuntime().getName({"omp_task_privates_map", ""});
3765   auto *TaskPrivatesMap = llvm::Function::Create(
3766       TaskPrivatesMapTy, llvm::GlobalValue::InternalLinkage, Name,
3767       &CGM.getModule());
3768   CGM.SetInternalFunctionAttributes(GlobalDecl(), TaskPrivatesMap,
3769                                     TaskPrivatesMapFnInfo);
3770   if (CGM.getLangOpts().Optimize) {
3771     TaskPrivatesMap->removeFnAttr(llvm::Attribute::NoInline);
3772     TaskPrivatesMap->removeFnAttr(llvm::Attribute::OptimizeNone);
3773     TaskPrivatesMap->addFnAttr(llvm::Attribute::AlwaysInline);
3774   }
3775   CodeGenFunction CGF(CGM);
3776   CGF.StartFunction(GlobalDecl(), C.VoidTy, TaskPrivatesMap,
3777                     TaskPrivatesMapFnInfo, Args, Loc, Loc);
3778 
3779   // *privi = &.privates.privi;
3780   LValue Base = CGF.EmitLoadOfPointerLValue(
3781       CGF.GetAddrOfLocalVar(&TaskPrivatesArg),
3782       TaskPrivatesArg.getType()->castAs<PointerType>());
3783   const auto *PrivatesQTyRD = cast<RecordDecl>(PrivatesQTy->getAsTagDecl());
3784   Counter = 0;
3785   for (const FieldDecl *Field : PrivatesQTyRD->fields()) {
3786     LValue FieldLVal = CGF.EmitLValueForField(Base, Field);
3787     const VarDecl *VD = Args[PrivateVarsPos[Privates[Counter].second.Original]];
3788     LValue RefLVal =
3789         CGF.MakeAddrLValue(CGF.GetAddrOfLocalVar(VD), VD->getType());
3790     LValue RefLoadLVal = CGF.EmitLoadOfPointerLValue(
3791         RefLVal.getAddress(CGF), RefLVal.getType()->castAs<PointerType>());
3792     CGF.EmitStoreOfScalar(FieldLVal.getPointer(CGF), RefLoadLVal);
3793     ++Counter;
3794   }
3795   CGF.FinishFunction();
3796   return TaskPrivatesMap;
3797 }
3798 
3799 /// Emit initialization for private variables in task-based directives.
3800 static void emitPrivatesInit(CodeGenFunction &CGF,
3801                              const OMPExecutableDirective &D,
3802                              Address KmpTaskSharedsPtr, LValue TDBase,
3803                              const RecordDecl *KmpTaskTWithPrivatesQTyRD,
3804                              QualType SharedsTy, QualType SharedsPtrTy,
3805                              const OMPTaskDataTy &Data,
3806                              ArrayRef<PrivateDataTy> Privates, bool ForDup) {
3807   ASTContext &C = CGF.getContext();
3808   auto FI = std::next(KmpTaskTWithPrivatesQTyRD->field_begin());
3809   LValue PrivatesBase = CGF.EmitLValueForField(TDBase, *FI);
3810   OpenMPDirectiveKind Kind = isOpenMPTaskLoopDirective(D.getDirectiveKind())
3811                                  ? OMPD_taskloop
3812                                  : OMPD_task;
3813   const CapturedStmt &CS = *D.getCapturedStmt(Kind);
3814   CodeGenFunction::CGCapturedStmtInfo CapturesInfo(CS);
3815   LValue SrcBase;
3816   bool IsTargetTask =
3817       isOpenMPTargetDataManagementDirective(D.getDirectiveKind()) ||
3818       isOpenMPTargetExecutionDirective(D.getDirectiveKind());
3819   // For target-based directives skip 3 firstprivate arrays BasePointersArray,
3820   // PointersArray and SizesArray. The original variables for these arrays are
3821   // not captured and we get their addresses explicitly.
3822   if ((!IsTargetTask && !Data.FirstprivateVars.empty() && ForDup) ||
3823       (IsTargetTask && KmpTaskSharedsPtr.isValid())) {
3824     SrcBase = CGF.MakeAddrLValue(
3825         CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(
3826             KmpTaskSharedsPtr, CGF.ConvertTypeForMem(SharedsPtrTy)),
3827         SharedsTy);
3828   }
3829   FI = cast<RecordDecl>(FI->getType()->getAsTagDecl())->field_begin();
3830   for (const PrivateDataTy &Pair : Privates) {
3831     const VarDecl *VD = Pair.second.PrivateCopy;
3832     const Expr *Init = VD->getAnyInitializer();
3833     if (Init && (!ForDup || (isa<CXXConstructExpr>(Init) &&
3834                              !CGF.isTrivialInitializer(Init)))) {
3835       LValue PrivateLValue = CGF.EmitLValueForField(PrivatesBase, *FI);
3836       if (const VarDecl *Elem = Pair.second.PrivateElemInit) {
3837         const VarDecl *OriginalVD = Pair.second.Original;
3838         // Check if the variable is the target-based BasePointersArray,
3839         // PointersArray or SizesArray.
3840         LValue SharedRefLValue;
3841         QualType Type = PrivateLValue.getType();
3842         const FieldDecl *SharedField = CapturesInfo.lookup(OriginalVD);
3843         if (IsTargetTask && !SharedField) {
3844           assert(isa<ImplicitParamDecl>(OriginalVD) &&
3845                  isa<CapturedDecl>(OriginalVD->getDeclContext()) &&
3846                  cast<CapturedDecl>(OriginalVD->getDeclContext())
3847                          ->getNumParams() == 0 &&
3848                  isa<TranslationUnitDecl>(
3849                      cast<CapturedDecl>(OriginalVD->getDeclContext())
3850                          ->getDeclContext()) &&
3851                  "Expected artificial target data variable.");
3852           SharedRefLValue =
3853               CGF.MakeAddrLValue(CGF.GetAddrOfLocalVar(OriginalVD), Type);
3854         } else if (ForDup) {
3855           SharedRefLValue = CGF.EmitLValueForField(SrcBase, SharedField);
3856           SharedRefLValue = CGF.MakeAddrLValue(
3857               Address(SharedRefLValue.getPointer(CGF),
3858                       C.getDeclAlign(OriginalVD)),
3859               SharedRefLValue.getType(), LValueBaseInfo(AlignmentSource::Decl),
3860               SharedRefLValue.getTBAAInfo());
3861         } else if (CGF.LambdaCaptureFields.count(
3862                        Pair.second.Original->getCanonicalDecl()) > 0 ||
3863                    dyn_cast_or_null<BlockDecl>(CGF.CurCodeDecl)) {
3864           SharedRefLValue = CGF.EmitLValue(Pair.second.OriginalRef);
3865         } else {
3866           // Processing for implicitly captured variables.
3867           InlinedOpenMPRegionRAII Region(
3868               CGF, [](CodeGenFunction &, PrePostActionTy &) {}, OMPD_unknown,
3869               /*HasCancel=*/false);
3870           SharedRefLValue = CGF.EmitLValue(Pair.second.OriginalRef);
3871         }
3872         if (Type->isArrayType()) {
3873           // Initialize firstprivate array.
3874           if (!isa<CXXConstructExpr>(Init) || CGF.isTrivialInitializer(Init)) {
3875             // Perform simple memcpy.
3876             CGF.EmitAggregateAssign(PrivateLValue, SharedRefLValue, Type);
3877           } else {
3878             // Initialize firstprivate array using element-by-element
3879             // initialization.
3880             CGF.EmitOMPAggregateAssign(
3881                 PrivateLValue.getAddress(CGF), SharedRefLValue.getAddress(CGF),
3882                 Type,
3883                 [&CGF, Elem, Init, &CapturesInfo](Address DestElement,
3884                                                   Address SrcElement) {
3885                   // Clean up any temporaries needed by the initialization.
3886                   CodeGenFunction::OMPPrivateScope InitScope(CGF);
3887                   InitScope.addPrivate(
3888                       Elem, [SrcElement]() -> Address { return SrcElement; });
3889                   (void)InitScope.Privatize();
3890                   // Emit initialization for single element.
3891                   CodeGenFunction::CGCapturedStmtRAII CapInfoRAII(
3892                       CGF, &CapturesInfo);
3893                   CGF.EmitAnyExprToMem(Init, DestElement,
3894                                        Init->getType().getQualifiers(),
3895                                        /*IsInitializer=*/false);
3896                 });
3897           }
3898         } else {
3899           CodeGenFunction::OMPPrivateScope InitScope(CGF);
3900           InitScope.addPrivate(Elem, [SharedRefLValue, &CGF]() -> Address {
3901             return SharedRefLValue.getAddress(CGF);
3902           });
3903           (void)InitScope.Privatize();
3904           CodeGenFunction::CGCapturedStmtRAII CapInfoRAII(CGF, &CapturesInfo);
3905           CGF.EmitExprAsInit(Init, VD, PrivateLValue,
3906                              /*capturedByInit=*/false);
3907         }
3908       } else {
3909         CGF.EmitExprAsInit(Init, VD, PrivateLValue, /*capturedByInit=*/false);
3910       }
3911     }
3912     ++FI;
3913   }
3914 }
3915 
3916 /// Check if duplication function is required for taskloops.
3917 static bool checkInitIsRequired(CodeGenFunction &CGF,
3918                                 ArrayRef<PrivateDataTy> Privates) {
3919   bool InitRequired = false;
3920   for (const PrivateDataTy &Pair : Privates) {
3921     const VarDecl *VD = Pair.second.PrivateCopy;
3922     const Expr *Init = VD->getAnyInitializer();
3923     InitRequired = InitRequired || (Init && isa<CXXConstructExpr>(Init) &&
3924                                     !CGF.isTrivialInitializer(Init));
3925     if (InitRequired)
3926       break;
3927   }
3928   return InitRequired;
3929 }
3930 
3931 
3932 /// Emit task_dup function (for initialization of
3933 /// private/firstprivate/lastprivate vars and last_iter flag)
3934 /// \code
3935 /// void __task_dup_entry(kmp_task_t *task_dst, const kmp_task_t *task_src, int
3936 /// lastpriv) {
3937 /// // setup lastprivate flag
3938 ///    task_dst->last = lastpriv;
3939 /// // could be constructor calls here...
3940 /// }
3941 /// \endcode
3942 static llvm::Value *
3943 emitTaskDupFunction(CodeGenModule &CGM, SourceLocation Loc,
3944                     const OMPExecutableDirective &D,
3945                     QualType KmpTaskTWithPrivatesPtrQTy,
3946                     const RecordDecl *KmpTaskTWithPrivatesQTyRD,
3947                     const RecordDecl *KmpTaskTQTyRD, QualType SharedsTy,
3948                     QualType SharedsPtrTy, const OMPTaskDataTy &Data,
3949                     ArrayRef<PrivateDataTy> Privates, bool WithLastIter) {
3950   ASTContext &C = CGM.getContext();
3951   FunctionArgList Args;
3952   ImplicitParamDecl DstArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr,
3953                            KmpTaskTWithPrivatesPtrQTy,
3954                            ImplicitParamDecl::Other);
3955   ImplicitParamDecl SrcArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr,
3956                            KmpTaskTWithPrivatesPtrQTy,
3957                            ImplicitParamDecl::Other);
3958   ImplicitParamDecl LastprivArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, C.IntTy,
3959                                 ImplicitParamDecl::Other);
3960   Args.push_back(&DstArg);
3961   Args.push_back(&SrcArg);
3962   Args.push_back(&LastprivArg);
3963   const auto &TaskDupFnInfo =
3964       CGM.getTypes().arrangeBuiltinFunctionDeclaration(C.VoidTy, Args);
3965   llvm::FunctionType *TaskDupTy = CGM.getTypes().GetFunctionType(TaskDupFnInfo);
3966   std::string Name = CGM.getOpenMPRuntime().getName({"omp_task_dup", ""});
3967   auto *TaskDup = llvm::Function::Create(
3968       TaskDupTy, llvm::GlobalValue::InternalLinkage, Name, &CGM.getModule());
3969   CGM.SetInternalFunctionAttributes(GlobalDecl(), TaskDup, TaskDupFnInfo);
3970   TaskDup->setDoesNotRecurse();
3971   CodeGenFunction CGF(CGM);
3972   CGF.StartFunction(GlobalDecl(), C.VoidTy, TaskDup, TaskDupFnInfo, Args, Loc,
3973                     Loc);
3974 
3975   LValue TDBase = CGF.EmitLoadOfPointerLValue(
3976       CGF.GetAddrOfLocalVar(&DstArg),
3977       KmpTaskTWithPrivatesPtrQTy->castAs<PointerType>());
3978   // task_dst->liter = lastpriv;
3979   if (WithLastIter) {
3980     auto LIFI = std::next(KmpTaskTQTyRD->field_begin(), KmpTaskTLastIter);
3981     LValue Base = CGF.EmitLValueForField(
3982         TDBase, *KmpTaskTWithPrivatesQTyRD->field_begin());
3983     LValue LILVal = CGF.EmitLValueForField(Base, *LIFI);
3984     llvm::Value *Lastpriv = CGF.EmitLoadOfScalar(
3985         CGF.GetAddrOfLocalVar(&LastprivArg), /*Volatile=*/false, C.IntTy, Loc);
3986     CGF.EmitStoreOfScalar(Lastpriv, LILVal);
3987   }
3988 
3989   // Emit initial values for private copies (if any).
3990   assert(!Privates.empty());
3991   Address KmpTaskSharedsPtr = Address::invalid();
3992   if (!Data.FirstprivateVars.empty()) {
3993     LValue TDBase = CGF.EmitLoadOfPointerLValue(
3994         CGF.GetAddrOfLocalVar(&SrcArg),
3995         KmpTaskTWithPrivatesPtrQTy->castAs<PointerType>());
3996     LValue Base = CGF.EmitLValueForField(
3997         TDBase, *KmpTaskTWithPrivatesQTyRD->field_begin());
3998     KmpTaskSharedsPtr = Address(
3999         CGF.EmitLoadOfScalar(CGF.EmitLValueForField(
4000                                  Base, *std::next(KmpTaskTQTyRD->field_begin(),
4001                                                   KmpTaskTShareds)),
4002                              Loc),
4003         CGM.getNaturalTypeAlignment(SharedsTy));
4004   }
4005   emitPrivatesInit(CGF, D, KmpTaskSharedsPtr, TDBase, KmpTaskTWithPrivatesQTyRD,
4006                    SharedsTy, SharedsPtrTy, Data, Privates, /*ForDup=*/true);
4007   CGF.FinishFunction();
4008   return TaskDup;
4009 }
4010 
4011 /// Checks if destructor function is required to be generated.
4012 /// \return true if cleanups are required, false otherwise.
4013 static bool
4014 checkDestructorsRequired(const RecordDecl *KmpTaskTWithPrivatesQTyRD) {
4015   bool NeedsCleanup = false;
4016   auto FI = std::next(KmpTaskTWithPrivatesQTyRD->field_begin(), 1);
4017   const auto *PrivateRD = cast<RecordDecl>(FI->getType()->getAsTagDecl());
4018   for (const FieldDecl *FD : PrivateRD->fields()) {
4019     NeedsCleanup = NeedsCleanup || FD->getType().isDestructedType();
4020     if (NeedsCleanup)
4021       break;
4022   }
4023   return NeedsCleanup;
4024 }
4025 
4026 namespace {
4027 /// Loop generator for OpenMP iterator expression.
4028 class OMPIteratorGeneratorScope final
4029     : public CodeGenFunction::OMPPrivateScope {
4030   CodeGenFunction &CGF;
4031   const OMPIteratorExpr *E = nullptr;
4032   SmallVector<CodeGenFunction::JumpDest, 4> ContDests;
4033   SmallVector<CodeGenFunction::JumpDest, 4> ExitDests;
4034   OMPIteratorGeneratorScope() = delete;
4035   OMPIteratorGeneratorScope(OMPIteratorGeneratorScope &) = delete;
4036 
4037 public:
4038   OMPIteratorGeneratorScope(CodeGenFunction &CGF, const OMPIteratorExpr *E)
4039       : CodeGenFunction::OMPPrivateScope(CGF), CGF(CGF), E(E) {
4040     if (!E)
4041       return;
4042     SmallVector<llvm::Value *, 4> Uppers;
4043     for (unsigned I = 0, End = E->numOfIterators(); I < End; ++I) {
4044       Uppers.push_back(CGF.EmitScalarExpr(E->getHelper(I).Upper));
4045       const auto *VD = cast<VarDecl>(E->getIteratorDecl(I));
4046       addPrivate(VD, [&CGF, VD]() {
4047         return CGF.CreateMemTemp(VD->getType(), VD->getName());
4048       });
4049       const OMPIteratorHelperData &HelperData = E->getHelper(I);
4050       addPrivate(HelperData.CounterVD, [&CGF, &HelperData]() {
4051         return CGF.CreateMemTemp(HelperData.CounterVD->getType(),
4052                                  "counter.addr");
4053       });
4054     }
4055     Privatize();
4056 
4057     for (unsigned I = 0, End = E->numOfIterators(); I < End; ++I) {
4058       const OMPIteratorHelperData &HelperData = E->getHelper(I);
4059       LValue CLVal =
4060           CGF.MakeAddrLValue(CGF.GetAddrOfLocalVar(HelperData.CounterVD),
4061                              HelperData.CounterVD->getType());
4062       // Counter = 0;
4063       CGF.EmitStoreOfScalar(
4064           llvm::ConstantInt::get(CLVal.getAddress(CGF).getElementType(), 0),
4065           CLVal);
4066       CodeGenFunction::JumpDest &ContDest =
4067           ContDests.emplace_back(CGF.getJumpDestInCurrentScope("iter.cont"));
4068       CodeGenFunction::JumpDest &ExitDest =
4069           ExitDests.emplace_back(CGF.getJumpDestInCurrentScope("iter.exit"));
4070       // N = <number-of_iterations>;
4071       llvm::Value *N = Uppers[I];
4072       // cont:
4073       // if (Counter < N) goto body; else goto exit;
4074       CGF.EmitBlock(ContDest.getBlock());
4075       auto *CVal =
4076           CGF.EmitLoadOfScalar(CLVal, HelperData.CounterVD->getLocation());
4077       llvm::Value *Cmp =
4078           HelperData.CounterVD->getType()->isSignedIntegerOrEnumerationType()
4079               ? CGF.Builder.CreateICmpSLT(CVal, N)
4080               : CGF.Builder.CreateICmpULT(CVal, N);
4081       llvm::BasicBlock *BodyBB = CGF.createBasicBlock("iter.body");
4082       CGF.Builder.CreateCondBr(Cmp, BodyBB, ExitDest.getBlock());
4083       // body:
4084       CGF.EmitBlock(BodyBB);
4085       // Iteri = Begini + Counter * Stepi;
4086       CGF.EmitIgnoredExpr(HelperData.Update);
4087     }
4088   }
4089   ~OMPIteratorGeneratorScope() {
4090     if (!E)
4091       return;
4092     for (unsigned I = E->numOfIterators(); I > 0; --I) {
4093       // Counter = Counter + 1;
4094       const OMPIteratorHelperData &HelperData = E->getHelper(I - 1);
4095       CGF.EmitIgnoredExpr(HelperData.CounterUpdate);
4096       // goto cont;
4097       CGF.EmitBranchThroughCleanup(ContDests[I - 1]);
4098       // exit:
4099       CGF.EmitBlock(ExitDests[I - 1].getBlock(), /*IsFinished=*/I == 1);
4100     }
4101   }
4102 };
4103 } // namespace
4104 
4105 static std::pair<llvm::Value *, llvm::Value *>
4106 getPointerAndSize(CodeGenFunction &CGF, const Expr *E) {
4107   const auto *OASE = dyn_cast<OMPArrayShapingExpr>(E);
4108   llvm::Value *Addr;
4109   if (OASE) {
4110     const Expr *Base = OASE->getBase();
4111     Addr = CGF.EmitScalarExpr(Base);
4112   } else {
4113     Addr = CGF.EmitLValue(E).getPointer(CGF);
4114   }
4115   llvm::Value *SizeVal;
4116   QualType Ty = E->getType();
4117   if (OASE) {
4118     SizeVal = CGF.getTypeSize(OASE->getBase()->getType()->getPointeeType());
4119     for (const Expr *SE : OASE->getDimensions()) {
4120       llvm::Value *Sz = CGF.EmitScalarExpr(SE);
4121       Sz = CGF.EmitScalarConversion(
4122           Sz, SE->getType(), CGF.getContext().getSizeType(), SE->getExprLoc());
4123       SizeVal = CGF.Builder.CreateNUWMul(SizeVal, Sz);
4124     }
4125   } else if (const auto *ASE =
4126                  dyn_cast<OMPArraySectionExpr>(E->IgnoreParenImpCasts())) {
4127     LValue UpAddrLVal =
4128         CGF.EmitOMPArraySectionExpr(ASE, /*IsLowerBound=*/false);
4129     llvm::Value *UpAddr =
4130         CGF.Builder.CreateConstGEP1_32(UpAddrLVal.getPointer(CGF), /*Idx0=*/1);
4131     llvm::Value *LowIntPtr = CGF.Builder.CreatePtrToInt(Addr, CGF.SizeTy);
4132     llvm::Value *UpIntPtr = CGF.Builder.CreatePtrToInt(UpAddr, CGF.SizeTy);
4133     SizeVal = CGF.Builder.CreateNUWSub(UpIntPtr, LowIntPtr);
4134   } else {
4135     SizeVal = CGF.getTypeSize(Ty);
4136   }
4137   return std::make_pair(Addr, SizeVal);
4138 }
4139 
4140 /// Builds kmp_depend_info, if it is not built yet, and builds flags type.
4141 static void getKmpAffinityType(ASTContext &C, QualType &KmpTaskAffinityInfoTy) {
4142   QualType FlagsTy = C.getIntTypeForBitwidth(32, /*Signed=*/false);
4143   if (KmpTaskAffinityInfoTy.isNull()) {
4144     RecordDecl *KmpAffinityInfoRD =
4145         C.buildImplicitRecord("kmp_task_affinity_info_t");
4146     KmpAffinityInfoRD->startDefinition();
4147     addFieldToRecordDecl(C, KmpAffinityInfoRD, C.getIntPtrType());
4148     addFieldToRecordDecl(C, KmpAffinityInfoRD, C.getSizeType());
4149     addFieldToRecordDecl(C, KmpAffinityInfoRD, FlagsTy);
4150     KmpAffinityInfoRD->completeDefinition();
4151     KmpTaskAffinityInfoTy = C.getRecordType(KmpAffinityInfoRD);
4152   }
4153 }
4154 
4155 CGOpenMPRuntime::TaskResultTy
4156 CGOpenMPRuntime::emitTaskInit(CodeGenFunction &CGF, SourceLocation Loc,
4157                               const OMPExecutableDirective &D,
4158                               llvm::Function *TaskFunction, QualType SharedsTy,
4159                               Address Shareds, const OMPTaskDataTy &Data) {
4160   ASTContext &C = CGM.getContext();
4161   llvm::SmallVector<PrivateDataTy, 4> Privates;
4162   // Aggregate privates and sort them by the alignment.
4163   const auto *I = Data.PrivateCopies.begin();
4164   for (const Expr *E : Data.PrivateVars) {
4165     const auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl());
4166     Privates.emplace_back(
4167         C.getDeclAlign(VD),
4168         PrivateHelpersTy(E, VD, cast<VarDecl>(cast<DeclRefExpr>(*I)->getDecl()),
4169                          /*PrivateElemInit=*/nullptr));
4170     ++I;
4171   }
4172   I = Data.FirstprivateCopies.begin();
4173   const auto *IElemInitRef = Data.FirstprivateInits.begin();
4174   for (const Expr *E : Data.FirstprivateVars) {
4175     const auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl());
4176     Privates.emplace_back(
4177         C.getDeclAlign(VD),
4178         PrivateHelpersTy(
4179             E, VD, cast<VarDecl>(cast<DeclRefExpr>(*I)->getDecl()),
4180             cast<VarDecl>(cast<DeclRefExpr>(*IElemInitRef)->getDecl())));
4181     ++I;
4182     ++IElemInitRef;
4183   }
4184   I = Data.LastprivateCopies.begin();
4185   for (const Expr *E : Data.LastprivateVars) {
4186     const auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl());
4187     Privates.emplace_back(
4188         C.getDeclAlign(VD),
4189         PrivateHelpersTy(E, VD, cast<VarDecl>(cast<DeclRefExpr>(*I)->getDecl()),
4190                          /*PrivateElemInit=*/nullptr));
4191     ++I;
4192   }
4193   llvm::stable_sort(Privates, [](PrivateDataTy L, PrivateDataTy R) {
4194     return L.first > R.first;
4195   });
4196   QualType KmpInt32Ty = C.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1);
4197   // Build type kmp_routine_entry_t (if not built yet).
4198   emitKmpRoutineEntryT(KmpInt32Ty);
4199   // Build type kmp_task_t (if not built yet).
4200   if (isOpenMPTaskLoopDirective(D.getDirectiveKind())) {
4201     if (SavedKmpTaskloopTQTy.isNull()) {
4202       SavedKmpTaskloopTQTy = C.getRecordType(createKmpTaskTRecordDecl(
4203           CGM, D.getDirectiveKind(), KmpInt32Ty, KmpRoutineEntryPtrQTy));
4204     }
4205     KmpTaskTQTy = SavedKmpTaskloopTQTy;
4206   } else {
4207     assert((D.getDirectiveKind() == OMPD_task ||
4208             isOpenMPTargetExecutionDirective(D.getDirectiveKind()) ||
4209             isOpenMPTargetDataManagementDirective(D.getDirectiveKind())) &&
4210            "Expected taskloop, task or target directive");
4211     if (SavedKmpTaskTQTy.isNull()) {
4212       SavedKmpTaskTQTy = C.getRecordType(createKmpTaskTRecordDecl(
4213           CGM, D.getDirectiveKind(), KmpInt32Ty, KmpRoutineEntryPtrQTy));
4214     }
4215     KmpTaskTQTy = SavedKmpTaskTQTy;
4216   }
4217   const auto *KmpTaskTQTyRD = cast<RecordDecl>(KmpTaskTQTy->getAsTagDecl());
4218   // Build particular struct kmp_task_t for the given task.
4219   const RecordDecl *KmpTaskTWithPrivatesQTyRD =
4220       createKmpTaskTWithPrivatesRecordDecl(CGM, KmpTaskTQTy, Privates);
4221   QualType KmpTaskTWithPrivatesQTy = C.getRecordType(KmpTaskTWithPrivatesQTyRD);
4222   QualType KmpTaskTWithPrivatesPtrQTy =
4223       C.getPointerType(KmpTaskTWithPrivatesQTy);
4224   llvm::Type *KmpTaskTWithPrivatesTy = CGF.ConvertType(KmpTaskTWithPrivatesQTy);
4225   llvm::Type *KmpTaskTWithPrivatesPtrTy =
4226       KmpTaskTWithPrivatesTy->getPointerTo();
4227   llvm::Value *KmpTaskTWithPrivatesTySize =
4228       CGF.getTypeSize(KmpTaskTWithPrivatesQTy);
4229   QualType SharedsPtrTy = C.getPointerType(SharedsTy);
4230 
4231   // Emit initial values for private copies (if any).
4232   llvm::Value *TaskPrivatesMap = nullptr;
4233   llvm::Type *TaskPrivatesMapTy =
4234       std::next(TaskFunction->arg_begin(), 3)->getType();
4235   if (!Privates.empty()) {
4236     auto FI = std::next(KmpTaskTWithPrivatesQTyRD->field_begin());
4237     TaskPrivatesMap = emitTaskPrivateMappingFunction(
4238         CGM, Loc, Data.PrivateVars, Data.FirstprivateVars, Data.LastprivateVars,
4239         FI->getType(), Privates);
4240     TaskPrivatesMap = CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(
4241         TaskPrivatesMap, TaskPrivatesMapTy);
4242   } else {
4243     TaskPrivatesMap = llvm::ConstantPointerNull::get(
4244         cast<llvm::PointerType>(TaskPrivatesMapTy));
4245   }
4246   // Build a proxy function kmp_int32 .omp_task_entry.(kmp_int32 gtid,
4247   // kmp_task_t *tt);
4248   llvm::Function *TaskEntry = emitProxyTaskFunction(
4249       CGM, Loc, D.getDirectiveKind(), KmpInt32Ty, KmpTaskTWithPrivatesPtrQTy,
4250       KmpTaskTWithPrivatesQTy, KmpTaskTQTy, SharedsPtrTy, TaskFunction,
4251       TaskPrivatesMap);
4252 
4253   // Build call kmp_task_t * __kmpc_omp_task_alloc(ident_t *, kmp_int32 gtid,
4254   // kmp_int32 flags, size_t sizeof_kmp_task_t, size_t sizeof_shareds,
4255   // kmp_routine_entry_t *task_entry);
4256   // Task flags. Format is taken from
4257   // https://github.com/llvm/llvm-project/blob/master/openmp/runtime/src/kmp.h,
4258   // description of kmp_tasking_flags struct.
4259   enum {
4260     TiedFlag = 0x1,
4261     FinalFlag = 0x2,
4262     DestructorsFlag = 0x8,
4263     PriorityFlag = 0x20,
4264     DetachableFlag = 0x40,
4265   };
4266   unsigned Flags = Data.Tied ? TiedFlag : 0;
4267   bool NeedsCleanup = false;
4268   if (!Privates.empty()) {
4269     NeedsCleanup = checkDestructorsRequired(KmpTaskTWithPrivatesQTyRD);
4270     if (NeedsCleanup)
4271       Flags = Flags | DestructorsFlag;
4272   }
4273   if (Data.Priority.getInt())
4274     Flags = Flags | PriorityFlag;
4275   if (D.hasClausesOfKind<OMPDetachClause>())
4276     Flags = Flags | DetachableFlag;
4277   llvm::Value *TaskFlags =
4278       Data.Final.getPointer()
4279           ? CGF.Builder.CreateSelect(Data.Final.getPointer(),
4280                                      CGF.Builder.getInt32(FinalFlag),
4281                                      CGF.Builder.getInt32(/*C=*/0))
4282           : CGF.Builder.getInt32(Data.Final.getInt() ? FinalFlag : 0);
4283   TaskFlags = CGF.Builder.CreateOr(TaskFlags, CGF.Builder.getInt32(Flags));
4284   llvm::Value *SharedsSize = CGM.getSize(C.getTypeSizeInChars(SharedsTy));
4285   SmallVector<llvm::Value *, 8> AllocArgs = {emitUpdateLocation(CGF, Loc),
4286       getThreadID(CGF, Loc), TaskFlags, KmpTaskTWithPrivatesTySize,
4287       SharedsSize, CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(
4288           TaskEntry, KmpRoutineEntryPtrTy)};
4289   llvm::Value *NewTask;
4290   if (D.hasClausesOfKind<OMPNowaitClause>()) {
4291     // Check if we have any device clause associated with the directive.
4292     const Expr *Device = nullptr;
4293     if (auto *C = D.getSingleClause<OMPDeviceClause>())
4294       Device = C->getDevice();
4295     // Emit device ID if any otherwise use default value.
4296     llvm::Value *DeviceID;
4297     if (Device)
4298       DeviceID = CGF.Builder.CreateIntCast(CGF.EmitScalarExpr(Device),
4299                                            CGF.Int64Ty, /*isSigned=*/true);
4300     else
4301       DeviceID = CGF.Builder.getInt64(OMP_DEVICEID_UNDEF);
4302     AllocArgs.push_back(DeviceID);
4303     NewTask = CGF.EmitRuntimeCall(
4304         OMPBuilder.getOrCreateRuntimeFunction(
4305             CGM.getModule(), OMPRTL___kmpc_omp_target_task_alloc),
4306         AllocArgs);
4307   } else {
4308     NewTask =
4309         CGF.EmitRuntimeCall(OMPBuilder.getOrCreateRuntimeFunction(
4310                                 CGM.getModule(), OMPRTL___kmpc_omp_task_alloc),
4311                             AllocArgs);
4312   }
4313   // Emit detach clause initialization.
4314   // evt = (typeof(evt))__kmpc_task_allow_completion_event(loc, tid,
4315   // task_descriptor);
4316   if (const auto *DC = D.getSingleClause<OMPDetachClause>()) {
4317     const Expr *Evt = DC->getEventHandler()->IgnoreParenImpCasts();
4318     LValue EvtLVal = CGF.EmitLValue(Evt);
4319 
4320     // Build kmp_event_t *__kmpc_task_allow_completion_event(ident_t *loc_ref,
4321     // int gtid, kmp_task_t *task);
4322     llvm::Value *Loc = emitUpdateLocation(CGF, DC->getBeginLoc());
4323     llvm::Value *Tid = getThreadID(CGF, DC->getBeginLoc());
4324     Tid = CGF.Builder.CreateIntCast(Tid, CGF.IntTy, /*isSigned=*/false);
4325     llvm::Value *EvtVal = CGF.EmitRuntimeCall(
4326         OMPBuilder.getOrCreateRuntimeFunction(
4327             CGM.getModule(), OMPRTL___kmpc_task_allow_completion_event),
4328         {Loc, Tid, NewTask});
4329     EvtVal = CGF.EmitScalarConversion(EvtVal, C.VoidPtrTy, Evt->getType(),
4330                                       Evt->getExprLoc());
4331     CGF.EmitStoreOfScalar(EvtVal, EvtLVal);
4332   }
4333   // Process affinity clauses.
4334   if (D.hasClausesOfKind<OMPAffinityClause>()) {
4335     // Process list of affinity data.
4336     ASTContext &C = CGM.getContext();
4337     Address AffinitiesArray = Address::invalid();
4338     // Calculate number of elements to form the array of affinity data.
4339     llvm::Value *NumOfElements = nullptr;
4340     unsigned NumAffinities = 0;
4341     for (const auto *C : D.getClausesOfKind<OMPAffinityClause>()) {
4342       if (const Expr *Modifier = C->getModifier()) {
4343         const auto *IE = cast<OMPIteratorExpr>(Modifier->IgnoreParenImpCasts());
4344         for (unsigned I = 0, E = IE->numOfIterators(); I < E; ++I) {
4345           llvm::Value *Sz = CGF.EmitScalarExpr(IE->getHelper(I).Upper);
4346           Sz = CGF.Builder.CreateIntCast(Sz, CGF.SizeTy, /*isSigned=*/false);
4347           NumOfElements =
4348               NumOfElements ? CGF.Builder.CreateNUWMul(NumOfElements, Sz) : Sz;
4349         }
4350       } else {
4351         NumAffinities += C->varlist_size();
4352       }
4353     }
4354     getKmpAffinityType(CGM.getContext(), KmpTaskAffinityInfoTy);
4355     // Fields ids in kmp_task_affinity_info record.
4356     enum RTLAffinityInfoFieldsTy { BaseAddr, Len, Flags };
4357 
4358     QualType KmpTaskAffinityInfoArrayTy;
4359     if (NumOfElements) {
4360       NumOfElements = CGF.Builder.CreateNUWAdd(
4361           llvm::ConstantInt::get(CGF.SizeTy, NumAffinities), NumOfElements);
4362       OpaqueValueExpr OVE(
4363           Loc,
4364           C.getIntTypeForBitwidth(C.getTypeSize(C.getSizeType()), /*Signed=*/0),
4365           VK_RValue);
4366       CodeGenFunction::OpaqueValueMapping OpaqueMap(CGF, &OVE,
4367                                                     RValue::get(NumOfElements));
4368       KmpTaskAffinityInfoArrayTy =
4369           C.getVariableArrayType(KmpTaskAffinityInfoTy, &OVE, ArrayType::Normal,
4370                                  /*IndexTypeQuals=*/0, SourceRange(Loc, Loc));
4371       // Properly emit variable-sized array.
4372       auto *PD = ImplicitParamDecl::Create(C, KmpTaskAffinityInfoArrayTy,
4373                                            ImplicitParamDecl::Other);
4374       CGF.EmitVarDecl(*PD);
4375       AffinitiesArray = CGF.GetAddrOfLocalVar(PD);
4376       NumOfElements = CGF.Builder.CreateIntCast(NumOfElements, CGF.Int32Ty,
4377                                                 /*isSigned=*/false);
4378     } else {
4379       KmpTaskAffinityInfoArrayTy = C.getConstantArrayType(
4380           KmpTaskAffinityInfoTy,
4381           llvm::APInt(C.getTypeSize(C.getSizeType()), NumAffinities), nullptr,
4382           ArrayType::Normal, /*IndexTypeQuals=*/0);
4383       AffinitiesArray =
4384           CGF.CreateMemTemp(KmpTaskAffinityInfoArrayTy, ".affs.arr.addr");
4385       AffinitiesArray = CGF.Builder.CreateConstArrayGEP(AffinitiesArray, 0);
4386       NumOfElements = llvm::ConstantInt::get(CGM.Int32Ty, NumAffinities,
4387                                              /*isSigned=*/false);
4388     }
4389 
4390     const auto *KmpAffinityInfoRD = KmpTaskAffinityInfoTy->getAsRecordDecl();
4391     // Fill array by elements without iterators.
4392     unsigned Pos = 0;
4393     bool HasIterator = false;
4394     for (const auto *C : D.getClausesOfKind<OMPAffinityClause>()) {
4395       if (C->getModifier()) {
4396         HasIterator = true;
4397         continue;
4398       }
4399       for (const Expr *E : C->varlists()) {
4400         llvm::Value *Addr;
4401         llvm::Value *Size;
4402         std::tie(Addr, Size) = getPointerAndSize(CGF, E);
4403         LValue Base =
4404             CGF.MakeAddrLValue(CGF.Builder.CreateConstGEP(AffinitiesArray, Pos),
4405                                KmpTaskAffinityInfoTy);
4406         // affs[i].base_addr = &<Affinities[i].second>;
4407         LValue BaseAddrLVal = CGF.EmitLValueForField(
4408             Base, *std::next(KmpAffinityInfoRD->field_begin(), BaseAddr));
4409         CGF.EmitStoreOfScalar(CGF.Builder.CreatePtrToInt(Addr, CGF.IntPtrTy),
4410                               BaseAddrLVal);
4411         // affs[i].len = sizeof(<Affinities[i].second>);
4412         LValue LenLVal = CGF.EmitLValueForField(
4413             Base, *std::next(KmpAffinityInfoRD->field_begin(), Len));
4414         CGF.EmitStoreOfScalar(Size, LenLVal);
4415         ++Pos;
4416       }
4417     }
4418     LValue PosLVal;
4419     if (HasIterator) {
4420       PosLVal = CGF.MakeAddrLValue(
4421           CGF.CreateMemTemp(C.getSizeType(), "affs.counter.addr"),
4422           C.getSizeType());
4423       CGF.EmitStoreOfScalar(llvm::ConstantInt::get(CGF.SizeTy, Pos), PosLVal);
4424     }
4425     // Process elements with iterators.
4426     for (const auto *C : D.getClausesOfKind<OMPAffinityClause>()) {
4427       const Expr *Modifier = C->getModifier();
4428       if (!Modifier)
4429         continue;
4430       OMPIteratorGeneratorScope IteratorScope(
4431           CGF, cast_or_null<OMPIteratorExpr>(Modifier->IgnoreParenImpCasts()));
4432       for (const Expr *E : C->varlists()) {
4433         llvm::Value *Addr;
4434         llvm::Value *Size;
4435         std::tie(Addr, Size) = getPointerAndSize(CGF, E);
4436         llvm::Value *Idx = CGF.EmitLoadOfScalar(PosLVal, E->getExprLoc());
4437         LValue Base = CGF.MakeAddrLValue(
4438             Address(CGF.Builder.CreateGEP(AffinitiesArray.getPointer(), Idx),
4439                     AffinitiesArray.getAlignment()),
4440             KmpTaskAffinityInfoTy);
4441         // affs[i].base_addr = &<Affinities[i].second>;
4442         LValue BaseAddrLVal = CGF.EmitLValueForField(
4443             Base, *std::next(KmpAffinityInfoRD->field_begin(), BaseAddr));
4444         CGF.EmitStoreOfScalar(CGF.Builder.CreatePtrToInt(Addr, CGF.IntPtrTy),
4445                               BaseAddrLVal);
4446         // affs[i].len = sizeof(<Affinities[i].second>);
4447         LValue LenLVal = CGF.EmitLValueForField(
4448             Base, *std::next(KmpAffinityInfoRD->field_begin(), Len));
4449         CGF.EmitStoreOfScalar(Size, LenLVal);
4450         Idx = CGF.Builder.CreateNUWAdd(
4451             Idx, llvm::ConstantInt::get(Idx->getType(), 1));
4452         CGF.EmitStoreOfScalar(Idx, PosLVal);
4453       }
4454     }
4455     // Call to kmp_int32 __kmpc_omp_reg_task_with_affinity(ident_t *loc_ref,
4456     // kmp_int32 gtid, kmp_task_t *new_task, kmp_int32
4457     // naffins, kmp_task_affinity_info_t *affin_list);
4458     llvm::Value *LocRef = emitUpdateLocation(CGF, Loc);
4459     llvm::Value *GTid = getThreadID(CGF, Loc);
4460     llvm::Value *AffinListPtr = CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(
4461         AffinitiesArray.getPointer(), CGM.VoidPtrTy);
4462     // FIXME: Emit the function and ignore its result for now unless the
4463     // runtime function is properly implemented.
4464     (void)CGF.EmitRuntimeCall(
4465         OMPBuilder.getOrCreateRuntimeFunction(
4466             CGM.getModule(), OMPRTL___kmpc_omp_reg_task_with_affinity),
4467         {LocRef, GTid, NewTask, NumOfElements, AffinListPtr});
4468   }
4469   llvm::Value *NewTaskNewTaskTTy =
4470       CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(
4471           NewTask, KmpTaskTWithPrivatesPtrTy);
4472   LValue Base = CGF.MakeNaturalAlignAddrLValue(NewTaskNewTaskTTy,
4473                                                KmpTaskTWithPrivatesQTy);
4474   LValue TDBase =
4475       CGF.EmitLValueForField(Base, *KmpTaskTWithPrivatesQTyRD->field_begin());
4476   // Fill the data in the resulting kmp_task_t record.
4477   // Copy shareds if there are any.
4478   Address KmpTaskSharedsPtr = Address::invalid();
4479   if (!SharedsTy->getAsStructureType()->getDecl()->field_empty()) {
4480     KmpTaskSharedsPtr =
4481         Address(CGF.EmitLoadOfScalar(
4482                     CGF.EmitLValueForField(
4483                         TDBase, *std::next(KmpTaskTQTyRD->field_begin(),
4484                                            KmpTaskTShareds)),
4485                     Loc),
4486                 CGM.getNaturalTypeAlignment(SharedsTy));
4487     LValue Dest = CGF.MakeAddrLValue(KmpTaskSharedsPtr, SharedsTy);
4488     LValue Src = CGF.MakeAddrLValue(Shareds, SharedsTy);
4489     CGF.EmitAggregateCopy(Dest, Src, SharedsTy, AggValueSlot::DoesNotOverlap);
4490   }
4491   // Emit initial values for private copies (if any).
4492   TaskResultTy Result;
4493   if (!Privates.empty()) {
4494     emitPrivatesInit(CGF, D, KmpTaskSharedsPtr, Base, KmpTaskTWithPrivatesQTyRD,
4495                      SharedsTy, SharedsPtrTy, Data, Privates,
4496                      /*ForDup=*/false);
4497     if (isOpenMPTaskLoopDirective(D.getDirectiveKind()) &&
4498         (!Data.LastprivateVars.empty() || checkInitIsRequired(CGF, Privates))) {
4499       Result.TaskDupFn = emitTaskDupFunction(
4500           CGM, Loc, D, KmpTaskTWithPrivatesPtrQTy, KmpTaskTWithPrivatesQTyRD,
4501           KmpTaskTQTyRD, SharedsTy, SharedsPtrTy, Data, Privates,
4502           /*WithLastIter=*/!Data.LastprivateVars.empty());
4503     }
4504   }
4505   // Fields of union "kmp_cmplrdata_t" for destructors and priority.
4506   enum { Priority = 0, Destructors = 1 };
4507   // Provide pointer to function with destructors for privates.
4508   auto FI = std::next(KmpTaskTQTyRD->field_begin(), Data1);
4509   const RecordDecl *KmpCmplrdataUD =
4510       (*FI)->getType()->getAsUnionType()->getDecl();
4511   if (NeedsCleanup) {
4512     llvm::Value *DestructorFn = emitDestructorsFunction(
4513         CGM, Loc, KmpInt32Ty, KmpTaskTWithPrivatesPtrQTy,
4514         KmpTaskTWithPrivatesQTy);
4515     LValue Data1LV = CGF.EmitLValueForField(TDBase, *FI);
4516     LValue DestructorsLV = CGF.EmitLValueForField(
4517         Data1LV, *std::next(KmpCmplrdataUD->field_begin(), Destructors));
4518     CGF.EmitStoreOfScalar(CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(
4519                               DestructorFn, KmpRoutineEntryPtrTy),
4520                           DestructorsLV);
4521   }
4522   // Set priority.
4523   if (Data.Priority.getInt()) {
4524     LValue Data2LV = CGF.EmitLValueForField(
4525         TDBase, *std::next(KmpTaskTQTyRD->field_begin(), Data2));
4526     LValue PriorityLV = CGF.EmitLValueForField(
4527         Data2LV, *std::next(KmpCmplrdataUD->field_begin(), Priority));
4528     CGF.EmitStoreOfScalar(Data.Priority.getPointer(), PriorityLV);
4529   }
4530   Result.NewTask = NewTask;
4531   Result.TaskEntry = TaskEntry;
4532   Result.NewTaskNewTaskTTy = NewTaskNewTaskTTy;
4533   Result.TDBase = TDBase;
4534   Result.KmpTaskTQTyRD = KmpTaskTQTyRD;
4535   return Result;
4536 }
4537 
4538 namespace {
4539 /// Dependence kind for RTL.
4540 enum RTLDependenceKindTy {
4541   DepIn = 0x01,
4542   DepInOut = 0x3,
4543   DepMutexInOutSet = 0x4
4544 };
4545 /// Fields ids in kmp_depend_info record.
4546 enum RTLDependInfoFieldsTy { BaseAddr, Len, Flags };
4547 } // namespace
4548 
4549 /// Translates internal dependency kind into the runtime kind.
4550 static RTLDependenceKindTy translateDependencyKind(OpenMPDependClauseKind K) {
4551   RTLDependenceKindTy DepKind;
4552   switch (K) {
4553   case OMPC_DEPEND_in:
4554     DepKind = DepIn;
4555     break;
4556   // Out and InOut dependencies must use the same code.
4557   case OMPC_DEPEND_out:
4558   case OMPC_DEPEND_inout:
4559     DepKind = DepInOut;
4560     break;
4561   case OMPC_DEPEND_mutexinoutset:
4562     DepKind = DepMutexInOutSet;
4563     break;
4564   case OMPC_DEPEND_source:
4565   case OMPC_DEPEND_sink:
4566   case OMPC_DEPEND_depobj:
4567   case OMPC_DEPEND_unknown:
4568     llvm_unreachable("Unknown task dependence type");
4569   }
4570   return DepKind;
4571 }
4572 
4573 /// Builds kmp_depend_info, if it is not built yet, and builds flags type.
4574 static void getDependTypes(ASTContext &C, QualType &KmpDependInfoTy,
4575                            QualType &FlagsTy) {
4576   FlagsTy = C.getIntTypeForBitwidth(C.getTypeSize(C.BoolTy), /*Signed=*/false);
4577   if (KmpDependInfoTy.isNull()) {
4578     RecordDecl *KmpDependInfoRD = C.buildImplicitRecord("kmp_depend_info");
4579     KmpDependInfoRD->startDefinition();
4580     addFieldToRecordDecl(C, KmpDependInfoRD, C.getIntPtrType());
4581     addFieldToRecordDecl(C, KmpDependInfoRD, C.getSizeType());
4582     addFieldToRecordDecl(C, KmpDependInfoRD, FlagsTy);
4583     KmpDependInfoRD->completeDefinition();
4584     KmpDependInfoTy = C.getRecordType(KmpDependInfoRD);
4585   }
4586 }
4587 
4588 std::pair<llvm::Value *, LValue>
4589 CGOpenMPRuntime::getDepobjElements(CodeGenFunction &CGF, LValue DepobjLVal,
4590                                    SourceLocation Loc) {
4591   ASTContext &C = CGM.getContext();
4592   QualType FlagsTy;
4593   getDependTypes(C, KmpDependInfoTy, FlagsTy);
4594   RecordDecl *KmpDependInfoRD =
4595       cast<RecordDecl>(KmpDependInfoTy->getAsTagDecl());
4596   LValue Base = CGF.EmitLoadOfPointerLValue(
4597       DepobjLVal.getAddress(CGF),
4598       C.getPointerType(C.VoidPtrTy).castAs<PointerType>());
4599   QualType KmpDependInfoPtrTy = C.getPointerType(KmpDependInfoTy);
4600   Address Addr = CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(
4601           Base.getAddress(CGF), CGF.ConvertTypeForMem(KmpDependInfoPtrTy));
4602   Base = CGF.MakeAddrLValue(Addr, KmpDependInfoTy, Base.getBaseInfo(),
4603                             Base.getTBAAInfo());
4604   llvm::Value *DepObjAddr = CGF.Builder.CreateGEP(
4605       Addr.getPointer(),
4606       llvm::ConstantInt::get(CGF.IntPtrTy, -1, /*isSigned=*/true));
4607   LValue NumDepsBase = CGF.MakeAddrLValue(
4608       Address(DepObjAddr, Addr.getAlignment()), KmpDependInfoTy,
4609       Base.getBaseInfo(), Base.getTBAAInfo());
4610   // NumDeps = deps[i].base_addr;
4611   LValue BaseAddrLVal = CGF.EmitLValueForField(
4612       NumDepsBase, *std::next(KmpDependInfoRD->field_begin(), BaseAddr));
4613   llvm::Value *NumDeps = CGF.EmitLoadOfScalar(BaseAddrLVal, Loc);
4614   return std::make_pair(NumDeps, Base);
4615 }
4616 
4617 static void emitDependData(CodeGenFunction &CGF, QualType &KmpDependInfoTy,
4618                            llvm::PointerUnion<unsigned *, LValue *> Pos,
4619                            const OMPTaskDataTy::DependData &Data,
4620                            Address DependenciesArray) {
4621   CodeGenModule &CGM = CGF.CGM;
4622   ASTContext &C = CGM.getContext();
4623   QualType FlagsTy;
4624   getDependTypes(C, KmpDependInfoTy, FlagsTy);
4625   RecordDecl *KmpDependInfoRD =
4626       cast<RecordDecl>(KmpDependInfoTy->getAsTagDecl());
4627   llvm::Type *LLVMFlagsTy = CGF.ConvertTypeForMem(FlagsTy);
4628 
4629   OMPIteratorGeneratorScope IteratorScope(
4630       CGF, cast_or_null<OMPIteratorExpr>(
4631                Data.IteratorExpr ? Data.IteratorExpr->IgnoreParenImpCasts()
4632                                  : nullptr));
4633   for (const Expr *E : Data.DepExprs) {
4634     llvm::Value *Addr;
4635     llvm::Value *Size;
4636     std::tie(Addr, Size) = getPointerAndSize(CGF, E);
4637     LValue Base;
4638     if (unsigned *P = Pos.dyn_cast<unsigned *>()) {
4639       Base = CGF.MakeAddrLValue(
4640           CGF.Builder.CreateConstGEP(DependenciesArray, *P), KmpDependInfoTy);
4641     } else {
4642       LValue &PosLVal = *Pos.get<LValue *>();
4643       llvm::Value *Idx = CGF.EmitLoadOfScalar(PosLVal, E->getExprLoc());
4644       Base = CGF.MakeAddrLValue(
4645           Address(CGF.Builder.CreateGEP(DependenciesArray.getPointer(), Idx),
4646                   DependenciesArray.getAlignment()),
4647           KmpDependInfoTy);
4648     }
4649     // deps[i].base_addr = &<Dependencies[i].second>;
4650     LValue BaseAddrLVal = CGF.EmitLValueForField(
4651         Base, *std::next(KmpDependInfoRD->field_begin(), BaseAddr));
4652     CGF.EmitStoreOfScalar(CGF.Builder.CreatePtrToInt(Addr, CGF.IntPtrTy),
4653                           BaseAddrLVal);
4654     // deps[i].len = sizeof(<Dependencies[i].second>);
4655     LValue LenLVal = CGF.EmitLValueForField(
4656         Base, *std::next(KmpDependInfoRD->field_begin(), Len));
4657     CGF.EmitStoreOfScalar(Size, LenLVal);
4658     // deps[i].flags = <Dependencies[i].first>;
4659     RTLDependenceKindTy DepKind = translateDependencyKind(Data.DepKind);
4660     LValue FlagsLVal = CGF.EmitLValueForField(
4661         Base, *std::next(KmpDependInfoRD->field_begin(), Flags));
4662     CGF.EmitStoreOfScalar(llvm::ConstantInt::get(LLVMFlagsTy, DepKind),
4663                           FlagsLVal);
4664     if (unsigned *P = Pos.dyn_cast<unsigned *>()) {
4665       ++(*P);
4666     } else {
4667       LValue &PosLVal = *Pos.get<LValue *>();
4668       llvm::Value *Idx = CGF.EmitLoadOfScalar(PosLVal, E->getExprLoc());
4669       Idx = CGF.Builder.CreateNUWAdd(Idx,
4670                                      llvm::ConstantInt::get(Idx->getType(), 1));
4671       CGF.EmitStoreOfScalar(Idx, PosLVal);
4672     }
4673   }
4674 }
4675 
4676 static SmallVector<llvm::Value *, 4>
4677 emitDepobjElementsSizes(CodeGenFunction &CGF, QualType &KmpDependInfoTy,
4678                         const OMPTaskDataTy::DependData &Data) {
4679   assert(Data.DepKind == OMPC_DEPEND_depobj &&
4680          "Expected depobj dependecy kind.");
4681   SmallVector<llvm::Value *, 4> Sizes;
4682   SmallVector<LValue, 4> SizeLVals;
4683   ASTContext &C = CGF.getContext();
4684   QualType FlagsTy;
4685   getDependTypes(C, KmpDependInfoTy, FlagsTy);
4686   RecordDecl *KmpDependInfoRD =
4687       cast<RecordDecl>(KmpDependInfoTy->getAsTagDecl());
4688   QualType KmpDependInfoPtrTy = C.getPointerType(KmpDependInfoTy);
4689   llvm::Type *KmpDependInfoPtrT = CGF.ConvertTypeForMem(KmpDependInfoPtrTy);
4690   {
4691     OMPIteratorGeneratorScope IteratorScope(
4692         CGF, cast_or_null<OMPIteratorExpr>(
4693                  Data.IteratorExpr ? Data.IteratorExpr->IgnoreParenImpCasts()
4694                                    : nullptr));
4695     for (const Expr *E : Data.DepExprs) {
4696       LValue DepobjLVal = CGF.EmitLValue(E->IgnoreParenImpCasts());
4697       LValue Base = CGF.EmitLoadOfPointerLValue(
4698           DepobjLVal.getAddress(CGF),
4699           C.getPointerType(C.VoidPtrTy).castAs<PointerType>());
4700       Address Addr = CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(
4701           Base.getAddress(CGF), KmpDependInfoPtrT);
4702       Base = CGF.MakeAddrLValue(Addr, KmpDependInfoTy, Base.getBaseInfo(),
4703                                 Base.getTBAAInfo());
4704       llvm::Value *DepObjAddr = CGF.Builder.CreateGEP(
4705           Addr.getPointer(),
4706           llvm::ConstantInt::get(CGF.IntPtrTy, -1, /*isSigned=*/true));
4707       LValue NumDepsBase = CGF.MakeAddrLValue(
4708           Address(DepObjAddr, Addr.getAlignment()), KmpDependInfoTy,
4709           Base.getBaseInfo(), Base.getTBAAInfo());
4710       // NumDeps = deps[i].base_addr;
4711       LValue BaseAddrLVal = CGF.EmitLValueForField(
4712           NumDepsBase, *std::next(KmpDependInfoRD->field_begin(), BaseAddr));
4713       llvm::Value *NumDeps =
4714           CGF.EmitLoadOfScalar(BaseAddrLVal, E->getExprLoc());
4715       LValue NumLVal = CGF.MakeAddrLValue(
4716           CGF.CreateMemTemp(C.getUIntPtrType(), "depobj.size.addr"),
4717           C.getUIntPtrType());
4718       CGF.InitTempAlloca(NumLVal.getAddress(CGF),
4719                          llvm::ConstantInt::get(CGF.IntPtrTy, 0));
4720       llvm::Value *PrevVal = CGF.EmitLoadOfScalar(NumLVal, E->getExprLoc());
4721       llvm::Value *Add = CGF.Builder.CreateNUWAdd(PrevVal, NumDeps);
4722       CGF.EmitStoreOfScalar(Add, NumLVal);
4723       SizeLVals.push_back(NumLVal);
4724     }
4725   }
4726   for (unsigned I = 0, E = SizeLVals.size(); I < E; ++I) {
4727     llvm::Value *Size =
4728         CGF.EmitLoadOfScalar(SizeLVals[I], Data.DepExprs[I]->getExprLoc());
4729     Sizes.push_back(Size);
4730   }
4731   return Sizes;
4732 }
4733 
4734 static void emitDepobjElements(CodeGenFunction &CGF, QualType &KmpDependInfoTy,
4735                                LValue PosLVal,
4736                                const OMPTaskDataTy::DependData &Data,
4737                                Address DependenciesArray) {
4738   assert(Data.DepKind == OMPC_DEPEND_depobj &&
4739          "Expected depobj dependecy kind.");
4740   ASTContext &C = CGF.getContext();
4741   QualType FlagsTy;
4742   getDependTypes(C, KmpDependInfoTy, FlagsTy);
4743   RecordDecl *KmpDependInfoRD =
4744       cast<RecordDecl>(KmpDependInfoTy->getAsTagDecl());
4745   QualType KmpDependInfoPtrTy = C.getPointerType(KmpDependInfoTy);
4746   llvm::Type *KmpDependInfoPtrT = CGF.ConvertTypeForMem(KmpDependInfoPtrTy);
4747   llvm::Value *ElSize = CGF.getTypeSize(KmpDependInfoTy);
4748   {
4749     OMPIteratorGeneratorScope IteratorScope(
4750         CGF, cast_or_null<OMPIteratorExpr>(
4751                  Data.IteratorExpr ? Data.IteratorExpr->IgnoreParenImpCasts()
4752                                    : nullptr));
4753     for (unsigned I = 0, End = Data.DepExprs.size(); I < End; ++I) {
4754       const Expr *E = Data.DepExprs[I];
4755       LValue DepobjLVal = CGF.EmitLValue(E->IgnoreParenImpCasts());
4756       LValue Base = CGF.EmitLoadOfPointerLValue(
4757           DepobjLVal.getAddress(CGF),
4758           C.getPointerType(C.VoidPtrTy).castAs<PointerType>());
4759       Address Addr = CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(
4760           Base.getAddress(CGF), KmpDependInfoPtrT);
4761       Base = CGF.MakeAddrLValue(Addr, KmpDependInfoTy, Base.getBaseInfo(),
4762                                 Base.getTBAAInfo());
4763 
4764       // Get number of elements in a single depobj.
4765       llvm::Value *DepObjAddr = CGF.Builder.CreateGEP(
4766           Addr.getPointer(),
4767           llvm::ConstantInt::get(CGF.IntPtrTy, -1, /*isSigned=*/true));
4768       LValue NumDepsBase = CGF.MakeAddrLValue(
4769           Address(DepObjAddr, Addr.getAlignment()), KmpDependInfoTy,
4770           Base.getBaseInfo(), Base.getTBAAInfo());
4771       // NumDeps = deps[i].base_addr;
4772       LValue BaseAddrLVal = CGF.EmitLValueForField(
4773           NumDepsBase, *std::next(KmpDependInfoRD->field_begin(), BaseAddr));
4774       llvm::Value *NumDeps =
4775           CGF.EmitLoadOfScalar(BaseAddrLVal, E->getExprLoc());
4776 
4777       // memcopy dependency data.
4778       llvm::Value *Size = CGF.Builder.CreateNUWMul(
4779           ElSize,
4780           CGF.Builder.CreateIntCast(NumDeps, CGF.SizeTy, /*isSigned=*/false));
4781       llvm::Value *Pos = CGF.EmitLoadOfScalar(PosLVal, E->getExprLoc());
4782       Address DepAddr =
4783           Address(CGF.Builder.CreateGEP(DependenciesArray.getPointer(), Pos),
4784                   DependenciesArray.getAlignment());
4785       CGF.Builder.CreateMemCpy(DepAddr, Base.getAddress(CGF), Size);
4786 
4787       // Increase pos.
4788       // pos += size;
4789       llvm::Value *Add = CGF.Builder.CreateNUWAdd(Pos, NumDeps);
4790       CGF.EmitStoreOfScalar(Add, PosLVal);
4791     }
4792   }
4793 }
4794 
4795 std::pair<llvm::Value *, Address> CGOpenMPRuntime::emitDependClause(
4796     CodeGenFunction &CGF, ArrayRef<OMPTaskDataTy::DependData> Dependencies,
4797     SourceLocation Loc) {
4798   if (llvm::all_of(Dependencies, [](const OMPTaskDataTy::DependData &D) {
4799         return D.DepExprs.empty();
4800       }))
4801     return std::make_pair(nullptr, Address::invalid());
4802   // Process list of dependencies.
4803   ASTContext &C = CGM.getContext();
4804   Address DependenciesArray = Address::invalid();
4805   llvm::Value *NumOfElements = nullptr;
4806   unsigned NumDependencies = std::accumulate(
4807       Dependencies.begin(), Dependencies.end(), 0,
4808       [](unsigned V, const OMPTaskDataTy::DependData &D) {
4809         return D.DepKind == OMPC_DEPEND_depobj
4810                    ? V
4811                    : (V + (D.IteratorExpr ? 0 : D.DepExprs.size()));
4812       });
4813   QualType FlagsTy;
4814   getDependTypes(C, KmpDependInfoTy, FlagsTy);
4815   bool HasDepobjDeps = false;
4816   bool HasRegularWithIterators = false;
4817   llvm::Value *NumOfDepobjElements = llvm::ConstantInt::get(CGF.IntPtrTy, 0);
4818   llvm::Value *NumOfRegularWithIterators =
4819       llvm::ConstantInt::get(CGF.IntPtrTy, 1);
4820   // Calculate number of depobj dependecies and regular deps with the iterators.
4821   for (const OMPTaskDataTy::DependData &D : Dependencies) {
4822     if (D.DepKind == OMPC_DEPEND_depobj) {
4823       SmallVector<llvm::Value *, 4> Sizes =
4824           emitDepobjElementsSizes(CGF, KmpDependInfoTy, D);
4825       for (llvm::Value *Size : Sizes) {
4826         NumOfDepobjElements =
4827             CGF.Builder.CreateNUWAdd(NumOfDepobjElements, Size);
4828       }
4829       HasDepobjDeps = true;
4830       continue;
4831     }
4832     // Include number of iterations, if any.
4833     if (const auto *IE = cast_or_null<OMPIteratorExpr>(D.IteratorExpr)) {
4834       for (unsigned I = 0, E = IE->numOfIterators(); I < E; ++I) {
4835         llvm::Value *Sz = CGF.EmitScalarExpr(IE->getHelper(I).Upper);
4836         Sz = CGF.Builder.CreateIntCast(Sz, CGF.IntPtrTy, /*isSigned=*/false);
4837         NumOfRegularWithIterators =
4838             CGF.Builder.CreateNUWMul(NumOfRegularWithIterators, Sz);
4839       }
4840       HasRegularWithIterators = true;
4841       continue;
4842     }
4843   }
4844 
4845   QualType KmpDependInfoArrayTy;
4846   if (HasDepobjDeps || HasRegularWithIterators) {
4847     NumOfElements = llvm::ConstantInt::get(CGM.IntPtrTy, NumDependencies,
4848                                            /*isSigned=*/false);
4849     if (HasDepobjDeps) {
4850       NumOfElements =
4851           CGF.Builder.CreateNUWAdd(NumOfDepobjElements, NumOfElements);
4852     }
4853     if (HasRegularWithIterators) {
4854       NumOfElements =
4855           CGF.Builder.CreateNUWAdd(NumOfRegularWithIterators, NumOfElements);
4856     }
4857     OpaqueValueExpr OVE(Loc,
4858                         C.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0),
4859                         VK_RValue);
4860     CodeGenFunction::OpaqueValueMapping OpaqueMap(CGF, &OVE,
4861                                                   RValue::get(NumOfElements));
4862     KmpDependInfoArrayTy =
4863         C.getVariableArrayType(KmpDependInfoTy, &OVE, ArrayType::Normal,
4864                                /*IndexTypeQuals=*/0, SourceRange(Loc, Loc));
4865     // CGF.EmitVariablyModifiedType(KmpDependInfoArrayTy);
4866     // Properly emit variable-sized array.
4867     auto *PD = ImplicitParamDecl::Create(C, KmpDependInfoArrayTy,
4868                                          ImplicitParamDecl::Other);
4869     CGF.EmitVarDecl(*PD);
4870     DependenciesArray = CGF.GetAddrOfLocalVar(PD);
4871     NumOfElements = CGF.Builder.CreateIntCast(NumOfElements, CGF.Int32Ty,
4872                                               /*isSigned=*/false);
4873   } else {
4874     KmpDependInfoArrayTy = C.getConstantArrayType(
4875         KmpDependInfoTy, llvm::APInt(/*numBits=*/64, NumDependencies), nullptr,
4876         ArrayType::Normal, /*IndexTypeQuals=*/0);
4877     DependenciesArray =
4878         CGF.CreateMemTemp(KmpDependInfoArrayTy, ".dep.arr.addr");
4879     DependenciesArray = CGF.Builder.CreateConstArrayGEP(DependenciesArray, 0);
4880     NumOfElements = llvm::ConstantInt::get(CGM.Int32Ty, NumDependencies,
4881                                            /*isSigned=*/false);
4882   }
4883   unsigned Pos = 0;
4884   for (unsigned I = 0, End = Dependencies.size(); I < End; ++I) {
4885     if (Dependencies[I].DepKind == OMPC_DEPEND_depobj ||
4886         Dependencies[I].IteratorExpr)
4887       continue;
4888     emitDependData(CGF, KmpDependInfoTy, &Pos, Dependencies[I],
4889                    DependenciesArray);
4890   }
4891   // Copy regular dependecies with iterators.
4892   LValue PosLVal = CGF.MakeAddrLValue(
4893       CGF.CreateMemTemp(C.getSizeType(), "dep.counter.addr"), C.getSizeType());
4894   CGF.EmitStoreOfScalar(llvm::ConstantInt::get(CGF.SizeTy, Pos), PosLVal);
4895   for (unsigned I = 0, End = Dependencies.size(); I < End; ++I) {
4896     if (Dependencies[I].DepKind == OMPC_DEPEND_depobj ||
4897         !Dependencies[I].IteratorExpr)
4898       continue;
4899     emitDependData(CGF, KmpDependInfoTy, &PosLVal, Dependencies[I],
4900                    DependenciesArray);
4901   }
4902   // Copy final depobj arrays without iterators.
4903   if (HasDepobjDeps) {
4904     for (unsigned I = 0, End = Dependencies.size(); I < End; ++I) {
4905       if (Dependencies[I].DepKind != OMPC_DEPEND_depobj)
4906         continue;
4907       emitDepobjElements(CGF, KmpDependInfoTy, PosLVal, Dependencies[I],
4908                          DependenciesArray);
4909     }
4910   }
4911   DependenciesArray = CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(
4912       DependenciesArray, CGF.VoidPtrTy);
4913   return std::make_pair(NumOfElements, DependenciesArray);
4914 }
4915 
4916 Address CGOpenMPRuntime::emitDepobjDependClause(
4917     CodeGenFunction &CGF, const OMPTaskDataTy::DependData &Dependencies,
4918     SourceLocation Loc) {
4919   if (Dependencies.DepExprs.empty())
4920     return Address::invalid();
4921   // Process list of dependencies.
4922   ASTContext &C = CGM.getContext();
4923   Address DependenciesArray = Address::invalid();
4924   unsigned NumDependencies = Dependencies.DepExprs.size();
4925   QualType FlagsTy;
4926   getDependTypes(C, KmpDependInfoTy, FlagsTy);
4927   RecordDecl *KmpDependInfoRD =
4928       cast<RecordDecl>(KmpDependInfoTy->getAsTagDecl());
4929 
4930   llvm::Value *Size;
4931   // Define type kmp_depend_info[<Dependencies.size()>];
4932   // For depobj reserve one extra element to store the number of elements.
4933   // It is required to handle depobj(x) update(in) construct.
4934   // kmp_depend_info[<Dependencies.size()>] deps;
4935   llvm::Value *NumDepsVal;
4936   CharUnits Align = C.getTypeAlignInChars(KmpDependInfoTy);
4937   if (const auto *IE =
4938           cast_or_null<OMPIteratorExpr>(Dependencies.IteratorExpr)) {
4939     NumDepsVal = llvm::ConstantInt::get(CGF.SizeTy, 1);
4940     for (unsigned I = 0, E = IE->numOfIterators(); I < E; ++I) {
4941       llvm::Value *Sz = CGF.EmitScalarExpr(IE->getHelper(I).Upper);
4942       Sz = CGF.Builder.CreateIntCast(Sz, CGF.SizeTy, /*isSigned=*/false);
4943       NumDepsVal = CGF.Builder.CreateNUWMul(NumDepsVal, Sz);
4944     }
4945     Size = CGF.Builder.CreateNUWAdd(llvm::ConstantInt::get(CGF.SizeTy, 1),
4946                                     NumDepsVal);
4947     CharUnits SizeInBytes =
4948         C.getTypeSizeInChars(KmpDependInfoTy).alignTo(Align);
4949     llvm::Value *RecSize = CGM.getSize(SizeInBytes);
4950     Size = CGF.Builder.CreateNUWMul(Size, RecSize);
4951     NumDepsVal =
4952         CGF.Builder.CreateIntCast(NumDepsVal, CGF.IntPtrTy, /*isSigned=*/false);
4953   } else {
4954     QualType KmpDependInfoArrayTy = C.getConstantArrayType(
4955         KmpDependInfoTy, llvm::APInt(/*numBits=*/64, NumDependencies + 1),
4956         nullptr, ArrayType::Normal, /*IndexTypeQuals=*/0);
4957     CharUnits Sz = C.getTypeSizeInChars(KmpDependInfoArrayTy);
4958     Size = CGM.getSize(Sz.alignTo(Align));
4959     NumDepsVal = llvm::ConstantInt::get(CGF.IntPtrTy, NumDependencies);
4960   }
4961   // Need to allocate on the dynamic memory.
4962   llvm::Value *ThreadID = getThreadID(CGF, Loc);
4963   // Use default allocator.
4964   llvm::Value *Allocator = llvm::ConstantPointerNull::get(CGF.VoidPtrTy);
4965   llvm::Value *Args[] = {ThreadID, Size, Allocator};
4966 
4967   llvm::Value *Addr =
4968       CGF.EmitRuntimeCall(OMPBuilder.getOrCreateRuntimeFunction(
4969                               CGM.getModule(), OMPRTL___kmpc_alloc),
4970                           Args, ".dep.arr.addr");
4971   Addr = CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(
4972       Addr, CGF.ConvertTypeForMem(KmpDependInfoTy)->getPointerTo());
4973   DependenciesArray = Address(Addr, Align);
4974   // Write number of elements in the first element of array for depobj.
4975   LValue Base = CGF.MakeAddrLValue(DependenciesArray, KmpDependInfoTy);
4976   // deps[i].base_addr = NumDependencies;
4977   LValue BaseAddrLVal = CGF.EmitLValueForField(
4978       Base, *std::next(KmpDependInfoRD->field_begin(), BaseAddr));
4979   CGF.EmitStoreOfScalar(NumDepsVal, BaseAddrLVal);
4980   llvm::PointerUnion<unsigned *, LValue *> Pos;
4981   unsigned Idx = 1;
4982   LValue PosLVal;
4983   if (Dependencies.IteratorExpr) {
4984     PosLVal = CGF.MakeAddrLValue(
4985         CGF.CreateMemTemp(C.getSizeType(), "iterator.counter.addr"),
4986         C.getSizeType());
4987     CGF.EmitStoreOfScalar(llvm::ConstantInt::get(CGF.SizeTy, Idx), PosLVal,
4988                           /*IsInit=*/true);
4989     Pos = &PosLVal;
4990   } else {
4991     Pos = &Idx;
4992   }
4993   emitDependData(CGF, KmpDependInfoTy, Pos, Dependencies, DependenciesArray);
4994   DependenciesArray = CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(
4995       CGF.Builder.CreateConstGEP(DependenciesArray, 1), CGF.VoidPtrTy);
4996   return DependenciesArray;
4997 }
4998 
4999 void CGOpenMPRuntime::emitDestroyClause(CodeGenFunction &CGF, LValue DepobjLVal,
5000                                         SourceLocation Loc) {
5001   ASTContext &C = CGM.getContext();
5002   QualType FlagsTy;
5003   getDependTypes(C, KmpDependInfoTy, FlagsTy);
5004   LValue Base = CGF.EmitLoadOfPointerLValue(
5005       DepobjLVal.getAddress(CGF),
5006       C.getPointerType(C.VoidPtrTy).castAs<PointerType>());
5007   QualType KmpDependInfoPtrTy = C.getPointerType(KmpDependInfoTy);
5008   Address Addr = CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(
5009       Base.getAddress(CGF), CGF.ConvertTypeForMem(KmpDependInfoPtrTy));
5010   llvm::Value *DepObjAddr = CGF.Builder.CreateGEP(
5011       Addr.getPointer(),
5012       llvm::ConstantInt::get(CGF.IntPtrTy, -1, /*isSigned=*/true));
5013   DepObjAddr = CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(DepObjAddr,
5014                                                                CGF.VoidPtrTy);
5015   llvm::Value *ThreadID = getThreadID(CGF, Loc);
5016   // Use default allocator.
5017   llvm::Value *Allocator = llvm::ConstantPointerNull::get(CGF.VoidPtrTy);
5018   llvm::Value *Args[] = {ThreadID, DepObjAddr, Allocator};
5019 
5020   // _kmpc_free(gtid, addr, nullptr);
5021   (void)CGF.EmitRuntimeCall(OMPBuilder.getOrCreateRuntimeFunction(
5022                                 CGM.getModule(), OMPRTL___kmpc_free),
5023                             Args);
5024 }
5025 
5026 void CGOpenMPRuntime::emitUpdateClause(CodeGenFunction &CGF, LValue DepobjLVal,
5027                                        OpenMPDependClauseKind NewDepKind,
5028                                        SourceLocation Loc) {
5029   ASTContext &C = CGM.getContext();
5030   QualType FlagsTy;
5031   getDependTypes(C, KmpDependInfoTy, FlagsTy);
5032   RecordDecl *KmpDependInfoRD =
5033       cast<RecordDecl>(KmpDependInfoTy->getAsTagDecl());
5034   llvm::Type *LLVMFlagsTy = CGF.ConvertTypeForMem(FlagsTy);
5035   llvm::Value *NumDeps;
5036   LValue Base;
5037   std::tie(NumDeps, Base) = getDepobjElements(CGF, DepobjLVal, Loc);
5038 
5039   Address Begin = Base.getAddress(CGF);
5040   // Cast from pointer to array type to pointer to single element.
5041   llvm::Value *End = CGF.Builder.CreateGEP(Begin.getPointer(), NumDeps);
5042   // The basic structure here is a while-do loop.
5043   llvm::BasicBlock *BodyBB = CGF.createBasicBlock("omp.body");
5044   llvm::BasicBlock *DoneBB = CGF.createBasicBlock("omp.done");
5045   llvm::BasicBlock *EntryBB = CGF.Builder.GetInsertBlock();
5046   CGF.EmitBlock(BodyBB);
5047   llvm::PHINode *ElementPHI =
5048       CGF.Builder.CreatePHI(Begin.getType(), 2, "omp.elementPast");
5049   ElementPHI->addIncoming(Begin.getPointer(), EntryBB);
5050   Begin = Address(ElementPHI, Begin.getAlignment());
5051   Base = CGF.MakeAddrLValue(Begin, KmpDependInfoTy, Base.getBaseInfo(),
5052                             Base.getTBAAInfo());
5053   // deps[i].flags = NewDepKind;
5054   RTLDependenceKindTy DepKind = translateDependencyKind(NewDepKind);
5055   LValue FlagsLVal = CGF.EmitLValueForField(
5056       Base, *std::next(KmpDependInfoRD->field_begin(), Flags));
5057   CGF.EmitStoreOfScalar(llvm::ConstantInt::get(LLVMFlagsTy, DepKind),
5058                         FlagsLVal);
5059 
5060   // Shift the address forward by one element.
5061   Address ElementNext =
5062       CGF.Builder.CreateConstGEP(Begin, /*Index=*/1, "omp.elementNext");
5063   ElementPHI->addIncoming(ElementNext.getPointer(),
5064                           CGF.Builder.GetInsertBlock());
5065   llvm::Value *IsEmpty =
5066       CGF.Builder.CreateICmpEQ(ElementNext.getPointer(), End, "omp.isempty");
5067   CGF.Builder.CreateCondBr(IsEmpty, DoneBB, BodyBB);
5068   // Done.
5069   CGF.EmitBlock(DoneBB, /*IsFinished=*/true);
5070 }
5071 
5072 void CGOpenMPRuntime::emitTaskCall(CodeGenFunction &CGF, SourceLocation Loc,
5073                                    const OMPExecutableDirective &D,
5074                                    llvm::Function *TaskFunction,
5075                                    QualType SharedsTy, Address Shareds,
5076                                    const Expr *IfCond,
5077                                    const OMPTaskDataTy &Data) {
5078   if (!CGF.HaveInsertPoint())
5079     return;
5080 
5081   TaskResultTy Result =
5082       emitTaskInit(CGF, Loc, D, TaskFunction, SharedsTy, Shareds, Data);
5083   llvm::Value *NewTask = Result.NewTask;
5084   llvm::Function *TaskEntry = Result.TaskEntry;
5085   llvm::Value *NewTaskNewTaskTTy = Result.NewTaskNewTaskTTy;
5086   LValue TDBase = Result.TDBase;
5087   const RecordDecl *KmpTaskTQTyRD = Result.KmpTaskTQTyRD;
5088   // Process list of dependences.
5089   Address DependenciesArray = Address::invalid();
5090   llvm::Value *NumOfElements;
5091   std::tie(NumOfElements, DependenciesArray) =
5092       emitDependClause(CGF, Data.Dependences, Loc);
5093 
5094   // NOTE: routine and part_id fields are initialized by __kmpc_omp_task_alloc()
5095   // libcall.
5096   // Build kmp_int32 __kmpc_omp_task_with_deps(ident_t *, kmp_int32 gtid,
5097   // kmp_task_t *new_task, kmp_int32 ndeps, kmp_depend_info_t *dep_list,
5098   // kmp_int32 ndeps_noalias, kmp_depend_info_t *noalias_dep_list) if dependence
5099   // list is not empty
5100   llvm::Value *ThreadID = getThreadID(CGF, Loc);
5101   llvm::Value *UpLoc = emitUpdateLocation(CGF, Loc);
5102   llvm::Value *TaskArgs[] = { UpLoc, ThreadID, NewTask };
5103   llvm::Value *DepTaskArgs[7];
5104   if (!Data.Dependences.empty()) {
5105     DepTaskArgs[0] = UpLoc;
5106     DepTaskArgs[1] = ThreadID;
5107     DepTaskArgs[2] = NewTask;
5108     DepTaskArgs[3] = NumOfElements;
5109     DepTaskArgs[4] = DependenciesArray.getPointer();
5110     DepTaskArgs[5] = CGF.Builder.getInt32(0);
5111     DepTaskArgs[6] = llvm::ConstantPointerNull::get(CGF.VoidPtrTy);
5112   }
5113   auto &&ThenCodeGen = [this, &Data, TDBase, KmpTaskTQTyRD, &TaskArgs,
5114                         &DepTaskArgs](CodeGenFunction &CGF, PrePostActionTy &) {
5115     if (!Data.Tied) {
5116       auto PartIdFI = std::next(KmpTaskTQTyRD->field_begin(), KmpTaskTPartId);
5117       LValue PartIdLVal = CGF.EmitLValueForField(TDBase, *PartIdFI);
5118       CGF.EmitStoreOfScalar(CGF.Builder.getInt32(0), PartIdLVal);
5119     }
5120     if (!Data.Dependences.empty()) {
5121       CGF.EmitRuntimeCall(
5122           OMPBuilder.getOrCreateRuntimeFunction(
5123               CGM.getModule(), OMPRTL___kmpc_omp_task_with_deps),
5124           DepTaskArgs);
5125     } else {
5126       CGF.EmitRuntimeCall(OMPBuilder.getOrCreateRuntimeFunction(
5127                               CGM.getModule(), OMPRTL___kmpc_omp_task),
5128                           TaskArgs);
5129     }
5130     // Check if parent region is untied and build return for untied task;
5131     if (auto *Region =
5132             dyn_cast_or_null<CGOpenMPRegionInfo>(CGF.CapturedStmtInfo))
5133       Region->emitUntiedSwitch(CGF);
5134   };
5135 
5136   llvm::Value *DepWaitTaskArgs[6];
5137   if (!Data.Dependences.empty()) {
5138     DepWaitTaskArgs[0] = UpLoc;
5139     DepWaitTaskArgs[1] = ThreadID;
5140     DepWaitTaskArgs[2] = NumOfElements;
5141     DepWaitTaskArgs[3] = DependenciesArray.getPointer();
5142     DepWaitTaskArgs[4] = CGF.Builder.getInt32(0);
5143     DepWaitTaskArgs[5] = llvm::ConstantPointerNull::get(CGF.VoidPtrTy);
5144   }
5145   auto &M = CGM.getModule();
5146   auto &&ElseCodeGen = [this, &M, &TaskArgs, ThreadID, NewTaskNewTaskTTy,
5147                         TaskEntry, &Data, &DepWaitTaskArgs,
5148                         Loc](CodeGenFunction &CGF, PrePostActionTy &) {
5149     CodeGenFunction::RunCleanupsScope LocalScope(CGF);
5150     // Build void __kmpc_omp_wait_deps(ident_t *, kmp_int32 gtid,
5151     // kmp_int32 ndeps, kmp_depend_info_t *dep_list, kmp_int32
5152     // ndeps_noalias, kmp_depend_info_t *noalias_dep_list); if dependence info
5153     // is specified.
5154     if (!Data.Dependences.empty())
5155       CGF.EmitRuntimeCall(
5156           OMPBuilder.getOrCreateRuntimeFunction(M, OMPRTL___kmpc_omp_wait_deps),
5157           DepWaitTaskArgs);
5158     // Call proxy_task_entry(gtid, new_task);
5159     auto &&CodeGen = [TaskEntry, ThreadID, NewTaskNewTaskTTy,
5160                       Loc](CodeGenFunction &CGF, PrePostActionTy &Action) {
5161       Action.Enter(CGF);
5162       llvm::Value *OutlinedFnArgs[] = {ThreadID, NewTaskNewTaskTTy};
5163       CGF.CGM.getOpenMPRuntime().emitOutlinedFunctionCall(CGF, Loc, TaskEntry,
5164                                                           OutlinedFnArgs);
5165     };
5166 
5167     // Build void __kmpc_omp_task_begin_if0(ident_t *, kmp_int32 gtid,
5168     // kmp_task_t *new_task);
5169     // Build void __kmpc_omp_task_complete_if0(ident_t *, kmp_int32 gtid,
5170     // kmp_task_t *new_task);
5171     RegionCodeGenTy RCG(CodeGen);
5172     CommonActionTy Action(OMPBuilder.getOrCreateRuntimeFunction(
5173                               M, OMPRTL___kmpc_omp_task_begin_if0),
5174                           TaskArgs,
5175                           OMPBuilder.getOrCreateRuntimeFunction(
5176                               M, OMPRTL___kmpc_omp_task_complete_if0),
5177                           TaskArgs);
5178     RCG.setAction(Action);
5179     RCG(CGF);
5180   };
5181 
5182   if (IfCond) {
5183     emitIfClause(CGF, IfCond, ThenCodeGen, ElseCodeGen);
5184   } else {
5185     RegionCodeGenTy ThenRCG(ThenCodeGen);
5186     ThenRCG(CGF);
5187   }
5188 }
5189 
5190 void CGOpenMPRuntime::emitTaskLoopCall(CodeGenFunction &CGF, SourceLocation Loc,
5191                                        const OMPLoopDirective &D,
5192                                        llvm::Function *TaskFunction,
5193                                        QualType SharedsTy, Address Shareds,
5194                                        const Expr *IfCond,
5195                                        const OMPTaskDataTy &Data) {
5196   if (!CGF.HaveInsertPoint())
5197     return;
5198   TaskResultTy Result =
5199       emitTaskInit(CGF, Loc, D, TaskFunction, SharedsTy, Shareds, Data);
5200   // NOTE: routine and part_id fields are initialized by __kmpc_omp_task_alloc()
5201   // libcall.
5202   // Call to void __kmpc_taskloop(ident_t *loc, int gtid, kmp_task_t *task, int
5203   // if_val, kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st, int nogroup, int
5204   // sched, kmp_uint64 grainsize, void *task_dup);
5205   llvm::Value *ThreadID = getThreadID(CGF, Loc);
5206   llvm::Value *UpLoc = emitUpdateLocation(CGF, Loc);
5207   llvm::Value *IfVal;
5208   if (IfCond) {
5209     IfVal = CGF.Builder.CreateIntCast(CGF.EvaluateExprAsBool(IfCond), CGF.IntTy,
5210                                       /*isSigned=*/true);
5211   } else {
5212     IfVal = llvm::ConstantInt::getSigned(CGF.IntTy, /*V=*/1);
5213   }
5214 
5215   LValue LBLVal = CGF.EmitLValueForField(
5216       Result.TDBase,
5217       *std::next(Result.KmpTaskTQTyRD->field_begin(), KmpTaskTLowerBound));
5218   const auto *LBVar =
5219       cast<VarDecl>(cast<DeclRefExpr>(D.getLowerBoundVariable())->getDecl());
5220   CGF.EmitAnyExprToMem(LBVar->getInit(), LBLVal.getAddress(CGF),
5221                        LBLVal.getQuals(),
5222                        /*IsInitializer=*/true);
5223   LValue UBLVal = CGF.EmitLValueForField(
5224       Result.TDBase,
5225       *std::next(Result.KmpTaskTQTyRD->field_begin(), KmpTaskTUpperBound));
5226   const auto *UBVar =
5227       cast<VarDecl>(cast<DeclRefExpr>(D.getUpperBoundVariable())->getDecl());
5228   CGF.EmitAnyExprToMem(UBVar->getInit(), UBLVal.getAddress(CGF),
5229                        UBLVal.getQuals(),
5230                        /*IsInitializer=*/true);
5231   LValue StLVal = CGF.EmitLValueForField(
5232       Result.TDBase,
5233       *std::next(Result.KmpTaskTQTyRD->field_begin(), KmpTaskTStride));
5234   const auto *StVar =
5235       cast<VarDecl>(cast<DeclRefExpr>(D.getStrideVariable())->getDecl());
5236   CGF.EmitAnyExprToMem(StVar->getInit(), StLVal.getAddress(CGF),
5237                        StLVal.getQuals(),
5238                        /*IsInitializer=*/true);
5239   // Store reductions address.
5240   LValue RedLVal = CGF.EmitLValueForField(
5241       Result.TDBase,
5242       *std::next(Result.KmpTaskTQTyRD->field_begin(), KmpTaskTReductions));
5243   if (Data.Reductions) {
5244     CGF.EmitStoreOfScalar(Data.Reductions, RedLVal);
5245   } else {
5246     CGF.EmitNullInitialization(RedLVal.getAddress(CGF),
5247                                CGF.getContext().VoidPtrTy);
5248   }
5249   enum { NoSchedule = 0, Grainsize = 1, NumTasks = 2 };
5250   llvm::Value *TaskArgs[] = {
5251       UpLoc,
5252       ThreadID,
5253       Result.NewTask,
5254       IfVal,
5255       LBLVal.getPointer(CGF),
5256       UBLVal.getPointer(CGF),
5257       CGF.EmitLoadOfScalar(StLVal, Loc),
5258       llvm::ConstantInt::getSigned(
5259           CGF.IntTy, 1), // Always 1 because taskgroup emitted by the compiler
5260       llvm::ConstantInt::getSigned(
5261           CGF.IntTy, Data.Schedule.getPointer()
5262                          ? Data.Schedule.getInt() ? NumTasks : Grainsize
5263                          : NoSchedule),
5264       Data.Schedule.getPointer()
5265           ? CGF.Builder.CreateIntCast(Data.Schedule.getPointer(), CGF.Int64Ty,
5266                                       /*isSigned=*/false)
5267           : llvm::ConstantInt::get(CGF.Int64Ty, /*V=*/0),
5268       Result.TaskDupFn ? CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(
5269                              Result.TaskDupFn, CGF.VoidPtrTy)
5270                        : llvm::ConstantPointerNull::get(CGF.VoidPtrTy)};
5271   CGF.EmitRuntimeCall(OMPBuilder.getOrCreateRuntimeFunction(
5272                           CGM.getModule(), OMPRTL___kmpc_taskloop),
5273                       TaskArgs);
5274 }
5275 
5276 /// Emit reduction operation for each element of array (required for
5277 /// array sections) LHS op = RHS.
5278 /// \param Type Type of array.
5279 /// \param LHSVar Variable on the left side of the reduction operation
5280 /// (references element of array in original variable).
5281 /// \param RHSVar Variable on the right side of the reduction operation
5282 /// (references element of array in original variable).
5283 /// \param RedOpGen Generator of reduction operation with use of LHSVar and
5284 /// RHSVar.
5285 static void EmitOMPAggregateReduction(
5286     CodeGenFunction &CGF, QualType Type, const VarDecl *LHSVar,
5287     const VarDecl *RHSVar,
5288     const llvm::function_ref<void(CodeGenFunction &CGF, const Expr *,
5289                                   const Expr *, const Expr *)> &RedOpGen,
5290     const Expr *XExpr = nullptr, const Expr *EExpr = nullptr,
5291     const Expr *UpExpr = nullptr) {
5292   // Perform element-by-element initialization.
5293   QualType ElementTy;
5294   Address LHSAddr = CGF.GetAddrOfLocalVar(LHSVar);
5295   Address RHSAddr = CGF.GetAddrOfLocalVar(RHSVar);
5296 
5297   // Drill down to the base element type on both arrays.
5298   const ArrayType *ArrayTy = Type->getAsArrayTypeUnsafe();
5299   llvm::Value *NumElements = CGF.emitArrayLength(ArrayTy, ElementTy, LHSAddr);
5300 
5301   llvm::Value *RHSBegin = RHSAddr.getPointer();
5302   llvm::Value *LHSBegin = LHSAddr.getPointer();
5303   // Cast from pointer to array type to pointer to single element.
5304   llvm::Value *LHSEnd = CGF.Builder.CreateGEP(LHSBegin, NumElements);
5305   // The basic structure here is a while-do loop.
5306   llvm::BasicBlock *BodyBB = CGF.createBasicBlock("omp.arraycpy.body");
5307   llvm::BasicBlock *DoneBB = CGF.createBasicBlock("omp.arraycpy.done");
5308   llvm::Value *IsEmpty =
5309       CGF.Builder.CreateICmpEQ(LHSBegin, LHSEnd, "omp.arraycpy.isempty");
5310   CGF.Builder.CreateCondBr(IsEmpty, DoneBB, BodyBB);
5311 
5312   // Enter the loop body, making that address the current address.
5313   llvm::BasicBlock *EntryBB = CGF.Builder.GetInsertBlock();
5314   CGF.EmitBlock(BodyBB);
5315 
5316   CharUnits ElementSize = CGF.getContext().getTypeSizeInChars(ElementTy);
5317 
5318   llvm::PHINode *RHSElementPHI = CGF.Builder.CreatePHI(
5319       RHSBegin->getType(), 2, "omp.arraycpy.srcElementPast");
5320   RHSElementPHI->addIncoming(RHSBegin, EntryBB);
5321   Address RHSElementCurrent =
5322       Address(RHSElementPHI,
5323               RHSAddr.getAlignment().alignmentOfArrayElement(ElementSize));
5324 
5325   llvm::PHINode *LHSElementPHI = CGF.Builder.CreatePHI(
5326       LHSBegin->getType(), 2, "omp.arraycpy.destElementPast");
5327   LHSElementPHI->addIncoming(LHSBegin, EntryBB);
5328   Address LHSElementCurrent =
5329       Address(LHSElementPHI,
5330               LHSAddr.getAlignment().alignmentOfArrayElement(ElementSize));
5331 
5332   // Emit copy.
5333   CodeGenFunction::OMPPrivateScope Scope(CGF);
5334   Scope.addPrivate(LHSVar, [=]() { return LHSElementCurrent; });
5335   Scope.addPrivate(RHSVar, [=]() { return RHSElementCurrent; });
5336   Scope.Privatize();
5337   RedOpGen(CGF, XExpr, EExpr, UpExpr);
5338   Scope.ForceCleanup();
5339 
5340   // Shift the address forward by one element.
5341   llvm::Value *LHSElementNext = CGF.Builder.CreateConstGEP1_32(
5342       LHSElementPHI, /*Idx0=*/1, "omp.arraycpy.dest.element");
5343   llvm::Value *RHSElementNext = CGF.Builder.CreateConstGEP1_32(
5344       RHSElementPHI, /*Idx0=*/1, "omp.arraycpy.src.element");
5345   // Check whether we've reached the end.
5346   llvm::Value *Done =
5347       CGF.Builder.CreateICmpEQ(LHSElementNext, LHSEnd, "omp.arraycpy.done");
5348   CGF.Builder.CreateCondBr(Done, DoneBB, BodyBB);
5349   LHSElementPHI->addIncoming(LHSElementNext, CGF.Builder.GetInsertBlock());
5350   RHSElementPHI->addIncoming(RHSElementNext, CGF.Builder.GetInsertBlock());
5351 
5352   // Done.
5353   CGF.EmitBlock(DoneBB, /*IsFinished=*/true);
5354 }
5355 
5356 /// Emit reduction combiner. If the combiner is a simple expression emit it as
5357 /// is, otherwise consider it as combiner of UDR decl and emit it as a call of
5358 /// UDR combiner function.
5359 static void emitReductionCombiner(CodeGenFunction &CGF,
5360                                   const Expr *ReductionOp) {
5361   if (const auto *CE = dyn_cast<CallExpr>(ReductionOp))
5362     if (const auto *OVE = dyn_cast<OpaqueValueExpr>(CE->getCallee()))
5363       if (const auto *DRE =
5364               dyn_cast<DeclRefExpr>(OVE->getSourceExpr()->IgnoreImpCasts()))
5365         if (const auto *DRD =
5366                 dyn_cast<OMPDeclareReductionDecl>(DRE->getDecl())) {
5367           std::pair<llvm::Function *, llvm::Function *> Reduction =
5368               CGF.CGM.getOpenMPRuntime().getUserDefinedReduction(DRD);
5369           RValue Func = RValue::get(Reduction.first);
5370           CodeGenFunction::OpaqueValueMapping Map(CGF, OVE, Func);
5371           CGF.EmitIgnoredExpr(ReductionOp);
5372           return;
5373         }
5374   CGF.EmitIgnoredExpr(ReductionOp);
5375 }
5376 
5377 llvm::Function *CGOpenMPRuntime::emitReductionFunction(
5378     SourceLocation Loc, llvm::Type *ArgsType, ArrayRef<const Expr *> Privates,
5379     ArrayRef<const Expr *> LHSExprs, ArrayRef<const Expr *> RHSExprs,
5380     ArrayRef<const Expr *> ReductionOps) {
5381   ASTContext &C = CGM.getContext();
5382 
5383   // void reduction_func(void *LHSArg, void *RHSArg);
5384   FunctionArgList Args;
5385   ImplicitParamDecl LHSArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, C.VoidPtrTy,
5386                            ImplicitParamDecl::Other);
5387   ImplicitParamDecl RHSArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, C.VoidPtrTy,
5388                            ImplicitParamDecl::Other);
5389   Args.push_back(&LHSArg);
5390   Args.push_back(&RHSArg);
5391   const auto &CGFI =
5392       CGM.getTypes().arrangeBuiltinFunctionDeclaration(C.VoidTy, Args);
5393   std::string Name = getName({"omp", "reduction", "reduction_func"});
5394   auto *Fn = llvm::Function::Create(CGM.getTypes().GetFunctionType(CGFI),
5395                                     llvm::GlobalValue::InternalLinkage, Name,
5396                                     &CGM.getModule());
5397   CGM.SetInternalFunctionAttributes(GlobalDecl(), Fn, CGFI);
5398   Fn->setDoesNotRecurse();
5399   CodeGenFunction CGF(CGM);
5400   CGF.StartFunction(GlobalDecl(), C.VoidTy, Fn, CGFI, Args, Loc, Loc);
5401 
5402   // Dst = (void*[n])(LHSArg);
5403   // Src = (void*[n])(RHSArg);
5404   Address LHS(CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(
5405       CGF.Builder.CreateLoad(CGF.GetAddrOfLocalVar(&LHSArg)),
5406       ArgsType), CGF.getPointerAlign());
5407   Address RHS(CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(
5408       CGF.Builder.CreateLoad(CGF.GetAddrOfLocalVar(&RHSArg)),
5409       ArgsType), CGF.getPointerAlign());
5410 
5411   //  ...
5412   //  *(Type<i>*)lhs[i] = RedOp<i>(*(Type<i>*)lhs[i], *(Type<i>*)rhs[i]);
5413   //  ...
5414   CodeGenFunction::OMPPrivateScope Scope(CGF);
5415   auto IPriv = Privates.begin();
5416   unsigned Idx = 0;
5417   for (unsigned I = 0, E = ReductionOps.size(); I < E; ++I, ++IPriv, ++Idx) {
5418     const auto *RHSVar =
5419         cast<VarDecl>(cast<DeclRefExpr>(RHSExprs[I])->getDecl());
5420     Scope.addPrivate(RHSVar, [&CGF, RHS, Idx, RHSVar]() {
5421       return emitAddrOfVarFromArray(CGF, RHS, Idx, RHSVar);
5422     });
5423     const auto *LHSVar =
5424         cast<VarDecl>(cast<DeclRefExpr>(LHSExprs[I])->getDecl());
5425     Scope.addPrivate(LHSVar, [&CGF, LHS, Idx, LHSVar]() {
5426       return emitAddrOfVarFromArray(CGF, LHS, Idx, LHSVar);
5427     });
5428     QualType PrivTy = (*IPriv)->getType();
5429     if (PrivTy->isVariablyModifiedType()) {
5430       // Get array size and emit VLA type.
5431       ++Idx;
5432       Address Elem = CGF.Builder.CreateConstArrayGEP(LHS, Idx);
5433       llvm::Value *Ptr = CGF.Builder.CreateLoad(Elem);
5434       const VariableArrayType *VLA =
5435           CGF.getContext().getAsVariableArrayType(PrivTy);
5436       const auto *OVE = cast<OpaqueValueExpr>(VLA->getSizeExpr());
5437       CodeGenFunction::OpaqueValueMapping OpaqueMap(
5438           CGF, OVE, RValue::get(CGF.Builder.CreatePtrToInt(Ptr, CGF.SizeTy)));
5439       CGF.EmitVariablyModifiedType(PrivTy);
5440     }
5441   }
5442   Scope.Privatize();
5443   IPriv = Privates.begin();
5444   auto ILHS = LHSExprs.begin();
5445   auto IRHS = RHSExprs.begin();
5446   for (const Expr *E : ReductionOps) {
5447     if ((*IPriv)->getType()->isArrayType()) {
5448       // Emit reduction for array section.
5449       const auto *LHSVar = cast<VarDecl>(cast<DeclRefExpr>(*ILHS)->getDecl());
5450       const auto *RHSVar = cast<VarDecl>(cast<DeclRefExpr>(*IRHS)->getDecl());
5451       EmitOMPAggregateReduction(
5452           CGF, (*IPriv)->getType(), LHSVar, RHSVar,
5453           [=](CodeGenFunction &CGF, const Expr *, const Expr *, const Expr *) {
5454             emitReductionCombiner(CGF, E);
5455           });
5456     } else {
5457       // Emit reduction for array subscript or single variable.
5458       emitReductionCombiner(CGF, E);
5459     }
5460     ++IPriv;
5461     ++ILHS;
5462     ++IRHS;
5463   }
5464   Scope.ForceCleanup();
5465   CGF.FinishFunction();
5466   return Fn;
5467 }
5468 
5469 void CGOpenMPRuntime::emitSingleReductionCombiner(CodeGenFunction &CGF,
5470                                                   const Expr *ReductionOp,
5471                                                   const Expr *PrivateRef,
5472                                                   const DeclRefExpr *LHS,
5473                                                   const DeclRefExpr *RHS) {
5474   if (PrivateRef->getType()->isArrayType()) {
5475     // Emit reduction for array section.
5476     const auto *LHSVar = cast<VarDecl>(LHS->getDecl());
5477     const auto *RHSVar = cast<VarDecl>(RHS->getDecl());
5478     EmitOMPAggregateReduction(
5479         CGF, PrivateRef->getType(), LHSVar, RHSVar,
5480         [=](CodeGenFunction &CGF, const Expr *, const Expr *, const Expr *) {
5481           emitReductionCombiner(CGF, ReductionOp);
5482         });
5483   } else {
5484     // Emit reduction for array subscript or single variable.
5485     emitReductionCombiner(CGF, ReductionOp);
5486   }
5487 }
5488 
5489 void CGOpenMPRuntime::emitReduction(CodeGenFunction &CGF, SourceLocation Loc,
5490                                     ArrayRef<const Expr *> Privates,
5491                                     ArrayRef<const Expr *> LHSExprs,
5492                                     ArrayRef<const Expr *> RHSExprs,
5493                                     ArrayRef<const Expr *> ReductionOps,
5494                                     ReductionOptionsTy Options) {
5495   if (!CGF.HaveInsertPoint())
5496     return;
5497 
5498   bool WithNowait = Options.WithNowait;
5499   bool SimpleReduction = Options.SimpleReduction;
5500 
5501   // Next code should be emitted for reduction:
5502   //
5503   // static kmp_critical_name lock = { 0 };
5504   //
5505   // void reduce_func(void *lhs[<n>], void *rhs[<n>]) {
5506   //  *(Type0*)lhs[0] = ReductionOperation0(*(Type0*)lhs[0], *(Type0*)rhs[0]);
5507   //  ...
5508   //  *(Type<n>-1*)lhs[<n>-1] = ReductionOperation<n>-1(*(Type<n>-1*)lhs[<n>-1],
5509   //  *(Type<n>-1*)rhs[<n>-1]);
5510   // }
5511   //
5512   // ...
5513   // void *RedList[<n>] = {&<RHSExprs>[0], ..., &<RHSExprs>[<n>-1]};
5514   // switch (__kmpc_reduce{_nowait}(<loc>, <gtid>, <n>, sizeof(RedList),
5515   // RedList, reduce_func, &<lock>)) {
5516   // case 1:
5517   //  ...
5518   //  <LHSExprs>[i] = RedOp<i>(*<LHSExprs>[i], *<RHSExprs>[i]);
5519   //  ...
5520   // __kmpc_end_reduce{_nowait}(<loc>, <gtid>, &<lock>);
5521   // break;
5522   // case 2:
5523   //  ...
5524   //  Atomic(<LHSExprs>[i] = RedOp<i>(*<LHSExprs>[i], *<RHSExprs>[i]));
5525   //  ...
5526   // [__kmpc_end_reduce(<loc>, <gtid>, &<lock>);]
5527   // break;
5528   // default:;
5529   // }
5530   //
5531   // if SimpleReduction is true, only the next code is generated:
5532   //  ...
5533   //  <LHSExprs>[i] = RedOp<i>(*<LHSExprs>[i], *<RHSExprs>[i]);
5534   //  ...
5535 
5536   ASTContext &C = CGM.getContext();
5537 
5538   if (SimpleReduction) {
5539     CodeGenFunction::RunCleanupsScope Scope(CGF);
5540     auto IPriv = Privates.begin();
5541     auto ILHS = LHSExprs.begin();
5542     auto IRHS = RHSExprs.begin();
5543     for (const Expr *E : ReductionOps) {
5544       emitSingleReductionCombiner(CGF, E, *IPriv, cast<DeclRefExpr>(*ILHS),
5545                                   cast<DeclRefExpr>(*IRHS));
5546       ++IPriv;
5547       ++ILHS;
5548       ++IRHS;
5549     }
5550     return;
5551   }
5552 
5553   // 1. Build a list of reduction variables.
5554   // void *RedList[<n>] = {<ReductionVars>[0], ..., <ReductionVars>[<n>-1]};
5555   auto Size = RHSExprs.size();
5556   for (const Expr *E : Privates) {
5557     if (E->getType()->isVariablyModifiedType())
5558       // Reserve place for array size.
5559       ++Size;
5560   }
5561   llvm::APInt ArraySize(/*unsigned int numBits=*/32, Size);
5562   QualType ReductionArrayTy =
5563       C.getConstantArrayType(C.VoidPtrTy, ArraySize, nullptr, ArrayType::Normal,
5564                              /*IndexTypeQuals=*/0);
5565   Address ReductionList =
5566       CGF.CreateMemTemp(ReductionArrayTy, ".omp.reduction.red_list");
5567   auto IPriv = Privates.begin();
5568   unsigned Idx = 0;
5569   for (unsigned I = 0, E = RHSExprs.size(); I < E; ++I, ++IPriv, ++Idx) {
5570     Address Elem = CGF.Builder.CreateConstArrayGEP(ReductionList, Idx);
5571     CGF.Builder.CreateStore(
5572         CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(
5573             CGF.EmitLValue(RHSExprs[I]).getPointer(CGF), CGF.VoidPtrTy),
5574         Elem);
5575     if ((*IPriv)->getType()->isVariablyModifiedType()) {
5576       // Store array size.
5577       ++Idx;
5578       Elem = CGF.Builder.CreateConstArrayGEP(ReductionList, Idx);
5579       llvm::Value *Size = CGF.Builder.CreateIntCast(
5580           CGF.getVLASize(
5581                  CGF.getContext().getAsVariableArrayType((*IPriv)->getType()))
5582               .NumElts,
5583           CGF.SizeTy, /*isSigned=*/false);
5584       CGF.Builder.CreateStore(CGF.Builder.CreateIntToPtr(Size, CGF.VoidPtrTy),
5585                               Elem);
5586     }
5587   }
5588 
5589   // 2. Emit reduce_func().
5590   llvm::Function *ReductionFn = emitReductionFunction(
5591       Loc, CGF.ConvertTypeForMem(ReductionArrayTy)->getPointerTo(), Privates,
5592       LHSExprs, RHSExprs, ReductionOps);
5593 
5594   // 3. Create static kmp_critical_name lock = { 0 };
5595   std::string Name = getName({"reduction"});
5596   llvm::Value *Lock = getCriticalRegionLock(Name);
5597 
5598   // 4. Build res = __kmpc_reduce{_nowait}(<loc>, <gtid>, <n>, sizeof(RedList),
5599   // RedList, reduce_func, &<lock>);
5600   llvm::Value *IdentTLoc = emitUpdateLocation(CGF, Loc, OMP_ATOMIC_REDUCE);
5601   llvm::Value *ThreadId = getThreadID(CGF, Loc);
5602   llvm::Value *ReductionArrayTySize = CGF.getTypeSize(ReductionArrayTy);
5603   llvm::Value *RL = CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(
5604       ReductionList.getPointer(), CGF.VoidPtrTy);
5605   llvm::Value *Args[] = {
5606       IdentTLoc,                             // ident_t *<loc>
5607       ThreadId,                              // i32 <gtid>
5608       CGF.Builder.getInt32(RHSExprs.size()), // i32 <n>
5609       ReductionArrayTySize,                  // size_type sizeof(RedList)
5610       RL,                                    // void *RedList
5611       ReductionFn, // void (*) (void *, void *) <reduce_func>
5612       Lock         // kmp_critical_name *&<lock>
5613   };
5614   llvm::Value *Res = CGF.EmitRuntimeCall(
5615       OMPBuilder.getOrCreateRuntimeFunction(
5616           CGM.getModule(),
5617           WithNowait ? OMPRTL___kmpc_reduce_nowait : OMPRTL___kmpc_reduce),
5618       Args);
5619 
5620   // 5. Build switch(res)
5621   llvm::BasicBlock *DefaultBB = CGF.createBasicBlock(".omp.reduction.default");
5622   llvm::SwitchInst *SwInst =
5623       CGF.Builder.CreateSwitch(Res, DefaultBB, /*NumCases=*/2);
5624 
5625   // 6. Build case 1:
5626   //  ...
5627   //  <LHSExprs>[i] = RedOp<i>(*<LHSExprs>[i], *<RHSExprs>[i]);
5628   //  ...
5629   // __kmpc_end_reduce{_nowait}(<loc>, <gtid>, &<lock>);
5630   // break;
5631   llvm::BasicBlock *Case1BB = CGF.createBasicBlock(".omp.reduction.case1");
5632   SwInst->addCase(CGF.Builder.getInt32(1), Case1BB);
5633   CGF.EmitBlock(Case1BB);
5634 
5635   // Add emission of __kmpc_end_reduce{_nowait}(<loc>, <gtid>, &<lock>);
5636   llvm::Value *EndArgs[] = {
5637       IdentTLoc, // ident_t *<loc>
5638       ThreadId,  // i32 <gtid>
5639       Lock       // kmp_critical_name *&<lock>
5640   };
5641   auto &&CodeGen = [Privates, LHSExprs, RHSExprs, ReductionOps](
5642                        CodeGenFunction &CGF, PrePostActionTy &Action) {
5643     CGOpenMPRuntime &RT = CGF.CGM.getOpenMPRuntime();
5644     auto IPriv = Privates.begin();
5645     auto ILHS = LHSExprs.begin();
5646     auto IRHS = RHSExprs.begin();
5647     for (const Expr *E : ReductionOps) {
5648       RT.emitSingleReductionCombiner(CGF, E, *IPriv, cast<DeclRefExpr>(*ILHS),
5649                                      cast<DeclRefExpr>(*IRHS));
5650       ++IPriv;
5651       ++ILHS;
5652       ++IRHS;
5653     }
5654   };
5655   RegionCodeGenTy RCG(CodeGen);
5656   CommonActionTy Action(
5657       nullptr, llvm::None,
5658       OMPBuilder.getOrCreateRuntimeFunction(
5659           CGM.getModule(), WithNowait ? OMPRTL___kmpc_end_reduce_nowait
5660                                       : OMPRTL___kmpc_end_reduce),
5661       EndArgs);
5662   RCG.setAction(Action);
5663   RCG(CGF);
5664 
5665   CGF.EmitBranch(DefaultBB);
5666 
5667   // 7. Build case 2:
5668   //  ...
5669   //  Atomic(<LHSExprs>[i] = RedOp<i>(*<LHSExprs>[i], *<RHSExprs>[i]));
5670   //  ...
5671   // break;
5672   llvm::BasicBlock *Case2BB = CGF.createBasicBlock(".omp.reduction.case2");
5673   SwInst->addCase(CGF.Builder.getInt32(2), Case2BB);
5674   CGF.EmitBlock(Case2BB);
5675 
5676   auto &&AtomicCodeGen = [Loc, Privates, LHSExprs, RHSExprs, ReductionOps](
5677                              CodeGenFunction &CGF, PrePostActionTy &Action) {
5678     auto ILHS = LHSExprs.begin();
5679     auto IRHS = RHSExprs.begin();
5680     auto IPriv = Privates.begin();
5681     for (const Expr *E : ReductionOps) {
5682       const Expr *XExpr = nullptr;
5683       const Expr *EExpr = nullptr;
5684       const Expr *UpExpr = nullptr;
5685       BinaryOperatorKind BO = BO_Comma;
5686       if (const auto *BO = dyn_cast<BinaryOperator>(E)) {
5687         if (BO->getOpcode() == BO_Assign) {
5688           XExpr = BO->getLHS();
5689           UpExpr = BO->getRHS();
5690         }
5691       }
5692       // Try to emit update expression as a simple atomic.
5693       const Expr *RHSExpr = UpExpr;
5694       if (RHSExpr) {
5695         // Analyze RHS part of the whole expression.
5696         if (const auto *ACO = dyn_cast<AbstractConditionalOperator>(
5697                 RHSExpr->IgnoreParenImpCasts())) {
5698           // If this is a conditional operator, analyze its condition for
5699           // min/max reduction operator.
5700           RHSExpr = ACO->getCond();
5701         }
5702         if (const auto *BORHS =
5703                 dyn_cast<BinaryOperator>(RHSExpr->IgnoreParenImpCasts())) {
5704           EExpr = BORHS->getRHS();
5705           BO = BORHS->getOpcode();
5706         }
5707       }
5708       if (XExpr) {
5709         const auto *VD = cast<VarDecl>(cast<DeclRefExpr>(*ILHS)->getDecl());
5710         auto &&AtomicRedGen = [BO, VD,
5711                                Loc](CodeGenFunction &CGF, const Expr *XExpr,
5712                                     const Expr *EExpr, const Expr *UpExpr) {
5713           LValue X = CGF.EmitLValue(XExpr);
5714           RValue E;
5715           if (EExpr)
5716             E = CGF.EmitAnyExpr(EExpr);
5717           CGF.EmitOMPAtomicSimpleUpdateExpr(
5718               X, E, BO, /*IsXLHSInRHSPart=*/true,
5719               llvm::AtomicOrdering::Monotonic, Loc,
5720               [&CGF, UpExpr, VD, Loc](RValue XRValue) {
5721                 CodeGenFunction::OMPPrivateScope PrivateScope(CGF);
5722                 PrivateScope.addPrivate(
5723                     VD, [&CGF, VD, XRValue, Loc]() {
5724                       Address LHSTemp = CGF.CreateMemTemp(VD->getType());
5725                       CGF.emitOMPSimpleStore(
5726                           CGF.MakeAddrLValue(LHSTemp, VD->getType()), XRValue,
5727                           VD->getType().getNonReferenceType(), Loc);
5728                       return LHSTemp;
5729                     });
5730                 (void)PrivateScope.Privatize();
5731                 return CGF.EmitAnyExpr(UpExpr);
5732               });
5733         };
5734         if ((*IPriv)->getType()->isArrayType()) {
5735           // Emit atomic reduction for array section.
5736           const auto *RHSVar =
5737               cast<VarDecl>(cast<DeclRefExpr>(*IRHS)->getDecl());
5738           EmitOMPAggregateReduction(CGF, (*IPriv)->getType(), VD, RHSVar,
5739                                     AtomicRedGen, XExpr, EExpr, UpExpr);
5740         } else {
5741           // Emit atomic reduction for array subscript or single variable.
5742           AtomicRedGen(CGF, XExpr, EExpr, UpExpr);
5743         }
5744       } else {
5745         // Emit as a critical region.
5746         auto &&CritRedGen = [E, Loc](CodeGenFunction &CGF, const Expr *,
5747                                            const Expr *, const Expr *) {
5748           CGOpenMPRuntime &RT = CGF.CGM.getOpenMPRuntime();
5749           std::string Name = RT.getName({"atomic_reduction"});
5750           RT.emitCriticalRegion(
5751               CGF, Name,
5752               [=](CodeGenFunction &CGF, PrePostActionTy &Action) {
5753                 Action.Enter(CGF);
5754                 emitReductionCombiner(CGF, E);
5755               },
5756               Loc);
5757         };
5758         if ((*IPriv)->getType()->isArrayType()) {
5759           const auto *LHSVar =
5760               cast<VarDecl>(cast<DeclRefExpr>(*ILHS)->getDecl());
5761           const auto *RHSVar =
5762               cast<VarDecl>(cast<DeclRefExpr>(*IRHS)->getDecl());
5763           EmitOMPAggregateReduction(CGF, (*IPriv)->getType(), LHSVar, RHSVar,
5764                                     CritRedGen);
5765         } else {
5766           CritRedGen(CGF, nullptr, nullptr, nullptr);
5767         }
5768       }
5769       ++ILHS;
5770       ++IRHS;
5771       ++IPriv;
5772     }
5773   };
5774   RegionCodeGenTy AtomicRCG(AtomicCodeGen);
5775   if (!WithNowait) {
5776     // Add emission of __kmpc_end_reduce(<loc>, <gtid>, &<lock>);
5777     llvm::Value *EndArgs[] = {
5778         IdentTLoc, // ident_t *<loc>
5779         ThreadId,  // i32 <gtid>
5780         Lock       // kmp_critical_name *&<lock>
5781     };
5782     CommonActionTy Action(nullptr, llvm::None,
5783                           OMPBuilder.getOrCreateRuntimeFunction(
5784                               CGM.getModule(), OMPRTL___kmpc_end_reduce),
5785                           EndArgs);
5786     AtomicRCG.setAction(Action);
5787     AtomicRCG(CGF);
5788   } else {
5789     AtomicRCG(CGF);
5790   }
5791 
5792   CGF.EmitBranch(DefaultBB);
5793   CGF.EmitBlock(DefaultBB, /*IsFinished=*/true);
5794 }
5795 
5796 /// Generates unique name for artificial threadprivate variables.
5797 /// Format is: <Prefix> "." <Decl_mangled_name> "_" "<Decl_start_loc_raw_enc>"
5798 static std::string generateUniqueName(CodeGenModule &CGM, StringRef Prefix,
5799                                       const Expr *Ref) {
5800   SmallString<256> Buffer;
5801   llvm::raw_svector_ostream Out(Buffer);
5802   const clang::DeclRefExpr *DE;
5803   const VarDecl *D = ::getBaseDecl(Ref, DE);
5804   if (!D)
5805     D = cast<VarDecl>(cast<DeclRefExpr>(Ref)->getDecl());
5806   D = D->getCanonicalDecl();
5807   std::string Name = CGM.getOpenMPRuntime().getName(
5808       {D->isLocalVarDeclOrParm() ? D->getName() : CGM.getMangledName(D)});
5809   Out << Prefix << Name << "_"
5810       << D->getCanonicalDecl()->getBeginLoc().getRawEncoding();
5811   return std::string(Out.str());
5812 }
5813 
5814 /// Emits reduction initializer function:
5815 /// \code
5816 /// void @.red_init(void* %arg, void* %orig) {
5817 /// %0 = bitcast void* %arg to <type>*
5818 /// store <type> <init>, <type>* %0
5819 /// ret void
5820 /// }
5821 /// \endcode
5822 static llvm::Value *emitReduceInitFunction(CodeGenModule &CGM,
5823                                            SourceLocation Loc,
5824                                            ReductionCodeGen &RCG, unsigned N) {
5825   ASTContext &C = CGM.getContext();
5826   QualType VoidPtrTy = C.VoidPtrTy;
5827   VoidPtrTy.addRestrict();
5828   FunctionArgList Args;
5829   ImplicitParamDecl Param(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, VoidPtrTy,
5830                           ImplicitParamDecl::Other);
5831   ImplicitParamDecl ParamOrig(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, VoidPtrTy,
5832                               ImplicitParamDecl::Other);
5833   Args.emplace_back(&Param);
5834   Args.emplace_back(&ParamOrig);
5835   const auto &FnInfo =
5836       CGM.getTypes().arrangeBuiltinFunctionDeclaration(C.VoidTy, Args);
5837   llvm::FunctionType *FnTy = CGM.getTypes().GetFunctionType(FnInfo);
5838   std::string Name = CGM.getOpenMPRuntime().getName({"red_init", ""});
5839   auto *Fn = llvm::Function::Create(FnTy, llvm::GlobalValue::InternalLinkage,
5840                                     Name, &CGM.getModule());
5841   CGM.SetInternalFunctionAttributes(GlobalDecl(), Fn, FnInfo);
5842   Fn->setDoesNotRecurse();
5843   CodeGenFunction CGF(CGM);
5844   CGF.StartFunction(GlobalDecl(), C.VoidTy, Fn, FnInfo, Args, Loc, Loc);
5845   Address PrivateAddr = CGF.EmitLoadOfPointer(
5846       CGF.GetAddrOfLocalVar(&Param),
5847       C.getPointerType(C.VoidPtrTy).castAs<PointerType>());
5848   llvm::Value *Size = nullptr;
5849   // If the size of the reduction item is non-constant, load it from global
5850   // threadprivate variable.
5851   if (RCG.getSizes(N).second) {
5852     Address SizeAddr = CGM.getOpenMPRuntime().getAddrOfArtificialThreadPrivate(
5853         CGF, CGM.getContext().getSizeType(),
5854         generateUniqueName(CGM, "reduction_size", RCG.getRefExpr(N)));
5855     Size = CGF.EmitLoadOfScalar(SizeAddr, /*Volatile=*/false,
5856                                 CGM.getContext().getSizeType(), Loc);
5857   }
5858   RCG.emitAggregateType(CGF, N, Size);
5859   LValue OrigLVal;
5860   // If initializer uses initializer from declare reduction construct, emit a
5861   // pointer to the address of the original reduction item (reuired by reduction
5862   // initializer)
5863   if (RCG.usesReductionInitializer(N)) {
5864     Address SharedAddr = CGF.GetAddrOfLocalVar(&ParamOrig);
5865     SharedAddr = CGF.EmitLoadOfPointer(
5866         SharedAddr,
5867         CGM.getContext().VoidPtrTy.castAs<PointerType>()->getTypePtr());
5868     OrigLVal = CGF.MakeAddrLValue(SharedAddr, CGM.getContext().VoidPtrTy);
5869   } else {
5870     OrigLVal = CGF.MakeNaturalAlignAddrLValue(
5871         llvm::ConstantPointerNull::get(CGM.VoidPtrTy),
5872         CGM.getContext().VoidPtrTy);
5873   }
5874   // Emit the initializer:
5875   // %0 = bitcast void* %arg to <type>*
5876   // store <type> <init>, <type>* %0
5877   RCG.emitInitialization(CGF, N, PrivateAddr, OrigLVal,
5878                          [](CodeGenFunction &) { return false; });
5879   CGF.FinishFunction();
5880   return Fn;
5881 }
5882 
5883 /// Emits reduction combiner function:
5884 /// \code
5885 /// void @.red_comb(void* %arg0, void* %arg1) {
5886 /// %lhs = bitcast void* %arg0 to <type>*
5887 /// %rhs = bitcast void* %arg1 to <type>*
5888 /// %2 = <ReductionOp>(<type>* %lhs, <type>* %rhs)
5889 /// store <type> %2, <type>* %lhs
5890 /// ret void
5891 /// }
5892 /// \endcode
5893 static llvm::Value *emitReduceCombFunction(CodeGenModule &CGM,
5894                                            SourceLocation Loc,
5895                                            ReductionCodeGen &RCG, unsigned N,
5896                                            const Expr *ReductionOp,
5897                                            const Expr *LHS, const Expr *RHS,
5898                                            const Expr *PrivateRef) {
5899   ASTContext &C = CGM.getContext();
5900   const auto *LHSVD = cast<VarDecl>(cast<DeclRefExpr>(LHS)->getDecl());
5901   const auto *RHSVD = cast<VarDecl>(cast<DeclRefExpr>(RHS)->getDecl());
5902   FunctionArgList Args;
5903   ImplicitParamDecl ParamInOut(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr,
5904                                C.VoidPtrTy, ImplicitParamDecl::Other);
5905   ImplicitParamDecl ParamIn(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, C.VoidPtrTy,
5906                             ImplicitParamDecl::Other);
5907   Args.emplace_back(&ParamInOut);
5908   Args.emplace_back(&ParamIn);
5909   const auto &FnInfo =
5910       CGM.getTypes().arrangeBuiltinFunctionDeclaration(C.VoidTy, Args);
5911   llvm::FunctionType *FnTy = CGM.getTypes().GetFunctionType(FnInfo);
5912   std::string Name = CGM.getOpenMPRuntime().getName({"red_comb", ""});
5913   auto *Fn = llvm::Function::Create(FnTy, llvm::GlobalValue::InternalLinkage,
5914                                     Name, &CGM.getModule());
5915   CGM.SetInternalFunctionAttributes(GlobalDecl(), Fn, FnInfo);
5916   Fn->setDoesNotRecurse();
5917   CodeGenFunction CGF(CGM);
5918   CGF.StartFunction(GlobalDecl(), C.VoidTy, Fn, FnInfo, Args, Loc, Loc);
5919   llvm::Value *Size = nullptr;
5920   // If the size of the reduction item is non-constant, load it from global
5921   // threadprivate variable.
5922   if (RCG.getSizes(N).second) {
5923     Address SizeAddr = CGM.getOpenMPRuntime().getAddrOfArtificialThreadPrivate(
5924         CGF, CGM.getContext().getSizeType(),
5925         generateUniqueName(CGM, "reduction_size", RCG.getRefExpr(N)));
5926     Size = CGF.EmitLoadOfScalar(SizeAddr, /*Volatile=*/false,
5927                                 CGM.getContext().getSizeType(), Loc);
5928   }
5929   RCG.emitAggregateType(CGF, N, Size);
5930   // Remap lhs and rhs variables to the addresses of the function arguments.
5931   // %lhs = bitcast void* %arg0 to <type>*
5932   // %rhs = bitcast void* %arg1 to <type>*
5933   CodeGenFunction::OMPPrivateScope PrivateScope(CGF);
5934   PrivateScope.addPrivate(LHSVD, [&C, &CGF, &ParamInOut, LHSVD]() {
5935     // Pull out the pointer to the variable.
5936     Address PtrAddr = CGF.EmitLoadOfPointer(
5937         CGF.GetAddrOfLocalVar(&ParamInOut),
5938         C.getPointerType(C.VoidPtrTy).castAs<PointerType>());
5939     return CGF.Builder.CreateElementBitCast(
5940         PtrAddr, CGF.ConvertTypeForMem(LHSVD->getType()));
5941   });
5942   PrivateScope.addPrivate(RHSVD, [&C, &CGF, &ParamIn, RHSVD]() {
5943     // Pull out the pointer to the variable.
5944     Address PtrAddr = CGF.EmitLoadOfPointer(
5945         CGF.GetAddrOfLocalVar(&ParamIn),
5946         C.getPointerType(C.VoidPtrTy).castAs<PointerType>());
5947     return CGF.Builder.CreateElementBitCast(
5948         PtrAddr, CGF.ConvertTypeForMem(RHSVD->getType()));
5949   });
5950   PrivateScope.Privatize();
5951   // Emit the combiner body:
5952   // %2 = <ReductionOp>(<type> *%lhs, <type> *%rhs)
5953   // store <type> %2, <type>* %lhs
5954   CGM.getOpenMPRuntime().emitSingleReductionCombiner(
5955       CGF, ReductionOp, PrivateRef, cast<DeclRefExpr>(LHS),
5956       cast<DeclRefExpr>(RHS));
5957   CGF.FinishFunction();
5958   return Fn;
5959 }
5960 
5961 /// Emits reduction finalizer function:
5962 /// \code
5963 /// void @.red_fini(void* %arg) {
5964 /// %0 = bitcast void* %arg to <type>*
5965 /// <destroy>(<type>* %0)
5966 /// ret void
5967 /// }
5968 /// \endcode
5969 static llvm::Value *emitReduceFiniFunction(CodeGenModule &CGM,
5970                                            SourceLocation Loc,
5971                                            ReductionCodeGen &RCG, unsigned N) {
5972   if (!RCG.needCleanups(N))
5973     return nullptr;
5974   ASTContext &C = CGM.getContext();
5975   FunctionArgList Args;
5976   ImplicitParamDecl Param(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, C.VoidPtrTy,
5977                           ImplicitParamDecl::Other);
5978   Args.emplace_back(&Param);
5979   const auto &FnInfo =
5980       CGM.getTypes().arrangeBuiltinFunctionDeclaration(C.VoidTy, Args);
5981   llvm::FunctionType *FnTy = CGM.getTypes().GetFunctionType(FnInfo);
5982   std::string Name = CGM.getOpenMPRuntime().getName({"red_fini", ""});
5983   auto *Fn = llvm::Function::Create(FnTy, llvm::GlobalValue::InternalLinkage,
5984                                     Name, &CGM.getModule());
5985   CGM.SetInternalFunctionAttributes(GlobalDecl(), Fn, FnInfo);
5986   Fn->setDoesNotRecurse();
5987   CodeGenFunction CGF(CGM);
5988   CGF.StartFunction(GlobalDecl(), C.VoidTy, Fn, FnInfo, Args, Loc, Loc);
5989   Address PrivateAddr = CGF.EmitLoadOfPointer(
5990       CGF.GetAddrOfLocalVar(&Param),
5991       C.getPointerType(C.VoidPtrTy).castAs<PointerType>());
5992   llvm::Value *Size = nullptr;
5993   // If the size of the reduction item is non-constant, load it from global
5994   // threadprivate variable.
5995   if (RCG.getSizes(N).second) {
5996     Address SizeAddr = CGM.getOpenMPRuntime().getAddrOfArtificialThreadPrivate(
5997         CGF, CGM.getContext().getSizeType(),
5998         generateUniqueName(CGM, "reduction_size", RCG.getRefExpr(N)));
5999     Size = CGF.EmitLoadOfScalar(SizeAddr, /*Volatile=*/false,
6000                                 CGM.getContext().getSizeType(), Loc);
6001   }
6002   RCG.emitAggregateType(CGF, N, Size);
6003   // Emit the finalizer body:
6004   // <destroy>(<type>* %0)
6005   RCG.emitCleanups(CGF, N, PrivateAddr);
6006   CGF.FinishFunction(Loc);
6007   return Fn;
6008 }
6009 
6010 llvm::Value *CGOpenMPRuntime::emitTaskReductionInit(
6011     CodeGenFunction &CGF, SourceLocation Loc, ArrayRef<const Expr *> LHSExprs,
6012     ArrayRef<const Expr *> RHSExprs, const OMPTaskDataTy &Data) {
6013   if (!CGF.HaveInsertPoint() || Data.ReductionVars.empty())
6014     return nullptr;
6015 
6016   // Build typedef struct:
6017   // kmp_taskred_input {
6018   //   void *reduce_shar; // shared reduction item
6019   //   void *reduce_orig; // original reduction item used for initialization
6020   //   size_t reduce_size; // size of data item
6021   //   void *reduce_init; // data initialization routine
6022   //   void *reduce_fini; // data finalization routine
6023   //   void *reduce_comb; // data combiner routine
6024   //   kmp_task_red_flags_t flags; // flags for additional info from compiler
6025   // } kmp_taskred_input_t;
6026   ASTContext &C = CGM.getContext();
6027   RecordDecl *RD = C.buildImplicitRecord("kmp_taskred_input_t");
6028   RD->startDefinition();
6029   const FieldDecl *SharedFD = addFieldToRecordDecl(C, RD, C.VoidPtrTy);
6030   const FieldDecl *OrigFD = addFieldToRecordDecl(C, RD, C.VoidPtrTy);
6031   const FieldDecl *SizeFD = addFieldToRecordDecl(C, RD, C.getSizeType());
6032   const FieldDecl *InitFD  = addFieldToRecordDecl(C, RD, C.VoidPtrTy);
6033   const FieldDecl *FiniFD = addFieldToRecordDecl(C, RD, C.VoidPtrTy);
6034   const FieldDecl *CombFD = addFieldToRecordDecl(C, RD, C.VoidPtrTy);
6035   const FieldDecl *FlagsFD = addFieldToRecordDecl(
6036       C, RD, C.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/false));
6037   RD->completeDefinition();
6038   QualType RDType = C.getRecordType(RD);
6039   unsigned Size = Data.ReductionVars.size();
6040   llvm::APInt ArraySize(/*numBits=*/64, Size);
6041   QualType ArrayRDType = C.getConstantArrayType(
6042       RDType, ArraySize, nullptr, ArrayType::Normal, /*IndexTypeQuals=*/0);
6043   // kmp_task_red_input_t .rd_input.[Size];
6044   Address TaskRedInput = CGF.CreateMemTemp(ArrayRDType, ".rd_input.");
6045   ReductionCodeGen RCG(Data.ReductionVars, Data.ReductionOrigs,
6046                        Data.ReductionCopies, Data.ReductionOps);
6047   for (unsigned Cnt = 0; Cnt < Size; ++Cnt) {
6048     // kmp_task_red_input_t &ElemLVal = .rd_input.[Cnt];
6049     llvm::Value *Idxs[] = {llvm::ConstantInt::get(CGM.SizeTy, /*V=*/0),
6050                            llvm::ConstantInt::get(CGM.SizeTy, Cnt)};
6051     llvm::Value *GEP = CGF.EmitCheckedInBoundsGEP(
6052         TaskRedInput.getPointer(), Idxs,
6053         /*SignedIndices=*/false, /*IsSubtraction=*/false, Loc,
6054         ".rd_input.gep.");
6055     LValue ElemLVal = CGF.MakeNaturalAlignAddrLValue(GEP, RDType);
6056     // ElemLVal.reduce_shar = &Shareds[Cnt];
6057     LValue SharedLVal = CGF.EmitLValueForField(ElemLVal, SharedFD);
6058     RCG.emitSharedOrigLValue(CGF, Cnt);
6059     llvm::Value *CastedShared =
6060         CGF.EmitCastToVoidPtr(RCG.getSharedLValue(Cnt).getPointer(CGF));
6061     CGF.EmitStoreOfScalar(CastedShared, SharedLVal);
6062     // ElemLVal.reduce_orig = &Origs[Cnt];
6063     LValue OrigLVal = CGF.EmitLValueForField(ElemLVal, OrigFD);
6064     llvm::Value *CastedOrig =
6065         CGF.EmitCastToVoidPtr(RCG.getOrigLValue(Cnt).getPointer(CGF));
6066     CGF.EmitStoreOfScalar(CastedOrig, OrigLVal);
6067     RCG.emitAggregateType(CGF, Cnt);
6068     llvm::Value *SizeValInChars;
6069     llvm::Value *SizeVal;
6070     std::tie(SizeValInChars, SizeVal) = RCG.getSizes(Cnt);
6071     // We use delayed creation/initialization for VLAs and array sections. It is
6072     // required because runtime does not provide the way to pass the sizes of
6073     // VLAs/array sections to initializer/combiner/finalizer functions. Instead
6074     // threadprivate global variables are used to store these values and use
6075     // them in the functions.
6076     bool DelayedCreation = !!SizeVal;
6077     SizeValInChars = CGF.Builder.CreateIntCast(SizeValInChars, CGM.SizeTy,
6078                                                /*isSigned=*/false);
6079     LValue SizeLVal = CGF.EmitLValueForField(ElemLVal, SizeFD);
6080     CGF.EmitStoreOfScalar(SizeValInChars, SizeLVal);
6081     // ElemLVal.reduce_init = init;
6082     LValue InitLVal = CGF.EmitLValueForField(ElemLVal, InitFD);
6083     llvm::Value *InitAddr =
6084         CGF.EmitCastToVoidPtr(emitReduceInitFunction(CGM, Loc, RCG, Cnt));
6085     CGF.EmitStoreOfScalar(InitAddr, InitLVal);
6086     // ElemLVal.reduce_fini = fini;
6087     LValue FiniLVal = CGF.EmitLValueForField(ElemLVal, FiniFD);
6088     llvm::Value *Fini = emitReduceFiniFunction(CGM, Loc, RCG, Cnt);
6089     llvm::Value *FiniAddr = Fini
6090                                 ? CGF.EmitCastToVoidPtr(Fini)
6091                                 : llvm::ConstantPointerNull::get(CGM.VoidPtrTy);
6092     CGF.EmitStoreOfScalar(FiniAddr, FiniLVal);
6093     // ElemLVal.reduce_comb = comb;
6094     LValue CombLVal = CGF.EmitLValueForField(ElemLVal, CombFD);
6095     llvm::Value *CombAddr = CGF.EmitCastToVoidPtr(emitReduceCombFunction(
6096         CGM, Loc, RCG, Cnt, Data.ReductionOps[Cnt], LHSExprs[Cnt],
6097         RHSExprs[Cnt], Data.ReductionCopies[Cnt]));
6098     CGF.EmitStoreOfScalar(CombAddr, CombLVal);
6099     // ElemLVal.flags = 0;
6100     LValue FlagsLVal = CGF.EmitLValueForField(ElemLVal, FlagsFD);
6101     if (DelayedCreation) {
6102       CGF.EmitStoreOfScalar(
6103           llvm::ConstantInt::get(CGM.Int32Ty, /*V=*/1, /*isSigned=*/true),
6104           FlagsLVal);
6105     } else
6106       CGF.EmitNullInitialization(FlagsLVal.getAddress(CGF),
6107                                  FlagsLVal.getType());
6108   }
6109   if (Data.IsReductionWithTaskMod) {
6110     // Build call void *__kmpc_taskred_modifier_init(ident_t *loc, int gtid, int
6111     // is_ws, int num, void *data);
6112     llvm::Value *IdentTLoc = emitUpdateLocation(CGF, Loc);
6113     llvm::Value *GTid = CGF.Builder.CreateIntCast(getThreadID(CGF, Loc),
6114                                                   CGM.IntTy, /*isSigned=*/true);
6115     llvm::Value *Args[] = {
6116         IdentTLoc, GTid,
6117         llvm::ConstantInt::get(CGM.IntTy, Data.IsWorksharingReduction ? 1 : 0,
6118                                /*isSigned=*/true),
6119         llvm::ConstantInt::get(CGM.IntTy, Size, /*isSigned=*/true),
6120         CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(
6121             TaskRedInput.getPointer(), CGM.VoidPtrTy)};
6122     return CGF.EmitRuntimeCall(
6123         OMPBuilder.getOrCreateRuntimeFunction(
6124             CGM.getModule(), OMPRTL___kmpc_taskred_modifier_init),
6125         Args);
6126   }
6127   // Build call void *__kmpc_taskred_init(int gtid, int num_data, void *data);
6128   llvm::Value *Args[] = {
6129       CGF.Builder.CreateIntCast(getThreadID(CGF, Loc), CGM.IntTy,
6130                                 /*isSigned=*/true),
6131       llvm::ConstantInt::get(CGM.IntTy, Size, /*isSigned=*/true),
6132       CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(TaskRedInput.getPointer(),
6133                                                       CGM.VoidPtrTy)};
6134   return CGF.EmitRuntimeCall(OMPBuilder.getOrCreateRuntimeFunction(
6135                                  CGM.getModule(), OMPRTL___kmpc_taskred_init),
6136                              Args);
6137 }
6138 
6139 void CGOpenMPRuntime::emitTaskReductionFini(CodeGenFunction &CGF,
6140                                             SourceLocation Loc,
6141                                             bool IsWorksharingReduction) {
6142   // Build call void *__kmpc_taskred_modifier_init(ident_t *loc, int gtid, int
6143   // is_ws, int num, void *data);
6144   llvm::Value *IdentTLoc = emitUpdateLocation(CGF, Loc);
6145   llvm::Value *GTid = CGF.Builder.CreateIntCast(getThreadID(CGF, Loc),
6146                                                 CGM.IntTy, /*isSigned=*/true);
6147   llvm::Value *Args[] = {IdentTLoc, GTid,
6148                          llvm::ConstantInt::get(CGM.IntTy,
6149                                                 IsWorksharingReduction ? 1 : 0,
6150                                                 /*isSigned=*/true)};
6151   (void)CGF.EmitRuntimeCall(
6152       OMPBuilder.getOrCreateRuntimeFunction(
6153           CGM.getModule(), OMPRTL___kmpc_task_reduction_modifier_fini),
6154       Args);
6155 }
6156 
6157 void CGOpenMPRuntime::emitTaskReductionFixups(CodeGenFunction &CGF,
6158                                               SourceLocation Loc,
6159                                               ReductionCodeGen &RCG,
6160                                               unsigned N) {
6161   auto Sizes = RCG.getSizes(N);
6162   // Emit threadprivate global variable if the type is non-constant
6163   // (Sizes.second = nullptr).
6164   if (Sizes.second) {
6165     llvm::Value *SizeVal = CGF.Builder.CreateIntCast(Sizes.second, CGM.SizeTy,
6166                                                      /*isSigned=*/false);
6167     Address SizeAddr = getAddrOfArtificialThreadPrivate(
6168         CGF, CGM.getContext().getSizeType(),
6169         generateUniqueName(CGM, "reduction_size", RCG.getRefExpr(N)));
6170     CGF.Builder.CreateStore(SizeVal, SizeAddr, /*IsVolatile=*/false);
6171   }
6172 }
6173 
6174 Address CGOpenMPRuntime::getTaskReductionItem(CodeGenFunction &CGF,
6175                                               SourceLocation Loc,
6176                                               llvm::Value *ReductionsPtr,
6177                                               LValue SharedLVal) {
6178   // Build call void *__kmpc_task_reduction_get_th_data(int gtid, void *tg, void
6179   // *d);
6180   llvm::Value *Args[] = {CGF.Builder.CreateIntCast(getThreadID(CGF, Loc),
6181                                                    CGM.IntTy,
6182                                                    /*isSigned=*/true),
6183                          ReductionsPtr,
6184                          CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(
6185                              SharedLVal.getPointer(CGF), CGM.VoidPtrTy)};
6186   return Address(
6187       CGF.EmitRuntimeCall(
6188           OMPBuilder.getOrCreateRuntimeFunction(
6189               CGM.getModule(), OMPRTL___kmpc_task_reduction_get_th_data),
6190           Args),
6191       SharedLVal.getAlignment());
6192 }
6193 
6194 void CGOpenMPRuntime::emitTaskwaitCall(CodeGenFunction &CGF,
6195                                        SourceLocation Loc) {
6196   if (!CGF.HaveInsertPoint())
6197     return;
6198 
6199   if (CGF.CGM.getLangOpts().OpenMPIRBuilder) {
6200     OMPBuilder.CreateTaskwait(CGF.Builder);
6201   } else {
6202     // Build call kmp_int32 __kmpc_omp_taskwait(ident_t *loc, kmp_int32
6203     // global_tid);
6204     llvm::Value *Args[] = {emitUpdateLocation(CGF, Loc), getThreadID(CGF, Loc)};
6205     // Ignore return result until untied tasks are supported.
6206     CGF.EmitRuntimeCall(OMPBuilder.getOrCreateRuntimeFunction(
6207                             CGM.getModule(), OMPRTL___kmpc_omp_taskwait),
6208                         Args);
6209   }
6210 
6211   if (auto *Region = dyn_cast_or_null<CGOpenMPRegionInfo>(CGF.CapturedStmtInfo))
6212     Region->emitUntiedSwitch(CGF);
6213 }
6214 
6215 void CGOpenMPRuntime::emitInlinedDirective(CodeGenFunction &CGF,
6216                                            OpenMPDirectiveKind InnerKind,
6217                                            const RegionCodeGenTy &CodeGen,
6218                                            bool HasCancel) {
6219   if (!CGF.HaveInsertPoint())
6220     return;
6221   InlinedOpenMPRegionRAII Region(CGF, CodeGen, InnerKind, HasCancel);
6222   CGF.CapturedStmtInfo->EmitBody(CGF, /*S=*/nullptr);
6223 }
6224 
6225 namespace {
6226 enum RTCancelKind {
6227   CancelNoreq = 0,
6228   CancelParallel = 1,
6229   CancelLoop = 2,
6230   CancelSections = 3,
6231   CancelTaskgroup = 4
6232 };
6233 } // anonymous namespace
6234 
6235 static RTCancelKind getCancellationKind(OpenMPDirectiveKind CancelRegion) {
6236   RTCancelKind CancelKind = CancelNoreq;
6237   if (CancelRegion == OMPD_parallel)
6238     CancelKind = CancelParallel;
6239   else if (CancelRegion == OMPD_for)
6240     CancelKind = CancelLoop;
6241   else if (CancelRegion == OMPD_sections)
6242     CancelKind = CancelSections;
6243   else {
6244     assert(CancelRegion == OMPD_taskgroup);
6245     CancelKind = CancelTaskgroup;
6246   }
6247   return CancelKind;
6248 }
6249 
6250 void CGOpenMPRuntime::emitCancellationPointCall(
6251     CodeGenFunction &CGF, SourceLocation Loc,
6252     OpenMPDirectiveKind CancelRegion) {
6253   if (!CGF.HaveInsertPoint())
6254     return;
6255   // Build call kmp_int32 __kmpc_cancellationpoint(ident_t *loc, kmp_int32
6256   // global_tid, kmp_int32 cncl_kind);
6257   if (auto *OMPRegionInfo =
6258           dyn_cast_or_null<CGOpenMPRegionInfo>(CGF.CapturedStmtInfo)) {
6259     // For 'cancellation point taskgroup', the task region info may not have a
6260     // cancel. This may instead happen in another adjacent task.
6261     if (CancelRegion == OMPD_taskgroup || OMPRegionInfo->hasCancel()) {
6262       llvm::Value *Args[] = {
6263           emitUpdateLocation(CGF, Loc), getThreadID(CGF, Loc),
6264           CGF.Builder.getInt32(getCancellationKind(CancelRegion))};
6265       // Ignore return result until untied tasks are supported.
6266       llvm::Value *Result = CGF.EmitRuntimeCall(
6267           OMPBuilder.getOrCreateRuntimeFunction(
6268               CGM.getModule(), OMPRTL___kmpc_cancellationpoint),
6269           Args);
6270       // if (__kmpc_cancellationpoint()) {
6271       //   exit from construct;
6272       // }
6273       llvm::BasicBlock *ExitBB = CGF.createBasicBlock(".cancel.exit");
6274       llvm::BasicBlock *ContBB = CGF.createBasicBlock(".cancel.continue");
6275       llvm::Value *Cmp = CGF.Builder.CreateIsNotNull(Result);
6276       CGF.Builder.CreateCondBr(Cmp, ExitBB, ContBB);
6277       CGF.EmitBlock(ExitBB);
6278       // exit from construct;
6279       CodeGenFunction::JumpDest CancelDest =
6280           CGF.getOMPCancelDestination(OMPRegionInfo->getDirectiveKind());
6281       CGF.EmitBranchThroughCleanup(CancelDest);
6282       CGF.EmitBlock(ContBB, /*IsFinished=*/true);
6283     }
6284   }
6285 }
6286 
6287 void CGOpenMPRuntime::emitCancelCall(CodeGenFunction &CGF, SourceLocation Loc,
6288                                      const Expr *IfCond,
6289                                      OpenMPDirectiveKind CancelRegion) {
6290   if (!CGF.HaveInsertPoint())
6291     return;
6292   // Build call kmp_int32 __kmpc_cancel(ident_t *loc, kmp_int32 global_tid,
6293   // kmp_int32 cncl_kind);
6294   auto &M = CGM.getModule();
6295   if (auto *OMPRegionInfo =
6296           dyn_cast_or_null<CGOpenMPRegionInfo>(CGF.CapturedStmtInfo)) {
6297     auto &&ThenGen = [this, &M, Loc, CancelRegion,
6298                       OMPRegionInfo](CodeGenFunction &CGF, PrePostActionTy &) {
6299       CGOpenMPRuntime &RT = CGF.CGM.getOpenMPRuntime();
6300       llvm::Value *Args[] = {
6301           RT.emitUpdateLocation(CGF, Loc), RT.getThreadID(CGF, Loc),
6302           CGF.Builder.getInt32(getCancellationKind(CancelRegion))};
6303       // Ignore return result until untied tasks are supported.
6304       llvm::Value *Result = CGF.EmitRuntimeCall(
6305           OMPBuilder.getOrCreateRuntimeFunction(M, OMPRTL___kmpc_cancel), Args);
6306       // if (__kmpc_cancel()) {
6307       //   exit from construct;
6308       // }
6309       llvm::BasicBlock *ExitBB = CGF.createBasicBlock(".cancel.exit");
6310       llvm::BasicBlock *ContBB = CGF.createBasicBlock(".cancel.continue");
6311       llvm::Value *Cmp = CGF.Builder.CreateIsNotNull(Result);
6312       CGF.Builder.CreateCondBr(Cmp, ExitBB, ContBB);
6313       CGF.EmitBlock(ExitBB);
6314       // exit from construct;
6315       CodeGenFunction::JumpDest CancelDest =
6316           CGF.getOMPCancelDestination(OMPRegionInfo->getDirectiveKind());
6317       CGF.EmitBranchThroughCleanup(CancelDest);
6318       CGF.EmitBlock(ContBB, /*IsFinished=*/true);
6319     };
6320     if (IfCond) {
6321       emitIfClause(CGF, IfCond, ThenGen,
6322                    [](CodeGenFunction &, PrePostActionTy &) {});
6323     } else {
6324       RegionCodeGenTy ThenRCG(ThenGen);
6325       ThenRCG(CGF);
6326     }
6327   }
6328 }
6329 
6330 namespace {
6331 /// Cleanup action for uses_allocators support.
6332 class OMPUsesAllocatorsActionTy final : public PrePostActionTy {
6333   ArrayRef<std::pair<const Expr *, const Expr *>> Allocators;
6334 
6335 public:
6336   OMPUsesAllocatorsActionTy(
6337       ArrayRef<std::pair<const Expr *, const Expr *>> Allocators)
6338       : Allocators(Allocators) {}
6339   void Enter(CodeGenFunction &CGF) override {
6340     if (!CGF.HaveInsertPoint())
6341       return;
6342     for (const auto &AllocatorData : Allocators) {
6343       CGF.CGM.getOpenMPRuntime().emitUsesAllocatorsInit(
6344           CGF, AllocatorData.first, AllocatorData.second);
6345     }
6346   }
6347   void Exit(CodeGenFunction &CGF) override {
6348     if (!CGF.HaveInsertPoint())
6349       return;
6350     for (const auto &AllocatorData : Allocators) {
6351       CGF.CGM.getOpenMPRuntime().emitUsesAllocatorsFini(CGF,
6352                                                         AllocatorData.first);
6353     }
6354   }
6355 };
6356 } // namespace
6357 
6358 void CGOpenMPRuntime::emitTargetOutlinedFunction(
6359     const OMPExecutableDirective &D, StringRef ParentName,
6360     llvm::Function *&OutlinedFn, llvm::Constant *&OutlinedFnID,
6361     bool IsOffloadEntry, const RegionCodeGenTy &CodeGen) {
6362   assert(!ParentName.empty() && "Invalid target region parent name!");
6363   HasEmittedTargetRegion = true;
6364   SmallVector<std::pair<const Expr *, const Expr *>, 4> Allocators;
6365   for (const auto *C : D.getClausesOfKind<OMPUsesAllocatorsClause>()) {
6366     for (unsigned I = 0, E = C->getNumberOfAllocators(); I < E; ++I) {
6367       const OMPUsesAllocatorsClause::Data D = C->getAllocatorData(I);
6368       if (!D.AllocatorTraits)
6369         continue;
6370       Allocators.emplace_back(D.Allocator, D.AllocatorTraits);
6371     }
6372   }
6373   OMPUsesAllocatorsActionTy UsesAllocatorAction(Allocators);
6374   CodeGen.setAction(UsesAllocatorAction);
6375   emitTargetOutlinedFunctionHelper(D, ParentName, OutlinedFn, OutlinedFnID,
6376                                    IsOffloadEntry, CodeGen);
6377 }
6378 
6379 void CGOpenMPRuntime::emitUsesAllocatorsInit(CodeGenFunction &CGF,
6380                                              const Expr *Allocator,
6381                                              const Expr *AllocatorTraits) {
6382   llvm::Value *ThreadId = getThreadID(CGF, Allocator->getExprLoc());
6383   ThreadId = CGF.Builder.CreateIntCast(ThreadId, CGF.IntTy, /*isSigned=*/true);
6384   // Use default memspace handle.
6385   llvm::Value *MemSpaceHandle = llvm::ConstantPointerNull::get(CGF.VoidPtrTy);
6386   llvm::Value *NumTraits = llvm::ConstantInt::get(
6387       CGF.IntTy, cast<ConstantArrayType>(
6388                      AllocatorTraits->getType()->getAsArrayTypeUnsafe())
6389                      ->getSize()
6390                      .getLimitedValue());
6391   LValue AllocatorTraitsLVal = CGF.EmitLValue(AllocatorTraits);
6392   Address Addr = CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(
6393       AllocatorTraitsLVal.getAddress(CGF), CGF.VoidPtrPtrTy);
6394   AllocatorTraitsLVal = CGF.MakeAddrLValue(Addr, CGF.getContext().VoidPtrTy,
6395                                            AllocatorTraitsLVal.getBaseInfo(),
6396                                            AllocatorTraitsLVal.getTBAAInfo());
6397   llvm::Value *Traits =
6398       CGF.EmitLoadOfScalar(AllocatorTraitsLVal, AllocatorTraits->getExprLoc());
6399 
6400   llvm::Value *AllocatorVal =
6401       CGF.EmitRuntimeCall(OMPBuilder.getOrCreateRuntimeFunction(
6402                               CGM.getModule(), OMPRTL___kmpc_init_allocator),
6403                           {ThreadId, MemSpaceHandle, NumTraits, Traits});
6404   // Store to allocator.
6405   CGF.EmitVarDecl(*cast<VarDecl>(
6406       cast<DeclRefExpr>(Allocator->IgnoreParenImpCasts())->getDecl()));
6407   LValue AllocatorLVal = CGF.EmitLValue(Allocator->IgnoreParenImpCasts());
6408   AllocatorVal =
6409       CGF.EmitScalarConversion(AllocatorVal, CGF.getContext().VoidPtrTy,
6410                                Allocator->getType(), Allocator->getExprLoc());
6411   CGF.EmitStoreOfScalar(AllocatorVal, AllocatorLVal);
6412 }
6413 
6414 void CGOpenMPRuntime::emitUsesAllocatorsFini(CodeGenFunction &CGF,
6415                                              const Expr *Allocator) {
6416   llvm::Value *ThreadId = getThreadID(CGF, Allocator->getExprLoc());
6417   ThreadId = CGF.Builder.CreateIntCast(ThreadId, CGF.IntTy, /*isSigned=*/true);
6418   LValue AllocatorLVal = CGF.EmitLValue(Allocator->IgnoreParenImpCasts());
6419   llvm::Value *AllocatorVal =
6420       CGF.EmitLoadOfScalar(AllocatorLVal, Allocator->getExprLoc());
6421   AllocatorVal = CGF.EmitScalarConversion(AllocatorVal, Allocator->getType(),
6422                                           CGF.getContext().VoidPtrTy,
6423                                           Allocator->getExprLoc());
6424   (void)CGF.EmitRuntimeCall(
6425       OMPBuilder.getOrCreateRuntimeFunction(CGM.getModule(),
6426                                             OMPRTL___kmpc_destroy_allocator),
6427       {ThreadId, AllocatorVal});
6428 }
6429 
6430 void CGOpenMPRuntime::emitTargetOutlinedFunctionHelper(
6431     const OMPExecutableDirective &D, StringRef ParentName,
6432     llvm::Function *&OutlinedFn, llvm::Constant *&OutlinedFnID,
6433     bool IsOffloadEntry, const RegionCodeGenTy &CodeGen) {
6434   // Create a unique name for the entry function using the source location
6435   // information of the current target region. The name will be something like:
6436   //
6437   // __omp_offloading_DD_FFFF_PP_lBB
6438   //
6439   // where DD_FFFF is an ID unique to the file (device and file IDs), PP is the
6440   // mangled name of the function that encloses the target region and BB is the
6441   // line number of the target region.
6442 
6443   unsigned DeviceID;
6444   unsigned FileID;
6445   unsigned Line;
6446   getTargetEntryUniqueInfo(CGM.getContext(), D.getBeginLoc(), DeviceID, FileID,
6447                            Line);
6448   SmallString<64> EntryFnName;
6449   {
6450     llvm::raw_svector_ostream OS(EntryFnName);
6451     OS << "__omp_offloading" << llvm::format("_%x", DeviceID)
6452        << llvm::format("_%x_", FileID) << ParentName << "_l" << Line;
6453   }
6454 
6455   const CapturedStmt &CS = *D.getCapturedStmt(OMPD_target);
6456 
6457   CodeGenFunction CGF(CGM, true);
6458   CGOpenMPTargetRegionInfo CGInfo(CS, CodeGen, EntryFnName);
6459   CodeGenFunction::CGCapturedStmtRAII CapInfoRAII(CGF, &CGInfo);
6460 
6461   OutlinedFn = CGF.GenerateOpenMPCapturedStmtFunction(CS, D.getBeginLoc());
6462 
6463   // If this target outline function is not an offload entry, we don't need to
6464   // register it.
6465   if (!IsOffloadEntry)
6466     return;
6467 
6468   // The target region ID is used by the runtime library to identify the current
6469   // target region, so it only has to be unique and not necessarily point to
6470   // anything. It could be the pointer to the outlined function that implements
6471   // the target region, but we aren't using that so that the compiler doesn't
6472   // need to keep that, and could therefore inline the host function if proven
6473   // worthwhile during optimization. In the other hand, if emitting code for the
6474   // device, the ID has to be the function address so that it can retrieved from
6475   // the offloading entry and launched by the runtime library. We also mark the
6476   // outlined function to have external linkage in case we are emitting code for
6477   // the device, because these functions will be entry points to the device.
6478 
6479   if (CGM.getLangOpts().OpenMPIsDevice) {
6480     OutlinedFnID = llvm::ConstantExpr::getBitCast(OutlinedFn, CGM.Int8PtrTy);
6481     OutlinedFn->setLinkage(llvm::GlobalValue::WeakAnyLinkage);
6482     OutlinedFn->setDSOLocal(false);
6483   } else {
6484     std::string Name = getName({EntryFnName, "region_id"});
6485     OutlinedFnID = new llvm::GlobalVariable(
6486         CGM.getModule(), CGM.Int8Ty, /*isConstant=*/true,
6487         llvm::GlobalValue::WeakAnyLinkage,
6488         llvm::Constant::getNullValue(CGM.Int8Ty), Name);
6489   }
6490 
6491   // Register the information for the entry associated with this target region.
6492   OffloadEntriesInfoManager.registerTargetRegionEntryInfo(
6493       DeviceID, FileID, ParentName, Line, OutlinedFn, OutlinedFnID,
6494       OffloadEntriesInfoManagerTy::OMPTargetRegionEntryTargetRegion);
6495 }
6496 
6497 /// Checks if the expression is constant or does not have non-trivial function
6498 /// calls.
6499 static bool isTrivial(ASTContext &Ctx, const Expr * E) {
6500   // We can skip constant expressions.
6501   // We can skip expressions with trivial calls or simple expressions.
6502   return (E->isEvaluatable(Ctx, Expr::SE_AllowUndefinedBehavior) ||
6503           !E->hasNonTrivialCall(Ctx)) &&
6504          !E->HasSideEffects(Ctx, /*IncludePossibleEffects=*/true);
6505 }
6506 
6507 const Stmt *CGOpenMPRuntime::getSingleCompoundChild(ASTContext &Ctx,
6508                                                     const Stmt *Body) {
6509   const Stmt *Child = Body->IgnoreContainers();
6510   while (const auto *C = dyn_cast_or_null<CompoundStmt>(Child)) {
6511     Child = nullptr;
6512     for (const Stmt *S : C->body()) {
6513       if (const auto *E = dyn_cast<Expr>(S)) {
6514         if (isTrivial(Ctx, E))
6515           continue;
6516       }
6517       // Some of the statements can be ignored.
6518       if (isa<AsmStmt>(S) || isa<NullStmt>(S) || isa<OMPFlushDirective>(S) ||
6519           isa<OMPBarrierDirective>(S) || isa<OMPTaskyieldDirective>(S))
6520         continue;
6521       // Analyze declarations.
6522       if (const auto *DS = dyn_cast<DeclStmt>(S)) {
6523         if (llvm::all_of(DS->decls(), [&Ctx](const Decl *D) {
6524               if (isa<EmptyDecl>(D) || isa<DeclContext>(D) ||
6525                   isa<TypeDecl>(D) || isa<PragmaCommentDecl>(D) ||
6526                   isa<PragmaDetectMismatchDecl>(D) || isa<UsingDecl>(D) ||
6527                   isa<UsingDirectiveDecl>(D) ||
6528                   isa<OMPDeclareReductionDecl>(D) ||
6529                   isa<OMPThreadPrivateDecl>(D) || isa<OMPAllocateDecl>(D))
6530                 return true;
6531               const auto *VD = dyn_cast<VarDecl>(D);
6532               if (!VD)
6533                 return false;
6534               return VD->isConstexpr() ||
6535                      ((VD->getType().isTrivialType(Ctx) ||
6536                        VD->getType()->isReferenceType()) &&
6537                       (!VD->hasInit() || isTrivial(Ctx, VD->getInit())));
6538             }))
6539           continue;
6540       }
6541       // Found multiple children - cannot get the one child only.
6542       if (Child)
6543         return nullptr;
6544       Child = S;
6545     }
6546     if (Child)
6547       Child = Child->IgnoreContainers();
6548   }
6549   return Child;
6550 }
6551 
6552 /// Emit the number of teams for a target directive.  Inspect the num_teams
6553 /// clause associated with a teams construct combined or closely nested
6554 /// with the target directive.
6555 ///
6556 /// Emit a team of size one for directives such as 'target parallel' that
6557 /// have no associated teams construct.
6558 ///
6559 /// Otherwise, return nullptr.
6560 static llvm::Value *
6561 emitNumTeamsForTargetDirective(CodeGenFunction &CGF,
6562                                const OMPExecutableDirective &D) {
6563   assert(!CGF.getLangOpts().OpenMPIsDevice &&
6564          "Clauses associated with the teams directive expected to be emitted "
6565          "only for the host!");
6566   OpenMPDirectiveKind DirectiveKind = D.getDirectiveKind();
6567   assert(isOpenMPTargetExecutionDirective(DirectiveKind) &&
6568          "Expected target-based executable directive.");
6569   CGBuilderTy &Bld = CGF.Builder;
6570   switch (DirectiveKind) {
6571   case OMPD_target: {
6572     const auto *CS = D.getInnermostCapturedStmt();
6573     const auto *Body =
6574         CS->getCapturedStmt()->IgnoreContainers(/*IgnoreCaptured=*/true);
6575     const Stmt *ChildStmt =
6576         CGOpenMPRuntime::getSingleCompoundChild(CGF.getContext(), Body);
6577     if (const auto *NestedDir =
6578             dyn_cast_or_null<OMPExecutableDirective>(ChildStmt)) {
6579       if (isOpenMPTeamsDirective(NestedDir->getDirectiveKind())) {
6580         if (NestedDir->hasClausesOfKind<OMPNumTeamsClause>()) {
6581           CGOpenMPInnerExprInfo CGInfo(CGF, *CS);
6582           CodeGenFunction::CGCapturedStmtRAII CapInfoRAII(CGF, &CGInfo);
6583           const Expr *NumTeams =
6584               NestedDir->getSingleClause<OMPNumTeamsClause>()->getNumTeams();
6585           llvm::Value *NumTeamsVal =
6586               CGF.EmitScalarExpr(NumTeams,
6587                                  /*IgnoreResultAssign*/ true);
6588           return Bld.CreateIntCast(NumTeamsVal, CGF.Int32Ty,
6589                                    /*isSigned=*/true);
6590         }
6591         return Bld.getInt32(0);
6592       }
6593       if (isOpenMPParallelDirective(NestedDir->getDirectiveKind()) ||
6594           isOpenMPSimdDirective(NestedDir->getDirectiveKind()))
6595         return Bld.getInt32(1);
6596       return Bld.getInt32(0);
6597     }
6598     return nullptr;
6599   }
6600   case OMPD_target_teams:
6601   case OMPD_target_teams_distribute:
6602   case OMPD_target_teams_distribute_simd:
6603   case OMPD_target_teams_distribute_parallel_for:
6604   case OMPD_target_teams_distribute_parallel_for_simd: {
6605     if (D.hasClausesOfKind<OMPNumTeamsClause>()) {
6606       CodeGenFunction::RunCleanupsScope NumTeamsScope(CGF);
6607       const Expr *NumTeams =
6608           D.getSingleClause<OMPNumTeamsClause>()->getNumTeams();
6609       llvm::Value *NumTeamsVal =
6610           CGF.EmitScalarExpr(NumTeams,
6611                              /*IgnoreResultAssign*/ true);
6612       return Bld.CreateIntCast(NumTeamsVal, CGF.Int32Ty,
6613                                /*isSigned=*/true);
6614     }
6615     return Bld.getInt32(0);
6616   }
6617   case OMPD_target_parallel:
6618   case OMPD_target_parallel_for:
6619   case OMPD_target_parallel_for_simd:
6620   case OMPD_target_simd:
6621     return Bld.getInt32(1);
6622   case OMPD_parallel:
6623   case OMPD_for:
6624   case OMPD_parallel_for:
6625   case OMPD_parallel_master:
6626   case OMPD_parallel_sections:
6627   case OMPD_for_simd:
6628   case OMPD_parallel_for_simd:
6629   case OMPD_cancel:
6630   case OMPD_cancellation_point:
6631   case OMPD_ordered:
6632   case OMPD_threadprivate:
6633   case OMPD_allocate:
6634   case OMPD_task:
6635   case OMPD_simd:
6636   case OMPD_sections:
6637   case OMPD_section:
6638   case OMPD_single:
6639   case OMPD_master:
6640   case OMPD_critical:
6641   case OMPD_taskyield:
6642   case OMPD_barrier:
6643   case OMPD_taskwait:
6644   case OMPD_taskgroup:
6645   case OMPD_atomic:
6646   case OMPD_flush:
6647   case OMPD_depobj:
6648   case OMPD_scan:
6649   case OMPD_teams:
6650   case OMPD_target_data:
6651   case OMPD_target_exit_data:
6652   case OMPD_target_enter_data:
6653   case OMPD_distribute:
6654   case OMPD_distribute_simd:
6655   case OMPD_distribute_parallel_for:
6656   case OMPD_distribute_parallel_for_simd:
6657   case OMPD_teams_distribute:
6658   case OMPD_teams_distribute_simd:
6659   case OMPD_teams_distribute_parallel_for:
6660   case OMPD_teams_distribute_parallel_for_simd:
6661   case OMPD_target_update:
6662   case OMPD_declare_simd:
6663   case OMPD_declare_variant:
6664   case OMPD_begin_declare_variant:
6665   case OMPD_end_declare_variant:
6666   case OMPD_declare_target:
6667   case OMPD_end_declare_target:
6668   case OMPD_declare_reduction:
6669   case OMPD_declare_mapper:
6670   case OMPD_taskloop:
6671   case OMPD_taskloop_simd:
6672   case OMPD_master_taskloop:
6673   case OMPD_master_taskloop_simd:
6674   case OMPD_parallel_master_taskloop:
6675   case OMPD_parallel_master_taskloop_simd:
6676   case OMPD_requires:
6677   case OMPD_unknown:
6678     break;
6679   default:
6680     break;
6681   }
6682   llvm_unreachable("Unexpected directive kind.");
6683 }
6684 
6685 static llvm::Value *getNumThreads(CodeGenFunction &CGF, const CapturedStmt *CS,
6686                                   llvm::Value *DefaultThreadLimitVal) {
6687   const Stmt *Child = CGOpenMPRuntime::getSingleCompoundChild(
6688       CGF.getContext(), CS->getCapturedStmt());
6689   if (const auto *Dir = dyn_cast_or_null<OMPExecutableDirective>(Child)) {
6690     if (isOpenMPParallelDirective(Dir->getDirectiveKind())) {
6691       llvm::Value *NumThreads = nullptr;
6692       llvm::Value *CondVal = nullptr;
6693       // Handle if clause. If if clause present, the number of threads is
6694       // calculated as <cond> ? (<numthreads> ? <numthreads> : 0 ) : 1.
6695       if (Dir->hasClausesOfKind<OMPIfClause>()) {
6696         CGOpenMPInnerExprInfo CGInfo(CGF, *CS);
6697         CodeGenFunction::CGCapturedStmtRAII CapInfoRAII(CGF, &CGInfo);
6698         const OMPIfClause *IfClause = nullptr;
6699         for (const auto *C : Dir->getClausesOfKind<OMPIfClause>()) {
6700           if (C->getNameModifier() == OMPD_unknown ||
6701               C->getNameModifier() == OMPD_parallel) {
6702             IfClause = C;
6703             break;
6704           }
6705         }
6706         if (IfClause) {
6707           const Expr *Cond = IfClause->getCondition();
6708           bool Result;
6709           if (Cond->EvaluateAsBooleanCondition(Result, CGF.getContext())) {
6710             if (!Result)
6711               return CGF.Builder.getInt32(1);
6712           } else {
6713             CodeGenFunction::LexicalScope Scope(CGF, Cond->getSourceRange());
6714             if (const auto *PreInit =
6715                     cast_or_null<DeclStmt>(IfClause->getPreInitStmt())) {
6716               for (const auto *I : PreInit->decls()) {
6717                 if (!I->hasAttr<OMPCaptureNoInitAttr>()) {
6718                   CGF.EmitVarDecl(cast<VarDecl>(*I));
6719                 } else {
6720                   CodeGenFunction::AutoVarEmission Emission =
6721                       CGF.EmitAutoVarAlloca(cast<VarDecl>(*I));
6722                   CGF.EmitAutoVarCleanups(Emission);
6723                 }
6724               }
6725             }
6726             CondVal = CGF.EvaluateExprAsBool(Cond);
6727           }
6728         }
6729       }
6730       // Check the value of num_threads clause iff if clause was not specified
6731       // or is not evaluated to false.
6732       if (Dir->hasClausesOfKind<OMPNumThreadsClause>()) {
6733         CGOpenMPInnerExprInfo CGInfo(CGF, *CS);
6734         CodeGenFunction::CGCapturedStmtRAII CapInfoRAII(CGF, &CGInfo);
6735         const auto *NumThreadsClause =
6736             Dir->getSingleClause<OMPNumThreadsClause>();
6737         CodeGenFunction::LexicalScope Scope(
6738             CGF, NumThreadsClause->getNumThreads()->getSourceRange());
6739         if (const auto *PreInit =
6740                 cast_or_null<DeclStmt>(NumThreadsClause->getPreInitStmt())) {
6741           for (const auto *I : PreInit->decls()) {
6742             if (!I->hasAttr<OMPCaptureNoInitAttr>()) {
6743               CGF.EmitVarDecl(cast<VarDecl>(*I));
6744             } else {
6745               CodeGenFunction::AutoVarEmission Emission =
6746                   CGF.EmitAutoVarAlloca(cast<VarDecl>(*I));
6747               CGF.EmitAutoVarCleanups(Emission);
6748             }
6749           }
6750         }
6751         NumThreads = CGF.EmitScalarExpr(NumThreadsClause->getNumThreads());
6752         NumThreads = CGF.Builder.CreateIntCast(NumThreads, CGF.Int32Ty,
6753                                                /*isSigned=*/false);
6754         if (DefaultThreadLimitVal)
6755           NumThreads = CGF.Builder.CreateSelect(
6756               CGF.Builder.CreateICmpULT(DefaultThreadLimitVal, NumThreads),
6757               DefaultThreadLimitVal, NumThreads);
6758       } else {
6759         NumThreads = DefaultThreadLimitVal ? DefaultThreadLimitVal
6760                                            : CGF.Builder.getInt32(0);
6761       }
6762       // Process condition of the if clause.
6763       if (CondVal) {
6764         NumThreads = CGF.Builder.CreateSelect(CondVal, NumThreads,
6765                                               CGF.Builder.getInt32(1));
6766       }
6767       return NumThreads;
6768     }
6769     if (isOpenMPSimdDirective(Dir->getDirectiveKind()))
6770       return CGF.Builder.getInt32(1);
6771     return DefaultThreadLimitVal;
6772   }
6773   return DefaultThreadLimitVal ? DefaultThreadLimitVal
6774                                : CGF.Builder.getInt32(0);
6775 }
6776 
6777 /// Emit the number of threads for a target directive.  Inspect the
6778 /// thread_limit clause associated with a teams construct combined or closely
6779 /// nested with the target directive.
6780 ///
6781 /// Emit the num_threads clause for directives such as 'target parallel' that
6782 /// have no associated teams construct.
6783 ///
6784 /// Otherwise, return nullptr.
6785 static llvm::Value *
6786 emitNumThreadsForTargetDirective(CodeGenFunction &CGF,
6787                                  const OMPExecutableDirective &D) {
6788   assert(!CGF.getLangOpts().OpenMPIsDevice &&
6789          "Clauses associated with the teams directive expected to be emitted "
6790          "only for the host!");
6791   OpenMPDirectiveKind DirectiveKind = D.getDirectiveKind();
6792   assert(isOpenMPTargetExecutionDirective(DirectiveKind) &&
6793          "Expected target-based executable directive.");
6794   CGBuilderTy &Bld = CGF.Builder;
6795   llvm::Value *ThreadLimitVal = nullptr;
6796   llvm::Value *NumThreadsVal = nullptr;
6797   switch (DirectiveKind) {
6798   case OMPD_target: {
6799     const CapturedStmt *CS = D.getInnermostCapturedStmt();
6800     if (llvm::Value *NumThreads = getNumThreads(CGF, CS, ThreadLimitVal))
6801       return NumThreads;
6802     const Stmt *Child = CGOpenMPRuntime::getSingleCompoundChild(
6803         CGF.getContext(), CS->getCapturedStmt());
6804     if (const auto *Dir = dyn_cast_or_null<OMPExecutableDirective>(Child)) {
6805       if (Dir->hasClausesOfKind<OMPThreadLimitClause>()) {
6806         CGOpenMPInnerExprInfo CGInfo(CGF, *CS);
6807         CodeGenFunction::CGCapturedStmtRAII CapInfoRAII(CGF, &CGInfo);
6808         const auto *ThreadLimitClause =
6809             Dir->getSingleClause<OMPThreadLimitClause>();
6810         CodeGenFunction::LexicalScope Scope(
6811             CGF, ThreadLimitClause->getThreadLimit()->getSourceRange());
6812         if (const auto *PreInit =
6813                 cast_or_null<DeclStmt>(ThreadLimitClause->getPreInitStmt())) {
6814           for (const auto *I : PreInit->decls()) {
6815             if (!I->hasAttr<OMPCaptureNoInitAttr>()) {
6816               CGF.EmitVarDecl(cast<VarDecl>(*I));
6817             } else {
6818               CodeGenFunction::AutoVarEmission Emission =
6819                   CGF.EmitAutoVarAlloca(cast<VarDecl>(*I));
6820               CGF.EmitAutoVarCleanups(Emission);
6821             }
6822           }
6823         }
6824         llvm::Value *ThreadLimit = CGF.EmitScalarExpr(
6825             ThreadLimitClause->getThreadLimit(), /*IgnoreResultAssign=*/true);
6826         ThreadLimitVal =
6827             Bld.CreateIntCast(ThreadLimit, CGF.Int32Ty, /*isSigned=*/false);
6828       }
6829       if (isOpenMPTeamsDirective(Dir->getDirectiveKind()) &&
6830           !isOpenMPDistributeDirective(Dir->getDirectiveKind())) {
6831         CS = Dir->getInnermostCapturedStmt();
6832         const Stmt *Child = CGOpenMPRuntime::getSingleCompoundChild(
6833             CGF.getContext(), CS->getCapturedStmt());
6834         Dir = dyn_cast_or_null<OMPExecutableDirective>(Child);
6835       }
6836       if (Dir && isOpenMPDistributeDirective(Dir->getDirectiveKind()) &&
6837           !isOpenMPSimdDirective(Dir->getDirectiveKind())) {
6838         CS = Dir->getInnermostCapturedStmt();
6839         if (llvm::Value *NumThreads = getNumThreads(CGF, CS, ThreadLimitVal))
6840           return NumThreads;
6841       }
6842       if (Dir && isOpenMPSimdDirective(Dir->getDirectiveKind()))
6843         return Bld.getInt32(1);
6844     }
6845     return ThreadLimitVal ? ThreadLimitVal : Bld.getInt32(0);
6846   }
6847   case OMPD_target_teams: {
6848     if (D.hasClausesOfKind<OMPThreadLimitClause>()) {
6849       CodeGenFunction::RunCleanupsScope ThreadLimitScope(CGF);
6850       const auto *ThreadLimitClause = D.getSingleClause<OMPThreadLimitClause>();
6851       llvm::Value *ThreadLimit = CGF.EmitScalarExpr(
6852           ThreadLimitClause->getThreadLimit(), /*IgnoreResultAssign=*/true);
6853       ThreadLimitVal =
6854           Bld.CreateIntCast(ThreadLimit, CGF.Int32Ty, /*isSigned=*/false);
6855     }
6856     const CapturedStmt *CS = D.getInnermostCapturedStmt();
6857     if (llvm::Value *NumThreads = getNumThreads(CGF, CS, ThreadLimitVal))
6858       return NumThreads;
6859     const Stmt *Child = CGOpenMPRuntime::getSingleCompoundChild(
6860         CGF.getContext(), CS->getCapturedStmt());
6861     if (const auto *Dir = dyn_cast_or_null<OMPExecutableDirective>(Child)) {
6862       if (Dir->getDirectiveKind() == OMPD_distribute) {
6863         CS = Dir->getInnermostCapturedStmt();
6864         if (llvm::Value *NumThreads = getNumThreads(CGF, CS, ThreadLimitVal))
6865           return NumThreads;
6866       }
6867     }
6868     return ThreadLimitVal ? ThreadLimitVal : Bld.getInt32(0);
6869   }
6870   case OMPD_target_teams_distribute:
6871     if (D.hasClausesOfKind<OMPThreadLimitClause>()) {
6872       CodeGenFunction::RunCleanupsScope ThreadLimitScope(CGF);
6873       const auto *ThreadLimitClause = D.getSingleClause<OMPThreadLimitClause>();
6874       llvm::Value *ThreadLimit = CGF.EmitScalarExpr(
6875           ThreadLimitClause->getThreadLimit(), /*IgnoreResultAssign=*/true);
6876       ThreadLimitVal =
6877           Bld.CreateIntCast(ThreadLimit, CGF.Int32Ty, /*isSigned=*/false);
6878     }
6879     return getNumThreads(CGF, D.getInnermostCapturedStmt(), ThreadLimitVal);
6880   case OMPD_target_parallel:
6881   case OMPD_target_parallel_for:
6882   case OMPD_target_parallel_for_simd:
6883   case OMPD_target_teams_distribute_parallel_for:
6884   case OMPD_target_teams_distribute_parallel_for_simd: {
6885     llvm::Value *CondVal = nullptr;
6886     // Handle if clause. If if clause present, the number of threads is
6887     // calculated as <cond> ? (<numthreads> ? <numthreads> : 0 ) : 1.
6888     if (D.hasClausesOfKind<OMPIfClause>()) {
6889       const OMPIfClause *IfClause = nullptr;
6890       for (const auto *C : D.getClausesOfKind<OMPIfClause>()) {
6891         if (C->getNameModifier() == OMPD_unknown ||
6892             C->getNameModifier() == OMPD_parallel) {
6893           IfClause = C;
6894           break;
6895         }
6896       }
6897       if (IfClause) {
6898         const Expr *Cond = IfClause->getCondition();
6899         bool Result;
6900         if (Cond->EvaluateAsBooleanCondition(Result, CGF.getContext())) {
6901           if (!Result)
6902             return Bld.getInt32(1);
6903         } else {
6904           CodeGenFunction::RunCleanupsScope Scope(CGF);
6905           CondVal = CGF.EvaluateExprAsBool(Cond);
6906         }
6907       }
6908     }
6909     if (D.hasClausesOfKind<OMPThreadLimitClause>()) {
6910       CodeGenFunction::RunCleanupsScope ThreadLimitScope(CGF);
6911       const auto *ThreadLimitClause = D.getSingleClause<OMPThreadLimitClause>();
6912       llvm::Value *ThreadLimit = CGF.EmitScalarExpr(
6913           ThreadLimitClause->getThreadLimit(), /*IgnoreResultAssign=*/true);
6914       ThreadLimitVal =
6915           Bld.CreateIntCast(ThreadLimit, CGF.Int32Ty, /*isSigned=*/false);
6916     }
6917     if (D.hasClausesOfKind<OMPNumThreadsClause>()) {
6918       CodeGenFunction::RunCleanupsScope NumThreadsScope(CGF);
6919       const auto *NumThreadsClause = D.getSingleClause<OMPNumThreadsClause>();
6920       llvm::Value *NumThreads = CGF.EmitScalarExpr(
6921           NumThreadsClause->getNumThreads(), /*IgnoreResultAssign=*/true);
6922       NumThreadsVal =
6923           Bld.CreateIntCast(NumThreads, CGF.Int32Ty, /*isSigned=*/false);
6924       ThreadLimitVal = ThreadLimitVal
6925                            ? Bld.CreateSelect(Bld.CreateICmpULT(NumThreadsVal,
6926                                                                 ThreadLimitVal),
6927                                               NumThreadsVal, ThreadLimitVal)
6928                            : NumThreadsVal;
6929     }
6930     if (!ThreadLimitVal)
6931       ThreadLimitVal = Bld.getInt32(0);
6932     if (CondVal)
6933       return Bld.CreateSelect(CondVal, ThreadLimitVal, Bld.getInt32(1));
6934     return ThreadLimitVal;
6935   }
6936   case OMPD_target_teams_distribute_simd:
6937   case OMPD_target_simd:
6938     return Bld.getInt32(1);
6939   case OMPD_parallel:
6940   case OMPD_for:
6941   case OMPD_parallel_for:
6942   case OMPD_parallel_master:
6943   case OMPD_parallel_sections:
6944   case OMPD_for_simd:
6945   case OMPD_parallel_for_simd:
6946   case OMPD_cancel:
6947   case OMPD_cancellation_point:
6948   case OMPD_ordered:
6949   case OMPD_threadprivate:
6950   case OMPD_allocate:
6951   case OMPD_task:
6952   case OMPD_simd:
6953   case OMPD_sections:
6954   case OMPD_section:
6955   case OMPD_single:
6956   case OMPD_master:
6957   case OMPD_critical:
6958   case OMPD_taskyield:
6959   case OMPD_barrier:
6960   case OMPD_taskwait:
6961   case OMPD_taskgroup:
6962   case OMPD_atomic:
6963   case OMPD_flush:
6964   case OMPD_depobj:
6965   case OMPD_scan:
6966   case OMPD_teams:
6967   case OMPD_target_data:
6968   case OMPD_target_exit_data:
6969   case OMPD_target_enter_data:
6970   case OMPD_distribute:
6971   case OMPD_distribute_simd:
6972   case OMPD_distribute_parallel_for:
6973   case OMPD_distribute_parallel_for_simd:
6974   case OMPD_teams_distribute:
6975   case OMPD_teams_distribute_simd:
6976   case OMPD_teams_distribute_parallel_for:
6977   case OMPD_teams_distribute_parallel_for_simd:
6978   case OMPD_target_update:
6979   case OMPD_declare_simd:
6980   case OMPD_declare_variant:
6981   case OMPD_begin_declare_variant:
6982   case OMPD_end_declare_variant:
6983   case OMPD_declare_target:
6984   case OMPD_end_declare_target:
6985   case OMPD_declare_reduction:
6986   case OMPD_declare_mapper:
6987   case OMPD_taskloop:
6988   case OMPD_taskloop_simd:
6989   case OMPD_master_taskloop:
6990   case OMPD_master_taskloop_simd:
6991   case OMPD_parallel_master_taskloop:
6992   case OMPD_parallel_master_taskloop_simd:
6993   case OMPD_requires:
6994   case OMPD_unknown:
6995     break;
6996   default:
6997     break;
6998   }
6999   llvm_unreachable("Unsupported directive kind.");
7000 }
7001 
7002 namespace {
7003 LLVM_ENABLE_BITMASK_ENUMS_IN_NAMESPACE();
7004 
7005 // Utility to handle information from clauses associated with a given
7006 // construct that use mappable expressions (e.g. 'map' clause, 'to' clause).
7007 // It provides a convenient interface to obtain the information and generate
7008 // code for that information.
7009 class MappableExprsHandler {
7010 public:
7011   /// Values for bit flags used to specify the mapping type for
7012   /// offloading.
7013   enum OpenMPOffloadMappingFlags : uint64_t {
7014     /// No flags
7015     OMP_MAP_NONE = 0x0,
7016     /// Allocate memory on the device and move data from host to device.
7017     OMP_MAP_TO = 0x01,
7018     /// Allocate memory on the device and move data from device to host.
7019     OMP_MAP_FROM = 0x02,
7020     /// Always perform the requested mapping action on the element, even
7021     /// if it was already mapped before.
7022     OMP_MAP_ALWAYS = 0x04,
7023     /// Delete the element from the device environment, ignoring the
7024     /// current reference count associated with the element.
7025     OMP_MAP_DELETE = 0x08,
7026     /// The element being mapped is a pointer-pointee pair; both the
7027     /// pointer and the pointee should be mapped.
7028     OMP_MAP_PTR_AND_OBJ = 0x10,
7029     /// This flags signals that the base address of an entry should be
7030     /// passed to the target kernel as an argument.
7031     OMP_MAP_TARGET_PARAM = 0x20,
7032     /// Signal that the runtime library has to return the device pointer
7033     /// in the current position for the data being mapped. Used when we have the
7034     /// use_device_ptr or use_device_addr clause.
7035     OMP_MAP_RETURN_PARAM = 0x40,
7036     /// This flag signals that the reference being passed is a pointer to
7037     /// private data.
7038     OMP_MAP_PRIVATE = 0x80,
7039     /// Pass the element to the device by value.
7040     OMP_MAP_LITERAL = 0x100,
7041     /// Implicit map
7042     OMP_MAP_IMPLICIT = 0x200,
7043     /// Close is a hint to the runtime to allocate memory close to
7044     /// the target device.
7045     OMP_MAP_CLOSE = 0x400,
7046     /// The 16 MSBs of the flags indicate whether the entry is member of some
7047     /// struct/class.
7048     OMP_MAP_MEMBER_OF = 0xffff000000000000,
7049     LLVM_MARK_AS_BITMASK_ENUM(/* LargestFlag = */ OMP_MAP_MEMBER_OF),
7050   };
7051 
7052   /// Get the offset of the OMP_MAP_MEMBER_OF field.
7053   static unsigned getFlagMemberOffset() {
7054     unsigned Offset = 0;
7055     for (uint64_t Remain = OMP_MAP_MEMBER_OF; !(Remain & 1);
7056          Remain = Remain >> 1)
7057       Offset++;
7058     return Offset;
7059   }
7060 
7061   /// Class that associates information with a base pointer to be passed to the
7062   /// runtime library.
7063   class BasePointerInfo {
7064     /// The base pointer.
7065     llvm::Value *Ptr = nullptr;
7066     /// The base declaration that refers to this device pointer, or null if
7067     /// there is none.
7068     const ValueDecl *DevPtrDecl = nullptr;
7069 
7070   public:
7071     BasePointerInfo(llvm::Value *Ptr, const ValueDecl *DevPtrDecl = nullptr)
7072         : Ptr(Ptr), DevPtrDecl(DevPtrDecl) {}
7073     llvm::Value *operator*() const { return Ptr; }
7074     const ValueDecl *getDevicePtrDecl() const { return DevPtrDecl; }
7075     void setDevicePtrDecl(const ValueDecl *D) { DevPtrDecl = D; }
7076   };
7077 
7078   using MapBaseValuesArrayTy = SmallVector<BasePointerInfo, 4>;
7079   using MapValuesArrayTy = SmallVector<llvm::Value *, 4>;
7080   using MapFlagsArrayTy = SmallVector<OpenMPOffloadMappingFlags, 4>;
7081   using MapMappersArrayTy = SmallVector<const ValueDecl *, 4>;
7082 
7083   /// This structure contains combined information generated for mappable
7084   /// clauses, including base pointers, pointers, sizes, map types, and
7085   /// user-defined mappers.
7086   struct MapCombinedInfoTy {
7087     MapBaseValuesArrayTy BasePointers;
7088     MapValuesArrayTy Pointers;
7089     MapValuesArrayTy Sizes;
7090     MapFlagsArrayTy Types;
7091     MapMappersArrayTy Mappers;
7092 
7093     /// Append arrays in \a CurInfo.
7094     void append(MapCombinedInfoTy &CurInfo) {
7095       BasePointers.append(CurInfo.BasePointers.begin(),
7096                           CurInfo.BasePointers.end());
7097       Pointers.append(CurInfo.Pointers.begin(), CurInfo.Pointers.end());
7098       Sizes.append(CurInfo.Sizes.begin(), CurInfo.Sizes.end());
7099       Types.append(CurInfo.Types.begin(), CurInfo.Types.end());
7100       Mappers.append(CurInfo.Mappers.begin(), CurInfo.Mappers.end());
7101     }
7102   };
7103 
7104   /// Map between a struct and the its lowest & highest elements which have been
7105   /// mapped.
7106   /// [ValueDecl *] --> {LE(FieldIndex, Pointer),
7107   ///                    HE(FieldIndex, Pointer)}
7108   struct StructRangeInfoTy {
7109     std::pair<unsigned /*FieldIndex*/, Address /*Pointer*/> LowestElem = {
7110         0, Address::invalid()};
7111     std::pair<unsigned /*FieldIndex*/, Address /*Pointer*/> HighestElem = {
7112         0, Address::invalid()};
7113     Address Base = Address::invalid();
7114   };
7115 
7116 private:
7117   /// Kind that defines how a device pointer has to be returned.
7118   struct MapInfo {
7119     OMPClauseMappableExprCommon::MappableExprComponentListRef Components;
7120     OpenMPMapClauseKind MapType = OMPC_MAP_unknown;
7121     ArrayRef<OpenMPMapModifierKind> MapModifiers;
7122     bool ReturnDevicePointer = false;
7123     bool IsImplicit = false;
7124     const ValueDecl *Mapper = nullptr;
7125     bool ForDeviceAddr = false;
7126 
7127     MapInfo() = default;
7128     MapInfo(
7129         OMPClauseMappableExprCommon::MappableExprComponentListRef Components,
7130         OpenMPMapClauseKind MapType,
7131         ArrayRef<OpenMPMapModifierKind> MapModifiers, bool ReturnDevicePointer,
7132         bool IsImplicit, const ValueDecl *Mapper = nullptr,
7133         bool ForDeviceAddr = false)
7134         : Components(Components), MapType(MapType), MapModifiers(MapModifiers),
7135           ReturnDevicePointer(ReturnDevicePointer), IsImplicit(IsImplicit),
7136           Mapper(Mapper), ForDeviceAddr(ForDeviceAddr) {}
7137   };
7138 
7139   /// If use_device_ptr or use_device_addr is used on a decl which is a struct
7140   /// member and there is no map information about it, then emission of that
7141   /// entry is deferred until the whole struct has been processed.
7142   struct DeferredDevicePtrEntryTy {
7143     const Expr *IE = nullptr;
7144     const ValueDecl *VD = nullptr;
7145     bool ForDeviceAddr = false;
7146 
7147     DeferredDevicePtrEntryTy(const Expr *IE, const ValueDecl *VD,
7148                              bool ForDeviceAddr)
7149         : IE(IE), VD(VD), ForDeviceAddr(ForDeviceAddr) {}
7150   };
7151 
7152   /// The target directive from where the mappable clauses were extracted. It
7153   /// is either a executable directive or a user-defined mapper directive.
7154   llvm::PointerUnion<const OMPExecutableDirective *,
7155                      const OMPDeclareMapperDecl *>
7156       CurDir;
7157 
7158   /// Function the directive is being generated for.
7159   CodeGenFunction &CGF;
7160 
7161   /// Set of all first private variables in the current directive.
7162   /// bool data is set to true if the variable is implicitly marked as
7163   /// firstprivate, false otherwise.
7164   llvm::DenseMap<CanonicalDeclPtr<const VarDecl>, bool> FirstPrivateDecls;
7165 
7166   /// Map between device pointer declarations and their expression components.
7167   /// The key value for declarations in 'this' is null.
7168   llvm::DenseMap<
7169       const ValueDecl *,
7170       SmallVector<OMPClauseMappableExprCommon::MappableExprComponentListRef, 4>>
7171       DevPointersMap;
7172 
7173   llvm::Value *getExprTypeSize(const Expr *E) const {
7174     QualType ExprTy = E->getType().getCanonicalType();
7175 
7176     // Calculate the size for array shaping expression.
7177     if (const auto *OAE = dyn_cast<OMPArrayShapingExpr>(E)) {
7178       llvm::Value *Size =
7179           CGF.getTypeSize(OAE->getBase()->getType()->getPointeeType());
7180       for (const Expr *SE : OAE->getDimensions()) {
7181         llvm::Value *Sz = CGF.EmitScalarExpr(SE);
7182         Sz = CGF.EmitScalarConversion(Sz, SE->getType(),
7183                                       CGF.getContext().getSizeType(),
7184                                       SE->getExprLoc());
7185         Size = CGF.Builder.CreateNUWMul(Size, Sz);
7186       }
7187       return Size;
7188     }
7189 
7190     // Reference types are ignored for mapping purposes.
7191     if (const auto *RefTy = ExprTy->getAs<ReferenceType>())
7192       ExprTy = RefTy->getPointeeType().getCanonicalType();
7193 
7194     // Given that an array section is considered a built-in type, we need to
7195     // do the calculation based on the length of the section instead of relying
7196     // on CGF.getTypeSize(E->getType()).
7197     if (const auto *OAE = dyn_cast<OMPArraySectionExpr>(E)) {
7198       QualType BaseTy = OMPArraySectionExpr::getBaseOriginalType(
7199                             OAE->getBase()->IgnoreParenImpCasts())
7200                             .getCanonicalType();
7201 
7202       // If there is no length associated with the expression and lower bound is
7203       // not specified too, that means we are using the whole length of the
7204       // base.
7205       if (!OAE->getLength() && OAE->getColonLocFirst().isValid() &&
7206           !OAE->getLowerBound())
7207         return CGF.getTypeSize(BaseTy);
7208 
7209       llvm::Value *ElemSize;
7210       if (const auto *PTy = BaseTy->getAs<PointerType>()) {
7211         ElemSize = CGF.getTypeSize(PTy->getPointeeType().getCanonicalType());
7212       } else {
7213         const auto *ATy = cast<ArrayType>(BaseTy.getTypePtr());
7214         assert(ATy && "Expecting array type if not a pointer type.");
7215         ElemSize = CGF.getTypeSize(ATy->getElementType().getCanonicalType());
7216       }
7217 
7218       // If we don't have a length at this point, that is because we have an
7219       // array section with a single element.
7220       if (!OAE->getLength() && OAE->getColonLocFirst().isInvalid())
7221         return ElemSize;
7222 
7223       if (const Expr *LenExpr = OAE->getLength()) {
7224         llvm::Value *LengthVal = CGF.EmitScalarExpr(LenExpr);
7225         LengthVal = CGF.EmitScalarConversion(LengthVal, LenExpr->getType(),
7226                                              CGF.getContext().getSizeType(),
7227                                              LenExpr->getExprLoc());
7228         return CGF.Builder.CreateNUWMul(LengthVal, ElemSize);
7229       }
7230       assert(!OAE->getLength() && OAE->getColonLocFirst().isValid() &&
7231              OAE->getLowerBound() && "expected array_section[lb:].");
7232       // Size = sizetype - lb * elemtype;
7233       llvm::Value *LengthVal = CGF.getTypeSize(BaseTy);
7234       llvm::Value *LBVal = CGF.EmitScalarExpr(OAE->getLowerBound());
7235       LBVal = CGF.EmitScalarConversion(LBVal, OAE->getLowerBound()->getType(),
7236                                        CGF.getContext().getSizeType(),
7237                                        OAE->getLowerBound()->getExprLoc());
7238       LBVal = CGF.Builder.CreateNUWMul(LBVal, ElemSize);
7239       llvm::Value *Cmp = CGF.Builder.CreateICmpUGT(LengthVal, LBVal);
7240       llvm::Value *TrueVal = CGF.Builder.CreateNUWSub(LengthVal, LBVal);
7241       LengthVal = CGF.Builder.CreateSelect(
7242           Cmp, TrueVal, llvm::ConstantInt::get(CGF.SizeTy, 0));
7243       return LengthVal;
7244     }
7245     return CGF.getTypeSize(ExprTy);
7246   }
7247 
7248   /// Return the corresponding bits for a given map clause modifier. Add
7249   /// a flag marking the map as a pointer if requested. Add a flag marking the
7250   /// map as the first one of a series of maps that relate to the same map
7251   /// expression.
7252   OpenMPOffloadMappingFlags getMapTypeBits(
7253       OpenMPMapClauseKind MapType, ArrayRef<OpenMPMapModifierKind> MapModifiers,
7254       bool IsImplicit, bool AddPtrFlag, bool AddIsTargetParamFlag) const {
7255     OpenMPOffloadMappingFlags Bits =
7256         IsImplicit ? OMP_MAP_IMPLICIT : OMP_MAP_NONE;
7257     switch (MapType) {
7258     case OMPC_MAP_alloc:
7259     case OMPC_MAP_release:
7260       // alloc and release is the default behavior in the runtime library,  i.e.
7261       // if we don't pass any bits alloc/release that is what the runtime is
7262       // going to do. Therefore, we don't need to signal anything for these two
7263       // type modifiers.
7264       break;
7265     case OMPC_MAP_to:
7266       Bits |= OMP_MAP_TO;
7267       break;
7268     case OMPC_MAP_from:
7269       Bits |= OMP_MAP_FROM;
7270       break;
7271     case OMPC_MAP_tofrom:
7272       Bits |= OMP_MAP_TO | OMP_MAP_FROM;
7273       break;
7274     case OMPC_MAP_delete:
7275       Bits |= OMP_MAP_DELETE;
7276       break;
7277     case OMPC_MAP_unknown:
7278       llvm_unreachable("Unexpected map type!");
7279     }
7280     if (AddPtrFlag)
7281       Bits |= OMP_MAP_PTR_AND_OBJ;
7282     if (AddIsTargetParamFlag)
7283       Bits |= OMP_MAP_TARGET_PARAM;
7284     if (llvm::find(MapModifiers, OMPC_MAP_MODIFIER_always)
7285         != MapModifiers.end())
7286       Bits |= OMP_MAP_ALWAYS;
7287     if (llvm::find(MapModifiers, OMPC_MAP_MODIFIER_close)
7288         != MapModifiers.end())
7289       Bits |= OMP_MAP_CLOSE;
7290     return Bits;
7291   }
7292 
7293   /// Return true if the provided expression is a final array section. A
7294   /// final array section, is one whose length can't be proved to be one.
7295   bool isFinalArraySectionExpression(const Expr *E) const {
7296     const auto *OASE = dyn_cast<OMPArraySectionExpr>(E);
7297 
7298     // It is not an array section and therefore not a unity-size one.
7299     if (!OASE)
7300       return false;
7301 
7302     // An array section with no colon always refer to a single element.
7303     if (OASE->getColonLocFirst().isInvalid())
7304       return false;
7305 
7306     const Expr *Length = OASE->getLength();
7307 
7308     // If we don't have a length we have to check if the array has size 1
7309     // for this dimension. Also, we should always expect a length if the
7310     // base type is pointer.
7311     if (!Length) {
7312       QualType BaseQTy = OMPArraySectionExpr::getBaseOriginalType(
7313                              OASE->getBase()->IgnoreParenImpCasts())
7314                              .getCanonicalType();
7315       if (const auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr()))
7316         return ATy->getSize().getSExtValue() != 1;
7317       // If we don't have a constant dimension length, we have to consider
7318       // the current section as having any size, so it is not necessarily
7319       // unitary. If it happen to be unity size, that's user fault.
7320       return true;
7321     }
7322 
7323     // Check if the length evaluates to 1.
7324     Expr::EvalResult Result;
7325     if (!Length->EvaluateAsInt(Result, CGF.getContext()))
7326       return true; // Can have more that size 1.
7327 
7328     llvm::APSInt ConstLength = Result.Val.getInt();
7329     return ConstLength.getSExtValue() != 1;
7330   }
7331 
7332   /// Generate the base pointers, section pointers, sizes, map type bits, and
7333   /// user-defined mappers (all included in \a CombinedInfo) for the provided
7334   /// map type, map modifier, and expression components. \a IsFirstComponent
7335   /// should be set to true if the provided set of components is the first
7336   /// associated with a capture.
7337   void generateInfoForComponentList(
7338       OpenMPMapClauseKind MapType, ArrayRef<OpenMPMapModifierKind> MapModifiers,
7339       OMPClauseMappableExprCommon::MappableExprComponentListRef Components,
7340       MapCombinedInfoTy &CombinedInfo, StructRangeInfoTy &PartialStruct,
7341       bool IsFirstComponentList, bool IsImplicit,
7342       const ValueDecl *Mapper = nullptr, bool ForDeviceAddr = false,
7343       ArrayRef<OMPClauseMappableExprCommon::MappableExprComponentListRef>
7344           OverlappedElements = llvm::None) const {
7345     // The following summarizes what has to be generated for each map and the
7346     // types below. The generated information is expressed in this order:
7347     // base pointer, section pointer, size, flags
7348     // (to add to the ones that come from the map type and modifier).
7349     //
7350     // double d;
7351     // int i[100];
7352     // float *p;
7353     //
7354     // struct S1 {
7355     //   int i;
7356     //   float f[50];
7357     // }
7358     // struct S2 {
7359     //   int i;
7360     //   float f[50];
7361     //   S1 s;
7362     //   double *p;
7363     //   struct S2 *ps;
7364     // }
7365     // S2 s;
7366     // S2 *ps;
7367     //
7368     // map(d)
7369     // &d, &d, sizeof(double), TARGET_PARAM | TO | FROM
7370     //
7371     // map(i)
7372     // &i, &i, 100*sizeof(int), TARGET_PARAM | TO | FROM
7373     //
7374     // map(i[1:23])
7375     // &i(=&i[0]), &i[1], 23*sizeof(int), TARGET_PARAM | TO | FROM
7376     //
7377     // map(p)
7378     // &p, &p, sizeof(float*), TARGET_PARAM | TO | FROM
7379     //
7380     // map(p[1:24])
7381     // p, &p[1], 24*sizeof(float), TARGET_PARAM | TO | FROM
7382     //
7383     // map(s)
7384     // &s, &s, sizeof(S2), TARGET_PARAM | TO | FROM
7385     //
7386     // map(s.i)
7387     // &s, &(s.i), sizeof(int), TARGET_PARAM | TO | FROM
7388     //
7389     // map(s.s.f)
7390     // &s, &(s.s.f[0]), 50*sizeof(float), TARGET_PARAM | TO | FROM
7391     //
7392     // map(s.p)
7393     // &s, &(s.p), sizeof(double*), TARGET_PARAM | TO | FROM
7394     //
7395     // map(to: s.p[:22])
7396     // &s, &(s.p), sizeof(double*), TARGET_PARAM (*)
7397     // &s, &(s.p), sizeof(double*), MEMBER_OF(1) (**)
7398     // &(s.p), &(s.p[0]), 22*sizeof(double),
7399     //   MEMBER_OF(1) | PTR_AND_OBJ | TO (***)
7400     // (*) alloc space for struct members, only this is a target parameter
7401     // (**) map the pointer (nothing to be mapped in this example) (the compiler
7402     //      optimizes this entry out, same in the examples below)
7403     // (***) map the pointee (map: to)
7404     //
7405     // map(s.ps)
7406     // &s, &(s.ps), sizeof(S2*), TARGET_PARAM | TO | FROM
7407     //
7408     // map(from: s.ps->s.i)
7409     // &s, &(s.ps), sizeof(S2*), TARGET_PARAM
7410     // &s, &(s.ps), sizeof(S2*), MEMBER_OF(1)
7411     // &(s.ps), &(s.ps->s.i), sizeof(int), MEMBER_OF(1) | PTR_AND_OBJ  | FROM
7412     //
7413     // map(to: s.ps->ps)
7414     // &s, &(s.ps), sizeof(S2*), TARGET_PARAM
7415     // &s, &(s.ps), sizeof(S2*), MEMBER_OF(1)
7416     // &(s.ps), &(s.ps->ps), sizeof(S2*), MEMBER_OF(1) | PTR_AND_OBJ  | TO
7417     //
7418     // map(s.ps->ps->ps)
7419     // &s, &(s.ps), sizeof(S2*), TARGET_PARAM
7420     // &s, &(s.ps), sizeof(S2*), MEMBER_OF(1)
7421     // &(s.ps), &(s.ps->ps), sizeof(S2*), MEMBER_OF(1) | PTR_AND_OBJ
7422     // &(s.ps->ps), &(s.ps->ps->ps), sizeof(S2*), PTR_AND_OBJ | TO | FROM
7423     //
7424     // map(to: s.ps->ps->s.f[:22])
7425     // &s, &(s.ps), sizeof(S2*), TARGET_PARAM
7426     // &s, &(s.ps), sizeof(S2*), MEMBER_OF(1)
7427     // &(s.ps), &(s.ps->ps), sizeof(S2*), MEMBER_OF(1) | PTR_AND_OBJ
7428     // &(s.ps->ps), &(s.ps->ps->s.f[0]), 22*sizeof(float), PTR_AND_OBJ | TO
7429     //
7430     // map(ps)
7431     // &ps, &ps, sizeof(S2*), TARGET_PARAM | TO | FROM
7432     //
7433     // map(ps->i)
7434     // ps, &(ps->i), sizeof(int), TARGET_PARAM | TO | FROM
7435     //
7436     // map(ps->s.f)
7437     // ps, &(ps->s.f[0]), 50*sizeof(float), TARGET_PARAM | TO | FROM
7438     //
7439     // map(from: ps->p)
7440     // ps, &(ps->p), sizeof(double*), TARGET_PARAM | FROM
7441     //
7442     // map(to: ps->p[:22])
7443     // ps, &(ps->p), sizeof(double*), TARGET_PARAM
7444     // ps, &(ps->p), sizeof(double*), MEMBER_OF(1)
7445     // &(ps->p), &(ps->p[0]), 22*sizeof(double), MEMBER_OF(1) | PTR_AND_OBJ | TO
7446     //
7447     // map(ps->ps)
7448     // ps, &(ps->ps), sizeof(S2*), TARGET_PARAM | TO | FROM
7449     //
7450     // map(from: ps->ps->s.i)
7451     // ps, &(ps->ps), sizeof(S2*), TARGET_PARAM
7452     // ps, &(ps->ps), sizeof(S2*), MEMBER_OF(1)
7453     // &(ps->ps), &(ps->ps->s.i), sizeof(int), MEMBER_OF(1) | PTR_AND_OBJ | FROM
7454     //
7455     // map(from: ps->ps->ps)
7456     // ps, &(ps->ps), sizeof(S2*), TARGET_PARAM
7457     // ps, &(ps->ps), sizeof(S2*), MEMBER_OF(1)
7458     // &(ps->ps), &(ps->ps->ps), sizeof(S2*), MEMBER_OF(1) | PTR_AND_OBJ | FROM
7459     //
7460     // map(ps->ps->ps->ps)
7461     // ps, &(ps->ps), sizeof(S2*), TARGET_PARAM
7462     // ps, &(ps->ps), sizeof(S2*), MEMBER_OF(1)
7463     // &(ps->ps), &(ps->ps->ps), sizeof(S2*), MEMBER_OF(1) | PTR_AND_OBJ
7464     // &(ps->ps->ps), &(ps->ps->ps->ps), sizeof(S2*), PTR_AND_OBJ | TO | FROM
7465     //
7466     // map(to: ps->ps->ps->s.f[:22])
7467     // ps, &(ps->ps), sizeof(S2*), TARGET_PARAM
7468     // ps, &(ps->ps), sizeof(S2*), MEMBER_OF(1)
7469     // &(ps->ps), &(ps->ps->ps), sizeof(S2*), MEMBER_OF(1) | PTR_AND_OBJ
7470     // &(ps->ps->ps), &(ps->ps->ps->s.f[0]), 22*sizeof(float), PTR_AND_OBJ | TO
7471     //
7472     // map(to: s.f[:22]) map(from: s.p[:33])
7473     // &s, &(s.f[0]), 50*sizeof(float) + sizeof(struct S1) +
7474     //     sizeof(double*) (**), TARGET_PARAM
7475     // &s, &(s.f[0]), 22*sizeof(float), MEMBER_OF(1) | TO
7476     // &s, &(s.p), sizeof(double*), MEMBER_OF(1)
7477     // &(s.p), &(s.p[0]), 33*sizeof(double), MEMBER_OF(1) | PTR_AND_OBJ | FROM
7478     // (*) allocate contiguous space needed to fit all mapped members even if
7479     //     we allocate space for members not mapped (in this example,
7480     //     s.f[22..49] and s.s are not mapped, yet we must allocate space for
7481     //     them as well because they fall between &s.f[0] and &s.p)
7482     //
7483     // map(from: s.f[:22]) map(to: ps->p[:33])
7484     // &s, &(s.f[0]), 22*sizeof(float), TARGET_PARAM | FROM
7485     // ps, &(ps->p), sizeof(S2*), TARGET_PARAM
7486     // ps, &(ps->p), sizeof(double*), MEMBER_OF(2) (*)
7487     // &(ps->p), &(ps->p[0]), 33*sizeof(double), MEMBER_OF(2) | PTR_AND_OBJ | TO
7488     // (*) the struct this entry pertains to is the 2nd element in the list of
7489     //     arguments, hence MEMBER_OF(2)
7490     //
7491     // map(from: s.f[:22], s.s) map(to: ps->p[:33])
7492     // &s, &(s.f[0]), 50*sizeof(float) + sizeof(struct S1), TARGET_PARAM
7493     // &s, &(s.f[0]), 22*sizeof(float), MEMBER_OF(1) | FROM
7494     // &s, &(s.s), sizeof(struct S1), MEMBER_OF(1) | FROM
7495     // ps, &(ps->p), sizeof(S2*), TARGET_PARAM
7496     // ps, &(ps->p), sizeof(double*), MEMBER_OF(4) (*)
7497     // &(ps->p), &(ps->p[0]), 33*sizeof(double), MEMBER_OF(4) | PTR_AND_OBJ | TO
7498     // (*) the struct this entry pertains to is the 4th element in the list
7499     //     of arguments, hence MEMBER_OF(4)
7500 
7501     // Track if the map information being generated is the first for a capture.
7502     bool IsCaptureFirstInfo = IsFirstComponentList;
7503     // When the variable is on a declare target link or in a to clause with
7504     // unified memory, a reference is needed to hold the host/device address
7505     // of the variable.
7506     bool RequiresReference = false;
7507 
7508     // Scan the components from the base to the complete expression.
7509     auto CI = Components.rbegin();
7510     auto CE = Components.rend();
7511     auto I = CI;
7512 
7513     // Track if the map information being generated is the first for a list of
7514     // components.
7515     bool IsExpressionFirstInfo = true;
7516     Address BP = Address::invalid();
7517     const Expr *AssocExpr = I->getAssociatedExpression();
7518     const auto *AE = dyn_cast<ArraySubscriptExpr>(AssocExpr);
7519     const auto *OASE = dyn_cast<OMPArraySectionExpr>(AssocExpr);
7520     const auto *OAShE = dyn_cast<OMPArrayShapingExpr>(AssocExpr);
7521 
7522     if (isa<MemberExpr>(AssocExpr)) {
7523       // The base is the 'this' pointer. The content of the pointer is going
7524       // to be the base of the field being mapped.
7525       BP = CGF.LoadCXXThisAddress();
7526     } else if ((AE && isa<CXXThisExpr>(AE->getBase()->IgnoreParenImpCasts())) ||
7527                (OASE &&
7528                 isa<CXXThisExpr>(OASE->getBase()->IgnoreParenImpCasts()))) {
7529       BP = CGF.EmitOMPSharedLValue(AssocExpr).getAddress(CGF);
7530     } else if (OAShE &&
7531                isa<CXXThisExpr>(OAShE->getBase()->IgnoreParenCasts())) {
7532       BP = Address(
7533           CGF.EmitScalarExpr(OAShE->getBase()),
7534           CGF.getContext().getTypeAlignInChars(OAShE->getBase()->getType()));
7535     } else {
7536       // The base is the reference to the variable.
7537       // BP = &Var.
7538       BP = CGF.EmitOMPSharedLValue(AssocExpr).getAddress(CGF);
7539       if (const auto *VD =
7540               dyn_cast_or_null<VarDecl>(I->getAssociatedDeclaration())) {
7541         if (llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res =
7542                 OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD)) {
7543           if ((*Res == OMPDeclareTargetDeclAttr::MT_Link) ||
7544               (*Res == OMPDeclareTargetDeclAttr::MT_To &&
7545                CGF.CGM.getOpenMPRuntime().hasRequiresUnifiedSharedMemory())) {
7546             RequiresReference = true;
7547             BP = CGF.CGM.getOpenMPRuntime().getAddrOfDeclareTargetVar(VD);
7548           }
7549         }
7550       }
7551 
7552       // If the variable is a pointer and is being dereferenced (i.e. is not
7553       // the last component), the base has to be the pointer itself, not its
7554       // reference. References are ignored for mapping purposes.
7555       QualType Ty =
7556           I->getAssociatedDeclaration()->getType().getNonReferenceType();
7557       if (Ty->isAnyPointerType() && std::next(I) != CE) {
7558         BP = CGF.EmitLoadOfPointer(BP, Ty->castAs<PointerType>());
7559 
7560         // We do not need to generate individual map information for the
7561         // pointer, it can be associated with the combined storage.
7562         ++I;
7563       }
7564     }
7565 
7566     // Track whether a component of the list should be marked as MEMBER_OF some
7567     // combined entry (for partial structs). Only the first PTR_AND_OBJ entry
7568     // in a component list should be marked as MEMBER_OF, all subsequent entries
7569     // do not belong to the base struct. E.g.
7570     // struct S2 s;
7571     // s.ps->ps->ps->f[:]
7572     //   (1) (2) (3) (4)
7573     // ps(1) is a member pointer, ps(2) is a pointee of ps(1), so it is a
7574     // PTR_AND_OBJ entry; the PTR is ps(1), so MEMBER_OF the base struct. ps(3)
7575     // is the pointee of ps(2) which is not member of struct s, so it should not
7576     // be marked as such (it is still PTR_AND_OBJ).
7577     // The variable is initialized to false so that PTR_AND_OBJ entries which
7578     // are not struct members are not considered (e.g. array of pointers to
7579     // data).
7580     bool ShouldBeMemberOf = false;
7581 
7582     // Variable keeping track of whether or not we have encountered a component
7583     // in the component list which is a member expression. Useful when we have a
7584     // pointer or a final array section, in which case it is the previous
7585     // component in the list which tells us whether we have a member expression.
7586     // E.g. X.f[:]
7587     // While processing the final array section "[:]" it is "f" which tells us
7588     // whether we are dealing with a member of a declared struct.
7589     const MemberExpr *EncounteredME = nullptr;
7590 
7591     for (; I != CE; ++I) {
7592       // If the current component is member of a struct (parent struct) mark it.
7593       if (!EncounteredME) {
7594         EncounteredME = dyn_cast<MemberExpr>(I->getAssociatedExpression());
7595         // If we encounter a PTR_AND_OBJ entry from now on it should be marked
7596         // as MEMBER_OF the parent struct.
7597         if (EncounteredME)
7598           ShouldBeMemberOf = true;
7599       }
7600 
7601       auto Next = std::next(I);
7602 
7603       // We need to generate the addresses and sizes if this is the last
7604       // component, if the component is a pointer or if it is an array section
7605       // whose length can't be proved to be one. If this is a pointer, it
7606       // becomes the base address for the following components.
7607 
7608       // A final array section, is one whose length can't be proved to be one.
7609       bool IsFinalArraySection =
7610           isFinalArraySectionExpression(I->getAssociatedExpression());
7611 
7612       // Get information on whether the element is a pointer. Have to do a
7613       // special treatment for array sections given that they are built-in
7614       // types.
7615       const auto *OASE =
7616           dyn_cast<OMPArraySectionExpr>(I->getAssociatedExpression());
7617       const auto *OAShE =
7618           dyn_cast<OMPArrayShapingExpr>(I->getAssociatedExpression());
7619       const auto *UO = dyn_cast<UnaryOperator>(I->getAssociatedExpression());
7620       const auto *BO = dyn_cast<BinaryOperator>(I->getAssociatedExpression());
7621       bool IsPointer =
7622           OAShE ||
7623           (OASE && OMPArraySectionExpr::getBaseOriginalType(OASE)
7624                        .getCanonicalType()
7625                        ->isAnyPointerType()) ||
7626           I->getAssociatedExpression()->getType()->isAnyPointerType();
7627       bool IsNonDerefPointer = IsPointer && !UO && !BO;
7628 
7629       if (Next == CE || IsNonDerefPointer || IsFinalArraySection) {
7630         // If this is not the last component, we expect the pointer to be
7631         // associated with an array expression or member expression.
7632         assert((Next == CE ||
7633                 isa<MemberExpr>(Next->getAssociatedExpression()) ||
7634                 isa<ArraySubscriptExpr>(Next->getAssociatedExpression()) ||
7635                 isa<OMPArraySectionExpr>(Next->getAssociatedExpression()) ||
7636                 isa<UnaryOperator>(Next->getAssociatedExpression()) ||
7637                 isa<BinaryOperator>(Next->getAssociatedExpression())) &&
7638                "Unexpected expression");
7639 
7640         Address LB = Address::invalid();
7641         if (OAShE) {
7642           LB = Address(CGF.EmitScalarExpr(OAShE->getBase()),
7643                        CGF.getContext().getTypeAlignInChars(
7644                            OAShE->getBase()->getType()));
7645         } else {
7646           LB = CGF.EmitOMPSharedLValue(I->getAssociatedExpression())
7647                    .getAddress(CGF);
7648         }
7649 
7650         // If this component is a pointer inside the base struct then we don't
7651         // need to create any entry for it - it will be combined with the object
7652         // it is pointing to into a single PTR_AND_OBJ entry.
7653         bool IsMemberPointerOrAddr =
7654             (IsPointer || ForDeviceAddr) && EncounteredME &&
7655             (dyn_cast<MemberExpr>(I->getAssociatedExpression()) ==
7656              EncounteredME);
7657         if (!OverlappedElements.empty()) {
7658           // Handle base element with the info for overlapped elements.
7659           assert(!PartialStruct.Base.isValid() && "The base element is set.");
7660           assert(Next == CE &&
7661                  "Expected last element for the overlapped elements.");
7662           assert(!IsPointer &&
7663                  "Unexpected base element with the pointer type.");
7664           // Mark the whole struct as the struct that requires allocation on the
7665           // device.
7666           PartialStruct.LowestElem = {0, LB};
7667           CharUnits TypeSize = CGF.getContext().getTypeSizeInChars(
7668               I->getAssociatedExpression()->getType());
7669           Address HB = CGF.Builder.CreateConstGEP(
7670               CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(LB,
7671                                                               CGF.VoidPtrTy),
7672               TypeSize.getQuantity() - 1);
7673           PartialStruct.HighestElem = {
7674               std::numeric_limits<decltype(
7675                   PartialStruct.HighestElem.first)>::max(),
7676               HB};
7677           PartialStruct.Base = BP;
7678           // Emit data for non-overlapped data.
7679           OpenMPOffloadMappingFlags Flags =
7680               OMP_MAP_MEMBER_OF |
7681               getMapTypeBits(MapType, MapModifiers, IsImplicit,
7682                              /*AddPtrFlag=*/false,
7683                              /*AddIsTargetParamFlag=*/false);
7684           LB = BP;
7685           llvm::Value *Size = nullptr;
7686           // Do bitcopy of all non-overlapped structure elements.
7687           for (OMPClauseMappableExprCommon::MappableExprComponentListRef
7688                    Component : OverlappedElements) {
7689             Address ComponentLB = Address::invalid();
7690             for (const OMPClauseMappableExprCommon::MappableComponent &MC :
7691                  Component) {
7692               if (MC.getAssociatedDeclaration()) {
7693                 ComponentLB =
7694                     CGF.EmitOMPSharedLValue(MC.getAssociatedExpression())
7695                         .getAddress(CGF);
7696                 Size = CGF.Builder.CreatePtrDiff(
7697                     CGF.EmitCastToVoidPtr(ComponentLB.getPointer()),
7698                     CGF.EmitCastToVoidPtr(LB.getPointer()));
7699                 break;
7700               }
7701             }
7702             CombinedInfo.BasePointers.push_back(BP.getPointer());
7703             CombinedInfo.Pointers.push_back(LB.getPointer());
7704             CombinedInfo.Sizes.push_back(CGF.Builder.CreateIntCast(
7705                 Size, CGF.Int64Ty, /*isSigned=*/true));
7706             CombinedInfo.Types.push_back(Flags);
7707             CombinedInfo.Mappers.push_back(nullptr);
7708             LB = CGF.Builder.CreateConstGEP(ComponentLB, 1);
7709           }
7710           CombinedInfo.BasePointers.push_back(BP.getPointer());
7711           CombinedInfo.Pointers.push_back(LB.getPointer());
7712           Size = CGF.Builder.CreatePtrDiff(
7713               CGF.EmitCastToVoidPtr(
7714                   CGF.Builder.CreateConstGEP(HB, 1).getPointer()),
7715               CGF.EmitCastToVoidPtr(LB.getPointer()));
7716           CombinedInfo.Sizes.push_back(
7717               CGF.Builder.CreateIntCast(Size, CGF.Int64Ty, /*isSigned=*/true));
7718           CombinedInfo.Types.push_back(Flags);
7719           CombinedInfo.Mappers.push_back(nullptr);
7720           break;
7721         }
7722         llvm::Value *Size = getExprTypeSize(I->getAssociatedExpression());
7723         if (!IsMemberPointerOrAddr) {
7724           CombinedInfo.BasePointers.push_back(BP.getPointer());
7725           CombinedInfo.Pointers.push_back(LB.getPointer());
7726           CombinedInfo.Sizes.push_back(
7727               CGF.Builder.CreateIntCast(Size, CGF.Int64Ty, /*isSigned=*/true));
7728 
7729           // If Mapper is valid, the last component inherits the mapper.
7730           bool HasMapper = Mapper && Next == CE;
7731           CombinedInfo.Mappers.push_back(HasMapper ? Mapper : nullptr);
7732 
7733           // We need to add a pointer flag for each map that comes from the
7734           // same expression except for the first one. We also need to signal
7735           // this map is the first one that relates with the current capture
7736           // (there is a set of entries for each capture).
7737           OpenMPOffloadMappingFlags Flags = getMapTypeBits(
7738               MapType, MapModifiers, IsImplicit,
7739               !IsExpressionFirstInfo || RequiresReference,
7740               IsCaptureFirstInfo && !RequiresReference);
7741 
7742           if (!IsExpressionFirstInfo) {
7743             // If we have a PTR_AND_OBJ pair where the OBJ is a pointer as well,
7744             // then we reset the TO/FROM/ALWAYS/DELETE/CLOSE flags.
7745             if (IsPointer)
7746               Flags &= ~(OMP_MAP_TO | OMP_MAP_FROM | OMP_MAP_ALWAYS |
7747                          OMP_MAP_DELETE | OMP_MAP_CLOSE);
7748 
7749             if (ShouldBeMemberOf) {
7750               // Set placeholder value MEMBER_OF=FFFF to indicate that the flag
7751               // should be later updated with the correct value of MEMBER_OF.
7752               Flags |= OMP_MAP_MEMBER_OF;
7753               // From now on, all subsequent PTR_AND_OBJ entries should not be
7754               // marked as MEMBER_OF.
7755               ShouldBeMemberOf = false;
7756             }
7757           }
7758 
7759           CombinedInfo.Types.push_back(Flags);
7760         }
7761 
7762         // If we have encountered a member expression so far, keep track of the
7763         // mapped member. If the parent is "*this", then the value declaration
7764         // is nullptr.
7765         if (EncounteredME) {
7766           const auto *FD = cast<FieldDecl>(EncounteredME->getMemberDecl());
7767           unsigned FieldIndex = FD->getFieldIndex();
7768 
7769           // Update info about the lowest and highest elements for this struct
7770           if (!PartialStruct.Base.isValid()) {
7771             PartialStruct.LowestElem = {FieldIndex, LB};
7772             if (IsFinalArraySection) {
7773               Address HB =
7774                   CGF.EmitOMPArraySectionExpr(OASE, /*IsLowerBound=*/false)
7775                       .getAddress(CGF);
7776               PartialStruct.HighestElem = {FieldIndex, HB};
7777             } else {
7778               PartialStruct.HighestElem = {FieldIndex, LB};
7779             }
7780             PartialStruct.Base = BP;
7781           } else if (FieldIndex < PartialStruct.LowestElem.first) {
7782             PartialStruct.LowestElem = {FieldIndex, LB};
7783           } else if (FieldIndex > PartialStruct.HighestElem.first) {
7784             PartialStruct.HighestElem = {FieldIndex, LB};
7785           }
7786         }
7787 
7788         // If we have a final array section, we are done with this expression.
7789         if (IsFinalArraySection)
7790           break;
7791 
7792         // The pointer becomes the base for the next element.
7793         if (Next != CE)
7794           BP = LB;
7795 
7796         IsExpressionFirstInfo = false;
7797         IsCaptureFirstInfo = false;
7798       }
7799     }
7800   }
7801 
7802   /// Return the adjusted map modifiers if the declaration a capture refers to
7803   /// appears in a first-private clause. This is expected to be used only with
7804   /// directives that start with 'target'.
7805   MappableExprsHandler::OpenMPOffloadMappingFlags
7806   getMapModifiersForPrivateClauses(const CapturedStmt::Capture &Cap) const {
7807     assert(Cap.capturesVariable() && "Expected capture by reference only!");
7808 
7809     // A first private variable captured by reference will use only the
7810     // 'private ptr' and 'map to' flag. Return the right flags if the captured
7811     // declaration is known as first-private in this handler.
7812     if (FirstPrivateDecls.count(Cap.getCapturedVar())) {
7813       if (Cap.getCapturedVar()->getType().isConstant(CGF.getContext()) &&
7814           Cap.getCaptureKind() == CapturedStmt::VCK_ByRef)
7815         return MappableExprsHandler::OMP_MAP_ALWAYS |
7816                MappableExprsHandler::OMP_MAP_TO;
7817       if (Cap.getCapturedVar()->getType()->isAnyPointerType())
7818         return MappableExprsHandler::OMP_MAP_TO |
7819                MappableExprsHandler::OMP_MAP_PTR_AND_OBJ;
7820       return MappableExprsHandler::OMP_MAP_PRIVATE |
7821              MappableExprsHandler::OMP_MAP_TO;
7822     }
7823     return MappableExprsHandler::OMP_MAP_TO |
7824            MappableExprsHandler::OMP_MAP_FROM;
7825   }
7826 
7827   static OpenMPOffloadMappingFlags getMemberOfFlag(unsigned Position) {
7828     // Rotate by getFlagMemberOffset() bits.
7829     return static_cast<OpenMPOffloadMappingFlags>(((uint64_t)Position + 1)
7830                                                   << getFlagMemberOffset());
7831   }
7832 
7833   static void setCorrectMemberOfFlag(OpenMPOffloadMappingFlags &Flags,
7834                                      OpenMPOffloadMappingFlags MemberOfFlag) {
7835     // If the entry is PTR_AND_OBJ but has not been marked with the special
7836     // placeholder value 0xFFFF in the MEMBER_OF field, then it should not be
7837     // marked as MEMBER_OF.
7838     if ((Flags & OMP_MAP_PTR_AND_OBJ) &&
7839         ((Flags & OMP_MAP_MEMBER_OF) != OMP_MAP_MEMBER_OF))
7840       return;
7841 
7842     // Reset the placeholder value to prepare the flag for the assignment of the
7843     // proper MEMBER_OF value.
7844     Flags &= ~OMP_MAP_MEMBER_OF;
7845     Flags |= MemberOfFlag;
7846   }
7847 
7848   void getPlainLayout(const CXXRecordDecl *RD,
7849                       llvm::SmallVectorImpl<const FieldDecl *> &Layout,
7850                       bool AsBase) const {
7851     const CGRecordLayout &RL = CGF.getTypes().getCGRecordLayout(RD);
7852 
7853     llvm::StructType *St =
7854         AsBase ? RL.getBaseSubobjectLLVMType() : RL.getLLVMType();
7855 
7856     unsigned NumElements = St->getNumElements();
7857     llvm::SmallVector<
7858         llvm::PointerUnion<const CXXRecordDecl *, const FieldDecl *>, 4>
7859         RecordLayout(NumElements);
7860 
7861     // Fill bases.
7862     for (const auto &I : RD->bases()) {
7863       if (I.isVirtual())
7864         continue;
7865       const auto *Base = I.getType()->getAsCXXRecordDecl();
7866       // Ignore empty bases.
7867       if (Base->isEmpty() || CGF.getContext()
7868                                  .getASTRecordLayout(Base)
7869                                  .getNonVirtualSize()
7870                                  .isZero())
7871         continue;
7872 
7873       unsigned FieldIndex = RL.getNonVirtualBaseLLVMFieldNo(Base);
7874       RecordLayout[FieldIndex] = Base;
7875     }
7876     // Fill in virtual bases.
7877     for (const auto &I : RD->vbases()) {
7878       const auto *Base = I.getType()->getAsCXXRecordDecl();
7879       // Ignore empty bases.
7880       if (Base->isEmpty())
7881         continue;
7882       unsigned FieldIndex = RL.getVirtualBaseIndex(Base);
7883       if (RecordLayout[FieldIndex])
7884         continue;
7885       RecordLayout[FieldIndex] = Base;
7886     }
7887     // Fill in all the fields.
7888     assert(!RD->isUnion() && "Unexpected union.");
7889     for (const auto *Field : RD->fields()) {
7890       // Fill in non-bitfields. (Bitfields always use a zero pattern, which we
7891       // will fill in later.)
7892       if (!Field->isBitField() && !Field->isZeroSize(CGF.getContext())) {
7893         unsigned FieldIndex = RL.getLLVMFieldNo(Field);
7894         RecordLayout[FieldIndex] = Field;
7895       }
7896     }
7897     for (const llvm::PointerUnion<const CXXRecordDecl *, const FieldDecl *>
7898              &Data : RecordLayout) {
7899       if (Data.isNull())
7900         continue;
7901       if (const auto *Base = Data.dyn_cast<const CXXRecordDecl *>())
7902         getPlainLayout(Base, Layout, /*AsBase=*/true);
7903       else
7904         Layout.push_back(Data.get<const FieldDecl *>());
7905     }
7906   }
7907 
7908 public:
7909   MappableExprsHandler(const OMPExecutableDirective &Dir, CodeGenFunction &CGF)
7910       : CurDir(&Dir), CGF(CGF) {
7911     // Extract firstprivate clause information.
7912     for (const auto *C : Dir.getClausesOfKind<OMPFirstprivateClause>())
7913       for (const auto *D : C->varlists())
7914         FirstPrivateDecls.try_emplace(
7915             cast<VarDecl>(cast<DeclRefExpr>(D)->getDecl()), C->isImplicit());
7916     // Extract implicit firstprivates from uses_allocators clauses.
7917     for (const auto *C : Dir.getClausesOfKind<OMPUsesAllocatorsClause>()) {
7918       for (unsigned I = 0, E = C->getNumberOfAllocators(); I < E; ++I) {
7919         OMPUsesAllocatorsClause::Data D = C->getAllocatorData(I);
7920         if (const auto *DRE = dyn_cast_or_null<DeclRefExpr>(D.AllocatorTraits))
7921           FirstPrivateDecls.try_emplace(cast<VarDecl>(DRE->getDecl()),
7922                                         /*Implicit=*/true);
7923         else if (const auto *VD = dyn_cast<VarDecl>(
7924                      cast<DeclRefExpr>(D.Allocator->IgnoreParenImpCasts())
7925                          ->getDecl()))
7926           FirstPrivateDecls.try_emplace(VD, /*Implicit=*/true);
7927       }
7928     }
7929     // Extract device pointer clause information.
7930     for (const auto *C : Dir.getClausesOfKind<OMPIsDevicePtrClause>())
7931       for (auto L : C->component_lists())
7932         DevPointersMap[std::get<0>(L)].push_back(std::get<1>(L));
7933   }
7934 
7935   /// Constructor for the declare mapper directive.
7936   MappableExprsHandler(const OMPDeclareMapperDecl &Dir, CodeGenFunction &CGF)
7937       : CurDir(&Dir), CGF(CGF) {}
7938 
7939   /// Generate code for the combined entry if we have a partially mapped struct
7940   /// and take care of the mapping flags of the arguments corresponding to
7941   /// individual struct members.
7942   void emitCombinedEntry(MapCombinedInfoTy &CombinedInfo,
7943                          MapFlagsArrayTy &CurTypes,
7944                          const StructRangeInfoTy &PartialStruct) const {
7945     // Base is the base of the struct
7946     CombinedInfo.BasePointers.push_back(PartialStruct.Base.getPointer());
7947     // Pointer is the address of the lowest element
7948     llvm::Value *LB = PartialStruct.LowestElem.second.getPointer();
7949     CombinedInfo.Pointers.push_back(LB);
7950     // There should not be a mapper for a combined entry.
7951     CombinedInfo.Mappers.push_back(nullptr);
7952     // Size is (addr of {highest+1} element) - (addr of lowest element)
7953     llvm::Value *HB = PartialStruct.HighestElem.second.getPointer();
7954     llvm::Value *HAddr = CGF.Builder.CreateConstGEP1_32(HB, /*Idx0=*/1);
7955     llvm::Value *CLAddr = CGF.Builder.CreatePointerCast(LB, CGF.VoidPtrTy);
7956     llvm::Value *CHAddr = CGF.Builder.CreatePointerCast(HAddr, CGF.VoidPtrTy);
7957     llvm::Value *Diff = CGF.Builder.CreatePtrDiff(CHAddr, CLAddr);
7958     llvm::Value *Size = CGF.Builder.CreateIntCast(Diff, CGF.Int64Ty,
7959                                                   /*isSigned=*/false);
7960     CombinedInfo.Sizes.push_back(Size);
7961     // Map type is always TARGET_PARAM
7962     CombinedInfo.Types.push_back(OMP_MAP_TARGET_PARAM);
7963     // Remove TARGET_PARAM flag from the first element
7964     (*CurTypes.begin()) &= ~OMP_MAP_TARGET_PARAM;
7965 
7966     // All other current entries will be MEMBER_OF the combined entry
7967     // (except for PTR_AND_OBJ entries which do not have a placeholder value
7968     // 0xFFFF in the MEMBER_OF field).
7969     OpenMPOffloadMappingFlags MemberOfFlag =
7970         getMemberOfFlag(CombinedInfo.BasePointers.size() - 1);
7971     for (auto &M : CurTypes)
7972       setCorrectMemberOfFlag(M, MemberOfFlag);
7973   }
7974 
7975   /// Generate all the base pointers, section pointers, sizes, map types, and
7976   /// mappers for the extracted mappable expressions (all included in \a
7977   /// CombinedInfo). Also, for each item that relates with a device pointer, a
7978   /// pair of the relevant declaration and index where it occurs is appended to
7979   /// the device pointers info array.
7980   void generateAllInfo(
7981       MapCombinedInfoTy &CombinedInfo,
7982       const llvm::DenseSet<CanonicalDeclPtr<const Decl>> &SkipVarSet =
7983           llvm::DenseSet<CanonicalDeclPtr<const Decl>>()) const {
7984     // We have to process the component lists that relate with the same
7985     // declaration in a single chunk so that we can generate the map flags
7986     // correctly. Therefore, we organize all lists in a map.
7987     llvm::MapVector<const ValueDecl *, SmallVector<MapInfo, 8>> Info;
7988 
7989     // Helper function to fill the information map for the different supported
7990     // clauses.
7991     auto &&InfoGen =
7992         [&Info, &SkipVarSet](
7993             const ValueDecl *D,
7994             OMPClauseMappableExprCommon::MappableExprComponentListRef L,
7995             OpenMPMapClauseKind MapType,
7996             ArrayRef<OpenMPMapModifierKind> MapModifiers,
7997             bool ReturnDevicePointer, bool IsImplicit, const ValueDecl *Mapper,
7998             bool ForDeviceAddr = false) {
7999           const ValueDecl *VD =
8000               D ? cast<ValueDecl>(D->getCanonicalDecl()) : nullptr;
8001           if (SkipVarSet.count(VD))
8002             return;
8003           Info[VD].emplace_back(L, MapType, MapModifiers, ReturnDevicePointer,
8004                                 IsImplicit, Mapper, ForDeviceAddr);
8005         };
8006 
8007     assert(CurDir.is<const OMPExecutableDirective *>() &&
8008            "Expect a executable directive");
8009     const auto *CurExecDir = CurDir.get<const OMPExecutableDirective *>();
8010     for (const auto *C : CurExecDir->getClausesOfKind<OMPMapClause>())
8011       for (const auto L : C->component_lists()) {
8012         InfoGen(std::get<0>(L), std::get<1>(L), C->getMapType(),
8013                 C->getMapTypeModifiers(), /*ReturnDevicePointer=*/false,
8014                 C->isImplicit(), std::get<2>(L));
8015       }
8016     for (const auto *C : CurExecDir->getClausesOfKind<OMPToClause>())
8017       for (const auto L : C->component_lists()) {
8018         InfoGen(std::get<0>(L), std::get<1>(L), OMPC_MAP_to, llvm::None,
8019                 /*ReturnDevicePointer=*/false, C->isImplicit(), std::get<2>(L));
8020       }
8021     for (const auto *C : CurExecDir->getClausesOfKind<OMPFromClause>())
8022       for (const auto L : C->component_lists()) {
8023         InfoGen(std::get<0>(L), std::get<1>(L), OMPC_MAP_from, llvm::None,
8024                 /*ReturnDevicePointer=*/false, C->isImplicit(), std::get<2>(L));
8025       }
8026 
8027     // Look at the use_device_ptr clause information and mark the existing map
8028     // entries as such. If there is no map information for an entry in the
8029     // use_device_ptr list, we create one with map type 'alloc' and zero size
8030     // section. It is the user fault if that was not mapped before. If there is
8031     // no map information and the pointer is a struct member, then we defer the
8032     // emission of that entry until the whole struct has been processed.
8033     llvm::MapVector<const ValueDecl *, SmallVector<DeferredDevicePtrEntryTy, 4>>
8034         DeferredInfo;
8035 
8036     for (const auto *C :
8037          CurExecDir->getClausesOfKind<OMPUseDevicePtrClause>()) {
8038       for (const auto L : C->component_lists()) {
8039         OMPClauseMappableExprCommon::MappableExprComponentListRef Components =
8040             std::get<1>(L);
8041         assert(!Components.empty() &&
8042                "Not expecting empty list of components!");
8043         const ValueDecl *VD = Components.back().getAssociatedDeclaration();
8044         VD = cast<ValueDecl>(VD->getCanonicalDecl());
8045         const Expr *IE = Components.back().getAssociatedExpression();
8046         // If the first component is a member expression, we have to look into
8047         // 'this', which maps to null in the map of map information. Otherwise
8048         // look directly for the information.
8049         auto It = Info.find(isa<MemberExpr>(IE) ? nullptr : VD);
8050 
8051         // We potentially have map information for this declaration already.
8052         // Look for the first set of components that refer to it.
8053         if (It != Info.end()) {
8054           auto CI = std::find_if(
8055               It->second.begin(), It->second.end(), [VD](const MapInfo &MI) {
8056                 return MI.Components.back().getAssociatedDeclaration() == VD;
8057               });
8058           // If we found a map entry, signal that the pointer has to be returned
8059           // and move on to the next declaration.
8060           if (CI != It->second.end()) {
8061             CI->ReturnDevicePointer = true;
8062             continue;
8063           }
8064         }
8065 
8066         // We didn't find any match in our map information - generate a zero
8067         // size array section - if the pointer is a struct member we defer this
8068         // action until the whole struct has been processed.
8069         if (isa<MemberExpr>(IE)) {
8070           // Insert the pointer into Info to be processed by
8071           // generateInfoForComponentList. Because it is a member pointer
8072           // without a pointee, no entry will be generated for it, therefore
8073           // we need to generate one after the whole struct has been processed.
8074           // Nonetheless, generateInfoForComponentList must be called to take
8075           // the pointer into account for the calculation of the range of the
8076           // partial struct.
8077           InfoGen(nullptr, Components, OMPC_MAP_unknown, llvm::None,
8078                   /*ReturnDevicePointer=*/false, C->isImplicit(), nullptr);
8079           DeferredInfo[nullptr].emplace_back(IE, VD, /*ForDeviceAddr=*/false);
8080         } else {
8081           llvm::Value *Ptr =
8082               CGF.EmitLoadOfScalar(CGF.EmitLValue(IE), IE->getExprLoc());
8083           CombinedInfo.BasePointers.emplace_back(Ptr, VD);
8084           CombinedInfo.Pointers.push_back(Ptr);
8085           CombinedInfo.Sizes.push_back(
8086               llvm::Constant::getNullValue(CGF.Int64Ty));
8087           CombinedInfo.Types.push_back(OMP_MAP_RETURN_PARAM |
8088                                        OMP_MAP_TARGET_PARAM);
8089           CombinedInfo.Mappers.push_back(nullptr);
8090         }
8091       }
8092     }
8093 
8094     // Look at the use_device_addr clause information and mark the existing map
8095     // entries as such. If there is no map information for an entry in the
8096     // use_device_addr list, we create one with map type 'alloc' and zero size
8097     // section. It is the user fault if that was not mapped before. If there is
8098     // no map information and the pointer is a struct member, then we defer the
8099     // emission of that entry until the whole struct has been processed.
8100     llvm::SmallDenseSet<CanonicalDeclPtr<const Decl>, 4> Processed;
8101     for (const auto *C :
8102          CurExecDir->getClausesOfKind<OMPUseDeviceAddrClause>()) {
8103       for (const auto L : C->component_lists()) {
8104         assert(!std::get<1>(L).empty() &&
8105                "Not expecting empty list of components!");
8106         const ValueDecl *VD = std::get<1>(L).back().getAssociatedDeclaration();
8107         if (!Processed.insert(VD).second)
8108           continue;
8109         VD = cast<ValueDecl>(VD->getCanonicalDecl());
8110         const Expr *IE = std::get<1>(L).back().getAssociatedExpression();
8111         // If the first component is a member expression, we have to look into
8112         // 'this', which maps to null in the map of map information. Otherwise
8113         // look directly for the information.
8114         auto It = Info.find(isa<MemberExpr>(IE) ? nullptr : VD);
8115 
8116         // We potentially have map information for this declaration already.
8117         // Look for the first set of components that refer to it.
8118         if (It != Info.end()) {
8119           auto *CI = llvm::find_if(It->second, [VD](const MapInfo &MI) {
8120             return MI.Components.back().getAssociatedDeclaration() == VD;
8121           });
8122           // If we found a map entry, signal that the pointer has to be returned
8123           // and move on to the next declaration.
8124           if (CI != It->second.end()) {
8125             CI->ReturnDevicePointer = true;
8126             continue;
8127           }
8128         }
8129 
8130         // We didn't find any match in our map information - generate a zero
8131         // size array section - if the pointer is a struct member we defer this
8132         // action until the whole struct has been processed.
8133         if (isa<MemberExpr>(IE)) {
8134           // Insert the pointer into Info to be processed by
8135           // generateInfoForComponentList. Because it is a member pointer
8136           // without a pointee, no entry will be generated for it, therefore
8137           // we need to generate one after the whole struct has been processed.
8138           // Nonetheless, generateInfoForComponentList must be called to take
8139           // the pointer into account for the calculation of the range of the
8140           // partial struct.
8141           InfoGen(nullptr, std::get<1>(L), OMPC_MAP_unknown, llvm::None,
8142                   /*ReturnDevicePointer=*/false, C->isImplicit(), nullptr,
8143                   /*ForDeviceAddr=*/true);
8144           DeferredInfo[nullptr].emplace_back(IE, VD, /*ForDeviceAddr=*/true);
8145         } else {
8146           llvm::Value *Ptr;
8147           if (IE->isGLValue())
8148             Ptr = CGF.EmitLValue(IE).getPointer(CGF);
8149           else
8150             Ptr = CGF.EmitScalarExpr(IE);
8151           CombinedInfo.BasePointers.emplace_back(Ptr, VD);
8152           CombinedInfo.Pointers.push_back(Ptr);
8153           CombinedInfo.Sizes.push_back(llvm::Constant::getNullValue(CGF.Int64Ty));
8154           CombinedInfo.Types.push_back(OMP_MAP_RETURN_PARAM | OMP_MAP_TARGET_PARAM);
8155           CombinedInfo.Mappers.push_back(nullptr);
8156         }
8157       }
8158     }
8159 
8160     for (const auto &M : Info) {
8161       // We need to know when we generate information for the first component
8162       // associated with a capture, because the mapping flags depend on it.
8163       bool IsFirstComponentList = true;
8164 
8165       // Temporary generated information.
8166       MapCombinedInfoTy CurInfo;
8167       StructRangeInfoTy PartialStruct;
8168 
8169       for (const MapInfo &L : M.second) {
8170         assert(!L.Components.empty() &&
8171                "Not expecting declaration with no component lists.");
8172 
8173         // Remember the current base pointer index.
8174         unsigned CurrentBasePointersIdx = CurInfo.BasePointers.size();
8175         generateInfoForComponentList(
8176             L.MapType, L.MapModifiers, L.Components, CurInfo, PartialStruct,
8177             IsFirstComponentList, L.IsImplicit, L.Mapper, L.ForDeviceAddr);
8178 
8179         // If this entry relates with a device pointer, set the relevant
8180         // declaration and add the 'return pointer' flag.
8181         if (L.ReturnDevicePointer) {
8182           assert(CurInfo.BasePointers.size() > CurrentBasePointersIdx &&
8183                  "Unexpected number of mapped base pointers.");
8184 
8185           const ValueDecl *RelevantVD =
8186               L.Components.back().getAssociatedDeclaration();
8187           assert(RelevantVD &&
8188                  "No relevant declaration related with device pointer??");
8189 
8190           CurInfo.BasePointers[CurrentBasePointersIdx].setDevicePtrDecl(
8191               RelevantVD);
8192           CurInfo.Types[CurrentBasePointersIdx] |= OMP_MAP_RETURN_PARAM;
8193         }
8194         IsFirstComponentList = false;
8195       }
8196 
8197       // Append any pending zero-length pointers which are struct members and
8198       // used with use_device_ptr or use_device_addr.
8199       auto CI = DeferredInfo.find(M.first);
8200       if (CI != DeferredInfo.end()) {
8201         for (const DeferredDevicePtrEntryTy &L : CI->second) {
8202           llvm::Value *BasePtr;
8203           llvm::Value *Ptr;
8204           if (L.ForDeviceAddr) {
8205             if (L.IE->isGLValue())
8206               Ptr = this->CGF.EmitLValue(L.IE).getPointer(CGF);
8207             else
8208               Ptr = this->CGF.EmitScalarExpr(L.IE);
8209             BasePtr = Ptr;
8210             // Entry is RETURN_PARAM. Also, set the placeholder value
8211             // MEMBER_OF=FFFF so that the entry is later updated with the
8212             // correct value of MEMBER_OF.
8213             CurInfo.Types.push_back(OMP_MAP_RETURN_PARAM | OMP_MAP_MEMBER_OF);
8214           } else {
8215             BasePtr = this->CGF.EmitLValue(L.IE).getPointer(CGF);
8216             Ptr = this->CGF.EmitLoadOfScalar(this->CGF.EmitLValue(L.IE),
8217                                              L.IE->getExprLoc());
8218             // Entry is PTR_AND_OBJ and RETURN_PARAM. Also, set the placeholder
8219             // value MEMBER_OF=FFFF so that the entry is later updated with the
8220             // correct value of MEMBER_OF.
8221             CurInfo.Types.push_back(OMP_MAP_PTR_AND_OBJ | OMP_MAP_RETURN_PARAM |
8222                                     OMP_MAP_MEMBER_OF);
8223           }
8224           CurInfo.BasePointers.emplace_back(BasePtr, L.VD);
8225           CurInfo.Pointers.push_back(Ptr);
8226           CurInfo.Sizes.push_back(
8227               llvm::Constant::getNullValue(this->CGF.Int64Ty));
8228           CurInfo.Mappers.push_back(nullptr);
8229         }
8230       }
8231 
8232       // If there is an entry in PartialStruct it means we have a struct with
8233       // individual members mapped. Emit an extra combined entry.
8234       if (PartialStruct.Base.isValid())
8235         emitCombinedEntry(CombinedInfo, CurInfo.Types, PartialStruct);
8236 
8237       // We need to append the results of this capture to what we already have.
8238       CombinedInfo.append(CurInfo);
8239     }
8240   }
8241 
8242   /// Generate all the base pointers, section pointers, sizes, map types, and
8243   /// mappers for the extracted map clauses of user-defined mapper (all included
8244   /// in \a CombinedInfo).
8245   void generateAllInfoForMapper(MapCombinedInfoTy &CombinedInfo) const {
8246     assert(CurDir.is<const OMPDeclareMapperDecl *>() &&
8247            "Expect a declare mapper directive");
8248     const auto *CurMapperDir = CurDir.get<const OMPDeclareMapperDecl *>();
8249     // We have to process the component lists that relate with the same
8250     // declaration in a single chunk so that we can generate the map flags
8251     // correctly. Therefore, we organize all lists in a map.
8252     llvm::MapVector<const ValueDecl *, SmallVector<MapInfo, 8>> Info;
8253 
8254     // Fill the information map for map clauses.
8255     for (const auto *C : CurMapperDir->clauselists()) {
8256       const auto *MC = cast<OMPMapClause>(C);
8257       for (const auto L : MC->component_lists()) {
8258         const ValueDecl *VD =
8259             std::get<0>(L) ? cast<ValueDecl>(std::get<0>(L)->getCanonicalDecl())
8260                            : nullptr;
8261         // Get the corresponding user-defined mapper.
8262         Info[VD].emplace_back(
8263             std::get<1>(L), MC->getMapType(), MC->getMapTypeModifiers(),
8264             /*ReturnDevicePointer=*/false, MC->isImplicit(), std::get<2>(L));
8265       }
8266     }
8267 
8268     for (const auto &M : Info) {
8269       // We need to know when we generate information for the first component
8270       // associated with a capture, because the mapping flags depend on it.
8271       bool IsFirstComponentList = true;
8272 
8273       // Temporary generated information.
8274       MapCombinedInfoTy CurInfo;
8275       StructRangeInfoTy PartialStruct;
8276 
8277       for (const MapInfo &L : M.second) {
8278         assert(!L.Components.empty() &&
8279                "Not expecting declaration with no component lists.");
8280         generateInfoForComponentList(
8281             L.MapType, L.MapModifiers, L.Components, CurInfo, PartialStruct,
8282             IsFirstComponentList, L.IsImplicit, L.Mapper, L.ForDeviceAddr);
8283         IsFirstComponentList = false;
8284       }
8285 
8286       // If there is an entry in PartialStruct it means we have a struct with
8287       // individual members mapped. Emit an extra combined entry.
8288       if (PartialStruct.Base.isValid())
8289         emitCombinedEntry(CombinedInfo, CurInfo.Types, PartialStruct);
8290 
8291       // We need to append the results of this capture to what we already have.
8292       CombinedInfo.append(CurInfo);
8293     }
8294   }
8295 
8296   /// Emit capture info for lambdas for variables captured by reference.
8297   void generateInfoForLambdaCaptures(
8298       const ValueDecl *VD, llvm::Value *Arg, MapCombinedInfoTy &CombinedInfo,
8299       llvm::DenseMap<llvm::Value *, llvm::Value *> &LambdaPointers) const {
8300     const auto *RD = VD->getType()
8301                          .getCanonicalType()
8302                          .getNonReferenceType()
8303                          ->getAsCXXRecordDecl();
8304     if (!RD || !RD->isLambda())
8305       return;
8306     Address VDAddr = Address(Arg, CGF.getContext().getDeclAlign(VD));
8307     LValue VDLVal = CGF.MakeAddrLValue(
8308         VDAddr, VD->getType().getCanonicalType().getNonReferenceType());
8309     llvm::DenseMap<const VarDecl *, FieldDecl *> Captures;
8310     FieldDecl *ThisCapture = nullptr;
8311     RD->getCaptureFields(Captures, ThisCapture);
8312     if (ThisCapture) {
8313       LValue ThisLVal =
8314           CGF.EmitLValueForFieldInitialization(VDLVal, ThisCapture);
8315       LValue ThisLValVal = CGF.EmitLValueForField(VDLVal, ThisCapture);
8316       LambdaPointers.try_emplace(ThisLVal.getPointer(CGF),
8317                                  VDLVal.getPointer(CGF));
8318       CombinedInfo.BasePointers.push_back(ThisLVal.getPointer(CGF));
8319       CombinedInfo.Pointers.push_back(ThisLValVal.getPointer(CGF));
8320       CombinedInfo.Sizes.push_back(
8321           CGF.Builder.CreateIntCast(CGF.getTypeSize(CGF.getContext().VoidPtrTy),
8322                                     CGF.Int64Ty, /*isSigned=*/true));
8323       CombinedInfo.Types.push_back(OMP_MAP_PTR_AND_OBJ | OMP_MAP_LITERAL |
8324                                    OMP_MAP_MEMBER_OF | OMP_MAP_IMPLICIT);
8325       CombinedInfo.Mappers.push_back(nullptr);
8326     }
8327     for (const LambdaCapture &LC : RD->captures()) {
8328       if (!LC.capturesVariable())
8329         continue;
8330       const VarDecl *VD = LC.getCapturedVar();
8331       if (LC.getCaptureKind() != LCK_ByRef && !VD->getType()->isPointerType())
8332         continue;
8333       auto It = Captures.find(VD);
8334       assert(It != Captures.end() && "Found lambda capture without field.");
8335       LValue VarLVal = CGF.EmitLValueForFieldInitialization(VDLVal, It->second);
8336       if (LC.getCaptureKind() == LCK_ByRef) {
8337         LValue VarLValVal = CGF.EmitLValueForField(VDLVal, It->second);
8338         LambdaPointers.try_emplace(VarLVal.getPointer(CGF),
8339                                    VDLVal.getPointer(CGF));
8340         CombinedInfo.BasePointers.push_back(VarLVal.getPointer(CGF));
8341         CombinedInfo.Pointers.push_back(VarLValVal.getPointer(CGF));
8342         CombinedInfo.Sizes.push_back(CGF.Builder.CreateIntCast(
8343             CGF.getTypeSize(
8344                 VD->getType().getCanonicalType().getNonReferenceType()),
8345             CGF.Int64Ty, /*isSigned=*/true));
8346       } else {
8347         RValue VarRVal = CGF.EmitLoadOfLValue(VarLVal, RD->getLocation());
8348         LambdaPointers.try_emplace(VarLVal.getPointer(CGF),
8349                                    VDLVal.getPointer(CGF));
8350         CombinedInfo.BasePointers.push_back(VarLVal.getPointer(CGF));
8351         CombinedInfo.Pointers.push_back(VarRVal.getScalarVal());
8352         CombinedInfo.Sizes.push_back(llvm::ConstantInt::get(CGF.Int64Ty, 0));
8353       }
8354       CombinedInfo.Types.push_back(OMP_MAP_PTR_AND_OBJ | OMP_MAP_LITERAL |
8355                                    OMP_MAP_MEMBER_OF | OMP_MAP_IMPLICIT);
8356       CombinedInfo.Mappers.push_back(nullptr);
8357     }
8358   }
8359 
8360   /// Set correct indices for lambdas captures.
8361   void adjustMemberOfForLambdaCaptures(
8362       const llvm::DenseMap<llvm::Value *, llvm::Value *> &LambdaPointers,
8363       MapBaseValuesArrayTy &BasePointers, MapValuesArrayTy &Pointers,
8364       MapFlagsArrayTy &Types) const {
8365     for (unsigned I = 0, E = Types.size(); I < E; ++I) {
8366       // Set correct member_of idx for all implicit lambda captures.
8367       if (Types[I] != (OMP_MAP_PTR_AND_OBJ | OMP_MAP_LITERAL |
8368                        OMP_MAP_MEMBER_OF | OMP_MAP_IMPLICIT))
8369         continue;
8370       llvm::Value *BasePtr = LambdaPointers.lookup(*BasePointers[I]);
8371       assert(BasePtr && "Unable to find base lambda address.");
8372       int TgtIdx = -1;
8373       for (unsigned J = I; J > 0; --J) {
8374         unsigned Idx = J - 1;
8375         if (Pointers[Idx] != BasePtr)
8376           continue;
8377         TgtIdx = Idx;
8378         break;
8379       }
8380       assert(TgtIdx != -1 && "Unable to find parent lambda.");
8381       // All other current entries will be MEMBER_OF the combined entry
8382       // (except for PTR_AND_OBJ entries which do not have a placeholder value
8383       // 0xFFFF in the MEMBER_OF field).
8384       OpenMPOffloadMappingFlags MemberOfFlag = getMemberOfFlag(TgtIdx);
8385       setCorrectMemberOfFlag(Types[I], MemberOfFlag);
8386     }
8387   }
8388 
8389   /// Generate the base pointers, section pointers, sizes, map types, and
8390   /// mappers associated to a given capture (all included in \a CombinedInfo).
8391   void generateInfoForCapture(const CapturedStmt::Capture *Cap,
8392                               llvm::Value *Arg, MapCombinedInfoTy &CombinedInfo,
8393                               StructRangeInfoTy &PartialStruct) const {
8394     assert(!Cap->capturesVariableArrayType() &&
8395            "Not expecting to generate map info for a variable array type!");
8396 
8397     // We need to know when we generating information for the first component
8398     const ValueDecl *VD = Cap->capturesThis()
8399                               ? nullptr
8400                               : Cap->getCapturedVar()->getCanonicalDecl();
8401 
8402     // If this declaration appears in a is_device_ptr clause we just have to
8403     // pass the pointer by value. If it is a reference to a declaration, we just
8404     // pass its value.
8405     if (DevPointersMap.count(VD)) {
8406       CombinedInfo.BasePointers.emplace_back(Arg, VD);
8407       CombinedInfo.Pointers.push_back(Arg);
8408       CombinedInfo.Sizes.push_back(
8409           CGF.Builder.CreateIntCast(CGF.getTypeSize(CGF.getContext().VoidPtrTy),
8410                                     CGF.Int64Ty, /*isSigned=*/true));
8411       CombinedInfo.Types.push_back(OMP_MAP_LITERAL | OMP_MAP_TARGET_PARAM);
8412       CombinedInfo.Mappers.push_back(nullptr);
8413       return;
8414     }
8415 
8416     using MapData =
8417         std::tuple<OMPClauseMappableExprCommon::MappableExprComponentListRef,
8418                    OpenMPMapClauseKind, ArrayRef<OpenMPMapModifierKind>, bool,
8419                    const ValueDecl *>;
8420     SmallVector<MapData, 4> DeclComponentLists;
8421     assert(CurDir.is<const OMPExecutableDirective *>() &&
8422            "Expect a executable directive");
8423     const auto *CurExecDir = CurDir.get<const OMPExecutableDirective *>();
8424     for (const auto *C : CurExecDir->getClausesOfKind<OMPMapClause>()) {
8425       for (const auto L : C->decl_component_lists(VD)) {
8426         const ValueDecl *VDecl, *Mapper;
8427         OMPClauseMappableExprCommon::MappableExprComponentListRef Components;
8428         std::tie(VDecl, Components, Mapper) = L;
8429         assert(VDecl == VD && "We got information for the wrong declaration??");
8430         assert(!Components.empty() &&
8431                "Not expecting declaration with no component lists.");
8432         DeclComponentLists.emplace_back(Components, C->getMapType(),
8433                                         C->getMapTypeModifiers(),
8434                                         C->isImplicit(), Mapper);
8435       }
8436     }
8437 
8438     // Find overlapping elements (including the offset from the base element).
8439     llvm::SmallDenseMap<
8440         const MapData *,
8441         llvm::SmallVector<
8442             OMPClauseMappableExprCommon::MappableExprComponentListRef, 4>,
8443         4>
8444         OverlappedData;
8445     size_t Count = 0;
8446     for (const MapData &L : DeclComponentLists) {
8447       OMPClauseMappableExprCommon::MappableExprComponentListRef Components;
8448       OpenMPMapClauseKind MapType;
8449       ArrayRef<OpenMPMapModifierKind> MapModifiers;
8450       bool IsImplicit;
8451       const ValueDecl *Mapper;
8452       std::tie(Components, MapType, MapModifiers, IsImplicit, Mapper) = L;
8453       ++Count;
8454       for (const MapData &L1 : makeArrayRef(DeclComponentLists).slice(Count)) {
8455         OMPClauseMappableExprCommon::MappableExprComponentListRef Components1;
8456         std::tie(Components1, MapType, MapModifiers, IsImplicit, Mapper) = L1;
8457         auto CI = Components.rbegin();
8458         auto CE = Components.rend();
8459         auto SI = Components1.rbegin();
8460         auto SE = Components1.rend();
8461         for (; CI != CE && SI != SE; ++CI, ++SI) {
8462           if (CI->getAssociatedExpression()->getStmtClass() !=
8463               SI->getAssociatedExpression()->getStmtClass())
8464             break;
8465           // Are we dealing with different variables/fields?
8466           if (CI->getAssociatedDeclaration() != SI->getAssociatedDeclaration())
8467             break;
8468         }
8469         // Found overlapping if, at least for one component, reached the head of
8470         // the components list.
8471         if (CI == CE || SI == SE) {
8472           assert((CI != CE || SI != SE) &&
8473                  "Unexpected full match of the mapping components.");
8474           const MapData &BaseData = CI == CE ? L : L1;
8475           OMPClauseMappableExprCommon::MappableExprComponentListRef SubData =
8476               SI == SE ? Components : Components1;
8477           auto &OverlappedElements = OverlappedData.FindAndConstruct(&BaseData);
8478           OverlappedElements.getSecond().push_back(SubData);
8479         }
8480       }
8481     }
8482     // Sort the overlapped elements for each item.
8483     llvm::SmallVector<const FieldDecl *, 4> Layout;
8484     if (!OverlappedData.empty()) {
8485       if (const auto *CRD =
8486               VD->getType().getCanonicalType()->getAsCXXRecordDecl())
8487         getPlainLayout(CRD, Layout, /*AsBase=*/false);
8488       else {
8489         const auto *RD = VD->getType().getCanonicalType()->getAsRecordDecl();
8490         Layout.append(RD->field_begin(), RD->field_end());
8491       }
8492     }
8493     for (auto &Pair : OverlappedData) {
8494       llvm::sort(
8495           Pair.getSecond(),
8496           [&Layout](
8497               OMPClauseMappableExprCommon::MappableExprComponentListRef First,
8498               OMPClauseMappableExprCommon::MappableExprComponentListRef
8499                   Second) {
8500             auto CI = First.rbegin();
8501             auto CE = First.rend();
8502             auto SI = Second.rbegin();
8503             auto SE = Second.rend();
8504             for (; CI != CE && SI != SE; ++CI, ++SI) {
8505               if (CI->getAssociatedExpression()->getStmtClass() !=
8506                   SI->getAssociatedExpression()->getStmtClass())
8507                 break;
8508               // Are we dealing with different variables/fields?
8509               if (CI->getAssociatedDeclaration() !=
8510                   SI->getAssociatedDeclaration())
8511                 break;
8512             }
8513 
8514             // Lists contain the same elements.
8515             if (CI == CE && SI == SE)
8516               return false;
8517 
8518             // List with less elements is less than list with more elements.
8519             if (CI == CE || SI == SE)
8520               return CI == CE;
8521 
8522             const auto *FD1 = cast<FieldDecl>(CI->getAssociatedDeclaration());
8523             const auto *FD2 = cast<FieldDecl>(SI->getAssociatedDeclaration());
8524             if (FD1->getParent() == FD2->getParent())
8525               return FD1->getFieldIndex() < FD2->getFieldIndex();
8526             const auto It =
8527                 llvm::find_if(Layout, [FD1, FD2](const FieldDecl *FD) {
8528                   return FD == FD1 || FD == FD2;
8529                 });
8530             return *It == FD1;
8531           });
8532     }
8533 
8534     // Associated with a capture, because the mapping flags depend on it.
8535     // Go through all of the elements with the overlapped elements.
8536     for (const auto &Pair : OverlappedData) {
8537       const MapData &L = *Pair.getFirst();
8538       OMPClauseMappableExprCommon::MappableExprComponentListRef Components;
8539       OpenMPMapClauseKind MapType;
8540       ArrayRef<OpenMPMapModifierKind> MapModifiers;
8541       bool IsImplicit;
8542       const ValueDecl *Mapper;
8543       std::tie(Components, MapType, MapModifiers, IsImplicit, Mapper) = L;
8544       ArrayRef<OMPClauseMappableExprCommon::MappableExprComponentListRef>
8545           OverlappedComponents = Pair.getSecond();
8546       bool IsFirstComponentList = true;
8547       generateInfoForComponentList(
8548           MapType, MapModifiers, Components, CombinedInfo, PartialStruct,
8549           IsFirstComponentList, IsImplicit, Mapper, /*ForDeviceAddr=*/false,
8550           OverlappedComponents);
8551     }
8552     // Go through other elements without overlapped elements.
8553     bool IsFirstComponentList = OverlappedData.empty();
8554     for (const MapData &L : DeclComponentLists) {
8555       OMPClauseMappableExprCommon::MappableExprComponentListRef Components;
8556       OpenMPMapClauseKind MapType;
8557       ArrayRef<OpenMPMapModifierKind> MapModifiers;
8558       bool IsImplicit;
8559       const ValueDecl *Mapper;
8560       std::tie(Components, MapType, MapModifiers, IsImplicit, Mapper) = L;
8561       auto It = OverlappedData.find(&L);
8562       if (It == OverlappedData.end())
8563         generateInfoForComponentList(MapType, MapModifiers, Components,
8564                                      CombinedInfo, PartialStruct,
8565                                      IsFirstComponentList, IsImplicit, Mapper);
8566       IsFirstComponentList = false;
8567     }
8568   }
8569 
8570   /// Generate the default map information for a given capture \a CI,
8571   /// record field declaration \a RI and captured value \a CV.
8572   void generateDefaultMapInfo(const CapturedStmt::Capture &CI,
8573                               const FieldDecl &RI, llvm::Value *CV,
8574                               MapCombinedInfoTy &CombinedInfo) const {
8575     bool IsImplicit = true;
8576     // Do the default mapping.
8577     if (CI.capturesThis()) {
8578       CombinedInfo.BasePointers.push_back(CV);
8579       CombinedInfo.Pointers.push_back(CV);
8580       const auto *PtrTy = cast<PointerType>(RI.getType().getTypePtr());
8581       CombinedInfo.Sizes.push_back(
8582           CGF.Builder.CreateIntCast(CGF.getTypeSize(PtrTy->getPointeeType()),
8583                                     CGF.Int64Ty, /*isSigned=*/true));
8584       // Default map type.
8585       CombinedInfo.Types.push_back(OMP_MAP_TO | OMP_MAP_FROM);
8586     } else if (CI.capturesVariableByCopy()) {
8587       CombinedInfo.BasePointers.push_back(CV);
8588       CombinedInfo.Pointers.push_back(CV);
8589       if (!RI.getType()->isAnyPointerType()) {
8590         // We have to signal to the runtime captures passed by value that are
8591         // not pointers.
8592         CombinedInfo.Types.push_back(OMP_MAP_LITERAL);
8593         CombinedInfo.Sizes.push_back(CGF.Builder.CreateIntCast(
8594             CGF.getTypeSize(RI.getType()), CGF.Int64Ty, /*isSigned=*/true));
8595       } else {
8596         // Pointers are implicitly mapped with a zero size and no flags
8597         // (other than first map that is added for all implicit maps).
8598         CombinedInfo.Types.push_back(OMP_MAP_NONE);
8599         CombinedInfo.Sizes.push_back(llvm::Constant::getNullValue(CGF.Int64Ty));
8600       }
8601       const VarDecl *VD = CI.getCapturedVar();
8602       auto I = FirstPrivateDecls.find(VD);
8603       if (I != FirstPrivateDecls.end())
8604         IsImplicit = I->getSecond();
8605     } else {
8606       assert(CI.capturesVariable() && "Expected captured reference.");
8607       const auto *PtrTy = cast<ReferenceType>(RI.getType().getTypePtr());
8608       QualType ElementType = PtrTy->getPointeeType();
8609       CombinedInfo.Sizes.push_back(CGF.Builder.CreateIntCast(
8610           CGF.getTypeSize(ElementType), CGF.Int64Ty, /*isSigned=*/true));
8611       // The default map type for a scalar/complex type is 'to' because by
8612       // default the value doesn't have to be retrieved. For an aggregate
8613       // type, the default is 'tofrom'.
8614       CombinedInfo.Types.push_back(getMapModifiersForPrivateClauses(CI));
8615       const VarDecl *VD = CI.getCapturedVar();
8616       auto I = FirstPrivateDecls.find(VD);
8617       if (I != FirstPrivateDecls.end() &&
8618           VD->getType().isConstant(CGF.getContext())) {
8619         llvm::Constant *Addr =
8620             CGF.CGM.getOpenMPRuntime().registerTargetFirstprivateCopy(CGF, VD);
8621         // Copy the value of the original variable to the new global copy.
8622         CGF.Builder.CreateMemCpy(
8623             CGF.MakeNaturalAlignAddrLValue(Addr, ElementType).getAddress(CGF),
8624             Address(CV, CGF.getContext().getTypeAlignInChars(ElementType)),
8625             CombinedInfo.Sizes.back(), /*IsVolatile=*/false);
8626         // Use new global variable as the base pointers.
8627         CombinedInfo.BasePointers.push_back(Addr);
8628         CombinedInfo.Pointers.push_back(Addr);
8629       } else {
8630         CombinedInfo.BasePointers.push_back(CV);
8631         if (I != FirstPrivateDecls.end() && ElementType->isAnyPointerType()) {
8632           Address PtrAddr = CGF.EmitLoadOfReference(CGF.MakeAddrLValue(
8633               CV, ElementType, CGF.getContext().getDeclAlign(VD),
8634               AlignmentSource::Decl));
8635           CombinedInfo.Pointers.push_back(PtrAddr.getPointer());
8636         } else {
8637           CombinedInfo.Pointers.push_back(CV);
8638         }
8639       }
8640       if (I != FirstPrivateDecls.end())
8641         IsImplicit = I->getSecond();
8642     }
8643     // Every default map produces a single argument which is a target parameter.
8644     CombinedInfo.Types.back() |= OMP_MAP_TARGET_PARAM;
8645 
8646     // Add flag stating this is an implicit map.
8647     if (IsImplicit)
8648       CombinedInfo.Types.back() |= OMP_MAP_IMPLICIT;
8649 
8650     // No user-defined mapper for default mapping.
8651     CombinedInfo.Mappers.push_back(nullptr);
8652   }
8653 };
8654 } // anonymous namespace
8655 
8656 /// Emit the arrays used to pass the captures and map information to the
8657 /// offloading runtime library. If there is no map or capture information,
8658 /// return nullptr by reference.
8659 static void
8660 emitOffloadingArrays(CodeGenFunction &CGF,
8661                      MappableExprsHandler::MapCombinedInfoTy &CombinedInfo,
8662                      CGOpenMPRuntime::TargetDataInfo &Info) {
8663   CodeGenModule &CGM = CGF.CGM;
8664   ASTContext &Ctx = CGF.getContext();
8665 
8666   // Reset the array information.
8667   Info.clearArrayInfo();
8668   Info.NumberOfPtrs = CombinedInfo.BasePointers.size();
8669 
8670   if (Info.NumberOfPtrs) {
8671     // Detect if we have any capture size requiring runtime evaluation of the
8672     // size so that a constant array could be eventually used.
8673     bool hasRuntimeEvaluationCaptureSize = false;
8674     for (llvm::Value *S : CombinedInfo.Sizes)
8675       if (!isa<llvm::Constant>(S)) {
8676         hasRuntimeEvaluationCaptureSize = true;
8677         break;
8678       }
8679 
8680     llvm::APInt PointerNumAP(32, Info.NumberOfPtrs, /*isSigned=*/true);
8681     QualType PointerArrayType = Ctx.getConstantArrayType(
8682         Ctx.VoidPtrTy, PointerNumAP, nullptr, ArrayType::Normal,
8683         /*IndexTypeQuals=*/0);
8684 
8685     Info.BasePointersArray =
8686         CGF.CreateMemTemp(PointerArrayType, ".offload_baseptrs").getPointer();
8687     Info.PointersArray =
8688         CGF.CreateMemTemp(PointerArrayType, ".offload_ptrs").getPointer();
8689     Address MappersArray =
8690         CGF.CreateMemTemp(PointerArrayType, ".offload_mappers");
8691     Info.MappersArray = MappersArray.getPointer();
8692 
8693     // If we don't have any VLA types or other types that require runtime
8694     // evaluation, we can use a constant array for the map sizes, otherwise we
8695     // need to fill up the arrays as we do for the pointers.
8696     QualType Int64Ty =
8697         Ctx.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1);
8698     if (hasRuntimeEvaluationCaptureSize) {
8699       QualType SizeArrayType = Ctx.getConstantArrayType(
8700           Int64Ty, PointerNumAP, nullptr, ArrayType::Normal,
8701           /*IndexTypeQuals=*/0);
8702       Info.SizesArray =
8703           CGF.CreateMemTemp(SizeArrayType, ".offload_sizes").getPointer();
8704     } else {
8705       // We expect all the sizes to be constant, so we collect them to create
8706       // a constant array.
8707       SmallVector<llvm::Constant *, 16> ConstSizes;
8708       for (llvm::Value *S : CombinedInfo.Sizes)
8709         ConstSizes.push_back(cast<llvm::Constant>(S));
8710 
8711       auto *SizesArrayInit = llvm::ConstantArray::get(
8712           llvm::ArrayType::get(CGM.Int64Ty, ConstSizes.size()), ConstSizes);
8713       std::string Name = CGM.getOpenMPRuntime().getName({"offload_sizes"});
8714       auto *SizesArrayGbl = new llvm::GlobalVariable(
8715           CGM.getModule(), SizesArrayInit->getType(),
8716           /*isConstant=*/true, llvm::GlobalValue::PrivateLinkage,
8717           SizesArrayInit, Name);
8718       SizesArrayGbl->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
8719       Info.SizesArray = SizesArrayGbl;
8720     }
8721 
8722     // The map types are always constant so we don't need to generate code to
8723     // fill arrays. Instead, we create an array constant.
8724     SmallVector<uint64_t, 4> Mapping(CombinedInfo.Types.size(), 0);
8725     llvm::copy(CombinedInfo.Types, Mapping.begin());
8726     llvm::Constant *MapTypesArrayInit =
8727         llvm::ConstantDataArray::get(CGF.Builder.getContext(), Mapping);
8728     std::string MaptypesName =
8729         CGM.getOpenMPRuntime().getName({"offload_maptypes"});
8730     auto *MapTypesArrayGbl = new llvm::GlobalVariable(
8731         CGM.getModule(), MapTypesArrayInit->getType(),
8732         /*isConstant=*/true, llvm::GlobalValue::PrivateLinkage,
8733         MapTypesArrayInit, MaptypesName);
8734     MapTypesArrayGbl->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
8735     Info.MapTypesArray = MapTypesArrayGbl;
8736 
8737     for (unsigned I = 0; I < Info.NumberOfPtrs; ++I) {
8738       llvm::Value *BPVal = *CombinedInfo.BasePointers[I];
8739       llvm::Value *BP = CGF.Builder.CreateConstInBoundsGEP2_32(
8740           llvm::ArrayType::get(CGM.VoidPtrTy, Info.NumberOfPtrs),
8741           Info.BasePointersArray, 0, I);
8742       BP = CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(
8743           BP, BPVal->getType()->getPointerTo(/*AddrSpace=*/0));
8744       Address BPAddr(BP, Ctx.getTypeAlignInChars(Ctx.VoidPtrTy));
8745       CGF.Builder.CreateStore(BPVal, BPAddr);
8746 
8747       if (Info.requiresDevicePointerInfo())
8748         if (const ValueDecl *DevVD =
8749                 CombinedInfo.BasePointers[I].getDevicePtrDecl())
8750           Info.CaptureDeviceAddrMap.try_emplace(DevVD, BPAddr);
8751 
8752       llvm::Value *PVal = CombinedInfo.Pointers[I];
8753       llvm::Value *P = CGF.Builder.CreateConstInBoundsGEP2_32(
8754           llvm::ArrayType::get(CGM.VoidPtrTy, Info.NumberOfPtrs),
8755           Info.PointersArray, 0, I);
8756       P = CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(
8757           P, PVal->getType()->getPointerTo(/*AddrSpace=*/0));
8758       Address PAddr(P, Ctx.getTypeAlignInChars(Ctx.VoidPtrTy));
8759       CGF.Builder.CreateStore(PVal, PAddr);
8760 
8761       if (hasRuntimeEvaluationCaptureSize) {
8762         llvm::Value *S = CGF.Builder.CreateConstInBoundsGEP2_32(
8763             llvm::ArrayType::get(CGM.Int64Ty, Info.NumberOfPtrs),
8764             Info.SizesArray,
8765             /*Idx0=*/0,
8766             /*Idx1=*/I);
8767         Address SAddr(S, Ctx.getTypeAlignInChars(Int64Ty));
8768         CGF.Builder.CreateStore(CGF.Builder.CreateIntCast(CombinedInfo.Sizes[I],
8769                                                           CGM.Int64Ty,
8770                                                           /*isSigned=*/true),
8771                                 SAddr);
8772       }
8773 
8774       // Fill up the mapper array.
8775       llvm::Value *MFunc = llvm::ConstantPointerNull::get(CGM.VoidPtrTy);
8776       if (CombinedInfo.Mappers[I]) {
8777         MFunc = CGM.getOpenMPRuntime().getOrCreateUserDefinedMapperFunc(
8778             cast<OMPDeclareMapperDecl>(CombinedInfo.Mappers[I]));
8779         MFunc = CGF.Builder.CreatePointerCast(MFunc, CGM.VoidPtrTy);
8780         Info.HasMapper = true;
8781       }
8782       Address MAddr = CGF.Builder.CreateConstArrayGEP(MappersArray, I);
8783       CGF.Builder.CreateStore(MFunc, MAddr);
8784     }
8785   }
8786 }
8787 
8788 /// Emit the arguments to be passed to the runtime library based on the
8789 /// arrays of base pointers, pointers, sizes, map types, and mappers.
8790 static void emitOffloadingArraysArgument(
8791     CodeGenFunction &CGF, llvm::Value *&BasePointersArrayArg,
8792     llvm::Value *&PointersArrayArg, llvm::Value *&SizesArrayArg,
8793     llvm::Value *&MapTypesArrayArg, llvm::Value *&MappersArrayArg,
8794     CGOpenMPRuntime::TargetDataInfo &Info) {
8795   CodeGenModule &CGM = CGF.CGM;
8796   if (Info.NumberOfPtrs) {
8797     BasePointersArrayArg = CGF.Builder.CreateConstInBoundsGEP2_32(
8798         llvm::ArrayType::get(CGM.VoidPtrTy, Info.NumberOfPtrs),
8799         Info.BasePointersArray,
8800         /*Idx0=*/0, /*Idx1=*/0);
8801     PointersArrayArg = CGF.Builder.CreateConstInBoundsGEP2_32(
8802         llvm::ArrayType::get(CGM.VoidPtrTy, Info.NumberOfPtrs),
8803         Info.PointersArray,
8804         /*Idx0=*/0,
8805         /*Idx1=*/0);
8806     SizesArrayArg = CGF.Builder.CreateConstInBoundsGEP2_32(
8807         llvm::ArrayType::get(CGM.Int64Ty, Info.NumberOfPtrs), Info.SizesArray,
8808         /*Idx0=*/0, /*Idx1=*/0);
8809     MapTypesArrayArg = CGF.Builder.CreateConstInBoundsGEP2_32(
8810         llvm::ArrayType::get(CGM.Int64Ty, Info.NumberOfPtrs),
8811         Info.MapTypesArray,
8812         /*Idx0=*/0,
8813         /*Idx1=*/0);
8814     MappersArrayArg =
8815         Info.HasMapper
8816             ? CGF.Builder.CreatePointerCast(Info.MappersArray, CGM.VoidPtrPtrTy)
8817             : llvm::ConstantPointerNull::get(CGM.VoidPtrPtrTy);
8818   } else {
8819     BasePointersArrayArg = llvm::ConstantPointerNull::get(CGM.VoidPtrPtrTy);
8820     PointersArrayArg = llvm::ConstantPointerNull::get(CGM.VoidPtrPtrTy);
8821     SizesArrayArg = llvm::ConstantPointerNull::get(CGM.Int64Ty->getPointerTo());
8822     MapTypesArrayArg =
8823         llvm::ConstantPointerNull::get(CGM.Int64Ty->getPointerTo());
8824     MappersArrayArg = llvm::ConstantPointerNull::get(CGM.VoidPtrPtrTy);
8825   }
8826 }
8827 
8828 /// Check for inner distribute directive.
8829 static const OMPExecutableDirective *
8830 getNestedDistributeDirective(ASTContext &Ctx, const OMPExecutableDirective &D) {
8831   const auto *CS = D.getInnermostCapturedStmt();
8832   const auto *Body =
8833       CS->getCapturedStmt()->IgnoreContainers(/*IgnoreCaptured=*/true);
8834   const Stmt *ChildStmt =
8835       CGOpenMPSIMDRuntime::getSingleCompoundChild(Ctx, Body);
8836 
8837   if (const auto *NestedDir =
8838           dyn_cast_or_null<OMPExecutableDirective>(ChildStmt)) {
8839     OpenMPDirectiveKind DKind = NestedDir->getDirectiveKind();
8840     switch (D.getDirectiveKind()) {
8841     case OMPD_target:
8842       if (isOpenMPDistributeDirective(DKind))
8843         return NestedDir;
8844       if (DKind == OMPD_teams) {
8845         Body = NestedDir->getInnermostCapturedStmt()->IgnoreContainers(
8846             /*IgnoreCaptured=*/true);
8847         if (!Body)
8848           return nullptr;
8849         ChildStmt = CGOpenMPSIMDRuntime::getSingleCompoundChild(Ctx, Body);
8850         if (const auto *NND =
8851                 dyn_cast_or_null<OMPExecutableDirective>(ChildStmt)) {
8852           DKind = NND->getDirectiveKind();
8853           if (isOpenMPDistributeDirective(DKind))
8854             return NND;
8855         }
8856       }
8857       return nullptr;
8858     case OMPD_target_teams:
8859       if (isOpenMPDistributeDirective(DKind))
8860         return NestedDir;
8861       return nullptr;
8862     case OMPD_target_parallel:
8863     case OMPD_target_simd:
8864     case OMPD_target_parallel_for:
8865     case OMPD_target_parallel_for_simd:
8866       return nullptr;
8867     case OMPD_target_teams_distribute:
8868     case OMPD_target_teams_distribute_simd:
8869     case OMPD_target_teams_distribute_parallel_for:
8870     case OMPD_target_teams_distribute_parallel_for_simd:
8871     case OMPD_parallel:
8872     case OMPD_for:
8873     case OMPD_parallel_for:
8874     case OMPD_parallel_master:
8875     case OMPD_parallel_sections:
8876     case OMPD_for_simd:
8877     case OMPD_parallel_for_simd:
8878     case OMPD_cancel:
8879     case OMPD_cancellation_point:
8880     case OMPD_ordered:
8881     case OMPD_threadprivate:
8882     case OMPD_allocate:
8883     case OMPD_task:
8884     case OMPD_simd:
8885     case OMPD_sections:
8886     case OMPD_section:
8887     case OMPD_single:
8888     case OMPD_master:
8889     case OMPD_critical:
8890     case OMPD_taskyield:
8891     case OMPD_barrier:
8892     case OMPD_taskwait:
8893     case OMPD_taskgroup:
8894     case OMPD_atomic:
8895     case OMPD_flush:
8896     case OMPD_depobj:
8897     case OMPD_scan:
8898     case OMPD_teams:
8899     case OMPD_target_data:
8900     case OMPD_target_exit_data:
8901     case OMPD_target_enter_data:
8902     case OMPD_distribute:
8903     case OMPD_distribute_simd:
8904     case OMPD_distribute_parallel_for:
8905     case OMPD_distribute_parallel_for_simd:
8906     case OMPD_teams_distribute:
8907     case OMPD_teams_distribute_simd:
8908     case OMPD_teams_distribute_parallel_for:
8909     case OMPD_teams_distribute_parallel_for_simd:
8910     case OMPD_target_update:
8911     case OMPD_declare_simd:
8912     case OMPD_declare_variant:
8913     case OMPD_begin_declare_variant:
8914     case OMPD_end_declare_variant:
8915     case OMPD_declare_target:
8916     case OMPD_end_declare_target:
8917     case OMPD_declare_reduction:
8918     case OMPD_declare_mapper:
8919     case OMPD_taskloop:
8920     case OMPD_taskloop_simd:
8921     case OMPD_master_taskloop:
8922     case OMPD_master_taskloop_simd:
8923     case OMPD_parallel_master_taskloop:
8924     case OMPD_parallel_master_taskloop_simd:
8925     case OMPD_requires:
8926     case OMPD_unknown:
8927     default:
8928       llvm_unreachable("Unexpected directive.");
8929     }
8930   }
8931 
8932   return nullptr;
8933 }
8934 
8935 /// Emit the user-defined mapper function. The code generation follows the
8936 /// pattern in the example below.
8937 /// \code
8938 /// void .omp_mapper.<type_name>.<mapper_id>.(void *rt_mapper_handle,
8939 ///                                           void *base, void *begin,
8940 ///                                           int64_t size, int64_t type) {
8941 ///   // Allocate space for an array section first.
8942 ///   if (size > 1 && !maptype.IsDelete)
8943 ///     __tgt_push_mapper_component(rt_mapper_handle, base, begin,
8944 ///                                 size*sizeof(Ty), clearToFrom(type));
8945 ///   // Map members.
8946 ///   for (unsigned i = 0; i < size; i++) {
8947 ///     // For each component specified by this mapper:
8948 ///     for (auto c : all_components) {
8949 ///       if (c.hasMapper())
8950 ///         (*c.Mapper())(rt_mapper_handle, c.arg_base, c.arg_begin, c.arg_size,
8951 ///                       c.arg_type);
8952 ///       else
8953 ///         __tgt_push_mapper_component(rt_mapper_handle, c.arg_base,
8954 ///                                     c.arg_begin, c.arg_size, c.arg_type);
8955 ///     }
8956 ///   }
8957 ///   // Delete the array section.
8958 ///   if (size > 1 && maptype.IsDelete)
8959 ///     __tgt_push_mapper_component(rt_mapper_handle, base, begin,
8960 ///                                 size*sizeof(Ty), clearToFrom(type));
8961 /// }
8962 /// \endcode
8963 void CGOpenMPRuntime::emitUserDefinedMapper(const OMPDeclareMapperDecl *D,
8964                                             CodeGenFunction *CGF) {
8965   if (UDMMap.count(D) > 0)
8966     return;
8967   ASTContext &C = CGM.getContext();
8968   QualType Ty = D->getType();
8969   QualType PtrTy = C.getPointerType(Ty).withRestrict();
8970   QualType Int64Ty = C.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/true);
8971   auto *MapperVarDecl =
8972       cast<VarDecl>(cast<DeclRefExpr>(D->getMapperVarRef())->getDecl());
8973   SourceLocation Loc = D->getLocation();
8974   CharUnits ElementSize = C.getTypeSizeInChars(Ty);
8975 
8976   // Prepare mapper function arguments and attributes.
8977   ImplicitParamDecl HandleArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr,
8978                               C.VoidPtrTy, ImplicitParamDecl::Other);
8979   ImplicitParamDecl BaseArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, C.VoidPtrTy,
8980                             ImplicitParamDecl::Other);
8981   ImplicitParamDecl BeginArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr,
8982                              C.VoidPtrTy, ImplicitParamDecl::Other);
8983   ImplicitParamDecl SizeArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, Int64Ty,
8984                             ImplicitParamDecl::Other);
8985   ImplicitParamDecl TypeArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, Int64Ty,
8986                             ImplicitParamDecl::Other);
8987   FunctionArgList Args;
8988   Args.push_back(&HandleArg);
8989   Args.push_back(&BaseArg);
8990   Args.push_back(&BeginArg);
8991   Args.push_back(&SizeArg);
8992   Args.push_back(&TypeArg);
8993   const CGFunctionInfo &FnInfo =
8994       CGM.getTypes().arrangeBuiltinFunctionDeclaration(C.VoidTy, Args);
8995   llvm::FunctionType *FnTy = CGM.getTypes().GetFunctionType(FnInfo);
8996   SmallString<64> TyStr;
8997   llvm::raw_svector_ostream Out(TyStr);
8998   CGM.getCXXABI().getMangleContext().mangleTypeName(Ty, Out);
8999   std::string Name = getName({"omp_mapper", TyStr, D->getName()});
9000   auto *Fn = llvm::Function::Create(FnTy, llvm::GlobalValue::InternalLinkage,
9001                                     Name, &CGM.getModule());
9002   CGM.SetInternalFunctionAttributes(GlobalDecl(), Fn, FnInfo);
9003   Fn->removeFnAttr(llvm::Attribute::OptimizeNone);
9004   // Start the mapper function code generation.
9005   CodeGenFunction MapperCGF(CGM);
9006   MapperCGF.StartFunction(GlobalDecl(), C.VoidTy, Fn, FnInfo, Args, Loc, Loc);
9007   // Compute the starting and end addreses of array elements.
9008   llvm::Value *Size = MapperCGF.EmitLoadOfScalar(
9009       MapperCGF.GetAddrOfLocalVar(&SizeArg), /*Volatile=*/false,
9010       C.getPointerType(Int64Ty), Loc);
9011   // Convert the size in bytes into the number of array elements.
9012   Size = MapperCGF.Builder.CreateExactUDiv(
9013       Size, MapperCGF.Builder.getInt64(ElementSize.getQuantity()));
9014   llvm::Value *PtrBegin = MapperCGF.Builder.CreateBitCast(
9015       MapperCGF.GetAddrOfLocalVar(&BeginArg).getPointer(),
9016       CGM.getTypes().ConvertTypeForMem(C.getPointerType(PtrTy)));
9017   llvm::Value *PtrEnd = MapperCGF.Builder.CreateGEP(PtrBegin, Size);
9018   llvm::Value *MapType = MapperCGF.EmitLoadOfScalar(
9019       MapperCGF.GetAddrOfLocalVar(&TypeArg), /*Volatile=*/false,
9020       C.getPointerType(Int64Ty), Loc);
9021   // Prepare common arguments for array initiation and deletion.
9022   llvm::Value *Handle = MapperCGF.EmitLoadOfScalar(
9023       MapperCGF.GetAddrOfLocalVar(&HandleArg),
9024       /*Volatile=*/false, C.getPointerType(C.VoidPtrTy), Loc);
9025   llvm::Value *BaseIn = MapperCGF.EmitLoadOfScalar(
9026       MapperCGF.GetAddrOfLocalVar(&BaseArg),
9027       /*Volatile=*/false, C.getPointerType(C.VoidPtrTy), Loc);
9028   llvm::Value *BeginIn = MapperCGF.EmitLoadOfScalar(
9029       MapperCGF.GetAddrOfLocalVar(&BeginArg),
9030       /*Volatile=*/false, C.getPointerType(C.VoidPtrTy), Loc);
9031 
9032   // Emit array initiation if this is an array section and \p MapType indicates
9033   // that memory allocation is required.
9034   llvm::BasicBlock *HeadBB = MapperCGF.createBasicBlock("omp.arraymap.head");
9035   emitUDMapperArrayInitOrDel(MapperCGF, Handle, BaseIn, BeginIn, Size, MapType,
9036                              ElementSize, HeadBB, /*IsInit=*/true);
9037 
9038   // Emit a for loop to iterate through SizeArg of elements and map all of them.
9039 
9040   // Emit the loop header block.
9041   MapperCGF.EmitBlock(HeadBB);
9042   llvm::BasicBlock *BodyBB = MapperCGF.createBasicBlock("omp.arraymap.body");
9043   llvm::BasicBlock *DoneBB = MapperCGF.createBasicBlock("omp.done");
9044   // Evaluate whether the initial condition is satisfied.
9045   llvm::Value *IsEmpty =
9046       MapperCGF.Builder.CreateICmpEQ(PtrBegin, PtrEnd, "omp.arraymap.isempty");
9047   MapperCGF.Builder.CreateCondBr(IsEmpty, DoneBB, BodyBB);
9048   llvm::BasicBlock *EntryBB = MapperCGF.Builder.GetInsertBlock();
9049 
9050   // Emit the loop body block.
9051   MapperCGF.EmitBlock(BodyBB);
9052   llvm::BasicBlock *LastBB = BodyBB;
9053   llvm::PHINode *PtrPHI = MapperCGF.Builder.CreatePHI(
9054       PtrBegin->getType(), 2, "omp.arraymap.ptrcurrent");
9055   PtrPHI->addIncoming(PtrBegin, EntryBB);
9056   Address PtrCurrent =
9057       Address(PtrPHI, MapperCGF.GetAddrOfLocalVar(&BeginArg)
9058                           .getAlignment()
9059                           .alignmentOfArrayElement(ElementSize));
9060   // Privatize the declared variable of mapper to be the current array element.
9061   CodeGenFunction::OMPPrivateScope Scope(MapperCGF);
9062   Scope.addPrivate(MapperVarDecl, [&MapperCGF, PtrCurrent, PtrTy]() {
9063     return MapperCGF
9064         .EmitLoadOfPointerLValue(PtrCurrent, PtrTy->castAs<PointerType>())
9065         .getAddress(MapperCGF);
9066   });
9067   (void)Scope.Privatize();
9068 
9069   // Get map clause information. Fill up the arrays with all mapped variables.
9070   MappableExprsHandler::MapCombinedInfoTy Info;
9071   MappableExprsHandler MEHandler(*D, MapperCGF);
9072   MEHandler.generateAllInfoForMapper(Info);
9073 
9074   // Call the runtime API __tgt_mapper_num_components to get the number of
9075   // pre-existing components.
9076   llvm::Value *OffloadingArgs[] = {Handle};
9077   llvm::Value *PreviousSize = MapperCGF.EmitRuntimeCall(
9078       OMPBuilder.getOrCreateRuntimeFunction(CGM.getModule(),
9079                                             OMPRTL___tgt_mapper_num_components),
9080       OffloadingArgs);
9081   llvm::Value *ShiftedPreviousSize = MapperCGF.Builder.CreateShl(
9082       PreviousSize,
9083       MapperCGF.Builder.getInt64(MappableExprsHandler::getFlagMemberOffset()));
9084 
9085   // Fill up the runtime mapper handle for all components.
9086   for (unsigned I = 0; I < Info.BasePointers.size(); ++I) {
9087     llvm::Value *CurBaseArg = MapperCGF.Builder.CreateBitCast(
9088         *Info.BasePointers[I], CGM.getTypes().ConvertTypeForMem(C.VoidPtrTy));
9089     llvm::Value *CurBeginArg = MapperCGF.Builder.CreateBitCast(
9090         Info.Pointers[I], CGM.getTypes().ConvertTypeForMem(C.VoidPtrTy));
9091     llvm::Value *CurSizeArg = Info.Sizes[I];
9092 
9093     // Extract the MEMBER_OF field from the map type.
9094     llvm::BasicBlock *MemberBB = MapperCGF.createBasicBlock("omp.member");
9095     MapperCGF.EmitBlock(MemberBB);
9096     llvm::Value *OriMapType = MapperCGF.Builder.getInt64(Info.Types[I]);
9097     llvm::Value *Member = MapperCGF.Builder.CreateAnd(
9098         OriMapType,
9099         MapperCGF.Builder.getInt64(MappableExprsHandler::OMP_MAP_MEMBER_OF));
9100     llvm::BasicBlock *MemberCombineBB =
9101         MapperCGF.createBasicBlock("omp.member.combine");
9102     llvm::BasicBlock *TypeBB = MapperCGF.createBasicBlock("omp.type");
9103     llvm::Value *IsMember = MapperCGF.Builder.CreateIsNull(Member);
9104     MapperCGF.Builder.CreateCondBr(IsMember, TypeBB, MemberCombineBB);
9105     // Add the number of pre-existing components to the MEMBER_OF field if it
9106     // is valid.
9107     MapperCGF.EmitBlock(MemberCombineBB);
9108     llvm::Value *CombinedMember =
9109         MapperCGF.Builder.CreateNUWAdd(OriMapType, ShiftedPreviousSize);
9110     // Do nothing if it is not a member of previous components.
9111     MapperCGF.EmitBlock(TypeBB);
9112     llvm::PHINode *MemberMapType =
9113         MapperCGF.Builder.CreatePHI(CGM.Int64Ty, 4, "omp.membermaptype");
9114     MemberMapType->addIncoming(OriMapType, MemberBB);
9115     MemberMapType->addIncoming(CombinedMember, MemberCombineBB);
9116 
9117     // Combine the map type inherited from user-defined mapper with that
9118     // specified in the program. According to the OMP_MAP_TO and OMP_MAP_FROM
9119     // bits of the \a MapType, which is the input argument of the mapper
9120     // function, the following code will set the OMP_MAP_TO and OMP_MAP_FROM
9121     // bits of MemberMapType.
9122     // [OpenMP 5.0], 1.2.6. map-type decay.
9123     //        | alloc |  to   | from  | tofrom | release | delete
9124     // ----------------------------------------------------------
9125     // alloc  | alloc | alloc | alloc | alloc  | release | delete
9126     // to     | alloc |  to   | alloc |   to   | release | delete
9127     // from   | alloc | alloc | from  |  from  | release | delete
9128     // tofrom | alloc |  to   | from  | tofrom | release | delete
9129     llvm::Value *LeftToFrom = MapperCGF.Builder.CreateAnd(
9130         MapType,
9131         MapperCGF.Builder.getInt64(MappableExprsHandler::OMP_MAP_TO |
9132                                    MappableExprsHandler::OMP_MAP_FROM));
9133     llvm::BasicBlock *AllocBB = MapperCGF.createBasicBlock("omp.type.alloc");
9134     llvm::BasicBlock *AllocElseBB =
9135         MapperCGF.createBasicBlock("omp.type.alloc.else");
9136     llvm::BasicBlock *ToBB = MapperCGF.createBasicBlock("omp.type.to");
9137     llvm::BasicBlock *ToElseBB = MapperCGF.createBasicBlock("omp.type.to.else");
9138     llvm::BasicBlock *FromBB = MapperCGF.createBasicBlock("omp.type.from");
9139     llvm::BasicBlock *EndBB = MapperCGF.createBasicBlock("omp.type.end");
9140     llvm::Value *IsAlloc = MapperCGF.Builder.CreateIsNull(LeftToFrom);
9141     MapperCGF.Builder.CreateCondBr(IsAlloc, AllocBB, AllocElseBB);
9142     // In case of alloc, clear OMP_MAP_TO and OMP_MAP_FROM.
9143     MapperCGF.EmitBlock(AllocBB);
9144     llvm::Value *AllocMapType = MapperCGF.Builder.CreateAnd(
9145         MemberMapType,
9146         MapperCGF.Builder.getInt64(~(MappableExprsHandler::OMP_MAP_TO |
9147                                      MappableExprsHandler::OMP_MAP_FROM)));
9148     MapperCGF.Builder.CreateBr(EndBB);
9149     MapperCGF.EmitBlock(AllocElseBB);
9150     llvm::Value *IsTo = MapperCGF.Builder.CreateICmpEQ(
9151         LeftToFrom,
9152         MapperCGF.Builder.getInt64(MappableExprsHandler::OMP_MAP_TO));
9153     MapperCGF.Builder.CreateCondBr(IsTo, ToBB, ToElseBB);
9154     // In case of to, clear OMP_MAP_FROM.
9155     MapperCGF.EmitBlock(ToBB);
9156     llvm::Value *ToMapType = MapperCGF.Builder.CreateAnd(
9157         MemberMapType,
9158         MapperCGF.Builder.getInt64(~MappableExprsHandler::OMP_MAP_FROM));
9159     MapperCGF.Builder.CreateBr(EndBB);
9160     MapperCGF.EmitBlock(ToElseBB);
9161     llvm::Value *IsFrom = MapperCGF.Builder.CreateICmpEQ(
9162         LeftToFrom,
9163         MapperCGF.Builder.getInt64(MappableExprsHandler::OMP_MAP_FROM));
9164     MapperCGF.Builder.CreateCondBr(IsFrom, FromBB, EndBB);
9165     // In case of from, clear OMP_MAP_TO.
9166     MapperCGF.EmitBlock(FromBB);
9167     llvm::Value *FromMapType = MapperCGF.Builder.CreateAnd(
9168         MemberMapType,
9169         MapperCGF.Builder.getInt64(~MappableExprsHandler::OMP_MAP_TO));
9170     // In case of tofrom, do nothing.
9171     MapperCGF.EmitBlock(EndBB);
9172     LastBB = EndBB;
9173     llvm::PHINode *CurMapType =
9174         MapperCGF.Builder.CreatePHI(CGM.Int64Ty, 4, "omp.maptype");
9175     CurMapType->addIncoming(AllocMapType, AllocBB);
9176     CurMapType->addIncoming(ToMapType, ToBB);
9177     CurMapType->addIncoming(FromMapType, FromBB);
9178     CurMapType->addIncoming(MemberMapType, ToElseBB);
9179 
9180     llvm::Value *OffloadingArgs[] = {Handle, CurBaseArg, CurBeginArg,
9181                                      CurSizeArg, CurMapType};
9182     if (Info.Mappers[I]) {
9183       // Call the corresponding mapper function.
9184       llvm::Function *MapperFunc = getOrCreateUserDefinedMapperFunc(
9185           cast<OMPDeclareMapperDecl>(Info.Mappers[I]));
9186       assert(MapperFunc && "Expect a valid mapper function is available.");
9187       MapperCGF.EmitNounwindRuntimeCall(MapperFunc, OffloadingArgs);
9188     } else {
9189       // Call the runtime API __tgt_push_mapper_component to fill up the runtime
9190       // data structure.
9191       MapperCGF.EmitRuntimeCall(
9192           OMPBuilder.getOrCreateRuntimeFunction(
9193               CGM.getModule(), OMPRTL___tgt_push_mapper_component),
9194           OffloadingArgs);
9195     }
9196   }
9197 
9198   // Update the pointer to point to the next element that needs to be mapped,
9199   // and check whether we have mapped all elements.
9200   llvm::Value *PtrNext = MapperCGF.Builder.CreateConstGEP1_32(
9201       PtrPHI, /*Idx0=*/1, "omp.arraymap.next");
9202   PtrPHI->addIncoming(PtrNext, LastBB);
9203   llvm::Value *IsDone =
9204       MapperCGF.Builder.CreateICmpEQ(PtrNext, PtrEnd, "omp.arraymap.isdone");
9205   llvm::BasicBlock *ExitBB = MapperCGF.createBasicBlock("omp.arraymap.exit");
9206   MapperCGF.Builder.CreateCondBr(IsDone, ExitBB, BodyBB);
9207 
9208   MapperCGF.EmitBlock(ExitBB);
9209   // Emit array deletion if this is an array section and \p MapType indicates
9210   // that deletion is required.
9211   emitUDMapperArrayInitOrDel(MapperCGF, Handle, BaseIn, BeginIn, Size, MapType,
9212                              ElementSize, DoneBB, /*IsInit=*/false);
9213 
9214   // Emit the function exit block.
9215   MapperCGF.EmitBlock(DoneBB, /*IsFinished=*/true);
9216   MapperCGF.FinishFunction();
9217   UDMMap.try_emplace(D, Fn);
9218   if (CGF) {
9219     auto &Decls = FunctionUDMMap.FindAndConstruct(CGF->CurFn);
9220     Decls.second.push_back(D);
9221   }
9222 }
9223 
9224 /// Emit the array initialization or deletion portion for user-defined mapper
9225 /// code generation. First, it evaluates whether an array section is mapped and
9226 /// whether the \a MapType instructs to delete this section. If \a IsInit is
9227 /// true, and \a MapType indicates to not delete this array, array
9228 /// initialization code is generated. If \a IsInit is false, and \a MapType
9229 /// indicates to not this array, array deletion code is generated.
9230 void CGOpenMPRuntime::emitUDMapperArrayInitOrDel(
9231     CodeGenFunction &MapperCGF, llvm::Value *Handle, llvm::Value *Base,
9232     llvm::Value *Begin, llvm::Value *Size, llvm::Value *MapType,
9233     CharUnits ElementSize, llvm::BasicBlock *ExitBB, bool IsInit) {
9234   StringRef Prefix = IsInit ? ".init" : ".del";
9235 
9236   // Evaluate if this is an array section.
9237   llvm::BasicBlock *IsDeleteBB =
9238       MapperCGF.createBasicBlock(getName({"omp.array", Prefix, ".evaldelete"}));
9239   llvm::BasicBlock *BodyBB =
9240       MapperCGF.createBasicBlock(getName({"omp.array", Prefix}));
9241   llvm::Value *IsArray = MapperCGF.Builder.CreateICmpSGE(
9242       Size, MapperCGF.Builder.getInt64(1), "omp.arrayinit.isarray");
9243   MapperCGF.Builder.CreateCondBr(IsArray, IsDeleteBB, ExitBB);
9244 
9245   // Evaluate if we are going to delete this section.
9246   MapperCGF.EmitBlock(IsDeleteBB);
9247   llvm::Value *DeleteBit = MapperCGF.Builder.CreateAnd(
9248       MapType,
9249       MapperCGF.Builder.getInt64(MappableExprsHandler::OMP_MAP_DELETE));
9250   llvm::Value *DeleteCond;
9251   if (IsInit) {
9252     DeleteCond = MapperCGF.Builder.CreateIsNull(
9253         DeleteBit, getName({"omp.array", Prefix, ".delete"}));
9254   } else {
9255     DeleteCond = MapperCGF.Builder.CreateIsNotNull(
9256         DeleteBit, getName({"omp.array", Prefix, ".delete"}));
9257   }
9258   MapperCGF.Builder.CreateCondBr(DeleteCond, BodyBB, ExitBB);
9259 
9260   MapperCGF.EmitBlock(BodyBB);
9261   // Get the array size by multiplying element size and element number (i.e., \p
9262   // Size).
9263   llvm::Value *ArraySize = MapperCGF.Builder.CreateNUWMul(
9264       Size, MapperCGF.Builder.getInt64(ElementSize.getQuantity()));
9265   // Remove OMP_MAP_TO and OMP_MAP_FROM from the map type, so that it achieves
9266   // memory allocation/deletion purpose only.
9267   llvm::Value *MapTypeArg = MapperCGF.Builder.CreateAnd(
9268       MapType,
9269       MapperCGF.Builder.getInt64(~(MappableExprsHandler::OMP_MAP_TO |
9270                                    MappableExprsHandler::OMP_MAP_FROM)));
9271   // Call the runtime API __tgt_push_mapper_component to fill up the runtime
9272   // data structure.
9273   llvm::Value *OffloadingArgs[] = {Handle, Base, Begin, ArraySize, MapTypeArg};
9274   MapperCGF.EmitRuntimeCall(
9275       OMPBuilder.getOrCreateRuntimeFunction(CGM.getModule(),
9276                                             OMPRTL___tgt_push_mapper_component),
9277       OffloadingArgs);
9278 }
9279 
9280 llvm::Function *CGOpenMPRuntime::getOrCreateUserDefinedMapperFunc(
9281     const OMPDeclareMapperDecl *D) {
9282   auto I = UDMMap.find(D);
9283   if (I != UDMMap.end())
9284     return I->second;
9285   emitUserDefinedMapper(D);
9286   return UDMMap.lookup(D);
9287 }
9288 
9289 void CGOpenMPRuntime::emitTargetNumIterationsCall(
9290     CodeGenFunction &CGF, const OMPExecutableDirective &D,
9291     llvm::Value *DeviceID,
9292     llvm::function_ref<llvm::Value *(CodeGenFunction &CGF,
9293                                      const OMPLoopDirective &D)>
9294         SizeEmitter) {
9295   OpenMPDirectiveKind Kind = D.getDirectiveKind();
9296   const OMPExecutableDirective *TD = &D;
9297   // Get nested teams distribute kind directive, if any.
9298   if (!isOpenMPDistributeDirective(Kind) || !isOpenMPTeamsDirective(Kind))
9299     TD = getNestedDistributeDirective(CGM.getContext(), D);
9300   if (!TD)
9301     return;
9302   const auto *LD = cast<OMPLoopDirective>(TD);
9303   auto &&CodeGen = [LD, DeviceID, SizeEmitter, this](CodeGenFunction &CGF,
9304                                                      PrePostActionTy &) {
9305     if (llvm::Value *NumIterations = SizeEmitter(CGF, *LD)) {
9306       llvm::Value *Args[] = {DeviceID, NumIterations};
9307       CGF.EmitRuntimeCall(
9308           OMPBuilder.getOrCreateRuntimeFunction(
9309               CGM.getModule(), OMPRTL___kmpc_push_target_tripcount),
9310           Args);
9311     }
9312   };
9313   emitInlinedDirective(CGF, OMPD_unknown, CodeGen);
9314 }
9315 
9316 void CGOpenMPRuntime::emitTargetCall(
9317     CodeGenFunction &CGF, const OMPExecutableDirective &D,
9318     llvm::Function *OutlinedFn, llvm::Value *OutlinedFnID, const Expr *IfCond,
9319     llvm::PointerIntPair<const Expr *, 2, OpenMPDeviceClauseModifier> Device,
9320     llvm::function_ref<llvm::Value *(CodeGenFunction &CGF,
9321                                      const OMPLoopDirective &D)>
9322         SizeEmitter) {
9323   if (!CGF.HaveInsertPoint())
9324     return;
9325 
9326   assert(OutlinedFn && "Invalid outlined function!");
9327 
9328   const bool RequiresOuterTask = D.hasClausesOfKind<OMPDependClause>();
9329   llvm::SmallVector<llvm::Value *, 16> CapturedVars;
9330   const CapturedStmt &CS = *D.getCapturedStmt(OMPD_target);
9331   auto &&ArgsCodegen = [&CS, &CapturedVars](CodeGenFunction &CGF,
9332                                             PrePostActionTy &) {
9333     CGF.GenerateOpenMPCapturedVars(CS, CapturedVars);
9334   };
9335   emitInlinedDirective(CGF, OMPD_unknown, ArgsCodegen);
9336 
9337   CodeGenFunction::OMPTargetDataInfo InputInfo;
9338   llvm::Value *MapTypesArray = nullptr;
9339   // Fill up the pointer arrays and transfer execution to the device.
9340   auto &&ThenGen = [this, Device, OutlinedFn, OutlinedFnID, &D, &InputInfo,
9341                     &MapTypesArray, &CS, RequiresOuterTask, &CapturedVars,
9342                     SizeEmitter](CodeGenFunction &CGF, PrePostActionTy &) {
9343     if (Device.getInt() == OMPC_DEVICE_ancestor) {
9344       // Reverse offloading is not supported, so just execute on the host.
9345       if (RequiresOuterTask) {
9346         CapturedVars.clear();
9347         CGF.GenerateOpenMPCapturedVars(CS, CapturedVars);
9348       }
9349       emitOutlinedFunctionCall(CGF, D.getBeginLoc(), OutlinedFn, CapturedVars);
9350       return;
9351     }
9352 
9353     // On top of the arrays that were filled up, the target offloading call
9354     // takes as arguments the device id as well as the host pointer. The host
9355     // pointer is used by the runtime library to identify the current target
9356     // region, so it only has to be unique and not necessarily point to
9357     // anything. It could be the pointer to the outlined function that
9358     // implements the target region, but we aren't using that so that the
9359     // compiler doesn't need to keep that, and could therefore inline the host
9360     // function if proven worthwhile during optimization.
9361 
9362     // From this point on, we need to have an ID of the target region defined.
9363     assert(OutlinedFnID && "Invalid outlined function ID!");
9364 
9365     // Emit device ID if any.
9366     llvm::Value *DeviceID;
9367     if (Device.getPointer()) {
9368       assert((Device.getInt() == OMPC_DEVICE_unknown ||
9369               Device.getInt() == OMPC_DEVICE_device_num) &&
9370              "Expected device_num modifier.");
9371       llvm::Value *DevVal = CGF.EmitScalarExpr(Device.getPointer());
9372       DeviceID =
9373           CGF.Builder.CreateIntCast(DevVal, CGF.Int64Ty, /*isSigned=*/true);
9374     } else {
9375       DeviceID = CGF.Builder.getInt64(OMP_DEVICEID_UNDEF);
9376     }
9377 
9378     // Emit the number of elements in the offloading arrays.
9379     llvm::Value *PointerNum =
9380         CGF.Builder.getInt32(InputInfo.NumberOfTargetItems);
9381 
9382     // Return value of the runtime offloading call.
9383     llvm::Value *Return;
9384 
9385     llvm::Value *NumTeams = emitNumTeamsForTargetDirective(CGF, D);
9386     llvm::Value *NumThreads = emitNumThreadsForTargetDirective(CGF, D);
9387 
9388     // Emit tripcount for the target loop-based directive.
9389     emitTargetNumIterationsCall(CGF, D, DeviceID, SizeEmitter);
9390 
9391     bool HasNowait = D.hasClausesOfKind<OMPNowaitClause>();
9392     // The target region is an outlined function launched by the runtime
9393     // via calls __tgt_target() or __tgt_target_teams().
9394     //
9395     // __tgt_target() launches a target region with one team and one thread,
9396     // executing a serial region.  This master thread may in turn launch
9397     // more threads within its team upon encountering a parallel region,
9398     // however, no additional teams can be launched on the device.
9399     //
9400     // __tgt_target_teams() launches a target region with one or more teams,
9401     // each with one or more threads.  This call is required for target
9402     // constructs such as:
9403     //  'target teams'
9404     //  'target' / 'teams'
9405     //  'target teams distribute parallel for'
9406     //  'target parallel'
9407     // and so on.
9408     //
9409     // Note that on the host and CPU targets, the runtime implementation of
9410     // these calls simply call the outlined function without forking threads.
9411     // The outlined functions themselves have runtime calls to
9412     // __kmpc_fork_teams() and __kmpc_fork() for this purpose, codegen'd by
9413     // the compiler in emitTeamsCall() and emitParallelCall().
9414     //
9415     // In contrast, on the NVPTX target, the implementation of
9416     // __tgt_target_teams() launches a GPU kernel with the requested number
9417     // of teams and threads so no additional calls to the runtime are required.
9418     if (NumTeams) {
9419       // If we have NumTeams defined this means that we have an enclosed teams
9420       // region. Therefore we also expect to have NumThreads defined. These two
9421       // values should be defined in the presence of a teams directive,
9422       // regardless of having any clauses associated. If the user is using teams
9423       // but no clauses, these two values will be the default that should be
9424       // passed to the runtime library - a 32-bit integer with the value zero.
9425       assert(NumThreads && "Thread limit expression should be available along "
9426                            "with number of teams.");
9427       llvm::Value *OffloadingArgs[] = {DeviceID,
9428                                        OutlinedFnID,
9429                                        PointerNum,
9430                                        InputInfo.BasePointersArray.getPointer(),
9431                                        InputInfo.PointersArray.getPointer(),
9432                                        InputInfo.SizesArray.getPointer(),
9433                                        MapTypesArray,
9434                                        InputInfo.MappersArray.getPointer(),
9435                                        NumTeams,
9436                                        NumThreads};
9437       Return = CGF.EmitRuntimeCall(
9438           OMPBuilder.getOrCreateRuntimeFunction(
9439               CGM.getModule(), HasNowait
9440                                    ? OMPRTL___tgt_target_teams_nowait_mapper
9441                                    : OMPRTL___tgt_target_teams_mapper),
9442           OffloadingArgs);
9443     } else {
9444       llvm::Value *OffloadingArgs[] = {DeviceID,
9445                                        OutlinedFnID,
9446                                        PointerNum,
9447                                        InputInfo.BasePointersArray.getPointer(),
9448                                        InputInfo.PointersArray.getPointer(),
9449                                        InputInfo.SizesArray.getPointer(),
9450                                        MapTypesArray,
9451                                        InputInfo.MappersArray.getPointer()};
9452       Return = CGF.EmitRuntimeCall(
9453           OMPBuilder.getOrCreateRuntimeFunction(
9454               CGM.getModule(), HasNowait ? OMPRTL___tgt_target_nowait_mapper
9455                                          : OMPRTL___tgt_target_mapper),
9456           OffloadingArgs);
9457     }
9458 
9459     // Check the error code and execute the host version if required.
9460     llvm::BasicBlock *OffloadFailedBlock =
9461         CGF.createBasicBlock("omp_offload.failed");
9462     llvm::BasicBlock *OffloadContBlock =
9463         CGF.createBasicBlock("omp_offload.cont");
9464     llvm::Value *Failed = CGF.Builder.CreateIsNotNull(Return);
9465     CGF.Builder.CreateCondBr(Failed, OffloadFailedBlock, OffloadContBlock);
9466 
9467     CGF.EmitBlock(OffloadFailedBlock);
9468     if (RequiresOuterTask) {
9469       CapturedVars.clear();
9470       CGF.GenerateOpenMPCapturedVars(CS, CapturedVars);
9471     }
9472     emitOutlinedFunctionCall(CGF, D.getBeginLoc(), OutlinedFn, CapturedVars);
9473     CGF.EmitBranch(OffloadContBlock);
9474 
9475     CGF.EmitBlock(OffloadContBlock, /*IsFinished=*/true);
9476   };
9477 
9478   // Notify that the host version must be executed.
9479   auto &&ElseGen = [this, &D, OutlinedFn, &CS, &CapturedVars,
9480                     RequiresOuterTask](CodeGenFunction &CGF,
9481                                        PrePostActionTy &) {
9482     if (RequiresOuterTask) {
9483       CapturedVars.clear();
9484       CGF.GenerateOpenMPCapturedVars(CS, CapturedVars);
9485     }
9486     emitOutlinedFunctionCall(CGF, D.getBeginLoc(), OutlinedFn, CapturedVars);
9487   };
9488 
9489   auto &&TargetThenGen = [this, &ThenGen, &D, &InputInfo, &MapTypesArray,
9490                           &CapturedVars, RequiresOuterTask,
9491                           &CS](CodeGenFunction &CGF, PrePostActionTy &) {
9492     // Fill up the arrays with all the captured variables.
9493     MappableExprsHandler::MapCombinedInfoTy CombinedInfo;
9494 
9495     // Get mappable expression information.
9496     MappableExprsHandler MEHandler(D, CGF);
9497     llvm::DenseMap<llvm::Value *, llvm::Value *> LambdaPointers;
9498     llvm::DenseSet<CanonicalDeclPtr<const Decl>> MappedVarSet;
9499 
9500     auto RI = CS.getCapturedRecordDecl()->field_begin();
9501     auto CV = CapturedVars.begin();
9502     for (CapturedStmt::const_capture_iterator CI = CS.capture_begin(),
9503                                               CE = CS.capture_end();
9504          CI != CE; ++CI, ++RI, ++CV) {
9505       MappableExprsHandler::MapCombinedInfoTy CurInfo;
9506       MappableExprsHandler::StructRangeInfoTy PartialStruct;
9507 
9508       // VLA sizes are passed to the outlined region by copy and do not have map
9509       // information associated.
9510       if (CI->capturesVariableArrayType()) {
9511         CurInfo.BasePointers.push_back(*CV);
9512         CurInfo.Pointers.push_back(*CV);
9513         CurInfo.Sizes.push_back(CGF.Builder.CreateIntCast(
9514             CGF.getTypeSize(RI->getType()), CGF.Int64Ty, /*isSigned=*/true));
9515         // Copy to the device as an argument. No need to retrieve it.
9516         CurInfo.Types.push_back(MappableExprsHandler::OMP_MAP_LITERAL |
9517                                 MappableExprsHandler::OMP_MAP_TARGET_PARAM |
9518                                 MappableExprsHandler::OMP_MAP_IMPLICIT);
9519         CurInfo.Mappers.push_back(nullptr);
9520       } else {
9521         // If we have any information in the map clause, we use it, otherwise we
9522         // just do a default mapping.
9523         MEHandler.generateInfoForCapture(CI, *CV, CurInfo, PartialStruct);
9524         if (!CI->capturesThis())
9525           MappedVarSet.insert(CI->getCapturedVar());
9526         else
9527           MappedVarSet.insert(nullptr);
9528         if (CurInfo.BasePointers.empty())
9529           MEHandler.generateDefaultMapInfo(*CI, **RI, *CV, CurInfo);
9530         // Generate correct mapping for variables captured by reference in
9531         // lambdas.
9532         if (CI->capturesVariable())
9533           MEHandler.generateInfoForLambdaCaptures(CI->getCapturedVar(), *CV,
9534                                                   CurInfo, LambdaPointers);
9535       }
9536       // We expect to have at least an element of information for this capture.
9537       assert(!CurInfo.BasePointers.empty() &&
9538              "Non-existing map pointer for capture!");
9539       assert(CurInfo.BasePointers.size() == CurInfo.Pointers.size() &&
9540              CurInfo.BasePointers.size() == CurInfo.Sizes.size() &&
9541              CurInfo.BasePointers.size() == CurInfo.Types.size() &&
9542              CurInfo.BasePointers.size() == CurInfo.Mappers.size() &&
9543              "Inconsistent map information sizes!");
9544 
9545       // If there is an entry in PartialStruct it means we have a struct with
9546       // individual members mapped. Emit an extra combined entry.
9547       if (PartialStruct.Base.isValid())
9548         MEHandler.emitCombinedEntry(CombinedInfo, CurInfo.Types, PartialStruct);
9549 
9550       // We need to append the results of this capture to what we already have.
9551       CombinedInfo.append(CurInfo);
9552     }
9553     // Adjust MEMBER_OF flags for the lambdas captures.
9554     MEHandler.adjustMemberOfForLambdaCaptures(
9555         LambdaPointers, CombinedInfo.BasePointers, CombinedInfo.Pointers,
9556         CombinedInfo.Types);
9557     // Map any list items in a map clause that were not captures because they
9558     // weren't referenced within the construct.
9559     MEHandler.generateAllInfo(CombinedInfo, MappedVarSet);
9560 
9561     TargetDataInfo Info;
9562     // Fill up the arrays and create the arguments.
9563     emitOffloadingArrays(CGF, CombinedInfo, Info);
9564     emitOffloadingArraysArgument(CGF, Info.BasePointersArray,
9565                                  Info.PointersArray, Info.SizesArray,
9566                                  Info.MapTypesArray, Info.MappersArray, Info);
9567     InputInfo.NumberOfTargetItems = Info.NumberOfPtrs;
9568     InputInfo.BasePointersArray =
9569         Address(Info.BasePointersArray, CGM.getPointerAlign());
9570     InputInfo.PointersArray =
9571         Address(Info.PointersArray, CGM.getPointerAlign());
9572     InputInfo.SizesArray = Address(Info.SizesArray, CGM.getPointerAlign());
9573     InputInfo.MappersArray = Address(Info.MappersArray, CGM.getPointerAlign());
9574     MapTypesArray = Info.MapTypesArray;
9575     if (RequiresOuterTask)
9576       CGF.EmitOMPTargetTaskBasedDirective(D, ThenGen, InputInfo);
9577     else
9578       emitInlinedDirective(CGF, D.getDirectiveKind(), ThenGen);
9579   };
9580 
9581   auto &&TargetElseGen = [this, &ElseGen, &D, RequiresOuterTask](
9582                              CodeGenFunction &CGF, PrePostActionTy &) {
9583     if (RequiresOuterTask) {
9584       CodeGenFunction::OMPTargetDataInfo InputInfo;
9585       CGF.EmitOMPTargetTaskBasedDirective(D, ElseGen, InputInfo);
9586     } else {
9587       emitInlinedDirective(CGF, D.getDirectiveKind(), ElseGen);
9588     }
9589   };
9590 
9591   // If we have a target function ID it means that we need to support
9592   // offloading, otherwise, just execute on the host. We need to execute on host
9593   // regardless of the conditional in the if clause if, e.g., the user do not
9594   // specify target triples.
9595   if (OutlinedFnID) {
9596     if (IfCond) {
9597       emitIfClause(CGF, IfCond, TargetThenGen, TargetElseGen);
9598     } else {
9599       RegionCodeGenTy ThenRCG(TargetThenGen);
9600       ThenRCG(CGF);
9601     }
9602   } else {
9603     RegionCodeGenTy ElseRCG(TargetElseGen);
9604     ElseRCG(CGF);
9605   }
9606 }
9607 
9608 void CGOpenMPRuntime::scanForTargetRegionsFunctions(const Stmt *S,
9609                                                     StringRef ParentName) {
9610   if (!S)
9611     return;
9612 
9613   // Codegen OMP target directives that offload compute to the device.
9614   bool RequiresDeviceCodegen =
9615       isa<OMPExecutableDirective>(S) &&
9616       isOpenMPTargetExecutionDirective(
9617           cast<OMPExecutableDirective>(S)->getDirectiveKind());
9618 
9619   if (RequiresDeviceCodegen) {
9620     const auto &E = *cast<OMPExecutableDirective>(S);
9621     unsigned DeviceID;
9622     unsigned FileID;
9623     unsigned Line;
9624     getTargetEntryUniqueInfo(CGM.getContext(), E.getBeginLoc(), DeviceID,
9625                              FileID, Line);
9626 
9627     // Is this a target region that should not be emitted as an entry point? If
9628     // so just signal we are done with this target region.
9629     if (!OffloadEntriesInfoManager.hasTargetRegionEntryInfo(DeviceID, FileID,
9630                                                             ParentName, Line))
9631       return;
9632 
9633     switch (E.getDirectiveKind()) {
9634     case OMPD_target:
9635       CodeGenFunction::EmitOMPTargetDeviceFunction(CGM, ParentName,
9636                                                    cast<OMPTargetDirective>(E));
9637       break;
9638     case OMPD_target_parallel:
9639       CodeGenFunction::EmitOMPTargetParallelDeviceFunction(
9640           CGM, ParentName, cast<OMPTargetParallelDirective>(E));
9641       break;
9642     case OMPD_target_teams:
9643       CodeGenFunction::EmitOMPTargetTeamsDeviceFunction(
9644           CGM, ParentName, cast<OMPTargetTeamsDirective>(E));
9645       break;
9646     case OMPD_target_teams_distribute:
9647       CodeGenFunction::EmitOMPTargetTeamsDistributeDeviceFunction(
9648           CGM, ParentName, cast<OMPTargetTeamsDistributeDirective>(E));
9649       break;
9650     case OMPD_target_teams_distribute_simd:
9651       CodeGenFunction::EmitOMPTargetTeamsDistributeSimdDeviceFunction(
9652           CGM, ParentName, cast<OMPTargetTeamsDistributeSimdDirective>(E));
9653       break;
9654     case OMPD_target_parallel_for:
9655       CodeGenFunction::EmitOMPTargetParallelForDeviceFunction(
9656           CGM, ParentName, cast<OMPTargetParallelForDirective>(E));
9657       break;
9658     case OMPD_target_parallel_for_simd:
9659       CodeGenFunction::EmitOMPTargetParallelForSimdDeviceFunction(
9660           CGM, ParentName, cast<OMPTargetParallelForSimdDirective>(E));
9661       break;
9662     case OMPD_target_simd:
9663       CodeGenFunction::EmitOMPTargetSimdDeviceFunction(
9664           CGM, ParentName, cast<OMPTargetSimdDirective>(E));
9665       break;
9666     case OMPD_target_teams_distribute_parallel_for:
9667       CodeGenFunction::EmitOMPTargetTeamsDistributeParallelForDeviceFunction(
9668           CGM, ParentName,
9669           cast<OMPTargetTeamsDistributeParallelForDirective>(E));
9670       break;
9671     case OMPD_target_teams_distribute_parallel_for_simd:
9672       CodeGenFunction::
9673           EmitOMPTargetTeamsDistributeParallelForSimdDeviceFunction(
9674               CGM, ParentName,
9675               cast<OMPTargetTeamsDistributeParallelForSimdDirective>(E));
9676       break;
9677     case OMPD_parallel:
9678     case OMPD_for:
9679     case OMPD_parallel_for:
9680     case OMPD_parallel_master:
9681     case OMPD_parallel_sections:
9682     case OMPD_for_simd:
9683     case OMPD_parallel_for_simd:
9684     case OMPD_cancel:
9685     case OMPD_cancellation_point:
9686     case OMPD_ordered:
9687     case OMPD_threadprivate:
9688     case OMPD_allocate:
9689     case OMPD_task:
9690     case OMPD_simd:
9691     case OMPD_sections:
9692     case OMPD_section:
9693     case OMPD_single:
9694     case OMPD_master:
9695     case OMPD_critical:
9696     case OMPD_taskyield:
9697     case OMPD_barrier:
9698     case OMPD_taskwait:
9699     case OMPD_taskgroup:
9700     case OMPD_atomic:
9701     case OMPD_flush:
9702     case OMPD_depobj:
9703     case OMPD_scan:
9704     case OMPD_teams:
9705     case OMPD_target_data:
9706     case OMPD_target_exit_data:
9707     case OMPD_target_enter_data:
9708     case OMPD_distribute:
9709     case OMPD_distribute_simd:
9710     case OMPD_distribute_parallel_for:
9711     case OMPD_distribute_parallel_for_simd:
9712     case OMPD_teams_distribute:
9713     case OMPD_teams_distribute_simd:
9714     case OMPD_teams_distribute_parallel_for:
9715     case OMPD_teams_distribute_parallel_for_simd:
9716     case OMPD_target_update:
9717     case OMPD_declare_simd:
9718     case OMPD_declare_variant:
9719     case OMPD_begin_declare_variant:
9720     case OMPD_end_declare_variant:
9721     case OMPD_declare_target:
9722     case OMPD_end_declare_target:
9723     case OMPD_declare_reduction:
9724     case OMPD_declare_mapper:
9725     case OMPD_taskloop:
9726     case OMPD_taskloop_simd:
9727     case OMPD_master_taskloop:
9728     case OMPD_master_taskloop_simd:
9729     case OMPD_parallel_master_taskloop:
9730     case OMPD_parallel_master_taskloop_simd:
9731     case OMPD_requires:
9732     case OMPD_unknown:
9733     default:
9734       llvm_unreachable("Unknown target directive for OpenMP device codegen.");
9735     }
9736     return;
9737   }
9738 
9739   if (const auto *E = dyn_cast<OMPExecutableDirective>(S)) {
9740     if (!E->hasAssociatedStmt() || !E->getAssociatedStmt())
9741       return;
9742 
9743     scanForTargetRegionsFunctions(
9744         E->getInnermostCapturedStmt()->getCapturedStmt(), ParentName);
9745     return;
9746   }
9747 
9748   // If this is a lambda function, look into its body.
9749   if (const auto *L = dyn_cast<LambdaExpr>(S))
9750     S = L->getBody();
9751 
9752   // Keep looking for target regions recursively.
9753   for (const Stmt *II : S->children())
9754     scanForTargetRegionsFunctions(II, ParentName);
9755 }
9756 
9757 bool CGOpenMPRuntime::emitTargetFunctions(GlobalDecl GD) {
9758   // If emitting code for the host, we do not process FD here. Instead we do
9759   // the normal code generation.
9760   if (!CGM.getLangOpts().OpenMPIsDevice) {
9761     if (const auto *FD = dyn_cast<FunctionDecl>(GD.getDecl())) {
9762       Optional<OMPDeclareTargetDeclAttr::DevTypeTy> DevTy =
9763           OMPDeclareTargetDeclAttr::getDeviceType(FD);
9764       // Do not emit device_type(nohost) functions for the host.
9765       if (DevTy && *DevTy == OMPDeclareTargetDeclAttr::DT_NoHost)
9766         return true;
9767     }
9768     return false;
9769   }
9770 
9771   const ValueDecl *VD = cast<ValueDecl>(GD.getDecl());
9772   // Try to detect target regions in the function.
9773   if (const auto *FD = dyn_cast<FunctionDecl>(VD)) {
9774     StringRef Name = CGM.getMangledName(GD);
9775     scanForTargetRegionsFunctions(FD->getBody(), Name);
9776     Optional<OMPDeclareTargetDeclAttr::DevTypeTy> DevTy =
9777         OMPDeclareTargetDeclAttr::getDeviceType(FD);
9778     // Do not emit device_type(nohost) functions for the host.
9779     if (DevTy && *DevTy == OMPDeclareTargetDeclAttr::DT_Host)
9780       return true;
9781   }
9782 
9783   // Do not to emit function if it is not marked as declare target.
9784   return !OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD) &&
9785          AlreadyEmittedTargetDecls.count(VD) == 0;
9786 }
9787 
9788 bool CGOpenMPRuntime::emitTargetGlobalVariable(GlobalDecl GD) {
9789   if (!CGM.getLangOpts().OpenMPIsDevice)
9790     return false;
9791 
9792   // Check if there are Ctors/Dtors in this declaration and look for target
9793   // regions in it. We use the complete variant to produce the kernel name
9794   // mangling.
9795   QualType RDTy = cast<VarDecl>(GD.getDecl())->getType();
9796   if (const auto *RD = RDTy->getBaseElementTypeUnsafe()->getAsCXXRecordDecl()) {
9797     for (const CXXConstructorDecl *Ctor : RD->ctors()) {
9798       StringRef ParentName =
9799           CGM.getMangledName(GlobalDecl(Ctor, Ctor_Complete));
9800       scanForTargetRegionsFunctions(Ctor->getBody(), ParentName);
9801     }
9802     if (const CXXDestructorDecl *Dtor = RD->getDestructor()) {
9803       StringRef ParentName =
9804           CGM.getMangledName(GlobalDecl(Dtor, Dtor_Complete));
9805       scanForTargetRegionsFunctions(Dtor->getBody(), ParentName);
9806     }
9807   }
9808 
9809   // Do not to emit variable if it is not marked as declare target.
9810   llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res =
9811       OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(
9812           cast<VarDecl>(GD.getDecl()));
9813   if (!Res || *Res == OMPDeclareTargetDeclAttr::MT_Link ||
9814       (*Res == OMPDeclareTargetDeclAttr::MT_To &&
9815        HasRequiresUnifiedSharedMemory)) {
9816     DeferredGlobalVariables.insert(cast<VarDecl>(GD.getDecl()));
9817     return true;
9818   }
9819   return false;
9820 }
9821 
9822 llvm::Constant *
9823 CGOpenMPRuntime::registerTargetFirstprivateCopy(CodeGenFunction &CGF,
9824                                                 const VarDecl *VD) {
9825   assert(VD->getType().isConstant(CGM.getContext()) &&
9826          "Expected constant variable.");
9827   StringRef VarName;
9828   llvm::Constant *Addr;
9829   llvm::GlobalValue::LinkageTypes Linkage;
9830   QualType Ty = VD->getType();
9831   SmallString<128> Buffer;
9832   {
9833     unsigned DeviceID;
9834     unsigned FileID;
9835     unsigned Line;
9836     getTargetEntryUniqueInfo(CGM.getContext(), VD->getLocation(), DeviceID,
9837                              FileID, Line);
9838     llvm::raw_svector_ostream OS(Buffer);
9839     OS << "__omp_offloading_firstprivate_" << llvm::format("_%x", DeviceID)
9840        << llvm::format("_%x_", FileID) << VD->getName() << "_l" << Line;
9841     VarName = OS.str();
9842   }
9843   Linkage = llvm::GlobalValue::InternalLinkage;
9844   Addr =
9845       getOrCreateInternalVariable(CGM.getTypes().ConvertTypeForMem(Ty), VarName,
9846                                   getDefaultFirstprivateAddressSpace());
9847   cast<llvm::GlobalValue>(Addr)->setLinkage(Linkage);
9848   CharUnits VarSize = CGM.getContext().getTypeSizeInChars(Ty);
9849   CGM.addCompilerUsedGlobal(cast<llvm::GlobalValue>(Addr));
9850   OffloadEntriesInfoManager.registerDeviceGlobalVarEntryInfo(
9851       VarName, Addr, VarSize,
9852       OffloadEntriesInfoManagerTy::OMPTargetGlobalVarEntryTo, Linkage);
9853   return Addr;
9854 }
9855 
9856 void CGOpenMPRuntime::registerTargetGlobalVariable(const VarDecl *VD,
9857                                                    llvm::Constant *Addr) {
9858   if (CGM.getLangOpts().OMPTargetTriples.empty() &&
9859       !CGM.getLangOpts().OpenMPIsDevice)
9860     return;
9861   llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res =
9862       OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD);
9863   if (!Res) {
9864     if (CGM.getLangOpts().OpenMPIsDevice) {
9865       // Register non-target variables being emitted in device code (debug info
9866       // may cause this).
9867       StringRef VarName = CGM.getMangledName(VD);
9868       EmittedNonTargetVariables.try_emplace(VarName, Addr);
9869     }
9870     return;
9871   }
9872   // Register declare target variables.
9873   OffloadEntriesInfoManagerTy::OMPTargetGlobalVarEntryKind Flags;
9874   StringRef VarName;
9875   CharUnits VarSize;
9876   llvm::GlobalValue::LinkageTypes Linkage;
9877 
9878   if (*Res == OMPDeclareTargetDeclAttr::MT_To &&
9879       !HasRequiresUnifiedSharedMemory) {
9880     Flags = OffloadEntriesInfoManagerTy::OMPTargetGlobalVarEntryTo;
9881     VarName = CGM.getMangledName(VD);
9882     if (VD->hasDefinition(CGM.getContext()) != VarDecl::DeclarationOnly) {
9883       VarSize = CGM.getContext().getTypeSizeInChars(VD->getType());
9884       assert(!VarSize.isZero() && "Expected non-zero size of the variable");
9885     } else {
9886       VarSize = CharUnits::Zero();
9887     }
9888     Linkage = CGM.getLLVMLinkageVarDefinition(VD, /*IsConstant=*/false);
9889     // Temp solution to prevent optimizations of the internal variables.
9890     if (CGM.getLangOpts().OpenMPIsDevice && !VD->isExternallyVisible()) {
9891       std::string RefName = getName({VarName, "ref"});
9892       if (!CGM.GetGlobalValue(RefName)) {
9893         llvm::Constant *AddrRef =
9894             getOrCreateInternalVariable(Addr->getType(), RefName);
9895         auto *GVAddrRef = cast<llvm::GlobalVariable>(AddrRef);
9896         GVAddrRef->setConstant(/*Val=*/true);
9897         GVAddrRef->setLinkage(llvm::GlobalValue::InternalLinkage);
9898         GVAddrRef->setInitializer(Addr);
9899         CGM.addCompilerUsedGlobal(GVAddrRef);
9900       }
9901     }
9902   } else {
9903     assert(((*Res == OMPDeclareTargetDeclAttr::MT_Link) ||
9904             (*Res == OMPDeclareTargetDeclAttr::MT_To &&
9905              HasRequiresUnifiedSharedMemory)) &&
9906            "Declare target attribute must link or to with unified memory.");
9907     if (*Res == OMPDeclareTargetDeclAttr::MT_Link)
9908       Flags = OffloadEntriesInfoManagerTy::OMPTargetGlobalVarEntryLink;
9909     else
9910       Flags = OffloadEntriesInfoManagerTy::OMPTargetGlobalVarEntryTo;
9911 
9912     if (CGM.getLangOpts().OpenMPIsDevice) {
9913       VarName = Addr->getName();
9914       Addr = nullptr;
9915     } else {
9916       VarName = getAddrOfDeclareTargetVar(VD).getName();
9917       Addr = cast<llvm::Constant>(getAddrOfDeclareTargetVar(VD).getPointer());
9918     }
9919     VarSize = CGM.getPointerSize();
9920     Linkage = llvm::GlobalValue::WeakAnyLinkage;
9921   }
9922 
9923   OffloadEntriesInfoManager.registerDeviceGlobalVarEntryInfo(
9924       VarName, Addr, VarSize, Flags, Linkage);
9925 }
9926 
9927 bool CGOpenMPRuntime::emitTargetGlobal(GlobalDecl GD) {
9928   if (isa<FunctionDecl>(GD.getDecl()) ||
9929       isa<OMPDeclareReductionDecl>(GD.getDecl()))
9930     return emitTargetFunctions(GD);
9931 
9932   return emitTargetGlobalVariable(GD);
9933 }
9934 
9935 void CGOpenMPRuntime::emitDeferredTargetDecls() const {
9936   for (const VarDecl *VD : DeferredGlobalVariables) {
9937     llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res =
9938         OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD);
9939     if (!Res)
9940       continue;
9941     if (*Res == OMPDeclareTargetDeclAttr::MT_To &&
9942         !HasRequiresUnifiedSharedMemory) {
9943       CGM.EmitGlobal(VD);
9944     } else {
9945       assert((*Res == OMPDeclareTargetDeclAttr::MT_Link ||
9946               (*Res == OMPDeclareTargetDeclAttr::MT_To &&
9947                HasRequiresUnifiedSharedMemory)) &&
9948              "Expected link clause or to clause with unified memory.");
9949       (void)CGM.getOpenMPRuntime().getAddrOfDeclareTargetVar(VD);
9950     }
9951   }
9952 }
9953 
9954 void CGOpenMPRuntime::adjustTargetSpecificDataForLambdas(
9955     CodeGenFunction &CGF, const OMPExecutableDirective &D) const {
9956   assert(isOpenMPTargetExecutionDirective(D.getDirectiveKind()) &&
9957          " Expected target-based directive.");
9958 }
9959 
9960 void CGOpenMPRuntime::processRequiresDirective(const OMPRequiresDecl *D) {
9961   for (const OMPClause *Clause : D->clauselists()) {
9962     if (Clause->getClauseKind() == OMPC_unified_shared_memory) {
9963       HasRequiresUnifiedSharedMemory = true;
9964     } else if (const auto *AC =
9965                    dyn_cast<OMPAtomicDefaultMemOrderClause>(Clause)) {
9966       switch (AC->getAtomicDefaultMemOrderKind()) {
9967       case OMPC_ATOMIC_DEFAULT_MEM_ORDER_acq_rel:
9968         RequiresAtomicOrdering = llvm::AtomicOrdering::AcquireRelease;
9969         break;
9970       case OMPC_ATOMIC_DEFAULT_MEM_ORDER_seq_cst:
9971         RequiresAtomicOrdering = llvm::AtomicOrdering::SequentiallyConsistent;
9972         break;
9973       case OMPC_ATOMIC_DEFAULT_MEM_ORDER_relaxed:
9974         RequiresAtomicOrdering = llvm::AtomicOrdering::Monotonic;
9975         break;
9976       case OMPC_ATOMIC_DEFAULT_MEM_ORDER_unknown:
9977         break;
9978       }
9979     }
9980   }
9981 }
9982 
9983 llvm::AtomicOrdering CGOpenMPRuntime::getDefaultMemoryOrdering() const {
9984   return RequiresAtomicOrdering;
9985 }
9986 
9987 bool CGOpenMPRuntime::hasAllocateAttributeForGlobalVar(const VarDecl *VD,
9988                                                        LangAS &AS) {
9989   if (!VD || !VD->hasAttr<OMPAllocateDeclAttr>())
9990     return false;
9991   const auto *A = VD->getAttr<OMPAllocateDeclAttr>();
9992   switch(A->getAllocatorType()) {
9993   case OMPAllocateDeclAttr::OMPNullMemAlloc:
9994   case OMPAllocateDeclAttr::OMPDefaultMemAlloc:
9995   // Not supported, fallback to the default mem space.
9996   case OMPAllocateDeclAttr::OMPLargeCapMemAlloc:
9997   case OMPAllocateDeclAttr::OMPCGroupMemAlloc:
9998   case OMPAllocateDeclAttr::OMPHighBWMemAlloc:
9999   case OMPAllocateDeclAttr::OMPLowLatMemAlloc:
10000   case OMPAllocateDeclAttr::OMPThreadMemAlloc:
10001   case OMPAllocateDeclAttr::OMPConstMemAlloc:
10002   case OMPAllocateDeclAttr::OMPPTeamMemAlloc:
10003     AS = LangAS::Default;
10004     return true;
10005   case OMPAllocateDeclAttr::OMPUserDefinedMemAlloc:
10006     llvm_unreachable("Expected predefined allocator for the variables with the "
10007                      "static storage.");
10008   }
10009   return false;
10010 }
10011 
10012 bool CGOpenMPRuntime::hasRequiresUnifiedSharedMemory() const {
10013   return HasRequiresUnifiedSharedMemory;
10014 }
10015 
10016 CGOpenMPRuntime::DisableAutoDeclareTargetRAII::DisableAutoDeclareTargetRAII(
10017     CodeGenModule &CGM)
10018     : CGM(CGM) {
10019   if (CGM.getLangOpts().OpenMPIsDevice) {
10020     SavedShouldMarkAsGlobal = CGM.getOpenMPRuntime().ShouldMarkAsGlobal;
10021     CGM.getOpenMPRuntime().ShouldMarkAsGlobal = false;
10022   }
10023 }
10024 
10025 CGOpenMPRuntime::DisableAutoDeclareTargetRAII::~DisableAutoDeclareTargetRAII() {
10026   if (CGM.getLangOpts().OpenMPIsDevice)
10027     CGM.getOpenMPRuntime().ShouldMarkAsGlobal = SavedShouldMarkAsGlobal;
10028 }
10029 
10030 bool CGOpenMPRuntime::markAsGlobalTarget(GlobalDecl GD) {
10031   if (!CGM.getLangOpts().OpenMPIsDevice || !ShouldMarkAsGlobal)
10032     return true;
10033 
10034   const auto *D = cast<FunctionDecl>(GD.getDecl());
10035   // Do not to emit function if it is marked as declare target as it was already
10036   // emitted.
10037   if (OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(D)) {
10038     if (D->hasBody() && AlreadyEmittedTargetDecls.count(D) == 0) {
10039       if (auto *F = dyn_cast_or_null<llvm::Function>(
10040               CGM.GetGlobalValue(CGM.getMangledName(GD))))
10041         return !F->isDeclaration();
10042       return false;
10043     }
10044     return true;
10045   }
10046 
10047   return !AlreadyEmittedTargetDecls.insert(D).second;
10048 }
10049 
10050 llvm::Function *CGOpenMPRuntime::emitRequiresDirectiveRegFun() {
10051   // If we don't have entries or if we are emitting code for the device, we
10052   // don't need to do anything.
10053   if (CGM.getLangOpts().OMPTargetTriples.empty() ||
10054       CGM.getLangOpts().OpenMPSimd || CGM.getLangOpts().OpenMPIsDevice ||
10055       (OffloadEntriesInfoManager.empty() &&
10056        !HasEmittedDeclareTargetRegion &&
10057        !HasEmittedTargetRegion))
10058     return nullptr;
10059 
10060   // Create and register the function that handles the requires directives.
10061   ASTContext &C = CGM.getContext();
10062 
10063   llvm::Function *RequiresRegFn;
10064   {
10065     CodeGenFunction CGF(CGM);
10066     const auto &FI = CGM.getTypes().arrangeNullaryFunction();
10067     llvm::FunctionType *FTy = CGM.getTypes().GetFunctionType(FI);
10068     std::string ReqName = getName({"omp_offloading", "requires_reg"});
10069     RequiresRegFn = CGM.CreateGlobalInitOrCleanUpFunction(FTy, ReqName, FI);
10070     CGF.StartFunction(GlobalDecl(), C.VoidTy, RequiresRegFn, FI, {});
10071     OpenMPOffloadingRequiresDirFlags Flags = OMP_REQ_NONE;
10072     // TODO: check for other requires clauses.
10073     // The requires directive takes effect only when a target region is
10074     // present in the compilation unit. Otherwise it is ignored and not
10075     // passed to the runtime. This avoids the runtime from throwing an error
10076     // for mismatching requires clauses across compilation units that don't
10077     // contain at least 1 target region.
10078     assert((HasEmittedTargetRegion ||
10079             HasEmittedDeclareTargetRegion ||
10080             !OffloadEntriesInfoManager.empty()) &&
10081            "Target or declare target region expected.");
10082     if (HasRequiresUnifiedSharedMemory)
10083       Flags = OMP_REQ_UNIFIED_SHARED_MEMORY;
10084     CGF.EmitRuntimeCall(OMPBuilder.getOrCreateRuntimeFunction(
10085                             CGM.getModule(), OMPRTL___tgt_register_requires),
10086                         llvm::ConstantInt::get(CGM.Int64Ty, Flags));
10087     CGF.FinishFunction();
10088   }
10089   return RequiresRegFn;
10090 }
10091 
10092 void CGOpenMPRuntime::emitTeamsCall(CodeGenFunction &CGF,
10093                                     const OMPExecutableDirective &D,
10094                                     SourceLocation Loc,
10095                                     llvm::Function *OutlinedFn,
10096                                     ArrayRef<llvm::Value *> CapturedVars) {
10097   if (!CGF.HaveInsertPoint())
10098     return;
10099 
10100   llvm::Value *RTLoc = emitUpdateLocation(CGF, Loc);
10101   CodeGenFunction::RunCleanupsScope Scope(CGF);
10102 
10103   // Build call __kmpc_fork_teams(loc, n, microtask, var1, .., varn);
10104   llvm::Value *Args[] = {
10105       RTLoc,
10106       CGF.Builder.getInt32(CapturedVars.size()), // Number of captured vars
10107       CGF.Builder.CreateBitCast(OutlinedFn, getKmpc_MicroPointerTy())};
10108   llvm::SmallVector<llvm::Value *, 16> RealArgs;
10109   RealArgs.append(std::begin(Args), std::end(Args));
10110   RealArgs.append(CapturedVars.begin(), CapturedVars.end());
10111 
10112   llvm::FunctionCallee RTLFn = OMPBuilder.getOrCreateRuntimeFunction(
10113       CGM.getModule(), OMPRTL___kmpc_fork_teams);
10114   CGF.EmitRuntimeCall(RTLFn, RealArgs);
10115 }
10116 
10117 void CGOpenMPRuntime::emitNumTeamsClause(CodeGenFunction &CGF,
10118                                          const Expr *NumTeams,
10119                                          const Expr *ThreadLimit,
10120                                          SourceLocation Loc) {
10121   if (!CGF.HaveInsertPoint())
10122     return;
10123 
10124   llvm::Value *RTLoc = emitUpdateLocation(CGF, Loc);
10125 
10126   llvm::Value *NumTeamsVal =
10127       NumTeams
10128           ? CGF.Builder.CreateIntCast(CGF.EmitScalarExpr(NumTeams),
10129                                       CGF.CGM.Int32Ty, /* isSigned = */ true)
10130           : CGF.Builder.getInt32(0);
10131 
10132   llvm::Value *ThreadLimitVal =
10133       ThreadLimit
10134           ? CGF.Builder.CreateIntCast(CGF.EmitScalarExpr(ThreadLimit),
10135                                       CGF.CGM.Int32Ty, /* isSigned = */ true)
10136           : CGF.Builder.getInt32(0);
10137 
10138   // Build call __kmpc_push_num_teamss(&loc, global_tid, num_teams, thread_limit)
10139   llvm::Value *PushNumTeamsArgs[] = {RTLoc, getThreadID(CGF, Loc), NumTeamsVal,
10140                                      ThreadLimitVal};
10141   CGF.EmitRuntimeCall(OMPBuilder.getOrCreateRuntimeFunction(
10142                           CGM.getModule(), OMPRTL___kmpc_push_num_teams),
10143                       PushNumTeamsArgs);
10144 }
10145 
10146 void CGOpenMPRuntime::emitTargetDataCalls(
10147     CodeGenFunction &CGF, const OMPExecutableDirective &D, const Expr *IfCond,
10148     const Expr *Device, const RegionCodeGenTy &CodeGen, TargetDataInfo &Info) {
10149   if (!CGF.HaveInsertPoint())
10150     return;
10151 
10152   // Action used to replace the default codegen action and turn privatization
10153   // off.
10154   PrePostActionTy NoPrivAction;
10155 
10156   // Generate the code for the opening of the data environment. Capture all the
10157   // arguments of the runtime call by reference because they are used in the
10158   // closing of the region.
10159   auto &&BeginThenGen = [this, &D, Device, &Info,
10160                          &CodeGen](CodeGenFunction &CGF, PrePostActionTy &) {
10161     // Fill up the arrays with all the mapped variables.
10162     MappableExprsHandler::MapCombinedInfoTy CombinedInfo;
10163 
10164     // Get map clause information.
10165     MappableExprsHandler MEHandler(D, CGF);
10166     MEHandler.generateAllInfo(CombinedInfo);
10167 
10168     // Fill up the arrays and create the arguments.
10169     emitOffloadingArrays(CGF, CombinedInfo, Info);
10170 
10171     llvm::Value *BasePointersArrayArg = nullptr;
10172     llvm::Value *PointersArrayArg = nullptr;
10173     llvm::Value *SizesArrayArg = nullptr;
10174     llvm::Value *MapTypesArrayArg = nullptr;
10175     llvm::Value *MappersArrayArg = nullptr;
10176     emitOffloadingArraysArgument(CGF, BasePointersArrayArg, PointersArrayArg,
10177                                  SizesArrayArg, MapTypesArrayArg,
10178                                  MappersArrayArg, Info);
10179 
10180     // Emit device ID if any.
10181     llvm::Value *DeviceID = nullptr;
10182     if (Device) {
10183       DeviceID = CGF.Builder.CreateIntCast(CGF.EmitScalarExpr(Device),
10184                                            CGF.Int64Ty, /*isSigned=*/true);
10185     } else {
10186       DeviceID = CGF.Builder.getInt64(OMP_DEVICEID_UNDEF);
10187     }
10188 
10189     // Emit the number of elements in the offloading arrays.
10190     llvm::Value *PointerNum = CGF.Builder.getInt32(Info.NumberOfPtrs);
10191 
10192     llvm::Value *OffloadingArgs[] = {
10193         DeviceID,      PointerNum,       BasePointersArrayArg, PointersArrayArg,
10194         SizesArrayArg, MapTypesArrayArg, MappersArrayArg};
10195     CGF.EmitRuntimeCall(
10196         OMPBuilder.getOrCreateRuntimeFunction(
10197             CGM.getModule(), OMPRTL___tgt_target_data_begin_mapper),
10198         OffloadingArgs);
10199 
10200     // If device pointer privatization is required, emit the body of the region
10201     // here. It will have to be duplicated: with and without privatization.
10202     if (!Info.CaptureDeviceAddrMap.empty())
10203       CodeGen(CGF);
10204   };
10205 
10206   // Generate code for the closing of the data region.
10207   auto &&EndThenGen = [this, Device, &Info](CodeGenFunction &CGF,
10208                                             PrePostActionTy &) {
10209     assert(Info.isValid() && "Invalid data environment closing arguments.");
10210 
10211     llvm::Value *BasePointersArrayArg = nullptr;
10212     llvm::Value *PointersArrayArg = nullptr;
10213     llvm::Value *SizesArrayArg = nullptr;
10214     llvm::Value *MapTypesArrayArg = nullptr;
10215     llvm::Value *MappersArrayArg = nullptr;
10216     emitOffloadingArraysArgument(CGF, BasePointersArrayArg, PointersArrayArg,
10217                                  SizesArrayArg, MapTypesArrayArg,
10218                                  MappersArrayArg, Info);
10219 
10220     // Emit device ID if any.
10221     llvm::Value *DeviceID = nullptr;
10222     if (Device) {
10223       DeviceID = CGF.Builder.CreateIntCast(CGF.EmitScalarExpr(Device),
10224                                            CGF.Int64Ty, /*isSigned=*/true);
10225     } else {
10226       DeviceID = CGF.Builder.getInt64(OMP_DEVICEID_UNDEF);
10227     }
10228 
10229     // Emit the number of elements in the offloading arrays.
10230     llvm::Value *PointerNum = CGF.Builder.getInt32(Info.NumberOfPtrs);
10231 
10232     llvm::Value *OffloadingArgs[] = {
10233         DeviceID,      PointerNum,       BasePointersArrayArg, PointersArrayArg,
10234         SizesArrayArg, MapTypesArrayArg, MappersArrayArg};
10235     CGF.EmitRuntimeCall(
10236         OMPBuilder.getOrCreateRuntimeFunction(
10237             CGM.getModule(), OMPRTL___tgt_target_data_end_mapper),
10238         OffloadingArgs);
10239   };
10240 
10241   // If we need device pointer privatization, we need to emit the body of the
10242   // region with no privatization in the 'else' branch of the conditional.
10243   // Otherwise, we don't have to do anything.
10244   auto &&BeginElseGen = [&Info, &CodeGen, &NoPrivAction](CodeGenFunction &CGF,
10245                                                          PrePostActionTy &) {
10246     if (!Info.CaptureDeviceAddrMap.empty()) {
10247       CodeGen.setAction(NoPrivAction);
10248       CodeGen(CGF);
10249     }
10250   };
10251 
10252   // We don't have to do anything to close the region if the if clause evaluates
10253   // to false.
10254   auto &&EndElseGen = [](CodeGenFunction &CGF, PrePostActionTy &) {};
10255 
10256   if (IfCond) {
10257     emitIfClause(CGF, IfCond, BeginThenGen, BeginElseGen);
10258   } else {
10259     RegionCodeGenTy RCG(BeginThenGen);
10260     RCG(CGF);
10261   }
10262 
10263   // If we don't require privatization of device pointers, we emit the body in
10264   // between the runtime calls. This avoids duplicating the body code.
10265   if (Info.CaptureDeviceAddrMap.empty()) {
10266     CodeGen.setAction(NoPrivAction);
10267     CodeGen(CGF);
10268   }
10269 
10270   if (IfCond) {
10271     emitIfClause(CGF, IfCond, EndThenGen, EndElseGen);
10272   } else {
10273     RegionCodeGenTy RCG(EndThenGen);
10274     RCG(CGF);
10275   }
10276 }
10277 
10278 void CGOpenMPRuntime::emitTargetDataStandAloneCall(
10279     CodeGenFunction &CGF, const OMPExecutableDirective &D, const Expr *IfCond,
10280     const Expr *Device) {
10281   if (!CGF.HaveInsertPoint())
10282     return;
10283 
10284   assert((isa<OMPTargetEnterDataDirective>(D) ||
10285           isa<OMPTargetExitDataDirective>(D) ||
10286           isa<OMPTargetUpdateDirective>(D)) &&
10287          "Expecting either target enter, exit data, or update directives.");
10288 
10289   CodeGenFunction::OMPTargetDataInfo InputInfo;
10290   llvm::Value *MapTypesArray = nullptr;
10291   // Generate the code for the opening of the data environment.
10292   auto &&ThenGen = [this, &D, Device, &InputInfo,
10293                     &MapTypesArray](CodeGenFunction &CGF, PrePostActionTy &) {
10294     // Emit device ID if any.
10295     llvm::Value *DeviceID = nullptr;
10296     if (Device) {
10297       DeviceID = CGF.Builder.CreateIntCast(CGF.EmitScalarExpr(Device),
10298                                            CGF.Int64Ty, /*isSigned=*/true);
10299     } else {
10300       DeviceID = CGF.Builder.getInt64(OMP_DEVICEID_UNDEF);
10301     }
10302 
10303     // Emit the number of elements in the offloading arrays.
10304     llvm::Constant *PointerNum =
10305         CGF.Builder.getInt32(InputInfo.NumberOfTargetItems);
10306 
10307     llvm::Value *OffloadingArgs[] = {DeviceID,
10308                                      PointerNum,
10309                                      InputInfo.BasePointersArray.getPointer(),
10310                                      InputInfo.PointersArray.getPointer(),
10311                                      InputInfo.SizesArray.getPointer(),
10312                                      MapTypesArray,
10313                                      InputInfo.MappersArray.getPointer()};
10314 
10315     // Select the right runtime function call for each standalone
10316     // directive.
10317     const bool HasNowait = D.hasClausesOfKind<OMPNowaitClause>();
10318     RuntimeFunction RTLFn;
10319     switch (D.getDirectiveKind()) {
10320     case OMPD_target_enter_data:
10321       RTLFn = HasNowait ? OMPRTL___tgt_target_data_begin_nowait_mapper
10322                         : OMPRTL___tgt_target_data_begin_mapper;
10323       break;
10324     case OMPD_target_exit_data:
10325       RTLFn = HasNowait ? OMPRTL___tgt_target_data_end_nowait_mapper
10326                         : OMPRTL___tgt_target_data_end_mapper;
10327       break;
10328     case OMPD_target_update:
10329       RTLFn = HasNowait ? OMPRTL___tgt_target_data_update_nowait_mapper
10330                         : OMPRTL___tgt_target_data_update_mapper;
10331       break;
10332     case OMPD_parallel:
10333     case OMPD_for:
10334     case OMPD_parallel_for:
10335     case OMPD_parallel_master:
10336     case OMPD_parallel_sections:
10337     case OMPD_for_simd:
10338     case OMPD_parallel_for_simd:
10339     case OMPD_cancel:
10340     case OMPD_cancellation_point:
10341     case OMPD_ordered:
10342     case OMPD_threadprivate:
10343     case OMPD_allocate:
10344     case OMPD_task:
10345     case OMPD_simd:
10346     case OMPD_sections:
10347     case OMPD_section:
10348     case OMPD_single:
10349     case OMPD_master:
10350     case OMPD_critical:
10351     case OMPD_taskyield:
10352     case OMPD_barrier:
10353     case OMPD_taskwait:
10354     case OMPD_taskgroup:
10355     case OMPD_atomic:
10356     case OMPD_flush:
10357     case OMPD_depobj:
10358     case OMPD_scan:
10359     case OMPD_teams:
10360     case OMPD_target_data:
10361     case OMPD_distribute:
10362     case OMPD_distribute_simd:
10363     case OMPD_distribute_parallel_for:
10364     case OMPD_distribute_parallel_for_simd:
10365     case OMPD_teams_distribute:
10366     case OMPD_teams_distribute_simd:
10367     case OMPD_teams_distribute_parallel_for:
10368     case OMPD_teams_distribute_parallel_for_simd:
10369     case OMPD_declare_simd:
10370     case OMPD_declare_variant:
10371     case OMPD_begin_declare_variant:
10372     case OMPD_end_declare_variant:
10373     case OMPD_declare_target:
10374     case OMPD_end_declare_target:
10375     case OMPD_declare_reduction:
10376     case OMPD_declare_mapper:
10377     case OMPD_taskloop:
10378     case OMPD_taskloop_simd:
10379     case OMPD_master_taskloop:
10380     case OMPD_master_taskloop_simd:
10381     case OMPD_parallel_master_taskloop:
10382     case OMPD_parallel_master_taskloop_simd:
10383     case OMPD_target:
10384     case OMPD_target_simd:
10385     case OMPD_target_teams_distribute:
10386     case OMPD_target_teams_distribute_simd:
10387     case OMPD_target_teams_distribute_parallel_for:
10388     case OMPD_target_teams_distribute_parallel_for_simd:
10389     case OMPD_target_teams:
10390     case OMPD_target_parallel:
10391     case OMPD_target_parallel_for:
10392     case OMPD_target_parallel_for_simd:
10393     case OMPD_requires:
10394     case OMPD_unknown:
10395     default:
10396       llvm_unreachable("Unexpected standalone target data directive.");
10397       break;
10398     }
10399     CGF.EmitRuntimeCall(
10400         OMPBuilder.getOrCreateRuntimeFunction(CGM.getModule(), RTLFn),
10401         OffloadingArgs);
10402   };
10403 
10404   auto &&TargetThenGen = [this, &ThenGen, &D, &InputInfo, &MapTypesArray](
10405                              CodeGenFunction &CGF, PrePostActionTy &) {
10406     // Fill up the arrays with all the mapped variables.
10407     MappableExprsHandler::MapCombinedInfoTy CombinedInfo;
10408 
10409     // Get map clause information.
10410     MappableExprsHandler MEHandler(D, CGF);
10411     MEHandler.generateAllInfo(CombinedInfo);
10412 
10413     TargetDataInfo Info;
10414     // Fill up the arrays and create the arguments.
10415     emitOffloadingArrays(CGF, CombinedInfo, Info);
10416     emitOffloadingArraysArgument(CGF, Info.BasePointersArray,
10417                                  Info.PointersArray, Info.SizesArray,
10418                                  Info.MapTypesArray, Info.MappersArray, Info);
10419     InputInfo.NumberOfTargetItems = Info.NumberOfPtrs;
10420     InputInfo.BasePointersArray =
10421         Address(Info.BasePointersArray, CGM.getPointerAlign());
10422     InputInfo.PointersArray =
10423         Address(Info.PointersArray, CGM.getPointerAlign());
10424     InputInfo.SizesArray =
10425         Address(Info.SizesArray, CGM.getPointerAlign());
10426     InputInfo.MappersArray = Address(Info.MappersArray, CGM.getPointerAlign());
10427     MapTypesArray = Info.MapTypesArray;
10428     if (D.hasClausesOfKind<OMPDependClause>())
10429       CGF.EmitOMPTargetTaskBasedDirective(D, ThenGen, InputInfo);
10430     else
10431       emitInlinedDirective(CGF, D.getDirectiveKind(), ThenGen);
10432   };
10433 
10434   if (IfCond) {
10435     emitIfClause(CGF, IfCond, TargetThenGen,
10436                  [](CodeGenFunction &CGF, PrePostActionTy &) {});
10437   } else {
10438     RegionCodeGenTy ThenRCG(TargetThenGen);
10439     ThenRCG(CGF);
10440   }
10441 }
10442 
10443 namespace {
10444   /// Kind of parameter in a function with 'declare simd' directive.
10445   enum ParamKindTy { LinearWithVarStride, Linear, Uniform, Vector };
10446   /// Attribute set of the parameter.
10447   struct ParamAttrTy {
10448     ParamKindTy Kind = Vector;
10449     llvm::APSInt StrideOrArg;
10450     llvm::APSInt Alignment;
10451   };
10452 } // namespace
10453 
10454 static unsigned evaluateCDTSize(const FunctionDecl *FD,
10455                                 ArrayRef<ParamAttrTy> ParamAttrs) {
10456   // Every vector variant of a SIMD-enabled function has a vector length (VLEN).
10457   // If OpenMP clause "simdlen" is used, the VLEN is the value of the argument
10458   // of that clause. The VLEN value must be power of 2.
10459   // In other case the notion of the function`s "characteristic data type" (CDT)
10460   // is used to compute the vector length.
10461   // CDT is defined in the following order:
10462   //   a) For non-void function, the CDT is the return type.
10463   //   b) If the function has any non-uniform, non-linear parameters, then the
10464   //   CDT is the type of the first such parameter.
10465   //   c) If the CDT determined by a) or b) above is struct, union, or class
10466   //   type which is pass-by-value (except for the type that maps to the
10467   //   built-in complex data type), the characteristic data type is int.
10468   //   d) If none of the above three cases is applicable, the CDT is int.
10469   // The VLEN is then determined based on the CDT and the size of vector
10470   // register of that ISA for which current vector version is generated. The
10471   // VLEN is computed using the formula below:
10472   //   VLEN  = sizeof(vector_register) / sizeof(CDT),
10473   // where vector register size specified in section 3.2.1 Registers and the
10474   // Stack Frame of original AMD64 ABI document.
10475   QualType RetType = FD->getReturnType();
10476   if (RetType.isNull())
10477     return 0;
10478   ASTContext &C = FD->getASTContext();
10479   QualType CDT;
10480   if (!RetType.isNull() && !RetType->isVoidType()) {
10481     CDT = RetType;
10482   } else {
10483     unsigned Offset = 0;
10484     if (const auto *MD = dyn_cast<CXXMethodDecl>(FD)) {
10485       if (ParamAttrs[Offset].Kind == Vector)
10486         CDT = C.getPointerType(C.getRecordType(MD->getParent()));
10487       ++Offset;
10488     }
10489     if (CDT.isNull()) {
10490       for (unsigned I = 0, E = FD->getNumParams(); I < E; ++I) {
10491         if (ParamAttrs[I + Offset].Kind == Vector) {
10492           CDT = FD->getParamDecl(I)->getType();
10493           break;
10494         }
10495       }
10496     }
10497   }
10498   if (CDT.isNull())
10499     CDT = C.IntTy;
10500   CDT = CDT->getCanonicalTypeUnqualified();
10501   if (CDT->isRecordType() || CDT->isUnionType())
10502     CDT = C.IntTy;
10503   return C.getTypeSize(CDT);
10504 }
10505 
10506 static void
10507 emitX86DeclareSimdFunction(const FunctionDecl *FD, llvm::Function *Fn,
10508                            const llvm::APSInt &VLENVal,
10509                            ArrayRef<ParamAttrTy> ParamAttrs,
10510                            OMPDeclareSimdDeclAttr::BranchStateTy State) {
10511   struct ISADataTy {
10512     char ISA;
10513     unsigned VecRegSize;
10514   };
10515   ISADataTy ISAData[] = {
10516       {
10517           'b', 128
10518       }, // SSE
10519       {
10520           'c', 256
10521       }, // AVX
10522       {
10523           'd', 256
10524       }, // AVX2
10525       {
10526           'e', 512
10527       }, // AVX512
10528   };
10529   llvm::SmallVector<char, 2> Masked;
10530   switch (State) {
10531   case OMPDeclareSimdDeclAttr::BS_Undefined:
10532     Masked.push_back('N');
10533     Masked.push_back('M');
10534     break;
10535   case OMPDeclareSimdDeclAttr::BS_Notinbranch:
10536     Masked.push_back('N');
10537     break;
10538   case OMPDeclareSimdDeclAttr::BS_Inbranch:
10539     Masked.push_back('M');
10540     break;
10541   }
10542   for (char Mask : Masked) {
10543     for (const ISADataTy &Data : ISAData) {
10544       SmallString<256> Buffer;
10545       llvm::raw_svector_ostream Out(Buffer);
10546       Out << "_ZGV" << Data.ISA << Mask;
10547       if (!VLENVal) {
10548         unsigned NumElts = evaluateCDTSize(FD, ParamAttrs);
10549         assert(NumElts && "Non-zero simdlen/cdtsize expected");
10550         Out << llvm::APSInt::getUnsigned(Data.VecRegSize / NumElts);
10551       } else {
10552         Out << VLENVal;
10553       }
10554       for (const ParamAttrTy &ParamAttr : ParamAttrs) {
10555         switch (ParamAttr.Kind){
10556         case LinearWithVarStride:
10557           Out << 's' << ParamAttr.StrideOrArg;
10558           break;
10559         case Linear:
10560           Out << 'l';
10561           if (ParamAttr.StrideOrArg != 1)
10562             Out << ParamAttr.StrideOrArg;
10563           break;
10564         case Uniform:
10565           Out << 'u';
10566           break;
10567         case Vector:
10568           Out << 'v';
10569           break;
10570         }
10571         if (!!ParamAttr.Alignment)
10572           Out << 'a' << ParamAttr.Alignment;
10573       }
10574       Out << '_' << Fn->getName();
10575       Fn->addFnAttr(Out.str());
10576     }
10577   }
10578 }
10579 
10580 // This are the Functions that are needed to mangle the name of the
10581 // vector functions generated by the compiler, according to the rules
10582 // defined in the "Vector Function ABI specifications for AArch64",
10583 // available at
10584 // https://developer.arm.com/products/software-development-tools/hpc/arm-compiler-for-hpc/vector-function-abi.
10585 
10586 /// Maps To Vector (MTV), as defined in 3.1.1 of the AAVFABI.
10587 ///
10588 /// TODO: Need to implement the behavior for reference marked with a
10589 /// var or no linear modifiers (1.b in the section). For this, we
10590 /// need to extend ParamKindTy to support the linear modifiers.
10591 static bool getAArch64MTV(QualType QT, ParamKindTy Kind) {
10592   QT = QT.getCanonicalType();
10593 
10594   if (QT->isVoidType())
10595     return false;
10596 
10597   if (Kind == ParamKindTy::Uniform)
10598     return false;
10599 
10600   if (Kind == ParamKindTy::Linear)
10601     return false;
10602 
10603   // TODO: Handle linear references with modifiers
10604 
10605   if (Kind == ParamKindTy::LinearWithVarStride)
10606     return false;
10607 
10608   return true;
10609 }
10610 
10611 /// Pass By Value (PBV), as defined in 3.1.2 of the AAVFABI.
10612 static bool getAArch64PBV(QualType QT, ASTContext &C) {
10613   QT = QT.getCanonicalType();
10614   unsigned Size = C.getTypeSize(QT);
10615 
10616   // Only scalars and complex within 16 bytes wide set PVB to true.
10617   if (Size != 8 && Size != 16 && Size != 32 && Size != 64 && Size != 128)
10618     return false;
10619 
10620   if (QT->isFloatingType())
10621     return true;
10622 
10623   if (QT->isIntegerType())
10624     return true;
10625 
10626   if (QT->isPointerType())
10627     return true;
10628 
10629   // TODO: Add support for complex types (section 3.1.2, item 2).
10630 
10631   return false;
10632 }
10633 
10634 /// Computes the lane size (LS) of a return type or of an input parameter,
10635 /// as defined by `LS(P)` in 3.2.1 of the AAVFABI.
10636 /// TODO: Add support for references, section 3.2.1, item 1.
10637 static unsigned getAArch64LS(QualType QT, ParamKindTy Kind, ASTContext &C) {
10638   if (!getAArch64MTV(QT, Kind) && QT.getCanonicalType()->isPointerType()) {
10639     QualType PTy = QT.getCanonicalType()->getPointeeType();
10640     if (getAArch64PBV(PTy, C))
10641       return C.getTypeSize(PTy);
10642   }
10643   if (getAArch64PBV(QT, C))
10644     return C.getTypeSize(QT);
10645 
10646   return C.getTypeSize(C.getUIntPtrType());
10647 }
10648 
10649 // Get Narrowest Data Size (NDS) and Widest Data Size (WDS) from the
10650 // signature of the scalar function, as defined in 3.2.2 of the
10651 // AAVFABI.
10652 static std::tuple<unsigned, unsigned, bool>
10653 getNDSWDS(const FunctionDecl *FD, ArrayRef<ParamAttrTy> ParamAttrs) {
10654   QualType RetType = FD->getReturnType().getCanonicalType();
10655 
10656   ASTContext &C = FD->getASTContext();
10657 
10658   bool OutputBecomesInput = false;
10659 
10660   llvm::SmallVector<unsigned, 8> Sizes;
10661   if (!RetType->isVoidType()) {
10662     Sizes.push_back(getAArch64LS(RetType, ParamKindTy::Vector, C));
10663     if (!getAArch64PBV(RetType, C) && getAArch64MTV(RetType, {}))
10664       OutputBecomesInput = true;
10665   }
10666   for (unsigned I = 0, E = FD->getNumParams(); I < E; ++I) {
10667     QualType QT = FD->getParamDecl(I)->getType().getCanonicalType();
10668     Sizes.push_back(getAArch64LS(QT, ParamAttrs[I].Kind, C));
10669   }
10670 
10671   assert(!Sizes.empty() && "Unable to determine NDS and WDS.");
10672   // The LS of a function parameter / return value can only be a power
10673   // of 2, starting from 8 bits, up to 128.
10674   assert(std::all_of(Sizes.begin(), Sizes.end(),
10675                      [](unsigned Size) {
10676                        return Size == 8 || Size == 16 || Size == 32 ||
10677                               Size == 64 || Size == 128;
10678                      }) &&
10679          "Invalid size");
10680 
10681   return std::make_tuple(*std::min_element(std::begin(Sizes), std::end(Sizes)),
10682                          *std::max_element(std::begin(Sizes), std::end(Sizes)),
10683                          OutputBecomesInput);
10684 }
10685 
10686 /// Mangle the parameter part of the vector function name according to
10687 /// their OpenMP classification. The mangling function is defined in
10688 /// section 3.5 of the AAVFABI.
10689 static std::string mangleVectorParameters(ArrayRef<ParamAttrTy> ParamAttrs) {
10690   SmallString<256> Buffer;
10691   llvm::raw_svector_ostream Out(Buffer);
10692   for (const auto &ParamAttr : ParamAttrs) {
10693     switch (ParamAttr.Kind) {
10694     case LinearWithVarStride:
10695       Out << "ls" << ParamAttr.StrideOrArg;
10696       break;
10697     case Linear:
10698       Out << 'l';
10699       // Don't print the step value if it is not present or if it is
10700       // equal to 1.
10701       if (ParamAttr.StrideOrArg != 1)
10702         Out << ParamAttr.StrideOrArg;
10703       break;
10704     case Uniform:
10705       Out << 'u';
10706       break;
10707     case Vector:
10708       Out << 'v';
10709       break;
10710     }
10711 
10712     if (!!ParamAttr.Alignment)
10713       Out << 'a' << ParamAttr.Alignment;
10714   }
10715 
10716   return std::string(Out.str());
10717 }
10718 
10719 // Function used to add the attribute. The parameter `VLEN` is
10720 // templated to allow the use of "x" when targeting scalable functions
10721 // for SVE.
10722 template <typename T>
10723 static void addAArch64VectorName(T VLEN, StringRef LMask, StringRef Prefix,
10724                                  char ISA, StringRef ParSeq,
10725                                  StringRef MangledName, bool OutputBecomesInput,
10726                                  llvm::Function *Fn) {
10727   SmallString<256> Buffer;
10728   llvm::raw_svector_ostream Out(Buffer);
10729   Out << Prefix << ISA << LMask << VLEN;
10730   if (OutputBecomesInput)
10731     Out << "v";
10732   Out << ParSeq << "_" << MangledName;
10733   Fn->addFnAttr(Out.str());
10734 }
10735 
10736 // Helper function to generate the Advanced SIMD names depending on
10737 // the value of the NDS when simdlen is not present.
10738 static void addAArch64AdvSIMDNDSNames(unsigned NDS, StringRef Mask,
10739                                       StringRef Prefix, char ISA,
10740                                       StringRef ParSeq, StringRef MangledName,
10741                                       bool OutputBecomesInput,
10742                                       llvm::Function *Fn) {
10743   switch (NDS) {
10744   case 8:
10745     addAArch64VectorName(8, Mask, Prefix, ISA, ParSeq, MangledName,
10746                          OutputBecomesInput, Fn);
10747     addAArch64VectorName(16, Mask, Prefix, ISA, ParSeq, MangledName,
10748                          OutputBecomesInput, Fn);
10749     break;
10750   case 16:
10751     addAArch64VectorName(4, Mask, Prefix, ISA, ParSeq, MangledName,
10752                          OutputBecomesInput, Fn);
10753     addAArch64VectorName(8, Mask, Prefix, ISA, ParSeq, MangledName,
10754                          OutputBecomesInput, Fn);
10755     break;
10756   case 32:
10757     addAArch64VectorName(2, Mask, Prefix, ISA, ParSeq, MangledName,
10758                          OutputBecomesInput, Fn);
10759     addAArch64VectorName(4, Mask, Prefix, ISA, ParSeq, MangledName,
10760                          OutputBecomesInput, Fn);
10761     break;
10762   case 64:
10763   case 128:
10764     addAArch64VectorName(2, Mask, Prefix, ISA, ParSeq, MangledName,
10765                          OutputBecomesInput, Fn);
10766     break;
10767   default:
10768     llvm_unreachable("Scalar type is too wide.");
10769   }
10770 }
10771 
10772 /// Emit vector function attributes for AArch64, as defined in the AAVFABI.
10773 static void emitAArch64DeclareSimdFunction(
10774     CodeGenModule &CGM, const FunctionDecl *FD, unsigned UserVLEN,
10775     ArrayRef<ParamAttrTy> ParamAttrs,
10776     OMPDeclareSimdDeclAttr::BranchStateTy State, StringRef MangledName,
10777     char ISA, unsigned VecRegSize, llvm::Function *Fn, SourceLocation SLoc) {
10778 
10779   // Get basic data for building the vector signature.
10780   const auto Data = getNDSWDS(FD, ParamAttrs);
10781   const unsigned NDS = std::get<0>(Data);
10782   const unsigned WDS = std::get<1>(Data);
10783   const bool OutputBecomesInput = std::get<2>(Data);
10784 
10785   // Check the values provided via `simdlen` by the user.
10786   // 1. A `simdlen(1)` doesn't produce vector signatures,
10787   if (UserVLEN == 1) {
10788     unsigned DiagID = CGM.getDiags().getCustomDiagID(
10789         DiagnosticsEngine::Warning,
10790         "The clause simdlen(1) has no effect when targeting aarch64.");
10791     CGM.getDiags().Report(SLoc, DiagID);
10792     return;
10793   }
10794 
10795   // 2. Section 3.3.1, item 1: user input must be a power of 2 for
10796   // Advanced SIMD output.
10797   if (ISA == 'n' && UserVLEN && !llvm::isPowerOf2_32(UserVLEN)) {
10798     unsigned DiagID = CGM.getDiags().getCustomDiagID(
10799         DiagnosticsEngine::Warning, "The value specified in simdlen must be a "
10800                                     "power of 2 when targeting Advanced SIMD.");
10801     CGM.getDiags().Report(SLoc, DiagID);
10802     return;
10803   }
10804 
10805   // 3. Section 3.4.1. SVE fixed lengh must obey the architectural
10806   // limits.
10807   if (ISA == 's' && UserVLEN != 0) {
10808     if ((UserVLEN * WDS > 2048) || (UserVLEN * WDS % 128 != 0)) {
10809       unsigned DiagID = CGM.getDiags().getCustomDiagID(
10810           DiagnosticsEngine::Warning, "The clause simdlen must fit the %0-bit "
10811                                       "lanes in the architectural constraints "
10812                                       "for SVE (min is 128-bit, max is "
10813                                       "2048-bit, by steps of 128-bit)");
10814       CGM.getDiags().Report(SLoc, DiagID) << WDS;
10815       return;
10816     }
10817   }
10818 
10819   // Sort out parameter sequence.
10820   const std::string ParSeq = mangleVectorParameters(ParamAttrs);
10821   StringRef Prefix = "_ZGV";
10822   // Generate simdlen from user input (if any).
10823   if (UserVLEN) {
10824     if (ISA == 's') {
10825       // SVE generates only a masked function.
10826       addAArch64VectorName(UserVLEN, "M", Prefix, ISA, ParSeq, MangledName,
10827                            OutputBecomesInput, Fn);
10828     } else {
10829       assert(ISA == 'n' && "Expected ISA either 's' or 'n'.");
10830       // Advanced SIMD generates one or two functions, depending on
10831       // the `[not]inbranch` clause.
10832       switch (State) {
10833       case OMPDeclareSimdDeclAttr::BS_Undefined:
10834         addAArch64VectorName(UserVLEN, "N", Prefix, ISA, ParSeq, MangledName,
10835                              OutputBecomesInput, Fn);
10836         addAArch64VectorName(UserVLEN, "M", Prefix, ISA, ParSeq, MangledName,
10837                              OutputBecomesInput, Fn);
10838         break;
10839       case OMPDeclareSimdDeclAttr::BS_Notinbranch:
10840         addAArch64VectorName(UserVLEN, "N", Prefix, ISA, ParSeq, MangledName,
10841                              OutputBecomesInput, Fn);
10842         break;
10843       case OMPDeclareSimdDeclAttr::BS_Inbranch:
10844         addAArch64VectorName(UserVLEN, "M", Prefix, ISA, ParSeq, MangledName,
10845                              OutputBecomesInput, Fn);
10846         break;
10847       }
10848     }
10849   } else {
10850     // If no user simdlen is provided, follow the AAVFABI rules for
10851     // generating the vector length.
10852     if (ISA == 's') {
10853       // SVE, section 3.4.1, item 1.
10854       addAArch64VectorName("x", "M", Prefix, ISA, ParSeq, MangledName,
10855                            OutputBecomesInput, Fn);
10856     } else {
10857       assert(ISA == 'n' && "Expected ISA either 's' or 'n'.");
10858       // Advanced SIMD, Section 3.3.1 of the AAVFABI, generates one or
10859       // two vector names depending on the use of the clause
10860       // `[not]inbranch`.
10861       switch (State) {
10862       case OMPDeclareSimdDeclAttr::BS_Undefined:
10863         addAArch64AdvSIMDNDSNames(NDS, "N", Prefix, ISA, ParSeq, MangledName,
10864                                   OutputBecomesInput, Fn);
10865         addAArch64AdvSIMDNDSNames(NDS, "M", Prefix, ISA, ParSeq, MangledName,
10866                                   OutputBecomesInput, Fn);
10867         break;
10868       case OMPDeclareSimdDeclAttr::BS_Notinbranch:
10869         addAArch64AdvSIMDNDSNames(NDS, "N", Prefix, ISA, ParSeq, MangledName,
10870                                   OutputBecomesInput, Fn);
10871         break;
10872       case OMPDeclareSimdDeclAttr::BS_Inbranch:
10873         addAArch64AdvSIMDNDSNames(NDS, "M", Prefix, ISA, ParSeq, MangledName,
10874                                   OutputBecomesInput, Fn);
10875         break;
10876       }
10877     }
10878   }
10879 }
10880 
10881 void CGOpenMPRuntime::emitDeclareSimdFunction(const FunctionDecl *FD,
10882                                               llvm::Function *Fn) {
10883   ASTContext &C = CGM.getContext();
10884   FD = FD->getMostRecentDecl();
10885   // Map params to their positions in function decl.
10886   llvm::DenseMap<const Decl *, unsigned> ParamPositions;
10887   if (isa<CXXMethodDecl>(FD))
10888     ParamPositions.try_emplace(FD, 0);
10889   unsigned ParamPos = ParamPositions.size();
10890   for (const ParmVarDecl *P : FD->parameters()) {
10891     ParamPositions.try_emplace(P->getCanonicalDecl(), ParamPos);
10892     ++ParamPos;
10893   }
10894   while (FD) {
10895     for (const auto *Attr : FD->specific_attrs<OMPDeclareSimdDeclAttr>()) {
10896       llvm::SmallVector<ParamAttrTy, 8> ParamAttrs(ParamPositions.size());
10897       // Mark uniform parameters.
10898       for (const Expr *E : Attr->uniforms()) {
10899         E = E->IgnoreParenImpCasts();
10900         unsigned Pos;
10901         if (isa<CXXThisExpr>(E)) {
10902           Pos = ParamPositions[FD];
10903         } else {
10904           const auto *PVD = cast<ParmVarDecl>(cast<DeclRefExpr>(E)->getDecl())
10905                                 ->getCanonicalDecl();
10906           Pos = ParamPositions[PVD];
10907         }
10908         ParamAttrs[Pos].Kind = Uniform;
10909       }
10910       // Get alignment info.
10911       auto NI = Attr->alignments_begin();
10912       for (const Expr *E : Attr->aligneds()) {
10913         E = E->IgnoreParenImpCasts();
10914         unsigned Pos;
10915         QualType ParmTy;
10916         if (isa<CXXThisExpr>(E)) {
10917           Pos = ParamPositions[FD];
10918           ParmTy = E->getType();
10919         } else {
10920           const auto *PVD = cast<ParmVarDecl>(cast<DeclRefExpr>(E)->getDecl())
10921                                 ->getCanonicalDecl();
10922           Pos = ParamPositions[PVD];
10923           ParmTy = PVD->getType();
10924         }
10925         ParamAttrs[Pos].Alignment =
10926             (*NI)
10927                 ? (*NI)->EvaluateKnownConstInt(C)
10928                 : llvm::APSInt::getUnsigned(
10929                       C.toCharUnitsFromBits(C.getOpenMPDefaultSimdAlign(ParmTy))
10930                           .getQuantity());
10931         ++NI;
10932       }
10933       // Mark linear parameters.
10934       auto SI = Attr->steps_begin();
10935       auto MI = Attr->modifiers_begin();
10936       for (const Expr *E : Attr->linears()) {
10937         E = E->IgnoreParenImpCasts();
10938         unsigned Pos;
10939         // Rescaling factor needed to compute the linear parameter
10940         // value in the mangled name.
10941         unsigned PtrRescalingFactor = 1;
10942         if (isa<CXXThisExpr>(E)) {
10943           Pos = ParamPositions[FD];
10944         } else {
10945           const auto *PVD = cast<ParmVarDecl>(cast<DeclRefExpr>(E)->getDecl())
10946                                 ->getCanonicalDecl();
10947           Pos = ParamPositions[PVD];
10948           if (auto *P = dyn_cast<PointerType>(PVD->getType()))
10949             PtrRescalingFactor = CGM.getContext()
10950                                      .getTypeSizeInChars(P->getPointeeType())
10951                                      .getQuantity();
10952         }
10953         ParamAttrTy &ParamAttr = ParamAttrs[Pos];
10954         ParamAttr.Kind = Linear;
10955         // Assuming a stride of 1, for `linear` without modifiers.
10956         ParamAttr.StrideOrArg = llvm::APSInt::getUnsigned(1);
10957         if (*SI) {
10958           Expr::EvalResult Result;
10959           if (!(*SI)->EvaluateAsInt(Result, C, Expr::SE_AllowSideEffects)) {
10960             if (const auto *DRE =
10961                     cast<DeclRefExpr>((*SI)->IgnoreParenImpCasts())) {
10962               if (const auto *StridePVD = cast<ParmVarDecl>(DRE->getDecl())) {
10963                 ParamAttr.Kind = LinearWithVarStride;
10964                 ParamAttr.StrideOrArg = llvm::APSInt::getUnsigned(
10965                     ParamPositions[StridePVD->getCanonicalDecl()]);
10966               }
10967             }
10968           } else {
10969             ParamAttr.StrideOrArg = Result.Val.getInt();
10970           }
10971         }
10972         // If we are using a linear clause on a pointer, we need to
10973         // rescale the value of linear_step with the byte size of the
10974         // pointee type.
10975         if (Linear == ParamAttr.Kind)
10976           ParamAttr.StrideOrArg = ParamAttr.StrideOrArg * PtrRescalingFactor;
10977         ++SI;
10978         ++MI;
10979       }
10980       llvm::APSInt VLENVal;
10981       SourceLocation ExprLoc;
10982       const Expr *VLENExpr = Attr->getSimdlen();
10983       if (VLENExpr) {
10984         VLENVal = VLENExpr->EvaluateKnownConstInt(C);
10985         ExprLoc = VLENExpr->getExprLoc();
10986       }
10987       OMPDeclareSimdDeclAttr::BranchStateTy State = Attr->getBranchState();
10988       if (CGM.getTriple().isX86()) {
10989         emitX86DeclareSimdFunction(FD, Fn, VLENVal, ParamAttrs, State);
10990       } else if (CGM.getTriple().getArch() == llvm::Triple::aarch64) {
10991         unsigned VLEN = VLENVal.getExtValue();
10992         StringRef MangledName = Fn->getName();
10993         if (CGM.getTarget().hasFeature("sve"))
10994           emitAArch64DeclareSimdFunction(CGM, FD, VLEN, ParamAttrs, State,
10995                                          MangledName, 's', 128, Fn, ExprLoc);
10996         if (CGM.getTarget().hasFeature("neon"))
10997           emitAArch64DeclareSimdFunction(CGM, FD, VLEN, ParamAttrs, State,
10998                                          MangledName, 'n', 128, Fn, ExprLoc);
10999       }
11000     }
11001     FD = FD->getPreviousDecl();
11002   }
11003 }
11004 
11005 namespace {
11006 /// Cleanup action for doacross support.
11007 class DoacrossCleanupTy final : public EHScopeStack::Cleanup {
11008 public:
11009   static const int DoacrossFinArgs = 2;
11010 
11011 private:
11012   llvm::FunctionCallee RTLFn;
11013   llvm::Value *Args[DoacrossFinArgs];
11014 
11015 public:
11016   DoacrossCleanupTy(llvm::FunctionCallee RTLFn,
11017                     ArrayRef<llvm::Value *> CallArgs)
11018       : RTLFn(RTLFn) {
11019     assert(CallArgs.size() == DoacrossFinArgs);
11020     std::copy(CallArgs.begin(), CallArgs.end(), std::begin(Args));
11021   }
11022   void Emit(CodeGenFunction &CGF, Flags /*flags*/) override {
11023     if (!CGF.HaveInsertPoint())
11024       return;
11025     CGF.EmitRuntimeCall(RTLFn, Args);
11026   }
11027 };
11028 } // namespace
11029 
11030 void CGOpenMPRuntime::emitDoacrossInit(CodeGenFunction &CGF,
11031                                        const OMPLoopDirective &D,
11032                                        ArrayRef<Expr *> NumIterations) {
11033   if (!CGF.HaveInsertPoint())
11034     return;
11035 
11036   ASTContext &C = CGM.getContext();
11037   QualType Int64Ty = C.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/true);
11038   RecordDecl *RD;
11039   if (KmpDimTy.isNull()) {
11040     // Build struct kmp_dim {  // loop bounds info casted to kmp_int64
11041     //  kmp_int64 lo; // lower
11042     //  kmp_int64 up; // upper
11043     //  kmp_int64 st; // stride
11044     // };
11045     RD = C.buildImplicitRecord("kmp_dim");
11046     RD->startDefinition();
11047     addFieldToRecordDecl(C, RD, Int64Ty);
11048     addFieldToRecordDecl(C, RD, Int64Ty);
11049     addFieldToRecordDecl(C, RD, Int64Ty);
11050     RD->completeDefinition();
11051     KmpDimTy = C.getRecordType(RD);
11052   } else {
11053     RD = cast<RecordDecl>(KmpDimTy->getAsTagDecl());
11054   }
11055   llvm::APInt Size(/*numBits=*/32, NumIterations.size());
11056   QualType ArrayTy =
11057       C.getConstantArrayType(KmpDimTy, Size, nullptr, ArrayType::Normal, 0);
11058 
11059   Address DimsAddr = CGF.CreateMemTemp(ArrayTy, "dims");
11060   CGF.EmitNullInitialization(DimsAddr, ArrayTy);
11061   enum { LowerFD = 0, UpperFD, StrideFD };
11062   // Fill dims with data.
11063   for (unsigned I = 0, E = NumIterations.size(); I < E; ++I) {
11064     LValue DimsLVal = CGF.MakeAddrLValue(
11065         CGF.Builder.CreateConstArrayGEP(DimsAddr, I), KmpDimTy);
11066     // dims.upper = num_iterations;
11067     LValue UpperLVal = CGF.EmitLValueForField(
11068         DimsLVal, *std::next(RD->field_begin(), UpperFD));
11069     llvm::Value *NumIterVal = CGF.EmitScalarConversion(
11070         CGF.EmitScalarExpr(NumIterations[I]), NumIterations[I]->getType(),
11071         Int64Ty, NumIterations[I]->getExprLoc());
11072     CGF.EmitStoreOfScalar(NumIterVal, UpperLVal);
11073     // dims.stride = 1;
11074     LValue StrideLVal = CGF.EmitLValueForField(
11075         DimsLVal, *std::next(RD->field_begin(), StrideFD));
11076     CGF.EmitStoreOfScalar(llvm::ConstantInt::getSigned(CGM.Int64Ty, /*V=*/1),
11077                           StrideLVal);
11078   }
11079 
11080   // Build call void __kmpc_doacross_init(ident_t *loc, kmp_int32 gtid,
11081   // kmp_int32 num_dims, struct kmp_dim * dims);
11082   llvm::Value *Args[] = {
11083       emitUpdateLocation(CGF, D.getBeginLoc()),
11084       getThreadID(CGF, D.getBeginLoc()),
11085       llvm::ConstantInt::getSigned(CGM.Int32Ty, NumIterations.size()),
11086       CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(
11087           CGF.Builder.CreateConstArrayGEP(DimsAddr, 0).getPointer(),
11088           CGM.VoidPtrTy)};
11089 
11090   llvm::FunctionCallee RTLFn = OMPBuilder.getOrCreateRuntimeFunction(
11091       CGM.getModule(), OMPRTL___kmpc_doacross_init);
11092   CGF.EmitRuntimeCall(RTLFn, Args);
11093   llvm::Value *FiniArgs[DoacrossCleanupTy::DoacrossFinArgs] = {
11094       emitUpdateLocation(CGF, D.getEndLoc()), getThreadID(CGF, D.getEndLoc())};
11095   llvm::FunctionCallee FiniRTLFn = OMPBuilder.getOrCreateRuntimeFunction(
11096       CGM.getModule(), OMPRTL___kmpc_doacross_fini);
11097   CGF.EHStack.pushCleanup<DoacrossCleanupTy>(NormalAndEHCleanup, FiniRTLFn,
11098                                              llvm::makeArrayRef(FiniArgs));
11099 }
11100 
11101 void CGOpenMPRuntime::emitDoacrossOrdered(CodeGenFunction &CGF,
11102                                           const OMPDependClause *C) {
11103   QualType Int64Ty =
11104       CGM.getContext().getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1);
11105   llvm::APInt Size(/*numBits=*/32, C->getNumLoops());
11106   QualType ArrayTy = CGM.getContext().getConstantArrayType(
11107       Int64Ty, Size, nullptr, ArrayType::Normal, 0);
11108   Address CntAddr = CGF.CreateMemTemp(ArrayTy, ".cnt.addr");
11109   for (unsigned I = 0, E = C->getNumLoops(); I < E; ++I) {
11110     const Expr *CounterVal = C->getLoopData(I);
11111     assert(CounterVal);
11112     llvm::Value *CntVal = CGF.EmitScalarConversion(
11113         CGF.EmitScalarExpr(CounterVal), CounterVal->getType(), Int64Ty,
11114         CounterVal->getExprLoc());
11115     CGF.EmitStoreOfScalar(CntVal, CGF.Builder.CreateConstArrayGEP(CntAddr, I),
11116                           /*Volatile=*/false, Int64Ty);
11117   }
11118   llvm::Value *Args[] = {
11119       emitUpdateLocation(CGF, C->getBeginLoc()),
11120       getThreadID(CGF, C->getBeginLoc()),
11121       CGF.Builder.CreateConstArrayGEP(CntAddr, 0).getPointer()};
11122   llvm::FunctionCallee RTLFn;
11123   if (C->getDependencyKind() == OMPC_DEPEND_source) {
11124     RTLFn = OMPBuilder.getOrCreateRuntimeFunction(CGM.getModule(),
11125                                                   OMPRTL___kmpc_doacross_post);
11126   } else {
11127     assert(C->getDependencyKind() == OMPC_DEPEND_sink);
11128     RTLFn = OMPBuilder.getOrCreateRuntimeFunction(CGM.getModule(),
11129                                                   OMPRTL___kmpc_doacross_wait);
11130   }
11131   CGF.EmitRuntimeCall(RTLFn, Args);
11132 }
11133 
11134 void CGOpenMPRuntime::emitCall(CodeGenFunction &CGF, SourceLocation Loc,
11135                                llvm::FunctionCallee Callee,
11136                                ArrayRef<llvm::Value *> Args) const {
11137   assert(Loc.isValid() && "Outlined function call location must be valid.");
11138   auto DL = ApplyDebugLocation::CreateDefaultArtificial(CGF, Loc);
11139 
11140   if (auto *Fn = dyn_cast<llvm::Function>(Callee.getCallee())) {
11141     if (Fn->doesNotThrow()) {
11142       CGF.EmitNounwindRuntimeCall(Fn, Args);
11143       return;
11144     }
11145   }
11146   CGF.EmitRuntimeCall(Callee, Args);
11147 }
11148 
11149 void CGOpenMPRuntime::emitOutlinedFunctionCall(
11150     CodeGenFunction &CGF, SourceLocation Loc, llvm::FunctionCallee OutlinedFn,
11151     ArrayRef<llvm::Value *> Args) const {
11152   emitCall(CGF, Loc, OutlinedFn, Args);
11153 }
11154 
11155 void CGOpenMPRuntime::emitFunctionProlog(CodeGenFunction &CGF, const Decl *D) {
11156   if (const auto *FD = dyn_cast<FunctionDecl>(D))
11157     if (OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(FD))
11158       HasEmittedDeclareTargetRegion = true;
11159 }
11160 
11161 Address CGOpenMPRuntime::getParameterAddress(CodeGenFunction &CGF,
11162                                              const VarDecl *NativeParam,
11163                                              const VarDecl *TargetParam) const {
11164   return CGF.GetAddrOfLocalVar(NativeParam);
11165 }
11166 
11167 namespace {
11168 /// Cleanup action for allocate support.
11169 class OMPAllocateCleanupTy final : public EHScopeStack::Cleanup {
11170 public:
11171   static const int CleanupArgs = 3;
11172 
11173 private:
11174   llvm::FunctionCallee RTLFn;
11175   llvm::Value *Args[CleanupArgs];
11176 
11177 public:
11178   OMPAllocateCleanupTy(llvm::FunctionCallee RTLFn,
11179                        ArrayRef<llvm::Value *> CallArgs)
11180       : RTLFn(RTLFn) {
11181     assert(CallArgs.size() == CleanupArgs &&
11182            "Size of arguments does not match.");
11183     std::copy(CallArgs.begin(), CallArgs.end(), std::begin(Args));
11184   }
11185   void Emit(CodeGenFunction &CGF, Flags /*flags*/) override {
11186     if (!CGF.HaveInsertPoint())
11187       return;
11188     CGF.EmitRuntimeCall(RTLFn, Args);
11189   }
11190 };
11191 } // namespace
11192 
11193 Address CGOpenMPRuntime::getAddressOfLocalVariable(CodeGenFunction &CGF,
11194                                                    const VarDecl *VD) {
11195   if (!VD)
11196     return Address::invalid();
11197   const VarDecl *CVD = VD->getCanonicalDecl();
11198   if (!CVD->hasAttr<OMPAllocateDeclAttr>())
11199     return Address::invalid();
11200   const auto *AA = CVD->getAttr<OMPAllocateDeclAttr>();
11201   // Use the default allocation.
11202   if ((AA->getAllocatorType() == OMPAllocateDeclAttr::OMPDefaultMemAlloc ||
11203        AA->getAllocatorType() == OMPAllocateDeclAttr::OMPNullMemAlloc) &&
11204       !AA->getAllocator())
11205     return Address::invalid();
11206   llvm::Value *Size;
11207   CharUnits Align = CGM.getContext().getDeclAlign(CVD);
11208   if (CVD->getType()->isVariablyModifiedType()) {
11209     Size = CGF.getTypeSize(CVD->getType());
11210     // Align the size: ((size + align - 1) / align) * align
11211     Size = CGF.Builder.CreateNUWAdd(
11212         Size, CGM.getSize(Align - CharUnits::fromQuantity(1)));
11213     Size = CGF.Builder.CreateUDiv(Size, CGM.getSize(Align));
11214     Size = CGF.Builder.CreateNUWMul(Size, CGM.getSize(Align));
11215   } else {
11216     CharUnits Sz = CGM.getContext().getTypeSizeInChars(CVD->getType());
11217     Size = CGM.getSize(Sz.alignTo(Align));
11218   }
11219   llvm::Value *ThreadID = getThreadID(CGF, CVD->getBeginLoc());
11220   assert(AA->getAllocator() &&
11221          "Expected allocator expression for non-default allocator.");
11222   llvm::Value *Allocator = CGF.EmitScalarExpr(AA->getAllocator());
11223   // According to the standard, the original allocator type is a enum (integer).
11224   // Convert to pointer type, if required.
11225   if (Allocator->getType()->isIntegerTy())
11226     Allocator = CGF.Builder.CreateIntToPtr(Allocator, CGM.VoidPtrTy);
11227   else if (Allocator->getType()->isPointerTy())
11228     Allocator = CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(Allocator,
11229                                                                 CGM.VoidPtrTy);
11230   llvm::Value *Args[] = {ThreadID, Size, Allocator};
11231 
11232   llvm::Value *Addr =
11233       CGF.EmitRuntimeCall(OMPBuilder.getOrCreateRuntimeFunction(
11234                               CGM.getModule(), OMPRTL___kmpc_alloc),
11235                           Args, getName({CVD->getName(), ".void.addr"}));
11236   llvm::Value *FiniArgs[OMPAllocateCleanupTy::CleanupArgs] = {ThreadID, Addr,
11237                                                               Allocator};
11238   llvm::FunctionCallee FiniRTLFn = OMPBuilder.getOrCreateRuntimeFunction(
11239       CGM.getModule(), OMPRTL___kmpc_free);
11240 
11241   CGF.EHStack.pushCleanup<OMPAllocateCleanupTy>(NormalAndEHCleanup, FiniRTLFn,
11242                                                 llvm::makeArrayRef(FiniArgs));
11243   Addr = CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(
11244       Addr,
11245       CGF.ConvertTypeForMem(CGM.getContext().getPointerType(CVD->getType())),
11246       getName({CVD->getName(), ".addr"}));
11247   return Address(Addr, Align);
11248 }
11249 
11250 CGOpenMPRuntime::NontemporalDeclsRAII::NontemporalDeclsRAII(
11251     CodeGenModule &CGM, const OMPLoopDirective &S)
11252     : CGM(CGM), NeedToPush(S.hasClausesOfKind<OMPNontemporalClause>()) {
11253   assert(CGM.getLangOpts().OpenMP && "Not in OpenMP mode.");
11254   if (!NeedToPush)
11255     return;
11256   NontemporalDeclsSet &DS =
11257       CGM.getOpenMPRuntime().NontemporalDeclsStack.emplace_back();
11258   for (const auto *C : S.getClausesOfKind<OMPNontemporalClause>()) {
11259     for (const Stmt *Ref : C->private_refs()) {
11260       const auto *SimpleRefExpr = cast<Expr>(Ref)->IgnoreParenImpCasts();
11261       const ValueDecl *VD;
11262       if (const auto *DRE = dyn_cast<DeclRefExpr>(SimpleRefExpr)) {
11263         VD = DRE->getDecl();
11264       } else {
11265         const auto *ME = cast<MemberExpr>(SimpleRefExpr);
11266         assert((ME->isImplicitCXXThis() ||
11267                 isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts())) &&
11268                "Expected member of current class.");
11269         VD = ME->getMemberDecl();
11270       }
11271       DS.insert(VD);
11272     }
11273   }
11274 }
11275 
11276 CGOpenMPRuntime::NontemporalDeclsRAII::~NontemporalDeclsRAII() {
11277   if (!NeedToPush)
11278     return;
11279   CGM.getOpenMPRuntime().NontemporalDeclsStack.pop_back();
11280 }
11281 
11282 bool CGOpenMPRuntime::isNontemporalDecl(const ValueDecl *VD) const {
11283   assert(CGM.getLangOpts().OpenMP && "Not in OpenMP mode.");
11284 
11285   return llvm::any_of(
11286       CGM.getOpenMPRuntime().NontemporalDeclsStack,
11287       [VD](const NontemporalDeclsSet &Set) { return Set.count(VD) > 0; });
11288 }
11289 
11290 void CGOpenMPRuntime::LastprivateConditionalRAII::tryToDisableInnerAnalysis(
11291     const OMPExecutableDirective &S,
11292     llvm::DenseSet<CanonicalDeclPtr<const Decl>> &NeedToAddForLPCsAsDisabled)
11293     const {
11294   llvm::DenseSet<CanonicalDeclPtr<const Decl>> NeedToCheckForLPCs;
11295   // Vars in target/task regions must be excluded completely.
11296   if (isOpenMPTargetExecutionDirective(S.getDirectiveKind()) ||
11297       isOpenMPTaskingDirective(S.getDirectiveKind())) {
11298     SmallVector<OpenMPDirectiveKind, 4> CaptureRegions;
11299     getOpenMPCaptureRegions(CaptureRegions, S.getDirectiveKind());
11300     const CapturedStmt *CS = S.getCapturedStmt(CaptureRegions.front());
11301     for (const CapturedStmt::Capture &Cap : CS->captures()) {
11302       if (Cap.capturesVariable() || Cap.capturesVariableByCopy())
11303         NeedToCheckForLPCs.insert(Cap.getCapturedVar());
11304     }
11305   }
11306   // Exclude vars in private clauses.
11307   for (const auto *C : S.getClausesOfKind<OMPPrivateClause>()) {
11308     for (const Expr *Ref : C->varlists()) {
11309       if (!Ref->getType()->isScalarType())
11310         continue;
11311       const auto *DRE = dyn_cast<DeclRefExpr>(Ref->IgnoreParenImpCasts());
11312       if (!DRE)
11313         continue;
11314       NeedToCheckForLPCs.insert(DRE->getDecl());
11315     }
11316   }
11317   for (const auto *C : S.getClausesOfKind<OMPFirstprivateClause>()) {
11318     for (const Expr *Ref : C->varlists()) {
11319       if (!Ref->getType()->isScalarType())
11320         continue;
11321       const auto *DRE = dyn_cast<DeclRefExpr>(Ref->IgnoreParenImpCasts());
11322       if (!DRE)
11323         continue;
11324       NeedToCheckForLPCs.insert(DRE->getDecl());
11325     }
11326   }
11327   for (const auto *C : S.getClausesOfKind<OMPLastprivateClause>()) {
11328     for (const Expr *Ref : C->varlists()) {
11329       if (!Ref->getType()->isScalarType())
11330         continue;
11331       const auto *DRE = dyn_cast<DeclRefExpr>(Ref->IgnoreParenImpCasts());
11332       if (!DRE)
11333         continue;
11334       NeedToCheckForLPCs.insert(DRE->getDecl());
11335     }
11336   }
11337   for (const auto *C : S.getClausesOfKind<OMPReductionClause>()) {
11338     for (const Expr *Ref : C->varlists()) {
11339       if (!Ref->getType()->isScalarType())
11340         continue;
11341       const auto *DRE = dyn_cast<DeclRefExpr>(Ref->IgnoreParenImpCasts());
11342       if (!DRE)
11343         continue;
11344       NeedToCheckForLPCs.insert(DRE->getDecl());
11345     }
11346   }
11347   for (const auto *C : S.getClausesOfKind<OMPLinearClause>()) {
11348     for (const Expr *Ref : C->varlists()) {
11349       if (!Ref->getType()->isScalarType())
11350         continue;
11351       const auto *DRE = dyn_cast<DeclRefExpr>(Ref->IgnoreParenImpCasts());
11352       if (!DRE)
11353         continue;
11354       NeedToCheckForLPCs.insert(DRE->getDecl());
11355     }
11356   }
11357   for (const Decl *VD : NeedToCheckForLPCs) {
11358     for (const LastprivateConditionalData &Data :
11359          llvm::reverse(CGM.getOpenMPRuntime().LastprivateConditionalStack)) {
11360       if (Data.DeclToUniqueName.count(VD) > 0) {
11361         if (!Data.Disabled)
11362           NeedToAddForLPCsAsDisabled.insert(VD);
11363         break;
11364       }
11365     }
11366   }
11367 }
11368 
11369 CGOpenMPRuntime::LastprivateConditionalRAII::LastprivateConditionalRAII(
11370     CodeGenFunction &CGF, const OMPExecutableDirective &S, LValue IVLVal)
11371     : CGM(CGF.CGM),
11372       Action((CGM.getLangOpts().OpenMP >= 50 &&
11373               llvm::any_of(S.getClausesOfKind<OMPLastprivateClause>(),
11374                            [](const OMPLastprivateClause *C) {
11375                              return C->getKind() ==
11376                                     OMPC_LASTPRIVATE_conditional;
11377                            }))
11378                  ? ActionToDo::PushAsLastprivateConditional
11379                  : ActionToDo::DoNotPush) {
11380   assert(CGM.getLangOpts().OpenMP && "Not in OpenMP mode.");
11381   if (CGM.getLangOpts().OpenMP < 50 || Action == ActionToDo::DoNotPush)
11382     return;
11383   assert(Action == ActionToDo::PushAsLastprivateConditional &&
11384          "Expected a push action.");
11385   LastprivateConditionalData &Data =
11386       CGM.getOpenMPRuntime().LastprivateConditionalStack.emplace_back();
11387   for (const auto *C : S.getClausesOfKind<OMPLastprivateClause>()) {
11388     if (C->getKind() != OMPC_LASTPRIVATE_conditional)
11389       continue;
11390 
11391     for (const Expr *Ref : C->varlists()) {
11392       Data.DeclToUniqueName.insert(std::make_pair(
11393           cast<DeclRefExpr>(Ref->IgnoreParenImpCasts())->getDecl(),
11394           SmallString<16>(generateUniqueName(CGM, "pl_cond", Ref))));
11395     }
11396   }
11397   Data.IVLVal = IVLVal;
11398   Data.Fn = CGF.CurFn;
11399 }
11400 
11401 CGOpenMPRuntime::LastprivateConditionalRAII::LastprivateConditionalRAII(
11402     CodeGenFunction &CGF, const OMPExecutableDirective &S)
11403     : CGM(CGF.CGM), Action(ActionToDo::DoNotPush) {
11404   assert(CGM.getLangOpts().OpenMP && "Not in OpenMP mode.");
11405   if (CGM.getLangOpts().OpenMP < 50)
11406     return;
11407   llvm::DenseSet<CanonicalDeclPtr<const Decl>> NeedToAddForLPCsAsDisabled;
11408   tryToDisableInnerAnalysis(S, NeedToAddForLPCsAsDisabled);
11409   if (!NeedToAddForLPCsAsDisabled.empty()) {
11410     Action = ActionToDo::DisableLastprivateConditional;
11411     LastprivateConditionalData &Data =
11412         CGM.getOpenMPRuntime().LastprivateConditionalStack.emplace_back();
11413     for (const Decl *VD : NeedToAddForLPCsAsDisabled)
11414       Data.DeclToUniqueName.insert(std::make_pair(VD, SmallString<16>()));
11415     Data.Fn = CGF.CurFn;
11416     Data.Disabled = true;
11417   }
11418 }
11419 
11420 CGOpenMPRuntime::LastprivateConditionalRAII
11421 CGOpenMPRuntime::LastprivateConditionalRAII::disable(
11422     CodeGenFunction &CGF, const OMPExecutableDirective &S) {
11423   return LastprivateConditionalRAII(CGF, S);
11424 }
11425 
11426 CGOpenMPRuntime::LastprivateConditionalRAII::~LastprivateConditionalRAII() {
11427   if (CGM.getLangOpts().OpenMP < 50)
11428     return;
11429   if (Action == ActionToDo::DisableLastprivateConditional) {
11430     assert(CGM.getOpenMPRuntime().LastprivateConditionalStack.back().Disabled &&
11431            "Expected list of disabled private vars.");
11432     CGM.getOpenMPRuntime().LastprivateConditionalStack.pop_back();
11433   }
11434   if (Action == ActionToDo::PushAsLastprivateConditional) {
11435     assert(
11436         !CGM.getOpenMPRuntime().LastprivateConditionalStack.back().Disabled &&
11437         "Expected list of lastprivate conditional vars.");
11438     CGM.getOpenMPRuntime().LastprivateConditionalStack.pop_back();
11439   }
11440 }
11441 
11442 Address CGOpenMPRuntime::emitLastprivateConditionalInit(CodeGenFunction &CGF,
11443                                                         const VarDecl *VD) {
11444   ASTContext &C = CGM.getContext();
11445   auto I = LastprivateConditionalToTypes.find(CGF.CurFn);
11446   if (I == LastprivateConditionalToTypes.end())
11447     I = LastprivateConditionalToTypes.try_emplace(CGF.CurFn).first;
11448   QualType NewType;
11449   const FieldDecl *VDField;
11450   const FieldDecl *FiredField;
11451   LValue BaseLVal;
11452   auto VI = I->getSecond().find(VD);
11453   if (VI == I->getSecond().end()) {
11454     RecordDecl *RD = C.buildImplicitRecord("lasprivate.conditional");
11455     RD->startDefinition();
11456     VDField = addFieldToRecordDecl(C, RD, VD->getType().getNonReferenceType());
11457     FiredField = addFieldToRecordDecl(C, RD, C.CharTy);
11458     RD->completeDefinition();
11459     NewType = C.getRecordType(RD);
11460     Address Addr = CGF.CreateMemTemp(NewType, C.getDeclAlign(VD), VD->getName());
11461     BaseLVal = CGF.MakeAddrLValue(Addr, NewType, AlignmentSource::Decl);
11462     I->getSecond().try_emplace(VD, NewType, VDField, FiredField, BaseLVal);
11463   } else {
11464     NewType = std::get<0>(VI->getSecond());
11465     VDField = std::get<1>(VI->getSecond());
11466     FiredField = std::get<2>(VI->getSecond());
11467     BaseLVal = std::get<3>(VI->getSecond());
11468   }
11469   LValue FiredLVal =
11470       CGF.EmitLValueForField(BaseLVal, FiredField);
11471   CGF.EmitStoreOfScalar(
11472       llvm::ConstantInt::getNullValue(CGF.ConvertTypeForMem(C.CharTy)),
11473       FiredLVal);
11474   return CGF.EmitLValueForField(BaseLVal, VDField).getAddress(CGF);
11475 }
11476 
11477 namespace {
11478 /// Checks if the lastprivate conditional variable is referenced in LHS.
11479 class LastprivateConditionalRefChecker final
11480     : public ConstStmtVisitor<LastprivateConditionalRefChecker, bool> {
11481   ArrayRef<CGOpenMPRuntime::LastprivateConditionalData> LPM;
11482   const Expr *FoundE = nullptr;
11483   const Decl *FoundD = nullptr;
11484   StringRef UniqueDeclName;
11485   LValue IVLVal;
11486   llvm::Function *FoundFn = nullptr;
11487   SourceLocation Loc;
11488 
11489 public:
11490   bool VisitDeclRefExpr(const DeclRefExpr *E) {
11491     for (const CGOpenMPRuntime::LastprivateConditionalData &D :
11492          llvm::reverse(LPM)) {
11493       auto It = D.DeclToUniqueName.find(E->getDecl());
11494       if (It == D.DeclToUniqueName.end())
11495         continue;
11496       if (D.Disabled)
11497         return false;
11498       FoundE = E;
11499       FoundD = E->getDecl()->getCanonicalDecl();
11500       UniqueDeclName = It->second;
11501       IVLVal = D.IVLVal;
11502       FoundFn = D.Fn;
11503       break;
11504     }
11505     return FoundE == E;
11506   }
11507   bool VisitMemberExpr(const MemberExpr *E) {
11508     if (!CodeGenFunction::IsWrappedCXXThis(E->getBase()))
11509       return false;
11510     for (const CGOpenMPRuntime::LastprivateConditionalData &D :
11511          llvm::reverse(LPM)) {
11512       auto It = D.DeclToUniqueName.find(E->getMemberDecl());
11513       if (It == D.DeclToUniqueName.end())
11514         continue;
11515       if (D.Disabled)
11516         return false;
11517       FoundE = E;
11518       FoundD = E->getMemberDecl()->getCanonicalDecl();
11519       UniqueDeclName = It->second;
11520       IVLVal = D.IVLVal;
11521       FoundFn = D.Fn;
11522       break;
11523     }
11524     return FoundE == E;
11525   }
11526   bool VisitStmt(const Stmt *S) {
11527     for (const Stmt *Child : S->children()) {
11528       if (!Child)
11529         continue;
11530       if (const auto *E = dyn_cast<Expr>(Child))
11531         if (!E->isGLValue())
11532           continue;
11533       if (Visit(Child))
11534         return true;
11535     }
11536     return false;
11537   }
11538   explicit LastprivateConditionalRefChecker(
11539       ArrayRef<CGOpenMPRuntime::LastprivateConditionalData> LPM)
11540       : LPM(LPM) {}
11541   std::tuple<const Expr *, const Decl *, StringRef, LValue, llvm::Function *>
11542   getFoundData() const {
11543     return std::make_tuple(FoundE, FoundD, UniqueDeclName, IVLVal, FoundFn);
11544   }
11545 };
11546 } // namespace
11547 
11548 void CGOpenMPRuntime::emitLastprivateConditionalUpdate(CodeGenFunction &CGF,
11549                                                        LValue IVLVal,
11550                                                        StringRef UniqueDeclName,
11551                                                        LValue LVal,
11552                                                        SourceLocation Loc) {
11553   // Last updated loop counter for the lastprivate conditional var.
11554   // int<xx> last_iv = 0;
11555   llvm::Type *LLIVTy = CGF.ConvertTypeForMem(IVLVal.getType());
11556   llvm::Constant *LastIV =
11557       getOrCreateInternalVariable(LLIVTy, getName({UniqueDeclName, "iv"}));
11558   cast<llvm::GlobalVariable>(LastIV)->setAlignment(
11559       IVLVal.getAlignment().getAsAlign());
11560   LValue LastIVLVal = CGF.MakeNaturalAlignAddrLValue(LastIV, IVLVal.getType());
11561 
11562   // Last value of the lastprivate conditional.
11563   // decltype(priv_a) last_a;
11564   llvm::Constant *Last = getOrCreateInternalVariable(
11565       CGF.ConvertTypeForMem(LVal.getType()), UniqueDeclName);
11566   cast<llvm::GlobalVariable>(Last)->setAlignment(
11567       LVal.getAlignment().getAsAlign());
11568   LValue LastLVal =
11569       CGF.MakeAddrLValue(Last, LVal.getType(), LVal.getAlignment());
11570 
11571   // Global loop counter. Required to handle inner parallel-for regions.
11572   // iv
11573   llvm::Value *IVVal = CGF.EmitLoadOfScalar(IVLVal, Loc);
11574 
11575   // #pragma omp critical(a)
11576   // if (last_iv <= iv) {
11577   //   last_iv = iv;
11578   //   last_a = priv_a;
11579   // }
11580   auto &&CodeGen = [&LastIVLVal, &IVLVal, IVVal, &LVal, &LastLVal,
11581                     Loc](CodeGenFunction &CGF, PrePostActionTy &Action) {
11582     Action.Enter(CGF);
11583     llvm::Value *LastIVVal = CGF.EmitLoadOfScalar(LastIVLVal, Loc);
11584     // (last_iv <= iv) ? Check if the variable is updated and store new
11585     // value in global var.
11586     llvm::Value *CmpRes;
11587     if (IVLVal.getType()->isSignedIntegerType()) {
11588       CmpRes = CGF.Builder.CreateICmpSLE(LastIVVal, IVVal);
11589     } else {
11590       assert(IVLVal.getType()->isUnsignedIntegerType() &&
11591              "Loop iteration variable must be integer.");
11592       CmpRes = CGF.Builder.CreateICmpULE(LastIVVal, IVVal);
11593     }
11594     llvm::BasicBlock *ThenBB = CGF.createBasicBlock("lp_cond_then");
11595     llvm::BasicBlock *ExitBB = CGF.createBasicBlock("lp_cond_exit");
11596     CGF.Builder.CreateCondBr(CmpRes, ThenBB, ExitBB);
11597     // {
11598     CGF.EmitBlock(ThenBB);
11599 
11600     //   last_iv = iv;
11601     CGF.EmitStoreOfScalar(IVVal, LastIVLVal);
11602 
11603     //   last_a = priv_a;
11604     switch (CGF.getEvaluationKind(LVal.getType())) {
11605     case TEK_Scalar: {
11606       llvm::Value *PrivVal = CGF.EmitLoadOfScalar(LVal, Loc);
11607       CGF.EmitStoreOfScalar(PrivVal, LastLVal);
11608       break;
11609     }
11610     case TEK_Complex: {
11611       CodeGenFunction::ComplexPairTy PrivVal = CGF.EmitLoadOfComplex(LVal, Loc);
11612       CGF.EmitStoreOfComplex(PrivVal, LastLVal, /*isInit=*/false);
11613       break;
11614     }
11615     case TEK_Aggregate:
11616       llvm_unreachable(
11617           "Aggregates are not supported in lastprivate conditional.");
11618     }
11619     // }
11620     CGF.EmitBranch(ExitBB);
11621     // There is no need to emit line number for unconditional branch.
11622     (void)ApplyDebugLocation::CreateEmpty(CGF);
11623     CGF.EmitBlock(ExitBB, /*IsFinished=*/true);
11624   };
11625 
11626   if (CGM.getLangOpts().OpenMPSimd) {
11627     // Do not emit as a critical region as no parallel region could be emitted.
11628     RegionCodeGenTy ThenRCG(CodeGen);
11629     ThenRCG(CGF);
11630   } else {
11631     emitCriticalRegion(CGF, UniqueDeclName, CodeGen, Loc);
11632   }
11633 }
11634 
11635 void CGOpenMPRuntime::checkAndEmitLastprivateConditional(CodeGenFunction &CGF,
11636                                                          const Expr *LHS) {
11637   if (CGF.getLangOpts().OpenMP < 50 || LastprivateConditionalStack.empty())
11638     return;
11639   LastprivateConditionalRefChecker Checker(LastprivateConditionalStack);
11640   if (!Checker.Visit(LHS))
11641     return;
11642   const Expr *FoundE;
11643   const Decl *FoundD;
11644   StringRef UniqueDeclName;
11645   LValue IVLVal;
11646   llvm::Function *FoundFn;
11647   std::tie(FoundE, FoundD, UniqueDeclName, IVLVal, FoundFn) =
11648       Checker.getFoundData();
11649   if (FoundFn != CGF.CurFn) {
11650     // Special codegen for inner parallel regions.
11651     // ((struct.lastprivate.conditional*)&priv_a)->Fired = 1;
11652     auto It = LastprivateConditionalToTypes[FoundFn].find(FoundD);
11653     assert(It != LastprivateConditionalToTypes[FoundFn].end() &&
11654            "Lastprivate conditional is not found in outer region.");
11655     QualType StructTy = std::get<0>(It->getSecond());
11656     const FieldDecl* FiredDecl = std::get<2>(It->getSecond());
11657     LValue PrivLVal = CGF.EmitLValue(FoundE);
11658     Address StructAddr = CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(
11659         PrivLVal.getAddress(CGF),
11660         CGF.ConvertTypeForMem(CGF.getContext().getPointerType(StructTy)));
11661     LValue BaseLVal =
11662         CGF.MakeAddrLValue(StructAddr, StructTy, AlignmentSource::Decl);
11663     LValue FiredLVal = CGF.EmitLValueForField(BaseLVal, FiredDecl);
11664     CGF.EmitAtomicStore(RValue::get(llvm::ConstantInt::get(
11665                             CGF.ConvertTypeForMem(FiredDecl->getType()), 1)),
11666                         FiredLVal, llvm::AtomicOrdering::Unordered,
11667                         /*IsVolatile=*/true, /*isInit=*/false);
11668     return;
11669   }
11670 
11671   // Private address of the lastprivate conditional in the current context.
11672   // priv_a
11673   LValue LVal = CGF.EmitLValue(FoundE);
11674   emitLastprivateConditionalUpdate(CGF, IVLVal, UniqueDeclName, LVal,
11675                                    FoundE->getExprLoc());
11676 }
11677 
11678 void CGOpenMPRuntime::checkAndEmitSharedLastprivateConditional(
11679     CodeGenFunction &CGF, const OMPExecutableDirective &D,
11680     const llvm::DenseSet<CanonicalDeclPtr<const VarDecl>> &IgnoredDecls) {
11681   if (CGF.getLangOpts().OpenMP < 50 || LastprivateConditionalStack.empty())
11682     return;
11683   auto Range = llvm::reverse(LastprivateConditionalStack);
11684   auto It = llvm::find_if(
11685       Range, [](const LastprivateConditionalData &D) { return !D.Disabled; });
11686   if (It == Range.end() || It->Fn != CGF.CurFn)
11687     return;
11688   auto LPCI = LastprivateConditionalToTypes.find(It->Fn);
11689   assert(LPCI != LastprivateConditionalToTypes.end() &&
11690          "Lastprivates must be registered already.");
11691   SmallVector<OpenMPDirectiveKind, 4> CaptureRegions;
11692   getOpenMPCaptureRegions(CaptureRegions, D.getDirectiveKind());
11693   const CapturedStmt *CS = D.getCapturedStmt(CaptureRegions.back());
11694   for (const auto &Pair : It->DeclToUniqueName) {
11695     const auto *VD = cast<VarDecl>(Pair.first->getCanonicalDecl());
11696     if (!CS->capturesVariable(VD) || IgnoredDecls.count(VD) > 0)
11697       continue;
11698     auto I = LPCI->getSecond().find(Pair.first);
11699     assert(I != LPCI->getSecond().end() &&
11700            "Lastprivate must be rehistered already.");
11701     // bool Cmp = priv_a.Fired != 0;
11702     LValue BaseLVal = std::get<3>(I->getSecond());
11703     LValue FiredLVal =
11704         CGF.EmitLValueForField(BaseLVal, std::get<2>(I->getSecond()));
11705     llvm::Value *Res = CGF.EmitLoadOfScalar(FiredLVal, D.getBeginLoc());
11706     llvm::Value *Cmp = CGF.Builder.CreateIsNotNull(Res);
11707     llvm::BasicBlock *ThenBB = CGF.createBasicBlock("lpc.then");
11708     llvm::BasicBlock *DoneBB = CGF.createBasicBlock("lpc.done");
11709     // if (Cmp) {
11710     CGF.Builder.CreateCondBr(Cmp, ThenBB, DoneBB);
11711     CGF.EmitBlock(ThenBB);
11712     Address Addr = CGF.GetAddrOfLocalVar(VD);
11713     LValue LVal;
11714     if (VD->getType()->isReferenceType())
11715       LVal = CGF.EmitLoadOfReferenceLValue(Addr, VD->getType(),
11716                                            AlignmentSource::Decl);
11717     else
11718       LVal = CGF.MakeAddrLValue(Addr, VD->getType().getNonReferenceType(),
11719                                 AlignmentSource::Decl);
11720     emitLastprivateConditionalUpdate(CGF, It->IVLVal, Pair.second, LVal,
11721                                      D.getBeginLoc());
11722     auto AL = ApplyDebugLocation::CreateArtificial(CGF);
11723     CGF.EmitBlock(DoneBB, /*IsFinal=*/true);
11724     // }
11725   }
11726 }
11727 
11728 void CGOpenMPRuntime::emitLastprivateConditionalFinalUpdate(
11729     CodeGenFunction &CGF, LValue PrivLVal, const VarDecl *VD,
11730     SourceLocation Loc) {
11731   if (CGF.getLangOpts().OpenMP < 50)
11732     return;
11733   auto It = LastprivateConditionalStack.back().DeclToUniqueName.find(VD);
11734   assert(It != LastprivateConditionalStack.back().DeclToUniqueName.end() &&
11735          "Unknown lastprivate conditional variable.");
11736   StringRef UniqueName = It->second;
11737   llvm::GlobalVariable *GV = CGM.getModule().getNamedGlobal(UniqueName);
11738   // The variable was not updated in the region - exit.
11739   if (!GV)
11740     return;
11741   LValue LPLVal = CGF.MakeAddrLValue(
11742       GV, PrivLVal.getType().getNonReferenceType(), PrivLVal.getAlignment());
11743   llvm::Value *Res = CGF.EmitLoadOfScalar(LPLVal, Loc);
11744   CGF.EmitStoreOfScalar(Res, PrivLVal);
11745 }
11746 
11747 llvm::Function *CGOpenMPSIMDRuntime::emitParallelOutlinedFunction(
11748     const OMPExecutableDirective &D, const VarDecl *ThreadIDVar,
11749     OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen) {
11750   llvm_unreachable("Not supported in SIMD-only mode");
11751 }
11752 
11753 llvm::Function *CGOpenMPSIMDRuntime::emitTeamsOutlinedFunction(
11754     const OMPExecutableDirective &D, const VarDecl *ThreadIDVar,
11755     OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen) {
11756   llvm_unreachable("Not supported in SIMD-only mode");
11757 }
11758 
11759 llvm::Function *CGOpenMPSIMDRuntime::emitTaskOutlinedFunction(
11760     const OMPExecutableDirective &D, const VarDecl *ThreadIDVar,
11761     const VarDecl *PartIDVar, const VarDecl *TaskTVar,
11762     OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen,
11763     bool Tied, unsigned &NumberOfParts) {
11764   llvm_unreachable("Not supported in SIMD-only mode");
11765 }
11766 
11767 void CGOpenMPSIMDRuntime::emitParallelCall(CodeGenFunction &CGF,
11768                                            SourceLocation Loc,
11769                                            llvm::Function *OutlinedFn,
11770                                            ArrayRef<llvm::Value *> CapturedVars,
11771                                            const Expr *IfCond) {
11772   llvm_unreachable("Not supported in SIMD-only mode");
11773 }
11774 
11775 void CGOpenMPSIMDRuntime::emitCriticalRegion(
11776     CodeGenFunction &CGF, StringRef CriticalName,
11777     const RegionCodeGenTy &CriticalOpGen, SourceLocation Loc,
11778     const Expr *Hint) {
11779   llvm_unreachable("Not supported in SIMD-only mode");
11780 }
11781 
11782 void CGOpenMPSIMDRuntime::emitMasterRegion(CodeGenFunction &CGF,
11783                                            const RegionCodeGenTy &MasterOpGen,
11784                                            SourceLocation Loc) {
11785   llvm_unreachable("Not supported in SIMD-only mode");
11786 }
11787 
11788 void CGOpenMPSIMDRuntime::emitTaskyieldCall(CodeGenFunction &CGF,
11789                                             SourceLocation Loc) {
11790   llvm_unreachable("Not supported in SIMD-only mode");
11791 }
11792 
11793 void CGOpenMPSIMDRuntime::emitTaskgroupRegion(
11794     CodeGenFunction &CGF, const RegionCodeGenTy &TaskgroupOpGen,
11795     SourceLocation Loc) {
11796   llvm_unreachable("Not supported in SIMD-only mode");
11797 }
11798 
11799 void CGOpenMPSIMDRuntime::emitSingleRegion(
11800     CodeGenFunction &CGF, const RegionCodeGenTy &SingleOpGen,
11801     SourceLocation Loc, ArrayRef<const Expr *> CopyprivateVars,
11802     ArrayRef<const Expr *> DestExprs, ArrayRef<const Expr *> SrcExprs,
11803     ArrayRef<const Expr *> AssignmentOps) {
11804   llvm_unreachable("Not supported in SIMD-only mode");
11805 }
11806 
11807 void CGOpenMPSIMDRuntime::emitOrderedRegion(CodeGenFunction &CGF,
11808                                             const RegionCodeGenTy &OrderedOpGen,
11809                                             SourceLocation Loc,
11810                                             bool IsThreads) {
11811   llvm_unreachable("Not supported in SIMD-only mode");
11812 }
11813 
11814 void CGOpenMPSIMDRuntime::emitBarrierCall(CodeGenFunction &CGF,
11815                                           SourceLocation Loc,
11816                                           OpenMPDirectiveKind Kind,
11817                                           bool EmitChecks,
11818                                           bool ForceSimpleCall) {
11819   llvm_unreachable("Not supported in SIMD-only mode");
11820 }
11821 
11822 void CGOpenMPSIMDRuntime::emitForDispatchInit(
11823     CodeGenFunction &CGF, SourceLocation Loc,
11824     const OpenMPScheduleTy &ScheduleKind, unsigned IVSize, bool IVSigned,
11825     bool Ordered, const DispatchRTInput &DispatchValues) {
11826   llvm_unreachable("Not supported in SIMD-only mode");
11827 }
11828 
11829 void CGOpenMPSIMDRuntime::emitForStaticInit(
11830     CodeGenFunction &CGF, SourceLocation Loc, OpenMPDirectiveKind DKind,
11831     const OpenMPScheduleTy &ScheduleKind, const StaticRTInput &Values) {
11832   llvm_unreachable("Not supported in SIMD-only mode");
11833 }
11834 
11835 void CGOpenMPSIMDRuntime::emitDistributeStaticInit(
11836     CodeGenFunction &CGF, SourceLocation Loc,
11837     OpenMPDistScheduleClauseKind SchedKind, const StaticRTInput &Values) {
11838   llvm_unreachable("Not supported in SIMD-only mode");
11839 }
11840 
11841 void CGOpenMPSIMDRuntime::emitForOrderedIterationEnd(CodeGenFunction &CGF,
11842                                                      SourceLocation Loc,
11843                                                      unsigned IVSize,
11844                                                      bool IVSigned) {
11845   llvm_unreachable("Not supported in SIMD-only mode");
11846 }
11847 
11848 void CGOpenMPSIMDRuntime::emitForStaticFinish(CodeGenFunction &CGF,
11849                                               SourceLocation Loc,
11850                                               OpenMPDirectiveKind DKind) {
11851   llvm_unreachable("Not supported in SIMD-only mode");
11852 }
11853 
11854 llvm::Value *CGOpenMPSIMDRuntime::emitForNext(CodeGenFunction &CGF,
11855                                               SourceLocation Loc,
11856                                               unsigned IVSize, bool IVSigned,
11857                                               Address IL, Address LB,
11858                                               Address UB, Address ST) {
11859   llvm_unreachable("Not supported in SIMD-only mode");
11860 }
11861 
11862 void CGOpenMPSIMDRuntime::emitNumThreadsClause(CodeGenFunction &CGF,
11863                                                llvm::Value *NumThreads,
11864                                                SourceLocation Loc) {
11865   llvm_unreachable("Not supported in SIMD-only mode");
11866 }
11867 
11868 void CGOpenMPSIMDRuntime::emitProcBindClause(CodeGenFunction &CGF,
11869                                              ProcBindKind ProcBind,
11870                                              SourceLocation Loc) {
11871   llvm_unreachable("Not supported in SIMD-only mode");
11872 }
11873 
11874 Address CGOpenMPSIMDRuntime::getAddrOfThreadPrivate(CodeGenFunction &CGF,
11875                                                     const VarDecl *VD,
11876                                                     Address VDAddr,
11877                                                     SourceLocation Loc) {
11878   llvm_unreachable("Not supported in SIMD-only mode");
11879 }
11880 
11881 llvm::Function *CGOpenMPSIMDRuntime::emitThreadPrivateVarDefinition(
11882     const VarDecl *VD, Address VDAddr, SourceLocation Loc, bool PerformInit,
11883     CodeGenFunction *CGF) {
11884   llvm_unreachable("Not supported in SIMD-only mode");
11885 }
11886 
11887 Address CGOpenMPSIMDRuntime::getAddrOfArtificialThreadPrivate(
11888     CodeGenFunction &CGF, QualType VarType, StringRef Name) {
11889   llvm_unreachable("Not supported in SIMD-only mode");
11890 }
11891 
11892 void CGOpenMPSIMDRuntime::emitFlush(CodeGenFunction &CGF,
11893                                     ArrayRef<const Expr *> Vars,
11894                                     SourceLocation Loc,
11895                                     llvm::AtomicOrdering AO) {
11896   llvm_unreachable("Not supported in SIMD-only mode");
11897 }
11898 
11899 void CGOpenMPSIMDRuntime::emitTaskCall(CodeGenFunction &CGF, SourceLocation Loc,
11900                                        const OMPExecutableDirective &D,
11901                                        llvm::Function *TaskFunction,
11902                                        QualType SharedsTy, Address Shareds,
11903                                        const Expr *IfCond,
11904                                        const OMPTaskDataTy &Data) {
11905   llvm_unreachable("Not supported in SIMD-only mode");
11906 }
11907 
11908 void CGOpenMPSIMDRuntime::emitTaskLoopCall(
11909     CodeGenFunction &CGF, SourceLocation Loc, const OMPLoopDirective &D,
11910     llvm::Function *TaskFunction, QualType SharedsTy, Address Shareds,
11911     const Expr *IfCond, const OMPTaskDataTy &Data) {
11912   llvm_unreachable("Not supported in SIMD-only mode");
11913 }
11914 
11915 void CGOpenMPSIMDRuntime::emitReduction(
11916     CodeGenFunction &CGF, SourceLocation Loc, ArrayRef<const Expr *> Privates,
11917     ArrayRef<const Expr *> LHSExprs, ArrayRef<const Expr *> RHSExprs,
11918     ArrayRef<const Expr *> ReductionOps, ReductionOptionsTy Options) {
11919   assert(Options.SimpleReduction && "Only simple reduction is expected.");
11920   CGOpenMPRuntime::emitReduction(CGF, Loc, Privates, LHSExprs, RHSExprs,
11921                                  ReductionOps, Options);
11922 }
11923 
11924 llvm::Value *CGOpenMPSIMDRuntime::emitTaskReductionInit(
11925     CodeGenFunction &CGF, SourceLocation Loc, ArrayRef<const Expr *> LHSExprs,
11926     ArrayRef<const Expr *> RHSExprs, const OMPTaskDataTy &Data) {
11927   llvm_unreachable("Not supported in SIMD-only mode");
11928 }
11929 
11930 void CGOpenMPSIMDRuntime::emitTaskReductionFini(CodeGenFunction &CGF,
11931                                                 SourceLocation Loc,
11932                                                 bool IsWorksharingReduction) {
11933   llvm_unreachable("Not supported in SIMD-only mode");
11934 }
11935 
11936 void CGOpenMPSIMDRuntime::emitTaskReductionFixups(CodeGenFunction &CGF,
11937                                                   SourceLocation Loc,
11938                                                   ReductionCodeGen &RCG,
11939                                                   unsigned N) {
11940   llvm_unreachable("Not supported in SIMD-only mode");
11941 }
11942 
11943 Address CGOpenMPSIMDRuntime::getTaskReductionItem(CodeGenFunction &CGF,
11944                                                   SourceLocation Loc,
11945                                                   llvm::Value *ReductionsPtr,
11946                                                   LValue SharedLVal) {
11947   llvm_unreachable("Not supported in SIMD-only mode");
11948 }
11949 
11950 void CGOpenMPSIMDRuntime::emitTaskwaitCall(CodeGenFunction &CGF,
11951                                            SourceLocation Loc) {
11952   llvm_unreachable("Not supported in SIMD-only mode");
11953 }
11954 
11955 void CGOpenMPSIMDRuntime::emitCancellationPointCall(
11956     CodeGenFunction &CGF, SourceLocation Loc,
11957     OpenMPDirectiveKind CancelRegion) {
11958   llvm_unreachable("Not supported in SIMD-only mode");
11959 }
11960 
11961 void CGOpenMPSIMDRuntime::emitCancelCall(CodeGenFunction &CGF,
11962                                          SourceLocation Loc, const Expr *IfCond,
11963                                          OpenMPDirectiveKind CancelRegion) {
11964   llvm_unreachable("Not supported in SIMD-only mode");
11965 }
11966 
11967 void CGOpenMPSIMDRuntime::emitTargetOutlinedFunction(
11968     const OMPExecutableDirective &D, StringRef ParentName,
11969     llvm::Function *&OutlinedFn, llvm::Constant *&OutlinedFnID,
11970     bool IsOffloadEntry, const RegionCodeGenTy &CodeGen) {
11971   llvm_unreachable("Not supported in SIMD-only mode");
11972 }
11973 
11974 void CGOpenMPSIMDRuntime::emitTargetCall(
11975     CodeGenFunction &CGF, const OMPExecutableDirective &D,
11976     llvm::Function *OutlinedFn, llvm::Value *OutlinedFnID, const Expr *IfCond,
11977     llvm::PointerIntPair<const Expr *, 2, OpenMPDeviceClauseModifier> Device,
11978     llvm::function_ref<llvm::Value *(CodeGenFunction &CGF,
11979                                      const OMPLoopDirective &D)>
11980         SizeEmitter) {
11981   llvm_unreachable("Not supported in SIMD-only mode");
11982 }
11983 
11984 bool CGOpenMPSIMDRuntime::emitTargetFunctions(GlobalDecl GD) {
11985   llvm_unreachable("Not supported in SIMD-only mode");
11986 }
11987 
11988 bool CGOpenMPSIMDRuntime::emitTargetGlobalVariable(GlobalDecl GD) {
11989   llvm_unreachable("Not supported in SIMD-only mode");
11990 }
11991 
11992 bool CGOpenMPSIMDRuntime::emitTargetGlobal(GlobalDecl GD) {
11993   return false;
11994 }
11995 
11996 void CGOpenMPSIMDRuntime::emitTeamsCall(CodeGenFunction &CGF,
11997                                         const OMPExecutableDirective &D,
11998                                         SourceLocation Loc,
11999                                         llvm::Function *OutlinedFn,
12000                                         ArrayRef<llvm::Value *> CapturedVars) {
12001   llvm_unreachable("Not supported in SIMD-only mode");
12002 }
12003 
12004 void CGOpenMPSIMDRuntime::emitNumTeamsClause(CodeGenFunction &CGF,
12005                                              const Expr *NumTeams,
12006                                              const Expr *ThreadLimit,
12007                                              SourceLocation Loc) {
12008   llvm_unreachable("Not supported in SIMD-only mode");
12009 }
12010 
12011 void CGOpenMPSIMDRuntime::emitTargetDataCalls(
12012     CodeGenFunction &CGF, const OMPExecutableDirective &D, const Expr *IfCond,
12013     const Expr *Device, const RegionCodeGenTy &CodeGen, TargetDataInfo &Info) {
12014   llvm_unreachable("Not supported in SIMD-only mode");
12015 }
12016 
12017 void CGOpenMPSIMDRuntime::emitTargetDataStandAloneCall(
12018     CodeGenFunction &CGF, const OMPExecutableDirective &D, const Expr *IfCond,
12019     const Expr *Device) {
12020   llvm_unreachable("Not supported in SIMD-only mode");
12021 }
12022 
12023 void CGOpenMPSIMDRuntime::emitDoacrossInit(CodeGenFunction &CGF,
12024                                            const OMPLoopDirective &D,
12025                                            ArrayRef<Expr *> NumIterations) {
12026   llvm_unreachable("Not supported in SIMD-only mode");
12027 }
12028 
12029 void CGOpenMPSIMDRuntime::emitDoacrossOrdered(CodeGenFunction &CGF,
12030                                               const OMPDependClause *C) {
12031   llvm_unreachable("Not supported in SIMD-only mode");
12032 }
12033 
12034 const VarDecl *
12035 CGOpenMPSIMDRuntime::translateParameter(const FieldDecl *FD,
12036                                         const VarDecl *NativeParam) const {
12037   llvm_unreachable("Not supported in SIMD-only mode");
12038 }
12039 
12040 Address
12041 CGOpenMPSIMDRuntime::getParameterAddress(CodeGenFunction &CGF,
12042                                          const VarDecl *NativeParam,
12043                                          const VarDecl *TargetParam) const {
12044   llvm_unreachable("Not supported in SIMD-only mode");
12045 }
12046