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(); |
| 1226 | } |
| 1227 | |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 1228 | std::string CGOpenMPRuntime::getName(ArrayRef<StringRef> Parts) const { |
| 1229 | SmallString<128> Buffer; |
| 1230 | llvm::raw_svector_ostream OS(Buffer); |
| 1231 | StringRef Sep = FirstSeparator; |
| 1232 | for (StringRef Part : Parts) { |
| 1233 | OS << Sep << Part; |
| 1234 | Sep = Separator; |
| 1235 | } |
| 1236 | return OS.str(); |
| 1237 | } |
| 1238 | |
Alexey Bataev | c5b1d32 | 2016-03-04 09:22:22 +0000 | [diff] [blame] | 1239 | static llvm::Function * |
| 1240 | emitCombinerOrInitializer(CodeGenModule &CGM, QualType Ty, |
| 1241 | const Expr *CombinerInitializer, const VarDecl *In, |
| 1242 | const VarDecl *Out, bool IsCombiner) { |
| 1243 | // void .omp_combiner.(Ty *in, Ty *out); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1244 | ASTContext &C = CGM.getContext(); |
Alexey Bataev | c5b1d32 | 2016-03-04 09:22:22 +0000 | [diff] [blame] | 1245 | QualType PtrTy = C.getPointerType(Ty).withRestrict(); |
| 1246 | FunctionArgList Args; |
Alexey Bataev | c5b1d32 | 2016-03-04 09:22:22 +0000 | [diff] [blame] | 1247 | ImplicitParamDecl OmpOutParm(C, /*DC=*/nullptr, Out->getLocation(), |
Alexey Bataev | 5622323 | 2017-06-09 13:40:18 +0000 | [diff] [blame] | 1248 | /*Id=*/nullptr, PtrTy, ImplicitParamDecl::Other); |
Alexey Bataev | a839ddd | 2016-03-17 10:19:46 +0000 | [diff] [blame] | 1249 | ImplicitParamDecl OmpInParm(C, /*DC=*/nullptr, In->getLocation(), |
Alexey Bataev | 5622323 | 2017-06-09 13:40:18 +0000 | [diff] [blame] | 1250 | /*Id=*/nullptr, PtrTy, ImplicitParamDecl::Other); |
Alexey Bataev | c5b1d32 | 2016-03-04 09:22:22 +0000 | [diff] [blame] | 1251 | Args.push_back(&OmpOutParm); |
Alexey Bataev | a839ddd | 2016-03-17 10:19:46 +0000 | [diff] [blame] | 1252 | Args.push_back(&OmpInParm); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1253 | const CGFunctionInfo &FnInfo = |
John McCall | c56a8b3 | 2016-03-11 04:30:31 +0000 | [diff] [blame] | 1254 | CGM.getTypes().arrangeBuiltinFunctionDeclaration(C.VoidTy, Args); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1255 | llvm::FunctionType *FnTy = CGM.getTypes().GetFunctionType(FnInfo); |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 1256 | std::string Name = CGM.getOpenMPRuntime().getName( |
| 1257 | {IsCombiner ? "omp_combiner" : "omp_initializer", ""}); |
| 1258 | auto *Fn = llvm::Function::Create(FnTy, llvm::GlobalValue::InternalLinkage, |
| 1259 | Name, &CGM.getModule()); |
Rafael Espindola | 51ec5a9 | 2018-02-28 23:46:35 +0000 | [diff] [blame] | 1260 | CGM.SetInternalFunctionAttributes(GlobalDecl(), Fn, FnInfo); |
Chandler Carruth | fcd3314 | 2016-12-23 01:24:49 +0000 | [diff] [blame] | 1261 | Fn->removeFnAttr(llvm::Attribute::NoInline); |
Mehdi Amini | 6aa9e9b | 2017-05-29 05:38:20 +0000 | [diff] [blame] | 1262 | Fn->removeFnAttr(llvm::Attribute::OptimizeNone); |
Alexey Bataev | a839ddd | 2016-03-17 10:19:46 +0000 | [diff] [blame] | 1263 | Fn->addFnAttr(llvm::Attribute::AlwaysInline); |
Alexey Bataev | c5b1d32 | 2016-03-04 09:22:22 +0000 | [diff] [blame] | 1264 | CodeGenFunction CGF(CGM); |
| 1265 | // Map "T omp_in;" variable to "*omp_in_parm" value in all expressions. |
| 1266 | // 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] | 1267 | CGF.StartFunction(GlobalDecl(), C.VoidTy, Fn, FnInfo, Args, In->getLocation(), |
| 1268 | Out->getLocation()); |
Alexey Bataev | c5b1d32 | 2016-03-04 09:22:22 +0000 | [diff] [blame] | 1269 | CodeGenFunction::OMPPrivateScope Scope(CGF); |
| 1270 | Address AddrIn = CGF.GetAddrOfLocalVar(&OmpInParm); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1271 | Scope.addPrivate(In, [&CGF, AddrIn, PtrTy]() { |
Alexey Bataev | c5b1d32 | 2016-03-04 09:22:22 +0000 | [diff] [blame] | 1272 | return CGF.EmitLoadOfPointerLValue(AddrIn, PtrTy->castAs<PointerType>()) |
| 1273 | .getAddress(); |
| 1274 | }); |
| 1275 | Address AddrOut = CGF.GetAddrOfLocalVar(&OmpOutParm); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1276 | Scope.addPrivate(Out, [&CGF, AddrOut, PtrTy]() { |
Alexey Bataev | c5b1d32 | 2016-03-04 09:22:22 +0000 | [diff] [blame] | 1277 | return CGF.EmitLoadOfPointerLValue(AddrOut, PtrTy->castAs<PointerType>()) |
| 1278 | .getAddress(); |
| 1279 | }); |
| 1280 | (void)Scope.Privatize(); |
Alexey Bataev | 070f43a | 2017-09-06 14:49:58 +0000 | [diff] [blame] | 1281 | if (!IsCombiner && Out->hasInit() && |
| 1282 | !CGF.isTrivialInitializer(Out->getInit())) { |
| 1283 | CGF.EmitAnyExprToMem(Out->getInit(), CGF.GetAddrOfLocalVar(Out), |
| 1284 | Out->getType().getQualifiers(), |
| 1285 | /*IsInitializer=*/true); |
| 1286 | } |
| 1287 | if (CombinerInitializer) |
| 1288 | CGF.EmitIgnoredExpr(CombinerInitializer); |
Alexey Bataev | c5b1d32 | 2016-03-04 09:22:22 +0000 | [diff] [blame] | 1289 | Scope.ForceCleanup(); |
| 1290 | CGF.FinishFunction(); |
| 1291 | return Fn; |
| 1292 | } |
| 1293 | |
| 1294 | void CGOpenMPRuntime::emitUserDefinedReduction( |
| 1295 | CodeGenFunction *CGF, const OMPDeclareReductionDecl *D) { |
| 1296 | if (UDRMap.count(D) > 0) |
| 1297 | return; |
Alexey Bataev | c5b1d32 | 2016-03-04 09:22:22 +0000 | [diff] [blame] | 1298 | llvm::Function *Combiner = emitCombinerOrInitializer( |
Alexey Bataev | e6aa469 | 2018-09-13 16:54:05 +0000 | [diff] [blame] | 1299 | CGM, D->getType(), D->getCombiner(), |
| 1300 | cast<VarDecl>(cast<DeclRefExpr>(D->getCombinerIn())->getDecl()), |
| 1301 | cast<VarDecl>(cast<DeclRefExpr>(D->getCombinerOut())->getDecl()), |
Alexey Bataev | c5b1d32 | 2016-03-04 09:22:22 +0000 | [diff] [blame] | 1302 | /*IsCombiner=*/true); |
| 1303 | llvm::Function *Initializer = nullptr; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1304 | if (const Expr *Init = D->getInitializer()) { |
Alexey Bataev | c5b1d32 | 2016-03-04 09:22:22 +0000 | [diff] [blame] | 1305 | Initializer = emitCombinerOrInitializer( |
Alexey Bataev | 070f43a | 2017-09-06 14:49:58 +0000 | [diff] [blame] | 1306 | CGM, D->getType(), |
| 1307 | D->getInitializerKind() == OMPDeclareReductionDecl::CallInit ? Init |
| 1308 | : nullptr, |
Alexey Bataev | e6aa469 | 2018-09-13 16:54:05 +0000 | [diff] [blame] | 1309 | cast<VarDecl>(cast<DeclRefExpr>(D->getInitOrig())->getDecl()), |
| 1310 | cast<VarDecl>(cast<DeclRefExpr>(D->getInitPriv())->getDecl()), |
Alexey Bataev | c5b1d32 | 2016-03-04 09:22:22 +0000 | [diff] [blame] | 1311 | /*IsCombiner=*/false); |
| 1312 | } |
Alexey Bataev | 43a919f | 2018-04-13 17:48:43 +0000 | [diff] [blame] | 1313 | UDRMap.try_emplace(D, Combiner, Initializer); |
Alexey Bataev | c5b1d32 | 2016-03-04 09:22:22 +0000 | [diff] [blame] | 1314 | if (CGF) { |
| 1315 | auto &Decls = FunctionUDRMap.FindAndConstruct(CGF->CurFn); |
| 1316 | Decls.second.push_back(D); |
| 1317 | } |
| 1318 | } |
| 1319 | |
Alexey Bataev | a839ddd | 2016-03-17 10:19:46 +0000 | [diff] [blame] | 1320 | std::pair<llvm::Function *, llvm::Function *> |
| 1321 | CGOpenMPRuntime::getUserDefinedReduction(const OMPDeclareReductionDecl *D) { |
| 1322 | auto I = UDRMap.find(D); |
| 1323 | if (I != UDRMap.end()) |
| 1324 | return I->second; |
| 1325 | emitUserDefinedReduction(/*CGF=*/nullptr, D); |
| 1326 | return UDRMap.lookup(D); |
| 1327 | } |
| 1328 | |
Arpith Chacko Jacob | 19b911c | 2017-01-18 18:18:53 +0000 | [diff] [blame] | 1329 | static llvm::Value *emitParallelOrTeamsOutlinedFunction( |
| 1330 | CodeGenModule &CGM, const OMPExecutableDirective &D, const CapturedStmt *CS, |
| 1331 | const VarDecl *ThreadIDVar, OpenMPDirectiveKind InnermostKind, |
| 1332 | const StringRef OutlinedHelperName, const RegionCodeGenTy &CodeGen) { |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 1333 | assert(ThreadIDVar->getType()->isPointerType() && |
| 1334 | "thread id variable must be of type kmp_int32 *"); |
Alexey Bataev | 1809571 | 2014-10-10 12:19:54 +0000 | [diff] [blame] | 1335 | CodeGenFunction CGF(CGM, true); |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 1336 | bool HasCancel = false; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1337 | if (const auto *OPD = dyn_cast<OMPParallelDirective>(&D)) |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 1338 | HasCancel = OPD->hasCancel(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1339 | else if (const auto *OPSD = dyn_cast<OMPParallelSectionsDirective>(&D)) |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 1340 | HasCancel = OPSD->hasCancel(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1341 | else if (const auto *OPFD = dyn_cast<OMPParallelForDirective>(&D)) |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 1342 | HasCancel = OPFD->hasCancel(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1343 | else if (const auto *OPFD = dyn_cast<OMPTargetParallelForDirective>(&D)) |
Alexey Bataev | 2139ed6 | 2017-11-16 18:20:21 +0000 | [diff] [blame] | 1344 | HasCancel = OPFD->hasCancel(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1345 | else if (const auto *OPFD = dyn_cast<OMPDistributeParallelForDirective>(&D)) |
Alexey Bataev | 10a5431 | 2017-11-27 16:54:08 +0000 | [diff] [blame] | 1346 | HasCancel = OPFD->hasCancel(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1347 | else if (const auto *OPFD = |
| 1348 | dyn_cast<OMPTeamsDistributeParallelForDirective>(&D)) |
Alexey Bataev | 10a5431 | 2017-11-27 16:54:08 +0000 | [diff] [blame] | 1349 | HasCancel = OPFD->hasCancel(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1350 | else if (const auto *OPFD = |
Alexey Bataev | 10a5431 | 2017-11-27 16:54:08 +0000 | [diff] [blame] | 1351 | dyn_cast<OMPTargetTeamsDistributeParallelForDirective>(&D)) |
| 1352 | HasCancel = OPFD->hasCancel(); |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 1353 | CGOpenMPOutlinedRegionInfo CGInfo(*CS, ThreadIDVar, CodeGen, InnermostKind, |
Arpith Chacko Jacob | 19b911c | 2017-01-18 18:18:53 +0000 | [diff] [blame] | 1354 | HasCancel, OutlinedHelperName); |
Alexey Bataev | d157d47 | 2015-06-24 03:35:38 +0000 | [diff] [blame] | 1355 | CodeGenFunction::CGCapturedStmtRAII CapInfoRAII(CGF, &CGInfo); |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 1356 | return CGF.GenerateOpenMPCapturedStmtFunction(*CS); |
Alexey Bataev | 1809571 | 2014-10-10 12:19:54 +0000 | [diff] [blame] | 1357 | } |
| 1358 | |
Arpith Chacko Jacob | 19b911c | 2017-01-18 18:18:53 +0000 | [diff] [blame] | 1359 | llvm::Value *CGOpenMPRuntime::emitParallelOutlinedFunction( |
| 1360 | const OMPExecutableDirective &D, const VarDecl *ThreadIDVar, |
| 1361 | OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen) { |
| 1362 | const CapturedStmt *CS = D.getCapturedStmt(OMPD_parallel); |
| 1363 | return emitParallelOrTeamsOutlinedFunction( |
| 1364 | CGM, D, CS, ThreadIDVar, InnermostKind, getOutlinedHelperName(), CodeGen); |
| 1365 | } |
| 1366 | |
| 1367 | llvm::Value *CGOpenMPRuntime::emitTeamsOutlinedFunction( |
| 1368 | const OMPExecutableDirective &D, const VarDecl *ThreadIDVar, |
| 1369 | OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen) { |
| 1370 | const CapturedStmt *CS = D.getCapturedStmt(OMPD_teams); |
| 1371 | return emitParallelOrTeamsOutlinedFunction( |
| 1372 | CGM, D, CS, ThreadIDVar, InnermostKind, getOutlinedHelperName(), CodeGen); |
| 1373 | } |
| 1374 | |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 1375 | llvm::Value *CGOpenMPRuntime::emitTaskOutlinedFunction( |
| 1376 | const OMPExecutableDirective &D, const VarDecl *ThreadIDVar, |
Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 1377 | const VarDecl *PartIDVar, const VarDecl *TaskTVar, |
| 1378 | OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen, |
| 1379 | bool Tied, unsigned &NumberOfParts) { |
| 1380 | auto &&UntiedCodeGen = [this, &D, TaskTVar](CodeGenFunction &CGF, |
| 1381 | PrePostActionTy &) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1382 | llvm::Value *ThreadID = getThreadID(CGF, D.getBeginLoc()); |
| 1383 | llvm::Value *UpLoc = emitUpdateLocation(CGF, D.getBeginLoc()); |
Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 1384 | llvm::Value *TaskArgs[] = { |
| 1385 | UpLoc, ThreadID, |
| 1386 | CGF.EmitLoadOfPointerLValue(CGF.GetAddrOfLocalVar(TaskTVar), |
| 1387 | TaskTVar->getType()->castAs<PointerType>()) |
| 1388 | .getPointer()}; |
| 1389 | CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_omp_task), TaskArgs); |
| 1390 | }; |
| 1391 | CGOpenMPTaskOutlinedRegionInfo::UntiedTaskActionTy Action(Tied, PartIDVar, |
| 1392 | UntiedCodeGen); |
| 1393 | CodeGen.setAction(Action); |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 1394 | assert(!ThreadIDVar->getType()->isPointerType() && |
| 1395 | "thread id variable must be of type kmp_int32 for tasks"); |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 1396 | const OpenMPDirectiveKind Region = |
| 1397 | isOpenMPTaskLoopDirective(D.getDirectiveKind()) ? OMPD_taskloop |
| 1398 | : OMPD_task; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1399 | const CapturedStmt *CS = D.getCapturedStmt(Region); |
| 1400 | const auto *TD = dyn_cast<OMPTaskDirective>(&D); |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 1401 | CodeGenFunction CGF(CGM, true); |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 1402 | CGOpenMPTaskOutlinedRegionInfo CGInfo(*CS, ThreadIDVar, CodeGen, |
| 1403 | InnermostKind, |
| 1404 | TD ? TD->hasCancel() : false, Action); |
Alexey Bataev | d157d47 | 2015-06-24 03:35:38 +0000 | [diff] [blame] | 1405 | CodeGenFunction::CGCapturedStmtRAII CapInfoRAII(CGF, &CGInfo); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1406 | llvm::Value *Res = CGF.GenerateCapturedStmtFunction(*CS); |
Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 1407 | if (!Tied) |
| 1408 | NumberOfParts = Action.getNumberOfParts(); |
| 1409 | return Res; |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 1410 | } |
| 1411 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1412 | static void buildStructValue(ConstantStructBuilder &Fields, CodeGenModule &CGM, |
| 1413 | const RecordDecl *RD, const CGRecordLayout &RL, |
| 1414 | ArrayRef<llvm::Constant *> Data) { |
| 1415 | llvm::StructType *StructTy = RL.getLLVMType(); |
| 1416 | unsigned PrevIdx = 0; |
| 1417 | ConstantInitBuilder CIBuilder(CGM); |
| 1418 | auto DI = Data.begin(); |
| 1419 | for (const FieldDecl *FD : RD->fields()) { |
| 1420 | unsigned Idx = RL.getLLVMFieldNo(FD); |
| 1421 | // Fill the alignment. |
| 1422 | for (unsigned I = PrevIdx; I < Idx; ++I) |
| 1423 | Fields.add(llvm::Constant::getNullValue(StructTy->getElementType(I))); |
| 1424 | PrevIdx = Idx + 1; |
| 1425 | Fields.add(*DI); |
| 1426 | ++DI; |
| 1427 | } |
| 1428 | } |
| 1429 | |
| 1430 | template <class... As> |
| 1431 | static llvm::GlobalVariable * |
Mike Rice | e1ca7b6 | 2018-08-29 15:45:11 +0000 | [diff] [blame] | 1432 | createGlobalStruct(CodeGenModule &CGM, QualType Ty, bool IsConstant, |
| 1433 | ArrayRef<llvm::Constant *> Data, const Twine &Name, |
| 1434 | As &&... Args) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1435 | const auto *RD = cast<RecordDecl>(Ty->getAsTagDecl()); |
| 1436 | const CGRecordLayout &RL = CGM.getTypes().getCGRecordLayout(RD); |
| 1437 | ConstantInitBuilder CIBuilder(CGM); |
| 1438 | ConstantStructBuilder Fields = CIBuilder.beginStruct(RL.getLLVMType()); |
| 1439 | buildStructValue(Fields, CGM, RD, RL, Data); |
| 1440 | return Fields.finishAndCreateGlobal( |
Mike Rice | e1ca7b6 | 2018-08-29 15:45:11 +0000 | [diff] [blame] | 1441 | Name, CGM.getContext().getAlignOfGlobalVarInChars(Ty), IsConstant, |
| 1442 | std::forward<As>(Args)...); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1443 | } |
| 1444 | |
| 1445 | template <typename T> |
Benjamin Kramer | 651d0bf | 2018-05-15 21:26:47 +0000 | [diff] [blame] | 1446 | static void |
| 1447 | createConstantGlobalStructAndAddToParent(CodeGenModule &CGM, QualType Ty, |
| 1448 | ArrayRef<llvm::Constant *> Data, |
| 1449 | T &Parent) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1450 | const auto *RD = cast<RecordDecl>(Ty->getAsTagDecl()); |
| 1451 | const CGRecordLayout &RL = CGM.getTypes().getCGRecordLayout(RD); |
| 1452 | ConstantStructBuilder Fields = Parent.beginStruct(RL.getLLVMType()); |
| 1453 | buildStructValue(Fields, CGM, RD, RL, Data); |
| 1454 | Fields.finishAndAddTo(Parent); |
| 1455 | } |
| 1456 | |
Alexey Bataev | 50b3c95 | 2016-02-19 10:38:26 +0000 | [diff] [blame] | 1457 | Address CGOpenMPRuntime::getOrCreateDefaultLocation(unsigned Flags) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1458 | CharUnits Align = CGM.getContext().getTypeAlignInChars(IdentQTy); |
Alexey Bataev | 15007ba | 2014-05-07 06:18:01 +0000 | [diff] [blame] | 1459 | llvm::Value *Entry = OpenMPDefaultLocMap.lookup(Flags); |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1460 | if (!Entry) { |
| 1461 | if (!DefaultOpenMPPSource) { |
| 1462 | // Initialize default location for psource field of ident_t structure of |
| 1463 | // all ident_t objects. Format is ";file;function;line;column;;". |
| 1464 | // Taken from |
| 1465 | // http://llvm.org/svn/llvm-project/openmp/trunk/runtime/src/kmp_str.c |
| 1466 | DefaultOpenMPPSource = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1467 | CGM.GetAddrOfConstantCString(";unknown;unknown;0;0;;").getPointer(); |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1468 | DefaultOpenMPPSource = |
| 1469 | llvm::ConstantExpr::getBitCast(DefaultOpenMPPSource, CGM.Int8PtrTy); |
| 1470 | } |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1471 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1472 | llvm::Constant *Data[] = {llvm::ConstantInt::getNullValue(CGM.Int32Ty), |
| 1473 | llvm::ConstantInt::get(CGM.Int32Ty, Flags), |
| 1474 | llvm::ConstantInt::getNullValue(CGM.Int32Ty), |
| 1475 | llvm::ConstantInt::getNullValue(CGM.Int32Ty), |
| 1476 | DefaultOpenMPPSource}; |
Mike Rice | e1ca7b6 | 2018-08-29 15:45:11 +0000 | [diff] [blame] | 1477 | llvm::GlobalValue *DefaultOpenMPLocation = |
| 1478 | createGlobalStruct(CGM, IdentQTy, /*IsConstant=*/false, Data, "", |
| 1479 | llvm::GlobalValue::PrivateLinkage); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1480 | DefaultOpenMPLocation->setUnnamedAddr( |
| 1481 | llvm::GlobalValue::UnnamedAddr::Global); |
John McCall | 6c9f1fdb | 2016-11-19 08:17:24 +0000 | [diff] [blame] | 1482 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1483 | OpenMPDefaultLocMap[Flags] = Entry = DefaultOpenMPLocation; |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1484 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1485 | return Address(Entry, Align); |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1486 | } |
| 1487 | |
Alexey Bataev | fd006c4 | 2018-10-05 15:08:53 +0000 | [diff] [blame] | 1488 | void CGOpenMPRuntime::setLocThreadIdInsertPt(CodeGenFunction &CGF, |
| 1489 | bool AtCurrentPoint) { |
| 1490 | auto &Elem = OpenMPLocThreadIDMap.FindAndConstruct(CGF.CurFn); |
| 1491 | assert(!Elem.second.ServiceInsertPt && "Insert point is set already."); |
| 1492 | |
| 1493 | llvm::Value *Undef = llvm::UndefValue::get(CGF.Int32Ty); |
| 1494 | if (AtCurrentPoint) { |
| 1495 | Elem.second.ServiceInsertPt = new llvm::BitCastInst( |
| 1496 | Undef, CGF.Int32Ty, "svcpt", CGF.Builder.GetInsertBlock()); |
| 1497 | } else { |
| 1498 | Elem.second.ServiceInsertPt = |
| 1499 | new llvm::BitCastInst(Undef, CGF.Int32Ty, "svcpt"); |
| 1500 | Elem.second.ServiceInsertPt->insertAfter(CGF.AllocaInsertPt); |
| 1501 | } |
| 1502 | } |
| 1503 | |
| 1504 | void CGOpenMPRuntime::clearLocThreadIdInsertPt(CodeGenFunction &CGF) { |
| 1505 | auto &Elem = OpenMPLocThreadIDMap.FindAndConstruct(CGF.CurFn); |
| 1506 | if (Elem.second.ServiceInsertPt) { |
| 1507 | llvm::Instruction *Ptr = Elem.second.ServiceInsertPt; |
| 1508 | Elem.second.ServiceInsertPt = nullptr; |
| 1509 | Ptr->eraseFromParent(); |
| 1510 | } |
| 1511 | } |
| 1512 | |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 1513 | llvm::Value *CGOpenMPRuntime::emitUpdateLocation(CodeGenFunction &CGF, |
| 1514 | SourceLocation Loc, |
Alexey Bataev | 50b3c95 | 2016-02-19 10:38:26 +0000 | [diff] [blame] | 1515 | unsigned Flags) { |
| 1516 | Flags |= OMP_IDENT_KMPC; |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1517 | // If no debug info is generated - return global default location. |
Benjamin Kramer | 8c30592 | 2016-02-02 11:06:51 +0000 | [diff] [blame] | 1518 | if (CGM.getCodeGenOpts().getDebugInfo() == codegenoptions::NoDebugInfo || |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1519 | Loc.isInvalid()) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1520 | return getOrCreateDefaultLocation(Flags).getPointer(); |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1521 | |
| 1522 | assert(CGF.CurFn && "No function in current CodeGenFunction."); |
| 1523 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1524 | CharUnits Align = CGM.getContext().getTypeAlignInChars(IdentQTy); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1525 | Address LocValue = Address::invalid(); |
Alexey Bataev | 1e4b713 | 2014-12-03 12:11:24 +0000 | [diff] [blame] | 1526 | auto I = OpenMPLocThreadIDMap.find(CGF.CurFn); |
| 1527 | if (I != OpenMPLocThreadIDMap.end()) |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1528 | LocValue = Address(I->second.DebugLoc, Align); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1529 | |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 1530 | // OpenMPLocThreadIDMap may have null DebugLoc and non-null ThreadID, if |
| 1531 | // GetOpenMPThreadID was called before this routine. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1532 | if (!LocValue.isValid()) { |
Alexey Bataev | 15007ba | 2014-05-07 06:18:01 +0000 | [diff] [blame] | 1533 | // Generate "ident_t .kmpc_loc.addr;" |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1534 | Address AI = CGF.CreateMemTemp(IdentQTy, ".kmpc_loc.addr"); |
Alexey Bataev | 1809571 | 2014-10-10 12:19:54 +0000 | [diff] [blame] | 1535 | auto &Elem = OpenMPLocThreadIDMap.FindAndConstruct(CGF.CurFn); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1536 | Elem.second.DebugLoc = AI.getPointer(); |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1537 | LocValue = AI; |
| 1538 | |
Alexey Bataev | fd006c4 | 2018-10-05 15:08:53 +0000 | [diff] [blame] | 1539 | if (!Elem.second.ServiceInsertPt) |
| 1540 | setLocThreadIdInsertPt(CGF); |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1541 | CGBuilderTy::InsertPointGuard IPG(CGF.Builder); |
Alexey Bataev | fd006c4 | 2018-10-05 15:08:53 +0000 | [diff] [blame] | 1542 | CGF.Builder.SetInsertPoint(Elem.second.ServiceInsertPt); |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 1543 | CGF.Builder.CreateMemCpy(LocValue, getOrCreateDefaultLocation(Flags), |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1544 | CGF.getTypeSize(IdentQTy)); |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1545 | } |
| 1546 | |
| 1547 | // char **psource = &.kmpc_loc_<flags>.addr.psource; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1548 | LValue Base = CGF.MakeAddrLValue(LocValue, IdentQTy); |
| 1549 | auto Fields = cast<RecordDecl>(IdentQTy->getAsTagDecl())->field_begin(); |
| 1550 | LValue PSource = |
| 1551 | CGF.EmitLValueForField(Base, *std::next(Fields, IdentField_PSource)); |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1552 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1553 | llvm::Value *OMPDebugLoc = OpenMPDebugLocMap.lookup(Loc.getRawEncoding()); |
Alexey Bataev | f002aca | 2014-05-30 05:48:40 +0000 | [diff] [blame] | 1554 | if (OMPDebugLoc == nullptr) { |
| 1555 | SmallString<128> Buffer2; |
| 1556 | llvm::raw_svector_ostream OS2(Buffer2); |
| 1557 | // Build debug location |
| 1558 | PresumedLoc PLoc = CGF.getContext().getSourceManager().getPresumedLoc(Loc); |
| 1559 | OS2 << ";" << PLoc.getFilename() << ";"; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1560 | if (const auto *FD = dyn_cast_or_null<FunctionDecl>(CGF.CurFuncDecl)) |
Alexey Bataev | f002aca | 2014-05-30 05:48:40 +0000 | [diff] [blame] | 1561 | OS2 << FD->getQualifiedNameAsString(); |
Alexey Bataev | f002aca | 2014-05-30 05:48:40 +0000 | [diff] [blame] | 1562 | OS2 << ";" << PLoc.getLine() << ";" << PLoc.getColumn() << ";;"; |
| 1563 | OMPDebugLoc = CGF.Builder.CreateGlobalStringPtr(OS2.str()); |
| 1564 | OpenMPDebugLocMap[Loc.getRawEncoding()] = OMPDebugLoc; |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1565 | } |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1566 | // *psource = ";<File>;<Function>;<Line>;<Column>;;"; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1567 | CGF.EmitStoreOfScalar(OMPDebugLoc, PSource); |
Alexey Bataev | f002aca | 2014-05-30 05:48:40 +0000 | [diff] [blame] | 1568 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1569 | // Our callers always pass this to a runtime function, so for |
| 1570 | // convenience, go ahead and return a naked pointer. |
| 1571 | return LocValue.getPointer(); |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1572 | } |
| 1573 | |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 1574 | llvm::Value *CGOpenMPRuntime::getThreadID(CodeGenFunction &CGF, |
| 1575 | SourceLocation Loc) { |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1576 | assert(CGF.CurFn && "No function in current CodeGenFunction."); |
| 1577 | |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 1578 | llvm::Value *ThreadID = nullptr; |
Alexey Bataev | 1809571 | 2014-10-10 12:19:54 +0000 | [diff] [blame] | 1579 | // Check whether we've already cached a load of the thread id in this |
| 1580 | // function. |
Alexey Bataev | 1e4b713 | 2014-12-03 12:11:24 +0000 | [diff] [blame] | 1581 | auto I = OpenMPLocThreadIDMap.find(CGF.CurFn); |
Alexey Bataev | 1809571 | 2014-10-10 12:19:54 +0000 | [diff] [blame] | 1582 | if (I != OpenMPLocThreadIDMap.end()) { |
| 1583 | ThreadID = I->second.ThreadID; |
Alexey Bataev | 03b340a | 2014-10-21 03:16:40 +0000 | [diff] [blame] | 1584 | if (ThreadID != nullptr) |
| 1585 | return ThreadID; |
| 1586 | } |
Alexey Bataev | aee1855 | 2017-08-16 14:01:00 +0000 | [diff] [blame] | 1587 | // If exceptions are enabled, do not use parameter to avoid possible crash. |
Alexey Bataev | 5d2c9a4 | 2017-11-02 18:55:05 +0000 | [diff] [blame] | 1588 | if (!CGF.EHStack.requiresLandingPad() || !CGF.getLangOpts().Exceptions || |
| 1589 | !CGF.getLangOpts().CXXExceptions || |
Alexey Bataev | 0e1b458 | 2017-11-02 14:25:34 +0000 | [diff] [blame] | 1590 | CGF.Builder.GetInsertBlock() == CGF.AllocaInsertPt->getParent()) { |
Alexey Bataev | aee1855 | 2017-08-16 14:01:00 +0000 | [diff] [blame] | 1591 | if (auto *OMPRegionInfo = |
| 1592 | dyn_cast_or_null<CGOpenMPRegionInfo>(CGF.CapturedStmtInfo)) { |
| 1593 | if (OMPRegionInfo->getThreadIDVariable()) { |
| 1594 | // Check if this an outlined function with thread id passed as argument. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1595 | LValue LVal = OMPRegionInfo->getThreadIDVariableLValue(CGF); |
Alexey Bataev | 1e49137 | 2018-01-23 18:44:14 +0000 | [diff] [blame] | 1596 | ThreadID = CGF.EmitLoadOfScalar(LVal, Loc); |
Alexey Bataev | aee1855 | 2017-08-16 14:01:00 +0000 | [diff] [blame] | 1597 | // If value loaded in entry block, cache it and use it everywhere in |
| 1598 | // function. |
| 1599 | if (CGF.Builder.GetInsertBlock() == CGF.AllocaInsertPt->getParent()) { |
| 1600 | auto &Elem = OpenMPLocThreadIDMap.FindAndConstruct(CGF.CurFn); |
| 1601 | Elem.second.ThreadID = ThreadID; |
| 1602 | } |
| 1603 | return ThreadID; |
Alexey Bataev | 8cbe0a6 | 2015-02-26 10:27:34 +0000 | [diff] [blame] | 1604 | } |
Alexey Bataev | d6c5755 | 2014-07-25 07:55:17 +0000 | [diff] [blame] | 1605 | } |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1606 | } |
Alexey Bataev | 8cbe0a6 | 2015-02-26 10:27:34 +0000 | [diff] [blame] | 1607 | |
| 1608 | // This is not an outlined function region - need to call __kmpc_int32 |
| 1609 | // kmpc_global_thread_num(ident_t *loc). |
| 1610 | // Generate thread id value and cache this value for use across the |
| 1611 | // function. |
Alexey Bataev | fd006c4 | 2018-10-05 15:08:53 +0000 | [diff] [blame] | 1612 | auto &Elem = OpenMPLocThreadIDMap.FindAndConstruct(CGF.CurFn); |
| 1613 | if (!Elem.second.ServiceInsertPt) |
| 1614 | setLocThreadIdInsertPt(CGF); |
Alexey Bataev | 8cbe0a6 | 2015-02-26 10:27:34 +0000 | [diff] [blame] | 1615 | CGBuilderTy::InsertPointGuard IPG(CGF.Builder); |
Alexey Bataev | fd006c4 | 2018-10-05 15:08:53 +0000 | [diff] [blame] | 1616 | CGF.Builder.SetInsertPoint(Elem.second.ServiceInsertPt); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1617 | llvm::CallInst *Call = CGF.Builder.CreateCall( |
Alexey Bataev | 0e1b458 | 2017-11-02 14:25:34 +0000 | [diff] [blame] | 1618 | createRuntimeFunction(OMPRTL__kmpc_global_thread_num), |
| 1619 | emitUpdateLocation(CGF, Loc)); |
| 1620 | Call->setCallingConv(CGF.getRuntimeCC()); |
Alexey Bataev | 0e1b458 | 2017-11-02 14:25:34 +0000 | [diff] [blame] | 1621 | Elem.second.ThreadID = Call; |
| 1622 | return Call; |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1623 | } |
| 1624 | |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 1625 | void CGOpenMPRuntime::functionFinished(CodeGenFunction &CGF) { |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1626 | assert(CGF.CurFn && "No function in current CodeGenFunction."); |
Alexey Bataev | fd006c4 | 2018-10-05 15:08:53 +0000 | [diff] [blame] | 1627 | if (OpenMPLocThreadIDMap.count(CGF.CurFn)) { |
| 1628 | clearLocThreadIdInsertPt(CGF); |
Alexey Bataev | 03b340a | 2014-10-21 03:16:40 +0000 | [diff] [blame] | 1629 | OpenMPLocThreadIDMap.erase(CGF.CurFn); |
Alexey Bataev | fd006c4 | 2018-10-05 15:08:53 +0000 | [diff] [blame] | 1630 | } |
Alexey Bataev | c5b1d32 | 2016-03-04 09:22:22 +0000 | [diff] [blame] | 1631 | if (FunctionUDRMap.count(CGF.CurFn) > 0) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1632 | for(auto *D : FunctionUDRMap[CGF.CurFn]) |
Alexey Bataev | c5b1d32 | 2016-03-04 09:22:22 +0000 | [diff] [blame] | 1633 | UDRMap.erase(D); |
Alexey Bataev | c5b1d32 | 2016-03-04 09:22:22 +0000 | [diff] [blame] | 1634 | FunctionUDRMap.erase(CGF.CurFn); |
| 1635 | } |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1636 | } |
| 1637 | |
| 1638 | llvm::Type *CGOpenMPRuntime::getIdentTyPointerTy() { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1639 | return IdentTy->getPointerTo(); |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1640 | } |
| 1641 | |
| 1642 | llvm::Type *CGOpenMPRuntime::getKmpc_MicroPointerTy() { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 1643 | if (!Kmpc_MicroTy) { |
| 1644 | // Build void (*kmpc_micro)(kmp_int32 *global_tid, kmp_int32 *bound_tid,...) |
| 1645 | llvm::Type *MicroParams[] = {llvm::PointerType::getUnqual(CGM.Int32Ty), |
| 1646 | llvm::PointerType::getUnqual(CGM.Int32Ty)}; |
| 1647 | Kmpc_MicroTy = llvm::FunctionType::get(CGM.VoidTy, MicroParams, true); |
| 1648 | } |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1649 | return llvm::PointerType::getUnqual(Kmpc_MicroTy); |
| 1650 | } |
| 1651 | |
| 1652 | llvm::Constant * |
Alexey Bataev | 50b3c95 | 2016-02-19 10:38:26 +0000 | [diff] [blame] | 1653 | CGOpenMPRuntime::createRuntimeFunction(unsigned Function) { |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1654 | llvm::Constant *RTLFn = nullptr; |
Alexey Bataev | 50b3c95 | 2016-02-19 10:38:26 +0000 | [diff] [blame] | 1655 | switch (static_cast<OpenMPRTLFunction>(Function)) { |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1656 | case OMPRTL__kmpc_fork_call: { |
| 1657 | // Build void __kmpc_fork_call(ident_t *loc, kmp_int32 argc, kmpc_micro |
| 1658 | // microtask, ...); |
Alexey Bataev | 23b6942 | 2014-06-18 07:08:49 +0000 | [diff] [blame] | 1659 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty, |
| 1660 | getKmpc_MicroPointerTy()}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1661 | auto *FnTy = |
Alexey Bataev | d74d060 | 2014-10-13 06:02:40 +0000 | [diff] [blame] | 1662 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ true); |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1663 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_fork_call"); |
| 1664 | break; |
| 1665 | } |
| 1666 | case OMPRTL__kmpc_global_thread_num: { |
| 1667 | // Build kmp_int32 __kmpc_global_thread_num(ident_t *loc); |
Alexey Bataev | 23b6942 | 2014-06-18 07:08:49 +0000 | [diff] [blame] | 1668 | llvm::Type *TypeParams[] = {getIdentTyPointerTy()}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1669 | auto *FnTy = |
Alexey Bataev | d74d060 | 2014-10-13 06:02:40 +0000 | [diff] [blame] | 1670 | llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg*/ false); |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1671 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_global_thread_num"); |
| 1672 | break; |
| 1673 | } |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 1674 | case OMPRTL__kmpc_threadprivate_cached: { |
| 1675 | // Build void *__kmpc_threadprivate_cached(ident_t *loc, |
| 1676 | // kmp_int32 global_tid, void *data, size_t size, void ***cache); |
| 1677 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty, |
| 1678 | CGM.VoidPtrTy, CGM.SizeTy, |
| 1679 | CGM.VoidPtrTy->getPointerTo()->getPointerTo()}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1680 | auto *FnTy = |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 1681 | llvm::FunctionType::get(CGM.VoidPtrTy, TypeParams, /*isVarArg*/ false); |
| 1682 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_threadprivate_cached"); |
| 1683 | break; |
| 1684 | } |
Alexey Bataev | 3a3bf0b | 2014-09-22 10:01:53 +0000 | [diff] [blame] | 1685 | case OMPRTL__kmpc_critical: { |
Alexey Bataev | f947218 | 2014-09-22 12:32:31 +0000 | [diff] [blame] | 1686 | // Build void __kmpc_critical(ident_t *loc, kmp_int32 global_tid, |
| 1687 | // kmp_critical_name *crit); |
Alexey Bataev | 3a3bf0b | 2014-09-22 10:01:53 +0000 | [diff] [blame] | 1688 | llvm::Type *TypeParams[] = { |
| 1689 | getIdentTyPointerTy(), CGM.Int32Ty, |
| 1690 | llvm::PointerType::getUnqual(KmpCriticalNameTy)}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1691 | auto *FnTy = |
Alexey Bataev | 3a3bf0b | 2014-09-22 10:01:53 +0000 | [diff] [blame] | 1692 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false); |
| 1693 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_critical"); |
| 1694 | break; |
| 1695 | } |
Alexey Bataev | fc57d16 | 2015-12-15 10:55:09 +0000 | [diff] [blame] | 1696 | case OMPRTL__kmpc_critical_with_hint: { |
| 1697 | // Build void __kmpc_critical_with_hint(ident_t *loc, kmp_int32 global_tid, |
| 1698 | // kmp_critical_name *crit, uintptr_t hint); |
| 1699 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty, |
| 1700 | llvm::PointerType::getUnqual(KmpCriticalNameTy), |
| 1701 | CGM.IntPtrTy}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1702 | auto *FnTy = |
Alexey Bataev | fc57d16 | 2015-12-15 10:55:09 +0000 | [diff] [blame] | 1703 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false); |
| 1704 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_critical_with_hint"); |
| 1705 | break; |
| 1706 | } |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 1707 | case OMPRTL__kmpc_threadprivate_register: { |
| 1708 | // Build void __kmpc_threadprivate_register(ident_t *, void *data, |
| 1709 | // kmpc_ctor ctor, kmpc_cctor cctor, kmpc_dtor dtor); |
| 1710 | // typedef void *(*kmpc_ctor)(void *); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1711 | auto *KmpcCtorTy = |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 1712 | llvm::FunctionType::get(CGM.VoidPtrTy, CGM.VoidPtrTy, |
| 1713 | /*isVarArg*/ false)->getPointerTo(); |
| 1714 | // typedef void *(*kmpc_cctor)(void *, void *); |
| 1715 | llvm::Type *KmpcCopyCtorTyArgs[] = {CGM.VoidPtrTy, CGM.VoidPtrTy}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1716 | auto *KmpcCopyCtorTy = |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 1717 | llvm::FunctionType::get(CGM.VoidPtrTy, KmpcCopyCtorTyArgs, |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1718 | /*isVarArg*/ false) |
| 1719 | ->getPointerTo(); |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 1720 | // typedef void (*kmpc_dtor)(void *); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1721 | auto *KmpcDtorTy = |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 1722 | llvm::FunctionType::get(CGM.VoidTy, CGM.VoidPtrTy, /*isVarArg*/ false) |
| 1723 | ->getPointerTo(); |
| 1724 | llvm::Type *FnTyArgs[] = {getIdentTyPointerTy(), CGM.VoidPtrTy, KmpcCtorTy, |
| 1725 | KmpcCopyCtorTy, KmpcDtorTy}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1726 | auto *FnTy = llvm::FunctionType::get(CGM.VoidTy, FnTyArgs, |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 1727 | /*isVarArg*/ false); |
| 1728 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_threadprivate_register"); |
| 1729 | break; |
| 1730 | } |
Alexey Bataev | 3a3bf0b | 2014-09-22 10:01:53 +0000 | [diff] [blame] | 1731 | case OMPRTL__kmpc_end_critical: { |
Alexey Bataev | f947218 | 2014-09-22 12:32:31 +0000 | [diff] [blame] | 1732 | // Build void __kmpc_end_critical(ident_t *loc, kmp_int32 global_tid, |
| 1733 | // kmp_critical_name *crit); |
Alexey Bataev | 3a3bf0b | 2014-09-22 10:01:53 +0000 | [diff] [blame] | 1734 | llvm::Type *TypeParams[] = { |
| 1735 | getIdentTyPointerTy(), CGM.Int32Ty, |
| 1736 | llvm::PointerType::getUnqual(KmpCriticalNameTy)}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1737 | auto *FnTy = |
Alexey Bataev | 3a3bf0b | 2014-09-22 10:01:53 +0000 | [diff] [blame] | 1738 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false); |
| 1739 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_end_critical"); |
| 1740 | break; |
| 1741 | } |
Alexey Bataev | 8f7c1b0 | 2014-12-05 04:09:23 +0000 | [diff] [blame] | 1742 | case OMPRTL__kmpc_cancel_barrier: { |
| 1743 | // Build kmp_int32 __kmpc_cancel_barrier(ident_t *loc, kmp_int32 |
| 1744 | // global_tid); |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 1745 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1746 | auto *FnTy = |
Alexey Bataev | 8f7c1b0 | 2014-12-05 04:09:23 +0000 | [diff] [blame] | 1747 | llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg*/ false); |
| 1748 | RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name*/ "__kmpc_cancel_barrier"); |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 1749 | break; |
| 1750 | } |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 1751 | case OMPRTL__kmpc_barrier: { |
Alexey Bataev | 7d5d33e | 2015-07-06 05:50:32 +0000 | [diff] [blame] | 1752 | // Build void __kmpc_barrier(ident_t *loc, kmp_int32 global_tid); |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 1753 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1754 | auto *FnTy = |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 1755 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false); |
| 1756 | RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name*/ "__kmpc_barrier"); |
| 1757 | break; |
| 1758 | } |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 1759 | case OMPRTL__kmpc_for_static_fini: { |
| 1760 | // Build void __kmpc_for_static_fini(ident_t *loc, kmp_int32 global_tid); |
| 1761 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1762 | auto *FnTy = |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 1763 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false); |
| 1764 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_for_static_fini"); |
| 1765 | break; |
| 1766 | } |
Alexey Bataev | b205978 | 2014-10-13 08:23:51 +0000 | [diff] [blame] | 1767 | case OMPRTL__kmpc_push_num_threads: { |
| 1768 | // Build void __kmpc_push_num_threads(ident_t *loc, kmp_int32 global_tid, |
| 1769 | // kmp_int32 num_threads) |
| 1770 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty, |
| 1771 | CGM.Int32Ty}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1772 | auto *FnTy = |
Alexey Bataev | b205978 | 2014-10-13 08:23:51 +0000 | [diff] [blame] | 1773 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false); |
| 1774 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_push_num_threads"); |
| 1775 | break; |
| 1776 | } |
Alexey Bataev | d74d060 | 2014-10-13 06:02:40 +0000 | [diff] [blame] | 1777 | case OMPRTL__kmpc_serialized_parallel: { |
| 1778 | // Build void __kmpc_serialized_parallel(ident_t *loc, kmp_int32 |
| 1779 | // global_tid); |
| 1780 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1781 | auto *FnTy = |
Alexey Bataev | d74d060 | 2014-10-13 06:02:40 +0000 | [diff] [blame] | 1782 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false); |
| 1783 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_serialized_parallel"); |
| 1784 | break; |
| 1785 | } |
| 1786 | case OMPRTL__kmpc_end_serialized_parallel: { |
| 1787 | // Build void __kmpc_end_serialized_parallel(ident_t *loc, kmp_int32 |
| 1788 | // global_tid); |
| 1789 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1790 | auto *FnTy = |
Alexey Bataev | d74d060 | 2014-10-13 06:02:40 +0000 | [diff] [blame] | 1791 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false); |
| 1792 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_end_serialized_parallel"); |
| 1793 | break; |
| 1794 | } |
Alexey Bataev | cc37cc1 | 2014-11-20 04:34:54 +0000 | [diff] [blame] | 1795 | case OMPRTL__kmpc_flush: { |
Alexey Bataev | d76df6d | 2015-02-24 12:55:09 +0000 | [diff] [blame] | 1796 | // Build void __kmpc_flush(ident_t *loc); |
Alexey Bataev | cc37cc1 | 2014-11-20 04:34:54 +0000 | [diff] [blame] | 1797 | llvm::Type *TypeParams[] = {getIdentTyPointerTy()}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1798 | auto *FnTy = |
Alexey Bataev | d76df6d | 2015-02-24 12:55:09 +0000 | [diff] [blame] | 1799 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false); |
Alexey Bataev | cc37cc1 | 2014-11-20 04:34:54 +0000 | [diff] [blame] | 1800 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_flush"); |
| 1801 | break; |
| 1802 | } |
Alexey Bataev | 8d69065 | 2014-12-04 07:23:53 +0000 | [diff] [blame] | 1803 | case OMPRTL__kmpc_master: { |
| 1804 | // Build kmp_int32 __kmpc_master(ident_t *loc, kmp_int32 global_tid); |
| 1805 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1806 | auto *FnTy = |
Alexey Bataev | 8d69065 | 2014-12-04 07:23:53 +0000 | [diff] [blame] | 1807 | llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg=*/false); |
| 1808 | RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_master"); |
| 1809 | break; |
| 1810 | } |
| 1811 | case OMPRTL__kmpc_end_master: { |
| 1812 | // Build void __kmpc_end_master(ident_t *loc, kmp_int32 global_tid); |
| 1813 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1814 | auto *FnTy = |
Alexey Bataev | 8d69065 | 2014-12-04 07:23:53 +0000 | [diff] [blame] | 1815 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false); |
| 1816 | RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_end_master"); |
| 1817 | break; |
| 1818 | } |
Alexey Bataev | 9f797f3 | 2015-02-05 05:57:51 +0000 | [diff] [blame] | 1819 | case OMPRTL__kmpc_omp_taskyield: { |
| 1820 | // Build kmp_int32 __kmpc_omp_taskyield(ident_t *, kmp_int32 global_tid, |
| 1821 | // int end_part); |
| 1822 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty, CGM.IntTy}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1823 | auto *FnTy = |
Alexey Bataev | 9f797f3 | 2015-02-05 05:57:51 +0000 | [diff] [blame] | 1824 | llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg=*/false); |
| 1825 | RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_omp_taskyield"); |
| 1826 | break; |
| 1827 | } |
Alexey Bataev | 6956e2e | 2015-02-05 06:35:41 +0000 | [diff] [blame] | 1828 | case OMPRTL__kmpc_single: { |
| 1829 | // Build kmp_int32 __kmpc_single(ident_t *loc, kmp_int32 global_tid); |
| 1830 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1831 | auto *FnTy = |
Alexey Bataev | 6956e2e | 2015-02-05 06:35:41 +0000 | [diff] [blame] | 1832 | llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg=*/false); |
| 1833 | RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_single"); |
| 1834 | break; |
| 1835 | } |
| 1836 | case OMPRTL__kmpc_end_single: { |
| 1837 | // Build void __kmpc_end_single(ident_t *loc, kmp_int32 global_tid); |
| 1838 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1839 | auto *FnTy = |
Alexey Bataev | 6956e2e | 2015-02-05 06:35:41 +0000 | [diff] [blame] | 1840 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false); |
| 1841 | RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_end_single"); |
| 1842 | break; |
| 1843 | } |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 1844 | case OMPRTL__kmpc_omp_task_alloc: { |
| 1845 | // Build kmp_task_t *__kmpc_omp_task_alloc(ident_t *, kmp_int32 gtid, |
| 1846 | // kmp_int32 flags, size_t sizeof_kmp_task_t, size_t sizeof_shareds, |
| 1847 | // kmp_routine_entry_t *task_entry); |
| 1848 | assert(KmpRoutineEntryPtrTy != nullptr && |
| 1849 | "Type kmp_routine_entry_t must be created."); |
| 1850 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty, CGM.Int32Ty, |
| 1851 | CGM.SizeTy, CGM.SizeTy, KmpRoutineEntryPtrTy}; |
| 1852 | // Return void * and then cast to particular kmp_task_t type. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1853 | auto *FnTy = |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 1854 | llvm::FunctionType::get(CGM.VoidPtrTy, TypeParams, /*isVarArg=*/false); |
| 1855 | RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_omp_task_alloc"); |
| 1856 | break; |
| 1857 | } |
| 1858 | case OMPRTL__kmpc_omp_task: { |
| 1859 | // Build kmp_int32 __kmpc_omp_task(ident_t *, kmp_int32 gtid, kmp_task_t |
| 1860 | // *new_task); |
| 1861 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty, |
| 1862 | CGM.VoidPtrTy}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1863 | auto *FnTy = |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 1864 | llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg=*/false); |
| 1865 | RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_omp_task"); |
| 1866 | break; |
| 1867 | } |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 1868 | case OMPRTL__kmpc_copyprivate: { |
| 1869 | // Build void __kmpc_copyprivate(ident_t *loc, kmp_int32 global_tid, |
Alexey Bataev | 66beaa9 | 2015-04-30 03:47:32 +0000 | [diff] [blame] | 1870 | // size_t cpy_size, void *cpy_data, void(*cpy_func)(void *, void *), |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 1871 | // kmp_int32 didit); |
| 1872 | llvm::Type *CpyTypeParams[] = {CGM.VoidPtrTy, CGM.VoidPtrTy}; |
| 1873 | auto *CpyFnTy = |
| 1874 | llvm::FunctionType::get(CGM.VoidTy, CpyTypeParams, /*isVarArg=*/false); |
Alexey Bataev | 66beaa9 | 2015-04-30 03:47:32 +0000 | [diff] [blame] | 1875 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty, CGM.SizeTy, |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 1876 | CGM.VoidPtrTy, CpyFnTy->getPointerTo(), |
| 1877 | CGM.Int32Ty}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1878 | auto *FnTy = |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 1879 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false); |
| 1880 | RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_copyprivate"); |
| 1881 | break; |
| 1882 | } |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 1883 | case OMPRTL__kmpc_reduce: { |
| 1884 | // Build kmp_int32 __kmpc_reduce(ident_t *loc, kmp_int32 global_tid, |
| 1885 | // kmp_int32 num_vars, size_t reduce_size, void *reduce_data, void |
| 1886 | // (*reduce_func)(void *lhs_data, void *rhs_data), kmp_critical_name *lck); |
| 1887 | llvm::Type *ReduceTypeParams[] = {CGM.VoidPtrTy, CGM.VoidPtrTy}; |
| 1888 | auto *ReduceFnTy = llvm::FunctionType::get(CGM.VoidTy, ReduceTypeParams, |
| 1889 | /*isVarArg=*/false); |
| 1890 | llvm::Type *TypeParams[] = { |
| 1891 | getIdentTyPointerTy(), CGM.Int32Ty, CGM.Int32Ty, CGM.SizeTy, |
| 1892 | CGM.VoidPtrTy, ReduceFnTy->getPointerTo(), |
| 1893 | llvm::PointerType::getUnqual(KmpCriticalNameTy)}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1894 | auto *FnTy = |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 1895 | llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg=*/false); |
| 1896 | RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_reduce"); |
| 1897 | break; |
| 1898 | } |
| 1899 | case OMPRTL__kmpc_reduce_nowait: { |
| 1900 | // Build kmp_int32 __kmpc_reduce_nowait(ident_t *loc, kmp_int32 |
| 1901 | // global_tid, kmp_int32 num_vars, size_t reduce_size, void *reduce_data, |
| 1902 | // void (*reduce_func)(void *lhs_data, void *rhs_data), kmp_critical_name |
| 1903 | // *lck); |
| 1904 | llvm::Type *ReduceTypeParams[] = {CGM.VoidPtrTy, CGM.VoidPtrTy}; |
| 1905 | auto *ReduceFnTy = llvm::FunctionType::get(CGM.VoidTy, ReduceTypeParams, |
| 1906 | /*isVarArg=*/false); |
| 1907 | llvm::Type *TypeParams[] = { |
| 1908 | getIdentTyPointerTy(), CGM.Int32Ty, CGM.Int32Ty, CGM.SizeTy, |
| 1909 | CGM.VoidPtrTy, ReduceFnTy->getPointerTo(), |
| 1910 | llvm::PointerType::getUnqual(KmpCriticalNameTy)}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1911 | auto *FnTy = |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 1912 | llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg=*/false); |
| 1913 | RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_reduce_nowait"); |
| 1914 | break; |
| 1915 | } |
| 1916 | case OMPRTL__kmpc_end_reduce: { |
| 1917 | // Build void __kmpc_end_reduce(ident_t *loc, kmp_int32 global_tid, |
| 1918 | // kmp_critical_name *lck); |
| 1919 | llvm::Type *TypeParams[] = { |
| 1920 | getIdentTyPointerTy(), CGM.Int32Ty, |
| 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.VoidTy, TypeParams, /*isVarArg=*/false); |
| 1924 | RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_end_reduce"); |
| 1925 | break; |
| 1926 | } |
| 1927 | case OMPRTL__kmpc_end_reduce_nowait: { |
| 1928 | // Build __kmpc_end_reduce_nowait(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 = |
| 1936 | CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_end_reduce_nowait"); |
| 1937 | break; |
| 1938 | } |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 1939 | case OMPRTL__kmpc_omp_task_begin_if0: { |
| 1940 | // Build void __kmpc_omp_task(ident_t *, kmp_int32 gtid, kmp_task_t |
| 1941 | // *new_task); |
| 1942 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty, |
| 1943 | CGM.VoidPtrTy}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1944 | auto *FnTy = |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 1945 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false); |
| 1946 | RTLFn = |
| 1947 | CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_omp_task_begin_if0"); |
| 1948 | break; |
| 1949 | } |
| 1950 | case OMPRTL__kmpc_omp_task_complete_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 = CGM.CreateRuntimeFunction(FnTy, |
| 1958 | /*Name=*/"__kmpc_omp_task_complete_if0"); |
| 1959 | break; |
| 1960 | } |
Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 1961 | case OMPRTL__kmpc_ordered: { |
| 1962 | // Build void __kmpc_ordered(ident_t *loc, kmp_int32 global_tid); |
| 1963 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1964 | auto *FnTy = |
Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 1965 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false); |
| 1966 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_ordered"); |
| 1967 | break; |
| 1968 | } |
| 1969 | case OMPRTL__kmpc_end_ordered: { |
Alexey Bataev | c30dd2d | 2015-06-18 12:14:09 +0000 | [diff] [blame] | 1970 | // Build void __kmpc_end_ordered(ident_t *loc, kmp_int32 global_tid); |
Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 1971 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1972 | auto *FnTy = |
Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 1973 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false); |
| 1974 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_end_ordered"); |
| 1975 | break; |
| 1976 | } |
Alexey Bataev | 8b8e202 | 2015-04-27 05:22:09 +0000 | [diff] [blame] | 1977 | case OMPRTL__kmpc_omp_taskwait: { |
| 1978 | // Build kmp_int32 __kmpc_omp_taskwait(ident_t *loc, kmp_int32 global_tid); |
| 1979 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1980 | auto *FnTy = |
Alexey Bataev | 8b8e202 | 2015-04-27 05:22:09 +0000 | [diff] [blame] | 1981 | llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg=*/false); |
| 1982 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_omp_taskwait"); |
| 1983 | break; |
| 1984 | } |
Alexey Bataev | c30dd2d | 2015-06-18 12:14:09 +0000 | [diff] [blame] | 1985 | case OMPRTL__kmpc_taskgroup: { |
| 1986 | // Build void __kmpc_taskgroup(ident_t *loc, kmp_int32 global_tid); |
| 1987 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1988 | auto *FnTy = |
Alexey Bataev | c30dd2d | 2015-06-18 12:14:09 +0000 | [diff] [blame] | 1989 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false); |
| 1990 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_taskgroup"); |
| 1991 | break; |
| 1992 | } |
| 1993 | case OMPRTL__kmpc_end_taskgroup: { |
| 1994 | // Build void __kmpc_end_taskgroup(ident_t *loc, kmp_int32 global_tid); |
| 1995 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1996 | auto *FnTy = |
Alexey Bataev | c30dd2d | 2015-06-18 12:14:09 +0000 | [diff] [blame] | 1997 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false); |
| 1998 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_end_taskgroup"); |
| 1999 | break; |
| 2000 | } |
Alexey Bataev | 7f210c6 | 2015-06-18 13:40:03 +0000 | [diff] [blame] | 2001 | case OMPRTL__kmpc_push_proc_bind: { |
| 2002 | // Build void __kmpc_push_proc_bind(ident_t *loc, kmp_int32 global_tid, |
| 2003 | // int proc_bind) |
| 2004 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty, CGM.IntTy}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2005 | auto *FnTy = |
Alexey Bataev | 7f210c6 | 2015-06-18 13:40:03 +0000 | [diff] [blame] | 2006 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false); |
| 2007 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_push_proc_bind"); |
| 2008 | break; |
| 2009 | } |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 2010 | case OMPRTL__kmpc_omp_task_with_deps: { |
| 2011 | // Build kmp_int32 __kmpc_omp_task_with_deps(ident_t *, kmp_int32 gtid, |
| 2012 | // kmp_task_t *new_task, kmp_int32 ndeps, kmp_depend_info_t *dep_list, |
| 2013 | // kmp_int32 ndeps_noalias, kmp_depend_info_t *noalias_dep_list); |
| 2014 | llvm::Type *TypeParams[] = { |
| 2015 | getIdentTyPointerTy(), CGM.Int32Ty, CGM.VoidPtrTy, CGM.Int32Ty, |
| 2016 | CGM.VoidPtrTy, CGM.Int32Ty, CGM.VoidPtrTy}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2017 | auto *FnTy = |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 2018 | llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg=*/false); |
| 2019 | RTLFn = |
| 2020 | CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_omp_task_with_deps"); |
| 2021 | break; |
| 2022 | } |
| 2023 | case OMPRTL__kmpc_omp_wait_deps: { |
| 2024 | // Build void __kmpc_omp_wait_deps(ident_t *, kmp_int32 gtid, |
| 2025 | // kmp_int32 ndeps, kmp_depend_info_t *dep_list, kmp_int32 ndeps_noalias, |
| 2026 | // kmp_depend_info_t *noalias_dep_list); |
| 2027 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty, |
| 2028 | CGM.Int32Ty, CGM.VoidPtrTy, |
| 2029 | CGM.Int32Ty, CGM.VoidPtrTy}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2030 | auto *FnTy = |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 2031 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false); |
| 2032 | RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_omp_wait_deps"); |
| 2033 | break; |
| 2034 | } |
Alexey Bataev | 0f34da1 | 2015-07-02 04:17:07 +0000 | [diff] [blame] | 2035 | case OMPRTL__kmpc_cancellationpoint: { |
| 2036 | // Build kmp_int32 __kmpc_cancellationpoint(ident_t *loc, kmp_int32 |
| 2037 | // global_tid, kmp_int32 cncl_kind) |
| 2038 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty, CGM.IntTy}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2039 | auto *FnTy = |
Alexey Bataev | 0f34da1 | 2015-07-02 04:17:07 +0000 | [diff] [blame] | 2040 | llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg*/ false); |
| 2041 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_cancellationpoint"); |
| 2042 | break; |
| 2043 | } |
Alexey Bataev | 7d5d33e | 2015-07-06 05:50:32 +0000 | [diff] [blame] | 2044 | case OMPRTL__kmpc_cancel: { |
| 2045 | // Build kmp_int32 __kmpc_cancel(ident_t *loc, kmp_int32 global_tid, |
| 2046 | // kmp_int32 cncl_kind) |
| 2047 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty, CGM.IntTy}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2048 | auto *FnTy = |
Alexey Bataev | 7d5d33e | 2015-07-06 05:50:32 +0000 | [diff] [blame] | 2049 | llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg*/ false); |
| 2050 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_cancel"); |
| 2051 | break; |
| 2052 | } |
Carlo Bertolli | 430d8ec | 2016-03-03 20:34:23 +0000 | [diff] [blame] | 2053 | case OMPRTL__kmpc_push_num_teams: { |
| 2054 | // Build void kmpc_push_num_teams (ident_t loc, kmp_int32 global_tid, |
| 2055 | // kmp_int32 num_teams, kmp_int32 num_threads) |
| 2056 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty, CGM.Int32Ty, |
| 2057 | CGM.Int32Ty}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2058 | auto *FnTy = |
Carlo Bertolli | 430d8ec | 2016-03-03 20:34:23 +0000 | [diff] [blame] | 2059 | llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg*/ false); |
| 2060 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_push_num_teams"); |
| 2061 | break; |
| 2062 | } |
| 2063 | case OMPRTL__kmpc_fork_teams: { |
| 2064 | // Build void __kmpc_fork_teams(ident_t *loc, kmp_int32 argc, kmpc_micro |
| 2065 | // microtask, ...); |
| 2066 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty, |
| 2067 | getKmpc_MicroPointerTy()}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2068 | auto *FnTy = |
Carlo Bertolli | 430d8ec | 2016-03-03 20:34:23 +0000 | [diff] [blame] | 2069 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ true); |
| 2070 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_fork_teams"); |
| 2071 | break; |
| 2072 | } |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 2073 | case OMPRTL__kmpc_taskloop: { |
| 2074 | // Build void __kmpc_taskloop(ident_t *loc, int gtid, kmp_task_t *task, int |
| 2075 | // if_val, kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st, int nogroup, int |
| 2076 | // sched, kmp_uint64 grainsize, void *task_dup); |
| 2077 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), |
| 2078 | CGM.IntTy, |
| 2079 | CGM.VoidPtrTy, |
| 2080 | CGM.IntTy, |
| 2081 | CGM.Int64Ty->getPointerTo(), |
| 2082 | CGM.Int64Ty->getPointerTo(), |
| 2083 | CGM.Int64Ty, |
| 2084 | CGM.IntTy, |
| 2085 | CGM.IntTy, |
| 2086 | CGM.Int64Ty, |
| 2087 | CGM.VoidPtrTy}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2088 | auto *FnTy = |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 2089 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false); |
| 2090 | RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_taskloop"); |
| 2091 | break; |
| 2092 | } |
Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 2093 | case OMPRTL__kmpc_doacross_init: { |
| 2094 | // Build void __kmpc_doacross_init(ident_t *loc, kmp_int32 gtid, kmp_int32 |
| 2095 | // num_dims, struct kmp_dim *dims); |
| 2096 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), |
| 2097 | CGM.Int32Ty, |
| 2098 | CGM.Int32Ty, |
| 2099 | CGM.VoidPtrTy}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2100 | auto *FnTy = |
Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 2101 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false); |
| 2102 | RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_doacross_init"); |
| 2103 | break; |
| 2104 | } |
| 2105 | case OMPRTL__kmpc_doacross_fini: { |
| 2106 | // Build void __kmpc_doacross_fini(ident_t *loc, kmp_int32 gtid); |
| 2107 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2108 | auto *FnTy = |
Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 2109 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false); |
| 2110 | RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_doacross_fini"); |
| 2111 | break; |
| 2112 | } |
| 2113 | case OMPRTL__kmpc_doacross_post: { |
| 2114 | // Build void __kmpc_doacross_post(ident_t *loc, kmp_int32 gtid, kmp_int64 |
| 2115 | // *vec); |
| 2116 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty, |
| 2117 | CGM.Int64Ty->getPointerTo()}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2118 | auto *FnTy = |
Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 2119 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false); |
| 2120 | RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_doacross_post"); |
| 2121 | break; |
| 2122 | } |
| 2123 | case OMPRTL__kmpc_doacross_wait: { |
| 2124 | // Build void __kmpc_doacross_wait(ident_t *loc, kmp_int32 gtid, kmp_int64 |
| 2125 | // *vec); |
| 2126 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty, |
| 2127 | CGM.Int64Ty->getPointerTo()}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2128 | auto *FnTy = |
Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 2129 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false); |
| 2130 | RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_doacross_wait"); |
| 2131 | break; |
| 2132 | } |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 2133 | case OMPRTL__kmpc_task_reduction_init: { |
| 2134 | // Build void *__kmpc_task_reduction_init(int gtid, int num_data, void |
| 2135 | // *data); |
| 2136 | llvm::Type *TypeParams[] = {CGM.IntTy, CGM.IntTy, CGM.VoidPtrTy}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2137 | auto *FnTy = |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 2138 | llvm::FunctionType::get(CGM.VoidPtrTy, TypeParams, /*isVarArg=*/false); |
| 2139 | RTLFn = |
| 2140 | CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_task_reduction_init"); |
| 2141 | break; |
| 2142 | } |
| 2143 | case OMPRTL__kmpc_task_reduction_get_th_data: { |
| 2144 | // Build void *__kmpc_task_reduction_get_th_data(int gtid, void *tg, void |
| 2145 | // *d); |
| 2146 | llvm::Type *TypeParams[] = {CGM.IntTy, CGM.VoidPtrTy, CGM.VoidPtrTy}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2147 | auto *FnTy = |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 2148 | llvm::FunctionType::get(CGM.VoidPtrTy, TypeParams, /*isVarArg=*/false); |
| 2149 | RTLFn = CGM.CreateRuntimeFunction( |
| 2150 | FnTy, /*Name=*/"__kmpc_task_reduction_get_th_data"); |
| 2151 | break; |
| 2152 | } |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 2153 | case OMPRTL__tgt_target: { |
George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 2154 | // Build int32_t __tgt_target(int64_t device_id, void *host_ptr, int32_t |
| 2155 | // 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] | 2156 | // *arg_types); |
George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 2157 | llvm::Type *TypeParams[] = {CGM.Int64Ty, |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 2158 | CGM.VoidPtrTy, |
| 2159 | CGM.Int32Ty, |
| 2160 | CGM.VoidPtrPtrTy, |
| 2161 | CGM.VoidPtrPtrTy, |
| 2162 | CGM.SizeTy->getPointerTo(), |
George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 2163 | CGM.Int64Ty->getPointerTo()}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2164 | auto *FnTy = |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 2165 | llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg*/ false); |
| 2166 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__tgt_target"); |
| 2167 | break; |
| 2168 | } |
Alexey Bataev | a9f77c6 | 2017-12-13 21:04:20 +0000 | [diff] [blame] | 2169 | case OMPRTL__tgt_target_nowait: { |
| 2170 | // Build int32_t __tgt_target_nowait(int64_t device_id, void *host_ptr, |
| 2171 | // int32_t arg_num, void** args_base, void **args, size_t *arg_sizes, |
| 2172 | // int64_t *arg_types); |
| 2173 | llvm::Type *TypeParams[] = {CGM.Int64Ty, |
| 2174 | CGM.VoidPtrTy, |
| 2175 | CGM.Int32Ty, |
| 2176 | CGM.VoidPtrPtrTy, |
| 2177 | CGM.VoidPtrPtrTy, |
| 2178 | CGM.SizeTy->getPointerTo(), |
| 2179 | CGM.Int64Ty->getPointerTo()}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2180 | auto *FnTy = |
Alexey Bataev | a9f77c6 | 2017-12-13 21:04:20 +0000 | [diff] [blame] | 2181 | llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg*/ false); |
| 2182 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__tgt_target_nowait"); |
| 2183 | break; |
| 2184 | } |
Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 2185 | case OMPRTL__tgt_target_teams: { |
George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 2186 | // 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] | 2187 | // 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] | 2188 | // int64_t *arg_types, int32_t num_teams, int32_t thread_limit); |
| 2189 | llvm::Type *TypeParams[] = {CGM.Int64Ty, |
Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 2190 | CGM.VoidPtrTy, |
| 2191 | CGM.Int32Ty, |
| 2192 | CGM.VoidPtrPtrTy, |
| 2193 | CGM.VoidPtrPtrTy, |
| 2194 | CGM.SizeTy->getPointerTo(), |
George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 2195 | CGM.Int64Ty->getPointerTo(), |
Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 2196 | CGM.Int32Ty, |
| 2197 | CGM.Int32Ty}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2198 | auto *FnTy = |
Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 2199 | llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg*/ false); |
| 2200 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__tgt_target_teams"); |
| 2201 | break; |
| 2202 | } |
Alexey Bataev | a9f77c6 | 2017-12-13 21:04:20 +0000 | [diff] [blame] | 2203 | case OMPRTL__tgt_target_teams_nowait: { |
| 2204 | // Build int32_t __tgt_target_teams_nowait(int64_t device_id, void |
| 2205 | // *host_ptr, int32_t arg_num, void** args_base, void **args, size_t |
| 2206 | // *arg_sizes, int64_t *arg_types, int32_t num_teams, int32_t thread_limit); |
| 2207 | llvm::Type *TypeParams[] = {CGM.Int64Ty, |
| 2208 | CGM.VoidPtrTy, |
| 2209 | CGM.Int32Ty, |
| 2210 | CGM.VoidPtrPtrTy, |
| 2211 | CGM.VoidPtrPtrTy, |
| 2212 | CGM.SizeTy->getPointerTo(), |
| 2213 | CGM.Int64Ty->getPointerTo(), |
| 2214 | CGM.Int32Ty, |
| 2215 | CGM.Int32Ty}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2216 | auto *FnTy = |
Alexey Bataev | a9f77c6 | 2017-12-13 21:04:20 +0000 | [diff] [blame] | 2217 | llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg*/ false); |
| 2218 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__tgt_target_teams_nowait"); |
| 2219 | break; |
| 2220 | } |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 2221 | case OMPRTL__tgt_register_lib: { |
| 2222 | // Build void __tgt_register_lib(__tgt_bin_desc *desc); |
| 2223 | QualType ParamTy = |
| 2224 | CGM.getContext().getPointerType(getTgtBinaryDescriptorQTy()); |
| 2225 | llvm::Type *TypeParams[] = {CGM.getTypes().ConvertTypeForMem(ParamTy)}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2226 | auto *FnTy = |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 2227 | llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg*/ false); |
| 2228 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__tgt_register_lib"); |
| 2229 | break; |
| 2230 | } |
| 2231 | case OMPRTL__tgt_unregister_lib: { |
| 2232 | // Build void __tgt_unregister_lib(__tgt_bin_desc *desc); |
| 2233 | QualType ParamTy = |
| 2234 | CGM.getContext().getPointerType(getTgtBinaryDescriptorQTy()); |
| 2235 | llvm::Type *TypeParams[] = {CGM.getTypes().ConvertTypeForMem(ParamTy)}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2236 | auto *FnTy = |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 2237 | llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg*/ false); |
| 2238 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__tgt_unregister_lib"); |
| 2239 | break; |
| 2240 | } |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 2241 | case OMPRTL__tgt_target_data_begin: { |
George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 2242 | // Build void __tgt_target_data_begin(int64_t device_id, int32_t arg_num, |
| 2243 | // void** args_base, void **args, size_t *arg_sizes, int64_t *arg_types); |
| 2244 | llvm::Type *TypeParams[] = {CGM.Int64Ty, |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 2245 | CGM.Int32Ty, |
| 2246 | CGM.VoidPtrPtrTy, |
| 2247 | CGM.VoidPtrPtrTy, |
| 2248 | CGM.SizeTy->getPointerTo(), |
George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 2249 | CGM.Int64Ty->getPointerTo()}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2250 | auto *FnTy = |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 2251 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false); |
| 2252 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__tgt_target_data_begin"); |
| 2253 | break; |
| 2254 | } |
Alexey Bataev | 0cc6b8e | 2017-12-14 17:00:17 +0000 | [diff] [blame] | 2255 | case OMPRTL__tgt_target_data_begin_nowait: { |
| 2256 | // Build void __tgt_target_data_begin_nowait(int64_t device_id, int32_t |
| 2257 | // arg_num, void** args_base, void **args, size_t *arg_sizes, int64_t |
| 2258 | // *arg_types); |
| 2259 | llvm::Type *TypeParams[] = {CGM.Int64Ty, |
| 2260 | CGM.Int32Ty, |
| 2261 | CGM.VoidPtrPtrTy, |
| 2262 | CGM.VoidPtrPtrTy, |
| 2263 | CGM.SizeTy->getPointerTo(), |
| 2264 | CGM.Int64Ty->getPointerTo()}; |
| 2265 | auto *FnTy = |
| 2266 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false); |
| 2267 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__tgt_target_data_begin_nowait"); |
| 2268 | break; |
| 2269 | } |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 2270 | case OMPRTL__tgt_target_data_end: { |
George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 2271 | // Build void __tgt_target_data_end(int64_t device_id, int32_t arg_num, |
| 2272 | // void** args_base, void **args, size_t *arg_sizes, int64_t *arg_types); |
| 2273 | llvm::Type *TypeParams[] = {CGM.Int64Ty, |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 2274 | CGM.Int32Ty, |
| 2275 | CGM.VoidPtrPtrTy, |
| 2276 | CGM.VoidPtrPtrTy, |
| 2277 | CGM.SizeTy->getPointerTo(), |
George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 2278 | CGM.Int64Ty->getPointerTo()}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2279 | auto *FnTy = |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 2280 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false); |
| 2281 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__tgt_target_data_end"); |
| 2282 | break; |
| 2283 | } |
Alexey Bataev | 0cc6b8e | 2017-12-14 17:00:17 +0000 | [diff] [blame] | 2284 | case OMPRTL__tgt_target_data_end_nowait: { |
| 2285 | // Build void __tgt_target_data_end_nowait(int64_t device_id, int32_t |
| 2286 | // arg_num, void** args_base, void **args, size_t *arg_sizes, int64_t |
| 2287 | // *arg_types); |
| 2288 | llvm::Type *TypeParams[] = {CGM.Int64Ty, |
| 2289 | CGM.Int32Ty, |
| 2290 | CGM.VoidPtrPtrTy, |
| 2291 | CGM.VoidPtrPtrTy, |
| 2292 | CGM.SizeTy->getPointerTo(), |
| 2293 | CGM.Int64Ty->getPointerTo()}; |
| 2294 | auto *FnTy = |
| 2295 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false); |
| 2296 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__tgt_target_data_end_nowait"); |
| 2297 | break; |
| 2298 | } |
Samuel Antao | 8d2d730 | 2016-05-26 18:30:22 +0000 | [diff] [blame] | 2299 | case OMPRTL__tgt_target_data_update: { |
George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 2300 | // Build void __tgt_target_data_update(int64_t device_id, int32_t arg_num, |
| 2301 | // void** args_base, void **args, size_t *arg_sizes, int64_t *arg_types); |
| 2302 | llvm::Type *TypeParams[] = {CGM.Int64Ty, |
Samuel Antao | 8d2d730 | 2016-05-26 18:30:22 +0000 | [diff] [blame] | 2303 | CGM.Int32Ty, |
| 2304 | CGM.VoidPtrPtrTy, |
| 2305 | CGM.VoidPtrPtrTy, |
| 2306 | CGM.SizeTy->getPointerTo(), |
George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 2307 | CGM.Int64Ty->getPointerTo()}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2308 | auto *FnTy = |
Samuel Antao | 8d2d730 | 2016-05-26 18:30:22 +0000 | [diff] [blame] | 2309 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false); |
| 2310 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__tgt_target_data_update"); |
| 2311 | break; |
| 2312 | } |
Alexey Bataev | 0cc6b8e | 2017-12-14 17:00:17 +0000 | [diff] [blame] | 2313 | case OMPRTL__tgt_target_data_update_nowait: { |
| 2314 | // Build void __tgt_target_data_update_nowait(int64_t device_id, int32_t |
| 2315 | // arg_num, void** args_base, void **args, size_t *arg_sizes, int64_t |
| 2316 | // *arg_types); |
| 2317 | llvm::Type *TypeParams[] = {CGM.Int64Ty, |
| 2318 | CGM.Int32Ty, |
| 2319 | CGM.VoidPtrPtrTy, |
| 2320 | CGM.VoidPtrPtrTy, |
| 2321 | CGM.SizeTy->getPointerTo(), |
| 2322 | CGM.Int64Ty->getPointerTo()}; |
| 2323 | auto *FnTy = |
| 2324 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false); |
| 2325 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__tgt_target_data_update_nowait"); |
| 2326 | break; |
| 2327 | } |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 2328 | } |
Alexey Bataev | 50b3c95 | 2016-02-19 10:38:26 +0000 | [diff] [blame] | 2329 | assert(RTLFn && "Unable to find OpenMP runtime function"); |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 2330 | return RTLFn; |
| 2331 | } |
Alexey Bataev | 3a3bf0b | 2014-09-22 10:01:53 +0000 | [diff] [blame] | 2332 | |
Alexander Musman | 21212e4 | 2015-03-13 10:38:23 +0000 | [diff] [blame] | 2333 | llvm::Constant *CGOpenMPRuntime::createForStaticInitFunction(unsigned IVSize, |
| 2334 | bool IVSigned) { |
| 2335 | assert((IVSize == 32 || IVSize == 64) && |
| 2336 | "IV size is not compatible with the omp runtime"); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2337 | StringRef Name = IVSize == 32 ? (IVSigned ? "__kmpc_for_static_init_4" |
| 2338 | : "__kmpc_for_static_init_4u") |
| 2339 | : (IVSigned ? "__kmpc_for_static_init_8" |
| 2340 | : "__kmpc_for_static_init_8u"); |
| 2341 | llvm::Type *ITy = IVSize == 32 ? CGM.Int32Ty : CGM.Int64Ty; |
| 2342 | auto *PtrTy = llvm::PointerType::getUnqual(ITy); |
Alexander Musman | 21212e4 | 2015-03-13 10:38:23 +0000 | [diff] [blame] | 2343 | llvm::Type *TypeParams[] = { |
| 2344 | getIdentTyPointerTy(), // loc |
| 2345 | CGM.Int32Ty, // tid |
| 2346 | CGM.Int32Ty, // schedtype |
| 2347 | llvm::PointerType::getUnqual(CGM.Int32Ty), // p_lastiter |
| 2348 | PtrTy, // p_lower |
| 2349 | PtrTy, // p_upper |
| 2350 | PtrTy, // p_stride |
| 2351 | ITy, // incr |
| 2352 | ITy // chunk |
| 2353 | }; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2354 | auto *FnTy = |
Alexander Musman | 21212e4 | 2015-03-13 10:38:23 +0000 | [diff] [blame] | 2355 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false); |
| 2356 | return CGM.CreateRuntimeFunction(FnTy, Name); |
| 2357 | } |
| 2358 | |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 2359 | llvm::Constant *CGOpenMPRuntime::createDispatchInitFunction(unsigned IVSize, |
| 2360 | bool IVSigned) { |
| 2361 | assert((IVSize == 32 || IVSize == 64) && |
| 2362 | "IV size is not compatible with the omp runtime"); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2363 | StringRef Name = |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 2364 | IVSize == 32 |
| 2365 | ? (IVSigned ? "__kmpc_dispatch_init_4" : "__kmpc_dispatch_init_4u") |
| 2366 | : (IVSigned ? "__kmpc_dispatch_init_8" : "__kmpc_dispatch_init_8u"); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2367 | llvm::Type *ITy = IVSize == 32 ? CGM.Int32Ty : CGM.Int64Ty; |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 2368 | llvm::Type *TypeParams[] = { getIdentTyPointerTy(), // loc |
| 2369 | CGM.Int32Ty, // tid |
| 2370 | CGM.Int32Ty, // schedtype |
| 2371 | ITy, // lower |
| 2372 | ITy, // upper |
| 2373 | ITy, // stride |
| 2374 | ITy // chunk |
| 2375 | }; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2376 | auto *FnTy = |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 2377 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false); |
| 2378 | return CGM.CreateRuntimeFunction(FnTy, Name); |
| 2379 | } |
| 2380 | |
Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 2381 | llvm::Constant *CGOpenMPRuntime::createDispatchFiniFunction(unsigned IVSize, |
| 2382 | bool IVSigned) { |
| 2383 | assert((IVSize == 32 || IVSize == 64) && |
| 2384 | "IV size is not compatible with the omp runtime"); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2385 | StringRef Name = |
Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 2386 | IVSize == 32 |
| 2387 | ? (IVSigned ? "__kmpc_dispatch_fini_4" : "__kmpc_dispatch_fini_4u") |
| 2388 | : (IVSigned ? "__kmpc_dispatch_fini_8" : "__kmpc_dispatch_fini_8u"); |
| 2389 | llvm::Type *TypeParams[] = { |
| 2390 | getIdentTyPointerTy(), // loc |
| 2391 | CGM.Int32Ty, // tid |
| 2392 | }; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2393 | auto *FnTy = |
Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 2394 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false); |
| 2395 | return CGM.CreateRuntimeFunction(FnTy, Name); |
| 2396 | } |
| 2397 | |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 2398 | llvm::Constant *CGOpenMPRuntime::createDispatchNextFunction(unsigned IVSize, |
| 2399 | bool IVSigned) { |
| 2400 | assert((IVSize == 32 || IVSize == 64) && |
| 2401 | "IV size is not compatible with the omp runtime"); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2402 | StringRef Name = |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 2403 | IVSize == 32 |
| 2404 | ? (IVSigned ? "__kmpc_dispatch_next_4" : "__kmpc_dispatch_next_4u") |
| 2405 | : (IVSigned ? "__kmpc_dispatch_next_8" : "__kmpc_dispatch_next_8u"); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2406 | llvm::Type *ITy = IVSize == 32 ? CGM.Int32Ty : CGM.Int64Ty; |
| 2407 | auto *PtrTy = llvm::PointerType::getUnqual(ITy); |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 2408 | llvm::Type *TypeParams[] = { |
| 2409 | getIdentTyPointerTy(), // loc |
| 2410 | CGM.Int32Ty, // tid |
| 2411 | llvm::PointerType::getUnqual(CGM.Int32Ty), // p_lastiter |
| 2412 | PtrTy, // p_lower |
| 2413 | PtrTy, // p_upper |
| 2414 | PtrTy // p_stride |
| 2415 | }; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2416 | auto *FnTy = |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 2417 | llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg*/ false); |
| 2418 | return CGM.CreateRuntimeFunction(FnTy, Name); |
| 2419 | } |
| 2420 | |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 2421 | Address CGOpenMPRuntime::getAddrOfDeclareTargetLink(const VarDecl *VD) { |
| 2422 | if (CGM.getLangOpts().OpenMPSimd) |
| 2423 | return Address::invalid(); |
Alexey Bataev | 92327c5 | 2018-03-26 16:40:55 +0000 | [diff] [blame] | 2424 | llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res = |
Alexey Bataev | 97b7221 | 2018-08-14 18:31:20 +0000 | [diff] [blame] | 2425 | OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD); |
Alexey Bataev | 92327c5 | 2018-03-26 16:40:55 +0000 | [diff] [blame] | 2426 | if (Res && *Res == OMPDeclareTargetDeclAttr::MT_Link) { |
| 2427 | SmallString<64> PtrName; |
| 2428 | { |
| 2429 | llvm::raw_svector_ostream OS(PtrName); |
| 2430 | OS << CGM.getMangledName(GlobalDecl(VD)) << "_decl_tgt_link_ptr"; |
| 2431 | } |
| 2432 | llvm::Value *Ptr = CGM.getModule().getNamedValue(PtrName); |
| 2433 | if (!Ptr) { |
| 2434 | QualType PtrTy = CGM.getContext().getPointerType(VD->getType()); |
| 2435 | Ptr = getOrCreateInternalVariable(CGM.getTypes().ConvertTypeForMem(PtrTy), |
| 2436 | PtrName); |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 2437 | if (!CGM.getLangOpts().OpenMPIsDevice) { |
| 2438 | auto *GV = cast<llvm::GlobalVariable>(Ptr); |
| 2439 | GV->setLinkage(llvm::GlobalValue::ExternalLinkage); |
| 2440 | GV->setInitializer(CGM.GetAddrOfGlobal(VD)); |
| 2441 | } |
| 2442 | CGM.addUsedGlobal(cast<llvm::GlobalValue>(Ptr)); |
| 2443 | registerTargetGlobalVariable(VD, cast<llvm::Constant>(Ptr)); |
Alexey Bataev | 92327c5 | 2018-03-26 16:40:55 +0000 | [diff] [blame] | 2444 | } |
| 2445 | return Address(Ptr, CGM.getContext().getDeclAlign(VD)); |
| 2446 | } |
| 2447 | return Address::invalid(); |
| 2448 | } |
| 2449 | |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2450 | llvm::Constant * |
| 2451 | CGOpenMPRuntime::getOrCreateThreadPrivateCache(const VarDecl *VD) { |
Samuel Antao | f8b5012 | 2015-07-13 22:54:53 +0000 | [diff] [blame] | 2452 | assert(!CGM.getLangOpts().OpenMPUseTLS || |
| 2453 | !CGM.getContext().getTargetInfo().isTLSSupported()); |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2454 | // Lookup the entry, lazily creating it if necessary. |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 2455 | std::string Suffix = getName({"cache", ""}); |
| 2456 | return getOrCreateInternalVariable( |
| 2457 | CGM.Int8PtrPtrTy, Twine(CGM.getMangledName(VD)).concat(Suffix)); |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2458 | } |
| 2459 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2460 | Address CGOpenMPRuntime::getAddrOfThreadPrivate(CodeGenFunction &CGF, |
| 2461 | const VarDecl *VD, |
| 2462 | Address VDAddr, |
| 2463 | SourceLocation Loc) { |
Samuel Antao | f8b5012 | 2015-07-13 22:54:53 +0000 | [diff] [blame] | 2464 | if (CGM.getLangOpts().OpenMPUseTLS && |
| 2465 | CGM.getContext().getTargetInfo().isTLSSupported()) |
| 2466 | return VDAddr; |
| 2467 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2468 | llvm::Type *VarTy = VDAddr.getElementType(); |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 2469 | llvm::Value *Args[] = {emitUpdateLocation(CGF, Loc), getThreadID(CGF, Loc), |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2470 | CGF.Builder.CreatePointerCast(VDAddr.getPointer(), |
| 2471 | CGM.Int8PtrTy), |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2472 | CGM.getSize(CGM.GetTargetTypeStoreSize(VarTy)), |
| 2473 | getOrCreateThreadPrivateCache(VD)}; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2474 | return Address(CGF.EmitRuntimeCall( |
| 2475 | createRuntimeFunction(OMPRTL__kmpc_threadprivate_cached), Args), |
| 2476 | VDAddr.getAlignment()); |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2477 | } |
| 2478 | |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 2479 | void CGOpenMPRuntime::emitThreadPrivateVarInit( |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2480 | CodeGenFunction &CGF, Address VDAddr, llvm::Value *Ctor, |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2481 | llvm::Value *CopyCtor, llvm::Value *Dtor, SourceLocation Loc) { |
| 2482 | // Call kmp_int32 __kmpc_global_thread_num(&loc) to init OpenMP runtime |
| 2483 | // library. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2484 | llvm::Value *OMPLoc = emitUpdateLocation(CGF, Loc); |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 2485 | CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_global_thread_num), |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2486 | OMPLoc); |
| 2487 | // Call __kmpc_threadprivate_register(&loc, &var, ctor, cctor/*NULL*/, dtor) |
| 2488 | // to register constructor/destructor for variable. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2489 | llvm::Value *Args[] = { |
| 2490 | OMPLoc, CGF.Builder.CreatePointerCast(VDAddr.getPointer(), CGM.VoidPtrTy), |
| 2491 | Ctor, CopyCtor, Dtor}; |
Alexey Bataev | 1e4b713 | 2014-12-03 12:11:24 +0000 | [diff] [blame] | 2492 | CGF.EmitRuntimeCall( |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 2493 | createRuntimeFunction(OMPRTL__kmpc_threadprivate_register), Args); |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2494 | } |
| 2495 | |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 2496 | llvm::Function *CGOpenMPRuntime::emitThreadPrivateVarDefinition( |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2497 | const VarDecl *VD, Address VDAddr, SourceLocation Loc, |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2498 | bool PerformInit, CodeGenFunction *CGF) { |
Samuel Antao | f8b5012 | 2015-07-13 22:54:53 +0000 | [diff] [blame] | 2499 | if (CGM.getLangOpts().OpenMPUseTLS && |
| 2500 | CGM.getContext().getTargetInfo().isTLSSupported()) |
| 2501 | return nullptr; |
| 2502 | |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2503 | VD = VD->getDefinition(CGM.getContext()); |
| 2504 | if (VD && ThreadPrivateWithDefinition.count(VD) == 0) { |
| 2505 | ThreadPrivateWithDefinition.insert(VD); |
| 2506 | QualType ASTTy = VD->getType(); |
| 2507 | |
| 2508 | llvm::Value *Ctor = nullptr, *CopyCtor = nullptr, *Dtor = nullptr; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2509 | const Expr *Init = VD->getAnyInitializer(); |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2510 | if (CGM.getLangOpts().CPlusPlus && PerformInit) { |
| 2511 | // Generate function that re-emits the declaration's initializer into the |
| 2512 | // threadprivate copy of the variable VD |
| 2513 | CodeGenFunction CtorCGF(CGM); |
| 2514 | FunctionArgList Args; |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 2515 | ImplicitParamDecl Dst(CGM.getContext(), /*DC=*/nullptr, Loc, |
| 2516 | /*Id=*/nullptr, CGM.getContext().VoidPtrTy, |
Alexey Bataev | 5622323 | 2017-06-09 13:40:18 +0000 | [diff] [blame] | 2517 | ImplicitParamDecl::Other); |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2518 | Args.push_back(&Dst); |
| 2519 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2520 | const auto &FI = CGM.getTypes().arrangeBuiltinFunctionDeclaration( |
John McCall | c56a8b3 | 2016-03-11 04:30:31 +0000 | [diff] [blame] | 2521 | CGM.getContext().VoidPtrTy, Args); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2522 | llvm::FunctionType *FTy = CGM.getTypes().GetFunctionType(FI); |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 2523 | std::string Name = getName({"__kmpc_global_ctor_", ""}); |
| 2524 | llvm::Function *Fn = |
| 2525 | CGM.CreateGlobalInitOrDestructFunction(FTy, Name, FI, Loc); |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2526 | CtorCGF.StartFunction(GlobalDecl(), CGM.getContext().VoidPtrTy, Fn, FI, |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 2527 | Args, Loc, Loc); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2528 | llvm::Value *ArgVal = CtorCGF.EmitLoadOfScalar( |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2529 | CtorCGF.GetAddrOfLocalVar(&Dst), /*Volatile=*/false, |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2530 | CGM.getContext().VoidPtrTy, Dst.getLocation()); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2531 | Address Arg = Address(ArgVal, VDAddr.getAlignment()); |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 2532 | Arg = CtorCGF.Builder.CreateElementBitCast( |
| 2533 | Arg, CtorCGF.ConvertTypeForMem(ASTTy)); |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2534 | CtorCGF.EmitAnyExprToMem(Init, Arg, Init->getType().getQualifiers(), |
| 2535 | /*IsInitializer=*/true); |
| 2536 | ArgVal = CtorCGF.EmitLoadOfScalar( |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2537 | CtorCGF.GetAddrOfLocalVar(&Dst), /*Volatile=*/false, |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2538 | CGM.getContext().VoidPtrTy, Dst.getLocation()); |
| 2539 | CtorCGF.Builder.CreateStore(ArgVal, CtorCGF.ReturnValue); |
| 2540 | CtorCGF.FinishFunction(); |
| 2541 | Ctor = Fn; |
| 2542 | } |
| 2543 | if (VD->getType().isDestructedType() != QualType::DK_none) { |
| 2544 | // Generate function that emits destructor call for the threadprivate copy |
| 2545 | // of the variable VD |
| 2546 | CodeGenFunction DtorCGF(CGM); |
| 2547 | FunctionArgList Args; |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 2548 | ImplicitParamDecl Dst(CGM.getContext(), /*DC=*/nullptr, Loc, |
| 2549 | /*Id=*/nullptr, CGM.getContext().VoidPtrTy, |
Alexey Bataev | 5622323 | 2017-06-09 13:40:18 +0000 | [diff] [blame] | 2550 | ImplicitParamDecl::Other); |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2551 | Args.push_back(&Dst); |
| 2552 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2553 | const auto &FI = CGM.getTypes().arrangeBuiltinFunctionDeclaration( |
John McCall | c56a8b3 | 2016-03-11 04:30:31 +0000 | [diff] [blame] | 2554 | CGM.getContext().VoidTy, Args); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2555 | llvm::FunctionType *FTy = CGM.getTypes().GetFunctionType(FI); |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 2556 | std::string Name = getName({"__kmpc_global_dtor_", ""}); |
| 2557 | llvm::Function *Fn = |
| 2558 | CGM.CreateGlobalInitOrDestructFunction(FTy, Name, FI, Loc); |
Adrian Prantl | 1858c66 | 2016-04-24 22:22:29 +0000 | [diff] [blame] | 2559 | auto NL = ApplyDebugLocation::CreateEmpty(DtorCGF); |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2560 | DtorCGF.StartFunction(GlobalDecl(), CGM.getContext().VoidTy, Fn, FI, Args, |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 2561 | Loc, Loc); |
Adrian Prantl | 1858c66 | 2016-04-24 22:22:29 +0000 | [diff] [blame] | 2562 | // Create a scope with an artificial location for the body of this function. |
| 2563 | auto AL = ApplyDebugLocation::CreateArtificial(DtorCGF); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2564 | llvm::Value *ArgVal = DtorCGF.EmitLoadOfScalar( |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2565 | DtorCGF.GetAddrOfLocalVar(&Dst), |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2566 | /*Volatile=*/false, CGM.getContext().VoidPtrTy, Dst.getLocation()); |
| 2567 | DtorCGF.emitDestroy(Address(ArgVal, VDAddr.getAlignment()), ASTTy, |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2568 | DtorCGF.getDestroyer(ASTTy.isDestructedType()), |
| 2569 | DtorCGF.needsEHCleanup(ASTTy.isDestructedType())); |
| 2570 | DtorCGF.FinishFunction(); |
| 2571 | Dtor = Fn; |
| 2572 | } |
| 2573 | // Do not emit init function if it is not required. |
| 2574 | if (!Ctor && !Dtor) |
| 2575 | return nullptr; |
| 2576 | |
| 2577 | llvm::Type *CopyCtorTyArgs[] = {CGM.VoidPtrTy, CGM.VoidPtrTy}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2578 | auto *CopyCtorTy = llvm::FunctionType::get(CGM.VoidPtrTy, CopyCtorTyArgs, |
| 2579 | /*isVarArg=*/false) |
| 2580 | ->getPointerTo(); |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2581 | // Copying constructor for the threadprivate variable. |
| 2582 | // Must be NULL - reserved by runtime, but currently it requires that this |
| 2583 | // parameter is always NULL. Otherwise it fires assertion. |
| 2584 | CopyCtor = llvm::Constant::getNullValue(CopyCtorTy); |
| 2585 | if (Ctor == nullptr) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2586 | auto *CtorTy = llvm::FunctionType::get(CGM.VoidPtrTy, CGM.VoidPtrTy, |
| 2587 | /*isVarArg=*/false) |
| 2588 | ->getPointerTo(); |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2589 | Ctor = llvm::Constant::getNullValue(CtorTy); |
| 2590 | } |
| 2591 | if (Dtor == nullptr) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2592 | auto *DtorTy = llvm::FunctionType::get(CGM.VoidTy, CGM.VoidPtrTy, |
| 2593 | /*isVarArg=*/false) |
| 2594 | ->getPointerTo(); |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2595 | Dtor = llvm::Constant::getNullValue(DtorTy); |
| 2596 | } |
| 2597 | if (!CGF) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2598 | auto *InitFunctionTy = |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2599 | llvm::FunctionType::get(CGM.VoidTy, /*isVarArg*/ false); |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 2600 | std::string Name = getName({"__omp_threadprivate_init_", ""}); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2601 | llvm::Function *InitFunction = CGM.CreateGlobalInitOrDestructFunction( |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 2602 | InitFunctionTy, Name, CGM.getTypes().arrangeNullaryFunction()); |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2603 | CodeGenFunction InitCGF(CGM); |
| 2604 | FunctionArgList ArgList; |
| 2605 | InitCGF.StartFunction(GlobalDecl(), CGM.getContext().VoidTy, InitFunction, |
| 2606 | CGM.getTypes().arrangeNullaryFunction(), ArgList, |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 2607 | Loc, Loc); |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 2608 | emitThreadPrivateVarInit(InitCGF, VDAddr, Ctor, CopyCtor, Dtor, Loc); |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2609 | InitCGF.FinishFunction(); |
| 2610 | return InitFunction; |
| 2611 | } |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 2612 | emitThreadPrivateVarInit(*CGF, VDAddr, Ctor, CopyCtor, Dtor, Loc); |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2613 | } |
| 2614 | return nullptr; |
| 2615 | } |
| 2616 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2617 | /// Obtain information that uniquely identifies a target entry. This |
Alexey Bataev | 34f8a70 | 2018-03-28 14:28:54 +0000 | [diff] [blame] | 2618 | /// consists of the file and device IDs as well as line number associated with |
| 2619 | /// the relevant entry source location. |
| 2620 | static void getTargetEntryUniqueInfo(ASTContext &C, SourceLocation Loc, |
| 2621 | unsigned &DeviceID, unsigned &FileID, |
| 2622 | unsigned &LineNum) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2623 | SourceManager &SM = C.getSourceManager(); |
Alexey Bataev | 34f8a70 | 2018-03-28 14:28:54 +0000 | [diff] [blame] | 2624 | |
| 2625 | // The loc should be always valid and have a file ID (the user cannot use |
| 2626 | // #pragma directives in macros) |
| 2627 | |
| 2628 | assert(Loc.isValid() && "Source location is expected to be always valid."); |
Alexey Bataev | 34f8a70 | 2018-03-28 14:28:54 +0000 | [diff] [blame] | 2629 | |
| 2630 | PresumedLoc PLoc = SM.getPresumedLoc(Loc); |
| 2631 | assert(PLoc.isValid() && "Source location is expected to be always valid."); |
| 2632 | |
| 2633 | llvm::sys::fs::UniqueID ID; |
Alexey Bataev | 64e62dc | 2018-04-30 16:26:57 +0000 | [diff] [blame] | 2634 | if (auto EC = llvm::sys::fs::getUniqueID(PLoc.getFilename(), ID)) |
| 2635 | SM.getDiagnostics().Report(diag::err_cannot_open_file) |
| 2636 | << PLoc.getFilename() << EC.message(); |
Alexey Bataev | 34f8a70 | 2018-03-28 14:28:54 +0000 | [diff] [blame] | 2637 | |
| 2638 | DeviceID = ID.getDevice(); |
| 2639 | FileID = ID.getFile(); |
| 2640 | LineNum = PLoc.getLine(); |
| 2641 | } |
| 2642 | |
| 2643 | bool CGOpenMPRuntime::emitDeclareTargetVarDefinition(const VarDecl *VD, |
| 2644 | llvm::GlobalVariable *Addr, |
| 2645 | bool PerformInit) { |
| 2646 | Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res = |
Alexey Bataev | 97b7221 | 2018-08-14 18:31:20 +0000 | [diff] [blame] | 2647 | OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD); |
Alexey Bataev | 34f8a70 | 2018-03-28 14:28:54 +0000 | [diff] [blame] | 2648 | if (!Res || *Res == OMPDeclareTargetDeclAttr::MT_Link) |
Alexey Bataev | d01b749 | 2018-08-15 19:45:12 +0000 | [diff] [blame] | 2649 | return CGM.getLangOpts().OpenMPIsDevice; |
Alexey Bataev | 34f8a70 | 2018-03-28 14:28:54 +0000 | [diff] [blame] | 2650 | VD = VD->getDefinition(CGM.getContext()); |
| 2651 | if (VD && !DeclareTargetWithDefinition.insert(VD).second) |
| 2652 | return CGM.getLangOpts().OpenMPIsDevice; |
| 2653 | |
| 2654 | QualType ASTTy = VD->getType(); |
| 2655 | |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2656 | SourceLocation Loc = VD->getCanonicalDecl()->getBeginLoc(); |
Alexey Bataev | 34f8a70 | 2018-03-28 14:28:54 +0000 | [diff] [blame] | 2657 | // Produce the unique prefix to identify the new target regions. We use |
| 2658 | // the source location of the variable declaration which we know to not |
| 2659 | // conflict with any target region. |
| 2660 | unsigned DeviceID; |
| 2661 | unsigned FileID; |
| 2662 | unsigned Line; |
| 2663 | getTargetEntryUniqueInfo(CGM.getContext(), Loc, DeviceID, FileID, Line); |
| 2664 | SmallString<128> Buffer, Out; |
| 2665 | { |
| 2666 | llvm::raw_svector_ostream OS(Buffer); |
| 2667 | OS << "__omp_offloading_" << llvm::format("_%x", DeviceID) |
| 2668 | << llvm::format("_%x_", FileID) << VD->getName() << "_l" << Line; |
| 2669 | } |
| 2670 | |
| 2671 | const Expr *Init = VD->getAnyInitializer(); |
| 2672 | if (CGM.getLangOpts().CPlusPlus && PerformInit) { |
| 2673 | llvm::Constant *Ctor; |
| 2674 | llvm::Constant *ID; |
| 2675 | if (CGM.getLangOpts().OpenMPIsDevice) { |
| 2676 | // Generate function that re-emits the declaration's initializer into |
| 2677 | // the threadprivate copy of the variable VD |
| 2678 | CodeGenFunction CtorCGF(CGM); |
| 2679 | |
| 2680 | const CGFunctionInfo &FI = CGM.getTypes().arrangeNullaryFunction(); |
| 2681 | llvm::FunctionType *FTy = CGM.getTypes().GetFunctionType(FI); |
| 2682 | llvm::Function *Fn = CGM.CreateGlobalInitOrDestructFunction( |
| 2683 | FTy, Twine(Buffer, "_ctor"), FI, Loc); |
| 2684 | auto NL = ApplyDebugLocation::CreateEmpty(CtorCGF); |
| 2685 | CtorCGF.StartFunction(GlobalDecl(), CGM.getContext().VoidTy, Fn, FI, |
| 2686 | FunctionArgList(), Loc, Loc); |
| 2687 | auto AL = ApplyDebugLocation::CreateArtificial(CtorCGF); |
| 2688 | CtorCGF.EmitAnyExprToMem(Init, |
| 2689 | Address(Addr, CGM.getContext().getDeclAlign(VD)), |
| 2690 | Init->getType().getQualifiers(), |
| 2691 | /*IsInitializer=*/true); |
| 2692 | CtorCGF.FinishFunction(); |
| 2693 | Ctor = Fn; |
| 2694 | ID = llvm::ConstantExpr::getBitCast(Fn, CGM.Int8PtrTy); |
Alexey Bataev | e253f2f | 2018-05-09 14:15:18 +0000 | [diff] [blame] | 2695 | CGM.addUsedGlobal(cast<llvm::GlobalValue>(Ctor)); |
Alexey Bataev | 34f8a70 | 2018-03-28 14:28:54 +0000 | [diff] [blame] | 2696 | } else { |
| 2697 | Ctor = new llvm::GlobalVariable( |
| 2698 | CGM.getModule(), CGM.Int8Ty, /*isConstant=*/true, |
| 2699 | llvm::GlobalValue::PrivateLinkage, |
| 2700 | llvm::Constant::getNullValue(CGM.Int8Ty), Twine(Buffer, "_ctor")); |
| 2701 | ID = Ctor; |
| 2702 | } |
| 2703 | |
| 2704 | // Register the information for the entry associated with the constructor. |
| 2705 | Out.clear(); |
| 2706 | OffloadEntriesInfoManager.registerTargetRegionEntryInfo( |
| 2707 | DeviceID, FileID, Twine(Buffer, "_ctor").toStringRef(Out), Line, Ctor, |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 2708 | ID, OffloadEntriesInfoManagerTy::OMPTargetRegionEntryCtor); |
Alexey Bataev | 34f8a70 | 2018-03-28 14:28:54 +0000 | [diff] [blame] | 2709 | } |
| 2710 | if (VD->getType().isDestructedType() != QualType::DK_none) { |
| 2711 | llvm::Constant *Dtor; |
| 2712 | llvm::Constant *ID; |
| 2713 | if (CGM.getLangOpts().OpenMPIsDevice) { |
| 2714 | // Generate function that emits destructor call for the threadprivate |
| 2715 | // copy of the variable VD |
| 2716 | CodeGenFunction DtorCGF(CGM); |
| 2717 | |
| 2718 | const CGFunctionInfo &FI = CGM.getTypes().arrangeNullaryFunction(); |
| 2719 | llvm::FunctionType *FTy = CGM.getTypes().GetFunctionType(FI); |
| 2720 | llvm::Function *Fn = CGM.CreateGlobalInitOrDestructFunction( |
| 2721 | FTy, Twine(Buffer, "_dtor"), FI, Loc); |
| 2722 | auto NL = ApplyDebugLocation::CreateEmpty(DtorCGF); |
| 2723 | DtorCGF.StartFunction(GlobalDecl(), CGM.getContext().VoidTy, Fn, FI, |
| 2724 | FunctionArgList(), Loc, Loc); |
| 2725 | // Create a scope with an artificial location for the body of this |
| 2726 | // function. |
| 2727 | auto AL = ApplyDebugLocation::CreateArtificial(DtorCGF); |
| 2728 | DtorCGF.emitDestroy(Address(Addr, CGM.getContext().getDeclAlign(VD)), |
| 2729 | ASTTy, DtorCGF.getDestroyer(ASTTy.isDestructedType()), |
| 2730 | DtorCGF.needsEHCleanup(ASTTy.isDestructedType())); |
| 2731 | DtorCGF.FinishFunction(); |
| 2732 | Dtor = Fn; |
| 2733 | ID = llvm::ConstantExpr::getBitCast(Fn, CGM.Int8PtrTy); |
Alexey Bataev | e253f2f | 2018-05-09 14:15:18 +0000 | [diff] [blame] | 2734 | CGM.addUsedGlobal(cast<llvm::GlobalValue>(Dtor)); |
Alexey Bataev | 34f8a70 | 2018-03-28 14:28:54 +0000 | [diff] [blame] | 2735 | } else { |
| 2736 | Dtor = new llvm::GlobalVariable( |
| 2737 | CGM.getModule(), CGM.Int8Ty, /*isConstant=*/true, |
| 2738 | llvm::GlobalValue::PrivateLinkage, |
| 2739 | llvm::Constant::getNullValue(CGM.Int8Ty), Twine(Buffer, "_dtor")); |
| 2740 | ID = Dtor; |
| 2741 | } |
| 2742 | // Register the information for the entry associated with the destructor. |
| 2743 | Out.clear(); |
| 2744 | OffloadEntriesInfoManager.registerTargetRegionEntryInfo( |
| 2745 | DeviceID, FileID, Twine(Buffer, "_dtor").toStringRef(Out), Line, Dtor, |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 2746 | ID, OffloadEntriesInfoManagerTy::OMPTargetRegionEntryDtor); |
Alexey Bataev | 34f8a70 | 2018-03-28 14:28:54 +0000 | [diff] [blame] | 2747 | } |
| 2748 | return CGM.getLangOpts().OpenMPIsDevice; |
| 2749 | } |
| 2750 | |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 2751 | Address CGOpenMPRuntime::getAddrOfArtificialThreadPrivate(CodeGenFunction &CGF, |
| 2752 | QualType VarType, |
| 2753 | StringRef Name) { |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 2754 | std::string Suffix = getName({"artificial", ""}); |
| 2755 | std::string CacheSuffix = getName({"cache", ""}); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 2756 | llvm::Type *VarLVType = CGF.ConvertTypeForMem(VarType); |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 2757 | llvm::Value *GAddr = |
| 2758 | getOrCreateInternalVariable(VarLVType, Twine(Name).concat(Suffix)); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 2759 | llvm::Value *Args[] = { |
| 2760 | emitUpdateLocation(CGF, SourceLocation()), |
| 2761 | getThreadID(CGF, SourceLocation()), |
| 2762 | CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(GAddr, CGM.VoidPtrTy), |
| 2763 | CGF.Builder.CreateIntCast(CGF.getTypeSize(VarType), CGM.SizeTy, |
| 2764 | /*IsSigned=*/false), |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 2765 | getOrCreateInternalVariable( |
| 2766 | CGM.VoidPtrPtrTy, Twine(Name).concat(Suffix).concat(CacheSuffix))}; |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 2767 | return Address( |
| 2768 | CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
| 2769 | CGF.EmitRuntimeCall( |
| 2770 | createRuntimeFunction(OMPRTL__kmpc_threadprivate_cached), Args), |
| 2771 | VarLVType->getPointerTo(/*AddrSpace=*/0)), |
| 2772 | CGM.getPointerAlign()); |
| 2773 | } |
| 2774 | |
Arpith Chacko Jacob | bb36fe8 | 2017-01-10 15:42:51 +0000 | [diff] [blame] | 2775 | void CGOpenMPRuntime::emitOMPIfClause(CodeGenFunction &CGF, const Expr *Cond, |
| 2776 | const RegionCodeGenTy &ThenGen, |
| 2777 | const RegionCodeGenTy &ElseGen) { |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 2778 | CodeGenFunction::LexicalScope ConditionScope(CGF, Cond->getSourceRange()); |
| 2779 | |
| 2780 | // If the condition constant folds and can be elided, try to avoid emitting |
| 2781 | // the condition and the dead arm of the if/else. |
| 2782 | bool CondConstant; |
| 2783 | if (CGF.ConstantFoldsToSimpleInteger(Cond, CondConstant)) { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2784 | if (CondConstant) |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 2785 | ThenGen(CGF); |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2786 | else |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 2787 | ElseGen(CGF); |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 2788 | return; |
| 2789 | } |
| 2790 | |
| 2791 | // Otherwise, the condition did not fold, or we couldn't elide it. Just |
| 2792 | // emit the conditional branch. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2793 | llvm::BasicBlock *ThenBlock = CGF.createBasicBlock("omp_if.then"); |
| 2794 | llvm::BasicBlock *ElseBlock = CGF.createBasicBlock("omp_if.else"); |
| 2795 | llvm::BasicBlock *ContBlock = CGF.createBasicBlock("omp_if.end"); |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 2796 | CGF.EmitBranchOnBoolExpr(Cond, ThenBlock, ElseBlock, /*TrueCount=*/0); |
| 2797 | |
| 2798 | // Emit the 'then' code. |
| 2799 | CGF.EmitBlock(ThenBlock); |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2800 | ThenGen(CGF); |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 2801 | CGF.EmitBranch(ContBlock); |
| 2802 | // Emit the 'else' code if present. |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2803 | // There is no need to emit line number for unconditional branch. |
| 2804 | (void)ApplyDebugLocation::CreateEmpty(CGF); |
| 2805 | CGF.EmitBlock(ElseBlock); |
| 2806 | ElseGen(CGF); |
| 2807 | // There is no need to emit line number for unconditional branch. |
| 2808 | (void)ApplyDebugLocation::CreateEmpty(CGF); |
| 2809 | CGF.EmitBranch(ContBlock); |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 2810 | // Emit the continuation block for code after the if. |
| 2811 | CGF.EmitBlock(ContBlock, /*IsFinished=*/true); |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 2812 | } |
| 2813 | |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 2814 | void CGOpenMPRuntime::emitParallelCall(CodeGenFunction &CGF, SourceLocation Loc, |
| 2815 | llvm::Value *OutlinedFn, |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 2816 | ArrayRef<llvm::Value *> CapturedVars, |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 2817 | const Expr *IfCond) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 2818 | if (!CGF.HaveInsertPoint()) |
| 2819 | return; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2820 | llvm::Value *RTLoc = emitUpdateLocation(CGF, Loc); |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2821 | auto &&ThenGen = [OutlinedFn, CapturedVars, RTLoc](CodeGenFunction &CGF, |
| 2822 | PrePostActionTy &) { |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 2823 | // Build call __kmpc_fork_call(loc, n, microtask, var1, .., varn); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2824 | CGOpenMPRuntime &RT = CGF.CGM.getOpenMPRuntime(); |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 2825 | llvm::Value *Args[] = { |
| 2826 | RTLoc, |
| 2827 | CGF.Builder.getInt32(CapturedVars.size()), // Number of captured vars |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2828 | CGF.Builder.CreateBitCast(OutlinedFn, RT.getKmpc_MicroPointerTy())}; |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 2829 | llvm::SmallVector<llvm::Value *, 16> RealArgs; |
| 2830 | RealArgs.append(std::begin(Args), std::end(Args)); |
| 2831 | RealArgs.append(CapturedVars.begin(), CapturedVars.end()); |
| 2832 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2833 | llvm::Value *RTLFn = RT.createRuntimeFunction(OMPRTL__kmpc_fork_call); |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 2834 | CGF.EmitRuntimeCall(RTLFn, RealArgs); |
| 2835 | }; |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2836 | auto &&ElseGen = [OutlinedFn, CapturedVars, RTLoc, Loc](CodeGenFunction &CGF, |
| 2837 | PrePostActionTy &) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2838 | CGOpenMPRuntime &RT = CGF.CGM.getOpenMPRuntime(); |
| 2839 | llvm::Value *ThreadID = RT.getThreadID(CGF, Loc); |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 2840 | // Build calls: |
| 2841 | // __kmpc_serialized_parallel(&Loc, GTid); |
| 2842 | llvm::Value *Args[] = {RTLoc, ThreadID}; |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2843 | CGF.EmitRuntimeCall( |
| 2844 | RT.createRuntimeFunction(OMPRTL__kmpc_serialized_parallel), Args); |
Alexey Bataev | d74d060 | 2014-10-13 06:02:40 +0000 | [diff] [blame] | 2845 | |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 2846 | // OutlinedFn(>id, &zero, CapturedStruct); |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 2847 | Address ZeroAddr = CGF.CreateDefaultAlignTempAlloca(CGF.Int32Ty, |
| 2848 | /*Name*/ ".zero.addr"); |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 2849 | CGF.InitTempAlloca(ZeroAddr, CGF.Builder.getInt32(/*C*/ 0)); |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 2850 | llvm::SmallVector<llvm::Value *, 16> OutlinedFnArgs; |
Alexey Bataev | 8521ff6 | 2018-07-25 20:03:01 +0000 | [diff] [blame] | 2851 | // ThreadId for serialized parallels is 0. |
| 2852 | OutlinedFnArgs.push_back(ZeroAddr.getPointer()); |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 2853 | OutlinedFnArgs.push_back(ZeroAddr.getPointer()); |
| 2854 | OutlinedFnArgs.append(CapturedVars.begin(), CapturedVars.end()); |
Alexey Bataev | 3c595a6 | 2017-08-14 15:01:03 +0000 | [diff] [blame] | 2855 | RT.emitOutlinedFunctionCall(CGF, Loc, OutlinedFn, OutlinedFnArgs); |
Alexey Bataev | d74d060 | 2014-10-13 06:02:40 +0000 | [diff] [blame] | 2856 | |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 2857 | // __kmpc_end_serialized_parallel(&Loc, GTid); |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2858 | llvm::Value *EndArgs[] = {RT.emitUpdateLocation(CGF, Loc), ThreadID}; |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 2859 | CGF.EmitRuntimeCall( |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2860 | RT.createRuntimeFunction(OMPRTL__kmpc_end_serialized_parallel), |
| 2861 | EndArgs); |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 2862 | }; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2863 | if (IfCond) { |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 2864 | emitOMPIfClause(CGF, IfCond, ThenGen, ElseGen); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2865 | } else { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2866 | RegionCodeGenTy ThenRCG(ThenGen); |
| 2867 | ThenRCG(CGF); |
Alexey Bataev | f539faa | 2016-03-28 12:58:34 +0000 | [diff] [blame] | 2868 | } |
Alexey Bataev | d74d060 | 2014-10-13 06:02:40 +0000 | [diff] [blame] | 2869 | } |
| 2870 | |
NAKAMURA Takumi | 59c74b22 | 2014-10-27 08:08:18 +0000 | [diff] [blame] | 2871 | // 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] | 2872 | // thread-ID variable (it is passed in a first argument of the outlined function |
| 2873 | // as "kmp_int32 *gtid"). Otherwise, if we're not inside parallel region, but in |
| 2874 | // regular serial code region, get thread ID by calling kmp_int32 |
| 2875 | // kmpc_global_thread_num(ident_t *loc), stash this thread ID in a temporary and |
| 2876 | // return the address of that temp. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2877 | Address CGOpenMPRuntime::emitThreadIDAddress(CodeGenFunction &CGF, |
| 2878 | SourceLocation Loc) { |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 2879 | if (auto *OMPRegionInfo = |
Alexey Bataev | d74d060 | 2014-10-13 06:02:40 +0000 | [diff] [blame] | 2880 | dyn_cast_or_null<CGOpenMPRegionInfo>(CGF.CapturedStmtInfo)) |
Alexey Bataev | 8cbe0a6 | 2015-02-26 10:27:34 +0000 | [diff] [blame] | 2881 | if (OMPRegionInfo->getThreadIDVariable()) |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 2882 | return OMPRegionInfo->getThreadIDVariableLValue(CGF).getAddress(); |
Alexey Bataev | 8cbe0a6 | 2015-02-26 10:27:34 +0000 | [diff] [blame] | 2883 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2884 | llvm::Value *ThreadID = getThreadID(CGF, Loc); |
| 2885 | QualType Int32Ty = |
Alexey Bataev | d74d060 | 2014-10-13 06:02:40 +0000 | [diff] [blame] | 2886 | CGF.getContext().getIntTypeForBitwidth(/*DestWidth*/ 32, /*Signed*/ true); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2887 | Address ThreadIDTemp = CGF.CreateMemTemp(Int32Ty, /*Name*/ ".threadid_temp."); |
Alexey Bataev | d74d060 | 2014-10-13 06:02:40 +0000 | [diff] [blame] | 2888 | CGF.EmitStoreOfScalar(ThreadID, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2889 | CGF.MakeAddrLValue(ThreadIDTemp, Int32Ty)); |
Alexey Bataev | d74d060 | 2014-10-13 06:02:40 +0000 | [diff] [blame] | 2890 | |
| 2891 | return ThreadIDTemp; |
| 2892 | } |
| 2893 | |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2894 | llvm::Constant * |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 2895 | CGOpenMPRuntime::getOrCreateInternalVariable(llvm::Type *Ty, |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2896 | const llvm::Twine &Name) { |
Alexey Bataev | 3a3bf0b | 2014-09-22 10:01:53 +0000 | [diff] [blame] | 2897 | SmallString<256> Buffer; |
| 2898 | llvm::raw_svector_ostream Out(Buffer); |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2899 | Out << Name; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2900 | StringRef RuntimeName = Out.str(); |
Alexey Bataev | 43a919f | 2018-04-13 17:48:43 +0000 | [diff] [blame] | 2901 | auto &Elem = *InternalVars.try_emplace(RuntimeName, nullptr).first; |
David Blaikie | 13156b6 | 2014-11-19 03:06:06 +0000 | [diff] [blame] | 2902 | if (Elem.second) { |
| 2903 | assert(Elem.second->getType()->getPointerElementType() == Ty && |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2904 | "OMP internal variable has different type than requested"); |
David Blaikie | 13156b6 | 2014-11-19 03:06:06 +0000 | [diff] [blame] | 2905 | return &*Elem.second; |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2906 | } |
Alexey Bataev | 3a3bf0b | 2014-09-22 10:01:53 +0000 | [diff] [blame] | 2907 | |
David Blaikie | 13156b6 | 2014-11-19 03:06:06 +0000 | [diff] [blame] | 2908 | return Elem.second = new llvm::GlobalVariable( |
| 2909 | CGM.getModule(), Ty, /*IsConstant*/ false, |
| 2910 | llvm::GlobalValue::CommonLinkage, llvm::Constant::getNullValue(Ty), |
| 2911 | Elem.first()); |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2912 | } |
| 2913 | |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 2914 | llvm::Value *CGOpenMPRuntime::getCriticalRegionLock(StringRef CriticalName) { |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 2915 | std::string Prefix = Twine("gomp_critical_user_", CriticalName).str(); |
| 2916 | std::string Name = getName({Prefix, "var"}); |
| 2917 | return getOrCreateInternalVariable(KmpCriticalNameTy, Name); |
Alexey Bataev | 3a3bf0b | 2014-09-22 10:01:53 +0000 | [diff] [blame] | 2918 | } |
| 2919 | |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 2920 | namespace { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2921 | /// Common pre(post)-action for different OpenMP constructs. |
| 2922 | class CommonActionTy final : public PrePostActionTy { |
| 2923 | llvm::Value *EnterCallee; |
| 2924 | ArrayRef<llvm::Value *> EnterArgs; |
| 2925 | llvm::Value *ExitCallee; |
| 2926 | ArrayRef<llvm::Value *> ExitArgs; |
| 2927 | bool Conditional; |
| 2928 | llvm::BasicBlock *ContBlock = nullptr; |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 2929 | |
| 2930 | public: |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2931 | CommonActionTy(llvm::Value *EnterCallee, ArrayRef<llvm::Value *> EnterArgs, |
| 2932 | llvm::Value *ExitCallee, ArrayRef<llvm::Value *> ExitArgs, |
| 2933 | bool Conditional = false) |
| 2934 | : EnterCallee(EnterCallee), EnterArgs(EnterArgs), ExitCallee(ExitCallee), |
| 2935 | ExitArgs(ExitArgs), Conditional(Conditional) {} |
| 2936 | void Enter(CodeGenFunction &CGF) override { |
| 2937 | llvm::Value *EnterRes = CGF.EmitRuntimeCall(EnterCallee, EnterArgs); |
| 2938 | if (Conditional) { |
| 2939 | llvm::Value *CallBool = CGF.Builder.CreateIsNotNull(EnterRes); |
| 2940 | auto *ThenBlock = CGF.createBasicBlock("omp_if.then"); |
| 2941 | ContBlock = CGF.createBasicBlock("omp_if.end"); |
| 2942 | // Generate the branch (If-stmt) |
| 2943 | CGF.Builder.CreateCondBr(CallBool, ThenBlock, ContBlock); |
| 2944 | CGF.EmitBlock(ThenBlock); |
| 2945 | } |
Alexey Bataev | a744ff5 | 2015-05-05 09:24:37 +0000 | [diff] [blame] | 2946 | } |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2947 | void Done(CodeGenFunction &CGF) { |
| 2948 | // Emit the rest of blocks/branches |
| 2949 | CGF.EmitBranch(ContBlock); |
| 2950 | CGF.EmitBlock(ContBlock, true); |
| 2951 | } |
| 2952 | void Exit(CodeGenFunction &CGF) override { |
| 2953 | CGF.EmitRuntimeCall(ExitCallee, ExitArgs); |
Alexey Bataev | 3e6124b | 2015-04-10 07:48:12 +0000 | [diff] [blame] | 2954 | } |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 2955 | }; |
Hans Wennborg | 7eb5464 | 2015-09-10 17:07:54 +0000 | [diff] [blame] | 2956 | } // anonymous namespace |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 2957 | |
| 2958 | void CGOpenMPRuntime::emitCriticalRegion(CodeGenFunction &CGF, |
| 2959 | StringRef CriticalName, |
| 2960 | const RegionCodeGenTy &CriticalOpGen, |
Alexey Bataev | fc57d16 | 2015-12-15 10:55:09 +0000 | [diff] [blame] | 2961 | SourceLocation Loc, const Expr *Hint) { |
| 2962 | // __kmpc_critical[_with_hint](ident_t *, gtid, Lock[, hint]); |
Alexey Bataev | 75ddfab | 2014-12-01 11:32:38 +0000 | [diff] [blame] | 2963 | // CriticalOpGen(); |
| 2964 | // __kmpc_end_critical(ident_t *, gtid, Lock); |
| 2965 | // Prepare arguments and build a call to __kmpc_critical |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 2966 | if (!CGF.HaveInsertPoint()) |
| 2967 | return; |
Alexey Bataev | fc57d16 | 2015-12-15 10:55:09 +0000 | [diff] [blame] | 2968 | llvm::Value *Args[] = {emitUpdateLocation(CGF, Loc), getThreadID(CGF, Loc), |
| 2969 | getCriticalRegionLock(CriticalName)}; |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2970 | llvm::SmallVector<llvm::Value *, 4> EnterArgs(std::begin(Args), |
| 2971 | std::end(Args)); |
Alexey Bataev | fc57d16 | 2015-12-15 10:55:09 +0000 | [diff] [blame] | 2972 | if (Hint) { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2973 | EnterArgs.push_back(CGF.Builder.CreateIntCast( |
| 2974 | CGF.EmitScalarExpr(Hint), CGM.IntPtrTy, /*isSigned=*/false)); |
| 2975 | } |
| 2976 | CommonActionTy Action( |
| 2977 | createRuntimeFunction(Hint ? OMPRTL__kmpc_critical_with_hint |
| 2978 | : OMPRTL__kmpc_critical), |
| 2979 | EnterArgs, createRuntimeFunction(OMPRTL__kmpc_end_critical), Args); |
| 2980 | CriticalOpGen.setAction(Action); |
Alexey Bataev | fc57d16 | 2015-12-15 10:55:09 +0000 | [diff] [blame] | 2981 | emitInlinedDirective(CGF, OMPD_critical, CriticalOpGen); |
Alexey Bataev | 3a3bf0b | 2014-09-22 10:01:53 +0000 | [diff] [blame] | 2982 | } |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 2983 | |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 2984 | void CGOpenMPRuntime::emitMasterRegion(CodeGenFunction &CGF, |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 2985 | const RegionCodeGenTy &MasterOpGen, |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 2986 | SourceLocation Loc) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 2987 | if (!CGF.HaveInsertPoint()) |
| 2988 | return; |
Alexey Bataev | 8d69065 | 2014-12-04 07:23:53 +0000 | [diff] [blame] | 2989 | // if(__kmpc_master(ident_t *, gtid)) { |
| 2990 | // MasterOpGen(); |
| 2991 | // __kmpc_end_master(ident_t *, gtid); |
| 2992 | // } |
| 2993 | // Prepare arguments and build a call to __kmpc_master |
Alexey Bataev | d7614fb | 2015-04-10 06:33:45 +0000 | [diff] [blame] | 2994 | llvm::Value *Args[] = {emitUpdateLocation(CGF, Loc), getThreadID(CGF, Loc)}; |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2995 | CommonActionTy Action(createRuntimeFunction(OMPRTL__kmpc_master), Args, |
| 2996 | createRuntimeFunction(OMPRTL__kmpc_end_master), Args, |
| 2997 | /*Conditional=*/true); |
| 2998 | MasterOpGen.setAction(Action); |
| 2999 | emitInlinedDirective(CGF, OMPD_master, MasterOpGen); |
| 3000 | Action.Done(CGF); |
Alexey Bataev | 8d69065 | 2014-12-04 07:23:53 +0000 | [diff] [blame] | 3001 | } |
| 3002 | |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 3003 | void CGOpenMPRuntime::emitTaskyieldCall(CodeGenFunction &CGF, |
| 3004 | SourceLocation Loc) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 3005 | if (!CGF.HaveInsertPoint()) |
| 3006 | return; |
Alexey Bataev | 9f797f3 | 2015-02-05 05:57:51 +0000 | [diff] [blame] | 3007 | // Build call __kmpc_omp_taskyield(loc, thread_id, 0); |
| 3008 | llvm::Value *Args[] = { |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 3009 | emitUpdateLocation(CGF, Loc), getThreadID(CGF, Loc), |
Alexey Bataev | 9f797f3 | 2015-02-05 05:57:51 +0000 | [diff] [blame] | 3010 | llvm::ConstantInt::get(CGM.IntTy, /*V=*/0, /*isSigned=*/true)}; |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 3011 | CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_omp_taskyield), Args); |
Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 3012 | if (auto *Region = dyn_cast_or_null<CGOpenMPRegionInfo>(CGF.CapturedStmtInfo)) |
| 3013 | Region->emitUntiedSwitch(CGF); |
Alexey Bataev | 9f797f3 | 2015-02-05 05:57:51 +0000 | [diff] [blame] | 3014 | } |
| 3015 | |
Alexey Bataev | c30dd2d | 2015-06-18 12:14:09 +0000 | [diff] [blame] | 3016 | void CGOpenMPRuntime::emitTaskgroupRegion(CodeGenFunction &CGF, |
| 3017 | const RegionCodeGenTy &TaskgroupOpGen, |
| 3018 | SourceLocation Loc) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 3019 | if (!CGF.HaveInsertPoint()) |
| 3020 | return; |
Alexey Bataev | c30dd2d | 2015-06-18 12:14:09 +0000 | [diff] [blame] | 3021 | // __kmpc_taskgroup(ident_t *, gtid); |
| 3022 | // TaskgroupOpGen(); |
| 3023 | // __kmpc_end_taskgroup(ident_t *, gtid); |
| 3024 | // Prepare arguments and build a call to __kmpc_taskgroup |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 3025 | llvm::Value *Args[] = {emitUpdateLocation(CGF, Loc), getThreadID(CGF, Loc)}; |
| 3026 | CommonActionTy Action(createRuntimeFunction(OMPRTL__kmpc_taskgroup), Args, |
| 3027 | createRuntimeFunction(OMPRTL__kmpc_end_taskgroup), |
| 3028 | Args); |
| 3029 | TaskgroupOpGen.setAction(Action); |
| 3030 | emitInlinedDirective(CGF, OMPD_taskgroup, TaskgroupOpGen); |
Alexey Bataev | c30dd2d | 2015-06-18 12:14:09 +0000 | [diff] [blame] | 3031 | } |
| 3032 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3033 | /// Given an array of pointers to variables, project the address of a |
| 3034 | /// given variable. |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 3035 | static Address emitAddrOfVarFromArray(CodeGenFunction &CGF, Address Array, |
| 3036 | unsigned Index, const VarDecl *Var) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3037 | // Pull out the pointer to the variable. |
| 3038 | Address PtrAddr = |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 3039 | CGF.Builder.CreateConstArrayGEP(Array, Index, CGF.getPointerSize()); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3040 | llvm::Value *Ptr = CGF.Builder.CreateLoad(PtrAddr); |
| 3041 | |
| 3042 | Address Addr = Address(Ptr, CGF.getContext().getDeclAlign(Var)); |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 3043 | Addr = CGF.Builder.CreateElementBitCast( |
| 3044 | Addr, CGF.ConvertTypeForMem(Var->getType())); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3045 | return Addr; |
| 3046 | } |
| 3047 | |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3048 | static llvm::Value *emitCopyprivateCopyFunction( |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 3049 | CodeGenModule &CGM, llvm::Type *ArgsType, |
| 3050 | ArrayRef<const Expr *> CopyprivateVars, ArrayRef<const Expr *> DestExprs, |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 3051 | ArrayRef<const Expr *> SrcExprs, ArrayRef<const Expr *> AssignmentOps, |
| 3052 | SourceLocation Loc) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3053 | ASTContext &C = CGM.getContext(); |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3054 | // void copy_func(void *LHSArg, void *RHSArg); |
| 3055 | FunctionArgList Args; |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 3056 | ImplicitParamDecl LHSArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, C.VoidPtrTy, |
| 3057 | ImplicitParamDecl::Other); |
| 3058 | ImplicitParamDecl RHSArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, C.VoidPtrTy, |
| 3059 | ImplicitParamDecl::Other); |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3060 | Args.push_back(&LHSArg); |
| 3061 | Args.push_back(&RHSArg); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3062 | const auto &CGFI = |
| 3063 | CGM.getTypes().arrangeBuiltinFunctionDeclaration(C.VoidTy, Args); |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 3064 | std::string Name = |
| 3065 | CGM.getOpenMPRuntime().getName({"omp", "copyprivate", "copy_func"}); |
| 3066 | auto *Fn = llvm::Function::Create(CGM.getTypes().GetFunctionType(CGFI), |
| 3067 | llvm::GlobalValue::InternalLinkage, Name, |
| 3068 | &CGM.getModule()); |
Rafael Espindola | 51ec5a9 | 2018-02-28 23:46:35 +0000 | [diff] [blame] | 3069 | CGM.SetInternalFunctionAttributes(GlobalDecl(), Fn, CGFI); |
Alexey Bataev | c0f879b | 2018-04-10 20:10:53 +0000 | [diff] [blame] | 3070 | Fn->setDoesNotRecurse(); |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3071 | CodeGenFunction CGF(CGM); |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 3072 | CGF.StartFunction(GlobalDecl(), C.VoidTy, Fn, CGFI, Args, Loc, Loc); |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 3073 | // Dest = (void*[n])(LHSArg); |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3074 | // Src = (void*[n])(RHSArg); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3075 | Address LHS(CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
| 3076 | CGF.Builder.CreateLoad(CGF.GetAddrOfLocalVar(&LHSArg)), |
| 3077 | ArgsType), CGF.getPointerAlign()); |
| 3078 | Address RHS(CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
| 3079 | CGF.Builder.CreateLoad(CGF.GetAddrOfLocalVar(&RHSArg)), |
| 3080 | ArgsType), CGF.getPointerAlign()); |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3081 | // *(Type0*)Dst[0] = *(Type0*)Src[0]; |
| 3082 | // *(Type1*)Dst[1] = *(Type1*)Src[1]; |
| 3083 | // ... |
| 3084 | // *(Typen*)Dst[n] = *(Typen*)Src[n]; |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3085 | for (unsigned I = 0, E = AssignmentOps.size(); I < E; ++I) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3086 | const auto *DestVar = |
| 3087 | cast<VarDecl>(cast<DeclRefExpr>(DestExprs[I])->getDecl()); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3088 | Address DestAddr = emitAddrOfVarFromArray(CGF, LHS, I, DestVar); |
| 3089 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3090 | const auto *SrcVar = |
| 3091 | cast<VarDecl>(cast<DeclRefExpr>(SrcExprs[I])->getDecl()); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3092 | Address SrcAddr = emitAddrOfVarFromArray(CGF, RHS, I, SrcVar); |
| 3093 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3094 | const auto *VD = cast<DeclRefExpr>(CopyprivateVars[I])->getDecl(); |
Alexey Bataev | 1d9c15c | 2015-05-19 12:31:28 +0000 | [diff] [blame] | 3095 | QualType Type = VD->getType(); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3096 | CGF.EmitOMPCopy(Type, DestAddr, SrcAddr, DestVar, SrcVar, AssignmentOps[I]); |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3097 | } |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3098 | CGF.FinishFunction(); |
| 3099 | return Fn; |
| 3100 | } |
| 3101 | |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 3102 | void CGOpenMPRuntime::emitSingleRegion(CodeGenFunction &CGF, |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 3103 | const RegionCodeGenTy &SingleOpGen, |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3104 | SourceLocation Loc, |
| 3105 | ArrayRef<const Expr *> CopyprivateVars, |
| 3106 | ArrayRef<const Expr *> SrcExprs, |
| 3107 | ArrayRef<const Expr *> DstExprs, |
| 3108 | ArrayRef<const Expr *> AssignmentOps) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 3109 | if (!CGF.HaveInsertPoint()) |
| 3110 | return; |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3111 | assert(CopyprivateVars.size() == SrcExprs.size() && |
| 3112 | CopyprivateVars.size() == DstExprs.size() && |
| 3113 | CopyprivateVars.size() == AssignmentOps.size()); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3114 | ASTContext &C = CGM.getContext(); |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3115 | // int32 did_it = 0; |
Alexey Bataev | 6956e2e | 2015-02-05 06:35:41 +0000 | [diff] [blame] | 3116 | // if(__kmpc_single(ident_t *, gtid)) { |
| 3117 | // SingleOpGen(); |
| 3118 | // __kmpc_end_single(ident_t *, gtid); |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3119 | // did_it = 1; |
Alexey Bataev | 6956e2e | 2015-02-05 06:35:41 +0000 | [diff] [blame] | 3120 | // } |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3121 | // call __kmpc_copyprivate(ident_t *, gtid, <buf_size>, <copyprivate list>, |
| 3122 | // <copy_func>, did_it); |
| 3123 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3124 | Address DidIt = Address::invalid(); |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3125 | if (!CopyprivateVars.empty()) { |
| 3126 | // int32 did_it = 0; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3127 | QualType KmpInt32Ty = |
| 3128 | C.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1); |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3129 | DidIt = CGF.CreateMemTemp(KmpInt32Ty, ".omp.copyprivate.did_it"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3130 | CGF.Builder.CreateStore(CGF.Builder.getInt32(0), DidIt); |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3131 | } |
Alexey Bataev | 6956e2e | 2015-02-05 06:35:41 +0000 | [diff] [blame] | 3132 | // Prepare arguments and build a call to __kmpc_single |
Alexey Bataev | d7614fb | 2015-04-10 06:33:45 +0000 | [diff] [blame] | 3133 | llvm::Value *Args[] = {emitUpdateLocation(CGF, Loc), getThreadID(CGF, Loc)}; |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 3134 | CommonActionTy Action(createRuntimeFunction(OMPRTL__kmpc_single), Args, |
| 3135 | createRuntimeFunction(OMPRTL__kmpc_end_single), Args, |
| 3136 | /*Conditional=*/true); |
| 3137 | SingleOpGen.setAction(Action); |
| 3138 | emitInlinedDirective(CGF, OMPD_single, SingleOpGen); |
| 3139 | if (DidIt.isValid()) { |
| 3140 | // did_it = 1; |
| 3141 | CGF.Builder.CreateStore(CGF.Builder.getInt32(1), DidIt); |
| 3142 | } |
| 3143 | Action.Done(CGF); |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3144 | // call __kmpc_copyprivate(ident_t *, gtid, <buf_size>, <copyprivate list>, |
| 3145 | // <copy_func>, did_it); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3146 | if (DidIt.isValid()) { |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3147 | llvm::APInt ArraySize(/*unsigned int numBits=*/32, CopyprivateVars.size()); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3148 | QualType CopyprivateArrayTy = |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3149 | C.getConstantArrayType(C.VoidPtrTy, ArraySize, ArrayType::Normal, |
| 3150 | /*IndexTypeQuals=*/0); |
| 3151 | // Create a list of all private variables for copyprivate. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3152 | Address CopyprivateList = |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3153 | CGF.CreateMemTemp(CopyprivateArrayTy, ".omp.copyprivate.cpr_list"); |
| 3154 | for (unsigned I = 0, E = CopyprivateVars.size(); I < E; ++I) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3155 | Address Elem = CGF.Builder.CreateConstArrayGEP( |
| 3156 | CopyprivateList, I, CGF.getPointerSize()); |
| 3157 | CGF.Builder.CreateStore( |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3158 | CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3159 | CGF.EmitLValue(CopyprivateVars[I]).getPointer(), CGF.VoidPtrTy), |
| 3160 | Elem); |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3161 | } |
| 3162 | // Build function that copies private values from single region to all other |
| 3163 | // threads in the corresponding parallel region. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3164 | llvm::Value *CpyFn = emitCopyprivateCopyFunction( |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3165 | CGM, CGF.ConvertTypeForMem(CopyprivateArrayTy)->getPointerTo(), |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 3166 | CopyprivateVars, SrcExprs, DstExprs, AssignmentOps, Loc); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3167 | llvm::Value *BufSize = CGF.getTypeSize(CopyprivateArrayTy); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3168 | Address CL = |
| 3169 | CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(CopyprivateList, |
| 3170 | CGF.VoidPtrTy); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3171 | llvm::Value *DidItVal = CGF.Builder.CreateLoad(DidIt); |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3172 | llvm::Value *Args[] = { |
| 3173 | emitUpdateLocation(CGF, Loc), // ident_t *<loc> |
| 3174 | getThreadID(CGF, Loc), // i32 <gtid> |
Alexey Bataev | 66beaa9 | 2015-04-30 03:47:32 +0000 | [diff] [blame] | 3175 | BufSize, // size_t <buf_size> |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3176 | CL.getPointer(), // void *<copyprivate list> |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3177 | CpyFn, // void (*) (void *, void *) <copy_func> |
| 3178 | DidItVal // i32 did_it |
| 3179 | }; |
| 3180 | CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_copyprivate), Args); |
| 3181 | } |
Alexey Bataev | 6956e2e | 2015-02-05 06:35:41 +0000 | [diff] [blame] | 3182 | } |
| 3183 | |
Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 3184 | void CGOpenMPRuntime::emitOrderedRegion(CodeGenFunction &CGF, |
| 3185 | const RegionCodeGenTy &OrderedOpGen, |
Alexey Bataev | 5f600d6 | 2015-09-29 03:48:57 +0000 | [diff] [blame] | 3186 | SourceLocation Loc, bool IsThreads) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 3187 | if (!CGF.HaveInsertPoint()) |
| 3188 | return; |
Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 3189 | // __kmpc_ordered(ident_t *, gtid); |
| 3190 | // OrderedOpGen(); |
| 3191 | // __kmpc_end_ordered(ident_t *, gtid); |
| 3192 | // Prepare arguments and build a call to __kmpc_ordered |
Alexey Bataev | 5f600d6 | 2015-09-29 03:48:57 +0000 | [diff] [blame] | 3193 | if (IsThreads) { |
Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 3194 | llvm::Value *Args[] = {emitUpdateLocation(CGF, Loc), getThreadID(CGF, Loc)}; |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 3195 | CommonActionTy Action(createRuntimeFunction(OMPRTL__kmpc_ordered), Args, |
| 3196 | createRuntimeFunction(OMPRTL__kmpc_end_ordered), |
| 3197 | Args); |
| 3198 | OrderedOpGen.setAction(Action); |
| 3199 | emitInlinedDirective(CGF, OMPD_ordered, OrderedOpGen); |
| 3200 | return; |
Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 3201 | } |
Alexey Bataev | 5f600d6 | 2015-09-29 03:48:57 +0000 | [diff] [blame] | 3202 | emitInlinedDirective(CGF, OMPD_ordered, OrderedOpGen); |
Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 3203 | } |
| 3204 | |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 3205 | void CGOpenMPRuntime::emitBarrierCall(CodeGenFunction &CGF, SourceLocation Loc, |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 3206 | OpenMPDirectiveKind Kind, bool EmitChecks, |
| 3207 | bool ForceSimpleCall) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 3208 | if (!CGF.HaveInsertPoint()) |
| 3209 | return; |
Alexey Bataev | 8f7c1b0 | 2014-12-05 04:09:23 +0000 | [diff] [blame] | 3210 | // Build call __kmpc_cancel_barrier(loc, thread_id); |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 3211 | // Build call __kmpc_barrier(loc, thread_id); |
Alexey Bataev | 50b3c95 | 2016-02-19 10:38:26 +0000 | [diff] [blame] | 3212 | unsigned Flags; |
| 3213 | if (Kind == OMPD_for) |
| 3214 | Flags = OMP_IDENT_BARRIER_IMPL_FOR; |
| 3215 | else if (Kind == OMPD_sections) |
| 3216 | Flags = OMP_IDENT_BARRIER_IMPL_SECTIONS; |
| 3217 | else if (Kind == OMPD_single) |
| 3218 | Flags = OMP_IDENT_BARRIER_IMPL_SINGLE; |
| 3219 | else if (Kind == OMPD_barrier) |
| 3220 | Flags = OMP_IDENT_BARRIER_EXPL; |
| 3221 | else |
| 3222 | Flags = OMP_IDENT_BARRIER_IMPL; |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 3223 | // Build call __kmpc_cancel_barrier(loc, thread_id) or __kmpc_barrier(loc, |
| 3224 | // thread_id); |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 3225 | llvm::Value *Args[] = {emitUpdateLocation(CGF, Loc, Flags), |
| 3226 | getThreadID(CGF, Loc)}; |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 3227 | if (auto *OMPRegionInfo = |
| 3228 | dyn_cast_or_null<CGOpenMPRegionInfo>(CGF.CapturedStmtInfo)) { |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 3229 | if (!ForceSimpleCall && OMPRegionInfo->hasCancel()) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3230 | llvm::Value *Result = CGF.EmitRuntimeCall( |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 3231 | createRuntimeFunction(OMPRTL__kmpc_cancel_barrier), Args); |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 3232 | if (EmitChecks) { |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 3233 | // if (__kmpc_cancel_barrier()) { |
| 3234 | // exit from construct; |
| 3235 | // } |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3236 | llvm::BasicBlock *ExitBB = CGF.createBasicBlock(".cancel.exit"); |
| 3237 | llvm::BasicBlock *ContBB = CGF.createBasicBlock(".cancel.continue"); |
| 3238 | llvm::Value *Cmp = CGF.Builder.CreateIsNotNull(Result); |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 3239 | CGF.Builder.CreateCondBr(Cmp, ExitBB, ContBB); |
| 3240 | CGF.EmitBlock(ExitBB); |
| 3241 | // exit from construct; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3242 | CodeGenFunction::JumpDest CancelDestination = |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 3243 | CGF.getOMPCancelDestination(OMPRegionInfo->getDirectiveKind()); |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 3244 | CGF.EmitBranchThroughCleanup(CancelDestination); |
| 3245 | CGF.EmitBlock(ContBB, /*IsFinished=*/true); |
| 3246 | } |
| 3247 | return; |
| 3248 | } |
| 3249 | } |
| 3250 | CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_barrier), Args); |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 3251 | } |
| 3252 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 3253 | /// Map the OpenMP loop schedule to the runtime enumeration. |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 3254 | static OpenMPSchedType getRuntimeSchedule(OpenMPScheduleClauseKind ScheduleKind, |
Alexey Bataev | d7589ffe | 2015-05-20 13:12:48 +0000 | [diff] [blame] | 3255 | bool Chunked, bool Ordered) { |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 3256 | switch (ScheduleKind) { |
| 3257 | case OMPC_SCHEDULE_static: |
Alexey Bataev | d7589ffe | 2015-05-20 13:12:48 +0000 | [diff] [blame] | 3258 | return Chunked ? (Ordered ? OMP_ord_static_chunked : OMP_sch_static_chunked) |
| 3259 | : (Ordered ? OMP_ord_static : OMP_sch_static); |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 3260 | case OMPC_SCHEDULE_dynamic: |
Alexey Bataev | d7589ffe | 2015-05-20 13:12:48 +0000 | [diff] [blame] | 3261 | return Ordered ? OMP_ord_dynamic_chunked : OMP_sch_dynamic_chunked; |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 3262 | case OMPC_SCHEDULE_guided: |
Alexey Bataev | d7589ffe | 2015-05-20 13:12:48 +0000 | [diff] [blame] | 3263 | return Ordered ? OMP_ord_guided_chunked : OMP_sch_guided_chunked; |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 3264 | case OMPC_SCHEDULE_runtime: |
Alexey Bataev | d7589ffe | 2015-05-20 13:12:48 +0000 | [diff] [blame] | 3265 | return Ordered ? OMP_ord_runtime : OMP_sch_runtime; |
| 3266 | case OMPC_SCHEDULE_auto: |
| 3267 | return Ordered ? OMP_ord_auto : OMP_sch_auto; |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 3268 | case OMPC_SCHEDULE_unknown: |
| 3269 | assert(!Chunked && "chunk was specified but schedule kind not known"); |
Alexey Bataev | d7589ffe | 2015-05-20 13:12:48 +0000 | [diff] [blame] | 3270 | return Ordered ? OMP_ord_static : OMP_sch_static; |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 3271 | } |
| 3272 | llvm_unreachable("Unexpected runtime schedule"); |
| 3273 | } |
| 3274 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 3275 | /// Map the OpenMP distribute schedule to the runtime enumeration. |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3276 | static OpenMPSchedType |
| 3277 | getRuntimeSchedule(OpenMPDistScheduleClauseKind ScheduleKind, bool Chunked) { |
| 3278 | // only static is allowed for dist_schedule |
| 3279 | return Chunked ? OMP_dist_sch_static_chunked : OMP_dist_sch_static; |
| 3280 | } |
| 3281 | |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 3282 | bool CGOpenMPRuntime::isStaticNonchunked(OpenMPScheduleClauseKind ScheduleKind, |
| 3283 | bool Chunked) const { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3284 | OpenMPSchedType Schedule = |
| 3285 | getRuntimeSchedule(ScheduleKind, Chunked, /*Ordered=*/false); |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 3286 | return Schedule == OMP_sch_static; |
| 3287 | } |
| 3288 | |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3289 | bool CGOpenMPRuntime::isStaticNonchunked( |
| 3290 | OpenMPDistScheduleClauseKind ScheduleKind, bool Chunked) const { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3291 | OpenMPSchedType Schedule = getRuntimeSchedule(ScheduleKind, Chunked); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3292 | return Schedule == OMP_dist_sch_static; |
| 3293 | } |
| 3294 | |
| 3295 | |
Alexander Musman | df7a8e2 | 2015-01-22 08:49:35 +0000 | [diff] [blame] | 3296 | bool CGOpenMPRuntime::isDynamic(OpenMPScheduleClauseKind ScheduleKind) const { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3297 | OpenMPSchedType Schedule = |
Alexey Bataev | d7589ffe | 2015-05-20 13:12:48 +0000 | [diff] [blame] | 3298 | getRuntimeSchedule(ScheduleKind, /*Chunked=*/false, /*Ordered=*/false); |
Alexander Musman | df7a8e2 | 2015-01-22 08:49:35 +0000 | [diff] [blame] | 3299 | assert(Schedule != OMP_sch_static_chunked && "cannot be chunked here"); |
| 3300 | return Schedule != OMP_sch_static; |
| 3301 | } |
| 3302 | |
Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 3303 | static int addMonoNonMonoModifier(OpenMPSchedType Schedule, |
| 3304 | OpenMPScheduleClauseModifier M1, |
| 3305 | OpenMPScheduleClauseModifier M2) { |
Alexey Bataev | 6cff624 | 2016-05-30 13:05:14 +0000 | [diff] [blame] | 3306 | int Modifier = 0; |
Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 3307 | switch (M1) { |
| 3308 | case OMPC_SCHEDULE_MODIFIER_monotonic: |
Alexey Bataev | 6cff624 | 2016-05-30 13:05:14 +0000 | [diff] [blame] | 3309 | Modifier = OMP_sch_modifier_monotonic; |
| 3310 | break; |
Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 3311 | case OMPC_SCHEDULE_MODIFIER_nonmonotonic: |
Alexey Bataev | 6cff624 | 2016-05-30 13:05:14 +0000 | [diff] [blame] | 3312 | Modifier = OMP_sch_modifier_nonmonotonic; |
| 3313 | break; |
Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 3314 | case OMPC_SCHEDULE_MODIFIER_simd: |
Alexey Bataev | 6cff624 | 2016-05-30 13:05:14 +0000 | [diff] [blame] | 3315 | if (Schedule == OMP_sch_static_chunked) |
| 3316 | Schedule = OMP_sch_static_balanced_chunked; |
| 3317 | break; |
Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 3318 | case OMPC_SCHEDULE_MODIFIER_last: |
| 3319 | case OMPC_SCHEDULE_MODIFIER_unknown: |
| 3320 | break; |
| 3321 | } |
| 3322 | switch (M2) { |
| 3323 | case OMPC_SCHEDULE_MODIFIER_monotonic: |
Alexey Bataev | 6cff624 | 2016-05-30 13:05:14 +0000 | [diff] [blame] | 3324 | Modifier = OMP_sch_modifier_monotonic; |
| 3325 | break; |
Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 3326 | case OMPC_SCHEDULE_MODIFIER_nonmonotonic: |
Alexey Bataev | 6cff624 | 2016-05-30 13:05:14 +0000 | [diff] [blame] | 3327 | Modifier = OMP_sch_modifier_nonmonotonic; |
| 3328 | break; |
Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 3329 | case OMPC_SCHEDULE_MODIFIER_simd: |
Alexey Bataev | 6cff624 | 2016-05-30 13:05:14 +0000 | [diff] [blame] | 3330 | if (Schedule == OMP_sch_static_chunked) |
| 3331 | Schedule = OMP_sch_static_balanced_chunked; |
| 3332 | break; |
Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 3333 | case OMPC_SCHEDULE_MODIFIER_last: |
| 3334 | case OMPC_SCHEDULE_MODIFIER_unknown: |
| 3335 | break; |
| 3336 | } |
Alexey Bataev | 6cff624 | 2016-05-30 13:05:14 +0000 | [diff] [blame] | 3337 | return Schedule | Modifier; |
Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 3338 | } |
| 3339 | |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 3340 | void CGOpenMPRuntime::emitForDispatchInit( |
| 3341 | CodeGenFunction &CGF, SourceLocation Loc, |
| 3342 | const OpenMPScheduleTy &ScheduleKind, unsigned IVSize, bool IVSigned, |
| 3343 | bool Ordered, const DispatchRTInput &DispatchValues) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 3344 | if (!CGF.HaveInsertPoint()) |
| 3345 | return; |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 3346 | OpenMPSchedType Schedule = getRuntimeSchedule( |
| 3347 | ScheduleKind.Schedule, DispatchValues.Chunk != nullptr, Ordered); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3348 | assert(Ordered || |
| 3349 | (Schedule != OMP_sch_static && Schedule != OMP_sch_static_chunked && |
Alexey Bataev | 6cff624 | 2016-05-30 13:05:14 +0000 | [diff] [blame] | 3350 | Schedule != OMP_ord_static && Schedule != OMP_ord_static_chunked && |
| 3351 | Schedule != OMP_sch_static_balanced_chunked)); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3352 | // Call __kmpc_dispatch_init( |
| 3353 | // ident_t *loc, kmp_int32 tid, kmp_int32 schedule, |
| 3354 | // kmp_int[32|64] lower, kmp_int[32|64] upper, |
| 3355 | // kmp_int[32|64] stride, kmp_int[32|64] chunk); |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 3356 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3357 | // 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] | 3358 | llvm::Value *Chunk = DispatchValues.Chunk ? DispatchValues.Chunk |
| 3359 | : CGF.Builder.getIntN(IVSize, 1); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3360 | llvm::Value *Args[] = { |
Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 3361 | emitUpdateLocation(CGF, Loc), getThreadID(CGF, Loc), |
| 3362 | CGF.Builder.getInt32(addMonoNonMonoModifier( |
| 3363 | Schedule, ScheduleKind.M1, ScheduleKind.M2)), // Schedule type |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 3364 | DispatchValues.LB, // Lower |
| 3365 | DispatchValues.UB, // Upper |
Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 3366 | CGF.Builder.getIntN(IVSize, 1), // Stride |
| 3367 | Chunk // Chunk |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3368 | }; |
| 3369 | CGF.EmitRuntimeCall(createDispatchInitFunction(IVSize, IVSigned), Args); |
| 3370 | } |
| 3371 | |
Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 3372 | static void emitForStaticInitCall( |
| 3373 | CodeGenFunction &CGF, llvm::Value *UpdateLocation, llvm::Value *ThreadId, |
| 3374 | llvm::Constant *ForStaticInitFunction, OpenMPSchedType Schedule, |
| 3375 | OpenMPScheduleClauseModifier M1, OpenMPScheduleClauseModifier M2, |
Alexey Bataev | 0f87dbe | 2017-08-14 17:56:13 +0000 | [diff] [blame] | 3376 | const CGOpenMPRuntime::StaticRTInput &Values) { |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3377 | if (!CGF.HaveInsertPoint()) |
Alexey Bataev | 0f87dbe | 2017-08-14 17:56:13 +0000 | [diff] [blame] | 3378 | return; |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3379 | |
Alexey Bataev | 0f87dbe | 2017-08-14 17:56:13 +0000 | [diff] [blame] | 3380 | assert(!Values.Ordered); |
| 3381 | assert(Schedule == OMP_sch_static || Schedule == OMP_sch_static_chunked || |
| 3382 | Schedule == OMP_sch_static_balanced_chunked || |
| 3383 | Schedule == OMP_ord_static || Schedule == OMP_ord_static_chunked || |
| 3384 | Schedule == OMP_dist_sch_static || |
| 3385 | Schedule == OMP_dist_sch_static_chunked); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3386 | |
Alexey Bataev | 0f87dbe | 2017-08-14 17:56:13 +0000 | [diff] [blame] | 3387 | // Call __kmpc_for_static_init( |
| 3388 | // ident_t *loc, kmp_int32 tid, kmp_int32 schedtype, |
| 3389 | // kmp_int32 *p_lastiter, kmp_int[32|64] *p_lower, |
| 3390 | // kmp_int[32|64] *p_upper, kmp_int[32|64] *p_stride, |
| 3391 | // kmp_int[32|64] incr, kmp_int[32|64] chunk); |
| 3392 | llvm::Value *Chunk = Values.Chunk; |
| 3393 | if (Chunk == nullptr) { |
| 3394 | assert((Schedule == OMP_sch_static || Schedule == OMP_ord_static || |
| 3395 | Schedule == OMP_dist_sch_static) && |
| 3396 | "expected static non-chunked schedule"); |
| 3397 | // If the Chunk was not specified in the clause - use default value 1. |
| 3398 | Chunk = CGF.Builder.getIntN(Values.IVSize, 1); |
| 3399 | } else { |
| 3400 | assert((Schedule == OMP_sch_static_chunked || |
| 3401 | Schedule == OMP_sch_static_balanced_chunked || |
| 3402 | Schedule == OMP_ord_static_chunked || |
| 3403 | Schedule == OMP_dist_sch_static_chunked) && |
| 3404 | "expected static chunked schedule"); |
| 3405 | } |
| 3406 | llvm::Value *Args[] = { |
| 3407 | UpdateLocation, |
| 3408 | ThreadId, |
| 3409 | CGF.Builder.getInt32(addMonoNonMonoModifier(Schedule, M1, |
| 3410 | M2)), // Schedule type |
| 3411 | Values.IL.getPointer(), // &isLastIter |
| 3412 | Values.LB.getPointer(), // &LB |
| 3413 | Values.UB.getPointer(), // &UB |
| 3414 | Values.ST.getPointer(), // &Stride |
| 3415 | CGF.Builder.getIntN(Values.IVSize, 1), // Incr |
| 3416 | Chunk // Chunk |
| 3417 | }; |
| 3418 | CGF.EmitRuntimeCall(ForStaticInitFunction, Args); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3419 | } |
| 3420 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3421 | void CGOpenMPRuntime::emitForStaticInit(CodeGenFunction &CGF, |
| 3422 | SourceLocation Loc, |
Alexey Bataev | 0f87dbe | 2017-08-14 17:56:13 +0000 | [diff] [blame] | 3423 | OpenMPDirectiveKind DKind, |
Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 3424 | const OpenMPScheduleTy &ScheduleKind, |
Alexey Bataev | 0f87dbe | 2017-08-14 17:56:13 +0000 | [diff] [blame] | 3425 | const StaticRTInput &Values) { |
| 3426 | OpenMPSchedType ScheduleNum = getRuntimeSchedule( |
| 3427 | ScheduleKind.Schedule, Values.Chunk != nullptr, Values.Ordered); |
| 3428 | assert(isOpenMPWorksharingDirective(DKind) && |
| 3429 | "Expected loop-based or sections-based directive."); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3430 | llvm::Value *UpdatedLocation = emitUpdateLocation(CGF, Loc, |
Alexey Bataev | 0f87dbe | 2017-08-14 17:56:13 +0000 | [diff] [blame] | 3431 | isOpenMPLoopDirective(DKind) |
| 3432 | ? OMP_IDENT_WORK_LOOP |
| 3433 | : OMP_IDENT_WORK_SECTIONS); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3434 | llvm::Value *ThreadId = getThreadID(CGF, Loc); |
| 3435 | llvm::Constant *StaticInitFunction = |
Alexey Bataev | 0f87dbe | 2017-08-14 17:56:13 +0000 | [diff] [blame] | 3436 | createForStaticInitFunction(Values.IVSize, Values.IVSigned); |
Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 3437 | emitForStaticInitCall(CGF, UpdatedLocation, ThreadId, StaticInitFunction, |
Alexey Bataev | 0f87dbe | 2017-08-14 17:56:13 +0000 | [diff] [blame] | 3438 | ScheduleNum, ScheduleKind.M1, ScheduleKind.M2, Values); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3439 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3440 | |
Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 3441 | void CGOpenMPRuntime::emitDistributeStaticInit( |
| 3442 | CodeGenFunction &CGF, SourceLocation Loc, |
Alexey Bataev | 0f87dbe | 2017-08-14 17:56:13 +0000 | [diff] [blame] | 3443 | OpenMPDistScheduleClauseKind SchedKind, |
| 3444 | const CGOpenMPRuntime::StaticRTInput &Values) { |
| 3445 | OpenMPSchedType ScheduleNum = |
| 3446 | getRuntimeSchedule(SchedKind, Values.Chunk != nullptr); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3447 | llvm::Value *UpdatedLocation = |
Alexey Bataev | 0f87dbe | 2017-08-14 17:56:13 +0000 | [diff] [blame] | 3448 | emitUpdateLocation(CGF, Loc, OMP_IDENT_WORK_DISTRIBUTE); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3449 | llvm::Value *ThreadId = getThreadID(CGF, Loc); |
| 3450 | llvm::Constant *StaticInitFunction = |
Alexey Bataev | 0f87dbe | 2017-08-14 17:56:13 +0000 | [diff] [blame] | 3451 | createForStaticInitFunction(Values.IVSize, Values.IVSigned); |
Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 3452 | emitForStaticInitCall(CGF, UpdatedLocation, ThreadId, StaticInitFunction, |
| 3453 | ScheduleNum, OMPC_SCHEDULE_MODIFIER_unknown, |
Alexey Bataev | 0f87dbe | 2017-08-14 17:56:13 +0000 | [diff] [blame] | 3454 | OMPC_SCHEDULE_MODIFIER_unknown, Values); |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 3455 | } |
| 3456 | |
Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 3457 | void CGOpenMPRuntime::emitForStaticFinish(CodeGenFunction &CGF, |
Alexey Bataev | f43f714 | 2017-09-06 16:17:35 +0000 | [diff] [blame] | 3458 | SourceLocation Loc, |
| 3459 | OpenMPDirectiveKind DKind) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 3460 | if (!CGF.HaveInsertPoint()) |
| 3461 | return; |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 3462 | // Call __kmpc_for_static_fini(ident_t *loc, kmp_int32 tid); |
Alexey Bataev | f43f714 | 2017-09-06 16:17:35 +0000 | [diff] [blame] | 3463 | llvm::Value *Args[] = { |
| 3464 | emitUpdateLocation(CGF, Loc, |
| 3465 | isOpenMPDistributeDirective(DKind) |
| 3466 | ? OMP_IDENT_WORK_DISTRIBUTE |
| 3467 | : isOpenMPLoopDirective(DKind) |
| 3468 | ? OMP_IDENT_WORK_LOOP |
| 3469 | : OMP_IDENT_WORK_SECTIONS), |
| 3470 | getThreadID(CGF, Loc)}; |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 3471 | CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_for_static_fini), |
| 3472 | Args); |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 3473 | } |
| 3474 | |
Alexey Bataev | d7589ffe | 2015-05-20 13:12:48 +0000 | [diff] [blame] | 3475 | void CGOpenMPRuntime::emitForOrderedIterationEnd(CodeGenFunction &CGF, |
| 3476 | SourceLocation Loc, |
| 3477 | unsigned IVSize, |
| 3478 | bool IVSigned) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 3479 | if (!CGF.HaveInsertPoint()) |
| 3480 | return; |
Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 3481 | // 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] | 3482 | llvm::Value *Args[] = {emitUpdateLocation(CGF, Loc), getThreadID(CGF, Loc)}; |
Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 3483 | CGF.EmitRuntimeCall(createDispatchFiniFunction(IVSize, IVSigned), Args); |
| 3484 | } |
| 3485 | |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 3486 | llvm::Value *CGOpenMPRuntime::emitForNext(CodeGenFunction &CGF, |
| 3487 | SourceLocation Loc, unsigned IVSize, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3488 | bool IVSigned, Address IL, |
| 3489 | Address LB, Address UB, |
| 3490 | Address ST) { |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 3491 | // Call __kmpc_dispatch_next( |
| 3492 | // ident_t *loc, kmp_int32 tid, kmp_int32 *p_lastiter, |
| 3493 | // kmp_int[32|64] *p_lower, kmp_int[32|64] *p_upper, |
| 3494 | // kmp_int[32|64] *p_stride); |
| 3495 | llvm::Value *Args[] = { |
Alexey Bataev | 50b3c95 | 2016-02-19 10:38:26 +0000 | [diff] [blame] | 3496 | emitUpdateLocation(CGF, Loc), |
| 3497 | getThreadID(CGF, Loc), |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3498 | IL.getPointer(), // &isLastIter |
| 3499 | LB.getPointer(), // &Lower |
| 3500 | UB.getPointer(), // &Upper |
| 3501 | ST.getPointer() // &Stride |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 3502 | }; |
| 3503 | llvm::Value *Call = |
| 3504 | CGF.EmitRuntimeCall(createDispatchNextFunction(IVSize, IVSigned), Args); |
| 3505 | return CGF.EmitScalarConversion( |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3506 | Call, CGF.getContext().getIntTypeForBitwidth(32, /*Signed=*/1), |
Filipe Cabecinhas | 7af183d | 2015-08-11 04:19:28 +0000 | [diff] [blame] | 3507 | CGF.getContext().BoolTy, Loc); |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 3508 | } |
| 3509 | |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 3510 | void CGOpenMPRuntime::emitNumThreadsClause(CodeGenFunction &CGF, |
| 3511 | llvm::Value *NumThreads, |
| 3512 | SourceLocation Loc) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 3513 | if (!CGF.HaveInsertPoint()) |
| 3514 | return; |
Alexey Bataev | b205978 | 2014-10-13 08:23:51 +0000 | [diff] [blame] | 3515 | // Build call __kmpc_push_num_threads(&loc, global_tid, num_threads) |
| 3516 | llvm::Value *Args[] = { |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 3517 | emitUpdateLocation(CGF, Loc), getThreadID(CGF, Loc), |
Alexey Bataev | b205978 | 2014-10-13 08:23:51 +0000 | [diff] [blame] | 3518 | CGF.Builder.CreateIntCast(NumThreads, CGF.Int32Ty, /*isSigned*/ true)}; |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 3519 | CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_push_num_threads), |
| 3520 | Args); |
Alexey Bataev | b205978 | 2014-10-13 08:23:51 +0000 | [diff] [blame] | 3521 | } |
| 3522 | |
Alexey Bataev | 7f210c6 | 2015-06-18 13:40:03 +0000 | [diff] [blame] | 3523 | void CGOpenMPRuntime::emitProcBindClause(CodeGenFunction &CGF, |
| 3524 | OpenMPProcBindClauseKind ProcBind, |
| 3525 | SourceLocation Loc) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 3526 | if (!CGF.HaveInsertPoint()) |
| 3527 | return; |
Alexey Bataev | 7f210c6 | 2015-06-18 13:40:03 +0000 | [diff] [blame] | 3528 | // Constants for proc bind value accepted by the runtime. |
| 3529 | enum ProcBindTy { |
| 3530 | ProcBindFalse = 0, |
| 3531 | ProcBindTrue, |
| 3532 | ProcBindMaster, |
| 3533 | ProcBindClose, |
| 3534 | ProcBindSpread, |
| 3535 | ProcBindIntel, |
| 3536 | ProcBindDefault |
| 3537 | } RuntimeProcBind; |
| 3538 | switch (ProcBind) { |
| 3539 | case OMPC_PROC_BIND_master: |
| 3540 | RuntimeProcBind = ProcBindMaster; |
| 3541 | break; |
| 3542 | case OMPC_PROC_BIND_close: |
| 3543 | RuntimeProcBind = ProcBindClose; |
| 3544 | break; |
| 3545 | case OMPC_PROC_BIND_spread: |
| 3546 | RuntimeProcBind = ProcBindSpread; |
| 3547 | break; |
| 3548 | case OMPC_PROC_BIND_unknown: |
| 3549 | llvm_unreachable("Unsupported proc_bind value."); |
| 3550 | } |
| 3551 | // Build call __kmpc_push_proc_bind(&loc, global_tid, proc_bind) |
| 3552 | llvm::Value *Args[] = { |
| 3553 | emitUpdateLocation(CGF, Loc), getThreadID(CGF, Loc), |
| 3554 | llvm::ConstantInt::get(CGM.IntTy, RuntimeProcBind, /*isSigned=*/true)}; |
| 3555 | CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_push_proc_bind), Args); |
| 3556 | } |
| 3557 | |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 3558 | void CGOpenMPRuntime::emitFlush(CodeGenFunction &CGF, ArrayRef<const Expr *>, |
| 3559 | SourceLocation Loc) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 3560 | if (!CGF.HaveInsertPoint()) |
| 3561 | return; |
Alexey Bataev | d76df6d | 2015-02-24 12:55:09 +0000 | [diff] [blame] | 3562 | // Build call void __kmpc_flush(ident_t *loc) |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 3563 | CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_flush), |
| 3564 | emitUpdateLocation(CGF, Loc)); |
Alexey Bataev | cc37cc1 | 2014-11-20 04:34:54 +0000 | [diff] [blame] | 3565 | } |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 3566 | |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 3567 | namespace { |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 3568 | /// Indexes of fields for type kmp_task_t. |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 3569 | enum KmpTaskTFields { |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 3570 | /// List of shared variables. |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 3571 | KmpTaskTShareds, |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 3572 | /// Task routine. |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 3573 | KmpTaskTRoutine, |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 3574 | /// Partition id for the untied tasks. |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 3575 | KmpTaskTPartId, |
Alexey Bataev | ad537bb | 2016-05-30 09:06:50 +0000 | [diff] [blame] | 3576 | /// Function with call of destructors for private variables. |
| 3577 | Data1, |
| 3578 | /// Task priority. |
| 3579 | Data2, |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 3580 | /// (Taskloops only) Lower bound. |
| 3581 | KmpTaskTLowerBound, |
| 3582 | /// (Taskloops only) Upper bound. |
| 3583 | KmpTaskTUpperBound, |
| 3584 | /// (Taskloops only) Stride. |
| 3585 | KmpTaskTStride, |
| 3586 | /// (Taskloops only) Is last iteration flag. |
| 3587 | KmpTaskTLastIter, |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 3588 | /// (Taskloops only) Reduction data. |
| 3589 | KmpTaskTReductions, |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 3590 | }; |
Hans Wennborg | 7eb5464 | 2015-09-10 17:07:54 +0000 | [diff] [blame] | 3591 | } // anonymous namespace |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 3592 | |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3593 | bool CGOpenMPRuntime::OffloadEntriesInfoManagerTy::empty() const { |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 3594 | return OffloadEntriesTargetRegion.empty() && |
| 3595 | OffloadEntriesDeviceGlobalVar.empty(); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3596 | } |
| 3597 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 3598 | /// Initialize target region entry. |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3599 | void CGOpenMPRuntime::OffloadEntriesInfoManagerTy:: |
| 3600 | initializeTargetRegionEntryInfo(unsigned DeviceID, unsigned FileID, |
| 3601 | StringRef ParentName, unsigned LineNum, |
Samuel Antao | 2de62b0 | 2016-02-13 23:35:10 +0000 | [diff] [blame] | 3602 | unsigned Order) { |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3603 | assert(CGM.getLangOpts().OpenMPIsDevice && "Initialization of entries is " |
| 3604 | "only required for the device " |
| 3605 | "code generation."); |
Samuel Antao | 2de62b0 | 2016-02-13 23:35:10 +0000 | [diff] [blame] | 3606 | OffloadEntriesTargetRegion[DeviceID][FileID][ParentName][LineNum] = |
Samuel Antao | f83efdb | 2017-01-05 16:02:49 +0000 | [diff] [blame] | 3607 | OffloadEntryInfoTargetRegion(Order, /*Addr=*/nullptr, /*ID=*/nullptr, |
Alexey Bataev | 34f8a70 | 2018-03-28 14:28:54 +0000 | [diff] [blame] | 3608 | OMPTargetRegionEntryTargetRegion); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3609 | ++OffloadingEntriesNum; |
| 3610 | } |
| 3611 | |
| 3612 | void CGOpenMPRuntime::OffloadEntriesInfoManagerTy:: |
| 3613 | registerTargetRegionEntryInfo(unsigned DeviceID, unsigned FileID, |
| 3614 | StringRef ParentName, unsigned LineNum, |
Samuel Antao | f83efdb | 2017-01-05 16:02:49 +0000 | [diff] [blame] | 3615 | llvm::Constant *Addr, llvm::Constant *ID, |
Alexey Bataev | 34f8a70 | 2018-03-28 14:28:54 +0000 | [diff] [blame] | 3616 | OMPTargetRegionEntryKind Flags) { |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3617 | // If we are emitting code for a target, the entry is already initialized, |
| 3618 | // only has to be registered. |
| 3619 | if (CGM.getLangOpts().OpenMPIsDevice) { |
Alexey Bataev | 64e62dc | 2018-04-30 16:26:57 +0000 | [diff] [blame] | 3620 | if (!hasTargetRegionEntryInfo(DeviceID, FileID, ParentName, LineNum)) { |
| 3621 | unsigned DiagID = CGM.getDiags().getCustomDiagID( |
| 3622 | DiagnosticsEngine::Error, |
| 3623 | "Unable to find target region on line '%0' in the device code."); |
| 3624 | CGM.getDiags().Report(DiagID) << LineNum; |
| 3625 | return; |
| 3626 | } |
Samuel Antao | 2de62b0 | 2016-02-13 23:35:10 +0000 | [diff] [blame] | 3627 | auto &Entry = |
| 3628 | OffloadEntriesTargetRegion[DeviceID][FileID][ParentName][LineNum]; |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3629 | assert(Entry.isValid() && "Entry not initialized!"); |
| 3630 | Entry.setAddress(Addr); |
| 3631 | Entry.setID(ID); |
Samuel Antao | f83efdb | 2017-01-05 16:02:49 +0000 | [diff] [blame] | 3632 | Entry.setFlags(Flags); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3633 | } else { |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 3634 | OffloadEntryInfoTargetRegion Entry(OffloadingEntriesNum, Addr, ID, Flags); |
Samuel Antao | 2de62b0 | 2016-02-13 23:35:10 +0000 | [diff] [blame] | 3635 | OffloadEntriesTargetRegion[DeviceID][FileID][ParentName][LineNum] = Entry; |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 3636 | ++OffloadingEntriesNum; |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3637 | } |
| 3638 | } |
| 3639 | |
| 3640 | bool CGOpenMPRuntime::OffloadEntriesInfoManagerTy::hasTargetRegionEntryInfo( |
Samuel Antao | 2de62b0 | 2016-02-13 23:35:10 +0000 | [diff] [blame] | 3641 | unsigned DeviceID, unsigned FileID, StringRef ParentName, |
| 3642 | unsigned LineNum) const { |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3643 | auto PerDevice = OffloadEntriesTargetRegion.find(DeviceID); |
| 3644 | if (PerDevice == OffloadEntriesTargetRegion.end()) |
| 3645 | return false; |
| 3646 | auto PerFile = PerDevice->second.find(FileID); |
| 3647 | if (PerFile == PerDevice->second.end()) |
| 3648 | return false; |
| 3649 | auto PerParentName = PerFile->second.find(ParentName); |
| 3650 | if (PerParentName == PerFile->second.end()) |
| 3651 | return false; |
| 3652 | auto PerLine = PerParentName->second.find(LineNum); |
| 3653 | if (PerLine == PerParentName->second.end()) |
| 3654 | return false; |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3655 | // Fail if this entry is already registered. |
Samuel Antao | 2de62b0 | 2016-02-13 23:35:10 +0000 | [diff] [blame] | 3656 | if (PerLine->second.getAddress() || PerLine->second.getID()) |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3657 | return false; |
| 3658 | return true; |
| 3659 | } |
| 3660 | |
| 3661 | void CGOpenMPRuntime::OffloadEntriesInfoManagerTy::actOnTargetRegionEntriesInfo( |
| 3662 | const OffloadTargetRegionEntryInfoActTy &Action) { |
| 3663 | // Scan all target region entries and perform the provided action. |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 3664 | for (const auto &D : OffloadEntriesTargetRegion) |
| 3665 | for (const auto &F : D.second) |
| 3666 | for (const auto &P : F.second) |
| 3667 | for (const auto &L : P.second) |
Samuel Antao | 2de62b0 | 2016-02-13 23:35:10 +0000 | [diff] [blame] | 3668 | Action(D.first, F.first, P.first(), L.first, L.second); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3669 | } |
| 3670 | |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 3671 | void CGOpenMPRuntime::OffloadEntriesInfoManagerTy:: |
| 3672 | initializeDeviceGlobalVarEntryInfo(StringRef Name, |
| 3673 | OMPTargetGlobalVarEntryKind Flags, |
| 3674 | unsigned Order) { |
| 3675 | assert(CGM.getLangOpts().OpenMPIsDevice && "Initialization of entries is " |
| 3676 | "only required for the device " |
| 3677 | "code generation."); |
| 3678 | OffloadEntriesDeviceGlobalVar.try_emplace(Name, Order, Flags); |
| 3679 | ++OffloadingEntriesNum; |
| 3680 | } |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3681 | |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 3682 | void CGOpenMPRuntime::OffloadEntriesInfoManagerTy:: |
| 3683 | registerDeviceGlobalVarEntryInfo(StringRef VarName, llvm::Constant *Addr, |
| 3684 | CharUnits VarSize, |
| 3685 | OMPTargetGlobalVarEntryKind Flags, |
| 3686 | llvm::GlobalValue::LinkageTypes Linkage) { |
| 3687 | if (CGM.getLangOpts().OpenMPIsDevice) { |
| 3688 | auto &Entry = OffloadEntriesDeviceGlobalVar[VarName]; |
| 3689 | assert(Entry.isValid() && Entry.getFlags() == Flags && |
| 3690 | "Entry not initialized!"); |
| 3691 | assert((!Entry.getAddress() || Entry.getAddress() == Addr) && |
| 3692 | "Resetting with the new address."); |
| 3693 | if (Entry.getAddress() && hasDeviceGlobalVarEntryInfo(VarName)) |
| 3694 | return; |
| 3695 | Entry.setAddress(Addr); |
| 3696 | Entry.setVarSize(VarSize); |
| 3697 | Entry.setLinkage(Linkage); |
| 3698 | } else { |
| 3699 | if (hasDeviceGlobalVarEntryInfo(VarName)) |
| 3700 | return; |
| 3701 | OffloadEntriesDeviceGlobalVar.try_emplace( |
| 3702 | VarName, OffloadingEntriesNum, Addr, VarSize, Flags, Linkage); |
| 3703 | ++OffloadingEntriesNum; |
| 3704 | } |
| 3705 | } |
| 3706 | |
| 3707 | void CGOpenMPRuntime::OffloadEntriesInfoManagerTy:: |
| 3708 | actOnDeviceGlobalVarEntriesInfo( |
| 3709 | const OffloadDeviceGlobalVarEntryInfoActTy &Action) { |
| 3710 | // Scan all target region entries and perform the provided action. |
| 3711 | for (const auto &E : OffloadEntriesDeviceGlobalVar) |
| 3712 | Action(E.getKey(), E.getValue()); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3713 | } |
| 3714 | |
| 3715 | llvm::Function * |
| 3716 | CGOpenMPRuntime::createOffloadingBinaryDescriptorRegistration() { |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3717 | // If we don't have entries or if we are emitting code for the device, we |
| 3718 | // don't need to do anything. |
| 3719 | if (CGM.getLangOpts().OpenMPIsDevice || OffloadEntriesInfoManager.empty()) |
| 3720 | return nullptr; |
| 3721 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3722 | llvm::Module &M = CGM.getModule(); |
| 3723 | ASTContext &C = CGM.getContext(); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3724 | |
| 3725 | // Get list of devices we care about |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3726 | const std::vector<llvm::Triple> &Devices = CGM.getLangOpts().OMPTargetTriples; |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3727 | |
| 3728 | // We should be creating an offloading descriptor only if there are devices |
| 3729 | // specified. |
| 3730 | assert(!Devices.empty() && "No OpenMP offloading devices??"); |
| 3731 | |
| 3732 | // Create the external variables that will point to the begin and end of the |
| 3733 | // host entries section. These will be defined by the linker. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3734 | llvm::Type *OffloadEntryTy = |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3735 | CGM.getTypes().ConvertTypeForMem(getTgtOffloadEntryQTy()); |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 3736 | std::string EntriesBeginName = getName({"omp_offloading", "entries_begin"}); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3737 | auto *HostEntriesBegin = new llvm::GlobalVariable( |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3738 | M, OffloadEntryTy, /*isConstant=*/true, |
Eugene Zelenko | 1660a5d | 2016-01-26 19:01:06 +0000 | [diff] [blame] | 3739 | llvm::GlobalValue::ExternalLinkage, /*Initializer=*/nullptr, |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 3740 | EntriesBeginName); |
| 3741 | std::string EntriesEndName = getName({"omp_offloading", "entries_end"}); |
| 3742 | auto *HostEntriesEnd = |
| 3743 | new llvm::GlobalVariable(M, OffloadEntryTy, /*isConstant=*/true, |
| 3744 | llvm::GlobalValue::ExternalLinkage, |
| 3745 | /*Initializer=*/nullptr, EntriesEndName); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3746 | |
| 3747 | // Create all device images |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3748 | auto *DeviceImageTy = cast<llvm::StructType>( |
| 3749 | CGM.getTypes().ConvertTypeForMem(getTgtDeviceImageQTy())); |
John McCall | 23c9dc6 | 2016-11-28 22:18:27 +0000 | [diff] [blame] | 3750 | ConstantInitBuilder DeviceImagesBuilder(CGM); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3751 | ConstantArrayBuilder DeviceImagesEntries = |
| 3752 | DeviceImagesBuilder.beginArray(DeviceImageTy); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3753 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3754 | for (const llvm::Triple &Device : Devices) { |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 3755 | StringRef T = Device.getTriple(); |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 3756 | std::string BeginName = getName({"omp_offloading", "img_start", ""}); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3757 | auto *ImgBegin = new llvm::GlobalVariable( |
Alexey Bataev | 62a4cb0 | 2018-07-31 18:27:42 +0000 | [diff] [blame] | 3758 | M, CGM.Int8Ty, /*isConstant=*/true, |
| 3759 | llvm::GlobalValue::ExternalWeakLinkage, |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 3760 | /*Initializer=*/nullptr, Twine(BeginName).concat(T)); |
| 3761 | std::string EndName = getName({"omp_offloading", "img_end", ""}); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3762 | auto *ImgEnd = new llvm::GlobalVariable( |
Alexey Bataev | 62a4cb0 | 2018-07-31 18:27:42 +0000 | [diff] [blame] | 3763 | M, CGM.Int8Ty, /*isConstant=*/true, |
| 3764 | llvm::GlobalValue::ExternalWeakLinkage, |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 3765 | /*Initializer=*/nullptr, Twine(EndName).concat(T)); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3766 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3767 | llvm::Constant *Data[] = {ImgBegin, ImgEnd, HostEntriesBegin, |
| 3768 | HostEntriesEnd}; |
| 3769 | createConstantGlobalStructAndAddToParent(CGM, getTgtDeviceImageQTy(), Data, |
| 3770 | DeviceImagesEntries); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3771 | } |
| 3772 | |
| 3773 | // Create device images global array. |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 3774 | std::string ImagesName = getName({"omp_offloading", "device_images"}); |
John McCall | 6c9f1fdb | 2016-11-19 08:17:24 +0000 | [diff] [blame] | 3775 | llvm::GlobalVariable *DeviceImages = |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 3776 | DeviceImagesEntries.finishAndCreateGlobal(ImagesName, |
| 3777 | CGM.getPointerAlign(), |
| 3778 | /*isConstant=*/true); |
Peter Collingbourne | bcf909d | 2016-06-14 21:02:05 +0000 | [diff] [blame] | 3779 | DeviceImages->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3780 | |
| 3781 | // This is a Zero array to be used in the creation of the constant expressions |
| 3782 | llvm::Constant *Index[] = {llvm::Constant::getNullValue(CGM.Int32Ty), |
| 3783 | llvm::Constant::getNullValue(CGM.Int32Ty)}; |
| 3784 | |
| 3785 | // Create the target region descriptor. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3786 | llvm::Constant *Data[] = { |
| 3787 | llvm::ConstantInt::get(CGM.Int32Ty, Devices.size()), |
| 3788 | llvm::ConstantExpr::getGetElementPtr(DeviceImages->getValueType(), |
| 3789 | DeviceImages, Index), |
| 3790 | HostEntriesBegin, HostEntriesEnd}; |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 3791 | std::string Descriptor = getName({"omp_offloading", "descriptor"}); |
Mike Rice | e1ca7b6 | 2018-08-29 15:45:11 +0000 | [diff] [blame] | 3792 | llvm::GlobalVariable *Desc = createGlobalStruct( |
| 3793 | CGM, getTgtBinaryDescriptorQTy(), /*IsConstant=*/true, Data, Descriptor); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3794 | |
| 3795 | // Emit code to register or unregister the descriptor at execution |
| 3796 | // startup or closing, respectively. |
| 3797 | |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 3798 | llvm::Function *UnRegFn; |
| 3799 | { |
| 3800 | FunctionArgList Args; |
| 3801 | ImplicitParamDecl DummyPtr(C, C.VoidPtrTy, ImplicitParamDecl::Other); |
| 3802 | Args.push_back(&DummyPtr); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3803 | |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 3804 | CodeGenFunction CGF(CGM); |
| 3805 | // Disable debug info for global (de-)initializer because they are not part |
| 3806 | // of some particular construct. |
| 3807 | CGF.disableDebugInfo(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3808 | const auto &FI = |
| 3809 | CGM.getTypes().arrangeBuiltinFunctionDeclaration(C.VoidTy, Args); |
| 3810 | llvm::FunctionType *FTy = CGM.getTypes().GetFunctionType(FI); |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 3811 | std::string UnregName = getName({"omp_offloading", "descriptor_unreg"}); |
| 3812 | UnRegFn = CGM.CreateGlobalInitOrDestructFunction(FTy, UnregName, FI); |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 3813 | CGF.StartFunction(GlobalDecl(), C.VoidTy, UnRegFn, FI, Args); |
| 3814 | CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__tgt_unregister_lib), |
| 3815 | Desc); |
| 3816 | CGF.FinishFunction(); |
| 3817 | } |
| 3818 | llvm::Function *RegFn; |
| 3819 | { |
| 3820 | CodeGenFunction CGF(CGM); |
| 3821 | // Disable debug info for global (de-)initializer because they are not part |
| 3822 | // of some particular construct. |
| 3823 | CGF.disableDebugInfo(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3824 | const auto &FI = CGM.getTypes().arrangeNullaryFunction(); |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 3825 | llvm::FunctionType *FTy = CGM.getTypes().GetFunctionType(FI); |
Sergey Dmitriev | bde9cf9 | 2018-08-03 20:19:28 +0000 | [diff] [blame] | 3826 | |
| 3827 | // Encode offload target triples into the registration function name. It |
| 3828 | // will serve as a comdat key for the registration/unregistration code for |
| 3829 | // this particular combination of offloading targets. |
| 3830 | SmallVector<StringRef, 4U> RegFnNameParts(Devices.size() + 2U); |
| 3831 | RegFnNameParts[0] = "omp_offloading"; |
| 3832 | RegFnNameParts[1] = "descriptor_reg"; |
| 3833 | llvm::transform(Devices, std::next(RegFnNameParts.begin(), 2), |
| 3834 | [](const llvm::Triple &T) -> const std::string& { |
| 3835 | return T.getTriple(); |
| 3836 | }); |
| 3837 | llvm::sort(std::next(RegFnNameParts.begin(), 2), RegFnNameParts.end()); |
| 3838 | std::string Descriptor = getName(RegFnNameParts); |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 3839 | RegFn = CGM.CreateGlobalInitOrDestructFunction(FTy, Descriptor, FI); |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 3840 | CGF.StartFunction(GlobalDecl(), C.VoidTy, RegFn, FI, FunctionArgList()); |
| 3841 | CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__tgt_register_lib), Desc); |
| 3842 | // Create a variable to drive the registration and unregistration of the |
| 3843 | // descriptor, so we can reuse the logic that emits Ctors and Dtors. |
| 3844 | ImplicitParamDecl RegUnregVar(C, C.getTranslationUnitDecl(), |
| 3845 | SourceLocation(), nullptr, C.CharTy, |
| 3846 | ImplicitParamDecl::Other); |
| 3847 | CGM.getCXXABI().registerGlobalDtor(CGF, RegUnregVar, UnRegFn, Desc); |
| 3848 | CGF.FinishFunction(); |
| 3849 | } |
George Rokos | 29d0f00 | 2017-05-27 03:03:13 +0000 | [diff] [blame] | 3850 | if (CGM.supportsCOMDAT()) { |
| 3851 | // It is sufficient to call registration function only once, so create a |
| 3852 | // COMDAT group for registration/unregistration functions and associated |
| 3853 | // data. That would reduce startup time and code size. Registration |
| 3854 | // function serves as a COMDAT group key. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3855 | llvm::Comdat *ComdatKey = M.getOrInsertComdat(RegFn->getName()); |
George Rokos | 29d0f00 | 2017-05-27 03:03:13 +0000 | [diff] [blame] | 3856 | RegFn->setLinkage(llvm::GlobalValue::LinkOnceAnyLinkage); |
| 3857 | RegFn->setVisibility(llvm::GlobalValue::HiddenVisibility); |
| 3858 | RegFn->setComdat(ComdatKey); |
| 3859 | UnRegFn->setComdat(ComdatKey); |
| 3860 | DeviceImages->setComdat(ComdatKey); |
| 3861 | Desc->setComdat(ComdatKey); |
| 3862 | } |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3863 | return RegFn; |
| 3864 | } |
| 3865 | |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 3866 | void CGOpenMPRuntime::createOffloadEntry( |
| 3867 | llvm::Constant *ID, llvm::Constant *Addr, uint64_t Size, int32_t Flags, |
| 3868 | llvm::GlobalValue::LinkageTypes Linkage) { |
Samuel Antao | 2de62b0 | 2016-02-13 23:35:10 +0000 | [diff] [blame] | 3869 | StringRef Name = Addr->getName(); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3870 | llvm::Module &M = CGM.getModule(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3871 | llvm::LLVMContext &C = M.getContext(); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3872 | |
| 3873 | // Create constant string with the name. |
| 3874 | llvm::Constant *StrPtrInit = llvm::ConstantDataArray::getString(C, Name); |
| 3875 | |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 3876 | std::string StringName = getName({"omp_offloading", "entry_name"}); |
| 3877 | auto *Str = new llvm::GlobalVariable( |
| 3878 | M, StrPtrInit->getType(), /*isConstant=*/true, |
| 3879 | llvm::GlobalValue::InternalLinkage, StrPtrInit, StringName); |
Peter Collingbourne | bcf909d | 2016-06-14 21:02:05 +0000 | [diff] [blame] | 3880 | Str->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3881 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3882 | llvm::Constant *Data[] = {llvm::ConstantExpr::getBitCast(ID, CGM.VoidPtrTy), |
| 3883 | llvm::ConstantExpr::getBitCast(Str, CGM.Int8PtrTy), |
| 3884 | llvm::ConstantInt::get(CGM.SizeTy, Size), |
| 3885 | llvm::ConstantInt::get(CGM.Int32Ty, Flags), |
| 3886 | llvm::ConstantInt::get(CGM.Int32Ty, 0)}; |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 3887 | std::string EntryName = getName({"omp_offloading", "entry", ""}); |
Mike Rice | e1ca7b6 | 2018-08-29 15:45:11 +0000 | [diff] [blame] | 3888 | llvm::GlobalVariable *Entry = createGlobalStruct( |
| 3889 | CGM, getTgtOffloadEntryQTy(), /*IsConstant=*/true, Data, |
| 3890 | Twine(EntryName).concat(Name), llvm::GlobalValue::WeakAnyLinkage); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3891 | |
| 3892 | // 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] | 3893 | std::string Section = getName({"omp_offloading", "entries"}); |
| 3894 | Entry->setSection(Section); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3895 | } |
| 3896 | |
| 3897 | void CGOpenMPRuntime::createOffloadEntriesAndInfoMetadata() { |
| 3898 | // Emit the offloading entries and metadata so that the device codegen side |
Samuel Antao | 4c8035b | 2016-12-12 18:00:20 +0000 | [diff] [blame] | 3899 | // can easily figure out what to emit. The produced metadata looks like |
| 3900 | // this: |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3901 | // |
| 3902 | // !omp_offload.info = !{!1, ...} |
| 3903 | // |
| 3904 | // Right now we only generate metadata for function that contain target |
| 3905 | // regions. |
| 3906 | |
Alexander Kornienko | 2a8c18d | 2018-04-06 15:14:32 +0000 | [diff] [blame] | 3907 | // 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] | 3908 | if (OffloadEntriesInfoManager.empty()) |
| 3909 | return; |
| 3910 | |
| 3911 | llvm::Module &M = CGM.getModule(); |
| 3912 | llvm::LLVMContext &C = M.getContext(); |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 3913 | SmallVector<const OffloadEntriesInfoManagerTy::OffloadEntryInfo *, 16> |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3914 | OrderedEntries(OffloadEntriesInfoManager.size()); |
| 3915 | |
Simon Pilgrim | 2c51880 | 2017-03-30 14:13:19 +0000 | [diff] [blame] | 3916 | // Auxiliary methods to create metadata values and strings. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3917 | auto &&GetMDInt = [this](unsigned V) { |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3918 | return llvm::ConstantAsMetadata::get( |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3919 | llvm::ConstantInt::get(CGM.Int32Ty, V)); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3920 | }; |
| 3921 | |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 3922 | auto &&GetMDString = [&C](StringRef V) { return llvm::MDString::get(C, V); }; |
| 3923 | |
| 3924 | // Create the offloading info metadata node. |
| 3925 | llvm::NamedMDNode *MD = M.getOrInsertNamedMetadata("omp_offload.info"); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3926 | |
| 3927 | // Create function that emits metadata for each target region entry; |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 3928 | auto &&TargetRegionMetadataEmitter = |
| 3929 | [&C, MD, &OrderedEntries, &GetMDInt, &GetMDString]( |
| 3930 | unsigned DeviceID, unsigned FileID, StringRef ParentName, |
| 3931 | unsigned Line, |
| 3932 | const OffloadEntriesInfoManagerTy::OffloadEntryInfoTargetRegion &E) { |
| 3933 | // Generate metadata for target regions. Each entry of this metadata |
| 3934 | // contains: |
| 3935 | // - Entry 0 -> Kind of this type of metadata (0). |
| 3936 | // - Entry 1 -> Device ID of the file where the entry was identified. |
| 3937 | // - Entry 2 -> File ID of the file where the entry was identified. |
| 3938 | // - Entry 3 -> Mangled name of the function where the entry was |
| 3939 | // identified. |
| 3940 | // - Entry 4 -> Line in the file where the entry was identified. |
| 3941 | // - Entry 5 -> Order the entry was created. |
| 3942 | // The first element of the metadata node is the kind. |
| 3943 | llvm::Metadata *Ops[] = {GetMDInt(E.getKind()), GetMDInt(DeviceID), |
| 3944 | GetMDInt(FileID), GetMDString(ParentName), |
| 3945 | GetMDInt(Line), GetMDInt(E.getOrder())}; |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3946 | |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 3947 | // Save this entry in the right position of the ordered entries array. |
| 3948 | OrderedEntries[E.getOrder()] = &E; |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3949 | |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 3950 | // Add metadata to the named metadata node. |
| 3951 | MD->addOperand(llvm::MDNode::get(C, Ops)); |
| 3952 | }; |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3953 | |
| 3954 | OffloadEntriesInfoManager.actOnTargetRegionEntriesInfo( |
| 3955 | TargetRegionMetadataEmitter); |
| 3956 | |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 3957 | // Create function that emits metadata for each device global variable entry; |
| 3958 | auto &&DeviceGlobalVarMetadataEmitter = |
| 3959 | [&C, &OrderedEntries, &GetMDInt, &GetMDString, |
| 3960 | MD](StringRef MangledName, |
| 3961 | const OffloadEntriesInfoManagerTy::OffloadEntryInfoDeviceGlobalVar |
| 3962 | &E) { |
| 3963 | // Generate metadata for global variables. Each entry of this metadata |
| 3964 | // contains: |
| 3965 | // - Entry 0 -> Kind of this type of metadata (1). |
| 3966 | // - Entry 1 -> Mangled name of the variable. |
| 3967 | // - Entry 2 -> Declare target kind. |
| 3968 | // - Entry 3 -> Order the entry was created. |
| 3969 | // The first element of the metadata node is the kind. |
| 3970 | llvm::Metadata *Ops[] = { |
| 3971 | GetMDInt(E.getKind()), GetMDString(MangledName), |
| 3972 | GetMDInt(E.getFlags()), GetMDInt(E.getOrder())}; |
| 3973 | |
| 3974 | // Save this entry in the right position of the ordered entries array. |
| 3975 | OrderedEntries[E.getOrder()] = &E; |
| 3976 | |
| 3977 | // Add metadata to the named metadata node. |
| 3978 | MD->addOperand(llvm::MDNode::get(C, Ops)); |
| 3979 | }; |
| 3980 | |
| 3981 | OffloadEntriesInfoManager.actOnDeviceGlobalVarEntriesInfo( |
| 3982 | DeviceGlobalVarMetadataEmitter); |
| 3983 | |
| 3984 | for (const auto *E : OrderedEntries) { |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3985 | assert(E && "All ordered entries must exist!"); |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 3986 | if (const auto *CE = |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3987 | dyn_cast<OffloadEntriesInfoManagerTy::OffloadEntryInfoTargetRegion>( |
| 3988 | E)) { |
Alexey Bataev | 64e62dc | 2018-04-30 16:26:57 +0000 | [diff] [blame] | 3989 | if (!CE->getID() || !CE->getAddress()) { |
| 3990 | unsigned DiagID = CGM.getDiags().getCustomDiagID( |
| 3991 | DiagnosticsEngine::Error, |
Alexey Bataev | 7f01d20 | 2018-07-16 18:12:18 +0000 | [diff] [blame] | 3992 | "Offloading entry for target region is incorrect: either the " |
Alexey Bataev | 64e62dc | 2018-04-30 16:26:57 +0000 | [diff] [blame] | 3993 | "address or the ID is invalid."); |
| 3994 | CGM.getDiags().Report(DiagID); |
| 3995 | continue; |
| 3996 | } |
Alexey Bataev | 34f8a70 | 2018-03-28 14:28:54 +0000 | [diff] [blame] | 3997 | createOffloadEntry(CE->getID(), CE->getAddress(), /*Size=*/0, |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 3998 | CE->getFlags(), llvm::GlobalValue::WeakAnyLinkage); |
| 3999 | } else if (const auto *CE = |
| 4000 | dyn_cast<OffloadEntriesInfoManagerTy:: |
| 4001 | OffloadEntryInfoDeviceGlobalVar>(E)) { |
Alexey Bataev | c52f01d | 2018-07-16 20:05:25 +0000 | [diff] [blame] | 4002 | OffloadEntriesInfoManagerTy::OMPTargetGlobalVarEntryKind Flags = |
| 4003 | static_cast<OffloadEntriesInfoManagerTy::OMPTargetGlobalVarEntryKind>( |
| 4004 | CE->getFlags()); |
| 4005 | switch (Flags) { |
| 4006 | case OffloadEntriesInfoManagerTy::OMPTargetGlobalVarEntryTo: { |
| 4007 | if (!CE->getAddress()) { |
| 4008 | unsigned DiagID = CGM.getDiags().getCustomDiagID( |
| 4009 | DiagnosticsEngine::Error, |
| 4010 | "Offloading entry for declare target variable is incorrect: the " |
| 4011 | "address is invalid."); |
| 4012 | CGM.getDiags().Report(DiagID); |
| 4013 | continue; |
| 4014 | } |
Alexey Bataev | b4dd6d2 | 2018-08-29 20:41:37 +0000 | [diff] [blame] | 4015 | // The vaiable has no definition - no need to add the entry. |
| 4016 | if (CE->getVarSize().isZero()) |
| 4017 | continue; |
Alexey Bataev | c52f01d | 2018-07-16 20:05:25 +0000 | [diff] [blame] | 4018 | break; |
| 4019 | } |
| 4020 | case OffloadEntriesInfoManagerTy::OMPTargetGlobalVarEntryLink: |
| 4021 | assert(((CGM.getLangOpts().OpenMPIsDevice && !CE->getAddress()) || |
| 4022 | (!CGM.getLangOpts().OpenMPIsDevice && CE->getAddress())) && |
| 4023 | "Declaret target link address is set."); |
| 4024 | if (CGM.getLangOpts().OpenMPIsDevice) |
| 4025 | continue; |
| 4026 | if (!CE->getAddress()) { |
| 4027 | unsigned DiagID = CGM.getDiags().getCustomDiagID( |
| 4028 | DiagnosticsEngine::Error, |
| 4029 | "Offloading entry for declare target variable is incorrect: the " |
| 4030 | "address is invalid."); |
| 4031 | CGM.getDiags().Report(DiagID); |
| 4032 | continue; |
| 4033 | } |
| 4034 | break; |
Alexey Bataev | 64e62dc | 2018-04-30 16:26:57 +0000 | [diff] [blame] | 4035 | } |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 4036 | createOffloadEntry(CE->getAddress(), CE->getAddress(), |
Alexey Bataev | c52f01d | 2018-07-16 20:05:25 +0000 | [diff] [blame] | 4037 | CE->getVarSize().getQuantity(), Flags, |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 4038 | CE->getLinkage()); |
| 4039 | } else { |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4040 | llvm_unreachable("Unsupported entry kind."); |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 4041 | } |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4042 | } |
| 4043 | } |
| 4044 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4045 | /// Loads all the offload entries information from the host IR |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4046 | /// metadata. |
| 4047 | void CGOpenMPRuntime::loadOffloadInfoMetadata() { |
| 4048 | // If we are in target mode, load the metadata from the host IR. This code has |
| 4049 | // to match the metadaata creation in createOffloadEntriesAndInfoMetadata(). |
| 4050 | |
| 4051 | if (!CGM.getLangOpts().OpenMPIsDevice) |
| 4052 | return; |
| 4053 | |
| 4054 | if (CGM.getLangOpts().OMPHostIRFile.empty()) |
| 4055 | return; |
| 4056 | |
| 4057 | auto Buf = llvm::MemoryBuffer::getFile(CGM.getLangOpts().OMPHostIRFile); |
Alexey Bataev | 64e62dc | 2018-04-30 16:26:57 +0000 | [diff] [blame] | 4058 | if (auto EC = Buf.getError()) { |
| 4059 | CGM.getDiags().Report(diag::err_cannot_open_file) |
| 4060 | << CGM.getLangOpts().OMPHostIRFile << EC.message(); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4061 | return; |
Alexey Bataev | 64e62dc | 2018-04-30 16:26:57 +0000 | [diff] [blame] | 4062 | } |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4063 | |
| 4064 | llvm::LLVMContext C; |
Peter Collingbourne | d9445c4 | 2016-11-13 07:00:17 +0000 | [diff] [blame] | 4065 | auto ME = expectedToErrorOrAndEmitErrors( |
| 4066 | C, llvm::parseBitcodeFile(Buf.get()->getMemBufferRef(), C)); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4067 | |
Alexey Bataev | 64e62dc | 2018-04-30 16:26:57 +0000 | [diff] [blame] | 4068 | if (auto EC = ME.getError()) { |
| 4069 | unsigned DiagID = CGM.getDiags().getCustomDiagID( |
| 4070 | DiagnosticsEngine::Error, "Unable to parse host IR file '%0':'%1'"); |
| 4071 | CGM.getDiags().Report(DiagID) |
| 4072 | << CGM.getLangOpts().OMPHostIRFile << EC.message(); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4073 | return; |
Alexey Bataev | 64e62dc | 2018-04-30 16:26:57 +0000 | [diff] [blame] | 4074 | } |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4075 | |
| 4076 | llvm::NamedMDNode *MD = ME.get()->getNamedMetadata("omp_offload.info"); |
| 4077 | if (!MD) |
| 4078 | return; |
| 4079 | |
George Burgess IV | 00f70bd | 2018-03-01 05:43:23 +0000 | [diff] [blame] | 4080 | for (llvm::MDNode *MN : MD->operands()) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4081 | auto &&GetMDInt = [MN](unsigned Idx) { |
| 4082 | auto *V = cast<llvm::ConstantAsMetadata>(MN->getOperand(Idx)); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4083 | return cast<llvm::ConstantInt>(V->getValue())->getZExtValue(); |
| 4084 | }; |
| 4085 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4086 | auto &&GetMDString = [MN](unsigned Idx) { |
| 4087 | auto *V = cast<llvm::MDString>(MN->getOperand(Idx)); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4088 | return V->getString(); |
| 4089 | }; |
| 4090 | |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 4091 | switch (GetMDInt(0)) { |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4092 | default: |
| 4093 | llvm_unreachable("Unexpected metadata!"); |
| 4094 | break; |
| 4095 | case OffloadEntriesInfoManagerTy::OffloadEntryInfo:: |
Alexey Bataev | 34f8a70 | 2018-03-28 14:28:54 +0000 | [diff] [blame] | 4096 | OffloadingEntryInfoTargetRegion: |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4097 | OffloadEntriesInfoManager.initializeTargetRegionEntryInfo( |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 4098 | /*DeviceID=*/GetMDInt(1), /*FileID=*/GetMDInt(2), |
| 4099 | /*ParentName=*/GetMDString(3), /*Line=*/GetMDInt(4), |
| 4100 | /*Order=*/GetMDInt(5)); |
| 4101 | break; |
| 4102 | case OffloadEntriesInfoManagerTy::OffloadEntryInfo:: |
| 4103 | OffloadingEntryInfoDeviceGlobalVar: |
| 4104 | OffloadEntriesInfoManager.initializeDeviceGlobalVarEntryInfo( |
| 4105 | /*MangledName=*/GetMDString(1), |
| 4106 | static_cast<OffloadEntriesInfoManagerTy::OMPTargetGlobalVarEntryKind>( |
| 4107 | /*Flags=*/GetMDInt(2)), |
| 4108 | /*Order=*/GetMDInt(3)); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4109 | break; |
| 4110 | } |
| 4111 | } |
| 4112 | } |
| 4113 | |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 4114 | void CGOpenMPRuntime::emitKmpRoutineEntryT(QualType KmpInt32Ty) { |
| 4115 | if (!KmpRoutineEntryPtrTy) { |
| 4116 | // Build typedef kmp_int32 (* kmp_routine_entry_t)(kmp_int32, void *); type. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4117 | ASTContext &C = CGM.getContext(); |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 4118 | QualType KmpRoutineEntryTyArgs[] = {KmpInt32Ty, C.VoidPtrTy}; |
| 4119 | FunctionProtoType::ExtProtoInfo EPI; |
| 4120 | KmpRoutineEntryPtrQTy = C.getPointerType( |
| 4121 | C.getFunctionType(KmpInt32Ty, KmpRoutineEntryTyArgs, EPI)); |
| 4122 | KmpRoutineEntryPtrTy = CGM.getTypes().ConvertType(KmpRoutineEntryPtrQTy); |
| 4123 | } |
| 4124 | } |
| 4125 | |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4126 | QualType CGOpenMPRuntime::getTgtOffloadEntryQTy() { |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4127 | // Make sure the type of the entry is already created. This is the type we |
| 4128 | // have to create: |
| 4129 | // struct __tgt_offload_entry{ |
| 4130 | // void *addr; // Pointer to the offload entry info. |
| 4131 | // // (function or global) |
| 4132 | // char *name; // Name of the function or global. |
| 4133 | // 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] | 4134 | // int32_t flags; // Flags associated with the entry, e.g. 'link'. |
| 4135 | // int32_t reserved; // Reserved, to use by the runtime library. |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4136 | // }; |
| 4137 | if (TgtOffloadEntryQTy.isNull()) { |
| 4138 | ASTContext &C = CGM.getContext(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4139 | RecordDecl *RD = C.buildImplicitRecord("__tgt_offload_entry"); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4140 | RD->startDefinition(); |
| 4141 | addFieldToRecordDecl(C, RD, C.VoidPtrTy); |
| 4142 | addFieldToRecordDecl(C, RD, C.getPointerType(C.CharTy)); |
| 4143 | addFieldToRecordDecl(C, RD, C.getSizeType()); |
Samuel Antao | f83efdb | 2017-01-05 16:02:49 +0000 | [diff] [blame] | 4144 | addFieldToRecordDecl( |
| 4145 | C, RD, C.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/true)); |
| 4146 | addFieldToRecordDecl( |
| 4147 | C, RD, C.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/true)); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4148 | RD->completeDefinition(); |
Jonas Hahnfeld | 5e4df28 | 2018-01-18 15:38:03 +0000 | [diff] [blame] | 4149 | RD->addAttr(PackedAttr::CreateImplicit(C)); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4150 | TgtOffloadEntryQTy = C.getRecordType(RD); |
| 4151 | } |
| 4152 | return TgtOffloadEntryQTy; |
| 4153 | } |
| 4154 | |
| 4155 | QualType CGOpenMPRuntime::getTgtDeviceImageQTy() { |
| 4156 | // These are the types we need to build: |
| 4157 | // struct __tgt_device_image{ |
| 4158 | // void *ImageStart; // Pointer to the target code start. |
| 4159 | // void *ImageEnd; // Pointer to the target code end. |
| 4160 | // // We also add the host entries to the device image, as it may be useful |
| 4161 | // // for the target runtime to have access to that information. |
| 4162 | // __tgt_offload_entry *EntriesBegin; // Begin of the table with all |
| 4163 | // // the entries. |
| 4164 | // __tgt_offload_entry *EntriesEnd; // End of the table with all the |
| 4165 | // // entries (non inclusive). |
| 4166 | // }; |
| 4167 | if (TgtDeviceImageQTy.isNull()) { |
| 4168 | ASTContext &C = CGM.getContext(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4169 | RecordDecl *RD = C.buildImplicitRecord("__tgt_device_image"); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4170 | RD->startDefinition(); |
| 4171 | addFieldToRecordDecl(C, RD, C.VoidPtrTy); |
| 4172 | addFieldToRecordDecl(C, RD, C.VoidPtrTy); |
| 4173 | addFieldToRecordDecl(C, RD, C.getPointerType(getTgtOffloadEntryQTy())); |
| 4174 | addFieldToRecordDecl(C, RD, C.getPointerType(getTgtOffloadEntryQTy())); |
| 4175 | RD->completeDefinition(); |
| 4176 | TgtDeviceImageQTy = C.getRecordType(RD); |
| 4177 | } |
| 4178 | return TgtDeviceImageQTy; |
| 4179 | } |
| 4180 | |
| 4181 | QualType CGOpenMPRuntime::getTgtBinaryDescriptorQTy() { |
| 4182 | // struct __tgt_bin_desc{ |
| 4183 | // int32_t NumDevices; // Number of devices supported. |
| 4184 | // __tgt_device_image *DeviceImages; // Arrays of device images |
| 4185 | // // (one per device). |
| 4186 | // __tgt_offload_entry *EntriesBegin; // Begin of the table with all the |
| 4187 | // // entries. |
| 4188 | // __tgt_offload_entry *EntriesEnd; // End of the table with all the |
| 4189 | // // entries (non inclusive). |
| 4190 | // }; |
| 4191 | if (TgtBinaryDescriptorQTy.isNull()) { |
| 4192 | ASTContext &C = CGM.getContext(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4193 | RecordDecl *RD = C.buildImplicitRecord("__tgt_bin_desc"); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4194 | RD->startDefinition(); |
| 4195 | addFieldToRecordDecl( |
| 4196 | C, RD, C.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/true)); |
| 4197 | addFieldToRecordDecl(C, RD, C.getPointerType(getTgtDeviceImageQTy())); |
| 4198 | addFieldToRecordDecl(C, RD, C.getPointerType(getTgtOffloadEntryQTy())); |
| 4199 | addFieldToRecordDecl(C, RD, C.getPointerType(getTgtOffloadEntryQTy())); |
| 4200 | RD->completeDefinition(); |
| 4201 | TgtBinaryDescriptorQTy = C.getRecordType(RD); |
| 4202 | } |
| 4203 | return TgtBinaryDescriptorQTy; |
| 4204 | } |
| 4205 | |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 4206 | namespace { |
Alexey Bataev | 9e03404 | 2015-05-05 04:05:12 +0000 | [diff] [blame] | 4207 | struct PrivateHelpersTy { |
| 4208 | PrivateHelpersTy(const VarDecl *Original, const VarDecl *PrivateCopy, |
| 4209 | const VarDecl *PrivateElemInit) |
| 4210 | : Original(Original), PrivateCopy(PrivateCopy), |
| 4211 | PrivateElemInit(PrivateElemInit) {} |
| 4212 | const VarDecl *Original; |
| 4213 | const VarDecl *PrivateCopy; |
| 4214 | const VarDecl *PrivateElemInit; |
| 4215 | }; |
| 4216 | typedef std::pair<CharUnits /*Align*/, PrivateHelpersTy> PrivateDataTy; |
Hans Wennborg | 7eb5464 | 2015-09-10 17:07:54 +0000 | [diff] [blame] | 4217 | } // anonymous namespace |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 4218 | |
Alexey Bataev | 9e03404 | 2015-05-05 04:05:12 +0000 | [diff] [blame] | 4219 | static RecordDecl * |
Craig Topper | 8674c5c | 2015-09-29 04:30:07 +0000 | [diff] [blame] | 4220 | createPrivatesRecordDecl(CodeGenModule &CGM, ArrayRef<PrivateDataTy> Privates) { |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 4221 | if (!Privates.empty()) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4222 | ASTContext &C = CGM.getContext(); |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 4223 | // Build struct .kmp_privates_t. { |
| 4224 | // /* private vars */ |
| 4225 | // }; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4226 | RecordDecl *RD = C.buildImplicitRecord(".kmp_privates.t"); |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 4227 | RD->startDefinition(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4228 | for (const auto &Pair : Privates) { |
| 4229 | const VarDecl *VD = Pair.second.Original; |
| 4230 | QualType Type = VD->getType().getNonReferenceType(); |
| 4231 | FieldDecl *FD = addFieldToRecordDecl(C, RD, Type); |
Alexey Bataev | c71a409 | 2015-09-11 10:29:41 +0000 | [diff] [blame] | 4232 | if (VD->hasAttrs()) { |
| 4233 | for (specific_attr_iterator<AlignedAttr> I(VD->getAttrs().begin()), |
| 4234 | E(VD->getAttrs().end()); |
| 4235 | I != E; ++I) |
| 4236 | FD->addAttr(*I); |
| 4237 | } |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 4238 | } |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 4239 | RD->completeDefinition(); |
| 4240 | return RD; |
| 4241 | } |
| 4242 | return nullptr; |
| 4243 | } |
| 4244 | |
Alexey Bataev | 9e03404 | 2015-05-05 04:05:12 +0000 | [diff] [blame] | 4245 | static RecordDecl * |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4246 | createKmpTaskTRecordDecl(CodeGenModule &CGM, OpenMPDirectiveKind Kind, |
| 4247 | QualType KmpInt32Ty, |
Alexey Bataev | 8fc69dc | 2015-05-18 07:54:53 +0000 | [diff] [blame] | 4248 | QualType KmpRoutineEntryPointerQTy) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4249 | ASTContext &C = CGM.getContext(); |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 4250 | // Build struct kmp_task_t { |
| 4251 | // void * shareds; |
| 4252 | // kmp_routine_entry_t routine; |
| 4253 | // kmp_int32 part_id; |
Alexey Bataev | ad537bb | 2016-05-30 09:06:50 +0000 | [diff] [blame] | 4254 | // kmp_cmplrdata_t data1; |
| 4255 | // kmp_cmplrdata_t data2; |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4256 | // For taskloops additional fields: |
| 4257 | // kmp_uint64 lb; |
| 4258 | // kmp_uint64 ub; |
| 4259 | // kmp_int64 st; |
| 4260 | // kmp_int32 liter; |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 4261 | // void * reductions; |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 4262 | // }; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4263 | RecordDecl *UD = C.buildImplicitRecord("kmp_cmplrdata_t", TTK_Union); |
Alexey Bataev | ad537bb | 2016-05-30 09:06:50 +0000 | [diff] [blame] | 4264 | UD->startDefinition(); |
| 4265 | addFieldToRecordDecl(C, UD, KmpInt32Ty); |
| 4266 | addFieldToRecordDecl(C, UD, KmpRoutineEntryPointerQTy); |
| 4267 | UD->completeDefinition(); |
| 4268 | QualType KmpCmplrdataTy = C.getRecordType(UD); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4269 | RecordDecl *RD = C.buildImplicitRecord("kmp_task_t"); |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 4270 | RD->startDefinition(); |
| 4271 | addFieldToRecordDecl(C, RD, C.VoidPtrTy); |
| 4272 | addFieldToRecordDecl(C, RD, KmpRoutineEntryPointerQTy); |
| 4273 | addFieldToRecordDecl(C, RD, KmpInt32Ty); |
Alexey Bataev | ad537bb | 2016-05-30 09:06:50 +0000 | [diff] [blame] | 4274 | addFieldToRecordDecl(C, RD, KmpCmplrdataTy); |
| 4275 | addFieldToRecordDecl(C, RD, KmpCmplrdataTy); |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4276 | if (isOpenMPTaskLoopDirective(Kind)) { |
| 4277 | QualType KmpUInt64Ty = |
| 4278 | CGM.getContext().getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0); |
| 4279 | QualType KmpInt64Ty = |
| 4280 | CGM.getContext().getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1); |
| 4281 | addFieldToRecordDecl(C, RD, KmpUInt64Ty); |
| 4282 | addFieldToRecordDecl(C, RD, KmpUInt64Ty); |
| 4283 | addFieldToRecordDecl(C, RD, KmpInt64Ty); |
| 4284 | addFieldToRecordDecl(C, RD, KmpInt32Ty); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 4285 | addFieldToRecordDecl(C, RD, C.VoidPtrTy); |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4286 | } |
Alexey Bataev | 8fc69dc | 2015-05-18 07:54:53 +0000 | [diff] [blame] | 4287 | RD->completeDefinition(); |
| 4288 | return RD; |
| 4289 | } |
| 4290 | |
| 4291 | static RecordDecl * |
| 4292 | createKmpTaskTWithPrivatesRecordDecl(CodeGenModule &CGM, QualType KmpTaskTQTy, |
Craig Topper | 8674c5c | 2015-09-29 04:30:07 +0000 | [diff] [blame] | 4293 | ArrayRef<PrivateDataTy> Privates) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4294 | ASTContext &C = CGM.getContext(); |
Alexey Bataev | 8fc69dc | 2015-05-18 07:54:53 +0000 | [diff] [blame] | 4295 | // Build struct kmp_task_t_with_privates { |
| 4296 | // kmp_task_t task_data; |
| 4297 | // .kmp_privates_t. privates; |
| 4298 | // }; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4299 | RecordDecl *RD = C.buildImplicitRecord("kmp_task_t_with_privates"); |
Alexey Bataev | 8fc69dc | 2015-05-18 07:54:53 +0000 | [diff] [blame] | 4300 | RD->startDefinition(); |
| 4301 | addFieldToRecordDecl(C, RD, KmpTaskTQTy); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4302 | if (const RecordDecl *PrivateRD = createPrivatesRecordDecl(CGM, Privates)) |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 4303 | addFieldToRecordDecl(C, RD, C.getRecordType(PrivateRD)); |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 4304 | RD->completeDefinition(); |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 4305 | return RD; |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 4306 | } |
| 4307 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4308 | /// Emit a proxy function which accepts kmp_task_t as the second |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 4309 | /// argument. |
| 4310 | /// \code |
| 4311 | /// kmp_int32 .omp_task_entry.(kmp_int32 gtid, kmp_task_t *tt) { |
Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 4312 | /// TaskFunction(gtid, tt->part_id, &tt->privates, task_privates_map, tt, |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4313 | /// For taskloops: |
| 4314 | /// 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] | 4315 | /// tt->reductions, tt->shareds); |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 4316 | /// return 0; |
| 4317 | /// } |
| 4318 | /// \endcode |
| 4319 | static llvm::Value * |
| 4320 | emitProxyTaskFunction(CodeGenModule &CGM, SourceLocation Loc, |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4321 | OpenMPDirectiveKind Kind, QualType KmpInt32Ty, |
| 4322 | QualType KmpTaskTWithPrivatesPtrQTy, |
Alexey Bataev | 8fc69dc | 2015-05-18 07:54:53 +0000 | [diff] [blame] | 4323 | QualType KmpTaskTWithPrivatesQTy, QualType KmpTaskTQTy, |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4324 | QualType SharedsPtrTy, llvm::Value *TaskFunction, |
| 4325 | llvm::Value *TaskPrivatesMap) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4326 | ASTContext &C = CGM.getContext(); |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 4327 | FunctionArgList Args; |
Alexey Bataev | 5622323 | 2017-06-09 13:40:18 +0000 | [diff] [blame] | 4328 | ImplicitParamDecl GtidArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, KmpInt32Ty, |
| 4329 | ImplicitParamDecl::Other); |
| 4330 | ImplicitParamDecl TaskTypeArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, |
| 4331 | KmpTaskTWithPrivatesPtrQTy.withRestrict(), |
| 4332 | ImplicitParamDecl::Other); |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 4333 | Args.push_back(&GtidArg); |
| 4334 | Args.push_back(&TaskTypeArg); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4335 | const auto &TaskEntryFnInfo = |
John McCall | c56a8b3 | 2016-03-11 04:30:31 +0000 | [diff] [blame] | 4336 | CGM.getTypes().arrangeBuiltinFunctionDeclaration(KmpInt32Ty, Args); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4337 | llvm::FunctionType *TaskEntryTy = |
| 4338 | CGM.getTypes().GetFunctionType(TaskEntryFnInfo); |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 4339 | std::string Name = CGM.getOpenMPRuntime().getName({"omp_task_entry", ""}); |
| 4340 | auto *TaskEntry = llvm::Function::Create( |
| 4341 | TaskEntryTy, llvm::GlobalValue::InternalLinkage, Name, &CGM.getModule()); |
Rafael Espindola | 51ec5a9 | 2018-02-28 23:46:35 +0000 | [diff] [blame] | 4342 | CGM.SetInternalFunctionAttributes(GlobalDecl(), TaskEntry, TaskEntryFnInfo); |
Alexey Bataev | c0f879b | 2018-04-10 20:10:53 +0000 | [diff] [blame] | 4343 | TaskEntry->setDoesNotRecurse(); |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 4344 | CodeGenFunction CGF(CGM); |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 4345 | CGF.StartFunction(GlobalDecl(), KmpInt32Ty, TaskEntry, TaskEntryFnInfo, Args, |
| 4346 | Loc, Loc); |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 4347 | |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4348 | // TaskFunction(gtid, tt->task_data.part_id, &tt->privates, task_privates_map, |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4349 | // tt, |
| 4350 | // For taskloops: |
| 4351 | // tt->task_data.lb, tt->task_data.ub, tt->task_data.st, tt->task_data.liter, |
| 4352 | // tt->task_data.shareds); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4353 | llvm::Value *GtidParam = CGF.EmitLoadOfScalar( |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4354 | CGF.GetAddrOfLocalVar(&GtidArg), /*Volatile=*/false, KmpInt32Ty, Loc); |
Alexey Bataev | 31300ed | 2016-02-04 11:27:03 +0000 | [diff] [blame] | 4355 | LValue TDBase = CGF.EmitLoadOfPointerLValue( |
| 4356 | CGF.GetAddrOfLocalVar(&TaskTypeArg), |
| 4357 | KmpTaskTWithPrivatesPtrQTy->castAs<PointerType>()); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4358 | const auto *KmpTaskTWithPrivatesQTyRD = |
Alexey Bataev | 8fc69dc | 2015-05-18 07:54:53 +0000 | [diff] [blame] | 4359 | cast<RecordDecl>(KmpTaskTWithPrivatesQTy->getAsTagDecl()); |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4360 | LValue Base = |
| 4361 | CGF.EmitLValueForField(TDBase, *KmpTaskTWithPrivatesQTyRD->field_begin()); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4362 | const auto *KmpTaskTQTyRD = cast<RecordDecl>(KmpTaskTQTy->getAsTagDecl()); |
Alexey Bataev | 8fc69dc | 2015-05-18 07:54:53 +0000 | [diff] [blame] | 4363 | auto PartIdFI = std::next(KmpTaskTQTyRD->field_begin(), KmpTaskTPartId); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4364 | LValue PartIdLVal = CGF.EmitLValueForField(Base, *PartIdFI); |
| 4365 | llvm::Value *PartidParam = PartIdLVal.getPointer(); |
Alexey Bataev | 8fc69dc | 2015-05-18 07:54:53 +0000 | [diff] [blame] | 4366 | |
| 4367 | auto SharedsFI = std::next(KmpTaskTQTyRD->field_begin(), KmpTaskTShareds); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4368 | LValue SharedsLVal = CGF.EmitLValueForField(Base, *SharedsFI); |
| 4369 | llvm::Value *SharedsParam = CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
Alexey Bataev | 1e49137 | 2018-01-23 18:44:14 +0000 | [diff] [blame] | 4370 | CGF.EmitLoadOfScalar(SharedsLVal, Loc), |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 4371 | CGF.ConvertTypeForMem(SharedsPtrTy)); |
| 4372 | |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4373 | auto PrivatesFI = std::next(KmpTaskTWithPrivatesQTyRD->field_begin(), 1); |
| 4374 | llvm::Value *PrivatesParam; |
| 4375 | if (PrivatesFI != KmpTaskTWithPrivatesQTyRD->field_end()) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4376 | LValue PrivatesLVal = CGF.EmitLValueForField(TDBase, *PrivatesFI); |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4377 | PrivatesParam = CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4378 | PrivatesLVal.getPointer(), CGF.VoidPtrTy); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4379 | } else { |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4380 | PrivatesParam = llvm::ConstantPointerNull::get(CGF.VoidPtrTy); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4381 | } |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4382 | |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4383 | llvm::Value *CommonArgs[] = {GtidParam, PartidParam, PrivatesParam, |
| 4384 | TaskPrivatesMap, |
| 4385 | CGF.Builder |
| 4386 | .CreatePointerBitCastOrAddrSpaceCast( |
| 4387 | TDBase.getAddress(), CGF.VoidPtrTy) |
| 4388 | .getPointer()}; |
| 4389 | SmallVector<llvm::Value *, 16> CallArgs(std::begin(CommonArgs), |
| 4390 | std::end(CommonArgs)); |
| 4391 | if (isOpenMPTaskLoopDirective(Kind)) { |
| 4392 | auto LBFI = std::next(KmpTaskTQTyRD->field_begin(), KmpTaskTLowerBound); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4393 | LValue LBLVal = CGF.EmitLValueForField(Base, *LBFI); |
| 4394 | llvm::Value *LBParam = CGF.EmitLoadOfScalar(LBLVal, Loc); |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4395 | auto UBFI = std::next(KmpTaskTQTyRD->field_begin(), KmpTaskTUpperBound); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4396 | LValue UBLVal = CGF.EmitLValueForField(Base, *UBFI); |
| 4397 | llvm::Value *UBParam = CGF.EmitLoadOfScalar(UBLVal, Loc); |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4398 | auto StFI = std::next(KmpTaskTQTyRD->field_begin(), KmpTaskTStride); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4399 | LValue StLVal = CGF.EmitLValueForField(Base, *StFI); |
| 4400 | llvm::Value *StParam = CGF.EmitLoadOfScalar(StLVal, Loc); |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4401 | auto LIFI = std::next(KmpTaskTQTyRD->field_begin(), KmpTaskTLastIter); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4402 | LValue LILVal = CGF.EmitLValueForField(Base, *LIFI); |
| 4403 | llvm::Value *LIParam = CGF.EmitLoadOfScalar(LILVal, Loc); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 4404 | auto RFI = std::next(KmpTaskTQTyRD->field_begin(), KmpTaskTReductions); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4405 | LValue RLVal = CGF.EmitLValueForField(Base, *RFI); |
| 4406 | llvm::Value *RParam = CGF.EmitLoadOfScalar(RLVal, Loc); |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4407 | CallArgs.push_back(LBParam); |
| 4408 | CallArgs.push_back(UBParam); |
| 4409 | CallArgs.push_back(StParam); |
| 4410 | CallArgs.push_back(LIParam); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 4411 | CallArgs.push_back(RParam); |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4412 | } |
| 4413 | CallArgs.push_back(SharedsParam); |
| 4414 | |
Alexey Bataev | 3c595a6 | 2017-08-14 15:01:03 +0000 | [diff] [blame] | 4415 | CGM.getOpenMPRuntime().emitOutlinedFunctionCall(CGF, Loc, TaskFunction, |
| 4416 | CallArgs); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4417 | CGF.EmitStoreThroughLValue(RValue::get(CGF.Builder.getInt32(/*C=*/0)), |
| 4418 | CGF.MakeAddrLValue(CGF.ReturnValue, KmpInt32Ty)); |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 4419 | CGF.FinishFunction(); |
| 4420 | return TaskEntry; |
| 4421 | } |
| 4422 | |
Alexey Bataev | 8fc69dc | 2015-05-18 07:54:53 +0000 | [diff] [blame] | 4423 | static llvm::Value *emitDestructorsFunction(CodeGenModule &CGM, |
| 4424 | SourceLocation Loc, |
| 4425 | QualType KmpInt32Ty, |
| 4426 | QualType KmpTaskTWithPrivatesPtrQTy, |
| 4427 | QualType KmpTaskTWithPrivatesQTy) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4428 | ASTContext &C = CGM.getContext(); |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 4429 | FunctionArgList Args; |
Alexey Bataev | 5622323 | 2017-06-09 13:40:18 +0000 | [diff] [blame] | 4430 | ImplicitParamDecl GtidArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, KmpInt32Ty, |
| 4431 | ImplicitParamDecl::Other); |
| 4432 | ImplicitParamDecl TaskTypeArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, |
| 4433 | KmpTaskTWithPrivatesPtrQTy.withRestrict(), |
| 4434 | ImplicitParamDecl::Other); |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 4435 | Args.push_back(&GtidArg); |
| 4436 | Args.push_back(&TaskTypeArg); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4437 | const auto &DestructorFnInfo = |
John McCall | c56a8b3 | 2016-03-11 04:30:31 +0000 | [diff] [blame] | 4438 | CGM.getTypes().arrangeBuiltinFunctionDeclaration(KmpInt32Ty, Args); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4439 | llvm::FunctionType *DestructorFnTy = |
| 4440 | CGM.getTypes().GetFunctionType(DestructorFnInfo); |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 4441 | std::string Name = |
| 4442 | CGM.getOpenMPRuntime().getName({"omp_task_destructor", ""}); |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 4443 | auto *DestructorFn = |
| 4444 | llvm::Function::Create(DestructorFnTy, llvm::GlobalValue::InternalLinkage, |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 4445 | Name, &CGM.getModule()); |
Rafael Espindola | 51ec5a9 | 2018-02-28 23:46:35 +0000 | [diff] [blame] | 4446 | CGM.SetInternalFunctionAttributes(GlobalDecl(), DestructorFn, |
Akira Hatanaka | 44a59f8 | 2015-10-28 02:30:47 +0000 | [diff] [blame] | 4447 | DestructorFnInfo); |
Alexey Bataev | c0f879b | 2018-04-10 20:10:53 +0000 | [diff] [blame] | 4448 | DestructorFn->setDoesNotRecurse(); |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 4449 | CodeGenFunction CGF(CGM); |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 4450 | CGF.StartFunction(GlobalDecl(), KmpInt32Ty, DestructorFn, DestructorFnInfo, |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 4451 | Args, Loc, Loc); |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 4452 | |
Alexey Bataev | 31300ed | 2016-02-04 11:27:03 +0000 | [diff] [blame] | 4453 | LValue Base = CGF.EmitLoadOfPointerLValue( |
| 4454 | CGF.GetAddrOfLocalVar(&TaskTypeArg), |
| 4455 | KmpTaskTWithPrivatesPtrQTy->castAs<PointerType>()); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4456 | const auto *KmpTaskTWithPrivatesQTyRD = |
Alexey Bataev | 8fc69dc | 2015-05-18 07:54:53 +0000 | [diff] [blame] | 4457 | cast<RecordDecl>(KmpTaskTWithPrivatesQTy->getAsTagDecl()); |
| 4458 | auto FI = std::next(KmpTaskTWithPrivatesQTyRD->field_begin()); |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 4459 | Base = CGF.EmitLValueForField(Base, *FI); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4460 | for (const auto *Field : |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 4461 | cast<RecordDecl>(FI->getType()->getAsTagDecl())->fields()) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4462 | if (QualType::DestructionKind DtorKind = |
| 4463 | Field->getType().isDestructedType()) { |
| 4464 | LValue FieldLValue = CGF.EmitLValueForField(Base, Field); |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 4465 | CGF.pushDestroy(DtorKind, FieldLValue.getAddress(), Field->getType()); |
| 4466 | } |
| 4467 | } |
| 4468 | CGF.FinishFunction(); |
| 4469 | return DestructorFn; |
| 4470 | } |
| 4471 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4472 | /// Emit a privates mapping function for correct handling of private and |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4473 | /// firstprivate variables. |
| 4474 | /// \code |
| 4475 | /// void .omp_task_privates_map.(const .privates. *noalias privs, <ty1> |
| 4476 | /// **noalias priv1,..., <tyn> **noalias privn) { |
| 4477 | /// *priv1 = &.privates.priv1; |
| 4478 | /// ...; |
| 4479 | /// *privn = &.privates.privn; |
| 4480 | /// } |
| 4481 | /// \endcode |
| 4482 | static llvm::Value * |
| 4483 | emitTaskPrivateMappingFunction(CodeGenModule &CGM, SourceLocation Loc, |
Craig Topper | 8674c5c | 2015-09-29 04:30:07 +0000 | [diff] [blame] | 4484 | ArrayRef<const Expr *> PrivateVars, |
| 4485 | ArrayRef<const Expr *> FirstprivateVars, |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4486 | ArrayRef<const Expr *> LastprivateVars, |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4487 | QualType PrivatesQTy, |
Craig Topper | 8674c5c | 2015-09-29 04:30:07 +0000 | [diff] [blame] | 4488 | ArrayRef<PrivateDataTy> Privates) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4489 | ASTContext &C = CGM.getContext(); |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4490 | FunctionArgList Args; |
| 4491 | ImplicitParamDecl TaskPrivatesArg( |
| 4492 | C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, |
Alexey Bataev | 5622323 | 2017-06-09 13:40:18 +0000 | [diff] [blame] | 4493 | C.getPointerType(PrivatesQTy).withConst().withRestrict(), |
| 4494 | ImplicitParamDecl::Other); |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4495 | Args.push_back(&TaskPrivatesArg); |
| 4496 | llvm::DenseMap<const VarDecl *, unsigned> PrivateVarsPos; |
| 4497 | unsigned Counter = 1; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4498 | for (const Expr *E : PrivateVars) { |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4499 | Args.push_back(ImplicitParamDecl::Create( |
Alexey Bataev | 5622323 | 2017-06-09 13:40:18 +0000 | [diff] [blame] | 4500 | C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, |
| 4501 | C.getPointerType(C.getPointerType(E->getType())) |
| 4502 | .withConst() |
| 4503 | .withRestrict(), |
| 4504 | ImplicitParamDecl::Other)); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4505 | const auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl()); |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4506 | PrivateVarsPos[VD] = Counter; |
| 4507 | ++Counter; |
| 4508 | } |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4509 | for (const Expr *E : FirstprivateVars) { |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4510 | Args.push_back(ImplicitParamDecl::Create( |
Alexey Bataev | 5622323 | 2017-06-09 13:40:18 +0000 | [diff] [blame] | 4511 | C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, |
| 4512 | C.getPointerType(C.getPointerType(E->getType())) |
| 4513 | .withConst() |
| 4514 | .withRestrict(), |
| 4515 | ImplicitParamDecl::Other)); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4516 | const auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl()); |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4517 | PrivateVarsPos[VD] = Counter; |
| 4518 | ++Counter; |
| 4519 | } |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4520 | for (const Expr *E : LastprivateVars) { |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4521 | Args.push_back(ImplicitParamDecl::Create( |
Alexey Bataev | 5622323 | 2017-06-09 13:40:18 +0000 | [diff] [blame] | 4522 | C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, |
| 4523 | C.getPointerType(C.getPointerType(E->getType())) |
| 4524 | .withConst() |
| 4525 | .withRestrict(), |
| 4526 | ImplicitParamDecl::Other)); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4527 | const auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl()); |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4528 | PrivateVarsPos[VD] = Counter; |
| 4529 | ++Counter; |
| 4530 | } |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4531 | const auto &TaskPrivatesMapFnInfo = |
John McCall | c56a8b3 | 2016-03-11 04:30:31 +0000 | [diff] [blame] | 4532 | CGM.getTypes().arrangeBuiltinFunctionDeclaration(C.VoidTy, Args); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4533 | llvm::FunctionType *TaskPrivatesMapTy = |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4534 | CGM.getTypes().GetFunctionType(TaskPrivatesMapFnInfo); |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 4535 | std::string Name = |
| 4536 | CGM.getOpenMPRuntime().getName({"omp_task_privates_map", ""}); |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4537 | auto *TaskPrivatesMap = llvm::Function::Create( |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 4538 | TaskPrivatesMapTy, llvm::GlobalValue::InternalLinkage, Name, |
| 4539 | &CGM.getModule()); |
Rafael Espindola | 51ec5a9 | 2018-02-28 23:46:35 +0000 | [diff] [blame] | 4540 | CGM.SetInternalFunctionAttributes(GlobalDecl(), TaskPrivatesMap, |
Akira Hatanaka | 44a59f8 | 2015-10-28 02:30:47 +0000 | [diff] [blame] | 4541 | TaskPrivatesMapFnInfo); |
Chandler Carruth | fcd3314 | 2016-12-23 01:24:49 +0000 | [diff] [blame] | 4542 | TaskPrivatesMap->removeFnAttr(llvm::Attribute::NoInline); |
Mehdi Amini | 6aa9e9b | 2017-05-29 05:38:20 +0000 | [diff] [blame] | 4543 | TaskPrivatesMap->removeFnAttr(llvm::Attribute::OptimizeNone); |
Evgeniy Stepanov | 6b2a61d | 2015-09-14 21:35:16 +0000 | [diff] [blame] | 4544 | TaskPrivatesMap->addFnAttr(llvm::Attribute::AlwaysInline); |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4545 | CodeGenFunction CGF(CGM); |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4546 | CGF.StartFunction(GlobalDecl(), C.VoidTy, TaskPrivatesMap, |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 4547 | TaskPrivatesMapFnInfo, Args, Loc, Loc); |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4548 | |
| 4549 | // *privi = &.privates.privi; |
Alexey Bataev | 31300ed | 2016-02-04 11:27:03 +0000 | [diff] [blame] | 4550 | LValue Base = CGF.EmitLoadOfPointerLValue( |
| 4551 | CGF.GetAddrOfLocalVar(&TaskPrivatesArg), |
| 4552 | TaskPrivatesArg.getType()->castAs<PointerType>()); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4553 | const auto *PrivatesQTyRD = cast<RecordDecl>(PrivatesQTy->getAsTagDecl()); |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4554 | Counter = 0; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4555 | for (const FieldDecl *Field : PrivatesQTyRD->fields()) { |
| 4556 | LValue FieldLVal = CGF.EmitLValueForField(Base, Field); |
| 4557 | const VarDecl *VD = Args[PrivateVarsPos[Privates[Counter].second.Original]]; |
| 4558 | LValue RefLVal = |
| 4559 | CGF.MakeAddrLValue(CGF.GetAddrOfLocalVar(VD), VD->getType()); |
| 4560 | LValue RefLoadLVal = CGF.EmitLoadOfPointerLValue( |
Alexey Bataev | 31300ed | 2016-02-04 11:27:03 +0000 | [diff] [blame] | 4561 | RefLVal.getAddress(), RefLVal.getType()->castAs<PointerType>()); |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 4562 | CGF.EmitStoreOfScalar(FieldLVal.getPointer(), RefLoadLVal); |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4563 | ++Counter; |
| 4564 | } |
| 4565 | CGF.FinishFunction(); |
| 4566 | return TaskPrivatesMap; |
| 4567 | } |
| 4568 | |
Mandeep Singh Grang | b14fb6a2 | 2017-11-28 20:41:13 +0000 | [diff] [blame] | 4569 | static bool stable_sort_comparator(const PrivateDataTy P1, |
| 4570 | const PrivateDataTy P2) { |
| 4571 | return P1.first > P2.first; |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 4572 | } |
| 4573 | |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4574 | /// Emit initialization for private variables in task-based directives. |
Alexey Bataev | 8a83159 | 2016-05-10 10:36:51 +0000 | [diff] [blame] | 4575 | static void emitPrivatesInit(CodeGenFunction &CGF, |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4576 | const OMPExecutableDirective &D, |
| 4577 | Address KmpTaskSharedsPtr, LValue TDBase, |
| 4578 | const RecordDecl *KmpTaskTWithPrivatesQTyRD, |
| 4579 | QualType SharedsTy, QualType SharedsPtrTy, |
| 4580 | const OMPTaskDataTy &Data, |
| 4581 | ArrayRef<PrivateDataTy> Privates, bool ForDup) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4582 | ASTContext &C = CGF.getContext(); |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4583 | auto FI = std::next(KmpTaskTWithPrivatesQTyRD->field_begin()); |
| 4584 | LValue PrivatesBase = CGF.EmitLValueForField(TDBase, *FI); |
Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 4585 | OpenMPDirectiveKind Kind = isOpenMPTaskLoopDirective(D.getDirectiveKind()) |
| 4586 | ? OMPD_taskloop |
| 4587 | : OMPD_task; |
| 4588 | const CapturedStmt &CS = *D.getCapturedStmt(Kind); |
| 4589 | CodeGenFunction::CGCapturedStmtInfo CapturesInfo(CS); |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4590 | LValue SrcBase; |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 4591 | bool IsTargetTask = |
| 4592 | isOpenMPTargetDataManagementDirective(D.getDirectiveKind()) || |
| 4593 | isOpenMPTargetExecutionDirective(D.getDirectiveKind()); |
| 4594 | // For target-based directives skip 3 firstprivate arrays BasePointersArray, |
| 4595 | // PointersArray and SizesArray. The original variables for these arrays are |
| 4596 | // not captured and we get their addresses explicitly. |
| 4597 | if ((!IsTargetTask && !Data.FirstprivateVars.empty()) || |
Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 4598 | (IsTargetTask && KmpTaskSharedsPtr.isValid())) { |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4599 | SrcBase = CGF.MakeAddrLValue( |
| 4600 | CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
| 4601 | KmpTaskSharedsPtr, CGF.ConvertTypeForMem(SharedsPtrTy)), |
| 4602 | SharedsTy); |
| 4603 | } |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4604 | FI = cast<RecordDecl>(FI->getType()->getAsTagDecl())->field_begin(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4605 | for (const PrivateDataTy &Pair : Privates) { |
| 4606 | const VarDecl *VD = Pair.second.PrivateCopy; |
| 4607 | const Expr *Init = VD->getAnyInitializer(); |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4608 | if (Init && (!ForDup || (isa<CXXConstructExpr>(Init) && |
| 4609 | !CGF.isTrivialInitializer(Init)))) { |
Alexey Bataev | 8a83159 | 2016-05-10 10:36:51 +0000 | [diff] [blame] | 4610 | LValue PrivateLValue = CGF.EmitLValueForField(PrivatesBase, *FI); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4611 | if (const VarDecl *Elem = Pair.second.PrivateElemInit) { |
| 4612 | const VarDecl *OriginalVD = Pair.second.Original; |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 4613 | // Check if the variable is the target-based BasePointersArray, |
| 4614 | // PointersArray or SizesArray. |
| 4615 | LValue SharedRefLValue; |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4616 | QualType Type = OriginalVD->getType(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4617 | const FieldDecl *SharedField = CapturesInfo.lookup(OriginalVD); |
Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 4618 | if (IsTargetTask && !SharedField) { |
| 4619 | assert(isa<ImplicitParamDecl>(OriginalVD) && |
| 4620 | isa<CapturedDecl>(OriginalVD->getDeclContext()) && |
| 4621 | cast<CapturedDecl>(OriginalVD->getDeclContext()) |
| 4622 | ->getNumParams() == 0 && |
| 4623 | isa<TranslationUnitDecl>( |
| 4624 | cast<CapturedDecl>(OriginalVD->getDeclContext()) |
| 4625 | ->getDeclContext()) && |
| 4626 | "Expected artificial target data variable."); |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 4627 | SharedRefLValue = |
| 4628 | CGF.MakeAddrLValue(CGF.GetAddrOfLocalVar(OriginalVD), Type); |
| 4629 | } else { |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 4630 | SharedRefLValue = CGF.EmitLValueForField(SrcBase, SharedField); |
| 4631 | SharedRefLValue = CGF.MakeAddrLValue( |
| 4632 | Address(SharedRefLValue.getPointer(), C.getDeclAlign(OriginalVD)), |
| 4633 | SharedRefLValue.getType(), LValueBaseInfo(AlignmentSource::Decl), |
| 4634 | SharedRefLValue.getTBAAInfo()); |
| 4635 | } |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4636 | if (Type->isArrayType()) { |
| 4637 | // Initialize firstprivate array. |
| 4638 | if (!isa<CXXConstructExpr>(Init) || CGF.isTrivialInitializer(Init)) { |
| 4639 | // Perform simple memcpy. |
Ivan A. Kosarev | 1860b52 | 2018-01-25 14:21:55 +0000 | [diff] [blame] | 4640 | CGF.EmitAggregateAssign(PrivateLValue, SharedRefLValue, Type); |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4641 | } else { |
| 4642 | // Initialize firstprivate array using element-by-element |
Simon Pilgrim | 2c51880 | 2017-03-30 14:13:19 +0000 | [diff] [blame] | 4643 | // initialization. |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4644 | CGF.EmitOMPAggregateAssign( |
| 4645 | PrivateLValue.getAddress(), SharedRefLValue.getAddress(), Type, |
| 4646 | [&CGF, Elem, Init, &CapturesInfo](Address DestElement, |
| 4647 | Address SrcElement) { |
| 4648 | // Clean up any temporaries needed by the initialization. |
| 4649 | CodeGenFunction::OMPPrivateScope InitScope(CGF); |
| 4650 | InitScope.addPrivate( |
| 4651 | Elem, [SrcElement]() -> Address { return SrcElement; }); |
| 4652 | (void)InitScope.Privatize(); |
| 4653 | // Emit initialization for single element. |
| 4654 | CodeGenFunction::CGCapturedStmtRAII CapInfoRAII( |
| 4655 | CGF, &CapturesInfo); |
| 4656 | CGF.EmitAnyExprToMem(Init, DestElement, |
| 4657 | Init->getType().getQualifiers(), |
| 4658 | /*IsInitializer=*/false); |
| 4659 | }); |
| 4660 | } |
| 4661 | } else { |
| 4662 | CodeGenFunction::OMPPrivateScope InitScope(CGF); |
| 4663 | InitScope.addPrivate(Elem, [SharedRefLValue]() -> Address { |
| 4664 | return SharedRefLValue.getAddress(); |
| 4665 | }); |
| 4666 | (void)InitScope.Privatize(); |
| 4667 | CodeGenFunction::CGCapturedStmtRAII CapInfoRAII(CGF, &CapturesInfo); |
| 4668 | CGF.EmitExprAsInit(Init, VD, PrivateLValue, |
| 4669 | /*capturedByInit=*/false); |
| 4670 | } |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4671 | } else { |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4672 | CGF.EmitExprAsInit(Init, VD, PrivateLValue, /*capturedByInit=*/false); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4673 | } |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4674 | } |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4675 | ++FI; |
| 4676 | } |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4677 | } |
| 4678 | |
| 4679 | /// Check if duplication function is required for taskloops. |
| 4680 | static bool checkInitIsRequired(CodeGenFunction &CGF, |
| 4681 | ArrayRef<PrivateDataTy> Privates) { |
| 4682 | bool InitRequired = false; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4683 | for (const PrivateDataTy &Pair : Privates) { |
| 4684 | const VarDecl *VD = Pair.second.PrivateCopy; |
| 4685 | const Expr *Init = VD->getAnyInitializer(); |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4686 | InitRequired = InitRequired || (Init && isa<CXXConstructExpr>(Init) && |
| 4687 | !CGF.isTrivialInitializer(Init)); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4688 | if (InitRequired) |
| 4689 | break; |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4690 | } |
| 4691 | return InitRequired; |
| 4692 | } |
| 4693 | |
| 4694 | |
| 4695 | /// Emit task_dup function (for initialization of |
| 4696 | /// private/firstprivate/lastprivate vars and last_iter flag) |
| 4697 | /// \code |
| 4698 | /// void __task_dup_entry(kmp_task_t *task_dst, const kmp_task_t *task_src, int |
| 4699 | /// lastpriv) { |
| 4700 | /// // setup lastprivate flag |
| 4701 | /// task_dst->last = lastpriv; |
| 4702 | /// // could be constructor calls here... |
| 4703 | /// } |
| 4704 | /// \endcode |
| 4705 | static llvm::Value * |
| 4706 | emitTaskDupFunction(CodeGenModule &CGM, SourceLocation Loc, |
| 4707 | const OMPExecutableDirective &D, |
| 4708 | QualType KmpTaskTWithPrivatesPtrQTy, |
| 4709 | const RecordDecl *KmpTaskTWithPrivatesQTyRD, |
| 4710 | const RecordDecl *KmpTaskTQTyRD, QualType SharedsTy, |
| 4711 | QualType SharedsPtrTy, const OMPTaskDataTy &Data, |
| 4712 | ArrayRef<PrivateDataTy> Privates, bool WithLastIter) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4713 | ASTContext &C = CGM.getContext(); |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4714 | FunctionArgList Args; |
Alexey Bataev | 5622323 | 2017-06-09 13:40:18 +0000 | [diff] [blame] | 4715 | ImplicitParamDecl DstArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, |
| 4716 | KmpTaskTWithPrivatesPtrQTy, |
| 4717 | ImplicitParamDecl::Other); |
| 4718 | ImplicitParamDecl SrcArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, |
| 4719 | KmpTaskTWithPrivatesPtrQTy, |
| 4720 | ImplicitParamDecl::Other); |
| 4721 | ImplicitParamDecl LastprivArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, C.IntTy, |
| 4722 | ImplicitParamDecl::Other); |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4723 | Args.push_back(&DstArg); |
| 4724 | Args.push_back(&SrcArg); |
| 4725 | Args.push_back(&LastprivArg); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4726 | const auto &TaskDupFnInfo = |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4727 | CGM.getTypes().arrangeBuiltinFunctionDeclaration(C.VoidTy, Args); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4728 | llvm::FunctionType *TaskDupTy = CGM.getTypes().GetFunctionType(TaskDupFnInfo); |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 4729 | std::string Name = CGM.getOpenMPRuntime().getName({"omp_task_dup", ""}); |
| 4730 | auto *TaskDup = llvm::Function::Create( |
| 4731 | TaskDupTy, llvm::GlobalValue::InternalLinkage, Name, &CGM.getModule()); |
Rafael Espindola | 51ec5a9 | 2018-02-28 23:46:35 +0000 | [diff] [blame] | 4732 | CGM.SetInternalFunctionAttributes(GlobalDecl(), TaskDup, TaskDupFnInfo); |
Alexey Bataev | c0f879b | 2018-04-10 20:10:53 +0000 | [diff] [blame] | 4733 | TaskDup->setDoesNotRecurse(); |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4734 | CodeGenFunction CGF(CGM); |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 4735 | CGF.StartFunction(GlobalDecl(), C.VoidTy, TaskDup, TaskDupFnInfo, Args, Loc, |
| 4736 | Loc); |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4737 | |
| 4738 | LValue TDBase = CGF.EmitLoadOfPointerLValue( |
| 4739 | CGF.GetAddrOfLocalVar(&DstArg), |
| 4740 | KmpTaskTWithPrivatesPtrQTy->castAs<PointerType>()); |
| 4741 | // task_dst->liter = lastpriv; |
| 4742 | if (WithLastIter) { |
| 4743 | auto LIFI = std::next(KmpTaskTQTyRD->field_begin(), KmpTaskTLastIter); |
| 4744 | LValue Base = CGF.EmitLValueForField( |
| 4745 | TDBase, *KmpTaskTWithPrivatesQTyRD->field_begin()); |
| 4746 | LValue LILVal = CGF.EmitLValueForField(Base, *LIFI); |
| 4747 | llvm::Value *Lastpriv = CGF.EmitLoadOfScalar( |
| 4748 | CGF.GetAddrOfLocalVar(&LastprivArg), /*Volatile=*/false, C.IntTy, Loc); |
| 4749 | CGF.EmitStoreOfScalar(Lastpriv, LILVal); |
| 4750 | } |
| 4751 | |
| 4752 | // Emit initial values for private copies (if any). |
| 4753 | assert(!Privates.empty()); |
| 4754 | Address KmpTaskSharedsPtr = Address::invalid(); |
| 4755 | if (!Data.FirstprivateVars.empty()) { |
| 4756 | LValue TDBase = CGF.EmitLoadOfPointerLValue( |
| 4757 | CGF.GetAddrOfLocalVar(&SrcArg), |
| 4758 | KmpTaskTWithPrivatesPtrQTy->castAs<PointerType>()); |
| 4759 | LValue Base = CGF.EmitLValueForField( |
| 4760 | TDBase, *KmpTaskTWithPrivatesQTyRD->field_begin()); |
| 4761 | KmpTaskSharedsPtr = Address( |
| 4762 | CGF.EmitLoadOfScalar(CGF.EmitLValueForField( |
| 4763 | Base, *std::next(KmpTaskTQTyRD->field_begin(), |
| 4764 | KmpTaskTShareds)), |
| 4765 | Loc), |
| 4766 | CGF.getNaturalTypeAlignment(SharedsTy)); |
| 4767 | } |
Alexey Bataev | 8a83159 | 2016-05-10 10:36:51 +0000 | [diff] [blame] | 4768 | emitPrivatesInit(CGF, D, KmpTaskSharedsPtr, TDBase, KmpTaskTWithPrivatesQTyRD, |
| 4769 | SharedsTy, SharedsPtrTy, Data, Privates, /*ForDup=*/true); |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4770 | CGF.FinishFunction(); |
| 4771 | return TaskDup; |
| 4772 | } |
| 4773 | |
Alexey Bataev | 8a83159 | 2016-05-10 10:36:51 +0000 | [diff] [blame] | 4774 | /// Checks if destructor function is required to be generated. |
| 4775 | /// \return true if cleanups are required, false otherwise. |
| 4776 | static bool |
| 4777 | checkDestructorsRequired(const RecordDecl *KmpTaskTWithPrivatesQTyRD) { |
| 4778 | bool NeedsCleanup = false; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4779 | auto FI = std::next(KmpTaskTWithPrivatesQTyRD->field_begin(), 1); |
| 4780 | const auto *PrivateRD = cast<RecordDecl>(FI->getType()->getAsTagDecl()); |
| 4781 | for (const FieldDecl *FD : PrivateRD->fields()) { |
Alexey Bataev | 8a83159 | 2016-05-10 10:36:51 +0000 | [diff] [blame] | 4782 | NeedsCleanup = NeedsCleanup || FD->getType().isDestructedType(); |
| 4783 | if (NeedsCleanup) |
| 4784 | break; |
| 4785 | } |
| 4786 | return NeedsCleanup; |
| 4787 | } |
| 4788 | |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 4789 | CGOpenMPRuntime::TaskResultTy |
| 4790 | CGOpenMPRuntime::emitTaskInit(CodeGenFunction &CGF, SourceLocation Loc, |
| 4791 | const OMPExecutableDirective &D, |
| 4792 | llvm::Value *TaskFunction, QualType SharedsTy, |
| 4793 | Address Shareds, const OMPTaskDataTy &Data) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4794 | ASTContext &C = CGM.getContext(); |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4795 | llvm::SmallVector<PrivateDataTy, 4> Privates; |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4796 | // Aggregate privates and sort them by the alignment. |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 4797 | auto I = Data.PrivateCopies.begin(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4798 | for (const Expr *E : Data.PrivateVars) { |
| 4799 | const auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl()); |
Alexey Bataev | 43a919f | 2018-04-13 17:48:43 +0000 | [diff] [blame] | 4800 | Privates.emplace_back( |
Alexey Bataev | c71a409 | 2015-09-11 10:29:41 +0000 | [diff] [blame] | 4801 | C.getDeclAlign(VD), |
Alexey Bataev | 9e03404 | 2015-05-05 04:05:12 +0000 | [diff] [blame] | 4802 | PrivateHelpersTy(VD, cast<VarDecl>(cast<DeclRefExpr>(*I)->getDecl()), |
Alexey Bataev | 43a919f | 2018-04-13 17:48:43 +0000 | [diff] [blame] | 4803 | /*PrivateElemInit=*/nullptr)); |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 4804 | ++I; |
| 4805 | } |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 4806 | I = Data.FirstprivateCopies.begin(); |
| 4807 | auto IElemInitRef = Data.FirstprivateInits.begin(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4808 | for (const Expr *E : Data.FirstprivateVars) { |
| 4809 | const auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl()); |
Alexey Bataev | 43a919f | 2018-04-13 17:48:43 +0000 | [diff] [blame] | 4810 | Privates.emplace_back( |
Alexey Bataev | c71a409 | 2015-09-11 10:29:41 +0000 | [diff] [blame] | 4811 | C.getDeclAlign(VD), |
Alexey Bataev | 9e03404 | 2015-05-05 04:05:12 +0000 | [diff] [blame] | 4812 | PrivateHelpersTy( |
| 4813 | VD, cast<VarDecl>(cast<DeclRefExpr>(*I)->getDecl()), |
Alexey Bataev | 43a919f | 2018-04-13 17:48:43 +0000 | [diff] [blame] | 4814 | cast<VarDecl>(cast<DeclRefExpr>(*IElemInitRef)->getDecl()))); |
Richard Trieu | cc3949d | 2016-02-18 22:34:54 +0000 | [diff] [blame] | 4815 | ++I; |
| 4816 | ++IElemInitRef; |
Alexey Bataev | 9e03404 | 2015-05-05 04:05:12 +0000 | [diff] [blame] | 4817 | } |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4818 | I = Data.LastprivateCopies.begin(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4819 | for (const Expr *E : Data.LastprivateVars) { |
| 4820 | const auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl()); |
Alexey Bataev | 43a919f | 2018-04-13 17:48:43 +0000 | [diff] [blame] | 4821 | Privates.emplace_back( |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4822 | C.getDeclAlign(VD), |
| 4823 | PrivateHelpersTy(VD, cast<VarDecl>(cast<DeclRefExpr>(*I)->getDecl()), |
Alexey Bataev | 43a919f | 2018-04-13 17:48:43 +0000 | [diff] [blame] | 4824 | /*PrivateElemInit=*/nullptr)); |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4825 | ++I; |
| 4826 | } |
Mandeep Singh Grang | b14fb6a2 | 2017-11-28 20:41:13 +0000 | [diff] [blame] | 4827 | std::stable_sort(Privates.begin(), Privates.end(), stable_sort_comparator); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4828 | QualType KmpInt32Ty = C.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1); |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 4829 | // Build type kmp_routine_entry_t (if not built yet). |
| 4830 | emitKmpRoutineEntryT(KmpInt32Ty); |
Alexey Bataev | 8fc69dc | 2015-05-18 07:54:53 +0000 | [diff] [blame] | 4831 | // Build type kmp_task_t (if not built yet). |
Alexey Bataev | e213f3e | 2017-10-11 15:29:40 +0000 | [diff] [blame] | 4832 | if (isOpenMPTaskLoopDirective(D.getDirectiveKind())) { |
| 4833 | if (SavedKmpTaskloopTQTy.isNull()) { |
| 4834 | SavedKmpTaskloopTQTy = C.getRecordType(createKmpTaskTRecordDecl( |
| 4835 | CGM, D.getDirectiveKind(), KmpInt32Ty, KmpRoutineEntryPtrQTy)); |
| 4836 | } |
| 4837 | KmpTaskTQTy = SavedKmpTaskloopTQTy; |
Alexey Bataev | 3a03a7f | 2017-10-11 15:56:38 +0000 | [diff] [blame] | 4838 | } else { |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 4839 | assert((D.getDirectiveKind() == OMPD_task || |
| 4840 | isOpenMPTargetExecutionDirective(D.getDirectiveKind()) || |
| 4841 | isOpenMPTargetDataManagementDirective(D.getDirectiveKind())) && |
| 4842 | "Expected taskloop, task or target directive"); |
Alexey Bataev | e213f3e | 2017-10-11 15:29:40 +0000 | [diff] [blame] | 4843 | if (SavedKmpTaskTQTy.isNull()) { |
| 4844 | SavedKmpTaskTQTy = C.getRecordType(createKmpTaskTRecordDecl( |
| 4845 | CGM, D.getDirectiveKind(), KmpInt32Ty, KmpRoutineEntryPtrQTy)); |
| 4846 | } |
| 4847 | KmpTaskTQTy = SavedKmpTaskTQTy; |
Alexey Bataev | 8fc69dc | 2015-05-18 07:54:53 +0000 | [diff] [blame] | 4848 | } |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4849 | const auto *KmpTaskTQTyRD = cast<RecordDecl>(KmpTaskTQTy->getAsTagDecl()); |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 4850 | // Build particular struct kmp_task_t for the given task. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4851 | const RecordDecl *KmpTaskTWithPrivatesQTyRD = |
Alexey Bataev | 8fc69dc | 2015-05-18 07:54:53 +0000 | [diff] [blame] | 4852 | createKmpTaskTWithPrivatesRecordDecl(CGM, KmpTaskTQTy, Privates); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4853 | QualType KmpTaskTWithPrivatesQTy = C.getRecordType(KmpTaskTWithPrivatesQTyRD); |
Alexey Bataev | 8fc69dc | 2015-05-18 07:54:53 +0000 | [diff] [blame] | 4854 | QualType KmpTaskTWithPrivatesPtrQTy = |
| 4855 | C.getPointerType(KmpTaskTWithPrivatesQTy); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4856 | llvm::Type *KmpTaskTWithPrivatesTy = CGF.ConvertType(KmpTaskTWithPrivatesQTy); |
| 4857 | llvm::Type *KmpTaskTWithPrivatesPtrTy = |
| 4858 | KmpTaskTWithPrivatesTy->getPointerTo(); |
| 4859 | llvm::Value *KmpTaskTWithPrivatesTySize = |
| 4860 | CGF.getTypeSize(KmpTaskTWithPrivatesQTy); |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 4861 | QualType SharedsPtrTy = C.getPointerType(SharedsTy); |
| 4862 | |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4863 | // Emit initial values for private copies (if any). |
| 4864 | llvm::Value *TaskPrivatesMap = nullptr; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4865 | llvm::Type *TaskPrivatesMapTy = |
Reid Kleckner | e258c44 | 2017-03-16 18:55:46 +0000 | [diff] [blame] | 4866 | std::next(cast<llvm::Function>(TaskFunction)->arg_begin(), 3)->getType(); |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4867 | if (!Privates.empty()) { |
| 4868 | auto FI = std::next(KmpTaskTWithPrivatesQTyRD->field_begin()); |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4869 | TaskPrivatesMap = emitTaskPrivateMappingFunction( |
| 4870 | CGM, Loc, Data.PrivateVars, Data.FirstprivateVars, Data.LastprivateVars, |
| 4871 | FI->getType(), Privates); |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4872 | TaskPrivatesMap = CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
| 4873 | TaskPrivatesMap, TaskPrivatesMapTy); |
| 4874 | } else { |
| 4875 | TaskPrivatesMap = llvm::ConstantPointerNull::get( |
| 4876 | cast<llvm::PointerType>(TaskPrivatesMapTy)); |
| 4877 | } |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 4878 | // Build a proxy function kmp_int32 .omp_task_entry.(kmp_int32 gtid, |
| 4879 | // kmp_task_t *tt); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4880 | llvm::Value *TaskEntry = emitProxyTaskFunction( |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4881 | CGM, Loc, D.getDirectiveKind(), KmpInt32Ty, KmpTaskTWithPrivatesPtrQTy, |
| 4882 | KmpTaskTWithPrivatesQTy, KmpTaskTQTy, SharedsPtrTy, TaskFunction, |
| 4883 | TaskPrivatesMap); |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 4884 | |
| 4885 | // Build call kmp_task_t * __kmpc_omp_task_alloc(ident_t *, kmp_int32 gtid, |
| 4886 | // kmp_int32 flags, size_t sizeof_kmp_task_t, size_t sizeof_shareds, |
| 4887 | // kmp_routine_entry_t *task_entry); |
| 4888 | // Task flags. Format is taken from |
| 4889 | // http://llvm.org/svn/llvm-project/openmp/trunk/runtime/src/kmp.h, |
| 4890 | // description of kmp_tasking_flags struct. |
Alexey Bataev | 1e1e286 | 2016-05-10 12:21:02 +0000 | [diff] [blame] | 4891 | enum { |
| 4892 | TiedFlag = 0x1, |
| 4893 | FinalFlag = 0x2, |
| 4894 | DestructorsFlag = 0x8, |
| 4895 | PriorityFlag = 0x20 |
| 4896 | }; |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 4897 | unsigned Flags = Data.Tied ? TiedFlag : 0; |
Alexey Bataev | 8a83159 | 2016-05-10 10:36:51 +0000 | [diff] [blame] | 4898 | bool NeedsCleanup = false; |
| 4899 | if (!Privates.empty()) { |
| 4900 | NeedsCleanup = checkDestructorsRequired(KmpTaskTWithPrivatesQTyRD); |
| 4901 | if (NeedsCleanup) |
| 4902 | Flags = Flags | DestructorsFlag; |
| 4903 | } |
Alexey Bataev | 1e1e286 | 2016-05-10 12:21:02 +0000 | [diff] [blame] | 4904 | if (Data.Priority.getInt()) |
| 4905 | Flags = Flags | PriorityFlag; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4906 | llvm::Value *TaskFlags = |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 4907 | Data.Final.getPointer() |
| 4908 | ? CGF.Builder.CreateSelect(Data.Final.getPointer(), |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 4909 | CGF.Builder.getInt32(FinalFlag), |
| 4910 | CGF.Builder.getInt32(/*C=*/0)) |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 4911 | : CGF.Builder.getInt32(Data.Final.getInt() ? FinalFlag : 0); |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 4912 | TaskFlags = CGF.Builder.CreateOr(TaskFlags, CGF.Builder.getInt32(Flags)); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4913 | llvm::Value *SharedsSize = CGM.getSize(C.getTypeSizeInChars(SharedsTy)); |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 4914 | llvm::Value *AllocArgs[] = {emitUpdateLocation(CGF, Loc), |
| 4915 | getThreadID(CGF, Loc), TaskFlags, |
| 4916 | KmpTaskTWithPrivatesTySize, SharedsSize, |
| 4917 | CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
| 4918 | TaskEntry, KmpRoutineEntryPtrTy)}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4919 | llvm::Value *NewTask = CGF.EmitRuntimeCall( |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 4920 | createRuntimeFunction(OMPRTL__kmpc_omp_task_alloc), AllocArgs); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4921 | llvm::Value *NewTaskNewTaskTTy = |
| 4922 | CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
| 4923 | NewTask, KmpTaskTWithPrivatesPtrTy); |
Alexey Bataev | 8fc69dc | 2015-05-18 07:54:53 +0000 | [diff] [blame] | 4924 | LValue Base = CGF.MakeNaturalAlignAddrLValue(NewTaskNewTaskTTy, |
| 4925 | KmpTaskTWithPrivatesQTy); |
| 4926 | LValue TDBase = |
| 4927 | CGF.EmitLValueForField(Base, *KmpTaskTWithPrivatesQTyRD->field_begin()); |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 4928 | // Fill the data in the resulting kmp_task_t record. |
| 4929 | // Copy shareds if there are any. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4930 | Address KmpTaskSharedsPtr = Address::invalid(); |
Alexey Bataev | 8fc69dc | 2015-05-18 07:54:53 +0000 | [diff] [blame] | 4931 | if (!SharedsTy->getAsStructureType()->getDecl()->field_empty()) { |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 4932 | KmpTaskSharedsPtr = |
| 4933 | Address(CGF.EmitLoadOfScalar( |
| 4934 | CGF.EmitLValueForField( |
| 4935 | TDBase, *std::next(KmpTaskTQTyRD->field_begin(), |
| 4936 | KmpTaskTShareds)), |
| 4937 | Loc), |
| 4938 | CGF.getNaturalTypeAlignment(SharedsTy)); |
Ivan A. Kosarev | 1860b52 | 2018-01-25 14:21:55 +0000 | [diff] [blame] | 4939 | LValue Dest = CGF.MakeAddrLValue(KmpTaskSharedsPtr, SharedsTy); |
| 4940 | LValue Src = CGF.MakeAddrLValue(Shareds, SharedsTy); |
Richard Smith | e78fac5 | 2018-04-05 20:52:58 +0000 | [diff] [blame] | 4941 | CGF.EmitAggregateCopy(Dest, Src, SharedsTy, AggValueSlot::DoesNotOverlap); |
Alexey Bataev | 8fc69dc | 2015-05-18 07:54:53 +0000 | [diff] [blame] | 4942 | } |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 4943 | // Emit initial values for private copies (if any). |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4944 | TaskResultTy Result; |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 4945 | if (!Privates.empty()) { |
Alexey Bataev | 8a83159 | 2016-05-10 10:36:51 +0000 | [diff] [blame] | 4946 | emitPrivatesInit(CGF, D, KmpTaskSharedsPtr, Base, KmpTaskTWithPrivatesQTyRD, |
| 4947 | SharedsTy, SharedsPtrTy, Data, Privates, |
| 4948 | /*ForDup=*/false); |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4949 | if (isOpenMPTaskLoopDirective(D.getDirectiveKind()) && |
| 4950 | (!Data.LastprivateVars.empty() || checkInitIsRequired(CGF, Privates))) { |
| 4951 | Result.TaskDupFn = emitTaskDupFunction( |
| 4952 | CGM, Loc, D, KmpTaskTWithPrivatesPtrQTy, KmpTaskTWithPrivatesQTyRD, |
| 4953 | KmpTaskTQTyRD, SharedsTy, SharedsPtrTy, Data, Privates, |
| 4954 | /*WithLastIter=*/!Data.LastprivateVars.empty()); |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 4955 | } |
| 4956 | } |
Alexey Bataev | ad537bb | 2016-05-30 09:06:50 +0000 | [diff] [blame] | 4957 | // Fields of union "kmp_cmplrdata_t" for destructors and priority. |
| 4958 | enum { Priority = 0, Destructors = 1 }; |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 4959 | // Provide pointer to function with destructors for privates. |
Alexey Bataev | ad537bb | 2016-05-30 09:06:50 +0000 | [diff] [blame] | 4960 | auto FI = std::next(KmpTaskTQTyRD->field_begin(), Data1); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4961 | const RecordDecl *KmpCmplrdataUD = |
| 4962 | (*FI)->getType()->getAsUnionType()->getDecl(); |
Alexey Bataev | ad537bb | 2016-05-30 09:06:50 +0000 | [diff] [blame] | 4963 | if (NeedsCleanup) { |
| 4964 | llvm::Value *DestructorFn = emitDestructorsFunction( |
| 4965 | CGM, Loc, KmpInt32Ty, KmpTaskTWithPrivatesPtrQTy, |
| 4966 | KmpTaskTWithPrivatesQTy); |
| 4967 | LValue Data1LV = CGF.EmitLValueForField(TDBase, *FI); |
| 4968 | LValue DestructorsLV = CGF.EmitLValueForField( |
| 4969 | Data1LV, *std::next(KmpCmplrdataUD->field_begin(), Destructors)); |
| 4970 | CGF.EmitStoreOfScalar(CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
| 4971 | DestructorFn, KmpRoutineEntryPtrTy), |
| 4972 | DestructorsLV); |
| 4973 | } |
| 4974 | // Set priority. |
| 4975 | if (Data.Priority.getInt()) { |
| 4976 | LValue Data2LV = CGF.EmitLValueForField( |
| 4977 | TDBase, *std::next(KmpTaskTQTyRD->field_begin(), Data2)); |
| 4978 | LValue PriorityLV = CGF.EmitLValueForField( |
| 4979 | Data2LV, *std::next(KmpCmplrdataUD->field_begin(), Priority)); |
| 4980 | CGF.EmitStoreOfScalar(Data.Priority.getPointer(), PriorityLV); |
| 4981 | } |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 4982 | Result.NewTask = NewTask; |
| 4983 | Result.TaskEntry = TaskEntry; |
| 4984 | Result.NewTaskNewTaskTTy = NewTaskNewTaskTTy; |
| 4985 | Result.TDBase = TDBase; |
| 4986 | Result.KmpTaskTQTyRD = KmpTaskTQTyRD; |
| 4987 | return Result; |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4988 | } |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 4989 | |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 4990 | void CGOpenMPRuntime::emitTaskCall(CodeGenFunction &CGF, SourceLocation Loc, |
| 4991 | const OMPExecutableDirective &D, |
| 4992 | llvm::Value *TaskFunction, |
| 4993 | QualType SharedsTy, Address Shareds, |
| 4994 | const Expr *IfCond, |
| 4995 | const OMPTaskDataTy &Data) { |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4996 | if (!CGF.HaveInsertPoint()) |
| 4997 | return; |
| 4998 | |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 4999 | TaskResultTy Result = |
| 5000 | emitTaskInit(CGF, Loc, D, TaskFunction, SharedsTy, Shareds, Data); |
| 5001 | llvm::Value *NewTask = Result.NewTask; |
| 5002 | llvm::Value *TaskEntry = Result.TaskEntry; |
| 5003 | llvm::Value *NewTaskNewTaskTTy = Result.NewTaskNewTaskTTy; |
| 5004 | LValue TDBase = Result.TDBase; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5005 | const RecordDecl *KmpTaskTQTyRD = Result.KmpTaskTQTyRD; |
| 5006 | ASTContext &C = CGM.getContext(); |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 5007 | // Process list of dependences. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5008 | Address DependenciesArray = Address::invalid(); |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 5009 | unsigned NumDependencies = Data.Dependences.size(); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5010 | if (NumDependencies) { |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 5011 | // Dependence kind for RTL. |
Alexey Bataev | 92e82f9 | 2015-11-23 13:33:42 +0000 | [diff] [blame] | 5012 | enum RTLDependenceKindTy { DepIn = 0x01, DepInOut = 0x3 }; |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 5013 | enum RTLDependInfoFieldsTy { BaseAddr, Len, Flags }; |
| 5014 | RecordDecl *KmpDependInfoRD; |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5015 | QualType FlagsTy = |
| 5016 | C.getIntTypeForBitwidth(C.getTypeSize(C.BoolTy), /*Signed=*/false); |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 5017 | llvm::Type *LLVMFlagsTy = CGF.ConvertTypeForMem(FlagsTy); |
| 5018 | if (KmpDependInfoTy.isNull()) { |
| 5019 | KmpDependInfoRD = C.buildImplicitRecord("kmp_depend_info"); |
| 5020 | KmpDependInfoRD->startDefinition(); |
| 5021 | addFieldToRecordDecl(C, KmpDependInfoRD, C.getIntPtrType()); |
| 5022 | addFieldToRecordDecl(C, KmpDependInfoRD, C.getSizeType()); |
| 5023 | addFieldToRecordDecl(C, KmpDependInfoRD, FlagsTy); |
| 5024 | KmpDependInfoRD->completeDefinition(); |
| 5025 | KmpDependInfoTy = C.getRecordType(KmpDependInfoRD); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5026 | } else { |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 5027 | KmpDependInfoRD = cast<RecordDecl>(KmpDependInfoTy->getAsTagDecl()); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5028 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5029 | CharUnits DependencySize = C.getTypeSizeInChars(KmpDependInfoTy); |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 5030 | // Define type kmp_depend_info[<Dependences.size()>]; |
| 5031 | QualType KmpDependInfoArrayTy = C.getConstantArrayType( |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5032 | KmpDependInfoTy, llvm::APInt(/*numBits=*/64, NumDependencies), |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 5033 | ArrayType::Normal, /*IndexTypeQuals=*/0); |
| 5034 | // kmp_depend_info[<Dependences.size()>] deps; |
Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 5035 | DependenciesArray = |
| 5036 | CGF.CreateMemTemp(KmpDependInfoArrayTy, ".dep.arr.addr"); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5037 | for (unsigned I = 0; I < NumDependencies; ++I) { |
| 5038 | const Expr *E = Data.Dependences[I].second; |
| 5039 | LValue Addr = CGF.EmitLValue(E); |
Alexey Bataev | d6fdc8b | 2015-08-31 07:32:19 +0000 | [diff] [blame] | 5040 | llvm::Value *Size; |
| 5041 | QualType Ty = E->getType(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5042 | if (const auto *ASE = |
| 5043 | dyn_cast<OMPArraySectionExpr>(E->IgnoreParenImpCasts())) { |
Alexey Bataev | d6fdc8b | 2015-08-31 07:32:19 +0000 | [diff] [blame] | 5044 | LValue UpAddrLVal = |
| 5045 | CGF.EmitOMPArraySectionExpr(ASE, /*LowerBound=*/false); |
| 5046 | llvm::Value *UpAddr = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5047 | CGF.Builder.CreateConstGEP1_32(UpAddrLVal.getPointer(), /*Idx0=*/1); |
Alexey Bataev | d6fdc8b | 2015-08-31 07:32:19 +0000 | [diff] [blame] | 5048 | llvm::Value *LowIntPtr = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5049 | CGF.Builder.CreatePtrToInt(Addr.getPointer(), CGM.SizeTy); |
Alexey Bataev | d6fdc8b | 2015-08-31 07:32:19 +0000 | [diff] [blame] | 5050 | llvm::Value *UpIntPtr = CGF.Builder.CreatePtrToInt(UpAddr, CGM.SizeTy); |
| 5051 | Size = CGF.Builder.CreateNUWSub(UpIntPtr, LowIntPtr); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5052 | } else { |
Alexey Bataev | 1189bd0 | 2016-01-26 12:20:39 +0000 | [diff] [blame] | 5053 | Size = CGF.getTypeSize(Ty); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5054 | } |
| 5055 | LValue Base = CGF.MakeAddrLValue( |
| 5056 | CGF.Builder.CreateConstArrayGEP(DependenciesArray, I, DependencySize), |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 5057 | KmpDependInfoTy); |
| 5058 | // deps[i].base_addr = &<Dependences[i].second>; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5059 | LValue BaseAddrLVal = CGF.EmitLValueForField( |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 5060 | Base, *std::next(KmpDependInfoRD->field_begin(), BaseAddr)); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5061 | CGF.EmitStoreOfScalar( |
| 5062 | CGF.Builder.CreatePtrToInt(Addr.getPointer(), CGF.IntPtrTy), |
| 5063 | BaseAddrLVal); |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 5064 | // deps[i].len = sizeof(<Dependences[i].second>); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5065 | LValue LenLVal = CGF.EmitLValueForField( |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 5066 | Base, *std::next(KmpDependInfoRD->field_begin(), Len)); |
| 5067 | CGF.EmitStoreOfScalar(Size, LenLVal); |
| 5068 | // deps[i].flags = <Dependences[i].first>; |
| 5069 | RTLDependenceKindTy DepKind; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5070 | switch (Data.Dependences[I].first) { |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 5071 | case OMPC_DEPEND_in: |
| 5072 | DepKind = DepIn; |
| 5073 | break; |
Alexey Bataev | 92e82f9 | 2015-11-23 13:33:42 +0000 | [diff] [blame] | 5074 | // Out and InOut dependencies must use the same code. |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 5075 | case OMPC_DEPEND_out: |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 5076 | case OMPC_DEPEND_inout: |
| 5077 | DepKind = DepInOut; |
| 5078 | break; |
Alexey Bataev | eb48235 | 2015-12-18 05:05:56 +0000 | [diff] [blame] | 5079 | case OMPC_DEPEND_source: |
Alexey Bataev | a636c7f | 2015-12-23 10:27:45 +0000 | [diff] [blame] | 5080 | case OMPC_DEPEND_sink: |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 5081 | case OMPC_DEPEND_unknown: |
| 5082 | llvm_unreachable("Unknown task dependence type"); |
| 5083 | } |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5084 | LValue FlagsLVal = CGF.EmitLValueForField( |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 5085 | Base, *std::next(KmpDependInfoRD->field_begin(), Flags)); |
| 5086 | CGF.EmitStoreOfScalar(llvm::ConstantInt::get(LLVMFlagsTy, DepKind), |
| 5087 | FlagsLVal); |
| 5088 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5089 | DependenciesArray = CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
| 5090 | CGF.Builder.CreateStructGEP(DependenciesArray, 0, CharUnits::Zero()), |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 5091 | CGF.VoidPtrTy); |
| 5092 | } |
| 5093 | |
Alexander Kornienko | 2a8c18d | 2018-04-06 15:14:32 +0000 | [diff] [blame] | 5094 | // 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] | 5095 | // libcall. |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 5096 | // Build kmp_int32 __kmpc_omp_task_with_deps(ident_t *, kmp_int32 gtid, |
| 5097 | // kmp_task_t *new_task, kmp_int32 ndeps, kmp_depend_info_t *dep_list, |
| 5098 | // kmp_int32 ndeps_noalias, kmp_depend_info_t *noalias_dep_list) if dependence |
| 5099 | // list is not empty |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5100 | llvm::Value *ThreadID = getThreadID(CGF, Loc); |
| 5101 | llvm::Value *UpLoc = emitUpdateLocation(CGF, Loc); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5102 | llvm::Value *TaskArgs[] = { UpLoc, ThreadID, NewTask }; |
| 5103 | llvm::Value *DepTaskArgs[7]; |
| 5104 | if (NumDependencies) { |
| 5105 | DepTaskArgs[0] = UpLoc; |
| 5106 | DepTaskArgs[1] = ThreadID; |
| 5107 | DepTaskArgs[2] = NewTask; |
| 5108 | DepTaskArgs[3] = CGF.Builder.getInt32(NumDependencies); |
| 5109 | DepTaskArgs[4] = DependenciesArray.getPointer(); |
| 5110 | DepTaskArgs[5] = CGF.Builder.getInt32(0); |
| 5111 | DepTaskArgs[6] = llvm::ConstantPointerNull::get(CGF.VoidPtrTy); |
| 5112 | } |
Malcolm Parsons | c6e4583 | 2017-01-13 18:55:32 +0000 | [diff] [blame] | 5113 | auto &&ThenCodeGen = [this, &Data, TDBase, KmpTaskTQTyRD, NumDependencies, |
| 5114 | &TaskArgs, |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5115 | &DepTaskArgs](CodeGenFunction &CGF, PrePostActionTy &) { |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 5116 | if (!Data.Tied) { |
Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 5117 | auto PartIdFI = std::next(KmpTaskTQTyRD->field_begin(), KmpTaskTPartId); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5118 | LValue PartIdLVal = CGF.EmitLValueForField(TDBase, *PartIdFI); |
Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 5119 | CGF.EmitStoreOfScalar(CGF.Builder.getInt32(0), PartIdLVal); |
| 5120 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5121 | if (NumDependencies) { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5122 | CGF.EmitRuntimeCall( |
Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 5123 | createRuntimeFunction(OMPRTL__kmpc_omp_task_with_deps), DepTaskArgs); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5124 | } else { |
Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 5125 | CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_omp_task), |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5126 | TaskArgs); |
| 5127 | } |
Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 5128 | // Check if parent region is untied and build return for untied task; |
| 5129 | if (auto *Region = |
| 5130 | dyn_cast_or_null<CGOpenMPRegionInfo>(CGF.CapturedStmtInfo)) |
| 5131 | Region->emitUntiedSwitch(CGF); |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 5132 | }; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5133 | |
| 5134 | llvm::Value *DepWaitTaskArgs[6]; |
| 5135 | if (NumDependencies) { |
| 5136 | DepWaitTaskArgs[0] = UpLoc; |
| 5137 | DepWaitTaskArgs[1] = ThreadID; |
| 5138 | DepWaitTaskArgs[2] = CGF.Builder.getInt32(NumDependencies); |
| 5139 | DepWaitTaskArgs[3] = DependenciesArray.getPointer(); |
| 5140 | DepWaitTaskArgs[4] = CGF.Builder.getInt32(0); |
| 5141 | DepWaitTaskArgs[5] = llvm::ConstantPointerNull::get(CGF.VoidPtrTy); |
| 5142 | } |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5143 | auto &&ElseCodeGen = [&TaskArgs, ThreadID, NewTaskNewTaskTTy, TaskEntry, |
Alexey Bataev | 3c595a6 | 2017-08-14 15:01:03 +0000 | [diff] [blame] | 5144 | NumDependencies, &DepWaitTaskArgs, |
| 5145 | Loc](CodeGenFunction &CGF, PrePostActionTy &) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5146 | CGOpenMPRuntime &RT = CGF.CGM.getOpenMPRuntime(); |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 5147 | CodeGenFunction::RunCleanupsScope LocalScope(CGF); |
| 5148 | // Build void __kmpc_omp_wait_deps(ident_t *, kmp_int32 gtid, |
| 5149 | // kmp_int32 ndeps, kmp_depend_info_t *dep_list, kmp_int32 |
| 5150 | // ndeps_noalias, kmp_depend_info_t *noalias_dep_list); if dependence info |
| 5151 | // is specified. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5152 | if (NumDependencies) |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5153 | CGF.EmitRuntimeCall(RT.createRuntimeFunction(OMPRTL__kmpc_omp_wait_deps), |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 5154 | DepWaitTaskArgs); |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5155 | // Call proxy_task_entry(gtid, new_task); |
Alexey Bataev | 3c595a6 | 2017-08-14 15:01:03 +0000 | [diff] [blame] | 5156 | auto &&CodeGen = [TaskEntry, ThreadID, NewTaskNewTaskTTy, |
| 5157 | Loc](CodeGenFunction &CGF, PrePostActionTy &Action) { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5158 | Action.Enter(CGF); |
| 5159 | llvm::Value *OutlinedFnArgs[] = {ThreadID, NewTaskNewTaskTTy}; |
Alexey Bataev | 3c595a6 | 2017-08-14 15:01:03 +0000 | [diff] [blame] | 5160 | CGF.CGM.getOpenMPRuntime().emitOutlinedFunctionCall(CGF, Loc, TaskEntry, |
Alexey Bataev | 2c7eee5 | 2017-08-04 19:10:54 +0000 | [diff] [blame] | 5161 | OutlinedFnArgs); |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5162 | }; |
| 5163 | |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 5164 | // Build void __kmpc_omp_task_begin_if0(ident_t *, kmp_int32 gtid, |
| 5165 | // kmp_task_t *new_task); |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 5166 | // Build void __kmpc_omp_task_complete_if0(ident_t *, kmp_int32 gtid, |
| 5167 | // kmp_task_t *new_task); |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5168 | RegionCodeGenTy RCG(CodeGen); |
| 5169 | CommonActionTy Action( |
| 5170 | RT.createRuntimeFunction(OMPRTL__kmpc_omp_task_begin_if0), TaskArgs, |
| 5171 | RT.createRuntimeFunction(OMPRTL__kmpc_omp_task_complete_if0), TaskArgs); |
| 5172 | RCG.setAction(Action); |
| 5173 | RCG(CGF); |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 5174 | }; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5175 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5176 | if (IfCond) { |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 5177 | emitOMPIfClause(CGF, IfCond, ThenCodeGen, ElseCodeGen); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5178 | } else { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5179 | RegionCodeGenTy ThenRCG(ThenCodeGen); |
| 5180 | ThenRCG(CGF); |
Alexey Bataev | f539faa | 2016-03-28 12:58:34 +0000 | [diff] [blame] | 5181 | } |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 5182 | } |
| 5183 | |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 5184 | void CGOpenMPRuntime::emitTaskLoopCall(CodeGenFunction &CGF, SourceLocation Loc, |
| 5185 | const OMPLoopDirective &D, |
| 5186 | llvm::Value *TaskFunction, |
| 5187 | QualType SharedsTy, Address Shareds, |
| 5188 | const Expr *IfCond, |
| 5189 | const OMPTaskDataTy &Data) { |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 5190 | if (!CGF.HaveInsertPoint()) |
| 5191 | return; |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 5192 | TaskResultTy Result = |
| 5193 | emitTaskInit(CGF, Loc, D, TaskFunction, SharedsTy, Shareds, Data); |
Alexander Kornienko | 2a8c18d | 2018-04-06 15:14:32 +0000 | [diff] [blame] | 5194 | // 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] | 5195 | // libcall. |
| 5196 | // Call to void __kmpc_taskloop(ident_t *loc, int gtid, kmp_task_t *task, int |
| 5197 | // if_val, kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st, int nogroup, int |
| 5198 | // sched, kmp_uint64 grainsize, void *task_dup); |
| 5199 | llvm::Value *ThreadID = getThreadID(CGF, Loc); |
| 5200 | llvm::Value *UpLoc = emitUpdateLocation(CGF, Loc); |
| 5201 | llvm::Value *IfVal; |
| 5202 | if (IfCond) { |
| 5203 | IfVal = CGF.Builder.CreateIntCast(CGF.EvaluateExprAsBool(IfCond), CGF.IntTy, |
| 5204 | /*isSigned=*/true); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5205 | } else { |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 5206 | IfVal = llvm::ConstantInt::getSigned(CGF.IntTy, /*V=*/1); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5207 | } |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 5208 | |
| 5209 | LValue LBLVal = CGF.EmitLValueForField( |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 5210 | Result.TDBase, |
| 5211 | *std::next(Result.KmpTaskTQTyRD->field_begin(), KmpTaskTLowerBound)); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5212 | const auto *LBVar = |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 5213 | cast<VarDecl>(cast<DeclRefExpr>(D.getLowerBoundVariable())->getDecl()); |
| 5214 | CGF.EmitAnyExprToMem(LBVar->getInit(), LBLVal.getAddress(), LBLVal.getQuals(), |
| 5215 | /*IsInitializer=*/true); |
| 5216 | LValue UBLVal = CGF.EmitLValueForField( |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 5217 | Result.TDBase, |
| 5218 | *std::next(Result.KmpTaskTQTyRD->field_begin(), KmpTaskTUpperBound)); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5219 | const auto *UBVar = |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 5220 | cast<VarDecl>(cast<DeclRefExpr>(D.getUpperBoundVariable())->getDecl()); |
| 5221 | CGF.EmitAnyExprToMem(UBVar->getInit(), UBLVal.getAddress(), UBLVal.getQuals(), |
| 5222 | /*IsInitializer=*/true); |
| 5223 | LValue StLVal = CGF.EmitLValueForField( |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 5224 | Result.TDBase, |
| 5225 | *std::next(Result.KmpTaskTQTyRD->field_begin(), KmpTaskTStride)); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5226 | const auto *StVar = |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 5227 | cast<VarDecl>(cast<DeclRefExpr>(D.getStrideVariable())->getDecl()); |
| 5228 | CGF.EmitAnyExprToMem(StVar->getInit(), StLVal.getAddress(), StLVal.getQuals(), |
| 5229 | /*IsInitializer=*/true); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 5230 | // Store reductions address. |
| 5231 | LValue RedLVal = CGF.EmitLValueForField( |
| 5232 | Result.TDBase, |
| 5233 | *std::next(Result.KmpTaskTQTyRD->field_begin(), KmpTaskTReductions)); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5234 | if (Data.Reductions) { |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 5235 | CGF.EmitStoreOfScalar(Data.Reductions, RedLVal); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5236 | } else { |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 5237 | CGF.EmitNullInitialization(RedLVal.getAddress(), |
| 5238 | CGF.getContext().VoidPtrTy); |
| 5239 | } |
Alexey Bataev | 2b19a6f | 2016-04-28 09:15:06 +0000 | [diff] [blame] | 5240 | enum { NoSchedule = 0, Grainsize = 1, NumTasks = 2 }; |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 5241 | llvm::Value *TaskArgs[] = { |
Alexey Bataev | 3344603 | 2017-07-12 18:09:32 +0000 | [diff] [blame] | 5242 | UpLoc, |
| 5243 | ThreadID, |
| 5244 | Result.NewTask, |
| 5245 | IfVal, |
| 5246 | LBLVal.getPointer(), |
| 5247 | UBLVal.getPointer(), |
Alexey Bataev | a9b9cc0 | 2018-01-23 18:12:38 +0000 | [diff] [blame] | 5248 | CGF.EmitLoadOfScalar(StLVal, Loc), |
Alexey Bataev | ac6e4de | 2018-10-24 19:06:37 +0000 | [diff] [blame^] | 5249 | llvm::ConstantInt::getSigned( |
| 5250 | CGF.IntTy, 1), // Always 1 because taskgroup emitted by the compiler |
Alexey Bataev | 2b19a6f | 2016-04-28 09:15:06 +0000 | [diff] [blame] | 5251 | llvm::ConstantInt::getSigned( |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 5252 | CGF.IntTy, Data.Schedule.getPointer() |
| 5253 | ? Data.Schedule.getInt() ? NumTasks : Grainsize |
Alexey Bataev | 2b19a6f | 2016-04-28 09:15:06 +0000 | [diff] [blame] | 5254 | : NoSchedule), |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 5255 | Data.Schedule.getPointer() |
| 5256 | ? CGF.Builder.CreateIntCast(Data.Schedule.getPointer(), CGF.Int64Ty, |
Alexey Bataev | 2b19a6f | 2016-04-28 09:15:06 +0000 | [diff] [blame] | 5257 | /*isSigned=*/false) |
| 5258 | : llvm::ConstantInt::get(CGF.Int64Ty, /*V=*/0), |
Alexey Bataev | 3344603 | 2017-07-12 18:09:32 +0000 | [diff] [blame] | 5259 | Result.TaskDupFn ? CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
| 5260 | Result.TaskDupFn, CGF.VoidPtrTy) |
| 5261 | : llvm::ConstantPointerNull::get(CGF.VoidPtrTy)}; |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 5262 | CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_taskloop), TaskArgs); |
| 5263 | } |
| 5264 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 5265 | /// Emit reduction operation for each element of array (required for |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5266 | /// array sections) LHS op = RHS. |
| 5267 | /// \param Type Type of array. |
| 5268 | /// \param LHSVar Variable on the left side of the reduction operation |
| 5269 | /// (references element of array in original variable). |
| 5270 | /// \param RHSVar Variable on the right side of the reduction operation |
| 5271 | /// (references element of array in original variable). |
| 5272 | /// \param RedOpGen Generator of reduction operation with use of LHSVar and |
| 5273 | /// RHSVar. |
Benjamin Kramer | e003ca2 | 2015-10-28 13:54:16 +0000 | [diff] [blame] | 5274 | static void EmitOMPAggregateReduction( |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5275 | CodeGenFunction &CGF, QualType Type, const VarDecl *LHSVar, |
| 5276 | const VarDecl *RHSVar, |
| 5277 | const llvm::function_ref<void(CodeGenFunction &CGF, const Expr *, |
| 5278 | const Expr *, const Expr *)> &RedOpGen, |
| 5279 | const Expr *XExpr = nullptr, const Expr *EExpr = nullptr, |
| 5280 | const Expr *UpExpr = nullptr) { |
| 5281 | // Perform element-by-element initialization. |
| 5282 | QualType ElementTy; |
| 5283 | Address LHSAddr = CGF.GetAddrOfLocalVar(LHSVar); |
| 5284 | Address RHSAddr = CGF.GetAddrOfLocalVar(RHSVar); |
| 5285 | |
| 5286 | // Drill down to the base element type on both arrays. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5287 | const ArrayType *ArrayTy = Type->getAsArrayTypeUnsafe(); |
| 5288 | llvm::Value *NumElements = CGF.emitArrayLength(ArrayTy, ElementTy, LHSAddr); |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5289 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5290 | llvm::Value *RHSBegin = RHSAddr.getPointer(); |
| 5291 | llvm::Value *LHSBegin = LHSAddr.getPointer(); |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5292 | // Cast from pointer to array type to pointer to single element. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5293 | llvm::Value *LHSEnd = CGF.Builder.CreateGEP(LHSBegin, NumElements); |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5294 | // The basic structure here is a while-do loop. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5295 | llvm::BasicBlock *BodyBB = CGF.createBasicBlock("omp.arraycpy.body"); |
| 5296 | llvm::BasicBlock *DoneBB = CGF.createBasicBlock("omp.arraycpy.done"); |
| 5297 | llvm::Value *IsEmpty = |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5298 | CGF.Builder.CreateICmpEQ(LHSBegin, LHSEnd, "omp.arraycpy.isempty"); |
| 5299 | CGF.Builder.CreateCondBr(IsEmpty, DoneBB, BodyBB); |
| 5300 | |
| 5301 | // Enter the loop body, making that address the current address. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5302 | llvm::BasicBlock *EntryBB = CGF.Builder.GetInsertBlock(); |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5303 | CGF.EmitBlock(BodyBB); |
| 5304 | |
| 5305 | CharUnits ElementSize = CGF.getContext().getTypeSizeInChars(ElementTy); |
| 5306 | |
| 5307 | llvm::PHINode *RHSElementPHI = CGF.Builder.CreatePHI( |
| 5308 | RHSBegin->getType(), 2, "omp.arraycpy.srcElementPast"); |
| 5309 | RHSElementPHI->addIncoming(RHSBegin, EntryBB); |
| 5310 | Address RHSElementCurrent = |
| 5311 | Address(RHSElementPHI, |
| 5312 | RHSAddr.getAlignment().alignmentOfArrayElement(ElementSize)); |
| 5313 | |
| 5314 | llvm::PHINode *LHSElementPHI = CGF.Builder.CreatePHI( |
| 5315 | LHSBegin->getType(), 2, "omp.arraycpy.destElementPast"); |
| 5316 | LHSElementPHI->addIncoming(LHSBegin, EntryBB); |
| 5317 | Address LHSElementCurrent = |
| 5318 | Address(LHSElementPHI, |
| 5319 | LHSAddr.getAlignment().alignmentOfArrayElement(ElementSize)); |
| 5320 | |
| 5321 | // Emit copy. |
| 5322 | CodeGenFunction::OMPPrivateScope Scope(CGF); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5323 | Scope.addPrivate(LHSVar, [=]() { return LHSElementCurrent; }); |
| 5324 | Scope.addPrivate(RHSVar, [=]() { return RHSElementCurrent; }); |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5325 | Scope.Privatize(); |
| 5326 | RedOpGen(CGF, XExpr, EExpr, UpExpr); |
| 5327 | Scope.ForceCleanup(); |
| 5328 | |
| 5329 | // Shift the address forward by one element. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5330 | llvm::Value *LHSElementNext = CGF.Builder.CreateConstGEP1_32( |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5331 | LHSElementPHI, /*Idx0=*/1, "omp.arraycpy.dest.element"); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5332 | llvm::Value *RHSElementNext = CGF.Builder.CreateConstGEP1_32( |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5333 | RHSElementPHI, /*Idx0=*/1, "omp.arraycpy.src.element"); |
| 5334 | // Check whether we've reached the end. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5335 | llvm::Value *Done = |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5336 | CGF.Builder.CreateICmpEQ(LHSElementNext, LHSEnd, "omp.arraycpy.done"); |
| 5337 | CGF.Builder.CreateCondBr(Done, DoneBB, BodyBB); |
| 5338 | LHSElementPHI->addIncoming(LHSElementNext, CGF.Builder.GetInsertBlock()); |
| 5339 | RHSElementPHI->addIncoming(RHSElementNext, CGF.Builder.GetInsertBlock()); |
| 5340 | |
| 5341 | // Done. |
| 5342 | CGF.EmitBlock(DoneBB, /*IsFinished=*/true); |
| 5343 | } |
| 5344 | |
Alexey Bataev | a839ddd | 2016-03-17 10:19:46 +0000 | [diff] [blame] | 5345 | /// Emit reduction combiner. If the combiner is a simple expression emit it as |
| 5346 | /// is, otherwise consider it as combiner of UDR decl and emit it as a call of |
| 5347 | /// UDR combiner function. |
| 5348 | static void emitReductionCombiner(CodeGenFunction &CGF, |
| 5349 | const Expr *ReductionOp) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5350 | if (const auto *CE = dyn_cast<CallExpr>(ReductionOp)) |
| 5351 | if (const auto *OVE = dyn_cast<OpaqueValueExpr>(CE->getCallee())) |
| 5352 | if (const auto *DRE = |
Alexey Bataev | a839ddd | 2016-03-17 10:19:46 +0000 | [diff] [blame] | 5353 | dyn_cast<DeclRefExpr>(OVE->getSourceExpr()->IgnoreImpCasts())) |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5354 | if (const auto *DRD = |
| 5355 | dyn_cast<OMPDeclareReductionDecl>(DRE->getDecl())) { |
Alexey Bataev | a839ddd | 2016-03-17 10:19:46 +0000 | [diff] [blame] | 5356 | std::pair<llvm::Function *, llvm::Function *> Reduction = |
| 5357 | CGF.CGM.getOpenMPRuntime().getUserDefinedReduction(DRD); |
| 5358 | RValue Func = RValue::get(Reduction.first); |
| 5359 | CodeGenFunction::OpaqueValueMapping Map(CGF, OVE, Func); |
| 5360 | CGF.EmitIgnoredExpr(ReductionOp); |
| 5361 | return; |
| 5362 | } |
| 5363 | CGF.EmitIgnoredExpr(ReductionOp); |
| 5364 | } |
| 5365 | |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 5366 | llvm::Value *CGOpenMPRuntime::emitReductionFunction( |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 5367 | CodeGenModule &CGM, SourceLocation Loc, llvm::Type *ArgsType, |
| 5368 | ArrayRef<const Expr *> Privates, ArrayRef<const Expr *> LHSExprs, |
| 5369 | ArrayRef<const Expr *> RHSExprs, ArrayRef<const Expr *> ReductionOps) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5370 | ASTContext &C = CGM.getContext(); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5371 | |
| 5372 | // void reduction_func(void *LHSArg, void *RHSArg); |
| 5373 | FunctionArgList Args; |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 5374 | ImplicitParamDecl LHSArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, C.VoidPtrTy, |
| 5375 | ImplicitParamDecl::Other); |
| 5376 | ImplicitParamDecl RHSArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, C.VoidPtrTy, |
| 5377 | ImplicitParamDecl::Other); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5378 | Args.push_back(&LHSArg); |
| 5379 | Args.push_back(&RHSArg); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5380 | const auto &CGFI = |
| 5381 | CGM.getTypes().arrangeBuiltinFunctionDeclaration(C.VoidTy, Args); |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 5382 | std::string Name = getName({"omp", "reduction", "reduction_func"}); |
| 5383 | auto *Fn = llvm::Function::Create(CGM.getTypes().GetFunctionType(CGFI), |
| 5384 | llvm::GlobalValue::InternalLinkage, Name, |
| 5385 | &CGM.getModule()); |
Rafael Espindola | 51ec5a9 | 2018-02-28 23:46:35 +0000 | [diff] [blame] | 5386 | CGM.SetInternalFunctionAttributes(GlobalDecl(), Fn, CGFI); |
Alexey Bataev | c0f879b | 2018-04-10 20:10:53 +0000 | [diff] [blame] | 5387 | Fn->setDoesNotRecurse(); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5388 | CodeGenFunction CGF(CGM); |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 5389 | CGF.StartFunction(GlobalDecl(), C.VoidTy, Fn, CGFI, Args, Loc, Loc); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5390 | |
| 5391 | // Dst = (void*[n])(LHSArg); |
| 5392 | // Src = (void*[n])(RHSArg); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5393 | Address LHS(CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
| 5394 | CGF.Builder.CreateLoad(CGF.GetAddrOfLocalVar(&LHSArg)), |
| 5395 | ArgsType), CGF.getPointerAlign()); |
| 5396 | Address RHS(CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
| 5397 | CGF.Builder.CreateLoad(CGF.GetAddrOfLocalVar(&RHSArg)), |
| 5398 | ArgsType), CGF.getPointerAlign()); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5399 | |
| 5400 | // ... |
| 5401 | // *(Type<i>*)lhs[i] = RedOp<i>(*(Type<i>*)lhs[i], *(Type<i>*)rhs[i]); |
| 5402 | // ... |
| 5403 | CodeGenFunction::OMPPrivateScope Scope(CGF); |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5404 | auto IPriv = Privates.begin(); |
| 5405 | unsigned Idx = 0; |
| 5406 | for (unsigned I = 0, E = ReductionOps.size(); I < E; ++I, ++IPriv, ++Idx) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5407 | const auto *RHSVar = |
| 5408 | cast<VarDecl>(cast<DeclRefExpr>(RHSExprs[I])->getDecl()); |
| 5409 | Scope.addPrivate(RHSVar, [&CGF, RHS, Idx, RHSVar]() { |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5410 | return emitAddrOfVarFromArray(CGF, RHS, Idx, RHSVar); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5411 | }); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5412 | const auto *LHSVar = |
| 5413 | cast<VarDecl>(cast<DeclRefExpr>(LHSExprs[I])->getDecl()); |
| 5414 | Scope.addPrivate(LHSVar, [&CGF, LHS, Idx, LHSVar]() { |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5415 | return emitAddrOfVarFromArray(CGF, LHS, Idx, LHSVar); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5416 | }); |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5417 | QualType PrivTy = (*IPriv)->getType(); |
Alexey Bataev | 1189bd0 | 2016-01-26 12:20:39 +0000 | [diff] [blame] | 5418 | if (PrivTy->isVariablyModifiedType()) { |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5419 | // Get array size and emit VLA type. |
| 5420 | ++Idx; |
| 5421 | Address Elem = |
| 5422 | CGF.Builder.CreateConstArrayGEP(LHS, Idx, CGF.getPointerSize()); |
| 5423 | llvm::Value *Ptr = CGF.Builder.CreateLoad(Elem); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5424 | const VariableArrayType *VLA = |
| 5425 | CGF.getContext().getAsVariableArrayType(PrivTy); |
| 5426 | const auto *OVE = cast<OpaqueValueExpr>(VLA->getSizeExpr()); |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5427 | CodeGenFunction::OpaqueValueMapping OpaqueMap( |
Alexey Bataev | 1189bd0 | 2016-01-26 12:20:39 +0000 | [diff] [blame] | 5428 | CGF, OVE, RValue::get(CGF.Builder.CreatePtrToInt(Ptr, CGF.SizeTy))); |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5429 | CGF.EmitVariablyModifiedType(PrivTy); |
| 5430 | } |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5431 | } |
| 5432 | Scope.Privatize(); |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5433 | IPriv = Privates.begin(); |
| 5434 | auto ILHS = LHSExprs.begin(); |
| 5435 | auto IRHS = RHSExprs.begin(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5436 | for (const Expr *E : ReductionOps) { |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5437 | if ((*IPriv)->getType()->isArrayType()) { |
| 5438 | // Emit reduction for array section. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5439 | const auto *LHSVar = cast<VarDecl>(cast<DeclRefExpr>(*ILHS)->getDecl()); |
| 5440 | const auto *RHSVar = cast<VarDecl>(cast<DeclRefExpr>(*IRHS)->getDecl()); |
Alexey Bataev | a839ddd | 2016-03-17 10:19:46 +0000 | [diff] [blame] | 5441 | EmitOMPAggregateReduction( |
| 5442 | CGF, (*IPriv)->getType(), LHSVar, RHSVar, |
| 5443 | [=](CodeGenFunction &CGF, const Expr *, const Expr *, const Expr *) { |
| 5444 | emitReductionCombiner(CGF, E); |
| 5445 | }); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5446 | } else { |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5447 | // Emit reduction for array subscript or single variable. |
Alexey Bataev | a839ddd | 2016-03-17 10:19:46 +0000 | [diff] [blame] | 5448 | emitReductionCombiner(CGF, E); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5449 | } |
Richard Trieu | cc3949d | 2016-02-18 22:34:54 +0000 | [diff] [blame] | 5450 | ++IPriv; |
| 5451 | ++ILHS; |
| 5452 | ++IRHS; |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5453 | } |
| 5454 | Scope.ForceCleanup(); |
| 5455 | CGF.FinishFunction(); |
| 5456 | return Fn; |
| 5457 | } |
| 5458 | |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 5459 | void CGOpenMPRuntime::emitSingleReductionCombiner(CodeGenFunction &CGF, |
| 5460 | const Expr *ReductionOp, |
| 5461 | const Expr *PrivateRef, |
| 5462 | const DeclRefExpr *LHS, |
| 5463 | const DeclRefExpr *RHS) { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5464 | if (PrivateRef->getType()->isArrayType()) { |
| 5465 | // Emit reduction for array section. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5466 | const auto *LHSVar = cast<VarDecl>(LHS->getDecl()); |
| 5467 | const auto *RHSVar = cast<VarDecl>(RHS->getDecl()); |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5468 | EmitOMPAggregateReduction( |
| 5469 | CGF, PrivateRef->getType(), LHSVar, RHSVar, |
| 5470 | [=](CodeGenFunction &CGF, const Expr *, const Expr *, const Expr *) { |
| 5471 | emitReductionCombiner(CGF, ReductionOp); |
| 5472 | }); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5473 | } else { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5474 | // Emit reduction for array subscript or single variable. |
| 5475 | emitReductionCombiner(CGF, ReductionOp); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5476 | } |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5477 | } |
| 5478 | |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5479 | void CGOpenMPRuntime::emitReduction(CodeGenFunction &CGF, SourceLocation Loc, |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5480 | ArrayRef<const Expr *> Privates, |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5481 | ArrayRef<const Expr *> LHSExprs, |
| 5482 | ArrayRef<const Expr *> RHSExprs, |
| 5483 | ArrayRef<const Expr *> ReductionOps, |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 5484 | ReductionOptionsTy Options) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 5485 | if (!CGF.HaveInsertPoint()) |
| 5486 | return; |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 5487 | |
| 5488 | bool WithNowait = Options.WithNowait; |
| 5489 | bool SimpleReduction = Options.SimpleReduction; |
| 5490 | |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5491 | // Next code should be emitted for reduction: |
| 5492 | // |
| 5493 | // static kmp_critical_name lock = { 0 }; |
| 5494 | // |
| 5495 | // void reduce_func(void *lhs[<n>], void *rhs[<n>]) { |
| 5496 | // *(Type0*)lhs[0] = ReductionOperation0(*(Type0*)lhs[0], *(Type0*)rhs[0]); |
| 5497 | // ... |
| 5498 | // *(Type<n>-1*)lhs[<n>-1] = ReductionOperation<n>-1(*(Type<n>-1*)lhs[<n>-1], |
| 5499 | // *(Type<n>-1*)rhs[<n>-1]); |
| 5500 | // } |
| 5501 | // |
| 5502 | // ... |
| 5503 | // void *RedList[<n>] = {&<RHSExprs>[0], ..., &<RHSExprs>[<n>-1]}; |
| 5504 | // switch (__kmpc_reduce{_nowait}(<loc>, <gtid>, <n>, sizeof(RedList), |
| 5505 | // RedList, reduce_func, &<lock>)) { |
| 5506 | // case 1: |
| 5507 | // ... |
| 5508 | // <LHSExprs>[i] = RedOp<i>(*<LHSExprs>[i], *<RHSExprs>[i]); |
| 5509 | // ... |
| 5510 | // __kmpc_end_reduce{_nowait}(<loc>, <gtid>, &<lock>); |
| 5511 | // break; |
| 5512 | // case 2: |
| 5513 | // ... |
| 5514 | // Atomic(<LHSExprs>[i] = RedOp<i>(*<LHSExprs>[i], *<RHSExprs>[i])); |
| 5515 | // ... |
Alexey Bataev | 69a4779 | 2015-05-07 03:54:03 +0000 | [diff] [blame] | 5516 | // [__kmpc_end_reduce(<loc>, <gtid>, &<lock>);] |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5517 | // break; |
| 5518 | // default:; |
| 5519 | // } |
Alexey Bataev | 89e7e8e | 2015-06-17 06:21:39 +0000 | [diff] [blame] | 5520 | // |
| 5521 | // if SimpleReduction is true, only the next code is generated: |
| 5522 | // ... |
| 5523 | // <LHSExprs>[i] = RedOp<i>(*<LHSExprs>[i], *<RHSExprs>[i]); |
| 5524 | // ... |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5525 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5526 | ASTContext &C = CGM.getContext(); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5527 | |
Alexey Bataev | 89e7e8e | 2015-06-17 06:21:39 +0000 | [diff] [blame] | 5528 | if (SimpleReduction) { |
| 5529 | CodeGenFunction::RunCleanupsScope Scope(CGF); |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5530 | auto IPriv = Privates.begin(); |
| 5531 | auto ILHS = LHSExprs.begin(); |
| 5532 | auto IRHS = RHSExprs.begin(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5533 | for (const Expr *E : ReductionOps) { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5534 | emitSingleReductionCombiner(CGF, E, *IPriv, cast<DeclRefExpr>(*ILHS), |
| 5535 | cast<DeclRefExpr>(*IRHS)); |
Richard Trieu | cc3949d | 2016-02-18 22:34:54 +0000 | [diff] [blame] | 5536 | ++IPriv; |
| 5537 | ++ILHS; |
| 5538 | ++IRHS; |
Alexey Bataev | 89e7e8e | 2015-06-17 06:21:39 +0000 | [diff] [blame] | 5539 | } |
| 5540 | return; |
| 5541 | } |
| 5542 | |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5543 | // 1. Build a list of reduction variables. |
| 5544 | // void *RedList[<n>] = {<ReductionVars>[0], ..., <ReductionVars>[<n>-1]}; |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5545 | auto Size = RHSExprs.size(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5546 | for (const Expr *E : Privates) { |
Alexey Bataev | 1189bd0 | 2016-01-26 12:20:39 +0000 | [diff] [blame] | 5547 | if (E->getType()->isVariablyModifiedType()) |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5548 | // Reserve place for array size. |
| 5549 | ++Size; |
| 5550 | } |
| 5551 | llvm::APInt ArraySize(/*unsigned int numBits=*/32, Size); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5552 | QualType ReductionArrayTy = |
| 5553 | C.getConstantArrayType(C.VoidPtrTy, ArraySize, ArrayType::Normal, |
| 5554 | /*IndexTypeQuals=*/0); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5555 | Address ReductionList = |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5556 | CGF.CreateMemTemp(ReductionArrayTy, ".omp.reduction.red_list"); |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5557 | auto IPriv = Privates.begin(); |
| 5558 | unsigned Idx = 0; |
| 5559 | for (unsigned I = 0, E = RHSExprs.size(); I < E; ++I, ++IPriv, ++Idx) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5560 | Address Elem = |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5561 | CGF.Builder.CreateConstArrayGEP(ReductionList, Idx, CGF.getPointerSize()); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5562 | CGF.Builder.CreateStore( |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5563 | CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5564 | CGF.EmitLValue(RHSExprs[I]).getPointer(), CGF.VoidPtrTy), |
| 5565 | Elem); |
Alexey Bataev | 1189bd0 | 2016-01-26 12:20:39 +0000 | [diff] [blame] | 5566 | if ((*IPriv)->getType()->isVariablyModifiedType()) { |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5567 | // Store array size. |
| 5568 | ++Idx; |
| 5569 | Elem = CGF.Builder.CreateConstArrayGEP(ReductionList, Idx, |
| 5570 | CGF.getPointerSize()); |
Alexey Bataev | 1189bd0 | 2016-01-26 12:20:39 +0000 | [diff] [blame] | 5571 | llvm::Value *Size = CGF.Builder.CreateIntCast( |
| 5572 | CGF.getVLASize( |
| 5573 | CGF.getContext().getAsVariableArrayType((*IPriv)->getType())) |
Sander de Smalen | 891af03a | 2018-02-03 13:55:59 +0000 | [diff] [blame] | 5574 | .NumElts, |
Alexey Bataev | 1189bd0 | 2016-01-26 12:20:39 +0000 | [diff] [blame] | 5575 | CGF.SizeTy, /*isSigned=*/false); |
| 5576 | CGF.Builder.CreateStore(CGF.Builder.CreateIntToPtr(Size, CGF.VoidPtrTy), |
| 5577 | Elem); |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5578 | } |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5579 | } |
| 5580 | |
| 5581 | // 2. Emit reduce_func(). |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5582 | llvm::Value *ReductionFn = emitReductionFunction( |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 5583 | CGM, Loc, CGF.ConvertTypeForMem(ReductionArrayTy)->getPointerTo(), |
| 5584 | Privates, LHSExprs, RHSExprs, ReductionOps); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5585 | |
| 5586 | // 3. Create static kmp_critical_name lock = { 0 }; |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 5587 | std::string Name = getName({"reduction"}); |
| 5588 | llvm::Value *Lock = getCriticalRegionLock(Name); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5589 | |
| 5590 | // 4. Build res = __kmpc_reduce{_nowait}(<loc>, <gtid>, <n>, sizeof(RedList), |
| 5591 | // RedList, reduce_func, &<lock>); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5592 | llvm::Value *IdentTLoc = emitUpdateLocation(CGF, Loc, OMP_ATOMIC_REDUCE); |
| 5593 | llvm::Value *ThreadId = getThreadID(CGF, Loc); |
| 5594 | llvm::Value *ReductionArrayTySize = CGF.getTypeSize(ReductionArrayTy); |
| 5595 | llvm::Value *RL = CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
Samuel Antao | 4c8035b | 2016-12-12 18:00:20 +0000 | [diff] [blame] | 5596 | ReductionList.getPointer(), CGF.VoidPtrTy); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5597 | llvm::Value *Args[] = { |
| 5598 | IdentTLoc, // ident_t *<loc> |
| 5599 | ThreadId, // i32 <gtid> |
| 5600 | CGF.Builder.getInt32(RHSExprs.size()), // i32 <n> |
| 5601 | ReductionArrayTySize, // size_type sizeof(RedList) |
| 5602 | RL, // void *RedList |
| 5603 | ReductionFn, // void (*) (void *, void *) <reduce_func> |
| 5604 | Lock // kmp_critical_name *&<lock> |
| 5605 | }; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5606 | llvm::Value *Res = CGF.EmitRuntimeCall( |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5607 | createRuntimeFunction(WithNowait ? OMPRTL__kmpc_reduce_nowait |
| 5608 | : OMPRTL__kmpc_reduce), |
| 5609 | Args); |
| 5610 | |
| 5611 | // 5. Build switch(res) |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5612 | llvm::BasicBlock *DefaultBB = CGF.createBasicBlock(".omp.reduction.default"); |
| 5613 | llvm::SwitchInst *SwInst = |
| 5614 | CGF.Builder.CreateSwitch(Res, DefaultBB, /*NumCases=*/2); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5615 | |
| 5616 | // 6. Build case 1: |
| 5617 | // ... |
| 5618 | // <LHSExprs>[i] = RedOp<i>(*<LHSExprs>[i], *<RHSExprs>[i]); |
| 5619 | // ... |
| 5620 | // __kmpc_end_reduce{_nowait}(<loc>, <gtid>, &<lock>); |
| 5621 | // break; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5622 | llvm::BasicBlock *Case1BB = CGF.createBasicBlock(".omp.reduction.case1"); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5623 | SwInst->addCase(CGF.Builder.getInt32(1), Case1BB); |
| 5624 | CGF.EmitBlock(Case1BB); |
| 5625 | |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5626 | // Add emission of __kmpc_end_reduce{_nowait}(<loc>, <gtid>, &<lock>); |
| 5627 | llvm::Value *EndArgs[] = { |
| 5628 | IdentTLoc, // ident_t *<loc> |
| 5629 | ThreadId, // i32 <gtid> |
| 5630 | Lock // kmp_critical_name *&<lock> |
| 5631 | }; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5632 | auto &&CodeGen = [Privates, LHSExprs, RHSExprs, ReductionOps]( |
| 5633 | CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 5634 | CGOpenMPRuntime &RT = CGF.CGM.getOpenMPRuntime(); |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5635 | auto IPriv = Privates.begin(); |
| 5636 | auto ILHS = LHSExprs.begin(); |
| 5637 | auto IRHS = RHSExprs.begin(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5638 | for (const Expr *E : ReductionOps) { |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 5639 | RT.emitSingleReductionCombiner(CGF, E, *IPriv, cast<DeclRefExpr>(*ILHS), |
| 5640 | cast<DeclRefExpr>(*IRHS)); |
Richard Trieu | cc3949d | 2016-02-18 22:34:54 +0000 | [diff] [blame] | 5641 | ++IPriv; |
| 5642 | ++ILHS; |
| 5643 | ++IRHS; |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5644 | } |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5645 | }; |
| 5646 | RegionCodeGenTy RCG(CodeGen); |
| 5647 | CommonActionTy Action( |
| 5648 | nullptr, llvm::None, |
| 5649 | createRuntimeFunction(WithNowait ? OMPRTL__kmpc_end_reduce_nowait |
| 5650 | : OMPRTL__kmpc_end_reduce), |
| 5651 | EndArgs); |
| 5652 | RCG.setAction(Action); |
| 5653 | RCG(CGF); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5654 | |
| 5655 | CGF.EmitBranch(DefaultBB); |
| 5656 | |
| 5657 | // 7. Build case 2: |
| 5658 | // ... |
| 5659 | // Atomic(<LHSExprs>[i] = RedOp<i>(*<LHSExprs>[i], *<RHSExprs>[i])); |
| 5660 | // ... |
| 5661 | // break; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5662 | llvm::BasicBlock *Case2BB = CGF.createBasicBlock(".omp.reduction.case2"); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5663 | SwInst->addCase(CGF.Builder.getInt32(2), Case2BB); |
| 5664 | CGF.EmitBlock(Case2BB); |
| 5665 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5666 | auto &&AtomicCodeGen = [Loc, Privates, LHSExprs, RHSExprs, ReductionOps]( |
| 5667 | CodeGenFunction &CGF, PrePostActionTy &Action) { |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5668 | auto ILHS = LHSExprs.begin(); |
| 5669 | auto IRHS = RHSExprs.begin(); |
| 5670 | auto IPriv = Privates.begin(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5671 | for (const Expr *E : ReductionOps) { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5672 | const Expr *XExpr = nullptr; |
| 5673 | const Expr *EExpr = nullptr; |
| 5674 | const Expr *UpExpr = nullptr; |
| 5675 | BinaryOperatorKind BO = BO_Comma; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5676 | if (const auto *BO = dyn_cast<BinaryOperator>(E)) { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5677 | if (BO->getOpcode() == BO_Assign) { |
| 5678 | XExpr = BO->getLHS(); |
| 5679 | UpExpr = BO->getRHS(); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5680 | } |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5681 | } |
| 5682 | // Try to emit update expression as a simple atomic. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5683 | const Expr *RHSExpr = UpExpr; |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5684 | if (RHSExpr) { |
| 5685 | // Analyze RHS part of the whole expression. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5686 | if (const auto *ACO = dyn_cast<AbstractConditionalOperator>( |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5687 | RHSExpr->IgnoreParenImpCasts())) { |
| 5688 | // If this is a conditional operator, analyze its condition for |
| 5689 | // min/max reduction operator. |
| 5690 | RHSExpr = ACO->getCond(); |
Alexey Bataev | 69a4779 | 2015-05-07 03:54:03 +0000 | [diff] [blame] | 5691 | } |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5692 | if (const auto *BORHS = |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5693 | dyn_cast<BinaryOperator>(RHSExpr->IgnoreParenImpCasts())) { |
| 5694 | EExpr = BORHS->getRHS(); |
| 5695 | BO = BORHS->getOpcode(); |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5696 | } |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5697 | } |
| 5698 | if (XExpr) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5699 | const auto *VD = cast<VarDecl>(cast<DeclRefExpr>(*ILHS)->getDecl()); |
Malcolm Parsons | c6e4583 | 2017-01-13 18:55:32 +0000 | [diff] [blame] | 5700 | auto &&AtomicRedGen = [BO, VD, |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5701 | Loc](CodeGenFunction &CGF, const Expr *XExpr, |
| 5702 | const Expr *EExpr, const Expr *UpExpr) { |
| 5703 | LValue X = CGF.EmitLValue(XExpr); |
| 5704 | RValue E; |
| 5705 | if (EExpr) |
| 5706 | E = CGF.EmitAnyExpr(EExpr); |
| 5707 | CGF.EmitOMPAtomicSimpleUpdateExpr( |
JF Bastien | 92f4ef1 | 2016-04-06 17:26:42 +0000 | [diff] [blame] | 5708 | X, E, BO, /*IsXLHSInRHSPart=*/true, |
| 5709 | llvm::AtomicOrdering::Monotonic, Loc, |
Malcolm Parsons | c6e4583 | 2017-01-13 18:55:32 +0000 | [diff] [blame] | 5710 | [&CGF, UpExpr, VD, Loc](RValue XRValue) { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5711 | CodeGenFunction::OMPPrivateScope PrivateScope(CGF); |
| 5712 | PrivateScope.addPrivate( |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5713 | VD, [&CGF, VD, XRValue, Loc]() { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5714 | Address LHSTemp = CGF.CreateMemTemp(VD->getType()); |
| 5715 | CGF.emitOMPSimpleStore( |
| 5716 | CGF.MakeAddrLValue(LHSTemp, VD->getType()), XRValue, |
| 5717 | VD->getType().getNonReferenceType(), Loc); |
| 5718 | return LHSTemp; |
| 5719 | }); |
| 5720 | (void)PrivateScope.Privatize(); |
| 5721 | return CGF.EmitAnyExpr(UpExpr); |
| 5722 | }); |
| 5723 | }; |
| 5724 | if ((*IPriv)->getType()->isArrayType()) { |
| 5725 | // Emit atomic reduction for array section. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5726 | const auto *RHSVar = |
| 5727 | cast<VarDecl>(cast<DeclRefExpr>(*IRHS)->getDecl()); |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5728 | EmitOMPAggregateReduction(CGF, (*IPriv)->getType(), VD, RHSVar, |
| 5729 | AtomicRedGen, XExpr, EExpr, UpExpr); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5730 | } else { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5731 | // Emit atomic reduction for array subscript or single variable. |
| 5732 | AtomicRedGen(CGF, XExpr, EExpr, UpExpr); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5733 | } |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5734 | } else { |
| 5735 | // Emit as a critical region. |
| 5736 | auto &&CritRedGen = [E, Loc](CodeGenFunction &CGF, const Expr *, |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 5737 | const Expr *, const Expr *) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5738 | CGOpenMPRuntime &RT = CGF.CGM.getOpenMPRuntime(); |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 5739 | std::string Name = RT.getName({"atomic_reduction"}); |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5740 | RT.emitCriticalRegion( |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 5741 | CGF, Name, |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5742 | [=](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 5743 | Action.Enter(CGF); |
| 5744 | emitReductionCombiner(CGF, E); |
| 5745 | }, |
| 5746 | Loc); |
| 5747 | }; |
| 5748 | if ((*IPriv)->getType()->isArrayType()) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5749 | const auto *LHSVar = |
| 5750 | cast<VarDecl>(cast<DeclRefExpr>(*ILHS)->getDecl()); |
| 5751 | const auto *RHSVar = |
| 5752 | cast<VarDecl>(cast<DeclRefExpr>(*IRHS)->getDecl()); |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5753 | EmitOMPAggregateReduction(CGF, (*IPriv)->getType(), LHSVar, RHSVar, |
| 5754 | CritRedGen); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5755 | } else { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5756 | CritRedGen(CGF, nullptr, nullptr, nullptr); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5757 | } |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5758 | } |
Richard Trieu | cc3949d | 2016-02-18 22:34:54 +0000 | [diff] [blame] | 5759 | ++ILHS; |
| 5760 | ++IRHS; |
| 5761 | ++IPriv; |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5762 | } |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5763 | }; |
| 5764 | RegionCodeGenTy AtomicRCG(AtomicCodeGen); |
| 5765 | if (!WithNowait) { |
| 5766 | // Add emission of __kmpc_end_reduce(<loc>, <gtid>, &<lock>); |
| 5767 | llvm::Value *EndArgs[] = { |
| 5768 | IdentTLoc, // ident_t *<loc> |
| 5769 | ThreadId, // i32 <gtid> |
| 5770 | Lock // kmp_critical_name *&<lock> |
| 5771 | }; |
| 5772 | CommonActionTy Action(nullptr, llvm::None, |
| 5773 | createRuntimeFunction(OMPRTL__kmpc_end_reduce), |
| 5774 | EndArgs); |
| 5775 | AtomicRCG.setAction(Action); |
| 5776 | AtomicRCG(CGF); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5777 | } else { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5778 | AtomicRCG(CGF); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5779 | } |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5780 | |
| 5781 | CGF.EmitBranch(DefaultBB); |
| 5782 | CGF.EmitBlock(DefaultBB, /*IsFinished=*/true); |
| 5783 | } |
| 5784 | |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 5785 | /// Generates unique name for artificial threadprivate variables. |
Alexey Bataev | 1c44e15 | 2018-03-06 18:59:43 +0000 | [diff] [blame] | 5786 | /// Format is: <Prefix> "." <Decl_mangled_name> "_" "<Decl_start_loc_raw_enc>" |
| 5787 | static std::string generateUniqueName(CodeGenModule &CGM, StringRef Prefix, |
| 5788 | const Expr *Ref) { |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 5789 | SmallString<256> Buffer; |
| 5790 | llvm::raw_svector_ostream Out(Buffer); |
Alexey Bataev | 1c44e15 | 2018-03-06 18:59:43 +0000 | [diff] [blame] | 5791 | const clang::DeclRefExpr *DE; |
| 5792 | const VarDecl *D = ::getBaseDecl(Ref, DE); |
| 5793 | if (!D) |
| 5794 | D = cast<VarDecl>(cast<DeclRefExpr>(Ref)->getDecl()); |
| 5795 | D = D->getCanonicalDecl(); |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 5796 | std::string Name = CGM.getOpenMPRuntime().getName( |
| 5797 | {D->isLocalVarDeclOrParm() ? D->getName() : CGM.getMangledName(D)}); |
| 5798 | Out << Prefix << Name << "_" |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 5799 | << D->getCanonicalDecl()->getBeginLoc().getRawEncoding(); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 5800 | return Out.str(); |
| 5801 | } |
| 5802 | |
| 5803 | /// Emits reduction initializer function: |
| 5804 | /// \code |
| 5805 | /// void @.red_init(void* %arg) { |
| 5806 | /// %0 = bitcast void* %arg to <type>* |
| 5807 | /// store <type> <init>, <type>* %0 |
| 5808 | /// ret void |
| 5809 | /// } |
| 5810 | /// \endcode |
| 5811 | static llvm::Value *emitReduceInitFunction(CodeGenModule &CGM, |
| 5812 | SourceLocation Loc, |
| 5813 | ReductionCodeGen &RCG, unsigned N) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5814 | ASTContext &C = CGM.getContext(); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 5815 | FunctionArgList Args; |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 5816 | ImplicitParamDecl Param(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, C.VoidPtrTy, |
| 5817 | ImplicitParamDecl::Other); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 5818 | Args.emplace_back(&Param); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5819 | const auto &FnInfo = |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 5820 | CGM.getTypes().arrangeBuiltinFunctionDeclaration(C.VoidTy, Args); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5821 | llvm::FunctionType *FnTy = CGM.getTypes().GetFunctionType(FnInfo); |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 5822 | std::string Name = CGM.getOpenMPRuntime().getName({"red_init", ""}); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 5823 | auto *Fn = llvm::Function::Create(FnTy, llvm::GlobalValue::InternalLinkage, |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 5824 | Name, &CGM.getModule()); |
Rafael Espindola | 51ec5a9 | 2018-02-28 23:46:35 +0000 | [diff] [blame] | 5825 | CGM.SetInternalFunctionAttributes(GlobalDecl(), Fn, FnInfo); |
Alexey Bataev | c0f879b | 2018-04-10 20:10:53 +0000 | [diff] [blame] | 5826 | Fn->setDoesNotRecurse(); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 5827 | CodeGenFunction CGF(CGM); |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 5828 | CGF.StartFunction(GlobalDecl(), C.VoidTy, Fn, FnInfo, Args, Loc, Loc); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 5829 | Address PrivateAddr = CGF.EmitLoadOfPointer( |
| 5830 | CGF.GetAddrOfLocalVar(&Param), |
| 5831 | C.getPointerType(C.VoidPtrTy).castAs<PointerType>()); |
| 5832 | llvm::Value *Size = nullptr; |
| 5833 | // If the size of the reduction item is non-constant, load it from global |
| 5834 | // threadprivate variable. |
| 5835 | if (RCG.getSizes(N).second) { |
| 5836 | Address SizeAddr = CGM.getOpenMPRuntime().getAddrOfArtificialThreadPrivate( |
| 5837 | CGF, CGM.getContext().getSizeType(), |
Alexey Bataev | 1c44e15 | 2018-03-06 18:59:43 +0000 | [diff] [blame] | 5838 | generateUniqueName(CGM, "reduction_size", RCG.getRefExpr(N))); |
Alexey Bataev | a9b9cc0 | 2018-01-23 18:12:38 +0000 | [diff] [blame] | 5839 | Size = CGF.EmitLoadOfScalar(SizeAddr, /*Volatile=*/false, |
| 5840 | CGM.getContext().getSizeType(), Loc); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 5841 | } |
| 5842 | RCG.emitAggregateType(CGF, N, Size); |
| 5843 | LValue SharedLVal; |
| 5844 | // If initializer uses initializer from declare reduction construct, emit a |
| 5845 | // pointer to the address of the original reduction item (reuired by reduction |
| 5846 | // initializer) |
| 5847 | if (RCG.usesReductionInitializer(N)) { |
| 5848 | Address SharedAddr = |
| 5849 | CGM.getOpenMPRuntime().getAddrOfArtificialThreadPrivate( |
| 5850 | CGF, CGM.getContext().VoidPtrTy, |
Alexey Bataev | 1c44e15 | 2018-03-06 18:59:43 +0000 | [diff] [blame] | 5851 | generateUniqueName(CGM, "reduction", RCG.getRefExpr(N))); |
Alexey Bataev | 21dab12 | 2018-03-09 15:20:30 +0000 | [diff] [blame] | 5852 | SharedAddr = CGF.EmitLoadOfPointer( |
| 5853 | SharedAddr, |
| 5854 | CGM.getContext().VoidPtrTy.castAs<PointerType>()->getTypePtr()); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 5855 | SharedLVal = CGF.MakeAddrLValue(SharedAddr, CGM.getContext().VoidPtrTy); |
| 5856 | } else { |
| 5857 | SharedLVal = CGF.MakeNaturalAlignAddrLValue( |
| 5858 | llvm::ConstantPointerNull::get(CGM.VoidPtrTy), |
| 5859 | CGM.getContext().VoidPtrTy); |
| 5860 | } |
| 5861 | // Emit the initializer: |
| 5862 | // %0 = bitcast void* %arg to <type>* |
| 5863 | // store <type> <init>, <type>* %0 |
| 5864 | RCG.emitInitialization(CGF, N, PrivateAddr, SharedLVal, |
| 5865 | [](CodeGenFunction &) { return false; }); |
| 5866 | CGF.FinishFunction(); |
| 5867 | return Fn; |
| 5868 | } |
| 5869 | |
| 5870 | /// Emits reduction combiner function: |
| 5871 | /// \code |
| 5872 | /// void @.red_comb(void* %arg0, void* %arg1) { |
| 5873 | /// %lhs = bitcast void* %arg0 to <type>* |
| 5874 | /// %rhs = bitcast void* %arg1 to <type>* |
| 5875 | /// %2 = <ReductionOp>(<type>* %lhs, <type>* %rhs) |
| 5876 | /// store <type> %2, <type>* %lhs |
| 5877 | /// ret void |
| 5878 | /// } |
| 5879 | /// \endcode |
| 5880 | static llvm::Value *emitReduceCombFunction(CodeGenModule &CGM, |
| 5881 | SourceLocation Loc, |
| 5882 | ReductionCodeGen &RCG, unsigned N, |
| 5883 | const Expr *ReductionOp, |
| 5884 | const Expr *LHS, const Expr *RHS, |
| 5885 | const Expr *PrivateRef) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5886 | ASTContext &C = CGM.getContext(); |
| 5887 | const auto *LHSVD = cast<VarDecl>(cast<DeclRefExpr>(LHS)->getDecl()); |
| 5888 | const auto *RHSVD = cast<VarDecl>(cast<DeclRefExpr>(RHS)->getDecl()); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 5889 | FunctionArgList Args; |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 5890 | ImplicitParamDecl ParamInOut(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, |
| 5891 | C.VoidPtrTy, ImplicitParamDecl::Other); |
| 5892 | ImplicitParamDecl ParamIn(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, C.VoidPtrTy, |
| 5893 | ImplicitParamDecl::Other); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 5894 | Args.emplace_back(&ParamInOut); |
| 5895 | Args.emplace_back(&ParamIn); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5896 | const auto &FnInfo = |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 5897 | CGM.getTypes().arrangeBuiltinFunctionDeclaration(C.VoidTy, Args); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5898 | llvm::FunctionType *FnTy = CGM.getTypes().GetFunctionType(FnInfo); |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 5899 | std::string Name = CGM.getOpenMPRuntime().getName({"red_comb", ""}); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 5900 | auto *Fn = llvm::Function::Create(FnTy, llvm::GlobalValue::InternalLinkage, |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 5901 | Name, &CGM.getModule()); |
Rafael Espindola | 51ec5a9 | 2018-02-28 23:46:35 +0000 | [diff] [blame] | 5902 | CGM.SetInternalFunctionAttributes(GlobalDecl(), Fn, FnInfo); |
Alexey Bataev | c0f879b | 2018-04-10 20:10:53 +0000 | [diff] [blame] | 5903 | Fn->setDoesNotRecurse(); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 5904 | CodeGenFunction CGF(CGM); |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 5905 | CGF.StartFunction(GlobalDecl(), C.VoidTy, Fn, FnInfo, Args, Loc, Loc); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 5906 | llvm::Value *Size = nullptr; |
| 5907 | // If the size of the reduction item is non-constant, load it from global |
| 5908 | // threadprivate variable. |
| 5909 | if (RCG.getSizes(N).second) { |
| 5910 | Address SizeAddr = CGM.getOpenMPRuntime().getAddrOfArtificialThreadPrivate( |
| 5911 | CGF, CGM.getContext().getSizeType(), |
Alexey Bataev | 1c44e15 | 2018-03-06 18:59:43 +0000 | [diff] [blame] | 5912 | generateUniqueName(CGM, "reduction_size", RCG.getRefExpr(N))); |
Alexey Bataev | a9b9cc0 | 2018-01-23 18:12:38 +0000 | [diff] [blame] | 5913 | Size = CGF.EmitLoadOfScalar(SizeAddr, /*Volatile=*/false, |
| 5914 | CGM.getContext().getSizeType(), Loc); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 5915 | } |
| 5916 | RCG.emitAggregateType(CGF, N, Size); |
| 5917 | // Remap lhs and rhs variables to the addresses of the function arguments. |
| 5918 | // %lhs = bitcast void* %arg0 to <type>* |
| 5919 | // %rhs = bitcast void* %arg1 to <type>* |
| 5920 | CodeGenFunction::OMPPrivateScope PrivateScope(CGF); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5921 | PrivateScope.addPrivate(LHSVD, [&C, &CGF, &ParamInOut, LHSVD]() { |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 5922 | // Pull out the pointer to the variable. |
| 5923 | Address PtrAddr = CGF.EmitLoadOfPointer( |
| 5924 | CGF.GetAddrOfLocalVar(&ParamInOut), |
| 5925 | C.getPointerType(C.VoidPtrTy).castAs<PointerType>()); |
| 5926 | return CGF.Builder.CreateElementBitCast( |
| 5927 | PtrAddr, CGF.ConvertTypeForMem(LHSVD->getType())); |
| 5928 | }); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5929 | PrivateScope.addPrivate(RHSVD, [&C, &CGF, &ParamIn, RHSVD]() { |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 5930 | // Pull out the pointer to the variable. |
| 5931 | Address PtrAddr = CGF.EmitLoadOfPointer( |
| 5932 | CGF.GetAddrOfLocalVar(&ParamIn), |
| 5933 | C.getPointerType(C.VoidPtrTy).castAs<PointerType>()); |
| 5934 | return CGF.Builder.CreateElementBitCast( |
| 5935 | PtrAddr, CGF.ConvertTypeForMem(RHSVD->getType())); |
| 5936 | }); |
| 5937 | PrivateScope.Privatize(); |
| 5938 | // Emit the combiner body: |
| 5939 | // %2 = <ReductionOp>(<type> *%lhs, <type> *%rhs) |
| 5940 | // store <type> %2, <type>* %lhs |
| 5941 | CGM.getOpenMPRuntime().emitSingleReductionCombiner( |
| 5942 | CGF, ReductionOp, PrivateRef, cast<DeclRefExpr>(LHS), |
| 5943 | cast<DeclRefExpr>(RHS)); |
| 5944 | CGF.FinishFunction(); |
| 5945 | return Fn; |
| 5946 | } |
| 5947 | |
| 5948 | /// Emits reduction finalizer function: |
| 5949 | /// \code |
| 5950 | /// void @.red_fini(void* %arg) { |
| 5951 | /// %0 = bitcast void* %arg to <type>* |
| 5952 | /// <destroy>(<type>* %0) |
| 5953 | /// ret void |
| 5954 | /// } |
| 5955 | /// \endcode |
| 5956 | static llvm::Value *emitReduceFiniFunction(CodeGenModule &CGM, |
| 5957 | SourceLocation Loc, |
| 5958 | ReductionCodeGen &RCG, unsigned N) { |
| 5959 | if (!RCG.needCleanups(N)) |
| 5960 | return nullptr; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5961 | ASTContext &C = CGM.getContext(); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 5962 | FunctionArgList Args; |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 5963 | ImplicitParamDecl Param(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, C.VoidPtrTy, |
| 5964 | ImplicitParamDecl::Other); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 5965 | Args.emplace_back(&Param); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5966 | const auto &FnInfo = |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 5967 | CGM.getTypes().arrangeBuiltinFunctionDeclaration(C.VoidTy, Args); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5968 | llvm::FunctionType *FnTy = CGM.getTypes().GetFunctionType(FnInfo); |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 5969 | std::string Name = CGM.getOpenMPRuntime().getName({"red_fini", ""}); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 5970 | auto *Fn = llvm::Function::Create(FnTy, llvm::GlobalValue::InternalLinkage, |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 5971 | Name, &CGM.getModule()); |
Rafael Espindola | 51ec5a9 | 2018-02-28 23:46:35 +0000 | [diff] [blame] | 5972 | CGM.SetInternalFunctionAttributes(GlobalDecl(), Fn, FnInfo); |
Alexey Bataev | c0f879b | 2018-04-10 20:10:53 +0000 | [diff] [blame] | 5973 | Fn->setDoesNotRecurse(); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 5974 | CodeGenFunction CGF(CGM); |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 5975 | CGF.StartFunction(GlobalDecl(), C.VoidTy, Fn, FnInfo, Args, Loc, Loc); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 5976 | Address PrivateAddr = CGF.EmitLoadOfPointer( |
| 5977 | CGF.GetAddrOfLocalVar(&Param), |
| 5978 | C.getPointerType(C.VoidPtrTy).castAs<PointerType>()); |
| 5979 | llvm::Value *Size = nullptr; |
| 5980 | // If the size of the reduction item is non-constant, load it from global |
| 5981 | // threadprivate variable. |
| 5982 | if (RCG.getSizes(N).second) { |
| 5983 | Address SizeAddr = CGM.getOpenMPRuntime().getAddrOfArtificialThreadPrivate( |
| 5984 | CGF, CGM.getContext().getSizeType(), |
Alexey Bataev | 1c44e15 | 2018-03-06 18:59:43 +0000 | [diff] [blame] | 5985 | generateUniqueName(CGM, "reduction_size", RCG.getRefExpr(N))); |
Alexey Bataev | a9b9cc0 | 2018-01-23 18:12:38 +0000 | [diff] [blame] | 5986 | Size = CGF.EmitLoadOfScalar(SizeAddr, /*Volatile=*/false, |
| 5987 | CGM.getContext().getSizeType(), Loc); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 5988 | } |
| 5989 | RCG.emitAggregateType(CGF, N, Size); |
| 5990 | // Emit the finalizer body: |
| 5991 | // <destroy>(<type>* %0) |
| 5992 | RCG.emitCleanups(CGF, N, PrivateAddr); |
| 5993 | CGF.FinishFunction(); |
| 5994 | return Fn; |
| 5995 | } |
| 5996 | |
| 5997 | llvm::Value *CGOpenMPRuntime::emitTaskReductionInit( |
| 5998 | CodeGenFunction &CGF, SourceLocation Loc, ArrayRef<const Expr *> LHSExprs, |
| 5999 | ArrayRef<const Expr *> RHSExprs, const OMPTaskDataTy &Data) { |
| 6000 | if (!CGF.HaveInsertPoint() || Data.ReductionVars.empty()) |
| 6001 | return nullptr; |
| 6002 | |
| 6003 | // Build typedef struct: |
| 6004 | // kmp_task_red_input { |
| 6005 | // void *reduce_shar; // shared reduction item |
| 6006 | // size_t reduce_size; // size of data item |
| 6007 | // void *reduce_init; // data initialization routine |
| 6008 | // void *reduce_fini; // data finalization routine |
| 6009 | // void *reduce_comb; // data combiner routine |
| 6010 | // kmp_task_red_flags_t flags; // flags for additional info from compiler |
| 6011 | // } kmp_task_red_input_t; |
| 6012 | ASTContext &C = CGM.getContext(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6013 | RecordDecl *RD = C.buildImplicitRecord("kmp_task_red_input_t"); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 6014 | RD->startDefinition(); |
| 6015 | const FieldDecl *SharedFD = addFieldToRecordDecl(C, RD, C.VoidPtrTy); |
| 6016 | const FieldDecl *SizeFD = addFieldToRecordDecl(C, RD, C.getSizeType()); |
| 6017 | const FieldDecl *InitFD = addFieldToRecordDecl(C, RD, C.VoidPtrTy); |
| 6018 | const FieldDecl *FiniFD = addFieldToRecordDecl(C, RD, C.VoidPtrTy); |
| 6019 | const FieldDecl *CombFD = addFieldToRecordDecl(C, RD, C.VoidPtrTy); |
| 6020 | const FieldDecl *FlagsFD = addFieldToRecordDecl( |
| 6021 | C, RD, C.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/false)); |
| 6022 | RD->completeDefinition(); |
| 6023 | QualType RDType = C.getRecordType(RD); |
| 6024 | unsigned Size = Data.ReductionVars.size(); |
| 6025 | llvm::APInt ArraySize(/*numBits=*/64, Size); |
| 6026 | QualType ArrayRDType = C.getConstantArrayType( |
| 6027 | RDType, ArraySize, ArrayType::Normal, /*IndexTypeQuals=*/0); |
| 6028 | // kmp_task_red_input_t .rd_input.[Size]; |
| 6029 | Address TaskRedInput = CGF.CreateMemTemp(ArrayRDType, ".rd_input."); |
| 6030 | ReductionCodeGen RCG(Data.ReductionVars, Data.ReductionCopies, |
| 6031 | Data.ReductionOps); |
| 6032 | for (unsigned Cnt = 0; Cnt < Size; ++Cnt) { |
| 6033 | // kmp_task_red_input_t &ElemLVal = .rd_input.[Cnt]; |
| 6034 | llvm::Value *Idxs[] = {llvm::ConstantInt::get(CGM.SizeTy, /*V=*/0), |
| 6035 | llvm::ConstantInt::get(CGM.SizeTy, Cnt)}; |
| 6036 | llvm::Value *GEP = CGF.EmitCheckedInBoundsGEP( |
| 6037 | TaskRedInput.getPointer(), Idxs, |
| 6038 | /*SignedIndices=*/false, /*IsSubtraction=*/false, Loc, |
| 6039 | ".rd_input.gep."); |
| 6040 | LValue ElemLVal = CGF.MakeNaturalAlignAddrLValue(GEP, RDType); |
| 6041 | // ElemLVal.reduce_shar = &Shareds[Cnt]; |
| 6042 | LValue SharedLVal = CGF.EmitLValueForField(ElemLVal, SharedFD); |
| 6043 | RCG.emitSharedLValue(CGF, Cnt); |
| 6044 | llvm::Value *CastedShared = |
| 6045 | CGF.EmitCastToVoidPtr(RCG.getSharedLValue(Cnt).getPointer()); |
| 6046 | CGF.EmitStoreOfScalar(CastedShared, SharedLVal); |
| 6047 | RCG.emitAggregateType(CGF, Cnt); |
| 6048 | llvm::Value *SizeValInChars; |
| 6049 | llvm::Value *SizeVal; |
| 6050 | std::tie(SizeValInChars, SizeVal) = RCG.getSizes(Cnt); |
| 6051 | // We use delayed creation/initialization for VLAs, array sections and |
| 6052 | // custom reduction initializations. It is required because runtime does not |
| 6053 | // provide the way to pass the sizes of VLAs/array sections to |
| 6054 | // initializer/combiner/finalizer functions and does not pass the pointer to |
| 6055 | // original reduction item to the initializer. Instead threadprivate global |
| 6056 | // variables are used to store these values and use them in the functions. |
| 6057 | bool DelayedCreation = !!SizeVal; |
| 6058 | SizeValInChars = CGF.Builder.CreateIntCast(SizeValInChars, CGM.SizeTy, |
| 6059 | /*isSigned=*/false); |
| 6060 | LValue SizeLVal = CGF.EmitLValueForField(ElemLVal, SizeFD); |
| 6061 | CGF.EmitStoreOfScalar(SizeValInChars, SizeLVal); |
| 6062 | // ElemLVal.reduce_init = init; |
| 6063 | LValue InitLVal = CGF.EmitLValueForField(ElemLVal, InitFD); |
| 6064 | llvm::Value *InitAddr = |
| 6065 | CGF.EmitCastToVoidPtr(emitReduceInitFunction(CGM, Loc, RCG, Cnt)); |
| 6066 | CGF.EmitStoreOfScalar(InitAddr, InitLVal); |
| 6067 | DelayedCreation = DelayedCreation || RCG.usesReductionInitializer(Cnt); |
| 6068 | // ElemLVal.reduce_fini = fini; |
| 6069 | LValue FiniLVal = CGF.EmitLValueForField(ElemLVal, FiniFD); |
| 6070 | llvm::Value *Fini = emitReduceFiniFunction(CGM, Loc, RCG, Cnt); |
| 6071 | llvm::Value *FiniAddr = Fini |
| 6072 | ? CGF.EmitCastToVoidPtr(Fini) |
| 6073 | : llvm::ConstantPointerNull::get(CGM.VoidPtrTy); |
| 6074 | CGF.EmitStoreOfScalar(FiniAddr, FiniLVal); |
| 6075 | // ElemLVal.reduce_comb = comb; |
| 6076 | LValue CombLVal = CGF.EmitLValueForField(ElemLVal, CombFD); |
| 6077 | llvm::Value *CombAddr = CGF.EmitCastToVoidPtr(emitReduceCombFunction( |
| 6078 | CGM, Loc, RCG, Cnt, Data.ReductionOps[Cnt], LHSExprs[Cnt], |
| 6079 | RHSExprs[Cnt], Data.ReductionCopies[Cnt])); |
| 6080 | CGF.EmitStoreOfScalar(CombAddr, CombLVal); |
| 6081 | // ElemLVal.flags = 0; |
| 6082 | LValue FlagsLVal = CGF.EmitLValueForField(ElemLVal, FlagsFD); |
| 6083 | if (DelayedCreation) { |
| 6084 | CGF.EmitStoreOfScalar( |
| 6085 | llvm::ConstantInt::get(CGM.Int32Ty, /*V=*/1, /*IsSigned=*/true), |
| 6086 | FlagsLVal); |
| 6087 | } else |
| 6088 | CGF.EmitNullInitialization(FlagsLVal.getAddress(), FlagsLVal.getType()); |
| 6089 | } |
| 6090 | // Build call void *__kmpc_task_reduction_init(int gtid, int num_data, void |
| 6091 | // *data); |
| 6092 | llvm::Value *Args[] = { |
| 6093 | CGF.Builder.CreateIntCast(getThreadID(CGF, Loc), CGM.IntTy, |
| 6094 | /*isSigned=*/true), |
| 6095 | llvm::ConstantInt::get(CGM.IntTy, Size, /*isSigned=*/true), |
| 6096 | CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(TaskRedInput.getPointer(), |
| 6097 | CGM.VoidPtrTy)}; |
| 6098 | return CGF.EmitRuntimeCall( |
| 6099 | createRuntimeFunction(OMPRTL__kmpc_task_reduction_init), Args); |
| 6100 | } |
| 6101 | |
| 6102 | void CGOpenMPRuntime::emitTaskReductionFixups(CodeGenFunction &CGF, |
| 6103 | SourceLocation Loc, |
| 6104 | ReductionCodeGen &RCG, |
| 6105 | unsigned N) { |
| 6106 | auto Sizes = RCG.getSizes(N); |
| 6107 | // Emit threadprivate global variable if the type is non-constant |
| 6108 | // (Sizes.second = nullptr). |
| 6109 | if (Sizes.second) { |
| 6110 | llvm::Value *SizeVal = CGF.Builder.CreateIntCast(Sizes.second, CGM.SizeTy, |
| 6111 | /*isSigned=*/false); |
| 6112 | Address SizeAddr = getAddrOfArtificialThreadPrivate( |
| 6113 | CGF, CGM.getContext().getSizeType(), |
Alexey Bataev | 1c44e15 | 2018-03-06 18:59:43 +0000 | [diff] [blame] | 6114 | generateUniqueName(CGM, "reduction_size", RCG.getRefExpr(N))); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 6115 | CGF.Builder.CreateStore(SizeVal, SizeAddr, /*IsVolatile=*/false); |
| 6116 | } |
| 6117 | // Store address of the original reduction item if custom initializer is used. |
| 6118 | if (RCG.usesReductionInitializer(N)) { |
| 6119 | Address SharedAddr = getAddrOfArtificialThreadPrivate( |
| 6120 | CGF, CGM.getContext().VoidPtrTy, |
Alexey Bataev | 1c44e15 | 2018-03-06 18:59:43 +0000 | [diff] [blame] | 6121 | generateUniqueName(CGM, "reduction", RCG.getRefExpr(N))); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 6122 | CGF.Builder.CreateStore( |
| 6123 | CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
| 6124 | RCG.getSharedLValue(N).getPointer(), CGM.VoidPtrTy), |
| 6125 | SharedAddr, /*IsVolatile=*/false); |
| 6126 | } |
| 6127 | } |
| 6128 | |
| 6129 | Address CGOpenMPRuntime::getTaskReductionItem(CodeGenFunction &CGF, |
| 6130 | SourceLocation Loc, |
| 6131 | llvm::Value *ReductionsPtr, |
| 6132 | LValue SharedLVal) { |
| 6133 | // Build call void *__kmpc_task_reduction_get_th_data(int gtid, void *tg, void |
| 6134 | // *d); |
| 6135 | llvm::Value *Args[] = { |
| 6136 | CGF.Builder.CreateIntCast(getThreadID(CGF, Loc), CGM.IntTy, |
| 6137 | /*isSigned=*/true), |
| 6138 | ReductionsPtr, |
| 6139 | CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(SharedLVal.getPointer(), |
| 6140 | CGM.VoidPtrTy)}; |
| 6141 | return Address( |
| 6142 | CGF.EmitRuntimeCall( |
| 6143 | createRuntimeFunction(OMPRTL__kmpc_task_reduction_get_th_data), Args), |
| 6144 | SharedLVal.getAlignment()); |
| 6145 | } |
| 6146 | |
Alexey Bataev | 8b8e202 | 2015-04-27 05:22:09 +0000 | [diff] [blame] | 6147 | void CGOpenMPRuntime::emitTaskwaitCall(CodeGenFunction &CGF, |
| 6148 | SourceLocation Loc) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 6149 | if (!CGF.HaveInsertPoint()) |
| 6150 | return; |
Alexey Bataev | 8b8e202 | 2015-04-27 05:22:09 +0000 | [diff] [blame] | 6151 | // Build call kmp_int32 __kmpc_omp_taskwait(ident_t *loc, kmp_int32 |
| 6152 | // global_tid); |
| 6153 | llvm::Value *Args[] = {emitUpdateLocation(CGF, Loc), getThreadID(CGF, Loc)}; |
| 6154 | // Ignore return result until untied tasks are supported. |
| 6155 | CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_omp_taskwait), Args); |
Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 6156 | if (auto *Region = dyn_cast_or_null<CGOpenMPRegionInfo>(CGF.CapturedStmtInfo)) |
| 6157 | Region->emitUntiedSwitch(CGF); |
Alexey Bataev | 8b8e202 | 2015-04-27 05:22:09 +0000 | [diff] [blame] | 6158 | } |
| 6159 | |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 6160 | void CGOpenMPRuntime::emitInlinedDirective(CodeGenFunction &CGF, |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 6161 | OpenMPDirectiveKind InnerKind, |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 6162 | const RegionCodeGenTy &CodeGen, |
| 6163 | bool HasCancel) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 6164 | if (!CGF.HaveInsertPoint()) |
| 6165 | return; |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 6166 | InlinedOpenMPRegionRAII Region(CGF, CodeGen, InnerKind, HasCancel); |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 6167 | CGF.CapturedStmtInfo->EmitBody(CGF, /*S=*/nullptr); |
Alexey Bataev | 8cbe0a6 | 2015-02-26 10:27:34 +0000 | [diff] [blame] | 6168 | } |
| 6169 | |
Alexey Bataev | 7d5d33e | 2015-07-06 05:50:32 +0000 | [diff] [blame] | 6170 | namespace { |
| 6171 | enum RTCancelKind { |
| 6172 | CancelNoreq = 0, |
| 6173 | CancelParallel = 1, |
| 6174 | CancelLoop = 2, |
| 6175 | CancelSections = 3, |
| 6176 | CancelTaskgroup = 4 |
| 6177 | }; |
Eugene Zelenko | 0a4f3f4 | 2016-02-10 19:11:58 +0000 | [diff] [blame] | 6178 | } // anonymous namespace |
Alexey Bataev | 7d5d33e | 2015-07-06 05:50:32 +0000 | [diff] [blame] | 6179 | |
| 6180 | static RTCancelKind getCancellationKind(OpenMPDirectiveKind CancelRegion) { |
| 6181 | RTCancelKind CancelKind = CancelNoreq; |
Alexey Bataev | 0f34da1 | 2015-07-02 04:17:07 +0000 | [diff] [blame] | 6182 | if (CancelRegion == OMPD_parallel) |
| 6183 | CancelKind = CancelParallel; |
| 6184 | else if (CancelRegion == OMPD_for) |
| 6185 | CancelKind = CancelLoop; |
| 6186 | else if (CancelRegion == OMPD_sections) |
| 6187 | CancelKind = CancelSections; |
| 6188 | else { |
| 6189 | assert(CancelRegion == OMPD_taskgroup); |
| 6190 | CancelKind = CancelTaskgroup; |
| 6191 | } |
Alexey Bataev | 7d5d33e | 2015-07-06 05:50:32 +0000 | [diff] [blame] | 6192 | return CancelKind; |
| 6193 | } |
| 6194 | |
| 6195 | void CGOpenMPRuntime::emitCancellationPointCall( |
| 6196 | CodeGenFunction &CGF, SourceLocation Loc, |
| 6197 | OpenMPDirectiveKind CancelRegion) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 6198 | if (!CGF.HaveInsertPoint()) |
| 6199 | return; |
Alexey Bataev | 7d5d33e | 2015-07-06 05:50:32 +0000 | [diff] [blame] | 6200 | // Build call kmp_int32 __kmpc_cancellationpoint(ident_t *loc, kmp_int32 |
| 6201 | // global_tid, kmp_int32 cncl_kind); |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 6202 | if (auto *OMPRegionInfo = |
| 6203 | dyn_cast_or_null<CGOpenMPRegionInfo>(CGF.CapturedStmtInfo)) { |
Jonas Hahnfeld | b07931f | 2017-02-17 18:32:58 +0000 | [diff] [blame] | 6204 | // For 'cancellation point taskgroup', the task region info may not have a |
| 6205 | // cancel. This may instead happen in another adjacent task. |
| 6206 | if (CancelRegion == OMPD_taskgroup || OMPRegionInfo->hasCancel()) { |
Alexey Bataev | 7d5d33e | 2015-07-06 05:50:32 +0000 | [diff] [blame] | 6207 | llvm::Value *Args[] = { |
| 6208 | emitUpdateLocation(CGF, Loc), getThreadID(CGF, Loc), |
| 6209 | CGF.Builder.getInt32(getCancellationKind(CancelRegion))}; |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 6210 | // Ignore return result until untied tasks are supported. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6211 | llvm::Value *Result = CGF.EmitRuntimeCall( |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 6212 | createRuntimeFunction(OMPRTL__kmpc_cancellationpoint), Args); |
| 6213 | // if (__kmpc_cancellationpoint()) { |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 6214 | // exit from construct; |
| 6215 | // } |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6216 | llvm::BasicBlock *ExitBB = CGF.createBasicBlock(".cancel.exit"); |
| 6217 | llvm::BasicBlock *ContBB = CGF.createBasicBlock(".cancel.continue"); |
| 6218 | llvm::Value *Cmp = CGF.Builder.CreateIsNotNull(Result); |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 6219 | CGF.Builder.CreateCondBr(Cmp, ExitBB, ContBB); |
| 6220 | CGF.EmitBlock(ExitBB); |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 6221 | // exit from construct; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6222 | CodeGenFunction::JumpDest CancelDest = |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 6223 | CGF.getOMPCancelDestination(OMPRegionInfo->getDirectiveKind()); |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 6224 | CGF.EmitBranchThroughCleanup(CancelDest); |
| 6225 | CGF.EmitBlock(ContBB, /*IsFinished=*/true); |
| 6226 | } |
Alexey Bataev | 0f34da1 | 2015-07-02 04:17:07 +0000 | [diff] [blame] | 6227 | } |
Alexey Bataev | 0f34da1 | 2015-07-02 04:17:07 +0000 | [diff] [blame] | 6228 | } |
| 6229 | |
Alexey Bataev | 7d5d33e | 2015-07-06 05:50:32 +0000 | [diff] [blame] | 6230 | void CGOpenMPRuntime::emitCancelCall(CodeGenFunction &CGF, SourceLocation Loc, |
Alexey Bataev | 87933c7 | 2015-09-18 08:07:34 +0000 | [diff] [blame] | 6231 | const Expr *IfCond, |
Alexey Bataev | 7d5d33e | 2015-07-06 05:50:32 +0000 | [diff] [blame] | 6232 | OpenMPDirectiveKind CancelRegion) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 6233 | if (!CGF.HaveInsertPoint()) |
| 6234 | return; |
Alexey Bataev | 7d5d33e | 2015-07-06 05:50:32 +0000 | [diff] [blame] | 6235 | // Build call kmp_int32 __kmpc_cancel(ident_t *loc, kmp_int32 global_tid, |
| 6236 | // kmp_int32 cncl_kind); |
| 6237 | if (auto *OMPRegionInfo = |
| 6238 | dyn_cast_or_null<CGOpenMPRegionInfo>(CGF.CapturedStmtInfo)) { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 6239 | auto &&ThenGen = [Loc, CancelRegion, OMPRegionInfo](CodeGenFunction &CGF, |
| 6240 | PrePostActionTy &) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6241 | CGOpenMPRuntime &RT = CGF.CGM.getOpenMPRuntime(); |
Alexey Bataev | 87933c7 | 2015-09-18 08:07:34 +0000 | [diff] [blame] | 6242 | llvm::Value *Args[] = { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 6243 | RT.emitUpdateLocation(CGF, Loc), RT.getThreadID(CGF, Loc), |
Alexey Bataev | 87933c7 | 2015-09-18 08:07:34 +0000 | [diff] [blame] | 6244 | CGF.Builder.getInt32(getCancellationKind(CancelRegion))}; |
| 6245 | // Ignore return result until untied tasks are supported. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6246 | llvm::Value *Result = CGF.EmitRuntimeCall( |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 6247 | RT.createRuntimeFunction(OMPRTL__kmpc_cancel), Args); |
Alexey Bataev | 87933c7 | 2015-09-18 08:07:34 +0000 | [diff] [blame] | 6248 | // if (__kmpc_cancel()) { |
Alexey Bataev | 87933c7 | 2015-09-18 08:07:34 +0000 | [diff] [blame] | 6249 | // exit from construct; |
| 6250 | // } |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6251 | llvm::BasicBlock *ExitBB = CGF.createBasicBlock(".cancel.exit"); |
| 6252 | llvm::BasicBlock *ContBB = CGF.createBasicBlock(".cancel.continue"); |
| 6253 | llvm::Value *Cmp = CGF.Builder.CreateIsNotNull(Result); |
Alexey Bataev | 87933c7 | 2015-09-18 08:07:34 +0000 | [diff] [blame] | 6254 | CGF.Builder.CreateCondBr(Cmp, ExitBB, ContBB); |
| 6255 | CGF.EmitBlock(ExitBB); |
Alexey Bataev | 87933c7 | 2015-09-18 08:07:34 +0000 | [diff] [blame] | 6256 | // exit from construct; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6257 | CodeGenFunction::JumpDest CancelDest = |
Alexey Bataev | 87933c7 | 2015-09-18 08:07:34 +0000 | [diff] [blame] | 6258 | CGF.getOMPCancelDestination(OMPRegionInfo->getDirectiveKind()); |
| 6259 | CGF.EmitBranchThroughCleanup(CancelDest); |
| 6260 | CGF.EmitBlock(ContBB, /*IsFinished=*/true); |
| 6261 | }; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6262 | if (IfCond) { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 6263 | emitOMPIfClause(CGF, IfCond, ThenGen, |
| 6264 | [](CodeGenFunction &, PrePostActionTy &) {}); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6265 | } else { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 6266 | RegionCodeGenTy ThenRCG(ThenGen); |
| 6267 | ThenRCG(CGF); |
| 6268 | } |
Alexey Bataev | 7d5d33e | 2015-07-06 05:50:32 +0000 | [diff] [blame] | 6269 | } |
| 6270 | } |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 6271 | |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 6272 | void CGOpenMPRuntime::emitTargetOutlinedFunction( |
| 6273 | const OMPExecutableDirective &D, StringRef ParentName, |
| 6274 | llvm::Function *&OutlinedFn, llvm::Constant *&OutlinedFnID, |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 6275 | bool IsOffloadEntry, const RegionCodeGenTy &CodeGen) { |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 6276 | assert(!ParentName.empty() && "Invalid target region parent name!"); |
Arpith Chacko Jacob | 5c309e4 | 2016-03-22 01:48:56 +0000 | [diff] [blame] | 6277 | emitTargetOutlinedFunctionHelper(D, ParentName, OutlinedFn, OutlinedFnID, |
| 6278 | IsOffloadEntry, CodeGen); |
| 6279 | } |
| 6280 | |
| 6281 | void CGOpenMPRuntime::emitTargetOutlinedFunctionHelper( |
| 6282 | const OMPExecutableDirective &D, StringRef ParentName, |
| 6283 | llvm::Function *&OutlinedFn, llvm::Constant *&OutlinedFnID, |
| 6284 | bool IsOffloadEntry, const RegionCodeGenTy &CodeGen) { |
Samuel Antao | 2de62b0 | 2016-02-13 23:35:10 +0000 | [diff] [blame] | 6285 | // Create a unique name for the entry function using the source location |
| 6286 | // information of the current target region. The name will be something like: |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 6287 | // |
Samuel Antao | 2de62b0 | 2016-02-13 23:35:10 +0000 | [diff] [blame] | 6288 | // __omp_offloading_DD_FFFF_PP_lBB |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 6289 | // |
| 6290 | // 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] | 6291 | // mangled name of the function that encloses the target region and BB is the |
| 6292 | // line number of the target region. |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 6293 | |
| 6294 | unsigned DeviceID; |
| 6295 | unsigned FileID; |
| 6296 | unsigned Line; |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 6297 | getTargetEntryUniqueInfo(CGM.getContext(), D.getBeginLoc(), DeviceID, FileID, |
Samuel Antao | 2de62b0 | 2016-02-13 23:35:10 +0000 | [diff] [blame] | 6298 | Line); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 6299 | SmallString<64> EntryFnName; |
| 6300 | { |
| 6301 | llvm::raw_svector_ostream OS(EntryFnName); |
Samuel Antao | 2de62b0 | 2016-02-13 23:35:10 +0000 | [diff] [blame] | 6302 | OS << "__omp_offloading" << llvm::format("_%x", DeviceID) |
| 6303 | << llvm::format("_%x_", FileID) << ParentName << "_l" << Line; |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 6304 | } |
| 6305 | |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 6306 | const CapturedStmt &CS = *D.getCapturedStmt(OMPD_target); |
Arpith Chacko Jacob | 5c309e4 | 2016-03-22 01:48:56 +0000 | [diff] [blame] | 6307 | |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 6308 | CodeGenFunction CGF(CGM, true); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 6309 | CGOpenMPTargetRegionInfo CGInfo(CS, CodeGen, EntryFnName); |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 6310 | CodeGenFunction::CGCapturedStmtRAII CapInfoRAII(CGF, &CGInfo); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 6311 | |
Samuel Antao | 6d00426 | 2016-06-16 18:39:34 +0000 | [diff] [blame] | 6312 | OutlinedFn = CGF.GenerateOpenMPCapturedStmtFunction(CS); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 6313 | |
| 6314 | // If this target outline function is not an offload entry, we don't need to |
| 6315 | // register it. |
| 6316 | if (!IsOffloadEntry) |
| 6317 | return; |
| 6318 | |
| 6319 | // The target region ID is used by the runtime library to identify the current |
| 6320 | // target region, so it only has to be unique and not necessarily point to |
| 6321 | // anything. It could be the pointer to the outlined function that implements |
| 6322 | // the target region, but we aren't using that so that the compiler doesn't |
| 6323 | // need to keep that, and could therefore inline the host function if proven |
| 6324 | // worthwhile during optimization. In the other hand, if emitting code for the |
| 6325 | // device, the ID has to be the function address so that it can retrieved from |
| 6326 | // the offloading entry and launched by the runtime library. We also mark the |
| 6327 | // outlined function to have external linkage in case we are emitting code for |
| 6328 | // the device, because these functions will be entry points to the device. |
| 6329 | |
| 6330 | if (CGM.getLangOpts().OpenMPIsDevice) { |
| 6331 | OutlinedFnID = llvm::ConstantExpr::getBitCast(OutlinedFn, CGM.Int8PtrTy); |
Alexey Bataev | 9a70017 | 2018-05-08 14:16:57 +0000 | [diff] [blame] | 6332 | OutlinedFn->setLinkage(llvm::GlobalValue::WeakAnyLinkage); |
Rafael Espindola | cbca487 | 2018-01-11 22:15:12 +0000 | [diff] [blame] | 6333 | OutlinedFn->setDSOLocal(false); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6334 | } else { |
Alexey Bataev | c15ea70 | 2018-05-09 18:02:37 +0000 | [diff] [blame] | 6335 | std::string Name = getName({EntryFnName, "region_id"}); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 6336 | OutlinedFnID = new llvm::GlobalVariable( |
| 6337 | CGM.getModule(), CGM.Int8Ty, /*isConstant=*/true, |
Alexey Bataev | 9a70017 | 2018-05-08 14:16:57 +0000 | [diff] [blame] | 6338 | llvm::GlobalValue::WeakAnyLinkage, |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 6339 | llvm::Constant::getNullValue(CGM.Int8Ty), Name); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6340 | } |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 6341 | |
| 6342 | // Register the information for the entry associated with this target region. |
| 6343 | OffloadEntriesInfoManager.registerTargetRegionEntryInfo( |
Samuel Antao | f83efdb | 2017-01-05 16:02:49 +0000 | [diff] [blame] | 6344 | DeviceID, FileID, ParentName, Line, OutlinedFn, OutlinedFnID, |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 6345 | OffloadEntriesInfoManagerTy::OMPTargetRegionEntryTargetRegion); |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 6346 | } |
| 6347 | |
Carlo Bertolli | 6eee906 | 2016-04-29 01:37:30 +0000 | [diff] [blame] | 6348 | /// discard all CompoundStmts intervening between two constructs |
| 6349 | static const Stmt *ignoreCompoundStmts(const Stmt *Body) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6350 | while (const auto *CS = dyn_cast_or_null<CompoundStmt>(Body)) |
Carlo Bertolli | 6eee906 | 2016-04-29 01:37:30 +0000 | [diff] [blame] | 6351 | Body = CS->body_front(); |
| 6352 | |
| 6353 | return Body; |
| 6354 | } |
| 6355 | |
Arpith Chacko Jacob | 33c849a | 2017-01-25 00:57:16 +0000 | [diff] [blame] | 6356 | /// Emit the number of teams for a target directive. Inspect the num_teams |
| 6357 | /// clause associated with a teams construct combined or closely nested |
| 6358 | /// with the target directive. |
| 6359 | /// |
| 6360 | /// Emit a team of size one for directives such as 'target parallel' that |
| 6361 | /// have no associated teams construct. |
| 6362 | /// |
| 6363 | /// Otherwise, return nullptr. |
Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 6364 | static llvm::Value * |
Arpith Chacko Jacob | 33c849a | 2017-01-25 00:57:16 +0000 | [diff] [blame] | 6365 | emitNumTeamsForTargetDirective(CGOpenMPRuntime &OMPRuntime, |
| 6366 | CodeGenFunction &CGF, |
| 6367 | const OMPExecutableDirective &D) { |
Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 6368 | assert(!CGF.getLangOpts().OpenMPIsDevice && "Clauses associated with the " |
| 6369 | "teams directive expected to be " |
| 6370 | "emitted only for the host!"); |
| 6371 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6372 | CGBuilderTy &Bld = CGF.Builder; |
Arpith Chacko Jacob | 99a1e0e | 2017-01-25 02:18:43 +0000 | [diff] [blame] | 6373 | |
| 6374 | // If the target directive is combined with a teams directive: |
| 6375 | // Return the value in the num_teams clause, if any. |
| 6376 | // Otherwise, return 0 to denote the runtime default. |
| 6377 | if (isOpenMPTeamsDirective(D.getDirectiveKind())) { |
| 6378 | if (const auto *NumTeamsClause = D.getSingleClause<OMPNumTeamsClause>()) { |
| 6379 | CodeGenFunction::RunCleanupsScope NumTeamsScope(CGF); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6380 | llvm::Value *NumTeams = CGF.EmitScalarExpr(NumTeamsClause->getNumTeams(), |
| 6381 | /*IgnoreResultAssign*/ true); |
Arpith Chacko Jacob | 99a1e0e | 2017-01-25 02:18:43 +0000 | [diff] [blame] | 6382 | return Bld.CreateIntCast(NumTeams, CGF.Int32Ty, |
| 6383 | /*IsSigned=*/true); |
| 6384 | } |
| 6385 | |
| 6386 | // The default value is 0. |
| 6387 | return Bld.getInt32(0); |
| 6388 | } |
| 6389 | |
Arpith Chacko Jacob | 33c849a | 2017-01-25 00:57:16 +0000 | [diff] [blame] | 6390 | // If the target directive is combined with a parallel directive but not a |
| 6391 | // teams directive, start one team. |
Arpith Chacko Jacob | 99a1e0e | 2017-01-25 02:18:43 +0000 | [diff] [blame] | 6392 | if (isOpenMPParallelDirective(D.getDirectiveKind())) |
| 6393 | return Bld.getInt32(1); |
Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 6394 | |
| 6395 | // If the current target region has a teams region enclosed, we need to get |
| 6396 | // the number of teams to pass to the runtime function call. This is done |
| 6397 | // by generating the expression in a inlined region. This is required because |
| 6398 | // the expression is captured in the enclosing target environment when the |
| 6399 | // teams directive is not combined with target. |
| 6400 | |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 6401 | const CapturedStmt &CS = *D.getCapturedStmt(OMPD_target); |
Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 6402 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6403 | if (const auto *TeamsDir = dyn_cast_or_null<OMPExecutableDirective>( |
Carlo Bertolli | 6eee906 | 2016-04-29 01:37:30 +0000 | [diff] [blame] | 6404 | ignoreCompoundStmts(CS.getCapturedStmt()))) { |
Alexey Bataev | 50a1c78 | 2017-12-01 21:31:08 +0000 | [diff] [blame] | 6405 | if (isOpenMPTeamsDirective(TeamsDir->getDirectiveKind())) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6406 | if (const auto *NTE = TeamsDir->getSingleClause<OMPNumTeamsClause>()) { |
Alexey Bataev | 50a1c78 | 2017-12-01 21:31:08 +0000 | [diff] [blame] | 6407 | CGOpenMPInnerExprInfo CGInfo(CGF, CS); |
| 6408 | CodeGenFunction::CGCapturedStmtRAII CapInfoRAII(CGF, &CGInfo); |
| 6409 | llvm::Value *NumTeams = CGF.EmitScalarExpr(NTE->getNumTeams()); |
| 6410 | return Bld.CreateIntCast(NumTeams, CGF.Int32Ty, |
| 6411 | /*IsSigned=*/true); |
| 6412 | } |
Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 6413 | |
Alexey Bataev | 50a1c78 | 2017-12-01 21:31:08 +0000 | [diff] [blame] | 6414 | // If we have an enclosed teams directive but no num_teams clause we use |
| 6415 | // the default value 0. |
| 6416 | return Bld.getInt32(0); |
| 6417 | } |
Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 6418 | } |
| 6419 | |
| 6420 | // No teams associated with the directive. |
| 6421 | return nullptr; |
| 6422 | } |
| 6423 | |
Arpith Chacko Jacob | 33c849a | 2017-01-25 00:57:16 +0000 | [diff] [blame] | 6424 | /// Emit the number of threads for a target directive. Inspect the |
| 6425 | /// thread_limit clause associated with a teams construct combined or closely |
| 6426 | /// nested with the target directive. |
| 6427 | /// |
| 6428 | /// Emit the num_threads clause for directives such as 'target parallel' that |
| 6429 | /// have no associated teams construct. |
| 6430 | /// |
| 6431 | /// Otherwise, return nullptr. |
Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 6432 | static llvm::Value * |
Arpith Chacko Jacob | 33c849a | 2017-01-25 00:57:16 +0000 | [diff] [blame] | 6433 | emitNumThreadsForTargetDirective(CGOpenMPRuntime &OMPRuntime, |
| 6434 | CodeGenFunction &CGF, |
| 6435 | const OMPExecutableDirective &D) { |
Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 6436 | assert(!CGF.getLangOpts().OpenMPIsDevice && "Clauses associated with the " |
| 6437 | "teams directive expected to be " |
| 6438 | "emitted only for the host!"); |
| 6439 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6440 | CGBuilderTy &Bld = CGF.Builder; |
Arpith Chacko Jacob | 33c849a | 2017-01-25 00:57:16 +0000 | [diff] [blame] | 6441 | |
| 6442 | // |
| 6443 | // If the target directive is combined with a teams directive: |
| 6444 | // Return the value in the thread_limit clause, if any. |
| 6445 | // |
| 6446 | // If the target directive is combined with a parallel directive: |
| 6447 | // Return the value in the num_threads clause, if any. |
| 6448 | // |
| 6449 | // If both clauses are set, select the minimum of the two. |
| 6450 | // |
| 6451 | // If neither teams or parallel combined directives set the number of threads |
| 6452 | // in a team, return 0 to denote the runtime default. |
| 6453 | // |
| 6454 | // If this is not a teams directive return nullptr. |
| 6455 | |
Arpith Chacko Jacob | 99a1e0e | 2017-01-25 02:18:43 +0000 | [diff] [blame] | 6456 | if (isOpenMPTeamsDirective(D.getDirectiveKind()) || |
| 6457 | isOpenMPParallelDirective(D.getDirectiveKind())) { |
Arpith Chacko Jacob | 33c849a | 2017-01-25 00:57:16 +0000 | [diff] [blame] | 6458 | llvm::Value *DefaultThreadLimitVal = Bld.getInt32(0); |
| 6459 | llvm::Value *NumThreadsVal = nullptr; |
Arpith Chacko Jacob | 99a1e0e | 2017-01-25 02:18:43 +0000 | [diff] [blame] | 6460 | llvm::Value *ThreadLimitVal = nullptr; |
| 6461 | |
| 6462 | if (const auto *ThreadLimitClause = |
| 6463 | D.getSingleClause<OMPThreadLimitClause>()) { |
| 6464 | CodeGenFunction::RunCleanupsScope ThreadLimitScope(CGF); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6465 | llvm::Value *ThreadLimit = |
| 6466 | CGF.EmitScalarExpr(ThreadLimitClause->getThreadLimit(), |
| 6467 | /*IgnoreResultAssign*/ true); |
Arpith Chacko Jacob | 99a1e0e | 2017-01-25 02:18:43 +0000 | [diff] [blame] | 6468 | ThreadLimitVal = Bld.CreateIntCast(ThreadLimit, CGF.Int32Ty, |
| 6469 | /*IsSigned=*/true); |
| 6470 | } |
Arpith Chacko Jacob | 33c849a | 2017-01-25 00:57:16 +0000 | [diff] [blame] | 6471 | |
| 6472 | if (const auto *NumThreadsClause = |
| 6473 | D.getSingleClause<OMPNumThreadsClause>()) { |
| 6474 | CodeGenFunction::RunCleanupsScope NumThreadsScope(CGF); |
| 6475 | llvm::Value *NumThreads = |
| 6476 | CGF.EmitScalarExpr(NumThreadsClause->getNumThreads(), |
| 6477 | /*IgnoreResultAssign*/ true); |
| 6478 | NumThreadsVal = |
| 6479 | Bld.CreateIntCast(NumThreads, CGF.Int32Ty, /*IsSigned=*/true); |
| 6480 | } |
| 6481 | |
Arpith Chacko Jacob | 99a1e0e | 2017-01-25 02:18:43 +0000 | [diff] [blame] | 6482 | // Select the lesser of thread_limit and num_threads. |
| 6483 | if (NumThreadsVal) |
| 6484 | ThreadLimitVal = ThreadLimitVal |
| 6485 | ? Bld.CreateSelect(Bld.CreateICmpSLT(NumThreadsVal, |
| 6486 | ThreadLimitVal), |
| 6487 | NumThreadsVal, ThreadLimitVal) |
| 6488 | : NumThreadsVal; |
Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 6489 | |
Arpith Chacko Jacob | 99a1e0e | 2017-01-25 02:18:43 +0000 | [diff] [blame] | 6490 | // Set default value passed to the runtime if either teams or a target |
| 6491 | // parallel type directive is found but no clause is specified. |
| 6492 | if (!ThreadLimitVal) |
| 6493 | ThreadLimitVal = DefaultThreadLimitVal; |
| 6494 | |
| 6495 | return ThreadLimitVal; |
| 6496 | } |
Arpith Chacko Jacob | 86f9e46 | 2017-01-25 01:45:59 +0000 | [diff] [blame] | 6497 | |
Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 6498 | // If the current target region has a teams region enclosed, we need to get |
| 6499 | // the thread limit to pass to the runtime function call. This is done |
| 6500 | // by generating the expression in a inlined region. This is required because |
| 6501 | // the expression is captured in the enclosing target environment when the |
| 6502 | // teams directive is not combined with target. |
| 6503 | |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 6504 | const CapturedStmt &CS = *D.getCapturedStmt(OMPD_target); |
Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 6505 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6506 | if (const auto *TeamsDir = dyn_cast_or_null<OMPExecutableDirective>( |
Carlo Bertolli | 6eee906 | 2016-04-29 01:37:30 +0000 | [diff] [blame] | 6507 | ignoreCompoundStmts(CS.getCapturedStmt()))) { |
Alexey Bataev | 50a1c78 | 2017-12-01 21:31:08 +0000 | [diff] [blame] | 6508 | if (isOpenMPTeamsDirective(TeamsDir->getDirectiveKind())) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6509 | if (const auto *TLE = TeamsDir->getSingleClause<OMPThreadLimitClause>()) { |
Alexey Bataev | 50a1c78 | 2017-12-01 21:31:08 +0000 | [diff] [blame] | 6510 | CGOpenMPInnerExprInfo CGInfo(CGF, CS); |
| 6511 | CodeGenFunction::CGCapturedStmtRAII CapInfoRAII(CGF, &CGInfo); |
| 6512 | llvm::Value *ThreadLimit = CGF.EmitScalarExpr(TLE->getThreadLimit()); |
| 6513 | return CGF.Builder.CreateIntCast(ThreadLimit, CGF.Int32Ty, |
| 6514 | /*IsSigned=*/true); |
| 6515 | } |
Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 6516 | |
Alexey Bataev | 50a1c78 | 2017-12-01 21:31:08 +0000 | [diff] [blame] | 6517 | // If we have an enclosed teams directive but no thread_limit clause we |
| 6518 | // use the default value 0. |
| 6519 | return CGF.Builder.getInt32(0); |
| 6520 | } |
Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 6521 | } |
| 6522 | |
| 6523 | // No teams associated with the directive. |
| 6524 | return nullptr; |
| 6525 | } |
| 6526 | |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6527 | namespace { |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 6528 | LLVM_ENABLE_BITMASK_ENUMS_IN_NAMESPACE(); |
| 6529 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 6530 | // Utility to handle information from clauses associated with a given |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6531 | // construct that use mappable expressions (e.g. 'map' clause, 'to' clause). |
| 6532 | // It provides a convenient interface to obtain the information and generate |
| 6533 | // code for that information. |
| 6534 | class MappableExprsHandler { |
| 6535 | public: |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 6536 | /// Values for bit flags used to specify the mapping type for |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6537 | /// offloading. |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 6538 | enum OpenMPOffloadMappingFlags : uint64_t { |
| 6539 | /// No flags |
| 6540 | OMP_MAP_NONE = 0x0, |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 6541 | /// Allocate memory on the device and move data from host to device. |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6542 | OMP_MAP_TO = 0x01, |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 6543 | /// Allocate memory on the device and move data from device to host. |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6544 | OMP_MAP_FROM = 0x02, |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 6545 | /// Always perform the requested mapping action on the element, even |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6546 | /// if it was already mapped before. |
| 6547 | OMP_MAP_ALWAYS = 0x04, |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 6548 | /// Delete the element from the device environment, ignoring the |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6549 | /// current reference count associated with the element. |
Samuel Antao | 6782e94 | 2016-05-26 16:48:10 +0000 | [diff] [blame] | 6550 | OMP_MAP_DELETE = 0x08, |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 6551 | /// The element being mapped is a pointer-pointee pair; both the |
George Rokos | 065755d | 2017-11-07 18:27:04 +0000 | [diff] [blame] | 6552 | /// pointer and the pointee should be mapped. |
| 6553 | OMP_MAP_PTR_AND_OBJ = 0x10, |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 6554 | /// This flags signals that the base address of an entry should be |
George Rokos | 065755d | 2017-11-07 18:27:04 +0000 | [diff] [blame] | 6555 | /// passed to the target kernel as an argument. |
| 6556 | OMP_MAP_TARGET_PARAM = 0x20, |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 6557 | /// Signal that the runtime library has to return the device pointer |
George Rokos | 065755d | 2017-11-07 18:27:04 +0000 | [diff] [blame] | 6558 | /// in the current position for the data being mapped. Used when we have the |
| 6559 | /// use_device_ptr clause. |
| 6560 | OMP_MAP_RETURN_PARAM = 0x40, |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 6561 | /// This flag signals that the reference being passed is a pointer to |
Samuel Antao | d486f84 | 2016-05-26 16:53:38 +0000 | [diff] [blame] | 6562 | /// private data. |
George Rokos | 065755d | 2017-11-07 18:27:04 +0000 | [diff] [blame] | 6563 | OMP_MAP_PRIVATE = 0x80, |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 6564 | /// Pass the element to the device by value. |
George Rokos | 065755d | 2017-11-07 18:27:04 +0000 | [diff] [blame] | 6565 | OMP_MAP_LITERAL = 0x100, |
Alexey Bataev | f47c4b4 | 2017-09-26 13:47:31 +0000 | [diff] [blame] | 6566 | /// Implicit map |
| 6567 | OMP_MAP_IMPLICIT = 0x200, |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 6568 | /// The 16 MSBs of the flags indicate whether the entry is member of some |
| 6569 | /// struct/class. |
| 6570 | OMP_MAP_MEMBER_OF = 0xffff000000000000, |
| 6571 | LLVM_MARK_AS_BITMASK_ENUM(/* LargestFlag = */ OMP_MAP_MEMBER_OF), |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6572 | }; |
| 6573 | |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 6574 | /// Class that associates information with a base pointer to be passed to the |
| 6575 | /// runtime library. |
| 6576 | class BasePointerInfo { |
| 6577 | /// The base pointer. |
| 6578 | llvm::Value *Ptr = nullptr; |
| 6579 | /// The base declaration that refers to this device pointer, or null if |
| 6580 | /// there is none. |
| 6581 | const ValueDecl *DevPtrDecl = nullptr; |
| 6582 | |
| 6583 | public: |
| 6584 | BasePointerInfo(llvm::Value *Ptr, const ValueDecl *DevPtrDecl = nullptr) |
| 6585 | : Ptr(Ptr), DevPtrDecl(DevPtrDecl) {} |
| 6586 | llvm::Value *operator*() const { return Ptr; } |
| 6587 | const ValueDecl *getDevicePtrDecl() const { return DevPtrDecl; } |
| 6588 | void setDevicePtrDecl(const ValueDecl *D) { DevPtrDecl = D; } |
| 6589 | }; |
| 6590 | |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 6591 | using MapBaseValuesArrayTy = SmallVector<BasePointerInfo, 4>; |
| 6592 | using MapValuesArrayTy = SmallVector<llvm::Value *, 4>; |
| 6593 | using MapFlagsArrayTy = SmallVector<OpenMPOffloadMappingFlags, 4>; |
| 6594 | |
| 6595 | /// Map between a struct and the its lowest & highest elements which have been |
| 6596 | /// mapped. |
| 6597 | /// [ValueDecl *] --> {LE(FieldIndex, Pointer), |
| 6598 | /// HE(FieldIndex, Pointer)} |
| 6599 | struct StructRangeInfoTy { |
| 6600 | std::pair<unsigned /*FieldIndex*/, Address /*Pointer*/> LowestElem = { |
| 6601 | 0, Address::invalid()}; |
| 6602 | std::pair<unsigned /*FieldIndex*/, Address /*Pointer*/> HighestElem = { |
| 6603 | 0, Address::invalid()}; |
| 6604 | Address Base = Address::invalid(); |
| 6605 | }; |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6606 | |
| 6607 | private: |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 6608 | /// Kind that defines how a device pointer has to be returned. |
| 6609 | struct MapInfo { |
| 6610 | OMPClauseMappableExprCommon::MappableExprComponentListRef Components; |
| 6611 | OpenMPMapClauseKind MapType = OMPC_MAP_unknown; |
| 6612 | OpenMPMapClauseKind MapTypeModifier = OMPC_MAP_unknown; |
| 6613 | bool ReturnDevicePointer = false; |
| 6614 | bool IsImplicit = false; |
| 6615 | |
| 6616 | MapInfo() = default; |
| 6617 | MapInfo( |
| 6618 | OMPClauseMappableExprCommon::MappableExprComponentListRef Components, |
| 6619 | OpenMPMapClauseKind MapType, OpenMPMapClauseKind MapTypeModifier, |
| 6620 | bool ReturnDevicePointer, bool IsImplicit) |
| 6621 | : Components(Components), MapType(MapType), |
| 6622 | MapTypeModifier(MapTypeModifier), |
| 6623 | ReturnDevicePointer(ReturnDevicePointer), IsImplicit(IsImplicit) {} |
| 6624 | }; |
| 6625 | |
| 6626 | /// If use_device_ptr is used on a pointer which is a struct member and there |
| 6627 | /// is no map information about it, then emission of that entry is deferred |
| 6628 | /// until the whole struct has been processed. |
| 6629 | struct DeferredDevicePtrEntryTy { |
| 6630 | const Expr *IE = nullptr; |
| 6631 | const ValueDecl *VD = nullptr; |
| 6632 | |
| 6633 | DeferredDevicePtrEntryTy(const Expr *IE, const ValueDecl *VD) |
| 6634 | : IE(IE), VD(VD) {} |
| 6635 | }; |
| 6636 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 6637 | /// Directive from where the map clauses were extracted. |
Samuel Antao | 44bcdb3 | 2016-07-28 15:31:29 +0000 | [diff] [blame] | 6638 | const OMPExecutableDirective &CurDir; |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6639 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 6640 | /// Function the directive is being generated for. |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6641 | CodeGenFunction &CGF; |
| 6642 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 6643 | /// Set of all first private variables in the current directive. |
Samuel Antao | d486f84 | 2016-05-26 16:53:38 +0000 | [diff] [blame] | 6644 | llvm::SmallPtrSet<const VarDecl *, 8> FirstPrivateDecls; |
| 6645 | |
Samuel Antao | 6890b09 | 2016-07-28 14:25:09 +0000 | [diff] [blame] | 6646 | /// Map between device pointer declarations and their expression components. |
| 6647 | /// The key value for declarations in 'this' is null. |
| 6648 | llvm::DenseMap< |
| 6649 | const ValueDecl *, |
| 6650 | SmallVector<OMPClauseMappableExprCommon::MappableExprComponentListRef, 4>> |
| 6651 | DevPointersMap; |
| 6652 | |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6653 | llvm::Value *getExprTypeSize(const Expr *E) const { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6654 | QualType ExprTy = E->getType().getCanonicalType(); |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6655 | |
| 6656 | // Reference types are ignored for mapping purposes. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6657 | if (const auto *RefTy = ExprTy->getAs<ReferenceType>()) |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6658 | ExprTy = RefTy->getPointeeType().getCanonicalType(); |
| 6659 | |
| 6660 | // Given that an array section is considered a built-in type, we need to |
| 6661 | // do the calculation based on the length of the section instead of relying |
| 6662 | // on CGF.getTypeSize(E->getType()). |
| 6663 | if (const auto *OAE = dyn_cast<OMPArraySectionExpr>(E)) { |
| 6664 | QualType BaseTy = OMPArraySectionExpr::getBaseOriginalType( |
| 6665 | OAE->getBase()->IgnoreParenImpCasts()) |
| 6666 | .getCanonicalType(); |
| 6667 | |
| 6668 | // If there is no length associated with the expression, that means we |
| 6669 | // are using the whole length of the base. |
| 6670 | if (!OAE->getLength() && OAE->getColonLoc().isValid()) |
| 6671 | return CGF.getTypeSize(BaseTy); |
| 6672 | |
| 6673 | llvm::Value *ElemSize; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6674 | if (const auto *PTy = BaseTy->getAs<PointerType>()) { |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6675 | ElemSize = CGF.getTypeSize(PTy->getPointeeType().getCanonicalType()); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6676 | } else { |
| 6677 | const auto *ATy = cast<ArrayType>(BaseTy.getTypePtr()); |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6678 | assert(ATy && "Expecting array type if not a pointer type."); |
| 6679 | ElemSize = CGF.getTypeSize(ATy->getElementType().getCanonicalType()); |
| 6680 | } |
| 6681 | |
| 6682 | // If we don't have a length at this point, that is because we have an |
| 6683 | // array section with a single element. |
| 6684 | if (!OAE->getLength()) |
| 6685 | return ElemSize; |
| 6686 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6687 | llvm::Value *LengthVal = CGF.EmitScalarExpr(OAE->getLength()); |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6688 | LengthVal = |
| 6689 | CGF.Builder.CreateIntCast(LengthVal, CGF.SizeTy, /*isSigned=*/false); |
| 6690 | return CGF.Builder.CreateNUWMul(LengthVal, ElemSize); |
| 6691 | } |
| 6692 | return CGF.getTypeSize(ExprTy); |
| 6693 | } |
| 6694 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 6695 | /// Return the corresponding bits for a given map clause modifier. Add |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6696 | /// 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] | 6697 | /// map as the first one of a series of maps that relate to the same map |
| 6698 | /// expression. |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 6699 | OpenMPOffloadMappingFlags getMapTypeBits(OpenMPMapClauseKind MapType, |
| 6700 | OpenMPMapClauseKind MapTypeModifier, |
| 6701 | bool IsImplicit, bool AddPtrFlag, |
| 6702 | bool AddIsTargetParamFlag) const { |
| 6703 | OpenMPOffloadMappingFlags Bits = |
| 6704 | IsImplicit ? OMP_MAP_IMPLICIT : OMP_MAP_NONE; |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6705 | switch (MapType) { |
| 6706 | case OMPC_MAP_alloc: |
Samuel Antao | 6782e94 | 2016-05-26 16:48:10 +0000 | [diff] [blame] | 6707 | case OMPC_MAP_release: |
| 6708 | // alloc and release is the default behavior in the runtime library, i.e. |
| 6709 | // if we don't pass any bits alloc/release that is what the runtime is |
| 6710 | // going to do. Therefore, we don't need to signal anything for these two |
| 6711 | // type modifiers. |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6712 | break; |
| 6713 | case OMPC_MAP_to: |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 6714 | Bits |= OMP_MAP_TO; |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6715 | break; |
| 6716 | case OMPC_MAP_from: |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 6717 | Bits |= OMP_MAP_FROM; |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6718 | break; |
| 6719 | case OMPC_MAP_tofrom: |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 6720 | Bits |= OMP_MAP_TO | OMP_MAP_FROM; |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6721 | break; |
| 6722 | case OMPC_MAP_delete: |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 6723 | Bits |= OMP_MAP_DELETE; |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6724 | break; |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 6725 | case OMPC_MAP_always: |
| 6726 | case OMPC_MAP_unknown: |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6727 | llvm_unreachable("Unexpected map type!"); |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6728 | } |
| 6729 | if (AddPtrFlag) |
George Rokos | 065755d | 2017-11-07 18:27:04 +0000 | [diff] [blame] | 6730 | Bits |= OMP_MAP_PTR_AND_OBJ; |
| 6731 | if (AddIsTargetParamFlag) |
| 6732 | Bits |= OMP_MAP_TARGET_PARAM; |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6733 | if (MapTypeModifier == OMPC_MAP_always) |
| 6734 | Bits |= OMP_MAP_ALWAYS; |
| 6735 | return Bits; |
| 6736 | } |
| 6737 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 6738 | /// Return true if the provided expression is a final array section. A |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6739 | /// final array section, is one whose length can't be proved to be one. |
| 6740 | bool isFinalArraySectionExpression(const Expr *E) const { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6741 | const auto *OASE = dyn_cast<OMPArraySectionExpr>(E); |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6742 | |
| 6743 | // It is not an array section and therefore not a unity-size one. |
| 6744 | if (!OASE) |
| 6745 | return false; |
| 6746 | |
| 6747 | // An array section with no colon always refer to a single element. |
| 6748 | if (OASE->getColonLoc().isInvalid()) |
| 6749 | return false; |
| 6750 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6751 | const Expr *Length = OASE->getLength(); |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6752 | |
| 6753 | // If we don't have a length we have to check if the array has size 1 |
| 6754 | // for this dimension. Also, we should always expect a length if the |
| 6755 | // base type is pointer. |
| 6756 | if (!Length) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6757 | QualType BaseQTy = OMPArraySectionExpr::getBaseOriginalType( |
| 6758 | OASE->getBase()->IgnoreParenImpCasts()) |
| 6759 | .getCanonicalType(); |
| 6760 | if (const auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr())) |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6761 | return ATy->getSize().getSExtValue() != 1; |
| 6762 | // If we don't have a constant dimension length, we have to consider |
| 6763 | // the current section as having any size, so it is not necessarily |
| 6764 | // unitary. If it happen to be unity size, that's user fault. |
| 6765 | return true; |
| 6766 | } |
| 6767 | |
| 6768 | // Check if the length evaluates to 1. |
| 6769 | llvm::APSInt ConstLength; |
| 6770 | if (!Length->EvaluateAsInt(ConstLength, CGF.getContext())) |
| 6771 | return true; // Can have more that size 1. |
| 6772 | |
| 6773 | return ConstLength.getSExtValue() != 1; |
| 6774 | } |
| 6775 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 6776 | /// Generate the base pointers, section pointers, sizes and map type |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6777 | /// bits for the provided map type, map modifier, and expression components. |
| 6778 | /// \a IsFirstComponent should be set to true if the provided set of |
| 6779 | /// components is the first associated with a capture. |
| 6780 | void generateInfoForComponentList( |
| 6781 | OpenMPMapClauseKind MapType, OpenMPMapClauseKind MapTypeModifier, |
| 6782 | OMPClauseMappableExprCommon::MappableExprComponentListRef Components, |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 6783 | MapBaseValuesArrayTy &BasePointers, MapValuesArrayTy &Pointers, |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6784 | MapValuesArrayTy &Sizes, MapFlagsArrayTy &Types, |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 6785 | StructRangeInfoTy &PartialStruct, bool IsFirstComponentList, |
Alexey Bataev | e82445f | 2018-09-20 13:54:02 +0000 | [diff] [blame] | 6786 | bool IsImplicit, |
| 6787 | ArrayRef<OMPClauseMappableExprCommon::MappableExprComponentListRef> |
| 6788 | OverlappedElements = llvm::None) const { |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6789 | // 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] | 6790 | // types below. The generated information is expressed in this order: |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6791 | // base pointer, section pointer, size, flags |
| 6792 | // (to add to the ones that come from the map type and modifier). |
| 6793 | // |
| 6794 | // double d; |
| 6795 | // int i[100]; |
| 6796 | // float *p; |
| 6797 | // |
| 6798 | // struct S1 { |
| 6799 | // int i; |
| 6800 | // float f[50]; |
| 6801 | // } |
| 6802 | // struct S2 { |
| 6803 | // int i; |
| 6804 | // float f[50]; |
| 6805 | // S1 s; |
| 6806 | // double *p; |
| 6807 | // struct S2 *ps; |
| 6808 | // } |
| 6809 | // S2 s; |
| 6810 | // S2 *ps; |
| 6811 | // |
| 6812 | // map(d) |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 6813 | // &d, &d, sizeof(double), TARGET_PARAM | TO | FROM |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6814 | // |
| 6815 | // map(i) |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 6816 | // &i, &i, 100*sizeof(int), TARGET_PARAM | TO | FROM |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6817 | // |
| 6818 | // map(i[1:23]) |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 6819 | // &i(=&i[0]), &i[1], 23*sizeof(int), TARGET_PARAM | TO | FROM |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6820 | // |
| 6821 | // map(p) |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 6822 | // &p, &p, sizeof(float*), TARGET_PARAM | TO | FROM |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6823 | // |
| 6824 | // map(p[1:24]) |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 6825 | // p, &p[1], 24*sizeof(float), TARGET_PARAM | TO | FROM |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6826 | // |
| 6827 | // map(s) |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 6828 | // &s, &s, sizeof(S2), TARGET_PARAM | TO | FROM |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6829 | // |
| 6830 | // map(s.i) |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 6831 | // &s, &(s.i), sizeof(int), TARGET_PARAM | TO | FROM |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6832 | // |
| 6833 | // map(s.s.f) |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 6834 | // &s, &(s.s.f[0]), 50*sizeof(float), TARGET_PARAM | TO | FROM |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6835 | // |
| 6836 | // map(s.p) |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 6837 | // &s, &(s.p), sizeof(double*), TARGET_PARAM | TO | FROM |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6838 | // |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 6839 | // map(to: s.p[:22]) |
| 6840 | // &s, &(s.p), sizeof(double*), TARGET_PARAM (*) |
| 6841 | // &s, &(s.p), sizeof(double*), MEMBER_OF(1) (**) |
| 6842 | // &(s.p), &(s.p[0]), 22*sizeof(double), |
| 6843 | // MEMBER_OF(1) | PTR_AND_OBJ | TO (***) |
| 6844 | // (*) alloc space for struct members, only this is a target parameter |
| 6845 | // (**) map the pointer (nothing to be mapped in this example) (the compiler |
| 6846 | // optimizes this entry out, same in the examples below) |
| 6847 | // (***) map the pointee (map: to) |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6848 | // |
| 6849 | // map(s.ps) |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 6850 | // &s, &(s.ps), sizeof(S2*), TARGET_PARAM | TO | FROM |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6851 | // |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 6852 | // map(from: s.ps->s.i) |
| 6853 | // &s, &(s.ps), sizeof(S2*), TARGET_PARAM |
| 6854 | // &s, &(s.ps), sizeof(S2*), MEMBER_OF(1) |
| 6855 | // &(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] | 6856 | // |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 6857 | // map(to: s.ps->ps) |
| 6858 | // &s, &(s.ps), sizeof(S2*), TARGET_PARAM |
| 6859 | // &s, &(s.ps), sizeof(S2*), MEMBER_OF(1) |
| 6860 | // &(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] | 6861 | // |
| 6862 | // map(s.ps->ps->ps) |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 6863 | // &s, &(s.ps), sizeof(S2*), TARGET_PARAM |
| 6864 | // &s, &(s.ps), sizeof(S2*), MEMBER_OF(1) |
| 6865 | // &(s.ps), &(s.ps->ps), sizeof(S2*), MEMBER_OF(1) | PTR_AND_OBJ |
| 6866 | // &(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] | 6867 | // |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 6868 | // map(to: s.ps->ps->s.f[:22]) |
| 6869 | // &s, &(s.ps), sizeof(S2*), TARGET_PARAM |
| 6870 | // &s, &(s.ps), sizeof(S2*), MEMBER_OF(1) |
| 6871 | // &(s.ps), &(s.ps->ps), sizeof(S2*), MEMBER_OF(1) | PTR_AND_OBJ |
| 6872 | // &(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] | 6873 | // |
| 6874 | // map(ps) |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 6875 | // &ps, &ps, sizeof(S2*), TARGET_PARAM | TO | FROM |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6876 | // |
| 6877 | // map(ps->i) |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 6878 | // ps, &(ps->i), sizeof(int), TARGET_PARAM | TO | FROM |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6879 | // |
| 6880 | // map(ps->s.f) |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 6881 | // ps, &(ps->s.f[0]), 50*sizeof(float), TARGET_PARAM | TO | FROM |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6882 | // |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 6883 | // map(from: ps->p) |
| 6884 | // ps, &(ps->p), sizeof(double*), TARGET_PARAM | 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: ps->p[:22]) |
| 6887 | // ps, &(ps->p), sizeof(double*), TARGET_PARAM |
| 6888 | // ps, &(ps->p), sizeof(double*), MEMBER_OF(1) |
| 6889 | // &(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] | 6890 | // |
| 6891 | // map(ps->ps) |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 6892 | // ps, &(ps->ps), sizeof(S2*), TARGET_PARAM | TO | FROM |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6893 | // |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 6894 | // map(from: ps->ps->s.i) |
| 6895 | // ps, &(ps->ps), sizeof(S2*), TARGET_PARAM |
| 6896 | // ps, &(ps->ps), sizeof(S2*), MEMBER_OF(1) |
| 6897 | // &(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] | 6898 | // |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 6899 | // map(from: ps->ps->ps) |
| 6900 | // ps, &(ps->ps), sizeof(S2*), TARGET_PARAM |
| 6901 | // ps, &(ps->ps), sizeof(S2*), MEMBER_OF(1) |
| 6902 | // &(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] | 6903 | // |
| 6904 | // map(ps->ps->ps->ps) |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 6905 | // ps, &(ps->ps), sizeof(S2*), TARGET_PARAM |
| 6906 | // ps, &(ps->ps), sizeof(S2*), MEMBER_OF(1) |
| 6907 | // &(ps->ps), &(ps->ps->ps), sizeof(S2*), MEMBER_OF(1) | PTR_AND_OBJ |
| 6908 | // &(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] | 6909 | // |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 6910 | // map(to: ps->ps->ps->s.f[:22]) |
| 6911 | // ps, &(ps->ps), sizeof(S2*), TARGET_PARAM |
| 6912 | // ps, &(ps->ps), sizeof(S2*), MEMBER_OF(1) |
| 6913 | // &(ps->ps), &(ps->ps->ps), sizeof(S2*), MEMBER_OF(1) | PTR_AND_OBJ |
| 6914 | // &(ps->ps->ps), &(ps->ps->ps->s.f[0]), 22*sizeof(float), PTR_AND_OBJ | TO |
| 6915 | // |
| 6916 | // map(to: s.f[:22]) map(from: s.p[:33]) |
| 6917 | // &s, &(s.f[0]), 50*sizeof(float) + sizeof(struct S1) + |
| 6918 | // sizeof(double*) (**), TARGET_PARAM |
| 6919 | // &s, &(s.f[0]), 22*sizeof(float), MEMBER_OF(1) | TO |
| 6920 | // &s, &(s.p), sizeof(double*), MEMBER_OF(1) |
| 6921 | // &(s.p), &(s.p[0]), 33*sizeof(double), MEMBER_OF(1) | PTR_AND_OBJ | FROM |
| 6922 | // (*) allocate contiguous space needed to fit all mapped members even if |
| 6923 | // we allocate space for members not mapped (in this example, |
| 6924 | // s.f[22..49] and s.s are not mapped, yet we must allocate space for |
| 6925 | // them as well because they fall between &s.f[0] and &s.p) |
| 6926 | // |
| 6927 | // map(from: s.f[:22]) map(to: ps->p[:33]) |
| 6928 | // &s, &(s.f[0]), 22*sizeof(float), TARGET_PARAM | FROM |
| 6929 | // ps, &(ps->p), sizeof(S2*), TARGET_PARAM |
| 6930 | // ps, &(ps->p), sizeof(double*), MEMBER_OF(2) (*) |
| 6931 | // &(ps->p), &(ps->p[0]), 33*sizeof(double), MEMBER_OF(2) | PTR_AND_OBJ | TO |
| 6932 | // (*) the struct this entry pertains to is the 2nd element in the list of |
| 6933 | // arguments, hence MEMBER_OF(2) |
| 6934 | // |
| 6935 | // map(from: s.f[:22], s.s) map(to: ps->p[:33]) |
| 6936 | // &s, &(s.f[0]), 50*sizeof(float) + sizeof(struct S1), TARGET_PARAM |
| 6937 | // &s, &(s.f[0]), 22*sizeof(float), MEMBER_OF(1) | FROM |
| 6938 | // &s, &(s.s), sizeof(struct S1), MEMBER_OF(1) | FROM |
| 6939 | // ps, &(ps->p), sizeof(S2*), TARGET_PARAM |
| 6940 | // ps, &(ps->p), sizeof(double*), MEMBER_OF(4) (*) |
| 6941 | // &(ps->p), &(ps->p[0]), 33*sizeof(double), MEMBER_OF(4) | PTR_AND_OBJ | TO |
| 6942 | // (*) the struct this entry pertains to is the 4th element in the list |
| 6943 | // of arguments, hence MEMBER_OF(4) |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6944 | |
| 6945 | // Track if the map information being generated is the first for a capture. |
| 6946 | bool IsCaptureFirstInfo = IsFirstComponentList; |
Alexey Bataev | 92327c5 | 2018-03-26 16:40:55 +0000 | [diff] [blame] | 6947 | bool IsLink = false; // Is this variable a "declare target link"? |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6948 | |
| 6949 | // Scan the components from the base to the complete expression. |
| 6950 | auto CI = Components.rbegin(); |
| 6951 | auto CE = Components.rend(); |
| 6952 | auto I = CI; |
| 6953 | |
| 6954 | // Track if the map information being generated is the first for a list of |
| 6955 | // components. |
| 6956 | bool IsExpressionFirstInfo = true; |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 6957 | Address BP = Address::invalid(); |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6958 | |
Erich Keane | e69755a | 2018-07-19 17:19:16 +0000 | [diff] [blame] | 6959 | if (isa<MemberExpr>(I->getAssociatedExpression())) { |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6960 | // The base is the 'this' pointer. The content of the pointer is going |
| 6961 | // to be the base of the field being mapped. |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 6962 | BP = CGF.LoadCXXThisAddress(); |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6963 | } else { |
| 6964 | // The base is the reference to the variable. |
| 6965 | // BP = &Var. |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 6966 | BP = CGF.EmitOMPSharedLValue(I->getAssociatedExpression()).getAddress(); |
Alexey Bataev | 92327c5 | 2018-03-26 16:40:55 +0000 | [diff] [blame] | 6967 | if (const auto *VD = |
| 6968 | dyn_cast_or_null<VarDecl>(I->getAssociatedDeclaration())) { |
| 6969 | if (llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res = |
Alexey Bataev | 97b7221 | 2018-08-14 18:31:20 +0000 | [diff] [blame] | 6970 | OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD)) |
Alexey Bataev | 2c1dffe | 2018-04-16 20:34:41 +0000 | [diff] [blame] | 6971 | if (*Res == OMPDeclareTargetDeclAttr::MT_Link) { |
| 6972 | IsLink = true; |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 6973 | BP = CGF.CGM.getOpenMPRuntime().getAddrOfDeclareTargetLink(VD); |
Alexey Bataev | 2c1dffe | 2018-04-16 20:34:41 +0000 | [diff] [blame] | 6974 | } |
Alexey Bataev | 92327c5 | 2018-03-26 16:40:55 +0000 | [diff] [blame] | 6975 | } |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6976 | |
| 6977 | // 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] | 6978 | // 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] | 6979 | // reference. References are ignored for mapping purposes. |
| 6980 | QualType Ty = |
| 6981 | I->getAssociatedDeclaration()->getType().getNonReferenceType(); |
| 6982 | if (Ty->isAnyPointerType() && std::next(I) != CE) { |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 6983 | BP = CGF.EmitLoadOfPointer(BP, Ty->castAs<PointerType>()); |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6984 | |
| 6985 | // We do not need to generate individual map information for the |
| 6986 | // pointer, it can be associated with the combined storage. |
| 6987 | ++I; |
| 6988 | } |
| 6989 | } |
| 6990 | |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 6991 | // Track whether a component of the list should be marked as MEMBER_OF some |
| 6992 | // combined entry (for partial structs). Only the first PTR_AND_OBJ entry |
| 6993 | // in a component list should be marked as MEMBER_OF, all subsequent entries |
| 6994 | // do not belong to the base struct. E.g. |
| 6995 | // struct S2 s; |
| 6996 | // s.ps->ps->ps->f[:] |
| 6997 | // (1) (2) (3) (4) |
| 6998 | // ps(1) is a member pointer, ps(2) is a pointee of ps(1), so it is a |
| 6999 | // PTR_AND_OBJ entry; the PTR is ps(1), so MEMBER_OF the base struct. ps(3) |
| 7000 | // is the pointee of ps(2) which is not member of struct s, so it should not |
| 7001 | // be marked as such (it is still PTR_AND_OBJ). |
| 7002 | // The variable is initialized to false so that PTR_AND_OBJ entries which |
| 7003 | // are not struct members are not considered (e.g. array of pointers to |
| 7004 | // data). |
| 7005 | bool ShouldBeMemberOf = false; |
| 7006 | |
| 7007 | // Variable keeping track of whether or not we have encountered a component |
| 7008 | // in the component list which is a member expression. Useful when we have a |
| 7009 | // pointer or a final array section, in which case it is the previous |
| 7010 | // component in the list which tells us whether we have a member expression. |
| 7011 | // E.g. X.f[:] |
| 7012 | // While processing the final array section "[:]" it is "f" which tells us |
| 7013 | // whether we are dealing with a member of a declared struct. |
| 7014 | const MemberExpr *EncounteredME = nullptr; |
| 7015 | |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7016 | for (; I != CE; ++I) { |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7017 | // If the current component is member of a struct (parent struct) mark it. |
| 7018 | if (!EncounteredME) { |
| 7019 | EncounteredME = dyn_cast<MemberExpr>(I->getAssociatedExpression()); |
| 7020 | // If we encounter a PTR_AND_OBJ entry from now on it should be marked |
| 7021 | // as MEMBER_OF the parent struct. |
| 7022 | if (EncounteredME) |
| 7023 | ShouldBeMemberOf = true; |
| 7024 | } |
| 7025 | |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7026 | auto Next = std::next(I); |
| 7027 | |
| 7028 | // We need to generate the addresses and sizes if this is the last |
| 7029 | // component, if the component is a pointer or if it is an array section |
| 7030 | // whose length can't be proved to be one. If this is a pointer, it |
| 7031 | // becomes the base address for the following components. |
| 7032 | |
| 7033 | // A final array section, is one whose length can't be proved to be one. |
| 7034 | bool IsFinalArraySection = |
| 7035 | isFinalArraySectionExpression(I->getAssociatedExpression()); |
| 7036 | |
| 7037 | // Get information on whether the element is a pointer. Have to do a |
| 7038 | // special treatment for array sections given that they are built-in |
| 7039 | // types. |
| 7040 | const auto *OASE = |
| 7041 | dyn_cast<OMPArraySectionExpr>(I->getAssociatedExpression()); |
| 7042 | bool IsPointer = |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7043 | (OASE && OMPArraySectionExpr::getBaseOriginalType(OASE) |
| 7044 | .getCanonicalType() |
| 7045 | ->isAnyPointerType()) || |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7046 | I->getAssociatedExpression()->getType()->isAnyPointerType(); |
| 7047 | |
| 7048 | if (Next == CE || IsPointer || IsFinalArraySection) { |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7049 | // If this is not the last component, we expect the pointer to be |
| 7050 | // associated with an array expression or member expression. |
| 7051 | assert((Next == CE || |
| 7052 | isa<MemberExpr>(Next->getAssociatedExpression()) || |
| 7053 | isa<ArraySubscriptExpr>(Next->getAssociatedExpression()) || |
| 7054 | isa<OMPArraySectionExpr>(Next->getAssociatedExpression())) && |
| 7055 | "Unexpected expression"); |
| 7056 | |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7057 | Address LB = |
| 7058 | CGF.EmitOMPSharedLValue(I->getAssociatedExpression()).getAddress(); |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7059 | |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7060 | // If this component is a pointer inside the base struct then we don't |
| 7061 | // need to create any entry for it - it will be combined with the object |
| 7062 | // it is pointing to into a single PTR_AND_OBJ entry. |
| 7063 | bool IsMemberPointer = |
| 7064 | IsPointer && EncounteredME && |
| 7065 | (dyn_cast<MemberExpr>(I->getAssociatedExpression()) == |
| 7066 | EncounteredME); |
Alexey Bataev | e82445f | 2018-09-20 13:54:02 +0000 | [diff] [blame] | 7067 | if (!OverlappedElements.empty()) { |
| 7068 | // Handle base element with the info for overlapped elements. |
| 7069 | assert(!PartialStruct.Base.isValid() && "The base element is set."); |
| 7070 | assert(Next == CE && |
| 7071 | "Expected last element for the overlapped elements."); |
| 7072 | assert(!IsPointer && |
| 7073 | "Unexpected base element with the pointer type."); |
| 7074 | // Mark the whole struct as the struct that requires allocation on the |
| 7075 | // device. |
| 7076 | PartialStruct.LowestElem = {0, LB}; |
| 7077 | CharUnits TypeSize = CGF.getContext().getTypeSizeInChars( |
| 7078 | I->getAssociatedExpression()->getType()); |
| 7079 | Address HB = CGF.Builder.CreateConstGEP( |
| 7080 | CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(LB, |
| 7081 | CGF.VoidPtrTy), |
| 7082 | TypeSize.getQuantity() - 1, CharUnits::One()); |
| 7083 | PartialStruct.HighestElem = { |
| 7084 | std::numeric_limits<decltype( |
| 7085 | PartialStruct.HighestElem.first)>::max(), |
| 7086 | HB}; |
| 7087 | PartialStruct.Base = BP; |
| 7088 | // Emit data for non-overlapped data. |
| 7089 | OpenMPOffloadMappingFlags Flags = |
| 7090 | OMP_MAP_MEMBER_OF | |
| 7091 | getMapTypeBits(MapType, MapTypeModifier, IsImplicit, |
| 7092 | /*AddPtrFlag=*/false, |
| 7093 | /*AddIsTargetParamFlag=*/false); |
| 7094 | LB = BP; |
| 7095 | llvm::Value *Size = nullptr; |
| 7096 | // Do bitcopy of all non-overlapped structure elements. |
| 7097 | for (OMPClauseMappableExprCommon::MappableExprComponentListRef |
| 7098 | Component : OverlappedElements) { |
| 7099 | Address ComponentLB = Address::invalid(); |
| 7100 | for (const OMPClauseMappableExprCommon::MappableComponent &MC : |
| 7101 | Component) { |
| 7102 | if (MC.getAssociatedDeclaration()) { |
| 7103 | ComponentLB = |
| 7104 | CGF.EmitOMPSharedLValue(MC.getAssociatedExpression()) |
| 7105 | .getAddress(); |
| 7106 | Size = CGF.Builder.CreatePtrDiff( |
| 7107 | CGF.EmitCastToVoidPtr(ComponentLB.getPointer()), |
| 7108 | CGF.EmitCastToVoidPtr(LB.getPointer())); |
| 7109 | break; |
| 7110 | } |
| 7111 | } |
| 7112 | BasePointers.push_back(BP.getPointer()); |
| 7113 | Pointers.push_back(LB.getPointer()); |
| 7114 | Sizes.push_back(Size); |
| 7115 | Types.push_back(Flags); |
| 7116 | LB = CGF.Builder.CreateConstGEP(ComponentLB, 1, |
| 7117 | CGF.getPointerSize()); |
| 7118 | } |
| 7119 | BasePointers.push_back(BP.getPointer()); |
| 7120 | Pointers.push_back(LB.getPointer()); |
| 7121 | Size = CGF.Builder.CreatePtrDiff( |
| 7122 | CGF.EmitCastToVoidPtr( |
| 7123 | CGF.Builder.CreateConstGEP(HB, 1, CharUnits::One()) |
| 7124 | .getPointer()), |
| 7125 | CGF.EmitCastToVoidPtr(LB.getPointer())); |
| 7126 | Sizes.push_back(Size); |
| 7127 | Types.push_back(Flags); |
| 7128 | break; |
| 7129 | } |
| 7130 | llvm::Value *Size = getExprTypeSize(I->getAssociatedExpression()); |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7131 | if (!IsMemberPointer) { |
| 7132 | BasePointers.push_back(BP.getPointer()); |
| 7133 | Pointers.push_back(LB.getPointer()); |
| 7134 | Sizes.push_back(Size); |
Samuel Antao | 03a3cec | 2016-07-27 22:52:16 +0000 | [diff] [blame] | 7135 | |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7136 | // We need to add a pointer flag for each map that comes from the |
| 7137 | // same expression except for the first one. We also need to signal |
| 7138 | // this map is the first one that relates with the current capture |
| 7139 | // (there is a set of entries for each capture). |
| 7140 | OpenMPOffloadMappingFlags Flags = getMapTypeBits( |
| 7141 | MapType, MapTypeModifier, IsImplicit, |
| 7142 | !IsExpressionFirstInfo || IsLink, IsCaptureFirstInfo && !IsLink); |
| 7143 | |
| 7144 | if (!IsExpressionFirstInfo) { |
| 7145 | // If we have a PTR_AND_OBJ pair where the OBJ is a pointer as well, |
| 7146 | // then we reset the TO/FROM/ALWAYS/DELETE flags. |
| 7147 | if (IsPointer) |
| 7148 | Flags &= ~(OMP_MAP_TO | OMP_MAP_FROM | OMP_MAP_ALWAYS | |
| 7149 | OMP_MAP_DELETE); |
| 7150 | |
| 7151 | if (ShouldBeMemberOf) { |
| 7152 | // Set placeholder value MEMBER_OF=FFFF to indicate that the flag |
| 7153 | // should be later updated with the correct value of MEMBER_OF. |
| 7154 | Flags |= OMP_MAP_MEMBER_OF; |
| 7155 | // From now on, all subsequent PTR_AND_OBJ entries should not be |
| 7156 | // marked as MEMBER_OF. |
| 7157 | ShouldBeMemberOf = false; |
| 7158 | } |
| 7159 | } |
| 7160 | |
| 7161 | Types.push_back(Flags); |
Samuel Antao | 03a3cec | 2016-07-27 22:52:16 +0000 | [diff] [blame] | 7162 | } |
| 7163 | |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7164 | // If we have encountered a member expression so far, keep track of the |
| 7165 | // mapped member. If the parent is "*this", then the value declaration |
| 7166 | // is nullptr. |
| 7167 | if (EncounteredME) { |
| 7168 | const auto *FD = dyn_cast<FieldDecl>(EncounteredME->getMemberDecl()); |
| 7169 | unsigned FieldIndex = FD->getFieldIndex(); |
Samuel Antao | 03a3cec | 2016-07-27 22:52:16 +0000 | [diff] [blame] | 7170 | |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7171 | // Update info about the lowest and highest elements for this struct |
| 7172 | if (!PartialStruct.Base.isValid()) { |
| 7173 | PartialStruct.LowestElem = {FieldIndex, LB}; |
| 7174 | PartialStruct.HighestElem = {FieldIndex, LB}; |
| 7175 | PartialStruct.Base = BP; |
| 7176 | } else if (FieldIndex < PartialStruct.LowestElem.first) { |
| 7177 | PartialStruct.LowestElem = {FieldIndex, LB}; |
| 7178 | } else if (FieldIndex > PartialStruct.HighestElem.first) { |
| 7179 | PartialStruct.HighestElem = {FieldIndex, LB}; |
| 7180 | } |
| 7181 | } |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7182 | |
| 7183 | // If we have a final array section, we are done with this expression. |
| 7184 | if (IsFinalArraySection) |
| 7185 | break; |
| 7186 | |
| 7187 | // The pointer becomes the base for the next element. |
| 7188 | if (Next != CE) |
| 7189 | BP = LB; |
| 7190 | |
| 7191 | IsExpressionFirstInfo = false; |
| 7192 | IsCaptureFirstInfo = false; |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7193 | } |
| 7194 | } |
| 7195 | } |
| 7196 | |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7197 | /// Return the adjusted map modifiers if the declaration a capture refers to |
| 7198 | /// appears in a first-private clause. This is expected to be used only with |
| 7199 | /// directives that start with 'target'. |
| 7200 | MappableExprsHandler::OpenMPOffloadMappingFlags |
| 7201 | getMapModifiersForPrivateClauses(const CapturedStmt::Capture &Cap) const { |
| 7202 | assert(Cap.capturesVariable() && "Expected capture by reference only!"); |
| 7203 | |
| 7204 | // A first private variable captured by reference will use only the |
| 7205 | // 'private ptr' and 'map to' flag. Return the right flags if the captured |
| 7206 | // declaration is known as first-private in this handler. |
| 7207 | if (FirstPrivateDecls.count(Cap.getCapturedVar())) |
| 7208 | return MappableExprsHandler::OMP_MAP_PRIVATE | |
| 7209 | MappableExprsHandler::OMP_MAP_TO; |
| 7210 | return MappableExprsHandler::OMP_MAP_TO | |
| 7211 | MappableExprsHandler::OMP_MAP_FROM; |
| 7212 | } |
| 7213 | |
| 7214 | static OpenMPOffloadMappingFlags getMemberOfFlag(unsigned Position) { |
| 7215 | // Member of is given by the 16 MSB of the flag, so rotate by 48 bits. |
| 7216 | return static_cast<OpenMPOffloadMappingFlags>(((uint64_t)Position + 1) |
| 7217 | << 48); |
| 7218 | } |
| 7219 | |
| 7220 | static void setCorrectMemberOfFlag(OpenMPOffloadMappingFlags &Flags, |
| 7221 | OpenMPOffloadMappingFlags MemberOfFlag) { |
| 7222 | // If the entry is PTR_AND_OBJ but has not been marked with the special |
| 7223 | // placeholder value 0xFFFF in the MEMBER_OF field, then it should not be |
| 7224 | // marked as MEMBER_OF. |
| 7225 | if ((Flags & OMP_MAP_PTR_AND_OBJ) && |
| 7226 | ((Flags & OMP_MAP_MEMBER_OF) != OMP_MAP_MEMBER_OF)) |
| 7227 | return; |
| 7228 | |
| 7229 | // Reset the placeholder value to prepare the flag for the assignment of the |
| 7230 | // proper MEMBER_OF value. |
| 7231 | Flags &= ~OMP_MAP_MEMBER_OF; |
| 7232 | Flags |= MemberOfFlag; |
| 7233 | } |
| 7234 | |
Alexey Bataev | e82445f | 2018-09-20 13:54:02 +0000 | [diff] [blame] | 7235 | void getPlainLayout(const CXXRecordDecl *RD, |
| 7236 | llvm::SmallVectorImpl<const FieldDecl *> &Layout, |
| 7237 | bool AsBase) const { |
| 7238 | const CGRecordLayout &RL = CGF.getTypes().getCGRecordLayout(RD); |
| 7239 | |
| 7240 | llvm::StructType *St = |
| 7241 | AsBase ? RL.getBaseSubobjectLLVMType() : RL.getLLVMType(); |
| 7242 | |
| 7243 | unsigned NumElements = St->getNumElements(); |
| 7244 | llvm::SmallVector< |
| 7245 | llvm::PointerUnion<const CXXRecordDecl *, const FieldDecl *>, 4> |
| 7246 | RecordLayout(NumElements); |
| 7247 | |
| 7248 | // Fill bases. |
| 7249 | for (const auto &I : RD->bases()) { |
| 7250 | if (I.isVirtual()) |
| 7251 | continue; |
| 7252 | const auto *Base = I.getType()->getAsCXXRecordDecl(); |
| 7253 | // Ignore empty bases. |
| 7254 | if (Base->isEmpty() || CGF.getContext() |
| 7255 | .getASTRecordLayout(Base) |
| 7256 | .getNonVirtualSize() |
| 7257 | .isZero()) |
| 7258 | continue; |
| 7259 | |
| 7260 | unsigned FieldIndex = RL.getNonVirtualBaseLLVMFieldNo(Base); |
| 7261 | RecordLayout[FieldIndex] = Base; |
| 7262 | } |
| 7263 | // Fill in virtual bases. |
| 7264 | for (const auto &I : RD->vbases()) { |
| 7265 | const auto *Base = I.getType()->getAsCXXRecordDecl(); |
| 7266 | // Ignore empty bases. |
| 7267 | if (Base->isEmpty()) |
| 7268 | continue; |
| 7269 | unsigned FieldIndex = RL.getVirtualBaseIndex(Base); |
| 7270 | if (RecordLayout[FieldIndex]) |
| 7271 | continue; |
| 7272 | RecordLayout[FieldIndex] = Base; |
| 7273 | } |
| 7274 | // Fill in all the fields. |
| 7275 | assert(!RD->isUnion() && "Unexpected union."); |
| 7276 | for (const auto *Field : RD->fields()) { |
| 7277 | // Fill in non-bitfields. (Bitfields always use a zero pattern, which we |
| 7278 | // will fill in later.) |
| 7279 | if (!Field->isBitField()) { |
| 7280 | unsigned FieldIndex = RL.getLLVMFieldNo(Field); |
| 7281 | RecordLayout[FieldIndex] = Field; |
| 7282 | } |
| 7283 | } |
| 7284 | for (const llvm::PointerUnion<const CXXRecordDecl *, const FieldDecl *> |
| 7285 | &Data : RecordLayout) { |
| 7286 | if (Data.isNull()) |
| 7287 | continue; |
| 7288 | if (const auto *Base = Data.dyn_cast<const CXXRecordDecl *>()) |
| 7289 | getPlainLayout(Base, Layout, /*AsBase=*/true); |
| 7290 | else |
| 7291 | Layout.push_back(Data.get<const FieldDecl *>()); |
| 7292 | } |
| 7293 | } |
| 7294 | |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7295 | public: |
| 7296 | MappableExprsHandler(const OMPExecutableDirective &Dir, CodeGenFunction &CGF) |
| 7297 | : CurDir(Dir), CGF(CGF) { |
| 7298 | // Extract firstprivate clause information. |
| 7299 | for (const auto *C : Dir.getClausesOfKind<OMPFirstprivateClause>()) |
| 7300 | for (const auto *D : C->varlists()) |
| 7301 | FirstPrivateDecls.insert( |
| 7302 | cast<VarDecl>(cast<DeclRefExpr>(D)->getDecl())->getCanonicalDecl()); |
| 7303 | // Extract device pointer clause information. |
| 7304 | for (const auto *C : Dir.getClausesOfKind<OMPIsDevicePtrClause>()) |
| 7305 | for (auto L : C->component_lists()) |
| 7306 | DevPointersMap[L.first].push_back(L.second); |
| 7307 | } |
| 7308 | |
| 7309 | /// Generate code for the combined entry if we have a partially mapped struct |
| 7310 | /// and take care of the mapping flags of the arguments corresponding to |
| 7311 | /// individual struct members. |
| 7312 | void emitCombinedEntry(MapBaseValuesArrayTy &BasePointers, |
| 7313 | MapValuesArrayTy &Pointers, MapValuesArrayTy &Sizes, |
| 7314 | MapFlagsArrayTy &Types, MapFlagsArrayTy &CurTypes, |
| 7315 | const StructRangeInfoTy &PartialStruct) const { |
| 7316 | // Base is the base of the struct |
| 7317 | BasePointers.push_back(PartialStruct.Base.getPointer()); |
| 7318 | // Pointer is the address of the lowest element |
| 7319 | llvm::Value *LB = PartialStruct.LowestElem.second.getPointer(); |
| 7320 | Pointers.push_back(LB); |
| 7321 | // Size is (addr of {highest+1} element) - (addr of lowest element) |
| 7322 | llvm::Value *HB = PartialStruct.HighestElem.second.getPointer(); |
| 7323 | llvm::Value *HAddr = CGF.Builder.CreateConstGEP1_32(HB, /*Idx0=*/1); |
| 7324 | llvm::Value *CLAddr = CGF.Builder.CreatePointerCast(LB, CGF.VoidPtrTy); |
| 7325 | llvm::Value *CHAddr = CGF.Builder.CreatePointerCast(HAddr, CGF.VoidPtrTy); |
| 7326 | llvm::Value *Diff = CGF.Builder.CreatePtrDiff(CHAddr, CLAddr); |
| 7327 | llvm::Value *Size = CGF.Builder.CreateIntCast(Diff, CGF.SizeTy, |
| 7328 | /*isSinged=*/false); |
| 7329 | Sizes.push_back(Size); |
| 7330 | // Map type is always TARGET_PARAM |
| 7331 | Types.push_back(OMP_MAP_TARGET_PARAM); |
| 7332 | // Remove TARGET_PARAM flag from the first element |
| 7333 | (*CurTypes.begin()) &= ~OMP_MAP_TARGET_PARAM; |
| 7334 | |
| 7335 | // All other current entries will be MEMBER_OF the combined entry |
| 7336 | // (except for PTR_AND_OBJ entries which do not have a placeholder value |
| 7337 | // 0xFFFF in the MEMBER_OF field). |
| 7338 | OpenMPOffloadMappingFlags MemberOfFlag = |
| 7339 | getMemberOfFlag(BasePointers.size() - 1); |
| 7340 | for (auto &M : CurTypes) |
| 7341 | setCorrectMemberOfFlag(M, MemberOfFlag); |
| 7342 | } |
| 7343 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 7344 | /// Generate all the base pointers, section pointers, sizes and map |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7345 | /// types for the extracted mappable expressions. Also, for each item that |
| 7346 | /// relates with a device pointer, a pair of the relevant declaration and |
| 7347 | /// index where it occurs is appended to the device pointers info array. |
| 7348 | void generateAllInfo(MapBaseValuesArrayTy &BasePointers, |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7349 | MapValuesArrayTy &Pointers, MapValuesArrayTy &Sizes, |
| 7350 | MapFlagsArrayTy &Types) const { |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7351 | // We have to process the component lists that relate with the same |
| 7352 | // declaration in a single chunk so that we can generate the map flags |
| 7353 | // correctly. Therefore, we organize all lists in a map. |
Alexey Bataev | 5d1c3f6 | 2017-06-27 15:46:42 +0000 | [diff] [blame] | 7354 | llvm::MapVector<const ValueDecl *, SmallVector<MapInfo, 8>> Info; |
Samuel Antao | 8d2d730 | 2016-05-26 18:30:22 +0000 | [diff] [blame] | 7355 | |
| 7356 | // Helper function to fill the information map for the different supported |
| 7357 | // clauses. |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7358 | auto &&InfoGen = [&Info]( |
| 7359 | const ValueDecl *D, |
| 7360 | OMPClauseMappableExprCommon::MappableExprComponentListRef L, |
| 7361 | OpenMPMapClauseKind MapType, OpenMPMapClauseKind MapModifier, |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7362 | bool ReturnDevicePointer, bool IsImplicit) { |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7363 | const ValueDecl *VD = |
| 7364 | D ? cast<ValueDecl>(D->getCanonicalDecl()) : nullptr; |
Alexey Bataev | f47c4b4 | 2017-09-26 13:47:31 +0000 | [diff] [blame] | 7365 | Info[VD].emplace_back(L, MapType, MapModifier, ReturnDevicePointer, |
| 7366 | IsImplicit); |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7367 | }; |
Samuel Antao | 8d2d730 | 2016-05-26 18:30:22 +0000 | [diff] [blame] | 7368 | |
Paul Robinson | 78fb132 | 2016-08-01 22:12:46 +0000 | [diff] [blame] | 7369 | // FIXME: MSVC 2013 seems to require this-> to find member CurDir. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 7370 | for (const auto *C : this->CurDir.getClausesOfKind<OMPMapClause>()) |
| 7371 | for (const auto &L : C->component_lists()) { |
Samuel Antao | cf3f83e | 2016-07-28 14:47:35 +0000 | [diff] [blame] | 7372 | InfoGen(L.first, L.second, C->getMapType(), C->getMapTypeModifier(), |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7373 | /*ReturnDevicePointer=*/false, C->isImplicit()); |
Alexey Bataev | f47c4b4 | 2017-09-26 13:47:31 +0000 | [diff] [blame] | 7374 | } |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 7375 | for (const auto *C : this->CurDir.getClausesOfKind<OMPToClause>()) |
| 7376 | for (const auto &L : C->component_lists()) { |
Samuel Antao | cf3f83e | 2016-07-28 14:47:35 +0000 | [diff] [blame] | 7377 | InfoGen(L.first, L.second, OMPC_MAP_to, OMPC_MAP_unknown, |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7378 | /*ReturnDevicePointer=*/false, C->isImplicit()); |
Alexey Bataev | f47c4b4 | 2017-09-26 13:47:31 +0000 | [diff] [blame] | 7379 | } |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 7380 | for (const auto *C : this->CurDir.getClausesOfKind<OMPFromClause>()) |
| 7381 | for (const auto &L : C->component_lists()) { |
Samuel Antao | cf3f83e | 2016-07-28 14:47:35 +0000 | [diff] [blame] | 7382 | InfoGen(L.first, L.second, OMPC_MAP_from, OMPC_MAP_unknown, |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7383 | /*ReturnDevicePointer=*/false, C->isImplicit()); |
Alexey Bataev | f47c4b4 | 2017-09-26 13:47:31 +0000 | [diff] [blame] | 7384 | } |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7385 | |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7386 | // Look at the use_device_ptr clause information and mark the existing map |
| 7387 | // entries as such. If there is no map information for an entry in the |
| 7388 | // 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] | 7389 | // section. It is the user fault if that was not mapped before. If there is |
| 7390 | // no map information and the pointer is a struct member, then we defer the |
| 7391 | // emission of that entry until the whole struct has been processed. |
| 7392 | llvm::MapVector<const ValueDecl *, SmallVector<DeferredDevicePtrEntryTy, 4>> |
| 7393 | DeferredInfo; |
| 7394 | |
Paul Robinson | 78fb132 | 2016-08-01 22:12:46 +0000 | [diff] [blame] | 7395 | // FIXME: MSVC 2013 seems to require this-> to find member CurDir. |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7396 | for (const auto *C : |
| 7397 | this->CurDir.getClausesOfKind<OMPUseDevicePtrClause>()) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 7398 | for (const auto &L : C->component_lists()) { |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7399 | assert(!L.second.empty() && "Not expecting empty list of components!"); |
| 7400 | const ValueDecl *VD = L.second.back().getAssociatedDeclaration(); |
| 7401 | VD = cast<ValueDecl>(VD->getCanonicalDecl()); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 7402 | const Expr *IE = L.second.back().getAssociatedExpression(); |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7403 | // If the first component is a member expression, we have to look into |
| 7404 | // 'this', which maps to null in the map of map information. Otherwise |
| 7405 | // look directly for the information. |
| 7406 | auto It = Info.find(isa<MemberExpr>(IE) ? nullptr : VD); |
| 7407 | |
| 7408 | // We potentially have map information for this declaration already. |
| 7409 | // Look for the first set of components that refer to it. |
| 7410 | if (It != Info.end()) { |
| 7411 | auto CI = std::find_if( |
| 7412 | It->second.begin(), It->second.end(), [VD](const MapInfo &MI) { |
| 7413 | return MI.Components.back().getAssociatedDeclaration() == VD; |
| 7414 | }); |
| 7415 | // If we found a map entry, signal that the pointer has to be returned |
| 7416 | // and move on to the next declaration. |
| 7417 | if (CI != It->second.end()) { |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7418 | CI->ReturnDevicePointer = true; |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7419 | continue; |
| 7420 | } |
| 7421 | } |
| 7422 | |
| 7423 | // 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] | 7424 | // size array section - if the pointer is a struct member we defer this |
| 7425 | // action until the whole struct has been processed. |
Paul Robinson | 78fb132 | 2016-08-01 22:12:46 +0000 | [diff] [blame] | 7426 | // FIXME: MSVC 2013 seems to require this-> to find member CGF. |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7427 | if (isa<MemberExpr>(IE)) { |
| 7428 | // Insert the pointer into Info to be processed by |
| 7429 | // generateInfoForComponentList. Because it is a member pointer |
| 7430 | // without a pointee, no entry will be generated for it, therefore |
| 7431 | // we need to generate one after the whole struct has been processed. |
| 7432 | // Nonetheless, generateInfoForComponentList must be called to take |
| 7433 | // the pointer into account for the calculation of the range of the |
| 7434 | // partial struct. |
| 7435 | InfoGen(nullptr, L.second, OMPC_MAP_unknown, OMPC_MAP_unknown, |
| 7436 | /*ReturnDevicePointer=*/false, C->isImplicit()); |
| 7437 | DeferredInfo[nullptr].emplace_back(IE, VD); |
| 7438 | } else { |
| 7439 | llvm::Value *Ptr = this->CGF.EmitLoadOfScalar( |
| 7440 | this->CGF.EmitLValue(IE), IE->getExprLoc()); |
| 7441 | BasePointers.emplace_back(Ptr, VD); |
| 7442 | Pointers.push_back(Ptr); |
| 7443 | Sizes.push_back(llvm::Constant::getNullValue(this->CGF.SizeTy)); |
| 7444 | Types.push_back(OMP_MAP_RETURN_PARAM | OMP_MAP_TARGET_PARAM); |
| 7445 | } |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7446 | } |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7447 | } |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7448 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 7449 | for (const auto &M : Info) { |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7450 | // We need to know when we generate information for the first component |
| 7451 | // associated with a capture, because the mapping flags depend on it. |
| 7452 | bool IsFirstComponentList = true; |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7453 | |
| 7454 | // Temporary versions of arrays |
| 7455 | MapBaseValuesArrayTy CurBasePointers; |
| 7456 | MapValuesArrayTy CurPointers; |
| 7457 | MapValuesArrayTy CurSizes; |
| 7458 | MapFlagsArrayTy CurTypes; |
| 7459 | StructRangeInfoTy PartialStruct; |
| 7460 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 7461 | for (const MapInfo &L : M.second) { |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7462 | assert(!L.Components.empty() && |
| 7463 | "Not expecting declaration with no component lists."); |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7464 | |
| 7465 | // Remember the current base pointer index. |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7466 | unsigned CurrentBasePointersIdx = CurBasePointers.size(); |
Paul Robinson | 78fb132 | 2016-08-01 22:12:46 +0000 | [diff] [blame] | 7467 | // FIXME: MSVC 2013 seems to require this-> to find the member method. |
Alexey Bataev | f47c4b4 | 2017-09-26 13:47:31 +0000 | [diff] [blame] | 7468 | this->generateInfoForComponentList( |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7469 | L.MapType, L.MapTypeModifier, L.Components, CurBasePointers, |
| 7470 | CurPointers, CurSizes, CurTypes, PartialStruct, |
| 7471 | IsFirstComponentList, L.IsImplicit); |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7472 | |
| 7473 | // If this entry relates with a device pointer, set the relevant |
| 7474 | // declaration and add the 'return pointer' flag. |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7475 | if (L.ReturnDevicePointer) { |
| 7476 | assert(CurBasePointers.size() > CurrentBasePointersIdx && |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7477 | "Unexpected number of mapped base pointers."); |
| 7478 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 7479 | const ValueDecl *RelevantVD = |
| 7480 | L.Components.back().getAssociatedDeclaration(); |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7481 | assert(RelevantVD && |
| 7482 | "No relevant declaration related with device pointer??"); |
| 7483 | |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7484 | CurBasePointers[CurrentBasePointersIdx].setDevicePtrDecl(RelevantVD); |
| 7485 | CurTypes[CurrentBasePointersIdx] |= OMP_MAP_RETURN_PARAM; |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7486 | } |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7487 | IsFirstComponentList = false; |
| 7488 | } |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7489 | |
| 7490 | // Append any pending zero-length pointers which are struct members and |
| 7491 | // used with use_device_ptr. |
| 7492 | auto CI = DeferredInfo.find(M.first); |
| 7493 | if (CI != DeferredInfo.end()) { |
| 7494 | for (const DeferredDevicePtrEntryTy &L : CI->second) { |
| 7495 | llvm::Value *BasePtr = this->CGF.EmitLValue(L.IE).getPointer(); |
| 7496 | llvm::Value *Ptr = this->CGF.EmitLoadOfScalar( |
| 7497 | this->CGF.EmitLValue(L.IE), L.IE->getExprLoc()); |
| 7498 | CurBasePointers.emplace_back(BasePtr, L.VD); |
| 7499 | CurPointers.push_back(Ptr); |
| 7500 | CurSizes.push_back(llvm::Constant::getNullValue(this->CGF.SizeTy)); |
| 7501 | // Entry is PTR_AND_OBJ and RETURN_PARAM. Also, set the placeholder |
| 7502 | // value MEMBER_OF=FFFF so that the entry is later updated with the |
| 7503 | // correct value of MEMBER_OF. |
| 7504 | CurTypes.push_back(OMP_MAP_PTR_AND_OBJ | OMP_MAP_RETURN_PARAM | |
| 7505 | OMP_MAP_MEMBER_OF); |
| 7506 | } |
| 7507 | } |
| 7508 | |
| 7509 | // If there is an entry in PartialStruct it means we have a struct with |
| 7510 | // individual members mapped. Emit an extra combined entry. |
| 7511 | if (PartialStruct.Base.isValid()) |
| 7512 | emitCombinedEntry(BasePointers, Pointers, Sizes, Types, CurTypes, |
| 7513 | PartialStruct); |
| 7514 | |
| 7515 | // We need to append the results of this capture to what we already have. |
| 7516 | BasePointers.append(CurBasePointers.begin(), CurBasePointers.end()); |
| 7517 | Pointers.append(CurPointers.begin(), CurPointers.end()); |
| 7518 | Sizes.append(CurSizes.begin(), CurSizes.end()); |
| 7519 | Types.append(CurTypes.begin(), CurTypes.end()); |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7520 | } |
| 7521 | } |
| 7522 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 7523 | /// Generate the base pointers, section pointers, sizes and map types |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7524 | /// associated to a given capture. |
| 7525 | void generateInfoForCapture(const CapturedStmt::Capture *Cap, |
Samuel Antao | 6890b09 | 2016-07-28 14:25:09 +0000 | [diff] [blame] | 7526 | llvm::Value *Arg, |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7527 | MapBaseValuesArrayTy &BasePointers, |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7528 | MapValuesArrayTy &Pointers, |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7529 | MapValuesArrayTy &Sizes, MapFlagsArrayTy &Types, |
| 7530 | StructRangeInfoTy &PartialStruct) const { |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7531 | assert(!Cap->capturesVariableArrayType() && |
| 7532 | "Not expecting to generate map info for a variable array type!"); |
| 7533 | |
Samuel Antao | 6890b09 | 2016-07-28 14:25:09 +0000 | [diff] [blame] | 7534 | // We need to know when we generating information for the first component |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7535 | const ValueDecl *VD = Cap->capturesThis() |
| 7536 | ? nullptr |
| 7537 | : Cap->getCapturedVar()->getCanonicalDecl(); |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7538 | |
Samuel Antao | 6890b09 | 2016-07-28 14:25:09 +0000 | [diff] [blame] | 7539 | // If this declaration appears in a is_device_ptr clause we just have to |
| 7540 | // 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] | 7541 | // pass its value. |
| 7542 | if (DevPointersMap.count(VD)) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 7543 | BasePointers.emplace_back(Arg, VD); |
Samuel Antao | 6890b09 | 2016-07-28 14:25:09 +0000 | [diff] [blame] | 7544 | Pointers.push_back(Arg); |
| 7545 | Sizes.push_back(CGF.getTypeSize(CGF.getContext().VoidPtrTy)); |
George Rokos | 065755d | 2017-11-07 18:27:04 +0000 | [diff] [blame] | 7546 | Types.push_back(OMP_MAP_LITERAL | OMP_MAP_TARGET_PARAM); |
Samuel Antao | 6890b09 | 2016-07-28 14:25:09 +0000 | [diff] [blame] | 7547 | return; |
| 7548 | } |
| 7549 | |
Alexey Bataev | e82445f | 2018-09-20 13:54:02 +0000 | [diff] [blame] | 7550 | using MapData = |
| 7551 | std::tuple<OMPClauseMappableExprCommon::MappableExprComponentListRef, |
| 7552 | OpenMPMapClauseKind, OpenMPMapClauseKind, bool>; |
| 7553 | SmallVector<MapData, 4> DeclComponentLists; |
Paul Robinson | 78fb132 | 2016-08-01 22:12:46 +0000 | [diff] [blame] | 7554 | // FIXME: MSVC 2013 seems to require this-> to find member CurDir. |
Alexey Bataev | e82445f | 2018-09-20 13:54:02 +0000 | [diff] [blame] | 7555 | for (const auto *C : this->CurDir.getClausesOfKind<OMPMapClause>()) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 7556 | for (const auto &L : C->decl_component_lists(VD)) { |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7557 | assert(L.first == VD && |
| 7558 | "We got information for the wrong declaration??"); |
| 7559 | assert(!L.second.empty() && |
| 7560 | "Not expecting declaration with no component lists."); |
Alexey Bataev | e82445f | 2018-09-20 13:54:02 +0000 | [diff] [blame] | 7561 | DeclComponentLists.emplace_back(L.second, C->getMapType(), |
| 7562 | C->getMapTypeModifier(), |
| 7563 | C->isImplicit()); |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7564 | } |
Alexey Bataev | e82445f | 2018-09-20 13:54:02 +0000 | [diff] [blame] | 7565 | } |
| 7566 | |
| 7567 | // Find overlapping elements (including the offset from the base element). |
| 7568 | llvm::SmallDenseMap< |
| 7569 | const MapData *, |
| 7570 | llvm::SmallVector< |
| 7571 | OMPClauseMappableExprCommon::MappableExprComponentListRef, 4>, |
| 7572 | 4> |
| 7573 | OverlappedData; |
| 7574 | size_t Count = 0; |
| 7575 | for (const MapData &L : DeclComponentLists) { |
| 7576 | OMPClauseMappableExprCommon::MappableExprComponentListRef Components; |
| 7577 | OpenMPMapClauseKind MapType; |
| 7578 | OpenMPMapClauseKind MapTypeModifier; |
| 7579 | bool IsImplicit; |
| 7580 | std::tie(Components, MapType, MapTypeModifier, IsImplicit) = L; |
| 7581 | ++Count; |
| 7582 | for (const MapData &L1 : makeArrayRef(DeclComponentLists).slice(Count)) { |
| 7583 | OMPClauseMappableExprCommon::MappableExprComponentListRef Components1; |
| 7584 | std::tie(Components1, MapType, MapTypeModifier, IsImplicit) = L1; |
| 7585 | auto CI = Components.rbegin(); |
| 7586 | auto CE = Components.rend(); |
| 7587 | auto SI = Components1.rbegin(); |
| 7588 | auto SE = Components1.rend(); |
| 7589 | for (; CI != CE && SI != SE; ++CI, ++SI) { |
| 7590 | if (CI->getAssociatedExpression()->getStmtClass() != |
| 7591 | SI->getAssociatedExpression()->getStmtClass()) |
| 7592 | break; |
| 7593 | // Are we dealing with different variables/fields? |
| 7594 | if (CI->getAssociatedDeclaration() != SI->getAssociatedDeclaration()) |
| 7595 | break; |
| 7596 | } |
| 7597 | // Found overlapping if, at least for one component, reached the head of |
| 7598 | // the components list. |
| 7599 | if (CI == CE || SI == SE) { |
| 7600 | assert((CI != CE || SI != SE) && |
| 7601 | "Unexpected full match of the mapping components."); |
| 7602 | const MapData &BaseData = CI == CE ? L : L1; |
| 7603 | OMPClauseMappableExprCommon::MappableExprComponentListRef SubData = |
| 7604 | SI == SE ? Components : Components1; |
Alexey Bataev | e82445f | 2018-09-20 13:54:02 +0000 | [diff] [blame] | 7605 | auto &OverlappedElements = OverlappedData.FindAndConstruct(&BaseData); |
| 7606 | OverlappedElements.getSecond().push_back(SubData); |
| 7607 | } |
| 7608 | } |
| 7609 | } |
| 7610 | // Sort the overlapped elements for each item. |
| 7611 | llvm::SmallVector<const FieldDecl *, 4> Layout; |
| 7612 | if (!OverlappedData.empty()) { |
| 7613 | if (const auto *CRD = |
| 7614 | VD->getType().getCanonicalType()->getAsCXXRecordDecl()) |
| 7615 | getPlainLayout(CRD, Layout, /*AsBase=*/false); |
| 7616 | else { |
| 7617 | const auto *RD = VD->getType().getCanonicalType()->getAsRecordDecl(); |
| 7618 | Layout.append(RD->field_begin(), RD->field_end()); |
| 7619 | } |
| 7620 | } |
| 7621 | for (auto &Pair : OverlappedData) { |
| 7622 | llvm::sort( |
| 7623 | Pair.getSecond(), |
| 7624 | [&Layout]( |
| 7625 | OMPClauseMappableExprCommon::MappableExprComponentListRef First, |
| 7626 | OMPClauseMappableExprCommon::MappableExprComponentListRef |
| 7627 | Second) { |
| 7628 | auto CI = First.rbegin(); |
| 7629 | auto CE = First.rend(); |
| 7630 | auto SI = Second.rbegin(); |
| 7631 | auto SE = Second.rend(); |
| 7632 | for (; CI != CE && SI != SE; ++CI, ++SI) { |
| 7633 | if (CI->getAssociatedExpression()->getStmtClass() != |
| 7634 | SI->getAssociatedExpression()->getStmtClass()) |
| 7635 | break; |
| 7636 | // Are we dealing with different variables/fields? |
| 7637 | if (CI->getAssociatedDeclaration() != |
| 7638 | SI->getAssociatedDeclaration()) |
| 7639 | break; |
| 7640 | } |
Richard Trieu | 5061e83 | 2018-09-21 21:20:33 +0000 | [diff] [blame] | 7641 | |
| 7642 | // Lists contain the same elements. |
| 7643 | if (CI == CE && SI == SE) |
| 7644 | return false; |
| 7645 | |
| 7646 | // List with less elements is less than list with more elements. |
| 7647 | if (CI == CE || SI == SE) |
| 7648 | return CI == CE; |
| 7649 | |
Alexey Bataev | e82445f | 2018-09-20 13:54:02 +0000 | [diff] [blame] | 7650 | const auto *FD1 = cast<FieldDecl>(CI->getAssociatedDeclaration()); |
| 7651 | const auto *FD2 = cast<FieldDecl>(SI->getAssociatedDeclaration()); |
| 7652 | if (FD1->getParent() == FD2->getParent()) |
| 7653 | return FD1->getFieldIndex() < FD2->getFieldIndex(); |
| 7654 | const auto It = |
| 7655 | llvm::find_if(Layout, [FD1, FD2](const FieldDecl *FD) { |
| 7656 | return FD == FD1 || FD == FD2; |
| 7657 | }); |
| 7658 | return *It == FD1; |
| 7659 | }); |
| 7660 | } |
| 7661 | |
| 7662 | // Associated with a capture, because the mapping flags depend on it. |
| 7663 | // Go through all of the elements with the overlapped elements. |
| 7664 | for (const auto &Pair : OverlappedData) { |
| 7665 | const MapData &L = *Pair.getFirst(); |
| 7666 | OMPClauseMappableExprCommon::MappableExprComponentListRef Components; |
| 7667 | OpenMPMapClauseKind MapType; |
| 7668 | OpenMPMapClauseKind MapTypeModifier; |
| 7669 | bool IsImplicit; |
| 7670 | std::tie(Components, MapType, MapTypeModifier, IsImplicit) = L; |
| 7671 | ArrayRef<OMPClauseMappableExprCommon::MappableExprComponentListRef> |
| 7672 | OverlappedComponents = Pair.getSecond(); |
| 7673 | bool IsFirstComponentList = true; |
| 7674 | generateInfoForComponentList(MapType, MapTypeModifier, Components, |
| 7675 | BasePointers, Pointers, Sizes, Types, |
| 7676 | PartialStruct, IsFirstComponentList, |
| 7677 | IsImplicit, OverlappedComponents); |
| 7678 | } |
| 7679 | // Go through other elements without overlapped elements. |
| 7680 | bool IsFirstComponentList = OverlappedData.empty(); |
| 7681 | for (const MapData &L : DeclComponentLists) { |
| 7682 | OMPClauseMappableExprCommon::MappableExprComponentListRef Components; |
| 7683 | OpenMPMapClauseKind MapType; |
| 7684 | OpenMPMapClauseKind MapTypeModifier; |
| 7685 | bool IsImplicit; |
| 7686 | std::tie(Components, MapType, MapTypeModifier, IsImplicit) = L; |
| 7687 | auto It = OverlappedData.find(&L); |
| 7688 | if (It == OverlappedData.end()) |
| 7689 | generateInfoForComponentList(MapType, MapTypeModifier, Components, |
| 7690 | BasePointers, Pointers, Sizes, Types, |
| 7691 | PartialStruct, IsFirstComponentList, |
| 7692 | IsImplicit); |
| 7693 | IsFirstComponentList = false; |
| 7694 | } |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7695 | } |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7696 | |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7697 | /// Generate the base pointers, section pointers, sizes and map types |
| 7698 | /// associated with the declare target link variables. |
| 7699 | void generateInfoForDeclareTargetLink(MapBaseValuesArrayTy &BasePointers, |
| 7700 | MapValuesArrayTy &Pointers, |
| 7701 | MapValuesArrayTy &Sizes, |
| 7702 | MapFlagsArrayTy &Types) const { |
| 7703 | // Map other list items in the map clause which are not captured variables |
| 7704 | // but "declare target link" global variables., |
| 7705 | for (const auto *C : this->CurDir.getClausesOfKind<OMPMapClause>()) { |
| 7706 | for (const auto &L : C->component_lists()) { |
| 7707 | if (!L.first) |
| 7708 | continue; |
| 7709 | const auto *VD = dyn_cast<VarDecl>(L.first); |
| 7710 | if (!VD) |
| 7711 | continue; |
| 7712 | llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res = |
Alexey Bataev | 97b7221 | 2018-08-14 18:31:20 +0000 | [diff] [blame] | 7713 | OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD); |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7714 | if (!Res || *Res != OMPDeclareTargetDeclAttr::MT_Link) |
| 7715 | continue; |
| 7716 | StructRangeInfoTy PartialStruct; |
| 7717 | generateInfoForComponentList( |
| 7718 | C->getMapType(), C->getMapTypeModifier(), L.second, BasePointers, |
| 7719 | Pointers, Sizes, Types, PartialStruct, |
| 7720 | /*IsFirstComponentList=*/true, C->isImplicit()); |
| 7721 | assert(!PartialStruct.Base.isValid() && |
| 7722 | "No partial structs for declare target link expected."); |
| 7723 | } |
| 7724 | } |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7725 | } |
Samuel Antao | d486f84 | 2016-05-26 16:53:38 +0000 | [diff] [blame] | 7726 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 7727 | /// Generate the default map information for a given capture \a CI, |
Samuel Antao | d486f84 | 2016-05-26 16:53:38 +0000 | [diff] [blame] | 7728 | /// record field declaration \a RI and captured value \a CV. |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7729 | void generateDefaultMapInfo(const CapturedStmt::Capture &CI, |
| 7730 | const FieldDecl &RI, llvm::Value *CV, |
| 7731 | MapBaseValuesArrayTy &CurBasePointers, |
| 7732 | MapValuesArrayTy &CurPointers, |
| 7733 | MapValuesArrayTy &CurSizes, |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7734 | MapFlagsArrayTy &CurMapTypes) const { |
Samuel Antao | d486f84 | 2016-05-26 16:53:38 +0000 | [diff] [blame] | 7735 | // Do the default mapping. |
| 7736 | if (CI.capturesThis()) { |
| 7737 | CurBasePointers.push_back(CV); |
| 7738 | CurPointers.push_back(CV); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 7739 | const auto *PtrTy = cast<PointerType>(RI.getType().getTypePtr()); |
Samuel Antao | d486f84 | 2016-05-26 16:53:38 +0000 | [diff] [blame] | 7740 | CurSizes.push_back(CGF.getTypeSize(PtrTy->getPointeeType())); |
| 7741 | // Default map type. |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7742 | CurMapTypes.push_back(OMP_MAP_TO | OMP_MAP_FROM); |
Samuel Antao | d486f84 | 2016-05-26 16:53:38 +0000 | [diff] [blame] | 7743 | } else if (CI.capturesVariableByCopy()) { |
Samuel Antao | 6d00426 | 2016-06-16 18:39:34 +0000 | [diff] [blame] | 7744 | CurBasePointers.push_back(CV); |
| 7745 | CurPointers.push_back(CV); |
Samuel Antao | d486f84 | 2016-05-26 16:53:38 +0000 | [diff] [blame] | 7746 | if (!RI.getType()->isAnyPointerType()) { |
Samuel Antao | 6d00426 | 2016-06-16 18:39:34 +0000 | [diff] [blame] | 7747 | // We have to signal to the runtime captures passed by value that are |
| 7748 | // not pointers. |
George Rokos | 065755d | 2017-11-07 18:27:04 +0000 | [diff] [blame] | 7749 | CurMapTypes.push_back(OMP_MAP_LITERAL); |
Samuel Antao | d486f84 | 2016-05-26 16:53:38 +0000 | [diff] [blame] | 7750 | CurSizes.push_back(CGF.getTypeSize(RI.getType())); |
| 7751 | } else { |
| 7752 | // Pointers are implicitly mapped with a zero size and no flags |
| 7753 | // (other than first map that is added for all implicit maps). |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7754 | CurMapTypes.push_back(OMP_MAP_NONE); |
Samuel Antao | d486f84 | 2016-05-26 16:53:38 +0000 | [diff] [blame] | 7755 | CurSizes.push_back(llvm::Constant::getNullValue(CGF.SizeTy)); |
| 7756 | } |
| 7757 | } else { |
| 7758 | assert(CI.capturesVariable() && "Expected captured reference."); |
| 7759 | CurBasePointers.push_back(CV); |
| 7760 | CurPointers.push_back(CV); |
| 7761 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 7762 | const auto *PtrTy = cast<ReferenceType>(RI.getType().getTypePtr()); |
Samuel Antao | d486f84 | 2016-05-26 16:53:38 +0000 | [diff] [blame] | 7763 | QualType ElementType = PtrTy->getPointeeType(); |
| 7764 | CurSizes.push_back(CGF.getTypeSize(ElementType)); |
| 7765 | // The default map type for a scalar/complex type is 'to' because by |
| 7766 | // default the value doesn't have to be retrieved. For an aggregate |
| 7767 | // type, the default is 'tofrom'. |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7768 | CurMapTypes.push_back(getMapModifiersForPrivateClauses(CI)); |
Samuel Antao | d486f84 | 2016-05-26 16:53:38 +0000 | [diff] [blame] | 7769 | } |
George Rokos | 065755d | 2017-11-07 18:27:04 +0000 | [diff] [blame] | 7770 | // Every default map produces a single argument which is a target parameter. |
| 7771 | CurMapTypes.back() |= OMP_MAP_TARGET_PARAM; |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7772 | |
| 7773 | // Add flag stating this is an implicit map. |
| 7774 | CurMapTypes.back() |= OMP_MAP_IMPLICIT; |
Samuel Antao | d486f84 | 2016-05-26 16:53:38 +0000 | [diff] [blame] | 7775 | } |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7776 | }; |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 7777 | |
| 7778 | enum OpenMPOffloadingReservedDeviceIDs { |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 7779 | /// Device ID if the device was not defined, runtime should get it |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 7780 | /// from environment variables in the spec. |
| 7781 | OMP_DEVICEID_UNDEF = -1, |
| 7782 | }; |
| 7783 | } // anonymous namespace |
| 7784 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 7785 | /// 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] | 7786 | /// offloading runtime library. If there is no map or capture information, |
| 7787 | /// return nullptr by reference. |
| 7788 | static void |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7789 | emitOffloadingArrays(CodeGenFunction &CGF, |
| 7790 | MappableExprsHandler::MapBaseValuesArrayTy &BasePointers, |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 7791 | MappableExprsHandler::MapValuesArrayTy &Pointers, |
| 7792 | MappableExprsHandler::MapValuesArrayTy &Sizes, |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7793 | MappableExprsHandler::MapFlagsArrayTy &MapTypes, |
| 7794 | CGOpenMPRuntime::TargetDataInfo &Info) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 7795 | CodeGenModule &CGM = CGF.CGM; |
| 7796 | ASTContext &Ctx = CGF.getContext(); |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 7797 | |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7798 | // Reset the array information. |
| 7799 | Info.clearArrayInfo(); |
| 7800 | Info.NumberOfPtrs = BasePointers.size(); |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 7801 | |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7802 | if (Info.NumberOfPtrs) { |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 7803 | // Detect if we have any capture size requiring runtime evaluation of the |
| 7804 | // size so that a constant array could be eventually used. |
| 7805 | bool hasRuntimeEvaluationCaptureSize = false; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 7806 | for (llvm::Value *S : Sizes) |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 7807 | if (!isa<llvm::Constant>(S)) { |
| 7808 | hasRuntimeEvaluationCaptureSize = true; |
| 7809 | break; |
| 7810 | } |
| 7811 | |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7812 | llvm::APInt PointerNumAP(32, Info.NumberOfPtrs, /*isSigned=*/true); |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 7813 | QualType PointerArrayType = |
| 7814 | Ctx.getConstantArrayType(Ctx.VoidPtrTy, PointerNumAP, ArrayType::Normal, |
| 7815 | /*IndexTypeQuals=*/0); |
| 7816 | |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7817 | Info.BasePointersArray = |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 7818 | CGF.CreateMemTemp(PointerArrayType, ".offload_baseptrs").getPointer(); |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7819 | Info.PointersArray = |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 7820 | CGF.CreateMemTemp(PointerArrayType, ".offload_ptrs").getPointer(); |
| 7821 | |
| 7822 | // If we don't have any VLA types or other types that require runtime |
| 7823 | // evaluation, we can use a constant array for the map sizes, otherwise we |
| 7824 | // need to fill up the arrays as we do for the pointers. |
| 7825 | if (hasRuntimeEvaluationCaptureSize) { |
| 7826 | QualType SizeArrayType = Ctx.getConstantArrayType( |
| 7827 | Ctx.getSizeType(), PointerNumAP, ArrayType::Normal, |
| 7828 | /*IndexTypeQuals=*/0); |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7829 | Info.SizesArray = |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 7830 | CGF.CreateMemTemp(SizeArrayType, ".offload_sizes").getPointer(); |
| 7831 | } else { |
| 7832 | // We expect all the sizes to be constant, so we collect them to create |
| 7833 | // a constant array. |
| 7834 | SmallVector<llvm::Constant *, 16> ConstSizes; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 7835 | for (llvm::Value *S : Sizes) |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 7836 | ConstSizes.push_back(cast<llvm::Constant>(S)); |
| 7837 | |
| 7838 | auto *SizesArrayInit = llvm::ConstantArray::get( |
| 7839 | llvm::ArrayType::get(CGM.SizeTy, ConstSizes.size()), ConstSizes); |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 7840 | std::string Name = CGM.getOpenMPRuntime().getName({"offload_sizes"}); |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 7841 | auto *SizesArrayGbl = new llvm::GlobalVariable( |
| 7842 | CGM.getModule(), SizesArrayInit->getType(), |
| 7843 | /*isConstant=*/true, llvm::GlobalValue::PrivateLinkage, |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 7844 | SizesArrayInit, Name); |
Peter Collingbourne | bcf909d | 2016-06-14 21:02:05 +0000 | [diff] [blame] | 7845 | SizesArrayGbl->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global); |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7846 | Info.SizesArray = SizesArrayGbl; |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 7847 | } |
| 7848 | |
| 7849 | // The map types are always constant so we don't need to generate code to |
| 7850 | // fill arrays. Instead, we create an array constant. |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7851 | SmallVector<uint64_t, 4> Mapping(MapTypes.size(), 0); |
| 7852 | llvm::copy(MapTypes, Mapping.begin()); |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 7853 | llvm::Constant *MapTypesArrayInit = |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7854 | llvm::ConstantDataArray::get(CGF.Builder.getContext(), Mapping); |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 7855 | std::string MaptypesName = |
| 7856 | CGM.getOpenMPRuntime().getName({"offload_maptypes"}); |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 7857 | auto *MapTypesArrayGbl = new llvm::GlobalVariable( |
| 7858 | CGM.getModule(), MapTypesArrayInit->getType(), |
| 7859 | /*isConstant=*/true, llvm::GlobalValue::PrivateLinkage, |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 7860 | MapTypesArrayInit, MaptypesName); |
Peter Collingbourne | bcf909d | 2016-06-14 21:02:05 +0000 | [diff] [blame] | 7861 | MapTypesArrayGbl->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global); |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7862 | Info.MapTypesArray = MapTypesArrayGbl; |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 7863 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 7864 | for (unsigned I = 0; I < Info.NumberOfPtrs; ++I) { |
| 7865 | llvm::Value *BPVal = *BasePointers[I]; |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 7866 | llvm::Value *BP = CGF.Builder.CreateConstInBoundsGEP2_32( |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7867 | llvm::ArrayType::get(CGM.VoidPtrTy, Info.NumberOfPtrs), |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 7868 | Info.BasePointersArray, 0, I); |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 7869 | BP = CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
| 7870 | BP, BPVal->getType()->getPointerTo(/*AddrSpace=*/0)); |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 7871 | Address BPAddr(BP, Ctx.getTypeAlignInChars(Ctx.VoidPtrTy)); |
| 7872 | CGF.Builder.CreateStore(BPVal, BPAddr); |
| 7873 | |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7874 | if (Info.requiresDevicePointerInfo()) |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 7875 | if (const ValueDecl *DevVD = BasePointers[I].getDevicePtrDecl()) |
Alexey Bataev | 43a919f | 2018-04-13 17:48:43 +0000 | [diff] [blame] | 7876 | Info.CaptureDeviceAddrMap.try_emplace(DevVD, BPAddr); |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7877 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 7878 | llvm::Value *PVal = Pointers[I]; |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 7879 | llvm::Value *P = CGF.Builder.CreateConstInBoundsGEP2_32( |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7880 | llvm::ArrayType::get(CGM.VoidPtrTy, Info.NumberOfPtrs), |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 7881 | Info.PointersArray, 0, I); |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 7882 | P = CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
| 7883 | P, PVal->getType()->getPointerTo(/*AddrSpace=*/0)); |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 7884 | Address PAddr(P, Ctx.getTypeAlignInChars(Ctx.VoidPtrTy)); |
| 7885 | CGF.Builder.CreateStore(PVal, PAddr); |
| 7886 | |
| 7887 | if (hasRuntimeEvaluationCaptureSize) { |
| 7888 | llvm::Value *S = CGF.Builder.CreateConstInBoundsGEP2_32( |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7889 | llvm::ArrayType::get(CGM.SizeTy, Info.NumberOfPtrs), |
| 7890 | Info.SizesArray, |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 7891 | /*Idx0=*/0, |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 7892 | /*Idx1=*/I); |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 7893 | Address SAddr(S, Ctx.getTypeAlignInChars(Ctx.getSizeType())); |
| 7894 | CGF.Builder.CreateStore( |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 7895 | CGF.Builder.CreateIntCast(Sizes[I], CGM.SizeTy, /*isSigned=*/true), |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 7896 | SAddr); |
| 7897 | } |
| 7898 | } |
| 7899 | } |
| 7900 | } |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 7901 | /// 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] | 7902 | /// arrays of pointers, sizes and map types. |
| 7903 | static void emitOffloadingArraysArgument( |
| 7904 | CodeGenFunction &CGF, llvm::Value *&BasePointersArrayArg, |
| 7905 | llvm::Value *&PointersArrayArg, llvm::Value *&SizesArrayArg, |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7906 | llvm::Value *&MapTypesArrayArg, CGOpenMPRuntime::TargetDataInfo &Info) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 7907 | CodeGenModule &CGM = CGF.CGM; |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7908 | if (Info.NumberOfPtrs) { |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 7909 | BasePointersArrayArg = CGF.Builder.CreateConstInBoundsGEP2_32( |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7910 | llvm::ArrayType::get(CGM.VoidPtrTy, Info.NumberOfPtrs), |
| 7911 | Info.BasePointersArray, |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 7912 | /*Idx0=*/0, /*Idx1=*/0); |
| 7913 | PointersArrayArg = CGF.Builder.CreateConstInBoundsGEP2_32( |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7914 | llvm::ArrayType::get(CGM.VoidPtrTy, Info.NumberOfPtrs), |
| 7915 | Info.PointersArray, |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 7916 | /*Idx0=*/0, |
| 7917 | /*Idx1=*/0); |
| 7918 | SizesArrayArg = CGF.Builder.CreateConstInBoundsGEP2_32( |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7919 | llvm::ArrayType::get(CGM.SizeTy, Info.NumberOfPtrs), Info.SizesArray, |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 7920 | /*Idx0=*/0, /*Idx1=*/0); |
| 7921 | MapTypesArrayArg = CGF.Builder.CreateConstInBoundsGEP2_32( |
George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 7922 | llvm::ArrayType::get(CGM.Int64Ty, Info.NumberOfPtrs), |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7923 | Info.MapTypesArray, |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 7924 | /*Idx0=*/0, |
| 7925 | /*Idx1=*/0); |
| 7926 | } else { |
| 7927 | BasePointersArrayArg = llvm::ConstantPointerNull::get(CGM.VoidPtrPtrTy); |
| 7928 | PointersArrayArg = llvm::ConstantPointerNull::get(CGM.VoidPtrPtrTy); |
| 7929 | SizesArrayArg = llvm::ConstantPointerNull::get(CGM.SizeTy->getPointerTo()); |
| 7930 | MapTypesArrayArg = |
George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 7931 | llvm::ConstantPointerNull::get(CGM.Int64Ty->getPointerTo()); |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 7932 | } |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7933 | } |
| 7934 | |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 7935 | void CGOpenMPRuntime::emitTargetCall(CodeGenFunction &CGF, |
| 7936 | const OMPExecutableDirective &D, |
| 7937 | llvm::Value *OutlinedFn, |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 7938 | llvm::Value *OutlinedFnID, |
Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 7939 | const Expr *IfCond, const Expr *Device) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 7940 | if (!CGF.HaveInsertPoint()) |
| 7941 | return; |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 7942 | |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 7943 | assert(OutlinedFn && "Invalid outlined function!"); |
| 7944 | |
Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 7945 | const bool RequiresOuterTask = D.hasClausesOfKind<OMPDependClause>(); |
| 7946 | llvm::SmallVector<llvm::Value *, 16> CapturedVars; |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 7947 | const CapturedStmt &CS = *D.getCapturedStmt(OMPD_target); |
Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 7948 | auto &&ArgsCodegen = [&CS, &CapturedVars](CodeGenFunction &CGF, |
| 7949 | PrePostActionTy &) { |
| 7950 | CGF.GenerateOpenMPCapturedVars(CS, CapturedVars); |
| 7951 | }; |
| 7952 | emitInlinedDirective(CGF, OMPD_unknown, ArgsCodegen); |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7953 | |
Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 7954 | CodeGenFunction::OMPTargetDataInfo InputInfo; |
| 7955 | llvm::Value *MapTypesArray = nullptr; |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 7956 | // Fill up the pointer arrays and transfer execution to the device. |
Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 7957 | auto &&ThenGen = [this, Device, OutlinedFn, OutlinedFnID, &D, &InputInfo, |
| 7958 | &MapTypesArray, &CS, RequiresOuterTask, |
| 7959 | &CapturedVars](CodeGenFunction &CGF, PrePostActionTy &) { |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 7960 | // On top of the arrays that were filled up, the target offloading call |
| 7961 | // takes as arguments the device id as well as the host pointer. The host |
| 7962 | // pointer is used by the runtime library to identify the current target |
| 7963 | // region, so it only has to be unique and not necessarily point to |
| 7964 | // anything. It could be the pointer to the outlined function that |
| 7965 | // implements the target region, but we aren't using that so that the |
| 7966 | // compiler doesn't need to keep that, and could therefore inline the host |
| 7967 | // function if proven worthwhile during optimization. |
| 7968 | |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 7969 | // From this point on, we need to have an ID of the target region defined. |
| 7970 | assert(OutlinedFnID && "Invalid outlined function ID!"); |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 7971 | |
| 7972 | // Emit device ID if any. |
| 7973 | llvm::Value *DeviceID; |
George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 7974 | if (Device) { |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 7975 | DeviceID = CGF.Builder.CreateIntCast(CGF.EmitScalarExpr(Device), |
George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 7976 | CGF.Int64Ty, /*isSigned=*/true); |
| 7977 | } else { |
| 7978 | DeviceID = CGF.Builder.getInt64(OMP_DEVICEID_UNDEF); |
| 7979 | } |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 7980 | |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 7981 | // Emit the number of elements in the offloading arrays. |
Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 7982 | llvm::Value *PointerNum = |
| 7983 | CGF.Builder.getInt32(InputInfo.NumberOfTargetItems); |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 7984 | |
Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 7985 | // Return value of the runtime offloading call. |
| 7986 | llvm::Value *Return; |
| 7987 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 7988 | llvm::Value *NumTeams = emitNumTeamsForTargetDirective(*this, CGF, D); |
| 7989 | llvm::Value *NumThreads = emitNumThreadsForTargetDirective(*this, CGF, D); |
Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 7990 | |
Alexey Bataev | a9f77c6 | 2017-12-13 21:04:20 +0000 | [diff] [blame] | 7991 | bool HasNowait = D.hasClausesOfKind<OMPNowaitClause>(); |
Arpith Chacko Jacob | 33c849a | 2017-01-25 00:57:16 +0000 | [diff] [blame] | 7992 | // The target region is an outlined function launched by the runtime |
| 7993 | // via calls __tgt_target() or __tgt_target_teams(). |
| 7994 | // |
| 7995 | // __tgt_target() launches a target region with one team and one thread, |
| 7996 | // executing a serial region. This master thread may in turn launch |
| 7997 | // more threads within its team upon encountering a parallel region, |
| 7998 | // however, no additional teams can be launched on the device. |
| 7999 | // |
| 8000 | // __tgt_target_teams() launches a target region with one or more teams, |
| 8001 | // each with one or more threads. This call is required for target |
| 8002 | // constructs such as: |
| 8003 | // 'target teams' |
| 8004 | // 'target' / 'teams' |
| 8005 | // 'target teams distribute parallel for' |
| 8006 | // 'target parallel' |
| 8007 | // and so on. |
| 8008 | // |
| 8009 | // Note that on the host and CPU targets, the runtime implementation of |
| 8010 | // these calls simply call the outlined function without forking threads. |
| 8011 | // The outlined functions themselves have runtime calls to |
| 8012 | // __kmpc_fork_teams() and __kmpc_fork() for this purpose, codegen'd by |
| 8013 | // the compiler in emitTeamsCall() and emitParallelCall(). |
| 8014 | // |
| 8015 | // In contrast, on the NVPTX target, the implementation of |
| 8016 | // __tgt_target_teams() launches a GPU kernel with the requested number |
| 8017 | // 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] | 8018 | if (NumTeams) { |
Arpith Chacko Jacob | 33c849a | 2017-01-25 00:57:16 +0000 | [diff] [blame] | 8019 | // If we have NumTeams defined this means that we have an enclosed teams |
| 8020 | // region. Therefore we also expect to have NumThreads defined. These two |
| 8021 | // values should be defined in the presence of a teams directive, |
| 8022 | // regardless of having any clauses associated. If the user is using teams |
| 8023 | // but no clauses, these two values will be the default that should be |
| 8024 | // passed to the runtime library - a 32-bit integer with the value zero. |
| 8025 | assert(NumThreads && "Thread limit expression should be available along " |
| 8026 | "with number of teams."); |
Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 8027 | llvm::Value *OffloadingArgs[] = {DeviceID, |
| 8028 | OutlinedFnID, |
| 8029 | PointerNum, |
| 8030 | InputInfo.BasePointersArray.getPointer(), |
| 8031 | InputInfo.PointersArray.getPointer(), |
| 8032 | InputInfo.SizesArray.getPointer(), |
| 8033 | MapTypesArray, |
| 8034 | NumTeams, |
| 8035 | NumThreads}; |
Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 8036 | Return = CGF.EmitRuntimeCall( |
Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 8037 | createRuntimeFunction(HasNowait ? OMPRTL__tgt_target_teams_nowait |
| 8038 | : OMPRTL__tgt_target_teams), |
Alexey Bataev | a9f77c6 | 2017-12-13 21:04:20 +0000 | [diff] [blame] | 8039 | OffloadingArgs); |
Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 8040 | } else { |
Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 8041 | llvm::Value *OffloadingArgs[] = {DeviceID, |
| 8042 | OutlinedFnID, |
| 8043 | PointerNum, |
| 8044 | InputInfo.BasePointersArray.getPointer(), |
| 8045 | InputInfo.PointersArray.getPointer(), |
| 8046 | InputInfo.SizesArray.getPointer(), |
| 8047 | MapTypesArray}; |
Alexey Bataev | a9f77c6 | 2017-12-13 21:04:20 +0000 | [diff] [blame] | 8048 | Return = CGF.EmitRuntimeCall( |
Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 8049 | createRuntimeFunction(HasNowait ? OMPRTL__tgt_target_nowait |
| 8050 | : OMPRTL__tgt_target), |
Alexey Bataev | a9f77c6 | 2017-12-13 21:04:20 +0000 | [diff] [blame] | 8051 | OffloadingArgs); |
Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 8052 | } |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 8053 | |
Alexey Bataev | 2a007e0 | 2017-10-02 14:20:58 +0000 | [diff] [blame] | 8054 | // Check the error code and execute the host version if required. |
| 8055 | llvm::BasicBlock *OffloadFailedBlock = |
| 8056 | CGF.createBasicBlock("omp_offload.failed"); |
| 8057 | llvm::BasicBlock *OffloadContBlock = |
| 8058 | CGF.createBasicBlock("omp_offload.cont"); |
| 8059 | llvm::Value *Failed = CGF.Builder.CreateIsNotNull(Return); |
| 8060 | CGF.Builder.CreateCondBr(Failed, OffloadFailedBlock, OffloadContBlock); |
| 8061 | |
| 8062 | CGF.EmitBlock(OffloadFailedBlock); |
Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 8063 | if (RequiresOuterTask) { |
| 8064 | CapturedVars.clear(); |
| 8065 | CGF.GenerateOpenMPCapturedVars(CS, CapturedVars); |
| 8066 | } |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 8067 | emitOutlinedFunctionCall(CGF, D.getBeginLoc(), OutlinedFn, CapturedVars); |
Alexey Bataev | 2a007e0 | 2017-10-02 14:20:58 +0000 | [diff] [blame] | 8068 | CGF.EmitBranch(OffloadContBlock); |
| 8069 | |
| 8070 | CGF.EmitBlock(OffloadContBlock, /*IsFinished=*/true); |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 8071 | }; |
| 8072 | |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 8073 | // Notify that the host version must be executed. |
Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 8074 | auto &&ElseGen = [this, &D, OutlinedFn, &CS, &CapturedVars, |
| 8075 | RequiresOuterTask](CodeGenFunction &CGF, |
| 8076 | PrePostActionTy &) { |
| 8077 | if (RequiresOuterTask) { |
| 8078 | CapturedVars.clear(); |
| 8079 | CGF.GenerateOpenMPCapturedVars(CS, CapturedVars); |
| 8080 | } |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 8081 | emitOutlinedFunctionCall(CGF, D.getBeginLoc(), OutlinedFn, CapturedVars); |
Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 8082 | }; |
| 8083 | |
| 8084 | auto &&TargetThenGen = [this, &ThenGen, &D, &InputInfo, &MapTypesArray, |
| 8085 | &CapturedVars, RequiresOuterTask, |
| 8086 | &CS](CodeGenFunction &CGF, PrePostActionTy &) { |
| 8087 | // Fill up the arrays with all the captured variables. |
| 8088 | MappableExprsHandler::MapBaseValuesArrayTy BasePointers; |
| 8089 | MappableExprsHandler::MapValuesArrayTy Pointers; |
| 8090 | MappableExprsHandler::MapValuesArrayTy Sizes; |
| 8091 | MappableExprsHandler::MapFlagsArrayTy MapTypes; |
| 8092 | |
Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 8093 | // Get mappable expression information. |
| 8094 | MappableExprsHandler MEHandler(D, CGF); |
| 8095 | |
| 8096 | auto RI = CS.getCapturedRecordDecl()->field_begin(); |
| 8097 | auto CV = CapturedVars.begin(); |
| 8098 | for (CapturedStmt::const_capture_iterator CI = CS.capture_begin(), |
| 8099 | CE = CS.capture_end(); |
| 8100 | CI != CE; ++CI, ++RI, ++CV) { |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 8101 | MappableExprsHandler::MapBaseValuesArrayTy CurBasePointers; |
| 8102 | MappableExprsHandler::MapValuesArrayTy CurPointers; |
| 8103 | MappableExprsHandler::MapValuesArrayTy CurSizes; |
| 8104 | MappableExprsHandler::MapFlagsArrayTy CurMapTypes; |
| 8105 | MappableExprsHandler::StructRangeInfoTy PartialStruct; |
Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 8106 | |
| 8107 | // VLA sizes are passed to the outlined region by copy and do not have map |
| 8108 | // information associated. |
| 8109 | if (CI->capturesVariableArrayType()) { |
| 8110 | CurBasePointers.push_back(*CV); |
| 8111 | CurPointers.push_back(*CV); |
| 8112 | CurSizes.push_back(CGF.getTypeSize(RI->getType())); |
| 8113 | // Copy to the device as an argument. No need to retrieve it. |
| 8114 | CurMapTypes.push_back(MappableExprsHandler::OMP_MAP_LITERAL | |
| 8115 | MappableExprsHandler::OMP_MAP_TARGET_PARAM); |
| 8116 | } else { |
| 8117 | // If we have any information in the map clause, we use it, otherwise we |
| 8118 | // just do a default mapping. |
| 8119 | MEHandler.generateInfoForCapture(CI, *CV, CurBasePointers, CurPointers, |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 8120 | CurSizes, CurMapTypes, PartialStruct); |
Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 8121 | if (CurBasePointers.empty()) |
| 8122 | MEHandler.generateDefaultMapInfo(*CI, **RI, *CV, CurBasePointers, |
| 8123 | CurPointers, CurSizes, CurMapTypes); |
| 8124 | } |
| 8125 | // We expect to have at least an element of information for this capture. |
| 8126 | assert(!CurBasePointers.empty() && |
| 8127 | "Non-existing map pointer for capture!"); |
| 8128 | assert(CurBasePointers.size() == CurPointers.size() && |
| 8129 | CurBasePointers.size() == CurSizes.size() && |
| 8130 | CurBasePointers.size() == CurMapTypes.size() && |
| 8131 | "Inconsistent map information sizes!"); |
| 8132 | |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 8133 | // If there is an entry in PartialStruct it means we have a struct with |
| 8134 | // individual members mapped. Emit an extra combined entry. |
| 8135 | if (PartialStruct.Base.isValid()) |
| 8136 | MEHandler.emitCombinedEntry(BasePointers, Pointers, Sizes, MapTypes, |
| 8137 | CurMapTypes, PartialStruct); |
| 8138 | |
Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 8139 | // We need to append the results of this capture to what we already have. |
| 8140 | BasePointers.append(CurBasePointers.begin(), CurBasePointers.end()); |
| 8141 | Pointers.append(CurPointers.begin(), CurPointers.end()); |
| 8142 | Sizes.append(CurSizes.begin(), CurSizes.end()); |
| 8143 | MapTypes.append(CurMapTypes.begin(), CurMapTypes.end()); |
| 8144 | } |
Alexey Bataev | 92327c5 | 2018-03-26 16:40:55 +0000 | [diff] [blame] | 8145 | // Map other list items in the map clause which are not captured variables |
| 8146 | // but "declare target link" global variables. |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 8147 | MEHandler.generateInfoForDeclareTargetLink(BasePointers, Pointers, Sizes, |
| 8148 | MapTypes); |
Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 8149 | |
| 8150 | TargetDataInfo Info; |
| 8151 | // Fill up the arrays and create the arguments. |
| 8152 | emitOffloadingArrays(CGF, BasePointers, Pointers, Sizes, MapTypes, Info); |
| 8153 | emitOffloadingArraysArgument(CGF, Info.BasePointersArray, |
| 8154 | Info.PointersArray, Info.SizesArray, |
| 8155 | Info.MapTypesArray, Info); |
| 8156 | InputInfo.NumberOfTargetItems = Info.NumberOfPtrs; |
| 8157 | InputInfo.BasePointersArray = |
| 8158 | Address(Info.BasePointersArray, CGM.getPointerAlign()); |
| 8159 | InputInfo.PointersArray = |
| 8160 | Address(Info.PointersArray, CGM.getPointerAlign()); |
| 8161 | InputInfo.SizesArray = Address(Info.SizesArray, CGM.getPointerAlign()); |
| 8162 | MapTypesArray = Info.MapTypesArray; |
| 8163 | if (RequiresOuterTask) |
| 8164 | CGF.EmitOMPTargetTaskBasedDirective(D, ThenGen, InputInfo); |
| 8165 | else |
| 8166 | emitInlinedDirective(CGF, D.getDirectiveKind(), ThenGen); |
| 8167 | }; |
| 8168 | |
| 8169 | auto &&TargetElseGen = [this, &ElseGen, &D, RequiresOuterTask]( |
| 8170 | CodeGenFunction &CGF, PrePostActionTy &) { |
| 8171 | if (RequiresOuterTask) { |
| 8172 | CodeGenFunction::OMPTargetDataInfo InputInfo; |
| 8173 | CGF.EmitOMPTargetTaskBasedDirective(D, ElseGen, InputInfo); |
| 8174 | } else { |
| 8175 | emitInlinedDirective(CGF, D.getDirectiveKind(), ElseGen); |
| 8176 | } |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 8177 | }; |
| 8178 | |
| 8179 | // If we have a target function ID it means that we need to support |
| 8180 | // offloading, otherwise, just execute on the host. We need to execute on host |
| 8181 | // regardless of the conditional in the if clause if, e.g., the user do not |
| 8182 | // specify target triples. |
| 8183 | if (OutlinedFnID) { |
Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 8184 | if (IfCond) { |
| 8185 | emitOMPIfClause(CGF, IfCond, TargetThenGen, TargetElseGen); |
| 8186 | } else { |
| 8187 | RegionCodeGenTy ThenRCG(TargetThenGen); |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 8188 | ThenRCG(CGF); |
Alexey Bataev | f539faa | 2016-03-28 12:58:34 +0000 | [diff] [blame] | 8189 | } |
| 8190 | } else { |
Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 8191 | RegionCodeGenTy ElseRCG(TargetElseGen); |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 8192 | ElseRCG(CGF); |
Alexey Bataev | f539faa | 2016-03-28 12:58:34 +0000 | [diff] [blame] | 8193 | } |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 8194 | } |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 8195 | |
| 8196 | void CGOpenMPRuntime::scanForTargetRegionsFunctions(const Stmt *S, |
| 8197 | StringRef ParentName) { |
| 8198 | if (!S) |
| 8199 | return; |
| 8200 | |
Arpith Chacko Jacob | 43a8b7b | 2017-01-16 15:26:02 +0000 | [diff] [blame] | 8201 | // Codegen OMP target directives that offload compute to the device. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8202 | bool RequiresDeviceCodegen = |
Arpith Chacko Jacob | 43a8b7b | 2017-01-16 15:26:02 +0000 | [diff] [blame] | 8203 | isa<OMPExecutableDirective>(S) && |
| 8204 | isOpenMPTargetExecutionDirective( |
| 8205 | cast<OMPExecutableDirective>(S)->getDirectiveKind()); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 8206 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8207 | if (RequiresDeviceCodegen) { |
| 8208 | const auto &E = *cast<OMPExecutableDirective>(S); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 8209 | unsigned DeviceID; |
| 8210 | unsigned FileID; |
| 8211 | unsigned Line; |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 8212 | getTargetEntryUniqueInfo(CGM.getContext(), E.getBeginLoc(), DeviceID, |
Samuel Antao | 2de62b0 | 2016-02-13 23:35:10 +0000 | [diff] [blame] | 8213 | FileID, Line); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 8214 | |
| 8215 | // Is this a target region that should not be emitted as an entry point? If |
| 8216 | // so just signal we are done with this target region. |
Samuel Antao | 2de62b0 | 2016-02-13 23:35:10 +0000 | [diff] [blame] | 8217 | if (!OffloadEntriesInfoManager.hasTargetRegionEntryInfo(DeviceID, FileID, |
| 8218 | ParentName, Line)) |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 8219 | return; |
| 8220 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8221 | switch (E.getDirectiveKind()) { |
| 8222 | case OMPD_target: |
| 8223 | CodeGenFunction::EmitOMPTargetDeviceFunction(CGM, ParentName, |
| 8224 | cast<OMPTargetDirective>(E)); |
Arpith Chacko Jacob | 43a8b7b | 2017-01-16 15:26:02 +0000 | [diff] [blame] | 8225 | break; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8226 | case OMPD_target_parallel: |
Arpith Chacko Jacob | 19b911c | 2017-01-18 18:18:53 +0000 | [diff] [blame] | 8227 | CodeGenFunction::EmitOMPTargetParallelDeviceFunction( |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8228 | CGM, ParentName, cast<OMPTargetParallelDirective>(E)); |
Arpith Chacko Jacob | 19b911c | 2017-01-18 18:18:53 +0000 | [diff] [blame] | 8229 | break; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8230 | case OMPD_target_teams: |
Arpith Chacko Jacob | 99a1e0e | 2017-01-25 02:18:43 +0000 | [diff] [blame] | 8231 | CodeGenFunction::EmitOMPTargetTeamsDeviceFunction( |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8232 | CGM, ParentName, cast<OMPTargetTeamsDirective>(E)); |
Arpith Chacko Jacob | 99a1e0e | 2017-01-25 02:18:43 +0000 | [diff] [blame] | 8233 | break; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8234 | case OMPD_target_teams_distribute: |
Alexey Bataev | dfa430f | 2017-12-08 15:03:50 +0000 | [diff] [blame] | 8235 | CodeGenFunction::EmitOMPTargetTeamsDistributeDeviceFunction( |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8236 | CGM, ParentName, cast<OMPTargetTeamsDistributeDirective>(E)); |
Alexey Bataev | dfa430f | 2017-12-08 15:03:50 +0000 | [diff] [blame] | 8237 | break; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8238 | case OMPD_target_teams_distribute_simd: |
Alexey Bataev | fbe17fb | 2017-12-13 19:45:06 +0000 | [diff] [blame] | 8239 | CodeGenFunction::EmitOMPTargetTeamsDistributeSimdDeviceFunction( |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8240 | CGM, ParentName, cast<OMPTargetTeamsDistributeSimdDirective>(E)); |
Alexey Bataev | fbe17fb | 2017-12-13 19:45:06 +0000 | [diff] [blame] | 8241 | break; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8242 | case OMPD_target_parallel_for: |
Alexey Bataev | fb0ebec | 2017-11-08 20:16:14 +0000 | [diff] [blame] | 8243 | CodeGenFunction::EmitOMPTargetParallelForDeviceFunction( |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8244 | CGM, ParentName, cast<OMPTargetParallelForDirective>(E)); |
Alexey Bataev | fb0ebec | 2017-11-08 20:16:14 +0000 | [diff] [blame] | 8245 | break; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8246 | case OMPD_target_parallel_for_simd: |
Alexey Bataev | 5d7edca | 2017-11-09 17:32:15 +0000 | [diff] [blame] | 8247 | CodeGenFunction::EmitOMPTargetParallelForSimdDeviceFunction( |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8248 | CGM, ParentName, cast<OMPTargetParallelForSimdDirective>(E)); |
Alexey Bataev | 5d7edca | 2017-11-09 17:32:15 +0000 | [diff] [blame] | 8249 | break; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8250 | case OMPD_target_simd: |
Alexey Bataev | f836537 | 2017-11-17 17:57:25 +0000 | [diff] [blame] | 8251 | CodeGenFunction::EmitOMPTargetSimdDeviceFunction( |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8252 | CGM, ParentName, cast<OMPTargetSimdDirective>(E)); |
Alexey Bataev | f836537 | 2017-11-17 17:57:25 +0000 | [diff] [blame] | 8253 | break; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8254 | case OMPD_target_teams_distribute_parallel_for: |
Carlo Bertolli | 52978c3 | 2018-01-03 21:12:44 +0000 | [diff] [blame] | 8255 | CodeGenFunction::EmitOMPTargetTeamsDistributeParallelForDeviceFunction( |
| 8256 | CGM, ParentName, |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8257 | cast<OMPTargetTeamsDistributeParallelForDirective>(E)); |
Carlo Bertolli | 52978c3 | 2018-01-03 21:12:44 +0000 | [diff] [blame] | 8258 | break; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8259 | case OMPD_target_teams_distribute_parallel_for_simd: |
Alexey Bataev | 647dd84 | 2018-01-15 20:59:40 +0000 | [diff] [blame] | 8260 | CodeGenFunction:: |
| 8261 | EmitOMPTargetTeamsDistributeParallelForSimdDeviceFunction( |
| 8262 | CGM, ParentName, |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8263 | cast<OMPTargetTeamsDistributeParallelForSimdDirective>(E)); |
Alexey Bataev | 647dd84 | 2018-01-15 20:59:40 +0000 | [diff] [blame] | 8264 | break; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8265 | case OMPD_parallel: |
| 8266 | case OMPD_for: |
| 8267 | case OMPD_parallel_for: |
| 8268 | case OMPD_parallel_sections: |
| 8269 | case OMPD_for_simd: |
| 8270 | case OMPD_parallel_for_simd: |
| 8271 | case OMPD_cancel: |
| 8272 | case OMPD_cancellation_point: |
| 8273 | case OMPD_ordered: |
| 8274 | case OMPD_threadprivate: |
| 8275 | case OMPD_task: |
| 8276 | case OMPD_simd: |
| 8277 | case OMPD_sections: |
| 8278 | case OMPD_section: |
| 8279 | case OMPD_single: |
| 8280 | case OMPD_master: |
| 8281 | case OMPD_critical: |
| 8282 | case OMPD_taskyield: |
| 8283 | case OMPD_barrier: |
| 8284 | case OMPD_taskwait: |
| 8285 | case OMPD_taskgroup: |
| 8286 | case OMPD_atomic: |
| 8287 | case OMPD_flush: |
| 8288 | case OMPD_teams: |
| 8289 | case OMPD_target_data: |
| 8290 | case OMPD_target_exit_data: |
| 8291 | case OMPD_target_enter_data: |
| 8292 | case OMPD_distribute: |
| 8293 | case OMPD_distribute_simd: |
| 8294 | case OMPD_distribute_parallel_for: |
| 8295 | case OMPD_distribute_parallel_for_simd: |
| 8296 | case OMPD_teams_distribute: |
| 8297 | case OMPD_teams_distribute_simd: |
| 8298 | case OMPD_teams_distribute_parallel_for: |
| 8299 | case OMPD_teams_distribute_parallel_for_simd: |
| 8300 | case OMPD_target_update: |
| 8301 | case OMPD_declare_simd: |
| 8302 | case OMPD_declare_target: |
| 8303 | case OMPD_end_declare_target: |
| 8304 | case OMPD_declare_reduction: |
| 8305 | case OMPD_taskloop: |
| 8306 | case OMPD_taskloop_simd: |
Kelvin Li | 1408f91 | 2018-09-26 04:28:39 +0000 | [diff] [blame] | 8307 | case OMPD_requires: |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8308 | case OMPD_unknown: |
Arpith Chacko Jacob | 43a8b7b | 2017-01-16 15:26:02 +0000 | [diff] [blame] | 8309 | llvm_unreachable("Unknown target directive for OpenMP device codegen."); |
| 8310 | } |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 8311 | return; |
| 8312 | } |
| 8313 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8314 | if (const auto *E = dyn_cast<OMPExecutableDirective>(S)) { |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 8315 | if (!E->hasAssociatedStmt() || !E->getAssociatedStmt()) |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 8316 | return; |
| 8317 | |
| 8318 | scanForTargetRegionsFunctions( |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 8319 | E->getInnermostCapturedStmt()->getCapturedStmt(), ParentName); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 8320 | return; |
| 8321 | } |
| 8322 | |
| 8323 | // If this is a lambda function, look into its body. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8324 | if (const auto *L = dyn_cast<LambdaExpr>(S)) |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 8325 | S = L->getBody(); |
| 8326 | |
| 8327 | // Keep looking for target regions recursively. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8328 | for (const Stmt *II : S->children()) |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 8329 | scanForTargetRegionsFunctions(II, ParentName); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 8330 | } |
| 8331 | |
| 8332 | bool CGOpenMPRuntime::emitTargetFunctions(GlobalDecl GD) { |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 8333 | // If emitting code for the host, we do not process FD here. Instead we do |
| 8334 | // the normal code generation. |
| 8335 | if (!CGM.getLangOpts().OpenMPIsDevice) |
| 8336 | return false; |
| 8337 | |
| 8338 | // Try to detect target regions in the function. |
Alexey Bataev | e6aa469 | 2018-09-13 16:54:05 +0000 | [diff] [blame] | 8339 | const ValueDecl *VD = cast<ValueDecl>(GD.getDecl()); |
| 8340 | if (const auto *FD = dyn_cast<FunctionDecl>(VD)) |
| 8341 | scanForTargetRegionsFunctions(FD->getBody(), CGM.getMangledName(GD)); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 8342 | |
Alexey Bataev | 4f4bf7c | 2018-03-15 15:47:20 +0000 | [diff] [blame] | 8343 | // 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] | 8344 | return !OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD) && |
| 8345 | AlreadyEmittedTargetFunctions.count(VD->getCanonicalDecl()) == 0; |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 8346 | } |
| 8347 | |
| 8348 | bool CGOpenMPRuntime::emitTargetGlobalVariable(GlobalDecl GD) { |
| 8349 | if (!CGM.getLangOpts().OpenMPIsDevice) |
| 8350 | return false; |
| 8351 | |
| 8352 | // Check if there are Ctors/Dtors in this declaration and look for target |
| 8353 | // regions in it. We use the complete variant to produce the kernel name |
| 8354 | // mangling. |
| 8355 | QualType RDTy = cast<VarDecl>(GD.getDecl())->getType(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8356 | if (const auto *RD = RDTy->getBaseElementTypeUnsafe()->getAsCXXRecordDecl()) { |
| 8357 | for (const CXXConstructorDecl *Ctor : RD->ctors()) { |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 8358 | StringRef ParentName = |
| 8359 | CGM.getMangledName(GlobalDecl(Ctor, Ctor_Complete)); |
| 8360 | scanForTargetRegionsFunctions(Ctor->getBody(), ParentName); |
| 8361 | } |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8362 | if (const CXXDestructorDecl *Dtor = RD->getDestructor()) { |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 8363 | StringRef ParentName = |
| 8364 | CGM.getMangledName(GlobalDecl(Dtor, Dtor_Complete)); |
| 8365 | scanForTargetRegionsFunctions(Dtor->getBody(), ParentName); |
| 8366 | } |
| 8367 | } |
| 8368 | |
Alexey Bataev | 4f4bf7c | 2018-03-15 15:47:20 +0000 | [diff] [blame] | 8369 | // 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] | 8370 | llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res = |
Alexey Bataev | 97b7221 | 2018-08-14 18:31:20 +0000 | [diff] [blame] | 8371 | OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration( |
| 8372 | cast<VarDecl>(GD.getDecl())); |
Alexey Bataev | bf8fe71 | 2018-08-07 16:14:36 +0000 | [diff] [blame] | 8373 | if (!Res || *Res == OMPDeclareTargetDeclAttr::MT_Link) { |
Alexey Bataev | d01b749 | 2018-08-15 19:45:12 +0000 | [diff] [blame] | 8374 | DeferredGlobalVariables.insert(cast<VarDecl>(GD.getDecl())); |
Alexey Bataev | bf8fe71 | 2018-08-07 16:14:36 +0000 | [diff] [blame] | 8375 | return true; |
| 8376 | } |
| 8377 | return false; |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 8378 | } |
| 8379 | |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 8380 | void CGOpenMPRuntime::registerTargetGlobalVariable(const VarDecl *VD, |
| 8381 | llvm::Constant *Addr) { |
| 8382 | if (llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res = |
Alexey Bataev | 97b7221 | 2018-08-14 18:31:20 +0000 | [diff] [blame] | 8383 | OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD)) { |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 8384 | OffloadEntriesInfoManagerTy::OMPTargetGlobalVarEntryKind Flags; |
| 8385 | StringRef VarName; |
| 8386 | CharUnits VarSize; |
| 8387 | llvm::GlobalValue::LinkageTypes Linkage; |
| 8388 | switch (*Res) { |
| 8389 | case OMPDeclareTargetDeclAttr::MT_To: |
| 8390 | Flags = OffloadEntriesInfoManagerTy::OMPTargetGlobalVarEntryTo; |
| 8391 | VarName = CGM.getMangledName(VD); |
Alexey Bataev | b4dd6d2 | 2018-08-29 20:41:37 +0000 | [diff] [blame] | 8392 | if (VD->hasDefinition(CGM.getContext()) != VarDecl::DeclarationOnly) { |
| 8393 | VarSize = CGM.getContext().getTypeSizeInChars(VD->getType()); |
| 8394 | assert(!VarSize.isZero() && "Expected non-zero size of the variable"); |
| 8395 | } else { |
| 8396 | VarSize = CharUnits::Zero(); |
| 8397 | } |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 8398 | Linkage = CGM.getLLVMLinkageVarDefinition(VD, /*IsConstant=*/false); |
Alexey Bataev | 3823514 | 2018-07-31 16:40:15 +0000 | [diff] [blame] | 8399 | // Temp solution to prevent optimizations of the internal variables. |
| 8400 | if (CGM.getLangOpts().OpenMPIsDevice && !VD->isExternallyVisible()) { |
| 8401 | std::string RefName = getName({VarName, "ref"}); |
| 8402 | if (!CGM.GetGlobalValue(RefName)) { |
| 8403 | llvm::Constant *AddrRef = |
| 8404 | getOrCreateInternalVariable(Addr->getType(), RefName); |
| 8405 | auto *GVAddrRef = cast<llvm::GlobalVariable>(AddrRef); |
| 8406 | GVAddrRef->setConstant(/*Val=*/true); |
| 8407 | GVAddrRef->setLinkage(llvm::GlobalValue::InternalLinkage); |
| 8408 | GVAddrRef->setInitializer(Addr); |
| 8409 | CGM.addCompilerUsedGlobal(GVAddrRef); |
| 8410 | } |
| 8411 | } |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 8412 | break; |
| 8413 | case OMPDeclareTargetDeclAttr::MT_Link: |
Alexey Bataev | c52f01d | 2018-07-16 20:05:25 +0000 | [diff] [blame] | 8414 | Flags = OffloadEntriesInfoManagerTy::OMPTargetGlobalVarEntryLink; |
| 8415 | if (CGM.getLangOpts().OpenMPIsDevice) { |
| 8416 | VarName = Addr->getName(); |
| 8417 | Addr = nullptr; |
| 8418 | } else { |
| 8419 | VarName = getAddrOfDeclareTargetLink(VD).getName(); |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 8420 | Addr = |
| 8421 | cast<llvm::Constant>(getAddrOfDeclareTargetLink(VD).getPointer()); |
| 8422 | } |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 8423 | VarSize = CGM.getPointerSize(); |
| 8424 | Linkage = llvm::GlobalValue::WeakAnyLinkage; |
| 8425 | break; |
| 8426 | } |
| 8427 | OffloadEntriesInfoManager.registerDeviceGlobalVarEntryInfo( |
| 8428 | VarName, Addr, VarSize, Flags, Linkage); |
| 8429 | } |
| 8430 | } |
| 8431 | |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 8432 | bool CGOpenMPRuntime::emitTargetGlobal(GlobalDecl GD) { |
Alexey Bataev | e6aa469 | 2018-09-13 16:54:05 +0000 | [diff] [blame] | 8433 | if (isa<FunctionDecl>(GD.getDecl()) || |
| 8434 | isa<OMPDeclareReductionDecl>(GD.getDecl())) |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 8435 | return emitTargetFunctions(GD); |
| 8436 | |
| 8437 | return emitTargetGlobalVariable(GD); |
| 8438 | } |
| 8439 | |
Alexey Bataev | bf8fe71 | 2018-08-07 16:14:36 +0000 | [diff] [blame] | 8440 | void CGOpenMPRuntime::emitDeferredTargetDecls() const { |
| 8441 | for (const VarDecl *VD : DeferredGlobalVariables) { |
| 8442 | llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res = |
Alexey Bataev | 97b7221 | 2018-08-14 18:31:20 +0000 | [diff] [blame] | 8443 | OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD); |
Alexey Bataev | d01b749 | 2018-08-15 19:45:12 +0000 | [diff] [blame] | 8444 | if (!Res) |
| 8445 | continue; |
| 8446 | if (*Res == OMPDeclareTargetDeclAttr::MT_To) { |
Alexey Bataev | bf8fe71 | 2018-08-07 16:14:36 +0000 | [diff] [blame] | 8447 | CGM.EmitGlobal(VD); |
Alexey Bataev | d01b749 | 2018-08-15 19:45:12 +0000 | [diff] [blame] | 8448 | } else { |
| 8449 | assert(*Res == OMPDeclareTargetDeclAttr::MT_Link && |
| 8450 | "Expected to or link clauses."); |
| 8451 | (void)CGM.getOpenMPRuntime().getAddrOfDeclareTargetLink(VD); |
Alexey Bataev | bf8fe71 | 2018-08-07 16:14:36 +0000 | [diff] [blame] | 8452 | } |
| 8453 | } |
| 8454 | } |
| 8455 | |
Alexey Bataev | 4f4bf7c | 2018-03-15 15:47:20 +0000 | [diff] [blame] | 8456 | CGOpenMPRuntime::DisableAutoDeclareTargetRAII::DisableAutoDeclareTargetRAII( |
| 8457 | CodeGenModule &CGM) |
| 8458 | : CGM(CGM) { |
| 8459 | if (CGM.getLangOpts().OpenMPIsDevice) { |
| 8460 | SavedShouldMarkAsGlobal = CGM.getOpenMPRuntime().ShouldMarkAsGlobal; |
| 8461 | CGM.getOpenMPRuntime().ShouldMarkAsGlobal = false; |
| 8462 | } |
| 8463 | } |
| 8464 | |
| 8465 | CGOpenMPRuntime::DisableAutoDeclareTargetRAII::~DisableAutoDeclareTargetRAII() { |
| 8466 | if (CGM.getLangOpts().OpenMPIsDevice) |
| 8467 | CGM.getOpenMPRuntime().ShouldMarkAsGlobal = SavedShouldMarkAsGlobal; |
| 8468 | } |
| 8469 | |
Alexey Bataev | 6d94410 | 2018-05-02 15:45:28 +0000 | [diff] [blame] | 8470 | bool CGOpenMPRuntime::markAsGlobalTarget(GlobalDecl GD) { |
Alexey Bataev | 4f4bf7c | 2018-03-15 15:47:20 +0000 | [diff] [blame] | 8471 | if (!CGM.getLangOpts().OpenMPIsDevice || !ShouldMarkAsGlobal) |
| 8472 | return true; |
Alexey Bataev | 4f4bf7c | 2018-03-15 15:47:20 +0000 | [diff] [blame] | 8473 | |
Alexey Bataev | 6d94410 | 2018-05-02 15:45:28 +0000 | [diff] [blame] | 8474 | const auto *D = cast<FunctionDecl>(GD.getDecl()); |
Alexey Bataev | 4f4bf7c | 2018-03-15 15:47:20 +0000 | [diff] [blame] | 8475 | const FunctionDecl *FD = D->getCanonicalDecl(); |
Alexey Bataev | 34f8a70 | 2018-03-28 14:28:54 +0000 | [diff] [blame] | 8476 | // Do not to emit function if it is marked as declare target as it was already |
| 8477 | // emitted. |
Alexey Bataev | 97b7221 | 2018-08-14 18:31:20 +0000 | [diff] [blame] | 8478 | if (OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(D)) { |
Alexey Bataev | 34f8a70 | 2018-03-28 14:28:54 +0000 | [diff] [blame] | 8479 | if (D->hasBody() && AlreadyEmittedTargetFunctions.count(FD) == 0) { |
| 8480 | if (auto *F = dyn_cast_or_null<llvm::Function>( |
Alexey Bataev | 6d94410 | 2018-05-02 15:45:28 +0000 | [diff] [blame] | 8481 | CGM.GetGlobalValue(CGM.getMangledName(GD)))) |
Alexey Bataev | 34f8a70 | 2018-03-28 14:28:54 +0000 | [diff] [blame] | 8482 | return !F->isDeclaration(); |
| 8483 | return false; |
| 8484 | } |
| 8485 | return true; |
| 8486 | } |
| 8487 | |
Alexey Bataev | 4f4bf7c | 2018-03-15 15:47:20 +0000 | [diff] [blame] | 8488 | return !AlreadyEmittedTargetFunctions.insert(FD).second; |
| 8489 | } |
| 8490 | |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 8491 | llvm::Function *CGOpenMPRuntime::emitRegistrationFunction() { |
| 8492 | // If we have offloading in the current module, we need to emit the entries |
| 8493 | // now and register the offloading descriptor. |
| 8494 | createOffloadEntriesAndInfoMetadata(); |
| 8495 | |
| 8496 | // Create and register the offloading binary descriptors. This is the main |
| 8497 | // entity that captures all the information about offloading in the current |
| 8498 | // compilation unit. |
| 8499 | return createOffloadingBinaryDescriptorRegistration(); |
| 8500 | } |
Carlo Bertolli | 430d8ec | 2016-03-03 20:34:23 +0000 | [diff] [blame] | 8501 | |
| 8502 | void CGOpenMPRuntime::emitTeamsCall(CodeGenFunction &CGF, |
| 8503 | const OMPExecutableDirective &D, |
| 8504 | SourceLocation Loc, |
| 8505 | llvm::Value *OutlinedFn, |
| 8506 | ArrayRef<llvm::Value *> CapturedVars) { |
| 8507 | if (!CGF.HaveInsertPoint()) |
| 8508 | return; |
| 8509 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8510 | llvm::Value *RTLoc = emitUpdateLocation(CGF, Loc); |
Carlo Bertolli | 430d8ec | 2016-03-03 20:34:23 +0000 | [diff] [blame] | 8511 | CodeGenFunction::RunCleanupsScope Scope(CGF); |
| 8512 | |
| 8513 | // Build call __kmpc_fork_teams(loc, n, microtask, var1, .., varn); |
| 8514 | llvm::Value *Args[] = { |
| 8515 | RTLoc, |
| 8516 | CGF.Builder.getInt32(CapturedVars.size()), // Number of captured vars |
| 8517 | CGF.Builder.CreateBitCast(OutlinedFn, getKmpc_MicroPointerTy())}; |
| 8518 | llvm::SmallVector<llvm::Value *, 16> RealArgs; |
| 8519 | RealArgs.append(std::begin(Args), std::end(Args)); |
| 8520 | RealArgs.append(CapturedVars.begin(), CapturedVars.end()); |
| 8521 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8522 | llvm::Value *RTLFn = createRuntimeFunction(OMPRTL__kmpc_fork_teams); |
Carlo Bertolli | 430d8ec | 2016-03-03 20:34:23 +0000 | [diff] [blame] | 8523 | CGF.EmitRuntimeCall(RTLFn, RealArgs); |
| 8524 | } |
| 8525 | |
| 8526 | void CGOpenMPRuntime::emitNumTeamsClause(CodeGenFunction &CGF, |
Carlo Bertolli | c687225 | 2016-04-04 15:55:02 +0000 | [diff] [blame] | 8527 | const Expr *NumTeams, |
| 8528 | const Expr *ThreadLimit, |
Carlo Bertolli | 430d8ec | 2016-03-03 20:34:23 +0000 | [diff] [blame] | 8529 | SourceLocation Loc) { |
| 8530 | if (!CGF.HaveInsertPoint()) |
| 8531 | return; |
| 8532 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8533 | llvm::Value *RTLoc = emitUpdateLocation(CGF, Loc); |
Carlo Bertolli | 430d8ec | 2016-03-03 20:34:23 +0000 | [diff] [blame] | 8534 | |
Carlo Bertolli | c687225 | 2016-04-04 15:55:02 +0000 | [diff] [blame] | 8535 | llvm::Value *NumTeamsVal = |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8536 | NumTeams |
Carlo Bertolli | c687225 | 2016-04-04 15:55:02 +0000 | [diff] [blame] | 8537 | ? CGF.Builder.CreateIntCast(CGF.EmitScalarExpr(NumTeams), |
| 8538 | CGF.CGM.Int32Ty, /* isSigned = */ true) |
| 8539 | : CGF.Builder.getInt32(0); |
| 8540 | |
| 8541 | llvm::Value *ThreadLimitVal = |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8542 | ThreadLimit |
Carlo Bertolli | c687225 | 2016-04-04 15:55:02 +0000 | [diff] [blame] | 8543 | ? CGF.Builder.CreateIntCast(CGF.EmitScalarExpr(ThreadLimit), |
| 8544 | CGF.CGM.Int32Ty, /* isSigned = */ true) |
| 8545 | : CGF.Builder.getInt32(0); |
| 8546 | |
Carlo Bertolli | 430d8ec | 2016-03-03 20:34:23 +0000 | [diff] [blame] | 8547 | // 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] | 8548 | llvm::Value *PushNumTeamsArgs[] = {RTLoc, getThreadID(CGF, Loc), NumTeamsVal, |
| 8549 | ThreadLimitVal}; |
Carlo Bertolli | 430d8ec | 2016-03-03 20:34:23 +0000 | [diff] [blame] | 8550 | CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_push_num_teams), |
| 8551 | PushNumTeamsArgs); |
| 8552 | } |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8553 | |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 8554 | void CGOpenMPRuntime::emitTargetDataCalls( |
| 8555 | CodeGenFunction &CGF, const OMPExecutableDirective &D, const Expr *IfCond, |
| 8556 | const Expr *Device, const RegionCodeGenTy &CodeGen, TargetDataInfo &Info) { |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8557 | if (!CGF.HaveInsertPoint()) |
| 8558 | return; |
| 8559 | |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 8560 | // Action used to replace the default codegen action and turn privatization |
| 8561 | // off. |
| 8562 | PrePostActionTy NoPrivAction; |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8563 | |
| 8564 | // Generate the code for the opening of the data environment. Capture all the |
| 8565 | // arguments of the runtime call by reference because they are used in the |
| 8566 | // closing of the region. |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 8567 | auto &&BeginThenGen = [this, &D, Device, &Info, |
| 8568 | &CodeGen](CodeGenFunction &CGF, PrePostActionTy &) { |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8569 | // Fill up the arrays with all the mapped variables. |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 8570 | MappableExprsHandler::MapBaseValuesArrayTy BasePointers; |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8571 | MappableExprsHandler::MapValuesArrayTy Pointers; |
| 8572 | MappableExprsHandler::MapValuesArrayTy Sizes; |
| 8573 | MappableExprsHandler::MapFlagsArrayTy MapTypes; |
| 8574 | |
| 8575 | // Get map clause information. |
| 8576 | MappableExprsHandler MCHandler(D, CGF); |
| 8577 | MCHandler.generateAllInfo(BasePointers, Pointers, Sizes, MapTypes); |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8578 | |
| 8579 | // Fill up the arrays and create the arguments. |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 8580 | emitOffloadingArrays(CGF, BasePointers, Pointers, Sizes, MapTypes, Info); |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8581 | |
| 8582 | llvm::Value *BasePointersArrayArg = nullptr; |
| 8583 | llvm::Value *PointersArrayArg = nullptr; |
| 8584 | llvm::Value *SizesArrayArg = nullptr; |
| 8585 | llvm::Value *MapTypesArrayArg = nullptr; |
| 8586 | emitOffloadingArraysArgument(CGF, BasePointersArrayArg, PointersArrayArg, |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 8587 | SizesArrayArg, MapTypesArrayArg, Info); |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8588 | |
| 8589 | // Emit device ID if any. |
| 8590 | llvm::Value *DeviceID = nullptr; |
George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 8591 | if (Device) { |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8592 | DeviceID = CGF.Builder.CreateIntCast(CGF.EmitScalarExpr(Device), |
George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 8593 | CGF.Int64Ty, /*isSigned=*/true); |
| 8594 | } else { |
| 8595 | DeviceID = CGF.Builder.getInt64(OMP_DEVICEID_UNDEF); |
| 8596 | } |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8597 | |
| 8598 | // Emit the number of elements in the offloading arrays. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8599 | llvm::Value *PointerNum = CGF.Builder.getInt32(Info.NumberOfPtrs); |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8600 | |
| 8601 | llvm::Value *OffloadingArgs[] = { |
| 8602 | DeviceID, PointerNum, BasePointersArrayArg, |
| 8603 | PointersArrayArg, SizesArrayArg, MapTypesArrayArg}; |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 8604 | CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__tgt_target_data_begin), |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8605 | OffloadingArgs); |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 8606 | |
| 8607 | // If device pointer privatization is required, emit the body of the region |
| 8608 | // here. It will have to be duplicated: with and without privatization. |
| 8609 | if (!Info.CaptureDeviceAddrMap.empty()) |
| 8610 | CodeGen(CGF); |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8611 | }; |
| 8612 | |
| 8613 | // Generate code for the closing of the data region. |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 8614 | auto &&EndThenGen = [this, Device, &Info](CodeGenFunction &CGF, |
| 8615 | PrePostActionTy &) { |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 8616 | assert(Info.isValid() && "Invalid data environment closing arguments."); |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8617 | |
| 8618 | llvm::Value *BasePointersArrayArg = nullptr; |
| 8619 | llvm::Value *PointersArrayArg = nullptr; |
| 8620 | llvm::Value *SizesArrayArg = nullptr; |
| 8621 | llvm::Value *MapTypesArrayArg = nullptr; |
| 8622 | emitOffloadingArraysArgument(CGF, BasePointersArrayArg, PointersArrayArg, |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 8623 | SizesArrayArg, MapTypesArrayArg, Info); |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8624 | |
| 8625 | // Emit device ID if any. |
| 8626 | llvm::Value *DeviceID = nullptr; |
George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 8627 | if (Device) { |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8628 | DeviceID = CGF.Builder.CreateIntCast(CGF.EmitScalarExpr(Device), |
George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 8629 | CGF.Int64Ty, /*isSigned=*/true); |
| 8630 | } else { |
| 8631 | DeviceID = CGF.Builder.getInt64(OMP_DEVICEID_UNDEF); |
| 8632 | } |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8633 | |
| 8634 | // Emit the number of elements in the offloading arrays. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8635 | llvm::Value *PointerNum = CGF.Builder.getInt32(Info.NumberOfPtrs); |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8636 | |
| 8637 | llvm::Value *OffloadingArgs[] = { |
| 8638 | DeviceID, PointerNum, BasePointersArrayArg, |
| 8639 | PointersArrayArg, SizesArrayArg, MapTypesArrayArg}; |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 8640 | CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__tgt_target_data_end), |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8641 | OffloadingArgs); |
| 8642 | }; |
| 8643 | |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 8644 | // If we need device pointer privatization, we need to emit the body of the |
| 8645 | // region with no privatization in the 'else' branch of the conditional. |
| 8646 | // Otherwise, we don't have to do anything. |
| 8647 | auto &&BeginElseGen = [&Info, &CodeGen, &NoPrivAction](CodeGenFunction &CGF, |
| 8648 | PrePostActionTy &) { |
| 8649 | if (!Info.CaptureDeviceAddrMap.empty()) { |
| 8650 | CodeGen.setAction(NoPrivAction); |
| 8651 | CodeGen(CGF); |
| 8652 | } |
| 8653 | }; |
| 8654 | |
| 8655 | // We don't have to do anything to close the region if the if clause evaluates |
| 8656 | // to false. |
| 8657 | auto &&EndElseGen = [](CodeGenFunction &CGF, PrePostActionTy &) {}; |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8658 | |
| 8659 | if (IfCond) { |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 8660 | emitOMPIfClause(CGF, IfCond, BeginThenGen, BeginElseGen); |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8661 | } else { |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 8662 | RegionCodeGenTy RCG(BeginThenGen); |
| 8663 | RCG(CGF); |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8664 | } |
| 8665 | |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 8666 | // If we don't require privatization of device pointers, we emit the body in |
| 8667 | // between the runtime calls. This avoids duplicating the body code. |
| 8668 | if (Info.CaptureDeviceAddrMap.empty()) { |
| 8669 | CodeGen.setAction(NoPrivAction); |
| 8670 | CodeGen(CGF); |
| 8671 | } |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8672 | |
| 8673 | if (IfCond) { |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 8674 | emitOMPIfClause(CGF, IfCond, EndThenGen, EndElseGen); |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8675 | } else { |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 8676 | RegionCodeGenTy RCG(EndThenGen); |
| 8677 | RCG(CGF); |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8678 | } |
| 8679 | } |
Samuel Antao | bd0ae2e | 2016-04-27 23:07:29 +0000 | [diff] [blame] | 8680 | |
Samuel Antao | 8d2d730 | 2016-05-26 18:30:22 +0000 | [diff] [blame] | 8681 | void CGOpenMPRuntime::emitTargetDataStandAloneCall( |
Samuel Antao | 8dd6628 | 2016-04-27 23:14:30 +0000 | [diff] [blame] | 8682 | CodeGenFunction &CGF, const OMPExecutableDirective &D, const Expr *IfCond, |
| 8683 | const Expr *Device) { |
Samuel Antao | bd0ae2e | 2016-04-27 23:07:29 +0000 | [diff] [blame] | 8684 | if (!CGF.HaveInsertPoint()) |
| 8685 | return; |
| 8686 | |
Samuel Antao | 8dd6628 | 2016-04-27 23:14:30 +0000 | [diff] [blame] | 8687 | assert((isa<OMPTargetEnterDataDirective>(D) || |
Samuel Antao | 8d2d730 | 2016-05-26 18:30:22 +0000 | [diff] [blame] | 8688 | isa<OMPTargetExitDataDirective>(D) || |
| 8689 | isa<OMPTargetUpdateDirective>(D)) && |
| 8690 | "Expecting either target enter, exit data, or update directives."); |
Samuel Antao | 8dd6628 | 2016-04-27 23:14:30 +0000 | [diff] [blame] | 8691 | |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 8692 | CodeGenFunction::OMPTargetDataInfo InputInfo; |
| 8693 | llvm::Value *MapTypesArray = nullptr; |
Samuel Antao | bd0ae2e | 2016-04-27 23:07:29 +0000 | [diff] [blame] | 8694 | // Generate the code for the opening of the data environment. |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 8695 | auto &&ThenGen = [this, &D, Device, &InputInfo, |
| 8696 | &MapTypesArray](CodeGenFunction &CGF, PrePostActionTy &) { |
Samuel Antao | bd0ae2e | 2016-04-27 23:07:29 +0000 | [diff] [blame] | 8697 | // Emit device ID if any. |
| 8698 | llvm::Value *DeviceID = nullptr; |
George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 8699 | if (Device) { |
Samuel Antao | bd0ae2e | 2016-04-27 23:07:29 +0000 | [diff] [blame] | 8700 | DeviceID = CGF.Builder.CreateIntCast(CGF.EmitScalarExpr(Device), |
George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 8701 | CGF.Int64Ty, /*isSigned=*/true); |
| 8702 | } else { |
| 8703 | DeviceID = CGF.Builder.getInt64(OMP_DEVICEID_UNDEF); |
| 8704 | } |
Samuel Antao | bd0ae2e | 2016-04-27 23:07:29 +0000 | [diff] [blame] | 8705 | |
| 8706 | // Emit the number of elements in the offloading arrays. |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 8707 | llvm::Constant *PointerNum = |
| 8708 | CGF.Builder.getInt32(InputInfo.NumberOfTargetItems); |
Samuel Antao | bd0ae2e | 2016-04-27 23:07:29 +0000 | [diff] [blame] | 8709 | |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 8710 | llvm::Value *OffloadingArgs[] = {DeviceID, |
| 8711 | PointerNum, |
| 8712 | InputInfo.BasePointersArray.getPointer(), |
| 8713 | InputInfo.PointersArray.getPointer(), |
| 8714 | InputInfo.SizesArray.getPointer(), |
| 8715 | MapTypesArray}; |
Samuel Antao | 8d2d730 | 2016-05-26 18:30:22 +0000 | [diff] [blame] | 8716 | |
Samuel Antao | 8d2d730 | 2016-05-26 18:30:22 +0000 | [diff] [blame] | 8717 | // Select the right runtime function call for each expected standalone |
| 8718 | // directive. |
Alexey Bataev | 0cc6b8e | 2017-12-14 17:00:17 +0000 | [diff] [blame] | 8719 | const bool HasNowait = D.hasClausesOfKind<OMPNowaitClause>(); |
Samuel Antao | 8d2d730 | 2016-05-26 18:30:22 +0000 | [diff] [blame] | 8720 | OpenMPRTLFunction RTLFn; |
| 8721 | switch (D.getDirectiveKind()) { |
Samuel Antao | 8d2d730 | 2016-05-26 18:30:22 +0000 | [diff] [blame] | 8722 | case OMPD_target_enter_data: |
Alexey Bataev | 0cc6b8e | 2017-12-14 17:00:17 +0000 | [diff] [blame] | 8723 | RTLFn = HasNowait ? OMPRTL__tgt_target_data_begin_nowait |
| 8724 | : OMPRTL__tgt_target_data_begin; |
Samuel Antao | 8d2d730 | 2016-05-26 18:30:22 +0000 | [diff] [blame] | 8725 | break; |
| 8726 | case OMPD_target_exit_data: |
Alexey Bataev | 0cc6b8e | 2017-12-14 17:00:17 +0000 | [diff] [blame] | 8727 | RTLFn = HasNowait ? OMPRTL__tgt_target_data_end_nowait |
| 8728 | : OMPRTL__tgt_target_data_end; |
Samuel Antao | 8d2d730 | 2016-05-26 18:30:22 +0000 | [diff] [blame] | 8729 | break; |
| 8730 | case OMPD_target_update: |
Alexey Bataev | 0cc6b8e | 2017-12-14 17:00:17 +0000 | [diff] [blame] | 8731 | RTLFn = HasNowait ? OMPRTL__tgt_target_data_update_nowait |
| 8732 | : OMPRTL__tgt_target_data_update; |
Samuel Antao | 8d2d730 | 2016-05-26 18:30:22 +0000 | [diff] [blame] | 8733 | break; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8734 | case OMPD_parallel: |
| 8735 | case OMPD_for: |
| 8736 | case OMPD_parallel_for: |
| 8737 | case OMPD_parallel_sections: |
| 8738 | case OMPD_for_simd: |
| 8739 | case OMPD_parallel_for_simd: |
| 8740 | case OMPD_cancel: |
| 8741 | case OMPD_cancellation_point: |
| 8742 | case OMPD_ordered: |
| 8743 | case OMPD_threadprivate: |
| 8744 | case OMPD_task: |
| 8745 | case OMPD_simd: |
| 8746 | case OMPD_sections: |
| 8747 | case OMPD_section: |
| 8748 | case OMPD_single: |
| 8749 | case OMPD_master: |
| 8750 | case OMPD_critical: |
| 8751 | case OMPD_taskyield: |
| 8752 | case OMPD_barrier: |
| 8753 | case OMPD_taskwait: |
| 8754 | case OMPD_taskgroup: |
| 8755 | case OMPD_atomic: |
| 8756 | case OMPD_flush: |
| 8757 | case OMPD_teams: |
| 8758 | case OMPD_target_data: |
| 8759 | case OMPD_distribute: |
| 8760 | case OMPD_distribute_simd: |
| 8761 | case OMPD_distribute_parallel_for: |
| 8762 | case OMPD_distribute_parallel_for_simd: |
| 8763 | case OMPD_teams_distribute: |
| 8764 | case OMPD_teams_distribute_simd: |
| 8765 | case OMPD_teams_distribute_parallel_for: |
| 8766 | case OMPD_teams_distribute_parallel_for_simd: |
| 8767 | case OMPD_declare_simd: |
| 8768 | case OMPD_declare_target: |
| 8769 | case OMPD_end_declare_target: |
| 8770 | case OMPD_declare_reduction: |
| 8771 | case OMPD_taskloop: |
| 8772 | case OMPD_taskloop_simd: |
| 8773 | case OMPD_target: |
| 8774 | case OMPD_target_simd: |
| 8775 | case OMPD_target_teams_distribute: |
| 8776 | case OMPD_target_teams_distribute_simd: |
| 8777 | case OMPD_target_teams_distribute_parallel_for: |
| 8778 | case OMPD_target_teams_distribute_parallel_for_simd: |
| 8779 | case OMPD_target_teams: |
| 8780 | case OMPD_target_parallel: |
| 8781 | case OMPD_target_parallel_for: |
| 8782 | case OMPD_target_parallel_for_simd: |
Kelvin Li | 1408f91 | 2018-09-26 04:28:39 +0000 | [diff] [blame] | 8783 | case OMPD_requires: |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8784 | case OMPD_unknown: |
| 8785 | llvm_unreachable("Unexpected standalone target data directive."); |
| 8786 | break; |
Samuel Antao | 8d2d730 | 2016-05-26 18:30:22 +0000 | [diff] [blame] | 8787 | } |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 8788 | CGF.EmitRuntimeCall(createRuntimeFunction(RTLFn), OffloadingArgs); |
Samuel Antao | bd0ae2e | 2016-04-27 23:07:29 +0000 | [diff] [blame] | 8789 | }; |
| 8790 | |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 8791 | auto &&TargetThenGen = [this, &ThenGen, &D, &InputInfo, &MapTypesArray]( |
| 8792 | CodeGenFunction &CGF, PrePostActionTy &) { |
| 8793 | // Fill up the arrays with all the mapped variables. |
| 8794 | MappableExprsHandler::MapBaseValuesArrayTy BasePointers; |
| 8795 | MappableExprsHandler::MapValuesArrayTy Pointers; |
| 8796 | MappableExprsHandler::MapValuesArrayTy Sizes; |
| 8797 | MappableExprsHandler::MapFlagsArrayTy MapTypes; |
Samuel Antao | bd0ae2e | 2016-04-27 23:07:29 +0000 | [diff] [blame] | 8798 | |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 8799 | // Get map clause information. |
| 8800 | MappableExprsHandler MEHandler(D, CGF); |
| 8801 | MEHandler.generateAllInfo(BasePointers, Pointers, Sizes, MapTypes); |
| 8802 | |
| 8803 | TargetDataInfo Info; |
| 8804 | // Fill up the arrays and create the arguments. |
| 8805 | emitOffloadingArrays(CGF, BasePointers, Pointers, Sizes, MapTypes, Info); |
| 8806 | emitOffloadingArraysArgument(CGF, Info.BasePointersArray, |
| 8807 | Info.PointersArray, Info.SizesArray, |
| 8808 | Info.MapTypesArray, Info); |
| 8809 | InputInfo.NumberOfTargetItems = Info.NumberOfPtrs; |
| 8810 | InputInfo.BasePointersArray = |
| 8811 | Address(Info.BasePointersArray, CGM.getPointerAlign()); |
| 8812 | InputInfo.PointersArray = |
| 8813 | Address(Info.PointersArray, CGM.getPointerAlign()); |
| 8814 | InputInfo.SizesArray = |
| 8815 | Address(Info.SizesArray, CGM.getPointerAlign()); |
| 8816 | MapTypesArray = Info.MapTypesArray; |
| 8817 | if (D.hasClausesOfKind<OMPDependClause>()) |
| 8818 | CGF.EmitOMPTargetTaskBasedDirective(D, ThenGen, InputInfo); |
| 8819 | else |
Alexey Bataev | 768f1f2 | 2018-01-09 19:59:25 +0000 | [diff] [blame] | 8820 | emitInlinedDirective(CGF, D.getDirectiveKind(), ThenGen); |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 8821 | }; |
| 8822 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8823 | if (IfCond) { |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 8824 | emitOMPIfClause(CGF, IfCond, TargetThenGen, |
| 8825 | [](CodeGenFunction &CGF, PrePostActionTy &) {}); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8826 | } else { |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 8827 | RegionCodeGenTy ThenRCG(TargetThenGen); |
| 8828 | ThenRCG(CGF); |
Samuel Antao | bd0ae2e | 2016-04-27 23:07:29 +0000 | [diff] [blame] | 8829 | } |
| 8830 | } |
Alexey Bataev | c7a82b4 | 2016-05-06 09:40:08 +0000 | [diff] [blame] | 8831 | |
| 8832 | namespace { |
| 8833 | /// Kind of parameter in a function with 'declare simd' directive. |
| 8834 | enum ParamKindTy { LinearWithVarStride, Linear, Uniform, Vector }; |
| 8835 | /// Attribute set of the parameter. |
| 8836 | struct ParamAttrTy { |
| 8837 | ParamKindTy Kind = Vector; |
| 8838 | llvm::APSInt StrideOrArg; |
| 8839 | llvm::APSInt Alignment; |
| 8840 | }; |
| 8841 | } // namespace |
| 8842 | |
| 8843 | static unsigned evaluateCDTSize(const FunctionDecl *FD, |
| 8844 | ArrayRef<ParamAttrTy> ParamAttrs) { |
| 8845 | // Every vector variant of a SIMD-enabled function has a vector length (VLEN). |
| 8846 | // If OpenMP clause "simdlen" is used, the VLEN is the value of the argument |
| 8847 | // of that clause. The VLEN value must be power of 2. |
| 8848 | // In other case the notion of the function`s "characteristic data type" (CDT) |
| 8849 | // is used to compute the vector length. |
| 8850 | // CDT is defined in the following order: |
| 8851 | // a) For non-void function, the CDT is the return type. |
| 8852 | // b) If the function has any non-uniform, non-linear parameters, then the |
| 8853 | // CDT is the type of the first such parameter. |
| 8854 | // c) If the CDT determined by a) or b) above is struct, union, or class |
| 8855 | // type which is pass-by-value (except for the type that maps to the |
| 8856 | // built-in complex data type), the characteristic data type is int. |
| 8857 | // d) If none of the above three cases is applicable, the CDT is int. |
| 8858 | // The VLEN is then determined based on the CDT and the size of vector |
| 8859 | // register of that ISA for which current vector version is generated. The |
| 8860 | // VLEN is computed using the formula below: |
| 8861 | // VLEN = sizeof(vector_register) / sizeof(CDT), |
| 8862 | // where vector register size specified in section 3.2.1 Registers and the |
| 8863 | // Stack Frame of original AMD64 ABI document. |
| 8864 | QualType RetType = FD->getReturnType(); |
| 8865 | if (RetType.isNull()) |
| 8866 | return 0; |
| 8867 | ASTContext &C = FD->getASTContext(); |
| 8868 | QualType CDT; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8869 | if (!RetType.isNull() && !RetType->isVoidType()) { |
Alexey Bataev | c7a82b4 | 2016-05-06 09:40:08 +0000 | [diff] [blame] | 8870 | CDT = RetType; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8871 | } else { |
Alexey Bataev | c7a82b4 | 2016-05-06 09:40:08 +0000 | [diff] [blame] | 8872 | unsigned Offset = 0; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8873 | if (const auto *MD = dyn_cast<CXXMethodDecl>(FD)) { |
Alexey Bataev | c7a82b4 | 2016-05-06 09:40:08 +0000 | [diff] [blame] | 8874 | if (ParamAttrs[Offset].Kind == Vector) |
| 8875 | CDT = C.getPointerType(C.getRecordType(MD->getParent())); |
| 8876 | ++Offset; |
| 8877 | } |
| 8878 | if (CDT.isNull()) { |
| 8879 | for (unsigned I = 0, E = FD->getNumParams(); I < E; ++I) { |
| 8880 | if (ParamAttrs[I + Offset].Kind == Vector) { |
| 8881 | CDT = FD->getParamDecl(I)->getType(); |
| 8882 | break; |
| 8883 | } |
| 8884 | } |
| 8885 | } |
| 8886 | } |
| 8887 | if (CDT.isNull()) |
| 8888 | CDT = C.IntTy; |
| 8889 | CDT = CDT->getCanonicalTypeUnqualified(); |
| 8890 | if (CDT->isRecordType() || CDT->isUnionType()) |
| 8891 | CDT = C.IntTy; |
| 8892 | return C.getTypeSize(CDT); |
| 8893 | } |
| 8894 | |
| 8895 | static void |
| 8896 | emitX86DeclareSimdFunction(const FunctionDecl *FD, llvm::Function *Fn, |
Benjamin Kramer | 81cb4b7 | 2016-11-24 16:01:20 +0000 | [diff] [blame] | 8897 | const llvm::APSInt &VLENVal, |
Alexey Bataev | c7a82b4 | 2016-05-06 09:40:08 +0000 | [diff] [blame] | 8898 | ArrayRef<ParamAttrTy> ParamAttrs, |
| 8899 | OMPDeclareSimdDeclAttr::BranchStateTy State) { |
| 8900 | struct ISADataTy { |
| 8901 | char ISA; |
| 8902 | unsigned VecRegSize; |
| 8903 | }; |
| 8904 | ISADataTy ISAData[] = { |
| 8905 | { |
| 8906 | 'b', 128 |
| 8907 | }, // SSE |
| 8908 | { |
| 8909 | 'c', 256 |
| 8910 | }, // AVX |
| 8911 | { |
| 8912 | 'd', 256 |
| 8913 | }, // AVX2 |
| 8914 | { |
| 8915 | 'e', 512 |
| 8916 | }, // AVX512 |
| 8917 | }; |
| 8918 | llvm::SmallVector<char, 2> Masked; |
| 8919 | switch (State) { |
| 8920 | case OMPDeclareSimdDeclAttr::BS_Undefined: |
| 8921 | Masked.push_back('N'); |
| 8922 | Masked.push_back('M'); |
| 8923 | break; |
| 8924 | case OMPDeclareSimdDeclAttr::BS_Notinbranch: |
| 8925 | Masked.push_back('N'); |
| 8926 | break; |
| 8927 | case OMPDeclareSimdDeclAttr::BS_Inbranch: |
| 8928 | Masked.push_back('M'); |
| 8929 | break; |
| 8930 | } |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8931 | for (char Mask : Masked) { |
| 8932 | for (const ISADataTy &Data : ISAData) { |
Alexey Bataev | c7a82b4 | 2016-05-06 09:40:08 +0000 | [diff] [blame] | 8933 | SmallString<256> Buffer; |
| 8934 | llvm::raw_svector_ostream Out(Buffer); |
| 8935 | Out << "_ZGV" << Data.ISA << Mask; |
| 8936 | if (!VLENVal) { |
| 8937 | Out << llvm::APSInt::getUnsigned(Data.VecRegSize / |
| 8938 | evaluateCDTSize(FD, ParamAttrs)); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8939 | } else { |
Alexey Bataev | c7a82b4 | 2016-05-06 09:40:08 +0000 | [diff] [blame] | 8940 | Out << VLENVal; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8941 | } |
| 8942 | for (const ParamAttrTy &ParamAttr : ParamAttrs) { |
Alexey Bataev | c7a82b4 | 2016-05-06 09:40:08 +0000 | [diff] [blame] | 8943 | switch (ParamAttr.Kind){ |
| 8944 | case LinearWithVarStride: |
| 8945 | Out << 's' << ParamAttr.StrideOrArg; |
| 8946 | break; |
| 8947 | case Linear: |
| 8948 | Out << 'l'; |
| 8949 | if (!!ParamAttr.StrideOrArg) |
| 8950 | Out << ParamAttr.StrideOrArg; |
| 8951 | break; |
| 8952 | case Uniform: |
| 8953 | Out << 'u'; |
| 8954 | break; |
| 8955 | case Vector: |
| 8956 | Out << 'v'; |
| 8957 | break; |
| 8958 | } |
| 8959 | if (!!ParamAttr.Alignment) |
| 8960 | Out << 'a' << ParamAttr.Alignment; |
| 8961 | } |
| 8962 | Out << '_' << Fn->getName(); |
| 8963 | Fn->addFnAttr(Out.str()); |
| 8964 | } |
| 8965 | } |
| 8966 | } |
| 8967 | |
| 8968 | void CGOpenMPRuntime::emitDeclareSimdFunction(const FunctionDecl *FD, |
| 8969 | llvm::Function *Fn) { |
| 8970 | ASTContext &C = CGM.getContext(); |
Alexey Bataev | 20cf67c | 2018-03-02 18:07:00 +0000 | [diff] [blame] | 8971 | FD = FD->getMostRecentDecl(); |
Alexey Bataev | c7a82b4 | 2016-05-06 09:40:08 +0000 | [diff] [blame] | 8972 | // Map params to their positions in function decl. |
| 8973 | llvm::DenseMap<const Decl *, unsigned> ParamPositions; |
| 8974 | if (isa<CXXMethodDecl>(FD)) |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8975 | ParamPositions.try_emplace(FD, 0); |
Alexey Bataev | c7a82b4 | 2016-05-06 09:40:08 +0000 | [diff] [blame] | 8976 | unsigned ParamPos = ParamPositions.size(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8977 | for (const ParmVarDecl *P : FD->parameters()) { |
| 8978 | ParamPositions.try_emplace(P->getCanonicalDecl(), ParamPos); |
Alexey Bataev | c7a82b4 | 2016-05-06 09:40:08 +0000 | [diff] [blame] | 8979 | ++ParamPos; |
| 8980 | } |
Alexey Bataev | 20cf67c | 2018-03-02 18:07:00 +0000 | [diff] [blame] | 8981 | while (FD) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8982 | for (const auto *Attr : FD->specific_attrs<OMPDeclareSimdDeclAttr>()) { |
Alexey Bataev | 20cf67c | 2018-03-02 18:07:00 +0000 | [diff] [blame] | 8983 | llvm::SmallVector<ParamAttrTy, 8> ParamAttrs(ParamPositions.size()); |
| 8984 | // Mark uniform parameters. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8985 | for (const Expr *E : Attr->uniforms()) { |
Alexey Bataev | 20cf67c | 2018-03-02 18:07:00 +0000 | [diff] [blame] | 8986 | E = E->IgnoreParenImpCasts(); |
| 8987 | unsigned Pos; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8988 | if (isa<CXXThisExpr>(E)) { |
Alexey Bataev | 20cf67c | 2018-03-02 18:07:00 +0000 | [diff] [blame] | 8989 | Pos = ParamPositions[FD]; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8990 | } else { |
| 8991 | const auto *PVD = cast<ParmVarDecl>(cast<DeclRefExpr>(E)->getDecl()) |
| 8992 | ->getCanonicalDecl(); |
Alexey Bataev | 20cf67c | 2018-03-02 18:07:00 +0000 | [diff] [blame] | 8993 | Pos = ParamPositions[PVD]; |
| 8994 | } |
| 8995 | ParamAttrs[Pos].Kind = Uniform; |
Alexey Bataev | c7a82b4 | 2016-05-06 09:40:08 +0000 | [diff] [blame] | 8996 | } |
Alexey Bataev | 20cf67c | 2018-03-02 18:07:00 +0000 | [diff] [blame] | 8997 | // Get alignment info. |
| 8998 | auto NI = Attr->alignments_begin(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8999 | for (const Expr *E : Attr->aligneds()) { |
Alexey Bataev | 20cf67c | 2018-03-02 18:07:00 +0000 | [diff] [blame] | 9000 | E = E->IgnoreParenImpCasts(); |
| 9001 | unsigned Pos; |
| 9002 | QualType ParmTy; |
| 9003 | if (isa<CXXThisExpr>(E)) { |
| 9004 | Pos = ParamPositions[FD]; |
| 9005 | ParmTy = E->getType(); |
| 9006 | } else { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 9007 | const auto *PVD = cast<ParmVarDecl>(cast<DeclRefExpr>(E)->getDecl()) |
| 9008 | ->getCanonicalDecl(); |
Alexey Bataev | 20cf67c | 2018-03-02 18:07:00 +0000 | [diff] [blame] | 9009 | Pos = ParamPositions[PVD]; |
| 9010 | ParmTy = PVD->getType(); |
| 9011 | } |
| 9012 | ParamAttrs[Pos].Alignment = |
| 9013 | (*NI) |
| 9014 | ? (*NI)->EvaluateKnownConstInt(C) |
Alexey Bataev | c7a82b4 | 2016-05-06 09:40:08 +0000 | [diff] [blame] | 9015 | : llvm::APSInt::getUnsigned( |
| 9016 | C.toCharUnitsFromBits(C.getOpenMPDefaultSimdAlign(ParmTy)) |
| 9017 | .getQuantity()); |
Alexey Bataev | 20cf67c | 2018-03-02 18:07:00 +0000 | [diff] [blame] | 9018 | ++NI; |
Alexey Bataev | c7a82b4 | 2016-05-06 09:40:08 +0000 | [diff] [blame] | 9019 | } |
Alexey Bataev | 20cf67c | 2018-03-02 18:07:00 +0000 | [diff] [blame] | 9020 | // Mark linear parameters. |
| 9021 | auto SI = Attr->steps_begin(); |
| 9022 | auto MI = Attr->modifiers_begin(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 9023 | for (const Expr *E : Attr->linears()) { |
Alexey Bataev | 20cf67c | 2018-03-02 18:07:00 +0000 | [diff] [blame] | 9024 | E = E->IgnoreParenImpCasts(); |
| 9025 | unsigned Pos; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 9026 | if (isa<CXXThisExpr>(E)) { |
Alexey Bataev | 20cf67c | 2018-03-02 18:07:00 +0000 | [diff] [blame] | 9027 | Pos = ParamPositions[FD]; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 9028 | } else { |
| 9029 | const auto *PVD = cast<ParmVarDecl>(cast<DeclRefExpr>(E)->getDecl()) |
| 9030 | ->getCanonicalDecl(); |
Alexey Bataev | 20cf67c | 2018-03-02 18:07:00 +0000 | [diff] [blame] | 9031 | Pos = ParamPositions[PVD]; |
| 9032 | } |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 9033 | ParamAttrTy &ParamAttr = ParamAttrs[Pos]; |
Alexey Bataev | 20cf67c | 2018-03-02 18:07:00 +0000 | [diff] [blame] | 9034 | ParamAttr.Kind = Linear; |
| 9035 | if (*SI) { |
| 9036 | if (!(*SI)->EvaluateAsInt(ParamAttr.StrideOrArg, C, |
| 9037 | Expr::SE_AllowSideEffects)) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 9038 | if (const auto *DRE = |
| 9039 | cast<DeclRefExpr>((*SI)->IgnoreParenImpCasts())) { |
| 9040 | if (const auto *StridePVD = cast<ParmVarDecl>(DRE->getDecl())) { |
Alexey Bataev | 20cf67c | 2018-03-02 18:07:00 +0000 | [diff] [blame] | 9041 | ParamAttr.Kind = LinearWithVarStride; |
| 9042 | ParamAttr.StrideOrArg = llvm::APSInt::getUnsigned( |
| 9043 | ParamPositions[StridePVD->getCanonicalDecl()]); |
| 9044 | } |
Alexey Bataev | c7a82b4 | 2016-05-06 09:40:08 +0000 | [diff] [blame] | 9045 | } |
| 9046 | } |
| 9047 | } |
Alexey Bataev | 20cf67c | 2018-03-02 18:07:00 +0000 | [diff] [blame] | 9048 | ++SI; |
| 9049 | ++MI; |
Alexey Bataev | c7a82b4 | 2016-05-06 09:40:08 +0000 | [diff] [blame] | 9050 | } |
Alexey Bataev | 20cf67c | 2018-03-02 18:07:00 +0000 | [diff] [blame] | 9051 | llvm::APSInt VLENVal; |
| 9052 | if (const Expr *VLEN = Attr->getSimdlen()) |
| 9053 | VLENVal = VLEN->EvaluateKnownConstInt(C); |
| 9054 | OMPDeclareSimdDeclAttr::BranchStateTy State = Attr->getBranchState(); |
| 9055 | if (CGM.getTriple().getArch() == llvm::Triple::x86 || |
| 9056 | CGM.getTriple().getArch() == llvm::Triple::x86_64) |
| 9057 | emitX86DeclareSimdFunction(FD, Fn, VLENVal, ParamAttrs, State); |
Alexey Bataev | c7a82b4 | 2016-05-06 09:40:08 +0000 | [diff] [blame] | 9058 | } |
Alexey Bataev | 20cf67c | 2018-03-02 18:07:00 +0000 | [diff] [blame] | 9059 | FD = FD->getPreviousDecl(); |
Alexey Bataev | c7a82b4 | 2016-05-06 09:40:08 +0000 | [diff] [blame] | 9060 | } |
| 9061 | } |
Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 9062 | |
| 9063 | namespace { |
| 9064 | /// Cleanup action for doacross support. |
| 9065 | class DoacrossCleanupTy final : public EHScopeStack::Cleanup { |
| 9066 | public: |
| 9067 | static const int DoacrossFinArgs = 2; |
| 9068 | |
| 9069 | private: |
| 9070 | llvm::Value *RTLFn; |
| 9071 | llvm::Value *Args[DoacrossFinArgs]; |
| 9072 | |
| 9073 | public: |
| 9074 | DoacrossCleanupTy(llvm::Value *RTLFn, ArrayRef<llvm::Value *> CallArgs) |
| 9075 | : RTLFn(RTLFn) { |
| 9076 | assert(CallArgs.size() == DoacrossFinArgs); |
| 9077 | std::copy(CallArgs.begin(), CallArgs.end(), std::begin(Args)); |
| 9078 | } |
| 9079 | void Emit(CodeGenFunction &CGF, Flags /*flags*/) override { |
| 9080 | if (!CGF.HaveInsertPoint()) |
| 9081 | return; |
| 9082 | CGF.EmitRuntimeCall(RTLFn, Args); |
| 9083 | } |
| 9084 | }; |
| 9085 | } // namespace |
| 9086 | |
| 9087 | void CGOpenMPRuntime::emitDoacrossInit(CodeGenFunction &CGF, |
Alexey Bataev | f138fda | 2018-08-13 19:04:24 +0000 | [diff] [blame] | 9088 | const OMPLoopDirective &D, |
| 9089 | ArrayRef<Expr *> NumIterations) { |
Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 9090 | if (!CGF.HaveInsertPoint()) |
| 9091 | return; |
| 9092 | |
| 9093 | ASTContext &C = CGM.getContext(); |
| 9094 | QualType Int64Ty = C.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/true); |
| 9095 | RecordDecl *RD; |
| 9096 | if (KmpDimTy.isNull()) { |
| 9097 | // Build struct kmp_dim { // loop bounds info casted to kmp_int64 |
| 9098 | // kmp_int64 lo; // lower |
| 9099 | // kmp_int64 up; // upper |
| 9100 | // kmp_int64 st; // stride |
| 9101 | // }; |
| 9102 | RD = C.buildImplicitRecord("kmp_dim"); |
| 9103 | RD->startDefinition(); |
| 9104 | addFieldToRecordDecl(C, RD, Int64Ty); |
| 9105 | addFieldToRecordDecl(C, RD, Int64Ty); |
| 9106 | addFieldToRecordDecl(C, RD, Int64Ty); |
| 9107 | RD->completeDefinition(); |
| 9108 | KmpDimTy = C.getRecordType(RD); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 9109 | } else { |
Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 9110 | RD = cast<RecordDecl>(KmpDimTy->getAsTagDecl()); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 9111 | } |
Alexey Bataev | f138fda | 2018-08-13 19:04:24 +0000 | [diff] [blame] | 9112 | llvm::APInt Size(/*numBits=*/32, NumIterations.size()); |
| 9113 | QualType ArrayTy = |
| 9114 | C.getConstantArrayType(KmpDimTy, Size, ArrayType::Normal, 0); |
Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 9115 | |
Alexey Bataev | f138fda | 2018-08-13 19:04:24 +0000 | [diff] [blame] | 9116 | Address DimsAddr = CGF.CreateMemTemp(ArrayTy, "dims"); |
| 9117 | CGF.EmitNullInitialization(DimsAddr, ArrayTy); |
Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 9118 | enum { LowerFD = 0, UpperFD, StrideFD }; |
| 9119 | // Fill dims with data. |
Alexey Bataev | f138fda | 2018-08-13 19:04:24 +0000 | [diff] [blame] | 9120 | for (unsigned I = 0, E = NumIterations.size(); I < E; ++I) { |
| 9121 | LValue DimsLVal = |
| 9122 | CGF.MakeAddrLValue(CGF.Builder.CreateConstArrayGEP( |
| 9123 | DimsAddr, I, C.getTypeSizeInChars(KmpDimTy)), |
| 9124 | KmpDimTy); |
| 9125 | // dims.upper = num_iterations; |
| 9126 | LValue UpperLVal = CGF.EmitLValueForField( |
| 9127 | DimsLVal, *std::next(RD->field_begin(), UpperFD)); |
| 9128 | llvm::Value *NumIterVal = |
| 9129 | CGF.EmitScalarConversion(CGF.EmitScalarExpr(NumIterations[I]), |
| 9130 | D.getNumIterations()->getType(), Int64Ty, |
| 9131 | D.getNumIterations()->getExprLoc()); |
| 9132 | CGF.EmitStoreOfScalar(NumIterVal, UpperLVal); |
| 9133 | // dims.stride = 1; |
| 9134 | LValue StrideLVal = CGF.EmitLValueForField( |
| 9135 | DimsLVal, *std::next(RD->field_begin(), StrideFD)); |
| 9136 | CGF.EmitStoreOfScalar(llvm::ConstantInt::getSigned(CGM.Int64Ty, /*V=*/1), |
| 9137 | StrideLVal); |
| 9138 | } |
Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 9139 | |
| 9140 | // Build call void __kmpc_doacross_init(ident_t *loc, kmp_int32 gtid, |
| 9141 | // kmp_int32 num_dims, struct kmp_dim * dims); |
Alexey Bataev | f138fda | 2018-08-13 19:04:24 +0000 | [diff] [blame] | 9142 | llvm::Value *Args[] = { |
| 9143 | emitUpdateLocation(CGF, D.getBeginLoc()), |
| 9144 | getThreadID(CGF, D.getBeginLoc()), |
| 9145 | llvm::ConstantInt::getSigned(CGM.Int32Ty, NumIterations.size()), |
| 9146 | CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
| 9147 | CGF.Builder |
| 9148 | .CreateConstArrayGEP(DimsAddr, 0, C.getTypeSizeInChars(KmpDimTy)) |
| 9149 | .getPointer(), |
| 9150 | CGM.VoidPtrTy)}; |
Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 9151 | |
| 9152 | llvm::Value *RTLFn = createRuntimeFunction(OMPRTL__kmpc_doacross_init); |
| 9153 | CGF.EmitRuntimeCall(RTLFn, Args); |
| 9154 | llvm::Value *FiniArgs[DoacrossCleanupTy::DoacrossFinArgs] = { |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 9155 | emitUpdateLocation(CGF, D.getEndLoc()), getThreadID(CGF, D.getEndLoc())}; |
Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 9156 | llvm::Value *FiniRTLFn = createRuntimeFunction(OMPRTL__kmpc_doacross_fini); |
| 9157 | CGF.EHStack.pushCleanup<DoacrossCleanupTy>(NormalAndEHCleanup, FiniRTLFn, |
| 9158 | llvm::makeArrayRef(FiniArgs)); |
| 9159 | } |
| 9160 | |
| 9161 | void CGOpenMPRuntime::emitDoacrossOrdered(CodeGenFunction &CGF, |
| 9162 | const OMPDependClause *C) { |
| 9163 | QualType Int64Ty = |
| 9164 | CGM.getContext().getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1); |
Alexey Bataev | f138fda | 2018-08-13 19:04:24 +0000 | [diff] [blame] | 9165 | llvm::APInt Size(/*numBits=*/32, C->getNumLoops()); |
| 9166 | QualType ArrayTy = CGM.getContext().getConstantArrayType( |
| 9167 | Int64Ty, Size, ArrayType::Normal, 0); |
| 9168 | Address CntAddr = CGF.CreateMemTemp(ArrayTy, ".cnt.addr"); |
| 9169 | for (unsigned I = 0, E = C->getNumLoops(); I < E; ++I) { |
| 9170 | const Expr *CounterVal = C->getLoopData(I); |
| 9171 | assert(CounterVal); |
| 9172 | llvm::Value *CntVal = CGF.EmitScalarConversion( |
| 9173 | CGF.EmitScalarExpr(CounterVal), CounterVal->getType(), Int64Ty, |
| 9174 | CounterVal->getExprLoc()); |
| 9175 | CGF.EmitStoreOfScalar( |
| 9176 | CntVal, |
| 9177 | CGF.Builder.CreateConstArrayGEP( |
| 9178 | CntAddr, I, CGM.getContext().getTypeSizeInChars(Int64Ty)), |
| 9179 | /*Volatile=*/false, Int64Ty); |
| 9180 | } |
| 9181 | llvm::Value *Args[] = { |
| 9182 | emitUpdateLocation(CGF, C->getBeginLoc()), |
| 9183 | getThreadID(CGF, C->getBeginLoc()), |
| 9184 | CGF.Builder |
| 9185 | .CreateConstArrayGEP(CntAddr, 0, |
| 9186 | CGM.getContext().getTypeSizeInChars(Int64Ty)) |
| 9187 | .getPointer()}; |
Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 9188 | llvm::Value *RTLFn; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 9189 | if (C->getDependencyKind() == OMPC_DEPEND_source) { |
Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 9190 | RTLFn = createRuntimeFunction(OMPRTL__kmpc_doacross_post); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 9191 | } else { |
Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 9192 | assert(C->getDependencyKind() == OMPC_DEPEND_sink); |
| 9193 | RTLFn = createRuntimeFunction(OMPRTL__kmpc_doacross_wait); |
| 9194 | } |
| 9195 | CGF.EmitRuntimeCall(RTLFn, Args); |
| 9196 | } |
| 9197 | |
Alexey Bataev | 7ef47a6 | 2018-02-22 18:33:31 +0000 | [diff] [blame] | 9198 | void CGOpenMPRuntime::emitCall(CodeGenFunction &CGF, SourceLocation Loc, |
| 9199 | llvm::Value *Callee, |
| 9200 | ArrayRef<llvm::Value *> Args) const { |
| 9201 | assert(Loc.isValid() && "Outlined function call location must be valid."); |
Alexey Bataev | 3c595a6 | 2017-08-14 15:01:03 +0000 | [diff] [blame] | 9202 | auto DL = ApplyDebugLocation::CreateDefaultArtificial(CGF, Loc); |
| 9203 | |
| 9204 | if (auto *Fn = dyn_cast<llvm::Function>(Callee)) { |
Alexey Bataev | 2c7eee5 | 2017-08-04 19:10:54 +0000 | [diff] [blame] | 9205 | if (Fn->doesNotThrow()) { |
Alexey Bataev | 3c595a6 | 2017-08-14 15:01:03 +0000 | [diff] [blame] | 9206 | CGF.EmitNounwindRuntimeCall(Fn, Args); |
Alexey Bataev | 2c7eee5 | 2017-08-04 19:10:54 +0000 | [diff] [blame] | 9207 | return; |
| 9208 | } |
| 9209 | } |
Alexey Bataev | 3c595a6 | 2017-08-14 15:01:03 +0000 | [diff] [blame] | 9210 | CGF.EmitRuntimeCall(Callee, Args); |
| 9211 | } |
| 9212 | |
| 9213 | void CGOpenMPRuntime::emitOutlinedFunctionCall( |
| 9214 | CodeGenFunction &CGF, SourceLocation Loc, llvm::Value *OutlinedFn, |
| 9215 | ArrayRef<llvm::Value *> Args) const { |
Alexey Bataev | 7ef47a6 | 2018-02-22 18:33:31 +0000 | [diff] [blame] | 9216 | emitCall(CGF, Loc, OutlinedFn, Args); |
Alexey Bataev | 2c7eee5 | 2017-08-04 19:10:54 +0000 | [diff] [blame] | 9217 | } |
Alexey Bataev | 3b8d558 | 2017-08-08 18:04:06 +0000 | [diff] [blame] | 9218 | |
| 9219 | Address CGOpenMPRuntime::getParameterAddress(CodeGenFunction &CGF, |
| 9220 | const VarDecl *NativeParam, |
| 9221 | const VarDecl *TargetParam) const { |
| 9222 | return CGF.GetAddrOfLocalVar(NativeParam); |
| 9223 | } |
Alexey Bataev | a8a9153a | 2017-12-29 18:07:07 +0000 | [diff] [blame] | 9224 | |
Gheorghe-Teodor Bercea | d3dcf2f | 2018-03-14 14:17:45 +0000 | [diff] [blame] | 9225 | Address CGOpenMPRuntime::getAddressOfLocalVariable(CodeGenFunction &CGF, |
| 9226 | const VarDecl *VD) { |
| 9227 | return Address::invalid(); |
| 9228 | } |
| 9229 | |
Alexey Bataev | a8a9153a | 2017-12-29 18:07:07 +0000 | [diff] [blame] | 9230 | llvm::Value *CGOpenMPSIMDRuntime::emitParallelOutlinedFunction( |
| 9231 | const OMPExecutableDirective &D, const VarDecl *ThreadIDVar, |
| 9232 | OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen) { |
| 9233 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 9234 | } |
| 9235 | |
| 9236 | llvm::Value *CGOpenMPSIMDRuntime::emitTeamsOutlinedFunction( |
| 9237 | const OMPExecutableDirective &D, const VarDecl *ThreadIDVar, |
| 9238 | OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen) { |
| 9239 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 9240 | } |
| 9241 | |
| 9242 | llvm::Value *CGOpenMPSIMDRuntime::emitTaskOutlinedFunction( |
| 9243 | const OMPExecutableDirective &D, const VarDecl *ThreadIDVar, |
| 9244 | const VarDecl *PartIDVar, const VarDecl *TaskTVar, |
| 9245 | OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen, |
| 9246 | bool Tied, unsigned &NumberOfParts) { |
| 9247 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 9248 | } |
| 9249 | |
| 9250 | void CGOpenMPSIMDRuntime::emitParallelCall(CodeGenFunction &CGF, |
| 9251 | SourceLocation Loc, |
| 9252 | llvm::Value *OutlinedFn, |
| 9253 | ArrayRef<llvm::Value *> CapturedVars, |
| 9254 | const Expr *IfCond) { |
| 9255 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 9256 | } |
| 9257 | |
| 9258 | void CGOpenMPSIMDRuntime::emitCriticalRegion( |
| 9259 | CodeGenFunction &CGF, StringRef CriticalName, |
| 9260 | const RegionCodeGenTy &CriticalOpGen, SourceLocation Loc, |
| 9261 | const Expr *Hint) { |
| 9262 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 9263 | } |
| 9264 | |
| 9265 | void CGOpenMPSIMDRuntime::emitMasterRegion(CodeGenFunction &CGF, |
| 9266 | const RegionCodeGenTy &MasterOpGen, |
| 9267 | SourceLocation Loc) { |
| 9268 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 9269 | } |
| 9270 | |
| 9271 | void CGOpenMPSIMDRuntime::emitTaskyieldCall(CodeGenFunction &CGF, |
| 9272 | SourceLocation Loc) { |
| 9273 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 9274 | } |
| 9275 | |
| 9276 | void CGOpenMPSIMDRuntime::emitTaskgroupRegion( |
| 9277 | CodeGenFunction &CGF, const RegionCodeGenTy &TaskgroupOpGen, |
| 9278 | SourceLocation Loc) { |
| 9279 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 9280 | } |
| 9281 | |
| 9282 | void CGOpenMPSIMDRuntime::emitSingleRegion( |
| 9283 | CodeGenFunction &CGF, const RegionCodeGenTy &SingleOpGen, |
| 9284 | SourceLocation Loc, ArrayRef<const Expr *> CopyprivateVars, |
| 9285 | ArrayRef<const Expr *> DestExprs, ArrayRef<const Expr *> SrcExprs, |
| 9286 | ArrayRef<const Expr *> AssignmentOps) { |
| 9287 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 9288 | } |
| 9289 | |
| 9290 | void CGOpenMPSIMDRuntime::emitOrderedRegion(CodeGenFunction &CGF, |
| 9291 | const RegionCodeGenTy &OrderedOpGen, |
| 9292 | SourceLocation Loc, |
| 9293 | bool IsThreads) { |
| 9294 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 9295 | } |
| 9296 | |
| 9297 | void CGOpenMPSIMDRuntime::emitBarrierCall(CodeGenFunction &CGF, |
| 9298 | SourceLocation Loc, |
| 9299 | OpenMPDirectiveKind Kind, |
| 9300 | bool EmitChecks, |
| 9301 | bool ForceSimpleCall) { |
| 9302 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 9303 | } |
| 9304 | |
| 9305 | void CGOpenMPSIMDRuntime::emitForDispatchInit( |
| 9306 | CodeGenFunction &CGF, SourceLocation Loc, |
| 9307 | const OpenMPScheduleTy &ScheduleKind, unsigned IVSize, bool IVSigned, |
| 9308 | bool Ordered, const DispatchRTInput &DispatchValues) { |
| 9309 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 9310 | } |
| 9311 | |
| 9312 | void CGOpenMPSIMDRuntime::emitForStaticInit( |
| 9313 | CodeGenFunction &CGF, SourceLocation Loc, OpenMPDirectiveKind DKind, |
| 9314 | const OpenMPScheduleTy &ScheduleKind, const StaticRTInput &Values) { |
| 9315 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 9316 | } |
| 9317 | |
| 9318 | void CGOpenMPSIMDRuntime::emitDistributeStaticInit( |
| 9319 | CodeGenFunction &CGF, SourceLocation Loc, |
| 9320 | OpenMPDistScheduleClauseKind SchedKind, const StaticRTInput &Values) { |
| 9321 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 9322 | } |
| 9323 | |
| 9324 | void CGOpenMPSIMDRuntime::emitForOrderedIterationEnd(CodeGenFunction &CGF, |
| 9325 | SourceLocation Loc, |
| 9326 | unsigned IVSize, |
| 9327 | bool IVSigned) { |
| 9328 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 9329 | } |
| 9330 | |
| 9331 | void CGOpenMPSIMDRuntime::emitForStaticFinish(CodeGenFunction &CGF, |
| 9332 | SourceLocation Loc, |
| 9333 | OpenMPDirectiveKind DKind) { |
| 9334 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 9335 | } |
| 9336 | |
| 9337 | llvm::Value *CGOpenMPSIMDRuntime::emitForNext(CodeGenFunction &CGF, |
| 9338 | SourceLocation Loc, |
| 9339 | unsigned IVSize, bool IVSigned, |
| 9340 | Address IL, Address LB, |
| 9341 | Address UB, Address ST) { |
| 9342 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 9343 | } |
| 9344 | |
| 9345 | void CGOpenMPSIMDRuntime::emitNumThreadsClause(CodeGenFunction &CGF, |
| 9346 | llvm::Value *NumThreads, |
| 9347 | SourceLocation Loc) { |
| 9348 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 9349 | } |
| 9350 | |
| 9351 | void CGOpenMPSIMDRuntime::emitProcBindClause(CodeGenFunction &CGF, |
| 9352 | OpenMPProcBindClauseKind ProcBind, |
| 9353 | SourceLocation Loc) { |
| 9354 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 9355 | } |
| 9356 | |
| 9357 | Address CGOpenMPSIMDRuntime::getAddrOfThreadPrivate(CodeGenFunction &CGF, |
| 9358 | const VarDecl *VD, |
| 9359 | Address VDAddr, |
| 9360 | SourceLocation Loc) { |
| 9361 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 9362 | } |
| 9363 | |
| 9364 | llvm::Function *CGOpenMPSIMDRuntime::emitThreadPrivateVarDefinition( |
| 9365 | const VarDecl *VD, Address VDAddr, SourceLocation Loc, bool PerformInit, |
| 9366 | CodeGenFunction *CGF) { |
| 9367 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 9368 | } |
| 9369 | |
| 9370 | Address CGOpenMPSIMDRuntime::getAddrOfArtificialThreadPrivate( |
| 9371 | CodeGenFunction &CGF, QualType VarType, StringRef Name) { |
| 9372 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 9373 | } |
| 9374 | |
| 9375 | void CGOpenMPSIMDRuntime::emitFlush(CodeGenFunction &CGF, |
| 9376 | ArrayRef<const Expr *> Vars, |
| 9377 | SourceLocation Loc) { |
| 9378 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 9379 | } |
| 9380 | |
| 9381 | void CGOpenMPSIMDRuntime::emitTaskCall(CodeGenFunction &CGF, SourceLocation Loc, |
| 9382 | const OMPExecutableDirective &D, |
| 9383 | llvm::Value *TaskFunction, |
| 9384 | QualType SharedsTy, Address Shareds, |
| 9385 | const Expr *IfCond, |
| 9386 | const OMPTaskDataTy &Data) { |
| 9387 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 9388 | } |
| 9389 | |
| 9390 | void CGOpenMPSIMDRuntime::emitTaskLoopCall( |
| 9391 | CodeGenFunction &CGF, SourceLocation Loc, const OMPLoopDirective &D, |
| 9392 | llvm::Value *TaskFunction, QualType SharedsTy, Address Shareds, |
| 9393 | const Expr *IfCond, const OMPTaskDataTy &Data) { |
| 9394 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 9395 | } |
| 9396 | |
| 9397 | void CGOpenMPSIMDRuntime::emitReduction( |
| 9398 | CodeGenFunction &CGF, SourceLocation Loc, ArrayRef<const Expr *> Privates, |
| 9399 | ArrayRef<const Expr *> LHSExprs, ArrayRef<const Expr *> RHSExprs, |
| 9400 | ArrayRef<const Expr *> ReductionOps, ReductionOptionsTy Options) { |
| 9401 | assert(Options.SimpleReduction && "Only simple reduction is expected."); |
| 9402 | CGOpenMPRuntime::emitReduction(CGF, Loc, Privates, LHSExprs, RHSExprs, |
| 9403 | ReductionOps, Options); |
| 9404 | } |
| 9405 | |
| 9406 | llvm::Value *CGOpenMPSIMDRuntime::emitTaskReductionInit( |
| 9407 | CodeGenFunction &CGF, SourceLocation Loc, ArrayRef<const Expr *> LHSExprs, |
| 9408 | ArrayRef<const Expr *> RHSExprs, const OMPTaskDataTy &Data) { |
| 9409 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 9410 | } |
| 9411 | |
| 9412 | void CGOpenMPSIMDRuntime::emitTaskReductionFixups(CodeGenFunction &CGF, |
| 9413 | SourceLocation Loc, |
| 9414 | ReductionCodeGen &RCG, |
| 9415 | unsigned N) { |
| 9416 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 9417 | } |
| 9418 | |
| 9419 | Address CGOpenMPSIMDRuntime::getTaskReductionItem(CodeGenFunction &CGF, |
| 9420 | SourceLocation Loc, |
| 9421 | llvm::Value *ReductionsPtr, |
| 9422 | LValue SharedLVal) { |
| 9423 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 9424 | } |
| 9425 | |
| 9426 | void CGOpenMPSIMDRuntime::emitTaskwaitCall(CodeGenFunction &CGF, |
| 9427 | SourceLocation Loc) { |
| 9428 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 9429 | } |
| 9430 | |
| 9431 | void CGOpenMPSIMDRuntime::emitCancellationPointCall( |
| 9432 | CodeGenFunction &CGF, SourceLocation Loc, |
| 9433 | OpenMPDirectiveKind CancelRegion) { |
| 9434 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 9435 | } |
| 9436 | |
| 9437 | void CGOpenMPSIMDRuntime::emitCancelCall(CodeGenFunction &CGF, |
| 9438 | SourceLocation Loc, const Expr *IfCond, |
| 9439 | OpenMPDirectiveKind CancelRegion) { |
| 9440 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 9441 | } |
| 9442 | |
| 9443 | void CGOpenMPSIMDRuntime::emitTargetOutlinedFunction( |
| 9444 | const OMPExecutableDirective &D, StringRef ParentName, |
| 9445 | llvm::Function *&OutlinedFn, llvm::Constant *&OutlinedFnID, |
| 9446 | bool IsOffloadEntry, const RegionCodeGenTy &CodeGen) { |
| 9447 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 9448 | } |
| 9449 | |
| 9450 | void CGOpenMPSIMDRuntime::emitTargetCall(CodeGenFunction &CGF, |
| 9451 | const OMPExecutableDirective &D, |
| 9452 | llvm::Value *OutlinedFn, |
| 9453 | llvm::Value *OutlinedFnID, |
Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 9454 | const Expr *IfCond, const Expr *Device) { |
Alexey Bataev | a8a9153a | 2017-12-29 18:07:07 +0000 | [diff] [blame] | 9455 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 9456 | } |
| 9457 | |
| 9458 | bool CGOpenMPSIMDRuntime::emitTargetFunctions(GlobalDecl GD) { |
| 9459 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 9460 | } |
| 9461 | |
| 9462 | bool CGOpenMPSIMDRuntime::emitTargetGlobalVariable(GlobalDecl GD) { |
| 9463 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 9464 | } |
| 9465 | |
| 9466 | bool CGOpenMPSIMDRuntime::emitTargetGlobal(GlobalDecl GD) { |
| 9467 | return false; |
| 9468 | } |
| 9469 | |
| 9470 | llvm::Function *CGOpenMPSIMDRuntime::emitRegistrationFunction() { |
| 9471 | return nullptr; |
| 9472 | } |
| 9473 | |
| 9474 | void CGOpenMPSIMDRuntime::emitTeamsCall(CodeGenFunction &CGF, |
| 9475 | const OMPExecutableDirective &D, |
| 9476 | SourceLocation Loc, |
| 9477 | llvm::Value *OutlinedFn, |
| 9478 | ArrayRef<llvm::Value *> CapturedVars) { |
| 9479 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 9480 | } |
| 9481 | |
| 9482 | void CGOpenMPSIMDRuntime::emitNumTeamsClause(CodeGenFunction &CGF, |
| 9483 | const Expr *NumTeams, |
| 9484 | const Expr *ThreadLimit, |
| 9485 | SourceLocation Loc) { |
| 9486 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 9487 | } |
| 9488 | |
| 9489 | void CGOpenMPSIMDRuntime::emitTargetDataCalls( |
| 9490 | CodeGenFunction &CGF, const OMPExecutableDirective &D, const Expr *IfCond, |
| 9491 | const Expr *Device, const RegionCodeGenTy &CodeGen, TargetDataInfo &Info) { |
| 9492 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 9493 | } |
| 9494 | |
| 9495 | void CGOpenMPSIMDRuntime::emitTargetDataStandAloneCall( |
| 9496 | CodeGenFunction &CGF, const OMPExecutableDirective &D, const Expr *IfCond, |
| 9497 | const Expr *Device) { |
| 9498 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 9499 | } |
| 9500 | |
| 9501 | void CGOpenMPSIMDRuntime::emitDoacrossInit(CodeGenFunction &CGF, |
Alexey Bataev | f138fda | 2018-08-13 19:04:24 +0000 | [diff] [blame] | 9502 | const OMPLoopDirective &D, |
| 9503 | ArrayRef<Expr *> NumIterations) { |
Alexey Bataev | a8a9153a | 2017-12-29 18:07:07 +0000 | [diff] [blame] | 9504 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 9505 | } |
| 9506 | |
| 9507 | void CGOpenMPSIMDRuntime::emitDoacrossOrdered(CodeGenFunction &CGF, |
| 9508 | const OMPDependClause *C) { |
| 9509 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 9510 | } |
| 9511 | |
| 9512 | const VarDecl * |
| 9513 | CGOpenMPSIMDRuntime::translateParameter(const FieldDecl *FD, |
| 9514 | const VarDecl *NativeParam) const { |
| 9515 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 9516 | } |
| 9517 | |
| 9518 | Address |
| 9519 | CGOpenMPSIMDRuntime::getParameterAddress(CodeGenFunction &CGF, |
| 9520 | const VarDecl *NativeParam, |
| 9521 | const VarDecl *TargetParam) const { |
| 9522 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 9523 | } |
| 9524 | |