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" |
Richard Smith | bf5bcf2 | 2018-06-26 23:20:26 +0000 | [diff] [blame] | 22 | #include "clang/Basic/BitmaskEnum.h" |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/ArrayRef.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 { |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 37 | /// 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: |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 40 | /// Kinds of OpenMP regions used in codegen. |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 41 | enum CGOpenMPRegionKind { |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 42 | /// Region with outlined function for standalone 'parallel' |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 43 | /// directive. |
| 44 | ParallelOutlinedRegion, |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 45 | /// Region with outlined function for standalone 'task' directive. |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 46 | TaskOutlinedRegion, |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 47 | /// Region for constructs that do not require function outlining, |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 48 | /// like 'for', 'sections', 'atomic' etc. directives. |
| 49 | InlinedRegion, |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 50 | /// Region with outlined function for standalone 'target' directive. |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 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 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 67 | /// 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 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 71 | /// 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 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 74 | /// 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 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 99 | /// 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 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 112 | /// Get a variable or parameter for storing global thread id |
Alexey Bataev | 8cbe0a6 | 2015-02-26 10:27:34 +0000 | [diff] [blame] | 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 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 116 | /// 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: |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 126 | /// A variable or parameter storing global thread id for OpenMP |
Alexey Bataev | 1809571 | 2014-10-10 12:19:54 +0000 | [diff] [blame] | 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 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 132 | /// 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 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 193 | /// Get a variable or parameter for storing global thread id |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 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 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 197 | /// 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 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 200 | /// Get the name of the capture helper. |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 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: |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 214 | /// A variable or parameter storing global thread id for OpenMP |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 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 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 221 | /// API for inlined captured statement code generation in OpenMP |
Alexey Bataev | 8cbe0a6 | 2015-02-26 10:27:34 +0000 | [diff] [blame] | 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 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 232 | // 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 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 247 | /// 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 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 262 | /// Get a variable or parameter for storing global thread id |
Alexey Bataev | 8cbe0a6 | 2015-02-26 10:27:34 +0000 | [diff] [blame] | 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 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 270 | /// Get an LValue for the current ThreadID variable. |
Alexey Bataev | 311a928 | 2017-10-12 13:51:32 +0000 | [diff] [blame] | 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 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 277 | /// 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: |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 299 | /// CodeGen info about outer OpenMP region. |
Alexey Bataev | 8cbe0a6 | 2015-02-26 10:27:34 +0000 | [diff] [blame] | 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 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 304 | /// API for captured statement code generation in OpenMP target |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 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 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 317 | /// This is unused for target regions because each starts executing |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 318 | /// with a single thread. |
| 319 | const VarDecl *getThreadIDVariable() const override { return nullptr; } |
| 320 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 321 | /// 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 | } |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 336 | /// API for generation of expressions captured in a innermost OpenMP |
Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 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 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 366 | /// Lookup the captured field decl for a variable. |
Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 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 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 373 | /// Emit the captured statement body. |
Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 374 | void EmitBody(CodeGenFunction &CGF, const Stmt *S) override { |
| 375 | llvm_unreachable("No body for expressions"); |
| 376 | } |
| 377 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 378 | /// Get a variable or parameter for storing global thread id |
Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 379 | /// inside OpenMP construct. |
| 380 | const VarDecl *getThreadIDVariable() const override { |
| 381 | llvm_unreachable("No thread id for expressions"); |
| 382 | } |
| 383 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 384 | /// Get the name of the capture helper. |
Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 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 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 396 | /// RAII for emitting code of OpenMP constructs. |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 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: |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 404 | /// Constructs region for combined constructs. |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 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 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 433 | /// Values for bit flags used in the ident_t to describe the fields. |
Alexey Bataev | 50b3c95 | 2016-02-19 10:38:26 +0000 | [diff] [blame] | 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 { |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 437 | /// Use trampoline for internal microtask. |
Alexey Bataev | 50b3c95 | 2016-02-19 10:38:26 +0000 | [diff] [blame] | 438 | OMP_IDENT_IMD = 0x01, |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 439 | /// Use c-style ident structure. |
Alexey Bataev | 50b3c95 | 2016-02-19 10:38:26 +0000 | [diff] [blame] | 440 | OMP_IDENT_KMPC = 0x02, |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 441 | /// Atomic reduction option for kmpc_reduce. |
Alexey Bataev | 50b3c95 | 2016-02-19 10:38:26 +0000 | [diff] [blame] | 442 | OMP_ATOMIC_REDUCE = 0x10, |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 443 | /// Explicit 'barrier' directive. |
Alexey Bataev | 50b3c95 | 2016-02-19 10:38:26 +0000 | [diff] [blame] | 444 | OMP_IDENT_BARRIER_EXPL = 0x20, |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 445 | /// Implicit barrier in code. |
Alexey Bataev | 50b3c95 | 2016-02-19 10:38:26 +0000 | [diff] [blame] | 446 | OMP_IDENT_BARRIER_IMPL = 0x40, |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 447 | /// Implicit barrier in 'for' directive. |
Alexey Bataev | 50b3c95 | 2016-02-19 10:38:26 +0000 | [diff] [blame] | 448 | OMP_IDENT_BARRIER_IMPL_FOR = 0x40, |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 449 | /// Implicit barrier in 'sections' directive. |
Alexey Bataev | 50b3c95 | 2016-02-19 10:38:26 +0000 | [diff] [blame] | 450 | OMP_IDENT_BARRIER_IMPL_SECTIONS = 0xC0, |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 451 | /// 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 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 462 | /// Describes ident structure that describes a source location. |
Alexey Bataev | 50b3c95 | 2016-02-19 10:38:26 +0000 | [diff] [blame] | 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 { |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 489 | /// might be used in Fortran |
Alexey Bataev | 50b3c95 | 2016-02-19 10:38:26 +0000 | [diff] [blame] | 490 | IdentField_Reserved_1, |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 491 | /// OMP_IDENT_xxx flags; OMP_IDENT_KMPC identifies this union member. |
Alexey Bataev | 50b3c95 | 2016-02-19 10:38:26 +0000 | [diff] [blame] | 492 | IdentField_Flags, |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 493 | /// Not really used in Fortran any more |
Alexey Bataev | 50b3c95 | 2016-02-19 10:38:26 +0000 | [diff] [blame] | 494 | IdentField_Reserved_2, |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 495 | /// Source[4] in Fortran, do not use for C++ |
Alexey Bataev | 50b3c95 | 2016-02-19 10:38:26 +0000 | [diff] [blame] | 496 | IdentField_Reserved_3, |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 497 | /// String describing the source location. The string is composed of |
Alexey Bataev | 50b3c95 | 2016-02-19 10:38:26 +0000 | [diff] [blame] | 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 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 503 | /// Schedule types for 'omp for' loops (these enumerators are taken from |
Alexey Bataev | 50b3c95 | 2016-02-19 10:38:26 +0000 | [diff] [blame] | 504 | /// the enum sched_type in kmp.h). |
| 505 | enum OpenMPSchedType { |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 506 | /// Lower bound for default (unordered) versions. |
Alexey Bataev | 50b3c95 | 2016-02-19 10:38:26 +0000 | [diff] [blame] | 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, |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 516 | /// Lower bound for 'ordered' versions. |
Alexey Bataev | 50b3c95 | 2016-02-19 10:38:26 +0000 | [diff] [blame] | 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, |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 525 | /// dist_schedule types |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 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 { |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 536 | /// Call to void __kmpc_fork_call(ident_t *loc, kmp_int32 argc, |
Alexey Bataev | 50b3c95 | 2016-02-19 10:38:26 +0000 | [diff] [blame] | 537 | /// kmpc_micro microtask, ...); |
| 538 | OMPRTL__kmpc_fork_call, |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 539 | /// Call to void *__kmpc_threadprivate_cached(ident_t *loc, |
Alexey Bataev | 50b3c95 | 2016-02-19 10:38:26 +0000 | [diff] [blame] | 540 | /// kmp_int32 global_tid, void *data, size_t size, void ***cache); |
| 541 | OMPRTL__kmpc_threadprivate_cached, |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 542 | /// Call to void __kmpc_threadprivate_register( ident_t *, |
Alexey Bataev | 50b3c95 | 2016-02-19 10:38:26 +0000 | [diff] [blame] | 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); |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 786 | std::string Name = CGF.CGM.getOpenMPRuntime().getName({"init"}); |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 787 | auto *GV = new llvm::GlobalVariable( |
| 788 | CGF.CGM.getModule(), Init->getType(), /*isConstant=*/true, |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 789 | llvm::GlobalValue::PrivateLinkage, Init, Name); |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 790 | LValue LV = CGF.MakeNaturalAlignAddrLValue(GV, Ty); |
| 791 | RValue InitRVal; |
| 792 | switch (CGF.getEvaluationKind(Ty)) { |
| 793 | case TEK_Scalar: |
Alexey Bataev | a9b9cc0 | 2018-01-23 18:12:38 +0000 | [diff] [blame] | 794 | InitRVal = CGF.EmitLoadOfLValue(LV, DRD->getLocation()); |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 795 | break; |
| 796 | case TEK_Complex: |
| 797 | InitRVal = |
Alexey Bataev | a9b9cc0 | 2018-01-23 18:12:38 +0000 | [diff] [blame] | 798 | RValue::getComplex(CGF.EmitLoadOfComplex(LV, DRD->getLocation())); |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 799 | break; |
| 800 | case TEK_Aggregate: |
| 801 | InitRVal = RValue::getAggregate(LV.getAddress()); |
| 802 | break; |
| 803 | } |
Alexey Bataev | a9b9cc0 | 2018-01-23 18:12:38 +0000 | [diff] [blame] | 804 | OpaqueValueExpr OVE(DRD->getLocation(), Ty, VK_RValue); |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 805 | CodeGenFunction::OpaqueValueMapping OpaqueMap(CGF, &OVE, InitRVal); |
| 806 | CGF.EmitAnyExprToMem(&OVE, Private, Ty.getQualifiers(), |
| 807 | /*IsInitializer=*/false); |
| 808 | } |
| 809 | } |
| 810 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 811 | /// Emit initialization of arrays of complex types. |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 812 | /// \param DestAddr Address of the array. |
| 813 | /// \param Type Type of array. |
| 814 | /// \param Init Initial expression of array. |
| 815 | /// \param SrcAddr Address of the original array. |
| 816 | static void EmitOMPAggregateInit(CodeGenFunction &CGF, Address DestAddr, |
Alexey Bataev | a7b1915 | 2017-10-12 20:03:39 +0000 | [diff] [blame] | 817 | QualType Type, bool EmitDeclareReductionInit, |
| 818 | const Expr *Init, |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 819 | const OMPDeclareReductionDecl *DRD, |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 820 | Address SrcAddr = Address::invalid()) { |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 821 | // Perform element-by-element initialization. |
| 822 | QualType ElementTy; |
| 823 | |
| 824 | // Drill down to the base element type on both arrays. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 825 | const ArrayType *ArrayTy = Type->getAsArrayTypeUnsafe(); |
| 826 | llvm::Value *NumElements = CGF.emitArrayLength(ArrayTy, ElementTy, DestAddr); |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 827 | DestAddr = |
| 828 | CGF.Builder.CreateElementBitCast(DestAddr, DestAddr.getElementType()); |
| 829 | if (DRD) |
| 830 | SrcAddr = |
| 831 | CGF.Builder.CreateElementBitCast(SrcAddr, DestAddr.getElementType()); |
| 832 | |
| 833 | llvm::Value *SrcBegin = nullptr; |
| 834 | if (DRD) |
| 835 | SrcBegin = SrcAddr.getPointer(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 836 | llvm::Value *DestBegin = DestAddr.getPointer(); |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 837 | // Cast from pointer to array type to pointer to single element. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 838 | llvm::Value *DestEnd = CGF.Builder.CreateGEP(DestBegin, NumElements); |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 839 | // The basic structure here is a while-do loop. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 840 | llvm::BasicBlock *BodyBB = CGF.createBasicBlock("omp.arrayinit.body"); |
| 841 | llvm::BasicBlock *DoneBB = CGF.createBasicBlock("omp.arrayinit.done"); |
| 842 | llvm::Value *IsEmpty = |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 843 | CGF.Builder.CreateICmpEQ(DestBegin, DestEnd, "omp.arrayinit.isempty"); |
| 844 | CGF.Builder.CreateCondBr(IsEmpty, DoneBB, BodyBB); |
| 845 | |
| 846 | // Enter the loop body, making that address the current address. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 847 | llvm::BasicBlock *EntryBB = CGF.Builder.GetInsertBlock(); |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 848 | CGF.EmitBlock(BodyBB); |
| 849 | |
| 850 | CharUnits ElementSize = CGF.getContext().getTypeSizeInChars(ElementTy); |
| 851 | |
| 852 | llvm::PHINode *SrcElementPHI = nullptr; |
| 853 | Address SrcElementCurrent = Address::invalid(); |
| 854 | if (DRD) { |
| 855 | SrcElementPHI = CGF.Builder.CreatePHI(SrcBegin->getType(), 2, |
| 856 | "omp.arraycpy.srcElementPast"); |
| 857 | SrcElementPHI->addIncoming(SrcBegin, EntryBB); |
| 858 | SrcElementCurrent = |
| 859 | Address(SrcElementPHI, |
| 860 | SrcAddr.getAlignment().alignmentOfArrayElement(ElementSize)); |
| 861 | } |
| 862 | llvm::PHINode *DestElementPHI = CGF.Builder.CreatePHI( |
| 863 | DestBegin->getType(), 2, "omp.arraycpy.destElementPast"); |
| 864 | DestElementPHI->addIncoming(DestBegin, EntryBB); |
| 865 | Address DestElementCurrent = |
| 866 | Address(DestElementPHI, |
| 867 | DestAddr.getAlignment().alignmentOfArrayElement(ElementSize)); |
| 868 | |
| 869 | // Emit copy. |
| 870 | { |
| 871 | CodeGenFunction::RunCleanupsScope InitScope(CGF); |
Alexey Bataev | a7b1915 | 2017-10-12 20:03:39 +0000 | [diff] [blame] | 872 | if (EmitDeclareReductionInit) { |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 873 | emitInitWithReductionInitializer(CGF, DRD, Init, DestElementCurrent, |
| 874 | SrcElementCurrent, ElementTy); |
| 875 | } else |
| 876 | CGF.EmitAnyExprToMem(Init, DestElementCurrent, ElementTy.getQualifiers(), |
| 877 | /*IsInitializer=*/false); |
| 878 | } |
| 879 | |
| 880 | if (DRD) { |
| 881 | // Shift the address forward by one element. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 882 | llvm::Value *SrcElementNext = CGF.Builder.CreateConstGEP1_32( |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 883 | SrcElementPHI, /*Idx0=*/1, "omp.arraycpy.dest.element"); |
| 884 | SrcElementPHI->addIncoming(SrcElementNext, CGF.Builder.GetInsertBlock()); |
| 885 | } |
| 886 | |
| 887 | // Shift the address forward by one element. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 888 | llvm::Value *DestElementNext = CGF.Builder.CreateConstGEP1_32( |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 889 | DestElementPHI, /*Idx0=*/1, "omp.arraycpy.dest.element"); |
| 890 | // Check whether we've reached the end. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 891 | llvm::Value *Done = |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 892 | CGF.Builder.CreateICmpEQ(DestElementNext, DestEnd, "omp.arraycpy.done"); |
| 893 | CGF.Builder.CreateCondBr(Done, DoneBB, BodyBB); |
| 894 | DestElementPHI->addIncoming(DestElementNext, CGF.Builder.GetInsertBlock()); |
| 895 | |
| 896 | // Done. |
| 897 | CGF.EmitBlock(DoneBB, /*IsFinished=*/true); |
| 898 | } |
| 899 | |
| 900 | LValue ReductionCodeGen::emitSharedLValue(CodeGenFunction &CGF, const Expr *E) { |
Alexey Bataev | f47c4b4 | 2017-09-26 13:47:31 +0000 | [diff] [blame] | 901 | return CGF.EmitOMPSharedLValue(E); |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 902 | } |
| 903 | |
| 904 | LValue ReductionCodeGen::emitSharedLValueUB(CodeGenFunction &CGF, |
| 905 | const Expr *E) { |
| 906 | if (const auto *OASE = dyn_cast<OMPArraySectionExpr>(E)) |
| 907 | return CGF.EmitOMPArraySectionExpr(OASE, /*IsLowerBound=*/false); |
| 908 | return LValue(); |
| 909 | } |
| 910 | |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 911 | void ReductionCodeGen::emitAggregateInitialization( |
| 912 | CodeGenFunction &CGF, unsigned N, Address PrivateAddr, LValue SharedLVal, |
| 913 | const OMPDeclareReductionDecl *DRD) { |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 914 | // Emit VarDecl with copy init for arrays. |
| 915 | // Get the address of the original variable captured in current |
| 916 | // captured region. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 917 | const auto *PrivateVD = |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 918 | cast<VarDecl>(cast<DeclRefExpr>(ClausesData[N].Private)->getDecl()); |
Alexey Bataev | a7b1915 | 2017-10-12 20:03:39 +0000 | [diff] [blame] | 919 | bool EmitDeclareReductionInit = |
| 920 | DRD && (DRD->getInitializer() || !PrivateVD->hasInit()); |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 921 | EmitOMPAggregateInit(CGF, PrivateAddr, PrivateVD->getType(), |
Alexey Bataev | a7b1915 | 2017-10-12 20:03:39 +0000 | [diff] [blame] | 922 | EmitDeclareReductionInit, |
| 923 | EmitDeclareReductionInit ? ClausesData[N].ReductionOp |
| 924 | : PrivateVD->getInit(), |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 925 | DRD, SharedLVal.getAddress()); |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 926 | } |
| 927 | |
| 928 | ReductionCodeGen::ReductionCodeGen(ArrayRef<const Expr *> Shareds, |
| 929 | ArrayRef<const Expr *> Privates, |
| 930 | ArrayRef<const Expr *> ReductionOps) { |
| 931 | ClausesData.reserve(Shareds.size()); |
| 932 | SharedAddresses.reserve(Shareds.size()); |
| 933 | Sizes.reserve(Shareds.size()); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 934 | BaseDecls.reserve(Shareds.size()); |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 935 | auto IPriv = Privates.begin(); |
| 936 | auto IRed = ReductionOps.begin(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 937 | for (const Expr *Ref : Shareds) { |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 938 | ClausesData.emplace_back(Ref, *IPriv, *IRed); |
| 939 | std::advance(IPriv, 1); |
| 940 | std::advance(IRed, 1); |
| 941 | } |
| 942 | } |
| 943 | |
| 944 | void ReductionCodeGen::emitSharedLValue(CodeGenFunction &CGF, unsigned N) { |
| 945 | assert(SharedAddresses.size() == N && |
| 946 | "Number of generated lvalues must be exactly N."); |
Jonas Hahnfeld | 4525c82 | 2017-10-23 19:01:35 +0000 | [diff] [blame] | 947 | LValue First = emitSharedLValue(CGF, ClausesData[N].Ref); |
| 948 | LValue Second = emitSharedLValueUB(CGF, ClausesData[N].Ref); |
| 949 | SharedAddresses.emplace_back(First, Second); |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 950 | } |
| 951 | |
| 952 | void ReductionCodeGen::emitAggregateType(CodeGenFunction &CGF, unsigned N) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 953 | const auto *PrivateVD = |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 954 | cast<VarDecl>(cast<DeclRefExpr>(ClausesData[N].Private)->getDecl()); |
| 955 | QualType PrivateType = PrivateVD->getType(); |
| 956 | bool AsArraySection = isa<OMPArraySectionExpr>(ClausesData[N].Ref); |
Jonas Hahnfeld | 4525c82 | 2017-10-23 19:01:35 +0000 | [diff] [blame] | 957 | if (!PrivateType->isVariablyModifiedType()) { |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 958 | Sizes.emplace_back( |
| 959 | CGF.getTypeSize( |
| 960 | SharedAddresses[N].first.getType().getNonReferenceType()), |
| 961 | nullptr); |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 962 | return; |
| 963 | } |
| 964 | llvm::Value *Size; |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 965 | llvm::Value *SizeInChars; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 966 | auto *ElemType = |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 967 | cast<llvm::PointerType>(SharedAddresses[N].first.getPointer()->getType()) |
| 968 | ->getElementType(); |
| 969 | auto *ElemSizeOf = llvm::ConstantExpr::getSizeOf(ElemType); |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 970 | if (AsArraySection) { |
| 971 | Size = CGF.Builder.CreatePtrDiff(SharedAddresses[N].second.getPointer(), |
| 972 | SharedAddresses[N].first.getPointer()); |
| 973 | Size = CGF.Builder.CreateNUWAdd( |
| 974 | Size, llvm::ConstantInt::get(Size->getType(), /*V=*/1)); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 975 | SizeInChars = CGF.Builder.CreateNUWMul(Size, ElemSizeOf); |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 976 | } else { |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 977 | SizeInChars = CGF.getTypeSize( |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 978 | SharedAddresses[N].first.getType().getNonReferenceType()); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 979 | Size = CGF.Builder.CreateExactUDiv(SizeInChars, ElemSizeOf); |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 980 | } |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 981 | Sizes.emplace_back(SizeInChars, Size); |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 982 | CodeGenFunction::OpaqueValueMapping OpaqueMap( |
| 983 | CGF, |
| 984 | cast<OpaqueValueExpr>( |
| 985 | CGF.getContext().getAsVariableArrayType(PrivateType)->getSizeExpr()), |
| 986 | RValue::get(Size)); |
| 987 | CGF.EmitVariablyModifiedType(PrivateType); |
| 988 | } |
| 989 | |
| 990 | void ReductionCodeGen::emitAggregateType(CodeGenFunction &CGF, unsigned N, |
| 991 | llvm::Value *Size) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 992 | const auto *PrivateVD = |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 993 | cast<VarDecl>(cast<DeclRefExpr>(ClausesData[N].Private)->getDecl()); |
| 994 | QualType PrivateType = PrivateVD->getType(); |
Jonas Hahnfeld | 4525c82 | 2017-10-23 19:01:35 +0000 | [diff] [blame] | 995 | if (!PrivateType->isVariablyModifiedType()) { |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 996 | assert(!Size && !Sizes[N].second && |
Jonas Hahnfeld | 4525c82 | 2017-10-23 19:01:35 +0000 | [diff] [blame] | 997 | "Size should be nullptr for non-variably modified reduction " |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 998 | "items."); |
| 999 | return; |
| 1000 | } |
| 1001 | CodeGenFunction::OpaqueValueMapping OpaqueMap( |
| 1002 | CGF, |
| 1003 | cast<OpaqueValueExpr>( |
| 1004 | CGF.getContext().getAsVariableArrayType(PrivateType)->getSizeExpr()), |
| 1005 | RValue::get(Size)); |
| 1006 | CGF.EmitVariablyModifiedType(PrivateType); |
| 1007 | } |
| 1008 | |
| 1009 | void ReductionCodeGen::emitInitialization( |
| 1010 | CodeGenFunction &CGF, unsigned N, Address PrivateAddr, LValue SharedLVal, |
| 1011 | llvm::function_ref<bool(CodeGenFunction &)> DefaultInit) { |
| 1012 | assert(SharedAddresses.size() > N && "No variable was generated"); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1013 | const auto *PrivateVD = |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1014 | cast<VarDecl>(cast<DeclRefExpr>(ClausesData[N].Private)->getDecl()); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1015 | const OMPDeclareReductionDecl *DRD = |
| 1016 | getReductionInit(ClausesData[N].ReductionOp); |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1017 | QualType PrivateType = PrivateVD->getType(); |
| 1018 | PrivateAddr = CGF.Builder.CreateElementBitCast( |
| 1019 | PrivateAddr, CGF.ConvertTypeForMem(PrivateType)); |
| 1020 | QualType SharedType = SharedAddresses[N].first.getType(); |
| 1021 | SharedLVal = CGF.MakeAddrLValue( |
| 1022 | CGF.Builder.CreateElementBitCast(SharedLVal.getAddress(), |
| 1023 | CGF.ConvertTypeForMem(SharedType)), |
Ivan A. Kosarev | f5f2046 | 2017-10-12 11:29:46 +0000 | [diff] [blame] | 1024 | SharedType, SharedAddresses[N].first.getBaseInfo(), |
Ivan A. Kosarev | b9c59f3 | 2017-10-31 11:05:34 +0000 | [diff] [blame] | 1025 | CGF.CGM.getTBAAInfoForSubobject(SharedAddresses[N].first, SharedType)); |
Jonas Hahnfeld | 4525c82 | 2017-10-23 19:01:35 +0000 | [diff] [blame] | 1026 | if (CGF.getContext().getAsArrayType(PrivateVD->getType())) { |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 1027 | emitAggregateInitialization(CGF, N, PrivateAddr, SharedLVal, DRD); |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1028 | } else if (DRD && (DRD->getInitializer() || !PrivateVD->hasInit())) { |
| 1029 | emitInitWithReductionInitializer(CGF, DRD, ClausesData[N].ReductionOp, |
| 1030 | PrivateAddr, SharedLVal.getAddress(), |
| 1031 | SharedLVal.getType()); |
| 1032 | } else if (!DefaultInit(CGF) && PrivateVD->hasInit() && |
| 1033 | !CGF.isTrivialInitializer(PrivateVD->getInit())) { |
| 1034 | CGF.EmitAnyExprToMem(PrivateVD->getInit(), PrivateAddr, |
| 1035 | PrivateVD->getType().getQualifiers(), |
| 1036 | /*IsInitializer=*/false); |
| 1037 | } |
| 1038 | } |
| 1039 | |
| 1040 | bool ReductionCodeGen::needCleanups(unsigned N) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1041 | const auto *PrivateVD = |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1042 | cast<VarDecl>(cast<DeclRefExpr>(ClausesData[N].Private)->getDecl()); |
| 1043 | QualType PrivateType = PrivateVD->getType(); |
| 1044 | QualType::DestructionKind DTorKind = PrivateType.isDestructedType(); |
| 1045 | return DTorKind != QualType::DK_none; |
| 1046 | } |
| 1047 | |
| 1048 | void ReductionCodeGen::emitCleanups(CodeGenFunction &CGF, unsigned N, |
| 1049 | Address PrivateAddr) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1050 | const auto *PrivateVD = |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1051 | cast<VarDecl>(cast<DeclRefExpr>(ClausesData[N].Private)->getDecl()); |
| 1052 | QualType PrivateType = PrivateVD->getType(); |
| 1053 | QualType::DestructionKind DTorKind = PrivateType.isDestructedType(); |
| 1054 | if (needCleanups(N)) { |
| 1055 | PrivateAddr = CGF.Builder.CreateElementBitCast( |
| 1056 | PrivateAddr, CGF.ConvertTypeForMem(PrivateType)); |
| 1057 | CGF.pushDestroy(DTorKind, PrivateAddr, PrivateType); |
| 1058 | } |
| 1059 | } |
| 1060 | |
| 1061 | static LValue loadToBegin(CodeGenFunction &CGF, QualType BaseTy, QualType ElTy, |
| 1062 | LValue BaseLV) { |
| 1063 | BaseTy = BaseTy.getNonReferenceType(); |
| 1064 | while ((BaseTy->isPointerType() || BaseTy->isReferenceType()) && |
| 1065 | !CGF.getContext().hasSameType(BaseTy, ElTy)) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1066 | if (const auto *PtrTy = BaseTy->getAs<PointerType>()) { |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1067 | BaseLV = CGF.EmitLoadOfPointerLValue(BaseLV.getAddress(), PtrTy); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1068 | } else { |
Ivan A. Kosarev | 9f9d157 | 2017-10-30 11:49:31 +0000 | [diff] [blame] | 1069 | LValue RefLVal = CGF.MakeAddrLValue(BaseLV.getAddress(), BaseTy); |
| 1070 | BaseLV = CGF.EmitLoadOfReferenceLValue(RefLVal); |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1071 | } |
| 1072 | BaseTy = BaseTy->getPointeeType(); |
| 1073 | } |
| 1074 | return CGF.MakeAddrLValue( |
| 1075 | CGF.Builder.CreateElementBitCast(BaseLV.getAddress(), |
| 1076 | CGF.ConvertTypeForMem(ElTy)), |
Ivan A. Kosarev | f5f2046 | 2017-10-12 11:29:46 +0000 | [diff] [blame] | 1077 | BaseLV.getType(), BaseLV.getBaseInfo(), |
Ivan A. Kosarev | b9c59f3 | 2017-10-31 11:05:34 +0000 | [diff] [blame] | 1078 | CGF.CGM.getTBAAInfoForSubobject(BaseLV, BaseLV.getType())); |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1079 | } |
| 1080 | |
| 1081 | static Address castToBase(CodeGenFunction &CGF, QualType BaseTy, QualType ElTy, |
| 1082 | llvm::Type *BaseLVType, CharUnits BaseLVAlignment, |
| 1083 | llvm::Value *Addr) { |
| 1084 | Address Tmp = Address::invalid(); |
| 1085 | Address TopTmp = Address::invalid(); |
| 1086 | Address MostTopTmp = Address::invalid(); |
| 1087 | BaseTy = BaseTy.getNonReferenceType(); |
| 1088 | while ((BaseTy->isPointerType() || BaseTy->isReferenceType()) && |
| 1089 | !CGF.getContext().hasSameType(BaseTy, ElTy)) { |
| 1090 | Tmp = CGF.CreateMemTemp(BaseTy); |
| 1091 | if (TopTmp.isValid()) |
| 1092 | CGF.Builder.CreateStore(Tmp.getPointer(), TopTmp); |
| 1093 | else |
| 1094 | MostTopTmp = Tmp; |
| 1095 | TopTmp = Tmp; |
| 1096 | BaseTy = BaseTy->getPointeeType(); |
| 1097 | } |
| 1098 | llvm::Type *Ty = BaseLVType; |
| 1099 | if (Tmp.isValid()) |
| 1100 | Ty = Tmp.getElementType(); |
| 1101 | Addr = CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(Addr, Ty); |
| 1102 | if (Tmp.isValid()) { |
| 1103 | CGF.Builder.CreateStore(Addr, Tmp); |
| 1104 | return MostTopTmp; |
| 1105 | } |
| 1106 | return Address(Addr, BaseLVAlignment); |
| 1107 | } |
| 1108 | |
Alexey Bataev | 1c44e15 | 2018-03-06 18:59:43 +0000 | [diff] [blame] | 1109 | static const VarDecl *getBaseDecl(const Expr *Ref, const DeclRefExpr *&DE) { |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1110 | const VarDecl *OrigVD = nullptr; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1111 | if (const auto *OASE = dyn_cast<OMPArraySectionExpr>(Ref)) { |
| 1112 | const Expr *Base = OASE->getBase()->IgnoreParenImpCasts(); |
| 1113 | while (const auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base)) |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1114 | Base = TempOASE->getBase()->IgnoreParenImpCasts(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1115 | while (const auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base)) |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1116 | Base = TempASE->getBase()->IgnoreParenImpCasts(); |
| 1117 | DE = cast<DeclRefExpr>(Base); |
| 1118 | OrigVD = cast<VarDecl>(DE->getDecl()); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1119 | } else if (const auto *ASE = dyn_cast<ArraySubscriptExpr>(Ref)) { |
| 1120 | const Expr *Base = ASE->getBase()->IgnoreParenImpCasts(); |
| 1121 | while (const auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base)) |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1122 | Base = TempASE->getBase()->IgnoreParenImpCasts(); |
| 1123 | DE = cast<DeclRefExpr>(Base); |
| 1124 | OrigVD = cast<VarDecl>(DE->getDecl()); |
| 1125 | } |
Alexey Bataev | 1c44e15 | 2018-03-06 18:59:43 +0000 | [diff] [blame] | 1126 | return OrigVD; |
| 1127 | } |
| 1128 | |
| 1129 | Address ReductionCodeGen::adjustPrivateAddress(CodeGenFunction &CGF, unsigned N, |
| 1130 | Address PrivateAddr) { |
| 1131 | const DeclRefExpr *DE; |
| 1132 | if (const VarDecl *OrigVD = ::getBaseDecl(ClausesData[N].Ref, DE)) { |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1133 | BaseDecls.emplace_back(OrigVD); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1134 | LValue OriginalBaseLValue = CGF.EmitLValue(DE); |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1135 | LValue BaseLValue = |
| 1136 | loadToBegin(CGF, OrigVD->getType(), SharedAddresses[N].first.getType(), |
| 1137 | OriginalBaseLValue); |
| 1138 | llvm::Value *Adjustment = CGF.Builder.CreatePtrDiff( |
| 1139 | BaseLValue.getPointer(), SharedAddresses[N].first.getPointer()); |
Jonas Hahnfeld | 273d261 | 2017-12-06 19:15:28 +0000 | [diff] [blame] | 1140 | llvm::Value *PrivatePointer = |
| 1141 | CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
| 1142 | PrivateAddr.getPointer(), |
| 1143 | SharedAddresses[N].first.getAddress().getType()); |
| 1144 | llvm::Value *Ptr = CGF.Builder.CreateGEP(PrivatePointer, Adjustment); |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1145 | return castToBase(CGF, OrigVD->getType(), |
| 1146 | SharedAddresses[N].first.getType(), |
Jonas Hahnfeld | 273d261 | 2017-12-06 19:15:28 +0000 | [diff] [blame] | 1147 | OriginalBaseLValue.getAddress().getType(), |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1148 | OriginalBaseLValue.getAlignment(), Ptr); |
| 1149 | } |
| 1150 | BaseDecls.emplace_back( |
| 1151 | cast<VarDecl>(cast<DeclRefExpr>(ClausesData[N].Ref)->getDecl())); |
| 1152 | return PrivateAddr; |
| 1153 | } |
| 1154 | |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 1155 | bool ReductionCodeGen::usesReductionInitializer(unsigned N) const { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1156 | const OMPDeclareReductionDecl *DRD = |
| 1157 | getReductionInit(ClausesData[N].ReductionOp); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 1158 | return DRD && DRD->getInitializer(); |
| 1159 | } |
| 1160 | |
Alexey Bataev | 1809571 | 2014-10-10 12:19:54 +0000 | [diff] [blame] | 1161 | LValue CGOpenMPRegionInfo::getThreadIDVariableLValue(CodeGenFunction &CGF) { |
Alexey Bataev | 31300ed | 2016-02-04 11:27:03 +0000 | [diff] [blame] | 1162 | return CGF.EmitLoadOfPointerLValue( |
| 1163 | CGF.GetAddrOfLocalVar(getThreadIDVariable()), |
| 1164 | getThreadIDVariable()->getType()->castAs<PointerType>()); |
Alexey Bataev | 1809571 | 2014-10-10 12:19:54 +0000 | [diff] [blame] | 1165 | } |
| 1166 | |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 1167 | void CGOpenMPRegionInfo::EmitBody(CodeGenFunction &CGF, const Stmt * /*S*/) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 1168 | if (!CGF.HaveInsertPoint()) |
| 1169 | return; |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 1170 | // 1.2.2 OpenMP Language Terminology |
| 1171 | // Structured block - An executable statement with a single entry at the |
| 1172 | // top and a single exit at the bottom. |
| 1173 | // The point of exit cannot be a branch out of the structured block. |
| 1174 | // longjmp() and throw() must not violate the entry/exit criteria. |
| 1175 | CGF.EHStack.pushTerminate(); |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 1176 | CodeGen(CGF); |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 1177 | CGF.EHStack.popTerminate(); |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 1178 | } |
| 1179 | |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 1180 | LValue CGOpenMPTaskOutlinedRegionInfo::getThreadIDVariableLValue( |
| 1181 | CodeGenFunction &CGF) { |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 1182 | return CGF.MakeAddrLValue(CGF.GetAddrOfLocalVar(getThreadIDVariable()), |
| 1183 | getThreadIDVariable()->getType(), |
Ivan A. Kosarev | 5f8c0ca | 2017-10-10 09:39:32 +0000 | [diff] [blame] | 1184 | AlignmentSource::Decl); |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 1185 | } |
| 1186 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1187 | static FieldDecl *addFieldToRecordDecl(ASTContext &C, DeclContext *DC, |
| 1188 | QualType FieldTy) { |
| 1189 | auto *Field = FieldDecl::Create( |
| 1190 | C, DC, SourceLocation(), SourceLocation(), /*Id=*/nullptr, FieldTy, |
| 1191 | C.getTrivialTypeSourceInfo(FieldTy, SourceLocation()), |
| 1192 | /*BW=*/nullptr, /*Mutable=*/false, /*InitStyle=*/ICIS_NoInit); |
| 1193 | Field->setAccess(AS_public); |
| 1194 | DC->addDecl(Field); |
| 1195 | return Field; |
| 1196 | } |
| 1197 | |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 1198 | CGOpenMPRuntime::CGOpenMPRuntime(CodeGenModule &CGM, StringRef FirstSeparator, |
| 1199 | StringRef Separator) |
| 1200 | : CGM(CGM), FirstSeparator(FirstSeparator), Separator(Separator), |
| 1201 | OffloadEntriesInfoManager(CGM) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1202 | ASTContext &C = CGM.getContext(); |
| 1203 | RecordDecl *RD = C.buildImplicitRecord("ident_t"); |
| 1204 | QualType KmpInt32Ty = C.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1); |
| 1205 | RD->startDefinition(); |
| 1206 | // reserved_1 |
| 1207 | addFieldToRecordDecl(C, RD, KmpInt32Ty); |
| 1208 | // flags |
| 1209 | addFieldToRecordDecl(C, RD, KmpInt32Ty); |
| 1210 | // reserved_2 |
| 1211 | addFieldToRecordDecl(C, RD, KmpInt32Ty); |
| 1212 | // reserved_3 |
| 1213 | addFieldToRecordDecl(C, RD, KmpInt32Ty); |
| 1214 | // psource |
| 1215 | addFieldToRecordDecl(C, RD, C.VoidPtrTy); |
| 1216 | RD->completeDefinition(); |
| 1217 | IdentQTy = C.getRecordType(RD); |
| 1218 | IdentTy = CGM.getTypes().ConvertRecordDeclType(RD); |
Alexey Bataev | 3a3bf0b | 2014-09-22 10:01:53 +0000 | [diff] [blame] | 1219 | KmpCriticalNameTy = llvm::ArrayType::get(CGM.Int32Ty, /*NumElements*/ 8); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 1220 | |
| 1221 | loadOffloadInfoMetadata(); |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1222 | } |
| 1223 | |
Alexey Bataev | 9179755 | 2015-03-18 04:13:55 +0000 | [diff] [blame] | 1224 | void CGOpenMPRuntime::clear() { |
| 1225 | InternalVars.clear(); |
Alexey Bataev | 2a6f3f5 | 2018-11-07 19:11:14 +0000 | [diff] [blame] | 1226 | // Clean non-target variable declarations possibly used only in debug info. |
| 1227 | for (const auto &Data : EmittedNonTargetVariables) { |
| 1228 | if (!Data.getValue().pointsToAliveValue()) |
| 1229 | continue; |
| 1230 | auto *GV = dyn_cast<llvm::GlobalVariable>(Data.getValue()); |
| 1231 | if (!GV) |
| 1232 | continue; |
| 1233 | if (!GV->isDeclaration() || GV->getNumUses() > 0) |
| 1234 | continue; |
| 1235 | GV->eraseFromParent(); |
| 1236 | } |
Alexey Bataev | 9179755 | 2015-03-18 04:13:55 +0000 | [diff] [blame] | 1237 | } |
| 1238 | |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 1239 | std::string CGOpenMPRuntime::getName(ArrayRef<StringRef> Parts) const { |
| 1240 | SmallString<128> Buffer; |
| 1241 | llvm::raw_svector_ostream OS(Buffer); |
| 1242 | StringRef Sep = FirstSeparator; |
| 1243 | for (StringRef Part : Parts) { |
| 1244 | OS << Sep << Part; |
| 1245 | Sep = Separator; |
| 1246 | } |
| 1247 | return OS.str(); |
| 1248 | } |
| 1249 | |
Alexey Bataev | c5b1d32 | 2016-03-04 09:22:22 +0000 | [diff] [blame] | 1250 | static llvm::Function * |
| 1251 | emitCombinerOrInitializer(CodeGenModule &CGM, QualType Ty, |
| 1252 | const Expr *CombinerInitializer, const VarDecl *In, |
| 1253 | const VarDecl *Out, bool IsCombiner) { |
| 1254 | // void .omp_combiner.(Ty *in, Ty *out); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1255 | ASTContext &C = CGM.getContext(); |
Alexey Bataev | c5b1d32 | 2016-03-04 09:22:22 +0000 | [diff] [blame] | 1256 | QualType PtrTy = C.getPointerType(Ty).withRestrict(); |
| 1257 | FunctionArgList Args; |
Alexey Bataev | c5b1d32 | 2016-03-04 09:22:22 +0000 | [diff] [blame] | 1258 | ImplicitParamDecl OmpOutParm(C, /*DC=*/nullptr, Out->getLocation(), |
Alexey Bataev | 5622323 | 2017-06-09 13:40:18 +0000 | [diff] [blame] | 1259 | /*Id=*/nullptr, PtrTy, ImplicitParamDecl::Other); |
Alexey Bataev | a839ddd | 2016-03-17 10:19:46 +0000 | [diff] [blame] | 1260 | ImplicitParamDecl OmpInParm(C, /*DC=*/nullptr, In->getLocation(), |
Alexey Bataev | 5622323 | 2017-06-09 13:40:18 +0000 | [diff] [blame] | 1261 | /*Id=*/nullptr, PtrTy, ImplicitParamDecl::Other); |
Alexey Bataev | c5b1d32 | 2016-03-04 09:22:22 +0000 | [diff] [blame] | 1262 | Args.push_back(&OmpOutParm); |
Alexey Bataev | a839ddd | 2016-03-17 10:19:46 +0000 | [diff] [blame] | 1263 | Args.push_back(&OmpInParm); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1264 | const CGFunctionInfo &FnInfo = |
John McCall | c56a8b3 | 2016-03-11 04:30:31 +0000 | [diff] [blame] | 1265 | CGM.getTypes().arrangeBuiltinFunctionDeclaration(C.VoidTy, Args); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1266 | llvm::FunctionType *FnTy = CGM.getTypes().GetFunctionType(FnInfo); |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 1267 | std::string Name = CGM.getOpenMPRuntime().getName( |
| 1268 | {IsCombiner ? "omp_combiner" : "omp_initializer", ""}); |
| 1269 | auto *Fn = llvm::Function::Create(FnTy, llvm::GlobalValue::InternalLinkage, |
| 1270 | Name, &CGM.getModule()); |
Rafael Espindola | 51ec5a9 | 2018-02-28 23:46:35 +0000 | [diff] [blame] | 1271 | CGM.SetInternalFunctionAttributes(GlobalDecl(), Fn, FnInfo); |
Chandler Carruth | fcd3314 | 2016-12-23 01:24:49 +0000 | [diff] [blame] | 1272 | Fn->removeFnAttr(llvm::Attribute::NoInline); |
Mehdi Amini | 6aa9e9b | 2017-05-29 05:38:20 +0000 | [diff] [blame] | 1273 | Fn->removeFnAttr(llvm::Attribute::OptimizeNone); |
Alexey Bataev | a839ddd | 2016-03-17 10:19:46 +0000 | [diff] [blame] | 1274 | Fn->addFnAttr(llvm::Attribute::AlwaysInline); |
Alexey Bataev | c5b1d32 | 2016-03-04 09:22:22 +0000 | [diff] [blame] | 1275 | CodeGenFunction CGF(CGM); |
| 1276 | // Map "T omp_in;" variable to "*omp_in_parm" value in all expressions. |
| 1277 | // 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] | 1278 | CGF.StartFunction(GlobalDecl(), C.VoidTy, Fn, FnInfo, Args, In->getLocation(), |
| 1279 | Out->getLocation()); |
Alexey Bataev | c5b1d32 | 2016-03-04 09:22:22 +0000 | [diff] [blame] | 1280 | CodeGenFunction::OMPPrivateScope Scope(CGF); |
| 1281 | Address AddrIn = CGF.GetAddrOfLocalVar(&OmpInParm); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1282 | Scope.addPrivate(In, [&CGF, AddrIn, PtrTy]() { |
Alexey Bataev | c5b1d32 | 2016-03-04 09:22:22 +0000 | [diff] [blame] | 1283 | return CGF.EmitLoadOfPointerLValue(AddrIn, PtrTy->castAs<PointerType>()) |
| 1284 | .getAddress(); |
| 1285 | }); |
| 1286 | Address AddrOut = CGF.GetAddrOfLocalVar(&OmpOutParm); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1287 | Scope.addPrivate(Out, [&CGF, AddrOut, PtrTy]() { |
Alexey Bataev | c5b1d32 | 2016-03-04 09:22:22 +0000 | [diff] [blame] | 1288 | return CGF.EmitLoadOfPointerLValue(AddrOut, PtrTy->castAs<PointerType>()) |
| 1289 | .getAddress(); |
| 1290 | }); |
| 1291 | (void)Scope.Privatize(); |
Alexey Bataev | 070f43a | 2017-09-06 14:49:58 +0000 | [diff] [blame] | 1292 | if (!IsCombiner && Out->hasInit() && |
| 1293 | !CGF.isTrivialInitializer(Out->getInit())) { |
| 1294 | CGF.EmitAnyExprToMem(Out->getInit(), CGF.GetAddrOfLocalVar(Out), |
| 1295 | Out->getType().getQualifiers(), |
| 1296 | /*IsInitializer=*/true); |
| 1297 | } |
| 1298 | if (CombinerInitializer) |
| 1299 | CGF.EmitIgnoredExpr(CombinerInitializer); |
Alexey Bataev | c5b1d32 | 2016-03-04 09:22:22 +0000 | [diff] [blame] | 1300 | Scope.ForceCleanup(); |
| 1301 | CGF.FinishFunction(); |
| 1302 | return Fn; |
| 1303 | } |
| 1304 | |
| 1305 | void CGOpenMPRuntime::emitUserDefinedReduction( |
| 1306 | CodeGenFunction *CGF, const OMPDeclareReductionDecl *D) { |
| 1307 | if (UDRMap.count(D) > 0) |
| 1308 | return; |
Alexey Bataev | c5b1d32 | 2016-03-04 09:22:22 +0000 | [diff] [blame] | 1309 | llvm::Function *Combiner = emitCombinerOrInitializer( |
Alexey Bataev | e6aa469 | 2018-09-13 16:54:05 +0000 | [diff] [blame] | 1310 | CGM, D->getType(), D->getCombiner(), |
| 1311 | cast<VarDecl>(cast<DeclRefExpr>(D->getCombinerIn())->getDecl()), |
| 1312 | cast<VarDecl>(cast<DeclRefExpr>(D->getCombinerOut())->getDecl()), |
Alexey Bataev | c5b1d32 | 2016-03-04 09:22:22 +0000 | [diff] [blame] | 1313 | /*IsCombiner=*/true); |
| 1314 | llvm::Function *Initializer = nullptr; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1315 | if (const Expr *Init = D->getInitializer()) { |
Alexey Bataev | c5b1d32 | 2016-03-04 09:22:22 +0000 | [diff] [blame] | 1316 | Initializer = emitCombinerOrInitializer( |
Alexey Bataev | 070f43a | 2017-09-06 14:49:58 +0000 | [diff] [blame] | 1317 | CGM, D->getType(), |
| 1318 | D->getInitializerKind() == OMPDeclareReductionDecl::CallInit ? Init |
| 1319 | : nullptr, |
Alexey Bataev | e6aa469 | 2018-09-13 16:54:05 +0000 | [diff] [blame] | 1320 | cast<VarDecl>(cast<DeclRefExpr>(D->getInitOrig())->getDecl()), |
| 1321 | cast<VarDecl>(cast<DeclRefExpr>(D->getInitPriv())->getDecl()), |
Alexey Bataev | c5b1d32 | 2016-03-04 09:22:22 +0000 | [diff] [blame] | 1322 | /*IsCombiner=*/false); |
| 1323 | } |
Alexey Bataev | 43a919f | 2018-04-13 17:48:43 +0000 | [diff] [blame] | 1324 | UDRMap.try_emplace(D, Combiner, Initializer); |
Alexey Bataev | c5b1d32 | 2016-03-04 09:22:22 +0000 | [diff] [blame] | 1325 | if (CGF) { |
| 1326 | auto &Decls = FunctionUDRMap.FindAndConstruct(CGF->CurFn); |
| 1327 | Decls.second.push_back(D); |
| 1328 | } |
| 1329 | } |
| 1330 | |
Alexey Bataev | a839ddd | 2016-03-17 10:19:46 +0000 | [diff] [blame] | 1331 | std::pair<llvm::Function *, llvm::Function *> |
| 1332 | CGOpenMPRuntime::getUserDefinedReduction(const OMPDeclareReductionDecl *D) { |
| 1333 | auto I = UDRMap.find(D); |
| 1334 | if (I != UDRMap.end()) |
| 1335 | return I->second; |
| 1336 | emitUserDefinedReduction(/*CGF=*/nullptr, D); |
| 1337 | return UDRMap.lookup(D); |
| 1338 | } |
| 1339 | |
Arpith Chacko Jacob | 19b911c | 2017-01-18 18:18:53 +0000 | [diff] [blame] | 1340 | static llvm::Value *emitParallelOrTeamsOutlinedFunction( |
| 1341 | CodeGenModule &CGM, const OMPExecutableDirective &D, const CapturedStmt *CS, |
| 1342 | const VarDecl *ThreadIDVar, OpenMPDirectiveKind InnermostKind, |
| 1343 | const StringRef OutlinedHelperName, const RegionCodeGenTy &CodeGen) { |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 1344 | assert(ThreadIDVar->getType()->isPointerType() && |
| 1345 | "thread id variable must be of type kmp_int32 *"); |
Alexey Bataev | 1809571 | 2014-10-10 12:19:54 +0000 | [diff] [blame] | 1346 | CodeGenFunction CGF(CGM, true); |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 1347 | bool HasCancel = false; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1348 | if (const auto *OPD = dyn_cast<OMPParallelDirective>(&D)) |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 1349 | HasCancel = OPD->hasCancel(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1350 | else if (const auto *OPSD = dyn_cast<OMPParallelSectionsDirective>(&D)) |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 1351 | HasCancel = OPSD->hasCancel(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1352 | else if (const auto *OPFD = dyn_cast<OMPParallelForDirective>(&D)) |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +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 = dyn_cast<OMPTargetParallelForDirective>(&D)) |
Alexey Bataev | 2139ed6 | 2017-11-16 18:20:21 +0000 | [diff] [blame] | 1355 | HasCancel = OPFD->hasCancel(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1356 | else if (const auto *OPFD = dyn_cast<OMPDistributeParallelForDirective>(&D)) |
Alexey Bataev | 10a5431 | 2017-11-27 16:54:08 +0000 | [diff] [blame] | 1357 | HasCancel = OPFD->hasCancel(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1358 | else if (const auto *OPFD = |
| 1359 | dyn_cast<OMPTeamsDistributeParallelForDirective>(&D)) |
Alexey Bataev | 10a5431 | 2017-11-27 16:54:08 +0000 | [diff] [blame] | 1360 | HasCancel = OPFD->hasCancel(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1361 | else if (const auto *OPFD = |
Alexey Bataev | 10a5431 | 2017-11-27 16:54:08 +0000 | [diff] [blame] | 1362 | dyn_cast<OMPTargetTeamsDistributeParallelForDirective>(&D)) |
| 1363 | HasCancel = OPFD->hasCancel(); |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 1364 | CGOpenMPOutlinedRegionInfo CGInfo(*CS, ThreadIDVar, CodeGen, InnermostKind, |
Arpith Chacko Jacob | 19b911c | 2017-01-18 18:18:53 +0000 | [diff] [blame] | 1365 | HasCancel, OutlinedHelperName); |
Alexey Bataev | d157d47 | 2015-06-24 03:35:38 +0000 | [diff] [blame] | 1366 | CodeGenFunction::CGCapturedStmtRAII CapInfoRAII(CGF, &CGInfo); |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 1367 | return CGF.GenerateOpenMPCapturedStmtFunction(*CS); |
Alexey Bataev | 1809571 | 2014-10-10 12:19:54 +0000 | [diff] [blame] | 1368 | } |
| 1369 | |
Arpith Chacko Jacob | 19b911c | 2017-01-18 18:18:53 +0000 | [diff] [blame] | 1370 | llvm::Value *CGOpenMPRuntime::emitParallelOutlinedFunction( |
| 1371 | const OMPExecutableDirective &D, const VarDecl *ThreadIDVar, |
| 1372 | OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen) { |
| 1373 | const CapturedStmt *CS = D.getCapturedStmt(OMPD_parallel); |
| 1374 | return emitParallelOrTeamsOutlinedFunction( |
| 1375 | CGM, D, CS, ThreadIDVar, InnermostKind, getOutlinedHelperName(), CodeGen); |
| 1376 | } |
| 1377 | |
| 1378 | llvm::Value *CGOpenMPRuntime::emitTeamsOutlinedFunction( |
| 1379 | const OMPExecutableDirective &D, const VarDecl *ThreadIDVar, |
| 1380 | OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen) { |
| 1381 | const CapturedStmt *CS = D.getCapturedStmt(OMPD_teams); |
| 1382 | return emitParallelOrTeamsOutlinedFunction( |
| 1383 | CGM, D, CS, ThreadIDVar, InnermostKind, getOutlinedHelperName(), CodeGen); |
| 1384 | } |
| 1385 | |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 1386 | llvm::Value *CGOpenMPRuntime::emitTaskOutlinedFunction( |
| 1387 | const OMPExecutableDirective &D, const VarDecl *ThreadIDVar, |
Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 1388 | const VarDecl *PartIDVar, const VarDecl *TaskTVar, |
| 1389 | OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen, |
| 1390 | bool Tied, unsigned &NumberOfParts) { |
| 1391 | auto &&UntiedCodeGen = [this, &D, TaskTVar](CodeGenFunction &CGF, |
| 1392 | PrePostActionTy &) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1393 | llvm::Value *ThreadID = getThreadID(CGF, D.getBeginLoc()); |
| 1394 | llvm::Value *UpLoc = emitUpdateLocation(CGF, D.getBeginLoc()); |
Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 1395 | llvm::Value *TaskArgs[] = { |
| 1396 | UpLoc, ThreadID, |
| 1397 | CGF.EmitLoadOfPointerLValue(CGF.GetAddrOfLocalVar(TaskTVar), |
| 1398 | TaskTVar->getType()->castAs<PointerType>()) |
| 1399 | .getPointer()}; |
| 1400 | CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_omp_task), TaskArgs); |
| 1401 | }; |
| 1402 | CGOpenMPTaskOutlinedRegionInfo::UntiedTaskActionTy Action(Tied, PartIDVar, |
| 1403 | UntiedCodeGen); |
| 1404 | CodeGen.setAction(Action); |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 1405 | assert(!ThreadIDVar->getType()->isPointerType() && |
| 1406 | "thread id variable must be of type kmp_int32 for tasks"); |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 1407 | const OpenMPDirectiveKind Region = |
| 1408 | isOpenMPTaskLoopDirective(D.getDirectiveKind()) ? OMPD_taskloop |
| 1409 | : OMPD_task; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1410 | const CapturedStmt *CS = D.getCapturedStmt(Region); |
| 1411 | const auto *TD = dyn_cast<OMPTaskDirective>(&D); |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 1412 | CodeGenFunction CGF(CGM, true); |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 1413 | CGOpenMPTaskOutlinedRegionInfo CGInfo(*CS, ThreadIDVar, CodeGen, |
| 1414 | InnermostKind, |
| 1415 | TD ? TD->hasCancel() : false, Action); |
Alexey Bataev | d157d47 | 2015-06-24 03:35:38 +0000 | [diff] [blame] | 1416 | CodeGenFunction::CGCapturedStmtRAII CapInfoRAII(CGF, &CGInfo); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1417 | llvm::Value *Res = CGF.GenerateCapturedStmtFunction(*CS); |
Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 1418 | if (!Tied) |
| 1419 | NumberOfParts = Action.getNumberOfParts(); |
| 1420 | return Res; |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 1421 | } |
| 1422 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1423 | static void buildStructValue(ConstantStructBuilder &Fields, CodeGenModule &CGM, |
| 1424 | const RecordDecl *RD, const CGRecordLayout &RL, |
| 1425 | ArrayRef<llvm::Constant *> Data) { |
| 1426 | llvm::StructType *StructTy = RL.getLLVMType(); |
| 1427 | unsigned PrevIdx = 0; |
| 1428 | ConstantInitBuilder CIBuilder(CGM); |
| 1429 | auto DI = Data.begin(); |
| 1430 | for (const FieldDecl *FD : RD->fields()) { |
| 1431 | unsigned Idx = RL.getLLVMFieldNo(FD); |
| 1432 | // Fill the alignment. |
| 1433 | for (unsigned I = PrevIdx; I < Idx; ++I) |
| 1434 | Fields.add(llvm::Constant::getNullValue(StructTy->getElementType(I))); |
| 1435 | PrevIdx = Idx + 1; |
| 1436 | Fields.add(*DI); |
| 1437 | ++DI; |
| 1438 | } |
| 1439 | } |
| 1440 | |
| 1441 | template <class... As> |
| 1442 | static llvm::GlobalVariable * |
Mike Rice | e1ca7b6 | 2018-08-29 15:45:11 +0000 | [diff] [blame] | 1443 | createGlobalStruct(CodeGenModule &CGM, QualType Ty, bool IsConstant, |
| 1444 | ArrayRef<llvm::Constant *> Data, const Twine &Name, |
| 1445 | As &&... Args) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1446 | const auto *RD = cast<RecordDecl>(Ty->getAsTagDecl()); |
| 1447 | const CGRecordLayout &RL = CGM.getTypes().getCGRecordLayout(RD); |
| 1448 | ConstantInitBuilder CIBuilder(CGM); |
| 1449 | ConstantStructBuilder Fields = CIBuilder.beginStruct(RL.getLLVMType()); |
| 1450 | buildStructValue(Fields, CGM, RD, RL, Data); |
| 1451 | return Fields.finishAndCreateGlobal( |
Mike Rice | e1ca7b6 | 2018-08-29 15:45:11 +0000 | [diff] [blame] | 1452 | Name, CGM.getContext().getAlignOfGlobalVarInChars(Ty), IsConstant, |
| 1453 | std::forward<As>(Args)...); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1454 | } |
| 1455 | |
| 1456 | template <typename T> |
Benjamin Kramer | 651d0bf | 2018-05-15 21:26:47 +0000 | [diff] [blame] | 1457 | static void |
| 1458 | createConstantGlobalStructAndAddToParent(CodeGenModule &CGM, QualType Ty, |
| 1459 | ArrayRef<llvm::Constant *> Data, |
| 1460 | T &Parent) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1461 | const auto *RD = cast<RecordDecl>(Ty->getAsTagDecl()); |
| 1462 | const CGRecordLayout &RL = CGM.getTypes().getCGRecordLayout(RD); |
| 1463 | ConstantStructBuilder Fields = Parent.beginStruct(RL.getLLVMType()); |
| 1464 | buildStructValue(Fields, CGM, RD, RL, Data); |
| 1465 | Fields.finishAndAddTo(Parent); |
| 1466 | } |
| 1467 | |
Alexey Bataev | 50b3c95 | 2016-02-19 10:38:26 +0000 | [diff] [blame] | 1468 | Address CGOpenMPRuntime::getOrCreateDefaultLocation(unsigned Flags) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1469 | CharUnits Align = CGM.getContext().getTypeAlignInChars(IdentQTy); |
Alexey Bataev | 15007ba | 2014-05-07 06:18:01 +0000 | [diff] [blame] | 1470 | llvm::Value *Entry = OpenMPDefaultLocMap.lookup(Flags); |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1471 | if (!Entry) { |
| 1472 | if (!DefaultOpenMPPSource) { |
| 1473 | // Initialize default location for psource field of ident_t structure of |
| 1474 | // all ident_t objects. Format is ";file;function;line;column;;". |
| 1475 | // Taken from |
| 1476 | // http://llvm.org/svn/llvm-project/openmp/trunk/runtime/src/kmp_str.c |
| 1477 | DefaultOpenMPPSource = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1478 | CGM.GetAddrOfConstantCString(";unknown;unknown;0;0;;").getPointer(); |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1479 | DefaultOpenMPPSource = |
| 1480 | llvm::ConstantExpr::getBitCast(DefaultOpenMPPSource, CGM.Int8PtrTy); |
| 1481 | } |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1482 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1483 | llvm::Constant *Data[] = {llvm::ConstantInt::getNullValue(CGM.Int32Ty), |
| 1484 | llvm::ConstantInt::get(CGM.Int32Ty, Flags), |
| 1485 | llvm::ConstantInt::getNullValue(CGM.Int32Ty), |
| 1486 | llvm::ConstantInt::getNullValue(CGM.Int32Ty), |
| 1487 | DefaultOpenMPPSource}; |
Mike Rice | e1ca7b6 | 2018-08-29 15:45:11 +0000 | [diff] [blame] | 1488 | llvm::GlobalValue *DefaultOpenMPLocation = |
| 1489 | createGlobalStruct(CGM, IdentQTy, /*IsConstant=*/false, Data, "", |
| 1490 | llvm::GlobalValue::PrivateLinkage); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1491 | DefaultOpenMPLocation->setUnnamedAddr( |
| 1492 | llvm::GlobalValue::UnnamedAddr::Global); |
John McCall | 6c9f1fdb | 2016-11-19 08:17:24 +0000 | [diff] [blame] | 1493 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1494 | OpenMPDefaultLocMap[Flags] = Entry = DefaultOpenMPLocation; |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1495 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1496 | return Address(Entry, Align); |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1497 | } |
| 1498 | |
Alexey Bataev | fd006c4 | 2018-10-05 15:08:53 +0000 | [diff] [blame] | 1499 | void CGOpenMPRuntime::setLocThreadIdInsertPt(CodeGenFunction &CGF, |
| 1500 | bool AtCurrentPoint) { |
| 1501 | auto &Elem = OpenMPLocThreadIDMap.FindAndConstruct(CGF.CurFn); |
| 1502 | assert(!Elem.second.ServiceInsertPt && "Insert point is set already."); |
| 1503 | |
| 1504 | llvm::Value *Undef = llvm::UndefValue::get(CGF.Int32Ty); |
| 1505 | if (AtCurrentPoint) { |
| 1506 | Elem.second.ServiceInsertPt = new llvm::BitCastInst( |
| 1507 | Undef, CGF.Int32Ty, "svcpt", CGF.Builder.GetInsertBlock()); |
| 1508 | } else { |
| 1509 | Elem.second.ServiceInsertPt = |
| 1510 | new llvm::BitCastInst(Undef, CGF.Int32Ty, "svcpt"); |
| 1511 | Elem.second.ServiceInsertPt->insertAfter(CGF.AllocaInsertPt); |
| 1512 | } |
| 1513 | } |
| 1514 | |
| 1515 | void CGOpenMPRuntime::clearLocThreadIdInsertPt(CodeGenFunction &CGF) { |
| 1516 | auto &Elem = OpenMPLocThreadIDMap.FindAndConstruct(CGF.CurFn); |
| 1517 | if (Elem.second.ServiceInsertPt) { |
| 1518 | llvm::Instruction *Ptr = Elem.second.ServiceInsertPt; |
| 1519 | Elem.second.ServiceInsertPt = nullptr; |
| 1520 | Ptr->eraseFromParent(); |
| 1521 | } |
| 1522 | } |
| 1523 | |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 1524 | llvm::Value *CGOpenMPRuntime::emitUpdateLocation(CodeGenFunction &CGF, |
| 1525 | SourceLocation Loc, |
Alexey Bataev | 50b3c95 | 2016-02-19 10:38:26 +0000 | [diff] [blame] | 1526 | unsigned Flags) { |
| 1527 | Flags |= OMP_IDENT_KMPC; |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1528 | // If no debug info is generated - return global default location. |
Benjamin Kramer | 8c30592 | 2016-02-02 11:06:51 +0000 | [diff] [blame] | 1529 | if (CGM.getCodeGenOpts().getDebugInfo() == codegenoptions::NoDebugInfo || |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1530 | Loc.isInvalid()) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1531 | return getOrCreateDefaultLocation(Flags).getPointer(); |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1532 | |
| 1533 | assert(CGF.CurFn && "No function in current CodeGenFunction."); |
| 1534 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1535 | CharUnits Align = CGM.getContext().getTypeAlignInChars(IdentQTy); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1536 | Address LocValue = Address::invalid(); |
Alexey Bataev | 1e4b713 | 2014-12-03 12:11:24 +0000 | [diff] [blame] | 1537 | auto I = OpenMPLocThreadIDMap.find(CGF.CurFn); |
| 1538 | if (I != OpenMPLocThreadIDMap.end()) |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1539 | LocValue = Address(I->second.DebugLoc, Align); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1540 | |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 1541 | // OpenMPLocThreadIDMap may have null DebugLoc and non-null ThreadID, if |
| 1542 | // GetOpenMPThreadID was called before this routine. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1543 | if (!LocValue.isValid()) { |
Alexey Bataev | 15007ba | 2014-05-07 06:18:01 +0000 | [diff] [blame] | 1544 | // Generate "ident_t .kmpc_loc.addr;" |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1545 | Address AI = CGF.CreateMemTemp(IdentQTy, ".kmpc_loc.addr"); |
Alexey Bataev | 1809571 | 2014-10-10 12:19:54 +0000 | [diff] [blame] | 1546 | auto &Elem = OpenMPLocThreadIDMap.FindAndConstruct(CGF.CurFn); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1547 | Elem.second.DebugLoc = AI.getPointer(); |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1548 | LocValue = AI; |
| 1549 | |
Alexey Bataev | fd006c4 | 2018-10-05 15:08:53 +0000 | [diff] [blame] | 1550 | if (!Elem.second.ServiceInsertPt) |
| 1551 | setLocThreadIdInsertPt(CGF); |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1552 | CGBuilderTy::InsertPointGuard IPG(CGF.Builder); |
Alexey Bataev | fd006c4 | 2018-10-05 15:08:53 +0000 | [diff] [blame] | 1553 | CGF.Builder.SetInsertPoint(Elem.second.ServiceInsertPt); |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 1554 | CGF.Builder.CreateMemCpy(LocValue, getOrCreateDefaultLocation(Flags), |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1555 | CGF.getTypeSize(IdentQTy)); |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1556 | } |
| 1557 | |
| 1558 | // char **psource = &.kmpc_loc_<flags>.addr.psource; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1559 | LValue Base = CGF.MakeAddrLValue(LocValue, IdentQTy); |
| 1560 | auto Fields = cast<RecordDecl>(IdentQTy->getAsTagDecl())->field_begin(); |
| 1561 | LValue PSource = |
| 1562 | CGF.EmitLValueForField(Base, *std::next(Fields, IdentField_PSource)); |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1563 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1564 | llvm::Value *OMPDebugLoc = OpenMPDebugLocMap.lookup(Loc.getRawEncoding()); |
Alexey Bataev | f002aca | 2014-05-30 05:48:40 +0000 | [diff] [blame] | 1565 | if (OMPDebugLoc == nullptr) { |
| 1566 | SmallString<128> Buffer2; |
| 1567 | llvm::raw_svector_ostream OS2(Buffer2); |
| 1568 | // Build debug location |
| 1569 | PresumedLoc PLoc = CGF.getContext().getSourceManager().getPresumedLoc(Loc); |
| 1570 | OS2 << ";" << PLoc.getFilename() << ";"; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1571 | if (const auto *FD = dyn_cast_or_null<FunctionDecl>(CGF.CurFuncDecl)) |
Alexey Bataev | f002aca | 2014-05-30 05:48:40 +0000 | [diff] [blame] | 1572 | OS2 << FD->getQualifiedNameAsString(); |
Alexey Bataev | f002aca | 2014-05-30 05:48:40 +0000 | [diff] [blame] | 1573 | OS2 << ";" << PLoc.getLine() << ";" << PLoc.getColumn() << ";;"; |
| 1574 | OMPDebugLoc = CGF.Builder.CreateGlobalStringPtr(OS2.str()); |
| 1575 | OpenMPDebugLocMap[Loc.getRawEncoding()] = OMPDebugLoc; |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1576 | } |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1577 | // *psource = ";<File>;<Function>;<Line>;<Column>;;"; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1578 | CGF.EmitStoreOfScalar(OMPDebugLoc, PSource); |
Alexey Bataev | f002aca | 2014-05-30 05:48:40 +0000 | [diff] [blame] | 1579 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1580 | // Our callers always pass this to a runtime function, so for |
| 1581 | // convenience, go ahead and return a naked pointer. |
| 1582 | return LocValue.getPointer(); |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1583 | } |
| 1584 | |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 1585 | llvm::Value *CGOpenMPRuntime::getThreadID(CodeGenFunction &CGF, |
| 1586 | SourceLocation Loc) { |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1587 | assert(CGF.CurFn && "No function in current CodeGenFunction."); |
| 1588 | |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 1589 | llvm::Value *ThreadID = nullptr; |
Alexey Bataev | 1809571 | 2014-10-10 12:19:54 +0000 | [diff] [blame] | 1590 | // Check whether we've already cached a load of the thread id in this |
| 1591 | // function. |
Alexey Bataev | 1e4b713 | 2014-12-03 12:11:24 +0000 | [diff] [blame] | 1592 | auto I = OpenMPLocThreadIDMap.find(CGF.CurFn); |
Alexey Bataev | 1809571 | 2014-10-10 12:19:54 +0000 | [diff] [blame] | 1593 | if (I != OpenMPLocThreadIDMap.end()) { |
| 1594 | ThreadID = I->second.ThreadID; |
Alexey Bataev | 03b340a | 2014-10-21 03:16:40 +0000 | [diff] [blame] | 1595 | if (ThreadID != nullptr) |
| 1596 | return ThreadID; |
| 1597 | } |
Alexey Bataev | aee1855 | 2017-08-16 14:01:00 +0000 | [diff] [blame] | 1598 | // If exceptions are enabled, do not use parameter to avoid possible crash. |
Alexey Bataev | 5d2c9a4 | 2017-11-02 18:55:05 +0000 | [diff] [blame] | 1599 | if (!CGF.EHStack.requiresLandingPad() || !CGF.getLangOpts().Exceptions || |
| 1600 | !CGF.getLangOpts().CXXExceptions || |
Alexey Bataev | 0e1b458 | 2017-11-02 14:25:34 +0000 | [diff] [blame] | 1601 | CGF.Builder.GetInsertBlock() == CGF.AllocaInsertPt->getParent()) { |
Alexey Bataev | aee1855 | 2017-08-16 14:01:00 +0000 | [diff] [blame] | 1602 | if (auto *OMPRegionInfo = |
| 1603 | dyn_cast_or_null<CGOpenMPRegionInfo>(CGF.CapturedStmtInfo)) { |
| 1604 | if (OMPRegionInfo->getThreadIDVariable()) { |
| 1605 | // Check if this an outlined function with thread id passed as argument. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1606 | LValue LVal = OMPRegionInfo->getThreadIDVariableLValue(CGF); |
Alexey Bataev | 1e49137 | 2018-01-23 18:44:14 +0000 | [diff] [blame] | 1607 | ThreadID = CGF.EmitLoadOfScalar(LVal, Loc); |
Alexey Bataev | aee1855 | 2017-08-16 14:01:00 +0000 | [diff] [blame] | 1608 | // If value loaded in entry block, cache it and use it everywhere in |
| 1609 | // function. |
| 1610 | if (CGF.Builder.GetInsertBlock() == CGF.AllocaInsertPt->getParent()) { |
| 1611 | auto &Elem = OpenMPLocThreadIDMap.FindAndConstruct(CGF.CurFn); |
| 1612 | Elem.second.ThreadID = ThreadID; |
| 1613 | } |
| 1614 | return ThreadID; |
Alexey Bataev | 8cbe0a6 | 2015-02-26 10:27:34 +0000 | [diff] [blame] | 1615 | } |
Alexey Bataev | d6c5755 | 2014-07-25 07:55:17 +0000 | [diff] [blame] | 1616 | } |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1617 | } |
Alexey Bataev | 8cbe0a6 | 2015-02-26 10:27:34 +0000 | [diff] [blame] | 1618 | |
| 1619 | // This is not an outlined function region - need to call __kmpc_int32 |
| 1620 | // kmpc_global_thread_num(ident_t *loc). |
| 1621 | // Generate thread id value and cache this value for use across the |
| 1622 | // function. |
Alexey Bataev | fd006c4 | 2018-10-05 15:08:53 +0000 | [diff] [blame] | 1623 | auto &Elem = OpenMPLocThreadIDMap.FindAndConstruct(CGF.CurFn); |
| 1624 | if (!Elem.second.ServiceInsertPt) |
| 1625 | setLocThreadIdInsertPt(CGF); |
Alexey Bataev | 8cbe0a6 | 2015-02-26 10:27:34 +0000 | [diff] [blame] | 1626 | CGBuilderTy::InsertPointGuard IPG(CGF.Builder); |
Alexey Bataev | fd006c4 | 2018-10-05 15:08:53 +0000 | [diff] [blame] | 1627 | CGF.Builder.SetInsertPoint(Elem.second.ServiceInsertPt); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1628 | llvm::CallInst *Call = CGF.Builder.CreateCall( |
Alexey Bataev | 0e1b458 | 2017-11-02 14:25:34 +0000 | [diff] [blame] | 1629 | createRuntimeFunction(OMPRTL__kmpc_global_thread_num), |
| 1630 | emitUpdateLocation(CGF, Loc)); |
| 1631 | Call->setCallingConv(CGF.getRuntimeCC()); |
Alexey Bataev | 0e1b458 | 2017-11-02 14:25:34 +0000 | [diff] [blame] | 1632 | Elem.second.ThreadID = Call; |
| 1633 | return Call; |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1634 | } |
| 1635 | |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 1636 | void CGOpenMPRuntime::functionFinished(CodeGenFunction &CGF) { |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1637 | assert(CGF.CurFn && "No function in current CodeGenFunction."); |
Alexey Bataev | fd006c4 | 2018-10-05 15:08:53 +0000 | [diff] [blame] | 1638 | if (OpenMPLocThreadIDMap.count(CGF.CurFn)) { |
| 1639 | clearLocThreadIdInsertPt(CGF); |
Alexey Bataev | 03b340a | 2014-10-21 03:16:40 +0000 | [diff] [blame] | 1640 | OpenMPLocThreadIDMap.erase(CGF.CurFn); |
Alexey Bataev | fd006c4 | 2018-10-05 15:08:53 +0000 | [diff] [blame] | 1641 | } |
Alexey Bataev | c5b1d32 | 2016-03-04 09:22:22 +0000 | [diff] [blame] | 1642 | if (FunctionUDRMap.count(CGF.CurFn) > 0) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1643 | for(auto *D : FunctionUDRMap[CGF.CurFn]) |
Alexey Bataev | c5b1d32 | 2016-03-04 09:22:22 +0000 | [diff] [blame] | 1644 | UDRMap.erase(D); |
Alexey Bataev | c5b1d32 | 2016-03-04 09:22:22 +0000 | [diff] [blame] | 1645 | FunctionUDRMap.erase(CGF.CurFn); |
| 1646 | } |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1647 | } |
| 1648 | |
| 1649 | llvm::Type *CGOpenMPRuntime::getIdentTyPointerTy() { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1650 | return IdentTy->getPointerTo(); |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1651 | } |
| 1652 | |
| 1653 | llvm::Type *CGOpenMPRuntime::getKmpc_MicroPointerTy() { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 1654 | if (!Kmpc_MicroTy) { |
| 1655 | // Build void (*kmpc_micro)(kmp_int32 *global_tid, kmp_int32 *bound_tid,...) |
| 1656 | llvm::Type *MicroParams[] = {llvm::PointerType::getUnqual(CGM.Int32Ty), |
| 1657 | llvm::PointerType::getUnqual(CGM.Int32Ty)}; |
| 1658 | Kmpc_MicroTy = llvm::FunctionType::get(CGM.VoidTy, MicroParams, true); |
| 1659 | } |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1660 | return llvm::PointerType::getUnqual(Kmpc_MicroTy); |
| 1661 | } |
| 1662 | |
| 1663 | llvm::Constant * |
Alexey Bataev | 50b3c95 | 2016-02-19 10:38:26 +0000 | [diff] [blame] | 1664 | CGOpenMPRuntime::createRuntimeFunction(unsigned Function) { |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1665 | llvm::Constant *RTLFn = nullptr; |
Alexey Bataev | 50b3c95 | 2016-02-19 10:38:26 +0000 | [diff] [blame] | 1666 | switch (static_cast<OpenMPRTLFunction>(Function)) { |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1667 | case OMPRTL__kmpc_fork_call: { |
| 1668 | // Build void __kmpc_fork_call(ident_t *loc, kmp_int32 argc, kmpc_micro |
| 1669 | // microtask, ...); |
Alexey Bataev | 23b6942 | 2014-06-18 07:08:49 +0000 | [diff] [blame] | 1670 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty, |
| 1671 | getKmpc_MicroPointerTy()}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1672 | auto *FnTy = |
Alexey Bataev | d74d060 | 2014-10-13 06:02:40 +0000 | [diff] [blame] | 1673 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ true); |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1674 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_fork_call"); |
| 1675 | break; |
| 1676 | } |
| 1677 | case OMPRTL__kmpc_global_thread_num: { |
| 1678 | // Build kmp_int32 __kmpc_global_thread_num(ident_t *loc); |
Alexey Bataev | 23b6942 | 2014-06-18 07:08:49 +0000 | [diff] [blame] | 1679 | llvm::Type *TypeParams[] = {getIdentTyPointerTy()}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1680 | auto *FnTy = |
Alexey Bataev | d74d060 | 2014-10-13 06:02:40 +0000 | [diff] [blame] | 1681 | llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg*/ false); |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1682 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_global_thread_num"); |
| 1683 | break; |
| 1684 | } |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 1685 | case OMPRTL__kmpc_threadprivate_cached: { |
| 1686 | // Build void *__kmpc_threadprivate_cached(ident_t *loc, |
| 1687 | // kmp_int32 global_tid, void *data, size_t size, void ***cache); |
| 1688 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty, |
| 1689 | CGM.VoidPtrTy, CGM.SizeTy, |
| 1690 | CGM.VoidPtrTy->getPointerTo()->getPointerTo()}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1691 | auto *FnTy = |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 1692 | llvm::FunctionType::get(CGM.VoidPtrTy, TypeParams, /*isVarArg*/ false); |
| 1693 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_threadprivate_cached"); |
| 1694 | break; |
| 1695 | } |
Alexey Bataev | 3a3bf0b | 2014-09-22 10:01:53 +0000 | [diff] [blame] | 1696 | case OMPRTL__kmpc_critical: { |
Alexey Bataev | f947218 | 2014-09-22 12:32:31 +0000 | [diff] [blame] | 1697 | // Build void __kmpc_critical(ident_t *loc, kmp_int32 global_tid, |
| 1698 | // kmp_critical_name *crit); |
Alexey Bataev | 3a3bf0b | 2014-09-22 10:01:53 +0000 | [diff] [blame] | 1699 | llvm::Type *TypeParams[] = { |
| 1700 | getIdentTyPointerTy(), CGM.Int32Ty, |
| 1701 | llvm::PointerType::getUnqual(KmpCriticalNameTy)}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1702 | auto *FnTy = |
Alexey Bataev | 3a3bf0b | 2014-09-22 10:01:53 +0000 | [diff] [blame] | 1703 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false); |
| 1704 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_critical"); |
| 1705 | break; |
| 1706 | } |
Alexey Bataev | fc57d16 | 2015-12-15 10:55:09 +0000 | [diff] [blame] | 1707 | case OMPRTL__kmpc_critical_with_hint: { |
| 1708 | // Build void __kmpc_critical_with_hint(ident_t *loc, kmp_int32 global_tid, |
| 1709 | // kmp_critical_name *crit, uintptr_t hint); |
| 1710 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty, |
| 1711 | llvm::PointerType::getUnqual(KmpCriticalNameTy), |
| 1712 | CGM.IntPtrTy}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1713 | auto *FnTy = |
Alexey Bataev | fc57d16 | 2015-12-15 10:55:09 +0000 | [diff] [blame] | 1714 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false); |
| 1715 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_critical_with_hint"); |
| 1716 | break; |
| 1717 | } |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 1718 | case OMPRTL__kmpc_threadprivate_register: { |
| 1719 | // Build void __kmpc_threadprivate_register(ident_t *, void *data, |
| 1720 | // kmpc_ctor ctor, kmpc_cctor cctor, kmpc_dtor dtor); |
| 1721 | // typedef void *(*kmpc_ctor)(void *); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1722 | auto *KmpcCtorTy = |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 1723 | llvm::FunctionType::get(CGM.VoidPtrTy, CGM.VoidPtrTy, |
| 1724 | /*isVarArg*/ false)->getPointerTo(); |
| 1725 | // typedef void *(*kmpc_cctor)(void *, void *); |
| 1726 | llvm::Type *KmpcCopyCtorTyArgs[] = {CGM.VoidPtrTy, CGM.VoidPtrTy}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1727 | auto *KmpcCopyCtorTy = |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 1728 | llvm::FunctionType::get(CGM.VoidPtrTy, KmpcCopyCtorTyArgs, |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1729 | /*isVarArg*/ false) |
| 1730 | ->getPointerTo(); |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 1731 | // typedef void (*kmpc_dtor)(void *); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1732 | auto *KmpcDtorTy = |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 1733 | llvm::FunctionType::get(CGM.VoidTy, CGM.VoidPtrTy, /*isVarArg*/ false) |
| 1734 | ->getPointerTo(); |
| 1735 | llvm::Type *FnTyArgs[] = {getIdentTyPointerTy(), CGM.VoidPtrTy, KmpcCtorTy, |
| 1736 | KmpcCopyCtorTy, KmpcDtorTy}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1737 | auto *FnTy = llvm::FunctionType::get(CGM.VoidTy, FnTyArgs, |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 1738 | /*isVarArg*/ false); |
| 1739 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_threadprivate_register"); |
| 1740 | break; |
| 1741 | } |
Alexey Bataev | 3a3bf0b | 2014-09-22 10:01:53 +0000 | [diff] [blame] | 1742 | case OMPRTL__kmpc_end_critical: { |
Alexey Bataev | f947218 | 2014-09-22 12:32:31 +0000 | [diff] [blame] | 1743 | // Build void __kmpc_end_critical(ident_t *loc, kmp_int32 global_tid, |
| 1744 | // kmp_critical_name *crit); |
Alexey Bataev | 3a3bf0b | 2014-09-22 10:01:53 +0000 | [diff] [blame] | 1745 | llvm::Type *TypeParams[] = { |
| 1746 | getIdentTyPointerTy(), CGM.Int32Ty, |
| 1747 | llvm::PointerType::getUnqual(KmpCriticalNameTy)}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1748 | auto *FnTy = |
Alexey Bataev | 3a3bf0b | 2014-09-22 10:01:53 +0000 | [diff] [blame] | 1749 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false); |
| 1750 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_end_critical"); |
| 1751 | break; |
| 1752 | } |
Alexey Bataev | 8f7c1b0 | 2014-12-05 04:09:23 +0000 | [diff] [blame] | 1753 | case OMPRTL__kmpc_cancel_barrier: { |
| 1754 | // Build kmp_int32 __kmpc_cancel_barrier(ident_t *loc, kmp_int32 |
| 1755 | // global_tid); |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 1756 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1757 | auto *FnTy = |
Alexey Bataev | 8f7c1b0 | 2014-12-05 04:09:23 +0000 | [diff] [blame] | 1758 | llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg*/ false); |
| 1759 | RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name*/ "__kmpc_cancel_barrier"); |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 1760 | break; |
| 1761 | } |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 1762 | case OMPRTL__kmpc_barrier: { |
Alexey Bataev | 7d5d33e | 2015-07-06 05:50:32 +0000 | [diff] [blame] | 1763 | // Build void __kmpc_barrier(ident_t *loc, kmp_int32 global_tid); |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 1764 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1765 | auto *FnTy = |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 1766 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false); |
| 1767 | RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name*/ "__kmpc_barrier"); |
| 1768 | break; |
| 1769 | } |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 1770 | case OMPRTL__kmpc_for_static_fini: { |
| 1771 | // Build void __kmpc_for_static_fini(ident_t *loc, kmp_int32 global_tid); |
| 1772 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1773 | auto *FnTy = |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 1774 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false); |
| 1775 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_for_static_fini"); |
| 1776 | break; |
| 1777 | } |
Alexey Bataev | b205978 | 2014-10-13 08:23:51 +0000 | [diff] [blame] | 1778 | case OMPRTL__kmpc_push_num_threads: { |
| 1779 | // Build void __kmpc_push_num_threads(ident_t *loc, kmp_int32 global_tid, |
| 1780 | // kmp_int32 num_threads) |
| 1781 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty, |
| 1782 | CGM.Int32Ty}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1783 | auto *FnTy = |
Alexey Bataev | b205978 | 2014-10-13 08:23:51 +0000 | [diff] [blame] | 1784 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false); |
| 1785 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_push_num_threads"); |
| 1786 | break; |
| 1787 | } |
Alexey Bataev | d74d060 | 2014-10-13 06:02:40 +0000 | [diff] [blame] | 1788 | case OMPRTL__kmpc_serialized_parallel: { |
| 1789 | // Build void __kmpc_serialized_parallel(ident_t *loc, kmp_int32 |
| 1790 | // global_tid); |
| 1791 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1792 | auto *FnTy = |
Alexey Bataev | d74d060 | 2014-10-13 06:02:40 +0000 | [diff] [blame] | 1793 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false); |
| 1794 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_serialized_parallel"); |
| 1795 | break; |
| 1796 | } |
| 1797 | case OMPRTL__kmpc_end_serialized_parallel: { |
| 1798 | // Build void __kmpc_end_serialized_parallel(ident_t *loc, kmp_int32 |
| 1799 | // global_tid); |
| 1800 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1801 | auto *FnTy = |
Alexey Bataev | d74d060 | 2014-10-13 06:02:40 +0000 | [diff] [blame] | 1802 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false); |
| 1803 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_end_serialized_parallel"); |
| 1804 | break; |
| 1805 | } |
Alexey Bataev | cc37cc1 | 2014-11-20 04:34:54 +0000 | [diff] [blame] | 1806 | case OMPRTL__kmpc_flush: { |
Alexey Bataev | d76df6d | 2015-02-24 12:55:09 +0000 | [diff] [blame] | 1807 | // Build void __kmpc_flush(ident_t *loc); |
Alexey Bataev | cc37cc1 | 2014-11-20 04:34:54 +0000 | [diff] [blame] | 1808 | llvm::Type *TypeParams[] = {getIdentTyPointerTy()}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1809 | auto *FnTy = |
Alexey Bataev | d76df6d | 2015-02-24 12:55:09 +0000 | [diff] [blame] | 1810 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false); |
Alexey Bataev | cc37cc1 | 2014-11-20 04:34:54 +0000 | [diff] [blame] | 1811 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_flush"); |
| 1812 | break; |
| 1813 | } |
Alexey Bataev | 8d69065 | 2014-12-04 07:23:53 +0000 | [diff] [blame] | 1814 | case OMPRTL__kmpc_master: { |
| 1815 | // Build kmp_int32 __kmpc_master(ident_t *loc, kmp_int32 global_tid); |
| 1816 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1817 | auto *FnTy = |
Alexey Bataev | 8d69065 | 2014-12-04 07:23:53 +0000 | [diff] [blame] | 1818 | llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg=*/false); |
| 1819 | RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_master"); |
| 1820 | break; |
| 1821 | } |
| 1822 | case OMPRTL__kmpc_end_master: { |
| 1823 | // Build void __kmpc_end_master(ident_t *loc, kmp_int32 global_tid); |
| 1824 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1825 | auto *FnTy = |
Alexey Bataev | 8d69065 | 2014-12-04 07:23:53 +0000 | [diff] [blame] | 1826 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false); |
| 1827 | RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_end_master"); |
| 1828 | break; |
| 1829 | } |
Alexey Bataev | 9f797f3 | 2015-02-05 05:57:51 +0000 | [diff] [blame] | 1830 | case OMPRTL__kmpc_omp_taskyield: { |
| 1831 | // Build kmp_int32 __kmpc_omp_taskyield(ident_t *, kmp_int32 global_tid, |
| 1832 | // int end_part); |
| 1833 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty, CGM.IntTy}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1834 | auto *FnTy = |
Alexey Bataev | 9f797f3 | 2015-02-05 05:57:51 +0000 | [diff] [blame] | 1835 | llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg=*/false); |
| 1836 | RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_omp_taskyield"); |
| 1837 | break; |
| 1838 | } |
Alexey Bataev | 6956e2e | 2015-02-05 06:35:41 +0000 | [diff] [blame] | 1839 | case OMPRTL__kmpc_single: { |
| 1840 | // Build kmp_int32 __kmpc_single(ident_t *loc, kmp_int32 global_tid); |
| 1841 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1842 | auto *FnTy = |
Alexey Bataev | 6956e2e | 2015-02-05 06:35:41 +0000 | [diff] [blame] | 1843 | llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg=*/false); |
| 1844 | RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_single"); |
| 1845 | break; |
| 1846 | } |
| 1847 | case OMPRTL__kmpc_end_single: { |
| 1848 | // Build void __kmpc_end_single(ident_t *loc, kmp_int32 global_tid); |
| 1849 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1850 | auto *FnTy = |
Alexey Bataev | 6956e2e | 2015-02-05 06:35:41 +0000 | [diff] [blame] | 1851 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false); |
| 1852 | RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_end_single"); |
| 1853 | break; |
| 1854 | } |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 1855 | case OMPRTL__kmpc_omp_task_alloc: { |
| 1856 | // Build kmp_task_t *__kmpc_omp_task_alloc(ident_t *, kmp_int32 gtid, |
| 1857 | // kmp_int32 flags, size_t sizeof_kmp_task_t, size_t sizeof_shareds, |
| 1858 | // kmp_routine_entry_t *task_entry); |
| 1859 | assert(KmpRoutineEntryPtrTy != nullptr && |
| 1860 | "Type kmp_routine_entry_t must be created."); |
| 1861 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty, CGM.Int32Ty, |
| 1862 | CGM.SizeTy, CGM.SizeTy, KmpRoutineEntryPtrTy}; |
| 1863 | // Return void * and then cast to particular kmp_task_t type. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1864 | auto *FnTy = |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 1865 | llvm::FunctionType::get(CGM.VoidPtrTy, TypeParams, /*isVarArg=*/false); |
| 1866 | RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_omp_task_alloc"); |
| 1867 | break; |
| 1868 | } |
| 1869 | case OMPRTL__kmpc_omp_task: { |
| 1870 | // Build kmp_int32 __kmpc_omp_task(ident_t *, kmp_int32 gtid, kmp_task_t |
| 1871 | // *new_task); |
| 1872 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty, |
| 1873 | CGM.VoidPtrTy}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1874 | auto *FnTy = |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 1875 | llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg=*/false); |
| 1876 | RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_omp_task"); |
| 1877 | break; |
| 1878 | } |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 1879 | case OMPRTL__kmpc_copyprivate: { |
| 1880 | // Build void __kmpc_copyprivate(ident_t *loc, kmp_int32 global_tid, |
Alexey Bataev | 66beaa9 | 2015-04-30 03:47:32 +0000 | [diff] [blame] | 1881 | // size_t cpy_size, void *cpy_data, void(*cpy_func)(void *, void *), |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 1882 | // kmp_int32 didit); |
| 1883 | llvm::Type *CpyTypeParams[] = {CGM.VoidPtrTy, CGM.VoidPtrTy}; |
| 1884 | auto *CpyFnTy = |
| 1885 | llvm::FunctionType::get(CGM.VoidTy, CpyTypeParams, /*isVarArg=*/false); |
Alexey Bataev | 66beaa9 | 2015-04-30 03:47:32 +0000 | [diff] [blame] | 1886 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty, CGM.SizeTy, |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 1887 | CGM.VoidPtrTy, CpyFnTy->getPointerTo(), |
| 1888 | CGM.Int32Ty}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1889 | auto *FnTy = |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 1890 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false); |
| 1891 | RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_copyprivate"); |
| 1892 | break; |
| 1893 | } |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 1894 | case OMPRTL__kmpc_reduce: { |
| 1895 | // Build kmp_int32 __kmpc_reduce(ident_t *loc, kmp_int32 global_tid, |
| 1896 | // kmp_int32 num_vars, size_t reduce_size, void *reduce_data, void |
| 1897 | // (*reduce_func)(void *lhs_data, void *rhs_data), kmp_critical_name *lck); |
| 1898 | llvm::Type *ReduceTypeParams[] = {CGM.VoidPtrTy, CGM.VoidPtrTy}; |
| 1899 | auto *ReduceFnTy = llvm::FunctionType::get(CGM.VoidTy, ReduceTypeParams, |
| 1900 | /*isVarArg=*/false); |
| 1901 | llvm::Type *TypeParams[] = { |
| 1902 | getIdentTyPointerTy(), CGM.Int32Ty, CGM.Int32Ty, CGM.SizeTy, |
| 1903 | CGM.VoidPtrTy, ReduceFnTy->getPointerTo(), |
| 1904 | llvm::PointerType::getUnqual(KmpCriticalNameTy)}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1905 | auto *FnTy = |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 1906 | llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg=*/false); |
| 1907 | RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_reduce"); |
| 1908 | break; |
| 1909 | } |
| 1910 | case OMPRTL__kmpc_reduce_nowait: { |
| 1911 | // Build kmp_int32 __kmpc_reduce_nowait(ident_t *loc, kmp_int32 |
| 1912 | // global_tid, kmp_int32 num_vars, size_t reduce_size, void *reduce_data, |
| 1913 | // void (*reduce_func)(void *lhs_data, void *rhs_data), kmp_critical_name |
| 1914 | // *lck); |
| 1915 | llvm::Type *ReduceTypeParams[] = {CGM.VoidPtrTy, CGM.VoidPtrTy}; |
| 1916 | auto *ReduceFnTy = llvm::FunctionType::get(CGM.VoidTy, ReduceTypeParams, |
| 1917 | /*isVarArg=*/false); |
| 1918 | llvm::Type *TypeParams[] = { |
| 1919 | getIdentTyPointerTy(), CGM.Int32Ty, CGM.Int32Ty, CGM.SizeTy, |
| 1920 | CGM.VoidPtrTy, ReduceFnTy->getPointerTo(), |
| 1921 | llvm::PointerType::getUnqual(KmpCriticalNameTy)}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1922 | auto *FnTy = |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 1923 | llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg=*/false); |
| 1924 | RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_reduce_nowait"); |
| 1925 | break; |
| 1926 | } |
| 1927 | case OMPRTL__kmpc_end_reduce: { |
| 1928 | // Build void __kmpc_end_reduce(ident_t *loc, kmp_int32 global_tid, |
| 1929 | // kmp_critical_name *lck); |
| 1930 | llvm::Type *TypeParams[] = { |
| 1931 | getIdentTyPointerTy(), CGM.Int32Ty, |
| 1932 | llvm::PointerType::getUnqual(KmpCriticalNameTy)}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1933 | auto *FnTy = |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 1934 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false); |
| 1935 | RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_end_reduce"); |
| 1936 | break; |
| 1937 | } |
| 1938 | case OMPRTL__kmpc_end_reduce_nowait: { |
| 1939 | // Build __kmpc_end_reduce_nowait(ident_t *loc, kmp_int32 global_tid, |
| 1940 | // kmp_critical_name *lck); |
| 1941 | llvm::Type *TypeParams[] = { |
| 1942 | getIdentTyPointerTy(), CGM.Int32Ty, |
| 1943 | llvm::PointerType::getUnqual(KmpCriticalNameTy)}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1944 | auto *FnTy = |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 1945 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false); |
| 1946 | RTLFn = |
| 1947 | CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_end_reduce_nowait"); |
| 1948 | break; |
| 1949 | } |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 1950 | case OMPRTL__kmpc_omp_task_begin_if0: { |
| 1951 | // Build void __kmpc_omp_task(ident_t *, kmp_int32 gtid, kmp_task_t |
| 1952 | // *new_task); |
| 1953 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty, |
| 1954 | CGM.VoidPtrTy}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1955 | auto *FnTy = |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 1956 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false); |
| 1957 | RTLFn = |
| 1958 | CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_omp_task_begin_if0"); |
| 1959 | break; |
| 1960 | } |
| 1961 | case OMPRTL__kmpc_omp_task_complete_if0: { |
| 1962 | // Build void __kmpc_omp_task(ident_t *, kmp_int32 gtid, kmp_task_t |
| 1963 | // *new_task); |
| 1964 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty, |
| 1965 | CGM.VoidPtrTy}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1966 | auto *FnTy = |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 1967 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false); |
| 1968 | RTLFn = CGM.CreateRuntimeFunction(FnTy, |
| 1969 | /*Name=*/"__kmpc_omp_task_complete_if0"); |
| 1970 | break; |
| 1971 | } |
Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 1972 | case OMPRTL__kmpc_ordered: { |
| 1973 | // Build void __kmpc_ordered(ident_t *loc, kmp_int32 global_tid); |
| 1974 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1975 | auto *FnTy = |
Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 1976 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false); |
| 1977 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_ordered"); |
| 1978 | break; |
| 1979 | } |
| 1980 | case OMPRTL__kmpc_end_ordered: { |
Alexey Bataev | c30dd2d | 2015-06-18 12:14:09 +0000 | [diff] [blame] | 1981 | // Build void __kmpc_end_ordered(ident_t *loc, kmp_int32 global_tid); |
Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 1982 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1983 | auto *FnTy = |
Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 1984 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false); |
| 1985 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_end_ordered"); |
| 1986 | break; |
| 1987 | } |
Alexey Bataev | 8b8e202 | 2015-04-27 05:22:09 +0000 | [diff] [blame] | 1988 | case OMPRTL__kmpc_omp_taskwait: { |
| 1989 | // Build kmp_int32 __kmpc_omp_taskwait(ident_t *loc, kmp_int32 global_tid); |
| 1990 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1991 | auto *FnTy = |
Alexey Bataev | 8b8e202 | 2015-04-27 05:22:09 +0000 | [diff] [blame] | 1992 | llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg=*/false); |
| 1993 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_omp_taskwait"); |
| 1994 | break; |
| 1995 | } |
Alexey Bataev | c30dd2d | 2015-06-18 12:14:09 +0000 | [diff] [blame] | 1996 | case OMPRTL__kmpc_taskgroup: { |
| 1997 | // Build void __kmpc_taskgroup(ident_t *loc, kmp_int32 global_tid); |
| 1998 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1999 | auto *FnTy = |
Alexey Bataev | c30dd2d | 2015-06-18 12:14:09 +0000 | [diff] [blame] | 2000 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false); |
| 2001 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_taskgroup"); |
| 2002 | break; |
| 2003 | } |
| 2004 | case OMPRTL__kmpc_end_taskgroup: { |
| 2005 | // Build void __kmpc_end_taskgroup(ident_t *loc, kmp_int32 global_tid); |
| 2006 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2007 | auto *FnTy = |
Alexey Bataev | c30dd2d | 2015-06-18 12:14:09 +0000 | [diff] [blame] | 2008 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false); |
| 2009 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_end_taskgroup"); |
| 2010 | break; |
| 2011 | } |
Alexey Bataev | 7f210c6 | 2015-06-18 13:40:03 +0000 | [diff] [blame] | 2012 | case OMPRTL__kmpc_push_proc_bind: { |
| 2013 | // Build void __kmpc_push_proc_bind(ident_t *loc, kmp_int32 global_tid, |
| 2014 | // int proc_bind) |
| 2015 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty, CGM.IntTy}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2016 | auto *FnTy = |
Alexey Bataev | 7f210c6 | 2015-06-18 13:40:03 +0000 | [diff] [blame] | 2017 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false); |
| 2018 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_push_proc_bind"); |
| 2019 | break; |
| 2020 | } |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 2021 | case OMPRTL__kmpc_omp_task_with_deps: { |
| 2022 | // Build kmp_int32 __kmpc_omp_task_with_deps(ident_t *, kmp_int32 gtid, |
| 2023 | // kmp_task_t *new_task, kmp_int32 ndeps, kmp_depend_info_t *dep_list, |
| 2024 | // kmp_int32 ndeps_noalias, kmp_depend_info_t *noalias_dep_list); |
| 2025 | llvm::Type *TypeParams[] = { |
| 2026 | getIdentTyPointerTy(), CGM.Int32Ty, CGM.VoidPtrTy, CGM.Int32Ty, |
| 2027 | CGM.VoidPtrTy, CGM.Int32Ty, CGM.VoidPtrTy}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2028 | auto *FnTy = |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 2029 | llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg=*/false); |
| 2030 | RTLFn = |
| 2031 | CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_omp_task_with_deps"); |
| 2032 | break; |
| 2033 | } |
| 2034 | case OMPRTL__kmpc_omp_wait_deps: { |
| 2035 | // Build void __kmpc_omp_wait_deps(ident_t *, kmp_int32 gtid, |
| 2036 | // kmp_int32 ndeps, kmp_depend_info_t *dep_list, kmp_int32 ndeps_noalias, |
| 2037 | // kmp_depend_info_t *noalias_dep_list); |
| 2038 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty, |
| 2039 | CGM.Int32Ty, CGM.VoidPtrTy, |
| 2040 | CGM.Int32Ty, CGM.VoidPtrTy}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2041 | auto *FnTy = |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 2042 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false); |
| 2043 | RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_omp_wait_deps"); |
| 2044 | break; |
| 2045 | } |
Alexey Bataev | 0f34da1 | 2015-07-02 04:17:07 +0000 | [diff] [blame] | 2046 | case OMPRTL__kmpc_cancellationpoint: { |
| 2047 | // Build kmp_int32 __kmpc_cancellationpoint(ident_t *loc, kmp_int32 |
| 2048 | // global_tid, kmp_int32 cncl_kind) |
| 2049 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty, CGM.IntTy}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2050 | auto *FnTy = |
Alexey Bataev | 0f34da1 | 2015-07-02 04:17:07 +0000 | [diff] [blame] | 2051 | llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg*/ false); |
| 2052 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_cancellationpoint"); |
| 2053 | break; |
| 2054 | } |
Alexey Bataev | 7d5d33e | 2015-07-06 05:50:32 +0000 | [diff] [blame] | 2055 | case OMPRTL__kmpc_cancel: { |
| 2056 | // Build kmp_int32 __kmpc_cancel(ident_t *loc, kmp_int32 global_tid, |
| 2057 | // kmp_int32 cncl_kind) |
| 2058 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty, CGM.IntTy}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2059 | auto *FnTy = |
Alexey Bataev | 7d5d33e | 2015-07-06 05:50:32 +0000 | [diff] [blame] | 2060 | llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg*/ false); |
| 2061 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_cancel"); |
| 2062 | break; |
| 2063 | } |
Carlo Bertolli | 430d8ec | 2016-03-03 20:34:23 +0000 | [diff] [blame] | 2064 | case OMPRTL__kmpc_push_num_teams: { |
| 2065 | // Build void kmpc_push_num_teams (ident_t loc, kmp_int32 global_tid, |
| 2066 | // kmp_int32 num_teams, kmp_int32 num_threads) |
| 2067 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty, CGM.Int32Ty, |
| 2068 | CGM.Int32Ty}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2069 | auto *FnTy = |
Carlo Bertolli | 430d8ec | 2016-03-03 20:34:23 +0000 | [diff] [blame] | 2070 | llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg*/ false); |
| 2071 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_push_num_teams"); |
| 2072 | break; |
| 2073 | } |
| 2074 | case OMPRTL__kmpc_fork_teams: { |
| 2075 | // Build void __kmpc_fork_teams(ident_t *loc, kmp_int32 argc, kmpc_micro |
| 2076 | // microtask, ...); |
| 2077 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty, |
| 2078 | getKmpc_MicroPointerTy()}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2079 | auto *FnTy = |
Carlo Bertolli | 430d8ec | 2016-03-03 20:34:23 +0000 | [diff] [blame] | 2080 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ true); |
| 2081 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_fork_teams"); |
| 2082 | break; |
| 2083 | } |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 2084 | case OMPRTL__kmpc_taskloop: { |
| 2085 | // Build void __kmpc_taskloop(ident_t *loc, int gtid, kmp_task_t *task, int |
| 2086 | // if_val, kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st, int nogroup, int |
| 2087 | // sched, kmp_uint64 grainsize, void *task_dup); |
| 2088 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), |
| 2089 | CGM.IntTy, |
| 2090 | CGM.VoidPtrTy, |
| 2091 | CGM.IntTy, |
| 2092 | CGM.Int64Ty->getPointerTo(), |
| 2093 | CGM.Int64Ty->getPointerTo(), |
| 2094 | CGM.Int64Ty, |
| 2095 | CGM.IntTy, |
| 2096 | CGM.IntTy, |
| 2097 | CGM.Int64Ty, |
| 2098 | CGM.VoidPtrTy}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2099 | auto *FnTy = |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 2100 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false); |
| 2101 | RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_taskloop"); |
| 2102 | break; |
| 2103 | } |
Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 2104 | case OMPRTL__kmpc_doacross_init: { |
| 2105 | // Build void __kmpc_doacross_init(ident_t *loc, kmp_int32 gtid, kmp_int32 |
| 2106 | // num_dims, struct kmp_dim *dims); |
| 2107 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), |
| 2108 | CGM.Int32Ty, |
| 2109 | CGM.Int32Ty, |
| 2110 | CGM.VoidPtrTy}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2111 | auto *FnTy = |
Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 2112 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false); |
| 2113 | RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_doacross_init"); |
| 2114 | break; |
| 2115 | } |
| 2116 | case OMPRTL__kmpc_doacross_fini: { |
| 2117 | // Build void __kmpc_doacross_fini(ident_t *loc, kmp_int32 gtid); |
| 2118 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2119 | auto *FnTy = |
Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 2120 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false); |
| 2121 | RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_doacross_fini"); |
| 2122 | break; |
| 2123 | } |
| 2124 | case OMPRTL__kmpc_doacross_post: { |
| 2125 | // Build void __kmpc_doacross_post(ident_t *loc, kmp_int32 gtid, kmp_int64 |
| 2126 | // *vec); |
| 2127 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty, |
| 2128 | CGM.Int64Ty->getPointerTo()}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2129 | auto *FnTy = |
Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 2130 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false); |
| 2131 | RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_doacross_post"); |
| 2132 | break; |
| 2133 | } |
| 2134 | case OMPRTL__kmpc_doacross_wait: { |
| 2135 | // Build void __kmpc_doacross_wait(ident_t *loc, kmp_int32 gtid, kmp_int64 |
| 2136 | // *vec); |
| 2137 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty, |
| 2138 | CGM.Int64Ty->getPointerTo()}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2139 | auto *FnTy = |
Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 2140 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false); |
| 2141 | RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_doacross_wait"); |
| 2142 | break; |
| 2143 | } |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 2144 | case OMPRTL__kmpc_task_reduction_init: { |
| 2145 | // Build void *__kmpc_task_reduction_init(int gtid, int num_data, void |
| 2146 | // *data); |
| 2147 | llvm::Type *TypeParams[] = {CGM.IntTy, CGM.IntTy, CGM.VoidPtrTy}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2148 | auto *FnTy = |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 2149 | llvm::FunctionType::get(CGM.VoidPtrTy, TypeParams, /*isVarArg=*/false); |
| 2150 | RTLFn = |
| 2151 | CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_task_reduction_init"); |
| 2152 | break; |
| 2153 | } |
| 2154 | case OMPRTL__kmpc_task_reduction_get_th_data: { |
| 2155 | // Build void *__kmpc_task_reduction_get_th_data(int gtid, void *tg, void |
| 2156 | // *d); |
| 2157 | llvm::Type *TypeParams[] = {CGM.IntTy, CGM.VoidPtrTy, CGM.VoidPtrTy}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2158 | auto *FnTy = |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 2159 | llvm::FunctionType::get(CGM.VoidPtrTy, TypeParams, /*isVarArg=*/false); |
| 2160 | RTLFn = CGM.CreateRuntimeFunction( |
| 2161 | FnTy, /*Name=*/"__kmpc_task_reduction_get_th_data"); |
| 2162 | break; |
| 2163 | } |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 2164 | case OMPRTL__tgt_target: { |
George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 2165 | // Build int32_t __tgt_target(int64_t device_id, void *host_ptr, int32_t |
| 2166 | // 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] | 2167 | // *arg_types); |
George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 2168 | llvm::Type *TypeParams[] = {CGM.Int64Ty, |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 2169 | CGM.VoidPtrTy, |
| 2170 | CGM.Int32Ty, |
| 2171 | CGM.VoidPtrPtrTy, |
| 2172 | CGM.VoidPtrPtrTy, |
| 2173 | CGM.SizeTy->getPointerTo(), |
George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 2174 | CGM.Int64Ty->getPointerTo()}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2175 | auto *FnTy = |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 2176 | llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg*/ false); |
| 2177 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__tgt_target"); |
| 2178 | break; |
| 2179 | } |
Alexey Bataev | a9f77c6 | 2017-12-13 21:04:20 +0000 | [diff] [blame] | 2180 | case OMPRTL__tgt_target_nowait: { |
| 2181 | // Build int32_t __tgt_target_nowait(int64_t device_id, void *host_ptr, |
| 2182 | // int32_t arg_num, void** args_base, void **args, size_t *arg_sizes, |
| 2183 | // int64_t *arg_types); |
| 2184 | llvm::Type *TypeParams[] = {CGM.Int64Ty, |
| 2185 | CGM.VoidPtrTy, |
| 2186 | CGM.Int32Ty, |
| 2187 | CGM.VoidPtrPtrTy, |
| 2188 | CGM.VoidPtrPtrTy, |
| 2189 | CGM.SizeTy->getPointerTo(), |
| 2190 | CGM.Int64Ty->getPointerTo()}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2191 | auto *FnTy = |
Alexey Bataev | a9f77c6 | 2017-12-13 21:04:20 +0000 | [diff] [blame] | 2192 | llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg*/ false); |
| 2193 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__tgt_target_nowait"); |
| 2194 | break; |
| 2195 | } |
Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 2196 | case OMPRTL__tgt_target_teams: { |
George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 2197 | // 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] | 2198 | // 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] | 2199 | // int64_t *arg_types, int32_t num_teams, int32_t thread_limit); |
| 2200 | llvm::Type *TypeParams[] = {CGM.Int64Ty, |
Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 2201 | CGM.VoidPtrTy, |
| 2202 | CGM.Int32Ty, |
| 2203 | CGM.VoidPtrPtrTy, |
| 2204 | CGM.VoidPtrPtrTy, |
| 2205 | CGM.SizeTy->getPointerTo(), |
George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 2206 | CGM.Int64Ty->getPointerTo(), |
Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 2207 | CGM.Int32Ty, |
| 2208 | CGM.Int32Ty}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2209 | auto *FnTy = |
Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 2210 | llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg*/ false); |
| 2211 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__tgt_target_teams"); |
| 2212 | break; |
| 2213 | } |
Alexey Bataev | a9f77c6 | 2017-12-13 21:04:20 +0000 | [diff] [blame] | 2214 | case OMPRTL__tgt_target_teams_nowait: { |
| 2215 | // Build int32_t __tgt_target_teams_nowait(int64_t device_id, void |
| 2216 | // *host_ptr, int32_t arg_num, void** args_base, void **args, size_t |
| 2217 | // *arg_sizes, int64_t *arg_types, int32_t num_teams, int32_t thread_limit); |
| 2218 | llvm::Type *TypeParams[] = {CGM.Int64Ty, |
| 2219 | CGM.VoidPtrTy, |
| 2220 | CGM.Int32Ty, |
| 2221 | CGM.VoidPtrPtrTy, |
| 2222 | CGM.VoidPtrPtrTy, |
| 2223 | CGM.SizeTy->getPointerTo(), |
| 2224 | CGM.Int64Ty->getPointerTo(), |
| 2225 | CGM.Int32Ty, |
| 2226 | CGM.Int32Ty}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2227 | auto *FnTy = |
Alexey Bataev | a9f77c6 | 2017-12-13 21:04:20 +0000 | [diff] [blame] | 2228 | llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg*/ false); |
| 2229 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__tgt_target_teams_nowait"); |
| 2230 | break; |
| 2231 | } |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 2232 | case OMPRTL__tgt_register_lib: { |
| 2233 | // Build void __tgt_register_lib(__tgt_bin_desc *desc); |
| 2234 | QualType ParamTy = |
| 2235 | CGM.getContext().getPointerType(getTgtBinaryDescriptorQTy()); |
| 2236 | llvm::Type *TypeParams[] = {CGM.getTypes().ConvertTypeForMem(ParamTy)}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2237 | auto *FnTy = |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 2238 | llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg*/ false); |
| 2239 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__tgt_register_lib"); |
| 2240 | break; |
| 2241 | } |
| 2242 | case OMPRTL__tgt_unregister_lib: { |
| 2243 | // Build void __tgt_unregister_lib(__tgt_bin_desc *desc); |
| 2244 | QualType ParamTy = |
| 2245 | CGM.getContext().getPointerType(getTgtBinaryDescriptorQTy()); |
| 2246 | llvm::Type *TypeParams[] = {CGM.getTypes().ConvertTypeForMem(ParamTy)}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2247 | auto *FnTy = |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 2248 | llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg*/ false); |
| 2249 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__tgt_unregister_lib"); |
| 2250 | break; |
| 2251 | } |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 2252 | case OMPRTL__tgt_target_data_begin: { |
George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 2253 | // Build void __tgt_target_data_begin(int64_t device_id, int32_t arg_num, |
| 2254 | // void** args_base, void **args, size_t *arg_sizes, int64_t *arg_types); |
| 2255 | llvm::Type *TypeParams[] = {CGM.Int64Ty, |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 2256 | CGM.Int32Ty, |
| 2257 | CGM.VoidPtrPtrTy, |
| 2258 | CGM.VoidPtrPtrTy, |
| 2259 | CGM.SizeTy->getPointerTo(), |
George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 2260 | CGM.Int64Ty->getPointerTo()}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2261 | auto *FnTy = |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 2262 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false); |
| 2263 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__tgt_target_data_begin"); |
| 2264 | break; |
| 2265 | } |
Alexey Bataev | 0cc6b8e | 2017-12-14 17:00:17 +0000 | [diff] [blame] | 2266 | case OMPRTL__tgt_target_data_begin_nowait: { |
| 2267 | // Build void __tgt_target_data_begin_nowait(int64_t device_id, int32_t |
| 2268 | // arg_num, void** args_base, void **args, size_t *arg_sizes, int64_t |
| 2269 | // *arg_types); |
| 2270 | llvm::Type *TypeParams[] = {CGM.Int64Ty, |
| 2271 | CGM.Int32Ty, |
| 2272 | CGM.VoidPtrPtrTy, |
| 2273 | CGM.VoidPtrPtrTy, |
| 2274 | CGM.SizeTy->getPointerTo(), |
| 2275 | CGM.Int64Ty->getPointerTo()}; |
| 2276 | auto *FnTy = |
| 2277 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false); |
| 2278 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__tgt_target_data_begin_nowait"); |
| 2279 | break; |
| 2280 | } |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 2281 | case OMPRTL__tgt_target_data_end: { |
George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 2282 | // Build void __tgt_target_data_end(int64_t device_id, int32_t arg_num, |
| 2283 | // void** args_base, void **args, size_t *arg_sizes, int64_t *arg_types); |
| 2284 | llvm::Type *TypeParams[] = {CGM.Int64Ty, |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 2285 | CGM.Int32Ty, |
| 2286 | CGM.VoidPtrPtrTy, |
| 2287 | CGM.VoidPtrPtrTy, |
| 2288 | CGM.SizeTy->getPointerTo(), |
George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 2289 | CGM.Int64Ty->getPointerTo()}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2290 | auto *FnTy = |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 2291 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false); |
| 2292 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__tgt_target_data_end"); |
| 2293 | break; |
| 2294 | } |
Alexey Bataev | 0cc6b8e | 2017-12-14 17:00:17 +0000 | [diff] [blame] | 2295 | case OMPRTL__tgt_target_data_end_nowait: { |
| 2296 | // Build void __tgt_target_data_end_nowait(int64_t device_id, int32_t |
| 2297 | // arg_num, void** args_base, void **args, size_t *arg_sizes, int64_t |
| 2298 | // *arg_types); |
| 2299 | llvm::Type *TypeParams[] = {CGM.Int64Ty, |
| 2300 | CGM.Int32Ty, |
| 2301 | CGM.VoidPtrPtrTy, |
| 2302 | CGM.VoidPtrPtrTy, |
| 2303 | CGM.SizeTy->getPointerTo(), |
| 2304 | CGM.Int64Ty->getPointerTo()}; |
| 2305 | auto *FnTy = |
| 2306 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false); |
| 2307 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__tgt_target_data_end_nowait"); |
| 2308 | break; |
| 2309 | } |
Samuel Antao | 8d2d730 | 2016-05-26 18:30:22 +0000 | [diff] [blame] | 2310 | case OMPRTL__tgt_target_data_update: { |
George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 2311 | // Build void __tgt_target_data_update(int64_t device_id, int32_t arg_num, |
| 2312 | // void** args_base, void **args, size_t *arg_sizes, int64_t *arg_types); |
| 2313 | llvm::Type *TypeParams[] = {CGM.Int64Ty, |
Samuel Antao | 8d2d730 | 2016-05-26 18:30:22 +0000 | [diff] [blame] | 2314 | CGM.Int32Ty, |
| 2315 | CGM.VoidPtrPtrTy, |
| 2316 | CGM.VoidPtrPtrTy, |
| 2317 | CGM.SizeTy->getPointerTo(), |
George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 2318 | CGM.Int64Ty->getPointerTo()}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2319 | auto *FnTy = |
Samuel Antao | 8d2d730 | 2016-05-26 18:30:22 +0000 | [diff] [blame] | 2320 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false); |
| 2321 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__tgt_target_data_update"); |
| 2322 | break; |
| 2323 | } |
Alexey Bataev | 0cc6b8e | 2017-12-14 17:00:17 +0000 | [diff] [blame] | 2324 | case OMPRTL__tgt_target_data_update_nowait: { |
| 2325 | // Build void __tgt_target_data_update_nowait(int64_t device_id, int32_t |
| 2326 | // arg_num, void** args_base, void **args, size_t *arg_sizes, int64_t |
| 2327 | // *arg_types); |
| 2328 | llvm::Type *TypeParams[] = {CGM.Int64Ty, |
| 2329 | CGM.Int32Ty, |
| 2330 | CGM.VoidPtrPtrTy, |
| 2331 | CGM.VoidPtrPtrTy, |
| 2332 | CGM.SizeTy->getPointerTo(), |
| 2333 | CGM.Int64Ty->getPointerTo()}; |
| 2334 | auto *FnTy = |
| 2335 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false); |
| 2336 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__tgt_target_data_update_nowait"); |
| 2337 | break; |
| 2338 | } |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 2339 | } |
Alexey Bataev | 50b3c95 | 2016-02-19 10:38:26 +0000 | [diff] [blame] | 2340 | assert(RTLFn && "Unable to find OpenMP runtime function"); |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 2341 | return RTLFn; |
| 2342 | } |
Alexey Bataev | 3a3bf0b | 2014-09-22 10:01:53 +0000 | [diff] [blame] | 2343 | |
Alexander Musman | 21212e4 | 2015-03-13 10:38:23 +0000 | [diff] [blame] | 2344 | llvm::Constant *CGOpenMPRuntime::createForStaticInitFunction(unsigned IVSize, |
| 2345 | bool IVSigned) { |
| 2346 | assert((IVSize == 32 || IVSize == 64) && |
| 2347 | "IV size is not compatible with the omp runtime"); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2348 | StringRef Name = IVSize == 32 ? (IVSigned ? "__kmpc_for_static_init_4" |
| 2349 | : "__kmpc_for_static_init_4u") |
| 2350 | : (IVSigned ? "__kmpc_for_static_init_8" |
| 2351 | : "__kmpc_for_static_init_8u"); |
| 2352 | llvm::Type *ITy = IVSize == 32 ? CGM.Int32Ty : CGM.Int64Ty; |
| 2353 | auto *PtrTy = llvm::PointerType::getUnqual(ITy); |
Alexander Musman | 21212e4 | 2015-03-13 10:38:23 +0000 | [diff] [blame] | 2354 | llvm::Type *TypeParams[] = { |
| 2355 | getIdentTyPointerTy(), // loc |
| 2356 | CGM.Int32Ty, // tid |
| 2357 | CGM.Int32Ty, // schedtype |
| 2358 | llvm::PointerType::getUnqual(CGM.Int32Ty), // p_lastiter |
| 2359 | PtrTy, // p_lower |
| 2360 | PtrTy, // p_upper |
| 2361 | PtrTy, // p_stride |
| 2362 | ITy, // incr |
| 2363 | ITy // chunk |
| 2364 | }; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2365 | auto *FnTy = |
Alexander Musman | 21212e4 | 2015-03-13 10:38:23 +0000 | [diff] [blame] | 2366 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false); |
| 2367 | return CGM.CreateRuntimeFunction(FnTy, Name); |
| 2368 | } |
| 2369 | |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 2370 | llvm::Constant *CGOpenMPRuntime::createDispatchInitFunction(unsigned IVSize, |
| 2371 | bool IVSigned) { |
| 2372 | assert((IVSize == 32 || IVSize == 64) && |
| 2373 | "IV size is not compatible with the omp runtime"); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2374 | StringRef Name = |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 2375 | IVSize == 32 |
| 2376 | ? (IVSigned ? "__kmpc_dispatch_init_4" : "__kmpc_dispatch_init_4u") |
| 2377 | : (IVSigned ? "__kmpc_dispatch_init_8" : "__kmpc_dispatch_init_8u"); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2378 | llvm::Type *ITy = IVSize == 32 ? CGM.Int32Ty : CGM.Int64Ty; |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 2379 | llvm::Type *TypeParams[] = { getIdentTyPointerTy(), // loc |
| 2380 | CGM.Int32Ty, // tid |
| 2381 | CGM.Int32Ty, // schedtype |
| 2382 | ITy, // lower |
| 2383 | ITy, // upper |
| 2384 | ITy, // stride |
| 2385 | ITy // chunk |
| 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.VoidTy, TypeParams, /*isVarArg*/ false); |
| 2389 | return CGM.CreateRuntimeFunction(FnTy, Name); |
| 2390 | } |
| 2391 | |
Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 2392 | llvm::Constant *CGOpenMPRuntime::createDispatchFiniFunction(unsigned IVSize, |
| 2393 | bool IVSigned) { |
| 2394 | assert((IVSize == 32 || IVSize == 64) && |
| 2395 | "IV size is not compatible with the omp runtime"); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2396 | StringRef Name = |
Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 2397 | IVSize == 32 |
| 2398 | ? (IVSigned ? "__kmpc_dispatch_fini_4" : "__kmpc_dispatch_fini_4u") |
| 2399 | : (IVSigned ? "__kmpc_dispatch_fini_8" : "__kmpc_dispatch_fini_8u"); |
| 2400 | llvm::Type *TypeParams[] = { |
| 2401 | getIdentTyPointerTy(), // loc |
| 2402 | CGM.Int32Ty, // tid |
| 2403 | }; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2404 | auto *FnTy = |
Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 2405 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false); |
| 2406 | return CGM.CreateRuntimeFunction(FnTy, Name); |
| 2407 | } |
| 2408 | |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 2409 | llvm::Constant *CGOpenMPRuntime::createDispatchNextFunction(unsigned IVSize, |
| 2410 | bool IVSigned) { |
| 2411 | assert((IVSize == 32 || IVSize == 64) && |
| 2412 | "IV size is not compatible with the omp runtime"); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2413 | StringRef Name = |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 2414 | IVSize == 32 |
| 2415 | ? (IVSigned ? "__kmpc_dispatch_next_4" : "__kmpc_dispatch_next_4u") |
| 2416 | : (IVSigned ? "__kmpc_dispatch_next_8" : "__kmpc_dispatch_next_8u"); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2417 | llvm::Type *ITy = IVSize == 32 ? CGM.Int32Ty : CGM.Int64Ty; |
| 2418 | auto *PtrTy = llvm::PointerType::getUnqual(ITy); |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 2419 | llvm::Type *TypeParams[] = { |
| 2420 | getIdentTyPointerTy(), // loc |
| 2421 | CGM.Int32Ty, // tid |
| 2422 | llvm::PointerType::getUnqual(CGM.Int32Ty), // p_lastiter |
| 2423 | PtrTy, // p_lower |
| 2424 | PtrTy, // p_upper |
| 2425 | PtrTy // p_stride |
| 2426 | }; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2427 | auto *FnTy = |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 2428 | llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg*/ false); |
| 2429 | return CGM.CreateRuntimeFunction(FnTy, Name); |
| 2430 | } |
| 2431 | |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 2432 | Address CGOpenMPRuntime::getAddrOfDeclareTargetLink(const VarDecl *VD) { |
| 2433 | if (CGM.getLangOpts().OpenMPSimd) |
| 2434 | return Address::invalid(); |
Alexey Bataev | 92327c5 | 2018-03-26 16:40:55 +0000 | [diff] [blame] | 2435 | llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res = |
Alexey Bataev | 97b7221 | 2018-08-14 18:31:20 +0000 | [diff] [blame] | 2436 | OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD); |
Alexey Bataev | 92327c5 | 2018-03-26 16:40:55 +0000 | [diff] [blame] | 2437 | if (Res && *Res == OMPDeclareTargetDeclAttr::MT_Link) { |
| 2438 | SmallString<64> PtrName; |
| 2439 | { |
| 2440 | llvm::raw_svector_ostream OS(PtrName); |
| 2441 | OS << CGM.getMangledName(GlobalDecl(VD)) << "_decl_tgt_link_ptr"; |
| 2442 | } |
| 2443 | llvm::Value *Ptr = CGM.getModule().getNamedValue(PtrName); |
| 2444 | if (!Ptr) { |
| 2445 | QualType PtrTy = CGM.getContext().getPointerType(VD->getType()); |
| 2446 | Ptr = getOrCreateInternalVariable(CGM.getTypes().ConvertTypeForMem(PtrTy), |
| 2447 | PtrName); |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 2448 | if (!CGM.getLangOpts().OpenMPIsDevice) { |
| 2449 | auto *GV = cast<llvm::GlobalVariable>(Ptr); |
| 2450 | GV->setLinkage(llvm::GlobalValue::ExternalLinkage); |
| 2451 | GV->setInitializer(CGM.GetAddrOfGlobal(VD)); |
| 2452 | } |
| 2453 | CGM.addUsedGlobal(cast<llvm::GlobalValue>(Ptr)); |
| 2454 | registerTargetGlobalVariable(VD, cast<llvm::Constant>(Ptr)); |
Alexey Bataev | 92327c5 | 2018-03-26 16:40:55 +0000 | [diff] [blame] | 2455 | } |
| 2456 | return Address(Ptr, CGM.getContext().getDeclAlign(VD)); |
| 2457 | } |
| 2458 | return Address::invalid(); |
| 2459 | } |
| 2460 | |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2461 | llvm::Constant * |
| 2462 | CGOpenMPRuntime::getOrCreateThreadPrivateCache(const VarDecl *VD) { |
Samuel Antao | f8b5012 | 2015-07-13 22:54:53 +0000 | [diff] [blame] | 2463 | assert(!CGM.getLangOpts().OpenMPUseTLS || |
| 2464 | !CGM.getContext().getTargetInfo().isTLSSupported()); |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2465 | // Lookup the entry, lazily creating it if necessary. |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 2466 | std::string Suffix = getName({"cache", ""}); |
| 2467 | return getOrCreateInternalVariable( |
| 2468 | CGM.Int8PtrPtrTy, Twine(CGM.getMangledName(VD)).concat(Suffix)); |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2469 | } |
| 2470 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2471 | Address CGOpenMPRuntime::getAddrOfThreadPrivate(CodeGenFunction &CGF, |
| 2472 | const VarDecl *VD, |
| 2473 | Address VDAddr, |
| 2474 | SourceLocation Loc) { |
Samuel Antao | f8b5012 | 2015-07-13 22:54:53 +0000 | [diff] [blame] | 2475 | if (CGM.getLangOpts().OpenMPUseTLS && |
| 2476 | CGM.getContext().getTargetInfo().isTLSSupported()) |
| 2477 | return VDAddr; |
| 2478 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2479 | llvm::Type *VarTy = VDAddr.getElementType(); |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 2480 | llvm::Value *Args[] = {emitUpdateLocation(CGF, Loc), getThreadID(CGF, Loc), |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2481 | CGF.Builder.CreatePointerCast(VDAddr.getPointer(), |
| 2482 | CGM.Int8PtrTy), |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2483 | CGM.getSize(CGM.GetTargetTypeStoreSize(VarTy)), |
| 2484 | getOrCreateThreadPrivateCache(VD)}; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2485 | return Address(CGF.EmitRuntimeCall( |
| 2486 | createRuntimeFunction(OMPRTL__kmpc_threadprivate_cached), Args), |
| 2487 | VDAddr.getAlignment()); |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2488 | } |
| 2489 | |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 2490 | void CGOpenMPRuntime::emitThreadPrivateVarInit( |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2491 | CodeGenFunction &CGF, Address VDAddr, llvm::Value *Ctor, |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2492 | llvm::Value *CopyCtor, llvm::Value *Dtor, SourceLocation Loc) { |
| 2493 | // Call kmp_int32 __kmpc_global_thread_num(&loc) to init OpenMP runtime |
| 2494 | // library. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2495 | llvm::Value *OMPLoc = emitUpdateLocation(CGF, Loc); |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 2496 | CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_global_thread_num), |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2497 | OMPLoc); |
| 2498 | // Call __kmpc_threadprivate_register(&loc, &var, ctor, cctor/*NULL*/, dtor) |
| 2499 | // to register constructor/destructor for variable. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2500 | llvm::Value *Args[] = { |
| 2501 | OMPLoc, CGF.Builder.CreatePointerCast(VDAddr.getPointer(), CGM.VoidPtrTy), |
| 2502 | Ctor, CopyCtor, Dtor}; |
Alexey Bataev | 1e4b713 | 2014-12-03 12:11:24 +0000 | [diff] [blame] | 2503 | CGF.EmitRuntimeCall( |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 2504 | createRuntimeFunction(OMPRTL__kmpc_threadprivate_register), Args); |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2505 | } |
| 2506 | |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 2507 | llvm::Function *CGOpenMPRuntime::emitThreadPrivateVarDefinition( |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2508 | const VarDecl *VD, Address VDAddr, SourceLocation Loc, |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2509 | bool PerformInit, CodeGenFunction *CGF) { |
Samuel Antao | f8b5012 | 2015-07-13 22:54:53 +0000 | [diff] [blame] | 2510 | if (CGM.getLangOpts().OpenMPUseTLS && |
| 2511 | CGM.getContext().getTargetInfo().isTLSSupported()) |
| 2512 | return nullptr; |
| 2513 | |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2514 | VD = VD->getDefinition(CGM.getContext()); |
Alexey Bataev | 2a6f3f5 | 2018-11-07 19:11:14 +0000 | [diff] [blame] | 2515 | if (VD && ThreadPrivateWithDefinition.insert(CGM.getMangledName(VD)).second) { |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2516 | QualType ASTTy = VD->getType(); |
| 2517 | |
| 2518 | llvm::Value *Ctor = nullptr, *CopyCtor = nullptr, *Dtor = nullptr; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2519 | const Expr *Init = VD->getAnyInitializer(); |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2520 | if (CGM.getLangOpts().CPlusPlus && PerformInit) { |
| 2521 | // Generate function that re-emits the declaration's initializer into the |
| 2522 | // threadprivate copy of the variable VD |
| 2523 | CodeGenFunction CtorCGF(CGM); |
| 2524 | FunctionArgList Args; |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 2525 | ImplicitParamDecl Dst(CGM.getContext(), /*DC=*/nullptr, Loc, |
| 2526 | /*Id=*/nullptr, CGM.getContext().VoidPtrTy, |
Alexey Bataev | 5622323 | 2017-06-09 13:40:18 +0000 | [diff] [blame] | 2527 | ImplicitParamDecl::Other); |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2528 | Args.push_back(&Dst); |
| 2529 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2530 | const auto &FI = CGM.getTypes().arrangeBuiltinFunctionDeclaration( |
John McCall | c56a8b3 | 2016-03-11 04:30:31 +0000 | [diff] [blame] | 2531 | CGM.getContext().VoidPtrTy, Args); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2532 | llvm::FunctionType *FTy = CGM.getTypes().GetFunctionType(FI); |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 2533 | std::string Name = getName({"__kmpc_global_ctor_", ""}); |
| 2534 | llvm::Function *Fn = |
| 2535 | CGM.CreateGlobalInitOrDestructFunction(FTy, Name, FI, Loc); |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2536 | CtorCGF.StartFunction(GlobalDecl(), CGM.getContext().VoidPtrTy, Fn, FI, |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 2537 | Args, Loc, Loc); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2538 | llvm::Value *ArgVal = CtorCGF.EmitLoadOfScalar( |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2539 | CtorCGF.GetAddrOfLocalVar(&Dst), /*Volatile=*/false, |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2540 | CGM.getContext().VoidPtrTy, Dst.getLocation()); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2541 | Address Arg = Address(ArgVal, VDAddr.getAlignment()); |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 2542 | Arg = CtorCGF.Builder.CreateElementBitCast( |
| 2543 | Arg, CtorCGF.ConvertTypeForMem(ASTTy)); |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2544 | CtorCGF.EmitAnyExprToMem(Init, Arg, Init->getType().getQualifiers(), |
| 2545 | /*IsInitializer=*/true); |
| 2546 | ArgVal = CtorCGF.EmitLoadOfScalar( |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2547 | CtorCGF.GetAddrOfLocalVar(&Dst), /*Volatile=*/false, |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2548 | CGM.getContext().VoidPtrTy, Dst.getLocation()); |
| 2549 | CtorCGF.Builder.CreateStore(ArgVal, CtorCGF.ReturnValue); |
| 2550 | CtorCGF.FinishFunction(); |
| 2551 | Ctor = Fn; |
| 2552 | } |
| 2553 | if (VD->getType().isDestructedType() != QualType::DK_none) { |
| 2554 | // Generate function that emits destructor call for the threadprivate copy |
| 2555 | // of the variable VD |
| 2556 | CodeGenFunction DtorCGF(CGM); |
| 2557 | FunctionArgList Args; |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 2558 | ImplicitParamDecl Dst(CGM.getContext(), /*DC=*/nullptr, Loc, |
| 2559 | /*Id=*/nullptr, CGM.getContext().VoidPtrTy, |
Alexey Bataev | 5622323 | 2017-06-09 13:40:18 +0000 | [diff] [blame] | 2560 | ImplicitParamDecl::Other); |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2561 | Args.push_back(&Dst); |
| 2562 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2563 | const auto &FI = CGM.getTypes().arrangeBuiltinFunctionDeclaration( |
John McCall | c56a8b3 | 2016-03-11 04:30:31 +0000 | [diff] [blame] | 2564 | CGM.getContext().VoidTy, Args); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2565 | llvm::FunctionType *FTy = CGM.getTypes().GetFunctionType(FI); |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 2566 | std::string Name = getName({"__kmpc_global_dtor_", ""}); |
| 2567 | llvm::Function *Fn = |
| 2568 | CGM.CreateGlobalInitOrDestructFunction(FTy, Name, FI, Loc); |
Adrian Prantl | 1858c66 | 2016-04-24 22:22:29 +0000 | [diff] [blame] | 2569 | auto NL = ApplyDebugLocation::CreateEmpty(DtorCGF); |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2570 | DtorCGF.StartFunction(GlobalDecl(), CGM.getContext().VoidTy, Fn, FI, Args, |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 2571 | Loc, Loc); |
Adrian Prantl | 1858c66 | 2016-04-24 22:22:29 +0000 | [diff] [blame] | 2572 | // Create a scope with an artificial location for the body of this function. |
| 2573 | auto AL = ApplyDebugLocation::CreateArtificial(DtorCGF); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2574 | llvm::Value *ArgVal = DtorCGF.EmitLoadOfScalar( |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2575 | DtorCGF.GetAddrOfLocalVar(&Dst), |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2576 | /*Volatile=*/false, CGM.getContext().VoidPtrTy, Dst.getLocation()); |
| 2577 | DtorCGF.emitDestroy(Address(ArgVal, VDAddr.getAlignment()), ASTTy, |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2578 | DtorCGF.getDestroyer(ASTTy.isDestructedType()), |
| 2579 | DtorCGF.needsEHCleanup(ASTTy.isDestructedType())); |
| 2580 | DtorCGF.FinishFunction(); |
| 2581 | Dtor = Fn; |
| 2582 | } |
| 2583 | // Do not emit init function if it is not required. |
| 2584 | if (!Ctor && !Dtor) |
| 2585 | return nullptr; |
| 2586 | |
| 2587 | llvm::Type *CopyCtorTyArgs[] = {CGM.VoidPtrTy, CGM.VoidPtrTy}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2588 | auto *CopyCtorTy = llvm::FunctionType::get(CGM.VoidPtrTy, CopyCtorTyArgs, |
| 2589 | /*isVarArg=*/false) |
| 2590 | ->getPointerTo(); |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2591 | // Copying constructor for the threadprivate variable. |
| 2592 | // Must be NULL - reserved by runtime, but currently it requires that this |
| 2593 | // parameter is always NULL. Otherwise it fires assertion. |
| 2594 | CopyCtor = llvm::Constant::getNullValue(CopyCtorTy); |
| 2595 | if (Ctor == nullptr) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2596 | auto *CtorTy = llvm::FunctionType::get(CGM.VoidPtrTy, CGM.VoidPtrTy, |
| 2597 | /*isVarArg=*/false) |
| 2598 | ->getPointerTo(); |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2599 | Ctor = llvm::Constant::getNullValue(CtorTy); |
| 2600 | } |
| 2601 | if (Dtor == nullptr) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2602 | auto *DtorTy = llvm::FunctionType::get(CGM.VoidTy, CGM.VoidPtrTy, |
| 2603 | /*isVarArg=*/false) |
| 2604 | ->getPointerTo(); |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2605 | Dtor = llvm::Constant::getNullValue(DtorTy); |
| 2606 | } |
| 2607 | if (!CGF) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2608 | auto *InitFunctionTy = |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2609 | llvm::FunctionType::get(CGM.VoidTy, /*isVarArg*/ false); |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 2610 | std::string Name = getName({"__omp_threadprivate_init_", ""}); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2611 | llvm::Function *InitFunction = CGM.CreateGlobalInitOrDestructFunction( |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 2612 | InitFunctionTy, Name, CGM.getTypes().arrangeNullaryFunction()); |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2613 | CodeGenFunction InitCGF(CGM); |
| 2614 | FunctionArgList ArgList; |
| 2615 | InitCGF.StartFunction(GlobalDecl(), CGM.getContext().VoidTy, InitFunction, |
| 2616 | CGM.getTypes().arrangeNullaryFunction(), ArgList, |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 2617 | Loc, Loc); |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 2618 | emitThreadPrivateVarInit(InitCGF, VDAddr, Ctor, CopyCtor, Dtor, Loc); |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2619 | InitCGF.FinishFunction(); |
| 2620 | return InitFunction; |
| 2621 | } |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 2622 | emitThreadPrivateVarInit(*CGF, VDAddr, Ctor, CopyCtor, Dtor, Loc); |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2623 | } |
| 2624 | return nullptr; |
| 2625 | } |
| 2626 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2627 | /// Obtain information that uniquely identifies a target entry. This |
Alexey Bataev | 34f8a70 | 2018-03-28 14:28:54 +0000 | [diff] [blame] | 2628 | /// consists of the file and device IDs as well as line number associated with |
| 2629 | /// the relevant entry source location. |
| 2630 | static void getTargetEntryUniqueInfo(ASTContext &C, SourceLocation Loc, |
| 2631 | unsigned &DeviceID, unsigned &FileID, |
| 2632 | unsigned &LineNum) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2633 | SourceManager &SM = C.getSourceManager(); |
Alexey Bataev | 34f8a70 | 2018-03-28 14:28:54 +0000 | [diff] [blame] | 2634 | |
| 2635 | // The loc should be always valid and have a file ID (the user cannot use |
| 2636 | // #pragma directives in macros) |
| 2637 | |
| 2638 | assert(Loc.isValid() && "Source location is expected to be always valid."); |
Alexey Bataev | 34f8a70 | 2018-03-28 14:28:54 +0000 | [diff] [blame] | 2639 | |
| 2640 | PresumedLoc PLoc = SM.getPresumedLoc(Loc); |
| 2641 | assert(PLoc.isValid() && "Source location is expected to be always valid."); |
| 2642 | |
| 2643 | llvm::sys::fs::UniqueID ID; |
Alexey Bataev | 64e62dc | 2018-04-30 16:26:57 +0000 | [diff] [blame] | 2644 | if (auto EC = llvm::sys::fs::getUniqueID(PLoc.getFilename(), ID)) |
| 2645 | SM.getDiagnostics().Report(diag::err_cannot_open_file) |
| 2646 | << PLoc.getFilename() << EC.message(); |
Alexey Bataev | 34f8a70 | 2018-03-28 14:28:54 +0000 | [diff] [blame] | 2647 | |
| 2648 | DeviceID = ID.getDevice(); |
| 2649 | FileID = ID.getFile(); |
| 2650 | LineNum = PLoc.getLine(); |
| 2651 | } |
| 2652 | |
| 2653 | bool CGOpenMPRuntime::emitDeclareTargetVarDefinition(const VarDecl *VD, |
| 2654 | llvm::GlobalVariable *Addr, |
| 2655 | bool PerformInit) { |
| 2656 | Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res = |
Alexey Bataev | 97b7221 | 2018-08-14 18:31:20 +0000 | [diff] [blame] | 2657 | OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD); |
Alexey Bataev | 34f8a70 | 2018-03-28 14:28:54 +0000 | [diff] [blame] | 2658 | if (!Res || *Res == OMPDeclareTargetDeclAttr::MT_Link) |
Alexey Bataev | d01b749 | 2018-08-15 19:45:12 +0000 | [diff] [blame] | 2659 | return CGM.getLangOpts().OpenMPIsDevice; |
Alexey Bataev | 34f8a70 | 2018-03-28 14:28:54 +0000 | [diff] [blame] | 2660 | VD = VD->getDefinition(CGM.getContext()); |
Alexey Bataev | 2a6f3f5 | 2018-11-07 19:11:14 +0000 | [diff] [blame] | 2661 | if (VD && !DeclareTargetWithDefinition.insert(CGM.getMangledName(VD)).second) |
Alexey Bataev | 34f8a70 | 2018-03-28 14:28:54 +0000 | [diff] [blame] | 2662 | return CGM.getLangOpts().OpenMPIsDevice; |
| 2663 | |
| 2664 | QualType ASTTy = VD->getType(); |
| 2665 | |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2666 | SourceLocation Loc = VD->getCanonicalDecl()->getBeginLoc(); |
Alexey Bataev | 34f8a70 | 2018-03-28 14:28:54 +0000 | [diff] [blame] | 2667 | // Produce the unique prefix to identify the new target regions. We use |
| 2668 | // the source location of the variable declaration which we know to not |
| 2669 | // conflict with any target region. |
| 2670 | unsigned DeviceID; |
| 2671 | unsigned FileID; |
| 2672 | unsigned Line; |
| 2673 | getTargetEntryUniqueInfo(CGM.getContext(), Loc, DeviceID, FileID, Line); |
| 2674 | SmallString<128> Buffer, Out; |
| 2675 | { |
| 2676 | llvm::raw_svector_ostream OS(Buffer); |
| 2677 | OS << "__omp_offloading_" << llvm::format("_%x", DeviceID) |
| 2678 | << llvm::format("_%x_", FileID) << VD->getName() << "_l" << Line; |
| 2679 | } |
| 2680 | |
| 2681 | const Expr *Init = VD->getAnyInitializer(); |
| 2682 | if (CGM.getLangOpts().CPlusPlus && PerformInit) { |
| 2683 | llvm::Constant *Ctor; |
| 2684 | llvm::Constant *ID; |
| 2685 | if (CGM.getLangOpts().OpenMPIsDevice) { |
| 2686 | // Generate function that re-emits the declaration's initializer into |
| 2687 | // the threadprivate copy of the variable VD |
| 2688 | CodeGenFunction CtorCGF(CGM); |
| 2689 | |
| 2690 | const CGFunctionInfo &FI = CGM.getTypes().arrangeNullaryFunction(); |
| 2691 | llvm::FunctionType *FTy = CGM.getTypes().GetFunctionType(FI); |
| 2692 | llvm::Function *Fn = CGM.CreateGlobalInitOrDestructFunction( |
| 2693 | FTy, Twine(Buffer, "_ctor"), FI, Loc); |
| 2694 | auto NL = ApplyDebugLocation::CreateEmpty(CtorCGF); |
| 2695 | CtorCGF.StartFunction(GlobalDecl(), CGM.getContext().VoidTy, Fn, FI, |
| 2696 | FunctionArgList(), Loc, Loc); |
| 2697 | auto AL = ApplyDebugLocation::CreateArtificial(CtorCGF); |
| 2698 | CtorCGF.EmitAnyExprToMem(Init, |
| 2699 | Address(Addr, CGM.getContext().getDeclAlign(VD)), |
| 2700 | Init->getType().getQualifiers(), |
| 2701 | /*IsInitializer=*/true); |
| 2702 | CtorCGF.FinishFunction(); |
| 2703 | Ctor = Fn; |
| 2704 | ID = llvm::ConstantExpr::getBitCast(Fn, CGM.Int8PtrTy); |
Alexey Bataev | e253f2f | 2018-05-09 14:15:18 +0000 | [diff] [blame] | 2705 | CGM.addUsedGlobal(cast<llvm::GlobalValue>(Ctor)); |
Alexey Bataev | 34f8a70 | 2018-03-28 14:28:54 +0000 | [diff] [blame] | 2706 | } else { |
| 2707 | Ctor = new llvm::GlobalVariable( |
| 2708 | CGM.getModule(), CGM.Int8Ty, /*isConstant=*/true, |
| 2709 | llvm::GlobalValue::PrivateLinkage, |
| 2710 | llvm::Constant::getNullValue(CGM.Int8Ty), Twine(Buffer, "_ctor")); |
| 2711 | ID = Ctor; |
| 2712 | } |
| 2713 | |
| 2714 | // Register the information for the entry associated with the constructor. |
| 2715 | Out.clear(); |
| 2716 | OffloadEntriesInfoManager.registerTargetRegionEntryInfo( |
| 2717 | DeviceID, FileID, Twine(Buffer, "_ctor").toStringRef(Out), Line, Ctor, |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 2718 | ID, OffloadEntriesInfoManagerTy::OMPTargetRegionEntryCtor); |
Alexey Bataev | 34f8a70 | 2018-03-28 14:28:54 +0000 | [diff] [blame] | 2719 | } |
| 2720 | if (VD->getType().isDestructedType() != QualType::DK_none) { |
| 2721 | llvm::Constant *Dtor; |
| 2722 | llvm::Constant *ID; |
| 2723 | if (CGM.getLangOpts().OpenMPIsDevice) { |
| 2724 | // Generate function that emits destructor call for the threadprivate |
| 2725 | // copy of the variable VD |
| 2726 | CodeGenFunction DtorCGF(CGM); |
| 2727 | |
| 2728 | const CGFunctionInfo &FI = CGM.getTypes().arrangeNullaryFunction(); |
| 2729 | llvm::FunctionType *FTy = CGM.getTypes().GetFunctionType(FI); |
| 2730 | llvm::Function *Fn = CGM.CreateGlobalInitOrDestructFunction( |
| 2731 | FTy, Twine(Buffer, "_dtor"), FI, Loc); |
| 2732 | auto NL = ApplyDebugLocation::CreateEmpty(DtorCGF); |
| 2733 | DtorCGF.StartFunction(GlobalDecl(), CGM.getContext().VoidTy, Fn, FI, |
| 2734 | FunctionArgList(), Loc, Loc); |
| 2735 | // Create a scope with an artificial location for the body of this |
| 2736 | // function. |
| 2737 | auto AL = ApplyDebugLocation::CreateArtificial(DtorCGF); |
| 2738 | DtorCGF.emitDestroy(Address(Addr, CGM.getContext().getDeclAlign(VD)), |
| 2739 | ASTTy, DtorCGF.getDestroyer(ASTTy.isDestructedType()), |
| 2740 | DtorCGF.needsEHCleanup(ASTTy.isDestructedType())); |
| 2741 | DtorCGF.FinishFunction(); |
| 2742 | Dtor = Fn; |
| 2743 | ID = llvm::ConstantExpr::getBitCast(Fn, CGM.Int8PtrTy); |
Alexey Bataev | e253f2f | 2018-05-09 14:15:18 +0000 | [diff] [blame] | 2744 | CGM.addUsedGlobal(cast<llvm::GlobalValue>(Dtor)); |
Alexey Bataev | 34f8a70 | 2018-03-28 14:28:54 +0000 | [diff] [blame] | 2745 | } else { |
| 2746 | Dtor = new llvm::GlobalVariable( |
| 2747 | CGM.getModule(), CGM.Int8Ty, /*isConstant=*/true, |
| 2748 | llvm::GlobalValue::PrivateLinkage, |
| 2749 | llvm::Constant::getNullValue(CGM.Int8Ty), Twine(Buffer, "_dtor")); |
| 2750 | ID = Dtor; |
| 2751 | } |
| 2752 | // Register the information for the entry associated with the destructor. |
| 2753 | Out.clear(); |
| 2754 | OffloadEntriesInfoManager.registerTargetRegionEntryInfo( |
| 2755 | DeviceID, FileID, Twine(Buffer, "_dtor").toStringRef(Out), Line, Dtor, |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 2756 | ID, OffloadEntriesInfoManagerTy::OMPTargetRegionEntryDtor); |
Alexey Bataev | 34f8a70 | 2018-03-28 14:28:54 +0000 | [diff] [blame] | 2757 | } |
| 2758 | return CGM.getLangOpts().OpenMPIsDevice; |
| 2759 | } |
| 2760 | |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 2761 | Address CGOpenMPRuntime::getAddrOfArtificialThreadPrivate(CodeGenFunction &CGF, |
| 2762 | QualType VarType, |
| 2763 | StringRef Name) { |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 2764 | std::string Suffix = getName({"artificial", ""}); |
| 2765 | std::string CacheSuffix = getName({"cache", ""}); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 2766 | llvm::Type *VarLVType = CGF.ConvertTypeForMem(VarType); |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 2767 | llvm::Value *GAddr = |
| 2768 | getOrCreateInternalVariable(VarLVType, Twine(Name).concat(Suffix)); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 2769 | llvm::Value *Args[] = { |
| 2770 | emitUpdateLocation(CGF, SourceLocation()), |
| 2771 | getThreadID(CGF, SourceLocation()), |
| 2772 | CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(GAddr, CGM.VoidPtrTy), |
| 2773 | CGF.Builder.CreateIntCast(CGF.getTypeSize(VarType), CGM.SizeTy, |
| 2774 | /*IsSigned=*/false), |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 2775 | getOrCreateInternalVariable( |
| 2776 | CGM.VoidPtrPtrTy, Twine(Name).concat(Suffix).concat(CacheSuffix))}; |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 2777 | return Address( |
| 2778 | CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
| 2779 | CGF.EmitRuntimeCall( |
| 2780 | createRuntimeFunction(OMPRTL__kmpc_threadprivate_cached), Args), |
| 2781 | VarLVType->getPointerTo(/*AddrSpace=*/0)), |
| 2782 | CGM.getPointerAlign()); |
| 2783 | } |
| 2784 | |
Arpith Chacko Jacob | bb36fe8 | 2017-01-10 15:42:51 +0000 | [diff] [blame] | 2785 | void CGOpenMPRuntime::emitOMPIfClause(CodeGenFunction &CGF, const Expr *Cond, |
| 2786 | const RegionCodeGenTy &ThenGen, |
| 2787 | const RegionCodeGenTy &ElseGen) { |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 2788 | CodeGenFunction::LexicalScope ConditionScope(CGF, Cond->getSourceRange()); |
| 2789 | |
| 2790 | // If the condition constant folds and can be elided, try to avoid emitting |
| 2791 | // the condition and the dead arm of the if/else. |
| 2792 | bool CondConstant; |
| 2793 | if (CGF.ConstantFoldsToSimpleInteger(Cond, CondConstant)) { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2794 | if (CondConstant) |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 2795 | ThenGen(CGF); |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2796 | else |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 2797 | ElseGen(CGF); |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 2798 | return; |
| 2799 | } |
| 2800 | |
| 2801 | // Otherwise, the condition did not fold, or we couldn't elide it. Just |
| 2802 | // emit the conditional branch. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2803 | llvm::BasicBlock *ThenBlock = CGF.createBasicBlock("omp_if.then"); |
| 2804 | llvm::BasicBlock *ElseBlock = CGF.createBasicBlock("omp_if.else"); |
| 2805 | llvm::BasicBlock *ContBlock = CGF.createBasicBlock("omp_if.end"); |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 2806 | CGF.EmitBranchOnBoolExpr(Cond, ThenBlock, ElseBlock, /*TrueCount=*/0); |
| 2807 | |
| 2808 | // Emit the 'then' code. |
| 2809 | CGF.EmitBlock(ThenBlock); |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2810 | ThenGen(CGF); |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 2811 | CGF.EmitBranch(ContBlock); |
| 2812 | // Emit the 'else' code if present. |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2813 | // There is no need to emit line number for unconditional branch. |
| 2814 | (void)ApplyDebugLocation::CreateEmpty(CGF); |
| 2815 | CGF.EmitBlock(ElseBlock); |
| 2816 | ElseGen(CGF); |
| 2817 | // There is no need to emit line number for unconditional branch. |
| 2818 | (void)ApplyDebugLocation::CreateEmpty(CGF); |
| 2819 | CGF.EmitBranch(ContBlock); |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 2820 | // Emit the continuation block for code after the if. |
| 2821 | CGF.EmitBlock(ContBlock, /*IsFinished=*/true); |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 2822 | } |
| 2823 | |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 2824 | void CGOpenMPRuntime::emitParallelCall(CodeGenFunction &CGF, SourceLocation Loc, |
| 2825 | llvm::Value *OutlinedFn, |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 2826 | ArrayRef<llvm::Value *> CapturedVars, |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 2827 | const Expr *IfCond) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 2828 | if (!CGF.HaveInsertPoint()) |
| 2829 | return; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2830 | llvm::Value *RTLoc = emitUpdateLocation(CGF, Loc); |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2831 | auto &&ThenGen = [OutlinedFn, CapturedVars, RTLoc](CodeGenFunction &CGF, |
| 2832 | PrePostActionTy &) { |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 2833 | // Build call __kmpc_fork_call(loc, n, microtask, var1, .., varn); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2834 | CGOpenMPRuntime &RT = CGF.CGM.getOpenMPRuntime(); |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 2835 | llvm::Value *Args[] = { |
| 2836 | RTLoc, |
| 2837 | CGF.Builder.getInt32(CapturedVars.size()), // Number of captured vars |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2838 | CGF.Builder.CreateBitCast(OutlinedFn, RT.getKmpc_MicroPointerTy())}; |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 2839 | llvm::SmallVector<llvm::Value *, 16> RealArgs; |
| 2840 | RealArgs.append(std::begin(Args), std::end(Args)); |
| 2841 | RealArgs.append(CapturedVars.begin(), CapturedVars.end()); |
| 2842 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2843 | llvm::Value *RTLFn = RT.createRuntimeFunction(OMPRTL__kmpc_fork_call); |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 2844 | CGF.EmitRuntimeCall(RTLFn, RealArgs); |
| 2845 | }; |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2846 | auto &&ElseGen = [OutlinedFn, CapturedVars, RTLoc, Loc](CodeGenFunction &CGF, |
| 2847 | PrePostActionTy &) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2848 | CGOpenMPRuntime &RT = CGF.CGM.getOpenMPRuntime(); |
| 2849 | llvm::Value *ThreadID = RT.getThreadID(CGF, Loc); |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 2850 | // Build calls: |
| 2851 | // __kmpc_serialized_parallel(&Loc, GTid); |
| 2852 | llvm::Value *Args[] = {RTLoc, ThreadID}; |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2853 | CGF.EmitRuntimeCall( |
| 2854 | RT.createRuntimeFunction(OMPRTL__kmpc_serialized_parallel), Args); |
Alexey Bataev | d74d060 | 2014-10-13 06:02:40 +0000 | [diff] [blame] | 2855 | |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 2856 | // OutlinedFn(>id, &zero, CapturedStruct); |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 2857 | Address ZeroAddr = CGF.CreateDefaultAlignTempAlloca(CGF.Int32Ty, |
| 2858 | /*Name*/ ".zero.addr"); |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 2859 | CGF.InitTempAlloca(ZeroAddr, CGF.Builder.getInt32(/*C*/ 0)); |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 2860 | llvm::SmallVector<llvm::Value *, 16> OutlinedFnArgs; |
Alexey Bataev | 8521ff6 | 2018-07-25 20:03:01 +0000 | [diff] [blame] | 2861 | // ThreadId for serialized parallels is 0. |
| 2862 | OutlinedFnArgs.push_back(ZeroAddr.getPointer()); |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 2863 | OutlinedFnArgs.push_back(ZeroAddr.getPointer()); |
| 2864 | OutlinedFnArgs.append(CapturedVars.begin(), CapturedVars.end()); |
Alexey Bataev | 3c595a6 | 2017-08-14 15:01:03 +0000 | [diff] [blame] | 2865 | RT.emitOutlinedFunctionCall(CGF, Loc, OutlinedFn, OutlinedFnArgs); |
Alexey Bataev | d74d060 | 2014-10-13 06:02:40 +0000 | [diff] [blame] | 2866 | |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 2867 | // __kmpc_end_serialized_parallel(&Loc, GTid); |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2868 | llvm::Value *EndArgs[] = {RT.emitUpdateLocation(CGF, Loc), ThreadID}; |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 2869 | CGF.EmitRuntimeCall( |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2870 | RT.createRuntimeFunction(OMPRTL__kmpc_end_serialized_parallel), |
| 2871 | EndArgs); |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 2872 | }; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2873 | if (IfCond) { |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 2874 | emitOMPIfClause(CGF, IfCond, ThenGen, ElseGen); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2875 | } else { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2876 | RegionCodeGenTy ThenRCG(ThenGen); |
| 2877 | ThenRCG(CGF); |
Alexey Bataev | f539faa | 2016-03-28 12:58:34 +0000 | [diff] [blame] | 2878 | } |
Alexey Bataev | d74d060 | 2014-10-13 06:02:40 +0000 | [diff] [blame] | 2879 | } |
| 2880 | |
NAKAMURA Takumi | 59c74b22 | 2014-10-27 08:08:18 +0000 | [diff] [blame] | 2881 | // 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] | 2882 | // thread-ID variable (it is passed in a first argument of the outlined function |
| 2883 | // as "kmp_int32 *gtid"). Otherwise, if we're not inside parallel region, but in |
| 2884 | // regular serial code region, get thread ID by calling kmp_int32 |
| 2885 | // kmpc_global_thread_num(ident_t *loc), stash this thread ID in a temporary and |
| 2886 | // return the address of that temp. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2887 | Address CGOpenMPRuntime::emitThreadIDAddress(CodeGenFunction &CGF, |
| 2888 | SourceLocation Loc) { |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 2889 | if (auto *OMPRegionInfo = |
Alexey Bataev | d74d060 | 2014-10-13 06:02:40 +0000 | [diff] [blame] | 2890 | dyn_cast_or_null<CGOpenMPRegionInfo>(CGF.CapturedStmtInfo)) |
Alexey Bataev | 8cbe0a6 | 2015-02-26 10:27:34 +0000 | [diff] [blame] | 2891 | if (OMPRegionInfo->getThreadIDVariable()) |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 2892 | return OMPRegionInfo->getThreadIDVariableLValue(CGF).getAddress(); |
Alexey Bataev | 8cbe0a6 | 2015-02-26 10:27:34 +0000 | [diff] [blame] | 2893 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2894 | llvm::Value *ThreadID = getThreadID(CGF, Loc); |
| 2895 | QualType Int32Ty = |
Alexey Bataev | d74d060 | 2014-10-13 06:02:40 +0000 | [diff] [blame] | 2896 | CGF.getContext().getIntTypeForBitwidth(/*DestWidth*/ 32, /*Signed*/ true); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2897 | Address ThreadIDTemp = CGF.CreateMemTemp(Int32Ty, /*Name*/ ".threadid_temp."); |
Alexey Bataev | d74d060 | 2014-10-13 06:02:40 +0000 | [diff] [blame] | 2898 | CGF.EmitStoreOfScalar(ThreadID, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2899 | CGF.MakeAddrLValue(ThreadIDTemp, Int32Ty)); |
Alexey Bataev | d74d060 | 2014-10-13 06:02:40 +0000 | [diff] [blame] | 2900 | |
| 2901 | return ThreadIDTemp; |
| 2902 | } |
| 2903 | |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2904 | llvm::Constant * |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 2905 | CGOpenMPRuntime::getOrCreateInternalVariable(llvm::Type *Ty, |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2906 | const llvm::Twine &Name) { |
Alexey Bataev | 3a3bf0b | 2014-09-22 10:01:53 +0000 | [diff] [blame] | 2907 | SmallString<256> Buffer; |
| 2908 | llvm::raw_svector_ostream Out(Buffer); |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2909 | Out << Name; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2910 | StringRef RuntimeName = Out.str(); |
Alexey Bataev | 43a919f | 2018-04-13 17:48:43 +0000 | [diff] [blame] | 2911 | auto &Elem = *InternalVars.try_emplace(RuntimeName, nullptr).first; |
David Blaikie | 13156b6 | 2014-11-19 03:06:06 +0000 | [diff] [blame] | 2912 | if (Elem.second) { |
| 2913 | assert(Elem.second->getType()->getPointerElementType() == Ty && |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2914 | "OMP internal variable has different type than requested"); |
David Blaikie | 13156b6 | 2014-11-19 03:06:06 +0000 | [diff] [blame] | 2915 | return &*Elem.second; |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2916 | } |
Alexey Bataev | 3a3bf0b | 2014-09-22 10:01:53 +0000 | [diff] [blame] | 2917 | |
David Blaikie | 13156b6 | 2014-11-19 03:06:06 +0000 | [diff] [blame] | 2918 | return Elem.second = new llvm::GlobalVariable( |
| 2919 | CGM.getModule(), Ty, /*IsConstant*/ false, |
| 2920 | llvm::GlobalValue::CommonLinkage, llvm::Constant::getNullValue(Ty), |
| 2921 | Elem.first()); |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2922 | } |
| 2923 | |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 2924 | llvm::Value *CGOpenMPRuntime::getCriticalRegionLock(StringRef CriticalName) { |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 2925 | std::string Prefix = Twine("gomp_critical_user_", CriticalName).str(); |
| 2926 | std::string Name = getName({Prefix, "var"}); |
| 2927 | return getOrCreateInternalVariable(KmpCriticalNameTy, Name); |
Alexey Bataev | 3a3bf0b | 2014-09-22 10:01:53 +0000 | [diff] [blame] | 2928 | } |
| 2929 | |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 2930 | namespace { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2931 | /// Common pre(post)-action for different OpenMP constructs. |
| 2932 | class CommonActionTy final : public PrePostActionTy { |
| 2933 | llvm::Value *EnterCallee; |
| 2934 | ArrayRef<llvm::Value *> EnterArgs; |
| 2935 | llvm::Value *ExitCallee; |
| 2936 | ArrayRef<llvm::Value *> ExitArgs; |
| 2937 | bool Conditional; |
| 2938 | llvm::BasicBlock *ContBlock = nullptr; |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 2939 | |
| 2940 | public: |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2941 | CommonActionTy(llvm::Value *EnterCallee, ArrayRef<llvm::Value *> EnterArgs, |
| 2942 | llvm::Value *ExitCallee, ArrayRef<llvm::Value *> ExitArgs, |
| 2943 | bool Conditional = false) |
| 2944 | : EnterCallee(EnterCallee), EnterArgs(EnterArgs), ExitCallee(ExitCallee), |
| 2945 | ExitArgs(ExitArgs), Conditional(Conditional) {} |
| 2946 | void Enter(CodeGenFunction &CGF) override { |
| 2947 | llvm::Value *EnterRes = CGF.EmitRuntimeCall(EnterCallee, EnterArgs); |
| 2948 | if (Conditional) { |
| 2949 | llvm::Value *CallBool = CGF.Builder.CreateIsNotNull(EnterRes); |
| 2950 | auto *ThenBlock = CGF.createBasicBlock("omp_if.then"); |
| 2951 | ContBlock = CGF.createBasicBlock("omp_if.end"); |
| 2952 | // Generate the branch (If-stmt) |
| 2953 | CGF.Builder.CreateCondBr(CallBool, ThenBlock, ContBlock); |
| 2954 | CGF.EmitBlock(ThenBlock); |
| 2955 | } |
Alexey Bataev | a744ff5 | 2015-05-05 09:24:37 +0000 | [diff] [blame] | 2956 | } |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2957 | void Done(CodeGenFunction &CGF) { |
| 2958 | // Emit the rest of blocks/branches |
| 2959 | CGF.EmitBranch(ContBlock); |
| 2960 | CGF.EmitBlock(ContBlock, true); |
| 2961 | } |
| 2962 | void Exit(CodeGenFunction &CGF) override { |
| 2963 | CGF.EmitRuntimeCall(ExitCallee, ExitArgs); |
Alexey Bataev | 3e6124b | 2015-04-10 07:48:12 +0000 | [diff] [blame] | 2964 | } |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 2965 | }; |
Hans Wennborg | 7eb5464 | 2015-09-10 17:07:54 +0000 | [diff] [blame] | 2966 | } // anonymous namespace |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 2967 | |
| 2968 | void CGOpenMPRuntime::emitCriticalRegion(CodeGenFunction &CGF, |
| 2969 | StringRef CriticalName, |
| 2970 | const RegionCodeGenTy &CriticalOpGen, |
Alexey Bataev | fc57d16 | 2015-12-15 10:55:09 +0000 | [diff] [blame] | 2971 | SourceLocation Loc, const Expr *Hint) { |
| 2972 | // __kmpc_critical[_with_hint](ident_t *, gtid, Lock[, hint]); |
Alexey Bataev | 75ddfab | 2014-12-01 11:32:38 +0000 | [diff] [blame] | 2973 | // CriticalOpGen(); |
| 2974 | // __kmpc_end_critical(ident_t *, gtid, Lock); |
| 2975 | // Prepare arguments and build a call to __kmpc_critical |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 2976 | if (!CGF.HaveInsertPoint()) |
| 2977 | return; |
Alexey Bataev | fc57d16 | 2015-12-15 10:55:09 +0000 | [diff] [blame] | 2978 | llvm::Value *Args[] = {emitUpdateLocation(CGF, Loc), getThreadID(CGF, Loc), |
| 2979 | getCriticalRegionLock(CriticalName)}; |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2980 | llvm::SmallVector<llvm::Value *, 4> EnterArgs(std::begin(Args), |
| 2981 | std::end(Args)); |
Alexey Bataev | fc57d16 | 2015-12-15 10:55:09 +0000 | [diff] [blame] | 2982 | if (Hint) { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2983 | EnterArgs.push_back(CGF.Builder.CreateIntCast( |
| 2984 | CGF.EmitScalarExpr(Hint), CGM.IntPtrTy, /*isSigned=*/false)); |
| 2985 | } |
| 2986 | CommonActionTy Action( |
| 2987 | createRuntimeFunction(Hint ? OMPRTL__kmpc_critical_with_hint |
| 2988 | : OMPRTL__kmpc_critical), |
| 2989 | EnterArgs, createRuntimeFunction(OMPRTL__kmpc_end_critical), Args); |
| 2990 | CriticalOpGen.setAction(Action); |
Alexey Bataev | fc57d16 | 2015-12-15 10:55:09 +0000 | [diff] [blame] | 2991 | emitInlinedDirective(CGF, OMPD_critical, CriticalOpGen); |
Alexey Bataev | 3a3bf0b | 2014-09-22 10:01:53 +0000 | [diff] [blame] | 2992 | } |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 2993 | |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 2994 | void CGOpenMPRuntime::emitMasterRegion(CodeGenFunction &CGF, |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 2995 | const RegionCodeGenTy &MasterOpGen, |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 2996 | SourceLocation Loc) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 2997 | if (!CGF.HaveInsertPoint()) |
| 2998 | return; |
Alexey Bataev | 8d69065 | 2014-12-04 07:23:53 +0000 | [diff] [blame] | 2999 | // if(__kmpc_master(ident_t *, gtid)) { |
| 3000 | // MasterOpGen(); |
| 3001 | // __kmpc_end_master(ident_t *, gtid); |
| 3002 | // } |
| 3003 | // Prepare arguments and build a call to __kmpc_master |
Alexey Bataev | d7614fb | 2015-04-10 06:33:45 +0000 | [diff] [blame] | 3004 | llvm::Value *Args[] = {emitUpdateLocation(CGF, Loc), getThreadID(CGF, Loc)}; |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 3005 | CommonActionTy Action(createRuntimeFunction(OMPRTL__kmpc_master), Args, |
| 3006 | createRuntimeFunction(OMPRTL__kmpc_end_master), Args, |
| 3007 | /*Conditional=*/true); |
| 3008 | MasterOpGen.setAction(Action); |
| 3009 | emitInlinedDirective(CGF, OMPD_master, MasterOpGen); |
| 3010 | Action.Done(CGF); |
Alexey Bataev | 8d69065 | 2014-12-04 07:23:53 +0000 | [diff] [blame] | 3011 | } |
| 3012 | |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 3013 | void CGOpenMPRuntime::emitTaskyieldCall(CodeGenFunction &CGF, |
| 3014 | SourceLocation Loc) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 3015 | if (!CGF.HaveInsertPoint()) |
| 3016 | return; |
Alexey Bataev | 9f797f3 | 2015-02-05 05:57:51 +0000 | [diff] [blame] | 3017 | // Build call __kmpc_omp_taskyield(loc, thread_id, 0); |
| 3018 | llvm::Value *Args[] = { |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 3019 | emitUpdateLocation(CGF, Loc), getThreadID(CGF, Loc), |
Alexey Bataev | 9f797f3 | 2015-02-05 05:57:51 +0000 | [diff] [blame] | 3020 | llvm::ConstantInt::get(CGM.IntTy, /*V=*/0, /*isSigned=*/true)}; |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 3021 | CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_omp_taskyield), Args); |
Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 3022 | if (auto *Region = dyn_cast_or_null<CGOpenMPRegionInfo>(CGF.CapturedStmtInfo)) |
| 3023 | Region->emitUntiedSwitch(CGF); |
Alexey Bataev | 9f797f3 | 2015-02-05 05:57:51 +0000 | [diff] [blame] | 3024 | } |
| 3025 | |
Alexey Bataev | c30dd2d | 2015-06-18 12:14:09 +0000 | [diff] [blame] | 3026 | void CGOpenMPRuntime::emitTaskgroupRegion(CodeGenFunction &CGF, |
| 3027 | const RegionCodeGenTy &TaskgroupOpGen, |
| 3028 | SourceLocation Loc) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 3029 | if (!CGF.HaveInsertPoint()) |
| 3030 | return; |
Alexey Bataev | c30dd2d | 2015-06-18 12:14:09 +0000 | [diff] [blame] | 3031 | // __kmpc_taskgroup(ident_t *, gtid); |
| 3032 | // TaskgroupOpGen(); |
| 3033 | // __kmpc_end_taskgroup(ident_t *, gtid); |
| 3034 | // Prepare arguments and build a call to __kmpc_taskgroup |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 3035 | llvm::Value *Args[] = {emitUpdateLocation(CGF, Loc), getThreadID(CGF, Loc)}; |
| 3036 | CommonActionTy Action(createRuntimeFunction(OMPRTL__kmpc_taskgroup), Args, |
| 3037 | createRuntimeFunction(OMPRTL__kmpc_end_taskgroup), |
| 3038 | Args); |
| 3039 | TaskgroupOpGen.setAction(Action); |
| 3040 | emitInlinedDirective(CGF, OMPD_taskgroup, TaskgroupOpGen); |
Alexey Bataev | c30dd2d | 2015-06-18 12:14:09 +0000 | [diff] [blame] | 3041 | } |
| 3042 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3043 | /// Given an array of pointers to variables, project the address of a |
| 3044 | /// given variable. |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 3045 | static Address emitAddrOfVarFromArray(CodeGenFunction &CGF, Address Array, |
| 3046 | unsigned Index, const VarDecl *Var) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3047 | // Pull out the pointer to the variable. |
| 3048 | Address PtrAddr = |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 3049 | CGF.Builder.CreateConstArrayGEP(Array, Index, CGF.getPointerSize()); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3050 | llvm::Value *Ptr = CGF.Builder.CreateLoad(PtrAddr); |
| 3051 | |
| 3052 | Address Addr = Address(Ptr, CGF.getContext().getDeclAlign(Var)); |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 3053 | Addr = CGF.Builder.CreateElementBitCast( |
| 3054 | Addr, CGF.ConvertTypeForMem(Var->getType())); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3055 | return Addr; |
| 3056 | } |
| 3057 | |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3058 | static llvm::Value *emitCopyprivateCopyFunction( |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 3059 | CodeGenModule &CGM, llvm::Type *ArgsType, |
| 3060 | ArrayRef<const Expr *> CopyprivateVars, ArrayRef<const Expr *> DestExprs, |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 3061 | ArrayRef<const Expr *> SrcExprs, ArrayRef<const Expr *> AssignmentOps, |
| 3062 | SourceLocation Loc) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3063 | ASTContext &C = CGM.getContext(); |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3064 | // void copy_func(void *LHSArg, void *RHSArg); |
| 3065 | FunctionArgList Args; |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 3066 | ImplicitParamDecl LHSArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, C.VoidPtrTy, |
| 3067 | ImplicitParamDecl::Other); |
| 3068 | ImplicitParamDecl RHSArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, C.VoidPtrTy, |
| 3069 | ImplicitParamDecl::Other); |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3070 | Args.push_back(&LHSArg); |
| 3071 | Args.push_back(&RHSArg); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3072 | const auto &CGFI = |
| 3073 | CGM.getTypes().arrangeBuiltinFunctionDeclaration(C.VoidTy, Args); |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 3074 | std::string Name = |
| 3075 | CGM.getOpenMPRuntime().getName({"omp", "copyprivate", "copy_func"}); |
| 3076 | auto *Fn = llvm::Function::Create(CGM.getTypes().GetFunctionType(CGFI), |
| 3077 | llvm::GlobalValue::InternalLinkage, Name, |
| 3078 | &CGM.getModule()); |
Rafael Espindola | 51ec5a9 | 2018-02-28 23:46:35 +0000 | [diff] [blame] | 3079 | CGM.SetInternalFunctionAttributes(GlobalDecl(), Fn, CGFI); |
Alexey Bataev | c0f879b | 2018-04-10 20:10:53 +0000 | [diff] [blame] | 3080 | Fn->setDoesNotRecurse(); |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3081 | CodeGenFunction CGF(CGM); |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 3082 | CGF.StartFunction(GlobalDecl(), C.VoidTy, Fn, CGFI, Args, Loc, Loc); |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 3083 | // Dest = (void*[n])(LHSArg); |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3084 | // Src = (void*[n])(RHSArg); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3085 | Address LHS(CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
| 3086 | CGF.Builder.CreateLoad(CGF.GetAddrOfLocalVar(&LHSArg)), |
| 3087 | ArgsType), CGF.getPointerAlign()); |
| 3088 | Address RHS(CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
| 3089 | CGF.Builder.CreateLoad(CGF.GetAddrOfLocalVar(&RHSArg)), |
| 3090 | ArgsType), CGF.getPointerAlign()); |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3091 | // *(Type0*)Dst[0] = *(Type0*)Src[0]; |
| 3092 | // *(Type1*)Dst[1] = *(Type1*)Src[1]; |
| 3093 | // ... |
| 3094 | // *(Typen*)Dst[n] = *(Typen*)Src[n]; |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3095 | for (unsigned I = 0, E = AssignmentOps.size(); I < E; ++I) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3096 | const auto *DestVar = |
| 3097 | cast<VarDecl>(cast<DeclRefExpr>(DestExprs[I])->getDecl()); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3098 | Address DestAddr = emitAddrOfVarFromArray(CGF, LHS, I, DestVar); |
| 3099 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3100 | const auto *SrcVar = |
| 3101 | cast<VarDecl>(cast<DeclRefExpr>(SrcExprs[I])->getDecl()); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3102 | Address SrcAddr = emitAddrOfVarFromArray(CGF, RHS, I, SrcVar); |
| 3103 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3104 | const auto *VD = cast<DeclRefExpr>(CopyprivateVars[I])->getDecl(); |
Alexey Bataev | 1d9c15c | 2015-05-19 12:31:28 +0000 | [diff] [blame] | 3105 | QualType Type = VD->getType(); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3106 | CGF.EmitOMPCopy(Type, DestAddr, SrcAddr, DestVar, SrcVar, AssignmentOps[I]); |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3107 | } |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3108 | CGF.FinishFunction(); |
| 3109 | return Fn; |
| 3110 | } |
| 3111 | |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 3112 | void CGOpenMPRuntime::emitSingleRegion(CodeGenFunction &CGF, |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 3113 | const RegionCodeGenTy &SingleOpGen, |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3114 | SourceLocation Loc, |
| 3115 | ArrayRef<const Expr *> CopyprivateVars, |
| 3116 | ArrayRef<const Expr *> SrcExprs, |
| 3117 | ArrayRef<const Expr *> DstExprs, |
| 3118 | ArrayRef<const Expr *> AssignmentOps) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 3119 | if (!CGF.HaveInsertPoint()) |
| 3120 | return; |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3121 | assert(CopyprivateVars.size() == SrcExprs.size() && |
| 3122 | CopyprivateVars.size() == DstExprs.size() && |
| 3123 | CopyprivateVars.size() == AssignmentOps.size()); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3124 | ASTContext &C = CGM.getContext(); |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3125 | // int32 did_it = 0; |
Alexey Bataev | 6956e2e | 2015-02-05 06:35:41 +0000 | [diff] [blame] | 3126 | // if(__kmpc_single(ident_t *, gtid)) { |
| 3127 | // SingleOpGen(); |
| 3128 | // __kmpc_end_single(ident_t *, gtid); |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3129 | // did_it = 1; |
Alexey Bataev | 6956e2e | 2015-02-05 06:35:41 +0000 | [diff] [blame] | 3130 | // } |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3131 | // call __kmpc_copyprivate(ident_t *, gtid, <buf_size>, <copyprivate list>, |
| 3132 | // <copy_func>, did_it); |
| 3133 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3134 | Address DidIt = Address::invalid(); |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3135 | if (!CopyprivateVars.empty()) { |
| 3136 | // int32 did_it = 0; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3137 | QualType KmpInt32Ty = |
| 3138 | C.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1); |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3139 | DidIt = CGF.CreateMemTemp(KmpInt32Ty, ".omp.copyprivate.did_it"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3140 | CGF.Builder.CreateStore(CGF.Builder.getInt32(0), DidIt); |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3141 | } |
Alexey Bataev | 6956e2e | 2015-02-05 06:35:41 +0000 | [diff] [blame] | 3142 | // Prepare arguments and build a call to __kmpc_single |
Alexey Bataev | d7614fb | 2015-04-10 06:33:45 +0000 | [diff] [blame] | 3143 | llvm::Value *Args[] = {emitUpdateLocation(CGF, Loc), getThreadID(CGF, Loc)}; |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 3144 | CommonActionTy Action(createRuntimeFunction(OMPRTL__kmpc_single), Args, |
| 3145 | createRuntimeFunction(OMPRTL__kmpc_end_single), Args, |
| 3146 | /*Conditional=*/true); |
| 3147 | SingleOpGen.setAction(Action); |
| 3148 | emitInlinedDirective(CGF, OMPD_single, SingleOpGen); |
| 3149 | if (DidIt.isValid()) { |
| 3150 | // did_it = 1; |
| 3151 | CGF.Builder.CreateStore(CGF.Builder.getInt32(1), DidIt); |
| 3152 | } |
| 3153 | Action.Done(CGF); |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3154 | // call __kmpc_copyprivate(ident_t *, gtid, <buf_size>, <copyprivate list>, |
| 3155 | // <copy_func>, did_it); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3156 | if (DidIt.isValid()) { |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3157 | llvm::APInt ArraySize(/*unsigned int numBits=*/32, CopyprivateVars.size()); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3158 | QualType CopyprivateArrayTy = |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3159 | C.getConstantArrayType(C.VoidPtrTy, ArraySize, ArrayType::Normal, |
| 3160 | /*IndexTypeQuals=*/0); |
| 3161 | // Create a list of all private variables for copyprivate. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3162 | Address CopyprivateList = |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3163 | CGF.CreateMemTemp(CopyprivateArrayTy, ".omp.copyprivate.cpr_list"); |
| 3164 | for (unsigned I = 0, E = CopyprivateVars.size(); I < E; ++I) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3165 | Address Elem = CGF.Builder.CreateConstArrayGEP( |
| 3166 | CopyprivateList, I, CGF.getPointerSize()); |
| 3167 | CGF.Builder.CreateStore( |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3168 | CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3169 | CGF.EmitLValue(CopyprivateVars[I]).getPointer(), CGF.VoidPtrTy), |
| 3170 | Elem); |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3171 | } |
| 3172 | // Build function that copies private values from single region to all other |
| 3173 | // threads in the corresponding parallel region. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3174 | llvm::Value *CpyFn = emitCopyprivateCopyFunction( |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3175 | CGM, CGF.ConvertTypeForMem(CopyprivateArrayTy)->getPointerTo(), |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 3176 | CopyprivateVars, SrcExprs, DstExprs, AssignmentOps, Loc); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3177 | llvm::Value *BufSize = CGF.getTypeSize(CopyprivateArrayTy); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3178 | Address CL = |
| 3179 | CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(CopyprivateList, |
| 3180 | CGF.VoidPtrTy); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3181 | llvm::Value *DidItVal = CGF.Builder.CreateLoad(DidIt); |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3182 | llvm::Value *Args[] = { |
| 3183 | emitUpdateLocation(CGF, Loc), // ident_t *<loc> |
| 3184 | getThreadID(CGF, Loc), // i32 <gtid> |
Alexey Bataev | 66beaa9 | 2015-04-30 03:47:32 +0000 | [diff] [blame] | 3185 | BufSize, // size_t <buf_size> |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3186 | CL.getPointer(), // void *<copyprivate list> |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3187 | CpyFn, // void (*) (void *, void *) <copy_func> |
| 3188 | DidItVal // i32 did_it |
| 3189 | }; |
| 3190 | CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_copyprivate), Args); |
| 3191 | } |
Alexey Bataev | 6956e2e | 2015-02-05 06:35:41 +0000 | [diff] [blame] | 3192 | } |
| 3193 | |
Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 3194 | void CGOpenMPRuntime::emitOrderedRegion(CodeGenFunction &CGF, |
| 3195 | const RegionCodeGenTy &OrderedOpGen, |
Alexey Bataev | 5f600d6 | 2015-09-29 03:48:57 +0000 | [diff] [blame] | 3196 | SourceLocation Loc, bool IsThreads) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 3197 | if (!CGF.HaveInsertPoint()) |
| 3198 | return; |
Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 3199 | // __kmpc_ordered(ident_t *, gtid); |
| 3200 | // OrderedOpGen(); |
| 3201 | // __kmpc_end_ordered(ident_t *, gtid); |
| 3202 | // Prepare arguments and build a call to __kmpc_ordered |
Alexey Bataev | 5f600d6 | 2015-09-29 03:48:57 +0000 | [diff] [blame] | 3203 | if (IsThreads) { |
Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 3204 | llvm::Value *Args[] = {emitUpdateLocation(CGF, Loc), getThreadID(CGF, Loc)}; |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 3205 | CommonActionTy Action(createRuntimeFunction(OMPRTL__kmpc_ordered), Args, |
| 3206 | createRuntimeFunction(OMPRTL__kmpc_end_ordered), |
| 3207 | Args); |
| 3208 | OrderedOpGen.setAction(Action); |
| 3209 | emitInlinedDirective(CGF, OMPD_ordered, OrderedOpGen); |
| 3210 | return; |
Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 3211 | } |
Alexey Bataev | 5f600d6 | 2015-09-29 03:48:57 +0000 | [diff] [blame] | 3212 | emitInlinedDirective(CGF, OMPD_ordered, OrderedOpGen); |
Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 3213 | } |
| 3214 | |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 3215 | void CGOpenMPRuntime::emitBarrierCall(CodeGenFunction &CGF, SourceLocation Loc, |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 3216 | OpenMPDirectiveKind Kind, bool EmitChecks, |
| 3217 | bool ForceSimpleCall) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 3218 | if (!CGF.HaveInsertPoint()) |
| 3219 | return; |
Alexey Bataev | 8f7c1b0 | 2014-12-05 04:09:23 +0000 | [diff] [blame] | 3220 | // Build call __kmpc_cancel_barrier(loc, thread_id); |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 3221 | // Build call __kmpc_barrier(loc, thread_id); |
Alexey Bataev | 50b3c95 | 2016-02-19 10:38:26 +0000 | [diff] [blame] | 3222 | unsigned Flags; |
| 3223 | if (Kind == OMPD_for) |
| 3224 | Flags = OMP_IDENT_BARRIER_IMPL_FOR; |
| 3225 | else if (Kind == OMPD_sections) |
| 3226 | Flags = OMP_IDENT_BARRIER_IMPL_SECTIONS; |
| 3227 | else if (Kind == OMPD_single) |
| 3228 | Flags = OMP_IDENT_BARRIER_IMPL_SINGLE; |
| 3229 | else if (Kind == OMPD_barrier) |
| 3230 | Flags = OMP_IDENT_BARRIER_EXPL; |
| 3231 | else |
| 3232 | Flags = OMP_IDENT_BARRIER_IMPL; |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 3233 | // Build call __kmpc_cancel_barrier(loc, thread_id) or __kmpc_barrier(loc, |
| 3234 | // thread_id); |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 3235 | llvm::Value *Args[] = {emitUpdateLocation(CGF, Loc, Flags), |
| 3236 | getThreadID(CGF, Loc)}; |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 3237 | if (auto *OMPRegionInfo = |
| 3238 | dyn_cast_or_null<CGOpenMPRegionInfo>(CGF.CapturedStmtInfo)) { |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 3239 | if (!ForceSimpleCall && OMPRegionInfo->hasCancel()) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3240 | llvm::Value *Result = CGF.EmitRuntimeCall( |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 3241 | createRuntimeFunction(OMPRTL__kmpc_cancel_barrier), Args); |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 3242 | if (EmitChecks) { |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 3243 | // if (__kmpc_cancel_barrier()) { |
| 3244 | // exit from construct; |
| 3245 | // } |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3246 | llvm::BasicBlock *ExitBB = CGF.createBasicBlock(".cancel.exit"); |
| 3247 | llvm::BasicBlock *ContBB = CGF.createBasicBlock(".cancel.continue"); |
| 3248 | llvm::Value *Cmp = CGF.Builder.CreateIsNotNull(Result); |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 3249 | CGF.Builder.CreateCondBr(Cmp, ExitBB, ContBB); |
| 3250 | CGF.EmitBlock(ExitBB); |
| 3251 | // exit from construct; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3252 | CodeGenFunction::JumpDest CancelDestination = |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 3253 | CGF.getOMPCancelDestination(OMPRegionInfo->getDirectiveKind()); |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 3254 | CGF.EmitBranchThroughCleanup(CancelDestination); |
| 3255 | CGF.EmitBlock(ContBB, /*IsFinished=*/true); |
| 3256 | } |
| 3257 | return; |
| 3258 | } |
| 3259 | } |
| 3260 | CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_barrier), Args); |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 3261 | } |
| 3262 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 3263 | /// Map the OpenMP loop schedule to the runtime enumeration. |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 3264 | static OpenMPSchedType getRuntimeSchedule(OpenMPScheduleClauseKind ScheduleKind, |
Alexey Bataev | d7589ffe | 2015-05-20 13:12:48 +0000 | [diff] [blame] | 3265 | bool Chunked, bool Ordered) { |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 3266 | switch (ScheduleKind) { |
| 3267 | case OMPC_SCHEDULE_static: |
Alexey Bataev | d7589ffe | 2015-05-20 13:12:48 +0000 | [diff] [blame] | 3268 | return Chunked ? (Ordered ? OMP_ord_static_chunked : OMP_sch_static_chunked) |
| 3269 | : (Ordered ? OMP_ord_static : OMP_sch_static); |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 3270 | case OMPC_SCHEDULE_dynamic: |
Alexey Bataev | d7589ffe | 2015-05-20 13:12:48 +0000 | [diff] [blame] | 3271 | return Ordered ? OMP_ord_dynamic_chunked : OMP_sch_dynamic_chunked; |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 3272 | case OMPC_SCHEDULE_guided: |
Alexey Bataev | d7589ffe | 2015-05-20 13:12:48 +0000 | [diff] [blame] | 3273 | return Ordered ? OMP_ord_guided_chunked : OMP_sch_guided_chunked; |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 3274 | case OMPC_SCHEDULE_runtime: |
Alexey Bataev | d7589ffe | 2015-05-20 13:12:48 +0000 | [diff] [blame] | 3275 | return Ordered ? OMP_ord_runtime : OMP_sch_runtime; |
| 3276 | case OMPC_SCHEDULE_auto: |
| 3277 | return Ordered ? OMP_ord_auto : OMP_sch_auto; |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 3278 | case OMPC_SCHEDULE_unknown: |
| 3279 | assert(!Chunked && "chunk was specified but schedule kind not known"); |
Alexey Bataev | d7589ffe | 2015-05-20 13:12:48 +0000 | [diff] [blame] | 3280 | return Ordered ? OMP_ord_static : OMP_sch_static; |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 3281 | } |
| 3282 | llvm_unreachable("Unexpected runtime schedule"); |
| 3283 | } |
| 3284 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 3285 | /// Map the OpenMP distribute schedule to the runtime enumeration. |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3286 | static OpenMPSchedType |
| 3287 | getRuntimeSchedule(OpenMPDistScheduleClauseKind ScheduleKind, bool Chunked) { |
| 3288 | // only static is allowed for dist_schedule |
| 3289 | return Chunked ? OMP_dist_sch_static_chunked : OMP_dist_sch_static; |
| 3290 | } |
| 3291 | |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 3292 | bool CGOpenMPRuntime::isStaticNonchunked(OpenMPScheduleClauseKind ScheduleKind, |
| 3293 | bool Chunked) const { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3294 | OpenMPSchedType Schedule = |
| 3295 | getRuntimeSchedule(ScheduleKind, Chunked, /*Ordered=*/false); |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 3296 | return Schedule == OMP_sch_static; |
| 3297 | } |
| 3298 | |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3299 | bool CGOpenMPRuntime::isStaticNonchunked( |
| 3300 | OpenMPDistScheduleClauseKind ScheduleKind, bool Chunked) const { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3301 | OpenMPSchedType Schedule = getRuntimeSchedule(ScheduleKind, Chunked); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3302 | return Schedule == OMP_dist_sch_static; |
| 3303 | } |
| 3304 | |
Gheorghe-Teodor Bercea | e925676 | 2018-10-29 15:45:47 +0000 | [diff] [blame] | 3305 | bool CGOpenMPRuntime::isStaticChunked(OpenMPScheduleClauseKind ScheduleKind, |
| 3306 | bool Chunked) const { |
| 3307 | OpenMPSchedType Schedule = |
| 3308 | getRuntimeSchedule(ScheduleKind, Chunked, /*Ordered=*/false); |
| 3309 | return Schedule == OMP_sch_static_chunked; |
| 3310 | } |
| 3311 | |
| 3312 | bool CGOpenMPRuntime::isStaticChunked( |
| 3313 | OpenMPDistScheduleClauseKind ScheduleKind, bool Chunked) const { |
| 3314 | OpenMPSchedType Schedule = getRuntimeSchedule(ScheduleKind, Chunked); |
| 3315 | return Schedule == OMP_dist_sch_static_chunked; |
| 3316 | } |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3317 | |
Alexander Musman | df7a8e2 | 2015-01-22 08:49:35 +0000 | [diff] [blame] | 3318 | bool CGOpenMPRuntime::isDynamic(OpenMPScheduleClauseKind ScheduleKind) const { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3319 | OpenMPSchedType Schedule = |
Alexey Bataev | d7589ffe | 2015-05-20 13:12:48 +0000 | [diff] [blame] | 3320 | getRuntimeSchedule(ScheduleKind, /*Chunked=*/false, /*Ordered=*/false); |
Alexander Musman | df7a8e2 | 2015-01-22 08:49:35 +0000 | [diff] [blame] | 3321 | assert(Schedule != OMP_sch_static_chunked && "cannot be chunked here"); |
| 3322 | return Schedule != OMP_sch_static; |
| 3323 | } |
| 3324 | |
Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 3325 | static int addMonoNonMonoModifier(OpenMPSchedType Schedule, |
| 3326 | OpenMPScheduleClauseModifier M1, |
| 3327 | OpenMPScheduleClauseModifier M2) { |
Alexey Bataev | 6cff624 | 2016-05-30 13:05:14 +0000 | [diff] [blame] | 3328 | int Modifier = 0; |
Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 3329 | switch (M1) { |
| 3330 | case OMPC_SCHEDULE_MODIFIER_monotonic: |
Alexey Bataev | 6cff624 | 2016-05-30 13:05:14 +0000 | [diff] [blame] | 3331 | Modifier = OMP_sch_modifier_monotonic; |
| 3332 | break; |
Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 3333 | case OMPC_SCHEDULE_MODIFIER_nonmonotonic: |
Alexey Bataev | 6cff624 | 2016-05-30 13:05:14 +0000 | [diff] [blame] | 3334 | Modifier = OMP_sch_modifier_nonmonotonic; |
| 3335 | break; |
Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 3336 | case OMPC_SCHEDULE_MODIFIER_simd: |
Alexey Bataev | 6cff624 | 2016-05-30 13:05:14 +0000 | [diff] [blame] | 3337 | if (Schedule == OMP_sch_static_chunked) |
| 3338 | Schedule = OMP_sch_static_balanced_chunked; |
| 3339 | break; |
Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 3340 | case OMPC_SCHEDULE_MODIFIER_last: |
| 3341 | case OMPC_SCHEDULE_MODIFIER_unknown: |
| 3342 | break; |
| 3343 | } |
| 3344 | switch (M2) { |
| 3345 | case OMPC_SCHEDULE_MODIFIER_monotonic: |
Alexey Bataev | 6cff624 | 2016-05-30 13:05:14 +0000 | [diff] [blame] | 3346 | Modifier = OMP_sch_modifier_monotonic; |
| 3347 | break; |
Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 3348 | case OMPC_SCHEDULE_MODIFIER_nonmonotonic: |
Alexey Bataev | 6cff624 | 2016-05-30 13:05:14 +0000 | [diff] [blame] | 3349 | Modifier = OMP_sch_modifier_nonmonotonic; |
| 3350 | break; |
Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 3351 | case OMPC_SCHEDULE_MODIFIER_simd: |
Alexey Bataev | 6cff624 | 2016-05-30 13:05:14 +0000 | [diff] [blame] | 3352 | if (Schedule == OMP_sch_static_chunked) |
| 3353 | Schedule = OMP_sch_static_balanced_chunked; |
| 3354 | break; |
Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 3355 | case OMPC_SCHEDULE_MODIFIER_last: |
| 3356 | case OMPC_SCHEDULE_MODIFIER_unknown: |
| 3357 | break; |
| 3358 | } |
Alexey Bataev | 6cff624 | 2016-05-30 13:05:14 +0000 | [diff] [blame] | 3359 | return Schedule | Modifier; |
Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 3360 | } |
| 3361 | |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 3362 | void CGOpenMPRuntime::emitForDispatchInit( |
| 3363 | CodeGenFunction &CGF, SourceLocation Loc, |
| 3364 | const OpenMPScheduleTy &ScheduleKind, unsigned IVSize, bool IVSigned, |
| 3365 | bool Ordered, const DispatchRTInput &DispatchValues) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 3366 | if (!CGF.HaveInsertPoint()) |
| 3367 | return; |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 3368 | OpenMPSchedType Schedule = getRuntimeSchedule( |
| 3369 | ScheduleKind.Schedule, DispatchValues.Chunk != nullptr, Ordered); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3370 | assert(Ordered || |
| 3371 | (Schedule != OMP_sch_static && Schedule != OMP_sch_static_chunked && |
Alexey Bataev | 6cff624 | 2016-05-30 13:05:14 +0000 | [diff] [blame] | 3372 | Schedule != OMP_ord_static && Schedule != OMP_ord_static_chunked && |
| 3373 | Schedule != OMP_sch_static_balanced_chunked)); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3374 | // Call __kmpc_dispatch_init( |
| 3375 | // ident_t *loc, kmp_int32 tid, kmp_int32 schedule, |
| 3376 | // kmp_int[32|64] lower, kmp_int[32|64] upper, |
| 3377 | // kmp_int[32|64] stride, kmp_int[32|64] chunk); |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 3378 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3379 | // 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] | 3380 | llvm::Value *Chunk = DispatchValues.Chunk ? DispatchValues.Chunk |
| 3381 | : CGF.Builder.getIntN(IVSize, 1); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3382 | llvm::Value *Args[] = { |
Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 3383 | emitUpdateLocation(CGF, Loc), getThreadID(CGF, Loc), |
| 3384 | CGF.Builder.getInt32(addMonoNonMonoModifier( |
| 3385 | Schedule, ScheduleKind.M1, ScheduleKind.M2)), // Schedule type |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 3386 | DispatchValues.LB, // Lower |
| 3387 | DispatchValues.UB, // Upper |
Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 3388 | CGF.Builder.getIntN(IVSize, 1), // Stride |
| 3389 | Chunk // Chunk |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3390 | }; |
| 3391 | CGF.EmitRuntimeCall(createDispatchInitFunction(IVSize, IVSigned), Args); |
| 3392 | } |
| 3393 | |
Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 3394 | static void emitForStaticInitCall( |
| 3395 | CodeGenFunction &CGF, llvm::Value *UpdateLocation, llvm::Value *ThreadId, |
| 3396 | llvm::Constant *ForStaticInitFunction, OpenMPSchedType Schedule, |
| 3397 | OpenMPScheduleClauseModifier M1, OpenMPScheduleClauseModifier M2, |
Alexey Bataev | 0f87dbe | 2017-08-14 17:56:13 +0000 | [diff] [blame] | 3398 | const CGOpenMPRuntime::StaticRTInput &Values) { |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3399 | if (!CGF.HaveInsertPoint()) |
Alexey Bataev | 0f87dbe | 2017-08-14 17:56:13 +0000 | [diff] [blame] | 3400 | return; |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3401 | |
Alexey Bataev | 0f87dbe | 2017-08-14 17:56:13 +0000 | [diff] [blame] | 3402 | assert(!Values.Ordered); |
| 3403 | assert(Schedule == OMP_sch_static || Schedule == OMP_sch_static_chunked || |
| 3404 | Schedule == OMP_sch_static_balanced_chunked || |
| 3405 | Schedule == OMP_ord_static || Schedule == OMP_ord_static_chunked || |
| 3406 | Schedule == OMP_dist_sch_static || |
| 3407 | Schedule == OMP_dist_sch_static_chunked); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3408 | |
Alexey Bataev | 0f87dbe | 2017-08-14 17:56:13 +0000 | [diff] [blame] | 3409 | // Call __kmpc_for_static_init( |
| 3410 | // ident_t *loc, kmp_int32 tid, kmp_int32 schedtype, |
| 3411 | // kmp_int32 *p_lastiter, kmp_int[32|64] *p_lower, |
| 3412 | // kmp_int[32|64] *p_upper, kmp_int[32|64] *p_stride, |
| 3413 | // kmp_int[32|64] incr, kmp_int[32|64] chunk); |
| 3414 | llvm::Value *Chunk = Values.Chunk; |
| 3415 | if (Chunk == nullptr) { |
| 3416 | assert((Schedule == OMP_sch_static || Schedule == OMP_ord_static || |
| 3417 | Schedule == OMP_dist_sch_static) && |
| 3418 | "expected static non-chunked schedule"); |
| 3419 | // If the Chunk was not specified in the clause - use default value 1. |
| 3420 | Chunk = CGF.Builder.getIntN(Values.IVSize, 1); |
| 3421 | } else { |
| 3422 | assert((Schedule == OMP_sch_static_chunked || |
| 3423 | Schedule == OMP_sch_static_balanced_chunked || |
| 3424 | Schedule == OMP_ord_static_chunked || |
| 3425 | Schedule == OMP_dist_sch_static_chunked) && |
| 3426 | "expected static chunked schedule"); |
| 3427 | } |
| 3428 | llvm::Value *Args[] = { |
| 3429 | UpdateLocation, |
| 3430 | ThreadId, |
| 3431 | CGF.Builder.getInt32(addMonoNonMonoModifier(Schedule, M1, |
| 3432 | M2)), // Schedule type |
| 3433 | Values.IL.getPointer(), // &isLastIter |
| 3434 | Values.LB.getPointer(), // &LB |
| 3435 | Values.UB.getPointer(), // &UB |
| 3436 | Values.ST.getPointer(), // &Stride |
| 3437 | CGF.Builder.getIntN(Values.IVSize, 1), // Incr |
| 3438 | Chunk // Chunk |
| 3439 | }; |
| 3440 | CGF.EmitRuntimeCall(ForStaticInitFunction, Args); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3441 | } |
| 3442 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3443 | void CGOpenMPRuntime::emitForStaticInit(CodeGenFunction &CGF, |
| 3444 | SourceLocation Loc, |
Alexey Bataev | 0f87dbe | 2017-08-14 17:56:13 +0000 | [diff] [blame] | 3445 | OpenMPDirectiveKind DKind, |
Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 3446 | const OpenMPScheduleTy &ScheduleKind, |
Alexey Bataev | 0f87dbe | 2017-08-14 17:56:13 +0000 | [diff] [blame] | 3447 | const StaticRTInput &Values) { |
| 3448 | OpenMPSchedType ScheduleNum = getRuntimeSchedule( |
| 3449 | ScheduleKind.Schedule, Values.Chunk != nullptr, Values.Ordered); |
| 3450 | assert(isOpenMPWorksharingDirective(DKind) && |
| 3451 | "Expected loop-based or sections-based directive."); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3452 | llvm::Value *UpdatedLocation = emitUpdateLocation(CGF, Loc, |
Alexey Bataev | 0f87dbe | 2017-08-14 17:56:13 +0000 | [diff] [blame] | 3453 | isOpenMPLoopDirective(DKind) |
| 3454 | ? OMP_IDENT_WORK_LOOP |
| 3455 | : OMP_IDENT_WORK_SECTIONS); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3456 | llvm::Value *ThreadId = getThreadID(CGF, Loc); |
| 3457 | llvm::Constant *StaticInitFunction = |
Alexey Bataev | 0f87dbe | 2017-08-14 17:56:13 +0000 | [diff] [blame] | 3458 | createForStaticInitFunction(Values.IVSize, Values.IVSigned); |
Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 3459 | emitForStaticInitCall(CGF, UpdatedLocation, ThreadId, StaticInitFunction, |
Alexey Bataev | 0f87dbe | 2017-08-14 17:56:13 +0000 | [diff] [blame] | 3460 | ScheduleNum, ScheduleKind.M1, ScheduleKind.M2, Values); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3461 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3462 | |
Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 3463 | void CGOpenMPRuntime::emitDistributeStaticInit( |
| 3464 | CodeGenFunction &CGF, SourceLocation Loc, |
Alexey Bataev | 0f87dbe | 2017-08-14 17:56:13 +0000 | [diff] [blame] | 3465 | OpenMPDistScheduleClauseKind SchedKind, |
| 3466 | const CGOpenMPRuntime::StaticRTInput &Values) { |
| 3467 | OpenMPSchedType ScheduleNum = |
| 3468 | getRuntimeSchedule(SchedKind, Values.Chunk != nullptr); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3469 | llvm::Value *UpdatedLocation = |
Alexey Bataev | 0f87dbe | 2017-08-14 17:56:13 +0000 | [diff] [blame] | 3470 | emitUpdateLocation(CGF, Loc, OMP_IDENT_WORK_DISTRIBUTE); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3471 | llvm::Value *ThreadId = getThreadID(CGF, Loc); |
| 3472 | llvm::Constant *StaticInitFunction = |
Alexey Bataev | 0f87dbe | 2017-08-14 17:56:13 +0000 | [diff] [blame] | 3473 | createForStaticInitFunction(Values.IVSize, Values.IVSigned); |
Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 3474 | emitForStaticInitCall(CGF, UpdatedLocation, ThreadId, StaticInitFunction, |
| 3475 | ScheduleNum, OMPC_SCHEDULE_MODIFIER_unknown, |
Alexey Bataev | 0f87dbe | 2017-08-14 17:56:13 +0000 | [diff] [blame] | 3476 | OMPC_SCHEDULE_MODIFIER_unknown, Values); |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 3477 | } |
| 3478 | |
Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 3479 | void CGOpenMPRuntime::emitForStaticFinish(CodeGenFunction &CGF, |
Alexey Bataev | f43f714 | 2017-09-06 16:17:35 +0000 | [diff] [blame] | 3480 | SourceLocation Loc, |
| 3481 | OpenMPDirectiveKind DKind) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 3482 | if (!CGF.HaveInsertPoint()) |
| 3483 | return; |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 3484 | // Call __kmpc_for_static_fini(ident_t *loc, kmp_int32 tid); |
Alexey Bataev | f43f714 | 2017-09-06 16:17:35 +0000 | [diff] [blame] | 3485 | llvm::Value *Args[] = { |
| 3486 | emitUpdateLocation(CGF, Loc, |
| 3487 | isOpenMPDistributeDirective(DKind) |
| 3488 | ? OMP_IDENT_WORK_DISTRIBUTE |
| 3489 | : isOpenMPLoopDirective(DKind) |
| 3490 | ? OMP_IDENT_WORK_LOOP |
| 3491 | : OMP_IDENT_WORK_SECTIONS), |
| 3492 | getThreadID(CGF, Loc)}; |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 3493 | CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_for_static_fini), |
| 3494 | Args); |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 3495 | } |
| 3496 | |
Alexey Bataev | d7589ffe | 2015-05-20 13:12:48 +0000 | [diff] [blame] | 3497 | void CGOpenMPRuntime::emitForOrderedIterationEnd(CodeGenFunction &CGF, |
| 3498 | SourceLocation Loc, |
| 3499 | unsigned IVSize, |
| 3500 | bool IVSigned) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 3501 | if (!CGF.HaveInsertPoint()) |
| 3502 | return; |
Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 3503 | // 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] | 3504 | llvm::Value *Args[] = {emitUpdateLocation(CGF, Loc), getThreadID(CGF, Loc)}; |
Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 3505 | CGF.EmitRuntimeCall(createDispatchFiniFunction(IVSize, IVSigned), Args); |
| 3506 | } |
| 3507 | |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 3508 | llvm::Value *CGOpenMPRuntime::emitForNext(CodeGenFunction &CGF, |
| 3509 | SourceLocation Loc, unsigned IVSize, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3510 | bool IVSigned, Address IL, |
| 3511 | Address LB, Address UB, |
| 3512 | Address ST) { |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 3513 | // Call __kmpc_dispatch_next( |
| 3514 | // ident_t *loc, kmp_int32 tid, kmp_int32 *p_lastiter, |
| 3515 | // kmp_int[32|64] *p_lower, kmp_int[32|64] *p_upper, |
| 3516 | // kmp_int[32|64] *p_stride); |
| 3517 | llvm::Value *Args[] = { |
Alexey Bataev | 50b3c95 | 2016-02-19 10:38:26 +0000 | [diff] [blame] | 3518 | emitUpdateLocation(CGF, Loc), |
| 3519 | getThreadID(CGF, Loc), |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3520 | IL.getPointer(), // &isLastIter |
| 3521 | LB.getPointer(), // &Lower |
| 3522 | UB.getPointer(), // &Upper |
| 3523 | ST.getPointer() // &Stride |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 3524 | }; |
| 3525 | llvm::Value *Call = |
| 3526 | CGF.EmitRuntimeCall(createDispatchNextFunction(IVSize, IVSigned), Args); |
| 3527 | return CGF.EmitScalarConversion( |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3528 | Call, CGF.getContext().getIntTypeForBitwidth(32, /*Signed=*/1), |
Filipe Cabecinhas | 7af183d | 2015-08-11 04:19:28 +0000 | [diff] [blame] | 3529 | CGF.getContext().BoolTy, Loc); |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 3530 | } |
| 3531 | |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 3532 | void CGOpenMPRuntime::emitNumThreadsClause(CodeGenFunction &CGF, |
| 3533 | llvm::Value *NumThreads, |
| 3534 | SourceLocation Loc) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 3535 | if (!CGF.HaveInsertPoint()) |
| 3536 | return; |
Alexey Bataev | b205978 | 2014-10-13 08:23:51 +0000 | [diff] [blame] | 3537 | // Build call __kmpc_push_num_threads(&loc, global_tid, num_threads) |
| 3538 | llvm::Value *Args[] = { |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 3539 | emitUpdateLocation(CGF, Loc), getThreadID(CGF, Loc), |
Alexey Bataev | b205978 | 2014-10-13 08:23:51 +0000 | [diff] [blame] | 3540 | CGF.Builder.CreateIntCast(NumThreads, CGF.Int32Ty, /*isSigned*/ true)}; |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 3541 | CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_push_num_threads), |
| 3542 | Args); |
Alexey Bataev | b205978 | 2014-10-13 08:23:51 +0000 | [diff] [blame] | 3543 | } |
| 3544 | |
Alexey Bataev | 7f210c6 | 2015-06-18 13:40:03 +0000 | [diff] [blame] | 3545 | void CGOpenMPRuntime::emitProcBindClause(CodeGenFunction &CGF, |
| 3546 | OpenMPProcBindClauseKind ProcBind, |
| 3547 | SourceLocation Loc) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 3548 | if (!CGF.HaveInsertPoint()) |
| 3549 | return; |
Alexey Bataev | 7f210c6 | 2015-06-18 13:40:03 +0000 | [diff] [blame] | 3550 | // Constants for proc bind value accepted by the runtime. |
| 3551 | enum ProcBindTy { |
| 3552 | ProcBindFalse = 0, |
| 3553 | ProcBindTrue, |
| 3554 | ProcBindMaster, |
| 3555 | ProcBindClose, |
| 3556 | ProcBindSpread, |
| 3557 | ProcBindIntel, |
| 3558 | ProcBindDefault |
| 3559 | } RuntimeProcBind; |
| 3560 | switch (ProcBind) { |
| 3561 | case OMPC_PROC_BIND_master: |
| 3562 | RuntimeProcBind = ProcBindMaster; |
| 3563 | break; |
| 3564 | case OMPC_PROC_BIND_close: |
| 3565 | RuntimeProcBind = ProcBindClose; |
| 3566 | break; |
| 3567 | case OMPC_PROC_BIND_spread: |
| 3568 | RuntimeProcBind = ProcBindSpread; |
| 3569 | break; |
| 3570 | case OMPC_PROC_BIND_unknown: |
| 3571 | llvm_unreachable("Unsupported proc_bind value."); |
| 3572 | } |
| 3573 | // Build call __kmpc_push_proc_bind(&loc, global_tid, proc_bind) |
| 3574 | llvm::Value *Args[] = { |
| 3575 | emitUpdateLocation(CGF, Loc), getThreadID(CGF, Loc), |
| 3576 | llvm::ConstantInt::get(CGM.IntTy, RuntimeProcBind, /*isSigned=*/true)}; |
| 3577 | CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_push_proc_bind), Args); |
| 3578 | } |
| 3579 | |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 3580 | void CGOpenMPRuntime::emitFlush(CodeGenFunction &CGF, ArrayRef<const Expr *>, |
| 3581 | SourceLocation Loc) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 3582 | if (!CGF.HaveInsertPoint()) |
| 3583 | return; |
Alexey Bataev | d76df6d | 2015-02-24 12:55:09 +0000 | [diff] [blame] | 3584 | // Build call void __kmpc_flush(ident_t *loc) |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 3585 | CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_flush), |
| 3586 | emitUpdateLocation(CGF, Loc)); |
Alexey Bataev | cc37cc1 | 2014-11-20 04:34:54 +0000 | [diff] [blame] | 3587 | } |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 3588 | |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 3589 | namespace { |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 3590 | /// Indexes of fields for type kmp_task_t. |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 3591 | enum KmpTaskTFields { |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 3592 | /// List of shared variables. |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 3593 | KmpTaskTShareds, |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 3594 | /// Task routine. |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 3595 | KmpTaskTRoutine, |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 3596 | /// Partition id for the untied tasks. |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 3597 | KmpTaskTPartId, |
Alexey Bataev | ad537bb | 2016-05-30 09:06:50 +0000 | [diff] [blame] | 3598 | /// Function with call of destructors for private variables. |
| 3599 | Data1, |
| 3600 | /// Task priority. |
| 3601 | Data2, |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 3602 | /// (Taskloops only) Lower bound. |
| 3603 | KmpTaskTLowerBound, |
| 3604 | /// (Taskloops only) Upper bound. |
| 3605 | KmpTaskTUpperBound, |
| 3606 | /// (Taskloops only) Stride. |
| 3607 | KmpTaskTStride, |
| 3608 | /// (Taskloops only) Is last iteration flag. |
| 3609 | KmpTaskTLastIter, |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 3610 | /// (Taskloops only) Reduction data. |
| 3611 | KmpTaskTReductions, |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 3612 | }; |
Hans Wennborg | 7eb5464 | 2015-09-10 17:07:54 +0000 | [diff] [blame] | 3613 | } // anonymous namespace |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 3614 | |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3615 | bool CGOpenMPRuntime::OffloadEntriesInfoManagerTy::empty() const { |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 3616 | return OffloadEntriesTargetRegion.empty() && |
| 3617 | OffloadEntriesDeviceGlobalVar.empty(); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3618 | } |
| 3619 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 3620 | /// Initialize target region entry. |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3621 | void CGOpenMPRuntime::OffloadEntriesInfoManagerTy:: |
| 3622 | initializeTargetRegionEntryInfo(unsigned DeviceID, unsigned FileID, |
| 3623 | StringRef ParentName, unsigned LineNum, |
Samuel Antao | 2de62b0 | 2016-02-13 23:35:10 +0000 | [diff] [blame] | 3624 | unsigned Order) { |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3625 | assert(CGM.getLangOpts().OpenMPIsDevice && "Initialization of entries is " |
| 3626 | "only required for the device " |
| 3627 | "code generation."); |
Samuel Antao | 2de62b0 | 2016-02-13 23:35:10 +0000 | [diff] [blame] | 3628 | OffloadEntriesTargetRegion[DeviceID][FileID][ParentName][LineNum] = |
Samuel Antao | f83efdb | 2017-01-05 16:02:49 +0000 | [diff] [blame] | 3629 | OffloadEntryInfoTargetRegion(Order, /*Addr=*/nullptr, /*ID=*/nullptr, |
Alexey Bataev | 34f8a70 | 2018-03-28 14:28:54 +0000 | [diff] [blame] | 3630 | OMPTargetRegionEntryTargetRegion); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3631 | ++OffloadingEntriesNum; |
| 3632 | } |
| 3633 | |
| 3634 | void CGOpenMPRuntime::OffloadEntriesInfoManagerTy:: |
| 3635 | registerTargetRegionEntryInfo(unsigned DeviceID, unsigned FileID, |
| 3636 | StringRef ParentName, unsigned LineNum, |
Samuel Antao | f83efdb | 2017-01-05 16:02:49 +0000 | [diff] [blame] | 3637 | llvm::Constant *Addr, llvm::Constant *ID, |
Alexey Bataev | 34f8a70 | 2018-03-28 14:28:54 +0000 | [diff] [blame] | 3638 | OMPTargetRegionEntryKind Flags) { |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3639 | // If we are emitting code for a target, the entry is already initialized, |
| 3640 | // only has to be registered. |
| 3641 | if (CGM.getLangOpts().OpenMPIsDevice) { |
Alexey Bataev | 64e62dc | 2018-04-30 16:26:57 +0000 | [diff] [blame] | 3642 | if (!hasTargetRegionEntryInfo(DeviceID, FileID, ParentName, LineNum)) { |
| 3643 | unsigned DiagID = CGM.getDiags().getCustomDiagID( |
| 3644 | DiagnosticsEngine::Error, |
| 3645 | "Unable to find target region on line '%0' in the device code."); |
| 3646 | CGM.getDiags().Report(DiagID) << LineNum; |
| 3647 | return; |
| 3648 | } |
Samuel Antao | 2de62b0 | 2016-02-13 23:35:10 +0000 | [diff] [blame] | 3649 | auto &Entry = |
| 3650 | OffloadEntriesTargetRegion[DeviceID][FileID][ParentName][LineNum]; |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3651 | assert(Entry.isValid() && "Entry not initialized!"); |
| 3652 | Entry.setAddress(Addr); |
| 3653 | Entry.setID(ID); |
Samuel Antao | f83efdb | 2017-01-05 16:02:49 +0000 | [diff] [blame] | 3654 | Entry.setFlags(Flags); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3655 | } else { |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 3656 | OffloadEntryInfoTargetRegion Entry(OffloadingEntriesNum, Addr, ID, Flags); |
Samuel Antao | 2de62b0 | 2016-02-13 23:35:10 +0000 | [diff] [blame] | 3657 | OffloadEntriesTargetRegion[DeviceID][FileID][ParentName][LineNum] = Entry; |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 3658 | ++OffloadingEntriesNum; |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3659 | } |
| 3660 | } |
| 3661 | |
| 3662 | bool CGOpenMPRuntime::OffloadEntriesInfoManagerTy::hasTargetRegionEntryInfo( |
Samuel Antao | 2de62b0 | 2016-02-13 23:35:10 +0000 | [diff] [blame] | 3663 | unsigned DeviceID, unsigned FileID, StringRef ParentName, |
| 3664 | unsigned LineNum) const { |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3665 | auto PerDevice = OffloadEntriesTargetRegion.find(DeviceID); |
| 3666 | if (PerDevice == OffloadEntriesTargetRegion.end()) |
| 3667 | return false; |
| 3668 | auto PerFile = PerDevice->second.find(FileID); |
| 3669 | if (PerFile == PerDevice->second.end()) |
| 3670 | return false; |
| 3671 | auto PerParentName = PerFile->second.find(ParentName); |
| 3672 | if (PerParentName == PerFile->second.end()) |
| 3673 | return false; |
| 3674 | auto PerLine = PerParentName->second.find(LineNum); |
| 3675 | if (PerLine == PerParentName->second.end()) |
| 3676 | return false; |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3677 | // Fail if this entry is already registered. |
Samuel Antao | 2de62b0 | 2016-02-13 23:35:10 +0000 | [diff] [blame] | 3678 | if (PerLine->second.getAddress() || PerLine->second.getID()) |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3679 | return false; |
| 3680 | return true; |
| 3681 | } |
| 3682 | |
| 3683 | void CGOpenMPRuntime::OffloadEntriesInfoManagerTy::actOnTargetRegionEntriesInfo( |
| 3684 | const OffloadTargetRegionEntryInfoActTy &Action) { |
| 3685 | // Scan all target region entries and perform the provided action. |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 3686 | for (const auto &D : OffloadEntriesTargetRegion) |
| 3687 | for (const auto &F : D.second) |
| 3688 | for (const auto &P : F.second) |
| 3689 | for (const auto &L : P.second) |
Samuel Antao | 2de62b0 | 2016-02-13 23:35:10 +0000 | [diff] [blame] | 3690 | Action(D.first, F.first, P.first(), L.first, L.second); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3691 | } |
| 3692 | |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 3693 | void CGOpenMPRuntime::OffloadEntriesInfoManagerTy:: |
| 3694 | initializeDeviceGlobalVarEntryInfo(StringRef Name, |
| 3695 | OMPTargetGlobalVarEntryKind Flags, |
| 3696 | unsigned Order) { |
| 3697 | assert(CGM.getLangOpts().OpenMPIsDevice && "Initialization of entries is " |
| 3698 | "only required for the device " |
| 3699 | "code generation."); |
| 3700 | OffloadEntriesDeviceGlobalVar.try_emplace(Name, Order, Flags); |
| 3701 | ++OffloadingEntriesNum; |
| 3702 | } |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3703 | |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 3704 | void CGOpenMPRuntime::OffloadEntriesInfoManagerTy:: |
| 3705 | registerDeviceGlobalVarEntryInfo(StringRef VarName, llvm::Constant *Addr, |
| 3706 | CharUnits VarSize, |
| 3707 | OMPTargetGlobalVarEntryKind Flags, |
| 3708 | llvm::GlobalValue::LinkageTypes Linkage) { |
| 3709 | if (CGM.getLangOpts().OpenMPIsDevice) { |
| 3710 | auto &Entry = OffloadEntriesDeviceGlobalVar[VarName]; |
| 3711 | assert(Entry.isValid() && Entry.getFlags() == Flags && |
| 3712 | "Entry not initialized!"); |
| 3713 | assert((!Entry.getAddress() || Entry.getAddress() == Addr) && |
| 3714 | "Resetting with the new address."); |
| 3715 | if (Entry.getAddress() && hasDeviceGlobalVarEntryInfo(VarName)) |
| 3716 | return; |
| 3717 | Entry.setAddress(Addr); |
| 3718 | Entry.setVarSize(VarSize); |
| 3719 | Entry.setLinkage(Linkage); |
| 3720 | } else { |
| 3721 | if (hasDeviceGlobalVarEntryInfo(VarName)) |
| 3722 | return; |
| 3723 | OffloadEntriesDeviceGlobalVar.try_emplace( |
| 3724 | VarName, OffloadingEntriesNum, Addr, VarSize, Flags, Linkage); |
| 3725 | ++OffloadingEntriesNum; |
| 3726 | } |
| 3727 | } |
| 3728 | |
| 3729 | void CGOpenMPRuntime::OffloadEntriesInfoManagerTy:: |
| 3730 | actOnDeviceGlobalVarEntriesInfo( |
| 3731 | const OffloadDeviceGlobalVarEntryInfoActTy &Action) { |
| 3732 | // Scan all target region entries and perform the provided action. |
| 3733 | for (const auto &E : OffloadEntriesDeviceGlobalVar) |
| 3734 | Action(E.getKey(), E.getValue()); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3735 | } |
| 3736 | |
| 3737 | llvm::Function * |
| 3738 | CGOpenMPRuntime::createOffloadingBinaryDescriptorRegistration() { |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3739 | // If we don't have entries or if we are emitting code for the device, we |
| 3740 | // don't need to do anything. |
| 3741 | if (CGM.getLangOpts().OpenMPIsDevice || OffloadEntriesInfoManager.empty()) |
| 3742 | return nullptr; |
| 3743 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3744 | llvm::Module &M = CGM.getModule(); |
| 3745 | ASTContext &C = CGM.getContext(); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3746 | |
| 3747 | // Get list of devices we care about |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3748 | const std::vector<llvm::Triple> &Devices = CGM.getLangOpts().OMPTargetTriples; |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3749 | |
| 3750 | // We should be creating an offloading descriptor only if there are devices |
| 3751 | // specified. |
| 3752 | assert(!Devices.empty() && "No OpenMP offloading devices??"); |
| 3753 | |
| 3754 | // Create the external variables that will point to the begin and end of the |
| 3755 | // host entries section. These will be defined by the linker. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3756 | llvm::Type *OffloadEntryTy = |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3757 | CGM.getTypes().ConvertTypeForMem(getTgtOffloadEntryQTy()); |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 3758 | std::string EntriesBeginName = getName({"omp_offloading", "entries_begin"}); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3759 | auto *HostEntriesBegin = new llvm::GlobalVariable( |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3760 | M, OffloadEntryTy, /*isConstant=*/true, |
Eugene Zelenko | 1660a5d | 2016-01-26 19:01:06 +0000 | [diff] [blame] | 3761 | llvm::GlobalValue::ExternalLinkage, /*Initializer=*/nullptr, |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 3762 | EntriesBeginName); |
| 3763 | std::string EntriesEndName = getName({"omp_offloading", "entries_end"}); |
| 3764 | auto *HostEntriesEnd = |
| 3765 | new llvm::GlobalVariable(M, OffloadEntryTy, /*isConstant=*/true, |
| 3766 | llvm::GlobalValue::ExternalLinkage, |
| 3767 | /*Initializer=*/nullptr, EntriesEndName); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3768 | |
| 3769 | // Create all device images |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3770 | auto *DeviceImageTy = cast<llvm::StructType>( |
| 3771 | CGM.getTypes().ConvertTypeForMem(getTgtDeviceImageQTy())); |
John McCall | 23c9dc6 | 2016-11-28 22:18:27 +0000 | [diff] [blame] | 3772 | ConstantInitBuilder DeviceImagesBuilder(CGM); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3773 | ConstantArrayBuilder DeviceImagesEntries = |
| 3774 | DeviceImagesBuilder.beginArray(DeviceImageTy); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3775 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3776 | for (const llvm::Triple &Device : Devices) { |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 3777 | StringRef T = Device.getTriple(); |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 3778 | std::string BeginName = getName({"omp_offloading", "img_start", ""}); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3779 | auto *ImgBegin = new llvm::GlobalVariable( |
Alexey Bataev | 62a4cb0 | 2018-07-31 18:27:42 +0000 | [diff] [blame] | 3780 | M, CGM.Int8Ty, /*isConstant=*/true, |
| 3781 | llvm::GlobalValue::ExternalWeakLinkage, |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 3782 | /*Initializer=*/nullptr, Twine(BeginName).concat(T)); |
| 3783 | std::string EndName = getName({"omp_offloading", "img_end", ""}); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3784 | auto *ImgEnd = new llvm::GlobalVariable( |
Alexey Bataev | 62a4cb0 | 2018-07-31 18:27:42 +0000 | [diff] [blame] | 3785 | M, CGM.Int8Ty, /*isConstant=*/true, |
| 3786 | llvm::GlobalValue::ExternalWeakLinkage, |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 3787 | /*Initializer=*/nullptr, Twine(EndName).concat(T)); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3788 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3789 | llvm::Constant *Data[] = {ImgBegin, ImgEnd, HostEntriesBegin, |
| 3790 | HostEntriesEnd}; |
| 3791 | createConstantGlobalStructAndAddToParent(CGM, getTgtDeviceImageQTy(), Data, |
| 3792 | DeviceImagesEntries); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3793 | } |
| 3794 | |
| 3795 | // Create device images global array. |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 3796 | std::string ImagesName = getName({"omp_offloading", "device_images"}); |
John McCall | 6c9f1fdb | 2016-11-19 08:17:24 +0000 | [diff] [blame] | 3797 | llvm::GlobalVariable *DeviceImages = |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 3798 | DeviceImagesEntries.finishAndCreateGlobal(ImagesName, |
| 3799 | CGM.getPointerAlign(), |
| 3800 | /*isConstant=*/true); |
Peter Collingbourne | bcf909d | 2016-06-14 21:02:05 +0000 | [diff] [blame] | 3801 | DeviceImages->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3802 | |
| 3803 | // This is a Zero array to be used in the creation of the constant expressions |
| 3804 | llvm::Constant *Index[] = {llvm::Constant::getNullValue(CGM.Int32Ty), |
| 3805 | llvm::Constant::getNullValue(CGM.Int32Ty)}; |
| 3806 | |
| 3807 | // Create the target region descriptor. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3808 | llvm::Constant *Data[] = { |
| 3809 | llvm::ConstantInt::get(CGM.Int32Ty, Devices.size()), |
| 3810 | llvm::ConstantExpr::getGetElementPtr(DeviceImages->getValueType(), |
| 3811 | DeviceImages, Index), |
| 3812 | HostEntriesBegin, HostEntriesEnd}; |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 3813 | std::string Descriptor = getName({"omp_offloading", "descriptor"}); |
Mike Rice | e1ca7b6 | 2018-08-29 15:45:11 +0000 | [diff] [blame] | 3814 | llvm::GlobalVariable *Desc = createGlobalStruct( |
| 3815 | CGM, getTgtBinaryDescriptorQTy(), /*IsConstant=*/true, Data, Descriptor); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3816 | |
| 3817 | // Emit code to register or unregister the descriptor at execution |
| 3818 | // startup or closing, respectively. |
| 3819 | |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 3820 | llvm::Function *UnRegFn; |
| 3821 | { |
| 3822 | FunctionArgList Args; |
| 3823 | ImplicitParamDecl DummyPtr(C, C.VoidPtrTy, ImplicitParamDecl::Other); |
| 3824 | Args.push_back(&DummyPtr); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3825 | |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 3826 | CodeGenFunction CGF(CGM); |
| 3827 | // Disable debug info for global (de-)initializer because they are not part |
| 3828 | // of some particular construct. |
| 3829 | CGF.disableDebugInfo(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3830 | const auto &FI = |
| 3831 | CGM.getTypes().arrangeBuiltinFunctionDeclaration(C.VoidTy, Args); |
| 3832 | llvm::FunctionType *FTy = CGM.getTypes().GetFunctionType(FI); |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 3833 | std::string UnregName = getName({"omp_offloading", "descriptor_unreg"}); |
| 3834 | UnRegFn = CGM.CreateGlobalInitOrDestructFunction(FTy, UnregName, FI); |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 3835 | CGF.StartFunction(GlobalDecl(), C.VoidTy, UnRegFn, FI, Args); |
| 3836 | CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__tgt_unregister_lib), |
| 3837 | Desc); |
| 3838 | CGF.FinishFunction(); |
| 3839 | } |
| 3840 | llvm::Function *RegFn; |
| 3841 | { |
| 3842 | CodeGenFunction CGF(CGM); |
| 3843 | // Disable debug info for global (de-)initializer because they are not part |
| 3844 | // of some particular construct. |
| 3845 | CGF.disableDebugInfo(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3846 | const auto &FI = CGM.getTypes().arrangeNullaryFunction(); |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 3847 | llvm::FunctionType *FTy = CGM.getTypes().GetFunctionType(FI); |
Sergey Dmitriev | bde9cf9 | 2018-08-03 20:19:28 +0000 | [diff] [blame] | 3848 | |
| 3849 | // Encode offload target triples into the registration function name. It |
| 3850 | // will serve as a comdat key for the registration/unregistration code for |
| 3851 | // this particular combination of offloading targets. |
| 3852 | SmallVector<StringRef, 4U> RegFnNameParts(Devices.size() + 2U); |
| 3853 | RegFnNameParts[0] = "omp_offloading"; |
| 3854 | RegFnNameParts[1] = "descriptor_reg"; |
| 3855 | llvm::transform(Devices, std::next(RegFnNameParts.begin(), 2), |
| 3856 | [](const llvm::Triple &T) -> const std::string& { |
| 3857 | return T.getTriple(); |
| 3858 | }); |
| 3859 | llvm::sort(std::next(RegFnNameParts.begin(), 2), RegFnNameParts.end()); |
| 3860 | std::string Descriptor = getName(RegFnNameParts); |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 3861 | RegFn = CGM.CreateGlobalInitOrDestructFunction(FTy, Descriptor, FI); |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 3862 | CGF.StartFunction(GlobalDecl(), C.VoidTy, RegFn, FI, FunctionArgList()); |
| 3863 | CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__tgt_register_lib), Desc); |
| 3864 | // Create a variable to drive the registration and unregistration of the |
| 3865 | // descriptor, so we can reuse the logic that emits Ctors and Dtors. |
| 3866 | ImplicitParamDecl RegUnregVar(C, C.getTranslationUnitDecl(), |
| 3867 | SourceLocation(), nullptr, C.CharTy, |
| 3868 | ImplicitParamDecl::Other); |
| 3869 | CGM.getCXXABI().registerGlobalDtor(CGF, RegUnregVar, UnRegFn, Desc); |
| 3870 | CGF.FinishFunction(); |
| 3871 | } |
George Rokos | 29d0f00 | 2017-05-27 03:03:13 +0000 | [diff] [blame] | 3872 | if (CGM.supportsCOMDAT()) { |
| 3873 | // It is sufficient to call registration function only once, so create a |
| 3874 | // COMDAT group for registration/unregistration functions and associated |
| 3875 | // data. That would reduce startup time and code size. Registration |
| 3876 | // function serves as a COMDAT group key. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3877 | llvm::Comdat *ComdatKey = M.getOrInsertComdat(RegFn->getName()); |
George Rokos | 29d0f00 | 2017-05-27 03:03:13 +0000 | [diff] [blame] | 3878 | RegFn->setLinkage(llvm::GlobalValue::LinkOnceAnyLinkage); |
| 3879 | RegFn->setVisibility(llvm::GlobalValue::HiddenVisibility); |
| 3880 | RegFn->setComdat(ComdatKey); |
| 3881 | UnRegFn->setComdat(ComdatKey); |
| 3882 | DeviceImages->setComdat(ComdatKey); |
| 3883 | Desc->setComdat(ComdatKey); |
| 3884 | } |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3885 | return RegFn; |
| 3886 | } |
| 3887 | |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 3888 | void CGOpenMPRuntime::createOffloadEntry( |
| 3889 | llvm::Constant *ID, llvm::Constant *Addr, uint64_t Size, int32_t Flags, |
| 3890 | llvm::GlobalValue::LinkageTypes Linkage) { |
Samuel Antao | 2de62b0 | 2016-02-13 23:35:10 +0000 | [diff] [blame] | 3891 | StringRef Name = Addr->getName(); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3892 | llvm::Module &M = CGM.getModule(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3893 | llvm::LLVMContext &C = M.getContext(); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3894 | |
| 3895 | // Create constant string with the name. |
| 3896 | llvm::Constant *StrPtrInit = llvm::ConstantDataArray::getString(C, Name); |
| 3897 | |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 3898 | std::string StringName = getName({"omp_offloading", "entry_name"}); |
| 3899 | auto *Str = new llvm::GlobalVariable( |
| 3900 | M, StrPtrInit->getType(), /*isConstant=*/true, |
| 3901 | llvm::GlobalValue::InternalLinkage, StrPtrInit, StringName); |
Peter Collingbourne | bcf909d | 2016-06-14 21:02:05 +0000 | [diff] [blame] | 3902 | Str->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3903 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3904 | llvm::Constant *Data[] = {llvm::ConstantExpr::getBitCast(ID, CGM.VoidPtrTy), |
| 3905 | llvm::ConstantExpr::getBitCast(Str, CGM.Int8PtrTy), |
| 3906 | llvm::ConstantInt::get(CGM.SizeTy, Size), |
| 3907 | llvm::ConstantInt::get(CGM.Int32Ty, Flags), |
| 3908 | llvm::ConstantInt::get(CGM.Int32Ty, 0)}; |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 3909 | std::string EntryName = getName({"omp_offloading", "entry", ""}); |
Mike Rice | e1ca7b6 | 2018-08-29 15:45:11 +0000 | [diff] [blame] | 3910 | llvm::GlobalVariable *Entry = createGlobalStruct( |
| 3911 | CGM, getTgtOffloadEntryQTy(), /*IsConstant=*/true, Data, |
| 3912 | Twine(EntryName).concat(Name), llvm::GlobalValue::WeakAnyLinkage); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3913 | |
| 3914 | // The entry has to be created in the section the linker expects it to be. |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 3915 | std::string Section = getName({"omp_offloading", "entries"}); |
| 3916 | Entry->setSection(Section); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3917 | } |
| 3918 | |
| 3919 | void CGOpenMPRuntime::createOffloadEntriesAndInfoMetadata() { |
| 3920 | // Emit the offloading entries and metadata so that the device codegen side |
Samuel Antao | 4c8035b | 2016-12-12 18:00:20 +0000 | [diff] [blame] | 3921 | // can easily figure out what to emit. The produced metadata looks like |
| 3922 | // this: |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3923 | // |
| 3924 | // !omp_offload.info = !{!1, ...} |
| 3925 | // |
| 3926 | // Right now we only generate metadata for function that contain target |
| 3927 | // regions. |
| 3928 | |
Alexander Kornienko | 2a8c18d | 2018-04-06 15:14:32 +0000 | [diff] [blame] | 3929 | // 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] | 3930 | if (OffloadEntriesInfoManager.empty()) |
| 3931 | return; |
| 3932 | |
| 3933 | llvm::Module &M = CGM.getModule(); |
| 3934 | llvm::LLVMContext &C = M.getContext(); |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 3935 | SmallVector<const OffloadEntriesInfoManagerTy::OffloadEntryInfo *, 16> |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3936 | OrderedEntries(OffloadEntriesInfoManager.size()); |
Alexey Bataev | 2a6f3f5 | 2018-11-07 19:11:14 +0000 | [diff] [blame] | 3937 | llvm::SmallVector<StringRef, 16> ParentFunctions( |
| 3938 | OffloadEntriesInfoManager.size()); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3939 | |
Simon Pilgrim | 2c51880 | 2017-03-30 14:13:19 +0000 | [diff] [blame] | 3940 | // Auxiliary methods to create metadata values and strings. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3941 | auto &&GetMDInt = [this](unsigned V) { |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3942 | return llvm::ConstantAsMetadata::get( |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3943 | llvm::ConstantInt::get(CGM.Int32Ty, V)); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3944 | }; |
| 3945 | |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 3946 | auto &&GetMDString = [&C](StringRef V) { return llvm::MDString::get(C, V); }; |
| 3947 | |
| 3948 | // Create the offloading info metadata node. |
| 3949 | llvm::NamedMDNode *MD = M.getOrInsertNamedMetadata("omp_offload.info"); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3950 | |
| 3951 | // Create function that emits metadata for each target region entry; |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 3952 | auto &&TargetRegionMetadataEmitter = |
Alexey Bataev | 2a6f3f5 | 2018-11-07 19:11:14 +0000 | [diff] [blame] | 3953 | [&C, MD, &OrderedEntries, &ParentFunctions, &GetMDInt, &GetMDString]( |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 3954 | unsigned DeviceID, unsigned FileID, StringRef ParentName, |
| 3955 | unsigned Line, |
| 3956 | const OffloadEntriesInfoManagerTy::OffloadEntryInfoTargetRegion &E) { |
| 3957 | // Generate metadata for target regions. Each entry of this metadata |
| 3958 | // contains: |
| 3959 | // - Entry 0 -> Kind of this type of metadata (0). |
| 3960 | // - Entry 1 -> Device ID of the file where the entry was identified. |
| 3961 | // - Entry 2 -> File ID of the file where the entry was identified. |
| 3962 | // - Entry 3 -> Mangled name of the function where the entry was |
| 3963 | // identified. |
| 3964 | // - Entry 4 -> Line in the file where the entry was identified. |
| 3965 | // - Entry 5 -> Order the entry was created. |
| 3966 | // The first element of the metadata node is the kind. |
| 3967 | llvm::Metadata *Ops[] = {GetMDInt(E.getKind()), GetMDInt(DeviceID), |
| 3968 | GetMDInt(FileID), GetMDString(ParentName), |
| 3969 | GetMDInt(Line), GetMDInt(E.getOrder())}; |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3970 | |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 3971 | // Save this entry in the right position of the ordered entries array. |
| 3972 | OrderedEntries[E.getOrder()] = &E; |
Alexey Bataev | 2a6f3f5 | 2018-11-07 19:11:14 +0000 | [diff] [blame] | 3973 | ParentFunctions[E.getOrder()] = ParentName; |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3974 | |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 3975 | // Add metadata to the named metadata node. |
| 3976 | MD->addOperand(llvm::MDNode::get(C, Ops)); |
| 3977 | }; |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3978 | |
| 3979 | OffloadEntriesInfoManager.actOnTargetRegionEntriesInfo( |
| 3980 | TargetRegionMetadataEmitter); |
| 3981 | |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 3982 | // Create function that emits metadata for each device global variable entry; |
| 3983 | auto &&DeviceGlobalVarMetadataEmitter = |
| 3984 | [&C, &OrderedEntries, &GetMDInt, &GetMDString, |
| 3985 | MD](StringRef MangledName, |
| 3986 | const OffloadEntriesInfoManagerTy::OffloadEntryInfoDeviceGlobalVar |
| 3987 | &E) { |
| 3988 | // Generate metadata for global variables. Each entry of this metadata |
| 3989 | // contains: |
| 3990 | // - Entry 0 -> Kind of this type of metadata (1). |
| 3991 | // - Entry 1 -> Mangled name of the variable. |
| 3992 | // - Entry 2 -> Declare target kind. |
| 3993 | // - Entry 3 -> Order the entry was created. |
| 3994 | // The first element of the metadata node is the kind. |
| 3995 | llvm::Metadata *Ops[] = { |
| 3996 | GetMDInt(E.getKind()), GetMDString(MangledName), |
| 3997 | GetMDInt(E.getFlags()), GetMDInt(E.getOrder())}; |
| 3998 | |
| 3999 | // Save this entry in the right position of the ordered entries array. |
| 4000 | OrderedEntries[E.getOrder()] = &E; |
| 4001 | |
| 4002 | // Add metadata to the named metadata node. |
| 4003 | MD->addOperand(llvm::MDNode::get(C, Ops)); |
| 4004 | }; |
| 4005 | |
| 4006 | OffloadEntriesInfoManager.actOnDeviceGlobalVarEntriesInfo( |
| 4007 | DeviceGlobalVarMetadataEmitter); |
| 4008 | |
| 4009 | for (const auto *E : OrderedEntries) { |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4010 | assert(E && "All ordered entries must exist!"); |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 4011 | if (const auto *CE = |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4012 | dyn_cast<OffloadEntriesInfoManagerTy::OffloadEntryInfoTargetRegion>( |
| 4013 | E)) { |
Alexey Bataev | 64e62dc | 2018-04-30 16:26:57 +0000 | [diff] [blame] | 4014 | if (!CE->getID() || !CE->getAddress()) { |
Alexey Bataev | 2a6f3f5 | 2018-11-07 19:11:14 +0000 | [diff] [blame] | 4015 | // Do not blame the entry if the parent funtion is not emitted. |
| 4016 | StringRef FnName = ParentFunctions[CE->getOrder()]; |
| 4017 | if (!CGM.GetGlobalValue(FnName)) |
| 4018 | continue; |
Alexey Bataev | 64e62dc | 2018-04-30 16:26:57 +0000 | [diff] [blame] | 4019 | unsigned DiagID = CGM.getDiags().getCustomDiagID( |
| 4020 | DiagnosticsEngine::Error, |
Alexey Bataev | 7f01d20 | 2018-07-16 18:12:18 +0000 | [diff] [blame] | 4021 | "Offloading entry for target region is incorrect: either the " |
Alexey Bataev | 64e62dc | 2018-04-30 16:26:57 +0000 | [diff] [blame] | 4022 | "address or the ID is invalid."); |
| 4023 | CGM.getDiags().Report(DiagID); |
| 4024 | continue; |
| 4025 | } |
Alexey Bataev | 34f8a70 | 2018-03-28 14:28:54 +0000 | [diff] [blame] | 4026 | createOffloadEntry(CE->getID(), CE->getAddress(), /*Size=*/0, |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 4027 | CE->getFlags(), llvm::GlobalValue::WeakAnyLinkage); |
| 4028 | } else if (const auto *CE = |
| 4029 | dyn_cast<OffloadEntriesInfoManagerTy:: |
| 4030 | OffloadEntryInfoDeviceGlobalVar>(E)) { |
Alexey Bataev | c52f01d | 2018-07-16 20:05:25 +0000 | [diff] [blame] | 4031 | OffloadEntriesInfoManagerTy::OMPTargetGlobalVarEntryKind Flags = |
| 4032 | static_cast<OffloadEntriesInfoManagerTy::OMPTargetGlobalVarEntryKind>( |
| 4033 | CE->getFlags()); |
| 4034 | switch (Flags) { |
| 4035 | case OffloadEntriesInfoManagerTy::OMPTargetGlobalVarEntryTo: { |
| 4036 | if (!CE->getAddress()) { |
| 4037 | unsigned DiagID = CGM.getDiags().getCustomDiagID( |
| 4038 | DiagnosticsEngine::Error, |
| 4039 | "Offloading entry for declare target variable is incorrect: the " |
| 4040 | "address is invalid."); |
| 4041 | CGM.getDiags().Report(DiagID); |
| 4042 | continue; |
| 4043 | } |
Alexey Bataev | b4dd6d2 | 2018-08-29 20:41:37 +0000 | [diff] [blame] | 4044 | // The vaiable has no definition - no need to add the entry. |
| 4045 | if (CE->getVarSize().isZero()) |
| 4046 | continue; |
Alexey Bataev | c52f01d | 2018-07-16 20:05:25 +0000 | [diff] [blame] | 4047 | break; |
| 4048 | } |
| 4049 | case OffloadEntriesInfoManagerTy::OMPTargetGlobalVarEntryLink: |
| 4050 | assert(((CGM.getLangOpts().OpenMPIsDevice && !CE->getAddress()) || |
| 4051 | (!CGM.getLangOpts().OpenMPIsDevice && CE->getAddress())) && |
| 4052 | "Declaret target link address is set."); |
| 4053 | if (CGM.getLangOpts().OpenMPIsDevice) |
| 4054 | continue; |
| 4055 | if (!CE->getAddress()) { |
| 4056 | unsigned DiagID = CGM.getDiags().getCustomDiagID( |
| 4057 | DiagnosticsEngine::Error, |
| 4058 | "Offloading entry for declare target variable is incorrect: the " |
| 4059 | "address is invalid."); |
| 4060 | CGM.getDiags().Report(DiagID); |
| 4061 | continue; |
| 4062 | } |
| 4063 | break; |
Alexey Bataev | 64e62dc | 2018-04-30 16:26:57 +0000 | [diff] [blame] | 4064 | } |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 4065 | createOffloadEntry(CE->getAddress(), CE->getAddress(), |
Alexey Bataev | c52f01d | 2018-07-16 20:05:25 +0000 | [diff] [blame] | 4066 | CE->getVarSize().getQuantity(), Flags, |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 4067 | CE->getLinkage()); |
| 4068 | } else { |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4069 | llvm_unreachable("Unsupported entry kind."); |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 4070 | } |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4071 | } |
| 4072 | } |
| 4073 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4074 | /// Loads all the offload entries information from the host IR |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4075 | /// metadata. |
| 4076 | void CGOpenMPRuntime::loadOffloadInfoMetadata() { |
| 4077 | // If we are in target mode, load the metadata from the host IR. This code has |
| 4078 | // to match the metadaata creation in createOffloadEntriesAndInfoMetadata(). |
| 4079 | |
| 4080 | if (!CGM.getLangOpts().OpenMPIsDevice) |
| 4081 | return; |
| 4082 | |
| 4083 | if (CGM.getLangOpts().OMPHostIRFile.empty()) |
| 4084 | return; |
| 4085 | |
| 4086 | auto Buf = llvm::MemoryBuffer::getFile(CGM.getLangOpts().OMPHostIRFile); |
Alexey Bataev | 64e62dc | 2018-04-30 16:26:57 +0000 | [diff] [blame] | 4087 | if (auto EC = Buf.getError()) { |
| 4088 | CGM.getDiags().Report(diag::err_cannot_open_file) |
| 4089 | << CGM.getLangOpts().OMPHostIRFile << EC.message(); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4090 | return; |
Alexey Bataev | 64e62dc | 2018-04-30 16:26:57 +0000 | [diff] [blame] | 4091 | } |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4092 | |
| 4093 | llvm::LLVMContext C; |
Peter Collingbourne | d9445c4 | 2016-11-13 07:00:17 +0000 | [diff] [blame] | 4094 | auto ME = expectedToErrorOrAndEmitErrors( |
| 4095 | C, llvm::parseBitcodeFile(Buf.get()->getMemBufferRef(), C)); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4096 | |
Alexey Bataev | 64e62dc | 2018-04-30 16:26:57 +0000 | [diff] [blame] | 4097 | if (auto EC = ME.getError()) { |
| 4098 | unsigned DiagID = CGM.getDiags().getCustomDiagID( |
| 4099 | DiagnosticsEngine::Error, "Unable to parse host IR file '%0':'%1'"); |
| 4100 | CGM.getDiags().Report(DiagID) |
| 4101 | << CGM.getLangOpts().OMPHostIRFile << EC.message(); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4102 | return; |
Alexey Bataev | 64e62dc | 2018-04-30 16:26:57 +0000 | [diff] [blame] | 4103 | } |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4104 | |
| 4105 | llvm::NamedMDNode *MD = ME.get()->getNamedMetadata("omp_offload.info"); |
| 4106 | if (!MD) |
| 4107 | return; |
| 4108 | |
George Burgess IV | 00f70bd | 2018-03-01 05:43:23 +0000 | [diff] [blame] | 4109 | for (llvm::MDNode *MN : MD->operands()) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4110 | auto &&GetMDInt = [MN](unsigned Idx) { |
| 4111 | auto *V = cast<llvm::ConstantAsMetadata>(MN->getOperand(Idx)); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4112 | return cast<llvm::ConstantInt>(V->getValue())->getZExtValue(); |
| 4113 | }; |
| 4114 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4115 | auto &&GetMDString = [MN](unsigned Idx) { |
| 4116 | auto *V = cast<llvm::MDString>(MN->getOperand(Idx)); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4117 | return V->getString(); |
| 4118 | }; |
| 4119 | |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 4120 | switch (GetMDInt(0)) { |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4121 | default: |
| 4122 | llvm_unreachable("Unexpected metadata!"); |
| 4123 | break; |
| 4124 | case OffloadEntriesInfoManagerTy::OffloadEntryInfo:: |
Alexey Bataev | 34f8a70 | 2018-03-28 14:28:54 +0000 | [diff] [blame] | 4125 | OffloadingEntryInfoTargetRegion: |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4126 | OffloadEntriesInfoManager.initializeTargetRegionEntryInfo( |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 4127 | /*DeviceID=*/GetMDInt(1), /*FileID=*/GetMDInt(2), |
| 4128 | /*ParentName=*/GetMDString(3), /*Line=*/GetMDInt(4), |
| 4129 | /*Order=*/GetMDInt(5)); |
| 4130 | break; |
| 4131 | case OffloadEntriesInfoManagerTy::OffloadEntryInfo:: |
| 4132 | OffloadingEntryInfoDeviceGlobalVar: |
| 4133 | OffloadEntriesInfoManager.initializeDeviceGlobalVarEntryInfo( |
| 4134 | /*MangledName=*/GetMDString(1), |
| 4135 | static_cast<OffloadEntriesInfoManagerTy::OMPTargetGlobalVarEntryKind>( |
| 4136 | /*Flags=*/GetMDInt(2)), |
| 4137 | /*Order=*/GetMDInt(3)); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4138 | break; |
| 4139 | } |
| 4140 | } |
| 4141 | } |
| 4142 | |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 4143 | void CGOpenMPRuntime::emitKmpRoutineEntryT(QualType KmpInt32Ty) { |
| 4144 | if (!KmpRoutineEntryPtrTy) { |
| 4145 | // Build typedef kmp_int32 (* kmp_routine_entry_t)(kmp_int32, void *); type. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4146 | ASTContext &C = CGM.getContext(); |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 4147 | QualType KmpRoutineEntryTyArgs[] = {KmpInt32Ty, C.VoidPtrTy}; |
| 4148 | FunctionProtoType::ExtProtoInfo EPI; |
| 4149 | KmpRoutineEntryPtrQTy = C.getPointerType( |
| 4150 | C.getFunctionType(KmpInt32Ty, KmpRoutineEntryTyArgs, EPI)); |
| 4151 | KmpRoutineEntryPtrTy = CGM.getTypes().ConvertType(KmpRoutineEntryPtrQTy); |
| 4152 | } |
| 4153 | } |
| 4154 | |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4155 | QualType CGOpenMPRuntime::getTgtOffloadEntryQTy() { |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4156 | // Make sure the type of the entry is already created. This is the type we |
| 4157 | // have to create: |
| 4158 | // struct __tgt_offload_entry{ |
| 4159 | // void *addr; // Pointer to the offload entry info. |
| 4160 | // // (function or global) |
| 4161 | // char *name; // Name of the function or global. |
| 4162 | // 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] | 4163 | // int32_t flags; // Flags associated with the entry, e.g. 'link'. |
| 4164 | // int32_t reserved; // Reserved, to use by the runtime library. |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4165 | // }; |
| 4166 | if (TgtOffloadEntryQTy.isNull()) { |
| 4167 | ASTContext &C = CGM.getContext(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4168 | RecordDecl *RD = C.buildImplicitRecord("__tgt_offload_entry"); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4169 | RD->startDefinition(); |
| 4170 | addFieldToRecordDecl(C, RD, C.VoidPtrTy); |
| 4171 | addFieldToRecordDecl(C, RD, C.getPointerType(C.CharTy)); |
| 4172 | addFieldToRecordDecl(C, RD, C.getSizeType()); |
Samuel Antao | f83efdb | 2017-01-05 16:02:49 +0000 | [diff] [blame] | 4173 | addFieldToRecordDecl( |
| 4174 | C, RD, C.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/true)); |
| 4175 | addFieldToRecordDecl( |
| 4176 | C, RD, C.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/true)); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4177 | RD->completeDefinition(); |
Jonas Hahnfeld | 5e4df28 | 2018-01-18 15:38:03 +0000 | [diff] [blame] | 4178 | RD->addAttr(PackedAttr::CreateImplicit(C)); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4179 | TgtOffloadEntryQTy = C.getRecordType(RD); |
| 4180 | } |
| 4181 | return TgtOffloadEntryQTy; |
| 4182 | } |
| 4183 | |
| 4184 | QualType CGOpenMPRuntime::getTgtDeviceImageQTy() { |
| 4185 | // These are the types we need to build: |
| 4186 | // struct __tgt_device_image{ |
| 4187 | // void *ImageStart; // Pointer to the target code start. |
| 4188 | // void *ImageEnd; // Pointer to the target code end. |
| 4189 | // // We also add the host entries to the device image, as it may be useful |
| 4190 | // // for the target runtime to have access to that information. |
| 4191 | // __tgt_offload_entry *EntriesBegin; // Begin of the table with all |
| 4192 | // // the entries. |
| 4193 | // __tgt_offload_entry *EntriesEnd; // End of the table with all the |
| 4194 | // // entries (non inclusive). |
| 4195 | // }; |
| 4196 | if (TgtDeviceImageQTy.isNull()) { |
| 4197 | ASTContext &C = CGM.getContext(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4198 | RecordDecl *RD = C.buildImplicitRecord("__tgt_device_image"); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4199 | RD->startDefinition(); |
| 4200 | addFieldToRecordDecl(C, RD, C.VoidPtrTy); |
| 4201 | addFieldToRecordDecl(C, RD, C.VoidPtrTy); |
| 4202 | addFieldToRecordDecl(C, RD, C.getPointerType(getTgtOffloadEntryQTy())); |
| 4203 | addFieldToRecordDecl(C, RD, C.getPointerType(getTgtOffloadEntryQTy())); |
| 4204 | RD->completeDefinition(); |
| 4205 | TgtDeviceImageQTy = C.getRecordType(RD); |
| 4206 | } |
| 4207 | return TgtDeviceImageQTy; |
| 4208 | } |
| 4209 | |
| 4210 | QualType CGOpenMPRuntime::getTgtBinaryDescriptorQTy() { |
| 4211 | // struct __tgt_bin_desc{ |
| 4212 | // int32_t NumDevices; // Number of devices supported. |
| 4213 | // __tgt_device_image *DeviceImages; // Arrays of device images |
| 4214 | // // (one per device). |
| 4215 | // __tgt_offload_entry *EntriesBegin; // Begin of the table with all the |
| 4216 | // // entries. |
| 4217 | // __tgt_offload_entry *EntriesEnd; // End of the table with all the |
| 4218 | // // entries (non inclusive). |
| 4219 | // }; |
| 4220 | if (TgtBinaryDescriptorQTy.isNull()) { |
| 4221 | ASTContext &C = CGM.getContext(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4222 | RecordDecl *RD = C.buildImplicitRecord("__tgt_bin_desc"); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4223 | RD->startDefinition(); |
| 4224 | addFieldToRecordDecl( |
| 4225 | C, RD, C.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/true)); |
| 4226 | addFieldToRecordDecl(C, RD, C.getPointerType(getTgtDeviceImageQTy())); |
| 4227 | addFieldToRecordDecl(C, RD, C.getPointerType(getTgtOffloadEntryQTy())); |
| 4228 | addFieldToRecordDecl(C, RD, C.getPointerType(getTgtOffloadEntryQTy())); |
| 4229 | RD->completeDefinition(); |
| 4230 | TgtBinaryDescriptorQTy = C.getRecordType(RD); |
| 4231 | } |
| 4232 | return TgtBinaryDescriptorQTy; |
| 4233 | } |
| 4234 | |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 4235 | namespace { |
Alexey Bataev | 9e03404 | 2015-05-05 04:05:12 +0000 | [diff] [blame] | 4236 | struct PrivateHelpersTy { |
| 4237 | PrivateHelpersTy(const VarDecl *Original, const VarDecl *PrivateCopy, |
| 4238 | const VarDecl *PrivateElemInit) |
| 4239 | : Original(Original), PrivateCopy(PrivateCopy), |
| 4240 | PrivateElemInit(PrivateElemInit) {} |
| 4241 | const VarDecl *Original; |
| 4242 | const VarDecl *PrivateCopy; |
| 4243 | const VarDecl *PrivateElemInit; |
| 4244 | }; |
| 4245 | typedef std::pair<CharUnits /*Align*/, PrivateHelpersTy> PrivateDataTy; |
Hans Wennborg | 7eb5464 | 2015-09-10 17:07:54 +0000 | [diff] [blame] | 4246 | } // anonymous namespace |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 4247 | |
Alexey Bataev | 9e03404 | 2015-05-05 04:05:12 +0000 | [diff] [blame] | 4248 | static RecordDecl * |
Craig Topper | 8674c5c | 2015-09-29 04:30:07 +0000 | [diff] [blame] | 4249 | createPrivatesRecordDecl(CodeGenModule &CGM, ArrayRef<PrivateDataTy> Privates) { |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 4250 | if (!Privates.empty()) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4251 | ASTContext &C = CGM.getContext(); |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 4252 | // Build struct .kmp_privates_t. { |
| 4253 | // /* private vars */ |
| 4254 | // }; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4255 | RecordDecl *RD = C.buildImplicitRecord(".kmp_privates.t"); |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 4256 | RD->startDefinition(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4257 | for (const auto &Pair : Privates) { |
| 4258 | const VarDecl *VD = Pair.second.Original; |
| 4259 | QualType Type = VD->getType().getNonReferenceType(); |
| 4260 | FieldDecl *FD = addFieldToRecordDecl(C, RD, Type); |
Alexey Bataev | c71a409 | 2015-09-11 10:29:41 +0000 | [diff] [blame] | 4261 | if (VD->hasAttrs()) { |
| 4262 | for (specific_attr_iterator<AlignedAttr> I(VD->getAttrs().begin()), |
| 4263 | E(VD->getAttrs().end()); |
| 4264 | I != E; ++I) |
| 4265 | FD->addAttr(*I); |
| 4266 | } |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 4267 | } |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 4268 | RD->completeDefinition(); |
| 4269 | return RD; |
| 4270 | } |
| 4271 | return nullptr; |
| 4272 | } |
| 4273 | |
Alexey Bataev | 9e03404 | 2015-05-05 04:05:12 +0000 | [diff] [blame] | 4274 | static RecordDecl * |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4275 | createKmpTaskTRecordDecl(CodeGenModule &CGM, OpenMPDirectiveKind Kind, |
| 4276 | QualType KmpInt32Ty, |
Alexey Bataev | 8fc69dc | 2015-05-18 07:54:53 +0000 | [diff] [blame] | 4277 | QualType KmpRoutineEntryPointerQTy) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4278 | ASTContext &C = CGM.getContext(); |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 4279 | // Build struct kmp_task_t { |
| 4280 | // void * shareds; |
| 4281 | // kmp_routine_entry_t routine; |
| 4282 | // kmp_int32 part_id; |
Alexey Bataev | ad537bb | 2016-05-30 09:06:50 +0000 | [diff] [blame] | 4283 | // kmp_cmplrdata_t data1; |
| 4284 | // kmp_cmplrdata_t data2; |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4285 | // For taskloops additional fields: |
| 4286 | // kmp_uint64 lb; |
| 4287 | // kmp_uint64 ub; |
| 4288 | // kmp_int64 st; |
| 4289 | // kmp_int32 liter; |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 4290 | // void * reductions; |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 4291 | // }; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4292 | RecordDecl *UD = C.buildImplicitRecord("kmp_cmplrdata_t", TTK_Union); |
Alexey Bataev | ad537bb | 2016-05-30 09:06:50 +0000 | [diff] [blame] | 4293 | UD->startDefinition(); |
| 4294 | addFieldToRecordDecl(C, UD, KmpInt32Ty); |
| 4295 | addFieldToRecordDecl(C, UD, KmpRoutineEntryPointerQTy); |
| 4296 | UD->completeDefinition(); |
| 4297 | QualType KmpCmplrdataTy = C.getRecordType(UD); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4298 | RecordDecl *RD = C.buildImplicitRecord("kmp_task_t"); |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 4299 | RD->startDefinition(); |
| 4300 | addFieldToRecordDecl(C, RD, C.VoidPtrTy); |
| 4301 | addFieldToRecordDecl(C, RD, KmpRoutineEntryPointerQTy); |
| 4302 | addFieldToRecordDecl(C, RD, KmpInt32Ty); |
Alexey Bataev | ad537bb | 2016-05-30 09:06:50 +0000 | [diff] [blame] | 4303 | addFieldToRecordDecl(C, RD, KmpCmplrdataTy); |
| 4304 | addFieldToRecordDecl(C, RD, KmpCmplrdataTy); |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4305 | if (isOpenMPTaskLoopDirective(Kind)) { |
| 4306 | QualType KmpUInt64Ty = |
| 4307 | CGM.getContext().getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0); |
| 4308 | QualType KmpInt64Ty = |
| 4309 | CGM.getContext().getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1); |
| 4310 | addFieldToRecordDecl(C, RD, KmpUInt64Ty); |
| 4311 | addFieldToRecordDecl(C, RD, KmpUInt64Ty); |
| 4312 | addFieldToRecordDecl(C, RD, KmpInt64Ty); |
| 4313 | addFieldToRecordDecl(C, RD, KmpInt32Ty); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 4314 | addFieldToRecordDecl(C, RD, C.VoidPtrTy); |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4315 | } |
Alexey Bataev | 8fc69dc | 2015-05-18 07:54:53 +0000 | [diff] [blame] | 4316 | RD->completeDefinition(); |
| 4317 | return RD; |
| 4318 | } |
| 4319 | |
| 4320 | static RecordDecl * |
| 4321 | createKmpTaskTWithPrivatesRecordDecl(CodeGenModule &CGM, QualType KmpTaskTQTy, |
Craig Topper | 8674c5c | 2015-09-29 04:30:07 +0000 | [diff] [blame] | 4322 | ArrayRef<PrivateDataTy> Privates) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4323 | ASTContext &C = CGM.getContext(); |
Alexey Bataev | 8fc69dc | 2015-05-18 07:54:53 +0000 | [diff] [blame] | 4324 | // Build struct kmp_task_t_with_privates { |
| 4325 | // kmp_task_t task_data; |
| 4326 | // .kmp_privates_t. privates; |
| 4327 | // }; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4328 | RecordDecl *RD = C.buildImplicitRecord("kmp_task_t_with_privates"); |
Alexey Bataev | 8fc69dc | 2015-05-18 07:54:53 +0000 | [diff] [blame] | 4329 | RD->startDefinition(); |
| 4330 | addFieldToRecordDecl(C, RD, KmpTaskTQTy); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4331 | if (const RecordDecl *PrivateRD = createPrivatesRecordDecl(CGM, Privates)) |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 4332 | addFieldToRecordDecl(C, RD, C.getRecordType(PrivateRD)); |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 4333 | RD->completeDefinition(); |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 4334 | return RD; |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 4335 | } |
| 4336 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4337 | /// Emit a proxy function which accepts kmp_task_t as the second |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 4338 | /// argument. |
| 4339 | /// \code |
| 4340 | /// kmp_int32 .omp_task_entry.(kmp_int32 gtid, kmp_task_t *tt) { |
Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 4341 | /// TaskFunction(gtid, tt->part_id, &tt->privates, task_privates_map, tt, |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4342 | /// For taskloops: |
| 4343 | /// 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] | 4344 | /// tt->reductions, tt->shareds); |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 4345 | /// return 0; |
| 4346 | /// } |
| 4347 | /// \endcode |
| 4348 | static llvm::Value * |
| 4349 | emitProxyTaskFunction(CodeGenModule &CGM, SourceLocation Loc, |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4350 | OpenMPDirectiveKind Kind, QualType KmpInt32Ty, |
| 4351 | QualType KmpTaskTWithPrivatesPtrQTy, |
Alexey Bataev | 8fc69dc | 2015-05-18 07:54:53 +0000 | [diff] [blame] | 4352 | QualType KmpTaskTWithPrivatesQTy, QualType KmpTaskTQTy, |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4353 | QualType SharedsPtrTy, llvm::Value *TaskFunction, |
| 4354 | llvm::Value *TaskPrivatesMap) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4355 | ASTContext &C = CGM.getContext(); |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 4356 | FunctionArgList Args; |
Alexey Bataev | 5622323 | 2017-06-09 13:40:18 +0000 | [diff] [blame] | 4357 | ImplicitParamDecl GtidArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, KmpInt32Ty, |
| 4358 | ImplicitParamDecl::Other); |
| 4359 | ImplicitParamDecl TaskTypeArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, |
| 4360 | KmpTaskTWithPrivatesPtrQTy.withRestrict(), |
| 4361 | ImplicitParamDecl::Other); |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 4362 | Args.push_back(&GtidArg); |
| 4363 | Args.push_back(&TaskTypeArg); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4364 | const auto &TaskEntryFnInfo = |
John McCall | c56a8b3 | 2016-03-11 04:30:31 +0000 | [diff] [blame] | 4365 | CGM.getTypes().arrangeBuiltinFunctionDeclaration(KmpInt32Ty, Args); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4366 | llvm::FunctionType *TaskEntryTy = |
| 4367 | CGM.getTypes().GetFunctionType(TaskEntryFnInfo); |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 4368 | std::string Name = CGM.getOpenMPRuntime().getName({"omp_task_entry", ""}); |
| 4369 | auto *TaskEntry = llvm::Function::Create( |
| 4370 | TaskEntryTy, llvm::GlobalValue::InternalLinkage, Name, &CGM.getModule()); |
Rafael Espindola | 51ec5a9 | 2018-02-28 23:46:35 +0000 | [diff] [blame] | 4371 | CGM.SetInternalFunctionAttributes(GlobalDecl(), TaskEntry, TaskEntryFnInfo); |
Alexey Bataev | c0f879b | 2018-04-10 20:10:53 +0000 | [diff] [blame] | 4372 | TaskEntry->setDoesNotRecurse(); |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 4373 | CodeGenFunction CGF(CGM); |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 4374 | CGF.StartFunction(GlobalDecl(), KmpInt32Ty, TaskEntry, TaskEntryFnInfo, Args, |
| 4375 | Loc, Loc); |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 4376 | |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4377 | // TaskFunction(gtid, tt->task_data.part_id, &tt->privates, task_privates_map, |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4378 | // tt, |
| 4379 | // For taskloops: |
| 4380 | // tt->task_data.lb, tt->task_data.ub, tt->task_data.st, tt->task_data.liter, |
| 4381 | // tt->task_data.shareds); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4382 | llvm::Value *GtidParam = CGF.EmitLoadOfScalar( |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4383 | CGF.GetAddrOfLocalVar(&GtidArg), /*Volatile=*/false, KmpInt32Ty, Loc); |
Alexey Bataev | 31300ed | 2016-02-04 11:27:03 +0000 | [diff] [blame] | 4384 | LValue TDBase = CGF.EmitLoadOfPointerLValue( |
| 4385 | CGF.GetAddrOfLocalVar(&TaskTypeArg), |
| 4386 | KmpTaskTWithPrivatesPtrQTy->castAs<PointerType>()); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4387 | const auto *KmpTaskTWithPrivatesQTyRD = |
Alexey Bataev | 8fc69dc | 2015-05-18 07:54:53 +0000 | [diff] [blame] | 4388 | cast<RecordDecl>(KmpTaskTWithPrivatesQTy->getAsTagDecl()); |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4389 | LValue Base = |
| 4390 | CGF.EmitLValueForField(TDBase, *KmpTaskTWithPrivatesQTyRD->field_begin()); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4391 | const auto *KmpTaskTQTyRD = cast<RecordDecl>(KmpTaskTQTy->getAsTagDecl()); |
Alexey Bataev | 8fc69dc | 2015-05-18 07:54:53 +0000 | [diff] [blame] | 4392 | auto PartIdFI = std::next(KmpTaskTQTyRD->field_begin(), KmpTaskTPartId); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4393 | LValue PartIdLVal = CGF.EmitLValueForField(Base, *PartIdFI); |
| 4394 | llvm::Value *PartidParam = PartIdLVal.getPointer(); |
Alexey Bataev | 8fc69dc | 2015-05-18 07:54:53 +0000 | [diff] [blame] | 4395 | |
| 4396 | auto SharedsFI = std::next(KmpTaskTQTyRD->field_begin(), KmpTaskTShareds); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4397 | LValue SharedsLVal = CGF.EmitLValueForField(Base, *SharedsFI); |
| 4398 | llvm::Value *SharedsParam = CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
Alexey Bataev | 1e49137 | 2018-01-23 18:44:14 +0000 | [diff] [blame] | 4399 | CGF.EmitLoadOfScalar(SharedsLVal, Loc), |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 4400 | CGF.ConvertTypeForMem(SharedsPtrTy)); |
| 4401 | |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4402 | auto PrivatesFI = std::next(KmpTaskTWithPrivatesQTyRD->field_begin(), 1); |
| 4403 | llvm::Value *PrivatesParam; |
| 4404 | if (PrivatesFI != KmpTaskTWithPrivatesQTyRD->field_end()) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4405 | LValue PrivatesLVal = CGF.EmitLValueForField(TDBase, *PrivatesFI); |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4406 | PrivatesParam = CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4407 | PrivatesLVal.getPointer(), CGF.VoidPtrTy); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4408 | } else { |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4409 | PrivatesParam = llvm::ConstantPointerNull::get(CGF.VoidPtrTy); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4410 | } |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4411 | |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4412 | llvm::Value *CommonArgs[] = {GtidParam, PartidParam, PrivatesParam, |
| 4413 | TaskPrivatesMap, |
| 4414 | CGF.Builder |
| 4415 | .CreatePointerBitCastOrAddrSpaceCast( |
| 4416 | TDBase.getAddress(), CGF.VoidPtrTy) |
| 4417 | .getPointer()}; |
| 4418 | SmallVector<llvm::Value *, 16> CallArgs(std::begin(CommonArgs), |
| 4419 | std::end(CommonArgs)); |
| 4420 | if (isOpenMPTaskLoopDirective(Kind)) { |
| 4421 | auto LBFI = std::next(KmpTaskTQTyRD->field_begin(), KmpTaskTLowerBound); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4422 | LValue LBLVal = CGF.EmitLValueForField(Base, *LBFI); |
| 4423 | llvm::Value *LBParam = CGF.EmitLoadOfScalar(LBLVal, Loc); |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4424 | auto UBFI = std::next(KmpTaskTQTyRD->field_begin(), KmpTaskTUpperBound); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4425 | LValue UBLVal = CGF.EmitLValueForField(Base, *UBFI); |
| 4426 | llvm::Value *UBParam = CGF.EmitLoadOfScalar(UBLVal, Loc); |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4427 | auto StFI = std::next(KmpTaskTQTyRD->field_begin(), KmpTaskTStride); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4428 | LValue StLVal = CGF.EmitLValueForField(Base, *StFI); |
| 4429 | llvm::Value *StParam = CGF.EmitLoadOfScalar(StLVal, Loc); |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4430 | auto LIFI = std::next(KmpTaskTQTyRD->field_begin(), KmpTaskTLastIter); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4431 | LValue LILVal = CGF.EmitLValueForField(Base, *LIFI); |
| 4432 | llvm::Value *LIParam = CGF.EmitLoadOfScalar(LILVal, Loc); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 4433 | auto RFI = std::next(KmpTaskTQTyRD->field_begin(), KmpTaskTReductions); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4434 | LValue RLVal = CGF.EmitLValueForField(Base, *RFI); |
| 4435 | llvm::Value *RParam = CGF.EmitLoadOfScalar(RLVal, Loc); |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4436 | CallArgs.push_back(LBParam); |
| 4437 | CallArgs.push_back(UBParam); |
| 4438 | CallArgs.push_back(StParam); |
| 4439 | CallArgs.push_back(LIParam); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 4440 | CallArgs.push_back(RParam); |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4441 | } |
| 4442 | CallArgs.push_back(SharedsParam); |
| 4443 | |
Alexey Bataev | 3c595a6 | 2017-08-14 15:01:03 +0000 | [diff] [blame] | 4444 | CGM.getOpenMPRuntime().emitOutlinedFunctionCall(CGF, Loc, TaskFunction, |
| 4445 | CallArgs); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4446 | CGF.EmitStoreThroughLValue(RValue::get(CGF.Builder.getInt32(/*C=*/0)), |
| 4447 | CGF.MakeAddrLValue(CGF.ReturnValue, KmpInt32Ty)); |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 4448 | CGF.FinishFunction(); |
| 4449 | return TaskEntry; |
| 4450 | } |
| 4451 | |
Alexey Bataev | 8fc69dc | 2015-05-18 07:54:53 +0000 | [diff] [blame] | 4452 | static llvm::Value *emitDestructorsFunction(CodeGenModule &CGM, |
| 4453 | SourceLocation Loc, |
| 4454 | QualType KmpInt32Ty, |
| 4455 | QualType KmpTaskTWithPrivatesPtrQTy, |
| 4456 | QualType KmpTaskTWithPrivatesQTy) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4457 | ASTContext &C = CGM.getContext(); |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 4458 | FunctionArgList Args; |
Alexey Bataev | 5622323 | 2017-06-09 13:40:18 +0000 | [diff] [blame] | 4459 | ImplicitParamDecl GtidArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, KmpInt32Ty, |
| 4460 | ImplicitParamDecl::Other); |
| 4461 | ImplicitParamDecl TaskTypeArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, |
| 4462 | KmpTaskTWithPrivatesPtrQTy.withRestrict(), |
| 4463 | ImplicitParamDecl::Other); |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 4464 | Args.push_back(&GtidArg); |
| 4465 | Args.push_back(&TaskTypeArg); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4466 | const auto &DestructorFnInfo = |
John McCall | c56a8b3 | 2016-03-11 04:30:31 +0000 | [diff] [blame] | 4467 | CGM.getTypes().arrangeBuiltinFunctionDeclaration(KmpInt32Ty, Args); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4468 | llvm::FunctionType *DestructorFnTy = |
| 4469 | CGM.getTypes().GetFunctionType(DestructorFnInfo); |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 4470 | std::string Name = |
| 4471 | CGM.getOpenMPRuntime().getName({"omp_task_destructor", ""}); |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 4472 | auto *DestructorFn = |
| 4473 | llvm::Function::Create(DestructorFnTy, llvm::GlobalValue::InternalLinkage, |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 4474 | Name, &CGM.getModule()); |
Rafael Espindola | 51ec5a9 | 2018-02-28 23:46:35 +0000 | [diff] [blame] | 4475 | CGM.SetInternalFunctionAttributes(GlobalDecl(), DestructorFn, |
Akira Hatanaka | 44a59f8 | 2015-10-28 02:30:47 +0000 | [diff] [blame] | 4476 | DestructorFnInfo); |
Alexey Bataev | c0f879b | 2018-04-10 20:10:53 +0000 | [diff] [blame] | 4477 | DestructorFn->setDoesNotRecurse(); |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 4478 | CodeGenFunction CGF(CGM); |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 4479 | CGF.StartFunction(GlobalDecl(), KmpInt32Ty, DestructorFn, DestructorFnInfo, |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 4480 | Args, Loc, Loc); |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 4481 | |
Alexey Bataev | 31300ed | 2016-02-04 11:27:03 +0000 | [diff] [blame] | 4482 | LValue Base = CGF.EmitLoadOfPointerLValue( |
| 4483 | CGF.GetAddrOfLocalVar(&TaskTypeArg), |
| 4484 | KmpTaskTWithPrivatesPtrQTy->castAs<PointerType>()); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4485 | const auto *KmpTaskTWithPrivatesQTyRD = |
Alexey Bataev | 8fc69dc | 2015-05-18 07:54:53 +0000 | [diff] [blame] | 4486 | cast<RecordDecl>(KmpTaskTWithPrivatesQTy->getAsTagDecl()); |
| 4487 | auto FI = std::next(KmpTaskTWithPrivatesQTyRD->field_begin()); |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 4488 | Base = CGF.EmitLValueForField(Base, *FI); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4489 | for (const auto *Field : |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 4490 | cast<RecordDecl>(FI->getType()->getAsTagDecl())->fields()) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4491 | if (QualType::DestructionKind DtorKind = |
| 4492 | Field->getType().isDestructedType()) { |
| 4493 | LValue FieldLValue = CGF.EmitLValueForField(Base, Field); |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 4494 | CGF.pushDestroy(DtorKind, FieldLValue.getAddress(), Field->getType()); |
| 4495 | } |
| 4496 | } |
| 4497 | CGF.FinishFunction(); |
| 4498 | return DestructorFn; |
| 4499 | } |
| 4500 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4501 | /// Emit a privates mapping function for correct handling of private and |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4502 | /// firstprivate variables. |
| 4503 | /// \code |
| 4504 | /// void .omp_task_privates_map.(const .privates. *noalias privs, <ty1> |
| 4505 | /// **noalias priv1,..., <tyn> **noalias privn) { |
| 4506 | /// *priv1 = &.privates.priv1; |
| 4507 | /// ...; |
| 4508 | /// *privn = &.privates.privn; |
| 4509 | /// } |
| 4510 | /// \endcode |
| 4511 | static llvm::Value * |
| 4512 | emitTaskPrivateMappingFunction(CodeGenModule &CGM, SourceLocation Loc, |
Craig Topper | 8674c5c | 2015-09-29 04:30:07 +0000 | [diff] [blame] | 4513 | ArrayRef<const Expr *> PrivateVars, |
| 4514 | ArrayRef<const Expr *> FirstprivateVars, |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4515 | ArrayRef<const Expr *> LastprivateVars, |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4516 | QualType PrivatesQTy, |
Craig Topper | 8674c5c | 2015-09-29 04:30:07 +0000 | [diff] [blame] | 4517 | ArrayRef<PrivateDataTy> Privates) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4518 | ASTContext &C = CGM.getContext(); |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4519 | FunctionArgList Args; |
| 4520 | ImplicitParamDecl TaskPrivatesArg( |
| 4521 | C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, |
Alexey Bataev | 5622323 | 2017-06-09 13:40:18 +0000 | [diff] [blame] | 4522 | C.getPointerType(PrivatesQTy).withConst().withRestrict(), |
| 4523 | ImplicitParamDecl::Other); |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4524 | Args.push_back(&TaskPrivatesArg); |
| 4525 | llvm::DenseMap<const VarDecl *, unsigned> PrivateVarsPos; |
| 4526 | unsigned Counter = 1; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4527 | for (const Expr *E : PrivateVars) { |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4528 | Args.push_back(ImplicitParamDecl::Create( |
Alexey Bataev | 5622323 | 2017-06-09 13:40:18 +0000 | [diff] [blame] | 4529 | C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, |
| 4530 | C.getPointerType(C.getPointerType(E->getType())) |
| 4531 | .withConst() |
| 4532 | .withRestrict(), |
| 4533 | ImplicitParamDecl::Other)); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4534 | const auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl()); |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4535 | PrivateVarsPos[VD] = Counter; |
| 4536 | ++Counter; |
| 4537 | } |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4538 | for (const Expr *E : FirstprivateVars) { |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4539 | Args.push_back(ImplicitParamDecl::Create( |
Alexey Bataev | 5622323 | 2017-06-09 13:40:18 +0000 | [diff] [blame] | 4540 | C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, |
| 4541 | C.getPointerType(C.getPointerType(E->getType())) |
| 4542 | .withConst() |
| 4543 | .withRestrict(), |
| 4544 | ImplicitParamDecl::Other)); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4545 | const auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl()); |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4546 | PrivateVarsPos[VD] = Counter; |
| 4547 | ++Counter; |
| 4548 | } |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4549 | for (const Expr *E : LastprivateVars) { |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4550 | Args.push_back(ImplicitParamDecl::Create( |
Alexey Bataev | 5622323 | 2017-06-09 13:40:18 +0000 | [diff] [blame] | 4551 | C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, |
| 4552 | C.getPointerType(C.getPointerType(E->getType())) |
| 4553 | .withConst() |
| 4554 | .withRestrict(), |
| 4555 | ImplicitParamDecl::Other)); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4556 | const auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl()); |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4557 | PrivateVarsPos[VD] = Counter; |
| 4558 | ++Counter; |
| 4559 | } |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4560 | const auto &TaskPrivatesMapFnInfo = |
John McCall | c56a8b3 | 2016-03-11 04:30:31 +0000 | [diff] [blame] | 4561 | CGM.getTypes().arrangeBuiltinFunctionDeclaration(C.VoidTy, Args); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4562 | llvm::FunctionType *TaskPrivatesMapTy = |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4563 | CGM.getTypes().GetFunctionType(TaskPrivatesMapFnInfo); |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 4564 | std::string Name = |
| 4565 | CGM.getOpenMPRuntime().getName({"omp_task_privates_map", ""}); |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4566 | auto *TaskPrivatesMap = llvm::Function::Create( |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 4567 | TaskPrivatesMapTy, llvm::GlobalValue::InternalLinkage, Name, |
| 4568 | &CGM.getModule()); |
Rafael Espindola | 51ec5a9 | 2018-02-28 23:46:35 +0000 | [diff] [blame] | 4569 | CGM.SetInternalFunctionAttributes(GlobalDecl(), TaskPrivatesMap, |
Akira Hatanaka | 44a59f8 | 2015-10-28 02:30:47 +0000 | [diff] [blame] | 4570 | TaskPrivatesMapFnInfo); |
Chandler Carruth | fcd3314 | 2016-12-23 01:24:49 +0000 | [diff] [blame] | 4571 | TaskPrivatesMap->removeFnAttr(llvm::Attribute::NoInline); |
Mehdi Amini | 6aa9e9b | 2017-05-29 05:38:20 +0000 | [diff] [blame] | 4572 | TaskPrivatesMap->removeFnAttr(llvm::Attribute::OptimizeNone); |
Evgeniy Stepanov | 6b2a61d | 2015-09-14 21:35:16 +0000 | [diff] [blame] | 4573 | TaskPrivatesMap->addFnAttr(llvm::Attribute::AlwaysInline); |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4574 | CodeGenFunction CGF(CGM); |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4575 | CGF.StartFunction(GlobalDecl(), C.VoidTy, TaskPrivatesMap, |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 4576 | TaskPrivatesMapFnInfo, Args, Loc, Loc); |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4577 | |
| 4578 | // *privi = &.privates.privi; |
Alexey Bataev | 31300ed | 2016-02-04 11:27:03 +0000 | [diff] [blame] | 4579 | LValue Base = CGF.EmitLoadOfPointerLValue( |
| 4580 | CGF.GetAddrOfLocalVar(&TaskPrivatesArg), |
| 4581 | TaskPrivatesArg.getType()->castAs<PointerType>()); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4582 | const auto *PrivatesQTyRD = cast<RecordDecl>(PrivatesQTy->getAsTagDecl()); |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4583 | Counter = 0; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4584 | for (const FieldDecl *Field : PrivatesQTyRD->fields()) { |
| 4585 | LValue FieldLVal = CGF.EmitLValueForField(Base, Field); |
| 4586 | const VarDecl *VD = Args[PrivateVarsPos[Privates[Counter].second.Original]]; |
| 4587 | LValue RefLVal = |
| 4588 | CGF.MakeAddrLValue(CGF.GetAddrOfLocalVar(VD), VD->getType()); |
| 4589 | LValue RefLoadLVal = CGF.EmitLoadOfPointerLValue( |
Alexey Bataev | 31300ed | 2016-02-04 11:27:03 +0000 | [diff] [blame] | 4590 | RefLVal.getAddress(), RefLVal.getType()->castAs<PointerType>()); |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 4591 | CGF.EmitStoreOfScalar(FieldLVal.getPointer(), RefLoadLVal); |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4592 | ++Counter; |
| 4593 | } |
| 4594 | CGF.FinishFunction(); |
| 4595 | return TaskPrivatesMap; |
| 4596 | } |
| 4597 | |
Mandeep Singh Grang | b14fb6a2 | 2017-11-28 20:41:13 +0000 | [diff] [blame] | 4598 | static bool stable_sort_comparator(const PrivateDataTy P1, |
| 4599 | const PrivateDataTy P2) { |
| 4600 | return P1.first > P2.first; |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 4601 | } |
| 4602 | |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4603 | /// Emit initialization for private variables in task-based directives. |
Alexey Bataev | 8a83159 | 2016-05-10 10:36:51 +0000 | [diff] [blame] | 4604 | static void emitPrivatesInit(CodeGenFunction &CGF, |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4605 | const OMPExecutableDirective &D, |
| 4606 | Address KmpTaskSharedsPtr, LValue TDBase, |
| 4607 | const RecordDecl *KmpTaskTWithPrivatesQTyRD, |
| 4608 | QualType SharedsTy, QualType SharedsPtrTy, |
| 4609 | const OMPTaskDataTy &Data, |
| 4610 | ArrayRef<PrivateDataTy> Privates, bool ForDup) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4611 | ASTContext &C = CGF.getContext(); |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4612 | auto FI = std::next(KmpTaskTWithPrivatesQTyRD->field_begin()); |
| 4613 | LValue PrivatesBase = CGF.EmitLValueForField(TDBase, *FI); |
Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 4614 | OpenMPDirectiveKind Kind = isOpenMPTaskLoopDirective(D.getDirectiveKind()) |
| 4615 | ? OMPD_taskloop |
| 4616 | : OMPD_task; |
| 4617 | const CapturedStmt &CS = *D.getCapturedStmt(Kind); |
| 4618 | CodeGenFunction::CGCapturedStmtInfo CapturesInfo(CS); |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4619 | LValue SrcBase; |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 4620 | bool IsTargetTask = |
| 4621 | isOpenMPTargetDataManagementDirective(D.getDirectiveKind()) || |
| 4622 | isOpenMPTargetExecutionDirective(D.getDirectiveKind()); |
| 4623 | // For target-based directives skip 3 firstprivate arrays BasePointersArray, |
| 4624 | // PointersArray and SizesArray. The original variables for these arrays are |
| 4625 | // not captured and we get their addresses explicitly. |
| 4626 | if ((!IsTargetTask && !Data.FirstprivateVars.empty()) || |
Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 4627 | (IsTargetTask && KmpTaskSharedsPtr.isValid())) { |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4628 | SrcBase = CGF.MakeAddrLValue( |
| 4629 | CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
| 4630 | KmpTaskSharedsPtr, CGF.ConvertTypeForMem(SharedsPtrTy)), |
| 4631 | SharedsTy); |
| 4632 | } |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4633 | FI = cast<RecordDecl>(FI->getType()->getAsTagDecl())->field_begin(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4634 | for (const PrivateDataTy &Pair : Privates) { |
| 4635 | const VarDecl *VD = Pair.second.PrivateCopy; |
| 4636 | const Expr *Init = VD->getAnyInitializer(); |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4637 | if (Init && (!ForDup || (isa<CXXConstructExpr>(Init) && |
| 4638 | !CGF.isTrivialInitializer(Init)))) { |
Alexey Bataev | 8a83159 | 2016-05-10 10:36:51 +0000 | [diff] [blame] | 4639 | LValue PrivateLValue = CGF.EmitLValueForField(PrivatesBase, *FI); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4640 | if (const VarDecl *Elem = Pair.second.PrivateElemInit) { |
| 4641 | const VarDecl *OriginalVD = Pair.second.Original; |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 4642 | // Check if the variable is the target-based BasePointersArray, |
| 4643 | // PointersArray or SizesArray. |
| 4644 | LValue SharedRefLValue; |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4645 | QualType Type = OriginalVD->getType(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4646 | const FieldDecl *SharedField = CapturesInfo.lookup(OriginalVD); |
Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 4647 | if (IsTargetTask && !SharedField) { |
| 4648 | assert(isa<ImplicitParamDecl>(OriginalVD) && |
| 4649 | isa<CapturedDecl>(OriginalVD->getDeclContext()) && |
| 4650 | cast<CapturedDecl>(OriginalVD->getDeclContext()) |
| 4651 | ->getNumParams() == 0 && |
| 4652 | isa<TranslationUnitDecl>( |
| 4653 | cast<CapturedDecl>(OriginalVD->getDeclContext()) |
| 4654 | ->getDeclContext()) && |
| 4655 | "Expected artificial target data variable."); |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 4656 | SharedRefLValue = |
| 4657 | CGF.MakeAddrLValue(CGF.GetAddrOfLocalVar(OriginalVD), Type); |
| 4658 | } else { |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 4659 | SharedRefLValue = CGF.EmitLValueForField(SrcBase, SharedField); |
| 4660 | SharedRefLValue = CGF.MakeAddrLValue( |
| 4661 | Address(SharedRefLValue.getPointer(), C.getDeclAlign(OriginalVD)), |
| 4662 | SharedRefLValue.getType(), LValueBaseInfo(AlignmentSource::Decl), |
| 4663 | SharedRefLValue.getTBAAInfo()); |
| 4664 | } |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4665 | if (Type->isArrayType()) { |
| 4666 | // Initialize firstprivate array. |
| 4667 | if (!isa<CXXConstructExpr>(Init) || CGF.isTrivialInitializer(Init)) { |
| 4668 | // Perform simple memcpy. |
Ivan A. Kosarev | 1860b52 | 2018-01-25 14:21:55 +0000 | [diff] [blame] | 4669 | CGF.EmitAggregateAssign(PrivateLValue, SharedRefLValue, Type); |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4670 | } else { |
| 4671 | // Initialize firstprivate array using element-by-element |
Simon Pilgrim | 2c51880 | 2017-03-30 14:13:19 +0000 | [diff] [blame] | 4672 | // initialization. |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4673 | CGF.EmitOMPAggregateAssign( |
| 4674 | PrivateLValue.getAddress(), SharedRefLValue.getAddress(), Type, |
| 4675 | [&CGF, Elem, Init, &CapturesInfo](Address DestElement, |
| 4676 | Address SrcElement) { |
| 4677 | // Clean up any temporaries needed by the initialization. |
| 4678 | CodeGenFunction::OMPPrivateScope InitScope(CGF); |
| 4679 | InitScope.addPrivate( |
| 4680 | Elem, [SrcElement]() -> Address { return SrcElement; }); |
| 4681 | (void)InitScope.Privatize(); |
| 4682 | // Emit initialization for single element. |
| 4683 | CodeGenFunction::CGCapturedStmtRAII CapInfoRAII( |
| 4684 | CGF, &CapturesInfo); |
| 4685 | CGF.EmitAnyExprToMem(Init, DestElement, |
| 4686 | Init->getType().getQualifiers(), |
| 4687 | /*IsInitializer=*/false); |
| 4688 | }); |
| 4689 | } |
| 4690 | } else { |
| 4691 | CodeGenFunction::OMPPrivateScope InitScope(CGF); |
| 4692 | InitScope.addPrivate(Elem, [SharedRefLValue]() -> Address { |
| 4693 | return SharedRefLValue.getAddress(); |
| 4694 | }); |
| 4695 | (void)InitScope.Privatize(); |
| 4696 | CodeGenFunction::CGCapturedStmtRAII CapInfoRAII(CGF, &CapturesInfo); |
| 4697 | CGF.EmitExprAsInit(Init, VD, PrivateLValue, |
| 4698 | /*capturedByInit=*/false); |
| 4699 | } |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4700 | } else { |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4701 | CGF.EmitExprAsInit(Init, VD, PrivateLValue, /*capturedByInit=*/false); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4702 | } |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4703 | } |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4704 | ++FI; |
| 4705 | } |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4706 | } |
| 4707 | |
| 4708 | /// Check if duplication function is required for taskloops. |
| 4709 | static bool checkInitIsRequired(CodeGenFunction &CGF, |
| 4710 | ArrayRef<PrivateDataTy> Privates) { |
| 4711 | bool InitRequired = false; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4712 | for (const PrivateDataTy &Pair : Privates) { |
| 4713 | const VarDecl *VD = Pair.second.PrivateCopy; |
| 4714 | const Expr *Init = VD->getAnyInitializer(); |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4715 | InitRequired = InitRequired || (Init && isa<CXXConstructExpr>(Init) && |
| 4716 | !CGF.isTrivialInitializer(Init)); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4717 | if (InitRequired) |
| 4718 | break; |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4719 | } |
| 4720 | return InitRequired; |
| 4721 | } |
| 4722 | |
| 4723 | |
| 4724 | /// Emit task_dup function (for initialization of |
| 4725 | /// private/firstprivate/lastprivate vars and last_iter flag) |
| 4726 | /// \code |
| 4727 | /// void __task_dup_entry(kmp_task_t *task_dst, const kmp_task_t *task_src, int |
| 4728 | /// lastpriv) { |
| 4729 | /// // setup lastprivate flag |
| 4730 | /// task_dst->last = lastpriv; |
| 4731 | /// // could be constructor calls here... |
| 4732 | /// } |
| 4733 | /// \endcode |
| 4734 | static llvm::Value * |
| 4735 | emitTaskDupFunction(CodeGenModule &CGM, SourceLocation Loc, |
| 4736 | const OMPExecutableDirective &D, |
| 4737 | QualType KmpTaskTWithPrivatesPtrQTy, |
| 4738 | const RecordDecl *KmpTaskTWithPrivatesQTyRD, |
| 4739 | const RecordDecl *KmpTaskTQTyRD, QualType SharedsTy, |
| 4740 | QualType SharedsPtrTy, const OMPTaskDataTy &Data, |
| 4741 | ArrayRef<PrivateDataTy> Privates, bool WithLastIter) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4742 | ASTContext &C = CGM.getContext(); |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4743 | FunctionArgList Args; |
Alexey Bataev | 5622323 | 2017-06-09 13:40:18 +0000 | [diff] [blame] | 4744 | ImplicitParamDecl DstArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, |
| 4745 | KmpTaskTWithPrivatesPtrQTy, |
| 4746 | ImplicitParamDecl::Other); |
| 4747 | ImplicitParamDecl SrcArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, |
| 4748 | KmpTaskTWithPrivatesPtrQTy, |
| 4749 | ImplicitParamDecl::Other); |
| 4750 | ImplicitParamDecl LastprivArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, C.IntTy, |
| 4751 | ImplicitParamDecl::Other); |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4752 | Args.push_back(&DstArg); |
| 4753 | Args.push_back(&SrcArg); |
| 4754 | Args.push_back(&LastprivArg); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4755 | const auto &TaskDupFnInfo = |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4756 | CGM.getTypes().arrangeBuiltinFunctionDeclaration(C.VoidTy, Args); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4757 | llvm::FunctionType *TaskDupTy = CGM.getTypes().GetFunctionType(TaskDupFnInfo); |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 4758 | std::string Name = CGM.getOpenMPRuntime().getName({"omp_task_dup", ""}); |
| 4759 | auto *TaskDup = llvm::Function::Create( |
| 4760 | TaskDupTy, llvm::GlobalValue::InternalLinkage, Name, &CGM.getModule()); |
Rafael Espindola | 51ec5a9 | 2018-02-28 23:46:35 +0000 | [diff] [blame] | 4761 | CGM.SetInternalFunctionAttributes(GlobalDecl(), TaskDup, TaskDupFnInfo); |
Alexey Bataev | c0f879b | 2018-04-10 20:10:53 +0000 | [diff] [blame] | 4762 | TaskDup->setDoesNotRecurse(); |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4763 | CodeGenFunction CGF(CGM); |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 4764 | CGF.StartFunction(GlobalDecl(), C.VoidTy, TaskDup, TaskDupFnInfo, Args, Loc, |
| 4765 | Loc); |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4766 | |
| 4767 | LValue TDBase = CGF.EmitLoadOfPointerLValue( |
| 4768 | CGF.GetAddrOfLocalVar(&DstArg), |
| 4769 | KmpTaskTWithPrivatesPtrQTy->castAs<PointerType>()); |
| 4770 | // task_dst->liter = lastpriv; |
| 4771 | if (WithLastIter) { |
| 4772 | auto LIFI = std::next(KmpTaskTQTyRD->field_begin(), KmpTaskTLastIter); |
| 4773 | LValue Base = CGF.EmitLValueForField( |
| 4774 | TDBase, *KmpTaskTWithPrivatesQTyRD->field_begin()); |
| 4775 | LValue LILVal = CGF.EmitLValueForField(Base, *LIFI); |
| 4776 | llvm::Value *Lastpriv = CGF.EmitLoadOfScalar( |
| 4777 | CGF.GetAddrOfLocalVar(&LastprivArg), /*Volatile=*/false, C.IntTy, Loc); |
| 4778 | CGF.EmitStoreOfScalar(Lastpriv, LILVal); |
| 4779 | } |
| 4780 | |
| 4781 | // Emit initial values for private copies (if any). |
| 4782 | assert(!Privates.empty()); |
| 4783 | Address KmpTaskSharedsPtr = Address::invalid(); |
| 4784 | if (!Data.FirstprivateVars.empty()) { |
| 4785 | LValue TDBase = CGF.EmitLoadOfPointerLValue( |
| 4786 | CGF.GetAddrOfLocalVar(&SrcArg), |
| 4787 | KmpTaskTWithPrivatesPtrQTy->castAs<PointerType>()); |
| 4788 | LValue Base = CGF.EmitLValueForField( |
| 4789 | TDBase, *KmpTaskTWithPrivatesQTyRD->field_begin()); |
| 4790 | KmpTaskSharedsPtr = Address( |
| 4791 | CGF.EmitLoadOfScalar(CGF.EmitLValueForField( |
| 4792 | Base, *std::next(KmpTaskTQTyRD->field_begin(), |
| 4793 | KmpTaskTShareds)), |
| 4794 | Loc), |
| 4795 | CGF.getNaturalTypeAlignment(SharedsTy)); |
| 4796 | } |
Alexey Bataev | 8a83159 | 2016-05-10 10:36:51 +0000 | [diff] [blame] | 4797 | emitPrivatesInit(CGF, D, KmpTaskSharedsPtr, TDBase, KmpTaskTWithPrivatesQTyRD, |
| 4798 | SharedsTy, SharedsPtrTy, Data, Privates, /*ForDup=*/true); |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4799 | CGF.FinishFunction(); |
| 4800 | return TaskDup; |
| 4801 | } |
| 4802 | |
Alexey Bataev | 8a83159 | 2016-05-10 10:36:51 +0000 | [diff] [blame] | 4803 | /// Checks if destructor function is required to be generated. |
| 4804 | /// \return true if cleanups are required, false otherwise. |
| 4805 | static bool |
| 4806 | checkDestructorsRequired(const RecordDecl *KmpTaskTWithPrivatesQTyRD) { |
| 4807 | bool NeedsCleanup = false; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4808 | auto FI = std::next(KmpTaskTWithPrivatesQTyRD->field_begin(), 1); |
| 4809 | const auto *PrivateRD = cast<RecordDecl>(FI->getType()->getAsTagDecl()); |
| 4810 | for (const FieldDecl *FD : PrivateRD->fields()) { |
Alexey Bataev | 8a83159 | 2016-05-10 10:36:51 +0000 | [diff] [blame] | 4811 | NeedsCleanup = NeedsCleanup || FD->getType().isDestructedType(); |
| 4812 | if (NeedsCleanup) |
| 4813 | break; |
| 4814 | } |
| 4815 | return NeedsCleanup; |
| 4816 | } |
| 4817 | |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 4818 | CGOpenMPRuntime::TaskResultTy |
| 4819 | CGOpenMPRuntime::emitTaskInit(CodeGenFunction &CGF, SourceLocation Loc, |
| 4820 | const OMPExecutableDirective &D, |
| 4821 | llvm::Value *TaskFunction, QualType SharedsTy, |
| 4822 | Address Shareds, const OMPTaskDataTy &Data) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4823 | ASTContext &C = CGM.getContext(); |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4824 | llvm::SmallVector<PrivateDataTy, 4> Privates; |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4825 | // Aggregate privates and sort them by the alignment. |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 4826 | auto I = Data.PrivateCopies.begin(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4827 | for (const Expr *E : Data.PrivateVars) { |
| 4828 | const auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl()); |
Alexey Bataev | 43a919f | 2018-04-13 17:48:43 +0000 | [diff] [blame] | 4829 | Privates.emplace_back( |
Alexey Bataev | c71a409 | 2015-09-11 10:29:41 +0000 | [diff] [blame] | 4830 | C.getDeclAlign(VD), |
Alexey Bataev | 9e03404 | 2015-05-05 04:05:12 +0000 | [diff] [blame] | 4831 | PrivateHelpersTy(VD, cast<VarDecl>(cast<DeclRefExpr>(*I)->getDecl()), |
Alexey Bataev | 43a919f | 2018-04-13 17:48:43 +0000 | [diff] [blame] | 4832 | /*PrivateElemInit=*/nullptr)); |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 4833 | ++I; |
| 4834 | } |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 4835 | I = Data.FirstprivateCopies.begin(); |
| 4836 | auto IElemInitRef = Data.FirstprivateInits.begin(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4837 | for (const Expr *E : Data.FirstprivateVars) { |
| 4838 | const auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl()); |
Alexey Bataev | 43a919f | 2018-04-13 17:48:43 +0000 | [diff] [blame] | 4839 | Privates.emplace_back( |
Alexey Bataev | c71a409 | 2015-09-11 10:29:41 +0000 | [diff] [blame] | 4840 | C.getDeclAlign(VD), |
Alexey Bataev | 9e03404 | 2015-05-05 04:05:12 +0000 | [diff] [blame] | 4841 | PrivateHelpersTy( |
| 4842 | VD, cast<VarDecl>(cast<DeclRefExpr>(*I)->getDecl()), |
Alexey Bataev | 43a919f | 2018-04-13 17:48:43 +0000 | [diff] [blame] | 4843 | cast<VarDecl>(cast<DeclRefExpr>(*IElemInitRef)->getDecl()))); |
Richard Trieu | cc3949d | 2016-02-18 22:34:54 +0000 | [diff] [blame] | 4844 | ++I; |
| 4845 | ++IElemInitRef; |
Alexey Bataev | 9e03404 | 2015-05-05 04:05:12 +0000 | [diff] [blame] | 4846 | } |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4847 | I = Data.LastprivateCopies.begin(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4848 | for (const Expr *E : Data.LastprivateVars) { |
| 4849 | const auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl()); |
Alexey Bataev | 43a919f | 2018-04-13 17:48:43 +0000 | [diff] [blame] | 4850 | Privates.emplace_back( |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4851 | C.getDeclAlign(VD), |
| 4852 | PrivateHelpersTy(VD, cast<VarDecl>(cast<DeclRefExpr>(*I)->getDecl()), |
Alexey Bataev | 43a919f | 2018-04-13 17:48:43 +0000 | [diff] [blame] | 4853 | /*PrivateElemInit=*/nullptr)); |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4854 | ++I; |
| 4855 | } |
Mandeep Singh Grang | b14fb6a2 | 2017-11-28 20:41:13 +0000 | [diff] [blame] | 4856 | std::stable_sort(Privates.begin(), Privates.end(), stable_sort_comparator); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4857 | QualType KmpInt32Ty = C.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1); |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 4858 | // Build type kmp_routine_entry_t (if not built yet). |
| 4859 | emitKmpRoutineEntryT(KmpInt32Ty); |
Alexey Bataev | 8fc69dc | 2015-05-18 07:54:53 +0000 | [diff] [blame] | 4860 | // Build type kmp_task_t (if not built yet). |
Alexey Bataev | e213f3e | 2017-10-11 15:29:40 +0000 | [diff] [blame] | 4861 | if (isOpenMPTaskLoopDirective(D.getDirectiveKind())) { |
| 4862 | if (SavedKmpTaskloopTQTy.isNull()) { |
| 4863 | SavedKmpTaskloopTQTy = C.getRecordType(createKmpTaskTRecordDecl( |
| 4864 | CGM, D.getDirectiveKind(), KmpInt32Ty, KmpRoutineEntryPtrQTy)); |
| 4865 | } |
| 4866 | KmpTaskTQTy = SavedKmpTaskloopTQTy; |
Alexey Bataev | 3a03a7f | 2017-10-11 15:56:38 +0000 | [diff] [blame] | 4867 | } else { |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 4868 | assert((D.getDirectiveKind() == OMPD_task || |
| 4869 | isOpenMPTargetExecutionDirective(D.getDirectiveKind()) || |
| 4870 | isOpenMPTargetDataManagementDirective(D.getDirectiveKind())) && |
| 4871 | "Expected taskloop, task or target directive"); |
Alexey Bataev | e213f3e | 2017-10-11 15:29:40 +0000 | [diff] [blame] | 4872 | if (SavedKmpTaskTQTy.isNull()) { |
| 4873 | SavedKmpTaskTQTy = C.getRecordType(createKmpTaskTRecordDecl( |
| 4874 | CGM, D.getDirectiveKind(), KmpInt32Ty, KmpRoutineEntryPtrQTy)); |
| 4875 | } |
| 4876 | KmpTaskTQTy = SavedKmpTaskTQTy; |
Alexey Bataev | 8fc69dc | 2015-05-18 07:54:53 +0000 | [diff] [blame] | 4877 | } |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4878 | const auto *KmpTaskTQTyRD = cast<RecordDecl>(KmpTaskTQTy->getAsTagDecl()); |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 4879 | // Build particular struct kmp_task_t for the given task. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4880 | const RecordDecl *KmpTaskTWithPrivatesQTyRD = |
Alexey Bataev | 8fc69dc | 2015-05-18 07:54:53 +0000 | [diff] [blame] | 4881 | createKmpTaskTWithPrivatesRecordDecl(CGM, KmpTaskTQTy, Privates); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4882 | QualType KmpTaskTWithPrivatesQTy = C.getRecordType(KmpTaskTWithPrivatesQTyRD); |
Alexey Bataev | 8fc69dc | 2015-05-18 07:54:53 +0000 | [diff] [blame] | 4883 | QualType KmpTaskTWithPrivatesPtrQTy = |
| 4884 | C.getPointerType(KmpTaskTWithPrivatesQTy); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4885 | llvm::Type *KmpTaskTWithPrivatesTy = CGF.ConvertType(KmpTaskTWithPrivatesQTy); |
| 4886 | llvm::Type *KmpTaskTWithPrivatesPtrTy = |
| 4887 | KmpTaskTWithPrivatesTy->getPointerTo(); |
| 4888 | llvm::Value *KmpTaskTWithPrivatesTySize = |
| 4889 | CGF.getTypeSize(KmpTaskTWithPrivatesQTy); |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 4890 | QualType SharedsPtrTy = C.getPointerType(SharedsTy); |
| 4891 | |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4892 | // Emit initial values for private copies (if any). |
| 4893 | llvm::Value *TaskPrivatesMap = nullptr; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4894 | llvm::Type *TaskPrivatesMapTy = |
Reid Kleckner | e258c44 | 2017-03-16 18:55:46 +0000 | [diff] [blame] | 4895 | std::next(cast<llvm::Function>(TaskFunction)->arg_begin(), 3)->getType(); |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4896 | if (!Privates.empty()) { |
| 4897 | auto FI = std::next(KmpTaskTWithPrivatesQTyRD->field_begin()); |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4898 | TaskPrivatesMap = emitTaskPrivateMappingFunction( |
| 4899 | CGM, Loc, Data.PrivateVars, Data.FirstprivateVars, Data.LastprivateVars, |
| 4900 | FI->getType(), Privates); |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4901 | TaskPrivatesMap = CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
| 4902 | TaskPrivatesMap, TaskPrivatesMapTy); |
| 4903 | } else { |
| 4904 | TaskPrivatesMap = llvm::ConstantPointerNull::get( |
| 4905 | cast<llvm::PointerType>(TaskPrivatesMapTy)); |
| 4906 | } |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 4907 | // Build a proxy function kmp_int32 .omp_task_entry.(kmp_int32 gtid, |
| 4908 | // kmp_task_t *tt); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4909 | llvm::Value *TaskEntry = emitProxyTaskFunction( |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4910 | CGM, Loc, D.getDirectiveKind(), KmpInt32Ty, KmpTaskTWithPrivatesPtrQTy, |
| 4911 | KmpTaskTWithPrivatesQTy, KmpTaskTQTy, SharedsPtrTy, TaskFunction, |
| 4912 | TaskPrivatesMap); |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 4913 | |
| 4914 | // Build call kmp_task_t * __kmpc_omp_task_alloc(ident_t *, kmp_int32 gtid, |
| 4915 | // kmp_int32 flags, size_t sizeof_kmp_task_t, size_t sizeof_shareds, |
| 4916 | // kmp_routine_entry_t *task_entry); |
| 4917 | // Task flags. Format is taken from |
| 4918 | // http://llvm.org/svn/llvm-project/openmp/trunk/runtime/src/kmp.h, |
| 4919 | // description of kmp_tasking_flags struct. |
Alexey Bataev | 1e1e286 | 2016-05-10 12:21:02 +0000 | [diff] [blame] | 4920 | enum { |
| 4921 | TiedFlag = 0x1, |
| 4922 | FinalFlag = 0x2, |
| 4923 | DestructorsFlag = 0x8, |
| 4924 | PriorityFlag = 0x20 |
| 4925 | }; |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 4926 | unsigned Flags = Data.Tied ? TiedFlag : 0; |
Alexey Bataev | 8a83159 | 2016-05-10 10:36:51 +0000 | [diff] [blame] | 4927 | bool NeedsCleanup = false; |
| 4928 | if (!Privates.empty()) { |
| 4929 | NeedsCleanup = checkDestructorsRequired(KmpTaskTWithPrivatesQTyRD); |
| 4930 | if (NeedsCleanup) |
| 4931 | Flags = Flags | DestructorsFlag; |
| 4932 | } |
Alexey Bataev | 1e1e286 | 2016-05-10 12:21:02 +0000 | [diff] [blame] | 4933 | if (Data.Priority.getInt()) |
| 4934 | Flags = Flags | PriorityFlag; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4935 | llvm::Value *TaskFlags = |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 4936 | Data.Final.getPointer() |
| 4937 | ? CGF.Builder.CreateSelect(Data.Final.getPointer(), |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 4938 | CGF.Builder.getInt32(FinalFlag), |
| 4939 | CGF.Builder.getInt32(/*C=*/0)) |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 4940 | : CGF.Builder.getInt32(Data.Final.getInt() ? FinalFlag : 0); |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 4941 | TaskFlags = CGF.Builder.CreateOr(TaskFlags, CGF.Builder.getInt32(Flags)); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4942 | llvm::Value *SharedsSize = CGM.getSize(C.getTypeSizeInChars(SharedsTy)); |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 4943 | llvm::Value *AllocArgs[] = {emitUpdateLocation(CGF, Loc), |
| 4944 | getThreadID(CGF, Loc), TaskFlags, |
| 4945 | KmpTaskTWithPrivatesTySize, SharedsSize, |
| 4946 | CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
| 4947 | TaskEntry, KmpRoutineEntryPtrTy)}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4948 | llvm::Value *NewTask = CGF.EmitRuntimeCall( |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 4949 | createRuntimeFunction(OMPRTL__kmpc_omp_task_alloc), AllocArgs); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4950 | llvm::Value *NewTaskNewTaskTTy = |
| 4951 | CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
| 4952 | NewTask, KmpTaskTWithPrivatesPtrTy); |
Alexey Bataev | 8fc69dc | 2015-05-18 07:54:53 +0000 | [diff] [blame] | 4953 | LValue Base = CGF.MakeNaturalAlignAddrLValue(NewTaskNewTaskTTy, |
| 4954 | KmpTaskTWithPrivatesQTy); |
| 4955 | LValue TDBase = |
| 4956 | CGF.EmitLValueForField(Base, *KmpTaskTWithPrivatesQTyRD->field_begin()); |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 4957 | // Fill the data in the resulting kmp_task_t record. |
| 4958 | // Copy shareds if there are any. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4959 | Address KmpTaskSharedsPtr = Address::invalid(); |
Alexey Bataev | 8fc69dc | 2015-05-18 07:54:53 +0000 | [diff] [blame] | 4960 | if (!SharedsTy->getAsStructureType()->getDecl()->field_empty()) { |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 4961 | KmpTaskSharedsPtr = |
| 4962 | Address(CGF.EmitLoadOfScalar( |
| 4963 | CGF.EmitLValueForField( |
| 4964 | TDBase, *std::next(KmpTaskTQTyRD->field_begin(), |
| 4965 | KmpTaskTShareds)), |
| 4966 | Loc), |
| 4967 | CGF.getNaturalTypeAlignment(SharedsTy)); |
Ivan A. Kosarev | 1860b52 | 2018-01-25 14:21:55 +0000 | [diff] [blame] | 4968 | LValue Dest = CGF.MakeAddrLValue(KmpTaskSharedsPtr, SharedsTy); |
| 4969 | LValue Src = CGF.MakeAddrLValue(Shareds, SharedsTy); |
Richard Smith | e78fac5 | 2018-04-05 20:52:58 +0000 | [diff] [blame] | 4970 | CGF.EmitAggregateCopy(Dest, Src, SharedsTy, AggValueSlot::DoesNotOverlap); |
Alexey Bataev | 8fc69dc | 2015-05-18 07:54:53 +0000 | [diff] [blame] | 4971 | } |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 4972 | // Emit initial values for private copies (if any). |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4973 | TaskResultTy Result; |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 4974 | if (!Privates.empty()) { |
Alexey Bataev | 8a83159 | 2016-05-10 10:36:51 +0000 | [diff] [blame] | 4975 | emitPrivatesInit(CGF, D, KmpTaskSharedsPtr, Base, KmpTaskTWithPrivatesQTyRD, |
| 4976 | SharedsTy, SharedsPtrTy, Data, Privates, |
| 4977 | /*ForDup=*/false); |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4978 | if (isOpenMPTaskLoopDirective(D.getDirectiveKind()) && |
| 4979 | (!Data.LastprivateVars.empty() || checkInitIsRequired(CGF, Privates))) { |
| 4980 | Result.TaskDupFn = emitTaskDupFunction( |
| 4981 | CGM, Loc, D, KmpTaskTWithPrivatesPtrQTy, KmpTaskTWithPrivatesQTyRD, |
| 4982 | KmpTaskTQTyRD, SharedsTy, SharedsPtrTy, Data, Privates, |
| 4983 | /*WithLastIter=*/!Data.LastprivateVars.empty()); |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 4984 | } |
| 4985 | } |
Alexey Bataev | ad537bb | 2016-05-30 09:06:50 +0000 | [diff] [blame] | 4986 | // Fields of union "kmp_cmplrdata_t" for destructors and priority. |
| 4987 | enum { Priority = 0, Destructors = 1 }; |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 4988 | // Provide pointer to function with destructors for privates. |
Alexey Bataev | ad537bb | 2016-05-30 09:06:50 +0000 | [diff] [blame] | 4989 | auto FI = std::next(KmpTaskTQTyRD->field_begin(), Data1); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4990 | const RecordDecl *KmpCmplrdataUD = |
| 4991 | (*FI)->getType()->getAsUnionType()->getDecl(); |
Alexey Bataev | ad537bb | 2016-05-30 09:06:50 +0000 | [diff] [blame] | 4992 | if (NeedsCleanup) { |
| 4993 | llvm::Value *DestructorFn = emitDestructorsFunction( |
| 4994 | CGM, Loc, KmpInt32Ty, KmpTaskTWithPrivatesPtrQTy, |
| 4995 | KmpTaskTWithPrivatesQTy); |
| 4996 | LValue Data1LV = CGF.EmitLValueForField(TDBase, *FI); |
| 4997 | LValue DestructorsLV = CGF.EmitLValueForField( |
| 4998 | Data1LV, *std::next(KmpCmplrdataUD->field_begin(), Destructors)); |
| 4999 | CGF.EmitStoreOfScalar(CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
| 5000 | DestructorFn, KmpRoutineEntryPtrTy), |
| 5001 | DestructorsLV); |
| 5002 | } |
| 5003 | // Set priority. |
| 5004 | if (Data.Priority.getInt()) { |
| 5005 | LValue Data2LV = CGF.EmitLValueForField( |
| 5006 | TDBase, *std::next(KmpTaskTQTyRD->field_begin(), Data2)); |
| 5007 | LValue PriorityLV = CGF.EmitLValueForField( |
| 5008 | Data2LV, *std::next(KmpCmplrdataUD->field_begin(), Priority)); |
| 5009 | CGF.EmitStoreOfScalar(Data.Priority.getPointer(), PriorityLV); |
| 5010 | } |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 5011 | Result.NewTask = NewTask; |
| 5012 | Result.TaskEntry = TaskEntry; |
| 5013 | Result.NewTaskNewTaskTTy = NewTaskNewTaskTTy; |
| 5014 | Result.TDBase = TDBase; |
| 5015 | Result.KmpTaskTQTyRD = KmpTaskTQTyRD; |
| 5016 | return Result; |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 5017 | } |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 5018 | |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 5019 | void CGOpenMPRuntime::emitTaskCall(CodeGenFunction &CGF, SourceLocation Loc, |
| 5020 | const OMPExecutableDirective &D, |
| 5021 | llvm::Value *TaskFunction, |
| 5022 | QualType SharedsTy, Address Shareds, |
| 5023 | const Expr *IfCond, |
| 5024 | const OMPTaskDataTy &Data) { |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 5025 | if (!CGF.HaveInsertPoint()) |
| 5026 | return; |
| 5027 | |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 5028 | TaskResultTy Result = |
| 5029 | emitTaskInit(CGF, Loc, D, TaskFunction, SharedsTy, Shareds, Data); |
| 5030 | llvm::Value *NewTask = Result.NewTask; |
| 5031 | llvm::Value *TaskEntry = Result.TaskEntry; |
| 5032 | llvm::Value *NewTaskNewTaskTTy = Result.NewTaskNewTaskTTy; |
| 5033 | LValue TDBase = Result.TDBase; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5034 | const RecordDecl *KmpTaskTQTyRD = Result.KmpTaskTQTyRD; |
| 5035 | ASTContext &C = CGM.getContext(); |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 5036 | // Process list of dependences. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5037 | Address DependenciesArray = Address::invalid(); |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 5038 | unsigned NumDependencies = Data.Dependences.size(); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5039 | if (NumDependencies) { |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 5040 | // Dependence kind for RTL. |
Alexey Bataev | 92e82f9 | 2015-11-23 13:33:42 +0000 | [diff] [blame] | 5041 | enum RTLDependenceKindTy { DepIn = 0x01, DepInOut = 0x3 }; |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 5042 | enum RTLDependInfoFieldsTy { BaseAddr, Len, Flags }; |
| 5043 | RecordDecl *KmpDependInfoRD; |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5044 | QualType FlagsTy = |
| 5045 | C.getIntTypeForBitwidth(C.getTypeSize(C.BoolTy), /*Signed=*/false); |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 5046 | llvm::Type *LLVMFlagsTy = CGF.ConvertTypeForMem(FlagsTy); |
| 5047 | if (KmpDependInfoTy.isNull()) { |
| 5048 | KmpDependInfoRD = C.buildImplicitRecord("kmp_depend_info"); |
| 5049 | KmpDependInfoRD->startDefinition(); |
| 5050 | addFieldToRecordDecl(C, KmpDependInfoRD, C.getIntPtrType()); |
| 5051 | addFieldToRecordDecl(C, KmpDependInfoRD, C.getSizeType()); |
| 5052 | addFieldToRecordDecl(C, KmpDependInfoRD, FlagsTy); |
| 5053 | KmpDependInfoRD->completeDefinition(); |
| 5054 | KmpDependInfoTy = C.getRecordType(KmpDependInfoRD); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5055 | } else { |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 5056 | KmpDependInfoRD = cast<RecordDecl>(KmpDependInfoTy->getAsTagDecl()); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5057 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5058 | CharUnits DependencySize = C.getTypeSizeInChars(KmpDependInfoTy); |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 5059 | // Define type kmp_depend_info[<Dependences.size()>]; |
| 5060 | QualType KmpDependInfoArrayTy = C.getConstantArrayType( |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5061 | KmpDependInfoTy, llvm::APInt(/*numBits=*/64, NumDependencies), |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 5062 | ArrayType::Normal, /*IndexTypeQuals=*/0); |
| 5063 | // kmp_depend_info[<Dependences.size()>] deps; |
Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 5064 | DependenciesArray = |
| 5065 | CGF.CreateMemTemp(KmpDependInfoArrayTy, ".dep.arr.addr"); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5066 | for (unsigned I = 0; I < NumDependencies; ++I) { |
| 5067 | const Expr *E = Data.Dependences[I].second; |
| 5068 | LValue Addr = CGF.EmitLValue(E); |
Alexey Bataev | d6fdc8b | 2015-08-31 07:32:19 +0000 | [diff] [blame] | 5069 | llvm::Value *Size; |
| 5070 | QualType Ty = E->getType(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5071 | if (const auto *ASE = |
| 5072 | dyn_cast<OMPArraySectionExpr>(E->IgnoreParenImpCasts())) { |
Alexey Bataev | d6fdc8b | 2015-08-31 07:32:19 +0000 | [diff] [blame] | 5073 | LValue UpAddrLVal = |
| 5074 | CGF.EmitOMPArraySectionExpr(ASE, /*LowerBound=*/false); |
| 5075 | llvm::Value *UpAddr = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5076 | CGF.Builder.CreateConstGEP1_32(UpAddrLVal.getPointer(), /*Idx0=*/1); |
Alexey Bataev | d6fdc8b | 2015-08-31 07:32:19 +0000 | [diff] [blame] | 5077 | llvm::Value *LowIntPtr = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5078 | CGF.Builder.CreatePtrToInt(Addr.getPointer(), CGM.SizeTy); |
Alexey Bataev | d6fdc8b | 2015-08-31 07:32:19 +0000 | [diff] [blame] | 5079 | llvm::Value *UpIntPtr = CGF.Builder.CreatePtrToInt(UpAddr, CGM.SizeTy); |
| 5080 | Size = CGF.Builder.CreateNUWSub(UpIntPtr, LowIntPtr); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5081 | } else { |
Alexey Bataev | 1189bd0 | 2016-01-26 12:20:39 +0000 | [diff] [blame] | 5082 | Size = CGF.getTypeSize(Ty); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5083 | } |
| 5084 | LValue Base = CGF.MakeAddrLValue( |
| 5085 | CGF.Builder.CreateConstArrayGEP(DependenciesArray, I, DependencySize), |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 5086 | KmpDependInfoTy); |
| 5087 | // deps[i].base_addr = &<Dependences[i].second>; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5088 | LValue BaseAddrLVal = CGF.EmitLValueForField( |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 5089 | Base, *std::next(KmpDependInfoRD->field_begin(), BaseAddr)); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5090 | CGF.EmitStoreOfScalar( |
| 5091 | CGF.Builder.CreatePtrToInt(Addr.getPointer(), CGF.IntPtrTy), |
| 5092 | BaseAddrLVal); |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 5093 | // deps[i].len = sizeof(<Dependences[i].second>); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5094 | LValue LenLVal = CGF.EmitLValueForField( |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 5095 | Base, *std::next(KmpDependInfoRD->field_begin(), Len)); |
| 5096 | CGF.EmitStoreOfScalar(Size, LenLVal); |
| 5097 | // deps[i].flags = <Dependences[i].first>; |
| 5098 | RTLDependenceKindTy DepKind; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5099 | switch (Data.Dependences[I].first) { |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 5100 | case OMPC_DEPEND_in: |
| 5101 | DepKind = DepIn; |
| 5102 | break; |
Alexey Bataev | 92e82f9 | 2015-11-23 13:33:42 +0000 | [diff] [blame] | 5103 | // Out and InOut dependencies must use the same code. |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 5104 | case OMPC_DEPEND_out: |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 5105 | case OMPC_DEPEND_inout: |
| 5106 | DepKind = DepInOut; |
| 5107 | break; |
Alexey Bataev | eb48235 | 2015-12-18 05:05:56 +0000 | [diff] [blame] | 5108 | case OMPC_DEPEND_source: |
Alexey Bataev | a636c7f | 2015-12-23 10:27:45 +0000 | [diff] [blame] | 5109 | case OMPC_DEPEND_sink: |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 5110 | case OMPC_DEPEND_unknown: |
| 5111 | llvm_unreachable("Unknown task dependence type"); |
| 5112 | } |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5113 | LValue FlagsLVal = CGF.EmitLValueForField( |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 5114 | Base, *std::next(KmpDependInfoRD->field_begin(), Flags)); |
| 5115 | CGF.EmitStoreOfScalar(llvm::ConstantInt::get(LLVMFlagsTy, DepKind), |
| 5116 | FlagsLVal); |
| 5117 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5118 | DependenciesArray = CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
| 5119 | CGF.Builder.CreateStructGEP(DependenciesArray, 0, CharUnits::Zero()), |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 5120 | CGF.VoidPtrTy); |
| 5121 | } |
| 5122 | |
Alexander Kornienko | 2a8c18d | 2018-04-06 15:14:32 +0000 | [diff] [blame] | 5123 | // 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] | 5124 | // libcall. |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 5125 | // Build kmp_int32 __kmpc_omp_task_with_deps(ident_t *, kmp_int32 gtid, |
| 5126 | // kmp_task_t *new_task, kmp_int32 ndeps, kmp_depend_info_t *dep_list, |
| 5127 | // kmp_int32 ndeps_noalias, kmp_depend_info_t *noalias_dep_list) if dependence |
| 5128 | // list is not empty |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5129 | llvm::Value *ThreadID = getThreadID(CGF, Loc); |
| 5130 | llvm::Value *UpLoc = emitUpdateLocation(CGF, Loc); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5131 | llvm::Value *TaskArgs[] = { UpLoc, ThreadID, NewTask }; |
| 5132 | llvm::Value *DepTaskArgs[7]; |
| 5133 | if (NumDependencies) { |
| 5134 | DepTaskArgs[0] = UpLoc; |
| 5135 | DepTaskArgs[1] = ThreadID; |
| 5136 | DepTaskArgs[2] = NewTask; |
| 5137 | DepTaskArgs[3] = CGF.Builder.getInt32(NumDependencies); |
| 5138 | DepTaskArgs[4] = DependenciesArray.getPointer(); |
| 5139 | DepTaskArgs[5] = CGF.Builder.getInt32(0); |
| 5140 | DepTaskArgs[6] = llvm::ConstantPointerNull::get(CGF.VoidPtrTy); |
| 5141 | } |
Malcolm Parsons | c6e4583 | 2017-01-13 18:55:32 +0000 | [diff] [blame] | 5142 | auto &&ThenCodeGen = [this, &Data, TDBase, KmpTaskTQTyRD, NumDependencies, |
| 5143 | &TaskArgs, |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5144 | &DepTaskArgs](CodeGenFunction &CGF, PrePostActionTy &) { |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 5145 | if (!Data.Tied) { |
Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 5146 | auto PartIdFI = std::next(KmpTaskTQTyRD->field_begin(), KmpTaskTPartId); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5147 | LValue PartIdLVal = CGF.EmitLValueForField(TDBase, *PartIdFI); |
Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 5148 | CGF.EmitStoreOfScalar(CGF.Builder.getInt32(0), PartIdLVal); |
| 5149 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5150 | if (NumDependencies) { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5151 | CGF.EmitRuntimeCall( |
Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 5152 | createRuntimeFunction(OMPRTL__kmpc_omp_task_with_deps), DepTaskArgs); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5153 | } else { |
Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 5154 | CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_omp_task), |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5155 | TaskArgs); |
| 5156 | } |
Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 5157 | // Check if parent region is untied and build return for untied task; |
| 5158 | if (auto *Region = |
| 5159 | dyn_cast_or_null<CGOpenMPRegionInfo>(CGF.CapturedStmtInfo)) |
| 5160 | Region->emitUntiedSwitch(CGF); |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 5161 | }; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5162 | |
| 5163 | llvm::Value *DepWaitTaskArgs[6]; |
| 5164 | if (NumDependencies) { |
| 5165 | DepWaitTaskArgs[0] = UpLoc; |
| 5166 | DepWaitTaskArgs[1] = ThreadID; |
| 5167 | DepWaitTaskArgs[2] = CGF.Builder.getInt32(NumDependencies); |
| 5168 | DepWaitTaskArgs[3] = DependenciesArray.getPointer(); |
| 5169 | DepWaitTaskArgs[4] = CGF.Builder.getInt32(0); |
| 5170 | DepWaitTaskArgs[5] = llvm::ConstantPointerNull::get(CGF.VoidPtrTy); |
| 5171 | } |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5172 | auto &&ElseCodeGen = [&TaskArgs, ThreadID, NewTaskNewTaskTTy, TaskEntry, |
Alexey Bataev | 3c595a6 | 2017-08-14 15:01:03 +0000 | [diff] [blame] | 5173 | NumDependencies, &DepWaitTaskArgs, |
| 5174 | Loc](CodeGenFunction &CGF, PrePostActionTy &) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5175 | CGOpenMPRuntime &RT = CGF.CGM.getOpenMPRuntime(); |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 5176 | CodeGenFunction::RunCleanupsScope LocalScope(CGF); |
| 5177 | // Build void __kmpc_omp_wait_deps(ident_t *, kmp_int32 gtid, |
| 5178 | // kmp_int32 ndeps, kmp_depend_info_t *dep_list, kmp_int32 |
| 5179 | // ndeps_noalias, kmp_depend_info_t *noalias_dep_list); if dependence info |
| 5180 | // is specified. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5181 | if (NumDependencies) |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5182 | CGF.EmitRuntimeCall(RT.createRuntimeFunction(OMPRTL__kmpc_omp_wait_deps), |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 5183 | DepWaitTaskArgs); |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5184 | // Call proxy_task_entry(gtid, new_task); |
Alexey Bataev | 3c595a6 | 2017-08-14 15:01:03 +0000 | [diff] [blame] | 5185 | auto &&CodeGen = [TaskEntry, ThreadID, NewTaskNewTaskTTy, |
| 5186 | Loc](CodeGenFunction &CGF, PrePostActionTy &Action) { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5187 | Action.Enter(CGF); |
| 5188 | llvm::Value *OutlinedFnArgs[] = {ThreadID, NewTaskNewTaskTTy}; |
Alexey Bataev | 3c595a6 | 2017-08-14 15:01:03 +0000 | [diff] [blame] | 5189 | CGF.CGM.getOpenMPRuntime().emitOutlinedFunctionCall(CGF, Loc, TaskEntry, |
Alexey Bataev | 2c7eee5 | 2017-08-04 19:10:54 +0000 | [diff] [blame] | 5190 | OutlinedFnArgs); |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5191 | }; |
| 5192 | |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 5193 | // Build void __kmpc_omp_task_begin_if0(ident_t *, kmp_int32 gtid, |
| 5194 | // kmp_task_t *new_task); |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 5195 | // Build void __kmpc_omp_task_complete_if0(ident_t *, kmp_int32 gtid, |
| 5196 | // kmp_task_t *new_task); |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5197 | RegionCodeGenTy RCG(CodeGen); |
| 5198 | CommonActionTy Action( |
| 5199 | RT.createRuntimeFunction(OMPRTL__kmpc_omp_task_begin_if0), TaskArgs, |
| 5200 | RT.createRuntimeFunction(OMPRTL__kmpc_omp_task_complete_if0), TaskArgs); |
| 5201 | RCG.setAction(Action); |
| 5202 | RCG(CGF); |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 5203 | }; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5204 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5205 | if (IfCond) { |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 5206 | emitOMPIfClause(CGF, IfCond, ThenCodeGen, ElseCodeGen); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5207 | } else { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5208 | RegionCodeGenTy ThenRCG(ThenCodeGen); |
| 5209 | ThenRCG(CGF); |
Alexey Bataev | f539faa | 2016-03-28 12:58:34 +0000 | [diff] [blame] | 5210 | } |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 5211 | } |
| 5212 | |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 5213 | void CGOpenMPRuntime::emitTaskLoopCall(CodeGenFunction &CGF, SourceLocation Loc, |
| 5214 | const OMPLoopDirective &D, |
| 5215 | llvm::Value *TaskFunction, |
| 5216 | QualType SharedsTy, Address Shareds, |
| 5217 | const Expr *IfCond, |
| 5218 | const OMPTaskDataTy &Data) { |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 5219 | if (!CGF.HaveInsertPoint()) |
| 5220 | return; |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 5221 | TaskResultTy Result = |
| 5222 | emitTaskInit(CGF, Loc, D, TaskFunction, SharedsTy, Shareds, Data); |
Alexander Kornienko | 2a8c18d | 2018-04-06 15:14:32 +0000 | [diff] [blame] | 5223 | // 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] | 5224 | // libcall. |
| 5225 | // Call to void __kmpc_taskloop(ident_t *loc, int gtid, kmp_task_t *task, int |
| 5226 | // if_val, kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st, int nogroup, int |
| 5227 | // sched, kmp_uint64 grainsize, void *task_dup); |
| 5228 | llvm::Value *ThreadID = getThreadID(CGF, Loc); |
| 5229 | llvm::Value *UpLoc = emitUpdateLocation(CGF, Loc); |
| 5230 | llvm::Value *IfVal; |
| 5231 | if (IfCond) { |
| 5232 | IfVal = CGF.Builder.CreateIntCast(CGF.EvaluateExprAsBool(IfCond), CGF.IntTy, |
| 5233 | /*isSigned=*/true); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5234 | } else { |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 5235 | IfVal = llvm::ConstantInt::getSigned(CGF.IntTy, /*V=*/1); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5236 | } |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 5237 | |
| 5238 | LValue LBLVal = CGF.EmitLValueForField( |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 5239 | Result.TDBase, |
| 5240 | *std::next(Result.KmpTaskTQTyRD->field_begin(), KmpTaskTLowerBound)); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5241 | const auto *LBVar = |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 5242 | cast<VarDecl>(cast<DeclRefExpr>(D.getLowerBoundVariable())->getDecl()); |
| 5243 | CGF.EmitAnyExprToMem(LBVar->getInit(), LBLVal.getAddress(), LBLVal.getQuals(), |
| 5244 | /*IsInitializer=*/true); |
| 5245 | LValue UBLVal = CGF.EmitLValueForField( |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 5246 | Result.TDBase, |
| 5247 | *std::next(Result.KmpTaskTQTyRD->field_begin(), KmpTaskTUpperBound)); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5248 | const auto *UBVar = |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 5249 | cast<VarDecl>(cast<DeclRefExpr>(D.getUpperBoundVariable())->getDecl()); |
| 5250 | CGF.EmitAnyExprToMem(UBVar->getInit(), UBLVal.getAddress(), UBLVal.getQuals(), |
| 5251 | /*IsInitializer=*/true); |
| 5252 | LValue StLVal = CGF.EmitLValueForField( |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 5253 | Result.TDBase, |
| 5254 | *std::next(Result.KmpTaskTQTyRD->field_begin(), KmpTaskTStride)); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5255 | const auto *StVar = |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 5256 | cast<VarDecl>(cast<DeclRefExpr>(D.getStrideVariable())->getDecl()); |
| 5257 | CGF.EmitAnyExprToMem(StVar->getInit(), StLVal.getAddress(), StLVal.getQuals(), |
| 5258 | /*IsInitializer=*/true); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 5259 | // Store reductions address. |
| 5260 | LValue RedLVal = CGF.EmitLValueForField( |
| 5261 | Result.TDBase, |
| 5262 | *std::next(Result.KmpTaskTQTyRD->field_begin(), KmpTaskTReductions)); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5263 | if (Data.Reductions) { |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 5264 | CGF.EmitStoreOfScalar(Data.Reductions, RedLVal); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5265 | } else { |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 5266 | CGF.EmitNullInitialization(RedLVal.getAddress(), |
| 5267 | CGF.getContext().VoidPtrTy); |
| 5268 | } |
Alexey Bataev | 2b19a6f | 2016-04-28 09:15:06 +0000 | [diff] [blame] | 5269 | enum { NoSchedule = 0, Grainsize = 1, NumTasks = 2 }; |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 5270 | llvm::Value *TaskArgs[] = { |
Alexey Bataev | 3344603 | 2017-07-12 18:09:32 +0000 | [diff] [blame] | 5271 | UpLoc, |
| 5272 | ThreadID, |
| 5273 | Result.NewTask, |
| 5274 | IfVal, |
| 5275 | LBLVal.getPointer(), |
| 5276 | UBLVal.getPointer(), |
Alexey Bataev | a9b9cc0 | 2018-01-23 18:12:38 +0000 | [diff] [blame] | 5277 | CGF.EmitLoadOfScalar(StLVal, Loc), |
Alexey Bataev | ac6e4de | 2018-10-24 19:06:37 +0000 | [diff] [blame] | 5278 | llvm::ConstantInt::getSigned( |
| 5279 | CGF.IntTy, 1), // Always 1 because taskgroup emitted by the compiler |
Alexey Bataev | 2b19a6f | 2016-04-28 09:15:06 +0000 | [diff] [blame] | 5280 | llvm::ConstantInt::getSigned( |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 5281 | CGF.IntTy, Data.Schedule.getPointer() |
| 5282 | ? Data.Schedule.getInt() ? NumTasks : Grainsize |
Alexey Bataev | 2b19a6f | 2016-04-28 09:15:06 +0000 | [diff] [blame] | 5283 | : NoSchedule), |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 5284 | Data.Schedule.getPointer() |
| 5285 | ? CGF.Builder.CreateIntCast(Data.Schedule.getPointer(), CGF.Int64Ty, |
Alexey Bataev | 2b19a6f | 2016-04-28 09:15:06 +0000 | [diff] [blame] | 5286 | /*isSigned=*/false) |
| 5287 | : llvm::ConstantInt::get(CGF.Int64Ty, /*V=*/0), |
Alexey Bataev | 3344603 | 2017-07-12 18:09:32 +0000 | [diff] [blame] | 5288 | Result.TaskDupFn ? CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
| 5289 | Result.TaskDupFn, CGF.VoidPtrTy) |
| 5290 | : llvm::ConstantPointerNull::get(CGF.VoidPtrTy)}; |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 5291 | CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_taskloop), TaskArgs); |
| 5292 | } |
| 5293 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 5294 | /// Emit reduction operation for each element of array (required for |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5295 | /// array sections) LHS op = RHS. |
| 5296 | /// \param Type Type of array. |
| 5297 | /// \param LHSVar Variable on the left side of the reduction operation |
| 5298 | /// (references element of array in original variable). |
| 5299 | /// \param RHSVar Variable on the right side of the reduction operation |
| 5300 | /// (references element of array in original variable). |
| 5301 | /// \param RedOpGen Generator of reduction operation with use of LHSVar and |
| 5302 | /// RHSVar. |
Benjamin Kramer | e003ca2 | 2015-10-28 13:54:16 +0000 | [diff] [blame] | 5303 | static void EmitOMPAggregateReduction( |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5304 | CodeGenFunction &CGF, QualType Type, const VarDecl *LHSVar, |
| 5305 | const VarDecl *RHSVar, |
| 5306 | const llvm::function_ref<void(CodeGenFunction &CGF, const Expr *, |
| 5307 | const Expr *, const Expr *)> &RedOpGen, |
| 5308 | const Expr *XExpr = nullptr, const Expr *EExpr = nullptr, |
| 5309 | const Expr *UpExpr = nullptr) { |
| 5310 | // Perform element-by-element initialization. |
| 5311 | QualType ElementTy; |
| 5312 | Address LHSAddr = CGF.GetAddrOfLocalVar(LHSVar); |
| 5313 | Address RHSAddr = CGF.GetAddrOfLocalVar(RHSVar); |
| 5314 | |
| 5315 | // Drill down to the base element type on both arrays. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5316 | const ArrayType *ArrayTy = Type->getAsArrayTypeUnsafe(); |
| 5317 | llvm::Value *NumElements = CGF.emitArrayLength(ArrayTy, ElementTy, LHSAddr); |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5318 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5319 | llvm::Value *RHSBegin = RHSAddr.getPointer(); |
| 5320 | llvm::Value *LHSBegin = LHSAddr.getPointer(); |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5321 | // Cast from pointer to array type to pointer to single element. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5322 | llvm::Value *LHSEnd = CGF.Builder.CreateGEP(LHSBegin, NumElements); |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5323 | // The basic structure here is a while-do loop. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5324 | llvm::BasicBlock *BodyBB = CGF.createBasicBlock("omp.arraycpy.body"); |
| 5325 | llvm::BasicBlock *DoneBB = CGF.createBasicBlock("omp.arraycpy.done"); |
| 5326 | llvm::Value *IsEmpty = |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5327 | CGF.Builder.CreateICmpEQ(LHSBegin, LHSEnd, "omp.arraycpy.isempty"); |
| 5328 | CGF.Builder.CreateCondBr(IsEmpty, DoneBB, BodyBB); |
| 5329 | |
| 5330 | // Enter the loop body, making that address the current address. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5331 | llvm::BasicBlock *EntryBB = CGF.Builder.GetInsertBlock(); |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5332 | CGF.EmitBlock(BodyBB); |
| 5333 | |
| 5334 | CharUnits ElementSize = CGF.getContext().getTypeSizeInChars(ElementTy); |
| 5335 | |
| 5336 | llvm::PHINode *RHSElementPHI = CGF.Builder.CreatePHI( |
| 5337 | RHSBegin->getType(), 2, "omp.arraycpy.srcElementPast"); |
| 5338 | RHSElementPHI->addIncoming(RHSBegin, EntryBB); |
| 5339 | Address RHSElementCurrent = |
| 5340 | Address(RHSElementPHI, |
| 5341 | RHSAddr.getAlignment().alignmentOfArrayElement(ElementSize)); |
| 5342 | |
| 5343 | llvm::PHINode *LHSElementPHI = CGF.Builder.CreatePHI( |
| 5344 | LHSBegin->getType(), 2, "omp.arraycpy.destElementPast"); |
| 5345 | LHSElementPHI->addIncoming(LHSBegin, EntryBB); |
| 5346 | Address LHSElementCurrent = |
| 5347 | Address(LHSElementPHI, |
| 5348 | LHSAddr.getAlignment().alignmentOfArrayElement(ElementSize)); |
| 5349 | |
| 5350 | // Emit copy. |
| 5351 | CodeGenFunction::OMPPrivateScope Scope(CGF); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5352 | Scope.addPrivate(LHSVar, [=]() { return LHSElementCurrent; }); |
| 5353 | Scope.addPrivate(RHSVar, [=]() { return RHSElementCurrent; }); |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5354 | Scope.Privatize(); |
| 5355 | RedOpGen(CGF, XExpr, EExpr, UpExpr); |
| 5356 | Scope.ForceCleanup(); |
| 5357 | |
| 5358 | // Shift the address forward by one element. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5359 | llvm::Value *LHSElementNext = CGF.Builder.CreateConstGEP1_32( |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5360 | LHSElementPHI, /*Idx0=*/1, "omp.arraycpy.dest.element"); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5361 | llvm::Value *RHSElementNext = CGF.Builder.CreateConstGEP1_32( |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5362 | RHSElementPHI, /*Idx0=*/1, "omp.arraycpy.src.element"); |
| 5363 | // Check whether we've reached the end. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5364 | llvm::Value *Done = |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5365 | CGF.Builder.CreateICmpEQ(LHSElementNext, LHSEnd, "omp.arraycpy.done"); |
| 5366 | CGF.Builder.CreateCondBr(Done, DoneBB, BodyBB); |
| 5367 | LHSElementPHI->addIncoming(LHSElementNext, CGF.Builder.GetInsertBlock()); |
| 5368 | RHSElementPHI->addIncoming(RHSElementNext, CGF.Builder.GetInsertBlock()); |
| 5369 | |
| 5370 | // Done. |
| 5371 | CGF.EmitBlock(DoneBB, /*IsFinished=*/true); |
| 5372 | } |
| 5373 | |
Alexey Bataev | a839ddd | 2016-03-17 10:19:46 +0000 | [diff] [blame] | 5374 | /// Emit reduction combiner. If the combiner is a simple expression emit it as |
| 5375 | /// is, otherwise consider it as combiner of UDR decl and emit it as a call of |
| 5376 | /// UDR combiner function. |
| 5377 | static void emitReductionCombiner(CodeGenFunction &CGF, |
| 5378 | const Expr *ReductionOp) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5379 | if (const auto *CE = dyn_cast<CallExpr>(ReductionOp)) |
| 5380 | if (const auto *OVE = dyn_cast<OpaqueValueExpr>(CE->getCallee())) |
| 5381 | if (const auto *DRE = |
Alexey Bataev | a839ddd | 2016-03-17 10:19:46 +0000 | [diff] [blame] | 5382 | dyn_cast<DeclRefExpr>(OVE->getSourceExpr()->IgnoreImpCasts())) |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5383 | if (const auto *DRD = |
| 5384 | dyn_cast<OMPDeclareReductionDecl>(DRE->getDecl())) { |
Alexey Bataev | a839ddd | 2016-03-17 10:19:46 +0000 | [diff] [blame] | 5385 | std::pair<llvm::Function *, llvm::Function *> Reduction = |
| 5386 | CGF.CGM.getOpenMPRuntime().getUserDefinedReduction(DRD); |
| 5387 | RValue Func = RValue::get(Reduction.first); |
| 5388 | CodeGenFunction::OpaqueValueMapping Map(CGF, OVE, Func); |
| 5389 | CGF.EmitIgnoredExpr(ReductionOp); |
| 5390 | return; |
| 5391 | } |
| 5392 | CGF.EmitIgnoredExpr(ReductionOp); |
| 5393 | } |
| 5394 | |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 5395 | llvm::Value *CGOpenMPRuntime::emitReductionFunction( |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 5396 | CodeGenModule &CGM, SourceLocation Loc, llvm::Type *ArgsType, |
| 5397 | ArrayRef<const Expr *> Privates, ArrayRef<const Expr *> LHSExprs, |
| 5398 | ArrayRef<const Expr *> RHSExprs, ArrayRef<const Expr *> ReductionOps) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5399 | ASTContext &C = CGM.getContext(); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5400 | |
| 5401 | // void reduction_func(void *LHSArg, void *RHSArg); |
| 5402 | FunctionArgList Args; |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 5403 | ImplicitParamDecl LHSArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, C.VoidPtrTy, |
| 5404 | ImplicitParamDecl::Other); |
| 5405 | ImplicitParamDecl RHSArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, C.VoidPtrTy, |
| 5406 | ImplicitParamDecl::Other); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5407 | Args.push_back(&LHSArg); |
| 5408 | Args.push_back(&RHSArg); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5409 | const auto &CGFI = |
| 5410 | CGM.getTypes().arrangeBuiltinFunctionDeclaration(C.VoidTy, Args); |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 5411 | std::string Name = getName({"omp", "reduction", "reduction_func"}); |
| 5412 | auto *Fn = llvm::Function::Create(CGM.getTypes().GetFunctionType(CGFI), |
| 5413 | llvm::GlobalValue::InternalLinkage, Name, |
| 5414 | &CGM.getModule()); |
Rafael Espindola | 51ec5a9 | 2018-02-28 23:46:35 +0000 | [diff] [blame] | 5415 | CGM.SetInternalFunctionAttributes(GlobalDecl(), Fn, CGFI); |
Alexey Bataev | c0f879b | 2018-04-10 20:10:53 +0000 | [diff] [blame] | 5416 | Fn->setDoesNotRecurse(); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5417 | CodeGenFunction CGF(CGM); |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 5418 | CGF.StartFunction(GlobalDecl(), C.VoidTy, Fn, CGFI, Args, Loc, Loc); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5419 | |
| 5420 | // Dst = (void*[n])(LHSArg); |
| 5421 | // Src = (void*[n])(RHSArg); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5422 | Address LHS(CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
| 5423 | CGF.Builder.CreateLoad(CGF.GetAddrOfLocalVar(&LHSArg)), |
| 5424 | ArgsType), CGF.getPointerAlign()); |
| 5425 | Address RHS(CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
| 5426 | CGF.Builder.CreateLoad(CGF.GetAddrOfLocalVar(&RHSArg)), |
| 5427 | ArgsType), CGF.getPointerAlign()); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5428 | |
| 5429 | // ... |
| 5430 | // *(Type<i>*)lhs[i] = RedOp<i>(*(Type<i>*)lhs[i], *(Type<i>*)rhs[i]); |
| 5431 | // ... |
| 5432 | CodeGenFunction::OMPPrivateScope Scope(CGF); |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5433 | auto IPriv = Privates.begin(); |
| 5434 | unsigned Idx = 0; |
| 5435 | for (unsigned I = 0, E = ReductionOps.size(); I < E; ++I, ++IPriv, ++Idx) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5436 | const auto *RHSVar = |
| 5437 | cast<VarDecl>(cast<DeclRefExpr>(RHSExprs[I])->getDecl()); |
| 5438 | Scope.addPrivate(RHSVar, [&CGF, RHS, Idx, RHSVar]() { |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5439 | return emitAddrOfVarFromArray(CGF, RHS, Idx, RHSVar); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5440 | }); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5441 | const auto *LHSVar = |
| 5442 | cast<VarDecl>(cast<DeclRefExpr>(LHSExprs[I])->getDecl()); |
| 5443 | Scope.addPrivate(LHSVar, [&CGF, LHS, Idx, LHSVar]() { |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5444 | return emitAddrOfVarFromArray(CGF, LHS, Idx, LHSVar); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5445 | }); |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5446 | QualType PrivTy = (*IPriv)->getType(); |
Alexey Bataev | 1189bd0 | 2016-01-26 12:20:39 +0000 | [diff] [blame] | 5447 | if (PrivTy->isVariablyModifiedType()) { |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5448 | // Get array size and emit VLA type. |
| 5449 | ++Idx; |
| 5450 | Address Elem = |
| 5451 | CGF.Builder.CreateConstArrayGEP(LHS, Idx, CGF.getPointerSize()); |
| 5452 | llvm::Value *Ptr = CGF.Builder.CreateLoad(Elem); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5453 | const VariableArrayType *VLA = |
| 5454 | CGF.getContext().getAsVariableArrayType(PrivTy); |
| 5455 | const auto *OVE = cast<OpaqueValueExpr>(VLA->getSizeExpr()); |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5456 | CodeGenFunction::OpaqueValueMapping OpaqueMap( |
Alexey Bataev | 1189bd0 | 2016-01-26 12:20:39 +0000 | [diff] [blame] | 5457 | CGF, OVE, RValue::get(CGF.Builder.CreatePtrToInt(Ptr, CGF.SizeTy))); |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5458 | CGF.EmitVariablyModifiedType(PrivTy); |
| 5459 | } |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5460 | } |
| 5461 | Scope.Privatize(); |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5462 | IPriv = Privates.begin(); |
| 5463 | auto ILHS = LHSExprs.begin(); |
| 5464 | auto IRHS = RHSExprs.begin(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5465 | for (const Expr *E : ReductionOps) { |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5466 | if ((*IPriv)->getType()->isArrayType()) { |
| 5467 | // Emit reduction for array section. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5468 | const auto *LHSVar = cast<VarDecl>(cast<DeclRefExpr>(*ILHS)->getDecl()); |
| 5469 | const auto *RHSVar = cast<VarDecl>(cast<DeclRefExpr>(*IRHS)->getDecl()); |
Alexey Bataev | a839ddd | 2016-03-17 10:19:46 +0000 | [diff] [blame] | 5470 | EmitOMPAggregateReduction( |
| 5471 | CGF, (*IPriv)->getType(), LHSVar, RHSVar, |
| 5472 | [=](CodeGenFunction &CGF, const Expr *, const Expr *, const Expr *) { |
| 5473 | emitReductionCombiner(CGF, E); |
| 5474 | }); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5475 | } else { |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5476 | // Emit reduction for array subscript or single variable. |
Alexey Bataev | a839ddd | 2016-03-17 10:19:46 +0000 | [diff] [blame] | 5477 | emitReductionCombiner(CGF, E); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5478 | } |
Richard Trieu | cc3949d | 2016-02-18 22:34:54 +0000 | [diff] [blame] | 5479 | ++IPriv; |
| 5480 | ++ILHS; |
| 5481 | ++IRHS; |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5482 | } |
| 5483 | Scope.ForceCleanup(); |
| 5484 | CGF.FinishFunction(); |
| 5485 | return Fn; |
| 5486 | } |
| 5487 | |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 5488 | void CGOpenMPRuntime::emitSingleReductionCombiner(CodeGenFunction &CGF, |
| 5489 | const Expr *ReductionOp, |
| 5490 | const Expr *PrivateRef, |
| 5491 | const DeclRefExpr *LHS, |
| 5492 | const DeclRefExpr *RHS) { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5493 | if (PrivateRef->getType()->isArrayType()) { |
| 5494 | // Emit reduction for array section. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5495 | const auto *LHSVar = cast<VarDecl>(LHS->getDecl()); |
| 5496 | const auto *RHSVar = cast<VarDecl>(RHS->getDecl()); |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5497 | EmitOMPAggregateReduction( |
| 5498 | CGF, PrivateRef->getType(), LHSVar, RHSVar, |
| 5499 | [=](CodeGenFunction &CGF, const Expr *, const Expr *, const Expr *) { |
| 5500 | emitReductionCombiner(CGF, ReductionOp); |
| 5501 | }); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5502 | } else { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5503 | // Emit reduction for array subscript or single variable. |
| 5504 | emitReductionCombiner(CGF, ReductionOp); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5505 | } |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5506 | } |
| 5507 | |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5508 | void CGOpenMPRuntime::emitReduction(CodeGenFunction &CGF, SourceLocation Loc, |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5509 | ArrayRef<const Expr *> Privates, |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5510 | ArrayRef<const Expr *> LHSExprs, |
| 5511 | ArrayRef<const Expr *> RHSExprs, |
| 5512 | ArrayRef<const Expr *> ReductionOps, |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 5513 | ReductionOptionsTy Options) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 5514 | if (!CGF.HaveInsertPoint()) |
| 5515 | return; |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 5516 | |
| 5517 | bool WithNowait = Options.WithNowait; |
| 5518 | bool SimpleReduction = Options.SimpleReduction; |
| 5519 | |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5520 | // Next code should be emitted for reduction: |
| 5521 | // |
| 5522 | // static kmp_critical_name lock = { 0 }; |
| 5523 | // |
| 5524 | // void reduce_func(void *lhs[<n>], void *rhs[<n>]) { |
| 5525 | // *(Type0*)lhs[0] = ReductionOperation0(*(Type0*)lhs[0], *(Type0*)rhs[0]); |
| 5526 | // ... |
| 5527 | // *(Type<n>-1*)lhs[<n>-1] = ReductionOperation<n>-1(*(Type<n>-1*)lhs[<n>-1], |
| 5528 | // *(Type<n>-1*)rhs[<n>-1]); |
| 5529 | // } |
| 5530 | // |
| 5531 | // ... |
| 5532 | // void *RedList[<n>] = {&<RHSExprs>[0], ..., &<RHSExprs>[<n>-1]}; |
| 5533 | // switch (__kmpc_reduce{_nowait}(<loc>, <gtid>, <n>, sizeof(RedList), |
| 5534 | // RedList, reduce_func, &<lock>)) { |
| 5535 | // case 1: |
| 5536 | // ... |
| 5537 | // <LHSExprs>[i] = RedOp<i>(*<LHSExprs>[i], *<RHSExprs>[i]); |
| 5538 | // ... |
| 5539 | // __kmpc_end_reduce{_nowait}(<loc>, <gtid>, &<lock>); |
| 5540 | // break; |
| 5541 | // case 2: |
| 5542 | // ... |
| 5543 | // Atomic(<LHSExprs>[i] = RedOp<i>(*<LHSExprs>[i], *<RHSExprs>[i])); |
| 5544 | // ... |
Alexey Bataev | 69a4779 | 2015-05-07 03:54:03 +0000 | [diff] [blame] | 5545 | // [__kmpc_end_reduce(<loc>, <gtid>, &<lock>);] |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5546 | // break; |
| 5547 | // default:; |
| 5548 | // } |
Alexey Bataev | 89e7e8e | 2015-06-17 06:21:39 +0000 | [diff] [blame] | 5549 | // |
| 5550 | // if SimpleReduction is true, only the next code is generated: |
| 5551 | // ... |
| 5552 | // <LHSExprs>[i] = RedOp<i>(*<LHSExprs>[i], *<RHSExprs>[i]); |
| 5553 | // ... |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5554 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5555 | ASTContext &C = CGM.getContext(); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5556 | |
Alexey Bataev | 89e7e8e | 2015-06-17 06:21:39 +0000 | [diff] [blame] | 5557 | if (SimpleReduction) { |
| 5558 | CodeGenFunction::RunCleanupsScope Scope(CGF); |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5559 | auto IPriv = Privates.begin(); |
| 5560 | auto ILHS = LHSExprs.begin(); |
| 5561 | auto IRHS = RHSExprs.begin(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5562 | for (const Expr *E : ReductionOps) { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5563 | emitSingleReductionCombiner(CGF, E, *IPriv, cast<DeclRefExpr>(*ILHS), |
| 5564 | cast<DeclRefExpr>(*IRHS)); |
Richard Trieu | cc3949d | 2016-02-18 22:34:54 +0000 | [diff] [blame] | 5565 | ++IPriv; |
| 5566 | ++ILHS; |
| 5567 | ++IRHS; |
Alexey Bataev | 89e7e8e | 2015-06-17 06:21:39 +0000 | [diff] [blame] | 5568 | } |
| 5569 | return; |
| 5570 | } |
| 5571 | |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5572 | // 1. Build a list of reduction variables. |
| 5573 | // void *RedList[<n>] = {<ReductionVars>[0], ..., <ReductionVars>[<n>-1]}; |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5574 | auto Size = RHSExprs.size(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5575 | for (const Expr *E : Privates) { |
Alexey Bataev | 1189bd0 | 2016-01-26 12:20:39 +0000 | [diff] [blame] | 5576 | if (E->getType()->isVariablyModifiedType()) |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5577 | // Reserve place for array size. |
| 5578 | ++Size; |
| 5579 | } |
| 5580 | llvm::APInt ArraySize(/*unsigned int numBits=*/32, Size); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5581 | QualType ReductionArrayTy = |
| 5582 | C.getConstantArrayType(C.VoidPtrTy, ArraySize, ArrayType::Normal, |
| 5583 | /*IndexTypeQuals=*/0); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5584 | Address ReductionList = |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5585 | CGF.CreateMemTemp(ReductionArrayTy, ".omp.reduction.red_list"); |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5586 | auto IPriv = Privates.begin(); |
| 5587 | unsigned Idx = 0; |
| 5588 | for (unsigned I = 0, E = RHSExprs.size(); I < E; ++I, ++IPriv, ++Idx) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5589 | Address Elem = |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5590 | CGF.Builder.CreateConstArrayGEP(ReductionList, Idx, CGF.getPointerSize()); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5591 | CGF.Builder.CreateStore( |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5592 | CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5593 | CGF.EmitLValue(RHSExprs[I]).getPointer(), CGF.VoidPtrTy), |
| 5594 | Elem); |
Alexey Bataev | 1189bd0 | 2016-01-26 12:20:39 +0000 | [diff] [blame] | 5595 | if ((*IPriv)->getType()->isVariablyModifiedType()) { |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5596 | // Store array size. |
| 5597 | ++Idx; |
| 5598 | Elem = CGF.Builder.CreateConstArrayGEP(ReductionList, Idx, |
| 5599 | CGF.getPointerSize()); |
Alexey Bataev | 1189bd0 | 2016-01-26 12:20:39 +0000 | [diff] [blame] | 5600 | llvm::Value *Size = CGF.Builder.CreateIntCast( |
| 5601 | CGF.getVLASize( |
| 5602 | CGF.getContext().getAsVariableArrayType((*IPriv)->getType())) |
Sander de Smalen | 891af03a | 2018-02-03 13:55:59 +0000 | [diff] [blame] | 5603 | .NumElts, |
Alexey Bataev | 1189bd0 | 2016-01-26 12:20:39 +0000 | [diff] [blame] | 5604 | CGF.SizeTy, /*isSigned=*/false); |
| 5605 | CGF.Builder.CreateStore(CGF.Builder.CreateIntToPtr(Size, CGF.VoidPtrTy), |
| 5606 | Elem); |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5607 | } |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5608 | } |
| 5609 | |
| 5610 | // 2. Emit reduce_func(). |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5611 | llvm::Value *ReductionFn = emitReductionFunction( |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 5612 | CGM, Loc, CGF.ConvertTypeForMem(ReductionArrayTy)->getPointerTo(), |
| 5613 | Privates, LHSExprs, RHSExprs, ReductionOps); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5614 | |
| 5615 | // 3. Create static kmp_critical_name lock = { 0 }; |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 5616 | std::string Name = getName({"reduction"}); |
| 5617 | llvm::Value *Lock = getCriticalRegionLock(Name); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5618 | |
| 5619 | // 4. Build res = __kmpc_reduce{_nowait}(<loc>, <gtid>, <n>, sizeof(RedList), |
| 5620 | // RedList, reduce_func, &<lock>); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5621 | llvm::Value *IdentTLoc = emitUpdateLocation(CGF, Loc, OMP_ATOMIC_REDUCE); |
| 5622 | llvm::Value *ThreadId = getThreadID(CGF, Loc); |
| 5623 | llvm::Value *ReductionArrayTySize = CGF.getTypeSize(ReductionArrayTy); |
| 5624 | llvm::Value *RL = CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
Samuel Antao | 4c8035b | 2016-12-12 18:00:20 +0000 | [diff] [blame] | 5625 | ReductionList.getPointer(), CGF.VoidPtrTy); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5626 | llvm::Value *Args[] = { |
| 5627 | IdentTLoc, // ident_t *<loc> |
| 5628 | ThreadId, // i32 <gtid> |
| 5629 | CGF.Builder.getInt32(RHSExprs.size()), // i32 <n> |
| 5630 | ReductionArrayTySize, // size_type sizeof(RedList) |
| 5631 | RL, // void *RedList |
| 5632 | ReductionFn, // void (*) (void *, void *) <reduce_func> |
| 5633 | Lock // kmp_critical_name *&<lock> |
| 5634 | }; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5635 | llvm::Value *Res = CGF.EmitRuntimeCall( |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5636 | createRuntimeFunction(WithNowait ? OMPRTL__kmpc_reduce_nowait |
| 5637 | : OMPRTL__kmpc_reduce), |
| 5638 | Args); |
| 5639 | |
| 5640 | // 5. Build switch(res) |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5641 | llvm::BasicBlock *DefaultBB = CGF.createBasicBlock(".omp.reduction.default"); |
| 5642 | llvm::SwitchInst *SwInst = |
| 5643 | CGF.Builder.CreateSwitch(Res, DefaultBB, /*NumCases=*/2); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5644 | |
| 5645 | // 6. Build case 1: |
| 5646 | // ... |
| 5647 | // <LHSExprs>[i] = RedOp<i>(*<LHSExprs>[i], *<RHSExprs>[i]); |
| 5648 | // ... |
| 5649 | // __kmpc_end_reduce{_nowait}(<loc>, <gtid>, &<lock>); |
| 5650 | // break; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5651 | llvm::BasicBlock *Case1BB = CGF.createBasicBlock(".omp.reduction.case1"); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5652 | SwInst->addCase(CGF.Builder.getInt32(1), Case1BB); |
| 5653 | CGF.EmitBlock(Case1BB); |
| 5654 | |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5655 | // Add emission of __kmpc_end_reduce{_nowait}(<loc>, <gtid>, &<lock>); |
| 5656 | llvm::Value *EndArgs[] = { |
| 5657 | IdentTLoc, // ident_t *<loc> |
| 5658 | ThreadId, // i32 <gtid> |
| 5659 | Lock // kmp_critical_name *&<lock> |
| 5660 | }; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5661 | auto &&CodeGen = [Privates, LHSExprs, RHSExprs, ReductionOps]( |
| 5662 | CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 5663 | CGOpenMPRuntime &RT = CGF.CGM.getOpenMPRuntime(); |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5664 | auto IPriv = Privates.begin(); |
| 5665 | auto ILHS = LHSExprs.begin(); |
| 5666 | auto IRHS = RHSExprs.begin(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5667 | for (const Expr *E : ReductionOps) { |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 5668 | RT.emitSingleReductionCombiner(CGF, E, *IPriv, cast<DeclRefExpr>(*ILHS), |
| 5669 | cast<DeclRefExpr>(*IRHS)); |
Richard Trieu | cc3949d | 2016-02-18 22:34:54 +0000 | [diff] [blame] | 5670 | ++IPriv; |
| 5671 | ++ILHS; |
| 5672 | ++IRHS; |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5673 | } |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5674 | }; |
| 5675 | RegionCodeGenTy RCG(CodeGen); |
| 5676 | CommonActionTy Action( |
| 5677 | nullptr, llvm::None, |
| 5678 | createRuntimeFunction(WithNowait ? OMPRTL__kmpc_end_reduce_nowait |
| 5679 | : OMPRTL__kmpc_end_reduce), |
| 5680 | EndArgs); |
| 5681 | RCG.setAction(Action); |
| 5682 | RCG(CGF); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5683 | |
| 5684 | CGF.EmitBranch(DefaultBB); |
| 5685 | |
| 5686 | // 7. Build case 2: |
| 5687 | // ... |
| 5688 | // Atomic(<LHSExprs>[i] = RedOp<i>(*<LHSExprs>[i], *<RHSExprs>[i])); |
| 5689 | // ... |
| 5690 | // break; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5691 | llvm::BasicBlock *Case2BB = CGF.createBasicBlock(".omp.reduction.case2"); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5692 | SwInst->addCase(CGF.Builder.getInt32(2), Case2BB); |
| 5693 | CGF.EmitBlock(Case2BB); |
| 5694 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5695 | auto &&AtomicCodeGen = [Loc, Privates, LHSExprs, RHSExprs, ReductionOps]( |
| 5696 | CodeGenFunction &CGF, PrePostActionTy &Action) { |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5697 | auto ILHS = LHSExprs.begin(); |
| 5698 | auto IRHS = RHSExprs.begin(); |
| 5699 | auto IPriv = Privates.begin(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5700 | for (const Expr *E : ReductionOps) { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5701 | const Expr *XExpr = nullptr; |
| 5702 | const Expr *EExpr = nullptr; |
| 5703 | const Expr *UpExpr = nullptr; |
| 5704 | BinaryOperatorKind BO = BO_Comma; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5705 | if (const auto *BO = dyn_cast<BinaryOperator>(E)) { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5706 | if (BO->getOpcode() == BO_Assign) { |
| 5707 | XExpr = BO->getLHS(); |
| 5708 | UpExpr = BO->getRHS(); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5709 | } |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5710 | } |
| 5711 | // Try to emit update expression as a simple atomic. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5712 | const Expr *RHSExpr = UpExpr; |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5713 | if (RHSExpr) { |
| 5714 | // Analyze RHS part of the whole expression. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5715 | if (const auto *ACO = dyn_cast<AbstractConditionalOperator>( |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5716 | RHSExpr->IgnoreParenImpCasts())) { |
| 5717 | // If this is a conditional operator, analyze its condition for |
| 5718 | // min/max reduction operator. |
| 5719 | RHSExpr = ACO->getCond(); |
Alexey Bataev | 69a4779 | 2015-05-07 03:54:03 +0000 | [diff] [blame] | 5720 | } |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5721 | if (const auto *BORHS = |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5722 | dyn_cast<BinaryOperator>(RHSExpr->IgnoreParenImpCasts())) { |
| 5723 | EExpr = BORHS->getRHS(); |
| 5724 | BO = BORHS->getOpcode(); |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5725 | } |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5726 | } |
| 5727 | if (XExpr) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5728 | const auto *VD = cast<VarDecl>(cast<DeclRefExpr>(*ILHS)->getDecl()); |
Malcolm Parsons | c6e4583 | 2017-01-13 18:55:32 +0000 | [diff] [blame] | 5729 | auto &&AtomicRedGen = [BO, VD, |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5730 | Loc](CodeGenFunction &CGF, const Expr *XExpr, |
| 5731 | const Expr *EExpr, const Expr *UpExpr) { |
| 5732 | LValue X = CGF.EmitLValue(XExpr); |
| 5733 | RValue E; |
| 5734 | if (EExpr) |
| 5735 | E = CGF.EmitAnyExpr(EExpr); |
| 5736 | CGF.EmitOMPAtomicSimpleUpdateExpr( |
JF Bastien | 92f4ef1 | 2016-04-06 17:26:42 +0000 | [diff] [blame] | 5737 | X, E, BO, /*IsXLHSInRHSPart=*/true, |
| 5738 | llvm::AtomicOrdering::Monotonic, Loc, |
Malcolm Parsons | c6e4583 | 2017-01-13 18:55:32 +0000 | [diff] [blame] | 5739 | [&CGF, UpExpr, VD, Loc](RValue XRValue) { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5740 | CodeGenFunction::OMPPrivateScope PrivateScope(CGF); |
| 5741 | PrivateScope.addPrivate( |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5742 | VD, [&CGF, VD, XRValue, Loc]() { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5743 | Address LHSTemp = CGF.CreateMemTemp(VD->getType()); |
| 5744 | CGF.emitOMPSimpleStore( |
| 5745 | CGF.MakeAddrLValue(LHSTemp, VD->getType()), XRValue, |
| 5746 | VD->getType().getNonReferenceType(), Loc); |
| 5747 | return LHSTemp; |
| 5748 | }); |
| 5749 | (void)PrivateScope.Privatize(); |
| 5750 | return CGF.EmitAnyExpr(UpExpr); |
| 5751 | }); |
| 5752 | }; |
| 5753 | if ((*IPriv)->getType()->isArrayType()) { |
| 5754 | // Emit atomic reduction for array section. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5755 | const auto *RHSVar = |
| 5756 | cast<VarDecl>(cast<DeclRefExpr>(*IRHS)->getDecl()); |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5757 | EmitOMPAggregateReduction(CGF, (*IPriv)->getType(), VD, RHSVar, |
| 5758 | AtomicRedGen, XExpr, EExpr, UpExpr); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5759 | } else { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5760 | // Emit atomic reduction for array subscript or single variable. |
| 5761 | AtomicRedGen(CGF, XExpr, EExpr, UpExpr); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5762 | } |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5763 | } else { |
| 5764 | // Emit as a critical region. |
| 5765 | auto &&CritRedGen = [E, Loc](CodeGenFunction &CGF, const Expr *, |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 5766 | const Expr *, const Expr *) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5767 | CGOpenMPRuntime &RT = CGF.CGM.getOpenMPRuntime(); |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 5768 | std::string Name = RT.getName({"atomic_reduction"}); |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5769 | RT.emitCriticalRegion( |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 5770 | CGF, Name, |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5771 | [=](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 5772 | Action.Enter(CGF); |
| 5773 | emitReductionCombiner(CGF, E); |
| 5774 | }, |
| 5775 | Loc); |
| 5776 | }; |
| 5777 | if ((*IPriv)->getType()->isArrayType()) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5778 | const auto *LHSVar = |
| 5779 | cast<VarDecl>(cast<DeclRefExpr>(*ILHS)->getDecl()); |
| 5780 | const auto *RHSVar = |
| 5781 | cast<VarDecl>(cast<DeclRefExpr>(*IRHS)->getDecl()); |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5782 | EmitOMPAggregateReduction(CGF, (*IPriv)->getType(), LHSVar, RHSVar, |
| 5783 | CritRedGen); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5784 | } else { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5785 | CritRedGen(CGF, nullptr, nullptr, nullptr); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5786 | } |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5787 | } |
Richard Trieu | cc3949d | 2016-02-18 22:34:54 +0000 | [diff] [blame] | 5788 | ++ILHS; |
| 5789 | ++IRHS; |
| 5790 | ++IPriv; |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5791 | } |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5792 | }; |
| 5793 | RegionCodeGenTy AtomicRCG(AtomicCodeGen); |
| 5794 | if (!WithNowait) { |
| 5795 | // Add emission of __kmpc_end_reduce(<loc>, <gtid>, &<lock>); |
| 5796 | llvm::Value *EndArgs[] = { |
| 5797 | IdentTLoc, // ident_t *<loc> |
| 5798 | ThreadId, // i32 <gtid> |
| 5799 | Lock // kmp_critical_name *&<lock> |
| 5800 | }; |
| 5801 | CommonActionTy Action(nullptr, llvm::None, |
| 5802 | createRuntimeFunction(OMPRTL__kmpc_end_reduce), |
| 5803 | EndArgs); |
| 5804 | AtomicRCG.setAction(Action); |
| 5805 | AtomicRCG(CGF); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5806 | } else { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5807 | AtomicRCG(CGF); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5808 | } |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5809 | |
| 5810 | CGF.EmitBranch(DefaultBB); |
| 5811 | CGF.EmitBlock(DefaultBB, /*IsFinished=*/true); |
| 5812 | } |
| 5813 | |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 5814 | /// Generates unique name for artificial threadprivate variables. |
Alexey Bataev | 1c44e15 | 2018-03-06 18:59:43 +0000 | [diff] [blame] | 5815 | /// Format is: <Prefix> "." <Decl_mangled_name> "_" "<Decl_start_loc_raw_enc>" |
| 5816 | static std::string generateUniqueName(CodeGenModule &CGM, StringRef Prefix, |
| 5817 | const Expr *Ref) { |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 5818 | SmallString<256> Buffer; |
| 5819 | llvm::raw_svector_ostream Out(Buffer); |
Alexey Bataev | 1c44e15 | 2018-03-06 18:59:43 +0000 | [diff] [blame] | 5820 | const clang::DeclRefExpr *DE; |
| 5821 | const VarDecl *D = ::getBaseDecl(Ref, DE); |
| 5822 | if (!D) |
| 5823 | D = cast<VarDecl>(cast<DeclRefExpr>(Ref)->getDecl()); |
| 5824 | D = D->getCanonicalDecl(); |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 5825 | std::string Name = CGM.getOpenMPRuntime().getName( |
| 5826 | {D->isLocalVarDeclOrParm() ? D->getName() : CGM.getMangledName(D)}); |
| 5827 | Out << Prefix << Name << "_" |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 5828 | << D->getCanonicalDecl()->getBeginLoc().getRawEncoding(); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 5829 | return Out.str(); |
| 5830 | } |
| 5831 | |
| 5832 | /// Emits reduction initializer function: |
| 5833 | /// \code |
| 5834 | /// void @.red_init(void* %arg) { |
| 5835 | /// %0 = bitcast void* %arg to <type>* |
| 5836 | /// store <type> <init>, <type>* %0 |
| 5837 | /// ret void |
| 5838 | /// } |
| 5839 | /// \endcode |
| 5840 | static llvm::Value *emitReduceInitFunction(CodeGenModule &CGM, |
| 5841 | SourceLocation Loc, |
| 5842 | ReductionCodeGen &RCG, unsigned N) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5843 | ASTContext &C = CGM.getContext(); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 5844 | FunctionArgList Args; |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 5845 | ImplicitParamDecl Param(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, C.VoidPtrTy, |
| 5846 | ImplicitParamDecl::Other); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 5847 | Args.emplace_back(&Param); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5848 | const auto &FnInfo = |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 5849 | CGM.getTypes().arrangeBuiltinFunctionDeclaration(C.VoidTy, Args); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5850 | llvm::FunctionType *FnTy = CGM.getTypes().GetFunctionType(FnInfo); |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 5851 | std::string Name = CGM.getOpenMPRuntime().getName({"red_init", ""}); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 5852 | auto *Fn = llvm::Function::Create(FnTy, llvm::GlobalValue::InternalLinkage, |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 5853 | Name, &CGM.getModule()); |
Rafael Espindola | 51ec5a9 | 2018-02-28 23:46:35 +0000 | [diff] [blame] | 5854 | CGM.SetInternalFunctionAttributes(GlobalDecl(), Fn, FnInfo); |
Alexey Bataev | c0f879b | 2018-04-10 20:10:53 +0000 | [diff] [blame] | 5855 | Fn->setDoesNotRecurse(); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 5856 | CodeGenFunction CGF(CGM); |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 5857 | CGF.StartFunction(GlobalDecl(), C.VoidTy, Fn, FnInfo, Args, Loc, Loc); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 5858 | Address PrivateAddr = CGF.EmitLoadOfPointer( |
| 5859 | CGF.GetAddrOfLocalVar(&Param), |
| 5860 | C.getPointerType(C.VoidPtrTy).castAs<PointerType>()); |
| 5861 | llvm::Value *Size = nullptr; |
| 5862 | // If the size of the reduction item is non-constant, load it from global |
| 5863 | // threadprivate variable. |
| 5864 | if (RCG.getSizes(N).second) { |
| 5865 | Address SizeAddr = CGM.getOpenMPRuntime().getAddrOfArtificialThreadPrivate( |
| 5866 | CGF, CGM.getContext().getSizeType(), |
Alexey Bataev | 1c44e15 | 2018-03-06 18:59:43 +0000 | [diff] [blame] | 5867 | generateUniqueName(CGM, "reduction_size", RCG.getRefExpr(N))); |
Alexey Bataev | a9b9cc0 | 2018-01-23 18:12:38 +0000 | [diff] [blame] | 5868 | Size = CGF.EmitLoadOfScalar(SizeAddr, /*Volatile=*/false, |
| 5869 | CGM.getContext().getSizeType(), Loc); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 5870 | } |
| 5871 | RCG.emitAggregateType(CGF, N, Size); |
| 5872 | LValue SharedLVal; |
| 5873 | // If initializer uses initializer from declare reduction construct, emit a |
| 5874 | // pointer to the address of the original reduction item (reuired by reduction |
| 5875 | // initializer) |
| 5876 | if (RCG.usesReductionInitializer(N)) { |
| 5877 | Address SharedAddr = |
| 5878 | CGM.getOpenMPRuntime().getAddrOfArtificialThreadPrivate( |
| 5879 | CGF, CGM.getContext().VoidPtrTy, |
Alexey Bataev | 1c44e15 | 2018-03-06 18:59:43 +0000 | [diff] [blame] | 5880 | generateUniqueName(CGM, "reduction", RCG.getRefExpr(N))); |
Alexey Bataev | 21dab12 | 2018-03-09 15:20:30 +0000 | [diff] [blame] | 5881 | SharedAddr = CGF.EmitLoadOfPointer( |
| 5882 | SharedAddr, |
| 5883 | CGM.getContext().VoidPtrTy.castAs<PointerType>()->getTypePtr()); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 5884 | SharedLVal = CGF.MakeAddrLValue(SharedAddr, CGM.getContext().VoidPtrTy); |
| 5885 | } else { |
| 5886 | SharedLVal = CGF.MakeNaturalAlignAddrLValue( |
| 5887 | llvm::ConstantPointerNull::get(CGM.VoidPtrTy), |
| 5888 | CGM.getContext().VoidPtrTy); |
| 5889 | } |
| 5890 | // Emit the initializer: |
| 5891 | // %0 = bitcast void* %arg to <type>* |
| 5892 | // store <type> <init>, <type>* %0 |
| 5893 | RCG.emitInitialization(CGF, N, PrivateAddr, SharedLVal, |
| 5894 | [](CodeGenFunction &) { return false; }); |
| 5895 | CGF.FinishFunction(); |
| 5896 | return Fn; |
| 5897 | } |
| 5898 | |
| 5899 | /// Emits reduction combiner function: |
| 5900 | /// \code |
| 5901 | /// void @.red_comb(void* %arg0, void* %arg1) { |
| 5902 | /// %lhs = bitcast void* %arg0 to <type>* |
| 5903 | /// %rhs = bitcast void* %arg1 to <type>* |
| 5904 | /// %2 = <ReductionOp>(<type>* %lhs, <type>* %rhs) |
| 5905 | /// store <type> %2, <type>* %lhs |
| 5906 | /// ret void |
| 5907 | /// } |
| 5908 | /// \endcode |
| 5909 | static llvm::Value *emitReduceCombFunction(CodeGenModule &CGM, |
| 5910 | SourceLocation Loc, |
| 5911 | ReductionCodeGen &RCG, unsigned N, |
| 5912 | const Expr *ReductionOp, |
| 5913 | const Expr *LHS, const Expr *RHS, |
| 5914 | const Expr *PrivateRef) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5915 | ASTContext &C = CGM.getContext(); |
| 5916 | const auto *LHSVD = cast<VarDecl>(cast<DeclRefExpr>(LHS)->getDecl()); |
| 5917 | const auto *RHSVD = cast<VarDecl>(cast<DeclRefExpr>(RHS)->getDecl()); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 5918 | FunctionArgList Args; |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 5919 | ImplicitParamDecl ParamInOut(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, |
| 5920 | C.VoidPtrTy, ImplicitParamDecl::Other); |
| 5921 | ImplicitParamDecl ParamIn(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, C.VoidPtrTy, |
| 5922 | ImplicitParamDecl::Other); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 5923 | Args.emplace_back(&ParamInOut); |
| 5924 | Args.emplace_back(&ParamIn); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5925 | const auto &FnInfo = |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 5926 | CGM.getTypes().arrangeBuiltinFunctionDeclaration(C.VoidTy, Args); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5927 | llvm::FunctionType *FnTy = CGM.getTypes().GetFunctionType(FnInfo); |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 5928 | std::string Name = CGM.getOpenMPRuntime().getName({"red_comb", ""}); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 5929 | auto *Fn = llvm::Function::Create(FnTy, llvm::GlobalValue::InternalLinkage, |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 5930 | Name, &CGM.getModule()); |
Rafael Espindola | 51ec5a9 | 2018-02-28 23:46:35 +0000 | [diff] [blame] | 5931 | CGM.SetInternalFunctionAttributes(GlobalDecl(), Fn, FnInfo); |
Alexey Bataev | c0f879b | 2018-04-10 20:10:53 +0000 | [diff] [blame] | 5932 | Fn->setDoesNotRecurse(); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 5933 | CodeGenFunction CGF(CGM); |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 5934 | CGF.StartFunction(GlobalDecl(), C.VoidTy, Fn, FnInfo, Args, Loc, Loc); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 5935 | llvm::Value *Size = nullptr; |
| 5936 | // If the size of the reduction item is non-constant, load it from global |
| 5937 | // threadprivate variable. |
| 5938 | if (RCG.getSizes(N).second) { |
| 5939 | Address SizeAddr = CGM.getOpenMPRuntime().getAddrOfArtificialThreadPrivate( |
| 5940 | CGF, CGM.getContext().getSizeType(), |
Alexey Bataev | 1c44e15 | 2018-03-06 18:59:43 +0000 | [diff] [blame] | 5941 | generateUniqueName(CGM, "reduction_size", RCG.getRefExpr(N))); |
Alexey Bataev | a9b9cc0 | 2018-01-23 18:12:38 +0000 | [diff] [blame] | 5942 | Size = CGF.EmitLoadOfScalar(SizeAddr, /*Volatile=*/false, |
| 5943 | CGM.getContext().getSizeType(), Loc); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 5944 | } |
| 5945 | RCG.emitAggregateType(CGF, N, Size); |
| 5946 | // Remap lhs and rhs variables to the addresses of the function arguments. |
| 5947 | // %lhs = bitcast void* %arg0 to <type>* |
| 5948 | // %rhs = bitcast void* %arg1 to <type>* |
| 5949 | CodeGenFunction::OMPPrivateScope PrivateScope(CGF); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5950 | PrivateScope.addPrivate(LHSVD, [&C, &CGF, &ParamInOut, LHSVD]() { |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 5951 | // Pull out the pointer to the variable. |
| 5952 | Address PtrAddr = CGF.EmitLoadOfPointer( |
| 5953 | CGF.GetAddrOfLocalVar(&ParamInOut), |
| 5954 | C.getPointerType(C.VoidPtrTy).castAs<PointerType>()); |
| 5955 | return CGF.Builder.CreateElementBitCast( |
| 5956 | PtrAddr, CGF.ConvertTypeForMem(LHSVD->getType())); |
| 5957 | }); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5958 | PrivateScope.addPrivate(RHSVD, [&C, &CGF, &ParamIn, RHSVD]() { |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 5959 | // Pull out the pointer to the variable. |
| 5960 | Address PtrAddr = CGF.EmitLoadOfPointer( |
| 5961 | CGF.GetAddrOfLocalVar(&ParamIn), |
| 5962 | C.getPointerType(C.VoidPtrTy).castAs<PointerType>()); |
| 5963 | return CGF.Builder.CreateElementBitCast( |
| 5964 | PtrAddr, CGF.ConvertTypeForMem(RHSVD->getType())); |
| 5965 | }); |
| 5966 | PrivateScope.Privatize(); |
| 5967 | // Emit the combiner body: |
| 5968 | // %2 = <ReductionOp>(<type> *%lhs, <type> *%rhs) |
| 5969 | // store <type> %2, <type>* %lhs |
| 5970 | CGM.getOpenMPRuntime().emitSingleReductionCombiner( |
| 5971 | CGF, ReductionOp, PrivateRef, cast<DeclRefExpr>(LHS), |
| 5972 | cast<DeclRefExpr>(RHS)); |
| 5973 | CGF.FinishFunction(); |
| 5974 | return Fn; |
| 5975 | } |
| 5976 | |
| 5977 | /// Emits reduction finalizer function: |
| 5978 | /// \code |
| 5979 | /// void @.red_fini(void* %arg) { |
| 5980 | /// %0 = bitcast void* %arg to <type>* |
| 5981 | /// <destroy>(<type>* %0) |
| 5982 | /// ret void |
| 5983 | /// } |
| 5984 | /// \endcode |
| 5985 | static llvm::Value *emitReduceFiniFunction(CodeGenModule &CGM, |
| 5986 | SourceLocation Loc, |
| 5987 | ReductionCodeGen &RCG, unsigned N) { |
| 5988 | if (!RCG.needCleanups(N)) |
| 5989 | return nullptr; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5990 | ASTContext &C = CGM.getContext(); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 5991 | FunctionArgList Args; |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 5992 | ImplicitParamDecl Param(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, C.VoidPtrTy, |
| 5993 | ImplicitParamDecl::Other); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 5994 | Args.emplace_back(&Param); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5995 | const auto &FnInfo = |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 5996 | CGM.getTypes().arrangeBuiltinFunctionDeclaration(C.VoidTy, Args); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5997 | llvm::FunctionType *FnTy = CGM.getTypes().GetFunctionType(FnInfo); |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 5998 | std::string Name = CGM.getOpenMPRuntime().getName({"red_fini", ""}); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 5999 | auto *Fn = llvm::Function::Create(FnTy, llvm::GlobalValue::InternalLinkage, |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 6000 | Name, &CGM.getModule()); |
Rafael Espindola | 51ec5a9 | 2018-02-28 23:46:35 +0000 | [diff] [blame] | 6001 | CGM.SetInternalFunctionAttributes(GlobalDecl(), Fn, FnInfo); |
Alexey Bataev | c0f879b | 2018-04-10 20:10:53 +0000 | [diff] [blame] | 6002 | Fn->setDoesNotRecurse(); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 6003 | CodeGenFunction CGF(CGM); |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 6004 | CGF.StartFunction(GlobalDecl(), C.VoidTy, Fn, FnInfo, Args, Loc, Loc); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 6005 | Address PrivateAddr = CGF.EmitLoadOfPointer( |
| 6006 | CGF.GetAddrOfLocalVar(&Param), |
| 6007 | C.getPointerType(C.VoidPtrTy).castAs<PointerType>()); |
| 6008 | llvm::Value *Size = nullptr; |
| 6009 | // If the size of the reduction item is non-constant, load it from global |
| 6010 | // threadprivate variable. |
| 6011 | if (RCG.getSizes(N).second) { |
| 6012 | Address SizeAddr = CGM.getOpenMPRuntime().getAddrOfArtificialThreadPrivate( |
| 6013 | CGF, CGM.getContext().getSizeType(), |
Alexey Bataev | 1c44e15 | 2018-03-06 18:59:43 +0000 | [diff] [blame] | 6014 | generateUniqueName(CGM, "reduction_size", RCG.getRefExpr(N))); |
Alexey Bataev | a9b9cc0 | 2018-01-23 18:12:38 +0000 | [diff] [blame] | 6015 | Size = CGF.EmitLoadOfScalar(SizeAddr, /*Volatile=*/false, |
| 6016 | CGM.getContext().getSizeType(), Loc); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 6017 | } |
| 6018 | RCG.emitAggregateType(CGF, N, Size); |
| 6019 | // Emit the finalizer body: |
| 6020 | // <destroy>(<type>* %0) |
| 6021 | RCG.emitCleanups(CGF, N, PrivateAddr); |
| 6022 | CGF.FinishFunction(); |
| 6023 | return Fn; |
| 6024 | } |
| 6025 | |
| 6026 | llvm::Value *CGOpenMPRuntime::emitTaskReductionInit( |
| 6027 | CodeGenFunction &CGF, SourceLocation Loc, ArrayRef<const Expr *> LHSExprs, |
| 6028 | ArrayRef<const Expr *> RHSExprs, const OMPTaskDataTy &Data) { |
| 6029 | if (!CGF.HaveInsertPoint() || Data.ReductionVars.empty()) |
| 6030 | return nullptr; |
| 6031 | |
| 6032 | // Build typedef struct: |
| 6033 | // kmp_task_red_input { |
| 6034 | // void *reduce_shar; // shared reduction item |
| 6035 | // size_t reduce_size; // size of data item |
| 6036 | // void *reduce_init; // data initialization routine |
| 6037 | // void *reduce_fini; // data finalization routine |
| 6038 | // void *reduce_comb; // data combiner routine |
| 6039 | // kmp_task_red_flags_t flags; // flags for additional info from compiler |
| 6040 | // } kmp_task_red_input_t; |
| 6041 | ASTContext &C = CGM.getContext(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6042 | RecordDecl *RD = C.buildImplicitRecord("kmp_task_red_input_t"); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 6043 | RD->startDefinition(); |
| 6044 | const FieldDecl *SharedFD = addFieldToRecordDecl(C, RD, C.VoidPtrTy); |
| 6045 | const FieldDecl *SizeFD = addFieldToRecordDecl(C, RD, C.getSizeType()); |
| 6046 | const FieldDecl *InitFD = addFieldToRecordDecl(C, RD, C.VoidPtrTy); |
| 6047 | const FieldDecl *FiniFD = addFieldToRecordDecl(C, RD, C.VoidPtrTy); |
| 6048 | const FieldDecl *CombFD = addFieldToRecordDecl(C, RD, C.VoidPtrTy); |
| 6049 | const FieldDecl *FlagsFD = addFieldToRecordDecl( |
| 6050 | C, RD, C.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/false)); |
| 6051 | RD->completeDefinition(); |
| 6052 | QualType RDType = C.getRecordType(RD); |
| 6053 | unsigned Size = Data.ReductionVars.size(); |
| 6054 | llvm::APInt ArraySize(/*numBits=*/64, Size); |
| 6055 | QualType ArrayRDType = C.getConstantArrayType( |
| 6056 | RDType, ArraySize, ArrayType::Normal, /*IndexTypeQuals=*/0); |
| 6057 | // kmp_task_red_input_t .rd_input.[Size]; |
| 6058 | Address TaskRedInput = CGF.CreateMemTemp(ArrayRDType, ".rd_input."); |
| 6059 | ReductionCodeGen RCG(Data.ReductionVars, Data.ReductionCopies, |
| 6060 | Data.ReductionOps); |
| 6061 | for (unsigned Cnt = 0; Cnt < Size; ++Cnt) { |
| 6062 | // kmp_task_red_input_t &ElemLVal = .rd_input.[Cnt]; |
| 6063 | llvm::Value *Idxs[] = {llvm::ConstantInt::get(CGM.SizeTy, /*V=*/0), |
| 6064 | llvm::ConstantInt::get(CGM.SizeTy, Cnt)}; |
| 6065 | llvm::Value *GEP = CGF.EmitCheckedInBoundsGEP( |
| 6066 | TaskRedInput.getPointer(), Idxs, |
| 6067 | /*SignedIndices=*/false, /*IsSubtraction=*/false, Loc, |
| 6068 | ".rd_input.gep."); |
| 6069 | LValue ElemLVal = CGF.MakeNaturalAlignAddrLValue(GEP, RDType); |
| 6070 | // ElemLVal.reduce_shar = &Shareds[Cnt]; |
| 6071 | LValue SharedLVal = CGF.EmitLValueForField(ElemLVal, SharedFD); |
| 6072 | RCG.emitSharedLValue(CGF, Cnt); |
| 6073 | llvm::Value *CastedShared = |
| 6074 | CGF.EmitCastToVoidPtr(RCG.getSharedLValue(Cnt).getPointer()); |
| 6075 | CGF.EmitStoreOfScalar(CastedShared, SharedLVal); |
| 6076 | RCG.emitAggregateType(CGF, Cnt); |
| 6077 | llvm::Value *SizeValInChars; |
| 6078 | llvm::Value *SizeVal; |
| 6079 | std::tie(SizeValInChars, SizeVal) = RCG.getSizes(Cnt); |
| 6080 | // We use delayed creation/initialization for VLAs, array sections and |
| 6081 | // custom reduction initializations. It is required because runtime does not |
| 6082 | // provide the way to pass the sizes of VLAs/array sections to |
| 6083 | // initializer/combiner/finalizer functions and does not pass the pointer to |
| 6084 | // original reduction item to the initializer. Instead threadprivate global |
| 6085 | // variables are used to store these values and use them in the functions. |
| 6086 | bool DelayedCreation = !!SizeVal; |
| 6087 | SizeValInChars = CGF.Builder.CreateIntCast(SizeValInChars, CGM.SizeTy, |
| 6088 | /*isSigned=*/false); |
| 6089 | LValue SizeLVal = CGF.EmitLValueForField(ElemLVal, SizeFD); |
| 6090 | CGF.EmitStoreOfScalar(SizeValInChars, SizeLVal); |
| 6091 | // ElemLVal.reduce_init = init; |
| 6092 | LValue InitLVal = CGF.EmitLValueForField(ElemLVal, InitFD); |
| 6093 | llvm::Value *InitAddr = |
| 6094 | CGF.EmitCastToVoidPtr(emitReduceInitFunction(CGM, Loc, RCG, Cnt)); |
| 6095 | CGF.EmitStoreOfScalar(InitAddr, InitLVal); |
| 6096 | DelayedCreation = DelayedCreation || RCG.usesReductionInitializer(Cnt); |
| 6097 | // ElemLVal.reduce_fini = fini; |
| 6098 | LValue FiniLVal = CGF.EmitLValueForField(ElemLVal, FiniFD); |
| 6099 | llvm::Value *Fini = emitReduceFiniFunction(CGM, Loc, RCG, Cnt); |
| 6100 | llvm::Value *FiniAddr = Fini |
| 6101 | ? CGF.EmitCastToVoidPtr(Fini) |
| 6102 | : llvm::ConstantPointerNull::get(CGM.VoidPtrTy); |
| 6103 | CGF.EmitStoreOfScalar(FiniAddr, FiniLVal); |
| 6104 | // ElemLVal.reduce_comb = comb; |
| 6105 | LValue CombLVal = CGF.EmitLValueForField(ElemLVal, CombFD); |
| 6106 | llvm::Value *CombAddr = CGF.EmitCastToVoidPtr(emitReduceCombFunction( |
| 6107 | CGM, Loc, RCG, Cnt, Data.ReductionOps[Cnt], LHSExprs[Cnt], |
| 6108 | RHSExprs[Cnt], Data.ReductionCopies[Cnt])); |
| 6109 | CGF.EmitStoreOfScalar(CombAddr, CombLVal); |
| 6110 | // ElemLVal.flags = 0; |
| 6111 | LValue FlagsLVal = CGF.EmitLValueForField(ElemLVal, FlagsFD); |
| 6112 | if (DelayedCreation) { |
| 6113 | CGF.EmitStoreOfScalar( |
| 6114 | llvm::ConstantInt::get(CGM.Int32Ty, /*V=*/1, /*IsSigned=*/true), |
| 6115 | FlagsLVal); |
| 6116 | } else |
| 6117 | CGF.EmitNullInitialization(FlagsLVal.getAddress(), FlagsLVal.getType()); |
| 6118 | } |
| 6119 | // Build call void *__kmpc_task_reduction_init(int gtid, int num_data, void |
| 6120 | // *data); |
| 6121 | llvm::Value *Args[] = { |
| 6122 | CGF.Builder.CreateIntCast(getThreadID(CGF, Loc), CGM.IntTy, |
| 6123 | /*isSigned=*/true), |
| 6124 | llvm::ConstantInt::get(CGM.IntTy, Size, /*isSigned=*/true), |
| 6125 | CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(TaskRedInput.getPointer(), |
| 6126 | CGM.VoidPtrTy)}; |
| 6127 | return CGF.EmitRuntimeCall( |
| 6128 | createRuntimeFunction(OMPRTL__kmpc_task_reduction_init), Args); |
| 6129 | } |
| 6130 | |
| 6131 | void CGOpenMPRuntime::emitTaskReductionFixups(CodeGenFunction &CGF, |
| 6132 | SourceLocation Loc, |
| 6133 | ReductionCodeGen &RCG, |
| 6134 | unsigned N) { |
| 6135 | auto Sizes = RCG.getSizes(N); |
| 6136 | // Emit threadprivate global variable if the type is non-constant |
| 6137 | // (Sizes.second = nullptr). |
| 6138 | if (Sizes.second) { |
| 6139 | llvm::Value *SizeVal = CGF.Builder.CreateIntCast(Sizes.second, CGM.SizeTy, |
| 6140 | /*isSigned=*/false); |
| 6141 | Address SizeAddr = getAddrOfArtificialThreadPrivate( |
| 6142 | CGF, CGM.getContext().getSizeType(), |
Alexey Bataev | 1c44e15 | 2018-03-06 18:59:43 +0000 | [diff] [blame] | 6143 | generateUniqueName(CGM, "reduction_size", RCG.getRefExpr(N))); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 6144 | CGF.Builder.CreateStore(SizeVal, SizeAddr, /*IsVolatile=*/false); |
| 6145 | } |
| 6146 | // Store address of the original reduction item if custom initializer is used. |
| 6147 | if (RCG.usesReductionInitializer(N)) { |
| 6148 | Address SharedAddr = getAddrOfArtificialThreadPrivate( |
| 6149 | CGF, CGM.getContext().VoidPtrTy, |
Alexey Bataev | 1c44e15 | 2018-03-06 18:59:43 +0000 | [diff] [blame] | 6150 | generateUniqueName(CGM, "reduction", RCG.getRefExpr(N))); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 6151 | CGF.Builder.CreateStore( |
| 6152 | CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
| 6153 | RCG.getSharedLValue(N).getPointer(), CGM.VoidPtrTy), |
| 6154 | SharedAddr, /*IsVolatile=*/false); |
| 6155 | } |
| 6156 | } |
| 6157 | |
| 6158 | Address CGOpenMPRuntime::getTaskReductionItem(CodeGenFunction &CGF, |
| 6159 | SourceLocation Loc, |
| 6160 | llvm::Value *ReductionsPtr, |
| 6161 | LValue SharedLVal) { |
| 6162 | // Build call void *__kmpc_task_reduction_get_th_data(int gtid, void *tg, void |
| 6163 | // *d); |
| 6164 | llvm::Value *Args[] = { |
| 6165 | CGF.Builder.CreateIntCast(getThreadID(CGF, Loc), CGM.IntTy, |
| 6166 | /*isSigned=*/true), |
| 6167 | ReductionsPtr, |
| 6168 | CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(SharedLVal.getPointer(), |
| 6169 | CGM.VoidPtrTy)}; |
| 6170 | return Address( |
| 6171 | CGF.EmitRuntimeCall( |
| 6172 | createRuntimeFunction(OMPRTL__kmpc_task_reduction_get_th_data), Args), |
| 6173 | SharedLVal.getAlignment()); |
| 6174 | } |
| 6175 | |
Alexey Bataev | 8b8e202 | 2015-04-27 05:22:09 +0000 | [diff] [blame] | 6176 | void CGOpenMPRuntime::emitTaskwaitCall(CodeGenFunction &CGF, |
| 6177 | SourceLocation Loc) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 6178 | if (!CGF.HaveInsertPoint()) |
| 6179 | return; |
Alexey Bataev | 8b8e202 | 2015-04-27 05:22:09 +0000 | [diff] [blame] | 6180 | // Build call kmp_int32 __kmpc_omp_taskwait(ident_t *loc, kmp_int32 |
| 6181 | // global_tid); |
| 6182 | llvm::Value *Args[] = {emitUpdateLocation(CGF, Loc), getThreadID(CGF, Loc)}; |
| 6183 | // Ignore return result until untied tasks are supported. |
| 6184 | CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_omp_taskwait), Args); |
Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 6185 | if (auto *Region = dyn_cast_or_null<CGOpenMPRegionInfo>(CGF.CapturedStmtInfo)) |
| 6186 | Region->emitUntiedSwitch(CGF); |
Alexey Bataev | 8b8e202 | 2015-04-27 05:22:09 +0000 | [diff] [blame] | 6187 | } |
| 6188 | |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 6189 | void CGOpenMPRuntime::emitInlinedDirective(CodeGenFunction &CGF, |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 6190 | OpenMPDirectiveKind InnerKind, |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 6191 | const RegionCodeGenTy &CodeGen, |
| 6192 | bool HasCancel) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 6193 | if (!CGF.HaveInsertPoint()) |
| 6194 | return; |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 6195 | InlinedOpenMPRegionRAII Region(CGF, CodeGen, InnerKind, HasCancel); |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 6196 | CGF.CapturedStmtInfo->EmitBody(CGF, /*S=*/nullptr); |
Alexey Bataev | 8cbe0a6 | 2015-02-26 10:27:34 +0000 | [diff] [blame] | 6197 | } |
| 6198 | |
Alexey Bataev | 7d5d33e | 2015-07-06 05:50:32 +0000 | [diff] [blame] | 6199 | namespace { |
| 6200 | enum RTCancelKind { |
| 6201 | CancelNoreq = 0, |
| 6202 | CancelParallel = 1, |
| 6203 | CancelLoop = 2, |
| 6204 | CancelSections = 3, |
| 6205 | CancelTaskgroup = 4 |
| 6206 | }; |
Eugene Zelenko | 0a4f3f4 | 2016-02-10 19:11:58 +0000 | [diff] [blame] | 6207 | } // anonymous namespace |
Alexey Bataev | 7d5d33e | 2015-07-06 05:50:32 +0000 | [diff] [blame] | 6208 | |
| 6209 | static RTCancelKind getCancellationKind(OpenMPDirectiveKind CancelRegion) { |
| 6210 | RTCancelKind CancelKind = CancelNoreq; |
Alexey Bataev | 0f34da1 | 2015-07-02 04:17:07 +0000 | [diff] [blame] | 6211 | if (CancelRegion == OMPD_parallel) |
| 6212 | CancelKind = CancelParallel; |
| 6213 | else if (CancelRegion == OMPD_for) |
| 6214 | CancelKind = CancelLoop; |
| 6215 | else if (CancelRegion == OMPD_sections) |
| 6216 | CancelKind = CancelSections; |
| 6217 | else { |
| 6218 | assert(CancelRegion == OMPD_taskgroup); |
| 6219 | CancelKind = CancelTaskgroup; |
| 6220 | } |
Alexey Bataev | 7d5d33e | 2015-07-06 05:50:32 +0000 | [diff] [blame] | 6221 | return CancelKind; |
| 6222 | } |
| 6223 | |
| 6224 | void CGOpenMPRuntime::emitCancellationPointCall( |
| 6225 | CodeGenFunction &CGF, SourceLocation Loc, |
| 6226 | OpenMPDirectiveKind CancelRegion) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 6227 | if (!CGF.HaveInsertPoint()) |
| 6228 | return; |
Alexey Bataev | 7d5d33e | 2015-07-06 05:50:32 +0000 | [diff] [blame] | 6229 | // Build call kmp_int32 __kmpc_cancellationpoint(ident_t *loc, kmp_int32 |
| 6230 | // global_tid, kmp_int32 cncl_kind); |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 6231 | if (auto *OMPRegionInfo = |
| 6232 | dyn_cast_or_null<CGOpenMPRegionInfo>(CGF.CapturedStmtInfo)) { |
Jonas Hahnfeld | b07931f | 2017-02-17 18:32:58 +0000 | [diff] [blame] | 6233 | // For 'cancellation point taskgroup', the task region info may not have a |
| 6234 | // cancel. This may instead happen in another adjacent task. |
| 6235 | if (CancelRegion == OMPD_taskgroup || OMPRegionInfo->hasCancel()) { |
Alexey Bataev | 7d5d33e | 2015-07-06 05:50:32 +0000 | [diff] [blame] | 6236 | llvm::Value *Args[] = { |
| 6237 | emitUpdateLocation(CGF, Loc), getThreadID(CGF, Loc), |
| 6238 | CGF.Builder.getInt32(getCancellationKind(CancelRegion))}; |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 6239 | // Ignore return result until untied tasks are supported. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6240 | llvm::Value *Result = CGF.EmitRuntimeCall( |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 6241 | createRuntimeFunction(OMPRTL__kmpc_cancellationpoint), Args); |
| 6242 | // if (__kmpc_cancellationpoint()) { |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 6243 | // exit from construct; |
| 6244 | // } |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6245 | llvm::BasicBlock *ExitBB = CGF.createBasicBlock(".cancel.exit"); |
| 6246 | llvm::BasicBlock *ContBB = CGF.createBasicBlock(".cancel.continue"); |
| 6247 | llvm::Value *Cmp = CGF.Builder.CreateIsNotNull(Result); |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 6248 | CGF.Builder.CreateCondBr(Cmp, ExitBB, ContBB); |
| 6249 | CGF.EmitBlock(ExitBB); |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 6250 | // exit from construct; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6251 | CodeGenFunction::JumpDest CancelDest = |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 6252 | CGF.getOMPCancelDestination(OMPRegionInfo->getDirectiveKind()); |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 6253 | CGF.EmitBranchThroughCleanup(CancelDest); |
| 6254 | CGF.EmitBlock(ContBB, /*IsFinished=*/true); |
| 6255 | } |
Alexey Bataev | 0f34da1 | 2015-07-02 04:17:07 +0000 | [diff] [blame] | 6256 | } |
Alexey Bataev | 0f34da1 | 2015-07-02 04:17:07 +0000 | [diff] [blame] | 6257 | } |
| 6258 | |
Alexey Bataev | 7d5d33e | 2015-07-06 05:50:32 +0000 | [diff] [blame] | 6259 | void CGOpenMPRuntime::emitCancelCall(CodeGenFunction &CGF, SourceLocation Loc, |
Alexey Bataev | 87933c7 | 2015-09-18 08:07:34 +0000 | [diff] [blame] | 6260 | const Expr *IfCond, |
Alexey Bataev | 7d5d33e | 2015-07-06 05:50:32 +0000 | [diff] [blame] | 6261 | OpenMPDirectiveKind CancelRegion) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 6262 | if (!CGF.HaveInsertPoint()) |
| 6263 | return; |
Alexey Bataev | 7d5d33e | 2015-07-06 05:50:32 +0000 | [diff] [blame] | 6264 | // Build call kmp_int32 __kmpc_cancel(ident_t *loc, kmp_int32 global_tid, |
| 6265 | // kmp_int32 cncl_kind); |
| 6266 | if (auto *OMPRegionInfo = |
| 6267 | dyn_cast_or_null<CGOpenMPRegionInfo>(CGF.CapturedStmtInfo)) { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 6268 | auto &&ThenGen = [Loc, CancelRegion, OMPRegionInfo](CodeGenFunction &CGF, |
| 6269 | PrePostActionTy &) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6270 | CGOpenMPRuntime &RT = CGF.CGM.getOpenMPRuntime(); |
Alexey Bataev | 87933c7 | 2015-09-18 08:07:34 +0000 | [diff] [blame] | 6271 | llvm::Value *Args[] = { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 6272 | RT.emitUpdateLocation(CGF, Loc), RT.getThreadID(CGF, Loc), |
Alexey Bataev | 87933c7 | 2015-09-18 08:07:34 +0000 | [diff] [blame] | 6273 | CGF.Builder.getInt32(getCancellationKind(CancelRegion))}; |
| 6274 | // Ignore return result until untied tasks are supported. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6275 | llvm::Value *Result = CGF.EmitRuntimeCall( |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 6276 | RT.createRuntimeFunction(OMPRTL__kmpc_cancel), Args); |
Alexey Bataev | 87933c7 | 2015-09-18 08:07:34 +0000 | [diff] [blame] | 6277 | // if (__kmpc_cancel()) { |
Alexey Bataev | 87933c7 | 2015-09-18 08:07:34 +0000 | [diff] [blame] | 6278 | // exit from construct; |
| 6279 | // } |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6280 | llvm::BasicBlock *ExitBB = CGF.createBasicBlock(".cancel.exit"); |
| 6281 | llvm::BasicBlock *ContBB = CGF.createBasicBlock(".cancel.continue"); |
| 6282 | llvm::Value *Cmp = CGF.Builder.CreateIsNotNull(Result); |
Alexey Bataev | 87933c7 | 2015-09-18 08:07:34 +0000 | [diff] [blame] | 6283 | CGF.Builder.CreateCondBr(Cmp, ExitBB, ContBB); |
| 6284 | CGF.EmitBlock(ExitBB); |
Alexey Bataev | 87933c7 | 2015-09-18 08:07:34 +0000 | [diff] [blame] | 6285 | // exit from construct; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6286 | CodeGenFunction::JumpDest CancelDest = |
Alexey Bataev | 87933c7 | 2015-09-18 08:07:34 +0000 | [diff] [blame] | 6287 | CGF.getOMPCancelDestination(OMPRegionInfo->getDirectiveKind()); |
| 6288 | CGF.EmitBranchThroughCleanup(CancelDest); |
| 6289 | CGF.EmitBlock(ContBB, /*IsFinished=*/true); |
| 6290 | }; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6291 | if (IfCond) { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 6292 | emitOMPIfClause(CGF, IfCond, ThenGen, |
| 6293 | [](CodeGenFunction &, PrePostActionTy &) {}); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6294 | } else { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 6295 | RegionCodeGenTy ThenRCG(ThenGen); |
| 6296 | ThenRCG(CGF); |
| 6297 | } |
Alexey Bataev | 7d5d33e | 2015-07-06 05:50:32 +0000 | [diff] [blame] | 6298 | } |
| 6299 | } |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 6300 | |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 6301 | void CGOpenMPRuntime::emitTargetOutlinedFunction( |
| 6302 | const OMPExecutableDirective &D, StringRef ParentName, |
| 6303 | llvm::Function *&OutlinedFn, llvm::Constant *&OutlinedFnID, |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 6304 | bool IsOffloadEntry, const RegionCodeGenTy &CodeGen) { |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 6305 | assert(!ParentName.empty() && "Invalid target region parent name!"); |
Arpith Chacko Jacob | 5c309e4 | 2016-03-22 01:48:56 +0000 | [diff] [blame] | 6306 | emitTargetOutlinedFunctionHelper(D, ParentName, OutlinedFn, OutlinedFnID, |
| 6307 | IsOffloadEntry, CodeGen); |
| 6308 | } |
| 6309 | |
| 6310 | void CGOpenMPRuntime::emitTargetOutlinedFunctionHelper( |
| 6311 | const OMPExecutableDirective &D, StringRef ParentName, |
| 6312 | llvm::Function *&OutlinedFn, llvm::Constant *&OutlinedFnID, |
| 6313 | bool IsOffloadEntry, const RegionCodeGenTy &CodeGen) { |
Samuel Antao | 2de62b0 | 2016-02-13 23:35:10 +0000 | [diff] [blame] | 6314 | // Create a unique name for the entry function using the source location |
| 6315 | // information of the current target region. The name will be something like: |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 6316 | // |
Samuel Antao | 2de62b0 | 2016-02-13 23:35:10 +0000 | [diff] [blame] | 6317 | // __omp_offloading_DD_FFFF_PP_lBB |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 6318 | // |
| 6319 | // 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] | 6320 | // mangled name of the function that encloses the target region and BB is the |
| 6321 | // line number of the target region. |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 6322 | |
| 6323 | unsigned DeviceID; |
| 6324 | unsigned FileID; |
| 6325 | unsigned Line; |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 6326 | getTargetEntryUniqueInfo(CGM.getContext(), D.getBeginLoc(), DeviceID, FileID, |
Samuel Antao | 2de62b0 | 2016-02-13 23:35:10 +0000 | [diff] [blame] | 6327 | Line); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 6328 | SmallString<64> EntryFnName; |
| 6329 | { |
| 6330 | llvm::raw_svector_ostream OS(EntryFnName); |
Samuel Antao | 2de62b0 | 2016-02-13 23:35:10 +0000 | [diff] [blame] | 6331 | OS << "__omp_offloading" << llvm::format("_%x", DeviceID) |
| 6332 | << llvm::format("_%x_", FileID) << ParentName << "_l" << Line; |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 6333 | } |
| 6334 | |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 6335 | const CapturedStmt &CS = *D.getCapturedStmt(OMPD_target); |
Arpith Chacko Jacob | 5c309e4 | 2016-03-22 01:48:56 +0000 | [diff] [blame] | 6336 | |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 6337 | CodeGenFunction CGF(CGM, true); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 6338 | CGOpenMPTargetRegionInfo CGInfo(CS, CodeGen, EntryFnName); |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 6339 | CodeGenFunction::CGCapturedStmtRAII CapInfoRAII(CGF, &CGInfo); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 6340 | |
Samuel Antao | 6d00426 | 2016-06-16 18:39:34 +0000 | [diff] [blame] | 6341 | OutlinedFn = CGF.GenerateOpenMPCapturedStmtFunction(CS); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 6342 | |
| 6343 | // If this target outline function is not an offload entry, we don't need to |
| 6344 | // register it. |
| 6345 | if (!IsOffloadEntry) |
| 6346 | return; |
| 6347 | |
| 6348 | // The target region ID is used by the runtime library to identify the current |
| 6349 | // target region, so it only has to be unique and not necessarily point to |
| 6350 | // anything. It could be the pointer to the outlined function that implements |
| 6351 | // the target region, but we aren't using that so that the compiler doesn't |
| 6352 | // need to keep that, and could therefore inline the host function if proven |
| 6353 | // worthwhile during optimization. In the other hand, if emitting code for the |
| 6354 | // device, the ID has to be the function address so that it can retrieved from |
| 6355 | // the offloading entry and launched by the runtime library. We also mark the |
| 6356 | // outlined function to have external linkage in case we are emitting code for |
| 6357 | // the device, because these functions will be entry points to the device. |
| 6358 | |
| 6359 | if (CGM.getLangOpts().OpenMPIsDevice) { |
| 6360 | OutlinedFnID = llvm::ConstantExpr::getBitCast(OutlinedFn, CGM.Int8PtrTy); |
Alexey Bataev | 9a70017 | 2018-05-08 14:16:57 +0000 | [diff] [blame] | 6361 | OutlinedFn->setLinkage(llvm::GlobalValue::WeakAnyLinkage); |
Rafael Espindola | cbca487 | 2018-01-11 22:15:12 +0000 | [diff] [blame] | 6362 | OutlinedFn->setDSOLocal(false); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6363 | } else { |
Alexey Bataev | c15ea70 | 2018-05-09 18:02:37 +0000 | [diff] [blame] | 6364 | std::string Name = getName({EntryFnName, "region_id"}); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 6365 | OutlinedFnID = new llvm::GlobalVariable( |
| 6366 | CGM.getModule(), CGM.Int8Ty, /*isConstant=*/true, |
Alexey Bataev | 9a70017 | 2018-05-08 14:16:57 +0000 | [diff] [blame] | 6367 | llvm::GlobalValue::WeakAnyLinkage, |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 6368 | llvm::Constant::getNullValue(CGM.Int8Ty), Name); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6369 | } |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 6370 | |
| 6371 | // Register the information for the entry associated with this target region. |
| 6372 | OffloadEntriesInfoManager.registerTargetRegionEntryInfo( |
Samuel Antao | f83efdb | 2017-01-05 16:02:49 +0000 | [diff] [blame] | 6373 | DeviceID, FileID, ParentName, Line, OutlinedFn, OutlinedFnID, |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 6374 | OffloadEntriesInfoManagerTy::OMPTargetRegionEntryTargetRegion); |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 6375 | } |
| 6376 | |
Carlo Bertolli | 6eee906 | 2016-04-29 01:37:30 +0000 | [diff] [blame] | 6377 | /// discard all CompoundStmts intervening between two constructs |
| 6378 | static const Stmt *ignoreCompoundStmts(const Stmt *Body) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6379 | while (const auto *CS = dyn_cast_or_null<CompoundStmt>(Body)) |
Carlo Bertolli | 6eee906 | 2016-04-29 01:37:30 +0000 | [diff] [blame] | 6380 | Body = CS->body_front(); |
| 6381 | |
| 6382 | return Body; |
| 6383 | } |
| 6384 | |
Arpith Chacko Jacob | 33c849a | 2017-01-25 00:57:16 +0000 | [diff] [blame] | 6385 | /// Emit the number of teams for a target directive. Inspect the num_teams |
| 6386 | /// clause associated with a teams construct combined or closely nested |
| 6387 | /// with the target directive. |
| 6388 | /// |
| 6389 | /// Emit a team of size one for directives such as 'target parallel' that |
| 6390 | /// have no associated teams construct. |
| 6391 | /// |
| 6392 | /// Otherwise, return nullptr. |
Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 6393 | static llvm::Value * |
Arpith Chacko Jacob | 33c849a | 2017-01-25 00:57:16 +0000 | [diff] [blame] | 6394 | emitNumTeamsForTargetDirective(CGOpenMPRuntime &OMPRuntime, |
| 6395 | CodeGenFunction &CGF, |
| 6396 | const OMPExecutableDirective &D) { |
Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 6397 | assert(!CGF.getLangOpts().OpenMPIsDevice && "Clauses associated with the " |
| 6398 | "teams directive expected to be " |
| 6399 | "emitted only for the host!"); |
| 6400 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6401 | CGBuilderTy &Bld = CGF.Builder; |
Arpith Chacko Jacob | 99a1e0e | 2017-01-25 02:18:43 +0000 | [diff] [blame] | 6402 | |
| 6403 | // If the target directive is combined with a teams directive: |
| 6404 | // Return the value in the num_teams clause, if any. |
| 6405 | // Otherwise, return 0 to denote the runtime default. |
| 6406 | if (isOpenMPTeamsDirective(D.getDirectiveKind())) { |
| 6407 | if (const auto *NumTeamsClause = D.getSingleClause<OMPNumTeamsClause>()) { |
| 6408 | CodeGenFunction::RunCleanupsScope NumTeamsScope(CGF); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6409 | llvm::Value *NumTeams = CGF.EmitScalarExpr(NumTeamsClause->getNumTeams(), |
| 6410 | /*IgnoreResultAssign*/ true); |
Arpith Chacko Jacob | 99a1e0e | 2017-01-25 02:18:43 +0000 | [diff] [blame] | 6411 | return Bld.CreateIntCast(NumTeams, CGF.Int32Ty, |
| 6412 | /*IsSigned=*/true); |
| 6413 | } |
| 6414 | |
| 6415 | // The default value is 0. |
| 6416 | return Bld.getInt32(0); |
| 6417 | } |
| 6418 | |
Arpith Chacko Jacob | 33c849a | 2017-01-25 00:57:16 +0000 | [diff] [blame] | 6419 | // If the target directive is combined with a parallel directive but not a |
| 6420 | // teams directive, start one team. |
Arpith Chacko Jacob | 99a1e0e | 2017-01-25 02:18:43 +0000 | [diff] [blame] | 6421 | if (isOpenMPParallelDirective(D.getDirectiveKind())) |
| 6422 | return Bld.getInt32(1); |
Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 6423 | |
| 6424 | // If the current target region has a teams region enclosed, we need to get |
| 6425 | // the number of teams to pass to the runtime function call. This is done |
| 6426 | // by generating the expression in a inlined region. This is required because |
| 6427 | // the expression is captured in the enclosing target environment when the |
| 6428 | // teams directive is not combined with target. |
| 6429 | |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 6430 | const CapturedStmt &CS = *D.getCapturedStmt(OMPD_target); |
Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 6431 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6432 | if (const auto *TeamsDir = dyn_cast_or_null<OMPExecutableDirective>( |
Carlo Bertolli | 6eee906 | 2016-04-29 01:37:30 +0000 | [diff] [blame] | 6433 | ignoreCompoundStmts(CS.getCapturedStmt()))) { |
Alexey Bataev | 50a1c78 | 2017-12-01 21:31:08 +0000 | [diff] [blame] | 6434 | if (isOpenMPTeamsDirective(TeamsDir->getDirectiveKind())) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6435 | if (const auto *NTE = TeamsDir->getSingleClause<OMPNumTeamsClause>()) { |
Alexey Bataev | 50a1c78 | 2017-12-01 21:31:08 +0000 | [diff] [blame] | 6436 | CGOpenMPInnerExprInfo CGInfo(CGF, CS); |
| 6437 | CodeGenFunction::CGCapturedStmtRAII CapInfoRAII(CGF, &CGInfo); |
| 6438 | llvm::Value *NumTeams = CGF.EmitScalarExpr(NTE->getNumTeams()); |
| 6439 | return Bld.CreateIntCast(NumTeams, CGF.Int32Ty, |
| 6440 | /*IsSigned=*/true); |
| 6441 | } |
Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 6442 | |
Alexey Bataev | 50a1c78 | 2017-12-01 21:31:08 +0000 | [diff] [blame] | 6443 | // If we have an enclosed teams directive but no num_teams clause we use |
| 6444 | // the default value 0. |
| 6445 | return Bld.getInt32(0); |
| 6446 | } |
Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 6447 | } |
| 6448 | |
| 6449 | // No teams associated with the directive. |
| 6450 | return nullptr; |
| 6451 | } |
| 6452 | |
Arpith Chacko Jacob | 33c849a | 2017-01-25 00:57:16 +0000 | [diff] [blame] | 6453 | /// Emit the number of threads for a target directive. Inspect the |
| 6454 | /// thread_limit clause associated with a teams construct combined or closely |
| 6455 | /// nested with the target directive. |
| 6456 | /// |
| 6457 | /// Emit the num_threads clause for directives such as 'target parallel' that |
| 6458 | /// have no associated teams construct. |
| 6459 | /// |
| 6460 | /// Otherwise, return nullptr. |
Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 6461 | static llvm::Value * |
Arpith Chacko Jacob | 33c849a | 2017-01-25 00:57:16 +0000 | [diff] [blame] | 6462 | emitNumThreadsForTargetDirective(CGOpenMPRuntime &OMPRuntime, |
| 6463 | CodeGenFunction &CGF, |
| 6464 | const OMPExecutableDirective &D) { |
Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 6465 | assert(!CGF.getLangOpts().OpenMPIsDevice && "Clauses associated with the " |
| 6466 | "teams directive expected to be " |
| 6467 | "emitted only for the host!"); |
| 6468 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6469 | CGBuilderTy &Bld = CGF.Builder; |
Arpith Chacko Jacob | 33c849a | 2017-01-25 00:57:16 +0000 | [diff] [blame] | 6470 | |
| 6471 | // |
| 6472 | // If the target directive is combined with a teams directive: |
| 6473 | // Return the value in the thread_limit clause, if any. |
| 6474 | // |
| 6475 | // If the target directive is combined with a parallel directive: |
| 6476 | // Return the value in the num_threads clause, if any. |
| 6477 | // |
| 6478 | // If both clauses are set, select the minimum of the two. |
| 6479 | // |
| 6480 | // If neither teams or parallel combined directives set the number of threads |
| 6481 | // in a team, return 0 to denote the runtime default. |
| 6482 | // |
| 6483 | // If this is not a teams directive return nullptr. |
| 6484 | |
Arpith Chacko Jacob | 99a1e0e | 2017-01-25 02:18:43 +0000 | [diff] [blame] | 6485 | if (isOpenMPTeamsDirective(D.getDirectiveKind()) || |
| 6486 | isOpenMPParallelDirective(D.getDirectiveKind())) { |
Arpith Chacko Jacob | 33c849a | 2017-01-25 00:57:16 +0000 | [diff] [blame] | 6487 | llvm::Value *DefaultThreadLimitVal = Bld.getInt32(0); |
| 6488 | llvm::Value *NumThreadsVal = nullptr; |
Arpith Chacko Jacob | 99a1e0e | 2017-01-25 02:18:43 +0000 | [diff] [blame] | 6489 | llvm::Value *ThreadLimitVal = nullptr; |
| 6490 | |
| 6491 | if (const auto *ThreadLimitClause = |
| 6492 | D.getSingleClause<OMPThreadLimitClause>()) { |
| 6493 | CodeGenFunction::RunCleanupsScope ThreadLimitScope(CGF); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6494 | llvm::Value *ThreadLimit = |
| 6495 | CGF.EmitScalarExpr(ThreadLimitClause->getThreadLimit(), |
| 6496 | /*IgnoreResultAssign*/ true); |
Arpith Chacko Jacob | 99a1e0e | 2017-01-25 02:18:43 +0000 | [diff] [blame] | 6497 | ThreadLimitVal = Bld.CreateIntCast(ThreadLimit, CGF.Int32Ty, |
| 6498 | /*IsSigned=*/true); |
| 6499 | } |
Arpith Chacko Jacob | 33c849a | 2017-01-25 00:57:16 +0000 | [diff] [blame] | 6500 | |
| 6501 | if (const auto *NumThreadsClause = |
| 6502 | D.getSingleClause<OMPNumThreadsClause>()) { |
| 6503 | CodeGenFunction::RunCleanupsScope NumThreadsScope(CGF); |
| 6504 | llvm::Value *NumThreads = |
| 6505 | CGF.EmitScalarExpr(NumThreadsClause->getNumThreads(), |
| 6506 | /*IgnoreResultAssign*/ true); |
| 6507 | NumThreadsVal = |
| 6508 | Bld.CreateIntCast(NumThreads, CGF.Int32Ty, /*IsSigned=*/true); |
| 6509 | } |
| 6510 | |
Arpith Chacko Jacob | 99a1e0e | 2017-01-25 02:18:43 +0000 | [diff] [blame] | 6511 | // Select the lesser of thread_limit and num_threads. |
| 6512 | if (NumThreadsVal) |
| 6513 | ThreadLimitVal = ThreadLimitVal |
| 6514 | ? Bld.CreateSelect(Bld.CreateICmpSLT(NumThreadsVal, |
| 6515 | ThreadLimitVal), |
| 6516 | NumThreadsVal, ThreadLimitVal) |
| 6517 | : NumThreadsVal; |
Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 6518 | |
Arpith Chacko Jacob | 99a1e0e | 2017-01-25 02:18:43 +0000 | [diff] [blame] | 6519 | // Set default value passed to the runtime if either teams or a target |
| 6520 | // parallel type directive is found but no clause is specified. |
| 6521 | if (!ThreadLimitVal) |
| 6522 | ThreadLimitVal = DefaultThreadLimitVal; |
| 6523 | |
| 6524 | return ThreadLimitVal; |
| 6525 | } |
Arpith Chacko Jacob | 86f9e46 | 2017-01-25 01:45:59 +0000 | [diff] [blame] | 6526 | |
Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 6527 | // If the current target region has a teams region enclosed, we need to get |
| 6528 | // the thread limit to pass to the runtime function call. This is done |
| 6529 | // by generating the expression in a inlined region. This is required because |
| 6530 | // the expression is captured in the enclosing target environment when the |
| 6531 | // teams directive is not combined with target. |
| 6532 | |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 6533 | const CapturedStmt &CS = *D.getCapturedStmt(OMPD_target); |
Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 6534 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6535 | if (const auto *TeamsDir = dyn_cast_or_null<OMPExecutableDirective>( |
Carlo Bertolli | 6eee906 | 2016-04-29 01:37:30 +0000 | [diff] [blame] | 6536 | ignoreCompoundStmts(CS.getCapturedStmt()))) { |
Alexey Bataev | 50a1c78 | 2017-12-01 21:31:08 +0000 | [diff] [blame] | 6537 | if (isOpenMPTeamsDirective(TeamsDir->getDirectiveKind())) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6538 | if (const auto *TLE = TeamsDir->getSingleClause<OMPThreadLimitClause>()) { |
Alexey Bataev | 50a1c78 | 2017-12-01 21:31:08 +0000 | [diff] [blame] | 6539 | CGOpenMPInnerExprInfo CGInfo(CGF, CS); |
| 6540 | CodeGenFunction::CGCapturedStmtRAII CapInfoRAII(CGF, &CGInfo); |
| 6541 | llvm::Value *ThreadLimit = CGF.EmitScalarExpr(TLE->getThreadLimit()); |
| 6542 | return CGF.Builder.CreateIntCast(ThreadLimit, CGF.Int32Ty, |
| 6543 | /*IsSigned=*/true); |
| 6544 | } |
Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 6545 | |
Alexey Bataev | 50a1c78 | 2017-12-01 21:31:08 +0000 | [diff] [blame] | 6546 | // If we have an enclosed teams directive but no thread_limit clause we |
| 6547 | // use the default value 0. |
| 6548 | return CGF.Builder.getInt32(0); |
| 6549 | } |
Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 6550 | } |
| 6551 | |
| 6552 | // No teams associated with the directive. |
| 6553 | return nullptr; |
| 6554 | } |
| 6555 | |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6556 | namespace { |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 6557 | LLVM_ENABLE_BITMASK_ENUMS_IN_NAMESPACE(); |
| 6558 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 6559 | // Utility to handle information from clauses associated with a given |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6560 | // construct that use mappable expressions (e.g. 'map' clause, 'to' clause). |
| 6561 | // It provides a convenient interface to obtain the information and generate |
| 6562 | // code for that information. |
| 6563 | class MappableExprsHandler { |
| 6564 | public: |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 6565 | /// Values for bit flags used to specify the mapping type for |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6566 | /// offloading. |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 6567 | enum OpenMPOffloadMappingFlags : uint64_t { |
| 6568 | /// No flags |
| 6569 | OMP_MAP_NONE = 0x0, |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 6570 | /// Allocate memory on the device and move data from host to device. |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6571 | OMP_MAP_TO = 0x01, |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 6572 | /// Allocate memory on the device and move data from device to host. |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6573 | OMP_MAP_FROM = 0x02, |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 6574 | /// Always perform the requested mapping action on the element, even |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6575 | /// if it was already mapped before. |
| 6576 | OMP_MAP_ALWAYS = 0x04, |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 6577 | /// Delete the element from the device environment, ignoring the |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6578 | /// current reference count associated with the element. |
Samuel Antao | 6782e94 | 2016-05-26 16:48:10 +0000 | [diff] [blame] | 6579 | OMP_MAP_DELETE = 0x08, |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 6580 | /// The element being mapped is a pointer-pointee pair; both the |
George Rokos | 065755d | 2017-11-07 18:27:04 +0000 | [diff] [blame] | 6581 | /// pointer and the pointee should be mapped. |
| 6582 | OMP_MAP_PTR_AND_OBJ = 0x10, |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 6583 | /// This flags signals that the base address of an entry should be |
George Rokos | 065755d | 2017-11-07 18:27:04 +0000 | [diff] [blame] | 6584 | /// passed to the target kernel as an argument. |
| 6585 | OMP_MAP_TARGET_PARAM = 0x20, |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 6586 | /// Signal that the runtime library has to return the device pointer |
George Rokos | 065755d | 2017-11-07 18:27:04 +0000 | [diff] [blame] | 6587 | /// in the current position for the data being mapped. Used when we have the |
| 6588 | /// use_device_ptr clause. |
| 6589 | OMP_MAP_RETURN_PARAM = 0x40, |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 6590 | /// This flag signals that the reference being passed is a pointer to |
Samuel Antao | d486f84 | 2016-05-26 16:53:38 +0000 | [diff] [blame] | 6591 | /// private data. |
George Rokos | 065755d | 2017-11-07 18:27:04 +0000 | [diff] [blame] | 6592 | OMP_MAP_PRIVATE = 0x80, |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 6593 | /// Pass the element to the device by value. |
George Rokos | 065755d | 2017-11-07 18:27:04 +0000 | [diff] [blame] | 6594 | OMP_MAP_LITERAL = 0x100, |
Alexey Bataev | f47c4b4 | 2017-09-26 13:47:31 +0000 | [diff] [blame] | 6595 | /// Implicit map |
| 6596 | OMP_MAP_IMPLICIT = 0x200, |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 6597 | /// The 16 MSBs of the flags indicate whether the entry is member of some |
| 6598 | /// struct/class. |
| 6599 | OMP_MAP_MEMBER_OF = 0xffff000000000000, |
| 6600 | LLVM_MARK_AS_BITMASK_ENUM(/* LargestFlag = */ OMP_MAP_MEMBER_OF), |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6601 | }; |
| 6602 | |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 6603 | /// Class that associates information with a base pointer to be passed to the |
| 6604 | /// runtime library. |
| 6605 | class BasePointerInfo { |
| 6606 | /// The base pointer. |
| 6607 | llvm::Value *Ptr = nullptr; |
| 6608 | /// The base declaration that refers to this device pointer, or null if |
| 6609 | /// there is none. |
| 6610 | const ValueDecl *DevPtrDecl = nullptr; |
| 6611 | |
| 6612 | public: |
| 6613 | BasePointerInfo(llvm::Value *Ptr, const ValueDecl *DevPtrDecl = nullptr) |
| 6614 | : Ptr(Ptr), DevPtrDecl(DevPtrDecl) {} |
| 6615 | llvm::Value *operator*() const { return Ptr; } |
| 6616 | const ValueDecl *getDevicePtrDecl() const { return DevPtrDecl; } |
| 6617 | void setDevicePtrDecl(const ValueDecl *D) { DevPtrDecl = D; } |
| 6618 | }; |
| 6619 | |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 6620 | using MapBaseValuesArrayTy = SmallVector<BasePointerInfo, 4>; |
| 6621 | using MapValuesArrayTy = SmallVector<llvm::Value *, 4>; |
| 6622 | using MapFlagsArrayTy = SmallVector<OpenMPOffloadMappingFlags, 4>; |
| 6623 | |
| 6624 | /// Map between a struct and the its lowest & highest elements which have been |
| 6625 | /// mapped. |
| 6626 | /// [ValueDecl *] --> {LE(FieldIndex, Pointer), |
| 6627 | /// HE(FieldIndex, Pointer)} |
| 6628 | struct StructRangeInfoTy { |
| 6629 | std::pair<unsigned /*FieldIndex*/, Address /*Pointer*/> LowestElem = { |
| 6630 | 0, Address::invalid()}; |
| 6631 | std::pair<unsigned /*FieldIndex*/, Address /*Pointer*/> HighestElem = { |
| 6632 | 0, Address::invalid()}; |
| 6633 | Address Base = Address::invalid(); |
| 6634 | }; |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6635 | |
| 6636 | private: |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 6637 | /// Kind that defines how a device pointer has to be returned. |
| 6638 | struct MapInfo { |
| 6639 | OMPClauseMappableExprCommon::MappableExprComponentListRef Components; |
| 6640 | OpenMPMapClauseKind MapType = OMPC_MAP_unknown; |
| 6641 | OpenMPMapClauseKind MapTypeModifier = OMPC_MAP_unknown; |
| 6642 | bool ReturnDevicePointer = false; |
| 6643 | bool IsImplicit = false; |
| 6644 | |
| 6645 | MapInfo() = default; |
| 6646 | MapInfo( |
| 6647 | OMPClauseMappableExprCommon::MappableExprComponentListRef Components, |
| 6648 | OpenMPMapClauseKind MapType, OpenMPMapClauseKind MapTypeModifier, |
| 6649 | bool ReturnDevicePointer, bool IsImplicit) |
| 6650 | : Components(Components), MapType(MapType), |
| 6651 | MapTypeModifier(MapTypeModifier), |
| 6652 | ReturnDevicePointer(ReturnDevicePointer), IsImplicit(IsImplicit) {} |
| 6653 | }; |
| 6654 | |
| 6655 | /// If use_device_ptr is used on a pointer which is a struct member and there |
| 6656 | /// is no map information about it, then emission of that entry is deferred |
| 6657 | /// until the whole struct has been processed. |
| 6658 | struct DeferredDevicePtrEntryTy { |
| 6659 | const Expr *IE = nullptr; |
| 6660 | const ValueDecl *VD = nullptr; |
| 6661 | |
| 6662 | DeferredDevicePtrEntryTy(const Expr *IE, const ValueDecl *VD) |
| 6663 | : IE(IE), VD(VD) {} |
| 6664 | }; |
| 6665 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 6666 | /// Directive from where the map clauses were extracted. |
Samuel Antao | 44bcdb3 | 2016-07-28 15:31:29 +0000 | [diff] [blame] | 6667 | const OMPExecutableDirective &CurDir; |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6668 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 6669 | /// Function the directive is being generated for. |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6670 | CodeGenFunction &CGF; |
| 6671 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 6672 | /// Set of all first private variables in the current directive. |
Samuel Antao | d486f84 | 2016-05-26 16:53:38 +0000 | [diff] [blame] | 6673 | llvm::SmallPtrSet<const VarDecl *, 8> FirstPrivateDecls; |
| 6674 | |
Samuel Antao | 6890b09 | 2016-07-28 14:25:09 +0000 | [diff] [blame] | 6675 | /// Map between device pointer declarations and their expression components. |
| 6676 | /// The key value for declarations in 'this' is null. |
| 6677 | llvm::DenseMap< |
| 6678 | const ValueDecl *, |
| 6679 | SmallVector<OMPClauseMappableExprCommon::MappableExprComponentListRef, 4>> |
| 6680 | DevPointersMap; |
| 6681 | |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6682 | llvm::Value *getExprTypeSize(const Expr *E) const { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6683 | QualType ExprTy = E->getType().getCanonicalType(); |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6684 | |
| 6685 | // Reference types are ignored for mapping purposes. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6686 | if (const auto *RefTy = ExprTy->getAs<ReferenceType>()) |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6687 | ExprTy = RefTy->getPointeeType().getCanonicalType(); |
| 6688 | |
| 6689 | // Given that an array section is considered a built-in type, we need to |
| 6690 | // do the calculation based on the length of the section instead of relying |
| 6691 | // on CGF.getTypeSize(E->getType()). |
| 6692 | if (const auto *OAE = dyn_cast<OMPArraySectionExpr>(E)) { |
| 6693 | QualType BaseTy = OMPArraySectionExpr::getBaseOriginalType( |
| 6694 | OAE->getBase()->IgnoreParenImpCasts()) |
| 6695 | .getCanonicalType(); |
| 6696 | |
| 6697 | // If there is no length associated with the expression, that means we |
| 6698 | // are using the whole length of the base. |
| 6699 | if (!OAE->getLength() && OAE->getColonLoc().isValid()) |
| 6700 | return CGF.getTypeSize(BaseTy); |
| 6701 | |
| 6702 | llvm::Value *ElemSize; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6703 | if (const auto *PTy = BaseTy->getAs<PointerType>()) { |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6704 | ElemSize = CGF.getTypeSize(PTy->getPointeeType().getCanonicalType()); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6705 | } else { |
| 6706 | const auto *ATy = cast<ArrayType>(BaseTy.getTypePtr()); |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6707 | assert(ATy && "Expecting array type if not a pointer type."); |
| 6708 | ElemSize = CGF.getTypeSize(ATy->getElementType().getCanonicalType()); |
| 6709 | } |
| 6710 | |
| 6711 | // If we don't have a length at this point, that is because we have an |
| 6712 | // array section with a single element. |
| 6713 | if (!OAE->getLength()) |
| 6714 | return ElemSize; |
| 6715 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6716 | llvm::Value *LengthVal = CGF.EmitScalarExpr(OAE->getLength()); |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6717 | LengthVal = |
| 6718 | CGF.Builder.CreateIntCast(LengthVal, CGF.SizeTy, /*isSigned=*/false); |
| 6719 | return CGF.Builder.CreateNUWMul(LengthVal, ElemSize); |
| 6720 | } |
| 6721 | return CGF.getTypeSize(ExprTy); |
| 6722 | } |
| 6723 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 6724 | /// Return the corresponding bits for a given map clause modifier. Add |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6725 | /// 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] | 6726 | /// map as the first one of a series of maps that relate to the same map |
| 6727 | /// expression. |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 6728 | OpenMPOffloadMappingFlags getMapTypeBits(OpenMPMapClauseKind MapType, |
| 6729 | OpenMPMapClauseKind MapTypeModifier, |
| 6730 | bool IsImplicit, bool AddPtrFlag, |
| 6731 | bool AddIsTargetParamFlag) const { |
| 6732 | OpenMPOffloadMappingFlags Bits = |
| 6733 | IsImplicit ? OMP_MAP_IMPLICIT : OMP_MAP_NONE; |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6734 | switch (MapType) { |
| 6735 | case OMPC_MAP_alloc: |
Samuel Antao | 6782e94 | 2016-05-26 16:48:10 +0000 | [diff] [blame] | 6736 | case OMPC_MAP_release: |
| 6737 | // alloc and release is the default behavior in the runtime library, i.e. |
| 6738 | // if we don't pass any bits alloc/release that is what the runtime is |
| 6739 | // going to do. Therefore, we don't need to signal anything for these two |
| 6740 | // type modifiers. |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6741 | break; |
| 6742 | case OMPC_MAP_to: |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 6743 | Bits |= OMP_MAP_TO; |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6744 | break; |
| 6745 | case OMPC_MAP_from: |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 6746 | Bits |= OMP_MAP_FROM; |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6747 | break; |
| 6748 | case OMPC_MAP_tofrom: |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 6749 | Bits |= OMP_MAP_TO | OMP_MAP_FROM; |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6750 | break; |
| 6751 | case OMPC_MAP_delete: |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 6752 | Bits |= OMP_MAP_DELETE; |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6753 | break; |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 6754 | case OMPC_MAP_always: |
| 6755 | case OMPC_MAP_unknown: |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6756 | llvm_unreachable("Unexpected map type!"); |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6757 | } |
| 6758 | if (AddPtrFlag) |
George Rokos | 065755d | 2017-11-07 18:27:04 +0000 | [diff] [blame] | 6759 | Bits |= OMP_MAP_PTR_AND_OBJ; |
| 6760 | if (AddIsTargetParamFlag) |
| 6761 | Bits |= OMP_MAP_TARGET_PARAM; |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6762 | if (MapTypeModifier == OMPC_MAP_always) |
| 6763 | Bits |= OMP_MAP_ALWAYS; |
| 6764 | return Bits; |
| 6765 | } |
| 6766 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 6767 | /// Return true if the provided expression is a final array section. A |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6768 | /// final array section, is one whose length can't be proved to be one. |
| 6769 | bool isFinalArraySectionExpression(const Expr *E) const { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6770 | const auto *OASE = dyn_cast<OMPArraySectionExpr>(E); |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6771 | |
| 6772 | // It is not an array section and therefore not a unity-size one. |
| 6773 | if (!OASE) |
| 6774 | return false; |
| 6775 | |
| 6776 | // An array section with no colon always refer to a single element. |
| 6777 | if (OASE->getColonLoc().isInvalid()) |
| 6778 | return false; |
| 6779 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6780 | const Expr *Length = OASE->getLength(); |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6781 | |
| 6782 | // If we don't have a length we have to check if the array has size 1 |
| 6783 | // for this dimension. Also, we should always expect a length if the |
| 6784 | // base type is pointer. |
| 6785 | if (!Length) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6786 | QualType BaseQTy = OMPArraySectionExpr::getBaseOriginalType( |
| 6787 | OASE->getBase()->IgnoreParenImpCasts()) |
| 6788 | .getCanonicalType(); |
| 6789 | if (const auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr())) |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6790 | return ATy->getSize().getSExtValue() != 1; |
| 6791 | // If we don't have a constant dimension length, we have to consider |
| 6792 | // the current section as having any size, so it is not necessarily |
| 6793 | // unitary. If it happen to be unity size, that's user fault. |
| 6794 | return true; |
| 6795 | } |
| 6796 | |
| 6797 | // Check if the length evaluates to 1. |
Nico Weber | 9f0246d | 2018-11-21 12:47:43 +0000 | [diff] [blame] | 6798 | llvm::APSInt ConstLength; |
| 6799 | if (!Length->EvaluateAsInt(ConstLength, CGF.getContext())) |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6800 | return true; // Can have more that size 1. |
| 6801 | |
| 6802 | return ConstLength.getSExtValue() != 1; |
| 6803 | } |
| 6804 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 6805 | /// Generate the base pointers, section pointers, sizes and map type |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6806 | /// bits for the provided map type, map modifier, and expression components. |
| 6807 | /// \a IsFirstComponent should be set to true if the provided set of |
| 6808 | /// components is the first associated with a capture. |
| 6809 | void generateInfoForComponentList( |
| 6810 | OpenMPMapClauseKind MapType, OpenMPMapClauseKind MapTypeModifier, |
| 6811 | OMPClauseMappableExprCommon::MappableExprComponentListRef Components, |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 6812 | MapBaseValuesArrayTy &BasePointers, MapValuesArrayTy &Pointers, |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6813 | MapValuesArrayTy &Sizes, MapFlagsArrayTy &Types, |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 6814 | StructRangeInfoTy &PartialStruct, bool IsFirstComponentList, |
Alexey Bataev | e82445f | 2018-09-20 13:54:02 +0000 | [diff] [blame] | 6815 | bool IsImplicit, |
| 6816 | ArrayRef<OMPClauseMappableExprCommon::MappableExprComponentListRef> |
| 6817 | OverlappedElements = llvm::None) const { |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6818 | // 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] | 6819 | // types below. The generated information is expressed in this order: |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6820 | // base pointer, section pointer, size, flags |
| 6821 | // (to add to the ones that come from the map type and modifier). |
| 6822 | // |
| 6823 | // double d; |
| 6824 | // int i[100]; |
| 6825 | // float *p; |
| 6826 | // |
| 6827 | // struct S1 { |
| 6828 | // int i; |
| 6829 | // float f[50]; |
| 6830 | // } |
| 6831 | // struct S2 { |
| 6832 | // int i; |
| 6833 | // float f[50]; |
| 6834 | // S1 s; |
| 6835 | // double *p; |
| 6836 | // struct S2 *ps; |
| 6837 | // } |
| 6838 | // S2 s; |
| 6839 | // S2 *ps; |
| 6840 | // |
| 6841 | // map(d) |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 6842 | // &d, &d, sizeof(double), TARGET_PARAM | TO | FROM |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6843 | // |
| 6844 | // map(i) |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 6845 | // &i, &i, 100*sizeof(int), TARGET_PARAM | TO | FROM |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6846 | // |
| 6847 | // map(i[1:23]) |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 6848 | // &i(=&i[0]), &i[1], 23*sizeof(int), TARGET_PARAM | TO | FROM |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6849 | // |
| 6850 | // map(p) |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 6851 | // &p, &p, sizeof(float*), TARGET_PARAM | TO | FROM |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6852 | // |
| 6853 | // map(p[1:24]) |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 6854 | // p, &p[1], 24*sizeof(float), TARGET_PARAM | TO | FROM |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6855 | // |
| 6856 | // map(s) |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 6857 | // &s, &s, sizeof(S2), TARGET_PARAM | TO | FROM |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6858 | // |
| 6859 | // map(s.i) |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 6860 | // &s, &(s.i), sizeof(int), TARGET_PARAM | TO | FROM |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6861 | // |
| 6862 | // map(s.s.f) |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 6863 | // &s, &(s.s.f[0]), 50*sizeof(float), TARGET_PARAM | TO | FROM |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6864 | // |
| 6865 | // map(s.p) |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 6866 | // &s, &(s.p), sizeof(double*), TARGET_PARAM | TO | FROM |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6867 | // |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 6868 | // map(to: s.p[:22]) |
| 6869 | // &s, &(s.p), sizeof(double*), TARGET_PARAM (*) |
| 6870 | // &s, &(s.p), sizeof(double*), MEMBER_OF(1) (**) |
| 6871 | // &(s.p), &(s.p[0]), 22*sizeof(double), |
| 6872 | // MEMBER_OF(1) | PTR_AND_OBJ | TO (***) |
| 6873 | // (*) alloc space for struct members, only this is a target parameter |
| 6874 | // (**) map the pointer (nothing to be mapped in this example) (the compiler |
| 6875 | // optimizes this entry out, same in the examples below) |
| 6876 | // (***) map the pointee (map: to) |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6877 | // |
| 6878 | // map(s.ps) |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 6879 | // &s, &(s.ps), sizeof(S2*), TARGET_PARAM | TO | FROM |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6880 | // |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 6881 | // map(from: s.ps->s.i) |
| 6882 | // &s, &(s.ps), sizeof(S2*), TARGET_PARAM |
| 6883 | // &s, &(s.ps), sizeof(S2*), MEMBER_OF(1) |
| 6884 | // &(s.ps), &(s.ps->s.i), sizeof(int), MEMBER_OF(1) | PTR_AND_OBJ | FROM |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6885 | // |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 6886 | // map(to: s.ps->ps) |
| 6887 | // &s, &(s.ps), sizeof(S2*), TARGET_PARAM |
| 6888 | // &s, &(s.ps), sizeof(S2*), MEMBER_OF(1) |
| 6889 | // &(s.ps), &(s.ps->ps), sizeof(S2*), MEMBER_OF(1) | PTR_AND_OBJ | TO |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6890 | // |
| 6891 | // map(s.ps->ps->ps) |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 6892 | // &s, &(s.ps), sizeof(S2*), TARGET_PARAM |
| 6893 | // &s, &(s.ps), sizeof(S2*), MEMBER_OF(1) |
| 6894 | // &(s.ps), &(s.ps->ps), sizeof(S2*), MEMBER_OF(1) | PTR_AND_OBJ |
| 6895 | // &(s.ps->ps), &(s.ps->ps->ps), sizeof(S2*), PTR_AND_OBJ | TO | FROM |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6896 | // |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 6897 | // map(to: s.ps->ps->s.f[:22]) |
| 6898 | // &s, &(s.ps), sizeof(S2*), TARGET_PARAM |
| 6899 | // &s, &(s.ps), sizeof(S2*), MEMBER_OF(1) |
| 6900 | // &(s.ps), &(s.ps->ps), sizeof(S2*), MEMBER_OF(1) | PTR_AND_OBJ |
| 6901 | // &(s.ps->ps), &(s.ps->ps->s.f[0]), 22*sizeof(float), PTR_AND_OBJ | TO |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6902 | // |
| 6903 | // map(ps) |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 6904 | // &ps, &ps, sizeof(S2*), TARGET_PARAM | TO | FROM |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6905 | // |
| 6906 | // map(ps->i) |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 6907 | // ps, &(ps->i), sizeof(int), TARGET_PARAM | TO | FROM |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6908 | // |
| 6909 | // map(ps->s.f) |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 6910 | // ps, &(ps->s.f[0]), 50*sizeof(float), TARGET_PARAM | TO | FROM |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6911 | // |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 6912 | // map(from: ps->p) |
| 6913 | // ps, &(ps->p), sizeof(double*), TARGET_PARAM | FROM |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6914 | // |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 6915 | // map(to: ps->p[:22]) |
| 6916 | // ps, &(ps->p), sizeof(double*), TARGET_PARAM |
| 6917 | // ps, &(ps->p), sizeof(double*), MEMBER_OF(1) |
| 6918 | // &(ps->p), &(ps->p[0]), 22*sizeof(double), MEMBER_OF(1) | PTR_AND_OBJ | TO |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6919 | // |
| 6920 | // map(ps->ps) |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 6921 | // ps, &(ps->ps), sizeof(S2*), TARGET_PARAM | TO | FROM |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6922 | // |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 6923 | // map(from: ps->ps->s.i) |
| 6924 | // ps, &(ps->ps), sizeof(S2*), TARGET_PARAM |
| 6925 | // ps, &(ps->ps), sizeof(S2*), MEMBER_OF(1) |
| 6926 | // &(ps->ps), &(ps->ps->s.i), sizeof(int), MEMBER_OF(1) | PTR_AND_OBJ | FROM |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6927 | // |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 6928 | // map(from: ps->ps->ps) |
| 6929 | // ps, &(ps->ps), sizeof(S2*), TARGET_PARAM |
| 6930 | // ps, &(ps->ps), sizeof(S2*), MEMBER_OF(1) |
| 6931 | // &(ps->ps), &(ps->ps->ps), sizeof(S2*), MEMBER_OF(1) | PTR_AND_OBJ | FROM |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6932 | // |
| 6933 | // map(ps->ps->ps->ps) |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 6934 | // ps, &(ps->ps), sizeof(S2*), TARGET_PARAM |
| 6935 | // ps, &(ps->ps), sizeof(S2*), MEMBER_OF(1) |
| 6936 | // &(ps->ps), &(ps->ps->ps), sizeof(S2*), MEMBER_OF(1) | PTR_AND_OBJ |
| 6937 | // &(ps->ps->ps), &(ps->ps->ps->ps), sizeof(S2*), PTR_AND_OBJ | TO | FROM |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6938 | // |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 6939 | // map(to: ps->ps->ps->s.f[:22]) |
| 6940 | // ps, &(ps->ps), sizeof(S2*), TARGET_PARAM |
| 6941 | // ps, &(ps->ps), sizeof(S2*), MEMBER_OF(1) |
| 6942 | // &(ps->ps), &(ps->ps->ps), sizeof(S2*), MEMBER_OF(1) | PTR_AND_OBJ |
| 6943 | // &(ps->ps->ps), &(ps->ps->ps->s.f[0]), 22*sizeof(float), PTR_AND_OBJ | TO |
| 6944 | // |
| 6945 | // map(to: s.f[:22]) map(from: s.p[:33]) |
| 6946 | // &s, &(s.f[0]), 50*sizeof(float) + sizeof(struct S1) + |
| 6947 | // sizeof(double*) (**), TARGET_PARAM |
| 6948 | // &s, &(s.f[0]), 22*sizeof(float), MEMBER_OF(1) | TO |
| 6949 | // &s, &(s.p), sizeof(double*), MEMBER_OF(1) |
| 6950 | // &(s.p), &(s.p[0]), 33*sizeof(double), MEMBER_OF(1) | PTR_AND_OBJ | FROM |
| 6951 | // (*) allocate contiguous space needed to fit all mapped members even if |
| 6952 | // we allocate space for members not mapped (in this example, |
| 6953 | // s.f[22..49] and s.s are not mapped, yet we must allocate space for |
| 6954 | // them as well because they fall between &s.f[0] and &s.p) |
| 6955 | // |
| 6956 | // map(from: s.f[:22]) map(to: ps->p[:33]) |
| 6957 | // &s, &(s.f[0]), 22*sizeof(float), TARGET_PARAM | FROM |
| 6958 | // ps, &(ps->p), sizeof(S2*), TARGET_PARAM |
| 6959 | // ps, &(ps->p), sizeof(double*), MEMBER_OF(2) (*) |
| 6960 | // &(ps->p), &(ps->p[0]), 33*sizeof(double), MEMBER_OF(2) | PTR_AND_OBJ | TO |
| 6961 | // (*) the struct this entry pertains to is the 2nd element in the list of |
| 6962 | // arguments, hence MEMBER_OF(2) |
| 6963 | // |
| 6964 | // map(from: s.f[:22], s.s) map(to: ps->p[:33]) |
| 6965 | // &s, &(s.f[0]), 50*sizeof(float) + sizeof(struct S1), TARGET_PARAM |
| 6966 | // &s, &(s.f[0]), 22*sizeof(float), MEMBER_OF(1) | FROM |
| 6967 | // &s, &(s.s), sizeof(struct S1), MEMBER_OF(1) | FROM |
| 6968 | // ps, &(ps->p), sizeof(S2*), TARGET_PARAM |
| 6969 | // ps, &(ps->p), sizeof(double*), MEMBER_OF(4) (*) |
| 6970 | // &(ps->p), &(ps->p[0]), 33*sizeof(double), MEMBER_OF(4) | PTR_AND_OBJ | TO |
| 6971 | // (*) the struct this entry pertains to is the 4th element in the list |
| 6972 | // of arguments, hence MEMBER_OF(4) |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6973 | |
| 6974 | // Track if the map information being generated is the first for a capture. |
| 6975 | bool IsCaptureFirstInfo = IsFirstComponentList; |
Alexey Bataev | 92327c5 | 2018-03-26 16:40:55 +0000 | [diff] [blame] | 6976 | bool IsLink = false; // Is this variable a "declare target link"? |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6977 | |
| 6978 | // Scan the components from the base to the complete expression. |
| 6979 | auto CI = Components.rbegin(); |
| 6980 | auto CE = Components.rend(); |
| 6981 | auto I = CI; |
| 6982 | |
| 6983 | // Track if the map information being generated is the first for a list of |
| 6984 | // components. |
| 6985 | bool IsExpressionFirstInfo = true; |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 6986 | Address BP = Address::invalid(); |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6987 | |
Erich Keane | e69755a | 2018-07-19 17:19:16 +0000 | [diff] [blame] | 6988 | if (isa<MemberExpr>(I->getAssociatedExpression())) { |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6989 | // The base is the 'this' pointer. The content of the pointer is going |
| 6990 | // to be the base of the field being mapped. |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 6991 | BP = CGF.LoadCXXThisAddress(); |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6992 | } else { |
| 6993 | // The base is the reference to the variable. |
| 6994 | // BP = &Var. |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 6995 | BP = CGF.EmitOMPSharedLValue(I->getAssociatedExpression()).getAddress(); |
Alexey Bataev | 92327c5 | 2018-03-26 16:40:55 +0000 | [diff] [blame] | 6996 | if (const auto *VD = |
| 6997 | dyn_cast_or_null<VarDecl>(I->getAssociatedDeclaration())) { |
| 6998 | if (llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res = |
Alexey Bataev | 97b7221 | 2018-08-14 18:31:20 +0000 | [diff] [blame] | 6999 | OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD)) |
Alexey Bataev | 2c1dffe | 2018-04-16 20:34:41 +0000 | [diff] [blame] | 7000 | if (*Res == OMPDeclareTargetDeclAttr::MT_Link) { |
| 7001 | IsLink = true; |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7002 | BP = CGF.CGM.getOpenMPRuntime().getAddrOfDeclareTargetLink(VD); |
Alexey Bataev | 2c1dffe | 2018-04-16 20:34:41 +0000 | [diff] [blame] | 7003 | } |
Alexey Bataev | 92327c5 | 2018-03-26 16:40:55 +0000 | [diff] [blame] | 7004 | } |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7005 | |
| 7006 | // 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] | 7007 | // 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] | 7008 | // reference. References are ignored for mapping purposes. |
| 7009 | QualType Ty = |
| 7010 | I->getAssociatedDeclaration()->getType().getNonReferenceType(); |
| 7011 | if (Ty->isAnyPointerType() && std::next(I) != CE) { |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7012 | BP = CGF.EmitLoadOfPointer(BP, Ty->castAs<PointerType>()); |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7013 | |
| 7014 | // We do not need to generate individual map information for the |
| 7015 | // pointer, it can be associated with the combined storage. |
| 7016 | ++I; |
| 7017 | } |
| 7018 | } |
| 7019 | |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7020 | // Track whether a component of the list should be marked as MEMBER_OF some |
| 7021 | // combined entry (for partial structs). Only the first PTR_AND_OBJ entry |
| 7022 | // in a component list should be marked as MEMBER_OF, all subsequent entries |
| 7023 | // do not belong to the base struct. E.g. |
| 7024 | // struct S2 s; |
| 7025 | // s.ps->ps->ps->f[:] |
| 7026 | // (1) (2) (3) (4) |
| 7027 | // ps(1) is a member pointer, ps(2) is a pointee of ps(1), so it is a |
| 7028 | // PTR_AND_OBJ entry; the PTR is ps(1), so MEMBER_OF the base struct. ps(3) |
| 7029 | // is the pointee of ps(2) which is not member of struct s, so it should not |
| 7030 | // be marked as such (it is still PTR_AND_OBJ). |
| 7031 | // The variable is initialized to false so that PTR_AND_OBJ entries which |
| 7032 | // are not struct members are not considered (e.g. array of pointers to |
| 7033 | // data). |
| 7034 | bool ShouldBeMemberOf = false; |
| 7035 | |
| 7036 | // Variable keeping track of whether or not we have encountered a component |
| 7037 | // in the component list which is a member expression. Useful when we have a |
| 7038 | // pointer or a final array section, in which case it is the previous |
| 7039 | // component in the list which tells us whether we have a member expression. |
| 7040 | // E.g. X.f[:] |
| 7041 | // While processing the final array section "[:]" it is "f" which tells us |
| 7042 | // whether we are dealing with a member of a declared struct. |
| 7043 | const MemberExpr *EncounteredME = nullptr; |
| 7044 | |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7045 | for (; I != CE; ++I) { |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7046 | // If the current component is member of a struct (parent struct) mark it. |
| 7047 | if (!EncounteredME) { |
| 7048 | EncounteredME = dyn_cast<MemberExpr>(I->getAssociatedExpression()); |
| 7049 | // If we encounter a PTR_AND_OBJ entry from now on it should be marked |
| 7050 | // as MEMBER_OF the parent struct. |
| 7051 | if (EncounteredME) |
| 7052 | ShouldBeMemberOf = true; |
| 7053 | } |
| 7054 | |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7055 | auto Next = std::next(I); |
| 7056 | |
| 7057 | // We need to generate the addresses and sizes if this is the last |
| 7058 | // component, if the component is a pointer or if it is an array section |
| 7059 | // whose length can't be proved to be one. If this is a pointer, it |
| 7060 | // becomes the base address for the following components. |
| 7061 | |
| 7062 | // A final array section, is one whose length can't be proved to be one. |
| 7063 | bool IsFinalArraySection = |
| 7064 | isFinalArraySectionExpression(I->getAssociatedExpression()); |
| 7065 | |
| 7066 | // Get information on whether the element is a pointer. Have to do a |
| 7067 | // special treatment for array sections given that they are built-in |
| 7068 | // types. |
| 7069 | const auto *OASE = |
| 7070 | dyn_cast<OMPArraySectionExpr>(I->getAssociatedExpression()); |
| 7071 | bool IsPointer = |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7072 | (OASE && OMPArraySectionExpr::getBaseOriginalType(OASE) |
| 7073 | .getCanonicalType() |
| 7074 | ->isAnyPointerType()) || |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7075 | I->getAssociatedExpression()->getType()->isAnyPointerType(); |
| 7076 | |
| 7077 | if (Next == CE || IsPointer || IsFinalArraySection) { |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7078 | // If this is not the last component, we expect the pointer to be |
| 7079 | // associated with an array expression or member expression. |
| 7080 | assert((Next == CE || |
| 7081 | isa<MemberExpr>(Next->getAssociatedExpression()) || |
| 7082 | isa<ArraySubscriptExpr>(Next->getAssociatedExpression()) || |
| 7083 | isa<OMPArraySectionExpr>(Next->getAssociatedExpression())) && |
| 7084 | "Unexpected expression"); |
| 7085 | |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7086 | Address LB = |
| 7087 | CGF.EmitOMPSharedLValue(I->getAssociatedExpression()).getAddress(); |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7088 | |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7089 | // If this component is a pointer inside the base struct then we don't |
| 7090 | // need to create any entry for it - it will be combined with the object |
| 7091 | // it is pointing to into a single PTR_AND_OBJ entry. |
| 7092 | bool IsMemberPointer = |
| 7093 | IsPointer && EncounteredME && |
| 7094 | (dyn_cast<MemberExpr>(I->getAssociatedExpression()) == |
| 7095 | EncounteredME); |
Alexey Bataev | e82445f | 2018-09-20 13:54:02 +0000 | [diff] [blame] | 7096 | if (!OverlappedElements.empty()) { |
| 7097 | // Handle base element with the info for overlapped elements. |
| 7098 | assert(!PartialStruct.Base.isValid() && "The base element is set."); |
| 7099 | assert(Next == CE && |
| 7100 | "Expected last element for the overlapped elements."); |
| 7101 | assert(!IsPointer && |
| 7102 | "Unexpected base element with the pointer type."); |
| 7103 | // Mark the whole struct as the struct that requires allocation on the |
| 7104 | // device. |
| 7105 | PartialStruct.LowestElem = {0, LB}; |
| 7106 | CharUnits TypeSize = CGF.getContext().getTypeSizeInChars( |
| 7107 | I->getAssociatedExpression()->getType()); |
| 7108 | Address HB = CGF.Builder.CreateConstGEP( |
| 7109 | CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(LB, |
| 7110 | CGF.VoidPtrTy), |
| 7111 | TypeSize.getQuantity() - 1, CharUnits::One()); |
| 7112 | PartialStruct.HighestElem = { |
| 7113 | std::numeric_limits<decltype( |
| 7114 | PartialStruct.HighestElem.first)>::max(), |
| 7115 | HB}; |
| 7116 | PartialStruct.Base = BP; |
| 7117 | // Emit data for non-overlapped data. |
| 7118 | OpenMPOffloadMappingFlags Flags = |
| 7119 | OMP_MAP_MEMBER_OF | |
| 7120 | getMapTypeBits(MapType, MapTypeModifier, IsImplicit, |
| 7121 | /*AddPtrFlag=*/false, |
| 7122 | /*AddIsTargetParamFlag=*/false); |
| 7123 | LB = BP; |
| 7124 | llvm::Value *Size = nullptr; |
| 7125 | // Do bitcopy of all non-overlapped structure elements. |
| 7126 | for (OMPClauseMappableExprCommon::MappableExprComponentListRef |
| 7127 | Component : OverlappedElements) { |
| 7128 | Address ComponentLB = Address::invalid(); |
| 7129 | for (const OMPClauseMappableExprCommon::MappableComponent &MC : |
| 7130 | Component) { |
| 7131 | if (MC.getAssociatedDeclaration()) { |
| 7132 | ComponentLB = |
| 7133 | CGF.EmitOMPSharedLValue(MC.getAssociatedExpression()) |
| 7134 | .getAddress(); |
| 7135 | Size = CGF.Builder.CreatePtrDiff( |
| 7136 | CGF.EmitCastToVoidPtr(ComponentLB.getPointer()), |
| 7137 | CGF.EmitCastToVoidPtr(LB.getPointer())); |
| 7138 | break; |
| 7139 | } |
| 7140 | } |
| 7141 | BasePointers.push_back(BP.getPointer()); |
| 7142 | Pointers.push_back(LB.getPointer()); |
| 7143 | Sizes.push_back(Size); |
| 7144 | Types.push_back(Flags); |
| 7145 | LB = CGF.Builder.CreateConstGEP(ComponentLB, 1, |
| 7146 | CGF.getPointerSize()); |
| 7147 | } |
| 7148 | BasePointers.push_back(BP.getPointer()); |
| 7149 | Pointers.push_back(LB.getPointer()); |
| 7150 | Size = CGF.Builder.CreatePtrDiff( |
| 7151 | CGF.EmitCastToVoidPtr( |
| 7152 | CGF.Builder.CreateConstGEP(HB, 1, CharUnits::One()) |
| 7153 | .getPointer()), |
| 7154 | CGF.EmitCastToVoidPtr(LB.getPointer())); |
| 7155 | Sizes.push_back(Size); |
| 7156 | Types.push_back(Flags); |
| 7157 | break; |
| 7158 | } |
| 7159 | llvm::Value *Size = getExprTypeSize(I->getAssociatedExpression()); |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7160 | if (!IsMemberPointer) { |
| 7161 | BasePointers.push_back(BP.getPointer()); |
| 7162 | Pointers.push_back(LB.getPointer()); |
| 7163 | Sizes.push_back(Size); |
Samuel Antao | 03a3cec | 2016-07-27 22:52:16 +0000 | [diff] [blame] | 7164 | |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7165 | // We need to add a pointer flag for each map that comes from the |
| 7166 | // same expression except for the first one. We also need to signal |
| 7167 | // this map is the first one that relates with the current capture |
| 7168 | // (there is a set of entries for each capture). |
| 7169 | OpenMPOffloadMappingFlags Flags = getMapTypeBits( |
| 7170 | MapType, MapTypeModifier, IsImplicit, |
| 7171 | !IsExpressionFirstInfo || IsLink, IsCaptureFirstInfo && !IsLink); |
| 7172 | |
| 7173 | if (!IsExpressionFirstInfo) { |
| 7174 | // If we have a PTR_AND_OBJ pair where the OBJ is a pointer as well, |
| 7175 | // then we reset the TO/FROM/ALWAYS/DELETE flags. |
| 7176 | if (IsPointer) |
| 7177 | Flags &= ~(OMP_MAP_TO | OMP_MAP_FROM | OMP_MAP_ALWAYS | |
| 7178 | OMP_MAP_DELETE); |
| 7179 | |
| 7180 | if (ShouldBeMemberOf) { |
| 7181 | // Set placeholder value MEMBER_OF=FFFF to indicate that the flag |
| 7182 | // should be later updated with the correct value of MEMBER_OF. |
| 7183 | Flags |= OMP_MAP_MEMBER_OF; |
| 7184 | // From now on, all subsequent PTR_AND_OBJ entries should not be |
| 7185 | // marked as MEMBER_OF. |
| 7186 | ShouldBeMemberOf = false; |
| 7187 | } |
| 7188 | } |
| 7189 | |
| 7190 | Types.push_back(Flags); |
Samuel Antao | 03a3cec | 2016-07-27 22:52:16 +0000 | [diff] [blame] | 7191 | } |
| 7192 | |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7193 | // If we have encountered a member expression so far, keep track of the |
| 7194 | // mapped member. If the parent is "*this", then the value declaration |
| 7195 | // is nullptr. |
| 7196 | if (EncounteredME) { |
| 7197 | const auto *FD = dyn_cast<FieldDecl>(EncounteredME->getMemberDecl()); |
| 7198 | unsigned FieldIndex = FD->getFieldIndex(); |
Samuel Antao | 03a3cec | 2016-07-27 22:52:16 +0000 | [diff] [blame] | 7199 | |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7200 | // Update info about the lowest and highest elements for this struct |
| 7201 | if (!PartialStruct.Base.isValid()) { |
| 7202 | PartialStruct.LowestElem = {FieldIndex, LB}; |
| 7203 | PartialStruct.HighestElem = {FieldIndex, LB}; |
| 7204 | PartialStruct.Base = BP; |
| 7205 | } else if (FieldIndex < PartialStruct.LowestElem.first) { |
| 7206 | PartialStruct.LowestElem = {FieldIndex, LB}; |
| 7207 | } else if (FieldIndex > PartialStruct.HighestElem.first) { |
| 7208 | PartialStruct.HighestElem = {FieldIndex, LB}; |
| 7209 | } |
| 7210 | } |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7211 | |
| 7212 | // If we have a final array section, we are done with this expression. |
| 7213 | if (IsFinalArraySection) |
| 7214 | break; |
| 7215 | |
| 7216 | // The pointer becomes the base for the next element. |
| 7217 | if (Next != CE) |
| 7218 | BP = LB; |
| 7219 | |
| 7220 | IsExpressionFirstInfo = false; |
| 7221 | IsCaptureFirstInfo = false; |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7222 | } |
| 7223 | } |
| 7224 | } |
| 7225 | |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7226 | /// Return the adjusted map modifiers if the declaration a capture refers to |
| 7227 | /// appears in a first-private clause. This is expected to be used only with |
| 7228 | /// directives that start with 'target'. |
| 7229 | MappableExprsHandler::OpenMPOffloadMappingFlags |
| 7230 | getMapModifiersForPrivateClauses(const CapturedStmt::Capture &Cap) const { |
| 7231 | assert(Cap.capturesVariable() && "Expected capture by reference only!"); |
| 7232 | |
| 7233 | // A first private variable captured by reference will use only the |
| 7234 | // 'private ptr' and 'map to' flag. Return the right flags if the captured |
| 7235 | // declaration is known as first-private in this handler. |
| 7236 | if (FirstPrivateDecls.count(Cap.getCapturedVar())) |
| 7237 | return MappableExprsHandler::OMP_MAP_PRIVATE | |
| 7238 | MappableExprsHandler::OMP_MAP_TO; |
| 7239 | return MappableExprsHandler::OMP_MAP_TO | |
| 7240 | MappableExprsHandler::OMP_MAP_FROM; |
| 7241 | } |
| 7242 | |
| 7243 | static OpenMPOffloadMappingFlags getMemberOfFlag(unsigned Position) { |
| 7244 | // Member of is given by the 16 MSB of the flag, so rotate by 48 bits. |
| 7245 | return static_cast<OpenMPOffloadMappingFlags>(((uint64_t)Position + 1) |
| 7246 | << 48); |
| 7247 | } |
| 7248 | |
| 7249 | static void setCorrectMemberOfFlag(OpenMPOffloadMappingFlags &Flags, |
| 7250 | OpenMPOffloadMappingFlags MemberOfFlag) { |
| 7251 | // If the entry is PTR_AND_OBJ but has not been marked with the special |
| 7252 | // placeholder value 0xFFFF in the MEMBER_OF field, then it should not be |
| 7253 | // marked as MEMBER_OF. |
| 7254 | if ((Flags & OMP_MAP_PTR_AND_OBJ) && |
| 7255 | ((Flags & OMP_MAP_MEMBER_OF) != OMP_MAP_MEMBER_OF)) |
| 7256 | return; |
| 7257 | |
| 7258 | // Reset the placeholder value to prepare the flag for the assignment of the |
| 7259 | // proper MEMBER_OF value. |
| 7260 | Flags &= ~OMP_MAP_MEMBER_OF; |
| 7261 | Flags |= MemberOfFlag; |
| 7262 | } |
| 7263 | |
Alexey Bataev | e82445f | 2018-09-20 13:54:02 +0000 | [diff] [blame] | 7264 | void getPlainLayout(const CXXRecordDecl *RD, |
| 7265 | llvm::SmallVectorImpl<const FieldDecl *> &Layout, |
| 7266 | bool AsBase) const { |
| 7267 | const CGRecordLayout &RL = CGF.getTypes().getCGRecordLayout(RD); |
| 7268 | |
| 7269 | llvm::StructType *St = |
| 7270 | AsBase ? RL.getBaseSubobjectLLVMType() : RL.getLLVMType(); |
| 7271 | |
| 7272 | unsigned NumElements = St->getNumElements(); |
| 7273 | llvm::SmallVector< |
| 7274 | llvm::PointerUnion<const CXXRecordDecl *, const FieldDecl *>, 4> |
| 7275 | RecordLayout(NumElements); |
| 7276 | |
| 7277 | // Fill bases. |
| 7278 | for (const auto &I : RD->bases()) { |
| 7279 | if (I.isVirtual()) |
| 7280 | continue; |
| 7281 | const auto *Base = I.getType()->getAsCXXRecordDecl(); |
| 7282 | // Ignore empty bases. |
| 7283 | if (Base->isEmpty() || CGF.getContext() |
| 7284 | .getASTRecordLayout(Base) |
| 7285 | .getNonVirtualSize() |
| 7286 | .isZero()) |
| 7287 | continue; |
| 7288 | |
| 7289 | unsigned FieldIndex = RL.getNonVirtualBaseLLVMFieldNo(Base); |
| 7290 | RecordLayout[FieldIndex] = Base; |
| 7291 | } |
| 7292 | // Fill in virtual bases. |
| 7293 | for (const auto &I : RD->vbases()) { |
| 7294 | const auto *Base = I.getType()->getAsCXXRecordDecl(); |
| 7295 | // Ignore empty bases. |
| 7296 | if (Base->isEmpty()) |
| 7297 | continue; |
| 7298 | unsigned FieldIndex = RL.getVirtualBaseIndex(Base); |
| 7299 | if (RecordLayout[FieldIndex]) |
| 7300 | continue; |
| 7301 | RecordLayout[FieldIndex] = Base; |
| 7302 | } |
| 7303 | // Fill in all the fields. |
| 7304 | assert(!RD->isUnion() && "Unexpected union."); |
| 7305 | for (const auto *Field : RD->fields()) { |
| 7306 | // Fill in non-bitfields. (Bitfields always use a zero pattern, which we |
| 7307 | // will fill in later.) |
| 7308 | if (!Field->isBitField()) { |
| 7309 | unsigned FieldIndex = RL.getLLVMFieldNo(Field); |
| 7310 | RecordLayout[FieldIndex] = Field; |
| 7311 | } |
| 7312 | } |
| 7313 | for (const llvm::PointerUnion<const CXXRecordDecl *, const FieldDecl *> |
| 7314 | &Data : RecordLayout) { |
| 7315 | if (Data.isNull()) |
| 7316 | continue; |
| 7317 | if (const auto *Base = Data.dyn_cast<const CXXRecordDecl *>()) |
| 7318 | getPlainLayout(Base, Layout, /*AsBase=*/true); |
| 7319 | else |
| 7320 | Layout.push_back(Data.get<const FieldDecl *>()); |
| 7321 | } |
| 7322 | } |
| 7323 | |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7324 | public: |
| 7325 | MappableExprsHandler(const OMPExecutableDirective &Dir, CodeGenFunction &CGF) |
| 7326 | : CurDir(Dir), CGF(CGF) { |
| 7327 | // Extract firstprivate clause information. |
| 7328 | for (const auto *C : Dir.getClausesOfKind<OMPFirstprivateClause>()) |
| 7329 | for (const auto *D : C->varlists()) |
| 7330 | FirstPrivateDecls.insert( |
| 7331 | cast<VarDecl>(cast<DeclRefExpr>(D)->getDecl())->getCanonicalDecl()); |
| 7332 | // Extract device pointer clause information. |
| 7333 | for (const auto *C : Dir.getClausesOfKind<OMPIsDevicePtrClause>()) |
| 7334 | for (auto L : C->component_lists()) |
| 7335 | DevPointersMap[L.first].push_back(L.second); |
| 7336 | } |
| 7337 | |
| 7338 | /// Generate code for the combined entry if we have a partially mapped struct |
| 7339 | /// and take care of the mapping flags of the arguments corresponding to |
| 7340 | /// individual struct members. |
| 7341 | void emitCombinedEntry(MapBaseValuesArrayTy &BasePointers, |
| 7342 | MapValuesArrayTy &Pointers, MapValuesArrayTy &Sizes, |
| 7343 | MapFlagsArrayTy &Types, MapFlagsArrayTy &CurTypes, |
| 7344 | const StructRangeInfoTy &PartialStruct) const { |
| 7345 | // Base is the base of the struct |
| 7346 | BasePointers.push_back(PartialStruct.Base.getPointer()); |
| 7347 | // Pointer is the address of the lowest element |
| 7348 | llvm::Value *LB = PartialStruct.LowestElem.second.getPointer(); |
| 7349 | Pointers.push_back(LB); |
| 7350 | // Size is (addr of {highest+1} element) - (addr of lowest element) |
| 7351 | llvm::Value *HB = PartialStruct.HighestElem.second.getPointer(); |
| 7352 | llvm::Value *HAddr = CGF.Builder.CreateConstGEP1_32(HB, /*Idx0=*/1); |
| 7353 | llvm::Value *CLAddr = CGF.Builder.CreatePointerCast(LB, CGF.VoidPtrTy); |
| 7354 | llvm::Value *CHAddr = CGF.Builder.CreatePointerCast(HAddr, CGF.VoidPtrTy); |
| 7355 | llvm::Value *Diff = CGF.Builder.CreatePtrDiff(CHAddr, CLAddr); |
| 7356 | llvm::Value *Size = CGF.Builder.CreateIntCast(Diff, CGF.SizeTy, |
| 7357 | /*isSinged=*/false); |
| 7358 | Sizes.push_back(Size); |
| 7359 | // Map type is always TARGET_PARAM |
| 7360 | Types.push_back(OMP_MAP_TARGET_PARAM); |
| 7361 | // Remove TARGET_PARAM flag from the first element |
| 7362 | (*CurTypes.begin()) &= ~OMP_MAP_TARGET_PARAM; |
| 7363 | |
| 7364 | // All other current entries will be MEMBER_OF the combined entry |
| 7365 | // (except for PTR_AND_OBJ entries which do not have a placeholder value |
| 7366 | // 0xFFFF in the MEMBER_OF field). |
| 7367 | OpenMPOffloadMappingFlags MemberOfFlag = |
| 7368 | getMemberOfFlag(BasePointers.size() - 1); |
| 7369 | for (auto &M : CurTypes) |
| 7370 | setCorrectMemberOfFlag(M, MemberOfFlag); |
| 7371 | } |
| 7372 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 7373 | /// Generate all the base pointers, section pointers, sizes and map |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7374 | /// types for the extracted mappable expressions. Also, for each item that |
| 7375 | /// relates with a device pointer, a pair of the relevant declaration and |
| 7376 | /// index where it occurs is appended to the device pointers info array. |
| 7377 | void generateAllInfo(MapBaseValuesArrayTy &BasePointers, |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7378 | MapValuesArrayTy &Pointers, MapValuesArrayTy &Sizes, |
| 7379 | MapFlagsArrayTy &Types) const { |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7380 | // We have to process the component lists that relate with the same |
| 7381 | // declaration in a single chunk so that we can generate the map flags |
| 7382 | // correctly. Therefore, we organize all lists in a map. |
Alexey Bataev | 5d1c3f6 | 2017-06-27 15:46:42 +0000 | [diff] [blame] | 7383 | llvm::MapVector<const ValueDecl *, SmallVector<MapInfo, 8>> Info; |
Samuel Antao | 8d2d730 | 2016-05-26 18:30:22 +0000 | [diff] [blame] | 7384 | |
| 7385 | // Helper function to fill the information map for the different supported |
| 7386 | // clauses. |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7387 | auto &&InfoGen = [&Info]( |
| 7388 | const ValueDecl *D, |
| 7389 | OMPClauseMappableExprCommon::MappableExprComponentListRef L, |
| 7390 | OpenMPMapClauseKind MapType, OpenMPMapClauseKind MapModifier, |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7391 | bool ReturnDevicePointer, bool IsImplicit) { |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7392 | const ValueDecl *VD = |
| 7393 | D ? cast<ValueDecl>(D->getCanonicalDecl()) : nullptr; |
Alexey Bataev | f47c4b4 | 2017-09-26 13:47:31 +0000 | [diff] [blame] | 7394 | Info[VD].emplace_back(L, MapType, MapModifier, ReturnDevicePointer, |
| 7395 | IsImplicit); |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7396 | }; |
Samuel Antao | 8d2d730 | 2016-05-26 18:30:22 +0000 | [diff] [blame] | 7397 | |
Paul Robinson | 78fb132 | 2016-08-01 22:12:46 +0000 | [diff] [blame] | 7398 | // FIXME: MSVC 2013 seems to require this-> to find member CurDir. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 7399 | for (const auto *C : this->CurDir.getClausesOfKind<OMPMapClause>()) |
| 7400 | for (const auto &L : C->component_lists()) { |
Samuel Antao | cf3f83e | 2016-07-28 14:47:35 +0000 | [diff] [blame] | 7401 | InfoGen(L.first, L.second, C->getMapType(), C->getMapTypeModifier(), |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7402 | /*ReturnDevicePointer=*/false, C->isImplicit()); |
Alexey Bataev | f47c4b4 | 2017-09-26 13:47:31 +0000 | [diff] [blame] | 7403 | } |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 7404 | for (const auto *C : this->CurDir.getClausesOfKind<OMPToClause>()) |
| 7405 | for (const auto &L : C->component_lists()) { |
Samuel Antao | cf3f83e | 2016-07-28 14:47:35 +0000 | [diff] [blame] | 7406 | InfoGen(L.first, L.second, OMPC_MAP_to, OMPC_MAP_unknown, |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7407 | /*ReturnDevicePointer=*/false, C->isImplicit()); |
Alexey Bataev | f47c4b4 | 2017-09-26 13:47:31 +0000 | [diff] [blame] | 7408 | } |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 7409 | for (const auto *C : this->CurDir.getClausesOfKind<OMPFromClause>()) |
| 7410 | for (const auto &L : C->component_lists()) { |
Samuel Antao | cf3f83e | 2016-07-28 14:47:35 +0000 | [diff] [blame] | 7411 | InfoGen(L.first, L.second, OMPC_MAP_from, OMPC_MAP_unknown, |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7412 | /*ReturnDevicePointer=*/false, C->isImplicit()); |
Alexey Bataev | f47c4b4 | 2017-09-26 13:47:31 +0000 | [diff] [blame] | 7413 | } |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7414 | |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7415 | // Look at the use_device_ptr clause information and mark the existing map |
| 7416 | // entries as such. If there is no map information for an entry in the |
| 7417 | // use_device_ptr list, we create one with map type 'alloc' and zero size |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7418 | // section. It is the user fault if that was not mapped before. If there is |
| 7419 | // no map information and the pointer is a struct member, then we defer the |
| 7420 | // emission of that entry until the whole struct has been processed. |
| 7421 | llvm::MapVector<const ValueDecl *, SmallVector<DeferredDevicePtrEntryTy, 4>> |
| 7422 | DeferredInfo; |
| 7423 | |
Paul Robinson | 78fb132 | 2016-08-01 22:12:46 +0000 | [diff] [blame] | 7424 | // FIXME: MSVC 2013 seems to require this-> to find member CurDir. |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7425 | for (const auto *C : |
| 7426 | this->CurDir.getClausesOfKind<OMPUseDevicePtrClause>()) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 7427 | for (const auto &L : C->component_lists()) { |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7428 | assert(!L.second.empty() && "Not expecting empty list of components!"); |
| 7429 | const ValueDecl *VD = L.second.back().getAssociatedDeclaration(); |
| 7430 | VD = cast<ValueDecl>(VD->getCanonicalDecl()); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 7431 | const Expr *IE = L.second.back().getAssociatedExpression(); |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7432 | // If the first component is a member expression, we have to look into |
| 7433 | // 'this', which maps to null in the map of map information. Otherwise |
| 7434 | // look directly for the information. |
| 7435 | auto It = Info.find(isa<MemberExpr>(IE) ? nullptr : VD); |
| 7436 | |
| 7437 | // We potentially have map information for this declaration already. |
| 7438 | // Look for the first set of components that refer to it. |
| 7439 | if (It != Info.end()) { |
| 7440 | auto CI = std::find_if( |
| 7441 | It->second.begin(), It->second.end(), [VD](const MapInfo &MI) { |
| 7442 | return MI.Components.back().getAssociatedDeclaration() == VD; |
| 7443 | }); |
| 7444 | // If we found a map entry, signal that the pointer has to be returned |
| 7445 | // and move on to the next declaration. |
| 7446 | if (CI != It->second.end()) { |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7447 | CI->ReturnDevicePointer = true; |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7448 | continue; |
| 7449 | } |
| 7450 | } |
| 7451 | |
| 7452 | // We didn't find any match in our map information - generate a zero |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7453 | // size array section - if the pointer is a struct member we defer this |
| 7454 | // action until the whole struct has been processed. |
Paul Robinson | 78fb132 | 2016-08-01 22:12:46 +0000 | [diff] [blame] | 7455 | // FIXME: MSVC 2013 seems to require this-> to find member CGF. |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7456 | if (isa<MemberExpr>(IE)) { |
| 7457 | // Insert the pointer into Info to be processed by |
| 7458 | // generateInfoForComponentList. Because it is a member pointer |
| 7459 | // without a pointee, no entry will be generated for it, therefore |
| 7460 | // we need to generate one after the whole struct has been processed. |
| 7461 | // Nonetheless, generateInfoForComponentList must be called to take |
| 7462 | // the pointer into account for the calculation of the range of the |
| 7463 | // partial struct. |
| 7464 | InfoGen(nullptr, L.second, OMPC_MAP_unknown, OMPC_MAP_unknown, |
| 7465 | /*ReturnDevicePointer=*/false, C->isImplicit()); |
| 7466 | DeferredInfo[nullptr].emplace_back(IE, VD); |
| 7467 | } else { |
| 7468 | llvm::Value *Ptr = this->CGF.EmitLoadOfScalar( |
| 7469 | this->CGF.EmitLValue(IE), IE->getExprLoc()); |
| 7470 | BasePointers.emplace_back(Ptr, VD); |
| 7471 | Pointers.push_back(Ptr); |
| 7472 | Sizes.push_back(llvm::Constant::getNullValue(this->CGF.SizeTy)); |
| 7473 | Types.push_back(OMP_MAP_RETURN_PARAM | OMP_MAP_TARGET_PARAM); |
| 7474 | } |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7475 | } |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7476 | } |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7477 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 7478 | for (const auto &M : Info) { |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7479 | // We need to know when we generate information for the first component |
| 7480 | // associated with a capture, because the mapping flags depend on it. |
| 7481 | bool IsFirstComponentList = true; |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7482 | |
| 7483 | // Temporary versions of arrays |
| 7484 | MapBaseValuesArrayTy CurBasePointers; |
| 7485 | MapValuesArrayTy CurPointers; |
| 7486 | MapValuesArrayTy CurSizes; |
| 7487 | MapFlagsArrayTy CurTypes; |
| 7488 | StructRangeInfoTy PartialStruct; |
| 7489 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 7490 | for (const MapInfo &L : M.second) { |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7491 | assert(!L.Components.empty() && |
| 7492 | "Not expecting declaration with no component lists."); |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7493 | |
| 7494 | // Remember the current base pointer index. |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7495 | unsigned CurrentBasePointersIdx = CurBasePointers.size(); |
Paul Robinson | 78fb132 | 2016-08-01 22:12:46 +0000 | [diff] [blame] | 7496 | // FIXME: MSVC 2013 seems to require this-> to find the member method. |
Alexey Bataev | f47c4b4 | 2017-09-26 13:47:31 +0000 | [diff] [blame] | 7497 | this->generateInfoForComponentList( |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7498 | L.MapType, L.MapTypeModifier, L.Components, CurBasePointers, |
| 7499 | CurPointers, CurSizes, CurTypes, PartialStruct, |
| 7500 | IsFirstComponentList, L.IsImplicit); |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7501 | |
| 7502 | // If this entry relates with a device pointer, set the relevant |
| 7503 | // declaration and add the 'return pointer' flag. |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7504 | if (L.ReturnDevicePointer) { |
| 7505 | assert(CurBasePointers.size() > CurrentBasePointersIdx && |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7506 | "Unexpected number of mapped base pointers."); |
| 7507 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 7508 | const ValueDecl *RelevantVD = |
| 7509 | L.Components.back().getAssociatedDeclaration(); |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7510 | assert(RelevantVD && |
| 7511 | "No relevant declaration related with device pointer??"); |
| 7512 | |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7513 | CurBasePointers[CurrentBasePointersIdx].setDevicePtrDecl(RelevantVD); |
| 7514 | CurTypes[CurrentBasePointersIdx] |= OMP_MAP_RETURN_PARAM; |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7515 | } |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7516 | IsFirstComponentList = false; |
| 7517 | } |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7518 | |
| 7519 | // Append any pending zero-length pointers which are struct members and |
| 7520 | // used with use_device_ptr. |
| 7521 | auto CI = DeferredInfo.find(M.first); |
| 7522 | if (CI != DeferredInfo.end()) { |
| 7523 | for (const DeferredDevicePtrEntryTy &L : CI->second) { |
| 7524 | llvm::Value *BasePtr = this->CGF.EmitLValue(L.IE).getPointer(); |
| 7525 | llvm::Value *Ptr = this->CGF.EmitLoadOfScalar( |
| 7526 | this->CGF.EmitLValue(L.IE), L.IE->getExprLoc()); |
| 7527 | CurBasePointers.emplace_back(BasePtr, L.VD); |
| 7528 | CurPointers.push_back(Ptr); |
| 7529 | CurSizes.push_back(llvm::Constant::getNullValue(this->CGF.SizeTy)); |
| 7530 | // Entry is PTR_AND_OBJ and RETURN_PARAM. Also, set the placeholder |
| 7531 | // value MEMBER_OF=FFFF so that the entry is later updated with the |
| 7532 | // correct value of MEMBER_OF. |
| 7533 | CurTypes.push_back(OMP_MAP_PTR_AND_OBJ | OMP_MAP_RETURN_PARAM | |
| 7534 | OMP_MAP_MEMBER_OF); |
| 7535 | } |
| 7536 | } |
| 7537 | |
| 7538 | // If there is an entry in PartialStruct it means we have a struct with |
| 7539 | // individual members mapped. Emit an extra combined entry. |
| 7540 | if (PartialStruct.Base.isValid()) |
| 7541 | emitCombinedEntry(BasePointers, Pointers, Sizes, Types, CurTypes, |
| 7542 | PartialStruct); |
| 7543 | |
| 7544 | // We need to append the results of this capture to what we already have. |
| 7545 | BasePointers.append(CurBasePointers.begin(), CurBasePointers.end()); |
| 7546 | Pointers.append(CurPointers.begin(), CurPointers.end()); |
| 7547 | Sizes.append(CurSizes.begin(), CurSizes.end()); |
| 7548 | Types.append(CurTypes.begin(), CurTypes.end()); |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7549 | } |
| 7550 | } |
| 7551 | |
Alexey Bataev | 6070542 | 2018-10-30 15:50:12 +0000 | [diff] [blame] | 7552 | /// Emit capture info for lambdas for variables captured by reference. |
Alexey Bataev | 969dbc0 | 2018-11-08 15:47:39 +0000 | [diff] [blame] | 7553 | void generateInfoForLambdaCaptures( |
| 7554 | const ValueDecl *VD, llvm::Value *Arg, MapBaseValuesArrayTy &BasePointers, |
| 7555 | MapValuesArrayTy &Pointers, MapValuesArrayTy &Sizes, |
| 7556 | MapFlagsArrayTy &Types, |
| 7557 | llvm::DenseMap<llvm::Value *, llvm::Value *> &LambdaPointers) const { |
Alexey Bataev | 6070542 | 2018-10-30 15:50:12 +0000 | [diff] [blame] | 7558 | const auto *RD = VD->getType() |
| 7559 | .getCanonicalType() |
| 7560 | .getNonReferenceType() |
| 7561 | ->getAsCXXRecordDecl(); |
| 7562 | if (!RD || !RD->isLambda()) |
| 7563 | return; |
| 7564 | Address VDAddr = Address(Arg, CGF.getContext().getDeclAlign(VD)); |
| 7565 | LValue VDLVal = CGF.MakeAddrLValue( |
| 7566 | VDAddr, VD->getType().getCanonicalType().getNonReferenceType()); |
| 7567 | llvm::DenseMap<const VarDecl *, FieldDecl *> Captures; |
| 7568 | FieldDecl *ThisCapture = nullptr; |
| 7569 | RD->getCaptureFields(Captures, ThisCapture); |
| 7570 | if (ThisCapture) { |
| 7571 | LValue ThisLVal = |
| 7572 | CGF.EmitLValueForFieldInitialization(VDLVal, ThisCapture); |
Alexey Bataev | 969dbc0 | 2018-11-08 15:47:39 +0000 | [diff] [blame] | 7573 | LValue ThisLValVal = CGF.EmitLValueForField(VDLVal, ThisCapture); |
| 7574 | LambdaPointers.try_emplace(ThisLVal.getPointer(), VDLVal.getPointer()); |
| 7575 | BasePointers.push_back(ThisLVal.getPointer()); |
| 7576 | Pointers.push_back(ThisLValVal.getPointer()); |
Alexey Bataev | 6070542 | 2018-10-30 15:50:12 +0000 | [diff] [blame] | 7577 | Sizes.push_back(CGF.getTypeSize(CGF.getContext().VoidPtrTy)); |
Alexey Bataev | 2dc07d0 | 2018-11-02 15:25:06 +0000 | [diff] [blame] | 7578 | Types.push_back(OMP_MAP_PTR_AND_OBJ | OMP_MAP_LITERAL | |
Alexey Bataev | 6070542 | 2018-10-30 15:50:12 +0000 | [diff] [blame] | 7579 | OMP_MAP_MEMBER_OF | OMP_MAP_IMPLICIT); |
| 7580 | } |
| 7581 | for (const LambdaCapture &LC : RD->captures()) { |
| 7582 | if (LC.getCaptureKind() != LCK_ByRef) |
| 7583 | continue; |
| 7584 | const VarDecl *VD = LC.getCapturedVar(); |
| 7585 | auto It = Captures.find(VD); |
| 7586 | assert(It != Captures.end() && "Found lambda capture without field."); |
| 7587 | LValue VarLVal = CGF.EmitLValueForFieldInitialization(VDLVal, It->second); |
Alexey Bataev | 969dbc0 | 2018-11-08 15:47:39 +0000 | [diff] [blame] | 7588 | LValue VarLValVal = CGF.EmitLValueForField(VDLVal, It->second); |
| 7589 | LambdaPointers.try_emplace(VarLVal.getPointer(), VDLVal.getPointer()); |
| 7590 | BasePointers.push_back(VarLVal.getPointer()); |
| 7591 | Pointers.push_back(VarLValVal.getPointer()); |
Alexey Bataev | 6070542 | 2018-10-30 15:50:12 +0000 | [diff] [blame] | 7592 | Sizes.push_back(CGF.getTypeSize( |
| 7593 | VD->getType().getCanonicalType().getNonReferenceType())); |
Alexey Bataev | 2dc07d0 | 2018-11-02 15:25:06 +0000 | [diff] [blame] | 7594 | Types.push_back(OMP_MAP_PTR_AND_OBJ | OMP_MAP_LITERAL | |
Alexey Bataev | 6070542 | 2018-10-30 15:50:12 +0000 | [diff] [blame] | 7595 | OMP_MAP_MEMBER_OF | OMP_MAP_IMPLICIT); |
| 7596 | } |
| 7597 | } |
| 7598 | |
| 7599 | /// Set correct indices for lambdas captures. |
Alexey Bataev | 969dbc0 | 2018-11-08 15:47:39 +0000 | [diff] [blame] | 7600 | void adjustMemberOfForLambdaCaptures( |
| 7601 | const llvm::DenseMap<llvm::Value *, llvm::Value *> &LambdaPointers, |
| 7602 | MapBaseValuesArrayTy &BasePointers, MapValuesArrayTy &Pointers, |
| 7603 | MapFlagsArrayTy &Types) const { |
Alexey Bataev | 6070542 | 2018-10-30 15:50:12 +0000 | [diff] [blame] | 7604 | for (unsigned I = 0, E = Types.size(); I < E; ++I) { |
| 7605 | // Set correct member_of idx for all implicit lambda captures. |
Alexey Bataev | 2dc07d0 | 2018-11-02 15:25:06 +0000 | [diff] [blame] | 7606 | if (Types[I] != (OMP_MAP_PTR_AND_OBJ | OMP_MAP_LITERAL | |
Alexey Bataev | 6070542 | 2018-10-30 15:50:12 +0000 | [diff] [blame] | 7607 | OMP_MAP_MEMBER_OF | OMP_MAP_IMPLICIT)) |
| 7608 | continue; |
Alexey Bataev | 969dbc0 | 2018-11-08 15:47:39 +0000 | [diff] [blame] | 7609 | llvm::Value *BasePtr = LambdaPointers.lookup(*BasePointers[I]); |
| 7610 | assert(BasePtr && "Unable to find base lambda address."); |
Alexey Bataev | 6070542 | 2018-10-30 15:50:12 +0000 | [diff] [blame] | 7611 | int TgtIdx = -1; |
| 7612 | for (unsigned J = I; J > 0; --J) { |
| 7613 | unsigned Idx = J - 1; |
| 7614 | if (Pointers[Idx] != BasePtr) |
| 7615 | continue; |
| 7616 | TgtIdx = Idx; |
| 7617 | break; |
| 7618 | } |
| 7619 | assert(TgtIdx != -1 && "Unable to find parent lambda."); |
| 7620 | // All other current entries will be MEMBER_OF the combined entry |
| 7621 | // (except for PTR_AND_OBJ entries which do not have a placeholder value |
| 7622 | // 0xFFFF in the MEMBER_OF field). |
| 7623 | OpenMPOffloadMappingFlags MemberOfFlag = getMemberOfFlag(TgtIdx); |
| 7624 | setCorrectMemberOfFlag(Types[I], MemberOfFlag); |
| 7625 | } |
| 7626 | } |
| 7627 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 7628 | /// Generate the base pointers, section pointers, sizes and map types |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7629 | /// associated to a given capture. |
| 7630 | void generateInfoForCapture(const CapturedStmt::Capture *Cap, |
Samuel Antao | 6890b09 | 2016-07-28 14:25:09 +0000 | [diff] [blame] | 7631 | llvm::Value *Arg, |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7632 | MapBaseValuesArrayTy &BasePointers, |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7633 | MapValuesArrayTy &Pointers, |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7634 | MapValuesArrayTy &Sizes, MapFlagsArrayTy &Types, |
| 7635 | StructRangeInfoTy &PartialStruct) const { |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7636 | assert(!Cap->capturesVariableArrayType() && |
| 7637 | "Not expecting to generate map info for a variable array type!"); |
| 7638 | |
Samuel Antao | 6890b09 | 2016-07-28 14:25:09 +0000 | [diff] [blame] | 7639 | // We need to know when we generating information for the first component |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7640 | const ValueDecl *VD = Cap->capturesThis() |
| 7641 | ? nullptr |
| 7642 | : Cap->getCapturedVar()->getCanonicalDecl(); |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7643 | |
Samuel Antao | 6890b09 | 2016-07-28 14:25:09 +0000 | [diff] [blame] | 7644 | // If this declaration appears in a is_device_ptr clause we just have to |
| 7645 | // pass the pointer by value. If it is a reference to a declaration, we just |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7646 | // pass its value. |
| 7647 | if (DevPointersMap.count(VD)) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 7648 | BasePointers.emplace_back(Arg, VD); |
Samuel Antao | 6890b09 | 2016-07-28 14:25:09 +0000 | [diff] [blame] | 7649 | Pointers.push_back(Arg); |
| 7650 | Sizes.push_back(CGF.getTypeSize(CGF.getContext().VoidPtrTy)); |
George Rokos | 065755d | 2017-11-07 18:27:04 +0000 | [diff] [blame] | 7651 | Types.push_back(OMP_MAP_LITERAL | OMP_MAP_TARGET_PARAM); |
Samuel Antao | 6890b09 | 2016-07-28 14:25:09 +0000 | [diff] [blame] | 7652 | return; |
| 7653 | } |
| 7654 | |
Alexey Bataev | e82445f | 2018-09-20 13:54:02 +0000 | [diff] [blame] | 7655 | using MapData = |
| 7656 | std::tuple<OMPClauseMappableExprCommon::MappableExprComponentListRef, |
| 7657 | OpenMPMapClauseKind, OpenMPMapClauseKind, bool>; |
| 7658 | SmallVector<MapData, 4> DeclComponentLists; |
Paul Robinson | 78fb132 | 2016-08-01 22:12:46 +0000 | [diff] [blame] | 7659 | // FIXME: MSVC 2013 seems to require this-> to find member CurDir. |
Alexey Bataev | e82445f | 2018-09-20 13:54:02 +0000 | [diff] [blame] | 7660 | for (const auto *C : this->CurDir.getClausesOfKind<OMPMapClause>()) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 7661 | for (const auto &L : C->decl_component_lists(VD)) { |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7662 | assert(L.first == VD && |
| 7663 | "We got information for the wrong declaration??"); |
| 7664 | assert(!L.second.empty() && |
| 7665 | "Not expecting declaration with no component lists."); |
Alexey Bataev | e82445f | 2018-09-20 13:54:02 +0000 | [diff] [blame] | 7666 | DeclComponentLists.emplace_back(L.second, C->getMapType(), |
| 7667 | C->getMapTypeModifier(), |
| 7668 | C->isImplicit()); |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7669 | } |
Alexey Bataev | e82445f | 2018-09-20 13:54:02 +0000 | [diff] [blame] | 7670 | } |
| 7671 | |
| 7672 | // Find overlapping elements (including the offset from the base element). |
| 7673 | llvm::SmallDenseMap< |
| 7674 | const MapData *, |
| 7675 | llvm::SmallVector< |
| 7676 | OMPClauseMappableExprCommon::MappableExprComponentListRef, 4>, |
| 7677 | 4> |
| 7678 | OverlappedData; |
| 7679 | size_t Count = 0; |
| 7680 | for (const MapData &L : DeclComponentLists) { |
| 7681 | OMPClauseMappableExprCommon::MappableExprComponentListRef Components; |
| 7682 | OpenMPMapClauseKind MapType; |
| 7683 | OpenMPMapClauseKind MapTypeModifier; |
| 7684 | bool IsImplicit; |
| 7685 | std::tie(Components, MapType, MapTypeModifier, IsImplicit) = L; |
| 7686 | ++Count; |
| 7687 | for (const MapData &L1 : makeArrayRef(DeclComponentLists).slice(Count)) { |
| 7688 | OMPClauseMappableExprCommon::MappableExprComponentListRef Components1; |
| 7689 | std::tie(Components1, MapType, MapTypeModifier, IsImplicit) = L1; |
| 7690 | auto CI = Components.rbegin(); |
| 7691 | auto CE = Components.rend(); |
| 7692 | auto SI = Components1.rbegin(); |
| 7693 | auto SE = Components1.rend(); |
| 7694 | for (; CI != CE && SI != SE; ++CI, ++SI) { |
| 7695 | if (CI->getAssociatedExpression()->getStmtClass() != |
| 7696 | SI->getAssociatedExpression()->getStmtClass()) |
| 7697 | break; |
| 7698 | // Are we dealing with different variables/fields? |
| 7699 | if (CI->getAssociatedDeclaration() != SI->getAssociatedDeclaration()) |
| 7700 | break; |
| 7701 | } |
| 7702 | // Found overlapping if, at least for one component, reached the head of |
| 7703 | // the components list. |
| 7704 | if (CI == CE || SI == SE) { |
| 7705 | assert((CI != CE || SI != SE) && |
| 7706 | "Unexpected full match of the mapping components."); |
| 7707 | const MapData &BaseData = CI == CE ? L : L1; |
| 7708 | OMPClauseMappableExprCommon::MappableExprComponentListRef SubData = |
| 7709 | SI == SE ? Components : Components1; |
Alexey Bataev | e82445f | 2018-09-20 13:54:02 +0000 | [diff] [blame] | 7710 | auto &OverlappedElements = OverlappedData.FindAndConstruct(&BaseData); |
| 7711 | OverlappedElements.getSecond().push_back(SubData); |
| 7712 | } |
| 7713 | } |
| 7714 | } |
| 7715 | // Sort the overlapped elements for each item. |
| 7716 | llvm::SmallVector<const FieldDecl *, 4> Layout; |
| 7717 | if (!OverlappedData.empty()) { |
| 7718 | if (const auto *CRD = |
| 7719 | VD->getType().getCanonicalType()->getAsCXXRecordDecl()) |
| 7720 | getPlainLayout(CRD, Layout, /*AsBase=*/false); |
| 7721 | else { |
| 7722 | const auto *RD = VD->getType().getCanonicalType()->getAsRecordDecl(); |
| 7723 | Layout.append(RD->field_begin(), RD->field_end()); |
| 7724 | } |
| 7725 | } |
| 7726 | for (auto &Pair : OverlappedData) { |
| 7727 | llvm::sort( |
| 7728 | Pair.getSecond(), |
| 7729 | [&Layout]( |
| 7730 | OMPClauseMappableExprCommon::MappableExprComponentListRef First, |
| 7731 | OMPClauseMappableExprCommon::MappableExprComponentListRef |
| 7732 | Second) { |
| 7733 | auto CI = First.rbegin(); |
| 7734 | auto CE = First.rend(); |
| 7735 | auto SI = Second.rbegin(); |
| 7736 | auto SE = Second.rend(); |
| 7737 | for (; CI != CE && SI != SE; ++CI, ++SI) { |
| 7738 | if (CI->getAssociatedExpression()->getStmtClass() != |
| 7739 | SI->getAssociatedExpression()->getStmtClass()) |
| 7740 | break; |
| 7741 | // Are we dealing with different variables/fields? |
| 7742 | if (CI->getAssociatedDeclaration() != |
| 7743 | SI->getAssociatedDeclaration()) |
| 7744 | break; |
| 7745 | } |
Richard Trieu | 5061e83 | 2018-09-21 21:20:33 +0000 | [diff] [blame] | 7746 | |
| 7747 | // Lists contain the same elements. |
| 7748 | if (CI == CE && SI == SE) |
| 7749 | return false; |
| 7750 | |
| 7751 | // List with less elements is less than list with more elements. |
| 7752 | if (CI == CE || SI == SE) |
| 7753 | return CI == CE; |
| 7754 | |
Alexey Bataev | e82445f | 2018-09-20 13:54:02 +0000 | [diff] [blame] | 7755 | const auto *FD1 = cast<FieldDecl>(CI->getAssociatedDeclaration()); |
| 7756 | const auto *FD2 = cast<FieldDecl>(SI->getAssociatedDeclaration()); |
| 7757 | if (FD1->getParent() == FD2->getParent()) |
| 7758 | return FD1->getFieldIndex() < FD2->getFieldIndex(); |
| 7759 | const auto It = |
| 7760 | llvm::find_if(Layout, [FD1, FD2](const FieldDecl *FD) { |
| 7761 | return FD == FD1 || FD == FD2; |
| 7762 | }); |
| 7763 | return *It == FD1; |
| 7764 | }); |
| 7765 | } |
| 7766 | |
| 7767 | // Associated with a capture, because the mapping flags depend on it. |
| 7768 | // Go through all of the elements with the overlapped elements. |
| 7769 | for (const auto &Pair : OverlappedData) { |
| 7770 | const MapData &L = *Pair.getFirst(); |
| 7771 | OMPClauseMappableExprCommon::MappableExprComponentListRef Components; |
| 7772 | OpenMPMapClauseKind MapType; |
| 7773 | OpenMPMapClauseKind MapTypeModifier; |
| 7774 | bool IsImplicit; |
| 7775 | std::tie(Components, MapType, MapTypeModifier, IsImplicit) = L; |
| 7776 | ArrayRef<OMPClauseMappableExprCommon::MappableExprComponentListRef> |
| 7777 | OverlappedComponents = Pair.getSecond(); |
| 7778 | bool IsFirstComponentList = true; |
| 7779 | generateInfoForComponentList(MapType, MapTypeModifier, Components, |
| 7780 | BasePointers, Pointers, Sizes, Types, |
| 7781 | PartialStruct, IsFirstComponentList, |
| 7782 | IsImplicit, OverlappedComponents); |
| 7783 | } |
| 7784 | // Go through other elements without overlapped elements. |
| 7785 | bool IsFirstComponentList = OverlappedData.empty(); |
| 7786 | for (const MapData &L : DeclComponentLists) { |
| 7787 | OMPClauseMappableExprCommon::MappableExprComponentListRef Components; |
| 7788 | OpenMPMapClauseKind MapType; |
| 7789 | OpenMPMapClauseKind MapTypeModifier; |
| 7790 | bool IsImplicit; |
| 7791 | std::tie(Components, MapType, MapTypeModifier, IsImplicit) = L; |
| 7792 | auto It = OverlappedData.find(&L); |
| 7793 | if (It == OverlappedData.end()) |
| 7794 | generateInfoForComponentList(MapType, MapTypeModifier, Components, |
| 7795 | BasePointers, Pointers, Sizes, Types, |
| 7796 | PartialStruct, IsFirstComponentList, |
| 7797 | IsImplicit); |
| 7798 | IsFirstComponentList = false; |
| 7799 | } |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7800 | } |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7801 | |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7802 | /// Generate the base pointers, section pointers, sizes and map types |
| 7803 | /// associated with the declare target link variables. |
| 7804 | void generateInfoForDeclareTargetLink(MapBaseValuesArrayTy &BasePointers, |
| 7805 | MapValuesArrayTy &Pointers, |
| 7806 | MapValuesArrayTy &Sizes, |
| 7807 | MapFlagsArrayTy &Types) const { |
| 7808 | // Map other list items in the map clause which are not captured variables |
| 7809 | // but "declare target link" global variables., |
| 7810 | for (const auto *C : this->CurDir.getClausesOfKind<OMPMapClause>()) { |
| 7811 | for (const auto &L : C->component_lists()) { |
| 7812 | if (!L.first) |
| 7813 | continue; |
| 7814 | const auto *VD = dyn_cast<VarDecl>(L.first); |
| 7815 | if (!VD) |
| 7816 | continue; |
| 7817 | llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res = |
Alexey Bataev | 97b7221 | 2018-08-14 18:31:20 +0000 | [diff] [blame] | 7818 | OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD); |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7819 | if (!Res || *Res != OMPDeclareTargetDeclAttr::MT_Link) |
| 7820 | continue; |
| 7821 | StructRangeInfoTy PartialStruct; |
| 7822 | generateInfoForComponentList( |
| 7823 | C->getMapType(), C->getMapTypeModifier(), L.second, BasePointers, |
| 7824 | Pointers, Sizes, Types, PartialStruct, |
| 7825 | /*IsFirstComponentList=*/true, C->isImplicit()); |
| 7826 | assert(!PartialStruct.Base.isValid() && |
| 7827 | "No partial structs for declare target link expected."); |
| 7828 | } |
| 7829 | } |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7830 | } |
Samuel Antao | d486f84 | 2016-05-26 16:53:38 +0000 | [diff] [blame] | 7831 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 7832 | /// Generate the default map information for a given capture \a CI, |
Samuel Antao | d486f84 | 2016-05-26 16:53:38 +0000 | [diff] [blame] | 7833 | /// record field declaration \a RI and captured value \a CV. |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7834 | void generateDefaultMapInfo(const CapturedStmt::Capture &CI, |
| 7835 | const FieldDecl &RI, llvm::Value *CV, |
| 7836 | MapBaseValuesArrayTy &CurBasePointers, |
| 7837 | MapValuesArrayTy &CurPointers, |
| 7838 | MapValuesArrayTy &CurSizes, |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7839 | MapFlagsArrayTy &CurMapTypes) const { |
Samuel Antao | d486f84 | 2016-05-26 16:53:38 +0000 | [diff] [blame] | 7840 | // Do the default mapping. |
| 7841 | if (CI.capturesThis()) { |
| 7842 | CurBasePointers.push_back(CV); |
| 7843 | CurPointers.push_back(CV); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 7844 | const auto *PtrTy = cast<PointerType>(RI.getType().getTypePtr()); |
Samuel Antao | d486f84 | 2016-05-26 16:53:38 +0000 | [diff] [blame] | 7845 | CurSizes.push_back(CGF.getTypeSize(PtrTy->getPointeeType())); |
| 7846 | // Default map type. |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7847 | CurMapTypes.push_back(OMP_MAP_TO | OMP_MAP_FROM); |
Samuel Antao | d486f84 | 2016-05-26 16:53:38 +0000 | [diff] [blame] | 7848 | } else if (CI.capturesVariableByCopy()) { |
Samuel Antao | 6d00426 | 2016-06-16 18:39:34 +0000 | [diff] [blame] | 7849 | CurBasePointers.push_back(CV); |
| 7850 | CurPointers.push_back(CV); |
Samuel Antao | d486f84 | 2016-05-26 16:53:38 +0000 | [diff] [blame] | 7851 | if (!RI.getType()->isAnyPointerType()) { |
Samuel Antao | 6d00426 | 2016-06-16 18:39:34 +0000 | [diff] [blame] | 7852 | // We have to signal to the runtime captures passed by value that are |
| 7853 | // not pointers. |
George Rokos | 065755d | 2017-11-07 18:27:04 +0000 | [diff] [blame] | 7854 | CurMapTypes.push_back(OMP_MAP_LITERAL); |
Samuel Antao | d486f84 | 2016-05-26 16:53:38 +0000 | [diff] [blame] | 7855 | CurSizes.push_back(CGF.getTypeSize(RI.getType())); |
| 7856 | } else { |
| 7857 | // Pointers are implicitly mapped with a zero size and no flags |
| 7858 | // (other than first map that is added for all implicit maps). |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7859 | CurMapTypes.push_back(OMP_MAP_NONE); |
Samuel Antao | d486f84 | 2016-05-26 16:53:38 +0000 | [diff] [blame] | 7860 | CurSizes.push_back(llvm::Constant::getNullValue(CGF.SizeTy)); |
| 7861 | } |
| 7862 | } else { |
| 7863 | assert(CI.capturesVariable() && "Expected captured reference."); |
| 7864 | CurBasePointers.push_back(CV); |
| 7865 | CurPointers.push_back(CV); |
| 7866 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 7867 | const auto *PtrTy = cast<ReferenceType>(RI.getType().getTypePtr()); |
Samuel Antao | d486f84 | 2016-05-26 16:53:38 +0000 | [diff] [blame] | 7868 | QualType ElementType = PtrTy->getPointeeType(); |
| 7869 | CurSizes.push_back(CGF.getTypeSize(ElementType)); |
| 7870 | // The default map type for a scalar/complex type is 'to' because by |
| 7871 | // default the value doesn't have to be retrieved. For an aggregate |
| 7872 | // type, the default is 'tofrom'. |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7873 | CurMapTypes.push_back(getMapModifiersForPrivateClauses(CI)); |
Samuel Antao | d486f84 | 2016-05-26 16:53:38 +0000 | [diff] [blame] | 7874 | } |
George Rokos | 065755d | 2017-11-07 18:27:04 +0000 | [diff] [blame] | 7875 | // Every default map produces a single argument which is a target parameter. |
| 7876 | CurMapTypes.back() |= OMP_MAP_TARGET_PARAM; |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7877 | |
| 7878 | // Add flag stating this is an implicit map. |
| 7879 | CurMapTypes.back() |= OMP_MAP_IMPLICIT; |
Samuel Antao | d486f84 | 2016-05-26 16:53:38 +0000 | [diff] [blame] | 7880 | } |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7881 | }; |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 7882 | |
| 7883 | enum OpenMPOffloadingReservedDeviceIDs { |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 7884 | /// Device ID if the device was not defined, runtime should get it |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 7885 | /// from environment variables in the spec. |
| 7886 | OMP_DEVICEID_UNDEF = -1, |
| 7887 | }; |
| 7888 | } // anonymous namespace |
| 7889 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 7890 | /// Emit the arrays used to pass the captures and map information to the |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 7891 | /// offloading runtime library. If there is no map or capture information, |
| 7892 | /// return nullptr by reference. |
| 7893 | static void |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7894 | emitOffloadingArrays(CodeGenFunction &CGF, |
| 7895 | MappableExprsHandler::MapBaseValuesArrayTy &BasePointers, |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 7896 | MappableExprsHandler::MapValuesArrayTy &Pointers, |
| 7897 | MappableExprsHandler::MapValuesArrayTy &Sizes, |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7898 | MappableExprsHandler::MapFlagsArrayTy &MapTypes, |
| 7899 | CGOpenMPRuntime::TargetDataInfo &Info) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 7900 | CodeGenModule &CGM = CGF.CGM; |
| 7901 | ASTContext &Ctx = CGF.getContext(); |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 7902 | |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7903 | // Reset the array information. |
| 7904 | Info.clearArrayInfo(); |
| 7905 | Info.NumberOfPtrs = BasePointers.size(); |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 7906 | |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7907 | if (Info.NumberOfPtrs) { |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 7908 | // Detect if we have any capture size requiring runtime evaluation of the |
| 7909 | // size so that a constant array could be eventually used. |
| 7910 | bool hasRuntimeEvaluationCaptureSize = false; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 7911 | for (llvm::Value *S : Sizes) |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 7912 | if (!isa<llvm::Constant>(S)) { |
| 7913 | hasRuntimeEvaluationCaptureSize = true; |
| 7914 | break; |
| 7915 | } |
| 7916 | |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7917 | llvm::APInt PointerNumAP(32, Info.NumberOfPtrs, /*isSigned=*/true); |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 7918 | QualType PointerArrayType = |
| 7919 | Ctx.getConstantArrayType(Ctx.VoidPtrTy, PointerNumAP, ArrayType::Normal, |
| 7920 | /*IndexTypeQuals=*/0); |
| 7921 | |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7922 | Info.BasePointersArray = |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 7923 | CGF.CreateMemTemp(PointerArrayType, ".offload_baseptrs").getPointer(); |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7924 | Info.PointersArray = |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 7925 | CGF.CreateMemTemp(PointerArrayType, ".offload_ptrs").getPointer(); |
| 7926 | |
| 7927 | // If we don't have any VLA types or other types that require runtime |
| 7928 | // evaluation, we can use a constant array for the map sizes, otherwise we |
| 7929 | // need to fill up the arrays as we do for the pointers. |
| 7930 | if (hasRuntimeEvaluationCaptureSize) { |
| 7931 | QualType SizeArrayType = Ctx.getConstantArrayType( |
| 7932 | Ctx.getSizeType(), PointerNumAP, ArrayType::Normal, |
| 7933 | /*IndexTypeQuals=*/0); |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7934 | Info.SizesArray = |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 7935 | CGF.CreateMemTemp(SizeArrayType, ".offload_sizes").getPointer(); |
| 7936 | } else { |
| 7937 | // We expect all the sizes to be constant, so we collect them to create |
| 7938 | // a constant array. |
| 7939 | SmallVector<llvm::Constant *, 16> ConstSizes; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 7940 | for (llvm::Value *S : Sizes) |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 7941 | ConstSizes.push_back(cast<llvm::Constant>(S)); |
| 7942 | |
| 7943 | auto *SizesArrayInit = llvm::ConstantArray::get( |
| 7944 | llvm::ArrayType::get(CGM.SizeTy, ConstSizes.size()), ConstSizes); |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 7945 | std::string Name = CGM.getOpenMPRuntime().getName({"offload_sizes"}); |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 7946 | auto *SizesArrayGbl = new llvm::GlobalVariable( |
| 7947 | CGM.getModule(), SizesArrayInit->getType(), |
| 7948 | /*isConstant=*/true, llvm::GlobalValue::PrivateLinkage, |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 7949 | SizesArrayInit, Name); |
Peter Collingbourne | bcf909d | 2016-06-14 21:02:05 +0000 | [diff] [blame] | 7950 | SizesArrayGbl->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global); |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7951 | Info.SizesArray = SizesArrayGbl; |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 7952 | } |
| 7953 | |
| 7954 | // The map types are always constant so we don't need to generate code to |
| 7955 | // fill arrays. Instead, we create an array constant. |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7956 | SmallVector<uint64_t, 4> Mapping(MapTypes.size(), 0); |
| 7957 | llvm::copy(MapTypes, Mapping.begin()); |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 7958 | llvm::Constant *MapTypesArrayInit = |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7959 | llvm::ConstantDataArray::get(CGF.Builder.getContext(), Mapping); |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 7960 | std::string MaptypesName = |
| 7961 | CGM.getOpenMPRuntime().getName({"offload_maptypes"}); |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 7962 | auto *MapTypesArrayGbl = new llvm::GlobalVariable( |
| 7963 | CGM.getModule(), MapTypesArrayInit->getType(), |
| 7964 | /*isConstant=*/true, llvm::GlobalValue::PrivateLinkage, |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 7965 | MapTypesArrayInit, MaptypesName); |
Peter Collingbourne | bcf909d | 2016-06-14 21:02:05 +0000 | [diff] [blame] | 7966 | MapTypesArrayGbl->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global); |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7967 | Info.MapTypesArray = MapTypesArrayGbl; |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 7968 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 7969 | for (unsigned I = 0; I < Info.NumberOfPtrs; ++I) { |
| 7970 | llvm::Value *BPVal = *BasePointers[I]; |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 7971 | llvm::Value *BP = CGF.Builder.CreateConstInBoundsGEP2_32( |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7972 | llvm::ArrayType::get(CGM.VoidPtrTy, Info.NumberOfPtrs), |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 7973 | Info.BasePointersArray, 0, I); |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 7974 | BP = CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
| 7975 | BP, BPVal->getType()->getPointerTo(/*AddrSpace=*/0)); |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 7976 | Address BPAddr(BP, Ctx.getTypeAlignInChars(Ctx.VoidPtrTy)); |
| 7977 | CGF.Builder.CreateStore(BPVal, BPAddr); |
| 7978 | |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7979 | if (Info.requiresDevicePointerInfo()) |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 7980 | if (const ValueDecl *DevVD = BasePointers[I].getDevicePtrDecl()) |
Alexey Bataev | 43a919f | 2018-04-13 17:48:43 +0000 | [diff] [blame] | 7981 | Info.CaptureDeviceAddrMap.try_emplace(DevVD, BPAddr); |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7982 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 7983 | llvm::Value *PVal = Pointers[I]; |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 7984 | llvm::Value *P = CGF.Builder.CreateConstInBoundsGEP2_32( |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7985 | llvm::ArrayType::get(CGM.VoidPtrTy, Info.NumberOfPtrs), |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 7986 | Info.PointersArray, 0, I); |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 7987 | P = CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
| 7988 | P, PVal->getType()->getPointerTo(/*AddrSpace=*/0)); |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 7989 | Address PAddr(P, Ctx.getTypeAlignInChars(Ctx.VoidPtrTy)); |
| 7990 | CGF.Builder.CreateStore(PVal, PAddr); |
| 7991 | |
| 7992 | if (hasRuntimeEvaluationCaptureSize) { |
| 7993 | llvm::Value *S = CGF.Builder.CreateConstInBoundsGEP2_32( |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7994 | llvm::ArrayType::get(CGM.SizeTy, Info.NumberOfPtrs), |
| 7995 | Info.SizesArray, |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 7996 | /*Idx0=*/0, |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 7997 | /*Idx1=*/I); |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 7998 | Address SAddr(S, Ctx.getTypeAlignInChars(Ctx.getSizeType())); |
| 7999 | CGF.Builder.CreateStore( |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8000 | CGF.Builder.CreateIntCast(Sizes[I], CGM.SizeTy, /*isSigned=*/true), |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8001 | SAddr); |
| 8002 | } |
| 8003 | } |
| 8004 | } |
| 8005 | } |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 8006 | /// Emit the arguments to be passed to the runtime library based on the |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8007 | /// arrays of pointers, sizes and map types. |
| 8008 | static void emitOffloadingArraysArgument( |
| 8009 | CodeGenFunction &CGF, llvm::Value *&BasePointersArrayArg, |
| 8010 | llvm::Value *&PointersArrayArg, llvm::Value *&SizesArrayArg, |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 8011 | llvm::Value *&MapTypesArrayArg, CGOpenMPRuntime::TargetDataInfo &Info) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8012 | CodeGenModule &CGM = CGF.CGM; |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 8013 | if (Info.NumberOfPtrs) { |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8014 | BasePointersArrayArg = CGF.Builder.CreateConstInBoundsGEP2_32( |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 8015 | llvm::ArrayType::get(CGM.VoidPtrTy, Info.NumberOfPtrs), |
| 8016 | Info.BasePointersArray, |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8017 | /*Idx0=*/0, /*Idx1=*/0); |
| 8018 | PointersArrayArg = CGF.Builder.CreateConstInBoundsGEP2_32( |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 8019 | llvm::ArrayType::get(CGM.VoidPtrTy, Info.NumberOfPtrs), |
| 8020 | Info.PointersArray, |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8021 | /*Idx0=*/0, |
| 8022 | /*Idx1=*/0); |
| 8023 | SizesArrayArg = CGF.Builder.CreateConstInBoundsGEP2_32( |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 8024 | llvm::ArrayType::get(CGM.SizeTy, Info.NumberOfPtrs), Info.SizesArray, |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8025 | /*Idx0=*/0, /*Idx1=*/0); |
| 8026 | MapTypesArrayArg = CGF.Builder.CreateConstInBoundsGEP2_32( |
George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 8027 | llvm::ArrayType::get(CGM.Int64Ty, Info.NumberOfPtrs), |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 8028 | Info.MapTypesArray, |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8029 | /*Idx0=*/0, |
| 8030 | /*Idx1=*/0); |
| 8031 | } else { |
| 8032 | BasePointersArrayArg = llvm::ConstantPointerNull::get(CGM.VoidPtrPtrTy); |
| 8033 | PointersArrayArg = llvm::ConstantPointerNull::get(CGM.VoidPtrPtrTy); |
| 8034 | SizesArrayArg = llvm::ConstantPointerNull::get(CGM.SizeTy->getPointerTo()); |
| 8035 | MapTypesArrayArg = |
George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 8036 | llvm::ConstantPointerNull::get(CGM.Int64Ty->getPointerTo()); |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8037 | } |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 8038 | } |
| 8039 | |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 8040 | void CGOpenMPRuntime::emitTargetCall(CodeGenFunction &CGF, |
| 8041 | const OMPExecutableDirective &D, |
| 8042 | llvm::Value *OutlinedFn, |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 8043 | llvm::Value *OutlinedFnID, |
Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 8044 | const Expr *IfCond, const Expr *Device) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 8045 | if (!CGF.HaveInsertPoint()) |
| 8046 | return; |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 8047 | |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 8048 | assert(OutlinedFn && "Invalid outlined function!"); |
| 8049 | |
Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 8050 | const bool RequiresOuterTask = D.hasClausesOfKind<OMPDependClause>(); |
| 8051 | llvm::SmallVector<llvm::Value *, 16> CapturedVars; |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 8052 | const CapturedStmt &CS = *D.getCapturedStmt(OMPD_target); |
Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 8053 | auto &&ArgsCodegen = [&CS, &CapturedVars](CodeGenFunction &CGF, |
| 8054 | PrePostActionTy &) { |
| 8055 | CGF.GenerateOpenMPCapturedVars(CS, CapturedVars); |
| 8056 | }; |
| 8057 | emitInlinedDirective(CGF, OMPD_unknown, ArgsCodegen); |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 8058 | |
Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 8059 | CodeGenFunction::OMPTargetDataInfo InputInfo; |
| 8060 | llvm::Value *MapTypesArray = nullptr; |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 8061 | // Fill up the pointer arrays and transfer execution to the device. |
Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 8062 | auto &&ThenGen = [this, Device, OutlinedFn, OutlinedFnID, &D, &InputInfo, |
| 8063 | &MapTypesArray, &CS, RequiresOuterTask, |
| 8064 | &CapturedVars](CodeGenFunction &CGF, PrePostActionTy &) { |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 8065 | // On top of the arrays that were filled up, the target offloading call |
| 8066 | // takes as arguments the device id as well as the host pointer. The host |
| 8067 | // pointer is used by the runtime library to identify the current target |
| 8068 | // region, so it only has to be unique and not necessarily point to |
| 8069 | // anything. It could be the pointer to the outlined function that |
| 8070 | // implements the target region, but we aren't using that so that the |
| 8071 | // compiler doesn't need to keep that, and could therefore inline the host |
| 8072 | // function if proven worthwhile during optimization. |
| 8073 | |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 8074 | // From this point on, we need to have an ID of the target region defined. |
| 8075 | assert(OutlinedFnID && "Invalid outlined function ID!"); |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 8076 | |
| 8077 | // Emit device ID if any. |
| 8078 | llvm::Value *DeviceID; |
George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 8079 | if (Device) { |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 8080 | DeviceID = CGF.Builder.CreateIntCast(CGF.EmitScalarExpr(Device), |
George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 8081 | CGF.Int64Ty, /*isSigned=*/true); |
| 8082 | } else { |
| 8083 | DeviceID = CGF.Builder.getInt64(OMP_DEVICEID_UNDEF); |
| 8084 | } |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 8085 | |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8086 | // Emit the number of elements in the offloading arrays. |
Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 8087 | llvm::Value *PointerNum = |
| 8088 | CGF.Builder.getInt32(InputInfo.NumberOfTargetItems); |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8089 | |
Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 8090 | // Return value of the runtime offloading call. |
| 8091 | llvm::Value *Return; |
| 8092 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8093 | llvm::Value *NumTeams = emitNumTeamsForTargetDirective(*this, CGF, D); |
| 8094 | llvm::Value *NumThreads = emitNumThreadsForTargetDirective(*this, CGF, D); |
Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 8095 | |
Alexey Bataev | a9f77c6 | 2017-12-13 21:04:20 +0000 | [diff] [blame] | 8096 | bool HasNowait = D.hasClausesOfKind<OMPNowaitClause>(); |
Arpith Chacko Jacob | 33c849a | 2017-01-25 00:57:16 +0000 | [diff] [blame] | 8097 | // The target region is an outlined function launched by the runtime |
| 8098 | // via calls __tgt_target() or __tgt_target_teams(). |
| 8099 | // |
| 8100 | // __tgt_target() launches a target region with one team and one thread, |
| 8101 | // executing a serial region. This master thread may in turn launch |
| 8102 | // more threads within its team upon encountering a parallel region, |
| 8103 | // however, no additional teams can be launched on the device. |
| 8104 | // |
| 8105 | // __tgt_target_teams() launches a target region with one or more teams, |
| 8106 | // each with one or more threads. This call is required for target |
| 8107 | // constructs such as: |
| 8108 | // 'target teams' |
| 8109 | // 'target' / 'teams' |
| 8110 | // 'target teams distribute parallel for' |
| 8111 | // 'target parallel' |
| 8112 | // and so on. |
| 8113 | // |
| 8114 | // Note that on the host and CPU targets, the runtime implementation of |
| 8115 | // these calls simply call the outlined function without forking threads. |
| 8116 | // The outlined functions themselves have runtime calls to |
| 8117 | // __kmpc_fork_teams() and __kmpc_fork() for this purpose, codegen'd by |
| 8118 | // the compiler in emitTeamsCall() and emitParallelCall(). |
| 8119 | // |
| 8120 | // In contrast, on the NVPTX target, the implementation of |
| 8121 | // __tgt_target_teams() launches a GPU kernel with the requested number |
| 8122 | // 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] | 8123 | if (NumTeams) { |
Arpith Chacko Jacob | 33c849a | 2017-01-25 00:57:16 +0000 | [diff] [blame] | 8124 | // If we have NumTeams defined this means that we have an enclosed teams |
| 8125 | // region. Therefore we also expect to have NumThreads defined. These two |
| 8126 | // values should be defined in the presence of a teams directive, |
| 8127 | // regardless of having any clauses associated. If the user is using teams |
| 8128 | // but no clauses, these two values will be the default that should be |
| 8129 | // passed to the runtime library - a 32-bit integer with the value zero. |
| 8130 | assert(NumThreads && "Thread limit expression should be available along " |
| 8131 | "with number of teams."); |
Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 8132 | llvm::Value *OffloadingArgs[] = {DeviceID, |
| 8133 | OutlinedFnID, |
| 8134 | PointerNum, |
| 8135 | InputInfo.BasePointersArray.getPointer(), |
| 8136 | InputInfo.PointersArray.getPointer(), |
| 8137 | InputInfo.SizesArray.getPointer(), |
| 8138 | MapTypesArray, |
| 8139 | NumTeams, |
| 8140 | NumThreads}; |
Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 8141 | Return = CGF.EmitRuntimeCall( |
Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 8142 | createRuntimeFunction(HasNowait ? OMPRTL__tgt_target_teams_nowait |
| 8143 | : OMPRTL__tgt_target_teams), |
Alexey Bataev | a9f77c6 | 2017-12-13 21:04:20 +0000 | [diff] [blame] | 8144 | OffloadingArgs); |
Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 8145 | } else { |
Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 8146 | llvm::Value *OffloadingArgs[] = {DeviceID, |
| 8147 | OutlinedFnID, |
| 8148 | PointerNum, |
| 8149 | InputInfo.BasePointersArray.getPointer(), |
| 8150 | InputInfo.PointersArray.getPointer(), |
| 8151 | InputInfo.SizesArray.getPointer(), |
| 8152 | MapTypesArray}; |
Alexey Bataev | a9f77c6 | 2017-12-13 21:04:20 +0000 | [diff] [blame] | 8153 | Return = CGF.EmitRuntimeCall( |
Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 8154 | createRuntimeFunction(HasNowait ? OMPRTL__tgt_target_nowait |
| 8155 | : OMPRTL__tgt_target), |
Alexey Bataev | a9f77c6 | 2017-12-13 21:04:20 +0000 | [diff] [blame] | 8156 | OffloadingArgs); |
Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 8157 | } |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 8158 | |
Alexey Bataev | 2a007e0 | 2017-10-02 14:20:58 +0000 | [diff] [blame] | 8159 | // Check the error code and execute the host version if required. |
| 8160 | llvm::BasicBlock *OffloadFailedBlock = |
| 8161 | CGF.createBasicBlock("omp_offload.failed"); |
| 8162 | llvm::BasicBlock *OffloadContBlock = |
| 8163 | CGF.createBasicBlock("omp_offload.cont"); |
| 8164 | llvm::Value *Failed = CGF.Builder.CreateIsNotNull(Return); |
| 8165 | CGF.Builder.CreateCondBr(Failed, OffloadFailedBlock, OffloadContBlock); |
| 8166 | |
| 8167 | CGF.EmitBlock(OffloadFailedBlock); |
Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 8168 | if (RequiresOuterTask) { |
| 8169 | CapturedVars.clear(); |
| 8170 | CGF.GenerateOpenMPCapturedVars(CS, CapturedVars); |
| 8171 | } |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 8172 | emitOutlinedFunctionCall(CGF, D.getBeginLoc(), OutlinedFn, CapturedVars); |
Alexey Bataev | 2a007e0 | 2017-10-02 14:20:58 +0000 | [diff] [blame] | 8173 | CGF.EmitBranch(OffloadContBlock); |
| 8174 | |
| 8175 | CGF.EmitBlock(OffloadContBlock, /*IsFinished=*/true); |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 8176 | }; |
| 8177 | |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 8178 | // Notify that the host version must be executed. |
Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 8179 | auto &&ElseGen = [this, &D, OutlinedFn, &CS, &CapturedVars, |
| 8180 | RequiresOuterTask](CodeGenFunction &CGF, |
| 8181 | PrePostActionTy &) { |
| 8182 | if (RequiresOuterTask) { |
| 8183 | CapturedVars.clear(); |
| 8184 | CGF.GenerateOpenMPCapturedVars(CS, CapturedVars); |
| 8185 | } |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 8186 | emitOutlinedFunctionCall(CGF, D.getBeginLoc(), OutlinedFn, CapturedVars); |
Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 8187 | }; |
| 8188 | |
| 8189 | auto &&TargetThenGen = [this, &ThenGen, &D, &InputInfo, &MapTypesArray, |
| 8190 | &CapturedVars, RequiresOuterTask, |
| 8191 | &CS](CodeGenFunction &CGF, PrePostActionTy &) { |
| 8192 | // Fill up the arrays with all the captured variables. |
| 8193 | MappableExprsHandler::MapBaseValuesArrayTy BasePointers; |
| 8194 | MappableExprsHandler::MapValuesArrayTy Pointers; |
| 8195 | MappableExprsHandler::MapValuesArrayTy Sizes; |
| 8196 | MappableExprsHandler::MapFlagsArrayTy MapTypes; |
| 8197 | |
Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 8198 | // Get mappable expression information. |
| 8199 | MappableExprsHandler MEHandler(D, CGF); |
Alexey Bataev | 969dbc0 | 2018-11-08 15:47:39 +0000 | [diff] [blame] | 8200 | llvm::DenseMap<llvm::Value *, llvm::Value *> LambdaPointers; |
Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 8201 | |
| 8202 | auto RI = CS.getCapturedRecordDecl()->field_begin(); |
| 8203 | auto CV = CapturedVars.begin(); |
| 8204 | for (CapturedStmt::const_capture_iterator CI = CS.capture_begin(), |
| 8205 | CE = CS.capture_end(); |
| 8206 | CI != CE; ++CI, ++RI, ++CV) { |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 8207 | MappableExprsHandler::MapBaseValuesArrayTy CurBasePointers; |
| 8208 | MappableExprsHandler::MapValuesArrayTy CurPointers; |
| 8209 | MappableExprsHandler::MapValuesArrayTy CurSizes; |
| 8210 | MappableExprsHandler::MapFlagsArrayTy CurMapTypes; |
| 8211 | MappableExprsHandler::StructRangeInfoTy PartialStruct; |
Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 8212 | |
| 8213 | // VLA sizes are passed to the outlined region by copy and do not have map |
| 8214 | // information associated. |
| 8215 | if (CI->capturesVariableArrayType()) { |
| 8216 | CurBasePointers.push_back(*CV); |
| 8217 | CurPointers.push_back(*CV); |
| 8218 | CurSizes.push_back(CGF.getTypeSize(RI->getType())); |
| 8219 | // Copy to the device as an argument. No need to retrieve it. |
| 8220 | CurMapTypes.push_back(MappableExprsHandler::OMP_MAP_LITERAL | |
| 8221 | MappableExprsHandler::OMP_MAP_TARGET_PARAM); |
| 8222 | } else { |
| 8223 | // If we have any information in the map clause, we use it, otherwise we |
| 8224 | // just do a default mapping. |
| 8225 | MEHandler.generateInfoForCapture(CI, *CV, CurBasePointers, CurPointers, |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 8226 | CurSizes, CurMapTypes, PartialStruct); |
Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 8227 | if (CurBasePointers.empty()) |
| 8228 | MEHandler.generateDefaultMapInfo(*CI, **RI, *CV, CurBasePointers, |
| 8229 | CurPointers, CurSizes, CurMapTypes); |
Alexey Bataev | 6070542 | 2018-10-30 15:50:12 +0000 | [diff] [blame] | 8230 | // Generate correct mapping for variables captured by reference in |
| 8231 | // lambdas. |
| 8232 | if (CI->capturesVariable()) |
Alexey Bataev | 969dbc0 | 2018-11-08 15:47:39 +0000 | [diff] [blame] | 8233 | MEHandler.generateInfoForLambdaCaptures( |
| 8234 | CI->getCapturedVar(), *CV, CurBasePointers, CurPointers, CurSizes, |
| 8235 | CurMapTypes, LambdaPointers); |
Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 8236 | } |
| 8237 | // We expect to have at least an element of information for this capture. |
| 8238 | assert(!CurBasePointers.empty() && |
| 8239 | "Non-existing map pointer for capture!"); |
| 8240 | assert(CurBasePointers.size() == CurPointers.size() && |
| 8241 | CurBasePointers.size() == CurSizes.size() && |
| 8242 | CurBasePointers.size() == CurMapTypes.size() && |
| 8243 | "Inconsistent map information sizes!"); |
| 8244 | |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 8245 | // If there is an entry in PartialStruct it means we have a struct with |
| 8246 | // individual members mapped. Emit an extra combined entry. |
| 8247 | if (PartialStruct.Base.isValid()) |
| 8248 | MEHandler.emitCombinedEntry(BasePointers, Pointers, Sizes, MapTypes, |
| 8249 | CurMapTypes, PartialStruct); |
| 8250 | |
Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 8251 | // We need to append the results of this capture to what we already have. |
| 8252 | BasePointers.append(CurBasePointers.begin(), CurBasePointers.end()); |
| 8253 | Pointers.append(CurPointers.begin(), CurPointers.end()); |
| 8254 | Sizes.append(CurSizes.begin(), CurSizes.end()); |
| 8255 | MapTypes.append(CurMapTypes.begin(), CurMapTypes.end()); |
| 8256 | } |
Alexey Bataev | 6070542 | 2018-10-30 15:50:12 +0000 | [diff] [blame] | 8257 | // Adjust MEMBER_OF flags for the lambdas captures. |
Alexey Bataev | 969dbc0 | 2018-11-08 15:47:39 +0000 | [diff] [blame] | 8258 | MEHandler.adjustMemberOfForLambdaCaptures(LambdaPointers, BasePointers, |
| 8259 | Pointers, MapTypes); |
Alexey Bataev | 92327c5 | 2018-03-26 16:40:55 +0000 | [diff] [blame] | 8260 | // Map other list items in the map clause which are not captured variables |
| 8261 | // but "declare target link" global variables. |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 8262 | MEHandler.generateInfoForDeclareTargetLink(BasePointers, Pointers, Sizes, |
| 8263 | MapTypes); |
Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 8264 | |
| 8265 | TargetDataInfo Info; |
| 8266 | // Fill up the arrays and create the arguments. |
| 8267 | emitOffloadingArrays(CGF, BasePointers, Pointers, Sizes, MapTypes, Info); |
| 8268 | emitOffloadingArraysArgument(CGF, Info.BasePointersArray, |
| 8269 | Info.PointersArray, Info.SizesArray, |
| 8270 | Info.MapTypesArray, Info); |
| 8271 | InputInfo.NumberOfTargetItems = Info.NumberOfPtrs; |
| 8272 | InputInfo.BasePointersArray = |
| 8273 | Address(Info.BasePointersArray, CGM.getPointerAlign()); |
| 8274 | InputInfo.PointersArray = |
| 8275 | Address(Info.PointersArray, CGM.getPointerAlign()); |
| 8276 | InputInfo.SizesArray = Address(Info.SizesArray, CGM.getPointerAlign()); |
| 8277 | MapTypesArray = Info.MapTypesArray; |
| 8278 | if (RequiresOuterTask) |
| 8279 | CGF.EmitOMPTargetTaskBasedDirective(D, ThenGen, InputInfo); |
| 8280 | else |
| 8281 | emitInlinedDirective(CGF, D.getDirectiveKind(), ThenGen); |
| 8282 | }; |
| 8283 | |
| 8284 | auto &&TargetElseGen = [this, &ElseGen, &D, RequiresOuterTask]( |
| 8285 | CodeGenFunction &CGF, PrePostActionTy &) { |
| 8286 | if (RequiresOuterTask) { |
| 8287 | CodeGenFunction::OMPTargetDataInfo InputInfo; |
| 8288 | CGF.EmitOMPTargetTaskBasedDirective(D, ElseGen, InputInfo); |
| 8289 | } else { |
| 8290 | emitInlinedDirective(CGF, D.getDirectiveKind(), ElseGen); |
| 8291 | } |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 8292 | }; |
| 8293 | |
| 8294 | // If we have a target function ID it means that we need to support |
| 8295 | // offloading, otherwise, just execute on the host. We need to execute on host |
| 8296 | // regardless of the conditional in the if clause if, e.g., the user do not |
| 8297 | // specify target triples. |
| 8298 | if (OutlinedFnID) { |
Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 8299 | if (IfCond) { |
| 8300 | emitOMPIfClause(CGF, IfCond, TargetThenGen, TargetElseGen); |
| 8301 | } else { |
| 8302 | RegionCodeGenTy ThenRCG(TargetThenGen); |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 8303 | ThenRCG(CGF); |
Alexey Bataev | f539faa | 2016-03-28 12:58:34 +0000 | [diff] [blame] | 8304 | } |
| 8305 | } else { |
Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 8306 | RegionCodeGenTy ElseRCG(TargetElseGen); |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 8307 | ElseRCG(CGF); |
Alexey Bataev | f539faa | 2016-03-28 12:58:34 +0000 | [diff] [blame] | 8308 | } |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 8309 | } |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 8310 | |
| 8311 | void CGOpenMPRuntime::scanForTargetRegionsFunctions(const Stmt *S, |
| 8312 | StringRef ParentName) { |
| 8313 | if (!S) |
| 8314 | return; |
| 8315 | |
Arpith Chacko Jacob | 43a8b7b | 2017-01-16 15:26:02 +0000 | [diff] [blame] | 8316 | // Codegen OMP target directives that offload compute to the device. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8317 | bool RequiresDeviceCodegen = |
Arpith Chacko Jacob | 43a8b7b | 2017-01-16 15:26:02 +0000 | [diff] [blame] | 8318 | isa<OMPExecutableDirective>(S) && |
| 8319 | isOpenMPTargetExecutionDirective( |
| 8320 | cast<OMPExecutableDirective>(S)->getDirectiveKind()); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 8321 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8322 | if (RequiresDeviceCodegen) { |
| 8323 | const auto &E = *cast<OMPExecutableDirective>(S); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 8324 | unsigned DeviceID; |
| 8325 | unsigned FileID; |
| 8326 | unsigned Line; |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 8327 | getTargetEntryUniqueInfo(CGM.getContext(), E.getBeginLoc(), DeviceID, |
Samuel Antao | 2de62b0 | 2016-02-13 23:35:10 +0000 | [diff] [blame] | 8328 | FileID, Line); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 8329 | |
| 8330 | // Is this a target region that should not be emitted as an entry point? If |
| 8331 | // so just signal we are done with this target region. |
Samuel Antao | 2de62b0 | 2016-02-13 23:35:10 +0000 | [diff] [blame] | 8332 | if (!OffloadEntriesInfoManager.hasTargetRegionEntryInfo(DeviceID, FileID, |
| 8333 | ParentName, Line)) |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 8334 | return; |
| 8335 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8336 | switch (E.getDirectiveKind()) { |
| 8337 | case OMPD_target: |
| 8338 | CodeGenFunction::EmitOMPTargetDeviceFunction(CGM, ParentName, |
| 8339 | cast<OMPTargetDirective>(E)); |
Arpith Chacko Jacob | 43a8b7b | 2017-01-16 15:26:02 +0000 | [diff] [blame] | 8340 | break; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8341 | case OMPD_target_parallel: |
Arpith Chacko Jacob | 19b911c | 2017-01-18 18:18:53 +0000 | [diff] [blame] | 8342 | CodeGenFunction::EmitOMPTargetParallelDeviceFunction( |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8343 | CGM, ParentName, cast<OMPTargetParallelDirective>(E)); |
Arpith Chacko Jacob | 19b911c | 2017-01-18 18:18:53 +0000 | [diff] [blame] | 8344 | break; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8345 | case OMPD_target_teams: |
Arpith Chacko Jacob | 99a1e0e | 2017-01-25 02:18:43 +0000 | [diff] [blame] | 8346 | CodeGenFunction::EmitOMPTargetTeamsDeviceFunction( |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8347 | CGM, ParentName, cast<OMPTargetTeamsDirective>(E)); |
Arpith Chacko Jacob | 99a1e0e | 2017-01-25 02:18:43 +0000 | [diff] [blame] | 8348 | break; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8349 | case OMPD_target_teams_distribute: |
Alexey Bataev | dfa430f | 2017-12-08 15:03:50 +0000 | [diff] [blame] | 8350 | CodeGenFunction::EmitOMPTargetTeamsDistributeDeviceFunction( |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8351 | CGM, ParentName, cast<OMPTargetTeamsDistributeDirective>(E)); |
Alexey Bataev | dfa430f | 2017-12-08 15:03:50 +0000 | [diff] [blame] | 8352 | break; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8353 | case OMPD_target_teams_distribute_simd: |
Alexey Bataev | fbe17fb | 2017-12-13 19:45:06 +0000 | [diff] [blame] | 8354 | CodeGenFunction::EmitOMPTargetTeamsDistributeSimdDeviceFunction( |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8355 | CGM, ParentName, cast<OMPTargetTeamsDistributeSimdDirective>(E)); |
Alexey Bataev | fbe17fb | 2017-12-13 19:45:06 +0000 | [diff] [blame] | 8356 | break; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8357 | case OMPD_target_parallel_for: |
Alexey Bataev | fb0ebec | 2017-11-08 20:16:14 +0000 | [diff] [blame] | 8358 | CodeGenFunction::EmitOMPTargetParallelForDeviceFunction( |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8359 | CGM, ParentName, cast<OMPTargetParallelForDirective>(E)); |
Alexey Bataev | fb0ebec | 2017-11-08 20:16:14 +0000 | [diff] [blame] | 8360 | break; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8361 | case OMPD_target_parallel_for_simd: |
Alexey Bataev | 5d7edca | 2017-11-09 17:32:15 +0000 | [diff] [blame] | 8362 | CodeGenFunction::EmitOMPTargetParallelForSimdDeviceFunction( |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8363 | CGM, ParentName, cast<OMPTargetParallelForSimdDirective>(E)); |
Alexey Bataev | 5d7edca | 2017-11-09 17:32:15 +0000 | [diff] [blame] | 8364 | break; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8365 | case OMPD_target_simd: |
Alexey Bataev | f836537 | 2017-11-17 17:57:25 +0000 | [diff] [blame] | 8366 | CodeGenFunction::EmitOMPTargetSimdDeviceFunction( |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8367 | CGM, ParentName, cast<OMPTargetSimdDirective>(E)); |
Alexey Bataev | f836537 | 2017-11-17 17:57:25 +0000 | [diff] [blame] | 8368 | break; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8369 | case OMPD_target_teams_distribute_parallel_for: |
Carlo Bertolli | 52978c3 | 2018-01-03 21:12:44 +0000 | [diff] [blame] | 8370 | CodeGenFunction::EmitOMPTargetTeamsDistributeParallelForDeviceFunction( |
| 8371 | CGM, ParentName, |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8372 | cast<OMPTargetTeamsDistributeParallelForDirective>(E)); |
Carlo Bertolli | 52978c3 | 2018-01-03 21:12:44 +0000 | [diff] [blame] | 8373 | break; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8374 | case OMPD_target_teams_distribute_parallel_for_simd: |
Alexey Bataev | 647dd84 | 2018-01-15 20:59:40 +0000 | [diff] [blame] | 8375 | CodeGenFunction:: |
| 8376 | EmitOMPTargetTeamsDistributeParallelForSimdDeviceFunction( |
| 8377 | CGM, ParentName, |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8378 | cast<OMPTargetTeamsDistributeParallelForSimdDirective>(E)); |
Alexey Bataev | 647dd84 | 2018-01-15 20:59:40 +0000 | [diff] [blame] | 8379 | break; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8380 | case OMPD_parallel: |
| 8381 | case OMPD_for: |
| 8382 | case OMPD_parallel_for: |
| 8383 | case OMPD_parallel_sections: |
| 8384 | case OMPD_for_simd: |
| 8385 | case OMPD_parallel_for_simd: |
| 8386 | case OMPD_cancel: |
| 8387 | case OMPD_cancellation_point: |
| 8388 | case OMPD_ordered: |
| 8389 | case OMPD_threadprivate: |
| 8390 | case OMPD_task: |
| 8391 | case OMPD_simd: |
| 8392 | case OMPD_sections: |
| 8393 | case OMPD_section: |
| 8394 | case OMPD_single: |
| 8395 | case OMPD_master: |
| 8396 | case OMPD_critical: |
| 8397 | case OMPD_taskyield: |
| 8398 | case OMPD_barrier: |
| 8399 | case OMPD_taskwait: |
| 8400 | case OMPD_taskgroup: |
| 8401 | case OMPD_atomic: |
| 8402 | case OMPD_flush: |
| 8403 | case OMPD_teams: |
| 8404 | case OMPD_target_data: |
| 8405 | case OMPD_target_exit_data: |
| 8406 | case OMPD_target_enter_data: |
| 8407 | case OMPD_distribute: |
| 8408 | case OMPD_distribute_simd: |
| 8409 | case OMPD_distribute_parallel_for: |
| 8410 | case OMPD_distribute_parallel_for_simd: |
| 8411 | case OMPD_teams_distribute: |
| 8412 | case OMPD_teams_distribute_simd: |
| 8413 | case OMPD_teams_distribute_parallel_for: |
| 8414 | case OMPD_teams_distribute_parallel_for_simd: |
| 8415 | case OMPD_target_update: |
| 8416 | case OMPD_declare_simd: |
| 8417 | case OMPD_declare_target: |
| 8418 | case OMPD_end_declare_target: |
| 8419 | case OMPD_declare_reduction: |
| 8420 | case OMPD_taskloop: |
| 8421 | case OMPD_taskloop_simd: |
Kelvin Li | 1408f91 | 2018-09-26 04:28:39 +0000 | [diff] [blame] | 8422 | case OMPD_requires: |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8423 | case OMPD_unknown: |
Arpith Chacko Jacob | 43a8b7b | 2017-01-16 15:26:02 +0000 | [diff] [blame] | 8424 | llvm_unreachable("Unknown target directive for OpenMP device codegen."); |
| 8425 | } |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 8426 | return; |
| 8427 | } |
| 8428 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8429 | if (const auto *E = dyn_cast<OMPExecutableDirective>(S)) { |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 8430 | if (!E->hasAssociatedStmt() || !E->getAssociatedStmt()) |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 8431 | return; |
| 8432 | |
| 8433 | scanForTargetRegionsFunctions( |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 8434 | E->getInnermostCapturedStmt()->getCapturedStmt(), ParentName); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 8435 | return; |
| 8436 | } |
| 8437 | |
| 8438 | // If this is a lambda function, look into its body. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8439 | if (const auto *L = dyn_cast<LambdaExpr>(S)) |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 8440 | S = L->getBody(); |
| 8441 | |
| 8442 | // Keep looking for target regions recursively. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8443 | for (const Stmt *II : S->children()) |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 8444 | scanForTargetRegionsFunctions(II, ParentName); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 8445 | } |
| 8446 | |
| 8447 | bool CGOpenMPRuntime::emitTargetFunctions(GlobalDecl GD) { |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 8448 | // If emitting code for the host, we do not process FD here. Instead we do |
| 8449 | // the normal code generation. |
| 8450 | if (!CGM.getLangOpts().OpenMPIsDevice) |
| 8451 | return false; |
| 8452 | |
Alexey Bataev | e6aa469 | 2018-09-13 16:54:05 +0000 | [diff] [blame] | 8453 | const ValueDecl *VD = cast<ValueDecl>(GD.getDecl()); |
Alexey Bataev | 2a6f3f5 | 2018-11-07 19:11:14 +0000 | [diff] [blame] | 8454 | StringRef Name = CGM.getMangledName(GD); |
| 8455 | // Try to detect target regions in the function. |
Alexey Bataev | e6aa469 | 2018-09-13 16:54:05 +0000 | [diff] [blame] | 8456 | if (const auto *FD = dyn_cast<FunctionDecl>(VD)) |
Alexey Bataev | 2a6f3f5 | 2018-11-07 19:11:14 +0000 | [diff] [blame] | 8457 | scanForTargetRegionsFunctions(FD->getBody(), Name); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 8458 | |
Alexey Bataev | 4f4bf7c | 2018-03-15 15:47:20 +0000 | [diff] [blame] | 8459 | // Do not to emit function if it is not marked as declare target. |
Alexey Bataev | e6aa469 | 2018-09-13 16:54:05 +0000 | [diff] [blame] | 8460 | return !OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD) && |
Alexey Bataev | 2a6f3f5 | 2018-11-07 19:11:14 +0000 | [diff] [blame] | 8461 | AlreadyEmittedTargetFunctions.count(Name) == 0; |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 8462 | } |
| 8463 | |
| 8464 | bool CGOpenMPRuntime::emitTargetGlobalVariable(GlobalDecl GD) { |
| 8465 | if (!CGM.getLangOpts().OpenMPIsDevice) |
| 8466 | return false; |
| 8467 | |
| 8468 | // Check if there are Ctors/Dtors in this declaration and look for target |
| 8469 | // regions in it. We use the complete variant to produce the kernel name |
| 8470 | // mangling. |
| 8471 | QualType RDTy = cast<VarDecl>(GD.getDecl())->getType(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8472 | if (const auto *RD = RDTy->getBaseElementTypeUnsafe()->getAsCXXRecordDecl()) { |
| 8473 | for (const CXXConstructorDecl *Ctor : RD->ctors()) { |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 8474 | StringRef ParentName = |
| 8475 | CGM.getMangledName(GlobalDecl(Ctor, Ctor_Complete)); |
| 8476 | scanForTargetRegionsFunctions(Ctor->getBody(), ParentName); |
| 8477 | } |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8478 | if (const CXXDestructorDecl *Dtor = RD->getDestructor()) { |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 8479 | StringRef ParentName = |
| 8480 | CGM.getMangledName(GlobalDecl(Dtor, Dtor_Complete)); |
| 8481 | scanForTargetRegionsFunctions(Dtor->getBody(), ParentName); |
| 8482 | } |
| 8483 | } |
| 8484 | |
Alexey Bataev | 4f4bf7c | 2018-03-15 15:47:20 +0000 | [diff] [blame] | 8485 | // 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] | 8486 | llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res = |
Alexey Bataev | 97b7221 | 2018-08-14 18:31:20 +0000 | [diff] [blame] | 8487 | OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration( |
| 8488 | cast<VarDecl>(GD.getDecl())); |
Alexey Bataev | bf8fe71 | 2018-08-07 16:14:36 +0000 | [diff] [blame] | 8489 | if (!Res || *Res == OMPDeclareTargetDeclAttr::MT_Link) { |
Alexey Bataev | d01b749 | 2018-08-15 19:45:12 +0000 | [diff] [blame] | 8490 | DeferredGlobalVariables.insert(cast<VarDecl>(GD.getDecl())); |
Alexey Bataev | bf8fe71 | 2018-08-07 16:14:36 +0000 | [diff] [blame] | 8491 | return true; |
| 8492 | } |
| 8493 | return false; |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 8494 | } |
| 8495 | |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 8496 | void CGOpenMPRuntime::registerTargetGlobalVariable(const VarDecl *VD, |
| 8497 | llvm::Constant *Addr) { |
Alexey Bataev | 2a6f3f5 | 2018-11-07 19:11:14 +0000 | [diff] [blame] | 8498 | llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res = |
| 8499 | OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD); |
| 8500 | if (!Res) { |
| 8501 | if (CGM.getLangOpts().OpenMPIsDevice) { |
| 8502 | // Register non-target variables being emitted in device code (debug info |
| 8503 | // may cause this). |
| 8504 | StringRef VarName = CGM.getMangledName(VD); |
| 8505 | EmittedNonTargetVariables.try_emplace(VarName, Addr); |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 8506 | } |
Alexey Bataev | 2a6f3f5 | 2018-11-07 19:11:14 +0000 | [diff] [blame] | 8507 | return; |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 8508 | } |
Alexey Bataev | 2a6f3f5 | 2018-11-07 19:11:14 +0000 | [diff] [blame] | 8509 | // Register declare target variables. |
| 8510 | OffloadEntriesInfoManagerTy::OMPTargetGlobalVarEntryKind Flags; |
| 8511 | StringRef VarName; |
| 8512 | CharUnits VarSize; |
| 8513 | llvm::GlobalValue::LinkageTypes Linkage; |
| 8514 | switch (*Res) { |
| 8515 | case OMPDeclareTargetDeclAttr::MT_To: |
| 8516 | Flags = OffloadEntriesInfoManagerTy::OMPTargetGlobalVarEntryTo; |
| 8517 | VarName = CGM.getMangledName(VD); |
| 8518 | if (VD->hasDefinition(CGM.getContext()) != VarDecl::DeclarationOnly) { |
| 8519 | VarSize = CGM.getContext().getTypeSizeInChars(VD->getType()); |
| 8520 | assert(!VarSize.isZero() && "Expected non-zero size of the variable"); |
| 8521 | } else { |
| 8522 | VarSize = CharUnits::Zero(); |
| 8523 | } |
| 8524 | Linkage = CGM.getLLVMLinkageVarDefinition(VD, /*IsConstant=*/false); |
| 8525 | // Temp solution to prevent optimizations of the internal variables. |
| 8526 | if (CGM.getLangOpts().OpenMPIsDevice && !VD->isExternallyVisible()) { |
| 8527 | std::string RefName = getName({VarName, "ref"}); |
| 8528 | if (!CGM.GetGlobalValue(RefName)) { |
| 8529 | llvm::Constant *AddrRef = |
| 8530 | getOrCreateInternalVariable(Addr->getType(), RefName); |
| 8531 | auto *GVAddrRef = cast<llvm::GlobalVariable>(AddrRef); |
| 8532 | GVAddrRef->setConstant(/*Val=*/true); |
| 8533 | GVAddrRef->setLinkage(llvm::GlobalValue::InternalLinkage); |
| 8534 | GVAddrRef->setInitializer(Addr); |
| 8535 | CGM.addCompilerUsedGlobal(GVAddrRef); |
| 8536 | } |
| 8537 | } |
| 8538 | break; |
| 8539 | case OMPDeclareTargetDeclAttr::MT_Link: |
| 8540 | Flags = OffloadEntriesInfoManagerTy::OMPTargetGlobalVarEntryLink; |
| 8541 | if (CGM.getLangOpts().OpenMPIsDevice) { |
| 8542 | VarName = Addr->getName(); |
| 8543 | Addr = nullptr; |
| 8544 | } else { |
| 8545 | VarName = getAddrOfDeclareTargetLink(VD).getName(); |
| 8546 | Addr = cast<llvm::Constant>(getAddrOfDeclareTargetLink(VD).getPointer()); |
| 8547 | } |
| 8548 | VarSize = CGM.getPointerSize(); |
| 8549 | Linkage = llvm::GlobalValue::WeakAnyLinkage; |
| 8550 | break; |
| 8551 | } |
| 8552 | OffloadEntriesInfoManager.registerDeviceGlobalVarEntryInfo( |
| 8553 | VarName, Addr, VarSize, Flags, Linkage); |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 8554 | } |
| 8555 | |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 8556 | bool CGOpenMPRuntime::emitTargetGlobal(GlobalDecl GD) { |
Alexey Bataev | e6aa469 | 2018-09-13 16:54:05 +0000 | [diff] [blame] | 8557 | if (isa<FunctionDecl>(GD.getDecl()) || |
| 8558 | isa<OMPDeclareReductionDecl>(GD.getDecl())) |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 8559 | return emitTargetFunctions(GD); |
| 8560 | |
| 8561 | return emitTargetGlobalVariable(GD); |
| 8562 | } |
| 8563 | |
Alexey Bataev | bf8fe71 | 2018-08-07 16:14:36 +0000 | [diff] [blame] | 8564 | void CGOpenMPRuntime::emitDeferredTargetDecls() const { |
| 8565 | for (const VarDecl *VD : DeferredGlobalVariables) { |
| 8566 | llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res = |
Alexey Bataev | 97b7221 | 2018-08-14 18:31:20 +0000 | [diff] [blame] | 8567 | OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD); |
Alexey Bataev | d01b749 | 2018-08-15 19:45:12 +0000 | [diff] [blame] | 8568 | if (!Res) |
| 8569 | continue; |
| 8570 | if (*Res == OMPDeclareTargetDeclAttr::MT_To) { |
Alexey Bataev | bf8fe71 | 2018-08-07 16:14:36 +0000 | [diff] [blame] | 8571 | CGM.EmitGlobal(VD); |
Alexey Bataev | d01b749 | 2018-08-15 19:45:12 +0000 | [diff] [blame] | 8572 | } else { |
| 8573 | assert(*Res == OMPDeclareTargetDeclAttr::MT_Link && |
| 8574 | "Expected to or link clauses."); |
| 8575 | (void)CGM.getOpenMPRuntime().getAddrOfDeclareTargetLink(VD); |
Alexey Bataev | bf8fe71 | 2018-08-07 16:14:36 +0000 | [diff] [blame] | 8576 | } |
| 8577 | } |
| 8578 | } |
| 8579 | |
Alexey Bataev | 6070542 | 2018-10-30 15:50:12 +0000 | [diff] [blame] | 8580 | void CGOpenMPRuntime::adjustTargetSpecificDataForLambdas( |
| 8581 | CodeGenFunction &CGF, const OMPExecutableDirective &D) const { |
| 8582 | assert(isOpenMPTargetExecutionDirective(D.getDirectiveKind()) && |
| 8583 | " Expected target-based directive."); |
| 8584 | } |
| 8585 | |
Alexey Bataev | 4f4bf7c | 2018-03-15 15:47:20 +0000 | [diff] [blame] | 8586 | CGOpenMPRuntime::DisableAutoDeclareTargetRAII::DisableAutoDeclareTargetRAII( |
| 8587 | CodeGenModule &CGM) |
| 8588 | : CGM(CGM) { |
| 8589 | if (CGM.getLangOpts().OpenMPIsDevice) { |
| 8590 | SavedShouldMarkAsGlobal = CGM.getOpenMPRuntime().ShouldMarkAsGlobal; |
| 8591 | CGM.getOpenMPRuntime().ShouldMarkAsGlobal = false; |
| 8592 | } |
| 8593 | } |
| 8594 | |
| 8595 | CGOpenMPRuntime::DisableAutoDeclareTargetRAII::~DisableAutoDeclareTargetRAII() { |
| 8596 | if (CGM.getLangOpts().OpenMPIsDevice) |
| 8597 | CGM.getOpenMPRuntime().ShouldMarkAsGlobal = SavedShouldMarkAsGlobal; |
| 8598 | } |
| 8599 | |
Alexey Bataev | 6d94410 | 2018-05-02 15:45:28 +0000 | [diff] [blame] | 8600 | bool CGOpenMPRuntime::markAsGlobalTarget(GlobalDecl GD) { |
Alexey Bataev | 4f4bf7c | 2018-03-15 15:47:20 +0000 | [diff] [blame] | 8601 | if (!CGM.getLangOpts().OpenMPIsDevice || !ShouldMarkAsGlobal) |
| 8602 | return true; |
Alexey Bataev | 4f4bf7c | 2018-03-15 15:47:20 +0000 | [diff] [blame] | 8603 | |
Alexey Bataev | 2a6f3f5 | 2018-11-07 19:11:14 +0000 | [diff] [blame] | 8604 | StringRef Name = CGM.getMangledName(GD); |
Alexey Bataev | 6d94410 | 2018-05-02 15:45:28 +0000 | [diff] [blame] | 8605 | const auto *D = cast<FunctionDecl>(GD.getDecl()); |
Alexey Bataev | 34f8a70 | 2018-03-28 14:28:54 +0000 | [diff] [blame] | 8606 | // Do not to emit function if it is marked as declare target as it was already |
| 8607 | // emitted. |
Alexey Bataev | 97b7221 | 2018-08-14 18:31:20 +0000 | [diff] [blame] | 8608 | if (OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(D)) { |
Alexey Bataev | 2a6f3f5 | 2018-11-07 19:11:14 +0000 | [diff] [blame] | 8609 | if (D->hasBody() && AlreadyEmittedTargetFunctions.count(Name) == 0) { |
| 8610 | if (auto *F = dyn_cast_or_null<llvm::Function>(CGM.GetGlobalValue(Name))) |
Alexey Bataev | 34f8a70 | 2018-03-28 14:28:54 +0000 | [diff] [blame] | 8611 | return !F->isDeclaration(); |
| 8612 | return false; |
| 8613 | } |
| 8614 | return true; |
| 8615 | } |
| 8616 | |
Alexey Bataev | 2a6f3f5 | 2018-11-07 19:11:14 +0000 | [diff] [blame] | 8617 | return !AlreadyEmittedTargetFunctions.insert(Name).second; |
Alexey Bataev | 4f4bf7c | 2018-03-15 15:47:20 +0000 | [diff] [blame] | 8618 | } |
| 8619 | |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 8620 | llvm::Function *CGOpenMPRuntime::emitRegistrationFunction() { |
| 8621 | // If we have offloading in the current module, we need to emit the entries |
| 8622 | // now and register the offloading descriptor. |
| 8623 | createOffloadEntriesAndInfoMetadata(); |
| 8624 | |
| 8625 | // Create and register the offloading binary descriptors. This is the main |
| 8626 | // entity that captures all the information about offloading in the current |
| 8627 | // compilation unit. |
| 8628 | return createOffloadingBinaryDescriptorRegistration(); |
| 8629 | } |
Carlo Bertolli | 430d8ec | 2016-03-03 20:34:23 +0000 | [diff] [blame] | 8630 | |
| 8631 | void CGOpenMPRuntime::emitTeamsCall(CodeGenFunction &CGF, |
| 8632 | const OMPExecutableDirective &D, |
| 8633 | SourceLocation Loc, |
| 8634 | llvm::Value *OutlinedFn, |
| 8635 | ArrayRef<llvm::Value *> CapturedVars) { |
| 8636 | if (!CGF.HaveInsertPoint()) |
| 8637 | return; |
| 8638 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8639 | llvm::Value *RTLoc = emitUpdateLocation(CGF, Loc); |
Carlo Bertolli | 430d8ec | 2016-03-03 20:34:23 +0000 | [diff] [blame] | 8640 | CodeGenFunction::RunCleanupsScope Scope(CGF); |
| 8641 | |
| 8642 | // Build call __kmpc_fork_teams(loc, n, microtask, var1, .., varn); |
| 8643 | llvm::Value *Args[] = { |
| 8644 | RTLoc, |
| 8645 | CGF.Builder.getInt32(CapturedVars.size()), // Number of captured vars |
| 8646 | CGF.Builder.CreateBitCast(OutlinedFn, getKmpc_MicroPointerTy())}; |
| 8647 | llvm::SmallVector<llvm::Value *, 16> RealArgs; |
| 8648 | RealArgs.append(std::begin(Args), std::end(Args)); |
| 8649 | RealArgs.append(CapturedVars.begin(), CapturedVars.end()); |
| 8650 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8651 | llvm::Value *RTLFn = createRuntimeFunction(OMPRTL__kmpc_fork_teams); |
Carlo Bertolli | 430d8ec | 2016-03-03 20:34:23 +0000 | [diff] [blame] | 8652 | CGF.EmitRuntimeCall(RTLFn, RealArgs); |
| 8653 | } |
| 8654 | |
| 8655 | void CGOpenMPRuntime::emitNumTeamsClause(CodeGenFunction &CGF, |
Carlo Bertolli | c687225 | 2016-04-04 15:55:02 +0000 | [diff] [blame] | 8656 | const Expr *NumTeams, |
| 8657 | const Expr *ThreadLimit, |
Carlo Bertolli | 430d8ec | 2016-03-03 20:34:23 +0000 | [diff] [blame] | 8658 | SourceLocation Loc) { |
| 8659 | if (!CGF.HaveInsertPoint()) |
| 8660 | return; |
| 8661 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8662 | llvm::Value *RTLoc = emitUpdateLocation(CGF, Loc); |
Carlo Bertolli | 430d8ec | 2016-03-03 20:34:23 +0000 | [diff] [blame] | 8663 | |
Carlo Bertolli | c687225 | 2016-04-04 15:55:02 +0000 | [diff] [blame] | 8664 | llvm::Value *NumTeamsVal = |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8665 | NumTeams |
Carlo Bertolli | c687225 | 2016-04-04 15:55:02 +0000 | [diff] [blame] | 8666 | ? CGF.Builder.CreateIntCast(CGF.EmitScalarExpr(NumTeams), |
| 8667 | CGF.CGM.Int32Ty, /* isSigned = */ true) |
| 8668 | : CGF.Builder.getInt32(0); |
| 8669 | |
| 8670 | llvm::Value *ThreadLimitVal = |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8671 | ThreadLimit |
Carlo Bertolli | c687225 | 2016-04-04 15:55:02 +0000 | [diff] [blame] | 8672 | ? CGF.Builder.CreateIntCast(CGF.EmitScalarExpr(ThreadLimit), |
| 8673 | CGF.CGM.Int32Ty, /* isSigned = */ true) |
| 8674 | : CGF.Builder.getInt32(0); |
| 8675 | |
Carlo Bertolli | 430d8ec | 2016-03-03 20:34:23 +0000 | [diff] [blame] | 8676 | // 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] | 8677 | llvm::Value *PushNumTeamsArgs[] = {RTLoc, getThreadID(CGF, Loc), NumTeamsVal, |
| 8678 | ThreadLimitVal}; |
Carlo Bertolli | 430d8ec | 2016-03-03 20:34:23 +0000 | [diff] [blame] | 8679 | CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_push_num_teams), |
| 8680 | PushNumTeamsArgs); |
| 8681 | } |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8682 | |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 8683 | void CGOpenMPRuntime::emitTargetDataCalls( |
| 8684 | CodeGenFunction &CGF, const OMPExecutableDirective &D, const Expr *IfCond, |
| 8685 | const Expr *Device, const RegionCodeGenTy &CodeGen, TargetDataInfo &Info) { |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8686 | if (!CGF.HaveInsertPoint()) |
| 8687 | return; |
| 8688 | |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 8689 | // Action used to replace the default codegen action and turn privatization |
| 8690 | // off. |
| 8691 | PrePostActionTy NoPrivAction; |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8692 | |
| 8693 | // Generate the code for the opening of the data environment. Capture all the |
| 8694 | // arguments of the runtime call by reference because they are used in the |
| 8695 | // closing of the region. |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 8696 | auto &&BeginThenGen = [this, &D, Device, &Info, |
| 8697 | &CodeGen](CodeGenFunction &CGF, PrePostActionTy &) { |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8698 | // Fill up the arrays with all the mapped variables. |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 8699 | MappableExprsHandler::MapBaseValuesArrayTy BasePointers; |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8700 | MappableExprsHandler::MapValuesArrayTy Pointers; |
| 8701 | MappableExprsHandler::MapValuesArrayTy Sizes; |
| 8702 | MappableExprsHandler::MapFlagsArrayTy MapTypes; |
| 8703 | |
| 8704 | // Get map clause information. |
| 8705 | MappableExprsHandler MCHandler(D, CGF); |
| 8706 | MCHandler.generateAllInfo(BasePointers, Pointers, Sizes, MapTypes); |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8707 | |
| 8708 | // Fill up the arrays and create the arguments. |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 8709 | emitOffloadingArrays(CGF, BasePointers, Pointers, Sizes, MapTypes, Info); |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8710 | |
| 8711 | llvm::Value *BasePointersArrayArg = nullptr; |
| 8712 | llvm::Value *PointersArrayArg = nullptr; |
| 8713 | llvm::Value *SizesArrayArg = nullptr; |
| 8714 | llvm::Value *MapTypesArrayArg = nullptr; |
| 8715 | emitOffloadingArraysArgument(CGF, BasePointersArrayArg, PointersArrayArg, |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 8716 | SizesArrayArg, MapTypesArrayArg, Info); |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8717 | |
| 8718 | // Emit device ID if any. |
| 8719 | llvm::Value *DeviceID = nullptr; |
George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 8720 | if (Device) { |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8721 | DeviceID = CGF.Builder.CreateIntCast(CGF.EmitScalarExpr(Device), |
George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 8722 | CGF.Int64Ty, /*isSigned=*/true); |
| 8723 | } else { |
| 8724 | DeviceID = CGF.Builder.getInt64(OMP_DEVICEID_UNDEF); |
| 8725 | } |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8726 | |
| 8727 | // Emit the number of elements in the offloading arrays. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8728 | llvm::Value *PointerNum = CGF.Builder.getInt32(Info.NumberOfPtrs); |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8729 | |
| 8730 | llvm::Value *OffloadingArgs[] = { |
| 8731 | DeviceID, PointerNum, BasePointersArrayArg, |
| 8732 | PointersArrayArg, SizesArrayArg, MapTypesArrayArg}; |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 8733 | CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__tgt_target_data_begin), |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8734 | OffloadingArgs); |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 8735 | |
| 8736 | // If device pointer privatization is required, emit the body of the region |
| 8737 | // here. It will have to be duplicated: with and without privatization. |
| 8738 | if (!Info.CaptureDeviceAddrMap.empty()) |
| 8739 | CodeGen(CGF); |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8740 | }; |
| 8741 | |
| 8742 | // Generate code for the closing of the data region. |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 8743 | auto &&EndThenGen = [this, Device, &Info](CodeGenFunction &CGF, |
| 8744 | PrePostActionTy &) { |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 8745 | assert(Info.isValid() && "Invalid data environment closing arguments."); |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8746 | |
| 8747 | llvm::Value *BasePointersArrayArg = nullptr; |
| 8748 | llvm::Value *PointersArrayArg = nullptr; |
| 8749 | llvm::Value *SizesArrayArg = nullptr; |
| 8750 | llvm::Value *MapTypesArrayArg = nullptr; |
| 8751 | emitOffloadingArraysArgument(CGF, BasePointersArrayArg, PointersArrayArg, |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 8752 | SizesArrayArg, MapTypesArrayArg, Info); |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8753 | |
| 8754 | // Emit device ID if any. |
| 8755 | llvm::Value *DeviceID = nullptr; |
George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 8756 | if (Device) { |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8757 | DeviceID = CGF.Builder.CreateIntCast(CGF.EmitScalarExpr(Device), |
George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 8758 | CGF.Int64Ty, /*isSigned=*/true); |
| 8759 | } else { |
| 8760 | DeviceID = CGF.Builder.getInt64(OMP_DEVICEID_UNDEF); |
| 8761 | } |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8762 | |
| 8763 | // Emit the number of elements in the offloading arrays. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8764 | llvm::Value *PointerNum = CGF.Builder.getInt32(Info.NumberOfPtrs); |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8765 | |
| 8766 | llvm::Value *OffloadingArgs[] = { |
| 8767 | DeviceID, PointerNum, BasePointersArrayArg, |
| 8768 | PointersArrayArg, SizesArrayArg, MapTypesArrayArg}; |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 8769 | CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__tgt_target_data_end), |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8770 | OffloadingArgs); |
| 8771 | }; |
| 8772 | |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 8773 | // If we need device pointer privatization, we need to emit the body of the |
| 8774 | // region with no privatization in the 'else' branch of the conditional. |
| 8775 | // Otherwise, we don't have to do anything. |
| 8776 | auto &&BeginElseGen = [&Info, &CodeGen, &NoPrivAction](CodeGenFunction &CGF, |
| 8777 | PrePostActionTy &) { |
| 8778 | if (!Info.CaptureDeviceAddrMap.empty()) { |
| 8779 | CodeGen.setAction(NoPrivAction); |
| 8780 | CodeGen(CGF); |
| 8781 | } |
| 8782 | }; |
| 8783 | |
| 8784 | // We don't have to do anything to close the region if the if clause evaluates |
| 8785 | // to false. |
| 8786 | auto &&EndElseGen = [](CodeGenFunction &CGF, PrePostActionTy &) {}; |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8787 | |
| 8788 | if (IfCond) { |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 8789 | emitOMPIfClause(CGF, IfCond, BeginThenGen, BeginElseGen); |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8790 | } else { |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 8791 | RegionCodeGenTy RCG(BeginThenGen); |
| 8792 | RCG(CGF); |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8793 | } |
| 8794 | |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 8795 | // If we don't require privatization of device pointers, we emit the body in |
| 8796 | // between the runtime calls. This avoids duplicating the body code. |
| 8797 | if (Info.CaptureDeviceAddrMap.empty()) { |
| 8798 | CodeGen.setAction(NoPrivAction); |
| 8799 | CodeGen(CGF); |
| 8800 | } |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8801 | |
| 8802 | if (IfCond) { |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 8803 | emitOMPIfClause(CGF, IfCond, EndThenGen, EndElseGen); |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8804 | } else { |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 8805 | RegionCodeGenTy RCG(EndThenGen); |
| 8806 | RCG(CGF); |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8807 | } |
| 8808 | } |
Samuel Antao | bd0ae2e | 2016-04-27 23:07:29 +0000 | [diff] [blame] | 8809 | |
Samuel Antao | 8d2d730 | 2016-05-26 18:30:22 +0000 | [diff] [blame] | 8810 | void CGOpenMPRuntime::emitTargetDataStandAloneCall( |
Samuel Antao | 8dd6628 | 2016-04-27 23:14:30 +0000 | [diff] [blame] | 8811 | CodeGenFunction &CGF, const OMPExecutableDirective &D, const Expr *IfCond, |
| 8812 | const Expr *Device) { |
Samuel Antao | bd0ae2e | 2016-04-27 23:07:29 +0000 | [diff] [blame] | 8813 | if (!CGF.HaveInsertPoint()) |
| 8814 | return; |
| 8815 | |
Samuel Antao | 8dd6628 | 2016-04-27 23:14:30 +0000 | [diff] [blame] | 8816 | assert((isa<OMPTargetEnterDataDirective>(D) || |
Samuel Antao | 8d2d730 | 2016-05-26 18:30:22 +0000 | [diff] [blame] | 8817 | isa<OMPTargetExitDataDirective>(D) || |
| 8818 | isa<OMPTargetUpdateDirective>(D)) && |
| 8819 | "Expecting either target enter, exit data, or update directives."); |
Samuel Antao | 8dd6628 | 2016-04-27 23:14:30 +0000 | [diff] [blame] | 8820 | |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 8821 | CodeGenFunction::OMPTargetDataInfo InputInfo; |
| 8822 | llvm::Value *MapTypesArray = nullptr; |
Samuel Antao | bd0ae2e | 2016-04-27 23:07:29 +0000 | [diff] [blame] | 8823 | // Generate the code for the opening of the data environment. |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 8824 | auto &&ThenGen = [this, &D, Device, &InputInfo, |
| 8825 | &MapTypesArray](CodeGenFunction &CGF, PrePostActionTy &) { |
Samuel Antao | bd0ae2e | 2016-04-27 23:07:29 +0000 | [diff] [blame] | 8826 | // Emit device ID if any. |
| 8827 | llvm::Value *DeviceID = nullptr; |
George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 8828 | if (Device) { |
Samuel Antao | bd0ae2e | 2016-04-27 23:07:29 +0000 | [diff] [blame] | 8829 | DeviceID = CGF.Builder.CreateIntCast(CGF.EmitScalarExpr(Device), |
George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 8830 | CGF.Int64Ty, /*isSigned=*/true); |
| 8831 | } else { |
| 8832 | DeviceID = CGF.Builder.getInt64(OMP_DEVICEID_UNDEF); |
| 8833 | } |
Samuel Antao | bd0ae2e | 2016-04-27 23:07:29 +0000 | [diff] [blame] | 8834 | |
| 8835 | // Emit the number of elements in the offloading arrays. |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 8836 | llvm::Constant *PointerNum = |
| 8837 | CGF.Builder.getInt32(InputInfo.NumberOfTargetItems); |
Samuel Antao | bd0ae2e | 2016-04-27 23:07:29 +0000 | [diff] [blame] | 8838 | |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 8839 | llvm::Value *OffloadingArgs[] = {DeviceID, |
| 8840 | PointerNum, |
| 8841 | InputInfo.BasePointersArray.getPointer(), |
| 8842 | InputInfo.PointersArray.getPointer(), |
| 8843 | InputInfo.SizesArray.getPointer(), |
| 8844 | MapTypesArray}; |
Samuel Antao | 8d2d730 | 2016-05-26 18:30:22 +0000 | [diff] [blame] | 8845 | |
Samuel Antao | 8d2d730 | 2016-05-26 18:30:22 +0000 | [diff] [blame] | 8846 | // Select the right runtime function call for each expected standalone |
| 8847 | // directive. |
Alexey Bataev | 0cc6b8e | 2017-12-14 17:00:17 +0000 | [diff] [blame] | 8848 | const bool HasNowait = D.hasClausesOfKind<OMPNowaitClause>(); |
Samuel Antao | 8d2d730 | 2016-05-26 18:30:22 +0000 | [diff] [blame] | 8849 | OpenMPRTLFunction RTLFn; |
| 8850 | switch (D.getDirectiveKind()) { |
Samuel Antao | 8d2d730 | 2016-05-26 18:30:22 +0000 | [diff] [blame] | 8851 | case OMPD_target_enter_data: |
Alexey Bataev | 0cc6b8e | 2017-12-14 17:00:17 +0000 | [diff] [blame] | 8852 | RTLFn = HasNowait ? OMPRTL__tgt_target_data_begin_nowait |
| 8853 | : OMPRTL__tgt_target_data_begin; |
Samuel Antao | 8d2d730 | 2016-05-26 18:30:22 +0000 | [diff] [blame] | 8854 | break; |
| 8855 | case OMPD_target_exit_data: |
Alexey Bataev | 0cc6b8e | 2017-12-14 17:00:17 +0000 | [diff] [blame] | 8856 | RTLFn = HasNowait ? OMPRTL__tgt_target_data_end_nowait |
| 8857 | : OMPRTL__tgt_target_data_end; |
Samuel Antao | 8d2d730 | 2016-05-26 18:30:22 +0000 | [diff] [blame] | 8858 | break; |
| 8859 | case OMPD_target_update: |
Alexey Bataev | 0cc6b8e | 2017-12-14 17:00:17 +0000 | [diff] [blame] | 8860 | RTLFn = HasNowait ? OMPRTL__tgt_target_data_update_nowait |
| 8861 | : OMPRTL__tgt_target_data_update; |
Samuel Antao | 8d2d730 | 2016-05-26 18:30:22 +0000 | [diff] [blame] | 8862 | break; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8863 | case OMPD_parallel: |
| 8864 | case OMPD_for: |
| 8865 | case OMPD_parallel_for: |
| 8866 | case OMPD_parallel_sections: |
| 8867 | case OMPD_for_simd: |
| 8868 | case OMPD_parallel_for_simd: |
| 8869 | case OMPD_cancel: |
| 8870 | case OMPD_cancellation_point: |
| 8871 | case OMPD_ordered: |
| 8872 | case OMPD_threadprivate: |
| 8873 | case OMPD_task: |
| 8874 | case OMPD_simd: |
| 8875 | case OMPD_sections: |
| 8876 | case OMPD_section: |
| 8877 | case OMPD_single: |
| 8878 | case OMPD_master: |
| 8879 | case OMPD_critical: |
| 8880 | case OMPD_taskyield: |
| 8881 | case OMPD_barrier: |
| 8882 | case OMPD_taskwait: |
| 8883 | case OMPD_taskgroup: |
| 8884 | case OMPD_atomic: |
| 8885 | case OMPD_flush: |
| 8886 | case OMPD_teams: |
| 8887 | case OMPD_target_data: |
| 8888 | case OMPD_distribute: |
| 8889 | case OMPD_distribute_simd: |
| 8890 | case OMPD_distribute_parallel_for: |
| 8891 | case OMPD_distribute_parallel_for_simd: |
| 8892 | case OMPD_teams_distribute: |
| 8893 | case OMPD_teams_distribute_simd: |
| 8894 | case OMPD_teams_distribute_parallel_for: |
| 8895 | case OMPD_teams_distribute_parallel_for_simd: |
| 8896 | case OMPD_declare_simd: |
| 8897 | case OMPD_declare_target: |
| 8898 | case OMPD_end_declare_target: |
| 8899 | case OMPD_declare_reduction: |
| 8900 | case OMPD_taskloop: |
| 8901 | case OMPD_taskloop_simd: |
| 8902 | case OMPD_target: |
| 8903 | case OMPD_target_simd: |
| 8904 | case OMPD_target_teams_distribute: |
| 8905 | case OMPD_target_teams_distribute_simd: |
| 8906 | case OMPD_target_teams_distribute_parallel_for: |
| 8907 | case OMPD_target_teams_distribute_parallel_for_simd: |
| 8908 | case OMPD_target_teams: |
| 8909 | case OMPD_target_parallel: |
| 8910 | case OMPD_target_parallel_for: |
| 8911 | case OMPD_target_parallel_for_simd: |
Kelvin Li | 1408f91 | 2018-09-26 04:28:39 +0000 | [diff] [blame] | 8912 | case OMPD_requires: |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8913 | case OMPD_unknown: |
| 8914 | llvm_unreachable("Unexpected standalone target data directive."); |
| 8915 | break; |
Samuel Antao | 8d2d730 | 2016-05-26 18:30:22 +0000 | [diff] [blame] | 8916 | } |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 8917 | CGF.EmitRuntimeCall(createRuntimeFunction(RTLFn), OffloadingArgs); |
Samuel Antao | bd0ae2e | 2016-04-27 23:07:29 +0000 | [diff] [blame] | 8918 | }; |
| 8919 | |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 8920 | auto &&TargetThenGen = [this, &ThenGen, &D, &InputInfo, &MapTypesArray]( |
| 8921 | CodeGenFunction &CGF, PrePostActionTy &) { |
| 8922 | // Fill up the arrays with all the mapped variables. |
| 8923 | MappableExprsHandler::MapBaseValuesArrayTy BasePointers; |
| 8924 | MappableExprsHandler::MapValuesArrayTy Pointers; |
| 8925 | MappableExprsHandler::MapValuesArrayTy Sizes; |
| 8926 | MappableExprsHandler::MapFlagsArrayTy MapTypes; |
Samuel Antao | bd0ae2e | 2016-04-27 23:07:29 +0000 | [diff] [blame] | 8927 | |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 8928 | // Get map clause information. |
| 8929 | MappableExprsHandler MEHandler(D, CGF); |
| 8930 | MEHandler.generateAllInfo(BasePointers, Pointers, Sizes, MapTypes); |
| 8931 | |
| 8932 | TargetDataInfo Info; |
| 8933 | // Fill up the arrays and create the arguments. |
| 8934 | emitOffloadingArrays(CGF, BasePointers, Pointers, Sizes, MapTypes, Info); |
| 8935 | emitOffloadingArraysArgument(CGF, Info.BasePointersArray, |
| 8936 | Info.PointersArray, Info.SizesArray, |
| 8937 | Info.MapTypesArray, Info); |
| 8938 | InputInfo.NumberOfTargetItems = Info.NumberOfPtrs; |
| 8939 | InputInfo.BasePointersArray = |
| 8940 | Address(Info.BasePointersArray, CGM.getPointerAlign()); |
| 8941 | InputInfo.PointersArray = |
| 8942 | Address(Info.PointersArray, CGM.getPointerAlign()); |
| 8943 | InputInfo.SizesArray = |
| 8944 | Address(Info.SizesArray, CGM.getPointerAlign()); |
| 8945 | MapTypesArray = Info.MapTypesArray; |
| 8946 | if (D.hasClausesOfKind<OMPDependClause>()) |
| 8947 | CGF.EmitOMPTargetTaskBasedDirective(D, ThenGen, InputInfo); |
| 8948 | else |
Alexey Bataev | 768f1f2 | 2018-01-09 19:59:25 +0000 | [diff] [blame] | 8949 | emitInlinedDirective(CGF, D.getDirectiveKind(), ThenGen); |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 8950 | }; |
| 8951 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8952 | if (IfCond) { |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 8953 | emitOMPIfClause(CGF, IfCond, TargetThenGen, |
| 8954 | [](CodeGenFunction &CGF, PrePostActionTy &) {}); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8955 | } else { |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 8956 | RegionCodeGenTy ThenRCG(TargetThenGen); |
| 8957 | ThenRCG(CGF); |
Samuel Antao | bd0ae2e | 2016-04-27 23:07:29 +0000 | [diff] [blame] | 8958 | } |
| 8959 | } |
Alexey Bataev | c7a82b4 | 2016-05-06 09:40:08 +0000 | [diff] [blame] | 8960 | |
| 8961 | namespace { |
| 8962 | /// Kind of parameter in a function with 'declare simd' directive. |
| 8963 | enum ParamKindTy { LinearWithVarStride, Linear, Uniform, Vector }; |
| 8964 | /// Attribute set of the parameter. |
| 8965 | struct ParamAttrTy { |
| 8966 | ParamKindTy Kind = Vector; |
| 8967 | llvm::APSInt StrideOrArg; |
| 8968 | llvm::APSInt Alignment; |
| 8969 | }; |
| 8970 | } // namespace |
| 8971 | |
| 8972 | static unsigned evaluateCDTSize(const FunctionDecl *FD, |
| 8973 | ArrayRef<ParamAttrTy> ParamAttrs) { |
| 8974 | // Every vector variant of a SIMD-enabled function has a vector length (VLEN). |
| 8975 | // If OpenMP clause "simdlen" is used, the VLEN is the value of the argument |
| 8976 | // of that clause. The VLEN value must be power of 2. |
| 8977 | // In other case the notion of the function`s "characteristic data type" (CDT) |
| 8978 | // is used to compute the vector length. |
| 8979 | // CDT is defined in the following order: |
| 8980 | // a) For non-void function, the CDT is the return type. |
| 8981 | // b) If the function has any non-uniform, non-linear parameters, then the |
| 8982 | // CDT is the type of the first such parameter. |
| 8983 | // c) If the CDT determined by a) or b) above is struct, union, or class |
| 8984 | // type which is pass-by-value (except for the type that maps to the |
| 8985 | // built-in complex data type), the characteristic data type is int. |
| 8986 | // d) If none of the above three cases is applicable, the CDT is int. |
| 8987 | // The VLEN is then determined based on the CDT and the size of vector |
| 8988 | // register of that ISA for which current vector version is generated. The |
| 8989 | // VLEN is computed using the formula below: |
| 8990 | // VLEN = sizeof(vector_register) / sizeof(CDT), |
| 8991 | // where vector register size specified in section 3.2.1 Registers and the |
| 8992 | // Stack Frame of original AMD64 ABI document. |
| 8993 | QualType RetType = FD->getReturnType(); |
| 8994 | if (RetType.isNull()) |
| 8995 | return 0; |
| 8996 | ASTContext &C = FD->getASTContext(); |
| 8997 | QualType CDT; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8998 | if (!RetType.isNull() && !RetType->isVoidType()) { |
Alexey Bataev | c7a82b4 | 2016-05-06 09:40:08 +0000 | [diff] [blame] | 8999 | CDT = RetType; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 9000 | } else { |
Alexey Bataev | c7a82b4 | 2016-05-06 09:40:08 +0000 | [diff] [blame] | 9001 | unsigned Offset = 0; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 9002 | if (const auto *MD = dyn_cast<CXXMethodDecl>(FD)) { |
Alexey Bataev | c7a82b4 | 2016-05-06 09:40:08 +0000 | [diff] [blame] | 9003 | if (ParamAttrs[Offset].Kind == Vector) |
| 9004 | CDT = C.getPointerType(C.getRecordType(MD->getParent())); |
| 9005 | ++Offset; |
| 9006 | } |
| 9007 | if (CDT.isNull()) { |
| 9008 | for (unsigned I = 0, E = FD->getNumParams(); I < E; ++I) { |
| 9009 | if (ParamAttrs[I + Offset].Kind == Vector) { |
| 9010 | CDT = FD->getParamDecl(I)->getType(); |
| 9011 | break; |
| 9012 | } |
| 9013 | } |
| 9014 | } |
| 9015 | } |
| 9016 | if (CDT.isNull()) |
| 9017 | CDT = C.IntTy; |
| 9018 | CDT = CDT->getCanonicalTypeUnqualified(); |
| 9019 | if (CDT->isRecordType() || CDT->isUnionType()) |
| 9020 | CDT = C.IntTy; |
| 9021 | return C.getTypeSize(CDT); |
| 9022 | } |
| 9023 | |
| 9024 | static void |
| 9025 | emitX86DeclareSimdFunction(const FunctionDecl *FD, llvm::Function *Fn, |
Benjamin Kramer | 81cb4b7 | 2016-11-24 16:01:20 +0000 | [diff] [blame] | 9026 | const llvm::APSInt &VLENVal, |
Alexey Bataev | c7a82b4 | 2016-05-06 09:40:08 +0000 | [diff] [blame] | 9027 | ArrayRef<ParamAttrTy> ParamAttrs, |
| 9028 | OMPDeclareSimdDeclAttr::BranchStateTy State) { |
| 9029 | struct ISADataTy { |
| 9030 | char ISA; |
| 9031 | unsigned VecRegSize; |
| 9032 | }; |
| 9033 | ISADataTy ISAData[] = { |
| 9034 | { |
| 9035 | 'b', 128 |
| 9036 | }, // SSE |
| 9037 | { |
| 9038 | 'c', 256 |
| 9039 | }, // AVX |
| 9040 | { |
| 9041 | 'd', 256 |
| 9042 | }, // AVX2 |
| 9043 | { |
| 9044 | 'e', 512 |
| 9045 | }, // AVX512 |
| 9046 | }; |
| 9047 | llvm::SmallVector<char, 2> Masked; |
| 9048 | switch (State) { |
| 9049 | case OMPDeclareSimdDeclAttr::BS_Undefined: |
| 9050 | Masked.push_back('N'); |
| 9051 | Masked.push_back('M'); |
| 9052 | break; |
| 9053 | case OMPDeclareSimdDeclAttr::BS_Notinbranch: |
| 9054 | Masked.push_back('N'); |
| 9055 | break; |
| 9056 | case OMPDeclareSimdDeclAttr::BS_Inbranch: |
| 9057 | Masked.push_back('M'); |
| 9058 | break; |
| 9059 | } |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 9060 | for (char Mask : Masked) { |
| 9061 | for (const ISADataTy &Data : ISAData) { |
Alexey Bataev | c7a82b4 | 2016-05-06 09:40:08 +0000 | [diff] [blame] | 9062 | SmallString<256> Buffer; |
| 9063 | llvm::raw_svector_ostream Out(Buffer); |
| 9064 | Out << "_ZGV" << Data.ISA << Mask; |
| 9065 | if (!VLENVal) { |
| 9066 | Out << llvm::APSInt::getUnsigned(Data.VecRegSize / |
| 9067 | evaluateCDTSize(FD, ParamAttrs)); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 9068 | } else { |
Alexey Bataev | c7a82b4 | 2016-05-06 09:40:08 +0000 | [diff] [blame] | 9069 | Out << VLENVal; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 9070 | } |
| 9071 | for (const ParamAttrTy &ParamAttr : ParamAttrs) { |
Alexey Bataev | c7a82b4 | 2016-05-06 09:40:08 +0000 | [diff] [blame] | 9072 | switch (ParamAttr.Kind){ |
| 9073 | case LinearWithVarStride: |
| 9074 | Out << 's' << ParamAttr.StrideOrArg; |
| 9075 | break; |
| 9076 | case Linear: |
| 9077 | Out << 'l'; |
| 9078 | if (!!ParamAttr.StrideOrArg) |
| 9079 | Out << ParamAttr.StrideOrArg; |
| 9080 | break; |
| 9081 | case Uniform: |
| 9082 | Out << 'u'; |
| 9083 | break; |
| 9084 | case Vector: |
| 9085 | Out << 'v'; |
| 9086 | break; |
| 9087 | } |
| 9088 | if (!!ParamAttr.Alignment) |
| 9089 | Out << 'a' << ParamAttr.Alignment; |
| 9090 | } |
| 9091 | Out << '_' << Fn->getName(); |
| 9092 | Fn->addFnAttr(Out.str()); |
| 9093 | } |
| 9094 | } |
| 9095 | } |
| 9096 | |
| 9097 | void CGOpenMPRuntime::emitDeclareSimdFunction(const FunctionDecl *FD, |
| 9098 | llvm::Function *Fn) { |
| 9099 | ASTContext &C = CGM.getContext(); |
Alexey Bataev | 20cf67c | 2018-03-02 18:07:00 +0000 | [diff] [blame] | 9100 | FD = FD->getMostRecentDecl(); |
Alexey Bataev | c7a82b4 | 2016-05-06 09:40:08 +0000 | [diff] [blame] | 9101 | // Map params to their positions in function decl. |
| 9102 | llvm::DenseMap<const Decl *, unsigned> ParamPositions; |
| 9103 | if (isa<CXXMethodDecl>(FD)) |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 9104 | ParamPositions.try_emplace(FD, 0); |
Alexey Bataev | c7a82b4 | 2016-05-06 09:40:08 +0000 | [diff] [blame] | 9105 | unsigned ParamPos = ParamPositions.size(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 9106 | for (const ParmVarDecl *P : FD->parameters()) { |
| 9107 | ParamPositions.try_emplace(P->getCanonicalDecl(), ParamPos); |
Alexey Bataev | c7a82b4 | 2016-05-06 09:40:08 +0000 | [diff] [blame] | 9108 | ++ParamPos; |
| 9109 | } |
Alexey Bataev | 20cf67c | 2018-03-02 18:07:00 +0000 | [diff] [blame] | 9110 | while (FD) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 9111 | for (const auto *Attr : FD->specific_attrs<OMPDeclareSimdDeclAttr>()) { |
Alexey Bataev | 20cf67c | 2018-03-02 18:07:00 +0000 | [diff] [blame] | 9112 | llvm::SmallVector<ParamAttrTy, 8> ParamAttrs(ParamPositions.size()); |
| 9113 | // Mark uniform parameters. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 9114 | for (const Expr *E : Attr->uniforms()) { |
Alexey Bataev | 20cf67c | 2018-03-02 18:07:00 +0000 | [diff] [blame] | 9115 | E = E->IgnoreParenImpCasts(); |
| 9116 | unsigned Pos; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 9117 | if (isa<CXXThisExpr>(E)) { |
Alexey Bataev | 20cf67c | 2018-03-02 18:07:00 +0000 | [diff] [blame] | 9118 | Pos = ParamPositions[FD]; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 9119 | } else { |
| 9120 | const auto *PVD = cast<ParmVarDecl>(cast<DeclRefExpr>(E)->getDecl()) |
| 9121 | ->getCanonicalDecl(); |
Alexey Bataev | 20cf67c | 2018-03-02 18:07:00 +0000 | [diff] [blame] | 9122 | Pos = ParamPositions[PVD]; |
| 9123 | } |
| 9124 | ParamAttrs[Pos].Kind = Uniform; |
Alexey Bataev | c7a82b4 | 2016-05-06 09:40:08 +0000 | [diff] [blame] | 9125 | } |
Alexey Bataev | 20cf67c | 2018-03-02 18:07:00 +0000 | [diff] [blame] | 9126 | // Get alignment info. |
| 9127 | auto NI = Attr->alignments_begin(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 9128 | for (const Expr *E : Attr->aligneds()) { |
Alexey Bataev | 20cf67c | 2018-03-02 18:07:00 +0000 | [diff] [blame] | 9129 | E = E->IgnoreParenImpCasts(); |
| 9130 | unsigned Pos; |
| 9131 | QualType ParmTy; |
| 9132 | if (isa<CXXThisExpr>(E)) { |
| 9133 | Pos = ParamPositions[FD]; |
| 9134 | ParmTy = E->getType(); |
| 9135 | } else { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 9136 | const auto *PVD = cast<ParmVarDecl>(cast<DeclRefExpr>(E)->getDecl()) |
| 9137 | ->getCanonicalDecl(); |
Alexey Bataev | 20cf67c | 2018-03-02 18:07:00 +0000 | [diff] [blame] | 9138 | Pos = ParamPositions[PVD]; |
| 9139 | ParmTy = PVD->getType(); |
| 9140 | } |
| 9141 | ParamAttrs[Pos].Alignment = |
| 9142 | (*NI) |
| 9143 | ? (*NI)->EvaluateKnownConstInt(C) |
Alexey Bataev | c7a82b4 | 2016-05-06 09:40:08 +0000 | [diff] [blame] | 9144 | : llvm::APSInt::getUnsigned( |
| 9145 | C.toCharUnitsFromBits(C.getOpenMPDefaultSimdAlign(ParmTy)) |
| 9146 | .getQuantity()); |
Alexey Bataev | 20cf67c | 2018-03-02 18:07:00 +0000 | [diff] [blame] | 9147 | ++NI; |
Alexey Bataev | c7a82b4 | 2016-05-06 09:40:08 +0000 | [diff] [blame] | 9148 | } |
Alexey Bataev | 20cf67c | 2018-03-02 18:07:00 +0000 | [diff] [blame] | 9149 | // Mark linear parameters. |
| 9150 | auto SI = Attr->steps_begin(); |
| 9151 | auto MI = Attr->modifiers_begin(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 9152 | for (const Expr *E : Attr->linears()) { |
Alexey Bataev | 20cf67c | 2018-03-02 18:07:00 +0000 | [diff] [blame] | 9153 | E = E->IgnoreParenImpCasts(); |
| 9154 | unsigned Pos; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 9155 | if (isa<CXXThisExpr>(E)) { |
Alexey Bataev | 20cf67c | 2018-03-02 18:07:00 +0000 | [diff] [blame] | 9156 | Pos = ParamPositions[FD]; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 9157 | } else { |
| 9158 | const auto *PVD = cast<ParmVarDecl>(cast<DeclRefExpr>(E)->getDecl()) |
| 9159 | ->getCanonicalDecl(); |
Alexey Bataev | 20cf67c | 2018-03-02 18:07:00 +0000 | [diff] [blame] | 9160 | Pos = ParamPositions[PVD]; |
| 9161 | } |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 9162 | ParamAttrTy &ParamAttr = ParamAttrs[Pos]; |
Alexey Bataev | 20cf67c | 2018-03-02 18:07:00 +0000 | [diff] [blame] | 9163 | ParamAttr.Kind = Linear; |
| 9164 | if (*SI) { |
Nico Weber | 9f0246d | 2018-11-21 12:47:43 +0000 | [diff] [blame] | 9165 | if (!(*SI)->EvaluateAsInt(ParamAttr.StrideOrArg, C, |
| 9166 | Expr::SE_AllowSideEffects)) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 9167 | if (const auto *DRE = |
| 9168 | cast<DeclRefExpr>((*SI)->IgnoreParenImpCasts())) { |
| 9169 | if (const auto *StridePVD = cast<ParmVarDecl>(DRE->getDecl())) { |
Alexey Bataev | 20cf67c | 2018-03-02 18:07:00 +0000 | [diff] [blame] | 9170 | ParamAttr.Kind = LinearWithVarStride; |
| 9171 | ParamAttr.StrideOrArg = llvm::APSInt::getUnsigned( |
| 9172 | ParamPositions[StridePVD->getCanonicalDecl()]); |
| 9173 | } |
Alexey Bataev | c7a82b4 | 2016-05-06 09:40:08 +0000 | [diff] [blame] | 9174 | } |
| 9175 | } |
| 9176 | } |
Alexey Bataev | 20cf67c | 2018-03-02 18:07:00 +0000 | [diff] [blame] | 9177 | ++SI; |
| 9178 | ++MI; |
Alexey Bataev | c7a82b4 | 2016-05-06 09:40:08 +0000 | [diff] [blame] | 9179 | } |
Alexey Bataev | 20cf67c | 2018-03-02 18:07:00 +0000 | [diff] [blame] | 9180 | llvm::APSInt VLENVal; |
| 9181 | if (const Expr *VLEN = Attr->getSimdlen()) |
| 9182 | VLENVal = VLEN->EvaluateKnownConstInt(C); |
| 9183 | OMPDeclareSimdDeclAttr::BranchStateTy State = Attr->getBranchState(); |
| 9184 | if (CGM.getTriple().getArch() == llvm::Triple::x86 || |
| 9185 | CGM.getTriple().getArch() == llvm::Triple::x86_64) |
| 9186 | emitX86DeclareSimdFunction(FD, Fn, VLENVal, ParamAttrs, State); |
Alexey Bataev | c7a82b4 | 2016-05-06 09:40:08 +0000 | [diff] [blame] | 9187 | } |
Alexey Bataev | 20cf67c | 2018-03-02 18:07:00 +0000 | [diff] [blame] | 9188 | FD = FD->getPreviousDecl(); |
Alexey Bataev | c7a82b4 | 2016-05-06 09:40:08 +0000 | [diff] [blame] | 9189 | } |
| 9190 | } |
Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 9191 | |
| 9192 | namespace { |
| 9193 | /// Cleanup action for doacross support. |
| 9194 | class DoacrossCleanupTy final : public EHScopeStack::Cleanup { |
| 9195 | public: |
| 9196 | static const int DoacrossFinArgs = 2; |
| 9197 | |
| 9198 | private: |
| 9199 | llvm::Value *RTLFn; |
| 9200 | llvm::Value *Args[DoacrossFinArgs]; |
| 9201 | |
| 9202 | public: |
| 9203 | DoacrossCleanupTy(llvm::Value *RTLFn, ArrayRef<llvm::Value *> CallArgs) |
| 9204 | : RTLFn(RTLFn) { |
| 9205 | assert(CallArgs.size() == DoacrossFinArgs); |
| 9206 | std::copy(CallArgs.begin(), CallArgs.end(), std::begin(Args)); |
| 9207 | } |
| 9208 | void Emit(CodeGenFunction &CGF, Flags /*flags*/) override { |
| 9209 | if (!CGF.HaveInsertPoint()) |
| 9210 | return; |
| 9211 | CGF.EmitRuntimeCall(RTLFn, Args); |
| 9212 | } |
| 9213 | }; |
| 9214 | } // namespace |
| 9215 | |
| 9216 | void CGOpenMPRuntime::emitDoacrossInit(CodeGenFunction &CGF, |
Alexey Bataev | f138fda | 2018-08-13 19:04:24 +0000 | [diff] [blame] | 9217 | const OMPLoopDirective &D, |
| 9218 | ArrayRef<Expr *> NumIterations) { |
Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 9219 | if (!CGF.HaveInsertPoint()) |
| 9220 | return; |
| 9221 | |
| 9222 | ASTContext &C = CGM.getContext(); |
| 9223 | QualType Int64Ty = C.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/true); |
| 9224 | RecordDecl *RD; |
| 9225 | if (KmpDimTy.isNull()) { |
| 9226 | // Build struct kmp_dim { // loop bounds info casted to kmp_int64 |
| 9227 | // kmp_int64 lo; // lower |
| 9228 | // kmp_int64 up; // upper |
| 9229 | // kmp_int64 st; // stride |
| 9230 | // }; |
| 9231 | RD = C.buildImplicitRecord("kmp_dim"); |
| 9232 | RD->startDefinition(); |
| 9233 | addFieldToRecordDecl(C, RD, Int64Ty); |
| 9234 | addFieldToRecordDecl(C, RD, Int64Ty); |
| 9235 | addFieldToRecordDecl(C, RD, Int64Ty); |
| 9236 | RD->completeDefinition(); |
| 9237 | KmpDimTy = C.getRecordType(RD); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 9238 | } else { |
Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 9239 | RD = cast<RecordDecl>(KmpDimTy->getAsTagDecl()); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 9240 | } |
Alexey Bataev | f138fda | 2018-08-13 19:04:24 +0000 | [diff] [blame] | 9241 | llvm::APInt Size(/*numBits=*/32, NumIterations.size()); |
| 9242 | QualType ArrayTy = |
| 9243 | C.getConstantArrayType(KmpDimTy, Size, ArrayType::Normal, 0); |
Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 9244 | |
Alexey Bataev | f138fda | 2018-08-13 19:04:24 +0000 | [diff] [blame] | 9245 | Address DimsAddr = CGF.CreateMemTemp(ArrayTy, "dims"); |
| 9246 | CGF.EmitNullInitialization(DimsAddr, ArrayTy); |
Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 9247 | enum { LowerFD = 0, UpperFD, StrideFD }; |
| 9248 | // Fill dims with data. |
Alexey Bataev | f138fda | 2018-08-13 19:04:24 +0000 | [diff] [blame] | 9249 | for (unsigned I = 0, E = NumIterations.size(); I < E; ++I) { |
| 9250 | LValue DimsLVal = |
| 9251 | CGF.MakeAddrLValue(CGF.Builder.CreateConstArrayGEP( |
| 9252 | DimsAddr, I, C.getTypeSizeInChars(KmpDimTy)), |
| 9253 | KmpDimTy); |
| 9254 | // dims.upper = num_iterations; |
| 9255 | LValue UpperLVal = CGF.EmitLValueForField( |
| 9256 | DimsLVal, *std::next(RD->field_begin(), UpperFD)); |
| 9257 | llvm::Value *NumIterVal = |
| 9258 | CGF.EmitScalarConversion(CGF.EmitScalarExpr(NumIterations[I]), |
| 9259 | D.getNumIterations()->getType(), Int64Ty, |
| 9260 | D.getNumIterations()->getExprLoc()); |
| 9261 | CGF.EmitStoreOfScalar(NumIterVal, UpperLVal); |
| 9262 | // dims.stride = 1; |
| 9263 | LValue StrideLVal = CGF.EmitLValueForField( |
| 9264 | DimsLVal, *std::next(RD->field_begin(), StrideFD)); |
| 9265 | CGF.EmitStoreOfScalar(llvm::ConstantInt::getSigned(CGM.Int64Ty, /*V=*/1), |
| 9266 | StrideLVal); |
| 9267 | } |
Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 9268 | |
| 9269 | // Build call void __kmpc_doacross_init(ident_t *loc, kmp_int32 gtid, |
| 9270 | // kmp_int32 num_dims, struct kmp_dim * dims); |
Alexey Bataev | f138fda | 2018-08-13 19:04:24 +0000 | [diff] [blame] | 9271 | llvm::Value *Args[] = { |
| 9272 | emitUpdateLocation(CGF, D.getBeginLoc()), |
| 9273 | getThreadID(CGF, D.getBeginLoc()), |
| 9274 | llvm::ConstantInt::getSigned(CGM.Int32Ty, NumIterations.size()), |
| 9275 | CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
| 9276 | CGF.Builder |
| 9277 | .CreateConstArrayGEP(DimsAddr, 0, C.getTypeSizeInChars(KmpDimTy)) |
| 9278 | .getPointer(), |
| 9279 | CGM.VoidPtrTy)}; |
Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 9280 | |
| 9281 | llvm::Value *RTLFn = createRuntimeFunction(OMPRTL__kmpc_doacross_init); |
| 9282 | CGF.EmitRuntimeCall(RTLFn, Args); |
| 9283 | llvm::Value *FiniArgs[DoacrossCleanupTy::DoacrossFinArgs] = { |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 9284 | emitUpdateLocation(CGF, D.getEndLoc()), getThreadID(CGF, D.getEndLoc())}; |
Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 9285 | llvm::Value *FiniRTLFn = createRuntimeFunction(OMPRTL__kmpc_doacross_fini); |
| 9286 | CGF.EHStack.pushCleanup<DoacrossCleanupTy>(NormalAndEHCleanup, FiniRTLFn, |
| 9287 | llvm::makeArrayRef(FiniArgs)); |
| 9288 | } |
| 9289 | |
| 9290 | void CGOpenMPRuntime::emitDoacrossOrdered(CodeGenFunction &CGF, |
| 9291 | const OMPDependClause *C) { |
| 9292 | QualType Int64Ty = |
| 9293 | CGM.getContext().getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1); |
Alexey Bataev | f138fda | 2018-08-13 19:04:24 +0000 | [diff] [blame] | 9294 | llvm::APInt Size(/*numBits=*/32, C->getNumLoops()); |
| 9295 | QualType ArrayTy = CGM.getContext().getConstantArrayType( |
| 9296 | Int64Ty, Size, ArrayType::Normal, 0); |
| 9297 | Address CntAddr = CGF.CreateMemTemp(ArrayTy, ".cnt.addr"); |
| 9298 | for (unsigned I = 0, E = C->getNumLoops(); I < E; ++I) { |
| 9299 | const Expr *CounterVal = C->getLoopData(I); |
| 9300 | assert(CounterVal); |
| 9301 | llvm::Value *CntVal = CGF.EmitScalarConversion( |
| 9302 | CGF.EmitScalarExpr(CounterVal), CounterVal->getType(), Int64Ty, |
| 9303 | CounterVal->getExprLoc()); |
| 9304 | CGF.EmitStoreOfScalar( |
| 9305 | CntVal, |
| 9306 | CGF.Builder.CreateConstArrayGEP( |
| 9307 | CntAddr, I, CGM.getContext().getTypeSizeInChars(Int64Ty)), |
| 9308 | /*Volatile=*/false, Int64Ty); |
| 9309 | } |
| 9310 | llvm::Value *Args[] = { |
| 9311 | emitUpdateLocation(CGF, C->getBeginLoc()), |
| 9312 | getThreadID(CGF, C->getBeginLoc()), |
| 9313 | CGF.Builder |
| 9314 | .CreateConstArrayGEP(CntAddr, 0, |
| 9315 | CGM.getContext().getTypeSizeInChars(Int64Ty)) |
| 9316 | .getPointer()}; |
Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 9317 | llvm::Value *RTLFn; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 9318 | if (C->getDependencyKind() == OMPC_DEPEND_source) { |
Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 9319 | RTLFn = createRuntimeFunction(OMPRTL__kmpc_doacross_post); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 9320 | } else { |
Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 9321 | assert(C->getDependencyKind() == OMPC_DEPEND_sink); |
| 9322 | RTLFn = createRuntimeFunction(OMPRTL__kmpc_doacross_wait); |
| 9323 | } |
| 9324 | CGF.EmitRuntimeCall(RTLFn, Args); |
| 9325 | } |
| 9326 | |
Alexey Bataev | 7ef47a6 | 2018-02-22 18:33:31 +0000 | [diff] [blame] | 9327 | void CGOpenMPRuntime::emitCall(CodeGenFunction &CGF, SourceLocation Loc, |
| 9328 | llvm::Value *Callee, |
| 9329 | ArrayRef<llvm::Value *> Args) const { |
| 9330 | assert(Loc.isValid() && "Outlined function call location must be valid."); |
Alexey Bataev | 3c595a6 | 2017-08-14 15:01:03 +0000 | [diff] [blame] | 9331 | auto DL = ApplyDebugLocation::CreateDefaultArtificial(CGF, Loc); |
| 9332 | |
| 9333 | if (auto *Fn = dyn_cast<llvm::Function>(Callee)) { |
Alexey Bataev | 2c7eee5 | 2017-08-04 19:10:54 +0000 | [diff] [blame] | 9334 | if (Fn->doesNotThrow()) { |
Alexey Bataev | 3c595a6 | 2017-08-14 15:01:03 +0000 | [diff] [blame] | 9335 | CGF.EmitNounwindRuntimeCall(Fn, Args); |
Alexey Bataev | 2c7eee5 | 2017-08-04 19:10:54 +0000 | [diff] [blame] | 9336 | return; |
| 9337 | } |
| 9338 | } |
Alexey Bataev | 3c595a6 | 2017-08-14 15:01:03 +0000 | [diff] [blame] | 9339 | CGF.EmitRuntimeCall(Callee, Args); |
| 9340 | } |
| 9341 | |
| 9342 | void CGOpenMPRuntime::emitOutlinedFunctionCall( |
| 9343 | CodeGenFunction &CGF, SourceLocation Loc, llvm::Value *OutlinedFn, |
| 9344 | ArrayRef<llvm::Value *> Args) const { |
Alexey Bataev | 7ef47a6 | 2018-02-22 18:33:31 +0000 | [diff] [blame] | 9345 | emitCall(CGF, Loc, OutlinedFn, Args); |
Alexey Bataev | 2c7eee5 | 2017-08-04 19:10:54 +0000 | [diff] [blame] | 9346 | } |
Alexey Bataev | 3b8d558 | 2017-08-08 18:04:06 +0000 | [diff] [blame] | 9347 | |
| 9348 | Address CGOpenMPRuntime::getParameterAddress(CodeGenFunction &CGF, |
| 9349 | const VarDecl *NativeParam, |
| 9350 | const VarDecl *TargetParam) const { |
| 9351 | return CGF.GetAddrOfLocalVar(NativeParam); |
| 9352 | } |
Alexey Bataev | a8a9153a | 2017-12-29 18:07:07 +0000 | [diff] [blame] | 9353 | |
Gheorghe-Teodor Bercea | d3dcf2f | 2018-03-14 14:17:45 +0000 | [diff] [blame] | 9354 | Address CGOpenMPRuntime::getAddressOfLocalVariable(CodeGenFunction &CGF, |
| 9355 | const VarDecl *VD) { |
| 9356 | return Address::invalid(); |
| 9357 | } |
| 9358 | |
Alexey Bataev | a8a9153a | 2017-12-29 18:07:07 +0000 | [diff] [blame] | 9359 | llvm::Value *CGOpenMPSIMDRuntime::emitParallelOutlinedFunction( |
| 9360 | const OMPExecutableDirective &D, const VarDecl *ThreadIDVar, |
| 9361 | OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen) { |
| 9362 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 9363 | } |
| 9364 | |
| 9365 | llvm::Value *CGOpenMPSIMDRuntime::emitTeamsOutlinedFunction( |
| 9366 | const OMPExecutableDirective &D, const VarDecl *ThreadIDVar, |
| 9367 | OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen) { |
| 9368 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 9369 | } |
| 9370 | |
| 9371 | llvm::Value *CGOpenMPSIMDRuntime::emitTaskOutlinedFunction( |
| 9372 | const OMPExecutableDirective &D, const VarDecl *ThreadIDVar, |
| 9373 | const VarDecl *PartIDVar, const VarDecl *TaskTVar, |
| 9374 | OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen, |
| 9375 | bool Tied, unsigned &NumberOfParts) { |
| 9376 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 9377 | } |
| 9378 | |
| 9379 | void CGOpenMPSIMDRuntime::emitParallelCall(CodeGenFunction &CGF, |
| 9380 | SourceLocation Loc, |
| 9381 | llvm::Value *OutlinedFn, |
| 9382 | ArrayRef<llvm::Value *> CapturedVars, |
| 9383 | const Expr *IfCond) { |
| 9384 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 9385 | } |
| 9386 | |
| 9387 | void CGOpenMPSIMDRuntime::emitCriticalRegion( |
| 9388 | CodeGenFunction &CGF, StringRef CriticalName, |
| 9389 | const RegionCodeGenTy &CriticalOpGen, SourceLocation Loc, |
| 9390 | const Expr *Hint) { |
| 9391 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 9392 | } |
| 9393 | |
| 9394 | void CGOpenMPSIMDRuntime::emitMasterRegion(CodeGenFunction &CGF, |
| 9395 | const RegionCodeGenTy &MasterOpGen, |
| 9396 | SourceLocation Loc) { |
| 9397 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 9398 | } |
| 9399 | |
| 9400 | void CGOpenMPSIMDRuntime::emitTaskyieldCall(CodeGenFunction &CGF, |
| 9401 | SourceLocation Loc) { |
| 9402 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 9403 | } |
| 9404 | |
| 9405 | void CGOpenMPSIMDRuntime::emitTaskgroupRegion( |
| 9406 | CodeGenFunction &CGF, const RegionCodeGenTy &TaskgroupOpGen, |
| 9407 | SourceLocation Loc) { |
| 9408 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 9409 | } |
| 9410 | |
| 9411 | void CGOpenMPSIMDRuntime::emitSingleRegion( |
| 9412 | CodeGenFunction &CGF, const RegionCodeGenTy &SingleOpGen, |
| 9413 | SourceLocation Loc, ArrayRef<const Expr *> CopyprivateVars, |
| 9414 | ArrayRef<const Expr *> DestExprs, ArrayRef<const Expr *> SrcExprs, |
| 9415 | ArrayRef<const Expr *> AssignmentOps) { |
| 9416 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 9417 | } |
| 9418 | |
| 9419 | void CGOpenMPSIMDRuntime::emitOrderedRegion(CodeGenFunction &CGF, |
| 9420 | const RegionCodeGenTy &OrderedOpGen, |
| 9421 | SourceLocation Loc, |
| 9422 | bool IsThreads) { |
| 9423 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 9424 | } |
| 9425 | |
| 9426 | void CGOpenMPSIMDRuntime::emitBarrierCall(CodeGenFunction &CGF, |
| 9427 | SourceLocation Loc, |
| 9428 | OpenMPDirectiveKind Kind, |
| 9429 | bool EmitChecks, |
| 9430 | bool ForceSimpleCall) { |
| 9431 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 9432 | } |
| 9433 | |
| 9434 | void CGOpenMPSIMDRuntime::emitForDispatchInit( |
| 9435 | CodeGenFunction &CGF, SourceLocation Loc, |
| 9436 | const OpenMPScheduleTy &ScheduleKind, unsigned IVSize, bool IVSigned, |
| 9437 | bool Ordered, const DispatchRTInput &DispatchValues) { |
| 9438 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 9439 | } |
| 9440 | |
| 9441 | void CGOpenMPSIMDRuntime::emitForStaticInit( |
| 9442 | CodeGenFunction &CGF, SourceLocation Loc, OpenMPDirectiveKind DKind, |
| 9443 | const OpenMPScheduleTy &ScheduleKind, const StaticRTInput &Values) { |
| 9444 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 9445 | } |
| 9446 | |
| 9447 | void CGOpenMPSIMDRuntime::emitDistributeStaticInit( |
| 9448 | CodeGenFunction &CGF, SourceLocation Loc, |
| 9449 | OpenMPDistScheduleClauseKind SchedKind, const StaticRTInput &Values) { |
| 9450 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 9451 | } |
| 9452 | |
| 9453 | void CGOpenMPSIMDRuntime::emitForOrderedIterationEnd(CodeGenFunction &CGF, |
| 9454 | SourceLocation Loc, |
| 9455 | unsigned IVSize, |
| 9456 | bool IVSigned) { |
| 9457 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 9458 | } |
| 9459 | |
| 9460 | void CGOpenMPSIMDRuntime::emitForStaticFinish(CodeGenFunction &CGF, |
| 9461 | SourceLocation Loc, |
| 9462 | OpenMPDirectiveKind DKind) { |
| 9463 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 9464 | } |
| 9465 | |
| 9466 | llvm::Value *CGOpenMPSIMDRuntime::emitForNext(CodeGenFunction &CGF, |
| 9467 | SourceLocation Loc, |
| 9468 | unsigned IVSize, bool IVSigned, |
| 9469 | Address IL, Address LB, |
| 9470 | Address UB, Address ST) { |
| 9471 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 9472 | } |
| 9473 | |
| 9474 | void CGOpenMPSIMDRuntime::emitNumThreadsClause(CodeGenFunction &CGF, |
| 9475 | llvm::Value *NumThreads, |
| 9476 | SourceLocation Loc) { |
| 9477 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 9478 | } |
| 9479 | |
| 9480 | void CGOpenMPSIMDRuntime::emitProcBindClause(CodeGenFunction &CGF, |
| 9481 | OpenMPProcBindClauseKind ProcBind, |
| 9482 | SourceLocation Loc) { |
| 9483 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 9484 | } |
| 9485 | |
| 9486 | Address CGOpenMPSIMDRuntime::getAddrOfThreadPrivate(CodeGenFunction &CGF, |
| 9487 | const VarDecl *VD, |
| 9488 | Address VDAddr, |
| 9489 | SourceLocation Loc) { |
| 9490 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 9491 | } |
| 9492 | |
| 9493 | llvm::Function *CGOpenMPSIMDRuntime::emitThreadPrivateVarDefinition( |
| 9494 | const VarDecl *VD, Address VDAddr, SourceLocation Loc, bool PerformInit, |
| 9495 | CodeGenFunction *CGF) { |
| 9496 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 9497 | } |
| 9498 | |
| 9499 | Address CGOpenMPSIMDRuntime::getAddrOfArtificialThreadPrivate( |
| 9500 | CodeGenFunction &CGF, QualType VarType, StringRef Name) { |
| 9501 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 9502 | } |
| 9503 | |
| 9504 | void CGOpenMPSIMDRuntime::emitFlush(CodeGenFunction &CGF, |
| 9505 | ArrayRef<const Expr *> Vars, |
| 9506 | SourceLocation Loc) { |
| 9507 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 9508 | } |
| 9509 | |
| 9510 | void CGOpenMPSIMDRuntime::emitTaskCall(CodeGenFunction &CGF, SourceLocation Loc, |
| 9511 | const OMPExecutableDirective &D, |
| 9512 | llvm::Value *TaskFunction, |
| 9513 | QualType SharedsTy, Address Shareds, |
| 9514 | const Expr *IfCond, |
| 9515 | const OMPTaskDataTy &Data) { |
| 9516 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 9517 | } |
| 9518 | |
| 9519 | void CGOpenMPSIMDRuntime::emitTaskLoopCall( |
| 9520 | CodeGenFunction &CGF, SourceLocation Loc, const OMPLoopDirective &D, |
| 9521 | llvm::Value *TaskFunction, QualType SharedsTy, Address Shareds, |
| 9522 | const Expr *IfCond, const OMPTaskDataTy &Data) { |
| 9523 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 9524 | } |
| 9525 | |
| 9526 | void CGOpenMPSIMDRuntime::emitReduction( |
| 9527 | CodeGenFunction &CGF, SourceLocation Loc, ArrayRef<const Expr *> Privates, |
| 9528 | ArrayRef<const Expr *> LHSExprs, ArrayRef<const Expr *> RHSExprs, |
| 9529 | ArrayRef<const Expr *> ReductionOps, ReductionOptionsTy Options) { |
| 9530 | assert(Options.SimpleReduction && "Only simple reduction is expected."); |
| 9531 | CGOpenMPRuntime::emitReduction(CGF, Loc, Privates, LHSExprs, RHSExprs, |
| 9532 | ReductionOps, Options); |
| 9533 | } |
| 9534 | |
| 9535 | llvm::Value *CGOpenMPSIMDRuntime::emitTaskReductionInit( |
| 9536 | CodeGenFunction &CGF, SourceLocation Loc, ArrayRef<const Expr *> LHSExprs, |
| 9537 | ArrayRef<const Expr *> RHSExprs, const OMPTaskDataTy &Data) { |
| 9538 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 9539 | } |
| 9540 | |
| 9541 | void CGOpenMPSIMDRuntime::emitTaskReductionFixups(CodeGenFunction &CGF, |
| 9542 | SourceLocation Loc, |
| 9543 | ReductionCodeGen &RCG, |
| 9544 | unsigned N) { |
| 9545 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 9546 | } |
| 9547 | |
| 9548 | Address CGOpenMPSIMDRuntime::getTaskReductionItem(CodeGenFunction &CGF, |
| 9549 | SourceLocation Loc, |
| 9550 | llvm::Value *ReductionsPtr, |
| 9551 | LValue SharedLVal) { |
| 9552 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 9553 | } |
| 9554 | |
| 9555 | void CGOpenMPSIMDRuntime::emitTaskwaitCall(CodeGenFunction &CGF, |
| 9556 | SourceLocation Loc) { |
| 9557 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 9558 | } |
| 9559 | |
| 9560 | void CGOpenMPSIMDRuntime::emitCancellationPointCall( |
| 9561 | CodeGenFunction &CGF, SourceLocation Loc, |
| 9562 | OpenMPDirectiveKind CancelRegion) { |
| 9563 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 9564 | } |
| 9565 | |
| 9566 | void CGOpenMPSIMDRuntime::emitCancelCall(CodeGenFunction &CGF, |
| 9567 | SourceLocation Loc, const Expr *IfCond, |
| 9568 | OpenMPDirectiveKind CancelRegion) { |
| 9569 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 9570 | } |
| 9571 | |
| 9572 | void CGOpenMPSIMDRuntime::emitTargetOutlinedFunction( |
| 9573 | const OMPExecutableDirective &D, StringRef ParentName, |
| 9574 | llvm::Function *&OutlinedFn, llvm::Constant *&OutlinedFnID, |
| 9575 | bool IsOffloadEntry, const RegionCodeGenTy &CodeGen) { |
| 9576 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 9577 | } |
| 9578 | |
| 9579 | void CGOpenMPSIMDRuntime::emitTargetCall(CodeGenFunction &CGF, |
| 9580 | const OMPExecutableDirective &D, |
| 9581 | llvm::Value *OutlinedFn, |
| 9582 | llvm::Value *OutlinedFnID, |
Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 9583 | const Expr *IfCond, const Expr *Device) { |
Alexey Bataev | a8a9153a | 2017-12-29 18:07:07 +0000 | [diff] [blame] | 9584 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 9585 | } |
| 9586 | |
| 9587 | bool CGOpenMPSIMDRuntime::emitTargetFunctions(GlobalDecl GD) { |
| 9588 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 9589 | } |
| 9590 | |
| 9591 | bool CGOpenMPSIMDRuntime::emitTargetGlobalVariable(GlobalDecl GD) { |
| 9592 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 9593 | } |
| 9594 | |
| 9595 | bool CGOpenMPSIMDRuntime::emitTargetGlobal(GlobalDecl GD) { |
| 9596 | return false; |
| 9597 | } |
| 9598 | |
| 9599 | llvm::Function *CGOpenMPSIMDRuntime::emitRegistrationFunction() { |
| 9600 | return nullptr; |
| 9601 | } |
| 9602 | |
| 9603 | void CGOpenMPSIMDRuntime::emitTeamsCall(CodeGenFunction &CGF, |
| 9604 | const OMPExecutableDirective &D, |
| 9605 | SourceLocation Loc, |
| 9606 | llvm::Value *OutlinedFn, |
| 9607 | ArrayRef<llvm::Value *> CapturedVars) { |
| 9608 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 9609 | } |
| 9610 | |
| 9611 | void CGOpenMPSIMDRuntime::emitNumTeamsClause(CodeGenFunction &CGF, |
| 9612 | const Expr *NumTeams, |
| 9613 | const Expr *ThreadLimit, |
| 9614 | SourceLocation Loc) { |
| 9615 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 9616 | } |
| 9617 | |
| 9618 | void CGOpenMPSIMDRuntime::emitTargetDataCalls( |
| 9619 | CodeGenFunction &CGF, const OMPExecutableDirective &D, const Expr *IfCond, |
| 9620 | const Expr *Device, const RegionCodeGenTy &CodeGen, TargetDataInfo &Info) { |
| 9621 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 9622 | } |
| 9623 | |
| 9624 | void CGOpenMPSIMDRuntime::emitTargetDataStandAloneCall( |
| 9625 | CodeGenFunction &CGF, const OMPExecutableDirective &D, const Expr *IfCond, |
| 9626 | const Expr *Device) { |
| 9627 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 9628 | } |
| 9629 | |
| 9630 | void CGOpenMPSIMDRuntime::emitDoacrossInit(CodeGenFunction &CGF, |
Alexey Bataev | f138fda | 2018-08-13 19:04:24 +0000 | [diff] [blame] | 9631 | const OMPLoopDirective &D, |
| 9632 | ArrayRef<Expr *> NumIterations) { |
Alexey Bataev | a8a9153a | 2017-12-29 18:07:07 +0000 | [diff] [blame] | 9633 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 9634 | } |
| 9635 | |
| 9636 | void CGOpenMPSIMDRuntime::emitDoacrossOrdered(CodeGenFunction &CGF, |
| 9637 | const OMPDependClause *C) { |
| 9638 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 9639 | } |
| 9640 | |
| 9641 | const VarDecl * |
| 9642 | CGOpenMPSIMDRuntime::translateParameter(const FieldDecl *FD, |
| 9643 | const VarDecl *NativeParam) const { |
| 9644 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 9645 | } |
| 9646 | |
| 9647 | Address |
| 9648 | CGOpenMPSIMDRuntime::getParameterAddress(CodeGenFunction &CGF, |
| 9649 | const VarDecl *NativeParam, |
| 9650 | const VarDecl *TargetParam) const { |
| 9651 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 9652 | } |
| 9653 | |