| Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1 | //===----- CGOpenMPRuntime.cpp - Interface to OpenMP Runtimes -------------===// |
| 2 | // |
| Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
| Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This provides a class for OpenMP runtime code generation. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| Reid Kleckner | 9803178 | 2019-12-09 16:11:56 -0800 | [diff] [blame] | 13 | #include "CGOpenMPRuntime.h" |
| Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 14 | #include "CGCXXABI.h" |
| 15 | #include "CGCleanup.h" |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 16 | #include "CGRecordLayout.h" |
| Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 17 | #include "CodeGenFunction.h" |
| Reid Kleckner | 9803178 | 2019-12-09 16:11:56 -0800 | [diff] [blame] | 18 | #include "clang/AST/Attr.h" |
| Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 19 | #include "clang/AST/Decl.h" |
| Alexey Bataev | 0860db9 | 2019-12-19 10:01:10 -0500 | [diff] [blame] | 20 | #include "clang/AST/OpenMPClause.h" |
| Chandler Carruth | 0d9593d | 2015-01-14 11:29:14 +0000 | [diff] [blame] | 21 | #include "clang/AST/StmtOpenMP.h" |
| Alexey Bataev | a58da1a | 2019-12-27 09:44:43 -0500 | [diff] [blame] | 22 | #include "clang/AST/StmtVisitor.h" |
| Richard Smith | bf5bcf2 | 2018-06-26 23:20:26 +0000 | [diff] [blame] | 23 | #include "clang/Basic/BitmaskEnum.h" |
| Alexey Bataev | 2d4f80f | 2020-02-11 15:15:21 -0500 | [diff] [blame] | 24 | #include "clang/Basic/OpenMPKinds.h" |
| Reid Kleckner | 9803178 | 2019-12-09 16:11:56 -0800 | [diff] [blame] | 25 | #include "clang/CodeGen/ConstantInitBuilder.h" |
| Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 26 | #include "llvm/ADT/ArrayRef.h" |
| Alexey Bataev | 4e8231b | 2019-11-05 15:13:30 -0500 | [diff] [blame] | 27 | #include "llvm/ADT/SetOperations.h" |
| Alexey Bataev | 8b32192 | 2020-01-16 15:46:34 -0500 | [diff] [blame] | 28 | #include "llvm/ADT/StringExtras.h" |
| Teresa Johnson | ffc4e24 | 2016-11-11 05:35:12 +0000 | [diff] [blame] | 29 | #include "llvm/Bitcode/BitcodeReader.h" |
| Johannes Doerfert | b3c06db | 2019-11-04 23:00:36 -0600 | [diff] [blame] | 30 | #include "llvm/Frontend/OpenMP/OMPIRBuilder.h" |
| Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 31 | #include "llvm/IR/DerivedTypes.h" |
| 32 | #include "llvm/IR/GlobalValue.h" |
| 33 | #include "llvm/IR/Value.h" |
| Alexey Bataev | 2d4f80f | 2020-02-11 15:15:21 -0500 | [diff] [blame] | 34 | #include "llvm/Support/AtomicOrdering.h" |
| Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 35 | #include "llvm/Support/Format.h" |
| Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 36 | #include "llvm/Support/raw_ostream.h" |
| Alexey Bataev | 23b6942 | 2014-06-18 07:08:49 +0000 | [diff] [blame] | 37 | #include <cassert> |
| Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 38 | |
| 39 | using namespace clang; |
| 40 | using namespace CodeGen; |
| Johannes Doerfert | eb3e81f | 2019-11-04 22:00:49 -0600 | [diff] [blame] | 41 | using namespace llvm::omp; |
| Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 42 | |
| Benjamin Kramer | c52193f | 2014-10-10 13:57:57 +0000 | [diff] [blame] | 43 | namespace { |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 44 | /// Base class for handling code generation inside OpenMP regions. |
| Alexey Bataev | 1809571 | 2014-10-10 12:19:54 +0000 | [diff] [blame] | 45 | class CGOpenMPRegionInfo : public CodeGenFunction::CGCapturedStmtInfo { |
| 46 | public: |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 47 | /// Kinds of OpenMP regions used in codegen. |
| Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 48 | enum CGOpenMPRegionKind { |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 49 | /// Region with outlined function for standalone 'parallel' |
| Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 50 | /// directive. |
| 51 | ParallelOutlinedRegion, |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 52 | /// Region with outlined function for standalone 'task' directive. |
| Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 53 | TaskOutlinedRegion, |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 54 | /// Region for constructs that do not require function outlining, |
| Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 55 | /// like 'for', 'sections', 'atomic' etc. directives. |
| 56 | InlinedRegion, |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 57 | /// Region with outlined function for standalone 'target' directive. |
| Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 58 | TargetRegion, |
| Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 59 | }; |
| Alexey Bataev | 1809571 | 2014-10-10 12:19:54 +0000 | [diff] [blame] | 60 | |
| Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 61 | CGOpenMPRegionInfo(const CapturedStmt &CS, |
| 62 | const CGOpenMPRegionKind RegionKind, |
| Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 63 | const RegionCodeGenTy &CodeGen, OpenMPDirectiveKind Kind, |
| 64 | bool HasCancel) |
| Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 65 | : CGCapturedStmtInfo(CS, CR_OpenMP), RegionKind(RegionKind), |
| Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 66 | CodeGen(CodeGen), Kind(Kind), HasCancel(HasCancel) {} |
| Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 67 | |
| 68 | CGOpenMPRegionInfo(const CGOpenMPRegionKind RegionKind, |
| Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 69 | const RegionCodeGenTy &CodeGen, OpenMPDirectiveKind Kind, |
| 70 | bool HasCancel) |
| Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 71 | : CGCapturedStmtInfo(CR_OpenMP), RegionKind(RegionKind), CodeGen(CodeGen), |
| Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 72 | Kind(Kind), HasCancel(HasCancel) {} |
| Alexey Bataev | 8cbe0a6 | 2015-02-26 10:27:34 +0000 | [diff] [blame] | 73 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 74 | /// Get a variable or parameter for storing global thread id |
| Alexey Bataev | 1809571 | 2014-10-10 12:19:54 +0000 | [diff] [blame] | 75 | /// inside OpenMP construct. |
| Alexey Bataev | 8cbe0a6 | 2015-02-26 10:27:34 +0000 | [diff] [blame] | 76 | virtual const VarDecl *getThreadIDVariable() const = 0; |
| Alexey Bataev | 1809571 | 2014-10-10 12:19:54 +0000 | [diff] [blame] | 77 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 78 | /// Emit the captured statement body. |
| Hans Wennborg | 7eb5464 | 2015-09-10 17:07:54 +0000 | [diff] [blame] | 79 | void EmitBody(CodeGenFunction &CGF, const Stmt *S) override; |
| Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 80 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 81 | /// Get an LValue for the current ThreadID variable. |
| Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 82 | /// \return LValue for thread id variable. This LValue always has type int32*. |
| 83 | virtual LValue getThreadIDVariableLValue(CodeGenFunction &CGF); |
| Alexey Bataev | 1809571 | 2014-10-10 12:19:54 +0000 | [diff] [blame] | 84 | |
| Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 85 | virtual void emitUntiedSwitch(CodeGenFunction & /*CGF*/) {} |
| 86 | |
| Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 87 | CGOpenMPRegionKind getRegionKind() const { return RegionKind; } |
| Alexey Bataev | 8cbe0a6 | 2015-02-26 10:27:34 +0000 | [diff] [blame] | 88 | |
| Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 89 | OpenMPDirectiveKind getDirectiveKind() const { return Kind; } |
| 90 | |
| Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 91 | bool hasCancel() const { return HasCancel; } |
| 92 | |
| Alexey Bataev | 1809571 | 2014-10-10 12:19:54 +0000 | [diff] [blame] | 93 | static bool classof(const CGCapturedStmtInfo *Info) { |
| 94 | return Info->getKind() == CR_OpenMP; |
| 95 | } |
| Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 96 | |
| Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 97 | ~CGOpenMPRegionInfo() override = default; |
| 98 | |
| Alexey Bataev | 8cbe0a6 | 2015-02-26 10:27:34 +0000 | [diff] [blame] | 99 | protected: |
| Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 100 | CGOpenMPRegionKind RegionKind; |
| Hans Wennborg | 45c7439 | 2016-01-12 20:54:36 +0000 | [diff] [blame] | 101 | RegionCodeGenTy CodeGen; |
| Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 102 | OpenMPDirectiveKind Kind; |
| Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 103 | bool HasCancel; |
| Alexey Bataev | 8cbe0a6 | 2015-02-26 10:27:34 +0000 | [diff] [blame] | 104 | }; |
| Alexey Bataev | 1809571 | 2014-10-10 12:19:54 +0000 | [diff] [blame] | 105 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 106 | /// API for captured statement code generation in OpenMP constructs. |
| Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 107 | class CGOpenMPOutlinedRegionInfo final : public CGOpenMPRegionInfo { |
| Alexey Bataev | 8cbe0a6 | 2015-02-26 10:27:34 +0000 | [diff] [blame] | 108 | public: |
| Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 109 | CGOpenMPOutlinedRegionInfo(const CapturedStmt &CS, const VarDecl *ThreadIDVar, |
| Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 110 | const RegionCodeGenTy &CodeGen, |
| Arpith Chacko Jacob | bb36fe8 | 2017-01-10 15:42:51 +0000 | [diff] [blame] | 111 | OpenMPDirectiveKind Kind, bool HasCancel, |
| 112 | StringRef HelperName) |
| Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 113 | : CGOpenMPRegionInfo(CS, ParallelOutlinedRegion, CodeGen, Kind, |
| 114 | HasCancel), |
| Arpith Chacko Jacob | bb36fe8 | 2017-01-10 15:42:51 +0000 | [diff] [blame] | 115 | ThreadIDVar(ThreadIDVar), HelperName(HelperName) { |
| Alexey Bataev | 8cbe0a6 | 2015-02-26 10:27:34 +0000 | [diff] [blame] | 116 | assert(ThreadIDVar != nullptr && "No ThreadID in OpenMP region."); |
| 117 | } |
| Eugene Zelenko | 0a4f3f4 | 2016-02-10 19:11:58 +0000 | [diff] [blame] | 118 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 119 | /// Get a variable or parameter for storing global thread id |
| Alexey Bataev | 8cbe0a6 | 2015-02-26 10:27:34 +0000 | [diff] [blame] | 120 | /// inside OpenMP construct. |
| Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 121 | const VarDecl *getThreadIDVariable() const override { return ThreadIDVar; } |
| Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 122 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 123 | /// Get the name of the capture helper. |
| Arpith Chacko Jacob | bb36fe8 | 2017-01-10 15:42:51 +0000 | [diff] [blame] | 124 | StringRef getHelperName() const override { return HelperName; } |
| Alexey Bataev | 1809571 | 2014-10-10 12:19:54 +0000 | [diff] [blame] | 125 | |
| Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 126 | static bool classof(const CGCapturedStmtInfo *Info) { |
| 127 | return CGOpenMPRegionInfo::classof(Info) && |
| 128 | cast<CGOpenMPRegionInfo>(Info)->getRegionKind() == |
| 129 | ParallelOutlinedRegion; |
| 130 | } |
| 131 | |
| Alexey Bataev | 1809571 | 2014-10-10 12:19:54 +0000 | [diff] [blame] | 132 | private: |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 133 | /// A variable or parameter storing global thread id for OpenMP |
| Alexey Bataev | 1809571 | 2014-10-10 12:19:54 +0000 | [diff] [blame] | 134 | /// constructs. |
| 135 | const VarDecl *ThreadIDVar; |
| Arpith Chacko Jacob | bb36fe8 | 2017-01-10 15:42:51 +0000 | [diff] [blame] | 136 | StringRef HelperName; |
| Alexey Bataev | 8cbe0a6 | 2015-02-26 10:27:34 +0000 | [diff] [blame] | 137 | }; |
| 138 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 139 | /// API for captured statement code generation in OpenMP constructs. |
| Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 140 | class CGOpenMPTaskOutlinedRegionInfo final : public CGOpenMPRegionInfo { |
| Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 141 | public: |
| Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 142 | class UntiedTaskActionTy final : public PrePostActionTy { |
| 143 | bool Untied; |
| 144 | const VarDecl *PartIDVar; |
| 145 | const RegionCodeGenTy UntiedCodeGen; |
| 146 | llvm::SwitchInst *UntiedSwitch = nullptr; |
| 147 | |
| 148 | public: |
| 149 | UntiedTaskActionTy(bool Tied, const VarDecl *PartIDVar, |
| 150 | const RegionCodeGenTy &UntiedCodeGen) |
| 151 | : Untied(!Tied), PartIDVar(PartIDVar), UntiedCodeGen(UntiedCodeGen) {} |
| 152 | void Enter(CodeGenFunction &CGF) override { |
| 153 | if (Untied) { |
| 154 | // Emit task switching point. |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 155 | LValue PartIdLVal = CGF.EmitLoadOfPointerLValue( |
| Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 156 | CGF.GetAddrOfLocalVar(PartIDVar), |
| 157 | PartIDVar->getType()->castAs<PointerType>()); |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 158 | llvm::Value *Res = |
| 159 | CGF.EmitLoadOfScalar(PartIdLVal, PartIDVar->getLocation()); |
| 160 | llvm::BasicBlock *DoneBB = CGF.createBasicBlock(".untied.done."); |
| Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 161 | UntiedSwitch = CGF.Builder.CreateSwitch(Res, DoneBB); |
| 162 | CGF.EmitBlock(DoneBB); |
| 163 | CGF.EmitBranchThroughCleanup(CGF.ReturnBlock); |
| 164 | CGF.EmitBlock(CGF.createBasicBlock(".untied.jmp.")); |
| 165 | UntiedSwitch->addCase(CGF.Builder.getInt32(0), |
| 166 | CGF.Builder.GetInsertBlock()); |
| 167 | emitUntiedSwitch(CGF); |
| 168 | } |
| 169 | } |
| 170 | void emitUntiedSwitch(CodeGenFunction &CGF) const { |
| 171 | if (Untied) { |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 172 | LValue PartIdLVal = CGF.EmitLoadOfPointerLValue( |
| Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 173 | CGF.GetAddrOfLocalVar(PartIDVar), |
| 174 | PartIDVar->getType()->castAs<PointerType>()); |
| 175 | CGF.EmitStoreOfScalar(CGF.Builder.getInt32(UntiedSwitch->getNumCases()), |
| 176 | PartIdLVal); |
| 177 | UntiedCodeGen(CGF); |
| 178 | CodeGenFunction::JumpDest CurPoint = |
| 179 | CGF.getJumpDestInCurrentScope(".untied.next."); |
| 180 | CGF.EmitBranchThroughCleanup(CGF.ReturnBlock); |
| 181 | CGF.EmitBlock(CGF.createBasicBlock(".untied.jmp.")); |
| 182 | UntiedSwitch->addCase(CGF.Builder.getInt32(UntiedSwitch->getNumCases()), |
| 183 | CGF.Builder.GetInsertBlock()); |
| 184 | CGF.EmitBranchThroughCleanup(CurPoint); |
| 185 | CGF.EmitBlock(CurPoint.getBlock()); |
| 186 | } |
| 187 | } |
| 188 | unsigned getNumberOfParts() const { return UntiedSwitch->getNumCases(); } |
| 189 | }; |
| Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 190 | CGOpenMPTaskOutlinedRegionInfo(const CapturedStmt &CS, |
| Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 191 | const VarDecl *ThreadIDVar, |
| Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 192 | const RegionCodeGenTy &CodeGen, |
| Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 193 | OpenMPDirectiveKind Kind, bool HasCancel, |
| 194 | const UntiedTaskActionTy &Action) |
| Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 195 | : CGOpenMPRegionInfo(CS, TaskOutlinedRegion, CodeGen, Kind, HasCancel), |
| Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 196 | ThreadIDVar(ThreadIDVar), Action(Action) { |
| Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 197 | assert(ThreadIDVar != nullptr && "No ThreadID in OpenMP region."); |
| 198 | } |
| Eugene Zelenko | 0a4f3f4 | 2016-02-10 19:11:58 +0000 | [diff] [blame] | 199 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 200 | /// Get a variable or parameter for storing global thread id |
| Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 201 | /// inside OpenMP construct. |
| Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 202 | const VarDecl *getThreadIDVariable() const override { return ThreadIDVar; } |
| Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 203 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 204 | /// Get an LValue for the current ThreadID variable. |
| Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 205 | LValue getThreadIDVariableLValue(CodeGenFunction &CGF) override; |
| Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 206 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 207 | /// Get the name of the capture helper. |
| Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 208 | StringRef getHelperName() const override { return ".omp_outlined."; } |
| 209 | |
| Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 210 | void emitUntiedSwitch(CodeGenFunction &CGF) override { |
| 211 | Action.emitUntiedSwitch(CGF); |
| 212 | } |
| 213 | |
| Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 214 | static bool classof(const CGCapturedStmtInfo *Info) { |
| 215 | return CGOpenMPRegionInfo::classof(Info) && |
| 216 | cast<CGOpenMPRegionInfo>(Info)->getRegionKind() == |
| 217 | TaskOutlinedRegion; |
| 218 | } |
| 219 | |
| Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 220 | private: |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 221 | /// A variable or parameter storing global thread id for OpenMP |
| Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 222 | /// constructs. |
| 223 | const VarDecl *ThreadIDVar; |
| Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 224 | /// Action for emitting code for untied tasks. |
| 225 | const UntiedTaskActionTy &Action; |
| Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 226 | }; |
| 227 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 228 | /// API for inlined captured statement code generation in OpenMP |
| Alexey Bataev | 8cbe0a6 | 2015-02-26 10:27:34 +0000 | [diff] [blame] | 229 | /// constructs. |
| 230 | class CGOpenMPInlinedRegionInfo : public CGOpenMPRegionInfo { |
| 231 | public: |
| Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 232 | CGOpenMPInlinedRegionInfo(CodeGenFunction::CGCapturedStmtInfo *OldCSI, |
| Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 233 | const RegionCodeGenTy &CodeGen, |
| Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 234 | OpenMPDirectiveKind Kind, bool HasCancel) |
| 235 | : CGOpenMPRegionInfo(InlinedRegion, CodeGen, Kind, HasCancel), |
| 236 | OldCSI(OldCSI), |
| Alexey Bataev | 8cbe0a6 | 2015-02-26 10:27:34 +0000 | [diff] [blame] | 237 | OuterRegionInfo(dyn_cast_or_null<CGOpenMPRegionInfo>(OldCSI)) {} |
| Eugene Zelenko | 0a4f3f4 | 2016-02-10 19:11:58 +0000 | [diff] [blame] | 238 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 239 | // Retrieve the value of the context parameter. |
| Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 240 | llvm::Value *getContextValue() const override { |
| Alexey Bataev | 8cbe0a6 | 2015-02-26 10:27:34 +0000 | [diff] [blame] | 241 | if (OuterRegionInfo) |
| 242 | return OuterRegionInfo->getContextValue(); |
| 243 | llvm_unreachable("No context value for inlined OpenMP region"); |
| 244 | } |
| Eugene Zelenko | 0a4f3f4 | 2016-02-10 19:11:58 +0000 | [diff] [blame] | 245 | |
| Hans Wennborg | 7eb5464 | 2015-09-10 17:07:54 +0000 | [diff] [blame] | 246 | void setContextValue(llvm::Value *V) override { |
| Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 247 | if (OuterRegionInfo) { |
| 248 | OuterRegionInfo->setContextValue(V); |
| 249 | return; |
| 250 | } |
| 251 | llvm_unreachable("No context value for inlined OpenMP region"); |
| 252 | } |
| Eugene Zelenko | 0a4f3f4 | 2016-02-10 19:11:58 +0000 | [diff] [blame] | 253 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 254 | /// Lookup the captured field decl for a variable. |
| Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 255 | const FieldDecl *lookup(const VarDecl *VD) const override { |
| Alexey Bataev | 8cbe0a6 | 2015-02-26 10:27:34 +0000 | [diff] [blame] | 256 | if (OuterRegionInfo) |
| 257 | return OuterRegionInfo->lookup(VD); |
| Alexey Bataev | 69c62a9 | 2015-04-15 04:52:20 +0000 | [diff] [blame] | 258 | // If there is no outer outlined region,no need to lookup in a list of |
| 259 | // captured variables, we can use the original one. |
| 260 | return nullptr; |
| Alexey Bataev | 8cbe0a6 | 2015-02-26 10:27:34 +0000 | [diff] [blame] | 261 | } |
| Eugene Zelenko | 0a4f3f4 | 2016-02-10 19:11:58 +0000 | [diff] [blame] | 262 | |
| Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 263 | FieldDecl *getThisFieldDecl() const override { |
| Alexey Bataev | 8cbe0a6 | 2015-02-26 10:27:34 +0000 | [diff] [blame] | 264 | if (OuterRegionInfo) |
| 265 | return OuterRegionInfo->getThisFieldDecl(); |
| 266 | return nullptr; |
| 267 | } |
| Eugene Zelenko | 0a4f3f4 | 2016-02-10 19:11:58 +0000 | [diff] [blame] | 268 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 269 | /// Get a variable or parameter for storing global thread id |
| Alexey Bataev | 8cbe0a6 | 2015-02-26 10:27:34 +0000 | [diff] [blame] | 270 | /// inside OpenMP construct. |
| Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 271 | const VarDecl *getThreadIDVariable() const override { |
| Alexey Bataev | 8cbe0a6 | 2015-02-26 10:27:34 +0000 | [diff] [blame] | 272 | if (OuterRegionInfo) |
| 273 | return OuterRegionInfo->getThreadIDVariable(); |
| 274 | return nullptr; |
| 275 | } |
| Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 276 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 277 | /// Get an LValue for the current ThreadID variable. |
| Alexey Bataev | 311a928 | 2017-10-12 13:51:32 +0000 | [diff] [blame] | 278 | LValue getThreadIDVariableLValue(CodeGenFunction &CGF) override { |
| 279 | if (OuterRegionInfo) |
| 280 | return OuterRegionInfo->getThreadIDVariableLValue(CGF); |
| 281 | llvm_unreachable("No LValue for inlined OpenMP construct"); |
| 282 | } |
| 283 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 284 | /// Get the name of the capture helper. |
| Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 285 | StringRef getHelperName() const override { |
| Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 286 | if (auto *OuterRegionInfo = getOldCSI()) |
| 287 | return OuterRegionInfo->getHelperName(); |
| Alexey Bataev | 8cbe0a6 | 2015-02-26 10:27:34 +0000 | [diff] [blame] | 288 | llvm_unreachable("No helper name for inlined OpenMP construct"); |
| 289 | } |
| 290 | |
| Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 291 | void emitUntiedSwitch(CodeGenFunction &CGF) override { |
| 292 | if (OuterRegionInfo) |
| 293 | OuterRegionInfo->emitUntiedSwitch(CGF); |
| 294 | } |
| 295 | |
| Alexey Bataev | 8cbe0a6 | 2015-02-26 10:27:34 +0000 | [diff] [blame] | 296 | CodeGenFunction::CGCapturedStmtInfo *getOldCSI() const { return OldCSI; } |
| 297 | |
| Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 298 | static bool classof(const CGCapturedStmtInfo *Info) { |
| 299 | return CGOpenMPRegionInfo::classof(Info) && |
| 300 | cast<CGOpenMPRegionInfo>(Info)->getRegionKind() == InlinedRegion; |
| 301 | } |
| 302 | |
| Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 303 | ~CGOpenMPInlinedRegionInfo() override = default; |
| 304 | |
| Alexey Bataev | 8cbe0a6 | 2015-02-26 10:27:34 +0000 | [diff] [blame] | 305 | private: |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 306 | /// CodeGen info about outer OpenMP region. |
| Alexey Bataev | 8cbe0a6 | 2015-02-26 10:27:34 +0000 | [diff] [blame] | 307 | CodeGenFunction::CGCapturedStmtInfo *OldCSI; |
| 308 | CGOpenMPRegionInfo *OuterRegionInfo; |
| Alexey Bataev | 1809571 | 2014-10-10 12:19:54 +0000 | [diff] [blame] | 309 | }; |
| Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 310 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 311 | /// API for captured statement code generation in OpenMP target |
| Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 312 | /// constructs. For this captures, implicit parameters are used instead of the |
| Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 313 | /// captured fields. The name of the target region has to be unique in a given |
| 314 | /// application so it is provided by the client, because only the client has |
| 315 | /// the information to generate that. |
| Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 316 | class CGOpenMPTargetRegionInfo final : public CGOpenMPRegionInfo { |
| Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 317 | public: |
| 318 | CGOpenMPTargetRegionInfo(const CapturedStmt &CS, |
| Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 319 | const RegionCodeGenTy &CodeGen, StringRef HelperName) |
| Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 320 | : CGOpenMPRegionInfo(CS, TargetRegion, CodeGen, OMPD_target, |
| Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 321 | /*HasCancel=*/false), |
| 322 | HelperName(HelperName) {} |
| Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 323 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 324 | /// This is unused for target regions because each starts executing |
| Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 325 | /// with a single thread. |
| 326 | const VarDecl *getThreadIDVariable() const override { return nullptr; } |
| 327 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 328 | /// Get the name of the capture helper. |
| Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 329 | StringRef getHelperName() const override { return HelperName; } |
| Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 330 | |
| 331 | static bool classof(const CGCapturedStmtInfo *Info) { |
| 332 | return CGOpenMPRegionInfo::classof(Info) && |
| 333 | cast<CGOpenMPRegionInfo>(Info)->getRegionKind() == TargetRegion; |
| 334 | } |
| Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 335 | |
| 336 | private: |
| 337 | StringRef HelperName; |
| Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 338 | }; |
| 339 | |
| Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 340 | static void EmptyCodeGen(CodeGenFunction &, PrePostActionTy &) { |
| Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 341 | llvm_unreachable("No codegen for expressions"); |
| 342 | } |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 343 | /// API for generation of expressions captured in a innermost OpenMP |
| Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 344 | /// region. |
| Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 345 | class CGOpenMPInnerExprInfo final : public CGOpenMPInlinedRegionInfo { |
| Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 346 | public: |
| 347 | CGOpenMPInnerExprInfo(CodeGenFunction &CGF, const CapturedStmt &CS) |
| 348 | : CGOpenMPInlinedRegionInfo(CGF.CapturedStmtInfo, EmptyCodeGen, |
| 349 | OMPD_unknown, |
| 350 | /*HasCancel=*/false), |
| 351 | PrivScope(CGF) { |
| 352 | // Make sure the globals captured in the provided statement are local by |
| 353 | // using the privatization logic. We assume the same variable is not |
| 354 | // captured more than once. |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 355 | for (const auto &C : CS.captures()) { |
| Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 356 | if (!C.capturesVariable() && !C.capturesVariableByCopy()) |
| 357 | continue; |
| 358 | |
| 359 | const VarDecl *VD = C.getCapturedVar(); |
| 360 | if (VD->isLocalVarDeclOrParm()) |
| 361 | continue; |
| 362 | |
| Bruno Ricci | 5fc4db7 | 2018-12-21 14:10:18 +0000 | [diff] [blame] | 363 | DeclRefExpr DRE(CGF.getContext(), const_cast<VarDecl *>(VD), |
| Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 364 | /*RefersToEnclosingVariableOrCapture=*/false, |
| 365 | VD->getType().getNonReferenceType(), VK_LValue, |
| Alexey Bataev | a9b9cc0 | 2018-01-23 18:12:38 +0000 | [diff] [blame] | 366 | C.getLocation()); |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 367 | PrivScope.addPrivate( |
| Akira Hatanaka | f139ae3 | 2019-12-03 15:17:01 -0800 | [diff] [blame] | 368 | VD, [&CGF, &DRE]() { return CGF.EmitLValue(&DRE).getAddress(CGF); }); |
| Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 369 | } |
| 370 | (void)PrivScope.Privatize(); |
| 371 | } |
| 372 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 373 | /// Lookup the captured field decl for a variable. |
| Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 374 | const FieldDecl *lookup(const VarDecl *VD) const override { |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 375 | if (const FieldDecl *FD = CGOpenMPInlinedRegionInfo::lookup(VD)) |
| Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 376 | return FD; |
| 377 | return nullptr; |
| 378 | } |
| 379 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 380 | /// Emit the captured statement body. |
| Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 381 | void EmitBody(CodeGenFunction &CGF, const Stmt *S) override { |
| 382 | llvm_unreachable("No body for expressions"); |
| 383 | } |
| 384 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 385 | /// Get a variable or parameter for storing global thread id |
| Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 386 | /// inside OpenMP construct. |
| 387 | const VarDecl *getThreadIDVariable() const override { |
| 388 | llvm_unreachable("No thread id for expressions"); |
| 389 | } |
| 390 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 391 | /// Get the name of the capture helper. |
| Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 392 | StringRef getHelperName() const override { |
| 393 | llvm_unreachable("No helper name for expressions"); |
| 394 | } |
| 395 | |
| 396 | static bool classof(const CGCapturedStmtInfo *Info) { return false; } |
| 397 | |
| 398 | private: |
| 399 | /// Private scope to capture global variables. |
| 400 | CodeGenFunction::OMPPrivateScope PrivScope; |
| 401 | }; |
| 402 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 403 | /// RAII for emitting code of OpenMP constructs. |
| Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 404 | class InlinedOpenMPRegionRAII { |
| 405 | CodeGenFunction &CGF; |
| Alexey Bataev | 4ba78a4 | 2016-04-27 07:56:03 +0000 | [diff] [blame] | 406 | llvm::DenseMap<const VarDecl *, FieldDecl *> LambdaCaptureFields; |
| 407 | FieldDecl *LambdaThisCaptureField = nullptr; |
| Alexey Bataev | a8a9153a | 2017-12-29 18:07:07 +0000 | [diff] [blame] | 408 | const CodeGen::CGBlockInfo *BlockInfo = nullptr; |
| Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 409 | |
| 410 | public: |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 411 | /// Constructs region for combined constructs. |
| Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 412 | /// \param CodeGen Code generation sequence for combined directives. Includes |
| 413 | /// a list of functions used for code generation of implicitly inlined |
| 414 | /// regions. |
| Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 415 | InlinedOpenMPRegionRAII(CodeGenFunction &CGF, const RegionCodeGenTy &CodeGen, |
| Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 416 | OpenMPDirectiveKind Kind, bool HasCancel) |
| Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 417 | : CGF(CGF) { |
| 418 | // Start emission for the construct. |
| Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 419 | CGF.CapturedStmtInfo = new CGOpenMPInlinedRegionInfo( |
| 420 | CGF.CapturedStmtInfo, CodeGen, Kind, HasCancel); |
| Alexey Bataev | 4ba78a4 | 2016-04-27 07:56:03 +0000 | [diff] [blame] | 421 | std::swap(CGF.LambdaCaptureFields, LambdaCaptureFields); |
| 422 | LambdaThisCaptureField = CGF.LambdaThisCaptureField; |
| 423 | CGF.LambdaThisCaptureField = nullptr; |
| Alexey Bataev | a8a9153a | 2017-12-29 18:07:07 +0000 | [diff] [blame] | 424 | BlockInfo = CGF.BlockInfo; |
| 425 | CGF.BlockInfo = nullptr; |
| Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 426 | } |
| Eugene Zelenko | 0a4f3f4 | 2016-02-10 19:11:58 +0000 | [diff] [blame] | 427 | |
| Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 428 | ~InlinedOpenMPRegionRAII() { |
| 429 | // Restore original CapturedStmtInfo only if we're done with code emission. |
| 430 | auto *OldCSI = |
| 431 | cast<CGOpenMPInlinedRegionInfo>(CGF.CapturedStmtInfo)->getOldCSI(); |
| 432 | delete CGF.CapturedStmtInfo; |
| 433 | CGF.CapturedStmtInfo = OldCSI; |
| Alexey Bataev | 4ba78a4 | 2016-04-27 07:56:03 +0000 | [diff] [blame] | 434 | std::swap(CGF.LambdaCaptureFields, LambdaCaptureFields); |
| 435 | CGF.LambdaThisCaptureField = LambdaThisCaptureField; |
| Alexey Bataev | a8a9153a | 2017-12-29 18:07:07 +0000 | [diff] [blame] | 436 | CGF.BlockInfo = BlockInfo; |
| Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 437 | } |
| 438 | }; |
| 439 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 440 | /// Values for bit flags used in the ident_t to describe the fields. |
| Alexey Bataev | 50b3c95 | 2016-02-19 10:38:26 +0000 | [diff] [blame] | 441 | /// All enumeric elements are named and described in accordance with the code |
| James Y Knight | 5d71fc5 | 2019-01-29 16:37:27 +0000 | [diff] [blame] | 442 | /// from https://github.com/llvm/llvm-project/blob/master/openmp/runtime/src/kmp.h |
| Alexey Bataev | 0f87dbe | 2017-08-14 17:56:13 +0000 | [diff] [blame] | 443 | enum OpenMPLocationFlags : unsigned { |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 444 | /// Use trampoline for internal microtask. |
| Alexey Bataev | 50b3c95 | 2016-02-19 10:38:26 +0000 | [diff] [blame] | 445 | OMP_IDENT_IMD = 0x01, |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 446 | /// Use c-style ident structure. |
| Alexey Bataev | 50b3c95 | 2016-02-19 10:38:26 +0000 | [diff] [blame] | 447 | OMP_IDENT_KMPC = 0x02, |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 448 | /// Atomic reduction option for kmpc_reduce. |
| Alexey Bataev | 50b3c95 | 2016-02-19 10:38:26 +0000 | [diff] [blame] | 449 | OMP_ATOMIC_REDUCE = 0x10, |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 450 | /// Explicit 'barrier' directive. |
| Alexey Bataev | 50b3c95 | 2016-02-19 10:38:26 +0000 | [diff] [blame] | 451 | OMP_IDENT_BARRIER_EXPL = 0x20, |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 452 | /// Implicit barrier in code. |
| Alexey Bataev | 50b3c95 | 2016-02-19 10:38:26 +0000 | [diff] [blame] | 453 | OMP_IDENT_BARRIER_IMPL = 0x40, |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 454 | /// Implicit barrier in 'for' directive. |
| Alexey Bataev | 50b3c95 | 2016-02-19 10:38:26 +0000 | [diff] [blame] | 455 | OMP_IDENT_BARRIER_IMPL_FOR = 0x40, |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 456 | /// Implicit barrier in 'sections' directive. |
| Alexey Bataev | 50b3c95 | 2016-02-19 10:38:26 +0000 | [diff] [blame] | 457 | OMP_IDENT_BARRIER_IMPL_SECTIONS = 0xC0, |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 458 | /// Implicit barrier in 'single' directive. |
| Alexey Bataev | 0f87dbe | 2017-08-14 17:56:13 +0000 | [diff] [blame] | 459 | OMP_IDENT_BARRIER_IMPL_SINGLE = 0x140, |
| 460 | /// Call of __kmp_for_static_init for static loop. |
| 461 | OMP_IDENT_WORK_LOOP = 0x200, |
| 462 | /// Call of __kmp_for_static_init for sections. |
| 463 | OMP_IDENT_WORK_SECTIONS = 0x400, |
| 464 | /// Call of __kmp_for_static_init for distribute. |
| 465 | OMP_IDENT_WORK_DISTRIBUTE = 0x800, |
| 466 | LLVM_MARK_AS_BITMASK_ENUM(/*LargestValue=*/OMP_IDENT_WORK_DISTRIBUTE) |
| Alexey Bataev | 50b3c95 | 2016-02-19 10:38:26 +0000 | [diff] [blame] | 467 | }; |
| 468 | |
| Gheorghe-Teodor Bercea | 66cdbb47 | 2019-05-21 19:42:01 +0000 | [diff] [blame] | 469 | namespace { |
| 470 | LLVM_ENABLE_BITMASK_ENUMS_IN_NAMESPACE(); |
| 471 | /// Values for bit flags for marking which requires clauses have been used. |
| 472 | enum OpenMPOffloadingRequiresDirFlags : int64_t { |
| 473 | /// flag undefined. |
| 474 | OMP_REQ_UNDEFINED = 0x000, |
| 475 | /// no requires clause present. |
| 476 | OMP_REQ_NONE = 0x001, |
| 477 | /// reverse_offload clause. |
| 478 | OMP_REQ_REVERSE_OFFLOAD = 0x002, |
| 479 | /// unified_address clause. |
| 480 | OMP_REQ_UNIFIED_ADDRESS = 0x004, |
| 481 | /// unified_shared_memory clause. |
| 482 | OMP_REQ_UNIFIED_SHARED_MEMORY = 0x008, |
| 483 | /// dynamic_allocators clause. |
| 484 | OMP_REQ_DYNAMIC_ALLOCATORS = 0x010, |
| 485 | LLVM_MARK_AS_BITMASK_ENUM(/*LargestValue=*/OMP_REQ_DYNAMIC_ALLOCATORS) |
| 486 | }; |
| Gheorghe-Teodor Bercea | 545a9fe | 2019-06-14 20:19:54 +0000 | [diff] [blame] | 487 | |
| 488 | enum OpenMPOffloadingReservedDeviceIDs { |
| 489 | /// Device ID if the device was not defined, runtime should get it |
| 490 | /// from environment variables in the spec. |
| 491 | OMP_DEVICEID_UNDEF = -1, |
| 492 | }; |
| Gheorghe-Teodor Bercea | 66cdbb47 | 2019-05-21 19:42:01 +0000 | [diff] [blame] | 493 | } // anonymous namespace |
| 494 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 495 | /// Describes ident structure that describes a source location. |
| Alexey Bataev | 50b3c95 | 2016-02-19 10:38:26 +0000 | [diff] [blame] | 496 | /// All descriptions are taken from |
| James Y Knight | 5d71fc5 | 2019-01-29 16:37:27 +0000 | [diff] [blame] | 497 | /// https://github.com/llvm/llvm-project/blob/master/openmp/runtime/src/kmp.h |
| Alexey Bataev | 50b3c95 | 2016-02-19 10:38:26 +0000 | [diff] [blame] | 498 | /// Original structure: |
| 499 | /// typedef struct ident { |
| 500 | /// kmp_int32 reserved_1; /**< might be used in Fortran; |
| 501 | /// see above */ |
| 502 | /// kmp_int32 flags; /**< also f.flags; KMP_IDENT_xxx flags; |
| 503 | /// KMP_IDENT_KMPC identifies this union |
| 504 | /// member */ |
| 505 | /// kmp_int32 reserved_2; /**< not really used in Fortran any more; |
| 506 | /// see above */ |
| 507 | ///#if USE_ITT_BUILD |
| 508 | /// /* but currently used for storing |
| 509 | /// region-specific ITT */ |
| 510 | /// /* contextual information. */ |
| 511 | ///#endif /* USE_ITT_BUILD */ |
| 512 | /// kmp_int32 reserved_3; /**< source[4] in Fortran, do not use for |
| 513 | /// C++ */ |
| 514 | /// char const *psource; /**< String describing the source location. |
| 515 | /// The string is composed of semi-colon separated |
| 516 | // fields which describe the source file, |
| 517 | /// the function and a pair of line numbers that |
| 518 | /// delimit the construct. |
| 519 | /// */ |
| 520 | /// } ident_t; |
| 521 | enum IdentFieldIndex { |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 522 | /// might be used in Fortran |
| Alexey Bataev | 50b3c95 | 2016-02-19 10:38:26 +0000 | [diff] [blame] | 523 | IdentField_Reserved_1, |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 524 | /// OMP_IDENT_xxx flags; OMP_IDENT_KMPC identifies this union member. |
| Alexey Bataev | 50b3c95 | 2016-02-19 10:38:26 +0000 | [diff] [blame] | 525 | IdentField_Flags, |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 526 | /// Not really used in Fortran any more |
| Alexey Bataev | 50b3c95 | 2016-02-19 10:38:26 +0000 | [diff] [blame] | 527 | IdentField_Reserved_2, |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 528 | /// Source[4] in Fortran, do not use for C++ |
| Alexey Bataev | 50b3c95 | 2016-02-19 10:38:26 +0000 | [diff] [blame] | 529 | IdentField_Reserved_3, |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 530 | /// String describing the source location. The string is composed of |
| Alexey Bataev | 50b3c95 | 2016-02-19 10:38:26 +0000 | [diff] [blame] | 531 | /// semi-colon separated fields which describe the source file, the function |
| 532 | /// and a pair of line numbers that delimit the construct. |
| 533 | IdentField_PSource |
| 534 | }; |
| 535 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 536 | /// Schedule types for 'omp for' loops (these enumerators are taken from |
| Alexey Bataev | 50b3c95 | 2016-02-19 10:38:26 +0000 | [diff] [blame] | 537 | /// the enum sched_type in kmp.h). |
| 538 | enum OpenMPSchedType { |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 539 | /// Lower bound for default (unordered) versions. |
| Alexey Bataev | 50b3c95 | 2016-02-19 10:38:26 +0000 | [diff] [blame] | 540 | OMP_sch_lower = 32, |
| 541 | OMP_sch_static_chunked = 33, |
| 542 | OMP_sch_static = 34, |
| 543 | OMP_sch_dynamic_chunked = 35, |
| 544 | OMP_sch_guided_chunked = 36, |
| 545 | OMP_sch_runtime = 37, |
| 546 | OMP_sch_auto = 38, |
| Alexey Bataev | 6cff624 | 2016-05-30 13:05:14 +0000 | [diff] [blame] | 547 | /// static with chunk adjustment (e.g., simd) |
| Samuel Antao | 4c8035b | 2016-12-12 18:00:20 +0000 | [diff] [blame] | 548 | OMP_sch_static_balanced_chunked = 45, |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 549 | /// Lower bound for 'ordered' versions. |
| Alexey Bataev | 50b3c95 | 2016-02-19 10:38:26 +0000 | [diff] [blame] | 550 | OMP_ord_lower = 64, |
| 551 | OMP_ord_static_chunked = 65, |
| 552 | OMP_ord_static = 66, |
| 553 | OMP_ord_dynamic_chunked = 67, |
| 554 | OMP_ord_guided_chunked = 68, |
| 555 | OMP_ord_runtime = 69, |
| 556 | OMP_ord_auto = 70, |
| 557 | OMP_sch_default = OMP_sch_static, |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 558 | /// dist_schedule types |
| Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 559 | OMP_dist_sch_static_chunked = 91, |
| 560 | OMP_dist_sch_static = 92, |
| Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 561 | /// Support for OpenMP 4.5 monotonic and nonmonotonic schedule modifiers. |
| 562 | /// Set if the monotonic schedule modifier was present. |
| 563 | OMP_sch_modifier_monotonic = (1 << 29), |
| 564 | /// Set if the nonmonotonic schedule modifier was present. |
| 565 | OMP_sch_modifier_nonmonotonic = (1 << 30), |
| Alexey Bataev | 50b3c95 | 2016-02-19 10:38:26 +0000 | [diff] [blame] | 566 | }; |
| 567 | |
| 568 | enum OpenMPRTLFunction { |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 569 | /// Call to void __kmpc_fork_call(ident_t *loc, kmp_int32 argc, |
| Alexey Bataev | 50b3c95 | 2016-02-19 10:38:26 +0000 | [diff] [blame] | 570 | /// kmpc_micro microtask, ...); |
| 571 | OMPRTL__kmpc_fork_call, |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 572 | /// Call to void *__kmpc_threadprivate_cached(ident_t *loc, |
| Alexey Bataev | 50b3c95 | 2016-02-19 10:38:26 +0000 | [diff] [blame] | 573 | /// kmp_int32 global_tid, void *data, size_t size, void ***cache); |
| 574 | OMPRTL__kmpc_threadprivate_cached, |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 575 | /// Call to void __kmpc_threadprivate_register( ident_t *, |
| Alexey Bataev | 50b3c95 | 2016-02-19 10:38:26 +0000 | [diff] [blame] | 576 | /// void *data, kmpc_ctor ctor, kmpc_cctor cctor, kmpc_dtor dtor); |
| 577 | OMPRTL__kmpc_threadprivate_register, |
| 578 | // Call to __kmpc_int32 kmpc_global_thread_num(ident_t *loc); |
| 579 | OMPRTL__kmpc_global_thread_num, |
| 580 | // Call to void __kmpc_critical(ident_t *loc, kmp_int32 global_tid, |
| 581 | // kmp_critical_name *crit); |
| 582 | OMPRTL__kmpc_critical, |
| 583 | // Call to void __kmpc_critical_with_hint(ident_t *loc, kmp_int32 |
| 584 | // global_tid, kmp_critical_name *crit, uintptr_t hint); |
| 585 | OMPRTL__kmpc_critical_with_hint, |
| 586 | // Call to void __kmpc_end_critical(ident_t *loc, kmp_int32 global_tid, |
| 587 | // kmp_critical_name *crit); |
| 588 | OMPRTL__kmpc_end_critical, |
| 589 | // Call to kmp_int32 __kmpc_cancel_barrier(ident_t *loc, kmp_int32 |
| 590 | // global_tid); |
| 591 | OMPRTL__kmpc_cancel_barrier, |
| 592 | // Call to void __kmpc_barrier(ident_t *loc, kmp_int32 global_tid); |
| 593 | OMPRTL__kmpc_barrier, |
| 594 | // Call to void __kmpc_for_static_fini(ident_t *loc, kmp_int32 global_tid); |
| 595 | OMPRTL__kmpc_for_static_fini, |
| 596 | // Call to void __kmpc_serialized_parallel(ident_t *loc, kmp_int32 |
| 597 | // global_tid); |
| 598 | OMPRTL__kmpc_serialized_parallel, |
| 599 | // Call to void __kmpc_end_serialized_parallel(ident_t *loc, kmp_int32 |
| 600 | // global_tid); |
| 601 | OMPRTL__kmpc_end_serialized_parallel, |
| 602 | // Call to void __kmpc_push_num_threads(ident_t *loc, kmp_int32 global_tid, |
| 603 | // kmp_int32 num_threads); |
| 604 | OMPRTL__kmpc_push_num_threads, |
| 605 | // Call to void __kmpc_flush(ident_t *loc); |
| 606 | OMPRTL__kmpc_flush, |
| 607 | // Call to kmp_int32 __kmpc_master(ident_t *, kmp_int32 global_tid); |
| 608 | OMPRTL__kmpc_master, |
| 609 | // Call to void __kmpc_end_master(ident_t *, kmp_int32 global_tid); |
| 610 | OMPRTL__kmpc_end_master, |
| 611 | // Call to kmp_int32 __kmpc_omp_taskyield(ident_t *, kmp_int32 global_tid, |
| 612 | // int end_part); |
| 613 | OMPRTL__kmpc_omp_taskyield, |
| 614 | // Call to kmp_int32 __kmpc_single(ident_t *, kmp_int32 global_tid); |
| 615 | OMPRTL__kmpc_single, |
| 616 | // Call to void __kmpc_end_single(ident_t *, kmp_int32 global_tid); |
| 617 | OMPRTL__kmpc_end_single, |
| 618 | // Call to kmp_task_t * __kmpc_omp_task_alloc(ident_t *, kmp_int32 gtid, |
| 619 | // kmp_int32 flags, size_t sizeof_kmp_task_t, size_t sizeof_shareds, |
| 620 | // kmp_routine_entry_t *task_entry); |
| 621 | OMPRTL__kmpc_omp_task_alloc, |
| Gheorghe-Teodor Bercea | 545a9fe | 2019-06-14 20:19:54 +0000 | [diff] [blame] | 622 | // Call to kmp_task_t * __kmpc_omp_target_task_alloc(ident_t *, |
| 623 | // kmp_int32 gtid, kmp_int32 flags, size_t sizeof_kmp_task_t, |
| 624 | // size_t sizeof_shareds, kmp_routine_entry_t *task_entry, |
| 625 | // kmp_int64 device_id); |
| 626 | OMPRTL__kmpc_omp_target_task_alloc, |
| Alexey Bataev | 50b3c95 | 2016-02-19 10:38:26 +0000 | [diff] [blame] | 627 | // Call to kmp_int32 __kmpc_omp_task(ident_t *, kmp_int32 gtid, kmp_task_t * |
| 628 | // new_task); |
| 629 | OMPRTL__kmpc_omp_task, |
| 630 | // Call to void __kmpc_copyprivate(ident_t *loc, kmp_int32 global_tid, |
| 631 | // size_t cpy_size, void *cpy_data, void(*cpy_func)(void *, void *), |
| 632 | // kmp_int32 didit); |
| 633 | OMPRTL__kmpc_copyprivate, |
| 634 | // Call to kmp_int32 __kmpc_reduce(ident_t *loc, kmp_int32 global_tid, |
| 635 | // kmp_int32 num_vars, size_t reduce_size, void *reduce_data, void |
| 636 | // (*reduce_func)(void *lhs_data, void *rhs_data), kmp_critical_name *lck); |
| 637 | OMPRTL__kmpc_reduce, |
| 638 | // Call to kmp_int32 __kmpc_reduce_nowait(ident_t *loc, kmp_int32 |
| 639 | // global_tid, kmp_int32 num_vars, size_t reduce_size, void *reduce_data, |
| 640 | // void (*reduce_func)(void *lhs_data, void *rhs_data), kmp_critical_name |
| 641 | // *lck); |
| 642 | OMPRTL__kmpc_reduce_nowait, |
| 643 | // Call to void __kmpc_end_reduce(ident_t *loc, kmp_int32 global_tid, |
| 644 | // kmp_critical_name *lck); |
| 645 | OMPRTL__kmpc_end_reduce, |
| 646 | // Call to void __kmpc_end_reduce_nowait(ident_t *loc, kmp_int32 global_tid, |
| 647 | // kmp_critical_name *lck); |
| 648 | OMPRTL__kmpc_end_reduce_nowait, |
| 649 | // Call to void __kmpc_omp_task_begin_if0(ident_t *, kmp_int32 gtid, |
| 650 | // kmp_task_t * new_task); |
| 651 | OMPRTL__kmpc_omp_task_begin_if0, |
| 652 | // Call to void __kmpc_omp_task_complete_if0(ident_t *, kmp_int32 gtid, |
| 653 | // kmp_task_t * new_task); |
| 654 | OMPRTL__kmpc_omp_task_complete_if0, |
| 655 | // Call to void __kmpc_ordered(ident_t *loc, kmp_int32 global_tid); |
| 656 | OMPRTL__kmpc_ordered, |
| 657 | // Call to void __kmpc_end_ordered(ident_t *loc, kmp_int32 global_tid); |
| 658 | OMPRTL__kmpc_end_ordered, |
| 659 | // Call to kmp_int32 __kmpc_omp_taskwait(ident_t *loc, kmp_int32 |
| 660 | // global_tid); |
| 661 | OMPRTL__kmpc_omp_taskwait, |
| 662 | // Call to void __kmpc_taskgroup(ident_t *loc, kmp_int32 global_tid); |
| 663 | OMPRTL__kmpc_taskgroup, |
| 664 | // Call to void __kmpc_end_taskgroup(ident_t *loc, kmp_int32 global_tid); |
| 665 | OMPRTL__kmpc_end_taskgroup, |
| 666 | // Call to void __kmpc_push_proc_bind(ident_t *loc, kmp_int32 global_tid, |
| 667 | // int proc_bind); |
| 668 | OMPRTL__kmpc_push_proc_bind, |
| 669 | // Call to kmp_int32 __kmpc_omp_task_with_deps(ident_t *loc_ref, kmp_int32 |
| 670 | // gtid, kmp_task_t * new_task, kmp_int32 ndeps, kmp_depend_info_t |
| 671 | // *dep_list, kmp_int32 ndeps_noalias, kmp_depend_info_t *noalias_dep_list); |
| 672 | OMPRTL__kmpc_omp_task_with_deps, |
| 673 | // Call to void __kmpc_omp_wait_deps(ident_t *loc_ref, kmp_int32 |
| 674 | // gtid, kmp_int32 ndeps, kmp_depend_info_t *dep_list, kmp_int32 |
| 675 | // ndeps_noalias, kmp_depend_info_t *noalias_dep_list); |
| 676 | OMPRTL__kmpc_omp_wait_deps, |
| 677 | // Call to kmp_int32 __kmpc_cancellationpoint(ident_t *loc, kmp_int32 |
| 678 | // global_tid, kmp_int32 cncl_kind); |
| 679 | OMPRTL__kmpc_cancellationpoint, |
| 680 | // Call to kmp_int32 __kmpc_cancel(ident_t *loc, kmp_int32 global_tid, |
| 681 | // kmp_int32 cncl_kind); |
| 682 | OMPRTL__kmpc_cancel, |
| Carlo Bertolli | 430d8ec | 2016-03-03 20:34:23 +0000 | [diff] [blame] | 683 | // Call to void __kmpc_push_num_teams(ident_t *loc, kmp_int32 global_tid, |
| 684 | // kmp_int32 num_teams, kmp_int32 thread_limit); |
| 685 | OMPRTL__kmpc_push_num_teams, |
| Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 686 | // Call to void __kmpc_fork_teams(ident_t *loc, kmp_int32 argc, kmpc_micro |
| 687 | // microtask, ...); |
| Carlo Bertolli | 430d8ec | 2016-03-03 20:34:23 +0000 | [diff] [blame] | 688 | OMPRTL__kmpc_fork_teams, |
| Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 689 | // Call to void __kmpc_taskloop(ident_t *loc, int gtid, kmp_task_t *task, int |
| 690 | // if_val, kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st, int nogroup, int |
| 691 | // sched, kmp_uint64 grainsize, void *task_dup); |
| 692 | OMPRTL__kmpc_taskloop, |
| Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 693 | // Call to void __kmpc_doacross_init(ident_t *loc, kmp_int32 gtid, kmp_int32 |
| 694 | // num_dims, struct kmp_dim *dims); |
| 695 | OMPRTL__kmpc_doacross_init, |
| 696 | // Call to void __kmpc_doacross_fini(ident_t *loc, kmp_int32 gtid); |
| 697 | OMPRTL__kmpc_doacross_fini, |
| 698 | // Call to void __kmpc_doacross_post(ident_t *loc, kmp_int32 gtid, kmp_int64 |
| 699 | // *vec); |
| 700 | OMPRTL__kmpc_doacross_post, |
| 701 | // Call to void __kmpc_doacross_wait(ident_t *loc, kmp_int32 gtid, kmp_int64 |
| 702 | // *vec); |
| 703 | OMPRTL__kmpc_doacross_wait, |
| Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 704 | // Call to void *__kmpc_task_reduction_init(int gtid, int num_data, void |
| 705 | // *data); |
| 706 | OMPRTL__kmpc_task_reduction_init, |
| 707 | // Call to void *__kmpc_task_reduction_get_th_data(int gtid, void *tg, void |
| 708 | // *d); |
| 709 | OMPRTL__kmpc_task_reduction_get_th_data, |
| Alexey Bataev | 6cf7b71 | 2019-04-08 19:06:42 +0000 | [diff] [blame] | 710 | // Call to void *__kmpc_alloc(int gtid, size_t sz, omp_allocator_handle_t al); |
| Alexey Bataev | 4f680db | 2019-03-19 16:41:16 +0000 | [diff] [blame] | 711 | OMPRTL__kmpc_alloc, |
| Alexey Bataev | 6cf7b71 | 2019-04-08 19:06:42 +0000 | [diff] [blame] | 712 | // Call to void __kmpc_free(int gtid, void *ptr, omp_allocator_handle_t al); |
| Alexey Bataev | 4f680db | 2019-03-19 16:41:16 +0000 | [diff] [blame] | 713 | OMPRTL__kmpc_free, |
| Alexey Bataev | 50b3c95 | 2016-02-19 10:38:26 +0000 | [diff] [blame] | 714 | |
| 715 | // |
| 716 | // Offloading related calls |
| 717 | // |
| Alexey Bataev | 7bb3353 | 2019-01-07 21:30:43 +0000 | [diff] [blame] | 718 | // Call to void __kmpc_push_target_tripcount(int64_t device_id, kmp_uint64 |
| 719 | // size); |
| 720 | OMPRTL__kmpc_push_target_tripcount, |
| George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 721 | // Call to int32_t __tgt_target(int64_t device_id, void *host_ptr, int32_t |
| Alexey Bataev | a90fc66 | 2019-06-25 16:00:43 +0000 | [diff] [blame] | 722 | // arg_num, void** args_base, void **args, int64_t *arg_sizes, int64_t |
| Alexey Bataev | 50b3c95 | 2016-02-19 10:38:26 +0000 | [diff] [blame] | 723 | // *arg_types); |
| 724 | OMPRTL__tgt_target, |
| Alexey Bataev | a9f77c6 | 2017-12-13 21:04:20 +0000 | [diff] [blame] | 725 | // Call to int32_t __tgt_target_nowait(int64_t device_id, void *host_ptr, |
| Alexey Bataev | a90fc66 | 2019-06-25 16:00:43 +0000 | [diff] [blame] | 726 | // int32_t arg_num, void** args_base, void **args, int64_t *arg_sizes, int64_t |
| Alexey Bataev | a9f77c6 | 2017-12-13 21:04:20 +0000 | [diff] [blame] | 727 | // *arg_types); |
| 728 | OMPRTL__tgt_target_nowait, |
| George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 729 | // Call to int32_t __tgt_target_teams(int64_t device_id, void *host_ptr, |
| Alexey Bataev | a90fc66 | 2019-06-25 16:00:43 +0000 | [diff] [blame] | 730 | // int32_t arg_num, void** args_base, void **args, int64_t *arg_sizes, int64_t |
| George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 731 | // *arg_types, int32_t num_teams, int32_t thread_limit); |
| Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 732 | OMPRTL__tgt_target_teams, |
| Alexey Bataev | a9f77c6 | 2017-12-13 21:04:20 +0000 | [diff] [blame] | 733 | // Call to int32_t __tgt_target_teams_nowait(int64_t device_id, void |
| Alexey Bataev | a90fc66 | 2019-06-25 16:00:43 +0000 | [diff] [blame] | 734 | // *host_ptr, int32_t arg_num, void** args_base, void **args, int64_t |
| Alexey Bataev | a9f77c6 | 2017-12-13 21:04:20 +0000 | [diff] [blame] | 735 | // *arg_sizes, int64_t *arg_types, int32_t num_teams, int32_t thread_limit); |
| 736 | OMPRTL__tgt_target_teams_nowait, |
| Gheorghe-Teodor Bercea | 66cdbb47 | 2019-05-21 19:42:01 +0000 | [diff] [blame] | 737 | // Call to void __tgt_register_requires(int64_t flags); |
| 738 | OMPRTL__tgt_register_requires, |
| George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 739 | // Call to void __tgt_target_data_begin(int64_t device_id, int32_t arg_num, |
| Alexey Bataev | a90fc66 | 2019-06-25 16:00:43 +0000 | [diff] [blame] | 740 | // void** args_base, void **args, int64_t *arg_sizes, int64_t *arg_types); |
| Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 741 | OMPRTL__tgt_target_data_begin, |
| Alexey Bataev | 0cc6b8e | 2017-12-14 17:00:17 +0000 | [diff] [blame] | 742 | // Call to void __tgt_target_data_begin_nowait(int64_t device_id, int32_t |
| Alexey Bataev | a90fc66 | 2019-06-25 16:00:43 +0000 | [diff] [blame] | 743 | // arg_num, void** args_base, void **args, int64_t *arg_sizes, int64_t |
| Alexey Bataev | 0cc6b8e | 2017-12-14 17:00:17 +0000 | [diff] [blame] | 744 | // *arg_types); |
| 745 | OMPRTL__tgt_target_data_begin_nowait, |
| George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 746 | // Call to void __tgt_target_data_end(int64_t device_id, int32_t arg_num, |
| 747 | // void** args_base, void **args, size_t *arg_sizes, int64_t *arg_types); |
| Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 748 | OMPRTL__tgt_target_data_end, |
| Alexey Bataev | 0cc6b8e | 2017-12-14 17:00:17 +0000 | [diff] [blame] | 749 | // Call to void __tgt_target_data_end_nowait(int64_t device_id, int32_t |
| Alexey Bataev | a90fc66 | 2019-06-25 16:00:43 +0000 | [diff] [blame] | 750 | // arg_num, void** args_base, void **args, int64_t *arg_sizes, int64_t |
| Alexey Bataev | 0cc6b8e | 2017-12-14 17:00:17 +0000 | [diff] [blame] | 751 | // *arg_types); |
| 752 | OMPRTL__tgt_target_data_end_nowait, |
| George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 753 | // Call to void __tgt_target_data_update(int64_t device_id, int32_t arg_num, |
| Alexey Bataev | a90fc66 | 2019-06-25 16:00:43 +0000 | [diff] [blame] | 754 | // void** args_base, void **args, int64_t *arg_sizes, int64_t *arg_types); |
| Samuel Antao | 8d2d730 | 2016-05-26 18:30:22 +0000 | [diff] [blame] | 755 | OMPRTL__tgt_target_data_update, |
| Alexey Bataev | 0cc6b8e | 2017-12-14 17:00:17 +0000 | [diff] [blame] | 756 | // Call to void __tgt_target_data_update_nowait(int64_t device_id, int32_t |
| Alexey Bataev | a90fc66 | 2019-06-25 16:00:43 +0000 | [diff] [blame] | 757 | // arg_num, void** args_base, void **args, int64_t *arg_sizes, int64_t |
| Alexey Bataev | 0cc6b8e | 2017-12-14 17:00:17 +0000 | [diff] [blame] | 758 | // *arg_types); |
| 759 | OMPRTL__tgt_target_data_update_nowait, |
| Michael Kruse | d47b943 | 2019-08-05 18:43:21 +0000 | [diff] [blame] | 760 | // Call to int64_t __tgt_mapper_num_components(void *rt_mapper_handle); |
| 761 | OMPRTL__tgt_mapper_num_components, |
| 762 | // Call to void __tgt_push_mapper_component(void *rt_mapper_handle, void |
| 763 | // *base, void *begin, int64_t size, int64_t type); |
| 764 | OMPRTL__tgt_push_mapper_component, |
| Alexey Bataev | 50b3c95 | 2016-02-19 10:38:26 +0000 | [diff] [blame] | 765 | }; |
| 766 | |
| Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 767 | /// A basic class for pre|post-action for advanced codegen sequence for OpenMP |
| 768 | /// region. |
| 769 | class CleanupTy final : public EHScopeStack::Cleanup { |
| 770 | PrePostActionTy *Action; |
| 771 | |
| 772 | public: |
| 773 | explicit CleanupTy(PrePostActionTy *Action) : Action(Action) {} |
| 774 | void Emit(CodeGenFunction &CGF, Flags /*flags*/) override { |
| 775 | if (!CGF.HaveInsertPoint()) |
| 776 | return; |
| 777 | Action->Exit(CGF); |
| 778 | } |
| 779 | }; |
| 780 | |
| Hans Wennborg | 7eb5464 | 2015-09-10 17:07:54 +0000 | [diff] [blame] | 781 | } // anonymous namespace |
| Alexey Bataev | 1809571 | 2014-10-10 12:19:54 +0000 | [diff] [blame] | 782 | |
| Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 783 | void RegionCodeGenTy::operator()(CodeGenFunction &CGF) const { |
| 784 | CodeGenFunction::RunCleanupsScope Scope(CGF); |
| 785 | if (PrePostAction) { |
| 786 | CGF.EHStack.pushCleanup<CleanupTy>(NormalAndEHCleanup, PrePostAction); |
| 787 | Callback(CodeGen, CGF, *PrePostAction); |
| 788 | } else { |
| 789 | PrePostActionTy Action; |
| 790 | Callback(CodeGen, CGF, Action); |
| 791 | } |
| 792 | } |
| 793 | |
| Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 794 | /// Check if the combiner is a call to UDR combiner and if it is so return the |
| 795 | /// UDR decl used for reduction. |
| 796 | static const OMPDeclareReductionDecl * |
| 797 | getReductionInit(const Expr *ReductionOp) { |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 798 | if (const auto *CE = dyn_cast<CallExpr>(ReductionOp)) |
| 799 | if (const auto *OVE = dyn_cast<OpaqueValueExpr>(CE->getCallee())) |
| 800 | if (const auto *DRE = |
| Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 801 | dyn_cast<DeclRefExpr>(OVE->getSourceExpr()->IgnoreImpCasts())) |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 802 | if (const auto *DRD = dyn_cast<OMPDeclareReductionDecl>(DRE->getDecl())) |
| Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 803 | return DRD; |
| 804 | return nullptr; |
| 805 | } |
| 806 | |
| 807 | static void emitInitWithReductionInitializer(CodeGenFunction &CGF, |
| 808 | const OMPDeclareReductionDecl *DRD, |
| 809 | const Expr *InitOp, |
| 810 | Address Private, Address Original, |
| 811 | QualType Ty) { |
| 812 | if (DRD->getInitializer()) { |
| 813 | std::pair<llvm::Function *, llvm::Function *> Reduction = |
| 814 | CGF.CGM.getOpenMPRuntime().getUserDefinedReduction(DRD); |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 815 | const auto *CE = cast<CallExpr>(InitOp); |
| 816 | const auto *OVE = cast<OpaqueValueExpr>(CE->getCallee()); |
| Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 817 | const Expr *LHS = CE->getArg(/*Arg=*/0)->IgnoreParenImpCasts(); |
| 818 | const Expr *RHS = CE->getArg(/*Arg=*/1)->IgnoreParenImpCasts(); |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 819 | const auto *LHSDRE = |
| 820 | cast<DeclRefExpr>(cast<UnaryOperator>(LHS)->getSubExpr()); |
| 821 | const auto *RHSDRE = |
| 822 | cast<DeclRefExpr>(cast<UnaryOperator>(RHS)->getSubExpr()); |
| Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 823 | CodeGenFunction::OMPPrivateScope PrivateScope(CGF); |
| 824 | PrivateScope.addPrivate(cast<VarDecl>(LHSDRE->getDecl()), |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 825 | [=]() { return Private; }); |
| Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 826 | PrivateScope.addPrivate(cast<VarDecl>(RHSDRE->getDecl()), |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 827 | [=]() { return Original; }); |
| Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 828 | (void)PrivateScope.Privatize(); |
| 829 | RValue Func = RValue::get(Reduction.second); |
| 830 | CodeGenFunction::OpaqueValueMapping Map(CGF, OVE, Func); |
| 831 | CGF.EmitIgnoredExpr(InitOp); |
| 832 | } else { |
| 833 | llvm::Constant *Init = CGF.CGM.EmitNullConstant(Ty); |
| Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 834 | std::string Name = CGF.CGM.getOpenMPRuntime().getName({"init"}); |
| Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 835 | auto *GV = new llvm::GlobalVariable( |
| 836 | CGF.CGM.getModule(), Init->getType(), /*isConstant=*/true, |
| Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 837 | llvm::GlobalValue::PrivateLinkage, Init, Name); |
| Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 838 | LValue LV = CGF.MakeNaturalAlignAddrLValue(GV, Ty); |
| 839 | RValue InitRVal; |
| 840 | switch (CGF.getEvaluationKind(Ty)) { |
| 841 | case TEK_Scalar: |
| Alexey Bataev | a9b9cc0 | 2018-01-23 18:12:38 +0000 | [diff] [blame] | 842 | InitRVal = CGF.EmitLoadOfLValue(LV, DRD->getLocation()); |
| Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 843 | break; |
| 844 | case TEK_Complex: |
| 845 | InitRVal = |
| Alexey Bataev | a9b9cc0 | 2018-01-23 18:12:38 +0000 | [diff] [blame] | 846 | RValue::getComplex(CGF.EmitLoadOfComplex(LV, DRD->getLocation())); |
| Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 847 | break; |
| 848 | case TEK_Aggregate: |
| Akira Hatanaka | f139ae3 | 2019-12-03 15:17:01 -0800 | [diff] [blame] | 849 | InitRVal = RValue::getAggregate(LV.getAddress(CGF)); |
| Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 850 | break; |
| 851 | } |
| Alexey Bataev | a9b9cc0 | 2018-01-23 18:12:38 +0000 | [diff] [blame] | 852 | OpaqueValueExpr OVE(DRD->getLocation(), Ty, VK_RValue); |
| Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 853 | CodeGenFunction::OpaqueValueMapping OpaqueMap(CGF, &OVE, InitRVal); |
| 854 | CGF.EmitAnyExprToMem(&OVE, Private, Ty.getQualifiers(), |
| 855 | /*IsInitializer=*/false); |
| 856 | } |
| 857 | } |
| 858 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 859 | /// Emit initialization of arrays of complex types. |
| Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 860 | /// \param DestAddr Address of the array. |
| 861 | /// \param Type Type of array. |
| 862 | /// \param Init Initial expression of array. |
| 863 | /// \param SrcAddr Address of the original array. |
| 864 | static void EmitOMPAggregateInit(CodeGenFunction &CGF, Address DestAddr, |
| Alexey Bataev | a7b1915 | 2017-10-12 20:03:39 +0000 | [diff] [blame] | 865 | QualType Type, bool EmitDeclareReductionInit, |
| 866 | const Expr *Init, |
| Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 867 | const OMPDeclareReductionDecl *DRD, |
| Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 868 | Address SrcAddr = Address::invalid()) { |
| Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 869 | // Perform element-by-element initialization. |
| 870 | QualType ElementTy; |
| 871 | |
| 872 | // Drill down to the base element type on both arrays. |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 873 | const ArrayType *ArrayTy = Type->getAsArrayTypeUnsafe(); |
| 874 | llvm::Value *NumElements = CGF.emitArrayLength(ArrayTy, ElementTy, DestAddr); |
| Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 875 | DestAddr = |
| 876 | CGF.Builder.CreateElementBitCast(DestAddr, DestAddr.getElementType()); |
| 877 | if (DRD) |
| 878 | SrcAddr = |
| 879 | CGF.Builder.CreateElementBitCast(SrcAddr, DestAddr.getElementType()); |
| 880 | |
| 881 | llvm::Value *SrcBegin = nullptr; |
| 882 | if (DRD) |
| 883 | SrcBegin = SrcAddr.getPointer(); |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 884 | llvm::Value *DestBegin = DestAddr.getPointer(); |
| Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 885 | // Cast from pointer to array type to pointer to single element. |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 886 | llvm::Value *DestEnd = CGF.Builder.CreateGEP(DestBegin, NumElements); |
| Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 887 | // The basic structure here is a while-do loop. |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 888 | llvm::BasicBlock *BodyBB = CGF.createBasicBlock("omp.arrayinit.body"); |
| 889 | llvm::BasicBlock *DoneBB = CGF.createBasicBlock("omp.arrayinit.done"); |
| 890 | llvm::Value *IsEmpty = |
| Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 891 | CGF.Builder.CreateICmpEQ(DestBegin, DestEnd, "omp.arrayinit.isempty"); |
| 892 | CGF.Builder.CreateCondBr(IsEmpty, DoneBB, BodyBB); |
| 893 | |
| 894 | // Enter the loop body, making that address the current address. |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 895 | llvm::BasicBlock *EntryBB = CGF.Builder.GetInsertBlock(); |
| Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 896 | CGF.EmitBlock(BodyBB); |
| 897 | |
| 898 | CharUnits ElementSize = CGF.getContext().getTypeSizeInChars(ElementTy); |
| 899 | |
| 900 | llvm::PHINode *SrcElementPHI = nullptr; |
| 901 | Address SrcElementCurrent = Address::invalid(); |
| 902 | if (DRD) { |
| 903 | SrcElementPHI = CGF.Builder.CreatePHI(SrcBegin->getType(), 2, |
| 904 | "omp.arraycpy.srcElementPast"); |
| 905 | SrcElementPHI->addIncoming(SrcBegin, EntryBB); |
| 906 | SrcElementCurrent = |
| 907 | Address(SrcElementPHI, |
| 908 | SrcAddr.getAlignment().alignmentOfArrayElement(ElementSize)); |
| 909 | } |
| 910 | llvm::PHINode *DestElementPHI = CGF.Builder.CreatePHI( |
| 911 | DestBegin->getType(), 2, "omp.arraycpy.destElementPast"); |
| 912 | DestElementPHI->addIncoming(DestBegin, EntryBB); |
| 913 | Address DestElementCurrent = |
| 914 | Address(DestElementPHI, |
| 915 | DestAddr.getAlignment().alignmentOfArrayElement(ElementSize)); |
| 916 | |
| 917 | // Emit copy. |
| 918 | { |
| 919 | CodeGenFunction::RunCleanupsScope InitScope(CGF); |
| Alexey Bataev | a7b1915 | 2017-10-12 20:03:39 +0000 | [diff] [blame] | 920 | if (EmitDeclareReductionInit) { |
| Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 921 | emitInitWithReductionInitializer(CGF, DRD, Init, DestElementCurrent, |
| 922 | SrcElementCurrent, ElementTy); |
| 923 | } else |
| 924 | CGF.EmitAnyExprToMem(Init, DestElementCurrent, ElementTy.getQualifiers(), |
| 925 | /*IsInitializer=*/false); |
| 926 | } |
| 927 | |
| 928 | if (DRD) { |
| 929 | // Shift the address forward by one element. |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 930 | llvm::Value *SrcElementNext = CGF.Builder.CreateConstGEP1_32( |
| Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 931 | SrcElementPHI, /*Idx0=*/1, "omp.arraycpy.dest.element"); |
| 932 | SrcElementPHI->addIncoming(SrcElementNext, CGF.Builder.GetInsertBlock()); |
| 933 | } |
| 934 | |
| 935 | // Shift the address forward by one element. |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 936 | llvm::Value *DestElementNext = CGF.Builder.CreateConstGEP1_32( |
| Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 937 | DestElementPHI, /*Idx0=*/1, "omp.arraycpy.dest.element"); |
| 938 | // Check whether we've reached the end. |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 939 | llvm::Value *Done = |
| Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 940 | CGF.Builder.CreateICmpEQ(DestElementNext, DestEnd, "omp.arraycpy.done"); |
| 941 | CGF.Builder.CreateCondBr(Done, DoneBB, BodyBB); |
| 942 | DestElementPHI->addIncoming(DestElementNext, CGF.Builder.GetInsertBlock()); |
| 943 | |
| 944 | // Done. |
| 945 | CGF.EmitBlock(DoneBB, /*IsFinished=*/true); |
| 946 | } |
| 947 | |
| 948 | LValue ReductionCodeGen::emitSharedLValue(CodeGenFunction &CGF, const Expr *E) { |
| Alexey Bataev | f47c4b4 | 2017-09-26 13:47:31 +0000 | [diff] [blame] | 949 | return CGF.EmitOMPSharedLValue(E); |
| Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 950 | } |
| 951 | |
| 952 | LValue ReductionCodeGen::emitSharedLValueUB(CodeGenFunction &CGF, |
| 953 | const Expr *E) { |
| 954 | if (const auto *OASE = dyn_cast<OMPArraySectionExpr>(E)) |
| 955 | return CGF.EmitOMPArraySectionExpr(OASE, /*IsLowerBound=*/false); |
| 956 | return LValue(); |
| 957 | } |
| 958 | |
| Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 959 | void ReductionCodeGen::emitAggregateInitialization( |
| 960 | CodeGenFunction &CGF, unsigned N, Address PrivateAddr, LValue SharedLVal, |
| 961 | const OMPDeclareReductionDecl *DRD) { |
| Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 962 | // Emit VarDecl with copy init for arrays. |
| 963 | // Get the address of the original variable captured in current |
| 964 | // captured region. |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 965 | const auto *PrivateVD = |
| Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 966 | cast<VarDecl>(cast<DeclRefExpr>(ClausesData[N].Private)->getDecl()); |
| Alexey Bataev | a7b1915 | 2017-10-12 20:03:39 +0000 | [diff] [blame] | 967 | bool EmitDeclareReductionInit = |
| 968 | DRD && (DRD->getInitializer() || !PrivateVD->hasInit()); |
| Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 969 | EmitOMPAggregateInit(CGF, PrivateAddr, PrivateVD->getType(), |
| Alexey Bataev | a7b1915 | 2017-10-12 20:03:39 +0000 | [diff] [blame] | 970 | EmitDeclareReductionInit, |
| 971 | EmitDeclareReductionInit ? ClausesData[N].ReductionOp |
| 972 | : PrivateVD->getInit(), |
| Akira Hatanaka | f139ae3 | 2019-12-03 15:17:01 -0800 | [diff] [blame] | 973 | DRD, SharedLVal.getAddress(CGF)); |
| Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 974 | } |
| 975 | |
| 976 | ReductionCodeGen::ReductionCodeGen(ArrayRef<const Expr *> Shareds, |
| 977 | ArrayRef<const Expr *> Privates, |
| 978 | ArrayRef<const Expr *> ReductionOps) { |
| 979 | ClausesData.reserve(Shareds.size()); |
| 980 | SharedAddresses.reserve(Shareds.size()); |
| 981 | Sizes.reserve(Shareds.size()); |
| Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 982 | BaseDecls.reserve(Shareds.size()); |
| Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 983 | auto IPriv = Privates.begin(); |
| 984 | auto IRed = ReductionOps.begin(); |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 985 | for (const Expr *Ref : Shareds) { |
| Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 986 | ClausesData.emplace_back(Ref, *IPriv, *IRed); |
| 987 | std::advance(IPriv, 1); |
| 988 | std::advance(IRed, 1); |
| 989 | } |
| 990 | } |
| 991 | |
| 992 | void ReductionCodeGen::emitSharedLValue(CodeGenFunction &CGF, unsigned N) { |
| 993 | assert(SharedAddresses.size() == N && |
| 994 | "Number of generated lvalues must be exactly N."); |
| Jonas Hahnfeld | 4525c82 | 2017-10-23 19:01:35 +0000 | [diff] [blame] | 995 | LValue First = emitSharedLValue(CGF, ClausesData[N].Ref); |
| 996 | LValue Second = emitSharedLValueUB(CGF, ClausesData[N].Ref); |
| 997 | SharedAddresses.emplace_back(First, Second); |
| Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 998 | } |
| 999 | |
| 1000 | void ReductionCodeGen::emitAggregateType(CodeGenFunction &CGF, unsigned N) { |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1001 | const auto *PrivateVD = |
| Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1002 | cast<VarDecl>(cast<DeclRefExpr>(ClausesData[N].Private)->getDecl()); |
| 1003 | QualType PrivateType = PrivateVD->getType(); |
| 1004 | bool AsArraySection = isa<OMPArraySectionExpr>(ClausesData[N].Ref); |
| Jonas Hahnfeld | 4525c82 | 2017-10-23 19:01:35 +0000 | [diff] [blame] | 1005 | if (!PrivateType->isVariablyModifiedType()) { |
| Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 1006 | Sizes.emplace_back( |
| 1007 | CGF.getTypeSize( |
| 1008 | SharedAddresses[N].first.getType().getNonReferenceType()), |
| 1009 | nullptr); |
| Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1010 | return; |
| 1011 | } |
| 1012 | llvm::Value *Size; |
| Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 1013 | llvm::Value *SizeInChars; |
| Akira Hatanaka | f139ae3 | 2019-12-03 15:17:01 -0800 | [diff] [blame] | 1014 | auto *ElemType = cast<llvm::PointerType>( |
| 1015 | SharedAddresses[N].first.getPointer(CGF)->getType()) |
| 1016 | ->getElementType(); |
| Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 1017 | auto *ElemSizeOf = llvm::ConstantExpr::getSizeOf(ElemType); |
| Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1018 | if (AsArraySection) { |
| Akira Hatanaka | f139ae3 | 2019-12-03 15:17:01 -0800 | [diff] [blame] | 1019 | Size = CGF.Builder.CreatePtrDiff(SharedAddresses[N].second.getPointer(CGF), |
| 1020 | SharedAddresses[N].first.getPointer(CGF)); |
| Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1021 | Size = CGF.Builder.CreateNUWAdd( |
| 1022 | Size, llvm::ConstantInt::get(Size->getType(), /*V=*/1)); |
| Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 1023 | SizeInChars = CGF.Builder.CreateNUWMul(Size, ElemSizeOf); |
| Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1024 | } else { |
| Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 1025 | SizeInChars = CGF.getTypeSize( |
| Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1026 | SharedAddresses[N].first.getType().getNonReferenceType()); |
| Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 1027 | Size = CGF.Builder.CreateExactUDiv(SizeInChars, ElemSizeOf); |
| Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1028 | } |
| Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 1029 | Sizes.emplace_back(SizeInChars, Size); |
| Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1030 | CodeGenFunction::OpaqueValueMapping OpaqueMap( |
| 1031 | CGF, |
| 1032 | cast<OpaqueValueExpr>( |
| 1033 | CGF.getContext().getAsVariableArrayType(PrivateType)->getSizeExpr()), |
| 1034 | RValue::get(Size)); |
| 1035 | CGF.EmitVariablyModifiedType(PrivateType); |
| 1036 | } |
| 1037 | |
| 1038 | void ReductionCodeGen::emitAggregateType(CodeGenFunction &CGF, unsigned N, |
| 1039 | llvm::Value *Size) { |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1040 | const auto *PrivateVD = |
| Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1041 | cast<VarDecl>(cast<DeclRefExpr>(ClausesData[N].Private)->getDecl()); |
| 1042 | QualType PrivateType = PrivateVD->getType(); |
| Jonas Hahnfeld | 4525c82 | 2017-10-23 19:01:35 +0000 | [diff] [blame] | 1043 | if (!PrivateType->isVariablyModifiedType()) { |
| Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 1044 | assert(!Size && !Sizes[N].second && |
| Jonas Hahnfeld | 4525c82 | 2017-10-23 19:01:35 +0000 | [diff] [blame] | 1045 | "Size should be nullptr for non-variably modified reduction " |
| Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1046 | "items."); |
| 1047 | return; |
| 1048 | } |
| 1049 | CodeGenFunction::OpaqueValueMapping OpaqueMap( |
| 1050 | CGF, |
| 1051 | cast<OpaqueValueExpr>( |
| 1052 | CGF.getContext().getAsVariableArrayType(PrivateType)->getSizeExpr()), |
| 1053 | RValue::get(Size)); |
| 1054 | CGF.EmitVariablyModifiedType(PrivateType); |
| 1055 | } |
| 1056 | |
| 1057 | void ReductionCodeGen::emitInitialization( |
| 1058 | CodeGenFunction &CGF, unsigned N, Address PrivateAddr, LValue SharedLVal, |
| 1059 | llvm::function_ref<bool(CodeGenFunction &)> DefaultInit) { |
| 1060 | assert(SharedAddresses.size() > N && "No variable was generated"); |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1061 | const auto *PrivateVD = |
| Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1062 | cast<VarDecl>(cast<DeclRefExpr>(ClausesData[N].Private)->getDecl()); |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1063 | const OMPDeclareReductionDecl *DRD = |
| 1064 | getReductionInit(ClausesData[N].ReductionOp); |
| Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1065 | QualType PrivateType = PrivateVD->getType(); |
| 1066 | PrivateAddr = CGF.Builder.CreateElementBitCast( |
| 1067 | PrivateAddr, CGF.ConvertTypeForMem(PrivateType)); |
| 1068 | QualType SharedType = SharedAddresses[N].first.getType(); |
| 1069 | SharedLVal = CGF.MakeAddrLValue( |
| Akira Hatanaka | f139ae3 | 2019-12-03 15:17:01 -0800 | [diff] [blame] | 1070 | CGF.Builder.CreateElementBitCast(SharedLVal.getAddress(CGF), |
| Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1071 | CGF.ConvertTypeForMem(SharedType)), |
| Ivan A. Kosarev | f5f2046 | 2017-10-12 11:29:46 +0000 | [diff] [blame] | 1072 | SharedType, SharedAddresses[N].first.getBaseInfo(), |
| Ivan A. Kosarev | b9c59f3 | 2017-10-31 11:05:34 +0000 | [diff] [blame] | 1073 | CGF.CGM.getTBAAInfoForSubobject(SharedAddresses[N].first, SharedType)); |
| Jonas Hahnfeld | 4525c82 | 2017-10-23 19:01:35 +0000 | [diff] [blame] | 1074 | if (CGF.getContext().getAsArrayType(PrivateVD->getType())) { |
| Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 1075 | emitAggregateInitialization(CGF, N, PrivateAddr, SharedLVal, DRD); |
| Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1076 | } else if (DRD && (DRD->getInitializer() || !PrivateVD->hasInit())) { |
| 1077 | emitInitWithReductionInitializer(CGF, DRD, ClausesData[N].ReductionOp, |
| Akira Hatanaka | f139ae3 | 2019-12-03 15:17:01 -0800 | [diff] [blame] | 1078 | PrivateAddr, SharedLVal.getAddress(CGF), |
| Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1079 | SharedLVal.getType()); |
| 1080 | } else if (!DefaultInit(CGF) && PrivateVD->hasInit() && |
| 1081 | !CGF.isTrivialInitializer(PrivateVD->getInit())) { |
| 1082 | CGF.EmitAnyExprToMem(PrivateVD->getInit(), PrivateAddr, |
| 1083 | PrivateVD->getType().getQualifiers(), |
| 1084 | /*IsInitializer=*/false); |
| 1085 | } |
| 1086 | } |
| 1087 | |
| 1088 | bool ReductionCodeGen::needCleanups(unsigned N) { |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1089 | const auto *PrivateVD = |
| Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1090 | cast<VarDecl>(cast<DeclRefExpr>(ClausesData[N].Private)->getDecl()); |
| 1091 | QualType PrivateType = PrivateVD->getType(); |
| 1092 | QualType::DestructionKind DTorKind = PrivateType.isDestructedType(); |
| 1093 | return DTorKind != QualType::DK_none; |
| 1094 | } |
| 1095 | |
| 1096 | void ReductionCodeGen::emitCleanups(CodeGenFunction &CGF, unsigned N, |
| 1097 | Address PrivateAddr) { |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1098 | const auto *PrivateVD = |
| Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1099 | cast<VarDecl>(cast<DeclRefExpr>(ClausesData[N].Private)->getDecl()); |
| 1100 | QualType PrivateType = PrivateVD->getType(); |
| 1101 | QualType::DestructionKind DTorKind = PrivateType.isDestructedType(); |
| 1102 | if (needCleanups(N)) { |
| 1103 | PrivateAddr = CGF.Builder.CreateElementBitCast( |
| 1104 | PrivateAddr, CGF.ConvertTypeForMem(PrivateType)); |
| 1105 | CGF.pushDestroy(DTorKind, PrivateAddr, PrivateType); |
| 1106 | } |
| 1107 | } |
| 1108 | |
| 1109 | static LValue loadToBegin(CodeGenFunction &CGF, QualType BaseTy, QualType ElTy, |
| 1110 | LValue BaseLV) { |
| 1111 | BaseTy = BaseTy.getNonReferenceType(); |
| 1112 | while ((BaseTy->isPointerType() || BaseTy->isReferenceType()) && |
| 1113 | !CGF.getContext().hasSameType(BaseTy, ElTy)) { |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1114 | if (const auto *PtrTy = BaseTy->getAs<PointerType>()) { |
| Akira Hatanaka | f139ae3 | 2019-12-03 15:17:01 -0800 | [diff] [blame] | 1115 | BaseLV = CGF.EmitLoadOfPointerLValue(BaseLV.getAddress(CGF), PtrTy); |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1116 | } else { |
| Akira Hatanaka | f139ae3 | 2019-12-03 15:17:01 -0800 | [diff] [blame] | 1117 | LValue RefLVal = CGF.MakeAddrLValue(BaseLV.getAddress(CGF), BaseTy); |
| Ivan A. Kosarev | 9f9d157 | 2017-10-30 11:49:31 +0000 | [diff] [blame] | 1118 | BaseLV = CGF.EmitLoadOfReferenceLValue(RefLVal); |
| Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1119 | } |
| 1120 | BaseTy = BaseTy->getPointeeType(); |
| 1121 | } |
| 1122 | return CGF.MakeAddrLValue( |
| Akira Hatanaka | f139ae3 | 2019-12-03 15:17:01 -0800 | [diff] [blame] | 1123 | CGF.Builder.CreateElementBitCast(BaseLV.getAddress(CGF), |
| Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1124 | CGF.ConvertTypeForMem(ElTy)), |
| Ivan A. Kosarev | f5f2046 | 2017-10-12 11:29:46 +0000 | [diff] [blame] | 1125 | BaseLV.getType(), BaseLV.getBaseInfo(), |
| Ivan A. Kosarev | b9c59f3 | 2017-10-31 11:05:34 +0000 | [diff] [blame] | 1126 | CGF.CGM.getTBAAInfoForSubobject(BaseLV, BaseLV.getType())); |
| Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1127 | } |
| 1128 | |
| 1129 | static Address castToBase(CodeGenFunction &CGF, QualType BaseTy, QualType ElTy, |
| 1130 | llvm::Type *BaseLVType, CharUnits BaseLVAlignment, |
| 1131 | llvm::Value *Addr) { |
| 1132 | Address Tmp = Address::invalid(); |
| 1133 | Address TopTmp = Address::invalid(); |
| 1134 | Address MostTopTmp = Address::invalid(); |
| 1135 | BaseTy = BaseTy.getNonReferenceType(); |
| 1136 | while ((BaseTy->isPointerType() || BaseTy->isReferenceType()) && |
| 1137 | !CGF.getContext().hasSameType(BaseTy, ElTy)) { |
| 1138 | Tmp = CGF.CreateMemTemp(BaseTy); |
| 1139 | if (TopTmp.isValid()) |
| 1140 | CGF.Builder.CreateStore(Tmp.getPointer(), TopTmp); |
| 1141 | else |
| 1142 | MostTopTmp = Tmp; |
| 1143 | TopTmp = Tmp; |
| 1144 | BaseTy = BaseTy->getPointeeType(); |
| 1145 | } |
| 1146 | llvm::Type *Ty = BaseLVType; |
| 1147 | if (Tmp.isValid()) |
| 1148 | Ty = Tmp.getElementType(); |
| 1149 | Addr = CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(Addr, Ty); |
| 1150 | if (Tmp.isValid()) { |
| 1151 | CGF.Builder.CreateStore(Addr, Tmp); |
| 1152 | return MostTopTmp; |
| 1153 | } |
| 1154 | return Address(Addr, BaseLVAlignment); |
| 1155 | } |
| 1156 | |
| Alexey Bataev | 1c44e15 | 2018-03-06 18:59:43 +0000 | [diff] [blame] | 1157 | static const VarDecl *getBaseDecl(const Expr *Ref, const DeclRefExpr *&DE) { |
| Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1158 | const VarDecl *OrigVD = nullptr; |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1159 | if (const auto *OASE = dyn_cast<OMPArraySectionExpr>(Ref)) { |
| 1160 | const Expr *Base = OASE->getBase()->IgnoreParenImpCasts(); |
| 1161 | while (const auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base)) |
| Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1162 | Base = TempOASE->getBase()->IgnoreParenImpCasts(); |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1163 | while (const auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base)) |
| Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1164 | Base = TempASE->getBase()->IgnoreParenImpCasts(); |
| 1165 | DE = cast<DeclRefExpr>(Base); |
| 1166 | OrigVD = cast<VarDecl>(DE->getDecl()); |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1167 | } else if (const auto *ASE = dyn_cast<ArraySubscriptExpr>(Ref)) { |
| 1168 | const Expr *Base = ASE->getBase()->IgnoreParenImpCasts(); |
| 1169 | while (const auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base)) |
| Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1170 | Base = TempASE->getBase()->IgnoreParenImpCasts(); |
| 1171 | DE = cast<DeclRefExpr>(Base); |
| 1172 | OrigVD = cast<VarDecl>(DE->getDecl()); |
| 1173 | } |
| Alexey Bataev | 1c44e15 | 2018-03-06 18:59:43 +0000 | [diff] [blame] | 1174 | return OrigVD; |
| 1175 | } |
| 1176 | |
| 1177 | Address ReductionCodeGen::adjustPrivateAddress(CodeGenFunction &CGF, unsigned N, |
| 1178 | Address PrivateAddr) { |
| 1179 | const DeclRefExpr *DE; |
| 1180 | if (const VarDecl *OrigVD = ::getBaseDecl(ClausesData[N].Ref, DE)) { |
| Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1181 | BaseDecls.emplace_back(OrigVD); |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1182 | LValue OriginalBaseLValue = CGF.EmitLValue(DE); |
| Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1183 | LValue BaseLValue = |
| 1184 | loadToBegin(CGF, OrigVD->getType(), SharedAddresses[N].first.getType(), |
| 1185 | OriginalBaseLValue); |
| 1186 | llvm::Value *Adjustment = CGF.Builder.CreatePtrDiff( |
| Akira Hatanaka | f139ae3 | 2019-12-03 15:17:01 -0800 | [diff] [blame] | 1187 | BaseLValue.getPointer(CGF), SharedAddresses[N].first.getPointer(CGF)); |
| Jonas Hahnfeld | 273d261 | 2017-12-06 19:15:28 +0000 | [diff] [blame] | 1188 | llvm::Value *PrivatePointer = |
| 1189 | CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
| 1190 | PrivateAddr.getPointer(), |
| Akira Hatanaka | f139ae3 | 2019-12-03 15:17:01 -0800 | [diff] [blame] | 1191 | SharedAddresses[N].first.getAddress(CGF).getType()); |
| Jonas Hahnfeld | 273d261 | 2017-12-06 19:15:28 +0000 | [diff] [blame] | 1192 | llvm::Value *Ptr = CGF.Builder.CreateGEP(PrivatePointer, Adjustment); |
| Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1193 | return castToBase(CGF, OrigVD->getType(), |
| 1194 | SharedAddresses[N].first.getType(), |
| Akira Hatanaka | f139ae3 | 2019-12-03 15:17:01 -0800 | [diff] [blame] | 1195 | OriginalBaseLValue.getAddress(CGF).getType(), |
| Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1196 | OriginalBaseLValue.getAlignment(), Ptr); |
| 1197 | } |
| 1198 | BaseDecls.emplace_back( |
| 1199 | cast<VarDecl>(cast<DeclRefExpr>(ClausesData[N].Ref)->getDecl())); |
| 1200 | return PrivateAddr; |
| 1201 | } |
| 1202 | |
| Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 1203 | bool ReductionCodeGen::usesReductionInitializer(unsigned N) const { |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1204 | const OMPDeclareReductionDecl *DRD = |
| 1205 | getReductionInit(ClausesData[N].ReductionOp); |
| Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 1206 | return DRD && DRD->getInitializer(); |
| 1207 | } |
| 1208 | |
| Alexey Bataev | 1809571 | 2014-10-10 12:19:54 +0000 | [diff] [blame] | 1209 | LValue CGOpenMPRegionInfo::getThreadIDVariableLValue(CodeGenFunction &CGF) { |
| Alexey Bataev | 31300ed | 2016-02-04 11:27:03 +0000 | [diff] [blame] | 1210 | return CGF.EmitLoadOfPointerLValue( |
| 1211 | CGF.GetAddrOfLocalVar(getThreadIDVariable()), |
| 1212 | getThreadIDVariable()->getType()->castAs<PointerType>()); |
| Alexey Bataev | 1809571 | 2014-10-10 12:19:54 +0000 | [diff] [blame] | 1213 | } |
| 1214 | |
| Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 1215 | void CGOpenMPRegionInfo::EmitBody(CodeGenFunction &CGF, const Stmt * /*S*/) { |
| Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 1216 | if (!CGF.HaveInsertPoint()) |
| 1217 | return; |
| Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 1218 | // 1.2.2 OpenMP Language Terminology |
| 1219 | // Structured block - An executable statement with a single entry at the |
| 1220 | // top and a single exit at the bottom. |
| 1221 | // The point of exit cannot be a branch out of the structured block. |
| 1222 | // longjmp() and throw() must not violate the entry/exit criteria. |
| 1223 | CGF.EHStack.pushTerminate(); |
| Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 1224 | CodeGen(CGF); |
| Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 1225 | CGF.EHStack.popTerminate(); |
| Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 1226 | } |
| 1227 | |
| Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 1228 | LValue CGOpenMPTaskOutlinedRegionInfo::getThreadIDVariableLValue( |
| 1229 | CodeGenFunction &CGF) { |
| Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 1230 | return CGF.MakeAddrLValue(CGF.GetAddrOfLocalVar(getThreadIDVariable()), |
| 1231 | getThreadIDVariable()->getType(), |
| Ivan A. Kosarev | 5f8c0ca | 2017-10-10 09:39:32 +0000 | [diff] [blame] | 1232 | AlignmentSource::Decl); |
| Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 1233 | } |
| 1234 | |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1235 | static FieldDecl *addFieldToRecordDecl(ASTContext &C, DeclContext *DC, |
| 1236 | QualType FieldTy) { |
| 1237 | auto *Field = FieldDecl::Create( |
| 1238 | C, DC, SourceLocation(), SourceLocation(), /*Id=*/nullptr, FieldTy, |
| 1239 | C.getTrivialTypeSourceInfo(FieldTy, SourceLocation()), |
| 1240 | /*BW=*/nullptr, /*Mutable=*/false, /*InitStyle=*/ICIS_NoInit); |
| 1241 | Field->setAccess(AS_public); |
| 1242 | DC->addDecl(Field); |
| 1243 | return Field; |
| 1244 | } |
| 1245 | |
| Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 1246 | CGOpenMPRuntime::CGOpenMPRuntime(CodeGenModule &CGM, StringRef FirstSeparator, |
| 1247 | StringRef Separator) |
| 1248 | : CGM(CGM), FirstSeparator(FirstSeparator), Separator(Separator), |
| 1249 | OffloadEntriesInfoManager(CGM) { |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1250 | ASTContext &C = CGM.getContext(); |
| 1251 | RecordDecl *RD = C.buildImplicitRecord("ident_t"); |
| 1252 | QualType KmpInt32Ty = C.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1); |
| 1253 | RD->startDefinition(); |
| 1254 | // reserved_1 |
| 1255 | addFieldToRecordDecl(C, RD, KmpInt32Ty); |
| 1256 | // flags |
| 1257 | addFieldToRecordDecl(C, RD, KmpInt32Ty); |
| 1258 | // reserved_2 |
| 1259 | addFieldToRecordDecl(C, RD, KmpInt32Ty); |
| 1260 | // reserved_3 |
| 1261 | addFieldToRecordDecl(C, RD, KmpInt32Ty); |
| 1262 | // psource |
| 1263 | addFieldToRecordDecl(C, RD, C.VoidPtrTy); |
| 1264 | RD->completeDefinition(); |
| 1265 | IdentQTy = C.getRecordType(RD); |
| 1266 | IdentTy = CGM.getTypes().ConvertRecordDeclType(RD); |
| Alexey Bataev | 3a3bf0b | 2014-09-22 10:01:53 +0000 | [diff] [blame] | 1267 | KmpCriticalNameTy = llvm::ArrayType::get(CGM.Int32Ty, /*NumElements*/ 8); |
| Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 1268 | |
| 1269 | loadOffloadInfoMetadata(); |
| Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1270 | } |
| 1271 | |
| Alexey Bataev | c2cd2d4 | 2019-10-10 17:28:10 +0000 | [diff] [blame] | 1272 | bool CGOpenMPRuntime::tryEmitDeclareVariant(const GlobalDecl &NewGD, |
| 1273 | const GlobalDecl &OldGD, |
| 1274 | llvm::GlobalValue *OrigAddr, |
| 1275 | bool IsForDefinition) { |
| Alexey Bataev | 2df5f12 | 2019-10-01 20:18:32 +0000 | [diff] [blame] | 1276 | // Emit at least a definition for the aliasee if the the address of the |
| 1277 | // original function is requested. |
| 1278 | if (IsForDefinition || OrigAddr) |
| 1279 | (void)CGM.GetAddrOfGlobal(NewGD); |
| 1280 | StringRef NewMangledName = CGM.getMangledName(NewGD); |
| 1281 | llvm::GlobalValue *Addr = CGM.GetGlobalValue(NewMangledName); |
| 1282 | if (Addr && !Addr->isDeclaration()) { |
| 1283 | const auto *D = cast<FunctionDecl>(OldGD.getDecl()); |
| Alexey Bataev | 5ad5258 | 2019-12-12 15:33:18 -0500 | [diff] [blame] | 1284 | const CGFunctionInfo &FI = CGM.getTypes().arrangeGlobalDeclaration(NewGD); |
| Alexey Bataev | 2df5f12 | 2019-10-01 20:18:32 +0000 | [diff] [blame] | 1285 | llvm::Type *DeclTy = CGM.getTypes().GetFunctionType(FI); |
| 1286 | |
| 1287 | // Create a reference to the named value. This ensures that it is emitted |
| 1288 | // if a deferred decl. |
| 1289 | llvm::GlobalValue::LinkageTypes LT = CGM.getFunctionLinkage(OldGD); |
| 1290 | |
| 1291 | // Create the new alias itself, but don't set a name yet. |
| 1292 | auto *GA = |
| 1293 | llvm::GlobalAlias::create(DeclTy, 0, LT, "", Addr, &CGM.getModule()); |
| 1294 | |
| 1295 | if (OrigAddr) { |
| 1296 | assert(OrigAddr->isDeclaration() && "Expected declaration"); |
| 1297 | |
| 1298 | GA->takeName(OrigAddr); |
| 1299 | OrigAddr->replaceAllUsesWith( |
| 1300 | llvm::ConstantExpr::getBitCast(GA, OrigAddr->getType())); |
| 1301 | OrigAddr->eraseFromParent(); |
| 1302 | } else { |
| 1303 | GA->setName(CGM.getMangledName(OldGD)); |
| 1304 | } |
| 1305 | |
| 1306 | // Set attributes which are particular to an alias; this is a |
| 1307 | // specialization of the attributes which may be set on a global function. |
| 1308 | if (D->hasAttr<WeakAttr>() || D->hasAttr<WeakRefAttr>() || |
| 1309 | D->isWeakImported()) |
| 1310 | GA->setLinkage(llvm::Function::WeakAnyLinkage); |
| 1311 | |
| 1312 | CGM.SetCommonAttributes(OldGD, GA); |
| 1313 | return true; |
| 1314 | } |
| 1315 | return false; |
| 1316 | } |
| 1317 | |
| Alexey Bataev | 9179755 | 2015-03-18 04:13:55 +0000 | [diff] [blame] | 1318 | void CGOpenMPRuntime::clear() { |
| 1319 | InternalVars.clear(); |
| Alexey Bataev | 2a6f3f5 | 2018-11-07 19:11:14 +0000 | [diff] [blame] | 1320 | // Clean non-target variable declarations possibly used only in debug info. |
| 1321 | for (const auto &Data : EmittedNonTargetVariables) { |
| 1322 | if (!Data.getValue().pointsToAliveValue()) |
| 1323 | continue; |
| 1324 | auto *GV = dyn_cast<llvm::GlobalVariable>(Data.getValue()); |
| 1325 | if (!GV) |
| 1326 | continue; |
| 1327 | if (!GV->isDeclaration() || GV->getNumUses() > 0) |
| 1328 | continue; |
| 1329 | GV->eraseFromParent(); |
| 1330 | } |
| Alexey Bataev | 2df5f12 | 2019-10-01 20:18:32 +0000 | [diff] [blame] | 1331 | // Emit aliases for the deferred aliasees. |
| 1332 | for (const auto &Pair : DeferredVariantFunction) { |
| 1333 | StringRef MangledName = CGM.getMangledName(Pair.second.second); |
| 1334 | llvm::GlobalValue *Addr = CGM.GetGlobalValue(MangledName); |
| 1335 | // If not able to emit alias, just emit original declaration. |
| Alexey Bataev | c2cd2d4 | 2019-10-10 17:28:10 +0000 | [diff] [blame] | 1336 | (void)tryEmitDeclareVariant(Pair.second.first, Pair.second.second, Addr, |
| 1337 | /*IsForDefinition=*/false); |
| Alexey Bataev | 2df5f12 | 2019-10-01 20:18:32 +0000 | [diff] [blame] | 1338 | } |
| Alexey Bataev | 9179755 | 2015-03-18 04:13:55 +0000 | [diff] [blame] | 1339 | } |
| 1340 | |
| Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 1341 | std::string CGOpenMPRuntime::getName(ArrayRef<StringRef> Parts) const { |
| 1342 | SmallString<128> Buffer; |
| 1343 | llvm::raw_svector_ostream OS(Buffer); |
| 1344 | StringRef Sep = FirstSeparator; |
| 1345 | for (StringRef Part : Parts) { |
| 1346 | OS << Sep << Part; |
| 1347 | Sep = Separator; |
| 1348 | } |
| Benjamin Kramer | adcd026 | 2020-01-28 20:23:46 +0100 | [diff] [blame] | 1349 | return std::string(OS.str()); |
| Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 1350 | } |
| 1351 | |
| Alexey Bataev | c5b1d32 | 2016-03-04 09:22:22 +0000 | [diff] [blame] | 1352 | static llvm::Function * |
| 1353 | emitCombinerOrInitializer(CodeGenModule &CGM, QualType Ty, |
| 1354 | const Expr *CombinerInitializer, const VarDecl *In, |
| 1355 | const VarDecl *Out, bool IsCombiner) { |
| 1356 | // void .omp_combiner.(Ty *in, Ty *out); |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1357 | ASTContext &C = CGM.getContext(); |
| Alexey Bataev | c5b1d32 | 2016-03-04 09:22:22 +0000 | [diff] [blame] | 1358 | QualType PtrTy = C.getPointerType(Ty).withRestrict(); |
| 1359 | FunctionArgList Args; |
| Alexey Bataev | c5b1d32 | 2016-03-04 09:22:22 +0000 | [diff] [blame] | 1360 | ImplicitParamDecl OmpOutParm(C, /*DC=*/nullptr, Out->getLocation(), |
| Alexey Bataev | 5622323 | 2017-06-09 13:40:18 +0000 | [diff] [blame] | 1361 | /*Id=*/nullptr, PtrTy, ImplicitParamDecl::Other); |
| Alexey Bataev | a839ddd | 2016-03-17 10:19:46 +0000 | [diff] [blame] | 1362 | ImplicitParamDecl OmpInParm(C, /*DC=*/nullptr, In->getLocation(), |
| Alexey Bataev | 5622323 | 2017-06-09 13:40:18 +0000 | [diff] [blame] | 1363 | /*Id=*/nullptr, PtrTy, ImplicitParamDecl::Other); |
| Alexey Bataev | c5b1d32 | 2016-03-04 09:22:22 +0000 | [diff] [blame] | 1364 | Args.push_back(&OmpOutParm); |
| Alexey Bataev | a839ddd | 2016-03-17 10:19:46 +0000 | [diff] [blame] | 1365 | Args.push_back(&OmpInParm); |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1366 | const CGFunctionInfo &FnInfo = |
| John McCall | c56a8b3 | 2016-03-11 04:30:31 +0000 | [diff] [blame] | 1367 | CGM.getTypes().arrangeBuiltinFunctionDeclaration(C.VoidTy, Args); |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1368 | llvm::FunctionType *FnTy = CGM.getTypes().GetFunctionType(FnInfo); |
| Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 1369 | std::string Name = CGM.getOpenMPRuntime().getName( |
| 1370 | {IsCombiner ? "omp_combiner" : "omp_initializer", ""}); |
| 1371 | auto *Fn = llvm::Function::Create(FnTy, llvm::GlobalValue::InternalLinkage, |
| 1372 | Name, &CGM.getModule()); |
| Rafael Espindola | 51ec5a9 | 2018-02-28 23:46:35 +0000 | [diff] [blame] | 1373 | CGM.SetInternalFunctionAttributes(GlobalDecl(), Fn, FnInfo); |
| Alexey Bataev | 8c5555c | 2019-05-21 15:11:58 +0000 | [diff] [blame] | 1374 | if (CGM.getLangOpts().Optimize) { |
| 1375 | Fn->removeFnAttr(llvm::Attribute::NoInline); |
| 1376 | Fn->removeFnAttr(llvm::Attribute::OptimizeNone); |
| 1377 | Fn->addFnAttr(llvm::Attribute::AlwaysInline); |
| 1378 | } |
| Alexey Bataev | c5b1d32 | 2016-03-04 09:22:22 +0000 | [diff] [blame] | 1379 | CodeGenFunction CGF(CGM); |
| 1380 | // Map "T omp_in;" variable to "*omp_in_parm" value in all expressions. |
| 1381 | // Map "T omp_out;" variable to "*omp_out_parm" value in all expressions. |
| Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 1382 | CGF.StartFunction(GlobalDecl(), C.VoidTy, Fn, FnInfo, Args, In->getLocation(), |
| 1383 | Out->getLocation()); |
| Alexey Bataev | c5b1d32 | 2016-03-04 09:22:22 +0000 | [diff] [blame] | 1384 | CodeGenFunction::OMPPrivateScope Scope(CGF); |
| 1385 | Address AddrIn = CGF.GetAddrOfLocalVar(&OmpInParm); |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1386 | Scope.addPrivate(In, [&CGF, AddrIn, PtrTy]() { |
| Alexey Bataev | c5b1d32 | 2016-03-04 09:22:22 +0000 | [diff] [blame] | 1387 | return CGF.EmitLoadOfPointerLValue(AddrIn, PtrTy->castAs<PointerType>()) |
| Akira Hatanaka | f139ae3 | 2019-12-03 15:17:01 -0800 | [diff] [blame] | 1388 | .getAddress(CGF); |
| Alexey Bataev | c5b1d32 | 2016-03-04 09:22:22 +0000 | [diff] [blame] | 1389 | }); |
| 1390 | Address AddrOut = CGF.GetAddrOfLocalVar(&OmpOutParm); |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1391 | Scope.addPrivate(Out, [&CGF, AddrOut, PtrTy]() { |
| Alexey Bataev | c5b1d32 | 2016-03-04 09:22:22 +0000 | [diff] [blame] | 1392 | return CGF.EmitLoadOfPointerLValue(AddrOut, PtrTy->castAs<PointerType>()) |
| Akira Hatanaka | f139ae3 | 2019-12-03 15:17:01 -0800 | [diff] [blame] | 1393 | .getAddress(CGF); |
| Alexey Bataev | c5b1d32 | 2016-03-04 09:22:22 +0000 | [diff] [blame] | 1394 | }); |
| 1395 | (void)Scope.Privatize(); |
| Alexey Bataev | 070f43a | 2017-09-06 14:49:58 +0000 | [diff] [blame] | 1396 | if (!IsCombiner && Out->hasInit() && |
| 1397 | !CGF.isTrivialInitializer(Out->getInit())) { |
| 1398 | CGF.EmitAnyExprToMem(Out->getInit(), CGF.GetAddrOfLocalVar(Out), |
| 1399 | Out->getType().getQualifiers(), |
| 1400 | /*IsInitializer=*/true); |
| 1401 | } |
| 1402 | if (CombinerInitializer) |
| 1403 | CGF.EmitIgnoredExpr(CombinerInitializer); |
| Alexey Bataev | c5b1d32 | 2016-03-04 09:22:22 +0000 | [diff] [blame] | 1404 | Scope.ForceCleanup(); |
| 1405 | CGF.FinishFunction(); |
| 1406 | return Fn; |
| 1407 | } |
| 1408 | |
| 1409 | void CGOpenMPRuntime::emitUserDefinedReduction( |
| 1410 | CodeGenFunction *CGF, const OMPDeclareReductionDecl *D) { |
| 1411 | if (UDRMap.count(D) > 0) |
| 1412 | return; |
| Alexey Bataev | c5b1d32 | 2016-03-04 09:22:22 +0000 | [diff] [blame] | 1413 | llvm::Function *Combiner = emitCombinerOrInitializer( |
| Alexey Bataev | e6aa469 | 2018-09-13 16:54:05 +0000 | [diff] [blame] | 1414 | CGM, D->getType(), D->getCombiner(), |
| 1415 | cast<VarDecl>(cast<DeclRefExpr>(D->getCombinerIn())->getDecl()), |
| 1416 | cast<VarDecl>(cast<DeclRefExpr>(D->getCombinerOut())->getDecl()), |
| Alexey Bataev | c5b1d32 | 2016-03-04 09:22:22 +0000 | [diff] [blame] | 1417 | /*IsCombiner=*/true); |
| 1418 | llvm::Function *Initializer = nullptr; |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1419 | if (const Expr *Init = D->getInitializer()) { |
| Alexey Bataev | c5b1d32 | 2016-03-04 09:22:22 +0000 | [diff] [blame] | 1420 | Initializer = emitCombinerOrInitializer( |
| Alexey Bataev | 070f43a | 2017-09-06 14:49:58 +0000 | [diff] [blame] | 1421 | CGM, D->getType(), |
| 1422 | D->getInitializerKind() == OMPDeclareReductionDecl::CallInit ? Init |
| 1423 | : nullptr, |
| Alexey Bataev | e6aa469 | 2018-09-13 16:54:05 +0000 | [diff] [blame] | 1424 | cast<VarDecl>(cast<DeclRefExpr>(D->getInitOrig())->getDecl()), |
| 1425 | cast<VarDecl>(cast<DeclRefExpr>(D->getInitPriv())->getDecl()), |
| Alexey Bataev | c5b1d32 | 2016-03-04 09:22:22 +0000 | [diff] [blame] | 1426 | /*IsCombiner=*/false); |
| 1427 | } |
| Alexey Bataev | 43a919f | 2018-04-13 17:48:43 +0000 | [diff] [blame] | 1428 | UDRMap.try_emplace(D, Combiner, Initializer); |
| Alexey Bataev | c5b1d32 | 2016-03-04 09:22:22 +0000 | [diff] [blame] | 1429 | if (CGF) { |
| 1430 | auto &Decls = FunctionUDRMap.FindAndConstruct(CGF->CurFn); |
| 1431 | Decls.second.push_back(D); |
| 1432 | } |
| 1433 | } |
| 1434 | |
| Alexey Bataev | a839ddd | 2016-03-17 10:19:46 +0000 | [diff] [blame] | 1435 | std::pair<llvm::Function *, llvm::Function *> |
| 1436 | CGOpenMPRuntime::getUserDefinedReduction(const OMPDeclareReductionDecl *D) { |
| 1437 | auto I = UDRMap.find(D); |
| 1438 | if (I != UDRMap.end()) |
| 1439 | return I->second; |
| 1440 | emitUserDefinedReduction(/*CGF=*/nullptr, D); |
| 1441 | return UDRMap.lookup(D); |
| 1442 | } |
| 1443 | |
| Benjamin Kramer | df18650 | 2020-01-14 14:06:12 +0100 | [diff] [blame] | 1444 | namespace { |
| Johannes Doerfert | f9c3c5da | 2019-12-25 10:33:56 -0600 | [diff] [blame] | 1445 | // Temporary RAII solution to perform a push/pop stack event on the OpenMP IR |
| 1446 | // Builder if one is present. |
| 1447 | struct PushAndPopStackRAII { |
| 1448 | PushAndPopStackRAII(llvm::OpenMPIRBuilder *OMPBuilder, CodeGenFunction &CGF, |
| 1449 | bool HasCancel) |
| 1450 | : OMPBuilder(OMPBuilder) { |
| 1451 | if (!OMPBuilder) |
| 1452 | return; |
| 1453 | |
| 1454 | // The following callback is the crucial part of clangs cleanup process. |
| 1455 | // |
| 1456 | // NOTE: |
| 1457 | // Once the OpenMPIRBuilder is used to create parallel regions (and |
| 1458 | // similar), the cancellation destination (Dest below) is determined via |
| 1459 | // IP. That means if we have variables to finalize we split the block at IP, |
| 1460 | // use the new block (=BB) as destination to build a JumpDest (via |
| 1461 | // getJumpDestInCurrentScope(BB)) which then is fed to |
| 1462 | // EmitBranchThroughCleanup. Furthermore, there will not be the need |
| 1463 | // to push & pop an FinalizationInfo object. |
| 1464 | // The FiniCB will still be needed but at the point where the |
| 1465 | // OpenMPIRBuilder is asked to construct a parallel (or similar) construct. |
| 1466 | auto FiniCB = [&CGF](llvm::OpenMPIRBuilder::InsertPointTy IP) { |
| 1467 | assert(IP.getBlock()->end() == IP.getPoint() && |
| 1468 | "Clang CG should cause non-terminated block!"); |
| 1469 | CGBuilderTy::InsertPointGuard IPG(CGF.Builder); |
| 1470 | CGF.Builder.restoreIP(IP); |
| 1471 | CodeGenFunction::JumpDest Dest = |
| 1472 | CGF.getOMPCancelDestination(OMPD_parallel); |
| 1473 | CGF.EmitBranchThroughCleanup(Dest); |
| 1474 | }; |
| 1475 | |
| 1476 | // TODO: Remove this once we emit parallel regions through the |
| 1477 | // OpenMPIRBuilder as it can do this setup internally. |
| 1478 | llvm::OpenMPIRBuilder::FinalizationInfo FI( |
| 1479 | {FiniCB, OMPD_parallel, HasCancel}); |
| 1480 | OMPBuilder->pushFinalizationCB(std::move(FI)); |
| 1481 | } |
| 1482 | ~PushAndPopStackRAII() { |
| 1483 | if (OMPBuilder) |
| 1484 | OMPBuilder->popFinalizationCB(); |
| 1485 | } |
| 1486 | llvm::OpenMPIRBuilder *OMPBuilder; |
| 1487 | }; |
| Benjamin Kramer | df18650 | 2020-01-14 14:06:12 +0100 | [diff] [blame] | 1488 | } // namespace |
| Johannes Doerfert | f9c3c5da | 2019-12-25 10:33:56 -0600 | [diff] [blame] | 1489 | |
| James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 1490 | static llvm::Function *emitParallelOrTeamsOutlinedFunction( |
| Arpith Chacko Jacob | 19b911c | 2017-01-18 18:18:53 +0000 | [diff] [blame] | 1491 | CodeGenModule &CGM, const OMPExecutableDirective &D, const CapturedStmt *CS, |
| 1492 | const VarDecl *ThreadIDVar, OpenMPDirectiveKind InnermostKind, |
| 1493 | const StringRef OutlinedHelperName, const RegionCodeGenTy &CodeGen) { |
| Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 1494 | assert(ThreadIDVar->getType()->isPointerType() && |
| 1495 | "thread id variable must be of type kmp_int32 *"); |
| Alexey Bataev | 1809571 | 2014-10-10 12:19:54 +0000 | [diff] [blame] | 1496 | CodeGenFunction CGF(CGM, true); |
| Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 1497 | bool HasCancel = false; |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1498 | if (const auto *OPD = dyn_cast<OMPParallelDirective>(&D)) |
| Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 1499 | HasCancel = OPD->hasCancel(); |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1500 | else if (const auto *OPSD = dyn_cast<OMPParallelSectionsDirective>(&D)) |
| Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 1501 | HasCancel = OPSD->hasCancel(); |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1502 | else if (const auto *OPFD = dyn_cast<OMPParallelForDirective>(&D)) |
| Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 1503 | HasCancel = OPFD->hasCancel(); |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1504 | else if (const auto *OPFD = dyn_cast<OMPTargetParallelForDirective>(&D)) |
| Alexey Bataev | 2139ed6 | 2017-11-16 18:20:21 +0000 | [diff] [blame] | 1505 | HasCancel = OPFD->hasCancel(); |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1506 | else if (const auto *OPFD = dyn_cast<OMPDistributeParallelForDirective>(&D)) |
| Alexey Bataev | 10a5431 | 2017-11-27 16:54:08 +0000 | [diff] [blame] | 1507 | HasCancel = OPFD->hasCancel(); |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1508 | else if (const auto *OPFD = |
| 1509 | dyn_cast<OMPTeamsDistributeParallelForDirective>(&D)) |
| Alexey Bataev | 10a5431 | 2017-11-27 16:54:08 +0000 | [diff] [blame] | 1510 | HasCancel = OPFD->hasCancel(); |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1511 | else if (const auto *OPFD = |
| Alexey Bataev | 10a5431 | 2017-11-27 16:54:08 +0000 | [diff] [blame] | 1512 | dyn_cast<OMPTargetTeamsDistributeParallelForDirective>(&D)) |
| 1513 | HasCancel = OPFD->hasCancel(); |
| Johannes Doerfert | f9c3c5da | 2019-12-25 10:33:56 -0600 | [diff] [blame] | 1514 | |
| 1515 | // TODO: Temporarily inform the OpenMPIRBuilder, if any, about the new |
| 1516 | // parallel region to make cancellation barriers work properly. |
| 1517 | llvm::OpenMPIRBuilder *OMPBuilder = CGM.getOpenMPIRBuilder(); |
| 1518 | PushAndPopStackRAII PSR(OMPBuilder, CGF, HasCancel); |
| Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 1519 | CGOpenMPOutlinedRegionInfo CGInfo(*CS, ThreadIDVar, CodeGen, InnermostKind, |
| Arpith Chacko Jacob | 19b911c | 2017-01-18 18:18:53 +0000 | [diff] [blame] | 1520 | HasCancel, OutlinedHelperName); |
| Alexey Bataev | d157d47 | 2015-06-24 03:35:38 +0000 | [diff] [blame] | 1521 | CodeGenFunction::CGCapturedStmtRAII CapInfoRAII(CGF, &CGInfo); |
| Alexey Bataev | c33ba8c | 2020-01-17 14:05:40 -0500 | [diff] [blame] | 1522 | return CGF.GenerateOpenMPCapturedStmtFunction(*CS, D.getBeginLoc()); |
| Alexey Bataev | 1809571 | 2014-10-10 12:19:54 +0000 | [diff] [blame] | 1523 | } |
| 1524 | |
| James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 1525 | llvm::Function *CGOpenMPRuntime::emitParallelOutlinedFunction( |
| Arpith Chacko Jacob | 19b911c | 2017-01-18 18:18:53 +0000 | [diff] [blame] | 1526 | const OMPExecutableDirective &D, const VarDecl *ThreadIDVar, |
| 1527 | OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen) { |
| 1528 | const CapturedStmt *CS = D.getCapturedStmt(OMPD_parallel); |
| 1529 | return emitParallelOrTeamsOutlinedFunction( |
| 1530 | CGM, D, CS, ThreadIDVar, InnermostKind, getOutlinedHelperName(), CodeGen); |
| 1531 | } |
| 1532 | |
| James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 1533 | llvm::Function *CGOpenMPRuntime::emitTeamsOutlinedFunction( |
| Arpith Chacko Jacob | 19b911c | 2017-01-18 18:18:53 +0000 | [diff] [blame] | 1534 | const OMPExecutableDirective &D, const VarDecl *ThreadIDVar, |
| 1535 | OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen) { |
| 1536 | const CapturedStmt *CS = D.getCapturedStmt(OMPD_teams); |
| 1537 | return emitParallelOrTeamsOutlinedFunction( |
| 1538 | CGM, D, CS, ThreadIDVar, InnermostKind, getOutlinedHelperName(), CodeGen); |
| 1539 | } |
| 1540 | |
| James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 1541 | llvm::Function *CGOpenMPRuntime::emitTaskOutlinedFunction( |
| Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 1542 | const OMPExecutableDirective &D, const VarDecl *ThreadIDVar, |
| Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 1543 | const VarDecl *PartIDVar, const VarDecl *TaskTVar, |
| 1544 | OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen, |
| 1545 | bool Tied, unsigned &NumberOfParts) { |
| 1546 | auto &&UntiedCodeGen = [this, &D, TaskTVar](CodeGenFunction &CGF, |
| 1547 | PrePostActionTy &) { |
| Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1548 | llvm::Value *ThreadID = getThreadID(CGF, D.getBeginLoc()); |
| 1549 | llvm::Value *UpLoc = emitUpdateLocation(CGF, D.getBeginLoc()); |
| Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 1550 | llvm::Value *TaskArgs[] = { |
| 1551 | UpLoc, ThreadID, |
| 1552 | CGF.EmitLoadOfPointerLValue(CGF.GetAddrOfLocalVar(TaskTVar), |
| 1553 | TaskTVar->getType()->castAs<PointerType>()) |
| Akira Hatanaka | f139ae3 | 2019-12-03 15:17:01 -0800 | [diff] [blame] | 1554 | .getPointer(CGF)}; |
| Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 1555 | CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_omp_task), TaskArgs); |
| 1556 | }; |
| 1557 | CGOpenMPTaskOutlinedRegionInfo::UntiedTaskActionTy Action(Tied, PartIDVar, |
| 1558 | UntiedCodeGen); |
| 1559 | CodeGen.setAction(Action); |
| Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 1560 | assert(!ThreadIDVar->getType()->isPointerType() && |
| 1561 | "thread id variable must be of type kmp_int32 for tasks"); |
| Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 1562 | const OpenMPDirectiveKind Region = |
| 1563 | isOpenMPTaskLoopDirective(D.getDirectiveKind()) ? OMPD_taskloop |
| 1564 | : OMPD_task; |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1565 | const CapturedStmt *CS = D.getCapturedStmt(Region); |
| Alexey Bataev | e0ca479 | 2020-02-12 16:12:53 -0500 | [diff] [blame] | 1566 | bool HasCancel = false; |
| 1567 | if (const auto *TD = dyn_cast<OMPTaskDirective>(&D)) |
| 1568 | HasCancel = TD->hasCancel(); |
| 1569 | else if (const auto *TD = dyn_cast<OMPTaskLoopDirective>(&D)) |
| 1570 | HasCancel = TD->hasCancel(); |
| 1571 | else if (const auto *TD = dyn_cast<OMPMasterTaskLoopDirective>(&D)) |
| 1572 | HasCancel = TD->hasCancel(); |
| 1573 | else if (const auto *TD = dyn_cast<OMPParallelMasterTaskLoopDirective>(&D)) |
| 1574 | HasCancel = TD->hasCancel(); |
| 1575 | |
| Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 1576 | CodeGenFunction CGF(CGM, true); |
| Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 1577 | CGOpenMPTaskOutlinedRegionInfo CGInfo(*CS, ThreadIDVar, CodeGen, |
| Alexey Bataev | e0ca479 | 2020-02-12 16:12:53 -0500 | [diff] [blame] | 1578 | InnermostKind, HasCancel, Action); |
| Alexey Bataev | d157d47 | 2015-06-24 03:35:38 +0000 | [diff] [blame] | 1579 | CodeGenFunction::CGCapturedStmtRAII CapInfoRAII(CGF, &CGInfo); |
| James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 1580 | llvm::Function *Res = CGF.GenerateCapturedStmtFunction(*CS); |
| Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 1581 | if (!Tied) |
| 1582 | NumberOfParts = Action.getNumberOfParts(); |
| 1583 | return Res; |
| Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 1584 | } |
| 1585 | |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1586 | static void buildStructValue(ConstantStructBuilder &Fields, CodeGenModule &CGM, |
| 1587 | const RecordDecl *RD, const CGRecordLayout &RL, |
| 1588 | ArrayRef<llvm::Constant *> Data) { |
| 1589 | llvm::StructType *StructTy = RL.getLLVMType(); |
| 1590 | unsigned PrevIdx = 0; |
| 1591 | ConstantInitBuilder CIBuilder(CGM); |
| 1592 | auto DI = Data.begin(); |
| 1593 | for (const FieldDecl *FD : RD->fields()) { |
| 1594 | unsigned Idx = RL.getLLVMFieldNo(FD); |
| 1595 | // Fill the alignment. |
| 1596 | for (unsigned I = PrevIdx; I < Idx; ++I) |
| 1597 | Fields.add(llvm::Constant::getNullValue(StructTy->getElementType(I))); |
| 1598 | PrevIdx = Idx + 1; |
| 1599 | Fields.add(*DI); |
| 1600 | ++DI; |
| 1601 | } |
| 1602 | } |
| 1603 | |
| 1604 | template <class... As> |
| 1605 | static llvm::GlobalVariable * |
| Mike Rice | e1ca7b6 | 2018-08-29 15:45:11 +0000 | [diff] [blame] | 1606 | createGlobalStruct(CodeGenModule &CGM, QualType Ty, bool IsConstant, |
| 1607 | ArrayRef<llvm::Constant *> Data, const Twine &Name, |
| 1608 | As &&... Args) { |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1609 | const auto *RD = cast<RecordDecl>(Ty->getAsTagDecl()); |
| 1610 | const CGRecordLayout &RL = CGM.getTypes().getCGRecordLayout(RD); |
| 1611 | ConstantInitBuilder CIBuilder(CGM); |
| 1612 | ConstantStructBuilder Fields = CIBuilder.beginStruct(RL.getLLVMType()); |
| 1613 | buildStructValue(Fields, CGM, RD, RL, Data); |
| 1614 | return Fields.finishAndCreateGlobal( |
| Mike Rice | e1ca7b6 | 2018-08-29 15:45:11 +0000 | [diff] [blame] | 1615 | Name, CGM.getContext().getAlignOfGlobalVarInChars(Ty), IsConstant, |
| 1616 | std::forward<As>(Args)...); |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1617 | } |
| 1618 | |
| 1619 | template <typename T> |
| Benjamin Kramer | 651d0bf | 2018-05-15 21:26:47 +0000 | [diff] [blame] | 1620 | static void |
| 1621 | createConstantGlobalStructAndAddToParent(CodeGenModule &CGM, QualType Ty, |
| 1622 | ArrayRef<llvm::Constant *> Data, |
| 1623 | T &Parent) { |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1624 | const auto *RD = cast<RecordDecl>(Ty->getAsTagDecl()); |
| 1625 | const CGRecordLayout &RL = CGM.getTypes().getCGRecordLayout(RD); |
| 1626 | ConstantStructBuilder Fields = Parent.beginStruct(RL.getLLVMType()); |
| 1627 | buildStructValue(Fields, CGM, RD, RL, Data); |
| 1628 | Fields.finishAndAddTo(Parent); |
| 1629 | } |
| 1630 | |
| Alexey Bataev | 50b3c95 | 2016-02-19 10:38:26 +0000 | [diff] [blame] | 1631 | Address CGOpenMPRuntime::getOrCreateDefaultLocation(unsigned Flags) { |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1632 | CharUnits Align = CGM.getContext().getTypeAlignInChars(IdentQTy); |
| Alexey Bataev | ceeaa48 | 2018-11-21 21:04:34 +0000 | [diff] [blame] | 1633 | unsigned Reserved2Flags = getDefaultLocationReserved2Flags(); |
| 1634 | FlagsTy FlagsKey(Flags, Reserved2Flags); |
| 1635 | llvm::Value *Entry = OpenMPDefaultLocMap.lookup(FlagsKey); |
| Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1636 | if (!Entry) { |
| 1637 | if (!DefaultOpenMPPSource) { |
| 1638 | // Initialize default location for psource field of ident_t structure of |
| 1639 | // all ident_t objects. Format is ";file;function;line;column;;". |
| 1640 | // Taken from |
| James Y Knight | 5d71fc5 | 2019-01-29 16:37:27 +0000 | [diff] [blame] | 1641 | // https://github.com/llvm/llvm-project/blob/master/openmp/runtime/src/kmp_str.cpp |
| Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1642 | DefaultOpenMPPSource = |
| John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1643 | CGM.GetAddrOfConstantCString(";unknown;unknown;0;0;;").getPointer(); |
| Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1644 | DefaultOpenMPPSource = |
| 1645 | llvm::ConstantExpr::getBitCast(DefaultOpenMPPSource, CGM.Int8PtrTy); |
| 1646 | } |
| Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1647 | |
| Alexey Bataev | ceeaa48 | 2018-11-21 21:04:34 +0000 | [diff] [blame] | 1648 | llvm::Constant *Data[] = { |
| 1649 | llvm::ConstantInt::getNullValue(CGM.Int32Ty), |
| 1650 | llvm::ConstantInt::get(CGM.Int32Ty, Flags), |
| 1651 | llvm::ConstantInt::get(CGM.Int32Ty, Reserved2Flags), |
| 1652 | llvm::ConstantInt::getNullValue(CGM.Int32Ty), DefaultOpenMPPSource}; |
| Mike Rice | e1ca7b6 | 2018-08-29 15:45:11 +0000 | [diff] [blame] | 1653 | llvm::GlobalValue *DefaultOpenMPLocation = |
| Alexey Bataev | ceeaa48 | 2018-11-21 21:04:34 +0000 | [diff] [blame] | 1654 | createGlobalStruct(CGM, IdentQTy, isDefaultLocationConstant(), Data, "", |
| Mike Rice | e1ca7b6 | 2018-08-29 15:45:11 +0000 | [diff] [blame] | 1655 | llvm::GlobalValue::PrivateLinkage); |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1656 | DefaultOpenMPLocation->setUnnamedAddr( |
| 1657 | llvm::GlobalValue::UnnamedAddr::Global); |
| John McCall | 6c9f1fdb | 2016-11-19 08:17:24 +0000 | [diff] [blame] | 1658 | |
| Alexey Bataev | ceeaa48 | 2018-11-21 21:04:34 +0000 | [diff] [blame] | 1659 | OpenMPDefaultLocMap[FlagsKey] = Entry = DefaultOpenMPLocation; |
| Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1660 | } |
| John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1661 | return Address(Entry, Align); |
| Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1662 | } |
| 1663 | |
| Alexey Bataev | fd006c4 | 2018-10-05 15:08:53 +0000 | [diff] [blame] | 1664 | void CGOpenMPRuntime::setLocThreadIdInsertPt(CodeGenFunction &CGF, |
| 1665 | bool AtCurrentPoint) { |
| 1666 | auto &Elem = OpenMPLocThreadIDMap.FindAndConstruct(CGF.CurFn); |
| 1667 | assert(!Elem.second.ServiceInsertPt && "Insert point is set already."); |
| 1668 | |
| 1669 | llvm::Value *Undef = llvm::UndefValue::get(CGF.Int32Ty); |
| 1670 | if (AtCurrentPoint) { |
| 1671 | Elem.second.ServiceInsertPt = new llvm::BitCastInst( |
| 1672 | Undef, CGF.Int32Ty, "svcpt", CGF.Builder.GetInsertBlock()); |
| 1673 | } else { |
| 1674 | Elem.second.ServiceInsertPt = |
| 1675 | new llvm::BitCastInst(Undef, CGF.Int32Ty, "svcpt"); |
| 1676 | Elem.second.ServiceInsertPt->insertAfter(CGF.AllocaInsertPt); |
| 1677 | } |
| 1678 | } |
| 1679 | |
| 1680 | void CGOpenMPRuntime::clearLocThreadIdInsertPt(CodeGenFunction &CGF) { |
| 1681 | auto &Elem = OpenMPLocThreadIDMap.FindAndConstruct(CGF.CurFn); |
| 1682 | if (Elem.second.ServiceInsertPt) { |
| 1683 | llvm::Instruction *Ptr = Elem.second.ServiceInsertPt; |
| 1684 | Elem.second.ServiceInsertPt = nullptr; |
| 1685 | Ptr->eraseFromParent(); |
| 1686 | } |
| 1687 | } |
| 1688 | |
| Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 1689 | llvm::Value *CGOpenMPRuntime::emitUpdateLocation(CodeGenFunction &CGF, |
| 1690 | SourceLocation Loc, |
| Alexey Bataev | 50b3c95 | 2016-02-19 10:38:26 +0000 | [diff] [blame] | 1691 | unsigned Flags) { |
| 1692 | Flags |= OMP_IDENT_KMPC; |
| Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1693 | // If no debug info is generated - return global default location. |
| Benjamin Kramer | 8c30592 | 2016-02-02 11:06:51 +0000 | [diff] [blame] | 1694 | if (CGM.getCodeGenOpts().getDebugInfo() == codegenoptions::NoDebugInfo || |
| Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1695 | Loc.isInvalid()) |
| John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1696 | return getOrCreateDefaultLocation(Flags).getPointer(); |
| Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1697 | |
| 1698 | assert(CGF.CurFn && "No function in current CodeGenFunction."); |
| 1699 | |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1700 | CharUnits Align = CGM.getContext().getTypeAlignInChars(IdentQTy); |
| John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1701 | Address LocValue = Address::invalid(); |
| Alexey Bataev | 1e4b713 | 2014-12-03 12:11:24 +0000 | [diff] [blame] | 1702 | auto I = OpenMPLocThreadIDMap.find(CGF.CurFn); |
| 1703 | if (I != OpenMPLocThreadIDMap.end()) |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1704 | LocValue = Address(I->second.DebugLoc, Align); |
| John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1705 | |
| Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 1706 | // OpenMPLocThreadIDMap may have null DebugLoc and non-null ThreadID, if |
| 1707 | // GetOpenMPThreadID was called before this routine. |
| John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1708 | if (!LocValue.isValid()) { |
| Alexey Bataev | 15007ba | 2014-05-07 06:18:01 +0000 | [diff] [blame] | 1709 | // Generate "ident_t .kmpc_loc.addr;" |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1710 | Address AI = CGF.CreateMemTemp(IdentQTy, ".kmpc_loc.addr"); |
| Alexey Bataev | 1809571 | 2014-10-10 12:19:54 +0000 | [diff] [blame] | 1711 | auto &Elem = OpenMPLocThreadIDMap.FindAndConstruct(CGF.CurFn); |
| John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1712 | Elem.second.DebugLoc = AI.getPointer(); |
| Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1713 | LocValue = AI; |
| 1714 | |
| Alexey Bataev | fd006c4 | 2018-10-05 15:08:53 +0000 | [diff] [blame] | 1715 | if (!Elem.second.ServiceInsertPt) |
| 1716 | setLocThreadIdInsertPt(CGF); |
| Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1717 | CGBuilderTy::InsertPointGuard IPG(CGF.Builder); |
| Alexey Bataev | fd006c4 | 2018-10-05 15:08:53 +0000 | [diff] [blame] | 1718 | CGF.Builder.SetInsertPoint(Elem.second.ServiceInsertPt); |
| Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 1719 | CGF.Builder.CreateMemCpy(LocValue, getOrCreateDefaultLocation(Flags), |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1720 | CGF.getTypeSize(IdentQTy)); |
| Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1721 | } |
| 1722 | |
| 1723 | // char **psource = &.kmpc_loc_<flags>.addr.psource; |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1724 | LValue Base = CGF.MakeAddrLValue(LocValue, IdentQTy); |
| 1725 | auto Fields = cast<RecordDecl>(IdentQTy->getAsTagDecl())->field_begin(); |
| 1726 | LValue PSource = |
| 1727 | CGF.EmitLValueForField(Base, *std::next(Fields, IdentField_PSource)); |
| Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1728 | |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1729 | llvm::Value *OMPDebugLoc = OpenMPDebugLocMap.lookup(Loc.getRawEncoding()); |
| Alexey Bataev | f002aca | 2014-05-30 05:48:40 +0000 | [diff] [blame] | 1730 | if (OMPDebugLoc == nullptr) { |
| 1731 | SmallString<128> Buffer2; |
| 1732 | llvm::raw_svector_ostream OS2(Buffer2); |
| 1733 | // Build debug location |
| 1734 | PresumedLoc PLoc = CGF.getContext().getSourceManager().getPresumedLoc(Loc); |
| 1735 | OS2 << ";" << PLoc.getFilename() << ";"; |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1736 | if (const auto *FD = dyn_cast_or_null<FunctionDecl>(CGF.CurFuncDecl)) |
| Alexey Bataev | f002aca | 2014-05-30 05:48:40 +0000 | [diff] [blame] | 1737 | OS2 << FD->getQualifiedNameAsString(); |
| Alexey Bataev | f002aca | 2014-05-30 05:48:40 +0000 | [diff] [blame] | 1738 | OS2 << ";" << PLoc.getLine() << ";" << PLoc.getColumn() << ";;"; |
| 1739 | OMPDebugLoc = CGF.Builder.CreateGlobalStringPtr(OS2.str()); |
| 1740 | OpenMPDebugLocMap[Loc.getRawEncoding()] = OMPDebugLoc; |
| Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1741 | } |
| Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1742 | // *psource = ";<File>;<Function>;<Line>;<Column>;;"; |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1743 | CGF.EmitStoreOfScalar(OMPDebugLoc, PSource); |
| Alexey Bataev | f002aca | 2014-05-30 05:48:40 +0000 | [diff] [blame] | 1744 | |
| John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1745 | // Our callers always pass this to a runtime function, so for |
| 1746 | // convenience, go ahead and return a naked pointer. |
| 1747 | return LocValue.getPointer(); |
| Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1748 | } |
| 1749 | |
| Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 1750 | llvm::Value *CGOpenMPRuntime::getThreadID(CodeGenFunction &CGF, |
| 1751 | SourceLocation Loc) { |
| Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1752 | assert(CGF.CurFn && "No function in current CodeGenFunction."); |
| 1753 | |
| Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 1754 | llvm::Value *ThreadID = nullptr; |
| Alexey Bataev | 1809571 | 2014-10-10 12:19:54 +0000 | [diff] [blame] | 1755 | // Check whether we've already cached a load of the thread id in this |
| 1756 | // function. |
| Alexey Bataev | 1e4b713 | 2014-12-03 12:11:24 +0000 | [diff] [blame] | 1757 | auto I = OpenMPLocThreadIDMap.find(CGF.CurFn); |
| Alexey Bataev | 1809571 | 2014-10-10 12:19:54 +0000 | [diff] [blame] | 1758 | if (I != OpenMPLocThreadIDMap.end()) { |
| 1759 | ThreadID = I->second.ThreadID; |
| Alexey Bataev | 03b340a | 2014-10-21 03:16:40 +0000 | [diff] [blame] | 1760 | if (ThreadID != nullptr) |
| 1761 | return ThreadID; |
| 1762 | } |
| Alexey Bataev | aee1855 | 2017-08-16 14:01:00 +0000 | [diff] [blame] | 1763 | // If exceptions are enabled, do not use parameter to avoid possible crash. |
| Alexey Bataev | 780f555 | 2019-10-17 17:12:03 +0000 | [diff] [blame] | 1764 | if (auto *OMPRegionInfo = |
| 1765 | dyn_cast_or_null<CGOpenMPRegionInfo>(CGF.CapturedStmtInfo)) { |
| 1766 | if (OMPRegionInfo->getThreadIDVariable()) { |
| 1767 | // Check if this an outlined function with thread id passed as argument. |
| 1768 | LValue LVal = OMPRegionInfo->getThreadIDVariableLValue(CGF); |
| 1769 | llvm::BasicBlock *TopBlock = CGF.AllocaInsertPt->getParent(); |
| 1770 | if (!CGF.EHStack.requiresLandingPad() || !CGF.getLangOpts().Exceptions || |
| 1771 | !CGF.getLangOpts().CXXExceptions || |
| 1772 | CGF.Builder.GetInsertBlock() == TopBlock || |
| Akira Hatanaka | f139ae3 | 2019-12-03 15:17:01 -0800 | [diff] [blame] | 1773 | !isa<llvm::Instruction>(LVal.getPointer(CGF)) || |
| 1774 | cast<llvm::Instruction>(LVal.getPointer(CGF))->getParent() == |
| 1775 | TopBlock || |
| 1776 | cast<llvm::Instruction>(LVal.getPointer(CGF))->getParent() == |
| Alexey Bataev | 780f555 | 2019-10-17 17:12:03 +0000 | [diff] [blame] | 1777 | CGF.Builder.GetInsertBlock()) { |
| Alexey Bataev | 1e49137 | 2018-01-23 18:44:14 +0000 | [diff] [blame] | 1778 | ThreadID = CGF.EmitLoadOfScalar(LVal, Loc); |
| Alexey Bataev | aee1855 | 2017-08-16 14:01:00 +0000 | [diff] [blame] | 1779 | // If value loaded in entry block, cache it and use it everywhere in |
| 1780 | // function. |
| Alexey Bataev | 780f555 | 2019-10-17 17:12:03 +0000 | [diff] [blame] | 1781 | if (CGF.Builder.GetInsertBlock() == TopBlock) { |
| Alexey Bataev | aee1855 | 2017-08-16 14:01:00 +0000 | [diff] [blame] | 1782 | auto &Elem = OpenMPLocThreadIDMap.FindAndConstruct(CGF.CurFn); |
| 1783 | Elem.second.ThreadID = ThreadID; |
| 1784 | } |
| 1785 | return ThreadID; |
| Alexey Bataev | 8cbe0a6 | 2015-02-26 10:27:34 +0000 | [diff] [blame] | 1786 | } |
| Alexey Bataev | d6c5755 | 2014-07-25 07:55:17 +0000 | [diff] [blame] | 1787 | } |
| Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1788 | } |
| Alexey Bataev | 8cbe0a6 | 2015-02-26 10:27:34 +0000 | [diff] [blame] | 1789 | |
| 1790 | // This is not an outlined function region - need to call __kmpc_int32 |
| 1791 | // kmpc_global_thread_num(ident_t *loc). |
| 1792 | // Generate thread id value and cache this value for use across the |
| 1793 | // function. |
| Alexey Bataev | fd006c4 | 2018-10-05 15:08:53 +0000 | [diff] [blame] | 1794 | auto &Elem = OpenMPLocThreadIDMap.FindAndConstruct(CGF.CurFn); |
| 1795 | if (!Elem.second.ServiceInsertPt) |
| 1796 | setLocThreadIdInsertPt(CGF); |
| Alexey Bataev | 8cbe0a6 | 2015-02-26 10:27:34 +0000 | [diff] [blame] | 1797 | CGBuilderTy::InsertPointGuard IPG(CGF.Builder); |
| Alexey Bataev | fd006c4 | 2018-10-05 15:08:53 +0000 | [diff] [blame] | 1798 | CGF.Builder.SetInsertPoint(Elem.second.ServiceInsertPt); |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1799 | llvm::CallInst *Call = CGF.Builder.CreateCall( |
| Alexey Bataev | 0e1b458 | 2017-11-02 14:25:34 +0000 | [diff] [blame] | 1800 | createRuntimeFunction(OMPRTL__kmpc_global_thread_num), |
| 1801 | emitUpdateLocation(CGF, Loc)); |
| 1802 | Call->setCallingConv(CGF.getRuntimeCC()); |
| Alexey Bataev | 0e1b458 | 2017-11-02 14:25:34 +0000 | [diff] [blame] | 1803 | Elem.second.ThreadID = Call; |
| 1804 | return Call; |
| Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1805 | } |
| 1806 | |
| Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 1807 | void CGOpenMPRuntime::functionFinished(CodeGenFunction &CGF) { |
| Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1808 | assert(CGF.CurFn && "No function in current CodeGenFunction."); |
| Alexey Bataev | fd006c4 | 2018-10-05 15:08:53 +0000 | [diff] [blame] | 1809 | if (OpenMPLocThreadIDMap.count(CGF.CurFn)) { |
| 1810 | clearLocThreadIdInsertPt(CGF); |
| Alexey Bataev | 03b340a | 2014-10-21 03:16:40 +0000 | [diff] [blame] | 1811 | OpenMPLocThreadIDMap.erase(CGF.CurFn); |
| Alexey Bataev | fd006c4 | 2018-10-05 15:08:53 +0000 | [diff] [blame] | 1812 | } |
| Alexey Bataev | c5b1d32 | 2016-03-04 09:22:22 +0000 | [diff] [blame] | 1813 | if (FunctionUDRMap.count(CGF.CurFn) > 0) { |
| Alexey Bataev | 4697874 | 2020-01-30 10:46:11 -0500 | [diff] [blame] | 1814 | for(const auto *D : FunctionUDRMap[CGF.CurFn]) |
| Alexey Bataev | c5b1d32 | 2016-03-04 09:22:22 +0000 | [diff] [blame] | 1815 | UDRMap.erase(D); |
| Alexey Bataev | c5b1d32 | 2016-03-04 09:22:22 +0000 | [diff] [blame] | 1816 | FunctionUDRMap.erase(CGF.CurFn); |
| 1817 | } |
| Michael Kruse | d47b943 | 2019-08-05 18:43:21 +0000 | [diff] [blame] | 1818 | auto I = FunctionUDMMap.find(CGF.CurFn); |
| 1819 | if (I != FunctionUDMMap.end()) { |
| Alexey Bataev | 4697874 | 2020-01-30 10:46:11 -0500 | [diff] [blame] | 1820 | for(const auto *D : I->second) |
| Michael Kruse | d47b943 | 2019-08-05 18:43:21 +0000 | [diff] [blame] | 1821 | UDMMap.erase(D); |
| 1822 | FunctionUDMMap.erase(I); |
| 1823 | } |
| Alexey Bataev | 4697874 | 2020-01-30 10:46:11 -0500 | [diff] [blame] | 1824 | LastprivateConditionalToTypes.erase(CGF.CurFn); |
| Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1825 | } |
| 1826 | |
| 1827 | llvm::Type *CGOpenMPRuntime::getIdentTyPointerTy() { |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1828 | return IdentTy->getPointerTo(); |
| Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1829 | } |
| 1830 | |
| 1831 | llvm::Type *CGOpenMPRuntime::getKmpc_MicroPointerTy() { |
| Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 1832 | if (!Kmpc_MicroTy) { |
| 1833 | // Build void (*kmpc_micro)(kmp_int32 *global_tid, kmp_int32 *bound_tid,...) |
| 1834 | llvm::Type *MicroParams[] = {llvm::PointerType::getUnqual(CGM.Int32Ty), |
| 1835 | llvm::PointerType::getUnqual(CGM.Int32Ty)}; |
| 1836 | Kmpc_MicroTy = llvm::FunctionType::get(CGM.VoidTy, MicroParams, true); |
| 1837 | } |
| Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1838 | return llvm::PointerType::getUnqual(Kmpc_MicroTy); |
| 1839 | } |
| 1840 | |
| James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 1841 | llvm::FunctionCallee CGOpenMPRuntime::createRuntimeFunction(unsigned Function) { |
| 1842 | llvm::FunctionCallee RTLFn = nullptr; |
| Alexey Bataev | 50b3c95 | 2016-02-19 10:38:26 +0000 | [diff] [blame] | 1843 | switch (static_cast<OpenMPRTLFunction>(Function)) { |
| Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1844 | case OMPRTL__kmpc_fork_call: { |
| 1845 | // Build void __kmpc_fork_call(ident_t *loc, kmp_int32 argc, kmpc_micro |
| 1846 | // microtask, ...); |
| Alexey Bataev | 23b6942 | 2014-06-18 07:08:49 +0000 | [diff] [blame] | 1847 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty, |
| 1848 | getKmpc_MicroPointerTy()}; |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1849 | auto *FnTy = |
| Alexey Bataev | d74d060 | 2014-10-13 06:02:40 +0000 | [diff] [blame] | 1850 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ true); |
| Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1851 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_fork_call"); |
| James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 1852 | if (auto *F = dyn_cast<llvm::Function>(RTLFn.getCallee())) { |
| Johannes Doerfert | ac991bb | 2019-01-19 05:36:54 +0000 | [diff] [blame] | 1853 | if (!F->hasMetadata(llvm::LLVMContext::MD_callback)) { |
| 1854 | llvm::LLVMContext &Ctx = F->getContext(); |
| 1855 | llvm::MDBuilder MDB(Ctx); |
| 1856 | // Annotate the callback behavior of the __kmpc_fork_call: |
| 1857 | // - The callback callee is argument number 2 (microtask). |
| 1858 | // - The first two arguments of the callback callee are unknown (-1). |
| 1859 | // - All variadic arguments to the __kmpc_fork_call are passed to the |
| 1860 | // callback callee. |
| 1861 | F->addMetadata( |
| 1862 | llvm::LLVMContext::MD_callback, |
| 1863 | *llvm::MDNode::get(Ctx, {MDB.createCallbackEncoding( |
| 1864 | 2, {-1, -1}, |
| 1865 | /* VarArgsArePassed */ true)})); |
| 1866 | } |
| 1867 | } |
| Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1868 | break; |
| 1869 | } |
| 1870 | case OMPRTL__kmpc_global_thread_num: { |
| 1871 | // Build kmp_int32 __kmpc_global_thread_num(ident_t *loc); |
| Alexey Bataev | 23b6942 | 2014-06-18 07:08:49 +0000 | [diff] [blame] | 1872 | llvm::Type *TypeParams[] = {getIdentTyPointerTy()}; |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1873 | auto *FnTy = |
| Alexey Bataev | d74d060 | 2014-10-13 06:02:40 +0000 | [diff] [blame] | 1874 | llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg*/ false); |
| Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1875 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_global_thread_num"); |
| 1876 | break; |
| 1877 | } |
| Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 1878 | case OMPRTL__kmpc_threadprivate_cached: { |
| 1879 | // Build void *__kmpc_threadprivate_cached(ident_t *loc, |
| 1880 | // kmp_int32 global_tid, void *data, size_t size, void ***cache); |
| 1881 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty, |
| 1882 | CGM.VoidPtrTy, CGM.SizeTy, |
| 1883 | CGM.VoidPtrTy->getPointerTo()->getPointerTo()}; |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1884 | auto *FnTy = |
| Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 1885 | llvm::FunctionType::get(CGM.VoidPtrTy, TypeParams, /*isVarArg*/ false); |
| 1886 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_threadprivate_cached"); |
| 1887 | break; |
| 1888 | } |
| Alexey Bataev | 3a3bf0b | 2014-09-22 10:01:53 +0000 | [diff] [blame] | 1889 | case OMPRTL__kmpc_critical: { |
| Alexey Bataev | f947218 | 2014-09-22 12:32:31 +0000 | [diff] [blame] | 1890 | // Build void __kmpc_critical(ident_t *loc, kmp_int32 global_tid, |
| 1891 | // kmp_critical_name *crit); |
| Alexey Bataev | 3a3bf0b | 2014-09-22 10:01:53 +0000 | [diff] [blame] | 1892 | llvm::Type *TypeParams[] = { |
| 1893 | getIdentTyPointerTy(), CGM.Int32Ty, |
| 1894 | llvm::PointerType::getUnqual(KmpCriticalNameTy)}; |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1895 | auto *FnTy = |
| Alexey Bataev | 3a3bf0b | 2014-09-22 10:01:53 +0000 | [diff] [blame] | 1896 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false); |
| 1897 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_critical"); |
| 1898 | break; |
| 1899 | } |
| Alexey Bataev | fc57d16 | 2015-12-15 10:55:09 +0000 | [diff] [blame] | 1900 | case OMPRTL__kmpc_critical_with_hint: { |
| 1901 | // Build void __kmpc_critical_with_hint(ident_t *loc, kmp_int32 global_tid, |
| 1902 | // kmp_critical_name *crit, uintptr_t hint); |
| 1903 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty, |
| 1904 | llvm::PointerType::getUnqual(KmpCriticalNameTy), |
| 1905 | CGM.IntPtrTy}; |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1906 | auto *FnTy = |
| Alexey Bataev | fc57d16 | 2015-12-15 10:55:09 +0000 | [diff] [blame] | 1907 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false); |
| 1908 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_critical_with_hint"); |
| 1909 | break; |
| 1910 | } |
| Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 1911 | case OMPRTL__kmpc_threadprivate_register: { |
| 1912 | // Build void __kmpc_threadprivate_register(ident_t *, void *data, |
| 1913 | // kmpc_ctor ctor, kmpc_cctor cctor, kmpc_dtor dtor); |
| 1914 | // typedef void *(*kmpc_ctor)(void *); |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1915 | auto *KmpcCtorTy = |
| Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 1916 | llvm::FunctionType::get(CGM.VoidPtrTy, CGM.VoidPtrTy, |
| 1917 | /*isVarArg*/ false)->getPointerTo(); |
| 1918 | // typedef void *(*kmpc_cctor)(void *, void *); |
| 1919 | llvm::Type *KmpcCopyCtorTyArgs[] = {CGM.VoidPtrTy, CGM.VoidPtrTy}; |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1920 | auto *KmpcCopyCtorTy = |
| Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 1921 | llvm::FunctionType::get(CGM.VoidPtrTy, KmpcCopyCtorTyArgs, |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1922 | /*isVarArg*/ false) |
| 1923 | ->getPointerTo(); |
| Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 1924 | // typedef void (*kmpc_dtor)(void *); |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1925 | auto *KmpcDtorTy = |
| Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 1926 | llvm::FunctionType::get(CGM.VoidTy, CGM.VoidPtrTy, /*isVarArg*/ false) |
| 1927 | ->getPointerTo(); |
| 1928 | llvm::Type *FnTyArgs[] = {getIdentTyPointerTy(), CGM.VoidPtrTy, KmpcCtorTy, |
| 1929 | KmpcCopyCtorTy, KmpcDtorTy}; |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1930 | auto *FnTy = llvm::FunctionType::get(CGM.VoidTy, FnTyArgs, |
| Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 1931 | /*isVarArg*/ false); |
| 1932 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_threadprivate_register"); |
| 1933 | break; |
| 1934 | } |
| Alexey Bataev | 3a3bf0b | 2014-09-22 10:01:53 +0000 | [diff] [blame] | 1935 | case OMPRTL__kmpc_end_critical: { |
| Alexey Bataev | f947218 | 2014-09-22 12:32:31 +0000 | [diff] [blame] | 1936 | // Build void __kmpc_end_critical(ident_t *loc, kmp_int32 global_tid, |
| 1937 | // kmp_critical_name *crit); |
| Alexey Bataev | 3a3bf0b | 2014-09-22 10:01:53 +0000 | [diff] [blame] | 1938 | llvm::Type *TypeParams[] = { |
| 1939 | getIdentTyPointerTy(), CGM.Int32Ty, |
| 1940 | llvm::PointerType::getUnqual(KmpCriticalNameTy)}; |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1941 | auto *FnTy = |
| Alexey Bataev | 3a3bf0b | 2014-09-22 10:01:53 +0000 | [diff] [blame] | 1942 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false); |
| 1943 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_end_critical"); |
| 1944 | break; |
| 1945 | } |
| Alexey Bataev | 8f7c1b0 | 2014-12-05 04:09:23 +0000 | [diff] [blame] | 1946 | case OMPRTL__kmpc_cancel_barrier: { |
| 1947 | // Build kmp_int32 __kmpc_cancel_barrier(ident_t *loc, kmp_int32 |
| 1948 | // global_tid); |
| Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 1949 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty}; |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1950 | auto *FnTy = |
| Alexey Bataev | 8f7c1b0 | 2014-12-05 04:09:23 +0000 | [diff] [blame] | 1951 | llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg*/ false); |
| 1952 | RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name*/ "__kmpc_cancel_barrier"); |
| Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 1953 | break; |
| 1954 | } |
| Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 1955 | case OMPRTL__kmpc_barrier: { |
| Alexey Bataev | 7d5d33e | 2015-07-06 05:50:32 +0000 | [diff] [blame] | 1956 | // Build void __kmpc_barrier(ident_t *loc, kmp_int32 global_tid); |
| Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 1957 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty}; |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1958 | auto *FnTy = |
| Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 1959 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false); |
| 1960 | RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name*/ "__kmpc_barrier"); |
| 1961 | break; |
| 1962 | } |
| Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 1963 | case OMPRTL__kmpc_for_static_fini: { |
| 1964 | // Build void __kmpc_for_static_fini(ident_t *loc, kmp_int32 global_tid); |
| 1965 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty}; |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1966 | auto *FnTy = |
| Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 1967 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false); |
| 1968 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_for_static_fini"); |
| 1969 | break; |
| 1970 | } |
| Alexey Bataev | b205978 | 2014-10-13 08:23:51 +0000 | [diff] [blame] | 1971 | case OMPRTL__kmpc_push_num_threads: { |
| 1972 | // Build void __kmpc_push_num_threads(ident_t *loc, kmp_int32 global_tid, |
| 1973 | // kmp_int32 num_threads) |
| 1974 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty, |
| 1975 | CGM.Int32Ty}; |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1976 | auto *FnTy = |
| Alexey Bataev | b205978 | 2014-10-13 08:23:51 +0000 | [diff] [blame] | 1977 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false); |
| 1978 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_push_num_threads"); |
| 1979 | break; |
| 1980 | } |
| Alexey Bataev | d74d060 | 2014-10-13 06:02:40 +0000 | [diff] [blame] | 1981 | case OMPRTL__kmpc_serialized_parallel: { |
| 1982 | // Build void __kmpc_serialized_parallel(ident_t *loc, kmp_int32 |
| 1983 | // global_tid); |
| 1984 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty}; |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1985 | auto *FnTy = |
| Alexey Bataev | d74d060 | 2014-10-13 06:02:40 +0000 | [diff] [blame] | 1986 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false); |
| 1987 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_serialized_parallel"); |
| 1988 | break; |
| 1989 | } |
| 1990 | case OMPRTL__kmpc_end_serialized_parallel: { |
| 1991 | // Build void __kmpc_end_serialized_parallel(ident_t *loc, kmp_int32 |
| 1992 | // global_tid); |
| 1993 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty}; |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1994 | auto *FnTy = |
| Alexey Bataev | d74d060 | 2014-10-13 06:02:40 +0000 | [diff] [blame] | 1995 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false); |
| 1996 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_end_serialized_parallel"); |
| 1997 | break; |
| 1998 | } |
| Alexey Bataev | cc37cc1 | 2014-11-20 04:34:54 +0000 | [diff] [blame] | 1999 | case OMPRTL__kmpc_flush: { |
| Alexey Bataev | d76df6d | 2015-02-24 12:55:09 +0000 | [diff] [blame] | 2000 | // Build void __kmpc_flush(ident_t *loc); |
| Alexey Bataev | cc37cc1 | 2014-11-20 04:34:54 +0000 | [diff] [blame] | 2001 | llvm::Type *TypeParams[] = {getIdentTyPointerTy()}; |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2002 | auto *FnTy = |
| Alexey Bataev | d76df6d | 2015-02-24 12:55:09 +0000 | [diff] [blame] | 2003 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false); |
| Alexey Bataev | cc37cc1 | 2014-11-20 04:34:54 +0000 | [diff] [blame] | 2004 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_flush"); |
| 2005 | break; |
| 2006 | } |
| Alexey Bataev | 8d69065 | 2014-12-04 07:23:53 +0000 | [diff] [blame] | 2007 | case OMPRTL__kmpc_master: { |
| 2008 | // Build kmp_int32 __kmpc_master(ident_t *loc, kmp_int32 global_tid); |
| 2009 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty}; |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2010 | auto *FnTy = |
| Alexey Bataev | 8d69065 | 2014-12-04 07:23:53 +0000 | [diff] [blame] | 2011 | llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg=*/false); |
| 2012 | RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_master"); |
| 2013 | break; |
| 2014 | } |
| 2015 | case OMPRTL__kmpc_end_master: { |
| 2016 | // Build void __kmpc_end_master(ident_t *loc, kmp_int32 global_tid); |
| 2017 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty}; |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2018 | auto *FnTy = |
| Alexey Bataev | 8d69065 | 2014-12-04 07:23:53 +0000 | [diff] [blame] | 2019 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false); |
| 2020 | RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_end_master"); |
| 2021 | break; |
| 2022 | } |
| Alexey Bataev | 9f797f3 | 2015-02-05 05:57:51 +0000 | [diff] [blame] | 2023 | case OMPRTL__kmpc_omp_taskyield: { |
| 2024 | // Build kmp_int32 __kmpc_omp_taskyield(ident_t *, kmp_int32 global_tid, |
| 2025 | // int end_part); |
| 2026 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty, CGM.IntTy}; |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2027 | auto *FnTy = |
| Alexey Bataev | 9f797f3 | 2015-02-05 05:57:51 +0000 | [diff] [blame] | 2028 | llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg=*/false); |
| 2029 | RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_omp_taskyield"); |
| 2030 | break; |
| 2031 | } |
| Alexey Bataev | 6956e2e | 2015-02-05 06:35:41 +0000 | [diff] [blame] | 2032 | case OMPRTL__kmpc_single: { |
| 2033 | // Build kmp_int32 __kmpc_single(ident_t *loc, kmp_int32 global_tid); |
| 2034 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty}; |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2035 | auto *FnTy = |
| Alexey Bataev | 6956e2e | 2015-02-05 06:35:41 +0000 | [diff] [blame] | 2036 | llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg=*/false); |
| 2037 | RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_single"); |
| 2038 | break; |
| 2039 | } |
| 2040 | case OMPRTL__kmpc_end_single: { |
| 2041 | // Build void __kmpc_end_single(ident_t *loc, kmp_int32 global_tid); |
| 2042 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty}; |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2043 | auto *FnTy = |
| Alexey Bataev | 6956e2e | 2015-02-05 06:35:41 +0000 | [diff] [blame] | 2044 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false); |
| 2045 | RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_end_single"); |
| 2046 | break; |
| 2047 | } |
| Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 2048 | case OMPRTL__kmpc_omp_task_alloc: { |
| 2049 | // Build kmp_task_t *__kmpc_omp_task_alloc(ident_t *, kmp_int32 gtid, |
| 2050 | // kmp_int32 flags, size_t sizeof_kmp_task_t, size_t sizeof_shareds, |
| 2051 | // kmp_routine_entry_t *task_entry); |
| 2052 | assert(KmpRoutineEntryPtrTy != nullptr && |
| 2053 | "Type kmp_routine_entry_t must be created."); |
| 2054 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty, CGM.Int32Ty, |
| 2055 | CGM.SizeTy, CGM.SizeTy, KmpRoutineEntryPtrTy}; |
| 2056 | // Return void * and then cast to particular kmp_task_t type. |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2057 | auto *FnTy = |
| Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 2058 | llvm::FunctionType::get(CGM.VoidPtrTy, TypeParams, /*isVarArg=*/false); |
| 2059 | RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_omp_task_alloc"); |
| 2060 | break; |
| 2061 | } |
| Gheorghe-Teodor Bercea | 545a9fe | 2019-06-14 20:19:54 +0000 | [diff] [blame] | 2062 | case OMPRTL__kmpc_omp_target_task_alloc: { |
| 2063 | // Build kmp_task_t *__kmpc_omp_target_task_alloc(ident_t *, kmp_int32 gtid, |
| 2064 | // kmp_int32 flags, size_t sizeof_kmp_task_t, size_t sizeof_shareds, |
| 2065 | // kmp_routine_entry_t *task_entry, kmp_int64 device_id); |
| 2066 | assert(KmpRoutineEntryPtrTy != nullptr && |
| 2067 | "Type kmp_routine_entry_t must be created."); |
| 2068 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty, CGM.Int32Ty, |
| 2069 | CGM.SizeTy, CGM.SizeTy, KmpRoutineEntryPtrTy, |
| 2070 | CGM.Int64Ty}; |
| 2071 | // Return void * and then cast to particular kmp_task_t type. |
| 2072 | auto *FnTy = |
| 2073 | llvm::FunctionType::get(CGM.VoidPtrTy, TypeParams, /*isVarArg=*/false); |
| 2074 | RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_omp_target_task_alloc"); |
| 2075 | break; |
| 2076 | } |
| Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 2077 | case OMPRTL__kmpc_omp_task: { |
| 2078 | // Build kmp_int32 __kmpc_omp_task(ident_t *, kmp_int32 gtid, kmp_task_t |
| 2079 | // *new_task); |
| 2080 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty, |
| 2081 | CGM.VoidPtrTy}; |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2082 | auto *FnTy = |
| Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 2083 | llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg=*/false); |
| 2084 | RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_omp_task"); |
| 2085 | break; |
| 2086 | } |
| Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 2087 | case OMPRTL__kmpc_copyprivate: { |
| 2088 | // Build void __kmpc_copyprivate(ident_t *loc, kmp_int32 global_tid, |
| Alexey Bataev | 66beaa9 | 2015-04-30 03:47:32 +0000 | [diff] [blame] | 2089 | // size_t cpy_size, void *cpy_data, void(*cpy_func)(void *, void *), |
| Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 2090 | // kmp_int32 didit); |
| 2091 | llvm::Type *CpyTypeParams[] = {CGM.VoidPtrTy, CGM.VoidPtrTy}; |
| 2092 | auto *CpyFnTy = |
| 2093 | llvm::FunctionType::get(CGM.VoidTy, CpyTypeParams, /*isVarArg=*/false); |
| Alexey Bataev | 66beaa9 | 2015-04-30 03:47:32 +0000 | [diff] [blame] | 2094 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty, CGM.SizeTy, |
| Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 2095 | CGM.VoidPtrTy, CpyFnTy->getPointerTo(), |
| 2096 | CGM.Int32Ty}; |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2097 | auto *FnTy = |
| Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 2098 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false); |
| 2099 | RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_copyprivate"); |
| 2100 | break; |
| 2101 | } |
| Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 2102 | case OMPRTL__kmpc_reduce: { |
| 2103 | // Build kmp_int32 __kmpc_reduce(ident_t *loc, kmp_int32 global_tid, |
| 2104 | // kmp_int32 num_vars, size_t reduce_size, void *reduce_data, void |
| 2105 | // (*reduce_func)(void *lhs_data, void *rhs_data), kmp_critical_name *lck); |
| 2106 | llvm::Type *ReduceTypeParams[] = {CGM.VoidPtrTy, CGM.VoidPtrTy}; |
| 2107 | auto *ReduceFnTy = llvm::FunctionType::get(CGM.VoidTy, ReduceTypeParams, |
| 2108 | /*isVarArg=*/false); |
| 2109 | llvm::Type *TypeParams[] = { |
| 2110 | getIdentTyPointerTy(), CGM.Int32Ty, CGM.Int32Ty, CGM.SizeTy, |
| 2111 | CGM.VoidPtrTy, ReduceFnTy->getPointerTo(), |
| 2112 | llvm::PointerType::getUnqual(KmpCriticalNameTy)}; |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2113 | auto *FnTy = |
| Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 2114 | llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg=*/false); |
| 2115 | RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_reduce"); |
| 2116 | break; |
| 2117 | } |
| 2118 | case OMPRTL__kmpc_reduce_nowait: { |
| 2119 | // Build kmp_int32 __kmpc_reduce_nowait(ident_t *loc, kmp_int32 |
| 2120 | // global_tid, kmp_int32 num_vars, size_t reduce_size, void *reduce_data, |
| 2121 | // void (*reduce_func)(void *lhs_data, void *rhs_data), kmp_critical_name |
| 2122 | // *lck); |
| 2123 | llvm::Type *ReduceTypeParams[] = {CGM.VoidPtrTy, CGM.VoidPtrTy}; |
| 2124 | auto *ReduceFnTy = llvm::FunctionType::get(CGM.VoidTy, ReduceTypeParams, |
| 2125 | /*isVarArg=*/false); |
| 2126 | llvm::Type *TypeParams[] = { |
| 2127 | getIdentTyPointerTy(), CGM.Int32Ty, CGM.Int32Ty, CGM.SizeTy, |
| 2128 | CGM.VoidPtrTy, ReduceFnTy->getPointerTo(), |
| 2129 | llvm::PointerType::getUnqual(KmpCriticalNameTy)}; |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2130 | auto *FnTy = |
| Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 2131 | llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg=*/false); |
| 2132 | RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_reduce_nowait"); |
| 2133 | break; |
| 2134 | } |
| 2135 | case OMPRTL__kmpc_end_reduce: { |
| 2136 | // Build void __kmpc_end_reduce(ident_t *loc, kmp_int32 global_tid, |
| 2137 | // kmp_critical_name *lck); |
| 2138 | llvm::Type *TypeParams[] = { |
| 2139 | getIdentTyPointerTy(), CGM.Int32Ty, |
| 2140 | llvm::PointerType::getUnqual(KmpCriticalNameTy)}; |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2141 | auto *FnTy = |
| Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 2142 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false); |
| 2143 | RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_end_reduce"); |
| 2144 | break; |
| 2145 | } |
| 2146 | case OMPRTL__kmpc_end_reduce_nowait: { |
| 2147 | // Build __kmpc_end_reduce_nowait(ident_t *loc, kmp_int32 global_tid, |
| 2148 | // kmp_critical_name *lck); |
| 2149 | llvm::Type *TypeParams[] = { |
| 2150 | getIdentTyPointerTy(), CGM.Int32Ty, |
| 2151 | llvm::PointerType::getUnqual(KmpCriticalNameTy)}; |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2152 | auto *FnTy = |
| Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 2153 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false); |
| 2154 | RTLFn = |
| 2155 | CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_end_reduce_nowait"); |
| 2156 | break; |
| 2157 | } |
| Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 2158 | case OMPRTL__kmpc_omp_task_begin_if0: { |
| 2159 | // Build void __kmpc_omp_task(ident_t *, kmp_int32 gtid, kmp_task_t |
| 2160 | // *new_task); |
| 2161 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty, |
| 2162 | CGM.VoidPtrTy}; |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2163 | auto *FnTy = |
| Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 2164 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false); |
| 2165 | RTLFn = |
| 2166 | CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_omp_task_begin_if0"); |
| 2167 | break; |
| 2168 | } |
| 2169 | case OMPRTL__kmpc_omp_task_complete_if0: { |
| 2170 | // Build void __kmpc_omp_task(ident_t *, kmp_int32 gtid, kmp_task_t |
| 2171 | // *new_task); |
| 2172 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty, |
| 2173 | CGM.VoidPtrTy}; |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2174 | auto *FnTy = |
| Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 2175 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false); |
| 2176 | RTLFn = CGM.CreateRuntimeFunction(FnTy, |
| 2177 | /*Name=*/"__kmpc_omp_task_complete_if0"); |
| 2178 | break; |
| 2179 | } |
| Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 2180 | case OMPRTL__kmpc_ordered: { |
| 2181 | // Build void __kmpc_ordered(ident_t *loc, kmp_int32 global_tid); |
| 2182 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty}; |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2183 | auto *FnTy = |
| Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 2184 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false); |
| 2185 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_ordered"); |
| 2186 | break; |
| 2187 | } |
| 2188 | case OMPRTL__kmpc_end_ordered: { |
| Alexey Bataev | c30dd2d | 2015-06-18 12:14:09 +0000 | [diff] [blame] | 2189 | // Build void __kmpc_end_ordered(ident_t *loc, kmp_int32 global_tid); |
| Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 2190 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty}; |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2191 | auto *FnTy = |
| Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 2192 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false); |
| 2193 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_end_ordered"); |
| 2194 | break; |
| 2195 | } |
| Alexey Bataev | 8b8e202 | 2015-04-27 05:22:09 +0000 | [diff] [blame] | 2196 | case OMPRTL__kmpc_omp_taskwait: { |
| 2197 | // Build kmp_int32 __kmpc_omp_taskwait(ident_t *loc, kmp_int32 global_tid); |
| 2198 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty}; |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2199 | auto *FnTy = |
| Alexey Bataev | 8b8e202 | 2015-04-27 05:22:09 +0000 | [diff] [blame] | 2200 | llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg=*/false); |
| 2201 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_omp_taskwait"); |
| 2202 | break; |
| 2203 | } |
| Alexey Bataev | c30dd2d | 2015-06-18 12:14:09 +0000 | [diff] [blame] | 2204 | case OMPRTL__kmpc_taskgroup: { |
| 2205 | // Build void __kmpc_taskgroup(ident_t *loc, kmp_int32 global_tid); |
| 2206 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty}; |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2207 | auto *FnTy = |
| Alexey Bataev | c30dd2d | 2015-06-18 12:14:09 +0000 | [diff] [blame] | 2208 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false); |
| 2209 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_taskgroup"); |
| 2210 | break; |
| 2211 | } |
| 2212 | case OMPRTL__kmpc_end_taskgroup: { |
| 2213 | // Build void __kmpc_end_taskgroup(ident_t *loc, kmp_int32 global_tid); |
| 2214 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty}; |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2215 | auto *FnTy = |
| Alexey Bataev | c30dd2d | 2015-06-18 12:14:09 +0000 | [diff] [blame] | 2216 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false); |
| 2217 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_end_taskgroup"); |
| 2218 | break; |
| 2219 | } |
| Alexey Bataev | 7f210c6 | 2015-06-18 13:40:03 +0000 | [diff] [blame] | 2220 | case OMPRTL__kmpc_push_proc_bind: { |
| 2221 | // Build void __kmpc_push_proc_bind(ident_t *loc, kmp_int32 global_tid, |
| 2222 | // int proc_bind) |
| 2223 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty, CGM.IntTy}; |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2224 | auto *FnTy = |
| Alexey Bataev | 7f210c6 | 2015-06-18 13:40:03 +0000 | [diff] [blame] | 2225 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false); |
| 2226 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_push_proc_bind"); |
| 2227 | break; |
| 2228 | } |
| Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 2229 | case OMPRTL__kmpc_omp_task_with_deps: { |
| 2230 | // Build kmp_int32 __kmpc_omp_task_with_deps(ident_t *, kmp_int32 gtid, |
| 2231 | // kmp_task_t *new_task, kmp_int32 ndeps, kmp_depend_info_t *dep_list, |
| 2232 | // kmp_int32 ndeps_noalias, kmp_depend_info_t *noalias_dep_list); |
| 2233 | llvm::Type *TypeParams[] = { |
| 2234 | getIdentTyPointerTy(), CGM.Int32Ty, CGM.VoidPtrTy, CGM.Int32Ty, |
| 2235 | CGM.VoidPtrTy, CGM.Int32Ty, CGM.VoidPtrTy}; |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2236 | auto *FnTy = |
| Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 2237 | llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg=*/false); |
| 2238 | RTLFn = |
| 2239 | CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_omp_task_with_deps"); |
| 2240 | break; |
| 2241 | } |
| 2242 | case OMPRTL__kmpc_omp_wait_deps: { |
| 2243 | // Build void __kmpc_omp_wait_deps(ident_t *, kmp_int32 gtid, |
| 2244 | // kmp_int32 ndeps, kmp_depend_info_t *dep_list, kmp_int32 ndeps_noalias, |
| 2245 | // kmp_depend_info_t *noalias_dep_list); |
| 2246 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty, |
| 2247 | CGM.Int32Ty, CGM.VoidPtrTy, |
| 2248 | CGM.Int32Ty, CGM.VoidPtrTy}; |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2249 | auto *FnTy = |
| Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 2250 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false); |
| 2251 | RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_omp_wait_deps"); |
| 2252 | break; |
| 2253 | } |
| Alexey Bataev | 0f34da1 | 2015-07-02 04:17:07 +0000 | [diff] [blame] | 2254 | case OMPRTL__kmpc_cancellationpoint: { |
| 2255 | // Build kmp_int32 __kmpc_cancellationpoint(ident_t *loc, kmp_int32 |
| 2256 | // global_tid, kmp_int32 cncl_kind) |
| 2257 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty, CGM.IntTy}; |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2258 | auto *FnTy = |
| Alexey Bataev | 0f34da1 | 2015-07-02 04:17:07 +0000 | [diff] [blame] | 2259 | llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg*/ false); |
| 2260 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_cancellationpoint"); |
| 2261 | break; |
| 2262 | } |
| Alexey Bataev | 7d5d33e | 2015-07-06 05:50:32 +0000 | [diff] [blame] | 2263 | case OMPRTL__kmpc_cancel: { |
| 2264 | // Build kmp_int32 __kmpc_cancel(ident_t *loc, kmp_int32 global_tid, |
| 2265 | // kmp_int32 cncl_kind) |
| 2266 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty, CGM.IntTy}; |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2267 | auto *FnTy = |
| Alexey Bataev | 7d5d33e | 2015-07-06 05:50:32 +0000 | [diff] [blame] | 2268 | llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg*/ false); |
| 2269 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_cancel"); |
| 2270 | break; |
| 2271 | } |
| Carlo Bertolli | 430d8ec | 2016-03-03 20:34:23 +0000 | [diff] [blame] | 2272 | case OMPRTL__kmpc_push_num_teams: { |
| 2273 | // Build void kmpc_push_num_teams (ident_t loc, kmp_int32 global_tid, |
| 2274 | // kmp_int32 num_teams, kmp_int32 num_threads) |
| 2275 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty, CGM.Int32Ty, |
| 2276 | CGM.Int32Ty}; |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2277 | auto *FnTy = |
| Carlo Bertolli | 430d8ec | 2016-03-03 20:34:23 +0000 | [diff] [blame] | 2278 | llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg*/ false); |
| 2279 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_push_num_teams"); |
| 2280 | break; |
| 2281 | } |
| 2282 | case OMPRTL__kmpc_fork_teams: { |
| 2283 | // Build void __kmpc_fork_teams(ident_t *loc, kmp_int32 argc, kmpc_micro |
| 2284 | // microtask, ...); |
| 2285 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty, |
| 2286 | getKmpc_MicroPointerTy()}; |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2287 | auto *FnTy = |
| Carlo Bertolli | 430d8ec | 2016-03-03 20:34:23 +0000 | [diff] [blame] | 2288 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ true); |
| 2289 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_fork_teams"); |
| James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 2290 | if (auto *F = dyn_cast<llvm::Function>(RTLFn.getCallee())) { |
| Johannes Doerfert | ac991bb | 2019-01-19 05:36:54 +0000 | [diff] [blame] | 2291 | if (!F->hasMetadata(llvm::LLVMContext::MD_callback)) { |
| 2292 | llvm::LLVMContext &Ctx = F->getContext(); |
| 2293 | llvm::MDBuilder MDB(Ctx); |
| 2294 | // Annotate the callback behavior of the __kmpc_fork_teams: |
| 2295 | // - The callback callee is argument number 2 (microtask). |
| 2296 | // - The first two arguments of the callback callee are unknown (-1). |
| 2297 | // - All variadic arguments to the __kmpc_fork_teams are passed to the |
| 2298 | // callback callee. |
| 2299 | F->addMetadata( |
| 2300 | llvm::LLVMContext::MD_callback, |
| 2301 | *llvm::MDNode::get(Ctx, {MDB.createCallbackEncoding( |
| 2302 | 2, {-1, -1}, |
| 2303 | /* VarArgsArePassed */ true)})); |
| 2304 | } |
| 2305 | } |
| Carlo Bertolli | 430d8ec | 2016-03-03 20:34:23 +0000 | [diff] [blame] | 2306 | break; |
| 2307 | } |
| Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 2308 | case OMPRTL__kmpc_taskloop: { |
| 2309 | // Build void __kmpc_taskloop(ident_t *loc, int gtid, kmp_task_t *task, int |
| 2310 | // if_val, kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st, int nogroup, int |
| 2311 | // sched, kmp_uint64 grainsize, void *task_dup); |
| 2312 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), |
| 2313 | CGM.IntTy, |
| 2314 | CGM.VoidPtrTy, |
| 2315 | CGM.IntTy, |
| 2316 | CGM.Int64Ty->getPointerTo(), |
| 2317 | CGM.Int64Ty->getPointerTo(), |
| 2318 | CGM.Int64Ty, |
| 2319 | CGM.IntTy, |
| 2320 | CGM.IntTy, |
| 2321 | CGM.Int64Ty, |
| 2322 | CGM.VoidPtrTy}; |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2323 | auto *FnTy = |
| Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 2324 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false); |
| 2325 | RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_taskloop"); |
| 2326 | break; |
| 2327 | } |
| Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 2328 | case OMPRTL__kmpc_doacross_init: { |
| 2329 | // Build void __kmpc_doacross_init(ident_t *loc, kmp_int32 gtid, kmp_int32 |
| 2330 | // num_dims, struct kmp_dim *dims); |
| 2331 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), |
| 2332 | CGM.Int32Ty, |
| 2333 | CGM.Int32Ty, |
| 2334 | CGM.VoidPtrTy}; |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2335 | auto *FnTy = |
| Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 2336 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false); |
| 2337 | RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_doacross_init"); |
| 2338 | break; |
| 2339 | } |
| 2340 | case OMPRTL__kmpc_doacross_fini: { |
| 2341 | // Build void __kmpc_doacross_fini(ident_t *loc, kmp_int32 gtid); |
| 2342 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty}; |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2343 | auto *FnTy = |
| Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 2344 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false); |
| 2345 | RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_doacross_fini"); |
| 2346 | break; |
| 2347 | } |
| 2348 | case OMPRTL__kmpc_doacross_post: { |
| 2349 | // Build void __kmpc_doacross_post(ident_t *loc, kmp_int32 gtid, kmp_int64 |
| 2350 | // *vec); |
| 2351 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty, |
| 2352 | CGM.Int64Ty->getPointerTo()}; |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2353 | auto *FnTy = |
| Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 2354 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false); |
| 2355 | RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_doacross_post"); |
| 2356 | break; |
| 2357 | } |
| 2358 | case OMPRTL__kmpc_doacross_wait: { |
| 2359 | // Build void __kmpc_doacross_wait(ident_t *loc, kmp_int32 gtid, kmp_int64 |
| 2360 | // *vec); |
| 2361 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty, |
| 2362 | CGM.Int64Ty->getPointerTo()}; |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2363 | auto *FnTy = |
| Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 2364 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false); |
| 2365 | RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_doacross_wait"); |
| 2366 | break; |
| 2367 | } |
| Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 2368 | case OMPRTL__kmpc_task_reduction_init: { |
| 2369 | // Build void *__kmpc_task_reduction_init(int gtid, int num_data, void |
| 2370 | // *data); |
| 2371 | llvm::Type *TypeParams[] = {CGM.IntTy, CGM.IntTy, CGM.VoidPtrTy}; |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2372 | auto *FnTy = |
| Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 2373 | llvm::FunctionType::get(CGM.VoidPtrTy, TypeParams, /*isVarArg=*/false); |
| 2374 | RTLFn = |
| 2375 | CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_task_reduction_init"); |
| 2376 | break; |
| 2377 | } |
| 2378 | case OMPRTL__kmpc_task_reduction_get_th_data: { |
| 2379 | // Build void *__kmpc_task_reduction_get_th_data(int gtid, void *tg, void |
| 2380 | // *d); |
| 2381 | llvm::Type *TypeParams[] = {CGM.IntTy, CGM.VoidPtrTy, CGM.VoidPtrTy}; |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2382 | auto *FnTy = |
| Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 2383 | llvm::FunctionType::get(CGM.VoidPtrTy, TypeParams, /*isVarArg=*/false); |
| 2384 | RTLFn = CGM.CreateRuntimeFunction( |
| 2385 | FnTy, /*Name=*/"__kmpc_task_reduction_get_th_data"); |
| 2386 | break; |
| 2387 | } |
| Alexey Bataev | 4f680db | 2019-03-19 16:41:16 +0000 | [diff] [blame] | 2388 | case OMPRTL__kmpc_alloc: { |
| Alexey Bataev | 6cf7b71 | 2019-04-08 19:06:42 +0000 | [diff] [blame] | 2389 | // Build to void *__kmpc_alloc(int gtid, size_t sz, omp_allocator_handle_t |
| 2390 | // al); omp_allocator_handle_t type is void *. |
| 2391 | llvm::Type *TypeParams[] = {CGM.IntTy, CGM.SizeTy, CGM.VoidPtrTy}; |
| Alexey Bataev | 4f680db | 2019-03-19 16:41:16 +0000 | [diff] [blame] | 2392 | auto *FnTy = |
| 2393 | llvm::FunctionType::get(CGM.VoidPtrTy, TypeParams, /*isVarArg=*/false); |
| 2394 | RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_alloc"); |
| 2395 | break; |
| 2396 | } |
| 2397 | case OMPRTL__kmpc_free: { |
| Alexey Bataev | 6cf7b71 | 2019-04-08 19:06:42 +0000 | [diff] [blame] | 2398 | // Build to void __kmpc_free(int gtid, void *ptr, omp_allocator_handle_t |
| 2399 | // al); omp_allocator_handle_t type is void *. |
| 2400 | llvm::Type *TypeParams[] = {CGM.IntTy, CGM.VoidPtrTy, CGM.VoidPtrTy}; |
| Alexey Bataev | 4f680db | 2019-03-19 16:41:16 +0000 | [diff] [blame] | 2401 | auto *FnTy = |
| 2402 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false); |
| 2403 | RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_free"); |
| 2404 | break; |
| 2405 | } |
| Alexey Bataev | 7bb3353 | 2019-01-07 21:30:43 +0000 | [diff] [blame] | 2406 | case OMPRTL__kmpc_push_target_tripcount: { |
| 2407 | // Build void __kmpc_push_target_tripcount(int64_t device_id, kmp_uint64 |
| 2408 | // size); |
| 2409 | llvm::Type *TypeParams[] = {CGM.Int64Ty, CGM.Int64Ty}; |
| 2410 | llvm::FunctionType *FnTy = |
| 2411 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false); |
| 2412 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_push_target_tripcount"); |
| 2413 | break; |
| 2414 | } |
| Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 2415 | case OMPRTL__tgt_target: { |
| George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 2416 | // Build int32_t __tgt_target(int64_t device_id, void *host_ptr, int32_t |
| Alexey Bataev | a90fc66 | 2019-06-25 16:00:43 +0000 | [diff] [blame] | 2417 | // arg_num, void** args_base, void **args, int64_t *arg_sizes, int64_t |
| Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 2418 | // *arg_types); |
| George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 2419 | llvm::Type *TypeParams[] = {CGM.Int64Ty, |
| Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 2420 | CGM.VoidPtrTy, |
| 2421 | CGM.Int32Ty, |
| 2422 | CGM.VoidPtrPtrTy, |
| 2423 | CGM.VoidPtrPtrTy, |
| Alexey Bataev | a90fc66 | 2019-06-25 16:00:43 +0000 | [diff] [blame] | 2424 | CGM.Int64Ty->getPointerTo(), |
| George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 2425 | CGM.Int64Ty->getPointerTo()}; |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2426 | auto *FnTy = |
| Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 2427 | llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg*/ false); |
| 2428 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__tgt_target"); |
| 2429 | break; |
| 2430 | } |
| Alexey Bataev | a9f77c6 | 2017-12-13 21:04:20 +0000 | [diff] [blame] | 2431 | case OMPRTL__tgt_target_nowait: { |
| 2432 | // Build int32_t __tgt_target_nowait(int64_t device_id, void *host_ptr, |
| Alexey Bataev | a90fc66 | 2019-06-25 16:00:43 +0000 | [diff] [blame] | 2433 | // int32_t arg_num, void** args_base, void **args, int64_t *arg_sizes, |
| Alexey Bataev | a9f77c6 | 2017-12-13 21:04:20 +0000 | [diff] [blame] | 2434 | // int64_t *arg_types); |
| 2435 | llvm::Type *TypeParams[] = {CGM.Int64Ty, |
| 2436 | CGM.VoidPtrTy, |
| 2437 | CGM.Int32Ty, |
| 2438 | CGM.VoidPtrPtrTy, |
| 2439 | CGM.VoidPtrPtrTy, |
| Alexey Bataev | a90fc66 | 2019-06-25 16:00:43 +0000 | [diff] [blame] | 2440 | CGM.Int64Ty->getPointerTo(), |
| Alexey Bataev | a9f77c6 | 2017-12-13 21:04:20 +0000 | [diff] [blame] | 2441 | CGM.Int64Ty->getPointerTo()}; |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2442 | auto *FnTy = |
| Alexey Bataev | a9f77c6 | 2017-12-13 21:04:20 +0000 | [diff] [blame] | 2443 | llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg*/ false); |
| 2444 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__tgt_target_nowait"); |
| 2445 | break; |
| 2446 | } |
| Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 2447 | case OMPRTL__tgt_target_teams: { |
| George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 2448 | // Build int32_t __tgt_target_teams(int64_t device_id, void *host_ptr, |
| Alexey Bataev | a90fc66 | 2019-06-25 16:00:43 +0000 | [diff] [blame] | 2449 | // int32_t arg_num, void** args_base, void **args, int64_t *arg_sizes, |
| George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 2450 | // int64_t *arg_types, int32_t num_teams, int32_t thread_limit); |
| 2451 | llvm::Type *TypeParams[] = {CGM.Int64Ty, |
| Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 2452 | CGM.VoidPtrTy, |
| 2453 | CGM.Int32Ty, |
| 2454 | CGM.VoidPtrPtrTy, |
| 2455 | CGM.VoidPtrPtrTy, |
| Alexey Bataev | a90fc66 | 2019-06-25 16:00:43 +0000 | [diff] [blame] | 2456 | CGM.Int64Ty->getPointerTo(), |
| George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 2457 | CGM.Int64Ty->getPointerTo(), |
| Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 2458 | CGM.Int32Ty, |
| 2459 | CGM.Int32Ty}; |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2460 | auto *FnTy = |
| Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 2461 | llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg*/ false); |
| 2462 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__tgt_target_teams"); |
| 2463 | break; |
| 2464 | } |
| Alexey Bataev | a9f77c6 | 2017-12-13 21:04:20 +0000 | [diff] [blame] | 2465 | case OMPRTL__tgt_target_teams_nowait: { |
| 2466 | // Build int32_t __tgt_target_teams_nowait(int64_t device_id, void |
| Alexey Bataev | a90fc66 | 2019-06-25 16:00:43 +0000 | [diff] [blame] | 2467 | // *host_ptr, int32_t arg_num, void** args_base, void **args, int64_t |
| Alexey Bataev | a9f77c6 | 2017-12-13 21:04:20 +0000 | [diff] [blame] | 2468 | // *arg_sizes, int64_t *arg_types, int32_t num_teams, int32_t thread_limit); |
| 2469 | llvm::Type *TypeParams[] = {CGM.Int64Ty, |
| 2470 | CGM.VoidPtrTy, |
| 2471 | CGM.Int32Ty, |
| 2472 | CGM.VoidPtrPtrTy, |
| 2473 | CGM.VoidPtrPtrTy, |
| Alexey Bataev | a90fc66 | 2019-06-25 16:00:43 +0000 | [diff] [blame] | 2474 | CGM.Int64Ty->getPointerTo(), |
| Alexey Bataev | a9f77c6 | 2017-12-13 21:04:20 +0000 | [diff] [blame] | 2475 | CGM.Int64Ty->getPointerTo(), |
| 2476 | CGM.Int32Ty, |
| 2477 | CGM.Int32Ty}; |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2478 | auto *FnTy = |
| Alexey Bataev | a9f77c6 | 2017-12-13 21:04:20 +0000 | [diff] [blame] | 2479 | llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg*/ false); |
| 2480 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__tgt_target_teams_nowait"); |
| 2481 | break; |
| 2482 | } |
| Gheorghe-Teodor Bercea | 66cdbb47 | 2019-05-21 19:42:01 +0000 | [diff] [blame] | 2483 | case OMPRTL__tgt_register_requires: { |
| 2484 | // Build void __tgt_register_requires(int64_t flags); |
| 2485 | llvm::Type *TypeParams[] = {CGM.Int64Ty}; |
| 2486 | auto *FnTy = |
| 2487 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false); |
| 2488 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__tgt_register_requires"); |
| 2489 | break; |
| 2490 | } |
| Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 2491 | case OMPRTL__tgt_target_data_begin: { |
| George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 2492 | // Build void __tgt_target_data_begin(int64_t device_id, int32_t arg_num, |
| Alexey Bataev | a90fc66 | 2019-06-25 16:00:43 +0000 | [diff] [blame] | 2493 | // void** args_base, void **args, int64_t *arg_sizes, int64_t *arg_types); |
| George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 2494 | llvm::Type *TypeParams[] = {CGM.Int64Ty, |
| Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 2495 | CGM.Int32Ty, |
| 2496 | CGM.VoidPtrPtrTy, |
| 2497 | CGM.VoidPtrPtrTy, |
| Alexey Bataev | a90fc66 | 2019-06-25 16:00:43 +0000 | [diff] [blame] | 2498 | CGM.Int64Ty->getPointerTo(), |
| George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 2499 | CGM.Int64Ty->getPointerTo()}; |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2500 | auto *FnTy = |
| Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 2501 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false); |
| 2502 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__tgt_target_data_begin"); |
| 2503 | break; |
| 2504 | } |
| Alexey Bataev | 0cc6b8e | 2017-12-14 17:00:17 +0000 | [diff] [blame] | 2505 | case OMPRTL__tgt_target_data_begin_nowait: { |
| 2506 | // Build void __tgt_target_data_begin_nowait(int64_t device_id, int32_t |
| Alexey Bataev | a90fc66 | 2019-06-25 16:00:43 +0000 | [diff] [blame] | 2507 | // arg_num, void** args_base, void **args, int64_t *arg_sizes, int64_t |
| Alexey Bataev | 0cc6b8e | 2017-12-14 17:00:17 +0000 | [diff] [blame] | 2508 | // *arg_types); |
| 2509 | llvm::Type *TypeParams[] = {CGM.Int64Ty, |
| 2510 | CGM.Int32Ty, |
| 2511 | CGM.VoidPtrPtrTy, |
| 2512 | CGM.VoidPtrPtrTy, |
| Alexey Bataev | a90fc66 | 2019-06-25 16:00:43 +0000 | [diff] [blame] | 2513 | CGM.Int64Ty->getPointerTo(), |
| Alexey Bataev | 0cc6b8e | 2017-12-14 17:00:17 +0000 | [diff] [blame] | 2514 | CGM.Int64Ty->getPointerTo()}; |
| 2515 | auto *FnTy = |
| 2516 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false); |
| 2517 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__tgt_target_data_begin_nowait"); |
| 2518 | break; |
| 2519 | } |
| Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 2520 | case OMPRTL__tgt_target_data_end: { |
| George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 2521 | // Build void __tgt_target_data_end(int64_t device_id, int32_t arg_num, |
| Alexey Bataev | a90fc66 | 2019-06-25 16:00:43 +0000 | [diff] [blame] | 2522 | // void** args_base, void **args, int64_t *arg_sizes, int64_t *arg_types); |
| George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 2523 | llvm::Type *TypeParams[] = {CGM.Int64Ty, |
| Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 2524 | CGM.Int32Ty, |
| 2525 | CGM.VoidPtrPtrTy, |
| 2526 | CGM.VoidPtrPtrTy, |
| Alexey Bataev | a90fc66 | 2019-06-25 16:00:43 +0000 | [diff] [blame] | 2527 | CGM.Int64Ty->getPointerTo(), |
| George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 2528 | CGM.Int64Ty->getPointerTo()}; |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2529 | auto *FnTy = |
| Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 2530 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false); |
| 2531 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__tgt_target_data_end"); |
| 2532 | break; |
| 2533 | } |
| Alexey Bataev | 0cc6b8e | 2017-12-14 17:00:17 +0000 | [diff] [blame] | 2534 | case OMPRTL__tgt_target_data_end_nowait: { |
| 2535 | // Build void __tgt_target_data_end_nowait(int64_t device_id, int32_t |
| Alexey Bataev | a90fc66 | 2019-06-25 16:00:43 +0000 | [diff] [blame] | 2536 | // arg_num, void** args_base, void **args, int64_t *arg_sizes, int64_t |
| Alexey Bataev | 0cc6b8e | 2017-12-14 17:00:17 +0000 | [diff] [blame] | 2537 | // *arg_types); |
| 2538 | llvm::Type *TypeParams[] = {CGM.Int64Ty, |
| 2539 | CGM.Int32Ty, |
| 2540 | CGM.VoidPtrPtrTy, |
| 2541 | CGM.VoidPtrPtrTy, |
| Alexey Bataev | a90fc66 | 2019-06-25 16:00:43 +0000 | [diff] [blame] | 2542 | CGM.Int64Ty->getPointerTo(), |
| Alexey Bataev | 0cc6b8e | 2017-12-14 17:00:17 +0000 | [diff] [blame] | 2543 | CGM.Int64Ty->getPointerTo()}; |
| 2544 | auto *FnTy = |
| 2545 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false); |
| 2546 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__tgt_target_data_end_nowait"); |
| 2547 | break; |
| 2548 | } |
| Samuel Antao | 8d2d730 | 2016-05-26 18:30:22 +0000 | [diff] [blame] | 2549 | case OMPRTL__tgt_target_data_update: { |
| George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 2550 | // Build void __tgt_target_data_update(int64_t device_id, int32_t arg_num, |
| Alexey Bataev | a90fc66 | 2019-06-25 16:00:43 +0000 | [diff] [blame] | 2551 | // void** args_base, void **args, int64_t *arg_sizes, int64_t *arg_types); |
| George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 2552 | llvm::Type *TypeParams[] = {CGM.Int64Ty, |
| Samuel Antao | 8d2d730 | 2016-05-26 18:30:22 +0000 | [diff] [blame] | 2553 | CGM.Int32Ty, |
| 2554 | CGM.VoidPtrPtrTy, |
| 2555 | CGM.VoidPtrPtrTy, |
| Alexey Bataev | a90fc66 | 2019-06-25 16:00:43 +0000 | [diff] [blame] | 2556 | CGM.Int64Ty->getPointerTo(), |
| George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 2557 | CGM.Int64Ty->getPointerTo()}; |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2558 | auto *FnTy = |
| Samuel Antao | 8d2d730 | 2016-05-26 18:30:22 +0000 | [diff] [blame] | 2559 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false); |
| 2560 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__tgt_target_data_update"); |
| 2561 | break; |
| 2562 | } |
| Alexey Bataev | 0cc6b8e | 2017-12-14 17:00:17 +0000 | [diff] [blame] | 2563 | case OMPRTL__tgt_target_data_update_nowait: { |
| 2564 | // Build void __tgt_target_data_update_nowait(int64_t device_id, int32_t |
| Alexey Bataev | a90fc66 | 2019-06-25 16:00:43 +0000 | [diff] [blame] | 2565 | // arg_num, void** args_base, void **args, int64_t *arg_sizes, int64_t |
| Alexey Bataev | 0cc6b8e | 2017-12-14 17:00:17 +0000 | [diff] [blame] | 2566 | // *arg_types); |
| 2567 | llvm::Type *TypeParams[] = {CGM.Int64Ty, |
| 2568 | CGM.Int32Ty, |
| 2569 | CGM.VoidPtrPtrTy, |
| 2570 | CGM.VoidPtrPtrTy, |
| Alexey Bataev | a90fc66 | 2019-06-25 16:00:43 +0000 | [diff] [blame] | 2571 | CGM.Int64Ty->getPointerTo(), |
| Alexey Bataev | 0cc6b8e | 2017-12-14 17:00:17 +0000 | [diff] [blame] | 2572 | CGM.Int64Ty->getPointerTo()}; |
| 2573 | auto *FnTy = |
| 2574 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false); |
| 2575 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__tgt_target_data_update_nowait"); |
| 2576 | break; |
| 2577 | } |
| Michael Kruse | d47b943 | 2019-08-05 18:43:21 +0000 | [diff] [blame] | 2578 | case OMPRTL__tgt_mapper_num_components: { |
| 2579 | // Build int64_t __tgt_mapper_num_components(void *rt_mapper_handle); |
| 2580 | llvm::Type *TypeParams[] = {CGM.VoidPtrTy}; |
| 2581 | auto *FnTy = |
| 2582 | llvm::FunctionType::get(CGM.Int64Ty, TypeParams, /*isVarArg*/ false); |
| 2583 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__tgt_mapper_num_components"); |
| 2584 | break; |
| 2585 | } |
| 2586 | case OMPRTL__tgt_push_mapper_component: { |
| 2587 | // Build void __tgt_push_mapper_component(void *rt_mapper_handle, void |
| 2588 | // *base, void *begin, int64_t size, int64_t type); |
| 2589 | llvm::Type *TypeParams[] = {CGM.VoidPtrTy, CGM.VoidPtrTy, CGM.VoidPtrTy, |
| 2590 | CGM.Int64Ty, CGM.Int64Ty}; |
| 2591 | auto *FnTy = |
| 2592 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false); |
| 2593 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__tgt_push_mapper_component"); |
| 2594 | break; |
| 2595 | } |
| Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 2596 | } |
| Alexey Bataev | 50b3c95 | 2016-02-19 10:38:26 +0000 | [diff] [blame] | 2597 | assert(RTLFn && "Unable to find OpenMP runtime function"); |
| Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 2598 | return RTLFn; |
| 2599 | } |
| Alexey Bataev | 3a3bf0b | 2014-09-22 10:01:53 +0000 | [diff] [blame] | 2600 | |
| James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 2601 | llvm::FunctionCallee |
| 2602 | CGOpenMPRuntime::createForStaticInitFunction(unsigned IVSize, bool IVSigned) { |
| Alexander Musman | 21212e4 | 2015-03-13 10:38:23 +0000 | [diff] [blame] | 2603 | assert((IVSize == 32 || IVSize == 64) && |
| 2604 | "IV size is not compatible with the omp runtime"); |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2605 | StringRef Name = IVSize == 32 ? (IVSigned ? "__kmpc_for_static_init_4" |
| 2606 | : "__kmpc_for_static_init_4u") |
| 2607 | : (IVSigned ? "__kmpc_for_static_init_8" |
| 2608 | : "__kmpc_for_static_init_8u"); |
| 2609 | llvm::Type *ITy = IVSize == 32 ? CGM.Int32Ty : CGM.Int64Ty; |
| 2610 | auto *PtrTy = llvm::PointerType::getUnqual(ITy); |
| Alexander Musman | 21212e4 | 2015-03-13 10:38:23 +0000 | [diff] [blame] | 2611 | llvm::Type *TypeParams[] = { |
| 2612 | getIdentTyPointerTy(), // loc |
| 2613 | CGM.Int32Ty, // tid |
| 2614 | CGM.Int32Ty, // schedtype |
| 2615 | llvm::PointerType::getUnqual(CGM.Int32Ty), // p_lastiter |
| 2616 | PtrTy, // p_lower |
| 2617 | PtrTy, // p_upper |
| 2618 | PtrTy, // p_stride |
| 2619 | ITy, // incr |
| 2620 | ITy // chunk |
| 2621 | }; |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2622 | auto *FnTy = |
| Alexander Musman | 21212e4 | 2015-03-13 10:38:23 +0000 | [diff] [blame] | 2623 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false); |
| 2624 | return CGM.CreateRuntimeFunction(FnTy, Name); |
| 2625 | } |
| 2626 | |
| James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 2627 | llvm::FunctionCallee |
| 2628 | CGOpenMPRuntime::createDispatchInitFunction(unsigned IVSize, bool IVSigned) { |
| Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 2629 | assert((IVSize == 32 || IVSize == 64) && |
| 2630 | "IV size is not compatible with the omp runtime"); |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2631 | StringRef Name = |
| Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 2632 | IVSize == 32 |
| 2633 | ? (IVSigned ? "__kmpc_dispatch_init_4" : "__kmpc_dispatch_init_4u") |
| 2634 | : (IVSigned ? "__kmpc_dispatch_init_8" : "__kmpc_dispatch_init_8u"); |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2635 | llvm::Type *ITy = IVSize == 32 ? CGM.Int32Ty : CGM.Int64Ty; |
| Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 2636 | llvm::Type *TypeParams[] = { getIdentTyPointerTy(), // loc |
| 2637 | CGM.Int32Ty, // tid |
| 2638 | CGM.Int32Ty, // schedtype |
| 2639 | ITy, // lower |
| 2640 | ITy, // upper |
| 2641 | ITy, // stride |
| 2642 | ITy // chunk |
| 2643 | }; |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2644 | auto *FnTy = |
| Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 2645 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false); |
| 2646 | return CGM.CreateRuntimeFunction(FnTy, Name); |
| 2647 | } |
| 2648 | |
| James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 2649 | llvm::FunctionCallee |
| 2650 | CGOpenMPRuntime::createDispatchFiniFunction(unsigned IVSize, bool IVSigned) { |
| Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 2651 | assert((IVSize == 32 || IVSize == 64) && |
| 2652 | "IV size is not compatible with the omp runtime"); |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2653 | StringRef Name = |
| Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 2654 | IVSize == 32 |
| 2655 | ? (IVSigned ? "__kmpc_dispatch_fini_4" : "__kmpc_dispatch_fini_4u") |
| 2656 | : (IVSigned ? "__kmpc_dispatch_fini_8" : "__kmpc_dispatch_fini_8u"); |
| 2657 | llvm::Type *TypeParams[] = { |
| 2658 | getIdentTyPointerTy(), // loc |
| 2659 | CGM.Int32Ty, // tid |
| 2660 | }; |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2661 | auto *FnTy = |
| Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 2662 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false); |
| 2663 | return CGM.CreateRuntimeFunction(FnTy, Name); |
| 2664 | } |
| 2665 | |
| James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 2666 | llvm::FunctionCallee |
| 2667 | CGOpenMPRuntime::createDispatchNextFunction(unsigned IVSize, bool IVSigned) { |
| Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 2668 | assert((IVSize == 32 || IVSize == 64) && |
| 2669 | "IV size is not compatible with the omp runtime"); |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2670 | StringRef Name = |
| Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 2671 | IVSize == 32 |
| 2672 | ? (IVSigned ? "__kmpc_dispatch_next_4" : "__kmpc_dispatch_next_4u") |
| 2673 | : (IVSigned ? "__kmpc_dispatch_next_8" : "__kmpc_dispatch_next_8u"); |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2674 | llvm::Type *ITy = IVSize == 32 ? CGM.Int32Ty : CGM.Int64Ty; |
| 2675 | auto *PtrTy = llvm::PointerType::getUnqual(ITy); |
| Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 2676 | llvm::Type *TypeParams[] = { |
| 2677 | getIdentTyPointerTy(), // loc |
| 2678 | CGM.Int32Ty, // tid |
| 2679 | llvm::PointerType::getUnqual(CGM.Int32Ty), // p_lastiter |
| 2680 | PtrTy, // p_lower |
| 2681 | PtrTy, // p_upper |
| 2682 | PtrTy // p_stride |
| 2683 | }; |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2684 | auto *FnTy = |
| Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 2685 | llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg*/ false); |
| 2686 | return CGM.CreateRuntimeFunction(FnTy, Name); |
| 2687 | } |
| 2688 | |
| Gheorghe-Teodor Bercea | 625f59d | 2019-08-01 21:15:58 +0000 | [diff] [blame] | 2689 | /// Obtain information that uniquely identifies a target entry. This |
| 2690 | /// consists of the file and device IDs as well as line number associated with |
| 2691 | /// the relevant entry source location. |
| 2692 | static void getTargetEntryUniqueInfo(ASTContext &C, SourceLocation Loc, |
| 2693 | unsigned &DeviceID, unsigned &FileID, |
| 2694 | unsigned &LineNum) { |
| 2695 | SourceManager &SM = C.getSourceManager(); |
| 2696 | |
| 2697 | // The loc should be always valid and have a file ID (the user cannot use |
| 2698 | // #pragma directives in macros) |
| 2699 | |
| 2700 | assert(Loc.isValid() && "Source location is expected to be always valid."); |
| 2701 | |
| 2702 | PresumedLoc PLoc = SM.getPresumedLoc(Loc); |
| 2703 | assert(PLoc.isValid() && "Source location is expected to be always valid."); |
| 2704 | |
| 2705 | llvm::sys::fs::UniqueID ID; |
| 2706 | if (auto EC = llvm::sys::fs::getUniqueID(PLoc.getFilename(), ID)) |
| 2707 | SM.getDiagnostics().Report(diag::err_cannot_open_file) |
| 2708 | << PLoc.getFilename() << EC.message(); |
| 2709 | |
| 2710 | DeviceID = ID.getDevice(); |
| 2711 | FileID = ID.getFile(); |
| 2712 | LineNum = PLoc.getLine(); |
| 2713 | } |
| 2714 | |
| Gheorghe-Teodor Bercea | 0034e84 | 2019-06-20 18:04:47 +0000 | [diff] [blame] | 2715 | Address CGOpenMPRuntime::getAddrOfDeclareTargetVar(const VarDecl *VD) { |
| Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 2716 | if (CGM.getLangOpts().OpenMPSimd) |
| 2717 | return Address::invalid(); |
| Alexey Bataev | 92327c5 | 2018-03-26 16:40:55 +0000 | [diff] [blame] | 2718 | llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res = |
| Alexey Bataev | 97b7221 | 2018-08-14 18:31:20 +0000 | [diff] [blame] | 2719 | OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD); |
| Gheorghe-Teodor Bercea | 0034e84 | 2019-06-20 18:04:47 +0000 | [diff] [blame] | 2720 | if (Res && (*Res == OMPDeclareTargetDeclAttr::MT_Link || |
| 2721 | (*Res == OMPDeclareTargetDeclAttr::MT_To && |
| 2722 | HasRequiresUnifiedSharedMemory))) { |
| Alexey Bataev | 92327c5 | 2018-03-26 16:40:55 +0000 | [diff] [blame] | 2723 | SmallString<64> PtrName; |
| 2724 | { |
| 2725 | llvm::raw_svector_ostream OS(PtrName); |
| Gheorghe-Teodor Bercea | 625f59d | 2019-08-01 21:15:58 +0000 | [diff] [blame] | 2726 | OS << CGM.getMangledName(GlobalDecl(VD)); |
| 2727 | if (!VD->isExternallyVisible()) { |
| 2728 | unsigned DeviceID, FileID, Line; |
| 2729 | getTargetEntryUniqueInfo(CGM.getContext(), |
| 2730 | VD->getCanonicalDecl()->getBeginLoc(), |
| 2731 | DeviceID, FileID, Line); |
| 2732 | OS << llvm::format("_%x", FileID); |
| 2733 | } |
| 2734 | OS << "_decl_tgt_ref_ptr"; |
| Alexey Bataev | 92327c5 | 2018-03-26 16:40:55 +0000 | [diff] [blame] | 2735 | } |
| 2736 | llvm::Value *Ptr = CGM.getModule().getNamedValue(PtrName); |
| 2737 | if (!Ptr) { |
| 2738 | QualType PtrTy = CGM.getContext().getPointerType(VD->getType()); |
| 2739 | Ptr = getOrCreateInternalVariable(CGM.getTypes().ConvertTypeForMem(PtrTy), |
| 2740 | PtrName); |
| Gheorghe-Teodor Bercea | 625f59d | 2019-08-01 21:15:58 +0000 | [diff] [blame] | 2741 | |
| 2742 | auto *GV = cast<llvm::GlobalVariable>(Ptr); |
| 2743 | GV->setLinkage(llvm::GlobalValue::WeakAnyLinkage); |
| 2744 | |
| 2745 | if (!CGM.getLangOpts().OpenMPIsDevice) |
| Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 2746 | GV->setInitializer(CGM.GetAddrOfGlobal(VD)); |
| Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 2747 | registerTargetGlobalVariable(VD, cast<llvm::Constant>(Ptr)); |
| Alexey Bataev | 92327c5 | 2018-03-26 16:40:55 +0000 | [diff] [blame] | 2748 | } |
| 2749 | return Address(Ptr, CGM.getContext().getDeclAlign(VD)); |
| 2750 | } |
| 2751 | return Address::invalid(); |
| 2752 | } |
| 2753 | |
| Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2754 | llvm::Constant * |
| 2755 | CGOpenMPRuntime::getOrCreateThreadPrivateCache(const VarDecl *VD) { |
| Samuel Antao | f8b5012 | 2015-07-13 22:54:53 +0000 | [diff] [blame] | 2756 | assert(!CGM.getLangOpts().OpenMPUseTLS || |
| 2757 | !CGM.getContext().getTargetInfo().isTLSSupported()); |
| Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2758 | // Lookup the entry, lazily creating it if necessary. |
| Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 2759 | std::string Suffix = getName({"cache", ""}); |
| 2760 | return getOrCreateInternalVariable( |
| 2761 | CGM.Int8PtrPtrTy, Twine(CGM.getMangledName(VD)).concat(Suffix)); |
| Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2762 | } |
| 2763 | |
| John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2764 | Address CGOpenMPRuntime::getAddrOfThreadPrivate(CodeGenFunction &CGF, |
| 2765 | const VarDecl *VD, |
| 2766 | Address VDAddr, |
| 2767 | SourceLocation Loc) { |
| Samuel Antao | f8b5012 | 2015-07-13 22:54:53 +0000 | [diff] [blame] | 2768 | if (CGM.getLangOpts().OpenMPUseTLS && |
| 2769 | CGM.getContext().getTargetInfo().isTLSSupported()) |
| 2770 | return VDAddr; |
| 2771 | |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2772 | llvm::Type *VarTy = VDAddr.getElementType(); |
| Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 2773 | llvm::Value *Args[] = {emitUpdateLocation(CGF, Loc), getThreadID(CGF, Loc), |
| John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2774 | CGF.Builder.CreatePointerCast(VDAddr.getPointer(), |
| 2775 | CGM.Int8PtrTy), |
| Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2776 | CGM.getSize(CGM.GetTargetTypeStoreSize(VarTy)), |
| 2777 | getOrCreateThreadPrivateCache(VD)}; |
| John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2778 | return Address(CGF.EmitRuntimeCall( |
| 2779 | createRuntimeFunction(OMPRTL__kmpc_threadprivate_cached), Args), |
| 2780 | VDAddr.getAlignment()); |
| Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2781 | } |
| 2782 | |
| Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 2783 | void CGOpenMPRuntime::emitThreadPrivateVarInit( |
| John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2784 | CodeGenFunction &CGF, Address VDAddr, llvm::Value *Ctor, |
| Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2785 | llvm::Value *CopyCtor, llvm::Value *Dtor, SourceLocation Loc) { |
| 2786 | // Call kmp_int32 __kmpc_global_thread_num(&loc) to init OpenMP runtime |
| 2787 | // library. |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2788 | llvm::Value *OMPLoc = emitUpdateLocation(CGF, Loc); |
| Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 2789 | CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_global_thread_num), |
| Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2790 | OMPLoc); |
| 2791 | // Call __kmpc_threadprivate_register(&loc, &var, ctor, cctor/*NULL*/, dtor) |
| 2792 | // to register constructor/destructor for variable. |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2793 | llvm::Value *Args[] = { |
| 2794 | OMPLoc, CGF.Builder.CreatePointerCast(VDAddr.getPointer(), CGM.VoidPtrTy), |
| 2795 | Ctor, CopyCtor, Dtor}; |
| Alexey Bataev | 1e4b713 | 2014-12-03 12:11:24 +0000 | [diff] [blame] | 2796 | CGF.EmitRuntimeCall( |
| Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 2797 | createRuntimeFunction(OMPRTL__kmpc_threadprivate_register), Args); |
| Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2798 | } |
| 2799 | |
| Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 2800 | llvm::Function *CGOpenMPRuntime::emitThreadPrivateVarDefinition( |
| John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2801 | const VarDecl *VD, Address VDAddr, SourceLocation Loc, |
| Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2802 | bool PerformInit, CodeGenFunction *CGF) { |
| Samuel Antao | f8b5012 | 2015-07-13 22:54:53 +0000 | [diff] [blame] | 2803 | if (CGM.getLangOpts().OpenMPUseTLS && |
| 2804 | CGM.getContext().getTargetInfo().isTLSSupported()) |
| 2805 | return nullptr; |
| 2806 | |
| Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2807 | VD = VD->getDefinition(CGM.getContext()); |
| Alexey Bataev | 2a6f3f5 | 2018-11-07 19:11:14 +0000 | [diff] [blame] | 2808 | if (VD && ThreadPrivateWithDefinition.insert(CGM.getMangledName(VD)).second) { |
| Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2809 | QualType ASTTy = VD->getType(); |
| 2810 | |
| 2811 | llvm::Value *Ctor = nullptr, *CopyCtor = nullptr, *Dtor = nullptr; |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2812 | const Expr *Init = VD->getAnyInitializer(); |
| Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2813 | if (CGM.getLangOpts().CPlusPlus && PerformInit) { |
| 2814 | // Generate function that re-emits the declaration's initializer into the |
| 2815 | // threadprivate copy of the variable VD |
| 2816 | CodeGenFunction CtorCGF(CGM); |
| 2817 | FunctionArgList Args; |
| Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 2818 | ImplicitParamDecl Dst(CGM.getContext(), /*DC=*/nullptr, Loc, |
| 2819 | /*Id=*/nullptr, CGM.getContext().VoidPtrTy, |
| Alexey Bataev | 5622323 | 2017-06-09 13:40:18 +0000 | [diff] [blame] | 2820 | ImplicitParamDecl::Other); |
| Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2821 | Args.push_back(&Dst); |
| 2822 | |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2823 | const auto &FI = CGM.getTypes().arrangeBuiltinFunctionDeclaration( |
| John McCall | c56a8b3 | 2016-03-11 04:30:31 +0000 | [diff] [blame] | 2824 | CGM.getContext().VoidPtrTy, Args); |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2825 | llvm::FunctionType *FTy = CGM.getTypes().GetFunctionType(FI); |
| Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 2826 | std::string Name = getName({"__kmpc_global_ctor_", ""}); |
| 2827 | llvm::Function *Fn = |
| 2828 | CGM.CreateGlobalInitOrDestructFunction(FTy, Name, FI, Loc); |
| Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2829 | CtorCGF.StartFunction(GlobalDecl(), CGM.getContext().VoidPtrTy, Fn, FI, |
| Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 2830 | Args, Loc, Loc); |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2831 | llvm::Value *ArgVal = CtorCGF.EmitLoadOfScalar( |
| John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2832 | CtorCGF.GetAddrOfLocalVar(&Dst), /*Volatile=*/false, |
| Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2833 | CGM.getContext().VoidPtrTy, Dst.getLocation()); |
| John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2834 | Address Arg = Address(ArgVal, VDAddr.getAlignment()); |
| Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 2835 | Arg = CtorCGF.Builder.CreateElementBitCast( |
| 2836 | Arg, CtorCGF.ConvertTypeForMem(ASTTy)); |
| Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2837 | CtorCGF.EmitAnyExprToMem(Init, Arg, Init->getType().getQualifiers(), |
| 2838 | /*IsInitializer=*/true); |
| 2839 | ArgVal = CtorCGF.EmitLoadOfScalar( |
| John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2840 | CtorCGF.GetAddrOfLocalVar(&Dst), /*Volatile=*/false, |
| Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2841 | CGM.getContext().VoidPtrTy, Dst.getLocation()); |
| 2842 | CtorCGF.Builder.CreateStore(ArgVal, CtorCGF.ReturnValue); |
| 2843 | CtorCGF.FinishFunction(); |
| 2844 | Ctor = Fn; |
| 2845 | } |
| 2846 | if (VD->getType().isDestructedType() != QualType::DK_none) { |
| 2847 | // Generate function that emits destructor call for the threadprivate copy |
| 2848 | // of the variable VD |
| 2849 | CodeGenFunction DtorCGF(CGM); |
| 2850 | FunctionArgList Args; |
| Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 2851 | ImplicitParamDecl Dst(CGM.getContext(), /*DC=*/nullptr, Loc, |
| 2852 | /*Id=*/nullptr, CGM.getContext().VoidPtrTy, |
| Alexey Bataev | 5622323 | 2017-06-09 13:40:18 +0000 | [diff] [blame] | 2853 | ImplicitParamDecl::Other); |
| Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2854 | Args.push_back(&Dst); |
| 2855 | |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2856 | const auto &FI = CGM.getTypes().arrangeBuiltinFunctionDeclaration( |
| John McCall | c56a8b3 | 2016-03-11 04:30:31 +0000 | [diff] [blame] | 2857 | CGM.getContext().VoidTy, Args); |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2858 | llvm::FunctionType *FTy = CGM.getTypes().GetFunctionType(FI); |
| Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 2859 | std::string Name = getName({"__kmpc_global_dtor_", ""}); |
| 2860 | llvm::Function *Fn = |
| 2861 | CGM.CreateGlobalInitOrDestructFunction(FTy, Name, FI, Loc); |
| Adrian Prantl | 1858c66 | 2016-04-24 22:22:29 +0000 | [diff] [blame] | 2862 | auto NL = ApplyDebugLocation::CreateEmpty(DtorCGF); |
| Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2863 | DtorCGF.StartFunction(GlobalDecl(), CGM.getContext().VoidTy, Fn, FI, Args, |
| Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 2864 | Loc, Loc); |
| Adrian Prantl | 1858c66 | 2016-04-24 22:22:29 +0000 | [diff] [blame] | 2865 | // Create a scope with an artificial location for the body of this function. |
| 2866 | auto AL = ApplyDebugLocation::CreateArtificial(DtorCGF); |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2867 | llvm::Value *ArgVal = DtorCGF.EmitLoadOfScalar( |
| Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2868 | DtorCGF.GetAddrOfLocalVar(&Dst), |
| John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2869 | /*Volatile=*/false, CGM.getContext().VoidPtrTy, Dst.getLocation()); |
| 2870 | DtorCGF.emitDestroy(Address(ArgVal, VDAddr.getAlignment()), ASTTy, |
| Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2871 | DtorCGF.getDestroyer(ASTTy.isDestructedType()), |
| 2872 | DtorCGF.needsEHCleanup(ASTTy.isDestructedType())); |
| 2873 | DtorCGF.FinishFunction(); |
| 2874 | Dtor = Fn; |
| 2875 | } |
| 2876 | // Do not emit init function if it is not required. |
| 2877 | if (!Ctor && !Dtor) |
| 2878 | return nullptr; |
| 2879 | |
| 2880 | llvm::Type *CopyCtorTyArgs[] = {CGM.VoidPtrTy, CGM.VoidPtrTy}; |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2881 | auto *CopyCtorTy = llvm::FunctionType::get(CGM.VoidPtrTy, CopyCtorTyArgs, |
| 2882 | /*isVarArg=*/false) |
| 2883 | ->getPointerTo(); |
| Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2884 | // Copying constructor for the threadprivate variable. |
| 2885 | // Must be NULL - reserved by runtime, but currently it requires that this |
| 2886 | // parameter is always NULL. Otherwise it fires assertion. |
| 2887 | CopyCtor = llvm::Constant::getNullValue(CopyCtorTy); |
| 2888 | if (Ctor == nullptr) { |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2889 | auto *CtorTy = llvm::FunctionType::get(CGM.VoidPtrTy, CGM.VoidPtrTy, |
| 2890 | /*isVarArg=*/false) |
| 2891 | ->getPointerTo(); |
| Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2892 | Ctor = llvm::Constant::getNullValue(CtorTy); |
| 2893 | } |
| 2894 | if (Dtor == nullptr) { |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2895 | auto *DtorTy = llvm::FunctionType::get(CGM.VoidTy, CGM.VoidPtrTy, |
| 2896 | /*isVarArg=*/false) |
| 2897 | ->getPointerTo(); |
| Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2898 | Dtor = llvm::Constant::getNullValue(DtorTy); |
| 2899 | } |
| 2900 | if (!CGF) { |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2901 | auto *InitFunctionTy = |
| Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2902 | llvm::FunctionType::get(CGM.VoidTy, /*isVarArg*/ false); |
| Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 2903 | std::string Name = getName({"__omp_threadprivate_init_", ""}); |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2904 | llvm::Function *InitFunction = CGM.CreateGlobalInitOrDestructFunction( |
| Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 2905 | InitFunctionTy, Name, CGM.getTypes().arrangeNullaryFunction()); |
| Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2906 | CodeGenFunction InitCGF(CGM); |
| 2907 | FunctionArgList ArgList; |
| 2908 | InitCGF.StartFunction(GlobalDecl(), CGM.getContext().VoidTy, InitFunction, |
| 2909 | CGM.getTypes().arrangeNullaryFunction(), ArgList, |
| Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 2910 | Loc, Loc); |
| Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 2911 | emitThreadPrivateVarInit(InitCGF, VDAddr, Ctor, CopyCtor, Dtor, Loc); |
| Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2912 | InitCGF.FinishFunction(); |
| 2913 | return InitFunction; |
| 2914 | } |
| Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 2915 | emitThreadPrivateVarInit(*CGF, VDAddr, Ctor, CopyCtor, Dtor, Loc); |
| Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2916 | } |
| 2917 | return nullptr; |
| 2918 | } |
| 2919 | |
| Alexey Bataev | 34f8a70 | 2018-03-28 14:28:54 +0000 | [diff] [blame] | 2920 | bool CGOpenMPRuntime::emitDeclareTargetVarDefinition(const VarDecl *VD, |
| 2921 | llvm::GlobalVariable *Addr, |
| 2922 | bool PerformInit) { |
| Alexey Bataev | 36724b7 | 2019-10-03 16:46:49 +0000 | [diff] [blame] | 2923 | if (CGM.getLangOpts().OMPTargetTriples.empty() && |
| 2924 | !CGM.getLangOpts().OpenMPIsDevice) |
| Alexey Bataev | 4db9dc6 | 2019-09-23 15:53:51 +0000 | [diff] [blame] | 2925 | return false; |
| Alexey Bataev | 34f8a70 | 2018-03-28 14:28:54 +0000 | [diff] [blame] | 2926 | Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res = |
| Alexey Bataev | 97b7221 | 2018-08-14 18:31:20 +0000 | [diff] [blame] | 2927 | OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD); |
| Gheorghe-Teodor Bercea | 0034e84 | 2019-06-20 18:04:47 +0000 | [diff] [blame] | 2928 | if (!Res || *Res == OMPDeclareTargetDeclAttr::MT_Link || |
| 2929 | (*Res == OMPDeclareTargetDeclAttr::MT_To && |
| 2930 | HasRequiresUnifiedSharedMemory)) |
| Alexey Bataev | d01b749 | 2018-08-15 19:45:12 +0000 | [diff] [blame] | 2931 | return CGM.getLangOpts().OpenMPIsDevice; |
| Alexey Bataev | 34f8a70 | 2018-03-28 14:28:54 +0000 | [diff] [blame] | 2932 | VD = VD->getDefinition(CGM.getContext()); |
| Alexey Bataev | 2a6f3f5 | 2018-11-07 19:11:14 +0000 | [diff] [blame] | 2933 | if (VD && !DeclareTargetWithDefinition.insert(CGM.getMangledName(VD)).second) |
| Alexey Bataev | 34f8a70 | 2018-03-28 14:28:54 +0000 | [diff] [blame] | 2934 | return CGM.getLangOpts().OpenMPIsDevice; |
| 2935 | |
| 2936 | QualType ASTTy = VD->getType(); |
| 2937 | |
| Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2938 | SourceLocation Loc = VD->getCanonicalDecl()->getBeginLoc(); |
| Alexey Bataev | 34f8a70 | 2018-03-28 14:28:54 +0000 | [diff] [blame] | 2939 | // Produce the unique prefix to identify the new target regions. We use |
| 2940 | // the source location of the variable declaration which we know to not |
| 2941 | // conflict with any target region. |
| 2942 | unsigned DeviceID; |
| 2943 | unsigned FileID; |
| 2944 | unsigned Line; |
| 2945 | getTargetEntryUniqueInfo(CGM.getContext(), Loc, DeviceID, FileID, Line); |
| 2946 | SmallString<128> Buffer, Out; |
| 2947 | { |
| 2948 | llvm::raw_svector_ostream OS(Buffer); |
| 2949 | OS << "__omp_offloading_" << llvm::format("_%x", DeviceID) |
| 2950 | << llvm::format("_%x_", FileID) << VD->getName() << "_l" << Line; |
| 2951 | } |
| 2952 | |
| 2953 | const Expr *Init = VD->getAnyInitializer(); |
| 2954 | if (CGM.getLangOpts().CPlusPlus && PerformInit) { |
| 2955 | llvm::Constant *Ctor; |
| 2956 | llvm::Constant *ID; |
| 2957 | if (CGM.getLangOpts().OpenMPIsDevice) { |
| 2958 | // Generate function that re-emits the declaration's initializer into |
| 2959 | // the threadprivate copy of the variable VD |
| 2960 | CodeGenFunction CtorCGF(CGM); |
| 2961 | |
| 2962 | const CGFunctionInfo &FI = CGM.getTypes().arrangeNullaryFunction(); |
| 2963 | llvm::FunctionType *FTy = CGM.getTypes().GetFunctionType(FI); |
| 2964 | llvm::Function *Fn = CGM.CreateGlobalInitOrDestructFunction( |
| 2965 | FTy, Twine(Buffer, "_ctor"), FI, Loc); |
| 2966 | auto NL = ApplyDebugLocation::CreateEmpty(CtorCGF); |
| 2967 | CtorCGF.StartFunction(GlobalDecl(), CGM.getContext().VoidTy, Fn, FI, |
| 2968 | FunctionArgList(), Loc, Loc); |
| 2969 | auto AL = ApplyDebugLocation::CreateArtificial(CtorCGF); |
| 2970 | CtorCGF.EmitAnyExprToMem(Init, |
| 2971 | Address(Addr, CGM.getContext().getDeclAlign(VD)), |
| 2972 | Init->getType().getQualifiers(), |
| 2973 | /*IsInitializer=*/true); |
| 2974 | CtorCGF.FinishFunction(); |
| 2975 | Ctor = Fn; |
| 2976 | ID = llvm::ConstantExpr::getBitCast(Fn, CGM.Int8PtrTy); |
| Alexey Bataev | e253f2f | 2018-05-09 14:15:18 +0000 | [diff] [blame] | 2977 | CGM.addUsedGlobal(cast<llvm::GlobalValue>(Ctor)); |
| Alexey Bataev | 34f8a70 | 2018-03-28 14:28:54 +0000 | [diff] [blame] | 2978 | } else { |
| 2979 | Ctor = new llvm::GlobalVariable( |
| 2980 | CGM.getModule(), CGM.Int8Ty, /*isConstant=*/true, |
| 2981 | llvm::GlobalValue::PrivateLinkage, |
| 2982 | llvm::Constant::getNullValue(CGM.Int8Ty), Twine(Buffer, "_ctor")); |
| 2983 | ID = Ctor; |
| 2984 | } |
| 2985 | |
| 2986 | // Register the information for the entry associated with the constructor. |
| 2987 | Out.clear(); |
| 2988 | OffloadEntriesInfoManager.registerTargetRegionEntryInfo( |
| 2989 | DeviceID, FileID, Twine(Buffer, "_ctor").toStringRef(Out), Line, Ctor, |
| Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 2990 | ID, OffloadEntriesInfoManagerTy::OMPTargetRegionEntryCtor); |
| Alexey Bataev | 34f8a70 | 2018-03-28 14:28:54 +0000 | [diff] [blame] | 2991 | } |
| 2992 | if (VD->getType().isDestructedType() != QualType::DK_none) { |
| 2993 | llvm::Constant *Dtor; |
| 2994 | llvm::Constant *ID; |
| 2995 | if (CGM.getLangOpts().OpenMPIsDevice) { |
| 2996 | // Generate function that emits destructor call for the threadprivate |
| 2997 | // copy of the variable VD |
| 2998 | CodeGenFunction DtorCGF(CGM); |
| 2999 | |
| 3000 | const CGFunctionInfo &FI = CGM.getTypes().arrangeNullaryFunction(); |
| 3001 | llvm::FunctionType *FTy = CGM.getTypes().GetFunctionType(FI); |
| 3002 | llvm::Function *Fn = CGM.CreateGlobalInitOrDestructFunction( |
| 3003 | FTy, Twine(Buffer, "_dtor"), FI, Loc); |
| 3004 | auto NL = ApplyDebugLocation::CreateEmpty(DtorCGF); |
| 3005 | DtorCGF.StartFunction(GlobalDecl(), CGM.getContext().VoidTy, Fn, FI, |
| 3006 | FunctionArgList(), Loc, Loc); |
| 3007 | // Create a scope with an artificial location for the body of this |
| 3008 | // function. |
| 3009 | auto AL = ApplyDebugLocation::CreateArtificial(DtorCGF); |
| 3010 | DtorCGF.emitDestroy(Address(Addr, CGM.getContext().getDeclAlign(VD)), |
| 3011 | ASTTy, DtorCGF.getDestroyer(ASTTy.isDestructedType()), |
| 3012 | DtorCGF.needsEHCleanup(ASTTy.isDestructedType())); |
| 3013 | DtorCGF.FinishFunction(); |
| 3014 | Dtor = Fn; |
| 3015 | ID = llvm::ConstantExpr::getBitCast(Fn, CGM.Int8PtrTy); |
| Alexey Bataev | e253f2f | 2018-05-09 14:15:18 +0000 | [diff] [blame] | 3016 | CGM.addUsedGlobal(cast<llvm::GlobalValue>(Dtor)); |
| Alexey Bataev | 34f8a70 | 2018-03-28 14:28:54 +0000 | [diff] [blame] | 3017 | } else { |
| 3018 | Dtor = new llvm::GlobalVariable( |
| 3019 | CGM.getModule(), CGM.Int8Ty, /*isConstant=*/true, |
| 3020 | llvm::GlobalValue::PrivateLinkage, |
| 3021 | llvm::Constant::getNullValue(CGM.Int8Ty), Twine(Buffer, "_dtor")); |
| 3022 | ID = Dtor; |
| 3023 | } |
| 3024 | // Register the information for the entry associated with the destructor. |
| 3025 | Out.clear(); |
| 3026 | OffloadEntriesInfoManager.registerTargetRegionEntryInfo( |
| 3027 | DeviceID, FileID, Twine(Buffer, "_dtor").toStringRef(Out), Line, Dtor, |
| Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 3028 | ID, OffloadEntriesInfoManagerTy::OMPTargetRegionEntryDtor); |
| Alexey Bataev | 34f8a70 | 2018-03-28 14:28:54 +0000 | [diff] [blame] | 3029 | } |
| 3030 | return CGM.getLangOpts().OpenMPIsDevice; |
| 3031 | } |
| 3032 | |
| Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 3033 | Address CGOpenMPRuntime::getAddrOfArtificialThreadPrivate(CodeGenFunction &CGF, |
| 3034 | QualType VarType, |
| 3035 | StringRef Name) { |
| Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 3036 | std::string Suffix = getName({"artificial", ""}); |
| Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 3037 | llvm::Type *VarLVType = CGF.ConvertTypeForMem(VarType); |
| Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 3038 | llvm::Value *GAddr = |
| 3039 | getOrCreateInternalVariable(VarLVType, Twine(Name).concat(Suffix)); |
| Alexey Bataev | 8be5a0f | 2019-12-31 12:41:57 -0500 | [diff] [blame] | 3040 | if (CGM.getLangOpts().OpenMP && CGM.getLangOpts().OpenMPUseTLS && |
| 3041 | CGM.getTarget().isTLSSupported()) { |
| 3042 | cast<llvm::GlobalVariable>(GAddr)->setThreadLocal(/*Val=*/true); |
| 3043 | return Address(GAddr, CGM.getContext().getTypeAlignInChars(VarType)); |
| 3044 | } |
| 3045 | std::string CacheSuffix = getName({"cache", ""}); |
| Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 3046 | llvm::Value *Args[] = { |
| 3047 | emitUpdateLocation(CGF, SourceLocation()), |
| 3048 | getThreadID(CGF, SourceLocation()), |
| 3049 | CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(GAddr, CGM.VoidPtrTy), |
| 3050 | CGF.Builder.CreateIntCast(CGF.getTypeSize(VarType), CGM.SizeTy, |
| Rui Ueyama | 49a3ad2 | 2019-07-16 04:46:31 +0000 | [diff] [blame] | 3051 | /*isSigned=*/false), |
| Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 3052 | getOrCreateInternalVariable( |
| 3053 | CGM.VoidPtrPtrTy, Twine(Name).concat(Suffix).concat(CacheSuffix))}; |
| Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 3054 | return Address( |
| 3055 | CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
| 3056 | CGF.EmitRuntimeCall( |
| 3057 | createRuntimeFunction(OMPRTL__kmpc_threadprivate_cached), Args), |
| 3058 | VarLVType->getPointerTo(/*AddrSpace=*/0)), |
| Alexey Bataev | 8be5a0f | 2019-12-31 12:41:57 -0500 | [diff] [blame] | 3059 | CGM.getContext().getTypeAlignInChars(VarType)); |
| Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 3060 | } |
| 3061 | |
| Alexey Bataev | 1d943ae | 2019-11-19 12:12:23 -0500 | [diff] [blame] | 3062 | void CGOpenMPRuntime::emitIfClause(CodeGenFunction &CGF, const Expr *Cond, |
| 3063 | const RegionCodeGenTy &ThenGen, |
| 3064 | const RegionCodeGenTy &ElseGen) { |
| Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 3065 | CodeGenFunction::LexicalScope ConditionScope(CGF, Cond->getSourceRange()); |
| 3066 | |
| 3067 | // If the condition constant folds and can be elided, try to avoid emitting |
| 3068 | // the condition and the dead arm of the if/else. |
| 3069 | bool CondConstant; |
| 3070 | if (CGF.ConstantFoldsToSimpleInteger(Cond, CondConstant)) { |
| Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 3071 | if (CondConstant) |
| Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 3072 | ThenGen(CGF); |
| Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 3073 | else |
| Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 3074 | ElseGen(CGF); |
| Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 3075 | return; |
| 3076 | } |
| 3077 | |
| 3078 | // Otherwise, the condition did not fold, or we couldn't elide it. Just |
| 3079 | // emit the conditional branch. |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3080 | llvm::BasicBlock *ThenBlock = CGF.createBasicBlock("omp_if.then"); |
| 3081 | llvm::BasicBlock *ElseBlock = CGF.createBasicBlock("omp_if.else"); |
| 3082 | llvm::BasicBlock *ContBlock = CGF.createBasicBlock("omp_if.end"); |
| Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 3083 | CGF.EmitBranchOnBoolExpr(Cond, ThenBlock, ElseBlock, /*TrueCount=*/0); |
| 3084 | |
| 3085 | // Emit the 'then' code. |
| 3086 | CGF.EmitBlock(ThenBlock); |
| Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 3087 | ThenGen(CGF); |
| Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 3088 | CGF.EmitBranch(ContBlock); |
| 3089 | // Emit the 'else' code if present. |
| Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 3090 | // There is no need to emit line number for unconditional branch. |
| 3091 | (void)ApplyDebugLocation::CreateEmpty(CGF); |
| 3092 | CGF.EmitBlock(ElseBlock); |
| 3093 | ElseGen(CGF); |
| 3094 | // There is no need to emit line number for unconditional branch. |
| 3095 | (void)ApplyDebugLocation::CreateEmpty(CGF); |
| 3096 | CGF.EmitBranch(ContBlock); |
| Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 3097 | // Emit the continuation block for code after the if. |
| 3098 | CGF.EmitBlock(ContBlock, /*IsFinished=*/true); |
| Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 3099 | } |
| 3100 | |
| Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 3101 | void CGOpenMPRuntime::emitParallelCall(CodeGenFunction &CGF, SourceLocation Loc, |
| James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 3102 | llvm::Function *OutlinedFn, |
| Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 3103 | ArrayRef<llvm::Value *> CapturedVars, |
| Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 3104 | const Expr *IfCond) { |
| Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 3105 | if (!CGF.HaveInsertPoint()) |
| 3106 | return; |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3107 | llvm::Value *RTLoc = emitUpdateLocation(CGF, Loc); |
| Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 3108 | auto &&ThenGen = [OutlinedFn, CapturedVars, RTLoc](CodeGenFunction &CGF, |
| 3109 | PrePostActionTy &) { |
| Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 3110 | // Build call __kmpc_fork_call(loc, n, microtask, var1, .., varn); |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3111 | CGOpenMPRuntime &RT = CGF.CGM.getOpenMPRuntime(); |
| Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 3112 | llvm::Value *Args[] = { |
| 3113 | RTLoc, |
| 3114 | CGF.Builder.getInt32(CapturedVars.size()), // Number of captured vars |
| Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 3115 | CGF.Builder.CreateBitCast(OutlinedFn, RT.getKmpc_MicroPointerTy())}; |
| Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 3116 | llvm::SmallVector<llvm::Value *, 16> RealArgs; |
| 3117 | RealArgs.append(std::begin(Args), std::end(Args)); |
| 3118 | RealArgs.append(CapturedVars.begin(), CapturedVars.end()); |
| 3119 | |
| James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 3120 | llvm::FunctionCallee RTLFn = |
| 3121 | RT.createRuntimeFunction(OMPRTL__kmpc_fork_call); |
| Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 3122 | CGF.EmitRuntimeCall(RTLFn, RealArgs); |
| 3123 | }; |
| Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 3124 | auto &&ElseGen = [OutlinedFn, CapturedVars, RTLoc, Loc](CodeGenFunction &CGF, |
| 3125 | PrePostActionTy &) { |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3126 | CGOpenMPRuntime &RT = CGF.CGM.getOpenMPRuntime(); |
| 3127 | llvm::Value *ThreadID = RT.getThreadID(CGF, Loc); |
| Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 3128 | // Build calls: |
| 3129 | // __kmpc_serialized_parallel(&Loc, GTid); |
| 3130 | llvm::Value *Args[] = {RTLoc, ThreadID}; |
| Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 3131 | CGF.EmitRuntimeCall( |
| 3132 | RT.createRuntimeFunction(OMPRTL__kmpc_serialized_parallel), Args); |
| Alexey Bataev | d74d060 | 2014-10-13 06:02:40 +0000 | [diff] [blame] | 3133 | |
| Alexey Bataev | 3e0f4f8 | 2019-10-17 14:36:43 +0000 | [diff] [blame] | 3134 | // OutlinedFn(>id, &zero_bound, CapturedStruct); |
| 3135 | Address ThreadIDAddr = RT.emitThreadIDAddress(CGF, Loc); |
| Alexey Bataev | f89cf21 | 2019-10-16 16:59:01 +0000 | [diff] [blame] | 3136 | Address ZeroAddrBound = |
| 3137 | CGF.CreateDefaultAlignTempAlloca(CGF.Int32Ty, |
| 3138 | /*Name=*/".bound.zero.addr"); |
| 3139 | CGF.InitTempAlloca(ZeroAddrBound, CGF.Builder.getInt32(/*C*/ 0)); |
| Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 3140 | llvm::SmallVector<llvm::Value *, 16> OutlinedFnArgs; |
| Alexey Bataev | 8521ff6 | 2018-07-25 20:03:01 +0000 | [diff] [blame] | 3141 | // ThreadId for serialized parallels is 0. |
| Alexey Bataev | 3e0f4f8 | 2019-10-17 14:36:43 +0000 | [diff] [blame] | 3142 | OutlinedFnArgs.push_back(ThreadIDAddr.getPointer()); |
| Alexey Bataev | f89cf21 | 2019-10-16 16:59:01 +0000 | [diff] [blame] | 3143 | OutlinedFnArgs.push_back(ZeroAddrBound.getPointer()); |
| Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 3144 | OutlinedFnArgs.append(CapturedVars.begin(), CapturedVars.end()); |
| Alexey Bataev | 3c595a6 | 2017-08-14 15:01:03 +0000 | [diff] [blame] | 3145 | RT.emitOutlinedFunctionCall(CGF, Loc, OutlinedFn, OutlinedFnArgs); |
| Alexey Bataev | d74d060 | 2014-10-13 06:02:40 +0000 | [diff] [blame] | 3146 | |
| Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 3147 | // __kmpc_end_serialized_parallel(&Loc, GTid); |
| Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 3148 | llvm::Value *EndArgs[] = {RT.emitUpdateLocation(CGF, Loc), ThreadID}; |
| Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 3149 | CGF.EmitRuntimeCall( |
| Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 3150 | RT.createRuntimeFunction(OMPRTL__kmpc_end_serialized_parallel), |
| 3151 | EndArgs); |
| Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 3152 | }; |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3153 | if (IfCond) { |
| Alexey Bataev | 1d943ae | 2019-11-19 12:12:23 -0500 | [diff] [blame] | 3154 | emitIfClause(CGF, IfCond, ThenGen, ElseGen); |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3155 | } else { |
| Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 3156 | RegionCodeGenTy ThenRCG(ThenGen); |
| 3157 | ThenRCG(CGF); |
| Alexey Bataev | f539faa | 2016-03-28 12:58:34 +0000 | [diff] [blame] | 3158 | } |
| Alexey Bataev | d74d060 | 2014-10-13 06:02:40 +0000 | [diff] [blame] | 3159 | } |
| 3160 | |
| NAKAMURA Takumi | 59c74b22 | 2014-10-27 08:08:18 +0000 | [diff] [blame] | 3161 | // If we're inside an (outlined) parallel region, use the region info's |
| Alexey Bataev | d74d060 | 2014-10-13 06:02:40 +0000 | [diff] [blame] | 3162 | // thread-ID variable (it is passed in a first argument of the outlined function |
| 3163 | // as "kmp_int32 *gtid"). Otherwise, if we're not inside parallel region, but in |
| 3164 | // regular serial code region, get thread ID by calling kmp_int32 |
| 3165 | // kmpc_global_thread_num(ident_t *loc), stash this thread ID in a temporary and |
| 3166 | // return the address of that temp. |
| John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3167 | Address CGOpenMPRuntime::emitThreadIDAddress(CodeGenFunction &CGF, |
| 3168 | SourceLocation Loc) { |
| Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 3169 | if (auto *OMPRegionInfo = |
| Alexey Bataev | d74d060 | 2014-10-13 06:02:40 +0000 | [diff] [blame] | 3170 | dyn_cast_or_null<CGOpenMPRegionInfo>(CGF.CapturedStmtInfo)) |
| Alexey Bataev | 8cbe0a6 | 2015-02-26 10:27:34 +0000 | [diff] [blame] | 3171 | if (OMPRegionInfo->getThreadIDVariable()) |
| Akira Hatanaka | f139ae3 | 2019-12-03 15:17:01 -0800 | [diff] [blame] | 3172 | return OMPRegionInfo->getThreadIDVariableLValue(CGF).getAddress(CGF); |
| Alexey Bataev | 8cbe0a6 | 2015-02-26 10:27:34 +0000 | [diff] [blame] | 3173 | |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3174 | llvm::Value *ThreadID = getThreadID(CGF, Loc); |
| 3175 | QualType Int32Ty = |
| Alexey Bataev | d74d060 | 2014-10-13 06:02:40 +0000 | [diff] [blame] | 3176 | CGF.getContext().getIntTypeForBitwidth(/*DestWidth*/ 32, /*Signed*/ true); |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3177 | Address ThreadIDTemp = CGF.CreateMemTemp(Int32Ty, /*Name*/ ".threadid_temp."); |
| Alexey Bataev | d74d060 | 2014-10-13 06:02:40 +0000 | [diff] [blame] | 3178 | CGF.EmitStoreOfScalar(ThreadID, |
| John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3179 | CGF.MakeAddrLValue(ThreadIDTemp, Int32Ty)); |
| Alexey Bataev | d74d060 | 2014-10-13 06:02:40 +0000 | [diff] [blame] | 3180 | |
| 3181 | return ThreadIDTemp; |
| 3182 | } |
| 3183 | |
| Alexey Bataev | 1af5bd5 | 2019-03-05 17:47:18 +0000 | [diff] [blame] | 3184 | llvm::Constant *CGOpenMPRuntime::getOrCreateInternalVariable( |
| 3185 | llvm::Type *Ty, const llvm::Twine &Name, unsigned AddressSpace) { |
| Alexey Bataev | 3a3bf0b | 2014-09-22 10:01:53 +0000 | [diff] [blame] | 3186 | SmallString<256> Buffer; |
| 3187 | llvm::raw_svector_ostream Out(Buffer); |
| Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 3188 | Out << Name; |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3189 | StringRef RuntimeName = Out.str(); |
| Alexey Bataev | 43a919f | 2018-04-13 17:48:43 +0000 | [diff] [blame] | 3190 | auto &Elem = *InternalVars.try_emplace(RuntimeName, nullptr).first; |
| David Blaikie | 13156b6 | 2014-11-19 03:06:06 +0000 | [diff] [blame] | 3191 | if (Elem.second) { |
| 3192 | assert(Elem.second->getType()->getPointerElementType() == Ty && |
| Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 3193 | "OMP internal variable has different type than requested"); |
| David Blaikie | 13156b6 | 2014-11-19 03:06:06 +0000 | [diff] [blame] | 3194 | return &*Elem.second; |
| Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 3195 | } |
| Alexey Bataev | 3a3bf0b | 2014-09-22 10:01:53 +0000 | [diff] [blame] | 3196 | |
| David Blaikie | 13156b6 | 2014-11-19 03:06:06 +0000 | [diff] [blame] | 3197 | return Elem.second = new llvm::GlobalVariable( |
| 3198 | CGM.getModule(), Ty, /*IsConstant*/ false, |
| 3199 | llvm::GlobalValue::CommonLinkage, llvm::Constant::getNullValue(Ty), |
| Alexey Bataev | 1af5bd5 | 2019-03-05 17:47:18 +0000 | [diff] [blame] | 3200 | Elem.first(), /*InsertBefore=*/nullptr, |
| 3201 | llvm::GlobalValue::NotThreadLocal, AddressSpace); |
| Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 3202 | } |
| 3203 | |
| Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 3204 | llvm::Value *CGOpenMPRuntime::getCriticalRegionLock(StringRef CriticalName) { |
| Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 3205 | std::string Prefix = Twine("gomp_critical_user_", CriticalName).str(); |
| 3206 | std::string Name = getName({Prefix, "var"}); |
| 3207 | return getOrCreateInternalVariable(KmpCriticalNameTy, Name); |
| Alexey Bataev | 3a3bf0b | 2014-09-22 10:01:53 +0000 | [diff] [blame] | 3208 | } |
| 3209 | |
| Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 3210 | namespace { |
| Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 3211 | /// Common pre(post)-action for different OpenMP constructs. |
| 3212 | class CommonActionTy final : public PrePostActionTy { |
| James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 3213 | llvm::FunctionCallee EnterCallee; |
| Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 3214 | ArrayRef<llvm::Value *> EnterArgs; |
| James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 3215 | llvm::FunctionCallee ExitCallee; |
| Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 3216 | ArrayRef<llvm::Value *> ExitArgs; |
| 3217 | bool Conditional; |
| 3218 | llvm::BasicBlock *ContBlock = nullptr; |
| Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 3219 | |
| 3220 | public: |
| James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 3221 | CommonActionTy(llvm::FunctionCallee EnterCallee, |
| 3222 | ArrayRef<llvm::Value *> EnterArgs, |
| 3223 | llvm::FunctionCallee ExitCallee, |
| 3224 | ArrayRef<llvm::Value *> ExitArgs, bool Conditional = false) |
| Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 3225 | : EnterCallee(EnterCallee), EnterArgs(EnterArgs), ExitCallee(ExitCallee), |
| 3226 | ExitArgs(ExitArgs), Conditional(Conditional) {} |
| 3227 | void Enter(CodeGenFunction &CGF) override { |
| 3228 | llvm::Value *EnterRes = CGF.EmitRuntimeCall(EnterCallee, EnterArgs); |
| 3229 | if (Conditional) { |
| 3230 | llvm::Value *CallBool = CGF.Builder.CreateIsNotNull(EnterRes); |
| 3231 | auto *ThenBlock = CGF.createBasicBlock("omp_if.then"); |
| 3232 | ContBlock = CGF.createBasicBlock("omp_if.end"); |
| 3233 | // Generate the branch (If-stmt) |
| 3234 | CGF.Builder.CreateCondBr(CallBool, ThenBlock, ContBlock); |
| 3235 | CGF.EmitBlock(ThenBlock); |
| 3236 | } |
| Alexey Bataev | a744ff5 | 2015-05-05 09:24:37 +0000 | [diff] [blame] | 3237 | } |
| Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 3238 | void Done(CodeGenFunction &CGF) { |
| 3239 | // Emit the rest of blocks/branches |
| 3240 | CGF.EmitBranch(ContBlock); |
| 3241 | CGF.EmitBlock(ContBlock, true); |
| 3242 | } |
| 3243 | void Exit(CodeGenFunction &CGF) override { |
| 3244 | CGF.EmitRuntimeCall(ExitCallee, ExitArgs); |
| Alexey Bataev | 3e6124b | 2015-04-10 07:48:12 +0000 | [diff] [blame] | 3245 | } |
| Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 3246 | }; |
| Hans Wennborg | 7eb5464 | 2015-09-10 17:07:54 +0000 | [diff] [blame] | 3247 | } // anonymous namespace |
| Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 3248 | |
| 3249 | void CGOpenMPRuntime::emitCriticalRegion(CodeGenFunction &CGF, |
| 3250 | StringRef CriticalName, |
| 3251 | const RegionCodeGenTy &CriticalOpGen, |
| Alexey Bataev | fc57d16 | 2015-12-15 10:55:09 +0000 | [diff] [blame] | 3252 | SourceLocation Loc, const Expr *Hint) { |
| 3253 | // __kmpc_critical[_with_hint](ident_t *, gtid, Lock[, hint]); |
| Alexey Bataev | 75ddfab | 2014-12-01 11:32:38 +0000 | [diff] [blame] | 3254 | // CriticalOpGen(); |
| 3255 | // __kmpc_end_critical(ident_t *, gtid, Lock); |
| 3256 | // Prepare arguments and build a call to __kmpc_critical |
| Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 3257 | if (!CGF.HaveInsertPoint()) |
| 3258 | return; |
| Alexey Bataev | fc57d16 | 2015-12-15 10:55:09 +0000 | [diff] [blame] | 3259 | llvm::Value *Args[] = {emitUpdateLocation(CGF, Loc), getThreadID(CGF, Loc), |
| 3260 | getCriticalRegionLock(CriticalName)}; |
| Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 3261 | llvm::SmallVector<llvm::Value *, 4> EnterArgs(std::begin(Args), |
| 3262 | std::end(Args)); |
| Alexey Bataev | fc57d16 | 2015-12-15 10:55:09 +0000 | [diff] [blame] | 3263 | if (Hint) { |
| Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 3264 | EnterArgs.push_back(CGF.Builder.CreateIntCast( |
| 3265 | CGF.EmitScalarExpr(Hint), CGM.IntPtrTy, /*isSigned=*/false)); |
| 3266 | } |
| 3267 | CommonActionTy Action( |
| 3268 | createRuntimeFunction(Hint ? OMPRTL__kmpc_critical_with_hint |
| 3269 | : OMPRTL__kmpc_critical), |
| 3270 | EnterArgs, createRuntimeFunction(OMPRTL__kmpc_end_critical), Args); |
| 3271 | CriticalOpGen.setAction(Action); |
| Alexey Bataev | fc57d16 | 2015-12-15 10:55:09 +0000 | [diff] [blame] | 3272 | emitInlinedDirective(CGF, OMPD_critical, CriticalOpGen); |
| Alexey Bataev | 3a3bf0b | 2014-09-22 10:01:53 +0000 | [diff] [blame] | 3273 | } |
| Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 3274 | |
| Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 3275 | void CGOpenMPRuntime::emitMasterRegion(CodeGenFunction &CGF, |
| Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 3276 | const RegionCodeGenTy &MasterOpGen, |
| Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 3277 | SourceLocation Loc) { |
| Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 3278 | if (!CGF.HaveInsertPoint()) |
| 3279 | return; |
| Alexey Bataev | 8d69065 | 2014-12-04 07:23:53 +0000 | [diff] [blame] | 3280 | // if(__kmpc_master(ident_t *, gtid)) { |
| 3281 | // MasterOpGen(); |
| 3282 | // __kmpc_end_master(ident_t *, gtid); |
| 3283 | // } |
| 3284 | // Prepare arguments and build a call to __kmpc_master |
| Alexey Bataev | d7614fb | 2015-04-10 06:33:45 +0000 | [diff] [blame] | 3285 | llvm::Value *Args[] = {emitUpdateLocation(CGF, Loc), getThreadID(CGF, Loc)}; |
| Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 3286 | CommonActionTy Action(createRuntimeFunction(OMPRTL__kmpc_master), Args, |
| 3287 | createRuntimeFunction(OMPRTL__kmpc_end_master), Args, |
| 3288 | /*Conditional=*/true); |
| 3289 | MasterOpGen.setAction(Action); |
| 3290 | emitInlinedDirective(CGF, OMPD_master, MasterOpGen); |
| 3291 | Action.Done(CGF); |
| Alexey Bataev | 8d69065 | 2014-12-04 07:23:53 +0000 | [diff] [blame] | 3292 | } |
| 3293 | |
| Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 3294 | void CGOpenMPRuntime::emitTaskyieldCall(CodeGenFunction &CGF, |
| 3295 | SourceLocation Loc) { |
| Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 3296 | if (!CGF.HaveInsertPoint()) |
| 3297 | return; |
| Alexey Bataev | 9f797f3 | 2015-02-05 05:57:51 +0000 | [diff] [blame] | 3298 | // Build call __kmpc_omp_taskyield(loc, thread_id, 0); |
| 3299 | llvm::Value *Args[] = { |
| Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 3300 | emitUpdateLocation(CGF, Loc), getThreadID(CGF, Loc), |
| Alexey Bataev | 9f797f3 | 2015-02-05 05:57:51 +0000 | [diff] [blame] | 3301 | llvm::ConstantInt::get(CGM.IntTy, /*V=*/0, /*isSigned=*/true)}; |
| Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 3302 | CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_omp_taskyield), Args); |
| Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 3303 | if (auto *Region = dyn_cast_or_null<CGOpenMPRegionInfo>(CGF.CapturedStmtInfo)) |
| 3304 | Region->emitUntiedSwitch(CGF); |
| Alexey Bataev | 9f797f3 | 2015-02-05 05:57:51 +0000 | [diff] [blame] | 3305 | } |
| 3306 | |
| Alexey Bataev | c30dd2d | 2015-06-18 12:14:09 +0000 | [diff] [blame] | 3307 | void CGOpenMPRuntime::emitTaskgroupRegion(CodeGenFunction &CGF, |
| 3308 | const RegionCodeGenTy &TaskgroupOpGen, |
| 3309 | SourceLocation Loc) { |
| Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 3310 | if (!CGF.HaveInsertPoint()) |
| 3311 | return; |
| Alexey Bataev | c30dd2d | 2015-06-18 12:14:09 +0000 | [diff] [blame] | 3312 | // __kmpc_taskgroup(ident_t *, gtid); |
| 3313 | // TaskgroupOpGen(); |
| 3314 | // __kmpc_end_taskgroup(ident_t *, gtid); |
| 3315 | // Prepare arguments and build a call to __kmpc_taskgroup |
| Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 3316 | llvm::Value *Args[] = {emitUpdateLocation(CGF, Loc), getThreadID(CGF, Loc)}; |
| 3317 | CommonActionTy Action(createRuntimeFunction(OMPRTL__kmpc_taskgroup), Args, |
| 3318 | createRuntimeFunction(OMPRTL__kmpc_end_taskgroup), |
| 3319 | Args); |
| 3320 | TaskgroupOpGen.setAction(Action); |
| 3321 | emitInlinedDirective(CGF, OMPD_taskgroup, TaskgroupOpGen); |
| Alexey Bataev | c30dd2d | 2015-06-18 12:14:09 +0000 | [diff] [blame] | 3322 | } |
| 3323 | |
| John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3324 | /// Given an array of pointers to variables, project the address of a |
| 3325 | /// given variable. |
| Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 3326 | static Address emitAddrOfVarFromArray(CodeGenFunction &CGF, Address Array, |
| 3327 | unsigned Index, const VarDecl *Var) { |
| John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3328 | // Pull out the pointer to the variable. |
| James Y Knight | 751fe28 | 2019-02-09 22:22:28 +0000 | [diff] [blame] | 3329 | Address PtrAddr = CGF.Builder.CreateConstArrayGEP(Array, Index); |
| John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3330 | llvm::Value *Ptr = CGF.Builder.CreateLoad(PtrAddr); |
| 3331 | |
| 3332 | Address Addr = Address(Ptr, CGF.getContext().getDeclAlign(Var)); |
| Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 3333 | Addr = CGF.Builder.CreateElementBitCast( |
| 3334 | Addr, CGF.ConvertTypeForMem(Var->getType())); |
| John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3335 | return Addr; |
| 3336 | } |
| 3337 | |
| Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3338 | static llvm::Value *emitCopyprivateCopyFunction( |
| Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 3339 | CodeGenModule &CGM, llvm::Type *ArgsType, |
| 3340 | ArrayRef<const Expr *> CopyprivateVars, ArrayRef<const Expr *> DestExprs, |
| Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 3341 | ArrayRef<const Expr *> SrcExprs, ArrayRef<const Expr *> AssignmentOps, |
| 3342 | SourceLocation Loc) { |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3343 | ASTContext &C = CGM.getContext(); |
| Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3344 | // void copy_func(void *LHSArg, void *RHSArg); |
| 3345 | FunctionArgList Args; |
| Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 3346 | ImplicitParamDecl LHSArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, C.VoidPtrTy, |
| 3347 | ImplicitParamDecl::Other); |
| 3348 | ImplicitParamDecl RHSArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, C.VoidPtrTy, |
| 3349 | ImplicitParamDecl::Other); |
| Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3350 | Args.push_back(&LHSArg); |
| 3351 | Args.push_back(&RHSArg); |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3352 | const auto &CGFI = |
| 3353 | CGM.getTypes().arrangeBuiltinFunctionDeclaration(C.VoidTy, Args); |
| Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 3354 | std::string Name = |
| 3355 | CGM.getOpenMPRuntime().getName({"omp", "copyprivate", "copy_func"}); |
| 3356 | auto *Fn = llvm::Function::Create(CGM.getTypes().GetFunctionType(CGFI), |
| 3357 | llvm::GlobalValue::InternalLinkage, Name, |
| 3358 | &CGM.getModule()); |
| Rafael Espindola | 51ec5a9 | 2018-02-28 23:46:35 +0000 | [diff] [blame] | 3359 | CGM.SetInternalFunctionAttributes(GlobalDecl(), Fn, CGFI); |
| Alexey Bataev | c0f879b | 2018-04-10 20:10:53 +0000 | [diff] [blame] | 3360 | Fn->setDoesNotRecurse(); |
| Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3361 | CodeGenFunction CGF(CGM); |
| Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 3362 | CGF.StartFunction(GlobalDecl(), C.VoidTy, Fn, CGFI, Args, Loc, Loc); |
| Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 3363 | // Dest = (void*[n])(LHSArg); |
| Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3364 | // Src = (void*[n])(RHSArg); |
| John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3365 | Address LHS(CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
| 3366 | CGF.Builder.CreateLoad(CGF.GetAddrOfLocalVar(&LHSArg)), |
| 3367 | ArgsType), CGF.getPointerAlign()); |
| 3368 | Address RHS(CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
| 3369 | CGF.Builder.CreateLoad(CGF.GetAddrOfLocalVar(&RHSArg)), |
| 3370 | ArgsType), CGF.getPointerAlign()); |
| Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3371 | // *(Type0*)Dst[0] = *(Type0*)Src[0]; |
| 3372 | // *(Type1*)Dst[1] = *(Type1*)Src[1]; |
| 3373 | // ... |
| 3374 | // *(Typen*)Dst[n] = *(Typen*)Src[n]; |
| Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3375 | for (unsigned I = 0, E = AssignmentOps.size(); I < E; ++I) { |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3376 | const auto *DestVar = |
| 3377 | cast<VarDecl>(cast<DeclRefExpr>(DestExprs[I])->getDecl()); |
| John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3378 | Address DestAddr = emitAddrOfVarFromArray(CGF, LHS, I, DestVar); |
| 3379 | |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3380 | const auto *SrcVar = |
| 3381 | cast<VarDecl>(cast<DeclRefExpr>(SrcExprs[I])->getDecl()); |
| John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3382 | Address SrcAddr = emitAddrOfVarFromArray(CGF, RHS, I, SrcVar); |
| 3383 | |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3384 | const auto *VD = cast<DeclRefExpr>(CopyprivateVars[I])->getDecl(); |
| Alexey Bataev | 1d9c15c | 2015-05-19 12:31:28 +0000 | [diff] [blame] | 3385 | QualType Type = VD->getType(); |
| John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3386 | CGF.EmitOMPCopy(Type, DestAddr, SrcAddr, DestVar, SrcVar, AssignmentOps[I]); |
| Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3387 | } |
| Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3388 | CGF.FinishFunction(); |
| 3389 | return Fn; |
| 3390 | } |
| 3391 | |
| Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 3392 | void CGOpenMPRuntime::emitSingleRegion(CodeGenFunction &CGF, |
| Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 3393 | const RegionCodeGenTy &SingleOpGen, |
| Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3394 | SourceLocation Loc, |
| 3395 | ArrayRef<const Expr *> CopyprivateVars, |
| 3396 | ArrayRef<const Expr *> SrcExprs, |
| 3397 | ArrayRef<const Expr *> DstExprs, |
| 3398 | ArrayRef<const Expr *> AssignmentOps) { |
| Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 3399 | if (!CGF.HaveInsertPoint()) |
| 3400 | return; |
| Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3401 | assert(CopyprivateVars.size() == SrcExprs.size() && |
| 3402 | CopyprivateVars.size() == DstExprs.size() && |
| 3403 | CopyprivateVars.size() == AssignmentOps.size()); |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3404 | ASTContext &C = CGM.getContext(); |
| Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3405 | // int32 did_it = 0; |
| Alexey Bataev | 6956e2e | 2015-02-05 06:35:41 +0000 | [diff] [blame] | 3406 | // if(__kmpc_single(ident_t *, gtid)) { |
| 3407 | // SingleOpGen(); |
| 3408 | // __kmpc_end_single(ident_t *, gtid); |
| Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3409 | // did_it = 1; |
| Alexey Bataev | 6956e2e | 2015-02-05 06:35:41 +0000 | [diff] [blame] | 3410 | // } |
| Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3411 | // call __kmpc_copyprivate(ident_t *, gtid, <buf_size>, <copyprivate list>, |
| 3412 | // <copy_func>, did_it); |
| 3413 | |
| John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3414 | Address DidIt = Address::invalid(); |
| Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3415 | if (!CopyprivateVars.empty()) { |
| 3416 | // int32 did_it = 0; |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3417 | QualType KmpInt32Ty = |
| 3418 | C.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1); |
| Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3419 | DidIt = CGF.CreateMemTemp(KmpInt32Ty, ".omp.copyprivate.did_it"); |
| John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3420 | CGF.Builder.CreateStore(CGF.Builder.getInt32(0), DidIt); |
| Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3421 | } |
| Alexey Bataev | 6956e2e | 2015-02-05 06:35:41 +0000 | [diff] [blame] | 3422 | // Prepare arguments and build a call to __kmpc_single |
| Alexey Bataev | d7614fb | 2015-04-10 06:33:45 +0000 | [diff] [blame] | 3423 | llvm::Value *Args[] = {emitUpdateLocation(CGF, Loc), getThreadID(CGF, Loc)}; |
| Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 3424 | CommonActionTy Action(createRuntimeFunction(OMPRTL__kmpc_single), Args, |
| 3425 | createRuntimeFunction(OMPRTL__kmpc_end_single), Args, |
| 3426 | /*Conditional=*/true); |
| 3427 | SingleOpGen.setAction(Action); |
| 3428 | emitInlinedDirective(CGF, OMPD_single, SingleOpGen); |
| 3429 | if (DidIt.isValid()) { |
| 3430 | // did_it = 1; |
| 3431 | CGF.Builder.CreateStore(CGF.Builder.getInt32(1), DidIt); |
| 3432 | } |
| 3433 | Action.Done(CGF); |
| Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3434 | // call __kmpc_copyprivate(ident_t *, gtid, <buf_size>, <copyprivate list>, |
| 3435 | // <copy_func>, did_it); |
| John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3436 | if (DidIt.isValid()) { |
| Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3437 | llvm::APInt ArraySize(/*unsigned int numBits=*/32, CopyprivateVars.size()); |
| Richard Smith | 772e266 | 2019-10-04 01:25:59 +0000 | [diff] [blame] | 3438 | QualType CopyprivateArrayTy = C.getConstantArrayType( |
| 3439 | C.VoidPtrTy, ArraySize, nullptr, ArrayType::Normal, |
| 3440 | /*IndexTypeQuals=*/0); |
| Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3441 | // Create a list of all private variables for copyprivate. |
| John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3442 | Address CopyprivateList = |
| Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3443 | CGF.CreateMemTemp(CopyprivateArrayTy, ".omp.copyprivate.cpr_list"); |
| 3444 | for (unsigned I = 0, E = CopyprivateVars.size(); I < E; ++I) { |
| James Y Knight | 751fe28 | 2019-02-09 22:22:28 +0000 | [diff] [blame] | 3445 | Address Elem = CGF.Builder.CreateConstArrayGEP(CopyprivateList, I); |
| John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3446 | CGF.Builder.CreateStore( |
| Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3447 | CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
| Akira Hatanaka | f139ae3 | 2019-12-03 15:17:01 -0800 | [diff] [blame] | 3448 | CGF.EmitLValue(CopyprivateVars[I]).getPointer(CGF), |
| 3449 | CGF.VoidPtrTy), |
| John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3450 | Elem); |
| Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3451 | } |
| 3452 | // Build function that copies private values from single region to all other |
| 3453 | // threads in the corresponding parallel region. |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3454 | llvm::Value *CpyFn = emitCopyprivateCopyFunction( |
| Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3455 | CGM, CGF.ConvertTypeForMem(CopyprivateArrayTy)->getPointerTo(), |
| Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 3456 | CopyprivateVars, SrcExprs, DstExprs, AssignmentOps, Loc); |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3457 | llvm::Value *BufSize = CGF.getTypeSize(CopyprivateArrayTy); |
| John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3458 | Address CL = |
| 3459 | CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(CopyprivateList, |
| 3460 | CGF.VoidPtrTy); |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3461 | llvm::Value *DidItVal = CGF.Builder.CreateLoad(DidIt); |
| Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3462 | llvm::Value *Args[] = { |
| 3463 | emitUpdateLocation(CGF, Loc), // ident_t *<loc> |
| 3464 | getThreadID(CGF, Loc), // i32 <gtid> |
| Alexey Bataev | 66beaa9 | 2015-04-30 03:47:32 +0000 | [diff] [blame] | 3465 | BufSize, // size_t <buf_size> |
| John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3466 | CL.getPointer(), // void *<copyprivate list> |
| Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3467 | CpyFn, // void (*) (void *, void *) <copy_func> |
| 3468 | DidItVal // i32 did_it |
| 3469 | }; |
| 3470 | CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_copyprivate), Args); |
| 3471 | } |
| Alexey Bataev | 6956e2e | 2015-02-05 06:35:41 +0000 | [diff] [blame] | 3472 | } |
| 3473 | |
| Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 3474 | void CGOpenMPRuntime::emitOrderedRegion(CodeGenFunction &CGF, |
| 3475 | const RegionCodeGenTy &OrderedOpGen, |
| Alexey Bataev | 5f600d6 | 2015-09-29 03:48:57 +0000 | [diff] [blame] | 3476 | SourceLocation Loc, bool IsThreads) { |
| Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 3477 | if (!CGF.HaveInsertPoint()) |
| 3478 | return; |
| Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 3479 | // __kmpc_ordered(ident_t *, gtid); |
| 3480 | // OrderedOpGen(); |
| 3481 | // __kmpc_end_ordered(ident_t *, gtid); |
| 3482 | // Prepare arguments and build a call to __kmpc_ordered |
| Alexey Bataev | 5f600d6 | 2015-09-29 03:48:57 +0000 | [diff] [blame] | 3483 | if (IsThreads) { |
| Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 3484 | llvm::Value *Args[] = {emitUpdateLocation(CGF, Loc), getThreadID(CGF, Loc)}; |
| Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 3485 | CommonActionTy Action(createRuntimeFunction(OMPRTL__kmpc_ordered), Args, |
| 3486 | createRuntimeFunction(OMPRTL__kmpc_end_ordered), |
| 3487 | Args); |
| 3488 | OrderedOpGen.setAction(Action); |
| 3489 | emitInlinedDirective(CGF, OMPD_ordered, OrderedOpGen); |
| 3490 | return; |
| Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 3491 | } |
| Alexey Bataev | 5f600d6 | 2015-09-29 03:48:57 +0000 | [diff] [blame] | 3492 | emitInlinedDirective(CGF, OMPD_ordered, OrderedOpGen); |
| Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 3493 | } |
| 3494 | |
| Alexey Bataev | c3028ca | 2018-12-04 15:03:25 +0000 | [diff] [blame] | 3495 | unsigned CGOpenMPRuntime::getDefaultFlagsForBarriers(OpenMPDirectiveKind Kind) { |
| Alexey Bataev | 50b3c95 | 2016-02-19 10:38:26 +0000 | [diff] [blame] | 3496 | unsigned Flags; |
| 3497 | if (Kind == OMPD_for) |
| 3498 | Flags = OMP_IDENT_BARRIER_IMPL_FOR; |
| 3499 | else if (Kind == OMPD_sections) |
| 3500 | Flags = OMP_IDENT_BARRIER_IMPL_SECTIONS; |
| 3501 | else if (Kind == OMPD_single) |
| 3502 | Flags = OMP_IDENT_BARRIER_IMPL_SINGLE; |
| 3503 | else if (Kind == OMPD_barrier) |
| 3504 | Flags = OMP_IDENT_BARRIER_EXPL; |
| 3505 | else |
| 3506 | Flags = OMP_IDENT_BARRIER_IMPL; |
| Alexey Bataev | c3028ca | 2018-12-04 15:03:25 +0000 | [diff] [blame] | 3507 | return Flags; |
| 3508 | } |
| 3509 | |
| Alexey Bataev | f6a53d6 | 2019-03-18 18:40:00 +0000 | [diff] [blame] | 3510 | void CGOpenMPRuntime::getDefaultScheduleAndChunk( |
| 3511 | CodeGenFunction &CGF, const OMPLoopDirective &S, |
| 3512 | OpenMPScheduleClauseKind &ScheduleKind, const Expr *&ChunkExpr) const { |
| 3513 | // Check if the loop directive is actually a doacross loop directive. In this |
| 3514 | // case choose static, 1 schedule. |
| 3515 | if (llvm::any_of( |
| 3516 | S.getClausesOfKind<OMPOrderedClause>(), |
| 3517 | [](const OMPOrderedClause *C) { return C->getNumForLoops(); })) { |
| 3518 | ScheduleKind = OMPC_SCHEDULE_static; |
| 3519 | // Chunk size is 1 in this case. |
| 3520 | llvm::APInt ChunkSize(32, 1); |
| 3521 | ChunkExpr = IntegerLiteral::Create( |
| 3522 | CGF.getContext(), ChunkSize, |
| 3523 | CGF.getContext().getIntTypeForBitwidth(32, /*Signed=*/0), |
| 3524 | SourceLocation()); |
| 3525 | } |
| 3526 | } |
| 3527 | |
| Alexey Bataev | c3028ca | 2018-12-04 15:03:25 +0000 | [diff] [blame] | 3528 | void CGOpenMPRuntime::emitBarrierCall(CodeGenFunction &CGF, SourceLocation Loc, |
| 3529 | OpenMPDirectiveKind Kind, bool EmitChecks, |
| 3530 | bool ForceSimpleCall) { |
| Johannes Doerfert | b3c06db | 2019-11-04 23:00:36 -0600 | [diff] [blame] | 3531 | // Check if we should use the OMPBuilder |
| 3532 | auto *OMPRegionInfo = |
| 3533 | dyn_cast_or_null<CGOpenMPRegionInfo>(CGF.CapturedStmtInfo); |
| 3534 | llvm::OpenMPIRBuilder *OMPBuilder = CGF.CGM.getOpenMPIRBuilder(); |
| 3535 | if (OMPBuilder) { |
| Johannes Doerfert | f9c3c5da | 2019-12-25 10:33:56 -0600 | [diff] [blame] | 3536 | CGF.Builder.restoreIP(OMPBuilder->CreateBarrier( |
| 3537 | CGF.Builder, Kind, ForceSimpleCall, EmitChecks)); |
| Johannes Doerfert | b3c06db | 2019-11-04 23:00:36 -0600 | [diff] [blame] | 3538 | return; |
| 3539 | } |
| 3540 | |
| Alexey Bataev | c3028ca | 2018-12-04 15:03:25 +0000 | [diff] [blame] | 3541 | if (!CGF.HaveInsertPoint()) |
| 3542 | return; |
| 3543 | // Build call __kmpc_cancel_barrier(loc, thread_id); |
| 3544 | // Build call __kmpc_barrier(loc, thread_id); |
| 3545 | unsigned Flags = getDefaultFlagsForBarriers(Kind); |
| Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 3546 | // Build call __kmpc_cancel_barrier(loc, thread_id) or __kmpc_barrier(loc, |
| 3547 | // thread_id); |
| Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 3548 | llvm::Value *Args[] = {emitUpdateLocation(CGF, Loc, Flags), |
| 3549 | getThreadID(CGF, Loc)}; |
| Johannes Doerfert | b3c06db | 2019-11-04 23:00:36 -0600 | [diff] [blame] | 3550 | if (OMPRegionInfo) { |
| Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 3551 | if (!ForceSimpleCall && OMPRegionInfo->hasCancel()) { |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3552 | llvm::Value *Result = CGF.EmitRuntimeCall( |
| Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 3553 | createRuntimeFunction(OMPRTL__kmpc_cancel_barrier), Args); |
| Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 3554 | if (EmitChecks) { |
| Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 3555 | // if (__kmpc_cancel_barrier()) { |
| 3556 | // exit from construct; |
| 3557 | // } |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3558 | llvm::BasicBlock *ExitBB = CGF.createBasicBlock(".cancel.exit"); |
| 3559 | llvm::BasicBlock *ContBB = CGF.createBasicBlock(".cancel.continue"); |
| 3560 | llvm::Value *Cmp = CGF.Builder.CreateIsNotNull(Result); |
| Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 3561 | CGF.Builder.CreateCondBr(Cmp, ExitBB, ContBB); |
| 3562 | CGF.EmitBlock(ExitBB); |
| 3563 | // exit from construct; |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3564 | CodeGenFunction::JumpDest CancelDestination = |
| Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 3565 | CGF.getOMPCancelDestination(OMPRegionInfo->getDirectiveKind()); |
| Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 3566 | CGF.EmitBranchThroughCleanup(CancelDestination); |
| 3567 | CGF.EmitBlock(ContBB, /*IsFinished=*/true); |
| 3568 | } |
| 3569 | return; |
| 3570 | } |
| 3571 | } |
| 3572 | CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_barrier), Args); |
| Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 3573 | } |
| 3574 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 3575 | /// Map the OpenMP loop schedule to the runtime enumeration. |
| Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 3576 | static OpenMPSchedType getRuntimeSchedule(OpenMPScheduleClauseKind ScheduleKind, |
| Alexey Bataev | d7589ffe | 2015-05-20 13:12:48 +0000 | [diff] [blame] | 3577 | bool Chunked, bool Ordered) { |
| Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 3578 | switch (ScheduleKind) { |
| 3579 | case OMPC_SCHEDULE_static: |
| Alexey Bataev | d7589ffe | 2015-05-20 13:12:48 +0000 | [diff] [blame] | 3580 | return Chunked ? (Ordered ? OMP_ord_static_chunked : OMP_sch_static_chunked) |
| 3581 | : (Ordered ? OMP_ord_static : OMP_sch_static); |
| Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 3582 | case OMPC_SCHEDULE_dynamic: |
| Alexey Bataev | d7589ffe | 2015-05-20 13:12:48 +0000 | [diff] [blame] | 3583 | return Ordered ? OMP_ord_dynamic_chunked : OMP_sch_dynamic_chunked; |
| Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 3584 | case OMPC_SCHEDULE_guided: |
| Alexey Bataev | d7589ffe | 2015-05-20 13:12:48 +0000 | [diff] [blame] | 3585 | return Ordered ? OMP_ord_guided_chunked : OMP_sch_guided_chunked; |
| Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 3586 | case OMPC_SCHEDULE_runtime: |
| Alexey Bataev | d7589ffe | 2015-05-20 13:12:48 +0000 | [diff] [blame] | 3587 | return Ordered ? OMP_ord_runtime : OMP_sch_runtime; |
| 3588 | case OMPC_SCHEDULE_auto: |
| 3589 | return Ordered ? OMP_ord_auto : OMP_sch_auto; |
| Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 3590 | case OMPC_SCHEDULE_unknown: |
| 3591 | assert(!Chunked && "chunk was specified but schedule kind not known"); |
| Alexey Bataev | d7589ffe | 2015-05-20 13:12:48 +0000 | [diff] [blame] | 3592 | return Ordered ? OMP_ord_static : OMP_sch_static; |
| Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 3593 | } |
| 3594 | llvm_unreachable("Unexpected runtime schedule"); |
| 3595 | } |
| 3596 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 3597 | /// Map the OpenMP distribute schedule to the runtime enumeration. |
| Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3598 | static OpenMPSchedType |
| 3599 | getRuntimeSchedule(OpenMPDistScheduleClauseKind ScheduleKind, bool Chunked) { |
| 3600 | // only static is allowed for dist_schedule |
| 3601 | return Chunked ? OMP_dist_sch_static_chunked : OMP_dist_sch_static; |
| 3602 | } |
| 3603 | |
| Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 3604 | bool CGOpenMPRuntime::isStaticNonchunked(OpenMPScheduleClauseKind ScheduleKind, |
| 3605 | bool Chunked) const { |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3606 | OpenMPSchedType Schedule = |
| 3607 | getRuntimeSchedule(ScheduleKind, Chunked, /*Ordered=*/false); |
| Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 3608 | return Schedule == OMP_sch_static; |
| 3609 | } |
| 3610 | |
| Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3611 | bool CGOpenMPRuntime::isStaticNonchunked( |
| 3612 | OpenMPDistScheduleClauseKind ScheduleKind, bool Chunked) const { |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3613 | OpenMPSchedType Schedule = getRuntimeSchedule(ScheduleKind, Chunked); |
| Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3614 | return Schedule == OMP_dist_sch_static; |
| 3615 | } |
| 3616 | |
| Gheorghe-Teodor Bercea | e925676 | 2018-10-29 15:45:47 +0000 | [diff] [blame] | 3617 | bool CGOpenMPRuntime::isStaticChunked(OpenMPScheduleClauseKind ScheduleKind, |
| 3618 | bool Chunked) const { |
| 3619 | OpenMPSchedType Schedule = |
| 3620 | getRuntimeSchedule(ScheduleKind, Chunked, /*Ordered=*/false); |
| 3621 | return Schedule == OMP_sch_static_chunked; |
| 3622 | } |
| 3623 | |
| 3624 | bool CGOpenMPRuntime::isStaticChunked( |
| 3625 | OpenMPDistScheduleClauseKind ScheduleKind, bool Chunked) const { |
| 3626 | OpenMPSchedType Schedule = getRuntimeSchedule(ScheduleKind, Chunked); |
| 3627 | return Schedule == OMP_dist_sch_static_chunked; |
| 3628 | } |
| Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3629 | |
| Alexander Musman | df7a8e2 | 2015-01-22 08:49:35 +0000 | [diff] [blame] | 3630 | bool CGOpenMPRuntime::isDynamic(OpenMPScheduleClauseKind ScheduleKind) const { |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3631 | OpenMPSchedType Schedule = |
| Alexey Bataev | d7589ffe | 2015-05-20 13:12:48 +0000 | [diff] [blame] | 3632 | getRuntimeSchedule(ScheduleKind, /*Chunked=*/false, /*Ordered=*/false); |
| Alexander Musman | df7a8e2 | 2015-01-22 08:49:35 +0000 | [diff] [blame] | 3633 | assert(Schedule != OMP_sch_static_chunked && "cannot be chunked here"); |
| 3634 | return Schedule != OMP_sch_static; |
| 3635 | } |
| 3636 | |
| Alexey Bataev | 07a3b59 | 2019-08-23 19:52:05 +0000 | [diff] [blame] | 3637 | static int addMonoNonMonoModifier(CodeGenModule &CGM, OpenMPSchedType Schedule, |
| Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 3638 | OpenMPScheduleClauseModifier M1, |
| 3639 | OpenMPScheduleClauseModifier M2) { |
| Alexey Bataev | 6cff624 | 2016-05-30 13:05:14 +0000 | [diff] [blame] | 3640 | int Modifier = 0; |
| Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 3641 | switch (M1) { |
| 3642 | case OMPC_SCHEDULE_MODIFIER_monotonic: |
| Alexey Bataev | 6cff624 | 2016-05-30 13:05:14 +0000 | [diff] [blame] | 3643 | Modifier = OMP_sch_modifier_monotonic; |
| 3644 | break; |
| Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 3645 | case OMPC_SCHEDULE_MODIFIER_nonmonotonic: |
| Alexey Bataev | 6cff624 | 2016-05-30 13:05:14 +0000 | [diff] [blame] | 3646 | Modifier = OMP_sch_modifier_nonmonotonic; |
| 3647 | break; |
| Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 3648 | case OMPC_SCHEDULE_MODIFIER_simd: |
| Alexey Bataev | 6cff624 | 2016-05-30 13:05:14 +0000 | [diff] [blame] | 3649 | if (Schedule == OMP_sch_static_chunked) |
| 3650 | Schedule = OMP_sch_static_balanced_chunked; |
| 3651 | break; |
| Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 3652 | case OMPC_SCHEDULE_MODIFIER_last: |
| 3653 | case OMPC_SCHEDULE_MODIFIER_unknown: |
| 3654 | break; |
| 3655 | } |
| 3656 | switch (M2) { |
| 3657 | case OMPC_SCHEDULE_MODIFIER_monotonic: |
| Alexey Bataev | 6cff624 | 2016-05-30 13:05:14 +0000 | [diff] [blame] | 3658 | Modifier = OMP_sch_modifier_monotonic; |
| 3659 | break; |
| Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 3660 | case OMPC_SCHEDULE_MODIFIER_nonmonotonic: |
| Alexey Bataev | 6cff624 | 2016-05-30 13:05:14 +0000 | [diff] [blame] | 3661 | Modifier = OMP_sch_modifier_nonmonotonic; |
| 3662 | break; |
| Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 3663 | case OMPC_SCHEDULE_MODIFIER_simd: |
| Alexey Bataev | 6cff624 | 2016-05-30 13:05:14 +0000 | [diff] [blame] | 3664 | if (Schedule == OMP_sch_static_chunked) |
| 3665 | Schedule = OMP_sch_static_balanced_chunked; |
| 3666 | break; |
| Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 3667 | case OMPC_SCHEDULE_MODIFIER_last: |
| 3668 | case OMPC_SCHEDULE_MODIFIER_unknown: |
| 3669 | break; |
| 3670 | } |
| Alexey Bataev | 07a3b59 | 2019-08-23 19:52:05 +0000 | [diff] [blame] | 3671 | // OpenMP 5.0, 2.9.2 Worksharing-Loop Construct, Desription. |
| 3672 | // If the static schedule kind is specified or if the ordered clause is |
| 3673 | // specified, and if the nonmonotonic modifier is not specified, the effect is |
| 3674 | // as if the monotonic modifier is specified. Otherwise, unless the monotonic |
| 3675 | // modifier is specified, the effect is as if the nonmonotonic modifier is |
| 3676 | // specified. |
| 3677 | if (CGM.getLangOpts().OpenMP >= 50 && Modifier == 0) { |
| 3678 | if (!(Schedule == OMP_sch_static_chunked || Schedule == OMP_sch_static || |
| 3679 | Schedule == OMP_sch_static_balanced_chunked || |
| Alexey Bataev | c3eded0 | 2019-11-18 11:13:08 -0500 | [diff] [blame] | 3680 | Schedule == OMP_ord_static_chunked || Schedule == OMP_ord_static || |
| 3681 | Schedule == OMP_dist_sch_static_chunked || |
| 3682 | Schedule == OMP_dist_sch_static)) |
| Alexey Bataev | 07a3b59 | 2019-08-23 19:52:05 +0000 | [diff] [blame] | 3683 | Modifier = OMP_sch_modifier_nonmonotonic; |
| 3684 | } |
| Alexey Bataev | 6cff624 | 2016-05-30 13:05:14 +0000 | [diff] [blame] | 3685 | return Schedule | Modifier; |
| Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 3686 | } |
| 3687 | |
| Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 3688 | void CGOpenMPRuntime::emitForDispatchInit( |
| 3689 | CodeGenFunction &CGF, SourceLocation Loc, |
| 3690 | const OpenMPScheduleTy &ScheduleKind, unsigned IVSize, bool IVSigned, |
| 3691 | bool Ordered, const DispatchRTInput &DispatchValues) { |
| Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 3692 | if (!CGF.HaveInsertPoint()) |
| 3693 | return; |
| Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 3694 | OpenMPSchedType Schedule = getRuntimeSchedule( |
| 3695 | ScheduleKind.Schedule, DispatchValues.Chunk != nullptr, Ordered); |
| John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3696 | assert(Ordered || |
| 3697 | (Schedule != OMP_sch_static && Schedule != OMP_sch_static_chunked && |
| Alexey Bataev | 6cff624 | 2016-05-30 13:05:14 +0000 | [diff] [blame] | 3698 | Schedule != OMP_ord_static && Schedule != OMP_ord_static_chunked && |
| 3699 | Schedule != OMP_sch_static_balanced_chunked)); |
| John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3700 | // Call __kmpc_dispatch_init( |
| 3701 | // ident_t *loc, kmp_int32 tid, kmp_int32 schedule, |
| 3702 | // kmp_int[32|64] lower, kmp_int[32|64] upper, |
| 3703 | // kmp_int[32|64] stride, kmp_int[32|64] chunk); |
| Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 3704 | |
| John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3705 | // If the Chunk was not specified in the clause - use default value 1. |
| Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 3706 | llvm::Value *Chunk = DispatchValues.Chunk ? DispatchValues.Chunk |
| 3707 | : CGF.Builder.getIntN(IVSize, 1); |
| John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3708 | llvm::Value *Args[] = { |
| Alexey Bataev | 07a3b59 | 2019-08-23 19:52:05 +0000 | [diff] [blame] | 3709 | emitUpdateLocation(CGF, Loc), |
| 3710 | getThreadID(CGF, Loc), |
| Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 3711 | CGF.Builder.getInt32(addMonoNonMonoModifier( |
| Alexey Bataev | 07a3b59 | 2019-08-23 19:52:05 +0000 | [diff] [blame] | 3712 | CGM, Schedule, ScheduleKind.M1, ScheduleKind.M2)), // Schedule type |
| 3713 | DispatchValues.LB, // Lower |
| 3714 | DispatchValues.UB, // Upper |
| 3715 | CGF.Builder.getIntN(IVSize, 1), // Stride |
| 3716 | Chunk // Chunk |
| John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3717 | }; |
| 3718 | CGF.EmitRuntimeCall(createDispatchInitFunction(IVSize, IVSigned), Args); |
| 3719 | } |
| 3720 | |
| Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 3721 | static void emitForStaticInitCall( |
| 3722 | CodeGenFunction &CGF, llvm::Value *UpdateLocation, llvm::Value *ThreadId, |
| James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 3723 | llvm::FunctionCallee ForStaticInitFunction, OpenMPSchedType Schedule, |
| Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 3724 | OpenMPScheduleClauseModifier M1, OpenMPScheduleClauseModifier M2, |
| Alexey Bataev | 0f87dbe | 2017-08-14 17:56:13 +0000 | [diff] [blame] | 3725 | const CGOpenMPRuntime::StaticRTInput &Values) { |
| Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3726 | if (!CGF.HaveInsertPoint()) |
| Alexey Bataev | 0f87dbe | 2017-08-14 17:56:13 +0000 | [diff] [blame] | 3727 | return; |
| Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3728 | |
| Alexey Bataev | 0f87dbe | 2017-08-14 17:56:13 +0000 | [diff] [blame] | 3729 | assert(!Values.Ordered); |
| 3730 | assert(Schedule == OMP_sch_static || Schedule == OMP_sch_static_chunked || |
| 3731 | Schedule == OMP_sch_static_balanced_chunked || |
| 3732 | Schedule == OMP_ord_static || Schedule == OMP_ord_static_chunked || |
| 3733 | Schedule == OMP_dist_sch_static || |
| 3734 | Schedule == OMP_dist_sch_static_chunked); |
| Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3735 | |
| Alexey Bataev | 0f87dbe | 2017-08-14 17:56:13 +0000 | [diff] [blame] | 3736 | // Call __kmpc_for_static_init( |
| 3737 | // ident_t *loc, kmp_int32 tid, kmp_int32 schedtype, |
| 3738 | // kmp_int32 *p_lastiter, kmp_int[32|64] *p_lower, |
| 3739 | // kmp_int[32|64] *p_upper, kmp_int[32|64] *p_stride, |
| 3740 | // kmp_int[32|64] incr, kmp_int[32|64] chunk); |
| 3741 | llvm::Value *Chunk = Values.Chunk; |
| 3742 | if (Chunk == nullptr) { |
| 3743 | assert((Schedule == OMP_sch_static || Schedule == OMP_ord_static || |
| 3744 | Schedule == OMP_dist_sch_static) && |
| 3745 | "expected static non-chunked schedule"); |
| 3746 | // If the Chunk was not specified in the clause - use default value 1. |
| 3747 | Chunk = CGF.Builder.getIntN(Values.IVSize, 1); |
| 3748 | } else { |
| 3749 | assert((Schedule == OMP_sch_static_chunked || |
| 3750 | Schedule == OMP_sch_static_balanced_chunked || |
| 3751 | Schedule == OMP_ord_static_chunked || |
| 3752 | Schedule == OMP_dist_sch_static_chunked) && |
| 3753 | "expected static chunked schedule"); |
| 3754 | } |
| 3755 | llvm::Value *Args[] = { |
| 3756 | UpdateLocation, |
| 3757 | ThreadId, |
| Alexey Bataev | 07a3b59 | 2019-08-23 19:52:05 +0000 | [diff] [blame] | 3758 | CGF.Builder.getInt32(addMonoNonMonoModifier(CGF.CGM, Schedule, M1, |
| Alexey Bataev | 0f87dbe | 2017-08-14 17:56:13 +0000 | [diff] [blame] | 3759 | M2)), // Schedule type |
| 3760 | Values.IL.getPointer(), // &isLastIter |
| 3761 | Values.LB.getPointer(), // &LB |
| 3762 | Values.UB.getPointer(), // &UB |
| 3763 | Values.ST.getPointer(), // &Stride |
| 3764 | CGF.Builder.getIntN(Values.IVSize, 1), // Incr |
| 3765 | Chunk // Chunk |
| 3766 | }; |
| 3767 | CGF.EmitRuntimeCall(ForStaticInitFunction, Args); |
| Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3768 | } |
| 3769 | |
| John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3770 | void CGOpenMPRuntime::emitForStaticInit(CodeGenFunction &CGF, |
| 3771 | SourceLocation Loc, |
| Alexey Bataev | 0f87dbe | 2017-08-14 17:56:13 +0000 | [diff] [blame] | 3772 | OpenMPDirectiveKind DKind, |
| Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 3773 | const OpenMPScheduleTy &ScheduleKind, |
| Alexey Bataev | 0f87dbe | 2017-08-14 17:56:13 +0000 | [diff] [blame] | 3774 | const StaticRTInput &Values) { |
| 3775 | OpenMPSchedType ScheduleNum = getRuntimeSchedule( |
| 3776 | ScheduleKind.Schedule, Values.Chunk != nullptr, Values.Ordered); |
| 3777 | assert(isOpenMPWorksharingDirective(DKind) && |
| 3778 | "Expected loop-based or sections-based directive."); |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3779 | llvm::Value *UpdatedLocation = emitUpdateLocation(CGF, Loc, |
| Alexey Bataev | 0f87dbe | 2017-08-14 17:56:13 +0000 | [diff] [blame] | 3780 | isOpenMPLoopDirective(DKind) |
| 3781 | ? OMP_IDENT_WORK_LOOP |
| 3782 | : OMP_IDENT_WORK_SECTIONS); |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3783 | llvm::Value *ThreadId = getThreadID(CGF, Loc); |
| James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 3784 | llvm::FunctionCallee StaticInitFunction = |
| Alexey Bataev | 0f87dbe | 2017-08-14 17:56:13 +0000 | [diff] [blame] | 3785 | createForStaticInitFunction(Values.IVSize, Values.IVSigned); |
| Alexey Bataev | c33ba8c | 2020-01-17 14:05:40 -0500 | [diff] [blame] | 3786 | auto DL = ApplyDebugLocation::CreateDefaultArtificial(CGF, Loc); |
| Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 3787 | emitForStaticInitCall(CGF, UpdatedLocation, ThreadId, StaticInitFunction, |
| Alexey Bataev | 0f87dbe | 2017-08-14 17:56:13 +0000 | [diff] [blame] | 3788 | ScheduleNum, ScheduleKind.M1, ScheduleKind.M2, Values); |
| Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3789 | } |
| John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3790 | |
| Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 3791 | void CGOpenMPRuntime::emitDistributeStaticInit( |
| 3792 | CodeGenFunction &CGF, SourceLocation Loc, |
| Alexey Bataev | 0f87dbe | 2017-08-14 17:56:13 +0000 | [diff] [blame] | 3793 | OpenMPDistScheduleClauseKind SchedKind, |
| 3794 | const CGOpenMPRuntime::StaticRTInput &Values) { |
| 3795 | OpenMPSchedType ScheduleNum = |
| 3796 | getRuntimeSchedule(SchedKind, Values.Chunk != nullptr); |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3797 | llvm::Value *UpdatedLocation = |
| Alexey Bataev | 0f87dbe | 2017-08-14 17:56:13 +0000 | [diff] [blame] | 3798 | emitUpdateLocation(CGF, Loc, OMP_IDENT_WORK_DISTRIBUTE); |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3799 | llvm::Value *ThreadId = getThreadID(CGF, Loc); |
| James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 3800 | llvm::FunctionCallee StaticInitFunction = |
| Alexey Bataev | 0f87dbe | 2017-08-14 17:56:13 +0000 | [diff] [blame] | 3801 | createForStaticInitFunction(Values.IVSize, Values.IVSigned); |
| Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 3802 | emitForStaticInitCall(CGF, UpdatedLocation, ThreadId, StaticInitFunction, |
| 3803 | ScheduleNum, OMPC_SCHEDULE_MODIFIER_unknown, |
| Alexey Bataev | 0f87dbe | 2017-08-14 17:56:13 +0000 | [diff] [blame] | 3804 | OMPC_SCHEDULE_MODIFIER_unknown, Values); |
| Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 3805 | } |
| 3806 | |
| Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 3807 | void CGOpenMPRuntime::emitForStaticFinish(CodeGenFunction &CGF, |
| Alexey Bataev | f43f714 | 2017-09-06 16:17:35 +0000 | [diff] [blame] | 3808 | SourceLocation Loc, |
| 3809 | OpenMPDirectiveKind DKind) { |
| Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 3810 | if (!CGF.HaveInsertPoint()) |
| 3811 | return; |
| Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 3812 | // Call __kmpc_for_static_fini(ident_t *loc, kmp_int32 tid); |
| Alexey Bataev | f43f714 | 2017-09-06 16:17:35 +0000 | [diff] [blame] | 3813 | llvm::Value *Args[] = { |
| 3814 | emitUpdateLocation(CGF, Loc, |
| 3815 | isOpenMPDistributeDirective(DKind) |
| 3816 | ? OMP_IDENT_WORK_DISTRIBUTE |
| 3817 | : isOpenMPLoopDirective(DKind) |
| 3818 | ? OMP_IDENT_WORK_LOOP |
| 3819 | : OMP_IDENT_WORK_SECTIONS), |
| 3820 | getThreadID(CGF, Loc)}; |
| Alexey Bataev | c33ba8c | 2020-01-17 14:05:40 -0500 | [diff] [blame] | 3821 | auto DL = ApplyDebugLocation::CreateDefaultArtificial(CGF, Loc); |
| Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 3822 | CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_for_static_fini), |
| 3823 | Args); |
| Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 3824 | } |
| 3825 | |
| Alexey Bataev | d7589ffe | 2015-05-20 13:12:48 +0000 | [diff] [blame] | 3826 | void CGOpenMPRuntime::emitForOrderedIterationEnd(CodeGenFunction &CGF, |
| 3827 | SourceLocation Loc, |
| 3828 | unsigned IVSize, |
| 3829 | bool IVSigned) { |
| Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 3830 | if (!CGF.HaveInsertPoint()) |
| 3831 | return; |
| Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 3832 | // Call __kmpc_for_dynamic_fini_(4|8)[u](ident_t *loc, kmp_int32 tid); |
| Alexey Bataev | 50b3c95 | 2016-02-19 10:38:26 +0000 | [diff] [blame] | 3833 | llvm::Value *Args[] = {emitUpdateLocation(CGF, Loc), getThreadID(CGF, Loc)}; |
| Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 3834 | CGF.EmitRuntimeCall(createDispatchFiniFunction(IVSize, IVSigned), Args); |
| 3835 | } |
| 3836 | |
| Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 3837 | llvm::Value *CGOpenMPRuntime::emitForNext(CodeGenFunction &CGF, |
| 3838 | SourceLocation Loc, unsigned IVSize, |
| John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3839 | bool IVSigned, Address IL, |
| 3840 | Address LB, Address UB, |
| 3841 | Address ST) { |
| Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 3842 | // Call __kmpc_dispatch_next( |
| 3843 | // ident_t *loc, kmp_int32 tid, kmp_int32 *p_lastiter, |
| 3844 | // kmp_int[32|64] *p_lower, kmp_int[32|64] *p_upper, |
| 3845 | // kmp_int[32|64] *p_stride); |
| 3846 | llvm::Value *Args[] = { |
| Alexey Bataev | 50b3c95 | 2016-02-19 10:38:26 +0000 | [diff] [blame] | 3847 | emitUpdateLocation(CGF, Loc), |
| 3848 | getThreadID(CGF, Loc), |
| John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3849 | IL.getPointer(), // &isLastIter |
| 3850 | LB.getPointer(), // &Lower |
| 3851 | UB.getPointer(), // &Upper |
| 3852 | ST.getPointer() // &Stride |
| Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 3853 | }; |
| 3854 | llvm::Value *Call = |
| 3855 | CGF.EmitRuntimeCall(createDispatchNextFunction(IVSize, IVSigned), Args); |
| 3856 | return CGF.EmitScalarConversion( |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3857 | Call, CGF.getContext().getIntTypeForBitwidth(32, /*Signed=*/1), |
| Filipe Cabecinhas | 7af183d | 2015-08-11 04:19:28 +0000 | [diff] [blame] | 3858 | CGF.getContext().BoolTy, Loc); |
| Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 3859 | } |
| 3860 | |
| Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 3861 | void CGOpenMPRuntime::emitNumThreadsClause(CodeGenFunction &CGF, |
| 3862 | llvm::Value *NumThreads, |
| 3863 | SourceLocation Loc) { |
| Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 3864 | if (!CGF.HaveInsertPoint()) |
| 3865 | return; |
| Alexey Bataev | b205978 | 2014-10-13 08:23:51 +0000 | [diff] [blame] | 3866 | // Build call __kmpc_push_num_threads(&loc, global_tid, num_threads) |
| 3867 | llvm::Value *Args[] = { |
| Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 3868 | emitUpdateLocation(CGF, Loc), getThreadID(CGF, Loc), |
| Alexey Bataev | b205978 | 2014-10-13 08:23:51 +0000 | [diff] [blame] | 3869 | CGF.Builder.CreateIntCast(NumThreads, CGF.Int32Ty, /*isSigned*/ true)}; |
| Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 3870 | CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_push_num_threads), |
| 3871 | Args); |
| Alexey Bataev | b205978 | 2014-10-13 08:23:51 +0000 | [diff] [blame] | 3872 | } |
| 3873 | |
| Alexey Bataev | 7f210c6 | 2015-06-18 13:40:03 +0000 | [diff] [blame] | 3874 | void CGOpenMPRuntime::emitProcBindClause(CodeGenFunction &CGF, |
| Johannes Doerfert | 6c5d1f40 | 2019-12-25 18:15:36 -0600 | [diff] [blame] | 3875 | ProcBindKind ProcBind, |
| Alexey Bataev | 7f210c6 | 2015-06-18 13:40:03 +0000 | [diff] [blame] | 3876 | SourceLocation Loc) { |
| Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 3877 | if (!CGF.HaveInsertPoint()) |
| 3878 | return; |
| Johannes Doerfert | 6c5d1f40 | 2019-12-25 18:15:36 -0600 | [diff] [blame] | 3879 | assert(ProcBind != OMP_PROC_BIND_unknown && "Unsupported proc_bind value."); |
| Alexey Bataev | 7f210c6 | 2015-06-18 13:40:03 +0000 | [diff] [blame] | 3880 | // Build call __kmpc_push_proc_bind(&loc, global_tid, proc_bind) |
| 3881 | llvm::Value *Args[] = { |
| 3882 | emitUpdateLocation(CGF, Loc), getThreadID(CGF, Loc), |
| Johannes Doerfert | 6c5d1f40 | 2019-12-25 18:15:36 -0600 | [diff] [blame] | 3883 | llvm::ConstantInt::get(CGM.IntTy, unsigned(ProcBind), /*isSigned=*/true)}; |
| Alexey Bataev | 7f210c6 | 2015-06-18 13:40:03 +0000 | [diff] [blame] | 3884 | CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_push_proc_bind), Args); |
| 3885 | } |
| 3886 | |
| Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 3887 | void CGOpenMPRuntime::emitFlush(CodeGenFunction &CGF, ArrayRef<const Expr *>, |
| Alexey Bataev | e8e05de | 2020-02-07 12:22:23 -0500 | [diff] [blame] | 3888 | SourceLocation Loc, llvm::AtomicOrdering AO) { |
| Kiran Chandramohan | a969e05 | 2020-02-04 21:43:40 +0000 | [diff] [blame] | 3889 | llvm::OpenMPIRBuilder *OMPBuilder = CGF.CGM.getOpenMPIRBuilder(); |
| 3890 | if (OMPBuilder) { |
| 3891 | OMPBuilder->CreateFlush(CGF.Builder); |
| 3892 | } else { |
| 3893 | if (!CGF.HaveInsertPoint()) |
| 3894 | return; |
| 3895 | // Build call void __kmpc_flush(ident_t *loc) |
| 3896 | CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_flush), |
| 3897 | emitUpdateLocation(CGF, Loc)); |
| 3898 | } |
| Alexey Bataev | cc37cc1 | 2014-11-20 04:34:54 +0000 | [diff] [blame] | 3899 | } |
| Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 3900 | |
| Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 3901 | namespace { |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 3902 | /// Indexes of fields for type kmp_task_t. |
| Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 3903 | enum KmpTaskTFields { |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 3904 | /// List of shared variables. |
| Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 3905 | KmpTaskTShareds, |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 3906 | /// Task routine. |
| Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 3907 | KmpTaskTRoutine, |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 3908 | /// Partition id for the untied tasks. |
| Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 3909 | KmpTaskTPartId, |
| Alexey Bataev | ad537bb | 2016-05-30 09:06:50 +0000 | [diff] [blame] | 3910 | /// Function with call of destructors for private variables. |
| 3911 | Data1, |
| 3912 | /// Task priority. |
| 3913 | Data2, |
| Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 3914 | /// (Taskloops only) Lower bound. |
| 3915 | KmpTaskTLowerBound, |
| 3916 | /// (Taskloops only) Upper bound. |
| 3917 | KmpTaskTUpperBound, |
| 3918 | /// (Taskloops only) Stride. |
| 3919 | KmpTaskTStride, |
| 3920 | /// (Taskloops only) Is last iteration flag. |
| 3921 | KmpTaskTLastIter, |
| Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 3922 | /// (Taskloops only) Reduction data. |
| 3923 | KmpTaskTReductions, |
| Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 3924 | }; |
| Hans Wennborg | 7eb5464 | 2015-09-10 17:07:54 +0000 | [diff] [blame] | 3925 | } // anonymous namespace |
| Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 3926 | |
| Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3927 | bool CGOpenMPRuntime::OffloadEntriesInfoManagerTy::empty() const { |
| Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 3928 | return OffloadEntriesTargetRegion.empty() && |
| 3929 | OffloadEntriesDeviceGlobalVar.empty(); |
| Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3930 | } |
| 3931 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 3932 | /// Initialize target region entry. |
| Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3933 | void CGOpenMPRuntime::OffloadEntriesInfoManagerTy:: |
| 3934 | initializeTargetRegionEntryInfo(unsigned DeviceID, unsigned FileID, |
| 3935 | StringRef ParentName, unsigned LineNum, |
| Samuel Antao | 2de62b0 | 2016-02-13 23:35:10 +0000 | [diff] [blame] | 3936 | unsigned Order) { |
| Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3937 | assert(CGM.getLangOpts().OpenMPIsDevice && "Initialization of entries is " |
| 3938 | "only required for the device " |
| 3939 | "code generation."); |
| Samuel Antao | 2de62b0 | 2016-02-13 23:35:10 +0000 | [diff] [blame] | 3940 | OffloadEntriesTargetRegion[DeviceID][FileID][ParentName][LineNum] = |
| Samuel Antao | f83efdb | 2017-01-05 16:02:49 +0000 | [diff] [blame] | 3941 | OffloadEntryInfoTargetRegion(Order, /*Addr=*/nullptr, /*ID=*/nullptr, |
| Alexey Bataev | 34f8a70 | 2018-03-28 14:28:54 +0000 | [diff] [blame] | 3942 | OMPTargetRegionEntryTargetRegion); |
| Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3943 | ++OffloadingEntriesNum; |
| 3944 | } |
| 3945 | |
| 3946 | void CGOpenMPRuntime::OffloadEntriesInfoManagerTy:: |
| 3947 | registerTargetRegionEntryInfo(unsigned DeviceID, unsigned FileID, |
| 3948 | StringRef ParentName, unsigned LineNum, |
| Samuel Antao | f83efdb | 2017-01-05 16:02:49 +0000 | [diff] [blame] | 3949 | llvm::Constant *Addr, llvm::Constant *ID, |
| Alexey Bataev | 34f8a70 | 2018-03-28 14:28:54 +0000 | [diff] [blame] | 3950 | OMPTargetRegionEntryKind Flags) { |
| Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3951 | // If we are emitting code for a target, the entry is already initialized, |
| 3952 | // only has to be registered. |
| 3953 | if (CGM.getLangOpts().OpenMPIsDevice) { |
| Alexey Bataev | 64e62dc | 2018-04-30 16:26:57 +0000 | [diff] [blame] | 3954 | if (!hasTargetRegionEntryInfo(DeviceID, FileID, ParentName, LineNum)) { |
| 3955 | unsigned DiagID = CGM.getDiags().getCustomDiagID( |
| 3956 | DiagnosticsEngine::Error, |
| 3957 | "Unable to find target region on line '%0' in the device code."); |
| 3958 | CGM.getDiags().Report(DiagID) << LineNum; |
| 3959 | return; |
| 3960 | } |
| Samuel Antao | 2de62b0 | 2016-02-13 23:35:10 +0000 | [diff] [blame] | 3961 | auto &Entry = |
| 3962 | OffloadEntriesTargetRegion[DeviceID][FileID][ParentName][LineNum]; |
| Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3963 | assert(Entry.isValid() && "Entry not initialized!"); |
| 3964 | Entry.setAddress(Addr); |
| 3965 | Entry.setID(ID); |
| Samuel Antao | f83efdb | 2017-01-05 16:02:49 +0000 | [diff] [blame] | 3966 | Entry.setFlags(Flags); |
| Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3967 | } else { |
| Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 3968 | OffloadEntryInfoTargetRegion Entry(OffloadingEntriesNum, Addr, ID, Flags); |
| Samuel Antao | 2de62b0 | 2016-02-13 23:35:10 +0000 | [diff] [blame] | 3969 | OffloadEntriesTargetRegion[DeviceID][FileID][ParentName][LineNum] = Entry; |
| Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 3970 | ++OffloadingEntriesNum; |
| Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3971 | } |
| 3972 | } |
| 3973 | |
| 3974 | bool CGOpenMPRuntime::OffloadEntriesInfoManagerTy::hasTargetRegionEntryInfo( |
| Samuel Antao | 2de62b0 | 2016-02-13 23:35:10 +0000 | [diff] [blame] | 3975 | unsigned DeviceID, unsigned FileID, StringRef ParentName, |
| 3976 | unsigned LineNum) const { |
| Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3977 | auto PerDevice = OffloadEntriesTargetRegion.find(DeviceID); |
| 3978 | if (PerDevice == OffloadEntriesTargetRegion.end()) |
| 3979 | return false; |
| 3980 | auto PerFile = PerDevice->second.find(FileID); |
| 3981 | if (PerFile == PerDevice->second.end()) |
| 3982 | return false; |
| 3983 | auto PerParentName = PerFile->second.find(ParentName); |
| 3984 | if (PerParentName == PerFile->second.end()) |
| 3985 | return false; |
| 3986 | auto PerLine = PerParentName->second.find(LineNum); |
| 3987 | if (PerLine == PerParentName->second.end()) |
| 3988 | return false; |
| Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3989 | // Fail if this entry is already registered. |
| Samuel Antao | 2de62b0 | 2016-02-13 23:35:10 +0000 | [diff] [blame] | 3990 | if (PerLine->second.getAddress() || PerLine->second.getID()) |
| Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3991 | return false; |
| 3992 | return true; |
| 3993 | } |
| 3994 | |
| 3995 | void CGOpenMPRuntime::OffloadEntriesInfoManagerTy::actOnTargetRegionEntriesInfo( |
| 3996 | const OffloadTargetRegionEntryInfoActTy &Action) { |
| 3997 | // Scan all target region entries and perform the provided action. |
| Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 3998 | for (const auto &D : OffloadEntriesTargetRegion) |
| 3999 | for (const auto &F : D.second) |
| 4000 | for (const auto &P : F.second) |
| 4001 | for (const auto &L : P.second) |
| Samuel Antao | 2de62b0 | 2016-02-13 23:35:10 +0000 | [diff] [blame] | 4002 | Action(D.first, F.first, P.first(), L.first, L.second); |
| Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4003 | } |
| 4004 | |
| Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 4005 | void CGOpenMPRuntime::OffloadEntriesInfoManagerTy:: |
| 4006 | initializeDeviceGlobalVarEntryInfo(StringRef Name, |
| 4007 | OMPTargetGlobalVarEntryKind Flags, |
| 4008 | unsigned Order) { |
| 4009 | assert(CGM.getLangOpts().OpenMPIsDevice && "Initialization of entries is " |
| 4010 | "only required for the device " |
| 4011 | "code generation."); |
| 4012 | OffloadEntriesDeviceGlobalVar.try_emplace(Name, Order, Flags); |
| 4013 | ++OffloadingEntriesNum; |
| 4014 | } |
| Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4015 | |
| Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 4016 | void CGOpenMPRuntime::OffloadEntriesInfoManagerTy:: |
| 4017 | registerDeviceGlobalVarEntryInfo(StringRef VarName, llvm::Constant *Addr, |
| 4018 | CharUnits VarSize, |
| 4019 | OMPTargetGlobalVarEntryKind Flags, |
| 4020 | llvm::GlobalValue::LinkageTypes Linkage) { |
| 4021 | if (CGM.getLangOpts().OpenMPIsDevice) { |
| 4022 | auto &Entry = OffloadEntriesDeviceGlobalVar[VarName]; |
| 4023 | assert(Entry.isValid() && Entry.getFlags() == Flags && |
| 4024 | "Entry not initialized!"); |
| 4025 | assert((!Entry.getAddress() || Entry.getAddress() == Addr) && |
| 4026 | "Resetting with the new address."); |
| Alexey Bataev | 8259cc3 | 2019-03-12 20:05:17 +0000 | [diff] [blame] | 4027 | if (Entry.getAddress() && hasDeviceGlobalVarEntryInfo(VarName)) { |
| 4028 | if (Entry.getVarSize().isZero()) { |
| 4029 | Entry.setVarSize(VarSize); |
| 4030 | Entry.setLinkage(Linkage); |
| 4031 | } |
| Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 4032 | return; |
| Alexey Bataev | 8259cc3 | 2019-03-12 20:05:17 +0000 | [diff] [blame] | 4033 | } |
| Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 4034 | Entry.setVarSize(VarSize); |
| 4035 | Entry.setLinkage(Linkage); |
| Alexey Bataev | 8259cc3 | 2019-03-12 20:05:17 +0000 | [diff] [blame] | 4036 | Entry.setAddress(Addr); |
| Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 4037 | } else { |
| Alexey Bataev | 8259cc3 | 2019-03-12 20:05:17 +0000 | [diff] [blame] | 4038 | if (hasDeviceGlobalVarEntryInfo(VarName)) { |
| 4039 | auto &Entry = OffloadEntriesDeviceGlobalVar[VarName]; |
| 4040 | assert(Entry.isValid() && Entry.getFlags() == Flags && |
| 4041 | "Entry not initialized!"); |
| 4042 | assert((!Entry.getAddress() || Entry.getAddress() == Addr) && |
| 4043 | "Resetting with the new address."); |
| 4044 | if (Entry.getVarSize().isZero()) { |
| 4045 | Entry.setVarSize(VarSize); |
| 4046 | Entry.setLinkage(Linkage); |
| 4047 | } |
| Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 4048 | return; |
| Alexey Bataev | 8259cc3 | 2019-03-12 20:05:17 +0000 | [diff] [blame] | 4049 | } |
| Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 4050 | OffloadEntriesDeviceGlobalVar.try_emplace( |
| 4051 | VarName, OffloadingEntriesNum, Addr, VarSize, Flags, Linkage); |
| 4052 | ++OffloadingEntriesNum; |
| 4053 | } |
| 4054 | } |
| 4055 | |
| 4056 | void CGOpenMPRuntime::OffloadEntriesInfoManagerTy:: |
| 4057 | actOnDeviceGlobalVarEntriesInfo( |
| 4058 | const OffloadDeviceGlobalVarEntryInfoActTy &Action) { |
| 4059 | // Scan all target region entries and perform the provided action. |
| 4060 | for (const auto &E : OffloadEntriesDeviceGlobalVar) |
| 4061 | Action(E.getKey(), E.getValue()); |
| Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4062 | } |
| 4063 | |
| Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 4064 | void CGOpenMPRuntime::createOffloadEntry( |
| 4065 | llvm::Constant *ID, llvm::Constant *Addr, uint64_t Size, int32_t Flags, |
| 4066 | llvm::GlobalValue::LinkageTypes Linkage) { |
| Samuel Antao | 2de62b0 | 2016-02-13 23:35:10 +0000 | [diff] [blame] | 4067 | StringRef Name = Addr->getName(); |
| Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4068 | llvm::Module &M = CGM.getModule(); |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4069 | llvm::LLVMContext &C = M.getContext(); |
| Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4070 | |
| 4071 | // Create constant string with the name. |
| 4072 | llvm::Constant *StrPtrInit = llvm::ConstantDataArray::getString(C, Name); |
| 4073 | |
| Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 4074 | std::string StringName = getName({"omp_offloading", "entry_name"}); |
| 4075 | auto *Str = new llvm::GlobalVariable( |
| 4076 | M, StrPtrInit->getType(), /*isConstant=*/true, |
| 4077 | llvm::GlobalValue::InternalLinkage, StrPtrInit, StringName); |
| Peter Collingbourne | bcf909d | 2016-06-14 21:02:05 +0000 | [diff] [blame] | 4078 | Str->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global); |
| Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4079 | |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4080 | llvm::Constant *Data[] = {llvm::ConstantExpr::getBitCast(ID, CGM.VoidPtrTy), |
| 4081 | llvm::ConstantExpr::getBitCast(Str, CGM.Int8PtrTy), |
| 4082 | llvm::ConstantInt::get(CGM.SizeTy, Size), |
| 4083 | llvm::ConstantInt::get(CGM.Int32Ty, Flags), |
| 4084 | llvm::ConstantInt::get(CGM.Int32Ty, 0)}; |
| Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 4085 | std::string EntryName = getName({"omp_offloading", "entry", ""}); |
| Mike Rice | e1ca7b6 | 2018-08-29 15:45:11 +0000 | [diff] [blame] | 4086 | llvm::GlobalVariable *Entry = createGlobalStruct( |
| 4087 | CGM, getTgtOffloadEntryQTy(), /*IsConstant=*/true, Data, |
| 4088 | Twine(EntryName).concat(Name), llvm::GlobalValue::WeakAnyLinkage); |
| Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4089 | |
| 4090 | // The entry has to be created in the section the linker expects it to be. |
| Sergey Dmitriev | 4b343fd | 2019-09-27 20:00:51 +0000 | [diff] [blame] | 4091 | Entry->setSection("omp_offloading_entries"); |
| Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4092 | } |
| 4093 | |
| 4094 | void CGOpenMPRuntime::createOffloadEntriesAndInfoMetadata() { |
| 4095 | // Emit the offloading entries and metadata so that the device codegen side |
| Samuel Antao | 4c8035b | 2016-12-12 18:00:20 +0000 | [diff] [blame] | 4096 | // can easily figure out what to emit. The produced metadata looks like |
| 4097 | // this: |
| Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4098 | // |
| 4099 | // !omp_offload.info = !{!1, ...} |
| 4100 | // |
| 4101 | // Right now we only generate metadata for function that contain target |
| 4102 | // regions. |
| 4103 | |
| Sergey Dmitriev | 5836c35 | 2019-10-15 18:42:47 +0000 | [diff] [blame] | 4104 | // If we are in simd mode or there are no entries, we don't need to do |
| 4105 | // anything. |
| 4106 | if (CGM.getLangOpts().OpenMPSimd || OffloadEntriesInfoManager.empty()) |
| Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4107 | return; |
| 4108 | |
| 4109 | llvm::Module &M = CGM.getModule(); |
| 4110 | llvm::LLVMContext &C = M.getContext(); |
| Alexey Bataev | ba643691d | 2019-10-03 16:20:34 +0000 | [diff] [blame] | 4111 | SmallVector<std::tuple<const OffloadEntriesInfoManagerTy::OffloadEntryInfo *, |
| 4112 | SourceLocation, StringRef>, |
| 4113 | 16> |
| Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4114 | OrderedEntries(OffloadEntriesInfoManager.size()); |
| Alexey Bataev | 2a6f3f5 | 2018-11-07 19:11:14 +0000 | [diff] [blame] | 4115 | llvm::SmallVector<StringRef, 16> ParentFunctions( |
| 4116 | OffloadEntriesInfoManager.size()); |
| Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4117 | |
| Simon Pilgrim | 2c51880 | 2017-03-30 14:13:19 +0000 | [diff] [blame] | 4118 | // Auxiliary methods to create metadata values and strings. |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4119 | auto &&GetMDInt = [this](unsigned V) { |
| Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4120 | return llvm::ConstantAsMetadata::get( |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4121 | llvm::ConstantInt::get(CGM.Int32Ty, V)); |
| Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4122 | }; |
| 4123 | |
| Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 4124 | auto &&GetMDString = [&C](StringRef V) { return llvm::MDString::get(C, V); }; |
| 4125 | |
| 4126 | // Create the offloading info metadata node. |
| 4127 | llvm::NamedMDNode *MD = M.getOrInsertNamedMetadata("omp_offload.info"); |
| Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4128 | |
| 4129 | // Create function that emits metadata for each target region entry; |
| Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 4130 | auto &&TargetRegionMetadataEmitter = |
| Alexey Bataev | ba643691d | 2019-10-03 16:20:34 +0000 | [diff] [blame] | 4131 | [this, &C, MD, &OrderedEntries, &ParentFunctions, &GetMDInt, |
| 4132 | &GetMDString]( |
| Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 4133 | unsigned DeviceID, unsigned FileID, StringRef ParentName, |
| 4134 | unsigned Line, |
| 4135 | const OffloadEntriesInfoManagerTy::OffloadEntryInfoTargetRegion &E) { |
| 4136 | // Generate metadata for target regions. Each entry of this metadata |
| 4137 | // contains: |
| 4138 | // - Entry 0 -> Kind of this type of metadata (0). |
| 4139 | // - Entry 1 -> Device ID of the file where the entry was identified. |
| 4140 | // - Entry 2 -> File ID of the file where the entry was identified. |
| 4141 | // - Entry 3 -> Mangled name of the function where the entry was |
| 4142 | // identified. |
| 4143 | // - Entry 4 -> Line in the file where the entry was identified. |
| 4144 | // - Entry 5 -> Order the entry was created. |
| 4145 | // The first element of the metadata node is the kind. |
| 4146 | llvm::Metadata *Ops[] = {GetMDInt(E.getKind()), GetMDInt(DeviceID), |
| 4147 | GetMDInt(FileID), GetMDString(ParentName), |
| 4148 | GetMDInt(Line), GetMDInt(E.getOrder())}; |
| Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4149 | |
| Alexey Bataev | ba643691d | 2019-10-03 16:20:34 +0000 | [diff] [blame] | 4150 | SourceLocation Loc; |
| 4151 | for (auto I = CGM.getContext().getSourceManager().fileinfo_begin(), |
| 4152 | E = CGM.getContext().getSourceManager().fileinfo_end(); |
| 4153 | I != E; ++I) { |
| 4154 | if (I->getFirst()->getUniqueID().getDevice() == DeviceID && |
| 4155 | I->getFirst()->getUniqueID().getFile() == FileID) { |
| 4156 | Loc = CGM.getContext().getSourceManager().translateFileLineCol( |
| 4157 | I->getFirst(), Line, 1); |
| 4158 | break; |
| 4159 | } |
| 4160 | } |
| Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 4161 | // Save this entry in the right position of the ordered entries array. |
| Alexey Bataev | ba643691d | 2019-10-03 16:20:34 +0000 | [diff] [blame] | 4162 | OrderedEntries[E.getOrder()] = std::make_tuple(&E, Loc, ParentName); |
| Alexey Bataev | 2a6f3f5 | 2018-11-07 19:11:14 +0000 | [diff] [blame] | 4163 | ParentFunctions[E.getOrder()] = ParentName; |
| Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4164 | |
| Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 4165 | // Add metadata to the named metadata node. |
| 4166 | MD->addOperand(llvm::MDNode::get(C, Ops)); |
| 4167 | }; |
| Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4168 | |
| 4169 | OffloadEntriesInfoManager.actOnTargetRegionEntriesInfo( |
| 4170 | TargetRegionMetadataEmitter); |
| 4171 | |
| Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 4172 | // Create function that emits metadata for each device global variable entry; |
| 4173 | auto &&DeviceGlobalVarMetadataEmitter = |
| 4174 | [&C, &OrderedEntries, &GetMDInt, &GetMDString, |
| 4175 | MD](StringRef MangledName, |
| 4176 | const OffloadEntriesInfoManagerTy::OffloadEntryInfoDeviceGlobalVar |
| 4177 | &E) { |
| 4178 | // Generate metadata for global variables. Each entry of this metadata |
| 4179 | // contains: |
| 4180 | // - Entry 0 -> Kind of this type of metadata (1). |
| 4181 | // - Entry 1 -> Mangled name of the variable. |
| 4182 | // - Entry 2 -> Declare target kind. |
| 4183 | // - Entry 3 -> Order the entry was created. |
| 4184 | // The first element of the metadata node is the kind. |
| 4185 | llvm::Metadata *Ops[] = { |
| 4186 | GetMDInt(E.getKind()), GetMDString(MangledName), |
| 4187 | GetMDInt(E.getFlags()), GetMDInt(E.getOrder())}; |
| 4188 | |
| 4189 | // Save this entry in the right position of the ordered entries array. |
| Alexey Bataev | ba643691d | 2019-10-03 16:20:34 +0000 | [diff] [blame] | 4190 | OrderedEntries[E.getOrder()] = |
| 4191 | std::make_tuple(&E, SourceLocation(), MangledName); |
| Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 4192 | |
| 4193 | // Add metadata to the named metadata node. |
| 4194 | MD->addOperand(llvm::MDNode::get(C, Ops)); |
| 4195 | }; |
| 4196 | |
| 4197 | OffloadEntriesInfoManager.actOnDeviceGlobalVarEntriesInfo( |
| 4198 | DeviceGlobalVarMetadataEmitter); |
| 4199 | |
| Alexey Bataev | ba643691d | 2019-10-03 16:20:34 +0000 | [diff] [blame] | 4200 | for (const auto &E : OrderedEntries) { |
| 4201 | assert(std::get<0>(E) && "All ordered entries must exist!"); |
| Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 4202 | if (const auto *CE = |
| Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4203 | dyn_cast<OffloadEntriesInfoManagerTy::OffloadEntryInfoTargetRegion>( |
| Alexey Bataev | ba643691d | 2019-10-03 16:20:34 +0000 | [diff] [blame] | 4204 | std::get<0>(E))) { |
| Alexey Bataev | 64e62dc | 2018-04-30 16:26:57 +0000 | [diff] [blame] | 4205 | if (!CE->getID() || !CE->getAddress()) { |
| Alexey Bataev | 2a6f3f5 | 2018-11-07 19:11:14 +0000 | [diff] [blame] | 4206 | // Do not blame the entry if the parent funtion is not emitted. |
| 4207 | StringRef FnName = ParentFunctions[CE->getOrder()]; |
| 4208 | if (!CGM.GetGlobalValue(FnName)) |
| 4209 | continue; |
| Alexey Bataev | 64e62dc | 2018-04-30 16:26:57 +0000 | [diff] [blame] | 4210 | unsigned DiagID = CGM.getDiags().getCustomDiagID( |
| 4211 | DiagnosticsEngine::Error, |
| Alexey Bataev | ba643691d | 2019-10-03 16:20:34 +0000 | [diff] [blame] | 4212 | "Offloading entry for target region in %0 is incorrect: either the " |
| Alexey Bataev | 64e62dc | 2018-04-30 16:26:57 +0000 | [diff] [blame] | 4213 | "address or the ID is invalid."); |
| Alexey Bataev | ba643691d | 2019-10-03 16:20:34 +0000 | [diff] [blame] | 4214 | CGM.getDiags().Report(std::get<1>(E), DiagID) << FnName; |
| Alexey Bataev | 64e62dc | 2018-04-30 16:26:57 +0000 | [diff] [blame] | 4215 | continue; |
| 4216 | } |
| Alexey Bataev | 34f8a70 | 2018-03-28 14:28:54 +0000 | [diff] [blame] | 4217 | createOffloadEntry(CE->getID(), CE->getAddress(), /*Size=*/0, |
| Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 4218 | CE->getFlags(), llvm::GlobalValue::WeakAnyLinkage); |
| Alexey Bataev | ba643691d | 2019-10-03 16:20:34 +0000 | [diff] [blame] | 4219 | } else if (const auto *CE = dyn_cast<OffloadEntriesInfoManagerTy:: |
| 4220 | OffloadEntryInfoDeviceGlobalVar>( |
| 4221 | std::get<0>(E))) { |
| Alexey Bataev | c52f01d | 2018-07-16 20:05:25 +0000 | [diff] [blame] | 4222 | OffloadEntriesInfoManagerTy::OMPTargetGlobalVarEntryKind Flags = |
| 4223 | static_cast<OffloadEntriesInfoManagerTy::OMPTargetGlobalVarEntryKind>( |
| 4224 | CE->getFlags()); |
| 4225 | switch (Flags) { |
| 4226 | case OffloadEntriesInfoManagerTy::OMPTargetGlobalVarEntryTo: { |
| Gheorghe-Teodor Bercea | 0034e84 | 2019-06-20 18:04:47 +0000 | [diff] [blame] | 4227 | if (CGM.getLangOpts().OpenMPIsDevice && |
| 4228 | CGM.getOpenMPRuntime().hasRequiresUnifiedSharedMemory()) |
| 4229 | continue; |
| Alexey Bataev | c52f01d | 2018-07-16 20:05:25 +0000 | [diff] [blame] | 4230 | if (!CE->getAddress()) { |
| 4231 | unsigned DiagID = CGM.getDiags().getCustomDiagID( |
| Alexey Bataev | ba643691d | 2019-10-03 16:20:34 +0000 | [diff] [blame] | 4232 | DiagnosticsEngine::Error, "Offloading entry for declare target " |
| 4233 | "variable %0 is incorrect: the " |
| 4234 | "address is invalid."); |
| 4235 | CGM.getDiags().Report(std::get<1>(E), DiagID) << std::get<2>(E); |
| Alexey Bataev | c52f01d | 2018-07-16 20:05:25 +0000 | [diff] [blame] | 4236 | continue; |
| 4237 | } |
| Alexey Bataev | b4dd6d2 | 2018-08-29 20:41:37 +0000 | [diff] [blame] | 4238 | // The vaiable has no definition - no need to add the entry. |
| 4239 | if (CE->getVarSize().isZero()) |
| 4240 | continue; |
| Alexey Bataev | c52f01d | 2018-07-16 20:05:25 +0000 | [diff] [blame] | 4241 | break; |
| 4242 | } |
| 4243 | case OffloadEntriesInfoManagerTy::OMPTargetGlobalVarEntryLink: |
| 4244 | assert(((CGM.getLangOpts().OpenMPIsDevice && !CE->getAddress()) || |
| 4245 | (!CGM.getLangOpts().OpenMPIsDevice && CE->getAddress())) && |
| 4246 | "Declaret target link address is set."); |
| 4247 | if (CGM.getLangOpts().OpenMPIsDevice) |
| 4248 | continue; |
| 4249 | if (!CE->getAddress()) { |
| 4250 | unsigned DiagID = CGM.getDiags().getCustomDiagID( |
| 4251 | DiagnosticsEngine::Error, |
| 4252 | "Offloading entry for declare target variable is incorrect: the " |
| 4253 | "address is invalid."); |
| 4254 | CGM.getDiags().Report(DiagID); |
| 4255 | continue; |
| 4256 | } |
| 4257 | break; |
| Alexey Bataev | 64e62dc | 2018-04-30 16:26:57 +0000 | [diff] [blame] | 4258 | } |
| Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 4259 | createOffloadEntry(CE->getAddress(), CE->getAddress(), |
| Alexey Bataev | c52f01d | 2018-07-16 20:05:25 +0000 | [diff] [blame] | 4260 | CE->getVarSize().getQuantity(), Flags, |
| Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 4261 | CE->getLinkage()); |
| 4262 | } else { |
| Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4263 | llvm_unreachable("Unsupported entry kind."); |
| Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 4264 | } |
| Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4265 | } |
| 4266 | } |
| 4267 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4268 | /// Loads all the offload entries information from the host IR |
| Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4269 | /// metadata. |
| 4270 | void CGOpenMPRuntime::loadOffloadInfoMetadata() { |
| 4271 | // If we are in target mode, load the metadata from the host IR. This code has |
| 4272 | // to match the metadaata creation in createOffloadEntriesAndInfoMetadata(). |
| 4273 | |
| 4274 | if (!CGM.getLangOpts().OpenMPIsDevice) |
| 4275 | return; |
| 4276 | |
| 4277 | if (CGM.getLangOpts().OMPHostIRFile.empty()) |
| 4278 | return; |
| 4279 | |
| 4280 | auto Buf = llvm::MemoryBuffer::getFile(CGM.getLangOpts().OMPHostIRFile); |
| Alexey Bataev | 64e62dc | 2018-04-30 16:26:57 +0000 | [diff] [blame] | 4281 | if (auto EC = Buf.getError()) { |
| 4282 | CGM.getDiags().Report(diag::err_cannot_open_file) |
| 4283 | << CGM.getLangOpts().OMPHostIRFile << EC.message(); |
| Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4284 | return; |
| Alexey Bataev | 64e62dc | 2018-04-30 16:26:57 +0000 | [diff] [blame] | 4285 | } |
| Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4286 | |
| 4287 | llvm::LLVMContext C; |
| Peter Collingbourne | d9445c4 | 2016-11-13 07:00:17 +0000 | [diff] [blame] | 4288 | auto ME = expectedToErrorOrAndEmitErrors( |
| 4289 | C, llvm::parseBitcodeFile(Buf.get()->getMemBufferRef(), C)); |
| Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4290 | |
| Alexey Bataev | 64e62dc | 2018-04-30 16:26:57 +0000 | [diff] [blame] | 4291 | if (auto EC = ME.getError()) { |
| 4292 | unsigned DiagID = CGM.getDiags().getCustomDiagID( |
| 4293 | DiagnosticsEngine::Error, "Unable to parse host IR file '%0':'%1'"); |
| 4294 | CGM.getDiags().Report(DiagID) |
| 4295 | << CGM.getLangOpts().OMPHostIRFile << EC.message(); |
| Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4296 | return; |
| Alexey Bataev | 64e62dc | 2018-04-30 16:26:57 +0000 | [diff] [blame] | 4297 | } |
| Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4298 | |
| 4299 | llvm::NamedMDNode *MD = ME.get()->getNamedMetadata("omp_offload.info"); |
| 4300 | if (!MD) |
| 4301 | return; |
| 4302 | |
| George Burgess IV | 00f70bd | 2018-03-01 05:43:23 +0000 | [diff] [blame] | 4303 | for (llvm::MDNode *MN : MD->operands()) { |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4304 | auto &&GetMDInt = [MN](unsigned Idx) { |
| 4305 | auto *V = cast<llvm::ConstantAsMetadata>(MN->getOperand(Idx)); |
| Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4306 | return cast<llvm::ConstantInt>(V->getValue())->getZExtValue(); |
| 4307 | }; |
| 4308 | |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4309 | auto &&GetMDString = [MN](unsigned Idx) { |
| 4310 | auto *V = cast<llvm::MDString>(MN->getOperand(Idx)); |
| Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4311 | return V->getString(); |
| 4312 | }; |
| 4313 | |
| Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 4314 | switch (GetMDInt(0)) { |
| Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4315 | default: |
| 4316 | llvm_unreachable("Unexpected metadata!"); |
| 4317 | break; |
| 4318 | case OffloadEntriesInfoManagerTy::OffloadEntryInfo:: |
| Alexey Bataev | 34f8a70 | 2018-03-28 14:28:54 +0000 | [diff] [blame] | 4319 | OffloadingEntryInfoTargetRegion: |
| Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4320 | OffloadEntriesInfoManager.initializeTargetRegionEntryInfo( |
| Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 4321 | /*DeviceID=*/GetMDInt(1), /*FileID=*/GetMDInt(2), |
| 4322 | /*ParentName=*/GetMDString(3), /*Line=*/GetMDInt(4), |
| 4323 | /*Order=*/GetMDInt(5)); |
| 4324 | break; |
| 4325 | case OffloadEntriesInfoManagerTy::OffloadEntryInfo:: |
| 4326 | OffloadingEntryInfoDeviceGlobalVar: |
| 4327 | OffloadEntriesInfoManager.initializeDeviceGlobalVarEntryInfo( |
| 4328 | /*MangledName=*/GetMDString(1), |
| 4329 | static_cast<OffloadEntriesInfoManagerTy::OMPTargetGlobalVarEntryKind>( |
| 4330 | /*Flags=*/GetMDInt(2)), |
| 4331 | /*Order=*/GetMDInt(3)); |
| Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4332 | break; |
| 4333 | } |
| 4334 | } |
| 4335 | } |
| 4336 | |
| Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 4337 | void CGOpenMPRuntime::emitKmpRoutineEntryT(QualType KmpInt32Ty) { |
| 4338 | if (!KmpRoutineEntryPtrTy) { |
| 4339 | // Build typedef kmp_int32 (* kmp_routine_entry_t)(kmp_int32, void *); type. |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4340 | ASTContext &C = CGM.getContext(); |
| Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 4341 | QualType KmpRoutineEntryTyArgs[] = {KmpInt32Ty, C.VoidPtrTy}; |
| 4342 | FunctionProtoType::ExtProtoInfo EPI; |
| 4343 | KmpRoutineEntryPtrQTy = C.getPointerType( |
| 4344 | C.getFunctionType(KmpInt32Ty, KmpRoutineEntryTyArgs, EPI)); |
| 4345 | KmpRoutineEntryPtrTy = CGM.getTypes().ConvertType(KmpRoutineEntryPtrQTy); |
| 4346 | } |
| 4347 | } |
| 4348 | |
| Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4349 | QualType CGOpenMPRuntime::getTgtOffloadEntryQTy() { |
| Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4350 | // Make sure the type of the entry is already created. This is the type we |
| 4351 | // have to create: |
| 4352 | // struct __tgt_offload_entry{ |
| 4353 | // void *addr; // Pointer to the offload entry info. |
| 4354 | // // (function or global) |
| 4355 | // char *name; // Name of the function or global. |
| 4356 | // size_t size; // Size of the entry info (0 if it a function). |
| Samuel Antao | f83efdb | 2017-01-05 16:02:49 +0000 | [diff] [blame] | 4357 | // int32_t flags; // Flags associated with the entry, e.g. 'link'. |
| 4358 | // int32_t reserved; // Reserved, to use by the runtime library. |
| Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4359 | // }; |
| 4360 | if (TgtOffloadEntryQTy.isNull()) { |
| 4361 | ASTContext &C = CGM.getContext(); |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4362 | RecordDecl *RD = C.buildImplicitRecord("__tgt_offload_entry"); |
| Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4363 | RD->startDefinition(); |
| 4364 | addFieldToRecordDecl(C, RD, C.VoidPtrTy); |
| 4365 | addFieldToRecordDecl(C, RD, C.getPointerType(C.CharTy)); |
| 4366 | addFieldToRecordDecl(C, RD, C.getSizeType()); |
| Samuel Antao | f83efdb | 2017-01-05 16:02:49 +0000 | [diff] [blame] | 4367 | addFieldToRecordDecl( |
| 4368 | C, RD, C.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/true)); |
| 4369 | addFieldToRecordDecl( |
| 4370 | C, RD, C.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/true)); |
| Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4371 | RD->completeDefinition(); |
| Jonas Hahnfeld | 5e4df28 | 2018-01-18 15:38:03 +0000 | [diff] [blame] | 4372 | RD->addAttr(PackedAttr::CreateImplicit(C)); |
| Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4373 | TgtOffloadEntryQTy = C.getRecordType(RD); |
| 4374 | } |
| 4375 | return TgtOffloadEntryQTy; |
| 4376 | } |
| 4377 | |
| Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 4378 | namespace { |
| Alexey Bataev | 9e03404 | 2015-05-05 04:05:12 +0000 | [diff] [blame] | 4379 | struct PrivateHelpersTy { |
| 4380 | PrivateHelpersTy(const VarDecl *Original, const VarDecl *PrivateCopy, |
| 4381 | const VarDecl *PrivateElemInit) |
| 4382 | : Original(Original), PrivateCopy(PrivateCopy), |
| 4383 | PrivateElemInit(PrivateElemInit) {} |
| 4384 | const VarDecl *Original; |
| 4385 | const VarDecl *PrivateCopy; |
| 4386 | const VarDecl *PrivateElemInit; |
| 4387 | }; |
| 4388 | typedef std::pair<CharUnits /*Align*/, PrivateHelpersTy> PrivateDataTy; |
| Hans Wennborg | 7eb5464 | 2015-09-10 17:07:54 +0000 | [diff] [blame] | 4389 | } // anonymous namespace |
| Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 4390 | |
| Alexey Bataev | 9e03404 | 2015-05-05 04:05:12 +0000 | [diff] [blame] | 4391 | static RecordDecl * |
| Craig Topper | 8674c5c | 2015-09-29 04:30:07 +0000 | [diff] [blame] | 4392 | createPrivatesRecordDecl(CodeGenModule &CGM, ArrayRef<PrivateDataTy> Privates) { |
| Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 4393 | if (!Privates.empty()) { |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4394 | ASTContext &C = CGM.getContext(); |
| Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 4395 | // Build struct .kmp_privates_t. { |
| 4396 | // /* private vars */ |
| 4397 | // }; |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4398 | RecordDecl *RD = C.buildImplicitRecord(".kmp_privates.t"); |
| Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 4399 | RD->startDefinition(); |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4400 | for (const auto &Pair : Privates) { |
| 4401 | const VarDecl *VD = Pair.second.Original; |
| 4402 | QualType Type = VD->getType().getNonReferenceType(); |
| 4403 | FieldDecl *FD = addFieldToRecordDecl(C, RD, Type); |
| Alexey Bataev | c71a409 | 2015-09-11 10:29:41 +0000 | [diff] [blame] | 4404 | if (VD->hasAttrs()) { |
| 4405 | for (specific_attr_iterator<AlignedAttr> I(VD->getAttrs().begin()), |
| 4406 | E(VD->getAttrs().end()); |
| 4407 | I != E; ++I) |
| 4408 | FD->addAttr(*I); |
| 4409 | } |
| Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 4410 | } |
| Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 4411 | RD->completeDefinition(); |
| 4412 | return RD; |
| 4413 | } |
| 4414 | return nullptr; |
| 4415 | } |
| 4416 | |
| Alexey Bataev | 9e03404 | 2015-05-05 04:05:12 +0000 | [diff] [blame] | 4417 | static RecordDecl * |
| Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4418 | createKmpTaskTRecordDecl(CodeGenModule &CGM, OpenMPDirectiveKind Kind, |
| 4419 | QualType KmpInt32Ty, |
| Alexey Bataev | 8fc69dc | 2015-05-18 07:54:53 +0000 | [diff] [blame] | 4420 | QualType KmpRoutineEntryPointerQTy) { |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4421 | ASTContext &C = CGM.getContext(); |
| Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 4422 | // Build struct kmp_task_t { |
| 4423 | // void * shareds; |
| 4424 | // kmp_routine_entry_t routine; |
| 4425 | // kmp_int32 part_id; |
| Alexey Bataev | ad537bb | 2016-05-30 09:06:50 +0000 | [diff] [blame] | 4426 | // kmp_cmplrdata_t data1; |
| 4427 | // kmp_cmplrdata_t data2; |
| Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4428 | // For taskloops additional fields: |
| 4429 | // kmp_uint64 lb; |
| 4430 | // kmp_uint64 ub; |
| 4431 | // kmp_int64 st; |
| 4432 | // kmp_int32 liter; |
| Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 4433 | // void * reductions; |
| Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 4434 | // }; |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4435 | RecordDecl *UD = C.buildImplicitRecord("kmp_cmplrdata_t", TTK_Union); |
| Alexey Bataev | ad537bb | 2016-05-30 09:06:50 +0000 | [diff] [blame] | 4436 | UD->startDefinition(); |
| 4437 | addFieldToRecordDecl(C, UD, KmpInt32Ty); |
| 4438 | addFieldToRecordDecl(C, UD, KmpRoutineEntryPointerQTy); |
| 4439 | UD->completeDefinition(); |
| 4440 | QualType KmpCmplrdataTy = C.getRecordType(UD); |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4441 | RecordDecl *RD = C.buildImplicitRecord("kmp_task_t"); |
| Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 4442 | RD->startDefinition(); |
| 4443 | addFieldToRecordDecl(C, RD, C.VoidPtrTy); |
| 4444 | addFieldToRecordDecl(C, RD, KmpRoutineEntryPointerQTy); |
| 4445 | addFieldToRecordDecl(C, RD, KmpInt32Ty); |
| Alexey Bataev | ad537bb | 2016-05-30 09:06:50 +0000 | [diff] [blame] | 4446 | addFieldToRecordDecl(C, RD, KmpCmplrdataTy); |
| 4447 | addFieldToRecordDecl(C, RD, KmpCmplrdataTy); |
| Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4448 | if (isOpenMPTaskLoopDirective(Kind)) { |
| 4449 | QualType KmpUInt64Ty = |
| 4450 | CGM.getContext().getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0); |
| 4451 | QualType KmpInt64Ty = |
| 4452 | CGM.getContext().getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1); |
| 4453 | addFieldToRecordDecl(C, RD, KmpUInt64Ty); |
| 4454 | addFieldToRecordDecl(C, RD, KmpUInt64Ty); |
| 4455 | addFieldToRecordDecl(C, RD, KmpInt64Ty); |
| 4456 | addFieldToRecordDecl(C, RD, KmpInt32Ty); |
| Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 4457 | addFieldToRecordDecl(C, RD, C.VoidPtrTy); |
| Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4458 | } |
| Alexey Bataev | 8fc69dc | 2015-05-18 07:54:53 +0000 | [diff] [blame] | 4459 | RD->completeDefinition(); |
| 4460 | return RD; |
| 4461 | } |
| 4462 | |
| 4463 | static RecordDecl * |
| 4464 | createKmpTaskTWithPrivatesRecordDecl(CodeGenModule &CGM, QualType KmpTaskTQTy, |
| Craig Topper | 8674c5c | 2015-09-29 04:30:07 +0000 | [diff] [blame] | 4465 | ArrayRef<PrivateDataTy> Privates) { |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4466 | ASTContext &C = CGM.getContext(); |
| Alexey Bataev | 8fc69dc | 2015-05-18 07:54:53 +0000 | [diff] [blame] | 4467 | // Build struct kmp_task_t_with_privates { |
| 4468 | // kmp_task_t task_data; |
| 4469 | // .kmp_privates_t. privates; |
| 4470 | // }; |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4471 | RecordDecl *RD = C.buildImplicitRecord("kmp_task_t_with_privates"); |
| Alexey Bataev | 8fc69dc | 2015-05-18 07:54:53 +0000 | [diff] [blame] | 4472 | RD->startDefinition(); |
| 4473 | addFieldToRecordDecl(C, RD, KmpTaskTQTy); |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4474 | if (const RecordDecl *PrivateRD = createPrivatesRecordDecl(CGM, Privates)) |
| Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 4475 | addFieldToRecordDecl(C, RD, C.getRecordType(PrivateRD)); |
| Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 4476 | RD->completeDefinition(); |
| Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 4477 | return RD; |
| Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 4478 | } |
| 4479 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4480 | /// Emit a proxy function which accepts kmp_task_t as the second |
| Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 4481 | /// argument. |
| 4482 | /// \code |
| 4483 | /// kmp_int32 .omp_task_entry.(kmp_int32 gtid, kmp_task_t *tt) { |
| Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 4484 | /// TaskFunction(gtid, tt->part_id, &tt->privates, task_privates_map, tt, |
| Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4485 | /// For taskloops: |
| 4486 | /// tt->task_data.lb, tt->task_data.ub, tt->task_data.st, tt->task_data.liter, |
| Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 4487 | /// tt->reductions, tt->shareds); |
| Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 4488 | /// return 0; |
| 4489 | /// } |
| 4490 | /// \endcode |
| James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 4491 | static llvm::Function * |
| Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 4492 | emitProxyTaskFunction(CodeGenModule &CGM, SourceLocation Loc, |
| Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4493 | OpenMPDirectiveKind Kind, QualType KmpInt32Ty, |
| 4494 | QualType KmpTaskTWithPrivatesPtrQTy, |
| Alexey Bataev | 8fc69dc | 2015-05-18 07:54:53 +0000 | [diff] [blame] | 4495 | QualType KmpTaskTWithPrivatesQTy, QualType KmpTaskTQTy, |
| James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 4496 | QualType SharedsPtrTy, llvm::Function *TaskFunction, |
| Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4497 | llvm::Value *TaskPrivatesMap) { |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4498 | ASTContext &C = CGM.getContext(); |
| Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 4499 | FunctionArgList Args; |
| Alexey Bataev | 5622323 | 2017-06-09 13:40:18 +0000 | [diff] [blame] | 4500 | ImplicitParamDecl GtidArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, KmpInt32Ty, |
| 4501 | ImplicitParamDecl::Other); |
| 4502 | ImplicitParamDecl TaskTypeArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, |
| 4503 | KmpTaskTWithPrivatesPtrQTy.withRestrict(), |
| 4504 | ImplicitParamDecl::Other); |
| Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 4505 | Args.push_back(&GtidArg); |
| 4506 | Args.push_back(&TaskTypeArg); |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4507 | const auto &TaskEntryFnInfo = |
| John McCall | c56a8b3 | 2016-03-11 04:30:31 +0000 | [diff] [blame] | 4508 | CGM.getTypes().arrangeBuiltinFunctionDeclaration(KmpInt32Ty, Args); |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4509 | llvm::FunctionType *TaskEntryTy = |
| 4510 | CGM.getTypes().GetFunctionType(TaskEntryFnInfo); |
| Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 4511 | std::string Name = CGM.getOpenMPRuntime().getName({"omp_task_entry", ""}); |
| 4512 | auto *TaskEntry = llvm::Function::Create( |
| 4513 | TaskEntryTy, llvm::GlobalValue::InternalLinkage, Name, &CGM.getModule()); |
| Rafael Espindola | 51ec5a9 | 2018-02-28 23:46:35 +0000 | [diff] [blame] | 4514 | CGM.SetInternalFunctionAttributes(GlobalDecl(), TaskEntry, TaskEntryFnInfo); |
| Alexey Bataev | c0f879b | 2018-04-10 20:10:53 +0000 | [diff] [blame] | 4515 | TaskEntry->setDoesNotRecurse(); |
| Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 4516 | CodeGenFunction CGF(CGM); |
| Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 4517 | CGF.StartFunction(GlobalDecl(), KmpInt32Ty, TaskEntry, TaskEntryFnInfo, Args, |
| 4518 | Loc, Loc); |
| Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 4519 | |
| Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4520 | // TaskFunction(gtid, tt->task_data.part_id, &tt->privates, task_privates_map, |
| Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4521 | // tt, |
| 4522 | // For taskloops: |
| 4523 | // tt->task_data.lb, tt->task_data.ub, tt->task_data.st, tt->task_data.liter, |
| 4524 | // tt->task_data.shareds); |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4525 | llvm::Value *GtidParam = CGF.EmitLoadOfScalar( |
| John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4526 | CGF.GetAddrOfLocalVar(&GtidArg), /*Volatile=*/false, KmpInt32Ty, Loc); |
| Alexey Bataev | 31300ed | 2016-02-04 11:27:03 +0000 | [diff] [blame] | 4527 | LValue TDBase = CGF.EmitLoadOfPointerLValue( |
| 4528 | CGF.GetAddrOfLocalVar(&TaskTypeArg), |
| 4529 | KmpTaskTWithPrivatesPtrQTy->castAs<PointerType>()); |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4530 | const auto *KmpTaskTWithPrivatesQTyRD = |
| Alexey Bataev | 8fc69dc | 2015-05-18 07:54:53 +0000 | [diff] [blame] | 4531 | cast<RecordDecl>(KmpTaskTWithPrivatesQTy->getAsTagDecl()); |
| Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4532 | LValue Base = |
| 4533 | CGF.EmitLValueForField(TDBase, *KmpTaskTWithPrivatesQTyRD->field_begin()); |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4534 | const auto *KmpTaskTQTyRD = cast<RecordDecl>(KmpTaskTQTy->getAsTagDecl()); |
| Alexey Bataev | 8fc69dc | 2015-05-18 07:54:53 +0000 | [diff] [blame] | 4535 | auto PartIdFI = std::next(KmpTaskTQTyRD->field_begin(), KmpTaskTPartId); |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4536 | LValue PartIdLVal = CGF.EmitLValueForField(Base, *PartIdFI); |
| Akira Hatanaka | f139ae3 | 2019-12-03 15:17:01 -0800 | [diff] [blame] | 4537 | llvm::Value *PartidParam = PartIdLVal.getPointer(CGF); |
| Alexey Bataev | 8fc69dc | 2015-05-18 07:54:53 +0000 | [diff] [blame] | 4538 | |
| 4539 | auto SharedsFI = std::next(KmpTaskTQTyRD->field_begin(), KmpTaskTShareds); |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4540 | LValue SharedsLVal = CGF.EmitLValueForField(Base, *SharedsFI); |
| 4541 | llvm::Value *SharedsParam = CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
| Alexey Bataev | 1e49137 | 2018-01-23 18:44:14 +0000 | [diff] [blame] | 4542 | CGF.EmitLoadOfScalar(SharedsLVal, Loc), |
| Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 4543 | CGF.ConvertTypeForMem(SharedsPtrTy)); |
| 4544 | |
| Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4545 | auto PrivatesFI = std::next(KmpTaskTWithPrivatesQTyRD->field_begin(), 1); |
| 4546 | llvm::Value *PrivatesParam; |
| 4547 | if (PrivatesFI != KmpTaskTWithPrivatesQTyRD->field_end()) { |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4548 | LValue PrivatesLVal = CGF.EmitLValueForField(TDBase, *PrivatesFI); |
| Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4549 | PrivatesParam = CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
| Akira Hatanaka | f139ae3 | 2019-12-03 15:17:01 -0800 | [diff] [blame] | 4550 | PrivatesLVal.getPointer(CGF), CGF.VoidPtrTy); |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4551 | } else { |
| Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4552 | PrivatesParam = llvm::ConstantPointerNull::get(CGF.VoidPtrTy); |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4553 | } |
| Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4554 | |
| Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4555 | llvm::Value *CommonArgs[] = {GtidParam, PartidParam, PrivatesParam, |
| 4556 | TaskPrivatesMap, |
| 4557 | CGF.Builder |
| 4558 | .CreatePointerBitCastOrAddrSpaceCast( |
| Akira Hatanaka | f139ae3 | 2019-12-03 15:17:01 -0800 | [diff] [blame] | 4559 | TDBase.getAddress(CGF), CGF.VoidPtrTy) |
| Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4560 | .getPointer()}; |
| 4561 | SmallVector<llvm::Value *, 16> CallArgs(std::begin(CommonArgs), |
| 4562 | std::end(CommonArgs)); |
| 4563 | if (isOpenMPTaskLoopDirective(Kind)) { |
| 4564 | auto LBFI = std::next(KmpTaskTQTyRD->field_begin(), KmpTaskTLowerBound); |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4565 | LValue LBLVal = CGF.EmitLValueForField(Base, *LBFI); |
| 4566 | llvm::Value *LBParam = CGF.EmitLoadOfScalar(LBLVal, Loc); |
| Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4567 | auto UBFI = std::next(KmpTaskTQTyRD->field_begin(), KmpTaskTUpperBound); |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4568 | LValue UBLVal = CGF.EmitLValueForField(Base, *UBFI); |
| 4569 | llvm::Value *UBParam = CGF.EmitLoadOfScalar(UBLVal, Loc); |
| Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4570 | auto StFI = std::next(KmpTaskTQTyRD->field_begin(), KmpTaskTStride); |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4571 | LValue StLVal = CGF.EmitLValueForField(Base, *StFI); |
| 4572 | llvm::Value *StParam = CGF.EmitLoadOfScalar(StLVal, Loc); |
| Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4573 | auto LIFI = std::next(KmpTaskTQTyRD->field_begin(), KmpTaskTLastIter); |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4574 | LValue LILVal = CGF.EmitLValueForField(Base, *LIFI); |
| 4575 | llvm::Value *LIParam = CGF.EmitLoadOfScalar(LILVal, Loc); |
| Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 4576 | auto RFI = std::next(KmpTaskTQTyRD->field_begin(), KmpTaskTReductions); |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4577 | LValue RLVal = CGF.EmitLValueForField(Base, *RFI); |
| 4578 | llvm::Value *RParam = CGF.EmitLoadOfScalar(RLVal, Loc); |
| Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4579 | CallArgs.push_back(LBParam); |
| 4580 | CallArgs.push_back(UBParam); |
| 4581 | CallArgs.push_back(StParam); |
| 4582 | CallArgs.push_back(LIParam); |
| Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 4583 | CallArgs.push_back(RParam); |
| Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4584 | } |
| 4585 | CallArgs.push_back(SharedsParam); |
| 4586 | |
| Alexey Bataev | 3c595a6 | 2017-08-14 15:01:03 +0000 | [diff] [blame] | 4587 | CGM.getOpenMPRuntime().emitOutlinedFunctionCall(CGF, Loc, TaskFunction, |
| 4588 | CallArgs); |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4589 | CGF.EmitStoreThroughLValue(RValue::get(CGF.Builder.getInt32(/*C=*/0)), |
| 4590 | CGF.MakeAddrLValue(CGF.ReturnValue, KmpInt32Ty)); |
| Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 4591 | CGF.FinishFunction(); |
| 4592 | return TaskEntry; |
| 4593 | } |
| 4594 | |
| Alexey Bataev | 8fc69dc | 2015-05-18 07:54:53 +0000 | [diff] [blame] | 4595 | static llvm::Value *emitDestructorsFunction(CodeGenModule &CGM, |
| 4596 | SourceLocation Loc, |
| 4597 | QualType KmpInt32Ty, |
| 4598 | QualType KmpTaskTWithPrivatesPtrQTy, |
| 4599 | QualType KmpTaskTWithPrivatesQTy) { |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4600 | ASTContext &C = CGM.getContext(); |
| Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 4601 | FunctionArgList Args; |
| Alexey Bataev | 5622323 | 2017-06-09 13:40:18 +0000 | [diff] [blame] | 4602 | ImplicitParamDecl GtidArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, KmpInt32Ty, |
| 4603 | ImplicitParamDecl::Other); |
| 4604 | ImplicitParamDecl TaskTypeArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, |
| 4605 | KmpTaskTWithPrivatesPtrQTy.withRestrict(), |
| 4606 | ImplicitParamDecl::Other); |
| Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 4607 | Args.push_back(&GtidArg); |
| 4608 | Args.push_back(&TaskTypeArg); |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4609 | const auto &DestructorFnInfo = |
| John McCall | c56a8b3 | 2016-03-11 04:30:31 +0000 | [diff] [blame] | 4610 | CGM.getTypes().arrangeBuiltinFunctionDeclaration(KmpInt32Ty, Args); |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4611 | llvm::FunctionType *DestructorFnTy = |
| 4612 | CGM.getTypes().GetFunctionType(DestructorFnInfo); |
| Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 4613 | std::string Name = |
| 4614 | CGM.getOpenMPRuntime().getName({"omp_task_destructor", ""}); |
| Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 4615 | auto *DestructorFn = |
| 4616 | llvm::Function::Create(DestructorFnTy, llvm::GlobalValue::InternalLinkage, |
| Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 4617 | Name, &CGM.getModule()); |
| Rafael Espindola | 51ec5a9 | 2018-02-28 23:46:35 +0000 | [diff] [blame] | 4618 | CGM.SetInternalFunctionAttributes(GlobalDecl(), DestructorFn, |
| Akira Hatanaka | 44a59f8 | 2015-10-28 02:30:47 +0000 | [diff] [blame] | 4619 | DestructorFnInfo); |
| Alexey Bataev | c0f879b | 2018-04-10 20:10:53 +0000 | [diff] [blame] | 4620 | DestructorFn->setDoesNotRecurse(); |
| Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 4621 | CodeGenFunction CGF(CGM); |
| Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 4622 | CGF.StartFunction(GlobalDecl(), KmpInt32Ty, DestructorFn, DestructorFnInfo, |
| Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 4623 | Args, Loc, Loc); |
| Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 4624 | |
| Alexey Bataev | 31300ed | 2016-02-04 11:27:03 +0000 | [diff] [blame] | 4625 | LValue Base = CGF.EmitLoadOfPointerLValue( |
| 4626 | CGF.GetAddrOfLocalVar(&TaskTypeArg), |
| 4627 | KmpTaskTWithPrivatesPtrQTy->castAs<PointerType>()); |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4628 | const auto *KmpTaskTWithPrivatesQTyRD = |
| Alexey Bataev | 8fc69dc | 2015-05-18 07:54:53 +0000 | [diff] [blame] | 4629 | cast<RecordDecl>(KmpTaskTWithPrivatesQTy->getAsTagDecl()); |
| 4630 | auto FI = std::next(KmpTaskTWithPrivatesQTyRD->field_begin()); |
| Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 4631 | Base = CGF.EmitLValueForField(Base, *FI); |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4632 | for (const auto *Field : |
| Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 4633 | cast<RecordDecl>(FI->getType()->getAsTagDecl())->fields()) { |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4634 | if (QualType::DestructionKind DtorKind = |
| 4635 | Field->getType().isDestructedType()) { |
| 4636 | LValue FieldLValue = CGF.EmitLValueForField(Base, Field); |
| Akira Hatanaka | f139ae3 | 2019-12-03 15:17:01 -0800 | [diff] [blame] | 4637 | CGF.pushDestroy(DtorKind, FieldLValue.getAddress(CGF), Field->getType()); |
| Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 4638 | } |
| 4639 | } |
| 4640 | CGF.FinishFunction(); |
| 4641 | return DestructorFn; |
| 4642 | } |
| 4643 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4644 | /// Emit a privates mapping function for correct handling of private and |
| Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4645 | /// firstprivate variables. |
| 4646 | /// \code |
| 4647 | /// void .omp_task_privates_map.(const .privates. *noalias privs, <ty1> |
| 4648 | /// **noalias priv1,..., <tyn> **noalias privn) { |
| 4649 | /// *priv1 = &.privates.priv1; |
| 4650 | /// ...; |
| 4651 | /// *privn = &.privates.privn; |
| 4652 | /// } |
| 4653 | /// \endcode |
| 4654 | static llvm::Value * |
| 4655 | emitTaskPrivateMappingFunction(CodeGenModule &CGM, SourceLocation Loc, |
| Craig Topper | 8674c5c | 2015-09-29 04:30:07 +0000 | [diff] [blame] | 4656 | ArrayRef<const Expr *> PrivateVars, |
| 4657 | ArrayRef<const Expr *> FirstprivateVars, |
| Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4658 | ArrayRef<const Expr *> LastprivateVars, |
| Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4659 | QualType PrivatesQTy, |
| Craig Topper | 8674c5c | 2015-09-29 04:30:07 +0000 | [diff] [blame] | 4660 | ArrayRef<PrivateDataTy> Privates) { |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4661 | ASTContext &C = CGM.getContext(); |
| Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4662 | FunctionArgList Args; |
| 4663 | ImplicitParamDecl TaskPrivatesArg( |
| 4664 | C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, |
| Alexey Bataev | 5622323 | 2017-06-09 13:40:18 +0000 | [diff] [blame] | 4665 | C.getPointerType(PrivatesQTy).withConst().withRestrict(), |
| 4666 | ImplicitParamDecl::Other); |
| Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4667 | Args.push_back(&TaskPrivatesArg); |
| 4668 | llvm::DenseMap<const VarDecl *, unsigned> PrivateVarsPos; |
| 4669 | unsigned Counter = 1; |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4670 | for (const Expr *E : PrivateVars) { |
| Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4671 | Args.push_back(ImplicitParamDecl::Create( |
| Alexey Bataev | 5622323 | 2017-06-09 13:40:18 +0000 | [diff] [blame] | 4672 | C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, |
| 4673 | C.getPointerType(C.getPointerType(E->getType())) |
| 4674 | .withConst() |
| 4675 | .withRestrict(), |
| 4676 | ImplicitParamDecl::Other)); |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4677 | const auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl()); |
| Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4678 | PrivateVarsPos[VD] = Counter; |
| 4679 | ++Counter; |
| 4680 | } |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4681 | for (const Expr *E : FirstprivateVars) { |
| Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4682 | Args.push_back(ImplicitParamDecl::Create( |
| Alexey Bataev | 5622323 | 2017-06-09 13:40:18 +0000 | [diff] [blame] | 4683 | C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, |
| 4684 | C.getPointerType(C.getPointerType(E->getType())) |
| 4685 | .withConst() |
| 4686 | .withRestrict(), |
| 4687 | ImplicitParamDecl::Other)); |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4688 | const auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl()); |
| Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4689 | PrivateVarsPos[VD] = Counter; |
| 4690 | ++Counter; |
| 4691 | } |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4692 | for (const Expr *E : LastprivateVars) { |
| Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4693 | Args.push_back(ImplicitParamDecl::Create( |
| Alexey Bataev | 5622323 | 2017-06-09 13:40:18 +0000 | [diff] [blame] | 4694 | C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, |
| 4695 | C.getPointerType(C.getPointerType(E->getType())) |
| 4696 | .withConst() |
| 4697 | .withRestrict(), |
| 4698 | ImplicitParamDecl::Other)); |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4699 | const auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl()); |
| Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4700 | PrivateVarsPos[VD] = Counter; |
| 4701 | ++Counter; |
| 4702 | } |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4703 | const auto &TaskPrivatesMapFnInfo = |
| John McCall | c56a8b3 | 2016-03-11 04:30:31 +0000 | [diff] [blame] | 4704 | CGM.getTypes().arrangeBuiltinFunctionDeclaration(C.VoidTy, Args); |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4705 | llvm::FunctionType *TaskPrivatesMapTy = |
| Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4706 | CGM.getTypes().GetFunctionType(TaskPrivatesMapFnInfo); |
| Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 4707 | std::string Name = |
| 4708 | CGM.getOpenMPRuntime().getName({"omp_task_privates_map", ""}); |
| Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4709 | auto *TaskPrivatesMap = llvm::Function::Create( |
| Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 4710 | TaskPrivatesMapTy, llvm::GlobalValue::InternalLinkage, Name, |
| 4711 | &CGM.getModule()); |
| Rafael Espindola | 51ec5a9 | 2018-02-28 23:46:35 +0000 | [diff] [blame] | 4712 | CGM.SetInternalFunctionAttributes(GlobalDecl(), TaskPrivatesMap, |
| Akira Hatanaka | 44a59f8 | 2015-10-28 02:30:47 +0000 | [diff] [blame] | 4713 | TaskPrivatesMapFnInfo); |
| Alexey Bataev | 8c5555c | 2019-05-21 15:11:58 +0000 | [diff] [blame] | 4714 | if (CGM.getLangOpts().Optimize) { |
| 4715 | TaskPrivatesMap->removeFnAttr(llvm::Attribute::NoInline); |
| 4716 | TaskPrivatesMap->removeFnAttr(llvm::Attribute::OptimizeNone); |
| 4717 | TaskPrivatesMap->addFnAttr(llvm::Attribute::AlwaysInline); |
| 4718 | } |
| Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4719 | CodeGenFunction CGF(CGM); |
| Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4720 | CGF.StartFunction(GlobalDecl(), C.VoidTy, TaskPrivatesMap, |
| Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 4721 | TaskPrivatesMapFnInfo, Args, Loc, Loc); |
| Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4722 | |
| 4723 | // *privi = &.privates.privi; |
| Alexey Bataev | 31300ed | 2016-02-04 11:27:03 +0000 | [diff] [blame] | 4724 | LValue Base = CGF.EmitLoadOfPointerLValue( |
| 4725 | CGF.GetAddrOfLocalVar(&TaskPrivatesArg), |
| 4726 | TaskPrivatesArg.getType()->castAs<PointerType>()); |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4727 | const auto *PrivatesQTyRD = cast<RecordDecl>(PrivatesQTy->getAsTagDecl()); |
| Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4728 | Counter = 0; |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4729 | for (const FieldDecl *Field : PrivatesQTyRD->fields()) { |
| 4730 | LValue FieldLVal = CGF.EmitLValueForField(Base, Field); |
| 4731 | const VarDecl *VD = Args[PrivateVarsPos[Privates[Counter].second.Original]]; |
| 4732 | LValue RefLVal = |
| 4733 | CGF.MakeAddrLValue(CGF.GetAddrOfLocalVar(VD), VD->getType()); |
| 4734 | LValue RefLoadLVal = CGF.EmitLoadOfPointerLValue( |
| Akira Hatanaka | f139ae3 | 2019-12-03 15:17:01 -0800 | [diff] [blame] | 4735 | RefLVal.getAddress(CGF), RefLVal.getType()->castAs<PointerType>()); |
| 4736 | CGF.EmitStoreOfScalar(FieldLVal.getPointer(CGF), RefLoadLVal); |
| Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4737 | ++Counter; |
| 4738 | } |
| 4739 | CGF.FinishFunction(); |
| 4740 | return TaskPrivatesMap; |
| 4741 | } |
| 4742 | |
| Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4743 | /// Emit initialization for private variables in task-based directives. |
| Alexey Bataev | 8a83159 | 2016-05-10 10:36:51 +0000 | [diff] [blame] | 4744 | static void emitPrivatesInit(CodeGenFunction &CGF, |
| Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4745 | const OMPExecutableDirective &D, |
| 4746 | Address KmpTaskSharedsPtr, LValue TDBase, |
| 4747 | const RecordDecl *KmpTaskTWithPrivatesQTyRD, |
| 4748 | QualType SharedsTy, QualType SharedsPtrTy, |
| 4749 | const OMPTaskDataTy &Data, |
| 4750 | ArrayRef<PrivateDataTy> Privates, bool ForDup) { |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4751 | ASTContext &C = CGF.getContext(); |
| Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4752 | auto FI = std::next(KmpTaskTWithPrivatesQTyRD->field_begin()); |
| 4753 | LValue PrivatesBase = CGF.EmitLValueForField(TDBase, *FI); |
| Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 4754 | OpenMPDirectiveKind Kind = isOpenMPTaskLoopDirective(D.getDirectiveKind()) |
| 4755 | ? OMPD_taskloop |
| 4756 | : OMPD_task; |
| 4757 | const CapturedStmt &CS = *D.getCapturedStmt(Kind); |
| 4758 | CodeGenFunction::CGCapturedStmtInfo CapturesInfo(CS); |
| Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4759 | LValue SrcBase; |
| Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 4760 | bool IsTargetTask = |
| 4761 | isOpenMPTargetDataManagementDirective(D.getDirectiveKind()) || |
| 4762 | isOpenMPTargetExecutionDirective(D.getDirectiveKind()); |
| 4763 | // For target-based directives skip 3 firstprivate arrays BasePointersArray, |
| 4764 | // PointersArray and SizesArray. The original variables for these arrays are |
| 4765 | // not captured and we get their addresses explicitly. |
| 4766 | if ((!IsTargetTask && !Data.FirstprivateVars.empty()) || |
| Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 4767 | (IsTargetTask && KmpTaskSharedsPtr.isValid())) { |
| Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4768 | SrcBase = CGF.MakeAddrLValue( |
| 4769 | CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
| 4770 | KmpTaskSharedsPtr, CGF.ConvertTypeForMem(SharedsPtrTy)), |
| 4771 | SharedsTy); |
| 4772 | } |
| Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4773 | FI = cast<RecordDecl>(FI->getType()->getAsTagDecl())->field_begin(); |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4774 | for (const PrivateDataTy &Pair : Privates) { |
| 4775 | const VarDecl *VD = Pair.second.PrivateCopy; |
| 4776 | const Expr *Init = VD->getAnyInitializer(); |
| Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4777 | if (Init && (!ForDup || (isa<CXXConstructExpr>(Init) && |
| 4778 | !CGF.isTrivialInitializer(Init)))) { |
| Alexey Bataev | 8a83159 | 2016-05-10 10:36:51 +0000 | [diff] [blame] | 4779 | LValue PrivateLValue = CGF.EmitLValueForField(PrivatesBase, *FI); |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4780 | if (const VarDecl *Elem = Pair.second.PrivateElemInit) { |
| 4781 | const VarDecl *OriginalVD = Pair.second.Original; |
| Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 4782 | // Check if the variable is the target-based BasePointersArray, |
| 4783 | // PointersArray or SizesArray. |
| 4784 | LValue SharedRefLValue; |
| Alexey Bataev | ab41ea6 | 2019-03-13 20:46:28 +0000 | [diff] [blame] | 4785 | QualType Type = PrivateLValue.getType(); |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4786 | const FieldDecl *SharedField = CapturesInfo.lookup(OriginalVD); |
| Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 4787 | if (IsTargetTask && !SharedField) { |
| 4788 | assert(isa<ImplicitParamDecl>(OriginalVD) && |
| 4789 | isa<CapturedDecl>(OriginalVD->getDeclContext()) && |
| 4790 | cast<CapturedDecl>(OriginalVD->getDeclContext()) |
| 4791 | ->getNumParams() == 0 && |
| 4792 | isa<TranslationUnitDecl>( |
| 4793 | cast<CapturedDecl>(OriginalVD->getDeclContext()) |
| 4794 | ->getDeclContext()) && |
| 4795 | "Expected artificial target data variable."); |
| Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 4796 | SharedRefLValue = |
| 4797 | CGF.MakeAddrLValue(CGF.GetAddrOfLocalVar(OriginalVD), Type); |
| 4798 | } else { |
| Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 4799 | SharedRefLValue = CGF.EmitLValueForField(SrcBase, SharedField); |
| 4800 | SharedRefLValue = CGF.MakeAddrLValue( |
| Akira Hatanaka | f139ae3 | 2019-12-03 15:17:01 -0800 | [diff] [blame] | 4801 | Address(SharedRefLValue.getPointer(CGF), |
| 4802 | C.getDeclAlign(OriginalVD)), |
| Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 4803 | SharedRefLValue.getType(), LValueBaseInfo(AlignmentSource::Decl), |
| 4804 | SharedRefLValue.getTBAAInfo()); |
| 4805 | } |
| Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4806 | if (Type->isArrayType()) { |
| 4807 | // Initialize firstprivate array. |
| 4808 | if (!isa<CXXConstructExpr>(Init) || CGF.isTrivialInitializer(Init)) { |
| 4809 | // Perform simple memcpy. |
| Ivan A. Kosarev | 1860b52 | 2018-01-25 14:21:55 +0000 | [diff] [blame] | 4810 | CGF.EmitAggregateAssign(PrivateLValue, SharedRefLValue, Type); |
| Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4811 | } else { |
| 4812 | // Initialize firstprivate array using element-by-element |
| Simon Pilgrim | 2c51880 | 2017-03-30 14:13:19 +0000 | [diff] [blame] | 4813 | // initialization. |
| Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4814 | CGF.EmitOMPAggregateAssign( |
| Akira Hatanaka | f139ae3 | 2019-12-03 15:17:01 -0800 | [diff] [blame] | 4815 | PrivateLValue.getAddress(CGF), SharedRefLValue.getAddress(CGF), |
| 4816 | Type, |
| Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4817 | [&CGF, Elem, Init, &CapturesInfo](Address DestElement, |
| 4818 | Address SrcElement) { |
| 4819 | // Clean up any temporaries needed by the initialization. |
| 4820 | CodeGenFunction::OMPPrivateScope InitScope(CGF); |
| 4821 | InitScope.addPrivate( |
| 4822 | Elem, [SrcElement]() -> Address { return SrcElement; }); |
| 4823 | (void)InitScope.Privatize(); |
| 4824 | // Emit initialization for single element. |
| 4825 | CodeGenFunction::CGCapturedStmtRAII CapInfoRAII( |
| 4826 | CGF, &CapturesInfo); |
| 4827 | CGF.EmitAnyExprToMem(Init, DestElement, |
| 4828 | Init->getType().getQualifiers(), |
| 4829 | /*IsInitializer=*/false); |
| 4830 | }); |
| 4831 | } |
| 4832 | } else { |
| 4833 | CodeGenFunction::OMPPrivateScope InitScope(CGF); |
| Akira Hatanaka | f139ae3 | 2019-12-03 15:17:01 -0800 | [diff] [blame] | 4834 | InitScope.addPrivate(Elem, [SharedRefLValue, &CGF]() -> Address { |
| 4835 | return SharedRefLValue.getAddress(CGF); |
| Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4836 | }); |
| 4837 | (void)InitScope.Privatize(); |
| 4838 | CodeGenFunction::CGCapturedStmtRAII CapInfoRAII(CGF, &CapturesInfo); |
| 4839 | CGF.EmitExprAsInit(Init, VD, PrivateLValue, |
| 4840 | /*capturedByInit=*/false); |
| 4841 | } |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4842 | } else { |
| Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4843 | CGF.EmitExprAsInit(Init, VD, PrivateLValue, /*capturedByInit=*/false); |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4844 | } |
| Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4845 | } |
| Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4846 | ++FI; |
| 4847 | } |
| Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4848 | } |
| 4849 | |
| 4850 | /// Check if duplication function is required for taskloops. |
| 4851 | static bool checkInitIsRequired(CodeGenFunction &CGF, |
| 4852 | ArrayRef<PrivateDataTy> Privates) { |
| 4853 | bool InitRequired = false; |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4854 | for (const PrivateDataTy &Pair : Privates) { |
| 4855 | const VarDecl *VD = Pair.second.PrivateCopy; |
| 4856 | const Expr *Init = VD->getAnyInitializer(); |
| Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4857 | InitRequired = InitRequired || (Init && isa<CXXConstructExpr>(Init) && |
| 4858 | !CGF.isTrivialInitializer(Init)); |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4859 | if (InitRequired) |
| 4860 | break; |
| Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4861 | } |
| 4862 | return InitRequired; |
| 4863 | } |
| 4864 | |
| 4865 | |
| 4866 | /// Emit task_dup function (for initialization of |
| 4867 | /// private/firstprivate/lastprivate vars and last_iter flag) |
| 4868 | /// \code |
| 4869 | /// void __task_dup_entry(kmp_task_t *task_dst, const kmp_task_t *task_src, int |
| 4870 | /// lastpriv) { |
| 4871 | /// // setup lastprivate flag |
| 4872 | /// task_dst->last = lastpriv; |
| 4873 | /// // could be constructor calls here... |
| 4874 | /// } |
| 4875 | /// \endcode |
| 4876 | static llvm::Value * |
| 4877 | emitTaskDupFunction(CodeGenModule &CGM, SourceLocation Loc, |
| 4878 | const OMPExecutableDirective &D, |
| 4879 | QualType KmpTaskTWithPrivatesPtrQTy, |
| 4880 | const RecordDecl *KmpTaskTWithPrivatesQTyRD, |
| 4881 | const RecordDecl *KmpTaskTQTyRD, QualType SharedsTy, |
| 4882 | QualType SharedsPtrTy, const OMPTaskDataTy &Data, |
| 4883 | ArrayRef<PrivateDataTy> Privates, bool WithLastIter) { |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4884 | ASTContext &C = CGM.getContext(); |
| Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4885 | FunctionArgList Args; |
| Alexey Bataev | 5622323 | 2017-06-09 13:40:18 +0000 | [diff] [blame] | 4886 | ImplicitParamDecl DstArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, |
| 4887 | KmpTaskTWithPrivatesPtrQTy, |
| 4888 | ImplicitParamDecl::Other); |
| 4889 | ImplicitParamDecl SrcArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, |
| 4890 | KmpTaskTWithPrivatesPtrQTy, |
| 4891 | ImplicitParamDecl::Other); |
| 4892 | ImplicitParamDecl LastprivArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, C.IntTy, |
| 4893 | ImplicitParamDecl::Other); |
| Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4894 | Args.push_back(&DstArg); |
| 4895 | Args.push_back(&SrcArg); |
| 4896 | Args.push_back(&LastprivArg); |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4897 | const auto &TaskDupFnInfo = |
| Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4898 | CGM.getTypes().arrangeBuiltinFunctionDeclaration(C.VoidTy, Args); |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4899 | llvm::FunctionType *TaskDupTy = CGM.getTypes().GetFunctionType(TaskDupFnInfo); |
| Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 4900 | std::string Name = CGM.getOpenMPRuntime().getName({"omp_task_dup", ""}); |
| 4901 | auto *TaskDup = llvm::Function::Create( |
| 4902 | TaskDupTy, llvm::GlobalValue::InternalLinkage, Name, &CGM.getModule()); |
| Rafael Espindola | 51ec5a9 | 2018-02-28 23:46:35 +0000 | [diff] [blame] | 4903 | CGM.SetInternalFunctionAttributes(GlobalDecl(), TaskDup, TaskDupFnInfo); |
| Alexey Bataev | c0f879b | 2018-04-10 20:10:53 +0000 | [diff] [blame] | 4904 | TaskDup->setDoesNotRecurse(); |
| Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4905 | CodeGenFunction CGF(CGM); |
| Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 4906 | CGF.StartFunction(GlobalDecl(), C.VoidTy, TaskDup, TaskDupFnInfo, Args, Loc, |
| 4907 | Loc); |
| Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4908 | |
| 4909 | LValue TDBase = CGF.EmitLoadOfPointerLValue( |
| 4910 | CGF.GetAddrOfLocalVar(&DstArg), |
| 4911 | KmpTaskTWithPrivatesPtrQTy->castAs<PointerType>()); |
| 4912 | // task_dst->liter = lastpriv; |
| 4913 | if (WithLastIter) { |
| 4914 | auto LIFI = std::next(KmpTaskTQTyRD->field_begin(), KmpTaskTLastIter); |
| 4915 | LValue Base = CGF.EmitLValueForField( |
| 4916 | TDBase, *KmpTaskTWithPrivatesQTyRD->field_begin()); |
| 4917 | LValue LILVal = CGF.EmitLValueForField(Base, *LIFI); |
| 4918 | llvm::Value *Lastpriv = CGF.EmitLoadOfScalar( |
| 4919 | CGF.GetAddrOfLocalVar(&LastprivArg), /*Volatile=*/false, C.IntTy, Loc); |
| 4920 | CGF.EmitStoreOfScalar(Lastpriv, LILVal); |
| 4921 | } |
| 4922 | |
| 4923 | // Emit initial values for private copies (if any). |
| 4924 | assert(!Privates.empty()); |
| 4925 | Address KmpTaskSharedsPtr = Address::invalid(); |
| 4926 | if (!Data.FirstprivateVars.empty()) { |
| 4927 | LValue TDBase = CGF.EmitLoadOfPointerLValue( |
| 4928 | CGF.GetAddrOfLocalVar(&SrcArg), |
| 4929 | KmpTaskTWithPrivatesPtrQTy->castAs<PointerType>()); |
| 4930 | LValue Base = CGF.EmitLValueForField( |
| 4931 | TDBase, *KmpTaskTWithPrivatesQTyRD->field_begin()); |
| 4932 | KmpTaskSharedsPtr = Address( |
| 4933 | CGF.EmitLoadOfScalar(CGF.EmitLValueForField( |
| 4934 | Base, *std::next(KmpTaskTQTyRD->field_begin(), |
| 4935 | KmpTaskTShareds)), |
| 4936 | Loc), |
| 4937 | CGF.getNaturalTypeAlignment(SharedsTy)); |
| 4938 | } |
| Alexey Bataev | 8a83159 | 2016-05-10 10:36:51 +0000 | [diff] [blame] | 4939 | emitPrivatesInit(CGF, D, KmpTaskSharedsPtr, TDBase, KmpTaskTWithPrivatesQTyRD, |
| 4940 | SharedsTy, SharedsPtrTy, Data, Privates, /*ForDup=*/true); |
| Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4941 | CGF.FinishFunction(); |
| 4942 | return TaskDup; |
| 4943 | } |
| 4944 | |
| Alexey Bataev | 8a83159 | 2016-05-10 10:36:51 +0000 | [diff] [blame] | 4945 | /// Checks if destructor function is required to be generated. |
| 4946 | /// \return true if cleanups are required, false otherwise. |
| 4947 | static bool |
| 4948 | checkDestructorsRequired(const RecordDecl *KmpTaskTWithPrivatesQTyRD) { |
| 4949 | bool NeedsCleanup = false; |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4950 | auto FI = std::next(KmpTaskTWithPrivatesQTyRD->field_begin(), 1); |
| 4951 | const auto *PrivateRD = cast<RecordDecl>(FI->getType()->getAsTagDecl()); |
| 4952 | for (const FieldDecl *FD : PrivateRD->fields()) { |
| Alexey Bataev | 8a83159 | 2016-05-10 10:36:51 +0000 | [diff] [blame] | 4953 | NeedsCleanup = NeedsCleanup || FD->getType().isDestructedType(); |
| 4954 | if (NeedsCleanup) |
| 4955 | break; |
| 4956 | } |
| 4957 | return NeedsCleanup; |
| 4958 | } |
| 4959 | |
| Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 4960 | CGOpenMPRuntime::TaskResultTy |
| 4961 | CGOpenMPRuntime::emitTaskInit(CodeGenFunction &CGF, SourceLocation Loc, |
| 4962 | const OMPExecutableDirective &D, |
| James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 4963 | llvm::Function *TaskFunction, QualType SharedsTy, |
| Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 4964 | Address Shareds, const OMPTaskDataTy &Data) { |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4965 | ASTContext &C = CGM.getContext(); |
| Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4966 | llvm::SmallVector<PrivateDataTy, 4> Privates; |
| Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4967 | // Aggregate privates and sort them by the alignment. |
| Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 4968 | auto I = Data.PrivateCopies.begin(); |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4969 | for (const Expr *E : Data.PrivateVars) { |
| 4970 | const auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl()); |
| Alexey Bataev | 43a919f | 2018-04-13 17:48:43 +0000 | [diff] [blame] | 4971 | Privates.emplace_back( |
| Alexey Bataev | c71a409 | 2015-09-11 10:29:41 +0000 | [diff] [blame] | 4972 | C.getDeclAlign(VD), |
| Alexey Bataev | 9e03404 | 2015-05-05 04:05:12 +0000 | [diff] [blame] | 4973 | PrivateHelpersTy(VD, cast<VarDecl>(cast<DeclRefExpr>(*I)->getDecl()), |
| Alexey Bataev | 43a919f | 2018-04-13 17:48:43 +0000 | [diff] [blame] | 4974 | /*PrivateElemInit=*/nullptr)); |
| Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 4975 | ++I; |
| 4976 | } |
| Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 4977 | I = Data.FirstprivateCopies.begin(); |
| 4978 | auto IElemInitRef = Data.FirstprivateInits.begin(); |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4979 | for (const Expr *E : Data.FirstprivateVars) { |
| 4980 | const auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl()); |
| Alexey Bataev | 43a919f | 2018-04-13 17:48:43 +0000 | [diff] [blame] | 4981 | Privates.emplace_back( |
| Alexey Bataev | c71a409 | 2015-09-11 10:29:41 +0000 | [diff] [blame] | 4982 | C.getDeclAlign(VD), |
| Alexey Bataev | 9e03404 | 2015-05-05 04:05:12 +0000 | [diff] [blame] | 4983 | PrivateHelpersTy( |
| 4984 | VD, cast<VarDecl>(cast<DeclRefExpr>(*I)->getDecl()), |
| Alexey Bataev | 43a919f | 2018-04-13 17:48:43 +0000 | [diff] [blame] | 4985 | cast<VarDecl>(cast<DeclRefExpr>(*IElemInitRef)->getDecl()))); |
| Richard Trieu | cc3949d | 2016-02-18 22:34:54 +0000 | [diff] [blame] | 4986 | ++I; |
| 4987 | ++IElemInitRef; |
| Alexey Bataev | 9e03404 | 2015-05-05 04:05:12 +0000 | [diff] [blame] | 4988 | } |
| Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4989 | I = Data.LastprivateCopies.begin(); |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4990 | for (const Expr *E : Data.LastprivateVars) { |
| 4991 | const auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl()); |
| Alexey Bataev | 43a919f | 2018-04-13 17:48:43 +0000 | [diff] [blame] | 4992 | Privates.emplace_back( |
| Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4993 | C.getDeclAlign(VD), |
| 4994 | PrivateHelpersTy(VD, cast<VarDecl>(cast<DeclRefExpr>(*I)->getDecl()), |
| Alexey Bataev | 43a919f | 2018-04-13 17:48:43 +0000 | [diff] [blame] | 4995 | /*PrivateElemInit=*/nullptr)); |
| Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4996 | ++I; |
| 4997 | } |
| Fangrui Song | 899d139 | 2019-04-24 14:43:05 +0000 | [diff] [blame] | 4998 | llvm::stable_sort(Privates, [](PrivateDataTy L, PrivateDataTy R) { |
| 4999 | return L.first > R.first; |
| 5000 | }); |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5001 | QualType KmpInt32Ty = C.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1); |
| Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 5002 | // Build type kmp_routine_entry_t (if not built yet). |
| 5003 | emitKmpRoutineEntryT(KmpInt32Ty); |
| Alexey Bataev | 8fc69dc | 2015-05-18 07:54:53 +0000 | [diff] [blame] | 5004 | // Build type kmp_task_t (if not built yet). |
| Alexey Bataev | e213f3e | 2017-10-11 15:29:40 +0000 | [diff] [blame] | 5005 | if (isOpenMPTaskLoopDirective(D.getDirectiveKind())) { |
| 5006 | if (SavedKmpTaskloopTQTy.isNull()) { |
| 5007 | SavedKmpTaskloopTQTy = C.getRecordType(createKmpTaskTRecordDecl( |
| 5008 | CGM, D.getDirectiveKind(), KmpInt32Ty, KmpRoutineEntryPtrQTy)); |
| 5009 | } |
| 5010 | KmpTaskTQTy = SavedKmpTaskloopTQTy; |
| Alexey Bataev | 3a03a7f | 2017-10-11 15:56:38 +0000 | [diff] [blame] | 5011 | } else { |
| Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 5012 | assert((D.getDirectiveKind() == OMPD_task || |
| 5013 | isOpenMPTargetExecutionDirective(D.getDirectiveKind()) || |
| 5014 | isOpenMPTargetDataManagementDirective(D.getDirectiveKind())) && |
| 5015 | "Expected taskloop, task or target directive"); |
| Alexey Bataev | e213f3e | 2017-10-11 15:29:40 +0000 | [diff] [blame] | 5016 | if (SavedKmpTaskTQTy.isNull()) { |
| 5017 | SavedKmpTaskTQTy = C.getRecordType(createKmpTaskTRecordDecl( |
| 5018 | CGM, D.getDirectiveKind(), KmpInt32Ty, KmpRoutineEntryPtrQTy)); |
| 5019 | } |
| 5020 | KmpTaskTQTy = SavedKmpTaskTQTy; |
| Alexey Bataev | 8fc69dc | 2015-05-18 07:54:53 +0000 | [diff] [blame] | 5021 | } |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5022 | const auto *KmpTaskTQTyRD = cast<RecordDecl>(KmpTaskTQTy->getAsTagDecl()); |
| Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 5023 | // Build particular struct kmp_task_t for the given task. |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5024 | const RecordDecl *KmpTaskTWithPrivatesQTyRD = |
| Alexey Bataev | 8fc69dc | 2015-05-18 07:54:53 +0000 | [diff] [blame] | 5025 | createKmpTaskTWithPrivatesRecordDecl(CGM, KmpTaskTQTy, Privates); |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5026 | QualType KmpTaskTWithPrivatesQTy = C.getRecordType(KmpTaskTWithPrivatesQTyRD); |
| Alexey Bataev | 8fc69dc | 2015-05-18 07:54:53 +0000 | [diff] [blame] | 5027 | QualType KmpTaskTWithPrivatesPtrQTy = |
| 5028 | C.getPointerType(KmpTaskTWithPrivatesQTy); |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5029 | llvm::Type *KmpTaskTWithPrivatesTy = CGF.ConvertType(KmpTaskTWithPrivatesQTy); |
| 5030 | llvm::Type *KmpTaskTWithPrivatesPtrTy = |
| 5031 | KmpTaskTWithPrivatesTy->getPointerTo(); |
| 5032 | llvm::Value *KmpTaskTWithPrivatesTySize = |
| 5033 | CGF.getTypeSize(KmpTaskTWithPrivatesQTy); |
| Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 5034 | QualType SharedsPtrTy = C.getPointerType(SharedsTy); |
| 5035 | |
| Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 5036 | // Emit initial values for private copies (if any). |
| 5037 | llvm::Value *TaskPrivatesMap = nullptr; |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5038 | llvm::Type *TaskPrivatesMapTy = |
| James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 5039 | std::next(TaskFunction->arg_begin(), 3)->getType(); |
| Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 5040 | if (!Privates.empty()) { |
| 5041 | auto FI = std::next(KmpTaskTWithPrivatesQTyRD->field_begin()); |
| Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 5042 | TaskPrivatesMap = emitTaskPrivateMappingFunction( |
| 5043 | CGM, Loc, Data.PrivateVars, Data.FirstprivateVars, Data.LastprivateVars, |
| 5044 | FI->getType(), Privates); |
| Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 5045 | TaskPrivatesMap = CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
| 5046 | TaskPrivatesMap, TaskPrivatesMapTy); |
| 5047 | } else { |
| 5048 | TaskPrivatesMap = llvm::ConstantPointerNull::get( |
| 5049 | cast<llvm::PointerType>(TaskPrivatesMapTy)); |
| 5050 | } |
| Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 5051 | // Build a proxy function kmp_int32 .omp_task_entry.(kmp_int32 gtid, |
| 5052 | // kmp_task_t *tt); |
| James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 5053 | llvm::Function *TaskEntry = emitProxyTaskFunction( |
| Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 5054 | CGM, Loc, D.getDirectiveKind(), KmpInt32Ty, KmpTaskTWithPrivatesPtrQTy, |
| 5055 | KmpTaskTWithPrivatesQTy, KmpTaskTQTy, SharedsPtrTy, TaskFunction, |
| 5056 | TaskPrivatesMap); |
| Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 5057 | |
| 5058 | // Build call kmp_task_t * __kmpc_omp_task_alloc(ident_t *, kmp_int32 gtid, |
| 5059 | // kmp_int32 flags, size_t sizeof_kmp_task_t, size_t sizeof_shareds, |
| 5060 | // kmp_routine_entry_t *task_entry); |
| 5061 | // Task flags. Format is taken from |
| James Y Knight | 5d71fc5 | 2019-01-29 16:37:27 +0000 | [diff] [blame] | 5062 | // https://github.com/llvm/llvm-project/blob/master/openmp/runtime/src/kmp.h, |
| Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 5063 | // description of kmp_tasking_flags struct. |
| Alexey Bataev | 1e1e286 | 2016-05-10 12:21:02 +0000 | [diff] [blame] | 5064 | enum { |
| 5065 | TiedFlag = 0x1, |
| 5066 | FinalFlag = 0x2, |
| 5067 | DestructorsFlag = 0x8, |
| 5068 | PriorityFlag = 0x20 |
| 5069 | }; |
| Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 5070 | unsigned Flags = Data.Tied ? TiedFlag : 0; |
| Alexey Bataev | 8a83159 | 2016-05-10 10:36:51 +0000 | [diff] [blame] | 5071 | bool NeedsCleanup = false; |
| 5072 | if (!Privates.empty()) { |
| 5073 | NeedsCleanup = checkDestructorsRequired(KmpTaskTWithPrivatesQTyRD); |
| 5074 | if (NeedsCleanup) |
| 5075 | Flags = Flags | DestructorsFlag; |
| 5076 | } |
| Alexey Bataev | 1e1e286 | 2016-05-10 12:21:02 +0000 | [diff] [blame] | 5077 | if (Data.Priority.getInt()) |
| 5078 | Flags = Flags | PriorityFlag; |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5079 | llvm::Value *TaskFlags = |
| Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 5080 | Data.Final.getPointer() |
| 5081 | ? CGF.Builder.CreateSelect(Data.Final.getPointer(), |
| Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 5082 | CGF.Builder.getInt32(FinalFlag), |
| 5083 | CGF.Builder.getInt32(/*C=*/0)) |
| Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 5084 | : CGF.Builder.getInt32(Data.Final.getInt() ? FinalFlag : 0); |
| Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 5085 | TaskFlags = CGF.Builder.CreateOr(TaskFlags, CGF.Builder.getInt32(Flags)); |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5086 | llvm::Value *SharedsSize = CGM.getSize(C.getTypeSizeInChars(SharedsTy)); |
| Gheorghe-Teodor Bercea | 545a9fe | 2019-06-14 20:19:54 +0000 | [diff] [blame] | 5087 | SmallVector<llvm::Value *, 8> AllocArgs = {emitUpdateLocation(CGF, Loc), |
| 5088 | getThreadID(CGF, Loc), TaskFlags, KmpTaskTWithPrivatesTySize, |
| 5089 | SharedsSize, CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
| 5090 | TaskEntry, KmpRoutineEntryPtrTy)}; |
| 5091 | llvm::Value *NewTask; |
| 5092 | if (D.hasClausesOfKind<OMPNowaitClause>()) { |
| 5093 | // Check if we have any device clause associated with the directive. |
| 5094 | const Expr *Device = nullptr; |
| 5095 | if (auto *C = D.getSingleClause<OMPDeviceClause>()) |
| 5096 | Device = C->getDevice(); |
| 5097 | // Emit device ID if any otherwise use default value. |
| 5098 | llvm::Value *DeviceID; |
| 5099 | if (Device) |
| 5100 | DeviceID = CGF.Builder.CreateIntCast(CGF.EmitScalarExpr(Device), |
| 5101 | CGF.Int64Ty, /*isSigned=*/true); |
| 5102 | else |
| 5103 | DeviceID = CGF.Builder.getInt64(OMP_DEVICEID_UNDEF); |
| 5104 | AllocArgs.push_back(DeviceID); |
| 5105 | NewTask = CGF.EmitRuntimeCall( |
| 5106 | createRuntimeFunction(OMPRTL__kmpc_omp_target_task_alloc), AllocArgs); |
| 5107 | } else { |
| 5108 | NewTask = CGF.EmitRuntimeCall( |
| Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 5109 | createRuntimeFunction(OMPRTL__kmpc_omp_task_alloc), AllocArgs); |
| Gheorghe-Teodor Bercea | 545a9fe | 2019-06-14 20:19:54 +0000 | [diff] [blame] | 5110 | } |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5111 | llvm::Value *NewTaskNewTaskTTy = |
| 5112 | CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
| 5113 | NewTask, KmpTaskTWithPrivatesPtrTy); |
| Alexey Bataev | 8fc69dc | 2015-05-18 07:54:53 +0000 | [diff] [blame] | 5114 | LValue Base = CGF.MakeNaturalAlignAddrLValue(NewTaskNewTaskTTy, |
| 5115 | KmpTaskTWithPrivatesQTy); |
| 5116 | LValue TDBase = |
| 5117 | CGF.EmitLValueForField(Base, *KmpTaskTWithPrivatesQTyRD->field_begin()); |
| Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 5118 | // Fill the data in the resulting kmp_task_t record. |
| 5119 | // Copy shareds if there are any. |
| John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5120 | Address KmpTaskSharedsPtr = Address::invalid(); |
| Alexey Bataev | 8fc69dc | 2015-05-18 07:54:53 +0000 | [diff] [blame] | 5121 | if (!SharedsTy->getAsStructureType()->getDecl()->field_empty()) { |
| Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 5122 | KmpTaskSharedsPtr = |
| 5123 | Address(CGF.EmitLoadOfScalar( |
| 5124 | CGF.EmitLValueForField( |
| 5125 | TDBase, *std::next(KmpTaskTQTyRD->field_begin(), |
| 5126 | KmpTaskTShareds)), |
| 5127 | Loc), |
| 5128 | CGF.getNaturalTypeAlignment(SharedsTy)); |
| Ivan A. Kosarev | 1860b52 | 2018-01-25 14:21:55 +0000 | [diff] [blame] | 5129 | LValue Dest = CGF.MakeAddrLValue(KmpTaskSharedsPtr, SharedsTy); |
| 5130 | LValue Src = CGF.MakeAddrLValue(Shareds, SharedsTy); |
| Richard Smith | e78fac5 | 2018-04-05 20:52:58 +0000 | [diff] [blame] | 5131 | CGF.EmitAggregateCopy(Dest, Src, SharedsTy, AggValueSlot::DoesNotOverlap); |
| Alexey Bataev | 8fc69dc | 2015-05-18 07:54:53 +0000 | [diff] [blame] | 5132 | } |
| Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 5133 | // Emit initial values for private copies (if any). |
| Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 5134 | TaskResultTy Result; |
| Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 5135 | if (!Privates.empty()) { |
| Alexey Bataev | 8a83159 | 2016-05-10 10:36:51 +0000 | [diff] [blame] | 5136 | emitPrivatesInit(CGF, D, KmpTaskSharedsPtr, Base, KmpTaskTWithPrivatesQTyRD, |
| 5137 | SharedsTy, SharedsPtrTy, Data, Privates, |
| 5138 | /*ForDup=*/false); |
| Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 5139 | if (isOpenMPTaskLoopDirective(D.getDirectiveKind()) && |
| 5140 | (!Data.LastprivateVars.empty() || checkInitIsRequired(CGF, Privates))) { |
| 5141 | Result.TaskDupFn = emitTaskDupFunction( |
| 5142 | CGM, Loc, D, KmpTaskTWithPrivatesPtrQTy, KmpTaskTWithPrivatesQTyRD, |
| 5143 | KmpTaskTQTyRD, SharedsTy, SharedsPtrTy, Data, Privates, |
| 5144 | /*WithLastIter=*/!Data.LastprivateVars.empty()); |
| Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 5145 | } |
| 5146 | } |
| Alexey Bataev | ad537bb | 2016-05-30 09:06:50 +0000 | [diff] [blame] | 5147 | // Fields of union "kmp_cmplrdata_t" for destructors and priority. |
| 5148 | enum { Priority = 0, Destructors = 1 }; |
| Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 5149 | // Provide pointer to function with destructors for privates. |
| Alexey Bataev | ad537bb | 2016-05-30 09:06:50 +0000 | [diff] [blame] | 5150 | auto FI = std::next(KmpTaskTQTyRD->field_begin(), Data1); |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5151 | const RecordDecl *KmpCmplrdataUD = |
| 5152 | (*FI)->getType()->getAsUnionType()->getDecl(); |
| Alexey Bataev | ad537bb | 2016-05-30 09:06:50 +0000 | [diff] [blame] | 5153 | if (NeedsCleanup) { |
| 5154 | llvm::Value *DestructorFn = emitDestructorsFunction( |
| 5155 | CGM, Loc, KmpInt32Ty, KmpTaskTWithPrivatesPtrQTy, |
| 5156 | KmpTaskTWithPrivatesQTy); |
| 5157 | LValue Data1LV = CGF.EmitLValueForField(TDBase, *FI); |
| 5158 | LValue DestructorsLV = CGF.EmitLValueForField( |
| 5159 | Data1LV, *std::next(KmpCmplrdataUD->field_begin(), Destructors)); |
| 5160 | CGF.EmitStoreOfScalar(CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
| 5161 | DestructorFn, KmpRoutineEntryPtrTy), |
| 5162 | DestructorsLV); |
| 5163 | } |
| 5164 | // Set priority. |
| 5165 | if (Data.Priority.getInt()) { |
| 5166 | LValue Data2LV = CGF.EmitLValueForField( |
| 5167 | TDBase, *std::next(KmpTaskTQTyRD->field_begin(), Data2)); |
| 5168 | LValue PriorityLV = CGF.EmitLValueForField( |
| 5169 | Data2LV, *std::next(KmpCmplrdataUD->field_begin(), Priority)); |
| 5170 | CGF.EmitStoreOfScalar(Data.Priority.getPointer(), PriorityLV); |
| 5171 | } |
| Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 5172 | Result.NewTask = NewTask; |
| 5173 | Result.TaskEntry = TaskEntry; |
| 5174 | Result.NewTaskNewTaskTTy = NewTaskNewTaskTTy; |
| 5175 | Result.TDBase = TDBase; |
| 5176 | Result.KmpTaskTQTyRD = KmpTaskTQTyRD; |
| 5177 | return Result; |
| Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 5178 | } |
| Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 5179 | |
| Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 5180 | void CGOpenMPRuntime::emitTaskCall(CodeGenFunction &CGF, SourceLocation Loc, |
| 5181 | const OMPExecutableDirective &D, |
| James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 5182 | llvm::Function *TaskFunction, |
| Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 5183 | QualType SharedsTy, Address Shareds, |
| 5184 | const Expr *IfCond, |
| 5185 | const OMPTaskDataTy &Data) { |
| Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 5186 | if (!CGF.HaveInsertPoint()) |
| 5187 | return; |
| 5188 | |
| Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 5189 | TaskResultTy Result = |
| 5190 | emitTaskInit(CGF, Loc, D, TaskFunction, SharedsTy, Shareds, Data); |
| 5191 | llvm::Value *NewTask = Result.NewTask; |
| James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 5192 | llvm::Function *TaskEntry = Result.TaskEntry; |
| Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 5193 | llvm::Value *NewTaskNewTaskTTy = Result.NewTaskNewTaskTTy; |
| 5194 | LValue TDBase = Result.TDBase; |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5195 | const RecordDecl *KmpTaskTQTyRD = Result.KmpTaskTQTyRD; |
| 5196 | ASTContext &C = CGM.getContext(); |
| Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 5197 | // Process list of dependences. |
| John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5198 | Address DependenciesArray = Address::invalid(); |
| Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 5199 | unsigned NumDependencies = Data.Dependences.size(); |
| John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5200 | if (NumDependencies) { |
| Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 5201 | // Dependence kind for RTL. |
| Sergi Mateo Bellido | 31df1ad | 2019-02-04 07:33:19 +0000 | [diff] [blame] | 5202 | enum RTLDependenceKindTy { DepIn = 0x01, DepInOut = 0x3, DepMutexInOutSet = 0x4 }; |
| Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 5203 | enum RTLDependInfoFieldsTy { BaseAddr, Len, Flags }; |
| 5204 | RecordDecl *KmpDependInfoRD; |
| Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5205 | QualType FlagsTy = |
| 5206 | C.getIntTypeForBitwidth(C.getTypeSize(C.BoolTy), /*Signed=*/false); |
| Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 5207 | llvm::Type *LLVMFlagsTy = CGF.ConvertTypeForMem(FlagsTy); |
| 5208 | if (KmpDependInfoTy.isNull()) { |
| 5209 | KmpDependInfoRD = C.buildImplicitRecord("kmp_depend_info"); |
| 5210 | KmpDependInfoRD->startDefinition(); |
| 5211 | addFieldToRecordDecl(C, KmpDependInfoRD, C.getIntPtrType()); |
| 5212 | addFieldToRecordDecl(C, KmpDependInfoRD, C.getSizeType()); |
| 5213 | addFieldToRecordDecl(C, KmpDependInfoRD, FlagsTy); |
| 5214 | KmpDependInfoRD->completeDefinition(); |
| 5215 | KmpDependInfoTy = C.getRecordType(KmpDependInfoRD); |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5216 | } else { |
| Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 5217 | KmpDependInfoRD = cast<RecordDecl>(KmpDependInfoTy->getAsTagDecl()); |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5218 | } |
| Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 5219 | // Define type kmp_depend_info[<Dependences.size()>]; |
| 5220 | QualType KmpDependInfoArrayTy = C.getConstantArrayType( |
| John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5221 | KmpDependInfoTy, llvm::APInt(/*numBits=*/64, NumDependencies), |
| Richard Smith | 772e266 | 2019-10-04 01:25:59 +0000 | [diff] [blame] | 5222 | nullptr, ArrayType::Normal, /*IndexTypeQuals=*/0); |
| Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 5223 | // kmp_depend_info[<Dependences.size()>] deps; |
| Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 5224 | DependenciesArray = |
| 5225 | CGF.CreateMemTemp(KmpDependInfoArrayTy, ".dep.arr.addr"); |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5226 | for (unsigned I = 0; I < NumDependencies; ++I) { |
| 5227 | const Expr *E = Data.Dependences[I].second; |
| 5228 | LValue Addr = CGF.EmitLValue(E); |
| Alexey Bataev | d6fdc8b | 2015-08-31 07:32:19 +0000 | [diff] [blame] | 5229 | llvm::Value *Size; |
| 5230 | QualType Ty = E->getType(); |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5231 | if (const auto *ASE = |
| 5232 | dyn_cast<OMPArraySectionExpr>(E->IgnoreParenImpCasts())) { |
| Alexey Bataev | d6fdc8b | 2015-08-31 07:32:19 +0000 | [diff] [blame] | 5233 | LValue UpAddrLVal = |
| Rui Ueyama | 49a3ad2 | 2019-07-16 04:46:31 +0000 | [diff] [blame] | 5234 | CGF.EmitOMPArraySectionExpr(ASE, /*IsLowerBound=*/false); |
| Akira Hatanaka | f139ae3 | 2019-12-03 15:17:01 -0800 | [diff] [blame] | 5235 | llvm::Value *UpAddr = CGF.Builder.CreateConstGEP1_32( |
| 5236 | UpAddrLVal.getPointer(CGF), /*Idx0=*/1); |
| Alexey Bataev | d6fdc8b | 2015-08-31 07:32:19 +0000 | [diff] [blame] | 5237 | llvm::Value *LowIntPtr = |
| Akira Hatanaka | f139ae3 | 2019-12-03 15:17:01 -0800 | [diff] [blame] | 5238 | CGF.Builder.CreatePtrToInt(Addr.getPointer(CGF), CGM.SizeTy); |
| Alexey Bataev | d6fdc8b | 2015-08-31 07:32:19 +0000 | [diff] [blame] | 5239 | llvm::Value *UpIntPtr = CGF.Builder.CreatePtrToInt(UpAddr, CGM.SizeTy); |
| 5240 | Size = CGF.Builder.CreateNUWSub(UpIntPtr, LowIntPtr); |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5241 | } else { |
| Alexey Bataev | 1189bd0 | 2016-01-26 12:20:39 +0000 | [diff] [blame] | 5242 | Size = CGF.getTypeSize(Ty); |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5243 | } |
| 5244 | LValue Base = CGF.MakeAddrLValue( |
| James Y Knight | 751fe28 | 2019-02-09 22:22:28 +0000 | [diff] [blame] | 5245 | CGF.Builder.CreateConstArrayGEP(DependenciesArray, I), |
| Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 5246 | KmpDependInfoTy); |
| 5247 | // deps[i].base_addr = &<Dependences[i].second>; |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5248 | LValue BaseAddrLVal = CGF.EmitLValueForField( |
| Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 5249 | Base, *std::next(KmpDependInfoRD->field_begin(), BaseAddr)); |
| John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5250 | CGF.EmitStoreOfScalar( |
| Akira Hatanaka | f139ae3 | 2019-12-03 15:17:01 -0800 | [diff] [blame] | 5251 | CGF.Builder.CreatePtrToInt(Addr.getPointer(CGF), CGF.IntPtrTy), |
| John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5252 | BaseAddrLVal); |
| Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 5253 | // deps[i].len = sizeof(<Dependences[i].second>); |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5254 | LValue LenLVal = CGF.EmitLValueForField( |
| Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 5255 | Base, *std::next(KmpDependInfoRD->field_begin(), Len)); |
| 5256 | CGF.EmitStoreOfScalar(Size, LenLVal); |
| 5257 | // deps[i].flags = <Dependences[i].first>; |
| 5258 | RTLDependenceKindTy DepKind; |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5259 | switch (Data.Dependences[I].first) { |
| Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 5260 | case OMPC_DEPEND_in: |
| 5261 | DepKind = DepIn; |
| 5262 | break; |
| Alexey Bataev | 92e82f9 | 2015-11-23 13:33:42 +0000 | [diff] [blame] | 5263 | // Out and InOut dependencies must use the same code. |
| Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 5264 | case OMPC_DEPEND_out: |
| Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 5265 | case OMPC_DEPEND_inout: |
| 5266 | DepKind = DepInOut; |
| 5267 | break; |
| Sergi Mateo Bellido | 31df1ad | 2019-02-04 07:33:19 +0000 | [diff] [blame] | 5268 | case OMPC_DEPEND_mutexinoutset: |
| 5269 | DepKind = DepMutexInOutSet; |
| 5270 | break; |
| Alexey Bataev | eb48235 | 2015-12-18 05:05:56 +0000 | [diff] [blame] | 5271 | case OMPC_DEPEND_source: |
| Alexey Bataev | a636c7f | 2015-12-23 10:27:45 +0000 | [diff] [blame] | 5272 | case OMPC_DEPEND_sink: |
| Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 5273 | case OMPC_DEPEND_unknown: |
| 5274 | llvm_unreachable("Unknown task dependence type"); |
| 5275 | } |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5276 | LValue FlagsLVal = CGF.EmitLValueForField( |
| Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 5277 | Base, *std::next(KmpDependInfoRD->field_begin(), Flags)); |
| 5278 | CGF.EmitStoreOfScalar(llvm::ConstantInt::get(LLVMFlagsTy, DepKind), |
| 5279 | FlagsLVal); |
| 5280 | } |
| John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5281 | DependenciesArray = CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
| James Y Knight | f5f1b0e | 2019-02-08 15:34:12 +0000 | [diff] [blame] | 5282 | CGF.Builder.CreateConstArrayGEP(DependenciesArray, 0), CGF.VoidPtrTy); |
| Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 5283 | } |
| 5284 | |
| Alexander Kornienko | 2a8c18d | 2018-04-06 15:14:32 +0000 | [diff] [blame] | 5285 | // NOTE: routine and part_id fields are initialized by __kmpc_omp_task_alloc() |
| Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 5286 | // libcall. |
| Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 5287 | // Build kmp_int32 __kmpc_omp_task_with_deps(ident_t *, kmp_int32 gtid, |
| 5288 | // kmp_task_t *new_task, kmp_int32 ndeps, kmp_depend_info_t *dep_list, |
| 5289 | // kmp_int32 ndeps_noalias, kmp_depend_info_t *noalias_dep_list) if dependence |
| 5290 | // list is not empty |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5291 | llvm::Value *ThreadID = getThreadID(CGF, Loc); |
| 5292 | llvm::Value *UpLoc = emitUpdateLocation(CGF, Loc); |
| John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5293 | llvm::Value *TaskArgs[] = { UpLoc, ThreadID, NewTask }; |
| 5294 | llvm::Value *DepTaskArgs[7]; |
| 5295 | if (NumDependencies) { |
| 5296 | DepTaskArgs[0] = UpLoc; |
| 5297 | DepTaskArgs[1] = ThreadID; |
| 5298 | DepTaskArgs[2] = NewTask; |
| 5299 | DepTaskArgs[3] = CGF.Builder.getInt32(NumDependencies); |
| 5300 | DepTaskArgs[4] = DependenciesArray.getPointer(); |
| 5301 | DepTaskArgs[5] = CGF.Builder.getInt32(0); |
| 5302 | DepTaskArgs[6] = llvm::ConstantPointerNull::get(CGF.VoidPtrTy); |
| 5303 | } |
| Malcolm Parsons | c6e4583 | 2017-01-13 18:55:32 +0000 | [diff] [blame] | 5304 | auto &&ThenCodeGen = [this, &Data, TDBase, KmpTaskTQTyRD, NumDependencies, |
| 5305 | &TaskArgs, |
| Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5306 | &DepTaskArgs](CodeGenFunction &CGF, PrePostActionTy &) { |
| Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 5307 | if (!Data.Tied) { |
| Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 5308 | auto PartIdFI = std::next(KmpTaskTQTyRD->field_begin(), KmpTaskTPartId); |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5309 | LValue PartIdLVal = CGF.EmitLValueForField(TDBase, *PartIdFI); |
| Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 5310 | CGF.EmitStoreOfScalar(CGF.Builder.getInt32(0), PartIdLVal); |
| 5311 | } |
| John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5312 | if (NumDependencies) { |
| Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5313 | CGF.EmitRuntimeCall( |
| Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 5314 | createRuntimeFunction(OMPRTL__kmpc_omp_task_with_deps), DepTaskArgs); |
| John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5315 | } else { |
| Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 5316 | CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_omp_task), |
| John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5317 | TaskArgs); |
| 5318 | } |
| Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 5319 | // Check if parent region is untied and build return for untied task; |
| 5320 | if (auto *Region = |
| 5321 | dyn_cast_or_null<CGOpenMPRegionInfo>(CGF.CapturedStmtInfo)) |
| 5322 | Region->emitUntiedSwitch(CGF); |
| Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 5323 | }; |
| John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5324 | |
| 5325 | llvm::Value *DepWaitTaskArgs[6]; |
| 5326 | if (NumDependencies) { |
| 5327 | DepWaitTaskArgs[0] = UpLoc; |
| 5328 | DepWaitTaskArgs[1] = ThreadID; |
| 5329 | DepWaitTaskArgs[2] = CGF.Builder.getInt32(NumDependencies); |
| 5330 | DepWaitTaskArgs[3] = DependenciesArray.getPointer(); |
| 5331 | DepWaitTaskArgs[4] = CGF.Builder.getInt32(0); |
| 5332 | DepWaitTaskArgs[5] = llvm::ConstantPointerNull::get(CGF.VoidPtrTy); |
| 5333 | } |
| Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5334 | auto &&ElseCodeGen = [&TaskArgs, ThreadID, NewTaskNewTaskTTy, TaskEntry, |
| Alexey Bataev | 3c595a6 | 2017-08-14 15:01:03 +0000 | [diff] [blame] | 5335 | NumDependencies, &DepWaitTaskArgs, |
| 5336 | Loc](CodeGenFunction &CGF, PrePostActionTy &) { |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5337 | CGOpenMPRuntime &RT = CGF.CGM.getOpenMPRuntime(); |
| Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 5338 | CodeGenFunction::RunCleanupsScope LocalScope(CGF); |
| 5339 | // Build void __kmpc_omp_wait_deps(ident_t *, kmp_int32 gtid, |
| 5340 | // kmp_int32 ndeps, kmp_depend_info_t *dep_list, kmp_int32 |
| 5341 | // ndeps_noalias, kmp_depend_info_t *noalias_dep_list); if dependence info |
| 5342 | // is specified. |
| John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5343 | if (NumDependencies) |
| Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5344 | CGF.EmitRuntimeCall(RT.createRuntimeFunction(OMPRTL__kmpc_omp_wait_deps), |
| Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 5345 | DepWaitTaskArgs); |
| Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5346 | // Call proxy_task_entry(gtid, new_task); |
| Alexey Bataev | 3c595a6 | 2017-08-14 15:01:03 +0000 | [diff] [blame] | 5347 | auto &&CodeGen = [TaskEntry, ThreadID, NewTaskNewTaskTTy, |
| 5348 | Loc](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5349 | Action.Enter(CGF); |
| 5350 | llvm::Value *OutlinedFnArgs[] = {ThreadID, NewTaskNewTaskTTy}; |
| Alexey Bataev | 3c595a6 | 2017-08-14 15:01:03 +0000 | [diff] [blame] | 5351 | CGF.CGM.getOpenMPRuntime().emitOutlinedFunctionCall(CGF, Loc, TaskEntry, |
| Alexey Bataev | 2c7eee5 | 2017-08-04 19:10:54 +0000 | [diff] [blame] | 5352 | OutlinedFnArgs); |
| Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5353 | }; |
| 5354 | |
| Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 5355 | // Build void __kmpc_omp_task_begin_if0(ident_t *, kmp_int32 gtid, |
| 5356 | // kmp_task_t *new_task); |
| Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 5357 | // Build void __kmpc_omp_task_complete_if0(ident_t *, kmp_int32 gtid, |
| 5358 | // kmp_task_t *new_task); |
| Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5359 | RegionCodeGenTy RCG(CodeGen); |
| 5360 | CommonActionTy Action( |
| 5361 | RT.createRuntimeFunction(OMPRTL__kmpc_omp_task_begin_if0), TaskArgs, |
| 5362 | RT.createRuntimeFunction(OMPRTL__kmpc_omp_task_complete_if0), TaskArgs); |
| 5363 | RCG.setAction(Action); |
| 5364 | RCG(CGF); |
| Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 5365 | }; |
| John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5366 | |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5367 | if (IfCond) { |
| Alexey Bataev | 1d943ae | 2019-11-19 12:12:23 -0500 | [diff] [blame] | 5368 | emitIfClause(CGF, IfCond, ThenCodeGen, ElseCodeGen); |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5369 | } else { |
| Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5370 | RegionCodeGenTy ThenRCG(ThenCodeGen); |
| 5371 | ThenRCG(CGF); |
| Alexey Bataev | f539faa | 2016-03-28 12:58:34 +0000 | [diff] [blame] | 5372 | } |
| Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 5373 | } |
| 5374 | |
| Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 5375 | void CGOpenMPRuntime::emitTaskLoopCall(CodeGenFunction &CGF, SourceLocation Loc, |
| 5376 | const OMPLoopDirective &D, |
| James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 5377 | llvm::Function *TaskFunction, |
| Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 5378 | QualType SharedsTy, Address Shareds, |
| 5379 | const Expr *IfCond, |
| 5380 | const OMPTaskDataTy &Data) { |
| Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 5381 | if (!CGF.HaveInsertPoint()) |
| 5382 | return; |
| Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 5383 | TaskResultTy Result = |
| 5384 | emitTaskInit(CGF, Loc, D, TaskFunction, SharedsTy, Shareds, Data); |
| Alexander Kornienko | 2a8c18d | 2018-04-06 15:14:32 +0000 | [diff] [blame] | 5385 | // NOTE: routine and part_id fields are initialized by __kmpc_omp_task_alloc() |
| Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 5386 | // libcall. |
| 5387 | // Call to void __kmpc_taskloop(ident_t *loc, int gtid, kmp_task_t *task, int |
| 5388 | // if_val, kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st, int nogroup, int |
| 5389 | // sched, kmp_uint64 grainsize, void *task_dup); |
| 5390 | llvm::Value *ThreadID = getThreadID(CGF, Loc); |
| 5391 | llvm::Value *UpLoc = emitUpdateLocation(CGF, Loc); |
| 5392 | llvm::Value *IfVal; |
| 5393 | if (IfCond) { |
| 5394 | IfVal = CGF.Builder.CreateIntCast(CGF.EvaluateExprAsBool(IfCond), CGF.IntTy, |
| 5395 | /*isSigned=*/true); |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5396 | } else { |
| Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 5397 | IfVal = llvm::ConstantInt::getSigned(CGF.IntTy, /*V=*/1); |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5398 | } |
| Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 5399 | |
| 5400 | LValue LBLVal = CGF.EmitLValueForField( |
| Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 5401 | Result.TDBase, |
| 5402 | *std::next(Result.KmpTaskTQTyRD->field_begin(), KmpTaskTLowerBound)); |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5403 | const auto *LBVar = |
| Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 5404 | cast<VarDecl>(cast<DeclRefExpr>(D.getLowerBoundVariable())->getDecl()); |
| Akira Hatanaka | f139ae3 | 2019-12-03 15:17:01 -0800 | [diff] [blame] | 5405 | CGF.EmitAnyExprToMem(LBVar->getInit(), LBLVal.getAddress(CGF), |
| 5406 | LBLVal.getQuals(), |
| Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 5407 | /*IsInitializer=*/true); |
| 5408 | LValue UBLVal = CGF.EmitLValueForField( |
| Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 5409 | Result.TDBase, |
| 5410 | *std::next(Result.KmpTaskTQTyRD->field_begin(), KmpTaskTUpperBound)); |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5411 | const auto *UBVar = |
| Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 5412 | cast<VarDecl>(cast<DeclRefExpr>(D.getUpperBoundVariable())->getDecl()); |
| Akira Hatanaka | f139ae3 | 2019-12-03 15:17:01 -0800 | [diff] [blame] | 5413 | CGF.EmitAnyExprToMem(UBVar->getInit(), UBLVal.getAddress(CGF), |
| 5414 | UBLVal.getQuals(), |
| Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 5415 | /*IsInitializer=*/true); |
| 5416 | LValue StLVal = CGF.EmitLValueForField( |
| Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 5417 | Result.TDBase, |
| 5418 | *std::next(Result.KmpTaskTQTyRD->field_begin(), KmpTaskTStride)); |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5419 | const auto *StVar = |
| Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 5420 | cast<VarDecl>(cast<DeclRefExpr>(D.getStrideVariable())->getDecl()); |
| Akira Hatanaka | f139ae3 | 2019-12-03 15:17:01 -0800 | [diff] [blame] | 5421 | CGF.EmitAnyExprToMem(StVar->getInit(), StLVal.getAddress(CGF), |
| 5422 | StLVal.getQuals(), |
| Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 5423 | /*IsInitializer=*/true); |
| Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 5424 | // Store reductions address. |
| 5425 | LValue RedLVal = CGF.EmitLValueForField( |
| 5426 | Result.TDBase, |
| 5427 | *std::next(Result.KmpTaskTQTyRD->field_begin(), KmpTaskTReductions)); |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5428 | if (Data.Reductions) { |
| Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 5429 | CGF.EmitStoreOfScalar(Data.Reductions, RedLVal); |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5430 | } else { |
| Akira Hatanaka | f139ae3 | 2019-12-03 15:17:01 -0800 | [diff] [blame] | 5431 | CGF.EmitNullInitialization(RedLVal.getAddress(CGF), |
| Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 5432 | CGF.getContext().VoidPtrTy); |
| 5433 | } |
| Alexey Bataev | 2b19a6f | 2016-04-28 09:15:06 +0000 | [diff] [blame] | 5434 | enum { NoSchedule = 0, Grainsize = 1, NumTasks = 2 }; |
| Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 5435 | llvm::Value *TaskArgs[] = { |
| Alexey Bataev | 3344603 | 2017-07-12 18:09:32 +0000 | [diff] [blame] | 5436 | UpLoc, |
| 5437 | ThreadID, |
| 5438 | Result.NewTask, |
| 5439 | IfVal, |
| Akira Hatanaka | f139ae3 | 2019-12-03 15:17:01 -0800 | [diff] [blame] | 5440 | LBLVal.getPointer(CGF), |
| 5441 | UBLVal.getPointer(CGF), |
| Alexey Bataev | a9b9cc0 | 2018-01-23 18:12:38 +0000 | [diff] [blame] | 5442 | CGF.EmitLoadOfScalar(StLVal, Loc), |
| Alexey Bataev | ac6e4de | 2018-10-24 19:06:37 +0000 | [diff] [blame] | 5443 | llvm::ConstantInt::getSigned( |
| Akira Hatanaka | f139ae3 | 2019-12-03 15:17:01 -0800 | [diff] [blame] | 5444 | CGF.IntTy, 1), // Always 1 because taskgroup emitted by the compiler |
| Alexey Bataev | 2b19a6f | 2016-04-28 09:15:06 +0000 | [diff] [blame] | 5445 | llvm::ConstantInt::getSigned( |
| Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 5446 | CGF.IntTy, Data.Schedule.getPointer() |
| 5447 | ? Data.Schedule.getInt() ? NumTasks : Grainsize |
| Alexey Bataev | 2b19a6f | 2016-04-28 09:15:06 +0000 | [diff] [blame] | 5448 | : NoSchedule), |
| Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 5449 | Data.Schedule.getPointer() |
| 5450 | ? CGF.Builder.CreateIntCast(Data.Schedule.getPointer(), CGF.Int64Ty, |
| Alexey Bataev | 2b19a6f | 2016-04-28 09:15:06 +0000 | [diff] [blame] | 5451 | /*isSigned=*/false) |
| 5452 | : llvm::ConstantInt::get(CGF.Int64Ty, /*V=*/0), |
| Alexey Bataev | 3344603 | 2017-07-12 18:09:32 +0000 | [diff] [blame] | 5453 | Result.TaskDupFn ? CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
| 5454 | Result.TaskDupFn, CGF.VoidPtrTy) |
| 5455 | : llvm::ConstantPointerNull::get(CGF.VoidPtrTy)}; |
| Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 5456 | CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_taskloop), TaskArgs); |
| 5457 | } |
| 5458 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 5459 | /// Emit reduction operation for each element of array (required for |
| Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5460 | /// array sections) LHS op = RHS. |
| 5461 | /// \param Type Type of array. |
| 5462 | /// \param LHSVar Variable on the left side of the reduction operation |
| 5463 | /// (references element of array in original variable). |
| 5464 | /// \param RHSVar Variable on the right side of the reduction operation |
| 5465 | /// (references element of array in original variable). |
| 5466 | /// \param RedOpGen Generator of reduction operation with use of LHSVar and |
| 5467 | /// RHSVar. |
| Benjamin Kramer | e003ca2 | 2015-10-28 13:54:16 +0000 | [diff] [blame] | 5468 | static void EmitOMPAggregateReduction( |
| Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5469 | CodeGenFunction &CGF, QualType Type, const VarDecl *LHSVar, |
| 5470 | const VarDecl *RHSVar, |
| 5471 | const llvm::function_ref<void(CodeGenFunction &CGF, const Expr *, |
| 5472 | const Expr *, const Expr *)> &RedOpGen, |
| 5473 | const Expr *XExpr = nullptr, const Expr *EExpr = nullptr, |
| 5474 | const Expr *UpExpr = nullptr) { |
| 5475 | // Perform element-by-element initialization. |
| 5476 | QualType ElementTy; |
| 5477 | Address LHSAddr = CGF.GetAddrOfLocalVar(LHSVar); |
| 5478 | Address RHSAddr = CGF.GetAddrOfLocalVar(RHSVar); |
| 5479 | |
| 5480 | // Drill down to the base element type on both arrays. |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5481 | const ArrayType *ArrayTy = Type->getAsArrayTypeUnsafe(); |
| 5482 | llvm::Value *NumElements = CGF.emitArrayLength(ArrayTy, ElementTy, LHSAddr); |
| Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5483 | |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5484 | llvm::Value *RHSBegin = RHSAddr.getPointer(); |
| 5485 | llvm::Value *LHSBegin = LHSAddr.getPointer(); |
| Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5486 | // Cast from pointer to array type to pointer to single element. |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5487 | llvm::Value *LHSEnd = CGF.Builder.CreateGEP(LHSBegin, NumElements); |
| Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5488 | // The basic structure here is a while-do loop. |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5489 | llvm::BasicBlock *BodyBB = CGF.createBasicBlock("omp.arraycpy.body"); |
| 5490 | llvm::BasicBlock *DoneBB = CGF.createBasicBlock("omp.arraycpy.done"); |
| 5491 | llvm::Value *IsEmpty = |
| Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5492 | CGF.Builder.CreateICmpEQ(LHSBegin, LHSEnd, "omp.arraycpy.isempty"); |
| 5493 | CGF.Builder.CreateCondBr(IsEmpty, DoneBB, BodyBB); |
| 5494 | |
| 5495 | // Enter the loop body, making that address the current address. |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5496 | llvm::BasicBlock *EntryBB = CGF.Builder.GetInsertBlock(); |
| Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5497 | CGF.EmitBlock(BodyBB); |
| 5498 | |
| 5499 | CharUnits ElementSize = CGF.getContext().getTypeSizeInChars(ElementTy); |
| 5500 | |
| 5501 | llvm::PHINode *RHSElementPHI = CGF.Builder.CreatePHI( |
| 5502 | RHSBegin->getType(), 2, "omp.arraycpy.srcElementPast"); |
| 5503 | RHSElementPHI->addIncoming(RHSBegin, EntryBB); |
| 5504 | Address RHSElementCurrent = |
| 5505 | Address(RHSElementPHI, |
| 5506 | RHSAddr.getAlignment().alignmentOfArrayElement(ElementSize)); |
| 5507 | |
| 5508 | llvm::PHINode *LHSElementPHI = CGF.Builder.CreatePHI( |
| 5509 | LHSBegin->getType(), 2, "omp.arraycpy.destElementPast"); |
| 5510 | LHSElementPHI->addIncoming(LHSBegin, EntryBB); |
| 5511 | Address LHSElementCurrent = |
| 5512 | Address(LHSElementPHI, |
| 5513 | LHSAddr.getAlignment().alignmentOfArrayElement(ElementSize)); |
| 5514 | |
| 5515 | // Emit copy. |
| 5516 | CodeGenFunction::OMPPrivateScope Scope(CGF); |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5517 | Scope.addPrivate(LHSVar, [=]() { return LHSElementCurrent; }); |
| 5518 | Scope.addPrivate(RHSVar, [=]() { return RHSElementCurrent; }); |
| Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5519 | Scope.Privatize(); |
| 5520 | RedOpGen(CGF, XExpr, EExpr, UpExpr); |
| 5521 | Scope.ForceCleanup(); |
| 5522 | |
| 5523 | // Shift the address forward by one element. |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5524 | llvm::Value *LHSElementNext = CGF.Builder.CreateConstGEP1_32( |
| Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5525 | LHSElementPHI, /*Idx0=*/1, "omp.arraycpy.dest.element"); |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5526 | llvm::Value *RHSElementNext = CGF.Builder.CreateConstGEP1_32( |
| Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5527 | RHSElementPHI, /*Idx0=*/1, "omp.arraycpy.src.element"); |
| 5528 | // Check whether we've reached the end. |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5529 | llvm::Value *Done = |
| Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5530 | CGF.Builder.CreateICmpEQ(LHSElementNext, LHSEnd, "omp.arraycpy.done"); |
| 5531 | CGF.Builder.CreateCondBr(Done, DoneBB, BodyBB); |
| 5532 | LHSElementPHI->addIncoming(LHSElementNext, CGF.Builder.GetInsertBlock()); |
| 5533 | RHSElementPHI->addIncoming(RHSElementNext, CGF.Builder.GetInsertBlock()); |
| 5534 | |
| 5535 | // Done. |
| 5536 | CGF.EmitBlock(DoneBB, /*IsFinished=*/true); |
| 5537 | } |
| 5538 | |
| Alexey Bataev | a839ddd | 2016-03-17 10:19:46 +0000 | [diff] [blame] | 5539 | /// Emit reduction combiner. If the combiner is a simple expression emit it as |
| 5540 | /// is, otherwise consider it as combiner of UDR decl and emit it as a call of |
| 5541 | /// UDR combiner function. |
| 5542 | static void emitReductionCombiner(CodeGenFunction &CGF, |
| 5543 | const Expr *ReductionOp) { |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5544 | if (const auto *CE = dyn_cast<CallExpr>(ReductionOp)) |
| 5545 | if (const auto *OVE = dyn_cast<OpaqueValueExpr>(CE->getCallee())) |
| 5546 | if (const auto *DRE = |
| Alexey Bataev | a839ddd | 2016-03-17 10:19:46 +0000 | [diff] [blame] | 5547 | dyn_cast<DeclRefExpr>(OVE->getSourceExpr()->IgnoreImpCasts())) |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5548 | if (const auto *DRD = |
| 5549 | dyn_cast<OMPDeclareReductionDecl>(DRE->getDecl())) { |
| Alexey Bataev | a839ddd | 2016-03-17 10:19:46 +0000 | [diff] [blame] | 5550 | std::pair<llvm::Function *, llvm::Function *> Reduction = |
| 5551 | CGF.CGM.getOpenMPRuntime().getUserDefinedReduction(DRD); |
| 5552 | RValue Func = RValue::get(Reduction.first); |
| 5553 | CodeGenFunction::OpaqueValueMapping Map(CGF, OVE, Func); |
| 5554 | CGF.EmitIgnoredExpr(ReductionOp); |
| 5555 | return; |
| 5556 | } |
| 5557 | CGF.EmitIgnoredExpr(ReductionOp); |
| 5558 | } |
| 5559 | |
| James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 5560 | llvm::Function *CGOpenMPRuntime::emitReductionFunction( |
| Alexey Bataev | 982a35e | 2019-03-19 17:09:52 +0000 | [diff] [blame] | 5561 | SourceLocation Loc, llvm::Type *ArgsType, ArrayRef<const Expr *> Privates, |
| 5562 | ArrayRef<const Expr *> LHSExprs, ArrayRef<const Expr *> RHSExprs, |
| 5563 | ArrayRef<const Expr *> ReductionOps) { |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5564 | ASTContext &C = CGM.getContext(); |
| Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5565 | |
| 5566 | // void reduction_func(void *LHSArg, void *RHSArg); |
| 5567 | FunctionArgList Args; |
| Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 5568 | ImplicitParamDecl LHSArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, C.VoidPtrTy, |
| 5569 | ImplicitParamDecl::Other); |
| 5570 | ImplicitParamDecl RHSArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, C.VoidPtrTy, |
| 5571 | ImplicitParamDecl::Other); |
| Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5572 | Args.push_back(&LHSArg); |
| 5573 | Args.push_back(&RHSArg); |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5574 | const auto &CGFI = |
| 5575 | CGM.getTypes().arrangeBuiltinFunctionDeclaration(C.VoidTy, Args); |
| Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 5576 | std::string Name = getName({"omp", "reduction", "reduction_func"}); |
| 5577 | auto *Fn = llvm::Function::Create(CGM.getTypes().GetFunctionType(CGFI), |
| 5578 | llvm::GlobalValue::InternalLinkage, Name, |
| 5579 | &CGM.getModule()); |
| Rafael Espindola | 51ec5a9 | 2018-02-28 23:46:35 +0000 | [diff] [blame] | 5580 | CGM.SetInternalFunctionAttributes(GlobalDecl(), Fn, CGFI); |
| Alexey Bataev | c0f879b | 2018-04-10 20:10:53 +0000 | [diff] [blame] | 5581 | Fn->setDoesNotRecurse(); |
| Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5582 | CodeGenFunction CGF(CGM); |
| Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 5583 | CGF.StartFunction(GlobalDecl(), C.VoidTy, Fn, CGFI, Args, Loc, Loc); |
| Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5584 | |
| 5585 | // Dst = (void*[n])(LHSArg); |
| 5586 | // Src = (void*[n])(RHSArg); |
| John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5587 | Address LHS(CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
| 5588 | CGF.Builder.CreateLoad(CGF.GetAddrOfLocalVar(&LHSArg)), |
| 5589 | ArgsType), CGF.getPointerAlign()); |
| 5590 | Address RHS(CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
| 5591 | CGF.Builder.CreateLoad(CGF.GetAddrOfLocalVar(&RHSArg)), |
| 5592 | ArgsType), CGF.getPointerAlign()); |
| Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5593 | |
| 5594 | // ... |
| 5595 | // *(Type<i>*)lhs[i] = RedOp<i>(*(Type<i>*)lhs[i], *(Type<i>*)rhs[i]); |
| 5596 | // ... |
| 5597 | CodeGenFunction::OMPPrivateScope Scope(CGF); |
| Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5598 | auto IPriv = Privates.begin(); |
| 5599 | unsigned Idx = 0; |
| 5600 | for (unsigned I = 0, E = ReductionOps.size(); I < E; ++I, ++IPriv, ++Idx) { |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5601 | const auto *RHSVar = |
| 5602 | cast<VarDecl>(cast<DeclRefExpr>(RHSExprs[I])->getDecl()); |
| 5603 | Scope.addPrivate(RHSVar, [&CGF, RHS, Idx, RHSVar]() { |
| Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5604 | return emitAddrOfVarFromArray(CGF, RHS, Idx, RHSVar); |
| John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5605 | }); |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5606 | const auto *LHSVar = |
| 5607 | cast<VarDecl>(cast<DeclRefExpr>(LHSExprs[I])->getDecl()); |
| 5608 | Scope.addPrivate(LHSVar, [&CGF, LHS, Idx, LHSVar]() { |
| Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5609 | return emitAddrOfVarFromArray(CGF, LHS, Idx, LHSVar); |
| John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5610 | }); |
| Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5611 | QualType PrivTy = (*IPriv)->getType(); |
| Alexey Bataev | 1189bd0 | 2016-01-26 12:20:39 +0000 | [diff] [blame] | 5612 | if (PrivTy->isVariablyModifiedType()) { |
| Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5613 | // Get array size and emit VLA type. |
| 5614 | ++Idx; |
| James Y Knight | 751fe28 | 2019-02-09 22:22:28 +0000 | [diff] [blame] | 5615 | Address Elem = CGF.Builder.CreateConstArrayGEP(LHS, Idx); |
| Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5616 | llvm::Value *Ptr = CGF.Builder.CreateLoad(Elem); |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5617 | const VariableArrayType *VLA = |
| 5618 | CGF.getContext().getAsVariableArrayType(PrivTy); |
| 5619 | const auto *OVE = cast<OpaqueValueExpr>(VLA->getSizeExpr()); |
| Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5620 | CodeGenFunction::OpaqueValueMapping OpaqueMap( |
| Alexey Bataev | 1189bd0 | 2016-01-26 12:20:39 +0000 | [diff] [blame] | 5621 | CGF, OVE, RValue::get(CGF.Builder.CreatePtrToInt(Ptr, CGF.SizeTy))); |
| Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5622 | CGF.EmitVariablyModifiedType(PrivTy); |
| 5623 | } |
| Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5624 | } |
| 5625 | Scope.Privatize(); |
| Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5626 | IPriv = Privates.begin(); |
| 5627 | auto ILHS = LHSExprs.begin(); |
| 5628 | auto IRHS = RHSExprs.begin(); |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5629 | for (const Expr *E : ReductionOps) { |
| Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5630 | if ((*IPriv)->getType()->isArrayType()) { |
| 5631 | // Emit reduction for array section. |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5632 | const auto *LHSVar = cast<VarDecl>(cast<DeclRefExpr>(*ILHS)->getDecl()); |
| 5633 | const auto *RHSVar = cast<VarDecl>(cast<DeclRefExpr>(*IRHS)->getDecl()); |
| Alexey Bataev | a839ddd | 2016-03-17 10:19:46 +0000 | [diff] [blame] | 5634 | EmitOMPAggregateReduction( |
| 5635 | CGF, (*IPriv)->getType(), LHSVar, RHSVar, |
| 5636 | [=](CodeGenFunction &CGF, const Expr *, const Expr *, const Expr *) { |
| 5637 | emitReductionCombiner(CGF, E); |
| 5638 | }); |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5639 | } else { |
| Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5640 | // Emit reduction for array subscript or single variable. |
| Alexey Bataev | a839ddd | 2016-03-17 10:19:46 +0000 | [diff] [blame] | 5641 | emitReductionCombiner(CGF, E); |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5642 | } |
| Richard Trieu | cc3949d | 2016-02-18 22:34:54 +0000 | [diff] [blame] | 5643 | ++IPriv; |
| 5644 | ++ILHS; |
| 5645 | ++IRHS; |
| Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5646 | } |
| 5647 | Scope.ForceCleanup(); |
| 5648 | CGF.FinishFunction(); |
| 5649 | return Fn; |
| 5650 | } |
| 5651 | |
| Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 5652 | void CGOpenMPRuntime::emitSingleReductionCombiner(CodeGenFunction &CGF, |
| 5653 | const Expr *ReductionOp, |
| 5654 | const Expr *PrivateRef, |
| 5655 | const DeclRefExpr *LHS, |
| 5656 | const DeclRefExpr *RHS) { |
| Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5657 | if (PrivateRef->getType()->isArrayType()) { |
| 5658 | // Emit reduction for array section. |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5659 | const auto *LHSVar = cast<VarDecl>(LHS->getDecl()); |
| 5660 | const auto *RHSVar = cast<VarDecl>(RHS->getDecl()); |
| Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5661 | EmitOMPAggregateReduction( |
| 5662 | CGF, PrivateRef->getType(), LHSVar, RHSVar, |
| 5663 | [=](CodeGenFunction &CGF, const Expr *, const Expr *, const Expr *) { |
| 5664 | emitReductionCombiner(CGF, ReductionOp); |
| 5665 | }); |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5666 | } else { |
| Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5667 | // Emit reduction for array subscript or single variable. |
| 5668 | emitReductionCombiner(CGF, ReductionOp); |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5669 | } |
| Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5670 | } |
| 5671 | |
| Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5672 | void CGOpenMPRuntime::emitReduction(CodeGenFunction &CGF, SourceLocation Loc, |
| Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5673 | ArrayRef<const Expr *> Privates, |
| Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5674 | ArrayRef<const Expr *> LHSExprs, |
| 5675 | ArrayRef<const Expr *> RHSExprs, |
| 5676 | ArrayRef<const Expr *> ReductionOps, |
| Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 5677 | ReductionOptionsTy Options) { |
| Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 5678 | if (!CGF.HaveInsertPoint()) |
| 5679 | return; |
| Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 5680 | |
| 5681 | bool WithNowait = Options.WithNowait; |
| 5682 | bool SimpleReduction = Options.SimpleReduction; |
| 5683 | |
| Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5684 | // Next code should be emitted for reduction: |
| 5685 | // |
| 5686 | // static kmp_critical_name lock = { 0 }; |
| 5687 | // |
| 5688 | // void reduce_func(void *lhs[<n>], void *rhs[<n>]) { |
| 5689 | // *(Type0*)lhs[0] = ReductionOperation0(*(Type0*)lhs[0], *(Type0*)rhs[0]); |
| 5690 | // ... |
| 5691 | // *(Type<n>-1*)lhs[<n>-1] = ReductionOperation<n>-1(*(Type<n>-1*)lhs[<n>-1], |
| 5692 | // *(Type<n>-1*)rhs[<n>-1]); |
| 5693 | // } |
| 5694 | // |
| 5695 | // ... |
| 5696 | // void *RedList[<n>] = {&<RHSExprs>[0], ..., &<RHSExprs>[<n>-1]}; |
| 5697 | // switch (__kmpc_reduce{_nowait}(<loc>, <gtid>, <n>, sizeof(RedList), |
| 5698 | // RedList, reduce_func, &<lock>)) { |
| 5699 | // case 1: |
| 5700 | // ... |
| 5701 | // <LHSExprs>[i] = RedOp<i>(*<LHSExprs>[i], *<RHSExprs>[i]); |
| 5702 | // ... |
| 5703 | // __kmpc_end_reduce{_nowait}(<loc>, <gtid>, &<lock>); |
| 5704 | // break; |
| 5705 | // case 2: |
| 5706 | // ... |
| 5707 | // Atomic(<LHSExprs>[i] = RedOp<i>(*<LHSExprs>[i], *<RHSExprs>[i])); |
| 5708 | // ... |
| Alexey Bataev | 69a4779 | 2015-05-07 03:54:03 +0000 | [diff] [blame] | 5709 | // [__kmpc_end_reduce(<loc>, <gtid>, &<lock>);] |
| Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5710 | // break; |
| 5711 | // default:; |
| 5712 | // } |
| Alexey Bataev | 89e7e8e | 2015-06-17 06:21:39 +0000 | [diff] [blame] | 5713 | // |
| 5714 | // if SimpleReduction is true, only the next code is generated: |
| 5715 | // ... |
| 5716 | // <LHSExprs>[i] = RedOp<i>(*<LHSExprs>[i], *<RHSExprs>[i]); |
| 5717 | // ... |
| Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5718 | |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5719 | ASTContext &C = CGM.getContext(); |
| Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5720 | |
| Alexey Bataev | 89e7e8e | 2015-06-17 06:21:39 +0000 | [diff] [blame] | 5721 | if (SimpleReduction) { |
| 5722 | CodeGenFunction::RunCleanupsScope Scope(CGF); |
| Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5723 | auto IPriv = Privates.begin(); |
| 5724 | auto ILHS = LHSExprs.begin(); |
| 5725 | auto IRHS = RHSExprs.begin(); |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5726 | for (const Expr *E : ReductionOps) { |
| Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5727 | emitSingleReductionCombiner(CGF, E, *IPriv, cast<DeclRefExpr>(*ILHS), |
| 5728 | cast<DeclRefExpr>(*IRHS)); |
| Richard Trieu | cc3949d | 2016-02-18 22:34:54 +0000 | [diff] [blame] | 5729 | ++IPriv; |
| 5730 | ++ILHS; |
| 5731 | ++IRHS; |
| Alexey Bataev | 89e7e8e | 2015-06-17 06:21:39 +0000 | [diff] [blame] | 5732 | } |
| 5733 | return; |
| 5734 | } |
| 5735 | |
| Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5736 | // 1. Build a list of reduction variables. |
| 5737 | // void *RedList[<n>] = {<ReductionVars>[0], ..., <ReductionVars>[<n>-1]}; |
| Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5738 | auto Size = RHSExprs.size(); |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5739 | for (const Expr *E : Privates) { |
| Alexey Bataev | 1189bd0 | 2016-01-26 12:20:39 +0000 | [diff] [blame] | 5740 | if (E->getType()->isVariablyModifiedType()) |
| Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5741 | // Reserve place for array size. |
| 5742 | ++Size; |
| 5743 | } |
| 5744 | llvm::APInt ArraySize(/*unsigned int numBits=*/32, Size); |
| Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5745 | QualType ReductionArrayTy = |
| Richard Smith | 772e266 | 2019-10-04 01:25:59 +0000 | [diff] [blame] | 5746 | C.getConstantArrayType(C.VoidPtrTy, ArraySize, nullptr, ArrayType::Normal, |
| Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5747 | /*IndexTypeQuals=*/0); |
| John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5748 | Address ReductionList = |
| Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5749 | CGF.CreateMemTemp(ReductionArrayTy, ".omp.reduction.red_list"); |
| Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5750 | auto IPriv = Privates.begin(); |
| 5751 | unsigned Idx = 0; |
| 5752 | for (unsigned I = 0, E = RHSExprs.size(); I < E; ++I, ++IPriv, ++Idx) { |
| James Y Knight | 751fe28 | 2019-02-09 22:22:28 +0000 | [diff] [blame] | 5753 | Address Elem = CGF.Builder.CreateConstArrayGEP(ReductionList, Idx); |
| John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5754 | CGF.Builder.CreateStore( |
| Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5755 | CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
| Akira Hatanaka | f139ae3 | 2019-12-03 15:17:01 -0800 | [diff] [blame] | 5756 | CGF.EmitLValue(RHSExprs[I]).getPointer(CGF), CGF.VoidPtrTy), |
| John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5757 | Elem); |
| Alexey Bataev | 1189bd0 | 2016-01-26 12:20:39 +0000 | [diff] [blame] | 5758 | if ((*IPriv)->getType()->isVariablyModifiedType()) { |
| Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5759 | // Store array size. |
| 5760 | ++Idx; |
| James Y Knight | 751fe28 | 2019-02-09 22:22:28 +0000 | [diff] [blame] | 5761 | Elem = CGF.Builder.CreateConstArrayGEP(ReductionList, Idx); |
| Alexey Bataev | 1189bd0 | 2016-01-26 12:20:39 +0000 | [diff] [blame] | 5762 | llvm::Value *Size = CGF.Builder.CreateIntCast( |
| 5763 | CGF.getVLASize( |
| 5764 | CGF.getContext().getAsVariableArrayType((*IPriv)->getType())) |
| Sander de Smalen | 891af03a | 2018-02-03 13:55:59 +0000 | [diff] [blame] | 5765 | .NumElts, |
| Alexey Bataev | 1189bd0 | 2016-01-26 12:20:39 +0000 | [diff] [blame] | 5766 | CGF.SizeTy, /*isSigned=*/false); |
| 5767 | CGF.Builder.CreateStore(CGF.Builder.CreateIntToPtr(Size, CGF.VoidPtrTy), |
| 5768 | Elem); |
| Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5769 | } |
| Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5770 | } |
| 5771 | |
| 5772 | // 2. Emit reduce_func(). |
| James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 5773 | llvm::Function *ReductionFn = emitReductionFunction( |
| Alexey Bataev | 982a35e | 2019-03-19 17:09:52 +0000 | [diff] [blame] | 5774 | Loc, CGF.ConvertTypeForMem(ReductionArrayTy)->getPointerTo(), Privates, |
| 5775 | LHSExprs, RHSExprs, ReductionOps); |
| Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5776 | |
| 5777 | // 3. Create static kmp_critical_name lock = { 0 }; |
| Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 5778 | std::string Name = getName({"reduction"}); |
| 5779 | llvm::Value *Lock = getCriticalRegionLock(Name); |
| Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5780 | |
| 5781 | // 4. Build res = __kmpc_reduce{_nowait}(<loc>, <gtid>, <n>, sizeof(RedList), |
| 5782 | // RedList, reduce_func, &<lock>); |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5783 | llvm::Value *IdentTLoc = emitUpdateLocation(CGF, Loc, OMP_ATOMIC_REDUCE); |
| 5784 | llvm::Value *ThreadId = getThreadID(CGF, Loc); |
| 5785 | llvm::Value *ReductionArrayTySize = CGF.getTypeSize(ReductionArrayTy); |
| 5786 | llvm::Value *RL = CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
| Samuel Antao | 4c8035b | 2016-12-12 18:00:20 +0000 | [diff] [blame] | 5787 | ReductionList.getPointer(), CGF.VoidPtrTy); |
| Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5788 | llvm::Value *Args[] = { |
| 5789 | IdentTLoc, // ident_t *<loc> |
| 5790 | ThreadId, // i32 <gtid> |
| 5791 | CGF.Builder.getInt32(RHSExprs.size()), // i32 <n> |
| 5792 | ReductionArrayTySize, // size_type sizeof(RedList) |
| 5793 | RL, // void *RedList |
| 5794 | ReductionFn, // void (*) (void *, void *) <reduce_func> |
| 5795 | Lock // kmp_critical_name *&<lock> |
| 5796 | }; |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5797 | llvm::Value *Res = CGF.EmitRuntimeCall( |
| Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5798 | createRuntimeFunction(WithNowait ? OMPRTL__kmpc_reduce_nowait |
| 5799 | : OMPRTL__kmpc_reduce), |
| 5800 | Args); |
| 5801 | |
| 5802 | // 5. Build switch(res) |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5803 | llvm::BasicBlock *DefaultBB = CGF.createBasicBlock(".omp.reduction.default"); |
| 5804 | llvm::SwitchInst *SwInst = |
| 5805 | CGF.Builder.CreateSwitch(Res, DefaultBB, /*NumCases=*/2); |
| Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5806 | |
| 5807 | // 6. Build case 1: |
| 5808 | // ... |
| 5809 | // <LHSExprs>[i] = RedOp<i>(*<LHSExprs>[i], *<RHSExprs>[i]); |
| 5810 | // ... |
| 5811 | // __kmpc_end_reduce{_nowait}(<loc>, <gtid>, &<lock>); |
| 5812 | // break; |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5813 | llvm::BasicBlock *Case1BB = CGF.createBasicBlock(".omp.reduction.case1"); |
| Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5814 | SwInst->addCase(CGF.Builder.getInt32(1), Case1BB); |
| 5815 | CGF.EmitBlock(Case1BB); |
| 5816 | |
| Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5817 | // Add emission of __kmpc_end_reduce{_nowait}(<loc>, <gtid>, &<lock>); |
| 5818 | llvm::Value *EndArgs[] = { |
| 5819 | IdentTLoc, // ident_t *<loc> |
| 5820 | ThreadId, // i32 <gtid> |
| 5821 | Lock // kmp_critical_name *&<lock> |
| 5822 | }; |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5823 | auto &&CodeGen = [Privates, LHSExprs, RHSExprs, ReductionOps]( |
| 5824 | CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 5825 | CGOpenMPRuntime &RT = CGF.CGM.getOpenMPRuntime(); |
| Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5826 | auto IPriv = Privates.begin(); |
| 5827 | auto ILHS = LHSExprs.begin(); |
| 5828 | auto IRHS = RHSExprs.begin(); |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5829 | for (const Expr *E : ReductionOps) { |
| Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 5830 | RT.emitSingleReductionCombiner(CGF, E, *IPriv, cast<DeclRefExpr>(*ILHS), |
| 5831 | cast<DeclRefExpr>(*IRHS)); |
| Richard Trieu | cc3949d | 2016-02-18 22:34:54 +0000 | [diff] [blame] | 5832 | ++IPriv; |
| 5833 | ++ILHS; |
| 5834 | ++IRHS; |
| Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5835 | } |
| Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5836 | }; |
| 5837 | RegionCodeGenTy RCG(CodeGen); |
| 5838 | CommonActionTy Action( |
| 5839 | nullptr, llvm::None, |
| 5840 | createRuntimeFunction(WithNowait ? OMPRTL__kmpc_end_reduce_nowait |
| 5841 | : OMPRTL__kmpc_end_reduce), |
| 5842 | EndArgs); |
| 5843 | RCG.setAction(Action); |
| 5844 | RCG(CGF); |
| Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5845 | |
| 5846 | CGF.EmitBranch(DefaultBB); |
| 5847 | |
| 5848 | // 7. Build case 2: |
| 5849 | // ... |
| 5850 | // Atomic(<LHSExprs>[i] = RedOp<i>(*<LHSExprs>[i], *<RHSExprs>[i])); |
| 5851 | // ... |
| 5852 | // break; |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5853 | llvm::BasicBlock *Case2BB = CGF.createBasicBlock(".omp.reduction.case2"); |
| Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5854 | SwInst->addCase(CGF.Builder.getInt32(2), Case2BB); |
| 5855 | CGF.EmitBlock(Case2BB); |
| 5856 | |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5857 | auto &&AtomicCodeGen = [Loc, Privates, LHSExprs, RHSExprs, ReductionOps]( |
| 5858 | CodeGenFunction &CGF, PrePostActionTy &Action) { |
| Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5859 | auto ILHS = LHSExprs.begin(); |
| 5860 | auto IRHS = RHSExprs.begin(); |
| 5861 | auto IPriv = Privates.begin(); |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5862 | for (const Expr *E : ReductionOps) { |
| Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5863 | const Expr *XExpr = nullptr; |
| 5864 | const Expr *EExpr = nullptr; |
| 5865 | const Expr *UpExpr = nullptr; |
| 5866 | BinaryOperatorKind BO = BO_Comma; |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5867 | if (const auto *BO = dyn_cast<BinaryOperator>(E)) { |
| Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5868 | if (BO->getOpcode() == BO_Assign) { |
| 5869 | XExpr = BO->getLHS(); |
| 5870 | UpExpr = BO->getRHS(); |
| Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5871 | } |
| Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5872 | } |
| 5873 | // Try to emit update expression as a simple atomic. |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5874 | const Expr *RHSExpr = UpExpr; |
| Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5875 | if (RHSExpr) { |
| 5876 | // Analyze RHS part of the whole expression. |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5877 | if (const auto *ACO = dyn_cast<AbstractConditionalOperator>( |
| Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5878 | RHSExpr->IgnoreParenImpCasts())) { |
| 5879 | // If this is a conditional operator, analyze its condition for |
| 5880 | // min/max reduction operator. |
| 5881 | RHSExpr = ACO->getCond(); |
| Alexey Bataev | 69a4779 | 2015-05-07 03:54:03 +0000 | [diff] [blame] | 5882 | } |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5883 | if (const auto *BORHS = |
| Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5884 | dyn_cast<BinaryOperator>(RHSExpr->IgnoreParenImpCasts())) { |
| 5885 | EExpr = BORHS->getRHS(); |
| 5886 | BO = BORHS->getOpcode(); |
| Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5887 | } |
| Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5888 | } |
| 5889 | if (XExpr) { |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5890 | const auto *VD = cast<VarDecl>(cast<DeclRefExpr>(*ILHS)->getDecl()); |
| Malcolm Parsons | c6e4583 | 2017-01-13 18:55:32 +0000 | [diff] [blame] | 5891 | auto &&AtomicRedGen = [BO, VD, |
| Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5892 | Loc](CodeGenFunction &CGF, const Expr *XExpr, |
| 5893 | const Expr *EExpr, const Expr *UpExpr) { |
| 5894 | LValue X = CGF.EmitLValue(XExpr); |
| 5895 | RValue E; |
| 5896 | if (EExpr) |
| 5897 | E = CGF.EmitAnyExpr(EExpr); |
| 5898 | CGF.EmitOMPAtomicSimpleUpdateExpr( |
| JF Bastien | 92f4ef1 | 2016-04-06 17:26:42 +0000 | [diff] [blame] | 5899 | X, E, BO, /*IsXLHSInRHSPart=*/true, |
| 5900 | llvm::AtomicOrdering::Monotonic, Loc, |
| Malcolm Parsons | c6e4583 | 2017-01-13 18:55:32 +0000 | [diff] [blame] | 5901 | [&CGF, UpExpr, VD, Loc](RValue XRValue) { |
| Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5902 | CodeGenFunction::OMPPrivateScope PrivateScope(CGF); |
| 5903 | PrivateScope.addPrivate( |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5904 | VD, [&CGF, VD, XRValue, Loc]() { |
| Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5905 | Address LHSTemp = CGF.CreateMemTemp(VD->getType()); |
| 5906 | CGF.emitOMPSimpleStore( |
| 5907 | CGF.MakeAddrLValue(LHSTemp, VD->getType()), XRValue, |
| 5908 | VD->getType().getNonReferenceType(), Loc); |
| 5909 | return LHSTemp; |
| 5910 | }); |
| 5911 | (void)PrivateScope.Privatize(); |
| 5912 | return CGF.EmitAnyExpr(UpExpr); |
| 5913 | }); |
| 5914 | }; |
| 5915 | if ((*IPriv)->getType()->isArrayType()) { |
| 5916 | // Emit atomic reduction for array section. |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5917 | const auto *RHSVar = |
| 5918 | cast<VarDecl>(cast<DeclRefExpr>(*IRHS)->getDecl()); |
| Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5919 | EmitOMPAggregateReduction(CGF, (*IPriv)->getType(), VD, RHSVar, |
| 5920 | AtomicRedGen, XExpr, EExpr, UpExpr); |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5921 | } else { |
| Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5922 | // Emit atomic reduction for array subscript or single variable. |
| 5923 | AtomicRedGen(CGF, XExpr, EExpr, UpExpr); |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5924 | } |
| Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5925 | } else { |
| 5926 | // Emit as a critical region. |
| 5927 | auto &&CritRedGen = [E, Loc](CodeGenFunction &CGF, const Expr *, |
| Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 5928 | const Expr *, const Expr *) { |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5929 | CGOpenMPRuntime &RT = CGF.CGM.getOpenMPRuntime(); |
| Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 5930 | std::string Name = RT.getName({"atomic_reduction"}); |
| Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5931 | RT.emitCriticalRegion( |
| Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 5932 | CGF, Name, |
| Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5933 | [=](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 5934 | Action.Enter(CGF); |
| 5935 | emitReductionCombiner(CGF, E); |
| 5936 | }, |
| 5937 | Loc); |
| 5938 | }; |
| 5939 | if ((*IPriv)->getType()->isArrayType()) { |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5940 | const auto *LHSVar = |
| 5941 | cast<VarDecl>(cast<DeclRefExpr>(*ILHS)->getDecl()); |
| 5942 | const auto *RHSVar = |
| 5943 | cast<VarDecl>(cast<DeclRefExpr>(*IRHS)->getDecl()); |
| Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5944 | EmitOMPAggregateReduction(CGF, (*IPriv)->getType(), LHSVar, RHSVar, |
| 5945 | CritRedGen); |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5946 | } else { |
| Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5947 | CritRedGen(CGF, nullptr, nullptr, nullptr); |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5948 | } |
| Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5949 | } |
| Richard Trieu | cc3949d | 2016-02-18 22:34:54 +0000 | [diff] [blame] | 5950 | ++ILHS; |
| 5951 | ++IRHS; |
| 5952 | ++IPriv; |
| Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5953 | } |
| Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5954 | }; |
| 5955 | RegionCodeGenTy AtomicRCG(AtomicCodeGen); |
| 5956 | if (!WithNowait) { |
| 5957 | // Add emission of __kmpc_end_reduce(<loc>, <gtid>, &<lock>); |
| 5958 | llvm::Value *EndArgs[] = { |
| 5959 | IdentTLoc, // ident_t *<loc> |
| 5960 | ThreadId, // i32 <gtid> |
| 5961 | Lock // kmp_critical_name *&<lock> |
| 5962 | }; |
| 5963 | CommonActionTy Action(nullptr, llvm::None, |
| 5964 | createRuntimeFunction(OMPRTL__kmpc_end_reduce), |
| 5965 | EndArgs); |
| 5966 | AtomicRCG.setAction(Action); |
| 5967 | AtomicRCG(CGF); |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5968 | } else { |
| Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5969 | AtomicRCG(CGF); |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5970 | } |
| Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5971 | |
| 5972 | CGF.EmitBranch(DefaultBB); |
| 5973 | CGF.EmitBlock(DefaultBB, /*IsFinished=*/true); |
| 5974 | } |
| 5975 | |
| Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 5976 | /// Generates unique name for artificial threadprivate variables. |
| Alexey Bataev | 1c44e15 | 2018-03-06 18:59:43 +0000 | [diff] [blame] | 5977 | /// Format is: <Prefix> "." <Decl_mangled_name> "_" "<Decl_start_loc_raw_enc>" |
| 5978 | static std::string generateUniqueName(CodeGenModule &CGM, StringRef Prefix, |
| 5979 | const Expr *Ref) { |
| Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 5980 | SmallString<256> Buffer; |
| 5981 | llvm::raw_svector_ostream Out(Buffer); |
| Alexey Bataev | 1c44e15 | 2018-03-06 18:59:43 +0000 | [diff] [blame] | 5982 | const clang::DeclRefExpr *DE; |
| 5983 | const VarDecl *D = ::getBaseDecl(Ref, DE); |
| 5984 | if (!D) |
| 5985 | D = cast<VarDecl>(cast<DeclRefExpr>(Ref)->getDecl()); |
| 5986 | D = D->getCanonicalDecl(); |
| Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 5987 | std::string Name = CGM.getOpenMPRuntime().getName( |
| 5988 | {D->isLocalVarDeclOrParm() ? D->getName() : CGM.getMangledName(D)}); |
| 5989 | Out << Prefix << Name << "_" |
| Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 5990 | << D->getCanonicalDecl()->getBeginLoc().getRawEncoding(); |
| Benjamin Kramer | adcd026 | 2020-01-28 20:23:46 +0100 | [diff] [blame] | 5991 | return std::string(Out.str()); |
| Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 5992 | } |
| 5993 | |
| 5994 | /// Emits reduction initializer function: |
| 5995 | /// \code |
| 5996 | /// void @.red_init(void* %arg) { |
| 5997 | /// %0 = bitcast void* %arg to <type>* |
| 5998 | /// store <type> <init>, <type>* %0 |
| 5999 | /// ret void |
| 6000 | /// } |
| 6001 | /// \endcode |
| 6002 | static llvm::Value *emitReduceInitFunction(CodeGenModule &CGM, |
| 6003 | SourceLocation Loc, |
| 6004 | ReductionCodeGen &RCG, unsigned N) { |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6005 | ASTContext &C = CGM.getContext(); |
| Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 6006 | FunctionArgList Args; |
| Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 6007 | ImplicitParamDecl Param(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, C.VoidPtrTy, |
| 6008 | ImplicitParamDecl::Other); |
| Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 6009 | Args.emplace_back(&Param); |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6010 | const auto &FnInfo = |
| Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 6011 | CGM.getTypes().arrangeBuiltinFunctionDeclaration(C.VoidTy, Args); |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6012 | llvm::FunctionType *FnTy = CGM.getTypes().GetFunctionType(FnInfo); |
| Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 6013 | std::string Name = CGM.getOpenMPRuntime().getName({"red_init", ""}); |
| Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 6014 | auto *Fn = llvm::Function::Create(FnTy, llvm::GlobalValue::InternalLinkage, |
| Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 6015 | Name, &CGM.getModule()); |
| Rafael Espindola | 51ec5a9 | 2018-02-28 23:46:35 +0000 | [diff] [blame] | 6016 | CGM.SetInternalFunctionAttributes(GlobalDecl(), Fn, FnInfo); |
| Alexey Bataev | c0f879b | 2018-04-10 20:10:53 +0000 | [diff] [blame] | 6017 | Fn->setDoesNotRecurse(); |
| Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 6018 | CodeGenFunction CGF(CGM); |
| Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 6019 | CGF.StartFunction(GlobalDecl(), C.VoidTy, Fn, FnInfo, Args, Loc, Loc); |
| Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 6020 | Address PrivateAddr = CGF.EmitLoadOfPointer( |
| 6021 | CGF.GetAddrOfLocalVar(&Param), |
| 6022 | C.getPointerType(C.VoidPtrTy).castAs<PointerType>()); |
| 6023 | llvm::Value *Size = nullptr; |
| 6024 | // If the size of the reduction item is non-constant, load it from global |
| 6025 | // threadprivate variable. |
| 6026 | if (RCG.getSizes(N).second) { |
| 6027 | Address SizeAddr = CGM.getOpenMPRuntime().getAddrOfArtificialThreadPrivate( |
| 6028 | CGF, CGM.getContext().getSizeType(), |
| Alexey Bataev | 1c44e15 | 2018-03-06 18:59:43 +0000 | [diff] [blame] | 6029 | generateUniqueName(CGM, "reduction_size", RCG.getRefExpr(N))); |
| Alexey Bataev | a9b9cc0 | 2018-01-23 18:12:38 +0000 | [diff] [blame] | 6030 | Size = CGF.EmitLoadOfScalar(SizeAddr, /*Volatile=*/false, |
| 6031 | CGM.getContext().getSizeType(), Loc); |
| Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 6032 | } |
| 6033 | RCG.emitAggregateType(CGF, N, Size); |
| 6034 | LValue SharedLVal; |
| 6035 | // If initializer uses initializer from declare reduction construct, emit a |
| 6036 | // pointer to the address of the original reduction item (reuired by reduction |
| 6037 | // initializer) |
| 6038 | if (RCG.usesReductionInitializer(N)) { |
| 6039 | Address SharedAddr = |
| 6040 | CGM.getOpenMPRuntime().getAddrOfArtificialThreadPrivate( |
| 6041 | CGF, CGM.getContext().VoidPtrTy, |
| Alexey Bataev | 1c44e15 | 2018-03-06 18:59:43 +0000 | [diff] [blame] | 6042 | generateUniqueName(CGM, "reduction", RCG.getRefExpr(N))); |
| Alexey Bataev | 21dab12 | 2018-03-09 15:20:30 +0000 | [diff] [blame] | 6043 | SharedAddr = CGF.EmitLoadOfPointer( |
| 6044 | SharedAddr, |
| 6045 | CGM.getContext().VoidPtrTy.castAs<PointerType>()->getTypePtr()); |
| Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 6046 | SharedLVal = CGF.MakeAddrLValue(SharedAddr, CGM.getContext().VoidPtrTy); |
| 6047 | } else { |
| 6048 | SharedLVal = CGF.MakeNaturalAlignAddrLValue( |
| 6049 | llvm::ConstantPointerNull::get(CGM.VoidPtrTy), |
| 6050 | CGM.getContext().VoidPtrTy); |
| 6051 | } |
| 6052 | // Emit the initializer: |
| 6053 | // %0 = bitcast void* %arg to <type>* |
| 6054 | // store <type> <init>, <type>* %0 |
| 6055 | RCG.emitInitialization(CGF, N, PrivateAddr, SharedLVal, |
| 6056 | [](CodeGenFunction &) { return false; }); |
| 6057 | CGF.FinishFunction(); |
| 6058 | return Fn; |
| 6059 | } |
| 6060 | |
| 6061 | /// Emits reduction combiner function: |
| 6062 | /// \code |
| 6063 | /// void @.red_comb(void* %arg0, void* %arg1) { |
| 6064 | /// %lhs = bitcast void* %arg0 to <type>* |
| 6065 | /// %rhs = bitcast void* %arg1 to <type>* |
| 6066 | /// %2 = <ReductionOp>(<type>* %lhs, <type>* %rhs) |
| 6067 | /// store <type> %2, <type>* %lhs |
| 6068 | /// ret void |
| 6069 | /// } |
| 6070 | /// \endcode |
| 6071 | static llvm::Value *emitReduceCombFunction(CodeGenModule &CGM, |
| 6072 | SourceLocation Loc, |
| 6073 | ReductionCodeGen &RCG, unsigned N, |
| 6074 | const Expr *ReductionOp, |
| 6075 | const Expr *LHS, const Expr *RHS, |
| 6076 | const Expr *PrivateRef) { |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6077 | ASTContext &C = CGM.getContext(); |
| 6078 | const auto *LHSVD = cast<VarDecl>(cast<DeclRefExpr>(LHS)->getDecl()); |
| 6079 | const auto *RHSVD = cast<VarDecl>(cast<DeclRefExpr>(RHS)->getDecl()); |
| Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 6080 | FunctionArgList Args; |
| Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 6081 | ImplicitParamDecl ParamInOut(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, |
| 6082 | C.VoidPtrTy, ImplicitParamDecl::Other); |
| 6083 | ImplicitParamDecl ParamIn(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, C.VoidPtrTy, |
| 6084 | ImplicitParamDecl::Other); |
| Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 6085 | Args.emplace_back(&ParamInOut); |
| 6086 | Args.emplace_back(&ParamIn); |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6087 | const auto &FnInfo = |
| Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 6088 | CGM.getTypes().arrangeBuiltinFunctionDeclaration(C.VoidTy, Args); |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6089 | llvm::FunctionType *FnTy = CGM.getTypes().GetFunctionType(FnInfo); |
| Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 6090 | std::string Name = CGM.getOpenMPRuntime().getName({"red_comb", ""}); |
| Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 6091 | auto *Fn = llvm::Function::Create(FnTy, llvm::GlobalValue::InternalLinkage, |
| Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 6092 | Name, &CGM.getModule()); |
| Rafael Espindola | 51ec5a9 | 2018-02-28 23:46:35 +0000 | [diff] [blame] | 6093 | CGM.SetInternalFunctionAttributes(GlobalDecl(), Fn, FnInfo); |
| Alexey Bataev | c0f879b | 2018-04-10 20:10:53 +0000 | [diff] [blame] | 6094 | Fn->setDoesNotRecurse(); |
| Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 6095 | CodeGenFunction CGF(CGM); |
| Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 6096 | CGF.StartFunction(GlobalDecl(), C.VoidTy, Fn, FnInfo, Args, Loc, Loc); |
| Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 6097 | llvm::Value *Size = nullptr; |
| 6098 | // If the size of the reduction item is non-constant, load it from global |
| 6099 | // threadprivate variable. |
| 6100 | if (RCG.getSizes(N).second) { |
| 6101 | Address SizeAddr = CGM.getOpenMPRuntime().getAddrOfArtificialThreadPrivate( |
| 6102 | CGF, CGM.getContext().getSizeType(), |
| Alexey Bataev | 1c44e15 | 2018-03-06 18:59:43 +0000 | [diff] [blame] | 6103 | generateUniqueName(CGM, "reduction_size", RCG.getRefExpr(N))); |
| Alexey Bataev | a9b9cc0 | 2018-01-23 18:12:38 +0000 | [diff] [blame] | 6104 | Size = CGF.EmitLoadOfScalar(SizeAddr, /*Volatile=*/false, |
| 6105 | CGM.getContext().getSizeType(), Loc); |
| Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 6106 | } |
| 6107 | RCG.emitAggregateType(CGF, N, Size); |
| 6108 | // Remap lhs and rhs variables to the addresses of the function arguments. |
| 6109 | // %lhs = bitcast void* %arg0 to <type>* |
| 6110 | // %rhs = bitcast void* %arg1 to <type>* |
| 6111 | CodeGenFunction::OMPPrivateScope PrivateScope(CGF); |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6112 | PrivateScope.addPrivate(LHSVD, [&C, &CGF, &ParamInOut, LHSVD]() { |
| Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 6113 | // Pull out the pointer to the variable. |
| 6114 | Address PtrAddr = CGF.EmitLoadOfPointer( |
| 6115 | CGF.GetAddrOfLocalVar(&ParamInOut), |
| 6116 | C.getPointerType(C.VoidPtrTy).castAs<PointerType>()); |
| 6117 | return CGF.Builder.CreateElementBitCast( |
| 6118 | PtrAddr, CGF.ConvertTypeForMem(LHSVD->getType())); |
| 6119 | }); |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6120 | PrivateScope.addPrivate(RHSVD, [&C, &CGF, &ParamIn, RHSVD]() { |
| Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 6121 | // Pull out the pointer to the variable. |
| 6122 | Address PtrAddr = CGF.EmitLoadOfPointer( |
| 6123 | CGF.GetAddrOfLocalVar(&ParamIn), |
| 6124 | C.getPointerType(C.VoidPtrTy).castAs<PointerType>()); |
| 6125 | return CGF.Builder.CreateElementBitCast( |
| 6126 | PtrAddr, CGF.ConvertTypeForMem(RHSVD->getType())); |
| 6127 | }); |
| 6128 | PrivateScope.Privatize(); |
| 6129 | // Emit the combiner body: |
| 6130 | // %2 = <ReductionOp>(<type> *%lhs, <type> *%rhs) |
| 6131 | // store <type> %2, <type>* %lhs |
| 6132 | CGM.getOpenMPRuntime().emitSingleReductionCombiner( |
| 6133 | CGF, ReductionOp, PrivateRef, cast<DeclRefExpr>(LHS), |
| 6134 | cast<DeclRefExpr>(RHS)); |
| 6135 | CGF.FinishFunction(); |
| 6136 | return Fn; |
| 6137 | } |
| 6138 | |
| 6139 | /// Emits reduction finalizer function: |
| 6140 | /// \code |
| 6141 | /// void @.red_fini(void* %arg) { |
| 6142 | /// %0 = bitcast void* %arg to <type>* |
| 6143 | /// <destroy>(<type>* %0) |
| 6144 | /// ret void |
| 6145 | /// } |
| 6146 | /// \endcode |
| 6147 | static llvm::Value *emitReduceFiniFunction(CodeGenModule &CGM, |
| 6148 | SourceLocation Loc, |
| 6149 | ReductionCodeGen &RCG, unsigned N) { |
| 6150 | if (!RCG.needCleanups(N)) |
| 6151 | return nullptr; |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6152 | ASTContext &C = CGM.getContext(); |
| Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 6153 | FunctionArgList Args; |
| Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 6154 | ImplicitParamDecl Param(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, C.VoidPtrTy, |
| 6155 | ImplicitParamDecl::Other); |
| Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 6156 | Args.emplace_back(&Param); |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6157 | const auto &FnInfo = |
| Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 6158 | CGM.getTypes().arrangeBuiltinFunctionDeclaration(C.VoidTy, Args); |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6159 | llvm::FunctionType *FnTy = CGM.getTypes().GetFunctionType(FnInfo); |
| Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 6160 | std::string Name = CGM.getOpenMPRuntime().getName({"red_fini", ""}); |
| Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 6161 | auto *Fn = llvm::Function::Create(FnTy, llvm::GlobalValue::InternalLinkage, |
| Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 6162 | Name, &CGM.getModule()); |
| Rafael Espindola | 51ec5a9 | 2018-02-28 23:46:35 +0000 | [diff] [blame] | 6163 | CGM.SetInternalFunctionAttributes(GlobalDecl(), Fn, FnInfo); |
| Alexey Bataev | c0f879b | 2018-04-10 20:10:53 +0000 | [diff] [blame] | 6164 | Fn->setDoesNotRecurse(); |
| Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 6165 | CodeGenFunction CGF(CGM); |
| Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 6166 | CGF.StartFunction(GlobalDecl(), C.VoidTy, Fn, FnInfo, Args, Loc, Loc); |
| Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 6167 | Address PrivateAddr = CGF.EmitLoadOfPointer( |
| 6168 | CGF.GetAddrOfLocalVar(&Param), |
| 6169 | C.getPointerType(C.VoidPtrTy).castAs<PointerType>()); |
| 6170 | llvm::Value *Size = nullptr; |
| 6171 | // If the size of the reduction item is non-constant, load it from global |
| 6172 | // threadprivate variable. |
| 6173 | if (RCG.getSizes(N).second) { |
| 6174 | Address SizeAddr = CGM.getOpenMPRuntime().getAddrOfArtificialThreadPrivate( |
| 6175 | CGF, CGM.getContext().getSizeType(), |
| Alexey Bataev | 1c44e15 | 2018-03-06 18:59:43 +0000 | [diff] [blame] | 6176 | generateUniqueName(CGM, "reduction_size", RCG.getRefExpr(N))); |
| Alexey Bataev | a9b9cc0 | 2018-01-23 18:12:38 +0000 | [diff] [blame] | 6177 | Size = CGF.EmitLoadOfScalar(SizeAddr, /*Volatile=*/false, |
| 6178 | CGM.getContext().getSizeType(), Loc); |
| Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 6179 | } |
| 6180 | RCG.emitAggregateType(CGF, N, Size); |
| 6181 | // Emit the finalizer body: |
| 6182 | // <destroy>(<type>* %0) |
| 6183 | RCG.emitCleanups(CGF, N, PrivateAddr); |
| Adrian Prantl | ce7d359 | 2019-12-05 12:26:16 -0800 | [diff] [blame] | 6184 | CGF.FinishFunction(Loc); |
| Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 6185 | return Fn; |
| 6186 | } |
| 6187 | |
| 6188 | llvm::Value *CGOpenMPRuntime::emitTaskReductionInit( |
| 6189 | CodeGenFunction &CGF, SourceLocation Loc, ArrayRef<const Expr *> LHSExprs, |
| 6190 | ArrayRef<const Expr *> RHSExprs, const OMPTaskDataTy &Data) { |
| 6191 | if (!CGF.HaveInsertPoint() || Data.ReductionVars.empty()) |
| 6192 | return nullptr; |
| 6193 | |
| 6194 | // Build typedef struct: |
| 6195 | // kmp_task_red_input { |
| 6196 | // void *reduce_shar; // shared reduction item |
| 6197 | // size_t reduce_size; // size of data item |
| 6198 | // void *reduce_init; // data initialization routine |
| 6199 | // void *reduce_fini; // data finalization routine |
| 6200 | // void *reduce_comb; // data combiner routine |
| 6201 | // kmp_task_red_flags_t flags; // flags for additional info from compiler |
| 6202 | // } kmp_task_red_input_t; |
| 6203 | ASTContext &C = CGM.getContext(); |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6204 | RecordDecl *RD = C.buildImplicitRecord("kmp_task_red_input_t"); |
| Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 6205 | RD->startDefinition(); |
| 6206 | const FieldDecl *SharedFD = addFieldToRecordDecl(C, RD, C.VoidPtrTy); |
| 6207 | const FieldDecl *SizeFD = addFieldToRecordDecl(C, RD, C.getSizeType()); |
| 6208 | const FieldDecl *InitFD = addFieldToRecordDecl(C, RD, C.VoidPtrTy); |
| 6209 | const FieldDecl *FiniFD = addFieldToRecordDecl(C, RD, C.VoidPtrTy); |
| 6210 | const FieldDecl *CombFD = addFieldToRecordDecl(C, RD, C.VoidPtrTy); |
| 6211 | const FieldDecl *FlagsFD = addFieldToRecordDecl( |
| 6212 | C, RD, C.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/false)); |
| 6213 | RD->completeDefinition(); |
| 6214 | QualType RDType = C.getRecordType(RD); |
| 6215 | unsigned Size = Data.ReductionVars.size(); |
| 6216 | llvm::APInt ArraySize(/*numBits=*/64, Size); |
| 6217 | QualType ArrayRDType = C.getConstantArrayType( |
| Richard Smith | 772e266 | 2019-10-04 01:25:59 +0000 | [diff] [blame] | 6218 | RDType, ArraySize, nullptr, ArrayType::Normal, /*IndexTypeQuals=*/0); |
| Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 6219 | // kmp_task_red_input_t .rd_input.[Size]; |
| 6220 | Address TaskRedInput = CGF.CreateMemTemp(ArrayRDType, ".rd_input."); |
| 6221 | ReductionCodeGen RCG(Data.ReductionVars, Data.ReductionCopies, |
| 6222 | Data.ReductionOps); |
| 6223 | for (unsigned Cnt = 0; Cnt < Size; ++Cnt) { |
| 6224 | // kmp_task_red_input_t &ElemLVal = .rd_input.[Cnt]; |
| 6225 | llvm::Value *Idxs[] = {llvm::ConstantInt::get(CGM.SizeTy, /*V=*/0), |
| 6226 | llvm::ConstantInt::get(CGM.SizeTy, Cnt)}; |
| 6227 | llvm::Value *GEP = CGF.EmitCheckedInBoundsGEP( |
| 6228 | TaskRedInput.getPointer(), Idxs, |
| 6229 | /*SignedIndices=*/false, /*IsSubtraction=*/false, Loc, |
| 6230 | ".rd_input.gep."); |
| 6231 | LValue ElemLVal = CGF.MakeNaturalAlignAddrLValue(GEP, RDType); |
| 6232 | // ElemLVal.reduce_shar = &Shareds[Cnt]; |
| 6233 | LValue SharedLVal = CGF.EmitLValueForField(ElemLVal, SharedFD); |
| 6234 | RCG.emitSharedLValue(CGF, Cnt); |
| 6235 | llvm::Value *CastedShared = |
| Akira Hatanaka | f139ae3 | 2019-12-03 15:17:01 -0800 | [diff] [blame] | 6236 | CGF.EmitCastToVoidPtr(RCG.getSharedLValue(Cnt).getPointer(CGF)); |
| Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 6237 | CGF.EmitStoreOfScalar(CastedShared, SharedLVal); |
| 6238 | RCG.emitAggregateType(CGF, Cnt); |
| 6239 | llvm::Value *SizeValInChars; |
| 6240 | llvm::Value *SizeVal; |
| 6241 | std::tie(SizeValInChars, SizeVal) = RCG.getSizes(Cnt); |
| 6242 | // We use delayed creation/initialization for VLAs, array sections and |
| 6243 | // custom reduction initializations. It is required because runtime does not |
| 6244 | // provide the way to pass the sizes of VLAs/array sections to |
| 6245 | // initializer/combiner/finalizer functions and does not pass the pointer to |
| 6246 | // original reduction item to the initializer. Instead threadprivate global |
| 6247 | // variables are used to store these values and use them in the functions. |
| 6248 | bool DelayedCreation = !!SizeVal; |
| 6249 | SizeValInChars = CGF.Builder.CreateIntCast(SizeValInChars, CGM.SizeTy, |
| 6250 | /*isSigned=*/false); |
| 6251 | LValue SizeLVal = CGF.EmitLValueForField(ElemLVal, SizeFD); |
| 6252 | CGF.EmitStoreOfScalar(SizeValInChars, SizeLVal); |
| 6253 | // ElemLVal.reduce_init = init; |
| 6254 | LValue InitLVal = CGF.EmitLValueForField(ElemLVal, InitFD); |
| 6255 | llvm::Value *InitAddr = |
| 6256 | CGF.EmitCastToVoidPtr(emitReduceInitFunction(CGM, Loc, RCG, Cnt)); |
| 6257 | CGF.EmitStoreOfScalar(InitAddr, InitLVal); |
| 6258 | DelayedCreation = DelayedCreation || RCG.usesReductionInitializer(Cnt); |
| 6259 | // ElemLVal.reduce_fini = fini; |
| 6260 | LValue FiniLVal = CGF.EmitLValueForField(ElemLVal, FiniFD); |
| 6261 | llvm::Value *Fini = emitReduceFiniFunction(CGM, Loc, RCG, Cnt); |
| 6262 | llvm::Value *FiniAddr = Fini |
| 6263 | ? CGF.EmitCastToVoidPtr(Fini) |
| 6264 | : llvm::ConstantPointerNull::get(CGM.VoidPtrTy); |
| 6265 | CGF.EmitStoreOfScalar(FiniAddr, FiniLVal); |
| 6266 | // ElemLVal.reduce_comb = comb; |
| 6267 | LValue CombLVal = CGF.EmitLValueForField(ElemLVal, CombFD); |
| 6268 | llvm::Value *CombAddr = CGF.EmitCastToVoidPtr(emitReduceCombFunction( |
| 6269 | CGM, Loc, RCG, Cnt, Data.ReductionOps[Cnt], LHSExprs[Cnt], |
| 6270 | RHSExprs[Cnt], Data.ReductionCopies[Cnt])); |
| 6271 | CGF.EmitStoreOfScalar(CombAddr, CombLVal); |
| 6272 | // ElemLVal.flags = 0; |
| 6273 | LValue FlagsLVal = CGF.EmitLValueForField(ElemLVal, FlagsFD); |
| 6274 | if (DelayedCreation) { |
| 6275 | CGF.EmitStoreOfScalar( |
| Rui Ueyama | 49a3ad2 | 2019-07-16 04:46:31 +0000 | [diff] [blame] | 6276 | llvm::ConstantInt::get(CGM.Int32Ty, /*V=*/1, /*isSigned=*/true), |
| Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 6277 | FlagsLVal); |
| 6278 | } else |
| Akira Hatanaka | f139ae3 | 2019-12-03 15:17:01 -0800 | [diff] [blame] | 6279 | CGF.EmitNullInitialization(FlagsLVal.getAddress(CGF), |
| 6280 | FlagsLVal.getType()); |
| Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 6281 | } |
| 6282 | // Build call void *__kmpc_task_reduction_init(int gtid, int num_data, void |
| 6283 | // *data); |
| 6284 | llvm::Value *Args[] = { |
| 6285 | CGF.Builder.CreateIntCast(getThreadID(CGF, Loc), CGM.IntTy, |
| 6286 | /*isSigned=*/true), |
| 6287 | llvm::ConstantInt::get(CGM.IntTy, Size, /*isSigned=*/true), |
| 6288 | CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(TaskRedInput.getPointer(), |
| 6289 | CGM.VoidPtrTy)}; |
| 6290 | return CGF.EmitRuntimeCall( |
| 6291 | createRuntimeFunction(OMPRTL__kmpc_task_reduction_init), Args); |
| 6292 | } |
| 6293 | |
| 6294 | void CGOpenMPRuntime::emitTaskReductionFixups(CodeGenFunction &CGF, |
| 6295 | SourceLocation Loc, |
| 6296 | ReductionCodeGen &RCG, |
| 6297 | unsigned N) { |
| 6298 | auto Sizes = RCG.getSizes(N); |
| 6299 | // Emit threadprivate global variable if the type is non-constant |
| 6300 | // (Sizes.second = nullptr). |
| 6301 | if (Sizes.second) { |
| 6302 | llvm::Value *SizeVal = CGF.Builder.CreateIntCast(Sizes.second, CGM.SizeTy, |
| 6303 | /*isSigned=*/false); |
| 6304 | Address SizeAddr = getAddrOfArtificialThreadPrivate( |
| 6305 | CGF, CGM.getContext().getSizeType(), |
| Alexey Bataev | 1c44e15 | 2018-03-06 18:59:43 +0000 | [diff] [blame] | 6306 | generateUniqueName(CGM, "reduction_size", RCG.getRefExpr(N))); |
| Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 6307 | CGF.Builder.CreateStore(SizeVal, SizeAddr, /*IsVolatile=*/false); |
| 6308 | } |
| 6309 | // Store address of the original reduction item if custom initializer is used. |
| 6310 | if (RCG.usesReductionInitializer(N)) { |
| 6311 | Address SharedAddr = getAddrOfArtificialThreadPrivate( |
| 6312 | CGF, CGM.getContext().VoidPtrTy, |
| Alexey Bataev | 1c44e15 | 2018-03-06 18:59:43 +0000 | [diff] [blame] | 6313 | generateUniqueName(CGM, "reduction", RCG.getRefExpr(N))); |
| Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 6314 | CGF.Builder.CreateStore( |
| 6315 | CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
| Akira Hatanaka | f139ae3 | 2019-12-03 15:17:01 -0800 | [diff] [blame] | 6316 | RCG.getSharedLValue(N).getPointer(CGF), CGM.VoidPtrTy), |
| Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 6317 | SharedAddr, /*IsVolatile=*/false); |
| 6318 | } |
| 6319 | } |
| 6320 | |
| 6321 | Address CGOpenMPRuntime::getTaskReductionItem(CodeGenFunction &CGF, |
| 6322 | SourceLocation Loc, |
| 6323 | llvm::Value *ReductionsPtr, |
| 6324 | LValue SharedLVal) { |
| 6325 | // Build call void *__kmpc_task_reduction_get_th_data(int gtid, void *tg, void |
| 6326 | // *d); |
| Akira Hatanaka | f139ae3 | 2019-12-03 15:17:01 -0800 | [diff] [blame] | 6327 | llvm::Value *Args[] = {CGF.Builder.CreateIntCast(getThreadID(CGF, Loc), |
| 6328 | CGM.IntTy, |
| 6329 | /*isSigned=*/true), |
| 6330 | ReductionsPtr, |
| 6331 | CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
| 6332 | SharedLVal.getPointer(CGF), CGM.VoidPtrTy)}; |
| Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 6333 | return Address( |
| 6334 | CGF.EmitRuntimeCall( |
| 6335 | createRuntimeFunction(OMPRTL__kmpc_task_reduction_get_th_data), Args), |
| 6336 | SharedLVal.getAlignment()); |
| 6337 | } |
| 6338 | |
| Alexey Bataev | 8b8e202 | 2015-04-27 05:22:09 +0000 | [diff] [blame] | 6339 | void CGOpenMPRuntime::emitTaskwaitCall(CodeGenFunction &CGF, |
| 6340 | SourceLocation Loc) { |
| Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 6341 | if (!CGF.HaveInsertPoint()) |
| 6342 | return; |
| Roger Ferrer Ibanez | a82f35e | 2019-12-12 09:19:10 +0000 | [diff] [blame] | 6343 | |
| 6344 | llvm::OpenMPIRBuilder *OMPBuilder = CGF.CGM.getOpenMPIRBuilder(); |
| 6345 | if (OMPBuilder) { |
| 6346 | OMPBuilder->CreateTaskwait(CGF.Builder); |
| 6347 | } else { |
| 6348 | // Build call kmp_int32 __kmpc_omp_taskwait(ident_t *loc, kmp_int32 |
| 6349 | // global_tid); |
| 6350 | llvm::Value *Args[] = {emitUpdateLocation(CGF, Loc), getThreadID(CGF, Loc)}; |
| 6351 | // Ignore return result until untied tasks are supported. |
| 6352 | CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_omp_taskwait), Args); |
| 6353 | } |
| 6354 | |
| Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 6355 | if (auto *Region = dyn_cast_or_null<CGOpenMPRegionInfo>(CGF.CapturedStmtInfo)) |
| 6356 | Region->emitUntiedSwitch(CGF); |
| Alexey Bataev | 8b8e202 | 2015-04-27 05:22:09 +0000 | [diff] [blame] | 6357 | } |
| 6358 | |
| Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 6359 | void CGOpenMPRuntime::emitInlinedDirective(CodeGenFunction &CGF, |
| Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 6360 | OpenMPDirectiveKind InnerKind, |
| Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 6361 | const RegionCodeGenTy &CodeGen, |
| 6362 | bool HasCancel) { |
| Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 6363 | if (!CGF.HaveInsertPoint()) |
| 6364 | return; |
| Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 6365 | InlinedOpenMPRegionRAII Region(CGF, CodeGen, InnerKind, HasCancel); |
| Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 6366 | CGF.CapturedStmtInfo->EmitBody(CGF, /*S=*/nullptr); |
| Alexey Bataev | 8cbe0a6 | 2015-02-26 10:27:34 +0000 | [diff] [blame] | 6367 | } |
| 6368 | |
| Alexey Bataev | 7d5d33e | 2015-07-06 05:50:32 +0000 | [diff] [blame] | 6369 | namespace { |
| 6370 | enum RTCancelKind { |
| 6371 | CancelNoreq = 0, |
| 6372 | CancelParallel = 1, |
| 6373 | CancelLoop = 2, |
| 6374 | CancelSections = 3, |
| 6375 | CancelTaskgroup = 4 |
| 6376 | }; |
| Eugene Zelenko | 0a4f3f4 | 2016-02-10 19:11:58 +0000 | [diff] [blame] | 6377 | } // anonymous namespace |
| Alexey Bataev | 7d5d33e | 2015-07-06 05:50:32 +0000 | [diff] [blame] | 6378 | |
| 6379 | static RTCancelKind getCancellationKind(OpenMPDirectiveKind CancelRegion) { |
| 6380 | RTCancelKind CancelKind = CancelNoreq; |
| Alexey Bataev | 0f34da1 | 2015-07-02 04:17:07 +0000 | [diff] [blame] | 6381 | if (CancelRegion == OMPD_parallel) |
| 6382 | CancelKind = CancelParallel; |
| 6383 | else if (CancelRegion == OMPD_for) |
| 6384 | CancelKind = CancelLoop; |
| 6385 | else if (CancelRegion == OMPD_sections) |
| 6386 | CancelKind = CancelSections; |
| 6387 | else { |
| 6388 | assert(CancelRegion == OMPD_taskgroup); |
| 6389 | CancelKind = CancelTaskgroup; |
| 6390 | } |
| Alexey Bataev | 7d5d33e | 2015-07-06 05:50:32 +0000 | [diff] [blame] | 6391 | return CancelKind; |
| 6392 | } |
| 6393 | |
| 6394 | void CGOpenMPRuntime::emitCancellationPointCall( |
| 6395 | CodeGenFunction &CGF, SourceLocation Loc, |
| 6396 | OpenMPDirectiveKind CancelRegion) { |
| Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 6397 | if (!CGF.HaveInsertPoint()) |
| 6398 | return; |
| Alexey Bataev | 7d5d33e | 2015-07-06 05:50:32 +0000 | [diff] [blame] | 6399 | // Build call kmp_int32 __kmpc_cancellationpoint(ident_t *loc, kmp_int32 |
| 6400 | // global_tid, kmp_int32 cncl_kind); |
| Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 6401 | if (auto *OMPRegionInfo = |
| 6402 | dyn_cast_or_null<CGOpenMPRegionInfo>(CGF.CapturedStmtInfo)) { |
| Jonas Hahnfeld | b07931f | 2017-02-17 18:32:58 +0000 | [diff] [blame] | 6403 | // For 'cancellation point taskgroup', the task region info may not have a |
| 6404 | // cancel. This may instead happen in another adjacent task. |
| 6405 | if (CancelRegion == OMPD_taskgroup || OMPRegionInfo->hasCancel()) { |
| Alexey Bataev | 7d5d33e | 2015-07-06 05:50:32 +0000 | [diff] [blame] | 6406 | llvm::Value *Args[] = { |
| 6407 | emitUpdateLocation(CGF, Loc), getThreadID(CGF, Loc), |
| 6408 | CGF.Builder.getInt32(getCancellationKind(CancelRegion))}; |
| Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 6409 | // Ignore return result until untied tasks are supported. |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6410 | llvm::Value *Result = CGF.EmitRuntimeCall( |
| Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 6411 | createRuntimeFunction(OMPRTL__kmpc_cancellationpoint), Args); |
| 6412 | // if (__kmpc_cancellationpoint()) { |
| Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 6413 | // exit from construct; |
| 6414 | // } |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6415 | llvm::BasicBlock *ExitBB = CGF.createBasicBlock(".cancel.exit"); |
| 6416 | llvm::BasicBlock *ContBB = CGF.createBasicBlock(".cancel.continue"); |
| 6417 | llvm::Value *Cmp = CGF.Builder.CreateIsNotNull(Result); |
| Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 6418 | CGF.Builder.CreateCondBr(Cmp, ExitBB, ContBB); |
| 6419 | CGF.EmitBlock(ExitBB); |
| Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 6420 | // exit from construct; |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6421 | CodeGenFunction::JumpDest CancelDest = |
| Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 6422 | CGF.getOMPCancelDestination(OMPRegionInfo->getDirectiveKind()); |
| Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 6423 | CGF.EmitBranchThroughCleanup(CancelDest); |
| 6424 | CGF.EmitBlock(ContBB, /*IsFinished=*/true); |
| 6425 | } |
| Alexey Bataev | 0f34da1 | 2015-07-02 04:17:07 +0000 | [diff] [blame] | 6426 | } |
| Alexey Bataev | 0f34da1 | 2015-07-02 04:17:07 +0000 | [diff] [blame] | 6427 | } |
| 6428 | |
| Alexey Bataev | 7d5d33e | 2015-07-06 05:50:32 +0000 | [diff] [blame] | 6429 | void CGOpenMPRuntime::emitCancelCall(CodeGenFunction &CGF, SourceLocation Loc, |
| Alexey Bataev | 87933c7 | 2015-09-18 08:07:34 +0000 | [diff] [blame] | 6430 | const Expr *IfCond, |
| Alexey Bataev | 7d5d33e | 2015-07-06 05:50:32 +0000 | [diff] [blame] | 6431 | OpenMPDirectiveKind CancelRegion) { |
| Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 6432 | if (!CGF.HaveInsertPoint()) |
| 6433 | return; |
| Alexey Bataev | 7d5d33e | 2015-07-06 05:50:32 +0000 | [diff] [blame] | 6434 | // Build call kmp_int32 __kmpc_cancel(ident_t *loc, kmp_int32 global_tid, |
| 6435 | // kmp_int32 cncl_kind); |
| 6436 | if (auto *OMPRegionInfo = |
| 6437 | dyn_cast_or_null<CGOpenMPRegionInfo>(CGF.CapturedStmtInfo)) { |
| Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 6438 | auto &&ThenGen = [Loc, CancelRegion, OMPRegionInfo](CodeGenFunction &CGF, |
| 6439 | PrePostActionTy &) { |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6440 | CGOpenMPRuntime &RT = CGF.CGM.getOpenMPRuntime(); |
| Alexey Bataev | 87933c7 | 2015-09-18 08:07:34 +0000 | [diff] [blame] | 6441 | llvm::Value *Args[] = { |
| Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 6442 | RT.emitUpdateLocation(CGF, Loc), RT.getThreadID(CGF, Loc), |
| Alexey Bataev | 87933c7 | 2015-09-18 08:07:34 +0000 | [diff] [blame] | 6443 | CGF.Builder.getInt32(getCancellationKind(CancelRegion))}; |
| 6444 | // Ignore return result until untied tasks are supported. |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6445 | llvm::Value *Result = CGF.EmitRuntimeCall( |
| Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 6446 | RT.createRuntimeFunction(OMPRTL__kmpc_cancel), Args); |
| Alexey Bataev | 87933c7 | 2015-09-18 08:07:34 +0000 | [diff] [blame] | 6447 | // if (__kmpc_cancel()) { |
| Alexey Bataev | 87933c7 | 2015-09-18 08:07:34 +0000 | [diff] [blame] | 6448 | // exit from construct; |
| 6449 | // } |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6450 | llvm::BasicBlock *ExitBB = CGF.createBasicBlock(".cancel.exit"); |
| 6451 | llvm::BasicBlock *ContBB = CGF.createBasicBlock(".cancel.continue"); |
| 6452 | llvm::Value *Cmp = CGF.Builder.CreateIsNotNull(Result); |
| Alexey Bataev | 87933c7 | 2015-09-18 08:07:34 +0000 | [diff] [blame] | 6453 | CGF.Builder.CreateCondBr(Cmp, ExitBB, ContBB); |
| 6454 | CGF.EmitBlock(ExitBB); |
| Alexey Bataev | 87933c7 | 2015-09-18 08:07:34 +0000 | [diff] [blame] | 6455 | // exit from construct; |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6456 | CodeGenFunction::JumpDest CancelDest = |
| Alexey Bataev | 87933c7 | 2015-09-18 08:07:34 +0000 | [diff] [blame] | 6457 | CGF.getOMPCancelDestination(OMPRegionInfo->getDirectiveKind()); |
| 6458 | CGF.EmitBranchThroughCleanup(CancelDest); |
| 6459 | CGF.EmitBlock(ContBB, /*IsFinished=*/true); |
| 6460 | }; |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6461 | if (IfCond) { |
| Alexey Bataev | 1d943ae | 2019-11-19 12:12:23 -0500 | [diff] [blame] | 6462 | emitIfClause(CGF, IfCond, ThenGen, |
| 6463 | [](CodeGenFunction &, PrePostActionTy &) {}); |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6464 | } else { |
| Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 6465 | RegionCodeGenTy ThenRCG(ThenGen); |
| 6466 | ThenRCG(CGF); |
| 6467 | } |
| Alexey Bataev | 7d5d33e | 2015-07-06 05:50:32 +0000 | [diff] [blame] | 6468 | } |
| 6469 | } |
| Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 6470 | |
| Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 6471 | void CGOpenMPRuntime::emitTargetOutlinedFunction( |
| 6472 | const OMPExecutableDirective &D, StringRef ParentName, |
| 6473 | llvm::Function *&OutlinedFn, llvm::Constant *&OutlinedFnID, |
| Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 6474 | bool IsOffloadEntry, const RegionCodeGenTy &CodeGen) { |
| Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 6475 | assert(!ParentName.empty() && "Invalid target region parent name!"); |
| Gheorghe-Teodor Bercea | 66cdbb47 | 2019-05-21 19:42:01 +0000 | [diff] [blame] | 6476 | HasEmittedTargetRegion = true; |
| Arpith Chacko Jacob | 5c309e4 | 2016-03-22 01:48:56 +0000 | [diff] [blame] | 6477 | emitTargetOutlinedFunctionHelper(D, ParentName, OutlinedFn, OutlinedFnID, |
| 6478 | IsOffloadEntry, CodeGen); |
| 6479 | } |
| 6480 | |
| 6481 | void CGOpenMPRuntime::emitTargetOutlinedFunctionHelper( |
| 6482 | const OMPExecutableDirective &D, StringRef ParentName, |
| 6483 | llvm::Function *&OutlinedFn, llvm::Constant *&OutlinedFnID, |
| 6484 | bool IsOffloadEntry, const RegionCodeGenTy &CodeGen) { |
| Samuel Antao | 2de62b0 | 2016-02-13 23:35:10 +0000 | [diff] [blame] | 6485 | // Create a unique name for the entry function using the source location |
| 6486 | // information of the current target region. The name will be something like: |
| Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 6487 | // |
| Samuel Antao | 2de62b0 | 2016-02-13 23:35:10 +0000 | [diff] [blame] | 6488 | // __omp_offloading_DD_FFFF_PP_lBB |
| Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 6489 | // |
| 6490 | // where DD_FFFF is an ID unique to the file (device and file IDs), PP is the |
| Samuel Antao | 2de62b0 | 2016-02-13 23:35:10 +0000 | [diff] [blame] | 6491 | // mangled name of the function that encloses the target region and BB is the |
| 6492 | // line number of the target region. |
| Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 6493 | |
| 6494 | unsigned DeviceID; |
| 6495 | unsigned FileID; |
| 6496 | unsigned Line; |
| Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 6497 | getTargetEntryUniqueInfo(CGM.getContext(), D.getBeginLoc(), DeviceID, FileID, |
| Samuel Antao | 2de62b0 | 2016-02-13 23:35:10 +0000 | [diff] [blame] | 6498 | Line); |
| Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 6499 | SmallString<64> EntryFnName; |
| 6500 | { |
| 6501 | llvm::raw_svector_ostream OS(EntryFnName); |
| Samuel Antao | 2de62b0 | 2016-02-13 23:35:10 +0000 | [diff] [blame] | 6502 | OS << "__omp_offloading" << llvm::format("_%x", DeviceID) |
| 6503 | << llvm::format("_%x_", FileID) << ParentName << "_l" << Line; |
| Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 6504 | } |
| 6505 | |
| Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 6506 | const CapturedStmt &CS = *D.getCapturedStmt(OMPD_target); |
| Arpith Chacko Jacob | 5c309e4 | 2016-03-22 01:48:56 +0000 | [diff] [blame] | 6507 | |
| Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 6508 | CodeGenFunction CGF(CGM, true); |
| Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 6509 | CGOpenMPTargetRegionInfo CGInfo(CS, CodeGen, EntryFnName); |
| Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 6510 | CodeGenFunction::CGCapturedStmtRAII CapInfoRAII(CGF, &CGInfo); |
| Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 6511 | |
| Alexey Bataev | c33ba8c | 2020-01-17 14:05:40 -0500 | [diff] [blame] | 6512 | OutlinedFn = CGF.GenerateOpenMPCapturedStmtFunction(CS, D.getBeginLoc()); |
| Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 6513 | |
| 6514 | // If this target outline function is not an offload entry, we don't need to |
| 6515 | // register it. |
| 6516 | if (!IsOffloadEntry) |
| 6517 | return; |
| 6518 | |
| 6519 | // The target region ID is used by the runtime library to identify the current |
| 6520 | // target region, so it only has to be unique and not necessarily point to |
| 6521 | // anything. It could be the pointer to the outlined function that implements |
| 6522 | // the target region, but we aren't using that so that the compiler doesn't |
| 6523 | // need to keep that, and could therefore inline the host function if proven |
| 6524 | // worthwhile during optimization. In the other hand, if emitting code for the |
| 6525 | // device, the ID has to be the function address so that it can retrieved from |
| 6526 | // the offloading entry and launched by the runtime library. We also mark the |
| 6527 | // outlined function to have external linkage in case we are emitting code for |
| 6528 | // the device, because these functions will be entry points to the device. |
| 6529 | |
| 6530 | if (CGM.getLangOpts().OpenMPIsDevice) { |
| 6531 | OutlinedFnID = llvm::ConstantExpr::getBitCast(OutlinedFn, CGM.Int8PtrTy); |
| Alexey Bataev | 9a70017 | 2018-05-08 14:16:57 +0000 | [diff] [blame] | 6532 | OutlinedFn->setLinkage(llvm::GlobalValue::WeakAnyLinkage); |
| Rafael Espindola | cbca487 | 2018-01-11 22:15:12 +0000 | [diff] [blame] | 6533 | OutlinedFn->setDSOLocal(false); |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6534 | } else { |
| Alexey Bataev | c15ea70 | 2018-05-09 18:02:37 +0000 | [diff] [blame] | 6535 | std::string Name = getName({EntryFnName, "region_id"}); |
| Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 6536 | OutlinedFnID = new llvm::GlobalVariable( |
| 6537 | CGM.getModule(), CGM.Int8Ty, /*isConstant=*/true, |
| Alexey Bataev | 9a70017 | 2018-05-08 14:16:57 +0000 | [diff] [blame] | 6538 | llvm::GlobalValue::WeakAnyLinkage, |
| Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 6539 | llvm::Constant::getNullValue(CGM.Int8Ty), Name); |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6540 | } |
| Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 6541 | |
| 6542 | // Register the information for the entry associated with this target region. |
| 6543 | OffloadEntriesInfoManager.registerTargetRegionEntryInfo( |
| Samuel Antao | f83efdb | 2017-01-05 16:02:49 +0000 | [diff] [blame] | 6544 | DeviceID, FileID, ParentName, Line, OutlinedFn, OutlinedFnID, |
| Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 6545 | OffloadEntriesInfoManagerTy::OMPTargetRegionEntryTargetRegion); |
| Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 6546 | } |
| 6547 | |
| Alexey Bataev | 5c42736 | 2019-04-10 19:11:33 +0000 | [diff] [blame] | 6548 | /// Checks if the expression is constant or does not have non-trivial function |
| 6549 | /// calls. |
| 6550 | static bool isTrivial(ASTContext &Ctx, const Expr * E) { |
| 6551 | // We can skip constant expressions. |
| 6552 | // We can skip expressions with trivial calls or simple expressions. |
| 6553 | return (E->isEvaluatable(Ctx, Expr::SE_AllowUndefinedBehavior) || |
| 6554 | !E->hasNonTrivialCall(Ctx)) && |
| 6555 | !E->HasSideEffects(Ctx, /*IncludePossibleEffects=*/true); |
| 6556 | } |
| Carlo Bertolli | 6eee906 | 2016-04-29 01:37:30 +0000 | [diff] [blame] | 6557 | |
| Alexey Bataev | 5c42736 | 2019-04-10 19:11:33 +0000 | [diff] [blame] | 6558 | const Stmt *CGOpenMPRuntime::getSingleCompoundChild(ASTContext &Ctx, |
| 6559 | const Stmt *Body) { |
| 6560 | const Stmt *Child = Body->IgnoreContainers(); |
| 6561 | while (const auto *C = dyn_cast_or_null<CompoundStmt>(Child)) { |
| 6562 | Child = nullptr; |
| 6563 | for (const Stmt *S : C->body()) { |
| 6564 | if (const auto *E = dyn_cast<Expr>(S)) { |
| 6565 | if (isTrivial(Ctx, E)) |
| 6566 | continue; |
| 6567 | } |
| 6568 | // Some of the statements can be ignored. |
| 6569 | if (isa<AsmStmt>(S) || isa<NullStmt>(S) || isa<OMPFlushDirective>(S) || |
| 6570 | isa<OMPBarrierDirective>(S) || isa<OMPTaskyieldDirective>(S)) |
| 6571 | continue; |
| 6572 | // Analyze declarations. |
| 6573 | if (const auto *DS = dyn_cast<DeclStmt>(S)) { |
| 6574 | if (llvm::all_of(DS->decls(), [&Ctx](const Decl *D) { |
| 6575 | if (isa<EmptyDecl>(D) || isa<DeclContext>(D) || |
| 6576 | isa<TypeDecl>(D) || isa<PragmaCommentDecl>(D) || |
| 6577 | isa<PragmaDetectMismatchDecl>(D) || isa<UsingDecl>(D) || |
| 6578 | isa<UsingDirectiveDecl>(D) || |
| 6579 | isa<OMPDeclareReductionDecl>(D) || |
| 6580 | isa<OMPThreadPrivateDecl>(D) || isa<OMPAllocateDecl>(D)) |
| 6581 | return true; |
| 6582 | const auto *VD = dyn_cast<VarDecl>(D); |
| 6583 | if (!VD) |
| 6584 | return false; |
| 6585 | return VD->isConstexpr() || |
| 6586 | ((VD->getType().isTrivialType(Ctx) || |
| 6587 | VD->getType()->isReferenceType()) && |
| 6588 | (!VD->hasInit() || isTrivial(Ctx, VD->getInit()))); |
| 6589 | })) |
| 6590 | continue; |
| 6591 | } |
| 6592 | // Found multiple children - cannot get the one child only. |
| 6593 | if (Child) |
| 6594 | return nullptr; |
| 6595 | Child = S; |
| 6596 | } |
| 6597 | if (Child) |
| 6598 | Child = Child->IgnoreContainers(); |
| 6599 | } |
| 6600 | return Child; |
| Carlo Bertolli | 6eee906 | 2016-04-29 01:37:30 +0000 | [diff] [blame] | 6601 | } |
| 6602 | |
| Arpith Chacko Jacob | 33c849a | 2017-01-25 00:57:16 +0000 | [diff] [blame] | 6603 | /// Emit the number of teams for a target directive. Inspect the num_teams |
| 6604 | /// clause associated with a teams construct combined or closely nested |
| 6605 | /// with the target directive. |
| 6606 | /// |
| 6607 | /// Emit a team of size one for directives such as 'target parallel' that |
| 6608 | /// have no associated teams construct. |
| 6609 | /// |
| 6610 | /// Otherwise, return nullptr. |
| Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 6611 | static llvm::Value * |
| Alexey Bataev | 5c42736 | 2019-04-10 19:11:33 +0000 | [diff] [blame] | 6612 | emitNumTeamsForTargetDirective(CodeGenFunction &CGF, |
| Arpith Chacko Jacob | 33c849a | 2017-01-25 00:57:16 +0000 | [diff] [blame] | 6613 | const OMPExecutableDirective &D) { |
| Alexey Bataev | 5c42736 | 2019-04-10 19:11:33 +0000 | [diff] [blame] | 6614 | assert(!CGF.getLangOpts().OpenMPIsDevice && |
| 6615 | "Clauses associated with the teams directive expected to be emitted " |
| 6616 | "only for the host!"); |
| 6617 | OpenMPDirectiveKind DirectiveKind = D.getDirectiveKind(); |
| 6618 | assert(isOpenMPTargetExecutionDirective(DirectiveKind) && |
| 6619 | "Expected target-based executable directive."); |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6620 | CGBuilderTy &Bld = CGF.Builder; |
| Alexey Bataev | 5c42736 | 2019-04-10 19:11:33 +0000 | [diff] [blame] | 6621 | switch (DirectiveKind) { |
| 6622 | case OMPD_target: { |
| 6623 | const auto *CS = D.getInnermostCapturedStmt(); |
| 6624 | const auto *Body = |
| 6625 | CS->getCapturedStmt()->IgnoreContainers(/*IgnoreCaptured=*/true); |
| 6626 | const Stmt *ChildStmt = |
| 6627 | CGOpenMPRuntime::getSingleCompoundChild(CGF.getContext(), Body); |
| 6628 | if (const auto *NestedDir = |
| 6629 | dyn_cast_or_null<OMPExecutableDirective>(ChildStmt)) { |
| 6630 | if (isOpenMPTeamsDirective(NestedDir->getDirectiveKind())) { |
| 6631 | if (NestedDir->hasClausesOfKind<OMPNumTeamsClause>()) { |
| 6632 | CGOpenMPInnerExprInfo CGInfo(CGF, *CS); |
| 6633 | CodeGenFunction::CGCapturedStmtRAII CapInfoRAII(CGF, &CGInfo); |
| 6634 | const Expr *NumTeams = |
| 6635 | NestedDir->getSingleClause<OMPNumTeamsClause>()->getNumTeams(); |
| 6636 | llvm::Value *NumTeamsVal = |
| 6637 | CGF.EmitScalarExpr(NumTeams, |
| 6638 | /*IgnoreResultAssign*/ true); |
| 6639 | return Bld.CreateIntCast(NumTeamsVal, CGF.Int32Ty, |
| Rui Ueyama | 49a3ad2 | 2019-07-16 04:46:31 +0000 | [diff] [blame] | 6640 | /*isSigned=*/true); |
| Alexey Bataev | 5c42736 | 2019-04-10 19:11:33 +0000 | [diff] [blame] | 6641 | } |
| 6642 | return Bld.getInt32(0); |
| Alexey Bataev | 50a1c78 | 2017-12-01 21:31:08 +0000 | [diff] [blame] | 6643 | } |
| Alexey Bataev | 5c42736 | 2019-04-10 19:11:33 +0000 | [diff] [blame] | 6644 | if (isOpenMPParallelDirective(NestedDir->getDirectiveKind()) || |
| 6645 | isOpenMPSimdDirective(NestedDir->getDirectiveKind())) |
| 6646 | return Bld.getInt32(1); |
| Alexey Bataev | 50a1c78 | 2017-12-01 21:31:08 +0000 | [diff] [blame] | 6647 | return Bld.getInt32(0); |
| 6648 | } |
| Alexey Bataev | 5c42736 | 2019-04-10 19:11:33 +0000 | [diff] [blame] | 6649 | return nullptr; |
| Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 6650 | } |
| Alexey Bataev | 5c42736 | 2019-04-10 19:11:33 +0000 | [diff] [blame] | 6651 | case OMPD_target_teams: |
| 6652 | case OMPD_target_teams_distribute: |
| 6653 | case OMPD_target_teams_distribute_simd: |
| 6654 | case OMPD_target_teams_distribute_parallel_for: |
| 6655 | case OMPD_target_teams_distribute_parallel_for_simd: { |
| 6656 | if (D.hasClausesOfKind<OMPNumTeamsClause>()) { |
| 6657 | CodeGenFunction::RunCleanupsScope NumTeamsScope(CGF); |
| 6658 | const Expr *NumTeams = |
| 6659 | D.getSingleClause<OMPNumTeamsClause>()->getNumTeams(); |
| 6660 | llvm::Value *NumTeamsVal = |
| 6661 | CGF.EmitScalarExpr(NumTeams, |
| 6662 | /*IgnoreResultAssign*/ true); |
| 6663 | return Bld.CreateIntCast(NumTeamsVal, CGF.Int32Ty, |
| Rui Ueyama | 49a3ad2 | 2019-07-16 04:46:31 +0000 | [diff] [blame] | 6664 | /*isSigned=*/true); |
| Alexey Bataev | 5c42736 | 2019-04-10 19:11:33 +0000 | [diff] [blame] | 6665 | } |
| 6666 | return Bld.getInt32(0); |
| 6667 | } |
| 6668 | case OMPD_target_parallel: |
| 6669 | case OMPD_target_parallel_for: |
| 6670 | case OMPD_target_parallel_for_simd: |
| 6671 | case OMPD_target_simd: |
| 6672 | return Bld.getInt32(1); |
| 6673 | case OMPD_parallel: |
| 6674 | case OMPD_for: |
| 6675 | case OMPD_parallel_for: |
| cchen | 47d6094 | 2019-12-05 13:43:48 -0500 | [diff] [blame] | 6676 | case OMPD_parallel_master: |
| Alexey Bataev | 5c42736 | 2019-04-10 19:11:33 +0000 | [diff] [blame] | 6677 | case OMPD_parallel_sections: |
| 6678 | case OMPD_for_simd: |
| 6679 | case OMPD_parallel_for_simd: |
| 6680 | case OMPD_cancel: |
| 6681 | case OMPD_cancellation_point: |
| 6682 | case OMPD_ordered: |
| 6683 | case OMPD_threadprivate: |
| 6684 | case OMPD_allocate: |
| 6685 | case OMPD_task: |
| 6686 | case OMPD_simd: |
| 6687 | case OMPD_sections: |
| 6688 | case OMPD_section: |
| 6689 | case OMPD_single: |
| 6690 | case OMPD_master: |
| 6691 | case OMPD_critical: |
| 6692 | case OMPD_taskyield: |
| 6693 | case OMPD_barrier: |
| 6694 | case OMPD_taskwait: |
| 6695 | case OMPD_taskgroup: |
| 6696 | case OMPD_atomic: |
| 6697 | case OMPD_flush: |
| 6698 | case OMPD_teams: |
| 6699 | case OMPD_target_data: |
| 6700 | case OMPD_target_exit_data: |
| 6701 | case OMPD_target_enter_data: |
| 6702 | case OMPD_distribute: |
| 6703 | case OMPD_distribute_simd: |
| 6704 | case OMPD_distribute_parallel_for: |
| 6705 | case OMPD_distribute_parallel_for_simd: |
| 6706 | case OMPD_teams_distribute: |
| 6707 | case OMPD_teams_distribute_simd: |
| 6708 | case OMPD_teams_distribute_parallel_for: |
| 6709 | case OMPD_teams_distribute_parallel_for_simd: |
| 6710 | case OMPD_target_update: |
| 6711 | case OMPD_declare_simd: |
| Alexey Bataev | d158cf6 | 2019-09-13 20:18:17 +0000 | [diff] [blame] | 6712 | case OMPD_declare_variant: |
| Alexey Bataev | 5c42736 | 2019-04-10 19:11:33 +0000 | [diff] [blame] | 6713 | case OMPD_declare_target: |
| 6714 | case OMPD_end_declare_target: |
| 6715 | case OMPD_declare_reduction: |
| 6716 | case OMPD_declare_mapper: |
| 6717 | case OMPD_taskloop: |
| 6718 | case OMPD_taskloop_simd: |
| Alexey Bataev | 60e51c4 | 2019-10-10 20:13:02 +0000 | [diff] [blame] | 6719 | case OMPD_master_taskloop: |
| Alexey Bataev | b8552ab | 2019-10-18 16:47:35 +0000 | [diff] [blame] | 6720 | case OMPD_master_taskloop_simd: |
| Alexey Bataev | 5bbcead | 2019-10-14 17:17:41 +0000 | [diff] [blame] | 6721 | case OMPD_parallel_master_taskloop: |
| Alexey Bataev | 14a388f | 2019-10-25 10:27:13 -0400 | [diff] [blame] | 6722 | case OMPD_parallel_master_taskloop_simd: |
| Alexey Bataev | 5c42736 | 2019-04-10 19:11:33 +0000 | [diff] [blame] | 6723 | case OMPD_requires: |
| 6724 | case OMPD_unknown: |
| 6725 | break; |
| 6726 | } |
| 6727 | llvm_unreachable("Unexpected directive kind."); |
| 6728 | } |
| Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 6729 | |
| Alexey Bataev | 5c42736 | 2019-04-10 19:11:33 +0000 | [diff] [blame] | 6730 | static llvm::Value *getNumThreads(CodeGenFunction &CGF, const CapturedStmt *CS, |
| 6731 | llvm::Value *DefaultThreadLimitVal) { |
| 6732 | const Stmt *Child = CGOpenMPRuntime::getSingleCompoundChild( |
| 6733 | CGF.getContext(), CS->getCapturedStmt()); |
| 6734 | if (const auto *Dir = dyn_cast_or_null<OMPExecutableDirective>(Child)) { |
| 6735 | if (isOpenMPParallelDirective(Dir->getDirectiveKind())) { |
| Alexey Bataev | dc9e7dc | 2019-04-17 16:53:08 +0000 | [diff] [blame] | 6736 | llvm::Value *NumThreads = nullptr; |
| 6737 | llvm::Value *CondVal = nullptr; |
| 6738 | // Handle if clause. If if clause present, the number of threads is |
| 6739 | // calculated as <cond> ? (<numthreads> ? <numthreads> : 0 ) : 1. |
| 6740 | if (Dir->hasClausesOfKind<OMPIfClause>()) { |
| 6741 | CGOpenMPInnerExprInfo CGInfo(CGF, *CS); |
| 6742 | CodeGenFunction::CGCapturedStmtRAII CapInfoRAII(CGF, &CGInfo); |
| 6743 | const OMPIfClause *IfClause = nullptr; |
| 6744 | for (const auto *C : Dir->getClausesOfKind<OMPIfClause>()) { |
| 6745 | if (C->getNameModifier() == OMPD_unknown || |
| 6746 | C->getNameModifier() == OMPD_parallel) { |
| 6747 | IfClause = C; |
| 6748 | break; |
| 6749 | } |
| 6750 | } |
| 6751 | if (IfClause) { |
| 6752 | const Expr *Cond = IfClause->getCondition(); |
| 6753 | bool Result; |
| 6754 | if (Cond->EvaluateAsBooleanCondition(Result, CGF.getContext())) { |
| 6755 | if (!Result) |
| 6756 | return CGF.Builder.getInt32(1); |
| 6757 | } else { |
| 6758 | CodeGenFunction::LexicalScope Scope(CGF, Cond->getSourceRange()); |
| 6759 | if (const auto *PreInit = |
| 6760 | cast_or_null<DeclStmt>(IfClause->getPreInitStmt())) { |
| 6761 | for (const auto *I : PreInit->decls()) { |
| 6762 | if (!I->hasAttr<OMPCaptureNoInitAttr>()) { |
| 6763 | CGF.EmitVarDecl(cast<VarDecl>(*I)); |
| 6764 | } else { |
| 6765 | CodeGenFunction::AutoVarEmission Emission = |
| 6766 | CGF.EmitAutoVarAlloca(cast<VarDecl>(*I)); |
| 6767 | CGF.EmitAutoVarCleanups(Emission); |
| 6768 | } |
| 6769 | } |
| 6770 | } |
| 6771 | CondVal = CGF.EvaluateExprAsBool(Cond); |
| 6772 | } |
| 6773 | } |
| 6774 | } |
| 6775 | // Check the value of num_threads clause iff if clause was not specified |
| 6776 | // or is not evaluated to false. |
| Alexey Bataev | 5c42736 | 2019-04-10 19:11:33 +0000 | [diff] [blame] | 6777 | if (Dir->hasClausesOfKind<OMPNumThreadsClause>()) { |
| 6778 | CGOpenMPInnerExprInfo CGInfo(CGF, *CS); |
| 6779 | CodeGenFunction::CGCapturedStmtRAII CapInfoRAII(CGF, &CGInfo); |
| 6780 | const auto *NumThreadsClause = |
| 6781 | Dir->getSingleClause<OMPNumThreadsClause>(); |
| 6782 | CodeGenFunction::LexicalScope Scope( |
| 6783 | CGF, NumThreadsClause->getNumThreads()->getSourceRange()); |
| 6784 | if (const auto *PreInit = |
| 6785 | cast_or_null<DeclStmt>(NumThreadsClause->getPreInitStmt())) { |
| 6786 | for (const auto *I : PreInit->decls()) { |
| 6787 | if (!I->hasAttr<OMPCaptureNoInitAttr>()) { |
| 6788 | CGF.EmitVarDecl(cast<VarDecl>(*I)); |
| 6789 | } else { |
| 6790 | CodeGenFunction::AutoVarEmission Emission = |
| 6791 | CGF.EmitAutoVarAlloca(cast<VarDecl>(*I)); |
| 6792 | CGF.EmitAutoVarCleanups(Emission); |
| 6793 | } |
| 6794 | } |
| 6795 | } |
| Alexey Bataev | dc9e7dc | 2019-04-17 16:53:08 +0000 | [diff] [blame] | 6796 | NumThreads = CGF.EmitScalarExpr(NumThreadsClause->getNumThreads()); |
| Alexey Bataev | 5c42736 | 2019-04-10 19:11:33 +0000 | [diff] [blame] | 6797 | NumThreads = CGF.Builder.CreateIntCast(NumThreads, CGF.Int32Ty, |
| Rui Ueyama | 49a3ad2 | 2019-07-16 04:46:31 +0000 | [diff] [blame] | 6798 | /*isSigned=*/false); |
| Alexey Bataev | dc9e7dc | 2019-04-17 16:53:08 +0000 | [diff] [blame] | 6799 | if (DefaultThreadLimitVal) |
| 6800 | NumThreads = CGF.Builder.CreateSelect( |
| 6801 | CGF.Builder.CreateICmpULT(DefaultThreadLimitVal, NumThreads), |
| 6802 | DefaultThreadLimitVal, NumThreads); |
| 6803 | } else { |
| 6804 | NumThreads = DefaultThreadLimitVal ? DefaultThreadLimitVal |
| 6805 | : CGF.Builder.getInt32(0); |
| Alexey Bataev | 5c42736 | 2019-04-10 19:11:33 +0000 | [diff] [blame] | 6806 | } |
| Alexey Bataev | dc9e7dc | 2019-04-17 16:53:08 +0000 | [diff] [blame] | 6807 | // Process condition of the if clause. |
| 6808 | if (CondVal) { |
| 6809 | NumThreads = CGF.Builder.CreateSelect(CondVal, NumThreads, |
| 6810 | CGF.Builder.getInt32(1)); |
| 6811 | } |
| 6812 | return NumThreads; |
| Alexey Bataev | 5c42736 | 2019-04-10 19:11:33 +0000 | [diff] [blame] | 6813 | } |
| 6814 | if (isOpenMPSimdDirective(Dir->getDirectiveKind())) |
| 6815 | return CGF.Builder.getInt32(1); |
| 6816 | return DefaultThreadLimitVal; |
| 6817 | } |
| 6818 | return DefaultThreadLimitVal ? DefaultThreadLimitVal |
| 6819 | : CGF.Builder.getInt32(0); |
| Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 6820 | } |
| 6821 | |
| Arpith Chacko Jacob | 33c849a | 2017-01-25 00:57:16 +0000 | [diff] [blame] | 6822 | /// Emit the number of threads for a target directive. Inspect the |
| 6823 | /// thread_limit clause associated with a teams construct combined or closely |
| 6824 | /// nested with the target directive. |
| 6825 | /// |
| 6826 | /// Emit the num_threads clause for directives such as 'target parallel' that |
| 6827 | /// have no associated teams construct. |
| 6828 | /// |
| 6829 | /// Otherwise, return nullptr. |
| Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 6830 | static llvm::Value * |
| Alexey Bataev | 5c42736 | 2019-04-10 19:11:33 +0000 | [diff] [blame] | 6831 | emitNumThreadsForTargetDirective(CodeGenFunction &CGF, |
| Arpith Chacko Jacob | 33c849a | 2017-01-25 00:57:16 +0000 | [diff] [blame] | 6832 | const OMPExecutableDirective &D) { |
| Alexey Bataev | 5c42736 | 2019-04-10 19:11:33 +0000 | [diff] [blame] | 6833 | assert(!CGF.getLangOpts().OpenMPIsDevice && |
| 6834 | "Clauses associated with the teams directive expected to be emitted " |
| 6835 | "only for the host!"); |
| 6836 | OpenMPDirectiveKind DirectiveKind = D.getDirectiveKind(); |
| 6837 | assert(isOpenMPTargetExecutionDirective(DirectiveKind) && |
| 6838 | "Expected target-based executable directive."); |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6839 | CGBuilderTy &Bld = CGF.Builder; |
| Alexey Bataev | 5c42736 | 2019-04-10 19:11:33 +0000 | [diff] [blame] | 6840 | llvm::Value *ThreadLimitVal = nullptr; |
| 6841 | llvm::Value *NumThreadsVal = nullptr; |
| 6842 | switch (DirectiveKind) { |
| 6843 | case OMPD_target: { |
| 6844 | const CapturedStmt *CS = D.getInnermostCapturedStmt(); |
| 6845 | if (llvm::Value *NumThreads = getNumThreads(CGF, CS, ThreadLimitVal)) |
| 6846 | return NumThreads; |
| 6847 | const Stmt *Child = CGOpenMPRuntime::getSingleCompoundChild( |
| 6848 | CGF.getContext(), CS->getCapturedStmt()); |
| 6849 | if (const auto *Dir = dyn_cast_or_null<OMPExecutableDirective>(Child)) { |
| 6850 | if (Dir->hasClausesOfKind<OMPThreadLimitClause>()) { |
| 6851 | CGOpenMPInnerExprInfo CGInfo(CGF, *CS); |
| 6852 | CodeGenFunction::CGCapturedStmtRAII CapInfoRAII(CGF, &CGInfo); |
| 6853 | const auto *ThreadLimitClause = |
| 6854 | Dir->getSingleClause<OMPThreadLimitClause>(); |
| 6855 | CodeGenFunction::LexicalScope Scope( |
| 6856 | CGF, ThreadLimitClause->getThreadLimit()->getSourceRange()); |
| 6857 | if (const auto *PreInit = |
| 6858 | cast_or_null<DeclStmt>(ThreadLimitClause->getPreInitStmt())) { |
| 6859 | for (const auto *I : PreInit->decls()) { |
| 6860 | if (!I->hasAttr<OMPCaptureNoInitAttr>()) { |
| 6861 | CGF.EmitVarDecl(cast<VarDecl>(*I)); |
| 6862 | } else { |
| 6863 | CodeGenFunction::AutoVarEmission Emission = |
| 6864 | CGF.EmitAutoVarAlloca(cast<VarDecl>(*I)); |
| 6865 | CGF.EmitAutoVarCleanups(Emission); |
| 6866 | } |
| 6867 | } |
| 6868 | } |
| 6869 | llvm::Value *ThreadLimit = CGF.EmitScalarExpr( |
| 6870 | ThreadLimitClause->getThreadLimit(), /*IgnoreResultAssign=*/true); |
| 6871 | ThreadLimitVal = |
| Rui Ueyama | 49a3ad2 | 2019-07-16 04:46:31 +0000 | [diff] [blame] | 6872 | Bld.CreateIntCast(ThreadLimit, CGF.Int32Ty, /*isSigned=*/false); |
| Alexey Bataev | 5c42736 | 2019-04-10 19:11:33 +0000 | [diff] [blame] | 6873 | } |
| 6874 | if (isOpenMPTeamsDirective(Dir->getDirectiveKind()) && |
| 6875 | !isOpenMPDistributeDirective(Dir->getDirectiveKind())) { |
| 6876 | CS = Dir->getInnermostCapturedStmt(); |
| 6877 | const Stmt *Child = CGOpenMPRuntime::getSingleCompoundChild( |
| 6878 | CGF.getContext(), CS->getCapturedStmt()); |
| 6879 | Dir = dyn_cast_or_null<OMPExecutableDirective>(Child); |
| 6880 | } |
| 6881 | if (Dir && isOpenMPDistributeDirective(Dir->getDirectiveKind()) && |
| 6882 | !isOpenMPSimdDirective(Dir->getDirectiveKind())) { |
| 6883 | CS = Dir->getInnermostCapturedStmt(); |
| 6884 | if (llvm::Value *NumThreads = getNumThreads(CGF, CS, ThreadLimitVal)) |
| 6885 | return NumThreads; |
| 6886 | } |
| 6887 | if (Dir && isOpenMPSimdDirective(Dir->getDirectiveKind())) |
| 6888 | return Bld.getInt32(1); |
| Arpith Chacko Jacob | 99a1e0e | 2017-01-25 02:18:43 +0000 | [diff] [blame] | 6889 | } |
| Alexey Bataev | 5c42736 | 2019-04-10 19:11:33 +0000 | [diff] [blame] | 6890 | return ThreadLimitVal ? ThreadLimitVal : Bld.getInt32(0); |
| 6891 | } |
| 6892 | case OMPD_target_teams: { |
| 6893 | if (D.hasClausesOfKind<OMPThreadLimitClause>()) { |
| 6894 | CodeGenFunction::RunCleanupsScope ThreadLimitScope(CGF); |
| 6895 | const auto *ThreadLimitClause = D.getSingleClause<OMPThreadLimitClause>(); |
| 6896 | llvm::Value *ThreadLimit = CGF.EmitScalarExpr( |
| 6897 | ThreadLimitClause->getThreadLimit(), /*IgnoreResultAssign=*/true); |
| 6898 | ThreadLimitVal = |
| Rui Ueyama | 49a3ad2 | 2019-07-16 04:46:31 +0000 | [diff] [blame] | 6899 | Bld.CreateIntCast(ThreadLimit, CGF.Int32Ty, /*isSigned=*/false); |
| Alexey Bataev | 5c42736 | 2019-04-10 19:11:33 +0000 | [diff] [blame] | 6900 | } |
| 6901 | const CapturedStmt *CS = D.getInnermostCapturedStmt(); |
| 6902 | if (llvm::Value *NumThreads = getNumThreads(CGF, CS, ThreadLimitVal)) |
| 6903 | return NumThreads; |
| 6904 | const Stmt *Child = CGOpenMPRuntime::getSingleCompoundChild( |
| 6905 | CGF.getContext(), CS->getCapturedStmt()); |
| 6906 | if (const auto *Dir = dyn_cast_or_null<OMPExecutableDirective>(Child)) { |
| 6907 | if (Dir->getDirectiveKind() == OMPD_distribute) { |
| 6908 | CS = Dir->getInnermostCapturedStmt(); |
| 6909 | if (llvm::Value *NumThreads = getNumThreads(CGF, CS, ThreadLimitVal)) |
| 6910 | return NumThreads; |
| 6911 | } |
| 6912 | } |
| 6913 | return ThreadLimitVal ? ThreadLimitVal : Bld.getInt32(0); |
| 6914 | } |
| 6915 | case OMPD_target_teams_distribute: |
| 6916 | if (D.hasClausesOfKind<OMPThreadLimitClause>()) { |
| 6917 | CodeGenFunction::RunCleanupsScope ThreadLimitScope(CGF); |
| 6918 | const auto *ThreadLimitClause = D.getSingleClause<OMPThreadLimitClause>(); |
| 6919 | llvm::Value *ThreadLimit = CGF.EmitScalarExpr( |
| 6920 | ThreadLimitClause->getThreadLimit(), /*IgnoreResultAssign=*/true); |
| 6921 | ThreadLimitVal = |
| Rui Ueyama | 49a3ad2 | 2019-07-16 04:46:31 +0000 | [diff] [blame] | 6922 | Bld.CreateIntCast(ThreadLimit, CGF.Int32Ty, /*isSigned=*/false); |
| Alexey Bataev | 5c42736 | 2019-04-10 19:11:33 +0000 | [diff] [blame] | 6923 | } |
| 6924 | return getNumThreads(CGF, D.getInnermostCapturedStmt(), ThreadLimitVal); |
| 6925 | case OMPD_target_parallel: |
| 6926 | case OMPD_target_parallel_for: |
| 6927 | case OMPD_target_parallel_for_simd: |
| 6928 | case OMPD_target_teams_distribute_parallel_for: |
| Alexey Bataev | dc9e7dc | 2019-04-17 16:53:08 +0000 | [diff] [blame] | 6929 | case OMPD_target_teams_distribute_parallel_for_simd: { |
| 6930 | llvm::Value *CondVal = nullptr; |
| 6931 | // Handle if clause. If if clause present, the number of threads is |
| 6932 | // calculated as <cond> ? (<numthreads> ? <numthreads> : 0 ) : 1. |
| 6933 | if (D.hasClausesOfKind<OMPIfClause>()) { |
| 6934 | const OMPIfClause *IfClause = nullptr; |
| 6935 | for (const auto *C : D.getClausesOfKind<OMPIfClause>()) { |
| 6936 | if (C->getNameModifier() == OMPD_unknown || |
| 6937 | C->getNameModifier() == OMPD_parallel) { |
| 6938 | IfClause = C; |
| 6939 | break; |
| 6940 | } |
| 6941 | } |
| 6942 | if (IfClause) { |
| 6943 | const Expr *Cond = IfClause->getCondition(); |
| 6944 | bool Result; |
| 6945 | if (Cond->EvaluateAsBooleanCondition(Result, CGF.getContext())) { |
| 6946 | if (!Result) |
| 6947 | return Bld.getInt32(1); |
| 6948 | } else { |
| 6949 | CodeGenFunction::RunCleanupsScope Scope(CGF); |
| 6950 | CondVal = CGF.EvaluateExprAsBool(Cond); |
| 6951 | } |
| 6952 | } |
| 6953 | } |
| Alexey Bataev | 5c42736 | 2019-04-10 19:11:33 +0000 | [diff] [blame] | 6954 | if (D.hasClausesOfKind<OMPThreadLimitClause>()) { |
| 6955 | CodeGenFunction::RunCleanupsScope ThreadLimitScope(CGF); |
| 6956 | const auto *ThreadLimitClause = D.getSingleClause<OMPThreadLimitClause>(); |
| 6957 | llvm::Value *ThreadLimit = CGF.EmitScalarExpr( |
| 6958 | ThreadLimitClause->getThreadLimit(), /*IgnoreResultAssign=*/true); |
| 6959 | ThreadLimitVal = |
| Rui Ueyama | 49a3ad2 | 2019-07-16 04:46:31 +0000 | [diff] [blame] | 6960 | Bld.CreateIntCast(ThreadLimit, CGF.Int32Ty, /*isSigned=*/false); |
| Alexey Bataev | 5c42736 | 2019-04-10 19:11:33 +0000 | [diff] [blame] | 6961 | } |
| 6962 | if (D.hasClausesOfKind<OMPNumThreadsClause>()) { |
| Arpith Chacko Jacob | 33c849a | 2017-01-25 00:57:16 +0000 | [diff] [blame] | 6963 | CodeGenFunction::RunCleanupsScope NumThreadsScope(CGF); |
| Alexey Bataev | 5c42736 | 2019-04-10 19:11:33 +0000 | [diff] [blame] | 6964 | const auto *NumThreadsClause = D.getSingleClause<OMPNumThreadsClause>(); |
| 6965 | llvm::Value *NumThreads = CGF.EmitScalarExpr( |
| 6966 | NumThreadsClause->getNumThreads(), /*IgnoreResultAssign=*/true); |
| Arpith Chacko Jacob | 33c849a | 2017-01-25 00:57:16 +0000 | [diff] [blame] | 6967 | NumThreadsVal = |
| Rui Ueyama | 49a3ad2 | 2019-07-16 04:46:31 +0000 | [diff] [blame] | 6968 | Bld.CreateIntCast(NumThreads, CGF.Int32Ty, /*isSigned=*/false); |
| Arpith Chacko Jacob | 99a1e0e | 2017-01-25 02:18:43 +0000 | [diff] [blame] | 6969 | ThreadLimitVal = ThreadLimitVal |
| Alexey Bataev | 5c42736 | 2019-04-10 19:11:33 +0000 | [diff] [blame] | 6970 | ? Bld.CreateSelect(Bld.CreateICmpULT(NumThreadsVal, |
| Arpith Chacko Jacob | 99a1e0e | 2017-01-25 02:18:43 +0000 | [diff] [blame] | 6971 | ThreadLimitVal), |
| 6972 | NumThreadsVal, ThreadLimitVal) |
| 6973 | : NumThreadsVal; |
| Alexey Bataev | 50a1c78 | 2017-12-01 21:31:08 +0000 | [diff] [blame] | 6974 | } |
| Alexey Bataev | dc9e7dc | 2019-04-17 16:53:08 +0000 | [diff] [blame] | 6975 | if (!ThreadLimitVal) |
| 6976 | ThreadLimitVal = Bld.getInt32(0); |
| 6977 | if (CondVal) |
| 6978 | return Bld.CreateSelect(CondVal, ThreadLimitVal, Bld.getInt32(1)); |
| 6979 | return ThreadLimitVal; |
| 6980 | } |
| Alexey Bataev | 5c42736 | 2019-04-10 19:11:33 +0000 | [diff] [blame] | 6981 | case OMPD_target_teams_distribute_simd: |
| 6982 | case OMPD_target_simd: |
| 6983 | return Bld.getInt32(1); |
| 6984 | case OMPD_parallel: |
| 6985 | case OMPD_for: |
| 6986 | case OMPD_parallel_for: |
| cchen | 47d6094 | 2019-12-05 13:43:48 -0500 | [diff] [blame] | 6987 | case OMPD_parallel_master: |
| Alexey Bataev | 5c42736 | 2019-04-10 19:11:33 +0000 | [diff] [blame] | 6988 | case OMPD_parallel_sections: |
| 6989 | case OMPD_for_simd: |
| 6990 | case OMPD_parallel_for_simd: |
| 6991 | case OMPD_cancel: |
| 6992 | case OMPD_cancellation_point: |
| 6993 | case OMPD_ordered: |
| 6994 | case OMPD_threadprivate: |
| 6995 | case OMPD_allocate: |
| 6996 | case OMPD_task: |
| 6997 | case OMPD_simd: |
| 6998 | case OMPD_sections: |
| 6999 | case OMPD_section: |
| 7000 | case OMPD_single: |
| 7001 | case OMPD_master: |
| 7002 | case OMPD_critical: |
| 7003 | case OMPD_taskyield: |
| 7004 | case OMPD_barrier: |
| 7005 | case OMPD_taskwait: |
| 7006 | case OMPD_taskgroup: |
| 7007 | case OMPD_atomic: |
| 7008 | case OMPD_flush: |
| 7009 | case OMPD_teams: |
| 7010 | case OMPD_target_data: |
| 7011 | case OMPD_target_exit_data: |
| 7012 | case OMPD_target_enter_data: |
| 7013 | case OMPD_distribute: |
| 7014 | case OMPD_distribute_simd: |
| 7015 | case OMPD_distribute_parallel_for: |
| 7016 | case OMPD_distribute_parallel_for_simd: |
| 7017 | case OMPD_teams_distribute: |
| 7018 | case OMPD_teams_distribute_simd: |
| 7019 | case OMPD_teams_distribute_parallel_for: |
| 7020 | case OMPD_teams_distribute_parallel_for_simd: |
| 7021 | case OMPD_target_update: |
| 7022 | case OMPD_declare_simd: |
| Alexey Bataev | d158cf6 | 2019-09-13 20:18:17 +0000 | [diff] [blame] | 7023 | case OMPD_declare_variant: |
| Alexey Bataev | 5c42736 | 2019-04-10 19:11:33 +0000 | [diff] [blame] | 7024 | case OMPD_declare_target: |
| 7025 | case OMPD_end_declare_target: |
| 7026 | case OMPD_declare_reduction: |
| 7027 | case OMPD_declare_mapper: |
| 7028 | case OMPD_taskloop: |
| 7029 | case OMPD_taskloop_simd: |
| Alexey Bataev | 60e51c4 | 2019-10-10 20:13:02 +0000 | [diff] [blame] | 7030 | case OMPD_master_taskloop: |
| Alexey Bataev | b8552ab | 2019-10-18 16:47:35 +0000 | [diff] [blame] | 7031 | case OMPD_master_taskloop_simd: |
| Alexey Bataev | 5bbcead | 2019-10-14 17:17:41 +0000 | [diff] [blame] | 7032 | case OMPD_parallel_master_taskloop: |
| Alexey Bataev | 14a388f | 2019-10-25 10:27:13 -0400 | [diff] [blame] | 7033 | case OMPD_parallel_master_taskloop_simd: |
| Alexey Bataev | 5c42736 | 2019-04-10 19:11:33 +0000 | [diff] [blame] | 7034 | case OMPD_requires: |
| 7035 | case OMPD_unknown: |
| 7036 | break; |
| Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 7037 | } |
| Alexey Bataev | 5c42736 | 2019-04-10 19:11:33 +0000 | [diff] [blame] | 7038 | llvm_unreachable("Unsupported directive kind."); |
| Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 7039 | } |
| 7040 | |
| Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7041 | namespace { |
| Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7042 | LLVM_ENABLE_BITMASK_ENUMS_IN_NAMESPACE(); |
| 7043 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 7044 | // Utility to handle information from clauses associated with a given |
| Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7045 | // construct that use mappable expressions (e.g. 'map' clause, 'to' clause). |
| 7046 | // It provides a convenient interface to obtain the information and generate |
| 7047 | // code for that information. |
| 7048 | class MappableExprsHandler { |
| 7049 | public: |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 7050 | /// Values for bit flags used to specify the mapping type for |
| Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7051 | /// offloading. |
| Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7052 | enum OpenMPOffloadMappingFlags : uint64_t { |
| 7053 | /// No flags |
| 7054 | OMP_MAP_NONE = 0x0, |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 7055 | /// Allocate memory on the device and move data from host to device. |
| Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7056 | OMP_MAP_TO = 0x01, |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 7057 | /// Allocate memory on the device and move data from device to host. |
| Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7058 | OMP_MAP_FROM = 0x02, |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 7059 | /// Always perform the requested mapping action on the element, even |
| Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7060 | /// if it was already mapped before. |
| 7061 | OMP_MAP_ALWAYS = 0x04, |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 7062 | /// Delete the element from the device environment, ignoring the |
| Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7063 | /// current reference count associated with the element. |
| Samuel Antao | 6782e94 | 2016-05-26 16:48:10 +0000 | [diff] [blame] | 7064 | OMP_MAP_DELETE = 0x08, |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 7065 | /// The element being mapped is a pointer-pointee pair; both the |
| George Rokos | 065755d | 2017-11-07 18:27:04 +0000 | [diff] [blame] | 7066 | /// pointer and the pointee should be mapped. |
| 7067 | OMP_MAP_PTR_AND_OBJ = 0x10, |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 7068 | /// This flags signals that the base address of an entry should be |
| George Rokos | 065755d | 2017-11-07 18:27:04 +0000 | [diff] [blame] | 7069 | /// passed to the target kernel as an argument. |
| 7070 | OMP_MAP_TARGET_PARAM = 0x20, |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 7071 | /// Signal that the runtime library has to return the device pointer |
| George Rokos | 065755d | 2017-11-07 18:27:04 +0000 | [diff] [blame] | 7072 | /// in the current position for the data being mapped. Used when we have the |
| 7073 | /// use_device_ptr clause. |
| 7074 | OMP_MAP_RETURN_PARAM = 0x40, |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 7075 | /// This flag signals that the reference being passed is a pointer to |
| Samuel Antao | d486f84 | 2016-05-26 16:53:38 +0000 | [diff] [blame] | 7076 | /// private data. |
| George Rokos | 065755d | 2017-11-07 18:27:04 +0000 | [diff] [blame] | 7077 | OMP_MAP_PRIVATE = 0x80, |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 7078 | /// Pass the element to the device by value. |
| George Rokos | 065755d | 2017-11-07 18:27:04 +0000 | [diff] [blame] | 7079 | OMP_MAP_LITERAL = 0x100, |
| Alexey Bataev | f47c4b4 | 2017-09-26 13:47:31 +0000 | [diff] [blame] | 7080 | /// Implicit map |
| 7081 | OMP_MAP_IMPLICIT = 0x200, |
| Gheorghe-Teodor Bercea | 0fd073b | 2019-08-09 21:42:13 +0000 | [diff] [blame] | 7082 | /// Close is a hint to the runtime to allocate memory close to |
| 7083 | /// the target device. |
| 7084 | OMP_MAP_CLOSE = 0x400, |
| Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7085 | /// The 16 MSBs of the flags indicate whether the entry is member of some |
| 7086 | /// struct/class. |
| 7087 | OMP_MAP_MEMBER_OF = 0xffff000000000000, |
| 7088 | LLVM_MARK_AS_BITMASK_ENUM(/* LargestFlag = */ OMP_MAP_MEMBER_OF), |
| Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7089 | }; |
| 7090 | |
| Michael Kruse | d47b943 | 2019-08-05 18:43:21 +0000 | [diff] [blame] | 7091 | /// Get the offset of the OMP_MAP_MEMBER_OF field. |
| 7092 | static unsigned getFlagMemberOffset() { |
| 7093 | unsigned Offset = 0; |
| 7094 | for (uint64_t Remain = OMP_MAP_MEMBER_OF; !(Remain & 1); |
| 7095 | Remain = Remain >> 1) |
| 7096 | Offset++; |
| 7097 | return Offset; |
| 7098 | } |
| 7099 | |
| Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7100 | /// Class that associates information with a base pointer to be passed to the |
| 7101 | /// runtime library. |
| 7102 | class BasePointerInfo { |
| 7103 | /// The base pointer. |
| 7104 | llvm::Value *Ptr = nullptr; |
| 7105 | /// The base declaration that refers to this device pointer, or null if |
| 7106 | /// there is none. |
| 7107 | const ValueDecl *DevPtrDecl = nullptr; |
| 7108 | |
| 7109 | public: |
| 7110 | BasePointerInfo(llvm::Value *Ptr, const ValueDecl *DevPtrDecl = nullptr) |
| 7111 | : Ptr(Ptr), DevPtrDecl(DevPtrDecl) {} |
| 7112 | llvm::Value *operator*() const { return Ptr; } |
| 7113 | const ValueDecl *getDevicePtrDecl() const { return DevPtrDecl; } |
| 7114 | void setDevicePtrDecl(const ValueDecl *D) { DevPtrDecl = D; } |
| 7115 | }; |
| 7116 | |
| Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7117 | using MapBaseValuesArrayTy = SmallVector<BasePointerInfo, 4>; |
| 7118 | using MapValuesArrayTy = SmallVector<llvm::Value *, 4>; |
| 7119 | using MapFlagsArrayTy = SmallVector<OpenMPOffloadMappingFlags, 4>; |
| 7120 | |
| 7121 | /// Map between a struct and the its lowest & highest elements which have been |
| 7122 | /// mapped. |
| 7123 | /// [ValueDecl *] --> {LE(FieldIndex, Pointer), |
| 7124 | /// HE(FieldIndex, Pointer)} |
| 7125 | struct StructRangeInfoTy { |
| 7126 | std::pair<unsigned /*FieldIndex*/, Address /*Pointer*/> LowestElem = { |
| 7127 | 0, Address::invalid()}; |
| 7128 | std::pair<unsigned /*FieldIndex*/, Address /*Pointer*/> HighestElem = { |
| 7129 | 0, Address::invalid()}; |
| 7130 | Address Base = Address::invalid(); |
| 7131 | }; |
| Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7132 | |
| 7133 | private: |
| Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7134 | /// Kind that defines how a device pointer has to be returned. |
| 7135 | struct MapInfo { |
| 7136 | OMPClauseMappableExprCommon::MappableExprComponentListRef Components; |
| 7137 | OpenMPMapClauseKind MapType = OMPC_MAP_unknown; |
| Kelvin Li | ef57943 | 2018-12-18 22:18:41 +0000 | [diff] [blame] | 7138 | ArrayRef<OpenMPMapModifierKind> MapModifiers; |
| Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7139 | bool ReturnDevicePointer = false; |
| 7140 | bool IsImplicit = false; |
| 7141 | |
| 7142 | MapInfo() = default; |
| 7143 | MapInfo( |
| 7144 | OMPClauseMappableExprCommon::MappableExprComponentListRef Components, |
| Kelvin Li | ef57943 | 2018-12-18 22:18:41 +0000 | [diff] [blame] | 7145 | OpenMPMapClauseKind MapType, |
| 7146 | ArrayRef<OpenMPMapModifierKind> MapModifiers, |
| Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7147 | bool ReturnDevicePointer, bool IsImplicit) |
| Kelvin Li | ef57943 | 2018-12-18 22:18:41 +0000 | [diff] [blame] | 7148 | : Components(Components), MapType(MapType), MapModifiers(MapModifiers), |
| Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7149 | ReturnDevicePointer(ReturnDevicePointer), IsImplicit(IsImplicit) {} |
| 7150 | }; |
| 7151 | |
| 7152 | /// If use_device_ptr is used on a pointer which is a struct member and there |
| 7153 | /// is no map information about it, then emission of that entry is deferred |
| 7154 | /// until the whole struct has been processed. |
| 7155 | struct DeferredDevicePtrEntryTy { |
| 7156 | const Expr *IE = nullptr; |
| 7157 | const ValueDecl *VD = nullptr; |
| 7158 | |
| 7159 | DeferredDevicePtrEntryTy(const Expr *IE, const ValueDecl *VD) |
| 7160 | : IE(IE), VD(VD) {} |
| 7161 | }; |
| 7162 | |
| Michael Kruse | d47b943 | 2019-08-05 18:43:21 +0000 | [diff] [blame] | 7163 | /// The target directive from where the mappable clauses were extracted. It |
| 7164 | /// is either a executable directive or a user-defined mapper directive. |
| 7165 | llvm::PointerUnion<const OMPExecutableDirective *, |
| 7166 | const OMPDeclareMapperDecl *> |
| 7167 | CurDir; |
| Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7168 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 7169 | /// Function the directive is being generated for. |
| Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7170 | CodeGenFunction &CGF; |
| 7171 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 7172 | /// Set of all first private variables in the current directive. |
| Alexey Bataev | f288cf9 | 2019-06-27 18:53:07 +0000 | [diff] [blame] | 7173 | /// bool data is set to true if the variable is implicitly marked as |
| 7174 | /// firstprivate, false otherwise. |
| 7175 | llvm::DenseMap<CanonicalDeclPtr<const VarDecl>, bool> FirstPrivateDecls; |
| Samuel Antao | d486f84 | 2016-05-26 16:53:38 +0000 | [diff] [blame] | 7176 | |
| Samuel Antao | 6890b09 | 2016-07-28 14:25:09 +0000 | [diff] [blame] | 7177 | /// Map between device pointer declarations and their expression components. |
| 7178 | /// The key value for declarations in 'this' is null. |
| 7179 | llvm::DenseMap< |
| 7180 | const ValueDecl *, |
| 7181 | SmallVector<OMPClauseMappableExprCommon::MappableExprComponentListRef, 4>> |
| 7182 | DevPointersMap; |
| 7183 | |
| Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7184 | llvm::Value *getExprTypeSize(const Expr *E) const { |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 7185 | QualType ExprTy = E->getType().getCanonicalType(); |
| Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7186 | |
| 7187 | // Reference types are ignored for mapping purposes. |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 7188 | if (const auto *RefTy = ExprTy->getAs<ReferenceType>()) |
| Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7189 | ExprTy = RefTy->getPointeeType().getCanonicalType(); |
| 7190 | |
| 7191 | // Given that an array section is considered a built-in type, we need to |
| 7192 | // do the calculation based on the length of the section instead of relying |
| 7193 | // on CGF.getTypeSize(E->getType()). |
| 7194 | if (const auto *OAE = dyn_cast<OMPArraySectionExpr>(E)) { |
| 7195 | QualType BaseTy = OMPArraySectionExpr::getBaseOriginalType( |
| 7196 | OAE->getBase()->IgnoreParenImpCasts()) |
| 7197 | .getCanonicalType(); |
| 7198 | |
| Alexey Bataev | b9b8ca2 | 2019-10-01 18:18:03 +0000 | [diff] [blame] | 7199 | // If there is no length associated with the expression and lower bound is |
| 7200 | // not specified too, that means we are using the whole length of the |
| 7201 | // base. |
| 7202 | if (!OAE->getLength() && OAE->getColonLoc().isValid() && |
| 7203 | !OAE->getLowerBound()) |
| Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7204 | return CGF.getTypeSize(BaseTy); |
| 7205 | |
| 7206 | llvm::Value *ElemSize; |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 7207 | if (const auto *PTy = BaseTy->getAs<PointerType>()) { |
| Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7208 | ElemSize = CGF.getTypeSize(PTy->getPointeeType().getCanonicalType()); |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 7209 | } else { |
| 7210 | const auto *ATy = cast<ArrayType>(BaseTy.getTypePtr()); |
| Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7211 | assert(ATy && "Expecting array type if not a pointer type."); |
| 7212 | ElemSize = CGF.getTypeSize(ATy->getElementType().getCanonicalType()); |
| 7213 | } |
| 7214 | |
| 7215 | // If we don't have a length at this point, that is because we have an |
| 7216 | // array section with a single element. |
| Alexey Bataev | b9b8ca2 | 2019-10-01 18:18:03 +0000 | [diff] [blame] | 7217 | if (!OAE->getLength() && OAE->getColonLoc().isInvalid()) |
| Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7218 | return ElemSize; |
| 7219 | |
| Alexey Bataev | b9b8ca2 | 2019-10-01 18:18:03 +0000 | [diff] [blame] | 7220 | if (const Expr *LenExpr = OAE->getLength()) { |
| Michael Liao | d838cf7 | 2019-10-02 00:22:45 +0000 | [diff] [blame] | 7221 | llvm::Value *LengthVal = CGF.EmitScalarExpr(LenExpr); |
| 7222 | LengthVal = CGF.EmitScalarConversion(LengthVal, LenExpr->getType(), |
| 7223 | CGF.getContext().getSizeType(), |
| 7224 | LenExpr->getExprLoc()); |
| Alexey Bataev | b9b8ca2 | 2019-10-01 18:18:03 +0000 | [diff] [blame] | 7225 | return CGF.Builder.CreateNUWMul(LengthVal, ElemSize); |
| 7226 | } |
| 7227 | assert(!OAE->getLength() && OAE->getColonLoc().isValid() && |
| 7228 | OAE->getLowerBound() && "expected array_section[lb:]."); |
| 7229 | // Size = sizetype - lb * elemtype; |
| 7230 | llvm::Value *LengthVal = CGF.getTypeSize(BaseTy); |
| 7231 | llvm::Value *LBVal = CGF.EmitScalarExpr(OAE->getLowerBound()); |
| 7232 | LBVal = CGF.EmitScalarConversion(LBVal, OAE->getLowerBound()->getType(), |
| 7233 | CGF.getContext().getSizeType(), |
| 7234 | OAE->getLowerBound()->getExprLoc()); |
| 7235 | LBVal = CGF.Builder.CreateNUWMul(LBVal, ElemSize); |
| 7236 | llvm::Value *Cmp = CGF.Builder.CreateICmpUGT(LengthVal, LBVal); |
| 7237 | llvm::Value *TrueVal = CGF.Builder.CreateNUWSub(LengthVal, LBVal); |
| 7238 | LengthVal = CGF.Builder.CreateSelect( |
| 7239 | Cmp, TrueVal, llvm::ConstantInt::get(CGF.SizeTy, 0)); |
| 7240 | return LengthVal; |
| Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7241 | } |
| 7242 | return CGF.getTypeSize(ExprTy); |
| 7243 | } |
| 7244 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 7245 | /// Return the corresponding bits for a given map clause modifier. Add |
| Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7246 | /// a flag marking the map as a pointer if requested. Add a flag marking the |
| Samuel Antao | 6782e94 | 2016-05-26 16:48:10 +0000 | [diff] [blame] | 7247 | /// map as the first one of a series of maps that relate to the same map |
| 7248 | /// expression. |
| Kelvin Li | ef57943 | 2018-12-18 22:18:41 +0000 | [diff] [blame] | 7249 | OpenMPOffloadMappingFlags getMapTypeBits( |
| 7250 | OpenMPMapClauseKind MapType, ArrayRef<OpenMPMapModifierKind> MapModifiers, |
| 7251 | bool IsImplicit, bool AddPtrFlag, bool AddIsTargetParamFlag) const { |
| Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7252 | OpenMPOffloadMappingFlags Bits = |
| 7253 | IsImplicit ? OMP_MAP_IMPLICIT : OMP_MAP_NONE; |
| Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7254 | switch (MapType) { |
| 7255 | case OMPC_MAP_alloc: |
| Samuel Antao | 6782e94 | 2016-05-26 16:48:10 +0000 | [diff] [blame] | 7256 | case OMPC_MAP_release: |
| 7257 | // alloc and release is the default behavior in the runtime library, i.e. |
| 7258 | // if we don't pass any bits alloc/release that is what the runtime is |
| 7259 | // going to do. Therefore, we don't need to signal anything for these two |
| 7260 | // type modifiers. |
| Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7261 | break; |
| 7262 | case OMPC_MAP_to: |
| Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7263 | Bits |= OMP_MAP_TO; |
| Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7264 | break; |
| 7265 | case OMPC_MAP_from: |
| Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7266 | Bits |= OMP_MAP_FROM; |
| Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7267 | break; |
| 7268 | case OMPC_MAP_tofrom: |
| Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7269 | Bits |= OMP_MAP_TO | OMP_MAP_FROM; |
| Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7270 | break; |
| 7271 | case OMPC_MAP_delete: |
| Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7272 | Bits |= OMP_MAP_DELETE; |
| Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7273 | break; |
| Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7274 | case OMPC_MAP_unknown: |
| Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7275 | llvm_unreachable("Unexpected map type!"); |
| Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7276 | } |
| 7277 | if (AddPtrFlag) |
| George Rokos | 065755d | 2017-11-07 18:27:04 +0000 | [diff] [blame] | 7278 | Bits |= OMP_MAP_PTR_AND_OBJ; |
| 7279 | if (AddIsTargetParamFlag) |
| 7280 | Bits |= OMP_MAP_TARGET_PARAM; |
| Kelvin Li | ef57943 | 2018-12-18 22:18:41 +0000 | [diff] [blame] | 7281 | if (llvm::find(MapModifiers, OMPC_MAP_MODIFIER_always) |
| 7282 | != MapModifiers.end()) |
| Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7283 | Bits |= OMP_MAP_ALWAYS; |
| Gheorghe-Teodor Bercea | 0fd073b | 2019-08-09 21:42:13 +0000 | [diff] [blame] | 7284 | if (llvm::find(MapModifiers, OMPC_MAP_MODIFIER_close) |
| 7285 | != MapModifiers.end()) |
| 7286 | Bits |= OMP_MAP_CLOSE; |
| Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7287 | return Bits; |
| 7288 | } |
| 7289 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 7290 | /// Return true if the provided expression is a final array section. A |
| Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7291 | /// final array section, is one whose length can't be proved to be one. |
| 7292 | bool isFinalArraySectionExpression(const Expr *E) const { |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 7293 | const auto *OASE = dyn_cast<OMPArraySectionExpr>(E); |
| Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7294 | |
| 7295 | // It is not an array section and therefore not a unity-size one. |
| 7296 | if (!OASE) |
| 7297 | return false; |
| 7298 | |
| 7299 | // An array section with no colon always refer to a single element. |
| 7300 | if (OASE->getColonLoc().isInvalid()) |
| 7301 | return false; |
| 7302 | |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 7303 | const Expr *Length = OASE->getLength(); |
| Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7304 | |
| 7305 | // If we don't have a length we have to check if the array has size 1 |
| 7306 | // for this dimension. Also, we should always expect a length if the |
| 7307 | // base type is pointer. |
| 7308 | if (!Length) { |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 7309 | QualType BaseQTy = OMPArraySectionExpr::getBaseOriginalType( |
| 7310 | OASE->getBase()->IgnoreParenImpCasts()) |
| 7311 | .getCanonicalType(); |
| 7312 | if (const auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr())) |
| Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7313 | return ATy->getSize().getSExtValue() != 1; |
| 7314 | // If we don't have a constant dimension length, we have to consider |
| 7315 | // the current section as having any size, so it is not necessarily |
| 7316 | // unitary. If it happen to be unity size, that's user fault. |
| 7317 | return true; |
| 7318 | } |
| 7319 | |
| 7320 | // Check if the length evaluates to 1. |
| Fangrui Song | 407659a | 2018-11-30 23:41:18 +0000 | [diff] [blame] | 7321 | Expr::EvalResult Result; |
| 7322 | if (!Length->EvaluateAsInt(Result, CGF.getContext())) |
| Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7323 | return true; // Can have more that size 1. |
| 7324 | |
| Fangrui Song | 407659a | 2018-11-30 23:41:18 +0000 | [diff] [blame] | 7325 | llvm::APSInt ConstLength = Result.Val.getInt(); |
| Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7326 | return ConstLength.getSExtValue() != 1; |
| 7327 | } |
| 7328 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 7329 | /// Generate the base pointers, section pointers, sizes and map type |
| Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7330 | /// bits for the provided map type, map modifier, and expression components. |
| 7331 | /// \a IsFirstComponent should be set to true if the provided set of |
| 7332 | /// components is the first associated with a capture. |
| 7333 | void generateInfoForComponentList( |
| Kelvin Li | ef57943 | 2018-12-18 22:18:41 +0000 | [diff] [blame] | 7334 | OpenMPMapClauseKind MapType, |
| 7335 | ArrayRef<OpenMPMapModifierKind> MapModifiers, |
| Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7336 | OMPClauseMappableExprCommon::MappableExprComponentListRef Components, |
| Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7337 | MapBaseValuesArrayTy &BasePointers, MapValuesArrayTy &Pointers, |
| Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7338 | MapValuesArrayTy &Sizes, MapFlagsArrayTy &Types, |
| Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7339 | StructRangeInfoTy &PartialStruct, bool IsFirstComponentList, |
| Alexey Bataev | e82445f | 2018-09-20 13:54:02 +0000 | [diff] [blame] | 7340 | bool IsImplicit, |
| 7341 | ArrayRef<OMPClauseMappableExprCommon::MappableExprComponentListRef> |
| 7342 | OverlappedElements = llvm::None) const { |
| Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7343 | // The following summarizes what has to be generated for each map and the |
| Alexander Kornienko | 2a8c18d | 2018-04-06 15:14:32 +0000 | [diff] [blame] | 7344 | // types below. The generated information is expressed in this order: |
| Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7345 | // base pointer, section pointer, size, flags |
| 7346 | // (to add to the ones that come from the map type and modifier). |
| 7347 | // |
| 7348 | // double d; |
| 7349 | // int i[100]; |
| 7350 | // float *p; |
| 7351 | // |
| 7352 | // struct S1 { |
| 7353 | // int i; |
| 7354 | // float f[50]; |
| 7355 | // } |
| 7356 | // struct S2 { |
| 7357 | // int i; |
| 7358 | // float f[50]; |
| 7359 | // S1 s; |
| 7360 | // double *p; |
| 7361 | // struct S2 *ps; |
| 7362 | // } |
| 7363 | // S2 s; |
| 7364 | // S2 *ps; |
| 7365 | // |
| 7366 | // map(d) |
| Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7367 | // &d, &d, sizeof(double), TARGET_PARAM | TO | FROM |
| Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7368 | // |
| 7369 | // map(i) |
| Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7370 | // &i, &i, 100*sizeof(int), TARGET_PARAM | TO | FROM |
| Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7371 | // |
| 7372 | // map(i[1:23]) |
| Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7373 | // &i(=&i[0]), &i[1], 23*sizeof(int), TARGET_PARAM | TO | FROM |
| Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7374 | // |
| 7375 | // map(p) |
| Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7376 | // &p, &p, sizeof(float*), TARGET_PARAM | TO | FROM |
| Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7377 | // |
| 7378 | // map(p[1:24]) |
| Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7379 | // p, &p[1], 24*sizeof(float), TARGET_PARAM | TO | FROM |
| Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7380 | // |
| 7381 | // map(s) |
| Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7382 | // &s, &s, sizeof(S2), TARGET_PARAM | TO | FROM |
| Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7383 | // |
| 7384 | // map(s.i) |
| Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7385 | // &s, &(s.i), sizeof(int), TARGET_PARAM | TO | FROM |
| Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7386 | // |
| 7387 | // map(s.s.f) |
| Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7388 | // &s, &(s.s.f[0]), 50*sizeof(float), TARGET_PARAM | TO | FROM |
| Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7389 | // |
| 7390 | // map(s.p) |
| Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7391 | // &s, &(s.p), sizeof(double*), TARGET_PARAM | TO | FROM |
| Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7392 | // |
| Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7393 | // map(to: s.p[:22]) |
| 7394 | // &s, &(s.p), sizeof(double*), TARGET_PARAM (*) |
| 7395 | // &s, &(s.p), sizeof(double*), MEMBER_OF(1) (**) |
| 7396 | // &(s.p), &(s.p[0]), 22*sizeof(double), |
| 7397 | // MEMBER_OF(1) | PTR_AND_OBJ | TO (***) |
| 7398 | // (*) alloc space for struct members, only this is a target parameter |
| 7399 | // (**) map the pointer (nothing to be mapped in this example) (the compiler |
| 7400 | // optimizes this entry out, same in the examples below) |
| 7401 | // (***) map the pointee (map: to) |
| Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7402 | // |
| 7403 | // map(s.ps) |
| Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7404 | // &s, &(s.ps), sizeof(S2*), TARGET_PARAM | TO | FROM |
| Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7405 | // |
| Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7406 | // map(from: s.ps->s.i) |
| 7407 | // &s, &(s.ps), sizeof(S2*), TARGET_PARAM |
| 7408 | // &s, &(s.ps), sizeof(S2*), MEMBER_OF(1) |
| 7409 | // &(s.ps), &(s.ps->s.i), sizeof(int), MEMBER_OF(1) | PTR_AND_OBJ | FROM |
| Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7410 | // |
| Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7411 | // map(to: s.ps->ps) |
| 7412 | // &s, &(s.ps), sizeof(S2*), TARGET_PARAM |
| 7413 | // &s, &(s.ps), sizeof(S2*), MEMBER_OF(1) |
| 7414 | // &(s.ps), &(s.ps->ps), sizeof(S2*), MEMBER_OF(1) | PTR_AND_OBJ | TO |
| Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7415 | // |
| 7416 | // map(s.ps->ps->ps) |
| Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7417 | // &s, &(s.ps), sizeof(S2*), TARGET_PARAM |
| 7418 | // &s, &(s.ps), sizeof(S2*), MEMBER_OF(1) |
| 7419 | // &(s.ps), &(s.ps->ps), sizeof(S2*), MEMBER_OF(1) | PTR_AND_OBJ |
| 7420 | // &(s.ps->ps), &(s.ps->ps->ps), sizeof(S2*), PTR_AND_OBJ | TO | FROM |
| Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7421 | // |
| Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7422 | // map(to: s.ps->ps->s.f[:22]) |
| 7423 | // &s, &(s.ps), sizeof(S2*), TARGET_PARAM |
| 7424 | // &s, &(s.ps), sizeof(S2*), MEMBER_OF(1) |
| 7425 | // &(s.ps), &(s.ps->ps), sizeof(S2*), MEMBER_OF(1) | PTR_AND_OBJ |
| 7426 | // &(s.ps->ps), &(s.ps->ps->s.f[0]), 22*sizeof(float), PTR_AND_OBJ | TO |
| Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7427 | // |
| 7428 | // map(ps) |
| Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7429 | // &ps, &ps, sizeof(S2*), TARGET_PARAM | TO | FROM |
| Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7430 | // |
| 7431 | // map(ps->i) |
| Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7432 | // ps, &(ps->i), sizeof(int), TARGET_PARAM | TO | FROM |
| Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7433 | // |
| 7434 | // map(ps->s.f) |
| Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7435 | // ps, &(ps->s.f[0]), 50*sizeof(float), TARGET_PARAM | TO | FROM |
| Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7436 | // |
| Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7437 | // map(from: ps->p) |
| 7438 | // ps, &(ps->p), sizeof(double*), TARGET_PARAM | FROM |
| Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7439 | // |
| Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7440 | // map(to: ps->p[:22]) |
| 7441 | // ps, &(ps->p), sizeof(double*), TARGET_PARAM |
| 7442 | // ps, &(ps->p), sizeof(double*), MEMBER_OF(1) |
| 7443 | // &(ps->p), &(ps->p[0]), 22*sizeof(double), MEMBER_OF(1) | PTR_AND_OBJ | TO |
| Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7444 | // |
| 7445 | // map(ps->ps) |
| Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7446 | // ps, &(ps->ps), sizeof(S2*), TARGET_PARAM | TO | FROM |
| Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7447 | // |
| Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7448 | // map(from: ps->ps->s.i) |
| 7449 | // ps, &(ps->ps), sizeof(S2*), TARGET_PARAM |
| 7450 | // ps, &(ps->ps), sizeof(S2*), MEMBER_OF(1) |
| 7451 | // &(ps->ps), &(ps->ps->s.i), sizeof(int), MEMBER_OF(1) | PTR_AND_OBJ | FROM |
| Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7452 | // |
| Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7453 | // map(from: ps->ps->ps) |
| 7454 | // ps, &(ps->ps), sizeof(S2*), TARGET_PARAM |
| 7455 | // ps, &(ps->ps), sizeof(S2*), MEMBER_OF(1) |
| 7456 | // &(ps->ps), &(ps->ps->ps), sizeof(S2*), MEMBER_OF(1) | PTR_AND_OBJ | FROM |
| Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7457 | // |
| 7458 | // map(ps->ps->ps->ps) |
| Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7459 | // ps, &(ps->ps), sizeof(S2*), TARGET_PARAM |
| 7460 | // ps, &(ps->ps), sizeof(S2*), MEMBER_OF(1) |
| 7461 | // &(ps->ps), &(ps->ps->ps), sizeof(S2*), MEMBER_OF(1) | PTR_AND_OBJ |
| 7462 | // &(ps->ps->ps), &(ps->ps->ps->ps), sizeof(S2*), PTR_AND_OBJ | TO | FROM |
| Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7463 | // |
| Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7464 | // map(to: ps->ps->ps->s.f[:22]) |
| 7465 | // ps, &(ps->ps), sizeof(S2*), TARGET_PARAM |
| 7466 | // ps, &(ps->ps), sizeof(S2*), MEMBER_OF(1) |
| 7467 | // &(ps->ps), &(ps->ps->ps), sizeof(S2*), MEMBER_OF(1) | PTR_AND_OBJ |
| 7468 | // &(ps->ps->ps), &(ps->ps->ps->s.f[0]), 22*sizeof(float), PTR_AND_OBJ | TO |
| 7469 | // |
| 7470 | // map(to: s.f[:22]) map(from: s.p[:33]) |
| 7471 | // &s, &(s.f[0]), 50*sizeof(float) + sizeof(struct S1) + |
| 7472 | // sizeof(double*) (**), TARGET_PARAM |
| 7473 | // &s, &(s.f[0]), 22*sizeof(float), MEMBER_OF(1) | TO |
| 7474 | // &s, &(s.p), sizeof(double*), MEMBER_OF(1) |
| 7475 | // &(s.p), &(s.p[0]), 33*sizeof(double), MEMBER_OF(1) | PTR_AND_OBJ | FROM |
| 7476 | // (*) allocate contiguous space needed to fit all mapped members even if |
| 7477 | // we allocate space for members not mapped (in this example, |
| 7478 | // s.f[22..49] and s.s are not mapped, yet we must allocate space for |
| 7479 | // them as well because they fall between &s.f[0] and &s.p) |
| 7480 | // |
| 7481 | // map(from: s.f[:22]) map(to: ps->p[:33]) |
| 7482 | // &s, &(s.f[0]), 22*sizeof(float), TARGET_PARAM | FROM |
| 7483 | // ps, &(ps->p), sizeof(S2*), TARGET_PARAM |
| 7484 | // ps, &(ps->p), sizeof(double*), MEMBER_OF(2) (*) |
| 7485 | // &(ps->p), &(ps->p[0]), 33*sizeof(double), MEMBER_OF(2) | PTR_AND_OBJ | TO |
| 7486 | // (*) the struct this entry pertains to is the 2nd element in the list of |
| 7487 | // arguments, hence MEMBER_OF(2) |
| 7488 | // |
| 7489 | // map(from: s.f[:22], s.s) map(to: ps->p[:33]) |
| 7490 | // &s, &(s.f[0]), 50*sizeof(float) + sizeof(struct S1), TARGET_PARAM |
| 7491 | // &s, &(s.f[0]), 22*sizeof(float), MEMBER_OF(1) | FROM |
| 7492 | // &s, &(s.s), sizeof(struct S1), MEMBER_OF(1) | FROM |
| 7493 | // ps, &(ps->p), sizeof(S2*), TARGET_PARAM |
| 7494 | // ps, &(ps->p), sizeof(double*), MEMBER_OF(4) (*) |
| 7495 | // &(ps->p), &(ps->p[0]), 33*sizeof(double), MEMBER_OF(4) | PTR_AND_OBJ | TO |
| 7496 | // (*) the struct this entry pertains to is the 4th element in the list |
| 7497 | // of arguments, hence MEMBER_OF(4) |
| Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7498 | |
| 7499 | // Track if the map information being generated is the first for a capture. |
| 7500 | bool IsCaptureFirstInfo = IsFirstComponentList; |
| Gheorghe-Teodor Bercea | 0034e84 | 2019-06-20 18:04:47 +0000 | [diff] [blame] | 7501 | // When the variable is on a declare target link or in a to clause with |
| 7502 | // unified memory, a reference is needed to hold the host/device address |
| 7503 | // of the variable. |
| 7504 | bool RequiresReference = false; |
| Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7505 | |
| 7506 | // Scan the components from the base to the complete expression. |
| 7507 | auto CI = Components.rbegin(); |
| 7508 | auto CE = Components.rend(); |
| 7509 | auto I = CI; |
| 7510 | |
| 7511 | // Track if the map information being generated is the first for a list of |
| 7512 | // components. |
| 7513 | bool IsExpressionFirstInfo = true; |
| Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7514 | Address BP = Address::invalid(); |
| Patrick Lyster | e13b1e3 | 2019-01-02 19:28:48 +0000 | [diff] [blame] | 7515 | const Expr *AssocExpr = I->getAssociatedExpression(); |
| 7516 | const auto *AE = dyn_cast<ArraySubscriptExpr>(AssocExpr); |
| 7517 | const auto *OASE = dyn_cast<OMPArraySectionExpr>(AssocExpr); |
| Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7518 | |
| Patrick Lyster | e13b1e3 | 2019-01-02 19:28:48 +0000 | [diff] [blame] | 7519 | if (isa<MemberExpr>(AssocExpr)) { |
| Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7520 | // The base is the 'this' pointer. The content of the pointer is going |
| 7521 | // to be the base of the field being mapped. |
| Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7522 | BP = CGF.LoadCXXThisAddress(); |
| Patrick Lyster | e13b1e3 | 2019-01-02 19:28:48 +0000 | [diff] [blame] | 7523 | } else if ((AE && isa<CXXThisExpr>(AE->getBase()->IgnoreParenImpCasts())) || |
| 7524 | (OASE && |
| 7525 | isa<CXXThisExpr>(OASE->getBase()->IgnoreParenImpCasts()))) { |
| Akira Hatanaka | f139ae3 | 2019-12-03 15:17:01 -0800 | [diff] [blame] | 7526 | BP = CGF.EmitOMPSharedLValue(AssocExpr).getAddress(CGF); |
| Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7527 | } else { |
| 7528 | // The base is the reference to the variable. |
| 7529 | // BP = &Var. |
| Akira Hatanaka | f139ae3 | 2019-12-03 15:17:01 -0800 | [diff] [blame] | 7530 | BP = CGF.EmitOMPSharedLValue(AssocExpr).getAddress(CGF); |
| Alexey Bataev | 92327c5 | 2018-03-26 16:40:55 +0000 | [diff] [blame] | 7531 | if (const auto *VD = |
| 7532 | dyn_cast_or_null<VarDecl>(I->getAssociatedDeclaration())) { |
| 7533 | if (llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res = |
| Gheorghe-Teodor Bercea | 0034e84 | 2019-06-20 18:04:47 +0000 | [diff] [blame] | 7534 | OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD)) { |
| 7535 | if ((*Res == OMPDeclareTargetDeclAttr::MT_Link) || |
| 7536 | (*Res == OMPDeclareTargetDeclAttr::MT_To && |
| 7537 | CGF.CGM.getOpenMPRuntime().hasRequiresUnifiedSharedMemory())) { |
| 7538 | RequiresReference = true; |
| 7539 | BP = CGF.CGM.getOpenMPRuntime().getAddrOfDeclareTargetVar(VD); |
| Alexey Bataev | 2c1dffe | 2018-04-16 20:34:41 +0000 | [diff] [blame] | 7540 | } |
| Gheorghe-Teodor Bercea | 0034e84 | 2019-06-20 18:04:47 +0000 | [diff] [blame] | 7541 | } |
| Alexey Bataev | 92327c5 | 2018-03-26 16:40:55 +0000 | [diff] [blame] | 7542 | } |
| Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7543 | |
| 7544 | // If the variable is a pointer and is being dereferenced (i.e. is not |
| Nico Weber | a691689 | 2016-06-10 18:53:04 +0000 | [diff] [blame] | 7545 | // the last component), the base has to be the pointer itself, not its |
| Samuel Antao | 403ffd4 | 2016-07-27 22:49:49 +0000 | [diff] [blame] | 7546 | // reference. References are ignored for mapping purposes. |
| 7547 | QualType Ty = |
| 7548 | I->getAssociatedDeclaration()->getType().getNonReferenceType(); |
| 7549 | if (Ty->isAnyPointerType() && std::next(I) != CE) { |
| Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7550 | BP = CGF.EmitLoadOfPointer(BP, Ty->castAs<PointerType>()); |
| Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7551 | |
| 7552 | // We do not need to generate individual map information for the |
| 7553 | // pointer, it can be associated with the combined storage. |
| 7554 | ++I; |
| 7555 | } |
| 7556 | } |
| 7557 | |
| Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7558 | // Track whether a component of the list should be marked as MEMBER_OF some |
| 7559 | // combined entry (for partial structs). Only the first PTR_AND_OBJ entry |
| 7560 | // in a component list should be marked as MEMBER_OF, all subsequent entries |
| 7561 | // do not belong to the base struct. E.g. |
| 7562 | // struct S2 s; |
| 7563 | // s.ps->ps->ps->f[:] |
| 7564 | // (1) (2) (3) (4) |
| 7565 | // ps(1) is a member pointer, ps(2) is a pointee of ps(1), so it is a |
| 7566 | // PTR_AND_OBJ entry; the PTR is ps(1), so MEMBER_OF the base struct. ps(3) |
| 7567 | // is the pointee of ps(2) which is not member of struct s, so it should not |
| 7568 | // be marked as such (it is still PTR_AND_OBJ). |
| 7569 | // The variable is initialized to false so that PTR_AND_OBJ entries which |
| 7570 | // are not struct members are not considered (e.g. array of pointers to |
| 7571 | // data). |
| 7572 | bool ShouldBeMemberOf = false; |
| 7573 | |
| 7574 | // Variable keeping track of whether or not we have encountered a component |
| 7575 | // in the component list which is a member expression. Useful when we have a |
| 7576 | // pointer or a final array section, in which case it is the previous |
| 7577 | // component in the list which tells us whether we have a member expression. |
| 7578 | // E.g. X.f[:] |
| 7579 | // While processing the final array section "[:]" it is "f" which tells us |
| 7580 | // whether we are dealing with a member of a declared struct. |
| 7581 | const MemberExpr *EncounteredME = nullptr; |
| 7582 | |
| Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7583 | for (; I != CE; ++I) { |
| Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7584 | // If the current component is member of a struct (parent struct) mark it. |
| 7585 | if (!EncounteredME) { |
| 7586 | EncounteredME = dyn_cast<MemberExpr>(I->getAssociatedExpression()); |
| 7587 | // If we encounter a PTR_AND_OBJ entry from now on it should be marked |
| 7588 | // as MEMBER_OF the parent struct. |
| 7589 | if (EncounteredME) |
| 7590 | ShouldBeMemberOf = true; |
| 7591 | } |
| 7592 | |
| Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7593 | auto Next = std::next(I); |
| 7594 | |
| 7595 | // We need to generate the addresses and sizes if this is the last |
| 7596 | // component, if the component is a pointer or if it is an array section |
| 7597 | // whose length can't be proved to be one. If this is a pointer, it |
| 7598 | // becomes the base address for the following components. |
| 7599 | |
| 7600 | // A final array section, is one whose length can't be proved to be one. |
| 7601 | bool IsFinalArraySection = |
| 7602 | isFinalArraySectionExpression(I->getAssociatedExpression()); |
| 7603 | |
| 7604 | // Get information on whether the element is a pointer. Have to do a |
| 7605 | // special treatment for array sections given that they are built-in |
| 7606 | // types. |
| 7607 | const auto *OASE = |
| 7608 | dyn_cast<OMPArraySectionExpr>(I->getAssociatedExpression()); |
| 7609 | bool IsPointer = |
| Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7610 | (OASE && OMPArraySectionExpr::getBaseOriginalType(OASE) |
| 7611 | .getCanonicalType() |
| 7612 | ->isAnyPointerType()) || |
| Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7613 | I->getAssociatedExpression()->getType()->isAnyPointerType(); |
| 7614 | |
| 7615 | if (Next == CE || IsPointer || IsFinalArraySection) { |
| Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7616 | // If this is not the last component, we expect the pointer to be |
| 7617 | // associated with an array expression or member expression. |
| 7618 | assert((Next == CE || |
| 7619 | isa<MemberExpr>(Next->getAssociatedExpression()) || |
| 7620 | isa<ArraySubscriptExpr>(Next->getAssociatedExpression()) || |
| 7621 | isa<OMPArraySectionExpr>(Next->getAssociatedExpression())) && |
| 7622 | "Unexpected expression"); |
| 7623 | |
| Akira Hatanaka | f139ae3 | 2019-12-03 15:17:01 -0800 | [diff] [blame] | 7624 | Address LB = CGF.EmitOMPSharedLValue(I->getAssociatedExpression()) |
| 7625 | .getAddress(CGF); |
| Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7626 | |
| Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7627 | // If this component is a pointer inside the base struct then we don't |
| 7628 | // need to create any entry for it - it will be combined with the object |
| 7629 | // it is pointing to into a single PTR_AND_OBJ entry. |
| 7630 | bool IsMemberPointer = |
| 7631 | IsPointer && EncounteredME && |
| 7632 | (dyn_cast<MemberExpr>(I->getAssociatedExpression()) == |
| 7633 | EncounteredME); |
| Alexey Bataev | e82445f | 2018-09-20 13:54:02 +0000 | [diff] [blame] | 7634 | if (!OverlappedElements.empty()) { |
| 7635 | // Handle base element with the info for overlapped elements. |
| 7636 | assert(!PartialStruct.Base.isValid() && "The base element is set."); |
| 7637 | assert(Next == CE && |
| 7638 | "Expected last element for the overlapped elements."); |
| 7639 | assert(!IsPointer && |
| 7640 | "Unexpected base element with the pointer type."); |
| 7641 | // Mark the whole struct as the struct that requires allocation on the |
| 7642 | // device. |
| 7643 | PartialStruct.LowestElem = {0, LB}; |
| 7644 | CharUnits TypeSize = CGF.getContext().getTypeSizeInChars( |
| 7645 | I->getAssociatedExpression()->getType()); |
| 7646 | Address HB = CGF.Builder.CreateConstGEP( |
| 7647 | CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(LB, |
| 7648 | CGF.VoidPtrTy), |
| James Y Knight | 751fe28 | 2019-02-09 22:22:28 +0000 | [diff] [blame] | 7649 | TypeSize.getQuantity() - 1); |
| Alexey Bataev | e82445f | 2018-09-20 13:54:02 +0000 | [diff] [blame] | 7650 | PartialStruct.HighestElem = { |
| 7651 | std::numeric_limits<decltype( |
| 7652 | PartialStruct.HighestElem.first)>::max(), |
| 7653 | HB}; |
| 7654 | PartialStruct.Base = BP; |
| 7655 | // Emit data for non-overlapped data. |
| 7656 | OpenMPOffloadMappingFlags Flags = |
| 7657 | OMP_MAP_MEMBER_OF | |
| Kelvin Li | ef57943 | 2018-12-18 22:18:41 +0000 | [diff] [blame] | 7658 | getMapTypeBits(MapType, MapModifiers, IsImplicit, |
| Alexey Bataev | e82445f | 2018-09-20 13:54:02 +0000 | [diff] [blame] | 7659 | /*AddPtrFlag=*/false, |
| 7660 | /*AddIsTargetParamFlag=*/false); |
| 7661 | LB = BP; |
| 7662 | llvm::Value *Size = nullptr; |
| 7663 | // Do bitcopy of all non-overlapped structure elements. |
| 7664 | for (OMPClauseMappableExprCommon::MappableExprComponentListRef |
| 7665 | Component : OverlappedElements) { |
| 7666 | Address ComponentLB = Address::invalid(); |
| 7667 | for (const OMPClauseMappableExprCommon::MappableComponent &MC : |
| 7668 | Component) { |
| 7669 | if (MC.getAssociatedDeclaration()) { |
| 7670 | ComponentLB = |
| 7671 | CGF.EmitOMPSharedLValue(MC.getAssociatedExpression()) |
| Akira Hatanaka | f139ae3 | 2019-12-03 15:17:01 -0800 | [diff] [blame] | 7672 | .getAddress(CGF); |
| Alexey Bataev | e82445f | 2018-09-20 13:54:02 +0000 | [diff] [blame] | 7673 | Size = CGF.Builder.CreatePtrDiff( |
| 7674 | CGF.EmitCastToVoidPtr(ComponentLB.getPointer()), |
| 7675 | CGF.EmitCastToVoidPtr(LB.getPointer())); |
| 7676 | break; |
| 7677 | } |
| 7678 | } |
| 7679 | BasePointers.push_back(BP.getPointer()); |
| 7680 | Pointers.push_back(LB.getPointer()); |
| Alexey Bataev | a90fc66 | 2019-06-25 16:00:43 +0000 | [diff] [blame] | 7681 | Sizes.push_back(CGF.Builder.CreateIntCast(Size, CGF.Int64Ty, |
| 7682 | /*isSigned=*/true)); |
| Alexey Bataev | e82445f | 2018-09-20 13:54:02 +0000 | [diff] [blame] | 7683 | Types.push_back(Flags); |
| James Y Knight | 751fe28 | 2019-02-09 22:22:28 +0000 | [diff] [blame] | 7684 | LB = CGF.Builder.CreateConstGEP(ComponentLB, 1); |
| Alexey Bataev | e82445f | 2018-09-20 13:54:02 +0000 | [diff] [blame] | 7685 | } |
| 7686 | BasePointers.push_back(BP.getPointer()); |
| 7687 | Pointers.push_back(LB.getPointer()); |
| 7688 | Size = CGF.Builder.CreatePtrDiff( |
| 7689 | CGF.EmitCastToVoidPtr( |
| James Y Knight | 751fe28 | 2019-02-09 22:22:28 +0000 | [diff] [blame] | 7690 | CGF.Builder.CreateConstGEP(HB, 1).getPointer()), |
| Alexey Bataev | e82445f | 2018-09-20 13:54:02 +0000 | [diff] [blame] | 7691 | CGF.EmitCastToVoidPtr(LB.getPointer())); |
| Alexey Bataev | a90fc66 | 2019-06-25 16:00:43 +0000 | [diff] [blame] | 7692 | Sizes.push_back( |
| 7693 | CGF.Builder.CreateIntCast(Size, CGF.Int64Ty, /*isSigned=*/true)); |
| Alexey Bataev | e82445f | 2018-09-20 13:54:02 +0000 | [diff] [blame] | 7694 | Types.push_back(Flags); |
| 7695 | break; |
| 7696 | } |
| 7697 | llvm::Value *Size = getExprTypeSize(I->getAssociatedExpression()); |
| Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7698 | if (!IsMemberPointer) { |
| 7699 | BasePointers.push_back(BP.getPointer()); |
| 7700 | Pointers.push_back(LB.getPointer()); |
| Alexey Bataev | a90fc66 | 2019-06-25 16:00:43 +0000 | [diff] [blame] | 7701 | Sizes.push_back( |
| 7702 | CGF.Builder.CreateIntCast(Size, CGF.Int64Ty, /*isSigned=*/true)); |
| Samuel Antao | 03a3cec | 2016-07-27 22:52:16 +0000 | [diff] [blame] | 7703 | |
| Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7704 | // We need to add a pointer flag for each map that comes from the |
| 7705 | // same expression except for the first one. We also need to signal |
| 7706 | // this map is the first one that relates with the current capture |
| 7707 | // (there is a set of entries for each capture). |
| 7708 | OpenMPOffloadMappingFlags Flags = getMapTypeBits( |
| Kelvin Li | ef57943 | 2018-12-18 22:18:41 +0000 | [diff] [blame] | 7709 | MapType, MapModifiers, IsImplicit, |
| Gheorghe-Teodor Bercea | 0034e84 | 2019-06-20 18:04:47 +0000 | [diff] [blame] | 7710 | !IsExpressionFirstInfo || RequiresReference, |
| 7711 | IsCaptureFirstInfo && !RequiresReference); |
| Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7712 | |
| 7713 | if (!IsExpressionFirstInfo) { |
| 7714 | // If we have a PTR_AND_OBJ pair where the OBJ is a pointer as well, |
| Gheorghe-Teodor Bercea | 0fd073b | 2019-08-09 21:42:13 +0000 | [diff] [blame] | 7715 | // then we reset the TO/FROM/ALWAYS/DELETE/CLOSE flags. |
| Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7716 | if (IsPointer) |
| 7717 | Flags &= ~(OMP_MAP_TO | OMP_MAP_FROM | OMP_MAP_ALWAYS | |
| Gheorghe-Teodor Bercea | 0fd073b | 2019-08-09 21:42:13 +0000 | [diff] [blame] | 7718 | OMP_MAP_DELETE | OMP_MAP_CLOSE); |
| Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7719 | |
| 7720 | if (ShouldBeMemberOf) { |
| 7721 | // Set placeholder value MEMBER_OF=FFFF to indicate that the flag |
| 7722 | // should be later updated with the correct value of MEMBER_OF. |
| 7723 | Flags |= OMP_MAP_MEMBER_OF; |
| 7724 | // From now on, all subsequent PTR_AND_OBJ entries should not be |
| 7725 | // marked as MEMBER_OF. |
| 7726 | ShouldBeMemberOf = false; |
| 7727 | } |
| 7728 | } |
| 7729 | |
| 7730 | Types.push_back(Flags); |
| Samuel Antao | 03a3cec | 2016-07-27 22:52:16 +0000 | [diff] [blame] | 7731 | } |
| 7732 | |
| Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7733 | // If we have encountered a member expression so far, keep track of the |
| 7734 | // mapped member. If the parent is "*this", then the value declaration |
| 7735 | // is nullptr. |
| 7736 | if (EncounteredME) { |
| 7737 | const auto *FD = dyn_cast<FieldDecl>(EncounteredME->getMemberDecl()); |
| 7738 | unsigned FieldIndex = FD->getFieldIndex(); |
| Samuel Antao | 03a3cec | 2016-07-27 22:52:16 +0000 | [diff] [blame] | 7739 | |
| Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7740 | // Update info about the lowest and highest elements for this struct |
| 7741 | if (!PartialStruct.Base.isValid()) { |
| 7742 | PartialStruct.LowestElem = {FieldIndex, LB}; |
| 7743 | PartialStruct.HighestElem = {FieldIndex, LB}; |
| 7744 | PartialStruct.Base = BP; |
| 7745 | } else if (FieldIndex < PartialStruct.LowestElem.first) { |
| 7746 | PartialStruct.LowestElem = {FieldIndex, LB}; |
| 7747 | } else if (FieldIndex > PartialStruct.HighestElem.first) { |
| 7748 | PartialStruct.HighestElem = {FieldIndex, LB}; |
| 7749 | } |
| 7750 | } |
| Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7751 | |
| 7752 | // If we have a final array section, we are done with this expression. |
| 7753 | if (IsFinalArraySection) |
| 7754 | break; |
| 7755 | |
| 7756 | // The pointer becomes the base for the next element. |
| 7757 | if (Next != CE) |
| 7758 | BP = LB; |
| 7759 | |
| 7760 | IsExpressionFirstInfo = false; |
| 7761 | IsCaptureFirstInfo = false; |
| Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7762 | } |
| 7763 | } |
| 7764 | } |
| 7765 | |
| Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7766 | /// Return the adjusted map modifiers if the declaration a capture refers to |
| 7767 | /// appears in a first-private clause. This is expected to be used only with |
| 7768 | /// directives that start with 'target'. |
| 7769 | MappableExprsHandler::OpenMPOffloadMappingFlags |
| 7770 | getMapModifiersForPrivateClauses(const CapturedStmt::Capture &Cap) const { |
| 7771 | assert(Cap.capturesVariable() && "Expected capture by reference only!"); |
| 7772 | |
| 7773 | // A first private variable captured by reference will use only the |
| 7774 | // 'private ptr' and 'map to' flag. Return the right flags if the captured |
| 7775 | // declaration is known as first-private in this handler. |
| Alexey Bataev | 1af5bd5 | 2019-03-05 17:47:18 +0000 | [diff] [blame] | 7776 | if (FirstPrivateDecls.count(Cap.getCapturedVar())) { |
| 7777 | if (Cap.getCapturedVar()->getType().isConstant(CGF.getContext()) && |
| 7778 | Cap.getCaptureKind() == CapturedStmt::VCK_ByRef) |
| 7779 | return MappableExprsHandler::OMP_MAP_ALWAYS | |
| 7780 | MappableExprsHandler::OMP_MAP_TO; |
| Alexey Bataev | c2b831f | 2019-04-02 16:03:40 +0000 | [diff] [blame] | 7781 | if (Cap.getCapturedVar()->getType()->isAnyPointerType()) |
| 7782 | return MappableExprsHandler::OMP_MAP_TO | |
| 7783 | MappableExprsHandler::OMP_MAP_PTR_AND_OBJ; |
| Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7784 | return MappableExprsHandler::OMP_MAP_PRIVATE | |
| 7785 | MappableExprsHandler::OMP_MAP_TO; |
| Alexey Bataev | 1af5bd5 | 2019-03-05 17:47:18 +0000 | [diff] [blame] | 7786 | } |
| Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7787 | return MappableExprsHandler::OMP_MAP_TO | |
| 7788 | MappableExprsHandler::OMP_MAP_FROM; |
| 7789 | } |
| 7790 | |
| 7791 | static OpenMPOffloadMappingFlags getMemberOfFlag(unsigned Position) { |
| Michael Kruse | d47b943 | 2019-08-05 18:43:21 +0000 | [diff] [blame] | 7792 | // Rotate by getFlagMemberOffset() bits. |
| Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7793 | return static_cast<OpenMPOffloadMappingFlags>(((uint64_t)Position + 1) |
| Michael Kruse | d47b943 | 2019-08-05 18:43:21 +0000 | [diff] [blame] | 7794 | << getFlagMemberOffset()); |
| Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7795 | } |
| 7796 | |
| 7797 | static void setCorrectMemberOfFlag(OpenMPOffloadMappingFlags &Flags, |
| 7798 | OpenMPOffloadMappingFlags MemberOfFlag) { |
| 7799 | // If the entry is PTR_AND_OBJ but has not been marked with the special |
| 7800 | // placeholder value 0xFFFF in the MEMBER_OF field, then it should not be |
| 7801 | // marked as MEMBER_OF. |
| 7802 | if ((Flags & OMP_MAP_PTR_AND_OBJ) && |
| 7803 | ((Flags & OMP_MAP_MEMBER_OF) != OMP_MAP_MEMBER_OF)) |
| 7804 | return; |
| 7805 | |
| 7806 | // Reset the placeholder value to prepare the flag for the assignment of the |
| 7807 | // proper MEMBER_OF value. |
| 7808 | Flags &= ~OMP_MAP_MEMBER_OF; |
| 7809 | Flags |= MemberOfFlag; |
| 7810 | } |
| 7811 | |
| Alexey Bataev | e82445f | 2018-09-20 13:54:02 +0000 | [diff] [blame] | 7812 | void getPlainLayout(const CXXRecordDecl *RD, |
| 7813 | llvm::SmallVectorImpl<const FieldDecl *> &Layout, |
| 7814 | bool AsBase) const { |
| 7815 | const CGRecordLayout &RL = CGF.getTypes().getCGRecordLayout(RD); |
| 7816 | |
| 7817 | llvm::StructType *St = |
| 7818 | AsBase ? RL.getBaseSubobjectLLVMType() : RL.getLLVMType(); |
| 7819 | |
| 7820 | unsigned NumElements = St->getNumElements(); |
| 7821 | llvm::SmallVector< |
| 7822 | llvm::PointerUnion<const CXXRecordDecl *, const FieldDecl *>, 4> |
| 7823 | RecordLayout(NumElements); |
| 7824 | |
| 7825 | // Fill bases. |
| 7826 | for (const auto &I : RD->bases()) { |
| 7827 | if (I.isVirtual()) |
| 7828 | continue; |
| 7829 | const auto *Base = I.getType()->getAsCXXRecordDecl(); |
| 7830 | // Ignore empty bases. |
| 7831 | if (Base->isEmpty() || CGF.getContext() |
| 7832 | .getASTRecordLayout(Base) |
| 7833 | .getNonVirtualSize() |
| 7834 | .isZero()) |
| 7835 | continue; |
| 7836 | |
| 7837 | unsigned FieldIndex = RL.getNonVirtualBaseLLVMFieldNo(Base); |
| 7838 | RecordLayout[FieldIndex] = Base; |
| 7839 | } |
| 7840 | // Fill in virtual bases. |
| 7841 | for (const auto &I : RD->vbases()) { |
| 7842 | const auto *Base = I.getType()->getAsCXXRecordDecl(); |
| 7843 | // Ignore empty bases. |
| 7844 | if (Base->isEmpty()) |
| 7845 | continue; |
| 7846 | unsigned FieldIndex = RL.getVirtualBaseIndex(Base); |
| 7847 | if (RecordLayout[FieldIndex]) |
| 7848 | continue; |
| 7849 | RecordLayout[FieldIndex] = Base; |
| 7850 | } |
| 7851 | // Fill in all the fields. |
| 7852 | assert(!RD->isUnion() && "Unexpected union."); |
| 7853 | for (const auto *Field : RD->fields()) { |
| 7854 | // Fill in non-bitfields. (Bitfields always use a zero pattern, which we |
| 7855 | // will fill in later.) |
| Richard Smith | 78b239e | 2019-06-20 20:44:45 +0000 | [diff] [blame] | 7856 | if (!Field->isBitField() && !Field->isZeroSize(CGF.getContext())) { |
| Alexey Bataev | e82445f | 2018-09-20 13:54:02 +0000 | [diff] [blame] | 7857 | unsigned FieldIndex = RL.getLLVMFieldNo(Field); |
| 7858 | RecordLayout[FieldIndex] = Field; |
| 7859 | } |
| 7860 | } |
| 7861 | for (const llvm::PointerUnion<const CXXRecordDecl *, const FieldDecl *> |
| 7862 | &Data : RecordLayout) { |
| 7863 | if (Data.isNull()) |
| 7864 | continue; |
| 7865 | if (const auto *Base = Data.dyn_cast<const CXXRecordDecl *>()) |
| 7866 | getPlainLayout(Base, Layout, /*AsBase=*/true); |
| 7867 | else |
| 7868 | Layout.push_back(Data.get<const FieldDecl *>()); |
| 7869 | } |
| 7870 | } |
| 7871 | |
| Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7872 | public: |
| 7873 | MappableExprsHandler(const OMPExecutableDirective &Dir, CodeGenFunction &CGF) |
| Michael Kruse | d47b943 | 2019-08-05 18:43:21 +0000 | [diff] [blame] | 7874 | : CurDir(&Dir), CGF(CGF) { |
| Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7875 | // Extract firstprivate clause information. |
| 7876 | for (const auto *C : Dir.getClausesOfKind<OMPFirstprivateClause>()) |
| 7877 | for (const auto *D : C->varlists()) |
| Alexey Bataev | f288cf9 | 2019-06-27 18:53:07 +0000 | [diff] [blame] | 7878 | FirstPrivateDecls.try_emplace( |
| 7879 | cast<VarDecl>(cast<DeclRefExpr>(D)->getDecl()), C->isImplicit()); |
| Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7880 | // Extract device pointer clause information. |
| 7881 | for (const auto *C : Dir.getClausesOfKind<OMPIsDevicePtrClause>()) |
| 7882 | for (auto L : C->component_lists()) |
| 7883 | DevPointersMap[L.first].push_back(L.second); |
| 7884 | } |
| 7885 | |
| Michael Kruse | d47b943 | 2019-08-05 18:43:21 +0000 | [diff] [blame] | 7886 | /// Constructor for the declare mapper directive. |
| 7887 | MappableExprsHandler(const OMPDeclareMapperDecl &Dir, CodeGenFunction &CGF) |
| 7888 | : CurDir(&Dir), CGF(CGF) {} |
| 7889 | |
| Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7890 | /// Generate code for the combined entry if we have a partially mapped struct |
| 7891 | /// and take care of the mapping flags of the arguments corresponding to |
| 7892 | /// individual struct members. |
| 7893 | void emitCombinedEntry(MapBaseValuesArrayTy &BasePointers, |
| 7894 | MapValuesArrayTy &Pointers, MapValuesArrayTy &Sizes, |
| 7895 | MapFlagsArrayTy &Types, MapFlagsArrayTy &CurTypes, |
| 7896 | const StructRangeInfoTy &PartialStruct) const { |
| 7897 | // Base is the base of the struct |
| 7898 | BasePointers.push_back(PartialStruct.Base.getPointer()); |
| 7899 | // Pointer is the address of the lowest element |
| 7900 | llvm::Value *LB = PartialStruct.LowestElem.second.getPointer(); |
| 7901 | Pointers.push_back(LB); |
| 7902 | // Size is (addr of {highest+1} element) - (addr of lowest element) |
| 7903 | llvm::Value *HB = PartialStruct.HighestElem.second.getPointer(); |
| 7904 | llvm::Value *HAddr = CGF.Builder.CreateConstGEP1_32(HB, /*Idx0=*/1); |
| 7905 | llvm::Value *CLAddr = CGF.Builder.CreatePointerCast(LB, CGF.VoidPtrTy); |
| 7906 | llvm::Value *CHAddr = CGF.Builder.CreatePointerCast(HAddr, CGF.VoidPtrTy); |
| 7907 | llvm::Value *Diff = CGF.Builder.CreatePtrDiff(CHAddr, CLAddr); |
| Alexey Bataev | a90fc66 | 2019-06-25 16:00:43 +0000 | [diff] [blame] | 7908 | llvm::Value *Size = CGF.Builder.CreateIntCast(Diff, CGF.Int64Ty, |
| Rui Ueyama | 49a3ad2 | 2019-07-16 04:46:31 +0000 | [diff] [blame] | 7909 | /*isSigned=*/false); |
| Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7910 | Sizes.push_back(Size); |
| 7911 | // Map type is always TARGET_PARAM |
| 7912 | Types.push_back(OMP_MAP_TARGET_PARAM); |
| 7913 | // Remove TARGET_PARAM flag from the first element |
| 7914 | (*CurTypes.begin()) &= ~OMP_MAP_TARGET_PARAM; |
| 7915 | |
| 7916 | // All other current entries will be MEMBER_OF the combined entry |
| 7917 | // (except for PTR_AND_OBJ entries which do not have a placeholder value |
| 7918 | // 0xFFFF in the MEMBER_OF field). |
| 7919 | OpenMPOffloadMappingFlags MemberOfFlag = |
| 7920 | getMemberOfFlag(BasePointers.size() - 1); |
| 7921 | for (auto &M : CurTypes) |
| 7922 | setCorrectMemberOfFlag(M, MemberOfFlag); |
| 7923 | } |
| 7924 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 7925 | /// Generate all the base pointers, section pointers, sizes and map |
| Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7926 | /// types for the extracted mappable expressions. Also, for each item that |
| 7927 | /// relates with a device pointer, a pair of the relevant declaration and |
| 7928 | /// index where it occurs is appended to the device pointers info array. |
| 7929 | void generateAllInfo(MapBaseValuesArrayTy &BasePointers, |
| Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7930 | MapValuesArrayTy &Pointers, MapValuesArrayTy &Sizes, |
| 7931 | MapFlagsArrayTy &Types) const { |
| Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7932 | // We have to process the component lists that relate with the same |
| 7933 | // declaration in a single chunk so that we can generate the map flags |
| 7934 | // correctly. Therefore, we organize all lists in a map. |
| Alexey Bataev | 5d1c3f6 | 2017-06-27 15:46:42 +0000 | [diff] [blame] | 7935 | llvm::MapVector<const ValueDecl *, SmallVector<MapInfo, 8>> Info; |
| Samuel Antao | 8d2d730 | 2016-05-26 18:30:22 +0000 | [diff] [blame] | 7936 | |
| 7937 | // Helper function to fill the information map for the different supported |
| 7938 | // clauses. |
| Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7939 | auto &&InfoGen = [&Info]( |
| 7940 | const ValueDecl *D, |
| 7941 | OMPClauseMappableExprCommon::MappableExprComponentListRef L, |
| Kelvin Li | ef57943 | 2018-12-18 22:18:41 +0000 | [diff] [blame] | 7942 | OpenMPMapClauseKind MapType, |
| 7943 | ArrayRef<OpenMPMapModifierKind> MapModifiers, |
| Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7944 | bool ReturnDevicePointer, bool IsImplicit) { |
| Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7945 | const ValueDecl *VD = |
| 7946 | D ? cast<ValueDecl>(D->getCanonicalDecl()) : nullptr; |
| Kelvin Li | ef57943 | 2018-12-18 22:18:41 +0000 | [diff] [blame] | 7947 | Info[VD].emplace_back(L, MapType, MapModifiers, ReturnDevicePointer, |
| Alexey Bataev | f47c4b4 | 2017-09-26 13:47:31 +0000 | [diff] [blame] | 7948 | IsImplicit); |
| Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7949 | }; |
| Samuel Antao | 8d2d730 | 2016-05-26 18:30:22 +0000 | [diff] [blame] | 7950 | |
| Michael Kruse | d47b943 | 2019-08-05 18:43:21 +0000 | [diff] [blame] | 7951 | assert(CurDir.is<const OMPExecutableDirective *>() && |
| 7952 | "Expect a executable directive"); |
| 7953 | const auto *CurExecDir = CurDir.get<const OMPExecutableDirective *>(); |
| 7954 | for (const auto *C : CurExecDir->getClausesOfKind<OMPMapClause>()) |
| Mark de Wever | 51abceb | 2019-11-12 20:48:11 +0100 | [diff] [blame] | 7955 | for (const auto L : C->component_lists()) { |
| Kelvin Li | ef57943 | 2018-12-18 22:18:41 +0000 | [diff] [blame] | 7956 | InfoGen(L.first, L.second, C->getMapType(), C->getMapTypeModifiers(), |
| Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7957 | /*ReturnDevicePointer=*/false, C->isImplicit()); |
| Alexey Bataev | f47c4b4 | 2017-09-26 13:47:31 +0000 | [diff] [blame] | 7958 | } |
| Michael Kruse | d47b943 | 2019-08-05 18:43:21 +0000 | [diff] [blame] | 7959 | for (const auto *C : CurExecDir->getClausesOfKind<OMPToClause>()) |
| Mark de Wever | 51abceb | 2019-11-12 20:48:11 +0100 | [diff] [blame] | 7960 | for (const auto L : C->component_lists()) { |
| Kelvin Li | ef57943 | 2018-12-18 22:18:41 +0000 | [diff] [blame] | 7961 | InfoGen(L.first, L.second, OMPC_MAP_to, llvm::None, |
| Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7962 | /*ReturnDevicePointer=*/false, C->isImplicit()); |
| Alexey Bataev | f47c4b4 | 2017-09-26 13:47:31 +0000 | [diff] [blame] | 7963 | } |
| Michael Kruse | d47b943 | 2019-08-05 18:43:21 +0000 | [diff] [blame] | 7964 | for (const auto *C : CurExecDir->getClausesOfKind<OMPFromClause>()) |
| Mark de Wever | 51abceb | 2019-11-12 20:48:11 +0100 | [diff] [blame] | 7965 | for (const auto L : C->component_lists()) { |
| Kelvin Li | ef57943 | 2018-12-18 22:18:41 +0000 | [diff] [blame] | 7966 | InfoGen(L.first, L.second, OMPC_MAP_from, llvm::None, |
| Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7967 | /*ReturnDevicePointer=*/false, C->isImplicit()); |
| Alexey Bataev | f47c4b4 | 2017-09-26 13:47:31 +0000 | [diff] [blame] | 7968 | } |
| Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7969 | |
| Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7970 | // Look at the use_device_ptr clause information and mark the existing map |
| 7971 | // entries as such. If there is no map information for an entry in the |
| 7972 | // use_device_ptr list, we create one with map type 'alloc' and zero size |
| Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7973 | // section. It is the user fault if that was not mapped before. If there is |
| 7974 | // no map information and the pointer is a struct member, then we defer the |
| 7975 | // emission of that entry until the whole struct has been processed. |
| 7976 | llvm::MapVector<const ValueDecl *, SmallVector<DeferredDevicePtrEntryTy, 4>> |
| 7977 | DeferredInfo; |
| 7978 | |
| Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7979 | for (const auto *C : |
| Michael Kruse | d47b943 | 2019-08-05 18:43:21 +0000 | [diff] [blame] | 7980 | CurExecDir->getClausesOfKind<OMPUseDevicePtrClause>()) { |
| Mark de Wever | 51abceb | 2019-11-12 20:48:11 +0100 | [diff] [blame] | 7981 | for (const auto L : C->component_lists()) { |
| Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7982 | assert(!L.second.empty() && "Not expecting empty list of components!"); |
| 7983 | const ValueDecl *VD = L.second.back().getAssociatedDeclaration(); |
| 7984 | VD = cast<ValueDecl>(VD->getCanonicalDecl()); |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 7985 | const Expr *IE = L.second.back().getAssociatedExpression(); |
| Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7986 | // If the first component is a member expression, we have to look into |
| 7987 | // 'this', which maps to null in the map of map information. Otherwise |
| 7988 | // look directly for the information. |
| 7989 | auto It = Info.find(isa<MemberExpr>(IE) ? nullptr : VD); |
| 7990 | |
| 7991 | // We potentially have map information for this declaration already. |
| 7992 | // Look for the first set of components that refer to it. |
| 7993 | if (It != Info.end()) { |
| 7994 | auto CI = std::find_if( |
| 7995 | It->second.begin(), It->second.end(), [VD](const MapInfo &MI) { |
| 7996 | return MI.Components.back().getAssociatedDeclaration() == VD; |
| 7997 | }); |
| 7998 | // If we found a map entry, signal that the pointer has to be returned |
| 7999 | // and move on to the next declaration. |
| 8000 | if (CI != It->second.end()) { |
| Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 8001 | CI->ReturnDevicePointer = true; |
| Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 8002 | continue; |
| 8003 | } |
| 8004 | } |
| 8005 | |
| 8006 | // We didn't find any match in our map information - generate a zero |
| Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 8007 | // size array section - if the pointer is a struct member we defer this |
| 8008 | // action until the whole struct has been processed. |
| Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 8009 | if (isa<MemberExpr>(IE)) { |
| 8010 | // Insert the pointer into Info to be processed by |
| 8011 | // generateInfoForComponentList. Because it is a member pointer |
| 8012 | // without a pointee, no entry will be generated for it, therefore |
| 8013 | // we need to generate one after the whole struct has been processed. |
| 8014 | // Nonetheless, generateInfoForComponentList must be called to take |
| 8015 | // the pointer into account for the calculation of the range of the |
| 8016 | // partial struct. |
| Kelvin Li | ef57943 | 2018-12-18 22:18:41 +0000 | [diff] [blame] | 8017 | InfoGen(nullptr, L.second, OMPC_MAP_unknown, llvm::None, |
| Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 8018 | /*ReturnDevicePointer=*/false, C->isImplicit()); |
| 8019 | DeferredInfo[nullptr].emplace_back(IE, VD); |
| 8020 | } else { |
| Michael Kruse | d47b943 | 2019-08-05 18:43:21 +0000 | [diff] [blame] | 8021 | llvm::Value *Ptr = |
| 8022 | CGF.EmitLoadOfScalar(CGF.EmitLValue(IE), IE->getExprLoc()); |
| Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 8023 | BasePointers.emplace_back(Ptr, VD); |
| 8024 | Pointers.push_back(Ptr); |
| Michael Kruse | d47b943 | 2019-08-05 18:43:21 +0000 | [diff] [blame] | 8025 | Sizes.push_back(llvm::Constant::getNullValue(CGF.Int64Ty)); |
| Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 8026 | Types.push_back(OMP_MAP_RETURN_PARAM | OMP_MAP_TARGET_PARAM); |
| 8027 | } |
| Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 8028 | } |
| Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 8029 | } |
| Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 8030 | |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8031 | for (const auto &M : Info) { |
| Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 8032 | // We need to know when we generate information for the first component |
| 8033 | // associated with a capture, because the mapping flags depend on it. |
| 8034 | bool IsFirstComponentList = true; |
| Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 8035 | |
| 8036 | // Temporary versions of arrays |
| 8037 | MapBaseValuesArrayTy CurBasePointers; |
| 8038 | MapValuesArrayTy CurPointers; |
| 8039 | MapValuesArrayTy CurSizes; |
| 8040 | MapFlagsArrayTy CurTypes; |
| 8041 | StructRangeInfoTy PartialStruct; |
| 8042 | |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8043 | for (const MapInfo &L : M.second) { |
| Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 8044 | assert(!L.Components.empty() && |
| 8045 | "Not expecting declaration with no component lists."); |
| Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 8046 | |
| 8047 | // Remember the current base pointer index. |
| Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 8048 | unsigned CurrentBasePointersIdx = CurBasePointers.size(); |
| Michael Kruse | d47b943 | 2019-08-05 18:43:21 +0000 | [diff] [blame] | 8049 | generateInfoForComponentList(L.MapType, L.MapModifiers, L.Components, |
| 8050 | CurBasePointers, CurPointers, CurSizes, |
| 8051 | CurTypes, PartialStruct, |
| 8052 | IsFirstComponentList, L.IsImplicit); |
| Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 8053 | |
| 8054 | // If this entry relates with a device pointer, set the relevant |
| 8055 | // declaration and add the 'return pointer' flag. |
| Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 8056 | if (L.ReturnDevicePointer) { |
| 8057 | assert(CurBasePointers.size() > CurrentBasePointersIdx && |
| Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 8058 | "Unexpected number of mapped base pointers."); |
| 8059 | |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8060 | const ValueDecl *RelevantVD = |
| 8061 | L.Components.back().getAssociatedDeclaration(); |
| Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 8062 | assert(RelevantVD && |
| 8063 | "No relevant declaration related with device pointer??"); |
| 8064 | |
| Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 8065 | CurBasePointers[CurrentBasePointersIdx].setDevicePtrDecl(RelevantVD); |
| 8066 | CurTypes[CurrentBasePointersIdx] |= OMP_MAP_RETURN_PARAM; |
| Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 8067 | } |
| Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 8068 | IsFirstComponentList = false; |
| 8069 | } |
| Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 8070 | |
| 8071 | // Append any pending zero-length pointers which are struct members and |
| 8072 | // used with use_device_ptr. |
| 8073 | auto CI = DeferredInfo.find(M.first); |
| 8074 | if (CI != DeferredInfo.end()) { |
| 8075 | for (const DeferredDevicePtrEntryTy &L : CI->second) { |
| Akira Hatanaka | f139ae3 | 2019-12-03 15:17:01 -0800 | [diff] [blame] | 8076 | llvm::Value *BasePtr = this->CGF.EmitLValue(L.IE).getPointer(CGF); |
| Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 8077 | llvm::Value *Ptr = this->CGF.EmitLoadOfScalar( |
| 8078 | this->CGF.EmitLValue(L.IE), L.IE->getExprLoc()); |
| 8079 | CurBasePointers.emplace_back(BasePtr, L.VD); |
| 8080 | CurPointers.push_back(Ptr); |
| Alexey Bataev | a90fc66 | 2019-06-25 16:00:43 +0000 | [diff] [blame] | 8081 | CurSizes.push_back(llvm::Constant::getNullValue(this->CGF.Int64Ty)); |
| Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 8082 | // Entry is PTR_AND_OBJ and RETURN_PARAM. Also, set the placeholder |
| 8083 | // value MEMBER_OF=FFFF so that the entry is later updated with the |
| 8084 | // correct value of MEMBER_OF. |
| 8085 | CurTypes.push_back(OMP_MAP_PTR_AND_OBJ | OMP_MAP_RETURN_PARAM | |
| 8086 | OMP_MAP_MEMBER_OF); |
| 8087 | } |
| 8088 | } |
| 8089 | |
| 8090 | // If there is an entry in PartialStruct it means we have a struct with |
| 8091 | // individual members mapped. Emit an extra combined entry. |
| 8092 | if (PartialStruct.Base.isValid()) |
| 8093 | emitCombinedEntry(BasePointers, Pointers, Sizes, Types, CurTypes, |
| 8094 | PartialStruct); |
| 8095 | |
| 8096 | // We need to append the results of this capture to what we already have. |
| 8097 | BasePointers.append(CurBasePointers.begin(), CurBasePointers.end()); |
| 8098 | Pointers.append(CurPointers.begin(), CurPointers.end()); |
| 8099 | Sizes.append(CurSizes.begin(), CurSizes.end()); |
| 8100 | Types.append(CurTypes.begin(), CurTypes.end()); |
| Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 8101 | } |
| 8102 | } |
| 8103 | |
| Michael Kruse | d47b943 | 2019-08-05 18:43:21 +0000 | [diff] [blame] | 8104 | /// Generate all the base pointers, section pointers, sizes and map types for |
| 8105 | /// the extracted map clauses of user-defined mapper. |
| 8106 | void generateAllInfoForMapper(MapBaseValuesArrayTy &BasePointers, |
| 8107 | MapValuesArrayTy &Pointers, |
| 8108 | MapValuesArrayTy &Sizes, |
| 8109 | MapFlagsArrayTy &Types) const { |
| 8110 | assert(CurDir.is<const OMPDeclareMapperDecl *>() && |
| 8111 | "Expect a declare mapper directive"); |
| 8112 | const auto *CurMapperDir = CurDir.get<const OMPDeclareMapperDecl *>(); |
| 8113 | // We have to process the component lists that relate with the same |
| 8114 | // declaration in a single chunk so that we can generate the map flags |
| 8115 | // correctly. Therefore, we organize all lists in a map. |
| 8116 | llvm::MapVector<const ValueDecl *, SmallVector<MapInfo, 8>> Info; |
| 8117 | |
| 8118 | // Helper function to fill the information map for the different supported |
| 8119 | // clauses. |
| 8120 | auto &&InfoGen = [&Info]( |
| 8121 | const ValueDecl *D, |
| 8122 | OMPClauseMappableExprCommon::MappableExprComponentListRef L, |
| 8123 | OpenMPMapClauseKind MapType, |
| 8124 | ArrayRef<OpenMPMapModifierKind> MapModifiers, |
| 8125 | bool ReturnDevicePointer, bool IsImplicit) { |
| 8126 | const ValueDecl *VD = |
| 8127 | D ? cast<ValueDecl>(D->getCanonicalDecl()) : nullptr; |
| 8128 | Info[VD].emplace_back(L, MapType, MapModifiers, ReturnDevicePointer, |
| 8129 | IsImplicit); |
| 8130 | }; |
| 8131 | |
| 8132 | for (const auto *C : CurMapperDir->clauselists()) { |
| 8133 | const auto *MC = cast<OMPMapClause>(C); |
| Mark de Wever | 51abceb | 2019-11-12 20:48:11 +0100 | [diff] [blame] | 8134 | for (const auto L : MC->component_lists()) { |
| Michael Kruse | d47b943 | 2019-08-05 18:43:21 +0000 | [diff] [blame] | 8135 | InfoGen(L.first, L.second, MC->getMapType(), MC->getMapTypeModifiers(), |
| 8136 | /*ReturnDevicePointer=*/false, MC->isImplicit()); |
| 8137 | } |
| 8138 | } |
| 8139 | |
| 8140 | for (const auto &M : Info) { |
| 8141 | // We need to know when we generate information for the first component |
| 8142 | // associated with a capture, because the mapping flags depend on it. |
| 8143 | bool IsFirstComponentList = true; |
| 8144 | |
| 8145 | // Temporary versions of arrays |
| 8146 | MapBaseValuesArrayTy CurBasePointers; |
| 8147 | MapValuesArrayTy CurPointers; |
| 8148 | MapValuesArrayTy CurSizes; |
| 8149 | MapFlagsArrayTy CurTypes; |
| 8150 | StructRangeInfoTy PartialStruct; |
| 8151 | |
| 8152 | for (const MapInfo &L : M.second) { |
| 8153 | assert(!L.Components.empty() && |
| 8154 | "Not expecting declaration with no component lists."); |
| 8155 | generateInfoForComponentList(L.MapType, L.MapModifiers, L.Components, |
| 8156 | CurBasePointers, CurPointers, CurSizes, |
| 8157 | CurTypes, PartialStruct, |
| 8158 | IsFirstComponentList, L.IsImplicit); |
| 8159 | IsFirstComponentList = false; |
| 8160 | } |
| 8161 | |
| 8162 | // If there is an entry in PartialStruct it means we have a struct with |
| 8163 | // individual members mapped. Emit an extra combined entry. |
| 8164 | if (PartialStruct.Base.isValid()) |
| 8165 | emitCombinedEntry(BasePointers, Pointers, Sizes, Types, CurTypes, |
| 8166 | PartialStruct); |
| 8167 | |
| 8168 | // We need to append the results of this capture to what we already have. |
| 8169 | BasePointers.append(CurBasePointers.begin(), CurBasePointers.end()); |
| 8170 | Pointers.append(CurPointers.begin(), CurPointers.end()); |
| 8171 | Sizes.append(CurSizes.begin(), CurSizes.end()); |
| 8172 | Types.append(CurTypes.begin(), CurTypes.end()); |
| 8173 | } |
| 8174 | } |
| 8175 | |
| Alexey Bataev | 6070542 | 2018-10-30 15:50:12 +0000 | [diff] [blame] | 8176 | /// Emit capture info for lambdas for variables captured by reference. |
| Alexey Bataev | 969dbc0 | 2018-11-08 15:47:39 +0000 | [diff] [blame] | 8177 | void generateInfoForLambdaCaptures( |
| 8178 | const ValueDecl *VD, llvm::Value *Arg, MapBaseValuesArrayTy &BasePointers, |
| 8179 | MapValuesArrayTy &Pointers, MapValuesArrayTy &Sizes, |
| 8180 | MapFlagsArrayTy &Types, |
| 8181 | llvm::DenseMap<llvm::Value *, llvm::Value *> &LambdaPointers) const { |
| Alexey Bataev | 6070542 | 2018-10-30 15:50:12 +0000 | [diff] [blame] | 8182 | const auto *RD = VD->getType() |
| 8183 | .getCanonicalType() |
| 8184 | .getNonReferenceType() |
| 8185 | ->getAsCXXRecordDecl(); |
| 8186 | if (!RD || !RD->isLambda()) |
| 8187 | return; |
| 8188 | Address VDAddr = Address(Arg, CGF.getContext().getDeclAlign(VD)); |
| 8189 | LValue VDLVal = CGF.MakeAddrLValue( |
| 8190 | VDAddr, VD->getType().getCanonicalType().getNonReferenceType()); |
| 8191 | llvm::DenseMap<const VarDecl *, FieldDecl *> Captures; |
| 8192 | FieldDecl *ThisCapture = nullptr; |
| 8193 | RD->getCaptureFields(Captures, ThisCapture); |
| 8194 | if (ThisCapture) { |
| 8195 | LValue ThisLVal = |
| 8196 | CGF.EmitLValueForFieldInitialization(VDLVal, ThisCapture); |
| Alexey Bataev | 969dbc0 | 2018-11-08 15:47:39 +0000 | [diff] [blame] | 8197 | LValue ThisLValVal = CGF.EmitLValueForField(VDLVal, ThisCapture); |
| Akira Hatanaka | f139ae3 | 2019-12-03 15:17:01 -0800 | [diff] [blame] | 8198 | LambdaPointers.try_emplace(ThisLVal.getPointer(CGF), |
| 8199 | VDLVal.getPointer(CGF)); |
| 8200 | BasePointers.push_back(ThisLVal.getPointer(CGF)); |
| 8201 | Pointers.push_back(ThisLValVal.getPointer(CGF)); |
| Alexey Bataev | a90fc66 | 2019-06-25 16:00:43 +0000 | [diff] [blame] | 8202 | Sizes.push_back( |
| 8203 | CGF.Builder.CreateIntCast(CGF.getTypeSize(CGF.getContext().VoidPtrTy), |
| 8204 | CGF.Int64Ty, /*isSigned=*/true)); |
| Alexey Bataev | 2dc07d0 | 2018-11-02 15:25:06 +0000 | [diff] [blame] | 8205 | Types.push_back(OMP_MAP_PTR_AND_OBJ | OMP_MAP_LITERAL | |
| Alexey Bataev | 6070542 | 2018-10-30 15:50:12 +0000 | [diff] [blame] | 8206 | OMP_MAP_MEMBER_OF | OMP_MAP_IMPLICIT); |
| 8207 | } |
| 8208 | for (const LambdaCapture &LC : RD->captures()) { |
| Alexey Bataev | af6725b | 2019-07-11 13:54:26 +0000 | [diff] [blame] | 8209 | if (!LC.capturesVariable()) |
| Alexey Bataev | 6070542 | 2018-10-30 15:50:12 +0000 | [diff] [blame] | 8210 | continue; |
| 8211 | const VarDecl *VD = LC.getCapturedVar(); |
| Alexey Bataev | af6725b | 2019-07-11 13:54:26 +0000 | [diff] [blame] | 8212 | if (LC.getCaptureKind() != LCK_ByRef && !VD->getType()->isPointerType()) |
| 8213 | continue; |
| Alexey Bataev | 6070542 | 2018-10-30 15:50:12 +0000 | [diff] [blame] | 8214 | auto It = Captures.find(VD); |
| 8215 | assert(It != Captures.end() && "Found lambda capture without field."); |
| 8216 | LValue VarLVal = CGF.EmitLValueForFieldInitialization(VDLVal, It->second); |
| Alexey Bataev | af6725b | 2019-07-11 13:54:26 +0000 | [diff] [blame] | 8217 | if (LC.getCaptureKind() == LCK_ByRef) { |
| 8218 | LValue VarLValVal = CGF.EmitLValueForField(VDLVal, It->second); |
| Akira Hatanaka | f139ae3 | 2019-12-03 15:17:01 -0800 | [diff] [blame] | 8219 | LambdaPointers.try_emplace(VarLVal.getPointer(CGF), |
| 8220 | VDLVal.getPointer(CGF)); |
| 8221 | BasePointers.push_back(VarLVal.getPointer(CGF)); |
| 8222 | Pointers.push_back(VarLValVal.getPointer(CGF)); |
| Alexey Bataev | af6725b | 2019-07-11 13:54:26 +0000 | [diff] [blame] | 8223 | Sizes.push_back(CGF.Builder.CreateIntCast( |
| 8224 | CGF.getTypeSize( |
| 8225 | VD->getType().getCanonicalType().getNonReferenceType()), |
| 8226 | CGF.Int64Ty, /*isSigned=*/true)); |
| 8227 | } else { |
| 8228 | RValue VarRVal = CGF.EmitLoadOfLValue(VarLVal, RD->getLocation()); |
| Akira Hatanaka | f139ae3 | 2019-12-03 15:17:01 -0800 | [diff] [blame] | 8229 | LambdaPointers.try_emplace(VarLVal.getPointer(CGF), |
| 8230 | VDLVal.getPointer(CGF)); |
| 8231 | BasePointers.push_back(VarLVal.getPointer(CGF)); |
| Alexey Bataev | af6725b | 2019-07-11 13:54:26 +0000 | [diff] [blame] | 8232 | Pointers.push_back(VarRVal.getScalarVal()); |
| 8233 | Sizes.push_back(llvm::ConstantInt::get(CGF.Int64Ty, 0)); |
| 8234 | } |
| Alexey Bataev | 2dc07d0 | 2018-11-02 15:25:06 +0000 | [diff] [blame] | 8235 | Types.push_back(OMP_MAP_PTR_AND_OBJ | OMP_MAP_LITERAL | |
| Alexey Bataev | 6070542 | 2018-10-30 15:50:12 +0000 | [diff] [blame] | 8236 | OMP_MAP_MEMBER_OF | OMP_MAP_IMPLICIT); |
| 8237 | } |
| 8238 | } |
| 8239 | |
| 8240 | /// Set correct indices for lambdas captures. |
| Alexey Bataev | 969dbc0 | 2018-11-08 15:47:39 +0000 | [diff] [blame] | 8241 | void adjustMemberOfForLambdaCaptures( |
| 8242 | const llvm::DenseMap<llvm::Value *, llvm::Value *> &LambdaPointers, |
| 8243 | MapBaseValuesArrayTy &BasePointers, MapValuesArrayTy &Pointers, |
| 8244 | MapFlagsArrayTy &Types) const { |
| Alexey Bataev | 6070542 | 2018-10-30 15:50:12 +0000 | [diff] [blame] | 8245 | for (unsigned I = 0, E = Types.size(); I < E; ++I) { |
| 8246 | // Set correct member_of idx for all implicit lambda captures. |
| Alexey Bataev | 2dc07d0 | 2018-11-02 15:25:06 +0000 | [diff] [blame] | 8247 | if (Types[I] != (OMP_MAP_PTR_AND_OBJ | OMP_MAP_LITERAL | |
| Alexey Bataev | 6070542 | 2018-10-30 15:50:12 +0000 | [diff] [blame] | 8248 | OMP_MAP_MEMBER_OF | OMP_MAP_IMPLICIT)) |
| 8249 | continue; |
| Alexey Bataev | 969dbc0 | 2018-11-08 15:47:39 +0000 | [diff] [blame] | 8250 | llvm::Value *BasePtr = LambdaPointers.lookup(*BasePointers[I]); |
| 8251 | assert(BasePtr && "Unable to find base lambda address."); |
| Alexey Bataev | 6070542 | 2018-10-30 15:50:12 +0000 | [diff] [blame] | 8252 | int TgtIdx = -1; |
| 8253 | for (unsigned J = I; J > 0; --J) { |
| 8254 | unsigned Idx = J - 1; |
| 8255 | if (Pointers[Idx] != BasePtr) |
| 8256 | continue; |
| 8257 | TgtIdx = Idx; |
| 8258 | break; |
| 8259 | } |
| 8260 | assert(TgtIdx != -1 && "Unable to find parent lambda."); |
| 8261 | // All other current entries will be MEMBER_OF the combined entry |
| 8262 | // (except for PTR_AND_OBJ entries which do not have a placeholder value |
| 8263 | // 0xFFFF in the MEMBER_OF field). |
| 8264 | OpenMPOffloadMappingFlags MemberOfFlag = getMemberOfFlag(TgtIdx); |
| 8265 | setCorrectMemberOfFlag(Types[I], MemberOfFlag); |
| 8266 | } |
| 8267 | } |
| 8268 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 8269 | /// Generate the base pointers, section pointers, sizes and map types |
| Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 8270 | /// associated to a given capture. |
| 8271 | void generateInfoForCapture(const CapturedStmt::Capture *Cap, |
| Samuel Antao | 6890b09 | 2016-07-28 14:25:09 +0000 | [diff] [blame] | 8272 | llvm::Value *Arg, |
| Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 8273 | MapBaseValuesArrayTy &BasePointers, |
| Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 8274 | MapValuesArrayTy &Pointers, |
| Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 8275 | MapValuesArrayTy &Sizes, MapFlagsArrayTy &Types, |
| 8276 | StructRangeInfoTy &PartialStruct) const { |
| Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 8277 | assert(!Cap->capturesVariableArrayType() && |
| 8278 | "Not expecting to generate map info for a variable array type!"); |
| 8279 | |
| Samuel Antao | 6890b09 | 2016-07-28 14:25:09 +0000 | [diff] [blame] | 8280 | // We need to know when we generating information for the first component |
| Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 8281 | const ValueDecl *VD = Cap->capturesThis() |
| 8282 | ? nullptr |
| 8283 | : Cap->getCapturedVar()->getCanonicalDecl(); |
| Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 8284 | |
| Samuel Antao | 6890b09 | 2016-07-28 14:25:09 +0000 | [diff] [blame] | 8285 | // If this declaration appears in a is_device_ptr clause we just have to |
| 8286 | // pass the pointer by value. If it is a reference to a declaration, we just |
| Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 8287 | // pass its value. |
| 8288 | if (DevPointersMap.count(VD)) { |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8289 | BasePointers.emplace_back(Arg, VD); |
| Samuel Antao | 6890b09 | 2016-07-28 14:25:09 +0000 | [diff] [blame] | 8290 | Pointers.push_back(Arg); |
| Alexey Bataev | a90fc66 | 2019-06-25 16:00:43 +0000 | [diff] [blame] | 8291 | Sizes.push_back( |
| 8292 | CGF.Builder.CreateIntCast(CGF.getTypeSize(CGF.getContext().VoidPtrTy), |
| 8293 | CGF.Int64Ty, /*isSigned=*/true)); |
| George Rokos | 065755d | 2017-11-07 18:27:04 +0000 | [diff] [blame] | 8294 | Types.push_back(OMP_MAP_LITERAL | OMP_MAP_TARGET_PARAM); |
| Samuel Antao | 6890b09 | 2016-07-28 14:25:09 +0000 | [diff] [blame] | 8295 | return; |
| 8296 | } |
| 8297 | |
| Alexey Bataev | e82445f | 2018-09-20 13:54:02 +0000 | [diff] [blame] | 8298 | using MapData = |
| 8299 | std::tuple<OMPClauseMappableExprCommon::MappableExprComponentListRef, |
| Kelvin Li | ef57943 | 2018-12-18 22:18:41 +0000 | [diff] [blame] | 8300 | OpenMPMapClauseKind, ArrayRef<OpenMPMapModifierKind>, bool>; |
| Alexey Bataev | e82445f | 2018-09-20 13:54:02 +0000 | [diff] [blame] | 8301 | SmallVector<MapData, 4> DeclComponentLists; |
| Michael Kruse | d47b943 | 2019-08-05 18:43:21 +0000 | [diff] [blame] | 8302 | assert(CurDir.is<const OMPExecutableDirective *>() && |
| 8303 | "Expect a executable directive"); |
| 8304 | const auto *CurExecDir = CurDir.get<const OMPExecutableDirective *>(); |
| 8305 | for (const auto *C : CurExecDir->getClausesOfKind<OMPMapClause>()) { |
| Mark de Wever | 51abceb | 2019-11-12 20:48:11 +0100 | [diff] [blame] | 8306 | for (const auto L : C->decl_component_lists(VD)) { |
| Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 8307 | assert(L.first == VD && |
| 8308 | "We got information for the wrong declaration??"); |
| 8309 | assert(!L.second.empty() && |
| 8310 | "Not expecting declaration with no component lists."); |
| Alexey Bataev | e82445f | 2018-09-20 13:54:02 +0000 | [diff] [blame] | 8311 | DeclComponentLists.emplace_back(L.second, C->getMapType(), |
| Kelvin Li | ef57943 | 2018-12-18 22:18:41 +0000 | [diff] [blame] | 8312 | C->getMapTypeModifiers(), |
| Alexey Bataev | e82445f | 2018-09-20 13:54:02 +0000 | [diff] [blame] | 8313 | C->isImplicit()); |
| Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 8314 | } |
| Alexey Bataev | e82445f | 2018-09-20 13:54:02 +0000 | [diff] [blame] | 8315 | } |
| 8316 | |
| 8317 | // Find overlapping elements (including the offset from the base element). |
| 8318 | llvm::SmallDenseMap< |
| 8319 | const MapData *, |
| 8320 | llvm::SmallVector< |
| 8321 | OMPClauseMappableExprCommon::MappableExprComponentListRef, 4>, |
| 8322 | 4> |
| 8323 | OverlappedData; |
| 8324 | size_t Count = 0; |
| 8325 | for (const MapData &L : DeclComponentLists) { |
| 8326 | OMPClauseMappableExprCommon::MappableExprComponentListRef Components; |
| 8327 | OpenMPMapClauseKind MapType; |
| Kelvin Li | ef57943 | 2018-12-18 22:18:41 +0000 | [diff] [blame] | 8328 | ArrayRef<OpenMPMapModifierKind> MapModifiers; |
| Alexey Bataev | e82445f | 2018-09-20 13:54:02 +0000 | [diff] [blame] | 8329 | bool IsImplicit; |
| Kelvin Li | ef57943 | 2018-12-18 22:18:41 +0000 | [diff] [blame] | 8330 | std::tie(Components, MapType, MapModifiers, IsImplicit) = L; |
| Alexey Bataev | e82445f | 2018-09-20 13:54:02 +0000 | [diff] [blame] | 8331 | ++Count; |
| 8332 | for (const MapData &L1 : makeArrayRef(DeclComponentLists).slice(Count)) { |
| 8333 | OMPClauseMappableExprCommon::MappableExprComponentListRef Components1; |
| Kelvin Li | ef57943 | 2018-12-18 22:18:41 +0000 | [diff] [blame] | 8334 | std::tie(Components1, MapType, MapModifiers, IsImplicit) = L1; |
| Alexey Bataev | e82445f | 2018-09-20 13:54:02 +0000 | [diff] [blame] | 8335 | auto CI = Components.rbegin(); |
| 8336 | auto CE = Components.rend(); |
| 8337 | auto SI = Components1.rbegin(); |
| 8338 | auto SE = Components1.rend(); |
| 8339 | for (; CI != CE && SI != SE; ++CI, ++SI) { |
| 8340 | if (CI->getAssociatedExpression()->getStmtClass() != |
| 8341 | SI->getAssociatedExpression()->getStmtClass()) |
| 8342 | break; |
| 8343 | // Are we dealing with different variables/fields? |
| 8344 | if (CI->getAssociatedDeclaration() != SI->getAssociatedDeclaration()) |
| 8345 | break; |
| 8346 | } |
| 8347 | // Found overlapping if, at least for one component, reached the head of |
| 8348 | // the components list. |
| 8349 | if (CI == CE || SI == SE) { |
| 8350 | assert((CI != CE || SI != SE) && |
| 8351 | "Unexpected full match of the mapping components."); |
| 8352 | const MapData &BaseData = CI == CE ? L : L1; |
| 8353 | OMPClauseMappableExprCommon::MappableExprComponentListRef SubData = |
| 8354 | SI == SE ? Components : Components1; |
| Alexey Bataev | e82445f | 2018-09-20 13:54:02 +0000 | [diff] [blame] | 8355 | auto &OverlappedElements = OverlappedData.FindAndConstruct(&BaseData); |
| 8356 | OverlappedElements.getSecond().push_back(SubData); |
| 8357 | } |
| 8358 | } |
| 8359 | } |
| 8360 | // Sort the overlapped elements for each item. |
| 8361 | llvm::SmallVector<const FieldDecl *, 4> Layout; |
| 8362 | if (!OverlappedData.empty()) { |
| 8363 | if (const auto *CRD = |
| 8364 | VD->getType().getCanonicalType()->getAsCXXRecordDecl()) |
| 8365 | getPlainLayout(CRD, Layout, /*AsBase=*/false); |
| 8366 | else { |
| 8367 | const auto *RD = VD->getType().getCanonicalType()->getAsRecordDecl(); |
| 8368 | Layout.append(RD->field_begin(), RD->field_end()); |
| 8369 | } |
| 8370 | } |
| 8371 | for (auto &Pair : OverlappedData) { |
| 8372 | llvm::sort( |
| 8373 | Pair.getSecond(), |
| 8374 | [&Layout]( |
| 8375 | OMPClauseMappableExprCommon::MappableExprComponentListRef First, |
| 8376 | OMPClauseMappableExprCommon::MappableExprComponentListRef |
| 8377 | Second) { |
| 8378 | auto CI = First.rbegin(); |
| 8379 | auto CE = First.rend(); |
| 8380 | auto SI = Second.rbegin(); |
| 8381 | auto SE = Second.rend(); |
| 8382 | for (; CI != CE && SI != SE; ++CI, ++SI) { |
| 8383 | if (CI->getAssociatedExpression()->getStmtClass() != |
| 8384 | SI->getAssociatedExpression()->getStmtClass()) |
| 8385 | break; |
| 8386 | // Are we dealing with different variables/fields? |
| 8387 | if (CI->getAssociatedDeclaration() != |
| 8388 | SI->getAssociatedDeclaration()) |
| 8389 | break; |
| 8390 | } |
| Richard Trieu | 5061e83 | 2018-09-21 21:20:33 +0000 | [diff] [blame] | 8391 | |
| 8392 | // Lists contain the same elements. |
| 8393 | if (CI == CE && SI == SE) |
| 8394 | return false; |
| 8395 | |
| 8396 | // List with less elements is less than list with more elements. |
| 8397 | if (CI == CE || SI == SE) |
| 8398 | return CI == CE; |
| 8399 | |
| Alexey Bataev | e82445f | 2018-09-20 13:54:02 +0000 | [diff] [blame] | 8400 | const auto *FD1 = cast<FieldDecl>(CI->getAssociatedDeclaration()); |
| 8401 | const auto *FD2 = cast<FieldDecl>(SI->getAssociatedDeclaration()); |
| 8402 | if (FD1->getParent() == FD2->getParent()) |
| 8403 | return FD1->getFieldIndex() < FD2->getFieldIndex(); |
| 8404 | const auto It = |
| 8405 | llvm::find_if(Layout, [FD1, FD2](const FieldDecl *FD) { |
| 8406 | return FD == FD1 || FD == FD2; |
| 8407 | }); |
| 8408 | return *It == FD1; |
| 8409 | }); |
| 8410 | } |
| 8411 | |
| 8412 | // Associated with a capture, because the mapping flags depend on it. |
| 8413 | // Go through all of the elements with the overlapped elements. |
| 8414 | for (const auto &Pair : OverlappedData) { |
| 8415 | const MapData &L = *Pair.getFirst(); |
| 8416 | OMPClauseMappableExprCommon::MappableExprComponentListRef Components; |
| 8417 | OpenMPMapClauseKind MapType; |
| Kelvin Li | ef57943 | 2018-12-18 22:18:41 +0000 | [diff] [blame] | 8418 | ArrayRef<OpenMPMapModifierKind> MapModifiers; |
| Alexey Bataev | e82445f | 2018-09-20 13:54:02 +0000 | [diff] [blame] | 8419 | bool IsImplicit; |
| Kelvin Li | ef57943 | 2018-12-18 22:18:41 +0000 | [diff] [blame] | 8420 | std::tie(Components, MapType, MapModifiers, IsImplicit) = L; |
| Alexey Bataev | e82445f | 2018-09-20 13:54:02 +0000 | [diff] [blame] | 8421 | ArrayRef<OMPClauseMappableExprCommon::MappableExprComponentListRef> |
| 8422 | OverlappedComponents = Pair.getSecond(); |
| 8423 | bool IsFirstComponentList = true; |
| Kelvin Li | ef57943 | 2018-12-18 22:18:41 +0000 | [diff] [blame] | 8424 | generateInfoForComponentList(MapType, MapModifiers, Components, |
| Alexey Bataev | e82445f | 2018-09-20 13:54:02 +0000 | [diff] [blame] | 8425 | BasePointers, Pointers, Sizes, Types, |
| 8426 | PartialStruct, IsFirstComponentList, |
| 8427 | IsImplicit, OverlappedComponents); |
| 8428 | } |
| 8429 | // Go through other elements without overlapped elements. |
| 8430 | bool IsFirstComponentList = OverlappedData.empty(); |
| 8431 | for (const MapData &L : DeclComponentLists) { |
| 8432 | OMPClauseMappableExprCommon::MappableExprComponentListRef Components; |
| 8433 | OpenMPMapClauseKind MapType; |
| Kelvin Li | ef57943 | 2018-12-18 22:18:41 +0000 | [diff] [blame] | 8434 | ArrayRef<OpenMPMapModifierKind> MapModifiers; |
| Alexey Bataev | e82445f | 2018-09-20 13:54:02 +0000 | [diff] [blame] | 8435 | bool IsImplicit; |
| Kelvin Li | ef57943 | 2018-12-18 22:18:41 +0000 | [diff] [blame] | 8436 | std::tie(Components, MapType, MapModifiers, IsImplicit) = L; |
| Alexey Bataev | e82445f | 2018-09-20 13:54:02 +0000 | [diff] [blame] | 8437 | auto It = OverlappedData.find(&L); |
| 8438 | if (It == OverlappedData.end()) |
| Kelvin Li | ef57943 | 2018-12-18 22:18:41 +0000 | [diff] [blame] | 8439 | generateInfoForComponentList(MapType, MapModifiers, Components, |
| Alexey Bataev | e82445f | 2018-09-20 13:54:02 +0000 | [diff] [blame] | 8440 | BasePointers, Pointers, Sizes, Types, |
| 8441 | PartialStruct, IsFirstComponentList, |
| 8442 | IsImplicit); |
| 8443 | IsFirstComponentList = false; |
| 8444 | } |
| Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 8445 | } |
| Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 8446 | |
| Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 8447 | /// Generate the base pointers, section pointers, sizes and map types |
| 8448 | /// associated with the declare target link variables. |
| 8449 | void generateInfoForDeclareTargetLink(MapBaseValuesArrayTy &BasePointers, |
| 8450 | MapValuesArrayTy &Pointers, |
| 8451 | MapValuesArrayTy &Sizes, |
| 8452 | MapFlagsArrayTy &Types) const { |
| Michael Kruse | d47b943 | 2019-08-05 18:43:21 +0000 | [diff] [blame] | 8453 | assert(CurDir.is<const OMPExecutableDirective *>() && |
| 8454 | "Expect a executable directive"); |
| 8455 | const auto *CurExecDir = CurDir.get<const OMPExecutableDirective *>(); |
| Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 8456 | // Map other list items in the map clause which are not captured variables |
| Gheorghe-Teodor Bercea | 66cdbb47 | 2019-05-21 19:42:01 +0000 | [diff] [blame] | 8457 | // but "declare target link" global variables. |
| Michael Kruse | d47b943 | 2019-08-05 18:43:21 +0000 | [diff] [blame] | 8458 | for (const auto *C : CurExecDir->getClausesOfKind<OMPMapClause>()) { |
| Mark de Wever | 51abceb | 2019-11-12 20:48:11 +0100 | [diff] [blame] | 8459 | for (const auto L : C->component_lists()) { |
| Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 8460 | if (!L.first) |
| 8461 | continue; |
| 8462 | const auto *VD = dyn_cast<VarDecl>(L.first); |
| 8463 | if (!VD) |
| 8464 | continue; |
| 8465 | llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res = |
| Alexey Bataev | 97b7221 | 2018-08-14 18:31:20 +0000 | [diff] [blame] | 8466 | OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD); |
| Gheorghe-Teodor Bercea | 5254f0a | 2019-06-14 17:58:26 +0000 | [diff] [blame] | 8467 | if (CGF.CGM.getOpenMPRuntime().hasRequiresUnifiedSharedMemory() || |
| 8468 | !Res || *Res != OMPDeclareTargetDeclAttr::MT_Link) |
| Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 8469 | continue; |
| 8470 | StructRangeInfoTy PartialStruct; |
| 8471 | generateInfoForComponentList( |
| Kelvin Li | ef57943 | 2018-12-18 22:18:41 +0000 | [diff] [blame] | 8472 | C->getMapType(), C->getMapTypeModifiers(), L.second, BasePointers, |
| Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 8473 | Pointers, Sizes, Types, PartialStruct, |
| 8474 | /*IsFirstComponentList=*/true, C->isImplicit()); |
| 8475 | assert(!PartialStruct.Base.isValid() && |
| 8476 | "No partial structs for declare target link expected."); |
| 8477 | } |
| 8478 | } |
| Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 8479 | } |
| Samuel Antao | d486f84 | 2016-05-26 16:53:38 +0000 | [diff] [blame] | 8480 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 8481 | /// Generate the default map information for a given capture \a CI, |
| Samuel Antao | d486f84 | 2016-05-26 16:53:38 +0000 | [diff] [blame] | 8482 | /// record field declaration \a RI and captured value \a CV. |
| Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 8483 | void generateDefaultMapInfo(const CapturedStmt::Capture &CI, |
| 8484 | const FieldDecl &RI, llvm::Value *CV, |
| 8485 | MapBaseValuesArrayTy &CurBasePointers, |
| 8486 | MapValuesArrayTy &CurPointers, |
| 8487 | MapValuesArrayTy &CurSizes, |
| Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 8488 | MapFlagsArrayTy &CurMapTypes) const { |
| Alexey Bataev | f288cf9 | 2019-06-27 18:53:07 +0000 | [diff] [blame] | 8489 | bool IsImplicit = true; |
| Samuel Antao | d486f84 | 2016-05-26 16:53:38 +0000 | [diff] [blame] | 8490 | // Do the default mapping. |
| 8491 | if (CI.capturesThis()) { |
| 8492 | CurBasePointers.push_back(CV); |
| 8493 | CurPointers.push_back(CV); |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8494 | const auto *PtrTy = cast<PointerType>(RI.getType().getTypePtr()); |
| Alexey Bataev | a90fc66 | 2019-06-25 16:00:43 +0000 | [diff] [blame] | 8495 | CurSizes.push_back( |
| 8496 | CGF.Builder.CreateIntCast(CGF.getTypeSize(PtrTy->getPointeeType()), |
| 8497 | CGF.Int64Ty, /*isSigned=*/true)); |
| Samuel Antao | d486f84 | 2016-05-26 16:53:38 +0000 | [diff] [blame] | 8498 | // Default map type. |
| Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 8499 | CurMapTypes.push_back(OMP_MAP_TO | OMP_MAP_FROM); |
| Samuel Antao | d486f84 | 2016-05-26 16:53:38 +0000 | [diff] [blame] | 8500 | } else if (CI.capturesVariableByCopy()) { |
| Samuel Antao | 6d00426 | 2016-06-16 18:39:34 +0000 | [diff] [blame] | 8501 | CurBasePointers.push_back(CV); |
| 8502 | CurPointers.push_back(CV); |
| Samuel Antao | d486f84 | 2016-05-26 16:53:38 +0000 | [diff] [blame] | 8503 | if (!RI.getType()->isAnyPointerType()) { |
| Samuel Antao | 6d00426 | 2016-06-16 18:39:34 +0000 | [diff] [blame] | 8504 | // We have to signal to the runtime captures passed by value that are |
| 8505 | // not pointers. |
| George Rokos | 065755d | 2017-11-07 18:27:04 +0000 | [diff] [blame] | 8506 | CurMapTypes.push_back(OMP_MAP_LITERAL); |
| Alexey Bataev | a90fc66 | 2019-06-25 16:00:43 +0000 | [diff] [blame] | 8507 | CurSizes.push_back(CGF.Builder.CreateIntCast( |
| 8508 | CGF.getTypeSize(RI.getType()), CGF.Int64Ty, /*isSigned=*/true)); |
| Samuel Antao | d486f84 | 2016-05-26 16:53:38 +0000 | [diff] [blame] | 8509 | } else { |
| 8510 | // Pointers are implicitly mapped with a zero size and no flags |
| 8511 | // (other than first map that is added for all implicit maps). |
| Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 8512 | CurMapTypes.push_back(OMP_MAP_NONE); |
| Alexey Bataev | a90fc66 | 2019-06-25 16:00:43 +0000 | [diff] [blame] | 8513 | CurSizes.push_back(llvm::Constant::getNullValue(CGF.Int64Ty)); |
| Samuel Antao | d486f84 | 2016-05-26 16:53:38 +0000 | [diff] [blame] | 8514 | } |
| Alexey Bataev | f288cf9 | 2019-06-27 18:53:07 +0000 | [diff] [blame] | 8515 | const VarDecl *VD = CI.getCapturedVar(); |
| 8516 | auto I = FirstPrivateDecls.find(VD); |
| 8517 | if (I != FirstPrivateDecls.end()) |
| 8518 | IsImplicit = I->getSecond(); |
| Samuel Antao | d486f84 | 2016-05-26 16:53:38 +0000 | [diff] [blame] | 8519 | } else { |
| 8520 | assert(CI.capturesVariable() && "Expected captured reference."); |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8521 | const auto *PtrTy = cast<ReferenceType>(RI.getType().getTypePtr()); |
| Samuel Antao | d486f84 | 2016-05-26 16:53:38 +0000 | [diff] [blame] | 8522 | QualType ElementType = PtrTy->getPointeeType(); |
| Alexey Bataev | a90fc66 | 2019-06-25 16:00:43 +0000 | [diff] [blame] | 8523 | CurSizes.push_back(CGF.Builder.CreateIntCast( |
| 8524 | CGF.getTypeSize(ElementType), CGF.Int64Ty, /*isSigned=*/true)); |
| Samuel Antao | d486f84 | 2016-05-26 16:53:38 +0000 | [diff] [blame] | 8525 | // The default map type for a scalar/complex type is 'to' because by |
| 8526 | // default the value doesn't have to be retrieved. For an aggregate |
| 8527 | // type, the default is 'tofrom'. |
| Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 8528 | CurMapTypes.push_back(getMapModifiersForPrivateClauses(CI)); |
| Alexey Bataev | 1af5bd5 | 2019-03-05 17:47:18 +0000 | [diff] [blame] | 8529 | const VarDecl *VD = CI.getCapturedVar(); |
| Alexey Bataev | f288cf9 | 2019-06-27 18:53:07 +0000 | [diff] [blame] | 8530 | auto I = FirstPrivateDecls.find(VD); |
| 8531 | if (I != FirstPrivateDecls.end() && |
| Alexey Bataev | 1af5bd5 | 2019-03-05 17:47:18 +0000 | [diff] [blame] | 8532 | VD->getType().isConstant(CGF.getContext())) { |
| 8533 | llvm::Constant *Addr = |
| 8534 | CGF.CGM.getOpenMPRuntime().registerTargetFirstprivateCopy(CGF, VD); |
| 8535 | // Copy the value of the original variable to the new global copy. |
| 8536 | CGF.Builder.CreateMemCpy( |
| Akira Hatanaka | f139ae3 | 2019-12-03 15:17:01 -0800 | [diff] [blame] | 8537 | CGF.MakeNaturalAlignAddrLValue(Addr, ElementType).getAddress(CGF), |
| Alexey Bataev | 1af5bd5 | 2019-03-05 17:47:18 +0000 | [diff] [blame] | 8538 | Address(CV, CGF.getContext().getTypeAlignInChars(ElementType)), |
| Rui Ueyama | 49a3ad2 | 2019-07-16 04:46:31 +0000 | [diff] [blame] | 8539 | CurSizes.back(), /*IsVolatile=*/false); |
| Alexey Bataev | 1af5bd5 | 2019-03-05 17:47:18 +0000 | [diff] [blame] | 8540 | // Use new global variable as the base pointers. |
| 8541 | CurBasePointers.push_back(Addr); |
| 8542 | CurPointers.push_back(Addr); |
| 8543 | } else { |
| 8544 | CurBasePointers.push_back(CV); |
| Alexey Bataev | f288cf9 | 2019-06-27 18:53:07 +0000 | [diff] [blame] | 8545 | if (I != FirstPrivateDecls.end() && ElementType->isAnyPointerType()) { |
| Alexey Bataev | c2b831f | 2019-04-02 16:03:40 +0000 | [diff] [blame] | 8546 | Address PtrAddr = CGF.EmitLoadOfReference(CGF.MakeAddrLValue( |
| 8547 | CV, ElementType, CGF.getContext().getDeclAlign(VD), |
| 8548 | AlignmentSource::Decl)); |
| 8549 | CurPointers.push_back(PtrAddr.getPointer()); |
| 8550 | } else { |
| 8551 | CurPointers.push_back(CV); |
| 8552 | } |
| Alexey Bataev | 1af5bd5 | 2019-03-05 17:47:18 +0000 | [diff] [blame] | 8553 | } |
| Alexey Bataev | f288cf9 | 2019-06-27 18:53:07 +0000 | [diff] [blame] | 8554 | if (I != FirstPrivateDecls.end()) |
| 8555 | IsImplicit = I->getSecond(); |
| Samuel Antao | d486f84 | 2016-05-26 16:53:38 +0000 | [diff] [blame] | 8556 | } |
| George Rokos | 065755d | 2017-11-07 18:27:04 +0000 | [diff] [blame] | 8557 | // Every default map produces a single argument which is a target parameter. |
| 8558 | CurMapTypes.back() |= OMP_MAP_TARGET_PARAM; |
| Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 8559 | |
| 8560 | // Add flag stating this is an implicit map. |
| Alexey Bataev | f288cf9 | 2019-06-27 18:53:07 +0000 | [diff] [blame] | 8561 | if (IsImplicit) |
| 8562 | CurMapTypes.back() |= OMP_MAP_IMPLICIT; |
| Samuel Antao | d486f84 | 2016-05-26 16:53:38 +0000 | [diff] [blame] | 8563 | } |
| Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 8564 | }; |
| Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8565 | } // anonymous namespace |
| 8566 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 8567 | /// Emit the arrays used to pass the captures and map information to the |
| Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8568 | /// offloading runtime library. If there is no map or capture information, |
| 8569 | /// return nullptr by reference. |
| 8570 | static void |
| Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 8571 | emitOffloadingArrays(CodeGenFunction &CGF, |
| 8572 | MappableExprsHandler::MapBaseValuesArrayTy &BasePointers, |
| Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8573 | MappableExprsHandler::MapValuesArrayTy &Pointers, |
| 8574 | MappableExprsHandler::MapValuesArrayTy &Sizes, |
| Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 8575 | MappableExprsHandler::MapFlagsArrayTy &MapTypes, |
| 8576 | CGOpenMPRuntime::TargetDataInfo &Info) { |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8577 | CodeGenModule &CGM = CGF.CGM; |
| 8578 | ASTContext &Ctx = CGF.getContext(); |
| Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8579 | |
| Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 8580 | // Reset the array information. |
| 8581 | Info.clearArrayInfo(); |
| 8582 | Info.NumberOfPtrs = BasePointers.size(); |
| Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8583 | |
| Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 8584 | if (Info.NumberOfPtrs) { |
| Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8585 | // Detect if we have any capture size requiring runtime evaluation of the |
| 8586 | // size so that a constant array could be eventually used. |
| 8587 | bool hasRuntimeEvaluationCaptureSize = false; |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8588 | for (llvm::Value *S : Sizes) |
| Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8589 | if (!isa<llvm::Constant>(S)) { |
| 8590 | hasRuntimeEvaluationCaptureSize = true; |
| 8591 | break; |
| 8592 | } |
| 8593 | |
| Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 8594 | llvm::APInt PointerNumAP(32, Info.NumberOfPtrs, /*isSigned=*/true); |
| Richard Smith | 772e266 | 2019-10-04 01:25:59 +0000 | [diff] [blame] | 8595 | QualType PointerArrayType = Ctx.getConstantArrayType( |
| 8596 | Ctx.VoidPtrTy, PointerNumAP, nullptr, ArrayType::Normal, |
| 8597 | /*IndexTypeQuals=*/0); |
| Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8598 | |
| Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 8599 | Info.BasePointersArray = |
| Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8600 | CGF.CreateMemTemp(PointerArrayType, ".offload_baseptrs").getPointer(); |
| Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 8601 | Info.PointersArray = |
| Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8602 | CGF.CreateMemTemp(PointerArrayType, ".offload_ptrs").getPointer(); |
| 8603 | |
| 8604 | // If we don't have any VLA types or other types that require runtime |
| 8605 | // evaluation, we can use a constant array for the map sizes, otherwise we |
| 8606 | // need to fill up the arrays as we do for the pointers. |
| Alexey Bataev | a90fc66 | 2019-06-25 16:00:43 +0000 | [diff] [blame] | 8607 | QualType Int64Ty = |
| 8608 | Ctx.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1); |
| Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8609 | if (hasRuntimeEvaluationCaptureSize) { |
| Richard Smith | 772e266 | 2019-10-04 01:25:59 +0000 | [diff] [blame] | 8610 | QualType SizeArrayType = Ctx.getConstantArrayType( |
| 8611 | Int64Ty, PointerNumAP, nullptr, ArrayType::Normal, |
| 8612 | /*IndexTypeQuals=*/0); |
| Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 8613 | Info.SizesArray = |
| Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8614 | CGF.CreateMemTemp(SizeArrayType, ".offload_sizes").getPointer(); |
| 8615 | } else { |
| 8616 | // We expect all the sizes to be constant, so we collect them to create |
| 8617 | // a constant array. |
| 8618 | SmallVector<llvm::Constant *, 16> ConstSizes; |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8619 | for (llvm::Value *S : Sizes) |
| Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8620 | ConstSizes.push_back(cast<llvm::Constant>(S)); |
| 8621 | |
| 8622 | auto *SizesArrayInit = llvm::ConstantArray::get( |
| Alexey Bataev | a90fc66 | 2019-06-25 16:00:43 +0000 | [diff] [blame] | 8623 | llvm::ArrayType::get(CGM.Int64Ty, ConstSizes.size()), ConstSizes); |
| Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 8624 | std::string Name = CGM.getOpenMPRuntime().getName({"offload_sizes"}); |
| Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8625 | auto *SizesArrayGbl = new llvm::GlobalVariable( |
| 8626 | CGM.getModule(), SizesArrayInit->getType(), |
| 8627 | /*isConstant=*/true, llvm::GlobalValue::PrivateLinkage, |
| Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 8628 | SizesArrayInit, Name); |
| Peter Collingbourne | bcf909d | 2016-06-14 21:02:05 +0000 | [diff] [blame] | 8629 | SizesArrayGbl->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global); |
| Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 8630 | Info.SizesArray = SizesArrayGbl; |
| Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8631 | } |
| 8632 | |
| 8633 | // The map types are always constant so we don't need to generate code to |
| 8634 | // fill arrays. Instead, we create an array constant. |
| Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 8635 | SmallVector<uint64_t, 4> Mapping(MapTypes.size(), 0); |
| 8636 | llvm::copy(MapTypes, Mapping.begin()); |
| Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8637 | llvm::Constant *MapTypesArrayInit = |
| Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 8638 | llvm::ConstantDataArray::get(CGF.Builder.getContext(), Mapping); |
| Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 8639 | std::string MaptypesName = |
| 8640 | CGM.getOpenMPRuntime().getName({"offload_maptypes"}); |
| Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8641 | auto *MapTypesArrayGbl = new llvm::GlobalVariable( |
| 8642 | CGM.getModule(), MapTypesArrayInit->getType(), |
| 8643 | /*isConstant=*/true, llvm::GlobalValue::PrivateLinkage, |
| Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 8644 | MapTypesArrayInit, MaptypesName); |
| Peter Collingbourne | bcf909d | 2016-06-14 21:02:05 +0000 | [diff] [blame] | 8645 | MapTypesArrayGbl->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global); |
| Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 8646 | Info.MapTypesArray = MapTypesArrayGbl; |
| Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8647 | |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8648 | for (unsigned I = 0; I < Info.NumberOfPtrs; ++I) { |
| 8649 | llvm::Value *BPVal = *BasePointers[I]; |
| Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8650 | llvm::Value *BP = CGF.Builder.CreateConstInBoundsGEP2_32( |
| Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 8651 | llvm::ArrayType::get(CGM.VoidPtrTy, Info.NumberOfPtrs), |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8652 | Info.BasePointersArray, 0, I); |
| Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 8653 | BP = CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
| 8654 | BP, BPVal->getType()->getPointerTo(/*AddrSpace=*/0)); |
| Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8655 | Address BPAddr(BP, Ctx.getTypeAlignInChars(Ctx.VoidPtrTy)); |
| 8656 | CGF.Builder.CreateStore(BPVal, BPAddr); |
| 8657 | |
| Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 8658 | if (Info.requiresDevicePointerInfo()) |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8659 | if (const ValueDecl *DevVD = BasePointers[I].getDevicePtrDecl()) |
| Alexey Bataev | 43a919f | 2018-04-13 17:48:43 +0000 | [diff] [blame] | 8660 | Info.CaptureDeviceAddrMap.try_emplace(DevVD, BPAddr); |
| Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 8661 | |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8662 | llvm::Value *PVal = Pointers[I]; |
| Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8663 | llvm::Value *P = CGF.Builder.CreateConstInBoundsGEP2_32( |
| Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 8664 | llvm::ArrayType::get(CGM.VoidPtrTy, Info.NumberOfPtrs), |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8665 | Info.PointersArray, 0, I); |
| Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 8666 | P = CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
| 8667 | P, PVal->getType()->getPointerTo(/*AddrSpace=*/0)); |
| Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8668 | Address PAddr(P, Ctx.getTypeAlignInChars(Ctx.VoidPtrTy)); |
| 8669 | CGF.Builder.CreateStore(PVal, PAddr); |
| 8670 | |
| 8671 | if (hasRuntimeEvaluationCaptureSize) { |
| 8672 | llvm::Value *S = CGF.Builder.CreateConstInBoundsGEP2_32( |
| Alexey Bataev | a90fc66 | 2019-06-25 16:00:43 +0000 | [diff] [blame] | 8673 | llvm::ArrayType::get(CGM.Int64Ty, Info.NumberOfPtrs), |
| Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 8674 | Info.SizesArray, |
| Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8675 | /*Idx0=*/0, |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8676 | /*Idx1=*/I); |
| Alexey Bataev | a90fc66 | 2019-06-25 16:00:43 +0000 | [diff] [blame] | 8677 | Address SAddr(S, Ctx.getTypeAlignInChars(Int64Ty)); |
| Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8678 | CGF.Builder.CreateStore( |
| Alexey Bataev | a90fc66 | 2019-06-25 16:00:43 +0000 | [diff] [blame] | 8679 | CGF.Builder.CreateIntCast(Sizes[I], CGM.Int64Ty, /*isSigned=*/true), |
| Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8680 | SAddr); |
| 8681 | } |
| 8682 | } |
| 8683 | } |
| 8684 | } |
| Michael Kruse | d47b943 | 2019-08-05 18:43:21 +0000 | [diff] [blame] | 8685 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 8686 | /// Emit the arguments to be passed to the runtime library based on the |
| Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8687 | /// arrays of pointers, sizes and map types. |
| 8688 | static void emitOffloadingArraysArgument( |
| 8689 | CodeGenFunction &CGF, llvm::Value *&BasePointersArrayArg, |
| 8690 | llvm::Value *&PointersArrayArg, llvm::Value *&SizesArrayArg, |
| Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 8691 | llvm::Value *&MapTypesArrayArg, CGOpenMPRuntime::TargetDataInfo &Info) { |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8692 | CodeGenModule &CGM = CGF.CGM; |
| Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 8693 | if (Info.NumberOfPtrs) { |
| Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8694 | BasePointersArrayArg = CGF.Builder.CreateConstInBoundsGEP2_32( |
| Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 8695 | llvm::ArrayType::get(CGM.VoidPtrTy, Info.NumberOfPtrs), |
| 8696 | Info.BasePointersArray, |
| Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8697 | /*Idx0=*/0, /*Idx1=*/0); |
| 8698 | PointersArrayArg = CGF.Builder.CreateConstInBoundsGEP2_32( |
| Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 8699 | llvm::ArrayType::get(CGM.VoidPtrTy, Info.NumberOfPtrs), |
| 8700 | Info.PointersArray, |
| Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8701 | /*Idx0=*/0, |
| 8702 | /*Idx1=*/0); |
| 8703 | SizesArrayArg = CGF.Builder.CreateConstInBoundsGEP2_32( |
| Alexey Bataev | a90fc66 | 2019-06-25 16:00:43 +0000 | [diff] [blame] | 8704 | llvm::ArrayType::get(CGM.Int64Ty, Info.NumberOfPtrs), Info.SizesArray, |
| Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8705 | /*Idx0=*/0, /*Idx1=*/0); |
| 8706 | MapTypesArrayArg = CGF.Builder.CreateConstInBoundsGEP2_32( |
| George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 8707 | llvm::ArrayType::get(CGM.Int64Ty, Info.NumberOfPtrs), |
| Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 8708 | Info.MapTypesArray, |
| Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8709 | /*Idx0=*/0, |
| 8710 | /*Idx1=*/0); |
| 8711 | } else { |
| 8712 | BasePointersArrayArg = llvm::ConstantPointerNull::get(CGM.VoidPtrPtrTy); |
| 8713 | PointersArrayArg = llvm::ConstantPointerNull::get(CGM.VoidPtrPtrTy); |
| Alexey Bataev | a90fc66 | 2019-06-25 16:00:43 +0000 | [diff] [blame] | 8714 | SizesArrayArg = llvm::ConstantPointerNull::get(CGM.Int64Ty->getPointerTo()); |
| Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8715 | MapTypesArrayArg = |
| George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 8716 | llvm::ConstantPointerNull::get(CGM.Int64Ty->getPointerTo()); |
| Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8717 | } |
| Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 8718 | } |
| 8719 | |
| Alexey Bataev | 7bb3353 | 2019-01-07 21:30:43 +0000 | [diff] [blame] | 8720 | /// Check for inner distribute directive. |
| 8721 | static const OMPExecutableDirective * |
| 8722 | getNestedDistributeDirective(ASTContext &Ctx, const OMPExecutableDirective &D) { |
| 8723 | const auto *CS = D.getInnermostCapturedStmt(); |
| 8724 | const auto *Body = |
| 8725 | CS->getCapturedStmt()->IgnoreContainers(/*IgnoreCaptured=*/true); |
| Alexey Bataev | 5c42736 | 2019-04-10 19:11:33 +0000 | [diff] [blame] | 8726 | const Stmt *ChildStmt = |
| 8727 | CGOpenMPSIMDRuntime::getSingleCompoundChild(Ctx, Body); |
| Alexey Bataev | 7bb3353 | 2019-01-07 21:30:43 +0000 | [diff] [blame] | 8728 | |
| Alexey Bataev | 5c42736 | 2019-04-10 19:11:33 +0000 | [diff] [blame] | 8729 | if (const auto *NestedDir = |
| 8730 | dyn_cast_or_null<OMPExecutableDirective>(ChildStmt)) { |
| Alexey Bataev | 7bb3353 | 2019-01-07 21:30:43 +0000 | [diff] [blame] | 8731 | OpenMPDirectiveKind DKind = NestedDir->getDirectiveKind(); |
| 8732 | switch (D.getDirectiveKind()) { |
| 8733 | case OMPD_target: |
| 8734 | if (isOpenMPDistributeDirective(DKind)) |
| 8735 | return NestedDir; |
| 8736 | if (DKind == OMPD_teams) { |
| 8737 | Body = NestedDir->getInnermostCapturedStmt()->IgnoreContainers( |
| 8738 | /*IgnoreCaptured=*/true); |
| 8739 | if (!Body) |
| 8740 | return nullptr; |
| Alexey Bataev | 5c42736 | 2019-04-10 19:11:33 +0000 | [diff] [blame] | 8741 | ChildStmt = CGOpenMPSIMDRuntime::getSingleCompoundChild(Ctx, Body); |
| 8742 | if (const auto *NND = |
| 8743 | dyn_cast_or_null<OMPExecutableDirective>(ChildStmt)) { |
| Alexey Bataev | 7bb3353 | 2019-01-07 21:30:43 +0000 | [diff] [blame] | 8744 | DKind = NND->getDirectiveKind(); |
| 8745 | if (isOpenMPDistributeDirective(DKind)) |
| 8746 | return NND; |
| 8747 | } |
| 8748 | } |
| 8749 | return nullptr; |
| 8750 | case OMPD_target_teams: |
| 8751 | if (isOpenMPDistributeDirective(DKind)) |
| 8752 | return NestedDir; |
| 8753 | return nullptr; |
| 8754 | case OMPD_target_parallel: |
| 8755 | case OMPD_target_simd: |
| 8756 | case OMPD_target_parallel_for: |
| 8757 | case OMPD_target_parallel_for_simd: |
| 8758 | return nullptr; |
| 8759 | case OMPD_target_teams_distribute: |
| 8760 | case OMPD_target_teams_distribute_simd: |
| 8761 | case OMPD_target_teams_distribute_parallel_for: |
| 8762 | case OMPD_target_teams_distribute_parallel_for_simd: |
| 8763 | case OMPD_parallel: |
| 8764 | case OMPD_for: |
| 8765 | case OMPD_parallel_for: |
| cchen | 47d6094 | 2019-12-05 13:43:48 -0500 | [diff] [blame] | 8766 | case OMPD_parallel_master: |
| Alexey Bataev | 7bb3353 | 2019-01-07 21:30:43 +0000 | [diff] [blame] | 8767 | case OMPD_parallel_sections: |
| 8768 | case OMPD_for_simd: |
| 8769 | case OMPD_parallel_for_simd: |
| 8770 | case OMPD_cancel: |
| 8771 | case OMPD_cancellation_point: |
| 8772 | case OMPD_ordered: |
| 8773 | case OMPD_threadprivate: |
| Alexey Bataev | 25ed0c0 | 2019-03-07 17:54:44 +0000 | [diff] [blame] | 8774 | case OMPD_allocate: |
| Alexey Bataev | 7bb3353 | 2019-01-07 21:30:43 +0000 | [diff] [blame] | 8775 | case OMPD_task: |
| 8776 | case OMPD_simd: |
| 8777 | case OMPD_sections: |
| 8778 | case OMPD_section: |
| 8779 | case OMPD_single: |
| 8780 | case OMPD_master: |
| 8781 | case OMPD_critical: |
| 8782 | case OMPD_taskyield: |
| 8783 | case OMPD_barrier: |
| 8784 | case OMPD_taskwait: |
| 8785 | case OMPD_taskgroup: |
| 8786 | case OMPD_atomic: |
| 8787 | case OMPD_flush: |
| 8788 | case OMPD_teams: |
| 8789 | case OMPD_target_data: |
| 8790 | case OMPD_target_exit_data: |
| 8791 | case OMPD_target_enter_data: |
| 8792 | case OMPD_distribute: |
| 8793 | case OMPD_distribute_simd: |
| 8794 | case OMPD_distribute_parallel_for: |
| 8795 | case OMPD_distribute_parallel_for_simd: |
| 8796 | case OMPD_teams_distribute: |
| 8797 | case OMPD_teams_distribute_simd: |
| 8798 | case OMPD_teams_distribute_parallel_for: |
| 8799 | case OMPD_teams_distribute_parallel_for_simd: |
| 8800 | case OMPD_target_update: |
| 8801 | case OMPD_declare_simd: |
| Alexey Bataev | d158cf6 | 2019-09-13 20:18:17 +0000 | [diff] [blame] | 8802 | case OMPD_declare_variant: |
| Alexey Bataev | 7bb3353 | 2019-01-07 21:30:43 +0000 | [diff] [blame] | 8803 | case OMPD_declare_target: |
| 8804 | case OMPD_end_declare_target: |
| 8805 | case OMPD_declare_reduction: |
| Michael Kruse | 251e148 | 2019-02-01 20:25:04 +0000 | [diff] [blame] | 8806 | case OMPD_declare_mapper: |
| Alexey Bataev | 7bb3353 | 2019-01-07 21:30:43 +0000 | [diff] [blame] | 8807 | case OMPD_taskloop: |
| 8808 | case OMPD_taskloop_simd: |
| Alexey Bataev | 60e51c4 | 2019-10-10 20:13:02 +0000 | [diff] [blame] | 8809 | case OMPD_master_taskloop: |
| Alexey Bataev | b8552ab | 2019-10-18 16:47:35 +0000 | [diff] [blame] | 8810 | case OMPD_master_taskloop_simd: |
| Alexey Bataev | 5bbcead | 2019-10-14 17:17:41 +0000 | [diff] [blame] | 8811 | case OMPD_parallel_master_taskloop: |
| Alexey Bataev | 14a388f | 2019-10-25 10:27:13 -0400 | [diff] [blame] | 8812 | case OMPD_parallel_master_taskloop_simd: |
| Alexey Bataev | 7bb3353 | 2019-01-07 21:30:43 +0000 | [diff] [blame] | 8813 | case OMPD_requires: |
| 8814 | case OMPD_unknown: |
| 8815 | llvm_unreachable("Unexpected directive."); |
| 8816 | } |
| 8817 | } |
| 8818 | |
| 8819 | return nullptr; |
| 8820 | } |
| 8821 | |
| Michael Kruse | d47b943 | 2019-08-05 18:43:21 +0000 | [diff] [blame] | 8822 | /// Emit the user-defined mapper function. The code generation follows the |
| 8823 | /// pattern in the example below. |
| 8824 | /// \code |
| 8825 | /// void .omp_mapper.<type_name>.<mapper_id>.(void *rt_mapper_handle, |
| 8826 | /// void *base, void *begin, |
| 8827 | /// int64_t size, int64_t type) { |
| 8828 | /// // Allocate space for an array section first. |
| 8829 | /// if (size > 1 && !maptype.IsDelete) |
| 8830 | /// __tgt_push_mapper_component(rt_mapper_handle, base, begin, |
| 8831 | /// size*sizeof(Ty), clearToFrom(type)); |
| 8832 | /// // Map members. |
| 8833 | /// for (unsigned i = 0; i < size; i++) { |
| 8834 | /// // For each component specified by this mapper: |
| 8835 | /// for (auto c : all_components) { |
| 8836 | /// if (c.hasMapper()) |
| 8837 | /// (*c.Mapper())(rt_mapper_handle, c.arg_base, c.arg_begin, c.arg_size, |
| 8838 | /// c.arg_type); |
| 8839 | /// else |
| 8840 | /// __tgt_push_mapper_component(rt_mapper_handle, c.arg_base, |
| 8841 | /// c.arg_begin, c.arg_size, c.arg_type); |
| 8842 | /// } |
| 8843 | /// } |
| 8844 | /// // Delete the array section. |
| 8845 | /// if (size > 1 && maptype.IsDelete) |
| 8846 | /// __tgt_push_mapper_component(rt_mapper_handle, base, begin, |
| 8847 | /// size*sizeof(Ty), clearToFrom(type)); |
| 8848 | /// } |
| 8849 | /// \endcode |
| 8850 | void CGOpenMPRuntime::emitUserDefinedMapper(const OMPDeclareMapperDecl *D, |
| 8851 | CodeGenFunction *CGF) { |
| 8852 | if (UDMMap.count(D) > 0) |
| 8853 | return; |
| 8854 | ASTContext &C = CGM.getContext(); |
| 8855 | QualType Ty = D->getType(); |
| 8856 | QualType PtrTy = C.getPointerType(Ty).withRestrict(); |
| 8857 | QualType Int64Ty = C.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/true); |
| 8858 | auto *MapperVarDecl = |
| 8859 | cast<VarDecl>(cast<DeclRefExpr>(D->getMapperVarRef())->getDecl()); |
| 8860 | SourceLocation Loc = D->getLocation(); |
| 8861 | CharUnits ElementSize = C.getTypeSizeInChars(Ty); |
| 8862 | |
| 8863 | // Prepare mapper function arguments and attributes. |
| 8864 | ImplicitParamDecl HandleArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, |
| 8865 | C.VoidPtrTy, ImplicitParamDecl::Other); |
| 8866 | ImplicitParamDecl BaseArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, C.VoidPtrTy, |
| 8867 | ImplicitParamDecl::Other); |
| 8868 | ImplicitParamDecl BeginArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, |
| 8869 | C.VoidPtrTy, ImplicitParamDecl::Other); |
| 8870 | ImplicitParamDecl SizeArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, Int64Ty, |
| 8871 | ImplicitParamDecl::Other); |
| 8872 | ImplicitParamDecl TypeArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, Int64Ty, |
| 8873 | ImplicitParamDecl::Other); |
| 8874 | FunctionArgList Args; |
| 8875 | Args.push_back(&HandleArg); |
| 8876 | Args.push_back(&BaseArg); |
| 8877 | Args.push_back(&BeginArg); |
| 8878 | Args.push_back(&SizeArg); |
| 8879 | Args.push_back(&TypeArg); |
| 8880 | const CGFunctionInfo &FnInfo = |
| 8881 | CGM.getTypes().arrangeBuiltinFunctionDeclaration(C.VoidTy, Args); |
| 8882 | llvm::FunctionType *FnTy = CGM.getTypes().GetFunctionType(FnInfo); |
| 8883 | SmallString<64> TyStr; |
| 8884 | llvm::raw_svector_ostream Out(TyStr); |
| 8885 | CGM.getCXXABI().getMangleContext().mangleTypeName(Ty, Out); |
| 8886 | std::string Name = getName({"omp_mapper", TyStr, D->getName()}); |
| 8887 | auto *Fn = llvm::Function::Create(FnTy, llvm::GlobalValue::InternalLinkage, |
| 8888 | Name, &CGM.getModule()); |
| 8889 | CGM.SetInternalFunctionAttributes(GlobalDecl(), Fn, FnInfo); |
| 8890 | Fn->removeFnAttr(llvm::Attribute::OptimizeNone); |
| 8891 | // Start the mapper function code generation. |
| 8892 | CodeGenFunction MapperCGF(CGM); |
| 8893 | MapperCGF.StartFunction(GlobalDecl(), C.VoidTy, Fn, FnInfo, Args, Loc, Loc); |
| 8894 | // Compute the starting and end addreses of array elements. |
| 8895 | llvm::Value *Size = MapperCGF.EmitLoadOfScalar( |
| 8896 | MapperCGF.GetAddrOfLocalVar(&SizeArg), /*Volatile=*/false, |
| 8897 | C.getPointerType(Int64Ty), Loc); |
| 8898 | llvm::Value *PtrBegin = MapperCGF.Builder.CreateBitCast( |
| 8899 | MapperCGF.GetAddrOfLocalVar(&BeginArg).getPointer(), |
| 8900 | CGM.getTypes().ConvertTypeForMem(C.getPointerType(PtrTy))); |
| 8901 | llvm::Value *PtrEnd = MapperCGF.Builder.CreateGEP(PtrBegin, Size); |
| 8902 | llvm::Value *MapType = MapperCGF.EmitLoadOfScalar( |
| 8903 | MapperCGF.GetAddrOfLocalVar(&TypeArg), /*Volatile=*/false, |
| 8904 | C.getPointerType(Int64Ty), Loc); |
| 8905 | // Prepare common arguments for array initiation and deletion. |
| 8906 | llvm::Value *Handle = MapperCGF.EmitLoadOfScalar( |
| 8907 | MapperCGF.GetAddrOfLocalVar(&HandleArg), |
| 8908 | /*Volatile=*/false, C.getPointerType(C.VoidPtrTy), Loc); |
| 8909 | llvm::Value *BaseIn = MapperCGF.EmitLoadOfScalar( |
| 8910 | MapperCGF.GetAddrOfLocalVar(&BaseArg), |
| 8911 | /*Volatile=*/false, C.getPointerType(C.VoidPtrTy), Loc); |
| 8912 | llvm::Value *BeginIn = MapperCGF.EmitLoadOfScalar( |
| 8913 | MapperCGF.GetAddrOfLocalVar(&BeginArg), |
| 8914 | /*Volatile=*/false, C.getPointerType(C.VoidPtrTy), Loc); |
| 8915 | |
| 8916 | // Emit array initiation if this is an array section and \p MapType indicates |
| 8917 | // that memory allocation is required. |
| 8918 | llvm::BasicBlock *HeadBB = MapperCGF.createBasicBlock("omp.arraymap.head"); |
| 8919 | emitUDMapperArrayInitOrDel(MapperCGF, Handle, BaseIn, BeginIn, Size, MapType, |
| 8920 | ElementSize, HeadBB, /*IsInit=*/true); |
| 8921 | |
| 8922 | // Emit a for loop to iterate through SizeArg of elements and map all of them. |
| 8923 | |
| 8924 | // Emit the loop header block. |
| 8925 | MapperCGF.EmitBlock(HeadBB); |
| 8926 | llvm::BasicBlock *BodyBB = MapperCGF.createBasicBlock("omp.arraymap.body"); |
| 8927 | llvm::BasicBlock *DoneBB = MapperCGF.createBasicBlock("omp.done"); |
| 8928 | // Evaluate whether the initial condition is satisfied. |
| 8929 | llvm::Value *IsEmpty = |
| 8930 | MapperCGF.Builder.CreateICmpEQ(PtrBegin, PtrEnd, "omp.arraymap.isempty"); |
| 8931 | MapperCGF.Builder.CreateCondBr(IsEmpty, DoneBB, BodyBB); |
| 8932 | llvm::BasicBlock *EntryBB = MapperCGF.Builder.GetInsertBlock(); |
| 8933 | |
| 8934 | // Emit the loop body block. |
| 8935 | MapperCGF.EmitBlock(BodyBB); |
| 8936 | llvm::PHINode *PtrPHI = MapperCGF.Builder.CreatePHI( |
| 8937 | PtrBegin->getType(), 2, "omp.arraymap.ptrcurrent"); |
| 8938 | PtrPHI->addIncoming(PtrBegin, EntryBB); |
| 8939 | Address PtrCurrent = |
| 8940 | Address(PtrPHI, MapperCGF.GetAddrOfLocalVar(&BeginArg) |
| 8941 | .getAlignment() |
| 8942 | .alignmentOfArrayElement(ElementSize)); |
| 8943 | // Privatize the declared variable of mapper to be the current array element. |
| 8944 | CodeGenFunction::OMPPrivateScope Scope(MapperCGF); |
| Akira Hatanaka | 9f37c0e | 2019-12-03 13:07:22 -0800 | [diff] [blame] | 8945 | Scope.addPrivate(MapperVarDecl, [&MapperCGF, PtrCurrent, PtrTy]() { |
| Michael Kruse | d47b943 | 2019-08-05 18:43:21 +0000 | [diff] [blame] | 8946 | return MapperCGF |
| 8947 | .EmitLoadOfPointerLValue(PtrCurrent, PtrTy->castAs<PointerType>()) |
| Akira Hatanaka | f139ae3 | 2019-12-03 15:17:01 -0800 | [diff] [blame] | 8948 | .getAddress(MapperCGF); |
| Michael Kruse | d47b943 | 2019-08-05 18:43:21 +0000 | [diff] [blame] | 8949 | }); |
| 8950 | (void)Scope.Privatize(); |
| 8951 | |
| 8952 | // Get map clause information. Fill up the arrays with all mapped variables. |
| 8953 | MappableExprsHandler::MapBaseValuesArrayTy BasePointers; |
| 8954 | MappableExprsHandler::MapValuesArrayTy Pointers; |
| 8955 | MappableExprsHandler::MapValuesArrayTy Sizes; |
| 8956 | MappableExprsHandler::MapFlagsArrayTy MapTypes; |
| 8957 | MappableExprsHandler MEHandler(*D, MapperCGF); |
| 8958 | MEHandler.generateAllInfoForMapper(BasePointers, Pointers, Sizes, MapTypes); |
| 8959 | |
| 8960 | // Call the runtime API __tgt_mapper_num_components to get the number of |
| 8961 | // pre-existing components. |
| 8962 | llvm::Value *OffloadingArgs[] = {Handle}; |
| 8963 | llvm::Value *PreviousSize = MapperCGF.EmitRuntimeCall( |
| 8964 | createRuntimeFunction(OMPRTL__tgt_mapper_num_components), OffloadingArgs); |
| 8965 | llvm::Value *ShiftedPreviousSize = MapperCGF.Builder.CreateShl( |
| 8966 | PreviousSize, |
| 8967 | MapperCGF.Builder.getInt64(MappableExprsHandler::getFlagMemberOffset())); |
| 8968 | |
| 8969 | // Fill up the runtime mapper handle for all components. |
| 8970 | for (unsigned I = 0; I < BasePointers.size(); ++I) { |
| 8971 | llvm::Value *CurBaseArg = MapperCGF.Builder.CreateBitCast( |
| 8972 | *BasePointers[I], CGM.getTypes().ConvertTypeForMem(C.VoidPtrTy)); |
| 8973 | llvm::Value *CurBeginArg = MapperCGF.Builder.CreateBitCast( |
| 8974 | Pointers[I], CGM.getTypes().ConvertTypeForMem(C.VoidPtrTy)); |
| 8975 | llvm::Value *CurSizeArg = Sizes[I]; |
| 8976 | |
| 8977 | // Extract the MEMBER_OF field from the map type. |
| 8978 | llvm::BasicBlock *MemberBB = MapperCGF.createBasicBlock("omp.member"); |
| 8979 | MapperCGF.EmitBlock(MemberBB); |
| 8980 | llvm::Value *OriMapType = MapperCGF.Builder.getInt64(MapTypes[I]); |
| 8981 | llvm::Value *Member = MapperCGF.Builder.CreateAnd( |
| 8982 | OriMapType, |
| 8983 | MapperCGF.Builder.getInt64(MappableExprsHandler::OMP_MAP_MEMBER_OF)); |
| 8984 | llvm::BasicBlock *MemberCombineBB = |
| 8985 | MapperCGF.createBasicBlock("omp.member.combine"); |
| 8986 | llvm::BasicBlock *TypeBB = MapperCGF.createBasicBlock("omp.type"); |
| 8987 | llvm::Value *IsMember = MapperCGF.Builder.CreateIsNull(Member); |
| 8988 | MapperCGF.Builder.CreateCondBr(IsMember, TypeBB, MemberCombineBB); |
| 8989 | // Add the number of pre-existing components to the MEMBER_OF field if it |
| 8990 | // is valid. |
| 8991 | MapperCGF.EmitBlock(MemberCombineBB); |
| 8992 | llvm::Value *CombinedMember = |
| 8993 | MapperCGF.Builder.CreateNUWAdd(OriMapType, ShiftedPreviousSize); |
| 8994 | // Do nothing if it is not a member of previous components. |
| 8995 | MapperCGF.EmitBlock(TypeBB); |
| 8996 | llvm::PHINode *MemberMapType = |
| 8997 | MapperCGF.Builder.CreatePHI(CGM.Int64Ty, 4, "omp.membermaptype"); |
| 8998 | MemberMapType->addIncoming(OriMapType, MemberBB); |
| 8999 | MemberMapType->addIncoming(CombinedMember, MemberCombineBB); |
| 9000 | |
| 9001 | // Combine the map type inherited from user-defined mapper with that |
| 9002 | // specified in the program. According to the OMP_MAP_TO and OMP_MAP_FROM |
| 9003 | // bits of the \a MapType, which is the input argument of the mapper |
| 9004 | // function, the following code will set the OMP_MAP_TO and OMP_MAP_FROM |
| 9005 | // bits of MemberMapType. |
| 9006 | // [OpenMP 5.0], 1.2.6. map-type decay. |
| 9007 | // | alloc | to | from | tofrom | release | delete |
| 9008 | // ---------------------------------------------------------- |
| 9009 | // alloc | alloc | alloc | alloc | alloc | release | delete |
| 9010 | // to | alloc | to | alloc | to | release | delete |
| 9011 | // from | alloc | alloc | from | from | release | delete |
| 9012 | // tofrom | alloc | to | from | tofrom | release | delete |
| 9013 | llvm::Value *LeftToFrom = MapperCGF.Builder.CreateAnd( |
| 9014 | MapType, |
| 9015 | MapperCGF.Builder.getInt64(MappableExprsHandler::OMP_MAP_TO | |
| 9016 | MappableExprsHandler::OMP_MAP_FROM)); |
| 9017 | llvm::BasicBlock *AllocBB = MapperCGF.createBasicBlock("omp.type.alloc"); |
| 9018 | llvm::BasicBlock *AllocElseBB = |
| 9019 | MapperCGF.createBasicBlock("omp.type.alloc.else"); |
| 9020 | llvm::BasicBlock *ToBB = MapperCGF.createBasicBlock("omp.type.to"); |
| 9021 | llvm::BasicBlock *ToElseBB = MapperCGF.createBasicBlock("omp.type.to.else"); |
| 9022 | llvm::BasicBlock *FromBB = MapperCGF.createBasicBlock("omp.type.from"); |
| 9023 | llvm::BasicBlock *EndBB = MapperCGF.createBasicBlock("omp.type.end"); |
| 9024 | llvm::Value *IsAlloc = MapperCGF.Builder.CreateIsNull(LeftToFrom); |
| 9025 | MapperCGF.Builder.CreateCondBr(IsAlloc, AllocBB, AllocElseBB); |
| 9026 | // In case of alloc, clear OMP_MAP_TO and OMP_MAP_FROM. |
| 9027 | MapperCGF.EmitBlock(AllocBB); |
| 9028 | llvm::Value *AllocMapType = MapperCGF.Builder.CreateAnd( |
| 9029 | MemberMapType, |
| 9030 | MapperCGF.Builder.getInt64(~(MappableExprsHandler::OMP_MAP_TO | |
| 9031 | MappableExprsHandler::OMP_MAP_FROM))); |
| 9032 | MapperCGF.Builder.CreateBr(EndBB); |
| 9033 | MapperCGF.EmitBlock(AllocElseBB); |
| 9034 | llvm::Value *IsTo = MapperCGF.Builder.CreateICmpEQ( |
| 9035 | LeftToFrom, |
| 9036 | MapperCGF.Builder.getInt64(MappableExprsHandler::OMP_MAP_TO)); |
| 9037 | MapperCGF.Builder.CreateCondBr(IsTo, ToBB, ToElseBB); |
| 9038 | // In case of to, clear OMP_MAP_FROM. |
| 9039 | MapperCGF.EmitBlock(ToBB); |
| 9040 | llvm::Value *ToMapType = MapperCGF.Builder.CreateAnd( |
| 9041 | MemberMapType, |
| 9042 | MapperCGF.Builder.getInt64(~MappableExprsHandler::OMP_MAP_FROM)); |
| 9043 | MapperCGF.Builder.CreateBr(EndBB); |
| 9044 | MapperCGF.EmitBlock(ToElseBB); |
| 9045 | llvm::Value *IsFrom = MapperCGF.Builder.CreateICmpEQ( |
| 9046 | LeftToFrom, |
| 9047 | MapperCGF.Builder.getInt64(MappableExprsHandler::OMP_MAP_FROM)); |
| 9048 | MapperCGF.Builder.CreateCondBr(IsFrom, FromBB, EndBB); |
| 9049 | // In case of from, clear OMP_MAP_TO. |
| 9050 | MapperCGF.EmitBlock(FromBB); |
| 9051 | llvm::Value *FromMapType = MapperCGF.Builder.CreateAnd( |
| 9052 | MemberMapType, |
| 9053 | MapperCGF.Builder.getInt64(~MappableExprsHandler::OMP_MAP_TO)); |
| 9054 | // In case of tofrom, do nothing. |
| 9055 | MapperCGF.EmitBlock(EndBB); |
| 9056 | llvm::PHINode *CurMapType = |
| 9057 | MapperCGF.Builder.CreatePHI(CGM.Int64Ty, 4, "omp.maptype"); |
| 9058 | CurMapType->addIncoming(AllocMapType, AllocBB); |
| 9059 | CurMapType->addIncoming(ToMapType, ToBB); |
| 9060 | CurMapType->addIncoming(FromMapType, FromBB); |
| 9061 | CurMapType->addIncoming(MemberMapType, ToElseBB); |
| 9062 | |
| 9063 | // TODO: call the corresponding mapper function if a user-defined mapper is |
| 9064 | // associated with this map clause. |
| 9065 | // Call the runtime API __tgt_push_mapper_component to fill up the runtime |
| 9066 | // data structure. |
| 9067 | llvm::Value *OffloadingArgs[] = {Handle, CurBaseArg, CurBeginArg, |
| 9068 | CurSizeArg, CurMapType}; |
| 9069 | MapperCGF.EmitRuntimeCall( |
| 9070 | createRuntimeFunction(OMPRTL__tgt_push_mapper_component), |
| 9071 | OffloadingArgs); |
| 9072 | } |
| 9073 | |
| 9074 | // Update the pointer to point to the next element that needs to be mapped, |
| 9075 | // and check whether we have mapped all elements. |
| 9076 | llvm::Value *PtrNext = MapperCGF.Builder.CreateConstGEP1_32( |
| 9077 | PtrPHI, /*Idx0=*/1, "omp.arraymap.next"); |
| 9078 | PtrPHI->addIncoming(PtrNext, BodyBB); |
| 9079 | llvm::Value *IsDone = |
| 9080 | MapperCGF.Builder.CreateICmpEQ(PtrNext, PtrEnd, "omp.arraymap.isdone"); |
| 9081 | llvm::BasicBlock *ExitBB = MapperCGF.createBasicBlock("omp.arraymap.exit"); |
| 9082 | MapperCGF.Builder.CreateCondBr(IsDone, ExitBB, BodyBB); |
| 9083 | |
| 9084 | MapperCGF.EmitBlock(ExitBB); |
| 9085 | // Emit array deletion if this is an array section and \p MapType indicates |
| 9086 | // that deletion is required. |
| 9087 | emitUDMapperArrayInitOrDel(MapperCGF, Handle, BaseIn, BeginIn, Size, MapType, |
| 9088 | ElementSize, DoneBB, /*IsInit=*/false); |
| 9089 | |
| 9090 | // Emit the function exit block. |
| 9091 | MapperCGF.EmitBlock(DoneBB, /*IsFinished=*/true); |
| 9092 | MapperCGF.FinishFunction(); |
| 9093 | UDMMap.try_emplace(D, Fn); |
| 9094 | if (CGF) { |
| 9095 | auto &Decls = FunctionUDMMap.FindAndConstruct(CGF->CurFn); |
| 9096 | Decls.second.push_back(D); |
| 9097 | } |
| 9098 | } |
| 9099 | |
| 9100 | /// Emit the array initialization or deletion portion for user-defined mapper |
| 9101 | /// code generation. First, it evaluates whether an array section is mapped and |
| 9102 | /// whether the \a MapType instructs to delete this section. If \a IsInit is |
| 9103 | /// true, and \a MapType indicates to not delete this array, array |
| 9104 | /// initialization code is generated. If \a IsInit is false, and \a MapType |
| 9105 | /// indicates to not this array, array deletion code is generated. |
| 9106 | void CGOpenMPRuntime::emitUDMapperArrayInitOrDel( |
| 9107 | CodeGenFunction &MapperCGF, llvm::Value *Handle, llvm::Value *Base, |
| 9108 | llvm::Value *Begin, llvm::Value *Size, llvm::Value *MapType, |
| 9109 | CharUnits ElementSize, llvm::BasicBlock *ExitBB, bool IsInit) { |
| 9110 | StringRef Prefix = IsInit ? ".init" : ".del"; |
| 9111 | |
| 9112 | // Evaluate if this is an array section. |
| 9113 | llvm::BasicBlock *IsDeleteBB = |
| Alexey Bataev | 8b32192 | 2020-01-16 15:46:34 -0500 | [diff] [blame] | 9114 | MapperCGF.createBasicBlock(getName({"omp.array", Prefix, ".evaldelete"})); |
| 9115 | llvm::BasicBlock *BodyBB = |
| 9116 | MapperCGF.createBasicBlock(getName({"omp.array", Prefix})); |
| Michael Kruse | d47b943 | 2019-08-05 18:43:21 +0000 | [diff] [blame] | 9117 | llvm::Value *IsArray = MapperCGF.Builder.CreateICmpSGE( |
| 9118 | Size, MapperCGF.Builder.getInt64(1), "omp.arrayinit.isarray"); |
| 9119 | MapperCGF.Builder.CreateCondBr(IsArray, IsDeleteBB, ExitBB); |
| 9120 | |
| 9121 | // Evaluate if we are going to delete this section. |
| 9122 | MapperCGF.EmitBlock(IsDeleteBB); |
| 9123 | llvm::Value *DeleteBit = MapperCGF.Builder.CreateAnd( |
| 9124 | MapType, |
| 9125 | MapperCGF.Builder.getInt64(MappableExprsHandler::OMP_MAP_DELETE)); |
| 9126 | llvm::Value *DeleteCond; |
| 9127 | if (IsInit) { |
| 9128 | DeleteCond = MapperCGF.Builder.CreateIsNull( |
| Alexey Bataev | 8b32192 | 2020-01-16 15:46:34 -0500 | [diff] [blame] | 9129 | DeleteBit, getName({"omp.array", Prefix, ".delete"})); |
| Michael Kruse | d47b943 | 2019-08-05 18:43:21 +0000 | [diff] [blame] | 9130 | } else { |
| 9131 | DeleteCond = MapperCGF.Builder.CreateIsNotNull( |
| Alexey Bataev | 8b32192 | 2020-01-16 15:46:34 -0500 | [diff] [blame] | 9132 | DeleteBit, getName({"omp.array", Prefix, ".delete"})); |
| Michael Kruse | d47b943 | 2019-08-05 18:43:21 +0000 | [diff] [blame] | 9133 | } |
| 9134 | MapperCGF.Builder.CreateCondBr(DeleteCond, BodyBB, ExitBB); |
| 9135 | |
| 9136 | MapperCGF.EmitBlock(BodyBB); |
| 9137 | // Get the array size by multiplying element size and element number (i.e., \p |
| 9138 | // Size). |
| 9139 | llvm::Value *ArraySize = MapperCGF.Builder.CreateNUWMul( |
| 9140 | Size, MapperCGF.Builder.getInt64(ElementSize.getQuantity())); |
| 9141 | // Remove OMP_MAP_TO and OMP_MAP_FROM from the map type, so that it achieves |
| 9142 | // memory allocation/deletion purpose only. |
| 9143 | llvm::Value *MapTypeArg = MapperCGF.Builder.CreateAnd( |
| 9144 | MapType, |
| 9145 | MapperCGF.Builder.getInt64(~(MappableExprsHandler::OMP_MAP_TO | |
| 9146 | MappableExprsHandler::OMP_MAP_FROM))); |
| 9147 | // Call the runtime API __tgt_push_mapper_component to fill up the runtime |
| 9148 | // data structure. |
| 9149 | llvm::Value *OffloadingArgs[] = {Handle, Base, Begin, ArraySize, MapTypeArg}; |
| 9150 | MapperCGF.EmitRuntimeCall( |
| 9151 | createRuntimeFunction(OMPRTL__tgt_push_mapper_component), OffloadingArgs); |
| 9152 | } |
| 9153 | |
| Alexey Bataev | 7bb3353 | 2019-01-07 21:30:43 +0000 | [diff] [blame] | 9154 | void CGOpenMPRuntime::emitTargetNumIterationsCall( |
| Alexey Bataev | ec7946e | 2019-09-23 14:06:51 +0000 | [diff] [blame] | 9155 | CodeGenFunction &CGF, const OMPExecutableDirective &D, |
| 9156 | llvm::Value *DeviceID, |
| 9157 | llvm::function_ref<llvm::Value *(CodeGenFunction &CGF, |
| 9158 | const OMPLoopDirective &D)> |
| 9159 | SizeEmitter) { |
| Alexey Bataev | 7bb3353 | 2019-01-07 21:30:43 +0000 | [diff] [blame] | 9160 | OpenMPDirectiveKind Kind = D.getDirectiveKind(); |
| 9161 | const OMPExecutableDirective *TD = &D; |
| 9162 | // Get nested teams distribute kind directive, if any. |
| 9163 | if (!isOpenMPDistributeDirective(Kind) || !isOpenMPTeamsDirective(Kind)) |
| 9164 | TD = getNestedDistributeDirective(CGM.getContext(), D); |
| 9165 | if (!TD) |
| 9166 | return; |
| 9167 | const auto *LD = cast<OMPLoopDirective>(TD); |
| Alexey Bataev | ec7946e | 2019-09-23 14:06:51 +0000 | [diff] [blame] | 9168 | auto &&CodeGen = [LD, DeviceID, SizeEmitter, this](CodeGenFunction &CGF, |
| Alexey Bataev | 7bb3353 | 2019-01-07 21:30:43 +0000 | [diff] [blame] | 9169 | PrePostActionTy &) { |
| Alexey Bataev | ec7946e | 2019-09-23 14:06:51 +0000 | [diff] [blame] | 9170 | if (llvm::Value *NumIterations = SizeEmitter(CGF, *LD)) { |
| 9171 | llvm::Value *Args[] = {DeviceID, NumIterations}; |
| 9172 | CGF.EmitRuntimeCall( |
| 9173 | createRuntimeFunction(OMPRTL__kmpc_push_target_tripcount), Args); |
| 9174 | } |
| Alexey Bataev | 7bb3353 | 2019-01-07 21:30:43 +0000 | [diff] [blame] | 9175 | }; |
| 9176 | emitInlinedDirective(CGF, OMPD_unknown, CodeGen); |
| 9177 | } |
| 9178 | |
| Alexey Bataev | ec7946e | 2019-09-23 14:06:51 +0000 | [diff] [blame] | 9179 | void CGOpenMPRuntime::emitTargetCall( |
| 9180 | CodeGenFunction &CGF, const OMPExecutableDirective &D, |
| 9181 | llvm::Function *OutlinedFn, llvm::Value *OutlinedFnID, const Expr *IfCond, |
| 9182 | const Expr *Device, |
| 9183 | llvm::function_ref<llvm::Value *(CodeGenFunction &CGF, |
| 9184 | const OMPLoopDirective &D)> |
| 9185 | SizeEmitter) { |
| Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 9186 | if (!CGF.HaveInsertPoint()) |
| 9187 | return; |
| Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 9188 | |
| Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 9189 | assert(OutlinedFn && "Invalid outlined function!"); |
| 9190 | |
| Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 9191 | const bool RequiresOuterTask = D.hasClausesOfKind<OMPDependClause>(); |
| 9192 | llvm::SmallVector<llvm::Value *, 16> CapturedVars; |
| Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 9193 | const CapturedStmt &CS = *D.getCapturedStmt(OMPD_target); |
| Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 9194 | auto &&ArgsCodegen = [&CS, &CapturedVars](CodeGenFunction &CGF, |
| 9195 | PrePostActionTy &) { |
| 9196 | CGF.GenerateOpenMPCapturedVars(CS, CapturedVars); |
| 9197 | }; |
| 9198 | emitInlinedDirective(CGF, OMPD_unknown, ArgsCodegen); |
| Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 9199 | |
| Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 9200 | CodeGenFunction::OMPTargetDataInfo InputInfo; |
| 9201 | llvm::Value *MapTypesArray = nullptr; |
| Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 9202 | // Fill up the pointer arrays and transfer execution to the device. |
| Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 9203 | auto &&ThenGen = [this, Device, OutlinedFn, OutlinedFnID, &D, &InputInfo, |
| Alexey Bataev | ec7946e | 2019-09-23 14:06:51 +0000 | [diff] [blame] | 9204 | &MapTypesArray, &CS, RequiresOuterTask, &CapturedVars, |
| 9205 | SizeEmitter](CodeGenFunction &CGF, PrePostActionTy &) { |
| Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 9206 | // On top of the arrays that were filled up, the target offloading call |
| 9207 | // takes as arguments the device id as well as the host pointer. The host |
| 9208 | // pointer is used by the runtime library to identify the current target |
| 9209 | // region, so it only has to be unique and not necessarily point to |
| 9210 | // anything. It could be the pointer to the outlined function that |
| 9211 | // implements the target region, but we aren't using that so that the |
| 9212 | // compiler doesn't need to keep that, and could therefore inline the host |
| 9213 | // function if proven worthwhile during optimization. |
| 9214 | |
| Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 9215 | // From this point on, we need to have an ID of the target region defined. |
| 9216 | assert(OutlinedFnID && "Invalid outlined function ID!"); |
| Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 9217 | |
| 9218 | // Emit device ID if any. |
| 9219 | llvm::Value *DeviceID; |
| George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 9220 | if (Device) { |
| Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 9221 | DeviceID = CGF.Builder.CreateIntCast(CGF.EmitScalarExpr(Device), |
| George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 9222 | CGF.Int64Ty, /*isSigned=*/true); |
| 9223 | } else { |
| 9224 | DeviceID = CGF.Builder.getInt64(OMP_DEVICEID_UNDEF); |
| 9225 | } |
| Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 9226 | |
| Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 9227 | // Emit the number of elements in the offloading arrays. |
| Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 9228 | llvm::Value *PointerNum = |
| 9229 | CGF.Builder.getInt32(InputInfo.NumberOfTargetItems); |
| Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 9230 | |
| Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 9231 | // Return value of the runtime offloading call. |
| 9232 | llvm::Value *Return; |
| 9233 | |
| Alexey Bataev | 5c42736 | 2019-04-10 19:11:33 +0000 | [diff] [blame] | 9234 | llvm::Value *NumTeams = emitNumTeamsForTargetDirective(CGF, D); |
| 9235 | llvm::Value *NumThreads = emitNumThreadsForTargetDirective(CGF, D); |
| Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 9236 | |
| Alexey Bataev | ec7946e | 2019-09-23 14:06:51 +0000 | [diff] [blame] | 9237 | // Emit tripcount for the target loop-based directive. |
| 9238 | emitTargetNumIterationsCall(CGF, D, DeviceID, SizeEmitter); |
| 9239 | |
| Alexey Bataev | a9f77c6 | 2017-12-13 21:04:20 +0000 | [diff] [blame] | 9240 | bool HasNowait = D.hasClausesOfKind<OMPNowaitClause>(); |
| Arpith Chacko Jacob | 33c849a | 2017-01-25 00:57:16 +0000 | [diff] [blame] | 9241 | // The target region is an outlined function launched by the runtime |
| 9242 | // via calls __tgt_target() or __tgt_target_teams(). |
| 9243 | // |
| 9244 | // __tgt_target() launches a target region with one team and one thread, |
| 9245 | // executing a serial region. This master thread may in turn launch |
| 9246 | // more threads within its team upon encountering a parallel region, |
| 9247 | // however, no additional teams can be launched on the device. |
| 9248 | // |
| 9249 | // __tgt_target_teams() launches a target region with one or more teams, |
| 9250 | // each with one or more threads. This call is required for target |
| 9251 | // constructs such as: |
| 9252 | // 'target teams' |
| 9253 | // 'target' / 'teams' |
| 9254 | // 'target teams distribute parallel for' |
| 9255 | // 'target parallel' |
| 9256 | // and so on. |
| 9257 | // |
| 9258 | // Note that on the host and CPU targets, the runtime implementation of |
| 9259 | // these calls simply call the outlined function without forking threads. |
| 9260 | // The outlined functions themselves have runtime calls to |
| 9261 | // __kmpc_fork_teams() and __kmpc_fork() for this purpose, codegen'd by |
| 9262 | // the compiler in emitTeamsCall() and emitParallelCall(). |
| 9263 | // |
| 9264 | // In contrast, on the NVPTX target, the implementation of |
| 9265 | // __tgt_target_teams() launches a GPU kernel with the requested number |
| 9266 | // of teams and threads so no additional calls to the runtime are required. |
| Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 9267 | if (NumTeams) { |
| Arpith Chacko Jacob | 33c849a | 2017-01-25 00:57:16 +0000 | [diff] [blame] | 9268 | // If we have NumTeams defined this means that we have an enclosed teams |
| 9269 | // region. Therefore we also expect to have NumThreads defined. These two |
| 9270 | // values should be defined in the presence of a teams directive, |
| 9271 | // regardless of having any clauses associated. If the user is using teams |
| 9272 | // but no clauses, these two values will be the default that should be |
| 9273 | // passed to the runtime library - a 32-bit integer with the value zero. |
| 9274 | assert(NumThreads && "Thread limit expression should be available along " |
| 9275 | "with number of teams."); |
| Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 9276 | llvm::Value *OffloadingArgs[] = {DeviceID, |
| 9277 | OutlinedFnID, |
| 9278 | PointerNum, |
| 9279 | InputInfo.BasePointersArray.getPointer(), |
| 9280 | InputInfo.PointersArray.getPointer(), |
| 9281 | InputInfo.SizesArray.getPointer(), |
| 9282 | MapTypesArray, |
| 9283 | NumTeams, |
| 9284 | NumThreads}; |
| Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 9285 | Return = CGF.EmitRuntimeCall( |
| Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 9286 | createRuntimeFunction(HasNowait ? OMPRTL__tgt_target_teams_nowait |
| 9287 | : OMPRTL__tgt_target_teams), |
| Alexey Bataev | a9f77c6 | 2017-12-13 21:04:20 +0000 | [diff] [blame] | 9288 | OffloadingArgs); |
| Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 9289 | } else { |
| Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 9290 | llvm::Value *OffloadingArgs[] = {DeviceID, |
| 9291 | OutlinedFnID, |
| 9292 | PointerNum, |
| 9293 | InputInfo.BasePointersArray.getPointer(), |
| 9294 | InputInfo.PointersArray.getPointer(), |
| 9295 | InputInfo.SizesArray.getPointer(), |
| 9296 | MapTypesArray}; |
| Alexey Bataev | a9f77c6 | 2017-12-13 21:04:20 +0000 | [diff] [blame] | 9297 | Return = CGF.EmitRuntimeCall( |
| Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 9298 | createRuntimeFunction(HasNowait ? OMPRTL__tgt_target_nowait |
| 9299 | : OMPRTL__tgt_target), |
| Alexey Bataev | a9f77c6 | 2017-12-13 21:04:20 +0000 | [diff] [blame] | 9300 | OffloadingArgs); |
| Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 9301 | } |
| Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 9302 | |
| Alexey Bataev | 2a007e0 | 2017-10-02 14:20:58 +0000 | [diff] [blame] | 9303 | // Check the error code and execute the host version if required. |
| 9304 | llvm::BasicBlock *OffloadFailedBlock = |
| 9305 | CGF.createBasicBlock("omp_offload.failed"); |
| 9306 | llvm::BasicBlock *OffloadContBlock = |
| 9307 | CGF.createBasicBlock("omp_offload.cont"); |
| 9308 | llvm::Value *Failed = CGF.Builder.CreateIsNotNull(Return); |
| 9309 | CGF.Builder.CreateCondBr(Failed, OffloadFailedBlock, OffloadContBlock); |
| 9310 | |
| 9311 | CGF.EmitBlock(OffloadFailedBlock); |
| Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 9312 | if (RequiresOuterTask) { |
| 9313 | CapturedVars.clear(); |
| 9314 | CGF.GenerateOpenMPCapturedVars(CS, CapturedVars); |
| 9315 | } |
| Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 9316 | emitOutlinedFunctionCall(CGF, D.getBeginLoc(), OutlinedFn, CapturedVars); |
| Alexey Bataev | 2a007e0 | 2017-10-02 14:20:58 +0000 | [diff] [blame] | 9317 | CGF.EmitBranch(OffloadContBlock); |
| 9318 | |
| 9319 | CGF.EmitBlock(OffloadContBlock, /*IsFinished=*/true); |
| Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 9320 | }; |
| 9321 | |
| Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 9322 | // Notify that the host version must be executed. |
| Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 9323 | auto &&ElseGen = [this, &D, OutlinedFn, &CS, &CapturedVars, |
| 9324 | RequiresOuterTask](CodeGenFunction &CGF, |
| 9325 | PrePostActionTy &) { |
| 9326 | if (RequiresOuterTask) { |
| 9327 | CapturedVars.clear(); |
| 9328 | CGF.GenerateOpenMPCapturedVars(CS, CapturedVars); |
| 9329 | } |
| Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 9330 | emitOutlinedFunctionCall(CGF, D.getBeginLoc(), OutlinedFn, CapturedVars); |
| Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 9331 | }; |
| 9332 | |
| 9333 | auto &&TargetThenGen = [this, &ThenGen, &D, &InputInfo, &MapTypesArray, |
| 9334 | &CapturedVars, RequiresOuterTask, |
| 9335 | &CS](CodeGenFunction &CGF, PrePostActionTy &) { |
| 9336 | // Fill up the arrays with all the captured variables. |
| 9337 | MappableExprsHandler::MapBaseValuesArrayTy BasePointers; |
| 9338 | MappableExprsHandler::MapValuesArrayTy Pointers; |
| 9339 | MappableExprsHandler::MapValuesArrayTy Sizes; |
| 9340 | MappableExprsHandler::MapFlagsArrayTy MapTypes; |
| 9341 | |
| Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 9342 | // Get mappable expression information. |
| 9343 | MappableExprsHandler MEHandler(D, CGF); |
| Alexey Bataev | 969dbc0 | 2018-11-08 15:47:39 +0000 | [diff] [blame] | 9344 | llvm::DenseMap<llvm::Value *, llvm::Value *> LambdaPointers; |
| Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 9345 | |
| 9346 | auto RI = CS.getCapturedRecordDecl()->field_begin(); |
| 9347 | auto CV = CapturedVars.begin(); |
| 9348 | for (CapturedStmt::const_capture_iterator CI = CS.capture_begin(), |
| 9349 | CE = CS.capture_end(); |
| 9350 | CI != CE; ++CI, ++RI, ++CV) { |
| Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 9351 | MappableExprsHandler::MapBaseValuesArrayTy CurBasePointers; |
| 9352 | MappableExprsHandler::MapValuesArrayTy CurPointers; |
| 9353 | MappableExprsHandler::MapValuesArrayTy CurSizes; |
| 9354 | MappableExprsHandler::MapFlagsArrayTy CurMapTypes; |
| 9355 | MappableExprsHandler::StructRangeInfoTy PartialStruct; |
| Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 9356 | |
| 9357 | // VLA sizes are passed to the outlined region by copy and do not have map |
| 9358 | // information associated. |
| 9359 | if (CI->capturesVariableArrayType()) { |
| 9360 | CurBasePointers.push_back(*CV); |
| 9361 | CurPointers.push_back(*CV); |
| Alexey Bataev | a90fc66 | 2019-06-25 16:00:43 +0000 | [diff] [blame] | 9362 | CurSizes.push_back(CGF.Builder.CreateIntCast( |
| 9363 | CGF.getTypeSize(RI->getType()), CGF.Int64Ty, /*isSigned=*/true)); |
| Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 9364 | // Copy to the device as an argument. No need to retrieve it. |
| 9365 | CurMapTypes.push_back(MappableExprsHandler::OMP_MAP_LITERAL | |
| Alexey Bataev | f288cf9 | 2019-06-27 18:53:07 +0000 | [diff] [blame] | 9366 | MappableExprsHandler::OMP_MAP_TARGET_PARAM | |
| 9367 | MappableExprsHandler::OMP_MAP_IMPLICIT); |
| Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 9368 | } else { |
| 9369 | // If we have any information in the map clause, we use it, otherwise we |
| 9370 | // just do a default mapping. |
| 9371 | MEHandler.generateInfoForCapture(CI, *CV, CurBasePointers, CurPointers, |
| Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 9372 | CurSizes, CurMapTypes, PartialStruct); |
| Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 9373 | if (CurBasePointers.empty()) |
| 9374 | MEHandler.generateDefaultMapInfo(*CI, **RI, *CV, CurBasePointers, |
| 9375 | CurPointers, CurSizes, CurMapTypes); |
| Alexey Bataev | 6070542 | 2018-10-30 15:50:12 +0000 | [diff] [blame] | 9376 | // Generate correct mapping for variables captured by reference in |
| 9377 | // lambdas. |
| 9378 | if (CI->capturesVariable()) |
| Alexey Bataev | 969dbc0 | 2018-11-08 15:47:39 +0000 | [diff] [blame] | 9379 | MEHandler.generateInfoForLambdaCaptures( |
| 9380 | CI->getCapturedVar(), *CV, CurBasePointers, CurPointers, CurSizes, |
| 9381 | CurMapTypes, LambdaPointers); |
| Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 9382 | } |
| 9383 | // We expect to have at least an element of information for this capture. |
| 9384 | assert(!CurBasePointers.empty() && |
| 9385 | "Non-existing map pointer for capture!"); |
| 9386 | assert(CurBasePointers.size() == CurPointers.size() && |
| 9387 | CurBasePointers.size() == CurSizes.size() && |
| 9388 | CurBasePointers.size() == CurMapTypes.size() && |
| 9389 | "Inconsistent map information sizes!"); |
| 9390 | |
| Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 9391 | // If there is an entry in PartialStruct it means we have a struct with |
| 9392 | // individual members mapped. Emit an extra combined entry. |
| 9393 | if (PartialStruct.Base.isValid()) |
| 9394 | MEHandler.emitCombinedEntry(BasePointers, Pointers, Sizes, MapTypes, |
| 9395 | CurMapTypes, PartialStruct); |
| 9396 | |
| Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 9397 | // We need to append the results of this capture to what we already have. |
| 9398 | BasePointers.append(CurBasePointers.begin(), CurBasePointers.end()); |
| 9399 | Pointers.append(CurPointers.begin(), CurPointers.end()); |
| 9400 | Sizes.append(CurSizes.begin(), CurSizes.end()); |
| 9401 | MapTypes.append(CurMapTypes.begin(), CurMapTypes.end()); |
| 9402 | } |
| Alexey Bataev | 6070542 | 2018-10-30 15:50:12 +0000 | [diff] [blame] | 9403 | // Adjust MEMBER_OF flags for the lambdas captures. |
| Alexey Bataev | 969dbc0 | 2018-11-08 15:47:39 +0000 | [diff] [blame] | 9404 | MEHandler.adjustMemberOfForLambdaCaptures(LambdaPointers, BasePointers, |
| 9405 | Pointers, MapTypes); |
| Alexey Bataev | 92327c5 | 2018-03-26 16:40:55 +0000 | [diff] [blame] | 9406 | // Map other list items in the map clause which are not captured variables |
| 9407 | // but "declare target link" global variables. |
| Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 9408 | MEHandler.generateInfoForDeclareTargetLink(BasePointers, Pointers, Sizes, |
| 9409 | MapTypes); |
| Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 9410 | |
| 9411 | TargetDataInfo Info; |
| 9412 | // Fill up the arrays and create the arguments. |
| 9413 | emitOffloadingArrays(CGF, BasePointers, Pointers, Sizes, MapTypes, Info); |
| 9414 | emitOffloadingArraysArgument(CGF, Info.BasePointersArray, |
| 9415 | Info.PointersArray, Info.SizesArray, |
| 9416 | Info.MapTypesArray, Info); |
| 9417 | InputInfo.NumberOfTargetItems = Info.NumberOfPtrs; |
| 9418 | InputInfo.BasePointersArray = |
| 9419 | Address(Info.BasePointersArray, CGM.getPointerAlign()); |
| 9420 | InputInfo.PointersArray = |
| 9421 | Address(Info.PointersArray, CGM.getPointerAlign()); |
| 9422 | InputInfo.SizesArray = Address(Info.SizesArray, CGM.getPointerAlign()); |
| 9423 | MapTypesArray = Info.MapTypesArray; |
| 9424 | if (RequiresOuterTask) |
| 9425 | CGF.EmitOMPTargetTaskBasedDirective(D, ThenGen, InputInfo); |
| 9426 | else |
| 9427 | emitInlinedDirective(CGF, D.getDirectiveKind(), ThenGen); |
| 9428 | }; |
| 9429 | |
| 9430 | auto &&TargetElseGen = [this, &ElseGen, &D, RequiresOuterTask]( |
| 9431 | CodeGenFunction &CGF, PrePostActionTy &) { |
| 9432 | if (RequiresOuterTask) { |
| 9433 | CodeGenFunction::OMPTargetDataInfo InputInfo; |
| 9434 | CGF.EmitOMPTargetTaskBasedDirective(D, ElseGen, InputInfo); |
| 9435 | } else { |
| 9436 | emitInlinedDirective(CGF, D.getDirectiveKind(), ElseGen); |
| 9437 | } |
| Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 9438 | }; |
| 9439 | |
| 9440 | // If we have a target function ID it means that we need to support |
| 9441 | // offloading, otherwise, just execute on the host. We need to execute on host |
| 9442 | // regardless of the conditional in the if clause if, e.g., the user do not |
| 9443 | // specify target triples. |
| 9444 | if (OutlinedFnID) { |
| Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 9445 | if (IfCond) { |
| Alexey Bataev | 1d943ae | 2019-11-19 12:12:23 -0500 | [diff] [blame] | 9446 | emitIfClause(CGF, IfCond, TargetThenGen, TargetElseGen); |
| Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 9447 | } else { |
| 9448 | RegionCodeGenTy ThenRCG(TargetThenGen); |
| Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 9449 | ThenRCG(CGF); |
| Alexey Bataev | f539faa | 2016-03-28 12:58:34 +0000 | [diff] [blame] | 9450 | } |
| 9451 | } else { |
| Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 9452 | RegionCodeGenTy ElseRCG(TargetElseGen); |
| Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 9453 | ElseRCG(CGF); |
| Alexey Bataev | f539faa | 2016-03-28 12:58:34 +0000 | [diff] [blame] | 9454 | } |
| Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 9455 | } |
| Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 9456 | |
| 9457 | void CGOpenMPRuntime::scanForTargetRegionsFunctions(const Stmt *S, |
| 9458 | StringRef ParentName) { |
| 9459 | if (!S) |
| 9460 | return; |
| 9461 | |
| Arpith Chacko Jacob | 43a8b7b | 2017-01-16 15:26:02 +0000 | [diff] [blame] | 9462 | // Codegen OMP target directives that offload compute to the device. |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 9463 | bool RequiresDeviceCodegen = |
| Arpith Chacko Jacob | 43a8b7b | 2017-01-16 15:26:02 +0000 | [diff] [blame] | 9464 | isa<OMPExecutableDirective>(S) && |
| 9465 | isOpenMPTargetExecutionDirective( |
| 9466 | cast<OMPExecutableDirective>(S)->getDirectiveKind()); |
| Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 9467 | |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 9468 | if (RequiresDeviceCodegen) { |
| 9469 | const auto &E = *cast<OMPExecutableDirective>(S); |
| Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 9470 | unsigned DeviceID; |
| 9471 | unsigned FileID; |
| 9472 | unsigned Line; |
| Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 9473 | getTargetEntryUniqueInfo(CGM.getContext(), E.getBeginLoc(), DeviceID, |
| Samuel Antao | 2de62b0 | 2016-02-13 23:35:10 +0000 | [diff] [blame] | 9474 | FileID, Line); |
| Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 9475 | |
| 9476 | // Is this a target region that should not be emitted as an entry point? If |
| 9477 | // so just signal we are done with this target region. |
| Samuel Antao | 2de62b0 | 2016-02-13 23:35:10 +0000 | [diff] [blame] | 9478 | if (!OffloadEntriesInfoManager.hasTargetRegionEntryInfo(DeviceID, FileID, |
| 9479 | ParentName, Line)) |
| Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 9480 | return; |
| 9481 | |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 9482 | switch (E.getDirectiveKind()) { |
| 9483 | case OMPD_target: |
| 9484 | CodeGenFunction::EmitOMPTargetDeviceFunction(CGM, ParentName, |
| 9485 | cast<OMPTargetDirective>(E)); |
| Arpith Chacko Jacob | 43a8b7b | 2017-01-16 15:26:02 +0000 | [diff] [blame] | 9486 | break; |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 9487 | case OMPD_target_parallel: |
| Arpith Chacko Jacob | 19b911c | 2017-01-18 18:18:53 +0000 | [diff] [blame] | 9488 | CodeGenFunction::EmitOMPTargetParallelDeviceFunction( |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 9489 | CGM, ParentName, cast<OMPTargetParallelDirective>(E)); |
| Arpith Chacko Jacob | 19b911c | 2017-01-18 18:18:53 +0000 | [diff] [blame] | 9490 | break; |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 9491 | case OMPD_target_teams: |
| Arpith Chacko Jacob | 99a1e0e | 2017-01-25 02:18:43 +0000 | [diff] [blame] | 9492 | CodeGenFunction::EmitOMPTargetTeamsDeviceFunction( |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 9493 | CGM, ParentName, cast<OMPTargetTeamsDirective>(E)); |
| Arpith Chacko Jacob | 99a1e0e | 2017-01-25 02:18:43 +0000 | [diff] [blame] | 9494 | break; |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 9495 | case OMPD_target_teams_distribute: |
| Alexey Bataev | dfa430f | 2017-12-08 15:03:50 +0000 | [diff] [blame] | 9496 | CodeGenFunction::EmitOMPTargetTeamsDistributeDeviceFunction( |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 9497 | CGM, ParentName, cast<OMPTargetTeamsDistributeDirective>(E)); |
| Alexey Bataev | dfa430f | 2017-12-08 15:03:50 +0000 | [diff] [blame] | 9498 | break; |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 9499 | case OMPD_target_teams_distribute_simd: |
| Alexey Bataev | fbe17fb | 2017-12-13 19:45:06 +0000 | [diff] [blame] | 9500 | CodeGenFunction::EmitOMPTargetTeamsDistributeSimdDeviceFunction( |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 9501 | CGM, ParentName, cast<OMPTargetTeamsDistributeSimdDirective>(E)); |
| Alexey Bataev | fbe17fb | 2017-12-13 19:45:06 +0000 | [diff] [blame] | 9502 | break; |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 9503 | case OMPD_target_parallel_for: |
| Alexey Bataev | fb0ebec | 2017-11-08 20:16:14 +0000 | [diff] [blame] | 9504 | CodeGenFunction::EmitOMPTargetParallelForDeviceFunction( |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 9505 | CGM, ParentName, cast<OMPTargetParallelForDirective>(E)); |
| Alexey Bataev | fb0ebec | 2017-11-08 20:16:14 +0000 | [diff] [blame] | 9506 | break; |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 9507 | case OMPD_target_parallel_for_simd: |
| Alexey Bataev | 5d7edca | 2017-11-09 17:32:15 +0000 | [diff] [blame] | 9508 | CodeGenFunction::EmitOMPTargetParallelForSimdDeviceFunction( |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 9509 | CGM, ParentName, cast<OMPTargetParallelForSimdDirective>(E)); |
| Alexey Bataev | 5d7edca | 2017-11-09 17:32:15 +0000 | [diff] [blame] | 9510 | break; |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 9511 | case OMPD_target_simd: |
| Alexey Bataev | f836537 | 2017-11-17 17:57:25 +0000 | [diff] [blame] | 9512 | CodeGenFunction::EmitOMPTargetSimdDeviceFunction( |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 9513 | CGM, ParentName, cast<OMPTargetSimdDirective>(E)); |
| Alexey Bataev | f836537 | 2017-11-17 17:57:25 +0000 | [diff] [blame] | 9514 | break; |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 9515 | case OMPD_target_teams_distribute_parallel_for: |
| Carlo Bertolli | 52978c3 | 2018-01-03 21:12:44 +0000 | [diff] [blame] | 9516 | CodeGenFunction::EmitOMPTargetTeamsDistributeParallelForDeviceFunction( |
| 9517 | CGM, ParentName, |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 9518 | cast<OMPTargetTeamsDistributeParallelForDirective>(E)); |
| Carlo Bertolli | 52978c3 | 2018-01-03 21:12:44 +0000 | [diff] [blame] | 9519 | break; |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 9520 | case OMPD_target_teams_distribute_parallel_for_simd: |
| Alexey Bataev | 647dd84 | 2018-01-15 20:59:40 +0000 | [diff] [blame] | 9521 | CodeGenFunction:: |
| 9522 | EmitOMPTargetTeamsDistributeParallelForSimdDeviceFunction( |
| 9523 | CGM, ParentName, |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 9524 | cast<OMPTargetTeamsDistributeParallelForSimdDirective>(E)); |
| Alexey Bataev | 647dd84 | 2018-01-15 20:59:40 +0000 | [diff] [blame] | 9525 | break; |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 9526 | case OMPD_parallel: |
| 9527 | case OMPD_for: |
| 9528 | case OMPD_parallel_for: |
| cchen | 47d6094 | 2019-12-05 13:43:48 -0500 | [diff] [blame] | 9529 | case OMPD_parallel_master: |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 9530 | case OMPD_parallel_sections: |
| 9531 | case OMPD_for_simd: |
| 9532 | case OMPD_parallel_for_simd: |
| 9533 | case OMPD_cancel: |
| 9534 | case OMPD_cancellation_point: |
| 9535 | case OMPD_ordered: |
| 9536 | case OMPD_threadprivate: |
| Alexey Bataev | 25ed0c0 | 2019-03-07 17:54:44 +0000 | [diff] [blame] | 9537 | case OMPD_allocate: |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 9538 | case OMPD_task: |
| 9539 | case OMPD_simd: |
| 9540 | case OMPD_sections: |
| 9541 | case OMPD_section: |
| 9542 | case OMPD_single: |
| 9543 | case OMPD_master: |
| 9544 | case OMPD_critical: |
| 9545 | case OMPD_taskyield: |
| 9546 | case OMPD_barrier: |
| 9547 | case OMPD_taskwait: |
| 9548 | case OMPD_taskgroup: |
| 9549 | case OMPD_atomic: |
| 9550 | case OMPD_flush: |
| 9551 | case OMPD_teams: |
| 9552 | case OMPD_target_data: |
| 9553 | case OMPD_target_exit_data: |
| 9554 | case OMPD_target_enter_data: |
| 9555 | case OMPD_distribute: |
| 9556 | case OMPD_distribute_simd: |
| 9557 | case OMPD_distribute_parallel_for: |
| 9558 | case OMPD_distribute_parallel_for_simd: |
| 9559 | case OMPD_teams_distribute: |
| 9560 | case OMPD_teams_distribute_simd: |
| 9561 | case OMPD_teams_distribute_parallel_for: |
| 9562 | case OMPD_teams_distribute_parallel_for_simd: |
| 9563 | case OMPD_target_update: |
| 9564 | case OMPD_declare_simd: |
| Alexey Bataev | d158cf6 | 2019-09-13 20:18:17 +0000 | [diff] [blame] | 9565 | case OMPD_declare_variant: |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 9566 | case OMPD_declare_target: |
| 9567 | case OMPD_end_declare_target: |
| 9568 | case OMPD_declare_reduction: |
| Michael Kruse | 251e148 | 2019-02-01 20:25:04 +0000 | [diff] [blame] | 9569 | case OMPD_declare_mapper: |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 9570 | case OMPD_taskloop: |
| 9571 | case OMPD_taskloop_simd: |
| Alexey Bataev | 60e51c4 | 2019-10-10 20:13:02 +0000 | [diff] [blame] | 9572 | case OMPD_master_taskloop: |
| Alexey Bataev | b8552ab | 2019-10-18 16:47:35 +0000 | [diff] [blame] | 9573 | case OMPD_master_taskloop_simd: |
| Alexey Bataev | 5bbcead | 2019-10-14 17:17:41 +0000 | [diff] [blame] | 9574 | case OMPD_parallel_master_taskloop: |
| Alexey Bataev | 14a388f | 2019-10-25 10:27:13 -0400 | [diff] [blame] | 9575 | case OMPD_parallel_master_taskloop_simd: |
| Kelvin Li | 1408f91 | 2018-09-26 04:28:39 +0000 | [diff] [blame] | 9576 | case OMPD_requires: |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 9577 | case OMPD_unknown: |
| Arpith Chacko Jacob | 43a8b7b | 2017-01-16 15:26:02 +0000 | [diff] [blame] | 9578 | llvm_unreachable("Unknown target directive for OpenMP device codegen."); |
| 9579 | } |
| Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 9580 | return; |
| 9581 | } |
| 9582 | |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 9583 | if (const auto *E = dyn_cast<OMPExecutableDirective>(S)) { |
| Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 9584 | if (!E->hasAssociatedStmt() || !E->getAssociatedStmt()) |
| Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 9585 | return; |
| 9586 | |
| 9587 | scanForTargetRegionsFunctions( |
| Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 9588 | E->getInnermostCapturedStmt()->getCapturedStmt(), ParentName); |
| Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 9589 | return; |
| 9590 | } |
| 9591 | |
| 9592 | // If this is a lambda function, look into its body. |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 9593 | if (const auto *L = dyn_cast<LambdaExpr>(S)) |
| Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 9594 | S = L->getBody(); |
| 9595 | |
| 9596 | // Keep looking for target regions recursively. |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 9597 | for (const Stmt *II : S->children()) |
| Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 9598 | scanForTargetRegionsFunctions(II, ParentName); |
| Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 9599 | } |
| 9600 | |
| 9601 | bool CGOpenMPRuntime::emitTargetFunctions(GlobalDecl GD) { |
| Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 9602 | // If emitting code for the host, we do not process FD here. Instead we do |
| 9603 | // the normal code generation. |
| Alexey Bataev | 729e242 | 2019-08-23 16:11:14 +0000 | [diff] [blame] | 9604 | if (!CGM.getLangOpts().OpenMPIsDevice) { |
| 9605 | if (const auto *FD = dyn_cast<FunctionDecl>(GD.getDecl())) { |
| 9606 | Optional<OMPDeclareTargetDeclAttr::DevTypeTy> DevTy = |
| 9607 | OMPDeclareTargetDeclAttr::getDeviceType(FD); |
| 9608 | // Do not emit device_type(nohost) functions for the host. |
| 9609 | if (DevTy && *DevTy == OMPDeclareTargetDeclAttr::DT_NoHost) |
| 9610 | return true; |
| 9611 | } |
| Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 9612 | return false; |
| Alexey Bataev | 729e242 | 2019-08-23 16:11:14 +0000 | [diff] [blame] | 9613 | } |
| Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 9614 | |
| Alexey Bataev | e6aa469 | 2018-09-13 16:54:05 +0000 | [diff] [blame] | 9615 | const ValueDecl *VD = cast<ValueDecl>(GD.getDecl()); |
| Alexey Bataev | 2a6f3f5 | 2018-11-07 19:11:14 +0000 | [diff] [blame] | 9616 | // Try to detect target regions in the function. |
| Alexey Bataev | 729e242 | 2019-08-23 16:11:14 +0000 | [diff] [blame] | 9617 | if (const auto *FD = dyn_cast<FunctionDecl>(VD)) { |
| Alexey Bataev | 4558842 | 2020-01-07 14:11:45 -0500 | [diff] [blame] | 9618 | StringRef Name = CGM.getMangledName(GD); |
| Alexey Bataev | 2a6f3f5 | 2018-11-07 19:11:14 +0000 | [diff] [blame] | 9619 | scanForTargetRegionsFunctions(FD->getBody(), Name); |
| Alexey Bataev | 729e242 | 2019-08-23 16:11:14 +0000 | [diff] [blame] | 9620 | Optional<OMPDeclareTargetDeclAttr::DevTypeTy> DevTy = |
| 9621 | OMPDeclareTargetDeclAttr::getDeviceType(FD); |
| 9622 | // Do not emit device_type(nohost) functions for the host. |
| 9623 | if (DevTy && *DevTy == OMPDeclareTargetDeclAttr::DT_Host) |
| 9624 | return true; |
| 9625 | } |
| Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 9626 | |
| Alexey Bataev | 4f4bf7c | 2018-03-15 15:47:20 +0000 | [diff] [blame] | 9627 | // Do not to emit function if it is not marked as declare target. |
| Alexey Bataev | e6aa469 | 2018-09-13 16:54:05 +0000 | [diff] [blame] | 9628 | return !OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD) && |
| Alexey Bataev | 4558842 | 2020-01-07 14:11:45 -0500 | [diff] [blame] | 9629 | AlreadyEmittedTargetDecls.count(VD) == 0; |
| Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 9630 | } |
| 9631 | |
| 9632 | bool CGOpenMPRuntime::emitTargetGlobalVariable(GlobalDecl GD) { |
| 9633 | if (!CGM.getLangOpts().OpenMPIsDevice) |
| 9634 | return false; |
| 9635 | |
| 9636 | // Check if there are Ctors/Dtors in this declaration and look for target |
| 9637 | // regions in it. We use the complete variant to produce the kernel name |
| 9638 | // mangling. |
| 9639 | QualType RDTy = cast<VarDecl>(GD.getDecl())->getType(); |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 9640 | if (const auto *RD = RDTy->getBaseElementTypeUnsafe()->getAsCXXRecordDecl()) { |
| 9641 | for (const CXXConstructorDecl *Ctor : RD->ctors()) { |
| Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 9642 | StringRef ParentName = |
| 9643 | CGM.getMangledName(GlobalDecl(Ctor, Ctor_Complete)); |
| 9644 | scanForTargetRegionsFunctions(Ctor->getBody(), ParentName); |
| 9645 | } |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 9646 | if (const CXXDestructorDecl *Dtor = RD->getDestructor()) { |
| Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 9647 | StringRef ParentName = |
| 9648 | CGM.getMangledName(GlobalDecl(Dtor, Dtor_Complete)); |
| 9649 | scanForTargetRegionsFunctions(Dtor->getBody(), ParentName); |
| 9650 | } |
| 9651 | } |
| 9652 | |
| Alexey Bataev | 4f4bf7c | 2018-03-15 15:47:20 +0000 | [diff] [blame] | 9653 | // Do not to emit variable if it is not marked as declare target. |
| Alexey Bataev | 92327c5 | 2018-03-26 16:40:55 +0000 | [diff] [blame] | 9654 | llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res = |
| Alexey Bataev | 97b7221 | 2018-08-14 18:31:20 +0000 | [diff] [blame] | 9655 | OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration( |
| 9656 | cast<VarDecl>(GD.getDecl())); |
| Gheorghe-Teodor Bercea | 0034e84 | 2019-06-20 18:04:47 +0000 | [diff] [blame] | 9657 | if (!Res || *Res == OMPDeclareTargetDeclAttr::MT_Link || |
| 9658 | (*Res == OMPDeclareTargetDeclAttr::MT_To && |
| 9659 | HasRequiresUnifiedSharedMemory)) { |
| Alexey Bataev | d01b749 | 2018-08-15 19:45:12 +0000 | [diff] [blame] | 9660 | DeferredGlobalVariables.insert(cast<VarDecl>(GD.getDecl())); |
| Alexey Bataev | bf8fe71 | 2018-08-07 16:14:36 +0000 | [diff] [blame] | 9661 | return true; |
| 9662 | } |
| 9663 | return false; |
| Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 9664 | } |
| 9665 | |
| Alexey Bataev | 1af5bd5 | 2019-03-05 17:47:18 +0000 | [diff] [blame] | 9666 | llvm::Constant * |
| 9667 | CGOpenMPRuntime::registerTargetFirstprivateCopy(CodeGenFunction &CGF, |
| 9668 | const VarDecl *VD) { |
| 9669 | assert(VD->getType().isConstant(CGM.getContext()) && |
| 9670 | "Expected constant variable."); |
| 9671 | StringRef VarName; |
| 9672 | llvm::Constant *Addr; |
| 9673 | llvm::GlobalValue::LinkageTypes Linkage; |
| 9674 | QualType Ty = VD->getType(); |
| 9675 | SmallString<128> Buffer; |
| 9676 | { |
| 9677 | unsigned DeviceID; |
| 9678 | unsigned FileID; |
| 9679 | unsigned Line; |
| 9680 | getTargetEntryUniqueInfo(CGM.getContext(), VD->getLocation(), DeviceID, |
| 9681 | FileID, Line); |
| 9682 | llvm::raw_svector_ostream OS(Buffer); |
| 9683 | OS << "__omp_offloading_firstprivate_" << llvm::format("_%x", DeviceID) |
| 9684 | << llvm::format("_%x_", FileID) << VD->getName() << "_l" << Line; |
| 9685 | VarName = OS.str(); |
| 9686 | } |
| 9687 | Linkage = llvm::GlobalValue::InternalLinkage; |
| 9688 | Addr = |
| 9689 | getOrCreateInternalVariable(CGM.getTypes().ConvertTypeForMem(Ty), VarName, |
| 9690 | getDefaultFirstprivateAddressSpace()); |
| 9691 | cast<llvm::GlobalValue>(Addr)->setLinkage(Linkage); |
| 9692 | CharUnits VarSize = CGM.getContext().getTypeSizeInChars(Ty); |
| 9693 | CGM.addCompilerUsedGlobal(cast<llvm::GlobalValue>(Addr)); |
| 9694 | OffloadEntriesInfoManager.registerDeviceGlobalVarEntryInfo( |
| 9695 | VarName, Addr, VarSize, |
| 9696 | OffloadEntriesInfoManagerTy::OMPTargetGlobalVarEntryTo, Linkage); |
| 9697 | return Addr; |
| 9698 | } |
| 9699 | |
| Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 9700 | void CGOpenMPRuntime::registerTargetGlobalVariable(const VarDecl *VD, |
| 9701 | llvm::Constant *Addr) { |
| Alexey Bataev | 36724b7 | 2019-10-03 16:46:49 +0000 | [diff] [blame] | 9702 | if (CGM.getLangOpts().OMPTargetTriples.empty() && |
| 9703 | !CGM.getLangOpts().OpenMPIsDevice) |
| Alexey Bataev | 4db9dc6 | 2019-09-23 15:53:51 +0000 | [diff] [blame] | 9704 | return; |
| Alexey Bataev | 2a6f3f5 | 2018-11-07 19:11:14 +0000 | [diff] [blame] | 9705 | llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res = |
| 9706 | OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD); |
| 9707 | if (!Res) { |
| 9708 | if (CGM.getLangOpts().OpenMPIsDevice) { |
| 9709 | // Register non-target variables being emitted in device code (debug info |
| 9710 | // may cause this). |
| 9711 | StringRef VarName = CGM.getMangledName(VD); |
| 9712 | EmittedNonTargetVariables.try_emplace(VarName, Addr); |
| Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 9713 | } |
| Alexey Bataev | 2a6f3f5 | 2018-11-07 19:11:14 +0000 | [diff] [blame] | 9714 | return; |
| Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 9715 | } |
| Alexey Bataev | 2a6f3f5 | 2018-11-07 19:11:14 +0000 | [diff] [blame] | 9716 | // Register declare target variables. |
| 9717 | OffloadEntriesInfoManagerTy::OMPTargetGlobalVarEntryKind Flags; |
| 9718 | StringRef VarName; |
| 9719 | CharUnits VarSize; |
| 9720 | llvm::GlobalValue::LinkageTypes Linkage; |
| Gheorghe-Teodor Bercea | 0034e84 | 2019-06-20 18:04:47 +0000 | [diff] [blame] | 9721 | |
| 9722 | if (*Res == OMPDeclareTargetDeclAttr::MT_To && |
| 9723 | !HasRequiresUnifiedSharedMemory) { |
| Alexey Bataev | 2a6f3f5 | 2018-11-07 19:11:14 +0000 | [diff] [blame] | 9724 | Flags = OffloadEntriesInfoManagerTy::OMPTargetGlobalVarEntryTo; |
| 9725 | VarName = CGM.getMangledName(VD); |
| 9726 | if (VD->hasDefinition(CGM.getContext()) != VarDecl::DeclarationOnly) { |
| 9727 | VarSize = CGM.getContext().getTypeSizeInChars(VD->getType()); |
| 9728 | assert(!VarSize.isZero() && "Expected non-zero size of the variable"); |
| 9729 | } else { |
| 9730 | VarSize = CharUnits::Zero(); |
| 9731 | } |
| 9732 | Linkage = CGM.getLLVMLinkageVarDefinition(VD, /*IsConstant=*/false); |
| 9733 | // Temp solution to prevent optimizations of the internal variables. |
| 9734 | if (CGM.getLangOpts().OpenMPIsDevice && !VD->isExternallyVisible()) { |
| 9735 | std::string RefName = getName({VarName, "ref"}); |
| 9736 | if (!CGM.GetGlobalValue(RefName)) { |
| 9737 | llvm::Constant *AddrRef = |
| 9738 | getOrCreateInternalVariable(Addr->getType(), RefName); |
| 9739 | auto *GVAddrRef = cast<llvm::GlobalVariable>(AddrRef); |
| 9740 | GVAddrRef->setConstant(/*Val=*/true); |
| 9741 | GVAddrRef->setLinkage(llvm::GlobalValue::InternalLinkage); |
| 9742 | GVAddrRef->setInitializer(Addr); |
| 9743 | CGM.addCompilerUsedGlobal(GVAddrRef); |
| 9744 | } |
| 9745 | } |
| Gheorghe-Teodor Bercea | 0034e84 | 2019-06-20 18:04:47 +0000 | [diff] [blame] | 9746 | } else { |
| 9747 | assert(((*Res == OMPDeclareTargetDeclAttr::MT_Link) || |
| 9748 | (*Res == OMPDeclareTargetDeclAttr::MT_To && |
| 9749 | HasRequiresUnifiedSharedMemory)) && |
| 9750 | "Declare target attribute must link or to with unified memory."); |
| 9751 | if (*Res == OMPDeclareTargetDeclAttr::MT_Link) |
| 9752 | Flags = OffloadEntriesInfoManagerTy::OMPTargetGlobalVarEntryLink; |
| 9753 | else |
| 9754 | Flags = OffloadEntriesInfoManagerTy::OMPTargetGlobalVarEntryTo; |
| 9755 | |
| Alexey Bataev | 2a6f3f5 | 2018-11-07 19:11:14 +0000 | [diff] [blame] | 9756 | if (CGM.getLangOpts().OpenMPIsDevice) { |
| 9757 | VarName = Addr->getName(); |
| 9758 | Addr = nullptr; |
| 9759 | } else { |
| Gheorghe-Teodor Bercea | 0034e84 | 2019-06-20 18:04:47 +0000 | [diff] [blame] | 9760 | VarName = getAddrOfDeclareTargetVar(VD).getName(); |
| 9761 | Addr = cast<llvm::Constant>(getAddrOfDeclareTargetVar(VD).getPointer()); |
| Alexey Bataev | 2a6f3f5 | 2018-11-07 19:11:14 +0000 | [diff] [blame] | 9762 | } |
| 9763 | VarSize = CGM.getPointerSize(); |
| 9764 | Linkage = llvm::GlobalValue::WeakAnyLinkage; |
| Alexey Bataev | 2a6f3f5 | 2018-11-07 19:11:14 +0000 | [diff] [blame] | 9765 | } |
| Gheorghe-Teodor Bercea | 0034e84 | 2019-06-20 18:04:47 +0000 | [diff] [blame] | 9766 | |
| Alexey Bataev | 2a6f3f5 | 2018-11-07 19:11:14 +0000 | [diff] [blame] | 9767 | OffloadEntriesInfoManager.registerDeviceGlobalVarEntryInfo( |
| 9768 | VarName, Addr, VarSize, Flags, Linkage); |
| Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 9769 | } |
| 9770 | |
| Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 9771 | bool CGOpenMPRuntime::emitTargetGlobal(GlobalDecl GD) { |
| Alexey Bataev | e6aa469 | 2018-09-13 16:54:05 +0000 | [diff] [blame] | 9772 | if (isa<FunctionDecl>(GD.getDecl()) || |
| 9773 | isa<OMPDeclareReductionDecl>(GD.getDecl())) |
| Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 9774 | return emitTargetFunctions(GD); |
| 9775 | |
| 9776 | return emitTargetGlobalVariable(GD); |
| 9777 | } |
| 9778 | |
| Alexey Bataev | bf8fe71 | 2018-08-07 16:14:36 +0000 | [diff] [blame] | 9779 | void CGOpenMPRuntime::emitDeferredTargetDecls() const { |
| 9780 | for (const VarDecl *VD : DeferredGlobalVariables) { |
| 9781 | llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res = |
| Alexey Bataev | 97b7221 | 2018-08-14 18:31:20 +0000 | [diff] [blame] | 9782 | OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD); |
| Alexey Bataev | d01b749 | 2018-08-15 19:45:12 +0000 | [diff] [blame] | 9783 | if (!Res) |
| 9784 | continue; |
| Gheorghe-Teodor Bercea | 0034e84 | 2019-06-20 18:04:47 +0000 | [diff] [blame] | 9785 | if (*Res == OMPDeclareTargetDeclAttr::MT_To && |
| 9786 | !HasRequiresUnifiedSharedMemory) { |
| Alexey Bataev | bf8fe71 | 2018-08-07 16:14:36 +0000 | [diff] [blame] | 9787 | CGM.EmitGlobal(VD); |
| Alexey Bataev | d01b749 | 2018-08-15 19:45:12 +0000 | [diff] [blame] | 9788 | } else { |
| Gheorghe-Teodor Bercea | 0034e84 | 2019-06-20 18:04:47 +0000 | [diff] [blame] | 9789 | assert((*Res == OMPDeclareTargetDeclAttr::MT_Link || |
| 9790 | (*Res == OMPDeclareTargetDeclAttr::MT_To && |
| 9791 | HasRequiresUnifiedSharedMemory)) && |
| 9792 | "Expected link clause or to clause with unified memory."); |
| 9793 | (void)CGM.getOpenMPRuntime().getAddrOfDeclareTargetVar(VD); |
| Alexey Bataev | bf8fe71 | 2018-08-07 16:14:36 +0000 | [diff] [blame] | 9794 | } |
| 9795 | } |
| 9796 | } |
| 9797 | |
| Alexey Bataev | 6070542 | 2018-10-30 15:50:12 +0000 | [diff] [blame] | 9798 | void CGOpenMPRuntime::adjustTargetSpecificDataForLambdas( |
| 9799 | CodeGenFunction &CGF, const OMPExecutableDirective &D) const { |
| 9800 | assert(isOpenMPTargetExecutionDirective(D.getDirectiveKind()) && |
| 9801 | " Expected target-based directive."); |
| 9802 | } |
| 9803 | |
| Alexey Bataev | 2d4f80f | 2020-02-11 15:15:21 -0500 | [diff] [blame] | 9804 | void CGOpenMPRuntime::processRequiresDirective(const OMPRequiresDecl *D) { |
| Gheorghe-Teodor Bercea | 66cdbb47 | 2019-05-21 19:42:01 +0000 | [diff] [blame] | 9805 | for (const OMPClause *Clause : D->clauselists()) { |
| 9806 | if (Clause->getClauseKind() == OMPC_unified_shared_memory) { |
| 9807 | HasRequiresUnifiedSharedMemory = true; |
| Alexey Bataev | 2d4f80f | 2020-02-11 15:15:21 -0500 | [diff] [blame] | 9808 | } else if (const auto *AC = |
| 9809 | dyn_cast<OMPAtomicDefaultMemOrderClause>(Clause)) { |
| 9810 | switch (AC->getAtomicDefaultMemOrderKind()) { |
| 9811 | case OMPC_ATOMIC_DEFAULT_MEM_ORDER_acq_rel: |
| 9812 | RequiresAtomicOrdering = llvm::AtomicOrdering::AcquireRelease; |
| 9813 | break; |
| 9814 | case OMPC_ATOMIC_DEFAULT_MEM_ORDER_seq_cst: |
| 9815 | RequiresAtomicOrdering = llvm::AtomicOrdering::SequentiallyConsistent; |
| 9816 | break; |
| 9817 | case OMPC_ATOMIC_DEFAULT_MEM_ORDER_relaxed: |
| 9818 | RequiresAtomicOrdering = llvm::AtomicOrdering::Monotonic; |
| 9819 | break; |
| 9820 | case OMPC_ATOMIC_DEFAULT_MEM_ORDER_unknown: |
| 9821 | break; |
| 9822 | } |
| Gheorghe-Teodor Bercea | 66cdbb47 | 2019-05-21 19:42:01 +0000 | [diff] [blame] | 9823 | } |
| 9824 | } |
| 9825 | } |
| 9826 | |
| Alexey Bataev | 2d4f80f | 2020-02-11 15:15:21 -0500 | [diff] [blame] | 9827 | llvm::AtomicOrdering CGOpenMPRuntime::getDefaultMemoryOrdering() const { |
| 9828 | return RequiresAtomicOrdering; |
| 9829 | } |
| 9830 | |
| Alexey Bataev | c568725 | 2019-03-21 19:35:27 +0000 | [diff] [blame] | 9831 | bool CGOpenMPRuntime::hasAllocateAttributeForGlobalVar(const VarDecl *VD, |
| 9832 | LangAS &AS) { |
| 9833 | if (!VD || !VD->hasAttr<OMPAllocateDeclAttr>()) |
| 9834 | return false; |
| 9835 | const auto *A = VD->getAttr<OMPAllocateDeclAttr>(); |
| 9836 | switch(A->getAllocatorType()) { |
| 9837 | case OMPAllocateDeclAttr::OMPDefaultMemAlloc: |
| 9838 | // Not supported, fallback to the default mem space. |
| 9839 | case OMPAllocateDeclAttr::OMPLargeCapMemAlloc: |
| 9840 | case OMPAllocateDeclAttr::OMPCGroupMemAlloc: |
| 9841 | case OMPAllocateDeclAttr::OMPHighBWMemAlloc: |
| 9842 | case OMPAllocateDeclAttr::OMPLowLatMemAlloc: |
| 9843 | case OMPAllocateDeclAttr::OMPThreadMemAlloc: |
| 9844 | case OMPAllocateDeclAttr::OMPConstMemAlloc: |
| 9845 | case OMPAllocateDeclAttr::OMPPTeamMemAlloc: |
| 9846 | AS = LangAS::Default; |
| 9847 | return true; |
| 9848 | case OMPAllocateDeclAttr::OMPUserDefinedMemAlloc: |
| 9849 | llvm_unreachable("Expected predefined allocator for the variables with the " |
| 9850 | "static storage."); |
| 9851 | } |
| 9852 | return false; |
| 9853 | } |
| 9854 | |
| Gheorghe-Teodor Bercea | 5254f0a | 2019-06-14 17:58:26 +0000 | [diff] [blame] | 9855 | bool CGOpenMPRuntime::hasRequiresUnifiedSharedMemory() const { |
| 9856 | return HasRequiresUnifiedSharedMemory; |
| 9857 | } |
| 9858 | |
| Alexey Bataev | 4f4bf7c | 2018-03-15 15:47:20 +0000 | [diff] [blame] | 9859 | CGOpenMPRuntime::DisableAutoDeclareTargetRAII::DisableAutoDeclareTargetRAII( |
| 9860 | CodeGenModule &CGM) |
| 9861 | : CGM(CGM) { |
| 9862 | if (CGM.getLangOpts().OpenMPIsDevice) { |
| 9863 | SavedShouldMarkAsGlobal = CGM.getOpenMPRuntime().ShouldMarkAsGlobal; |
| 9864 | CGM.getOpenMPRuntime().ShouldMarkAsGlobal = false; |
| 9865 | } |
| 9866 | } |
| 9867 | |
| 9868 | CGOpenMPRuntime::DisableAutoDeclareTargetRAII::~DisableAutoDeclareTargetRAII() { |
| 9869 | if (CGM.getLangOpts().OpenMPIsDevice) |
| 9870 | CGM.getOpenMPRuntime().ShouldMarkAsGlobal = SavedShouldMarkAsGlobal; |
| 9871 | } |
| 9872 | |
| Alexey Bataev | 6d94410 | 2018-05-02 15:45:28 +0000 | [diff] [blame] | 9873 | bool CGOpenMPRuntime::markAsGlobalTarget(GlobalDecl GD) { |
| Alexey Bataev | 4f4bf7c | 2018-03-15 15:47:20 +0000 | [diff] [blame] | 9874 | if (!CGM.getLangOpts().OpenMPIsDevice || !ShouldMarkAsGlobal) |
| 9875 | return true; |
| Alexey Bataev | 4f4bf7c | 2018-03-15 15:47:20 +0000 | [diff] [blame] | 9876 | |
| Alexey Bataev | 6d94410 | 2018-05-02 15:45:28 +0000 | [diff] [blame] | 9877 | const auto *D = cast<FunctionDecl>(GD.getDecl()); |
| Alexey Bataev | 34f8a70 | 2018-03-28 14:28:54 +0000 | [diff] [blame] | 9878 | // Do not to emit function if it is marked as declare target as it was already |
| 9879 | // emitted. |
| Alexey Bataev | 97b7221 | 2018-08-14 18:31:20 +0000 | [diff] [blame] | 9880 | if (OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(D)) { |
| Alexey Bataev | 4558842 | 2020-01-07 14:11:45 -0500 | [diff] [blame] | 9881 | if (D->hasBody() && AlreadyEmittedTargetDecls.count(D) == 0) { |
| 9882 | if (auto *F = dyn_cast_or_null<llvm::Function>( |
| 9883 | CGM.GetGlobalValue(CGM.getMangledName(GD)))) |
| Alexey Bataev | 34f8a70 | 2018-03-28 14:28:54 +0000 | [diff] [blame] | 9884 | return !F->isDeclaration(); |
| 9885 | return false; |
| 9886 | } |
| 9887 | return true; |
| 9888 | } |
| 9889 | |
| Alexey Bataev | 4558842 | 2020-01-07 14:11:45 -0500 | [diff] [blame] | 9890 | return !AlreadyEmittedTargetDecls.insert(D).second; |
| Alexey Bataev | 4f4bf7c | 2018-03-15 15:47:20 +0000 | [diff] [blame] | 9891 | } |
| 9892 | |
| Gheorghe-Teodor Bercea | 66cdbb47 | 2019-05-21 19:42:01 +0000 | [diff] [blame] | 9893 | llvm::Function *CGOpenMPRuntime::emitRequiresDirectiveRegFun() { |
| 9894 | // If we don't have entries or if we are emitting code for the device, we |
| 9895 | // don't need to do anything. |
| 9896 | if (CGM.getLangOpts().OMPTargetTriples.empty() || |
| 9897 | CGM.getLangOpts().OpenMPSimd || CGM.getLangOpts().OpenMPIsDevice || |
| 9898 | (OffloadEntriesInfoManager.empty() && |
| 9899 | !HasEmittedDeclareTargetRegion && |
| 9900 | !HasEmittedTargetRegion)) |
| 9901 | return nullptr; |
| 9902 | |
| 9903 | // Create and register the function that handles the requires directives. |
| 9904 | ASTContext &C = CGM.getContext(); |
| 9905 | |
| 9906 | llvm::Function *RequiresRegFn; |
| 9907 | { |
| 9908 | CodeGenFunction CGF(CGM); |
| 9909 | const auto &FI = CGM.getTypes().arrangeNullaryFunction(); |
| 9910 | llvm::FunctionType *FTy = CGM.getTypes().GetFunctionType(FI); |
| 9911 | std::string ReqName = getName({"omp_offloading", "requires_reg"}); |
| 9912 | RequiresRegFn = CGM.CreateGlobalInitOrDestructFunction(FTy, ReqName, FI); |
| 9913 | CGF.StartFunction(GlobalDecl(), C.VoidTy, RequiresRegFn, FI, {}); |
| 9914 | OpenMPOffloadingRequiresDirFlags Flags = OMP_REQ_NONE; |
| 9915 | // TODO: check for other requires clauses. |
| 9916 | // The requires directive takes effect only when a target region is |
| 9917 | // present in the compilation unit. Otherwise it is ignored and not |
| 9918 | // passed to the runtime. This avoids the runtime from throwing an error |
| 9919 | // for mismatching requires clauses across compilation units that don't |
| 9920 | // contain at least 1 target region. |
| 9921 | assert((HasEmittedTargetRegion || |
| 9922 | HasEmittedDeclareTargetRegion || |
| 9923 | !OffloadEntriesInfoManager.empty()) && |
| 9924 | "Target or declare target region expected."); |
| 9925 | if (HasRequiresUnifiedSharedMemory) |
| 9926 | Flags = OMP_REQ_UNIFIED_SHARED_MEMORY; |
| 9927 | CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__tgt_register_requires), |
| 9928 | llvm::ConstantInt::get(CGM.Int64Ty, Flags)); |
| 9929 | CGF.FinishFunction(); |
| 9930 | } |
| 9931 | return RequiresRegFn; |
| 9932 | } |
| 9933 | |
| Carlo Bertolli | 430d8ec | 2016-03-03 20:34:23 +0000 | [diff] [blame] | 9934 | void CGOpenMPRuntime::emitTeamsCall(CodeGenFunction &CGF, |
| 9935 | const OMPExecutableDirective &D, |
| 9936 | SourceLocation Loc, |
| James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 9937 | llvm::Function *OutlinedFn, |
| Carlo Bertolli | 430d8ec | 2016-03-03 20:34:23 +0000 | [diff] [blame] | 9938 | ArrayRef<llvm::Value *> CapturedVars) { |
| 9939 | if (!CGF.HaveInsertPoint()) |
| 9940 | return; |
| 9941 | |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 9942 | llvm::Value *RTLoc = emitUpdateLocation(CGF, Loc); |
| Carlo Bertolli | 430d8ec | 2016-03-03 20:34:23 +0000 | [diff] [blame] | 9943 | CodeGenFunction::RunCleanupsScope Scope(CGF); |
| 9944 | |
| 9945 | // Build call __kmpc_fork_teams(loc, n, microtask, var1, .., varn); |
| 9946 | llvm::Value *Args[] = { |
| 9947 | RTLoc, |
| 9948 | CGF.Builder.getInt32(CapturedVars.size()), // Number of captured vars |
| 9949 | CGF.Builder.CreateBitCast(OutlinedFn, getKmpc_MicroPointerTy())}; |
| 9950 | llvm::SmallVector<llvm::Value *, 16> RealArgs; |
| 9951 | RealArgs.append(std::begin(Args), std::end(Args)); |
| 9952 | RealArgs.append(CapturedVars.begin(), CapturedVars.end()); |
| 9953 | |
| James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 9954 | llvm::FunctionCallee RTLFn = createRuntimeFunction(OMPRTL__kmpc_fork_teams); |
| Carlo Bertolli | 430d8ec | 2016-03-03 20:34:23 +0000 | [diff] [blame] | 9955 | CGF.EmitRuntimeCall(RTLFn, RealArgs); |
| 9956 | } |
| 9957 | |
| 9958 | void CGOpenMPRuntime::emitNumTeamsClause(CodeGenFunction &CGF, |
| Carlo Bertolli | c687225 | 2016-04-04 15:55:02 +0000 | [diff] [blame] | 9959 | const Expr *NumTeams, |
| 9960 | const Expr *ThreadLimit, |
| Carlo Bertolli | 430d8ec | 2016-03-03 20:34:23 +0000 | [diff] [blame] | 9961 | SourceLocation Loc) { |
| 9962 | if (!CGF.HaveInsertPoint()) |
| 9963 | return; |
| 9964 | |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 9965 | llvm::Value *RTLoc = emitUpdateLocation(CGF, Loc); |
| Carlo Bertolli | 430d8ec | 2016-03-03 20:34:23 +0000 | [diff] [blame] | 9966 | |
| Carlo Bertolli | c687225 | 2016-04-04 15:55:02 +0000 | [diff] [blame] | 9967 | llvm::Value *NumTeamsVal = |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 9968 | NumTeams |
| Carlo Bertolli | c687225 | 2016-04-04 15:55:02 +0000 | [diff] [blame] | 9969 | ? CGF.Builder.CreateIntCast(CGF.EmitScalarExpr(NumTeams), |
| 9970 | CGF.CGM.Int32Ty, /* isSigned = */ true) |
| 9971 | : CGF.Builder.getInt32(0); |
| 9972 | |
| 9973 | llvm::Value *ThreadLimitVal = |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 9974 | ThreadLimit |
| Carlo Bertolli | c687225 | 2016-04-04 15:55:02 +0000 | [diff] [blame] | 9975 | ? CGF.Builder.CreateIntCast(CGF.EmitScalarExpr(ThreadLimit), |
| 9976 | CGF.CGM.Int32Ty, /* isSigned = */ true) |
| 9977 | : CGF.Builder.getInt32(0); |
| 9978 | |
| Carlo Bertolli | 430d8ec | 2016-03-03 20:34:23 +0000 | [diff] [blame] | 9979 | // Build call __kmpc_push_num_teamss(&loc, global_tid, num_teams, thread_limit) |
| Carlo Bertolli | c687225 | 2016-04-04 15:55:02 +0000 | [diff] [blame] | 9980 | llvm::Value *PushNumTeamsArgs[] = {RTLoc, getThreadID(CGF, Loc), NumTeamsVal, |
| 9981 | ThreadLimitVal}; |
| Carlo Bertolli | 430d8ec | 2016-03-03 20:34:23 +0000 | [diff] [blame] | 9982 | CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_push_num_teams), |
| 9983 | PushNumTeamsArgs); |
| 9984 | } |
| Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 9985 | |
| Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 9986 | void CGOpenMPRuntime::emitTargetDataCalls( |
| 9987 | CodeGenFunction &CGF, const OMPExecutableDirective &D, const Expr *IfCond, |
| 9988 | const Expr *Device, const RegionCodeGenTy &CodeGen, TargetDataInfo &Info) { |
| Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 9989 | if (!CGF.HaveInsertPoint()) |
| 9990 | return; |
| 9991 | |
| Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 9992 | // Action used to replace the default codegen action and turn privatization |
| 9993 | // off. |
| 9994 | PrePostActionTy NoPrivAction; |
| Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 9995 | |
| 9996 | // Generate the code for the opening of the data environment. Capture all the |
| 9997 | // arguments of the runtime call by reference because they are used in the |
| 9998 | // closing of the region. |
| Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 9999 | auto &&BeginThenGen = [this, &D, Device, &Info, |
| 10000 | &CodeGen](CodeGenFunction &CGF, PrePostActionTy &) { |
| Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 10001 | // Fill up the arrays with all the mapped variables. |
| Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 10002 | MappableExprsHandler::MapBaseValuesArrayTy BasePointers; |
| Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 10003 | MappableExprsHandler::MapValuesArrayTy Pointers; |
| 10004 | MappableExprsHandler::MapValuesArrayTy Sizes; |
| 10005 | MappableExprsHandler::MapFlagsArrayTy MapTypes; |
| 10006 | |
| 10007 | // Get map clause information. |
| 10008 | MappableExprsHandler MCHandler(D, CGF); |
| 10009 | MCHandler.generateAllInfo(BasePointers, Pointers, Sizes, MapTypes); |
| Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 10010 | |
| 10011 | // Fill up the arrays and create the arguments. |
| Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 10012 | emitOffloadingArrays(CGF, BasePointers, Pointers, Sizes, MapTypes, Info); |
| Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 10013 | |
| 10014 | llvm::Value *BasePointersArrayArg = nullptr; |
| 10015 | llvm::Value *PointersArrayArg = nullptr; |
| 10016 | llvm::Value *SizesArrayArg = nullptr; |
| 10017 | llvm::Value *MapTypesArrayArg = nullptr; |
| 10018 | emitOffloadingArraysArgument(CGF, BasePointersArrayArg, PointersArrayArg, |
| Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 10019 | SizesArrayArg, MapTypesArrayArg, Info); |
| Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 10020 | |
| 10021 | // Emit device ID if any. |
| 10022 | llvm::Value *DeviceID = nullptr; |
| George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 10023 | if (Device) { |
| Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 10024 | DeviceID = CGF.Builder.CreateIntCast(CGF.EmitScalarExpr(Device), |
| George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 10025 | CGF.Int64Ty, /*isSigned=*/true); |
| 10026 | } else { |
| 10027 | DeviceID = CGF.Builder.getInt64(OMP_DEVICEID_UNDEF); |
| 10028 | } |
| Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 10029 | |
| 10030 | // Emit the number of elements in the offloading arrays. |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 10031 | llvm::Value *PointerNum = CGF.Builder.getInt32(Info.NumberOfPtrs); |
| Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 10032 | |
| 10033 | llvm::Value *OffloadingArgs[] = { |
| 10034 | DeviceID, PointerNum, BasePointersArrayArg, |
| 10035 | PointersArrayArg, SizesArrayArg, MapTypesArrayArg}; |
| Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 10036 | CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__tgt_target_data_begin), |
| Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 10037 | OffloadingArgs); |
| Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 10038 | |
| 10039 | // If device pointer privatization is required, emit the body of the region |
| 10040 | // here. It will have to be duplicated: with and without privatization. |
| 10041 | if (!Info.CaptureDeviceAddrMap.empty()) |
| 10042 | CodeGen(CGF); |
| Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 10043 | }; |
| 10044 | |
| 10045 | // Generate code for the closing of the data region. |
| Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 10046 | auto &&EndThenGen = [this, Device, &Info](CodeGenFunction &CGF, |
| 10047 | PrePostActionTy &) { |
| Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 10048 | assert(Info.isValid() && "Invalid data environment closing arguments."); |
| Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 10049 | |
| 10050 | llvm::Value *BasePointersArrayArg = nullptr; |
| 10051 | llvm::Value *PointersArrayArg = nullptr; |
| 10052 | llvm::Value *SizesArrayArg = nullptr; |
| 10053 | llvm::Value *MapTypesArrayArg = nullptr; |
| 10054 | emitOffloadingArraysArgument(CGF, BasePointersArrayArg, PointersArrayArg, |
| Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 10055 | SizesArrayArg, MapTypesArrayArg, Info); |
| Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 10056 | |
| 10057 | // Emit device ID if any. |
| 10058 | llvm::Value *DeviceID = nullptr; |
| George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 10059 | if (Device) { |
| Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 10060 | DeviceID = CGF.Builder.CreateIntCast(CGF.EmitScalarExpr(Device), |
| George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 10061 | CGF.Int64Ty, /*isSigned=*/true); |
| 10062 | } else { |
| 10063 | DeviceID = CGF.Builder.getInt64(OMP_DEVICEID_UNDEF); |
| 10064 | } |
| Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 10065 | |
| 10066 | // Emit the number of elements in the offloading arrays. |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 10067 | llvm::Value *PointerNum = CGF.Builder.getInt32(Info.NumberOfPtrs); |
| Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 10068 | |
| 10069 | llvm::Value *OffloadingArgs[] = { |
| 10070 | DeviceID, PointerNum, BasePointersArrayArg, |
| 10071 | PointersArrayArg, SizesArrayArg, MapTypesArrayArg}; |
| Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 10072 | CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__tgt_target_data_end), |
| Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 10073 | OffloadingArgs); |
| 10074 | }; |
| 10075 | |
| Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 10076 | // If we need device pointer privatization, we need to emit the body of the |
| 10077 | // region with no privatization in the 'else' branch of the conditional. |
| 10078 | // Otherwise, we don't have to do anything. |
| 10079 | auto &&BeginElseGen = [&Info, &CodeGen, &NoPrivAction](CodeGenFunction &CGF, |
| 10080 | PrePostActionTy &) { |
| 10081 | if (!Info.CaptureDeviceAddrMap.empty()) { |
| 10082 | CodeGen.setAction(NoPrivAction); |
| 10083 | CodeGen(CGF); |
| 10084 | } |
| 10085 | }; |
| 10086 | |
| 10087 | // We don't have to do anything to close the region if the if clause evaluates |
| 10088 | // to false. |
| 10089 | auto &&EndElseGen = [](CodeGenFunction &CGF, PrePostActionTy &) {}; |
| Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 10090 | |
| 10091 | if (IfCond) { |
| Alexey Bataev | 1d943ae | 2019-11-19 12:12:23 -0500 | [diff] [blame] | 10092 | emitIfClause(CGF, IfCond, BeginThenGen, BeginElseGen); |
| Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 10093 | } else { |
| Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 10094 | RegionCodeGenTy RCG(BeginThenGen); |
| 10095 | RCG(CGF); |
| Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 10096 | } |
| 10097 | |
| Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 10098 | // If we don't require privatization of device pointers, we emit the body in |
| 10099 | // between the runtime calls. This avoids duplicating the body code. |
| 10100 | if (Info.CaptureDeviceAddrMap.empty()) { |
| 10101 | CodeGen.setAction(NoPrivAction); |
| 10102 | CodeGen(CGF); |
| 10103 | } |
| Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 10104 | |
| 10105 | if (IfCond) { |
| Alexey Bataev | 1d943ae | 2019-11-19 12:12:23 -0500 | [diff] [blame] | 10106 | emitIfClause(CGF, IfCond, EndThenGen, EndElseGen); |
| Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 10107 | } else { |
| Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 10108 | RegionCodeGenTy RCG(EndThenGen); |
| 10109 | RCG(CGF); |
| Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 10110 | } |
| 10111 | } |
| Samuel Antao | bd0ae2e | 2016-04-27 23:07:29 +0000 | [diff] [blame] | 10112 | |
| Samuel Antao | 8d2d730 | 2016-05-26 18:30:22 +0000 | [diff] [blame] | 10113 | void CGOpenMPRuntime::emitTargetDataStandAloneCall( |
| Samuel Antao | 8dd6628 | 2016-04-27 23:14:30 +0000 | [diff] [blame] | 10114 | CodeGenFunction &CGF, const OMPExecutableDirective &D, const Expr *IfCond, |
| 10115 | const Expr *Device) { |
| Samuel Antao | bd0ae2e | 2016-04-27 23:07:29 +0000 | [diff] [blame] | 10116 | if (!CGF.HaveInsertPoint()) |
| 10117 | return; |
| 10118 | |
| Samuel Antao | 8dd6628 | 2016-04-27 23:14:30 +0000 | [diff] [blame] | 10119 | assert((isa<OMPTargetEnterDataDirective>(D) || |
| Samuel Antao | 8d2d730 | 2016-05-26 18:30:22 +0000 | [diff] [blame] | 10120 | isa<OMPTargetExitDataDirective>(D) || |
| 10121 | isa<OMPTargetUpdateDirective>(D)) && |
| 10122 | "Expecting either target enter, exit data, or update directives."); |
| Samuel Antao | 8dd6628 | 2016-04-27 23:14:30 +0000 | [diff] [blame] | 10123 | |
| Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 10124 | CodeGenFunction::OMPTargetDataInfo InputInfo; |
| 10125 | llvm::Value *MapTypesArray = nullptr; |
| Samuel Antao | bd0ae2e | 2016-04-27 23:07:29 +0000 | [diff] [blame] | 10126 | // Generate the code for the opening of the data environment. |
| Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 10127 | auto &&ThenGen = [this, &D, Device, &InputInfo, |
| 10128 | &MapTypesArray](CodeGenFunction &CGF, PrePostActionTy &) { |
| Samuel Antao | bd0ae2e | 2016-04-27 23:07:29 +0000 | [diff] [blame] | 10129 | // Emit device ID if any. |
| 10130 | llvm::Value *DeviceID = nullptr; |
| George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 10131 | if (Device) { |
| Samuel Antao | bd0ae2e | 2016-04-27 23:07:29 +0000 | [diff] [blame] | 10132 | DeviceID = CGF.Builder.CreateIntCast(CGF.EmitScalarExpr(Device), |
| George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 10133 | CGF.Int64Ty, /*isSigned=*/true); |
| 10134 | } else { |
| 10135 | DeviceID = CGF.Builder.getInt64(OMP_DEVICEID_UNDEF); |
| 10136 | } |
| Samuel Antao | bd0ae2e | 2016-04-27 23:07:29 +0000 | [diff] [blame] | 10137 | |
| 10138 | // Emit the number of elements in the offloading arrays. |
| Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 10139 | llvm::Constant *PointerNum = |
| 10140 | CGF.Builder.getInt32(InputInfo.NumberOfTargetItems); |
| Samuel Antao | bd0ae2e | 2016-04-27 23:07:29 +0000 | [diff] [blame] | 10141 | |
| Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 10142 | llvm::Value *OffloadingArgs[] = {DeviceID, |
| 10143 | PointerNum, |
| 10144 | InputInfo.BasePointersArray.getPointer(), |
| 10145 | InputInfo.PointersArray.getPointer(), |
| 10146 | InputInfo.SizesArray.getPointer(), |
| 10147 | MapTypesArray}; |
| Samuel Antao | 8d2d730 | 2016-05-26 18:30:22 +0000 | [diff] [blame] | 10148 | |
| Samuel Antao | 8d2d730 | 2016-05-26 18:30:22 +0000 | [diff] [blame] | 10149 | // Select the right runtime function call for each expected standalone |
| 10150 | // directive. |
| Alexey Bataev | 0cc6b8e | 2017-12-14 17:00:17 +0000 | [diff] [blame] | 10151 | const bool HasNowait = D.hasClausesOfKind<OMPNowaitClause>(); |
| Samuel Antao | 8d2d730 | 2016-05-26 18:30:22 +0000 | [diff] [blame] | 10152 | OpenMPRTLFunction RTLFn; |
| 10153 | switch (D.getDirectiveKind()) { |
| Samuel Antao | 8d2d730 | 2016-05-26 18:30:22 +0000 | [diff] [blame] | 10154 | case OMPD_target_enter_data: |
| Alexey Bataev | 0cc6b8e | 2017-12-14 17:00:17 +0000 | [diff] [blame] | 10155 | RTLFn = HasNowait ? OMPRTL__tgt_target_data_begin_nowait |
| 10156 | : OMPRTL__tgt_target_data_begin; |
| Samuel Antao | 8d2d730 | 2016-05-26 18:30:22 +0000 | [diff] [blame] | 10157 | break; |
| 10158 | case OMPD_target_exit_data: |
| Alexey Bataev | 0cc6b8e | 2017-12-14 17:00:17 +0000 | [diff] [blame] | 10159 | RTLFn = HasNowait ? OMPRTL__tgt_target_data_end_nowait |
| 10160 | : OMPRTL__tgt_target_data_end; |
| Samuel Antao | 8d2d730 | 2016-05-26 18:30:22 +0000 | [diff] [blame] | 10161 | break; |
| 10162 | case OMPD_target_update: |
| Alexey Bataev | 0cc6b8e | 2017-12-14 17:00:17 +0000 | [diff] [blame] | 10163 | RTLFn = HasNowait ? OMPRTL__tgt_target_data_update_nowait |
| 10164 | : OMPRTL__tgt_target_data_update; |
| Samuel Antao | 8d2d730 | 2016-05-26 18:30:22 +0000 | [diff] [blame] | 10165 | break; |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 10166 | case OMPD_parallel: |
| 10167 | case OMPD_for: |
| 10168 | case OMPD_parallel_for: |
| cchen | 47d6094 | 2019-12-05 13:43:48 -0500 | [diff] [blame] | 10169 | case OMPD_parallel_master: |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 10170 | case OMPD_parallel_sections: |
| 10171 | case OMPD_for_simd: |
| 10172 | case OMPD_parallel_for_simd: |
| 10173 | case OMPD_cancel: |
| 10174 | case OMPD_cancellation_point: |
| 10175 | case OMPD_ordered: |
| 10176 | case OMPD_threadprivate: |
| Alexey Bataev | 25ed0c0 | 2019-03-07 17:54:44 +0000 | [diff] [blame] | 10177 | case OMPD_allocate: |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 10178 | case OMPD_task: |
| 10179 | case OMPD_simd: |
| 10180 | case OMPD_sections: |
| 10181 | case OMPD_section: |
| 10182 | case OMPD_single: |
| 10183 | case OMPD_master: |
| 10184 | case OMPD_critical: |
| 10185 | case OMPD_taskyield: |
| 10186 | case OMPD_barrier: |
| 10187 | case OMPD_taskwait: |
| 10188 | case OMPD_taskgroup: |
| 10189 | case OMPD_atomic: |
| 10190 | case OMPD_flush: |
| 10191 | case OMPD_teams: |
| 10192 | case OMPD_target_data: |
| 10193 | case OMPD_distribute: |
| 10194 | case OMPD_distribute_simd: |
| 10195 | case OMPD_distribute_parallel_for: |
| 10196 | case OMPD_distribute_parallel_for_simd: |
| 10197 | case OMPD_teams_distribute: |
| 10198 | case OMPD_teams_distribute_simd: |
| 10199 | case OMPD_teams_distribute_parallel_for: |
| 10200 | case OMPD_teams_distribute_parallel_for_simd: |
| 10201 | case OMPD_declare_simd: |
| Alexey Bataev | d158cf6 | 2019-09-13 20:18:17 +0000 | [diff] [blame] | 10202 | case OMPD_declare_variant: |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 10203 | case OMPD_declare_target: |
| 10204 | case OMPD_end_declare_target: |
| 10205 | case OMPD_declare_reduction: |
| Michael Kruse | 251e148 | 2019-02-01 20:25:04 +0000 | [diff] [blame] | 10206 | case OMPD_declare_mapper: |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 10207 | case OMPD_taskloop: |
| 10208 | case OMPD_taskloop_simd: |
| Alexey Bataev | 60e51c4 | 2019-10-10 20:13:02 +0000 | [diff] [blame] | 10209 | case OMPD_master_taskloop: |
| Alexey Bataev | b8552ab | 2019-10-18 16:47:35 +0000 | [diff] [blame] | 10210 | case OMPD_master_taskloop_simd: |
| Alexey Bataev | 5bbcead | 2019-10-14 17:17:41 +0000 | [diff] [blame] | 10211 | case OMPD_parallel_master_taskloop: |
| Alexey Bataev | 14a388f | 2019-10-25 10:27:13 -0400 | [diff] [blame] | 10212 | case OMPD_parallel_master_taskloop_simd: |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 10213 | case OMPD_target: |
| 10214 | case OMPD_target_simd: |
| 10215 | case OMPD_target_teams_distribute: |
| 10216 | case OMPD_target_teams_distribute_simd: |
| 10217 | case OMPD_target_teams_distribute_parallel_for: |
| 10218 | case OMPD_target_teams_distribute_parallel_for_simd: |
| 10219 | case OMPD_target_teams: |
| 10220 | case OMPD_target_parallel: |
| 10221 | case OMPD_target_parallel_for: |
| 10222 | case OMPD_target_parallel_for_simd: |
| Kelvin Li | 1408f91 | 2018-09-26 04:28:39 +0000 | [diff] [blame] | 10223 | case OMPD_requires: |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 10224 | case OMPD_unknown: |
| 10225 | llvm_unreachable("Unexpected standalone target data directive."); |
| 10226 | break; |
| Samuel Antao | 8d2d730 | 2016-05-26 18:30:22 +0000 | [diff] [blame] | 10227 | } |
| Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 10228 | CGF.EmitRuntimeCall(createRuntimeFunction(RTLFn), OffloadingArgs); |
| Samuel Antao | bd0ae2e | 2016-04-27 23:07:29 +0000 | [diff] [blame] | 10229 | }; |
| 10230 | |
| Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 10231 | auto &&TargetThenGen = [this, &ThenGen, &D, &InputInfo, &MapTypesArray]( |
| 10232 | CodeGenFunction &CGF, PrePostActionTy &) { |
| 10233 | // Fill up the arrays with all the mapped variables. |
| 10234 | MappableExprsHandler::MapBaseValuesArrayTy BasePointers; |
| 10235 | MappableExprsHandler::MapValuesArrayTy Pointers; |
| 10236 | MappableExprsHandler::MapValuesArrayTy Sizes; |
| 10237 | MappableExprsHandler::MapFlagsArrayTy MapTypes; |
| Samuel Antao | bd0ae2e | 2016-04-27 23:07:29 +0000 | [diff] [blame] | 10238 | |
| Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 10239 | // Get map clause information. |
| 10240 | MappableExprsHandler MEHandler(D, CGF); |
| 10241 | MEHandler.generateAllInfo(BasePointers, Pointers, Sizes, MapTypes); |
| 10242 | |
| 10243 | TargetDataInfo Info; |
| 10244 | // Fill up the arrays and create the arguments. |
| 10245 | emitOffloadingArrays(CGF, BasePointers, Pointers, Sizes, MapTypes, Info); |
| 10246 | emitOffloadingArraysArgument(CGF, Info.BasePointersArray, |
| 10247 | Info.PointersArray, Info.SizesArray, |
| 10248 | Info.MapTypesArray, Info); |
| 10249 | InputInfo.NumberOfTargetItems = Info.NumberOfPtrs; |
| 10250 | InputInfo.BasePointersArray = |
| 10251 | Address(Info.BasePointersArray, CGM.getPointerAlign()); |
| 10252 | InputInfo.PointersArray = |
| 10253 | Address(Info.PointersArray, CGM.getPointerAlign()); |
| 10254 | InputInfo.SizesArray = |
| 10255 | Address(Info.SizesArray, CGM.getPointerAlign()); |
| 10256 | MapTypesArray = Info.MapTypesArray; |
| 10257 | if (D.hasClausesOfKind<OMPDependClause>()) |
| 10258 | CGF.EmitOMPTargetTaskBasedDirective(D, ThenGen, InputInfo); |
| 10259 | else |
| Alexey Bataev | 768f1f2 | 2018-01-09 19:59:25 +0000 | [diff] [blame] | 10260 | emitInlinedDirective(CGF, D.getDirectiveKind(), ThenGen); |
| Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 10261 | }; |
| 10262 | |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 10263 | if (IfCond) { |
| Alexey Bataev | 1d943ae | 2019-11-19 12:12:23 -0500 | [diff] [blame] | 10264 | emitIfClause(CGF, IfCond, TargetThenGen, |
| 10265 | [](CodeGenFunction &CGF, PrePostActionTy &) {}); |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 10266 | } else { |
| Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 10267 | RegionCodeGenTy ThenRCG(TargetThenGen); |
| 10268 | ThenRCG(CGF); |
| Samuel Antao | bd0ae2e | 2016-04-27 23:07:29 +0000 | [diff] [blame] | 10269 | } |
| 10270 | } |
| Alexey Bataev | c7a82b4 | 2016-05-06 09:40:08 +0000 | [diff] [blame] | 10271 | |
| 10272 | namespace { |
| 10273 | /// Kind of parameter in a function with 'declare simd' directive. |
| 10274 | enum ParamKindTy { LinearWithVarStride, Linear, Uniform, Vector }; |
| 10275 | /// Attribute set of the parameter. |
| 10276 | struct ParamAttrTy { |
| 10277 | ParamKindTy Kind = Vector; |
| 10278 | llvm::APSInt StrideOrArg; |
| 10279 | llvm::APSInt Alignment; |
| 10280 | }; |
| 10281 | } // namespace |
| 10282 | |
| 10283 | static unsigned evaluateCDTSize(const FunctionDecl *FD, |
| 10284 | ArrayRef<ParamAttrTy> ParamAttrs) { |
| 10285 | // Every vector variant of a SIMD-enabled function has a vector length (VLEN). |
| 10286 | // If OpenMP clause "simdlen" is used, the VLEN is the value of the argument |
| 10287 | // of that clause. The VLEN value must be power of 2. |
| 10288 | // In other case the notion of the function`s "characteristic data type" (CDT) |
| 10289 | // is used to compute the vector length. |
| 10290 | // CDT is defined in the following order: |
| 10291 | // a) For non-void function, the CDT is the return type. |
| 10292 | // b) If the function has any non-uniform, non-linear parameters, then the |
| 10293 | // CDT is the type of the first such parameter. |
| 10294 | // c) If the CDT determined by a) or b) above is struct, union, or class |
| 10295 | // type which is pass-by-value (except for the type that maps to the |
| 10296 | // built-in complex data type), the characteristic data type is int. |
| 10297 | // d) If none of the above three cases is applicable, the CDT is int. |
| 10298 | // The VLEN is then determined based on the CDT and the size of vector |
| 10299 | // register of that ISA for which current vector version is generated. The |
| 10300 | // VLEN is computed using the formula below: |
| 10301 | // VLEN = sizeof(vector_register) / sizeof(CDT), |
| 10302 | // where vector register size specified in section 3.2.1 Registers and the |
| 10303 | // Stack Frame of original AMD64 ABI document. |
| 10304 | QualType RetType = FD->getReturnType(); |
| 10305 | if (RetType.isNull()) |
| 10306 | return 0; |
| 10307 | ASTContext &C = FD->getASTContext(); |
| 10308 | QualType CDT; |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 10309 | if (!RetType.isNull() && !RetType->isVoidType()) { |
| Alexey Bataev | c7a82b4 | 2016-05-06 09:40:08 +0000 | [diff] [blame] | 10310 | CDT = RetType; |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 10311 | } else { |
| Alexey Bataev | c7a82b4 | 2016-05-06 09:40:08 +0000 | [diff] [blame] | 10312 | unsigned Offset = 0; |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 10313 | if (const auto *MD = dyn_cast<CXXMethodDecl>(FD)) { |
| Alexey Bataev | c7a82b4 | 2016-05-06 09:40:08 +0000 | [diff] [blame] | 10314 | if (ParamAttrs[Offset].Kind == Vector) |
| 10315 | CDT = C.getPointerType(C.getRecordType(MD->getParent())); |
| 10316 | ++Offset; |
| 10317 | } |
| 10318 | if (CDT.isNull()) { |
| 10319 | for (unsigned I = 0, E = FD->getNumParams(); I < E; ++I) { |
| 10320 | if (ParamAttrs[I + Offset].Kind == Vector) { |
| 10321 | CDT = FD->getParamDecl(I)->getType(); |
| 10322 | break; |
| 10323 | } |
| 10324 | } |
| 10325 | } |
| 10326 | } |
| 10327 | if (CDT.isNull()) |
| 10328 | CDT = C.IntTy; |
| 10329 | CDT = CDT->getCanonicalTypeUnqualified(); |
| 10330 | if (CDT->isRecordType() || CDT->isUnionType()) |
| 10331 | CDT = C.IntTy; |
| 10332 | return C.getTypeSize(CDT); |
| 10333 | } |
| 10334 | |
| 10335 | static void |
| 10336 | emitX86DeclareSimdFunction(const FunctionDecl *FD, llvm::Function *Fn, |
| Benjamin Kramer | 81cb4b7 | 2016-11-24 16:01:20 +0000 | [diff] [blame] | 10337 | const llvm::APSInt &VLENVal, |
| Alexey Bataev | c7a82b4 | 2016-05-06 09:40:08 +0000 | [diff] [blame] | 10338 | ArrayRef<ParamAttrTy> ParamAttrs, |
| 10339 | OMPDeclareSimdDeclAttr::BranchStateTy State) { |
| 10340 | struct ISADataTy { |
| 10341 | char ISA; |
| 10342 | unsigned VecRegSize; |
| 10343 | }; |
| 10344 | ISADataTy ISAData[] = { |
| 10345 | { |
| 10346 | 'b', 128 |
| 10347 | }, // SSE |
| 10348 | { |
| 10349 | 'c', 256 |
| 10350 | }, // AVX |
| 10351 | { |
| 10352 | 'd', 256 |
| 10353 | }, // AVX2 |
| 10354 | { |
| 10355 | 'e', 512 |
| 10356 | }, // AVX512 |
| 10357 | }; |
| 10358 | llvm::SmallVector<char, 2> Masked; |
| 10359 | switch (State) { |
| 10360 | case OMPDeclareSimdDeclAttr::BS_Undefined: |
| 10361 | Masked.push_back('N'); |
| 10362 | Masked.push_back('M'); |
| 10363 | break; |
| 10364 | case OMPDeclareSimdDeclAttr::BS_Notinbranch: |
| 10365 | Masked.push_back('N'); |
| 10366 | break; |
| 10367 | case OMPDeclareSimdDeclAttr::BS_Inbranch: |
| 10368 | Masked.push_back('M'); |
| 10369 | break; |
| 10370 | } |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 10371 | for (char Mask : Masked) { |
| 10372 | for (const ISADataTy &Data : ISAData) { |
| Alexey Bataev | c7a82b4 | 2016-05-06 09:40:08 +0000 | [diff] [blame] | 10373 | SmallString<256> Buffer; |
| 10374 | llvm::raw_svector_ostream Out(Buffer); |
| 10375 | Out << "_ZGV" << Data.ISA << Mask; |
| 10376 | if (!VLENVal) { |
| Simon Pilgrim | 823a99c | 2019-05-22 13:02:19 +0000 | [diff] [blame] | 10377 | unsigned NumElts = evaluateCDTSize(FD, ParamAttrs); |
| 10378 | assert(NumElts && "Non-zero simdlen/cdtsize expected"); |
| 10379 | Out << llvm::APSInt::getUnsigned(Data.VecRegSize / NumElts); |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 10380 | } else { |
| Alexey Bataev | c7a82b4 | 2016-05-06 09:40:08 +0000 | [diff] [blame] | 10381 | Out << VLENVal; |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 10382 | } |
| 10383 | for (const ParamAttrTy &ParamAttr : ParamAttrs) { |
| Alexey Bataev | c7a82b4 | 2016-05-06 09:40:08 +0000 | [diff] [blame] | 10384 | switch (ParamAttr.Kind){ |
| 10385 | case LinearWithVarStride: |
| 10386 | Out << 's' << ParamAttr.StrideOrArg; |
| 10387 | break; |
| 10388 | case Linear: |
| 10389 | Out << 'l'; |
| 10390 | if (!!ParamAttr.StrideOrArg) |
| 10391 | Out << ParamAttr.StrideOrArg; |
| 10392 | break; |
| 10393 | case Uniform: |
| 10394 | Out << 'u'; |
| 10395 | break; |
| 10396 | case Vector: |
| 10397 | Out << 'v'; |
| 10398 | break; |
| 10399 | } |
| 10400 | if (!!ParamAttr.Alignment) |
| 10401 | Out << 'a' << ParamAttr.Alignment; |
| 10402 | } |
| 10403 | Out << '_' << Fn->getName(); |
| 10404 | Fn->addFnAttr(Out.str()); |
| 10405 | } |
| 10406 | } |
| 10407 | } |
| 10408 | |
| Alexey Bataev | a0a2264 | 2019-04-16 13:56:21 +0000 | [diff] [blame] | 10409 | // This are the Functions that are needed to mangle the name of the |
| 10410 | // vector functions generated by the compiler, according to the rules |
| 10411 | // defined in the "Vector Function ABI specifications for AArch64", |
| 10412 | // available at |
| 10413 | // https://developer.arm.com/products/software-development-tools/hpc/arm-compiler-for-hpc/vector-function-abi. |
| 10414 | |
| 10415 | /// Maps To Vector (MTV), as defined in 3.1.1 of the AAVFABI. |
| 10416 | /// |
| 10417 | /// TODO: Need to implement the behavior for reference marked with a |
| 10418 | /// var or no linear modifiers (1.b in the section). For this, we |
| 10419 | /// need to extend ParamKindTy to support the linear modifiers. |
| 10420 | static bool getAArch64MTV(QualType QT, ParamKindTy Kind) { |
| 10421 | QT = QT.getCanonicalType(); |
| 10422 | |
| 10423 | if (QT->isVoidType()) |
| 10424 | return false; |
| 10425 | |
| 10426 | if (Kind == ParamKindTy::Uniform) |
| 10427 | return false; |
| 10428 | |
| 10429 | if (Kind == ParamKindTy::Linear) |
| 10430 | return false; |
| 10431 | |
| 10432 | // TODO: Handle linear references with modifiers |
| 10433 | |
| 10434 | if (Kind == ParamKindTy::LinearWithVarStride) |
| 10435 | return false; |
| 10436 | |
| 10437 | return true; |
| 10438 | } |
| 10439 | |
| 10440 | /// Pass By Value (PBV), as defined in 3.1.2 of the AAVFABI. |
| 10441 | static bool getAArch64PBV(QualType QT, ASTContext &C) { |
| 10442 | QT = QT.getCanonicalType(); |
| 10443 | unsigned Size = C.getTypeSize(QT); |
| 10444 | |
| 10445 | // Only scalars and complex within 16 bytes wide set PVB to true. |
| 10446 | if (Size != 8 && Size != 16 && Size != 32 && Size != 64 && Size != 128) |
| 10447 | return false; |
| 10448 | |
| 10449 | if (QT->isFloatingType()) |
| 10450 | return true; |
| 10451 | |
| 10452 | if (QT->isIntegerType()) |
| 10453 | return true; |
| 10454 | |
| 10455 | if (QT->isPointerType()) |
| 10456 | return true; |
| 10457 | |
| 10458 | // TODO: Add support for complex types (section 3.1.2, item 2). |
| 10459 | |
| 10460 | return false; |
| 10461 | } |
| 10462 | |
| 10463 | /// Computes the lane size (LS) of a return type or of an input parameter, |
| 10464 | /// as defined by `LS(P)` in 3.2.1 of the AAVFABI. |
| 10465 | /// TODO: Add support for references, section 3.2.1, item 1. |
| 10466 | static unsigned getAArch64LS(QualType QT, ParamKindTy Kind, ASTContext &C) { |
| 10467 | if (getAArch64MTV(QT, Kind) && QT.getCanonicalType()->isPointerType()) { |
| 10468 | QualType PTy = QT.getCanonicalType()->getPointeeType(); |
| 10469 | if (getAArch64PBV(PTy, C)) |
| 10470 | return C.getTypeSize(PTy); |
| 10471 | } |
| 10472 | if (getAArch64PBV(QT, C)) |
| 10473 | return C.getTypeSize(QT); |
| 10474 | |
| 10475 | return C.getTypeSize(C.getUIntPtrType()); |
| 10476 | } |
| 10477 | |
| 10478 | // Get Narrowest Data Size (NDS) and Widest Data Size (WDS) from the |
| 10479 | // signature of the scalar function, as defined in 3.2.2 of the |
| 10480 | // AAVFABI. |
| 10481 | static std::tuple<unsigned, unsigned, bool> |
| 10482 | getNDSWDS(const FunctionDecl *FD, ArrayRef<ParamAttrTy> ParamAttrs) { |
| 10483 | QualType RetType = FD->getReturnType().getCanonicalType(); |
| 10484 | |
| 10485 | ASTContext &C = FD->getASTContext(); |
| 10486 | |
| 10487 | bool OutputBecomesInput = false; |
| 10488 | |
| 10489 | llvm::SmallVector<unsigned, 8> Sizes; |
| 10490 | if (!RetType->isVoidType()) { |
| 10491 | Sizes.push_back(getAArch64LS(RetType, ParamKindTy::Vector, C)); |
| 10492 | if (!getAArch64PBV(RetType, C) && getAArch64MTV(RetType, {})) |
| 10493 | OutputBecomesInput = true; |
| 10494 | } |
| 10495 | for (unsigned I = 0, E = FD->getNumParams(); I < E; ++I) { |
| 10496 | QualType QT = FD->getParamDecl(I)->getType().getCanonicalType(); |
| 10497 | Sizes.push_back(getAArch64LS(QT, ParamAttrs[I].Kind, C)); |
| 10498 | } |
| 10499 | |
| 10500 | assert(!Sizes.empty() && "Unable to determine NDS and WDS."); |
| 10501 | // The LS of a function parameter / return value can only be a power |
| 10502 | // of 2, starting from 8 bits, up to 128. |
| 10503 | assert(std::all_of(Sizes.begin(), Sizes.end(), |
| 10504 | [](unsigned Size) { |
| 10505 | return Size == 8 || Size == 16 || Size == 32 || |
| 10506 | Size == 64 || Size == 128; |
| 10507 | }) && |
| 10508 | "Invalid size"); |
| 10509 | |
| 10510 | return std::make_tuple(*std::min_element(std::begin(Sizes), std::end(Sizes)), |
| 10511 | *std::max_element(std::begin(Sizes), std::end(Sizes)), |
| 10512 | OutputBecomesInput); |
| 10513 | } |
| 10514 | |
| 10515 | /// Mangle the parameter part of the vector function name according to |
| 10516 | /// their OpenMP classification. The mangling function is defined in |
| 10517 | /// section 3.5 of the AAVFABI. |
| 10518 | static std::string mangleVectorParameters(ArrayRef<ParamAttrTy> ParamAttrs) { |
| 10519 | SmallString<256> Buffer; |
| 10520 | llvm::raw_svector_ostream Out(Buffer); |
| 10521 | for (const auto &ParamAttr : ParamAttrs) { |
| 10522 | switch (ParamAttr.Kind) { |
| 10523 | case LinearWithVarStride: |
| 10524 | Out << "ls" << ParamAttr.StrideOrArg; |
| 10525 | break; |
| 10526 | case Linear: |
| 10527 | Out << 'l'; |
| 10528 | // Don't print the step value if it is not present or if it is |
| 10529 | // equal to 1. |
| 10530 | if (!!ParamAttr.StrideOrArg && ParamAttr.StrideOrArg != 1) |
| 10531 | Out << ParamAttr.StrideOrArg; |
| 10532 | break; |
| 10533 | case Uniform: |
| 10534 | Out << 'u'; |
| 10535 | break; |
| 10536 | case Vector: |
| 10537 | Out << 'v'; |
| 10538 | break; |
| 10539 | } |
| 10540 | |
| 10541 | if (!!ParamAttr.Alignment) |
| 10542 | Out << 'a' << ParamAttr.Alignment; |
| 10543 | } |
| 10544 | |
| Benjamin Kramer | adcd026 | 2020-01-28 20:23:46 +0100 | [diff] [blame] | 10545 | return std::string(Out.str()); |
| Alexey Bataev | a0a2264 | 2019-04-16 13:56:21 +0000 | [diff] [blame] | 10546 | } |
| 10547 | |
| 10548 | // Function used to add the attribute. The parameter `VLEN` is |
| 10549 | // templated to allow the use of "x" when targeting scalable functions |
| 10550 | // for SVE. |
| 10551 | template <typename T> |
| 10552 | static void addAArch64VectorName(T VLEN, StringRef LMask, StringRef Prefix, |
| 10553 | char ISA, StringRef ParSeq, |
| 10554 | StringRef MangledName, bool OutputBecomesInput, |
| 10555 | llvm::Function *Fn) { |
| 10556 | SmallString<256> Buffer; |
| 10557 | llvm::raw_svector_ostream Out(Buffer); |
| 10558 | Out << Prefix << ISA << LMask << VLEN; |
| 10559 | if (OutputBecomesInput) |
| 10560 | Out << "v"; |
| 10561 | Out << ParSeq << "_" << MangledName; |
| 10562 | Fn->addFnAttr(Out.str()); |
| 10563 | } |
| 10564 | |
| 10565 | // Helper function to generate the Advanced SIMD names depending on |
| 10566 | // the value of the NDS when simdlen is not present. |
| 10567 | static void addAArch64AdvSIMDNDSNames(unsigned NDS, StringRef Mask, |
| 10568 | StringRef Prefix, char ISA, |
| 10569 | StringRef ParSeq, StringRef MangledName, |
| 10570 | bool OutputBecomesInput, |
| 10571 | llvm::Function *Fn) { |
| 10572 | switch (NDS) { |
| 10573 | case 8: |
| 10574 | addAArch64VectorName(8, Mask, Prefix, ISA, ParSeq, MangledName, |
| 10575 | OutputBecomesInput, Fn); |
| 10576 | addAArch64VectorName(16, Mask, Prefix, ISA, ParSeq, MangledName, |
| 10577 | OutputBecomesInput, Fn); |
| 10578 | break; |
| 10579 | case 16: |
| 10580 | addAArch64VectorName(4, Mask, Prefix, ISA, ParSeq, MangledName, |
| 10581 | OutputBecomesInput, Fn); |
| 10582 | addAArch64VectorName(8, Mask, Prefix, ISA, ParSeq, MangledName, |
| 10583 | OutputBecomesInput, Fn); |
| 10584 | break; |
| 10585 | case 32: |
| 10586 | addAArch64VectorName(2, Mask, Prefix, ISA, ParSeq, MangledName, |
| 10587 | OutputBecomesInput, Fn); |
| 10588 | addAArch64VectorName(4, Mask, Prefix, ISA, ParSeq, MangledName, |
| 10589 | OutputBecomesInput, Fn); |
| 10590 | break; |
| 10591 | case 64: |
| 10592 | case 128: |
| 10593 | addAArch64VectorName(2, Mask, Prefix, ISA, ParSeq, MangledName, |
| 10594 | OutputBecomesInput, Fn); |
| 10595 | break; |
| 10596 | default: |
| 10597 | llvm_unreachable("Scalar type is too wide."); |
| 10598 | } |
| 10599 | } |
| 10600 | |
| 10601 | /// Emit vector function attributes for AArch64, as defined in the AAVFABI. |
| 10602 | static void emitAArch64DeclareSimdFunction( |
| 10603 | CodeGenModule &CGM, const FunctionDecl *FD, unsigned UserVLEN, |
| 10604 | ArrayRef<ParamAttrTy> ParamAttrs, |
| 10605 | OMPDeclareSimdDeclAttr::BranchStateTy State, StringRef MangledName, |
| 10606 | char ISA, unsigned VecRegSize, llvm::Function *Fn, SourceLocation SLoc) { |
| 10607 | |
| 10608 | // Get basic data for building the vector signature. |
| 10609 | const auto Data = getNDSWDS(FD, ParamAttrs); |
| 10610 | const unsigned NDS = std::get<0>(Data); |
| 10611 | const unsigned WDS = std::get<1>(Data); |
| 10612 | const bool OutputBecomesInput = std::get<2>(Data); |
| 10613 | |
| 10614 | // Check the values provided via `simdlen` by the user. |
| 10615 | // 1. A `simdlen(1)` doesn't produce vector signatures, |
| 10616 | if (UserVLEN == 1) { |
| 10617 | unsigned DiagID = CGM.getDiags().getCustomDiagID( |
| 10618 | DiagnosticsEngine::Warning, |
| 10619 | "The clause simdlen(1) has no effect when targeting aarch64."); |
| 10620 | CGM.getDiags().Report(SLoc, DiagID); |
| 10621 | return; |
| 10622 | } |
| 10623 | |
| 10624 | // 2. Section 3.3.1, item 1: user input must be a power of 2 for |
| 10625 | // Advanced SIMD output. |
| 10626 | if (ISA == 'n' && UserVLEN && !llvm::isPowerOf2_32(UserVLEN)) { |
| 10627 | unsigned DiagID = CGM.getDiags().getCustomDiagID( |
| 10628 | DiagnosticsEngine::Warning, "The value specified in simdlen must be a " |
| 10629 | "power of 2 when targeting Advanced SIMD."); |
| 10630 | CGM.getDiags().Report(SLoc, DiagID); |
| 10631 | return; |
| 10632 | } |
| 10633 | |
| 10634 | // 3. Section 3.4.1. SVE fixed lengh must obey the architectural |
| 10635 | // limits. |
| 10636 | if (ISA == 's' && UserVLEN != 0) { |
| 10637 | if ((UserVLEN * WDS > 2048) || (UserVLEN * WDS % 128 != 0)) { |
| 10638 | unsigned DiagID = CGM.getDiags().getCustomDiagID( |
| 10639 | DiagnosticsEngine::Warning, "The clause simdlen must fit the %0-bit " |
| 10640 | "lanes in the architectural constraints " |
| 10641 | "for SVE (min is 128-bit, max is " |
| 10642 | "2048-bit, by steps of 128-bit)"); |
| 10643 | CGM.getDiags().Report(SLoc, DiagID) << WDS; |
| 10644 | return; |
| 10645 | } |
| 10646 | } |
| 10647 | |
| 10648 | // Sort out parameter sequence. |
| 10649 | const std::string ParSeq = mangleVectorParameters(ParamAttrs); |
| 10650 | StringRef Prefix = "_ZGV"; |
| 10651 | // Generate simdlen from user input (if any). |
| 10652 | if (UserVLEN) { |
| 10653 | if (ISA == 's') { |
| 10654 | // SVE generates only a masked function. |
| 10655 | addAArch64VectorName(UserVLEN, "M", Prefix, ISA, ParSeq, MangledName, |
| 10656 | OutputBecomesInput, Fn); |
| 10657 | } else { |
| 10658 | assert(ISA == 'n' && "Expected ISA either 's' or 'n'."); |
| 10659 | // Advanced SIMD generates one or two functions, depending on |
| 10660 | // the `[not]inbranch` clause. |
| 10661 | switch (State) { |
| 10662 | case OMPDeclareSimdDeclAttr::BS_Undefined: |
| 10663 | addAArch64VectorName(UserVLEN, "N", Prefix, ISA, ParSeq, MangledName, |
| 10664 | OutputBecomesInput, Fn); |
| 10665 | addAArch64VectorName(UserVLEN, "M", Prefix, ISA, ParSeq, MangledName, |
| 10666 | OutputBecomesInput, Fn); |
| 10667 | break; |
| 10668 | case OMPDeclareSimdDeclAttr::BS_Notinbranch: |
| 10669 | addAArch64VectorName(UserVLEN, "N", Prefix, ISA, ParSeq, MangledName, |
| 10670 | OutputBecomesInput, Fn); |
| 10671 | break; |
| 10672 | case OMPDeclareSimdDeclAttr::BS_Inbranch: |
| 10673 | addAArch64VectorName(UserVLEN, "M", Prefix, ISA, ParSeq, MangledName, |
| 10674 | OutputBecomesInput, Fn); |
| 10675 | break; |
| 10676 | } |
| 10677 | } |
| 10678 | } else { |
| 10679 | // If no user simdlen is provided, follow the AAVFABI rules for |
| 10680 | // generating the vector length. |
| 10681 | if (ISA == 's') { |
| 10682 | // SVE, section 3.4.1, item 1. |
| 10683 | addAArch64VectorName("x", "M", Prefix, ISA, ParSeq, MangledName, |
| 10684 | OutputBecomesInput, Fn); |
| 10685 | } else { |
| 10686 | assert(ISA == 'n' && "Expected ISA either 's' or 'n'."); |
| 10687 | // Advanced SIMD, Section 3.3.1 of the AAVFABI, generates one or |
| 10688 | // two vector names depending on the use of the clause |
| 10689 | // `[not]inbranch`. |
| 10690 | switch (State) { |
| 10691 | case OMPDeclareSimdDeclAttr::BS_Undefined: |
| 10692 | addAArch64AdvSIMDNDSNames(NDS, "N", Prefix, ISA, ParSeq, MangledName, |
| 10693 | OutputBecomesInput, Fn); |
| 10694 | addAArch64AdvSIMDNDSNames(NDS, "M", Prefix, ISA, ParSeq, MangledName, |
| 10695 | OutputBecomesInput, Fn); |
| 10696 | break; |
| 10697 | case OMPDeclareSimdDeclAttr::BS_Notinbranch: |
| 10698 | addAArch64AdvSIMDNDSNames(NDS, "N", Prefix, ISA, ParSeq, MangledName, |
| 10699 | OutputBecomesInput, Fn); |
| 10700 | break; |
| 10701 | case OMPDeclareSimdDeclAttr::BS_Inbranch: |
| 10702 | addAArch64AdvSIMDNDSNames(NDS, "M", Prefix, ISA, ParSeq, MangledName, |
| 10703 | OutputBecomesInput, Fn); |
| 10704 | break; |
| 10705 | } |
| 10706 | } |
| 10707 | } |
| 10708 | } |
| 10709 | |
| Alexey Bataev | c7a82b4 | 2016-05-06 09:40:08 +0000 | [diff] [blame] | 10710 | void CGOpenMPRuntime::emitDeclareSimdFunction(const FunctionDecl *FD, |
| 10711 | llvm::Function *Fn) { |
| 10712 | ASTContext &C = CGM.getContext(); |
| Alexey Bataev | 20cf67c | 2018-03-02 18:07:00 +0000 | [diff] [blame] | 10713 | FD = FD->getMostRecentDecl(); |
| Alexey Bataev | c7a82b4 | 2016-05-06 09:40:08 +0000 | [diff] [blame] | 10714 | // Map params to their positions in function decl. |
| 10715 | llvm::DenseMap<const Decl *, unsigned> ParamPositions; |
| 10716 | if (isa<CXXMethodDecl>(FD)) |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 10717 | ParamPositions.try_emplace(FD, 0); |
| Alexey Bataev | c7a82b4 | 2016-05-06 09:40:08 +0000 | [diff] [blame] | 10718 | unsigned ParamPos = ParamPositions.size(); |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 10719 | for (const ParmVarDecl *P : FD->parameters()) { |
| 10720 | ParamPositions.try_emplace(P->getCanonicalDecl(), ParamPos); |
| Alexey Bataev | c7a82b4 | 2016-05-06 09:40:08 +0000 | [diff] [blame] | 10721 | ++ParamPos; |
| 10722 | } |
| Alexey Bataev | 20cf67c | 2018-03-02 18:07:00 +0000 | [diff] [blame] | 10723 | while (FD) { |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 10724 | for (const auto *Attr : FD->specific_attrs<OMPDeclareSimdDeclAttr>()) { |
| Alexey Bataev | 20cf67c | 2018-03-02 18:07:00 +0000 | [diff] [blame] | 10725 | llvm::SmallVector<ParamAttrTy, 8> ParamAttrs(ParamPositions.size()); |
| 10726 | // Mark uniform parameters. |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 10727 | for (const Expr *E : Attr->uniforms()) { |
| Alexey Bataev | 20cf67c | 2018-03-02 18:07:00 +0000 | [diff] [blame] | 10728 | E = E->IgnoreParenImpCasts(); |
| 10729 | unsigned Pos; |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 10730 | if (isa<CXXThisExpr>(E)) { |
| Alexey Bataev | 20cf67c | 2018-03-02 18:07:00 +0000 | [diff] [blame] | 10731 | Pos = ParamPositions[FD]; |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 10732 | } else { |
| 10733 | const auto *PVD = cast<ParmVarDecl>(cast<DeclRefExpr>(E)->getDecl()) |
| 10734 | ->getCanonicalDecl(); |
| Alexey Bataev | 20cf67c | 2018-03-02 18:07:00 +0000 | [diff] [blame] | 10735 | Pos = ParamPositions[PVD]; |
| 10736 | } |
| 10737 | ParamAttrs[Pos].Kind = Uniform; |
| Alexey Bataev | c7a82b4 | 2016-05-06 09:40:08 +0000 | [diff] [blame] | 10738 | } |
| Alexey Bataev | 20cf67c | 2018-03-02 18:07:00 +0000 | [diff] [blame] | 10739 | // Get alignment info. |
| 10740 | auto NI = Attr->alignments_begin(); |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 10741 | for (const Expr *E : Attr->aligneds()) { |
| Alexey Bataev | 20cf67c | 2018-03-02 18:07:00 +0000 | [diff] [blame] | 10742 | E = E->IgnoreParenImpCasts(); |
| 10743 | unsigned Pos; |
| 10744 | QualType ParmTy; |
| 10745 | if (isa<CXXThisExpr>(E)) { |
| 10746 | Pos = ParamPositions[FD]; |
| 10747 | ParmTy = E->getType(); |
| 10748 | } else { |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 10749 | const auto *PVD = cast<ParmVarDecl>(cast<DeclRefExpr>(E)->getDecl()) |
| 10750 | ->getCanonicalDecl(); |
| Alexey Bataev | 20cf67c | 2018-03-02 18:07:00 +0000 | [diff] [blame] | 10751 | Pos = ParamPositions[PVD]; |
| 10752 | ParmTy = PVD->getType(); |
| 10753 | } |
| 10754 | ParamAttrs[Pos].Alignment = |
| 10755 | (*NI) |
| 10756 | ? (*NI)->EvaluateKnownConstInt(C) |
| Alexey Bataev | c7a82b4 | 2016-05-06 09:40:08 +0000 | [diff] [blame] | 10757 | : llvm::APSInt::getUnsigned( |
| 10758 | C.toCharUnitsFromBits(C.getOpenMPDefaultSimdAlign(ParmTy)) |
| 10759 | .getQuantity()); |
| Alexey Bataev | 20cf67c | 2018-03-02 18:07:00 +0000 | [diff] [blame] | 10760 | ++NI; |
| Alexey Bataev | c7a82b4 | 2016-05-06 09:40:08 +0000 | [diff] [blame] | 10761 | } |
| Alexey Bataev | 20cf67c | 2018-03-02 18:07:00 +0000 | [diff] [blame] | 10762 | // Mark linear parameters. |
| 10763 | auto SI = Attr->steps_begin(); |
| 10764 | auto MI = Attr->modifiers_begin(); |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 10765 | for (const Expr *E : Attr->linears()) { |
| Alexey Bataev | 20cf67c | 2018-03-02 18:07:00 +0000 | [diff] [blame] | 10766 | E = E->IgnoreParenImpCasts(); |
| 10767 | unsigned Pos; |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 10768 | if (isa<CXXThisExpr>(E)) { |
| Alexey Bataev | 20cf67c | 2018-03-02 18:07:00 +0000 | [diff] [blame] | 10769 | Pos = ParamPositions[FD]; |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 10770 | } else { |
| 10771 | const auto *PVD = cast<ParmVarDecl>(cast<DeclRefExpr>(E)->getDecl()) |
| 10772 | ->getCanonicalDecl(); |
| Alexey Bataev | 20cf67c | 2018-03-02 18:07:00 +0000 | [diff] [blame] | 10773 | Pos = ParamPositions[PVD]; |
| 10774 | } |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 10775 | ParamAttrTy &ParamAttr = ParamAttrs[Pos]; |
| Alexey Bataev | 20cf67c | 2018-03-02 18:07:00 +0000 | [diff] [blame] | 10776 | ParamAttr.Kind = Linear; |
| 10777 | if (*SI) { |
| Fangrui Song | 407659a | 2018-11-30 23:41:18 +0000 | [diff] [blame] | 10778 | Expr::EvalResult Result; |
| 10779 | if (!(*SI)->EvaluateAsInt(Result, C, Expr::SE_AllowSideEffects)) { |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 10780 | if (const auto *DRE = |
| 10781 | cast<DeclRefExpr>((*SI)->IgnoreParenImpCasts())) { |
| 10782 | if (const auto *StridePVD = cast<ParmVarDecl>(DRE->getDecl())) { |
| Alexey Bataev | 20cf67c | 2018-03-02 18:07:00 +0000 | [diff] [blame] | 10783 | ParamAttr.Kind = LinearWithVarStride; |
| 10784 | ParamAttr.StrideOrArg = llvm::APSInt::getUnsigned( |
| 10785 | ParamPositions[StridePVD->getCanonicalDecl()]); |
| 10786 | } |
| Alexey Bataev | c7a82b4 | 2016-05-06 09:40:08 +0000 | [diff] [blame] | 10787 | } |
| Fangrui Song | 407659a | 2018-11-30 23:41:18 +0000 | [diff] [blame] | 10788 | } else { |
| 10789 | ParamAttr.StrideOrArg = Result.Val.getInt(); |
| Alexey Bataev | c7a82b4 | 2016-05-06 09:40:08 +0000 | [diff] [blame] | 10790 | } |
| 10791 | } |
| Alexey Bataev | 20cf67c | 2018-03-02 18:07:00 +0000 | [diff] [blame] | 10792 | ++SI; |
| 10793 | ++MI; |
| Alexey Bataev | c7a82b4 | 2016-05-06 09:40:08 +0000 | [diff] [blame] | 10794 | } |
| Alexey Bataev | 20cf67c | 2018-03-02 18:07:00 +0000 | [diff] [blame] | 10795 | llvm::APSInt VLENVal; |
| Alexey Bataev | a0a2264 | 2019-04-16 13:56:21 +0000 | [diff] [blame] | 10796 | SourceLocation ExprLoc; |
| 10797 | const Expr *VLENExpr = Attr->getSimdlen(); |
| 10798 | if (VLENExpr) { |
| 10799 | VLENVal = VLENExpr->EvaluateKnownConstInt(C); |
| 10800 | ExprLoc = VLENExpr->getExprLoc(); |
| 10801 | } |
| Alexey Bataev | 20cf67c | 2018-03-02 18:07:00 +0000 | [diff] [blame] | 10802 | OMPDeclareSimdDeclAttr::BranchStateTy State = Attr->getBranchState(); |
| Jim Lin | ab1bcda | 2020-01-07 17:32:39 +0800 | [diff] [blame] | 10803 | if (CGM.getTriple().isX86()) { |
| Alexey Bataev | 20cf67c | 2018-03-02 18:07:00 +0000 | [diff] [blame] | 10804 | emitX86DeclareSimdFunction(FD, Fn, VLENVal, ParamAttrs, State); |
| Alexey Bataev | a0a2264 | 2019-04-16 13:56:21 +0000 | [diff] [blame] | 10805 | } else if (CGM.getTriple().getArch() == llvm::Triple::aarch64) { |
| 10806 | unsigned VLEN = VLENVal.getExtValue(); |
| 10807 | StringRef MangledName = Fn->getName(); |
| 10808 | if (CGM.getTarget().hasFeature("sve")) |
| 10809 | emitAArch64DeclareSimdFunction(CGM, FD, VLEN, ParamAttrs, State, |
| 10810 | MangledName, 's', 128, Fn, ExprLoc); |
| 10811 | if (CGM.getTarget().hasFeature("neon")) |
| 10812 | emitAArch64DeclareSimdFunction(CGM, FD, VLEN, ParamAttrs, State, |
| 10813 | MangledName, 'n', 128, Fn, ExprLoc); |
| 10814 | } |
| Alexey Bataev | c7a82b4 | 2016-05-06 09:40:08 +0000 | [diff] [blame] | 10815 | } |
| Alexey Bataev | 20cf67c | 2018-03-02 18:07:00 +0000 | [diff] [blame] | 10816 | FD = FD->getPreviousDecl(); |
| Alexey Bataev | c7a82b4 | 2016-05-06 09:40:08 +0000 | [diff] [blame] | 10817 | } |
| 10818 | } |
| Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 10819 | |
| 10820 | namespace { |
| 10821 | /// Cleanup action for doacross support. |
| 10822 | class DoacrossCleanupTy final : public EHScopeStack::Cleanup { |
| 10823 | public: |
| 10824 | static const int DoacrossFinArgs = 2; |
| 10825 | |
| 10826 | private: |
| James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 10827 | llvm::FunctionCallee RTLFn; |
| Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 10828 | llvm::Value *Args[DoacrossFinArgs]; |
| 10829 | |
| 10830 | public: |
| James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 10831 | DoacrossCleanupTy(llvm::FunctionCallee RTLFn, |
| 10832 | ArrayRef<llvm::Value *> CallArgs) |
| Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 10833 | : RTLFn(RTLFn) { |
| 10834 | assert(CallArgs.size() == DoacrossFinArgs); |
| 10835 | std::copy(CallArgs.begin(), CallArgs.end(), std::begin(Args)); |
| 10836 | } |
| 10837 | void Emit(CodeGenFunction &CGF, Flags /*flags*/) override { |
| 10838 | if (!CGF.HaveInsertPoint()) |
| 10839 | return; |
| 10840 | CGF.EmitRuntimeCall(RTLFn, Args); |
| 10841 | } |
| 10842 | }; |
| 10843 | } // namespace |
| 10844 | |
| 10845 | void CGOpenMPRuntime::emitDoacrossInit(CodeGenFunction &CGF, |
| Alexey Bataev | f138fda | 2018-08-13 19:04:24 +0000 | [diff] [blame] | 10846 | const OMPLoopDirective &D, |
| 10847 | ArrayRef<Expr *> NumIterations) { |
| Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 10848 | if (!CGF.HaveInsertPoint()) |
| 10849 | return; |
| 10850 | |
| 10851 | ASTContext &C = CGM.getContext(); |
| 10852 | QualType Int64Ty = C.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/true); |
| 10853 | RecordDecl *RD; |
| 10854 | if (KmpDimTy.isNull()) { |
| 10855 | // Build struct kmp_dim { // loop bounds info casted to kmp_int64 |
| 10856 | // kmp_int64 lo; // lower |
| 10857 | // kmp_int64 up; // upper |
| 10858 | // kmp_int64 st; // stride |
| 10859 | // }; |
| 10860 | RD = C.buildImplicitRecord("kmp_dim"); |
| 10861 | RD->startDefinition(); |
| 10862 | addFieldToRecordDecl(C, RD, Int64Ty); |
| 10863 | addFieldToRecordDecl(C, RD, Int64Ty); |
| 10864 | addFieldToRecordDecl(C, RD, Int64Ty); |
| 10865 | RD->completeDefinition(); |
| 10866 | KmpDimTy = C.getRecordType(RD); |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 10867 | } else { |
| Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 10868 | RD = cast<RecordDecl>(KmpDimTy->getAsTagDecl()); |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 10869 | } |
| Alexey Bataev | f138fda | 2018-08-13 19:04:24 +0000 | [diff] [blame] | 10870 | llvm::APInt Size(/*numBits=*/32, NumIterations.size()); |
| 10871 | QualType ArrayTy = |
| Richard Smith | 772e266 | 2019-10-04 01:25:59 +0000 | [diff] [blame] | 10872 | C.getConstantArrayType(KmpDimTy, Size, nullptr, ArrayType::Normal, 0); |
| Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 10873 | |
| Alexey Bataev | f138fda | 2018-08-13 19:04:24 +0000 | [diff] [blame] | 10874 | Address DimsAddr = CGF.CreateMemTemp(ArrayTy, "dims"); |
| 10875 | CGF.EmitNullInitialization(DimsAddr, ArrayTy); |
| Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 10876 | enum { LowerFD = 0, UpperFD, StrideFD }; |
| 10877 | // Fill dims with data. |
| Alexey Bataev | f138fda | 2018-08-13 19:04:24 +0000 | [diff] [blame] | 10878 | for (unsigned I = 0, E = NumIterations.size(); I < E; ++I) { |
| James Y Knight | 751fe28 | 2019-02-09 22:22:28 +0000 | [diff] [blame] | 10879 | LValue DimsLVal = CGF.MakeAddrLValue( |
| 10880 | CGF.Builder.CreateConstArrayGEP(DimsAddr, I), KmpDimTy); |
| Alexey Bataev | f138fda | 2018-08-13 19:04:24 +0000 | [diff] [blame] | 10881 | // dims.upper = num_iterations; |
| 10882 | LValue UpperLVal = CGF.EmitLValueForField( |
| 10883 | DimsLVal, *std::next(RD->field_begin(), UpperFD)); |
| 10884 | llvm::Value *NumIterVal = |
| 10885 | CGF.EmitScalarConversion(CGF.EmitScalarExpr(NumIterations[I]), |
| 10886 | D.getNumIterations()->getType(), Int64Ty, |
| 10887 | D.getNumIterations()->getExprLoc()); |
| 10888 | CGF.EmitStoreOfScalar(NumIterVal, UpperLVal); |
| 10889 | // dims.stride = 1; |
| 10890 | LValue StrideLVal = CGF.EmitLValueForField( |
| 10891 | DimsLVal, *std::next(RD->field_begin(), StrideFD)); |
| 10892 | CGF.EmitStoreOfScalar(llvm::ConstantInt::getSigned(CGM.Int64Ty, /*V=*/1), |
| 10893 | StrideLVal); |
| 10894 | } |
| Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 10895 | |
| 10896 | // Build call void __kmpc_doacross_init(ident_t *loc, kmp_int32 gtid, |
| 10897 | // kmp_int32 num_dims, struct kmp_dim * dims); |
| Alexey Bataev | f138fda | 2018-08-13 19:04:24 +0000 | [diff] [blame] | 10898 | llvm::Value *Args[] = { |
| 10899 | emitUpdateLocation(CGF, D.getBeginLoc()), |
| 10900 | getThreadID(CGF, D.getBeginLoc()), |
| 10901 | llvm::ConstantInt::getSigned(CGM.Int32Ty, NumIterations.size()), |
| 10902 | CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
| James Y Knight | 751fe28 | 2019-02-09 22:22:28 +0000 | [diff] [blame] | 10903 | CGF.Builder.CreateConstArrayGEP(DimsAddr, 0).getPointer(), |
| Alexey Bataev | f138fda | 2018-08-13 19:04:24 +0000 | [diff] [blame] | 10904 | CGM.VoidPtrTy)}; |
| Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 10905 | |
| James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 10906 | llvm::FunctionCallee RTLFn = |
| 10907 | createRuntimeFunction(OMPRTL__kmpc_doacross_init); |
| Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 10908 | CGF.EmitRuntimeCall(RTLFn, Args); |
| 10909 | llvm::Value *FiniArgs[DoacrossCleanupTy::DoacrossFinArgs] = { |
| Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 10910 | emitUpdateLocation(CGF, D.getEndLoc()), getThreadID(CGF, D.getEndLoc())}; |
| James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 10911 | llvm::FunctionCallee FiniRTLFn = |
| 10912 | createRuntimeFunction(OMPRTL__kmpc_doacross_fini); |
| Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 10913 | CGF.EHStack.pushCleanup<DoacrossCleanupTy>(NormalAndEHCleanup, FiniRTLFn, |
| 10914 | llvm::makeArrayRef(FiniArgs)); |
| 10915 | } |
| 10916 | |
| 10917 | void CGOpenMPRuntime::emitDoacrossOrdered(CodeGenFunction &CGF, |
| 10918 | const OMPDependClause *C) { |
| 10919 | QualType Int64Ty = |
| 10920 | CGM.getContext().getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1); |
| Alexey Bataev | f138fda | 2018-08-13 19:04:24 +0000 | [diff] [blame] | 10921 | llvm::APInt Size(/*numBits=*/32, C->getNumLoops()); |
| 10922 | QualType ArrayTy = CGM.getContext().getConstantArrayType( |
| Richard Smith | 772e266 | 2019-10-04 01:25:59 +0000 | [diff] [blame] | 10923 | Int64Ty, Size, nullptr, ArrayType::Normal, 0); |
| Alexey Bataev | f138fda | 2018-08-13 19:04:24 +0000 | [diff] [blame] | 10924 | Address CntAddr = CGF.CreateMemTemp(ArrayTy, ".cnt.addr"); |
| 10925 | for (unsigned I = 0, E = C->getNumLoops(); I < E; ++I) { |
| 10926 | const Expr *CounterVal = C->getLoopData(I); |
| 10927 | assert(CounterVal); |
| 10928 | llvm::Value *CntVal = CGF.EmitScalarConversion( |
| 10929 | CGF.EmitScalarExpr(CounterVal), CounterVal->getType(), Int64Ty, |
| 10930 | CounterVal->getExprLoc()); |
| James Y Knight | 751fe28 | 2019-02-09 22:22:28 +0000 | [diff] [blame] | 10931 | CGF.EmitStoreOfScalar(CntVal, CGF.Builder.CreateConstArrayGEP(CntAddr, I), |
| 10932 | /*Volatile=*/false, Int64Ty); |
| Alexey Bataev | f138fda | 2018-08-13 19:04:24 +0000 | [diff] [blame] | 10933 | } |
| 10934 | llvm::Value *Args[] = { |
| 10935 | emitUpdateLocation(CGF, C->getBeginLoc()), |
| 10936 | getThreadID(CGF, C->getBeginLoc()), |
| James Y Knight | 751fe28 | 2019-02-09 22:22:28 +0000 | [diff] [blame] | 10937 | CGF.Builder.CreateConstArrayGEP(CntAddr, 0).getPointer()}; |
| James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 10938 | llvm::FunctionCallee RTLFn; |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 10939 | if (C->getDependencyKind() == OMPC_DEPEND_source) { |
| Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 10940 | RTLFn = createRuntimeFunction(OMPRTL__kmpc_doacross_post); |
| Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 10941 | } else { |
| Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 10942 | assert(C->getDependencyKind() == OMPC_DEPEND_sink); |
| 10943 | RTLFn = createRuntimeFunction(OMPRTL__kmpc_doacross_wait); |
| 10944 | } |
| 10945 | CGF.EmitRuntimeCall(RTLFn, Args); |
| 10946 | } |
| 10947 | |
| Alexey Bataev | 7ef47a6 | 2018-02-22 18:33:31 +0000 | [diff] [blame] | 10948 | void CGOpenMPRuntime::emitCall(CodeGenFunction &CGF, SourceLocation Loc, |
| James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 10949 | llvm::FunctionCallee Callee, |
| Alexey Bataev | 7ef47a6 | 2018-02-22 18:33:31 +0000 | [diff] [blame] | 10950 | ArrayRef<llvm::Value *> Args) const { |
| 10951 | assert(Loc.isValid() && "Outlined function call location must be valid."); |
| Alexey Bataev | 3c595a6 | 2017-08-14 15:01:03 +0000 | [diff] [blame] | 10952 | auto DL = ApplyDebugLocation::CreateDefaultArtificial(CGF, Loc); |
| 10953 | |
| James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 10954 | if (auto *Fn = dyn_cast<llvm::Function>(Callee.getCallee())) { |
| Alexey Bataev | 2c7eee5 | 2017-08-04 19:10:54 +0000 | [diff] [blame] | 10955 | if (Fn->doesNotThrow()) { |
| Alexey Bataev | 3c595a6 | 2017-08-14 15:01:03 +0000 | [diff] [blame] | 10956 | CGF.EmitNounwindRuntimeCall(Fn, Args); |
| Alexey Bataev | 2c7eee5 | 2017-08-04 19:10:54 +0000 | [diff] [blame] | 10957 | return; |
| 10958 | } |
| 10959 | } |
| Alexey Bataev | 3c595a6 | 2017-08-14 15:01:03 +0000 | [diff] [blame] | 10960 | CGF.EmitRuntimeCall(Callee, Args); |
| 10961 | } |
| 10962 | |
| 10963 | void CGOpenMPRuntime::emitOutlinedFunctionCall( |
| James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 10964 | CodeGenFunction &CGF, SourceLocation Loc, llvm::FunctionCallee OutlinedFn, |
| Alexey Bataev | 3c595a6 | 2017-08-14 15:01:03 +0000 | [diff] [blame] | 10965 | ArrayRef<llvm::Value *> Args) const { |
| Alexey Bataev | 7ef47a6 | 2018-02-22 18:33:31 +0000 | [diff] [blame] | 10966 | emitCall(CGF, Loc, OutlinedFn, Args); |
| Alexey Bataev | 2c7eee5 | 2017-08-04 19:10:54 +0000 | [diff] [blame] | 10967 | } |
| Alexey Bataev | 3b8d558 | 2017-08-08 18:04:06 +0000 | [diff] [blame] | 10968 | |
| Gheorghe-Teodor Bercea | 66cdbb47 | 2019-05-21 19:42:01 +0000 | [diff] [blame] | 10969 | void CGOpenMPRuntime::emitFunctionProlog(CodeGenFunction &CGF, const Decl *D) { |
| 10970 | if (const auto *FD = dyn_cast<FunctionDecl>(D)) |
| 10971 | if (OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(FD)) |
| 10972 | HasEmittedDeclareTargetRegion = true; |
| 10973 | } |
| 10974 | |
| Alexey Bataev | 3b8d558 | 2017-08-08 18:04:06 +0000 | [diff] [blame] | 10975 | Address CGOpenMPRuntime::getParameterAddress(CodeGenFunction &CGF, |
| 10976 | const VarDecl *NativeParam, |
| 10977 | const VarDecl *TargetParam) const { |
| 10978 | return CGF.GetAddrOfLocalVar(NativeParam); |
| 10979 | } |
| Alexey Bataev | a8a9153a | 2017-12-29 18:07:07 +0000 | [diff] [blame] | 10980 | |
| Alexey Bataev | 4f680db | 2019-03-19 16:41:16 +0000 | [diff] [blame] | 10981 | namespace { |
| 10982 | /// Cleanup action for allocate support. |
| 10983 | class OMPAllocateCleanupTy final : public EHScopeStack::Cleanup { |
| 10984 | public: |
| 10985 | static const int CleanupArgs = 3; |
| 10986 | |
| 10987 | private: |
| 10988 | llvm::FunctionCallee RTLFn; |
| 10989 | llvm::Value *Args[CleanupArgs]; |
| 10990 | |
| 10991 | public: |
| 10992 | OMPAllocateCleanupTy(llvm::FunctionCallee RTLFn, |
| 10993 | ArrayRef<llvm::Value *> CallArgs) |
| 10994 | : RTLFn(RTLFn) { |
| 10995 | assert(CallArgs.size() == CleanupArgs && |
| 10996 | "Size of arguments does not match."); |
| 10997 | std::copy(CallArgs.begin(), CallArgs.end(), std::begin(Args)); |
| 10998 | } |
| 10999 | void Emit(CodeGenFunction &CGF, Flags /*flags*/) override { |
| 11000 | if (!CGF.HaveInsertPoint()) |
| 11001 | return; |
| 11002 | CGF.EmitRuntimeCall(RTLFn, Args); |
| 11003 | } |
| 11004 | }; |
| 11005 | } // namespace |
| 11006 | |
| Gheorghe-Teodor Bercea | d3dcf2f | 2018-03-14 14:17:45 +0000 | [diff] [blame] | 11007 | Address CGOpenMPRuntime::getAddressOfLocalVariable(CodeGenFunction &CGF, |
| 11008 | const VarDecl *VD) { |
| Alexey Bataev | 084b0c2f0 | 2019-03-21 20:36:16 +0000 | [diff] [blame] | 11009 | if (!VD) |
| 11010 | return Address::invalid(); |
| Alexey Bataev | 4f680db | 2019-03-19 16:41:16 +0000 | [diff] [blame] | 11011 | const VarDecl *CVD = VD->getCanonicalDecl(); |
| 11012 | if (!CVD->hasAttr<OMPAllocateDeclAttr>()) |
| 11013 | return Address::invalid(); |
| Alexey Bataev | 084b0c2f0 | 2019-03-21 20:36:16 +0000 | [diff] [blame] | 11014 | const auto *AA = CVD->getAttr<OMPAllocateDeclAttr>(); |
| 11015 | // Use the default allocation. |
| Alexey Bataev | 0fd3c68 | 2019-04-02 19:44:46 +0000 | [diff] [blame] | 11016 | if (AA->getAllocatorType() == OMPAllocateDeclAttr::OMPDefaultMemAlloc && |
| 11017 | !AA->getAllocator()) |
| Alexey Bataev | 084b0c2f0 | 2019-03-21 20:36:16 +0000 | [diff] [blame] | 11018 | return Address::invalid(); |
| Alexey Bataev | 084b0c2f0 | 2019-03-21 20:36:16 +0000 | [diff] [blame] | 11019 | llvm::Value *Size; |
| 11020 | CharUnits Align = CGM.getContext().getDeclAlign(CVD); |
| 11021 | if (CVD->getType()->isVariablyModifiedType()) { |
| 11022 | Size = CGF.getTypeSize(CVD->getType()); |
| Alexey Bataev | 9c39781 | 2019-04-03 17:57:06 +0000 | [diff] [blame] | 11023 | // Align the size: ((size + align - 1) / align) * align |
| 11024 | Size = CGF.Builder.CreateNUWAdd( |
| 11025 | Size, CGM.getSize(Align - CharUnits::fromQuantity(1))); |
| 11026 | Size = CGF.Builder.CreateUDiv(Size, CGM.getSize(Align)); |
| 11027 | Size = CGF.Builder.CreateNUWMul(Size, CGM.getSize(Align)); |
| Alexey Bataev | 084b0c2f0 | 2019-03-21 20:36:16 +0000 | [diff] [blame] | 11028 | } else { |
| 11029 | CharUnits Sz = CGM.getContext().getTypeSizeInChars(CVD->getType()); |
| Alexey Bataev | 084b0c2f0 | 2019-03-21 20:36:16 +0000 | [diff] [blame] | 11030 | Size = CGM.getSize(Sz.alignTo(Align)); |
| Alexey Bataev | 4f680db | 2019-03-19 16:41:16 +0000 | [diff] [blame] | 11031 | } |
| Alexey Bataev | 084b0c2f0 | 2019-03-21 20:36:16 +0000 | [diff] [blame] | 11032 | llvm::Value *ThreadID = getThreadID(CGF, CVD->getBeginLoc()); |
| 11033 | assert(AA->getAllocator() && |
| 11034 | "Expected allocator expression for non-default allocator."); |
| 11035 | llvm::Value *Allocator = CGF.EmitScalarExpr(AA->getAllocator()); |
| Alexey Bataev | 6cf7b71 | 2019-04-08 19:06:42 +0000 | [diff] [blame] | 11036 | // According to the standard, the original allocator type is a enum (integer). |
| 11037 | // Convert to pointer type, if required. |
| 11038 | if (Allocator->getType()->isIntegerTy()) |
| 11039 | Allocator = CGF.Builder.CreateIntToPtr(Allocator, CGM.VoidPtrTy); |
| 11040 | else if (Allocator->getType()->isPointerTy()) |
| 11041 | Allocator = CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(Allocator, |
| 11042 | CGM.VoidPtrTy); |
| Alexey Bataev | 084b0c2f0 | 2019-03-21 20:36:16 +0000 | [diff] [blame] | 11043 | llvm::Value *Args[] = {ThreadID, Size, Allocator}; |
| 11044 | |
| 11045 | llvm::Value *Addr = |
| 11046 | CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_alloc), Args, |
| Alexey Bataev | 8b32192 | 2020-01-16 15:46:34 -0500 | [diff] [blame] | 11047 | getName({CVD->getName(), ".void.addr"})); |
| Alexey Bataev | 084b0c2f0 | 2019-03-21 20:36:16 +0000 | [diff] [blame] | 11048 | llvm::Value *FiniArgs[OMPAllocateCleanupTy::CleanupArgs] = {ThreadID, Addr, |
| 11049 | Allocator}; |
| 11050 | llvm::FunctionCallee FiniRTLFn = createRuntimeFunction(OMPRTL__kmpc_free); |
| 11051 | |
| 11052 | CGF.EHStack.pushCleanup<OMPAllocateCleanupTy>(NormalAndEHCleanup, FiniRTLFn, |
| 11053 | llvm::makeArrayRef(FiniArgs)); |
| 11054 | Addr = CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
| 11055 | Addr, |
| 11056 | CGF.ConvertTypeForMem(CGM.getContext().getPointerType(CVD->getType())), |
| Alexey Bataev | 8b32192 | 2020-01-16 15:46:34 -0500 | [diff] [blame] | 11057 | getName({CVD->getName(), ".addr"})); |
| Alexey Bataev | 084b0c2f0 | 2019-03-21 20:36:16 +0000 | [diff] [blame] | 11058 | return Address(Addr, Align); |
| Gheorghe-Teodor Bercea | d3dcf2f | 2018-03-14 14:17:45 +0000 | [diff] [blame] | 11059 | } |
| 11060 | |
| Alexey Bataev | fde11e9 | 2019-11-07 11:03:10 -0500 | [diff] [blame] | 11061 | namespace { |
| 11062 | using OMPContextSelectorData = |
| Alexey Bataev | f8ff3d7 | 2019-11-21 16:36:49 -0500 | [diff] [blame] | 11063 | OpenMPCtxSelectorData<ArrayRef<StringRef>, llvm::APSInt>; |
| Alexey Bataev | fde11e9 | 2019-11-07 11:03:10 -0500 | [diff] [blame] | 11064 | using CompleteOMPContextSelectorData = SmallVector<OMPContextSelectorData, 4>; |
| 11065 | } // anonymous namespace |
| 11066 | |
| Alexey Bataev | 2df5f12 | 2019-10-01 20:18:32 +0000 | [diff] [blame] | 11067 | /// Checks current context and returns true if it matches the context selector. |
| Alexey Bataev | 4e8231b | 2019-11-05 15:13:30 -0500 | [diff] [blame] | 11068 | template <OpenMPContextSelectorSetKind CtxSet, OpenMPContextSelectorKind Ctx, |
| 11069 | typename... Arguments> |
| 11070 | static bool checkContext(const OMPContextSelectorData &Data, |
| 11071 | Arguments... Params) { |
| Alexey Bataev | fde11e9 | 2019-11-07 11:03:10 -0500 | [diff] [blame] | 11072 | assert(Data.CtxSet != OMP_CTX_SET_unknown && Data.Ctx != OMP_CTX_unknown && |
| Alexey Bataev | 2df5f12 | 2019-10-01 20:18:32 +0000 | [diff] [blame] | 11073 | "Unknown context selector or context selector set."); |
| 11074 | return false; |
| 11075 | } |
| 11076 | |
| 11077 | /// Checks for implementation={vendor(<vendor>)} context selector. |
| 11078 | /// \returns true iff <vendor>="llvm", false otherwise. |
| 11079 | template <> |
| Alexey Bataev | fde11e9 | 2019-11-07 11:03:10 -0500 | [diff] [blame] | 11080 | bool checkContext<OMP_CTX_SET_implementation, OMP_CTX_vendor>( |
| 11081 | const OMPContextSelectorData &Data) { |
| 11082 | return llvm::all_of(Data.Names, |
| Alexey Bataev | 303657a | 2019-10-08 19:44:16 +0000 | [diff] [blame] | 11083 | [](StringRef S) { return !S.compare_lower("llvm"); }); |
| Alexey Bataev | 2df5f12 | 2019-10-01 20:18:32 +0000 | [diff] [blame] | 11084 | } |
| 11085 | |
| Alexey Bataev | 4e8231b | 2019-11-05 15:13:30 -0500 | [diff] [blame] | 11086 | /// Checks for device={kind(<kind>)} context selector. |
| 11087 | /// \returns true if <kind>="host" and compilation is for host. |
| 11088 | /// true if <kind>="nohost" and compilation is for device. |
| 11089 | /// true if <kind>="cpu" and compilation is for Arm, X86 or PPC CPU. |
| 11090 | /// true if <kind>="gpu" and compilation is for NVPTX or AMDGCN. |
| 11091 | /// false otherwise. |
| 11092 | template <> |
| 11093 | bool checkContext<OMP_CTX_SET_device, OMP_CTX_kind, CodeGenModule &>( |
| 11094 | const OMPContextSelectorData &Data, CodeGenModule &CGM) { |
| 11095 | for (StringRef Name : Data.Names) { |
| 11096 | if (!Name.compare_lower("host")) { |
| 11097 | if (CGM.getLangOpts().OpenMPIsDevice) |
| 11098 | return false; |
| 11099 | continue; |
| 11100 | } |
| 11101 | if (!Name.compare_lower("nohost")) { |
| 11102 | if (!CGM.getLangOpts().OpenMPIsDevice) |
| 11103 | return false; |
| 11104 | continue; |
| 11105 | } |
| 11106 | switch (CGM.getTriple().getArch()) { |
| 11107 | case llvm::Triple::arm: |
| 11108 | case llvm::Triple::armeb: |
| 11109 | case llvm::Triple::aarch64: |
| 11110 | case llvm::Triple::aarch64_be: |
| 11111 | case llvm::Triple::aarch64_32: |
| 11112 | case llvm::Triple::ppc: |
| 11113 | case llvm::Triple::ppc64: |
| 11114 | case llvm::Triple::ppc64le: |
| 11115 | case llvm::Triple::x86: |
| 11116 | case llvm::Triple::x86_64: |
| 11117 | if (Name.compare_lower("cpu")) |
| 11118 | return false; |
| 11119 | break; |
| 11120 | case llvm::Triple::amdgcn: |
| 11121 | case llvm::Triple::nvptx: |
| 11122 | case llvm::Triple::nvptx64: |
| 11123 | if (Name.compare_lower("gpu")) |
| 11124 | return false; |
| 11125 | break; |
| 11126 | case llvm::Triple::UnknownArch: |
| 11127 | case llvm::Triple::arc: |
| 11128 | case llvm::Triple::avr: |
| 11129 | case llvm::Triple::bpfel: |
| 11130 | case llvm::Triple::bpfeb: |
| 11131 | case llvm::Triple::hexagon: |
| 11132 | case llvm::Triple::mips: |
| 11133 | case llvm::Triple::mipsel: |
| 11134 | case llvm::Triple::mips64: |
| 11135 | case llvm::Triple::mips64el: |
| 11136 | case llvm::Triple::msp430: |
| 11137 | case llvm::Triple::r600: |
| 11138 | case llvm::Triple::riscv32: |
| 11139 | case llvm::Triple::riscv64: |
| 11140 | case llvm::Triple::sparc: |
| 11141 | case llvm::Triple::sparcv9: |
| 11142 | case llvm::Triple::sparcel: |
| 11143 | case llvm::Triple::systemz: |
| 11144 | case llvm::Triple::tce: |
| 11145 | case llvm::Triple::tcele: |
| 11146 | case llvm::Triple::thumb: |
| 11147 | case llvm::Triple::thumbeb: |
| 11148 | case llvm::Triple::xcore: |
| 11149 | case llvm::Triple::le32: |
| 11150 | case llvm::Triple::le64: |
| 11151 | case llvm::Triple::amdil: |
| 11152 | case llvm::Triple::amdil64: |
| 11153 | case llvm::Triple::hsail: |
| 11154 | case llvm::Triple::hsail64: |
| 11155 | case llvm::Triple::spir: |
| 11156 | case llvm::Triple::spir64: |
| 11157 | case llvm::Triple::kalimba: |
| 11158 | case llvm::Triple::shave: |
| 11159 | case llvm::Triple::lanai: |
| 11160 | case llvm::Triple::wasm32: |
| 11161 | case llvm::Triple::wasm64: |
| 11162 | case llvm::Triple::renderscript32: |
| 11163 | case llvm::Triple::renderscript64: |
| Simon Pilgrim | 0d54079 | 2020-01-09 11:10:46 +0000 | [diff] [blame] | 11164 | case llvm::Triple::ve: |
| Alexey Bataev | 4e8231b | 2019-11-05 15:13:30 -0500 | [diff] [blame] | 11165 | return false; |
| 11166 | } |
| 11167 | } |
| 11168 | return true; |
| 11169 | } |
| 11170 | |
| Benjamin Kramer | df18650 | 2020-01-14 14:06:12 +0100 | [diff] [blame] | 11171 | static bool matchesContext(CodeGenModule &CGM, |
| 11172 | const CompleteOMPContextSelectorData &ContextData) { |
| Alexey Bataev | fde11e9 | 2019-11-07 11:03:10 -0500 | [diff] [blame] | 11173 | for (const OMPContextSelectorData &Data : ContextData) { |
| Alexey Bataev | 5459a90 | 2019-11-22 11:42:08 -0500 | [diff] [blame] | 11174 | switch (Data.Ctx) { |
| 11175 | case OMP_CTX_vendor: |
| 11176 | assert(Data.CtxSet == OMP_CTX_SET_implementation && |
| 11177 | "Expected implementation context selector set."); |
| 11178 | if (!checkContext<OMP_CTX_SET_implementation, OMP_CTX_vendor>(Data)) |
| 11179 | return false; |
| Alexey Bataev | 4e8231b | 2019-11-05 15:13:30 -0500 | [diff] [blame] | 11180 | break; |
| Alexey Bataev | 5459a90 | 2019-11-22 11:42:08 -0500 | [diff] [blame] | 11181 | case OMP_CTX_kind: |
| 11182 | assert(Data.CtxSet == OMP_CTX_SET_device && |
| 11183 | "Expected device context selector set."); |
| 11184 | if (!checkContext<OMP_CTX_SET_device, OMP_CTX_kind, CodeGenModule &>(Data, |
| 11185 | CGM)) |
| 11186 | return false; |
| Alexey Bataev | fde11e9 | 2019-11-07 11:03:10 -0500 | [diff] [blame] | 11187 | break; |
| Alexey Bataev | 5459a90 | 2019-11-22 11:42:08 -0500 | [diff] [blame] | 11188 | case OMP_CTX_unknown: |
| 11189 | llvm_unreachable("Unknown context selector kind."); |
| Alexey Bataev | fde11e9 | 2019-11-07 11:03:10 -0500 | [diff] [blame] | 11190 | } |
| 11191 | } |
| 11192 | return true; |
| 11193 | } |
| 11194 | |
| 11195 | static CompleteOMPContextSelectorData |
| 11196 | translateAttrToContextSelectorData(ASTContext &C, |
| 11197 | const OMPDeclareVariantAttr *A) { |
| 11198 | CompleteOMPContextSelectorData Data; |
| 11199 | for (unsigned I = 0, E = A->scores_size(); I < E; ++I) { |
| 11200 | Data.emplace_back(); |
| 11201 | auto CtxSet = static_cast<OpenMPContextSelectorSetKind>( |
| 11202 | *std::next(A->ctxSelectorSets_begin(), I)); |
| 11203 | auto Ctx = static_cast<OpenMPContextSelectorKind>( |
| 11204 | *std::next(A->ctxSelectors_begin(), I)); |
| 11205 | Data.back().CtxSet = CtxSet; |
| 11206 | Data.back().Ctx = Ctx; |
| 11207 | const Expr *Score = *std::next(A->scores_begin(), I); |
| 11208 | Data.back().Score = Score->EvaluateKnownConstInt(C); |
| Alexey Bataev | 5459a90 | 2019-11-22 11:42:08 -0500 | [diff] [blame] | 11209 | switch (Ctx) { |
| 11210 | case OMP_CTX_vendor: |
| 11211 | assert(CtxSet == OMP_CTX_SET_implementation && |
| 11212 | "Expected implementation context selector set."); |
| 11213 | Data.back().Names = |
| 11214 | llvm::makeArrayRef(A->implVendors_begin(), A->implVendors_end()); |
| Alexey Bataev | 4e8231b | 2019-11-05 15:13:30 -0500 | [diff] [blame] | 11215 | break; |
| Alexey Bataev | 5459a90 | 2019-11-22 11:42:08 -0500 | [diff] [blame] | 11216 | case OMP_CTX_kind: |
| 11217 | assert(CtxSet == OMP_CTX_SET_device && |
| 11218 | "Expected device context selector set."); |
| 11219 | Data.back().Names = |
| 11220 | llvm::makeArrayRef(A->deviceKinds_begin(), A->deviceKinds_end()); |
| Alexey Bataev | fde11e9 | 2019-11-07 11:03:10 -0500 | [diff] [blame] | 11221 | break; |
| Alexey Bataev | 5459a90 | 2019-11-22 11:42:08 -0500 | [diff] [blame] | 11222 | case OMP_CTX_unknown: |
| 11223 | llvm_unreachable("Unknown context selector kind."); |
| Alexey Bataev | fde11e9 | 2019-11-07 11:03:10 -0500 | [diff] [blame] | 11224 | } |
| 11225 | } |
| 11226 | return Data; |
| 11227 | } |
| 11228 | |
| Alexey Bataev | 4e8231b | 2019-11-05 15:13:30 -0500 | [diff] [blame] | 11229 | static bool isStrictSubset(const CompleteOMPContextSelectorData &LHS, |
| 11230 | const CompleteOMPContextSelectorData &RHS) { |
| 11231 | llvm::SmallDenseMap<std::pair<int, int>, llvm::StringSet<>, 4> RHSData; |
| 11232 | for (const OMPContextSelectorData &D : RHS) { |
| 11233 | auto &Pair = RHSData.FindAndConstruct(std::make_pair(D.CtxSet, D.Ctx)); |
| 11234 | Pair.getSecond().insert(D.Names.begin(), D.Names.end()); |
| 11235 | } |
| 11236 | bool AllSetsAreEqual = true; |
| 11237 | for (const OMPContextSelectorData &D : LHS) { |
| 11238 | auto It = RHSData.find(std::make_pair(D.CtxSet, D.Ctx)); |
| 11239 | if (It == RHSData.end()) |
| 11240 | return false; |
| 11241 | if (D.Names.size() > It->getSecond().size()) |
| 11242 | return false; |
| 11243 | if (llvm::set_union(It->getSecond(), D.Names)) |
| 11244 | return false; |
| 11245 | AllSetsAreEqual = |
| 11246 | AllSetsAreEqual && (D.Names.size() == It->getSecond().size()); |
| 11247 | } |
| 11248 | |
| 11249 | return LHS.size() != RHS.size() || !AllSetsAreEqual; |
| 11250 | } |
| 11251 | |
| Alexey Bataev | fde11e9 | 2019-11-07 11:03:10 -0500 | [diff] [blame] | 11252 | static bool greaterCtxScore(const CompleteOMPContextSelectorData &LHS, |
| 11253 | const CompleteOMPContextSelectorData &RHS) { |
| 11254 | // Score is calculated as sum of all scores + 1. |
| 11255 | llvm::APSInt LHSScore(llvm::APInt(64, 1), /*isUnsigned=*/false); |
| Alexey Bataev | 4e8231b | 2019-11-05 15:13:30 -0500 | [diff] [blame] | 11256 | bool RHSIsSubsetOfLHS = isStrictSubset(RHS, LHS); |
| 11257 | if (RHSIsSubsetOfLHS) { |
| 11258 | LHSScore = llvm::APSInt::get(0); |
| 11259 | } else { |
| 11260 | for (const OMPContextSelectorData &Data : LHS) { |
| 11261 | if (Data.Score.getBitWidth() > LHSScore.getBitWidth()) { |
| 11262 | LHSScore = LHSScore.extend(Data.Score.getBitWidth()) + Data.Score; |
| 11263 | } else if (Data.Score.getBitWidth() < LHSScore.getBitWidth()) { |
| 11264 | LHSScore += Data.Score.extend(LHSScore.getBitWidth()); |
| 11265 | } else { |
| 11266 | LHSScore += Data.Score; |
| 11267 | } |
| Alexey Bataev | fde11e9 | 2019-11-07 11:03:10 -0500 | [diff] [blame] | 11268 | } |
| 11269 | } |
| 11270 | llvm::APSInt RHSScore(llvm::APInt(64, 1), /*isUnsigned=*/false); |
| Alexey Bataev | 4e8231b | 2019-11-05 15:13:30 -0500 | [diff] [blame] | 11271 | if (!RHSIsSubsetOfLHS && isStrictSubset(LHS, RHS)) { |
| 11272 | RHSScore = llvm::APSInt::get(0); |
| 11273 | } else { |
| 11274 | for (const OMPContextSelectorData &Data : RHS) { |
| 11275 | if (Data.Score.getBitWidth() > RHSScore.getBitWidth()) { |
| 11276 | RHSScore = RHSScore.extend(Data.Score.getBitWidth()) + Data.Score; |
| 11277 | } else if (Data.Score.getBitWidth() < RHSScore.getBitWidth()) { |
| 11278 | RHSScore += Data.Score.extend(RHSScore.getBitWidth()); |
| 11279 | } else { |
| 11280 | RHSScore += Data.Score; |
| 11281 | } |
| Alexey Bataev | fde11e9 | 2019-11-07 11:03:10 -0500 | [diff] [blame] | 11282 | } |
| 11283 | } |
| 11284 | return llvm::APSInt::compareValues(LHSScore, RHSScore) >= 0; |
| Alexey Bataev | 0364c76 | 2019-10-03 20:49:48 +0000 | [diff] [blame] | 11285 | } |
| 11286 | |
| Alexey Bataev | 2df5f12 | 2019-10-01 20:18:32 +0000 | [diff] [blame] | 11287 | /// Finds the variant function that matches current context with its context |
| 11288 | /// selector. |
| Alexey Bataev | 4e8231b | 2019-11-05 15:13:30 -0500 | [diff] [blame] | 11289 | static const FunctionDecl *getDeclareVariantFunction(CodeGenModule &CGM, |
| Alexey Bataev | 0364c76 | 2019-10-03 20:49:48 +0000 | [diff] [blame] | 11290 | const FunctionDecl *FD) { |
| Alexey Bataev | 2df5f12 | 2019-10-01 20:18:32 +0000 | [diff] [blame] | 11291 | if (!FD->hasAttrs() || !FD->hasAttr<OMPDeclareVariantAttr>()) |
| 11292 | return FD; |
| 11293 | // Iterate through all DeclareVariant attributes and check context selectors. |
| Alexey Bataev | 0364c76 | 2019-10-03 20:49:48 +0000 | [diff] [blame] | 11294 | const OMPDeclareVariantAttr *TopMostAttr = nullptr; |
| Alexey Bataev | fde11e9 | 2019-11-07 11:03:10 -0500 | [diff] [blame] | 11295 | CompleteOMPContextSelectorData TopMostData; |
| Alexey Bataev | 0364c76 | 2019-10-03 20:49:48 +0000 | [diff] [blame] | 11296 | for (const auto *A : FD->specific_attrs<OMPDeclareVariantAttr>()) { |
| Alexey Bataev | fde11e9 | 2019-11-07 11:03:10 -0500 | [diff] [blame] | 11297 | CompleteOMPContextSelectorData Data = |
| Alexey Bataev | 4e8231b | 2019-11-05 15:13:30 -0500 | [diff] [blame] | 11298 | translateAttrToContextSelectorData(CGM.getContext(), A); |
| 11299 | if (!matchesContext(CGM, Data)) |
| Alexey Bataev | fde11e9 | 2019-11-07 11:03:10 -0500 | [diff] [blame] | 11300 | continue; |
| Alexey Bataev | 0364c76 | 2019-10-03 20:49:48 +0000 | [diff] [blame] | 11301 | // If the attribute matches the context, find the attribute with the highest |
| 11302 | // score. |
| Alexey Bataev | fde11e9 | 2019-11-07 11:03:10 -0500 | [diff] [blame] | 11303 | if (!TopMostAttr || !greaterCtxScore(TopMostData, Data)) { |
| 11304 | TopMostAttr = A; |
| 11305 | TopMostData.swap(Data); |
| 11306 | } |
| Alexey Bataev | 2df5f12 | 2019-10-01 20:18:32 +0000 | [diff] [blame] | 11307 | } |
| Alexey Bataev | 0364c76 | 2019-10-03 20:49:48 +0000 | [diff] [blame] | 11308 | if (!TopMostAttr) |
| Alexey Bataev | 2df5f12 | 2019-10-01 20:18:32 +0000 | [diff] [blame] | 11309 | return FD; |
| Alexey Bataev | 2df5f12 | 2019-10-01 20:18:32 +0000 | [diff] [blame] | 11310 | return cast<FunctionDecl>( |
| Alexey Bataev | 0364c76 | 2019-10-03 20:49:48 +0000 | [diff] [blame] | 11311 | cast<DeclRefExpr>(TopMostAttr->getVariantFuncRef()->IgnoreParenImpCasts()) |
| Alexey Bataev | 2df5f12 | 2019-10-01 20:18:32 +0000 | [diff] [blame] | 11312 | ->getDecl()); |
| 11313 | } |
| 11314 | |
| 11315 | bool CGOpenMPRuntime::emitDeclareVariant(GlobalDecl GD, bool IsForDefinition) { |
| 11316 | const auto *D = cast<FunctionDecl>(GD.getDecl()); |
| 11317 | // If the original function is defined already, use its definition. |
| 11318 | StringRef MangledName = CGM.getMangledName(GD); |
| 11319 | llvm::GlobalValue *Orig = CGM.GetGlobalValue(MangledName); |
| 11320 | if (Orig && !Orig->isDeclaration()) |
| 11321 | return false; |
| Alexey Bataev | 4e8231b | 2019-11-05 15:13:30 -0500 | [diff] [blame] | 11322 | const FunctionDecl *NewFD = getDeclareVariantFunction(CGM, D); |
| Alexey Bataev | 2df5f12 | 2019-10-01 20:18:32 +0000 | [diff] [blame] | 11323 | // Emit original function if it does not have declare variant attribute or the |
| 11324 | // context does not match. |
| 11325 | if (NewFD == D) |
| 11326 | return false; |
| 11327 | GlobalDecl NewGD = GD.getWithDecl(NewFD); |
| Alexey Bataev | c2cd2d4 | 2019-10-10 17:28:10 +0000 | [diff] [blame] | 11328 | if (tryEmitDeclareVariant(NewGD, GD, Orig, IsForDefinition)) { |
| Alexey Bataev | 2df5f12 | 2019-10-01 20:18:32 +0000 | [diff] [blame] | 11329 | DeferredVariantFunction.erase(D); |
| 11330 | return true; |
| 11331 | } |
| 11332 | DeferredVariantFunction.insert(std::make_pair(D, std::make_pair(NewGD, GD))); |
| 11333 | return true; |
| 11334 | } |
| 11335 | |
| Alexey Bataev | 0860db9 | 2019-12-19 10:01:10 -0500 | [diff] [blame] | 11336 | CGOpenMPRuntime::NontemporalDeclsRAII::NontemporalDeclsRAII( |
| 11337 | CodeGenModule &CGM, const OMPLoopDirective &S) |
| 11338 | : CGM(CGM), NeedToPush(S.hasClausesOfKind<OMPNontemporalClause>()) { |
| 11339 | assert(CGM.getLangOpts().OpenMP && "Not in OpenMP mode."); |
| 11340 | if (!NeedToPush) |
| 11341 | return; |
| 11342 | NontemporalDeclsSet &DS = |
| 11343 | CGM.getOpenMPRuntime().NontemporalDeclsStack.emplace_back(); |
| 11344 | for (const auto *C : S.getClausesOfKind<OMPNontemporalClause>()) { |
| 11345 | for (const Stmt *Ref : C->private_refs()) { |
| 11346 | const auto *SimpleRefExpr = cast<Expr>(Ref)->IgnoreParenImpCasts(); |
| 11347 | const ValueDecl *VD; |
| 11348 | if (const auto *DRE = dyn_cast<DeclRefExpr>(SimpleRefExpr)) { |
| 11349 | VD = DRE->getDecl(); |
| 11350 | } else { |
| 11351 | const auto *ME = cast<MemberExpr>(SimpleRefExpr); |
| 11352 | assert((ME->isImplicitCXXThis() || |
| 11353 | isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts())) && |
| 11354 | "Expected member of current class."); |
| 11355 | VD = ME->getMemberDecl(); |
| 11356 | } |
| 11357 | DS.insert(VD); |
| 11358 | } |
| 11359 | } |
| 11360 | } |
| 11361 | |
| 11362 | CGOpenMPRuntime::NontemporalDeclsRAII::~NontemporalDeclsRAII() { |
| 11363 | if (!NeedToPush) |
| 11364 | return; |
| 11365 | CGM.getOpenMPRuntime().NontemporalDeclsStack.pop_back(); |
| 11366 | } |
| 11367 | |
| 11368 | bool CGOpenMPRuntime::isNontemporalDecl(const ValueDecl *VD) const { |
| 11369 | assert(CGM.getLangOpts().OpenMP && "Not in OpenMP mode."); |
| 11370 | |
| 11371 | return llvm::any_of( |
| 11372 | CGM.getOpenMPRuntime().NontemporalDeclsStack, |
| 11373 | [VD](const NontemporalDeclsSet &Set) { return Set.count(VD) > 0; }); |
| 11374 | } |
| 11375 | |
| Alexey Bataev | 4697874 | 2020-01-30 10:46:11 -0500 | [diff] [blame] | 11376 | void CGOpenMPRuntime::LastprivateConditionalRAII::tryToDisableInnerAnalysis( |
| 11377 | const OMPExecutableDirective &S, |
| 11378 | llvm::DenseSet<CanonicalDeclPtr<const Decl>> &NeedToAddForLPCsAsDisabled) |
| 11379 | const { |
| 11380 | llvm::DenseSet<CanonicalDeclPtr<const Decl>> NeedToCheckForLPCs; |
| 11381 | // Vars in target/task regions must be excluded completely. |
| 11382 | if (isOpenMPTargetExecutionDirective(S.getDirectiveKind()) || |
| 11383 | isOpenMPTaskingDirective(S.getDirectiveKind())) { |
| 11384 | SmallVector<OpenMPDirectiveKind, 4> CaptureRegions; |
| 11385 | getOpenMPCaptureRegions(CaptureRegions, S.getDirectiveKind()); |
| 11386 | const CapturedStmt *CS = S.getCapturedStmt(CaptureRegions.front()); |
| 11387 | for (const CapturedStmt::Capture &Cap : CS->captures()) { |
| 11388 | if (Cap.capturesVariable() || Cap.capturesVariableByCopy()) |
| 11389 | NeedToCheckForLPCs.insert(Cap.getCapturedVar()); |
| 11390 | } |
| 11391 | } |
| 11392 | // Exclude vars in private clauses. |
| 11393 | for (const auto *C : S.getClausesOfKind<OMPPrivateClause>()) { |
| 11394 | for (const Expr *Ref : C->varlists()) { |
| 11395 | if (!Ref->getType()->isScalarType()) |
| 11396 | continue; |
| 11397 | const auto *DRE = dyn_cast<DeclRefExpr>(Ref->IgnoreParenImpCasts()); |
| 11398 | if (!DRE) |
| 11399 | continue; |
| 11400 | NeedToCheckForLPCs.insert(DRE->getDecl()); |
| 11401 | } |
| 11402 | } |
| 11403 | for (const auto *C : S.getClausesOfKind<OMPFirstprivateClause>()) { |
| 11404 | for (const Expr *Ref : C->varlists()) { |
| 11405 | if (!Ref->getType()->isScalarType()) |
| 11406 | continue; |
| 11407 | const auto *DRE = dyn_cast<DeclRefExpr>(Ref->IgnoreParenImpCasts()); |
| 11408 | if (!DRE) |
| 11409 | continue; |
| 11410 | NeedToCheckForLPCs.insert(DRE->getDecl()); |
| 11411 | } |
| 11412 | } |
| 11413 | for (const auto *C : S.getClausesOfKind<OMPLastprivateClause>()) { |
| 11414 | for (const Expr *Ref : C->varlists()) { |
| 11415 | if (!Ref->getType()->isScalarType()) |
| 11416 | continue; |
| 11417 | const auto *DRE = dyn_cast<DeclRefExpr>(Ref->IgnoreParenImpCasts()); |
| 11418 | if (!DRE) |
| 11419 | continue; |
| 11420 | NeedToCheckForLPCs.insert(DRE->getDecl()); |
| 11421 | } |
| 11422 | } |
| 11423 | for (const auto *C : S.getClausesOfKind<OMPReductionClause>()) { |
| 11424 | for (const Expr *Ref : C->varlists()) { |
| 11425 | if (!Ref->getType()->isScalarType()) |
| 11426 | continue; |
| 11427 | const auto *DRE = dyn_cast<DeclRefExpr>(Ref->IgnoreParenImpCasts()); |
| 11428 | if (!DRE) |
| 11429 | continue; |
| 11430 | NeedToCheckForLPCs.insert(DRE->getDecl()); |
| 11431 | } |
| 11432 | } |
| 11433 | for (const auto *C : S.getClausesOfKind<OMPLinearClause>()) { |
| 11434 | for (const Expr *Ref : C->varlists()) { |
| 11435 | if (!Ref->getType()->isScalarType()) |
| 11436 | continue; |
| 11437 | const auto *DRE = dyn_cast<DeclRefExpr>(Ref->IgnoreParenImpCasts()); |
| 11438 | if (!DRE) |
| 11439 | continue; |
| 11440 | NeedToCheckForLPCs.insert(DRE->getDecl()); |
| 11441 | } |
| 11442 | } |
| 11443 | for (const Decl *VD : NeedToCheckForLPCs) { |
| 11444 | for (const LastprivateConditionalData &Data : |
| 11445 | llvm::reverse(CGM.getOpenMPRuntime().LastprivateConditionalStack)) { |
| 11446 | if (Data.DeclToUniqueName.count(VD) > 0) { |
| 11447 | if (!Data.Disabled) |
| 11448 | NeedToAddForLPCsAsDisabled.insert(VD); |
| 11449 | break; |
| 11450 | } |
| 11451 | } |
| 11452 | } |
| 11453 | } |
| 11454 | |
| Alexey Bataev | a58da1a | 2019-12-27 09:44:43 -0500 | [diff] [blame] | 11455 | CGOpenMPRuntime::LastprivateConditionalRAII::LastprivateConditionalRAII( |
| 11456 | CodeGenFunction &CGF, const OMPExecutableDirective &S, LValue IVLVal) |
| 11457 | : CGM(CGF.CGM), |
| Alexey Bataev | 4697874 | 2020-01-30 10:46:11 -0500 | [diff] [blame] | 11458 | Action((CGM.getLangOpts().OpenMP >= 50 && |
| 11459 | llvm::any_of(S.getClausesOfKind<OMPLastprivateClause>(), |
| 11460 | [](const OMPLastprivateClause *C) { |
| 11461 | return C->getKind() == |
| 11462 | OMPC_LASTPRIVATE_conditional; |
| 11463 | })) |
| 11464 | ? ActionToDo::PushAsLastprivateConditional |
| 11465 | : ActionToDo::DoNotPush) { |
| Alexey Bataev | a58da1a | 2019-12-27 09:44:43 -0500 | [diff] [blame] | 11466 | assert(CGM.getLangOpts().OpenMP && "Not in OpenMP mode."); |
| Alexey Bataev | 4697874 | 2020-01-30 10:46:11 -0500 | [diff] [blame] | 11467 | if (CGM.getLangOpts().OpenMP < 50 || Action == ActionToDo::DoNotPush) |
| Alexey Bataev | a58da1a | 2019-12-27 09:44:43 -0500 | [diff] [blame] | 11468 | return; |
| Alexey Bataev | 4697874 | 2020-01-30 10:46:11 -0500 | [diff] [blame] | 11469 | assert(Action == ActionToDo::PushAsLastprivateConditional && |
| 11470 | "Expected a push action."); |
| Alexey Bataev | a58da1a | 2019-12-27 09:44:43 -0500 | [diff] [blame] | 11471 | LastprivateConditionalData &Data = |
| 11472 | CGM.getOpenMPRuntime().LastprivateConditionalStack.emplace_back(); |
| 11473 | for (const auto *C : S.getClausesOfKind<OMPLastprivateClause>()) { |
| 11474 | if (C->getKind() != OMPC_LASTPRIVATE_conditional) |
| 11475 | continue; |
| 11476 | |
| 11477 | for (const Expr *Ref : C->varlists()) { |
| Alexey Bataev | 4697874 | 2020-01-30 10:46:11 -0500 | [diff] [blame] | 11478 | Data.DeclToUniqueName.insert(std::make_pair( |
| Alexey Bataev | a58da1a | 2019-12-27 09:44:43 -0500 | [diff] [blame] | 11479 | cast<DeclRefExpr>(Ref->IgnoreParenImpCasts())->getDecl(), |
| Alexey Bataev | 4697874 | 2020-01-30 10:46:11 -0500 | [diff] [blame] | 11480 | SmallString<16>(generateUniqueName(CGM, "pl_cond", Ref)))); |
| Alexey Bataev | a58da1a | 2019-12-27 09:44:43 -0500 | [diff] [blame] | 11481 | } |
| 11482 | } |
| 11483 | Data.IVLVal = IVLVal; |
| Alexey Bataev | 4697874 | 2020-01-30 10:46:11 -0500 | [diff] [blame] | 11484 | Data.Fn = CGF.CurFn; |
| 11485 | } |
| 11486 | |
| 11487 | CGOpenMPRuntime::LastprivateConditionalRAII::LastprivateConditionalRAII( |
| 11488 | CodeGenFunction &CGF, const OMPExecutableDirective &S) |
| 11489 | : CGM(CGF.CGM), Action(ActionToDo::DoNotPush) { |
| 11490 | assert(CGM.getLangOpts().OpenMP && "Not in OpenMP mode."); |
| 11491 | if (CGM.getLangOpts().OpenMP < 50) |
| 11492 | return; |
| 11493 | llvm::DenseSet<CanonicalDeclPtr<const Decl>> NeedToAddForLPCsAsDisabled; |
| 11494 | tryToDisableInnerAnalysis(S, NeedToAddForLPCsAsDisabled); |
| 11495 | if (!NeedToAddForLPCsAsDisabled.empty()) { |
| 11496 | Action = ActionToDo::DisableLastprivateConditional; |
| 11497 | LastprivateConditionalData &Data = |
| 11498 | CGM.getOpenMPRuntime().LastprivateConditionalStack.emplace_back(); |
| 11499 | for (const Decl *VD : NeedToAddForLPCsAsDisabled) |
| 11500 | Data.DeclToUniqueName.insert(std::make_pair(VD, SmallString<16>())); |
| 11501 | Data.Fn = CGF.CurFn; |
| 11502 | Data.Disabled = true; |
| 11503 | } |
| 11504 | } |
| 11505 | |
| 11506 | CGOpenMPRuntime::LastprivateConditionalRAII |
| 11507 | CGOpenMPRuntime::LastprivateConditionalRAII::disable( |
| 11508 | CodeGenFunction &CGF, const OMPExecutableDirective &S) { |
| 11509 | return LastprivateConditionalRAII(CGF, S); |
| Alexey Bataev | a58da1a | 2019-12-27 09:44:43 -0500 | [diff] [blame] | 11510 | } |
| 11511 | |
| 11512 | CGOpenMPRuntime::LastprivateConditionalRAII::~LastprivateConditionalRAII() { |
| Alexey Bataev | 4697874 | 2020-01-30 10:46:11 -0500 | [diff] [blame] | 11513 | if (CGM.getLangOpts().OpenMP < 50) |
| Alexey Bataev | a58da1a | 2019-12-27 09:44:43 -0500 | [diff] [blame] | 11514 | return; |
| Alexey Bataev | 4697874 | 2020-01-30 10:46:11 -0500 | [diff] [blame] | 11515 | if (Action == ActionToDo::DisableLastprivateConditional) { |
| 11516 | assert(CGM.getOpenMPRuntime().LastprivateConditionalStack.back().Disabled && |
| 11517 | "Expected list of disabled private vars."); |
| 11518 | CGM.getOpenMPRuntime().LastprivateConditionalStack.pop_back(); |
| 11519 | } |
| 11520 | if (Action == ActionToDo::PushAsLastprivateConditional) { |
| 11521 | assert( |
| 11522 | !CGM.getOpenMPRuntime().LastprivateConditionalStack.back().Disabled && |
| 11523 | "Expected list of lastprivate conditional vars."); |
| 11524 | CGM.getOpenMPRuntime().LastprivateConditionalStack.pop_back(); |
| 11525 | } |
| 11526 | } |
| 11527 | |
| 11528 | Address CGOpenMPRuntime::emitLastprivateConditionalInit(CodeGenFunction &CGF, |
| 11529 | const VarDecl *VD) { |
| 11530 | ASTContext &C = CGM.getContext(); |
| 11531 | auto I = LastprivateConditionalToTypes.find(CGF.CurFn); |
| 11532 | if (I == LastprivateConditionalToTypes.end()) |
| 11533 | I = LastprivateConditionalToTypes.try_emplace(CGF.CurFn).first; |
| 11534 | QualType NewType; |
| 11535 | const FieldDecl *VDField; |
| 11536 | const FieldDecl *FiredField; |
| 11537 | LValue BaseLVal; |
| 11538 | auto VI = I->getSecond().find(VD); |
| 11539 | if (VI == I->getSecond().end()) { |
| 11540 | RecordDecl *RD = C.buildImplicitRecord("lasprivate.conditional"); |
| 11541 | RD->startDefinition(); |
| 11542 | VDField = addFieldToRecordDecl(C, RD, VD->getType().getNonReferenceType()); |
| 11543 | FiredField = addFieldToRecordDecl(C, RD, C.CharTy); |
| 11544 | RD->completeDefinition(); |
| 11545 | NewType = C.getRecordType(RD); |
| 11546 | Address Addr = CGF.CreateMemTemp(NewType, C.getDeclAlign(VD), VD->getName()); |
| 11547 | BaseLVal = CGF.MakeAddrLValue(Addr, NewType, AlignmentSource::Decl); |
| 11548 | I->getSecond().try_emplace(VD, NewType, VDField, FiredField, BaseLVal); |
| 11549 | } else { |
| 11550 | NewType = std::get<0>(VI->getSecond()); |
| 11551 | VDField = std::get<1>(VI->getSecond()); |
| 11552 | FiredField = std::get<2>(VI->getSecond()); |
| 11553 | BaseLVal = std::get<3>(VI->getSecond()); |
| 11554 | } |
| 11555 | LValue FiredLVal = |
| 11556 | CGF.EmitLValueForField(BaseLVal, FiredField); |
| 11557 | CGF.EmitStoreOfScalar( |
| 11558 | llvm::ConstantInt::getNullValue(CGF.ConvertTypeForMem(C.CharTy)), |
| 11559 | FiredLVal); |
| 11560 | return CGF.EmitLValueForField(BaseLVal, VDField).getAddress(CGF); |
| Alexey Bataev | a58da1a | 2019-12-27 09:44:43 -0500 | [diff] [blame] | 11561 | } |
| 11562 | |
| 11563 | namespace { |
| 11564 | /// Checks if the lastprivate conditional variable is referenced in LHS. |
| 11565 | class LastprivateConditionalRefChecker final |
| 11566 | : public ConstStmtVisitor<LastprivateConditionalRefChecker, bool> { |
| Alexey Bataev | a58da1a | 2019-12-27 09:44:43 -0500 | [diff] [blame] | 11567 | ArrayRef<CGOpenMPRuntime::LastprivateConditionalData> LPM; |
| 11568 | const Expr *FoundE = nullptr; |
| 11569 | const Decl *FoundD = nullptr; |
| 11570 | StringRef UniqueDeclName; |
| 11571 | LValue IVLVal; |
| Alexey Bataev | 4697874 | 2020-01-30 10:46:11 -0500 | [diff] [blame] | 11572 | llvm::Function *FoundFn = nullptr; |
| Alexey Bataev | a58da1a | 2019-12-27 09:44:43 -0500 | [diff] [blame] | 11573 | SourceLocation Loc; |
| Alexey Bataev | a58da1a | 2019-12-27 09:44:43 -0500 | [diff] [blame] | 11574 | |
| 11575 | public: |
| 11576 | bool VisitDeclRefExpr(const DeclRefExpr *E) { |
| 11577 | for (const CGOpenMPRuntime::LastprivateConditionalData &D : |
| 11578 | llvm::reverse(LPM)) { |
| Alexey Bataev | 4697874 | 2020-01-30 10:46:11 -0500 | [diff] [blame] | 11579 | auto It = D.DeclToUniqueName.find(E->getDecl()); |
| 11580 | if (It == D.DeclToUniqueName.end()) |
| Alexey Bataev | a58da1a | 2019-12-27 09:44:43 -0500 | [diff] [blame] | 11581 | continue; |
| Alexey Bataev | 4697874 | 2020-01-30 10:46:11 -0500 | [diff] [blame] | 11582 | if (D.Disabled) |
| 11583 | return false; |
| Alexey Bataev | a58da1a | 2019-12-27 09:44:43 -0500 | [diff] [blame] | 11584 | FoundE = E; |
| 11585 | FoundD = E->getDecl()->getCanonicalDecl(); |
| Alexey Bataev | 4697874 | 2020-01-30 10:46:11 -0500 | [diff] [blame] | 11586 | UniqueDeclName = It->second; |
| Alexey Bataev | a58da1a | 2019-12-27 09:44:43 -0500 | [diff] [blame] | 11587 | IVLVal = D.IVLVal; |
| Alexey Bataev | 4697874 | 2020-01-30 10:46:11 -0500 | [diff] [blame] | 11588 | FoundFn = D.Fn; |
| Alexey Bataev | a58da1a | 2019-12-27 09:44:43 -0500 | [diff] [blame] | 11589 | break; |
| 11590 | } |
| 11591 | return FoundE == E; |
| 11592 | } |
| 11593 | bool VisitMemberExpr(const MemberExpr *E) { |
| Alexey Bataev | 4697874 | 2020-01-30 10:46:11 -0500 | [diff] [blame] | 11594 | if (!CodeGenFunction::IsWrappedCXXThis(E->getBase())) |
| Alexey Bataev | a58da1a | 2019-12-27 09:44:43 -0500 | [diff] [blame] | 11595 | return false; |
| 11596 | for (const CGOpenMPRuntime::LastprivateConditionalData &D : |
| 11597 | llvm::reverse(LPM)) { |
| Alexey Bataev | 4697874 | 2020-01-30 10:46:11 -0500 | [diff] [blame] | 11598 | auto It = D.DeclToUniqueName.find(E->getMemberDecl()); |
| 11599 | if (It == D.DeclToUniqueName.end()) |
| Alexey Bataev | a58da1a | 2019-12-27 09:44:43 -0500 | [diff] [blame] | 11600 | continue; |
| Alexey Bataev | 4697874 | 2020-01-30 10:46:11 -0500 | [diff] [blame] | 11601 | if (D.Disabled) |
| 11602 | return false; |
| Alexey Bataev | a58da1a | 2019-12-27 09:44:43 -0500 | [diff] [blame] | 11603 | FoundE = E; |
| 11604 | FoundD = E->getMemberDecl()->getCanonicalDecl(); |
| Alexey Bataev | 4697874 | 2020-01-30 10:46:11 -0500 | [diff] [blame] | 11605 | UniqueDeclName = It->second; |
| Alexey Bataev | a58da1a | 2019-12-27 09:44:43 -0500 | [diff] [blame] | 11606 | IVLVal = D.IVLVal; |
| Alexey Bataev | 4697874 | 2020-01-30 10:46:11 -0500 | [diff] [blame] | 11607 | FoundFn = D.Fn; |
| Alexey Bataev | a58da1a | 2019-12-27 09:44:43 -0500 | [diff] [blame] | 11608 | break; |
| 11609 | } |
| 11610 | return FoundE == E; |
| 11611 | } |
| 11612 | bool VisitStmt(const Stmt *S) { |
| 11613 | for (const Stmt *Child : S->children()) { |
| 11614 | if (!Child) |
| 11615 | continue; |
| 11616 | if (const auto *E = dyn_cast<Expr>(Child)) |
| 11617 | if (!E->isGLValue()) |
| 11618 | continue; |
| 11619 | if (Visit(Child)) |
| 11620 | return true; |
| 11621 | } |
| 11622 | return false; |
| 11623 | } |
| 11624 | explicit LastprivateConditionalRefChecker( |
| Alexey Bataev | a58da1a | 2019-12-27 09:44:43 -0500 | [diff] [blame] | 11625 | ArrayRef<CGOpenMPRuntime::LastprivateConditionalData> LPM) |
| Alexey Bataev | 4697874 | 2020-01-30 10:46:11 -0500 | [diff] [blame] | 11626 | : LPM(LPM) {} |
| 11627 | std::tuple<const Expr *, const Decl *, StringRef, LValue, llvm::Function *> |
| Alexey Bataev | a58da1a | 2019-12-27 09:44:43 -0500 | [diff] [blame] | 11628 | getFoundData() const { |
| Alexey Bataev | 4697874 | 2020-01-30 10:46:11 -0500 | [diff] [blame] | 11629 | return std::make_tuple(FoundE, FoundD, UniqueDeclName, IVLVal, FoundFn); |
| Alexey Bataev | a58da1a | 2019-12-27 09:44:43 -0500 | [diff] [blame] | 11630 | } |
| 11631 | }; |
| 11632 | } // namespace |
| 11633 | |
| Alexey Bataev | 4697874 | 2020-01-30 10:46:11 -0500 | [diff] [blame] | 11634 | void CGOpenMPRuntime::emitLastprivateConditionalUpdate(CodeGenFunction &CGF, |
| 11635 | LValue IVLVal, |
| 11636 | StringRef UniqueDeclName, |
| 11637 | LValue LVal, |
| 11638 | SourceLocation Loc) { |
| Alexey Bataev | a58da1a | 2019-12-27 09:44:43 -0500 | [diff] [blame] | 11639 | // Last updated loop counter for the lastprivate conditional var. |
| 11640 | // int<xx> last_iv = 0; |
| 11641 | llvm::Type *LLIVTy = CGF.ConvertTypeForMem(IVLVal.getType()); |
| 11642 | llvm::Constant *LastIV = |
| Alexey Bataev | 8b32192 | 2020-01-16 15:46:34 -0500 | [diff] [blame] | 11643 | getOrCreateInternalVariable(LLIVTy, getName({UniqueDeclName, "iv"})); |
| Alexey Bataev | a58da1a | 2019-12-27 09:44:43 -0500 | [diff] [blame] | 11644 | cast<llvm::GlobalVariable>(LastIV)->setAlignment( |
| 11645 | IVLVal.getAlignment().getAsAlign()); |
| 11646 | LValue LastIVLVal = CGF.MakeNaturalAlignAddrLValue(LastIV, IVLVal.getType()); |
| 11647 | |
| Alexey Bataev | a58da1a | 2019-12-27 09:44:43 -0500 | [diff] [blame] | 11648 | // Last value of the lastprivate conditional. |
| 11649 | // decltype(priv_a) last_a; |
| 11650 | llvm::Constant *Last = getOrCreateInternalVariable( |
| Alexey Bataev | 4697874 | 2020-01-30 10:46:11 -0500 | [diff] [blame] | 11651 | CGF.ConvertTypeForMem(LVal.getType()), UniqueDeclName); |
| Alexey Bataev | a58da1a | 2019-12-27 09:44:43 -0500 | [diff] [blame] | 11652 | cast<llvm::GlobalVariable>(Last)->setAlignment( |
| 11653 | LVal.getAlignment().getAsAlign()); |
| 11654 | LValue LastLVal = |
| 11655 | CGF.MakeAddrLValue(Last, LVal.getType(), LVal.getAlignment()); |
| 11656 | |
| 11657 | // Global loop counter. Required to handle inner parallel-for regions. |
| Alexey Bataev | 4697874 | 2020-01-30 10:46:11 -0500 | [diff] [blame] | 11658 | // iv |
| 11659 | llvm::Value *IVVal = CGF.EmitLoadOfScalar(IVLVal, Loc); |
| Alexey Bataev | a58da1a | 2019-12-27 09:44:43 -0500 | [diff] [blame] | 11660 | |
| 11661 | // #pragma omp critical(a) |
| 11662 | // if (last_iv <= iv) { |
| 11663 | // last_iv = iv; |
| 11664 | // last_a = priv_a; |
| 11665 | // } |
| 11666 | auto &&CodeGen = [&LastIVLVal, &IVLVal, IVVal, &LVal, &LastLVal, |
| Alexey Bataev | 4697874 | 2020-01-30 10:46:11 -0500 | [diff] [blame] | 11667 | Loc](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| Alexey Bataev | a58da1a | 2019-12-27 09:44:43 -0500 | [diff] [blame] | 11668 | Action.Enter(CGF); |
| Alexey Bataev | 4697874 | 2020-01-30 10:46:11 -0500 | [diff] [blame] | 11669 | llvm::Value *LastIVVal = CGF.EmitLoadOfScalar(LastIVLVal, Loc); |
| 11670 | // (last_iv <= iv) ? Check if the variable is updated and store new |
| Alexey Bataev | a58da1a | 2019-12-27 09:44:43 -0500 | [diff] [blame] | 11671 | // value in global var. |
| 11672 | llvm::Value *CmpRes; |
| 11673 | if (IVLVal.getType()->isSignedIntegerType()) { |
| 11674 | CmpRes = CGF.Builder.CreateICmpSLE(LastIVVal, IVVal); |
| 11675 | } else { |
| 11676 | assert(IVLVal.getType()->isUnsignedIntegerType() && |
| 11677 | "Loop iteration variable must be integer."); |
| 11678 | CmpRes = CGF.Builder.CreateICmpULE(LastIVVal, IVVal); |
| 11679 | } |
| 11680 | llvm::BasicBlock *ThenBB = CGF.createBasicBlock("lp_cond_then"); |
| 11681 | llvm::BasicBlock *ExitBB = CGF.createBasicBlock("lp_cond_exit"); |
| 11682 | CGF.Builder.CreateCondBr(CmpRes, ThenBB, ExitBB); |
| 11683 | // { |
| 11684 | CGF.EmitBlock(ThenBB); |
| 11685 | |
| Alexey Bataev | 4697874 | 2020-01-30 10:46:11 -0500 | [diff] [blame] | 11686 | // last_iv = iv; |
| Alexey Bataev | a58da1a | 2019-12-27 09:44:43 -0500 | [diff] [blame] | 11687 | CGF.EmitStoreOfScalar(IVVal, LastIVLVal); |
| 11688 | |
| 11689 | // last_a = priv_a; |
| 11690 | switch (CGF.getEvaluationKind(LVal.getType())) { |
| 11691 | case TEK_Scalar: { |
| Alexey Bataev | 4697874 | 2020-01-30 10:46:11 -0500 | [diff] [blame] | 11692 | llvm::Value *PrivVal = CGF.EmitLoadOfScalar(LVal, Loc); |
| Alexey Bataev | a58da1a | 2019-12-27 09:44:43 -0500 | [diff] [blame] | 11693 | CGF.EmitStoreOfScalar(PrivVal, LastLVal); |
| 11694 | break; |
| 11695 | } |
| 11696 | case TEK_Complex: { |
| Alexey Bataev | 4697874 | 2020-01-30 10:46:11 -0500 | [diff] [blame] | 11697 | CodeGenFunction::ComplexPairTy PrivVal = CGF.EmitLoadOfComplex(LVal, Loc); |
| Alexey Bataev | a58da1a | 2019-12-27 09:44:43 -0500 | [diff] [blame] | 11698 | CGF.EmitStoreOfComplex(PrivVal, LastLVal, /*isInit=*/false); |
| 11699 | break; |
| 11700 | } |
| 11701 | case TEK_Aggregate: |
| 11702 | llvm_unreachable( |
| 11703 | "Aggregates are not supported in lastprivate conditional."); |
| 11704 | } |
| 11705 | // } |
| 11706 | CGF.EmitBranch(ExitBB); |
| 11707 | // There is no need to emit line number for unconditional branch. |
| 11708 | (void)ApplyDebugLocation::CreateEmpty(CGF); |
| 11709 | CGF.EmitBlock(ExitBB, /*IsFinished=*/true); |
| 11710 | }; |
| 11711 | |
| 11712 | if (CGM.getLangOpts().OpenMPSimd) { |
| 11713 | // Do not emit as a critical region as no parallel region could be emitted. |
| 11714 | RegionCodeGenTy ThenRCG(CodeGen); |
| 11715 | ThenRCG(CGF); |
| 11716 | } else { |
| Alexey Bataev | 4697874 | 2020-01-30 10:46:11 -0500 | [diff] [blame] | 11717 | emitCriticalRegion(CGF, UniqueDeclName, CodeGen, Loc); |
| 11718 | } |
| 11719 | } |
| 11720 | |
| 11721 | void CGOpenMPRuntime::checkAndEmitLastprivateConditional(CodeGenFunction &CGF, |
| 11722 | const Expr *LHS) { |
| 11723 | if (CGF.getLangOpts().OpenMP < 50 || LastprivateConditionalStack.empty()) |
| 11724 | return; |
| 11725 | LastprivateConditionalRefChecker Checker(LastprivateConditionalStack); |
| 11726 | if (!Checker.Visit(LHS)) |
| 11727 | return; |
| 11728 | const Expr *FoundE; |
| 11729 | const Decl *FoundD; |
| 11730 | StringRef UniqueDeclName; |
| 11731 | LValue IVLVal; |
| 11732 | llvm::Function *FoundFn; |
| 11733 | std::tie(FoundE, FoundD, UniqueDeclName, IVLVal, FoundFn) = |
| 11734 | Checker.getFoundData(); |
| 11735 | if (FoundFn != CGF.CurFn) { |
| 11736 | // Special codegen for inner parallel regions. |
| 11737 | // ((struct.lastprivate.conditional*)&priv_a)->Fired = 1; |
| 11738 | auto It = LastprivateConditionalToTypes[FoundFn].find(FoundD); |
| 11739 | assert(It != LastprivateConditionalToTypes[FoundFn].end() && |
| 11740 | "Lastprivate conditional is not found in outer region."); |
| 11741 | QualType StructTy = std::get<0>(It->getSecond()); |
| 11742 | const FieldDecl* FiredDecl = std::get<2>(It->getSecond()); |
| 11743 | LValue PrivLVal = CGF.EmitLValue(FoundE); |
| 11744 | Address StructAddr = CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
| 11745 | PrivLVal.getAddress(CGF), |
| 11746 | CGF.ConvertTypeForMem(CGF.getContext().getPointerType(StructTy))); |
| 11747 | LValue BaseLVal = |
| 11748 | CGF.MakeAddrLValue(StructAddr, StructTy, AlignmentSource::Decl); |
| 11749 | LValue FiredLVal = CGF.EmitLValueForField(BaseLVal, FiredDecl); |
| 11750 | CGF.EmitAtomicStore(RValue::get(llvm::ConstantInt::get( |
| 11751 | CGF.ConvertTypeForMem(FiredDecl->getType()), 1)), |
| 11752 | FiredLVal, llvm::AtomicOrdering::Unordered, |
| 11753 | /*IsVolatile=*/true, /*isInit=*/false); |
| 11754 | return; |
| 11755 | } |
| 11756 | |
| 11757 | // Private address of the lastprivate conditional in the current context. |
| 11758 | // priv_a |
| 11759 | LValue LVal = CGF.EmitLValue(FoundE); |
| 11760 | emitLastprivateConditionalUpdate(CGF, IVLVal, UniqueDeclName, LVal, |
| 11761 | FoundE->getExprLoc()); |
| 11762 | } |
| 11763 | |
| 11764 | void CGOpenMPRuntime::checkAndEmitSharedLastprivateConditional( |
| 11765 | CodeGenFunction &CGF, const OMPExecutableDirective &D, |
| 11766 | const llvm::DenseSet<CanonicalDeclPtr<const VarDecl>> &IgnoredDecls) { |
| 11767 | if (CGF.getLangOpts().OpenMP < 50 || LastprivateConditionalStack.empty()) |
| 11768 | return; |
| 11769 | auto Range = llvm::reverse(LastprivateConditionalStack); |
| 11770 | auto It = llvm::find_if( |
| 11771 | Range, [](const LastprivateConditionalData &D) { return !D.Disabled; }); |
| 11772 | if (It == Range.end() || It->Fn != CGF.CurFn) |
| 11773 | return; |
| 11774 | auto LPCI = LastprivateConditionalToTypes.find(It->Fn); |
| 11775 | assert(LPCI != LastprivateConditionalToTypes.end() && |
| 11776 | "Lastprivates must be registered already."); |
| 11777 | SmallVector<OpenMPDirectiveKind, 4> CaptureRegions; |
| 11778 | getOpenMPCaptureRegions(CaptureRegions, D.getDirectiveKind()); |
| 11779 | const CapturedStmt *CS = D.getCapturedStmt(CaptureRegions.back()); |
| 11780 | for (const auto &Pair : It->DeclToUniqueName) { |
| 11781 | const auto *VD = cast<VarDecl>(Pair.first->getCanonicalDecl()); |
| 11782 | if (!CS->capturesVariable(VD) || IgnoredDecls.count(VD) > 0) |
| 11783 | continue; |
| 11784 | auto I = LPCI->getSecond().find(Pair.first); |
| 11785 | assert(I != LPCI->getSecond().end() && |
| 11786 | "Lastprivate must be rehistered already."); |
| 11787 | // bool Cmp = priv_a.Fired != 0; |
| 11788 | LValue BaseLVal = std::get<3>(I->getSecond()); |
| 11789 | LValue FiredLVal = |
| 11790 | CGF.EmitLValueForField(BaseLVal, std::get<2>(I->getSecond())); |
| 11791 | llvm::Value *Res = CGF.EmitLoadOfScalar(FiredLVal, D.getBeginLoc()); |
| 11792 | llvm::Value *Cmp = CGF.Builder.CreateIsNotNull(Res); |
| 11793 | llvm::BasicBlock *ThenBB = CGF.createBasicBlock("lpc.then"); |
| 11794 | llvm::BasicBlock *DoneBB = CGF.createBasicBlock("lpc.done"); |
| 11795 | // if (Cmp) { |
| 11796 | CGF.Builder.CreateCondBr(Cmp, ThenBB, DoneBB); |
| 11797 | CGF.EmitBlock(ThenBB); |
| 11798 | Address Addr = CGF.GetAddrOfLocalVar(VD); |
| 11799 | LValue LVal; |
| 11800 | if (VD->getType()->isReferenceType()) |
| 11801 | LVal = CGF.EmitLoadOfReferenceLValue(Addr, VD->getType(), |
| 11802 | AlignmentSource::Decl); |
| 11803 | else |
| 11804 | LVal = CGF.MakeAddrLValue(Addr, VD->getType().getNonReferenceType(), |
| 11805 | AlignmentSource::Decl); |
| 11806 | emitLastprivateConditionalUpdate(CGF, It->IVLVal, Pair.second, LVal, |
| 11807 | D.getBeginLoc()); |
| 11808 | auto AL = ApplyDebugLocation::CreateArtificial(CGF); |
| 11809 | CGF.EmitBlock(DoneBB, /*IsFinal=*/true); |
| 11810 | // } |
| Alexey Bataev | a58da1a | 2019-12-27 09:44:43 -0500 | [diff] [blame] | 11811 | } |
| 11812 | } |
| 11813 | |
| 11814 | void CGOpenMPRuntime::emitLastprivateConditionalFinalUpdate( |
| 11815 | CodeGenFunction &CGF, LValue PrivLVal, const VarDecl *VD, |
| 11816 | SourceLocation Loc) { |
| 11817 | if (CGF.getLangOpts().OpenMP < 50) |
| 11818 | return; |
| Alexey Bataev | 4697874 | 2020-01-30 10:46:11 -0500 | [diff] [blame] | 11819 | auto It = LastprivateConditionalStack.back().DeclToUniqueName.find(VD); |
| 11820 | assert(It != LastprivateConditionalStack.back().DeclToUniqueName.end() && |
| Alexey Bataev | a58da1a | 2019-12-27 09:44:43 -0500 | [diff] [blame] | 11821 | "Unknown lastprivate conditional variable."); |
| Alexey Bataev | 4697874 | 2020-01-30 10:46:11 -0500 | [diff] [blame] | 11822 | StringRef UniqueName = It->second; |
| Alexey Bataev | a58da1a | 2019-12-27 09:44:43 -0500 | [diff] [blame] | 11823 | llvm::GlobalVariable *GV = CGM.getModule().getNamedGlobal(UniqueName); |
| 11824 | // The variable was not updated in the region - exit. |
| 11825 | if (!GV) |
| 11826 | return; |
| 11827 | LValue LPLVal = CGF.MakeAddrLValue( |
| 11828 | GV, PrivLVal.getType().getNonReferenceType(), PrivLVal.getAlignment()); |
| 11829 | llvm::Value *Res = CGF.EmitLoadOfScalar(LPLVal, Loc); |
| 11830 | CGF.EmitStoreOfScalar(Res, PrivLVal); |
| 11831 | } |
| 11832 | |
| James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 11833 | llvm::Function *CGOpenMPSIMDRuntime::emitParallelOutlinedFunction( |
| Alexey Bataev | a8a9153a | 2017-12-29 18:07:07 +0000 | [diff] [blame] | 11834 | const OMPExecutableDirective &D, const VarDecl *ThreadIDVar, |
| 11835 | OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen) { |
| 11836 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 11837 | } |
| 11838 | |
| James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 11839 | llvm::Function *CGOpenMPSIMDRuntime::emitTeamsOutlinedFunction( |
| Alexey Bataev | a8a9153a | 2017-12-29 18:07:07 +0000 | [diff] [blame] | 11840 | const OMPExecutableDirective &D, const VarDecl *ThreadIDVar, |
| 11841 | OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen) { |
| 11842 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 11843 | } |
| 11844 | |
| James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 11845 | llvm::Function *CGOpenMPSIMDRuntime::emitTaskOutlinedFunction( |
| Alexey Bataev | a8a9153a | 2017-12-29 18:07:07 +0000 | [diff] [blame] | 11846 | const OMPExecutableDirective &D, const VarDecl *ThreadIDVar, |
| 11847 | const VarDecl *PartIDVar, const VarDecl *TaskTVar, |
| 11848 | OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen, |
| 11849 | bool Tied, unsigned &NumberOfParts) { |
| 11850 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 11851 | } |
| 11852 | |
| 11853 | void CGOpenMPSIMDRuntime::emitParallelCall(CodeGenFunction &CGF, |
| 11854 | SourceLocation Loc, |
| James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 11855 | llvm::Function *OutlinedFn, |
| Alexey Bataev | a8a9153a | 2017-12-29 18:07:07 +0000 | [diff] [blame] | 11856 | ArrayRef<llvm::Value *> CapturedVars, |
| 11857 | const Expr *IfCond) { |
| 11858 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 11859 | } |
| 11860 | |
| 11861 | void CGOpenMPSIMDRuntime::emitCriticalRegion( |
| 11862 | CodeGenFunction &CGF, StringRef CriticalName, |
| 11863 | const RegionCodeGenTy &CriticalOpGen, SourceLocation Loc, |
| 11864 | const Expr *Hint) { |
| 11865 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 11866 | } |
| 11867 | |
| 11868 | void CGOpenMPSIMDRuntime::emitMasterRegion(CodeGenFunction &CGF, |
| 11869 | const RegionCodeGenTy &MasterOpGen, |
| 11870 | SourceLocation Loc) { |
| 11871 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 11872 | } |
| 11873 | |
| 11874 | void CGOpenMPSIMDRuntime::emitTaskyieldCall(CodeGenFunction &CGF, |
| 11875 | SourceLocation Loc) { |
| 11876 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 11877 | } |
| 11878 | |
| 11879 | void CGOpenMPSIMDRuntime::emitTaskgroupRegion( |
| 11880 | CodeGenFunction &CGF, const RegionCodeGenTy &TaskgroupOpGen, |
| 11881 | SourceLocation Loc) { |
| 11882 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 11883 | } |
| 11884 | |
| 11885 | void CGOpenMPSIMDRuntime::emitSingleRegion( |
| 11886 | CodeGenFunction &CGF, const RegionCodeGenTy &SingleOpGen, |
| 11887 | SourceLocation Loc, ArrayRef<const Expr *> CopyprivateVars, |
| 11888 | ArrayRef<const Expr *> DestExprs, ArrayRef<const Expr *> SrcExprs, |
| 11889 | ArrayRef<const Expr *> AssignmentOps) { |
| 11890 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 11891 | } |
| 11892 | |
| 11893 | void CGOpenMPSIMDRuntime::emitOrderedRegion(CodeGenFunction &CGF, |
| 11894 | const RegionCodeGenTy &OrderedOpGen, |
| 11895 | SourceLocation Loc, |
| 11896 | bool IsThreads) { |
| 11897 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 11898 | } |
| 11899 | |
| 11900 | void CGOpenMPSIMDRuntime::emitBarrierCall(CodeGenFunction &CGF, |
| 11901 | SourceLocation Loc, |
| 11902 | OpenMPDirectiveKind Kind, |
| 11903 | bool EmitChecks, |
| 11904 | bool ForceSimpleCall) { |
| 11905 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 11906 | } |
| 11907 | |
| 11908 | void CGOpenMPSIMDRuntime::emitForDispatchInit( |
| 11909 | CodeGenFunction &CGF, SourceLocation Loc, |
| 11910 | const OpenMPScheduleTy &ScheduleKind, unsigned IVSize, bool IVSigned, |
| 11911 | bool Ordered, const DispatchRTInput &DispatchValues) { |
| 11912 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 11913 | } |
| 11914 | |
| 11915 | void CGOpenMPSIMDRuntime::emitForStaticInit( |
| 11916 | CodeGenFunction &CGF, SourceLocation Loc, OpenMPDirectiveKind DKind, |
| 11917 | const OpenMPScheduleTy &ScheduleKind, const StaticRTInput &Values) { |
| 11918 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 11919 | } |
| 11920 | |
| 11921 | void CGOpenMPSIMDRuntime::emitDistributeStaticInit( |
| 11922 | CodeGenFunction &CGF, SourceLocation Loc, |
| 11923 | OpenMPDistScheduleClauseKind SchedKind, const StaticRTInput &Values) { |
| 11924 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 11925 | } |
| 11926 | |
| 11927 | void CGOpenMPSIMDRuntime::emitForOrderedIterationEnd(CodeGenFunction &CGF, |
| 11928 | SourceLocation Loc, |
| 11929 | unsigned IVSize, |
| 11930 | bool IVSigned) { |
| 11931 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 11932 | } |
| 11933 | |
| 11934 | void CGOpenMPSIMDRuntime::emitForStaticFinish(CodeGenFunction &CGF, |
| 11935 | SourceLocation Loc, |
| 11936 | OpenMPDirectiveKind DKind) { |
| 11937 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 11938 | } |
| 11939 | |
| 11940 | llvm::Value *CGOpenMPSIMDRuntime::emitForNext(CodeGenFunction &CGF, |
| 11941 | SourceLocation Loc, |
| 11942 | unsigned IVSize, bool IVSigned, |
| 11943 | Address IL, Address LB, |
| 11944 | Address UB, Address ST) { |
| 11945 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 11946 | } |
| 11947 | |
| 11948 | void CGOpenMPSIMDRuntime::emitNumThreadsClause(CodeGenFunction &CGF, |
| 11949 | llvm::Value *NumThreads, |
| 11950 | SourceLocation Loc) { |
| 11951 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 11952 | } |
| 11953 | |
| 11954 | void CGOpenMPSIMDRuntime::emitProcBindClause(CodeGenFunction &CGF, |
| Johannes Doerfert | 6c5d1f40 | 2019-12-25 18:15:36 -0600 | [diff] [blame] | 11955 | ProcBindKind ProcBind, |
| Alexey Bataev | a8a9153a | 2017-12-29 18:07:07 +0000 | [diff] [blame] | 11956 | SourceLocation Loc) { |
| 11957 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 11958 | } |
| 11959 | |
| 11960 | Address CGOpenMPSIMDRuntime::getAddrOfThreadPrivate(CodeGenFunction &CGF, |
| 11961 | const VarDecl *VD, |
| 11962 | Address VDAddr, |
| 11963 | SourceLocation Loc) { |
| 11964 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 11965 | } |
| 11966 | |
| 11967 | llvm::Function *CGOpenMPSIMDRuntime::emitThreadPrivateVarDefinition( |
| 11968 | const VarDecl *VD, Address VDAddr, SourceLocation Loc, bool PerformInit, |
| 11969 | CodeGenFunction *CGF) { |
| 11970 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 11971 | } |
| 11972 | |
| 11973 | Address CGOpenMPSIMDRuntime::getAddrOfArtificialThreadPrivate( |
| 11974 | CodeGenFunction &CGF, QualType VarType, StringRef Name) { |
| 11975 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 11976 | } |
| 11977 | |
| 11978 | void CGOpenMPSIMDRuntime::emitFlush(CodeGenFunction &CGF, |
| 11979 | ArrayRef<const Expr *> Vars, |
| Alexey Bataev | e8e05de | 2020-02-07 12:22:23 -0500 | [diff] [blame] | 11980 | SourceLocation Loc, |
| 11981 | llvm::AtomicOrdering AO) { |
| Alexey Bataev | a8a9153a | 2017-12-29 18:07:07 +0000 | [diff] [blame] | 11982 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 11983 | } |
| 11984 | |
| 11985 | void CGOpenMPSIMDRuntime::emitTaskCall(CodeGenFunction &CGF, SourceLocation Loc, |
| 11986 | const OMPExecutableDirective &D, |
| James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 11987 | llvm::Function *TaskFunction, |
| Alexey Bataev | a8a9153a | 2017-12-29 18:07:07 +0000 | [diff] [blame] | 11988 | QualType SharedsTy, Address Shareds, |
| 11989 | const Expr *IfCond, |
| 11990 | const OMPTaskDataTy &Data) { |
| 11991 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 11992 | } |
| 11993 | |
| 11994 | void CGOpenMPSIMDRuntime::emitTaskLoopCall( |
| 11995 | CodeGenFunction &CGF, SourceLocation Loc, const OMPLoopDirective &D, |
| James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 11996 | llvm::Function *TaskFunction, QualType SharedsTy, Address Shareds, |
| Alexey Bataev | a8a9153a | 2017-12-29 18:07:07 +0000 | [diff] [blame] | 11997 | const Expr *IfCond, const OMPTaskDataTy &Data) { |
| 11998 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 11999 | } |
| 12000 | |
| 12001 | void CGOpenMPSIMDRuntime::emitReduction( |
| 12002 | CodeGenFunction &CGF, SourceLocation Loc, ArrayRef<const Expr *> Privates, |
| 12003 | ArrayRef<const Expr *> LHSExprs, ArrayRef<const Expr *> RHSExprs, |
| 12004 | ArrayRef<const Expr *> ReductionOps, ReductionOptionsTy Options) { |
| 12005 | assert(Options.SimpleReduction && "Only simple reduction is expected."); |
| 12006 | CGOpenMPRuntime::emitReduction(CGF, Loc, Privates, LHSExprs, RHSExprs, |
| 12007 | ReductionOps, Options); |
| 12008 | } |
| 12009 | |
| 12010 | llvm::Value *CGOpenMPSIMDRuntime::emitTaskReductionInit( |
| 12011 | CodeGenFunction &CGF, SourceLocation Loc, ArrayRef<const Expr *> LHSExprs, |
| 12012 | ArrayRef<const Expr *> RHSExprs, const OMPTaskDataTy &Data) { |
| 12013 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 12014 | } |
| 12015 | |
| 12016 | void CGOpenMPSIMDRuntime::emitTaskReductionFixups(CodeGenFunction &CGF, |
| 12017 | SourceLocation Loc, |
| 12018 | ReductionCodeGen &RCG, |
| 12019 | unsigned N) { |
| 12020 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 12021 | } |
| 12022 | |
| 12023 | Address CGOpenMPSIMDRuntime::getTaskReductionItem(CodeGenFunction &CGF, |
| 12024 | SourceLocation Loc, |
| 12025 | llvm::Value *ReductionsPtr, |
| 12026 | LValue SharedLVal) { |
| 12027 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 12028 | } |
| 12029 | |
| 12030 | void CGOpenMPSIMDRuntime::emitTaskwaitCall(CodeGenFunction &CGF, |
| 12031 | SourceLocation Loc) { |
| 12032 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 12033 | } |
| 12034 | |
| 12035 | void CGOpenMPSIMDRuntime::emitCancellationPointCall( |
| 12036 | CodeGenFunction &CGF, SourceLocation Loc, |
| 12037 | OpenMPDirectiveKind CancelRegion) { |
| 12038 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 12039 | } |
| 12040 | |
| 12041 | void CGOpenMPSIMDRuntime::emitCancelCall(CodeGenFunction &CGF, |
| 12042 | SourceLocation Loc, const Expr *IfCond, |
| 12043 | OpenMPDirectiveKind CancelRegion) { |
| 12044 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 12045 | } |
| 12046 | |
| 12047 | void CGOpenMPSIMDRuntime::emitTargetOutlinedFunction( |
| 12048 | const OMPExecutableDirective &D, StringRef ParentName, |
| 12049 | llvm::Function *&OutlinedFn, llvm::Constant *&OutlinedFnID, |
| 12050 | bool IsOffloadEntry, const RegionCodeGenTy &CodeGen) { |
| 12051 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 12052 | } |
| 12053 | |
| Alexey Bataev | ec7946e | 2019-09-23 14:06:51 +0000 | [diff] [blame] | 12054 | void CGOpenMPSIMDRuntime::emitTargetCall( |
| 12055 | CodeGenFunction &CGF, const OMPExecutableDirective &D, |
| 12056 | llvm::Function *OutlinedFn, llvm::Value *OutlinedFnID, const Expr *IfCond, |
| 12057 | const Expr *Device, |
| 12058 | llvm::function_ref<llvm::Value *(CodeGenFunction &CGF, |
| 12059 | const OMPLoopDirective &D)> |
| 12060 | SizeEmitter) { |
| Alexey Bataev | a8a9153a | 2017-12-29 18:07:07 +0000 | [diff] [blame] | 12061 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 12062 | } |
| 12063 | |
| 12064 | bool CGOpenMPSIMDRuntime::emitTargetFunctions(GlobalDecl GD) { |
| 12065 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 12066 | } |
| 12067 | |
| 12068 | bool CGOpenMPSIMDRuntime::emitTargetGlobalVariable(GlobalDecl GD) { |
| 12069 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 12070 | } |
| 12071 | |
| 12072 | bool CGOpenMPSIMDRuntime::emitTargetGlobal(GlobalDecl GD) { |
| 12073 | return false; |
| 12074 | } |
| 12075 | |
| Alexey Bataev | a8a9153a | 2017-12-29 18:07:07 +0000 | [diff] [blame] | 12076 | void CGOpenMPSIMDRuntime::emitTeamsCall(CodeGenFunction &CGF, |
| 12077 | const OMPExecutableDirective &D, |
| 12078 | SourceLocation Loc, |
| James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 12079 | llvm::Function *OutlinedFn, |
| Alexey Bataev | a8a9153a | 2017-12-29 18:07:07 +0000 | [diff] [blame] | 12080 | ArrayRef<llvm::Value *> CapturedVars) { |
| 12081 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 12082 | } |
| 12083 | |
| 12084 | void CGOpenMPSIMDRuntime::emitNumTeamsClause(CodeGenFunction &CGF, |
| 12085 | const Expr *NumTeams, |
| 12086 | const Expr *ThreadLimit, |
| 12087 | SourceLocation Loc) { |
| 12088 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 12089 | } |
| 12090 | |
| 12091 | void CGOpenMPSIMDRuntime::emitTargetDataCalls( |
| 12092 | CodeGenFunction &CGF, const OMPExecutableDirective &D, const Expr *IfCond, |
| 12093 | const Expr *Device, const RegionCodeGenTy &CodeGen, TargetDataInfo &Info) { |
| 12094 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 12095 | } |
| 12096 | |
| 12097 | void CGOpenMPSIMDRuntime::emitTargetDataStandAloneCall( |
| 12098 | CodeGenFunction &CGF, const OMPExecutableDirective &D, const Expr *IfCond, |
| 12099 | const Expr *Device) { |
| 12100 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 12101 | } |
| 12102 | |
| 12103 | void CGOpenMPSIMDRuntime::emitDoacrossInit(CodeGenFunction &CGF, |
| Alexey Bataev | f138fda | 2018-08-13 19:04:24 +0000 | [diff] [blame] | 12104 | const OMPLoopDirective &D, |
| 12105 | ArrayRef<Expr *> NumIterations) { |
| Alexey Bataev | a8a9153a | 2017-12-29 18:07:07 +0000 | [diff] [blame] | 12106 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 12107 | } |
| 12108 | |
| 12109 | void CGOpenMPSIMDRuntime::emitDoacrossOrdered(CodeGenFunction &CGF, |
| 12110 | const OMPDependClause *C) { |
| 12111 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 12112 | } |
| 12113 | |
| 12114 | const VarDecl * |
| 12115 | CGOpenMPSIMDRuntime::translateParameter(const FieldDecl *FD, |
| 12116 | const VarDecl *NativeParam) const { |
| 12117 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 12118 | } |
| 12119 | |
| 12120 | Address |
| 12121 | CGOpenMPSIMDRuntime::getParameterAddress(CodeGenFunction &CGF, |
| 12122 | const VarDecl *NativeParam, |
| 12123 | const VarDecl *TargetParam) const { |
| 12124 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 12125 | } |