Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1 | //===----- CGOpenMPRuntime.cpp - Interface to OpenMP Runtimes -------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This provides a class for OpenMP runtime code generation. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 14 | #include "CGCXXABI.h" |
| 15 | #include "CGCleanup.h" |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 16 | #include "CGOpenMPRuntime.h" |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 17 | #include "CGRecordLayout.h" |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 18 | #include "CodeGenFunction.h" |
John McCall | 5ad7407 | 2017-03-02 20:04:19 +0000 | [diff] [blame] | 19 | #include "clang/CodeGen/ConstantInitBuilder.h" |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 20 | #include "clang/AST/Decl.h" |
Chandler Carruth | 0d9593d | 2015-01-14 11:29:14 +0000 | [diff] [blame] | 21 | #include "clang/AST/StmtOpenMP.h" |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/ArrayRef.h" |
Alexey Bataev | 0f87dbe | 2017-08-14 17:56:13 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/BitmaskEnum.h" |
Teresa Johnson | ffc4e24 | 2016-11-11 05:35:12 +0000 | [diff] [blame] | 24 | #include "llvm/Bitcode/BitcodeReader.h" |
Alexey Bataev | d74d060 | 2014-10-13 06:02:40 +0000 | [diff] [blame] | 25 | #include "llvm/IR/CallSite.h" |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 26 | #include "llvm/IR/DerivedTypes.h" |
| 27 | #include "llvm/IR/GlobalValue.h" |
| 28 | #include "llvm/IR/Value.h" |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 29 | #include "llvm/Support/Format.h" |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 30 | #include "llvm/Support/raw_ostream.h" |
Alexey Bataev | 23b6942 | 2014-06-18 07:08:49 +0000 | [diff] [blame] | 31 | #include <cassert> |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 32 | |
| 33 | using namespace clang; |
| 34 | using namespace CodeGen; |
| 35 | |
Benjamin Kramer | c52193f | 2014-10-10 13:57:57 +0000 | [diff] [blame] | 36 | namespace { |
Alexey Bataev | 8cbe0a6 | 2015-02-26 10:27:34 +0000 | [diff] [blame] | 37 | /// \brief Base class for handling code generation inside OpenMP regions. |
Alexey Bataev | 1809571 | 2014-10-10 12:19:54 +0000 | [diff] [blame] | 38 | class CGOpenMPRegionInfo : public CodeGenFunction::CGCapturedStmtInfo { |
| 39 | public: |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 40 | /// \brief Kinds of OpenMP regions used in codegen. |
| 41 | enum CGOpenMPRegionKind { |
| 42 | /// \brief Region with outlined function for standalone 'parallel' |
| 43 | /// directive. |
| 44 | ParallelOutlinedRegion, |
| 45 | /// \brief Region with outlined function for standalone 'task' directive. |
| 46 | TaskOutlinedRegion, |
| 47 | /// \brief Region for constructs that do not require function outlining, |
| 48 | /// like 'for', 'sections', 'atomic' etc. directives. |
| 49 | InlinedRegion, |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 50 | /// \brief Region with outlined function for standalone 'target' directive. |
| 51 | TargetRegion, |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 52 | }; |
Alexey Bataev | 1809571 | 2014-10-10 12:19:54 +0000 | [diff] [blame] | 53 | |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 54 | CGOpenMPRegionInfo(const CapturedStmt &CS, |
| 55 | const CGOpenMPRegionKind RegionKind, |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 56 | const RegionCodeGenTy &CodeGen, OpenMPDirectiveKind Kind, |
| 57 | bool HasCancel) |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 58 | : CGCapturedStmtInfo(CS, CR_OpenMP), RegionKind(RegionKind), |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 59 | CodeGen(CodeGen), Kind(Kind), HasCancel(HasCancel) {} |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 60 | |
| 61 | CGOpenMPRegionInfo(const CGOpenMPRegionKind RegionKind, |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 62 | const RegionCodeGenTy &CodeGen, OpenMPDirectiveKind Kind, |
| 63 | bool HasCancel) |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 64 | : CGCapturedStmtInfo(CR_OpenMP), RegionKind(RegionKind), CodeGen(CodeGen), |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 65 | Kind(Kind), HasCancel(HasCancel) {} |
Alexey Bataev | 8cbe0a6 | 2015-02-26 10:27:34 +0000 | [diff] [blame] | 66 | |
| 67 | /// \brief Get a variable or parameter for storing global thread id |
Alexey Bataev | 1809571 | 2014-10-10 12:19:54 +0000 | [diff] [blame] | 68 | /// inside OpenMP construct. |
Alexey Bataev | 8cbe0a6 | 2015-02-26 10:27:34 +0000 | [diff] [blame] | 69 | virtual const VarDecl *getThreadIDVariable() const = 0; |
Alexey Bataev | 1809571 | 2014-10-10 12:19:54 +0000 | [diff] [blame] | 70 | |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 71 | /// \brief Emit the captured statement body. |
Hans Wennborg | 7eb5464 | 2015-09-10 17:07:54 +0000 | [diff] [blame] | 72 | void EmitBody(CodeGenFunction &CGF, const Stmt *S) override; |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 73 | |
Alexey Bataev | 8cbe0a6 | 2015-02-26 10:27:34 +0000 | [diff] [blame] | 74 | /// \brief Get an LValue for the current ThreadID variable. |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 75 | /// \return LValue for thread id variable. This LValue always has type int32*. |
| 76 | virtual LValue getThreadIDVariableLValue(CodeGenFunction &CGF); |
Alexey Bataev | 1809571 | 2014-10-10 12:19:54 +0000 | [diff] [blame] | 77 | |
Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 78 | virtual void emitUntiedSwitch(CodeGenFunction & /*CGF*/) {} |
| 79 | |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 80 | CGOpenMPRegionKind getRegionKind() const { return RegionKind; } |
Alexey Bataev | 8cbe0a6 | 2015-02-26 10:27:34 +0000 | [diff] [blame] | 81 | |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 82 | OpenMPDirectiveKind getDirectiveKind() const { return Kind; } |
| 83 | |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 84 | bool hasCancel() const { return HasCancel; } |
| 85 | |
Alexey Bataev | 1809571 | 2014-10-10 12:19:54 +0000 | [diff] [blame] | 86 | static bool classof(const CGCapturedStmtInfo *Info) { |
| 87 | return Info->getKind() == CR_OpenMP; |
| 88 | } |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 89 | |
Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 90 | ~CGOpenMPRegionInfo() override = default; |
| 91 | |
Alexey Bataev | 8cbe0a6 | 2015-02-26 10:27:34 +0000 | [diff] [blame] | 92 | protected: |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 93 | CGOpenMPRegionKind RegionKind; |
Hans Wennborg | 45c7439 | 2016-01-12 20:54:36 +0000 | [diff] [blame] | 94 | RegionCodeGenTy CodeGen; |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 95 | OpenMPDirectiveKind Kind; |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 96 | bool HasCancel; |
Alexey Bataev | 8cbe0a6 | 2015-02-26 10:27:34 +0000 | [diff] [blame] | 97 | }; |
Alexey Bataev | 1809571 | 2014-10-10 12:19:54 +0000 | [diff] [blame] | 98 | |
Alexey Bataev | 8cbe0a6 | 2015-02-26 10:27:34 +0000 | [diff] [blame] | 99 | /// \brief API for captured statement code generation in OpenMP constructs. |
Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 100 | class CGOpenMPOutlinedRegionInfo final : public CGOpenMPRegionInfo { |
Alexey Bataev | 8cbe0a6 | 2015-02-26 10:27:34 +0000 | [diff] [blame] | 101 | public: |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 102 | CGOpenMPOutlinedRegionInfo(const CapturedStmt &CS, const VarDecl *ThreadIDVar, |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 103 | const RegionCodeGenTy &CodeGen, |
Arpith Chacko Jacob | bb36fe8 | 2017-01-10 15:42:51 +0000 | [diff] [blame] | 104 | OpenMPDirectiveKind Kind, bool HasCancel, |
| 105 | StringRef HelperName) |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 106 | : CGOpenMPRegionInfo(CS, ParallelOutlinedRegion, CodeGen, Kind, |
| 107 | HasCancel), |
Arpith Chacko Jacob | bb36fe8 | 2017-01-10 15:42:51 +0000 | [diff] [blame] | 108 | ThreadIDVar(ThreadIDVar), HelperName(HelperName) { |
Alexey Bataev | 8cbe0a6 | 2015-02-26 10:27:34 +0000 | [diff] [blame] | 109 | assert(ThreadIDVar != nullptr && "No ThreadID in OpenMP region."); |
| 110 | } |
Eugene Zelenko | 0a4f3f4 | 2016-02-10 19:11:58 +0000 | [diff] [blame] | 111 | |
Alexey Bataev | 8cbe0a6 | 2015-02-26 10:27:34 +0000 | [diff] [blame] | 112 | /// \brief Get a variable or parameter for storing global thread id |
| 113 | /// inside OpenMP construct. |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 114 | const VarDecl *getThreadIDVariable() const override { return ThreadIDVar; } |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 115 | |
Alexey Bataev | 1809571 | 2014-10-10 12:19:54 +0000 | [diff] [blame] | 116 | /// \brief Get the name of the capture helper. |
Arpith Chacko Jacob | bb36fe8 | 2017-01-10 15:42:51 +0000 | [diff] [blame] | 117 | StringRef getHelperName() const override { return HelperName; } |
Alexey Bataev | 1809571 | 2014-10-10 12:19:54 +0000 | [diff] [blame] | 118 | |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 119 | static bool classof(const CGCapturedStmtInfo *Info) { |
| 120 | return CGOpenMPRegionInfo::classof(Info) && |
| 121 | cast<CGOpenMPRegionInfo>(Info)->getRegionKind() == |
| 122 | ParallelOutlinedRegion; |
| 123 | } |
| 124 | |
Alexey Bataev | 1809571 | 2014-10-10 12:19:54 +0000 | [diff] [blame] | 125 | private: |
| 126 | /// \brief A variable or parameter storing global thread id for OpenMP |
| 127 | /// constructs. |
| 128 | const VarDecl *ThreadIDVar; |
Arpith Chacko Jacob | bb36fe8 | 2017-01-10 15:42:51 +0000 | [diff] [blame] | 129 | StringRef HelperName; |
Alexey Bataev | 8cbe0a6 | 2015-02-26 10:27:34 +0000 | [diff] [blame] | 130 | }; |
| 131 | |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 132 | /// \brief API for captured statement code generation in OpenMP constructs. |
Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 133 | class CGOpenMPTaskOutlinedRegionInfo final : public CGOpenMPRegionInfo { |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 134 | public: |
Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 135 | class UntiedTaskActionTy final : public PrePostActionTy { |
| 136 | bool Untied; |
| 137 | const VarDecl *PartIDVar; |
| 138 | const RegionCodeGenTy UntiedCodeGen; |
| 139 | llvm::SwitchInst *UntiedSwitch = nullptr; |
| 140 | |
| 141 | public: |
| 142 | UntiedTaskActionTy(bool Tied, const VarDecl *PartIDVar, |
| 143 | const RegionCodeGenTy &UntiedCodeGen) |
| 144 | : Untied(!Tied), PartIDVar(PartIDVar), UntiedCodeGen(UntiedCodeGen) {} |
| 145 | void Enter(CodeGenFunction &CGF) override { |
| 146 | if (Untied) { |
| 147 | // Emit task switching point. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 148 | LValue PartIdLVal = CGF.EmitLoadOfPointerLValue( |
Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 149 | CGF.GetAddrOfLocalVar(PartIDVar), |
| 150 | PartIDVar->getType()->castAs<PointerType>()); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 151 | llvm::Value *Res = |
| 152 | CGF.EmitLoadOfScalar(PartIdLVal, PartIDVar->getLocation()); |
| 153 | llvm::BasicBlock *DoneBB = CGF.createBasicBlock(".untied.done."); |
Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 154 | UntiedSwitch = CGF.Builder.CreateSwitch(Res, DoneBB); |
| 155 | CGF.EmitBlock(DoneBB); |
| 156 | CGF.EmitBranchThroughCleanup(CGF.ReturnBlock); |
| 157 | CGF.EmitBlock(CGF.createBasicBlock(".untied.jmp.")); |
| 158 | UntiedSwitch->addCase(CGF.Builder.getInt32(0), |
| 159 | CGF.Builder.GetInsertBlock()); |
| 160 | emitUntiedSwitch(CGF); |
| 161 | } |
| 162 | } |
| 163 | void emitUntiedSwitch(CodeGenFunction &CGF) const { |
| 164 | if (Untied) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 165 | LValue PartIdLVal = CGF.EmitLoadOfPointerLValue( |
Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 166 | CGF.GetAddrOfLocalVar(PartIDVar), |
| 167 | PartIDVar->getType()->castAs<PointerType>()); |
| 168 | CGF.EmitStoreOfScalar(CGF.Builder.getInt32(UntiedSwitch->getNumCases()), |
| 169 | PartIdLVal); |
| 170 | UntiedCodeGen(CGF); |
| 171 | CodeGenFunction::JumpDest CurPoint = |
| 172 | CGF.getJumpDestInCurrentScope(".untied.next."); |
| 173 | CGF.EmitBranchThroughCleanup(CGF.ReturnBlock); |
| 174 | CGF.EmitBlock(CGF.createBasicBlock(".untied.jmp.")); |
| 175 | UntiedSwitch->addCase(CGF.Builder.getInt32(UntiedSwitch->getNumCases()), |
| 176 | CGF.Builder.GetInsertBlock()); |
| 177 | CGF.EmitBranchThroughCleanup(CurPoint); |
| 178 | CGF.EmitBlock(CurPoint.getBlock()); |
| 179 | } |
| 180 | } |
| 181 | unsigned getNumberOfParts() const { return UntiedSwitch->getNumCases(); } |
| 182 | }; |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 183 | CGOpenMPTaskOutlinedRegionInfo(const CapturedStmt &CS, |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 184 | const VarDecl *ThreadIDVar, |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 185 | const RegionCodeGenTy &CodeGen, |
Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 186 | OpenMPDirectiveKind Kind, bool HasCancel, |
| 187 | const UntiedTaskActionTy &Action) |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 188 | : CGOpenMPRegionInfo(CS, TaskOutlinedRegion, CodeGen, Kind, HasCancel), |
Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 189 | ThreadIDVar(ThreadIDVar), Action(Action) { |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 190 | assert(ThreadIDVar != nullptr && "No ThreadID in OpenMP region."); |
| 191 | } |
Eugene Zelenko | 0a4f3f4 | 2016-02-10 19:11:58 +0000 | [diff] [blame] | 192 | |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 193 | /// \brief Get a variable or parameter for storing global thread id |
| 194 | /// inside OpenMP construct. |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 195 | const VarDecl *getThreadIDVariable() const override { return ThreadIDVar; } |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 196 | |
| 197 | /// \brief Get an LValue for the current ThreadID variable. |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 198 | LValue getThreadIDVariableLValue(CodeGenFunction &CGF) override; |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 199 | |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 200 | /// \brief Get the name of the capture helper. |
| 201 | StringRef getHelperName() const override { return ".omp_outlined."; } |
| 202 | |
Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 203 | void emitUntiedSwitch(CodeGenFunction &CGF) override { |
| 204 | Action.emitUntiedSwitch(CGF); |
| 205 | } |
| 206 | |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 207 | static bool classof(const CGCapturedStmtInfo *Info) { |
| 208 | return CGOpenMPRegionInfo::classof(Info) && |
| 209 | cast<CGOpenMPRegionInfo>(Info)->getRegionKind() == |
| 210 | TaskOutlinedRegion; |
| 211 | } |
| 212 | |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 213 | private: |
| 214 | /// \brief A variable or parameter storing global thread id for OpenMP |
| 215 | /// constructs. |
| 216 | const VarDecl *ThreadIDVar; |
Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 217 | /// Action for emitting code for untied tasks. |
| 218 | const UntiedTaskActionTy &Action; |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 219 | }; |
| 220 | |
Alexey Bataev | 8cbe0a6 | 2015-02-26 10:27:34 +0000 | [diff] [blame] | 221 | /// \brief API for inlined captured statement code generation in OpenMP |
| 222 | /// constructs. |
| 223 | class CGOpenMPInlinedRegionInfo : public CGOpenMPRegionInfo { |
| 224 | public: |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 225 | CGOpenMPInlinedRegionInfo(CodeGenFunction::CGCapturedStmtInfo *OldCSI, |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 226 | const RegionCodeGenTy &CodeGen, |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 227 | OpenMPDirectiveKind Kind, bool HasCancel) |
| 228 | : CGOpenMPRegionInfo(InlinedRegion, CodeGen, Kind, HasCancel), |
| 229 | OldCSI(OldCSI), |
Alexey Bataev | 8cbe0a6 | 2015-02-26 10:27:34 +0000 | [diff] [blame] | 230 | OuterRegionInfo(dyn_cast_or_null<CGOpenMPRegionInfo>(OldCSI)) {} |
Eugene Zelenko | 0a4f3f4 | 2016-02-10 19:11:58 +0000 | [diff] [blame] | 231 | |
Alexey Bataev | 8cbe0a6 | 2015-02-26 10:27:34 +0000 | [diff] [blame] | 232 | // \brief Retrieve the value of the context parameter. |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 233 | llvm::Value *getContextValue() const override { |
Alexey Bataev | 8cbe0a6 | 2015-02-26 10:27:34 +0000 | [diff] [blame] | 234 | if (OuterRegionInfo) |
| 235 | return OuterRegionInfo->getContextValue(); |
| 236 | llvm_unreachable("No context value for inlined OpenMP region"); |
| 237 | } |
Eugene Zelenko | 0a4f3f4 | 2016-02-10 19:11:58 +0000 | [diff] [blame] | 238 | |
Hans Wennborg | 7eb5464 | 2015-09-10 17:07:54 +0000 | [diff] [blame] | 239 | void setContextValue(llvm::Value *V) override { |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 240 | if (OuterRegionInfo) { |
| 241 | OuterRegionInfo->setContextValue(V); |
| 242 | return; |
| 243 | } |
| 244 | llvm_unreachable("No context value for inlined OpenMP region"); |
| 245 | } |
Eugene Zelenko | 0a4f3f4 | 2016-02-10 19:11:58 +0000 | [diff] [blame] | 246 | |
Alexey Bataev | 8cbe0a6 | 2015-02-26 10:27:34 +0000 | [diff] [blame] | 247 | /// \brief Lookup the captured field decl for a variable. |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 248 | const FieldDecl *lookup(const VarDecl *VD) const override { |
Alexey Bataev | 8cbe0a6 | 2015-02-26 10:27:34 +0000 | [diff] [blame] | 249 | if (OuterRegionInfo) |
| 250 | return OuterRegionInfo->lookup(VD); |
Alexey Bataev | 69c62a9 | 2015-04-15 04:52:20 +0000 | [diff] [blame] | 251 | // If there is no outer outlined region,no need to lookup in a list of |
| 252 | // captured variables, we can use the original one. |
| 253 | return nullptr; |
Alexey Bataev | 8cbe0a6 | 2015-02-26 10:27:34 +0000 | [diff] [blame] | 254 | } |
Eugene Zelenko | 0a4f3f4 | 2016-02-10 19:11:58 +0000 | [diff] [blame] | 255 | |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 256 | FieldDecl *getThisFieldDecl() const override { |
Alexey Bataev | 8cbe0a6 | 2015-02-26 10:27:34 +0000 | [diff] [blame] | 257 | if (OuterRegionInfo) |
| 258 | return OuterRegionInfo->getThisFieldDecl(); |
| 259 | return nullptr; |
| 260 | } |
Eugene Zelenko | 0a4f3f4 | 2016-02-10 19:11:58 +0000 | [diff] [blame] | 261 | |
Alexey Bataev | 8cbe0a6 | 2015-02-26 10:27:34 +0000 | [diff] [blame] | 262 | /// \brief Get a variable or parameter for storing global thread id |
| 263 | /// inside OpenMP construct. |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 264 | const VarDecl *getThreadIDVariable() const override { |
Alexey Bataev | 8cbe0a6 | 2015-02-26 10:27:34 +0000 | [diff] [blame] | 265 | if (OuterRegionInfo) |
| 266 | return OuterRegionInfo->getThreadIDVariable(); |
| 267 | return nullptr; |
| 268 | } |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 269 | |
Alexey Bataev | 311a928 | 2017-10-12 13:51:32 +0000 | [diff] [blame] | 270 | /// \brief Get an LValue for the current ThreadID variable. |
| 271 | LValue getThreadIDVariableLValue(CodeGenFunction &CGF) override { |
| 272 | if (OuterRegionInfo) |
| 273 | return OuterRegionInfo->getThreadIDVariableLValue(CGF); |
| 274 | llvm_unreachable("No LValue for inlined OpenMP construct"); |
| 275 | } |
| 276 | |
Alexey Bataev | 8cbe0a6 | 2015-02-26 10:27:34 +0000 | [diff] [blame] | 277 | /// \brief Get the name of the capture helper. |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 278 | StringRef getHelperName() const override { |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 279 | if (auto *OuterRegionInfo = getOldCSI()) |
| 280 | return OuterRegionInfo->getHelperName(); |
Alexey Bataev | 8cbe0a6 | 2015-02-26 10:27:34 +0000 | [diff] [blame] | 281 | llvm_unreachable("No helper name for inlined OpenMP construct"); |
| 282 | } |
| 283 | |
Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 284 | void emitUntiedSwitch(CodeGenFunction &CGF) override { |
| 285 | if (OuterRegionInfo) |
| 286 | OuterRegionInfo->emitUntiedSwitch(CGF); |
| 287 | } |
| 288 | |
Alexey Bataev | 8cbe0a6 | 2015-02-26 10:27:34 +0000 | [diff] [blame] | 289 | CodeGenFunction::CGCapturedStmtInfo *getOldCSI() const { return OldCSI; } |
| 290 | |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 291 | static bool classof(const CGCapturedStmtInfo *Info) { |
| 292 | return CGOpenMPRegionInfo::classof(Info) && |
| 293 | cast<CGOpenMPRegionInfo>(Info)->getRegionKind() == InlinedRegion; |
| 294 | } |
| 295 | |
Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 296 | ~CGOpenMPInlinedRegionInfo() override = default; |
| 297 | |
Alexey Bataev | 8cbe0a6 | 2015-02-26 10:27:34 +0000 | [diff] [blame] | 298 | private: |
| 299 | /// \brief CodeGen info about outer OpenMP region. |
| 300 | CodeGenFunction::CGCapturedStmtInfo *OldCSI; |
| 301 | CGOpenMPRegionInfo *OuterRegionInfo; |
Alexey Bataev | 1809571 | 2014-10-10 12:19:54 +0000 | [diff] [blame] | 302 | }; |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 303 | |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 304 | /// \brief API for captured statement code generation in OpenMP target |
| 305 | /// constructs. For this captures, implicit parameters are used instead of the |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 306 | /// captured fields. The name of the target region has to be unique in a given |
| 307 | /// application so it is provided by the client, because only the client has |
| 308 | /// the information to generate that. |
Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 309 | class CGOpenMPTargetRegionInfo final : public CGOpenMPRegionInfo { |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 310 | public: |
| 311 | CGOpenMPTargetRegionInfo(const CapturedStmt &CS, |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 312 | const RegionCodeGenTy &CodeGen, StringRef HelperName) |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 313 | : CGOpenMPRegionInfo(CS, TargetRegion, CodeGen, OMPD_target, |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 314 | /*HasCancel=*/false), |
| 315 | HelperName(HelperName) {} |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 316 | |
| 317 | /// \brief This is unused for target regions because each starts executing |
| 318 | /// with a single thread. |
| 319 | const VarDecl *getThreadIDVariable() const override { return nullptr; } |
| 320 | |
| 321 | /// \brief Get the name of the capture helper. |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 322 | StringRef getHelperName() const override { return HelperName; } |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 323 | |
| 324 | static bool classof(const CGCapturedStmtInfo *Info) { |
| 325 | return CGOpenMPRegionInfo::classof(Info) && |
| 326 | cast<CGOpenMPRegionInfo>(Info)->getRegionKind() == TargetRegion; |
| 327 | } |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 328 | |
| 329 | private: |
| 330 | StringRef HelperName; |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 331 | }; |
| 332 | |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 333 | static void EmptyCodeGen(CodeGenFunction &, PrePostActionTy &) { |
Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 334 | llvm_unreachable("No codegen for expressions"); |
| 335 | } |
| 336 | /// \brief API for generation of expressions captured in a innermost OpenMP |
| 337 | /// region. |
Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 338 | class CGOpenMPInnerExprInfo final : public CGOpenMPInlinedRegionInfo { |
Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 339 | public: |
| 340 | CGOpenMPInnerExprInfo(CodeGenFunction &CGF, const CapturedStmt &CS) |
| 341 | : CGOpenMPInlinedRegionInfo(CGF.CapturedStmtInfo, EmptyCodeGen, |
| 342 | OMPD_unknown, |
| 343 | /*HasCancel=*/false), |
| 344 | PrivScope(CGF) { |
| 345 | // Make sure the globals captured in the provided statement are local by |
| 346 | // using the privatization logic. We assume the same variable is not |
| 347 | // captured more than once. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 348 | for (const auto &C : CS.captures()) { |
Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 349 | if (!C.capturesVariable() && !C.capturesVariableByCopy()) |
| 350 | continue; |
| 351 | |
| 352 | const VarDecl *VD = C.getCapturedVar(); |
| 353 | if (VD->isLocalVarDeclOrParm()) |
| 354 | continue; |
| 355 | |
| 356 | DeclRefExpr DRE(const_cast<VarDecl *>(VD), |
| 357 | /*RefersToEnclosingVariableOrCapture=*/false, |
| 358 | VD->getType().getNonReferenceType(), VK_LValue, |
Alexey Bataev | a9b9cc0 | 2018-01-23 18:12:38 +0000 | [diff] [blame] | 359 | C.getLocation()); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 360 | PrivScope.addPrivate( |
| 361 | VD, [&CGF, &DRE]() { return CGF.EmitLValue(&DRE).getAddress(); }); |
Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 362 | } |
| 363 | (void)PrivScope.Privatize(); |
| 364 | } |
| 365 | |
| 366 | /// \brief Lookup the captured field decl for a variable. |
| 367 | const FieldDecl *lookup(const VarDecl *VD) const override { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 368 | if (const FieldDecl *FD = CGOpenMPInlinedRegionInfo::lookup(VD)) |
Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 369 | return FD; |
| 370 | return nullptr; |
| 371 | } |
| 372 | |
| 373 | /// \brief Emit the captured statement body. |
| 374 | void EmitBody(CodeGenFunction &CGF, const Stmt *S) override { |
| 375 | llvm_unreachable("No body for expressions"); |
| 376 | } |
| 377 | |
| 378 | /// \brief Get a variable or parameter for storing global thread id |
| 379 | /// inside OpenMP construct. |
| 380 | const VarDecl *getThreadIDVariable() const override { |
| 381 | llvm_unreachable("No thread id for expressions"); |
| 382 | } |
| 383 | |
| 384 | /// \brief Get the name of the capture helper. |
| 385 | StringRef getHelperName() const override { |
| 386 | llvm_unreachable("No helper name for expressions"); |
| 387 | } |
| 388 | |
| 389 | static bool classof(const CGCapturedStmtInfo *Info) { return false; } |
| 390 | |
| 391 | private: |
| 392 | /// Private scope to capture global variables. |
| 393 | CodeGenFunction::OMPPrivateScope PrivScope; |
| 394 | }; |
| 395 | |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 396 | /// \brief RAII for emitting code of OpenMP constructs. |
| 397 | class InlinedOpenMPRegionRAII { |
| 398 | CodeGenFunction &CGF; |
Alexey Bataev | 4ba78a4 | 2016-04-27 07:56:03 +0000 | [diff] [blame] | 399 | llvm::DenseMap<const VarDecl *, FieldDecl *> LambdaCaptureFields; |
| 400 | FieldDecl *LambdaThisCaptureField = nullptr; |
Alexey Bataev | a8a9153a | 2017-12-29 18:07:07 +0000 | [diff] [blame] | 401 | const CodeGen::CGBlockInfo *BlockInfo = nullptr; |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 402 | |
| 403 | public: |
| 404 | /// \brief Constructs region for combined constructs. |
| 405 | /// \param CodeGen Code generation sequence for combined directives. Includes |
| 406 | /// a list of functions used for code generation of implicitly inlined |
| 407 | /// regions. |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 408 | InlinedOpenMPRegionRAII(CodeGenFunction &CGF, const RegionCodeGenTy &CodeGen, |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 409 | OpenMPDirectiveKind Kind, bool HasCancel) |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 410 | : CGF(CGF) { |
| 411 | // Start emission for the construct. |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 412 | CGF.CapturedStmtInfo = new CGOpenMPInlinedRegionInfo( |
| 413 | CGF.CapturedStmtInfo, CodeGen, Kind, HasCancel); |
Alexey Bataev | 4ba78a4 | 2016-04-27 07:56:03 +0000 | [diff] [blame] | 414 | std::swap(CGF.LambdaCaptureFields, LambdaCaptureFields); |
| 415 | LambdaThisCaptureField = CGF.LambdaThisCaptureField; |
| 416 | CGF.LambdaThisCaptureField = nullptr; |
Alexey Bataev | a8a9153a | 2017-12-29 18:07:07 +0000 | [diff] [blame] | 417 | BlockInfo = CGF.BlockInfo; |
| 418 | CGF.BlockInfo = nullptr; |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 419 | } |
Eugene Zelenko | 0a4f3f4 | 2016-02-10 19:11:58 +0000 | [diff] [blame] | 420 | |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 421 | ~InlinedOpenMPRegionRAII() { |
| 422 | // Restore original CapturedStmtInfo only if we're done with code emission. |
| 423 | auto *OldCSI = |
| 424 | cast<CGOpenMPInlinedRegionInfo>(CGF.CapturedStmtInfo)->getOldCSI(); |
| 425 | delete CGF.CapturedStmtInfo; |
| 426 | CGF.CapturedStmtInfo = OldCSI; |
Alexey Bataev | 4ba78a4 | 2016-04-27 07:56:03 +0000 | [diff] [blame] | 427 | std::swap(CGF.LambdaCaptureFields, LambdaCaptureFields); |
| 428 | CGF.LambdaThisCaptureField = LambdaThisCaptureField; |
Alexey Bataev | a8a9153a | 2017-12-29 18:07:07 +0000 | [diff] [blame] | 429 | CGF.BlockInfo = BlockInfo; |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 430 | } |
| 431 | }; |
| 432 | |
Alexey Bataev | 50b3c95 | 2016-02-19 10:38:26 +0000 | [diff] [blame] | 433 | /// \brief Values for bit flags used in the ident_t to describe the fields. |
| 434 | /// All enumeric elements are named and described in accordance with the code |
| 435 | /// from http://llvm.org/svn/llvm-project/openmp/trunk/runtime/src/kmp.h |
Alexey Bataev | 0f87dbe | 2017-08-14 17:56:13 +0000 | [diff] [blame] | 436 | enum OpenMPLocationFlags : unsigned { |
Alexey Bataev | 50b3c95 | 2016-02-19 10:38:26 +0000 | [diff] [blame] | 437 | /// \brief Use trampoline for internal microtask. |
| 438 | OMP_IDENT_IMD = 0x01, |
| 439 | /// \brief Use c-style ident structure. |
| 440 | OMP_IDENT_KMPC = 0x02, |
| 441 | /// \brief Atomic reduction option for kmpc_reduce. |
| 442 | OMP_ATOMIC_REDUCE = 0x10, |
| 443 | /// \brief Explicit 'barrier' directive. |
| 444 | OMP_IDENT_BARRIER_EXPL = 0x20, |
| 445 | /// \brief Implicit barrier in code. |
| 446 | OMP_IDENT_BARRIER_IMPL = 0x40, |
| 447 | /// \brief Implicit barrier in 'for' directive. |
| 448 | OMP_IDENT_BARRIER_IMPL_FOR = 0x40, |
| 449 | /// \brief Implicit barrier in 'sections' directive. |
| 450 | OMP_IDENT_BARRIER_IMPL_SECTIONS = 0xC0, |
| 451 | /// \brief Implicit barrier in 'single' directive. |
Alexey Bataev | 0f87dbe | 2017-08-14 17:56:13 +0000 | [diff] [blame] | 452 | OMP_IDENT_BARRIER_IMPL_SINGLE = 0x140, |
| 453 | /// Call of __kmp_for_static_init for static loop. |
| 454 | OMP_IDENT_WORK_LOOP = 0x200, |
| 455 | /// Call of __kmp_for_static_init for sections. |
| 456 | OMP_IDENT_WORK_SECTIONS = 0x400, |
| 457 | /// Call of __kmp_for_static_init for distribute. |
| 458 | OMP_IDENT_WORK_DISTRIBUTE = 0x800, |
| 459 | LLVM_MARK_AS_BITMASK_ENUM(/*LargestValue=*/OMP_IDENT_WORK_DISTRIBUTE) |
Alexey Bataev | 50b3c95 | 2016-02-19 10:38:26 +0000 | [diff] [blame] | 460 | }; |
| 461 | |
| 462 | /// \brief Describes ident structure that describes a source location. |
| 463 | /// All descriptions are taken from |
| 464 | /// http://llvm.org/svn/llvm-project/openmp/trunk/runtime/src/kmp.h |
| 465 | /// Original structure: |
| 466 | /// typedef struct ident { |
| 467 | /// kmp_int32 reserved_1; /**< might be used in Fortran; |
| 468 | /// see above */ |
| 469 | /// kmp_int32 flags; /**< also f.flags; KMP_IDENT_xxx flags; |
| 470 | /// KMP_IDENT_KMPC identifies this union |
| 471 | /// member */ |
| 472 | /// kmp_int32 reserved_2; /**< not really used in Fortran any more; |
| 473 | /// see above */ |
| 474 | ///#if USE_ITT_BUILD |
| 475 | /// /* but currently used for storing |
| 476 | /// region-specific ITT */ |
| 477 | /// /* contextual information. */ |
| 478 | ///#endif /* USE_ITT_BUILD */ |
| 479 | /// kmp_int32 reserved_3; /**< source[4] in Fortran, do not use for |
| 480 | /// C++ */ |
| 481 | /// char const *psource; /**< String describing the source location. |
| 482 | /// The string is composed of semi-colon separated |
| 483 | // fields which describe the source file, |
| 484 | /// the function and a pair of line numbers that |
| 485 | /// delimit the construct. |
| 486 | /// */ |
| 487 | /// } ident_t; |
| 488 | enum IdentFieldIndex { |
| 489 | /// \brief might be used in Fortran |
| 490 | IdentField_Reserved_1, |
| 491 | /// \brief OMP_IDENT_xxx flags; OMP_IDENT_KMPC identifies this union member. |
| 492 | IdentField_Flags, |
| 493 | /// \brief Not really used in Fortran any more |
| 494 | IdentField_Reserved_2, |
| 495 | /// \brief Source[4] in Fortran, do not use for C++ |
| 496 | IdentField_Reserved_3, |
| 497 | /// \brief String describing the source location. The string is composed of |
| 498 | /// semi-colon separated fields which describe the source file, the function |
| 499 | /// and a pair of line numbers that delimit the construct. |
| 500 | IdentField_PSource |
| 501 | }; |
| 502 | |
| 503 | /// \brief Schedule types for 'omp for' loops (these enumerators are taken from |
| 504 | /// the enum sched_type in kmp.h). |
| 505 | enum OpenMPSchedType { |
| 506 | /// \brief Lower bound for default (unordered) versions. |
| 507 | OMP_sch_lower = 32, |
| 508 | OMP_sch_static_chunked = 33, |
| 509 | OMP_sch_static = 34, |
| 510 | OMP_sch_dynamic_chunked = 35, |
| 511 | OMP_sch_guided_chunked = 36, |
| 512 | OMP_sch_runtime = 37, |
| 513 | OMP_sch_auto = 38, |
Alexey Bataev | 6cff624 | 2016-05-30 13:05:14 +0000 | [diff] [blame] | 514 | /// static with chunk adjustment (e.g., simd) |
Samuel Antao | 4c8035b | 2016-12-12 18:00:20 +0000 | [diff] [blame] | 515 | OMP_sch_static_balanced_chunked = 45, |
Alexey Bataev | 50b3c95 | 2016-02-19 10:38:26 +0000 | [diff] [blame] | 516 | /// \brief Lower bound for 'ordered' versions. |
| 517 | OMP_ord_lower = 64, |
| 518 | OMP_ord_static_chunked = 65, |
| 519 | OMP_ord_static = 66, |
| 520 | OMP_ord_dynamic_chunked = 67, |
| 521 | OMP_ord_guided_chunked = 68, |
| 522 | OMP_ord_runtime = 69, |
| 523 | OMP_ord_auto = 70, |
| 524 | OMP_sch_default = OMP_sch_static, |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 525 | /// \brief dist_schedule types |
| 526 | OMP_dist_sch_static_chunked = 91, |
| 527 | OMP_dist_sch_static = 92, |
Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 528 | /// Support for OpenMP 4.5 monotonic and nonmonotonic schedule modifiers. |
| 529 | /// Set if the monotonic schedule modifier was present. |
| 530 | OMP_sch_modifier_monotonic = (1 << 29), |
| 531 | /// Set if the nonmonotonic schedule modifier was present. |
| 532 | OMP_sch_modifier_nonmonotonic = (1 << 30), |
Alexey Bataev | 50b3c95 | 2016-02-19 10:38:26 +0000 | [diff] [blame] | 533 | }; |
| 534 | |
| 535 | enum OpenMPRTLFunction { |
| 536 | /// \brief Call to void __kmpc_fork_call(ident_t *loc, kmp_int32 argc, |
| 537 | /// kmpc_micro microtask, ...); |
| 538 | OMPRTL__kmpc_fork_call, |
| 539 | /// \brief Call to void *__kmpc_threadprivate_cached(ident_t *loc, |
| 540 | /// kmp_int32 global_tid, void *data, size_t size, void ***cache); |
| 541 | OMPRTL__kmpc_threadprivate_cached, |
| 542 | /// \brief Call to void __kmpc_threadprivate_register( ident_t *, |
| 543 | /// void *data, kmpc_ctor ctor, kmpc_cctor cctor, kmpc_dtor dtor); |
| 544 | OMPRTL__kmpc_threadprivate_register, |
| 545 | // Call to __kmpc_int32 kmpc_global_thread_num(ident_t *loc); |
| 546 | OMPRTL__kmpc_global_thread_num, |
| 547 | // Call to void __kmpc_critical(ident_t *loc, kmp_int32 global_tid, |
| 548 | // kmp_critical_name *crit); |
| 549 | OMPRTL__kmpc_critical, |
| 550 | // Call to void __kmpc_critical_with_hint(ident_t *loc, kmp_int32 |
| 551 | // global_tid, kmp_critical_name *crit, uintptr_t hint); |
| 552 | OMPRTL__kmpc_critical_with_hint, |
| 553 | // Call to void __kmpc_end_critical(ident_t *loc, kmp_int32 global_tid, |
| 554 | // kmp_critical_name *crit); |
| 555 | OMPRTL__kmpc_end_critical, |
| 556 | // Call to kmp_int32 __kmpc_cancel_barrier(ident_t *loc, kmp_int32 |
| 557 | // global_tid); |
| 558 | OMPRTL__kmpc_cancel_barrier, |
| 559 | // Call to void __kmpc_barrier(ident_t *loc, kmp_int32 global_tid); |
| 560 | OMPRTL__kmpc_barrier, |
| 561 | // Call to void __kmpc_for_static_fini(ident_t *loc, kmp_int32 global_tid); |
| 562 | OMPRTL__kmpc_for_static_fini, |
| 563 | // Call to void __kmpc_serialized_parallel(ident_t *loc, kmp_int32 |
| 564 | // global_tid); |
| 565 | OMPRTL__kmpc_serialized_parallel, |
| 566 | // Call to void __kmpc_end_serialized_parallel(ident_t *loc, kmp_int32 |
| 567 | // global_tid); |
| 568 | OMPRTL__kmpc_end_serialized_parallel, |
| 569 | // Call to void __kmpc_push_num_threads(ident_t *loc, kmp_int32 global_tid, |
| 570 | // kmp_int32 num_threads); |
| 571 | OMPRTL__kmpc_push_num_threads, |
| 572 | // Call to void __kmpc_flush(ident_t *loc); |
| 573 | OMPRTL__kmpc_flush, |
| 574 | // Call to kmp_int32 __kmpc_master(ident_t *, kmp_int32 global_tid); |
| 575 | OMPRTL__kmpc_master, |
| 576 | // Call to void __kmpc_end_master(ident_t *, kmp_int32 global_tid); |
| 577 | OMPRTL__kmpc_end_master, |
| 578 | // Call to kmp_int32 __kmpc_omp_taskyield(ident_t *, kmp_int32 global_tid, |
| 579 | // int end_part); |
| 580 | OMPRTL__kmpc_omp_taskyield, |
| 581 | // Call to kmp_int32 __kmpc_single(ident_t *, kmp_int32 global_tid); |
| 582 | OMPRTL__kmpc_single, |
| 583 | // Call to void __kmpc_end_single(ident_t *, kmp_int32 global_tid); |
| 584 | OMPRTL__kmpc_end_single, |
| 585 | // Call to kmp_task_t * __kmpc_omp_task_alloc(ident_t *, kmp_int32 gtid, |
| 586 | // kmp_int32 flags, size_t sizeof_kmp_task_t, size_t sizeof_shareds, |
| 587 | // kmp_routine_entry_t *task_entry); |
| 588 | OMPRTL__kmpc_omp_task_alloc, |
| 589 | // Call to kmp_int32 __kmpc_omp_task(ident_t *, kmp_int32 gtid, kmp_task_t * |
| 590 | // new_task); |
| 591 | OMPRTL__kmpc_omp_task, |
| 592 | // Call to void __kmpc_copyprivate(ident_t *loc, kmp_int32 global_tid, |
| 593 | // size_t cpy_size, void *cpy_data, void(*cpy_func)(void *, void *), |
| 594 | // kmp_int32 didit); |
| 595 | OMPRTL__kmpc_copyprivate, |
| 596 | // Call to kmp_int32 __kmpc_reduce(ident_t *loc, kmp_int32 global_tid, |
| 597 | // kmp_int32 num_vars, size_t reduce_size, void *reduce_data, void |
| 598 | // (*reduce_func)(void *lhs_data, void *rhs_data), kmp_critical_name *lck); |
| 599 | OMPRTL__kmpc_reduce, |
| 600 | // Call to kmp_int32 __kmpc_reduce_nowait(ident_t *loc, kmp_int32 |
| 601 | // global_tid, kmp_int32 num_vars, size_t reduce_size, void *reduce_data, |
| 602 | // void (*reduce_func)(void *lhs_data, void *rhs_data), kmp_critical_name |
| 603 | // *lck); |
| 604 | OMPRTL__kmpc_reduce_nowait, |
| 605 | // Call to void __kmpc_end_reduce(ident_t *loc, kmp_int32 global_tid, |
| 606 | // kmp_critical_name *lck); |
| 607 | OMPRTL__kmpc_end_reduce, |
| 608 | // Call to void __kmpc_end_reduce_nowait(ident_t *loc, kmp_int32 global_tid, |
| 609 | // kmp_critical_name *lck); |
| 610 | OMPRTL__kmpc_end_reduce_nowait, |
| 611 | // Call to void __kmpc_omp_task_begin_if0(ident_t *, kmp_int32 gtid, |
| 612 | // kmp_task_t * new_task); |
| 613 | OMPRTL__kmpc_omp_task_begin_if0, |
| 614 | // Call to void __kmpc_omp_task_complete_if0(ident_t *, kmp_int32 gtid, |
| 615 | // kmp_task_t * new_task); |
| 616 | OMPRTL__kmpc_omp_task_complete_if0, |
| 617 | // Call to void __kmpc_ordered(ident_t *loc, kmp_int32 global_tid); |
| 618 | OMPRTL__kmpc_ordered, |
| 619 | // Call to void __kmpc_end_ordered(ident_t *loc, kmp_int32 global_tid); |
| 620 | OMPRTL__kmpc_end_ordered, |
| 621 | // Call to kmp_int32 __kmpc_omp_taskwait(ident_t *loc, kmp_int32 |
| 622 | // global_tid); |
| 623 | OMPRTL__kmpc_omp_taskwait, |
| 624 | // Call to void __kmpc_taskgroup(ident_t *loc, kmp_int32 global_tid); |
| 625 | OMPRTL__kmpc_taskgroup, |
| 626 | // Call to void __kmpc_end_taskgroup(ident_t *loc, kmp_int32 global_tid); |
| 627 | OMPRTL__kmpc_end_taskgroup, |
| 628 | // Call to void __kmpc_push_proc_bind(ident_t *loc, kmp_int32 global_tid, |
| 629 | // int proc_bind); |
| 630 | OMPRTL__kmpc_push_proc_bind, |
| 631 | // Call to kmp_int32 __kmpc_omp_task_with_deps(ident_t *loc_ref, kmp_int32 |
| 632 | // gtid, kmp_task_t * new_task, kmp_int32 ndeps, kmp_depend_info_t |
| 633 | // *dep_list, kmp_int32 ndeps_noalias, kmp_depend_info_t *noalias_dep_list); |
| 634 | OMPRTL__kmpc_omp_task_with_deps, |
| 635 | // Call to void __kmpc_omp_wait_deps(ident_t *loc_ref, kmp_int32 |
| 636 | // gtid, kmp_int32 ndeps, kmp_depend_info_t *dep_list, kmp_int32 |
| 637 | // ndeps_noalias, kmp_depend_info_t *noalias_dep_list); |
| 638 | OMPRTL__kmpc_omp_wait_deps, |
| 639 | // Call to kmp_int32 __kmpc_cancellationpoint(ident_t *loc, kmp_int32 |
| 640 | // global_tid, kmp_int32 cncl_kind); |
| 641 | OMPRTL__kmpc_cancellationpoint, |
| 642 | // Call to kmp_int32 __kmpc_cancel(ident_t *loc, kmp_int32 global_tid, |
| 643 | // kmp_int32 cncl_kind); |
| 644 | OMPRTL__kmpc_cancel, |
Carlo Bertolli | 430d8ec | 2016-03-03 20:34:23 +0000 | [diff] [blame] | 645 | // Call to void __kmpc_push_num_teams(ident_t *loc, kmp_int32 global_tid, |
| 646 | // kmp_int32 num_teams, kmp_int32 thread_limit); |
| 647 | OMPRTL__kmpc_push_num_teams, |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 648 | // Call to void __kmpc_fork_teams(ident_t *loc, kmp_int32 argc, kmpc_micro |
| 649 | // microtask, ...); |
Carlo Bertolli | 430d8ec | 2016-03-03 20:34:23 +0000 | [diff] [blame] | 650 | OMPRTL__kmpc_fork_teams, |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 651 | // Call to void __kmpc_taskloop(ident_t *loc, int gtid, kmp_task_t *task, int |
| 652 | // if_val, kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st, int nogroup, int |
| 653 | // sched, kmp_uint64 grainsize, void *task_dup); |
| 654 | OMPRTL__kmpc_taskloop, |
Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 655 | // Call to void __kmpc_doacross_init(ident_t *loc, kmp_int32 gtid, kmp_int32 |
| 656 | // num_dims, struct kmp_dim *dims); |
| 657 | OMPRTL__kmpc_doacross_init, |
| 658 | // Call to void __kmpc_doacross_fini(ident_t *loc, kmp_int32 gtid); |
| 659 | OMPRTL__kmpc_doacross_fini, |
| 660 | // Call to void __kmpc_doacross_post(ident_t *loc, kmp_int32 gtid, kmp_int64 |
| 661 | // *vec); |
| 662 | OMPRTL__kmpc_doacross_post, |
| 663 | // Call to void __kmpc_doacross_wait(ident_t *loc, kmp_int32 gtid, kmp_int64 |
| 664 | // *vec); |
| 665 | OMPRTL__kmpc_doacross_wait, |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 666 | // Call to void *__kmpc_task_reduction_init(int gtid, int num_data, void |
| 667 | // *data); |
| 668 | OMPRTL__kmpc_task_reduction_init, |
| 669 | // Call to void *__kmpc_task_reduction_get_th_data(int gtid, void *tg, void |
| 670 | // *d); |
| 671 | OMPRTL__kmpc_task_reduction_get_th_data, |
Alexey Bataev | 50b3c95 | 2016-02-19 10:38:26 +0000 | [diff] [blame] | 672 | |
| 673 | // |
| 674 | // Offloading related calls |
| 675 | // |
George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 676 | // Call to int32_t __tgt_target(int64_t device_id, void *host_ptr, int32_t |
| 677 | // arg_num, void** args_base, void **args, size_t *arg_sizes, int64_t |
Alexey Bataev | 50b3c95 | 2016-02-19 10:38:26 +0000 | [diff] [blame] | 678 | // *arg_types); |
| 679 | OMPRTL__tgt_target, |
Alexey Bataev | a9f77c6 | 2017-12-13 21:04:20 +0000 | [diff] [blame] | 680 | // Call to int32_t __tgt_target_nowait(int64_t device_id, void *host_ptr, |
| 681 | // int32_t arg_num, void** args_base, void **args, size_t *arg_sizes, int64_t |
| 682 | // *arg_types); |
| 683 | OMPRTL__tgt_target_nowait, |
George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 684 | // Call to int32_t __tgt_target_teams(int64_t device_id, void *host_ptr, |
| 685 | // int32_t arg_num, void** args_base, void **args, size_t *arg_sizes, int64_t |
| 686 | // *arg_types, int32_t num_teams, int32_t thread_limit); |
Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 687 | OMPRTL__tgt_target_teams, |
Alexey Bataev | a9f77c6 | 2017-12-13 21:04:20 +0000 | [diff] [blame] | 688 | // Call to int32_t __tgt_target_teams_nowait(int64_t device_id, void |
| 689 | // *host_ptr, int32_t arg_num, void** args_base, void **args, size_t |
| 690 | // *arg_sizes, int64_t *arg_types, int32_t num_teams, int32_t thread_limit); |
| 691 | OMPRTL__tgt_target_teams_nowait, |
Alexey Bataev | 50b3c95 | 2016-02-19 10:38:26 +0000 | [diff] [blame] | 692 | // Call to void __tgt_register_lib(__tgt_bin_desc *desc); |
| 693 | OMPRTL__tgt_register_lib, |
| 694 | // Call to void __tgt_unregister_lib(__tgt_bin_desc *desc); |
| 695 | OMPRTL__tgt_unregister_lib, |
George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 696 | // Call to void __tgt_target_data_begin(int64_t device_id, int32_t arg_num, |
| 697 | // 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] | 698 | OMPRTL__tgt_target_data_begin, |
Alexey Bataev | 0cc6b8e | 2017-12-14 17:00:17 +0000 | [diff] [blame] | 699 | // Call to void __tgt_target_data_begin_nowait(int64_t device_id, int32_t |
| 700 | // arg_num, void** args_base, void **args, size_t *arg_sizes, int64_t |
| 701 | // *arg_types); |
| 702 | OMPRTL__tgt_target_data_begin_nowait, |
George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 703 | // Call to void __tgt_target_data_end(int64_t device_id, int32_t arg_num, |
| 704 | // 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] | 705 | OMPRTL__tgt_target_data_end, |
Alexey Bataev | 0cc6b8e | 2017-12-14 17:00:17 +0000 | [diff] [blame] | 706 | // Call to void __tgt_target_data_end_nowait(int64_t device_id, int32_t |
| 707 | // arg_num, void** args_base, void **args, size_t *arg_sizes, int64_t |
| 708 | // *arg_types); |
| 709 | OMPRTL__tgt_target_data_end_nowait, |
George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 710 | // Call to void __tgt_target_data_update(int64_t device_id, int32_t arg_num, |
| 711 | // void** args_base, void **args, size_t *arg_sizes, int64_t *arg_types); |
Samuel Antao | 8d2d730 | 2016-05-26 18:30:22 +0000 | [diff] [blame] | 712 | OMPRTL__tgt_target_data_update, |
Alexey Bataev | 0cc6b8e | 2017-12-14 17:00:17 +0000 | [diff] [blame] | 713 | // Call to void __tgt_target_data_update_nowait(int64_t device_id, int32_t |
| 714 | // arg_num, void** args_base, void **args, size_t *arg_sizes, int64_t |
| 715 | // *arg_types); |
| 716 | OMPRTL__tgt_target_data_update_nowait, |
Alexey Bataev | 50b3c95 | 2016-02-19 10:38:26 +0000 | [diff] [blame] | 717 | }; |
| 718 | |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 719 | /// A basic class for pre|post-action for advanced codegen sequence for OpenMP |
| 720 | /// region. |
| 721 | class CleanupTy final : public EHScopeStack::Cleanup { |
| 722 | PrePostActionTy *Action; |
| 723 | |
| 724 | public: |
| 725 | explicit CleanupTy(PrePostActionTy *Action) : Action(Action) {} |
| 726 | void Emit(CodeGenFunction &CGF, Flags /*flags*/) override { |
| 727 | if (!CGF.HaveInsertPoint()) |
| 728 | return; |
| 729 | Action->Exit(CGF); |
| 730 | } |
| 731 | }; |
| 732 | |
Hans Wennborg | 7eb5464 | 2015-09-10 17:07:54 +0000 | [diff] [blame] | 733 | } // anonymous namespace |
Alexey Bataev | 1809571 | 2014-10-10 12:19:54 +0000 | [diff] [blame] | 734 | |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 735 | void RegionCodeGenTy::operator()(CodeGenFunction &CGF) const { |
| 736 | CodeGenFunction::RunCleanupsScope Scope(CGF); |
| 737 | if (PrePostAction) { |
| 738 | CGF.EHStack.pushCleanup<CleanupTy>(NormalAndEHCleanup, PrePostAction); |
| 739 | Callback(CodeGen, CGF, *PrePostAction); |
| 740 | } else { |
| 741 | PrePostActionTy Action; |
| 742 | Callback(CodeGen, CGF, Action); |
| 743 | } |
| 744 | } |
| 745 | |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 746 | /// Check if the combiner is a call to UDR combiner and if it is so return the |
| 747 | /// UDR decl used for reduction. |
| 748 | static const OMPDeclareReductionDecl * |
| 749 | getReductionInit(const Expr *ReductionOp) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 750 | if (const auto *CE = dyn_cast<CallExpr>(ReductionOp)) |
| 751 | if (const auto *OVE = dyn_cast<OpaqueValueExpr>(CE->getCallee())) |
| 752 | if (const auto *DRE = |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 753 | dyn_cast<DeclRefExpr>(OVE->getSourceExpr()->IgnoreImpCasts())) |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 754 | if (const auto *DRD = dyn_cast<OMPDeclareReductionDecl>(DRE->getDecl())) |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 755 | return DRD; |
| 756 | return nullptr; |
| 757 | } |
| 758 | |
| 759 | static void emitInitWithReductionInitializer(CodeGenFunction &CGF, |
| 760 | const OMPDeclareReductionDecl *DRD, |
| 761 | const Expr *InitOp, |
| 762 | Address Private, Address Original, |
| 763 | QualType Ty) { |
| 764 | if (DRD->getInitializer()) { |
| 765 | std::pair<llvm::Function *, llvm::Function *> Reduction = |
| 766 | CGF.CGM.getOpenMPRuntime().getUserDefinedReduction(DRD); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 767 | const auto *CE = cast<CallExpr>(InitOp); |
| 768 | const auto *OVE = cast<OpaqueValueExpr>(CE->getCallee()); |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 769 | const Expr *LHS = CE->getArg(/*Arg=*/0)->IgnoreParenImpCasts(); |
| 770 | const Expr *RHS = CE->getArg(/*Arg=*/1)->IgnoreParenImpCasts(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 771 | const auto *LHSDRE = |
| 772 | cast<DeclRefExpr>(cast<UnaryOperator>(LHS)->getSubExpr()); |
| 773 | const auto *RHSDRE = |
| 774 | cast<DeclRefExpr>(cast<UnaryOperator>(RHS)->getSubExpr()); |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 775 | CodeGenFunction::OMPPrivateScope PrivateScope(CGF); |
| 776 | PrivateScope.addPrivate(cast<VarDecl>(LHSDRE->getDecl()), |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 777 | [=]() { return Private; }); |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 778 | PrivateScope.addPrivate(cast<VarDecl>(RHSDRE->getDecl()), |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 779 | [=]() { return Original; }); |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 780 | (void)PrivateScope.Privatize(); |
| 781 | RValue Func = RValue::get(Reduction.second); |
| 782 | CodeGenFunction::OpaqueValueMapping Map(CGF, OVE, Func); |
| 783 | CGF.EmitIgnoredExpr(InitOp); |
| 784 | } else { |
| 785 | llvm::Constant *Init = CGF.CGM.EmitNullConstant(Ty); |
| 786 | auto *GV = new llvm::GlobalVariable( |
| 787 | CGF.CGM.getModule(), Init->getType(), /*isConstant=*/true, |
| 788 | llvm::GlobalValue::PrivateLinkage, Init, ".init"); |
| 789 | LValue LV = CGF.MakeNaturalAlignAddrLValue(GV, Ty); |
| 790 | RValue InitRVal; |
| 791 | switch (CGF.getEvaluationKind(Ty)) { |
| 792 | case TEK_Scalar: |
Alexey Bataev | a9b9cc0 | 2018-01-23 18:12:38 +0000 | [diff] [blame] | 793 | InitRVal = CGF.EmitLoadOfLValue(LV, DRD->getLocation()); |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 794 | break; |
| 795 | case TEK_Complex: |
| 796 | InitRVal = |
Alexey Bataev | a9b9cc0 | 2018-01-23 18:12:38 +0000 | [diff] [blame] | 797 | RValue::getComplex(CGF.EmitLoadOfComplex(LV, DRD->getLocation())); |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 798 | break; |
| 799 | case TEK_Aggregate: |
| 800 | InitRVal = RValue::getAggregate(LV.getAddress()); |
| 801 | break; |
| 802 | } |
Alexey Bataev | a9b9cc0 | 2018-01-23 18:12:38 +0000 | [diff] [blame] | 803 | OpaqueValueExpr OVE(DRD->getLocation(), Ty, VK_RValue); |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 804 | CodeGenFunction::OpaqueValueMapping OpaqueMap(CGF, &OVE, InitRVal); |
| 805 | CGF.EmitAnyExprToMem(&OVE, Private, Ty.getQualifiers(), |
| 806 | /*IsInitializer=*/false); |
| 807 | } |
| 808 | } |
| 809 | |
| 810 | /// \brief Emit initialization of arrays of complex types. |
| 811 | /// \param DestAddr Address of the array. |
| 812 | /// \param Type Type of array. |
| 813 | /// \param Init Initial expression of array. |
| 814 | /// \param SrcAddr Address of the original array. |
| 815 | static void EmitOMPAggregateInit(CodeGenFunction &CGF, Address DestAddr, |
Alexey Bataev | a7b1915 | 2017-10-12 20:03:39 +0000 | [diff] [blame] | 816 | QualType Type, bool EmitDeclareReductionInit, |
| 817 | const Expr *Init, |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 818 | const OMPDeclareReductionDecl *DRD, |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 819 | Address SrcAddr = Address::invalid()) { |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 820 | // Perform element-by-element initialization. |
| 821 | QualType ElementTy; |
| 822 | |
| 823 | // Drill down to the base element type on both arrays. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 824 | const ArrayType *ArrayTy = Type->getAsArrayTypeUnsafe(); |
| 825 | llvm::Value *NumElements = CGF.emitArrayLength(ArrayTy, ElementTy, DestAddr); |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 826 | DestAddr = |
| 827 | CGF.Builder.CreateElementBitCast(DestAddr, DestAddr.getElementType()); |
| 828 | if (DRD) |
| 829 | SrcAddr = |
| 830 | CGF.Builder.CreateElementBitCast(SrcAddr, DestAddr.getElementType()); |
| 831 | |
| 832 | llvm::Value *SrcBegin = nullptr; |
| 833 | if (DRD) |
| 834 | SrcBegin = SrcAddr.getPointer(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 835 | llvm::Value *DestBegin = DestAddr.getPointer(); |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 836 | // Cast from pointer to array type to pointer to single element. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 837 | llvm::Value *DestEnd = CGF.Builder.CreateGEP(DestBegin, NumElements); |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 838 | // The basic structure here is a while-do loop. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 839 | llvm::BasicBlock *BodyBB = CGF.createBasicBlock("omp.arrayinit.body"); |
| 840 | llvm::BasicBlock *DoneBB = CGF.createBasicBlock("omp.arrayinit.done"); |
| 841 | llvm::Value *IsEmpty = |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 842 | CGF.Builder.CreateICmpEQ(DestBegin, DestEnd, "omp.arrayinit.isempty"); |
| 843 | CGF.Builder.CreateCondBr(IsEmpty, DoneBB, BodyBB); |
| 844 | |
| 845 | // Enter the loop body, making that address the current address. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 846 | llvm::BasicBlock *EntryBB = CGF.Builder.GetInsertBlock(); |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 847 | CGF.EmitBlock(BodyBB); |
| 848 | |
| 849 | CharUnits ElementSize = CGF.getContext().getTypeSizeInChars(ElementTy); |
| 850 | |
| 851 | llvm::PHINode *SrcElementPHI = nullptr; |
| 852 | Address SrcElementCurrent = Address::invalid(); |
| 853 | if (DRD) { |
| 854 | SrcElementPHI = CGF.Builder.CreatePHI(SrcBegin->getType(), 2, |
| 855 | "omp.arraycpy.srcElementPast"); |
| 856 | SrcElementPHI->addIncoming(SrcBegin, EntryBB); |
| 857 | SrcElementCurrent = |
| 858 | Address(SrcElementPHI, |
| 859 | SrcAddr.getAlignment().alignmentOfArrayElement(ElementSize)); |
| 860 | } |
| 861 | llvm::PHINode *DestElementPHI = CGF.Builder.CreatePHI( |
| 862 | DestBegin->getType(), 2, "omp.arraycpy.destElementPast"); |
| 863 | DestElementPHI->addIncoming(DestBegin, EntryBB); |
| 864 | Address DestElementCurrent = |
| 865 | Address(DestElementPHI, |
| 866 | DestAddr.getAlignment().alignmentOfArrayElement(ElementSize)); |
| 867 | |
| 868 | // Emit copy. |
| 869 | { |
| 870 | CodeGenFunction::RunCleanupsScope InitScope(CGF); |
Alexey Bataev | a7b1915 | 2017-10-12 20:03:39 +0000 | [diff] [blame] | 871 | if (EmitDeclareReductionInit) { |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 872 | emitInitWithReductionInitializer(CGF, DRD, Init, DestElementCurrent, |
| 873 | SrcElementCurrent, ElementTy); |
| 874 | } else |
| 875 | CGF.EmitAnyExprToMem(Init, DestElementCurrent, ElementTy.getQualifiers(), |
| 876 | /*IsInitializer=*/false); |
| 877 | } |
| 878 | |
| 879 | if (DRD) { |
| 880 | // Shift the address forward by one element. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 881 | llvm::Value *SrcElementNext = CGF.Builder.CreateConstGEP1_32( |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 882 | SrcElementPHI, /*Idx0=*/1, "omp.arraycpy.dest.element"); |
| 883 | SrcElementPHI->addIncoming(SrcElementNext, CGF.Builder.GetInsertBlock()); |
| 884 | } |
| 885 | |
| 886 | // Shift the address forward by one element. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 887 | llvm::Value *DestElementNext = CGF.Builder.CreateConstGEP1_32( |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 888 | DestElementPHI, /*Idx0=*/1, "omp.arraycpy.dest.element"); |
| 889 | // Check whether we've reached the end. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 890 | llvm::Value *Done = |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 891 | CGF.Builder.CreateICmpEQ(DestElementNext, DestEnd, "omp.arraycpy.done"); |
| 892 | CGF.Builder.CreateCondBr(Done, DoneBB, BodyBB); |
| 893 | DestElementPHI->addIncoming(DestElementNext, CGF.Builder.GetInsertBlock()); |
| 894 | |
| 895 | // Done. |
| 896 | CGF.EmitBlock(DoneBB, /*IsFinished=*/true); |
| 897 | } |
| 898 | |
Alexey Bataev | 92327c5 | 2018-03-26 16:40:55 +0000 | [diff] [blame] | 899 | static llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> |
| 900 | isDeclareTargetDeclaration(const ValueDecl *VD) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 901 | for (const Decl *D : VD->redecls()) { |
Alexey Bataev | 92327c5 | 2018-03-26 16:40:55 +0000 | [diff] [blame] | 902 | if (!D->hasAttrs()) |
| 903 | continue; |
| 904 | if (const auto *Attr = D->getAttr<OMPDeclareTargetDeclAttr>()) |
| 905 | return Attr->getMapType(); |
| 906 | } |
| 907 | return llvm::None; |
| 908 | } |
| 909 | |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 910 | LValue ReductionCodeGen::emitSharedLValue(CodeGenFunction &CGF, const Expr *E) { |
Alexey Bataev | f47c4b4 | 2017-09-26 13:47:31 +0000 | [diff] [blame] | 911 | return CGF.EmitOMPSharedLValue(E); |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 912 | } |
| 913 | |
| 914 | LValue ReductionCodeGen::emitSharedLValueUB(CodeGenFunction &CGF, |
| 915 | const Expr *E) { |
| 916 | if (const auto *OASE = dyn_cast<OMPArraySectionExpr>(E)) |
| 917 | return CGF.EmitOMPArraySectionExpr(OASE, /*IsLowerBound=*/false); |
| 918 | return LValue(); |
| 919 | } |
| 920 | |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 921 | void ReductionCodeGen::emitAggregateInitialization( |
| 922 | CodeGenFunction &CGF, unsigned N, Address PrivateAddr, LValue SharedLVal, |
| 923 | const OMPDeclareReductionDecl *DRD) { |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 924 | // Emit VarDecl with copy init for arrays. |
| 925 | // Get the address of the original variable captured in current |
| 926 | // captured region. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 927 | const auto *PrivateVD = |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 928 | cast<VarDecl>(cast<DeclRefExpr>(ClausesData[N].Private)->getDecl()); |
Alexey Bataev | a7b1915 | 2017-10-12 20:03:39 +0000 | [diff] [blame] | 929 | bool EmitDeclareReductionInit = |
| 930 | DRD && (DRD->getInitializer() || !PrivateVD->hasInit()); |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 931 | EmitOMPAggregateInit(CGF, PrivateAddr, PrivateVD->getType(), |
Alexey Bataev | a7b1915 | 2017-10-12 20:03:39 +0000 | [diff] [blame] | 932 | EmitDeclareReductionInit, |
| 933 | EmitDeclareReductionInit ? ClausesData[N].ReductionOp |
| 934 | : PrivateVD->getInit(), |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 935 | DRD, SharedLVal.getAddress()); |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 936 | } |
| 937 | |
| 938 | ReductionCodeGen::ReductionCodeGen(ArrayRef<const Expr *> Shareds, |
| 939 | ArrayRef<const Expr *> Privates, |
| 940 | ArrayRef<const Expr *> ReductionOps) { |
| 941 | ClausesData.reserve(Shareds.size()); |
| 942 | SharedAddresses.reserve(Shareds.size()); |
| 943 | Sizes.reserve(Shareds.size()); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 944 | BaseDecls.reserve(Shareds.size()); |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 945 | auto IPriv = Privates.begin(); |
| 946 | auto IRed = ReductionOps.begin(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 947 | for (const Expr *Ref : Shareds) { |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 948 | ClausesData.emplace_back(Ref, *IPriv, *IRed); |
| 949 | std::advance(IPriv, 1); |
| 950 | std::advance(IRed, 1); |
| 951 | } |
| 952 | } |
| 953 | |
| 954 | void ReductionCodeGen::emitSharedLValue(CodeGenFunction &CGF, unsigned N) { |
| 955 | assert(SharedAddresses.size() == N && |
| 956 | "Number of generated lvalues must be exactly N."); |
Jonas Hahnfeld | 4525c82 | 2017-10-23 19:01:35 +0000 | [diff] [blame] | 957 | LValue First = emitSharedLValue(CGF, ClausesData[N].Ref); |
| 958 | LValue Second = emitSharedLValueUB(CGF, ClausesData[N].Ref); |
| 959 | SharedAddresses.emplace_back(First, Second); |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 960 | } |
| 961 | |
| 962 | void ReductionCodeGen::emitAggregateType(CodeGenFunction &CGF, unsigned N) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 963 | const auto *PrivateVD = |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 964 | cast<VarDecl>(cast<DeclRefExpr>(ClausesData[N].Private)->getDecl()); |
| 965 | QualType PrivateType = PrivateVD->getType(); |
| 966 | bool AsArraySection = isa<OMPArraySectionExpr>(ClausesData[N].Ref); |
Jonas Hahnfeld | 4525c82 | 2017-10-23 19:01:35 +0000 | [diff] [blame] | 967 | if (!PrivateType->isVariablyModifiedType()) { |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 968 | Sizes.emplace_back( |
| 969 | CGF.getTypeSize( |
| 970 | SharedAddresses[N].first.getType().getNonReferenceType()), |
| 971 | nullptr); |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 972 | return; |
| 973 | } |
| 974 | llvm::Value *Size; |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 975 | llvm::Value *SizeInChars; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 976 | auto *ElemType = |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 977 | cast<llvm::PointerType>(SharedAddresses[N].first.getPointer()->getType()) |
| 978 | ->getElementType(); |
| 979 | auto *ElemSizeOf = llvm::ConstantExpr::getSizeOf(ElemType); |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 980 | if (AsArraySection) { |
| 981 | Size = CGF.Builder.CreatePtrDiff(SharedAddresses[N].second.getPointer(), |
| 982 | SharedAddresses[N].first.getPointer()); |
| 983 | Size = CGF.Builder.CreateNUWAdd( |
| 984 | Size, llvm::ConstantInt::get(Size->getType(), /*V=*/1)); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 985 | SizeInChars = CGF.Builder.CreateNUWMul(Size, ElemSizeOf); |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 986 | } else { |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 987 | SizeInChars = CGF.getTypeSize( |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 988 | SharedAddresses[N].first.getType().getNonReferenceType()); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 989 | Size = CGF.Builder.CreateExactUDiv(SizeInChars, ElemSizeOf); |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 990 | } |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 991 | Sizes.emplace_back(SizeInChars, Size); |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 992 | CodeGenFunction::OpaqueValueMapping OpaqueMap( |
| 993 | CGF, |
| 994 | cast<OpaqueValueExpr>( |
| 995 | CGF.getContext().getAsVariableArrayType(PrivateType)->getSizeExpr()), |
| 996 | RValue::get(Size)); |
| 997 | CGF.EmitVariablyModifiedType(PrivateType); |
| 998 | } |
| 999 | |
| 1000 | void ReductionCodeGen::emitAggregateType(CodeGenFunction &CGF, unsigned N, |
| 1001 | llvm::Value *Size) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 1002 | const auto *PrivateVD = |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1003 | cast<VarDecl>(cast<DeclRefExpr>(ClausesData[N].Private)->getDecl()); |
| 1004 | QualType PrivateType = PrivateVD->getType(); |
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 | assert(!Size && !Sizes[N].second && |
Jonas Hahnfeld | 4525c82 | 2017-10-23 19:01:35 +0000 | [diff] [blame] | 1007 | "Size should be nullptr for non-variably modified reduction " |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1008 | "items."); |
| 1009 | return; |
| 1010 | } |
| 1011 | CodeGenFunction::OpaqueValueMapping OpaqueMap( |
| 1012 | CGF, |
| 1013 | cast<OpaqueValueExpr>( |
| 1014 | CGF.getContext().getAsVariableArrayType(PrivateType)->getSizeExpr()), |
| 1015 | RValue::get(Size)); |
| 1016 | CGF.EmitVariablyModifiedType(PrivateType); |
| 1017 | } |
| 1018 | |
| 1019 | void ReductionCodeGen::emitInitialization( |
| 1020 | CodeGenFunction &CGF, unsigned N, Address PrivateAddr, LValue SharedLVal, |
| 1021 | llvm::function_ref<bool(CodeGenFunction &)> DefaultInit) { |
| 1022 | assert(SharedAddresses.size() > N && "No variable was generated"); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 1023 | const auto *PrivateVD = |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1024 | cast<VarDecl>(cast<DeclRefExpr>(ClausesData[N].Private)->getDecl()); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 1025 | const OMPDeclareReductionDecl *DRD = |
| 1026 | getReductionInit(ClausesData[N].ReductionOp); |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1027 | QualType PrivateType = PrivateVD->getType(); |
| 1028 | PrivateAddr = CGF.Builder.CreateElementBitCast( |
| 1029 | PrivateAddr, CGF.ConvertTypeForMem(PrivateType)); |
| 1030 | QualType SharedType = SharedAddresses[N].first.getType(); |
| 1031 | SharedLVal = CGF.MakeAddrLValue( |
| 1032 | CGF.Builder.CreateElementBitCast(SharedLVal.getAddress(), |
| 1033 | CGF.ConvertTypeForMem(SharedType)), |
Ivan A. Kosarev | f5f2046 | 2017-10-12 11:29:46 +0000 | [diff] [blame] | 1034 | SharedType, SharedAddresses[N].first.getBaseInfo(), |
Ivan A. Kosarev | b9c59f3 | 2017-10-31 11:05:34 +0000 | [diff] [blame] | 1035 | CGF.CGM.getTBAAInfoForSubobject(SharedAddresses[N].first, SharedType)); |
Jonas Hahnfeld | 4525c82 | 2017-10-23 19:01:35 +0000 | [diff] [blame] | 1036 | if (CGF.getContext().getAsArrayType(PrivateVD->getType())) { |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 1037 | emitAggregateInitialization(CGF, N, PrivateAddr, SharedLVal, DRD); |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1038 | } else if (DRD && (DRD->getInitializer() || !PrivateVD->hasInit())) { |
| 1039 | emitInitWithReductionInitializer(CGF, DRD, ClausesData[N].ReductionOp, |
| 1040 | PrivateAddr, SharedLVal.getAddress(), |
| 1041 | SharedLVal.getType()); |
| 1042 | } else if (!DefaultInit(CGF) && PrivateVD->hasInit() && |
| 1043 | !CGF.isTrivialInitializer(PrivateVD->getInit())) { |
| 1044 | CGF.EmitAnyExprToMem(PrivateVD->getInit(), PrivateAddr, |
| 1045 | PrivateVD->getType().getQualifiers(), |
| 1046 | /*IsInitializer=*/false); |
| 1047 | } |
| 1048 | } |
| 1049 | |
| 1050 | bool ReductionCodeGen::needCleanups(unsigned N) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 1051 | const auto *PrivateVD = |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1052 | cast<VarDecl>(cast<DeclRefExpr>(ClausesData[N].Private)->getDecl()); |
| 1053 | QualType PrivateType = PrivateVD->getType(); |
| 1054 | QualType::DestructionKind DTorKind = PrivateType.isDestructedType(); |
| 1055 | return DTorKind != QualType::DK_none; |
| 1056 | } |
| 1057 | |
| 1058 | void ReductionCodeGen::emitCleanups(CodeGenFunction &CGF, unsigned N, |
| 1059 | Address PrivateAddr) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 1060 | const auto *PrivateVD = |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1061 | cast<VarDecl>(cast<DeclRefExpr>(ClausesData[N].Private)->getDecl()); |
| 1062 | QualType PrivateType = PrivateVD->getType(); |
| 1063 | QualType::DestructionKind DTorKind = PrivateType.isDestructedType(); |
| 1064 | if (needCleanups(N)) { |
| 1065 | PrivateAddr = CGF.Builder.CreateElementBitCast( |
| 1066 | PrivateAddr, CGF.ConvertTypeForMem(PrivateType)); |
| 1067 | CGF.pushDestroy(DTorKind, PrivateAddr, PrivateType); |
| 1068 | } |
| 1069 | } |
| 1070 | |
| 1071 | static LValue loadToBegin(CodeGenFunction &CGF, QualType BaseTy, QualType ElTy, |
| 1072 | LValue BaseLV) { |
| 1073 | BaseTy = BaseTy.getNonReferenceType(); |
| 1074 | while ((BaseTy->isPointerType() || BaseTy->isReferenceType()) && |
| 1075 | !CGF.getContext().hasSameType(BaseTy, ElTy)) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 1076 | if (const auto *PtrTy = BaseTy->getAs<PointerType>()) { |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1077 | BaseLV = CGF.EmitLoadOfPointerLValue(BaseLV.getAddress(), PtrTy); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 1078 | } else { |
Ivan A. Kosarev | 9f9d157 | 2017-10-30 11:49:31 +0000 | [diff] [blame] | 1079 | LValue RefLVal = CGF.MakeAddrLValue(BaseLV.getAddress(), BaseTy); |
| 1080 | BaseLV = CGF.EmitLoadOfReferenceLValue(RefLVal); |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1081 | } |
| 1082 | BaseTy = BaseTy->getPointeeType(); |
| 1083 | } |
| 1084 | return CGF.MakeAddrLValue( |
| 1085 | CGF.Builder.CreateElementBitCast(BaseLV.getAddress(), |
| 1086 | CGF.ConvertTypeForMem(ElTy)), |
Ivan A. Kosarev | f5f2046 | 2017-10-12 11:29:46 +0000 | [diff] [blame] | 1087 | BaseLV.getType(), BaseLV.getBaseInfo(), |
Ivan A. Kosarev | b9c59f3 | 2017-10-31 11:05:34 +0000 | [diff] [blame] | 1088 | CGF.CGM.getTBAAInfoForSubobject(BaseLV, BaseLV.getType())); |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1089 | } |
| 1090 | |
| 1091 | static Address castToBase(CodeGenFunction &CGF, QualType BaseTy, QualType ElTy, |
| 1092 | llvm::Type *BaseLVType, CharUnits BaseLVAlignment, |
| 1093 | llvm::Value *Addr) { |
| 1094 | Address Tmp = Address::invalid(); |
| 1095 | Address TopTmp = Address::invalid(); |
| 1096 | Address MostTopTmp = Address::invalid(); |
| 1097 | BaseTy = BaseTy.getNonReferenceType(); |
| 1098 | while ((BaseTy->isPointerType() || BaseTy->isReferenceType()) && |
| 1099 | !CGF.getContext().hasSameType(BaseTy, ElTy)) { |
| 1100 | Tmp = CGF.CreateMemTemp(BaseTy); |
| 1101 | if (TopTmp.isValid()) |
| 1102 | CGF.Builder.CreateStore(Tmp.getPointer(), TopTmp); |
| 1103 | else |
| 1104 | MostTopTmp = Tmp; |
| 1105 | TopTmp = Tmp; |
| 1106 | BaseTy = BaseTy->getPointeeType(); |
| 1107 | } |
| 1108 | llvm::Type *Ty = BaseLVType; |
| 1109 | if (Tmp.isValid()) |
| 1110 | Ty = Tmp.getElementType(); |
| 1111 | Addr = CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(Addr, Ty); |
| 1112 | if (Tmp.isValid()) { |
| 1113 | CGF.Builder.CreateStore(Addr, Tmp); |
| 1114 | return MostTopTmp; |
| 1115 | } |
| 1116 | return Address(Addr, BaseLVAlignment); |
| 1117 | } |
| 1118 | |
Alexey Bataev | 1c44e15 | 2018-03-06 18:59:43 +0000 | [diff] [blame] | 1119 | static const VarDecl *getBaseDecl(const Expr *Ref, const DeclRefExpr *&DE) { |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1120 | const VarDecl *OrigVD = nullptr; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 1121 | if (const auto *OASE = dyn_cast<OMPArraySectionExpr>(Ref)) { |
| 1122 | const Expr *Base = OASE->getBase()->IgnoreParenImpCasts(); |
| 1123 | while (const auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base)) |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1124 | Base = TempOASE->getBase()->IgnoreParenImpCasts(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 1125 | while (const auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base)) |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1126 | Base = TempASE->getBase()->IgnoreParenImpCasts(); |
| 1127 | DE = cast<DeclRefExpr>(Base); |
| 1128 | OrigVD = cast<VarDecl>(DE->getDecl()); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 1129 | } else if (const auto *ASE = dyn_cast<ArraySubscriptExpr>(Ref)) { |
| 1130 | const Expr *Base = ASE->getBase()->IgnoreParenImpCasts(); |
| 1131 | while (const auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base)) |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1132 | Base = TempASE->getBase()->IgnoreParenImpCasts(); |
| 1133 | DE = cast<DeclRefExpr>(Base); |
| 1134 | OrigVD = cast<VarDecl>(DE->getDecl()); |
| 1135 | } |
Alexey Bataev | 1c44e15 | 2018-03-06 18:59:43 +0000 | [diff] [blame] | 1136 | return OrigVD; |
| 1137 | } |
| 1138 | |
| 1139 | Address ReductionCodeGen::adjustPrivateAddress(CodeGenFunction &CGF, unsigned N, |
| 1140 | Address PrivateAddr) { |
| 1141 | const DeclRefExpr *DE; |
| 1142 | if (const VarDecl *OrigVD = ::getBaseDecl(ClausesData[N].Ref, DE)) { |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1143 | BaseDecls.emplace_back(OrigVD); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 1144 | LValue OriginalBaseLValue = CGF.EmitLValue(DE); |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1145 | LValue BaseLValue = |
| 1146 | loadToBegin(CGF, OrigVD->getType(), SharedAddresses[N].first.getType(), |
| 1147 | OriginalBaseLValue); |
| 1148 | llvm::Value *Adjustment = CGF.Builder.CreatePtrDiff( |
| 1149 | BaseLValue.getPointer(), SharedAddresses[N].first.getPointer()); |
Jonas Hahnfeld | 273d261 | 2017-12-06 19:15:28 +0000 | [diff] [blame] | 1150 | llvm::Value *PrivatePointer = |
| 1151 | CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
| 1152 | PrivateAddr.getPointer(), |
| 1153 | SharedAddresses[N].first.getAddress().getType()); |
| 1154 | llvm::Value *Ptr = CGF.Builder.CreateGEP(PrivatePointer, Adjustment); |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1155 | return castToBase(CGF, OrigVD->getType(), |
| 1156 | SharedAddresses[N].first.getType(), |
Jonas Hahnfeld | 273d261 | 2017-12-06 19:15:28 +0000 | [diff] [blame] | 1157 | OriginalBaseLValue.getAddress().getType(), |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1158 | OriginalBaseLValue.getAlignment(), Ptr); |
| 1159 | } |
| 1160 | BaseDecls.emplace_back( |
| 1161 | cast<VarDecl>(cast<DeclRefExpr>(ClausesData[N].Ref)->getDecl())); |
| 1162 | return PrivateAddr; |
| 1163 | } |
| 1164 | |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 1165 | bool ReductionCodeGen::usesReductionInitializer(unsigned N) const { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 1166 | const OMPDeclareReductionDecl *DRD = |
| 1167 | getReductionInit(ClausesData[N].ReductionOp); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 1168 | return DRD && DRD->getInitializer(); |
| 1169 | } |
| 1170 | |
Alexey Bataev | 1809571 | 2014-10-10 12:19:54 +0000 | [diff] [blame] | 1171 | LValue CGOpenMPRegionInfo::getThreadIDVariableLValue(CodeGenFunction &CGF) { |
Alexey Bataev | 31300ed | 2016-02-04 11:27:03 +0000 | [diff] [blame] | 1172 | return CGF.EmitLoadOfPointerLValue( |
| 1173 | CGF.GetAddrOfLocalVar(getThreadIDVariable()), |
| 1174 | getThreadIDVariable()->getType()->castAs<PointerType>()); |
Alexey Bataev | 1809571 | 2014-10-10 12:19:54 +0000 | [diff] [blame] | 1175 | } |
| 1176 | |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 1177 | void CGOpenMPRegionInfo::EmitBody(CodeGenFunction &CGF, const Stmt * /*S*/) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 1178 | if (!CGF.HaveInsertPoint()) |
| 1179 | return; |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 1180 | // 1.2.2 OpenMP Language Terminology |
| 1181 | // Structured block - An executable statement with a single entry at the |
| 1182 | // top and a single exit at the bottom. |
| 1183 | // The point of exit cannot be a branch out of the structured block. |
| 1184 | // longjmp() and throw() must not violate the entry/exit criteria. |
| 1185 | CGF.EHStack.pushTerminate(); |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 1186 | CodeGen(CGF); |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 1187 | CGF.EHStack.popTerminate(); |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 1188 | } |
| 1189 | |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 1190 | LValue CGOpenMPTaskOutlinedRegionInfo::getThreadIDVariableLValue( |
| 1191 | CodeGenFunction &CGF) { |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 1192 | return CGF.MakeAddrLValue(CGF.GetAddrOfLocalVar(getThreadIDVariable()), |
| 1193 | getThreadIDVariable()->getType(), |
Ivan A. Kosarev | 5f8c0ca | 2017-10-10 09:39:32 +0000 | [diff] [blame] | 1194 | AlignmentSource::Decl); |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 1195 | } |
| 1196 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 1197 | static FieldDecl *addFieldToRecordDecl(ASTContext &C, DeclContext *DC, |
| 1198 | QualType FieldTy) { |
| 1199 | auto *Field = FieldDecl::Create( |
| 1200 | C, DC, SourceLocation(), SourceLocation(), /*Id=*/nullptr, FieldTy, |
| 1201 | C.getTrivialTypeSourceInfo(FieldTy, SourceLocation()), |
| 1202 | /*BW=*/nullptr, /*Mutable=*/false, /*InitStyle=*/ICIS_NoInit); |
| 1203 | Field->setAccess(AS_public); |
| 1204 | DC->addDecl(Field); |
| 1205 | return Field; |
| 1206 | } |
| 1207 | |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1208 | CGOpenMPRuntime::CGOpenMPRuntime(CodeGenModule &CGM) |
Alexey Bataev | c5b1d32 | 2016-03-04 09:22:22 +0000 | [diff] [blame] | 1209 | : CGM(CGM), OffloadEntriesInfoManager(CGM) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 1210 | ASTContext &C = CGM.getContext(); |
| 1211 | RecordDecl *RD = C.buildImplicitRecord("ident_t"); |
| 1212 | QualType KmpInt32Ty = C.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1); |
| 1213 | RD->startDefinition(); |
| 1214 | // reserved_1 |
| 1215 | addFieldToRecordDecl(C, RD, KmpInt32Ty); |
| 1216 | // flags |
| 1217 | addFieldToRecordDecl(C, RD, KmpInt32Ty); |
| 1218 | // reserved_2 |
| 1219 | addFieldToRecordDecl(C, RD, KmpInt32Ty); |
| 1220 | // reserved_3 |
| 1221 | addFieldToRecordDecl(C, RD, KmpInt32Ty); |
| 1222 | // psource |
| 1223 | addFieldToRecordDecl(C, RD, C.VoidPtrTy); |
| 1224 | RD->completeDefinition(); |
| 1225 | IdentQTy = C.getRecordType(RD); |
| 1226 | IdentTy = CGM.getTypes().ConvertRecordDeclType(RD); |
Alexey Bataev | 3a3bf0b | 2014-09-22 10:01:53 +0000 | [diff] [blame] | 1227 | KmpCriticalNameTy = llvm::ArrayType::get(CGM.Int32Ty, /*NumElements*/ 8); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 1228 | |
| 1229 | loadOffloadInfoMetadata(); |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1230 | } |
| 1231 | |
Alexey Bataev | 9179755 | 2015-03-18 04:13:55 +0000 | [diff] [blame] | 1232 | void CGOpenMPRuntime::clear() { |
| 1233 | InternalVars.clear(); |
| 1234 | } |
| 1235 | |
Alexey Bataev | c5b1d32 | 2016-03-04 09:22:22 +0000 | [diff] [blame] | 1236 | static llvm::Function * |
| 1237 | emitCombinerOrInitializer(CodeGenModule &CGM, QualType Ty, |
| 1238 | const Expr *CombinerInitializer, const VarDecl *In, |
| 1239 | const VarDecl *Out, bool IsCombiner) { |
| 1240 | // void .omp_combiner.(Ty *in, Ty *out); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 1241 | ASTContext &C = CGM.getContext(); |
Alexey Bataev | c5b1d32 | 2016-03-04 09:22:22 +0000 | [diff] [blame] | 1242 | QualType PtrTy = C.getPointerType(Ty).withRestrict(); |
| 1243 | FunctionArgList Args; |
Alexey Bataev | c5b1d32 | 2016-03-04 09:22:22 +0000 | [diff] [blame] | 1244 | ImplicitParamDecl OmpOutParm(C, /*DC=*/nullptr, Out->getLocation(), |
Alexey Bataev | 5622323 | 2017-06-09 13:40:18 +0000 | [diff] [blame] | 1245 | /*Id=*/nullptr, PtrTy, ImplicitParamDecl::Other); |
Alexey Bataev | a839ddd | 2016-03-17 10:19:46 +0000 | [diff] [blame] | 1246 | ImplicitParamDecl OmpInParm(C, /*DC=*/nullptr, In->getLocation(), |
Alexey Bataev | 5622323 | 2017-06-09 13:40:18 +0000 | [diff] [blame] | 1247 | /*Id=*/nullptr, PtrTy, ImplicitParamDecl::Other); |
Alexey Bataev | c5b1d32 | 2016-03-04 09:22:22 +0000 | [diff] [blame] | 1248 | Args.push_back(&OmpOutParm); |
Alexey Bataev | a839ddd | 2016-03-17 10:19:46 +0000 | [diff] [blame] | 1249 | Args.push_back(&OmpInParm); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 1250 | const CGFunctionInfo &FnInfo = |
John McCall | c56a8b3 | 2016-03-11 04:30:31 +0000 | [diff] [blame] | 1251 | CGM.getTypes().arrangeBuiltinFunctionDeclaration(C.VoidTy, Args); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 1252 | llvm::FunctionType *FnTy = CGM.getTypes().GetFunctionType(FnInfo); |
Alexey Bataev | c5b1d32 | 2016-03-04 09:22:22 +0000 | [diff] [blame] | 1253 | auto *Fn = llvm::Function::Create( |
| 1254 | FnTy, llvm::GlobalValue::InternalLinkage, |
| 1255 | IsCombiner ? ".omp_combiner." : ".omp_initializer.", &CGM.getModule()); |
Rafael Espindola | 51ec5a9 | 2018-02-28 23:46:35 +0000 | [diff] [blame] | 1256 | CGM.SetInternalFunctionAttributes(GlobalDecl(), Fn, FnInfo); |
Chandler Carruth | fcd3314 | 2016-12-23 01:24:49 +0000 | [diff] [blame] | 1257 | Fn->removeFnAttr(llvm::Attribute::NoInline); |
Mehdi Amini | 6aa9e9b | 2017-05-29 05:38:20 +0000 | [diff] [blame] | 1258 | Fn->removeFnAttr(llvm::Attribute::OptimizeNone); |
Alexey Bataev | a839ddd | 2016-03-17 10:19:46 +0000 | [diff] [blame] | 1259 | Fn->addFnAttr(llvm::Attribute::AlwaysInline); |
Alexey Bataev | c5b1d32 | 2016-03-04 09:22:22 +0000 | [diff] [blame] | 1260 | CodeGenFunction CGF(CGM); |
| 1261 | // Map "T omp_in;" variable to "*omp_in_parm" value in all expressions. |
| 1262 | // 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] | 1263 | CGF.StartFunction(GlobalDecl(), C.VoidTy, Fn, FnInfo, Args, In->getLocation(), |
| 1264 | Out->getLocation()); |
Alexey Bataev | c5b1d32 | 2016-03-04 09:22:22 +0000 | [diff] [blame] | 1265 | CodeGenFunction::OMPPrivateScope Scope(CGF); |
| 1266 | Address AddrIn = CGF.GetAddrOfLocalVar(&OmpInParm); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 1267 | Scope.addPrivate(In, [&CGF, AddrIn, PtrTy]() { |
Alexey Bataev | c5b1d32 | 2016-03-04 09:22:22 +0000 | [diff] [blame] | 1268 | return CGF.EmitLoadOfPointerLValue(AddrIn, PtrTy->castAs<PointerType>()) |
| 1269 | .getAddress(); |
| 1270 | }); |
| 1271 | Address AddrOut = CGF.GetAddrOfLocalVar(&OmpOutParm); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 1272 | Scope.addPrivate(Out, [&CGF, AddrOut, PtrTy]() { |
Alexey Bataev | c5b1d32 | 2016-03-04 09:22:22 +0000 | [diff] [blame] | 1273 | return CGF.EmitLoadOfPointerLValue(AddrOut, PtrTy->castAs<PointerType>()) |
| 1274 | .getAddress(); |
| 1275 | }); |
| 1276 | (void)Scope.Privatize(); |
Alexey Bataev | 070f43a | 2017-09-06 14:49:58 +0000 | [diff] [blame] | 1277 | if (!IsCombiner && Out->hasInit() && |
| 1278 | !CGF.isTrivialInitializer(Out->getInit())) { |
| 1279 | CGF.EmitAnyExprToMem(Out->getInit(), CGF.GetAddrOfLocalVar(Out), |
| 1280 | Out->getType().getQualifiers(), |
| 1281 | /*IsInitializer=*/true); |
| 1282 | } |
| 1283 | if (CombinerInitializer) |
| 1284 | CGF.EmitIgnoredExpr(CombinerInitializer); |
Alexey Bataev | c5b1d32 | 2016-03-04 09:22:22 +0000 | [diff] [blame] | 1285 | Scope.ForceCleanup(); |
| 1286 | CGF.FinishFunction(); |
| 1287 | return Fn; |
| 1288 | } |
| 1289 | |
| 1290 | void CGOpenMPRuntime::emitUserDefinedReduction( |
| 1291 | CodeGenFunction *CGF, const OMPDeclareReductionDecl *D) { |
| 1292 | if (UDRMap.count(D) > 0) |
| 1293 | return; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 1294 | ASTContext &C = CGM.getContext(); |
Alexey Bataev | c5b1d32 | 2016-03-04 09:22:22 +0000 | [diff] [blame] | 1295 | if (!In || !Out) { |
| 1296 | In = &C.Idents.get("omp_in"); |
| 1297 | Out = &C.Idents.get("omp_out"); |
| 1298 | } |
| 1299 | llvm::Function *Combiner = emitCombinerOrInitializer( |
| 1300 | CGM, D->getType(), D->getCombiner(), cast<VarDecl>(D->lookup(In).front()), |
| 1301 | cast<VarDecl>(D->lookup(Out).front()), |
| 1302 | /*IsCombiner=*/true); |
| 1303 | llvm::Function *Initializer = nullptr; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 1304 | if (const Expr *Init = D->getInitializer()) { |
Alexey Bataev | c5b1d32 | 2016-03-04 09:22:22 +0000 | [diff] [blame] | 1305 | if (!Priv || !Orig) { |
| 1306 | Priv = &C.Idents.get("omp_priv"); |
| 1307 | Orig = &C.Idents.get("omp_orig"); |
| 1308 | } |
| 1309 | Initializer = emitCombinerOrInitializer( |
Alexey Bataev | 070f43a | 2017-09-06 14:49:58 +0000 | [diff] [blame] | 1310 | CGM, D->getType(), |
| 1311 | D->getInitializerKind() == OMPDeclareReductionDecl::CallInit ? Init |
| 1312 | : nullptr, |
| 1313 | cast<VarDecl>(D->lookup(Orig).front()), |
Alexey Bataev | c5b1d32 | 2016-03-04 09:22:22 +0000 | [diff] [blame] | 1314 | cast<VarDecl>(D->lookup(Priv).front()), |
| 1315 | /*IsCombiner=*/false); |
| 1316 | } |
Alexey Bataev | 43a919f | 2018-04-13 17:48:43 +0000 | [diff] [blame] | 1317 | UDRMap.try_emplace(D, Combiner, Initializer); |
Alexey Bataev | c5b1d32 | 2016-03-04 09:22:22 +0000 | [diff] [blame] | 1318 | if (CGF) { |
| 1319 | auto &Decls = FunctionUDRMap.FindAndConstruct(CGF->CurFn); |
| 1320 | Decls.second.push_back(D); |
| 1321 | } |
| 1322 | } |
| 1323 | |
Alexey Bataev | a839ddd | 2016-03-17 10:19:46 +0000 | [diff] [blame] | 1324 | std::pair<llvm::Function *, llvm::Function *> |
| 1325 | CGOpenMPRuntime::getUserDefinedReduction(const OMPDeclareReductionDecl *D) { |
| 1326 | auto I = UDRMap.find(D); |
| 1327 | if (I != UDRMap.end()) |
| 1328 | return I->second; |
| 1329 | emitUserDefinedReduction(/*CGF=*/nullptr, D); |
| 1330 | return UDRMap.lookup(D); |
| 1331 | } |
| 1332 | |
Arpith Chacko Jacob | 19b911c | 2017-01-18 18:18:53 +0000 | [diff] [blame] | 1333 | static llvm::Value *emitParallelOrTeamsOutlinedFunction( |
| 1334 | CodeGenModule &CGM, const OMPExecutableDirective &D, const CapturedStmt *CS, |
| 1335 | const VarDecl *ThreadIDVar, OpenMPDirectiveKind InnermostKind, |
| 1336 | const StringRef OutlinedHelperName, const RegionCodeGenTy &CodeGen) { |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 1337 | assert(ThreadIDVar->getType()->isPointerType() && |
| 1338 | "thread id variable must be of type kmp_int32 *"); |
Alexey Bataev | 1809571 | 2014-10-10 12:19:54 +0000 | [diff] [blame] | 1339 | CodeGenFunction CGF(CGM, true); |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 1340 | bool HasCancel = false; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 1341 | if (const auto *OPD = dyn_cast<OMPParallelDirective>(&D)) |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 1342 | HasCancel = OPD->hasCancel(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 1343 | else if (const auto *OPSD = dyn_cast<OMPParallelSectionsDirective>(&D)) |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 1344 | HasCancel = OPSD->hasCancel(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 1345 | else if (const auto *OPFD = dyn_cast<OMPParallelForDirective>(&D)) |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 1346 | HasCancel = OPFD->hasCancel(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 1347 | else if (const auto *OPFD = dyn_cast<OMPTargetParallelForDirective>(&D)) |
Alexey Bataev | 2139ed6 | 2017-11-16 18:20:21 +0000 | [diff] [blame] | 1348 | HasCancel = OPFD->hasCancel(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 1349 | else if (const auto *OPFD = dyn_cast<OMPDistributeParallelForDirective>(&D)) |
Alexey Bataev | 10a5431 | 2017-11-27 16:54:08 +0000 | [diff] [blame] | 1350 | HasCancel = OPFD->hasCancel(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 1351 | else if (const auto *OPFD = |
| 1352 | dyn_cast<OMPTeamsDistributeParallelForDirective>(&D)) |
Alexey Bataev | 10a5431 | 2017-11-27 16:54:08 +0000 | [diff] [blame] | 1353 | HasCancel = OPFD->hasCancel(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 1354 | else if (const auto *OPFD = |
Alexey Bataev | 10a5431 | 2017-11-27 16:54:08 +0000 | [diff] [blame] | 1355 | dyn_cast<OMPTargetTeamsDistributeParallelForDirective>(&D)) |
| 1356 | HasCancel = OPFD->hasCancel(); |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 1357 | CGOpenMPOutlinedRegionInfo CGInfo(*CS, ThreadIDVar, CodeGen, InnermostKind, |
Arpith Chacko Jacob | 19b911c | 2017-01-18 18:18:53 +0000 | [diff] [blame] | 1358 | HasCancel, OutlinedHelperName); |
Alexey Bataev | d157d47 | 2015-06-24 03:35:38 +0000 | [diff] [blame] | 1359 | CodeGenFunction::CGCapturedStmtRAII CapInfoRAII(CGF, &CGInfo); |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 1360 | return CGF.GenerateOpenMPCapturedStmtFunction(*CS); |
Alexey Bataev | 1809571 | 2014-10-10 12:19:54 +0000 | [diff] [blame] | 1361 | } |
| 1362 | |
Arpith Chacko Jacob | 19b911c | 2017-01-18 18:18:53 +0000 | [diff] [blame] | 1363 | llvm::Value *CGOpenMPRuntime::emitParallelOutlinedFunction( |
| 1364 | const OMPExecutableDirective &D, const VarDecl *ThreadIDVar, |
| 1365 | OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen) { |
| 1366 | const CapturedStmt *CS = D.getCapturedStmt(OMPD_parallel); |
| 1367 | return emitParallelOrTeamsOutlinedFunction( |
| 1368 | CGM, D, CS, ThreadIDVar, InnermostKind, getOutlinedHelperName(), CodeGen); |
| 1369 | } |
| 1370 | |
| 1371 | llvm::Value *CGOpenMPRuntime::emitTeamsOutlinedFunction( |
| 1372 | const OMPExecutableDirective &D, const VarDecl *ThreadIDVar, |
| 1373 | OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen) { |
| 1374 | const CapturedStmt *CS = D.getCapturedStmt(OMPD_teams); |
| 1375 | return emitParallelOrTeamsOutlinedFunction( |
| 1376 | CGM, D, CS, ThreadIDVar, InnermostKind, getOutlinedHelperName(), CodeGen); |
| 1377 | } |
| 1378 | |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 1379 | llvm::Value *CGOpenMPRuntime::emitTaskOutlinedFunction( |
| 1380 | const OMPExecutableDirective &D, const VarDecl *ThreadIDVar, |
Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 1381 | const VarDecl *PartIDVar, const VarDecl *TaskTVar, |
| 1382 | OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen, |
| 1383 | bool Tied, unsigned &NumberOfParts) { |
| 1384 | auto &&UntiedCodeGen = [this, &D, TaskTVar](CodeGenFunction &CGF, |
| 1385 | PrePostActionTy &) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 1386 | llvm::Value *ThreadID = getThreadID(CGF, D.getLocStart()); |
| 1387 | llvm::Value *UpLoc = emitUpdateLocation(CGF, D.getLocStart()); |
Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 1388 | llvm::Value *TaskArgs[] = { |
| 1389 | UpLoc, ThreadID, |
| 1390 | CGF.EmitLoadOfPointerLValue(CGF.GetAddrOfLocalVar(TaskTVar), |
| 1391 | TaskTVar->getType()->castAs<PointerType>()) |
| 1392 | .getPointer()}; |
| 1393 | CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_omp_task), TaskArgs); |
| 1394 | }; |
| 1395 | CGOpenMPTaskOutlinedRegionInfo::UntiedTaskActionTy Action(Tied, PartIDVar, |
| 1396 | UntiedCodeGen); |
| 1397 | CodeGen.setAction(Action); |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 1398 | assert(!ThreadIDVar->getType()->isPointerType() && |
| 1399 | "thread id variable must be of type kmp_int32 for tasks"); |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 1400 | const OpenMPDirectiveKind Region = |
| 1401 | isOpenMPTaskLoopDirective(D.getDirectiveKind()) ? OMPD_taskloop |
| 1402 | : OMPD_task; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 1403 | const CapturedStmt *CS = D.getCapturedStmt(Region); |
| 1404 | const auto *TD = dyn_cast<OMPTaskDirective>(&D); |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 1405 | CodeGenFunction CGF(CGM, true); |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 1406 | CGOpenMPTaskOutlinedRegionInfo CGInfo(*CS, ThreadIDVar, CodeGen, |
| 1407 | InnermostKind, |
| 1408 | TD ? TD->hasCancel() : false, Action); |
Alexey Bataev | d157d47 | 2015-06-24 03:35:38 +0000 | [diff] [blame] | 1409 | CodeGenFunction::CGCapturedStmtRAII CapInfoRAII(CGF, &CGInfo); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 1410 | llvm::Value *Res = CGF.GenerateCapturedStmtFunction(*CS); |
Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 1411 | if (!Tied) |
| 1412 | NumberOfParts = Action.getNumberOfParts(); |
| 1413 | return Res; |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 1414 | } |
| 1415 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 1416 | static void buildStructValue(ConstantStructBuilder &Fields, CodeGenModule &CGM, |
| 1417 | const RecordDecl *RD, const CGRecordLayout &RL, |
| 1418 | ArrayRef<llvm::Constant *> Data) { |
| 1419 | llvm::StructType *StructTy = RL.getLLVMType(); |
| 1420 | unsigned PrevIdx = 0; |
| 1421 | ConstantInitBuilder CIBuilder(CGM); |
| 1422 | auto DI = Data.begin(); |
| 1423 | for (const FieldDecl *FD : RD->fields()) { |
| 1424 | unsigned Idx = RL.getLLVMFieldNo(FD); |
| 1425 | // Fill the alignment. |
| 1426 | for (unsigned I = PrevIdx; I < Idx; ++I) |
| 1427 | Fields.add(llvm::Constant::getNullValue(StructTy->getElementType(I))); |
| 1428 | PrevIdx = Idx + 1; |
| 1429 | Fields.add(*DI); |
| 1430 | ++DI; |
| 1431 | } |
| 1432 | } |
| 1433 | |
| 1434 | template <class... As> |
| 1435 | static llvm::GlobalVariable * |
| 1436 | createConstantGlobalStruct(CodeGenModule &CGM, QualType Ty, |
| 1437 | ArrayRef<llvm::Constant *> Data, const Twine &Name, |
| 1438 | As &&... Args) { |
| 1439 | const auto *RD = cast<RecordDecl>(Ty->getAsTagDecl()); |
| 1440 | const CGRecordLayout &RL = CGM.getTypes().getCGRecordLayout(RD); |
| 1441 | ConstantInitBuilder CIBuilder(CGM); |
| 1442 | ConstantStructBuilder Fields = CIBuilder.beginStruct(RL.getLLVMType()); |
| 1443 | buildStructValue(Fields, CGM, RD, RL, Data); |
| 1444 | return Fields.finishAndCreateGlobal( |
| 1445 | Name, CGM.getContext().getAlignOfGlobalVarInChars(Ty), |
| 1446 | /*isConstant=*/true, std::forward<As>(Args)...); |
| 1447 | } |
| 1448 | |
| 1449 | template <typename T> |
| 1450 | void createConstantGlobalStructAndAddToParent(CodeGenModule &CGM, QualType Ty, |
| 1451 | ArrayRef<llvm::Constant *> Data, |
| 1452 | T &Parent) { |
| 1453 | const auto *RD = cast<RecordDecl>(Ty->getAsTagDecl()); |
| 1454 | const CGRecordLayout &RL = CGM.getTypes().getCGRecordLayout(RD); |
| 1455 | ConstantStructBuilder Fields = Parent.beginStruct(RL.getLLVMType()); |
| 1456 | buildStructValue(Fields, CGM, RD, RL, Data); |
| 1457 | Fields.finishAndAddTo(Parent); |
| 1458 | } |
| 1459 | |
Alexey Bataev | 50b3c95 | 2016-02-19 10:38:26 +0000 | [diff] [blame] | 1460 | Address CGOpenMPRuntime::getOrCreateDefaultLocation(unsigned Flags) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 1461 | CharUnits Align = CGM.getContext().getTypeAlignInChars(IdentQTy); |
Alexey Bataev | 15007ba | 2014-05-07 06:18:01 +0000 | [diff] [blame] | 1462 | llvm::Value *Entry = OpenMPDefaultLocMap.lookup(Flags); |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1463 | if (!Entry) { |
| 1464 | if (!DefaultOpenMPPSource) { |
| 1465 | // Initialize default location for psource field of ident_t structure of |
| 1466 | // all ident_t objects. Format is ";file;function;line;column;;". |
| 1467 | // Taken from |
| 1468 | // http://llvm.org/svn/llvm-project/openmp/trunk/runtime/src/kmp_str.c |
| 1469 | DefaultOpenMPPSource = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1470 | CGM.GetAddrOfConstantCString(";unknown;unknown;0;0;;").getPointer(); |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1471 | DefaultOpenMPPSource = |
| 1472 | llvm::ConstantExpr::getBitCast(DefaultOpenMPPSource, CGM.Int8PtrTy); |
| 1473 | } |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1474 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 1475 | llvm::Constant *Data[] = {llvm::ConstantInt::getNullValue(CGM.Int32Ty), |
| 1476 | llvm::ConstantInt::get(CGM.Int32Ty, Flags), |
| 1477 | llvm::ConstantInt::getNullValue(CGM.Int32Ty), |
| 1478 | llvm::ConstantInt::getNullValue(CGM.Int32Ty), |
| 1479 | DefaultOpenMPPSource}; |
| 1480 | llvm::GlobalValue *DefaultOpenMPLocation = createConstantGlobalStruct( |
| 1481 | CGM, IdentQTy, Data, "", llvm::GlobalValue::PrivateLinkage); |
| 1482 | DefaultOpenMPLocation->setUnnamedAddr( |
| 1483 | llvm::GlobalValue::UnnamedAddr::Global); |
John McCall | 6c9f1fdb | 2016-11-19 08:17:24 +0000 | [diff] [blame] | 1484 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1485 | OpenMPDefaultLocMap[Flags] = Entry = DefaultOpenMPLocation; |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1486 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1487 | return Address(Entry, Align); |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1488 | } |
| 1489 | |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 1490 | llvm::Value *CGOpenMPRuntime::emitUpdateLocation(CodeGenFunction &CGF, |
| 1491 | SourceLocation Loc, |
Alexey Bataev | 50b3c95 | 2016-02-19 10:38:26 +0000 | [diff] [blame] | 1492 | unsigned Flags) { |
| 1493 | Flags |= OMP_IDENT_KMPC; |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1494 | // If no debug info is generated - return global default location. |
Benjamin Kramer | 8c30592 | 2016-02-02 11:06:51 +0000 | [diff] [blame] | 1495 | if (CGM.getCodeGenOpts().getDebugInfo() == codegenoptions::NoDebugInfo || |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1496 | Loc.isInvalid()) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1497 | return getOrCreateDefaultLocation(Flags).getPointer(); |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1498 | |
| 1499 | assert(CGF.CurFn && "No function in current CodeGenFunction."); |
| 1500 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 1501 | CharUnits Align = CGM.getContext().getTypeAlignInChars(IdentQTy); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1502 | Address LocValue = Address::invalid(); |
Alexey Bataev | 1e4b713 | 2014-12-03 12:11:24 +0000 | [diff] [blame] | 1503 | auto I = OpenMPLocThreadIDMap.find(CGF.CurFn); |
| 1504 | if (I != OpenMPLocThreadIDMap.end()) |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 1505 | LocValue = Address(I->second.DebugLoc, Align); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1506 | |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 1507 | // OpenMPLocThreadIDMap may have null DebugLoc and non-null ThreadID, if |
| 1508 | // GetOpenMPThreadID was called before this routine. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1509 | if (!LocValue.isValid()) { |
Alexey Bataev | 15007ba | 2014-05-07 06:18:01 +0000 | [diff] [blame] | 1510 | // Generate "ident_t .kmpc_loc.addr;" |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 1511 | Address AI = CGF.CreateMemTemp(IdentQTy, ".kmpc_loc.addr"); |
Alexey Bataev | 1809571 | 2014-10-10 12:19:54 +0000 | [diff] [blame] | 1512 | auto &Elem = OpenMPLocThreadIDMap.FindAndConstruct(CGF.CurFn); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1513 | Elem.second.DebugLoc = AI.getPointer(); |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1514 | LocValue = AI; |
| 1515 | |
| 1516 | CGBuilderTy::InsertPointGuard IPG(CGF.Builder); |
| 1517 | CGF.Builder.SetInsertPoint(CGF.AllocaInsertPt); |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 1518 | CGF.Builder.CreateMemCpy(LocValue, getOrCreateDefaultLocation(Flags), |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 1519 | CGF.getTypeSize(IdentQTy)); |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1520 | } |
| 1521 | |
| 1522 | // char **psource = &.kmpc_loc_<flags>.addr.psource; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 1523 | LValue Base = CGF.MakeAddrLValue(LocValue, IdentQTy); |
| 1524 | auto Fields = cast<RecordDecl>(IdentQTy->getAsTagDecl())->field_begin(); |
| 1525 | LValue PSource = |
| 1526 | CGF.EmitLValueForField(Base, *std::next(Fields, IdentField_PSource)); |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1527 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 1528 | llvm::Value *OMPDebugLoc = OpenMPDebugLocMap.lookup(Loc.getRawEncoding()); |
Alexey Bataev | f002aca | 2014-05-30 05:48:40 +0000 | [diff] [blame] | 1529 | if (OMPDebugLoc == nullptr) { |
| 1530 | SmallString<128> Buffer2; |
| 1531 | llvm::raw_svector_ostream OS2(Buffer2); |
| 1532 | // Build debug location |
| 1533 | PresumedLoc PLoc = CGF.getContext().getSourceManager().getPresumedLoc(Loc); |
| 1534 | OS2 << ";" << PLoc.getFilename() << ";"; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 1535 | if (const auto *FD = dyn_cast_or_null<FunctionDecl>(CGF.CurFuncDecl)) |
Alexey Bataev | f002aca | 2014-05-30 05:48:40 +0000 | [diff] [blame] | 1536 | OS2 << FD->getQualifiedNameAsString(); |
Alexey Bataev | f002aca | 2014-05-30 05:48:40 +0000 | [diff] [blame] | 1537 | OS2 << ";" << PLoc.getLine() << ";" << PLoc.getColumn() << ";;"; |
| 1538 | OMPDebugLoc = CGF.Builder.CreateGlobalStringPtr(OS2.str()); |
| 1539 | OpenMPDebugLocMap[Loc.getRawEncoding()] = OMPDebugLoc; |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1540 | } |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1541 | // *psource = ";<File>;<Function>;<Line>;<Column>;;"; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 1542 | CGF.EmitStoreOfScalar(OMPDebugLoc, PSource); |
Alexey Bataev | f002aca | 2014-05-30 05:48:40 +0000 | [diff] [blame] | 1543 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1544 | // Our callers always pass this to a runtime function, so for |
| 1545 | // convenience, go ahead and return a naked pointer. |
| 1546 | return LocValue.getPointer(); |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1547 | } |
| 1548 | |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 1549 | llvm::Value *CGOpenMPRuntime::getThreadID(CodeGenFunction &CGF, |
| 1550 | SourceLocation Loc) { |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1551 | assert(CGF.CurFn && "No function in current CodeGenFunction."); |
| 1552 | |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 1553 | llvm::Value *ThreadID = nullptr; |
Alexey Bataev | 1809571 | 2014-10-10 12:19:54 +0000 | [diff] [blame] | 1554 | // Check whether we've already cached a load of the thread id in this |
| 1555 | // function. |
Alexey Bataev | 1e4b713 | 2014-12-03 12:11:24 +0000 | [diff] [blame] | 1556 | auto I = OpenMPLocThreadIDMap.find(CGF.CurFn); |
Alexey Bataev | 1809571 | 2014-10-10 12:19:54 +0000 | [diff] [blame] | 1557 | if (I != OpenMPLocThreadIDMap.end()) { |
| 1558 | ThreadID = I->second.ThreadID; |
Alexey Bataev | 03b340a | 2014-10-21 03:16:40 +0000 | [diff] [blame] | 1559 | if (ThreadID != nullptr) |
| 1560 | return ThreadID; |
| 1561 | } |
Alexey Bataev | aee1855 | 2017-08-16 14:01:00 +0000 | [diff] [blame] | 1562 | // If exceptions are enabled, do not use parameter to avoid possible crash. |
Alexey Bataev | 5d2c9a4 | 2017-11-02 18:55:05 +0000 | [diff] [blame] | 1563 | if (!CGF.EHStack.requiresLandingPad() || !CGF.getLangOpts().Exceptions || |
| 1564 | !CGF.getLangOpts().CXXExceptions || |
Alexey Bataev | 0e1b458 | 2017-11-02 14:25:34 +0000 | [diff] [blame] | 1565 | CGF.Builder.GetInsertBlock() == CGF.AllocaInsertPt->getParent()) { |
Alexey Bataev | aee1855 | 2017-08-16 14:01:00 +0000 | [diff] [blame] | 1566 | if (auto *OMPRegionInfo = |
| 1567 | dyn_cast_or_null<CGOpenMPRegionInfo>(CGF.CapturedStmtInfo)) { |
| 1568 | if (OMPRegionInfo->getThreadIDVariable()) { |
| 1569 | // Check if this an outlined function with thread id passed as argument. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 1570 | LValue LVal = OMPRegionInfo->getThreadIDVariableLValue(CGF); |
Alexey Bataev | 1e49137 | 2018-01-23 18:44:14 +0000 | [diff] [blame] | 1571 | ThreadID = CGF.EmitLoadOfScalar(LVal, Loc); |
Alexey Bataev | aee1855 | 2017-08-16 14:01:00 +0000 | [diff] [blame] | 1572 | // If value loaded in entry block, cache it and use it everywhere in |
| 1573 | // function. |
| 1574 | if (CGF.Builder.GetInsertBlock() == CGF.AllocaInsertPt->getParent()) { |
| 1575 | auto &Elem = OpenMPLocThreadIDMap.FindAndConstruct(CGF.CurFn); |
| 1576 | Elem.second.ThreadID = ThreadID; |
| 1577 | } |
| 1578 | return ThreadID; |
Alexey Bataev | 8cbe0a6 | 2015-02-26 10:27:34 +0000 | [diff] [blame] | 1579 | } |
Alexey Bataev | d6c5755 | 2014-07-25 07:55:17 +0000 | [diff] [blame] | 1580 | } |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1581 | } |
Alexey Bataev | 8cbe0a6 | 2015-02-26 10:27:34 +0000 | [diff] [blame] | 1582 | |
| 1583 | // This is not an outlined function region - need to call __kmpc_int32 |
| 1584 | // kmpc_global_thread_num(ident_t *loc). |
| 1585 | // Generate thread id value and cache this value for use across the |
| 1586 | // function. |
| 1587 | CGBuilderTy::InsertPointGuard IPG(CGF.Builder); |
| 1588 | CGF.Builder.SetInsertPoint(CGF.AllocaInsertPt); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 1589 | llvm::CallInst *Call = CGF.Builder.CreateCall( |
Alexey Bataev | 0e1b458 | 2017-11-02 14:25:34 +0000 | [diff] [blame] | 1590 | createRuntimeFunction(OMPRTL__kmpc_global_thread_num), |
| 1591 | emitUpdateLocation(CGF, Loc)); |
| 1592 | Call->setCallingConv(CGF.getRuntimeCC()); |
Alexey Bataev | 8cbe0a6 | 2015-02-26 10:27:34 +0000 | [diff] [blame] | 1593 | auto &Elem = OpenMPLocThreadIDMap.FindAndConstruct(CGF.CurFn); |
Alexey Bataev | 0e1b458 | 2017-11-02 14:25:34 +0000 | [diff] [blame] | 1594 | Elem.second.ThreadID = Call; |
| 1595 | return Call; |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1596 | } |
| 1597 | |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 1598 | void CGOpenMPRuntime::functionFinished(CodeGenFunction &CGF) { |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1599 | assert(CGF.CurFn && "No function in current CodeGenFunction."); |
Alexey Bataev | 03b340a | 2014-10-21 03:16:40 +0000 | [diff] [blame] | 1600 | if (OpenMPLocThreadIDMap.count(CGF.CurFn)) |
| 1601 | OpenMPLocThreadIDMap.erase(CGF.CurFn); |
Alexey Bataev | c5b1d32 | 2016-03-04 09:22:22 +0000 | [diff] [blame] | 1602 | if (FunctionUDRMap.count(CGF.CurFn) > 0) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 1603 | for(auto *D : FunctionUDRMap[CGF.CurFn]) |
Alexey Bataev | c5b1d32 | 2016-03-04 09:22:22 +0000 | [diff] [blame] | 1604 | UDRMap.erase(D); |
Alexey Bataev | c5b1d32 | 2016-03-04 09:22:22 +0000 | [diff] [blame] | 1605 | FunctionUDRMap.erase(CGF.CurFn); |
| 1606 | } |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1607 | } |
| 1608 | |
| 1609 | llvm::Type *CGOpenMPRuntime::getIdentTyPointerTy() { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 1610 | return IdentTy->getPointerTo(); |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1611 | } |
| 1612 | |
| 1613 | llvm::Type *CGOpenMPRuntime::getKmpc_MicroPointerTy() { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 1614 | if (!Kmpc_MicroTy) { |
| 1615 | // Build void (*kmpc_micro)(kmp_int32 *global_tid, kmp_int32 *bound_tid,...) |
| 1616 | llvm::Type *MicroParams[] = {llvm::PointerType::getUnqual(CGM.Int32Ty), |
| 1617 | llvm::PointerType::getUnqual(CGM.Int32Ty)}; |
| 1618 | Kmpc_MicroTy = llvm::FunctionType::get(CGM.VoidTy, MicroParams, true); |
| 1619 | } |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1620 | return llvm::PointerType::getUnqual(Kmpc_MicroTy); |
| 1621 | } |
| 1622 | |
| 1623 | llvm::Constant * |
Alexey Bataev | 50b3c95 | 2016-02-19 10:38:26 +0000 | [diff] [blame] | 1624 | CGOpenMPRuntime::createRuntimeFunction(unsigned Function) { |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1625 | llvm::Constant *RTLFn = nullptr; |
Alexey Bataev | 50b3c95 | 2016-02-19 10:38:26 +0000 | [diff] [blame] | 1626 | switch (static_cast<OpenMPRTLFunction>(Function)) { |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1627 | case OMPRTL__kmpc_fork_call: { |
| 1628 | // Build void __kmpc_fork_call(ident_t *loc, kmp_int32 argc, kmpc_micro |
| 1629 | // microtask, ...); |
Alexey Bataev | 23b6942 | 2014-06-18 07:08:49 +0000 | [diff] [blame] | 1630 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty, |
| 1631 | getKmpc_MicroPointerTy()}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 1632 | auto *FnTy = |
Alexey Bataev | d74d060 | 2014-10-13 06:02:40 +0000 | [diff] [blame] | 1633 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ true); |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1634 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_fork_call"); |
| 1635 | break; |
| 1636 | } |
| 1637 | case OMPRTL__kmpc_global_thread_num: { |
| 1638 | // Build kmp_int32 __kmpc_global_thread_num(ident_t *loc); |
Alexey Bataev | 23b6942 | 2014-06-18 07:08:49 +0000 | [diff] [blame] | 1639 | llvm::Type *TypeParams[] = {getIdentTyPointerTy()}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 1640 | auto *FnTy = |
Alexey Bataev | d74d060 | 2014-10-13 06:02:40 +0000 | [diff] [blame] | 1641 | llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg*/ false); |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1642 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_global_thread_num"); |
| 1643 | break; |
| 1644 | } |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 1645 | case OMPRTL__kmpc_threadprivate_cached: { |
| 1646 | // Build void *__kmpc_threadprivate_cached(ident_t *loc, |
| 1647 | // kmp_int32 global_tid, void *data, size_t size, void ***cache); |
| 1648 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty, |
| 1649 | CGM.VoidPtrTy, CGM.SizeTy, |
| 1650 | CGM.VoidPtrTy->getPointerTo()->getPointerTo()}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 1651 | auto *FnTy = |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 1652 | llvm::FunctionType::get(CGM.VoidPtrTy, TypeParams, /*isVarArg*/ false); |
| 1653 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_threadprivate_cached"); |
| 1654 | break; |
| 1655 | } |
Alexey Bataev | 3a3bf0b | 2014-09-22 10:01:53 +0000 | [diff] [blame] | 1656 | case OMPRTL__kmpc_critical: { |
Alexey Bataev | f947218 | 2014-09-22 12:32:31 +0000 | [diff] [blame] | 1657 | // Build void __kmpc_critical(ident_t *loc, kmp_int32 global_tid, |
| 1658 | // kmp_critical_name *crit); |
Alexey Bataev | 3a3bf0b | 2014-09-22 10:01:53 +0000 | [diff] [blame] | 1659 | llvm::Type *TypeParams[] = { |
| 1660 | getIdentTyPointerTy(), CGM.Int32Ty, |
| 1661 | llvm::PointerType::getUnqual(KmpCriticalNameTy)}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 1662 | auto *FnTy = |
Alexey Bataev | 3a3bf0b | 2014-09-22 10:01:53 +0000 | [diff] [blame] | 1663 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false); |
| 1664 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_critical"); |
| 1665 | break; |
| 1666 | } |
Alexey Bataev | fc57d16 | 2015-12-15 10:55:09 +0000 | [diff] [blame] | 1667 | case OMPRTL__kmpc_critical_with_hint: { |
| 1668 | // Build void __kmpc_critical_with_hint(ident_t *loc, kmp_int32 global_tid, |
| 1669 | // kmp_critical_name *crit, uintptr_t hint); |
| 1670 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty, |
| 1671 | llvm::PointerType::getUnqual(KmpCriticalNameTy), |
| 1672 | CGM.IntPtrTy}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 1673 | auto *FnTy = |
Alexey Bataev | fc57d16 | 2015-12-15 10:55:09 +0000 | [diff] [blame] | 1674 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false); |
| 1675 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_critical_with_hint"); |
| 1676 | break; |
| 1677 | } |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 1678 | case OMPRTL__kmpc_threadprivate_register: { |
| 1679 | // Build void __kmpc_threadprivate_register(ident_t *, void *data, |
| 1680 | // kmpc_ctor ctor, kmpc_cctor cctor, kmpc_dtor dtor); |
| 1681 | // typedef void *(*kmpc_ctor)(void *); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 1682 | auto *KmpcCtorTy = |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 1683 | llvm::FunctionType::get(CGM.VoidPtrTy, CGM.VoidPtrTy, |
| 1684 | /*isVarArg*/ false)->getPointerTo(); |
| 1685 | // typedef void *(*kmpc_cctor)(void *, void *); |
| 1686 | llvm::Type *KmpcCopyCtorTyArgs[] = {CGM.VoidPtrTy, CGM.VoidPtrTy}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 1687 | auto *KmpcCopyCtorTy = |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 1688 | llvm::FunctionType::get(CGM.VoidPtrTy, KmpcCopyCtorTyArgs, |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 1689 | /*isVarArg*/ false) |
| 1690 | ->getPointerTo(); |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 1691 | // typedef void (*kmpc_dtor)(void *); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 1692 | auto *KmpcDtorTy = |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 1693 | llvm::FunctionType::get(CGM.VoidTy, CGM.VoidPtrTy, /*isVarArg*/ false) |
| 1694 | ->getPointerTo(); |
| 1695 | llvm::Type *FnTyArgs[] = {getIdentTyPointerTy(), CGM.VoidPtrTy, KmpcCtorTy, |
| 1696 | KmpcCopyCtorTy, KmpcDtorTy}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 1697 | auto *FnTy = llvm::FunctionType::get(CGM.VoidTy, FnTyArgs, |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 1698 | /*isVarArg*/ false); |
| 1699 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_threadprivate_register"); |
| 1700 | break; |
| 1701 | } |
Alexey Bataev | 3a3bf0b | 2014-09-22 10:01:53 +0000 | [diff] [blame] | 1702 | case OMPRTL__kmpc_end_critical: { |
Alexey Bataev | f947218 | 2014-09-22 12:32:31 +0000 | [diff] [blame] | 1703 | // Build void __kmpc_end_critical(ident_t *loc, kmp_int32 global_tid, |
| 1704 | // kmp_critical_name *crit); |
Alexey Bataev | 3a3bf0b | 2014-09-22 10:01:53 +0000 | [diff] [blame] | 1705 | llvm::Type *TypeParams[] = { |
| 1706 | getIdentTyPointerTy(), CGM.Int32Ty, |
| 1707 | llvm::PointerType::getUnqual(KmpCriticalNameTy)}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 1708 | auto *FnTy = |
Alexey Bataev | 3a3bf0b | 2014-09-22 10:01:53 +0000 | [diff] [blame] | 1709 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false); |
| 1710 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_end_critical"); |
| 1711 | break; |
| 1712 | } |
Alexey Bataev | 8f7c1b0 | 2014-12-05 04:09:23 +0000 | [diff] [blame] | 1713 | case OMPRTL__kmpc_cancel_barrier: { |
| 1714 | // Build kmp_int32 __kmpc_cancel_barrier(ident_t *loc, kmp_int32 |
| 1715 | // global_tid); |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 1716 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 1717 | auto *FnTy = |
Alexey Bataev | 8f7c1b0 | 2014-12-05 04:09:23 +0000 | [diff] [blame] | 1718 | llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg*/ false); |
| 1719 | RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name*/ "__kmpc_cancel_barrier"); |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 1720 | break; |
| 1721 | } |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 1722 | case OMPRTL__kmpc_barrier: { |
Alexey Bataev | 7d5d33e | 2015-07-06 05:50:32 +0000 | [diff] [blame] | 1723 | // Build void __kmpc_barrier(ident_t *loc, kmp_int32 global_tid); |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 1724 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 1725 | auto *FnTy = |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 1726 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false); |
| 1727 | RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name*/ "__kmpc_barrier"); |
| 1728 | break; |
| 1729 | } |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 1730 | case OMPRTL__kmpc_for_static_fini: { |
| 1731 | // Build void __kmpc_for_static_fini(ident_t *loc, kmp_int32 global_tid); |
| 1732 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 1733 | auto *FnTy = |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 1734 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false); |
| 1735 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_for_static_fini"); |
| 1736 | break; |
| 1737 | } |
Alexey Bataev | b205978 | 2014-10-13 08:23:51 +0000 | [diff] [blame] | 1738 | case OMPRTL__kmpc_push_num_threads: { |
| 1739 | // Build void __kmpc_push_num_threads(ident_t *loc, kmp_int32 global_tid, |
| 1740 | // kmp_int32 num_threads) |
| 1741 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty, |
| 1742 | CGM.Int32Ty}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 1743 | auto *FnTy = |
Alexey Bataev | b205978 | 2014-10-13 08:23:51 +0000 | [diff] [blame] | 1744 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false); |
| 1745 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_push_num_threads"); |
| 1746 | break; |
| 1747 | } |
Alexey Bataev | d74d060 | 2014-10-13 06:02:40 +0000 | [diff] [blame] | 1748 | case OMPRTL__kmpc_serialized_parallel: { |
| 1749 | // Build void __kmpc_serialized_parallel(ident_t *loc, kmp_int32 |
| 1750 | // global_tid); |
| 1751 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 1752 | auto *FnTy = |
Alexey Bataev | d74d060 | 2014-10-13 06:02:40 +0000 | [diff] [blame] | 1753 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false); |
| 1754 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_serialized_parallel"); |
| 1755 | break; |
| 1756 | } |
| 1757 | case OMPRTL__kmpc_end_serialized_parallel: { |
| 1758 | // Build void __kmpc_end_serialized_parallel(ident_t *loc, kmp_int32 |
| 1759 | // global_tid); |
| 1760 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 1761 | auto *FnTy = |
Alexey Bataev | d74d060 | 2014-10-13 06:02:40 +0000 | [diff] [blame] | 1762 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false); |
| 1763 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_end_serialized_parallel"); |
| 1764 | break; |
| 1765 | } |
Alexey Bataev | cc37cc1 | 2014-11-20 04:34:54 +0000 | [diff] [blame] | 1766 | case OMPRTL__kmpc_flush: { |
Alexey Bataev | d76df6d | 2015-02-24 12:55:09 +0000 | [diff] [blame] | 1767 | // Build void __kmpc_flush(ident_t *loc); |
Alexey Bataev | cc37cc1 | 2014-11-20 04:34:54 +0000 | [diff] [blame] | 1768 | llvm::Type *TypeParams[] = {getIdentTyPointerTy()}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 1769 | auto *FnTy = |
Alexey Bataev | d76df6d | 2015-02-24 12:55:09 +0000 | [diff] [blame] | 1770 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false); |
Alexey Bataev | cc37cc1 | 2014-11-20 04:34:54 +0000 | [diff] [blame] | 1771 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_flush"); |
| 1772 | break; |
| 1773 | } |
Alexey Bataev | 8d69065 | 2014-12-04 07:23:53 +0000 | [diff] [blame] | 1774 | case OMPRTL__kmpc_master: { |
| 1775 | // Build kmp_int32 __kmpc_master(ident_t *loc, kmp_int32 global_tid); |
| 1776 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 1777 | auto *FnTy = |
Alexey Bataev | 8d69065 | 2014-12-04 07:23:53 +0000 | [diff] [blame] | 1778 | llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg=*/false); |
| 1779 | RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_master"); |
| 1780 | break; |
| 1781 | } |
| 1782 | case OMPRTL__kmpc_end_master: { |
| 1783 | // Build void __kmpc_end_master(ident_t *loc, kmp_int32 global_tid); |
| 1784 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 1785 | auto *FnTy = |
Alexey Bataev | 8d69065 | 2014-12-04 07:23:53 +0000 | [diff] [blame] | 1786 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false); |
| 1787 | RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_end_master"); |
| 1788 | break; |
| 1789 | } |
Alexey Bataev | 9f797f3 | 2015-02-05 05:57:51 +0000 | [diff] [blame] | 1790 | case OMPRTL__kmpc_omp_taskyield: { |
| 1791 | // Build kmp_int32 __kmpc_omp_taskyield(ident_t *, kmp_int32 global_tid, |
| 1792 | // int end_part); |
| 1793 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty, CGM.IntTy}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 1794 | auto *FnTy = |
Alexey Bataev | 9f797f3 | 2015-02-05 05:57:51 +0000 | [diff] [blame] | 1795 | llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg=*/false); |
| 1796 | RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_omp_taskyield"); |
| 1797 | break; |
| 1798 | } |
Alexey Bataev | 6956e2e | 2015-02-05 06:35:41 +0000 | [diff] [blame] | 1799 | case OMPRTL__kmpc_single: { |
| 1800 | // Build kmp_int32 __kmpc_single(ident_t *loc, kmp_int32 global_tid); |
| 1801 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 1802 | auto *FnTy = |
Alexey Bataev | 6956e2e | 2015-02-05 06:35:41 +0000 | [diff] [blame] | 1803 | llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg=*/false); |
| 1804 | RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_single"); |
| 1805 | break; |
| 1806 | } |
| 1807 | case OMPRTL__kmpc_end_single: { |
| 1808 | // Build void __kmpc_end_single(ident_t *loc, kmp_int32 global_tid); |
| 1809 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 1810 | auto *FnTy = |
Alexey Bataev | 6956e2e | 2015-02-05 06:35:41 +0000 | [diff] [blame] | 1811 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false); |
| 1812 | RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_end_single"); |
| 1813 | break; |
| 1814 | } |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 1815 | case OMPRTL__kmpc_omp_task_alloc: { |
| 1816 | // Build kmp_task_t *__kmpc_omp_task_alloc(ident_t *, kmp_int32 gtid, |
| 1817 | // kmp_int32 flags, size_t sizeof_kmp_task_t, size_t sizeof_shareds, |
| 1818 | // kmp_routine_entry_t *task_entry); |
| 1819 | assert(KmpRoutineEntryPtrTy != nullptr && |
| 1820 | "Type kmp_routine_entry_t must be created."); |
| 1821 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty, CGM.Int32Ty, |
| 1822 | CGM.SizeTy, CGM.SizeTy, KmpRoutineEntryPtrTy}; |
| 1823 | // Return void * and then cast to particular kmp_task_t type. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 1824 | auto *FnTy = |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 1825 | llvm::FunctionType::get(CGM.VoidPtrTy, TypeParams, /*isVarArg=*/false); |
| 1826 | RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_omp_task_alloc"); |
| 1827 | break; |
| 1828 | } |
| 1829 | case OMPRTL__kmpc_omp_task: { |
| 1830 | // Build kmp_int32 __kmpc_omp_task(ident_t *, kmp_int32 gtid, kmp_task_t |
| 1831 | // *new_task); |
| 1832 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty, |
| 1833 | CGM.VoidPtrTy}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 1834 | auto *FnTy = |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 1835 | llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg=*/false); |
| 1836 | RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_omp_task"); |
| 1837 | break; |
| 1838 | } |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 1839 | case OMPRTL__kmpc_copyprivate: { |
| 1840 | // Build void __kmpc_copyprivate(ident_t *loc, kmp_int32 global_tid, |
Alexey Bataev | 66beaa9 | 2015-04-30 03:47:32 +0000 | [diff] [blame] | 1841 | // size_t cpy_size, void *cpy_data, void(*cpy_func)(void *, void *), |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 1842 | // kmp_int32 didit); |
| 1843 | llvm::Type *CpyTypeParams[] = {CGM.VoidPtrTy, CGM.VoidPtrTy}; |
| 1844 | auto *CpyFnTy = |
| 1845 | llvm::FunctionType::get(CGM.VoidTy, CpyTypeParams, /*isVarArg=*/false); |
Alexey Bataev | 66beaa9 | 2015-04-30 03:47:32 +0000 | [diff] [blame] | 1846 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty, CGM.SizeTy, |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 1847 | CGM.VoidPtrTy, CpyFnTy->getPointerTo(), |
| 1848 | CGM.Int32Ty}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 1849 | auto *FnTy = |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 1850 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false); |
| 1851 | RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_copyprivate"); |
| 1852 | break; |
| 1853 | } |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 1854 | case OMPRTL__kmpc_reduce: { |
| 1855 | // Build kmp_int32 __kmpc_reduce(ident_t *loc, kmp_int32 global_tid, |
| 1856 | // kmp_int32 num_vars, size_t reduce_size, void *reduce_data, void |
| 1857 | // (*reduce_func)(void *lhs_data, void *rhs_data), kmp_critical_name *lck); |
| 1858 | llvm::Type *ReduceTypeParams[] = {CGM.VoidPtrTy, CGM.VoidPtrTy}; |
| 1859 | auto *ReduceFnTy = llvm::FunctionType::get(CGM.VoidTy, ReduceTypeParams, |
| 1860 | /*isVarArg=*/false); |
| 1861 | llvm::Type *TypeParams[] = { |
| 1862 | getIdentTyPointerTy(), CGM.Int32Ty, CGM.Int32Ty, CGM.SizeTy, |
| 1863 | CGM.VoidPtrTy, ReduceFnTy->getPointerTo(), |
| 1864 | llvm::PointerType::getUnqual(KmpCriticalNameTy)}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 1865 | auto *FnTy = |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 1866 | llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg=*/false); |
| 1867 | RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_reduce"); |
| 1868 | break; |
| 1869 | } |
| 1870 | case OMPRTL__kmpc_reduce_nowait: { |
| 1871 | // Build kmp_int32 __kmpc_reduce_nowait(ident_t *loc, kmp_int32 |
| 1872 | // global_tid, kmp_int32 num_vars, size_t reduce_size, void *reduce_data, |
| 1873 | // void (*reduce_func)(void *lhs_data, void *rhs_data), kmp_critical_name |
| 1874 | // *lck); |
| 1875 | llvm::Type *ReduceTypeParams[] = {CGM.VoidPtrTy, CGM.VoidPtrTy}; |
| 1876 | auto *ReduceFnTy = llvm::FunctionType::get(CGM.VoidTy, ReduceTypeParams, |
| 1877 | /*isVarArg=*/false); |
| 1878 | llvm::Type *TypeParams[] = { |
| 1879 | getIdentTyPointerTy(), CGM.Int32Ty, CGM.Int32Ty, CGM.SizeTy, |
| 1880 | CGM.VoidPtrTy, ReduceFnTy->getPointerTo(), |
| 1881 | llvm::PointerType::getUnqual(KmpCriticalNameTy)}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 1882 | auto *FnTy = |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 1883 | llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg=*/false); |
| 1884 | RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_reduce_nowait"); |
| 1885 | break; |
| 1886 | } |
| 1887 | case OMPRTL__kmpc_end_reduce: { |
| 1888 | // Build void __kmpc_end_reduce(ident_t *loc, kmp_int32 global_tid, |
| 1889 | // kmp_critical_name *lck); |
| 1890 | llvm::Type *TypeParams[] = { |
| 1891 | getIdentTyPointerTy(), CGM.Int32Ty, |
| 1892 | llvm::PointerType::getUnqual(KmpCriticalNameTy)}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 1893 | auto *FnTy = |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 1894 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false); |
| 1895 | RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_end_reduce"); |
| 1896 | break; |
| 1897 | } |
| 1898 | case OMPRTL__kmpc_end_reduce_nowait: { |
| 1899 | // Build __kmpc_end_reduce_nowait(ident_t *loc, kmp_int32 global_tid, |
| 1900 | // kmp_critical_name *lck); |
| 1901 | llvm::Type *TypeParams[] = { |
| 1902 | getIdentTyPointerTy(), CGM.Int32Ty, |
| 1903 | llvm::PointerType::getUnqual(KmpCriticalNameTy)}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 1904 | auto *FnTy = |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 1905 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false); |
| 1906 | RTLFn = |
| 1907 | CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_end_reduce_nowait"); |
| 1908 | break; |
| 1909 | } |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 1910 | case OMPRTL__kmpc_omp_task_begin_if0: { |
| 1911 | // Build void __kmpc_omp_task(ident_t *, kmp_int32 gtid, kmp_task_t |
| 1912 | // *new_task); |
| 1913 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty, |
| 1914 | CGM.VoidPtrTy}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 1915 | auto *FnTy = |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 1916 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false); |
| 1917 | RTLFn = |
| 1918 | CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_omp_task_begin_if0"); |
| 1919 | break; |
| 1920 | } |
| 1921 | case OMPRTL__kmpc_omp_task_complete_if0: { |
| 1922 | // Build void __kmpc_omp_task(ident_t *, kmp_int32 gtid, kmp_task_t |
| 1923 | // *new_task); |
| 1924 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty, |
| 1925 | CGM.VoidPtrTy}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 1926 | auto *FnTy = |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 1927 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false); |
| 1928 | RTLFn = CGM.CreateRuntimeFunction(FnTy, |
| 1929 | /*Name=*/"__kmpc_omp_task_complete_if0"); |
| 1930 | break; |
| 1931 | } |
Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 1932 | case OMPRTL__kmpc_ordered: { |
| 1933 | // Build void __kmpc_ordered(ident_t *loc, kmp_int32 global_tid); |
| 1934 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 1935 | auto *FnTy = |
Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 1936 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false); |
| 1937 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_ordered"); |
| 1938 | break; |
| 1939 | } |
| 1940 | case OMPRTL__kmpc_end_ordered: { |
Alexey Bataev | c30dd2d | 2015-06-18 12:14:09 +0000 | [diff] [blame] | 1941 | // Build void __kmpc_end_ordered(ident_t *loc, kmp_int32 global_tid); |
Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 1942 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 1943 | auto *FnTy = |
Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 1944 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false); |
| 1945 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_end_ordered"); |
| 1946 | break; |
| 1947 | } |
Alexey Bataev | 8b8e202 | 2015-04-27 05:22:09 +0000 | [diff] [blame] | 1948 | case OMPRTL__kmpc_omp_taskwait: { |
| 1949 | // Build kmp_int32 __kmpc_omp_taskwait(ident_t *loc, kmp_int32 global_tid); |
| 1950 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 1951 | auto *FnTy = |
Alexey Bataev | 8b8e202 | 2015-04-27 05:22:09 +0000 | [diff] [blame] | 1952 | llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg=*/false); |
| 1953 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_omp_taskwait"); |
| 1954 | break; |
| 1955 | } |
Alexey Bataev | c30dd2d | 2015-06-18 12:14:09 +0000 | [diff] [blame] | 1956 | case OMPRTL__kmpc_taskgroup: { |
| 1957 | // Build void __kmpc_taskgroup(ident_t *loc, kmp_int32 global_tid); |
| 1958 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 1959 | auto *FnTy = |
Alexey Bataev | c30dd2d | 2015-06-18 12:14:09 +0000 | [diff] [blame] | 1960 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false); |
| 1961 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_taskgroup"); |
| 1962 | break; |
| 1963 | } |
| 1964 | case OMPRTL__kmpc_end_taskgroup: { |
| 1965 | // Build void __kmpc_end_taskgroup(ident_t *loc, kmp_int32 global_tid); |
| 1966 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 1967 | auto *FnTy = |
Alexey Bataev | c30dd2d | 2015-06-18 12:14:09 +0000 | [diff] [blame] | 1968 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false); |
| 1969 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_end_taskgroup"); |
| 1970 | break; |
| 1971 | } |
Alexey Bataev | 7f210c6 | 2015-06-18 13:40:03 +0000 | [diff] [blame] | 1972 | case OMPRTL__kmpc_push_proc_bind: { |
| 1973 | // Build void __kmpc_push_proc_bind(ident_t *loc, kmp_int32 global_tid, |
| 1974 | // int proc_bind) |
| 1975 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty, CGM.IntTy}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 1976 | auto *FnTy = |
Alexey Bataev | 7f210c6 | 2015-06-18 13:40:03 +0000 | [diff] [blame] | 1977 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false); |
| 1978 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_push_proc_bind"); |
| 1979 | break; |
| 1980 | } |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 1981 | case OMPRTL__kmpc_omp_task_with_deps: { |
| 1982 | // Build kmp_int32 __kmpc_omp_task_with_deps(ident_t *, kmp_int32 gtid, |
| 1983 | // kmp_task_t *new_task, kmp_int32 ndeps, kmp_depend_info_t *dep_list, |
| 1984 | // kmp_int32 ndeps_noalias, kmp_depend_info_t *noalias_dep_list); |
| 1985 | llvm::Type *TypeParams[] = { |
| 1986 | getIdentTyPointerTy(), CGM.Int32Ty, CGM.VoidPtrTy, CGM.Int32Ty, |
| 1987 | CGM.VoidPtrTy, CGM.Int32Ty, CGM.VoidPtrTy}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 1988 | auto *FnTy = |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 1989 | llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg=*/false); |
| 1990 | RTLFn = |
| 1991 | CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_omp_task_with_deps"); |
| 1992 | break; |
| 1993 | } |
| 1994 | case OMPRTL__kmpc_omp_wait_deps: { |
| 1995 | // Build void __kmpc_omp_wait_deps(ident_t *, kmp_int32 gtid, |
| 1996 | // kmp_int32 ndeps, kmp_depend_info_t *dep_list, kmp_int32 ndeps_noalias, |
| 1997 | // kmp_depend_info_t *noalias_dep_list); |
| 1998 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty, |
| 1999 | CGM.Int32Ty, CGM.VoidPtrTy, |
| 2000 | CGM.Int32Ty, CGM.VoidPtrTy}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 2001 | auto *FnTy = |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 2002 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false); |
| 2003 | RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_omp_wait_deps"); |
| 2004 | break; |
| 2005 | } |
Alexey Bataev | 0f34da1 | 2015-07-02 04:17:07 +0000 | [diff] [blame] | 2006 | case OMPRTL__kmpc_cancellationpoint: { |
| 2007 | // Build kmp_int32 __kmpc_cancellationpoint(ident_t *loc, kmp_int32 |
| 2008 | // global_tid, kmp_int32 cncl_kind) |
| 2009 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty, CGM.IntTy}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 2010 | auto *FnTy = |
Alexey Bataev | 0f34da1 | 2015-07-02 04:17:07 +0000 | [diff] [blame] | 2011 | llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg*/ false); |
| 2012 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_cancellationpoint"); |
| 2013 | break; |
| 2014 | } |
Alexey Bataev | 7d5d33e | 2015-07-06 05:50:32 +0000 | [diff] [blame] | 2015 | case OMPRTL__kmpc_cancel: { |
| 2016 | // Build kmp_int32 __kmpc_cancel(ident_t *loc, kmp_int32 global_tid, |
| 2017 | // kmp_int32 cncl_kind) |
| 2018 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty, CGM.IntTy}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 2019 | auto *FnTy = |
Alexey Bataev | 7d5d33e | 2015-07-06 05:50:32 +0000 | [diff] [blame] | 2020 | llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg*/ false); |
| 2021 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_cancel"); |
| 2022 | break; |
| 2023 | } |
Carlo Bertolli | 430d8ec | 2016-03-03 20:34:23 +0000 | [diff] [blame] | 2024 | case OMPRTL__kmpc_push_num_teams: { |
| 2025 | // Build void kmpc_push_num_teams (ident_t loc, kmp_int32 global_tid, |
| 2026 | // kmp_int32 num_teams, kmp_int32 num_threads) |
| 2027 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty, CGM.Int32Ty, |
| 2028 | CGM.Int32Ty}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 2029 | auto *FnTy = |
Carlo Bertolli | 430d8ec | 2016-03-03 20:34:23 +0000 | [diff] [blame] | 2030 | llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg*/ false); |
| 2031 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_push_num_teams"); |
| 2032 | break; |
| 2033 | } |
| 2034 | case OMPRTL__kmpc_fork_teams: { |
| 2035 | // Build void __kmpc_fork_teams(ident_t *loc, kmp_int32 argc, kmpc_micro |
| 2036 | // microtask, ...); |
| 2037 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty, |
| 2038 | getKmpc_MicroPointerTy()}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 2039 | auto *FnTy = |
Carlo Bertolli | 430d8ec | 2016-03-03 20:34:23 +0000 | [diff] [blame] | 2040 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ true); |
| 2041 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_fork_teams"); |
| 2042 | break; |
| 2043 | } |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 2044 | case OMPRTL__kmpc_taskloop: { |
| 2045 | // Build void __kmpc_taskloop(ident_t *loc, int gtid, kmp_task_t *task, int |
| 2046 | // if_val, kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st, int nogroup, int |
| 2047 | // sched, kmp_uint64 grainsize, void *task_dup); |
| 2048 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), |
| 2049 | CGM.IntTy, |
| 2050 | CGM.VoidPtrTy, |
| 2051 | CGM.IntTy, |
| 2052 | CGM.Int64Ty->getPointerTo(), |
| 2053 | CGM.Int64Ty->getPointerTo(), |
| 2054 | CGM.Int64Ty, |
| 2055 | CGM.IntTy, |
| 2056 | CGM.IntTy, |
| 2057 | CGM.Int64Ty, |
| 2058 | CGM.VoidPtrTy}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 2059 | auto *FnTy = |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 2060 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false); |
| 2061 | RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_taskloop"); |
| 2062 | break; |
| 2063 | } |
Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 2064 | case OMPRTL__kmpc_doacross_init: { |
| 2065 | // Build void __kmpc_doacross_init(ident_t *loc, kmp_int32 gtid, kmp_int32 |
| 2066 | // num_dims, struct kmp_dim *dims); |
| 2067 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), |
| 2068 | CGM.Int32Ty, |
| 2069 | CGM.Int32Ty, |
| 2070 | CGM.VoidPtrTy}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 2071 | auto *FnTy = |
Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 2072 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false); |
| 2073 | RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_doacross_init"); |
| 2074 | break; |
| 2075 | } |
| 2076 | case OMPRTL__kmpc_doacross_fini: { |
| 2077 | // Build void __kmpc_doacross_fini(ident_t *loc, kmp_int32 gtid); |
| 2078 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 2079 | auto *FnTy = |
Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 2080 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false); |
| 2081 | RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_doacross_fini"); |
| 2082 | break; |
| 2083 | } |
| 2084 | case OMPRTL__kmpc_doacross_post: { |
| 2085 | // Build void __kmpc_doacross_post(ident_t *loc, kmp_int32 gtid, kmp_int64 |
| 2086 | // *vec); |
| 2087 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty, |
| 2088 | CGM.Int64Ty->getPointerTo()}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 2089 | auto *FnTy = |
Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 2090 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false); |
| 2091 | RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_doacross_post"); |
| 2092 | break; |
| 2093 | } |
| 2094 | case OMPRTL__kmpc_doacross_wait: { |
| 2095 | // Build void __kmpc_doacross_wait(ident_t *loc, kmp_int32 gtid, kmp_int64 |
| 2096 | // *vec); |
| 2097 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty, |
| 2098 | CGM.Int64Ty->getPointerTo()}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 2099 | auto *FnTy = |
Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 2100 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false); |
| 2101 | RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_doacross_wait"); |
| 2102 | break; |
| 2103 | } |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 2104 | case OMPRTL__kmpc_task_reduction_init: { |
| 2105 | // Build void *__kmpc_task_reduction_init(int gtid, int num_data, void |
| 2106 | // *data); |
| 2107 | llvm::Type *TypeParams[] = {CGM.IntTy, CGM.IntTy, CGM.VoidPtrTy}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 2108 | auto *FnTy = |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 2109 | llvm::FunctionType::get(CGM.VoidPtrTy, TypeParams, /*isVarArg=*/false); |
| 2110 | RTLFn = |
| 2111 | CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_task_reduction_init"); |
| 2112 | break; |
| 2113 | } |
| 2114 | case OMPRTL__kmpc_task_reduction_get_th_data: { |
| 2115 | // Build void *__kmpc_task_reduction_get_th_data(int gtid, void *tg, void |
| 2116 | // *d); |
| 2117 | llvm::Type *TypeParams[] = {CGM.IntTy, CGM.VoidPtrTy, CGM.VoidPtrTy}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 2118 | auto *FnTy = |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 2119 | llvm::FunctionType::get(CGM.VoidPtrTy, TypeParams, /*isVarArg=*/false); |
| 2120 | RTLFn = CGM.CreateRuntimeFunction( |
| 2121 | FnTy, /*Name=*/"__kmpc_task_reduction_get_th_data"); |
| 2122 | break; |
| 2123 | } |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 2124 | case OMPRTL__tgt_target: { |
George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 2125 | // Build int32_t __tgt_target(int64_t device_id, void *host_ptr, int32_t |
| 2126 | // arg_num, void** args_base, void **args, size_t *arg_sizes, int64_t |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 2127 | // *arg_types); |
George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 2128 | llvm::Type *TypeParams[] = {CGM.Int64Ty, |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 2129 | CGM.VoidPtrTy, |
| 2130 | CGM.Int32Ty, |
| 2131 | CGM.VoidPtrPtrTy, |
| 2132 | CGM.VoidPtrPtrTy, |
| 2133 | CGM.SizeTy->getPointerTo(), |
George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 2134 | CGM.Int64Ty->getPointerTo()}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 2135 | auto *FnTy = |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 2136 | llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg*/ false); |
| 2137 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__tgt_target"); |
| 2138 | break; |
| 2139 | } |
Alexey Bataev | a9f77c6 | 2017-12-13 21:04:20 +0000 | [diff] [blame] | 2140 | case OMPRTL__tgt_target_nowait: { |
| 2141 | // Build int32_t __tgt_target_nowait(int64_t device_id, void *host_ptr, |
| 2142 | // int32_t arg_num, void** args_base, void **args, size_t *arg_sizes, |
| 2143 | // int64_t *arg_types); |
| 2144 | llvm::Type *TypeParams[] = {CGM.Int64Ty, |
| 2145 | CGM.VoidPtrTy, |
| 2146 | CGM.Int32Ty, |
| 2147 | CGM.VoidPtrPtrTy, |
| 2148 | CGM.VoidPtrPtrTy, |
| 2149 | CGM.SizeTy->getPointerTo(), |
| 2150 | CGM.Int64Ty->getPointerTo()}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 2151 | auto *FnTy = |
Alexey Bataev | a9f77c6 | 2017-12-13 21:04:20 +0000 | [diff] [blame] | 2152 | llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg*/ false); |
| 2153 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__tgt_target_nowait"); |
| 2154 | break; |
| 2155 | } |
Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 2156 | case OMPRTL__tgt_target_teams: { |
George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 2157 | // Build int32_t __tgt_target_teams(int64_t device_id, void *host_ptr, |
Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 2158 | // int32_t arg_num, void** args_base, void **args, size_t *arg_sizes, |
George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 2159 | // int64_t *arg_types, int32_t num_teams, int32_t thread_limit); |
| 2160 | llvm::Type *TypeParams[] = {CGM.Int64Ty, |
Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 2161 | CGM.VoidPtrTy, |
| 2162 | CGM.Int32Ty, |
| 2163 | CGM.VoidPtrPtrTy, |
| 2164 | CGM.VoidPtrPtrTy, |
| 2165 | CGM.SizeTy->getPointerTo(), |
George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 2166 | CGM.Int64Ty->getPointerTo(), |
Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 2167 | CGM.Int32Ty, |
| 2168 | CGM.Int32Ty}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 2169 | auto *FnTy = |
Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 2170 | llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg*/ false); |
| 2171 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__tgt_target_teams"); |
| 2172 | break; |
| 2173 | } |
Alexey Bataev | a9f77c6 | 2017-12-13 21:04:20 +0000 | [diff] [blame] | 2174 | case OMPRTL__tgt_target_teams_nowait: { |
| 2175 | // Build int32_t __tgt_target_teams_nowait(int64_t device_id, void |
| 2176 | // *host_ptr, int32_t arg_num, void** args_base, void **args, size_t |
| 2177 | // *arg_sizes, int64_t *arg_types, int32_t num_teams, int32_t thread_limit); |
| 2178 | llvm::Type *TypeParams[] = {CGM.Int64Ty, |
| 2179 | CGM.VoidPtrTy, |
| 2180 | CGM.Int32Ty, |
| 2181 | CGM.VoidPtrPtrTy, |
| 2182 | CGM.VoidPtrPtrTy, |
| 2183 | CGM.SizeTy->getPointerTo(), |
| 2184 | CGM.Int64Ty->getPointerTo(), |
| 2185 | CGM.Int32Ty, |
| 2186 | CGM.Int32Ty}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 2187 | auto *FnTy = |
Alexey Bataev | a9f77c6 | 2017-12-13 21:04:20 +0000 | [diff] [blame] | 2188 | llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg*/ false); |
| 2189 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__tgt_target_teams_nowait"); |
| 2190 | break; |
| 2191 | } |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 2192 | case OMPRTL__tgt_register_lib: { |
| 2193 | // Build void __tgt_register_lib(__tgt_bin_desc *desc); |
| 2194 | QualType ParamTy = |
| 2195 | CGM.getContext().getPointerType(getTgtBinaryDescriptorQTy()); |
| 2196 | llvm::Type *TypeParams[] = {CGM.getTypes().ConvertTypeForMem(ParamTy)}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 2197 | auto *FnTy = |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 2198 | llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg*/ false); |
| 2199 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__tgt_register_lib"); |
| 2200 | break; |
| 2201 | } |
| 2202 | case OMPRTL__tgt_unregister_lib: { |
| 2203 | // Build void __tgt_unregister_lib(__tgt_bin_desc *desc); |
| 2204 | QualType ParamTy = |
| 2205 | CGM.getContext().getPointerType(getTgtBinaryDescriptorQTy()); |
| 2206 | llvm::Type *TypeParams[] = {CGM.getTypes().ConvertTypeForMem(ParamTy)}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 2207 | auto *FnTy = |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 2208 | llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg*/ false); |
| 2209 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__tgt_unregister_lib"); |
| 2210 | break; |
| 2211 | } |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 2212 | case OMPRTL__tgt_target_data_begin: { |
George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 2213 | // Build void __tgt_target_data_begin(int64_t device_id, int32_t arg_num, |
| 2214 | // void** args_base, void **args, size_t *arg_sizes, int64_t *arg_types); |
| 2215 | llvm::Type *TypeParams[] = {CGM.Int64Ty, |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 2216 | CGM.Int32Ty, |
| 2217 | CGM.VoidPtrPtrTy, |
| 2218 | CGM.VoidPtrPtrTy, |
| 2219 | CGM.SizeTy->getPointerTo(), |
George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 2220 | CGM.Int64Ty->getPointerTo()}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 2221 | auto *FnTy = |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 2222 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false); |
| 2223 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__tgt_target_data_begin"); |
| 2224 | break; |
| 2225 | } |
Alexey Bataev | 0cc6b8e | 2017-12-14 17:00:17 +0000 | [diff] [blame] | 2226 | case OMPRTL__tgt_target_data_begin_nowait: { |
| 2227 | // Build void __tgt_target_data_begin_nowait(int64_t device_id, int32_t |
| 2228 | // arg_num, void** args_base, void **args, size_t *arg_sizes, int64_t |
| 2229 | // *arg_types); |
| 2230 | llvm::Type *TypeParams[] = {CGM.Int64Ty, |
| 2231 | CGM.Int32Ty, |
| 2232 | CGM.VoidPtrPtrTy, |
| 2233 | CGM.VoidPtrPtrTy, |
| 2234 | CGM.SizeTy->getPointerTo(), |
| 2235 | CGM.Int64Ty->getPointerTo()}; |
| 2236 | auto *FnTy = |
| 2237 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false); |
| 2238 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__tgt_target_data_begin_nowait"); |
| 2239 | break; |
| 2240 | } |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 2241 | case OMPRTL__tgt_target_data_end: { |
George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 2242 | // Build void __tgt_target_data_end(int64_t device_id, int32_t arg_num, |
| 2243 | // void** args_base, void **args, size_t *arg_sizes, int64_t *arg_types); |
| 2244 | llvm::Type *TypeParams[] = {CGM.Int64Ty, |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 2245 | CGM.Int32Ty, |
| 2246 | CGM.VoidPtrPtrTy, |
| 2247 | CGM.VoidPtrPtrTy, |
| 2248 | CGM.SizeTy->getPointerTo(), |
George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 2249 | CGM.Int64Ty->getPointerTo()}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 2250 | auto *FnTy = |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 2251 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false); |
| 2252 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__tgt_target_data_end"); |
| 2253 | break; |
| 2254 | } |
Alexey Bataev | 0cc6b8e | 2017-12-14 17:00:17 +0000 | [diff] [blame] | 2255 | case OMPRTL__tgt_target_data_end_nowait: { |
| 2256 | // Build void __tgt_target_data_end_nowait(int64_t device_id, int32_t |
| 2257 | // arg_num, void** args_base, void **args, size_t *arg_sizes, int64_t |
| 2258 | // *arg_types); |
| 2259 | llvm::Type *TypeParams[] = {CGM.Int64Ty, |
| 2260 | CGM.Int32Ty, |
| 2261 | CGM.VoidPtrPtrTy, |
| 2262 | CGM.VoidPtrPtrTy, |
| 2263 | CGM.SizeTy->getPointerTo(), |
| 2264 | CGM.Int64Ty->getPointerTo()}; |
| 2265 | auto *FnTy = |
| 2266 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false); |
| 2267 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__tgt_target_data_end_nowait"); |
| 2268 | break; |
| 2269 | } |
Samuel Antao | 8d2d730 | 2016-05-26 18:30:22 +0000 | [diff] [blame] | 2270 | case OMPRTL__tgt_target_data_update: { |
George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 2271 | // Build void __tgt_target_data_update(int64_t device_id, int32_t arg_num, |
| 2272 | // void** args_base, void **args, size_t *arg_sizes, int64_t *arg_types); |
| 2273 | llvm::Type *TypeParams[] = {CGM.Int64Ty, |
Samuel Antao | 8d2d730 | 2016-05-26 18:30:22 +0000 | [diff] [blame] | 2274 | CGM.Int32Ty, |
| 2275 | CGM.VoidPtrPtrTy, |
| 2276 | CGM.VoidPtrPtrTy, |
| 2277 | CGM.SizeTy->getPointerTo(), |
George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 2278 | CGM.Int64Ty->getPointerTo()}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 2279 | auto *FnTy = |
Samuel Antao | 8d2d730 | 2016-05-26 18:30:22 +0000 | [diff] [blame] | 2280 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false); |
| 2281 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__tgt_target_data_update"); |
| 2282 | break; |
| 2283 | } |
Alexey Bataev | 0cc6b8e | 2017-12-14 17:00:17 +0000 | [diff] [blame] | 2284 | case OMPRTL__tgt_target_data_update_nowait: { |
| 2285 | // Build void __tgt_target_data_update_nowait(int64_t device_id, int32_t |
| 2286 | // arg_num, void** args_base, void **args, size_t *arg_sizes, int64_t |
| 2287 | // *arg_types); |
| 2288 | llvm::Type *TypeParams[] = {CGM.Int64Ty, |
| 2289 | CGM.Int32Ty, |
| 2290 | CGM.VoidPtrPtrTy, |
| 2291 | CGM.VoidPtrPtrTy, |
| 2292 | CGM.SizeTy->getPointerTo(), |
| 2293 | CGM.Int64Ty->getPointerTo()}; |
| 2294 | auto *FnTy = |
| 2295 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false); |
| 2296 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__tgt_target_data_update_nowait"); |
| 2297 | break; |
| 2298 | } |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 2299 | } |
Alexey Bataev | 50b3c95 | 2016-02-19 10:38:26 +0000 | [diff] [blame] | 2300 | assert(RTLFn && "Unable to find OpenMP runtime function"); |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 2301 | return RTLFn; |
| 2302 | } |
Alexey Bataev | 3a3bf0b | 2014-09-22 10:01:53 +0000 | [diff] [blame] | 2303 | |
Alexander Musman | 21212e4 | 2015-03-13 10:38:23 +0000 | [diff] [blame] | 2304 | llvm::Constant *CGOpenMPRuntime::createForStaticInitFunction(unsigned IVSize, |
| 2305 | bool IVSigned) { |
| 2306 | assert((IVSize == 32 || IVSize == 64) && |
| 2307 | "IV size is not compatible with the omp runtime"); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 2308 | StringRef Name = IVSize == 32 ? (IVSigned ? "__kmpc_for_static_init_4" |
| 2309 | : "__kmpc_for_static_init_4u") |
| 2310 | : (IVSigned ? "__kmpc_for_static_init_8" |
| 2311 | : "__kmpc_for_static_init_8u"); |
| 2312 | llvm::Type *ITy = IVSize == 32 ? CGM.Int32Ty : CGM.Int64Ty; |
| 2313 | auto *PtrTy = llvm::PointerType::getUnqual(ITy); |
Alexander Musman | 21212e4 | 2015-03-13 10:38:23 +0000 | [diff] [blame] | 2314 | llvm::Type *TypeParams[] = { |
| 2315 | getIdentTyPointerTy(), // loc |
| 2316 | CGM.Int32Ty, // tid |
| 2317 | CGM.Int32Ty, // schedtype |
| 2318 | llvm::PointerType::getUnqual(CGM.Int32Ty), // p_lastiter |
| 2319 | PtrTy, // p_lower |
| 2320 | PtrTy, // p_upper |
| 2321 | PtrTy, // p_stride |
| 2322 | ITy, // incr |
| 2323 | ITy // chunk |
| 2324 | }; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 2325 | auto *FnTy = |
Alexander Musman | 21212e4 | 2015-03-13 10:38:23 +0000 | [diff] [blame] | 2326 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false); |
| 2327 | return CGM.CreateRuntimeFunction(FnTy, Name); |
| 2328 | } |
| 2329 | |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 2330 | llvm::Constant *CGOpenMPRuntime::createDispatchInitFunction(unsigned IVSize, |
| 2331 | bool IVSigned) { |
| 2332 | assert((IVSize == 32 || IVSize == 64) && |
| 2333 | "IV size is not compatible with the omp runtime"); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 2334 | StringRef Name = |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 2335 | IVSize == 32 |
| 2336 | ? (IVSigned ? "__kmpc_dispatch_init_4" : "__kmpc_dispatch_init_4u") |
| 2337 | : (IVSigned ? "__kmpc_dispatch_init_8" : "__kmpc_dispatch_init_8u"); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 2338 | llvm::Type *ITy = IVSize == 32 ? CGM.Int32Ty : CGM.Int64Ty; |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 2339 | llvm::Type *TypeParams[] = { getIdentTyPointerTy(), // loc |
| 2340 | CGM.Int32Ty, // tid |
| 2341 | CGM.Int32Ty, // schedtype |
| 2342 | ITy, // lower |
| 2343 | ITy, // upper |
| 2344 | ITy, // stride |
| 2345 | ITy // chunk |
| 2346 | }; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 2347 | auto *FnTy = |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 2348 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false); |
| 2349 | return CGM.CreateRuntimeFunction(FnTy, Name); |
| 2350 | } |
| 2351 | |
Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 2352 | llvm::Constant *CGOpenMPRuntime::createDispatchFiniFunction(unsigned IVSize, |
| 2353 | bool IVSigned) { |
| 2354 | assert((IVSize == 32 || IVSize == 64) && |
| 2355 | "IV size is not compatible with the omp runtime"); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 2356 | StringRef Name = |
Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 2357 | IVSize == 32 |
| 2358 | ? (IVSigned ? "__kmpc_dispatch_fini_4" : "__kmpc_dispatch_fini_4u") |
| 2359 | : (IVSigned ? "__kmpc_dispatch_fini_8" : "__kmpc_dispatch_fini_8u"); |
| 2360 | llvm::Type *TypeParams[] = { |
| 2361 | getIdentTyPointerTy(), // loc |
| 2362 | CGM.Int32Ty, // tid |
| 2363 | }; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 2364 | auto *FnTy = |
Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 2365 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false); |
| 2366 | return CGM.CreateRuntimeFunction(FnTy, Name); |
| 2367 | } |
| 2368 | |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 2369 | llvm::Constant *CGOpenMPRuntime::createDispatchNextFunction(unsigned IVSize, |
| 2370 | bool IVSigned) { |
| 2371 | assert((IVSize == 32 || IVSize == 64) && |
| 2372 | "IV size is not compatible with the omp runtime"); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 2373 | StringRef Name = |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 2374 | IVSize == 32 |
| 2375 | ? (IVSigned ? "__kmpc_dispatch_next_4" : "__kmpc_dispatch_next_4u") |
| 2376 | : (IVSigned ? "__kmpc_dispatch_next_8" : "__kmpc_dispatch_next_8u"); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 2377 | llvm::Type *ITy = IVSize == 32 ? CGM.Int32Ty : CGM.Int64Ty; |
| 2378 | auto *PtrTy = llvm::PointerType::getUnqual(ITy); |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 2379 | llvm::Type *TypeParams[] = { |
| 2380 | getIdentTyPointerTy(), // loc |
| 2381 | CGM.Int32Ty, // tid |
| 2382 | llvm::PointerType::getUnqual(CGM.Int32Ty), // p_lastiter |
| 2383 | PtrTy, // p_lower |
| 2384 | PtrTy, // p_upper |
| 2385 | PtrTy // p_stride |
| 2386 | }; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 2387 | auto *FnTy = |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 2388 | llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg*/ false); |
| 2389 | return CGM.CreateRuntimeFunction(FnTy, Name); |
| 2390 | } |
| 2391 | |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 2392 | Address CGOpenMPRuntime::getAddrOfDeclareTargetLink(const VarDecl *VD) { |
| 2393 | if (CGM.getLangOpts().OpenMPSimd) |
| 2394 | return Address::invalid(); |
Alexey Bataev | 92327c5 | 2018-03-26 16:40:55 +0000 | [diff] [blame] | 2395 | llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res = |
| 2396 | isDeclareTargetDeclaration(VD); |
| 2397 | if (Res && *Res == OMPDeclareTargetDeclAttr::MT_Link) { |
| 2398 | SmallString<64> PtrName; |
| 2399 | { |
| 2400 | llvm::raw_svector_ostream OS(PtrName); |
| 2401 | OS << CGM.getMangledName(GlobalDecl(VD)) << "_decl_tgt_link_ptr"; |
| 2402 | } |
| 2403 | llvm::Value *Ptr = CGM.getModule().getNamedValue(PtrName); |
| 2404 | if (!Ptr) { |
| 2405 | QualType PtrTy = CGM.getContext().getPointerType(VD->getType()); |
| 2406 | Ptr = getOrCreateInternalVariable(CGM.getTypes().ConvertTypeForMem(PtrTy), |
| 2407 | PtrName); |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 2408 | if (!CGM.getLangOpts().OpenMPIsDevice) { |
| 2409 | auto *GV = cast<llvm::GlobalVariable>(Ptr); |
| 2410 | GV->setLinkage(llvm::GlobalValue::ExternalLinkage); |
| 2411 | GV->setInitializer(CGM.GetAddrOfGlobal(VD)); |
| 2412 | } |
| 2413 | CGM.addUsedGlobal(cast<llvm::GlobalValue>(Ptr)); |
| 2414 | registerTargetGlobalVariable(VD, cast<llvm::Constant>(Ptr)); |
Alexey Bataev | 92327c5 | 2018-03-26 16:40:55 +0000 | [diff] [blame] | 2415 | } |
| 2416 | return Address(Ptr, CGM.getContext().getDeclAlign(VD)); |
| 2417 | } |
| 2418 | return Address::invalid(); |
| 2419 | } |
| 2420 | |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2421 | llvm::Constant * |
| 2422 | CGOpenMPRuntime::getOrCreateThreadPrivateCache(const VarDecl *VD) { |
Samuel Antao | f8b5012 | 2015-07-13 22:54:53 +0000 | [diff] [blame] | 2423 | assert(!CGM.getLangOpts().OpenMPUseTLS || |
| 2424 | !CGM.getContext().getTargetInfo().isTLSSupported()); |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2425 | // Lookup the entry, lazily creating it if necessary. |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 2426 | return getOrCreateInternalVariable(CGM.Int8PtrPtrTy, |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 2427 | Twine(CGM.getMangledName(VD), ".cache.")); |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2428 | } |
| 2429 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2430 | Address CGOpenMPRuntime::getAddrOfThreadPrivate(CodeGenFunction &CGF, |
| 2431 | const VarDecl *VD, |
| 2432 | Address VDAddr, |
| 2433 | SourceLocation Loc) { |
Samuel Antao | f8b5012 | 2015-07-13 22:54:53 +0000 | [diff] [blame] | 2434 | if (CGM.getLangOpts().OpenMPUseTLS && |
| 2435 | CGM.getContext().getTargetInfo().isTLSSupported()) |
| 2436 | return VDAddr; |
| 2437 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 2438 | llvm::Type *VarTy = VDAddr.getElementType(); |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 2439 | llvm::Value *Args[] = {emitUpdateLocation(CGF, Loc), getThreadID(CGF, Loc), |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2440 | CGF.Builder.CreatePointerCast(VDAddr.getPointer(), |
| 2441 | CGM.Int8PtrTy), |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2442 | CGM.getSize(CGM.GetTargetTypeStoreSize(VarTy)), |
| 2443 | getOrCreateThreadPrivateCache(VD)}; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2444 | return Address(CGF.EmitRuntimeCall( |
| 2445 | createRuntimeFunction(OMPRTL__kmpc_threadprivate_cached), Args), |
| 2446 | VDAddr.getAlignment()); |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2447 | } |
| 2448 | |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 2449 | void CGOpenMPRuntime::emitThreadPrivateVarInit( |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2450 | CodeGenFunction &CGF, Address VDAddr, llvm::Value *Ctor, |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2451 | llvm::Value *CopyCtor, llvm::Value *Dtor, SourceLocation Loc) { |
| 2452 | // Call kmp_int32 __kmpc_global_thread_num(&loc) to init OpenMP runtime |
| 2453 | // library. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 2454 | llvm::Value *OMPLoc = emitUpdateLocation(CGF, Loc); |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 2455 | CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_global_thread_num), |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2456 | OMPLoc); |
| 2457 | // Call __kmpc_threadprivate_register(&loc, &var, ctor, cctor/*NULL*/, dtor) |
| 2458 | // to register constructor/destructor for variable. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 2459 | llvm::Value *Args[] = { |
| 2460 | OMPLoc, CGF.Builder.CreatePointerCast(VDAddr.getPointer(), CGM.VoidPtrTy), |
| 2461 | Ctor, CopyCtor, Dtor}; |
Alexey Bataev | 1e4b713 | 2014-12-03 12:11:24 +0000 | [diff] [blame] | 2462 | CGF.EmitRuntimeCall( |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 2463 | createRuntimeFunction(OMPRTL__kmpc_threadprivate_register), Args); |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2464 | } |
| 2465 | |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 2466 | llvm::Function *CGOpenMPRuntime::emitThreadPrivateVarDefinition( |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2467 | const VarDecl *VD, Address VDAddr, SourceLocation Loc, |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2468 | bool PerformInit, CodeGenFunction *CGF) { |
Samuel Antao | f8b5012 | 2015-07-13 22:54:53 +0000 | [diff] [blame] | 2469 | if (CGM.getLangOpts().OpenMPUseTLS && |
| 2470 | CGM.getContext().getTargetInfo().isTLSSupported()) |
| 2471 | return nullptr; |
| 2472 | |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2473 | VD = VD->getDefinition(CGM.getContext()); |
| 2474 | if (VD && ThreadPrivateWithDefinition.count(VD) == 0) { |
| 2475 | ThreadPrivateWithDefinition.insert(VD); |
| 2476 | QualType ASTTy = VD->getType(); |
| 2477 | |
| 2478 | llvm::Value *Ctor = nullptr, *CopyCtor = nullptr, *Dtor = nullptr; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 2479 | const Expr *Init = VD->getAnyInitializer(); |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2480 | if (CGM.getLangOpts().CPlusPlus && PerformInit) { |
| 2481 | // Generate function that re-emits the declaration's initializer into the |
| 2482 | // threadprivate copy of the variable VD |
| 2483 | CodeGenFunction CtorCGF(CGM); |
| 2484 | FunctionArgList Args; |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 2485 | ImplicitParamDecl Dst(CGM.getContext(), /*DC=*/nullptr, Loc, |
| 2486 | /*Id=*/nullptr, CGM.getContext().VoidPtrTy, |
Alexey Bataev | 5622323 | 2017-06-09 13:40:18 +0000 | [diff] [blame] | 2487 | ImplicitParamDecl::Other); |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2488 | Args.push_back(&Dst); |
| 2489 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 2490 | const auto &FI = CGM.getTypes().arrangeBuiltinFunctionDeclaration( |
John McCall | c56a8b3 | 2016-03-11 04:30:31 +0000 | [diff] [blame] | 2491 | CGM.getContext().VoidPtrTy, Args); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 2492 | llvm::FunctionType *FTy = CGM.getTypes().GetFunctionType(FI); |
| 2493 | llvm::Function *Fn = CGM.CreateGlobalInitOrDestructFunction( |
Akira Hatanaka | 7791f1a4 | 2015-10-31 01:28:07 +0000 | [diff] [blame] | 2494 | FTy, ".__kmpc_global_ctor_.", FI, Loc); |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2495 | CtorCGF.StartFunction(GlobalDecl(), CGM.getContext().VoidPtrTy, Fn, FI, |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 2496 | Args, Loc, Loc); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 2497 | llvm::Value *ArgVal = CtorCGF.EmitLoadOfScalar( |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2498 | CtorCGF.GetAddrOfLocalVar(&Dst), /*Volatile=*/false, |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2499 | CGM.getContext().VoidPtrTy, Dst.getLocation()); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2500 | Address Arg = Address(ArgVal, VDAddr.getAlignment()); |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 2501 | Arg = CtorCGF.Builder.CreateElementBitCast( |
| 2502 | Arg, CtorCGF.ConvertTypeForMem(ASTTy)); |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2503 | CtorCGF.EmitAnyExprToMem(Init, Arg, Init->getType().getQualifiers(), |
| 2504 | /*IsInitializer=*/true); |
| 2505 | ArgVal = CtorCGF.EmitLoadOfScalar( |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2506 | CtorCGF.GetAddrOfLocalVar(&Dst), /*Volatile=*/false, |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2507 | CGM.getContext().VoidPtrTy, Dst.getLocation()); |
| 2508 | CtorCGF.Builder.CreateStore(ArgVal, CtorCGF.ReturnValue); |
| 2509 | CtorCGF.FinishFunction(); |
| 2510 | Ctor = Fn; |
| 2511 | } |
| 2512 | if (VD->getType().isDestructedType() != QualType::DK_none) { |
| 2513 | // Generate function that emits destructor call for the threadprivate copy |
| 2514 | // of the variable VD |
| 2515 | CodeGenFunction DtorCGF(CGM); |
| 2516 | FunctionArgList Args; |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 2517 | ImplicitParamDecl Dst(CGM.getContext(), /*DC=*/nullptr, Loc, |
| 2518 | /*Id=*/nullptr, CGM.getContext().VoidPtrTy, |
Alexey Bataev | 5622323 | 2017-06-09 13:40:18 +0000 | [diff] [blame] | 2519 | ImplicitParamDecl::Other); |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2520 | Args.push_back(&Dst); |
| 2521 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 2522 | const auto &FI = CGM.getTypes().arrangeBuiltinFunctionDeclaration( |
John McCall | c56a8b3 | 2016-03-11 04:30:31 +0000 | [diff] [blame] | 2523 | CGM.getContext().VoidTy, Args); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 2524 | llvm::FunctionType *FTy = CGM.getTypes().GetFunctionType(FI); |
| 2525 | llvm::Function *Fn = CGM.CreateGlobalInitOrDestructFunction( |
Akira Hatanaka | 7791f1a4 | 2015-10-31 01:28:07 +0000 | [diff] [blame] | 2526 | FTy, ".__kmpc_global_dtor_.", FI, Loc); |
Adrian Prantl | 1858c66 | 2016-04-24 22:22:29 +0000 | [diff] [blame] | 2527 | auto NL = ApplyDebugLocation::CreateEmpty(DtorCGF); |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2528 | DtorCGF.StartFunction(GlobalDecl(), CGM.getContext().VoidTy, Fn, FI, Args, |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 2529 | Loc, Loc); |
Adrian Prantl | 1858c66 | 2016-04-24 22:22:29 +0000 | [diff] [blame] | 2530 | // Create a scope with an artificial location for the body of this function. |
| 2531 | auto AL = ApplyDebugLocation::CreateArtificial(DtorCGF); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 2532 | llvm::Value *ArgVal = DtorCGF.EmitLoadOfScalar( |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2533 | DtorCGF.GetAddrOfLocalVar(&Dst), |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2534 | /*Volatile=*/false, CGM.getContext().VoidPtrTy, Dst.getLocation()); |
| 2535 | DtorCGF.emitDestroy(Address(ArgVal, VDAddr.getAlignment()), ASTTy, |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2536 | DtorCGF.getDestroyer(ASTTy.isDestructedType()), |
| 2537 | DtorCGF.needsEHCleanup(ASTTy.isDestructedType())); |
| 2538 | DtorCGF.FinishFunction(); |
| 2539 | Dtor = Fn; |
| 2540 | } |
| 2541 | // Do not emit init function if it is not required. |
| 2542 | if (!Ctor && !Dtor) |
| 2543 | return nullptr; |
| 2544 | |
| 2545 | llvm::Type *CopyCtorTyArgs[] = {CGM.VoidPtrTy, CGM.VoidPtrTy}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 2546 | auto *CopyCtorTy = llvm::FunctionType::get(CGM.VoidPtrTy, CopyCtorTyArgs, |
| 2547 | /*isVarArg=*/false) |
| 2548 | ->getPointerTo(); |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2549 | // Copying constructor for the threadprivate variable. |
| 2550 | // Must be NULL - reserved by runtime, but currently it requires that this |
| 2551 | // parameter is always NULL. Otherwise it fires assertion. |
| 2552 | CopyCtor = llvm::Constant::getNullValue(CopyCtorTy); |
| 2553 | if (Ctor == nullptr) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 2554 | auto *CtorTy = llvm::FunctionType::get(CGM.VoidPtrTy, CGM.VoidPtrTy, |
| 2555 | /*isVarArg=*/false) |
| 2556 | ->getPointerTo(); |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2557 | Ctor = llvm::Constant::getNullValue(CtorTy); |
| 2558 | } |
| 2559 | if (Dtor == nullptr) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 2560 | auto *DtorTy = llvm::FunctionType::get(CGM.VoidTy, CGM.VoidPtrTy, |
| 2561 | /*isVarArg=*/false) |
| 2562 | ->getPointerTo(); |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2563 | Dtor = llvm::Constant::getNullValue(DtorTy); |
| 2564 | } |
| 2565 | if (!CGF) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 2566 | auto *InitFunctionTy = |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2567 | llvm::FunctionType::get(CGM.VoidTy, /*isVarArg*/ false); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 2568 | llvm::Function *InitFunction = CGM.CreateGlobalInitOrDestructFunction( |
Akira Hatanaka | 7791f1a4 | 2015-10-31 01:28:07 +0000 | [diff] [blame] | 2569 | InitFunctionTy, ".__omp_threadprivate_init_.", |
| 2570 | CGM.getTypes().arrangeNullaryFunction()); |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2571 | CodeGenFunction InitCGF(CGM); |
| 2572 | FunctionArgList ArgList; |
| 2573 | InitCGF.StartFunction(GlobalDecl(), CGM.getContext().VoidTy, InitFunction, |
| 2574 | CGM.getTypes().arrangeNullaryFunction(), ArgList, |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 2575 | Loc, Loc); |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 2576 | emitThreadPrivateVarInit(InitCGF, VDAddr, Ctor, CopyCtor, Dtor, Loc); |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2577 | InitCGF.FinishFunction(); |
| 2578 | return InitFunction; |
| 2579 | } |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 2580 | emitThreadPrivateVarInit(*CGF, VDAddr, Ctor, CopyCtor, Dtor, Loc); |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2581 | } |
| 2582 | return nullptr; |
| 2583 | } |
| 2584 | |
Alexey Bataev | 34f8a70 | 2018-03-28 14:28:54 +0000 | [diff] [blame] | 2585 | /// \brief Obtain information that uniquely identifies a target entry. This |
| 2586 | /// consists of the file and device IDs as well as line number associated with |
| 2587 | /// the relevant entry source location. |
| 2588 | static void getTargetEntryUniqueInfo(ASTContext &C, SourceLocation Loc, |
| 2589 | unsigned &DeviceID, unsigned &FileID, |
| 2590 | unsigned &LineNum) { |
| 2591 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 2592 | SourceManager &SM = C.getSourceManager(); |
Alexey Bataev | 34f8a70 | 2018-03-28 14:28:54 +0000 | [diff] [blame] | 2593 | |
| 2594 | // The loc should be always valid and have a file ID (the user cannot use |
| 2595 | // #pragma directives in macros) |
| 2596 | |
| 2597 | assert(Loc.isValid() && "Source location is expected to be always valid."); |
| 2598 | assert(Loc.isFileID() && "Source location is expected to refer to a file."); |
| 2599 | |
| 2600 | PresumedLoc PLoc = SM.getPresumedLoc(Loc); |
| 2601 | assert(PLoc.isValid() && "Source location is expected to be always valid."); |
| 2602 | |
| 2603 | llvm::sys::fs::UniqueID ID; |
| 2604 | if (llvm::sys::fs::getUniqueID(PLoc.getFilename(), ID)) |
| 2605 | llvm_unreachable("Source file with target region no longer exists!"); |
| 2606 | |
| 2607 | DeviceID = ID.getDevice(); |
| 2608 | FileID = ID.getFile(); |
| 2609 | LineNum = PLoc.getLine(); |
| 2610 | } |
| 2611 | |
| 2612 | bool CGOpenMPRuntime::emitDeclareTargetVarDefinition(const VarDecl *VD, |
| 2613 | llvm::GlobalVariable *Addr, |
| 2614 | bool PerformInit) { |
| 2615 | Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res = |
| 2616 | isDeclareTargetDeclaration(VD); |
| 2617 | if (!Res || *Res == OMPDeclareTargetDeclAttr::MT_Link) |
| 2618 | return false; |
| 2619 | VD = VD->getDefinition(CGM.getContext()); |
| 2620 | if (VD && !DeclareTargetWithDefinition.insert(VD).second) |
| 2621 | return CGM.getLangOpts().OpenMPIsDevice; |
| 2622 | |
| 2623 | QualType ASTTy = VD->getType(); |
| 2624 | |
| 2625 | SourceLocation Loc = VD->getCanonicalDecl()->getLocStart(); |
| 2626 | // Produce the unique prefix to identify the new target regions. We use |
| 2627 | // the source location of the variable declaration which we know to not |
| 2628 | // conflict with any target region. |
| 2629 | unsigned DeviceID; |
| 2630 | unsigned FileID; |
| 2631 | unsigned Line; |
| 2632 | getTargetEntryUniqueInfo(CGM.getContext(), Loc, DeviceID, FileID, Line); |
| 2633 | SmallString<128> Buffer, Out; |
| 2634 | { |
| 2635 | llvm::raw_svector_ostream OS(Buffer); |
| 2636 | OS << "__omp_offloading_" << llvm::format("_%x", DeviceID) |
| 2637 | << llvm::format("_%x_", FileID) << VD->getName() << "_l" << Line; |
| 2638 | } |
| 2639 | |
| 2640 | const Expr *Init = VD->getAnyInitializer(); |
| 2641 | if (CGM.getLangOpts().CPlusPlus && PerformInit) { |
| 2642 | llvm::Constant *Ctor; |
| 2643 | llvm::Constant *ID; |
| 2644 | if (CGM.getLangOpts().OpenMPIsDevice) { |
| 2645 | // Generate function that re-emits the declaration's initializer into |
| 2646 | // the threadprivate copy of the variable VD |
| 2647 | CodeGenFunction CtorCGF(CGM); |
| 2648 | |
| 2649 | const CGFunctionInfo &FI = CGM.getTypes().arrangeNullaryFunction(); |
| 2650 | llvm::FunctionType *FTy = CGM.getTypes().GetFunctionType(FI); |
| 2651 | llvm::Function *Fn = CGM.CreateGlobalInitOrDestructFunction( |
| 2652 | FTy, Twine(Buffer, "_ctor"), FI, Loc); |
| 2653 | auto NL = ApplyDebugLocation::CreateEmpty(CtorCGF); |
| 2654 | CtorCGF.StartFunction(GlobalDecl(), CGM.getContext().VoidTy, Fn, FI, |
| 2655 | FunctionArgList(), Loc, Loc); |
| 2656 | auto AL = ApplyDebugLocation::CreateArtificial(CtorCGF); |
| 2657 | CtorCGF.EmitAnyExprToMem(Init, |
| 2658 | Address(Addr, CGM.getContext().getDeclAlign(VD)), |
| 2659 | Init->getType().getQualifiers(), |
| 2660 | /*IsInitializer=*/true); |
| 2661 | CtorCGF.FinishFunction(); |
| 2662 | Ctor = Fn; |
| 2663 | ID = llvm::ConstantExpr::getBitCast(Fn, CGM.Int8PtrTy); |
| 2664 | } else { |
| 2665 | Ctor = new llvm::GlobalVariable( |
| 2666 | CGM.getModule(), CGM.Int8Ty, /*isConstant=*/true, |
| 2667 | llvm::GlobalValue::PrivateLinkage, |
| 2668 | llvm::Constant::getNullValue(CGM.Int8Ty), Twine(Buffer, "_ctor")); |
| 2669 | ID = Ctor; |
| 2670 | } |
| 2671 | |
| 2672 | // Register the information for the entry associated with the constructor. |
| 2673 | Out.clear(); |
| 2674 | OffloadEntriesInfoManager.registerTargetRegionEntryInfo( |
| 2675 | DeviceID, FileID, Twine(Buffer, "_ctor").toStringRef(Out), Line, Ctor, |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 2676 | ID, OffloadEntriesInfoManagerTy::OMPTargetRegionEntryCtor); |
Alexey Bataev | 34f8a70 | 2018-03-28 14:28:54 +0000 | [diff] [blame] | 2677 | } |
| 2678 | if (VD->getType().isDestructedType() != QualType::DK_none) { |
| 2679 | llvm::Constant *Dtor; |
| 2680 | llvm::Constant *ID; |
| 2681 | if (CGM.getLangOpts().OpenMPIsDevice) { |
| 2682 | // Generate function that emits destructor call for the threadprivate |
| 2683 | // copy of the variable VD |
| 2684 | CodeGenFunction DtorCGF(CGM); |
| 2685 | |
| 2686 | const CGFunctionInfo &FI = CGM.getTypes().arrangeNullaryFunction(); |
| 2687 | llvm::FunctionType *FTy = CGM.getTypes().GetFunctionType(FI); |
| 2688 | llvm::Function *Fn = CGM.CreateGlobalInitOrDestructFunction( |
| 2689 | FTy, Twine(Buffer, "_dtor"), FI, Loc); |
| 2690 | auto NL = ApplyDebugLocation::CreateEmpty(DtorCGF); |
| 2691 | DtorCGF.StartFunction(GlobalDecl(), CGM.getContext().VoidTy, Fn, FI, |
| 2692 | FunctionArgList(), Loc, Loc); |
| 2693 | // Create a scope with an artificial location for the body of this |
| 2694 | // function. |
| 2695 | auto AL = ApplyDebugLocation::CreateArtificial(DtorCGF); |
| 2696 | DtorCGF.emitDestroy(Address(Addr, CGM.getContext().getDeclAlign(VD)), |
| 2697 | ASTTy, DtorCGF.getDestroyer(ASTTy.isDestructedType()), |
| 2698 | DtorCGF.needsEHCleanup(ASTTy.isDestructedType())); |
| 2699 | DtorCGF.FinishFunction(); |
| 2700 | Dtor = Fn; |
| 2701 | ID = llvm::ConstantExpr::getBitCast(Fn, CGM.Int8PtrTy); |
| 2702 | } else { |
| 2703 | Dtor = new llvm::GlobalVariable( |
| 2704 | CGM.getModule(), CGM.Int8Ty, /*isConstant=*/true, |
| 2705 | llvm::GlobalValue::PrivateLinkage, |
| 2706 | llvm::Constant::getNullValue(CGM.Int8Ty), Twine(Buffer, "_dtor")); |
| 2707 | ID = Dtor; |
| 2708 | } |
| 2709 | // Register the information for the entry associated with the destructor. |
| 2710 | Out.clear(); |
| 2711 | OffloadEntriesInfoManager.registerTargetRegionEntryInfo( |
| 2712 | DeviceID, FileID, Twine(Buffer, "_dtor").toStringRef(Out), Line, Dtor, |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 2713 | ID, OffloadEntriesInfoManagerTy::OMPTargetRegionEntryDtor); |
Alexey Bataev | 34f8a70 | 2018-03-28 14:28:54 +0000 | [diff] [blame] | 2714 | } |
| 2715 | return CGM.getLangOpts().OpenMPIsDevice; |
| 2716 | } |
| 2717 | |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 2718 | Address CGOpenMPRuntime::getAddrOfArtificialThreadPrivate(CodeGenFunction &CGF, |
| 2719 | QualType VarType, |
| 2720 | StringRef Name) { |
| 2721 | llvm::Twine VarName(Name, ".artificial."); |
| 2722 | llvm::Type *VarLVType = CGF.ConvertTypeForMem(VarType); |
| 2723 | llvm::Value *GAddr = getOrCreateInternalVariable(VarLVType, VarName); |
| 2724 | llvm::Value *Args[] = { |
| 2725 | emitUpdateLocation(CGF, SourceLocation()), |
| 2726 | getThreadID(CGF, SourceLocation()), |
| 2727 | CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(GAddr, CGM.VoidPtrTy), |
| 2728 | CGF.Builder.CreateIntCast(CGF.getTypeSize(VarType), CGM.SizeTy, |
| 2729 | /*IsSigned=*/false), |
| 2730 | getOrCreateInternalVariable(CGM.VoidPtrPtrTy, VarName + ".cache.")}; |
| 2731 | return Address( |
| 2732 | CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
| 2733 | CGF.EmitRuntimeCall( |
| 2734 | createRuntimeFunction(OMPRTL__kmpc_threadprivate_cached), Args), |
| 2735 | VarLVType->getPointerTo(/*AddrSpace=*/0)), |
| 2736 | CGM.getPointerAlign()); |
| 2737 | } |
| 2738 | |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 2739 | /// \brief Emits code for OpenMP 'if' clause using specified \a CodeGen |
| 2740 | /// function. Here is the logic: |
| 2741 | /// if (Cond) { |
| 2742 | /// ThenGen(); |
| 2743 | /// } else { |
| 2744 | /// ElseGen(); |
| 2745 | /// } |
Arpith Chacko Jacob | bb36fe8 | 2017-01-10 15:42:51 +0000 | [diff] [blame] | 2746 | void CGOpenMPRuntime::emitOMPIfClause(CodeGenFunction &CGF, const Expr *Cond, |
| 2747 | const RegionCodeGenTy &ThenGen, |
| 2748 | const RegionCodeGenTy &ElseGen) { |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 2749 | CodeGenFunction::LexicalScope ConditionScope(CGF, Cond->getSourceRange()); |
| 2750 | |
| 2751 | // If the condition constant folds and can be elided, try to avoid emitting |
| 2752 | // the condition and the dead arm of the if/else. |
| 2753 | bool CondConstant; |
| 2754 | if (CGF.ConstantFoldsToSimpleInteger(Cond, CondConstant)) { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2755 | if (CondConstant) |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 2756 | ThenGen(CGF); |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2757 | else |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 2758 | ElseGen(CGF); |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 2759 | return; |
| 2760 | } |
| 2761 | |
| 2762 | // Otherwise, the condition did not fold, or we couldn't elide it. Just |
| 2763 | // emit the conditional branch. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 2764 | llvm::BasicBlock *ThenBlock = CGF.createBasicBlock("omp_if.then"); |
| 2765 | llvm::BasicBlock *ElseBlock = CGF.createBasicBlock("omp_if.else"); |
| 2766 | llvm::BasicBlock *ContBlock = CGF.createBasicBlock("omp_if.end"); |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 2767 | CGF.EmitBranchOnBoolExpr(Cond, ThenBlock, ElseBlock, /*TrueCount=*/0); |
| 2768 | |
| 2769 | // Emit the 'then' code. |
| 2770 | CGF.EmitBlock(ThenBlock); |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2771 | ThenGen(CGF); |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 2772 | CGF.EmitBranch(ContBlock); |
| 2773 | // Emit the 'else' code if present. |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2774 | // There is no need to emit line number for unconditional branch. |
| 2775 | (void)ApplyDebugLocation::CreateEmpty(CGF); |
| 2776 | CGF.EmitBlock(ElseBlock); |
| 2777 | ElseGen(CGF); |
| 2778 | // There is no need to emit line number for unconditional branch. |
| 2779 | (void)ApplyDebugLocation::CreateEmpty(CGF); |
| 2780 | CGF.EmitBranch(ContBlock); |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 2781 | // Emit the continuation block for code after the if. |
| 2782 | CGF.EmitBlock(ContBlock, /*IsFinished=*/true); |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 2783 | } |
| 2784 | |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 2785 | void CGOpenMPRuntime::emitParallelCall(CodeGenFunction &CGF, SourceLocation Loc, |
| 2786 | llvm::Value *OutlinedFn, |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 2787 | ArrayRef<llvm::Value *> CapturedVars, |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 2788 | const Expr *IfCond) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 2789 | if (!CGF.HaveInsertPoint()) |
| 2790 | return; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 2791 | llvm::Value *RTLoc = emitUpdateLocation(CGF, Loc); |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2792 | auto &&ThenGen = [OutlinedFn, CapturedVars, RTLoc](CodeGenFunction &CGF, |
| 2793 | PrePostActionTy &) { |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 2794 | // Build call __kmpc_fork_call(loc, n, microtask, var1, .., varn); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 2795 | CGOpenMPRuntime &RT = CGF.CGM.getOpenMPRuntime(); |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 2796 | llvm::Value *Args[] = { |
| 2797 | RTLoc, |
| 2798 | CGF.Builder.getInt32(CapturedVars.size()), // Number of captured vars |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2799 | CGF.Builder.CreateBitCast(OutlinedFn, RT.getKmpc_MicroPointerTy())}; |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 2800 | llvm::SmallVector<llvm::Value *, 16> RealArgs; |
| 2801 | RealArgs.append(std::begin(Args), std::end(Args)); |
| 2802 | RealArgs.append(CapturedVars.begin(), CapturedVars.end()); |
| 2803 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 2804 | llvm::Value *RTLFn = RT.createRuntimeFunction(OMPRTL__kmpc_fork_call); |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 2805 | CGF.EmitRuntimeCall(RTLFn, RealArgs); |
| 2806 | }; |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2807 | auto &&ElseGen = [OutlinedFn, CapturedVars, RTLoc, Loc](CodeGenFunction &CGF, |
| 2808 | PrePostActionTy &) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 2809 | CGOpenMPRuntime &RT = CGF.CGM.getOpenMPRuntime(); |
| 2810 | llvm::Value *ThreadID = RT.getThreadID(CGF, Loc); |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 2811 | // Build calls: |
| 2812 | // __kmpc_serialized_parallel(&Loc, GTid); |
| 2813 | llvm::Value *Args[] = {RTLoc, ThreadID}; |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2814 | CGF.EmitRuntimeCall( |
| 2815 | RT.createRuntimeFunction(OMPRTL__kmpc_serialized_parallel), Args); |
Alexey Bataev | d74d060 | 2014-10-13 06:02:40 +0000 | [diff] [blame] | 2816 | |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 2817 | // OutlinedFn(>id, &zero, CapturedStruct); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 2818 | Address ThreadIDAddr = RT.emitThreadIDAddress(CGF, Loc); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2819 | Address ZeroAddr = |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2820 | CGF.CreateTempAlloca(CGF.Int32Ty, CharUnits::fromQuantity(4), |
| 2821 | /*Name*/ ".zero.addr"); |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 2822 | CGF.InitTempAlloca(ZeroAddr, CGF.Builder.getInt32(/*C*/ 0)); |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 2823 | llvm::SmallVector<llvm::Value *, 16> OutlinedFnArgs; |
| 2824 | OutlinedFnArgs.push_back(ThreadIDAddr.getPointer()); |
| 2825 | OutlinedFnArgs.push_back(ZeroAddr.getPointer()); |
| 2826 | OutlinedFnArgs.append(CapturedVars.begin(), CapturedVars.end()); |
Alexey Bataev | 3c595a6 | 2017-08-14 15:01:03 +0000 | [diff] [blame] | 2827 | RT.emitOutlinedFunctionCall(CGF, Loc, OutlinedFn, OutlinedFnArgs); |
Alexey Bataev | d74d060 | 2014-10-13 06:02:40 +0000 | [diff] [blame] | 2828 | |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 2829 | // __kmpc_end_serialized_parallel(&Loc, GTid); |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2830 | llvm::Value *EndArgs[] = {RT.emitUpdateLocation(CGF, Loc), ThreadID}; |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 2831 | CGF.EmitRuntimeCall( |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2832 | RT.createRuntimeFunction(OMPRTL__kmpc_end_serialized_parallel), |
| 2833 | EndArgs); |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 2834 | }; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 2835 | if (IfCond) { |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 2836 | emitOMPIfClause(CGF, IfCond, ThenGen, ElseGen); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 2837 | } else { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2838 | RegionCodeGenTy ThenRCG(ThenGen); |
| 2839 | ThenRCG(CGF); |
Alexey Bataev | f539faa | 2016-03-28 12:58:34 +0000 | [diff] [blame] | 2840 | } |
Alexey Bataev | d74d060 | 2014-10-13 06:02:40 +0000 | [diff] [blame] | 2841 | } |
| 2842 | |
NAKAMURA Takumi | 59c74b22 | 2014-10-27 08:08:18 +0000 | [diff] [blame] | 2843 | // 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] | 2844 | // thread-ID variable (it is passed in a first argument of the outlined function |
| 2845 | // as "kmp_int32 *gtid"). Otherwise, if we're not inside parallel region, but in |
| 2846 | // regular serial code region, get thread ID by calling kmp_int32 |
| 2847 | // kmpc_global_thread_num(ident_t *loc), stash this thread ID in a temporary and |
| 2848 | // return the address of that temp. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2849 | Address CGOpenMPRuntime::emitThreadIDAddress(CodeGenFunction &CGF, |
| 2850 | SourceLocation Loc) { |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 2851 | if (auto *OMPRegionInfo = |
Alexey Bataev | d74d060 | 2014-10-13 06:02:40 +0000 | [diff] [blame] | 2852 | dyn_cast_or_null<CGOpenMPRegionInfo>(CGF.CapturedStmtInfo)) |
Alexey Bataev | 8cbe0a6 | 2015-02-26 10:27:34 +0000 | [diff] [blame] | 2853 | if (OMPRegionInfo->getThreadIDVariable()) |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 2854 | return OMPRegionInfo->getThreadIDVariableLValue(CGF).getAddress(); |
Alexey Bataev | 8cbe0a6 | 2015-02-26 10:27:34 +0000 | [diff] [blame] | 2855 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 2856 | llvm::Value *ThreadID = getThreadID(CGF, Loc); |
| 2857 | QualType Int32Ty = |
Alexey Bataev | d74d060 | 2014-10-13 06:02:40 +0000 | [diff] [blame] | 2858 | CGF.getContext().getIntTypeForBitwidth(/*DestWidth*/ 32, /*Signed*/ true); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 2859 | Address ThreadIDTemp = CGF.CreateMemTemp(Int32Ty, /*Name*/ ".threadid_temp."); |
Alexey Bataev | d74d060 | 2014-10-13 06:02:40 +0000 | [diff] [blame] | 2860 | CGF.EmitStoreOfScalar(ThreadID, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2861 | CGF.MakeAddrLValue(ThreadIDTemp, Int32Ty)); |
Alexey Bataev | d74d060 | 2014-10-13 06:02:40 +0000 | [diff] [blame] | 2862 | |
| 2863 | return ThreadIDTemp; |
| 2864 | } |
| 2865 | |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2866 | llvm::Constant * |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 2867 | CGOpenMPRuntime::getOrCreateInternalVariable(llvm::Type *Ty, |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2868 | const llvm::Twine &Name) { |
Alexey Bataev | 3a3bf0b | 2014-09-22 10:01:53 +0000 | [diff] [blame] | 2869 | SmallString<256> Buffer; |
| 2870 | llvm::raw_svector_ostream Out(Buffer); |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2871 | Out << Name; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 2872 | StringRef RuntimeName = Out.str(); |
Alexey Bataev | 43a919f | 2018-04-13 17:48:43 +0000 | [diff] [blame] | 2873 | auto &Elem = *InternalVars.try_emplace(RuntimeName, nullptr).first; |
David Blaikie | 13156b6 | 2014-11-19 03:06:06 +0000 | [diff] [blame] | 2874 | if (Elem.second) { |
| 2875 | assert(Elem.second->getType()->getPointerElementType() == Ty && |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2876 | "OMP internal variable has different type than requested"); |
David Blaikie | 13156b6 | 2014-11-19 03:06:06 +0000 | [diff] [blame] | 2877 | return &*Elem.second; |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2878 | } |
Alexey Bataev | 3a3bf0b | 2014-09-22 10:01:53 +0000 | [diff] [blame] | 2879 | |
David Blaikie | 13156b6 | 2014-11-19 03:06:06 +0000 | [diff] [blame] | 2880 | return Elem.second = new llvm::GlobalVariable( |
| 2881 | CGM.getModule(), Ty, /*IsConstant*/ false, |
| 2882 | llvm::GlobalValue::CommonLinkage, llvm::Constant::getNullValue(Ty), |
| 2883 | Elem.first()); |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2884 | } |
| 2885 | |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 2886 | llvm::Value *CGOpenMPRuntime::getCriticalRegionLock(StringRef CriticalName) { |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2887 | llvm::Twine Name(".gomp_critical_user_", CriticalName); |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 2888 | return getOrCreateInternalVariable(KmpCriticalNameTy, Name.concat(".var")); |
Alexey Bataev | 3a3bf0b | 2014-09-22 10:01:53 +0000 | [diff] [blame] | 2889 | } |
| 2890 | |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 2891 | namespace { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2892 | /// Common pre(post)-action for different OpenMP constructs. |
| 2893 | class CommonActionTy final : public PrePostActionTy { |
| 2894 | llvm::Value *EnterCallee; |
| 2895 | ArrayRef<llvm::Value *> EnterArgs; |
| 2896 | llvm::Value *ExitCallee; |
| 2897 | ArrayRef<llvm::Value *> ExitArgs; |
| 2898 | bool Conditional; |
| 2899 | llvm::BasicBlock *ContBlock = nullptr; |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 2900 | |
| 2901 | public: |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2902 | CommonActionTy(llvm::Value *EnterCallee, ArrayRef<llvm::Value *> EnterArgs, |
| 2903 | llvm::Value *ExitCallee, ArrayRef<llvm::Value *> ExitArgs, |
| 2904 | bool Conditional = false) |
| 2905 | : EnterCallee(EnterCallee), EnterArgs(EnterArgs), ExitCallee(ExitCallee), |
| 2906 | ExitArgs(ExitArgs), Conditional(Conditional) {} |
| 2907 | void Enter(CodeGenFunction &CGF) override { |
| 2908 | llvm::Value *EnterRes = CGF.EmitRuntimeCall(EnterCallee, EnterArgs); |
| 2909 | if (Conditional) { |
| 2910 | llvm::Value *CallBool = CGF.Builder.CreateIsNotNull(EnterRes); |
| 2911 | auto *ThenBlock = CGF.createBasicBlock("omp_if.then"); |
| 2912 | ContBlock = CGF.createBasicBlock("omp_if.end"); |
| 2913 | // Generate the branch (If-stmt) |
| 2914 | CGF.Builder.CreateCondBr(CallBool, ThenBlock, ContBlock); |
| 2915 | CGF.EmitBlock(ThenBlock); |
| 2916 | } |
Alexey Bataev | a744ff5 | 2015-05-05 09:24:37 +0000 | [diff] [blame] | 2917 | } |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2918 | void Done(CodeGenFunction &CGF) { |
| 2919 | // Emit the rest of blocks/branches |
| 2920 | CGF.EmitBranch(ContBlock); |
| 2921 | CGF.EmitBlock(ContBlock, true); |
| 2922 | } |
| 2923 | void Exit(CodeGenFunction &CGF) override { |
| 2924 | CGF.EmitRuntimeCall(ExitCallee, ExitArgs); |
Alexey Bataev | 3e6124b | 2015-04-10 07:48:12 +0000 | [diff] [blame] | 2925 | } |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 2926 | }; |
Hans Wennborg | 7eb5464 | 2015-09-10 17:07:54 +0000 | [diff] [blame] | 2927 | } // anonymous namespace |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 2928 | |
| 2929 | void CGOpenMPRuntime::emitCriticalRegion(CodeGenFunction &CGF, |
| 2930 | StringRef CriticalName, |
| 2931 | const RegionCodeGenTy &CriticalOpGen, |
Alexey Bataev | fc57d16 | 2015-12-15 10:55:09 +0000 | [diff] [blame] | 2932 | SourceLocation Loc, const Expr *Hint) { |
| 2933 | // __kmpc_critical[_with_hint](ident_t *, gtid, Lock[, hint]); |
Alexey Bataev | 75ddfab | 2014-12-01 11:32:38 +0000 | [diff] [blame] | 2934 | // CriticalOpGen(); |
| 2935 | // __kmpc_end_critical(ident_t *, gtid, Lock); |
| 2936 | // Prepare arguments and build a call to __kmpc_critical |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 2937 | if (!CGF.HaveInsertPoint()) |
| 2938 | return; |
Alexey Bataev | fc57d16 | 2015-12-15 10:55:09 +0000 | [diff] [blame] | 2939 | llvm::Value *Args[] = {emitUpdateLocation(CGF, Loc), getThreadID(CGF, Loc), |
| 2940 | getCriticalRegionLock(CriticalName)}; |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2941 | llvm::SmallVector<llvm::Value *, 4> EnterArgs(std::begin(Args), |
| 2942 | std::end(Args)); |
Alexey Bataev | fc57d16 | 2015-12-15 10:55:09 +0000 | [diff] [blame] | 2943 | if (Hint) { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2944 | EnterArgs.push_back(CGF.Builder.CreateIntCast( |
| 2945 | CGF.EmitScalarExpr(Hint), CGM.IntPtrTy, /*isSigned=*/false)); |
| 2946 | } |
| 2947 | CommonActionTy Action( |
| 2948 | createRuntimeFunction(Hint ? OMPRTL__kmpc_critical_with_hint |
| 2949 | : OMPRTL__kmpc_critical), |
| 2950 | EnterArgs, createRuntimeFunction(OMPRTL__kmpc_end_critical), Args); |
| 2951 | CriticalOpGen.setAction(Action); |
Alexey Bataev | fc57d16 | 2015-12-15 10:55:09 +0000 | [diff] [blame] | 2952 | emitInlinedDirective(CGF, OMPD_critical, CriticalOpGen); |
Alexey Bataev | 3a3bf0b | 2014-09-22 10:01:53 +0000 | [diff] [blame] | 2953 | } |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 2954 | |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 2955 | void CGOpenMPRuntime::emitMasterRegion(CodeGenFunction &CGF, |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 2956 | const RegionCodeGenTy &MasterOpGen, |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 2957 | SourceLocation Loc) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 2958 | if (!CGF.HaveInsertPoint()) |
| 2959 | return; |
Alexey Bataev | 8d69065 | 2014-12-04 07:23:53 +0000 | [diff] [blame] | 2960 | // if(__kmpc_master(ident_t *, gtid)) { |
| 2961 | // MasterOpGen(); |
| 2962 | // __kmpc_end_master(ident_t *, gtid); |
| 2963 | // } |
| 2964 | // Prepare arguments and build a call to __kmpc_master |
Alexey Bataev | d7614fb | 2015-04-10 06:33:45 +0000 | [diff] [blame] | 2965 | llvm::Value *Args[] = {emitUpdateLocation(CGF, Loc), getThreadID(CGF, Loc)}; |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2966 | CommonActionTy Action(createRuntimeFunction(OMPRTL__kmpc_master), Args, |
| 2967 | createRuntimeFunction(OMPRTL__kmpc_end_master), Args, |
| 2968 | /*Conditional=*/true); |
| 2969 | MasterOpGen.setAction(Action); |
| 2970 | emitInlinedDirective(CGF, OMPD_master, MasterOpGen); |
| 2971 | Action.Done(CGF); |
Alexey Bataev | 8d69065 | 2014-12-04 07:23:53 +0000 | [diff] [blame] | 2972 | } |
| 2973 | |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 2974 | void CGOpenMPRuntime::emitTaskyieldCall(CodeGenFunction &CGF, |
| 2975 | SourceLocation Loc) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 2976 | if (!CGF.HaveInsertPoint()) |
| 2977 | return; |
Alexey Bataev | 9f797f3 | 2015-02-05 05:57:51 +0000 | [diff] [blame] | 2978 | // Build call __kmpc_omp_taskyield(loc, thread_id, 0); |
| 2979 | llvm::Value *Args[] = { |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 2980 | emitUpdateLocation(CGF, Loc), getThreadID(CGF, Loc), |
Alexey Bataev | 9f797f3 | 2015-02-05 05:57:51 +0000 | [diff] [blame] | 2981 | llvm::ConstantInt::get(CGM.IntTy, /*V=*/0, /*isSigned=*/true)}; |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 2982 | CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_omp_taskyield), Args); |
Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 2983 | if (auto *Region = dyn_cast_or_null<CGOpenMPRegionInfo>(CGF.CapturedStmtInfo)) |
| 2984 | Region->emitUntiedSwitch(CGF); |
Alexey Bataev | 9f797f3 | 2015-02-05 05:57:51 +0000 | [diff] [blame] | 2985 | } |
| 2986 | |
Alexey Bataev | c30dd2d | 2015-06-18 12:14:09 +0000 | [diff] [blame] | 2987 | void CGOpenMPRuntime::emitTaskgroupRegion(CodeGenFunction &CGF, |
| 2988 | const RegionCodeGenTy &TaskgroupOpGen, |
| 2989 | SourceLocation Loc) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 2990 | if (!CGF.HaveInsertPoint()) |
| 2991 | return; |
Alexey Bataev | c30dd2d | 2015-06-18 12:14:09 +0000 | [diff] [blame] | 2992 | // __kmpc_taskgroup(ident_t *, gtid); |
| 2993 | // TaskgroupOpGen(); |
| 2994 | // __kmpc_end_taskgroup(ident_t *, gtid); |
| 2995 | // Prepare arguments and build a call to __kmpc_taskgroup |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2996 | llvm::Value *Args[] = {emitUpdateLocation(CGF, Loc), getThreadID(CGF, Loc)}; |
| 2997 | CommonActionTy Action(createRuntimeFunction(OMPRTL__kmpc_taskgroup), Args, |
| 2998 | createRuntimeFunction(OMPRTL__kmpc_end_taskgroup), |
| 2999 | Args); |
| 3000 | TaskgroupOpGen.setAction(Action); |
| 3001 | emitInlinedDirective(CGF, OMPD_taskgroup, TaskgroupOpGen); |
Alexey Bataev | c30dd2d | 2015-06-18 12:14:09 +0000 | [diff] [blame] | 3002 | } |
| 3003 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3004 | /// Given an array of pointers to variables, project the address of a |
| 3005 | /// given variable. |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 3006 | static Address emitAddrOfVarFromArray(CodeGenFunction &CGF, Address Array, |
| 3007 | unsigned Index, const VarDecl *Var) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3008 | // Pull out the pointer to the variable. |
| 3009 | Address PtrAddr = |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 3010 | CGF.Builder.CreateConstArrayGEP(Array, Index, CGF.getPointerSize()); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3011 | llvm::Value *Ptr = CGF.Builder.CreateLoad(PtrAddr); |
| 3012 | |
| 3013 | Address Addr = Address(Ptr, CGF.getContext().getDeclAlign(Var)); |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 3014 | Addr = CGF.Builder.CreateElementBitCast( |
| 3015 | Addr, CGF.ConvertTypeForMem(Var->getType())); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3016 | return Addr; |
| 3017 | } |
| 3018 | |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3019 | static llvm::Value *emitCopyprivateCopyFunction( |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 3020 | CodeGenModule &CGM, llvm::Type *ArgsType, |
| 3021 | ArrayRef<const Expr *> CopyprivateVars, ArrayRef<const Expr *> DestExprs, |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 3022 | ArrayRef<const Expr *> SrcExprs, ArrayRef<const Expr *> AssignmentOps, |
| 3023 | SourceLocation Loc) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 3024 | ASTContext &C = CGM.getContext(); |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3025 | // void copy_func(void *LHSArg, void *RHSArg); |
| 3026 | FunctionArgList Args; |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 3027 | ImplicitParamDecl LHSArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, C.VoidPtrTy, |
| 3028 | ImplicitParamDecl::Other); |
| 3029 | ImplicitParamDecl RHSArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, C.VoidPtrTy, |
| 3030 | ImplicitParamDecl::Other); |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3031 | Args.push_back(&LHSArg); |
| 3032 | Args.push_back(&RHSArg); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 3033 | const auto &CGFI = |
| 3034 | CGM.getTypes().arrangeBuiltinFunctionDeclaration(C.VoidTy, Args); |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3035 | auto *Fn = llvm::Function::Create( |
| 3036 | CGM.getTypes().GetFunctionType(CGFI), llvm::GlobalValue::InternalLinkage, |
| 3037 | ".omp.copyprivate.copy_func", &CGM.getModule()); |
Rafael Espindola | 51ec5a9 | 2018-02-28 23:46:35 +0000 | [diff] [blame] | 3038 | CGM.SetInternalFunctionAttributes(GlobalDecl(), Fn, CGFI); |
Alexey Bataev | c0f879b | 2018-04-10 20:10:53 +0000 | [diff] [blame] | 3039 | Fn->setDoesNotRecurse(); |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3040 | CodeGenFunction CGF(CGM); |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 3041 | CGF.StartFunction(GlobalDecl(), C.VoidTy, Fn, CGFI, Args, Loc, Loc); |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 3042 | // Dest = (void*[n])(LHSArg); |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3043 | // Src = (void*[n])(RHSArg); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3044 | Address LHS(CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
| 3045 | CGF.Builder.CreateLoad(CGF.GetAddrOfLocalVar(&LHSArg)), |
| 3046 | ArgsType), CGF.getPointerAlign()); |
| 3047 | Address RHS(CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
| 3048 | CGF.Builder.CreateLoad(CGF.GetAddrOfLocalVar(&RHSArg)), |
| 3049 | ArgsType), CGF.getPointerAlign()); |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3050 | // *(Type0*)Dst[0] = *(Type0*)Src[0]; |
| 3051 | // *(Type1*)Dst[1] = *(Type1*)Src[1]; |
| 3052 | // ... |
| 3053 | // *(Typen*)Dst[n] = *(Typen*)Src[n]; |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3054 | for (unsigned I = 0, E = AssignmentOps.size(); I < E; ++I) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 3055 | const auto *DestVar = |
| 3056 | cast<VarDecl>(cast<DeclRefExpr>(DestExprs[I])->getDecl()); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3057 | Address DestAddr = emitAddrOfVarFromArray(CGF, LHS, I, DestVar); |
| 3058 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 3059 | const auto *SrcVar = |
| 3060 | cast<VarDecl>(cast<DeclRefExpr>(SrcExprs[I])->getDecl()); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3061 | Address SrcAddr = emitAddrOfVarFromArray(CGF, RHS, I, SrcVar); |
| 3062 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 3063 | const auto *VD = cast<DeclRefExpr>(CopyprivateVars[I])->getDecl(); |
Alexey Bataev | 1d9c15c | 2015-05-19 12:31:28 +0000 | [diff] [blame] | 3064 | QualType Type = VD->getType(); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3065 | CGF.EmitOMPCopy(Type, DestAddr, SrcAddr, DestVar, SrcVar, AssignmentOps[I]); |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3066 | } |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3067 | CGF.FinishFunction(); |
| 3068 | return Fn; |
| 3069 | } |
| 3070 | |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 3071 | void CGOpenMPRuntime::emitSingleRegion(CodeGenFunction &CGF, |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 3072 | const RegionCodeGenTy &SingleOpGen, |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3073 | SourceLocation Loc, |
| 3074 | ArrayRef<const Expr *> CopyprivateVars, |
| 3075 | ArrayRef<const Expr *> SrcExprs, |
| 3076 | ArrayRef<const Expr *> DstExprs, |
| 3077 | ArrayRef<const Expr *> AssignmentOps) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 3078 | if (!CGF.HaveInsertPoint()) |
| 3079 | return; |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3080 | assert(CopyprivateVars.size() == SrcExprs.size() && |
| 3081 | CopyprivateVars.size() == DstExprs.size() && |
| 3082 | CopyprivateVars.size() == AssignmentOps.size()); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 3083 | ASTContext &C = CGM.getContext(); |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3084 | // int32 did_it = 0; |
Alexey Bataev | 6956e2e | 2015-02-05 06:35:41 +0000 | [diff] [blame] | 3085 | // if(__kmpc_single(ident_t *, gtid)) { |
| 3086 | // SingleOpGen(); |
| 3087 | // __kmpc_end_single(ident_t *, gtid); |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3088 | // did_it = 1; |
Alexey Bataev | 6956e2e | 2015-02-05 06:35:41 +0000 | [diff] [blame] | 3089 | // } |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3090 | // call __kmpc_copyprivate(ident_t *, gtid, <buf_size>, <copyprivate list>, |
| 3091 | // <copy_func>, did_it); |
| 3092 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3093 | Address DidIt = Address::invalid(); |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3094 | if (!CopyprivateVars.empty()) { |
| 3095 | // int32 did_it = 0; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 3096 | QualType KmpInt32Ty = |
| 3097 | C.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1); |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3098 | DidIt = CGF.CreateMemTemp(KmpInt32Ty, ".omp.copyprivate.did_it"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3099 | CGF.Builder.CreateStore(CGF.Builder.getInt32(0), DidIt); |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3100 | } |
Alexey Bataev | 6956e2e | 2015-02-05 06:35:41 +0000 | [diff] [blame] | 3101 | // Prepare arguments and build a call to __kmpc_single |
Alexey Bataev | d7614fb | 2015-04-10 06:33:45 +0000 | [diff] [blame] | 3102 | llvm::Value *Args[] = {emitUpdateLocation(CGF, Loc), getThreadID(CGF, Loc)}; |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 3103 | CommonActionTy Action(createRuntimeFunction(OMPRTL__kmpc_single), Args, |
| 3104 | createRuntimeFunction(OMPRTL__kmpc_end_single), Args, |
| 3105 | /*Conditional=*/true); |
| 3106 | SingleOpGen.setAction(Action); |
| 3107 | emitInlinedDirective(CGF, OMPD_single, SingleOpGen); |
| 3108 | if (DidIt.isValid()) { |
| 3109 | // did_it = 1; |
| 3110 | CGF.Builder.CreateStore(CGF.Builder.getInt32(1), DidIt); |
| 3111 | } |
| 3112 | Action.Done(CGF); |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3113 | // call __kmpc_copyprivate(ident_t *, gtid, <buf_size>, <copyprivate list>, |
| 3114 | // <copy_func>, did_it); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3115 | if (DidIt.isValid()) { |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3116 | llvm::APInt ArraySize(/*unsigned int numBits=*/32, CopyprivateVars.size()); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 3117 | QualType CopyprivateArrayTy = |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3118 | C.getConstantArrayType(C.VoidPtrTy, ArraySize, ArrayType::Normal, |
| 3119 | /*IndexTypeQuals=*/0); |
| 3120 | // Create a list of all private variables for copyprivate. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3121 | Address CopyprivateList = |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3122 | CGF.CreateMemTemp(CopyprivateArrayTy, ".omp.copyprivate.cpr_list"); |
| 3123 | for (unsigned I = 0, E = CopyprivateVars.size(); I < E; ++I) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3124 | Address Elem = CGF.Builder.CreateConstArrayGEP( |
| 3125 | CopyprivateList, I, CGF.getPointerSize()); |
| 3126 | CGF.Builder.CreateStore( |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3127 | CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3128 | CGF.EmitLValue(CopyprivateVars[I]).getPointer(), CGF.VoidPtrTy), |
| 3129 | Elem); |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3130 | } |
| 3131 | // Build function that copies private values from single region to all other |
| 3132 | // threads in the corresponding parallel region. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 3133 | llvm::Value *CpyFn = emitCopyprivateCopyFunction( |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3134 | CGM, CGF.ConvertTypeForMem(CopyprivateArrayTy)->getPointerTo(), |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 3135 | CopyprivateVars, SrcExprs, DstExprs, AssignmentOps, Loc); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 3136 | llvm::Value *BufSize = CGF.getTypeSize(CopyprivateArrayTy); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3137 | Address CL = |
| 3138 | CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(CopyprivateList, |
| 3139 | CGF.VoidPtrTy); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 3140 | llvm::Value *DidItVal = CGF.Builder.CreateLoad(DidIt); |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3141 | llvm::Value *Args[] = { |
| 3142 | emitUpdateLocation(CGF, Loc), // ident_t *<loc> |
| 3143 | getThreadID(CGF, Loc), // i32 <gtid> |
Alexey Bataev | 66beaa9 | 2015-04-30 03:47:32 +0000 | [diff] [blame] | 3144 | BufSize, // size_t <buf_size> |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3145 | CL.getPointer(), // void *<copyprivate list> |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3146 | CpyFn, // void (*) (void *, void *) <copy_func> |
| 3147 | DidItVal // i32 did_it |
| 3148 | }; |
| 3149 | CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_copyprivate), Args); |
| 3150 | } |
Alexey Bataev | 6956e2e | 2015-02-05 06:35:41 +0000 | [diff] [blame] | 3151 | } |
| 3152 | |
Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 3153 | void CGOpenMPRuntime::emitOrderedRegion(CodeGenFunction &CGF, |
| 3154 | const RegionCodeGenTy &OrderedOpGen, |
Alexey Bataev | 5f600d6 | 2015-09-29 03:48:57 +0000 | [diff] [blame] | 3155 | SourceLocation Loc, bool IsThreads) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 3156 | if (!CGF.HaveInsertPoint()) |
| 3157 | return; |
Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 3158 | // __kmpc_ordered(ident_t *, gtid); |
| 3159 | // OrderedOpGen(); |
| 3160 | // __kmpc_end_ordered(ident_t *, gtid); |
| 3161 | // Prepare arguments and build a call to __kmpc_ordered |
Alexey Bataev | 5f600d6 | 2015-09-29 03:48:57 +0000 | [diff] [blame] | 3162 | if (IsThreads) { |
Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 3163 | llvm::Value *Args[] = {emitUpdateLocation(CGF, Loc), getThreadID(CGF, Loc)}; |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 3164 | CommonActionTy Action(createRuntimeFunction(OMPRTL__kmpc_ordered), Args, |
| 3165 | createRuntimeFunction(OMPRTL__kmpc_end_ordered), |
| 3166 | Args); |
| 3167 | OrderedOpGen.setAction(Action); |
| 3168 | emitInlinedDirective(CGF, OMPD_ordered, OrderedOpGen); |
| 3169 | return; |
Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 3170 | } |
Alexey Bataev | 5f600d6 | 2015-09-29 03:48:57 +0000 | [diff] [blame] | 3171 | emitInlinedDirective(CGF, OMPD_ordered, OrderedOpGen); |
Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 3172 | } |
| 3173 | |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 3174 | void CGOpenMPRuntime::emitBarrierCall(CodeGenFunction &CGF, SourceLocation Loc, |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 3175 | OpenMPDirectiveKind Kind, bool EmitChecks, |
| 3176 | bool ForceSimpleCall) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 3177 | if (!CGF.HaveInsertPoint()) |
| 3178 | return; |
Alexey Bataev | 8f7c1b0 | 2014-12-05 04:09:23 +0000 | [diff] [blame] | 3179 | // Build call __kmpc_cancel_barrier(loc, thread_id); |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 3180 | // Build call __kmpc_barrier(loc, thread_id); |
Alexey Bataev | 50b3c95 | 2016-02-19 10:38:26 +0000 | [diff] [blame] | 3181 | unsigned Flags; |
| 3182 | if (Kind == OMPD_for) |
| 3183 | Flags = OMP_IDENT_BARRIER_IMPL_FOR; |
| 3184 | else if (Kind == OMPD_sections) |
| 3185 | Flags = OMP_IDENT_BARRIER_IMPL_SECTIONS; |
| 3186 | else if (Kind == OMPD_single) |
| 3187 | Flags = OMP_IDENT_BARRIER_IMPL_SINGLE; |
| 3188 | else if (Kind == OMPD_barrier) |
| 3189 | Flags = OMP_IDENT_BARRIER_EXPL; |
| 3190 | else |
| 3191 | Flags = OMP_IDENT_BARRIER_IMPL; |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 3192 | // Build call __kmpc_cancel_barrier(loc, thread_id) or __kmpc_barrier(loc, |
| 3193 | // thread_id); |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 3194 | llvm::Value *Args[] = {emitUpdateLocation(CGF, Loc, Flags), |
| 3195 | getThreadID(CGF, Loc)}; |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 3196 | if (auto *OMPRegionInfo = |
| 3197 | dyn_cast_or_null<CGOpenMPRegionInfo>(CGF.CapturedStmtInfo)) { |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 3198 | if (!ForceSimpleCall && OMPRegionInfo->hasCancel()) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 3199 | llvm::Value *Result = CGF.EmitRuntimeCall( |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 3200 | createRuntimeFunction(OMPRTL__kmpc_cancel_barrier), Args); |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 3201 | if (EmitChecks) { |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 3202 | // if (__kmpc_cancel_barrier()) { |
| 3203 | // exit from construct; |
| 3204 | // } |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 3205 | llvm::BasicBlock *ExitBB = CGF.createBasicBlock(".cancel.exit"); |
| 3206 | llvm::BasicBlock *ContBB = CGF.createBasicBlock(".cancel.continue"); |
| 3207 | llvm::Value *Cmp = CGF.Builder.CreateIsNotNull(Result); |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 3208 | CGF.Builder.CreateCondBr(Cmp, ExitBB, ContBB); |
| 3209 | CGF.EmitBlock(ExitBB); |
| 3210 | // exit from construct; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 3211 | CodeGenFunction::JumpDest CancelDestination = |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 3212 | CGF.getOMPCancelDestination(OMPRegionInfo->getDirectiveKind()); |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 3213 | CGF.EmitBranchThroughCleanup(CancelDestination); |
| 3214 | CGF.EmitBlock(ContBB, /*IsFinished=*/true); |
| 3215 | } |
| 3216 | return; |
| 3217 | } |
| 3218 | } |
| 3219 | CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_barrier), Args); |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 3220 | } |
| 3221 | |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 3222 | /// \brief Map the OpenMP loop schedule to the runtime enumeration. |
| 3223 | static OpenMPSchedType getRuntimeSchedule(OpenMPScheduleClauseKind ScheduleKind, |
Alexey Bataev | d7589ffe | 2015-05-20 13:12:48 +0000 | [diff] [blame] | 3224 | bool Chunked, bool Ordered) { |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 3225 | switch (ScheduleKind) { |
| 3226 | case OMPC_SCHEDULE_static: |
Alexey Bataev | d7589ffe | 2015-05-20 13:12:48 +0000 | [diff] [blame] | 3227 | return Chunked ? (Ordered ? OMP_ord_static_chunked : OMP_sch_static_chunked) |
| 3228 | : (Ordered ? OMP_ord_static : OMP_sch_static); |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 3229 | case OMPC_SCHEDULE_dynamic: |
Alexey Bataev | d7589ffe | 2015-05-20 13:12:48 +0000 | [diff] [blame] | 3230 | return Ordered ? OMP_ord_dynamic_chunked : OMP_sch_dynamic_chunked; |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 3231 | case OMPC_SCHEDULE_guided: |
Alexey Bataev | d7589ffe | 2015-05-20 13:12:48 +0000 | [diff] [blame] | 3232 | return Ordered ? OMP_ord_guided_chunked : OMP_sch_guided_chunked; |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 3233 | case OMPC_SCHEDULE_runtime: |
Alexey Bataev | d7589ffe | 2015-05-20 13:12:48 +0000 | [diff] [blame] | 3234 | return Ordered ? OMP_ord_runtime : OMP_sch_runtime; |
| 3235 | case OMPC_SCHEDULE_auto: |
| 3236 | return Ordered ? OMP_ord_auto : OMP_sch_auto; |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 3237 | case OMPC_SCHEDULE_unknown: |
| 3238 | assert(!Chunked && "chunk was specified but schedule kind not known"); |
Alexey Bataev | d7589ffe | 2015-05-20 13:12:48 +0000 | [diff] [blame] | 3239 | return Ordered ? OMP_ord_static : OMP_sch_static; |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 3240 | } |
| 3241 | llvm_unreachable("Unexpected runtime schedule"); |
| 3242 | } |
| 3243 | |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3244 | /// \brief Map the OpenMP distribute schedule to the runtime enumeration. |
| 3245 | static OpenMPSchedType |
| 3246 | getRuntimeSchedule(OpenMPDistScheduleClauseKind ScheduleKind, bool Chunked) { |
| 3247 | // only static is allowed for dist_schedule |
| 3248 | return Chunked ? OMP_dist_sch_static_chunked : OMP_dist_sch_static; |
| 3249 | } |
| 3250 | |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 3251 | bool CGOpenMPRuntime::isStaticNonchunked(OpenMPScheduleClauseKind ScheduleKind, |
| 3252 | bool Chunked) const { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 3253 | OpenMPSchedType Schedule = |
| 3254 | getRuntimeSchedule(ScheduleKind, Chunked, /*Ordered=*/false); |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 3255 | return Schedule == OMP_sch_static; |
| 3256 | } |
| 3257 | |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3258 | bool CGOpenMPRuntime::isStaticNonchunked( |
| 3259 | OpenMPDistScheduleClauseKind ScheduleKind, bool Chunked) const { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 3260 | OpenMPSchedType Schedule = getRuntimeSchedule(ScheduleKind, Chunked); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3261 | return Schedule == OMP_dist_sch_static; |
| 3262 | } |
| 3263 | |
| 3264 | |
Alexander Musman | df7a8e2 | 2015-01-22 08:49:35 +0000 | [diff] [blame] | 3265 | bool CGOpenMPRuntime::isDynamic(OpenMPScheduleClauseKind ScheduleKind) const { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 3266 | OpenMPSchedType Schedule = |
Alexey Bataev | d7589ffe | 2015-05-20 13:12:48 +0000 | [diff] [blame] | 3267 | getRuntimeSchedule(ScheduleKind, /*Chunked=*/false, /*Ordered=*/false); |
Alexander Musman | df7a8e2 | 2015-01-22 08:49:35 +0000 | [diff] [blame] | 3268 | assert(Schedule != OMP_sch_static_chunked && "cannot be chunked here"); |
| 3269 | return Schedule != OMP_sch_static; |
| 3270 | } |
| 3271 | |
Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 3272 | static int addMonoNonMonoModifier(OpenMPSchedType Schedule, |
| 3273 | OpenMPScheduleClauseModifier M1, |
| 3274 | OpenMPScheduleClauseModifier M2) { |
Alexey Bataev | 6cff624 | 2016-05-30 13:05:14 +0000 | [diff] [blame] | 3275 | int Modifier = 0; |
Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 3276 | switch (M1) { |
| 3277 | case OMPC_SCHEDULE_MODIFIER_monotonic: |
Alexey Bataev | 6cff624 | 2016-05-30 13:05:14 +0000 | [diff] [blame] | 3278 | Modifier = OMP_sch_modifier_monotonic; |
| 3279 | break; |
Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 3280 | case OMPC_SCHEDULE_MODIFIER_nonmonotonic: |
Alexey Bataev | 6cff624 | 2016-05-30 13:05:14 +0000 | [diff] [blame] | 3281 | Modifier = OMP_sch_modifier_nonmonotonic; |
| 3282 | break; |
Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 3283 | case OMPC_SCHEDULE_MODIFIER_simd: |
Alexey Bataev | 6cff624 | 2016-05-30 13:05:14 +0000 | [diff] [blame] | 3284 | if (Schedule == OMP_sch_static_chunked) |
| 3285 | Schedule = OMP_sch_static_balanced_chunked; |
| 3286 | break; |
Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 3287 | case OMPC_SCHEDULE_MODIFIER_last: |
| 3288 | case OMPC_SCHEDULE_MODIFIER_unknown: |
| 3289 | break; |
| 3290 | } |
| 3291 | switch (M2) { |
| 3292 | case OMPC_SCHEDULE_MODIFIER_monotonic: |
Alexey Bataev | 6cff624 | 2016-05-30 13:05:14 +0000 | [diff] [blame] | 3293 | Modifier = OMP_sch_modifier_monotonic; |
| 3294 | break; |
Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 3295 | case OMPC_SCHEDULE_MODIFIER_nonmonotonic: |
Alexey Bataev | 6cff624 | 2016-05-30 13:05:14 +0000 | [diff] [blame] | 3296 | Modifier = OMP_sch_modifier_nonmonotonic; |
| 3297 | break; |
Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 3298 | case OMPC_SCHEDULE_MODIFIER_simd: |
Alexey Bataev | 6cff624 | 2016-05-30 13:05:14 +0000 | [diff] [blame] | 3299 | if (Schedule == OMP_sch_static_chunked) |
| 3300 | Schedule = OMP_sch_static_balanced_chunked; |
| 3301 | break; |
Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 3302 | case OMPC_SCHEDULE_MODIFIER_last: |
| 3303 | case OMPC_SCHEDULE_MODIFIER_unknown: |
| 3304 | break; |
| 3305 | } |
Alexey Bataev | 6cff624 | 2016-05-30 13:05:14 +0000 | [diff] [blame] | 3306 | return Schedule | Modifier; |
Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 3307 | } |
| 3308 | |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 3309 | void CGOpenMPRuntime::emitForDispatchInit( |
| 3310 | CodeGenFunction &CGF, SourceLocation Loc, |
| 3311 | const OpenMPScheduleTy &ScheduleKind, unsigned IVSize, bool IVSigned, |
| 3312 | bool Ordered, const DispatchRTInput &DispatchValues) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 3313 | if (!CGF.HaveInsertPoint()) |
| 3314 | return; |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 3315 | OpenMPSchedType Schedule = getRuntimeSchedule( |
| 3316 | ScheduleKind.Schedule, DispatchValues.Chunk != nullptr, Ordered); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3317 | assert(Ordered || |
| 3318 | (Schedule != OMP_sch_static && Schedule != OMP_sch_static_chunked && |
Alexey Bataev | 6cff624 | 2016-05-30 13:05:14 +0000 | [diff] [blame] | 3319 | Schedule != OMP_ord_static && Schedule != OMP_ord_static_chunked && |
| 3320 | Schedule != OMP_sch_static_balanced_chunked)); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3321 | // Call __kmpc_dispatch_init( |
| 3322 | // ident_t *loc, kmp_int32 tid, kmp_int32 schedule, |
| 3323 | // kmp_int[32|64] lower, kmp_int[32|64] upper, |
| 3324 | // kmp_int[32|64] stride, kmp_int[32|64] chunk); |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 3325 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3326 | // 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] | 3327 | llvm::Value *Chunk = DispatchValues.Chunk ? DispatchValues.Chunk |
| 3328 | : CGF.Builder.getIntN(IVSize, 1); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3329 | llvm::Value *Args[] = { |
Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 3330 | emitUpdateLocation(CGF, Loc), getThreadID(CGF, Loc), |
| 3331 | CGF.Builder.getInt32(addMonoNonMonoModifier( |
| 3332 | Schedule, ScheduleKind.M1, ScheduleKind.M2)), // Schedule type |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 3333 | DispatchValues.LB, // Lower |
| 3334 | DispatchValues.UB, // Upper |
Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 3335 | CGF.Builder.getIntN(IVSize, 1), // Stride |
| 3336 | Chunk // Chunk |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3337 | }; |
| 3338 | CGF.EmitRuntimeCall(createDispatchInitFunction(IVSize, IVSigned), Args); |
| 3339 | } |
| 3340 | |
Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 3341 | static void emitForStaticInitCall( |
| 3342 | CodeGenFunction &CGF, llvm::Value *UpdateLocation, llvm::Value *ThreadId, |
| 3343 | llvm::Constant *ForStaticInitFunction, OpenMPSchedType Schedule, |
| 3344 | OpenMPScheduleClauseModifier M1, OpenMPScheduleClauseModifier M2, |
Alexey Bataev | 0f87dbe | 2017-08-14 17:56:13 +0000 | [diff] [blame] | 3345 | const CGOpenMPRuntime::StaticRTInput &Values) { |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3346 | if (!CGF.HaveInsertPoint()) |
Alexey Bataev | 0f87dbe | 2017-08-14 17:56:13 +0000 | [diff] [blame] | 3347 | return; |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3348 | |
Alexey Bataev | 0f87dbe | 2017-08-14 17:56:13 +0000 | [diff] [blame] | 3349 | assert(!Values.Ordered); |
| 3350 | assert(Schedule == OMP_sch_static || Schedule == OMP_sch_static_chunked || |
| 3351 | Schedule == OMP_sch_static_balanced_chunked || |
| 3352 | Schedule == OMP_ord_static || Schedule == OMP_ord_static_chunked || |
| 3353 | Schedule == OMP_dist_sch_static || |
| 3354 | Schedule == OMP_dist_sch_static_chunked); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3355 | |
Alexey Bataev | 0f87dbe | 2017-08-14 17:56:13 +0000 | [diff] [blame] | 3356 | // Call __kmpc_for_static_init( |
| 3357 | // ident_t *loc, kmp_int32 tid, kmp_int32 schedtype, |
| 3358 | // kmp_int32 *p_lastiter, kmp_int[32|64] *p_lower, |
| 3359 | // kmp_int[32|64] *p_upper, kmp_int[32|64] *p_stride, |
| 3360 | // kmp_int[32|64] incr, kmp_int[32|64] chunk); |
| 3361 | llvm::Value *Chunk = Values.Chunk; |
| 3362 | if (Chunk == nullptr) { |
| 3363 | assert((Schedule == OMP_sch_static || Schedule == OMP_ord_static || |
| 3364 | Schedule == OMP_dist_sch_static) && |
| 3365 | "expected static non-chunked schedule"); |
| 3366 | // If the Chunk was not specified in the clause - use default value 1. |
| 3367 | Chunk = CGF.Builder.getIntN(Values.IVSize, 1); |
| 3368 | } else { |
| 3369 | assert((Schedule == OMP_sch_static_chunked || |
| 3370 | Schedule == OMP_sch_static_balanced_chunked || |
| 3371 | Schedule == OMP_ord_static_chunked || |
| 3372 | Schedule == OMP_dist_sch_static_chunked) && |
| 3373 | "expected static chunked schedule"); |
| 3374 | } |
| 3375 | llvm::Value *Args[] = { |
| 3376 | UpdateLocation, |
| 3377 | ThreadId, |
| 3378 | CGF.Builder.getInt32(addMonoNonMonoModifier(Schedule, M1, |
| 3379 | M2)), // Schedule type |
| 3380 | Values.IL.getPointer(), // &isLastIter |
| 3381 | Values.LB.getPointer(), // &LB |
| 3382 | Values.UB.getPointer(), // &UB |
| 3383 | Values.ST.getPointer(), // &Stride |
| 3384 | CGF.Builder.getIntN(Values.IVSize, 1), // Incr |
| 3385 | Chunk // Chunk |
| 3386 | }; |
| 3387 | CGF.EmitRuntimeCall(ForStaticInitFunction, Args); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3388 | } |
| 3389 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3390 | void CGOpenMPRuntime::emitForStaticInit(CodeGenFunction &CGF, |
| 3391 | SourceLocation Loc, |
Alexey Bataev | 0f87dbe | 2017-08-14 17:56:13 +0000 | [diff] [blame] | 3392 | OpenMPDirectiveKind DKind, |
Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 3393 | const OpenMPScheduleTy &ScheduleKind, |
Alexey Bataev | 0f87dbe | 2017-08-14 17:56:13 +0000 | [diff] [blame] | 3394 | const StaticRTInput &Values) { |
| 3395 | OpenMPSchedType ScheduleNum = getRuntimeSchedule( |
| 3396 | ScheduleKind.Schedule, Values.Chunk != nullptr, Values.Ordered); |
| 3397 | assert(isOpenMPWorksharingDirective(DKind) && |
| 3398 | "Expected loop-based or sections-based directive."); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 3399 | llvm::Value *UpdatedLocation = emitUpdateLocation(CGF, Loc, |
Alexey Bataev | 0f87dbe | 2017-08-14 17:56:13 +0000 | [diff] [blame] | 3400 | isOpenMPLoopDirective(DKind) |
| 3401 | ? OMP_IDENT_WORK_LOOP |
| 3402 | : OMP_IDENT_WORK_SECTIONS); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 3403 | llvm::Value *ThreadId = getThreadID(CGF, Loc); |
| 3404 | llvm::Constant *StaticInitFunction = |
Alexey Bataev | 0f87dbe | 2017-08-14 17:56:13 +0000 | [diff] [blame] | 3405 | createForStaticInitFunction(Values.IVSize, Values.IVSigned); |
Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 3406 | emitForStaticInitCall(CGF, UpdatedLocation, ThreadId, StaticInitFunction, |
Alexey Bataev | 0f87dbe | 2017-08-14 17:56:13 +0000 | [diff] [blame] | 3407 | ScheduleNum, ScheduleKind.M1, ScheduleKind.M2, Values); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3408 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3409 | |
Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 3410 | void CGOpenMPRuntime::emitDistributeStaticInit( |
| 3411 | CodeGenFunction &CGF, SourceLocation Loc, |
Alexey Bataev | 0f87dbe | 2017-08-14 17:56:13 +0000 | [diff] [blame] | 3412 | OpenMPDistScheduleClauseKind SchedKind, |
| 3413 | const CGOpenMPRuntime::StaticRTInput &Values) { |
| 3414 | OpenMPSchedType ScheduleNum = |
| 3415 | getRuntimeSchedule(SchedKind, Values.Chunk != nullptr); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 3416 | llvm::Value *UpdatedLocation = |
Alexey Bataev | 0f87dbe | 2017-08-14 17:56:13 +0000 | [diff] [blame] | 3417 | emitUpdateLocation(CGF, Loc, OMP_IDENT_WORK_DISTRIBUTE); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 3418 | llvm::Value *ThreadId = getThreadID(CGF, Loc); |
| 3419 | llvm::Constant *StaticInitFunction = |
Alexey Bataev | 0f87dbe | 2017-08-14 17:56:13 +0000 | [diff] [blame] | 3420 | createForStaticInitFunction(Values.IVSize, Values.IVSigned); |
Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 3421 | emitForStaticInitCall(CGF, UpdatedLocation, ThreadId, StaticInitFunction, |
| 3422 | ScheduleNum, OMPC_SCHEDULE_MODIFIER_unknown, |
Alexey Bataev | 0f87dbe | 2017-08-14 17:56:13 +0000 | [diff] [blame] | 3423 | OMPC_SCHEDULE_MODIFIER_unknown, Values); |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 3424 | } |
| 3425 | |
Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 3426 | void CGOpenMPRuntime::emitForStaticFinish(CodeGenFunction &CGF, |
Alexey Bataev | f43f714 | 2017-09-06 16:17:35 +0000 | [diff] [blame] | 3427 | SourceLocation Loc, |
| 3428 | OpenMPDirectiveKind DKind) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 3429 | if (!CGF.HaveInsertPoint()) |
| 3430 | return; |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 3431 | // Call __kmpc_for_static_fini(ident_t *loc, kmp_int32 tid); |
Alexey Bataev | f43f714 | 2017-09-06 16:17:35 +0000 | [diff] [blame] | 3432 | llvm::Value *Args[] = { |
| 3433 | emitUpdateLocation(CGF, Loc, |
| 3434 | isOpenMPDistributeDirective(DKind) |
| 3435 | ? OMP_IDENT_WORK_DISTRIBUTE |
| 3436 | : isOpenMPLoopDirective(DKind) |
| 3437 | ? OMP_IDENT_WORK_LOOP |
| 3438 | : OMP_IDENT_WORK_SECTIONS), |
| 3439 | getThreadID(CGF, Loc)}; |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 3440 | CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_for_static_fini), |
| 3441 | Args); |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 3442 | } |
| 3443 | |
Alexey Bataev | d7589ffe | 2015-05-20 13:12:48 +0000 | [diff] [blame] | 3444 | void CGOpenMPRuntime::emitForOrderedIterationEnd(CodeGenFunction &CGF, |
| 3445 | SourceLocation Loc, |
| 3446 | unsigned IVSize, |
| 3447 | bool IVSigned) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 3448 | if (!CGF.HaveInsertPoint()) |
| 3449 | return; |
Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 3450 | // 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] | 3451 | llvm::Value *Args[] = {emitUpdateLocation(CGF, Loc), getThreadID(CGF, Loc)}; |
Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 3452 | CGF.EmitRuntimeCall(createDispatchFiniFunction(IVSize, IVSigned), Args); |
| 3453 | } |
| 3454 | |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 3455 | llvm::Value *CGOpenMPRuntime::emitForNext(CodeGenFunction &CGF, |
| 3456 | SourceLocation Loc, unsigned IVSize, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3457 | bool IVSigned, Address IL, |
| 3458 | Address LB, Address UB, |
| 3459 | Address ST) { |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 3460 | // Call __kmpc_dispatch_next( |
| 3461 | // ident_t *loc, kmp_int32 tid, kmp_int32 *p_lastiter, |
| 3462 | // kmp_int[32|64] *p_lower, kmp_int[32|64] *p_upper, |
| 3463 | // kmp_int[32|64] *p_stride); |
| 3464 | llvm::Value *Args[] = { |
Alexey Bataev | 50b3c95 | 2016-02-19 10:38:26 +0000 | [diff] [blame] | 3465 | emitUpdateLocation(CGF, Loc), |
| 3466 | getThreadID(CGF, Loc), |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3467 | IL.getPointer(), // &isLastIter |
| 3468 | LB.getPointer(), // &Lower |
| 3469 | UB.getPointer(), // &Upper |
| 3470 | ST.getPointer() // &Stride |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 3471 | }; |
| 3472 | llvm::Value *Call = |
| 3473 | CGF.EmitRuntimeCall(createDispatchNextFunction(IVSize, IVSigned), Args); |
| 3474 | return CGF.EmitScalarConversion( |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 3475 | Call, CGF.getContext().getIntTypeForBitwidth(32, /*Signed=*/1), |
Filipe Cabecinhas | 7af183d | 2015-08-11 04:19:28 +0000 | [diff] [blame] | 3476 | CGF.getContext().BoolTy, Loc); |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 3477 | } |
| 3478 | |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 3479 | void CGOpenMPRuntime::emitNumThreadsClause(CodeGenFunction &CGF, |
| 3480 | llvm::Value *NumThreads, |
| 3481 | SourceLocation Loc) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 3482 | if (!CGF.HaveInsertPoint()) |
| 3483 | return; |
Alexey Bataev | b205978 | 2014-10-13 08:23:51 +0000 | [diff] [blame] | 3484 | // Build call __kmpc_push_num_threads(&loc, global_tid, num_threads) |
| 3485 | llvm::Value *Args[] = { |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 3486 | emitUpdateLocation(CGF, Loc), getThreadID(CGF, Loc), |
Alexey Bataev | b205978 | 2014-10-13 08:23:51 +0000 | [diff] [blame] | 3487 | CGF.Builder.CreateIntCast(NumThreads, CGF.Int32Ty, /*isSigned*/ true)}; |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 3488 | CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_push_num_threads), |
| 3489 | Args); |
Alexey Bataev | b205978 | 2014-10-13 08:23:51 +0000 | [diff] [blame] | 3490 | } |
| 3491 | |
Alexey Bataev | 7f210c6 | 2015-06-18 13:40:03 +0000 | [diff] [blame] | 3492 | void CGOpenMPRuntime::emitProcBindClause(CodeGenFunction &CGF, |
| 3493 | OpenMPProcBindClauseKind ProcBind, |
| 3494 | SourceLocation Loc) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 3495 | if (!CGF.HaveInsertPoint()) |
| 3496 | return; |
Alexey Bataev | 7f210c6 | 2015-06-18 13:40:03 +0000 | [diff] [blame] | 3497 | // Constants for proc bind value accepted by the runtime. |
| 3498 | enum ProcBindTy { |
| 3499 | ProcBindFalse = 0, |
| 3500 | ProcBindTrue, |
| 3501 | ProcBindMaster, |
| 3502 | ProcBindClose, |
| 3503 | ProcBindSpread, |
| 3504 | ProcBindIntel, |
| 3505 | ProcBindDefault |
| 3506 | } RuntimeProcBind; |
| 3507 | switch (ProcBind) { |
| 3508 | case OMPC_PROC_BIND_master: |
| 3509 | RuntimeProcBind = ProcBindMaster; |
| 3510 | break; |
| 3511 | case OMPC_PROC_BIND_close: |
| 3512 | RuntimeProcBind = ProcBindClose; |
| 3513 | break; |
| 3514 | case OMPC_PROC_BIND_spread: |
| 3515 | RuntimeProcBind = ProcBindSpread; |
| 3516 | break; |
| 3517 | case OMPC_PROC_BIND_unknown: |
| 3518 | llvm_unreachable("Unsupported proc_bind value."); |
| 3519 | } |
| 3520 | // Build call __kmpc_push_proc_bind(&loc, global_tid, proc_bind) |
| 3521 | llvm::Value *Args[] = { |
| 3522 | emitUpdateLocation(CGF, Loc), getThreadID(CGF, Loc), |
| 3523 | llvm::ConstantInt::get(CGM.IntTy, RuntimeProcBind, /*isSigned=*/true)}; |
| 3524 | CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_push_proc_bind), Args); |
| 3525 | } |
| 3526 | |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 3527 | void CGOpenMPRuntime::emitFlush(CodeGenFunction &CGF, ArrayRef<const Expr *>, |
| 3528 | SourceLocation Loc) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 3529 | if (!CGF.HaveInsertPoint()) |
| 3530 | return; |
Alexey Bataev | d76df6d | 2015-02-24 12:55:09 +0000 | [diff] [blame] | 3531 | // Build call void __kmpc_flush(ident_t *loc) |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 3532 | CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_flush), |
| 3533 | emitUpdateLocation(CGF, Loc)); |
Alexey Bataev | cc37cc1 | 2014-11-20 04:34:54 +0000 | [diff] [blame] | 3534 | } |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 3535 | |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 3536 | namespace { |
| 3537 | /// \brief Indexes of fields for type kmp_task_t. |
| 3538 | enum KmpTaskTFields { |
| 3539 | /// \brief List of shared variables. |
| 3540 | KmpTaskTShareds, |
| 3541 | /// \brief Task routine. |
| 3542 | KmpTaskTRoutine, |
| 3543 | /// \brief Partition id for the untied tasks. |
| 3544 | KmpTaskTPartId, |
Alexey Bataev | ad537bb | 2016-05-30 09:06:50 +0000 | [diff] [blame] | 3545 | /// Function with call of destructors for private variables. |
| 3546 | Data1, |
| 3547 | /// Task priority. |
| 3548 | Data2, |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 3549 | /// (Taskloops only) Lower bound. |
| 3550 | KmpTaskTLowerBound, |
| 3551 | /// (Taskloops only) Upper bound. |
| 3552 | KmpTaskTUpperBound, |
| 3553 | /// (Taskloops only) Stride. |
| 3554 | KmpTaskTStride, |
| 3555 | /// (Taskloops only) Is last iteration flag. |
| 3556 | KmpTaskTLastIter, |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 3557 | /// (Taskloops only) Reduction data. |
| 3558 | KmpTaskTReductions, |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 3559 | }; |
Hans Wennborg | 7eb5464 | 2015-09-10 17:07:54 +0000 | [diff] [blame] | 3560 | } // anonymous namespace |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 3561 | |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3562 | bool CGOpenMPRuntime::OffloadEntriesInfoManagerTy::empty() const { |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 3563 | return OffloadEntriesTargetRegion.empty() && |
| 3564 | OffloadEntriesDeviceGlobalVar.empty(); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3565 | } |
| 3566 | |
| 3567 | /// \brief Initialize target region entry. |
| 3568 | void CGOpenMPRuntime::OffloadEntriesInfoManagerTy:: |
| 3569 | initializeTargetRegionEntryInfo(unsigned DeviceID, unsigned FileID, |
| 3570 | StringRef ParentName, unsigned LineNum, |
Samuel Antao | 2de62b0 | 2016-02-13 23:35:10 +0000 | [diff] [blame] | 3571 | unsigned Order) { |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3572 | assert(CGM.getLangOpts().OpenMPIsDevice && "Initialization of entries is " |
| 3573 | "only required for the device " |
| 3574 | "code generation."); |
Samuel Antao | 2de62b0 | 2016-02-13 23:35:10 +0000 | [diff] [blame] | 3575 | OffloadEntriesTargetRegion[DeviceID][FileID][ParentName][LineNum] = |
Samuel Antao | f83efdb | 2017-01-05 16:02:49 +0000 | [diff] [blame] | 3576 | OffloadEntryInfoTargetRegion(Order, /*Addr=*/nullptr, /*ID=*/nullptr, |
Alexey Bataev | 34f8a70 | 2018-03-28 14:28:54 +0000 | [diff] [blame] | 3577 | OMPTargetRegionEntryTargetRegion); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3578 | ++OffloadingEntriesNum; |
| 3579 | } |
| 3580 | |
| 3581 | void CGOpenMPRuntime::OffloadEntriesInfoManagerTy:: |
| 3582 | registerTargetRegionEntryInfo(unsigned DeviceID, unsigned FileID, |
| 3583 | StringRef ParentName, unsigned LineNum, |
Samuel Antao | f83efdb | 2017-01-05 16:02:49 +0000 | [diff] [blame] | 3584 | llvm::Constant *Addr, llvm::Constant *ID, |
Alexey Bataev | 34f8a70 | 2018-03-28 14:28:54 +0000 | [diff] [blame] | 3585 | OMPTargetRegionEntryKind Flags) { |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3586 | // If we are emitting code for a target, the entry is already initialized, |
| 3587 | // only has to be registered. |
| 3588 | if (CGM.getLangOpts().OpenMPIsDevice) { |
Samuel Antao | 2de62b0 | 2016-02-13 23:35:10 +0000 | [diff] [blame] | 3589 | assert(hasTargetRegionEntryInfo(DeviceID, FileID, ParentName, LineNum) && |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3590 | "Entry must exist."); |
Samuel Antao | 2de62b0 | 2016-02-13 23:35:10 +0000 | [diff] [blame] | 3591 | auto &Entry = |
| 3592 | OffloadEntriesTargetRegion[DeviceID][FileID][ParentName][LineNum]; |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3593 | assert(Entry.isValid() && "Entry not initialized!"); |
| 3594 | Entry.setAddress(Addr); |
| 3595 | Entry.setID(ID); |
Samuel Antao | f83efdb | 2017-01-05 16:02:49 +0000 | [diff] [blame] | 3596 | Entry.setFlags(Flags); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3597 | } else { |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 3598 | OffloadEntryInfoTargetRegion Entry(OffloadingEntriesNum, Addr, ID, Flags); |
Samuel Antao | 2de62b0 | 2016-02-13 23:35:10 +0000 | [diff] [blame] | 3599 | OffloadEntriesTargetRegion[DeviceID][FileID][ParentName][LineNum] = Entry; |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 3600 | ++OffloadingEntriesNum; |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3601 | } |
| 3602 | } |
| 3603 | |
| 3604 | bool CGOpenMPRuntime::OffloadEntriesInfoManagerTy::hasTargetRegionEntryInfo( |
Samuel Antao | 2de62b0 | 2016-02-13 23:35:10 +0000 | [diff] [blame] | 3605 | unsigned DeviceID, unsigned FileID, StringRef ParentName, |
| 3606 | unsigned LineNum) const { |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3607 | auto PerDevice = OffloadEntriesTargetRegion.find(DeviceID); |
| 3608 | if (PerDevice == OffloadEntriesTargetRegion.end()) |
| 3609 | return false; |
| 3610 | auto PerFile = PerDevice->second.find(FileID); |
| 3611 | if (PerFile == PerDevice->second.end()) |
| 3612 | return false; |
| 3613 | auto PerParentName = PerFile->second.find(ParentName); |
| 3614 | if (PerParentName == PerFile->second.end()) |
| 3615 | return false; |
| 3616 | auto PerLine = PerParentName->second.find(LineNum); |
| 3617 | if (PerLine == PerParentName->second.end()) |
| 3618 | return false; |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3619 | // Fail if this entry is already registered. |
Samuel Antao | 2de62b0 | 2016-02-13 23:35:10 +0000 | [diff] [blame] | 3620 | if (PerLine->second.getAddress() || PerLine->second.getID()) |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3621 | return false; |
| 3622 | return true; |
| 3623 | } |
| 3624 | |
| 3625 | void CGOpenMPRuntime::OffloadEntriesInfoManagerTy::actOnTargetRegionEntriesInfo( |
| 3626 | const OffloadTargetRegionEntryInfoActTy &Action) { |
| 3627 | // Scan all target region entries and perform the provided action. |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 3628 | for (const auto &D : OffloadEntriesTargetRegion) |
| 3629 | for (const auto &F : D.second) |
| 3630 | for (const auto &P : F.second) |
| 3631 | for (const auto &L : P.second) |
Samuel Antao | 2de62b0 | 2016-02-13 23:35:10 +0000 | [diff] [blame] | 3632 | Action(D.first, F.first, P.first(), L.first, L.second); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3633 | } |
| 3634 | |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 3635 | void CGOpenMPRuntime::OffloadEntriesInfoManagerTy:: |
| 3636 | initializeDeviceGlobalVarEntryInfo(StringRef Name, |
| 3637 | OMPTargetGlobalVarEntryKind Flags, |
| 3638 | unsigned Order) { |
| 3639 | assert(CGM.getLangOpts().OpenMPIsDevice && "Initialization of entries is " |
| 3640 | "only required for the device " |
| 3641 | "code generation."); |
| 3642 | OffloadEntriesDeviceGlobalVar.try_emplace(Name, Order, Flags); |
| 3643 | ++OffloadingEntriesNum; |
| 3644 | } |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3645 | |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 3646 | void CGOpenMPRuntime::OffloadEntriesInfoManagerTy:: |
| 3647 | registerDeviceGlobalVarEntryInfo(StringRef VarName, llvm::Constant *Addr, |
| 3648 | CharUnits VarSize, |
| 3649 | OMPTargetGlobalVarEntryKind Flags, |
| 3650 | llvm::GlobalValue::LinkageTypes Linkage) { |
| 3651 | if (CGM.getLangOpts().OpenMPIsDevice) { |
| 3652 | auto &Entry = OffloadEntriesDeviceGlobalVar[VarName]; |
| 3653 | assert(Entry.isValid() && Entry.getFlags() == Flags && |
| 3654 | "Entry not initialized!"); |
| 3655 | assert((!Entry.getAddress() || Entry.getAddress() == Addr) && |
| 3656 | "Resetting with the new address."); |
| 3657 | if (Entry.getAddress() && hasDeviceGlobalVarEntryInfo(VarName)) |
| 3658 | return; |
| 3659 | Entry.setAddress(Addr); |
| 3660 | Entry.setVarSize(VarSize); |
| 3661 | Entry.setLinkage(Linkage); |
| 3662 | } else { |
| 3663 | if (hasDeviceGlobalVarEntryInfo(VarName)) |
| 3664 | return; |
| 3665 | OffloadEntriesDeviceGlobalVar.try_emplace( |
| 3666 | VarName, OffloadingEntriesNum, Addr, VarSize, Flags, Linkage); |
| 3667 | ++OffloadingEntriesNum; |
| 3668 | } |
| 3669 | } |
| 3670 | |
| 3671 | void CGOpenMPRuntime::OffloadEntriesInfoManagerTy:: |
| 3672 | actOnDeviceGlobalVarEntriesInfo( |
| 3673 | const OffloadDeviceGlobalVarEntryInfoActTy &Action) { |
| 3674 | // Scan all target region entries and perform the provided action. |
| 3675 | for (const auto &E : OffloadEntriesDeviceGlobalVar) |
| 3676 | Action(E.getKey(), E.getValue()); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3677 | } |
| 3678 | |
| 3679 | llvm::Function * |
| 3680 | CGOpenMPRuntime::createOffloadingBinaryDescriptorRegistration() { |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3681 | // If we don't have entries or if we are emitting code for the device, we |
| 3682 | // don't need to do anything. |
| 3683 | if (CGM.getLangOpts().OpenMPIsDevice || OffloadEntriesInfoManager.empty()) |
| 3684 | return nullptr; |
| 3685 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 3686 | llvm::Module &M = CGM.getModule(); |
| 3687 | ASTContext &C = CGM.getContext(); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3688 | |
| 3689 | // Get list of devices we care about |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 3690 | const std::vector<llvm::Triple> &Devices = CGM.getLangOpts().OMPTargetTriples; |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3691 | |
| 3692 | // We should be creating an offloading descriptor only if there are devices |
| 3693 | // specified. |
| 3694 | assert(!Devices.empty() && "No OpenMP offloading devices??"); |
| 3695 | |
| 3696 | // Create the external variables that will point to the begin and end of the |
| 3697 | // host entries section. These will be defined by the linker. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 3698 | llvm::Type *OffloadEntryTy = |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3699 | CGM.getTypes().ConvertTypeForMem(getTgtOffloadEntryQTy()); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 3700 | auto *HostEntriesBegin = new llvm::GlobalVariable( |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3701 | M, OffloadEntryTy, /*isConstant=*/true, |
Eugene Zelenko | 1660a5d | 2016-01-26 19:01:06 +0000 | [diff] [blame] | 3702 | llvm::GlobalValue::ExternalLinkage, /*Initializer=*/nullptr, |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3703 | ".omp_offloading.entries_begin"); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 3704 | auto *HostEntriesEnd = new llvm::GlobalVariable( |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3705 | M, OffloadEntryTy, /*isConstant=*/true, |
Eugene Zelenko | 1660a5d | 2016-01-26 19:01:06 +0000 | [diff] [blame] | 3706 | llvm::GlobalValue::ExternalLinkage, /*Initializer=*/nullptr, |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3707 | ".omp_offloading.entries_end"); |
| 3708 | |
| 3709 | // Create all device images |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3710 | auto *DeviceImageTy = cast<llvm::StructType>( |
| 3711 | CGM.getTypes().ConvertTypeForMem(getTgtDeviceImageQTy())); |
John McCall | 23c9dc6 | 2016-11-28 22:18:27 +0000 | [diff] [blame] | 3712 | ConstantInitBuilder DeviceImagesBuilder(CGM); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 3713 | ConstantArrayBuilder DeviceImagesEntries = |
| 3714 | DeviceImagesBuilder.beginArray(DeviceImageTy); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3715 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 3716 | for (const llvm::Triple &Device : Devices) { |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 3717 | StringRef T = Device.getTriple(); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3718 | auto *ImgBegin = new llvm::GlobalVariable( |
| 3719 | M, CGM.Int8Ty, /*isConstant=*/true, llvm::GlobalValue::ExternalLinkage, |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 3720 | /*Initializer=*/nullptr, Twine(".omp_offloading.img_start.", T)); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3721 | auto *ImgEnd = new llvm::GlobalVariable( |
| 3722 | M, CGM.Int8Ty, /*isConstant=*/true, llvm::GlobalValue::ExternalLinkage, |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 3723 | /*Initializer=*/nullptr, Twine(".omp_offloading.img_end.", T)); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3724 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 3725 | llvm::Constant *Data[] = {ImgBegin, ImgEnd, HostEntriesBegin, |
| 3726 | HostEntriesEnd}; |
| 3727 | createConstantGlobalStructAndAddToParent(CGM, getTgtDeviceImageQTy(), Data, |
| 3728 | DeviceImagesEntries); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3729 | } |
| 3730 | |
| 3731 | // Create device images global array. |
John McCall | 6c9f1fdb | 2016-11-19 08:17:24 +0000 | [diff] [blame] | 3732 | llvm::GlobalVariable *DeviceImages = |
| 3733 | DeviceImagesEntries.finishAndCreateGlobal(".omp_offloading.device_images", |
| 3734 | CGM.getPointerAlign(), |
| 3735 | /*isConstant=*/true); |
Peter Collingbourne | bcf909d | 2016-06-14 21:02:05 +0000 | [diff] [blame] | 3736 | DeviceImages->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3737 | |
| 3738 | // This is a Zero array to be used in the creation of the constant expressions |
| 3739 | llvm::Constant *Index[] = {llvm::Constant::getNullValue(CGM.Int32Ty), |
| 3740 | llvm::Constant::getNullValue(CGM.Int32Ty)}; |
| 3741 | |
| 3742 | // Create the target region descriptor. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 3743 | llvm::Constant *Data[] = { |
| 3744 | llvm::ConstantInt::get(CGM.Int32Ty, Devices.size()), |
| 3745 | llvm::ConstantExpr::getGetElementPtr(DeviceImages->getValueType(), |
| 3746 | DeviceImages, Index), |
| 3747 | HostEntriesBegin, HostEntriesEnd}; |
| 3748 | llvm::GlobalVariable *Desc = createConstantGlobalStruct( |
| 3749 | CGM, getTgtBinaryDescriptorQTy(), Data, ".omp_offloading.descriptor"); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3750 | |
| 3751 | // Emit code to register or unregister the descriptor at execution |
| 3752 | // startup or closing, respectively. |
| 3753 | |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 3754 | llvm::Function *UnRegFn; |
| 3755 | { |
| 3756 | FunctionArgList Args; |
| 3757 | ImplicitParamDecl DummyPtr(C, C.VoidPtrTy, ImplicitParamDecl::Other); |
| 3758 | Args.push_back(&DummyPtr); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3759 | |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 3760 | CodeGenFunction CGF(CGM); |
| 3761 | // Disable debug info for global (de-)initializer because they are not part |
| 3762 | // of some particular construct. |
| 3763 | CGF.disableDebugInfo(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 3764 | const auto &FI = |
| 3765 | CGM.getTypes().arrangeBuiltinFunctionDeclaration(C.VoidTy, Args); |
| 3766 | llvm::FunctionType *FTy = CGM.getTypes().GetFunctionType(FI); |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 3767 | UnRegFn = CGM.CreateGlobalInitOrDestructFunction( |
| 3768 | FTy, ".omp_offloading.descriptor_unreg", FI); |
| 3769 | CGF.StartFunction(GlobalDecl(), C.VoidTy, UnRegFn, FI, Args); |
| 3770 | CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__tgt_unregister_lib), |
| 3771 | Desc); |
| 3772 | CGF.FinishFunction(); |
| 3773 | } |
| 3774 | llvm::Function *RegFn; |
| 3775 | { |
| 3776 | CodeGenFunction CGF(CGM); |
| 3777 | // Disable debug info for global (de-)initializer because they are not part |
| 3778 | // of some particular construct. |
| 3779 | CGF.disableDebugInfo(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 3780 | const auto &FI = CGM.getTypes().arrangeNullaryFunction(); |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 3781 | llvm::FunctionType *FTy = CGM.getTypes().GetFunctionType(FI); |
| 3782 | RegFn = CGM.CreateGlobalInitOrDestructFunction( |
| 3783 | FTy, ".omp_offloading.descriptor_reg", FI); |
| 3784 | CGF.StartFunction(GlobalDecl(), C.VoidTy, RegFn, FI, FunctionArgList()); |
| 3785 | CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__tgt_register_lib), Desc); |
| 3786 | // Create a variable to drive the registration and unregistration of the |
| 3787 | // descriptor, so we can reuse the logic that emits Ctors and Dtors. |
| 3788 | ImplicitParamDecl RegUnregVar(C, C.getTranslationUnitDecl(), |
| 3789 | SourceLocation(), nullptr, C.CharTy, |
| 3790 | ImplicitParamDecl::Other); |
| 3791 | CGM.getCXXABI().registerGlobalDtor(CGF, RegUnregVar, UnRegFn, Desc); |
| 3792 | CGF.FinishFunction(); |
| 3793 | } |
George Rokos | 29d0f00 | 2017-05-27 03:03:13 +0000 | [diff] [blame] | 3794 | if (CGM.supportsCOMDAT()) { |
| 3795 | // It is sufficient to call registration function only once, so create a |
| 3796 | // COMDAT group for registration/unregistration functions and associated |
| 3797 | // data. That would reduce startup time and code size. Registration |
| 3798 | // function serves as a COMDAT group key. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 3799 | llvm::Comdat *ComdatKey = M.getOrInsertComdat(RegFn->getName()); |
George Rokos | 29d0f00 | 2017-05-27 03:03:13 +0000 | [diff] [blame] | 3800 | RegFn->setLinkage(llvm::GlobalValue::LinkOnceAnyLinkage); |
| 3801 | RegFn->setVisibility(llvm::GlobalValue::HiddenVisibility); |
| 3802 | RegFn->setComdat(ComdatKey); |
| 3803 | UnRegFn->setComdat(ComdatKey); |
| 3804 | DeviceImages->setComdat(ComdatKey); |
| 3805 | Desc->setComdat(ComdatKey); |
| 3806 | } |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3807 | return RegFn; |
| 3808 | } |
| 3809 | |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 3810 | void CGOpenMPRuntime::createOffloadEntry( |
| 3811 | llvm::Constant *ID, llvm::Constant *Addr, uint64_t Size, int32_t Flags, |
| 3812 | llvm::GlobalValue::LinkageTypes Linkage) { |
Samuel Antao | 2de62b0 | 2016-02-13 23:35:10 +0000 | [diff] [blame] | 3813 | StringRef Name = Addr->getName(); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3814 | llvm::Module &M = CGM.getModule(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 3815 | llvm::LLVMContext &C = M.getContext(); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3816 | |
| 3817 | // Create constant string with the name. |
| 3818 | llvm::Constant *StrPtrInit = llvm::ConstantDataArray::getString(C, Name); |
| 3819 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 3820 | auto *Str = |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3821 | new llvm::GlobalVariable(M, StrPtrInit->getType(), /*isConstant=*/true, |
| 3822 | llvm::GlobalValue::InternalLinkage, StrPtrInit, |
| 3823 | ".omp_offloading.entry_name"); |
Peter Collingbourne | bcf909d | 2016-06-14 21:02:05 +0000 | [diff] [blame] | 3824 | Str->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3825 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 3826 | llvm::Constant *Data[] = {llvm::ConstantExpr::getBitCast(ID, CGM.VoidPtrTy), |
| 3827 | llvm::ConstantExpr::getBitCast(Str, CGM.Int8PtrTy), |
| 3828 | llvm::ConstantInt::get(CGM.SizeTy, Size), |
| 3829 | llvm::ConstantInt::get(CGM.Int32Ty, Flags), |
| 3830 | llvm::ConstantInt::get(CGM.Int32Ty, 0)}; |
| 3831 | llvm::GlobalVariable *Entry = createConstantGlobalStruct( |
| 3832 | CGM, getTgtOffloadEntryQTy(), Data, Twine(".omp_offloading.entry.", Name), |
| 3833 | Linkage); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3834 | |
| 3835 | // The entry has to be created in the section the linker expects it to be. |
| 3836 | Entry->setSection(".omp_offloading.entries"); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3837 | } |
| 3838 | |
| 3839 | void CGOpenMPRuntime::createOffloadEntriesAndInfoMetadata() { |
| 3840 | // Emit the offloading entries and metadata so that the device codegen side |
Samuel Antao | 4c8035b | 2016-12-12 18:00:20 +0000 | [diff] [blame] | 3841 | // can easily figure out what to emit. The produced metadata looks like |
| 3842 | // this: |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3843 | // |
| 3844 | // !omp_offload.info = !{!1, ...} |
| 3845 | // |
| 3846 | // Right now we only generate metadata for function that contain target |
| 3847 | // regions. |
| 3848 | |
Alexander Kornienko | 2a8c18d | 2018-04-06 15:14:32 +0000 | [diff] [blame] | 3849 | // If we do not have entries, we don't need to do anything. |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3850 | if (OffloadEntriesInfoManager.empty()) |
| 3851 | return; |
| 3852 | |
| 3853 | llvm::Module &M = CGM.getModule(); |
| 3854 | llvm::LLVMContext &C = M.getContext(); |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 3855 | SmallVector<const OffloadEntriesInfoManagerTy::OffloadEntryInfo *, 16> |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3856 | OrderedEntries(OffloadEntriesInfoManager.size()); |
| 3857 | |
Simon Pilgrim | 2c51880 | 2017-03-30 14:13:19 +0000 | [diff] [blame] | 3858 | // Auxiliary methods to create metadata values and strings. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 3859 | auto &&GetMDInt = [this](unsigned V) { |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3860 | return llvm::ConstantAsMetadata::get( |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 3861 | llvm::ConstantInt::get(CGM.Int32Ty, V)); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3862 | }; |
| 3863 | |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 3864 | auto &&GetMDString = [&C](StringRef V) { return llvm::MDString::get(C, V); }; |
| 3865 | |
| 3866 | // Create the offloading info metadata node. |
| 3867 | llvm::NamedMDNode *MD = M.getOrInsertNamedMetadata("omp_offload.info"); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3868 | |
| 3869 | // Create function that emits metadata for each target region entry; |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 3870 | auto &&TargetRegionMetadataEmitter = |
| 3871 | [&C, MD, &OrderedEntries, &GetMDInt, &GetMDString]( |
| 3872 | unsigned DeviceID, unsigned FileID, StringRef ParentName, |
| 3873 | unsigned Line, |
| 3874 | const OffloadEntriesInfoManagerTy::OffloadEntryInfoTargetRegion &E) { |
| 3875 | // Generate metadata for target regions. Each entry of this metadata |
| 3876 | // contains: |
| 3877 | // - Entry 0 -> Kind of this type of metadata (0). |
| 3878 | // - Entry 1 -> Device ID of the file where the entry was identified. |
| 3879 | // - Entry 2 -> File ID of the file where the entry was identified. |
| 3880 | // - Entry 3 -> Mangled name of the function where the entry was |
| 3881 | // identified. |
| 3882 | // - Entry 4 -> Line in the file where the entry was identified. |
| 3883 | // - Entry 5 -> Order the entry was created. |
| 3884 | // The first element of the metadata node is the kind. |
| 3885 | llvm::Metadata *Ops[] = {GetMDInt(E.getKind()), GetMDInt(DeviceID), |
| 3886 | GetMDInt(FileID), GetMDString(ParentName), |
| 3887 | GetMDInt(Line), GetMDInt(E.getOrder())}; |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3888 | |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 3889 | // Save this entry in the right position of the ordered entries array. |
| 3890 | OrderedEntries[E.getOrder()] = &E; |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3891 | |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 3892 | // Add metadata to the named metadata node. |
| 3893 | MD->addOperand(llvm::MDNode::get(C, Ops)); |
| 3894 | }; |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3895 | |
| 3896 | OffloadEntriesInfoManager.actOnTargetRegionEntriesInfo( |
| 3897 | TargetRegionMetadataEmitter); |
| 3898 | |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 3899 | // Create function that emits metadata for each device global variable entry; |
| 3900 | auto &&DeviceGlobalVarMetadataEmitter = |
| 3901 | [&C, &OrderedEntries, &GetMDInt, &GetMDString, |
| 3902 | MD](StringRef MangledName, |
| 3903 | const OffloadEntriesInfoManagerTy::OffloadEntryInfoDeviceGlobalVar |
| 3904 | &E) { |
| 3905 | // Generate metadata for global variables. Each entry of this metadata |
| 3906 | // contains: |
| 3907 | // - Entry 0 -> Kind of this type of metadata (1). |
| 3908 | // - Entry 1 -> Mangled name of the variable. |
| 3909 | // - Entry 2 -> Declare target kind. |
| 3910 | // - Entry 3 -> Order the entry was created. |
| 3911 | // The first element of the metadata node is the kind. |
| 3912 | llvm::Metadata *Ops[] = { |
| 3913 | GetMDInt(E.getKind()), GetMDString(MangledName), |
| 3914 | GetMDInt(E.getFlags()), GetMDInt(E.getOrder())}; |
| 3915 | |
| 3916 | // Save this entry in the right position of the ordered entries array. |
| 3917 | OrderedEntries[E.getOrder()] = &E; |
| 3918 | |
| 3919 | // Add metadata to the named metadata node. |
| 3920 | MD->addOperand(llvm::MDNode::get(C, Ops)); |
| 3921 | }; |
| 3922 | |
| 3923 | OffloadEntriesInfoManager.actOnDeviceGlobalVarEntriesInfo( |
| 3924 | DeviceGlobalVarMetadataEmitter); |
| 3925 | |
| 3926 | for (const auto *E : OrderedEntries) { |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3927 | assert(E && "All ordered entries must exist!"); |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 3928 | if (const auto *CE = |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3929 | dyn_cast<OffloadEntriesInfoManagerTy::OffloadEntryInfoTargetRegion>( |
| 3930 | E)) { |
| 3931 | assert(CE->getID() && CE->getAddress() && |
| 3932 | "Entry ID and Addr are invalid!"); |
Alexey Bataev | 34f8a70 | 2018-03-28 14:28:54 +0000 | [diff] [blame] | 3933 | createOffloadEntry(CE->getID(), CE->getAddress(), /*Size=*/0, |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 3934 | CE->getFlags(), llvm::GlobalValue::WeakAnyLinkage); |
| 3935 | } else if (const auto *CE = |
| 3936 | dyn_cast<OffloadEntriesInfoManagerTy:: |
| 3937 | OffloadEntryInfoDeviceGlobalVar>(E)) { |
| 3938 | assert(CE->getAddress() && "Entry Addr is invalid!"); |
| 3939 | createOffloadEntry(CE->getAddress(), CE->getAddress(), |
| 3940 | CE->getVarSize().getQuantity(), CE->getFlags(), |
| 3941 | CE->getLinkage()); |
| 3942 | } else { |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3943 | llvm_unreachable("Unsupported entry kind."); |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 3944 | } |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3945 | } |
| 3946 | } |
| 3947 | |
| 3948 | /// \brief Loads all the offload entries information from the host IR |
| 3949 | /// metadata. |
| 3950 | void CGOpenMPRuntime::loadOffloadInfoMetadata() { |
| 3951 | // If we are in target mode, load the metadata from the host IR. This code has |
| 3952 | // to match the metadaata creation in createOffloadEntriesAndInfoMetadata(). |
| 3953 | |
| 3954 | if (!CGM.getLangOpts().OpenMPIsDevice) |
| 3955 | return; |
| 3956 | |
| 3957 | if (CGM.getLangOpts().OMPHostIRFile.empty()) |
| 3958 | return; |
| 3959 | |
| 3960 | auto Buf = llvm::MemoryBuffer::getFile(CGM.getLangOpts().OMPHostIRFile); |
| 3961 | if (Buf.getError()) |
| 3962 | return; |
| 3963 | |
| 3964 | llvm::LLVMContext C; |
Peter Collingbourne | d9445c4 | 2016-11-13 07:00:17 +0000 | [diff] [blame] | 3965 | auto ME = expectedToErrorOrAndEmitErrors( |
| 3966 | C, llvm::parseBitcodeFile(Buf.get()->getMemBufferRef(), C)); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3967 | |
| 3968 | if (ME.getError()) |
| 3969 | return; |
| 3970 | |
| 3971 | llvm::NamedMDNode *MD = ME.get()->getNamedMetadata("omp_offload.info"); |
| 3972 | if (!MD) |
| 3973 | return; |
| 3974 | |
George Burgess IV | 00f70bd | 2018-03-01 05:43:23 +0000 | [diff] [blame] | 3975 | for (llvm::MDNode *MN : MD->operands()) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 3976 | auto &&GetMDInt = [MN](unsigned Idx) { |
| 3977 | auto *V = cast<llvm::ConstantAsMetadata>(MN->getOperand(Idx)); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3978 | return cast<llvm::ConstantInt>(V->getValue())->getZExtValue(); |
| 3979 | }; |
| 3980 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 3981 | auto &&GetMDString = [MN](unsigned Idx) { |
| 3982 | auto *V = cast<llvm::MDString>(MN->getOperand(Idx)); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3983 | return V->getString(); |
| 3984 | }; |
| 3985 | |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 3986 | switch (GetMDInt(0)) { |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3987 | default: |
| 3988 | llvm_unreachable("Unexpected metadata!"); |
| 3989 | break; |
| 3990 | case OffloadEntriesInfoManagerTy::OffloadEntryInfo:: |
Alexey Bataev | 34f8a70 | 2018-03-28 14:28:54 +0000 | [diff] [blame] | 3991 | OffloadingEntryInfoTargetRegion: |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3992 | OffloadEntriesInfoManager.initializeTargetRegionEntryInfo( |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 3993 | /*DeviceID=*/GetMDInt(1), /*FileID=*/GetMDInt(2), |
| 3994 | /*ParentName=*/GetMDString(3), /*Line=*/GetMDInt(4), |
| 3995 | /*Order=*/GetMDInt(5)); |
| 3996 | break; |
| 3997 | case OffloadEntriesInfoManagerTy::OffloadEntryInfo:: |
| 3998 | OffloadingEntryInfoDeviceGlobalVar: |
| 3999 | OffloadEntriesInfoManager.initializeDeviceGlobalVarEntryInfo( |
| 4000 | /*MangledName=*/GetMDString(1), |
| 4001 | static_cast<OffloadEntriesInfoManagerTy::OMPTargetGlobalVarEntryKind>( |
| 4002 | /*Flags=*/GetMDInt(2)), |
| 4003 | /*Order=*/GetMDInt(3)); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4004 | break; |
| 4005 | } |
| 4006 | } |
| 4007 | } |
| 4008 | |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 4009 | void CGOpenMPRuntime::emitKmpRoutineEntryT(QualType KmpInt32Ty) { |
| 4010 | if (!KmpRoutineEntryPtrTy) { |
| 4011 | // Build typedef kmp_int32 (* kmp_routine_entry_t)(kmp_int32, void *); type. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 4012 | ASTContext &C = CGM.getContext(); |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 4013 | QualType KmpRoutineEntryTyArgs[] = {KmpInt32Ty, C.VoidPtrTy}; |
| 4014 | FunctionProtoType::ExtProtoInfo EPI; |
| 4015 | KmpRoutineEntryPtrQTy = C.getPointerType( |
| 4016 | C.getFunctionType(KmpInt32Ty, KmpRoutineEntryTyArgs, EPI)); |
| 4017 | KmpRoutineEntryPtrTy = CGM.getTypes().ConvertType(KmpRoutineEntryPtrQTy); |
| 4018 | } |
| 4019 | } |
| 4020 | |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4021 | QualType CGOpenMPRuntime::getTgtOffloadEntryQTy() { |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4022 | // Make sure the type of the entry is already created. This is the type we |
| 4023 | // have to create: |
| 4024 | // struct __tgt_offload_entry{ |
| 4025 | // void *addr; // Pointer to the offload entry info. |
| 4026 | // // (function or global) |
| 4027 | // char *name; // Name of the function or global. |
| 4028 | // 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] | 4029 | // int32_t flags; // Flags associated with the entry, e.g. 'link'. |
| 4030 | // int32_t reserved; // Reserved, to use by the runtime library. |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4031 | // }; |
| 4032 | if (TgtOffloadEntryQTy.isNull()) { |
| 4033 | ASTContext &C = CGM.getContext(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 4034 | RecordDecl *RD = C.buildImplicitRecord("__tgt_offload_entry"); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4035 | RD->startDefinition(); |
| 4036 | addFieldToRecordDecl(C, RD, C.VoidPtrTy); |
| 4037 | addFieldToRecordDecl(C, RD, C.getPointerType(C.CharTy)); |
| 4038 | addFieldToRecordDecl(C, RD, C.getSizeType()); |
Samuel Antao | f83efdb | 2017-01-05 16:02:49 +0000 | [diff] [blame] | 4039 | addFieldToRecordDecl( |
| 4040 | C, RD, C.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/true)); |
| 4041 | addFieldToRecordDecl( |
| 4042 | C, RD, C.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/true)); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4043 | RD->completeDefinition(); |
Jonas Hahnfeld | 5e4df28 | 2018-01-18 15:38:03 +0000 | [diff] [blame] | 4044 | RD->addAttr(PackedAttr::CreateImplicit(C)); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4045 | TgtOffloadEntryQTy = C.getRecordType(RD); |
| 4046 | } |
| 4047 | return TgtOffloadEntryQTy; |
| 4048 | } |
| 4049 | |
| 4050 | QualType CGOpenMPRuntime::getTgtDeviceImageQTy() { |
| 4051 | // These are the types we need to build: |
| 4052 | // struct __tgt_device_image{ |
| 4053 | // void *ImageStart; // Pointer to the target code start. |
| 4054 | // void *ImageEnd; // Pointer to the target code end. |
| 4055 | // // We also add the host entries to the device image, as it may be useful |
| 4056 | // // for the target runtime to have access to that information. |
| 4057 | // __tgt_offload_entry *EntriesBegin; // Begin of the table with all |
| 4058 | // // the entries. |
| 4059 | // __tgt_offload_entry *EntriesEnd; // End of the table with all the |
| 4060 | // // entries (non inclusive). |
| 4061 | // }; |
| 4062 | if (TgtDeviceImageQTy.isNull()) { |
| 4063 | ASTContext &C = CGM.getContext(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 4064 | RecordDecl *RD = C.buildImplicitRecord("__tgt_device_image"); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4065 | RD->startDefinition(); |
| 4066 | addFieldToRecordDecl(C, RD, C.VoidPtrTy); |
| 4067 | addFieldToRecordDecl(C, RD, C.VoidPtrTy); |
| 4068 | addFieldToRecordDecl(C, RD, C.getPointerType(getTgtOffloadEntryQTy())); |
| 4069 | addFieldToRecordDecl(C, RD, C.getPointerType(getTgtOffloadEntryQTy())); |
| 4070 | RD->completeDefinition(); |
| 4071 | TgtDeviceImageQTy = C.getRecordType(RD); |
| 4072 | } |
| 4073 | return TgtDeviceImageQTy; |
| 4074 | } |
| 4075 | |
| 4076 | QualType CGOpenMPRuntime::getTgtBinaryDescriptorQTy() { |
| 4077 | // struct __tgt_bin_desc{ |
| 4078 | // int32_t NumDevices; // Number of devices supported. |
| 4079 | // __tgt_device_image *DeviceImages; // Arrays of device images |
| 4080 | // // (one per device). |
| 4081 | // __tgt_offload_entry *EntriesBegin; // Begin of the table with all the |
| 4082 | // // entries. |
| 4083 | // __tgt_offload_entry *EntriesEnd; // End of the table with all the |
| 4084 | // // entries (non inclusive). |
| 4085 | // }; |
| 4086 | if (TgtBinaryDescriptorQTy.isNull()) { |
| 4087 | ASTContext &C = CGM.getContext(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 4088 | RecordDecl *RD = C.buildImplicitRecord("__tgt_bin_desc"); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4089 | RD->startDefinition(); |
| 4090 | addFieldToRecordDecl( |
| 4091 | C, RD, C.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/true)); |
| 4092 | addFieldToRecordDecl(C, RD, C.getPointerType(getTgtDeviceImageQTy())); |
| 4093 | addFieldToRecordDecl(C, RD, C.getPointerType(getTgtOffloadEntryQTy())); |
| 4094 | addFieldToRecordDecl(C, RD, C.getPointerType(getTgtOffloadEntryQTy())); |
| 4095 | RD->completeDefinition(); |
| 4096 | TgtBinaryDescriptorQTy = C.getRecordType(RD); |
| 4097 | } |
| 4098 | return TgtBinaryDescriptorQTy; |
| 4099 | } |
| 4100 | |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 4101 | namespace { |
Alexey Bataev | 9e03404 | 2015-05-05 04:05:12 +0000 | [diff] [blame] | 4102 | struct PrivateHelpersTy { |
| 4103 | PrivateHelpersTy(const VarDecl *Original, const VarDecl *PrivateCopy, |
| 4104 | const VarDecl *PrivateElemInit) |
| 4105 | : Original(Original), PrivateCopy(PrivateCopy), |
| 4106 | PrivateElemInit(PrivateElemInit) {} |
| 4107 | const VarDecl *Original; |
| 4108 | const VarDecl *PrivateCopy; |
| 4109 | const VarDecl *PrivateElemInit; |
| 4110 | }; |
| 4111 | typedef std::pair<CharUnits /*Align*/, PrivateHelpersTy> PrivateDataTy; |
Hans Wennborg | 7eb5464 | 2015-09-10 17:07:54 +0000 | [diff] [blame] | 4112 | } // anonymous namespace |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 4113 | |
Alexey Bataev | 9e03404 | 2015-05-05 04:05:12 +0000 | [diff] [blame] | 4114 | static RecordDecl * |
Craig Topper | 8674c5c | 2015-09-29 04:30:07 +0000 | [diff] [blame] | 4115 | createPrivatesRecordDecl(CodeGenModule &CGM, ArrayRef<PrivateDataTy> Privates) { |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 4116 | if (!Privates.empty()) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 4117 | ASTContext &C = CGM.getContext(); |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 4118 | // Build struct .kmp_privates_t. { |
| 4119 | // /* private vars */ |
| 4120 | // }; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 4121 | RecordDecl *RD = C.buildImplicitRecord(".kmp_privates.t"); |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 4122 | RD->startDefinition(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 4123 | for (const auto &Pair : Privates) { |
| 4124 | const VarDecl *VD = Pair.second.Original; |
| 4125 | QualType Type = VD->getType().getNonReferenceType(); |
| 4126 | FieldDecl *FD = addFieldToRecordDecl(C, RD, Type); |
Alexey Bataev | c71a409 | 2015-09-11 10:29:41 +0000 | [diff] [blame] | 4127 | if (VD->hasAttrs()) { |
| 4128 | for (specific_attr_iterator<AlignedAttr> I(VD->getAttrs().begin()), |
| 4129 | E(VD->getAttrs().end()); |
| 4130 | I != E; ++I) |
| 4131 | FD->addAttr(*I); |
| 4132 | } |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 4133 | } |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 4134 | RD->completeDefinition(); |
| 4135 | return RD; |
| 4136 | } |
| 4137 | return nullptr; |
| 4138 | } |
| 4139 | |
Alexey Bataev | 9e03404 | 2015-05-05 04:05:12 +0000 | [diff] [blame] | 4140 | static RecordDecl * |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4141 | createKmpTaskTRecordDecl(CodeGenModule &CGM, OpenMPDirectiveKind Kind, |
| 4142 | QualType KmpInt32Ty, |
Alexey Bataev | 8fc69dc | 2015-05-18 07:54:53 +0000 | [diff] [blame] | 4143 | QualType KmpRoutineEntryPointerQTy) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 4144 | ASTContext &C = CGM.getContext(); |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 4145 | // Build struct kmp_task_t { |
| 4146 | // void * shareds; |
| 4147 | // kmp_routine_entry_t routine; |
| 4148 | // kmp_int32 part_id; |
Alexey Bataev | ad537bb | 2016-05-30 09:06:50 +0000 | [diff] [blame] | 4149 | // kmp_cmplrdata_t data1; |
| 4150 | // kmp_cmplrdata_t data2; |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4151 | // For taskloops additional fields: |
| 4152 | // kmp_uint64 lb; |
| 4153 | // kmp_uint64 ub; |
| 4154 | // kmp_int64 st; |
| 4155 | // kmp_int32 liter; |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 4156 | // void * reductions; |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 4157 | // }; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 4158 | RecordDecl *UD = C.buildImplicitRecord("kmp_cmplrdata_t", TTK_Union); |
Alexey Bataev | ad537bb | 2016-05-30 09:06:50 +0000 | [diff] [blame] | 4159 | UD->startDefinition(); |
| 4160 | addFieldToRecordDecl(C, UD, KmpInt32Ty); |
| 4161 | addFieldToRecordDecl(C, UD, KmpRoutineEntryPointerQTy); |
| 4162 | UD->completeDefinition(); |
| 4163 | QualType KmpCmplrdataTy = C.getRecordType(UD); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 4164 | RecordDecl *RD = C.buildImplicitRecord("kmp_task_t"); |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 4165 | RD->startDefinition(); |
| 4166 | addFieldToRecordDecl(C, RD, C.VoidPtrTy); |
| 4167 | addFieldToRecordDecl(C, RD, KmpRoutineEntryPointerQTy); |
| 4168 | addFieldToRecordDecl(C, RD, KmpInt32Ty); |
Alexey Bataev | ad537bb | 2016-05-30 09:06:50 +0000 | [diff] [blame] | 4169 | addFieldToRecordDecl(C, RD, KmpCmplrdataTy); |
| 4170 | addFieldToRecordDecl(C, RD, KmpCmplrdataTy); |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4171 | if (isOpenMPTaskLoopDirective(Kind)) { |
| 4172 | QualType KmpUInt64Ty = |
| 4173 | CGM.getContext().getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0); |
| 4174 | QualType KmpInt64Ty = |
| 4175 | CGM.getContext().getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1); |
| 4176 | addFieldToRecordDecl(C, RD, KmpUInt64Ty); |
| 4177 | addFieldToRecordDecl(C, RD, KmpUInt64Ty); |
| 4178 | addFieldToRecordDecl(C, RD, KmpInt64Ty); |
| 4179 | addFieldToRecordDecl(C, RD, KmpInt32Ty); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 4180 | addFieldToRecordDecl(C, RD, C.VoidPtrTy); |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4181 | } |
Alexey Bataev | 8fc69dc | 2015-05-18 07:54:53 +0000 | [diff] [blame] | 4182 | RD->completeDefinition(); |
| 4183 | return RD; |
| 4184 | } |
| 4185 | |
| 4186 | static RecordDecl * |
| 4187 | createKmpTaskTWithPrivatesRecordDecl(CodeGenModule &CGM, QualType KmpTaskTQTy, |
Craig Topper | 8674c5c | 2015-09-29 04:30:07 +0000 | [diff] [blame] | 4188 | ArrayRef<PrivateDataTy> Privates) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 4189 | ASTContext &C = CGM.getContext(); |
Alexey Bataev | 8fc69dc | 2015-05-18 07:54:53 +0000 | [diff] [blame] | 4190 | // Build struct kmp_task_t_with_privates { |
| 4191 | // kmp_task_t task_data; |
| 4192 | // .kmp_privates_t. privates; |
| 4193 | // }; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 4194 | RecordDecl *RD = C.buildImplicitRecord("kmp_task_t_with_privates"); |
Alexey Bataev | 8fc69dc | 2015-05-18 07:54:53 +0000 | [diff] [blame] | 4195 | RD->startDefinition(); |
| 4196 | addFieldToRecordDecl(C, RD, KmpTaskTQTy); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 4197 | if (const RecordDecl *PrivateRD = createPrivatesRecordDecl(CGM, Privates)) |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 4198 | addFieldToRecordDecl(C, RD, C.getRecordType(PrivateRD)); |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 4199 | RD->completeDefinition(); |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 4200 | return RD; |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 4201 | } |
| 4202 | |
| 4203 | /// \brief Emit a proxy function which accepts kmp_task_t as the second |
| 4204 | /// argument. |
| 4205 | /// \code |
| 4206 | /// kmp_int32 .omp_task_entry.(kmp_int32 gtid, kmp_task_t *tt) { |
Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 4207 | /// TaskFunction(gtid, tt->part_id, &tt->privates, task_privates_map, tt, |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4208 | /// For taskloops: |
| 4209 | /// 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] | 4210 | /// tt->reductions, tt->shareds); |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 4211 | /// return 0; |
| 4212 | /// } |
| 4213 | /// \endcode |
| 4214 | static llvm::Value * |
| 4215 | emitProxyTaskFunction(CodeGenModule &CGM, SourceLocation Loc, |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4216 | OpenMPDirectiveKind Kind, QualType KmpInt32Ty, |
| 4217 | QualType KmpTaskTWithPrivatesPtrQTy, |
Alexey Bataev | 8fc69dc | 2015-05-18 07:54:53 +0000 | [diff] [blame] | 4218 | QualType KmpTaskTWithPrivatesQTy, QualType KmpTaskTQTy, |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4219 | QualType SharedsPtrTy, llvm::Value *TaskFunction, |
| 4220 | llvm::Value *TaskPrivatesMap) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 4221 | ASTContext &C = CGM.getContext(); |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 4222 | FunctionArgList Args; |
Alexey Bataev | 5622323 | 2017-06-09 13:40:18 +0000 | [diff] [blame] | 4223 | ImplicitParamDecl GtidArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, KmpInt32Ty, |
| 4224 | ImplicitParamDecl::Other); |
| 4225 | ImplicitParamDecl TaskTypeArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, |
| 4226 | KmpTaskTWithPrivatesPtrQTy.withRestrict(), |
| 4227 | ImplicitParamDecl::Other); |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 4228 | Args.push_back(&GtidArg); |
| 4229 | Args.push_back(&TaskTypeArg); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 4230 | const auto &TaskEntryFnInfo = |
John McCall | c56a8b3 | 2016-03-11 04:30:31 +0000 | [diff] [blame] | 4231 | CGM.getTypes().arrangeBuiltinFunctionDeclaration(KmpInt32Ty, Args); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 4232 | llvm::FunctionType *TaskEntryTy = |
| 4233 | CGM.getTypes().GetFunctionType(TaskEntryFnInfo); |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 4234 | auto *TaskEntry = |
| 4235 | llvm::Function::Create(TaskEntryTy, llvm::GlobalValue::InternalLinkage, |
| 4236 | ".omp_task_entry.", &CGM.getModule()); |
Rafael Espindola | 51ec5a9 | 2018-02-28 23:46:35 +0000 | [diff] [blame] | 4237 | CGM.SetInternalFunctionAttributes(GlobalDecl(), TaskEntry, TaskEntryFnInfo); |
Alexey Bataev | c0f879b | 2018-04-10 20:10:53 +0000 | [diff] [blame] | 4238 | TaskEntry->setDoesNotRecurse(); |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 4239 | CodeGenFunction CGF(CGM); |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 4240 | CGF.StartFunction(GlobalDecl(), KmpInt32Ty, TaskEntry, TaskEntryFnInfo, Args, |
| 4241 | Loc, Loc); |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 4242 | |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4243 | // TaskFunction(gtid, tt->task_data.part_id, &tt->privates, task_privates_map, |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4244 | // tt, |
| 4245 | // For taskloops: |
| 4246 | // tt->task_data.lb, tt->task_data.ub, tt->task_data.st, tt->task_data.liter, |
| 4247 | // tt->task_data.shareds); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 4248 | llvm::Value *GtidParam = CGF.EmitLoadOfScalar( |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4249 | CGF.GetAddrOfLocalVar(&GtidArg), /*Volatile=*/false, KmpInt32Ty, Loc); |
Alexey Bataev | 31300ed | 2016-02-04 11:27:03 +0000 | [diff] [blame] | 4250 | LValue TDBase = CGF.EmitLoadOfPointerLValue( |
| 4251 | CGF.GetAddrOfLocalVar(&TaskTypeArg), |
| 4252 | KmpTaskTWithPrivatesPtrQTy->castAs<PointerType>()); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 4253 | const auto *KmpTaskTWithPrivatesQTyRD = |
Alexey Bataev | 8fc69dc | 2015-05-18 07:54:53 +0000 | [diff] [blame] | 4254 | cast<RecordDecl>(KmpTaskTWithPrivatesQTy->getAsTagDecl()); |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4255 | LValue Base = |
| 4256 | CGF.EmitLValueForField(TDBase, *KmpTaskTWithPrivatesQTyRD->field_begin()); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 4257 | const auto *KmpTaskTQTyRD = cast<RecordDecl>(KmpTaskTQTy->getAsTagDecl()); |
Alexey Bataev | 8fc69dc | 2015-05-18 07:54:53 +0000 | [diff] [blame] | 4258 | auto PartIdFI = std::next(KmpTaskTQTyRD->field_begin(), KmpTaskTPartId); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 4259 | LValue PartIdLVal = CGF.EmitLValueForField(Base, *PartIdFI); |
| 4260 | llvm::Value *PartidParam = PartIdLVal.getPointer(); |
Alexey Bataev | 8fc69dc | 2015-05-18 07:54:53 +0000 | [diff] [blame] | 4261 | |
| 4262 | auto SharedsFI = std::next(KmpTaskTQTyRD->field_begin(), KmpTaskTShareds); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 4263 | LValue SharedsLVal = CGF.EmitLValueForField(Base, *SharedsFI); |
| 4264 | llvm::Value *SharedsParam = CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
Alexey Bataev | 1e49137 | 2018-01-23 18:44:14 +0000 | [diff] [blame] | 4265 | CGF.EmitLoadOfScalar(SharedsLVal, Loc), |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 4266 | CGF.ConvertTypeForMem(SharedsPtrTy)); |
| 4267 | |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4268 | auto PrivatesFI = std::next(KmpTaskTWithPrivatesQTyRD->field_begin(), 1); |
| 4269 | llvm::Value *PrivatesParam; |
| 4270 | if (PrivatesFI != KmpTaskTWithPrivatesQTyRD->field_end()) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 4271 | LValue PrivatesLVal = CGF.EmitLValueForField(TDBase, *PrivatesFI); |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4272 | PrivatesParam = CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4273 | PrivatesLVal.getPointer(), CGF.VoidPtrTy); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 4274 | } else { |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4275 | PrivatesParam = llvm::ConstantPointerNull::get(CGF.VoidPtrTy); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 4276 | } |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4277 | |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4278 | llvm::Value *CommonArgs[] = {GtidParam, PartidParam, PrivatesParam, |
| 4279 | TaskPrivatesMap, |
| 4280 | CGF.Builder |
| 4281 | .CreatePointerBitCastOrAddrSpaceCast( |
| 4282 | TDBase.getAddress(), CGF.VoidPtrTy) |
| 4283 | .getPointer()}; |
| 4284 | SmallVector<llvm::Value *, 16> CallArgs(std::begin(CommonArgs), |
| 4285 | std::end(CommonArgs)); |
| 4286 | if (isOpenMPTaskLoopDirective(Kind)) { |
| 4287 | auto LBFI = std::next(KmpTaskTQTyRD->field_begin(), KmpTaskTLowerBound); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 4288 | LValue LBLVal = CGF.EmitLValueForField(Base, *LBFI); |
| 4289 | llvm::Value *LBParam = CGF.EmitLoadOfScalar(LBLVal, Loc); |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4290 | auto UBFI = std::next(KmpTaskTQTyRD->field_begin(), KmpTaskTUpperBound); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 4291 | LValue UBLVal = CGF.EmitLValueForField(Base, *UBFI); |
| 4292 | llvm::Value *UBParam = CGF.EmitLoadOfScalar(UBLVal, Loc); |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4293 | auto StFI = std::next(KmpTaskTQTyRD->field_begin(), KmpTaskTStride); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 4294 | LValue StLVal = CGF.EmitLValueForField(Base, *StFI); |
| 4295 | llvm::Value *StParam = CGF.EmitLoadOfScalar(StLVal, Loc); |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4296 | auto LIFI = std::next(KmpTaskTQTyRD->field_begin(), KmpTaskTLastIter); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 4297 | LValue LILVal = CGF.EmitLValueForField(Base, *LIFI); |
| 4298 | llvm::Value *LIParam = CGF.EmitLoadOfScalar(LILVal, Loc); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 4299 | auto RFI = std::next(KmpTaskTQTyRD->field_begin(), KmpTaskTReductions); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 4300 | LValue RLVal = CGF.EmitLValueForField(Base, *RFI); |
| 4301 | llvm::Value *RParam = CGF.EmitLoadOfScalar(RLVal, Loc); |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4302 | CallArgs.push_back(LBParam); |
| 4303 | CallArgs.push_back(UBParam); |
| 4304 | CallArgs.push_back(StParam); |
| 4305 | CallArgs.push_back(LIParam); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 4306 | CallArgs.push_back(RParam); |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4307 | } |
| 4308 | CallArgs.push_back(SharedsParam); |
| 4309 | |
Alexey Bataev | 3c595a6 | 2017-08-14 15:01:03 +0000 | [diff] [blame] | 4310 | CGM.getOpenMPRuntime().emitOutlinedFunctionCall(CGF, Loc, TaskFunction, |
| 4311 | CallArgs); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 4312 | CGF.EmitStoreThroughLValue(RValue::get(CGF.Builder.getInt32(/*C=*/0)), |
| 4313 | CGF.MakeAddrLValue(CGF.ReturnValue, KmpInt32Ty)); |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 4314 | CGF.FinishFunction(); |
| 4315 | return TaskEntry; |
| 4316 | } |
| 4317 | |
Alexey Bataev | 8fc69dc | 2015-05-18 07:54:53 +0000 | [diff] [blame] | 4318 | static llvm::Value *emitDestructorsFunction(CodeGenModule &CGM, |
| 4319 | SourceLocation Loc, |
| 4320 | QualType KmpInt32Ty, |
| 4321 | QualType KmpTaskTWithPrivatesPtrQTy, |
| 4322 | QualType KmpTaskTWithPrivatesQTy) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 4323 | ASTContext &C = CGM.getContext(); |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 4324 | FunctionArgList Args; |
Alexey Bataev | 5622323 | 2017-06-09 13:40:18 +0000 | [diff] [blame] | 4325 | ImplicitParamDecl GtidArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, KmpInt32Ty, |
| 4326 | ImplicitParamDecl::Other); |
| 4327 | ImplicitParamDecl TaskTypeArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, |
| 4328 | KmpTaskTWithPrivatesPtrQTy.withRestrict(), |
| 4329 | ImplicitParamDecl::Other); |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 4330 | Args.push_back(&GtidArg); |
| 4331 | Args.push_back(&TaskTypeArg); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 4332 | const auto &DestructorFnInfo = |
John McCall | c56a8b3 | 2016-03-11 04:30:31 +0000 | [diff] [blame] | 4333 | CGM.getTypes().arrangeBuiltinFunctionDeclaration(KmpInt32Ty, Args); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 4334 | llvm::FunctionType *DestructorFnTy = |
| 4335 | CGM.getTypes().GetFunctionType(DestructorFnInfo); |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 4336 | auto *DestructorFn = |
| 4337 | llvm::Function::Create(DestructorFnTy, llvm::GlobalValue::InternalLinkage, |
| 4338 | ".omp_task_destructor.", &CGM.getModule()); |
Rafael Espindola | 51ec5a9 | 2018-02-28 23:46:35 +0000 | [diff] [blame] | 4339 | CGM.SetInternalFunctionAttributes(GlobalDecl(), DestructorFn, |
Akira Hatanaka | 44a59f8 | 2015-10-28 02:30:47 +0000 | [diff] [blame] | 4340 | DestructorFnInfo); |
Alexey Bataev | c0f879b | 2018-04-10 20:10:53 +0000 | [diff] [blame] | 4341 | DestructorFn->setDoesNotRecurse(); |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 4342 | CodeGenFunction CGF(CGM); |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 4343 | CGF.StartFunction(GlobalDecl(), KmpInt32Ty, DestructorFn, DestructorFnInfo, |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 4344 | Args, Loc, Loc); |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 4345 | |
Alexey Bataev | 31300ed | 2016-02-04 11:27:03 +0000 | [diff] [blame] | 4346 | LValue Base = CGF.EmitLoadOfPointerLValue( |
| 4347 | CGF.GetAddrOfLocalVar(&TaskTypeArg), |
| 4348 | KmpTaskTWithPrivatesPtrQTy->castAs<PointerType>()); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 4349 | const auto *KmpTaskTWithPrivatesQTyRD = |
Alexey Bataev | 8fc69dc | 2015-05-18 07:54:53 +0000 | [diff] [blame] | 4350 | cast<RecordDecl>(KmpTaskTWithPrivatesQTy->getAsTagDecl()); |
| 4351 | auto FI = std::next(KmpTaskTWithPrivatesQTyRD->field_begin()); |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 4352 | Base = CGF.EmitLValueForField(Base, *FI); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 4353 | for (const auto *Field : |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 4354 | cast<RecordDecl>(FI->getType()->getAsTagDecl())->fields()) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 4355 | if (QualType::DestructionKind DtorKind = |
| 4356 | Field->getType().isDestructedType()) { |
| 4357 | LValue FieldLValue = CGF.EmitLValueForField(Base, Field); |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 4358 | CGF.pushDestroy(DtorKind, FieldLValue.getAddress(), Field->getType()); |
| 4359 | } |
| 4360 | } |
| 4361 | CGF.FinishFunction(); |
| 4362 | return DestructorFn; |
| 4363 | } |
| 4364 | |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4365 | /// \brief Emit a privates mapping function for correct handling of private and |
| 4366 | /// firstprivate variables. |
| 4367 | /// \code |
| 4368 | /// void .omp_task_privates_map.(const .privates. *noalias privs, <ty1> |
| 4369 | /// **noalias priv1,..., <tyn> **noalias privn) { |
| 4370 | /// *priv1 = &.privates.priv1; |
| 4371 | /// ...; |
| 4372 | /// *privn = &.privates.privn; |
| 4373 | /// } |
| 4374 | /// \endcode |
| 4375 | static llvm::Value * |
| 4376 | emitTaskPrivateMappingFunction(CodeGenModule &CGM, SourceLocation Loc, |
Craig Topper | 8674c5c | 2015-09-29 04:30:07 +0000 | [diff] [blame] | 4377 | ArrayRef<const Expr *> PrivateVars, |
| 4378 | ArrayRef<const Expr *> FirstprivateVars, |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4379 | ArrayRef<const Expr *> LastprivateVars, |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4380 | QualType PrivatesQTy, |
Craig Topper | 8674c5c | 2015-09-29 04:30:07 +0000 | [diff] [blame] | 4381 | ArrayRef<PrivateDataTy> Privates) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 4382 | ASTContext &C = CGM.getContext(); |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4383 | FunctionArgList Args; |
| 4384 | ImplicitParamDecl TaskPrivatesArg( |
| 4385 | C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, |
Alexey Bataev | 5622323 | 2017-06-09 13:40:18 +0000 | [diff] [blame] | 4386 | C.getPointerType(PrivatesQTy).withConst().withRestrict(), |
| 4387 | ImplicitParamDecl::Other); |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4388 | Args.push_back(&TaskPrivatesArg); |
| 4389 | llvm::DenseMap<const VarDecl *, unsigned> PrivateVarsPos; |
| 4390 | unsigned Counter = 1; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 4391 | for (const Expr *E : PrivateVars) { |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4392 | Args.push_back(ImplicitParamDecl::Create( |
Alexey Bataev | 5622323 | 2017-06-09 13:40:18 +0000 | [diff] [blame] | 4393 | C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, |
| 4394 | C.getPointerType(C.getPointerType(E->getType())) |
| 4395 | .withConst() |
| 4396 | .withRestrict(), |
| 4397 | ImplicitParamDecl::Other)); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 4398 | const auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl()); |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4399 | PrivateVarsPos[VD] = Counter; |
| 4400 | ++Counter; |
| 4401 | } |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 4402 | for (const Expr *E : FirstprivateVars) { |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4403 | Args.push_back(ImplicitParamDecl::Create( |
Alexey Bataev | 5622323 | 2017-06-09 13:40:18 +0000 | [diff] [blame] | 4404 | C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, |
| 4405 | C.getPointerType(C.getPointerType(E->getType())) |
| 4406 | .withConst() |
| 4407 | .withRestrict(), |
| 4408 | ImplicitParamDecl::Other)); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 4409 | const auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl()); |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4410 | PrivateVarsPos[VD] = Counter; |
| 4411 | ++Counter; |
| 4412 | } |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 4413 | for (const Expr *E : LastprivateVars) { |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4414 | Args.push_back(ImplicitParamDecl::Create( |
Alexey Bataev | 5622323 | 2017-06-09 13:40:18 +0000 | [diff] [blame] | 4415 | C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, |
| 4416 | C.getPointerType(C.getPointerType(E->getType())) |
| 4417 | .withConst() |
| 4418 | .withRestrict(), |
| 4419 | ImplicitParamDecl::Other)); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 4420 | const auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl()); |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4421 | PrivateVarsPos[VD] = Counter; |
| 4422 | ++Counter; |
| 4423 | } |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 4424 | const auto &TaskPrivatesMapFnInfo = |
John McCall | c56a8b3 | 2016-03-11 04:30:31 +0000 | [diff] [blame] | 4425 | CGM.getTypes().arrangeBuiltinFunctionDeclaration(C.VoidTy, Args); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 4426 | llvm::FunctionType *TaskPrivatesMapTy = |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4427 | CGM.getTypes().GetFunctionType(TaskPrivatesMapFnInfo); |
| 4428 | auto *TaskPrivatesMap = llvm::Function::Create( |
| 4429 | TaskPrivatesMapTy, llvm::GlobalValue::InternalLinkage, |
| 4430 | ".omp_task_privates_map.", &CGM.getModule()); |
Rafael Espindola | 51ec5a9 | 2018-02-28 23:46:35 +0000 | [diff] [blame] | 4431 | CGM.SetInternalFunctionAttributes(GlobalDecl(), TaskPrivatesMap, |
Akira Hatanaka | 44a59f8 | 2015-10-28 02:30:47 +0000 | [diff] [blame] | 4432 | TaskPrivatesMapFnInfo); |
Chandler Carruth | fcd3314 | 2016-12-23 01:24:49 +0000 | [diff] [blame] | 4433 | TaskPrivatesMap->removeFnAttr(llvm::Attribute::NoInline); |
Mehdi Amini | 6aa9e9b | 2017-05-29 05:38:20 +0000 | [diff] [blame] | 4434 | TaskPrivatesMap->removeFnAttr(llvm::Attribute::OptimizeNone); |
Evgeniy Stepanov | 6b2a61d | 2015-09-14 21:35:16 +0000 | [diff] [blame] | 4435 | TaskPrivatesMap->addFnAttr(llvm::Attribute::AlwaysInline); |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4436 | CodeGenFunction CGF(CGM); |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4437 | CGF.StartFunction(GlobalDecl(), C.VoidTy, TaskPrivatesMap, |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 4438 | TaskPrivatesMapFnInfo, Args, Loc, Loc); |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4439 | |
| 4440 | // *privi = &.privates.privi; |
Alexey Bataev | 31300ed | 2016-02-04 11:27:03 +0000 | [diff] [blame] | 4441 | LValue Base = CGF.EmitLoadOfPointerLValue( |
| 4442 | CGF.GetAddrOfLocalVar(&TaskPrivatesArg), |
| 4443 | TaskPrivatesArg.getType()->castAs<PointerType>()); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 4444 | const auto *PrivatesQTyRD = cast<RecordDecl>(PrivatesQTy->getAsTagDecl()); |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4445 | Counter = 0; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 4446 | for (const FieldDecl *Field : PrivatesQTyRD->fields()) { |
| 4447 | LValue FieldLVal = CGF.EmitLValueForField(Base, Field); |
| 4448 | const VarDecl *VD = Args[PrivateVarsPos[Privates[Counter].second.Original]]; |
| 4449 | LValue RefLVal = |
| 4450 | CGF.MakeAddrLValue(CGF.GetAddrOfLocalVar(VD), VD->getType()); |
| 4451 | LValue RefLoadLVal = CGF.EmitLoadOfPointerLValue( |
Alexey Bataev | 31300ed | 2016-02-04 11:27:03 +0000 | [diff] [blame] | 4452 | RefLVal.getAddress(), RefLVal.getType()->castAs<PointerType>()); |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 4453 | CGF.EmitStoreOfScalar(FieldLVal.getPointer(), RefLoadLVal); |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4454 | ++Counter; |
| 4455 | } |
| 4456 | CGF.FinishFunction(); |
| 4457 | return TaskPrivatesMap; |
| 4458 | } |
| 4459 | |
Mandeep Singh Grang | b14fb6a2 | 2017-11-28 20:41:13 +0000 | [diff] [blame] | 4460 | static bool stable_sort_comparator(const PrivateDataTy P1, |
| 4461 | const PrivateDataTy P2) { |
| 4462 | return P1.first > P2.first; |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 4463 | } |
| 4464 | |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4465 | /// Emit initialization for private variables in task-based directives. |
Alexey Bataev | 8a83159 | 2016-05-10 10:36:51 +0000 | [diff] [blame] | 4466 | static void emitPrivatesInit(CodeGenFunction &CGF, |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4467 | const OMPExecutableDirective &D, |
| 4468 | Address KmpTaskSharedsPtr, LValue TDBase, |
| 4469 | const RecordDecl *KmpTaskTWithPrivatesQTyRD, |
| 4470 | QualType SharedsTy, QualType SharedsPtrTy, |
| 4471 | const OMPTaskDataTy &Data, |
| 4472 | ArrayRef<PrivateDataTy> Privates, bool ForDup) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 4473 | ASTContext &C = CGF.getContext(); |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4474 | auto FI = std::next(KmpTaskTWithPrivatesQTyRD->field_begin()); |
| 4475 | LValue PrivatesBase = CGF.EmitLValueForField(TDBase, *FI); |
Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 4476 | OpenMPDirectiveKind Kind = isOpenMPTaskLoopDirective(D.getDirectiveKind()) |
| 4477 | ? OMPD_taskloop |
| 4478 | : OMPD_task; |
| 4479 | const CapturedStmt &CS = *D.getCapturedStmt(Kind); |
| 4480 | CodeGenFunction::CGCapturedStmtInfo CapturesInfo(CS); |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4481 | LValue SrcBase; |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 4482 | bool IsTargetTask = |
| 4483 | isOpenMPTargetDataManagementDirective(D.getDirectiveKind()) || |
| 4484 | isOpenMPTargetExecutionDirective(D.getDirectiveKind()); |
| 4485 | // For target-based directives skip 3 firstprivate arrays BasePointersArray, |
| 4486 | // PointersArray and SizesArray. The original variables for these arrays are |
| 4487 | // not captured and we get their addresses explicitly. |
| 4488 | if ((!IsTargetTask && !Data.FirstprivateVars.empty()) || |
Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 4489 | (IsTargetTask && KmpTaskSharedsPtr.isValid())) { |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4490 | SrcBase = CGF.MakeAddrLValue( |
| 4491 | CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
| 4492 | KmpTaskSharedsPtr, CGF.ConvertTypeForMem(SharedsPtrTy)), |
| 4493 | SharedsTy); |
| 4494 | } |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4495 | FI = cast<RecordDecl>(FI->getType()->getAsTagDecl())->field_begin(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 4496 | for (const PrivateDataTy &Pair : Privates) { |
| 4497 | const VarDecl *VD = Pair.second.PrivateCopy; |
| 4498 | const Expr *Init = VD->getAnyInitializer(); |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4499 | if (Init && (!ForDup || (isa<CXXConstructExpr>(Init) && |
| 4500 | !CGF.isTrivialInitializer(Init)))) { |
Alexey Bataev | 8a83159 | 2016-05-10 10:36:51 +0000 | [diff] [blame] | 4501 | LValue PrivateLValue = CGF.EmitLValueForField(PrivatesBase, *FI); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 4502 | if (const VarDecl *Elem = Pair.second.PrivateElemInit) { |
| 4503 | const VarDecl *OriginalVD = Pair.second.Original; |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 4504 | // Check if the variable is the target-based BasePointersArray, |
| 4505 | // PointersArray or SizesArray. |
| 4506 | LValue SharedRefLValue; |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4507 | QualType Type = OriginalVD->getType(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 4508 | const FieldDecl *SharedField = CapturesInfo.lookup(OriginalVD); |
Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 4509 | if (IsTargetTask && !SharedField) { |
| 4510 | assert(isa<ImplicitParamDecl>(OriginalVD) && |
| 4511 | isa<CapturedDecl>(OriginalVD->getDeclContext()) && |
| 4512 | cast<CapturedDecl>(OriginalVD->getDeclContext()) |
| 4513 | ->getNumParams() == 0 && |
| 4514 | isa<TranslationUnitDecl>( |
| 4515 | cast<CapturedDecl>(OriginalVD->getDeclContext()) |
| 4516 | ->getDeclContext()) && |
| 4517 | "Expected artificial target data variable."); |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 4518 | SharedRefLValue = |
| 4519 | CGF.MakeAddrLValue(CGF.GetAddrOfLocalVar(OriginalVD), Type); |
| 4520 | } else { |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 4521 | SharedRefLValue = CGF.EmitLValueForField(SrcBase, SharedField); |
| 4522 | SharedRefLValue = CGF.MakeAddrLValue( |
| 4523 | Address(SharedRefLValue.getPointer(), C.getDeclAlign(OriginalVD)), |
| 4524 | SharedRefLValue.getType(), LValueBaseInfo(AlignmentSource::Decl), |
| 4525 | SharedRefLValue.getTBAAInfo()); |
| 4526 | } |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4527 | if (Type->isArrayType()) { |
| 4528 | // Initialize firstprivate array. |
| 4529 | if (!isa<CXXConstructExpr>(Init) || CGF.isTrivialInitializer(Init)) { |
| 4530 | // Perform simple memcpy. |
Ivan A. Kosarev | 1860b52 | 2018-01-25 14:21:55 +0000 | [diff] [blame] | 4531 | CGF.EmitAggregateAssign(PrivateLValue, SharedRefLValue, Type); |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4532 | } else { |
| 4533 | // Initialize firstprivate array using element-by-element |
Simon Pilgrim | 2c51880 | 2017-03-30 14:13:19 +0000 | [diff] [blame] | 4534 | // initialization. |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4535 | CGF.EmitOMPAggregateAssign( |
| 4536 | PrivateLValue.getAddress(), SharedRefLValue.getAddress(), Type, |
| 4537 | [&CGF, Elem, Init, &CapturesInfo](Address DestElement, |
| 4538 | Address SrcElement) { |
| 4539 | // Clean up any temporaries needed by the initialization. |
| 4540 | CodeGenFunction::OMPPrivateScope InitScope(CGF); |
| 4541 | InitScope.addPrivate( |
| 4542 | Elem, [SrcElement]() -> Address { return SrcElement; }); |
| 4543 | (void)InitScope.Privatize(); |
| 4544 | // Emit initialization for single element. |
| 4545 | CodeGenFunction::CGCapturedStmtRAII CapInfoRAII( |
| 4546 | CGF, &CapturesInfo); |
| 4547 | CGF.EmitAnyExprToMem(Init, DestElement, |
| 4548 | Init->getType().getQualifiers(), |
| 4549 | /*IsInitializer=*/false); |
| 4550 | }); |
| 4551 | } |
| 4552 | } else { |
| 4553 | CodeGenFunction::OMPPrivateScope InitScope(CGF); |
| 4554 | InitScope.addPrivate(Elem, [SharedRefLValue]() -> Address { |
| 4555 | return SharedRefLValue.getAddress(); |
| 4556 | }); |
| 4557 | (void)InitScope.Privatize(); |
| 4558 | CodeGenFunction::CGCapturedStmtRAII CapInfoRAII(CGF, &CapturesInfo); |
| 4559 | CGF.EmitExprAsInit(Init, VD, PrivateLValue, |
| 4560 | /*capturedByInit=*/false); |
| 4561 | } |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 4562 | } else { |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4563 | CGF.EmitExprAsInit(Init, VD, PrivateLValue, /*capturedByInit=*/false); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 4564 | } |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4565 | } |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4566 | ++FI; |
| 4567 | } |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4568 | } |
| 4569 | |
| 4570 | /// Check if duplication function is required for taskloops. |
| 4571 | static bool checkInitIsRequired(CodeGenFunction &CGF, |
| 4572 | ArrayRef<PrivateDataTy> Privates) { |
| 4573 | bool InitRequired = false; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 4574 | for (const PrivateDataTy &Pair : Privates) { |
| 4575 | const VarDecl *VD = Pair.second.PrivateCopy; |
| 4576 | const Expr *Init = VD->getAnyInitializer(); |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4577 | InitRequired = InitRequired || (Init && isa<CXXConstructExpr>(Init) && |
| 4578 | !CGF.isTrivialInitializer(Init)); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 4579 | if (InitRequired) |
| 4580 | break; |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4581 | } |
| 4582 | return InitRequired; |
| 4583 | } |
| 4584 | |
| 4585 | |
| 4586 | /// Emit task_dup function (for initialization of |
| 4587 | /// private/firstprivate/lastprivate vars and last_iter flag) |
| 4588 | /// \code |
| 4589 | /// void __task_dup_entry(kmp_task_t *task_dst, const kmp_task_t *task_src, int |
| 4590 | /// lastpriv) { |
| 4591 | /// // setup lastprivate flag |
| 4592 | /// task_dst->last = lastpriv; |
| 4593 | /// // could be constructor calls here... |
| 4594 | /// } |
| 4595 | /// \endcode |
| 4596 | static llvm::Value * |
| 4597 | emitTaskDupFunction(CodeGenModule &CGM, SourceLocation Loc, |
| 4598 | const OMPExecutableDirective &D, |
| 4599 | QualType KmpTaskTWithPrivatesPtrQTy, |
| 4600 | const RecordDecl *KmpTaskTWithPrivatesQTyRD, |
| 4601 | const RecordDecl *KmpTaskTQTyRD, QualType SharedsTy, |
| 4602 | QualType SharedsPtrTy, const OMPTaskDataTy &Data, |
| 4603 | ArrayRef<PrivateDataTy> Privates, bool WithLastIter) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 4604 | ASTContext &C = CGM.getContext(); |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4605 | FunctionArgList Args; |
Alexey Bataev | 5622323 | 2017-06-09 13:40:18 +0000 | [diff] [blame] | 4606 | ImplicitParamDecl DstArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, |
| 4607 | KmpTaskTWithPrivatesPtrQTy, |
| 4608 | ImplicitParamDecl::Other); |
| 4609 | ImplicitParamDecl SrcArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, |
| 4610 | KmpTaskTWithPrivatesPtrQTy, |
| 4611 | ImplicitParamDecl::Other); |
| 4612 | ImplicitParamDecl LastprivArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, C.IntTy, |
| 4613 | ImplicitParamDecl::Other); |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4614 | Args.push_back(&DstArg); |
| 4615 | Args.push_back(&SrcArg); |
| 4616 | Args.push_back(&LastprivArg); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 4617 | const auto &TaskDupFnInfo = |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4618 | CGM.getTypes().arrangeBuiltinFunctionDeclaration(C.VoidTy, Args); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 4619 | llvm::FunctionType *TaskDupTy = CGM.getTypes().GetFunctionType(TaskDupFnInfo); |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4620 | auto *TaskDup = |
| 4621 | llvm::Function::Create(TaskDupTy, llvm::GlobalValue::InternalLinkage, |
| 4622 | ".omp_task_dup.", &CGM.getModule()); |
Rafael Espindola | 51ec5a9 | 2018-02-28 23:46:35 +0000 | [diff] [blame] | 4623 | CGM.SetInternalFunctionAttributes(GlobalDecl(), TaskDup, TaskDupFnInfo); |
Alexey Bataev | c0f879b | 2018-04-10 20:10:53 +0000 | [diff] [blame] | 4624 | TaskDup->setDoesNotRecurse(); |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4625 | CodeGenFunction CGF(CGM); |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 4626 | CGF.StartFunction(GlobalDecl(), C.VoidTy, TaskDup, TaskDupFnInfo, Args, Loc, |
| 4627 | Loc); |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4628 | |
| 4629 | LValue TDBase = CGF.EmitLoadOfPointerLValue( |
| 4630 | CGF.GetAddrOfLocalVar(&DstArg), |
| 4631 | KmpTaskTWithPrivatesPtrQTy->castAs<PointerType>()); |
| 4632 | // task_dst->liter = lastpriv; |
| 4633 | if (WithLastIter) { |
| 4634 | auto LIFI = std::next(KmpTaskTQTyRD->field_begin(), KmpTaskTLastIter); |
| 4635 | LValue Base = CGF.EmitLValueForField( |
| 4636 | TDBase, *KmpTaskTWithPrivatesQTyRD->field_begin()); |
| 4637 | LValue LILVal = CGF.EmitLValueForField(Base, *LIFI); |
| 4638 | llvm::Value *Lastpriv = CGF.EmitLoadOfScalar( |
| 4639 | CGF.GetAddrOfLocalVar(&LastprivArg), /*Volatile=*/false, C.IntTy, Loc); |
| 4640 | CGF.EmitStoreOfScalar(Lastpriv, LILVal); |
| 4641 | } |
| 4642 | |
| 4643 | // Emit initial values for private copies (if any). |
| 4644 | assert(!Privates.empty()); |
| 4645 | Address KmpTaskSharedsPtr = Address::invalid(); |
| 4646 | if (!Data.FirstprivateVars.empty()) { |
| 4647 | LValue TDBase = CGF.EmitLoadOfPointerLValue( |
| 4648 | CGF.GetAddrOfLocalVar(&SrcArg), |
| 4649 | KmpTaskTWithPrivatesPtrQTy->castAs<PointerType>()); |
| 4650 | LValue Base = CGF.EmitLValueForField( |
| 4651 | TDBase, *KmpTaskTWithPrivatesQTyRD->field_begin()); |
| 4652 | KmpTaskSharedsPtr = Address( |
| 4653 | CGF.EmitLoadOfScalar(CGF.EmitLValueForField( |
| 4654 | Base, *std::next(KmpTaskTQTyRD->field_begin(), |
| 4655 | KmpTaskTShareds)), |
| 4656 | Loc), |
| 4657 | CGF.getNaturalTypeAlignment(SharedsTy)); |
| 4658 | } |
Alexey Bataev | 8a83159 | 2016-05-10 10:36:51 +0000 | [diff] [blame] | 4659 | emitPrivatesInit(CGF, D, KmpTaskSharedsPtr, TDBase, KmpTaskTWithPrivatesQTyRD, |
| 4660 | SharedsTy, SharedsPtrTy, Data, Privates, /*ForDup=*/true); |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4661 | CGF.FinishFunction(); |
| 4662 | return TaskDup; |
| 4663 | } |
| 4664 | |
Alexey Bataev | 8a83159 | 2016-05-10 10:36:51 +0000 | [diff] [blame] | 4665 | /// Checks if destructor function is required to be generated. |
| 4666 | /// \return true if cleanups are required, false otherwise. |
| 4667 | static bool |
| 4668 | checkDestructorsRequired(const RecordDecl *KmpTaskTWithPrivatesQTyRD) { |
| 4669 | bool NeedsCleanup = false; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 4670 | auto FI = std::next(KmpTaskTWithPrivatesQTyRD->field_begin(), 1); |
| 4671 | const auto *PrivateRD = cast<RecordDecl>(FI->getType()->getAsTagDecl()); |
| 4672 | for (const FieldDecl *FD : PrivateRD->fields()) { |
Alexey Bataev | 8a83159 | 2016-05-10 10:36:51 +0000 | [diff] [blame] | 4673 | NeedsCleanup = NeedsCleanup || FD->getType().isDestructedType(); |
| 4674 | if (NeedsCleanup) |
| 4675 | break; |
| 4676 | } |
| 4677 | return NeedsCleanup; |
| 4678 | } |
| 4679 | |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 4680 | CGOpenMPRuntime::TaskResultTy |
| 4681 | CGOpenMPRuntime::emitTaskInit(CodeGenFunction &CGF, SourceLocation Loc, |
| 4682 | const OMPExecutableDirective &D, |
| 4683 | llvm::Value *TaskFunction, QualType SharedsTy, |
| 4684 | Address Shareds, const OMPTaskDataTy &Data) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 4685 | ASTContext &C = CGM.getContext(); |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4686 | llvm::SmallVector<PrivateDataTy, 4> Privates; |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4687 | // Aggregate privates and sort them by the alignment. |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 4688 | auto I = Data.PrivateCopies.begin(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 4689 | for (const Expr *E : Data.PrivateVars) { |
| 4690 | const auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl()); |
Alexey Bataev | 43a919f | 2018-04-13 17:48:43 +0000 | [diff] [blame] | 4691 | Privates.emplace_back( |
Alexey Bataev | c71a409 | 2015-09-11 10:29:41 +0000 | [diff] [blame] | 4692 | C.getDeclAlign(VD), |
Alexey Bataev | 9e03404 | 2015-05-05 04:05:12 +0000 | [diff] [blame] | 4693 | PrivateHelpersTy(VD, cast<VarDecl>(cast<DeclRefExpr>(*I)->getDecl()), |
Alexey Bataev | 43a919f | 2018-04-13 17:48:43 +0000 | [diff] [blame] | 4694 | /*PrivateElemInit=*/nullptr)); |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 4695 | ++I; |
| 4696 | } |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 4697 | I = Data.FirstprivateCopies.begin(); |
| 4698 | auto IElemInitRef = Data.FirstprivateInits.begin(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 4699 | for (const Expr *E : Data.FirstprivateVars) { |
| 4700 | const auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl()); |
Alexey Bataev | 43a919f | 2018-04-13 17:48:43 +0000 | [diff] [blame] | 4701 | Privates.emplace_back( |
Alexey Bataev | c71a409 | 2015-09-11 10:29:41 +0000 | [diff] [blame] | 4702 | C.getDeclAlign(VD), |
Alexey Bataev | 9e03404 | 2015-05-05 04:05:12 +0000 | [diff] [blame] | 4703 | PrivateHelpersTy( |
| 4704 | VD, cast<VarDecl>(cast<DeclRefExpr>(*I)->getDecl()), |
Alexey Bataev | 43a919f | 2018-04-13 17:48:43 +0000 | [diff] [blame] | 4705 | cast<VarDecl>(cast<DeclRefExpr>(*IElemInitRef)->getDecl()))); |
Richard Trieu | cc3949d | 2016-02-18 22:34:54 +0000 | [diff] [blame] | 4706 | ++I; |
| 4707 | ++IElemInitRef; |
Alexey Bataev | 9e03404 | 2015-05-05 04:05:12 +0000 | [diff] [blame] | 4708 | } |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4709 | I = Data.LastprivateCopies.begin(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 4710 | for (const Expr *E : Data.LastprivateVars) { |
| 4711 | const auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl()); |
Alexey Bataev | 43a919f | 2018-04-13 17:48:43 +0000 | [diff] [blame] | 4712 | Privates.emplace_back( |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4713 | C.getDeclAlign(VD), |
| 4714 | PrivateHelpersTy(VD, cast<VarDecl>(cast<DeclRefExpr>(*I)->getDecl()), |
Alexey Bataev | 43a919f | 2018-04-13 17:48:43 +0000 | [diff] [blame] | 4715 | /*PrivateElemInit=*/nullptr)); |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4716 | ++I; |
| 4717 | } |
Mandeep Singh Grang | b14fb6a2 | 2017-11-28 20:41:13 +0000 | [diff] [blame] | 4718 | std::stable_sort(Privates.begin(), Privates.end(), stable_sort_comparator); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 4719 | QualType KmpInt32Ty = C.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1); |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 4720 | // Build type kmp_routine_entry_t (if not built yet). |
| 4721 | emitKmpRoutineEntryT(KmpInt32Ty); |
Alexey Bataev | 8fc69dc | 2015-05-18 07:54:53 +0000 | [diff] [blame] | 4722 | // Build type kmp_task_t (if not built yet). |
Alexey Bataev | e213f3e | 2017-10-11 15:29:40 +0000 | [diff] [blame] | 4723 | if (isOpenMPTaskLoopDirective(D.getDirectiveKind())) { |
| 4724 | if (SavedKmpTaskloopTQTy.isNull()) { |
| 4725 | SavedKmpTaskloopTQTy = C.getRecordType(createKmpTaskTRecordDecl( |
| 4726 | CGM, D.getDirectiveKind(), KmpInt32Ty, KmpRoutineEntryPtrQTy)); |
| 4727 | } |
| 4728 | KmpTaskTQTy = SavedKmpTaskloopTQTy; |
Alexey Bataev | 3a03a7f | 2017-10-11 15:56:38 +0000 | [diff] [blame] | 4729 | } else { |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 4730 | assert((D.getDirectiveKind() == OMPD_task || |
| 4731 | isOpenMPTargetExecutionDirective(D.getDirectiveKind()) || |
| 4732 | isOpenMPTargetDataManagementDirective(D.getDirectiveKind())) && |
| 4733 | "Expected taskloop, task or target directive"); |
Alexey Bataev | e213f3e | 2017-10-11 15:29:40 +0000 | [diff] [blame] | 4734 | if (SavedKmpTaskTQTy.isNull()) { |
| 4735 | SavedKmpTaskTQTy = C.getRecordType(createKmpTaskTRecordDecl( |
| 4736 | CGM, D.getDirectiveKind(), KmpInt32Ty, KmpRoutineEntryPtrQTy)); |
| 4737 | } |
| 4738 | KmpTaskTQTy = SavedKmpTaskTQTy; |
Alexey Bataev | 8fc69dc | 2015-05-18 07:54:53 +0000 | [diff] [blame] | 4739 | } |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 4740 | const auto *KmpTaskTQTyRD = cast<RecordDecl>(KmpTaskTQTy->getAsTagDecl()); |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 4741 | // Build particular struct kmp_task_t for the given task. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 4742 | const RecordDecl *KmpTaskTWithPrivatesQTyRD = |
Alexey Bataev | 8fc69dc | 2015-05-18 07:54:53 +0000 | [diff] [blame] | 4743 | createKmpTaskTWithPrivatesRecordDecl(CGM, KmpTaskTQTy, Privates); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 4744 | QualType KmpTaskTWithPrivatesQTy = C.getRecordType(KmpTaskTWithPrivatesQTyRD); |
Alexey Bataev | 8fc69dc | 2015-05-18 07:54:53 +0000 | [diff] [blame] | 4745 | QualType KmpTaskTWithPrivatesPtrQTy = |
| 4746 | C.getPointerType(KmpTaskTWithPrivatesQTy); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 4747 | llvm::Type *KmpTaskTWithPrivatesTy = CGF.ConvertType(KmpTaskTWithPrivatesQTy); |
| 4748 | llvm::Type *KmpTaskTWithPrivatesPtrTy = |
| 4749 | KmpTaskTWithPrivatesTy->getPointerTo(); |
| 4750 | llvm::Value *KmpTaskTWithPrivatesTySize = |
| 4751 | CGF.getTypeSize(KmpTaskTWithPrivatesQTy); |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 4752 | QualType SharedsPtrTy = C.getPointerType(SharedsTy); |
| 4753 | |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4754 | // Emit initial values for private copies (if any). |
| 4755 | llvm::Value *TaskPrivatesMap = nullptr; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 4756 | llvm::Type *TaskPrivatesMapTy = |
Reid Kleckner | e258c44 | 2017-03-16 18:55:46 +0000 | [diff] [blame] | 4757 | std::next(cast<llvm::Function>(TaskFunction)->arg_begin(), 3)->getType(); |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4758 | if (!Privates.empty()) { |
| 4759 | auto FI = std::next(KmpTaskTWithPrivatesQTyRD->field_begin()); |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4760 | TaskPrivatesMap = emitTaskPrivateMappingFunction( |
| 4761 | CGM, Loc, Data.PrivateVars, Data.FirstprivateVars, Data.LastprivateVars, |
| 4762 | FI->getType(), Privates); |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4763 | TaskPrivatesMap = CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
| 4764 | TaskPrivatesMap, TaskPrivatesMapTy); |
| 4765 | } else { |
| 4766 | TaskPrivatesMap = llvm::ConstantPointerNull::get( |
| 4767 | cast<llvm::PointerType>(TaskPrivatesMapTy)); |
| 4768 | } |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 4769 | // Build a proxy function kmp_int32 .omp_task_entry.(kmp_int32 gtid, |
| 4770 | // kmp_task_t *tt); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 4771 | llvm::Value *TaskEntry = emitProxyTaskFunction( |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4772 | CGM, Loc, D.getDirectiveKind(), KmpInt32Ty, KmpTaskTWithPrivatesPtrQTy, |
| 4773 | KmpTaskTWithPrivatesQTy, KmpTaskTQTy, SharedsPtrTy, TaskFunction, |
| 4774 | TaskPrivatesMap); |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 4775 | |
| 4776 | // Build call kmp_task_t * __kmpc_omp_task_alloc(ident_t *, kmp_int32 gtid, |
| 4777 | // kmp_int32 flags, size_t sizeof_kmp_task_t, size_t sizeof_shareds, |
| 4778 | // kmp_routine_entry_t *task_entry); |
| 4779 | // Task flags. Format is taken from |
| 4780 | // http://llvm.org/svn/llvm-project/openmp/trunk/runtime/src/kmp.h, |
| 4781 | // description of kmp_tasking_flags struct. |
Alexey Bataev | 1e1e286 | 2016-05-10 12:21:02 +0000 | [diff] [blame] | 4782 | enum { |
| 4783 | TiedFlag = 0x1, |
| 4784 | FinalFlag = 0x2, |
| 4785 | DestructorsFlag = 0x8, |
| 4786 | PriorityFlag = 0x20 |
| 4787 | }; |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 4788 | unsigned Flags = Data.Tied ? TiedFlag : 0; |
Alexey Bataev | 8a83159 | 2016-05-10 10:36:51 +0000 | [diff] [blame] | 4789 | bool NeedsCleanup = false; |
| 4790 | if (!Privates.empty()) { |
| 4791 | NeedsCleanup = checkDestructorsRequired(KmpTaskTWithPrivatesQTyRD); |
| 4792 | if (NeedsCleanup) |
| 4793 | Flags = Flags | DestructorsFlag; |
| 4794 | } |
Alexey Bataev | 1e1e286 | 2016-05-10 12:21:02 +0000 | [diff] [blame] | 4795 | if (Data.Priority.getInt()) |
| 4796 | Flags = Flags | PriorityFlag; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 4797 | llvm::Value *TaskFlags = |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 4798 | Data.Final.getPointer() |
| 4799 | ? CGF.Builder.CreateSelect(Data.Final.getPointer(), |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 4800 | CGF.Builder.getInt32(FinalFlag), |
| 4801 | CGF.Builder.getInt32(/*C=*/0)) |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 4802 | : CGF.Builder.getInt32(Data.Final.getInt() ? FinalFlag : 0); |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 4803 | TaskFlags = CGF.Builder.CreateOr(TaskFlags, CGF.Builder.getInt32(Flags)); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 4804 | llvm::Value *SharedsSize = CGM.getSize(C.getTypeSizeInChars(SharedsTy)); |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 4805 | llvm::Value *AllocArgs[] = {emitUpdateLocation(CGF, Loc), |
| 4806 | getThreadID(CGF, Loc), TaskFlags, |
| 4807 | KmpTaskTWithPrivatesTySize, SharedsSize, |
| 4808 | CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
| 4809 | TaskEntry, KmpRoutineEntryPtrTy)}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 4810 | llvm::Value *NewTask = CGF.EmitRuntimeCall( |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 4811 | createRuntimeFunction(OMPRTL__kmpc_omp_task_alloc), AllocArgs); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 4812 | llvm::Value *NewTaskNewTaskTTy = |
| 4813 | CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
| 4814 | NewTask, KmpTaskTWithPrivatesPtrTy); |
Alexey Bataev | 8fc69dc | 2015-05-18 07:54:53 +0000 | [diff] [blame] | 4815 | LValue Base = CGF.MakeNaturalAlignAddrLValue(NewTaskNewTaskTTy, |
| 4816 | KmpTaskTWithPrivatesQTy); |
| 4817 | LValue TDBase = |
| 4818 | CGF.EmitLValueForField(Base, *KmpTaskTWithPrivatesQTyRD->field_begin()); |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 4819 | // Fill the data in the resulting kmp_task_t record. |
| 4820 | // Copy shareds if there are any. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4821 | Address KmpTaskSharedsPtr = Address::invalid(); |
Alexey Bataev | 8fc69dc | 2015-05-18 07:54:53 +0000 | [diff] [blame] | 4822 | if (!SharedsTy->getAsStructureType()->getDecl()->field_empty()) { |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 4823 | KmpTaskSharedsPtr = |
| 4824 | Address(CGF.EmitLoadOfScalar( |
| 4825 | CGF.EmitLValueForField( |
| 4826 | TDBase, *std::next(KmpTaskTQTyRD->field_begin(), |
| 4827 | KmpTaskTShareds)), |
| 4828 | Loc), |
| 4829 | CGF.getNaturalTypeAlignment(SharedsTy)); |
Ivan A. Kosarev | 1860b52 | 2018-01-25 14:21:55 +0000 | [diff] [blame] | 4830 | LValue Dest = CGF.MakeAddrLValue(KmpTaskSharedsPtr, SharedsTy); |
| 4831 | LValue Src = CGF.MakeAddrLValue(Shareds, SharedsTy); |
Richard Smith | e78fac5 | 2018-04-05 20:52:58 +0000 | [diff] [blame] | 4832 | CGF.EmitAggregateCopy(Dest, Src, SharedsTy, AggValueSlot::DoesNotOverlap); |
Alexey Bataev | 8fc69dc | 2015-05-18 07:54:53 +0000 | [diff] [blame] | 4833 | } |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 4834 | // Emit initial values for private copies (if any). |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4835 | TaskResultTy Result; |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 4836 | if (!Privates.empty()) { |
Alexey Bataev | 8a83159 | 2016-05-10 10:36:51 +0000 | [diff] [blame] | 4837 | emitPrivatesInit(CGF, D, KmpTaskSharedsPtr, Base, KmpTaskTWithPrivatesQTyRD, |
| 4838 | SharedsTy, SharedsPtrTy, Data, Privates, |
| 4839 | /*ForDup=*/false); |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4840 | if (isOpenMPTaskLoopDirective(D.getDirectiveKind()) && |
| 4841 | (!Data.LastprivateVars.empty() || checkInitIsRequired(CGF, Privates))) { |
| 4842 | Result.TaskDupFn = emitTaskDupFunction( |
| 4843 | CGM, Loc, D, KmpTaskTWithPrivatesPtrQTy, KmpTaskTWithPrivatesQTyRD, |
| 4844 | KmpTaskTQTyRD, SharedsTy, SharedsPtrTy, Data, Privates, |
| 4845 | /*WithLastIter=*/!Data.LastprivateVars.empty()); |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 4846 | } |
| 4847 | } |
Alexey Bataev | ad537bb | 2016-05-30 09:06:50 +0000 | [diff] [blame] | 4848 | // Fields of union "kmp_cmplrdata_t" for destructors and priority. |
| 4849 | enum { Priority = 0, Destructors = 1 }; |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 4850 | // Provide pointer to function with destructors for privates. |
Alexey Bataev | ad537bb | 2016-05-30 09:06:50 +0000 | [diff] [blame] | 4851 | auto FI = std::next(KmpTaskTQTyRD->field_begin(), Data1); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 4852 | const RecordDecl *KmpCmplrdataUD = |
| 4853 | (*FI)->getType()->getAsUnionType()->getDecl(); |
Alexey Bataev | ad537bb | 2016-05-30 09:06:50 +0000 | [diff] [blame] | 4854 | if (NeedsCleanup) { |
| 4855 | llvm::Value *DestructorFn = emitDestructorsFunction( |
| 4856 | CGM, Loc, KmpInt32Ty, KmpTaskTWithPrivatesPtrQTy, |
| 4857 | KmpTaskTWithPrivatesQTy); |
| 4858 | LValue Data1LV = CGF.EmitLValueForField(TDBase, *FI); |
| 4859 | LValue DestructorsLV = CGF.EmitLValueForField( |
| 4860 | Data1LV, *std::next(KmpCmplrdataUD->field_begin(), Destructors)); |
| 4861 | CGF.EmitStoreOfScalar(CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
| 4862 | DestructorFn, KmpRoutineEntryPtrTy), |
| 4863 | DestructorsLV); |
| 4864 | } |
| 4865 | // Set priority. |
| 4866 | if (Data.Priority.getInt()) { |
| 4867 | LValue Data2LV = CGF.EmitLValueForField( |
| 4868 | TDBase, *std::next(KmpTaskTQTyRD->field_begin(), Data2)); |
| 4869 | LValue PriorityLV = CGF.EmitLValueForField( |
| 4870 | Data2LV, *std::next(KmpCmplrdataUD->field_begin(), Priority)); |
| 4871 | CGF.EmitStoreOfScalar(Data.Priority.getPointer(), PriorityLV); |
| 4872 | } |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 4873 | Result.NewTask = NewTask; |
| 4874 | Result.TaskEntry = TaskEntry; |
| 4875 | Result.NewTaskNewTaskTTy = NewTaskNewTaskTTy; |
| 4876 | Result.TDBase = TDBase; |
| 4877 | Result.KmpTaskTQTyRD = KmpTaskTQTyRD; |
| 4878 | return Result; |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4879 | } |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 4880 | |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 4881 | void CGOpenMPRuntime::emitTaskCall(CodeGenFunction &CGF, SourceLocation Loc, |
| 4882 | const OMPExecutableDirective &D, |
| 4883 | llvm::Value *TaskFunction, |
| 4884 | QualType SharedsTy, Address Shareds, |
| 4885 | const Expr *IfCond, |
| 4886 | const OMPTaskDataTy &Data) { |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4887 | if (!CGF.HaveInsertPoint()) |
| 4888 | return; |
| 4889 | |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 4890 | TaskResultTy Result = |
| 4891 | emitTaskInit(CGF, Loc, D, TaskFunction, SharedsTy, Shareds, Data); |
| 4892 | llvm::Value *NewTask = Result.NewTask; |
| 4893 | llvm::Value *TaskEntry = Result.TaskEntry; |
| 4894 | llvm::Value *NewTaskNewTaskTTy = Result.NewTaskNewTaskTTy; |
| 4895 | LValue TDBase = Result.TDBase; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 4896 | const RecordDecl *KmpTaskTQTyRD = Result.KmpTaskTQTyRD; |
| 4897 | ASTContext &C = CGM.getContext(); |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 4898 | // Process list of dependences. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4899 | Address DependenciesArray = Address::invalid(); |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 4900 | unsigned NumDependencies = Data.Dependences.size(); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4901 | if (NumDependencies) { |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 4902 | // Dependence kind for RTL. |
Alexey Bataev | 92e82f9 | 2015-11-23 13:33:42 +0000 | [diff] [blame] | 4903 | enum RTLDependenceKindTy { DepIn = 0x01, DepInOut = 0x3 }; |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 4904 | enum RTLDependInfoFieldsTy { BaseAddr, Len, Flags }; |
| 4905 | RecordDecl *KmpDependInfoRD; |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 4906 | QualType FlagsTy = |
| 4907 | C.getIntTypeForBitwidth(C.getTypeSize(C.BoolTy), /*Signed=*/false); |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 4908 | llvm::Type *LLVMFlagsTy = CGF.ConvertTypeForMem(FlagsTy); |
| 4909 | if (KmpDependInfoTy.isNull()) { |
| 4910 | KmpDependInfoRD = C.buildImplicitRecord("kmp_depend_info"); |
| 4911 | KmpDependInfoRD->startDefinition(); |
| 4912 | addFieldToRecordDecl(C, KmpDependInfoRD, C.getIntPtrType()); |
| 4913 | addFieldToRecordDecl(C, KmpDependInfoRD, C.getSizeType()); |
| 4914 | addFieldToRecordDecl(C, KmpDependInfoRD, FlagsTy); |
| 4915 | KmpDependInfoRD->completeDefinition(); |
| 4916 | KmpDependInfoTy = C.getRecordType(KmpDependInfoRD); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 4917 | } else { |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 4918 | KmpDependInfoRD = cast<RecordDecl>(KmpDependInfoTy->getAsTagDecl()); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 4919 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4920 | CharUnits DependencySize = C.getTypeSizeInChars(KmpDependInfoTy); |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 4921 | // Define type kmp_depend_info[<Dependences.size()>]; |
| 4922 | QualType KmpDependInfoArrayTy = C.getConstantArrayType( |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4923 | KmpDependInfoTy, llvm::APInt(/*numBits=*/64, NumDependencies), |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 4924 | ArrayType::Normal, /*IndexTypeQuals=*/0); |
| 4925 | // kmp_depend_info[<Dependences.size()>] deps; |
Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 4926 | DependenciesArray = |
| 4927 | CGF.CreateMemTemp(KmpDependInfoArrayTy, ".dep.arr.addr"); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 4928 | for (unsigned I = 0; I < NumDependencies; ++I) { |
| 4929 | const Expr *E = Data.Dependences[I].second; |
| 4930 | LValue Addr = CGF.EmitLValue(E); |
Alexey Bataev | d6fdc8b | 2015-08-31 07:32:19 +0000 | [diff] [blame] | 4931 | llvm::Value *Size; |
| 4932 | QualType Ty = E->getType(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 4933 | if (const auto *ASE = |
| 4934 | dyn_cast<OMPArraySectionExpr>(E->IgnoreParenImpCasts())) { |
Alexey Bataev | d6fdc8b | 2015-08-31 07:32:19 +0000 | [diff] [blame] | 4935 | LValue UpAddrLVal = |
| 4936 | CGF.EmitOMPArraySectionExpr(ASE, /*LowerBound=*/false); |
| 4937 | llvm::Value *UpAddr = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4938 | CGF.Builder.CreateConstGEP1_32(UpAddrLVal.getPointer(), /*Idx0=*/1); |
Alexey Bataev | d6fdc8b | 2015-08-31 07:32:19 +0000 | [diff] [blame] | 4939 | llvm::Value *LowIntPtr = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4940 | CGF.Builder.CreatePtrToInt(Addr.getPointer(), CGM.SizeTy); |
Alexey Bataev | d6fdc8b | 2015-08-31 07:32:19 +0000 | [diff] [blame] | 4941 | llvm::Value *UpIntPtr = CGF.Builder.CreatePtrToInt(UpAddr, CGM.SizeTy); |
| 4942 | Size = CGF.Builder.CreateNUWSub(UpIntPtr, LowIntPtr); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 4943 | } else { |
Alexey Bataev | 1189bd0 | 2016-01-26 12:20:39 +0000 | [diff] [blame] | 4944 | Size = CGF.getTypeSize(Ty); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 4945 | } |
| 4946 | LValue Base = CGF.MakeAddrLValue( |
| 4947 | CGF.Builder.CreateConstArrayGEP(DependenciesArray, I, DependencySize), |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 4948 | KmpDependInfoTy); |
| 4949 | // deps[i].base_addr = &<Dependences[i].second>; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 4950 | LValue BaseAddrLVal = CGF.EmitLValueForField( |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 4951 | Base, *std::next(KmpDependInfoRD->field_begin(), BaseAddr)); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4952 | CGF.EmitStoreOfScalar( |
| 4953 | CGF.Builder.CreatePtrToInt(Addr.getPointer(), CGF.IntPtrTy), |
| 4954 | BaseAddrLVal); |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 4955 | // deps[i].len = sizeof(<Dependences[i].second>); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 4956 | LValue LenLVal = CGF.EmitLValueForField( |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 4957 | Base, *std::next(KmpDependInfoRD->field_begin(), Len)); |
| 4958 | CGF.EmitStoreOfScalar(Size, LenLVal); |
| 4959 | // deps[i].flags = <Dependences[i].first>; |
| 4960 | RTLDependenceKindTy DepKind; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 4961 | switch (Data.Dependences[I].first) { |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 4962 | case OMPC_DEPEND_in: |
| 4963 | DepKind = DepIn; |
| 4964 | break; |
Alexey Bataev | 92e82f9 | 2015-11-23 13:33:42 +0000 | [diff] [blame] | 4965 | // Out and InOut dependencies must use the same code. |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 4966 | case OMPC_DEPEND_out: |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 4967 | case OMPC_DEPEND_inout: |
| 4968 | DepKind = DepInOut; |
| 4969 | break; |
Alexey Bataev | eb48235 | 2015-12-18 05:05:56 +0000 | [diff] [blame] | 4970 | case OMPC_DEPEND_source: |
Alexey Bataev | a636c7f | 2015-12-23 10:27:45 +0000 | [diff] [blame] | 4971 | case OMPC_DEPEND_sink: |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 4972 | case OMPC_DEPEND_unknown: |
| 4973 | llvm_unreachable("Unknown task dependence type"); |
| 4974 | } |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 4975 | LValue FlagsLVal = CGF.EmitLValueForField( |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 4976 | Base, *std::next(KmpDependInfoRD->field_begin(), Flags)); |
| 4977 | CGF.EmitStoreOfScalar(llvm::ConstantInt::get(LLVMFlagsTy, DepKind), |
| 4978 | FlagsLVal); |
| 4979 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4980 | DependenciesArray = CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
| 4981 | CGF.Builder.CreateStructGEP(DependenciesArray, 0, CharUnits::Zero()), |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 4982 | CGF.VoidPtrTy); |
| 4983 | } |
| 4984 | |
Alexander Kornienko | 2a8c18d | 2018-04-06 15:14:32 +0000 | [diff] [blame] | 4985 | // 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] | 4986 | // libcall. |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 4987 | // Build kmp_int32 __kmpc_omp_task_with_deps(ident_t *, kmp_int32 gtid, |
| 4988 | // kmp_task_t *new_task, kmp_int32 ndeps, kmp_depend_info_t *dep_list, |
| 4989 | // kmp_int32 ndeps_noalias, kmp_depend_info_t *noalias_dep_list) if dependence |
| 4990 | // list is not empty |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 4991 | llvm::Value *ThreadID = getThreadID(CGF, Loc); |
| 4992 | llvm::Value *UpLoc = emitUpdateLocation(CGF, Loc); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4993 | llvm::Value *TaskArgs[] = { UpLoc, ThreadID, NewTask }; |
| 4994 | llvm::Value *DepTaskArgs[7]; |
| 4995 | if (NumDependencies) { |
| 4996 | DepTaskArgs[0] = UpLoc; |
| 4997 | DepTaskArgs[1] = ThreadID; |
| 4998 | DepTaskArgs[2] = NewTask; |
| 4999 | DepTaskArgs[3] = CGF.Builder.getInt32(NumDependencies); |
| 5000 | DepTaskArgs[4] = DependenciesArray.getPointer(); |
| 5001 | DepTaskArgs[5] = CGF.Builder.getInt32(0); |
| 5002 | DepTaskArgs[6] = llvm::ConstantPointerNull::get(CGF.VoidPtrTy); |
| 5003 | } |
Malcolm Parsons | c6e4583 | 2017-01-13 18:55:32 +0000 | [diff] [blame] | 5004 | auto &&ThenCodeGen = [this, &Data, TDBase, KmpTaskTQTyRD, NumDependencies, |
| 5005 | &TaskArgs, |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5006 | &DepTaskArgs](CodeGenFunction &CGF, PrePostActionTy &) { |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 5007 | if (!Data.Tied) { |
Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 5008 | auto PartIdFI = std::next(KmpTaskTQTyRD->field_begin(), KmpTaskTPartId); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 5009 | LValue PartIdLVal = CGF.EmitLValueForField(TDBase, *PartIdFI); |
Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 5010 | CGF.EmitStoreOfScalar(CGF.Builder.getInt32(0), PartIdLVal); |
| 5011 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5012 | if (NumDependencies) { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5013 | CGF.EmitRuntimeCall( |
Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 5014 | createRuntimeFunction(OMPRTL__kmpc_omp_task_with_deps), DepTaskArgs); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5015 | } else { |
Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 5016 | CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_omp_task), |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5017 | TaskArgs); |
| 5018 | } |
Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 5019 | // Check if parent region is untied and build return for untied task; |
| 5020 | if (auto *Region = |
| 5021 | dyn_cast_or_null<CGOpenMPRegionInfo>(CGF.CapturedStmtInfo)) |
| 5022 | Region->emitUntiedSwitch(CGF); |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 5023 | }; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5024 | |
| 5025 | llvm::Value *DepWaitTaskArgs[6]; |
| 5026 | if (NumDependencies) { |
| 5027 | DepWaitTaskArgs[0] = UpLoc; |
| 5028 | DepWaitTaskArgs[1] = ThreadID; |
| 5029 | DepWaitTaskArgs[2] = CGF.Builder.getInt32(NumDependencies); |
| 5030 | DepWaitTaskArgs[3] = DependenciesArray.getPointer(); |
| 5031 | DepWaitTaskArgs[4] = CGF.Builder.getInt32(0); |
| 5032 | DepWaitTaskArgs[5] = llvm::ConstantPointerNull::get(CGF.VoidPtrTy); |
| 5033 | } |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5034 | auto &&ElseCodeGen = [&TaskArgs, ThreadID, NewTaskNewTaskTTy, TaskEntry, |
Alexey Bataev | 3c595a6 | 2017-08-14 15:01:03 +0000 | [diff] [blame] | 5035 | NumDependencies, &DepWaitTaskArgs, |
| 5036 | Loc](CodeGenFunction &CGF, PrePostActionTy &) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 5037 | CGOpenMPRuntime &RT = CGF.CGM.getOpenMPRuntime(); |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 5038 | CodeGenFunction::RunCleanupsScope LocalScope(CGF); |
| 5039 | // Build void __kmpc_omp_wait_deps(ident_t *, kmp_int32 gtid, |
| 5040 | // kmp_int32 ndeps, kmp_depend_info_t *dep_list, kmp_int32 |
| 5041 | // ndeps_noalias, kmp_depend_info_t *noalias_dep_list); if dependence info |
| 5042 | // is specified. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5043 | if (NumDependencies) |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5044 | CGF.EmitRuntimeCall(RT.createRuntimeFunction(OMPRTL__kmpc_omp_wait_deps), |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 5045 | DepWaitTaskArgs); |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5046 | // Call proxy_task_entry(gtid, new_task); |
Alexey Bataev | 3c595a6 | 2017-08-14 15:01:03 +0000 | [diff] [blame] | 5047 | auto &&CodeGen = [TaskEntry, ThreadID, NewTaskNewTaskTTy, |
| 5048 | Loc](CodeGenFunction &CGF, PrePostActionTy &Action) { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5049 | Action.Enter(CGF); |
| 5050 | llvm::Value *OutlinedFnArgs[] = {ThreadID, NewTaskNewTaskTTy}; |
Alexey Bataev | 3c595a6 | 2017-08-14 15:01:03 +0000 | [diff] [blame] | 5051 | CGF.CGM.getOpenMPRuntime().emitOutlinedFunctionCall(CGF, Loc, TaskEntry, |
Alexey Bataev | 2c7eee5 | 2017-08-04 19:10:54 +0000 | [diff] [blame] | 5052 | OutlinedFnArgs); |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5053 | }; |
| 5054 | |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 5055 | // Build void __kmpc_omp_task_begin_if0(ident_t *, kmp_int32 gtid, |
| 5056 | // kmp_task_t *new_task); |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 5057 | // Build void __kmpc_omp_task_complete_if0(ident_t *, kmp_int32 gtid, |
| 5058 | // kmp_task_t *new_task); |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5059 | RegionCodeGenTy RCG(CodeGen); |
| 5060 | CommonActionTy Action( |
| 5061 | RT.createRuntimeFunction(OMPRTL__kmpc_omp_task_begin_if0), TaskArgs, |
| 5062 | RT.createRuntimeFunction(OMPRTL__kmpc_omp_task_complete_if0), TaskArgs); |
| 5063 | RCG.setAction(Action); |
| 5064 | RCG(CGF); |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 5065 | }; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5066 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 5067 | if (IfCond) { |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 5068 | emitOMPIfClause(CGF, IfCond, ThenCodeGen, ElseCodeGen); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 5069 | } else { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5070 | RegionCodeGenTy ThenRCG(ThenCodeGen); |
| 5071 | ThenRCG(CGF); |
Alexey Bataev | f539faa | 2016-03-28 12:58:34 +0000 | [diff] [blame] | 5072 | } |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 5073 | } |
| 5074 | |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 5075 | void CGOpenMPRuntime::emitTaskLoopCall(CodeGenFunction &CGF, SourceLocation Loc, |
| 5076 | const OMPLoopDirective &D, |
| 5077 | llvm::Value *TaskFunction, |
| 5078 | QualType SharedsTy, Address Shareds, |
| 5079 | const Expr *IfCond, |
| 5080 | const OMPTaskDataTy &Data) { |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 5081 | if (!CGF.HaveInsertPoint()) |
| 5082 | return; |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 5083 | TaskResultTy Result = |
| 5084 | emitTaskInit(CGF, Loc, D, TaskFunction, SharedsTy, Shareds, Data); |
Alexander Kornienko | 2a8c18d | 2018-04-06 15:14:32 +0000 | [diff] [blame] | 5085 | // 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] | 5086 | // libcall. |
| 5087 | // Call to void __kmpc_taskloop(ident_t *loc, int gtid, kmp_task_t *task, int |
| 5088 | // if_val, kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st, int nogroup, int |
| 5089 | // sched, kmp_uint64 grainsize, void *task_dup); |
| 5090 | llvm::Value *ThreadID = getThreadID(CGF, Loc); |
| 5091 | llvm::Value *UpLoc = emitUpdateLocation(CGF, Loc); |
| 5092 | llvm::Value *IfVal; |
| 5093 | if (IfCond) { |
| 5094 | IfVal = CGF.Builder.CreateIntCast(CGF.EvaluateExprAsBool(IfCond), CGF.IntTy, |
| 5095 | /*isSigned=*/true); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 5096 | } else { |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 5097 | IfVal = llvm::ConstantInt::getSigned(CGF.IntTy, /*V=*/1); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 5098 | } |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 5099 | |
| 5100 | LValue LBLVal = CGF.EmitLValueForField( |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 5101 | Result.TDBase, |
| 5102 | *std::next(Result.KmpTaskTQTyRD->field_begin(), KmpTaskTLowerBound)); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 5103 | const auto *LBVar = |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 5104 | cast<VarDecl>(cast<DeclRefExpr>(D.getLowerBoundVariable())->getDecl()); |
| 5105 | CGF.EmitAnyExprToMem(LBVar->getInit(), LBLVal.getAddress(), LBLVal.getQuals(), |
| 5106 | /*IsInitializer=*/true); |
| 5107 | LValue UBLVal = CGF.EmitLValueForField( |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 5108 | Result.TDBase, |
| 5109 | *std::next(Result.KmpTaskTQTyRD->field_begin(), KmpTaskTUpperBound)); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 5110 | const auto *UBVar = |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 5111 | cast<VarDecl>(cast<DeclRefExpr>(D.getUpperBoundVariable())->getDecl()); |
| 5112 | CGF.EmitAnyExprToMem(UBVar->getInit(), UBLVal.getAddress(), UBLVal.getQuals(), |
| 5113 | /*IsInitializer=*/true); |
| 5114 | LValue StLVal = CGF.EmitLValueForField( |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 5115 | Result.TDBase, |
| 5116 | *std::next(Result.KmpTaskTQTyRD->field_begin(), KmpTaskTStride)); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 5117 | const auto *StVar = |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 5118 | cast<VarDecl>(cast<DeclRefExpr>(D.getStrideVariable())->getDecl()); |
| 5119 | CGF.EmitAnyExprToMem(StVar->getInit(), StLVal.getAddress(), StLVal.getQuals(), |
| 5120 | /*IsInitializer=*/true); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 5121 | // Store reductions address. |
| 5122 | LValue RedLVal = CGF.EmitLValueForField( |
| 5123 | Result.TDBase, |
| 5124 | *std::next(Result.KmpTaskTQTyRD->field_begin(), KmpTaskTReductions)); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 5125 | if (Data.Reductions) { |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 5126 | CGF.EmitStoreOfScalar(Data.Reductions, RedLVal); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 5127 | } else { |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 5128 | CGF.EmitNullInitialization(RedLVal.getAddress(), |
| 5129 | CGF.getContext().VoidPtrTy); |
| 5130 | } |
Alexey Bataev | 2b19a6f | 2016-04-28 09:15:06 +0000 | [diff] [blame] | 5131 | enum { NoSchedule = 0, Grainsize = 1, NumTasks = 2 }; |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 5132 | llvm::Value *TaskArgs[] = { |
Alexey Bataev | 3344603 | 2017-07-12 18:09:32 +0000 | [diff] [blame] | 5133 | UpLoc, |
| 5134 | ThreadID, |
| 5135 | Result.NewTask, |
| 5136 | IfVal, |
| 5137 | LBLVal.getPointer(), |
| 5138 | UBLVal.getPointer(), |
Alexey Bataev | a9b9cc0 | 2018-01-23 18:12:38 +0000 | [diff] [blame] | 5139 | CGF.EmitLoadOfScalar(StLVal, Loc), |
Alexey Bataev | 3344603 | 2017-07-12 18:09:32 +0000 | [diff] [blame] | 5140 | llvm::ConstantInt::getNullValue( |
| 5141 | CGF.IntTy), // Always 0 because taskgroup emitted by the compiler |
Alexey Bataev | 2b19a6f | 2016-04-28 09:15:06 +0000 | [diff] [blame] | 5142 | llvm::ConstantInt::getSigned( |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 5143 | CGF.IntTy, Data.Schedule.getPointer() |
| 5144 | ? Data.Schedule.getInt() ? NumTasks : Grainsize |
Alexey Bataev | 2b19a6f | 2016-04-28 09:15:06 +0000 | [diff] [blame] | 5145 | : NoSchedule), |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 5146 | Data.Schedule.getPointer() |
| 5147 | ? CGF.Builder.CreateIntCast(Data.Schedule.getPointer(), CGF.Int64Ty, |
Alexey Bataev | 2b19a6f | 2016-04-28 09:15:06 +0000 | [diff] [blame] | 5148 | /*isSigned=*/false) |
| 5149 | : llvm::ConstantInt::get(CGF.Int64Ty, /*V=*/0), |
Alexey Bataev | 3344603 | 2017-07-12 18:09:32 +0000 | [diff] [blame] | 5150 | Result.TaskDupFn ? CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
| 5151 | Result.TaskDupFn, CGF.VoidPtrTy) |
| 5152 | : llvm::ConstantPointerNull::get(CGF.VoidPtrTy)}; |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 5153 | CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_taskloop), TaskArgs); |
| 5154 | } |
| 5155 | |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5156 | /// \brief Emit reduction operation for each element of array (required for |
| 5157 | /// array sections) LHS op = RHS. |
| 5158 | /// \param Type Type of array. |
| 5159 | /// \param LHSVar Variable on the left side of the reduction operation |
| 5160 | /// (references element of array in original variable). |
| 5161 | /// \param RHSVar Variable on the right side of the reduction operation |
| 5162 | /// (references element of array in original variable). |
| 5163 | /// \param RedOpGen Generator of reduction operation with use of LHSVar and |
| 5164 | /// RHSVar. |
Benjamin Kramer | e003ca2 | 2015-10-28 13:54:16 +0000 | [diff] [blame] | 5165 | static void EmitOMPAggregateReduction( |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5166 | CodeGenFunction &CGF, QualType Type, const VarDecl *LHSVar, |
| 5167 | const VarDecl *RHSVar, |
| 5168 | const llvm::function_ref<void(CodeGenFunction &CGF, const Expr *, |
| 5169 | const Expr *, const Expr *)> &RedOpGen, |
| 5170 | const Expr *XExpr = nullptr, const Expr *EExpr = nullptr, |
| 5171 | const Expr *UpExpr = nullptr) { |
| 5172 | // Perform element-by-element initialization. |
| 5173 | QualType ElementTy; |
| 5174 | Address LHSAddr = CGF.GetAddrOfLocalVar(LHSVar); |
| 5175 | Address RHSAddr = CGF.GetAddrOfLocalVar(RHSVar); |
| 5176 | |
| 5177 | // Drill down to the base element type on both arrays. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 5178 | const ArrayType *ArrayTy = Type->getAsArrayTypeUnsafe(); |
| 5179 | llvm::Value *NumElements = CGF.emitArrayLength(ArrayTy, ElementTy, LHSAddr); |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5180 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 5181 | llvm::Value *RHSBegin = RHSAddr.getPointer(); |
| 5182 | llvm::Value *LHSBegin = LHSAddr.getPointer(); |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5183 | // Cast from pointer to array type to pointer to single element. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 5184 | llvm::Value *LHSEnd = CGF.Builder.CreateGEP(LHSBegin, NumElements); |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5185 | // The basic structure here is a while-do loop. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 5186 | llvm::BasicBlock *BodyBB = CGF.createBasicBlock("omp.arraycpy.body"); |
| 5187 | llvm::BasicBlock *DoneBB = CGF.createBasicBlock("omp.arraycpy.done"); |
| 5188 | llvm::Value *IsEmpty = |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5189 | CGF.Builder.CreateICmpEQ(LHSBegin, LHSEnd, "omp.arraycpy.isempty"); |
| 5190 | CGF.Builder.CreateCondBr(IsEmpty, DoneBB, BodyBB); |
| 5191 | |
| 5192 | // Enter the loop body, making that address the current address. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 5193 | llvm::BasicBlock *EntryBB = CGF.Builder.GetInsertBlock(); |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5194 | CGF.EmitBlock(BodyBB); |
| 5195 | |
| 5196 | CharUnits ElementSize = CGF.getContext().getTypeSizeInChars(ElementTy); |
| 5197 | |
| 5198 | llvm::PHINode *RHSElementPHI = CGF.Builder.CreatePHI( |
| 5199 | RHSBegin->getType(), 2, "omp.arraycpy.srcElementPast"); |
| 5200 | RHSElementPHI->addIncoming(RHSBegin, EntryBB); |
| 5201 | Address RHSElementCurrent = |
| 5202 | Address(RHSElementPHI, |
| 5203 | RHSAddr.getAlignment().alignmentOfArrayElement(ElementSize)); |
| 5204 | |
| 5205 | llvm::PHINode *LHSElementPHI = CGF.Builder.CreatePHI( |
| 5206 | LHSBegin->getType(), 2, "omp.arraycpy.destElementPast"); |
| 5207 | LHSElementPHI->addIncoming(LHSBegin, EntryBB); |
| 5208 | Address LHSElementCurrent = |
| 5209 | Address(LHSElementPHI, |
| 5210 | LHSAddr.getAlignment().alignmentOfArrayElement(ElementSize)); |
| 5211 | |
| 5212 | // Emit copy. |
| 5213 | CodeGenFunction::OMPPrivateScope Scope(CGF); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 5214 | Scope.addPrivate(LHSVar, [=]() { return LHSElementCurrent; }); |
| 5215 | Scope.addPrivate(RHSVar, [=]() { return RHSElementCurrent; }); |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5216 | Scope.Privatize(); |
| 5217 | RedOpGen(CGF, XExpr, EExpr, UpExpr); |
| 5218 | Scope.ForceCleanup(); |
| 5219 | |
| 5220 | // Shift the address forward by one element. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 5221 | llvm::Value *LHSElementNext = CGF.Builder.CreateConstGEP1_32( |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5222 | LHSElementPHI, /*Idx0=*/1, "omp.arraycpy.dest.element"); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 5223 | llvm::Value *RHSElementNext = CGF.Builder.CreateConstGEP1_32( |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5224 | RHSElementPHI, /*Idx0=*/1, "omp.arraycpy.src.element"); |
| 5225 | // Check whether we've reached the end. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 5226 | llvm::Value *Done = |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5227 | CGF.Builder.CreateICmpEQ(LHSElementNext, LHSEnd, "omp.arraycpy.done"); |
| 5228 | CGF.Builder.CreateCondBr(Done, DoneBB, BodyBB); |
| 5229 | LHSElementPHI->addIncoming(LHSElementNext, CGF.Builder.GetInsertBlock()); |
| 5230 | RHSElementPHI->addIncoming(RHSElementNext, CGF.Builder.GetInsertBlock()); |
| 5231 | |
| 5232 | // Done. |
| 5233 | CGF.EmitBlock(DoneBB, /*IsFinished=*/true); |
| 5234 | } |
| 5235 | |
Alexey Bataev | a839ddd | 2016-03-17 10:19:46 +0000 | [diff] [blame] | 5236 | /// Emit reduction combiner. If the combiner is a simple expression emit it as |
| 5237 | /// is, otherwise consider it as combiner of UDR decl and emit it as a call of |
| 5238 | /// UDR combiner function. |
| 5239 | static void emitReductionCombiner(CodeGenFunction &CGF, |
| 5240 | const Expr *ReductionOp) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 5241 | if (const auto *CE = dyn_cast<CallExpr>(ReductionOp)) |
| 5242 | if (const auto *OVE = dyn_cast<OpaqueValueExpr>(CE->getCallee())) |
| 5243 | if (const auto *DRE = |
Alexey Bataev | a839ddd | 2016-03-17 10:19:46 +0000 | [diff] [blame] | 5244 | dyn_cast<DeclRefExpr>(OVE->getSourceExpr()->IgnoreImpCasts())) |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 5245 | if (const auto *DRD = |
| 5246 | dyn_cast<OMPDeclareReductionDecl>(DRE->getDecl())) { |
Alexey Bataev | a839ddd | 2016-03-17 10:19:46 +0000 | [diff] [blame] | 5247 | std::pair<llvm::Function *, llvm::Function *> Reduction = |
| 5248 | CGF.CGM.getOpenMPRuntime().getUserDefinedReduction(DRD); |
| 5249 | RValue Func = RValue::get(Reduction.first); |
| 5250 | CodeGenFunction::OpaqueValueMapping Map(CGF, OVE, Func); |
| 5251 | CGF.EmitIgnoredExpr(ReductionOp); |
| 5252 | return; |
| 5253 | } |
| 5254 | CGF.EmitIgnoredExpr(ReductionOp); |
| 5255 | } |
| 5256 | |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 5257 | llvm::Value *CGOpenMPRuntime::emitReductionFunction( |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 5258 | CodeGenModule &CGM, SourceLocation Loc, llvm::Type *ArgsType, |
| 5259 | ArrayRef<const Expr *> Privates, ArrayRef<const Expr *> LHSExprs, |
| 5260 | ArrayRef<const Expr *> RHSExprs, ArrayRef<const Expr *> ReductionOps) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 5261 | ASTContext &C = CGM.getContext(); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5262 | |
| 5263 | // void reduction_func(void *LHSArg, void *RHSArg); |
| 5264 | FunctionArgList Args; |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 5265 | ImplicitParamDecl LHSArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, C.VoidPtrTy, |
| 5266 | ImplicitParamDecl::Other); |
| 5267 | ImplicitParamDecl RHSArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, C.VoidPtrTy, |
| 5268 | ImplicitParamDecl::Other); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5269 | Args.push_back(&LHSArg); |
| 5270 | Args.push_back(&RHSArg); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 5271 | const auto &CGFI = |
| 5272 | CGM.getTypes().arrangeBuiltinFunctionDeclaration(C.VoidTy, Args); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5273 | auto *Fn = llvm::Function::Create( |
| 5274 | CGM.getTypes().GetFunctionType(CGFI), llvm::GlobalValue::InternalLinkage, |
| 5275 | ".omp.reduction.reduction_func", &CGM.getModule()); |
Rafael Espindola | 51ec5a9 | 2018-02-28 23:46:35 +0000 | [diff] [blame] | 5276 | CGM.SetInternalFunctionAttributes(GlobalDecl(), Fn, CGFI); |
Alexey Bataev | c0f879b | 2018-04-10 20:10:53 +0000 | [diff] [blame] | 5277 | Fn->setDoesNotRecurse(); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5278 | CodeGenFunction CGF(CGM); |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 5279 | CGF.StartFunction(GlobalDecl(), C.VoidTy, Fn, CGFI, Args, Loc, Loc); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5280 | |
| 5281 | // Dst = (void*[n])(LHSArg); |
| 5282 | // Src = (void*[n])(RHSArg); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5283 | Address LHS(CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
| 5284 | CGF.Builder.CreateLoad(CGF.GetAddrOfLocalVar(&LHSArg)), |
| 5285 | ArgsType), CGF.getPointerAlign()); |
| 5286 | Address RHS(CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
| 5287 | CGF.Builder.CreateLoad(CGF.GetAddrOfLocalVar(&RHSArg)), |
| 5288 | ArgsType), CGF.getPointerAlign()); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5289 | |
| 5290 | // ... |
| 5291 | // *(Type<i>*)lhs[i] = RedOp<i>(*(Type<i>*)lhs[i], *(Type<i>*)rhs[i]); |
| 5292 | // ... |
| 5293 | CodeGenFunction::OMPPrivateScope Scope(CGF); |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5294 | auto IPriv = Privates.begin(); |
| 5295 | unsigned Idx = 0; |
| 5296 | for (unsigned I = 0, E = ReductionOps.size(); I < E; ++I, ++IPriv, ++Idx) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 5297 | const auto *RHSVar = |
| 5298 | cast<VarDecl>(cast<DeclRefExpr>(RHSExprs[I])->getDecl()); |
| 5299 | Scope.addPrivate(RHSVar, [&CGF, RHS, Idx, RHSVar]() { |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5300 | return emitAddrOfVarFromArray(CGF, RHS, Idx, RHSVar); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5301 | }); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 5302 | const auto *LHSVar = |
| 5303 | cast<VarDecl>(cast<DeclRefExpr>(LHSExprs[I])->getDecl()); |
| 5304 | Scope.addPrivate(LHSVar, [&CGF, LHS, Idx, LHSVar]() { |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5305 | return emitAddrOfVarFromArray(CGF, LHS, Idx, LHSVar); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5306 | }); |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5307 | QualType PrivTy = (*IPriv)->getType(); |
Alexey Bataev | 1189bd0 | 2016-01-26 12:20:39 +0000 | [diff] [blame] | 5308 | if (PrivTy->isVariablyModifiedType()) { |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5309 | // Get array size and emit VLA type. |
| 5310 | ++Idx; |
| 5311 | Address Elem = |
| 5312 | CGF.Builder.CreateConstArrayGEP(LHS, Idx, CGF.getPointerSize()); |
| 5313 | llvm::Value *Ptr = CGF.Builder.CreateLoad(Elem); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 5314 | const VariableArrayType *VLA = |
| 5315 | CGF.getContext().getAsVariableArrayType(PrivTy); |
| 5316 | const auto *OVE = cast<OpaqueValueExpr>(VLA->getSizeExpr()); |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5317 | CodeGenFunction::OpaqueValueMapping OpaqueMap( |
Alexey Bataev | 1189bd0 | 2016-01-26 12:20:39 +0000 | [diff] [blame] | 5318 | CGF, OVE, RValue::get(CGF.Builder.CreatePtrToInt(Ptr, CGF.SizeTy))); |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5319 | CGF.EmitVariablyModifiedType(PrivTy); |
| 5320 | } |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5321 | } |
| 5322 | Scope.Privatize(); |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5323 | IPriv = Privates.begin(); |
| 5324 | auto ILHS = LHSExprs.begin(); |
| 5325 | auto IRHS = RHSExprs.begin(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 5326 | for (const Expr *E : ReductionOps) { |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5327 | if ((*IPriv)->getType()->isArrayType()) { |
| 5328 | // Emit reduction for array section. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 5329 | const auto *LHSVar = cast<VarDecl>(cast<DeclRefExpr>(*ILHS)->getDecl()); |
| 5330 | const auto *RHSVar = cast<VarDecl>(cast<DeclRefExpr>(*IRHS)->getDecl()); |
Alexey Bataev | a839ddd | 2016-03-17 10:19:46 +0000 | [diff] [blame] | 5331 | EmitOMPAggregateReduction( |
| 5332 | CGF, (*IPriv)->getType(), LHSVar, RHSVar, |
| 5333 | [=](CodeGenFunction &CGF, const Expr *, const Expr *, const Expr *) { |
| 5334 | emitReductionCombiner(CGF, E); |
| 5335 | }); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 5336 | } else { |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5337 | // Emit reduction for array subscript or single variable. |
Alexey Bataev | a839ddd | 2016-03-17 10:19:46 +0000 | [diff] [blame] | 5338 | emitReductionCombiner(CGF, E); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 5339 | } |
Richard Trieu | cc3949d | 2016-02-18 22:34:54 +0000 | [diff] [blame] | 5340 | ++IPriv; |
| 5341 | ++ILHS; |
| 5342 | ++IRHS; |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5343 | } |
| 5344 | Scope.ForceCleanup(); |
| 5345 | CGF.FinishFunction(); |
| 5346 | return Fn; |
| 5347 | } |
| 5348 | |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 5349 | void CGOpenMPRuntime::emitSingleReductionCombiner(CodeGenFunction &CGF, |
| 5350 | const Expr *ReductionOp, |
| 5351 | const Expr *PrivateRef, |
| 5352 | const DeclRefExpr *LHS, |
| 5353 | const DeclRefExpr *RHS) { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5354 | if (PrivateRef->getType()->isArrayType()) { |
| 5355 | // Emit reduction for array section. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 5356 | const auto *LHSVar = cast<VarDecl>(LHS->getDecl()); |
| 5357 | const auto *RHSVar = cast<VarDecl>(RHS->getDecl()); |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5358 | EmitOMPAggregateReduction( |
| 5359 | CGF, PrivateRef->getType(), LHSVar, RHSVar, |
| 5360 | [=](CodeGenFunction &CGF, const Expr *, const Expr *, const Expr *) { |
| 5361 | emitReductionCombiner(CGF, ReductionOp); |
| 5362 | }); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 5363 | } else { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5364 | // Emit reduction for array subscript or single variable. |
| 5365 | emitReductionCombiner(CGF, ReductionOp); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 5366 | } |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5367 | } |
| 5368 | |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5369 | void CGOpenMPRuntime::emitReduction(CodeGenFunction &CGF, SourceLocation Loc, |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5370 | ArrayRef<const Expr *> Privates, |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5371 | ArrayRef<const Expr *> LHSExprs, |
| 5372 | ArrayRef<const Expr *> RHSExprs, |
| 5373 | ArrayRef<const Expr *> ReductionOps, |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 5374 | ReductionOptionsTy Options) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 5375 | if (!CGF.HaveInsertPoint()) |
| 5376 | return; |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 5377 | |
| 5378 | bool WithNowait = Options.WithNowait; |
| 5379 | bool SimpleReduction = Options.SimpleReduction; |
| 5380 | |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5381 | // Next code should be emitted for reduction: |
| 5382 | // |
| 5383 | // static kmp_critical_name lock = { 0 }; |
| 5384 | // |
| 5385 | // void reduce_func(void *lhs[<n>], void *rhs[<n>]) { |
| 5386 | // *(Type0*)lhs[0] = ReductionOperation0(*(Type0*)lhs[0], *(Type0*)rhs[0]); |
| 5387 | // ... |
| 5388 | // *(Type<n>-1*)lhs[<n>-1] = ReductionOperation<n>-1(*(Type<n>-1*)lhs[<n>-1], |
| 5389 | // *(Type<n>-1*)rhs[<n>-1]); |
| 5390 | // } |
| 5391 | // |
| 5392 | // ... |
| 5393 | // void *RedList[<n>] = {&<RHSExprs>[0], ..., &<RHSExprs>[<n>-1]}; |
| 5394 | // switch (__kmpc_reduce{_nowait}(<loc>, <gtid>, <n>, sizeof(RedList), |
| 5395 | // RedList, reduce_func, &<lock>)) { |
| 5396 | // case 1: |
| 5397 | // ... |
| 5398 | // <LHSExprs>[i] = RedOp<i>(*<LHSExprs>[i], *<RHSExprs>[i]); |
| 5399 | // ... |
| 5400 | // __kmpc_end_reduce{_nowait}(<loc>, <gtid>, &<lock>); |
| 5401 | // break; |
| 5402 | // case 2: |
| 5403 | // ... |
| 5404 | // Atomic(<LHSExprs>[i] = RedOp<i>(*<LHSExprs>[i], *<RHSExprs>[i])); |
| 5405 | // ... |
Alexey Bataev | 69a4779 | 2015-05-07 03:54:03 +0000 | [diff] [blame] | 5406 | // [__kmpc_end_reduce(<loc>, <gtid>, &<lock>);] |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5407 | // break; |
| 5408 | // default:; |
| 5409 | // } |
Alexey Bataev | 89e7e8e | 2015-06-17 06:21:39 +0000 | [diff] [blame] | 5410 | // |
| 5411 | // if SimpleReduction is true, only the next code is generated: |
| 5412 | // ... |
| 5413 | // <LHSExprs>[i] = RedOp<i>(*<LHSExprs>[i], *<RHSExprs>[i]); |
| 5414 | // ... |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5415 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 5416 | ASTContext &C = CGM.getContext(); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5417 | |
Alexey Bataev | 89e7e8e | 2015-06-17 06:21:39 +0000 | [diff] [blame] | 5418 | if (SimpleReduction) { |
| 5419 | CodeGenFunction::RunCleanupsScope Scope(CGF); |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5420 | auto IPriv = Privates.begin(); |
| 5421 | auto ILHS = LHSExprs.begin(); |
| 5422 | auto IRHS = RHSExprs.begin(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 5423 | for (const Expr *E : ReductionOps) { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5424 | emitSingleReductionCombiner(CGF, E, *IPriv, cast<DeclRefExpr>(*ILHS), |
| 5425 | cast<DeclRefExpr>(*IRHS)); |
Richard Trieu | cc3949d | 2016-02-18 22:34:54 +0000 | [diff] [blame] | 5426 | ++IPriv; |
| 5427 | ++ILHS; |
| 5428 | ++IRHS; |
Alexey Bataev | 89e7e8e | 2015-06-17 06:21:39 +0000 | [diff] [blame] | 5429 | } |
| 5430 | return; |
| 5431 | } |
| 5432 | |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5433 | // 1. Build a list of reduction variables. |
| 5434 | // void *RedList[<n>] = {<ReductionVars>[0], ..., <ReductionVars>[<n>-1]}; |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5435 | auto Size = RHSExprs.size(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 5436 | for (const Expr *E : Privates) { |
Alexey Bataev | 1189bd0 | 2016-01-26 12:20:39 +0000 | [diff] [blame] | 5437 | if (E->getType()->isVariablyModifiedType()) |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5438 | // Reserve place for array size. |
| 5439 | ++Size; |
| 5440 | } |
| 5441 | llvm::APInt ArraySize(/*unsigned int numBits=*/32, Size); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5442 | QualType ReductionArrayTy = |
| 5443 | C.getConstantArrayType(C.VoidPtrTy, ArraySize, ArrayType::Normal, |
| 5444 | /*IndexTypeQuals=*/0); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5445 | Address ReductionList = |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5446 | CGF.CreateMemTemp(ReductionArrayTy, ".omp.reduction.red_list"); |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5447 | auto IPriv = Privates.begin(); |
| 5448 | unsigned Idx = 0; |
| 5449 | for (unsigned I = 0, E = RHSExprs.size(); I < E; ++I, ++IPriv, ++Idx) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5450 | Address Elem = |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5451 | CGF.Builder.CreateConstArrayGEP(ReductionList, Idx, CGF.getPointerSize()); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5452 | CGF.Builder.CreateStore( |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5453 | CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5454 | CGF.EmitLValue(RHSExprs[I]).getPointer(), CGF.VoidPtrTy), |
| 5455 | Elem); |
Alexey Bataev | 1189bd0 | 2016-01-26 12:20:39 +0000 | [diff] [blame] | 5456 | if ((*IPriv)->getType()->isVariablyModifiedType()) { |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5457 | // Store array size. |
| 5458 | ++Idx; |
| 5459 | Elem = CGF.Builder.CreateConstArrayGEP(ReductionList, Idx, |
| 5460 | CGF.getPointerSize()); |
Alexey Bataev | 1189bd0 | 2016-01-26 12:20:39 +0000 | [diff] [blame] | 5461 | llvm::Value *Size = CGF.Builder.CreateIntCast( |
| 5462 | CGF.getVLASize( |
| 5463 | CGF.getContext().getAsVariableArrayType((*IPriv)->getType())) |
Sander de Smalen | 891af03a | 2018-02-03 13:55:59 +0000 | [diff] [blame] | 5464 | .NumElts, |
Alexey Bataev | 1189bd0 | 2016-01-26 12:20:39 +0000 | [diff] [blame] | 5465 | CGF.SizeTy, /*isSigned=*/false); |
| 5466 | CGF.Builder.CreateStore(CGF.Builder.CreateIntToPtr(Size, CGF.VoidPtrTy), |
| 5467 | Elem); |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5468 | } |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5469 | } |
| 5470 | |
| 5471 | // 2. Emit reduce_func(). |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 5472 | llvm::Value *ReductionFn = emitReductionFunction( |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 5473 | CGM, Loc, CGF.ConvertTypeForMem(ReductionArrayTy)->getPointerTo(), |
| 5474 | Privates, LHSExprs, RHSExprs, ReductionOps); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5475 | |
| 5476 | // 3. Create static kmp_critical_name lock = { 0 }; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 5477 | llvm::Value *Lock = getCriticalRegionLock(".reduction"); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5478 | |
| 5479 | // 4. Build res = __kmpc_reduce{_nowait}(<loc>, <gtid>, <n>, sizeof(RedList), |
| 5480 | // RedList, reduce_func, &<lock>); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 5481 | llvm::Value *IdentTLoc = emitUpdateLocation(CGF, Loc, OMP_ATOMIC_REDUCE); |
| 5482 | llvm::Value *ThreadId = getThreadID(CGF, Loc); |
| 5483 | llvm::Value *ReductionArrayTySize = CGF.getTypeSize(ReductionArrayTy); |
| 5484 | llvm::Value *RL = CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
Samuel Antao | 4c8035b | 2016-12-12 18:00:20 +0000 | [diff] [blame] | 5485 | ReductionList.getPointer(), CGF.VoidPtrTy); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5486 | llvm::Value *Args[] = { |
| 5487 | IdentTLoc, // ident_t *<loc> |
| 5488 | ThreadId, // i32 <gtid> |
| 5489 | CGF.Builder.getInt32(RHSExprs.size()), // i32 <n> |
| 5490 | ReductionArrayTySize, // size_type sizeof(RedList) |
| 5491 | RL, // void *RedList |
| 5492 | ReductionFn, // void (*) (void *, void *) <reduce_func> |
| 5493 | Lock // kmp_critical_name *&<lock> |
| 5494 | }; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 5495 | llvm::Value *Res = CGF.EmitRuntimeCall( |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5496 | createRuntimeFunction(WithNowait ? OMPRTL__kmpc_reduce_nowait |
| 5497 | : OMPRTL__kmpc_reduce), |
| 5498 | Args); |
| 5499 | |
| 5500 | // 5. Build switch(res) |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 5501 | llvm::BasicBlock *DefaultBB = CGF.createBasicBlock(".omp.reduction.default"); |
| 5502 | llvm::SwitchInst *SwInst = |
| 5503 | CGF.Builder.CreateSwitch(Res, DefaultBB, /*NumCases=*/2); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5504 | |
| 5505 | // 6. Build case 1: |
| 5506 | // ... |
| 5507 | // <LHSExprs>[i] = RedOp<i>(*<LHSExprs>[i], *<RHSExprs>[i]); |
| 5508 | // ... |
| 5509 | // __kmpc_end_reduce{_nowait}(<loc>, <gtid>, &<lock>); |
| 5510 | // break; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 5511 | llvm::BasicBlock *Case1BB = CGF.createBasicBlock(".omp.reduction.case1"); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5512 | SwInst->addCase(CGF.Builder.getInt32(1), Case1BB); |
| 5513 | CGF.EmitBlock(Case1BB); |
| 5514 | |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5515 | // Add emission of __kmpc_end_reduce{_nowait}(<loc>, <gtid>, &<lock>); |
| 5516 | llvm::Value *EndArgs[] = { |
| 5517 | IdentTLoc, // ident_t *<loc> |
| 5518 | ThreadId, // i32 <gtid> |
| 5519 | Lock // kmp_critical_name *&<lock> |
| 5520 | }; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 5521 | auto &&CodeGen = [Privates, LHSExprs, RHSExprs, ReductionOps]( |
| 5522 | CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 5523 | CGOpenMPRuntime &RT = CGF.CGM.getOpenMPRuntime(); |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5524 | auto IPriv = Privates.begin(); |
| 5525 | auto ILHS = LHSExprs.begin(); |
| 5526 | auto IRHS = RHSExprs.begin(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 5527 | for (const Expr *E : ReductionOps) { |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 5528 | RT.emitSingleReductionCombiner(CGF, E, *IPriv, cast<DeclRefExpr>(*ILHS), |
| 5529 | cast<DeclRefExpr>(*IRHS)); |
Richard Trieu | cc3949d | 2016-02-18 22:34:54 +0000 | [diff] [blame] | 5530 | ++IPriv; |
| 5531 | ++ILHS; |
| 5532 | ++IRHS; |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5533 | } |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5534 | }; |
| 5535 | RegionCodeGenTy RCG(CodeGen); |
| 5536 | CommonActionTy Action( |
| 5537 | nullptr, llvm::None, |
| 5538 | createRuntimeFunction(WithNowait ? OMPRTL__kmpc_end_reduce_nowait |
| 5539 | : OMPRTL__kmpc_end_reduce), |
| 5540 | EndArgs); |
| 5541 | RCG.setAction(Action); |
| 5542 | RCG(CGF); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5543 | |
| 5544 | CGF.EmitBranch(DefaultBB); |
| 5545 | |
| 5546 | // 7. Build case 2: |
| 5547 | // ... |
| 5548 | // Atomic(<LHSExprs>[i] = RedOp<i>(*<LHSExprs>[i], *<RHSExprs>[i])); |
| 5549 | // ... |
| 5550 | // break; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 5551 | llvm::BasicBlock *Case2BB = CGF.createBasicBlock(".omp.reduction.case2"); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5552 | SwInst->addCase(CGF.Builder.getInt32(2), Case2BB); |
| 5553 | CGF.EmitBlock(Case2BB); |
| 5554 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 5555 | auto &&AtomicCodeGen = [Loc, Privates, LHSExprs, RHSExprs, ReductionOps]( |
| 5556 | CodeGenFunction &CGF, PrePostActionTy &Action) { |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5557 | auto ILHS = LHSExprs.begin(); |
| 5558 | auto IRHS = RHSExprs.begin(); |
| 5559 | auto IPriv = Privates.begin(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 5560 | for (const Expr *E : ReductionOps) { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5561 | const Expr *XExpr = nullptr; |
| 5562 | const Expr *EExpr = nullptr; |
| 5563 | const Expr *UpExpr = nullptr; |
| 5564 | BinaryOperatorKind BO = BO_Comma; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 5565 | if (const auto *BO = dyn_cast<BinaryOperator>(E)) { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5566 | if (BO->getOpcode() == BO_Assign) { |
| 5567 | XExpr = BO->getLHS(); |
| 5568 | UpExpr = BO->getRHS(); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5569 | } |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5570 | } |
| 5571 | // Try to emit update expression as a simple atomic. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 5572 | const Expr *RHSExpr = UpExpr; |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5573 | if (RHSExpr) { |
| 5574 | // Analyze RHS part of the whole expression. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 5575 | if (const auto *ACO = dyn_cast<AbstractConditionalOperator>( |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5576 | RHSExpr->IgnoreParenImpCasts())) { |
| 5577 | // If this is a conditional operator, analyze its condition for |
| 5578 | // min/max reduction operator. |
| 5579 | RHSExpr = ACO->getCond(); |
Alexey Bataev | 69a4779 | 2015-05-07 03:54:03 +0000 | [diff] [blame] | 5580 | } |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 5581 | if (const auto *BORHS = |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5582 | dyn_cast<BinaryOperator>(RHSExpr->IgnoreParenImpCasts())) { |
| 5583 | EExpr = BORHS->getRHS(); |
| 5584 | BO = BORHS->getOpcode(); |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5585 | } |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5586 | } |
| 5587 | if (XExpr) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 5588 | const auto *VD = cast<VarDecl>(cast<DeclRefExpr>(*ILHS)->getDecl()); |
Malcolm Parsons | c6e4583 | 2017-01-13 18:55:32 +0000 | [diff] [blame] | 5589 | auto &&AtomicRedGen = [BO, VD, |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5590 | Loc](CodeGenFunction &CGF, const Expr *XExpr, |
| 5591 | const Expr *EExpr, const Expr *UpExpr) { |
| 5592 | LValue X = CGF.EmitLValue(XExpr); |
| 5593 | RValue E; |
| 5594 | if (EExpr) |
| 5595 | E = CGF.EmitAnyExpr(EExpr); |
| 5596 | CGF.EmitOMPAtomicSimpleUpdateExpr( |
JF Bastien | 92f4ef1 | 2016-04-06 17:26:42 +0000 | [diff] [blame] | 5597 | X, E, BO, /*IsXLHSInRHSPart=*/true, |
| 5598 | llvm::AtomicOrdering::Monotonic, Loc, |
Malcolm Parsons | c6e4583 | 2017-01-13 18:55:32 +0000 | [diff] [blame] | 5599 | [&CGF, UpExpr, VD, Loc](RValue XRValue) { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5600 | CodeGenFunction::OMPPrivateScope PrivateScope(CGF); |
| 5601 | PrivateScope.addPrivate( |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 5602 | VD, [&CGF, VD, XRValue, Loc]() { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5603 | Address LHSTemp = CGF.CreateMemTemp(VD->getType()); |
| 5604 | CGF.emitOMPSimpleStore( |
| 5605 | CGF.MakeAddrLValue(LHSTemp, VD->getType()), XRValue, |
| 5606 | VD->getType().getNonReferenceType(), Loc); |
| 5607 | return LHSTemp; |
| 5608 | }); |
| 5609 | (void)PrivateScope.Privatize(); |
| 5610 | return CGF.EmitAnyExpr(UpExpr); |
| 5611 | }); |
| 5612 | }; |
| 5613 | if ((*IPriv)->getType()->isArrayType()) { |
| 5614 | // Emit atomic reduction for array section. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 5615 | const auto *RHSVar = |
| 5616 | cast<VarDecl>(cast<DeclRefExpr>(*IRHS)->getDecl()); |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5617 | EmitOMPAggregateReduction(CGF, (*IPriv)->getType(), VD, RHSVar, |
| 5618 | AtomicRedGen, XExpr, EExpr, UpExpr); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 5619 | } else { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5620 | // Emit atomic reduction for array subscript or single variable. |
| 5621 | AtomicRedGen(CGF, XExpr, EExpr, UpExpr); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 5622 | } |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5623 | } else { |
| 5624 | // Emit as a critical region. |
| 5625 | auto &&CritRedGen = [E, Loc](CodeGenFunction &CGF, const Expr *, |
| 5626 | const Expr *, const Expr *) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 5627 | CGOpenMPRuntime &RT = CGF.CGM.getOpenMPRuntime(); |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5628 | RT.emitCriticalRegion( |
| 5629 | CGF, ".atomic_reduction", |
| 5630 | [=](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 5631 | Action.Enter(CGF); |
| 5632 | emitReductionCombiner(CGF, E); |
| 5633 | }, |
| 5634 | Loc); |
| 5635 | }; |
| 5636 | if ((*IPriv)->getType()->isArrayType()) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 5637 | const auto *LHSVar = |
| 5638 | cast<VarDecl>(cast<DeclRefExpr>(*ILHS)->getDecl()); |
| 5639 | const auto *RHSVar = |
| 5640 | cast<VarDecl>(cast<DeclRefExpr>(*IRHS)->getDecl()); |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5641 | EmitOMPAggregateReduction(CGF, (*IPriv)->getType(), LHSVar, RHSVar, |
| 5642 | CritRedGen); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 5643 | } else { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5644 | CritRedGen(CGF, nullptr, nullptr, nullptr); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 5645 | } |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5646 | } |
Richard Trieu | cc3949d | 2016-02-18 22:34:54 +0000 | [diff] [blame] | 5647 | ++ILHS; |
| 5648 | ++IRHS; |
| 5649 | ++IPriv; |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5650 | } |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5651 | }; |
| 5652 | RegionCodeGenTy AtomicRCG(AtomicCodeGen); |
| 5653 | if (!WithNowait) { |
| 5654 | // Add emission of __kmpc_end_reduce(<loc>, <gtid>, &<lock>); |
| 5655 | llvm::Value *EndArgs[] = { |
| 5656 | IdentTLoc, // ident_t *<loc> |
| 5657 | ThreadId, // i32 <gtid> |
| 5658 | Lock // kmp_critical_name *&<lock> |
| 5659 | }; |
| 5660 | CommonActionTy Action(nullptr, llvm::None, |
| 5661 | createRuntimeFunction(OMPRTL__kmpc_end_reduce), |
| 5662 | EndArgs); |
| 5663 | AtomicRCG.setAction(Action); |
| 5664 | AtomicRCG(CGF); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 5665 | } else { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5666 | AtomicRCG(CGF); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 5667 | } |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5668 | |
| 5669 | CGF.EmitBranch(DefaultBB); |
| 5670 | CGF.EmitBlock(DefaultBB, /*IsFinished=*/true); |
| 5671 | } |
| 5672 | |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 5673 | /// Generates unique name for artificial threadprivate variables. |
Alexey Bataev | 1c44e15 | 2018-03-06 18:59:43 +0000 | [diff] [blame] | 5674 | /// Format is: <Prefix> "." <Decl_mangled_name> "_" "<Decl_start_loc_raw_enc>" |
| 5675 | static std::string generateUniqueName(CodeGenModule &CGM, StringRef Prefix, |
| 5676 | const Expr *Ref) { |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 5677 | SmallString<256> Buffer; |
| 5678 | llvm::raw_svector_ostream Out(Buffer); |
Alexey Bataev | 1c44e15 | 2018-03-06 18:59:43 +0000 | [diff] [blame] | 5679 | const clang::DeclRefExpr *DE; |
| 5680 | const VarDecl *D = ::getBaseDecl(Ref, DE); |
| 5681 | if (!D) |
| 5682 | D = cast<VarDecl>(cast<DeclRefExpr>(Ref)->getDecl()); |
| 5683 | D = D->getCanonicalDecl(); |
| 5684 | Out << Prefix << "." |
| 5685 | << (D->isLocalVarDeclOrParm() ? D->getName() : CGM.getMangledName(D)) |
| 5686 | << "_" << D->getCanonicalDecl()->getLocStart().getRawEncoding(); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 5687 | return Out.str(); |
| 5688 | } |
| 5689 | |
| 5690 | /// Emits reduction initializer function: |
| 5691 | /// \code |
| 5692 | /// void @.red_init(void* %arg) { |
| 5693 | /// %0 = bitcast void* %arg to <type>* |
| 5694 | /// store <type> <init>, <type>* %0 |
| 5695 | /// ret void |
| 5696 | /// } |
| 5697 | /// \endcode |
| 5698 | static llvm::Value *emitReduceInitFunction(CodeGenModule &CGM, |
| 5699 | SourceLocation Loc, |
| 5700 | ReductionCodeGen &RCG, unsigned N) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 5701 | ASTContext &C = CGM.getContext(); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 5702 | FunctionArgList Args; |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 5703 | ImplicitParamDecl Param(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, C.VoidPtrTy, |
| 5704 | ImplicitParamDecl::Other); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 5705 | Args.emplace_back(&Param); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 5706 | const auto &FnInfo = |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 5707 | CGM.getTypes().arrangeBuiltinFunctionDeclaration(C.VoidTy, Args); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 5708 | llvm::FunctionType *FnTy = CGM.getTypes().GetFunctionType(FnInfo); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 5709 | auto *Fn = llvm::Function::Create(FnTy, llvm::GlobalValue::InternalLinkage, |
| 5710 | ".red_init.", &CGM.getModule()); |
Rafael Espindola | 51ec5a9 | 2018-02-28 23:46:35 +0000 | [diff] [blame] | 5711 | CGM.SetInternalFunctionAttributes(GlobalDecl(), Fn, FnInfo); |
Alexey Bataev | c0f879b | 2018-04-10 20:10:53 +0000 | [diff] [blame] | 5712 | Fn->setDoesNotRecurse(); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 5713 | CodeGenFunction CGF(CGM); |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 5714 | CGF.StartFunction(GlobalDecl(), C.VoidTy, Fn, FnInfo, Args, Loc, Loc); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 5715 | Address PrivateAddr = CGF.EmitLoadOfPointer( |
| 5716 | CGF.GetAddrOfLocalVar(&Param), |
| 5717 | C.getPointerType(C.VoidPtrTy).castAs<PointerType>()); |
| 5718 | llvm::Value *Size = nullptr; |
| 5719 | // If the size of the reduction item is non-constant, load it from global |
| 5720 | // threadprivate variable. |
| 5721 | if (RCG.getSizes(N).second) { |
| 5722 | Address SizeAddr = CGM.getOpenMPRuntime().getAddrOfArtificialThreadPrivate( |
| 5723 | CGF, CGM.getContext().getSizeType(), |
Alexey Bataev | 1c44e15 | 2018-03-06 18:59:43 +0000 | [diff] [blame] | 5724 | generateUniqueName(CGM, "reduction_size", RCG.getRefExpr(N))); |
Alexey Bataev | a9b9cc0 | 2018-01-23 18:12:38 +0000 | [diff] [blame] | 5725 | Size = CGF.EmitLoadOfScalar(SizeAddr, /*Volatile=*/false, |
| 5726 | CGM.getContext().getSizeType(), Loc); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 5727 | } |
| 5728 | RCG.emitAggregateType(CGF, N, Size); |
| 5729 | LValue SharedLVal; |
| 5730 | // If initializer uses initializer from declare reduction construct, emit a |
| 5731 | // pointer to the address of the original reduction item (reuired by reduction |
| 5732 | // initializer) |
| 5733 | if (RCG.usesReductionInitializer(N)) { |
| 5734 | Address SharedAddr = |
| 5735 | CGM.getOpenMPRuntime().getAddrOfArtificialThreadPrivate( |
| 5736 | CGF, CGM.getContext().VoidPtrTy, |
Alexey Bataev | 1c44e15 | 2018-03-06 18:59:43 +0000 | [diff] [blame] | 5737 | generateUniqueName(CGM, "reduction", RCG.getRefExpr(N))); |
Alexey Bataev | 21dab12 | 2018-03-09 15:20:30 +0000 | [diff] [blame] | 5738 | SharedAddr = CGF.EmitLoadOfPointer( |
| 5739 | SharedAddr, |
| 5740 | CGM.getContext().VoidPtrTy.castAs<PointerType>()->getTypePtr()); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 5741 | SharedLVal = CGF.MakeAddrLValue(SharedAddr, CGM.getContext().VoidPtrTy); |
| 5742 | } else { |
| 5743 | SharedLVal = CGF.MakeNaturalAlignAddrLValue( |
| 5744 | llvm::ConstantPointerNull::get(CGM.VoidPtrTy), |
| 5745 | CGM.getContext().VoidPtrTy); |
| 5746 | } |
| 5747 | // Emit the initializer: |
| 5748 | // %0 = bitcast void* %arg to <type>* |
| 5749 | // store <type> <init>, <type>* %0 |
| 5750 | RCG.emitInitialization(CGF, N, PrivateAddr, SharedLVal, |
| 5751 | [](CodeGenFunction &) { return false; }); |
| 5752 | CGF.FinishFunction(); |
| 5753 | return Fn; |
| 5754 | } |
| 5755 | |
| 5756 | /// Emits reduction combiner function: |
| 5757 | /// \code |
| 5758 | /// void @.red_comb(void* %arg0, void* %arg1) { |
| 5759 | /// %lhs = bitcast void* %arg0 to <type>* |
| 5760 | /// %rhs = bitcast void* %arg1 to <type>* |
| 5761 | /// %2 = <ReductionOp>(<type>* %lhs, <type>* %rhs) |
| 5762 | /// store <type> %2, <type>* %lhs |
| 5763 | /// ret void |
| 5764 | /// } |
| 5765 | /// \endcode |
| 5766 | static llvm::Value *emitReduceCombFunction(CodeGenModule &CGM, |
| 5767 | SourceLocation Loc, |
| 5768 | ReductionCodeGen &RCG, unsigned N, |
| 5769 | const Expr *ReductionOp, |
| 5770 | const Expr *LHS, const Expr *RHS, |
| 5771 | const Expr *PrivateRef) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 5772 | ASTContext &C = CGM.getContext(); |
| 5773 | const auto *LHSVD = cast<VarDecl>(cast<DeclRefExpr>(LHS)->getDecl()); |
| 5774 | const auto *RHSVD = cast<VarDecl>(cast<DeclRefExpr>(RHS)->getDecl()); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 5775 | FunctionArgList Args; |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 5776 | ImplicitParamDecl ParamInOut(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, |
| 5777 | C.VoidPtrTy, ImplicitParamDecl::Other); |
| 5778 | ImplicitParamDecl ParamIn(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, C.VoidPtrTy, |
| 5779 | ImplicitParamDecl::Other); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 5780 | Args.emplace_back(&ParamInOut); |
| 5781 | Args.emplace_back(&ParamIn); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 5782 | const auto &FnInfo = |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 5783 | CGM.getTypes().arrangeBuiltinFunctionDeclaration(C.VoidTy, Args); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 5784 | llvm::FunctionType *FnTy = CGM.getTypes().GetFunctionType(FnInfo); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 5785 | auto *Fn = llvm::Function::Create(FnTy, llvm::GlobalValue::InternalLinkage, |
| 5786 | ".red_comb.", &CGM.getModule()); |
Rafael Espindola | 51ec5a9 | 2018-02-28 23:46:35 +0000 | [diff] [blame] | 5787 | CGM.SetInternalFunctionAttributes(GlobalDecl(), Fn, FnInfo); |
Alexey Bataev | c0f879b | 2018-04-10 20:10:53 +0000 | [diff] [blame] | 5788 | Fn->setDoesNotRecurse(); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 5789 | CodeGenFunction CGF(CGM); |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 5790 | CGF.StartFunction(GlobalDecl(), C.VoidTy, Fn, FnInfo, Args, Loc, Loc); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 5791 | llvm::Value *Size = nullptr; |
| 5792 | // If the size of the reduction item is non-constant, load it from global |
| 5793 | // threadprivate variable. |
| 5794 | if (RCG.getSizes(N).second) { |
| 5795 | Address SizeAddr = CGM.getOpenMPRuntime().getAddrOfArtificialThreadPrivate( |
| 5796 | CGF, CGM.getContext().getSizeType(), |
Alexey Bataev | 1c44e15 | 2018-03-06 18:59:43 +0000 | [diff] [blame] | 5797 | generateUniqueName(CGM, "reduction_size", RCG.getRefExpr(N))); |
Alexey Bataev | a9b9cc0 | 2018-01-23 18:12:38 +0000 | [diff] [blame] | 5798 | Size = CGF.EmitLoadOfScalar(SizeAddr, /*Volatile=*/false, |
| 5799 | CGM.getContext().getSizeType(), Loc); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 5800 | } |
| 5801 | RCG.emitAggregateType(CGF, N, Size); |
| 5802 | // Remap lhs and rhs variables to the addresses of the function arguments. |
| 5803 | // %lhs = bitcast void* %arg0 to <type>* |
| 5804 | // %rhs = bitcast void* %arg1 to <type>* |
| 5805 | CodeGenFunction::OMPPrivateScope PrivateScope(CGF); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 5806 | PrivateScope.addPrivate(LHSVD, [&C, &CGF, &ParamInOut, LHSVD]() { |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 5807 | // Pull out the pointer to the variable. |
| 5808 | Address PtrAddr = CGF.EmitLoadOfPointer( |
| 5809 | CGF.GetAddrOfLocalVar(&ParamInOut), |
| 5810 | C.getPointerType(C.VoidPtrTy).castAs<PointerType>()); |
| 5811 | return CGF.Builder.CreateElementBitCast( |
| 5812 | PtrAddr, CGF.ConvertTypeForMem(LHSVD->getType())); |
| 5813 | }); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 5814 | PrivateScope.addPrivate(RHSVD, [&C, &CGF, &ParamIn, RHSVD]() { |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 5815 | // Pull out the pointer to the variable. |
| 5816 | Address PtrAddr = CGF.EmitLoadOfPointer( |
| 5817 | CGF.GetAddrOfLocalVar(&ParamIn), |
| 5818 | C.getPointerType(C.VoidPtrTy).castAs<PointerType>()); |
| 5819 | return CGF.Builder.CreateElementBitCast( |
| 5820 | PtrAddr, CGF.ConvertTypeForMem(RHSVD->getType())); |
| 5821 | }); |
| 5822 | PrivateScope.Privatize(); |
| 5823 | // Emit the combiner body: |
| 5824 | // %2 = <ReductionOp>(<type> *%lhs, <type> *%rhs) |
| 5825 | // store <type> %2, <type>* %lhs |
| 5826 | CGM.getOpenMPRuntime().emitSingleReductionCombiner( |
| 5827 | CGF, ReductionOp, PrivateRef, cast<DeclRefExpr>(LHS), |
| 5828 | cast<DeclRefExpr>(RHS)); |
| 5829 | CGF.FinishFunction(); |
| 5830 | return Fn; |
| 5831 | } |
| 5832 | |
| 5833 | /// Emits reduction finalizer function: |
| 5834 | /// \code |
| 5835 | /// void @.red_fini(void* %arg) { |
| 5836 | /// %0 = bitcast void* %arg to <type>* |
| 5837 | /// <destroy>(<type>* %0) |
| 5838 | /// ret void |
| 5839 | /// } |
| 5840 | /// \endcode |
| 5841 | static llvm::Value *emitReduceFiniFunction(CodeGenModule &CGM, |
| 5842 | SourceLocation Loc, |
| 5843 | ReductionCodeGen &RCG, unsigned N) { |
| 5844 | if (!RCG.needCleanups(N)) |
| 5845 | return nullptr; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 5846 | ASTContext &C = CGM.getContext(); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 5847 | FunctionArgList Args; |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 5848 | ImplicitParamDecl Param(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, C.VoidPtrTy, |
| 5849 | ImplicitParamDecl::Other); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 5850 | Args.emplace_back(&Param); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 5851 | const auto &FnInfo = |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 5852 | CGM.getTypes().arrangeBuiltinFunctionDeclaration(C.VoidTy, Args); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 5853 | llvm::FunctionType *FnTy = CGM.getTypes().GetFunctionType(FnInfo); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 5854 | auto *Fn = llvm::Function::Create(FnTy, llvm::GlobalValue::InternalLinkage, |
| 5855 | ".red_fini.", &CGM.getModule()); |
Rafael Espindola | 51ec5a9 | 2018-02-28 23:46:35 +0000 | [diff] [blame] | 5856 | CGM.SetInternalFunctionAttributes(GlobalDecl(), Fn, FnInfo); |
Alexey Bataev | c0f879b | 2018-04-10 20:10:53 +0000 | [diff] [blame] | 5857 | Fn->setDoesNotRecurse(); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 5858 | CodeGenFunction CGF(CGM); |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 5859 | CGF.StartFunction(GlobalDecl(), C.VoidTy, Fn, FnInfo, Args, Loc, Loc); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 5860 | Address PrivateAddr = CGF.EmitLoadOfPointer( |
| 5861 | CGF.GetAddrOfLocalVar(&Param), |
| 5862 | C.getPointerType(C.VoidPtrTy).castAs<PointerType>()); |
| 5863 | llvm::Value *Size = nullptr; |
| 5864 | // If the size of the reduction item is non-constant, load it from global |
| 5865 | // threadprivate variable. |
| 5866 | if (RCG.getSizes(N).second) { |
| 5867 | Address SizeAddr = CGM.getOpenMPRuntime().getAddrOfArtificialThreadPrivate( |
| 5868 | CGF, CGM.getContext().getSizeType(), |
Alexey Bataev | 1c44e15 | 2018-03-06 18:59:43 +0000 | [diff] [blame] | 5869 | generateUniqueName(CGM, "reduction_size", RCG.getRefExpr(N))); |
Alexey Bataev | a9b9cc0 | 2018-01-23 18:12:38 +0000 | [diff] [blame] | 5870 | Size = CGF.EmitLoadOfScalar(SizeAddr, /*Volatile=*/false, |
| 5871 | CGM.getContext().getSizeType(), Loc); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 5872 | } |
| 5873 | RCG.emitAggregateType(CGF, N, Size); |
| 5874 | // Emit the finalizer body: |
| 5875 | // <destroy>(<type>* %0) |
| 5876 | RCG.emitCleanups(CGF, N, PrivateAddr); |
| 5877 | CGF.FinishFunction(); |
| 5878 | return Fn; |
| 5879 | } |
| 5880 | |
| 5881 | llvm::Value *CGOpenMPRuntime::emitTaskReductionInit( |
| 5882 | CodeGenFunction &CGF, SourceLocation Loc, ArrayRef<const Expr *> LHSExprs, |
| 5883 | ArrayRef<const Expr *> RHSExprs, const OMPTaskDataTy &Data) { |
| 5884 | if (!CGF.HaveInsertPoint() || Data.ReductionVars.empty()) |
| 5885 | return nullptr; |
| 5886 | |
| 5887 | // Build typedef struct: |
| 5888 | // kmp_task_red_input { |
| 5889 | // void *reduce_shar; // shared reduction item |
| 5890 | // size_t reduce_size; // size of data item |
| 5891 | // void *reduce_init; // data initialization routine |
| 5892 | // void *reduce_fini; // data finalization routine |
| 5893 | // void *reduce_comb; // data combiner routine |
| 5894 | // kmp_task_red_flags_t flags; // flags for additional info from compiler |
| 5895 | // } kmp_task_red_input_t; |
| 5896 | ASTContext &C = CGM.getContext(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 5897 | RecordDecl *RD = C.buildImplicitRecord("kmp_task_red_input_t"); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 5898 | RD->startDefinition(); |
| 5899 | const FieldDecl *SharedFD = addFieldToRecordDecl(C, RD, C.VoidPtrTy); |
| 5900 | const FieldDecl *SizeFD = addFieldToRecordDecl(C, RD, C.getSizeType()); |
| 5901 | const FieldDecl *InitFD = addFieldToRecordDecl(C, RD, C.VoidPtrTy); |
| 5902 | const FieldDecl *FiniFD = addFieldToRecordDecl(C, RD, C.VoidPtrTy); |
| 5903 | const FieldDecl *CombFD = addFieldToRecordDecl(C, RD, C.VoidPtrTy); |
| 5904 | const FieldDecl *FlagsFD = addFieldToRecordDecl( |
| 5905 | C, RD, C.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/false)); |
| 5906 | RD->completeDefinition(); |
| 5907 | QualType RDType = C.getRecordType(RD); |
| 5908 | unsigned Size = Data.ReductionVars.size(); |
| 5909 | llvm::APInt ArraySize(/*numBits=*/64, Size); |
| 5910 | QualType ArrayRDType = C.getConstantArrayType( |
| 5911 | RDType, ArraySize, ArrayType::Normal, /*IndexTypeQuals=*/0); |
| 5912 | // kmp_task_red_input_t .rd_input.[Size]; |
| 5913 | Address TaskRedInput = CGF.CreateMemTemp(ArrayRDType, ".rd_input."); |
| 5914 | ReductionCodeGen RCG(Data.ReductionVars, Data.ReductionCopies, |
| 5915 | Data.ReductionOps); |
| 5916 | for (unsigned Cnt = 0; Cnt < Size; ++Cnt) { |
| 5917 | // kmp_task_red_input_t &ElemLVal = .rd_input.[Cnt]; |
| 5918 | llvm::Value *Idxs[] = {llvm::ConstantInt::get(CGM.SizeTy, /*V=*/0), |
| 5919 | llvm::ConstantInt::get(CGM.SizeTy, Cnt)}; |
| 5920 | llvm::Value *GEP = CGF.EmitCheckedInBoundsGEP( |
| 5921 | TaskRedInput.getPointer(), Idxs, |
| 5922 | /*SignedIndices=*/false, /*IsSubtraction=*/false, Loc, |
| 5923 | ".rd_input.gep."); |
| 5924 | LValue ElemLVal = CGF.MakeNaturalAlignAddrLValue(GEP, RDType); |
| 5925 | // ElemLVal.reduce_shar = &Shareds[Cnt]; |
| 5926 | LValue SharedLVal = CGF.EmitLValueForField(ElemLVal, SharedFD); |
| 5927 | RCG.emitSharedLValue(CGF, Cnt); |
| 5928 | llvm::Value *CastedShared = |
| 5929 | CGF.EmitCastToVoidPtr(RCG.getSharedLValue(Cnt).getPointer()); |
| 5930 | CGF.EmitStoreOfScalar(CastedShared, SharedLVal); |
| 5931 | RCG.emitAggregateType(CGF, Cnt); |
| 5932 | llvm::Value *SizeValInChars; |
| 5933 | llvm::Value *SizeVal; |
| 5934 | std::tie(SizeValInChars, SizeVal) = RCG.getSizes(Cnt); |
| 5935 | // We use delayed creation/initialization for VLAs, array sections and |
| 5936 | // custom reduction initializations. It is required because runtime does not |
| 5937 | // provide the way to pass the sizes of VLAs/array sections to |
| 5938 | // initializer/combiner/finalizer functions and does not pass the pointer to |
| 5939 | // original reduction item to the initializer. Instead threadprivate global |
| 5940 | // variables are used to store these values and use them in the functions. |
| 5941 | bool DelayedCreation = !!SizeVal; |
| 5942 | SizeValInChars = CGF.Builder.CreateIntCast(SizeValInChars, CGM.SizeTy, |
| 5943 | /*isSigned=*/false); |
| 5944 | LValue SizeLVal = CGF.EmitLValueForField(ElemLVal, SizeFD); |
| 5945 | CGF.EmitStoreOfScalar(SizeValInChars, SizeLVal); |
| 5946 | // ElemLVal.reduce_init = init; |
| 5947 | LValue InitLVal = CGF.EmitLValueForField(ElemLVal, InitFD); |
| 5948 | llvm::Value *InitAddr = |
| 5949 | CGF.EmitCastToVoidPtr(emitReduceInitFunction(CGM, Loc, RCG, Cnt)); |
| 5950 | CGF.EmitStoreOfScalar(InitAddr, InitLVal); |
| 5951 | DelayedCreation = DelayedCreation || RCG.usesReductionInitializer(Cnt); |
| 5952 | // ElemLVal.reduce_fini = fini; |
| 5953 | LValue FiniLVal = CGF.EmitLValueForField(ElemLVal, FiniFD); |
| 5954 | llvm::Value *Fini = emitReduceFiniFunction(CGM, Loc, RCG, Cnt); |
| 5955 | llvm::Value *FiniAddr = Fini |
| 5956 | ? CGF.EmitCastToVoidPtr(Fini) |
| 5957 | : llvm::ConstantPointerNull::get(CGM.VoidPtrTy); |
| 5958 | CGF.EmitStoreOfScalar(FiniAddr, FiniLVal); |
| 5959 | // ElemLVal.reduce_comb = comb; |
| 5960 | LValue CombLVal = CGF.EmitLValueForField(ElemLVal, CombFD); |
| 5961 | llvm::Value *CombAddr = CGF.EmitCastToVoidPtr(emitReduceCombFunction( |
| 5962 | CGM, Loc, RCG, Cnt, Data.ReductionOps[Cnt], LHSExprs[Cnt], |
| 5963 | RHSExprs[Cnt], Data.ReductionCopies[Cnt])); |
| 5964 | CGF.EmitStoreOfScalar(CombAddr, CombLVal); |
| 5965 | // ElemLVal.flags = 0; |
| 5966 | LValue FlagsLVal = CGF.EmitLValueForField(ElemLVal, FlagsFD); |
| 5967 | if (DelayedCreation) { |
| 5968 | CGF.EmitStoreOfScalar( |
| 5969 | llvm::ConstantInt::get(CGM.Int32Ty, /*V=*/1, /*IsSigned=*/true), |
| 5970 | FlagsLVal); |
| 5971 | } else |
| 5972 | CGF.EmitNullInitialization(FlagsLVal.getAddress(), FlagsLVal.getType()); |
| 5973 | } |
| 5974 | // Build call void *__kmpc_task_reduction_init(int gtid, int num_data, void |
| 5975 | // *data); |
| 5976 | llvm::Value *Args[] = { |
| 5977 | CGF.Builder.CreateIntCast(getThreadID(CGF, Loc), CGM.IntTy, |
| 5978 | /*isSigned=*/true), |
| 5979 | llvm::ConstantInt::get(CGM.IntTy, Size, /*isSigned=*/true), |
| 5980 | CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(TaskRedInput.getPointer(), |
| 5981 | CGM.VoidPtrTy)}; |
| 5982 | return CGF.EmitRuntimeCall( |
| 5983 | createRuntimeFunction(OMPRTL__kmpc_task_reduction_init), Args); |
| 5984 | } |
| 5985 | |
| 5986 | void CGOpenMPRuntime::emitTaskReductionFixups(CodeGenFunction &CGF, |
| 5987 | SourceLocation Loc, |
| 5988 | ReductionCodeGen &RCG, |
| 5989 | unsigned N) { |
| 5990 | auto Sizes = RCG.getSizes(N); |
| 5991 | // Emit threadprivate global variable if the type is non-constant |
| 5992 | // (Sizes.second = nullptr). |
| 5993 | if (Sizes.second) { |
| 5994 | llvm::Value *SizeVal = CGF.Builder.CreateIntCast(Sizes.second, CGM.SizeTy, |
| 5995 | /*isSigned=*/false); |
| 5996 | Address SizeAddr = getAddrOfArtificialThreadPrivate( |
| 5997 | CGF, CGM.getContext().getSizeType(), |
Alexey Bataev | 1c44e15 | 2018-03-06 18:59:43 +0000 | [diff] [blame] | 5998 | generateUniqueName(CGM, "reduction_size", RCG.getRefExpr(N))); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 5999 | CGF.Builder.CreateStore(SizeVal, SizeAddr, /*IsVolatile=*/false); |
| 6000 | } |
| 6001 | // Store address of the original reduction item if custom initializer is used. |
| 6002 | if (RCG.usesReductionInitializer(N)) { |
| 6003 | Address SharedAddr = getAddrOfArtificialThreadPrivate( |
| 6004 | CGF, CGM.getContext().VoidPtrTy, |
Alexey Bataev | 1c44e15 | 2018-03-06 18:59:43 +0000 | [diff] [blame] | 6005 | generateUniqueName(CGM, "reduction", RCG.getRefExpr(N))); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 6006 | CGF.Builder.CreateStore( |
| 6007 | CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
| 6008 | RCG.getSharedLValue(N).getPointer(), CGM.VoidPtrTy), |
| 6009 | SharedAddr, /*IsVolatile=*/false); |
| 6010 | } |
| 6011 | } |
| 6012 | |
| 6013 | Address CGOpenMPRuntime::getTaskReductionItem(CodeGenFunction &CGF, |
| 6014 | SourceLocation Loc, |
| 6015 | llvm::Value *ReductionsPtr, |
| 6016 | LValue SharedLVal) { |
| 6017 | // Build call void *__kmpc_task_reduction_get_th_data(int gtid, void *tg, void |
| 6018 | // *d); |
| 6019 | llvm::Value *Args[] = { |
| 6020 | CGF.Builder.CreateIntCast(getThreadID(CGF, Loc), CGM.IntTy, |
| 6021 | /*isSigned=*/true), |
| 6022 | ReductionsPtr, |
| 6023 | CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(SharedLVal.getPointer(), |
| 6024 | CGM.VoidPtrTy)}; |
| 6025 | return Address( |
| 6026 | CGF.EmitRuntimeCall( |
| 6027 | createRuntimeFunction(OMPRTL__kmpc_task_reduction_get_th_data), Args), |
| 6028 | SharedLVal.getAlignment()); |
| 6029 | } |
| 6030 | |
Alexey Bataev | 8b8e202 | 2015-04-27 05:22:09 +0000 | [diff] [blame] | 6031 | void CGOpenMPRuntime::emitTaskwaitCall(CodeGenFunction &CGF, |
| 6032 | SourceLocation Loc) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 6033 | if (!CGF.HaveInsertPoint()) |
| 6034 | return; |
Alexey Bataev | 8b8e202 | 2015-04-27 05:22:09 +0000 | [diff] [blame] | 6035 | // Build call kmp_int32 __kmpc_omp_taskwait(ident_t *loc, kmp_int32 |
| 6036 | // global_tid); |
| 6037 | llvm::Value *Args[] = {emitUpdateLocation(CGF, Loc), getThreadID(CGF, Loc)}; |
| 6038 | // Ignore return result until untied tasks are supported. |
| 6039 | CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_omp_taskwait), Args); |
Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 6040 | if (auto *Region = dyn_cast_or_null<CGOpenMPRegionInfo>(CGF.CapturedStmtInfo)) |
| 6041 | Region->emitUntiedSwitch(CGF); |
Alexey Bataev | 8b8e202 | 2015-04-27 05:22:09 +0000 | [diff] [blame] | 6042 | } |
| 6043 | |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 6044 | void CGOpenMPRuntime::emitInlinedDirective(CodeGenFunction &CGF, |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 6045 | OpenMPDirectiveKind InnerKind, |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 6046 | const RegionCodeGenTy &CodeGen, |
| 6047 | bool HasCancel) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 6048 | if (!CGF.HaveInsertPoint()) |
| 6049 | return; |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 6050 | InlinedOpenMPRegionRAII Region(CGF, CodeGen, InnerKind, HasCancel); |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 6051 | CGF.CapturedStmtInfo->EmitBody(CGF, /*S=*/nullptr); |
Alexey Bataev | 8cbe0a6 | 2015-02-26 10:27:34 +0000 | [diff] [blame] | 6052 | } |
| 6053 | |
Alexey Bataev | 7d5d33e | 2015-07-06 05:50:32 +0000 | [diff] [blame] | 6054 | namespace { |
| 6055 | enum RTCancelKind { |
| 6056 | CancelNoreq = 0, |
| 6057 | CancelParallel = 1, |
| 6058 | CancelLoop = 2, |
| 6059 | CancelSections = 3, |
| 6060 | CancelTaskgroup = 4 |
| 6061 | }; |
Eugene Zelenko | 0a4f3f4 | 2016-02-10 19:11:58 +0000 | [diff] [blame] | 6062 | } // anonymous namespace |
Alexey Bataev | 7d5d33e | 2015-07-06 05:50:32 +0000 | [diff] [blame] | 6063 | |
| 6064 | static RTCancelKind getCancellationKind(OpenMPDirectiveKind CancelRegion) { |
| 6065 | RTCancelKind CancelKind = CancelNoreq; |
Alexey Bataev | 0f34da1 | 2015-07-02 04:17:07 +0000 | [diff] [blame] | 6066 | if (CancelRegion == OMPD_parallel) |
| 6067 | CancelKind = CancelParallel; |
| 6068 | else if (CancelRegion == OMPD_for) |
| 6069 | CancelKind = CancelLoop; |
| 6070 | else if (CancelRegion == OMPD_sections) |
| 6071 | CancelKind = CancelSections; |
| 6072 | else { |
| 6073 | assert(CancelRegion == OMPD_taskgroup); |
| 6074 | CancelKind = CancelTaskgroup; |
| 6075 | } |
Alexey Bataev | 7d5d33e | 2015-07-06 05:50:32 +0000 | [diff] [blame] | 6076 | return CancelKind; |
| 6077 | } |
| 6078 | |
| 6079 | void CGOpenMPRuntime::emitCancellationPointCall( |
| 6080 | CodeGenFunction &CGF, SourceLocation Loc, |
| 6081 | OpenMPDirectiveKind CancelRegion) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 6082 | if (!CGF.HaveInsertPoint()) |
| 6083 | return; |
Alexey Bataev | 7d5d33e | 2015-07-06 05:50:32 +0000 | [diff] [blame] | 6084 | // Build call kmp_int32 __kmpc_cancellationpoint(ident_t *loc, kmp_int32 |
| 6085 | // global_tid, kmp_int32 cncl_kind); |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 6086 | if (auto *OMPRegionInfo = |
| 6087 | dyn_cast_or_null<CGOpenMPRegionInfo>(CGF.CapturedStmtInfo)) { |
Jonas Hahnfeld | b07931f | 2017-02-17 18:32:58 +0000 | [diff] [blame] | 6088 | // For 'cancellation point taskgroup', the task region info may not have a |
| 6089 | // cancel. This may instead happen in another adjacent task. |
| 6090 | if (CancelRegion == OMPD_taskgroup || OMPRegionInfo->hasCancel()) { |
Alexey Bataev | 7d5d33e | 2015-07-06 05:50:32 +0000 | [diff] [blame] | 6091 | llvm::Value *Args[] = { |
| 6092 | emitUpdateLocation(CGF, Loc), getThreadID(CGF, Loc), |
| 6093 | CGF.Builder.getInt32(getCancellationKind(CancelRegion))}; |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 6094 | // Ignore return result until untied tasks are supported. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 6095 | llvm::Value *Result = CGF.EmitRuntimeCall( |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 6096 | createRuntimeFunction(OMPRTL__kmpc_cancellationpoint), Args); |
| 6097 | // if (__kmpc_cancellationpoint()) { |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 6098 | // exit from construct; |
| 6099 | // } |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 6100 | llvm::BasicBlock *ExitBB = CGF.createBasicBlock(".cancel.exit"); |
| 6101 | llvm::BasicBlock *ContBB = CGF.createBasicBlock(".cancel.continue"); |
| 6102 | llvm::Value *Cmp = CGF.Builder.CreateIsNotNull(Result); |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 6103 | CGF.Builder.CreateCondBr(Cmp, ExitBB, ContBB); |
| 6104 | CGF.EmitBlock(ExitBB); |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 6105 | // exit from construct; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 6106 | CodeGenFunction::JumpDest CancelDest = |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 6107 | CGF.getOMPCancelDestination(OMPRegionInfo->getDirectiveKind()); |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 6108 | CGF.EmitBranchThroughCleanup(CancelDest); |
| 6109 | CGF.EmitBlock(ContBB, /*IsFinished=*/true); |
| 6110 | } |
Alexey Bataev | 0f34da1 | 2015-07-02 04:17:07 +0000 | [diff] [blame] | 6111 | } |
Alexey Bataev | 0f34da1 | 2015-07-02 04:17:07 +0000 | [diff] [blame] | 6112 | } |
| 6113 | |
Alexey Bataev | 7d5d33e | 2015-07-06 05:50:32 +0000 | [diff] [blame] | 6114 | void CGOpenMPRuntime::emitCancelCall(CodeGenFunction &CGF, SourceLocation Loc, |
Alexey Bataev | 87933c7 | 2015-09-18 08:07:34 +0000 | [diff] [blame] | 6115 | const Expr *IfCond, |
Alexey Bataev | 7d5d33e | 2015-07-06 05:50:32 +0000 | [diff] [blame] | 6116 | OpenMPDirectiveKind CancelRegion) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 6117 | if (!CGF.HaveInsertPoint()) |
| 6118 | return; |
Alexey Bataev | 7d5d33e | 2015-07-06 05:50:32 +0000 | [diff] [blame] | 6119 | // Build call kmp_int32 __kmpc_cancel(ident_t *loc, kmp_int32 global_tid, |
| 6120 | // kmp_int32 cncl_kind); |
| 6121 | if (auto *OMPRegionInfo = |
| 6122 | dyn_cast_or_null<CGOpenMPRegionInfo>(CGF.CapturedStmtInfo)) { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 6123 | auto &&ThenGen = [Loc, CancelRegion, OMPRegionInfo](CodeGenFunction &CGF, |
| 6124 | PrePostActionTy &) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 6125 | CGOpenMPRuntime &RT = CGF.CGM.getOpenMPRuntime(); |
Alexey Bataev | 87933c7 | 2015-09-18 08:07:34 +0000 | [diff] [blame] | 6126 | llvm::Value *Args[] = { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 6127 | RT.emitUpdateLocation(CGF, Loc), RT.getThreadID(CGF, Loc), |
Alexey Bataev | 87933c7 | 2015-09-18 08:07:34 +0000 | [diff] [blame] | 6128 | CGF.Builder.getInt32(getCancellationKind(CancelRegion))}; |
| 6129 | // Ignore return result until untied tasks are supported. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 6130 | llvm::Value *Result = CGF.EmitRuntimeCall( |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 6131 | RT.createRuntimeFunction(OMPRTL__kmpc_cancel), Args); |
Alexey Bataev | 87933c7 | 2015-09-18 08:07:34 +0000 | [diff] [blame] | 6132 | // if (__kmpc_cancel()) { |
Alexey Bataev | 87933c7 | 2015-09-18 08:07:34 +0000 | [diff] [blame] | 6133 | // exit from construct; |
| 6134 | // } |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 6135 | llvm::BasicBlock *ExitBB = CGF.createBasicBlock(".cancel.exit"); |
| 6136 | llvm::BasicBlock *ContBB = CGF.createBasicBlock(".cancel.continue"); |
| 6137 | llvm::Value *Cmp = CGF.Builder.CreateIsNotNull(Result); |
Alexey Bataev | 87933c7 | 2015-09-18 08:07:34 +0000 | [diff] [blame] | 6138 | CGF.Builder.CreateCondBr(Cmp, ExitBB, ContBB); |
| 6139 | CGF.EmitBlock(ExitBB); |
Alexey Bataev | 87933c7 | 2015-09-18 08:07:34 +0000 | [diff] [blame] | 6140 | // exit from construct; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 6141 | CodeGenFunction::JumpDest CancelDest = |
Alexey Bataev | 87933c7 | 2015-09-18 08:07:34 +0000 | [diff] [blame] | 6142 | CGF.getOMPCancelDestination(OMPRegionInfo->getDirectiveKind()); |
| 6143 | CGF.EmitBranchThroughCleanup(CancelDest); |
| 6144 | CGF.EmitBlock(ContBB, /*IsFinished=*/true); |
| 6145 | }; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 6146 | if (IfCond) { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 6147 | emitOMPIfClause(CGF, IfCond, ThenGen, |
| 6148 | [](CodeGenFunction &, PrePostActionTy &) {}); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 6149 | } else { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 6150 | RegionCodeGenTy ThenRCG(ThenGen); |
| 6151 | ThenRCG(CGF); |
| 6152 | } |
Alexey Bataev | 7d5d33e | 2015-07-06 05:50:32 +0000 | [diff] [blame] | 6153 | } |
| 6154 | } |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 6155 | |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 6156 | void CGOpenMPRuntime::emitTargetOutlinedFunction( |
| 6157 | const OMPExecutableDirective &D, StringRef ParentName, |
| 6158 | llvm::Function *&OutlinedFn, llvm::Constant *&OutlinedFnID, |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 6159 | bool IsOffloadEntry, const RegionCodeGenTy &CodeGen) { |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 6160 | assert(!ParentName.empty() && "Invalid target region parent name!"); |
Arpith Chacko Jacob | 5c309e4 | 2016-03-22 01:48:56 +0000 | [diff] [blame] | 6161 | emitTargetOutlinedFunctionHelper(D, ParentName, OutlinedFn, OutlinedFnID, |
| 6162 | IsOffloadEntry, CodeGen); |
| 6163 | } |
| 6164 | |
| 6165 | void CGOpenMPRuntime::emitTargetOutlinedFunctionHelper( |
| 6166 | const OMPExecutableDirective &D, StringRef ParentName, |
| 6167 | llvm::Function *&OutlinedFn, llvm::Constant *&OutlinedFnID, |
| 6168 | bool IsOffloadEntry, const RegionCodeGenTy &CodeGen) { |
Samuel Antao | 2de62b0 | 2016-02-13 23:35:10 +0000 | [diff] [blame] | 6169 | // Create a unique name for the entry function using the source location |
| 6170 | // information of the current target region. The name will be something like: |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 6171 | // |
Samuel Antao | 2de62b0 | 2016-02-13 23:35:10 +0000 | [diff] [blame] | 6172 | // __omp_offloading_DD_FFFF_PP_lBB |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 6173 | // |
| 6174 | // 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] | 6175 | // mangled name of the function that encloses the target region and BB is the |
| 6176 | // line number of the target region. |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 6177 | |
| 6178 | unsigned DeviceID; |
| 6179 | unsigned FileID; |
| 6180 | unsigned Line; |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 6181 | getTargetEntryUniqueInfo(CGM.getContext(), D.getLocStart(), DeviceID, FileID, |
Samuel Antao | 2de62b0 | 2016-02-13 23:35:10 +0000 | [diff] [blame] | 6182 | Line); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 6183 | SmallString<64> EntryFnName; |
| 6184 | { |
| 6185 | llvm::raw_svector_ostream OS(EntryFnName); |
Samuel Antao | 2de62b0 | 2016-02-13 23:35:10 +0000 | [diff] [blame] | 6186 | OS << "__omp_offloading" << llvm::format("_%x", DeviceID) |
| 6187 | << llvm::format("_%x_", FileID) << ParentName << "_l" << Line; |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 6188 | } |
| 6189 | |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 6190 | const CapturedStmt &CS = *D.getCapturedStmt(OMPD_target); |
Arpith Chacko Jacob | 5c309e4 | 2016-03-22 01:48:56 +0000 | [diff] [blame] | 6191 | |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 6192 | CodeGenFunction CGF(CGM, true); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 6193 | CGOpenMPTargetRegionInfo CGInfo(CS, CodeGen, EntryFnName); |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 6194 | CodeGenFunction::CGCapturedStmtRAII CapInfoRAII(CGF, &CGInfo); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 6195 | |
Samuel Antao | 6d00426 | 2016-06-16 18:39:34 +0000 | [diff] [blame] | 6196 | OutlinedFn = CGF.GenerateOpenMPCapturedStmtFunction(CS); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 6197 | |
| 6198 | // If this target outline function is not an offload entry, we don't need to |
| 6199 | // register it. |
| 6200 | if (!IsOffloadEntry) |
| 6201 | return; |
| 6202 | |
| 6203 | // The target region ID is used by the runtime library to identify the current |
| 6204 | // target region, so it only has to be unique and not necessarily point to |
| 6205 | // anything. It could be the pointer to the outlined function that implements |
| 6206 | // the target region, but we aren't using that so that the compiler doesn't |
| 6207 | // need to keep that, and could therefore inline the host function if proven |
| 6208 | // worthwhile during optimization. In the other hand, if emitting code for the |
| 6209 | // device, the ID has to be the function address so that it can retrieved from |
| 6210 | // the offloading entry and launched by the runtime library. We also mark the |
| 6211 | // outlined function to have external linkage in case we are emitting code for |
| 6212 | // the device, because these functions will be entry points to the device. |
| 6213 | |
| 6214 | if (CGM.getLangOpts().OpenMPIsDevice) { |
| 6215 | OutlinedFnID = llvm::ConstantExpr::getBitCast(OutlinedFn, CGM.Int8PtrTy); |
| 6216 | OutlinedFn->setLinkage(llvm::GlobalValue::ExternalLinkage); |
Rafael Espindola | cbca487 | 2018-01-11 22:15:12 +0000 | [diff] [blame] | 6217 | OutlinedFn->setDSOLocal(false); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 6218 | } else { |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 6219 | OutlinedFnID = new llvm::GlobalVariable( |
| 6220 | CGM.getModule(), CGM.Int8Ty, /*isConstant=*/true, |
| 6221 | llvm::GlobalValue::PrivateLinkage, |
| 6222 | llvm::Constant::getNullValue(CGM.Int8Ty), ".omp_offload.region_id"); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 6223 | } |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 6224 | |
| 6225 | // Register the information for the entry associated with this target region. |
| 6226 | OffloadEntriesInfoManager.registerTargetRegionEntryInfo( |
Samuel Antao | f83efdb | 2017-01-05 16:02:49 +0000 | [diff] [blame] | 6227 | DeviceID, FileID, ParentName, Line, OutlinedFn, OutlinedFnID, |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 6228 | OffloadEntriesInfoManagerTy::OMPTargetRegionEntryTargetRegion); |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 6229 | } |
| 6230 | |
Carlo Bertolli | 6eee906 | 2016-04-29 01:37:30 +0000 | [diff] [blame] | 6231 | /// discard all CompoundStmts intervening between two constructs |
| 6232 | static const Stmt *ignoreCompoundStmts(const Stmt *Body) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 6233 | while (const auto *CS = dyn_cast_or_null<CompoundStmt>(Body)) |
Carlo Bertolli | 6eee906 | 2016-04-29 01:37:30 +0000 | [diff] [blame] | 6234 | Body = CS->body_front(); |
| 6235 | |
| 6236 | return Body; |
| 6237 | } |
| 6238 | |
Arpith Chacko Jacob | 33c849a | 2017-01-25 00:57:16 +0000 | [diff] [blame] | 6239 | /// Emit the number of teams for a target directive. Inspect the num_teams |
| 6240 | /// clause associated with a teams construct combined or closely nested |
| 6241 | /// with the target directive. |
| 6242 | /// |
| 6243 | /// Emit a team of size one for directives such as 'target parallel' that |
| 6244 | /// have no associated teams construct. |
| 6245 | /// |
| 6246 | /// Otherwise, return nullptr. |
Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 6247 | static llvm::Value * |
Arpith Chacko Jacob | 33c849a | 2017-01-25 00:57:16 +0000 | [diff] [blame] | 6248 | emitNumTeamsForTargetDirective(CGOpenMPRuntime &OMPRuntime, |
| 6249 | CodeGenFunction &CGF, |
| 6250 | const OMPExecutableDirective &D) { |
Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 6251 | assert(!CGF.getLangOpts().OpenMPIsDevice && "Clauses associated with the " |
| 6252 | "teams directive expected to be " |
| 6253 | "emitted only for the host!"); |
| 6254 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 6255 | CGBuilderTy &Bld = CGF.Builder; |
Arpith Chacko Jacob | 99a1e0e | 2017-01-25 02:18:43 +0000 | [diff] [blame] | 6256 | |
| 6257 | // If the target directive is combined with a teams directive: |
| 6258 | // Return the value in the num_teams clause, if any. |
| 6259 | // Otherwise, return 0 to denote the runtime default. |
| 6260 | if (isOpenMPTeamsDirective(D.getDirectiveKind())) { |
| 6261 | if (const auto *NumTeamsClause = D.getSingleClause<OMPNumTeamsClause>()) { |
| 6262 | CodeGenFunction::RunCleanupsScope NumTeamsScope(CGF); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 6263 | llvm::Value *NumTeams = CGF.EmitScalarExpr(NumTeamsClause->getNumTeams(), |
| 6264 | /*IgnoreResultAssign*/ true); |
Arpith Chacko Jacob | 99a1e0e | 2017-01-25 02:18:43 +0000 | [diff] [blame] | 6265 | return Bld.CreateIntCast(NumTeams, CGF.Int32Ty, |
| 6266 | /*IsSigned=*/true); |
| 6267 | } |
| 6268 | |
| 6269 | // The default value is 0. |
| 6270 | return Bld.getInt32(0); |
| 6271 | } |
| 6272 | |
Arpith Chacko Jacob | 33c849a | 2017-01-25 00:57:16 +0000 | [diff] [blame] | 6273 | // If the target directive is combined with a parallel directive but not a |
| 6274 | // teams directive, start one team. |
Arpith Chacko Jacob | 99a1e0e | 2017-01-25 02:18:43 +0000 | [diff] [blame] | 6275 | if (isOpenMPParallelDirective(D.getDirectiveKind())) |
| 6276 | return Bld.getInt32(1); |
Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 6277 | |
| 6278 | // If the current target region has a teams region enclosed, we need to get |
| 6279 | // the number of teams to pass to the runtime function call. This is done |
| 6280 | // by generating the expression in a inlined region. This is required because |
| 6281 | // the expression is captured in the enclosing target environment when the |
| 6282 | // teams directive is not combined with target. |
| 6283 | |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 6284 | const CapturedStmt &CS = *D.getCapturedStmt(OMPD_target); |
Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 6285 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 6286 | if (const auto *TeamsDir = dyn_cast_or_null<OMPExecutableDirective>( |
Carlo Bertolli | 6eee906 | 2016-04-29 01:37:30 +0000 | [diff] [blame] | 6287 | ignoreCompoundStmts(CS.getCapturedStmt()))) { |
Alexey Bataev | 50a1c78 | 2017-12-01 21:31:08 +0000 | [diff] [blame] | 6288 | if (isOpenMPTeamsDirective(TeamsDir->getDirectiveKind())) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 6289 | if (const auto *NTE = TeamsDir->getSingleClause<OMPNumTeamsClause>()) { |
Alexey Bataev | 50a1c78 | 2017-12-01 21:31:08 +0000 | [diff] [blame] | 6290 | CGOpenMPInnerExprInfo CGInfo(CGF, CS); |
| 6291 | CodeGenFunction::CGCapturedStmtRAII CapInfoRAII(CGF, &CGInfo); |
| 6292 | llvm::Value *NumTeams = CGF.EmitScalarExpr(NTE->getNumTeams()); |
| 6293 | return Bld.CreateIntCast(NumTeams, CGF.Int32Ty, |
| 6294 | /*IsSigned=*/true); |
| 6295 | } |
Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 6296 | |
Alexey Bataev | 50a1c78 | 2017-12-01 21:31:08 +0000 | [diff] [blame] | 6297 | // If we have an enclosed teams directive but no num_teams clause we use |
| 6298 | // the default value 0. |
| 6299 | return Bld.getInt32(0); |
| 6300 | } |
Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 6301 | } |
| 6302 | |
| 6303 | // No teams associated with the directive. |
| 6304 | return nullptr; |
| 6305 | } |
| 6306 | |
Arpith Chacko Jacob | 33c849a | 2017-01-25 00:57:16 +0000 | [diff] [blame] | 6307 | /// Emit the number of threads for a target directive. Inspect the |
| 6308 | /// thread_limit clause associated with a teams construct combined or closely |
| 6309 | /// nested with the target directive. |
| 6310 | /// |
| 6311 | /// Emit the num_threads clause for directives such as 'target parallel' that |
| 6312 | /// have no associated teams construct. |
| 6313 | /// |
| 6314 | /// Otherwise, return nullptr. |
Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 6315 | static llvm::Value * |
Arpith Chacko Jacob | 33c849a | 2017-01-25 00:57:16 +0000 | [diff] [blame] | 6316 | emitNumThreadsForTargetDirective(CGOpenMPRuntime &OMPRuntime, |
| 6317 | CodeGenFunction &CGF, |
| 6318 | const OMPExecutableDirective &D) { |
Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 6319 | assert(!CGF.getLangOpts().OpenMPIsDevice && "Clauses associated with the " |
| 6320 | "teams directive expected to be " |
| 6321 | "emitted only for the host!"); |
| 6322 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 6323 | CGBuilderTy &Bld = CGF.Builder; |
Arpith Chacko Jacob | 33c849a | 2017-01-25 00:57:16 +0000 | [diff] [blame] | 6324 | |
| 6325 | // |
| 6326 | // If the target directive is combined with a teams directive: |
| 6327 | // Return the value in the thread_limit clause, if any. |
| 6328 | // |
| 6329 | // If the target directive is combined with a parallel directive: |
| 6330 | // Return the value in the num_threads clause, if any. |
| 6331 | // |
| 6332 | // If both clauses are set, select the minimum of the two. |
| 6333 | // |
| 6334 | // If neither teams or parallel combined directives set the number of threads |
| 6335 | // in a team, return 0 to denote the runtime default. |
| 6336 | // |
| 6337 | // If this is not a teams directive return nullptr. |
| 6338 | |
Arpith Chacko Jacob | 99a1e0e | 2017-01-25 02:18:43 +0000 | [diff] [blame] | 6339 | if (isOpenMPTeamsDirective(D.getDirectiveKind()) || |
| 6340 | isOpenMPParallelDirective(D.getDirectiveKind())) { |
Arpith Chacko Jacob | 33c849a | 2017-01-25 00:57:16 +0000 | [diff] [blame] | 6341 | llvm::Value *DefaultThreadLimitVal = Bld.getInt32(0); |
| 6342 | llvm::Value *NumThreadsVal = nullptr; |
Arpith Chacko Jacob | 99a1e0e | 2017-01-25 02:18:43 +0000 | [diff] [blame] | 6343 | llvm::Value *ThreadLimitVal = nullptr; |
| 6344 | |
| 6345 | if (const auto *ThreadLimitClause = |
| 6346 | D.getSingleClause<OMPThreadLimitClause>()) { |
| 6347 | CodeGenFunction::RunCleanupsScope ThreadLimitScope(CGF); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 6348 | llvm::Value *ThreadLimit = |
| 6349 | CGF.EmitScalarExpr(ThreadLimitClause->getThreadLimit(), |
| 6350 | /*IgnoreResultAssign*/ true); |
Arpith Chacko Jacob | 99a1e0e | 2017-01-25 02:18:43 +0000 | [diff] [blame] | 6351 | ThreadLimitVal = Bld.CreateIntCast(ThreadLimit, CGF.Int32Ty, |
| 6352 | /*IsSigned=*/true); |
| 6353 | } |
Arpith Chacko Jacob | 33c849a | 2017-01-25 00:57:16 +0000 | [diff] [blame] | 6354 | |
| 6355 | if (const auto *NumThreadsClause = |
| 6356 | D.getSingleClause<OMPNumThreadsClause>()) { |
| 6357 | CodeGenFunction::RunCleanupsScope NumThreadsScope(CGF); |
| 6358 | llvm::Value *NumThreads = |
| 6359 | CGF.EmitScalarExpr(NumThreadsClause->getNumThreads(), |
| 6360 | /*IgnoreResultAssign*/ true); |
| 6361 | NumThreadsVal = |
| 6362 | Bld.CreateIntCast(NumThreads, CGF.Int32Ty, /*IsSigned=*/true); |
| 6363 | } |
| 6364 | |
Arpith Chacko Jacob | 99a1e0e | 2017-01-25 02:18:43 +0000 | [diff] [blame] | 6365 | // Select the lesser of thread_limit and num_threads. |
| 6366 | if (NumThreadsVal) |
| 6367 | ThreadLimitVal = ThreadLimitVal |
| 6368 | ? Bld.CreateSelect(Bld.CreateICmpSLT(NumThreadsVal, |
| 6369 | ThreadLimitVal), |
| 6370 | NumThreadsVal, ThreadLimitVal) |
| 6371 | : NumThreadsVal; |
Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 6372 | |
Arpith Chacko Jacob | 99a1e0e | 2017-01-25 02:18:43 +0000 | [diff] [blame] | 6373 | // Set default value passed to the runtime if either teams or a target |
| 6374 | // parallel type directive is found but no clause is specified. |
| 6375 | if (!ThreadLimitVal) |
| 6376 | ThreadLimitVal = DefaultThreadLimitVal; |
| 6377 | |
| 6378 | return ThreadLimitVal; |
| 6379 | } |
Arpith Chacko Jacob | 86f9e46 | 2017-01-25 01:45:59 +0000 | [diff] [blame] | 6380 | |
Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 6381 | // If the current target region has a teams region enclosed, we need to get |
| 6382 | // the thread limit to pass to the runtime function call. This is done |
| 6383 | // by generating the expression in a inlined region. This is required because |
| 6384 | // the expression is captured in the enclosing target environment when the |
| 6385 | // teams directive is not combined with target. |
| 6386 | |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 6387 | const CapturedStmt &CS = *D.getCapturedStmt(OMPD_target); |
Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 6388 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 6389 | if (const auto *TeamsDir = dyn_cast_or_null<OMPExecutableDirective>( |
Carlo Bertolli | 6eee906 | 2016-04-29 01:37:30 +0000 | [diff] [blame] | 6390 | ignoreCompoundStmts(CS.getCapturedStmt()))) { |
Alexey Bataev | 50a1c78 | 2017-12-01 21:31:08 +0000 | [diff] [blame] | 6391 | if (isOpenMPTeamsDirective(TeamsDir->getDirectiveKind())) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 6392 | if (const auto *TLE = TeamsDir->getSingleClause<OMPThreadLimitClause>()) { |
Alexey Bataev | 50a1c78 | 2017-12-01 21:31:08 +0000 | [diff] [blame] | 6393 | CGOpenMPInnerExprInfo CGInfo(CGF, CS); |
| 6394 | CodeGenFunction::CGCapturedStmtRAII CapInfoRAII(CGF, &CGInfo); |
| 6395 | llvm::Value *ThreadLimit = CGF.EmitScalarExpr(TLE->getThreadLimit()); |
| 6396 | return CGF.Builder.CreateIntCast(ThreadLimit, CGF.Int32Ty, |
| 6397 | /*IsSigned=*/true); |
| 6398 | } |
Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 6399 | |
Alexey Bataev | 50a1c78 | 2017-12-01 21:31:08 +0000 | [diff] [blame] | 6400 | // If we have an enclosed teams directive but no thread_limit clause we |
| 6401 | // use the default value 0. |
| 6402 | return CGF.Builder.getInt32(0); |
| 6403 | } |
Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 6404 | } |
| 6405 | |
| 6406 | // No teams associated with the directive. |
| 6407 | return nullptr; |
| 6408 | } |
| 6409 | |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6410 | namespace { |
| 6411 | // \brief Utility to handle information from clauses associated with a given |
| 6412 | // construct that use mappable expressions (e.g. 'map' clause, 'to' clause). |
| 6413 | // It provides a convenient interface to obtain the information and generate |
| 6414 | // code for that information. |
| 6415 | class MappableExprsHandler { |
| 6416 | public: |
| 6417 | /// \brief Values for bit flags used to specify the mapping type for |
| 6418 | /// offloading. |
| 6419 | enum OpenMPOffloadMappingFlags { |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6420 | /// \brief Allocate memory on the device and move data from host to device. |
| 6421 | OMP_MAP_TO = 0x01, |
| 6422 | /// \brief Allocate memory on the device and move data from device to host. |
| 6423 | OMP_MAP_FROM = 0x02, |
| 6424 | /// \brief Always perform the requested mapping action on the element, even |
| 6425 | /// if it was already mapped before. |
| 6426 | OMP_MAP_ALWAYS = 0x04, |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6427 | /// \brief Delete the element from the device environment, ignoring the |
| 6428 | /// current reference count associated with the element. |
Samuel Antao | 6782e94 | 2016-05-26 16:48:10 +0000 | [diff] [blame] | 6429 | OMP_MAP_DELETE = 0x08, |
George Rokos | 065755d | 2017-11-07 18:27:04 +0000 | [diff] [blame] | 6430 | /// \brief The element being mapped is a pointer-pointee pair; both the |
| 6431 | /// pointer and the pointee should be mapped. |
| 6432 | OMP_MAP_PTR_AND_OBJ = 0x10, |
| 6433 | /// \brief This flags signals that the base address of an entry should be |
| 6434 | /// passed to the target kernel as an argument. |
| 6435 | OMP_MAP_TARGET_PARAM = 0x20, |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 6436 | /// \brief Signal that the runtime library has to return the device pointer |
George Rokos | 065755d | 2017-11-07 18:27:04 +0000 | [diff] [blame] | 6437 | /// in the current position for the data being mapped. Used when we have the |
| 6438 | /// use_device_ptr clause. |
| 6439 | OMP_MAP_RETURN_PARAM = 0x40, |
Samuel Antao | d486f84 | 2016-05-26 16:53:38 +0000 | [diff] [blame] | 6440 | /// \brief This flag signals that the reference being passed is a pointer to |
| 6441 | /// private data. |
George Rokos | 065755d | 2017-11-07 18:27:04 +0000 | [diff] [blame] | 6442 | OMP_MAP_PRIVATE = 0x80, |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6443 | /// \brief Pass the element to the device by value. |
George Rokos | 065755d | 2017-11-07 18:27:04 +0000 | [diff] [blame] | 6444 | OMP_MAP_LITERAL = 0x100, |
Alexey Bataev | f47c4b4 | 2017-09-26 13:47:31 +0000 | [diff] [blame] | 6445 | /// Implicit map |
| 6446 | OMP_MAP_IMPLICIT = 0x200, |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6447 | }; |
| 6448 | |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 6449 | /// Class that associates information with a base pointer to be passed to the |
| 6450 | /// runtime library. |
| 6451 | class BasePointerInfo { |
| 6452 | /// The base pointer. |
| 6453 | llvm::Value *Ptr = nullptr; |
| 6454 | /// The base declaration that refers to this device pointer, or null if |
| 6455 | /// there is none. |
| 6456 | const ValueDecl *DevPtrDecl = nullptr; |
| 6457 | |
| 6458 | public: |
| 6459 | BasePointerInfo(llvm::Value *Ptr, const ValueDecl *DevPtrDecl = nullptr) |
| 6460 | : Ptr(Ptr), DevPtrDecl(DevPtrDecl) {} |
| 6461 | llvm::Value *operator*() const { return Ptr; } |
| 6462 | const ValueDecl *getDevicePtrDecl() const { return DevPtrDecl; } |
| 6463 | void setDevicePtrDecl(const ValueDecl *D) { DevPtrDecl = D; } |
| 6464 | }; |
| 6465 | |
| 6466 | typedef SmallVector<BasePointerInfo, 16> MapBaseValuesArrayTy; |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6467 | typedef SmallVector<llvm::Value *, 16> MapValuesArrayTy; |
George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 6468 | typedef SmallVector<uint64_t, 16> MapFlagsArrayTy; |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6469 | |
| 6470 | private: |
| 6471 | /// \brief Directive from where the map clauses were extracted. |
Samuel Antao | 44bcdb3 | 2016-07-28 15:31:29 +0000 | [diff] [blame] | 6472 | const OMPExecutableDirective &CurDir; |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6473 | |
| 6474 | /// \brief Function the directive is being generated for. |
| 6475 | CodeGenFunction &CGF; |
| 6476 | |
Samuel Antao | d486f84 | 2016-05-26 16:53:38 +0000 | [diff] [blame] | 6477 | /// \brief Set of all first private variables in the current directive. |
| 6478 | llvm::SmallPtrSet<const VarDecl *, 8> FirstPrivateDecls; |
Alexey Bataev | 3f96fe6 | 2017-12-13 17:31:39 +0000 | [diff] [blame] | 6479 | /// Set of all reduction variables in the current directive. |
| 6480 | llvm::SmallPtrSet<const VarDecl *, 8> ReductionDecls; |
Samuel Antao | d486f84 | 2016-05-26 16:53:38 +0000 | [diff] [blame] | 6481 | |
Samuel Antao | 6890b09 | 2016-07-28 14:25:09 +0000 | [diff] [blame] | 6482 | /// Map between device pointer declarations and their expression components. |
| 6483 | /// The key value for declarations in 'this' is null. |
| 6484 | llvm::DenseMap< |
| 6485 | const ValueDecl *, |
| 6486 | SmallVector<OMPClauseMappableExprCommon::MappableExprComponentListRef, 4>> |
| 6487 | DevPointersMap; |
| 6488 | |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6489 | llvm::Value *getExprTypeSize(const Expr *E) const { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 6490 | QualType ExprTy = E->getType().getCanonicalType(); |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6491 | |
| 6492 | // Reference types are ignored for mapping purposes. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 6493 | if (const auto *RefTy = ExprTy->getAs<ReferenceType>()) |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6494 | ExprTy = RefTy->getPointeeType().getCanonicalType(); |
| 6495 | |
| 6496 | // Given that an array section is considered a built-in type, we need to |
| 6497 | // do the calculation based on the length of the section instead of relying |
| 6498 | // on CGF.getTypeSize(E->getType()). |
| 6499 | if (const auto *OAE = dyn_cast<OMPArraySectionExpr>(E)) { |
| 6500 | QualType BaseTy = OMPArraySectionExpr::getBaseOriginalType( |
| 6501 | OAE->getBase()->IgnoreParenImpCasts()) |
| 6502 | .getCanonicalType(); |
| 6503 | |
| 6504 | // If there is no length associated with the expression, that means we |
| 6505 | // are using the whole length of the base. |
| 6506 | if (!OAE->getLength() && OAE->getColonLoc().isValid()) |
| 6507 | return CGF.getTypeSize(BaseTy); |
| 6508 | |
| 6509 | llvm::Value *ElemSize; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 6510 | if (const auto *PTy = BaseTy->getAs<PointerType>()) { |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6511 | ElemSize = CGF.getTypeSize(PTy->getPointeeType().getCanonicalType()); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 6512 | } else { |
| 6513 | const auto *ATy = cast<ArrayType>(BaseTy.getTypePtr()); |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6514 | assert(ATy && "Expecting array type if not a pointer type."); |
| 6515 | ElemSize = CGF.getTypeSize(ATy->getElementType().getCanonicalType()); |
| 6516 | } |
| 6517 | |
| 6518 | // If we don't have a length at this point, that is because we have an |
| 6519 | // array section with a single element. |
| 6520 | if (!OAE->getLength()) |
| 6521 | return ElemSize; |
| 6522 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 6523 | llvm::Value *LengthVal = CGF.EmitScalarExpr(OAE->getLength()); |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6524 | LengthVal = |
| 6525 | CGF.Builder.CreateIntCast(LengthVal, CGF.SizeTy, /*isSigned=*/false); |
| 6526 | return CGF.Builder.CreateNUWMul(LengthVal, ElemSize); |
| 6527 | } |
| 6528 | return CGF.getTypeSize(ExprTy); |
| 6529 | } |
| 6530 | |
| 6531 | /// \brief Return the corresponding bits for a given map clause modifier. Add |
| 6532 | /// 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] | 6533 | /// map as the first one of a series of maps that relate to the same map |
| 6534 | /// expression. |
George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 6535 | uint64_t getMapTypeBits(OpenMPMapClauseKind MapType, |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6536 | OpenMPMapClauseKind MapTypeModifier, bool AddPtrFlag, |
George Rokos | 065755d | 2017-11-07 18:27:04 +0000 | [diff] [blame] | 6537 | bool AddIsTargetParamFlag) const { |
George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 6538 | uint64_t Bits = 0u; |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6539 | switch (MapType) { |
| 6540 | case OMPC_MAP_alloc: |
Samuel Antao | 6782e94 | 2016-05-26 16:48:10 +0000 | [diff] [blame] | 6541 | case OMPC_MAP_release: |
| 6542 | // alloc and release is the default behavior in the runtime library, i.e. |
| 6543 | // if we don't pass any bits alloc/release that is what the runtime is |
| 6544 | // going to do. Therefore, we don't need to signal anything for these two |
| 6545 | // type modifiers. |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6546 | break; |
| 6547 | case OMPC_MAP_to: |
| 6548 | Bits = OMP_MAP_TO; |
| 6549 | break; |
| 6550 | case OMPC_MAP_from: |
| 6551 | Bits = OMP_MAP_FROM; |
| 6552 | break; |
| 6553 | case OMPC_MAP_tofrom: |
| 6554 | Bits = OMP_MAP_TO | OMP_MAP_FROM; |
| 6555 | break; |
| 6556 | case OMPC_MAP_delete: |
| 6557 | Bits = OMP_MAP_DELETE; |
| 6558 | break; |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6559 | default: |
| 6560 | llvm_unreachable("Unexpected map type!"); |
| 6561 | break; |
| 6562 | } |
| 6563 | if (AddPtrFlag) |
George Rokos | 065755d | 2017-11-07 18:27:04 +0000 | [diff] [blame] | 6564 | Bits |= OMP_MAP_PTR_AND_OBJ; |
| 6565 | if (AddIsTargetParamFlag) |
| 6566 | Bits |= OMP_MAP_TARGET_PARAM; |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6567 | if (MapTypeModifier == OMPC_MAP_always) |
| 6568 | Bits |= OMP_MAP_ALWAYS; |
| 6569 | return Bits; |
| 6570 | } |
| 6571 | |
| 6572 | /// \brief Return true if the provided expression is a final array section. A |
| 6573 | /// final array section, is one whose length can't be proved to be one. |
| 6574 | bool isFinalArraySectionExpression(const Expr *E) const { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 6575 | const auto *OASE = dyn_cast<OMPArraySectionExpr>(E); |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6576 | |
| 6577 | // It is not an array section and therefore not a unity-size one. |
| 6578 | if (!OASE) |
| 6579 | return false; |
| 6580 | |
| 6581 | // An array section with no colon always refer to a single element. |
| 6582 | if (OASE->getColonLoc().isInvalid()) |
| 6583 | return false; |
| 6584 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 6585 | const Expr *Length = OASE->getLength(); |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6586 | |
| 6587 | // If we don't have a length we have to check if the array has size 1 |
| 6588 | // for this dimension. Also, we should always expect a length if the |
| 6589 | // base type is pointer. |
| 6590 | if (!Length) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 6591 | QualType BaseQTy = OMPArraySectionExpr::getBaseOriginalType( |
| 6592 | OASE->getBase()->IgnoreParenImpCasts()) |
| 6593 | .getCanonicalType(); |
| 6594 | if (const auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr())) |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6595 | return ATy->getSize().getSExtValue() != 1; |
| 6596 | // If we don't have a constant dimension length, we have to consider |
| 6597 | // the current section as having any size, so it is not necessarily |
| 6598 | // unitary. If it happen to be unity size, that's user fault. |
| 6599 | return true; |
| 6600 | } |
| 6601 | |
| 6602 | // Check if the length evaluates to 1. |
| 6603 | llvm::APSInt ConstLength; |
| 6604 | if (!Length->EvaluateAsInt(ConstLength, CGF.getContext())) |
| 6605 | return true; // Can have more that size 1. |
| 6606 | |
| 6607 | return ConstLength.getSExtValue() != 1; |
| 6608 | } |
| 6609 | |
Alexey Bataev | 92327c5 | 2018-03-26 16:40:55 +0000 | [diff] [blame] | 6610 | /// \brief Return the adjusted map modifiers if the declaration a capture |
| 6611 | /// refers to appears in a first-private clause. This is expected to be used |
| 6612 | /// only with directives that start with 'target'. |
| 6613 | unsigned adjustMapModifiersForPrivateClauses(const CapturedStmt::Capture &Cap, |
| 6614 | unsigned CurrentModifiers) { |
| 6615 | assert(Cap.capturesVariable() && "Expected capture by reference only!"); |
| 6616 | |
| 6617 | // A first private variable captured by reference will use only the |
| 6618 | // 'private ptr' and 'map to' flag. Return the right flags if the captured |
| 6619 | // declaration is known as first-private in this handler. |
| 6620 | if (FirstPrivateDecls.count(Cap.getCapturedVar())) |
| 6621 | return MappableExprsHandler::OMP_MAP_PRIVATE | |
| 6622 | MappableExprsHandler::OMP_MAP_TO; |
| 6623 | // Reduction variable will use only the 'private ptr' and 'map to_from' |
| 6624 | // flag. |
| 6625 | if (ReductionDecls.count(Cap.getCapturedVar())) { |
| 6626 | return MappableExprsHandler::OMP_MAP_TO | |
| 6627 | MappableExprsHandler::OMP_MAP_FROM; |
| 6628 | } |
| 6629 | |
| 6630 | // We didn't modify anything. |
| 6631 | return CurrentModifiers; |
| 6632 | } |
| 6633 | |
| 6634 | public: |
| 6635 | MappableExprsHandler(const OMPExecutableDirective &Dir, CodeGenFunction &CGF) |
| 6636 | : CurDir(Dir), CGF(CGF) { |
| 6637 | // Extract firstprivate clause information. |
| 6638 | for (const auto *C : Dir.getClausesOfKind<OMPFirstprivateClause>()) |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 6639 | for (const Expr *D : C->varlists()) |
Alexey Bataev | 92327c5 | 2018-03-26 16:40:55 +0000 | [diff] [blame] | 6640 | FirstPrivateDecls.insert( |
| 6641 | cast<VarDecl>(cast<DeclRefExpr>(D)->getDecl())->getCanonicalDecl()); |
| 6642 | for (const auto *C : Dir.getClausesOfKind<OMPReductionClause>()) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 6643 | for (const Expr *D : C->varlists()) { |
Alexey Bataev | 92327c5 | 2018-03-26 16:40:55 +0000 | [diff] [blame] | 6644 | ReductionDecls.insert( |
| 6645 | cast<VarDecl>(cast<DeclRefExpr>(D)->getDecl())->getCanonicalDecl()); |
| 6646 | } |
| 6647 | } |
| 6648 | // Extract device pointer clause information. |
| 6649 | for (const auto *C : Dir.getClausesOfKind<OMPIsDevicePtrClause>()) |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 6650 | for (const auto &L : C->component_lists()) |
Alexey Bataev | 92327c5 | 2018-03-26 16:40:55 +0000 | [diff] [blame] | 6651 | DevPointersMap[L.first].push_back(L.second); |
| 6652 | } |
| 6653 | |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6654 | /// \brief Generate the base pointers, section pointers, sizes and map type |
| 6655 | /// bits for the provided map type, map modifier, and expression components. |
| 6656 | /// \a IsFirstComponent should be set to true if the provided set of |
| 6657 | /// components is the first associated with a capture. |
| 6658 | void generateInfoForComponentList( |
| 6659 | OpenMPMapClauseKind MapType, OpenMPMapClauseKind MapTypeModifier, |
| 6660 | OMPClauseMappableExprCommon::MappableExprComponentListRef Components, |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 6661 | MapBaseValuesArrayTy &BasePointers, MapValuesArrayTy &Pointers, |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6662 | MapValuesArrayTy &Sizes, MapFlagsArrayTy &Types, |
Alexey Bataev | f47c4b4 | 2017-09-26 13:47:31 +0000 | [diff] [blame] | 6663 | bool IsFirstComponentList, bool IsImplicit) const { |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6664 | |
| 6665 | // 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] | 6666 | // types below. The generated information is expressed in this order: |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6667 | // base pointer, section pointer, size, flags |
| 6668 | // (to add to the ones that come from the map type and modifier). |
| 6669 | // |
| 6670 | // double d; |
| 6671 | // int i[100]; |
| 6672 | // float *p; |
| 6673 | // |
| 6674 | // struct S1 { |
| 6675 | // int i; |
| 6676 | // float f[50]; |
| 6677 | // } |
| 6678 | // struct S2 { |
| 6679 | // int i; |
| 6680 | // float f[50]; |
| 6681 | // S1 s; |
| 6682 | // double *p; |
| 6683 | // struct S2 *ps; |
| 6684 | // } |
| 6685 | // S2 s; |
| 6686 | // S2 *ps; |
| 6687 | // |
| 6688 | // map(d) |
| 6689 | // &d, &d, sizeof(double), noflags |
| 6690 | // |
| 6691 | // map(i) |
| 6692 | // &i, &i, 100*sizeof(int), noflags |
| 6693 | // |
| 6694 | // map(i[1:23]) |
| 6695 | // &i(=&i[0]), &i[1], 23*sizeof(int), noflags |
| 6696 | // |
| 6697 | // map(p) |
| 6698 | // &p, &p, sizeof(float*), noflags |
| 6699 | // |
| 6700 | // map(p[1:24]) |
| 6701 | // p, &p[1], 24*sizeof(float), noflags |
| 6702 | // |
| 6703 | // map(s) |
| 6704 | // &s, &s, sizeof(S2), noflags |
| 6705 | // |
| 6706 | // map(s.i) |
| 6707 | // &s, &(s.i), sizeof(int), noflags |
| 6708 | // |
| 6709 | // map(s.s.f) |
| 6710 | // &s, &(s.i.f), 50*sizeof(int), noflags |
| 6711 | // |
| 6712 | // map(s.p) |
| 6713 | // &s, &(s.p), sizeof(double*), noflags |
| 6714 | // |
| 6715 | // map(s.p[:22], s.a s.b) |
| 6716 | // &s, &(s.p), sizeof(double*), noflags |
George Rokos | 065755d | 2017-11-07 18:27:04 +0000 | [diff] [blame] | 6717 | // &(s.p), &(s.p[0]), 22*sizeof(double), ptr_flag |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6718 | // |
| 6719 | // map(s.ps) |
| 6720 | // &s, &(s.ps), sizeof(S2*), noflags |
| 6721 | // |
| 6722 | // map(s.ps->s.i) |
| 6723 | // &s, &(s.ps), sizeof(S2*), noflags |
George Rokos | 065755d | 2017-11-07 18:27:04 +0000 | [diff] [blame] | 6724 | // &(s.ps), &(s.ps->s.i), sizeof(int), ptr_flag |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6725 | // |
| 6726 | // map(s.ps->ps) |
| 6727 | // &s, &(s.ps), sizeof(S2*), noflags |
George Rokos | 065755d | 2017-11-07 18:27:04 +0000 | [diff] [blame] | 6728 | // &(s.ps), &(s.ps->ps), sizeof(S2*), ptr_flag |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6729 | // |
| 6730 | // map(s.ps->ps->ps) |
| 6731 | // &s, &(s.ps), sizeof(S2*), noflags |
George Rokos | 065755d | 2017-11-07 18:27:04 +0000 | [diff] [blame] | 6732 | // &(s.ps), &(s.ps->ps), sizeof(S2*), ptr_flag |
| 6733 | // &(s.ps->ps), &(s.ps->ps->ps), sizeof(S2*), ptr_flag |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6734 | // |
| 6735 | // map(s.ps->ps->s.f[:22]) |
| 6736 | // &s, &(s.ps), sizeof(S2*), noflags |
George Rokos | 065755d | 2017-11-07 18:27:04 +0000 | [diff] [blame] | 6737 | // &(s.ps), &(s.ps->ps), sizeof(S2*), ptr_flag |
| 6738 | // &(s.ps->ps), &(s.ps->ps->s.f[0]), 22*sizeof(float), ptr_flag |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6739 | // |
| 6740 | // map(ps) |
| 6741 | // &ps, &ps, sizeof(S2*), noflags |
| 6742 | // |
| 6743 | // map(ps->i) |
| 6744 | // ps, &(ps->i), sizeof(int), noflags |
| 6745 | // |
| 6746 | // map(ps->s.f) |
| 6747 | // ps, &(ps->s.f[0]), 50*sizeof(float), noflags |
| 6748 | // |
| 6749 | // map(ps->p) |
| 6750 | // ps, &(ps->p), sizeof(double*), noflags |
| 6751 | // |
| 6752 | // map(ps->p[:22]) |
| 6753 | // ps, &(ps->p), sizeof(double*), noflags |
George Rokos | 065755d | 2017-11-07 18:27:04 +0000 | [diff] [blame] | 6754 | // &(ps->p), &(ps->p[0]), 22*sizeof(double), ptr_flag |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6755 | // |
| 6756 | // map(ps->ps) |
| 6757 | // ps, &(ps->ps), sizeof(S2*), noflags |
| 6758 | // |
| 6759 | // map(ps->ps->s.i) |
| 6760 | // ps, &(ps->ps), sizeof(S2*), noflags |
George Rokos | 065755d | 2017-11-07 18:27:04 +0000 | [diff] [blame] | 6761 | // &(ps->ps), &(ps->ps->s.i), sizeof(int), ptr_flag |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6762 | // |
| 6763 | // map(ps->ps->ps) |
| 6764 | // ps, &(ps->ps), sizeof(S2*), noflags |
George Rokos | 065755d | 2017-11-07 18:27:04 +0000 | [diff] [blame] | 6765 | // &(ps->ps), &(ps->ps->ps), sizeof(S2*), ptr_flag |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6766 | // |
| 6767 | // map(ps->ps->ps->ps) |
| 6768 | // ps, &(ps->ps), sizeof(S2*), noflags |
George Rokos | 065755d | 2017-11-07 18:27:04 +0000 | [diff] [blame] | 6769 | // &(ps->ps), &(ps->ps->ps), sizeof(S2*), ptr_flag |
| 6770 | // &(ps->ps->ps), &(ps->ps->ps->ps), sizeof(S2*), ptr_flag |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6771 | // |
| 6772 | // map(ps->ps->ps->s.f[:22]) |
| 6773 | // ps, &(ps->ps), sizeof(S2*), noflags |
George Rokos | 065755d | 2017-11-07 18:27:04 +0000 | [diff] [blame] | 6774 | // &(ps->ps), &(ps->ps->ps), sizeof(S2*), ptr_flag |
| 6775 | // &(ps->ps->ps), &(ps->ps->ps->s.f[0]), 22*sizeof(float), ptr_flag |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6776 | |
| 6777 | // Track if the map information being generated is the first for a capture. |
| 6778 | bool IsCaptureFirstInfo = IsFirstComponentList; |
Alexey Bataev | 92327c5 | 2018-03-26 16:40:55 +0000 | [diff] [blame] | 6779 | bool IsLink = false; // Is this variable a "declare target link"? |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6780 | |
| 6781 | // Scan the components from the base to the complete expression. |
| 6782 | auto CI = Components.rbegin(); |
| 6783 | auto CE = Components.rend(); |
| 6784 | auto I = CI; |
| 6785 | |
| 6786 | // Track if the map information being generated is the first for a list of |
| 6787 | // components. |
| 6788 | bool IsExpressionFirstInfo = true; |
| 6789 | llvm::Value *BP = nullptr; |
| 6790 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 6791 | if (const auto *ME = dyn_cast<MemberExpr>(I->getAssociatedExpression())) { |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6792 | // The base is the 'this' pointer. The content of the pointer is going |
| 6793 | // to be the base of the field being mapped. |
| 6794 | BP = CGF.EmitScalarExpr(ME->getBase()); |
| 6795 | } else { |
| 6796 | // The base is the reference to the variable. |
| 6797 | // BP = &Var. |
Alexey Bataev | f47c4b4 | 2017-09-26 13:47:31 +0000 | [diff] [blame] | 6798 | BP = CGF.EmitOMPSharedLValue(I->getAssociatedExpression()).getPointer(); |
Alexey Bataev | 92327c5 | 2018-03-26 16:40:55 +0000 | [diff] [blame] | 6799 | if (const auto *VD = |
| 6800 | dyn_cast_or_null<VarDecl>(I->getAssociatedDeclaration())) { |
| 6801 | if (llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res = |
| 6802 | isDeclareTargetDeclaration(VD)) { |
| 6803 | assert(*Res == OMPDeclareTargetDeclAttr::MT_Link && |
| 6804 | "Declare target link is expected."); |
| 6805 | // Avoid warning in release build. |
| 6806 | (void)*Res; |
| 6807 | IsLink = true; |
| 6808 | BP = CGF.CGM.getOpenMPRuntime() |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 6809 | .getAddrOfDeclareTargetLink(VD) |
Alexey Bataev | 92327c5 | 2018-03-26 16:40:55 +0000 | [diff] [blame] | 6810 | .getPointer(); |
| 6811 | } |
| 6812 | } |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6813 | |
| 6814 | // 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] | 6815 | // 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] | 6816 | // reference. References are ignored for mapping purposes. |
| 6817 | QualType Ty = |
| 6818 | I->getAssociatedDeclaration()->getType().getNonReferenceType(); |
| 6819 | if (Ty->isAnyPointerType() && std::next(I) != CE) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 6820 | LValue PtrAddr = CGF.MakeNaturalAlignAddrLValue(BP, Ty); |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6821 | BP = CGF.EmitLoadOfPointerLValue(PtrAddr.getAddress(), |
Samuel Antao | 403ffd4 | 2016-07-27 22:49:49 +0000 | [diff] [blame] | 6822 | Ty->castAs<PointerType>()) |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6823 | .getPointer(); |
| 6824 | |
| 6825 | // We do not need to generate individual map information for the |
| 6826 | // pointer, it can be associated with the combined storage. |
| 6827 | ++I; |
| 6828 | } |
| 6829 | } |
| 6830 | |
George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 6831 | uint64_t DefaultFlags = IsImplicit ? OMP_MAP_IMPLICIT : 0; |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6832 | for (; I != CE; ++I) { |
| 6833 | auto Next = std::next(I); |
| 6834 | |
| 6835 | // We need to generate the addresses and sizes if this is the last |
| 6836 | // component, if the component is a pointer or if it is an array section |
| 6837 | // whose length can't be proved to be one. If this is a pointer, it |
| 6838 | // becomes the base address for the following components. |
| 6839 | |
| 6840 | // A final array section, is one whose length can't be proved to be one. |
| 6841 | bool IsFinalArraySection = |
| 6842 | isFinalArraySectionExpression(I->getAssociatedExpression()); |
| 6843 | |
| 6844 | // Get information on whether the element is a pointer. Have to do a |
| 6845 | // special treatment for array sections given that they are built-in |
| 6846 | // types. |
| 6847 | const auto *OASE = |
| 6848 | dyn_cast<OMPArraySectionExpr>(I->getAssociatedExpression()); |
| 6849 | bool IsPointer = |
| 6850 | (OASE && |
| 6851 | OMPArraySectionExpr::getBaseOriginalType(OASE) |
| 6852 | .getCanonicalType() |
| 6853 | ->isAnyPointerType()) || |
| 6854 | I->getAssociatedExpression()->getType()->isAnyPointerType(); |
| 6855 | |
| 6856 | if (Next == CE || IsPointer || IsFinalArraySection) { |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6857 | // If this is not the last component, we expect the pointer to be |
| 6858 | // associated with an array expression or member expression. |
| 6859 | assert((Next == CE || |
| 6860 | isa<MemberExpr>(Next->getAssociatedExpression()) || |
| 6861 | isa<ArraySubscriptExpr>(Next->getAssociatedExpression()) || |
| 6862 | isa<OMPArraySectionExpr>(Next->getAssociatedExpression())) && |
| 6863 | "Unexpected expression"); |
| 6864 | |
Alexey Bataev | f47c4b4 | 2017-09-26 13:47:31 +0000 | [diff] [blame] | 6865 | llvm::Value *LB = |
| 6866 | CGF.EmitOMPSharedLValue(I->getAssociatedExpression()).getPointer(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 6867 | llvm::Value *Size = getExprTypeSize(I->getAssociatedExpression()); |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6868 | |
Samuel Antao | 03a3cec | 2016-07-27 22:52:16 +0000 | [diff] [blame] | 6869 | // If we have a member expression and the current component is a |
| 6870 | // reference, we have to map the reference too. Whenever we have a |
| 6871 | // reference, the section that reference refers to is going to be a |
| 6872 | // load instruction from the storage assigned to the reference. |
| 6873 | if (isa<MemberExpr>(I->getAssociatedExpression()) && |
| 6874 | I->getAssociatedDeclaration()->getType()->isReferenceType()) { |
| 6875 | auto *LI = cast<llvm::LoadInst>(LB); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 6876 | llvm::Value *RefAddr = LI->getPointerOperand(); |
Samuel Antao | 03a3cec | 2016-07-27 22:52:16 +0000 | [diff] [blame] | 6877 | |
| 6878 | BasePointers.push_back(BP); |
| 6879 | Pointers.push_back(RefAddr); |
| 6880 | Sizes.push_back(CGF.getTypeSize(CGF.getContext().VoidPtrTy)); |
Alexey Bataev | f47c4b4 | 2017-09-26 13:47:31 +0000 | [diff] [blame] | 6881 | Types.push_back(DefaultFlags | |
| 6882 | getMapTypeBits( |
| 6883 | /*MapType*/ OMPC_MAP_alloc, |
| 6884 | /*MapTypeModifier=*/OMPC_MAP_unknown, |
| 6885 | !IsExpressionFirstInfo, IsCaptureFirstInfo)); |
Samuel Antao | 03a3cec | 2016-07-27 22:52:16 +0000 | [diff] [blame] | 6886 | IsExpressionFirstInfo = false; |
| 6887 | IsCaptureFirstInfo = false; |
| 6888 | // The reference will be the next base address. |
| 6889 | BP = RefAddr; |
| 6890 | } |
| 6891 | |
| 6892 | BasePointers.push_back(BP); |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6893 | Pointers.push_back(LB); |
| 6894 | Sizes.push_back(Size); |
Samuel Antao | 03a3cec | 2016-07-27 22:52:16 +0000 | [diff] [blame] | 6895 | |
Samuel Antao | 6782e94 | 2016-05-26 16:48:10 +0000 | [diff] [blame] | 6896 | // We need to add a pointer flag for each map that comes from the |
| 6897 | // same expression except for the first one. We also need to signal |
| 6898 | // this map is the first one that relates with the current capture |
| 6899 | // (there is a set of entries for each capture). |
Alexey Bataev | 92327c5 | 2018-03-26 16:40:55 +0000 | [diff] [blame] | 6900 | Types.push_back(DefaultFlags | |
| 6901 | getMapTypeBits(MapType, MapTypeModifier, |
| 6902 | !IsExpressionFirstInfo || IsLink, |
| 6903 | IsCaptureFirstInfo && !IsLink)); |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6904 | |
| 6905 | // If we have a final array section, we are done with this expression. |
| 6906 | if (IsFinalArraySection) |
| 6907 | break; |
| 6908 | |
| 6909 | // The pointer becomes the base for the next element. |
| 6910 | if (Next != CE) |
| 6911 | BP = LB; |
| 6912 | |
| 6913 | IsExpressionFirstInfo = false; |
| 6914 | IsCaptureFirstInfo = false; |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6915 | } |
| 6916 | } |
| 6917 | } |
| 6918 | |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6919 | /// \brief Generate all the base pointers, section pointers, sizes and map |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 6920 | /// types for the extracted mappable expressions. Also, for each item that |
| 6921 | /// relates with a device pointer, a pair of the relevant declaration and |
| 6922 | /// index where it occurs is appended to the device pointers info array. |
| 6923 | void generateAllInfo(MapBaseValuesArrayTy &BasePointers, |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6924 | MapValuesArrayTy &Pointers, MapValuesArrayTy &Sizes, |
| 6925 | MapFlagsArrayTy &Types) const { |
| 6926 | BasePointers.clear(); |
| 6927 | Pointers.clear(); |
| 6928 | Sizes.clear(); |
| 6929 | Types.clear(); |
| 6930 | |
| 6931 | struct MapInfo { |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 6932 | /// Kind that defines how a device pointer has to be returned. |
| 6933 | enum ReturnPointerKind { |
| 6934 | // Don't have to return any pointer. |
| 6935 | RPK_None, |
| 6936 | // Pointer is the base of the declaration. |
| 6937 | RPK_Base, |
| 6938 | // Pointer is a member of the base declaration - 'this' |
| 6939 | RPK_Member, |
| 6940 | // Pointer is a reference and a member of the base declaration - 'this' |
| 6941 | RPK_MemberReference, |
| 6942 | }; |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6943 | OMPClauseMappableExprCommon::MappableExprComponentListRef Components; |
Alexey Bataev | f47c4b4 | 2017-09-26 13:47:31 +0000 | [diff] [blame] | 6944 | OpenMPMapClauseKind MapType = OMPC_MAP_unknown; |
| 6945 | OpenMPMapClauseKind MapTypeModifier = OMPC_MAP_unknown; |
| 6946 | ReturnPointerKind ReturnDevicePointer = RPK_None; |
| 6947 | bool IsImplicit = false; |
Hans Wennborg | bc1b58d | 2016-07-30 00:41:37 +0000 | [diff] [blame] | 6948 | |
Alexey Bataev | f47c4b4 | 2017-09-26 13:47:31 +0000 | [diff] [blame] | 6949 | MapInfo() = default; |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 6950 | MapInfo( |
| 6951 | OMPClauseMappableExprCommon::MappableExprComponentListRef Components, |
| 6952 | OpenMPMapClauseKind MapType, OpenMPMapClauseKind MapTypeModifier, |
Alexey Bataev | f47c4b4 | 2017-09-26 13:47:31 +0000 | [diff] [blame] | 6953 | ReturnPointerKind ReturnDevicePointer, bool IsImplicit) |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 6954 | : Components(Components), MapType(MapType), |
| 6955 | MapTypeModifier(MapTypeModifier), |
Alexey Bataev | f47c4b4 | 2017-09-26 13:47:31 +0000 | [diff] [blame] | 6956 | ReturnDevicePointer(ReturnDevicePointer), IsImplicit(IsImplicit) {} |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6957 | }; |
| 6958 | |
| 6959 | // We have to process the component lists that relate with the same |
| 6960 | // declaration in a single chunk so that we can generate the map flags |
| 6961 | // correctly. Therefore, we organize all lists in a map. |
Alexey Bataev | 5d1c3f6 | 2017-06-27 15:46:42 +0000 | [diff] [blame] | 6962 | llvm::MapVector<const ValueDecl *, SmallVector<MapInfo, 8>> Info; |
Samuel Antao | 8d2d730 | 2016-05-26 18:30:22 +0000 | [diff] [blame] | 6963 | |
| 6964 | // Helper function to fill the information map for the different supported |
| 6965 | // clauses. |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 6966 | auto &&InfoGen = [&Info]( |
| 6967 | const ValueDecl *D, |
| 6968 | OMPClauseMappableExprCommon::MappableExprComponentListRef L, |
| 6969 | OpenMPMapClauseKind MapType, OpenMPMapClauseKind MapModifier, |
Alexey Bataev | f47c4b4 | 2017-09-26 13:47:31 +0000 | [diff] [blame] | 6970 | MapInfo::ReturnPointerKind ReturnDevicePointer, bool IsImplicit) { |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 6971 | const ValueDecl *VD = |
| 6972 | D ? cast<ValueDecl>(D->getCanonicalDecl()) : nullptr; |
Alexey Bataev | f47c4b4 | 2017-09-26 13:47:31 +0000 | [diff] [blame] | 6973 | Info[VD].emplace_back(L, MapType, MapModifier, ReturnDevicePointer, |
| 6974 | IsImplicit); |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 6975 | }; |
Samuel Antao | 8d2d730 | 2016-05-26 18:30:22 +0000 | [diff] [blame] | 6976 | |
Paul Robinson | 78fb132 | 2016-08-01 22:12:46 +0000 | [diff] [blame] | 6977 | // FIXME: MSVC 2013 seems to require this-> to find member CurDir. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 6978 | for (const auto *C : this->CurDir.getClausesOfKind<OMPMapClause>()) |
| 6979 | for (const auto &L : C->component_lists()) { |
Samuel Antao | cf3f83e | 2016-07-28 14:47:35 +0000 | [diff] [blame] | 6980 | InfoGen(L.first, L.second, C->getMapType(), C->getMapTypeModifier(), |
Alexey Bataev | f47c4b4 | 2017-09-26 13:47:31 +0000 | [diff] [blame] | 6981 | MapInfo::RPK_None, C->isImplicit()); |
| 6982 | } |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 6983 | for (const auto *C : this->CurDir.getClausesOfKind<OMPToClause>()) |
| 6984 | for (const auto &L : C->component_lists()) { |
Samuel Antao | cf3f83e | 2016-07-28 14:47:35 +0000 | [diff] [blame] | 6985 | InfoGen(L.first, L.second, OMPC_MAP_to, OMPC_MAP_unknown, |
Alexey Bataev | f47c4b4 | 2017-09-26 13:47:31 +0000 | [diff] [blame] | 6986 | MapInfo::RPK_None, C->isImplicit()); |
| 6987 | } |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 6988 | for (const auto *C : this->CurDir.getClausesOfKind<OMPFromClause>()) |
| 6989 | for (const auto &L : C->component_lists()) { |
Samuel Antao | cf3f83e | 2016-07-28 14:47:35 +0000 | [diff] [blame] | 6990 | InfoGen(L.first, L.second, OMPC_MAP_from, OMPC_MAP_unknown, |
Alexey Bataev | f47c4b4 | 2017-09-26 13:47:31 +0000 | [diff] [blame] | 6991 | MapInfo::RPK_None, C->isImplicit()); |
| 6992 | } |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6993 | |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 6994 | // Look at the use_device_ptr clause information and mark the existing map |
| 6995 | // entries as such. If there is no map information for an entry in the |
| 6996 | // use_device_ptr list, we create one with map type 'alloc' and zero size |
| 6997 | // section. It is the user fault if that was not mapped before. |
Paul Robinson | 78fb132 | 2016-08-01 22:12:46 +0000 | [diff] [blame] | 6998 | // FIXME: MSVC 2013 seems to require this-> to find member CurDir. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 6999 | for (const auto *C : this->CurDir.getClausesOfKind<OMPUseDevicePtrClause>()) |
| 7000 | for (const auto &L : C->component_lists()) { |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7001 | assert(!L.second.empty() && "Not expecting empty list of components!"); |
| 7002 | const ValueDecl *VD = L.second.back().getAssociatedDeclaration(); |
| 7003 | VD = cast<ValueDecl>(VD->getCanonicalDecl()); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 7004 | const Expr *IE = L.second.back().getAssociatedExpression(); |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7005 | // If the first component is a member expression, we have to look into |
| 7006 | // 'this', which maps to null in the map of map information. Otherwise |
| 7007 | // look directly for the information. |
| 7008 | auto It = Info.find(isa<MemberExpr>(IE) ? nullptr : VD); |
| 7009 | |
| 7010 | // We potentially have map information for this declaration already. |
| 7011 | // Look for the first set of components that refer to it. |
| 7012 | if (It != Info.end()) { |
| 7013 | auto CI = std::find_if( |
| 7014 | It->second.begin(), It->second.end(), [VD](const MapInfo &MI) { |
| 7015 | return MI.Components.back().getAssociatedDeclaration() == VD; |
| 7016 | }); |
| 7017 | // If we found a map entry, signal that the pointer has to be returned |
| 7018 | // and move on to the next declaration. |
| 7019 | if (CI != It->second.end()) { |
| 7020 | CI->ReturnDevicePointer = isa<MemberExpr>(IE) |
| 7021 | ? (VD->getType()->isReferenceType() |
| 7022 | ? MapInfo::RPK_MemberReference |
| 7023 | : MapInfo::RPK_Member) |
| 7024 | : MapInfo::RPK_Base; |
| 7025 | continue; |
| 7026 | } |
| 7027 | } |
| 7028 | |
| 7029 | // We didn't find any match in our map information - generate a zero |
| 7030 | // size array section. |
Paul Robinson | 78fb132 | 2016-08-01 22:12:46 +0000 | [diff] [blame] | 7031 | // FIXME: MSVC 2013 seems to require this-> to find member CGF. |
Alexey Bataev | 1e49137 | 2018-01-23 18:44:14 +0000 | [diff] [blame] | 7032 | llvm::Value *Ptr = this->CGF.EmitLoadOfScalar(this->CGF.EmitLValue(IE), |
| 7033 | IE->getExprLoc()); |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7034 | BasePointers.push_back({Ptr, VD}); |
| 7035 | Pointers.push_back(Ptr); |
Paul Robinson | 15c8400 | 2016-07-29 20:46:16 +0000 | [diff] [blame] | 7036 | Sizes.push_back(llvm::Constant::getNullValue(this->CGF.SizeTy)); |
George Rokos | 065755d | 2017-11-07 18:27:04 +0000 | [diff] [blame] | 7037 | Types.push_back(OMP_MAP_RETURN_PARAM | OMP_MAP_TARGET_PARAM); |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7038 | } |
| 7039 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 7040 | for (const auto &M : Info) { |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7041 | // We need to know when we generate information for the first component |
| 7042 | // associated with a capture, because the mapping flags depend on it. |
| 7043 | bool IsFirstComponentList = true; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 7044 | for (const MapInfo &L : M.second) { |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7045 | assert(!L.Components.empty() && |
| 7046 | "Not expecting declaration with no component lists."); |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7047 | |
| 7048 | // Remember the current base pointer index. |
| 7049 | unsigned CurrentBasePointersIdx = BasePointers.size(); |
Paul Robinson | 78fb132 | 2016-08-01 22:12:46 +0000 | [diff] [blame] | 7050 | // FIXME: MSVC 2013 seems to require this-> to find the member method. |
Alexey Bataev | f47c4b4 | 2017-09-26 13:47:31 +0000 | [diff] [blame] | 7051 | this->generateInfoForComponentList( |
| 7052 | L.MapType, L.MapTypeModifier, L.Components, BasePointers, Pointers, |
| 7053 | Sizes, Types, IsFirstComponentList, L.IsImplicit); |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7054 | |
| 7055 | // If this entry relates with a device pointer, set the relevant |
| 7056 | // declaration and add the 'return pointer' flag. |
| 7057 | if (IsFirstComponentList && |
| 7058 | L.ReturnDevicePointer != MapInfo::RPK_None) { |
| 7059 | // If the pointer is not the base of the map, we need to skip the |
| 7060 | // base. If it is a reference in a member field, we also need to skip |
| 7061 | // the map of the reference. |
| 7062 | if (L.ReturnDevicePointer != MapInfo::RPK_Base) { |
| 7063 | ++CurrentBasePointersIdx; |
| 7064 | if (L.ReturnDevicePointer == MapInfo::RPK_MemberReference) |
| 7065 | ++CurrentBasePointersIdx; |
| 7066 | } |
| 7067 | assert(BasePointers.size() > CurrentBasePointersIdx && |
| 7068 | "Unexpected number of mapped base pointers."); |
| 7069 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 7070 | const ValueDecl *RelevantVD = |
| 7071 | L.Components.back().getAssociatedDeclaration(); |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7072 | assert(RelevantVD && |
| 7073 | "No relevant declaration related with device pointer??"); |
| 7074 | |
| 7075 | BasePointers[CurrentBasePointersIdx].setDevicePtrDecl(RelevantVD); |
George Rokos | 065755d | 2017-11-07 18:27:04 +0000 | [diff] [blame] | 7076 | Types[CurrentBasePointersIdx] |= OMP_MAP_RETURN_PARAM; |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7077 | } |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7078 | IsFirstComponentList = false; |
| 7079 | } |
| 7080 | } |
| 7081 | } |
| 7082 | |
| 7083 | /// \brief Generate the base pointers, section pointers, sizes and map types |
| 7084 | /// associated to a given capture. |
| 7085 | void generateInfoForCapture(const CapturedStmt::Capture *Cap, |
Samuel Antao | 6890b09 | 2016-07-28 14:25:09 +0000 | [diff] [blame] | 7086 | llvm::Value *Arg, |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7087 | MapBaseValuesArrayTy &BasePointers, |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7088 | MapValuesArrayTy &Pointers, |
| 7089 | MapValuesArrayTy &Sizes, |
| 7090 | MapFlagsArrayTy &Types) const { |
| 7091 | assert(!Cap->capturesVariableArrayType() && |
| 7092 | "Not expecting to generate map info for a variable array type!"); |
| 7093 | |
| 7094 | BasePointers.clear(); |
| 7095 | Pointers.clear(); |
| 7096 | Sizes.clear(); |
| 7097 | Types.clear(); |
| 7098 | |
Samuel Antao | 6890b09 | 2016-07-28 14:25:09 +0000 | [diff] [blame] | 7099 | // We need to know when we generating information for the first component |
| 7100 | // associated with a capture, because the mapping flags depend on it. |
| 7101 | bool IsFirstComponentList = true; |
| 7102 | |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7103 | const ValueDecl *VD = |
| 7104 | Cap->capturesThis() |
| 7105 | ? nullptr |
George Burgess IV | 00f70bd | 2018-03-01 05:43:23 +0000 | [diff] [blame] | 7106 | : Cap->getCapturedVar()->getCanonicalDecl(); |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7107 | |
Samuel Antao | 6890b09 | 2016-07-28 14:25:09 +0000 | [diff] [blame] | 7108 | // If this declaration appears in a is_device_ptr clause we just have to |
| 7109 | // pass the pointer by value. If it is a reference to a declaration, we just |
| 7110 | // pass its value, otherwise, if it is a member expression, we need to map |
| 7111 | // 'to' the field. |
| 7112 | if (!VD) { |
| 7113 | auto It = DevPointersMap.find(VD); |
| 7114 | if (It != DevPointersMap.end()) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 7115 | for (ArrayRef<OMPClauseMappableExprCommon::MappableComponent> L : |
| 7116 | It->second) { |
Samuel Antao | 6890b09 | 2016-07-28 14:25:09 +0000 | [diff] [blame] | 7117 | generateInfoForComponentList( |
| 7118 | /*MapType=*/OMPC_MAP_to, /*MapTypeModifier=*/OMPC_MAP_unknown, L, |
Alexey Bataev | f47c4b4 | 2017-09-26 13:47:31 +0000 | [diff] [blame] | 7119 | BasePointers, Pointers, Sizes, Types, IsFirstComponentList, |
| 7120 | /*IsImplicit=*/false); |
Samuel Antao | 6890b09 | 2016-07-28 14:25:09 +0000 | [diff] [blame] | 7121 | IsFirstComponentList = false; |
| 7122 | } |
| 7123 | return; |
| 7124 | } |
| 7125 | } else if (DevPointersMap.count(VD)) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 7126 | BasePointers.emplace_back(Arg, VD); |
Samuel Antao | 6890b09 | 2016-07-28 14:25:09 +0000 | [diff] [blame] | 7127 | Pointers.push_back(Arg); |
| 7128 | Sizes.push_back(CGF.getTypeSize(CGF.getContext().VoidPtrTy)); |
George Rokos | 065755d | 2017-11-07 18:27:04 +0000 | [diff] [blame] | 7129 | Types.push_back(OMP_MAP_LITERAL | OMP_MAP_TARGET_PARAM); |
Samuel Antao | 6890b09 | 2016-07-28 14:25:09 +0000 | [diff] [blame] | 7130 | return; |
| 7131 | } |
| 7132 | |
Paul Robinson | 78fb132 | 2016-08-01 22:12:46 +0000 | [diff] [blame] | 7133 | // FIXME: MSVC 2013 seems to require this-> to find member CurDir. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 7134 | for (const auto *C : this->CurDir.getClausesOfKind<OMPMapClause>()) |
| 7135 | for (const auto &L : C->decl_component_lists(VD)) { |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7136 | assert(L.first == VD && |
| 7137 | "We got information for the wrong declaration??"); |
| 7138 | assert(!L.second.empty() && |
| 7139 | "Not expecting declaration with no component lists."); |
Alexey Bataev | f47c4b4 | 2017-09-26 13:47:31 +0000 | [diff] [blame] | 7140 | generateInfoForComponentList( |
| 7141 | C->getMapType(), C->getMapTypeModifier(), L.second, BasePointers, |
| 7142 | Pointers, Sizes, Types, IsFirstComponentList, C->isImplicit()); |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7143 | IsFirstComponentList = false; |
| 7144 | } |
| 7145 | |
| 7146 | return; |
| 7147 | } |
Samuel Antao | d486f84 | 2016-05-26 16:53:38 +0000 | [diff] [blame] | 7148 | |
| 7149 | /// \brief Generate the default map information for a given capture \a CI, |
| 7150 | /// record field declaration \a RI and captured value \a CV. |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7151 | void generateDefaultMapInfo(const CapturedStmt::Capture &CI, |
| 7152 | const FieldDecl &RI, llvm::Value *CV, |
| 7153 | MapBaseValuesArrayTy &CurBasePointers, |
| 7154 | MapValuesArrayTy &CurPointers, |
| 7155 | MapValuesArrayTy &CurSizes, |
| 7156 | MapFlagsArrayTy &CurMapTypes) { |
Samuel Antao | d486f84 | 2016-05-26 16:53:38 +0000 | [diff] [blame] | 7157 | |
| 7158 | // Do the default mapping. |
| 7159 | if (CI.capturesThis()) { |
| 7160 | CurBasePointers.push_back(CV); |
| 7161 | CurPointers.push_back(CV); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 7162 | const auto *PtrTy = cast<PointerType>(RI.getType().getTypePtr()); |
Samuel Antao | d486f84 | 2016-05-26 16:53:38 +0000 | [diff] [blame] | 7163 | CurSizes.push_back(CGF.getTypeSize(PtrTy->getPointeeType())); |
| 7164 | // Default map type. |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7165 | CurMapTypes.push_back(OMP_MAP_TO | OMP_MAP_FROM); |
Samuel Antao | d486f84 | 2016-05-26 16:53:38 +0000 | [diff] [blame] | 7166 | } else if (CI.capturesVariableByCopy()) { |
Samuel Antao | 6d00426 | 2016-06-16 18:39:34 +0000 | [diff] [blame] | 7167 | CurBasePointers.push_back(CV); |
| 7168 | CurPointers.push_back(CV); |
Samuel Antao | d486f84 | 2016-05-26 16:53:38 +0000 | [diff] [blame] | 7169 | if (!RI.getType()->isAnyPointerType()) { |
Samuel Antao | 6d00426 | 2016-06-16 18:39:34 +0000 | [diff] [blame] | 7170 | // We have to signal to the runtime captures passed by value that are |
| 7171 | // not pointers. |
George Rokos | 065755d | 2017-11-07 18:27:04 +0000 | [diff] [blame] | 7172 | CurMapTypes.push_back(OMP_MAP_LITERAL); |
Samuel Antao | d486f84 | 2016-05-26 16:53:38 +0000 | [diff] [blame] | 7173 | CurSizes.push_back(CGF.getTypeSize(RI.getType())); |
| 7174 | } else { |
| 7175 | // Pointers are implicitly mapped with a zero size and no flags |
| 7176 | // (other than first map that is added for all implicit maps). |
| 7177 | CurMapTypes.push_back(0u); |
Samuel Antao | d486f84 | 2016-05-26 16:53:38 +0000 | [diff] [blame] | 7178 | CurSizes.push_back(llvm::Constant::getNullValue(CGF.SizeTy)); |
| 7179 | } |
| 7180 | } else { |
| 7181 | assert(CI.capturesVariable() && "Expected captured reference."); |
| 7182 | CurBasePointers.push_back(CV); |
| 7183 | CurPointers.push_back(CV); |
| 7184 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 7185 | const auto *PtrTy = cast<ReferenceType>(RI.getType().getTypePtr()); |
Samuel Antao | d486f84 | 2016-05-26 16:53:38 +0000 | [diff] [blame] | 7186 | QualType ElementType = PtrTy->getPointeeType(); |
| 7187 | CurSizes.push_back(CGF.getTypeSize(ElementType)); |
| 7188 | // The default map type for a scalar/complex type is 'to' because by |
| 7189 | // default the value doesn't have to be retrieved. For an aggregate |
| 7190 | // type, the default is 'tofrom'. |
Alexey Bataev | 3f96fe6 | 2017-12-13 17:31:39 +0000 | [diff] [blame] | 7191 | CurMapTypes.emplace_back(adjustMapModifiersForPrivateClauses( |
| 7192 | CI, ElementType->isAggregateType() ? (OMP_MAP_TO | OMP_MAP_FROM) |
| 7193 | : OMP_MAP_TO)); |
Samuel Antao | d486f84 | 2016-05-26 16:53:38 +0000 | [diff] [blame] | 7194 | } |
George Rokos | 065755d | 2017-11-07 18:27:04 +0000 | [diff] [blame] | 7195 | // Every default map produces a single argument which is a target parameter. |
| 7196 | CurMapTypes.back() |= OMP_MAP_TARGET_PARAM; |
Samuel Antao | d486f84 | 2016-05-26 16:53:38 +0000 | [diff] [blame] | 7197 | } |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7198 | }; |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 7199 | |
| 7200 | enum OpenMPOffloadingReservedDeviceIDs { |
| 7201 | /// \brief Device ID if the device was not defined, runtime should get it |
| 7202 | /// from environment variables in the spec. |
| 7203 | OMP_DEVICEID_UNDEF = -1, |
| 7204 | }; |
| 7205 | } // anonymous namespace |
| 7206 | |
| 7207 | /// \brief Emit the arrays used to pass the captures and map information to the |
| 7208 | /// offloading runtime library. If there is no map or capture information, |
| 7209 | /// return nullptr by reference. |
| 7210 | static void |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7211 | emitOffloadingArrays(CodeGenFunction &CGF, |
| 7212 | MappableExprsHandler::MapBaseValuesArrayTy &BasePointers, |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 7213 | MappableExprsHandler::MapValuesArrayTy &Pointers, |
| 7214 | MappableExprsHandler::MapValuesArrayTy &Sizes, |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7215 | MappableExprsHandler::MapFlagsArrayTy &MapTypes, |
| 7216 | CGOpenMPRuntime::TargetDataInfo &Info) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 7217 | CodeGenModule &CGM = CGF.CGM; |
| 7218 | ASTContext &Ctx = CGF.getContext(); |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 7219 | |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7220 | // Reset the array information. |
| 7221 | Info.clearArrayInfo(); |
| 7222 | Info.NumberOfPtrs = BasePointers.size(); |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 7223 | |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7224 | if (Info.NumberOfPtrs) { |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 7225 | // Detect if we have any capture size requiring runtime evaluation of the |
| 7226 | // size so that a constant array could be eventually used. |
| 7227 | bool hasRuntimeEvaluationCaptureSize = false; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 7228 | for (llvm::Value *S : Sizes) |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 7229 | if (!isa<llvm::Constant>(S)) { |
| 7230 | hasRuntimeEvaluationCaptureSize = true; |
| 7231 | break; |
| 7232 | } |
| 7233 | |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7234 | llvm::APInt PointerNumAP(32, Info.NumberOfPtrs, /*isSigned=*/true); |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 7235 | QualType PointerArrayType = |
| 7236 | Ctx.getConstantArrayType(Ctx.VoidPtrTy, PointerNumAP, ArrayType::Normal, |
| 7237 | /*IndexTypeQuals=*/0); |
| 7238 | |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7239 | Info.BasePointersArray = |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 7240 | CGF.CreateMemTemp(PointerArrayType, ".offload_baseptrs").getPointer(); |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7241 | Info.PointersArray = |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 7242 | CGF.CreateMemTemp(PointerArrayType, ".offload_ptrs").getPointer(); |
| 7243 | |
| 7244 | // If we don't have any VLA types or other types that require runtime |
| 7245 | // evaluation, we can use a constant array for the map sizes, otherwise we |
| 7246 | // need to fill up the arrays as we do for the pointers. |
| 7247 | if (hasRuntimeEvaluationCaptureSize) { |
| 7248 | QualType SizeArrayType = Ctx.getConstantArrayType( |
| 7249 | Ctx.getSizeType(), PointerNumAP, ArrayType::Normal, |
| 7250 | /*IndexTypeQuals=*/0); |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7251 | Info.SizesArray = |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 7252 | CGF.CreateMemTemp(SizeArrayType, ".offload_sizes").getPointer(); |
| 7253 | } else { |
| 7254 | // We expect all the sizes to be constant, so we collect them to create |
| 7255 | // a constant array. |
| 7256 | SmallVector<llvm::Constant *, 16> ConstSizes; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 7257 | for (llvm::Value *S : Sizes) |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 7258 | ConstSizes.push_back(cast<llvm::Constant>(S)); |
| 7259 | |
| 7260 | auto *SizesArrayInit = llvm::ConstantArray::get( |
| 7261 | llvm::ArrayType::get(CGM.SizeTy, ConstSizes.size()), ConstSizes); |
| 7262 | auto *SizesArrayGbl = new llvm::GlobalVariable( |
| 7263 | CGM.getModule(), SizesArrayInit->getType(), |
| 7264 | /*isConstant=*/true, llvm::GlobalValue::PrivateLinkage, |
| 7265 | SizesArrayInit, ".offload_sizes"); |
Peter Collingbourne | bcf909d | 2016-06-14 21:02:05 +0000 | [diff] [blame] | 7266 | SizesArrayGbl->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global); |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7267 | Info.SizesArray = SizesArrayGbl; |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 7268 | } |
| 7269 | |
| 7270 | // The map types are always constant so we don't need to generate code to |
| 7271 | // fill arrays. Instead, we create an array constant. |
| 7272 | llvm::Constant *MapTypesArrayInit = |
| 7273 | llvm::ConstantDataArray::get(CGF.Builder.getContext(), MapTypes); |
| 7274 | auto *MapTypesArrayGbl = new llvm::GlobalVariable( |
| 7275 | CGM.getModule(), MapTypesArrayInit->getType(), |
| 7276 | /*isConstant=*/true, llvm::GlobalValue::PrivateLinkage, |
| 7277 | MapTypesArrayInit, ".offload_maptypes"); |
Peter Collingbourne | bcf909d | 2016-06-14 21:02:05 +0000 | [diff] [blame] | 7278 | MapTypesArrayGbl->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global); |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7279 | Info.MapTypesArray = MapTypesArrayGbl; |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 7280 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 7281 | for (unsigned I = 0; I < Info.NumberOfPtrs; ++I) { |
| 7282 | llvm::Value *BPVal = *BasePointers[I]; |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 7283 | llvm::Value *BP = CGF.Builder.CreateConstInBoundsGEP2_32( |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7284 | llvm::ArrayType::get(CGM.VoidPtrTy, Info.NumberOfPtrs), |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 7285 | Info.BasePointersArray, 0, I); |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 7286 | BP = CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
| 7287 | BP, BPVal->getType()->getPointerTo(/*AddrSpace=*/0)); |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 7288 | Address BPAddr(BP, Ctx.getTypeAlignInChars(Ctx.VoidPtrTy)); |
| 7289 | CGF.Builder.CreateStore(BPVal, BPAddr); |
| 7290 | |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7291 | if (Info.requiresDevicePointerInfo()) |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 7292 | if (const ValueDecl *DevVD = BasePointers[I].getDevicePtrDecl()) |
Alexey Bataev | 43a919f | 2018-04-13 17:48:43 +0000 | [diff] [blame] | 7293 | Info.CaptureDeviceAddrMap.try_emplace(DevVD, BPAddr); |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7294 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 7295 | llvm::Value *PVal = Pointers[I]; |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 7296 | llvm::Value *P = CGF.Builder.CreateConstInBoundsGEP2_32( |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7297 | llvm::ArrayType::get(CGM.VoidPtrTy, Info.NumberOfPtrs), |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 7298 | Info.PointersArray, 0, I); |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 7299 | P = CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
| 7300 | P, PVal->getType()->getPointerTo(/*AddrSpace=*/0)); |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 7301 | Address PAddr(P, Ctx.getTypeAlignInChars(Ctx.VoidPtrTy)); |
| 7302 | CGF.Builder.CreateStore(PVal, PAddr); |
| 7303 | |
| 7304 | if (hasRuntimeEvaluationCaptureSize) { |
| 7305 | llvm::Value *S = CGF.Builder.CreateConstInBoundsGEP2_32( |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7306 | llvm::ArrayType::get(CGM.SizeTy, Info.NumberOfPtrs), |
| 7307 | Info.SizesArray, |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 7308 | /*Idx0=*/0, |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 7309 | /*Idx1=*/I); |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 7310 | Address SAddr(S, Ctx.getTypeAlignInChars(Ctx.getSizeType())); |
| 7311 | CGF.Builder.CreateStore( |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 7312 | CGF.Builder.CreateIntCast(Sizes[I], CGM.SizeTy, /*isSigned=*/true), |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 7313 | SAddr); |
| 7314 | } |
| 7315 | } |
| 7316 | } |
| 7317 | } |
| 7318 | /// \brief Emit the arguments to be passed to the runtime library based on the |
| 7319 | /// arrays of pointers, sizes and map types. |
| 7320 | static void emitOffloadingArraysArgument( |
| 7321 | CodeGenFunction &CGF, llvm::Value *&BasePointersArrayArg, |
| 7322 | llvm::Value *&PointersArrayArg, llvm::Value *&SizesArrayArg, |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7323 | llvm::Value *&MapTypesArrayArg, CGOpenMPRuntime::TargetDataInfo &Info) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 7324 | CodeGenModule &CGM = CGF.CGM; |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7325 | if (Info.NumberOfPtrs) { |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 7326 | BasePointersArrayArg = CGF.Builder.CreateConstInBoundsGEP2_32( |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7327 | llvm::ArrayType::get(CGM.VoidPtrTy, Info.NumberOfPtrs), |
| 7328 | Info.BasePointersArray, |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 7329 | /*Idx0=*/0, /*Idx1=*/0); |
| 7330 | PointersArrayArg = CGF.Builder.CreateConstInBoundsGEP2_32( |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7331 | llvm::ArrayType::get(CGM.VoidPtrTy, Info.NumberOfPtrs), |
| 7332 | Info.PointersArray, |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 7333 | /*Idx0=*/0, |
| 7334 | /*Idx1=*/0); |
| 7335 | SizesArrayArg = CGF.Builder.CreateConstInBoundsGEP2_32( |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7336 | llvm::ArrayType::get(CGM.SizeTy, Info.NumberOfPtrs), Info.SizesArray, |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 7337 | /*Idx0=*/0, /*Idx1=*/0); |
| 7338 | MapTypesArrayArg = CGF.Builder.CreateConstInBoundsGEP2_32( |
George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 7339 | llvm::ArrayType::get(CGM.Int64Ty, Info.NumberOfPtrs), |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7340 | Info.MapTypesArray, |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 7341 | /*Idx0=*/0, |
| 7342 | /*Idx1=*/0); |
| 7343 | } else { |
| 7344 | BasePointersArrayArg = llvm::ConstantPointerNull::get(CGM.VoidPtrPtrTy); |
| 7345 | PointersArrayArg = llvm::ConstantPointerNull::get(CGM.VoidPtrPtrTy); |
| 7346 | SizesArrayArg = llvm::ConstantPointerNull::get(CGM.SizeTy->getPointerTo()); |
| 7347 | MapTypesArrayArg = |
George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 7348 | llvm::ConstantPointerNull::get(CGM.Int64Ty->getPointerTo()); |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 7349 | } |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7350 | } |
| 7351 | |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 7352 | void CGOpenMPRuntime::emitTargetCall(CodeGenFunction &CGF, |
| 7353 | const OMPExecutableDirective &D, |
| 7354 | llvm::Value *OutlinedFn, |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 7355 | llvm::Value *OutlinedFnID, |
Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 7356 | const Expr *IfCond, const Expr *Device) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 7357 | if (!CGF.HaveInsertPoint()) |
| 7358 | return; |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 7359 | |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 7360 | assert(OutlinedFn && "Invalid outlined function!"); |
| 7361 | |
Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 7362 | const bool RequiresOuterTask = D.hasClausesOfKind<OMPDependClause>(); |
| 7363 | llvm::SmallVector<llvm::Value *, 16> CapturedVars; |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 7364 | const CapturedStmt &CS = *D.getCapturedStmt(OMPD_target); |
Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 7365 | auto &&ArgsCodegen = [&CS, &CapturedVars](CodeGenFunction &CGF, |
| 7366 | PrePostActionTy &) { |
| 7367 | CGF.GenerateOpenMPCapturedVars(CS, CapturedVars); |
| 7368 | }; |
| 7369 | emitInlinedDirective(CGF, OMPD_unknown, ArgsCodegen); |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7370 | |
Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 7371 | CodeGenFunction::OMPTargetDataInfo InputInfo; |
| 7372 | llvm::Value *MapTypesArray = nullptr; |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 7373 | // Fill up the pointer arrays and transfer execution to the device. |
Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 7374 | auto &&ThenGen = [this, Device, OutlinedFn, OutlinedFnID, &D, &InputInfo, |
| 7375 | &MapTypesArray, &CS, RequiresOuterTask, |
| 7376 | &CapturedVars](CodeGenFunction &CGF, PrePostActionTy &) { |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 7377 | // On top of the arrays that were filled up, the target offloading call |
| 7378 | // takes as arguments the device id as well as the host pointer. The host |
| 7379 | // pointer is used by the runtime library to identify the current target |
| 7380 | // region, so it only has to be unique and not necessarily point to |
| 7381 | // anything. It could be the pointer to the outlined function that |
| 7382 | // implements the target region, but we aren't using that so that the |
| 7383 | // compiler doesn't need to keep that, and could therefore inline the host |
| 7384 | // function if proven worthwhile during optimization. |
| 7385 | |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 7386 | // From this point on, we need to have an ID of the target region defined. |
| 7387 | assert(OutlinedFnID && "Invalid outlined function ID!"); |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 7388 | |
| 7389 | // Emit device ID if any. |
| 7390 | llvm::Value *DeviceID; |
George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 7391 | if (Device) { |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 7392 | DeviceID = CGF.Builder.CreateIntCast(CGF.EmitScalarExpr(Device), |
George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 7393 | CGF.Int64Ty, /*isSigned=*/true); |
| 7394 | } else { |
| 7395 | DeviceID = CGF.Builder.getInt64(OMP_DEVICEID_UNDEF); |
| 7396 | } |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 7397 | |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 7398 | // Emit the number of elements in the offloading arrays. |
Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 7399 | llvm::Value *PointerNum = |
| 7400 | CGF.Builder.getInt32(InputInfo.NumberOfTargetItems); |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 7401 | |
Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 7402 | // Return value of the runtime offloading call. |
| 7403 | llvm::Value *Return; |
| 7404 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 7405 | llvm::Value *NumTeams = emitNumTeamsForTargetDirective(*this, CGF, D); |
| 7406 | llvm::Value *NumThreads = emitNumThreadsForTargetDirective(*this, CGF, D); |
Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 7407 | |
Alexey Bataev | a9f77c6 | 2017-12-13 21:04:20 +0000 | [diff] [blame] | 7408 | bool HasNowait = D.hasClausesOfKind<OMPNowaitClause>(); |
Arpith Chacko Jacob | 33c849a | 2017-01-25 00:57:16 +0000 | [diff] [blame] | 7409 | // The target region is an outlined function launched by the runtime |
| 7410 | // via calls __tgt_target() or __tgt_target_teams(). |
| 7411 | // |
| 7412 | // __tgt_target() launches a target region with one team and one thread, |
| 7413 | // executing a serial region. This master thread may in turn launch |
| 7414 | // more threads within its team upon encountering a parallel region, |
| 7415 | // however, no additional teams can be launched on the device. |
| 7416 | // |
| 7417 | // __tgt_target_teams() launches a target region with one or more teams, |
| 7418 | // each with one or more threads. This call is required for target |
| 7419 | // constructs such as: |
| 7420 | // 'target teams' |
| 7421 | // 'target' / 'teams' |
| 7422 | // 'target teams distribute parallel for' |
| 7423 | // 'target parallel' |
| 7424 | // and so on. |
| 7425 | // |
| 7426 | // Note that on the host and CPU targets, the runtime implementation of |
| 7427 | // these calls simply call the outlined function without forking threads. |
| 7428 | // The outlined functions themselves have runtime calls to |
| 7429 | // __kmpc_fork_teams() and __kmpc_fork() for this purpose, codegen'd by |
| 7430 | // the compiler in emitTeamsCall() and emitParallelCall(). |
| 7431 | // |
| 7432 | // In contrast, on the NVPTX target, the implementation of |
| 7433 | // __tgt_target_teams() launches a GPU kernel with the requested number |
| 7434 | // 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] | 7435 | if (NumTeams) { |
Arpith Chacko Jacob | 33c849a | 2017-01-25 00:57:16 +0000 | [diff] [blame] | 7436 | // If we have NumTeams defined this means that we have an enclosed teams |
| 7437 | // region. Therefore we also expect to have NumThreads defined. These two |
| 7438 | // values should be defined in the presence of a teams directive, |
| 7439 | // regardless of having any clauses associated. If the user is using teams |
| 7440 | // but no clauses, these two values will be the default that should be |
| 7441 | // passed to the runtime library - a 32-bit integer with the value zero. |
| 7442 | assert(NumThreads && "Thread limit expression should be available along " |
| 7443 | "with number of teams."); |
Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 7444 | llvm::Value *OffloadingArgs[] = {DeviceID, |
| 7445 | OutlinedFnID, |
| 7446 | PointerNum, |
| 7447 | InputInfo.BasePointersArray.getPointer(), |
| 7448 | InputInfo.PointersArray.getPointer(), |
| 7449 | InputInfo.SizesArray.getPointer(), |
| 7450 | MapTypesArray, |
| 7451 | NumTeams, |
| 7452 | NumThreads}; |
Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 7453 | Return = CGF.EmitRuntimeCall( |
Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 7454 | createRuntimeFunction(HasNowait ? OMPRTL__tgt_target_teams_nowait |
| 7455 | : OMPRTL__tgt_target_teams), |
Alexey Bataev | a9f77c6 | 2017-12-13 21:04:20 +0000 | [diff] [blame] | 7456 | OffloadingArgs); |
Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 7457 | } else { |
Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 7458 | llvm::Value *OffloadingArgs[] = {DeviceID, |
| 7459 | OutlinedFnID, |
| 7460 | PointerNum, |
| 7461 | InputInfo.BasePointersArray.getPointer(), |
| 7462 | InputInfo.PointersArray.getPointer(), |
| 7463 | InputInfo.SizesArray.getPointer(), |
| 7464 | MapTypesArray}; |
Alexey Bataev | a9f77c6 | 2017-12-13 21:04:20 +0000 | [diff] [blame] | 7465 | Return = CGF.EmitRuntimeCall( |
Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 7466 | createRuntimeFunction(HasNowait ? OMPRTL__tgt_target_nowait |
| 7467 | : OMPRTL__tgt_target), |
Alexey Bataev | a9f77c6 | 2017-12-13 21:04:20 +0000 | [diff] [blame] | 7468 | OffloadingArgs); |
Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 7469 | } |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 7470 | |
Alexey Bataev | 2a007e0 | 2017-10-02 14:20:58 +0000 | [diff] [blame] | 7471 | // Check the error code and execute the host version if required. |
| 7472 | llvm::BasicBlock *OffloadFailedBlock = |
| 7473 | CGF.createBasicBlock("omp_offload.failed"); |
| 7474 | llvm::BasicBlock *OffloadContBlock = |
| 7475 | CGF.createBasicBlock("omp_offload.cont"); |
| 7476 | llvm::Value *Failed = CGF.Builder.CreateIsNotNull(Return); |
| 7477 | CGF.Builder.CreateCondBr(Failed, OffloadFailedBlock, OffloadContBlock); |
| 7478 | |
| 7479 | CGF.EmitBlock(OffloadFailedBlock); |
Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 7480 | if (RequiresOuterTask) { |
| 7481 | CapturedVars.clear(); |
| 7482 | CGF.GenerateOpenMPCapturedVars(CS, CapturedVars); |
| 7483 | } |
| 7484 | emitOutlinedFunctionCall(CGF, D.getLocStart(), OutlinedFn, CapturedVars); |
Alexey Bataev | 2a007e0 | 2017-10-02 14:20:58 +0000 | [diff] [blame] | 7485 | CGF.EmitBranch(OffloadContBlock); |
| 7486 | |
| 7487 | CGF.EmitBlock(OffloadContBlock, /*IsFinished=*/true); |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 7488 | }; |
| 7489 | |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 7490 | // Notify that the host version must be executed. |
Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 7491 | auto &&ElseGen = [this, &D, OutlinedFn, &CS, &CapturedVars, |
| 7492 | RequiresOuterTask](CodeGenFunction &CGF, |
| 7493 | PrePostActionTy &) { |
| 7494 | if (RequiresOuterTask) { |
| 7495 | CapturedVars.clear(); |
| 7496 | CGF.GenerateOpenMPCapturedVars(CS, CapturedVars); |
| 7497 | } |
| 7498 | emitOutlinedFunctionCall(CGF, D.getLocStart(), OutlinedFn, CapturedVars); |
| 7499 | }; |
| 7500 | |
| 7501 | auto &&TargetThenGen = [this, &ThenGen, &D, &InputInfo, &MapTypesArray, |
| 7502 | &CapturedVars, RequiresOuterTask, |
| 7503 | &CS](CodeGenFunction &CGF, PrePostActionTy &) { |
| 7504 | // Fill up the arrays with all the captured variables. |
| 7505 | MappableExprsHandler::MapBaseValuesArrayTy BasePointers; |
| 7506 | MappableExprsHandler::MapValuesArrayTy Pointers; |
| 7507 | MappableExprsHandler::MapValuesArrayTy Sizes; |
| 7508 | MappableExprsHandler::MapFlagsArrayTy MapTypes; |
| 7509 | |
| 7510 | MappableExprsHandler::MapBaseValuesArrayTy CurBasePointers; |
| 7511 | MappableExprsHandler::MapValuesArrayTy CurPointers; |
| 7512 | MappableExprsHandler::MapValuesArrayTy CurSizes; |
| 7513 | MappableExprsHandler::MapFlagsArrayTy CurMapTypes; |
| 7514 | |
| 7515 | // Get mappable expression information. |
| 7516 | MappableExprsHandler MEHandler(D, CGF); |
| 7517 | |
| 7518 | auto RI = CS.getCapturedRecordDecl()->field_begin(); |
| 7519 | auto CV = CapturedVars.begin(); |
| 7520 | for (CapturedStmt::const_capture_iterator CI = CS.capture_begin(), |
| 7521 | CE = CS.capture_end(); |
| 7522 | CI != CE; ++CI, ++RI, ++CV) { |
| 7523 | CurBasePointers.clear(); |
| 7524 | CurPointers.clear(); |
| 7525 | CurSizes.clear(); |
| 7526 | CurMapTypes.clear(); |
| 7527 | |
| 7528 | // VLA sizes are passed to the outlined region by copy and do not have map |
| 7529 | // information associated. |
| 7530 | if (CI->capturesVariableArrayType()) { |
| 7531 | CurBasePointers.push_back(*CV); |
| 7532 | CurPointers.push_back(*CV); |
| 7533 | CurSizes.push_back(CGF.getTypeSize(RI->getType())); |
| 7534 | // Copy to the device as an argument. No need to retrieve it. |
| 7535 | CurMapTypes.push_back(MappableExprsHandler::OMP_MAP_LITERAL | |
| 7536 | MappableExprsHandler::OMP_MAP_TARGET_PARAM); |
| 7537 | } else { |
| 7538 | // If we have any information in the map clause, we use it, otherwise we |
| 7539 | // just do a default mapping. |
| 7540 | MEHandler.generateInfoForCapture(CI, *CV, CurBasePointers, CurPointers, |
| 7541 | CurSizes, CurMapTypes); |
| 7542 | if (CurBasePointers.empty()) |
| 7543 | MEHandler.generateDefaultMapInfo(*CI, **RI, *CV, CurBasePointers, |
| 7544 | CurPointers, CurSizes, CurMapTypes); |
| 7545 | } |
| 7546 | // We expect to have at least an element of information for this capture. |
| 7547 | assert(!CurBasePointers.empty() && |
| 7548 | "Non-existing map pointer for capture!"); |
| 7549 | assert(CurBasePointers.size() == CurPointers.size() && |
| 7550 | CurBasePointers.size() == CurSizes.size() && |
| 7551 | CurBasePointers.size() == CurMapTypes.size() && |
| 7552 | "Inconsistent map information sizes!"); |
| 7553 | |
| 7554 | // We need to append the results of this capture to what we already have. |
| 7555 | BasePointers.append(CurBasePointers.begin(), CurBasePointers.end()); |
| 7556 | Pointers.append(CurPointers.begin(), CurPointers.end()); |
| 7557 | Sizes.append(CurSizes.begin(), CurSizes.end()); |
| 7558 | MapTypes.append(CurMapTypes.begin(), CurMapTypes.end()); |
| 7559 | } |
Alexey Bataev | 92327c5 | 2018-03-26 16:40:55 +0000 | [diff] [blame] | 7560 | // Map other list items in the map clause which are not captured variables |
| 7561 | // but "declare target link" global variables. |
| 7562 | for (const auto *C : D.getClausesOfKind<OMPMapClause>()) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 7563 | for (const auto &L : C->component_lists()) { |
Alexey Bataev | 92327c5 | 2018-03-26 16:40:55 +0000 | [diff] [blame] | 7564 | if (!L.first) |
| 7565 | continue; |
| 7566 | const auto *VD = dyn_cast<VarDecl>(L.first); |
| 7567 | if (!VD) |
| 7568 | continue; |
| 7569 | llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res = |
| 7570 | isDeclareTargetDeclaration(VD); |
| 7571 | if (!Res || *Res != OMPDeclareTargetDeclAttr::MT_Link) |
| 7572 | continue; |
| 7573 | MEHandler.generateInfoForComponentList( |
| 7574 | C->getMapType(), C->getMapTypeModifier(), L.second, BasePointers, |
| 7575 | Pointers, Sizes, MapTypes, /*IsFirstComponentList=*/true, |
| 7576 | C->isImplicit()); |
| 7577 | } |
| 7578 | } |
Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 7579 | |
| 7580 | TargetDataInfo Info; |
| 7581 | // Fill up the arrays and create the arguments. |
| 7582 | emitOffloadingArrays(CGF, BasePointers, Pointers, Sizes, MapTypes, Info); |
| 7583 | emitOffloadingArraysArgument(CGF, Info.BasePointersArray, |
| 7584 | Info.PointersArray, Info.SizesArray, |
| 7585 | Info.MapTypesArray, Info); |
| 7586 | InputInfo.NumberOfTargetItems = Info.NumberOfPtrs; |
| 7587 | InputInfo.BasePointersArray = |
| 7588 | Address(Info.BasePointersArray, CGM.getPointerAlign()); |
| 7589 | InputInfo.PointersArray = |
| 7590 | Address(Info.PointersArray, CGM.getPointerAlign()); |
| 7591 | InputInfo.SizesArray = Address(Info.SizesArray, CGM.getPointerAlign()); |
| 7592 | MapTypesArray = Info.MapTypesArray; |
| 7593 | if (RequiresOuterTask) |
| 7594 | CGF.EmitOMPTargetTaskBasedDirective(D, ThenGen, InputInfo); |
| 7595 | else |
| 7596 | emitInlinedDirective(CGF, D.getDirectiveKind(), ThenGen); |
| 7597 | }; |
| 7598 | |
| 7599 | auto &&TargetElseGen = [this, &ElseGen, &D, RequiresOuterTask]( |
| 7600 | CodeGenFunction &CGF, PrePostActionTy &) { |
| 7601 | if (RequiresOuterTask) { |
| 7602 | CodeGenFunction::OMPTargetDataInfo InputInfo; |
| 7603 | CGF.EmitOMPTargetTaskBasedDirective(D, ElseGen, InputInfo); |
| 7604 | } else { |
| 7605 | emitInlinedDirective(CGF, D.getDirectiveKind(), ElseGen); |
| 7606 | } |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 7607 | }; |
| 7608 | |
| 7609 | // If we have a target function ID it means that we need to support |
| 7610 | // offloading, otherwise, just execute on the host. We need to execute on host |
| 7611 | // regardless of the conditional in the if clause if, e.g., the user do not |
| 7612 | // specify target triples. |
| 7613 | if (OutlinedFnID) { |
Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 7614 | if (IfCond) { |
| 7615 | emitOMPIfClause(CGF, IfCond, TargetThenGen, TargetElseGen); |
| 7616 | } else { |
| 7617 | RegionCodeGenTy ThenRCG(TargetThenGen); |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 7618 | ThenRCG(CGF); |
Alexey Bataev | f539faa | 2016-03-28 12:58:34 +0000 | [diff] [blame] | 7619 | } |
| 7620 | } else { |
Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 7621 | RegionCodeGenTy ElseRCG(TargetElseGen); |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 7622 | ElseRCG(CGF); |
Alexey Bataev | f539faa | 2016-03-28 12:58:34 +0000 | [diff] [blame] | 7623 | } |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 7624 | } |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 7625 | |
| 7626 | void CGOpenMPRuntime::scanForTargetRegionsFunctions(const Stmt *S, |
| 7627 | StringRef ParentName) { |
| 7628 | if (!S) |
| 7629 | return; |
| 7630 | |
Arpith Chacko Jacob | 43a8b7b | 2017-01-16 15:26:02 +0000 | [diff] [blame] | 7631 | // Codegen OMP target directives that offload compute to the device. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 7632 | bool RequiresDeviceCodegen = |
Arpith Chacko Jacob | 43a8b7b | 2017-01-16 15:26:02 +0000 | [diff] [blame] | 7633 | isa<OMPExecutableDirective>(S) && |
| 7634 | isOpenMPTargetExecutionDirective( |
| 7635 | cast<OMPExecutableDirective>(S)->getDirectiveKind()); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 7636 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 7637 | if (RequiresDeviceCodegen) { |
| 7638 | const auto &E = *cast<OMPExecutableDirective>(S); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 7639 | unsigned DeviceID; |
| 7640 | unsigned FileID; |
| 7641 | unsigned Line; |
Arpith Chacko Jacob | 43a8b7b | 2017-01-16 15:26:02 +0000 | [diff] [blame] | 7642 | getTargetEntryUniqueInfo(CGM.getContext(), E.getLocStart(), DeviceID, |
Samuel Antao | 2de62b0 | 2016-02-13 23:35:10 +0000 | [diff] [blame] | 7643 | FileID, Line); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 7644 | |
| 7645 | // Is this a target region that should not be emitted as an entry point? If |
| 7646 | // so just signal we are done with this target region. |
Samuel Antao | 2de62b0 | 2016-02-13 23:35:10 +0000 | [diff] [blame] | 7647 | if (!OffloadEntriesInfoManager.hasTargetRegionEntryInfo(DeviceID, FileID, |
| 7648 | ParentName, Line)) |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 7649 | return; |
| 7650 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 7651 | switch (E.getDirectiveKind()) { |
| 7652 | case OMPD_target: |
| 7653 | CodeGenFunction::EmitOMPTargetDeviceFunction(CGM, ParentName, |
| 7654 | cast<OMPTargetDirective>(E)); |
Arpith Chacko Jacob | 43a8b7b | 2017-01-16 15:26:02 +0000 | [diff] [blame] | 7655 | break; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 7656 | case OMPD_target_parallel: |
Arpith Chacko Jacob | 19b911c | 2017-01-18 18:18:53 +0000 | [diff] [blame] | 7657 | CodeGenFunction::EmitOMPTargetParallelDeviceFunction( |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 7658 | CGM, ParentName, cast<OMPTargetParallelDirective>(E)); |
Arpith Chacko Jacob | 19b911c | 2017-01-18 18:18:53 +0000 | [diff] [blame] | 7659 | break; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 7660 | case OMPD_target_teams: |
Arpith Chacko Jacob | 99a1e0e | 2017-01-25 02:18:43 +0000 | [diff] [blame] | 7661 | CodeGenFunction::EmitOMPTargetTeamsDeviceFunction( |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 7662 | CGM, ParentName, cast<OMPTargetTeamsDirective>(E)); |
Arpith Chacko Jacob | 99a1e0e | 2017-01-25 02:18:43 +0000 | [diff] [blame] | 7663 | break; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 7664 | case OMPD_target_teams_distribute: |
Alexey Bataev | dfa430f | 2017-12-08 15:03:50 +0000 | [diff] [blame] | 7665 | CodeGenFunction::EmitOMPTargetTeamsDistributeDeviceFunction( |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 7666 | CGM, ParentName, cast<OMPTargetTeamsDistributeDirective>(E)); |
Alexey Bataev | dfa430f | 2017-12-08 15:03:50 +0000 | [diff] [blame] | 7667 | break; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 7668 | case OMPD_target_teams_distribute_simd: |
Alexey Bataev | fbe17fb | 2017-12-13 19:45:06 +0000 | [diff] [blame] | 7669 | CodeGenFunction::EmitOMPTargetTeamsDistributeSimdDeviceFunction( |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 7670 | CGM, ParentName, cast<OMPTargetTeamsDistributeSimdDirective>(E)); |
Alexey Bataev | fbe17fb | 2017-12-13 19:45:06 +0000 | [diff] [blame] | 7671 | break; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 7672 | case OMPD_target_parallel_for: |
Alexey Bataev | fb0ebec | 2017-11-08 20:16:14 +0000 | [diff] [blame] | 7673 | CodeGenFunction::EmitOMPTargetParallelForDeviceFunction( |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 7674 | CGM, ParentName, cast<OMPTargetParallelForDirective>(E)); |
Alexey Bataev | fb0ebec | 2017-11-08 20:16:14 +0000 | [diff] [blame] | 7675 | break; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 7676 | case OMPD_target_parallel_for_simd: |
Alexey Bataev | 5d7edca | 2017-11-09 17:32:15 +0000 | [diff] [blame] | 7677 | CodeGenFunction::EmitOMPTargetParallelForSimdDeviceFunction( |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 7678 | CGM, ParentName, cast<OMPTargetParallelForSimdDirective>(E)); |
Alexey Bataev | 5d7edca | 2017-11-09 17:32:15 +0000 | [diff] [blame] | 7679 | break; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 7680 | case OMPD_target_simd: |
Alexey Bataev | f836537 | 2017-11-17 17:57:25 +0000 | [diff] [blame] | 7681 | CodeGenFunction::EmitOMPTargetSimdDeviceFunction( |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 7682 | CGM, ParentName, cast<OMPTargetSimdDirective>(E)); |
Alexey Bataev | f836537 | 2017-11-17 17:57:25 +0000 | [diff] [blame] | 7683 | break; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 7684 | case OMPD_target_teams_distribute_parallel_for: |
Carlo Bertolli | 52978c3 | 2018-01-03 21:12:44 +0000 | [diff] [blame] | 7685 | CodeGenFunction::EmitOMPTargetTeamsDistributeParallelForDeviceFunction( |
| 7686 | CGM, ParentName, |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 7687 | cast<OMPTargetTeamsDistributeParallelForDirective>(E)); |
Carlo Bertolli | 52978c3 | 2018-01-03 21:12:44 +0000 | [diff] [blame] | 7688 | break; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 7689 | case OMPD_target_teams_distribute_parallel_for_simd: |
Alexey Bataev | 647dd84 | 2018-01-15 20:59:40 +0000 | [diff] [blame] | 7690 | CodeGenFunction:: |
| 7691 | EmitOMPTargetTeamsDistributeParallelForSimdDeviceFunction( |
| 7692 | CGM, ParentName, |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 7693 | cast<OMPTargetTeamsDistributeParallelForSimdDirective>(E)); |
Alexey Bataev | 647dd84 | 2018-01-15 20:59:40 +0000 | [diff] [blame] | 7694 | break; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 7695 | case OMPD_parallel: |
| 7696 | case OMPD_for: |
| 7697 | case OMPD_parallel_for: |
| 7698 | case OMPD_parallel_sections: |
| 7699 | case OMPD_for_simd: |
| 7700 | case OMPD_parallel_for_simd: |
| 7701 | case OMPD_cancel: |
| 7702 | case OMPD_cancellation_point: |
| 7703 | case OMPD_ordered: |
| 7704 | case OMPD_threadprivate: |
| 7705 | case OMPD_task: |
| 7706 | case OMPD_simd: |
| 7707 | case OMPD_sections: |
| 7708 | case OMPD_section: |
| 7709 | case OMPD_single: |
| 7710 | case OMPD_master: |
| 7711 | case OMPD_critical: |
| 7712 | case OMPD_taskyield: |
| 7713 | case OMPD_barrier: |
| 7714 | case OMPD_taskwait: |
| 7715 | case OMPD_taskgroup: |
| 7716 | case OMPD_atomic: |
| 7717 | case OMPD_flush: |
| 7718 | case OMPD_teams: |
| 7719 | case OMPD_target_data: |
| 7720 | case OMPD_target_exit_data: |
| 7721 | case OMPD_target_enter_data: |
| 7722 | case OMPD_distribute: |
| 7723 | case OMPD_distribute_simd: |
| 7724 | case OMPD_distribute_parallel_for: |
| 7725 | case OMPD_distribute_parallel_for_simd: |
| 7726 | case OMPD_teams_distribute: |
| 7727 | case OMPD_teams_distribute_simd: |
| 7728 | case OMPD_teams_distribute_parallel_for: |
| 7729 | case OMPD_teams_distribute_parallel_for_simd: |
| 7730 | case OMPD_target_update: |
| 7731 | case OMPD_declare_simd: |
| 7732 | case OMPD_declare_target: |
| 7733 | case OMPD_end_declare_target: |
| 7734 | case OMPD_declare_reduction: |
| 7735 | case OMPD_taskloop: |
| 7736 | case OMPD_taskloop_simd: |
| 7737 | case OMPD_unknown: |
Arpith Chacko Jacob | 43a8b7b | 2017-01-16 15:26:02 +0000 | [diff] [blame] | 7738 | llvm_unreachable("Unknown target directive for OpenMP device codegen."); |
| 7739 | } |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 7740 | return; |
| 7741 | } |
| 7742 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 7743 | if (const auto *E = dyn_cast<OMPExecutableDirective>(S)) { |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 7744 | if (!E->hasAssociatedStmt() || !E->getAssociatedStmt()) |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 7745 | return; |
| 7746 | |
| 7747 | scanForTargetRegionsFunctions( |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 7748 | E->getInnermostCapturedStmt()->getCapturedStmt(), ParentName); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 7749 | return; |
| 7750 | } |
| 7751 | |
| 7752 | // If this is a lambda function, look into its body. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 7753 | if (const auto *L = dyn_cast<LambdaExpr>(S)) |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 7754 | S = L->getBody(); |
| 7755 | |
| 7756 | // Keep looking for target regions recursively. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 7757 | for (const Stmt *II : S->children()) |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 7758 | scanForTargetRegionsFunctions(II, ParentName); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 7759 | } |
| 7760 | |
| 7761 | bool CGOpenMPRuntime::emitTargetFunctions(GlobalDecl GD) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 7762 | const auto *FD = cast<FunctionDecl>(GD.getDecl()); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 7763 | |
| 7764 | // If emitting code for the host, we do not process FD here. Instead we do |
| 7765 | // the normal code generation. |
| 7766 | if (!CGM.getLangOpts().OpenMPIsDevice) |
| 7767 | return false; |
| 7768 | |
| 7769 | // Try to detect target regions in the function. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 7770 | scanForTargetRegionsFunctions(FD->getBody(), CGM.getMangledName(GD)); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 7771 | |
Alexey Bataev | 4f4bf7c | 2018-03-15 15:47:20 +0000 | [diff] [blame] | 7772 | // Do not to emit function if it is not marked as declare target. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 7773 | return !isDeclareTargetDeclaration(FD); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 7774 | } |
| 7775 | |
| 7776 | bool CGOpenMPRuntime::emitTargetGlobalVariable(GlobalDecl GD) { |
| 7777 | if (!CGM.getLangOpts().OpenMPIsDevice) |
| 7778 | return false; |
| 7779 | |
| 7780 | // Check if there are Ctors/Dtors in this declaration and look for target |
| 7781 | // regions in it. We use the complete variant to produce the kernel name |
| 7782 | // mangling. |
| 7783 | QualType RDTy = cast<VarDecl>(GD.getDecl())->getType(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 7784 | if (const auto *RD = RDTy->getBaseElementTypeUnsafe()->getAsCXXRecordDecl()) { |
| 7785 | for (const CXXConstructorDecl *Ctor : RD->ctors()) { |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 7786 | StringRef ParentName = |
| 7787 | CGM.getMangledName(GlobalDecl(Ctor, Ctor_Complete)); |
| 7788 | scanForTargetRegionsFunctions(Ctor->getBody(), ParentName); |
| 7789 | } |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 7790 | if (const CXXDestructorDecl *Dtor = RD->getDestructor()) { |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 7791 | StringRef ParentName = |
| 7792 | CGM.getMangledName(GlobalDecl(Dtor, Dtor_Complete)); |
| 7793 | scanForTargetRegionsFunctions(Dtor->getBody(), ParentName); |
| 7794 | } |
| 7795 | } |
| 7796 | |
Alexey Bataev | 4f4bf7c | 2018-03-15 15:47:20 +0000 | [diff] [blame] | 7797 | // 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] | 7798 | llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res = |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 7799 | isDeclareTargetDeclaration(cast<VarDecl>(GD.getDecl())); |
Alexey Bataev | 92327c5 | 2018-03-26 16:40:55 +0000 | [diff] [blame] | 7800 | return !Res || *Res == OMPDeclareTargetDeclAttr::MT_Link; |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 7801 | } |
| 7802 | |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 7803 | void CGOpenMPRuntime::registerTargetGlobalVariable(const VarDecl *VD, |
| 7804 | llvm::Constant *Addr) { |
| 7805 | if (llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res = |
| 7806 | isDeclareTargetDeclaration(VD)) { |
| 7807 | OffloadEntriesInfoManagerTy::OMPTargetGlobalVarEntryKind Flags; |
| 7808 | StringRef VarName; |
| 7809 | CharUnits VarSize; |
| 7810 | llvm::GlobalValue::LinkageTypes Linkage; |
| 7811 | switch (*Res) { |
| 7812 | case OMPDeclareTargetDeclAttr::MT_To: |
| 7813 | Flags = OffloadEntriesInfoManagerTy::OMPTargetGlobalVarEntryTo; |
| 7814 | VarName = CGM.getMangledName(VD); |
| 7815 | VarSize = CGM.getContext().getTypeSizeInChars(VD->getType()); |
| 7816 | Linkage = CGM.getLLVMLinkageVarDefinition(VD, /*IsConstant=*/false); |
| 7817 | break; |
| 7818 | case OMPDeclareTargetDeclAttr::MT_Link: |
| 7819 | // Map type 'to' because we do not map the original variable but the |
| 7820 | // reference. |
| 7821 | Flags = OffloadEntriesInfoManagerTy::OMPTargetGlobalVarEntryTo; |
| 7822 | if (!CGM.getLangOpts().OpenMPIsDevice) { |
| 7823 | Addr = |
| 7824 | cast<llvm::Constant>(getAddrOfDeclareTargetLink(VD).getPointer()); |
| 7825 | } |
| 7826 | VarName = Addr->getName(); |
| 7827 | VarSize = CGM.getPointerSize(); |
| 7828 | Linkage = llvm::GlobalValue::WeakAnyLinkage; |
| 7829 | break; |
| 7830 | } |
| 7831 | OffloadEntriesInfoManager.registerDeviceGlobalVarEntryInfo( |
| 7832 | VarName, Addr, VarSize, Flags, Linkage); |
| 7833 | } |
| 7834 | } |
| 7835 | |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 7836 | bool CGOpenMPRuntime::emitTargetGlobal(GlobalDecl GD) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 7837 | if (isa<FunctionDecl>(GD.getDecl())) |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 7838 | return emitTargetFunctions(GD); |
| 7839 | |
| 7840 | return emitTargetGlobalVariable(GD); |
| 7841 | } |
| 7842 | |
Alexey Bataev | 4f4bf7c | 2018-03-15 15:47:20 +0000 | [diff] [blame] | 7843 | CGOpenMPRuntime::DisableAutoDeclareTargetRAII::DisableAutoDeclareTargetRAII( |
| 7844 | CodeGenModule &CGM) |
| 7845 | : CGM(CGM) { |
| 7846 | if (CGM.getLangOpts().OpenMPIsDevice) { |
| 7847 | SavedShouldMarkAsGlobal = CGM.getOpenMPRuntime().ShouldMarkAsGlobal; |
| 7848 | CGM.getOpenMPRuntime().ShouldMarkAsGlobal = false; |
| 7849 | } |
| 7850 | } |
| 7851 | |
| 7852 | CGOpenMPRuntime::DisableAutoDeclareTargetRAII::~DisableAutoDeclareTargetRAII() { |
| 7853 | if (CGM.getLangOpts().OpenMPIsDevice) |
| 7854 | CGM.getOpenMPRuntime().ShouldMarkAsGlobal = SavedShouldMarkAsGlobal; |
| 7855 | } |
| 7856 | |
| 7857 | bool CGOpenMPRuntime::markAsGlobalTarget(const FunctionDecl *D) { |
| 7858 | if (!CGM.getLangOpts().OpenMPIsDevice || !ShouldMarkAsGlobal) |
| 7859 | return true; |
Alexey Bataev | 4f4bf7c | 2018-03-15 15:47:20 +0000 | [diff] [blame] | 7860 | |
| 7861 | const FunctionDecl *FD = D->getCanonicalDecl(); |
Alexey Bataev | 34f8a70 | 2018-03-28 14:28:54 +0000 | [diff] [blame] | 7862 | // Do not to emit function if it is marked as declare target as it was already |
| 7863 | // emitted. |
| 7864 | if (isDeclareTargetDeclaration(D)) { |
| 7865 | if (D->hasBody() && AlreadyEmittedTargetFunctions.count(FD) == 0) { |
| 7866 | if (auto *F = dyn_cast_or_null<llvm::Function>( |
| 7867 | CGM.GetGlobalValue(CGM.getMangledName(D)))) |
| 7868 | return !F->isDeclaration(); |
| 7869 | return false; |
| 7870 | } |
| 7871 | return true; |
| 7872 | } |
| 7873 | |
Alexey Bataev | 4f4bf7c | 2018-03-15 15:47:20 +0000 | [diff] [blame] | 7874 | // Do not mark member functions except for static. |
| 7875 | if (const auto *Method = dyn_cast<CXXMethodDecl>(FD)) |
| 7876 | if (!Method->isStatic()) |
| 7877 | return true; |
| 7878 | |
| 7879 | return !AlreadyEmittedTargetFunctions.insert(FD).second; |
| 7880 | } |
| 7881 | |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 7882 | llvm::Function *CGOpenMPRuntime::emitRegistrationFunction() { |
| 7883 | // If we have offloading in the current module, we need to emit the entries |
| 7884 | // now and register the offloading descriptor. |
| 7885 | createOffloadEntriesAndInfoMetadata(); |
| 7886 | |
| 7887 | // Create and register the offloading binary descriptors. This is the main |
| 7888 | // entity that captures all the information about offloading in the current |
| 7889 | // compilation unit. |
| 7890 | return createOffloadingBinaryDescriptorRegistration(); |
| 7891 | } |
Carlo Bertolli | 430d8ec | 2016-03-03 20:34:23 +0000 | [diff] [blame] | 7892 | |
| 7893 | void CGOpenMPRuntime::emitTeamsCall(CodeGenFunction &CGF, |
| 7894 | const OMPExecutableDirective &D, |
| 7895 | SourceLocation Loc, |
| 7896 | llvm::Value *OutlinedFn, |
| 7897 | ArrayRef<llvm::Value *> CapturedVars) { |
| 7898 | if (!CGF.HaveInsertPoint()) |
| 7899 | return; |
| 7900 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 7901 | llvm::Value *RTLoc = emitUpdateLocation(CGF, Loc); |
Carlo Bertolli | 430d8ec | 2016-03-03 20:34:23 +0000 | [diff] [blame] | 7902 | CodeGenFunction::RunCleanupsScope Scope(CGF); |
| 7903 | |
| 7904 | // Build call __kmpc_fork_teams(loc, n, microtask, var1, .., varn); |
| 7905 | llvm::Value *Args[] = { |
| 7906 | RTLoc, |
| 7907 | CGF.Builder.getInt32(CapturedVars.size()), // Number of captured vars |
| 7908 | CGF.Builder.CreateBitCast(OutlinedFn, getKmpc_MicroPointerTy())}; |
| 7909 | llvm::SmallVector<llvm::Value *, 16> RealArgs; |
| 7910 | RealArgs.append(std::begin(Args), std::end(Args)); |
| 7911 | RealArgs.append(CapturedVars.begin(), CapturedVars.end()); |
| 7912 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 7913 | llvm::Value *RTLFn = createRuntimeFunction(OMPRTL__kmpc_fork_teams); |
Carlo Bertolli | 430d8ec | 2016-03-03 20:34:23 +0000 | [diff] [blame] | 7914 | CGF.EmitRuntimeCall(RTLFn, RealArgs); |
| 7915 | } |
| 7916 | |
| 7917 | void CGOpenMPRuntime::emitNumTeamsClause(CodeGenFunction &CGF, |
Carlo Bertolli | c687225 | 2016-04-04 15:55:02 +0000 | [diff] [blame] | 7918 | const Expr *NumTeams, |
| 7919 | const Expr *ThreadLimit, |
Carlo Bertolli | 430d8ec | 2016-03-03 20:34:23 +0000 | [diff] [blame] | 7920 | SourceLocation Loc) { |
| 7921 | if (!CGF.HaveInsertPoint()) |
| 7922 | return; |
| 7923 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 7924 | llvm::Value *RTLoc = emitUpdateLocation(CGF, Loc); |
Carlo Bertolli | 430d8ec | 2016-03-03 20:34:23 +0000 | [diff] [blame] | 7925 | |
Carlo Bertolli | c687225 | 2016-04-04 15:55:02 +0000 | [diff] [blame] | 7926 | llvm::Value *NumTeamsVal = |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 7927 | NumTeams |
Carlo Bertolli | c687225 | 2016-04-04 15:55:02 +0000 | [diff] [blame] | 7928 | ? CGF.Builder.CreateIntCast(CGF.EmitScalarExpr(NumTeams), |
| 7929 | CGF.CGM.Int32Ty, /* isSigned = */ true) |
| 7930 | : CGF.Builder.getInt32(0); |
| 7931 | |
| 7932 | llvm::Value *ThreadLimitVal = |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 7933 | ThreadLimit |
Carlo Bertolli | c687225 | 2016-04-04 15:55:02 +0000 | [diff] [blame] | 7934 | ? CGF.Builder.CreateIntCast(CGF.EmitScalarExpr(ThreadLimit), |
| 7935 | CGF.CGM.Int32Ty, /* isSigned = */ true) |
| 7936 | : CGF.Builder.getInt32(0); |
| 7937 | |
Carlo Bertolli | 430d8ec | 2016-03-03 20:34:23 +0000 | [diff] [blame] | 7938 | // 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] | 7939 | llvm::Value *PushNumTeamsArgs[] = {RTLoc, getThreadID(CGF, Loc), NumTeamsVal, |
| 7940 | ThreadLimitVal}; |
Carlo Bertolli | 430d8ec | 2016-03-03 20:34:23 +0000 | [diff] [blame] | 7941 | CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_push_num_teams), |
| 7942 | PushNumTeamsArgs); |
| 7943 | } |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 7944 | |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7945 | void CGOpenMPRuntime::emitTargetDataCalls( |
| 7946 | CodeGenFunction &CGF, const OMPExecutableDirective &D, const Expr *IfCond, |
| 7947 | const Expr *Device, const RegionCodeGenTy &CodeGen, TargetDataInfo &Info) { |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 7948 | if (!CGF.HaveInsertPoint()) |
| 7949 | return; |
| 7950 | |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7951 | // Action used to replace the default codegen action and turn privatization |
| 7952 | // off. |
| 7953 | PrePostActionTy NoPrivAction; |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 7954 | |
| 7955 | // Generate the code for the opening of the data environment. Capture all the |
| 7956 | // arguments of the runtime call by reference because they are used in the |
| 7957 | // closing of the region. |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 7958 | auto &&BeginThenGen = [this, &D, Device, &Info, |
| 7959 | &CodeGen](CodeGenFunction &CGF, PrePostActionTy &) { |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 7960 | // Fill up the arrays with all the mapped variables. |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7961 | MappableExprsHandler::MapBaseValuesArrayTy BasePointers; |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 7962 | MappableExprsHandler::MapValuesArrayTy Pointers; |
| 7963 | MappableExprsHandler::MapValuesArrayTy Sizes; |
| 7964 | MappableExprsHandler::MapFlagsArrayTy MapTypes; |
| 7965 | |
| 7966 | // Get map clause information. |
| 7967 | MappableExprsHandler MCHandler(D, CGF); |
| 7968 | MCHandler.generateAllInfo(BasePointers, Pointers, Sizes, MapTypes); |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 7969 | |
| 7970 | // Fill up the arrays and create the arguments. |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7971 | emitOffloadingArrays(CGF, BasePointers, Pointers, Sizes, MapTypes, Info); |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 7972 | |
| 7973 | llvm::Value *BasePointersArrayArg = nullptr; |
| 7974 | llvm::Value *PointersArrayArg = nullptr; |
| 7975 | llvm::Value *SizesArrayArg = nullptr; |
| 7976 | llvm::Value *MapTypesArrayArg = nullptr; |
| 7977 | emitOffloadingArraysArgument(CGF, BasePointersArrayArg, PointersArrayArg, |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7978 | SizesArrayArg, MapTypesArrayArg, Info); |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 7979 | |
| 7980 | // Emit device ID if any. |
| 7981 | llvm::Value *DeviceID = nullptr; |
George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 7982 | if (Device) { |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 7983 | DeviceID = CGF.Builder.CreateIntCast(CGF.EmitScalarExpr(Device), |
George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 7984 | CGF.Int64Ty, /*isSigned=*/true); |
| 7985 | } else { |
| 7986 | DeviceID = CGF.Builder.getInt64(OMP_DEVICEID_UNDEF); |
| 7987 | } |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 7988 | |
| 7989 | // Emit the number of elements in the offloading arrays. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 7990 | llvm::Value *PointerNum = CGF.Builder.getInt32(Info.NumberOfPtrs); |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 7991 | |
| 7992 | llvm::Value *OffloadingArgs[] = { |
| 7993 | DeviceID, PointerNum, BasePointersArrayArg, |
| 7994 | PointersArrayArg, SizesArrayArg, MapTypesArrayArg}; |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 7995 | CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__tgt_target_data_begin), |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 7996 | OffloadingArgs); |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7997 | |
| 7998 | // If device pointer privatization is required, emit the body of the region |
| 7999 | // here. It will have to be duplicated: with and without privatization. |
| 8000 | if (!Info.CaptureDeviceAddrMap.empty()) |
| 8001 | CodeGen(CGF); |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8002 | }; |
| 8003 | |
| 8004 | // Generate code for the closing of the data region. |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 8005 | auto &&EndThenGen = [this, Device, &Info](CodeGenFunction &CGF, |
| 8006 | PrePostActionTy &) { |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 8007 | assert(Info.isValid() && "Invalid data environment closing arguments."); |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8008 | |
| 8009 | llvm::Value *BasePointersArrayArg = nullptr; |
| 8010 | llvm::Value *PointersArrayArg = nullptr; |
| 8011 | llvm::Value *SizesArrayArg = nullptr; |
| 8012 | llvm::Value *MapTypesArrayArg = nullptr; |
| 8013 | emitOffloadingArraysArgument(CGF, BasePointersArrayArg, PointersArrayArg, |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 8014 | SizesArrayArg, MapTypesArrayArg, Info); |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8015 | |
| 8016 | // Emit device ID if any. |
| 8017 | llvm::Value *DeviceID = nullptr; |
George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 8018 | if (Device) { |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8019 | DeviceID = CGF.Builder.CreateIntCast(CGF.EmitScalarExpr(Device), |
George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 8020 | CGF.Int64Ty, /*isSigned=*/true); |
| 8021 | } else { |
| 8022 | DeviceID = CGF.Builder.getInt64(OMP_DEVICEID_UNDEF); |
| 8023 | } |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8024 | |
| 8025 | // Emit the number of elements in the offloading arrays. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 8026 | llvm::Value *PointerNum = CGF.Builder.getInt32(Info.NumberOfPtrs); |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8027 | |
| 8028 | llvm::Value *OffloadingArgs[] = { |
| 8029 | DeviceID, PointerNum, BasePointersArrayArg, |
| 8030 | PointersArrayArg, SizesArrayArg, MapTypesArrayArg}; |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 8031 | CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__tgt_target_data_end), |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8032 | OffloadingArgs); |
| 8033 | }; |
| 8034 | |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 8035 | // If we need device pointer privatization, we need to emit the body of the |
| 8036 | // region with no privatization in the 'else' branch of the conditional. |
| 8037 | // Otherwise, we don't have to do anything. |
| 8038 | auto &&BeginElseGen = [&Info, &CodeGen, &NoPrivAction](CodeGenFunction &CGF, |
| 8039 | PrePostActionTy &) { |
| 8040 | if (!Info.CaptureDeviceAddrMap.empty()) { |
| 8041 | CodeGen.setAction(NoPrivAction); |
| 8042 | CodeGen(CGF); |
| 8043 | } |
| 8044 | }; |
| 8045 | |
| 8046 | // We don't have to do anything to close the region if the if clause evaluates |
| 8047 | // to false. |
| 8048 | auto &&EndElseGen = [](CodeGenFunction &CGF, PrePostActionTy &) {}; |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8049 | |
| 8050 | if (IfCond) { |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 8051 | emitOMPIfClause(CGF, IfCond, BeginThenGen, BeginElseGen); |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8052 | } else { |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 8053 | RegionCodeGenTy RCG(BeginThenGen); |
| 8054 | RCG(CGF); |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8055 | } |
| 8056 | |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 8057 | // If we don't require privatization of device pointers, we emit the body in |
| 8058 | // between the runtime calls. This avoids duplicating the body code. |
| 8059 | if (Info.CaptureDeviceAddrMap.empty()) { |
| 8060 | CodeGen.setAction(NoPrivAction); |
| 8061 | CodeGen(CGF); |
| 8062 | } |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8063 | |
| 8064 | if (IfCond) { |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 8065 | emitOMPIfClause(CGF, IfCond, EndThenGen, EndElseGen); |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8066 | } else { |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 8067 | RegionCodeGenTy RCG(EndThenGen); |
| 8068 | RCG(CGF); |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8069 | } |
| 8070 | } |
Samuel Antao | bd0ae2e | 2016-04-27 23:07:29 +0000 | [diff] [blame] | 8071 | |
Samuel Antao | 8d2d730 | 2016-05-26 18:30:22 +0000 | [diff] [blame] | 8072 | void CGOpenMPRuntime::emitTargetDataStandAloneCall( |
Samuel Antao | 8dd6628 | 2016-04-27 23:14:30 +0000 | [diff] [blame] | 8073 | CodeGenFunction &CGF, const OMPExecutableDirective &D, const Expr *IfCond, |
| 8074 | const Expr *Device) { |
Samuel Antao | bd0ae2e | 2016-04-27 23:07:29 +0000 | [diff] [blame] | 8075 | if (!CGF.HaveInsertPoint()) |
| 8076 | return; |
| 8077 | |
Samuel Antao | 8dd6628 | 2016-04-27 23:14:30 +0000 | [diff] [blame] | 8078 | assert((isa<OMPTargetEnterDataDirective>(D) || |
Samuel Antao | 8d2d730 | 2016-05-26 18:30:22 +0000 | [diff] [blame] | 8079 | isa<OMPTargetExitDataDirective>(D) || |
| 8080 | isa<OMPTargetUpdateDirective>(D)) && |
| 8081 | "Expecting either target enter, exit data, or update directives."); |
Samuel Antao | 8dd6628 | 2016-04-27 23:14:30 +0000 | [diff] [blame] | 8082 | |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 8083 | CodeGenFunction::OMPTargetDataInfo InputInfo; |
| 8084 | llvm::Value *MapTypesArray = nullptr; |
Samuel Antao | bd0ae2e | 2016-04-27 23:07:29 +0000 | [diff] [blame] | 8085 | // Generate the code for the opening of the data environment. |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 8086 | auto &&ThenGen = [this, &D, Device, &InputInfo, |
| 8087 | &MapTypesArray](CodeGenFunction &CGF, PrePostActionTy &) { |
Samuel Antao | bd0ae2e | 2016-04-27 23:07:29 +0000 | [diff] [blame] | 8088 | // Emit device ID if any. |
| 8089 | llvm::Value *DeviceID = nullptr; |
George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 8090 | if (Device) { |
Samuel Antao | bd0ae2e | 2016-04-27 23:07:29 +0000 | [diff] [blame] | 8091 | DeviceID = CGF.Builder.CreateIntCast(CGF.EmitScalarExpr(Device), |
George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 8092 | CGF.Int64Ty, /*isSigned=*/true); |
| 8093 | } else { |
| 8094 | DeviceID = CGF.Builder.getInt64(OMP_DEVICEID_UNDEF); |
| 8095 | } |
Samuel Antao | bd0ae2e | 2016-04-27 23:07:29 +0000 | [diff] [blame] | 8096 | |
| 8097 | // Emit the number of elements in the offloading arrays. |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 8098 | llvm::Constant *PointerNum = |
| 8099 | CGF.Builder.getInt32(InputInfo.NumberOfTargetItems); |
Samuel Antao | bd0ae2e | 2016-04-27 23:07:29 +0000 | [diff] [blame] | 8100 | |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 8101 | llvm::Value *OffloadingArgs[] = {DeviceID, |
| 8102 | PointerNum, |
| 8103 | InputInfo.BasePointersArray.getPointer(), |
| 8104 | InputInfo.PointersArray.getPointer(), |
| 8105 | InputInfo.SizesArray.getPointer(), |
| 8106 | MapTypesArray}; |
Samuel Antao | 8d2d730 | 2016-05-26 18:30:22 +0000 | [diff] [blame] | 8107 | |
Samuel Antao | 8d2d730 | 2016-05-26 18:30:22 +0000 | [diff] [blame] | 8108 | // Select the right runtime function call for each expected standalone |
| 8109 | // directive. |
Alexey Bataev | 0cc6b8e | 2017-12-14 17:00:17 +0000 | [diff] [blame] | 8110 | const bool HasNowait = D.hasClausesOfKind<OMPNowaitClause>(); |
Samuel Antao | 8d2d730 | 2016-05-26 18:30:22 +0000 | [diff] [blame] | 8111 | OpenMPRTLFunction RTLFn; |
| 8112 | switch (D.getDirectiveKind()) { |
Samuel Antao | 8d2d730 | 2016-05-26 18:30:22 +0000 | [diff] [blame] | 8113 | case OMPD_target_enter_data: |
Alexey Bataev | 0cc6b8e | 2017-12-14 17:00:17 +0000 | [diff] [blame] | 8114 | RTLFn = HasNowait ? OMPRTL__tgt_target_data_begin_nowait |
| 8115 | : OMPRTL__tgt_target_data_begin; |
Samuel Antao | 8d2d730 | 2016-05-26 18:30:22 +0000 | [diff] [blame] | 8116 | break; |
| 8117 | case OMPD_target_exit_data: |
Alexey Bataev | 0cc6b8e | 2017-12-14 17:00:17 +0000 | [diff] [blame] | 8118 | RTLFn = HasNowait ? OMPRTL__tgt_target_data_end_nowait |
| 8119 | : OMPRTL__tgt_target_data_end; |
Samuel Antao | 8d2d730 | 2016-05-26 18:30:22 +0000 | [diff] [blame] | 8120 | break; |
| 8121 | case OMPD_target_update: |
Alexey Bataev | 0cc6b8e | 2017-12-14 17:00:17 +0000 | [diff] [blame] | 8122 | RTLFn = HasNowait ? OMPRTL__tgt_target_data_update_nowait |
| 8123 | : OMPRTL__tgt_target_data_update; |
Samuel Antao | 8d2d730 | 2016-05-26 18:30:22 +0000 | [diff] [blame] | 8124 | break; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 8125 | case OMPD_parallel: |
| 8126 | case OMPD_for: |
| 8127 | case OMPD_parallel_for: |
| 8128 | case OMPD_parallel_sections: |
| 8129 | case OMPD_for_simd: |
| 8130 | case OMPD_parallel_for_simd: |
| 8131 | case OMPD_cancel: |
| 8132 | case OMPD_cancellation_point: |
| 8133 | case OMPD_ordered: |
| 8134 | case OMPD_threadprivate: |
| 8135 | case OMPD_task: |
| 8136 | case OMPD_simd: |
| 8137 | case OMPD_sections: |
| 8138 | case OMPD_section: |
| 8139 | case OMPD_single: |
| 8140 | case OMPD_master: |
| 8141 | case OMPD_critical: |
| 8142 | case OMPD_taskyield: |
| 8143 | case OMPD_barrier: |
| 8144 | case OMPD_taskwait: |
| 8145 | case OMPD_taskgroup: |
| 8146 | case OMPD_atomic: |
| 8147 | case OMPD_flush: |
| 8148 | case OMPD_teams: |
| 8149 | case OMPD_target_data: |
| 8150 | case OMPD_distribute: |
| 8151 | case OMPD_distribute_simd: |
| 8152 | case OMPD_distribute_parallel_for: |
| 8153 | case OMPD_distribute_parallel_for_simd: |
| 8154 | case OMPD_teams_distribute: |
| 8155 | case OMPD_teams_distribute_simd: |
| 8156 | case OMPD_teams_distribute_parallel_for: |
| 8157 | case OMPD_teams_distribute_parallel_for_simd: |
| 8158 | case OMPD_declare_simd: |
| 8159 | case OMPD_declare_target: |
| 8160 | case OMPD_end_declare_target: |
| 8161 | case OMPD_declare_reduction: |
| 8162 | case OMPD_taskloop: |
| 8163 | case OMPD_taskloop_simd: |
| 8164 | case OMPD_target: |
| 8165 | case OMPD_target_simd: |
| 8166 | case OMPD_target_teams_distribute: |
| 8167 | case OMPD_target_teams_distribute_simd: |
| 8168 | case OMPD_target_teams_distribute_parallel_for: |
| 8169 | case OMPD_target_teams_distribute_parallel_for_simd: |
| 8170 | case OMPD_target_teams: |
| 8171 | case OMPD_target_parallel: |
| 8172 | case OMPD_target_parallel_for: |
| 8173 | case OMPD_target_parallel_for_simd: |
| 8174 | case OMPD_unknown: |
| 8175 | llvm_unreachable("Unexpected standalone target data directive."); |
| 8176 | break; |
Samuel Antao | 8d2d730 | 2016-05-26 18:30:22 +0000 | [diff] [blame] | 8177 | } |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 8178 | CGF.EmitRuntimeCall(createRuntimeFunction(RTLFn), OffloadingArgs); |
Samuel Antao | bd0ae2e | 2016-04-27 23:07:29 +0000 | [diff] [blame] | 8179 | }; |
| 8180 | |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 8181 | auto &&TargetThenGen = [this, &ThenGen, &D, &InputInfo, &MapTypesArray]( |
| 8182 | CodeGenFunction &CGF, PrePostActionTy &) { |
| 8183 | // Fill up the arrays with all the mapped variables. |
| 8184 | MappableExprsHandler::MapBaseValuesArrayTy BasePointers; |
| 8185 | MappableExprsHandler::MapValuesArrayTy Pointers; |
| 8186 | MappableExprsHandler::MapValuesArrayTy Sizes; |
| 8187 | MappableExprsHandler::MapFlagsArrayTy MapTypes; |
Samuel Antao | bd0ae2e | 2016-04-27 23:07:29 +0000 | [diff] [blame] | 8188 | |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 8189 | // Get map clause information. |
| 8190 | MappableExprsHandler MEHandler(D, CGF); |
| 8191 | MEHandler.generateAllInfo(BasePointers, Pointers, Sizes, MapTypes); |
| 8192 | |
| 8193 | TargetDataInfo Info; |
| 8194 | // Fill up the arrays and create the arguments. |
| 8195 | emitOffloadingArrays(CGF, BasePointers, Pointers, Sizes, MapTypes, Info); |
| 8196 | emitOffloadingArraysArgument(CGF, Info.BasePointersArray, |
| 8197 | Info.PointersArray, Info.SizesArray, |
| 8198 | Info.MapTypesArray, Info); |
| 8199 | InputInfo.NumberOfTargetItems = Info.NumberOfPtrs; |
| 8200 | InputInfo.BasePointersArray = |
| 8201 | Address(Info.BasePointersArray, CGM.getPointerAlign()); |
| 8202 | InputInfo.PointersArray = |
| 8203 | Address(Info.PointersArray, CGM.getPointerAlign()); |
| 8204 | InputInfo.SizesArray = |
| 8205 | Address(Info.SizesArray, CGM.getPointerAlign()); |
| 8206 | MapTypesArray = Info.MapTypesArray; |
| 8207 | if (D.hasClausesOfKind<OMPDependClause>()) |
| 8208 | CGF.EmitOMPTargetTaskBasedDirective(D, ThenGen, InputInfo); |
| 8209 | else |
Alexey Bataev | 768f1f2 | 2018-01-09 19:59:25 +0000 | [diff] [blame] | 8210 | emitInlinedDirective(CGF, D.getDirectiveKind(), ThenGen); |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 8211 | }; |
| 8212 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 8213 | if (IfCond) { |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 8214 | emitOMPIfClause(CGF, IfCond, TargetThenGen, |
| 8215 | [](CodeGenFunction &CGF, PrePostActionTy &) {}); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 8216 | } else { |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 8217 | RegionCodeGenTy ThenRCG(TargetThenGen); |
| 8218 | ThenRCG(CGF); |
Samuel Antao | bd0ae2e | 2016-04-27 23:07:29 +0000 | [diff] [blame] | 8219 | } |
| 8220 | } |
Alexey Bataev | c7a82b4 | 2016-05-06 09:40:08 +0000 | [diff] [blame] | 8221 | |
| 8222 | namespace { |
| 8223 | /// Kind of parameter in a function with 'declare simd' directive. |
| 8224 | enum ParamKindTy { LinearWithVarStride, Linear, Uniform, Vector }; |
| 8225 | /// Attribute set of the parameter. |
| 8226 | struct ParamAttrTy { |
| 8227 | ParamKindTy Kind = Vector; |
| 8228 | llvm::APSInt StrideOrArg; |
| 8229 | llvm::APSInt Alignment; |
| 8230 | }; |
| 8231 | } // namespace |
| 8232 | |
| 8233 | static unsigned evaluateCDTSize(const FunctionDecl *FD, |
| 8234 | ArrayRef<ParamAttrTy> ParamAttrs) { |
| 8235 | // Every vector variant of a SIMD-enabled function has a vector length (VLEN). |
| 8236 | // If OpenMP clause "simdlen" is used, the VLEN is the value of the argument |
| 8237 | // of that clause. The VLEN value must be power of 2. |
| 8238 | // In other case the notion of the function`s "characteristic data type" (CDT) |
| 8239 | // is used to compute the vector length. |
| 8240 | // CDT is defined in the following order: |
| 8241 | // a) For non-void function, the CDT is the return type. |
| 8242 | // b) If the function has any non-uniform, non-linear parameters, then the |
| 8243 | // CDT is the type of the first such parameter. |
| 8244 | // c) If the CDT determined by a) or b) above is struct, union, or class |
| 8245 | // type which is pass-by-value (except for the type that maps to the |
| 8246 | // built-in complex data type), the characteristic data type is int. |
| 8247 | // d) If none of the above three cases is applicable, the CDT is int. |
| 8248 | // The VLEN is then determined based on the CDT and the size of vector |
| 8249 | // register of that ISA for which current vector version is generated. The |
| 8250 | // VLEN is computed using the formula below: |
| 8251 | // VLEN = sizeof(vector_register) / sizeof(CDT), |
| 8252 | // where vector register size specified in section 3.2.1 Registers and the |
| 8253 | // Stack Frame of original AMD64 ABI document. |
| 8254 | QualType RetType = FD->getReturnType(); |
| 8255 | if (RetType.isNull()) |
| 8256 | return 0; |
| 8257 | ASTContext &C = FD->getASTContext(); |
| 8258 | QualType CDT; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 8259 | if (!RetType.isNull() && !RetType->isVoidType()) { |
Alexey Bataev | c7a82b4 | 2016-05-06 09:40:08 +0000 | [diff] [blame] | 8260 | CDT = RetType; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 8261 | } else { |
Alexey Bataev | c7a82b4 | 2016-05-06 09:40:08 +0000 | [diff] [blame] | 8262 | unsigned Offset = 0; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 8263 | if (const auto *MD = dyn_cast<CXXMethodDecl>(FD)) { |
Alexey Bataev | c7a82b4 | 2016-05-06 09:40:08 +0000 | [diff] [blame] | 8264 | if (ParamAttrs[Offset].Kind == Vector) |
| 8265 | CDT = C.getPointerType(C.getRecordType(MD->getParent())); |
| 8266 | ++Offset; |
| 8267 | } |
| 8268 | if (CDT.isNull()) { |
| 8269 | for (unsigned I = 0, E = FD->getNumParams(); I < E; ++I) { |
| 8270 | if (ParamAttrs[I + Offset].Kind == Vector) { |
| 8271 | CDT = FD->getParamDecl(I)->getType(); |
| 8272 | break; |
| 8273 | } |
| 8274 | } |
| 8275 | } |
| 8276 | } |
| 8277 | if (CDT.isNull()) |
| 8278 | CDT = C.IntTy; |
| 8279 | CDT = CDT->getCanonicalTypeUnqualified(); |
| 8280 | if (CDT->isRecordType() || CDT->isUnionType()) |
| 8281 | CDT = C.IntTy; |
| 8282 | return C.getTypeSize(CDT); |
| 8283 | } |
| 8284 | |
| 8285 | static void |
| 8286 | emitX86DeclareSimdFunction(const FunctionDecl *FD, llvm::Function *Fn, |
Benjamin Kramer | 81cb4b7 | 2016-11-24 16:01:20 +0000 | [diff] [blame] | 8287 | const llvm::APSInt &VLENVal, |
Alexey Bataev | c7a82b4 | 2016-05-06 09:40:08 +0000 | [diff] [blame] | 8288 | ArrayRef<ParamAttrTy> ParamAttrs, |
| 8289 | OMPDeclareSimdDeclAttr::BranchStateTy State) { |
| 8290 | struct ISADataTy { |
| 8291 | char ISA; |
| 8292 | unsigned VecRegSize; |
| 8293 | }; |
| 8294 | ISADataTy ISAData[] = { |
| 8295 | { |
| 8296 | 'b', 128 |
| 8297 | }, // SSE |
| 8298 | { |
| 8299 | 'c', 256 |
| 8300 | }, // AVX |
| 8301 | { |
| 8302 | 'd', 256 |
| 8303 | }, // AVX2 |
| 8304 | { |
| 8305 | 'e', 512 |
| 8306 | }, // AVX512 |
| 8307 | }; |
| 8308 | llvm::SmallVector<char, 2> Masked; |
| 8309 | switch (State) { |
| 8310 | case OMPDeclareSimdDeclAttr::BS_Undefined: |
| 8311 | Masked.push_back('N'); |
| 8312 | Masked.push_back('M'); |
| 8313 | break; |
| 8314 | case OMPDeclareSimdDeclAttr::BS_Notinbranch: |
| 8315 | Masked.push_back('N'); |
| 8316 | break; |
| 8317 | case OMPDeclareSimdDeclAttr::BS_Inbranch: |
| 8318 | Masked.push_back('M'); |
| 8319 | break; |
| 8320 | } |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 8321 | for (char Mask : Masked) { |
| 8322 | for (const ISADataTy &Data : ISAData) { |
Alexey Bataev | c7a82b4 | 2016-05-06 09:40:08 +0000 | [diff] [blame] | 8323 | SmallString<256> Buffer; |
| 8324 | llvm::raw_svector_ostream Out(Buffer); |
| 8325 | Out << "_ZGV" << Data.ISA << Mask; |
| 8326 | if (!VLENVal) { |
| 8327 | Out << llvm::APSInt::getUnsigned(Data.VecRegSize / |
| 8328 | evaluateCDTSize(FD, ParamAttrs)); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 8329 | } else { |
Alexey Bataev | c7a82b4 | 2016-05-06 09:40:08 +0000 | [diff] [blame] | 8330 | Out << VLENVal; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 8331 | } |
| 8332 | for (const ParamAttrTy &ParamAttr : ParamAttrs) { |
Alexey Bataev | c7a82b4 | 2016-05-06 09:40:08 +0000 | [diff] [blame] | 8333 | switch (ParamAttr.Kind){ |
| 8334 | case LinearWithVarStride: |
| 8335 | Out << 's' << ParamAttr.StrideOrArg; |
| 8336 | break; |
| 8337 | case Linear: |
| 8338 | Out << 'l'; |
| 8339 | if (!!ParamAttr.StrideOrArg) |
| 8340 | Out << ParamAttr.StrideOrArg; |
| 8341 | break; |
| 8342 | case Uniform: |
| 8343 | Out << 'u'; |
| 8344 | break; |
| 8345 | case Vector: |
| 8346 | Out << 'v'; |
| 8347 | break; |
| 8348 | } |
| 8349 | if (!!ParamAttr.Alignment) |
| 8350 | Out << 'a' << ParamAttr.Alignment; |
| 8351 | } |
| 8352 | Out << '_' << Fn->getName(); |
| 8353 | Fn->addFnAttr(Out.str()); |
| 8354 | } |
| 8355 | } |
| 8356 | } |
| 8357 | |
| 8358 | void CGOpenMPRuntime::emitDeclareSimdFunction(const FunctionDecl *FD, |
| 8359 | llvm::Function *Fn) { |
| 8360 | ASTContext &C = CGM.getContext(); |
Alexey Bataev | 20cf67c | 2018-03-02 18:07:00 +0000 | [diff] [blame] | 8361 | FD = FD->getMostRecentDecl(); |
Alexey Bataev | c7a82b4 | 2016-05-06 09:40:08 +0000 | [diff] [blame] | 8362 | // Map params to their positions in function decl. |
| 8363 | llvm::DenseMap<const Decl *, unsigned> ParamPositions; |
| 8364 | if (isa<CXXMethodDecl>(FD)) |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 8365 | ParamPositions.try_emplace(FD, 0); |
Alexey Bataev | c7a82b4 | 2016-05-06 09:40:08 +0000 | [diff] [blame] | 8366 | unsigned ParamPos = ParamPositions.size(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 8367 | for (const ParmVarDecl *P : FD->parameters()) { |
| 8368 | ParamPositions.try_emplace(P->getCanonicalDecl(), ParamPos); |
Alexey Bataev | c7a82b4 | 2016-05-06 09:40:08 +0000 | [diff] [blame] | 8369 | ++ParamPos; |
| 8370 | } |
Alexey Bataev | 20cf67c | 2018-03-02 18:07:00 +0000 | [diff] [blame] | 8371 | while (FD) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 8372 | for (const auto *Attr : FD->specific_attrs<OMPDeclareSimdDeclAttr>()) { |
Alexey Bataev | 20cf67c | 2018-03-02 18:07:00 +0000 | [diff] [blame] | 8373 | llvm::SmallVector<ParamAttrTy, 8> ParamAttrs(ParamPositions.size()); |
| 8374 | // Mark uniform parameters. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 8375 | for (const Expr *E : Attr->uniforms()) { |
Alexey Bataev | 20cf67c | 2018-03-02 18:07:00 +0000 | [diff] [blame] | 8376 | E = E->IgnoreParenImpCasts(); |
| 8377 | unsigned Pos; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 8378 | if (isa<CXXThisExpr>(E)) { |
Alexey Bataev | 20cf67c | 2018-03-02 18:07:00 +0000 | [diff] [blame] | 8379 | Pos = ParamPositions[FD]; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 8380 | } else { |
| 8381 | const auto *PVD = cast<ParmVarDecl>(cast<DeclRefExpr>(E)->getDecl()) |
| 8382 | ->getCanonicalDecl(); |
Alexey Bataev | 20cf67c | 2018-03-02 18:07:00 +0000 | [diff] [blame] | 8383 | Pos = ParamPositions[PVD]; |
| 8384 | } |
| 8385 | ParamAttrs[Pos].Kind = Uniform; |
Alexey Bataev | c7a82b4 | 2016-05-06 09:40:08 +0000 | [diff] [blame] | 8386 | } |
Alexey Bataev | 20cf67c | 2018-03-02 18:07:00 +0000 | [diff] [blame] | 8387 | // Get alignment info. |
| 8388 | auto NI = Attr->alignments_begin(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 8389 | for (const Expr *E : Attr->aligneds()) { |
Alexey Bataev | 20cf67c | 2018-03-02 18:07:00 +0000 | [diff] [blame] | 8390 | E = E->IgnoreParenImpCasts(); |
| 8391 | unsigned Pos; |
| 8392 | QualType ParmTy; |
| 8393 | if (isa<CXXThisExpr>(E)) { |
| 8394 | Pos = ParamPositions[FD]; |
| 8395 | ParmTy = E->getType(); |
| 8396 | } else { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 8397 | const auto *PVD = cast<ParmVarDecl>(cast<DeclRefExpr>(E)->getDecl()) |
| 8398 | ->getCanonicalDecl(); |
Alexey Bataev | 20cf67c | 2018-03-02 18:07:00 +0000 | [diff] [blame] | 8399 | Pos = ParamPositions[PVD]; |
| 8400 | ParmTy = PVD->getType(); |
| 8401 | } |
| 8402 | ParamAttrs[Pos].Alignment = |
| 8403 | (*NI) |
| 8404 | ? (*NI)->EvaluateKnownConstInt(C) |
Alexey Bataev | c7a82b4 | 2016-05-06 09:40:08 +0000 | [diff] [blame] | 8405 | : llvm::APSInt::getUnsigned( |
| 8406 | C.toCharUnitsFromBits(C.getOpenMPDefaultSimdAlign(ParmTy)) |
| 8407 | .getQuantity()); |
Alexey Bataev | 20cf67c | 2018-03-02 18:07:00 +0000 | [diff] [blame] | 8408 | ++NI; |
Alexey Bataev | c7a82b4 | 2016-05-06 09:40:08 +0000 | [diff] [blame] | 8409 | } |
Alexey Bataev | 20cf67c | 2018-03-02 18:07:00 +0000 | [diff] [blame] | 8410 | // Mark linear parameters. |
| 8411 | auto SI = Attr->steps_begin(); |
| 8412 | auto MI = Attr->modifiers_begin(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 8413 | for (const Expr *E : Attr->linears()) { |
Alexey Bataev | 20cf67c | 2018-03-02 18:07:00 +0000 | [diff] [blame] | 8414 | E = E->IgnoreParenImpCasts(); |
| 8415 | unsigned Pos; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 8416 | if (isa<CXXThisExpr>(E)) { |
Alexey Bataev | 20cf67c | 2018-03-02 18:07:00 +0000 | [diff] [blame] | 8417 | Pos = ParamPositions[FD]; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 8418 | } else { |
| 8419 | const auto *PVD = cast<ParmVarDecl>(cast<DeclRefExpr>(E)->getDecl()) |
| 8420 | ->getCanonicalDecl(); |
Alexey Bataev | 20cf67c | 2018-03-02 18:07:00 +0000 | [diff] [blame] | 8421 | Pos = ParamPositions[PVD]; |
| 8422 | } |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 8423 | ParamAttrTy &ParamAttr = ParamAttrs[Pos]; |
Alexey Bataev | 20cf67c | 2018-03-02 18:07:00 +0000 | [diff] [blame] | 8424 | ParamAttr.Kind = Linear; |
| 8425 | if (*SI) { |
| 8426 | if (!(*SI)->EvaluateAsInt(ParamAttr.StrideOrArg, C, |
| 8427 | Expr::SE_AllowSideEffects)) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 8428 | if (const auto *DRE = |
| 8429 | cast<DeclRefExpr>((*SI)->IgnoreParenImpCasts())) { |
| 8430 | if (const auto *StridePVD = cast<ParmVarDecl>(DRE->getDecl())) { |
Alexey Bataev | 20cf67c | 2018-03-02 18:07:00 +0000 | [diff] [blame] | 8431 | ParamAttr.Kind = LinearWithVarStride; |
| 8432 | ParamAttr.StrideOrArg = llvm::APSInt::getUnsigned( |
| 8433 | ParamPositions[StridePVD->getCanonicalDecl()]); |
| 8434 | } |
Alexey Bataev | c7a82b4 | 2016-05-06 09:40:08 +0000 | [diff] [blame] | 8435 | } |
| 8436 | } |
| 8437 | } |
Alexey Bataev | 20cf67c | 2018-03-02 18:07:00 +0000 | [diff] [blame] | 8438 | ++SI; |
| 8439 | ++MI; |
Alexey Bataev | c7a82b4 | 2016-05-06 09:40:08 +0000 | [diff] [blame] | 8440 | } |
Alexey Bataev | 20cf67c | 2018-03-02 18:07:00 +0000 | [diff] [blame] | 8441 | llvm::APSInt VLENVal; |
| 8442 | if (const Expr *VLEN = Attr->getSimdlen()) |
| 8443 | VLENVal = VLEN->EvaluateKnownConstInt(C); |
| 8444 | OMPDeclareSimdDeclAttr::BranchStateTy State = Attr->getBranchState(); |
| 8445 | if (CGM.getTriple().getArch() == llvm::Triple::x86 || |
| 8446 | CGM.getTriple().getArch() == llvm::Triple::x86_64) |
| 8447 | emitX86DeclareSimdFunction(FD, Fn, VLENVal, ParamAttrs, State); |
Alexey Bataev | c7a82b4 | 2016-05-06 09:40:08 +0000 | [diff] [blame] | 8448 | } |
Alexey Bataev | 20cf67c | 2018-03-02 18:07:00 +0000 | [diff] [blame] | 8449 | FD = FD->getPreviousDecl(); |
Alexey Bataev | c7a82b4 | 2016-05-06 09:40:08 +0000 | [diff] [blame] | 8450 | } |
| 8451 | } |
Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 8452 | |
| 8453 | namespace { |
| 8454 | /// Cleanup action for doacross support. |
| 8455 | class DoacrossCleanupTy final : public EHScopeStack::Cleanup { |
| 8456 | public: |
| 8457 | static const int DoacrossFinArgs = 2; |
| 8458 | |
| 8459 | private: |
| 8460 | llvm::Value *RTLFn; |
| 8461 | llvm::Value *Args[DoacrossFinArgs]; |
| 8462 | |
| 8463 | public: |
| 8464 | DoacrossCleanupTy(llvm::Value *RTLFn, ArrayRef<llvm::Value *> CallArgs) |
| 8465 | : RTLFn(RTLFn) { |
| 8466 | assert(CallArgs.size() == DoacrossFinArgs); |
| 8467 | std::copy(CallArgs.begin(), CallArgs.end(), std::begin(Args)); |
| 8468 | } |
| 8469 | void Emit(CodeGenFunction &CGF, Flags /*flags*/) override { |
| 8470 | if (!CGF.HaveInsertPoint()) |
| 8471 | return; |
| 8472 | CGF.EmitRuntimeCall(RTLFn, Args); |
| 8473 | } |
| 8474 | }; |
| 8475 | } // namespace |
| 8476 | |
| 8477 | void CGOpenMPRuntime::emitDoacrossInit(CodeGenFunction &CGF, |
| 8478 | const OMPLoopDirective &D) { |
| 8479 | if (!CGF.HaveInsertPoint()) |
| 8480 | return; |
| 8481 | |
| 8482 | ASTContext &C = CGM.getContext(); |
| 8483 | QualType Int64Ty = C.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/true); |
| 8484 | RecordDecl *RD; |
| 8485 | if (KmpDimTy.isNull()) { |
| 8486 | // Build struct kmp_dim { // loop bounds info casted to kmp_int64 |
| 8487 | // kmp_int64 lo; // lower |
| 8488 | // kmp_int64 up; // upper |
| 8489 | // kmp_int64 st; // stride |
| 8490 | // }; |
| 8491 | RD = C.buildImplicitRecord("kmp_dim"); |
| 8492 | RD->startDefinition(); |
| 8493 | addFieldToRecordDecl(C, RD, Int64Ty); |
| 8494 | addFieldToRecordDecl(C, RD, Int64Ty); |
| 8495 | addFieldToRecordDecl(C, RD, Int64Ty); |
| 8496 | RD->completeDefinition(); |
| 8497 | KmpDimTy = C.getRecordType(RD); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 8498 | } else { |
Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 8499 | RD = cast<RecordDecl>(KmpDimTy->getAsTagDecl()); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 8500 | } |
Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 8501 | |
| 8502 | Address DimsAddr = CGF.CreateMemTemp(KmpDimTy, "dims"); |
| 8503 | CGF.EmitNullInitialization(DimsAddr, KmpDimTy); |
| 8504 | enum { LowerFD = 0, UpperFD, StrideFD }; |
| 8505 | // Fill dims with data. |
| 8506 | LValue DimsLVal = CGF.MakeAddrLValue(DimsAddr, KmpDimTy); |
| 8507 | // dims.upper = num_iterations; |
| 8508 | LValue UpperLVal = |
| 8509 | CGF.EmitLValueForField(DimsLVal, *std::next(RD->field_begin(), UpperFD)); |
| 8510 | llvm::Value *NumIterVal = CGF.EmitScalarConversion( |
| 8511 | CGF.EmitScalarExpr(D.getNumIterations()), D.getNumIterations()->getType(), |
| 8512 | Int64Ty, D.getNumIterations()->getExprLoc()); |
| 8513 | CGF.EmitStoreOfScalar(NumIterVal, UpperLVal); |
| 8514 | // dims.stride = 1; |
| 8515 | LValue StrideLVal = |
| 8516 | CGF.EmitLValueForField(DimsLVal, *std::next(RD->field_begin(), StrideFD)); |
| 8517 | CGF.EmitStoreOfScalar(llvm::ConstantInt::getSigned(CGM.Int64Ty, /*V=*/1), |
| 8518 | StrideLVal); |
| 8519 | |
| 8520 | // Build call void __kmpc_doacross_init(ident_t *loc, kmp_int32 gtid, |
| 8521 | // kmp_int32 num_dims, struct kmp_dim * dims); |
| 8522 | llvm::Value *Args[] = {emitUpdateLocation(CGF, D.getLocStart()), |
| 8523 | getThreadID(CGF, D.getLocStart()), |
| 8524 | llvm::ConstantInt::getSigned(CGM.Int32Ty, 1), |
| 8525 | CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
| 8526 | DimsAddr.getPointer(), CGM.VoidPtrTy)}; |
| 8527 | |
| 8528 | llvm::Value *RTLFn = createRuntimeFunction(OMPRTL__kmpc_doacross_init); |
| 8529 | CGF.EmitRuntimeCall(RTLFn, Args); |
| 8530 | llvm::Value *FiniArgs[DoacrossCleanupTy::DoacrossFinArgs] = { |
| 8531 | emitUpdateLocation(CGF, D.getLocEnd()), getThreadID(CGF, D.getLocEnd())}; |
| 8532 | llvm::Value *FiniRTLFn = createRuntimeFunction(OMPRTL__kmpc_doacross_fini); |
| 8533 | CGF.EHStack.pushCleanup<DoacrossCleanupTy>(NormalAndEHCleanup, FiniRTLFn, |
| 8534 | llvm::makeArrayRef(FiniArgs)); |
| 8535 | } |
| 8536 | |
| 8537 | void CGOpenMPRuntime::emitDoacrossOrdered(CodeGenFunction &CGF, |
| 8538 | const OMPDependClause *C) { |
| 8539 | QualType Int64Ty = |
| 8540 | CGM.getContext().getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1); |
| 8541 | const Expr *CounterVal = C->getCounterValue(); |
| 8542 | assert(CounterVal); |
| 8543 | llvm::Value *CntVal = CGF.EmitScalarConversion(CGF.EmitScalarExpr(CounterVal), |
| 8544 | CounterVal->getType(), Int64Ty, |
| 8545 | CounterVal->getExprLoc()); |
| 8546 | Address CntAddr = CGF.CreateMemTemp(Int64Ty, ".cnt.addr"); |
| 8547 | CGF.EmitStoreOfScalar(CntVal, CntAddr, /*Volatile=*/false, Int64Ty); |
| 8548 | llvm::Value *Args[] = {emitUpdateLocation(CGF, C->getLocStart()), |
| 8549 | getThreadID(CGF, C->getLocStart()), |
| 8550 | CntAddr.getPointer()}; |
| 8551 | llvm::Value *RTLFn; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 8552 | if (C->getDependencyKind() == OMPC_DEPEND_source) { |
Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 8553 | RTLFn = createRuntimeFunction(OMPRTL__kmpc_doacross_post); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame^] | 8554 | } else { |
Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 8555 | assert(C->getDependencyKind() == OMPC_DEPEND_sink); |
| 8556 | RTLFn = createRuntimeFunction(OMPRTL__kmpc_doacross_wait); |
| 8557 | } |
| 8558 | CGF.EmitRuntimeCall(RTLFn, Args); |
| 8559 | } |
| 8560 | |
Alexey Bataev | 7ef47a6 | 2018-02-22 18:33:31 +0000 | [diff] [blame] | 8561 | void CGOpenMPRuntime::emitCall(CodeGenFunction &CGF, SourceLocation Loc, |
| 8562 | llvm::Value *Callee, |
| 8563 | ArrayRef<llvm::Value *> Args) const { |
| 8564 | assert(Loc.isValid() && "Outlined function call location must be valid."); |
Alexey Bataev | 3c595a6 | 2017-08-14 15:01:03 +0000 | [diff] [blame] | 8565 | auto DL = ApplyDebugLocation::CreateDefaultArtificial(CGF, Loc); |
| 8566 | |
| 8567 | if (auto *Fn = dyn_cast<llvm::Function>(Callee)) { |
Alexey Bataev | 2c7eee5 | 2017-08-04 19:10:54 +0000 | [diff] [blame] | 8568 | if (Fn->doesNotThrow()) { |
Alexey Bataev | 3c595a6 | 2017-08-14 15:01:03 +0000 | [diff] [blame] | 8569 | CGF.EmitNounwindRuntimeCall(Fn, Args); |
Alexey Bataev | 2c7eee5 | 2017-08-04 19:10:54 +0000 | [diff] [blame] | 8570 | return; |
| 8571 | } |
| 8572 | } |
Alexey Bataev | 3c595a6 | 2017-08-14 15:01:03 +0000 | [diff] [blame] | 8573 | CGF.EmitRuntimeCall(Callee, Args); |
| 8574 | } |
| 8575 | |
| 8576 | void CGOpenMPRuntime::emitOutlinedFunctionCall( |
| 8577 | CodeGenFunction &CGF, SourceLocation Loc, llvm::Value *OutlinedFn, |
| 8578 | ArrayRef<llvm::Value *> Args) const { |
Alexey Bataev | 7ef47a6 | 2018-02-22 18:33:31 +0000 | [diff] [blame] | 8579 | emitCall(CGF, Loc, OutlinedFn, Args); |
Alexey Bataev | 2c7eee5 | 2017-08-04 19:10:54 +0000 | [diff] [blame] | 8580 | } |
Alexey Bataev | 3b8d558 | 2017-08-08 18:04:06 +0000 | [diff] [blame] | 8581 | |
| 8582 | Address CGOpenMPRuntime::getParameterAddress(CodeGenFunction &CGF, |
| 8583 | const VarDecl *NativeParam, |
| 8584 | const VarDecl *TargetParam) const { |
| 8585 | return CGF.GetAddrOfLocalVar(NativeParam); |
| 8586 | } |
Alexey Bataev | a8a9153a | 2017-12-29 18:07:07 +0000 | [diff] [blame] | 8587 | |
Gheorghe-Teodor Bercea | d3dcf2f | 2018-03-14 14:17:45 +0000 | [diff] [blame] | 8588 | Address CGOpenMPRuntime::getAddressOfLocalVariable(CodeGenFunction &CGF, |
| 8589 | const VarDecl *VD) { |
| 8590 | return Address::invalid(); |
| 8591 | } |
| 8592 | |
Alexey Bataev | a8a9153a | 2017-12-29 18:07:07 +0000 | [diff] [blame] | 8593 | llvm::Value *CGOpenMPSIMDRuntime::emitParallelOutlinedFunction( |
| 8594 | const OMPExecutableDirective &D, const VarDecl *ThreadIDVar, |
| 8595 | OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen) { |
| 8596 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 8597 | } |
| 8598 | |
| 8599 | llvm::Value *CGOpenMPSIMDRuntime::emitTeamsOutlinedFunction( |
| 8600 | const OMPExecutableDirective &D, const VarDecl *ThreadIDVar, |
| 8601 | OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen) { |
| 8602 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 8603 | } |
| 8604 | |
| 8605 | llvm::Value *CGOpenMPSIMDRuntime::emitTaskOutlinedFunction( |
| 8606 | const OMPExecutableDirective &D, const VarDecl *ThreadIDVar, |
| 8607 | const VarDecl *PartIDVar, const VarDecl *TaskTVar, |
| 8608 | OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen, |
| 8609 | bool Tied, unsigned &NumberOfParts) { |
| 8610 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 8611 | } |
| 8612 | |
| 8613 | void CGOpenMPSIMDRuntime::emitParallelCall(CodeGenFunction &CGF, |
| 8614 | SourceLocation Loc, |
| 8615 | llvm::Value *OutlinedFn, |
| 8616 | ArrayRef<llvm::Value *> CapturedVars, |
| 8617 | const Expr *IfCond) { |
| 8618 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 8619 | } |
| 8620 | |
| 8621 | void CGOpenMPSIMDRuntime::emitCriticalRegion( |
| 8622 | CodeGenFunction &CGF, StringRef CriticalName, |
| 8623 | const RegionCodeGenTy &CriticalOpGen, SourceLocation Loc, |
| 8624 | const Expr *Hint) { |
| 8625 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 8626 | } |
| 8627 | |
| 8628 | void CGOpenMPSIMDRuntime::emitMasterRegion(CodeGenFunction &CGF, |
| 8629 | const RegionCodeGenTy &MasterOpGen, |
| 8630 | SourceLocation Loc) { |
| 8631 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 8632 | } |
| 8633 | |
| 8634 | void CGOpenMPSIMDRuntime::emitTaskyieldCall(CodeGenFunction &CGF, |
| 8635 | SourceLocation Loc) { |
| 8636 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 8637 | } |
| 8638 | |
| 8639 | void CGOpenMPSIMDRuntime::emitTaskgroupRegion( |
| 8640 | CodeGenFunction &CGF, const RegionCodeGenTy &TaskgroupOpGen, |
| 8641 | SourceLocation Loc) { |
| 8642 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 8643 | } |
| 8644 | |
| 8645 | void CGOpenMPSIMDRuntime::emitSingleRegion( |
| 8646 | CodeGenFunction &CGF, const RegionCodeGenTy &SingleOpGen, |
| 8647 | SourceLocation Loc, ArrayRef<const Expr *> CopyprivateVars, |
| 8648 | ArrayRef<const Expr *> DestExprs, ArrayRef<const Expr *> SrcExprs, |
| 8649 | ArrayRef<const Expr *> AssignmentOps) { |
| 8650 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 8651 | } |
| 8652 | |
| 8653 | void CGOpenMPSIMDRuntime::emitOrderedRegion(CodeGenFunction &CGF, |
| 8654 | const RegionCodeGenTy &OrderedOpGen, |
| 8655 | SourceLocation Loc, |
| 8656 | bool IsThreads) { |
| 8657 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 8658 | } |
| 8659 | |
| 8660 | void CGOpenMPSIMDRuntime::emitBarrierCall(CodeGenFunction &CGF, |
| 8661 | SourceLocation Loc, |
| 8662 | OpenMPDirectiveKind Kind, |
| 8663 | bool EmitChecks, |
| 8664 | bool ForceSimpleCall) { |
| 8665 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 8666 | } |
| 8667 | |
| 8668 | void CGOpenMPSIMDRuntime::emitForDispatchInit( |
| 8669 | CodeGenFunction &CGF, SourceLocation Loc, |
| 8670 | const OpenMPScheduleTy &ScheduleKind, unsigned IVSize, bool IVSigned, |
| 8671 | bool Ordered, const DispatchRTInput &DispatchValues) { |
| 8672 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 8673 | } |
| 8674 | |
| 8675 | void CGOpenMPSIMDRuntime::emitForStaticInit( |
| 8676 | CodeGenFunction &CGF, SourceLocation Loc, OpenMPDirectiveKind DKind, |
| 8677 | const OpenMPScheduleTy &ScheduleKind, const StaticRTInput &Values) { |
| 8678 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 8679 | } |
| 8680 | |
| 8681 | void CGOpenMPSIMDRuntime::emitDistributeStaticInit( |
| 8682 | CodeGenFunction &CGF, SourceLocation Loc, |
| 8683 | OpenMPDistScheduleClauseKind SchedKind, const StaticRTInput &Values) { |
| 8684 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 8685 | } |
| 8686 | |
| 8687 | void CGOpenMPSIMDRuntime::emitForOrderedIterationEnd(CodeGenFunction &CGF, |
| 8688 | SourceLocation Loc, |
| 8689 | unsigned IVSize, |
| 8690 | bool IVSigned) { |
| 8691 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 8692 | } |
| 8693 | |
| 8694 | void CGOpenMPSIMDRuntime::emitForStaticFinish(CodeGenFunction &CGF, |
| 8695 | SourceLocation Loc, |
| 8696 | OpenMPDirectiveKind DKind) { |
| 8697 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 8698 | } |
| 8699 | |
| 8700 | llvm::Value *CGOpenMPSIMDRuntime::emitForNext(CodeGenFunction &CGF, |
| 8701 | SourceLocation Loc, |
| 8702 | unsigned IVSize, bool IVSigned, |
| 8703 | Address IL, Address LB, |
| 8704 | Address UB, Address ST) { |
| 8705 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 8706 | } |
| 8707 | |
| 8708 | void CGOpenMPSIMDRuntime::emitNumThreadsClause(CodeGenFunction &CGF, |
| 8709 | llvm::Value *NumThreads, |
| 8710 | SourceLocation Loc) { |
| 8711 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 8712 | } |
| 8713 | |
| 8714 | void CGOpenMPSIMDRuntime::emitProcBindClause(CodeGenFunction &CGF, |
| 8715 | OpenMPProcBindClauseKind ProcBind, |
| 8716 | SourceLocation Loc) { |
| 8717 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 8718 | } |
| 8719 | |
| 8720 | Address CGOpenMPSIMDRuntime::getAddrOfThreadPrivate(CodeGenFunction &CGF, |
| 8721 | const VarDecl *VD, |
| 8722 | Address VDAddr, |
| 8723 | SourceLocation Loc) { |
| 8724 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 8725 | } |
| 8726 | |
| 8727 | llvm::Function *CGOpenMPSIMDRuntime::emitThreadPrivateVarDefinition( |
| 8728 | const VarDecl *VD, Address VDAddr, SourceLocation Loc, bool PerformInit, |
| 8729 | CodeGenFunction *CGF) { |
| 8730 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 8731 | } |
| 8732 | |
| 8733 | Address CGOpenMPSIMDRuntime::getAddrOfArtificialThreadPrivate( |
| 8734 | CodeGenFunction &CGF, QualType VarType, StringRef Name) { |
| 8735 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 8736 | } |
| 8737 | |
| 8738 | void CGOpenMPSIMDRuntime::emitFlush(CodeGenFunction &CGF, |
| 8739 | ArrayRef<const Expr *> Vars, |
| 8740 | SourceLocation Loc) { |
| 8741 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 8742 | } |
| 8743 | |
| 8744 | void CGOpenMPSIMDRuntime::emitTaskCall(CodeGenFunction &CGF, SourceLocation Loc, |
| 8745 | const OMPExecutableDirective &D, |
| 8746 | llvm::Value *TaskFunction, |
| 8747 | QualType SharedsTy, Address Shareds, |
| 8748 | const Expr *IfCond, |
| 8749 | const OMPTaskDataTy &Data) { |
| 8750 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 8751 | } |
| 8752 | |
| 8753 | void CGOpenMPSIMDRuntime::emitTaskLoopCall( |
| 8754 | CodeGenFunction &CGF, SourceLocation Loc, const OMPLoopDirective &D, |
| 8755 | llvm::Value *TaskFunction, QualType SharedsTy, Address Shareds, |
| 8756 | const Expr *IfCond, const OMPTaskDataTy &Data) { |
| 8757 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 8758 | } |
| 8759 | |
| 8760 | void CGOpenMPSIMDRuntime::emitReduction( |
| 8761 | CodeGenFunction &CGF, SourceLocation Loc, ArrayRef<const Expr *> Privates, |
| 8762 | ArrayRef<const Expr *> LHSExprs, ArrayRef<const Expr *> RHSExprs, |
| 8763 | ArrayRef<const Expr *> ReductionOps, ReductionOptionsTy Options) { |
| 8764 | assert(Options.SimpleReduction && "Only simple reduction is expected."); |
| 8765 | CGOpenMPRuntime::emitReduction(CGF, Loc, Privates, LHSExprs, RHSExprs, |
| 8766 | ReductionOps, Options); |
| 8767 | } |
| 8768 | |
| 8769 | llvm::Value *CGOpenMPSIMDRuntime::emitTaskReductionInit( |
| 8770 | CodeGenFunction &CGF, SourceLocation Loc, ArrayRef<const Expr *> LHSExprs, |
| 8771 | ArrayRef<const Expr *> RHSExprs, const OMPTaskDataTy &Data) { |
| 8772 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 8773 | } |
| 8774 | |
| 8775 | void CGOpenMPSIMDRuntime::emitTaskReductionFixups(CodeGenFunction &CGF, |
| 8776 | SourceLocation Loc, |
| 8777 | ReductionCodeGen &RCG, |
| 8778 | unsigned N) { |
| 8779 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 8780 | } |
| 8781 | |
| 8782 | Address CGOpenMPSIMDRuntime::getTaskReductionItem(CodeGenFunction &CGF, |
| 8783 | SourceLocation Loc, |
| 8784 | llvm::Value *ReductionsPtr, |
| 8785 | LValue SharedLVal) { |
| 8786 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 8787 | } |
| 8788 | |
| 8789 | void CGOpenMPSIMDRuntime::emitTaskwaitCall(CodeGenFunction &CGF, |
| 8790 | SourceLocation Loc) { |
| 8791 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 8792 | } |
| 8793 | |
| 8794 | void CGOpenMPSIMDRuntime::emitCancellationPointCall( |
| 8795 | CodeGenFunction &CGF, SourceLocation Loc, |
| 8796 | OpenMPDirectiveKind CancelRegion) { |
| 8797 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 8798 | } |
| 8799 | |
| 8800 | void CGOpenMPSIMDRuntime::emitCancelCall(CodeGenFunction &CGF, |
| 8801 | SourceLocation Loc, const Expr *IfCond, |
| 8802 | OpenMPDirectiveKind CancelRegion) { |
| 8803 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 8804 | } |
| 8805 | |
| 8806 | void CGOpenMPSIMDRuntime::emitTargetOutlinedFunction( |
| 8807 | const OMPExecutableDirective &D, StringRef ParentName, |
| 8808 | llvm::Function *&OutlinedFn, llvm::Constant *&OutlinedFnID, |
| 8809 | bool IsOffloadEntry, const RegionCodeGenTy &CodeGen) { |
| 8810 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 8811 | } |
| 8812 | |
| 8813 | void CGOpenMPSIMDRuntime::emitTargetCall(CodeGenFunction &CGF, |
| 8814 | const OMPExecutableDirective &D, |
| 8815 | llvm::Value *OutlinedFn, |
| 8816 | llvm::Value *OutlinedFnID, |
Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 8817 | const Expr *IfCond, const Expr *Device) { |
Alexey Bataev | a8a9153a | 2017-12-29 18:07:07 +0000 | [diff] [blame] | 8818 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 8819 | } |
| 8820 | |
| 8821 | bool CGOpenMPSIMDRuntime::emitTargetFunctions(GlobalDecl GD) { |
| 8822 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 8823 | } |
| 8824 | |
| 8825 | bool CGOpenMPSIMDRuntime::emitTargetGlobalVariable(GlobalDecl GD) { |
| 8826 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 8827 | } |
| 8828 | |
| 8829 | bool CGOpenMPSIMDRuntime::emitTargetGlobal(GlobalDecl GD) { |
| 8830 | return false; |
| 8831 | } |
| 8832 | |
| 8833 | llvm::Function *CGOpenMPSIMDRuntime::emitRegistrationFunction() { |
| 8834 | return nullptr; |
| 8835 | } |
| 8836 | |
| 8837 | void CGOpenMPSIMDRuntime::emitTeamsCall(CodeGenFunction &CGF, |
| 8838 | const OMPExecutableDirective &D, |
| 8839 | SourceLocation Loc, |
| 8840 | llvm::Value *OutlinedFn, |
| 8841 | ArrayRef<llvm::Value *> CapturedVars) { |
| 8842 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 8843 | } |
| 8844 | |
| 8845 | void CGOpenMPSIMDRuntime::emitNumTeamsClause(CodeGenFunction &CGF, |
| 8846 | const Expr *NumTeams, |
| 8847 | const Expr *ThreadLimit, |
| 8848 | SourceLocation Loc) { |
| 8849 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 8850 | } |
| 8851 | |
| 8852 | void CGOpenMPSIMDRuntime::emitTargetDataCalls( |
| 8853 | CodeGenFunction &CGF, const OMPExecutableDirective &D, const Expr *IfCond, |
| 8854 | const Expr *Device, const RegionCodeGenTy &CodeGen, TargetDataInfo &Info) { |
| 8855 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 8856 | } |
| 8857 | |
| 8858 | void CGOpenMPSIMDRuntime::emitTargetDataStandAloneCall( |
| 8859 | CodeGenFunction &CGF, const OMPExecutableDirective &D, const Expr *IfCond, |
| 8860 | const Expr *Device) { |
| 8861 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 8862 | } |
| 8863 | |
| 8864 | void CGOpenMPSIMDRuntime::emitDoacrossInit(CodeGenFunction &CGF, |
| 8865 | const OMPLoopDirective &D) { |
| 8866 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 8867 | } |
| 8868 | |
| 8869 | void CGOpenMPSIMDRuntime::emitDoacrossOrdered(CodeGenFunction &CGF, |
| 8870 | const OMPDependClause *C) { |
| 8871 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 8872 | } |
| 8873 | |
| 8874 | const VarDecl * |
| 8875 | CGOpenMPSIMDRuntime::translateParameter(const FieldDecl *FD, |
| 8876 | const VarDecl *NativeParam) const { |
| 8877 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 8878 | } |
| 8879 | |
| 8880 | Address |
| 8881 | CGOpenMPSIMDRuntime::getParameterAddress(CodeGenFunction &CGF, |
| 8882 | const VarDecl *NativeParam, |
| 8883 | const VarDecl *TargetParam) const { |
| 8884 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 8885 | } |
| 8886 | |