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 | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 1488 | llvm::Value *CGOpenMPRuntime::emitUpdateLocation(CodeGenFunction &CGF, |
| 1489 | SourceLocation Loc, |
Alexey Bataev | 50b3c95 | 2016-02-19 10:38:26 +0000 | [diff] [blame] | 1490 | unsigned Flags) { |
| 1491 | Flags |= OMP_IDENT_KMPC; |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1492 | // If no debug info is generated - return global default location. |
Benjamin Kramer | 8c30592 | 2016-02-02 11:06:51 +0000 | [diff] [blame] | 1493 | if (CGM.getCodeGenOpts().getDebugInfo() == codegenoptions::NoDebugInfo || |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1494 | Loc.isInvalid()) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1495 | return getOrCreateDefaultLocation(Flags).getPointer(); |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1496 | |
| 1497 | assert(CGF.CurFn && "No function in current CodeGenFunction."); |
| 1498 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1499 | CharUnits Align = CGM.getContext().getTypeAlignInChars(IdentQTy); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1500 | Address LocValue = Address::invalid(); |
Alexey Bataev | 1e4b713 | 2014-12-03 12:11:24 +0000 | [diff] [blame] | 1501 | auto I = OpenMPLocThreadIDMap.find(CGF.CurFn); |
| 1502 | if (I != OpenMPLocThreadIDMap.end()) |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1503 | LocValue = Address(I->second.DebugLoc, Align); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1504 | |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 1505 | // OpenMPLocThreadIDMap may have null DebugLoc and non-null ThreadID, if |
| 1506 | // GetOpenMPThreadID was called before this routine. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1507 | if (!LocValue.isValid()) { |
Alexey Bataev | 15007ba | 2014-05-07 06:18:01 +0000 | [diff] [blame] | 1508 | // Generate "ident_t .kmpc_loc.addr;" |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1509 | Address AI = CGF.CreateMemTemp(IdentQTy, ".kmpc_loc.addr"); |
Alexey Bataev | 1809571 | 2014-10-10 12:19:54 +0000 | [diff] [blame] | 1510 | auto &Elem = OpenMPLocThreadIDMap.FindAndConstruct(CGF.CurFn); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1511 | Elem.second.DebugLoc = AI.getPointer(); |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1512 | LocValue = AI; |
| 1513 | |
| 1514 | CGBuilderTy::InsertPointGuard IPG(CGF.Builder); |
| 1515 | CGF.Builder.SetInsertPoint(CGF.AllocaInsertPt); |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 1516 | CGF.Builder.CreateMemCpy(LocValue, getOrCreateDefaultLocation(Flags), |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1517 | CGF.getTypeSize(IdentQTy)); |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1518 | } |
| 1519 | |
| 1520 | // char **psource = &.kmpc_loc_<flags>.addr.psource; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1521 | LValue Base = CGF.MakeAddrLValue(LocValue, IdentQTy); |
| 1522 | auto Fields = cast<RecordDecl>(IdentQTy->getAsTagDecl())->field_begin(); |
| 1523 | LValue PSource = |
| 1524 | CGF.EmitLValueForField(Base, *std::next(Fields, IdentField_PSource)); |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1525 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1526 | llvm::Value *OMPDebugLoc = OpenMPDebugLocMap.lookup(Loc.getRawEncoding()); |
Alexey Bataev | f002aca | 2014-05-30 05:48:40 +0000 | [diff] [blame] | 1527 | if (OMPDebugLoc == nullptr) { |
| 1528 | SmallString<128> Buffer2; |
| 1529 | llvm::raw_svector_ostream OS2(Buffer2); |
| 1530 | // Build debug location |
| 1531 | PresumedLoc PLoc = CGF.getContext().getSourceManager().getPresumedLoc(Loc); |
| 1532 | OS2 << ";" << PLoc.getFilename() << ";"; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1533 | if (const auto *FD = dyn_cast_or_null<FunctionDecl>(CGF.CurFuncDecl)) |
Alexey Bataev | f002aca | 2014-05-30 05:48:40 +0000 | [diff] [blame] | 1534 | OS2 << FD->getQualifiedNameAsString(); |
Alexey Bataev | f002aca | 2014-05-30 05:48:40 +0000 | [diff] [blame] | 1535 | OS2 << ";" << PLoc.getLine() << ";" << PLoc.getColumn() << ";;"; |
| 1536 | OMPDebugLoc = CGF.Builder.CreateGlobalStringPtr(OS2.str()); |
| 1537 | OpenMPDebugLocMap[Loc.getRawEncoding()] = OMPDebugLoc; |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1538 | } |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1539 | // *psource = ";<File>;<Function>;<Line>;<Column>;;"; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1540 | CGF.EmitStoreOfScalar(OMPDebugLoc, PSource); |
Alexey Bataev | f002aca | 2014-05-30 05:48:40 +0000 | [diff] [blame] | 1541 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1542 | // Our callers always pass this to a runtime function, so for |
| 1543 | // convenience, go ahead and return a naked pointer. |
| 1544 | return LocValue.getPointer(); |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1545 | } |
| 1546 | |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 1547 | llvm::Value *CGOpenMPRuntime::getThreadID(CodeGenFunction &CGF, |
| 1548 | SourceLocation Loc) { |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1549 | assert(CGF.CurFn && "No function in current CodeGenFunction."); |
| 1550 | |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 1551 | llvm::Value *ThreadID = nullptr; |
Alexey Bataev | 1809571 | 2014-10-10 12:19:54 +0000 | [diff] [blame] | 1552 | // Check whether we've already cached a load of the thread id in this |
| 1553 | // function. |
Alexey Bataev | 1e4b713 | 2014-12-03 12:11:24 +0000 | [diff] [blame] | 1554 | auto I = OpenMPLocThreadIDMap.find(CGF.CurFn); |
Alexey Bataev | 1809571 | 2014-10-10 12:19:54 +0000 | [diff] [blame] | 1555 | if (I != OpenMPLocThreadIDMap.end()) { |
| 1556 | ThreadID = I->second.ThreadID; |
Alexey Bataev | 03b340a | 2014-10-21 03:16:40 +0000 | [diff] [blame] | 1557 | if (ThreadID != nullptr) |
| 1558 | return ThreadID; |
| 1559 | } |
Alexey Bataev | aee1855 | 2017-08-16 14:01:00 +0000 | [diff] [blame] | 1560 | // If exceptions are enabled, do not use parameter to avoid possible crash. |
Alexey Bataev | 5d2c9a4 | 2017-11-02 18:55:05 +0000 | [diff] [blame] | 1561 | if (!CGF.EHStack.requiresLandingPad() || !CGF.getLangOpts().Exceptions || |
| 1562 | !CGF.getLangOpts().CXXExceptions || |
Alexey Bataev | 0e1b458 | 2017-11-02 14:25:34 +0000 | [diff] [blame] | 1563 | CGF.Builder.GetInsertBlock() == CGF.AllocaInsertPt->getParent()) { |
Alexey Bataev | aee1855 | 2017-08-16 14:01:00 +0000 | [diff] [blame] | 1564 | if (auto *OMPRegionInfo = |
| 1565 | dyn_cast_or_null<CGOpenMPRegionInfo>(CGF.CapturedStmtInfo)) { |
| 1566 | if (OMPRegionInfo->getThreadIDVariable()) { |
| 1567 | // Check if this an outlined function with thread id passed as argument. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1568 | LValue LVal = OMPRegionInfo->getThreadIDVariableLValue(CGF); |
Alexey Bataev | 1e49137 | 2018-01-23 18:44:14 +0000 | [diff] [blame] | 1569 | ThreadID = CGF.EmitLoadOfScalar(LVal, Loc); |
Alexey Bataev | aee1855 | 2017-08-16 14:01:00 +0000 | [diff] [blame] | 1570 | // If value loaded in entry block, cache it and use it everywhere in |
| 1571 | // function. |
| 1572 | if (CGF.Builder.GetInsertBlock() == CGF.AllocaInsertPt->getParent()) { |
| 1573 | auto &Elem = OpenMPLocThreadIDMap.FindAndConstruct(CGF.CurFn); |
| 1574 | Elem.second.ThreadID = ThreadID; |
| 1575 | } |
| 1576 | return ThreadID; |
Alexey Bataev | 8cbe0a6 | 2015-02-26 10:27:34 +0000 | [diff] [blame] | 1577 | } |
Alexey Bataev | d6c5755 | 2014-07-25 07:55:17 +0000 | [diff] [blame] | 1578 | } |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1579 | } |
Alexey Bataev | 8cbe0a6 | 2015-02-26 10:27:34 +0000 | [diff] [blame] | 1580 | |
| 1581 | // This is not an outlined function region - need to call __kmpc_int32 |
| 1582 | // kmpc_global_thread_num(ident_t *loc). |
| 1583 | // Generate thread id value and cache this value for use across the |
| 1584 | // function. |
| 1585 | CGBuilderTy::InsertPointGuard IPG(CGF.Builder); |
| 1586 | CGF.Builder.SetInsertPoint(CGF.AllocaInsertPt); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1587 | llvm::CallInst *Call = CGF.Builder.CreateCall( |
Alexey Bataev | 0e1b458 | 2017-11-02 14:25:34 +0000 | [diff] [blame] | 1588 | createRuntimeFunction(OMPRTL__kmpc_global_thread_num), |
| 1589 | emitUpdateLocation(CGF, Loc)); |
| 1590 | Call->setCallingConv(CGF.getRuntimeCC()); |
Alexey Bataev | 8cbe0a6 | 2015-02-26 10:27:34 +0000 | [diff] [blame] | 1591 | auto &Elem = OpenMPLocThreadIDMap.FindAndConstruct(CGF.CurFn); |
Alexey Bataev | 0e1b458 | 2017-11-02 14:25:34 +0000 | [diff] [blame] | 1592 | Elem.second.ThreadID = Call; |
| 1593 | return Call; |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1594 | } |
| 1595 | |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 1596 | void CGOpenMPRuntime::functionFinished(CodeGenFunction &CGF) { |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1597 | assert(CGF.CurFn && "No function in current CodeGenFunction."); |
Alexey Bataev | 03b340a | 2014-10-21 03:16:40 +0000 | [diff] [blame] | 1598 | if (OpenMPLocThreadIDMap.count(CGF.CurFn)) |
| 1599 | OpenMPLocThreadIDMap.erase(CGF.CurFn); |
Alexey Bataev | c5b1d32 | 2016-03-04 09:22:22 +0000 | [diff] [blame] | 1600 | if (FunctionUDRMap.count(CGF.CurFn) > 0) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1601 | for(auto *D : FunctionUDRMap[CGF.CurFn]) |
Alexey Bataev | c5b1d32 | 2016-03-04 09:22:22 +0000 | [diff] [blame] | 1602 | UDRMap.erase(D); |
Alexey Bataev | c5b1d32 | 2016-03-04 09:22:22 +0000 | [diff] [blame] | 1603 | FunctionUDRMap.erase(CGF.CurFn); |
| 1604 | } |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1605 | } |
| 1606 | |
| 1607 | llvm::Type *CGOpenMPRuntime::getIdentTyPointerTy() { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1608 | return IdentTy->getPointerTo(); |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1609 | } |
| 1610 | |
| 1611 | llvm::Type *CGOpenMPRuntime::getKmpc_MicroPointerTy() { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 1612 | if (!Kmpc_MicroTy) { |
| 1613 | // Build void (*kmpc_micro)(kmp_int32 *global_tid, kmp_int32 *bound_tid,...) |
| 1614 | llvm::Type *MicroParams[] = {llvm::PointerType::getUnqual(CGM.Int32Ty), |
| 1615 | llvm::PointerType::getUnqual(CGM.Int32Ty)}; |
| 1616 | Kmpc_MicroTy = llvm::FunctionType::get(CGM.VoidTy, MicroParams, true); |
| 1617 | } |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1618 | return llvm::PointerType::getUnqual(Kmpc_MicroTy); |
| 1619 | } |
| 1620 | |
| 1621 | llvm::Constant * |
Alexey Bataev | 50b3c95 | 2016-02-19 10:38:26 +0000 | [diff] [blame] | 1622 | CGOpenMPRuntime::createRuntimeFunction(unsigned Function) { |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1623 | llvm::Constant *RTLFn = nullptr; |
Alexey Bataev | 50b3c95 | 2016-02-19 10:38:26 +0000 | [diff] [blame] | 1624 | switch (static_cast<OpenMPRTLFunction>(Function)) { |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1625 | case OMPRTL__kmpc_fork_call: { |
| 1626 | // Build void __kmpc_fork_call(ident_t *loc, kmp_int32 argc, kmpc_micro |
| 1627 | // microtask, ...); |
Alexey Bataev | 23b6942 | 2014-06-18 07:08:49 +0000 | [diff] [blame] | 1628 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty, |
| 1629 | getKmpc_MicroPointerTy()}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1630 | auto *FnTy = |
Alexey Bataev | d74d060 | 2014-10-13 06:02:40 +0000 | [diff] [blame] | 1631 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ true); |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1632 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_fork_call"); |
| 1633 | break; |
| 1634 | } |
| 1635 | case OMPRTL__kmpc_global_thread_num: { |
| 1636 | // Build kmp_int32 __kmpc_global_thread_num(ident_t *loc); |
Alexey Bataev | 23b6942 | 2014-06-18 07:08:49 +0000 | [diff] [blame] | 1637 | llvm::Type *TypeParams[] = {getIdentTyPointerTy()}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1638 | auto *FnTy = |
Alexey Bataev | d74d060 | 2014-10-13 06:02:40 +0000 | [diff] [blame] | 1639 | llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg*/ false); |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1640 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_global_thread_num"); |
| 1641 | break; |
| 1642 | } |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 1643 | case OMPRTL__kmpc_threadprivate_cached: { |
| 1644 | // Build void *__kmpc_threadprivate_cached(ident_t *loc, |
| 1645 | // kmp_int32 global_tid, void *data, size_t size, void ***cache); |
| 1646 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty, |
| 1647 | CGM.VoidPtrTy, CGM.SizeTy, |
| 1648 | CGM.VoidPtrTy->getPointerTo()->getPointerTo()}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1649 | auto *FnTy = |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 1650 | llvm::FunctionType::get(CGM.VoidPtrTy, TypeParams, /*isVarArg*/ false); |
| 1651 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_threadprivate_cached"); |
| 1652 | break; |
| 1653 | } |
Alexey Bataev | 3a3bf0b | 2014-09-22 10:01:53 +0000 | [diff] [blame] | 1654 | case OMPRTL__kmpc_critical: { |
Alexey Bataev | f947218 | 2014-09-22 12:32:31 +0000 | [diff] [blame] | 1655 | // Build void __kmpc_critical(ident_t *loc, kmp_int32 global_tid, |
| 1656 | // kmp_critical_name *crit); |
Alexey Bataev | 3a3bf0b | 2014-09-22 10:01:53 +0000 | [diff] [blame] | 1657 | llvm::Type *TypeParams[] = { |
| 1658 | getIdentTyPointerTy(), CGM.Int32Ty, |
| 1659 | llvm::PointerType::getUnqual(KmpCriticalNameTy)}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1660 | auto *FnTy = |
Alexey Bataev | 3a3bf0b | 2014-09-22 10:01:53 +0000 | [diff] [blame] | 1661 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false); |
| 1662 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_critical"); |
| 1663 | break; |
| 1664 | } |
Alexey Bataev | fc57d16 | 2015-12-15 10:55:09 +0000 | [diff] [blame] | 1665 | case OMPRTL__kmpc_critical_with_hint: { |
| 1666 | // Build void __kmpc_critical_with_hint(ident_t *loc, kmp_int32 global_tid, |
| 1667 | // kmp_critical_name *crit, uintptr_t hint); |
| 1668 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty, |
| 1669 | llvm::PointerType::getUnqual(KmpCriticalNameTy), |
| 1670 | CGM.IntPtrTy}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1671 | auto *FnTy = |
Alexey Bataev | fc57d16 | 2015-12-15 10:55:09 +0000 | [diff] [blame] | 1672 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false); |
| 1673 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_critical_with_hint"); |
| 1674 | break; |
| 1675 | } |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 1676 | case OMPRTL__kmpc_threadprivate_register: { |
| 1677 | // Build void __kmpc_threadprivate_register(ident_t *, void *data, |
| 1678 | // kmpc_ctor ctor, kmpc_cctor cctor, kmpc_dtor dtor); |
| 1679 | // typedef void *(*kmpc_ctor)(void *); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1680 | auto *KmpcCtorTy = |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 1681 | llvm::FunctionType::get(CGM.VoidPtrTy, CGM.VoidPtrTy, |
| 1682 | /*isVarArg*/ false)->getPointerTo(); |
| 1683 | // typedef void *(*kmpc_cctor)(void *, void *); |
| 1684 | llvm::Type *KmpcCopyCtorTyArgs[] = {CGM.VoidPtrTy, CGM.VoidPtrTy}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1685 | auto *KmpcCopyCtorTy = |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 1686 | llvm::FunctionType::get(CGM.VoidPtrTy, KmpcCopyCtorTyArgs, |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1687 | /*isVarArg*/ false) |
| 1688 | ->getPointerTo(); |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 1689 | // typedef void (*kmpc_dtor)(void *); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1690 | auto *KmpcDtorTy = |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 1691 | llvm::FunctionType::get(CGM.VoidTy, CGM.VoidPtrTy, /*isVarArg*/ false) |
| 1692 | ->getPointerTo(); |
| 1693 | llvm::Type *FnTyArgs[] = {getIdentTyPointerTy(), CGM.VoidPtrTy, KmpcCtorTy, |
| 1694 | KmpcCopyCtorTy, KmpcDtorTy}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1695 | auto *FnTy = llvm::FunctionType::get(CGM.VoidTy, FnTyArgs, |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 1696 | /*isVarArg*/ false); |
| 1697 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_threadprivate_register"); |
| 1698 | break; |
| 1699 | } |
Alexey Bataev | 3a3bf0b | 2014-09-22 10:01:53 +0000 | [diff] [blame] | 1700 | case OMPRTL__kmpc_end_critical: { |
Alexey Bataev | f947218 | 2014-09-22 12:32:31 +0000 | [diff] [blame] | 1701 | // Build void __kmpc_end_critical(ident_t *loc, kmp_int32 global_tid, |
| 1702 | // kmp_critical_name *crit); |
Alexey Bataev | 3a3bf0b | 2014-09-22 10:01:53 +0000 | [diff] [blame] | 1703 | llvm::Type *TypeParams[] = { |
| 1704 | getIdentTyPointerTy(), CGM.Int32Ty, |
| 1705 | llvm::PointerType::getUnqual(KmpCriticalNameTy)}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1706 | auto *FnTy = |
Alexey Bataev | 3a3bf0b | 2014-09-22 10:01:53 +0000 | [diff] [blame] | 1707 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false); |
| 1708 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_end_critical"); |
| 1709 | break; |
| 1710 | } |
Alexey Bataev | 8f7c1b0 | 2014-12-05 04:09:23 +0000 | [diff] [blame] | 1711 | case OMPRTL__kmpc_cancel_barrier: { |
| 1712 | // Build kmp_int32 __kmpc_cancel_barrier(ident_t *loc, kmp_int32 |
| 1713 | // global_tid); |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 1714 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1715 | auto *FnTy = |
Alexey Bataev | 8f7c1b0 | 2014-12-05 04:09:23 +0000 | [diff] [blame] | 1716 | llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg*/ false); |
| 1717 | RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name*/ "__kmpc_cancel_barrier"); |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 1718 | break; |
| 1719 | } |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 1720 | case OMPRTL__kmpc_barrier: { |
Alexey Bataev | 7d5d33e | 2015-07-06 05:50:32 +0000 | [diff] [blame] | 1721 | // Build void __kmpc_barrier(ident_t *loc, kmp_int32 global_tid); |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 1722 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1723 | auto *FnTy = |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 1724 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false); |
| 1725 | RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name*/ "__kmpc_barrier"); |
| 1726 | break; |
| 1727 | } |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 1728 | case OMPRTL__kmpc_for_static_fini: { |
| 1729 | // Build void __kmpc_for_static_fini(ident_t *loc, kmp_int32 global_tid); |
| 1730 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1731 | auto *FnTy = |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 1732 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false); |
| 1733 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_for_static_fini"); |
| 1734 | break; |
| 1735 | } |
Alexey Bataev | b205978 | 2014-10-13 08:23:51 +0000 | [diff] [blame] | 1736 | case OMPRTL__kmpc_push_num_threads: { |
| 1737 | // Build void __kmpc_push_num_threads(ident_t *loc, kmp_int32 global_tid, |
| 1738 | // kmp_int32 num_threads) |
| 1739 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty, |
| 1740 | CGM.Int32Ty}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1741 | auto *FnTy = |
Alexey Bataev | b205978 | 2014-10-13 08:23:51 +0000 | [diff] [blame] | 1742 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false); |
| 1743 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_push_num_threads"); |
| 1744 | break; |
| 1745 | } |
Alexey Bataev | d74d060 | 2014-10-13 06:02:40 +0000 | [diff] [blame] | 1746 | case OMPRTL__kmpc_serialized_parallel: { |
| 1747 | // Build void __kmpc_serialized_parallel(ident_t *loc, kmp_int32 |
| 1748 | // global_tid); |
| 1749 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1750 | auto *FnTy = |
Alexey Bataev | d74d060 | 2014-10-13 06:02:40 +0000 | [diff] [blame] | 1751 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false); |
| 1752 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_serialized_parallel"); |
| 1753 | break; |
| 1754 | } |
| 1755 | case OMPRTL__kmpc_end_serialized_parallel: { |
| 1756 | // Build void __kmpc_end_serialized_parallel(ident_t *loc, kmp_int32 |
| 1757 | // global_tid); |
| 1758 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1759 | auto *FnTy = |
Alexey Bataev | d74d060 | 2014-10-13 06:02:40 +0000 | [diff] [blame] | 1760 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false); |
| 1761 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_end_serialized_parallel"); |
| 1762 | break; |
| 1763 | } |
Alexey Bataev | cc37cc1 | 2014-11-20 04:34:54 +0000 | [diff] [blame] | 1764 | case OMPRTL__kmpc_flush: { |
Alexey Bataev | d76df6d | 2015-02-24 12:55:09 +0000 | [diff] [blame] | 1765 | // Build void __kmpc_flush(ident_t *loc); |
Alexey Bataev | cc37cc1 | 2014-11-20 04:34:54 +0000 | [diff] [blame] | 1766 | llvm::Type *TypeParams[] = {getIdentTyPointerTy()}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1767 | auto *FnTy = |
Alexey Bataev | d76df6d | 2015-02-24 12:55:09 +0000 | [diff] [blame] | 1768 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false); |
Alexey Bataev | cc37cc1 | 2014-11-20 04:34:54 +0000 | [diff] [blame] | 1769 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_flush"); |
| 1770 | break; |
| 1771 | } |
Alexey Bataev | 8d69065 | 2014-12-04 07:23:53 +0000 | [diff] [blame] | 1772 | case OMPRTL__kmpc_master: { |
| 1773 | // Build kmp_int32 __kmpc_master(ident_t *loc, kmp_int32 global_tid); |
| 1774 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1775 | auto *FnTy = |
Alexey Bataev | 8d69065 | 2014-12-04 07:23:53 +0000 | [diff] [blame] | 1776 | llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg=*/false); |
| 1777 | RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_master"); |
| 1778 | break; |
| 1779 | } |
| 1780 | case OMPRTL__kmpc_end_master: { |
| 1781 | // Build void __kmpc_end_master(ident_t *loc, kmp_int32 global_tid); |
| 1782 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1783 | auto *FnTy = |
Alexey Bataev | 8d69065 | 2014-12-04 07:23:53 +0000 | [diff] [blame] | 1784 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false); |
| 1785 | RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_end_master"); |
| 1786 | break; |
| 1787 | } |
Alexey Bataev | 9f797f3 | 2015-02-05 05:57:51 +0000 | [diff] [blame] | 1788 | case OMPRTL__kmpc_omp_taskyield: { |
| 1789 | // Build kmp_int32 __kmpc_omp_taskyield(ident_t *, kmp_int32 global_tid, |
| 1790 | // int end_part); |
| 1791 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty, CGM.IntTy}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1792 | auto *FnTy = |
Alexey Bataev | 9f797f3 | 2015-02-05 05:57:51 +0000 | [diff] [blame] | 1793 | llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg=*/false); |
| 1794 | RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_omp_taskyield"); |
| 1795 | break; |
| 1796 | } |
Alexey Bataev | 6956e2e | 2015-02-05 06:35:41 +0000 | [diff] [blame] | 1797 | case OMPRTL__kmpc_single: { |
| 1798 | // Build kmp_int32 __kmpc_single(ident_t *loc, kmp_int32 global_tid); |
| 1799 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1800 | auto *FnTy = |
Alexey Bataev | 6956e2e | 2015-02-05 06:35:41 +0000 | [diff] [blame] | 1801 | llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg=*/false); |
| 1802 | RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_single"); |
| 1803 | break; |
| 1804 | } |
| 1805 | case OMPRTL__kmpc_end_single: { |
| 1806 | // Build void __kmpc_end_single(ident_t *loc, kmp_int32 global_tid); |
| 1807 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1808 | auto *FnTy = |
Alexey Bataev | 6956e2e | 2015-02-05 06:35:41 +0000 | [diff] [blame] | 1809 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false); |
| 1810 | RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_end_single"); |
| 1811 | break; |
| 1812 | } |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 1813 | case OMPRTL__kmpc_omp_task_alloc: { |
| 1814 | // Build kmp_task_t *__kmpc_omp_task_alloc(ident_t *, kmp_int32 gtid, |
| 1815 | // kmp_int32 flags, size_t sizeof_kmp_task_t, size_t sizeof_shareds, |
| 1816 | // kmp_routine_entry_t *task_entry); |
| 1817 | assert(KmpRoutineEntryPtrTy != nullptr && |
| 1818 | "Type kmp_routine_entry_t must be created."); |
| 1819 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty, CGM.Int32Ty, |
| 1820 | CGM.SizeTy, CGM.SizeTy, KmpRoutineEntryPtrTy}; |
| 1821 | // Return void * and then cast to particular kmp_task_t type. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1822 | auto *FnTy = |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 1823 | llvm::FunctionType::get(CGM.VoidPtrTy, TypeParams, /*isVarArg=*/false); |
| 1824 | RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_omp_task_alloc"); |
| 1825 | break; |
| 1826 | } |
| 1827 | case OMPRTL__kmpc_omp_task: { |
| 1828 | // Build kmp_int32 __kmpc_omp_task(ident_t *, kmp_int32 gtid, kmp_task_t |
| 1829 | // *new_task); |
| 1830 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty, |
| 1831 | CGM.VoidPtrTy}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1832 | auto *FnTy = |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 1833 | llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg=*/false); |
| 1834 | RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_omp_task"); |
| 1835 | break; |
| 1836 | } |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 1837 | case OMPRTL__kmpc_copyprivate: { |
| 1838 | // Build void __kmpc_copyprivate(ident_t *loc, kmp_int32 global_tid, |
Alexey Bataev | 66beaa9 | 2015-04-30 03:47:32 +0000 | [diff] [blame] | 1839 | // size_t cpy_size, void *cpy_data, void(*cpy_func)(void *, void *), |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 1840 | // kmp_int32 didit); |
| 1841 | llvm::Type *CpyTypeParams[] = {CGM.VoidPtrTy, CGM.VoidPtrTy}; |
| 1842 | auto *CpyFnTy = |
| 1843 | llvm::FunctionType::get(CGM.VoidTy, CpyTypeParams, /*isVarArg=*/false); |
Alexey Bataev | 66beaa9 | 2015-04-30 03:47:32 +0000 | [diff] [blame] | 1844 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty, CGM.SizeTy, |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 1845 | CGM.VoidPtrTy, CpyFnTy->getPointerTo(), |
| 1846 | CGM.Int32Ty}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1847 | auto *FnTy = |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 1848 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false); |
| 1849 | RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_copyprivate"); |
| 1850 | break; |
| 1851 | } |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 1852 | case OMPRTL__kmpc_reduce: { |
| 1853 | // Build kmp_int32 __kmpc_reduce(ident_t *loc, kmp_int32 global_tid, |
| 1854 | // kmp_int32 num_vars, size_t reduce_size, void *reduce_data, void |
| 1855 | // (*reduce_func)(void *lhs_data, void *rhs_data), kmp_critical_name *lck); |
| 1856 | llvm::Type *ReduceTypeParams[] = {CGM.VoidPtrTy, CGM.VoidPtrTy}; |
| 1857 | auto *ReduceFnTy = llvm::FunctionType::get(CGM.VoidTy, ReduceTypeParams, |
| 1858 | /*isVarArg=*/false); |
| 1859 | llvm::Type *TypeParams[] = { |
| 1860 | getIdentTyPointerTy(), CGM.Int32Ty, CGM.Int32Ty, CGM.SizeTy, |
| 1861 | CGM.VoidPtrTy, ReduceFnTy->getPointerTo(), |
| 1862 | llvm::PointerType::getUnqual(KmpCriticalNameTy)}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1863 | auto *FnTy = |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 1864 | llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg=*/false); |
| 1865 | RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_reduce"); |
| 1866 | break; |
| 1867 | } |
| 1868 | case OMPRTL__kmpc_reduce_nowait: { |
| 1869 | // Build kmp_int32 __kmpc_reduce_nowait(ident_t *loc, kmp_int32 |
| 1870 | // global_tid, kmp_int32 num_vars, size_t reduce_size, void *reduce_data, |
| 1871 | // void (*reduce_func)(void *lhs_data, void *rhs_data), kmp_critical_name |
| 1872 | // *lck); |
| 1873 | llvm::Type *ReduceTypeParams[] = {CGM.VoidPtrTy, CGM.VoidPtrTy}; |
| 1874 | auto *ReduceFnTy = llvm::FunctionType::get(CGM.VoidTy, ReduceTypeParams, |
| 1875 | /*isVarArg=*/false); |
| 1876 | llvm::Type *TypeParams[] = { |
| 1877 | getIdentTyPointerTy(), CGM.Int32Ty, CGM.Int32Ty, CGM.SizeTy, |
| 1878 | CGM.VoidPtrTy, ReduceFnTy->getPointerTo(), |
| 1879 | llvm::PointerType::getUnqual(KmpCriticalNameTy)}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1880 | auto *FnTy = |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 1881 | llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg=*/false); |
| 1882 | RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_reduce_nowait"); |
| 1883 | break; |
| 1884 | } |
| 1885 | case OMPRTL__kmpc_end_reduce: { |
| 1886 | // Build void __kmpc_end_reduce(ident_t *loc, kmp_int32 global_tid, |
| 1887 | // kmp_critical_name *lck); |
| 1888 | llvm::Type *TypeParams[] = { |
| 1889 | getIdentTyPointerTy(), CGM.Int32Ty, |
| 1890 | llvm::PointerType::getUnqual(KmpCriticalNameTy)}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1891 | auto *FnTy = |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 1892 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false); |
| 1893 | RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_end_reduce"); |
| 1894 | break; |
| 1895 | } |
| 1896 | case OMPRTL__kmpc_end_reduce_nowait: { |
| 1897 | // Build __kmpc_end_reduce_nowait(ident_t *loc, kmp_int32 global_tid, |
| 1898 | // kmp_critical_name *lck); |
| 1899 | llvm::Type *TypeParams[] = { |
| 1900 | getIdentTyPointerTy(), CGM.Int32Ty, |
| 1901 | llvm::PointerType::getUnqual(KmpCriticalNameTy)}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1902 | auto *FnTy = |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 1903 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false); |
| 1904 | RTLFn = |
| 1905 | CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_end_reduce_nowait"); |
| 1906 | break; |
| 1907 | } |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 1908 | case OMPRTL__kmpc_omp_task_begin_if0: { |
| 1909 | // Build void __kmpc_omp_task(ident_t *, kmp_int32 gtid, kmp_task_t |
| 1910 | // *new_task); |
| 1911 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty, |
| 1912 | CGM.VoidPtrTy}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1913 | auto *FnTy = |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 1914 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false); |
| 1915 | RTLFn = |
| 1916 | CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_omp_task_begin_if0"); |
| 1917 | break; |
| 1918 | } |
| 1919 | case OMPRTL__kmpc_omp_task_complete_if0: { |
| 1920 | // Build void __kmpc_omp_task(ident_t *, kmp_int32 gtid, kmp_task_t |
| 1921 | // *new_task); |
| 1922 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty, |
| 1923 | CGM.VoidPtrTy}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1924 | auto *FnTy = |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 1925 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false); |
| 1926 | RTLFn = CGM.CreateRuntimeFunction(FnTy, |
| 1927 | /*Name=*/"__kmpc_omp_task_complete_if0"); |
| 1928 | break; |
| 1929 | } |
Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 1930 | case OMPRTL__kmpc_ordered: { |
| 1931 | // Build void __kmpc_ordered(ident_t *loc, kmp_int32 global_tid); |
| 1932 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1933 | auto *FnTy = |
Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 1934 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false); |
| 1935 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_ordered"); |
| 1936 | break; |
| 1937 | } |
| 1938 | case OMPRTL__kmpc_end_ordered: { |
Alexey Bataev | c30dd2d | 2015-06-18 12:14:09 +0000 | [diff] [blame] | 1939 | // Build void __kmpc_end_ordered(ident_t *loc, kmp_int32 global_tid); |
Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 1940 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1941 | auto *FnTy = |
Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 1942 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false); |
| 1943 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_end_ordered"); |
| 1944 | break; |
| 1945 | } |
Alexey Bataev | 8b8e202 | 2015-04-27 05:22:09 +0000 | [diff] [blame] | 1946 | case OMPRTL__kmpc_omp_taskwait: { |
| 1947 | // Build kmp_int32 __kmpc_omp_taskwait(ident_t *loc, kmp_int32 global_tid); |
| 1948 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1949 | auto *FnTy = |
Alexey Bataev | 8b8e202 | 2015-04-27 05:22:09 +0000 | [diff] [blame] | 1950 | llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg=*/false); |
| 1951 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_omp_taskwait"); |
| 1952 | break; |
| 1953 | } |
Alexey Bataev | c30dd2d | 2015-06-18 12:14:09 +0000 | [diff] [blame] | 1954 | case OMPRTL__kmpc_taskgroup: { |
| 1955 | // Build void __kmpc_taskgroup(ident_t *loc, kmp_int32 global_tid); |
| 1956 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1957 | auto *FnTy = |
Alexey Bataev | c30dd2d | 2015-06-18 12:14:09 +0000 | [diff] [blame] | 1958 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false); |
| 1959 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_taskgroup"); |
| 1960 | break; |
| 1961 | } |
| 1962 | case OMPRTL__kmpc_end_taskgroup: { |
| 1963 | // Build void __kmpc_end_taskgroup(ident_t *loc, kmp_int32 global_tid); |
| 1964 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1965 | auto *FnTy = |
Alexey Bataev | c30dd2d | 2015-06-18 12:14:09 +0000 | [diff] [blame] | 1966 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false); |
| 1967 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_end_taskgroup"); |
| 1968 | break; |
| 1969 | } |
Alexey Bataev | 7f210c6 | 2015-06-18 13:40:03 +0000 | [diff] [blame] | 1970 | case OMPRTL__kmpc_push_proc_bind: { |
| 1971 | // Build void __kmpc_push_proc_bind(ident_t *loc, kmp_int32 global_tid, |
| 1972 | // int proc_bind) |
| 1973 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty, CGM.IntTy}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1974 | auto *FnTy = |
Alexey Bataev | 7f210c6 | 2015-06-18 13:40:03 +0000 | [diff] [blame] | 1975 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false); |
| 1976 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_push_proc_bind"); |
| 1977 | break; |
| 1978 | } |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 1979 | case OMPRTL__kmpc_omp_task_with_deps: { |
| 1980 | // Build kmp_int32 __kmpc_omp_task_with_deps(ident_t *, kmp_int32 gtid, |
| 1981 | // kmp_task_t *new_task, kmp_int32 ndeps, kmp_depend_info_t *dep_list, |
| 1982 | // kmp_int32 ndeps_noalias, kmp_depend_info_t *noalias_dep_list); |
| 1983 | llvm::Type *TypeParams[] = { |
| 1984 | getIdentTyPointerTy(), CGM.Int32Ty, CGM.VoidPtrTy, CGM.Int32Ty, |
| 1985 | CGM.VoidPtrTy, CGM.Int32Ty, CGM.VoidPtrTy}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1986 | auto *FnTy = |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 1987 | llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg=*/false); |
| 1988 | RTLFn = |
| 1989 | CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_omp_task_with_deps"); |
| 1990 | break; |
| 1991 | } |
| 1992 | case OMPRTL__kmpc_omp_wait_deps: { |
| 1993 | // Build void __kmpc_omp_wait_deps(ident_t *, kmp_int32 gtid, |
| 1994 | // kmp_int32 ndeps, kmp_depend_info_t *dep_list, kmp_int32 ndeps_noalias, |
| 1995 | // kmp_depend_info_t *noalias_dep_list); |
| 1996 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty, |
| 1997 | CGM.Int32Ty, CGM.VoidPtrTy, |
| 1998 | CGM.Int32Ty, CGM.VoidPtrTy}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1999 | auto *FnTy = |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 2000 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false); |
| 2001 | RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_omp_wait_deps"); |
| 2002 | break; |
| 2003 | } |
Alexey Bataev | 0f34da1 | 2015-07-02 04:17:07 +0000 | [diff] [blame] | 2004 | case OMPRTL__kmpc_cancellationpoint: { |
| 2005 | // Build kmp_int32 __kmpc_cancellationpoint(ident_t *loc, kmp_int32 |
| 2006 | // global_tid, kmp_int32 cncl_kind) |
| 2007 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty, CGM.IntTy}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2008 | auto *FnTy = |
Alexey Bataev | 0f34da1 | 2015-07-02 04:17:07 +0000 | [diff] [blame] | 2009 | llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg*/ false); |
| 2010 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_cancellationpoint"); |
| 2011 | break; |
| 2012 | } |
Alexey Bataev | 7d5d33e | 2015-07-06 05:50:32 +0000 | [diff] [blame] | 2013 | case OMPRTL__kmpc_cancel: { |
| 2014 | // Build kmp_int32 __kmpc_cancel(ident_t *loc, kmp_int32 global_tid, |
| 2015 | // kmp_int32 cncl_kind) |
| 2016 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty, CGM.IntTy}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2017 | auto *FnTy = |
Alexey Bataev | 7d5d33e | 2015-07-06 05:50:32 +0000 | [diff] [blame] | 2018 | llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg*/ false); |
| 2019 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_cancel"); |
| 2020 | break; |
| 2021 | } |
Carlo Bertolli | 430d8ec | 2016-03-03 20:34:23 +0000 | [diff] [blame] | 2022 | case OMPRTL__kmpc_push_num_teams: { |
| 2023 | // Build void kmpc_push_num_teams (ident_t loc, kmp_int32 global_tid, |
| 2024 | // kmp_int32 num_teams, kmp_int32 num_threads) |
| 2025 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty, CGM.Int32Ty, |
| 2026 | CGM.Int32Ty}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2027 | auto *FnTy = |
Carlo Bertolli | 430d8ec | 2016-03-03 20:34:23 +0000 | [diff] [blame] | 2028 | llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg*/ false); |
| 2029 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_push_num_teams"); |
| 2030 | break; |
| 2031 | } |
| 2032 | case OMPRTL__kmpc_fork_teams: { |
| 2033 | // Build void __kmpc_fork_teams(ident_t *loc, kmp_int32 argc, kmpc_micro |
| 2034 | // microtask, ...); |
| 2035 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty, |
| 2036 | getKmpc_MicroPointerTy()}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2037 | auto *FnTy = |
Carlo Bertolli | 430d8ec | 2016-03-03 20:34:23 +0000 | [diff] [blame] | 2038 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ true); |
| 2039 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_fork_teams"); |
| 2040 | break; |
| 2041 | } |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 2042 | case OMPRTL__kmpc_taskloop: { |
| 2043 | // Build void __kmpc_taskloop(ident_t *loc, int gtid, kmp_task_t *task, int |
| 2044 | // if_val, kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st, int nogroup, int |
| 2045 | // sched, kmp_uint64 grainsize, void *task_dup); |
| 2046 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), |
| 2047 | CGM.IntTy, |
| 2048 | CGM.VoidPtrTy, |
| 2049 | CGM.IntTy, |
| 2050 | CGM.Int64Ty->getPointerTo(), |
| 2051 | CGM.Int64Ty->getPointerTo(), |
| 2052 | CGM.Int64Ty, |
| 2053 | CGM.IntTy, |
| 2054 | CGM.IntTy, |
| 2055 | CGM.Int64Ty, |
| 2056 | CGM.VoidPtrTy}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2057 | auto *FnTy = |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 2058 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false); |
| 2059 | RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_taskloop"); |
| 2060 | break; |
| 2061 | } |
Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 2062 | case OMPRTL__kmpc_doacross_init: { |
| 2063 | // Build void __kmpc_doacross_init(ident_t *loc, kmp_int32 gtid, kmp_int32 |
| 2064 | // num_dims, struct kmp_dim *dims); |
| 2065 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), |
| 2066 | CGM.Int32Ty, |
| 2067 | CGM.Int32Ty, |
| 2068 | CGM.VoidPtrTy}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2069 | auto *FnTy = |
Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 2070 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false); |
| 2071 | RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_doacross_init"); |
| 2072 | break; |
| 2073 | } |
| 2074 | case OMPRTL__kmpc_doacross_fini: { |
| 2075 | // Build void __kmpc_doacross_fini(ident_t *loc, kmp_int32 gtid); |
| 2076 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2077 | auto *FnTy = |
Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 2078 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false); |
| 2079 | RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_doacross_fini"); |
| 2080 | break; |
| 2081 | } |
| 2082 | case OMPRTL__kmpc_doacross_post: { |
| 2083 | // Build void __kmpc_doacross_post(ident_t *loc, kmp_int32 gtid, kmp_int64 |
| 2084 | // *vec); |
| 2085 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty, |
| 2086 | CGM.Int64Ty->getPointerTo()}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2087 | auto *FnTy = |
Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 2088 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false); |
| 2089 | RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_doacross_post"); |
| 2090 | break; |
| 2091 | } |
| 2092 | case OMPRTL__kmpc_doacross_wait: { |
| 2093 | // Build void __kmpc_doacross_wait(ident_t *loc, kmp_int32 gtid, kmp_int64 |
| 2094 | // *vec); |
| 2095 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty, |
| 2096 | CGM.Int64Ty->getPointerTo()}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2097 | auto *FnTy = |
Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 2098 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false); |
| 2099 | RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_doacross_wait"); |
| 2100 | break; |
| 2101 | } |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 2102 | case OMPRTL__kmpc_task_reduction_init: { |
| 2103 | // Build void *__kmpc_task_reduction_init(int gtid, int num_data, void |
| 2104 | // *data); |
| 2105 | llvm::Type *TypeParams[] = {CGM.IntTy, CGM.IntTy, CGM.VoidPtrTy}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2106 | auto *FnTy = |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 2107 | llvm::FunctionType::get(CGM.VoidPtrTy, TypeParams, /*isVarArg=*/false); |
| 2108 | RTLFn = |
| 2109 | CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_task_reduction_init"); |
| 2110 | break; |
| 2111 | } |
| 2112 | case OMPRTL__kmpc_task_reduction_get_th_data: { |
| 2113 | // Build void *__kmpc_task_reduction_get_th_data(int gtid, void *tg, void |
| 2114 | // *d); |
| 2115 | llvm::Type *TypeParams[] = {CGM.IntTy, CGM.VoidPtrTy, CGM.VoidPtrTy}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2116 | auto *FnTy = |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 2117 | llvm::FunctionType::get(CGM.VoidPtrTy, TypeParams, /*isVarArg=*/false); |
| 2118 | RTLFn = CGM.CreateRuntimeFunction( |
| 2119 | FnTy, /*Name=*/"__kmpc_task_reduction_get_th_data"); |
| 2120 | break; |
| 2121 | } |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 2122 | case OMPRTL__tgt_target: { |
George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 2123 | // Build int32_t __tgt_target(int64_t device_id, void *host_ptr, int32_t |
| 2124 | // 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] | 2125 | // *arg_types); |
George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 2126 | llvm::Type *TypeParams[] = {CGM.Int64Ty, |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 2127 | CGM.VoidPtrTy, |
| 2128 | CGM.Int32Ty, |
| 2129 | CGM.VoidPtrPtrTy, |
| 2130 | CGM.VoidPtrPtrTy, |
| 2131 | CGM.SizeTy->getPointerTo(), |
George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 2132 | CGM.Int64Ty->getPointerTo()}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2133 | auto *FnTy = |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 2134 | llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg*/ false); |
| 2135 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__tgt_target"); |
| 2136 | break; |
| 2137 | } |
Alexey Bataev | a9f77c6 | 2017-12-13 21:04:20 +0000 | [diff] [blame] | 2138 | case OMPRTL__tgt_target_nowait: { |
| 2139 | // Build int32_t __tgt_target_nowait(int64_t device_id, void *host_ptr, |
| 2140 | // int32_t arg_num, void** args_base, void **args, size_t *arg_sizes, |
| 2141 | // int64_t *arg_types); |
| 2142 | llvm::Type *TypeParams[] = {CGM.Int64Ty, |
| 2143 | CGM.VoidPtrTy, |
| 2144 | CGM.Int32Ty, |
| 2145 | CGM.VoidPtrPtrTy, |
| 2146 | CGM.VoidPtrPtrTy, |
| 2147 | CGM.SizeTy->getPointerTo(), |
| 2148 | CGM.Int64Ty->getPointerTo()}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2149 | auto *FnTy = |
Alexey Bataev | a9f77c6 | 2017-12-13 21:04:20 +0000 | [diff] [blame] | 2150 | llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg*/ false); |
| 2151 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__tgt_target_nowait"); |
| 2152 | break; |
| 2153 | } |
Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 2154 | case OMPRTL__tgt_target_teams: { |
George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 2155 | // 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] | 2156 | // 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] | 2157 | // int64_t *arg_types, int32_t num_teams, int32_t thread_limit); |
| 2158 | llvm::Type *TypeParams[] = {CGM.Int64Ty, |
Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 2159 | CGM.VoidPtrTy, |
| 2160 | CGM.Int32Ty, |
| 2161 | CGM.VoidPtrPtrTy, |
| 2162 | CGM.VoidPtrPtrTy, |
| 2163 | CGM.SizeTy->getPointerTo(), |
George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 2164 | CGM.Int64Ty->getPointerTo(), |
Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 2165 | CGM.Int32Ty, |
| 2166 | CGM.Int32Ty}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2167 | auto *FnTy = |
Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 2168 | llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg*/ false); |
| 2169 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__tgt_target_teams"); |
| 2170 | break; |
| 2171 | } |
Alexey Bataev | a9f77c6 | 2017-12-13 21:04:20 +0000 | [diff] [blame] | 2172 | case OMPRTL__tgt_target_teams_nowait: { |
| 2173 | // Build int32_t __tgt_target_teams_nowait(int64_t device_id, void |
| 2174 | // *host_ptr, int32_t arg_num, void** args_base, void **args, size_t |
| 2175 | // *arg_sizes, int64_t *arg_types, int32_t num_teams, int32_t thread_limit); |
| 2176 | llvm::Type *TypeParams[] = {CGM.Int64Ty, |
| 2177 | CGM.VoidPtrTy, |
| 2178 | CGM.Int32Ty, |
| 2179 | CGM.VoidPtrPtrTy, |
| 2180 | CGM.VoidPtrPtrTy, |
| 2181 | CGM.SizeTy->getPointerTo(), |
| 2182 | CGM.Int64Ty->getPointerTo(), |
| 2183 | CGM.Int32Ty, |
| 2184 | CGM.Int32Ty}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2185 | auto *FnTy = |
Alexey Bataev | a9f77c6 | 2017-12-13 21:04:20 +0000 | [diff] [blame] | 2186 | llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg*/ false); |
| 2187 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__tgt_target_teams_nowait"); |
| 2188 | break; |
| 2189 | } |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 2190 | case OMPRTL__tgt_register_lib: { |
| 2191 | // Build void __tgt_register_lib(__tgt_bin_desc *desc); |
| 2192 | QualType ParamTy = |
| 2193 | CGM.getContext().getPointerType(getTgtBinaryDescriptorQTy()); |
| 2194 | llvm::Type *TypeParams[] = {CGM.getTypes().ConvertTypeForMem(ParamTy)}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2195 | auto *FnTy = |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 2196 | llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg*/ false); |
| 2197 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__tgt_register_lib"); |
| 2198 | break; |
| 2199 | } |
| 2200 | case OMPRTL__tgt_unregister_lib: { |
| 2201 | // Build void __tgt_unregister_lib(__tgt_bin_desc *desc); |
| 2202 | QualType ParamTy = |
| 2203 | CGM.getContext().getPointerType(getTgtBinaryDescriptorQTy()); |
| 2204 | llvm::Type *TypeParams[] = {CGM.getTypes().ConvertTypeForMem(ParamTy)}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2205 | auto *FnTy = |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 2206 | llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg*/ false); |
| 2207 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__tgt_unregister_lib"); |
| 2208 | break; |
| 2209 | } |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 2210 | case OMPRTL__tgt_target_data_begin: { |
George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 2211 | // Build void __tgt_target_data_begin(int64_t device_id, int32_t arg_num, |
| 2212 | // void** args_base, void **args, size_t *arg_sizes, int64_t *arg_types); |
| 2213 | llvm::Type *TypeParams[] = {CGM.Int64Ty, |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 2214 | CGM.Int32Ty, |
| 2215 | CGM.VoidPtrPtrTy, |
| 2216 | CGM.VoidPtrPtrTy, |
| 2217 | CGM.SizeTy->getPointerTo(), |
George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 2218 | CGM.Int64Ty->getPointerTo()}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2219 | auto *FnTy = |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 2220 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false); |
| 2221 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__tgt_target_data_begin"); |
| 2222 | break; |
| 2223 | } |
Alexey Bataev | 0cc6b8e | 2017-12-14 17:00:17 +0000 | [diff] [blame] | 2224 | case OMPRTL__tgt_target_data_begin_nowait: { |
| 2225 | // Build void __tgt_target_data_begin_nowait(int64_t device_id, int32_t |
| 2226 | // arg_num, void** args_base, void **args, size_t *arg_sizes, int64_t |
| 2227 | // *arg_types); |
| 2228 | llvm::Type *TypeParams[] = {CGM.Int64Ty, |
| 2229 | CGM.Int32Ty, |
| 2230 | CGM.VoidPtrPtrTy, |
| 2231 | CGM.VoidPtrPtrTy, |
| 2232 | CGM.SizeTy->getPointerTo(), |
| 2233 | CGM.Int64Ty->getPointerTo()}; |
| 2234 | auto *FnTy = |
| 2235 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false); |
| 2236 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__tgt_target_data_begin_nowait"); |
| 2237 | break; |
| 2238 | } |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 2239 | case OMPRTL__tgt_target_data_end: { |
George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 2240 | // Build void __tgt_target_data_end(int64_t device_id, int32_t arg_num, |
| 2241 | // void** args_base, void **args, size_t *arg_sizes, int64_t *arg_types); |
| 2242 | llvm::Type *TypeParams[] = {CGM.Int64Ty, |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 2243 | CGM.Int32Ty, |
| 2244 | CGM.VoidPtrPtrTy, |
| 2245 | CGM.VoidPtrPtrTy, |
| 2246 | CGM.SizeTy->getPointerTo(), |
George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 2247 | CGM.Int64Ty->getPointerTo()}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2248 | auto *FnTy = |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 2249 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false); |
| 2250 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__tgt_target_data_end"); |
| 2251 | break; |
| 2252 | } |
Alexey Bataev | 0cc6b8e | 2017-12-14 17:00:17 +0000 | [diff] [blame] | 2253 | case OMPRTL__tgt_target_data_end_nowait: { |
| 2254 | // Build void __tgt_target_data_end_nowait(int64_t device_id, int32_t |
| 2255 | // arg_num, void** args_base, void **args, size_t *arg_sizes, int64_t |
| 2256 | // *arg_types); |
| 2257 | llvm::Type *TypeParams[] = {CGM.Int64Ty, |
| 2258 | CGM.Int32Ty, |
| 2259 | CGM.VoidPtrPtrTy, |
| 2260 | CGM.VoidPtrPtrTy, |
| 2261 | CGM.SizeTy->getPointerTo(), |
| 2262 | CGM.Int64Ty->getPointerTo()}; |
| 2263 | auto *FnTy = |
| 2264 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false); |
| 2265 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__tgt_target_data_end_nowait"); |
| 2266 | break; |
| 2267 | } |
Samuel Antao | 8d2d730 | 2016-05-26 18:30:22 +0000 | [diff] [blame] | 2268 | case OMPRTL__tgt_target_data_update: { |
George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 2269 | // Build void __tgt_target_data_update(int64_t device_id, int32_t arg_num, |
| 2270 | // void** args_base, void **args, size_t *arg_sizes, int64_t *arg_types); |
| 2271 | llvm::Type *TypeParams[] = {CGM.Int64Ty, |
Samuel Antao | 8d2d730 | 2016-05-26 18:30:22 +0000 | [diff] [blame] | 2272 | CGM.Int32Ty, |
| 2273 | CGM.VoidPtrPtrTy, |
| 2274 | CGM.VoidPtrPtrTy, |
| 2275 | CGM.SizeTy->getPointerTo(), |
George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 2276 | CGM.Int64Ty->getPointerTo()}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2277 | auto *FnTy = |
Samuel Antao | 8d2d730 | 2016-05-26 18:30:22 +0000 | [diff] [blame] | 2278 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false); |
| 2279 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__tgt_target_data_update"); |
| 2280 | break; |
| 2281 | } |
Alexey Bataev | 0cc6b8e | 2017-12-14 17:00:17 +0000 | [diff] [blame] | 2282 | case OMPRTL__tgt_target_data_update_nowait: { |
| 2283 | // Build void __tgt_target_data_update_nowait(int64_t device_id, int32_t |
| 2284 | // arg_num, void** args_base, void **args, size_t *arg_sizes, int64_t |
| 2285 | // *arg_types); |
| 2286 | llvm::Type *TypeParams[] = {CGM.Int64Ty, |
| 2287 | CGM.Int32Ty, |
| 2288 | CGM.VoidPtrPtrTy, |
| 2289 | CGM.VoidPtrPtrTy, |
| 2290 | CGM.SizeTy->getPointerTo(), |
| 2291 | CGM.Int64Ty->getPointerTo()}; |
| 2292 | auto *FnTy = |
| 2293 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false); |
| 2294 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__tgt_target_data_update_nowait"); |
| 2295 | break; |
| 2296 | } |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 2297 | } |
Alexey Bataev | 50b3c95 | 2016-02-19 10:38:26 +0000 | [diff] [blame] | 2298 | assert(RTLFn && "Unable to find OpenMP runtime function"); |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 2299 | return RTLFn; |
| 2300 | } |
Alexey Bataev | 3a3bf0b | 2014-09-22 10:01:53 +0000 | [diff] [blame] | 2301 | |
Alexander Musman | 21212e4 | 2015-03-13 10:38:23 +0000 | [diff] [blame] | 2302 | llvm::Constant *CGOpenMPRuntime::createForStaticInitFunction(unsigned IVSize, |
| 2303 | bool IVSigned) { |
| 2304 | assert((IVSize == 32 || IVSize == 64) && |
| 2305 | "IV size is not compatible with the omp runtime"); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2306 | StringRef Name = IVSize == 32 ? (IVSigned ? "__kmpc_for_static_init_4" |
| 2307 | : "__kmpc_for_static_init_4u") |
| 2308 | : (IVSigned ? "__kmpc_for_static_init_8" |
| 2309 | : "__kmpc_for_static_init_8u"); |
| 2310 | llvm::Type *ITy = IVSize == 32 ? CGM.Int32Ty : CGM.Int64Ty; |
| 2311 | auto *PtrTy = llvm::PointerType::getUnqual(ITy); |
Alexander Musman | 21212e4 | 2015-03-13 10:38:23 +0000 | [diff] [blame] | 2312 | llvm::Type *TypeParams[] = { |
| 2313 | getIdentTyPointerTy(), // loc |
| 2314 | CGM.Int32Ty, // tid |
| 2315 | CGM.Int32Ty, // schedtype |
| 2316 | llvm::PointerType::getUnqual(CGM.Int32Ty), // p_lastiter |
| 2317 | PtrTy, // p_lower |
| 2318 | PtrTy, // p_upper |
| 2319 | PtrTy, // p_stride |
| 2320 | ITy, // incr |
| 2321 | ITy // chunk |
| 2322 | }; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2323 | auto *FnTy = |
Alexander Musman | 21212e4 | 2015-03-13 10:38:23 +0000 | [diff] [blame] | 2324 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false); |
| 2325 | return CGM.CreateRuntimeFunction(FnTy, Name); |
| 2326 | } |
| 2327 | |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 2328 | llvm::Constant *CGOpenMPRuntime::createDispatchInitFunction(unsigned IVSize, |
| 2329 | bool IVSigned) { |
| 2330 | assert((IVSize == 32 || IVSize == 64) && |
| 2331 | "IV size is not compatible with the omp runtime"); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2332 | StringRef Name = |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 2333 | IVSize == 32 |
| 2334 | ? (IVSigned ? "__kmpc_dispatch_init_4" : "__kmpc_dispatch_init_4u") |
| 2335 | : (IVSigned ? "__kmpc_dispatch_init_8" : "__kmpc_dispatch_init_8u"); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2336 | llvm::Type *ITy = IVSize == 32 ? CGM.Int32Ty : CGM.Int64Ty; |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 2337 | llvm::Type *TypeParams[] = { getIdentTyPointerTy(), // loc |
| 2338 | CGM.Int32Ty, // tid |
| 2339 | CGM.Int32Ty, // schedtype |
| 2340 | ITy, // lower |
| 2341 | ITy, // upper |
| 2342 | ITy, // stride |
| 2343 | ITy // chunk |
| 2344 | }; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2345 | auto *FnTy = |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 2346 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false); |
| 2347 | return CGM.CreateRuntimeFunction(FnTy, Name); |
| 2348 | } |
| 2349 | |
Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 2350 | llvm::Constant *CGOpenMPRuntime::createDispatchFiniFunction(unsigned IVSize, |
| 2351 | bool IVSigned) { |
| 2352 | assert((IVSize == 32 || IVSize == 64) && |
| 2353 | "IV size is not compatible with the omp runtime"); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2354 | StringRef Name = |
Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 2355 | IVSize == 32 |
| 2356 | ? (IVSigned ? "__kmpc_dispatch_fini_4" : "__kmpc_dispatch_fini_4u") |
| 2357 | : (IVSigned ? "__kmpc_dispatch_fini_8" : "__kmpc_dispatch_fini_8u"); |
| 2358 | llvm::Type *TypeParams[] = { |
| 2359 | getIdentTyPointerTy(), // loc |
| 2360 | CGM.Int32Ty, // tid |
| 2361 | }; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2362 | auto *FnTy = |
Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 2363 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false); |
| 2364 | return CGM.CreateRuntimeFunction(FnTy, Name); |
| 2365 | } |
| 2366 | |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 2367 | llvm::Constant *CGOpenMPRuntime::createDispatchNextFunction(unsigned IVSize, |
| 2368 | bool IVSigned) { |
| 2369 | assert((IVSize == 32 || IVSize == 64) && |
| 2370 | "IV size is not compatible with the omp runtime"); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2371 | StringRef Name = |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 2372 | IVSize == 32 |
| 2373 | ? (IVSigned ? "__kmpc_dispatch_next_4" : "__kmpc_dispatch_next_4u") |
| 2374 | : (IVSigned ? "__kmpc_dispatch_next_8" : "__kmpc_dispatch_next_8u"); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2375 | llvm::Type *ITy = IVSize == 32 ? CGM.Int32Ty : CGM.Int64Ty; |
| 2376 | auto *PtrTy = llvm::PointerType::getUnqual(ITy); |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 2377 | llvm::Type *TypeParams[] = { |
| 2378 | getIdentTyPointerTy(), // loc |
| 2379 | CGM.Int32Ty, // tid |
| 2380 | llvm::PointerType::getUnqual(CGM.Int32Ty), // p_lastiter |
| 2381 | PtrTy, // p_lower |
| 2382 | PtrTy, // p_upper |
| 2383 | PtrTy // p_stride |
| 2384 | }; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2385 | auto *FnTy = |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 2386 | llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg*/ false); |
| 2387 | return CGM.CreateRuntimeFunction(FnTy, Name); |
| 2388 | } |
| 2389 | |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 2390 | Address CGOpenMPRuntime::getAddrOfDeclareTargetLink(const VarDecl *VD) { |
| 2391 | if (CGM.getLangOpts().OpenMPSimd) |
| 2392 | return Address::invalid(); |
Alexey Bataev | 92327c5 | 2018-03-26 16:40:55 +0000 | [diff] [blame] | 2393 | llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res = |
Alexey Bataev | 97b7221 | 2018-08-14 18:31:20 +0000 | [diff] [blame] | 2394 | OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD); |
Alexey Bataev | 92327c5 | 2018-03-26 16:40:55 +0000 | [diff] [blame] | 2395 | if (Res && *Res == OMPDeclareTargetDeclAttr::MT_Link) { |
| 2396 | SmallString<64> PtrName; |
| 2397 | { |
| 2398 | llvm::raw_svector_ostream OS(PtrName); |
| 2399 | OS << CGM.getMangledName(GlobalDecl(VD)) << "_decl_tgt_link_ptr"; |
| 2400 | } |
| 2401 | llvm::Value *Ptr = CGM.getModule().getNamedValue(PtrName); |
| 2402 | if (!Ptr) { |
| 2403 | QualType PtrTy = CGM.getContext().getPointerType(VD->getType()); |
| 2404 | Ptr = getOrCreateInternalVariable(CGM.getTypes().ConvertTypeForMem(PtrTy), |
| 2405 | PtrName); |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 2406 | if (!CGM.getLangOpts().OpenMPIsDevice) { |
| 2407 | auto *GV = cast<llvm::GlobalVariable>(Ptr); |
| 2408 | GV->setLinkage(llvm::GlobalValue::ExternalLinkage); |
| 2409 | GV->setInitializer(CGM.GetAddrOfGlobal(VD)); |
| 2410 | } |
| 2411 | CGM.addUsedGlobal(cast<llvm::GlobalValue>(Ptr)); |
| 2412 | registerTargetGlobalVariable(VD, cast<llvm::Constant>(Ptr)); |
Alexey Bataev | 92327c5 | 2018-03-26 16:40:55 +0000 | [diff] [blame] | 2413 | } |
| 2414 | return Address(Ptr, CGM.getContext().getDeclAlign(VD)); |
| 2415 | } |
| 2416 | return Address::invalid(); |
| 2417 | } |
| 2418 | |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2419 | llvm::Constant * |
| 2420 | CGOpenMPRuntime::getOrCreateThreadPrivateCache(const VarDecl *VD) { |
Samuel Antao | f8b5012 | 2015-07-13 22:54:53 +0000 | [diff] [blame] | 2421 | assert(!CGM.getLangOpts().OpenMPUseTLS || |
| 2422 | !CGM.getContext().getTargetInfo().isTLSSupported()); |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2423 | // Lookup the entry, lazily creating it if necessary. |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 2424 | std::string Suffix = getName({"cache", ""}); |
| 2425 | return getOrCreateInternalVariable( |
| 2426 | CGM.Int8PtrPtrTy, Twine(CGM.getMangledName(VD)).concat(Suffix)); |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2427 | } |
| 2428 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2429 | Address CGOpenMPRuntime::getAddrOfThreadPrivate(CodeGenFunction &CGF, |
| 2430 | const VarDecl *VD, |
| 2431 | Address VDAddr, |
| 2432 | SourceLocation Loc) { |
Samuel Antao | f8b5012 | 2015-07-13 22:54:53 +0000 | [diff] [blame] | 2433 | if (CGM.getLangOpts().OpenMPUseTLS && |
| 2434 | CGM.getContext().getTargetInfo().isTLSSupported()) |
| 2435 | return VDAddr; |
| 2436 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2437 | llvm::Type *VarTy = VDAddr.getElementType(); |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 2438 | llvm::Value *Args[] = {emitUpdateLocation(CGF, Loc), getThreadID(CGF, Loc), |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2439 | CGF.Builder.CreatePointerCast(VDAddr.getPointer(), |
| 2440 | CGM.Int8PtrTy), |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2441 | CGM.getSize(CGM.GetTargetTypeStoreSize(VarTy)), |
| 2442 | getOrCreateThreadPrivateCache(VD)}; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2443 | return Address(CGF.EmitRuntimeCall( |
| 2444 | createRuntimeFunction(OMPRTL__kmpc_threadprivate_cached), Args), |
| 2445 | VDAddr.getAlignment()); |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2446 | } |
| 2447 | |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 2448 | void CGOpenMPRuntime::emitThreadPrivateVarInit( |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2449 | CodeGenFunction &CGF, Address VDAddr, llvm::Value *Ctor, |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2450 | llvm::Value *CopyCtor, llvm::Value *Dtor, SourceLocation Loc) { |
| 2451 | // Call kmp_int32 __kmpc_global_thread_num(&loc) to init OpenMP runtime |
| 2452 | // library. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2453 | llvm::Value *OMPLoc = emitUpdateLocation(CGF, Loc); |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 2454 | CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_global_thread_num), |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2455 | OMPLoc); |
| 2456 | // Call __kmpc_threadprivate_register(&loc, &var, ctor, cctor/*NULL*/, dtor) |
| 2457 | // to register constructor/destructor for variable. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2458 | llvm::Value *Args[] = { |
| 2459 | OMPLoc, CGF.Builder.CreatePointerCast(VDAddr.getPointer(), CGM.VoidPtrTy), |
| 2460 | Ctor, CopyCtor, Dtor}; |
Alexey Bataev | 1e4b713 | 2014-12-03 12:11:24 +0000 | [diff] [blame] | 2461 | CGF.EmitRuntimeCall( |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 2462 | createRuntimeFunction(OMPRTL__kmpc_threadprivate_register), Args); |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2463 | } |
| 2464 | |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 2465 | llvm::Function *CGOpenMPRuntime::emitThreadPrivateVarDefinition( |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2466 | const VarDecl *VD, Address VDAddr, SourceLocation Loc, |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2467 | bool PerformInit, CodeGenFunction *CGF) { |
Samuel Antao | f8b5012 | 2015-07-13 22:54:53 +0000 | [diff] [blame] | 2468 | if (CGM.getLangOpts().OpenMPUseTLS && |
| 2469 | CGM.getContext().getTargetInfo().isTLSSupported()) |
| 2470 | return nullptr; |
| 2471 | |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2472 | VD = VD->getDefinition(CGM.getContext()); |
| 2473 | if (VD && ThreadPrivateWithDefinition.count(VD) == 0) { |
| 2474 | ThreadPrivateWithDefinition.insert(VD); |
| 2475 | QualType ASTTy = VD->getType(); |
| 2476 | |
| 2477 | llvm::Value *Ctor = nullptr, *CopyCtor = nullptr, *Dtor = nullptr; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2478 | const Expr *Init = VD->getAnyInitializer(); |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2479 | if (CGM.getLangOpts().CPlusPlus && PerformInit) { |
| 2480 | // Generate function that re-emits the declaration's initializer into the |
| 2481 | // threadprivate copy of the variable VD |
| 2482 | CodeGenFunction CtorCGF(CGM); |
| 2483 | FunctionArgList Args; |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 2484 | ImplicitParamDecl Dst(CGM.getContext(), /*DC=*/nullptr, Loc, |
| 2485 | /*Id=*/nullptr, CGM.getContext().VoidPtrTy, |
Alexey Bataev | 5622323 | 2017-06-09 13:40:18 +0000 | [diff] [blame] | 2486 | ImplicitParamDecl::Other); |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2487 | Args.push_back(&Dst); |
| 2488 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2489 | const auto &FI = CGM.getTypes().arrangeBuiltinFunctionDeclaration( |
John McCall | c56a8b3 | 2016-03-11 04:30:31 +0000 | [diff] [blame] | 2490 | CGM.getContext().VoidPtrTy, Args); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2491 | llvm::FunctionType *FTy = CGM.getTypes().GetFunctionType(FI); |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 2492 | std::string Name = getName({"__kmpc_global_ctor_", ""}); |
| 2493 | llvm::Function *Fn = |
| 2494 | CGM.CreateGlobalInitOrDestructFunction(FTy, Name, FI, Loc); |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2495 | CtorCGF.StartFunction(GlobalDecl(), CGM.getContext().VoidPtrTy, Fn, FI, |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 2496 | Args, Loc, Loc); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2497 | llvm::Value *ArgVal = CtorCGF.EmitLoadOfScalar( |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2498 | CtorCGF.GetAddrOfLocalVar(&Dst), /*Volatile=*/false, |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2499 | CGM.getContext().VoidPtrTy, Dst.getLocation()); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2500 | Address Arg = Address(ArgVal, VDAddr.getAlignment()); |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 2501 | Arg = CtorCGF.Builder.CreateElementBitCast( |
| 2502 | Arg, CtorCGF.ConvertTypeForMem(ASTTy)); |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2503 | CtorCGF.EmitAnyExprToMem(Init, Arg, Init->getType().getQualifiers(), |
| 2504 | /*IsInitializer=*/true); |
| 2505 | ArgVal = CtorCGF.EmitLoadOfScalar( |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2506 | CtorCGF.GetAddrOfLocalVar(&Dst), /*Volatile=*/false, |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2507 | CGM.getContext().VoidPtrTy, Dst.getLocation()); |
| 2508 | CtorCGF.Builder.CreateStore(ArgVal, CtorCGF.ReturnValue); |
| 2509 | CtorCGF.FinishFunction(); |
| 2510 | Ctor = Fn; |
| 2511 | } |
| 2512 | if (VD->getType().isDestructedType() != QualType::DK_none) { |
| 2513 | // Generate function that emits destructor call for the threadprivate copy |
| 2514 | // of the variable VD |
| 2515 | CodeGenFunction DtorCGF(CGM); |
| 2516 | FunctionArgList Args; |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 2517 | ImplicitParamDecl Dst(CGM.getContext(), /*DC=*/nullptr, Loc, |
| 2518 | /*Id=*/nullptr, CGM.getContext().VoidPtrTy, |
Alexey Bataev | 5622323 | 2017-06-09 13:40:18 +0000 | [diff] [blame] | 2519 | ImplicitParamDecl::Other); |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2520 | Args.push_back(&Dst); |
| 2521 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2522 | const auto &FI = CGM.getTypes().arrangeBuiltinFunctionDeclaration( |
John McCall | c56a8b3 | 2016-03-11 04:30:31 +0000 | [diff] [blame] | 2523 | CGM.getContext().VoidTy, Args); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2524 | llvm::FunctionType *FTy = CGM.getTypes().GetFunctionType(FI); |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 2525 | std::string Name = getName({"__kmpc_global_dtor_", ""}); |
| 2526 | llvm::Function *Fn = |
| 2527 | CGM.CreateGlobalInitOrDestructFunction(FTy, Name, FI, Loc); |
Adrian Prantl | 1858c66 | 2016-04-24 22:22:29 +0000 | [diff] [blame] | 2528 | auto NL = ApplyDebugLocation::CreateEmpty(DtorCGF); |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2529 | DtorCGF.StartFunction(GlobalDecl(), CGM.getContext().VoidTy, Fn, FI, Args, |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 2530 | Loc, Loc); |
Adrian Prantl | 1858c66 | 2016-04-24 22:22:29 +0000 | [diff] [blame] | 2531 | // Create a scope with an artificial location for the body of this function. |
| 2532 | auto AL = ApplyDebugLocation::CreateArtificial(DtorCGF); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2533 | llvm::Value *ArgVal = DtorCGF.EmitLoadOfScalar( |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2534 | DtorCGF.GetAddrOfLocalVar(&Dst), |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2535 | /*Volatile=*/false, CGM.getContext().VoidPtrTy, Dst.getLocation()); |
| 2536 | DtorCGF.emitDestroy(Address(ArgVal, VDAddr.getAlignment()), ASTTy, |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2537 | DtorCGF.getDestroyer(ASTTy.isDestructedType()), |
| 2538 | DtorCGF.needsEHCleanup(ASTTy.isDestructedType())); |
| 2539 | DtorCGF.FinishFunction(); |
| 2540 | Dtor = Fn; |
| 2541 | } |
| 2542 | // Do not emit init function if it is not required. |
| 2543 | if (!Ctor && !Dtor) |
| 2544 | return nullptr; |
| 2545 | |
| 2546 | llvm::Type *CopyCtorTyArgs[] = {CGM.VoidPtrTy, CGM.VoidPtrTy}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2547 | auto *CopyCtorTy = llvm::FunctionType::get(CGM.VoidPtrTy, CopyCtorTyArgs, |
| 2548 | /*isVarArg=*/false) |
| 2549 | ->getPointerTo(); |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2550 | // Copying constructor for the threadprivate variable. |
| 2551 | // Must be NULL - reserved by runtime, but currently it requires that this |
| 2552 | // parameter is always NULL. Otherwise it fires assertion. |
| 2553 | CopyCtor = llvm::Constant::getNullValue(CopyCtorTy); |
| 2554 | if (Ctor == nullptr) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2555 | auto *CtorTy = llvm::FunctionType::get(CGM.VoidPtrTy, CGM.VoidPtrTy, |
| 2556 | /*isVarArg=*/false) |
| 2557 | ->getPointerTo(); |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2558 | Ctor = llvm::Constant::getNullValue(CtorTy); |
| 2559 | } |
| 2560 | if (Dtor == nullptr) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2561 | auto *DtorTy = llvm::FunctionType::get(CGM.VoidTy, CGM.VoidPtrTy, |
| 2562 | /*isVarArg=*/false) |
| 2563 | ->getPointerTo(); |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2564 | Dtor = llvm::Constant::getNullValue(DtorTy); |
| 2565 | } |
| 2566 | if (!CGF) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2567 | auto *InitFunctionTy = |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2568 | llvm::FunctionType::get(CGM.VoidTy, /*isVarArg*/ false); |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 2569 | std::string Name = getName({"__omp_threadprivate_init_", ""}); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2570 | llvm::Function *InitFunction = CGM.CreateGlobalInitOrDestructFunction( |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 2571 | InitFunctionTy, Name, CGM.getTypes().arrangeNullaryFunction()); |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2572 | CodeGenFunction InitCGF(CGM); |
| 2573 | FunctionArgList ArgList; |
| 2574 | InitCGF.StartFunction(GlobalDecl(), CGM.getContext().VoidTy, InitFunction, |
| 2575 | CGM.getTypes().arrangeNullaryFunction(), ArgList, |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 2576 | Loc, Loc); |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 2577 | emitThreadPrivateVarInit(InitCGF, VDAddr, Ctor, CopyCtor, Dtor, Loc); |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2578 | InitCGF.FinishFunction(); |
| 2579 | return InitFunction; |
| 2580 | } |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 2581 | emitThreadPrivateVarInit(*CGF, VDAddr, Ctor, CopyCtor, Dtor, Loc); |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2582 | } |
| 2583 | return nullptr; |
| 2584 | } |
| 2585 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2586 | /// Obtain information that uniquely identifies a target entry. This |
Alexey Bataev | 34f8a70 | 2018-03-28 14:28:54 +0000 | [diff] [blame] | 2587 | /// consists of the file and device IDs as well as line number associated with |
| 2588 | /// the relevant entry source location. |
| 2589 | static void getTargetEntryUniqueInfo(ASTContext &C, SourceLocation Loc, |
| 2590 | unsigned &DeviceID, unsigned &FileID, |
| 2591 | unsigned &LineNum) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2592 | SourceManager &SM = C.getSourceManager(); |
Alexey Bataev | 34f8a70 | 2018-03-28 14:28:54 +0000 | [diff] [blame] | 2593 | |
| 2594 | // The loc should be always valid and have a file ID (the user cannot use |
| 2595 | // #pragma directives in macros) |
| 2596 | |
| 2597 | assert(Loc.isValid() && "Source location is expected to be always valid."); |
Alexey Bataev | 34f8a70 | 2018-03-28 14:28:54 +0000 | [diff] [blame] | 2598 | |
| 2599 | PresumedLoc PLoc = SM.getPresumedLoc(Loc); |
| 2600 | assert(PLoc.isValid() && "Source location is expected to be always valid."); |
| 2601 | |
| 2602 | llvm::sys::fs::UniqueID ID; |
Alexey Bataev | 64e62dc | 2018-04-30 16:26:57 +0000 | [diff] [blame] | 2603 | if (auto EC = llvm::sys::fs::getUniqueID(PLoc.getFilename(), ID)) |
| 2604 | SM.getDiagnostics().Report(diag::err_cannot_open_file) |
| 2605 | << PLoc.getFilename() << EC.message(); |
Alexey Bataev | 34f8a70 | 2018-03-28 14:28:54 +0000 | [diff] [blame] | 2606 | |
| 2607 | DeviceID = ID.getDevice(); |
| 2608 | FileID = ID.getFile(); |
| 2609 | LineNum = PLoc.getLine(); |
| 2610 | } |
| 2611 | |
| 2612 | bool CGOpenMPRuntime::emitDeclareTargetVarDefinition(const VarDecl *VD, |
| 2613 | llvm::GlobalVariable *Addr, |
| 2614 | bool PerformInit) { |
| 2615 | Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res = |
Alexey Bataev | 97b7221 | 2018-08-14 18:31:20 +0000 | [diff] [blame] | 2616 | OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD); |
Alexey Bataev | 34f8a70 | 2018-03-28 14:28:54 +0000 | [diff] [blame] | 2617 | if (!Res || *Res == OMPDeclareTargetDeclAttr::MT_Link) |
Alexey Bataev | d01b749 | 2018-08-15 19:45:12 +0000 | [diff] [blame] | 2618 | return CGM.getLangOpts().OpenMPIsDevice; |
Alexey Bataev | 34f8a70 | 2018-03-28 14:28:54 +0000 | [diff] [blame] | 2619 | VD = VD->getDefinition(CGM.getContext()); |
| 2620 | if (VD && !DeclareTargetWithDefinition.insert(VD).second) |
| 2621 | return CGM.getLangOpts().OpenMPIsDevice; |
| 2622 | |
| 2623 | QualType ASTTy = VD->getType(); |
| 2624 | |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2625 | SourceLocation Loc = VD->getCanonicalDecl()->getBeginLoc(); |
Alexey Bataev | 34f8a70 | 2018-03-28 14:28:54 +0000 | [diff] [blame] | 2626 | // Produce the unique prefix to identify the new target regions. We use |
| 2627 | // the source location of the variable declaration which we know to not |
| 2628 | // conflict with any target region. |
| 2629 | unsigned DeviceID; |
| 2630 | unsigned FileID; |
| 2631 | unsigned Line; |
| 2632 | getTargetEntryUniqueInfo(CGM.getContext(), Loc, DeviceID, FileID, Line); |
| 2633 | SmallString<128> Buffer, Out; |
| 2634 | { |
| 2635 | llvm::raw_svector_ostream OS(Buffer); |
| 2636 | OS << "__omp_offloading_" << llvm::format("_%x", DeviceID) |
| 2637 | << llvm::format("_%x_", FileID) << VD->getName() << "_l" << Line; |
| 2638 | } |
| 2639 | |
| 2640 | const Expr *Init = VD->getAnyInitializer(); |
| 2641 | if (CGM.getLangOpts().CPlusPlus && PerformInit) { |
| 2642 | llvm::Constant *Ctor; |
| 2643 | llvm::Constant *ID; |
| 2644 | if (CGM.getLangOpts().OpenMPIsDevice) { |
| 2645 | // Generate function that re-emits the declaration's initializer into |
| 2646 | // the threadprivate copy of the variable VD |
| 2647 | CodeGenFunction CtorCGF(CGM); |
| 2648 | |
| 2649 | const CGFunctionInfo &FI = CGM.getTypes().arrangeNullaryFunction(); |
| 2650 | llvm::FunctionType *FTy = CGM.getTypes().GetFunctionType(FI); |
| 2651 | llvm::Function *Fn = CGM.CreateGlobalInitOrDestructFunction( |
| 2652 | FTy, Twine(Buffer, "_ctor"), FI, Loc); |
| 2653 | auto NL = ApplyDebugLocation::CreateEmpty(CtorCGF); |
| 2654 | CtorCGF.StartFunction(GlobalDecl(), CGM.getContext().VoidTy, Fn, FI, |
| 2655 | FunctionArgList(), Loc, Loc); |
| 2656 | auto AL = ApplyDebugLocation::CreateArtificial(CtorCGF); |
| 2657 | CtorCGF.EmitAnyExprToMem(Init, |
| 2658 | Address(Addr, CGM.getContext().getDeclAlign(VD)), |
| 2659 | Init->getType().getQualifiers(), |
| 2660 | /*IsInitializer=*/true); |
| 2661 | CtorCGF.FinishFunction(); |
| 2662 | Ctor = Fn; |
| 2663 | ID = llvm::ConstantExpr::getBitCast(Fn, CGM.Int8PtrTy); |
Alexey Bataev | e253f2f | 2018-05-09 14:15:18 +0000 | [diff] [blame] | 2664 | CGM.addUsedGlobal(cast<llvm::GlobalValue>(Ctor)); |
Alexey Bataev | 34f8a70 | 2018-03-28 14:28:54 +0000 | [diff] [blame] | 2665 | } else { |
| 2666 | Ctor = new llvm::GlobalVariable( |
| 2667 | CGM.getModule(), CGM.Int8Ty, /*isConstant=*/true, |
| 2668 | llvm::GlobalValue::PrivateLinkage, |
| 2669 | llvm::Constant::getNullValue(CGM.Int8Ty), Twine(Buffer, "_ctor")); |
| 2670 | ID = Ctor; |
| 2671 | } |
| 2672 | |
| 2673 | // Register the information for the entry associated with the constructor. |
| 2674 | Out.clear(); |
| 2675 | OffloadEntriesInfoManager.registerTargetRegionEntryInfo( |
| 2676 | DeviceID, FileID, Twine(Buffer, "_ctor").toStringRef(Out), Line, Ctor, |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 2677 | ID, OffloadEntriesInfoManagerTy::OMPTargetRegionEntryCtor); |
Alexey Bataev | 34f8a70 | 2018-03-28 14:28:54 +0000 | [diff] [blame] | 2678 | } |
| 2679 | if (VD->getType().isDestructedType() != QualType::DK_none) { |
| 2680 | llvm::Constant *Dtor; |
| 2681 | llvm::Constant *ID; |
| 2682 | if (CGM.getLangOpts().OpenMPIsDevice) { |
| 2683 | // Generate function that emits destructor call for the threadprivate |
| 2684 | // copy of the variable VD |
| 2685 | CodeGenFunction DtorCGF(CGM); |
| 2686 | |
| 2687 | const CGFunctionInfo &FI = CGM.getTypes().arrangeNullaryFunction(); |
| 2688 | llvm::FunctionType *FTy = CGM.getTypes().GetFunctionType(FI); |
| 2689 | llvm::Function *Fn = CGM.CreateGlobalInitOrDestructFunction( |
| 2690 | FTy, Twine(Buffer, "_dtor"), FI, Loc); |
| 2691 | auto NL = ApplyDebugLocation::CreateEmpty(DtorCGF); |
| 2692 | DtorCGF.StartFunction(GlobalDecl(), CGM.getContext().VoidTy, Fn, FI, |
| 2693 | FunctionArgList(), Loc, Loc); |
| 2694 | // Create a scope with an artificial location for the body of this |
| 2695 | // function. |
| 2696 | auto AL = ApplyDebugLocation::CreateArtificial(DtorCGF); |
| 2697 | DtorCGF.emitDestroy(Address(Addr, CGM.getContext().getDeclAlign(VD)), |
| 2698 | ASTTy, DtorCGF.getDestroyer(ASTTy.isDestructedType()), |
| 2699 | DtorCGF.needsEHCleanup(ASTTy.isDestructedType())); |
| 2700 | DtorCGF.FinishFunction(); |
| 2701 | Dtor = Fn; |
| 2702 | ID = llvm::ConstantExpr::getBitCast(Fn, CGM.Int8PtrTy); |
Alexey Bataev | e253f2f | 2018-05-09 14:15:18 +0000 | [diff] [blame] | 2703 | CGM.addUsedGlobal(cast<llvm::GlobalValue>(Dtor)); |
Alexey Bataev | 34f8a70 | 2018-03-28 14:28:54 +0000 | [diff] [blame] | 2704 | } else { |
| 2705 | Dtor = new llvm::GlobalVariable( |
| 2706 | CGM.getModule(), CGM.Int8Ty, /*isConstant=*/true, |
| 2707 | llvm::GlobalValue::PrivateLinkage, |
| 2708 | llvm::Constant::getNullValue(CGM.Int8Ty), Twine(Buffer, "_dtor")); |
| 2709 | ID = Dtor; |
| 2710 | } |
| 2711 | // Register the information for the entry associated with the destructor. |
| 2712 | Out.clear(); |
| 2713 | OffloadEntriesInfoManager.registerTargetRegionEntryInfo( |
| 2714 | DeviceID, FileID, Twine(Buffer, "_dtor").toStringRef(Out), Line, Dtor, |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 2715 | ID, OffloadEntriesInfoManagerTy::OMPTargetRegionEntryDtor); |
Alexey Bataev | 34f8a70 | 2018-03-28 14:28:54 +0000 | [diff] [blame] | 2716 | } |
| 2717 | return CGM.getLangOpts().OpenMPIsDevice; |
| 2718 | } |
| 2719 | |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 2720 | Address CGOpenMPRuntime::getAddrOfArtificialThreadPrivate(CodeGenFunction &CGF, |
| 2721 | QualType VarType, |
| 2722 | StringRef Name) { |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 2723 | std::string Suffix = getName({"artificial", ""}); |
| 2724 | std::string CacheSuffix = getName({"cache", ""}); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 2725 | llvm::Type *VarLVType = CGF.ConvertTypeForMem(VarType); |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 2726 | llvm::Value *GAddr = |
| 2727 | getOrCreateInternalVariable(VarLVType, Twine(Name).concat(Suffix)); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 2728 | llvm::Value *Args[] = { |
| 2729 | emitUpdateLocation(CGF, SourceLocation()), |
| 2730 | getThreadID(CGF, SourceLocation()), |
| 2731 | CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(GAddr, CGM.VoidPtrTy), |
| 2732 | CGF.Builder.CreateIntCast(CGF.getTypeSize(VarType), CGM.SizeTy, |
| 2733 | /*IsSigned=*/false), |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 2734 | getOrCreateInternalVariable( |
| 2735 | CGM.VoidPtrPtrTy, Twine(Name).concat(Suffix).concat(CacheSuffix))}; |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 2736 | return Address( |
| 2737 | CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
| 2738 | CGF.EmitRuntimeCall( |
| 2739 | createRuntimeFunction(OMPRTL__kmpc_threadprivate_cached), Args), |
| 2740 | VarLVType->getPointerTo(/*AddrSpace=*/0)), |
| 2741 | CGM.getPointerAlign()); |
| 2742 | } |
| 2743 | |
Arpith Chacko Jacob | bb36fe8 | 2017-01-10 15:42:51 +0000 | [diff] [blame] | 2744 | void CGOpenMPRuntime::emitOMPIfClause(CodeGenFunction &CGF, const Expr *Cond, |
| 2745 | const RegionCodeGenTy &ThenGen, |
| 2746 | const RegionCodeGenTy &ElseGen) { |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 2747 | CodeGenFunction::LexicalScope ConditionScope(CGF, Cond->getSourceRange()); |
| 2748 | |
| 2749 | // If the condition constant folds and can be elided, try to avoid emitting |
| 2750 | // the condition and the dead arm of the if/else. |
| 2751 | bool CondConstant; |
| 2752 | if (CGF.ConstantFoldsToSimpleInteger(Cond, CondConstant)) { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2753 | if (CondConstant) |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 2754 | ThenGen(CGF); |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2755 | else |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 2756 | ElseGen(CGF); |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 2757 | return; |
| 2758 | } |
| 2759 | |
| 2760 | // Otherwise, the condition did not fold, or we couldn't elide it. Just |
| 2761 | // emit the conditional branch. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2762 | llvm::BasicBlock *ThenBlock = CGF.createBasicBlock("omp_if.then"); |
| 2763 | llvm::BasicBlock *ElseBlock = CGF.createBasicBlock("omp_if.else"); |
| 2764 | llvm::BasicBlock *ContBlock = CGF.createBasicBlock("omp_if.end"); |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 2765 | CGF.EmitBranchOnBoolExpr(Cond, ThenBlock, ElseBlock, /*TrueCount=*/0); |
| 2766 | |
| 2767 | // Emit the 'then' code. |
| 2768 | CGF.EmitBlock(ThenBlock); |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2769 | ThenGen(CGF); |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 2770 | CGF.EmitBranch(ContBlock); |
| 2771 | // Emit the 'else' code if present. |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2772 | // There is no need to emit line number for unconditional branch. |
| 2773 | (void)ApplyDebugLocation::CreateEmpty(CGF); |
| 2774 | CGF.EmitBlock(ElseBlock); |
| 2775 | ElseGen(CGF); |
| 2776 | // There is no need to emit line number for unconditional branch. |
| 2777 | (void)ApplyDebugLocation::CreateEmpty(CGF); |
| 2778 | CGF.EmitBranch(ContBlock); |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 2779 | // Emit the continuation block for code after the if. |
| 2780 | CGF.EmitBlock(ContBlock, /*IsFinished=*/true); |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 2781 | } |
| 2782 | |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 2783 | void CGOpenMPRuntime::emitParallelCall(CodeGenFunction &CGF, SourceLocation Loc, |
| 2784 | llvm::Value *OutlinedFn, |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 2785 | ArrayRef<llvm::Value *> CapturedVars, |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 2786 | const Expr *IfCond) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 2787 | if (!CGF.HaveInsertPoint()) |
| 2788 | return; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2789 | llvm::Value *RTLoc = emitUpdateLocation(CGF, Loc); |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2790 | auto &&ThenGen = [OutlinedFn, CapturedVars, RTLoc](CodeGenFunction &CGF, |
| 2791 | PrePostActionTy &) { |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 2792 | // Build call __kmpc_fork_call(loc, n, microtask, var1, .., varn); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2793 | CGOpenMPRuntime &RT = CGF.CGM.getOpenMPRuntime(); |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 2794 | llvm::Value *Args[] = { |
| 2795 | RTLoc, |
| 2796 | CGF.Builder.getInt32(CapturedVars.size()), // Number of captured vars |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2797 | CGF.Builder.CreateBitCast(OutlinedFn, RT.getKmpc_MicroPointerTy())}; |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 2798 | llvm::SmallVector<llvm::Value *, 16> RealArgs; |
| 2799 | RealArgs.append(std::begin(Args), std::end(Args)); |
| 2800 | RealArgs.append(CapturedVars.begin(), CapturedVars.end()); |
| 2801 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2802 | llvm::Value *RTLFn = RT.createRuntimeFunction(OMPRTL__kmpc_fork_call); |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 2803 | CGF.EmitRuntimeCall(RTLFn, RealArgs); |
| 2804 | }; |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2805 | auto &&ElseGen = [OutlinedFn, CapturedVars, RTLoc, Loc](CodeGenFunction &CGF, |
| 2806 | PrePostActionTy &) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2807 | CGOpenMPRuntime &RT = CGF.CGM.getOpenMPRuntime(); |
| 2808 | llvm::Value *ThreadID = RT.getThreadID(CGF, Loc); |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 2809 | // Build calls: |
| 2810 | // __kmpc_serialized_parallel(&Loc, GTid); |
| 2811 | llvm::Value *Args[] = {RTLoc, ThreadID}; |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2812 | CGF.EmitRuntimeCall( |
| 2813 | RT.createRuntimeFunction(OMPRTL__kmpc_serialized_parallel), Args); |
Alexey Bataev | d74d060 | 2014-10-13 06:02:40 +0000 | [diff] [blame] | 2814 | |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 2815 | // OutlinedFn(>id, &zero, CapturedStruct); |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 2816 | Address ZeroAddr = CGF.CreateDefaultAlignTempAlloca(CGF.Int32Ty, |
| 2817 | /*Name*/ ".zero.addr"); |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 2818 | CGF.InitTempAlloca(ZeroAddr, CGF.Builder.getInt32(/*C*/ 0)); |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 2819 | llvm::SmallVector<llvm::Value *, 16> OutlinedFnArgs; |
Alexey Bataev | 8521ff6 | 2018-07-25 20:03:01 +0000 | [diff] [blame] | 2820 | // ThreadId for serialized parallels is 0. |
| 2821 | OutlinedFnArgs.push_back(ZeroAddr.getPointer()); |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 2822 | OutlinedFnArgs.push_back(ZeroAddr.getPointer()); |
| 2823 | OutlinedFnArgs.append(CapturedVars.begin(), CapturedVars.end()); |
Alexey Bataev | 3c595a6 | 2017-08-14 15:01:03 +0000 | [diff] [blame] | 2824 | RT.emitOutlinedFunctionCall(CGF, Loc, OutlinedFn, OutlinedFnArgs); |
Alexey Bataev | d74d060 | 2014-10-13 06:02:40 +0000 | [diff] [blame] | 2825 | |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 2826 | // __kmpc_end_serialized_parallel(&Loc, GTid); |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2827 | llvm::Value *EndArgs[] = {RT.emitUpdateLocation(CGF, Loc), ThreadID}; |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 2828 | CGF.EmitRuntimeCall( |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2829 | RT.createRuntimeFunction(OMPRTL__kmpc_end_serialized_parallel), |
| 2830 | EndArgs); |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 2831 | }; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2832 | if (IfCond) { |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 2833 | emitOMPIfClause(CGF, IfCond, ThenGen, ElseGen); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2834 | } else { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2835 | RegionCodeGenTy ThenRCG(ThenGen); |
| 2836 | ThenRCG(CGF); |
Alexey Bataev | f539faa | 2016-03-28 12:58:34 +0000 | [diff] [blame] | 2837 | } |
Alexey Bataev | d74d060 | 2014-10-13 06:02:40 +0000 | [diff] [blame] | 2838 | } |
| 2839 | |
NAKAMURA Takumi | 59c74b22 | 2014-10-27 08:08:18 +0000 | [diff] [blame] | 2840 | // 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] | 2841 | // thread-ID variable (it is passed in a first argument of the outlined function |
| 2842 | // as "kmp_int32 *gtid"). Otherwise, if we're not inside parallel region, but in |
| 2843 | // regular serial code region, get thread ID by calling kmp_int32 |
| 2844 | // kmpc_global_thread_num(ident_t *loc), stash this thread ID in a temporary and |
| 2845 | // return the address of that temp. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2846 | Address CGOpenMPRuntime::emitThreadIDAddress(CodeGenFunction &CGF, |
| 2847 | SourceLocation Loc) { |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 2848 | if (auto *OMPRegionInfo = |
Alexey Bataev | d74d060 | 2014-10-13 06:02:40 +0000 | [diff] [blame] | 2849 | dyn_cast_or_null<CGOpenMPRegionInfo>(CGF.CapturedStmtInfo)) |
Alexey Bataev | 8cbe0a6 | 2015-02-26 10:27:34 +0000 | [diff] [blame] | 2850 | if (OMPRegionInfo->getThreadIDVariable()) |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 2851 | return OMPRegionInfo->getThreadIDVariableLValue(CGF).getAddress(); |
Alexey Bataev | 8cbe0a6 | 2015-02-26 10:27:34 +0000 | [diff] [blame] | 2852 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2853 | llvm::Value *ThreadID = getThreadID(CGF, Loc); |
| 2854 | QualType Int32Ty = |
Alexey Bataev | d74d060 | 2014-10-13 06:02:40 +0000 | [diff] [blame] | 2855 | CGF.getContext().getIntTypeForBitwidth(/*DestWidth*/ 32, /*Signed*/ true); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2856 | Address ThreadIDTemp = CGF.CreateMemTemp(Int32Ty, /*Name*/ ".threadid_temp."); |
Alexey Bataev | d74d060 | 2014-10-13 06:02:40 +0000 | [diff] [blame] | 2857 | CGF.EmitStoreOfScalar(ThreadID, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2858 | CGF.MakeAddrLValue(ThreadIDTemp, Int32Ty)); |
Alexey Bataev | d74d060 | 2014-10-13 06:02:40 +0000 | [diff] [blame] | 2859 | |
| 2860 | return ThreadIDTemp; |
| 2861 | } |
| 2862 | |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2863 | llvm::Constant * |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 2864 | CGOpenMPRuntime::getOrCreateInternalVariable(llvm::Type *Ty, |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2865 | const llvm::Twine &Name) { |
Alexey Bataev | 3a3bf0b | 2014-09-22 10:01:53 +0000 | [diff] [blame] | 2866 | SmallString<256> Buffer; |
| 2867 | llvm::raw_svector_ostream Out(Buffer); |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2868 | Out << Name; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2869 | StringRef RuntimeName = Out.str(); |
Alexey Bataev | 43a919f | 2018-04-13 17:48:43 +0000 | [diff] [blame] | 2870 | auto &Elem = *InternalVars.try_emplace(RuntimeName, nullptr).first; |
David Blaikie | 13156b6 | 2014-11-19 03:06:06 +0000 | [diff] [blame] | 2871 | if (Elem.second) { |
| 2872 | assert(Elem.second->getType()->getPointerElementType() == Ty && |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2873 | "OMP internal variable has different type than requested"); |
David Blaikie | 13156b6 | 2014-11-19 03:06:06 +0000 | [diff] [blame] | 2874 | return &*Elem.second; |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2875 | } |
Alexey Bataev | 3a3bf0b | 2014-09-22 10:01:53 +0000 | [diff] [blame] | 2876 | |
David Blaikie | 13156b6 | 2014-11-19 03:06:06 +0000 | [diff] [blame] | 2877 | return Elem.second = new llvm::GlobalVariable( |
| 2878 | CGM.getModule(), Ty, /*IsConstant*/ false, |
| 2879 | llvm::GlobalValue::CommonLinkage, llvm::Constant::getNullValue(Ty), |
| 2880 | Elem.first()); |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2881 | } |
| 2882 | |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 2883 | llvm::Value *CGOpenMPRuntime::getCriticalRegionLock(StringRef CriticalName) { |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 2884 | std::string Prefix = Twine("gomp_critical_user_", CriticalName).str(); |
| 2885 | std::string Name = getName({Prefix, "var"}); |
| 2886 | return getOrCreateInternalVariable(KmpCriticalNameTy, Name); |
Alexey Bataev | 3a3bf0b | 2014-09-22 10:01:53 +0000 | [diff] [blame] | 2887 | } |
| 2888 | |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 2889 | namespace { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2890 | /// Common pre(post)-action for different OpenMP constructs. |
| 2891 | class CommonActionTy final : public PrePostActionTy { |
| 2892 | llvm::Value *EnterCallee; |
| 2893 | ArrayRef<llvm::Value *> EnterArgs; |
| 2894 | llvm::Value *ExitCallee; |
| 2895 | ArrayRef<llvm::Value *> ExitArgs; |
| 2896 | bool Conditional; |
| 2897 | llvm::BasicBlock *ContBlock = nullptr; |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 2898 | |
| 2899 | public: |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2900 | CommonActionTy(llvm::Value *EnterCallee, ArrayRef<llvm::Value *> EnterArgs, |
| 2901 | llvm::Value *ExitCallee, ArrayRef<llvm::Value *> ExitArgs, |
| 2902 | bool Conditional = false) |
| 2903 | : EnterCallee(EnterCallee), EnterArgs(EnterArgs), ExitCallee(ExitCallee), |
| 2904 | ExitArgs(ExitArgs), Conditional(Conditional) {} |
| 2905 | void Enter(CodeGenFunction &CGF) override { |
| 2906 | llvm::Value *EnterRes = CGF.EmitRuntimeCall(EnterCallee, EnterArgs); |
| 2907 | if (Conditional) { |
| 2908 | llvm::Value *CallBool = CGF.Builder.CreateIsNotNull(EnterRes); |
| 2909 | auto *ThenBlock = CGF.createBasicBlock("omp_if.then"); |
| 2910 | ContBlock = CGF.createBasicBlock("omp_if.end"); |
| 2911 | // Generate the branch (If-stmt) |
| 2912 | CGF.Builder.CreateCondBr(CallBool, ThenBlock, ContBlock); |
| 2913 | CGF.EmitBlock(ThenBlock); |
| 2914 | } |
Alexey Bataev | a744ff5 | 2015-05-05 09:24:37 +0000 | [diff] [blame] | 2915 | } |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2916 | void Done(CodeGenFunction &CGF) { |
| 2917 | // Emit the rest of blocks/branches |
| 2918 | CGF.EmitBranch(ContBlock); |
| 2919 | CGF.EmitBlock(ContBlock, true); |
| 2920 | } |
| 2921 | void Exit(CodeGenFunction &CGF) override { |
| 2922 | CGF.EmitRuntimeCall(ExitCallee, ExitArgs); |
Alexey Bataev | 3e6124b | 2015-04-10 07:48:12 +0000 | [diff] [blame] | 2923 | } |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 2924 | }; |
Hans Wennborg | 7eb5464 | 2015-09-10 17:07:54 +0000 | [diff] [blame] | 2925 | } // anonymous namespace |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 2926 | |
| 2927 | void CGOpenMPRuntime::emitCriticalRegion(CodeGenFunction &CGF, |
| 2928 | StringRef CriticalName, |
| 2929 | const RegionCodeGenTy &CriticalOpGen, |
Alexey Bataev | fc57d16 | 2015-12-15 10:55:09 +0000 | [diff] [blame] | 2930 | SourceLocation Loc, const Expr *Hint) { |
| 2931 | // __kmpc_critical[_with_hint](ident_t *, gtid, Lock[, hint]); |
Alexey Bataev | 75ddfab | 2014-12-01 11:32:38 +0000 | [diff] [blame] | 2932 | // CriticalOpGen(); |
| 2933 | // __kmpc_end_critical(ident_t *, gtid, Lock); |
| 2934 | // Prepare arguments and build a call to __kmpc_critical |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 2935 | if (!CGF.HaveInsertPoint()) |
| 2936 | return; |
Alexey Bataev | fc57d16 | 2015-12-15 10:55:09 +0000 | [diff] [blame] | 2937 | llvm::Value *Args[] = {emitUpdateLocation(CGF, Loc), getThreadID(CGF, Loc), |
| 2938 | getCriticalRegionLock(CriticalName)}; |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2939 | llvm::SmallVector<llvm::Value *, 4> EnterArgs(std::begin(Args), |
| 2940 | std::end(Args)); |
Alexey Bataev | fc57d16 | 2015-12-15 10:55:09 +0000 | [diff] [blame] | 2941 | if (Hint) { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2942 | EnterArgs.push_back(CGF.Builder.CreateIntCast( |
| 2943 | CGF.EmitScalarExpr(Hint), CGM.IntPtrTy, /*isSigned=*/false)); |
| 2944 | } |
| 2945 | CommonActionTy Action( |
| 2946 | createRuntimeFunction(Hint ? OMPRTL__kmpc_critical_with_hint |
| 2947 | : OMPRTL__kmpc_critical), |
| 2948 | EnterArgs, createRuntimeFunction(OMPRTL__kmpc_end_critical), Args); |
| 2949 | CriticalOpGen.setAction(Action); |
Alexey Bataev | fc57d16 | 2015-12-15 10:55:09 +0000 | [diff] [blame] | 2950 | emitInlinedDirective(CGF, OMPD_critical, CriticalOpGen); |
Alexey Bataev | 3a3bf0b | 2014-09-22 10:01:53 +0000 | [diff] [blame] | 2951 | } |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 2952 | |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 2953 | void CGOpenMPRuntime::emitMasterRegion(CodeGenFunction &CGF, |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 2954 | const RegionCodeGenTy &MasterOpGen, |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 2955 | SourceLocation Loc) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 2956 | if (!CGF.HaveInsertPoint()) |
| 2957 | return; |
Alexey Bataev | 8d69065 | 2014-12-04 07:23:53 +0000 | [diff] [blame] | 2958 | // if(__kmpc_master(ident_t *, gtid)) { |
| 2959 | // MasterOpGen(); |
| 2960 | // __kmpc_end_master(ident_t *, gtid); |
| 2961 | // } |
| 2962 | // Prepare arguments and build a call to __kmpc_master |
Alexey Bataev | d7614fb | 2015-04-10 06:33:45 +0000 | [diff] [blame] | 2963 | llvm::Value *Args[] = {emitUpdateLocation(CGF, Loc), getThreadID(CGF, Loc)}; |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2964 | CommonActionTy Action(createRuntimeFunction(OMPRTL__kmpc_master), Args, |
| 2965 | createRuntimeFunction(OMPRTL__kmpc_end_master), Args, |
| 2966 | /*Conditional=*/true); |
| 2967 | MasterOpGen.setAction(Action); |
| 2968 | emitInlinedDirective(CGF, OMPD_master, MasterOpGen); |
| 2969 | Action.Done(CGF); |
Alexey Bataev | 8d69065 | 2014-12-04 07:23:53 +0000 | [diff] [blame] | 2970 | } |
| 2971 | |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 2972 | void CGOpenMPRuntime::emitTaskyieldCall(CodeGenFunction &CGF, |
| 2973 | SourceLocation Loc) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 2974 | if (!CGF.HaveInsertPoint()) |
| 2975 | return; |
Alexey Bataev | 9f797f3 | 2015-02-05 05:57:51 +0000 | [diff] [blame] | 2976 | // Build call __kmpc_omp_taskyield(loc, thread_id, 0); |
| 2977 | llvm::Value *Args[] = { |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 2978 | emitUpdateLocation(CGF, Loc), getThreadID(CGF, Loc), |
Alexey Bataev | 9f797f3 | 2015-02-05 05:57:51 +0000 | [diff] [blame] | 2979 | llvm::ConstantInt::get(CGM.IntTy, /*V=*/0, /*isSigned=*/true)}; |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 2980 | CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_omp_taskyield), Args); |
Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 2981 | if (auto *Region = dyn_cast_or_null<CGOpenMPRegionInfo>(CGF.CapturedStmtInfo)) |
| 2982 | Region->emitUntiedSwitch(CGF); |
Alexey Bataev | 9f797f3 | 2015-02-05 05:57:51 +0000 | [diff] [blame] | 2983 | } |
| 2984 | |
Alexey Bataev | c30dd2d | 2015-06-18 12:14:09 +0000 | [diff] [blame] | 2985 | void CGOpenMPRuntime::emitTaskgroupRegion(CodeGenFunction &CGF, |
| 2986 | const RegionCodeGenTy &TaskgroupOpGen, |
| 2987 | SourceLocation Loc) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 2988 | if (!CGF.HaveInsertPoint()) |
| 2989 | return; |
Alexey Bataev | c30dd2d | 2015-06-18 12:14:09 +0000 | [diff] [blame] | 2990 | // __kmpc_taskgroup(ident_t *, gtid); |
| 2991 | // TaskgroupOpGen(); |
| 2992 | // __kmpc_end_taskgroup(ident_t *, gtid); |
| 2993 | // Prepare arguments and build a call to __kmpc_taskgroup |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2994 | llvm::Value *Args[] = {emitUpdateLocation(CGF, Loc), getThreadID(CGF, Loc)}; |
| 2995 | CommonActionTy Action(createRuntimeFunction(OMPRTL__kmpc_taskgroup), Args, |
| 2996 | createRuntimeFunction(OMPRTL__kmpc_end_taskgroup), |
| 2997 | Args); |
| 2998 | TaskgroupOpGen.setAction(Action); |
| 2999 | emitInlinedDirective(CGF, OMPD_taskgroup, TaskgroupOpGen); |
Alexey Bataev | c30dd2d | 2015-06-18 12:14:09 +0000 | [diff] [blame] | 3000 | } |
| 3001 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3002 | /// Given an array of pointers to variables, project the address of a |
| 3003 | /// given variable. |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 3004 | static Address emitAddrOfVarFromArray(CodeGenFunction &CGF, Address Array, |
| 3005 | unsigned Index, const VarDecl *Var) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3006 | // Pull out the pointer to the variable. |
| 3007 | Address PtrAddr = |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 3008 | CGF.Builder.CreateConstArrayGEP(Array, Index, CGF.getPointerSize()); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3009 | llvm::Value *Ptr = CGF.Builder.CreateLoad(PtrAddr); |
| 3010 | |
| 3011 | Address Addr = Address(Ptr, CGF.getContext().getDeclAlign(Var)); |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 3012 | Addr = CGF.Builder.CreateElementBitCast( |
| 3013 | Addr, CGF.ConvertTypeForMem(Var->getType())); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3014 | return Addr; |
| 3015 | } |
| 3016 | |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3017 | static llvm::Value *emitCopyprivateCopyFunction( |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 3018 | CodeGenModule &CGM, llvm::Type *ArgsType, |
| 3019 | ArrayRef<const Expr *> CopyprivateVars, ArrayRef<const Expr *> DestExprs, |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 3020 | ArrayRef<const Expr *> SrcExprs, ArrayRef<const Expr *> AssignmentOps, |
| 3021 | SourceLocation Loc) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3022 | ASTContext &C = CGM.getContext(); |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3023 | // void copy_func(void *LHSArg, void *RHSArg); |
| 3024 | FunctionArgList Args; |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 3025 | ImplicitParamDecl LHSArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, C.VoidPtrTy, |
| 3026 | ImplicitParamDecl::Other); |
| 3027 | ImplicitParamDecl RHSArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, C.VoidPtrTy, |
| 3028 | ImplicitParamDecl::Other); |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3029 | Args.push_back(&LHSArg); |
| 3030 | Args.push_back(&RHSArg); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3031 | const auto &CGFI = |
| 3032 | CGM.getTypes().arrangeBuiltinFunctionDeclaration(C.VoidTy, Args); |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 3033 | std::string Name = |
| 3034 | CGM.getOpenMPRuntime().getName({"omp", "copyprivate", "copy_func"}); |
| 3035 | auto *Fn = llvm::Function::Create(CGM.getTypes().GetFunctionType(CGFI), |
| 3036 | llvm::GlobalValue::InternalLinkage, Name, |
| 3037 | &CGM.getModule()); |
Rafael Espindola | 51ec5a9 | 2018-02-28 23:46:35 +0000 | [diff] [blame] | 3038 | CGM.SetInternalFunctionAttributes(GlobalDecl(), Fn, CGFI); |
Alexey Bataev | c0f879b | 2018-04-10 20:10:53 +0000 | [diff] [blame] | 3039 | Fn->setDoesNotRecurse(); |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3040 | CodeGenFunction CGF(CGM); |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 3041 | CGF.StartFunction(GlobalDecl(), C.VoidTy, Fn, CGFI, Args, Loc, Loc); |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 3042 | // Dest = (void*[n])(LHSArg); |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3043 | // Src = (void*[n])(RHSArg); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3044 | Address LHS(CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
| 3045 | CGF.Builder.CreateLoad(CGF.GetAddrOfLocalVar(&LHSArg)), |
| 3046 | ArgsType), CGF.getPointerAlign()); |
| 3047 | Address RHS(CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
| 3048 | CGF.Builder.CreateLoad(CGF.GetAddrOfLocalVar(&RHSArg)), |
| 3049 | ArgsType), CGF.getPointerAlign()); |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3050 | // *(Type0*)Dst[0] = *(Type0*)Src[0]; |
| 3051 | // *(Type1*)Dst[1] = *(Type1*)Src[1]; |
| 3052 | // ... |
| 3053 | // *(Typen*)Dst[n] = *(Typen*)Src[n]; |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3054 | for (unsigned I = 0, E = AssignmentOps.size(); I < E; ++I) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3055 | const auto *DestVar = |
| 3056 | cast<VarDecl>(cast<DeclRefExpr>(DestExprs[I])->getDecl()); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3057 | Address DestAddr = emitAddrOfVarFromArray(CGF, LHS, I, DestVar); |
| 3058 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3059 | const auto *SrcVar = |
| 3060 | cast<VarDecl>(cast<DeclRefExpr>(SrcExprs[I])->getDecl()); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3061 | Address SrcAddr = emitAddrOfVarFromArray(CGF, RHS, I, SrcVar); |
| 3062 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3063 | const auto *VD = cast<DeclRefExpr>(CopyprivateVars[I])->getDecl(); |
Alexey Bataev | 1d9c15c | 2015-05-19 12:31:28 +0000 | [diff] [blame] | 3064 | QualType Type = VD->getType(); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3065 | CGF.EmitOMPCopy(Type, DestAddr, SrcAddr, DestVar, SrcVar, AssignmentOps[I]); |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3066 | } |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3067 | CGF.FinishFunction(); |
| 3068 | return Fn; |
| 3069 | } |
| 3070 | |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 3071 | void CGOpenMPRuntime::emitSingleRegion(CodeGenFunction &CGF, |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 3072 | const RegionCodeGenTy &SingleOpGen, |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3073 | SourceLocation Loc, |
| 3074 | ArrayRef<const Expr *> CopyprivateVars, |
| 3075 | ArrayRef<const Expr *> SrcExprs, |
| 3076 | ArrayRef<const Expr *> DstExprs, |
| 3077 | ArrayRef<const Expr *> AssignmentOps) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 3078 | if (!CGF.HaveInsertPoint()) |
| 3079 | return; |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3080 | assert(CopyprivateVars.size() == SrcExprs.size() && |
| 3081 | CopyprivateVars.size() == DstExprs.size() && |
| 3082 | CopyprivateVars.size() == AssignmentOps.size()); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3083 | ASTContext &C = CGM.getContext(); |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3084 | // int32 did_it = 0; |
Alexey Bataev | 6956e2e | 2015-02-05 06:35:41 +0000 | [diff] [blame] | 3085 | // if(__kmpc_single(ident_t *, gtid)) { |
| 3086 | // SingleOpGen(); |
| 3087 | // __kmpc_end_single(ident_t *, gtid); |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3088 | // did_it = 1; |
Alexey Bataev | 6956e2e | 2015-02-05 06:35:41 +0000 | [diff] [blame] | 3089 | // } |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3090 | // call __kmpc_copyprivate(ident_t *, gtid, <buf_size>, <copyprivate list>, |
| 3091 | // <copy_func>, did_it); |
| 3092 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3093 | Address DidIt = Address::invalid(); |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3094 | if (!CopyprivateVars.empty()) { |
| 3095 | // int32 did_it = 0; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3096 | QualType KmpInt32Ty = |
| 3097 | C.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1); |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3098 | DidIt = CGF.CreateMemTemp(KmpInt32Ty, ".omp.copyprivate.did_it"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3099 | CGF.Builder.CreateStore(CGF.Builder.getInt32(0), DidIt); |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3100 | } |
Alexey Bataev | 6956e2e | 2015-02-05 06:35:41 +0000 | [diff] [blame] | 3101 | // Prepare arguments and build a call to __kmpc_single |
Alexey Bataev | d7614fb | 2015-04-10 06:33:45 +0000 | [diff] [blame] | 3102 | llvm::Value *Args[] = {emitUpdateLocation(CGF, Loc), getThreadID(CGF, Loc)}; |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 3103 | CommonActionTy Action(createRuntimeFunction(OMPRTL__kmpc_single), Args, |
| 3104 | createRuntimeFunction(OMPRTL__kmpc_end_single), Args, |
| 3105 | /*Conditional=*/true); |
| 3106 | SingleOpGen.setAction(Action); |
| 3107 | emitInlinedDirective(CGF, OMPD_single, SingleOpGen); |
| 3108 | if (DidIt.isValid()) { |
| 3109 | // did_it = 1; |
| 3110 | CGF.Builder.CreateStore(CGF.Builder.getInt32(1), DidIt); |
| 3111 | } |
| 3112 | Action.Done(CGF); |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3113 | // call __kmpc_copyprivate(ident_t *, gtid, <buf_size>, <copyprivate list>, |
| 3114 | // <copy_func>, did_it); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3115 | if (DidIt.isValid()) { |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3116 | llvm::APInt ArraySize(/*unsigned int numBits=*/32, CopyprivateVars.size()); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3117 | QualType CopyprivateArrayTy = |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3118 | C.getConstantArrayType(C.VoidPtrTy, ArraySize, ArrayType::Normal, |
| 3119 | /*IndexTypeQuals=*/0); |
| 3120 | // Create a list of all private variables for copyprivate. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3121 | Address CopyprivateList = |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3122 | CGF.CreateMemTemp(CopyprivateArrayTy, ".omp.copyprivate.cpr_list"); |
| 3123 | for (unsigned I = 0, E = CopyprivateVars.size(); I < E; ++I) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3124 | Address Elem = CGF.Builder.CreateConstArrayGEP( |
| 3125 | CopyprivateList, I, CGF.getPointerSize()); |
| 3126 | CGF.Builder.CreateStore( |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3127 | CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3128 | CGF.EmitLValue(CopyprivateVars[I]).getPointer(), CGF.VoidPtrTy), |
| 3129 | Elem); |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3130 | } |
| 3131 | // Build function that copies private values from single region to all other |
| 3132 | // threads in the corresponding parallel region. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3133 | llvm::Value *CpyFn = emitCopyprivateCopyFunction( |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3134 | CGM, CGF.ConvertTypeForMem(CopyprivateArrayTy)->getPointerTo(), |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 3135 | CopyprivateVars, SrcExprs, DstExprs, AssignmentOps, Loc); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3136 | llvm::Value *BufSize = CGF.getTypeSize(CopyprivateArrayTy); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3137 | Address CL = |
| 3138 | CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(CopyprivateList, |
| 3139 | CGF.VoidPtrTy); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3140 | llvm::Value *DidItVal = CGF.Builder.CreateLoad(DidIt); |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3141 | llvm::Value *Args[] = { |
| 3142 | emitUpdateLocation(CGF, Loc), // ident_t *<loc> |
| 3143 | getThreadID(CGF, Loc), // i32 <gtid> |
Alexey Bataev | 66beaa9 | 2015-04-30 03:47:32 +0000 | [diff] [blame] | 3144 | BufSize, // size_t <buf_size> |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3145 | CL.getPointer(), // void *<copyprivate list> |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3146 | CpyFn, // void (*) (void *, void *) <copy_func> |
| 3147 | DidItVal // i32 did_it |
| 3148 | }; |
| 3149 | CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_copyprivate), Args); |
| 3150 | } |
Alexey Bataev | 6956e2e | 2015-02-05 06:35:41 +0000 | [diff] [blame] | 3151 | } |
| 3152 | |
Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 3153 | void CGOpenMPRuntime::emitOrderedRegion(CodeGenFunction &CGF, |
| 3154 | const RegionCodeGenTy &OrderedOpGen, |
Alexey Bataev | 5f600d6 | 2015-09-29 03:48:57 +0000 | [diff] [blame] | 3155 | SourceLocation Loc, bool IsThreads) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 3156 | if (!CGF.HaveInsertPoint()) |
| 3157 | return; |
Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 3158 | // __kmpc_ordered(ident_t *, gtid); |
| 3159 | // OrderedOpGen(); |
| 3160 | // __kmpc_end_ordered(ident_t *, gtid); |
| 3161 | // Prepare arguments and build a call to __kmpc_ordered |
Alexey Bataev | 5f600d6 | 2015-09-29 03:48:57 +0000 | [diff] [blame] | 3162 | if (IsThreads) { |
Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 3163 | llvm::Value *Args[] = {emitUpdateLocation(CGF, Loc), getThreadID(CGF, Loc)}; |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 3164 | CommonActionTy Action(createRuntimeFunction(OMPRTL__kmpc_ordered), Args, |
| 3165 | createRuntimeFunction(OMPRTL__kmpc_end_ordered), |
| 3166 | Args); |
| 3167 | OrderedOpGen.setAction(Action); |
| 3168 | emitInlinedDirective(CGF, OMPD_ordered, OrderedOpGen); |
| 3169 | return; |
Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 3170 | } |
Alexey Bataev | 5f600d6 | 2015-09-29 03:48:57 +0000 | [diff] [blame] | 3171 | emitInlinedDirective(CGF, OMPD_ordered, OrderedOpGen); |
Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 3172 | } |
| 3173 | |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 3174 | void CGOpenMPRuntime::emitBarrierCall(CodeGenFunction &CGF, SourceLocation Loc, |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 3175 | OpenMPDirectiveKind Kind, bool EmitChecks, |
| 3176 | bool ForceSimpleCall) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 3177 | if (!CGF.HaveInsertPoint()) |
| 3178 | return; |
Alexey Bataev | 8f7c1b0 | 2014-12-05 04:09:23 +0000 | [diff] [blame] | 3179 | // Build call __kmpc_cancel_barrier(loc, thread_id); |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 3180 | // Build call __kmpc_barrier(loc, thread_id); |
Alexey Bataev | 50b3c95 | 2016-02-19 10:38:26 +0000 | [diff] [blame] | 3181 | unsigned Flags; |
| 3182 | if (Kind == OMPD_for) |
| 3183 | Flags = OMP_IDENT_BARRIER_IMPL_FOR; |
| 3184 | else if (Kind == OMPD_sections) |
| 3185 | Flags = OMP_IDENT_BARRIER_IMPL_SECTIONS; |
| 3186 | else if (Kind == OMPD_single) |
| 3187 | Flags = OMP_IDENT_BARRIER_IMPL_SINGLE; |
| 3188 | else if (Kind == OMPD_barrier) |
| 3189 | Flags = OMP_IDENT_BARRIER_EXPL; |
| 3190 | else |
| 3191 | Flags = OMP_IDENT_BARRIER_IMPL; |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 3192 | // Build call __kmpc_cancel_barrier(loc, thread_id) or __kmpc_barrier(loc, |
| 3193 | // thread_id); |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 3194 | llvm::Value *Args[] = {emitUpdateLocation(CGF, Loc, Flags), |
| 3195 | getThreadID(CGF, Loc)}; |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 3196 | if (auto *OMPRegionInfo = |
| 3197 | dyn_cast_or_null<CGOpenMPRegionInfo>(CGF.CapturedStmtInfo)) { |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 3198 | if (!ForceSimpleCall && OMPRegionInfo->hasCancel()) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3199 | llvm::Value *Result = CGF.EmitRuntimeCall( |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 3200 | createRuntimeFunction(OMPRTL__kmpc_cancel_barrier), Args); |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 3201 | if (EmitChecks) { |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 3202 | // if (__kmpc_cancel_barrier()) { |
| 3203 | // exit from construct; |
| 3204 | // } |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3205 | llvm::BasicBlock *ExitBB = CGF.createBasicBlock(".cancel.exit"); |
| 3206 | llvm::BasicBlock *ContBB = CGF.createBasicBlock(".cancel.continue"); |
| 3207 | llvm::Value *Cmp = CGF.Builder.CreateIsNotNull(Result); |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 3208 | CGF.Builder.CreateCondBr(Cmp, ExitBB, ContBB); |
| 3209 | CGF.EmitBlock(ExitBB); |
| 3210 | // exit from construct; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3211 | CodeGenFunction::JumpDest CancelDestination = |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 3212 | CGF.getOMPCancelDestination(OMPRegionInfo->getDirectiveKind()); |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 3213 | CGF.EmitBranchThroughCleanup(CancelDestination); |
| 3214 | CGF.EmitBlock(ContBB, /*IsFinished=*/true); |
| 3215 | } |
| 3216 | return; |
| 3217 | } |
| 3218 | } |
| 3219 | CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_barrier), Args); |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 3220 | } |
| 3221 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 3222 | /// Map the OpenMP loop schedule to the runtime enumeration. |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 3223 | static OpenMPSchedType getRuntimeSchedule(OpenMPScheduleClauseKind ScheduleKind, |
Alexey Bataev | d7589ffe | 2015-05-20 13:12:48 +0000 | [diff] [blame] | 3224 | bool Chunked, bool Ordered) { |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 3225 | switch (ScheduleKind) { |
| 3226 | case OMPC_SCHEDULE_static: |
Alexey Bataev | d7589ffe | 2015-05-20 13:12:48 +0000 | [diff] [blame] | 3227 | return Chunked ? (Ordered ? OMP_ord_static_chunked : OMP_sch_static_chunked) |
| 3228 | : (Ordered ? OMP_ord_static : OMP_sch_static); |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 3229 | case OMPC_SCHEDULE_dynamic: |
Alexey Bataev | d7589ffe | 2015-05-20 13:12:48 +0000 | [diff] [blame] | 3230 | return Ordered ? OMP_ord_dynamic_chunked : OMP_sch_dynamic_chunked; |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 3231 | case OMPC_SCHEDULE_guided: |
Alexey Bataev | d7589ffe | 2015-05-20 13:12:48 +0000 | [diff] [blame] | 3232 | return Ordered ? OMP_ord_guided_chunked : OMP_sch_guided_chunked; |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 3233 | case OMPC_SCHEDULE_runtime: |
Alexey Bataev | d7589ffe | 2015-05-20 13:12:48 +0000 | [diff] [blame] | 3234 | return Ordered ? OMP_ord_runtime : OMP_sch_runtime; |
| 3235 | case OMPC_SCHEDULE_auto: |
| 3236 | return Ordered ? OMP_ord_auto : OMP_sch_auto; |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 3237 | case OMPC_SCHEDULE_unknown: |
| 3238 | assert(!Chunked && "chunk was specified but schedule kind not known"); |
Alexey Bataev | d7589ffe | 2015-05-20 13:12:48 +0000 | [diff] [blame] | 3239 | return Ordered ? OMP_ord_static : OMP_sch_static; |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 3240 | } |
| 3241 | llvm_unreachable("Unexpected runtime schedule"); |
| 3242 | } |
| 3243 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 3244 | /// Map the OpenMP distribute schedule to the runtime enumeration. |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3245 | static OpenMPSchedType |
| 3246 | getRuntimeSchedule(OpenMPDistScheduleClauseKind ScheduleKind, bool Chunked) { |
| 3247 | // only static is allowed for dist_schedule |
| 3248 | return Chunked ? OMP_dist_sch_static_chunked : OMP_dist_sch_static; |
| 3249 | } |
| 3250 | |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 3251 | bool CGOpenMPRuntime::isStaticNonchunked(OpenMPScheduleClauseKind ScheduleKind, |
| 3252 | bool Chunked) const { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3253 | OpenMPSchedType Schedule = |
| 3254 | getRuntimeSchedule(ScheduleKind, Chunked, /*Ordered=*/false); |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 3255 | return Schedule == OMP_sch_static; |
| 3256 | } |
| 3257 | |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3258 | bool CGOpenMPRuntime::isStaticNonchunked( |
| 3259 | OpenMPDistScheduleClauseKind ScheduleKind, bool Chunked) const { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3260 | OpenMPSchedType Schedule = getRuntimeSchedule(ScheduleKind, Chunked); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3261 | return Schedule == OMP_dist_sch_static; |
| 3262 | } |
| 3263 | |
| 3264 | |
Alexander Musman | df7a8e2 | 2015-01-22 08:49:35 +0000 | [diff] [blame] | 3265 | bool CGOpenMPRuntime::isDynamic(OpenMPScheduleClauseKind ScheduleKind) const { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3266 | OpenMPSchedType Schedule = |
Alexey Bataev | d7589ffe | 2015-05-20 13:12:48 +0000 | [diff] [blame] | 3267 | getRuntimeSchedule(ScheduleKind, /*Chunked=*/false, /*Ordered=*/false); |
Alexander Musman | df7a8e2 | 2015-01-22 08:49:35 +0000 | [diff] [blame] | 3268 | assert(Schedule != OMP_sch_static_chunked && "cannot be chunked here"); |
| 3269 | return Schedule != OMP_sch_static; |
| 3270 | } |
| 3271 | |
Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 3272 | static int addMonoNonMonoModifier(OpenMPSchedType Schedule, |
| 3273 | OpenMPScheduleClauseModifier M1, |
| 3274 | OpenMPScheduleClauseModifier M2) { |
Alexey Bataev | 6cff624 | 2016-05-30 13:05:14 +0000 | [diff] [blame] | 3275 | int Modifier = 0; |
Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 3276 | switch (M1) { |
| 3277 | case OMPC_SCHEDULE_MODIFIER_monotonic: |
Alexey Bataev | 6cff624 | 2016-05-30 13:05:14 +0000 | [diff] [blame] | 3278 | Modifier = OMP_sch_modifier_monotonic; |
| 3279 | break; |
Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 3280 | case OMPC_SCHEDULE_MODIFIER_nonmonotonic: |
Alexey Bataev | 6cff624 | 2016-05-30 13:05:14 +0000 | [diff] [blame] | 3281 | Modifier = OMP_sch_modifier_nonmonotonic; |
| 3282 | break; |
Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 3283 | case OMPC_SCHEDULE_MODIFIER_simd: |
Alexey Bataev | 6cff624 | 2016-05-30 13:05:14 +0000 | [diff] [blame] | 3284 | if (Schedule == OMP_sch_static_chunked) |
| 3285 | Schedule = OMP_sch_static_balanced_chunked; |
| 3286 | break; |
Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 3287 | case OMPC_SCHEDULE_MODIFIER_last: |
| 3288 | case OMPC_SCHEDULE_MODIFIER_unknown: |
| 3289 | break; |
| 3290 | } |
| 3291 | switch (M2) { |
| 3292 | case OMPC_SCHEDULE_MODIFIER_monotonic: |
Alexey Bataev | 6cff624 | 2016-05-30 13:05:14 +0000 | [diff] [blame] | 3293 | Modifier = OMP_sch_modifier_monotonic; |
| 3294 | break; |
Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 3295 | case OMPC_SCHEDULE_MODIFIER_nonmonotonic: |
Alexey Bataev | 6cff624 | 2016-05-30 13:05:14 +0000 | [diff] [blame] | 3296 | Modifier = OMP_sch_modifier_nonmonotonic; |
| 3297 | break; |
Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 3298 | case OMPC_SCHEDULE_MODIFIER_simd: |
Alexey Bataev | 6cff624 | 2016-05-30 13:05:14 +0000 | [diff] [blame] | 3299 | if (Schedule == OMP_sch_static_chunked) |
| 3300 | Schedule = OMP_sch_static_balanced_chunked; |
| 3301 | break; |
Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 3302 | case OMPC_SCHEDULE_MODIFIER_last: |
| 3303 | case OMPC_SCHEDULE_MODIFIER_unknown: |
| 3304 | break; |
| 3305 | } |
Alexey Bataev | 6cff624 | 2016-05-30 13:05:14 +0000 | [diff] [blame] | 3306 | return Schedule | Modifier; |
Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 3307 | } |
| 3308 | |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 3309 | void CGOpenMPRuntime::emitForDispatchInit( |
| 3310 | CodeGenFunction &CGF, SourceLocation Loc, |
| 3311 | const OpenMPScheduleTy &ScheduleKind, unsigned IVSize, bool IVSigned, |
| 3312 | bool Ordered, const DispatchRTInput &DispatchValues) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 3313 | if (!CGF.HaveInsertPoint()) |
| 3314 | return; |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 3315 | OpenMPSchedType Schedule = getRuntimeSchedule( |
| 3316 | ScheduleKind.Schedule, DispatchValues.Chunk != nullptr, Ordered); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3317 | assert(Ordered || |
| 3318 | (Schedule != OMP_sch_static && Schedule != OMP_sch_static_chunked && |
Alexey Bataev | 6cff624 | 2016-05-30 13:05:14 +0000 | [diff] [blame] | 3319 | Schedule != OMP_ord_static && Schedule != OMP_ord_static_chunked && |
| 3320 | Schedule != OMP_sch_static_balanced_chunked)); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3321 | // Call __kmpc_dispatch_init( |
| 3322 | // ident_t *loc, kmp_int32 tid, kmp_int32 schedule, |
| 3323 | // kmp_int[32|64] lower, kmp_int[32|64] upper, |
| 3324 | // kmp_int[32|64] stride, kmp_int[32|64] chunk); |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 3325 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3326 | // If the Chunk was not specified in the clause - use default value 1. |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 3327 | llvm::Value *Chunk = DispatchValues.Chunk ? DispatchValues.Chunk |
| 3328 | : CGF.Builder.getIntN(IVSize, 1); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3329 | llvm::Value *Args[] = { |
Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 3330 | emitUpdateLocation(CGF, Loc), getThreadID(CGF, Loc), |
| 3331 | CGF.Builder.getInt32(addMonoNonMonoModifier( |
| 3332 | Schedule, ScheduleKind.M1, ScheduleKind.M2)), // Schedule type |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 3333 | DispatchValues.LB, // Lower |
| 3334 | DispatchValues.UB, // Upper |
Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 3335 | CGF.Builder.getIntN(IVSize, 1), // Stride |
| 3336 | Chunk // Chunk |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3337 | }; |
| 3338 | CGF.EmitRuntimeCall(createDispatchInitFunction(IVSize, IVSigned), Args); |
| 3339 | } |
| 3340 | |
Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 3341 | static void emitForStaticInitCall( |
| 3342 | CodeGenFunction &CGF, llvm::Value *UpdateLocation, llvm::Value *ThreadId, |
| 3343 | llvm::Constant *ForStaticInitFunction, OpenMPSchedType Schedule, |
| 3344 | OpenMPScheduleClauseModifier M1, OpenMPScheduleClauseModifier M2, |
Alexey Bataev | 0f87dbe | 2017-08-14 17:56:13 +0000 | [diff] [blame] | 3345 | const CGOpenMPRuntime::StaticRTInput &Values) { |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3346 | if (!CGF.HaveInsertPoint()) |
Alexey Bataev | 0f87dbe | 2017-08-14 17:56:13 +0000 | [diff] [blame] | 3347 | return; |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3348 | |
Alexey Bataev | 0f87dbe | 2017-08-14 17:56:13 +0000 | [diff] [blame] | 3349 | assert(!Values.Ordered); |
| 3350 | assert(Schedule == OMP_sch_static || Schedule == OMP_sch_static_chunked || |
| 3351 | Schedule == OMP_sch_static_balanced_chunked || |
| 3352 | Schedule == OMP_ord_static || Schedule == OMP_ord_static_chunked || |
| 3353 | Schedule == OMP_dist_sch_static || |
| 3354 | Schedule == OMP_dist_sch_static_chunked); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3355 | |
Alexey Bataev | 0f87dbe | 2017-08-14 17:56:13 +0000 | [diff] [blame] | 3356 | // Call __kmpc_for_static_init( |
| 3357 | // ident_t *loc, kmp_int32 tid, kmp_int32 schedtype, |
| 3358 | // kmp_int32 *p_lastiter, kmp_int[32|64] *p_lower, |
| 3359 | // kmp_int[32|64] *p_upper, kmp_int[32|64] *p_stride, |
| 3360 | // kmp_int[32|64] incr, kmp_int[32|64] chunk); |
| 3361 | llvm::Value *Chunk = Values.Chunk; |
| 3362 | if (Chunk == nullptr) { |
| 3363 | assert((Schedule == OMP_sch_static || Schedule == OMP_ord_static || |
| 3364 | Schedule == OMP_dist_sch_static) && |
| 3365 | "expected static non-chunked schedule"); |
| 3366 | // If the Chunk was not specified in the clause - use default value 1. |
| 3367 | Chunk = CGF.Builder.getIntN(Values.IVSize, 1); |
| 3368 | } else { |
| 3369 | assert((Schedule == OMP_sch_static_chunked || |
| 3370 | Schedule == OMP_sch_static_balanced_chunked || |
| 3371 | Schedule == OMP_ord_static_chunked || |
| 3372 | Schedule == OMP_dist_sch_static_chunked) && |
| 3373 | "expected static chunked schedule"); |
| 3374 | } |
| 3375 | llvm::Value *Args[] = { |
| 3376 | UpdateLocation, |
| 3377 | ThreadId, |
| 3378 | CGF.Builder.getInt32(addMonoNonMonoModifier(Schedule, M1, |
| 3379 | M2)), // Schedule type |
| 3380 | Values.IL.getPointer(), // &isLastIter |
| 3381 | Values.LB.getPointer(), // &LB |
| 3382 | Values.UB.getPointer(), // &UB |
| 3383 | Values.ST.getPointer(), // &Stride |
| 3384 | CGF.Builder.getIntN(Values.IVSize, 1), // Incr |
| 3385 | Chunk // Chunk |
| 3386 | }; |
| 3387 | CGF.EmitRuntimeCall(ForStaticInitFunction, Args); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3388 | } |
| 3389 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3390 | void CGOpenMPRuntime::emitForStaticInit(CodeGenFunction &CGF, |
| 3391 | SourceLocation Loc, |
Alexey Bataev | 0f87dbe | 2017-08-14 17:56:13 +0000 | [diff] [blame] | 3392 | OpenMPDirectiveKind DKind, |
Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 3393 | const OpenMPScheduleTy &ScheduleKind, |
Alexey Bataev | 0f87dbe | 2017-08-14 17:56:13 +0000 | [diff] [blame] | 3394 | const StaticRTInput &Values) { |
| 3395 | OpenMPSchedType ScheduleNum = getRuntimeSchedule( |
| 3396 | ScheduleKind.Schedule, Values.Chunk != nullptr, Values.Ordered); |
| 3397 | assert(isOpenMPWorksharingDirective(DKind) && |
| 3398 | "Expected loop-based or sections-based directive."); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3399 | llvm::Value *UpdatedLocation = emitUpdateLocation(CGF, Loc, |
Alexey Bataev | 0f87dbe | 2017-08-14 17:56:13 +0000 | [diff] [blame] | 3400 | isOpenMPLoopDirective(DKind) |
| 3401 | ? OMP_IDENT_WORK_LOOP |
| 3402 | : OMP_IDENT_WORK_SECTIONS); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3403 | llvm::Value *ThreadId = getThreadID(CGF, Loc); |
| 3404 | llvm::Constant *StaticInitFunction = |
Alexey Bataev | 0f87dbe | 2017-08-14 17:56:13 +0000 | [diff] [blame] | 3405 | createForStaticInitFunction(Values.IVSize, Values.IVSigned); |
Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 3406 | emitForStaticInitCall(CGF, UpdatedLocation, ThreadId, StaticInitFunction, |
Alexey Bataev | 0f87dbe | 2017-08-14 17:56:13 +0000 | [diff] [blame] | 3407 | ScheduleNum, ScheduleKind.M1, ScheduleKind.M2, Values); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3408 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3409 | |
Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 3410 | void CGOpenMPRuntime::emitDistributeStaticInit( |
| 3411 | CodeGenFunction &CGF, SourceLocation Loc, |
Alexey Bataev | 0f87dbe | 2017-08-14 17:56:13 +0000 | [diff] [blame] | 3412 | OpenMPDistScheduleClauseKind SchedKind, |
| 3413 | const CGOpenMPRuntime::StaticRTInput &Values) { |
| 3414 | OpenMPSchedType ScheduleNum = |
| 3415 | getRuntimeSchedule(SchedKind, Values.Chunk != nullptr); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3416 | llvm::Value *UpdatedLocation = |
Alexey Bataev | 0f87dbe | 2017-08-14 17:56:13 +0000 | [diff] [blame] | 3417 | emitUpdateLocation(CGF, Loc, OMP_IDENT_WORK_DISTRIBUTE); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3418 | llvm::Value *ThreadId = getThreadID(CGF, Loc); |
| 3419 | llvm::Constant *StaticInitFunction = |
Alexey Bataev | 0f87dbe | 2017-08-14 17:56:13 +0000 | [diff] [blame] | 3420 | createForStaticInitFunction(Values.IVSize, Values.IVSigned); |
Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 3421 | emitForStaticInitCall(CGF, UpdatedLocation, ThreadId, StaticInitFunction, |
| 3422 | ScheduleNum, OMPC_SCHEDULE_MODIFIER_unknown, |
Alexey Bataev | 0f87dbe | 2017-08-14 17:56:13 +0000 | [diff] [blame] | 3423 | OMPC_SCHEDULE_MODIFIER_unknown, Values); |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 3424 | } |
| 3425 | |
Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 3426 | void CGOpenMPRuntime::emitForStaticFinish(CodeGenFunction &CGF, |
Alexey Bataev | f43f714 | 2017-09-06 16:17:35 +0000 | [diff] [blame] | 3427 | SourceLocation Loc, |
| 3428 | OpenMPDirectiveKind DKind) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 3429 | if (!CGF.HaveInsertPoint()) |
| 3430 | return; |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 3431 | // Call __kmpc_for_static_fini(ident_t *loc, kmp_int32 tid); |
Alexey Bataev | f43f714 | 2017-09-06 16:17:35 +0000 | [diff] [blame] | 3432 | llvm::Value *Args[] = { |
| 3433 | emitUpdateLocation(CGF, Loc, |
| 3434 | isOpenMPDistributeDirective(DKind) |
| 3435 | ? OMP_IDENT_WORK_DISTRIBUTE |
| 3436 | : isOpenMPLoopDirective(DKind) |
| 3437 | ? OMP_IDENT_WORK_LOOP |
| 3438 | : OMP_IDENT_WORK_SECTIONS), |
| 3439 | getThreadID(CGF, Loc)}; |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 3440 | CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_for_static_fini), |
| 3441 | Args); |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 3442 | } |
| 3443 | |
Alexey Bataev | d7589ffe | 2015-05-20 13:12:48 +0000 | [diff] [blame] | 3444 | void CGOpenMPRuntime::emitForOrderedIterationEnd(CodeGenFunction &CGF, |
| 3445 | SourceLocation Loc, |
| 3446 | unsigned IVSize, |
| 3447 | bool IVSigned) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 3448 | if (!CGF.HaveInsertPoint()) |
| 3449 | return; |
Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 3450 | // Call __kmpc_for_dynamic_fini_(4|8)[u](ident_t *loc, kmp_int32 tid); |
Alexey Bataev | 50b3c95 | 2016-02-19 10:38:26 +0000 | [diff] [blame] | 3451 | llvm::Value *Args[] = {emitUpdateLocation(CGF, Loc), getThreadID(CGF, Loc)}; |
Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 3452 | CGF.EmitRuntimeCall(createDispatchFiniFunction(IVSize, IVSigned), Args); |
| 3453 | } |
| 3454 | |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 3455 | llvm::Value *CGOpenMPRuntime::emitForNext(CodeGenFunction &CGF, |
| 3456 | SourceLocation Loc, unsigned IVSize, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3457 | bool IVSigned, Address IL, |
| 3458 | Address LB, Address UB, |
| 3459 | Address ST) { |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 3460 | // Call __kmpc_dispatch_next( |
| 3461 | // ident_t *loc, kmp_int32 tid, kmp_int32 *p_lastiter, |
| 3462 | // kmp_int[32|64] *p_lower, kmp_int[32|64] *p_upper, |
| 3463 | // kmp_int[32|64] *p_stride); |
| 3464 | llvm::Value *Args[] = { |
Alexey Bataev | 50b3c95 | 2016-02-19 10:38:26 +0000 | [diff] [blame] | 3465 | emitUpdateLocation(CGF, Loc), |
| 3466 | getThreadID(CGF, Loc), |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3467 | IL.getPointer(), // &isLastIter |
| 3468 | LB.getPointer(), // &Lower |
| 3469 | UB.getPointer(), // &Upper |
| 3470 | ST.getPointer() // &Stride |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 3471 | }; |
| 3472 | llvm::Value *Call = |
| 3473 | CGF.EmitRuntimeCall(createDispatchNextFunction(IVSize, IVSigned), Args); |
| 3474 | return CGF.EmitScalarConversion( |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3475 | Call, CGF.getContext().getIntTypeForBitwidth(32, /*Signed=*/1), |
Filipe Cabecinhas | 7af183d | 2015-08-11 04:19:28 +0000 | [diff] [blame] | 3476 | CGF.getContext().BoolTy, Loc); |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 3477 | } |
| 3478 | |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 3479 | void CGOpenMPRuntime::emitNumThreadsClause(CodeGenFunction &CGF, |
| 3480 | llvm::Value *NumThreads, |
| 3481 | SourceLocation Loc) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 3482 | if (!CGF.HaveInsertPoint()) |
| 3483 | return; |
Alexey Bataev | b205978 | 2014-10-13 08:23:51 +0000 | [diff] [blame] | 3484 | // Build call __kmpc_push_num_threads(&loc, global_tid, num_threads) |
| 3485 | llvm::Value *Args[] = { |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 3486 | emitUpdateLocation(CGF, Loc), getThreadID(CGF, Loc), |
Alexey Bataev | b205978 | 2014-10-13 08:23:51 +0000 | [diff] [blame] | 3487 | CGF.Builder.CreateIntCast(NumThreads, CGF.Int32Ty, /*isSigned*/ true)}; |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 3488 | CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_push_num_threads), |
| 3489 | Args); |
Alexey Bataev | b205978 | 2014-10-13 08:23:51 +0000 | [diff] [blame] | 3490 | } |
| 3491 | |
Alexey Bataev | 7f210c6 | 2015-06-18 13:40:03 +0000 | [diff] [blame] | 3492 | void CGOpenMPRuntime::emitProcBindClause(CodeGenFunction &CGF, |
| 3493 | OpenMPProcBindClauseKind ProcBind, |
| 3494 | SourceLocation Loc) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 3495 | if (!CGF.HaveInsertPoint()) |
| 3496 | return; |
Alexey Bataev | 7f210c6 | 2015-06-18 13:40:03 +0000 | [diff] [blame] | 3497 | // Constants for proc bind value accepted by the runtime. |
| 3498 | enum ProcBindTy { |
| 3499 | ProcBindFalse = 0, |
| 3500 | ProcBindTrue, |
| 3501 | ProcBindMaster, |
| 3502 | ProcBindClose, |
| 3503 | ProcBindSpread, |
| 3504 | ProcBindIntel, |
| 3505 | ProcBindDefault |
| 3506 | } RuntimeProcBind; |
| 3507 | switch (ProcBind) { |
| 3508 | case OMPC_PROC_BIND_master: |
| 3509 | RuntimeProcBind = ProcBindMaster; |
| 3510 | break; |
| 3511 | case OMPC_PROC_BIND_close: |
| 3512 | RuntimeProcBind = ProcBindClose; |
| 3513 | break; |
| 3514 | case OMPC_PROC_BIND_spread: |
| 3515 | RuntimeProcBind = ProcBindSpread; |
| 3516 | break; |
| 3517 | case OMPC_PROC_BIND_unknown: |
| 3518 | llvm_unreachable("Unsupported proc_bind value."); |
| 3519 | } |
| 3520 | // Build call __kmpc_push_proc_bind(&loc, global_tid, proc_bind) |
| 3521 | llvm::Value *Args[] = { |
| 3522 | emitUpdateLocation(CGF, Loc), getThreadID(CGF, Loc), |
| 3523 | llvm::ConstantInt::get(CGM.IntTy, RuntimeProcBind, /*isSigned=*/true)}; |
| 3524 | CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_push_proc_bind), Args); |
| 3525 | } |
| 3526 | |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 3527 | void CGOpenMPRuntime::emitFlush(CodeGenFunction &CGF, ArrayRef<const Expr *>, |
| 3528 | SourceLocation Loc) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 3529 | if (!CGF.HaveInsertPoint()) |
| 3530 | return; |
Alexey Bataev | d76df6d | 2015-02-24 12:55:09 +0000 | [diff] [blame] | 3531 | // Build call void __kmpc_flush(ident_t *loc) |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 3532 | CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_flush), |
| 3533 | emitUpdateLocation(CGF, Loc)); |
Alexey Bataev | cc37cc1 | 2014-11-20 04:34:54 +0000 | [diff] [blame] | 3534 | } |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 3535 | |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 3536 | namespace { |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 3537 | /// Indexes of fields for type kmp_task_t. |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 3538 | enum KmpTaskTFields { |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 3539 | /// List of shared variables. |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 3540 | KmpTaskTShareds, |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 3541 | /// Task routine. |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 3542 | KmpTaskTRoutine, |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 3543 | /// Partition id for the untied tasks. |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 3544 | KmpTaskTPartId, |
Alexey Bataev | ad537bb | 2016-05-30 09:06:50 +0000 | [diff] [blame] | 3545 | /// Function with call of destructors for private variables. |
| 3546 | Data1, |
| 3547 | /// Task priority. |
| 3548 | Data2, |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 3549 | /// (Taskloops only) Lower bound. |
| 3550 | KmpTaskTLowerBound, |
| 3551 | /// (Taskloops only) Upper bound. |
| 3552 | KmpTaskTUpperBound, |
| 3553 | /// (Taskloops only) Stride. |
| 3554 | KmpTaskTStride, |
| 3555 | /// (Taskloops only) Is last iteration flag. |
| 3556 | KmpTaskTLastIter, |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 3557 | /// (Taskloops only) Reduction data. |
| 3558 | KmpTaskTReductions, |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 3559 | }; |
Hans Wennborg | 7eb5464 | 2015-09-10 17:07:54 +0000 | [diff] [blame] | 3560 | } // anonymous namespace |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 3561 | |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3562 | bool CGOpenMPRuntime::OffloadEntriesInfoManagerTy::empty() const { |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 3563 | return OffloadEntriesTargetRegion.empty() && |
| 3564 | OffloadEntriesDeviceGlobalVar.empty(); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3565 | } |
| 3566 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 3567 | /// Initialize target region entry. |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3568 | void CGOpenMPRuntime::OffloadEntriesInfoManagerTy:: |
| 3569 | initializeTargetRegionEntryInfo(unsigned DeviceID, unsigned FileID, |
| 3570 | StringRef ParentName, unsigned LineNum, |
Samuel Antao | 2de62b0 | 2016-02-13 23:35:10 +0000 | [diff] [blame] | 3571 | unsigned Order) { |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3572 | assert(CGM.getLangOpts().OpenMPIsDevice && "Initialization of entries is " |
| 3573 | "only required for the device " |
| 3574 | "code generation."); |
Samuel Antao | 2de62b0 | 2016-02-13 23:35:10 +0000 | [diff] [blame] | 3575 | OffloadEntriesTargetRegion[DeviceID][FileID][ParentName][LineNum] = |
Samuel Antao | f83efdb | 2017-01-05 16:02:49 +0000 | [diff] [blame] | 3576 | OffloadEntryInfoTargetRegion(Order, /*Addr=*/nullptr, /*ID=*/nullptr, |
Alexey Bataev | 34f8a70 | 2018-03-28 14:28:54 +0000 | [diff] [blame] | 3577 | OMPTargetRegionEntryTargetRegion); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3578 | ++OffloadingEntriesNum; |
| 3579 | } |
| 3580 | |
| 3581 | void CGOpenMPRuntime::OffloadEntriesInfoManagerTy:: |
| 3582 | registerTargetRegionEntryInfo(unsigned DeviceID, unsigned FileID, |
| 3583 | StringRef ParentName, unsigned LineNum, |
Samuel Antao | f83efdb | 2017-01-05 16:02:49 +0000 | [diff] [blame] | 3584 | llvm::Constant *Addr, llvm::Constant *ID, |
Alexey Bataev | 34f8a70 | 2018-03-28 14:28:54 +0000 | [diff] [blame] | 3585 | OMPTargetRegionEntryKind Flags) { |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3586 | // If we are emitting code for a target, the entry is already initialized, |
| 3587 | // only has to be registered. |
| 3588 | if (CGM.getLangOpts().OpenMPIsDevice) { |
Alexey Bataev | 64e62dc | 2018-04-30 16:26:57 +0000 | [diff] [blame] | 3589 | if (!hasTargetRegionEntryInfo(DeviceID, FileID, ParentName, LineNum)) { |
| 3590 | unsigned DiagID = CGM.getDiags().getCustomDiagID( |
| 3591 | DiagnosticsEngine::Error, |
| 3592 | "Unable to find target region on line '%0' in the device code."); |
| 3593 | CGM.getDiags().Report(DiagID) << LineNum; |
| 3594 | return; |
| 3595 | } |
Samuel Antao | 2de62b0 | 2016-02-13 23:35:10 +0000 | [diff] [blame] | 3596 | auto &Entry = |
| 3597 | OffloadEntriesTargetRegion[DeviceID][FileID][ParentName][LineNum]; |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3598 | assert(Entry.isValid() && "Entry not initialized!"); |
| 3599 | Entry.setAddress(Addr); |
| 3600 | Entry.setID(ID); |
Samuel Antao | f83efdb | 2017-01-05 16:02:49 +0000 | [diff] [blame] | 3601 | Entry.setFlags(Flags); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3602 | } else { |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 3603 | OffloadEntryInfoTargetRegion Entry(OffloadingEntriesNum, Addr, ID, Flags); |
Samuel Antao | 2de62b0 | 2016-02-13 23:35:10 +0000 | [diff] [blame] | 3604 | OffloadEntriesTargetRegion[DeviceID][FileID][ParentName][LineNum] = Entry; |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 3605 | ++OffloadingEntriesNum; |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3606 | } |
| 3607 | } |
| 3608 | |
| 3609 | bool CGOpenMPRuntime::OffloadEntriesInfoManagerTy::hasTargetRegionEntryInfo( |
Samuel Antao | 2de62b0 | 2016-02-13 23:35:10 +0000 | [diff] [blame] | 3610 | unsigned DeviceID, unsigned FileID, StringRef ParentName, |
| 3611 | unsigned LineNum) const { |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3612 | auto PerDevice = OffloadEntriesTargetRegion.find(DeviceID); |
| 3613 | if (PerDevice == OffloadEntriesTargetRegion.end()) |
| 3614 | return false; |
| 3615 | auto PerFile = PerDevice->second.find(FileID); |
| 3616 | if (PerFile == PerDevice->second.end()) |
| 3617 | return false; |
| 3618 | auto PerParentName = PerFile->second.find(ParentName); |
| 3619 | if (PerParentName == PerFile->second.end()) |
| 3620 | return false; |
| 3621 | auto PerLine = PerParentName->second.find(LineNum); |
| 3622 | if (PerLine == PerParentName->second.end()) |
| 3623 | return false; |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3624 | // Fail if this entry is already registered. |
Samuel Antao | 2de62b0 | 2016-02-13 23:35:10 +0000 | [diff] [blame] | 3625 | if (PerLine->second.getAddress() || PerLine->second.getID()) |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3626 | return false; |
| 3627 | return true; |
| 3628 | } |
| 3629 | |
| 3630 | void CGOpenMPRuntime::OffloadEntriesInfoManagerTy::actOnTargetRegionEntriesInfo( |
| 3631 | const OffloadTargetRegionEntryInfoActTy &Action) { |
| 3632 | // Scan all target region entries and perform the provided action. |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 3633 | for (const auto &D : OffloadEntriesTargetRegion) |
| 3634 | for (const auto &F : D.second) |
| 3635 | for (const auto &P : F.second) |
| 3636 | for (const auto &L : P.second) |
Samuel Antao | 2de62b0 | 2016-02-13 23:35:10 +0000 | [diff] [blame] | 3637 | Action(D.first, F.first, P.first(), L.first, L.second); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3638 | } |
| 3639 | |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 3640 | void CGOpenMPRuntime::OffloadEntriesInfoManagerTy:: |
| 3641 | initializeDeviceGlobalVarEntryInfo(StringRef Name, |
| 3642 | OMPTargetGlobalVarEntryKind Flags, |
| 3643 | unsigned Order) { |
| 3644 | assert(CGM.getLangOpts().OpenMPIsDevice && "Initialization of entries is " |
| 3645 | "only required for the device " |
| 3646 | "code generation."); |
| 3647 | OffloadEntriesDeviceGlobalVar.try_emplace(Name, Order, Flags); |
| 3648 | ++OffloadingEntriesNum; |
| 3649 | } |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3650 | |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 3651 | void CGOpenMPRuntime::OffloadEntriesInfoManagerTy:: |
| 3652 | registerDeviceGlobalVarEntryInfo(StringRef VarName, llvm::Constant *Addr, |
| 3653 | CharUnits VarSize, |
| 3654 | OMPTargetGlobalVarEntryKind Flags, |
| 3655 | llvm::GlobalValue::LinkageTypes Linkage) { |
| 3656 | if (CGM.getLangOpts().OpenMPIsDevice) { |
| 3657 | auto &Entry = OffloadEntriesDeviceGlobalVar[VarName]; |
| 3658 | assert(Entry.isValid() && Entry.getFlags() == Flags && |
| 3659 | "Entry not initialized!"); |
| 3660 | assert((!Entry.getAddress() || Entry.getAddress() == Addr) && |
| 3661 | "Resetting with the new address."); |
| 3662 | if (Entry.getAddress() && hasDeviceGlobalVarEntryInfo(VarName)) |
| 3663 | return; |
| 3664 | Entry.setAddress(Addr); |
| 3665 | Entry.setVarSize(VarSize); |
| 3666 | Entry.setLinkage(Linkage); |
| 3667 | } else { |
| 3668 | if (hasDeviceGlobalVarEntryInfo(VarName)) |
| 3669 | return; |
| 3670 | OffloadEntriesDeviceGlobalVar.try_emplace( |
| 3671 | VarName, OffloadingEntriesNum, Addr, VarSize, Flags, Linkage); |
| 3672 | ++OffloadingEntriesNum; |
| 3673 | } |
| 3674 | } |
| 3675 | |
| 3676 | void CGOpenMPRuntime::OffloadEntriesInfoManagerTy:: |
| 3677 | actOnDeviceGlobalVarEntriesInfo( |
| 3678 | const OffloadDeviceGlobalVarEntryInfoActTy &Action) { |
| 3679 | // Scan all target region entries and perform the provided action. |
| 3680 | for (const auto &E : OffloadEntriesDeviceGlobalVar) |
| 3681 | Action(E.getKey(), E.getValue()); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3682 | } |
| 3683 | |
| 3684 | llvm::Function * |
| 3685 | CGOpenMPRuntime::createOffloadingBinaryDescriptorRegistration() { |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3686 | // If we don't have entries or if we are emitting code for the device, we |
| 3687 | // don't need to do anything. |
| 3688 | if (CGM.getLangOpts().OpenMPIsDevice || OffloadEntriesInfoManager.empty()) |
| 3689 | return nullptr; |
| 3690 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3691 | llvm::Module &M = CGM.getModule(); |
| 3692 | ASTContext &C = CGM.getContext(); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3693 | |
| 3694 | // Get list of devices we care about |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3695 | const std::vector<llvm::Triple> &Devices = CGM.getLangOpts().OMPTargetTriples; |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3696 | |
| 3697 | // We should be creating an offloading descriptor only if there are devices |
| 3698 | // specified. |
| 3699 | assert(!Devices.empty() && "No OpenMP offloading devices??"); |
| 3700 | |
| 3701 | // Create the external variables that will point to the begin and end of the |
| 3702 | // host entries section. These will be defined by the linker. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3703 | llvm::Type *OffloadEntryTy = |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3704 | CGM.getTypes().ConvertTypeForMem(getTgtOffloadEntryQTy()); |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 3705 | std::string EntriesBeginName = getName({"omp_offloading", "entries_begin"}); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3706 | auto *HostEntriesBegin = new llvm::GlobalVariable( |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3707 | M, OffloadEntryTy, /*isConstant=*/true, |
Eugene Zelenko | 1660a5d | 2016-01-26 19:01:06 +0000 | [diff] [blame] | 3708 | llvm::GlobalValue::ExternalLinkage, /*Initializer=*/nullptr, |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 3709 | EntriesBeginName); |
| 3710 | std::string EntriesEndName = getName({"omp_offloading", "entries_end"}); |
| 3711 | auto *HostEntriesEnd = |
| 3712 | new llvm::GlobalVariable(M, OffloadEntryTy, /*isConstant=*/true, |
| 3713 | llvm::GlobalValue::ExternalLinkage, |
| 3714 | /*Initializer=*/nullptr, EntriesEndName); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3715 | |
| 3716 | // Create all device images |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3717 | auto *DeviceImageTy = cast<llvm::StructType>( |
| 3718 | CGM.getTypes().ConvertTypeForMem(getTgtDeviceImageQTy())); |
John McCall | 23c9dc6 | 2016-11-28 22:18:27 +0000 | [diff] [blame] | 3719 | ConstantInitBuilder DeviceImagesBuilder(CGM); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3720 | ConstantArrayBuilder DeviceImagesEntries = |
| 3721 | DeviceImagesBuilder.beginArray(DeviceImageTy); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3722 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3723 | for (const llvm::Triple &Device : Devices) { |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 3724 | StringRef T = Device.getTriple(); |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 3725 | std::string BeginName = getName({"omp_offloading", "img_start", ""}); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3726 | auto *ImgBegin = new llvm::GlobalVariable( |
Alexey Bataev | 62a4cb0 | 2018-07-31 18:27:42 +0000 | [diff] [blame] | 3727 | M, CGM.Int8Ty, /*isConstant=*/true, |
| 3728 | llvm::GlobalValue::ExternalWeakLinkage, |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 3729 | /*Initializer=*/nullptr, Twine(BeginName).concat(T)); |
| 3730 | std::string EndName = getName({"omp_offloading", "img_end", ""}); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3731 | auto *ImgEnd = new llvm::GlobalVariable( |
Alexey Bataev | 62a4cb0 | 2018-07-31 18:27:42 +0000 | [diff] [blame] | 3732 | M, CGM.Int8Ty, /*isConstant=*/true, |
| 3733 | llvm::GlobalValue::ExternalWeakLinkage, |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 3734 | /*Initializer=*/nullptr, Twine(EndName).concat(T)); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3735 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3736 | llvm::Constant *Data[] = {ImgBegin, ImgEnd, HostEntriesBegin, |
| 3737 | HostEntriesEnd}; |
| 3738 | createConstantGlobalStructAndAddToParent(CGM, getTgtDeviceImageQTy(), Data, |
| 3739 | DeviceImagesEntries); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3740 | } |
| 3741 | |
| 3742 | // Create device images global array. |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 3743 | std::string ImagesName = getName({"omp_offloading", "device_images"}); |
John McCall | 6c9f1fdb | 2016-11-19 08:17:24 +0000 | [diff] [blame] | 3744 | llvm::GlobalVariable *DeviceImages = |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 3745 | DeviceImagesEntries.finishAndCreateGlobal(ImagesName, |
| 3746 | CGM.getPointerAlign(), |
| 3747 | /*isConstant=*/true); |
Peter Collingbourne | bcf909d | 2016-06-14 21:02:05 +0000 | [diff] [blame] | 3748 | DeviceImages->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3749 | |
| 3750 | // This is a Zero array to be used in the creation of the constant expressions |
| 3751 | llvm::Constant *Index[] = {llvm::Constant::getNullValue(CGM.Int32Ty), |
| 3752 | llvm::Constant::getNullValue(CGM.Int32Ty)}; |
| 3753 | |
| 3754 | // Create the target region descriptor. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3755 | llvm::Constant *Data[] = { |
| 3756 | llvm::ConstantInt::get(CGM.Int32Ty, Devices.size()), |
| 3757 | llvm::ConstantExpr::getGetElementPtr(DeviceImages->getValueType(), |
| 3758 | DeviceImages, Index), |
| 3759 | HostEntriesBegin, HostEntriesEnd}; |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 3760 | std::string Descriptor = getName({"omp_offloading", "descriptor"}); |
Mike Rice | e1ca7b6 | 2018-08-29 15:45:11 +0000 | [diff] [blame] | 3761 | llvm::GlobalVariable *Desc = createGlobalStruct( |
| 3762 | CGM, getTgtBinaryDescriptorQTy(), /*IsConstant=*/true, Data, Descriptor); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3763 | |
| 3764 | // Emit code to register or unregister the descriptor at execution |
| 3765 | // startup or closing, respectively. |
| 3766 | |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 3767 | llvm::Function *UnRegFn; |
| 3768 | { |
| 3769 | FunctionArgList Args; |
| 3770 | ImplicitParamDecl DummyPtr(C, C.VoidPtrTy, ImplicitParamDecl::Other); |
| 3771 | Args.push_back(&DummyPtr); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3772 | |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 3773 | CodeGenFunction CGF(CGM); |
| 3774 | // Disable debug info for global (de-)initializer because they are not part |
| 3775 | // of some particular construct. |
| 3776 | CGF.disableDebugInfo(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3777 | const auto &FI = |
| 3778 | CGM.getTypes().arrangeBuiltinFunctionDeclaration(C.VoidTy, Args); |
| 3779 | llvm::FunctionType *FTy = CGM.getTypes().GetFunctionType(FI); |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 3780 | std::string UnregName = getName({"omp_offloading", "descriptor_unreg"}); |
| 3781 | UnRegFn = CGM.CreateGlobalInitOrDestructFunction(FTy, UnregName, FI); |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 3782 | CGF.StartFunction(GlobalDecl(), C.VoidTy, UnRegFn, FI, Args); |
| 3783 | CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__tgt_unregister_lib), |
| 3784 | Desc); |
| 3785 | CGF.FinishFunction(); |
| 3786 | } |
| 3787 | llvm::Function *RegFn; |
| 3788 | { |
| 3789 | CodeGenFunction CGF(CGM); |
| 3790 | // Disable debug info for global (de-)initializer because they are not part |
| 3791 | // of some particular construct. |
| 3792 | CGF.disableDebugInfo(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3793 | const auto &FI = CGM.getTypes().arrangeNullaryFunction(); |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 3794 | llvm::FunctionType *FTy = CGM.getTypes().GetFunctionType(FI); |
Sergey Dmitriev | bde9cf9 | 2018-08-03 20:19:28 +0000 | [diff] [blame] | 3795 | |
| 3796 | // Encode offload target triples into the registration function name. It |
| 3797 | // will serve as a comdat key for the registration/unregistration code for |
| 3798 | // this particular combination of offloading targets. |
| 3799 | SmallVector<StringRef, 4U> RegFnNameParts(Devices.size() + 2U); |
| 3800 | RegFnNameParts[0] = "omp_offloading"; |
| 3801 | RegFnNameParts[1] = "descriptor_reg"; |
| 3802 | llvm::transform(Devices, std::next(RegFnNameParts.begin(), 2), |
| 3803 | [](const llvm::Triple &T) -> const std::string& { |
| 3804 | return T.getTriple(); |
| 3805 | }); |
| 3806 | llvm::sort(std::next(RegFnNameParts.begin(), 2), RegFnNameParts.end()); |
| 3807 | std::string Descriptor = getName(RegFnNameParts); |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 3808 | RegFn = CGM.CreateGlobalInitOrDestructFunction(FTy, Descriptor, FI); |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 3809 | CGF.StartFunction(GlobalDecl(), C.VoidTy, RegFn, FI, FunctionArgList()); |
| 3810 | CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__tgt_register_lib), Desc); |
| 3811 | // Create a variable to drive the registration and unregistration of the |
| 3812 | // descriptor, so we can reuse the logic that emits Ctors and Dtors. |
| 3813 | ImplicitParamDecl RegUnregVar(C, C.getTranslationUnitDecl(), |
| 3814 | SourceLocation(), nullptr, C.CharTy, |
| 3815 | ImplicitParamDecl::Other); |
| 3816 | CGM.getCXXABI().registerGlobalDtor(CGF, RegUnregVar, UnRegFn, Desc); |
| 3817 | CGF.FinishFunction(); |
| 3818 | } |
George Rokos | 29d0f00 | 2017-05-27 03:03:13 +0000 | [diff] [blame] | 3819 | if (CGM.supportsCOMDAT()) { |
| 3820 | // It is sufficient to call registration function only once, so create a |
| 3821 | // COMDAT group for registration/unregistration functions and associated |
| 3822 | // data. That would reduce startup time and code size. Registration |
| 3823 | // function serves as a COMDAT group key. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3824 | llvm::Comdat *ComdatKey = M.getOrInsertComdat(RegFn->getName()); |
George Rokos | 29d0f00 | 2017-05-27 03:03:13 +0000 | [diff] [blame] | 3825 | RegFn->setLinkage(llvm::GlobalValue::LinkOnceAnyLinkage); |
| 3826 | RegFn->setVisibility(llvm::GlobalValue::HiddenVisibility); |
| 3827 | RegFn->setComdat(ComdatKey); |
| 3828 | UnRegFn->setComdat(ComdatKey); |
| 3829 | DeviceImages->setComdat(ComdatKey); |
| 3830 | Desc->setComdat(ComdatKey); |
| 3831 | } |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3832 | return RegFn; |
| 3833 | } |
| 3834 | |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 3835 | void CGOpenMPRuntime::createOffloadEntry( |
| 3836 | llvm::Constant *ID, llvm::Constant *Addr, uint64_t Size, int32_t Flags, |
| 3837 | llvm::GlobalValue::LinkageTypes Linkage) { |
Samuel Antao | 2de62b0 | 2016-02-13 23:35:10 +0000 | [diff] [blame] | 3838 | StringRef Name = Addr->getName(); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3839 | llvm::Module &M = CGM.getModule(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3840 | llvm::LLVMContext &C = M.getContext(); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3841 | |
| 3842 | // Create constant string with the name. |
| 3843 | llvm::Constant *StrPtrInit = llvm::ConstantDataArray::getString(C, Name); |
| 3844 | |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 3845 | std::string StringName = getName({"omp_offloading", "entry_name"}); |
| 3846 | auto *Str = new llvm::GlobalVariable( |
| 3847 | M, StrPtrInit->getType(), /*isConstant=*/true, |
| 3848 | llvm::GlobalValue::InternalLinkage, StrPtrInit, StringName); |
Peter Collingbourne | bcf909d | 2016-06-14 21:02:05 +0000 | [diff] [blame] | 3849 | Str->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3850 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3851 | llvm::Constant *Data[] = {llvm::ConstantExpr::getBitCast(ID, CGM.VoidPtrTy), |
| 3852 | llvm::ConstantExpr::getBitCast(Str, CGM.Int8PtrTy), |
| 3853 | llvm::ConstantInt::get(CGM.SizeTy, Size), |
| 3854 | llvm::ConstantInt::get(CGM.Int32Ty, Flags), |
| 3855 | llvm::ConstantInt::get(CGM.Int32Ty, 0)}; |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 3856 | std::string EntryName = getName({"omp_offloading", "entry", ""}); |
Mike Rice | e1ca7b6 | 2018-08-29 15:45:11 +0000 | [diff] [blame] | 3857 | llvm::GlobalVariable *Entry = createGlobalStruct( |
| 3858 | CGM, getTgtOffloadEntryQTy(), /*IsConstant=*/true, Data, |
| 3859 | Twine(EntryName).concat(Name), llvm::GlobalValue::WeakAnyLinkage); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3860 | |
| 3861 | // 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] | 3862 | std::string Section = getName({"omp_offloading", "entries"}); |
| 3863 | Entry->setSection(Section); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3864 | } |
| 3865 | |
| 3866 | void CGOpenMPRuntime::createOffloadEntriesAndInfoMetadata() { |
| 3867 | // Emit the offloading entries and metadata so that the device codegen side |
Samuel Antao | 4c8035b | 2016-12-12 18:00:20 +0000 | [diff] [blame] | 3868 | // can easily figure out what to emit. The produced metadata looks like |
| 3869 | // this: |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3870 | // |
| 3871 | // !omp_offload.info = !{!1, ...} |
| 3872 | // |
| 3873 | // Right now we only generate metadata for function that contain target |
| 3874 | // regions. |
| 3875 | |
Alexander Kornienko | 2a8c18d | 2018-04-06 15:14:32 +0000 | [diff] [blame] | 3876 | // 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] | 3877 | if (OffloadEntriesInfoManager.empty()) |
| 3878 | return; |
| 3879 | |
| 3880 | llvm::Module &M = CGM.getModule(); |
| 3881 | llvm::LLVMContext &C = M.getContext(); |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 3882 | SmallVector<const OffloadEntriesInfoManagerTy::OffloadEntryInfo *, 16> |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3883 | OrderedEntries(OffloadEntriesInfoManager.size()); |
| 3884 | |
Simon Pilgrim | 2c51880 | 2017-03-30 14:13:19 +0000 | [diff] [blame] | 3885 | // Auxiliary methods to create metadata values and strings. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3886 | auto &&GetMDInt = [this](unsigned V) { |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3887 | return llvm::ConstantAsMetadata::get( |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3888 | llvm::ConstantInt::get(CGM.Int32Ty, V)); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3889 | }; |
| 3890 | |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 3891 | auto &&GetMDString = [&C](StringRef V) { return llvm::MDString::get(C, V); }; |
| 3892 | |
| 3893 | // Create the offloading info metadata node. |
| 3894 | llvm::NamedMDNode *MD = M.getOrInsertNamedMetadata("omp_offload.info"); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3895 | |
| 3896 | // Create function that emits metadata for each target region entry; |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 3897 | auto &&TargetRegionMetadataEmitter = |
| 3898 | [&C, MD, &OrderedEntries, &GetMDInt, &GetMDString]( |
| 3899 | unsigned DeviceID, unsigned FileID, StringRef ParentName, |
| 3900 | unsigned Line, |
| 3901 | const OffloadEntriesInfoManagerTy::OffloadEntryInfoTargetRegion &E) { |
| 3902 | // Generate metadata for target regions. Each entry of this metadata |
| 3903 | // contains: |
| 3904 | // - Entry 0 -> Kind of this type of metadata (0). |
| 3905 | // - Entry 1 -> Device ID of the file where the entry was identified. |
| 3906 | // - Entry 2 -> File ID of the file where the entry was identified. |
| 3907 | // - Entry 3 -> Mangled name of the function where the entry was |
| 3908 | // identified. |
| 3909 | // - Entry 4 -> Line in the file where the entry was identified. |
| 3910 | // - Entry 5 -> Order the entry was created. |
| 3911 | // The first element of the metadata node is the kind. |
| 3912 | llvm::Metadata *Ops[] = {GetMDInt(E.getKind()), GetMDInt(DeviceID), |
| 3913 | GetMDInt(FileID), GetMDString(ParentName), |
| 3914 | GetMDInt(Line), GetMDInt(E.getOrder())}; |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3915 | |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 3916 | // Save this entry in the right position of the ordered entries array. |
| 3917 | OrderedEntries[E.getOrder()] = &E; |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3918 | |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 3919 | // Add metadata to the named metadata node. |
| 3920 | MD->addOperand(llvm::MDNode::get(C, Ops)); |
| 3921 | }; |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3922 | |
| 3923 | OffloadEntriesInfoManager.actOnTargetRegionEntriesInfo( |
| 3924 | TargetRegionMetadataEmitter); |
| 3925 | |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 3926 | // Create function that emits metadata for each device global variable entry; |
| 3927 | auto &&DeviceGlobalVarMetadataEmitter = |
| 3928 | [&C, &OrderedEntries, &GetMDInt, &GetMDString, |
| 3929 | MD](StringRef MangledName, |
| 3930 | const OffloadEntriesInfoManagerTy::OffloadEntryInfoDeviceGlobalVar |
| 3931 | &E) { |
| 3932 | // Generate metadata for global variables. Each entry of this metadata |
| 3933 | // contains: |
| 3934 | // - Entry 0 -> Kind of this type of metadata (1). |
| 3935 | // - Entry 1 -> Mangled name of the variable. |
| 3936 | // - Entry 2 -> Declare target kind. |
| 3937 | // - Entry 3 -> Order the entry was created. |
| 3938 | // The first element of the metadata node is the kind. |
| 3939 | llvm::Metadata *Ops[] = { |
| 3940 | GetMDInt(E.getKind()), GetMDString(MangledName), |
| 3941 | GetMDInt(E.getFlags()), GetMDInt(E.getOrder())}; |
| 3942 | |
| 3943 | // Save this entry in the right position of the ordered entries array. |
| 3944 | OrderedEntries[E.getOrder()] = &E; |
| 3945 | |
| 3946 | // Add metadata to the named metadata node. |
| 3947 | MD->addOperand(llvm::MDNode::get(C, Ops)); |
| 3948 | }; |
| 3949 | |
| 3950 | OffloadEntriesInfoManager.actOnDeviceGlobalVarEntriesInfo( |
| 3951 | DeviceGlobalVarMetadataEmitter); |
| 3952 | |
| 3953 | for (const auto *E : OrderedEntries) { |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3954 | assert(E && "All ordered entries must exist!"); |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 3955 | if (const auto *CE = |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3956 | dyn_cast<OffloadEntriesInfoManagerTy::OffloadEntryInfoTargetRegion>( |
| 3957 | E)) { |
Alexey Bataev | 64e62dc | 2018-04-30 16:26:57 +0000 | [diff] [blame] | 3958 | if (!CE->getID() || !CE->getAddress()) { |
| 3959 | unsigned DiagID = CGM.getDiags().getCustomDiagID( |
| 3960 | DiagnosticsEngine::Error, |
Alexey Bataev | 7f01d20 | 2018-07-16 18:12:18 +0000 | [diff] [blame] | 3961 | "Offloading entry for target region is incorrect: either the " |
Alexey Bataev | 64e62dc | 2018-04-30 16:26:57 +0000 | [diff] [blame] | 3962 | "address or the ID is invalid."); |
| 3963 | CGM.getDiags().Report(DiagID); |
| 3964 | continue; |
| 3965 | } |
Alexey Bataev | 34f8a70 | 2018-03-28 14:28:54 +0000 | [diff] [blame] | 3966 | createOffloadEntry(CE->getID(), CE->getAddress(), /*Size=*/0, |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 3967 | CE->getFlags(), llvm::GlobalValue::WeakAnyLinkage); |
| 3968 | } else if (const auto *CE = |
| 3969 | dyn_cast<OffloadEntriesInfoManagerTy:: |
| 3970 | OffloadEntryInfoDeviceGlobalVar>(E)) { |
Alexey Bataev | c52f01d | 2018-07-16 20:05:25 +0000 | [diff] [blame] | 3971 | OffloadEntriesInfoManagerTy::OMPTargetGlobalVarEntryKind Flags = |
| 3972 | static_cast<OffloadEntriesInfoManagerTy::OMPTargetGlobalVarEntryKind>( |
| 3973 | CE->getFlags()); |
| 3974 | switch (Flags) { |
| 3975 | case OffloadEntriesInfoManagerTy::OMPTargetGlobalVarEntryTo: { |
| 3976 | if (!CE->getAddress()) { |
| 3977 | unsigned DiagID = CGM.getDiags().getCustomDiagID( |
| 3978 | DiagnosticsEngine::Error, |
| 3979 | "Offloading entry for declare target variable is incorrect: the " |
| 3980 | "address is invalid."); |
| 3981 | CGM.getDiags().Report(DiagID); |
| 3982 | continue; |
| 3983 | } |
Alexey Bataev | b4dd6d2 | 2018-08-29 20:41:37 +0000 | [diff] [blame] | 3984 | // The vaiable has no definition - no need to add the entry. |
| 3985 | if (CE->getVarSize().isZero()) |
| 3986 | continue; |
Alexey Bataev | c52f01d | 2018-07-16 20:05:25 +0000 | [diff] [blame] | 3987 | break; |
| 3988 | } |
| 3989 | case OffloadEntriesInfoManagerTy::OMPTargetGlobalVarEntryLink: |
| 3990 | assert(((CGM.getLangOpts().OpenMPIsDevice && !CE->getAddress()) || |
| 3991 | (!CGM.getLangOpts().OpenMPIsDevice && CE->getAddress())) && |
| 3992 | "Declaret target link address is set."); |
| 3993 | if (CGM.getLangOpts().OpenMPIsDevice) |
| 3994 | continue; |
| 3995 | if (!CE->getAddress()) { |
| 3996 | unsigned DiagID = CGM.getDiags().getCustomDiagID( |
| 3997 | DiagnosticsEngine::Error, |
| 3998 | "Offloading entry for declare target variable is incorrect: the " |
| 3999 | "address is invalid."); |
| 4000 | CGM.getDiags().Report(DiagID); |
| 4001 | continue; |
| 4002 | } |
| 4003 | break; |
Alexey Bataev | 64e62dc | 2018-04-30 16:26:57 +0000 | [diff] [blame] | 4004 | } |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 4005 | createOffloadEntry(CE->getAddress(), CE->getAddress(), |
Alexey Bataev | c52f01d | 2018-07-16 20:05:25 +0000 | [diff] [blame] | 4006 | CE->getVarSize().getQuantity(), Flags, |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 4007 | CE->getLinkage()); |
| 4008 | } else { |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4009 | llvm_unreachable("Unsupported entry kind."); |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 4010 | } |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4011 | } |
| 4012 | } |
| 4013 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4014 | /// Loads all the offload entries information from the host IR |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4015 | /// metadata. |
| 4016 | void CGOpenMPRuntime::loadOffloadInfoMetadata() { |
| 4017 | // If we are in target mode, load the metadata from the host IR. This code has |
| 4018 | // to match the metadaata creation in createOffloadEntriesAndInfoMetadata(). |
| 4019 | |
| 4020 | if (!CGM.getLangOpts().OpenMPIsDevice) |
| 4021 | return; |
| 4022 | |
| 4023 | if (CGM.getLangOpts().OMPHostIRFile.empty()) |
| 4024 | return; |
| 4025 | |
| 4026 | auto Buf = llvm::MemoryBuffer::getFile(CGM.getLangOpts().OMPHostIRFile); |
Alexey Bataev | 64e62dc | 2018-04-30 16:26:57 +0000 | [diff] [blame] | 4027 | if (auto EC = Buf.getError()) { |
| 4028 | CGM.getDiags().Report(diag::err_cannot_open_file) |
| 4029 | << CGM.getLangOpts().OMPHostIRFile << EC.message(); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4030 | return; |
Alexey Bataev | 64e62dc | 2018-04-30 16:26:57 +0000 | [diff] [blame] | 4031 | } |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4032 | |
| 4033 | llvm::LLVMContext C; |
Peter Collingbourne | d9445c4 | 2016-11-13 07:00:17 +0000 | [diff] [blame] | 4034 | auto ME = expectedToErrorOrAndEmitErrors( |
| 4035 | C, llvm::parseBitcodeFile(Buf.get()->getMemBufferRef(), C)); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4036 | |
Alexey Bataev | 64e62dc | 2018-04-30 16:26:57 +0000 | [diff] [blame] | 4037 | if (auto EC = ME.getError()) { |
| 4038 | unsigned DiagID = CGM.getDiags().getCustomDiagID( |
| 4039 | DiagnosticsEngine::Error, "Unable to parse host IR file '%0':'%1'"); |
| 4040 | CGM.getDiags().Report(DiagID) |
| 4041 | << CGM.getLangOpts().OMPHostIRFile << EC.message(); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4042 | return; |
Alexey Bataev | 64e62dc | 2018-04-30 16:26:57 +0000 | [diff] [blame] | 4043 | } |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4044 | |
| 4045 | llvm::NamedMDNode *MD = ME.get()->getNamedMetadata("omp_offload.info"); |
| 4046 | if (!MD) |
| 4047 | return; |
| 4048 | |
George Burgess IV | 00f70bd | 2018-03-01 05:43:23 +0000 | [diff] [blame] | 4049 | for (llvm::MDNode *MN : MD->operands()) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4050 | auto &&GetMDInt = [MN](unsigned Idx) { |
| 4051 | auto *V = cast<llvm::ConstantAsMetadata>(MN->getOperand(Idx)); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4052 | return cast<llvm::ConstantInt>(V->getValue())->getZExtValue(); |
| 4053 | }; |
| 4054 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4055 | auto &&GetMDString = [MN](unsigned Idx) { |
| 4056 | auto *V = cast<llvm::MDString>(MN->getOperand(Idx)); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4057 | return V->getString(); |
| 4058 | }; |
| 4059 | |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 4060 | switch (GetMDInt(0)) { |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4061 | default: |
| 4062 | llvm_unreachable("Unexpected metadata!"); |
| 4063 | break; |
| 4064 | case OffloadEntriesInfoManagerTy::OffloadEntryInfo:: |
Alexey Bataev | 34f8a70 | 2018-03-28 14:28:54 +0000 | [diff] [blame] | 4065 | OffloadingEntryInfoTargetRegion: |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4066 | OffloadEntriesInfoManager.initializeTargetRegionEntryInfo( |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 4067 | /*DeviceID=*/GetMDInt(1), /*FileID=*/GetMDInt(2), |
| 4068 | /*ParentName=*/GetMDString(3), /*Line=*/GetMDInt(4), |
| 4069 | /*Order=*/GetMDInt(5)); |
| 4070 | break; |
| 4071 | case OffloadEntriesInfoManagerTy::OffloadEntryInfo:: |
| 4072 | OffloadingEntryInfoDeviceGlobalVar: |
| 4073 | OffloadEntriesInfoManager.initializeDeviceGlobalVarEntryInfo( |
| 4074 | /*MangledName=*/GetMDString(1), |
| 4075 | static_cast<OffloadEntriesInfoManagerTy::OMPTargetGlobalVarEntryKind>( |
| 4076 | /*Flags=*/GetMDInt(2)), |
| 4077 | /*Order=*/GetMDInt(3)); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4078 | break; |
| 4079 | } |
| 4080 | } |
| 4081 | } |
| 4082 | |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 4083 | void CGOpenMPRuntime::emitKmpRoutineEntryT(QualType KmpInt32Ty) { |
| 4084 | if (!KmpRoutineEntryPtrTy) { |
| 4085 | // Build typedef kmp_int32 (* kmp_routine_entry_t)(kmp_int32, void *); type. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4086 | ASTContext &C = CGM.getContext(); |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 4087 | QualType KmpRoutineEntryTyArgs[] = {KmpInt32Ty, C.VoidPtrTy}; |
| 4088 | FunctionProtoType::ExtProtoInfo EPI; |
| 4089 | KmpRoutineEntryPtrQTy = C.getPointerType( |
| 4090 | C.getFunctionType(KmpInt32Ty, KmpRoutineEntryTyArgs, EPI)); |
| 4091 | KmpRoutineEntryPtrTy = CGM.getTypes().ConvertType(KmpRoutineEntryPtrQTy); |
| 4092 | } |
| 4093 | } |
| 4094 | |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4095 | QualType CGOpenMPRuntime::getTgtOffloadEntryQTy() { |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4096 | // Make sure the type of the entry is already created. This is the type we |
| 4097 | // have to create: |
| 4098 | // struct __tgt_offload_entry{ |
| 4099 | // void *addr; // Pointer to the offload entry info. |
| 4100 | // // (function or global) |
| 4101 | // char *name; // Name of the function or global. |
| 4102 | // 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] | 4103 | // int32_t flags; // Flags associated with the entry, e.g. 'link'. |
| 4104 | // int32_t reserved; // Reserved, to use by the runtime library. |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4105 | // }; |
| 4106 | if (TgtOffloadEntryQTy.isNull()) { |
| 4107 | ASTContext &C = CGM.getContext(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4108 | RecordDecl *RD = C.buildImplicitRecord("__tgt_offload_entry"); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4109 | RD->startDefinition(); |
| 4110 | addFieldToRecordDecl(C, RD, C.VoidPtrTy); |
| 4111 | addFieldToRecordDecl(C, RD, C.getPointerType(C.CharTy)); |
| 4112 | addFieldToRecordDecl(C, RD, C.getSizeType()); |
Samuel Antao | f83efdb | 2017-01-05 16:02:49 +0000 | [diff] [blame] | 4113 | addFieldToRecordDecl( |
| 4114 | C, RD, C.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/true)); |
| 4115 | addFieldToRecordDecl( |
| 4116 | C, RD, C.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/true)); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4117 | RD->completeDefinition(); |
Jonas Hahnfeld | 5e4df28 | 2018-01-18 15:38:03 +0000 | [diff] [blame] | 4118 | RD->addAttr(PackedAttr::CreateImplicit(C)); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4119 | TgtOffloadEntryQTy = C.getRecordType(RD); |
| 4120 | } |
| 4121 | return TgtOffloadEntryQTy; |
| 4122 | } |
| 4123 | |
| 4124 | QualType CGOpenMPRuntime::getTgtDeviceImageQTy() { |
| 4125 | // These are the types we need to build: |
| 4126 | // struct __tgt_device_image{ |
| 4127 | // void *ImageStart; // Pointer to the target code start. |
| 4128 | // void *ImageEnd; // Pointer to the target code end. |
| 4129 | // // We also add the host entries to the device image, as it may be useful |
| 4130 | // // for the target runtime to have access to that information. |
| 4131 | // __tgt_offload_entry *EntriesBegin; // Begin of the table with all |
| 4132 | // // the entries. |
| 4133 | // __tgt_offload_entry *EntriesEnd; // End of the table with all the |
| 4134 | // // entries (non inclusive). |
| 4135 | // }; |
| 4136 | if (TgtDeviceImageQTy.isNull()) { |
| 4137 | ASTContext &C = CGM.getContext(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4138 | RecordDecl *RD = C.buildImplicitRecord("__tgt_device_image"); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4139 | RD->startDefinition(); |
| 4140 | addFieldToRecordDecl(C, RD, C.VoidPtrTy); |
| 4141 | addFieldToRecordDecl(C, RD, C.VoidPtrTy); |
| 4142 | addFieldToRecordDecl(C, RD, C.getPointerType(getTgtOffloadEntryQTy())); |
| 4143 | addFieldToRecordDecl(C, RD, C.getPointerType(getTgtOffloadEntryQTy())); |
| 4144 | RD->completeDefinition(); |
| 4145 | TgtDeviceImageQTy = C.getRecordType(RD); |
| 4146 | } |
| 4147 | return TgtDeviceImageQTy; |
| 4148 | } |
| 4149 | |
| 4150 | QualType CGOpenMPRuntime::getTgtBinaryDescriptorQTy() { |
| 4151 | // struct __tgt_bin_desc{ |
| 4152 | // int32_t NumDevices; // Number of devices supported. |
| 4153 | // __tgt_device_image *DeviceImages; // Arrays of device images |
| 4154 | // // (one per device). |
| 4155 | // __tgt_offload_entry *EntriesBegin; // Begin of the table with all the |
| 4156 | // // entries. |
| 4157 | // __tgt_offload_entry *EntriesEnd; // End of the table with all the |
| 4158 | // // entries (non inclusive). |
| 4159 | // }; |
| 4160 | if (TgtBinaryDescriptorQTy.isNull()) { |
| 4161 | ASTContext &C = CGM.getContext(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4162 | RecordDecl *RD = C.buildImplicitRecord("__tgt_bin_desc"); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4163 | RD->startDefinition(); |
| 4164 | addFieldToRecordDecl( |
| 4165 | C, RD, C.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/true)); |
| 4166 | addFieldToRecordDecl(C, RD, C.getPointerType(getTgtDeviceImageQTy())); |
| 4167 | addFieldToRecordDecl(C, RD, C.getPointerType(getTgtOffloadEntryQTy())); |
| 4168 | addFieldToRecordDecl(C, RD, C.getPointerType(getTgtOffloadEntryQTy())); |
| 4169 | RD->completeDefinition(); |
| 4170 | TgtBinaryDescriptorQTy = C.getRecordType(RD); |
| 4171 | } |
| 4172 | return TgtBinaryDescriptorQTy; |
| 4173 | } |
| 4174 | |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 4175 | namespace { |
Alexey Bataev | 9e03404 | 2015-05-05 04:05:12 +0000 | [diff] [blame] | 4176 | struct PrivateHelpersTy { |
| 4177 | PrivateHelpersTy(const VarDecl *Original, const VarDecl *PrivateCopy, |
| 4178 | const VarDecl *PrivateElemInit) |
| 4179 | : Original(Original), PrivateCopy(PrivateCopy), |
| 4180 | PrivateElemInit(PrivateElemInit) {} |
| 4181 | const VarDecl *Original; |
| 4182 | const VarDecl *PrivateCopy; |
| 4183 | const VarDecl *PrivateElemInit; |
| 4184 | }; |
| 4185 | typedef std::pair<CharUnits /*Align*/, PrivateHelpersTy> PrivateDataTy; |
Hans Wennborg | 7eb5464 | 2015-09-10 17:07:54 +0000 | [diff] [blame] | 4186 | } // anonymous namespace |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 4187 | |
Alexey Bataev | 9e03404 | 2015-05-05 04:05:12 +0000 | [diff] [blame] | 4188 | static RecordDecl * |
Craig Topper | 8674c5c | 2015-09-29 04:30:07 +0000 | [diff] [blame] | 4189 | createPrivatesRecordDecl(CodeGenModule &CGM, ArrayRef<PrivateDataTy> Privates) { |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 4190 | if (!Privates.empty()) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4191 | ASTContext &C = CGM.getContext(); |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 4192 | // Build struct .kmp_privates_t. { |
| 4193 | // /* private vars */ |
| 4194 | // }; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4195 | RecordDecl *RD = C.buildImplicitRecord(".kmp_privates.t"); |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 4196 | RD->startDefinition(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4197 | for (const auto &Pair : Privates) { |
| 4198 | const VarDecl *VD = Pair.second.Original; |
| 4199 | QualType Type = VD->getType().getNonReferenceType(); |
| 4200 | FieldDecl *FD = addFieldToRecordDecl(C, RD, Type); |
Alexey Bataev | c71a409 | 2015-09-11 10:29:41 +0000 | [diff] [blame] | 4201 | if (VD->hasAttrs()) { |
| 4202 | for (specific_attr_iterator<AlignedAttr> I(VD->getAttrs().begin()), |
| 4203 | E(VD->getAttrs().end()); |
| 4204 | I != E; ++I) |
| 4205 | FD->addAttr(*I); |
| 4206 | } |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 4207 | } |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 4208 | RD->completeDefinition(); |
| 4209 | return RD; |
| 4210 | } |
| 4211 | return nullptr; |
| 4212 | } |
| 4213 | |
Alexey Bataev | 9e03404 | 2015-05-05 04:05:12 +0000 | [diff] [blame] | 4214 | static RecordDecl * |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4215 | createKmpTaskTRecordDecl(CodeGenModule &CGM, OpenMPDirectiveKind Kind, |
| 4216 | QualType KmpInt32Ty, |
Alexey Bataev | 8fc69dc | 2015-05-18 07:54:53 +0000 | [diff] [blame] | 4217 | QualType KmpRoutineEntryPointerQTy) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4218 | ASTContext &C = CGM.getContext(); |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 4219 | // Build struct kmp_task_t { |
| 4220 | // void * shareds; |
| 4221 | // kmp_routine_entry_t routine; |
| 4222 | // kmp_int32 part_id; |
Alexey Bataev | ad537bb | 2016-05-30 09:06:50 +0000 | [diff] [blame] | 4223 | // kmp_cmplrdata_t data1; |
| 4224 | // kmp_cmplrdata_t data2; |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4225 | // For taskloops additional fields: |
| 4226 | // kmp_uint64 lb; |
| 4227 | // kmp_uint64 ub; |
| 4228 | // kmp_int64 st; |
| 4229 | // kmp_int32 liter; |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 4230 | // void * reductions; |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 4231 | // }; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4232 | RecordDecl *UD = C.buildImplicitRecord("kmp_cmplrdata_t", TTK_Union); |
Alexey Bataev | ad537bb | 2016-05-30 09:06:50 +0000 | [diff] [blame] | 4233 | UD->startDefinition(); |
| 4234 | addFieldToRecordDecl(C, UD, KmpInt32Ty); |
| 4235 | addFieldToRecordDecl(C, UD, KmpRoutineEntryPointerQTy); |
| 4236 | UD->completeDefinition(); |
| 4237 | QualType KmpCmplrdataTy = C.getRecordType(UD); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4238 | RecordDecl *RD = C.buildImplicitRecord("kmp_task_t"); |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 4239 | RD->startDefinition(); |
| 4240 | addFieldToRecordDecl(C, RD, C.VoidPtrTy); |
| 4241 | addFieldToRecordDecl(C, RD, KmpRoutineEntryPointerQTy); |
| 4242 | addFieldToRecordDecl(C, RD, KmpInt32Ty); |
Alexey Bataev | ad537bb | 2016-05-30 09:06:50 +0000 | [diff] [blame] | 4243 | addFieldToRecordDecl(C, RD, KmpCmplrdataTy); |
| 4244 | addFieldToRecordDecl(C, RD, KmpCmplrdataTy); |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4245 | if (isOpenMPTaskLoopDirective(Kind)) { |
| 4246 | QualType KmpUInt64Ty = |
| 4247 | CGM.getContext().getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0); |
| 4248 | QualType KmpInt64Ty = |
| 4249 | CGM.getContext().getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1); |
| 4250 | addFieldToRecordDecl(C, RD, KmpUInt64Ty); |
| 4251 | addFieldToRecordDecl(C, RD, KmpUInt64Ty); |
| 4252 | addFieldToRecordDecl(C, RD, KmpInt64Ty); |
| 4253 | addFieldToRecordDecl(C, RD, KmpInt32Ty); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 4254 | addFieldToRecordDecl(C, RD, C.VoidPtrTy); |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4255 | } |
Alexey Bataev | 8fc69dc | 2015-05-18 07:54:53 +0000 | [diff] [blame] | 4256 | RD->completeDefinition(); |
| 4257 | return RD; |
| 4258 | } |
| 4259 | |
| 4260 | static RecordDecl * |
| 4261 | createKmpTaskTWithPrivatesRecordDecl(CodeGenModule &CGM, QualType KmpTaskTQTy, |
Craig Topper | 8674c5c | 2015-09-29 04:30:07 +0000 | [diff] [blame] | 4262 | ArrayRef<PrivateDataTy> Privates) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4263 | ASTContext &C = CGM.getContext(); |
Alexey Bataev | 8fc69dc | 2015-05-18 07:54:53 +0000 | [diff] [blame] | 4264 | // Build struct kmp_task_t_with_privates { |
| 4265 | // kmp_task_t task_data; |
| 4266 | // .kmp_privates_t. privates; |
| 4267 | // }; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4268 | RecordDecl *RD = C.buildImplicitRecord("kmp_task_t_with_privates"); |
Alexey Bataev | 8fc69dc | 2015-05-18 07:54:53 +0000 | [diff] [blame] | 4269 | RD->startDefinition(); |
| 4270 | addFieldToRecordDecl(C, RD, KmpTaskTQTy); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4271 | if (const RecordDecl *PrivateRD = createPrivatesRecordDecl(CGM, Privates)) |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 4272 | addFieldToRecordDecl(C, RD, C.getRecordType(PrivateRD)); |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 4273 | RD->completeDefinition(); |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 4274 | return RD; |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 4275 | } |
| 4276 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4277 | /// Emit a proxy function which accepts kmp_task_t as the second |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 4278 | /// argument. |
| 4279 | /// \code |
| 4280 | /// kmp_int32 .omp_task_entry.(kmp_int32 gtid, kmp_task_t *tt) { |
Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 4281 | /// TaskFunction(gtid, tt->part_id, &tt->privates, task_privates_map, tt, |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4282 | /// For taskloops: |
| 4283 | /// 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] | 4284 | /// tt->reductions, tt->shareds); |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 4285 | /// return 0; |
| 4286 | /// } |
| 4287 | /// \endcode |
| 4288 | static llvm::Value * |
| 4289 | emitProxyTaskFunction(CodeGenModule &CGM, SourceLocation Loc, |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4290 | OpenMPDirectiveKind Kind, QualType KmpInt32Ty, |
| 4291 | QualType KmpTaskTWithPrivatesPtrQTy, |
Alexey Bataev | 8fc69dc | 2015-05-18 07:54:53 +0000 | [diff] [blame] | 4292 | QualType KmpTaskTWithPrivatesQTy, QualType KmpTaskTQTy, |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4293 | QualType SharedsPtrTy, llvm::Value *TaskFunction, |
| 4294 | llvm::Value *TaskPrivatesMap) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4295 | ASTContext &C = CGM.getContext(); |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 4296 | FunctionArgList Args; |
Alexey Bataev | 5622323 | 2017-06-09 13:40:18 +0000 | [diff] [blame] | 4297 | ImplicitParamDecl GtidArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, KmpInt32Ty, |
| 4298 | ImplicitParamDecl::Other); |
| 4299 | ImplicitParamDecl TaskTypeArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, |
| 4300 | KmpTaskTWithPrivatesPtrQTy.withRestrict(), |
| 4301 | ImplicitParamDecl::Other); |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 4302 | Args.push_back(&GtidArg); |
| 4303 | Args.push_back(&TaskTypeArg); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4304 | const auto &TaskEntryFnInfo = |
John McCall | c56a8b3 | 2016-03-11 04:30:31 +0000 | [diff] [blame] | 4305 | CGM.getTypes().arrangeBuiltinFunctionDeclaration(KmpInt32Ty, Args); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4306 | llvm::FunctionType *TaskEntryTy = |
| 4307 | CGM.getTypes().GetFunctionType(TaskEntryFnInfo); |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 4308 | std::string Name = CGM.getOpenMPRuntime().getName({"omp_task_entry", ""}); |
| 4309 | auto *TaskEntry = llvm::Function::Create( |
| 4310 | TaskEntryTy, llvm::GlobalValue::InternalLinkage, Name, &CGM.getModule()); |
Rafael Espindola | 51ec5a9 | 2018-02-28 23:46:35 +0000 | [diff] [blame] | 4311 | CGM.SetInternalFunctionAttributes(GlobalDecl(), TaskEntry, TaskEntryFnInfo); |
Alexey Bataev | c0f879b | 2018-04-10 20:10:53 +0000 | [diff] [blame] | 4312 | TaskEntry->setDoesNotRecurse(); |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 4313 | CodeGenFunction CGF(CGM); |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 4314 | CGF.StartFunction(GlobalDecl(), KmpInt32Ty, TaskEntry, TaskEntryFnInfo, Args, |
| 4315 | Loc, Loc); |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 4316 | |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4317 | // TaskFunction(gtid, tt->task_data.part_id, &tt->privates, task_privates_map, |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4318 | // tt, |
| 4319 | // For taskloops: |
| 4320 | // tt->task_data.lb, tt->task_data.ub, tt->task_data.st, tt->task_data.liter, |
| 4321 | // tt->task_data.shareds); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4322 | llvm::Value *GtidParam = CGF.EmitLoadOfScalar( |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4323 | CGF.GetAddrOfLocalVar(&GtidArg), /*Volatile=*/false, KmpInt32Ty, Loc); |
Alexey Bataev | 31300ed | 2016-02-04 11:27:03 +0000 | [diff] [blame] | 4324 | LValue TDBase = CGF.EmitLoadOfPointerLValue( |
| 4325 | CGF.GetAddrOfLocalVar(&TaskTypeArg), |
| 4326 | KmpTaskTWithPrivatesPtrQTy->castAs<PointerType>()); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4327 | const auto *KmpTaskTWithPrivatesQTyRD = |
Alexey Bataev | 8fc69dc | 2015-05-18 07:54:53 +0000 | [diff] [blame] | 4328 | cast<RecordDecl>(KmpTaskTWithPrivatesQTy->getAsTagDecl()); |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4329 | LValue Base = |
| 4330 | CGF.EmitLValueForField(TDBase, *KmpTaskTWithPrivatesQTyRD->field_begin()); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4331 | const auto *KmpTaskTQTyRD = cast<RecordDecl>(KmpTaskTQTy->getAsTagDecl()); |
Alexey Bataev | 8fc69dc | 2015-05-18 07:54:53 +0000 | [diff] [blame] | 4332 | auto PartIdFI = std::next(KmpTaskTQTyRD->field_begin(), KmpTaskTPartId); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4333 | LValue PartIdLVal = CGF.EmitLValueForField(Base, *PartIdFI); |
| 4334 | llvm::Value *PartidParam = PartIdLVal.getPointer(); |
Alexey Bataev | 8fc69dc | 2015-05-18 07:54:53 +0000 | [diff] [blame] | 4335 | |
| 4336 | auto SharedsFI = std::next(KmpTaskTQTyRD->field_begin(), KmpTaskTShareds); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4337 | LValue SharedsLVal = CGF.EmitLValueForField(Base, *SharedsFI); |
| 4338 | llvm::Value *SharedsParam = CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
Alexey Bataev | 1e49137 | 2018-01-23 18:44:14 +0000 | [diff] [blame] | 4339 | CGF.EmitLoadOfScalar(SharedsLVal, Loc), |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 4340 | CGF.ConvertTypeForMem(SharedsPtrTy)); |
| 4341 | |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4342 | auto PrivatesFI = std::next(KmpTaskTWithPrivatesQTyRD->field_begin(), 1); |
| 4343 | llvm::Value *PrivatesParam; |
| 4344 | if (PrivatesFI != KmpTaskTWithPrivatesQTyRD->field_end()) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4345 | LValue PrivatesLVal = CGF.EmitLValueForField(TDBase, *PrivatesFI); |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4346 | PrivatesParam = CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4347 | PrivatesLVal.getPointer(), CGF.VoidPtrTy); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4348 | } else { |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4349 | PrivatesParam = llvm::ConstantPointerNull::get(CGF.VoidPtrTy); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4350 | } |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4351 | |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4352 | llvm::Value *CommonArgs[] = {GtidParam, PartidParam, PrivatesParam, |
| 4353 | TaskPrivatesMap, |
| 4354 | CGF.Builder |
| 4355 | .CreatePointerBitCastOrAddrSpaceCast( |
| 4356 | TDBase.getAddress(), CGF.VoidPtrTy) |
| 4357 | .getPointer()}; |
| 4358 | SmallVector<llvm::Value *, 16> CallArgs(std::begin(CommonArgs), |
| 4359 | std::end(CommonArgs)); |
| 4360 | if (isOpenMPTaskLoopDirective(Kind)) { |
| 4361 | auto LBFI = std::next(KmpTaskTQTyRD->field_begin(), KmpTaskTLowerBound); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4362 | LValue LBLVal = CGF.EmitLValueForField(Base, *LBFI); |
| 4363 | llvm::Value *LBParam = CGF.EmitLoadOfScalar(LBLVal, Loc); |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4364 | auto UBFI = std::next(KmpTaskTQTyRD->field_begin(), KmpTaskTUpperBound); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4365 | LValue UBLVal = CGF.EmitLValueForField(Base, *UBFI); |
| 4366 | llvm::Value *UBParam = CGF.EmitLoadOfScalar(UBLVal, Loc); |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4367 | auto StFI = std::next(KmpTaskTQTyRD->field_begin(), KmpTaskTStride); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4368 | LValue StLVal = CGF.EmitLValueForField(Base, *StFI); |
| 4369 | llvm::Value *StParam = CGF.EmitLoadOfScalar(StLVal, Loc); |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4370 | auto LIFI = std::next(KmpTaskTQTyRD->field_begin(), KmpTaskTLastIter); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4371 | LValue LILVal = CGF.EmitLValueForField(Base, *LIFI); |
| 4372 | llvm::Value *LIParam = CGF.EmitLoadOfScalar(LILVal, Loc); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 4373 | auto RFI = std::next(KmpTaskTQTyRD->field_begin(), KmpTaskTReductions); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4374 | LValue RLVal = CGF.EmitLValueForField(Base, *RFI); |
| 4375 | llvm::Value *RParam = CGF.EmitLoadOfScalar(RLVal, Loc); |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4376 | CallArgs.push_back(LBParam); |
| 4377 | CallArgs.push_back(UBParam); |
| 4378 | CallArgs.push_back(StParam); |
| 4379 | CallArgs.push_back(LIParam); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 4380 | CallArgs.push_back(RParam); |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4381 | } |
| 4382 | CallArgs.push_back(SharedsParam); |
| 4383 | |
Alexey Bataev | 3c595a6 | 2017-08-14 15:01:03 +0000 | [diff] [blame] | 4384 | CGM.getOpenMPRuntime().emitOutlinedFunctionCall(CGF, Loc, TaskFunction, |
| 4385 | CallArgs); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4386 | CGF.EmitStoreThroughLValue(RValue::get(CGF.Builder.getInt32(/*C=*/0)), |
| 4387 | CGF.MakeAddrLValue(CGF.ReturnValue, KmpInt32Ty)); |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 4388 | CGF.FinishFunction(); |
| 4389 | return TaskEntry; |
| 4390 | } |
| 4391 | |
Alexey Bataev | 8fc69dc | 2015-05-18 07:54:53 +0000 | [diff] [blame] | 4392 | static llvm::Value *emitDestructorsFunction(CodeGenModule &CGM, |
| 4393 | SourceLocation Loc, |
| 4394 | QualType KmpInt32Ty, |
| 4395 | QualType KmpTaskTWithPrivatesPtrQTy, |
| 4396 | QualType KmpTaskTWithPrivatesQTy) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4397 | ASTContext &C = CGM.getContext(); |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 4398 | FunctionArgList Args; |
Alexey Bataev | 5622323 | 2017-06-09 13:40:18 +0000 | [diff] [blame] | 4399 | ImplicitParamDecl GtidArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, KmpInt32Ty, |
| 4400 | ImplicitParamDecl::Other); |
| 4401 | ImplicitParamDecl TaskTypeArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, |
| 4402 | KmpTaskTWithPrivatesPtrQTy.withRestrict(), |
| 4403 | ImplicitParamDecl::Other); |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 4404 | Args.push_back(&GtidArg); |
| 4405 | Args.push_back(&TaskTypeArg); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4406 | const auto &DestructorFnInfo = |
John McCall | c56a8b3 | 2016-03-11 04:30:31 +0000 | [diff] [blame] | 4407 | CGM.getTypes().arrangeBuiltinFunctionDeclaration(KmpInt32Ty, Args); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4408 | llvm::FunctionType *DestructorFnTy = |
| 4409 | CGM.getTypes().GetFunctionType(DestructorFnInfo); |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 4410 | std::string Name = |
| 4411 | CGM.getOpenMPRuntime().getName({"omp_task_destructor", ""}); |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 4412 | auto *DestructorFn = |
| 4413 | llvm::Function::Create(DestructorFnTy, llvm::GlobalValue::InternalLinkage, |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 4414 | Name, &CGM.getModule()); |
Rafael Espindola | 51ec5a9 | 2018-02-28 23:46:35 +0000 | [diff] [blame] | 4415 | CGM.SetInternalFunctionAttributes(GlobalDecl(), DestructorFn, |
Akira Hatanaka | 44a59f8 | 2015-10-28 02:30:47 +0000 | [diff] [blame] | 4416 | DestructorFnInfo); |
Alexey Bataev | c0f879b | 2018-04-10 20:10:53 +0000 | [diff] [blame] | 4417 | DestructorFn->setDoesNotRecurse(); |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 4418 | CodeGenFunction CGF(CGM); |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 4419 | CGF.StartFunction(GlobalDecl(), KmpInt32Ty, DestructorFn, DestructorFnInfo, |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 4420 | Args, Loc, Loc); |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 4421 | |
Alexey Bataev | 31300ed | 2016-02-04 11:27:03 +0000 | [diff] [blame] | 4422 | LValue Base = CGF.EmitLoadOfPointerLValue( |
| 4423 | CGF.GetAddrOfLocalVar(&TaskTypeArg), |
| 4424 | KmpTaskTWithPrivatesPtrQTy->castAs<PointerType>()); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4425 | const auto *KmpTaskTWithPrivatesQTyRD = |
Alexey Bataev | 8fc69dc | 2015-05-18 07:54:53 +0000 | [diff] [blame] | 4426 | cast<RecordDecl>(KmpTaskTWithPrivatesQTy->getAsTagDecl()); |
| 4427 | auto FI = std::next(KmpTaskTWithPrivatesQTyRD->field_begin()); |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 4428 | Base = CGF.EmitLValueForField(Base, *FI); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4429 | for (const auto *Field : |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 4430 | cast<RecordDecl>(FI->getType()->getAsTagDecl())->fields()) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4431 | if (QualType::DestructionKind DtorKind = |
| 4432 | Field->getType().isDestructedType()) { |
| 4433 | LValue FieldLValue = CGF.EmitLValueForField(Base, Field); |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 4434 | CGF.pushDestroy(DtorKind, FieldLValue.getAddress(), Field->getType()); |
| 4435 | } |
| 4436 | } |
| 4437 | CGF.FinishFunction(); |
| 4438 | return DestructorFn; |
| 4439 | } |
| 4440 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4441 | /// Emit a privates mapping function for correct handling of private and |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4442 | /// firstprivate variables. |
| 4443 | /// \code |
| 4444 | /// void .omp_task_privates_map.(const .privates. *noalias privs, <ty1> |
| 4445 | /// **noalias priv1,..., <tyn> **noalias privn) { |
| 4446 | /// *priv1 = &.privates.priv1; |
| 4447 | /// ...; |
| 4448 | /// *privn = &.privates.privn; |
| 4449 | /// } |
| 4450 | /// \endcode |
| 4451 | static llvm::Value * |
| 4452 | emitTaskPrivateMappingFunction(CodeGenModule &CGM, SourceLocation Loc, |
Craig Topper | 8674c5c | 2015-09-29 04:30:07 +0000 | [diff] [blame] | 4453 | ArrayRef<const Expr *> PrivateVars, |
| 4454 | ArrayRef<const Expr *> FirstprivateVars, |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4455 | ArrayRef<const Expr *> LastprivateVars, |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4456 | QualType PrivatesQTy, |
Craig Topper | 8674c5c | 2015-09-29 04:30:07 +0000 | [diff] [blame] | 4457 | ArrayRef<PrivateDataTy> Privates) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4458 | ASTContext &C = CGM.getContext(); |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4459 | FunctionArgList Args; |
| 4460 | ImplicitParamDecl TaskPrivatesArg( |
| 4461 | C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, |
Alexey Bataev | 5622323 | 2017-06-09 13:40:18 +0000 | [diff] [blame] | 4462 | C.getPointerType(PrivatesQTy).withConst().withRestrict(), |
| 4463 | ImplicitParamDecl::Other); |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4464 | Args.push_back(&TaskPrivatesArg); |
| 4465 | llvm::DenseMap<const VarDecl *, unsigned> PrivateVarsPos; |
| 4466 | unsigned Counter = 1; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4467 | for (const Expr *E : PrivateVars) { |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4468 | Args.push_back(ImplicitParamDecl::Create( |
Alexey Bataev | 5622323 | 2017-06-09 13:40:18 +0000 | [diff] [blame] | 4469 | C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, |
| 4470 | C.getPointerType(C.getPointerType(E->getType())) |
| 4471 | .withConst() |
| 4472 | .withRestrict(), |
| 4473 | ImplicitParamDecl::Other)); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4474 | const auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl()); |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4475 | PrivateVarsPos[VD] = Counter; |
| 4476 | ++Counter; |
| 4477 | } |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4478 | for (const Expr *E : FirstprivateVars) { |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4479 | Args.push_back(ImplicitParamDecl::Create( |
Alexey Bataev | 5622323 | 2017-06-09 13:40:18 +0000 | [diff] [blame] | 4480 | C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, |
| 4481 | C.getPointerType(C.getPointerType(E->getType())) |
| 4482 | .withConst() |
| 4483 | .withRestrict(), |
| 4484 | ImplicitParamDecl::Other)); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4485 | const auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl()); |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4486 | PrivateVarsPos[VD] = Counter; |
| 4487 | ++Counter; |
| 4488 | } |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4489 | for (const Expr *E : LastprivateVars) { |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4490 | Args.push_back(ImplicitParamDecl::Create( |
Alexey Bataev | 5622323 | 2017-06-09 13:40:18 +0000 | [diff] [blame] | 4491 | C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, |
| 4492 | C.getPointerType(C.getPointerType(E->getType())) |
| 4493 | .withConst() |
| 4494 | .withRestrict(), |
| 4495 | ImplicitParamDecl::Other)); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4496 | const auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl()); |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4497 | PrivateVarsPos[VD] = Counter; |
| 4498 | ++Counter; |
| 4499 | } |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4500 | const auto &TaskPrivatesMapFnInfo = |
John McCall | c56a8b3 | 2016-03-11 04:30:31 +0000 | [diff] [blame] | 4501 | CGM.getTypes().arrangeBuiltinFunctionDeclaration(C.VoidTy, Args); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4502 | llvm::FunctionType *TaskPrivatesMapTy = |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4503 | CGM.getTypes().GetFunctionType(TaskPrivatesMapFnInfo); |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 4504 | std::string Name = |
| 4505 | CGM.getOpenMPRuntime().getName({"omp_task_privates_map", ""}); |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4506 | auto *TaskPrivatesMap = llvm::Function::Create( |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 4507 | TaskPrivatesMapTy, llvm::GlobalValue::InternalLinkage, Name, |
| 4508 | &CGM.getModule()); |
Rafael Espindola | 51ec5a9 | 2018-02-28 23:46:35 +0000 | [diff] [blame] | 4509 | CGM.SetInternalFunctionAttributes(GlobalDecl(), TaskPrivatesMap, |
Akira Hatanaka | 44a59f8 | 2015-10-28 02:30:47 +0000 | [diff] [blame] | 4510 | TaskPrivatesMapFnInfo); |
Chandler Carruth | fcd3314 | 2016-12-23 01:24:49 +0000 | [diff] [blame] | 4511 | TaskPrivatesMap->removeFnAttr(llvm::Attribute::NoInline); |
Mehdi Amini | 6aa9e9b | 2017-05-29 05:38:20 +0000 | [diff] [blame] | 4512 | TaskPrivatesMap->removeFnAttr(llvm::Attribute::OptimizeNone); |
Evgeniy Stepanov | 6b2a61d | 2015-09-14 21:35:16 +0000 | [diff] [blame] | 4513 | TaskPrivatesMap->addFnAttr(llvm::Attribute::AlwaysInline); |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4514 | CodeGenFunction CGF(CGM); |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4515 | CGF.StartFunction(GlobalDecl(), C.VoidTy, TaskPrivatesMap, |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 4516 | TaskPrivatesMapFnInfo, Args, Loc, Loc); |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4517 | |
| 4518 | // *privi = &.privates.privi; |
Alexey Bataev | 31300ed | 2016-02-04 11:27:03 +0000 | [diff] [blame] | 4519 | LValue Base = CGF.EmitLoadOfPointerLValue( |
| 4520 | CGF.GetAddrOfLocalVar(&TaskPrivatesArg), |
| 4521 | TaskPrivatesArg.getType()->castAs<PointerType>()); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4522 | const auto *PrivatesQTyRD = cast<RecordDecl>(PrivatesQTy->getAsTagDecl()); |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4523 | Counter = 0; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4524 | for (const FieldDecl *Field : PrivatesQTyRD->fields()) { |
| 4525 | LValue FieldLVal = CGF.EmitLValueForField(Base, Field); |
| 4526 | const VarDecl *VD = Args[PrivateVarsPos[Privates[Counter].second.Original]]; |
| 4527 | LValue RefLVal = |
| 4528 | CGF.MakeAddrLValue(CGF.GetAddrOfLocalVar(VD), VD->getType()); |
| 4529 | LValue RefLoadLVal = CGF.EmitLoadOfPointerLValue( |
Alexey Bataev | 31300ed | 2016-02-04 11:27:03 +0000 | [diff] [blame] | 4530 | RefLVal.getAddress(), RefLVal.getType()->castAs<PointerType>()); |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 4531 | CGF.EmitStoreOfScalar(FieldLVal.getPointer(), RefLoadLVal); |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4532 | ++Counter; |
| 4533 | } |
| 4534 | CGF.FinishFunction(); |
| 4535 | return TaskPrivatesMap; |
| 4536 | } |
| 4537 | |
Mandeep Singh Grang | b14fb6a2 | 2017-11-28 20:41:13 +0000 | [diff] [blame] | 4538 | static bool stable_sort_comparator(const PrivateDataTy P1, |
| 4539 | const PrivateDataTy P2) { |
| 4540 | return P1.first > P2.first; |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 4541 | } |
| 4542 | |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4543 | /// Emit initialization for private variables in task-based directives. |
Alexey Bataev | 8a83159 | 2016-05-10 10:36:51 +0000 | [diff] [blame] | 4544 | static void emitPrivatesInit(CodeGenFunction &CGF, |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4545 | const OMPExecutableDirective &D, |
| 4546 | Address KmpTaskSharedsPtr, LValue TDBase, |
| 4547 | const RecordDecl *KmpTaskTWithPrivatesQTyRD, |
| 4548 | QualType SharedsTy, QualType SharedsPtrTy, |
| 4549 | const OMPTaskDataTy &Data, |
| 4550 | ArrayRef<PrivateDataTy> Privates, bool ForDup) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4551 | ASTContext &C = CGF.getContext(); |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4552 | auto FI = std::next(KmpTaskTWithPrivatesQTyRD->field_begin()); |
| 4553 | LValue PrivatesBase = CGF.EmitLValueForField(TDBase, *FI); |
Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 4554 | OpenMPDirectiveKind Kind = isOpenMPTaskLoopDirective(D.getDirectiveKind()) |
| 4555 | ? OMPD_taskloop |
| 4556 | : OMPD_task; |
| 4557 | const CapturedStmt &CS = *D.getCapturedStmt(Kind); |
| 4558 | CodeGenFunction::CGCapturedStmtInfo CapturesInfo(CS); |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4559 | LValue SrcBase; |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 4560 | bool IsTargetTask = |
| 4561 | isOpenMPTargetDataManagementDirective(D.getDirectiveKind()) || |
| 4562 | isOpenMPTargetExecutionDirective(D.getDirectiveKind()); |
| 4563 | // For target-based directives skip 3 firstprivate arrays BasePointersArray, |
| 4564 | // PointersArray and SizesArray. The original variables for these arrays are |
| 4565 | // not captured and we get their addresses explicitly. |
| 4566 | if ((!IsTargetTask && !Data.FirstprivateVars.empty()) || |
Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 4567 | (IsTargetTask && KmpTaskSharedsPtr.isValid())) { |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4568 | SrcBase = CGF.MakeAddrLValue( |
| 4569 | CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
| 4570 | KmpTaskSharedsPtr, CGF.ConvertTypeForMem(SharedsPtrTy)), |
| 4571 | SharedsTy); |
| 4572 | } |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4573 | FI = cast<RecordDecl>(FI->getType()->getAsTagDecl())->field_begin(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4574 | for (const PrivateDataTy &Pair : Privates) { |
| 4575 | const VarDecl *VD = Pair.second.PrivateCopy; |
| 4576 | const Expr *Init = VD->getAnyInitializer(); |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4577 | if (Init && (!ForDup || (isa<CXXConstructExpr>(Init) && |
| 4578 | !CGF.isTrivialInitializer(Init)))) { |
Alexey Bataev | 8a83159 | 2016-05-10 10:36:51 +0000 | [diff] [blame] | 4579 | LValue PrivateLValue = CGF.EmitLValueForField(PrivatesBase, *FI); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4580 | if (const VarDecl *Elem = Pair.second.PrivateElemInit) { |
| 4581 | const VarDecl *OriginalVD = Pair.second.Original; |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 4582 | // Check if the variable is the target-based BasePointersArray, |
| 4583 | // PointersArray or SizesArray. |
| 4584 | LValue SharedRefLValue; |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4585 | QualType Type = OriginalVD->getType(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4586 | const FieldDecl *SharedField = CapturesInfo.lookup(OriginalVD); |
Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 4587 | if (IsTargetTask && !SharedField) { |
| 4588 | assert(isa<ImplicitParamDecl>(OriginalVD) && |
| 4589 | isa<CapturedDecl>(OriginalVD->getDeclContext()) && |
| 4590 | cast<CapturedDecl>(OriginalVD->getDeclContext()) |
| 4591 | ->getNumParams() == 0 && |
| 4592 | isa<TranslationUnitDecl>( |
| 4593 | cast<CapturedDecl>(OriginalVD->getDeclContext()) |
| 4594 | ->getDeclContext()) && |
| 4595 | "Expected artificial target data variable."); |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 4596 | SharedRefLValue = |
| 4597 | CGF.MakeAddrLValue(CGF.GetAddrOfLocalVar(OriginalVD), Type); |
| 4598 | } else { |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 4599 | SharedRefLValue = CGF.EmitLValueForField(SrcBase, SharedField); |
| 4600 | SharedRefLValue = CGF.MakeAddrLValue( |
| 4601 | Address(SharedRefLValue.getPointer(), C.getDeclAlign(OriginalVD)), |
| 4602 | SharedRefLValue.getType(), LValueBaseInfo(AlignmentSource::Decl), |
| 4603 | SharedRefLValue.getTBAAInfo()); |
| 4604 | } |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4605 | if (Type->isArrayType()) { |
| 4606 | // Initialize firstprivate array. |
| 4607 | if (!isa<CXXConstructExpr>(Init) || CGF.isTrivialInitializer(Init)) { |
| 4608 | // Perform simple memcpy. |
Ivan A. Kosarev | 1860b52 | 2018-01-25 14:21:55 +0000 | [diff] [blame] | 4609 | CGF.EmitAggregateAssign(PrivateLValue, SharedRefLValue, Type); |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4610 | } else { |
| 4611 | // Initialize firstprivate array using element-by-element |
Simon Pilgrim | 2c51880 | 2017-03-30 14:13:19 +0000 | [diff] [blame] | 4612 | // initialization. |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4613 | CGF.EmitOMPAggregateAssign( |
| 4614 | PrivateLValue.getAddress(), SharedRefLValue.getAddress(), Type, |
| 4615 | [&CGF, Elem, Init, &CapturesInfo](Address DestElement, |
| 4616 | Address SrcElement) { |
| 4617 | // Clean up any temporaries needed by the initialization. |
| 4618 | CodeGenFunction::OMPPrivateScope InitScope(CGF); |
| 4619 | InitScope.addPrivate( |
| 4620 | Elem, [SrcElement]() -> Address { return SrcElement; }); |
| 4621 | (void)InitScope.Privatize(); |
| 4622 | // Emit initialization for single element. |
| 4623 | CodeGenFunction::CGCapturedStmtRAII CapInfoRAII( |
| 4624 | CGF, &CapturesInfo); |
| 4625 | CGF.EmitAnyExprToMem(Init, DestElement, |
| 4626 | Init->getType().getQualifiers(), |
| 4627 | /*IsInitializer=*/false); |
| 4628 | }); |
| 4629 | } |
| 4630 | } else { |
| 4631 | CodeGenFunction::OMPPrivateScope InitScope(CGF); |
| 4632 | InitScope.addPrivate(Elem, [SharedRefLValue]() -> Address { |
| 4633 | return SharedRefLValue.getAddress(); |
| 4634 | }); |
| 4635 | (void)InitScope.Privatize(); |
| 4636 | CodeGenFunction::CGCapturedStmtRAII CapInfoRAII(CGF, &CapturesInfo); |
| 4637 | CGF.EmitExprAsInit(Init, VD, PrivateLValue, |
| 4638 | /*capturedByInit=*/false); |
| 4639 | } |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4640 | } else { |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4641 | CGF.EmitExprAsInit(Init, VD, PrivateLValue, /*capturedByInit=*/false); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4642 | } |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4643 | } |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4644 | ++FI; |
| 4645 | } |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4646 | } |
| 4647 | |
| 4648 | /// Check if duplication function is required for taskloops. |
| 4649 | static bool checkInitIsRequired(CodeGenFunction &CGF, |
| 4650 | ArrayRef<PrivateDataTy> Privates) { |
| 4651 | bool InitRequired = false; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4652 | for (const PrivateDataTy &Pair : Privates) { |
| 4653 | const VarDecl *VD = Pair.second.PrivateCopy; |
| 4654 | const Expr *Init = VD->getAnyInitializer(); |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4655 | InitRequired = InitRequired || (Init && isa<CXXConstructExpr>(Init) && |
| 4656 | !CGF.isTrivialInitializer(Init)); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4657 | if (InitRequired) |
| 4658 | break; |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4659 | } |
| 4660 | return InitRequired; |
| 4661 | } |
| 4662 | |
| 4663 | |
| 4664 | /// Emit task_dup function (for initialization of |
| 4665 | /// private/firstprivate/lastprivate vars and last_iter flag) |
| 4666 | /// \code |
| 4667 | /// void __task_dup_entry(kmp_task_t *task_dst, const kmp_task_t *task_src, int |
| 4668 | /// lastpriv) { |
| 4669 | /// // setup lastprivate flag |
| 4670 | /// task_dst->last = lastpriv; |
| 4671 | /// // could be constructor calls here... |
| 4672 | /// } |
| 4673 | /// \endcode |
| 4674 | static llvm::Value * |
| 4675 | emitTaskDupFunction(CodeGenModule &CGM, SourceLocation Loc, |
| 4676 | const OMPExecutableDirective &D, |
| 4677 | QualType KmpTaskTWithPrivatesPtrQTy, |
| 4678 | const RecordDecl *KmpTaskTWithPrivatesQTyRD, |
| 4679 | const RecordDecl *KmpTaskTQTyRD, QualType SharedsTy, |
| 4680 | QualType SharedsPtrTy, const OMPTaskDataTy &Data, |
| 4681 | ArrayRef<PrivateDataTy> Privates, bool WithLastIter) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4682 | ASTContext &C = CGM.getContext(); |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4683 | FunctionArgList Args; |
Alexey Bataev | 5622323 | 2017-06-09 13:40:18 +0000 | [diff] [blame] | 4684 | ImplicitParamDecl DstArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, |
| 4685 | KmpTaskTWithPrivatesPtrQTy, |
| 4686 | ImplicitParamDecl::Other); |
| 4687 | ImplicitParamDecl SrcArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, |
| 4688 | KmpTaskTWithPrivatesPtrQTy, |
| 4689 | ImplicitParamDecl::Other); |
| 4690 | ImplicitParamDecl LastprivArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, C.IntTy, |
| 4691 | ImplicitParamDecl::Other); |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4692 | Args.push_back(&DstArg); |
| 4693 | Args.push_back(&SrcArg); |
| 4694 | Args.push_back(&LastprivArg); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4695 | const auto &TaskDupFnInfo = |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4696 | CGM.getTypes().arrangeBuiltinFunctionDeclaration(C.VoidTy, Args); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4697 | llvm::FunctionType *TaskDupTy = CGM.getTypes().GetFunctionType(TaskDupFnInfo); |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 4698 | std::string Name = CGM.getOpenMPRuntime().getName({"omp_task_dup", ""}); |
| 4699 | auto *TaskDup = llvm::Function::Create( |
| 4700 | TaskDupTy, llvm::GlobalValue::InternalLinkage, Name, &CGM.getModule()); |
Rafael Espindola | 51ec5a9 | 2018-02-28 23:46:35 +0000 | [diff] [blame] | 4701 | CGM.SetInternalFunctionAttributes(GlobalDecl(), TaskDup, TaskDupFnInfo); |
Alexey Bataev | c0f879b | 2018-04-10 20:10:53 +0000 | [diff] [blame] | 4702 | TaskDup->setDoesNotRecurse(); |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4703 | CodeGenFunction CGF(CGM); |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 4704 | CGF.StartFunction(GlobalDecl(), C.VoidTy, TaskDup, TaskDupFnInfo, Args, Loc, |
| 4705 | Loc); |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4706 | |
| 4707 | LValue TDBase = CGF.EmitLoadOfPointerLValue( |
| 4708 | CGF.GetAddrOfLocalVar(&DstArg), |
| 4709 | KmpTaskTWithPrivatesPtrQTy->castAs<PointerType>()); |
| 4710 | // task_dst->liter = lastpriv; |
| 4711 | if (WithLastIter) { |
| 4712 | auto LIFI = std::next(KmpTaskTQTyRD->field_begin(), KmpTaskTLastIter); |
| 4713 | LValue Base = CGF.EmitLValueForField( |
| 4714 | TDBase, *KmpTaskTWithPrivatesQTyRD->field_begin()); |
| 4715 | LValue LILVal = CGF.EmitLValueForField(Base, *LIFI); |
| 4716 | llvm::Value *Lastpriv = CGF.EmitLoadOfScalar( |
| 4717 | CGF.GetAddrOfLocalVar(&LastprivArg), /*Volatile=*/false, C.IntTy, Loc); |
| 4718 | CGF.EmitStoreOfScalar(Lastpriv, LILVal); |
| 4719 | } |
| 4720 | |
| 4721 | // Emit initial values for private copies (if any). |
| 4722 | assert(!Privates.empty()); |
| 4723 | Address KmpTaskSharedsPtr = Address::invalid(); |
| 4724 | if (!Data.FirstprivateVars.empty()) { |
| 4725 | LValue TDBase = CGF.EmitLoadOfPointerLValue( |
| 4726 | CGF.GetAddrOfLocalVar(&SrcArg), |
| 4727 | KmpTaskTWithPrivatesPtrQTy->castAs<PointerType>()); |
| 4728 | LValue Base = CGF.EmitLValueForField( |
| 4729 | TDBase, *KmpTaskTWithPrivatesQTyRD->field_begin()); |
| 4730 | KmpTaskSharedsPtr = Address( |
| 4731 | CGF.EmitLoadOfScalar(CGF.EmitLValueForField( |
| 4732 | Base, *std::next(KmpTaskTQTyRD->field_begin(), |
| 4733 | KmpTaskTShareds)), |
| 4734 | Loc), |
| 4735 | CGF.getNaturalTypeAlignment(SharedsTy)); |
| 4736 | } |
Alexey Bataev | 8a83159 | 2016-05-10 10:36:51 +0000 | [diff] [blame] | 4737 | emitPrivatesInit(CGF, D, KmpTaskSharedsPtr, TDBase, KmpTaskTWithPrivatesQTyRD, |
| 4738 | SharedsTy, SharedsPtrTy, Data, Privates, /*ForDup=*/true); |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4739 | CGF.FinishFunction(); |
| 4740 | return TaskDup; |
| 4741 | } |
| 4742 | |
Alexey Bataev | 8a83159 | 2016-05-10 10:36:51 +0000 | [diff] [blame] | 4743 | /// Checks if destructor function is required to be generated. |
| 4744 | /// \return true if cleanups are required, false otherwise. |
| 4745 | static bool |
| 4746 | checkDestructorsRequired(const RecordDecl *KmpTaskTWithPrivatesQTyRD) { |
| 4747 | bool NeedsCleanup = false; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4748 | auto FI = std::next(KmpTaskTWithPrivatesQTyRD->field_begin(), 1); |
| 4749 | const auto *PrivateRD = cast<RecordDecl>(FI->getType()->getAsTagDecl()); |
| 4750 | for (const FieldDecl *FD : PrivateRD->fields()) { |
Alexey Bataev | 8a83159 | 2016-05-10 10:36:51 +0000 | [diff] [blame] | 4751 | NeedsCleanup = NeedsCleanup || FD->getType().isDestructedType(); |
| 4752 | if (NeedsCleanup) |
| 4753 | break; |
| 4754 | } |
| 4755 | return NeedsCleanup; |
| 4756 | } |
| 4757 | |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 4758 | CGOpenMPRuntime::TaskResultTy |
| 4759 | CGOpenMPRuntime::emitTaskInit(CodeGenFunction &CGF, SourceLocation Loc, |
| 4760 | const OMPExecutableDirective &D, |
| 4761 | llvm::Value *TaskFunction, QualType SharedsTy, |
| 4762 | Address Shareds, const OMPTaskDataTy &Data) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4763 | ASTContext &C = CGM.getContext(); |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4764 | llvm::SmallVector<PrivateDataTy, 4> Privates; |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4765 | // Aggregate privates and sort them by the alignment. |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 4766 | auto I = Data.PrivateCopies.begin(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4767 | for (const Expr *E : Data.PrivateVars) { |
| 4768 | const auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl()); |
Alexey Bataev | 43a919f | 2018-04-13 17:48:43 +0000 | [diff] [blame] | 4769 | Privates.emplace_back( |
Alexey Bataev | c71a409 | 2015-09-11 10:29:41 +0000 | [diff] [blame] | 4770 | C.getDeclAlign(VD), |
Alexey Bataev | 9e03404 | 2015-05-05 04:05:12 +0000 | [diff] [blame] | 4771 | PrivateHelpersTy(VD, cast<VarDecl>(cast<DeclRefExpr>(*I)->getDecl()), |
Alexey Bataev | 43a919f | 2018-04-13 17:48:43 +0000 | [diff] [blame] | 4772 | /*PrivateElemInit=*/nullptr)); |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 4773 | ++I; |
| 4774 | } |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 4775 | I = Data.FirstprivateCopies.begin(); |
| 4776 | auto IElemInitRef = Data.FirstprivateInits.begin(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4777 | for (const Expr *E : Data.FirstprivateVars) { |
| 4778 | const auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl()); |
Alexey Bataev | 43a919f | 2018-04-13 17:48:43 +0000 | [diff] [blame] | 4779 | Privates.emplace_back( |
Alexey Bataev | c71a409 | 2015-09-11 10:29:41 +0000 | [diff] [blame] | 4780 | C.getDeclAlign(VD), |
Alexey Bataev | 9e03404 | 2015-05-05 04:05:12 +0000 | [diff] [blame] | 4781 | PrivateHelpersTy( |
| 4782 | VD, cast<VarDecl>(cast<DeclRefExpr>(*I)->getDecl()), |
Alexey Bataev | 43a919f | 2018-04-13 17:48:43 +0000 | [diff] [blame] | 4783 | cast<VarDecl>(cast<DeclRefExpr>(*IElemInitRef)->getDecl()))); |
Richard Trieu | cc3949d | 2016-02-18 22:34:54 +0000 | [diff] [blame] | 4784 | ++I; |
| 4785 | ++IElemInitRef; |
Alexey Bataev | 9e03404 | 2015-05-05 04:05:12 +0000 | [diff] [blame] | 4786 | } |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4787 | I = Data.LastprivateCopies.begin(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4788 | for (const Expr *E : Data.LastprivateVars) { |
| 4789 | const auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl()); |
Alexey Bataev | 43a919f | 2018-04-13 17:48:43 +0000 | [diff] [blame] | 4790 | Privates.emplace_back( |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4791 | C.getDeclAlign(VD), |
| 4792 | PrivateHelpersTy(VD, cast<VarDecl>(cast<DeclRefExpr>(*I)->getDecl()), |
Alexey Bataev | 43a919f | 2018-04-13 17:48:43 +0000 | [diff] [blame] | 4793 | /*PrivateElemInit=*/nullptr)); |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4794 | ++I; |
| 4795 | } |
Mandeep Singh Grang | b14fb6a2 | 2017-11-28 20:41:13 +0000 | [diff] [blame] | 4796 | std::stable_sort(Privates.begin(), Privates.end(), stable_sort_comparator); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4797 | QualType KmpInt32Ty = C.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1); |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 4798 | // Build type kmp_routine_entry_t (if not built yet). |
| 4799 | emitKmpRoutineEntryT(KmpInt32Ty); |
Alexey Bataev | 8fc69dc | 2015-05-18 07:54:53 +0000 | [diff] [blame] | 4800 | // Build type kmp_task_t (if not built yet). |
Alexey Bataev | e213f3e | 2017-10-11 15:29:40 +0000 | [diff] [blame] | 4801 | if (isOpenMPTaskLoopDirective(D.getDirectiveKind())) { |
| 4802 | if (SavedKmpTaskloopTQTy.isNull()) { |
| 4803 | SavedKmpTaskloopTQTy = C.getRecordType(createKmpTaskTRecordDecl( |
| 4804 | CGM, D.getDirectiveKind(), KmpInt32Ty, KmpRoutineEntryPtrQTy)); |
| 4805 | } |
| 4806 | KmpTaskTQTy = SavedKmpTaskloopTQTy; |
Alexey Bataev | 3a03a7f | 2017-10-11 15:56:38 +0000 | [diff] [blame] | 4807 | } else { |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 4808 | assert((D.getDirectiveKind() == OMPD_task || |
| 4809 | isOpenMPTargetExecutionDirective(D.getDirectiveKind()) || |
| 4810 | isOpenMPTargetDataManagementDirective(D.getDirectiveKind())) && |
| 4811 | "Expected taskloop, task or target directive"); |
Alexey Bataev | e213f3e | 2017-10-11 15:29:40 +0000 | [diff] [blame] | 4812 | if (SavedKmpTaskTQTy.isNull()) { |
| 4813 | SavedKmpTaskTQTy = C.getRecordType(createKmpTaskTRecordDecl( |
| 4814 | CGM, D.getDirectiveKind(), KmpInt32Ty, KmpRoutineEntryPtrQTy)); |
| 4815 | } |
| 4816 | KmpTaskTQTy = SavedKmpTaskTQTy; |
Alexey Bataev | 8fc69dc | 2015-05-18 07:54:53 +0000 | [diff] [blame] | 4817 | } |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4818 | const auto *KmpTaskTQTyRD = cast<RecordDecl>(KmpTaskTQTy->getAsTagDecl()); |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 4819 | // Build particular struct kmp_task_t for the given task. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4820 | const RecordDecl *KmpTaskTWithPrivatesQTyRD = |
Alexey Bataev | 8fc69dc | 2015-05-18 07:54:53 +0000 | [diff] [blame] | 4821 | createKmpTaskTWithPrivatesRecordDecl(CGM, KmpTaskTQTy, Privates); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4822 | QualType KmpTaskTWithPrivatesQTy = C.getRecordType(KmpTaskTWithPrivatesQTyRD); |
Alexey Bataev | 8fc69dc | 2015-05-18 07:54:53 +0000 | [diff] [blame] | 4823 | QualType KmpTaskTWithPrivatesPtrQTy = |
| 4824 | C.getPointerType(KmpTaskTWithPrivatesQTy); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4825 | llvm::Type *KmpTaskTWithPrivatesTy = CGF.ConvertType(KmpTaskTWithPrivatesQTy); |
| 4826 | llvm::Type *KmpTaskTWithPrivatesPtrTy = |
| 4827 | KmpTaskTWithPrivatesTy->getPointerTo(); |
| 4828 | llvm::Value *KmpTaskTWithPrivatesTySize = |
| 4829 | CGF.getTypeSize(KmpTaskTWithPrivatesQTy); |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 4830 | QualType SharedsPtrTy = C.getPointerType(SharedsTy); |
| 4831 | |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4832 | // Emit initial values for private copies (if any). |
| 4833 | llvm::Value *TaskPrivatesMap = nullptr; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4834 | llvm::Type *TaskPrivatesMapTy = |
Reid Kleckner | e258c44 | 2017-03-16 18:55:46 +0000 | [diff] [blame] | 4835 | std::next(cast<llvm::Function>(TaskFunction)->arg_begin(), 3)->getType(); |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4836 | if (!Privates.empty()) { |
| 4837 | auto FI = std::next(KmpTaskTWithPrivatesQTyRD->field_begin()); |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4838 | TaskPrivatesMap = emitTaskPrivateMappingFunction( |
| 4839 | CGM, Loc, Data.PrivateVars, Data.FirstprivateVars, Data.LastprivateVars, |
| 4840 | FI->getType(), Privates); |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4841 | TaskPrivatesMap = CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
| 4842 | TaskPrivatesMap, TaskPrivatesMapTy); |
| 4843 | } else { |
| 4844 | TaskPrivatesMap = llvm::ConstantPointerNull::get( |
| 4845 | cast<llvm::PointerType>(TaskPrivatesMapTy)); |
| 4846 | } |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 4847 | // Build a proxy function kmp_int32 .omp_task_entry.(kmp_int32 gtid, |
| 4848 | // kmp_task_t *tt); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4849 | llvm::Value *TaskEntry = emitProxyTaskFunction( |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4850 | CGM, Loc, D.getDirectiveKind(), KmpInt32Ty, KmpTaskTWithPrivatesPtrQTy, |
| 4851 | KmpTaskTWithPrivatesQTy, KmpTaskTQTy, SharedsPtrTy, TaskFunction, |
| 4852 | TaskPrivatesMap); |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 4853 | |
| 4854 | // Build call kmp_task_t * __kmpc_omp_task_alloc(ident_t *, kmp_int32 gtid, |
| 4855 | // kmp_int32 flags, size_t sizeof_kmp_task_t, size_t sizeof_shareds, |
| 4856 | // kmp_routine_entry_t *task_entry); |
| 4857 | // Task flags. Format is taken from |
| 4858 | // http://llvm.org/svn/llvm-project/openmp/trunk/runtime/src/kmp.h, |
| 4859 | // description of kmp_tasking_flags struct. |
Alexey Bataev | 1e1e286 | 2016-05-10 12:21:02 +0000 | [diff] [blame] | 4860 | enum { |
| 4861 | TiedFlag = 0x1, |
| 4862 | FinalFlag = 0x2, |
| 4863 | DestructorsFlag = 0x8, |
| 4864 | PriorityFlag = 0x20 |
| 4865 | }; |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 4866 | unsigned Flags = Data.Tied ? TiedFlag : 0; |
Alexey Bataev | 8a83159 | 2016-05-10 10:36:51 +0000 | [diff] [blame] | 4867 | bool NeedsCleanup = false; |
| 4868 | if (!Privates.empty()) { |
| 4869 | NeedsCleanup = checkDestructorsRequired(KmpTaskTWithPrivatesQTyRD); |
| 4870 | if (NeedsCleanup) |
| 4871 | Flags = Flags | DestructorsFlag; |
| 4872 | } |
Alexey Bataev | 1e1e286 | 2016-05-10 12:21:02 +0000 | [diff] [blame] | 4873 | if (Data.Priority.getInt()) |
| 4874 | Flags = Flags | PriorityFlag; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4875 | llvm::Value *TaskFlags = |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 4876 | Data.Final.getPointer() |
| 4877 | ? CGF.Builder.CreateSelect(Data.Final.getPointer(), |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 4878 | CGF.Builder.getInt32(FinalFlag), |
| 4879 | CGF.Builder.getInt32(/*C=*/0)) |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 4880 | : CGF.Builder.getInt32(Data.Final.getInt() ? FinalFlag : 0); |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 4881 | TaskFlags = CGF.Builder.CreateOr(TaskFlags, CGF.Builder.getInt32(Flags)); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4882 | llvm::Value *SharedsSize = CGM.getSize(C.getTypeSizeInChars(SharedsTy)); |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 4883 | llvm::Value *AllocArgs[] = {emitUpdateLocation(CGF, Loc), |
| 4884 | getThreadID(CGF, Loc), TaskFlags, |
| 4885 | KmpTaskTWithPrivatesTySize, SharedsSize, |
| 4886 | CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
| 4887 | TaskEntry, KmpRoutineEntryPtrTy)}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4888 | llvm::Value *NewTask = CGF.EmitRuntimeCall( |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 4889 | createRuntimeFunction(OMPRTL__kmpc_omp_task_alloc), AllocArgs); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4890 | llvm::Value *NewTaskNewTaskTTy = |
| 4891 | CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
| 4892 | NewTask, KmpTaskTWithPrivatesPtrTy); |
Alexey Bataev | 8fc69dc | 2015-05-18 07:54:53 +0000 | [diff] [blame] | 4893 | LValue Base = CGF.MakeNaturalAlignAddrLValue(NewTaskNewTaskTTy, |
| 4894 | KmpTaskTWithPrivatesQTy); |
| 4895 | LValue TDBase = |
| 4896 | CGF.EmitLValueForField(Base, *KmpTaskTWithPrivatesQTyRD->field_begin()); |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 4897 | // Fill the data in the resulting kmp_task_t record. |
| 4898 | // Copy shareds if there are any. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4899 | Address KmpTaskSharedsPtr = Address::invalid(); |
Alexey Bataev | 8fc69dc | 2015-05-18 07:54:53 +0000 | [diff] [blame] | 4900 | if (!SharedsTy->getAsStructureType()->getDecl()->field_empty()) { |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 4901 | KmpTaskSharedsPtr = |
| 4902 | Address(CGF.EmitLoadOfScalar( |
| 4903 | CGF.EmitLValueForField( |
| 4904 | TDBase, *std::next(KmpTaskTQTyRD->field_begin(), |
| 4905 | KmpTaskTShareds)), |
| 4906 | Loc), |
| 4907 | CGF.getNaturalTypeAlignment(SharedsTy)); |
Ivan A. Kosarev | 1860b52 | 2018-01-25 14:21:55 +0000 | [diff] [blame] | 4908 | LValue Dest = CGF.MakeAddrLValue(KmpTaskSharedsPtr, SharedsTy); |
| 4909 | LValue Src = CGF.MakeAddrLValue(Shareds, SharedsTy); |
Richard Smith | e78fac5 | 2018-04-05 20:52:58 +0000 | [diff] [blame] | 4910 | CGF.EmitAggregateCopy(Dest, Src, SharedsTy, AggValueSlot::DoesNotOverlap); |
Alexey Bataev | 8fc69dc | 2015-05-18 07:54:53 +0000 | [diff] [blame] | 4911 | } |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 4912 | // Emit initial values for private copies (if any). |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4913 | TaskResultTy Result; |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 4914 | if (!Privates.empty()) { |
Alexey Bataev | 8a83159 | 2016-05-10 10:36:51 +0000 | [diff] [blame] | 4915 | emitPrivatesInit(CGF, D, KmpTaskSharedsPtr, Base, KmpTaskTWithPrivatesQTyRD, |
| 4916 | SharedsTy, SharedsPtrTy, Data, Privates, |
| 4917 | /*ForDup=*/false); |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4918 | if (isOpenMPTaskLoopDirective(D.getDirectiveKind()) && |
| 4919 | (!Data.LastprivateVars.empty() || checkInitIsRequired(CGF, Privates))) { |
| 4920 | Result.TaskDupFn = emitTaskDupFunction( |
| 4921 | CGM, Loc, D, KmpTaskTWithPrivatesPtrQTy, KmpTaskTWithPrivatesQTyRD, |
| 4922 | KmpTaskTQTyRD, SharedsTy, SharedsPtrTy, Data, Privates, |
| 4923 | /*WithLastIter=*/!Data.LastprivateVars.empty()); |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 4924 | } |
| 4925 | } |
Alexey Bataev | ad537bb | 2016-05-30 09:06:50 +0000 | [diff] [blame] | 4926 | // Fields of union "kmp_cmplrdata_t" for destructors and priority. |
| 4927 | enum { Priority = 0, Destructors = 1 }; |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 4928 | // Provide pointer to function with destructors for privates. |
Alexey Bataev | ad537bb | 2016-05-30 09:06:50 +0000 | [diff] [blame] | 4929 | auto FI = std::next(KmpTaskTQTyRD->field_begin(), Data1); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4930 | const RecordDecl *KmpCmplrdataUD = |
| 4931 | (*FI)->getType()->getAsUnionType()->getDecl(); |
Alexey Bataev | ad537bb | 2016-05-30 09:06:50 +0000 | [diff] [blame] | 4932 | if (NeedsCleanup) { |
| 4933 | llvm::Value *DestructorFn = emitDestructorsFunction( |
| 4934 | CGM, Loc, KmpInt32Ty, KmpTaskTWithPrivatesPtrQTy, |
| 4935 | KmpTaskTWithPrivatesQTy); |
| 4936 | LValue Data1LV = CGF.EmitLValueForField(TDBase, *FI); |
| 4937 | LValue DestructorsLV = CGF.EmitLValueForField( |
| 4938 | Data1LV, *std::next(KmpCmplrdataUD->field_begin(), Destructors)); |
| 4939 | CGF.EmitStoreOfScalar(CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
| 4940 | DestructorFn, KmpRoutineEntryPtrTy), |
| 4941 | DestructorsLV); |
| 4942 | } |
| 4943 | // Set priority. |
| 4944 | if (Data.Priority.getInt()) { |
| 4945 | LValue Data2LV = CGF.EmitLValueForField( |
| 4946 | TDBase, *std::next(KmpTaskTQTyRD->field_begin(), Data2)); |
| 4947 | LValue PriorityLV = CGF.EmitLValueForField( |
| 4948 | Data2LV, *std::next(KmpCmplrdataUD->field_begin(), Priority)); |
| 4949 | CGF.EmitStoreOfScalar(Data.Priority.getPointer(), PriorityLV); |
| 4950 | } |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 4951 | Result.NewTask = NewTask; |
| 4952 | Result.TaskEntry = TaskEntry; |
| 4953 | Result.NewTaskNewTaskTTy = NewTaskNewTaskTTy; |
| 4954 | Result.TDBase = TDBase; |
| 4955 | Result.KmpTaskTQTyRD = KmpTaskTQTyRD; |
| 4956 | return Result; |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4957 | } |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 4958 | |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 4959 | void CGOpenMPRuntime::emitTaskCall(CodeGenFunction &CGF, SourceLocation Loc, |
| 4960 | const OMPExecutableDirective &D, |
| 4961 | llvm::Value *TaskFunction, |
| 4962 | QualType SharedsTy, Address Shareds, |
| 4963 | const Expr *IfCond, |
| 4964 | const OMPTaskDataTy &Data) { |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4965 | if (!CGF.HaveInsertPoint()) |
| 4966 | return; |
| 4967 | |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 4968 | TaskResultTy Result = |
| 4969 | emitTaskInit(CGF, Loc, D, TaskFunction, SharedsTy, Shareds, Data); |
| 4970 | llvm::Value *NewTask = Result.NewTask; |
| 4971 | llvm::Value *TaskEntry = Result.TaskEntry; |
| 4972 | llvm::Value *NewTaskNewTaskTTy = Result.NewTaskNewTaskTTy; |
| 4973 | LValue TDBase = Result.TDBase; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4974 | const RecordDecl *KmpTaskTQTyRD = Result.KmpTaskTQTyRD; |
| 4975 | ASTContext &C = CGM.getContext(); |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 4976 | // Process list of dependences. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4977 | Address DependenciesArray = Address::invalid(); |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 4978 | unsigned NumDependencies = Data.Dependences.size(); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4979 | if (NumDependencies) { |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 4980 | // Dependence kind for RTL. |
Alexey Bataev | 92e82f9 | 2015-11-23 13:33:42 +0000 | [diff] [blame] | 4981 | enum RTLDependenceKindTy { DepIn = 0x01, DepInOut = 0x3 }; |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 4982 | enum RTLDependInfoFieldsTy { BaseAddr, Len, Flags }; |
| 4983 | RecordDecl *KmpDependInfoRD; |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 4984 | QualType FlagsTy = |
| 4985 | C.getIntTypeForBitwidth(C.getTypeSize(C.BoolTy), /*Signed=*/false); |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 4986 | llvm::Type *LLVMFlagsTy = CGF.ConvertTypeForMem(FlagsTy); |
| 4987 | if (KmpDependInfoTy.isNull()) { |
| 4988 | KmpDependInfoRD = C.buildImplicitRecord("kmp_depend_info"); |
| 4989 | KmpDependInfoRD->startDefinition(); |
| 4990 | addFieldToRecordDecl(C, KmpDependInfoRD, C.getIntPtrType()); |
| 4991 | addFieldToRecordDecl(C, KmpDependInfoRD, C.getSizeType()); |
| 4992 | addFieldToRecordDecl(C, KmpDependInfoRD, FlagsTy); |
| 4993 | KmpDependInfoRD->completeDefinition(); |
| 4994 | KmpDependInfoTy = C.getRecordType(KmpDependInfoRD); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4995 | } else { |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 4996 | KmpDependInfoRD = cast<RecordDecl>(KmpDependInfoTy->getAsTagDecl()); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4997 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4998 | CharUnits DependencySize = C.getTypeSizeInChars(KmpDependInfoTy); |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 4999 | // Define type kmp_depend_info[<Dependences.size()>]; |
| 5000 | QualType KmpDependInfoArrayTy = C.getConstantArrayType( |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5001 | KmpDependInfoTy, llvm::APInt(/*numBits=*/64, NumDependencies), |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 5002 | ArrayType::Normal, /*IndexTypeQuals=*/0); |
| 5003 | // kmp_depend_info[<Dependences.size()>] deps; |
Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 5004 | DependenciesArray = |
| 5005 | CGF.CreateMemTemp(KmpDependInfoArrayTy, ".dep.arr.addr"); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5006 | for (unsigned I = 0; I < NumDependencies; ++I) { |
| 5007 | const Expr *E = Data.Dependences[I].second; |
| 5008 | LValue Addr = CGF.EmitLValue(E); |
Alexey Bataev | d6fdc8b | 2015-08-31 07:32:19 +0000 | [diff] [blame] | 5009 | llvm::Value *Size; |
| 5010 | QualType Ty = E->getType(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5011 | if (const auto *ASE = |
| 5012 | dyn_cast<OMPArraySectionExpr>(E->IgnoreParenImpCasts())) { |
Alexey Bataev | d6fdc8b | 2015-08-31 07:32:19 +0000 | [diff] [blame] | 5013 | LValue UpAddrLVal = |
| 5014 | CGF.EmitOMPArraySectionExpr(ASE, /*LowerBound=*/false); |
| 5015 | llvm::Value *UpAddr = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5016 | CGF.Builder.CreateConstGEP1_32(UpAddrLVal.getPointer(), /*Idx0=*/1); |
Alexey Bataev | d6fdc8b | 2015-08-31 07:32:19 +0000 | [diff] [blame] | 5017 | llvm::Value *LowIntPtr = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5018 | CGF.Builder.CreatePtrToInt(Addr.getPointer(), CGM.SizeTy); |
Alexey Bataev | d6fdc8b | 2015-08-31 07:32:19 +0000 | [diff] [blame] | 5019 | llvm::Value *UpIntPtr = CGF.Builder.CreatePtrToInt(UpAddr, CGM.SizeTy); |
| 5020 | Size = CGF.Builder.CreateNUWSub(UpIntPtr, LowIntPtr); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5021 | } else { |
Alexey Bataev | 1189bd0 | 2016-01-26 12:20:39 +0000 | [diff] [blame] | 5022 | Size = CGF.getTypeSize(Ty); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5023 | } |
| 5024 | LValue Base = CGF.MakeAddrLValue( |
| 5025 | CGF.Builder.CreateConstArrayGEP(DependenciesArray, I, DependencySize), |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 5026 | KmpDependInfoTy); |
| 5027 | // deps[i].base_addr = &<Dependences[i].second>; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5028 | LValue BaseAddrLVal = CGF.EmitLValueForField( |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 5029 | Base, *std::next(KmpDependInfoRD->field_begin(), BaseAddr)); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5030 | CGF.EmitStoreOfScalar( |
| 5031 | CGF.Builder.CreatePtrToInt(Addr.getPointer(), CGF.IntPtrTy), |
| 5032 | BaseAddrLVal); |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 5033 | // deps[i].len = sizeof(<Dependences[i].second>); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5034 | LValue LenLVal = CGF.EmitLValueForField( |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 5035 | Base, *std::next(KmpDependInfoRD->field_begin(), Len)); |
| 5036 | CGF.EmitStoreOfScalar(Size, LenLVal); |
| 5037 | // deps[i].flags = <Dependences[i].first>; |
| 5038 | RTLDependenceKindTy DepKind; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5039 | switch (Data.Dependences[I].first) { |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 5040 | case OMPC_DEPEND_in: |
| 5041 | DepKind = DepIn; |
| 5042 | break; |
Alexey Bataev | 92e82f9 | 2015-11-23 13:33:42 +0000 | [diff] [blame] | 5043 | // Out and InOut dependencies must use the same code. |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 5044 | case OMPC_DEPEND_out: |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 5045 | case OMPC_DEPEND_inout: |
| 5046 | DepKind = DepInOut; |
| 5047 | break; |
Alexey Bataev | eb48235 | 2015-12-18 05:05:56 +0000 | [diff] [blame] | 5048 | case OMPC_DEPEND_source: |
Alexey Bataev | a636c7f | 2015-12-23 10:27:45 +0000 | [diff] [blame] | 5049 | case OMPC_DEPEND_sink: |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 5050 | case OMPC_DEPEND_unknown: |
| 5051 | llvm_unreachable("Unknown task dependence type"); |
| 5052 | } |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5053 | LValue FlagsLVal = CGF.EmitLValueForField( |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 5054 | Base, *std::next(KmpDependInfoRD->field_begin(), Flags)); |
| 5055 | CGF.EmitStoreOfScalar(llvm::ConstantInt::get(LLVMFlagsTy, DepKind), |
| 5056 | FlagsLVal); |
| 5057 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5058 | DependenciesArray = CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
| 5059 | CGF.Builder.CreateStructGEP(DependenciesArray, 0, CharUnits::Zero()), |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 5060 | CGF.VoidPtrTy); |
| 5061 | } |
| 5062 | |
Alexander Kornienko | 2a8c18d | 2018-04-06 15:14:32 +0000 | [diff] [blame] | 5063 | // 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] | 5064 | // libcall. |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 5065 | // Build kmp_int32 __kmpc_omp_task_with_deps(ident_t *, kmp_int32 gtid, |
| 5066 | // kmp_task_t *new_task, kmp_int32 ndeps, kmp_depend_info_t *dep_list, |
| 5067 | // kmp_int32 ndeps_noalias, kmp_depend_info_t *noalias_dep_list) if dependence |
| 5068 | // list is not empty |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5069 | llvm::Value *ThreadID = getThreadID(CGF, Loc); |
| 5070 | llvm::Value *UpLoc = emitUpdateLocation(CGF, Loc); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5071 | llvm::Value *TaskArgs[] = { UpLoc, ThreadID, NewTask }; |
| 5072 | llvm::Value *DepTaskArgs[7]; |
| 5073 | if (NumDependencies) { |
| 5074 | DepTaskArgs[0] = UpLoc; |
| 5075 | DepTaskArgs[1] = ThreadID; |
| 5076 | DepTaskArgs[2] = NewTask; |
| 5077 | DepTaskArgs[3] = CGF.Builder.getInt32(NumDependencies); |
| 5078 | DepTaskArgs[4] = DependenciesArray.getPointer(); |
| 5079 | DepTaskArgs[5] = CGF.Builder.getInt32(0); |
| 5080 | DepTaskArgs[6] = llvm::ConstantPointerNull::get(CGF.VoidPtrTy); |
| 5081 | } |
Malcolm Parsons | c6e4583 | 2017-01-13 18:55:32 +0000 | [diff] [blame] | 5082 | auto &&ThenCodeGen = [this, &Data, TDBase, KmpTaskTQTyRD, NumDependencies, |
| 5083 | &TaskArgs, |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5084 | &DepTaskArgs](CodeGenFunction &CGF, PrePostActionTy &) { |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 5085 | if (!Data.Tied) { |
Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 5086 | auto PartIdFI = std::next(KmpTaskTQTyRD->field_begin(), KmpTaskTPartId); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5087 | LValue PartIdLVal = CGF.EmitLValueForField(TDBase, *PartIdFI); |
Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 5088 | CGF.EmitStoreOfScalar(CGF.Builder.getInt32(0), PartIdLVal); |
| 5089 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5090 | if (NumDependencies) { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5091 | CGF.EmitRuntimeCall( |
Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 5092 | createRuntimeFunction(OMPRTL__kmpc_omp_task_with_deps), DepTaskArgs); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5093 | } else { |
Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 5094 | CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_omp_task), |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5095 | TaskArgs); |
| 5096 | } |
Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 5097 | // Check if parent region is untied and build return for untied task; |
| 5098 | if (auto *Region = |
| 5099 | dyn_cast_or_null<CGOpenMPRegionInfo>(CGF.CapturedStmtInfo)) |
| 5100 | Region->emitUntiedSwitch(CGF); |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 5101 | }; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5102 | |
| 5103 | llvm::Value *DepWaitTaskArgs[6]; |
| 5104 | if (NumDependencies) { |
| 5105 | DepWaitTaskArgs[0] = UpLoc; |
| 5106 | DepWaitTaskArgs[1] = ThreadID; |
| 5107 | DepWaitTaskArgs[2] = CGF.Builder.getInt32(NumDependencies); |
| 5108 | DepWaitTaskArgs[3] = DependenciesArray.getPointer(); |
| 5109 | DepWaitTaskArgs[4] = CGF.Builder.getInt32(0); |
| 5110 | DepWaitTaskArgs[5] = llvm::ConstantPointerNull::get(CGF.VoidPtrTy); |
| 5111 | } |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5112 | auto &&ElseCodeGen = [&TaskArgs, ThreadID, NewTaskNewTaskTTy, TaskEntry, |
Alexey Bataev | 3c595a6 | 2017-08-14 15:01:03 +0000 | [diff] [blame] | 5113 | NumDependencies, &DepWaitTaskArgs, |
| 5114 | Loc](CodeGenFunction &CGF, PrePostActionTy &) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5115 | CGOpenMPRuntime &RT = CGF.CGM.getOpenMPRuntime(); |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 5116 | CodeGenFunction::RunCleanupsScope LocalScope(CGF); |
| 5117 | // Build void __kmpc_omp_wait_deps(ident_t *, kmp_int32 gtid, |
| 5118 | // kmp_int32 ndeps, kmp_depend_info_t *dep_list, kmp_int32 |
| 5119 | // ndeps_noalias, kmp_depend_info_t *noalias_dep_list); if dependence info |
| 5120 | // is specified. |
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(RT.createRuntimeFunction(OMPRTL__kmpc_omp_wait_deps), |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 5123 | DepWaitTaskArgs); |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5124 | // Call proxy_task_entry(gtid, new_task); |
Alexey Bataev | 3c595a6 | 2017-08-14 15:01:03 +0000 | [diff] [blame] | 5125 | auto &&CodeGen = [TaskEntry, ThreadID, NewTaskNewTaskTTy, |
| 5126 | Loc](CodeGenFunction &CGF, PrePostActionTy &Action) { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5127 | Action.Enter(CGF); |
| 5128 | llvm::Value *OutlinedFnArgs[] = {ThreadID, NewTaskNewTaskTTy}; |
Alexey Bataev | 3c595a6 | 2017-08-14 15:01:03 +0000 | [diff] [blame] | 5129 | CGF.CGM.getOpenMPRuntime().emitOutlinedFunctionCall(CGF, Loc, TaskEntry, |
Alexey Bataev | 2c7eee5 | 2017-08-04 19:10:54 +0000 | [diff] [blame] | 5130 | OutlinedFnArgs); |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5131 | }; |
| 5132 | |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 5133 | // Build void __kmpc_omp_task_begin_if0(ident_t *, kmp_int32 gtid, |
| 5134 | // kmp_task_t *new_task); |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 5135 | // Build void __kmpc_omp_task_complete_if0(ident_t *, kmp_int32 gtid, |
| 5136 | // kmp_task_t *new_task); |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5137 | RegionCodeGenTy RCG(CodeGen); |
| 5138 | CommonActionTy Action( |
| 5139 | RT.createRuntimeFunction(OMPRTL__kmpc_omp_task_begin_if0), TaskArgs, |
| 5140 | RT.createRuntimeFunction(OMPRTL__kmpc_omp_task_complete_if0), TaskArgs); |
| 5141 | RCG.setAction(Action); |
| 5142 | RCG(CGF); |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 5143 | }; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5144 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5145 | if (IfCond) { |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 5146 | emitOMPIfClause(CGF, IfCond, ThenCodeGen, ElseCodeGen); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5147 | } else { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5148 | RegionCodeGenTy ThenRCG(ThenCodeGen); |
| 5149 | ThenRCG(CGF); |
Alexey Bataev | f539faa | 2016-03-28 12:58:34 +0000 | [diff] [blame] | 5150 | } |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 5151 | } |
| 5152 | |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 5153 | void CGOpenMPRuntime::emitTaskLoopCall(CodeGenFunction &CGF, SourceLocation Loc, |
| 5154 | const OMPLoopDirective &D, |
| 5155 | llvm::Value *TaskFunction, |
| 5156 | QualType SharedsTy, Address Shareds, |
| 5157 | const Expr *IfCond, |
| 5158 | const OMPTaskDataTy &Data) { |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 5159 | if (!CGF.HaveInsertPoint()) |
| 5160 | return; |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 5161 | TaskResultTy Result = |
| 5162 | emitTaskInit(CGF, Loc, D, TaskFunction, SharedsTy, Shareds, Data); |
Alexander Kornienko | 2a8c18d | 2018-04-06 15:14:32 +0000 | [diff] [blame] | 5163 | // 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] | 5164 | // libcall. |
| 5165 | // Call to void __kmpc_taskloop(ident_t *loc, int gtid, kmp_task_t *task, int |
| 5166 | // if_val, kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st, int nogroup, int |
| 5167 | // sched, kmp_uint64 grainsize, void *task_dup); |
| 5168 | llvm::Value *ThreadID = getThreadID(CGF, Loc); |
| 5169 | llvm::Value *UpLoc = emitUpdateLocation(CGF, Loc); |
| 5170 | llvm::Value *IfVal; |
| 5171 | if (IfCond) { |
| 5172 | IfVal = CGF.Builder.CreateIntCast(CGF.EvaluateExprAsBool(IfCond), CGF.IntTy, |
| 5173 | /*isSigned=*/true); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5174 | } else { |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 5175 | IfVal = llvm::ConstantInt::getSigned(CGF.IntTy, /*V=*/1); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5176 | } |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 5177 | |
| 5178 | LValue LBLVal = CGF.EmitLValueForField( |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 5179 | Result.TDBase, |
| 5180 | *std::next(Result.KmpTaskTQTyRD->field_begin(), KmpTaskTLowerBound)); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5181 | const auto *LBVar = |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 5182 | cast<VarDecl>(cast<DeclRefExpr>(D.getLowerBoundVariable())->getDecl()); |
| 5183 | CGF.EmitAnyExprToMem(LBVar->getInit(), LBLVal.getAddress(), LBLVal.getQuals(), |
| 5184 | /*IsInitializer=*/true); |
| 5185 | LValue UBLVal = CGF.EmitLValueForField( |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 5186 | Result.TDBase, |
| 5187 | *std::next(Result.KmpTaskTQTyRD->field_begin(), KmpTaskTUpperBound)); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5188 | const auto *UBVar = |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 5189 | cast<VarDecl>(cast<DeclRefExpr>(D.getUpperBoundVariable())->getDecl()); |
| 5190 | CGF.EmitAnyExprToMem(UBVar->getInit(), UBLVal.getAddress(), UBLVal.getQuals(), |
| 5191 | /*IsInitializer=*/true); |
| 5192 | LValue StLVal = CGF.EmitLValueForField( |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 5193 | Result.TDBase, |
| 5194 | *std::next(Result.KmpTaskTQTyRD->field_begin(), KmpTaskTStride)); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5195 | const auto *StVar = |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 5196 | cast<VarDecl>(cast<DeclRefExpr>(D.getStrideVariable())->getDecl()); |
| 5197 | CGF.EmitAnyExprToMem(StVar->getInit(), StLVal.getAddress(), StLVal.getQuals(), |
| 5198 | /*IsInitializer=*/true); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 5199 | // Store reductions address. |
| 5200 | LValue RedLVal = CGF.EmitLValueForField( |
| 5201 | Result.TDBase, |
| 5202 | *std::next(Result.KmpTaskTQTyRD->field_begin(), KmpTaskTReductions)); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5203 | if (Data.Reductions) { |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 5204 | CGF.EmitStoreOfScalar(Data.Reductions, RedLVal); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5205 | } else { |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 5206 | CGF.EmitNullInitialization(RedLVal.getAddress(), |
| 5207 | CGF.getContext().VoidPtrTy); |
| 5208 | } |
Alexey Bataev | 2b19a6f | 2016-04-28 09:15:06 +0000 | [diff] [blame] | 5209 | enum { NoSchedule = 0, Grainsize = 1, NumTasks = 2 }; |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 5210 | llvm::Value *TaskArgs[] = { |
Alexey Bataev | 3344603 | 2017-07-12 18:09:32 +0000 | [diff] [blame] | 5211 | UpLoc, |
| 5212 | ThreadID, |
| 5213 | Result.NewTask, |
| 5214 | IfVal, |
| 5215 | LBLVal.getPointer(), |
| 5216 | UBLVal.getPointer(), |
Alexey Bataev | a9b9cc0 | 2018-01-23 18:12:38 +0000 | [diff] [blame] | 5217 | CGF.EmitLoadOfScalar(StLVal, Loc), |
Alexey Bataev | 3344603 | 2017-07-12 18:09:32 +0000 | [diff] [blame] | 5218 | llvm::ConstantInt::getNullValue( |
| 5219 | CGF.IntTy), // Always 0 because taskgroup emitted by the compiler |
Alexey Bataev | 2b19a6f | 2016-04-28 09:15:06 +0000 | [diff] [blame] | 5220 | llvm::ConstantInt::getSigned( |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 5221 | CGF.IntTy, Data.Schedule.getPointer() |
| 5222 | ? Data.Schedule.getInt() ? NumTasks : Grainsize |
Alexey Bataev | 2b19a6f | 2016-04-28 09:15:06 +0000 | [diff] [blame] | 5223 | : NoSchedule), |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 5224 | Data.Schedule.getPointer() |
| 5225 | ? CGF.Builder.CreateIntCast(Data.Schedule.getPointer(), CGF.Int64Ty, |
Alexey Bataev | 2b19a6f | 2016-04-28 09:15:06 +0000 | [diff] [blame] | 5226 | /*isSigned=*/false) |
| 5227 | : llvm::ConstantInt::get(CGF.Int64Ty, /*V=*/0), |
Alexey Bataev | 3344603 | 2017-07-12 18:09:32 +0000 | [diff] [blame] | 5228 | Result.TaskDupFn ? CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
| 5229 | Result.TaskDupFn, CGF.VoidPtrTy) |
| 5230 | : llvm::ConstantPointerNull::get(CGF.VoidPtrTy)}; |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 5231 | CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_taskloop), TaskArgs); |
| 5232 | } |
| 5233 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 5234 | /// Emit reduction operation for each element of array (required for |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5235 | /// array sections) LHS op = RHS. |
| 5236 | /// \param Type Type of array. |
| 5237 | /// \param LHSVar Variable on the left side of the reduction operation |
| 5238 | /// (references element of array in original variable). |
| 5239 | /// \param RHSVar Variable on the right side of the reduction operation |
| 5240 | /// (references element of array in original variable). |
| 5241 | /// \param RedOpGen Generator of reduction operation with use of LHSVar and |
| 5242 | /// RHSVar. |
Benjamin Kramer | e003ca2 | 2015-10-28 13:54:16 +0000 | [diff] [blame] | 5243 | static void EmitOMPAggregateReduction( |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5244 | CodeGenFunction &CGF, QualType Type, const VarDecl *LHSVar, |
| 5245 | const VarDecl *RHSVar, |
| 5246 | const llvm::function_ref<void(CodeGenFunction &CGF, const Expr *, |
| 5247 | const Expr *, const Expr *)> &RedOpGen, |
| 5248 | const Expr *XExpr = nullptr, const Expr *EExpr = nullptr, |
| 5249 | const Expr *UpExpr = nullptr) { |
| 5250 | // Perform element-by-element initialization. |
| 5251 | QualType ElementTy; |
| 5252 | Address LHSAddr = CGF.GetAddrOfLocalVar(LHSVar); |
| 5253 | Address RHSAddr = CGF.GetAddrOfLocalVar(RHSVar); |
| 5254 | |
| 5255 | // Drill down to the base element type on both arrays. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5256 | const ArrayType *ArrayTy = Type->getAsArrayTypeUnsafe(); |
| 5257 | llvm::Value *NumElements = CGF.emitArrayLength(ArrayTy, ElementTy, LHSAddr); |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5258 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5259 | llvm::Value *RHSBegin = RHSAddr.getPointer(); |
| 5260 | llvm::Value *LHSBegin = LHSAddr.getPointer(); |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5261 | // Cast from pointer to array type to pointer to single element. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5262 | llvm::Value *LHSEnd = CGF.Builder.CreateGEP(LHSBegin, NumElements); |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5263 | // The basic structure here is a while-do loop. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5264 | llvm::BasicBlock *BodyBB = CGF.createBasicBlock("omp.arraycpy.body"); |
| 5265 | llvm::BasicBlock *DoneBB = CGF.createBasicBlock("omp.arraycpy.done"); |
| 5266 | llvm::Value *IsEmpty = |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5267 | CGF.Builder.CreateICmpEQ(LHSBegin, LHSEnd, "omp.arraycpy.isempty"); |
| 5268 | CGF.Builder.CreateCondBr(IsEmpty, DoneBB, BodyBB); |
| 5269 | |
| 5270 | // Enter the loop body, making that address the current address. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5271 | llvm::BasicBlock *EntryBB = CGF.Builder.GetInsertBlock(); |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5272 | CGF.EmitBlock(BodyBB); |
| 5273 | |
| 5274 | CharUnits ElementSize = CGF.getContext().getTypeSizeInChars(ElementTy); |
| 5275 | |
| 5276 | llvm::PHINode *RHSElementPHI = CGF.Builder.CreatePHI( |
| 5277 | RHSBegin->getType(), 2, "omp.arraycpy.srcElementPast"); |
| 5278 | RHSElementPHI->addIncoming(RHSBegin, EntryBB); |
| 5279 | Address RHSElementCurrent = |
| 5280 | Address(RHSElementPHI, |
| 5281 | RHSAddr.getAlignment().alignmentOfArrayElement(ElementSize)); |
| 5282 | |
| 5283 | llvm::PHINode *LHSElementPHI = CGF.Builder.CreatePHI( |
| 5284 | LHSBegin->getType(), 2, "omp.arraycpy.destElementPast"); |
| 5285 | LHSElementPHI->addIncoming(LHSBegin, EntryBB); |
| 5286 | Address LHSElementCurrent = |
| 5287 | Address(LHSElementPHI, |
| 5288 | LHSAddr.getAlignment().alignmentOfArrayElement(ElementSize)); |
| 5289 | |
| 5290 | // Emit copy. |
| 5291 | CodeGenFunction::OMPPrivateScope Scope(CGF); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5292 | Scope.addPrivate(LHSVar, [=]() { return LHSElementCurrent; }); |
| 5293 | Scope.addPrivate(RHSVar, [=]() { return RHSElementCurrent; }); |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5294 | Scope.Privatize(); |
| 5295 | RedOpGen(CGF, XExpr, EExpr, UpExpr); |
| 5296 | Scope.ForceCleanup(); |
| 5297 | |
| 5298 | // Shift the address forward by one element. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5299 | llvm::Value *LHSElementNext = CGF.Builder.CreateConstGEP1_32( |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5300 | LHSElementPHI, /*Idx0=*/1, "omp.arraycpy.dest.element"); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5301 | llvm::Value *RHSElementNext = CGF.Builder.CreateConstGEP1_32( |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5302 | RHSElementPHI, /*Idx0=*/1, "omp.arraycpy.src.element"); |
| 5303 | // Check whether we've reached the end. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5304 | llvm::Value *Done = |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5305 | CGF.Builder.CreateICmpEQ(LHSElementNext, LHSEnd, "omp.arraycpy.done"); |
| 5306 | CGF.Builder.CreateCondBr(Done, DoneBB, BodyBB); |
| 5307 | LHSElementPHI->addIncoming(LHSElementNext, CGF.Builder.GetInsertBlock()); |
| 5308 | RHSElementPHI->addIncoming(RHSElementNext, CGF.Builder.GetInsertBlock()); |
| 5309 | |
| 5310 | // Done. |
| 5311 | CGF.EmitBlock(DoneBB, /*IsFinished=*/true); |
| 5312 | } |
| 5313 | |
Alexey Bataev | a839ddd | 2016-03-17 10:19:46 +0000 | [diff] [blame] | 5314 | /// Emit reduction combiner. If the combiner is a simple expression emit it as |
| 5315 | /// is, otherwise consider it as combiner of UDR decl and emit it as a call of |
| 5316 | /// UDR combiner function. |
| 5317 | static void emitReductionCombiner(CodeGenFunction &CGF, |
| 5318 | const Expr *ReductionOp) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5319 | if (const auto *CE = dyn_cast<CallExpr>(ReductionOp)) |
| 5320 | if (const auto *OVE = dyn_cast<OpaqueValueExpr>(CE->getCallee())) |
| 5321 | if (const auto *DRE = |
Alexey Bataev | a839ddd | 2016-03-17 10:19:46 +0000 | [diff] [blame] | 5322 | dyn_cast<DeclRefExpr>(OVE->getSourceExpr()->IgnoreImpCasts())) |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5323 | if (const auto *DRD = |
| 5324 | dyn_cast<OMPDeclareReductionDecl>(DRE->getDecl())) { |
Alexey Bataev | a839ddd | 2016-03-17 10:19:46 +0000 | [diff] [blame] | 5325 | std::pair<llvm::Function *, llvm::Function *> Reduction = |
| 5326 | CGF.CGM.getOpenMPRuntime().getUserDefinedReduction(DRD); |
| 5327 | RValue Func = RValue::get(Reduction.first); |
| 5328 | CodeGenFunction::OpaqueValueMapping Map(CGF, OVE, Func); |
| 5329 | CGF.EmitIgnoredExpr(ReductionOp); |
| 5330 | return; |
| 5331 | } |
| 5332 | CGF.EmitIgnoredExpr(ReductionOp); |
| 5333 | } |
| 5334 | |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 5335 | llvm::Value *CGOpenMPRuntime::emitReductionFunction( |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 5336 | CodeGenModule &CGM, SourceLocation Loc, llvm::Type *ArgsType, |
| 5337 | ArrayRef<const Expr *> Privates, ArrayRef<const Expr *> LHSExprs, |
| 5338 | ArrayRef<const Expr *> RHSExprs, ArrayRef<const Expr *> ReductionOps) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5339 | ASTContext &C = CGM.getContext(); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5340 | |
| 5341 | // void reduction_func(void *LHSArg, void *RHSArg); |
| 5342 | FunctionArgList Args; |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 5343 | ImplicitParamDecl LHSArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, C.VoidPtrTy, |
| 5344 | ImplicitParamDecl::Other); |
| 5345 | ImplicitParamDecl RHSArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, C.VoidPtrTy, |
| 5346 | ImplicitParamDecl::Other); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5347 | Args.push_back(&LHSArg); |
| 5348 | Args.push_back(&RHSArg); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5349 | const auto &CGFI = |
| 5350 | CGM.getTypes().arrangeBuiltinFunctionDeclaration(C.VoidTy, Args); |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 5351 | std::string Name = getName({"omp", "reduction", "reduction_func"}); |
| 5352 | auto *Fn = llvm::Function::Create(CGM.getTypes().GetFunctionType(CGFI), |
| 5353 | llvm::GlobalValue::InternalLinkage, Name, |
| 5354 | &CGM.getModule()); |
Rafael Espindola | 51ec5a9 | 2018-02-28 23:46:35 +0000 | [diff] [blame] | 5355 | CGM.SetInternalFunctionAttributes(GlobalDecl(), Fn, CGFI); |
Alexey Bataev | c0f879b | 2018-04-10 20:10:53 +0000 | [diff] [blame] | 5356 | Fn->setDoesNotRecurse(); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5357 | CodeGenFunction CGF(CGM); |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 5358 | CGF.StartFunction(GlobalDecl(), C.VoidTy, Fn, CGFI, Args, Loc, Loc); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5359 | |
| 5360 | // Dst = (void*[n])(LHSArg); |
| 5361 | // Src = (void*[n])(RHSArg); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5362 | Address LHS(CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
| 5363 | CGF.Builder.CreateLoad(CGF.GetAddrOfLocalVar(&LHSArg)), |
| 5364 | ArgsType), CGF.getPointerAlign()); |
| 5365 | Address RHS(CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
| 5366 | CGF.Builder.CreateLoad(CGF.GetAddrOfLocalVar(&RHSArg)), |
| 5367 | ArgsType), CGF.getPointerAlign()); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5368 | |
| 5369 | // ... |
| 5370 | // *(Type<i>*)lhs[i] = RedOp<i>(*(Type<i>*)lhs[i], *(Type<i>*)rhs[i]); |
| 5371 | // ... |
| 5372 | CodeGenFunction::OMPPrivateScope Scope(CGF); |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5373 | auto IPriv = Privates.begin(); |
| 5374 | unsigned Idx = 0; |
| 5375 | for (unsigned I = 0, E = ReductionOps.size(); I < E; ++I, ++IPriv, ++Idx) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5376 | const auto *RHSVar = |
| 5377 | cast<VarDecl>(cast<DeclRefExpr>(RHSExprs[I])->getDecl()); |
| 5378 | Scope.addPrivate(RHSVar, [&CGF, RHS, Idx, RHSVar]() { |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5379 | return emitAddrOfVarFromArray(CGF, RHS, Idx, RHSVar); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5380 | }); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5381 | const auto *LHSVar = |
| 5382 | cast<VarDecl>(cast<DeclRefExpr>(LHSExprs[I])->getDecl()); |
| 5383 | Scope.addPrivate(LHSVar, [&CGF, LHS, Idx, LHSVar]() { |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5384 | return emitAddrOfVarFromArray(CGF, LHS, Idx, LHSVar); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5385 | }); |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5386 | QualType PrivTy = (*IPriv)->getType(); |
Alexey Bataev | 1189bd0 | 2016-01-26 12:20:39 +0000 | [diff] [blame] | 5387 | if (PrivTy->isVariablyModifiedType()) { |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5388 | // Get array size and emit VLA type. |
| 5389 | ++Idx; |
| 5390 | Address Elem = |
| 5391 | CGF.Builder.CreateConstArrayGEP(LHS, Idx, CGF.getPointerSize()); |
| 5392 | llvm::Value *Ptr = CGF.Builder.CreateLoad(Elem); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5393 | const VariableArrayType *VLA = |
| 5394 | CGF.getContext().getAsVariableArrayType(PrivTy); |
| 5395 | const auto *OVE = cast<OpaqueValueExpr>(VLA->getSizeExpr()); |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5396 | CodeGenFunction::OpaqueValueMapping OpaqueMap( |
Alexey Bataev | 1189bd0 | 2016-01-26 12:20:39 +0000 | [diff] [blame] | 5397 | CGF, OVE, RValue::get(CGF.Builder.CreatePtrToInt(Ptr, CGF.SizeTy))); |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5398 | CGF.EmitVariablyModifiedType(PrivTy); |
| 5399 | } |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5400 | } |
| 5401 | Scope.Privatize(); |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5402 | IPriv = Privates.begin(); |
| 5403 | auto ILHS = LHSExprs.begin(); |
| 5404 | auto IRHS = RHSExprs.begin(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5405 | for (const Expr *E : ReductionOps) { |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5406 | if ((*IPriv)->getType()->isArrayType()) { |
| 5407 | // Emit reduction for array section. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5408 | const auto *LHSVar = cast<VarDecl>(cast<DeclRefExpr>(*ILHS)->getDecl()); |
| 5409 | const auto *RHSVar = cast<VarDecl>(cast<DeclRefExpr>(*IRHS)->getDecl()); |
Alexey Bataev | a839ddd | 2016-03-17 10:19:46 +0000 | [diff] [blame] | 5410 | EmitOMPAggregateReduction( |
| 5411 | CGF, (*IPriv)->getType(), LHSVar, RHSVar, |
| 5412 | [=](CodeGenFunction &CGF, const Expr *, const Expr *, const Expr *) { |
| 5413 | emitReductionCombiner(CGF, E); |
| 5414 | }); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5415 | } else { |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5416 | // Emit reduction for array subscript or single variable. |
Alexey Bataev | a839ddd | 2016-03-17 10:19:46 +0000 | [diff] [blame] | 5417 | emitReductionCombiner(CGF, E); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5418 | } |
Richard Trieu | cc3949d | 2016-02-18 22:34:54 +0000 | [diff] [blame] | 5419 | ++IPriv; |
| 5420 | ++ILHS; |
| 5421 | ++IRHS; |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5422 | } |
| 5423 | Scope.ForceCleanup(); |
| 5424 | CGF.FinishFunction(); |
| 5425 | return Fn; |
| 5426 | } |
| 5427 | |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 5428 | void CGOpenMPRuntime::emitSingleReductionCombiner(CodeGenFunction &CGF, |
| 5429 | const Expr *ReductionOp, |
| 5430 | const Expr *PrivateRef, |
| 5431 | const DeclRefExpr *LHS, |
| 5432 | const DeclRefExpr *RHS) { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5433 | if (PrivateRef->getType()->isArrayType()) { |
| 5434 | // Emit reduction for array section. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5435 | const auto *LHSVar = cast<VarDecl>(LHS->getDecl()); |
| 5436 | const auto *RHSVar = cast<VarDecl>(RHS->getDecl()); |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5437 | EmitOMPAggregateReduction( |
| 5438 | CGF, PrivateRef->getType(), LHSVar, RHSVar, |
| 5439 | [=](CodeGenFunction &CGF, const Expr *, const Expr *, const Expr *) { |
| 5440 | emitReductionCombiner(CGF, ReductionOp); |
| 5441 | }); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5442 | } else { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5443 | // Emit reduction for array subscript or single variable. |
| 5444 | emitReductionCombiner(CGF, ReductionOp); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5445 | } |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5446 | } |
| 5447 | |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5448 | void CGOpenMPRuntime::emitReduction(CodeGenFunction &CGF, SourceLocation Loc, |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5449 | ArrayRef<const Expr *> Privates, |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5450 | ArrayRef<const Expr *> LHSExprs, |
| 5451 | ArrayRef<const Expr *> RHSExprs, |
| 5452 | ArrayRef<const Expr *> ReductionOps, |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 5453 | ReductionOptionsTy Options) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 5454 | if (!CGF.HaveInsertPoint()) |
| 5455 | return; |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 5456 | |
| 5457 | bool WithNowait = Options.WithNowait; |
| 5458 | bool SimpleReduction = Options.SimpleReduction; |
| 5459 | |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5460 | // Next code should be emitted for reduction: |
| 5461 | // |
| 5462 | // static kmp_critical_name lock = { 0 }; |
| 5463 | // |
| 5464 | // void reduce_func(void *lhs[<n>], void *rhs[<n>]) { |
| 5465 | // *(Type0*)lhs[0] = ReductionOperation0(*(Type0*)lhs[0], *(Type0*)rhs[0]); |
| 5466 | // ... |
| 5467 | // *(Type<n>-1*)lhs[<n>-1] = ReductionOperation<n>-1(*(Type<n>-1*)lhs[<n>-1], |
| 5468 | // *(Type<n>-1*)rhs[<n>-1]); |
| 5469 | // } |
| 5470 | // |
| 5471 | // ... |
| 5472 | // void *RedList[<n>] = {&<RHSExprs>[0], ..., &<RHSExprs>[<n>-1]}; |
| 5473 | // switch (__kmpc_reduce{_nowait}(<loc>, <gtid>, <n>, sizeof(RedList), |
| 5474 | // RedList, reduce_func, &<lock>)) { |
| 5475 | // case 1: |
| 5476 | // ... |
| 5477 | // <LHSExprs>[i] = RedOp<i>(*<LHSExprs>[i], *<RHSExprs>[i]); |
| 5478 | // ... |
| 5479 | // __kmpc_end_reduce{_nowait}(<loc>, <gtid>, &<lock>); |
| 5480 | // break; |
| 5481 | // case 2: |
| 5482 | // ... |
| 5483 | // Atomic(<LHSExprs>[i] = RedOp<i>(*<LHSExprs>[i], *<RHSExprs>[i])); |
| 5484 | // ... |
Alexey Bataev | 69a4779 | 2015-05-07 03:54:03 +0000 | [diff] [blame] | 5485 | // [__kmpc_end_reduce(<loc>, <gtid>, &<lock>);] |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5486 | // break; |
| 5487 | // default:; |
| 5488 | // } |
Alexey Bataev | 89e7e8e | 2015-06-17 06:21:39 +0000 | [diff] [blame] | 5489 | // |
| 5490 | // if SimpleReduction is true, only the next code is generated: |
| 5491 | // ... |
| 5492 | // <LHSExprs>[i] = RedOp<i>(*<LHSExprs>[i], *<RHSExprs>[i]); |
| 5493 | // ... |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5494 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5495 | ASTContext &C = CGM.getContext(); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5496 | |
Alexey Bataev | 89e7e8e | 2015-06-17 06:21:39 +0000 | [diff] [blame] | 5497 | if (SimpleReduction) { |
| 5498 | CodeGenFunction::RunCleanupsScope Scope(CGF); |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5499 | auto IPriv = Privates.begin(); |
| 5500 | auto ILHS = LHSExprs.begin(); |
| 5501 | auto IRHS = RHSExprs.begin(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5502 | for (const Expr *E : ReductionOps) { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5503 | emitSingleReductionCombiner(CGF, E, *IPriv, cast<DeclRefExpr>(*ILHS), |
| 5504 | cast<DeclRefExpr>(*IRHS)); |
Richard Trieu | cc3949d | 2016-02-18 22:34:54 +0000 | [diff] [blame] | 5505 | ++IPriv; |
| 5506 | ++ILHS; |
| 5507 | ++IRHS; |
Alexey Bataev | 89e7e8e | 2015-06-17 06:21:39 +0000 | [diff] [blame] | 5508 | } |
| 5509 | return; |
| 5510 | } |
| 5511 | |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5512 | // 1. Build a list of reduction variables. |
| 5513 | // void *RedList[<n>] = {<ReductionVars>[0], ..., <ReductionVars>[<n>-1]}; |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5514 | auto Size = RHSExprs.size(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5515 | for (const Expr *E : Privates) { |
Alexey Bataev | 1189bd0 | 2016-01-26 12:20:39 +0000 | [diff] [blame] | 5516 | if (E->getType()->isVariablyModifiedType()) |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5517 | // Reserve place for array size. |
| 5518 | ++Size; |
| 5519 | } |
| 5520 | llvm::APInt ArraySize(/*unsigned int numBits=*/32, Size); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5521 | QualType ReductionArrayTy = |
| 5522 | C.getConstantArrayType(C.VoidPtrTy, ArraySize, ArrayType::Normal, |
| 5523 | /*IndexTypeQuals=*/0); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5524 | Address ReductionList = |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5525 | CGF.CreateMemTemp(ReductionArrayTy, ".omp.reduction.red_list"); |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5526 | auto IPriv = Privates.begin(); |
| 5527 | unsigned Idx = 0; |
| 5528 | for (unsigned I = 0, E = RHSExprs.size(); I < E; ++I, ++IPriv, ++Idx) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5529 | Address Elem = |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5530 | CGF.Builder.CreateConstArrayGEP(ReductionList, Idx, CGF.getPointerSize()); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5531 | CGF.Builder.CreateStore( |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5532 | CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5533 | CGF.EmitLValue(RHSExprs[I]).getPointer(), CGF.VoidPtrTy), |
| 5534 | Elem); |
Alexey Bataev | 1189bd0 | 2016-01-26 12:20:39 +0000 | [diff] [blame] | 5535 | if ((*IPriv)->getType()->isVariablyModifiedType()) { |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5536 | // Store array size. |
| 5537 | ++Idx; |
| 5538 | Elem = CGF.Builder.CreateConstArrayGEP(ReductionList, Idx, |
| 5539 | CGF.getPointerSize()); |
Alexey Bataev | 1189bd0 | 2016-01-26 12:20:39 +0000 | [diff] [blame] | 5540 | llvm::Value *Size = CGF.Builder.CreateIntCast( |
| 5541 | CGF.getVLASize( |
| 5542 | CGF.getContext().getAsVariableArrayType((*IPriv)->getType())) |
Sander de Smalen | 891af03a | 2018-02-03 13:55:59 +0000 | [diff] [blame] | 5543 | .NumElts, |
Alexey Bataev | 1189bd0 | 2016-01-26 12:20:39 +0000 | [diff] [blame] | 5544 | CGF.SizeTy, /*isSigned=*/false); |
| 5545 | CGF.Builder.CreateStore(CGF.Builder.CreateIntToPtr(Size, CGF.VoidPtrTy), |
| 5546 | Elem); |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5547 | } |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5548 | } |
| 5549 | |
| 5550 | // 2. Emit reduce_func(). |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5551 | llvm::Value *ReductionFn = emitReductionFunction( |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 5552 | CGM, Loc, CGF.ConvertTypeForMem(ReductionArrayTy)->getPointerTo(), |
| 5553 | Privates, LHSExprs, RHSExprs, ReductionOps); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5554 | |
| 5555 | // 3. Create static kmp_critical_name lock = { 0 }; |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 5556 | std::string Name = getName({"reduction"}); |
| 5557 | llvm::Value *Lock = getCriticalRegionLock(Name); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5558 | |
| 5559 | // 4. Build res = __kmpc_reduce{_nowait}(<loc>, <gtid>, <n>, sizeof(RedList), |
| 5560 | // RedList, reduce_func, &<lock>); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5561 | llvm::Value *IdentTLoc = emitUpdateLocation(CGF, Loc, OMP_ATOMIC_REDUCE); |
| 5562 | llvm::Value *ThreadId = getThreadID(CGF, Loc); |
| 5563 | llvm::Value *ReductionArrayTySize = CGF.getTypeSize(ReductionArrayTy); |
| 5564 | llvm::Value *RL = CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
Samuel Antao | 4c8035b | 2016-12-12 18:00:20 +0000 | [diff] [blame] | 5565 | ReductionList.getPointer(), CGF.VoidPtrTy); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5566 | llvm::Value *Args[] = { |
| 5567 | IdentTLoc, // ident_t *<loc> |
| 5568 | ThreadId, // i32 <gtid> |
| 5569 | CGF.Builder.getInt32(RHSExprs.size()), // i32 <n> |
| 5570 | ReductionArrayTySize, // size_type sizeof(RedList) |
| 5571 | RL, // void *RedList |
| 5572 | ReductionFn, // void (*) (void *, void *) <reduce_func> |
| 5573 | Lock // kmp_critical_name *&<lock> |
| 5574 | }; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5575 | llvm::Value *Res = CGF.EmitRuntimeCall( |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5576 | createRuntimeFunction(WithNowait ? OMPRTL__kmpc_reduce_nowait |
| 5577 | : OMPRTL__kmpc_reduce), |
| 5578 | Args); |
| 5579 | |
| 5580 | // 5. Build switch(res) |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5581 | llvm::BasicBlock *DefaultBB = CGF.createBasicBlock(".omp.reduction.default"); |
| 5582 | llvm::SwitchInst *SwInst = |
| 5583 | CGF.Builder.CreateSwitch(Res, DefaultBB, /*NumCases=*/2); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5584 | |
| 5585 | // 6. Build case 1: |
| 5586 | // ... |
| 5587 | // <LHSExprs>[i] = RedOp<i>(*<LHSExprs>[i], *<RHSExprs>[i]); |
| 5588 | // ... |
| 5589 | // __kmpc_end_reduce{_nowait}(<loc>, <gtid>, &<lock>); |
| 5590 | // break; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5591 | llvm::BasicBlock *Case1BB = CGF.createBasicBlock(".omp.reduction.case1"); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5592 | SwInst->addCase(CGF.Builder.getInt32(1), Case1BB); |
| 5593 | CGF.EmitBlock(Case1BB); |
| 5594 | |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5595 | // Add emission of __kmpc_end_reduce{_nowait}(<loc>, <gtid>, &<lock>); |
| 5596 | llvm::Value *EndArgs[] = { |
| 5597 | IdentTLoc, // ident_t *<loc> |
| 5598 | ThreadId, // i32 <gtid> |
| 5599 | Lock // kmp_critical_name *&<lock> |
| 5600 | }; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5601 | auto &&CodeGen = [Privates, LHSExprs, RHSExprs, ReductionOps]( |
| 5602 | CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 5603 | CGOpenMPRuntime &RT = CGF.CGM.getOpenMPRuntime(); |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5604 | auto IPriv = Privates.begin(); |
| 5605 | auto ILHS = LHSExprs.begin(); |
| 5606 | auto IRHS = RHSExprs.begin(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5607 | for (const Expr *E : ReductionOps) { |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 5608 | RT.emitSingleReductionCombiner(CGF, E, *IPriv, cast<DeclRefExpr>(*ILHS), |
| 5609 | cast<DeclRefExpr>(*IRHS)); |
Richard Trieu | cc3949d | 2016-02-18 22:34:54 +0000 | [diff] [blame] | 5610 | ++IPriv; |
| 5611 | ++ILHS; |
| 5612 | ++IRHS; |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5613 | } |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5614 | }; |
| 5615 | RegionCodeGenTy RCG(CodeGen); |
| 5616 | CommonActionTy Action( |
| 5617 | nullptr, llvm::None, |
| 5618 | createRuntimeFunction(WithNowait ? OMPRTL__kmpc_end_reduce_nowait |
| 5619 | : OMPRTL__kmpc_end_reduce), |
| 5620 | EndArgs); |
| 5621 | RCG.setAction(Action); |
| 5622 | RCG(CGF); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5623 | |
| 5624 | CGF.EmitBranch(DefaultBB); |
| 5625 | |
| 5626 | // 7. Build case 2: |
| 5627 | // ... |
| 5628 | // Atomic(<LHSExprs>[i] = RedOp<i>(*<LHSExprs>[i], *<RHSExprs>[i])); |
| 5629 | // ... |
| 5630 | // break; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5631 | llvm::BasicBlock *Case2BB = CGF.createBasicBlock(".omp.reduction.case2"); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5632 | SwInst->addCase(CGF.Builder.getInt32(2), Case2BB); |
| 5633 | CGF.EmitBlock(Case2BB); |
| 5634 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5635 | auto &&AtomicCodeGen = [Loc, Privates, LHSExprs, RHSExprs, ReductionOps]( |
| 5636 | CodeGenFunction &CGF, PrePostActionTy &Action) { |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5637 | auto ILHS = LHSExprs.begin(); |
| 5638 | auto IRHS = RHSExprs.begin(); |
| 5639 | auto IPriv = Privates.begin(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5640 | for (const Expr *E : ReductionOps) { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5641 | const Expr *XExpr = nullptr; |
| 5642 | const Expr *EExpr = nullptr; |
| 5643 | const Expr *UpExpr = nullptr; |
| 5644 | BinaryOperatorKind BO = BO_Comma; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5645 | if (const auto *BO = dyn_cast<BinaryOperator>(E)) { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5646 | if (BO->getOpcode() == BO_Assign) { |
| 5647 | XExpr = BO->getLHS(); |
| 5648 | UpExpr = BO->getRHS(); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5649 | } |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5650 | } |
| 5651 | // Try to emit update expression as a simple atomic. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5652 | const Expr *RHSExpr = UpExpr; |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5653 | if (RHSExpr) { |
| 5654 | // Analyze RHS part of the whole expression. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5655 | if (const auto *ACO = dyn_cast<AbstractConditionalOperator>( |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5656 | RHSExpr->IgnoreParenImpCasts())) { |
| 5657 | // If this is a conditional operator, analyze its condition for |
| 5658 | // min/max reduction operator. |
| 5659 | RHSExpr = ACO->getCond(); |
Alexey Bataev | 69a4779 | 2015-05-07 03:54:03 +0000 | [diff] [blame] | 5660 | } |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5661 | if (const auto *BORHS = |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5662 | dyn_cast<BinaryOperator>(RHSExpr->IgnoreParenImpCasts())) { |
| 5663 | EExpr = BORHS->getRHS(); |
| 5664 | BO = BORHS->getOpcode(); |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5665 | } |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5666 | } |
| 5667 | if (XExpr) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5668 | const auto *VD = cast<VarDecl>(cast<DeclRefExpr>(*ILHS)->getDecl()); |
Malcolm Parsons | c6e4583 | 2017-01-13 18:55:32 +0000 | [diff] [blame] | 5669 | auto &&AtomicRedGen = [BO, VD, |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5670 | Loc](CodeGenFunction &CGF, const Expr *XExpr, |
| 5671 | const Expr *EExpr, const Expr *UpExpr) { |
| 5672 | LValue X = CGF.EmitLValue(XExpr); |
| 5673 | RValue E; |
| 5674 | if (EExpr) |
| 5675 | E = CGF.EmitAnyExpr(EExpr); |
| 5676 | CGF.EmitOMPAtomicSimpleUpdateExpr( |
JF Bastien | 92f4ef1 | 2016-04-06 17:26:42 +0000 | [diff] [blame] | 5677 | X, E, BO, /*IsXLHSInRHSPart=*/true, |
| 5678 | llvm::AtomicOrdering::Monotonic, Loc, |
Malcolm Parsons | c6e4583 | 2017-01-13 18:55:32 +0000 | [diff] [blame] | 5679 | [&CGF, UpExpr, VD, Loc](RValue XRValue) { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5680 | CodeGenFunction::OMPPrivateScope PrivateScope(CGF); |
| 5681 | PrivateScope.addPrivate( |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5682 | VD, [&CGF, VD, XRValue, Loc]() { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5683 | Address LHSTemp = CGF.CreateMemTemp(VD->getType()); |
| 5684 | CGF.emitOMPSimpleStore( |
| 5685 | CGF.MakeAddrLValue(LHSTemp, VD->getType()), XRValue, |
| 5686 | VD->getType().getNonReferenceType(), Loc); |
| 5687 | return LHSTemp; |
| 5688 | }); |
| 5689 | (void)PrivateScope.Privatize(); |
| 5690 | return CGF.EmitAnyExpr(UpExpr); |
| 5691 | }); |
| 5692 | }; |
| 5693 | if ((*IPriv)->getType()->isArrayType()) { |
| 5694 | // Emit atomic reduction for array section. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5695 | const auto *RHSVar = |
| 5696 | cast<VarDecl>(cast<DeclRefExpr>(*IRHS)->getDecl()); |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5697 | EmitOMPAggregateReduction(CGF, (*IPriv)->getType(), VD, RHSVar, |
| 5698 | AtomicRedGen, XExpr, EExpr, UpExpr); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5699 | } else { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5700 | // Emit atomic reduction for array subscript or single variable. |
| 5701 | AtomicRedGen(CGF, XExpr, EExpr, UpExpr); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5702 | } |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5703 | } else { |
| 5704 | // Emit as a critical region. |
| 5705 | auto &&CritRedGen = [E, Loc](CodeGenFunction &CGF, const Expr *, |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 5706 | const Expr *, const Expr *) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5707 | CGOpenMPRuntime &RT = CGF.CGM.getOpenMPRuntime(); |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 5708 | std::string Name = RT.getName({"atomic_reduction"}); |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5709 | RT.emitCriticalRegion( |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 5710 | CGF, Name, |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5711 | [=](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 5712 | Action.Enter(CGF); |
| 5713 | emitReductionCombiner(CGF, E); |
| 5714 | }, |
| 5715 | Loc); |
| 5716 | }; |
| 5717 | if ((*IPriv)->getType()->isArrayType()) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5718 | const auto *LHSVar = |
| 5719 | cast<VarDecl>(cast<DeclRefExpr>(*ILHS)->getDecl()); |
| 5720 | const auto *RHSVar = |
| 5721 | cast<VarDecl>(cast<DeclRefExpr>(*IRHS)->getDecl()); |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5722 | EmitOMPAggregateReduction(CGF, (*IPriv)->getType(), LHSVar, RHSVar, |
| 5723 | CritRedGen); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5724 | } else { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5725 | CritRedGen(CGF, nullptr, nullptr, nullptr); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5726 | } |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5727 | } |
Richard Trieu | cc3949d | 2016-02-18 22:34:54 +0000 | [diff] [blame] | 5728 | ++ILHS; |
| 5729 | ++IRHS; |
| 5730 | ++IPriv; |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5731 | } |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5732 | }; |
| 5733 | RegionCodeGenTy AtomicRCG(AtomicCodeGen); |
| 5734 | if (!WithNowait) { |
| 5735 | // Add emission of __kmpc_end_reduce(<loc>, <gtid>, &<lock>); |
| 5736 | llvm::Value *EndArgs[] = { |
| 5737 | IdentTLoc, // ident_t *<loc> |
| 5738 | ThreadId, // i32 <gtid> |
| 5739 | Lock // kmp_critical_name *&<lock> |
| 5740 | }; |
| 5741 | CommonActionTy Action(nullptr, llvm::None, |
| 5742 | createRuntimeFunction(OMPRTL__kmpc_end_reduce), |
| 5743 | EndArgs); |
| 5744 | AtomicRCG.setAction(Action); |
| 5745 | AtomicRCG(CGF); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5746 | } else { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5747 | AtomicRCG(CGF); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5748 | } |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5749 | |
| 5750 | CGF.EmitBranch(DefaultBB); |
| 5751 | CGF.EmitBlock(DefaultBB, /*IsFinished=*/true); |
| 5752 | } |
| 5753 | |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 5754 | /// Generates unique name for artificial threadprivate variables. |
Alexey Bataev | 1c44e15 | 2018-03-06 18:59:43 +0000 | [diff] [blame] | 5755 | /// Format is: <Prefix> "." <Decl_mangled_name> "_" "<Decl_start_loc_raw_enc>" |
| 5756 | static std::string generateUniqueName(CodeGenModule &CGM, StringRef Prefix, |
| 5757 | const Expr *Ref) { |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 5758 | SmallString<256> Buffer; |
| 5759 | llvm::raw_svector_ostream Out(Buffer); |
Alexey Bataev | 1c44e15 | 2018-03-06 18:59:43 +0000 | [diff] [blame] | 5760 | const clang::DeclRefExpr *DE; |
| 5761 | const VarDecl *D = ::getBaseDecl(Ref, DE); |
| 5762 | if (!D) |
| 5763 | D = cast<VarDecl>(cast<DeclRefExpr>(Ref)->getDecl()); |
| 5764 | D = D->getCanonicalDecl(); |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 5765 | std::string Name = CGM.getOpenMPRuntime().getName( |
| 5766 | {D->isLocalVarDeclOrParm() ? D->getName() : CGM.getMangledName(D)}); |
| 5767 | Out << Prefix << Name << "_" |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 5768 | << D->getCanonicalDecl()->getBeginLoc().getRawEncoding(); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 5769 | return Out.str(); |
| 5770 | } |
| 5771 | |
| 5772 | /// Emits reduction initializer function: |
| 5773 | /// \code |
| 5774 | /// void @.red_init(void* %arg) { |
| 5775 | /// %0 = bitcast void* %arg to <type>* |
| 5776 | /// store <type> <init>, <type>* %0 |
| 5777 | /// ret void |
| 5778 | /// } |
| 5779 | /// \endcode |
| 5780 | static llvm::Value *emitReduceInitFunction(CodeGenModule &CGM, |
| 5781 | SourceLocation Loc, |
| 5782 | ReductionCodeGen &RCG, unsigned N) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5783 | ASTContext &C = CGM.getContext(); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 5784 | FunctionArgList Args; |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 5785 | ImplicitParamDecl Param(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, C.VoidPtrTy, |
| 5786 | ImplicitParamDecl::Other); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 5787 | Args.emplace_back(&Param); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5788 | const auto &FnInfo = |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 5789 | CGM.getTypes().arrangeBuiltinFunctionDeclaration(C.VoidTy, Args); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5790 | llvm::FunctionType *FnTy = CGM.getTypes().GetFunctionType(FnInfo); |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 5791 | std::string Name = CGM.getOpenMPRuntime().getName({"red_init", ""}); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 5792 | auto *Fn = llvm::Function::Create(FnTy, llvm::GlobalValue::InternalLinkage, |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 5793 | Name, &CGM.getModule()); |
Rafael Espindola | 51ec5a9 | 2018-02-28 23:46:35 +0000 | [diff] [blame] | 5794 | CGM.SetInternalFunctionAttributes(GlobalDecl(), Fn, FnInfo); |
Alexey Bataev | c0f879b | 2018-04-10 20:10:53 +0000 | [diff] [blame] | 5795 | Fn->setDoesNotRecurse(); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 5796 | CodeGenFunction CGF(CGM); |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 5797 | CGF.StartFunction(GlobalDecl(), C.VoidTy, Fn, FnInfo, Args, Loc, Loc); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 5798 | Address PrivateAddr = CGF.EmitLoadOfPointer( |
| 5799 | CGF.GetAddrOfLocalVar(&Param), |
| 5800 | C.getPointerType(C.VoidPtrTy).castAs<PointerType>()); |
| 5801 | llvm::Value *Size = nullptr; |
| 5802 | // If the size of the reduction item is non-constant, load it from global |
| 5803 | // threadprivate variable. |
| 5804 | if (RCG.getSizes(N).second) { |
| 5805 | Address SizeAddr = CGM.getOpenMPRuntime().getAddrOfArtificialThreadPrivate( |
| 5806 | CGF, CGM.getContext().getSizeType(), |
Alexey Bataev | 1c44e15 | 2018-03-06 18:59:43 +0000 | [diff] [blame] | 5807 | generateUniqueName(CGM, "reduction_size", RCG.getRefExpr(N))); |
Alexey Bataev | a9b9cc0 | 2018-01-23 18:12:38 +0000 | [diff] [blame] | 5808 | Size = CGF.EmitLoadOfScalar(SizeAddr, /*Volatile=*/false, |
| 5809 | CGM.getContext().getSizeType(), Loc); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 5810 | } |
| 5811 | RCG.emitAggregateType(CGF, N, Size); |
| 5812 | LValue SharedLVal; |
| 5813 | // If initializer uses initializer from declare reduction construct, emit a |
| 5814 | // pointer to the address of the original reduction item (reuired by reduction |
| 5815 | // initializer) |
| 5816 | if (RCG.usesReductionInitializer(N)) { |
| 5817 | Address SharedAddr = |
| 5818 | CGM.getOpenMPRuntime().getAddrOfArtificialThreadPrivate( |
| 5819 | CGF, CGM.getContext().VoidPtrTy, |
Alexey Bataev | 1c44e15 | 2018-03-06 18:59:43 +0000 | [diff] [blame] | 5820 | generateUniqueName(CGM, "reduction", RCG.getRefExpr(N))); |
Alexey Bataev | 21dab12 | 2018-03-09 15:20:30 +0000 | [diff] [blame] | 5821 | SharedAddr = CGF.EmitLoadOfPointer( |
| 5822 | SharedAddr, |
| 5823 | CGM.getContext().VoidPtrTy.castAs<PointerType>()->getTypePtr()); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 5824 | SharedLVal = CGF.MakeAddrLValue(SharedAddr, CGM.getContext().VoidPtrTy); |
| 5825 | } else { |
| 5826 | SharedLVal = CGF.MakeNaturalAlignAddrLValue( |
| 5827 | llvm::ConstantPointerNull::get(CGM.VoidPtrTy), |
| 5828 | CGM.getContext().VoidPtrTy); |
| 5829 | } |
| 5830 | // Emit the initializer: |
| 5831 | // %0 = bitcast void* %arg to <type>* |
| 5832 | // store <type> <init>, <type>* %0 |
| 5833 | RCG.emitInitialization(CGF, N, PrivateAddr, SharedLVal, |
| 5834 | [](CodeGenFunction &) { return false; }); |
| 5835 | CGF.FinishFunction(); |
| 5836 | return Fn; |
| 5837 | } |
| 5838 | |
| 5839 | /// Emits reduction combiner function: |
| 5840 | /// \code |
| 5841 | /// void @.red_comb(void* %arg0, void* %arg1) { |
| 5842 | /// %lhs = bitcast void* %arg0 to <type>* |
| 5843 | /// %rhs = bitcast void* %arg1 to <type>* |
| 5844 | /// %2 = <ReductionOp>(<type>* %lhs, <type>* %rhs) |
| 5845 | /// store <type> %2, <type>* %lhs |
| 5846 | /// ret void |
| 5847 | /// } |
| 5848 | /// \endcode |
| 5849 | static llvm::Value *emitReduceCombFunction(CodeGenModule &CGM, |
| 5850 | SourceLocation Loc, |
| 5851 | ReductionCodeGen &RCG, unsigned N, |
| 5852 | const Expr *ReductionOp, |
| 5853 | const Expr *LHS, const Expr *RHS, |
| 5854 | const Expr *PrivateRef) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5855 | ASTContext &C = CGM.getContext(); |
| 5856 | const auto *LHSVD = cast<VarDecl>(cast<DeclRefExpr>(LHS)->getDecl()); |
| 5857 | const auto *RHSVD = cast<VarDecl>(cast<DeclRefExpr>(RHS)->getDecl()); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 5858 | FunctionArgList Args; |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 5859 | ImplicitParamDecl ParamInOut(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, |
| 5860 | C.VoidPtrTy, ImplicitParamDecl::Other); |
| 5861 | ImplicitParamDecl ParamIn(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, C.VoidPtrTy, |
| 5862 | ImplicitParamDecl::Other); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 5863 | Args.emplace_back(&ParamInOut); |
| 5864 | Args.emplace_back(&ParamIn); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5865 | const auto &FnInfo = |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 5866 | CGM.getTypes().arrangeBuiltinFunctionDeclaration(C.VoidTy, Args); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5867 | llvm::FunctionType *FnTy = CGM.getTypes().GetFunctionType(FnInfo); |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 5868 | std::string Name = CGM.getOpenMPRuntime().getName({"red_comb", ""}); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 5869 | auto *Fn = llvm::Function::Create(FnTy, llvm::GlobalValue::InternalLinkage, |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 5870 | Name, &CGM.getModule()); |
Rafael Espindola | 51ec5a9 | 2018-02-28 23:46:35 +0000 | [diff] [blame] | 5871 | CGM.SetInternalFunctionAttributes(GlobalDecl(), Fn, FnInfo); |
Alexey Bataev | c0f879b | 2018-04-10 20:10:53 +0000 | [diff] [blame] | 5872 | Fn->setDoesNotRecurse(); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 5873 | CodeGenFunction CGF(CGM); |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 5874 | CGF.StartFunction(GlobalDecl(), C.VoidTy, Fn, FnInfo, Args, Loc, Loc); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 5875 | llvm::Value *Size = nullptr; |
| 5876 | // If the size of the reduction item is non-constant, load it from global |
| 5877 | // threadprivate variable. |
| 5878 | if (RCG.getSizes(N).second) { |
| 5879 | Address SizeAddr = CGM.getOpenMPRuntime().getAddrOfArtificialThreadPrivate( |
| 5880 | CGF, CGM.getContext().getSizeType(), |
Alexey Bataev | 1c44e15 | 2018-03-06 18:59:43 +0000 | [diff] [blame] | 5881 | generateUniqueName(CGM, "reduction_size", RCG.getRefExpr(N))); |
Alexey Bataev | a9b9cc0 | 2018-01-23 18:12:38 +0000 | [diff] [blame] | 5882 | Size = CGF.EmitLoadOfScalar(SizeAddr, /*Volatile=*/false, |
| 5883 | CGM.getContext().getSizeType(), Loc); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 5884 | } |
| 5885 | RCG.emitAggregateType(CGF, N, Size); |
| 5886 | // Remap lhs and rhs variables to the addresses of the function arguments. |
| 5887 | // %lhs = bitcast void* %arg0 to <type>* |
| 5888 | // %rhs = bitcast void* %arg1 to <type>* |
| 5889 | CodeGenFunction::OMPPrivateScope PrivateScope(CGF); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5890 | PrivateScope.addPrivate(LHSVD, [&C, &CGF, &ParamInOut, LHSVD]() { |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 5891 | // Pull out the pointer to the variable. |
| 5892 | Address PtrAddr = CGF.EmitLoadOfPointer( |
| 5893 | CGF.GetAddrOfLocalVar(&ParamInOut), |
| 5894 | C.getPointerType(C.VoidPtrTy).castAs<PointerType>()); |
| 5895 | return CGF.Builder.CreateElementBitCast( |
| 5896 | PtrAddr, CGF.ConvertTypeForMem(LHSVD->getType())); |
| 5897 | }); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5898 | PrivateScope.addPrivate(RHSVD, [&C, &CGF, &ParamIn, RHSVD]() { |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 5899 | // Pull out the pointer to the variable. |
| 5900 | Address PtrAddr = CGF.EmitLoadOfPointer( |
| 5901 | CGF.GetAddrOfLocalVar(&ParamIn), |
| 5902 | C.getPointerType(C.VoidPtrTy).castAs<PointerType>()); |
| 5903 | return CGF.Builder.CreateElementBitCast( |
| 5904 | PtrAddr, CGF.ConvertTypeForMem(RHSVD->getType())); |
| 5905 | }); |
| 5906 | PrivateScope.Privatize(); |
| 5907 | // Emit the combiner body: |
| 5908 | // %2 = <ReductionOp>(<type> *%lhs, <type> *%rhs) |
| 5909 | // store <type> %2, <type>* %lhs |
| 5910 | CGM.getOpenMPRuntime().emitSingleReductionCombiner( |
| 5911 | CGF, ReductionOp, PrivateRef, cast<DeclRefExpr>(LHS), |
| 5912 | cast<DeclRefExpr>(RHS)); |
| 5913 | CGF.FinishFunction(); |
| 5914 | return Fn; |
| 5915 | } |
| 5916 | |
| 5917 | /// Emits reduction finalizer function: |
| 5918 | /// \code |
| 5919 | /// void @.red_fini(void* %arg) { |
| 5920 | /// %0 = bitcast void* %arg to <type>* |
| 5921 | /// <destroy>(<type>* %0) |
| 5922 | /// ret void |
| 5923 | /// } |
| 5924 | /// \endcode |
| 5925 | static llvm::Value *emitReduceFiniFunction(CodeGenModule &CGM, |
| 5926 | SourceLocation Loc, |
| 5927 | ReductionCodeGen &RCG, unsigned N) { |
| 5928 | if (!RCG.needCleanups(N)) |
| 5929 | return nullptr; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5930 | ASTContext &C = CGM.getContext(); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 5931 | FunctionArgList Args; |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 5932 | ImplicitParamDecl Param(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, C.VoidPtrTy, |
| 5933 | ImplicitParamDecl::Other); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 5934 | Args.emplace_back(&Param); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5935 | const auto &FnInfo = |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 5936 | CGM.getTypes().arrangeBuiltinFunctionDeclaration(C.VoidTy, Args); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5937 | llvm::FunctionType *FnTy = CGM.getTypes().GetFunctionType(FnInfo); |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 5938 | std::string Name = CGM.getOpenMPRuntime().getName({"red_fini", ""}); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 5939 | auto *Fn = llvm::Function::Create(FnTy, llvm::GlobalValue::InternalLinkage, |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 5940 | Name, &CGM.getModule()); |
Rafael Espindola | 51ec5a9 | 2018-02-28 23:46:35 +0000 | [diff] [blame] | 5941 | CGM.SetInternalFunctionAttributes(GlobalDecl(), Fn, FnInfo); |
Alexey Bataev | c0f879b | 2018-04-10 20:10:53 +0000 | [diff] [blame] | 5942 | Fn->setDoesNotRecurse(); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 5943 | CodeGenFunction CGF(CGM); |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 5944 | CGF.StartFunction(GlobalDecl(), C.VoidTy, Fn, FnInfo, Args, Loc, Loc); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 5945 | Address PrivateAddr = CGF.EmitLoadOfPointer( |
| 5946 | CGF.GetAddrOfLocalVar(&Param), |
| 5947 | C.getPointerType(C.VoidPtrTy).castAs<PointerType>()); |
| 5948 | llvm::Value *Size = nullptr; |
| 5949 | // If the size of the reduction item is non-constant, load it from global |
| 5950 | // threadprivate variable. |
| 5951 | if (RCG.getSizes(N).second) { |
| 5952 | Address SizeAddr = CGM.getOpenMPRuntime().getAddrOfArtificialThreadPrivate( |
| 5953 | CGF, CGM.getContext().getSizeType(), |
Alexey Bataev | 1c44e15 | 2018-03-06 18:59:43 +0000 | [diff] [blame] | 5954 | generateUniqueName(CGM, "reduction_size", RCG.getRefExpr(N))); |
Alexey Bataev | a9b9cc0 | 2018-01-23 18:12:38 +0000 | [diff] [blame] | 5955 | Size = CGF.EmitLoadOfScalar(SizeAddr, /*Volatile=*/false, |
| 5956 | CGM.getContext().getSizeType(), Loc); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 5957 | } |
| 5958 | RCG.emitAggregateType(CGF, N, Size); |
| 5959 | // Emit the finalizer body: |
| 5960 | // <destroy>(<type>* %0) |
| 5961 | RCG.emitCleanups(CGF, N, PrivateAddr); |
| 5962 | CGF.FinishFunction(); |
| 5963 | return Fn; |
| 5964 | } |
| 5965 | |
| 5966 | llvm::Value *CGOpenMPRuntime::emitTaskReductionInit( |
| 5967 | CodeGenFunction &CGF, SourceLocation Loc, ArrayRef<const Expr *> LHSExprs, |
| 5968 | ArrayRef<const Expr *> RHSExprs, const OMPTaskDataTy &Data) { |
| 5969 | if (!CGF.HaveInsertPoint() || Data.ReductionVars.empty()) |
| 5970 | return nullptr; |
| 5971 | |
| 5972 | // Build typedef struct: |
| 5973 | // kmp_task_red_input { |
| 5974 | // void *reduce_shar; // shared reduction item |
| 5975 | // size_t reduce_size; // size of data item |
| 5976 | // void *reduce_init; // data initialization routine |
| 5977 | // void *reduce_fini; // data finalization routine |
| 5978 | // void *reduce_comb; // data combiner routine |
| 5979 | // kmp_task_red_flags_t flags; // flags for additional info from compiler |
| 5980 | // } kmp_task_red_input_t; |
| 5981 | ASTContext &C = CGM.getContext(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5982 | RecordDecl *RD = C.buildImplicitRecord("kmp_task_red_input_t"); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 5983 | RD->startDefinition(); |
| 5984 | const FieldDecl *SharedFD = addFieldToRecordDecl(C, RD, C.VoidPtrTy); |
| 5985 | const FieldDecl *SizeFD = addFieldToRecordDecl(C, RD, C.getSizeType()); |
| 5986 | const FieldDecl *InitFD = addFieldToRecordDecl(C, RD, C.VoidPtrTy); |
| 5987 | const FieldDecl *FiniFD = addFieldToRecordDecl(C, RD, C.VoidPtrTy); |
| 5988 | const FieldDecl *CombFD = addFieldToRecordDecl(C, RD, C.VoidPtrTy); |
| 5989 | const FieldDecl *FlagsFD = addFieldToRecordDecl( |
| 5990 | C, RD, C.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/false)); |
| 5991 | RD->completeDefinition(); |
| 5992 | QualType RDType = C.getRecordType(RD); |
| 5993 | unsigned Size = Data.ReductionVars.size(); |
| 5994 | llvm::APInt ArraySize(/*numBits=*/64, Size); |
| 5995 | QualType ArrayRDType = C.getConstantArrayType( |
| 5996 | RDType, ArraySize, ArrayType::Normal, /*IndexTypeQuals=*/0); |
| 5997 | // kmp_task_red_input_t .rd_input.[Size]; |
| 5998 | Address TaskRedInput = CGF.CreateMemTemp(ArrayRDType, ".rd_input."); |
| 5999 | ReductionCodeGen RCG(Data.ReductionVars, Data.ReductionCopies, |
| 6000 | Data.ReductionOps); |
| 6001 | for (unsigned Cnt = 0; Cnt < Size; ++Cnt) { |
| 6002 | // kmp_task_red_input_t &ElemLVal = .rd_input.[Cnt]; |
| 6003 | llvm::Value *Idxs[] = {llvm::ConstantInt::get(CGM.SizeTy, /*V=*/0), |
| 6004 | llvm::ConstantInt::get(CGM.SizeTy, Cnt)}; |
| 6005 | llvm::Value *GEP = CGF.EmitCheckedInBoundsGEP( |
| 6006 | TaskRedInput.getPointer(), Idxs, |
| 6007 | /*SignedIndices=*/false, /*IsSubtraction=*/false, Loc, |
| 6008 | ".rd_input.gep."); |
| 6009 | LValue ElemLVal = CGF.MakeNaturalAlignAddrLValue(GEP, RDType); |
| 6010 | // ElemLVal.reduce_shar = &Shareds[Cnt]; |
| 6011 | LValue SharedLVal = CGF.EmitLValueForField(ElemLVal, SharedFD); |
| 6012 | RCG.emitSharedLValue(CGF, Cnt); |
| 6013 | llvm::Value *CastedShared = |
| 6014 | CGF.EmitCastToVoidPtr(RCG.getSharedLValue(Cnt).getPointer()); |
| 6015 | CGF.EmitStoreOfScalar(CastedShared, SharedLVal); |
| 6016 | RCG.emitAggregateType(CGF, Cnt); |
| 6017 | llvm::Value *SizeValInChars; |
| 6018 | llvm::Value *SizeVal; |
| 6019 | std::tie(SizeValInChars, SizeVal) = RCG.getSizes(Cnt); |
| 6020 | // We use delayed creation/initialization for VLAs, array sections and |
| 6021 | // custom reduction initializations. It is required because runtime does not |
| 6022 | // provide the way to pass the sizes of VLAs/array sections to |
| 6023 | // initializer/combiner/finalizer functions and does not pass the pointer to |
| 6024 | // original reduction item to the initializer. Instead threadprivate global |
| 6025 | // variables are used to store these values and use them in the functions. |
| 6026 | bool DelayedCreation = !!SizeVal; |
| 6027 | SizeValInChars = CGF.Builder.CreateIntCast(SizeValInChars, CGM.SizeTy, |
| 6028 | /*isSigned=*/false); |
| 6029 | LValue SizeLVal = CGF.EmitLValueForField(ElemLVal, SizeFD); |
| 6030 | CGF.EmitStoreOfScalar(SizeValInChars, SizeLVal); |
| 6031 | // ElemLVal.reduce_init = init; |
| 6032 | LValue InitLVal = CGF.EmitLValueForField(ElemLVal, InitFD); |
| 6033 | llvm::Value *InitAddr = |
| 6034 | CGF.EmitCastToVoidPtr(emitReduceInitFunction(CGM, Loc, RCG, Cnt)); |
| 6035 | CGF.EmitStoreOfScalar(InitAddr, InitLVal); |
| 6036 | DelayedCreation = DelayedCreation || RCG.usesReductionInitializer(Cnt); |
| 6037 | // ElemLVal.reduce_fini = fini; |
| 6038 | LValue FiniLVal = CGF.EmitLValueForField(ElemLVal, FiniFD); |
| 6039 | llvm::Value *Fini = emitReduceFiniFunction(CGM, Loc, RCG, Cnt); |
| 6040 | llvm::Value *FiniAddr = Fini |
| 6041 | ? CGF.EmitCastToVoidPtr(Fini) |
| 6042 | : llvm::ConstantPointerNull::get(CGM.VoidPtrTy); |
| 6043 | CGF.EmitStoreOfScalar(FiniAddr, FiniLVal); |
| 6044 | // ElemLVal.reduce_comb = comb; |
| 6045 | LValue CombLVal = CGF.EmitLValueForField(ElemLVal, CombFD); |
| 6046 | llvm::Value *CombAddr = CGF.EmitCastToVoidPtr(emitReduceCombFunction( |
| 6047 | CGM, Loc, RCG, Cnt, Data.ReductionOps[Cnt], LHSExprs[Cnt], |
| 6048 | RHSExprs[Cnt], Data.ReductionCopies[Cnt])); |
| 6049 | CGF.EmitStoreOfScalar(CombAddr, CombLVal); |
| 6050 | // ElemLVal.flags = 0; |
| 6051 | LValue FlagsLVal = CGF.EmitLValueForField(ElemLVal, FlagsFD); |
| 6052 | if (DelayedCreation) { |
| 6053 | CGF.EmitStoreOfScalar( |
| 6054 | llvm::ConstantInt::get(CGM.Int32Ty, /*V=*/1, /*IsSigned=*/true), |
| 6055 | FlagsLVal); |
| 6056 | } else |
| 6057 | CGF.EmitNullInitialization(FlagsLVal.getAddress(), FlagsLVal.getType()); |
| 6058 | } |
| 6059 | // Build call void *__kmpc_task_reduction_init(int gtid, int num_data, void |
| 6060 | // *data); |
| 6061 | llvm::Value *Args[] = { |
| 6062 | CGF.Builder.CreateIntCast(getThreadID(CGF, Loc), CGM.IntTy, |
| 6063 | /*isSigned=*/true), |
| 6064 | llvm::ConstantInt::get(CGM.IntTy, Size, /*isSigned=*/true), |
| 6065 | CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(TaskRedInput.getPointer(), |
| 6066 | CGM.VoidPtrTy)}; |
| 6067 | return CGF.EmitRuntimeCall( |
| 6068 | createRuntimeFunction(OMPRTL__kmpc_task_reduction_init), Args); |
| 6069 | } |
| 6070 | |
| 6071 | void CGOpenMPRuntime::emitTaskReductionFixups(CodeGenFunction &CGF, |
| 6072 | SourceLocation Loc, |
| 6073 | ReductionCodeGen &RCG, |
| 6074 | unsigned N) { |
| 6075 | auto Sizes = RCG.getSizes(N); |
| 6076 | // Emit threadprivate global variable if the type is non-constant |
| 6077 | // (Sizes.second = nullptr). |
| 6078 | if (Sizes.second) { |
| 6079 | llvm::Value *SizeVal = CGF.Builder.CreateIntCast(Sizes.second, CGM.SizeTy, |
| 6080 | /*isSigned=*/false); |
| 6081 | Address SizeAddr = getAddrOfArtificialThreadPrivate( |
| 6082 | CGF, CGM.getContext().getSizeType(), |
Alexey Bataev | 1c44e15 | 2018-03-06 18:59:43 +0000 | [diff] [blame] | 6083 | generateUniqueName(CGM, "reduction_size", RCG.getRefExpr(N))); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 6084 | CGF.Builder.CreateStore(SizeVal, SizeAddr, /*IsVolatile=*/false); |
| 6085 | } |
| 6086 | // Store address of the original reduction item if custom initializer is used. |
| 6087 | if (RCG.usesReductionInitializer(N)) { |
| 6088 | Address SharedAddr = getAddrOfArtificialThreadPrivate( |
| 6089 | CGF, CGM.getContext().VoidPtrTy, |
Alexey Bataev | 1c44e15 | 2018-03-06 18:59:43 +0000 | [diff] [blame] | 6090 | generateUniqueName(CGM, "reduction", RCG.getRefExpr(N))); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 6091 | CGF.Builder.CreateStore( |
| 6092 | CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
| 6093 | RCG.getSharedLValue(N).getPointer(), CGM.VoidPtrTy), |
| 6094 | SharedAddr, /*IsVolatile=*/false); |
| 6095 | } |
| 6096 | } |
| 6097 | |
| 6098 | Address CGOpenMPRuntime::getTaskReductionItem(CodeGenFunction &CGF, |
| 6099 | SourceLocation Loc, |
| 6100 | llvm::Value *ReductionsPtr, |
| 6101 | LValue SharedLVal) { |
| 6102 | // Build call void *__kmpc_task_reduction_get_th_data(int gtid, void *tg, void |
| 6103 | // *d); |
| 6104 | llvm::Value *Args[] = { |
| 6105 | CGF.Builder.CreateIntCast(getThreadID(CGF, Loc), CGM.IntTy, |
| 6106 | /*isSigned=*/true), |
| 6107 | ReductionsPtr, |
| 6108 | CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(SharedLVal.getPointer(), |
| 6109 | CGM.VoidPtrTy)}; |
| 6110 | return Address( |
| 6111 | CGF.EmitRuntimeCall( |
| 6112 | createRuntimeFunction(OMPRTL__kmpc_task_reduction_get_th_data), Args), |
| 6113 | SharedLVal.getAlignment()); |
| 6114 | } |
| 6115 | |
Alexey Bataev | 8b8e202 | 2015-04-27 05:22:09 +0000 | [diff] [blame] | 6116 | void CGOpenMPRuntime::emitTaskwaitCall(CodeGenFunction &CGF, |
| 6117 | SourceLocation Loc) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 6118 | if (!CGF.HaveInsertPoint()) |
| 6119 | return; |
Alexey Bataev | 8b8e202 | 2015-04-27 05:22:09 +0000 | [diff] [blame] | 6120 | // Build call kmp_int32 __kmpc_omp_taskwait(ident_t *loc, kmp_int32 |
| 6121 | // global_tid); |
| 6122 | llvm::Value *Args[] = {emitUpdateLocation(CGF, Loc), getThreadID(CGF, Loc)}; |
| 6123 | // Ignore return result until untied tasks are supported. |
| 6124 | CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_omp_taskwait), Args); |
Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 6125 | if (auto *Region = dyn_cast_or_null<CGOpenMPRegionInfo>(CGF.CapturedStmtInfo)) |
| 6126 | Region->emitUntiedSwitch(CGF); |
Alexey Bataev | 8b8e202 | 2015-04-27 05:22:09 +0000 | [diff] [blame] | 6127 | } |
| 6128 | |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 6129 | void CGOpenMPRuntime::emitInlinedDirective(CodeGenFunction &CGF, |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 6130 | OpenMPDirectiveKind InnerKind, |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 6131 | const RegionCodeGenTy &CodeGen, |
| 6132 | bool HasCancel) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 6133 | if (!CGF.HaveInsertPoint()) |
| 6134 | return; |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 6135 | InlinedOpenMPRegionRAII Region(CGF, CodeGen, InnerKind, HasCancel); |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 6136 | CGF.CapturedStmtInfo->EmitBody(CGF, /*S=*/nullptr); |
Alexey Bataev | 8cbe0a6 | 2015-02-26 10:27:34 +0000 | [diff] [blame] | 6137 | } |
| 6138 | |
Alexey Bataev | 7d5d33e | 2015-07-06 05:50:32 +0000 | [diff] [blame] | 6139 | namespace { |
| 6140 | enum RTCancelKind { |
| 6141 | CancelNoreq = 0, |
| 6142 | CancelParallel = 1, |
| 6143 | CancelLoop = 2, |
| 6144 | CancelSections = 3, |
| 6145 | CancelTaskgroup = 4 |
| 6146 | }; |
Eugene Zelenko | 0a4f3f4 | 2016-02-10 19:11:58 +0000 | [diff] [blame] | 6147 | } // anonymous namespace |
Alexey Bataev | 7d5d33e | 2015-07-06 05:50:32 +0000 | [diff] [blame] | 6148 | |
| 6149 | static RTCancelKind getCancellationKind(OpenMPDirectiveKind CancelRegion) { |
| 6150 | RTCancelKind CancelKind = CancelNoreq; |
Alexey Bataev | 0f34da1 | 2015-07-02 04:17:07 +0000 | [diff] [blame] | 6151 | if (CancelRegion == OMPD_parallel) |
| 6152 | CancelKind = CancelParallel; |
| 6153 | else if (CancelRegion == OMPD_for) |
| 6154 | CancelKind = CancelLoop; |
| 6155 | else if (CancelRegion == OMPD_sections) |
| 6156 | CancelKind = CancelSections; |
| 6157 | else { |
| 6158 | assert(CancelRegion == OMPD_taskgroup); |
| 6159 | CancelKind = CancelTaskgroup; |
| 6160 | } |
Alexey Bataev | 7d5d33e | 2015-07-06 05:50:32 +0000 | [diff] [blame] | 6161 | return CancelKind; |
| 6162 | } |
| 6163 | |
| 6164 | void CGOpenMPRuntime::emitCancellationPointCall( |
| 6165 | CodeGenFunction &CGF, SourceLocation Loc, |
| 6166 | OpenMPDirectiveKind CancelRegion) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 6167 | if (!CGF.HaveInsertPoint()) |
| 6168 | return; |
Alexey Bataev | 7d5d33e | 2015-07-06 05:50:32 +0000 | [diff] [blame] | 6169 | // Build call kmp_int32 __kmpc_cancellationpoint(ident_t *loc, kmp_int32 |
| 6170 | // global_tid, kmp_int32 cncl_kind); |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 6171 | if (auto *OMPRegionInfo = |
| 6172 | dyn_cast_or_null<CGOpenMPRegionInfo>(CGF.CapturedStmtInfo)) { |
Jonas Hahnfeld | b07931f | 2017-02-17 18:32:58 +0000 | [diff] [blame] | 6173 | // For 'cancellation point taskgroup', the task region info may not have a |
| 6174 | // cancel. This may instead happen in another adjacent task. |
| 6175 | if (CancelRegion == OMPD_taskgroup || OMPRegionInfo->hasCancel()) { |
Alexey Bataev | 7d5d33e | 2015-07-06 05:50:32 +0000 | [diff] [blame] | 6176 | llvm::Value *Args[] = { |
| 6177 | emitUpdateLocation(CGF, Loc), getThreadID(CGF, Loc), |
| 6178 | CGF.Builder.getInt32(getCancellationKind(CancelRegion))}; |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 6179 | // Ignore return result until untied tasks are supported. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6180 | llvm::Value *Result = CGF.EmitRuntimeCall( |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 6181 | createRuntimeFunction(OMPRTL__kmpc_cancellationpoint), Args); |
| 6182 | // if (__kmpc_cancellationpoint()) { |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 6183 | // exit from construct; |
| 6184 | // } |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6185 | llvm::BasicBlock *ExitBB = CGF.createBasicBlock(".cancel.exit"); |
| 6186 | llvm::BasicBlock *ContBB = CGF.createBasicBlock(".cancel.continue"); |
| 6187 | llvm::Value *Cmp = CGF.Builder.CreateIsNotNull(Result); |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 6188 | CGF.Builder.CreateCondBr(Cmp, ExitBB, ContBB); |
| 6189 | CGF.EmitBlock(ExitBB); |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 6190 | // exit from construct; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6191 | CodeGenFunction::JumpDest CancelDest = |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 6192 | CGF.getOMPCancelDestination(OMPRegionInfo->getDirectiveKind()); |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 6193 | CGF.EmitBranchThroughCleanup(CancelDest); |
| 6194 | CGF.EmitBlock(ContBB, /*IsFinished=*/true); |
| 6195 | } |
Alexey Bataev | 0f34da1 | 2015-07-02 04:17:07 +0000 | [diff] [blame] | 6196 | } |
Alexey Bataev | 0f34da1 | 2015-07-02 04:17:07 +0000 | [diff] [blame] | 6197 | } |
| 6198 | |
Alexey Bataev | 7d5d33e | 2015-07-06 05:50:32 +0000 | [diff] [blame] | 6199 | void CGOpenMPRuntime::emitCancelCall(CodeGenFunction &CGF, SourceLocation Loc, |
Alexey Bataev | 87933c7 | 2015-09-18 08:07:34 +0000 | [diff] [blame] | 6200 | const Expr *IfCond, |
Alexey Bataev | 7d5d33e | 2015-07-06 05:50:32 +0000 | [diff] [blame] | 6201 | OpenMPDirectiveKind CancelRegion) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 6202 | if (!CGF.HaveInsertPoint()) |
| 6203 | return; |
Alexey Bataev | 7d5d33e | 2015-07-06 05:50:32 +0000 | [diff] [blame] | 6204 | // Build call kmp_int32 __kmpc_cancel(ident_t *loc, kmp_int32 global_tid, |
| 6205 | // kmp_int32 cncl_kind); |
| 6206 | if (auto *OMPRegionInfo = |
| 6207 | dyn_cast_or_null<CGOpenMPRegionInfo>(CGF.CapturedStmtInfo)) { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 6208 | auto &&ThenGen = [Loc, CancelRegion, OMPRegionInfo](CodeGenFunction &CGF, |
| 6209 | PrePostActionTy &) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6210 | CGOpenMPRuntime &RT = CGF.CGM.getOpenMPRuntime(); |
Alexey Bataev | 87933c7 | 2015-09-18 08:07:34 +0000 | [diff] [blame] | 6211 | llvm::Value *Args[] = { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 6212 | RT.emitUpdateLocation(CGF, Loc), RT.getThreadID(CGF, Loc), |
Alexey Bataev | 87933c7 | 2015-09-18 08:07:34 +0000 | [diff] [blame] | 6213 | CGF.Builder.getInt32(getCancellationKind(CancelRegion))}; |
| 6214 | // Ignore return result until untied tasks are supported. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6215 | llvm::Value *Result = CGF.EmitRuntimeCall( |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 6216 | RT.createRuntimeFunction(OMPRTL__kmpc_cancel), Args); |
Alexey Bataev | 87933c7 | 2015-09-18 08:07:34 +0000 | [diff] [blame] | 6217 | // if (__kmpc_cancel()) { |
Alexey Bataev | 87933c7 | 2015-09-18 08:07:34 +0000 | [diff] [blame] | 6218 | // exit from construct; |
| 6219 | // } |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6220 | llvm::BasicBlock *ExitBB = CGF.createBasicBlock(".cancel.exit"); |
| 6221 | llvm::BasicBlock *ContBB = CGF.createBasicBlock(".cancel.continue"); |
| 6222 | llvm::Value *Cmp = CGF.Builder.CreateIsNotNull(Result); |
Alexey Bataev | 87933c7 | 2015-09-18 08:07:34 +0000 | [diff] [blame] | 6223 | CGF.Builder.CreateCondBr(Cmp, ExitBB, ContBB); |
| 6224 | CGF.EmitBlock(ExitBB); |
Alexey Bataev | 87933c7 | 2015-09-18 08:07:34 +0000 | [diff] [blame] | 6225 | // exit from construct; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6226 | CodeGenFunction::JumpDest CancelDest = |
Alexey Bataev | 87933c7 | 2015-09-18 08:07:34 +0000 | [diff] [blame] | 6227 | CGF.getOMPCancelDestination(OMPRegionInfo->getDirectiveKind()); |
| 6228 | CGF.EmitBranchThroughCleanup(CancelDest); |
| 6229 | CGF.EmitBlock(ContBB, /*IsFinished=*/true); |
| 6230 | }; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6231 | if (IfCond) { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 6232 | emitOMPIfClause(CGF, IfCond, ThenGen, |
| 6233 | [](CodeGenFunction &, PrePostActionTy &) {}); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6234 | } else { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 6235 | RegionCodeGenTy ThenRCG(ThenGen); |
| 6236 | ThenRCG(CGF); |
| 6237 | } |
Alexey Bataev | 7d5d33e | 2015-07-06 05:50:32 +0000 | [diff] [blame] | 6238 | } |
| 6239 | } |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 6240 | |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 6241 | void CGOpenMPRuntime::emitTargetOutlinedFunction( |
| 6242 | const OMPExecutableDirective &D, StringRef ParentName, |
| 6243 | llvm::Function *&OutlinedFn, llvm::Constant *&OutlinedFnID, |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 6244 | bool IsOffloadEntry, const RegionCodeGenTy &CodeGen) { |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 6245 | assert(!ParentName.empty() && "Invalid target region parent name!"); |
Arpith Chacko Jacob | 5c309e4 | 2016-03-22 01:48:56 +0000 | [diff] [blame] | 6246 | emitTargetOutlinedFunctionHelper(D, ParentName, OutlinedFn, OutlinedFnID, |
| 6247 | IsOffloadEntry, CodeGen); |
| 6248 | } |
| 6249 | |
| 6250 | void CGOpenMPRuntime::emitTargetOutlinedFunctionHelper( |
| 6251 | const OMPExecutableDirective &D, StringRef ParentName, |
| 6252 | llvm::Function *&OutlinedFn, llvm::Constant *&OutlinedFnID, |
| 6253 | bool IsOffloadEntry, const RegionCodeGenTy &CodeGen) { |
Samuel Antao | 2de62b0 | 2016-02-13 23:35:10 +0000 | [diff] [blame] | 6254 | // Create a unique name for the entry function using the source location |
| 6255 | // information of the current target region. The name will be something like: |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 6256 | // |
Samuel Antao | 2de62b0 | 2016-02-13 23:35:10 +0000 | [diff] [blame] | 6257 | // __omp_offloading_DD_FFFF_PP_lBB |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 6258 | // |
| 6259 | // 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] | 6260 | // mangled name of the function that encloses the target region and BB is the |
| 6261 | // line number of the target region. |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 6262 | |
| 6263 | unsigned DeviceID; |
| 6264 | unsigned FileID; |
| 6265 | unsigned Line; |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 6266 | getTargetEntryUniqueInfo(CGM.getContext(), D.getBeginLoc(), DeviceID, FileID, |
Samuel Antao | 2de62b0 | 2016-02-13 23:35:10 +0000 | [diff] [blame] | 6267 | Line); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 6268 | SmallString<64> EntryFnName; |
| 6269 | { |
| 6270 | llvm::raw_svector_ostream OS(EntryFnName); |
Samuel Antao | 2de62b0 | 2016-02-13 23:35:10 +0000 | [diff] [blame] | 6271 | OS << "__omp_offloading" << llvm::format("_%x", DeviceID) |
| 6272 | << llvm::format("_%x_", FileID) << ParentName << "_l" << Line; |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 6273 | } |
| 6274 | |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 6275 | const CapturedStmt &CS = *D.getCapturedStmt(OMPD_target); |
Arpith Chacko Jacob | 5c309e4 | 2016-03-22 01:48:56 +0000 | [diff] [blame] | 6276 | |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 6277 | CodeGenFunction CGF(CGM, true); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 6278 | CGOpenMPTargetRegionInfo CGInfo(CS, CodeGen, EntryFnName); |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 6279 | CodeGenFunction::CGCapturedStmtRAII CapInfoRAII(CGF, &CGInfo); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 6280 | |
Samuel Antao | 6d00426 | 2016-06-16 18:39:34 +0000 | [diff] [blame] | 6281 | OutlinedFn = CGF.GenerateOpenMPCapturedStmtFunction(CS); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 6282 | |
| 6283 | // If this target outline function is not an offload entry, we don't need to |
| 6284 | // register it. |
| 6285 | if (!IsOffloadEntry) |
| 6286 | return; |
| 6287 | |
| 6288 | // The target region ID is used by the runtime library to identify the current |
| 6289 | // target region, so it only has to be unique and not necessarily point to |
| 6290 | // anything. It could be the pointer to the outlined function that implements |
| 6291 | // the target region, but we aren't using that so that the compiler doesn't |
| 6292 | // need to keep that, and could therefore inline the host function if proven |
| 6293 | // worthwhile during optimization. In the other hand, if emitting code for the |
| 6294 | // device, the ID has to be the function address so that it can retrieved from |
| 6295 | // the offloading entry and launched by the runtime library. We also mark the |
| 6296 | // outlined function to have external linkage in case we are emitting code for |
| 6297 | // the device, because these functions will be entry points to the device. |
| 6298 | |
| 6299 | if (CGM.getLangOpts().OpenMPIsDevice) { |
| 6300 | OutlinedFnID = llvm::ConstantExpr::getBitCast(OutlinedFn, CGM.Int8PtrTy); |
Alexey Bataev | 9a70017 | 2018-05-08 14:16:57 +0000 | [diff] [blame] | 6301 | OutlinedFn->setLinkage(llvm::GlobalValue::WeakAnyLinkage); |
Rafael Espindola | cbca487 | 2018-01-11 22:15:12 +0000 | [diff] [blame] | 6302 | OutlinedFn->setDSOLocal(false); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6303 | } else { |
Alexey Bataev | c15ea70 | 2018-05-09 18:02:37 +0000 | [diff] [blame] | 6304 | std::string Name = getName({EntryFnName, "region_id"}); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 6305 | OutlinedFnID = new llvm::GlobalVariable( |
| 6306 | CGM.getModule(), CGM.Int8Ty, /*isConstant=*/true, |
Alexey Bataev | 9a70017 | 2018-05-08 14:16:57 +0000 | [diff] [blame] | 6307 | llvm::GlobalValue::WeakAnyLinkage, |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 6308 | llvm::Constant::getNullValue(CGM.Int8Ty), Name); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6309 | } |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 6310 | |
| 6311 | // Register the information for the entry associated with this target region. |
| 6312 | OffloadEntriesInfoManager.registerTargetRegionEntryInfo( |
Samuel Antao | f83efdb | 2017-01-05 16:02:49 +0000 | [diff] [blame] | 6313 | DeviceID, FileID, ParentName, Line, OutlinedFn, OutlinedFnID, |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 6314 | OffloadEntriesInfoManagerTy::OMPTargetRegionEntryTargetRegion); |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 6315 | } |
| 6316 | |
Carlo Bertolli | 6eee906 | 2016-04-29 01:37:30 +0000 | [diff] [blame] | 6317 | /// discard all CompoundStmts intervening between two constructs |
| 6318 | static const Stmt *ignoreCompoundStmts(const Stmt *Body) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6319 | while (const auto *CS = dyn_cast_or_null<CompoundStmt>(Body)) |
Carlo Bertolli | 6eee906 | 2016-04-29 01:37:30 +0000 | [diff] [blame] | 6320 | Body = CS->body_front(); |
| 6321 | |
| 6322 | return Body; |
| 6323 | } |
| 6324 | |
Arpith Chacko Jacob | 33c849a | 2017-01-25 00:57:16 +0000 | [diff] [blame] | 6325 | /// Emit the number of teams for a target directive. Inspect the num_teams |
| 6326 | /// clause associated with a teams construct combined or closely nested |
| 6327 | /// with the target directive. |
| 6328 | /// |
| 6329 | /// Emit a team of size one for directives such as 'target parallel' that |
| 6330 | /// have no associated teams construct. |
| 6331 | /// |
| 6332 | /// Otherwise, return nullptr. |
Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 6333 | static llvm::Value * |
Arpith Chacko Jacob | 33c849a | 2017-01-25 00:57:16 +0000 | [diff] [blame] | 6334 | emitNumTeamsForTargetDirective(CGOpenMPRuntime &OMPRuntime, |
| 6335 | CodeGenFunction &CGF, |
| 6336 | const OMPExecutableDirective &D) { |
Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 6337 | assert(!CGF.getLangOpts().OpenMPIsDevice && "Clauses associated with the " |
| 6338 | "teams directive expected to be " |
| 6339 | "emitted only for the host!"); |
| 6340 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6341 | CGBuilderTy &Bld = CGF.Builder; |
Arpith Chacko Jacob | 99a1e0e | 2017-01-25 02:18:43 +0000 | [diff] [blame] | 6342 | |
| 6343 | // If the target directive is combined with a teams directive: |
| 6344 | // Return the value in the num_teams clause, if any. |
| 6345 | // Otherwise, return 0 to denote the runtime default. |
| 6346 | if (isOpenMPTeamsDirective(D.getDirectiveKind())) { |
| 6347 | if (const auto *NumTeamsClause = D.getSingleClause<OMPNumTeamsClause>()) { |
| 6348 | CodeGenFunction::RunCleanupsScope NumTeamsScope(CGF); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6349 | llvm::Value *NumTeams = CGF.EmitScalarExpr(NumTeamsClause->getNumTeams(), |
| 6350 | /*IgnoreResultAssign*/ true); |
Arpith Chacko Jacob | 99a1e0e | 2017-01-25 02:18:43 +0000 | [diff] [blame] | 6351 | return Bld.CreateIntCast(NumTeams, CGF.Int32Ty, |
| 6352 | /*IsSigned=*/true); |
| 6353 | } |
| 6354 | |
| 6355 | // The default value is 0. |
| 6356 | return Bld.getInt32(0); |
| 6357 | } |
| 6358 | |
Arpith Chacko Jacob | 33c849a | 2017-01-25 00:57:16 +0000 | [diff] [blame] | 6359 | // If the target directive is combined with a parallel directive but not a |
| 6360 | // teams directive, start one team. |
Arpith Chacko Jacob | 99a1e0e | 2017-01-25 02:18:43 +0000 | [diff] [blame] | 6361 | if (isOpenMPParallelDirective(D.getDirectiveKind())) |
| 6362 | return Bld.getInt32(1); |
Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 6363 | |
| 6364 | // If the current target region has a teams region enclosed, we need to get |
| 6365 | // the number of teams to pass to the runtime function call. This is done |
| 6366 | // by generating the expression in a inlined region. This is required because |
| 6367 | // the expression is captured in the enclosing target environment when the |
| 6368 | // teams directive is not combined with target. |
| 6369 | |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 6370 | const CapturedStmt &CS = *D.getCapturedStmt(OMPD_target); |
Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 6371 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6372 | if (const auto *TeamsDir = dyn_cast_or_null<OMPExecutableDirective>( |
Carlo Bertolli | 6eee906 | 2016-04-29 01:37:30 +0000 | [diff] [blame] | 6373 | ignoreCompoundStmts(CS.getCapturedStmt()))) { |
Alexey Bataev | 50a1c78 | 2017-12-01 21:31:08 +0000 | [diff] [blame] | 6374 | if (isOpenMPTeamsDirective(TeamsDir->getDirectiveKind())) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6375 | if (const auto *NTE = TeamsDir->getSingleClause<OMPNumTeamsClause>()) { |
Alexey Bataev | 50a1c78 | 2017-12-01 21:31:08 +0000 | [diff] [blame] | 6376 | CGOpenMPInnerExprInfo CGInfo(CGF, CS); |
| 6377 | CodeGenFunction::CGCapturedStmtRAII CapInfoRAII(CGF, &CGInfo); |
| 6378 | llvm::Value *NumTeams = CGF.EmitScalarExpr(NTE->getNumTeams()); |
| 6379 | return Bld.CreateIntCast(NumTeams, CGF.Int32Ty, |
| 6380 | /*IsSigned=*/true); |
| 6381 | } |
Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 6382 | |
Alexey Bataev | 50a1c78 | 2017-12-01 21:31:08 +0000 | [diff] [blame] | 6383 | // If we have an enclosed teams directive but no num_teams clause we use |
| 6384 | // the default value 0. |
| 6385 | return Bld.getInt32(0); |
| 6386 | } |
Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 6387 | } |
| 6388 | |
| 6389 | // No teams associated with the directive. |
| 6390 | return nullptr; |
| 6391 | } |
| 6392 | |
Arpith Chacko Jacob | 33c849a | 2017-01-25 00:57:16 +0000 | [diff] [blame] | 6393 | /// Emit the number of threads for a target directive. Inspect the |
| 6394 | /// thread_limit clause associated with a teams construct combined or closely |
| 6395 | /// nested with the target directive. |
| 6396 | /// |
| 6397 | /// Emit the num_threads clause for directives such as 'target parallel' that |
| 6398 | /// have no associated teams construct. |
| 6399 | /// |
| 6400 | /// Otherwise, return nullptr. |
Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 6401 | static llvm::Value * |
Arpith Chacko Jacob | 33c849a | 2017-01-25 00:57:16 +0000 | [diff] [blame] | 6402 | emitNumThreadsForTargetDirective(CGOpenMPRuntime &OMPRuntime, |
| 6403 | CodeGenFunction &CGF, |
| 6404 | const OMPExecutableDirective &D) { |
Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 6405 | assert(!CGF.getLangOpts().OpenMPIsDevice && "Clauses associated with the " |
| 6406 | "teams directive expected to be " |
| 6407 | "emitted only for the host!"); |
| 6408 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6409 | CGBuilderTy &Bld = CGF.Builder; |
Arpith Chacko Jacob | 33c849a | 2017-01-25 00:57:16 +0000 | [diff] [blame] | 6410 | |
| 6411 | // |
| 6412 | // If the target directive is combined with a teams directive: |
| 6413 | // Return the value in the thread_limit clause, if any. |
| 6414 | // |
| 6415 | // If the target directive is combined with a parallel directive: |
| 6416 | // Return the value in the num_threads clause, if any. |
| 6417 | // |
| 6418 | // If both clauses are set, select the minimum of the two. |
| 6419 | // |
| 6420 | // If neither teams or parallel combined directives set the number of threads |
| 6421 | // in a team, return 0 to denote the runtime default. |
| 6422 | // |
| 6423 | // If this is not a teams directive return nullptr. |
| 6424 | |
Arpith Chacko Jacob | 99a1e0e | 2017-01-25 02:18:43 +0000 | [diff] [blame] | 6425 | if (isOpenMPTeamsDirective(D.getDirectiveKind()) || |
| 6426 | isOpenMPParallelDirective(D.getDirectiveKind())) { |
Arpith Chacko Jacob | 33c849a | 2017-01-25 00:57:16 +0000 | [diff] [blame] | 6427 | llvm::Value *DefaultThreadLimitVal = Bld.getInt32(0); |
| 6428 | llvm::Value *NumThreadsVal = nullptr; |
Arpith Chacko Jacob | 99a1e0e | 2017-01-25 02:18:43 +0000 | [diff] [blame] | 6429 | llvm::Value *ThreadLimitVal = nullptr; |
| 6430 | |
| 6431 | if (const auto *ThreadLimitClause = |
| 6432 | D.getSingleClause<OMPThreadLimitClause>()) { |
| 6433 | CodeGenFunction::RunCleanupsScope ThreadLimitScope(CGF); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6434 | llvm::Value *ThreadLimit = |
| 6435 | CGF.EmitScalarExpr(ThreadLimitClause->getThreadLimit(), |
| 6436 | /*IgnoreResultAssign*/ true); |
Arpith Chacko Jacob | 99a1e0e | 2017-01-25 02:18:43 +0000 | [diff] [blame] | 6437 | ThreadLimitVal = Bld.CreateIntCast(ThreadLimit, CGF.Int32Ty, |
| 6438 | /*IsSigned=*/true); |
| 6439 | } |
Arpith Chacko Jacob | 33c849a | 2017-01-25 00:57:16 +0000 | [diff] [blame] | 6440 | |
| 6441 | if (const auto *NumThreadsClause = |
| 6442 | D.getSingleClause<OMPNumThreadsClause>()) { |
| 6443 | CodeGenFunction::RunCleanupsScope NumThreadsScope(CGF); |
| 6444 | llvm::Value *NumThreads = |
| 6445 | CGF.EmitScalarExpr(NumThreadsClause->getNumThreads(), |
| 6446 | /*IgnoreResultAssign*/ true); |
| 6447 | NumThreadsVal = |
| 6448 | Bld.CreateIntCast(NumThreads, CGF.Int32Ty, /*IsSigned=*/true); |
| 6449 | } |
| 6450 | |
Arpith Chacko Jacob | 99a1e0e | 2017-01-25 02:18:43 +0000 | [diff] [blame] | 6451 | // Select the lesser of thread_limit and num_threads. |
| 6452 | if (NumThreadsVal) |
| 6453 | ThreadLimitVal = ThreadLimitVal |
| 6454 | ? Bld.CreateSelect(Bld.CreateICmpSLT(NumThreadsVal, |
| 6455 | ThreadLimitVal), |
| 6456 | NumThreadsVal, ThreadLimitVal) |
| 6457 | : NumThreadsVal; |
Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 6458 | |
Arpith Chacko Jacob | 99a1e0e | 2017-01-25 02:18:43 +0000 | [diff] [blame] | 6459 | // Set default value passed to the runtime if either teams or a target |
| 6460 | // parallel type directive is found but no clause is specified. |
| 6461 | if (!ThreadLimitVal) |
| 6462 | ThreadLimitVal = DefaultThreadLimitVal; |
| 6463 | |
| 6464 | return ThreadLimitVal; |
| 6465 | } |
Arpith Chacko Jacob | 86f9e46 | 2017-01-25 01:45:59 +0000 | [diff] [blame] | 6466 | |
Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 6467 | // If the current target region has a teams region enclosed, we need to get |
| 6468 | // the thread limit to pass to the runtime function call. This is done |
| 6469 | // by generating the expression in a inlined region. This is required because |
| 6470 | // the expression is captured in the enclosing target environment when the |
| 6471 | // teams directive is not combined with target. |
| 6472 | |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 6473 | const CapturedStmt &CS = *D.getCapturedStmt(OMPD_target); |
Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 6474 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6475 | if (const auto *TeamsDir = dyn_cast_or_null<OMPExecutableDirective>( |
Carlo Bertolli | 6eee906 | 2016-04-29 01:37:30 +0000 | [diff] [blame] | 6476 | ignoreCompoundStmts(CS.getCapturedStmt()))) { |
Alexey Bataev | 50a1c78 | 2017-12-01 21:31:08 +0000 | [diff] [blame] | 6477 | if (isOpenMPTeamsDirective(TeamsDir->getDirectiveKind())) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6478 | if (const auto *TLE = TeamsDir->getSingleClause<OMPThreadLimitClause>()) { |
Alexey Bataev | 50a1c78 | 2017-12-01 21:31:08 +0000 | [diff] [blame] | 6479 | CGOpenMPInnerExprInfo CGInfo(CGF, CS); |
| 6480 | CodeGenFunction::CGCapturedStmtRAII CapInfoRAII(CGF, &CGInfo); |
| 6481 | llvm::Value *ThreadLimit = CGF.EmitScalarExpr(TLE->getThreadLimit()); |
| 6482 | return CGF.Builder.CreateIntCast(ThreadLimit, CGF.Int32Ty, |
| 6483 | /*IsSigned=*/true); |
| 6484 | } |
Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 6485 | |
Alexey Bataev | 50a1c78 | 2017-12-01 21:31:08 +0000 | [diff] [blame] | 6486 | // If we have an enclosed teams directive but no thread_limit clause we |
| 6487 | // use the default value 0. |
| 6488 | return CGF.Builder.getInt32(0); |
| 6489 | } |
Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 6490 | } |
| 6491 | |
| 6492 | // No teams associated with the directive. |
| 6493 | return nullptr; |
| 6494 | } |
| 6495 | |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6496 | namespace { |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 6497 | LLVM_ENABLE_BITMASK_ENUMS_IN_NAMESPACE(); |
| 6498 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 6499 | // Utility to handle information from clauses associated with a given |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6500 | // construct that use mappable expressions (e.g. 'map' clause, 'to' clause). |
| 6501 | // It provides a convenient interface to obtain the information and generate |
| 6502 | // code for that information. |
| 6503 | class MappableExprsHandler { |
| 6504 | public: |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 6505 | /// Values for bit flags used to specify the mapping type for |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6506 | /// offloading. |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 6507 | enum OpenMPOffloadMappingFlags : uint64_t { |
| 6508 | /// No flags |
| 6509 | OMP_MAP_NONE = 0x0, |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 6510 | /// Allocate memory on the device and move data from host to device. |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6511 | OMP_MAP_TO = 0x01, |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 6512 | /// Allocate memory on the device and move data from device to host. |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6513 | OMP_MAP_FROM = 0x02, |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 6514 | /// Always perform the requested mapping action on the element, even |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6515 | /// if it was already mapped before. |
| 6516 | OMP_MAP_ALWAYS = 0x04, |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 6517 | /// Delete the element from the device environment, ignoring the |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6518 | /// current reference count associated with the element. |
Samuel Antao | 6782e94 | 2016-05-26 16:48:10 +0000 | [diff] [blame] | 6519 | OMP_MAP_DELETE = 0x08, |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 6520 | /// The element being mapped is a pointer-pointee pair; both the |
George Rokos | 065755d | 2017-11-07 18:27:04 +0000 | [diff] [blame] | 6521 | /// pointer and the pointee should be mapped. |
| 6522 | OMP_MAP_PTR_AND_OBJ = 0x10, |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 6523 | /// This flags signals that the base address of an entry should be |
George Rokos | 065755d | 2017-11-07 18:27:04 +0000 | [diff] [blame] | 6524 | /// passed to the target kernel as an argument. |
| 6525 | OMP_MAP_TARGET_PARAM = 0x20, |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 6526 | /// Signal that the runtime library has to return the device pointer |
George Rokos | 065755d | 2017-11-07 18:27:04 +0000 | [diff] [blame] | 6527 | /// in the current position for the data being mapped. Used when we have the |
| 6528 | /// use_device_ptr clause. |
| 6529 | OMP_MAP_RETURN_PARAM = 0x40, |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 6530 | /// This flag signals that the reference being passed is a pointer to |
Samuel Antao | d486f84 | 2016-05-26 16:53:38 +0000 | [diff] [blame] | 6531 | /// private data. |
George Rokos | 065755d | 2017-11-07 18:27:04 +0000 | [diff] [blame] | 6532 | OMP_MAP_PRIVATE = 0x80, |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 6533 | /// Pass the element to the device by value. |
George Rokos | 065755d | 2017-11-07 18:27:04 +0000 | [diff] [blame] | 6534 | OMP_MAP_LITERAL = 0x100, |
Alexey Bataev | f47c4b4 | 2017-09-26 13:47:31 +0000 | [diff] [blame] | 6535 | /// Implicit map |
| 6536 | OMP_MAP_IMPLICIT = 0x200, |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 6537 | /// The 16 MSBs of the flags indicate whether the entry is member of some |
| 6538 | /// struct/class. |
| 6539 | OMP_MAP_MEMBER_OF = 0xffff000000000000, |
| 6540 | LLVM_MARK_AS_BITMASK_ENUM(/* LargestFlag = */ OMP_MAP_MEMBER_OF), |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6541 | }; |
| 6542 | |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 6543 | /// Class that associates information with a base pointer to be passed to the |
| 6544 | /// runtime library. |
| 6545 | class BasePointerInfo { |
| 6546 | /// The base pointer. |
| 6547 | llvm::Value *Ptr = nullptr; |
| 6548 | /// The base declaration that refers to this device pointer, or null if |
| 6549 | /// there is none. |
| 6550 | const ValueDecl *DevPtrDecl = nullptr; |
| 6551 | |
| 6552 | public: |
| 6553 | BasePointerInfo(llvm::Value *Ptr, const ValueDecl *DevPtrDecl = nullptr) |
| 6554 | : Ptr(Ptr), DevPtrDecl(DevPtrDecl) {} |
| 6555 | llvm::Value *operator*() const { return Ptr; } |
| 6556 | const ValueDecl *getDevicePtrDecl() const { return DevPtrDecl; } |
| 6557 | void setDevicePtrDecl(const ValueDecl *D) { DevPtrDecl = D; } |
| 6558 | }; |
| 6559 | |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 6560 | using MapBaseValuesArrayTy = SmallVector<BasePointerInfo, 4>; |
| 6561 | using MapValuesArrayTy = SmallVector<llvm::Value *, 4>; |
| 6562 | using MapFlagsArrayTy = SmallVector<OpenMPOffloadMappingFlags, 4>; |
| 6563 | |
| 6564 | /// Map between a struct and the its lowest & highest elements which have been |
| 6565 | /// mapped. |
| 6566 | /// [ValueDecl *] --> {LE(FieldIndex, Pointer), |
| 6567 | /// HE(FieldIndex, Pointer)} |
| 6568 | struct StructRangeInfoTy { |
| 6569 | std::pair<unsigned /*FieldIndex*/, Address /*Pointer*/> LowestElem = { |
| 6570 | 0, Address::invalid()}; |
| 6571 | std::pair<unsigned /*FieldIndex*/, Address /*Pointer*/> HighestElem = { |
| 6572 | 0, Address::invalid()}; |
| 6573 | Address Base = Address::invalid(); |
| 6574 | }; |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6575 | |
| 6576 | private: |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 6577 | /// Kind that defines how a device pointer has to be returned. |
| 6578 | struct MapInfo { |
| 6579 | OMPClauseMappableExprCommon::MappableExprComponentListRef Components; |
| 6580 | OpenMPMapClauseKind MapType = OMPC_MAP_unknown; |
| 6581 | OpenMPMapClauseKind MapTypeModifier = OMPC_MAP_unknown; |
| 6582 | bool ReturnDevicePointer = false; |
| 6583 | bool IsImplicit = false; |
| 6584 | |
| 6585 | MapInfo() = default; |
| 6586 | MapInfo( |
| 6587 | OMPClauseMappableExprCommon::MappableExprComponentListRef Components, |
| 6588 | OpenMPMapClauseKind MapType, OpenMPMapClauseKind MapTypeModifier, |
| 6589 | bool ReturnDevicePointer, bool IsImplicit) |
| 6590 | : Components(Components), MapType(MapType), |
| 6591 | MapTypeModifier(MapTypeModifier), |
| 6592 | ReturnDevicePointer(ReturnDevicePointer), IsImplicit(IsImplicit) {} |
| 6593 | }; |
| 6594 | |
| 6595 | /// If use_device_ptr is used on a pointer which is a struct member and there |
| 6596 | /// is no map information about it, then emission of that entry is deferred |
| 6597 | /// until the whole struct has been processed. |
| 6598 | struct DeferredDevicePtrEntryTy { |
| 6599 | const Expr *IE = nullptr; |
| 6600 | const ValueDecl *VD = nullptr; |
| 6601 | |
| 6602 | DeferredDevicePtrEntryTy(const Expr *IE, const ValueDecl *VD) |
| 6603 | : IE(IE), VD(VD) {} |
| 6604 | }; |
| 6605 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 6606 | /// Directive from where the map clauses were extracted. |
Samuel Antao | 44bcdb3 | 2016-07-28 15:31:29 +0000 | [diff] [blame] | 6607 | const OMPExecutableDirective &CurDir; |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6608 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 6609 | /// Function the directive is being generated for. |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6610 | CodeGenFunction &CGF; |
| 6611 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 6612 | /// Set of all first private variables in the current directive. |
Samuel Antao | d486f84 | 2016-05-26 16:53:38 +0000 | [diff] [blame] | 6613 | llvm::SmallPtrSet<const VarDecl *, 8> FirstPrivateDecls; |
| 6614 | |
Samuel Antao | 6890b09 | 2016-07-28 14:25:09 +0000 | [diff] [blame] | 6615 | /// Map between device pointer declarations and their expression components. |
| 6616 | /// The key value for declarations in 'this' is null. |
| 6617 | llvm::DenseMap< |
| 6618 | const ValueDecl *, |
| 6619 | SmallVector<OMPClauseMappableExprCommon::MappableExprComponentListRef, 4>> |
| 6620 | DevPointersMap; |
| 6621 | |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6622 | llvm::Value *getExprTypeSize(const Expr *E) const { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6623 | QualType ExprTy = E->getType().getCanonicalType(); |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6624 | |
| 6625 | // Reference types are ignored for mapping purposes. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6626 | if (const auto *RefTy = ExprTy->getAs<ReferenceType>()) |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6627 | ExprTy = RefTy->getPointeeType().getCanonicalType(); |
| 6628 | |
| 6629 | // Given that an array section is considered a built-in type, we need to |
| 6630 | // do the calculation based on the length of the section instead of relying |
| 6631 | // on CGF.getTypeSize(E->getType()). |
| 6632 | if (const auto *OAE = dyn_cast<OMPArraySectionExpr>(E)) { |
| 6633 | QualType BaseTy = OMPArraySectionExpr::getBaseOriginalType( |
| 6634 | OAE->getBase()->IgnoreParenImpCasts()) |
| 6635 | .getCanonicalType(); |
| 6636 | |
| 6637 | // If there is no length associated with the expression, that means we |
| 6638 | // are using the whole length of the base. |
| 6639 | if (!OAE->getLength() && OAE->getColonLoc().isValid()) |
| 6640 | return CGF.getTypeSize(BaseTy); |
| 6641 | |
| 6642 | llvm::Value *ElemSize; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6643 | if (const auto *PTy = BaseTy->getAs<PointerType>()) { |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6644 | ElemSize = CGF.getTypeSize(PTy->getPointeeType().getCanonicalType()); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6645 | } else { |
| 6646 | const auto *ATy = cast<ArrayType>(BaseTy.getTypePtr()); |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6647 | assert(ATy && "Expecting array type if not a pointer type."); |
| 6648 | ElemSize = CGF.getTypeSize(ATy->getElementType().getCanonicalType()); |
| 6649 | } |
| 6650 | |
| 6651 | // If we don't have a length at this point, that is because we have an |
| 6652 | // array section with a single element. |
| 6653 | if (!OAE->getLength()) |
| 6654 | return ElemSize; |
| 6655 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6656 | llvm::Value *LengthVal = CGF.EmitScalarExpr(OAE->getLength()); |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6657 | LengthVal = |
| 6658 | CGF.Builder.CreateIntCast(LengthVal, CGF.SizeTy, /*isSigned=*/false); |
| 6659 | return CGF.Builder.CreateNUWMul(LengthVal, ElemSize); |
| 6660 | } |
| 6661 | return CGF.getTypeSize(ExprTy); |
| 6662 | } |
| 6663 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 6664 | /// Return the corresponding bits for a given map clause modifier. Add |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6665 | /// 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] | 6666 | /// map as the first one of a series of maps that relate to the same map |
| 6667 | /// expression. |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 6668 | OpenMPOffloadMappingFlags getMapTypeBits(OpenMPMapClauseKind MapType, |
| 6669 | OpenMPMapClauseKind MapTypeModifier, |
| 6670 | bool IsImplicit, bool AddPtrFlag, |
| 6671 | bool AddIsTargetParamFlag) const { |
| 6672 | OpenMPOffloadMappingFlags Bits = |
| 6673 | IsImplicit ? OMP_MAP_IMPLICIT : OMP_MAP_NONE; |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6674 | switch (MapType) { |
| 6675 | case OMPC_MAP_alloc: |
Samuel Antao | 6782e94 | 2016-05-26 16:48:10 +0000 | [diff] [blame] | 6676 | case OMPC_MAP_release: |
| 6677 | // alloc and release is the default behavior in the runtime library, i.e. |
| 6678 | // if we don't pass any bits alloc/release that is what the runtime is |
| 6679 | // going to do. Therefore, we don't need to signal anything for these two |
| 6680 | // type modifiers. |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6681 | break; |
| 6682 | case OMPC_MAP_to: |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 6683 | Bits |= OMP_MAP_TO; |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6684 | break; |
| 6685 | case OMPC_MAP_from: |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 6686 | Bits |= OMP_MAP_FROM; |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6687 | break; |
| 6688 | case OMPC_MAP_tofrom: |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 6689 | Bits |= OMP_MAP_TO | OMP_MAP_FROM; |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6690 | break; |
| 6691 | case OMPC_MAP_delete: |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 6692 | Bits |= OMP_MAP_DELETE; |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6693 | break; |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 6694 | case OMPC_MAP_always: |
| 6695 | case OMPC_MAP_unknown: |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6696 | llvm_unreachable("Unexpected map type!"); |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6697 | } |
| 6698 | if (AddPtrFlag) |
George Rokos | 065755d | 2017-11-07 18:27:04 +0000 | [diff] [blame] | 6699 | Bits |= OMP_MAP_PTR_AND_OBJ; |
| 6700 | if (AddIsTargetParamFlag) |
| 6701 | Bits |= OMP_MAP_TARGET_PARAM; |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6702 | if (MapTypeModifier == OMPC_MAP_always) |
| 6703 | Bits |= OMP_MAP_ALWAYS; |
| 6704 | return Bits; |
| 6705 | } |
| 6706 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 6707 | /// Return true if the provided expression is a final array section. A |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6708 | /// final array section, is one whose length can't be proved to be one. |
| 6709 | bool isFinalArraySectionExpression(const Expr *E) const { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6710 | const auto *OASE = dyn_cast<OMPArraySectionExpr>(E); |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6711 | |
| 6712 | // It is not an array section and therefore not a unity-size one. |
| 6713 | if (!OASE) |
| 6714 | return false; |
| 6715 | |
| 6716 | // An array section with no colon always refer to a single element. |
| 6717 | if (OASE->getColonLoc().isInvalid()) |
| 6718 | return false; |
| 6719 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6720 | const Expr *Length = OASE->getLength(); |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6721 | |
| 6722 | // If we don't have a length we have to check if the array has size 1 |
| 6723 | // for this dimension. Also, we should always expect a length if the |
| 6724 | // base type is pointer. |
| 6725 | if (!Length) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6726 | QualType BaseQTy = OMPArraySectionExpr::getBaseOriginalType( |
| 6727 | OASE->getBase()->IgnoreParenImpCasts()) |
| 6728 | .getCanonicalType(); |
| 6729 | if (const auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr())) |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6730 | return ATy->getSize().getSExtValue() != 1; |
| 6731 | // If we don't have a constant dimension length, we have to consider |
| 6732 | // the current section as having any size, so it is not necessarily |
| 6733 | // unitary. If it happen to be unity size, that's user fault. |
| 6734 | return true; |
| 6735 | } |
| 6736 | |
| 6737 | // Check if the length evaluates to 1. |
| 6738 | llvm::APSInt ConstLength; |
| 6739 | if (!Length->EvaluateAsInt(ConstLength, CGF.getContext())) |
| 6740 | return true; // Can have more that size 1. |
| 6741 | |
| 6742 | return ConstLength.getSExtValue() != 1; |
| 6743 | } |
| 6744 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 6745 | /// Generate the base pointers, section pointers, sizes and map type |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6746 | /// bits for the provided map type, map modifier, and expression components. |
| 6747 | /// \a IsFirstComponent should be set to true if the provided set of |
| 6748 | /// components is the first associated with a capture. |
| 6749 | void generateInfoForComponentList( |
| 6750 | OpenMPMapClauseKind MapType, OpenMPMapClauseKind MapTypeModifier, |
| 6751 | OMPClauseMappableExprCommon::MappableExprComponentListRef Components, |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 6752 | MapBaseValuesArrayTy &BasePointers, MapValuesArrayTy &Pointers, |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6753 | MapValuesArrayTy &Sizes, MapFlagsArrayTy &Types, |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 6754 | StructRangeInfoTy &PartialStruct, bool IsFirstComponentList, |
Alexey Bataev | e82445f | 2018-09-20 13:54:02 +0000 | [diff] [blame^] | 6755 | bool IsImplicit, |
| 6756 | ArrayRef<OMPClauseMappableExprCommon::MappableExprComponentListRef> |
| 6757 | OverlappedElements = llvm::None) const { |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6758 | // 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] | 6759 | // types below. The generated information is expressed in this order: |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6760 | // base pointer, section pointer, size, flags |
| 6761 | // (to add to the ones that come from the map type and modifier). |
| 6762 | // |
| 6763 | // double d; |
| 6764 | // int i[100]; |
| 6765 | // float *p; |
| 6766 | // |
| 6767 | // struct S1 { |
| 6768 | // int i; |
| 6769 | // float f[50]; |
| 6770 | // } |
| 6771 | // struct S2 { |
| 6772 | // int i; |
| 6773 | // float f[50]; |
| 6774 | // S1 s; |
| 6775 | // double *p; |
| 6776 | // struct S2 *ps; |
| 6777 | // } |
| 6778 | // S2 s; |
| 6779 | // S2 *ps; |
| 6780 | // |
| 6781 | // map(d) |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 6782 | // &d, &d, sizeof(double), TARGET_PARAM | TO | FROM |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6783 | // |
| 6784 | // map(i) |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 6785 | // &i, &i, 100*sizeof(int), TARGET_PARAM | TO | FROM |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6786 | // |
| 6787 | // map(i[1:23]) |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 6788 | // &i(=&i[0]), &i[1], 23*sizeof(int), TARGET_PARAM | TO | FROM |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6789 | // |
| 6790 | // map(p) |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 6791 | // &p, &p, sizeof(float*), TARGET_PARAM | TO | FROM |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6792 | // |
| 6793 | // map(p[1:24]) |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 6794 | // p, &p[1], 24*sizeof(float), TARGET_PARAM | TO | FROM |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6795 | // |
| 6796 | // map(s) |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 6797 | // &s, &s, sizeof(S2), TARGET_PARAM | TO | FROM |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6798 | // |
| 6799 | // map(s.i) |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 6800 | // &s, &(s.i), sizeof(int), TARGET_PARAM | TO | FROM |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6801 | // |
| 6802 | // map(s.s.f) |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 6803 | // &s, &(s.s.f[0]), 50*sizeof(float), TARGET_PARAM | TO | FROM |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6804 | // |
| 6805 | // map(s.p) |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 6806 | // &s, &(s.p), sizeof(double*), TARGET_PARAM | TO | FROM |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6807 | // |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 6808 | // map(to: s.p[:22]) |
| 6809 | // &s, &(s.p), sizeof(double*), TARGET_PARAM (*) |
| 6810 | // &s, &(s.p), sizeof(double*), MEMBER_OF(1) (**) |
| 6811 | // &(s.p), &(s.p[0]), 22*sizeof(double), |
| 6812 | // MEMBER_OF(1) | PTR_AND_OBJ | TO (***) |
| 6813 | // (*) alloc space for struct members, only this is a target parameter |
| 6814 | // (**) map the pointer (nothing to be mapped in this example) (the compiler |
| 6815 | // optimizes this entry out, same in the examples below) |
| 6816 | // (***) map the pointee (map: to) |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6817 | // |
| 6818 | // map(s.ps) |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 6819 | // &s, &(s.ps), sizeof(S2*), TARGET_PARAM | TO | FROM |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6820 | // |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 6821 | // map(from: s.ps->s.i) |
| 6822 | // &s, &(s.ps), sizeof(S2*), TARGET_PARAM |
| 6823 | // &s, &(s.ps), sizeof(S2*), MEMBER_OF(1) |
| 6824 | // &(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] | 6825 | // |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 6826 | // map(to: s.ps->ps) |
| 6827 | // &s, &(s.ps), sizeof(S2*), TARGET_PARAM |
| 6828 | // &s, &(s.ps), sizeof(S2*), MEMBER_OF(1) |
| 6829 | // &(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] | 6830 | // |
| 6831 | // map(s.ps->ps->ps) |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 6832 | // &s, &(s.ps), sizeof(S2*), TARGET_PARAM |
| 6833 | // &s, &(s.ps), sizeof(S2*), MEMBER_OF(1) |
| 6834 | // &(s.ps), &(s.ps->ps), sizeof(S2*), MEMBER_OF(1) | PTR_AND_OBJ |
| 6835 | // &(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] | 6836 | // |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 6837 | // map(to: s.ps->ps->s.f[:22]) |
| 6838 | // &s, &(s.ps), sizeof(S2*), TARGET_PARAM |
| 6839 | // &s, &(s.ps), sizeof(S2*), MEMBER_OF(1) |
| 6840 | // &(s.ps), &(s.ps->ps), sizeof(S2*), MEMBER_OF(1) | PTR_AND_OBJ |
| 6841 | // &(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] | 6842 | // |
| 6843 | // map(ps) |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 6844 | // &ps, &ps, sizeof(S2*), TARGET_PARAM | TO | FROM |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6845 | // |
| 6846 | // map(ps->i) |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 6847 | // ps, &(ps->i), sizeof(int), TARGET_PARAM | TO | FROM |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6848 | // |
| 6849 | // map(ps->s.f) |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 6850 | // ps, &(ps->s.f[0]), 50*sizeof(float), 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: ps->p) |
| 6853 | // ps, &(ps->p), sizeof(double*), TARGET_PARAM | FROM |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6854 | // |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 6855 | // map(to: ps->p[:22]) |
| 6856 | // ps, &(ps->p), sizeof(double*), TARGET_PARAM |
| 6857 | // ps, &(ps->p), sizeof(double*), MEMBER_OF(1) |
| 6858 | // &(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] | 6859 | // |
| 6860 | // map(ps->ps) |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 6861 | // ps, &(ps->ps), sizeof(S2*), TARGET_PARAM | TO | FROM |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6862 | // |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 6863 | // map(from: ps->ps->s.i) |
| 6864 | // ps, &(ps->ps), sizeof(S2*), TARGET_PARAM |
| 6865 | // ps, &(ps->ps), sizeof(S2*), MEMBER_OF(1) |
| 6866 | // &(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] | 6867 | // |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 6868 | // map(from: ps->ps->ps) |
| 6869 | // ps, &(ps->ps), sizeof(S2*), TARGET_PARAM |
| 6870 | // ps, &(ps->ps), sizeof(S2*), MEMBER_OF(1) |
| 6871 | // &(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] | 6872 | // |
| 6873 | // map(ps->ps->ps->ps) |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 6874 | // ps, &(ps->ps), sizeof(S2*), TARGET_PARAM |
| 6875 | // ps, &(ps->ps), sizeof(S2*), MEMBER_OF(1) |
| 6876 | // &(ps->ps), &(ps->ps->ps), sizeof(S2*), MEMBER_OF(1) | PTR_AND_OBJ |
| 6877 | // &(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] | 6878 | // |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 6879 | // map(to: ps->ps->ps->s.f[:22]) |
| 6880 | // ps, &(ps->ps), sizeof(S2*), TARGET_PARAM |
| 6881 | // ps, &(ps->ps), sizeof(S2*), MEMBER_OF(1) |
| 6882 | // &(ps->ps), &(ps->ps->ps), sizeof(S2*), MEMBER_OF(1) | PTR_AND_OBJ |
| 6883 | // &(ps->ps->ps), &(ps->ps->ps->s.f[0]), 22*sizeof(float), PTR_AND_OBJ | TO |
| 6884 | // |
| 6885 | // map(to: s.f[:22]) map(from: s.p[:33]) |
| 6886 | // &s, &(s.f[0]), 50*sizeof(float) + sizeof(struct S1) + |
| 6887 | // sizeof(double*) (**), TARGET_PARAM |
| 6888 | // &s, &(s.f[0]), 22*sizeof(float), MEMBER_OF(1) | TO |
| 6889 | // &s, &(s.p), sizeof(double*), MEMBER_OF(1) |
| 6890 | // &(s.p), &(s.p[0]), 33*sizeof(double), MEMBER_OF(1) | PTR_AND_OBJ | FROM |
| 6891 | // (*) allocate contiguous space needed to fit all mapped members even if |
| 6892 | // we allocate space for members not mapped (in this example, |
| 6893 | // s.f[22..49] and s.s are not mapped, yet we must allocate space for |
| 6894 | // them as well because they fall between &s.f[0] and &s.p) |
| 6895 | // |
| 6896 | // map(from: s.f[:22]) map(to: ps->p[:33]) |
| 6897 | // &s, &(s.f[0]), 22*sizeof(float), TARGET_PARAM | FROM |
| 6898 | // ps, &(ps->p), sizeof(S2*), TARGET_PARAM |
| 6899 | // ps, &(ps->p), sizeof(double*), MEMBER_OF(2) (*) |
| 6900 | // &(ps->p), &(ps->p[0]), 33*sizeof(double), MEMBER_OF(2) | PTR_AND_OBJ | TO |
| 6901 | // (*) the struct this entry pertains to is the 2nd element in the list of |
| 6902 | // arguments, hence MEMBER_OF(2) |
| 6903 | // |
| 6904 | // map(from: s.f[:22], s.s) map(to: ps->p[:33]) |
| 6905 | // &s, &(s.f[0]), 50*sizeof(float) + sizeof(struct S1), TARGET_PARAM |
| 6906 | // &s, &(s.f[0]), 22*sizeof(float), MEMBER_OF(1) | FROM |
| 6907 | // &s, &(s.s), sizeof(struct S1), MEMBER_OF(1) | FROM |
| 6908 | // ps, &(ps->p), sizeof(S2*), TARGET_PARAM |
| 6909 | // ps, &(ps->p), sizeof(double*), MEMBER_OF(4) (*) |
| 6910 | // &(ps->p), &(ps->p[0]), 33*sizeof(double), MEMBER_OF(4) | PTR_AND_OBJ | TO |
| 6911 | // (*) the struct this entry pertains to is the 4th element in the list |
| 6912 | // of arguments, hence MEMBER_OF(4) |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6913 | |
| 6914 | // Track if the map information being generated is the first for a capture. |
| 6915 | bool IsCaptureFirstInfo = IsFirstComponentList; |
Alexey Bataev | 92327c5 | 2018-03-26 16:40:55 +0000 | [diff] [blame] | 6916 | bool IsLink = false; // Is this variable a "declare target link"? |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6917 | |
| 6918 | // Scan the components from the base to the complete expression. |
| 6919 | auto CI = Components.rbegin(); |
| 6920 | auto CE = Components.rend(); |
| 6921 | auto I = CI; |
| 6922 | |
| 6923 | // Track if the map information being generated is the first for a list of |
| 6924 | // components. |
| 6925 | bool IsExpressionFirstInfo = true; |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 6926 | Address BP = Address::invalid(); |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6927 | |
Erich Keane | e69755a | 2018-07-19 17:19:16 +0000 | [diff] [blame] | 6928 | if (isa<MemberExpr>(I->getAssociatedExpression())) { |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6929 | // The base is the 'this' pointer. The content of the pointer is going |
| 6930 | // to be the base of the field being mapped. |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 6931 | BP = CGF.LoadCXXThisAddress(); |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6932 | } else { |
| 6933 | // The base is the reference to the variable. |
| 6934 | // BP = &Var. |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 6935 | BP = CGF.EmitOMPSharedLValue(I->getAssociatedExpression()).getAddress(); |
Alexey Bataev | 92327c5 | 2018-03-26 16:40:55 +0000 | [diff] [blame] | 6936 | if (const auto *VD = |
| 6937 | dyn_cast_or_null<VarDecl>(I->getAssociatedDeclaration())) { |
| 6938 | if (llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res = |
Alexey Bataev | 97b7221 | 2018-08-14 18:31:20 +0000 | [diff] [blame] | 6939 | OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD)) |
Alexey Bataev | 2c1dffe | 2018-04-16 20:34:41 +0000 | [diff] [blame] | 6940 | if (*Res == OMPDeclareTargetDeclAttr::MT_Link) { |
| 6941 | IsLink = true; |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 6942 | BP = CGF.CGM.getOpenMPRuntime().getAddrOfDeclareTargetLink(VD); |
Alexey Bataev | 2c1dffe | 2018-04-16 20:34:41 +0000 | [diff] [blame] | 6943 | } |
Alexey Bataev | 92327c5 | 2018-03-26 16:40:55 +0000 | [diff] [blame] | 6944 | } |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6945 | |
| 6946 | // 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] | 6947 | // 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] | 6948 | // reference. References are ignored for mapping purposes. |
| 6949 | QualType Ty = |
| 6950 | I->getAssociatedDeclaration()->getType().getNonReferenceType(); |
| 6951 | if (Ty->isAnyPointerType() && std::next(I) != CE) { |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 6952 | BP = CGF.EmitLoadOfPointer(BP, Ty->castAs<PointerType>()); |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6953 | |
| 6954 | // We do not need to generate individual map information for the |
| 6955 | // pointer, it can be associated with the combined storage. |
| 6956 | ++I; |
| 6957 | } |
| 6958 | } |
| 6959 | |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 6960 | // Track whether a component of the list should be marked as MEMBER_OF some |
| 6961 | // combined entry (for partial structs). Only the first PTR_AND_OBJ entry |
| 6962 | // in a component list should be marked as MEMBER_OF, all subsequent entries |
| 6963 | // do not belong to the base struct. E.g. |
| 6964 | // struct S2 s; |
| 6965 | // s.ps->ps->ps->f[:] |
| 6966 | // (1) (2) (3) (4) |
| 6967 | // ps(1) is a member pointer, ps(2) is a pointee of ps(1), so it is a |
| 6968 | // PTR_AND_OBJ entry; the PTR is ps(1), so MEMBER_OF the base struct. ps(3) |
| 6969 | // is the pointee of ps(2) which is not member of struct s, so it should not |
| 6970 | // be marked as such (it is still PTR_AND_OBJ). |
| 6971 | // The variable is initialized to false so that PTR_AND_OBJ entries which |
| 6972 | // are not struct members are not considered (e.g. array of pointers to |
| 6973 | // data). |
| 6974 | bool ShouldBeMemberOf = false; |
| 6975 | |
| 6976 | // Variable keeping track of whether or not we have encountered a component |
| 6977 | // in the component list which is a member expression. Useful when we have a |
| 6978 | // pointer or a final array section, in which case it is the previous |
| 6979 | // component in the list which tells us whether we have a member expression. |
| 6980 | // E.g. X.f[:] |
| 6981 | // While processing the final array section "[:]" it is "f" which tells us |
| 6982 | // whether we are dealing with a member of a declared struct. |
| 6983 | const MemberExpr *EncounteredME = nullptr; |
| 6984 | |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6985 | for (; I != CE; ++I) { |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 6986 | // If the current component is member of a struct (parent struct) mark it. |
| 6987 | if (!EncounteredME) { |
| 6988 | EncounteredME = dyn_cast<MemberExpr>(I->getAssociatedExpression()); |
| 6989 | // If we encounter a PTR_AND_OBJ entry from now on it should be marked |
| 6990 | // as MEMBER_OF the parent struct. |
| 6991 | if (EncounteredME) |
| 6992 | ShouldBeMemberOf = true; |
| 6993 | } |
| 6994 | |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6995 | auto Next = std::next(I); |
| 6996 | |
| 6997 | // We need to generate the addresses and sizes if this is the last |
| 6998 | // component, if the component is a pointer or if it is an array section |
| 6999 | // whose length can't be proved to be one. If this is a pointer, it |
| 7000 | // becomes the base address for the following components. |
| 7001 | |
| 7002 | // A final array section, is one whose length can't be proved to be one. |
| 7003 | bool IsFinalArraySection = |
| 7004 | isFinalArraySectionExpression(I->getAssociatedExpression()); |
| 7005 | |
| 7006 | // Get information on whether the element is a pointer. Have to do a |
| 7007 | // special treatment for array sections given that they are built-in |
| 7008 | // types. |
| 7009 | const auto *OASE = |
| 7010 | dyn_cast<OMPArraySectionExpr>(I->getAssociatedExpression()); |
| 7011 | bool IsPointer = |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7012 | (OASE && OMPArraySectionExpr::getBaseOriginalType(OASE) |
| 7013 | .getCanonicalType() |
| 7014 | ->isAnyPointerType()) || |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7015 | I->getAssociatedExpression()->getType()->isAnyPointerType(); |
| 7016 | |
| 7017 | if (Next == CE || IsPointer || IsFinalArraySection) { |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7018 | // If this is not the last component, we expect the pointer to be |
| 7019 | // associated with an array expression or member expression. |
| 7020 | assert((Next == CE || |
| 7021 | isa<MemberExpr>(Next->getAssociatedExpression()) || |
| 7022 | isa<ArraySubscriptExpr>(Next->getAssociatedExpression()) || |
| 7023 | isa<OMPArraySectionExpr>(Next->getAssociatedExpression())) && |
| 7024 | "Unexpected expression"); |
| 7025 | |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7026 | Address LB = |
| 7027 | CGF.EmitOMPSharedLValue(I->getAssociatedExpression()).getAddress(); |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7028 | |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7029 | // If this component is a pointer inside the base struct then we don't |
| 7030 | // need to create any entry for it - it will be combined with the object |
| 7031 | // it is pointing to into a single PTR_AND_OBJ entry. |
| 7032 | bool IsMemberPointer = |
| 7033 | IsPointer && EncounteredME && |
| 7034 | (dyn_cast<MemberExpr>(I->getAssociatedExpression()) == |
| 7035 | EncounteredME); |
Alexey Bataev | e82445f | 2018-09-20 13:54:02 +0000 | [diff] [blame^] | 7036 | if (!OverlappedElements.empty()) { |
| 7037 | // Handle base element with the info for overlapped elements. |
| 7038 | assert(!PartialStruct.Base.isValid() && "The base element is set."); |
| 7039 | assert(Next == CE && |
| 7040 | "Expected last element for the overlapped elements."); |
| 7041 | assert(!IsPointer && |
| 7042 | "Unexpected base element with the pointer type."); |
| 7043 | // Mark the whole struct as the struct that requires allocation on the |
| 7044 | // device. |
| 7045 | PartialStruct.LowestElem = {0, LB}; |
| 7046 | CharUnits TypeSize = CGF.getContext().getTypeSizeInChars( |
| 7047 | I->getAssociatedExpression()->getType()); |
| 7048 | Address HB = CGF.Builder.CreateConstGEP( |
| 7049 | CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(LB, |
| 7050 | CGF.VoidPtrTy), |
| 7051 | TypeSize.getQuantity() - 1, CharUnits::One()); |
| 7052 | PartialStruct.HighestElem = { |
| 7053 | std::numeric_limits<decltype( |
| 7054 | PartialStruct.HighestElem.first)>::max(), |
| 7055 | HB}; |
| 7056 | PartialStruct.Base = BP; |
| 7057 | // Emit data for non-overlapped data. |
| 7058 | OpenMPOffloadMappingFlags Flags = |
| 7059 | OMP_MAP_MEMBER_OF | |
| 7060 | getMapTypeBits(MapType, MapTypeModifier, IsImplicit, |
| 7061 | /*AddPtrFlag=*/false, |
| 7062 | /*AddIsTargetParamFlag=*/false); |
| 7063 | LB = BP; |
| 7064 | llvm::Value *Size = nullptr; |
| 7065 | // Do bitcopy of all non-overlapped structure elements. |
| 7066 | for (OMPClauseMappableExprCommon::MappableExprComponentListRef |
| 7067 | Component : OverlappedElements) { |
| 7068 | Address ComponentLB = Address::invalid(); |
| 7069 | for (const OMPClauseMappableExprCommon::MappableComponent &MC : |
| 7070 | Component) { |
| 7071 | if (MC.getAssociatedDeclaration()) { |
| 7072 | ComponentLB = |
| 7073 | CGF.EmitOMPSharedLValue(MC.getAssociatedExpression()) |
| 7074 | .getAddress(); |
| 7075 | Size = CGF.Builder.CreatePtrDiff( |
| 7076 | CGF.EmitCastToVoidPtr(ComponentLB.getPointer()), |
| 7077 | CGF.EmitCastToVoidPtr(LB.getPointer())); |
| 7078 | break; |
| 7079 | } |
| 7080 | } |
| 7081 | BasePointers.push_back(BP.getPointer()); |
| 7082 | Pointers.push_back(LB.getPointer()); |
| 7083 | Sizes.push_back(Size); |
| 7084 | Types.push_back(Flags); |
| 7085 | LB = CGF.Builder.CreateConstGEP(ComponentLB, 1, |
| 7086 | CGF.getPointerSize()); |
| 7087 | } |
| 7088 | BasePointers.push_back(BP.getPointer()); |
| 7089 | Pointers.push_back(LB.getPointer()); |
| 7090 | Size = CGF.Builder.CreatePtrDiff( |
| 7091 | CGF.EmitCastToVoidPtr( |
| 7092 | CGF.Builder.CreateConstGEP(HB, 1, CharUnits::One()) |
| 7093 | .getPointer()), |
| 7094 | CGF.EmitCastToVoidPtr(LB.getPointer())); |
| 7095 | Sizes.push_back(Size); |
| 7096 | Types.push_back(Flags); |
| 7097 | break; |
| 7098 | } |
| 7099 | llvm::Value *Size = getExprTypeSize(I->getAssociatedExpression()); |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7100 | if (!IsMemberPointer) { |
| 7101 | BasePointers.push_back(BP.getPointer()); |
| 7102 | Pointers.push_back(LB.getPointer()); |
| 7103 | Sizes.push_back(Size); |
Samuel Antao | 03a3cec | 2016-07-27 22:52:16 +0000 | [diff] [blame] | 7104 | |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7105 | // We need to add a pointer flag for each map that comes from the |
| 7106 | // same expression except for the first one. We also need to signal |
| 7107 | // this map is the first one that relates with the current capture |
| 7108 | // (there is a set of entries for each capture). |
| 7109 | OpenMPOffloadMappingFlags Flags = getMapTypeBits( |
| 7110 | MapType, MapTypeModifier, IsImplicit, |
| 7111 | !IsExpressionFirstInfo || IsLink, IsCaptureFirstInfo && !IsLink); |
| 7112 | |
| 7113 | if (!IsExpressionFirstInfo) { |
| 7114 | // If we have a PTR_AND_OBJ pair where the OBJ is a pointer as well, |
| 7115 | // then we reset the TO/FROM/ALWAYS/DELETE flags. |
| 7116 | if (IsPointer) |
| 7117 | Flags &= ~(OMP_MAP_TO | OMP_MAP_FROM | OMP_MAP_ALWAYS | |
| 7118 | OMP_MAP_DELETE); |
| 7119 | |
| 7120 | if (ShouldBeMemberOf) { |
| 7121 | // Set placeholder value MEMBER_OF=FFFF to indicate that the flag |
| 7122 | // should be later updated with the correct value of MEMBER_OF. |
| 7123 | Flags |= OMP_MAP_MEMBER_OF; |
| 7124 | // From now on, all subsequent PTR_AND_OBJ entries should not be |
| 7125 | // marked as MEMBER_OF. |
| 7126 | ShouldBeMemberOf = false; |
| 7127 | } |
| 7128 | } |
| 7129 | |
| 7130 | Types.push_back(Flags); |
Samuel Antao | 03a3cec | 2016-07-27 22:52:16 +0000 | [diff] [blame] | 7131 | } |
| 7132 | |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7133 | // If we have encountered a member expression so far, keep track of the |
| 7134 | // mapped member. If the parent is "*this", then the value declaration |
| 7135 | // is nullptr. |
| 7136 | if (EncounteredME) { |
| 7137 | const auto *FD = dyn_cast<FieldDecl>(EncounteredME->getMemberDecl()); |
| 7138 | unsigned FieldIndex = FD->getFieldIndex(); |
Samuel Antao | 03a3cec | 2016-07-27 22:52:16 +0000 | [diff] [blame] | 7139 | |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7140 | // Update info about the lowest and highest elements for this struct |
| 7141 | if (!PartialStruct.Base.isValid()) { |
| 7142 | PartialStruct.LowestElem = {FieldIndex, LB}; |
| 7143 | PartialStruct.HighestElem = {FieldIndex, LB}; |
| 7144 | PartialStruct.Base = BP; |
| 7145 | } else if (FieldIndex < PartialStruct.LowestElem.first) { |
| 7146 | PartialStruct.LowestElem = {FieldIndex, LB}; |
| 7147 | } else if (FieldIndex > PartialStruct.HighestElem.first) { |
| 7148 | PartialStruct.HighestElem = {FieldIndex, LB}; |
| 7149 | } |
| 7150 | } |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7151 | |
| 7152 | // If we have a final array section, we are done with this expression. |
| 7153 | if (IsFinalArraySection) |
| 7154 | break; |
| 7155 | |
| 7156 | // The pointer becomes the base for the next element. |
| 7157 | if (Next != CE) |
| 7158 | BP = LB; |
| 7159 | |
| 7160 | IsExpressionFirstInfo = false; |
| 7161 | IsCaptureFirstInfo = false; |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7162 | } |
| 7163 | } |
| 7164 | } |
| 7165 | |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7166 | /// Return the adjusted map modifiers if the declaration a capture refers to |
| 7167 | /// appears in a first-private clause. This is expected to be used only with |
| 7168 | /// directives that start with 'target'. |
| 7169 | MappableExprsHandler::OpenMPOffloadMappingFlags |
| 7170 | getMapModifiersForPrivateClauses(const CapturedStmt::Capture &Cap) const { |
| 7171 | assert(Cap.capturesVariable() && "Expected capture by reference only!"); |
| 7172 | |
| 7173 | // A first private variable captured by reference will use only the |
| 7174 | // 'private ptr' and 'map to' flag. Return the right flags if the captured |
| 7175 | // declaration is known as first-private in this handler. |
| 7176 | if (FirstPrivateDecls.count(Cap.getCapturedVar())) |
| 7177 | return MappableExprsHandler::OMP_MAP_PRIVATE | |
| 7178 | MappableExprsHandler::OMP_MAP_TO; |
| 7179 | return MappableExprsHandler::OMP_MAP_TO | |
| 7180 | MappableExprsHandler::OMP_MAP_FROM; |
| 7181 | } |
| 7182 | |
| 7183 | static OpenMPOffloadMappingFlags getMemberOfFlag(unsigned Position) { |
| 7184 | // Member of is given by the 16 MSB of the flag, so rotate by 48 bits. |
| 7185 | return static_cast<OpenMPOffloadMappingFlags>(((uint64_t)Position + 1) |
| 7186 | << 48); |
| 7187 | } |
| 7188 | |
| 7189 | static void setCorrectMemberOfFlag(OpenMPOffloadMappingFlags &Flags, |
| 7190 | OpenMPOffloadMappingFlags MemberOfFlag) { |
| 7191 | // If the entry is PTR_AND_OBJ but has not been marked with the special |
| 7192 | // placeholder value 0xFFFF in the MEMBER_OF field, then it should not be |
| 7193 | // marked as MEMBER_OF. |
| 7194 | if ((Flags & OMP_MAP_PTR_AND_OBJ) && |
| 7195 | ((Flags & OMP_MAP_MEMBER_OF) != OMP_MAP_MEMBER_OF)) |
| 7196 | return; |
| 7197 | |
| 7198 | // Reset the placeholder value to prepare the flag for the assignment of the |
| 7199 | // proper MEMBER_OF value. |
| 7200 | Flags &= ~OMP_MAP_MEMBER_OF; |
| 7201 | Flags |= MemberOfFlag; |
| 7202 | } |
| 7203 | |
Alexey Bataev | e82445f | 2018-09-20 13:54:02 +0000 | [diff] [blame^] | 7204 | void getPlainLayout(const CXXRecordDecl *RD, |
| 7205 | llvm::SmallVectorImpl<const FieldDecl *> &Layout, |
| 7206 | bool AsBase) const { |
| 7207 | const CGRecordLayout &RL = CGF.getTypes().getCGRecordLayout(RD); |
| 7208 | |
| 7209 | llvm::StructType *St = |
| 7210 | AsBase ? RL.getBaseSubobjectLLVMType() : RL.getLLVMType(); |
| 7211 | |
| 7212 | unsigned NumElements = St->getNumElements(); |
| 7213 | llvm::SmallVector< |
| 7214 | llvm::PointerUnion<const CXXRecordDecl *, const FieldDecl *>, 4> |
| 7215 | RecordLayout(NumElements); |
| 7216 | |
| 7217 | // Fill bases. |
| 7218 | for (const auto &I : RD->bases()) { |
| 7219 | if (I.isVirtual()) |
| 7220 | continue; |
| 7221 | const auto *Base = I.getType()->getAsCXXRecordDecl(); |
| 7222 | // Ignore empty bases. |
| 7223 | if (Base->isEmpty() || CGF.getContext() |
| 7224 | .getASTRecordLayout(Base) |
| 7225 | .getNonVirtualSize() |
| 7226 | .isZero()) |
| 7227 | continue; |
| 7228 | |
| 7229 | unsigned FieldIndex = RL.getNonVirtualBaseLLVMFieldNo(Base); |
| 7230 | RecordLayout[FieldIndex] = Base; |
| 7231 | } |
| 7232 | // Fill in virtual bases. |
| 7233 | for (const auto &I : RD->vbases()) { |
| 7234 | const auto *Base = I.getType()->getAsCXXRecordDecl(); |
| 7235 | // Ignore empty bases. |
| 7236 | if (Base->isEmpty()) |
| 7237 | continue; |
| 7238 | unsigned FieldIndex = RL.getVirtualBaseIndex(Base); |
| 7239 | if (RecordLayout[FieldIndex]) |
| 7240 | continue; |
| 7241 | RecordLayout[FieldIndex] = Base; |
| 7242 | } |
| 7243 | // Fill in all the fields. |
| 7244 | assert(!RD->isUnion() && "Unexpected union."); |
| 7245 | for (const auto *Field : RD->fields()) { |
| 7246 | // Fill in non-bitfields. (Bitfields always use a zero pattern, which we |
| 7247 | // will fill in later.) |
| 7248 | if (!Field->isBitField()) { |
| 7249 | unsigned FieldIndex = RL.getLLVMFieldNo(Field); |
| 7250 | RecordLayout[FieldIndex] = Field; |
| 7251 | } |
| 7252 | } |
| 7253 | for (const llvm::PointerUnion<const CXXRecordDecl *, const FieldDecl *> |
| 7254 | &Data : RecordLayout) { |
| 7255 | if (Data.isNull()) |
| 7256 | continue; |
| 7257 | if (const auto *Base = Data.dyn_cast<const CXXRecordDecl *>()) |
| 7258 | getPlainLayout(Base, Layout, /*AsBase=*/true); |
| 7259 | else |
| 7260 | Layout.push_back(Data.get<const FieldDecl *>()); |
| 7261 | } |
| 7262 | } |
| 7263 | |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7264 | public: |
| 7265 | MappableExprsHandler(const OMPExecutableDirective &Dir, CodeGenFunction &CGF) |
| 7266 | : CurDir(Dir), CGF(CGF) { |
| 7267 | // Extract firstprivate clause information. |
| 7268 | for (const auto *C : Dir.getClausesOfKind<OMPFirstprivateClause>()) |
| 7269 | for (const auto *D : C->varlists()) |
| 7270 | FirstPrivateDecls.insert( |
| 7271 | cast<VarDecl>(cast<DeclRefExpr>(D)->getDecl())->getCanonicalDecl()); |
| 7272 | // Extract device pointer clause information. |
| 7273 | for (const auto *C : Dir.getClausesOfKind<OMPIsDevicePtrClause>()) |
| 7274 | for (auto L : C->component_lists()) |
| 7275 | DevPointersMap[L.first].push_back(L.second); |
| 7276 | } |
| 7277 | |
| 7278 | /// Generate code for the combined entry if we have a partially mapped struct |
| 7279 | /// and take care of the mapping flags of the arguments corresponding to |
| 7280 | /// individual struct members. |
| 7281 | void emitCombinedEntry(MapBaseValuesArrayTy &BasePointers, |
| 7282 | MapValuesArrayTy &Pointers, MapValuesArrayTy &Sizes, |
| 7283 | MapFlagsArrayTy &Types, MapFlagsArrayTy &CurTypes, |
| 7284 | const StructRangeInfoTy &PartialStruct) const { |
| 7285 | // Base is the base of the struct |
| 7286 | BasePointers.push_back(PartialStruct.Base.getPointer()); |
| 7287 | // Pointer is the address of the lowest element |
| 7288 | llvm::Value *LB = PartialStruct.LowestElem.second.getPointer(); |
| 7289 | Pointers.push_back(LB); |
| 7290 | // Size is (addr of {highest+1} element) - (addr of lowest element) |
| 7291 | llvm::Value *HB = PartialStruct.HighestElem.second.getPointer(); |
| 7292 | llvm::Value *HAddr = CGF.Builder.CreateConstGEP1_32(HB, /*Idx0=*/1); |
| 7293 | llvm::Value *CLAddr = CGF.Builder.CreatePointerCast(LB, CGF.VoidPtrTy); |
| 7294 | llvm::Value *CHAddr = CGF.Builder.CreatePointerCast(HAddr, CGF.VoidPtrTy); |
| 7295 | llvm::Value *Diff = CGF.Builder.CreatePtrDiff(CHAddr, CLAddr); |
| 7296 | llvm::Value *Size = CGF.Builder.CreateIntCast(Diff, CGF.SizeTy, |
| 7297 | /*isSinged=*/false); |
| 7298 | Sizes.push_back(Size); |
| 7299 | // Map type is always TARGET_PARAM |
| 7300 | Types.push_back(OMP_MAP_TARGET_PARAM); |
| 7301 | // Remove TARGET_PARAM flag from the first element |
| 7302 | (*CurTypes.begin()) &= ~OMP_MAP_TARGET_PARAM; |
| 7303 | |
| 7304 | // All other current entries will be MEMBER_OF the combined entry |
| 7305 | // (except for PTR_AND_OBJ entries which do not have a placeholder value |
| 7306 | // 0xFFFF in the MEMBER_OF field). |
| 7307 | OpenMPOffloadMappingFlags MemberOfFlag = |
| 7308 | getMemberOfFlag(BasePointers.size() - 1); |
| 7309 | for (auto &M : CurTypes) |
| 7310 | setCorrectMemberOfFlag(M, MemberOfFlag); |
| 7311 | } |
| 7312 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 7313 | /// Generate all the base pointers, section pointers, sizes and map |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7314 | /// types for the extracted mappable expressions. Also, for each item that |
| 7315 | /// relates with a device pointer, a pair of the relevant declaration and |
| 7316 | /// index where it occurs is appended to the device pointers info array. |
| 7317 | void generateAllInfo(MapBaseValuesArrayTy &BasePointers, |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7318 | MapValuesArrayTy &Pointers, MapValuesArrayTy &Sizes, |
| 7319 | MapFlagsArrayTy &Types) const { |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7320 | // We have to process the component lists that relate with the same |
| 7321 | // declaration in a single chunk so that we can generate the map flags |
| 7322 | // correctly. Therefore, we organize all lists in a map. |
Alexey Bataev | 5d1c3f6 | 2017-06-27 15:46:42 +0000 | [diff] [blame] | 7323 | llvm::MapVector<const ValueDecl *, SmallVector<MapInfo, 8>> Info; |
Samuel Antao | 8d2d730 | 2016-05-26 18:30:22 +0000 | [diff] [blame] | 7324 | |
| 7325 | // Helper function to fill the information map for the different supported |
| 7326 | // clauses. |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7327 | auto &&InfoGen = [&Info]( |
| 7328 | const ValueDecl *D, |
| 7329 | OMPClauseMappableExprCommon::MappableExprComponentListRef L, |
| 7330 | OpenMPMapClauseKind MapType, OpenMPMapClauseKind MapModifier, |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7331 | bool ReturnDevicePointer, bool IsImplicit) { |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7332 | const ValueDecl *VD = |
| 7333 | D ? cast<ValueDecl>(D->getCanonicalDecl()) : nullptr; |
Alexey Bataev | f47c4b4 | 2017-09-26 13:47:31 +0000 | [diff] [blame] | 7334 | Info[VD].emplace_back(L, MapType, MapModifier, ReturnDevicePointer, |
| 7335 | IsImplicit); |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7336 | }; |
Samuel Antao | 8d2d730 | 2016-05-26 18:30:22 +0000 | [diff] [blame] | 7337 | |
Paul Robinson | 78fb132 | 2016-08-01 22:12:46 +0000 | [diff] [blame] | 7338 | // FIXME: MSVC 2013 seems to require this-> to find member CurDir. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 7339 | for (const auto *C : this->CurDir.getClausesOfKind<OMPMapClause>()) |
| 7340 | for (const auto &L : C->component_lists()) { |
Samuel Antao | cf3f83e | 2016-07-28 14:47:35 +0000 | [diff] [blame] | 7341 | InfoGen(L.first, L.second, C->getMapType(), C->getMapTypeModifier(), |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7342 | /*ReturnDevicePointer=*/false, C->isImplicit()); |
Alexey Bataev | f47c4b4 | 2017-09-26 13:47:31 +0000 | [diff] [blame] | 7343 | } |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 7344 | for (const auto *C : this->CurDir.getClausesOfKind<OMPToClause>()) |
| 7345 | for (const auto &L : C->component_lists()) { |
Samuel Antao | cf3f83e | 2016-07-28 14:47:35 +0000 | [diff] [blame] | 7346 | InfoGen(L.first, L.second, OMPC_MAP_to, OMPC_MAP_unknown, |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7347 | /*ReturnDevicePointer=*/false, C->isImplicit()); |
Alexey Bataev | f47c4b4 | 2017-09-26 13:47:31 +0000 | [diff] [blame] | 7348 | } |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 7349 | for (const auto *C : this->CurDir.getClausesOfKind<OMPFromClause>()) |
| 7350 | for (const auto &L : C->component_lists()) { |
Samuel Antao | cf3f83e | 2016-07-28 14:47:35 +0000 | [diff] [blame] | 7351 | InfoGen(L.first, L.second, OMPC_MAP_from, OMPC_MAP_unknown, |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7352 | /*ReturnDevicePointer=*/false, C->isImplicit()); |
Alexey Bataev | f47c4b4 | 2017-09-26 13:47:31 +0000 | [diff] [blame] | 7353 | } |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7354 | |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7355 | // Look at the use_device_ptr clause information and mark the existing map |
| 7356 | // entries as such. If there is no map information for an entry in the |
| 7357 | // 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] | 7358 | // section. It is the user fault if that was not mapped before. If there is |
| 7359 | // no map information and the pointer is a struct member, then we defer the |
| 7360 | // emission of that entry until the whole struct has been processed. |
| 7361 | llvm::MapVector<const ValueDecl *, SmallVector<DeferredDevicePtrEntryTy, 4>> |
| 7362 | DeferredInfo; |
| 7363 | |
Paul Robinson | 78fb132 | 2016-08-01 22:12:46 +0000 | [diff] [blame] | 7364 | // FIXME: MSVC 2013 seems to require this-> to find member CurDir. |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7365 | for (const auto *C : |
| 7366 | this->CurDir.getClausesOfKind<OMPUseDevicePtrClause>()) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 7367 | for (const auto &L : C->component_lists()) { |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7368 | assert(!L.second.empty() && "Not expecting empty list of components!"); |
| 7369 | const ValueDecl *VD = L.second.back().getAssociatedDeclaration(); |
| 7370 | VD = cast<ValueDecl>(VD->getCanonicalDecl()); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 7371 | const Expr *IE = L.second.back().getAssociatedExpression(); |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7372 | // If the first component is a member expression, we have to look into |
| 7373 | // 'this', which maps to null in the map of map information. Otherwise |
| 7374 | // look directly for the information. |
| 7375 | auto It = Info.find(isa<MemberExpr>(IE) ? nullptr : VD); |
| 7376 | |
| 7377 | // We potentially have map information for this declaration already. |
| 7378 | // Look for the first set of components that refer to it. |
| 7379 | if (It != Info.end()) { |
| 7380 | auto CI = std::find_if( |
| 7381 | It->second.begin(), It->second.end(), [VD](const MapInfo &MI) { |
| 7382 | return MI.Components.back().getAssociatedDeclaration() == VD; |
| 7383 | }); |
| 7384 | // If we found a map entry, signal that the pointer has to be returned |
| 7385 | // and move on to the next declaration. |
| 7386 | if (CI != It->second.end()) { |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7387 | CI->ReturnDevicePointer = true; |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7388 | continue; |
| 7389 | } |
| 7390 | } |
| 7391 | |
| 7392 | // 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] | 7393 | // size array section - if the pointer is a struct member we defer this |
| 7394 | // action until the whole struct has been processed. |
Paul Robinson | 78fb132 | 2016-08-01 22:12:46 +0000 | [diff] [blame] | 7395 | // FIXME: MSVC 2013 seems to require this-> to find member CGF. |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7396 | if (isa<MemberExpr>(IE)) { |
| 7397 | // Insert the pointer into Info to be processed by |
| 7398 | // generateInfoForComponentList. Because it is a member pointer |
| 7399 | // without a pointee, no entry will be generated for it, therefore |
| 7400 | // we need to generate one after the whole struct has been processed. |
| 7401 | // Nonetheless, generateInfoForComponentList must be called to take |
| 7402 | // the pointer into account for the calculation of the range of the |
| 7403 | // partial struct. |
| 7404 | InfoGen(nullptr, L.second, OMPC_MAP_unknown, OMPC_MAP_unknown, |
| 7405 | /*ReturnDevicePointer=*/false, C->isImplicit()); |
| 7406 | DeferredInfo[nullptr].emplace_back(IE, VD); |
| 7407 | } else { |
| 7408 | llvm::Value *Ptr = this->CGF.EmitLoadOfScalar( |
| 7409 | this->CGF.EmitLValue(IE), IE->getExprLoc()); |
| 7410 | BasePointers.emplace_back(Ptr, VD); |
| 7411 | Pointers.push_back(Ptr); |
| 7412 | Sizes.push_back(llvm::Constant::getNullValue(this->CGF.SizeTy)); |
| 7413 | Types.push_back(OMP_MAP_RETURN_PARAM | OMP_MAP_TARGET_PARAM); |
| 7414 | } |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7415 | } |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7416 | } |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7417 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 7418 | for (const auto &M : Info) { |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7419 | // We need to know when we generate information for the first component |
| 7420 | // associated with a capture, because the mapping flags depend on it. |
| 7421 | bool IsFirstComponentList = true; |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7422 | |
| 7423 | // Temporary versions of arrays |
| 7424 | MapBaseValuesArrayTy CurBasePointers; |
| 7425 | MapValuesArrayTy CurPointers; |
| 7426 | MapValuesArrayTy CurSizes; |
| 7427 | MapFlagsArrayTy CurTypes; |
| 7428 | StructRangeInfoTy PartialStruct; |
| 7429 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 7430 | for (const MapInfo &L : M.second) { |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7431 | assert(!L.Components.empty() && |
| 7432 | "Not expecting declaration with no component lists."); |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7433 | |
| 7434 | // Remember the current base pointer index. |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7435 | unsigned CurrentBasePointersIdx = CurBasePointers.size(); |
Paul Robinson | 78fb132 | 2016-08-01 22:12:46 +0000 | [diff] [blame] | 7436 | // FIXME: MSVC 2013 seems to require this-> to find the member method. |
Alexey Bataev | f47c4b4 | 2017-09-26 13:47:31 +0000 | [diff] [blame] | 7437 | this->generateInfoForComponentList( |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7438 | L.MapType, L.MapTypeModifier, L.Components, CurBasePointers, |
| 7439 | CurPointers, CurSizes, CurTypes, PartialStruct, |
| 7440 | IsFirstComponentList, L.IsImplicit); |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7441 | |
| 7442 | // If this entry relates with a device pointer, set the relevant |
| 7443 | // declaration and add the 'return pointer' flag. |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7444 | if (L.ReturnDevicePointer) { |
| 7445 | assert(CurBasePointers.size() > CurrentBasePointersIdx && |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7446 | "Unexpected number of mapped base pointers."); |
| 7447 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 7448 | const ValueDecl *RelevantVD = |
| 7449 | L.Components.back().getAssociatedDeclaration(); |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7450 | assert(RelevantVD && |
| 7451 | "No relevant declaration related with device pointer??"); |
| 7452 | |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7453 | CurBasePointers[CurrentBasePointersIdx].setDevicePtrDecl(RelevantVD); |
| 7454 | CurTypes[CurrentBasePointersIdx] |= OMP_MAP_RETURN_PARAM; |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7455 | } |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7456 | IsFirstComponentList = false; |
| 7457 | } |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7458 | |
| 7459 | // Append any pending zero-length pointers which are struct members and |
| 7460 | // used with use_device_ptr. |
| 7461 | auto CI = DeferredInfo.find(M.first); |
| 7462 | if (CI != DeferredInfo.end()) { |
| 7463 | for (const DeferredDevicePtrEntryTy &L : CI->second) { |
| 7464 | llvm::Value *BasePtr = this->CGF.EmitLValue(L.IE).getPointer(); |
| 7465 | llvm::Value *Ptr = this->CGF.EmitLoadOfScalar( |
| 7466 | this->CGF.EmitLValue(L.IE), L.IE->getExprLoc()); |
| 7467 | CurBasePointers.emplace_back(BasePtr, L.VD); |
| 7468 | CurPointers.push_back(Ptr); |
| 7469 | CurSizes.push_back(llvm::Constant::getNullValue(this->CGF.SizeTy)); |
| 7470 | // Entry is PTR_AND_OBJ and RETURN_PARAM. Also, set the placeholder |
| 7471 | // value MEMBER_OF=FFFF so that the entry is later updated with the |
| 7472 | // correct value of MEMBER_OF. |
| 7473 | CurTypes.push_back(OMP_MAP_PTR_AND_OBJ | OMP_MAP_RETURN_PARAM | |
| 7474 | OMP_MAP_MEMBER_OF); |
| 7475 | } |
| 7476 | } |
| 7477 | |
| 7478 | // If there is an entry in PartialStruct it means we have a struct with |
| 7479 | // individual members mapped. Emit an extra combined entry. |
| 7480 | if (PartialStruct.Base.isValid()) |
| 7481 | emitCombinedEntry(BasePointers, Pointers, Sizes, Types, CurTypes, |
| 7482 | PartialStruct); |
| 7483 | |
| 7484 | // We need to append the results of this capture to what we already have. |
| 7485 | BasePointers.append(CurBasePointers.begin(), CurBasePointers.end()); |
| 7486 | Pointers.append(CurPointers.begin(), CurPointers.end()); |
| 7487 | Sizes.append(CurSizes.begin(), CurSizes.end()); |
| 7488 | Types.append(CurTypes.begin(), CurTypes.end()); |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7489 | } |
| 7490 | } |
| 7491 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 7492 | /// Generate the base pointers, section pointers, sizes and map types |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7493 | /// associated to a given capture. |
| 7494 | void generateInfoForCapture(const CapturedStmt::Capture *Cap, |
Samuel Antao | 6890b09 | 2016-07-28 14:25:09 +0000 | [diff] [blame] | 7495 | llvm::Value *Arg, |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7496 | MapBaseValuesArrayTy &BasePointers, |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7497 | MapValuesArrayTy &Pointers, |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7498 | MapValuesArrayTy &Sizes, MapFlagsArrayTy &Types, |
| 7499 | StructRangeInfoTy &PartialStruct) const { |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7500 | assert(!Cap->capturesVariableArrayType() && |
| 7501 | "Not expecting to generate map info for a variable array type!"); |
| 7502 | |
Samuel Antao | 6890b09 | 2016-07-28 14:25:09 +0000 | [diff] [blame] | 7503 | // We need to know when we generating information for the first component |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7504 | const ValueDecl *VD = Cap->capturesThis() |
| 7505 | ? nullptr |
| 7506 | : Cap->getCapturedVar()->getCanonicalDecl(); |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7507 | |
Samuel Antao | 6890b09 | 2016-07-28 14:25:09 +0000 | [diff] [blame] | 7508 | // If this declaration appears in a is_device_ptr clause we just have to |
| 7509 | // 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] | 7510 | // pass its value. |
| 7511 | if (DevPointersMap.count(VD)) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 7512 | BasePointers.emplace_back(Arg, VD); |
Samuel Antao | 6890b09 | 2016-07-28 14:25:09 +0000 | [diff] [blame] | 7513 | Pointers.push_back(Arg); |
| 7514 | Sizes.push_back(CGF.getTypeSize(CGF.getContext().VoidPtrTy)); |
George Rokos | 065755d | 2017-11-07 18:27:04 +0000 | [diff] [blame] | 7515 | Types.push_back(OMP_MAP_LITERAL | OMP_MAP_TARGET_PARAM); |
Samuel Antao | 6890b09 | 2016-07-28 14:25:09 +0000 | [diff] [blame] | 7516 | return; |
| 7517 | } |
| 7518 | |
Alexey Bataev | e82445f | 2018-09-20 13:54:02 +0000 | [diff] [blame^] | 7519 | using MapData = |
| 7520 | std::tuple<OMPClauseMappableExprCommon::MappableExprComponentListRef, |
| 7521 | OpenMPMapClauseKind, OpenMPMapClauseKind, bool>; |
| 7522 | SmallVector<MapData, 4> DeclComponentLists; |
Paul Robinson | 78fb132 | 2016-08-01 22:12:46 +0000 | [diff] [blame] | 7523 | // FIXME: MSVC 2013 seems to require this-> to find member CurDir. |
Alexey Bataev | e82445f | 2018-09-20 13:54:02 +0000 | [diff] [blame^] | 7524 | for (const auto *C : this->CurDir.getClausesOfKind<OMPMapClause>()) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 7525 | for (const auto &L : C->decl_component_lists(VD)) { |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7526 | assert(L.first == VD && |
| 7527 | "We got information for the wrong declaration??"); |
| 7528 | assert(!L.second.empty() && |
| 7529 | "Not expecting declaration with no component lists."); |
Alexey Bataev | e82445f | 2018-09-20 13:54:02 +0000 | [diff] [blame^] | 7530 | DeclComponentLists.emplace_back(L.second, C->getMapType(), |
| 7531 | C->getMapTypeModifier(), |
| 7532 | C->isImplicit()); |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7533 | } |
Alexey Bataev | e82445f | 2018-09-20 13:54:02 +0000 | [diff] [blame^] | 7534 | } |
| 7535 | |
| 7536 | // Find overlapping elements (including the offset from the base element). |
| 7537 | llvm::SmallDenseMap< |
| 7538 | const MapData *, |
| 7539 | llvm::SmallVector< |
| 7540 | OMPClauseMappableExprCommon::MappableExprComponentListRef, 4>, |
| 7541 | 4> |
| 7542 | OverlappedData; |
| 7543 | size_t Count = 0; |
| 7544 | for (const MapData &L : DeclComponentLists) { |
| 7545 | OMPClauseMappableExprCommon::MappableExprComponentListRef Components; |
| 7546 | OpenMPMapClauseKind MapType; |
| 7547 | OpenMPMapClauseKind MapTypeModifier; |
| 7548 | bool IsImplicit; |
| 7549 | std::tie(Components, MapType, MapTypeModifier, IsImplicit) = L; |
| 7550 | ++Count; |
| 7551 | for (const MapData &L1 : makeArrayRef(DeclComponentLists).slice(Count)) { |
| 7552 | OMPClauseMappableExprCommon::MappableExprComponentListRef Components1; |
| 7553 | std::tie(Components1, MapType, MapTypeModifier, IsImplicit) = L1; |
| 7554 | auto CI = Components.rbegin(); |
| 7555 | auto CE = Components.rend(); |
| 7556 | auto SI = Components1.rbegin(); |
| 7557 | auto SE = Components1.rend(); |
| 7558 | for (; CI != CE && SI != SE; ++CI, ++SI) { |
| 7559 | if (CI->getAssociatedExpression()->getStmtClass() != |
| 7560 | SI->getAssociatedExpression()->getStmtClass()) |
| 7561 | break; |
| 7562 | // Are we dealing with different variables/fields? |
| 7563 | if (CI->getAssociatedDeclaration() != SI->getAssociatedDeclaration()) |
| 7564 | break; |
| 7565 | } |
| 7566 | // Found overlapping if, at least for one component, reached the head of |
| 7567 | // the components list. |
| 7568 | if (CI == CE || SI == SE) { |
| 7569 | assert((CI != CE || SI != SE) && |
| 7570 | "Unexpected full match of the mapping components."); |
| 7571 | const MapData &BaseData = CI == CE ? L : L1; |
| 7572 | OMPClauseMappableExprCommon::MappableExprComponentListRef SubData = |
| 7573 | SI == SE ? Components : Components1; |
| 7574 | auto It = CI == CE ? SI : CI; |
| 7575 | auto &OverlappedElements = OverlappedData.FindAndConstruct(&BaseData); |
| 7576 | OverlappedElements.getSecond().push_back(SubData); |
| 7577 | } |
| 7578 | } |
| 7579 | } |
| 7580 | // Sort the overlapped elements for each item. |
| 7581 | llvm::SmallVector<const FieldDecl *, 4> Layout; |
| 7582 | if (!OverlappedData.empty()) { |
| 7583 | if (const auto *CRD = |
| 7584 | VD->getType().getCanonicalType()->getAsCXXRecordDecl()) |
| 7585 | getPlainLayout(CRD, Layout, /*AsBase=*/false); |
| 7586 | else { |
| 7587 | const auto *RD = VD->getType().getCanonicalType()->getAsRecordDecl(); |
| 7588 | Layout.append(RD->field_begin(), RD->field_end()); |
| 7589 | } |
| 7590 | } |
| 7591 | for (auto &Pair : OverlappedData) { |
| 7592 | llvm::sort( |
| 7593 | Pair.getSecond(), |
| 7594 | [&Layout]( |
| 7595 | OMPClauseMappableExprCommon::MappableExprComponentListRef First, |
| 7596 | OMPClauseMappableExprCommon::MappableExprComponentListRef |
| 7597 | Second) { |
| 7598 | auto CI = First.rbegin(); |
| 7599 | auto CE = First.rend(); |
| 7600 | auto SI = Second.rbegin(); |
| 7601 | auto SE = Second.rend(); |
| 7602 | for (; CI != CE && SI != SE; ++CI, ++SI) { |
| 7603 | if (CI->getAssociatedExpression()->getStmtClass() != |
| 7604 | SI->getAssociatedExpression()->getStmtClass()) |
| 7605 | break; |
| 7606 | // Are we dealing with different variables/fields? |
| 7607 | if (CI->getAssociatedDeclaration() != |
| 7608 | SI->getAssociatedDeclaration()) |
| 7609 | break; |
| 7610 | } |
| 7611 | assert(CI != CE && SI != SE && |
| 7612 | "Unexpected end of the map components."); |
| 7613 | const auto *FD1 = cast<FieldDecl>(CI->getAssociatedDeclaration()); |
| 7614 | const auto *FD2 = cast<FieldDecl>(SI->getAssociatedDeclaration()); |
| 7615 | if (FD1->getParent() == FD2->getParent()) |
| 7616 | return FD1->getFieldIndex() < FD2->getFieldIndex(); |
| 7617 | const auto It = |
| 7618 | llvm::find_if(Layout, [FD1, FD2](const FieldDecl *FD) { |
| 7619 | return FD == FD1 || FD == FD2; |
| 7620 | }); |
| 7621 | return *It == FD1; |
| 7622 | }); |
| 7623 | } |
| 7624 | |
| 7625 | // Associated with a capture, because the mapping flags depend on it. |
| 7626 | // Go through all of the elements with the overlapped elements. |
| 7627 | for (const auto &Pair : OverlappedData) { |
| 7628 | const MapData &L = *Pair.getFirst(); |
| 7629 | OMPClauseMappableExprCommon::MappableExprComponentListRef Components; |
| 7630 | OpenMPMapClauseKind MapType; |
| 7631 | OpenMPMapClauseKind MapTypeModifier; |
| 7632 | bool IsImplicit; |
| 7633 | std::tie(Components, MapType, MapTypeModifier, IsImplicit) = L; |
| 7634 | ArrayRef<OMPClauseMappableExprCommon::MappableExprComponentListRef> |
| 7635 | OverlappedComponents = Pair.getSecond(); |
| 7636 | bool IsFirstComponentList = true; |
| 7637 | generateInfoForComponentList(MapType, MapTypeModifier, Components, |
| 7638 | BasePointers, Pointers, Sizes, Types, |
| 7639 | PartialStruct, IsFirstComponentList, |
| 7640 | IsImplicit, OverlappedComponents); |
| 7641 | } |
| 7642 | // Go through other elements without overlapped elements. |
| 7643 | bool IsFirstComponentList = OverlappedData.empty(); |
| 7644 | for (const MapData &L : DeclComponentLists) { |
| 7645 | OMPClauseMappableExprCommon::MappableExprComponentListRef Components; |
| 7646 | OpenMPMapClauseKind MapType; |
| 7647 | OpenMPMapClauseKind MapTypeModifier; |
| 7648 | bool IsImplicit; |
| 7649 | std::tie(Components, MapType, MapTypeModifier, IsImplicit) = L; |
| 7650 | auto It = OverlappedData.find(&L); |
| 7651 | if (It == OverlappedData.end()) |
| 7652 | generateInfoForComponentList(MapType, MapTypeModifier, Components, |
| 7653 | BasePointers, Pointers, Sizes, Types, |
| 7654 | PartialStruct, IsFirstComponentList, |
| 7655 | IsImplicit); |
| 7656 | IsFirstComponentList = false; |
| 7657 | } |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7658 | } |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7659 | |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7660 | /// Generate the base pointers, section pointers, sizes and map types |
| 7661 | /// associated with the declare target link variables. |
| 7662 | void generateInfoForDeclareTargetLink(MapBaseValuesArrayTy &BasePointers, |
| 7663 | MapValuesArrayTy &Pointers, |
| 7664 | MapValuesArrayTy &Sizes, |
| 7665 | MapFlagsArrayTy &Types) const { |
| 7666 | // Map other list items in the map clause which are not captured variables |
| 7667 | // but "declare target link" global variables., |
| 7668 | for (const auto *C : this->CurDir.getClausesOfKind<OMPMapClause>()) { |
| 7669 | for (const auto &L : C->component_lists()) { |
| 7670 | if (!L.first) |
| 7671 | continue; |
| 7672 | const auto *VD = dyn_cast<VarDecl>(L.first); |
| 7673 | if (!VD) |
| 7674 | continue; |
| 7675 | llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res = |
Alexey Bataev | 97b7221 | 2018-08-14 18:31:20 +0000 | [diff] [blame] | 7676 | OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD); |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7677 | if (!Res || *Res != OMPDeclareTargetDeclAttr::MT_Link) |
| 7678 | continue; |
| 7679 | StructRangeInfoTy PartialStruct; |
| 7680 | generateInfoForComponentList( |
| 7681 | C->getMapType(), C->getMapTypeModifier(), L.second, BasePointers, |
| 7682 | Pointers, Sizes, Types, PartialStruct, |
| 7683 | /*IsFirstComponentList=*/true, C->isImplicit()); |
| 7684 | assert(!PartialStruct.Base.isValid() && |
| 7685 | "No partial structs for declare target link expected."); |
| 7686 | } |
| 7687 | } |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7688 | } |
Samuel Antao | d486f84 | 2016-05-26 16:53:38 +0000 | [diff] [blame] | 7689 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 7690 | /// Generate the default map information for a given capture \a CI, |
Samuel Antao | d486f84 | 2016-05-26 16:53:38 +0000 | [diff] [blame] | 7691 | /// record field declaration \a RI and captured value \a CV. |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7692 | void generateDefaultMapInfo(const CapturedStmt::Capture &CI, |
| 7693 | const FieldDecl &RI, llvm::Value *CV, |
| 7694 | MapBaseValuesArrayTy &CurBasePointers, |
| 7695 | MapValuesArrayTy &CurPointers, |
| 7696 | MapValuesArrayTy &CurSizes, |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7697 | MapFlagsArrayTy &CurMapTypes) const { |
Samuel Antao | d486f84 | 2016-05-26 16:53:38 +0000 | [diff] [blame] | 7698 | // Do the default mapping. |
| 7699 | if (CI.capturesThis()) { |
| 7700 | CurBasePointers.push_back(CV); |
| 7701 | CurPointers.push_back(CV); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 7702 | const auto *PtrTy = cast<PointerType>(RI.getType().getTypePtr()); |
Samuel Antao | d486f84 | 2016-05-26 16:53:38 +0000 | [diff] [blame] | 7703 | CurSizes.push_back(CGF.getTypeSize(PtrTy->getPointeeType())); |
| 7704 | // Default map type. |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7705 | CurMapTypes.push_back(OMP_MAP_TO | OMP_MAP_FROM); |
Samuel Antao | d486f84 | 2016-05-26 16:53:38 +0000 | [diff] [blame] | 7706 | } else if (CI.capturesVariableByCopy()) { |
Samuel Antao | 6d00426 | 2016-06-16 18:39:34 +0000 | [diff] [blame] | 7707 | CurBasePointers.push_back(CV); |
| 7708 | CurPointers.push_back(CV); |
Samuel Antao | d486f84 | 2016-05-26 16:53:38 +0000 | [diff] [blame] | 7709 | if (!RI.getType()->isAnyPointerType()) { |
Samuel Antao | 6d00426 | 2016-06-16 18:39:34 +0000 | [diff] [blame] | 7710 | // We have to signal to the runtime captures passed by value that are |
| 7711 | // not pointers. |
George Rokos | 065755d | 2017-11-07 18:27:04 +0000 | [diff] [blame] | 7712 | CurMapTypes.push_back(OMP_MAP_LITERAL); |
Samuel Antao | d486f84 | 2016-05-26 16:53:38 +0000 | [diff] [blame] | 7713 | CurSizes.push_back(CGF.getTypeSize(RI.getType())); |
| 7714 | } else { |
| 7715 | // Pointers are implicitly mapped with a zero size and no flags |
| 7716 | // (other than first map that is added for all implicit maps). |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7717 | CurMapTypes.push_back(OMP_MAP_NONE); |
Samuel Antao | d486f84 | 2016-05-26 16:53:38 +0000 | [diff] [blame] | 7718 | CurSizes.push_back(llvm::Constant::getNullValue(CGF.SizeTy)); |
| 7719 | } |
| 7720 | } else { |
| 7721 | assert(CI.capturesVariable() && "Expected captured reference."); |
| 7722 | CurBasePointers.push_back(CV); |
| 7723 | CurPointers.push_back(CV); |
| 7724 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 7725 | const auto *PtrTy = cast<ReferenceType>(RI.getType().getTypePtr()); |
Samuel Antao | d486f84 | 2016-05-26 16:53:38 +0000 | [diff] [blame] | 7726 | QualType ElementType = PtrTy->getPointeeType(); |
| 7727 | CurSizes.push_back(CGF.getTypeSize(ElementType)); |
| 7728 | // The default map type for a scalar/complex type is 'to' because by |
| 7729 | // default the value doesn't have to be retrieved. For an aggregate |
| 7730 | // type, the default is 'tofrom'. |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7731 | CurMapTypes.push_back(getMapModifiersForPrivateClauses(CI)); |
Samuel Antao | d486f84 | 2016-05-26 16:53:38 +0000 | [diff] [blame] | 7732 | } |
George Rokos | 065755d | 2017-11-07 18:27:04 +0000 | [diff] [blame] | 7733 | // Every default map produces a single argument which is a target parameter. |
| 7734 | CurMapTypes.back() |= OMP_MAP_TARGET_PARAM; |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7735 | |
| 7736 | // Add flag stating this is an implicit map. |
| 7737 | CurMapTypes.back() |= OMP_MAP_IMPLICIT; |
Samuel Antao | d486f84 | 2016-05-26 16:53:38 +0000 | [diff] [blame] | 7738 | } |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7739 | }; |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 7740 | |
| 7741 | enum OpenMPOffloadingReservedDeviceIDs { |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 7742 | /// Device ID if the device was not defined, runtime should get it |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 7743 | /// from environment variables in the spec. |
| 7744 | OMP_DEVICEID_UNDEF = -1, |
| 7745 | }; |
| 7746 | } // anonymous namespace |
| 7747 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 7748 | /// 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] | 7749 | /// offloading runtime library. If there is no map or capture information, |
| 7750 | /// return nullptr by reference. |
| 7751 | static void |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7752 | emitOffloadingArrays(CodeGenFunction &CGF, |
| 7753 | MappableExprsHandler::MapBaseValuesArrayTy &BasePointers, |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 7754 | MappableExprsHandler::MapValuesArrayTy &Pointers, |
| 7755 | MappableExprsHandler::MapValuesArrayTy &Sizes, |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7756 | MappableExprsHandler::MapFlagsArrayTy &MapTypes, |
| 7757 | CGOpenMPRuntime::TargetDataInfo &Info) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 7758 | CodeGenModule &CGM = CGF.CGM; |
| 7759 | ASTContext &Ctx = CGF.getContext(); |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 7760 | |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7761 | // Reset the array information. |
| 7762 | Info.clearArrayInfo(); |
| 7763 | Info.NumberOfPtrs = BasePointers.size(); |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 7764 | |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7765 | if (Info.NumberOfPtrs) { |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 7766 | // Detect if we have any capture size requiring runtime evaluation of the |
| 7767 | // size so that a constant array could be eventually used. |
| 7768 | bool hasRuntimeEvaluationCaptureSize = false; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 7769 | for (llvm::Value *S : Sizes) |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 7770 | if (!isa<llvm::Constant>(S)) { |
| 7771 | hasRuntimeEvaluationCaptureSize = true; |
| 7772 | break; |
| 7773 | } |
| 7774 | |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7775 | llvm::APInt PointerNumAP(32, Info.NumberOfPtrs, /*isSigned=*/true); |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 7776 | QualType PointerArrayType = |
| 7777 | Ctx.getConstantArrayType(Ctx.VoidPtrTy, PointerNumAP, ArrayType::Normal, |
| 7778 | /*IndexTypeQuals=*/0); |
| 7779 | |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7780 | Info.BasePointersArray = |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 7781 | CGF.CreateMemTemp(PointerArrayType, ".offload_baseptrs").getPointer(); |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7782 | Info.PointersArray = |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 7783 | CGF.CreateMemTemp(PointerArrayType, ".offload_ptrs").getPointer(); |
| 7784 | |
| 7785 | // If we don't have any VLA types or other types that require runtime |
| 7786 | // evaluation, we can use a constant array for the map sizes, otherwise we |
| 7787 | // need to fill up the arrays as we do for the pointers. |
| 7788 | if (hasRuntimeEvaluationCaptureSize) { |
| 7789 | QualType SizeArrayType = Ctx.getConstantArrayType( |
| 7790 | Ctx.getSizeType(), PointerNumAP, ArrayType::Normal, |
| 7791 | /*IndexTypeQuals=*/0); |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7792 | Info.SizesArray = |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 7793 | CGF.CreateMemTemp(SizeArrayType, ".offload_sizes").getPointer(); |
| 7794 | } else { |
| 7795 | // We expect all the sizes to be constant, so we collect them to create |
| 7796 | // a constant array. |
| 7797 | SmallVector<llvm::Constant *, 16> ConstSizes; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 7798 | for (llvm::Value *S : Sizes) |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 7799 | ConstSizes.push_back(cast<llvm::Constant>(S)); |
| 7800 | |
| 7801 | auto *SizesArrayInit = llvm::ConstantArray::get( |
| 7802 | llvm::ArrayType::get(CGM.SizeTy, ConstSizes.size()), ConstSizes); |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 7803 | std::string Name = CGM.getOpenMPRuntime().getName({"offload_sizes"}); |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 7804 | auto *SizesArrayGbl = new llvm::GlobalVariable( |
| 7805 | CGM.getModule(), SizesArrayInit->getType(), |
| 7806 | /*isConstant=*/true, llvm::GlobalValue::PrivateLinkage, |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 7807 | SizesArrayInit, Name); |
Peter Collingbourne | bcf909d | 2016-06-14 21:02:05 +0000 | [diff] [blame] | 7808 | SizesArrayGbl->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global); |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7809 | Info.SizesArray = SizesArrayGbl; |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 7810 | } |
| 7811 | |
| 7812 | // The map types are always constant so we don't need to generate code to |
| 7813 | // fill arrays. Instead, we create an array constant. |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7814 | SmallVector<uint64_t, 4> Mapping(MapTypes.size(), 0); |
| 7815 | llvm::copy(MapTypes, Mapping.begin()); |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 7816 | llvm::Constant *MapTypesArrayInit = |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7817 | llvm::ConstantDataArray::get(CGF.Builder.getContext(), Mapping); |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 7818 | std::string MaptypesName = |
| 7819 | CGM.getOpenMPRuntime().getName({"offload_maptypes"}); |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 7820 | auto *MapTypesArrayGbl = new llvm::GlobalVariable( |
| 7821 | CGM.getModule(), MapTypesArrayInit->getType(), |
| 7822 | /*isConstant=*/true, llvm::GlobalValue::PrivateLinkage, |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 7823 | MapTypesArrayInit, MaptypesName); |
Peter Collingbourne | bcf909d | 2016-06-14 21:02:05 +0000 | [diff] [blame] | 7824 | MapTypesArrayGbl->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global); |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7825 | Info.MapTypesArray = MapTypesArrayGbl; |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 7826 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 7827 | for (unsigned I = 0; I < Info.NumberOfPtrs; ++I) { |
| 7828 | llvm::Value *BPVal = *BasePointers[I]; |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 7829 | llvm::Value *BP = CGF.Builder.CreateConstInBoundsGEP2_32( |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7830 | llvm::ArrayType::get(CGM.VoidPtrTy, Info.NumberOfPtrs), |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 7831 | Info.BasePointersArray, 0, I); |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 7832 | BP = CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
| 7833 | BP, BPVal->getType()->getPointerTo(/*AddrSpace=*/0)); |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 7834 | Address BPAddr(BP, Ctx.getTypeAlignInChars(Ctx.VoidPtrTy)); |
| 7835 | CGF.Builder.CreateStore(BPVal, BPAddr); |
| 7836 | |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7837 | if (Info.requiresDevicePointerInfo()) |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 7838 | if (const ValueDecl *DevVD = BasePointers[I].getDevicePtrDecl()) |
Alexey Bataev | 43a919f | 2018-04-13 17:48:43 +0000 | [diff] [blame] | 7839 | Info.CaptureDeviceAddrMap.try_emplace(DevVD, BPAddr); |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7840 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 7841 | llvm::Value *PVal = Pointers[I]; |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 7842 | llvm::Value *P = CGF.Builder.CreateConstInBoundsGEP2_32( |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7843 | llvm::ArrayType::get(CGM.VoidPtrTy, Info.NumberOfPtrs), |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 7844 | Info.PointersArray, 0, I); |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 7845 | P = CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
| 7846 | P, PVal->getType()->getPointerTo(/*AddrSpace=*/0)); |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 7847 | Address PAddr(P, Ctx.getTypeAlignInChars(Ctx.VoidPtrTy)); |
| 7848 | CGF.Builder.CreateStore(PVal, PAddr); |
| 7849 | |
| 7850 | if (hasRuntimeEvaluationCaptureSize) { |
| 7851 | llvm::Value *S = CGF.Builder.CreateConstInBoundsGEP2_32( |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7852 | llvm::ArrayType::get(CGM.SizeTy, Info.NumberOfPtrs), |
| 7853 | Info.SizesArray, |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 7854 | /*Idx0=*/0, |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 7855 | /*Idx1=*/I); |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 7856 | Address SAddr(S, Ctx.getTypeAlignInChars(Ctx.getSizeType())); |
| 7857 | CGF.Builder.CreateStore( |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 7858 | CGF.Builder.CreateIntCast(Sizes[I], CGM.SizeTy, /*isSigned=*/true), |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 7859 | SAddr); |
| 7860 | } |
| 7861 | } |
| 7862 | } |
| 7863 | } |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 7864 | /// 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] | 7865 | /// arrays of pointers, sizes and map types. |
| 7866 | static void emitOffloadingArraysArgument( |
| 7867 | CodeGenFunction &CGF, llvm::Value *&BasePointersArrayArg, |
| 7868 | llvm::Value *&PointersArrayArg, llvm::Value *&SizesArrayArg, |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7869 | llvm::Value *&MapTypesArrayArg, CGOpenMPRuntime::TargetDataInfo &Info) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 7870 | CodeGenModule &CGM = CGF.CGM; |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7871 | if (Info.NumberOfPtrs) { |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 7872 | BasePointersArrayArg = CGF.Builder.CreateConstInBoundsGEP2_32( |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7873 | llvm::ArrayType::get(CGM.VoidPtrTy, Info.NumberOfPtrs), |
| 7874 | Info.BasePointersArray, |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 7875 | /*Idx0=*/0, /*Idx1=*/0); |
| 7876 | PointersArrayArg = CGF.Builder.CreateConstInBoundsGEP2_32( |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7877 | llvm::ArrayType::get(CGM.VoidPtrTy, Info.NumberOfPtrs), |
| 7878 | Info.PointersArray, |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 7879 | /*Idx0=*/0, |
| 7880 | /*Idx1=*/0); |
| 7881 | SizesArrayArg = CGF.Builder.CreateConstInBoundsGEP2_32( |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7882 | llvm::ArrayType::get(CGM.SizeTy, Info.NumberOfPtrs), Info.SizesArray, |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 7883 | /*Idx0=*/0, /*Idx1=*/0); |
| 7884 | MapTypesArrayArg = CGF.Builder.CreateConstInBoundsGEP2_32( |
George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 7885 | llvm::ArrayType::get(CGM.Int64Ty, Info.NumberOfPtrs), |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7886 | Info.MapTypesArray, |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 7887 | /*Idx0=*/0, |
| 7888 | /*Idx1=*/0); |
| 7889 | } else { |
| 7890 | BasePointersArrayArg = llvm::ConstantPointerNull::get(CGM.VoidPtrPtrTy); |
| 7891 | PointersArrayArg = llvm::ConstantPointerNull::get(CGM.VoidPtrPtrTy); |
| 7892 | SizesArrayArg = llvm::ConstantPointerNull::get(CGM.SizeTy->getPointerTo()); |
| 7893 | MapTypesArrayArg = |
George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 7894 | llvm::ConstantPointerNull::get(CGM.Int64Ty->getPointerTo()); |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 7895 | } |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7896 | } |
| 7897 | |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 7898 | void CGOpenMPRuntime::emitTargetCall(CodeGenFunction &CGF, |
| 7899 | const OMPExecutableDirective &D, |
| 7900 | llvm::Value *OutlinedFn, |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 7901 | llvm::Value *OutlinedFnID, |
Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 7902 | const Expr *IfCond, const Expr *Device) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 7903 | if (!CGF.HaveInsertPoint()) |
| 7904 | return; |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 7905 | |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 7906 | assert(OutlinedFn && "Invalid outlined function!"); |
| 7907 | |
Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 7908 | const bool RequiresOuterTask = D.hasClausesOfKind<OMPDependClause>(); |
| 7909 | llvm::SmallVector<llvm::Value *, 16> CapturedVars; |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 7910 | const CapturedStmt &CS = *D.getCapturedStmt(OMPD_target); |
Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 7911 | auto &&ArgsCodegen = [&CS, &CapturedVars](CodeGenFunction &CGF, |
| 7912 | PrePostActionTy &) { |
| 7913 | CGF.GenerateOpenMPCapturedVars(CS, CapturedVars); |
| 7914 | }; |
| 7915 | emitInlinedDirective(CGF, OMPD_unknown, ArgsCodegen); |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7916 | |
Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 7917 | CodeGenFunction::OMPTargetDataInfo InputInfo; |
| 7918 | llvm::Value *MapTypesArray = nullptr; |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 7919 | // Fill up the pointer arrays and transfer execution to the device. |
Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 7920 | auto &&ThenGen = [this, Device, OutlinedFn, OutlinedFnID, &D, &InputInfo, |
| 7921 | &MapTypesArray, &CS, RequiresOuterTask, |
| 7922 | &CapturedVars](CodeGenFunction &CGF, PrePostActionTy &) { |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 7923 | // On top of the arrays that were filled up, the target offloading call |
| 7924 | // takes as arguments the device id as well as the host pointer. The host |
| 7925 | // pointer is used by the runtime library to identify the current target |
| 7926 | // region, so it only has to be unique and not necessarily point to |
| 7927 | // anything. It could be the pointer to the outlined function that |
| 7928 | // implements the target region, but we aren't using that so that the |
| 7929 | // compiler doesn't need to keep that, and could therefore inline the host |
| 7930 | // function if proven worthwhile during optimization. |
| 7931 | |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 7932 | // From this point on, we need to have an ID of the target region defined. |
| 7933 | assert(OutlinedFnID && "Invalid outlined function ID!"); |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 7934 | |
| 7935 | // Emit device ID if any. |
| 7936 | llvm::Value *DeviceID; |
George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 7937 | if (Device) { |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 7938 | DeviceID = CGF.Builder.CreateIntCast(CGF.EmitScalarExpr(Device), |
George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 7939 | CGF.Int64Ty, /*isSigned=*/true); |
| 7940 | } else { |
| 7941 | DeviceID = CGF.Builder.getInt64(OMP_DEVICEID_UNDEF); |
| 7942 | } |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 7943 | |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 7944 | // Emit the number of elements in the offloading arrays. |
Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 7945 | llvm::Value *PointerNum = |
| 7946 | CGF.Builder.getInt32(InputInfo.NumberOfTargetItems); |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 7947 | |
Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 7948 | // Return value of the runtime offloading call. |
| 7949 | llvm::Value *Return; |
| 7950 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 7951 | llvm::Value *NumTeams = emitNumTeamsForTargetDirective(*this, CGF, D); |
| 7952 | llvm::Value *NumThreads = emitNumThreadsForTargetDirective(*this, CGF, D); |
Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 7953 | |
Alexey Bataev | a9f77c6 | 2017-12-13 21:04:20 +0000 | [diff] [blame] | 7954 | bool HasNowait = D.hasClausesOfKind<OMPNowaitClause>(); |
Arpith Chacko Jacob | 33c849a | 2017-01-25 00:57:16 +0000 | [diff] [blame] | 7955 | // The target region is an outlined function launched by the runtime |
| 7956 | // via calls __tgt_target() or __tgt_target_teams(). |
| 7957 | // |
| 7958 | // __tgt_target() launches a target region with one team and one thread, |
| 7959 | // executing a serial region. This master thread may in turn launch |
| 7960 | // more threads within its team upon encountering a parallel region, |
| 7961 | // however, no additional teams can be launched on the device. |
| 7962 | // |
| 7963 | // __tgt_target_teams() launches a target region with one or more teams, |
| 7964 | // each with one or more threads. This call is required for target |
| 7965 | // constructs such as: |
| 7966 | // 'target teams' |
| 7967 | // 'target' / 'teams' |
| 7968 | // 'target teams distribute parallel for' |
| 7969 | // 'target parallel' |
| 7970 | // and so on. |
| 7971 | // |
| 7972 | // Note that on the host and CPU targets, the runtime implementation of |
| 7973 | // these calls simply call the outlined function without forking threads. |
| 7974 | // The outlined functions themselves have runtime calls to |
| 7975 | // __kmpc_fork_teams() and __kmpc_fork() for this purpose, codegen'd by |
| 7976 | // the compiler in emitTeamsCall() and emitParallelCall(). |
| 7977 | // |
| 7978 | // In contrast, on the NVPTX target, the implementation of |
| 7979 | // __tgt_target_teams() launches a GPU kernel with the requested number |
| 7980 | // 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] | 7981 | if (NumTeams) { |
Arpith Chacko Jacob | 33c849a | 2017-01-25 00:57:16 +0000 | [diff] [blame] | 7982 | // If we have NumTeams defined this means that we have an enclosed teams |
| 7983 | // region. Therefore we also expect to have NumThreads defined. These two |
| 7984 | // values should be defined in the presence of a teams directive, |
| 7985 | // regardless of having any clauses associated. If the user is using teams |
| 7986 | // but no clauses, these two values will be the default that should be |
| 7987 | // passed to the runtime library - a 32-bit integer with the value zero. |
| 7988 | assert(NumThreads && "Thread limit expression should be available along " |
| 7989 | "with number of teams."); |
Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 7990 | llvm::Value *OffloadingArgs[] = {DeviceID, |
| 7991 | OutlinedFnID, |
| 7992 | PointerNum, |
| 7993 | InputInfo.BasePointersArray.getPointer(), |
| 7994 | InputInfo.PointersArray.getPointer(), |
| 7995 | InputInfo.SizesArray.getPointer(), |
| 7996 | MapTypesArray, |
| 7997 | NumTeams, |
| 7998 | NumThreads}; |
Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 7999 | Return = CGF.EmitRuntimeCall( |
Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 8000 | createRuntimeFunction(HasNowait ? OMPRTL__tgt_target_teams_nowait |
| 8001 | : OMPRTL__tgt_target_teams), |
Alexey Bataev | a9f77c6 | 2017-12-13 21:04:20 +0000 | [diff] [blame] | 8002 | OffloadingArgs); |
Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 8003 | } else { |
Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 8004 | llvm::Value *OffloadingArgs[] = {DeviceID, |
| 8005 | OutlinedFnID, |
| 8006 | PointerNum, |
| 8007 | InputInfo.BasePointersArray.getPointer(), |
| 8008 | InputInfo.PointersArray.getPointer(), |
| 8009 | InputInfo.SizesArray.getPointer(), |
| 8010 | MapTypesArray}; |
Alexey Bataev | a9f77c6 | 2017-12-13 21:04:20 +0000 | [diff] [blame] | 8011 | Return = CGF.EmitRuntimeCall( |
Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 8012 | createRuntimeFunction(HasNowait ? OMPRTL__tgt_target_nowait |
| 8013 | : OMPRTL__tgt_target), |
Alexey Bataev | a9f77c6 | 2017-12-13 21:04:20 +0000 | [diff] [blame] | 8014 | OffloadingArgs); |
Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 8015 | } |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 8016 | |
Alexey Bataev | 2a007e0 | 2017-10-02 14:20:58 +0000 | [diff] [blame] | 8017 | // Check the error code and execute the host version if required. |
| 8018 | llvm::BasicBlock *OffloadFailedBlock = |
| 8019 | CGF.createBasicBlock("omp_offload.failed"); |
| 8020 | llvm::BasicBlock *OffloadContBlock = |
| 8021 | CGF.createBasicBlock("omp_offload.cont"); |
| 8022 | llvm::Value *Failed = CGF.Builder.CreateIsNotNull(Return); |
| 8023 | CGF.Builder.CreateCondBr(Failed, OffloadFailedBlock, OffloadContBlock); |
| 8024 | |
| 8025 | CGF.EmitBlock(OffloadFailedBlock); |
Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 8026 | if (RequiresOuterTask) { |
| 8027 | CapturedVars.clear(); |
| 8028 | CGF.GenerateOpenMPCapturedVars(CS, CapturedVars); |
| 8029 | } |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 8030 | emitOutlinedFunctionCall(CGF, D.getBeginLoc(), OutlinedFn, CapturedVars); |
Alexey Bataev | 2a007e0 | 2017-10-02 14:20:58 +0000 | [diff] [blame] | 8031 | CGF.EmitBranch(OffloadContBlock); |
| 8032 | |
| 8033 | CGF.EmitBlock(OffloadContBlock, /*IsFinished=*/true); |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 8034 | }; |
| 8035 | |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 8036 | // Notify that the host version must be executed. |
Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 8037 | auto &&ElseGen = [this, &D, OutlinedFn, &CS, &CapturedVars, |
| 8038 | RequiresOuterTask](CodeGenFunction &CGF, |
| 8039 | PrePostActionTy &) { |
| 8040 | if (RequiresOuterTask) { |
| 8041 | CapturedVars.clear(); |
| 8042 | CGF.GenerateOpenMPCapturedVars(CS, CapturedVars); |
| 8043 | } |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 8044 | emitOutlinedFunctionCall(CGF, D.getBeginLoc(), OutlinedFn, CapturedVars); |
Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 8045 | }; |
| 8046 | |
| 8047 | auto &&TargetThenGen = [this, &ThenGen, &D, &InputInfo, &MapTypesArray, |
| 8048 | &CapturedVars, RequiresOuterTask, |
| 8049 | &CS](CodeGenFunction &CGF, PrePostActionTy &) { |
| 8050 | // Fill up the arrays with all the captured variables. |
| 8051 | MappableExprsHandler::MapBaseValuesArrayTy BasePointers; |
| 8052 | MappableExprsHandler::MapValuesArrayTy Pointers; |
| 8053 | MappableExprsHandler::MapValuesArrayTy Sizes; |
| 8054 | MappableExprsHandler::MapFlagsArrayTy MapTypes; |
| 8055 | |
Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 8056 | // Get mappable expression information. |
| 8057 | MappableExprsHandler MEHandler(D, CGF); |
| 8058 | |
| 8059 | auto RI = CS.getCapturedRecordDecl()->field_begin(); |
| 8060 | auto CV = CapturedVars.begin(); |
| 8061 | for (CapturedStmt::const_capture_iterator CI = CS.capture_begin(), |
| 8062 | CE = CS.capture_end(); |
| 8063 | CI != CE; ++CI, ++RI, ++CV) { |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 8064 | MappableExprsHandler::MapBaseValuesArrayTy CurBasePointers; |
| 8065 | MappableExprsHandler::MapValuesArrayTy CurPointers; |
| 8066 | MappableExprsHandler::MapValuesArrayTy CurSizes; |
| 8067 | MappableExprsHandler::MapFlagsArrayTy CurMapTypes; |
| 8068 | MappableExprsHandler::StructRangeInfoTy PartialStruct; |
Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 8069 | |
| 8070 | // VLA sizes are passed to the outlined region by copy and do not have map |
| 8071 | // information associated. |
| 8072 | if (CI->capturesVariableArrayType()) { |
| 8073 | CurBasePointers.push_back(*CV); |
| 8074 | CurPointers.push_back(*CV); |
| 8075 | CurSizes.push_back(CGF.getTypeSize(RI->getType())); |
| 8076 | // Copy to the device as an argument. No need to retrieve it. |
| 8077 | CurMapTypes.push_back(MappableExprsHandler::OMP_MAP_LITERAL | |
| 8078 | MappableExprsHandler::OMP_MAP_TARGET_PARAM); |
| 8079 | } else { |
| 8080 | // If we have any information in the map clause, we use it, otherwise we |
| 8081 | // just do a default mapping. |
| 8082 | MEHandler.generateInfoForCapture(CI, *CV, CurBasePointers, CurPointers, |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 8083 | CurSizes, CurMapTypes, PartialStruct); |
Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 8084 | if (CurBasePointers.empty()) |
| 8085 | MEHandler.generateDefaultMapInfo(*CI, **RI, *CV, CurBasePointers, |
| 8086 | CurPointers, CurSizes, CurMapTypes); |
| 8087 | } |
| 8088 | // We expect to have at least an element of information for this capture. |
| 8089 | assert(!CurBasePointers.empty() && |
| 8090 | "Non-existing map pointer for capture!"); |
| 8091 | assert(CurBasePointers.size() == CurPointers.size() && |
| 8092 | CurBasePointers.size() == CurSizes.size() && |
| 8093 | CurBasePointers.size() == CurMapTypes.size() && |
| 8094 | "Inconsistent map information sizes!"); |
| 8095 | |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 8096 | // If there is an entry in PartialStruct it means we have a struct with |
| 8097 | // individual members mapped. Emit an extra combined entry. |
| 8098 | if (PartialStruct.Base.isValid()) |
| 8099 | MEHandler.emitCombinedEntry(BasePointers, Pointers, Sizes, MapTypes, |
| 8100 | CurMapTypes, PartialStruct); |
| 8101 | |
Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 8102 | // We need to append the results of this capture to what we already have. |
| 8103 | BasePointers.append(CurBasePointers.begin(), CurBasePointers.end()); |
| 8104 | Pointers.append(CurPointers.begin(), CurPointers.end()); |
| 8105 | Sizes.append(CurSizes.begin(), CurSizes.end()); |
| 8106 | MapTypes.append(CurMapTypes.begin(), CurMapTypes.end()); |
| 8107 | } |
Alexey Bataev | 92327c5 | 2018-03-26 16:40:55 +0000 | [diff] [blame] | 8108 | // Map other list items in the map clause which are not captured variables |
| 8109 | // but "declare target link" global variables. |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 8110 | MEHandler.generateInfoForDeclareTargetLink(BasePointers, Pointers, Sizes, |
| 8111 | MapTypes); |
Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 8112 | |
| 8113 | TargetDataInfo Info; |
| 8114 | // Fill up the arrays and create the arguments. |
| 8115 | emitOffloadingArrays(CGF, BasePointers, Pointers, Sizes, MapTypes, Info); |
| 8116 | emitOffloadingArraysArgument(CGF, Info.BasePointersArray, |
| 8117 | Info.PointersArray, Info.SizesArray, |
| 8118 | Info.MapTypesArray, Info); |
| 8119 | InputInfo.NumberOfTargetItems = Info.NumberOfPtrs; |
| 8120 | InputInfo.BasePointersArray = |
| 8121 | Address(Info.BasePointersArray, CGM.getPointerAlign()); |
| 8122 | InputInfo.PointersArray = |
| 8123 | Address(Info.PointersArray, CGM.getPointerAlign()); |
| 8124 | InputInfo.SizesArray = Address(Info.SizesArray, CGM.getPointerAlign()); |
| 8125 | MapTypesArray = Info.MapTypesArray; |
| 8126 | if (RequiresOuterTask) |
| 8127 | CGF.EmitOMPTargetTaskBasedDirective(D, ThenGen, InputInfo); |
| 8128 | else |
| 8129 | emitInlinedDirective(CGF, D.getDirectiveKind(), ThenGen); |
| 8130 | }; |
| 8131 | |
| 8132 | auto &&TargetElseGen = [this, &ElseGen, &D, RequiresOuterTask]( |
| 8133 | CodeGenFunction &CGF, PrePostActionTy &) { |
| 8134 | if (RequiresOuterTask) { |
| 8135 | CodeGenFunction::OMPTargetDataInfo InputInfo; |
| 8136 | CGF.EmitOMPTargetTaskBasedDirective(D, ElseGen, InputInfo); |
| 8137 | } else { |
| 8138 | emitInlinedDirective(CGF, D.getDirectiveKind(), ElseGen); |
| 8139 | } |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 8140 | }; |
| 8141 | |
| 8142 | // If we have a target function ID it means that we need to support |
| 8143 | // offloading, otherwise, just execute on the host. We need to execute on host |
| 8144 | // regardless of the conditional in the if clause if, e.g., the user do not |
| 8145 | // specify target triples. |
| 8146 | if (OutlinedFnID) { |
Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 8147 | if (IfCond) { |
| 8148 | emitOMPIfClause(CGF, IfCond, TargetThenGen, TargetElseGen); |
| 8149 | } else { |
| 8150 | RegionCodeGenTy ThenRCG(TargetThenGen); |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 8151 | ThenRCG(CGF); |
Alexey Bataev | f539faa | 2016-03-28 12:58:34 +0000 | [diff] [blame] | 8152 | } |
| 8153 | } else { |
Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 8154 | RegionCodeGenTy ElseRCG(TargetElseGen); |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 8155 | ElseRCG(CGF); |
Alexey Bataev | f539faa | 2016-03-28 12:58:34 +0000 | [diff] [blame] | 8156 | } |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 8157 | } |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 8158 | |
| 8159 | void CGOpenMPRuntime::scanForTargetRegionsFunctions(const Stmt *S, |
| 8160 | StringRef ParentName) { |
| 8161 | if (!S) |
| 8162 | return; |
| 8163 | |
Arpith Chacko Jacob | 43a8b7b | 2017-01-16 15:26:02 +0000 | [diff] [blame] | 8164 | // Codegen OMP target directives that offload compute to the device. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8165 | bool RequiresDeviceCodegen = |
Arpith Chacko Jacob | 43a8b7b | 2017-01-16 15:26:02 +0000 | [diff] [blame] | 8166 | isa<OMPExecutableDirective>(S) && |
| 8167 | isOpenMPTargetExecutionDirective( |
| 8168 | cast<OMPExecutableDirective>(S)->getDirectiveKind()); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 8169 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8170 | if (RequiresDeviceCodegen) { |
| 8171 | const auto &E = *cast<OMPExecutableDirective>(S); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 8172 | unsigned DeviceID; |
| 8173 | unsigned FileID; |
| 8174 | unsigned Line; |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 8175 | getTargetEntryUniqueInfo(CGM.getContext(), E.getBeginLoc(), DeviceID, |
Samuel Antao | 2de62b0 | 2016-02-13 23:35:10 +0000 | [diff] [blame] | 8176 | FileID, Line); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 8177 | |
| 8178 | // Is this a target region that should not be emitted as an entry point? If |
| 8179 | // so just signal we are done with this target region. |
Samuel Antao | 2de62b0 | 2016-02-13 23:35:10 +0000 | [diff] [blame] | 8180 | if (!OffloadEntriesInfoManager.hasTargetRegionEntryInfo(DeviceID, FileID, |
| 8181 | ParentName, Line)) |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 8182 | return; |
| 8183 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8184 | switch (E.getDirectiveKind()) { |
| 8185 | case OMPD_target: |
| 8186 | CodeGenFunction::EmitOMPTargetDeviceFunction(CGM, ParentName, |
| 8187 | cast<OMPTargetDirective>(E)); |
Arpith Chacko Jacob | 43a8b7b | 2017-01-16 15:26:02 +0000 | [diff] [blame] | 8188 | break; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8189 | case OMPD_target_parallel: |
Arpith Chacko Jacob | 19b911c | 2017-01-18 18:18:53 +0000 | [diff] [blame] | 8190 | CodeGenFunction::EmitOMPTargetParallelDeviceFunction( |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8191 | CGM, ParentName, cast<OMPTargetParallelDirective>(E)); |
Arpith Chacko Jacob | 19b911c | 2017-01-18 18:18:53 +0000 | [diff] [blame] | 8192 | break; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8193 | case OMPD_target_teams: |
Arpith Chacko Jacob | 99a1e0e | 2017-01-25 02:18:43 +0000 | [diff] [blame] | 8194 | CodeGenFunction::EmitOMPTargetTeamsDeviceFunction( |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8195 | CGM, ParentName, cast<OMPTargetTeamsDirective>(E)); |
Arpith Chacko Jacob | 99a1e0e | 2017-01-25 02:18:43 +0000 | [diff] [blame] | 8196 | break; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8197 | case OMPD_target_teams_distribute: |
Alexey Bataev | dfa430f | 2017-12-08 15:03:50 +0000 | [diff] [blame] | 8198 | CodeGenFunction::EmitOMPTargetTeamsDistributeDeviceFunction( |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8199 | CGM, ParentName, cast<OMPTargetTeamsDistributeDirective>(E)); |
Alexey Bataev | dfa430f | 2017-12-08 15:03:50 +0000 | [diff] [blame] | 8200 | break; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8201 | case OMPD_target_teams_distribute_simd: |
Alexey Bataev | fbe17fb | 2017-12-13 19:45:06 +0000 | [diff] [blame] | 8202 | CodeGenFunction::EmitOMPTargetTeamsDistributeSimdDeviceFunction( |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8203 | CGM, ParentName, cast<OMPTargetTeamsDistributeSimdDirective>(E)); |
Alexey Bataev | fbe17fb | 2017-12-13 19:45:06 +0000 | [diff] [blame] | 8204 | break; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8205 | case OMPD_target_parallel_for: |
Alexey Bataev | fb0ebec | 2017-11-08 20:16:14 +0000 | [diff] [blame] | 8206 | CodeGenFunction::EmitOMPTargetParallelForDeviceFunction( |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8207 | CGM, ParentName, cast<OMPTargetParallelForDirective>(E)); |
Alexey Bataev | fb0ebec | 2017-11-08 20:16:14 +0000 | [diff] [blame] | 8208 | break; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8209 | case OMPD_target_parallel_for_simd: |
Alexey Bataev | 5d7edca | 2017-11-09 17:32:15 +0000 | [diff] [blame] | 8210 | CodeGenFunction::EmitOMPTargetParallelForSimdDeviceFunction( |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8211 | CGM, ParentName, cast<OMPTargetParallelForSimdDirective>(E)); |
Alexey Bataev | 5d7edca | 2017-11-09 17:32:15 +0000 | [diff] [blame] | 8212 | break; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8213 | case OMPD_target_simd: |
Alexey Bataev | f836537 | 2017-11-17 17:57:25 +0000 | [diff] [blame] | 8214 | CodeGenFunction::EmitOMPTargetSimdDeviceFunction( |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8215 | CGM, ParentName, cast<OMPTargetSimdDirective>(E)); |
Alexey Bataev | f836537 | 2017-11-17 17:57:25 +0000 | [diff] [blame] | 8216 | break; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8217 | case OMPD_target_teams_distribute_parallel_for: |
Carlo Bertolli | 52978c3 | 2018-01-03 21:12:44 +0000 | [diff] [blame] | 8218 | CodeGenFunction::EmitOMPTargetTeamsDistributeParallelForDeviceFunction( |
| 8219 | CGM, ParentName, |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8220 | cast<OMPTargetTeamsDistributeParallelForDirective>(E)); |
Carlo Bertolli | 52978c3 | 2018-01-03 21:12:44 +0000 | [diff] [blame] | 8221 | break; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8222 | case OMPD_target_teams_distribute_parallel_for_simd: |
Alexey Bataev | 647dd84 | 2018-01-15 20:59:40 +0000 | [diff] [blame] | 8223 | CodeGenFunction:: |
| 8224 | EmitOMPTargetTeamsDistributeParallelForSimdDeviceFunction( |
| 8225 | CGM, ParentName, |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8226 | cast<OMPTargetTeamsDistributeParallelForSimdDirective>(E)); |
Alexey Bataev | 647dd84 | 2018-01-15 20:59:40 +0000 | [diff] [blame] | 8227 | break; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8228 | case OMPD_parallel: |
| 8229 | case OMPD_for: |
| 8230 | case OMPD_parallel_for: |
| 8231 | case OMPD_parallel_sections: |
| 8232 | case OMPD_for_simd: |
| 8233 | case OMPD_parallel_for_simd: |
| 8234 | case OMPD_cancel: |
| 8235 | case OMPD_cancellation_point: |
| 8236 | case OMPD_ordered: |
| 8237 | case OMPD_threadprivate: |
| 8238 | case OMPD_task: |
| 8239 | case OMPD_simd: |
| 8240 | case OMPD_sections: |
| 8241 | case OMPD_section: |
| 8242 | case OMPD_single: |
| 8243 | case OMPD_master: |
| 8244 | case OMPD_critical: |
| 8245 | case OMPD_taskyield: |
| 8246 | case OMPD_barrier: |
| 8247 | case OMPD_taskwait: |
| 8248 | case OMPD_taskgroup: |
| 8249 | case OMPD_atomic: |
| 8250 | case OMPD_flush: |
| 8251 | case OMPD_teams: |
| 8252 | case OMPD_target_data: |
| 8253 | case OMPD_target_exit_data: |
| 8254 | case OMPD_target_enter_data: |
| 8255 | case OMPD_distribute: |
| 8256 | case OMPD_distribute_simd: |
| 8257 | case OMPD_distribute_parallel_for: |
| 8258 | case OMPD_distribute_parallel_for_simd: |
| 8259 | case OMPD_teams_distribute: |
| 8260 | case OMPD_teams_distribute_simd: |
| 8261 | case OMPD_teams_distribute_parallel_for: |
| 8262 | case OMPD_teams_distribute_parallel_for_simd: |
| 8263 | case OMPD_target_update: |
| 8264 | case OMPD_declare_simd: |
| 8265 | case OMPD_declare_target: |
| 8266 | case OMPD_end_declare_target: |
| 8267 | case OMPD_declare_reduction: |
| 8268 | case OMPD_taskloop: |
| 8269 | case OMPD_taskloop_simd: |
| 8270 | case OMPD_unknown: |
Arpith Chacko Jacob | 43a8b7b | 2017-01-16 15:26:02 +0000 | [diff] [blame] | 8271 | llvm_unreachable("Unknown target directive for OpenMP device codegen."); |
| 8272 | } |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 8273 | return; |
| 8274 | } |
| 8275 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8276 | if (const auto *E = dyn_cast<OMPExecutableDirective>(S)) { |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 8277 | if (!E->hasAssociatedStmt() || !E->getAssociatedStmt()) |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 8278 | return; |
| 8279 | |
| 8280 | scanForTargetRegionsFunctions( |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 8281 | E->getInnermostCapturedStmt()->getCapturedStmt(), ParentName); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 8282 | return; |
| 8283 | } |
| 8284 | |
| 8285 | // If this is a lambda function, look into its body. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8286 | if (const auto *L = dyn_cast<LambdaExpr>(S)) |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 8287 | S = L->getBody(); |
| 8288 | |
| 8289 | // Keep looking for target regions recursively. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8290 | for (const Stmt *II : S->children()) |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 8291 | scanForTargetRegionsFunctions(II, ParentName); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 8292 | } |
| 8293 | |
| 8294 | bool CGOpenMPRuntime::emitTargetFunctions(GlobalDecl GD) { |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 8295 | // If emitting code for the host, we do not process FD here. Instead we do |
| 8296 | // the normal code generation. |
| 8297 | if (!CGM.getLangOpts().OpenMPIsDevice) |
| 8298 | return false; |
| 8299 | |
| 8300 | // Try to detect target regions in the function. |
Alexey Bataev | e6aa469 | 2018-09-13 16:54:05 +0000 | [diff] [blame] | 8301 | const ValueDecl *VD = cast<ValueDecl>(GD.getDecl()); |
| 8302 | if (const auto *FD = dyn_cast<FunctionDecl>(VD)) |
| 8303 | scanForTargetRegionsFunctions(FD->getBody(), CGM.getMangledName(GD)); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 8304 | |
Alexey Bataev | 4f4bf7c | 2018-03-15 15:47:20 +0000 | [diff] [blame] | 8305 | // 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] | 8306 | return !OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD) && |
| 8307 | AlreadyEmittedTargetFunctions.count(VD->getCanonicalDecl()) == 0; |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 8308 | } |
| 8309 | |
| 8310 | bool CGOpenMPRuntime::emitTargetGlobalVariable(GlobalDecl GD) { |
| 8311 | if (!CGM.getLangOpts().OpenMPIsDevice) |
| 8312 | return false; |
| 8313 | |
| 8314 | // Check if there are Ctors/Dtors in this declaration and look for target |
| 8315 | // regions in it. We use the complete variant to produce the kernel name |
| 8316 | // mangling. |
| 8317 | QualType RDTy = cast<VarDecl>(GD.getDecl())->getType(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8318 | if (const auto *RD = RDTy->getBaseElementTypeUnsafe()->getAsCXXRecordDecl()) { |
| 8319 | for (const CXXConstructorDecl *Ctor : RD->ctors()) { |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 8320 | StringRef ParentName = |
| 8321 | CGM.getMangledName(GlobalDecl(Ctor, Ctor_Complete)); |
| 8322 | scanForTargetRegionsFunctions(Ctor->getBody(), ParentName); |
| 8323 | } |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8324 | if (const CXXDestructorDecl *Dtor = RD->getDestructor()) { |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 8325 | StringRef ParentName = |
| 8326 | CGM.getMangledName(GlobalDecl(Dtor, Dtor_Complete)); |
| 8327 | scanForTargetRegionsFunctions(Dtor->getBody(), ParentName); |
| 8328 | } |
| 8329 | } |
| 8330 | |
Alexey Bataev | 4f4bf7c | 2018-03-15 15:47:20 +0000 | [diff] [blame] | 8331 | // 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] | 8332 | llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res = |
Alexey Bataev | 97b7221 | 2018-08-14 18:31:20 +0000 | [diff] [blame] | 8333 | OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration( |
| 8334 | cast<VarDecl>(GD.getDecl())); |
Alexey Bataev | bf8fe71 | 2018-08-07 16:14:36 +0000 | [diff] [blame] | 8335 | if (!Res || *Res == OMPDeclareTargetDeclAttr::MT_Link) { |
Alexey Bataev | d01b749 | 2018-08-15 19:45:12 +0000 | [diff] [blame] | 8336 | DeferredGlobalVariables.insert(cast<VarDecl>(GD.getDecl())); |
Alexey Bataev | bf8fe71 | 2018-08-07 16:14:36 +0000 | [diff] [blame] | 8337 | return true; |
| 8338 | } |
| 8339 | return false; |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 8340 | } |
| 8341 | |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 8342 | void CGOpenMPRuntime::registerTargetGlobalVariable(const VarDecl *VD, |
| 8343 | llvm::Constant *Addr) { |
| 8344 | if (llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res = |
Alexey Bataev | 97b7221 | 2018-08-14 18:31:20 +0000 | [diff] [blame] | 8345 | OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD)) { |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 8346 | OffloadEntriesInfoManagerTy::OMPTargetGlobalVarEntryKind Flags; |
| 8347 | StringRef VarName; |
| 8348 | CharUnits VarSize; |
| 8349 | llvm::GlobalValue::LinkageTypes Linkage; |
| 8350 | switch (*Res) { |
| 8351 | case OMPDeclareTargetDeclAttr::MT_To: |
| 8352 | Flags = OffloadEntriesInfoManagerTy::OMPTargetGlobalVarEntryTo; |
| 8353 | VarName = CGM.getMangledName(VD); |
Alexey Bataev | b4dd6d2 | 2018-08-29 20:41:37 +0000 | [diff] [blame] | 8354 | if (VD->hasDefinition(CGM.getContext()) != VarDecl::DeclarationOnly) { |
| 8355 | VarSize = CGM.getContext().getTypeSizeInChars(VD->getType()); |
| 8356 | assert(!VarSize.isZero() && "Expected non-zero size of the variable"); |
| 8357 | } else { |
| 8358 | VarSize = CharUnits::Zero(); |
| 8359 | } |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 8360 | Linkage = CGM.getLLVMLinkageVarDefinition(VD, /*IsConstant=*/false); |
Alexey Bataev | 3823514 | 2018-07-31 16:40:15 +0000 | [diff] [blame] | 8361 | // Temp solution to prevent optimizations of the internal variables. |
| 8362 | if (CGM.getLangOpts().OpenMPIsDevice && !VD->isExternallyVisible()) { |
| 8363 | std::string RefName = getName({VarName, "ref"}); |
| 8364 | if (!CGM.GetGlobalValue(RefName)) { |
| 8365 | llvm::Constant *AddrRef = |
| 8366 | getOrCreateInternalVariable(Addr->getType(), RefName); |
| 8367 | auto *GVAddrRef = cast<llvm::GlobalVariable>(AddrRef); |
| 8368 | GVAddrRef->setConstant(/*Val=*/true); |
| 8369 | GVAddrRef->setLinkage(llvm::GlobalValue::InternalLinkage); |
| 8370 | GVAddrRef->setInitializer(Addr); |
| 8371 | CGM.addCompilerUsedGlobal(GVAddrRef); |
| 8372 | } |
| 8373 | } |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 8374 | break; |
| 8375 | case OMPDeclareTargetDeclAttr::MT_Link: |
Alexey Bataev | c52f01d | 2018-07-16 20:05:25 +0000 | [diff] [blame] | 8376 | Flags = OffloadEntriesInfoManagerTy::OMPTargetGlobalVarEntryLink; |
| 8377 | if (CGM.getLangOpts().OpenMPIsDevice) { |
| 8378 | VarName = Addr->getName(); |
| 8379 | Addr = nullptr; |
| 8380 | } else { |
| 8381 | VarName = getAddrOfDeclareTargetLink(VD).getName(); |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 8382 | Addr = |
| 8383 | cast<llvm::Constant>(getAddrOfDeclareTargetLink(VD).getPointer()); |
| 8384 | } |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 8385 | VarSize = CGM.getPointerSize(); |
| 8386 | Linkage = llvm::GlobalValue::WeakAnyLinkage; |
| 8387 | break; |
| 8388 | } |
| 8389 | OffloadEntriesInfoManager.registerDeviceGlobalVarEntryInfo( |
| 8390 | VarName, Addr, VarSize, Flags, Linkage); |
| 8391 | } |
| 8392 | } |
| 8393 | |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 8394 | bool CGOpenMPRuntime::emitTargetGlobal(GlobalDecl GD) { |
Alexey Bataev | e6aa469 | 2018-09-13 16:54:05 +0000 | [diff] [blame] | 8395 | if (isa<FunctionDecl>(GD.getDecl()) || |
| 8396 | isa<OMPDeclareReductionDecl>(GD.getDecl())) |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 8397 | return emitTargetFunctions(GD); |
| 8398 | |
| 8399 | return emitTargetGlobalVariable(GD); |
| 8400 | } |
| 8401 | |
Alexey Bataev | bf8fe71 | 2018-08-07 16:14:36 +0000 | [diff] [blame] | 8402 | void CGOpenMPRuntime::emitDeferredTargetDecls() const { |
| 8403 | for (const VarDecl *VD : DeferredGlobalVariables) { |
| 8404 | llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res = |
Alexey Bataev | 97b7221 | 2018-08-14 18:31:20 +0000 | [diff] [blame] | 8405 | OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD); |
Alexey Bataev | d01b749 | 2018-08-15 19:45:12 +0000 | [diff] [blame] | 8406 | if (!Res) |
| 8407 | continue; |
| 8408 | if (*Res == OMPDeclareTargetDeclAttr::MT_To) { |
Alexey Bataev | bf8fe71 | 2018-08-07 16:14:36 +0000 | [diff] [blame] | 8409 | CGM.EmitGlobal(VD); |
Alexey Bataev | d01b749 | 2018-08-15 19:45:12 +0000 | [diff] [blame] | 8410 | } else { |
| 8411 | assert(*Res == OMPDeclareTargetDeclAttr::MT_Link && |
| 8412 | "Expected to or link clauses."); |
| 8413 | (void)CGM.getOpenMPRuntime().getAddrOfDeclareTargetLink(VD); |
Alexey Bataev | bf8fe71 | 2018-08-07 16:14:36 +0000 | [diff] [blame] | 8414 | } |
| 8415 | } |
| 8416 | } |
| 8417 | |
Alexey Bataev | 4f4bf7c | 2018-03-15 15:47:20 +0000 | [diff] [blame] | 8418 | CGOpenMPRuntime::DisableAutoDeclareTargetRAII::DisableAutoDeclareTargetRAII( |
| 8419 | CodeGenModule &CGM) |
| 8420 | : CGM(CGM) { |
| 8421 | if (CGM.getLangOpts().OpenMPIsDevice) { |
| 8422 | SavedShouldMarkAsGlobal = CGM.getOpenMPRuntime().ShouldMarkAsGlobal; |
| 8423 | CGM.getOpenMPRuntime().ShouldMarkAsGlobal = false; |
| 8424 | } |
| 8425 | } |
| 8426 | |
| 8427 | CGOpenMPRuntime::DisableAutoDeclareTargetRAII::~DisableAutoDeclareTargetRAII() { |
| 8428 | if (CGM.getLangOpts().OpenMPIsDevice) |
| 8429 | CGM.getOpenMPRuntime().ShouldMarkAsGlobal = SavedShouldMarkAsGlobal; |
| 8430 | } |
| 8431 | |
Alexey Bataev | 6d94410 | 2018-05-02 15:45:28 +0000 | [diff] [blame] | 8432 | bool CGOpenMPRuntime::markAsGlobalTarget(GlobalDecl GD) { |
Alexey Bataev | 4f4bf7c | 2018-03-15 15:47:20 +0000 | [diff] [blame] | 8433 | if (!CGM.getLangOpts().OpenMPIsDevice || !ShouldMarkAsGlobal) |
| 8434 | return true; |
Alexey Bataev | 4f4bf7c | 2018-03-15 15:47:20 +0000 | [diff] [blame] | 8435 | |
Alexey Bataev | 6d94410 | 2018-05-02 15:45:28 +0000 | [diff] [blame] | 8436 | const auto *D = cast<FunctionDecl>(GD.getDecl()); |
Alexey Bataev | 4f4bf7c | 2018-03-15 15:47:20 +0000 | [diff] [blame] | 8437 | const FunctionDecl *FD = D->getCanonicalDecl(); |
Alexey Bataev | 34f8a70 | 2018-03-28 14:28:54 +0000 | [diff] [blame] | 8438 | // Do not to emit function if it is marked as declare target as it was already |
| 8439 | // emitted. |
Alexey Bataev | 97b7221 | 2018-08-14 18:31:20 +0000 | [diff] [blame] | 8440 | if (OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(D)) { |
Alexey Bataev | 34f8a70 | 2018-03-28 14:28:54 +0000 | [diff] [blame] | 8441 | if (D->hasBody() && AlreadyEmittedTargetFunctions.count(FD) == 0) { |
| 8442 | if (auto *F = dyn_cast_or_null<llvm::Function>( |
Alexey Bataev | 6d94410 | 2018-05-02 15:45:28 +0000 | [diff] [blame] | 8443 | CGM.GetGlobalValue(CGM.getMangledName(GD)))) |
Alexey Bataev | 34f8a70 | 2018-03-28 14:28:54 +0000 | [diff] [blame] | 8444 | return !F->isDeclaration(); |
| 8445 | return false; |
| 8446 | } |
| 8447 | return true; |
| 8448 | } |
| 8449 | |
Alexey Bataev | 4f4bf7c | 2018-03-15 15:47:20 +0000 | [diff] [blame] | 8450 | return !AlreadyEmittedTargetFunctions.insert(FD).second; |
| 8451 | } |
| 8452 | |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 8453 | llvm::Function *CGOpenMPRuntime::emitRegistrationFunction() { |
| 8454 | // If we have offloading in the current module, we need to emit the entries |
| 8455 | // now and register the offloading descriptor. |
| 8456 | createOffloadEntriesAndInfoMetadata(); |
| 8457 | |
| 8458 | // Create and register the offloading binary descriptors. This is the main |
| 8459 | // entity that captures all the information about offloading in the current |
| 8460 | // compilation unit. |
| 8461 | return createOffloadingBinaryDescriptorRegistration(); |
| 8462 | } |
Carlo Bertolli | 430d8ec | 2016-03-03 20:34:23 +0000 | [diff] [blame] | 8463 | |
| 8464 | void CGOpenMPRuntime::emitTeamsCall(CodeGenFunction &CGF, |
| 8465 | const OMPExecutableDirective &D, |
| 8466 | SourceLocation Loc, |
| 8467 | llvm::Value *OutlinedFn, |
| 8468 | ArrayRef<llvm::Value *> CapturedVars) { |
| 8469 | if (!CGF.HaveInsertPoint()) |
| 8470 | return; |
| 8471 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8472 | llvm::Value *RTLoc = emitUpdateLocation(CGF, Loc); |
Carlo Bertolli | 430d8ec | 2016-03-03 20:34:23 +0000 | [diff] [blame] | 8473 | CodeGenFunction::RunCleanupsScope Scope(CGF); |
| 8474 | |
| 8475 | // Build call __kmpc_fork_teams(loc, n, microtask, var1, .., varn); |
| 8476 | llvm::Value *Args[] = { |
| 8477 | RTLoc, |
| 8478 | CGF.Builder.getInt32(CapturedVars.size()), // Number of captured vars |
| 8479 | CGF.Builder.CreateBitCast(OutlinedFn, getKmpc_MicroPointerTy())}; |
| 8480 | llvm::SmallVector<llvm::Value *, 16> RealArgs; |
| 8481 | RealArgs.append(std::begin(Args), std::end(Args)); |
| 8482 | RealArgs.append(CapturedVars.begin(), CapturedVars.end()); |
| 8483 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8484 | llvm::Value *RTLFn = createRuntimeFunction(OMPRTL__kmpc_fork_teams); |
Carlo Bertolli | 430d8ec | 2016-03-03 20:34:23 +0000 | [diff] [blame] | 8485 | CGF.EmitRuntimeCall(RTLFn, RealArgs); |
| 8486 | } |
| 8487 | |
| 8488 | void CGOpenMPRuntime::emitNumTeamsClause(CodeGenFunction &CGF, |
Carlo Bertolli | c687225 | 2016-04-04 15:55:02 +0000 | [diff] [blame] | 8489 | const Expr *NumTeams, |
| 8490 | const Expr *ThreadLimit, |
Carlo Bertolli | 430d8ec | 2016-03-03 20:34:23 +0000 | [diff] [blame] | 8491 | SourceLocation Loc) { |
| 8492 | if (!CGF.HaveInsertPoint()) |
| 8493 | return; |
| 8494 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8495 | llvm::Value *RTLoc = emitUpdateLocation(CGF, Loc); |
Carlo Bertolli | 430d8ec | 2016-03-03 20:34:23 +0000 | [diff] [blame] | 8496 | |
Carlo Bertolli | c687225 | 2016-04-04 15:55:02 +0000 | [diff] [blame] | 8497 | llvm::Value *NumTeamsVal = |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8498 | NumTeams |
Carlo Bertolli | c687225 | 2016-04-04 15:55:02 +0000 | [diff] [blame] | 8499 | ? CGF.Builder.CreateIntCast(CGF.EmitScalarExpr(NumTeams), |
| 8500 | CGF.CGM.Int32Ty, /* isSigned = */ true) |
| 8501 | : CGF.Builder.getInt32(0); |
| 8502 | |
| 8503 | llvm::Value *ThreadLimitVal = |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8504 | ThreadLimit |
Carlo Bertolli | c687225 | 2016-04-04 15:55:02 +0000 | [diff] [blame] | 8505 | ? CGF.Builder.CreateIntCast(CGF.EmitScalarExpr(ThreadLimit), |
| 8506 | CGF.CGM.Int32Ty, /* isSigned = */ true) |
| 8507 | : CGF.Builder.getInt32(0); |
| 8508 | |
Carlo Bertolli | 430d8ec | 2016-03-03 20:34:23 +0000 | [diff] [blame] | 8509 | // 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] | 8510 | llvm::Value *PushNumTeamsArgs[] = {RTLoc, getThreadID(CGF, Loc), NumTeamsVal, |
| 8511 | ThreadLimitVal}; |
Carlo Bertolli | 430d8ec | 2016-03-03 20:34:23 +0000 | [diff] [blame] | 8512 | CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_push_num_teams), |
| 8513 | PushNumTeamsArgs); |
| 8514 | } |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8515 | |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 8516 | void CGOpenMPRuntime::emitTargetDataCalls( |
| 8517 | CodeGenFunction &CGF, const OMPExecutableDirective &D, const Expr *IfCond, |
| 8518 | const Expr *Device, const RegionCodeGenTy &CodeGen, TargetDataInfo &Info) { |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8519 | if (!CGF.HaveInsertPoint()) |
| 8520 | return; |
| 8521 | |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 8522 | // Action used to replace the default codegen action and turn privatization |
| 8523 | // off. |
| 8524 | PrePostActionTy NoPrivAction; |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8525 | |
| 8526 | // Generate the code for the opening of the data environment. Capture all the |
| 8527 | // arguments of the runtime call by reference because they are used in the |
| 8528 | // closing of the region. |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 8529 | auto &&BeginThenGen = [this, &D, Device, &Info, |
| 8530 | &CodeGen](CodeGenFunction &CGF, PrePostActionTy &) { |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8531 | // Fill up the arrays with all the mapped variables. |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 8532 | MappableExprsHandler::MapBaseValuesArrayTy BasePointers; |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8533 | MappableExprsHandler::MapValuesArrayTy Pointers; |
| 8534 | MappableExprsHandler::MapValuesArrayTy Sizes; |
| 8535 | MappableExprsHandler::MapFlagsArrayTy MapTypes; |
| 8536 | |
| 8537 | // Get map clause information. |
| 8538 | MappableExprsHandler MCHandler(D, CGF); |
| 8539 | MCHandler.generateAllInfo(BasePointers, Pointers, Sizes, MapTypes); |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8540 | |
| 8541 | // Fill up the arrays and create the arguments. |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 8542 | emitOffloadingArrays(CGF, BasePointers, Pointers, Sizes, MapTypes, Info); |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8543 | |
| 8544 | llvm::Value *BasePointersArrayArg = nullptr; |
| 8545 | llvm::Value *PointersArrayArg = nullptr; |
| 8546 | llvm::Value *SizesArrayArg = nullptr; |
| 8547 | llvm::Value *MapTypesArrayArg = nullptr; |
| 8548 | emitOffloadingArraysArgument(CGF, BasePointersArrayArg, PointersArrayArg, |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 8549 | SizesArrayArg, MapTypesArrayArg, Info); |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8550 | |
| 8551 | // Emit device ID if any. |
| 8552 | llvm::Value *DeviceID = nullptr; |
George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 8553 | if (Device) { |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8554 | DeviceID = CGF.Builder.CreateIntCast(CGF.EmitScalarExpr(Device), |
George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 8555 | CGF.Int64Ty, /*isSigned=*/true); |
| 8556 | } else { |
| 8557 | DeviceID = CGF.Builder.getInt64(OMP_DEVICEID_UNDEF); |
| 8558 | } |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8559 | |
| 8560 | // Emit the number of elements in the offloading arrays. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8561 | llvm::Value *PointerNum = CGF.Builder.getInt32(Info.NumberOfPtrs); |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8562 | |
| 8563 | llvm::Value *OffloadingArgs[] = { |
| 8564 | DeviceID, PointerNum, BasePointersArrayArg, |
| 8565 | PointersArrayArg, SizesArrayArg, MapTypesArrayArg}; |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 8566 | CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__tgt_target_data_begin), |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8567 | OffloadingArgs); |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 8568 | |
| 8569 | // If device pointer privatization is required, emit the body of the region |
| 8570 | // here. It will have to be duplicated: with and without privatization. |
| 8571 | if (!Info.CaptureDeviceAddrMap.empty()) |
| 8572 | CodeGen(CGF); |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8573 | }; |
| 8574 | |
| 8575 | // Generate code for the closing of the data region. |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 8576 | auto &&EndThenGen = [this, Device, &Info](CodeGenFunction &CGF, |
| 8577 | PrePostActionTy &) { |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 8578 | assert(Info.isValid() && "Invalid data environment closing arguments."); |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8579 | |
| 8580 | llvm::Value *BasePointersArrayArg = nullptr; |
| 8581 | llvm::Value *PointersArrayArg = nullptr; |
| 8582 | llvm::Value *SizesArrayArg = nullptr; |
| 8583 | llvm::Value *MapTypesArrayArg = nullptr; |
| 8584 | emitOffloadingArraysArgument(CGF, BasePointersArrayArg, PointersArrayArg, |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 8585 | SizesArrayArg, MapTypesArrayArg, Info); |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8586 | |
| 8587 | // Emit device ID if any. |
| 8588 | llvm::Value *DeviceID = nullptr; |
George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 8589 | if (Device) { |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8590 | DeviceID = CGF.Builder.CreateIntCast(CGF.EmitScalarExpr(Device), |
George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 8591 | CGF.Int64Ty, /*isSigned=*/true); |
| 8592 | } else { |
| 8593 | DeviceID = CGF.Builder.getInt64(OMP_DEVICEID_UNDEF); |
| 8594 | } |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8595 | |
| 8596 | // Emit the number of elements in the offloading arrays. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8597 | llvm::Value *PointerNum = CGF.Builder.getInt32(Info.NumberOfPtrs); |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8598 | |
| 8599 | llvm::Value *OffloadingArgs[] = { |
| 8600 | DeviceID, PointerNum, BasePointersArrayArg, |
| 8601 | PointersArrayArg, SizesArrayArg, MapTypesArrayArg}; |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 8602 | CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__tgt_target_data_end), |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8603 | OffloadingArgs); |
| 8604 | }; |
| 8605 | |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 8606 | // If we need device pointer privatization, we need to emit the body of the |
| 8607 | // region with no privatization in the 'else' branch of the conditional. |
| 8608 | // Otherwise, we don't have to do anything. |
| 8609 | auto &&BeginElseGen = [&Info, &CodeGen, &NoPrivAction](CodeGenFunction &CGF, |
| 8610 | PrePostActionTy &) { |
| 8611 | if (!Info.CaptureDeviceAddrMap.empty()) { |
| 8612 | CodeGen.setAction(NoPrivAction); |
| 8613 | CodeGen(CGF); |
| 8614 | } |
| 8615 | }; |
| 8616 | |
| 8617 | // We don't have to do anything to close the region if the if clause evaluates |
| 8618 | // to false. |
| 8619 | auto &&EndElseGen = [](CodeGenFunction &CGF, PrePostActionTy &) {}; |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8620 | |
| 8621 | if (IfCond) { |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 8622 | emitOMPIfClause(CGF, IfCond, BeginThenGen, BeginElseGen); |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8623 | } else { |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 8624 | RegionCodeGenTy RCG(BeginThenGen); |
| 8625 | RCG(CGF); |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8626 | } |
| 8627 | |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 8628 | // If we don't require privatization of device pointers, we emit the body in |
| 8629 | // between the runtime calls. This avoids duplicating the body code. |
| 8630 | if (Info.CaptureDeviceAddrMap.empty()) { |
| 8631 | CodeGen.setAction(NoPrivAction); |
| 8632 | CodeGen(CGF); |
| 8633 | } |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8634 | |
| 8635 | if (IfCond) { |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 8636 | emitOMPIfClause(CGF, IfCond, EndThenGen, EndElseGen); |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8637 | } else { |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 8638 | RegionCodeGenTy RCG(EndThenGen); |
| 8639 | RCG(CGF); |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8640 | } |
| 8641 | } |
Samuel Antao | bd0ae2e | 2016-04-27 23:07:29 +0000 | [diff] [blame] | 8642 | |
Samuel Antao | 8d2d730 | 2016-05-26 18:30:22 +0000 | [diff] [blame] | 8643 | void CGOpenMPRuntime::emitTargetDataStandAloneCall( |
Samuel Antao | 8dd6628 | 2016-04-27 23:14:30 +0000 | [diff] [blame] | 8644 | CodeGenFunction &CGF, const OMPExecutableDirective &D, const Expr *IfCond, |
| 8645 | const Expr *Device) { |
Samuel Antao | bd0ae2e | 2016-04-27 23:07:29 +0000 | [diff] [blame] | 8646 | if (!CGF.HaveInsertPoint()) |
| 8647 | return; |
| 8648 | |
Samuel Antao | 8dd6628 | 2016-04-27 23:14:30 +0000 | [diff] [blame] | 8649 | assert((isa<OMPTargetEnterDataDirective>(D) || |
Samuel Antao | 8d2d730 | 2016-05-26 18:30:22 +0000 | [diff] [blame] | 8650 | isa<OMPTargetExitDataDirective>(D) || |
| 8651 | isa<OMPTargetUpdateDirective>(D)) && |
| 8652 | "Expecting either target enter, exit data, or update directives."); |
Samuel Antao | 8dd6628 | 2016-04-27 23:14:30 +0000 | [diff] [blame] | 8653 | |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 8654 | CodeGenFunction::OMPTargetDataInfo InputInfo; |
| 8655 | llvm::Value *MapTypesArray = nullptr; |
Samuel Antao | bd0ae2e | 2016-04-27 23:07:29 +0000 | [diff] [blame] | 8656 | // Generate the code for the opening of the data environment. |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 8657 | auto &&ThenGen = [this, &D, Device, &InputInfo, |
| 8658 | &MapTypesArray](CodeGenFunction &CGF, PrePostActionTy &) { |
Samuel Antao | bd0ae2e | 2016-04-27 23:07:29 +0000 | [diff] [blame] | 8659 | // Emit device ID if any. |
| 8660 | llvm::Value *DeviceID = nullptr; |
George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 8661 | if (Device) { |
Samuel Antao | bd0ae2e | 2016-04-27 23:07:29 +0000 | [diff] [blame] | 8662 | DeviceID = CGF.Builder.CreateIntCast(CGF.EmitScalarExpr(Device), |
George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 8663 | CGF.Int64Ty, /*isSigned=*/true); |
| 8664 | } else { |
| 8665 | DeviceID = CGF.Builder.getInt64(OMP_DEVICEID_UNDEF); |
| 8666 | } |
Samuel Antao | bd0ae2e | 2016-04-27 23:07:29 +0000 | [diff] [blame] | 8667 | |
| 8668 | // Emit the number of elements in the offloading arrays. |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 8669 | llvm::Constant *PointerNum = |
| 8670 | CGF.Builder.getInt32(InputInfo.NumberOfTargetItems); |
Samuel Antao | bd0ae2e | 2016-04-27 23:07:29 +0000 | [diff] [blame] | 8671 | |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 8672 | llvm::Value *OffloadingArgs[] = {DeviceID, |
| 8673 | PointerNum, |
| 8674 | InputInfo.BasePointersArray.getPointer(), |
| 8675 | InputInfo.PointersArray.getPointer(), |
| 8676 | InputInfo.SizesArray.getPointer(), |
| 8677 | MapTypesArray}; |
Samuel Antao | 8d2d730 | 2016-05-26 18:30:22 +0000 | [diff] [blame] | 8678 | |
Samuel Antao | 8d2d730 | 2016-05-26 18:30:22 +0000 | [diff] [blame] | 8679 | // Select the right runtime function call for each expected standalone |
| 8680 | // directive. |
Alexey Bataev | 0cc6b8e | 2017-12-14 17:00:17 +0000 | [diff] [blame] | 8681 | const bool HasNowait = D.hasClausesOfKind<OMPNowaitClause>(); |
Samuel Antao | 8d2d730 | 2016-05-26 18:30:22 +0000 | [diff] [blame] | 8682 | OpenMPRTLFunction RTLFn; |
| 8683 | switch (D.getDirectiveKind()) { |
Samuel Antao | 8d2d730 | 2016-05-26 18:30:22 +0000 | [diff] [blame] | 8684 | case OMPD_target_enter_data: |
Alexey Bataev | 0cc6b8e | 2017-12-14 17:00:17 +0000 | [diff] [blame] | 8685 | RTLFn = HasNowait ? OMPRTL__tgt_target_data_begin_nowait |
| 8686 | : OMPRTL__tgt_target_data_begin; |
Samuel Antao | 8d2d730 | 2016-05-26 18:30:22 +0000 | [diff] [blame] | 8687 | break; |
| 8688 | case OMPD_target_exit_data: |
Alexey Bataev | 0cc6b8e | 2017-12-14 17:00:17 +0000 | [diff] [blame] | 8689 | RTLFn = HasNowait ? OMPRTL__tgt_target_data_end_nowait |
| 8690 | : OMPRTL__tgt_target_data_end; |
Samuel Antao | 8d2d730 | 2016-05-26 18:30:22 +0000 | [diff] [blame] | 8691 | break; |
| 8692 | case OMPD_target_update: |
Alexey Bataev | 0cc6b8e | 2017-12-14 17:00:17 +0000 | [diff] [blame] | 8693 | RTLFn = HasNowait ? OMPRTL__tgt_target_data_update_nowait |
| 8694 | : OMPRTL__tgt_target_data_update; |
Samuel Antao | 8d2d730 | 2016-05-26 18:30:22 +0000 | [diff] [blame] | 8695 | break; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8696 | case OMPD_parallel: |
| 8697 | case OMPD_for: |
| 8698 | case OMPD_parallel_for: |
| 8699 | case OMPD_parallel_sections: |
| 8700 | case OMPD_for_simd: |
| 8701 | case OMPD_parallel_for_simd: |
| 8702 | case OMPD_cancel: |
| 8703 | case OMPD_cancellation_point: |
| 8704 | case OMPD_ordered: |
| 8705 | case OMPD_threadprivate: |
| 8706 | case OMPD_task: |
| 8707 | case OMPD_simd: |
| 8708 | case OMPD_sections: |
| 8709 | case OMPD_section: |
| 8710 | case OMPD_single: |
| 8711 | case OMPD_master: |
| 8712 | case OMPD_critical: |
| 8713 | case OMPD_taskyield: |
| 8714 | case OMPD_barrier: |
| 8715 | case OMPD_taskwait: |
| 8716 | case OMPD_taskgroup: |
| 8717 | case OMPD_atomic: |
| 8718 | case OMPD_flush: |
| 8719 | case OMPD_teams: |
| 8720 | case OMPD_target_data: |
| 8721 | case OMPD_distribute: |
| 8722 | case OMPD_distribute_simd: |
| 8723 | case OMPD_distribute_parallel_for: |
| 8724 | case OMPD_distribute_parallel_for_simd: |
| 8725 | case OMPD_teams_distribute: |
| 8726 | case OMPD_teams_distribute_simd: |
| 8727 | case OMPD_teams_distribute_parallel_for: |
| 8728 | case OMPD_teams_distribute_parallel_for_simd: |
| 8729 | case OMPD_declare_simd: |
| 8730 | case OMPD_declare_target: |
| 8731 | case OMPD_end_declare_target: |
| 8732 | case OMPD_declare_reduction: |
| 8733 | case OMPD_taskloop: |
| 8734 | case OMPD_taskloop_simd: |
| 8735 | case OMPD_target: |
| 8736 | case OMPD_target_simd: |
| 8737 | case OMPD_target_teams_distribute: |
| 8738 | case OMPD_target_teams_distribute_simd: |
| 8739 | case OMPD_target_teams_distribute_parallel_for: |
| 8740 | case OMPD_target_teams_distribute_parallel_for_simd: |
| 8741 | case OMPD_target_teams: |
| 8742 | case OMPD_target_parallel: |
| 8743 | case OMPD_target_parallel_for: |
| 8744 | case OMPD_target_parallel_for_simd: |
| 8745 | case OMPD_unknown: |
| 8746 | llvm_unreachable("Unexpected standalone target data directive."); |
| 8747 | break; |
Samuel Antao | 8d2d730 | 2016-05-26 18:30:22 +0000 | [diff] [blame] | 8748 | } |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 8749 | CGF.EmitRuntimeCall(createRuntimeFunction(RTLFn), OffloadingArgs); |
Samuel Antao | bd0ae2e | 2016-04-27 23:07:29 +0000 | [diff] [blame] | 8750 | }; |
| 8751 | |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 8752 | auto &&TargetThenGen = [this, &ThenGen, &D, &InputInfo, &MapTypesArray]( |
| 8753 | CodeGenFunction &CGF, PrePostActionTy &) { |
| 8754 | // Fill up the arrays with all the mapped variables. |
| 8755 | MappableExprsHandler::MapBaseValuesArrayTy BasePointers; |
| 8756 | MappableExprsHandler::MapValuesArrayTy Pointers; |
| 8757 | MappableExprsHandler::MapValuesArrayTy Sizes; |
| 8758 | MappableExprsHandler::MapFlagsArrayTy MapTypes; |
Samuel Antao | bd0ae2e | 2016-04-27 23:07:29 +0000 | [diff] [blame] | 8759 | |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 8760 | // Get map clause information. |
| 8761 | MappableExprsHandler MEHandler(D, CGF); |
| 8762 | MEHandler.generateAllInfo(BasePointers, Pointers, Sizes, MapTypes); |
| 8763 | |
| 8764 | TargetDataInfo Info; |
| 8765 | // Fill up the arrays and create the arguments. |
| 8766 | emitOffloadingArrays(CGF, BasePointers, Pointers, Sizes, MapTypes, Info); |
| 8767 | emitOffloadingArraysArgument(CGF, Info.BasePointersArray, |
| 8768 | Info.PointersArray, Info.SizesArray, |
| 8769 | Info.MapTypesArray, Info); |
| 8770 | InputInfo.NumberOfTargetItems = Info.NumberOfPtrs; |
| 8771 | InputInfo.BasePointersArray = |
| 8772 | Address(Info.BasePointersArray, CGM.getPointerAlign()); |
| 8773 | InputInfo.PointersArray = |
| 8774 | Address(Info.PointersArray, CGM.getPointerAlign()); |
| 8775 | InputInfo.SizesArray = |
| 8776 | Address(Info.SizesArray, CGM.getPointerAlign()); |
| 8777 | MapTypesArray = Info.MapTypesArray; |
| 8778 | if (D.hasClausesOfKind<OMPDependClause>()) |
| 8779 | CGF.EmitOMPTargetTaskBasedDirective(D, ThenGen, InputInfo); |
| 8780 | else |
Alexey Bataev | 768f1f2 | 2018-01-09 19:59:25 +0000 | [diff] [blame] | 8781 | emitInlinedDirective(CGF, D.getDirectiveKind(), ThenGen); |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 8782 | }; |
| 8783 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8784 | if (IfCond) { |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 8785 | emitOMPIfClause(CGF, IfCond, TargetThenGen, |
| 8786 | [](CodeGenFunction &CGF, PrePostActionTy &) {}); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8787 | } else { |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 8788 | RegionCodeGenTy ThenRCG(TargetThenGen); |
| 8789 | ThenRCG(CGF); |
Samuel Antao | bd0ae2e | 2016-04-27 23:07:29 +0000 | [diff] [blame] | 8790 | } |
| 8791 | } |
Alexey Bataev | c7a82b4 | 2016-05-06 09:40:08 +0000 | [diff] [blame] | 8792 | |
| 8793 | namespace { |
| 8794 | /// Kind of parameter in a function with 'declare simd' directive. |
| 8795 | enum ParamKindTy { LinearWithVarStride, Linear, Uniform, Vector }; |
| 8796 | /// Attribute set of the parameter. |
| 8797 | struct ParamAttrTy { |
| 8798 | ParamKindTy Kind = Vector; |
| 8799 | llvm::APSInt StrideOrArg; |
| 8800 | llvm::APSInt Alignment; |
| 8801 | }; |
| 8802 | } // namespace |
| 8803 | |
| 8804 | static unsigned evaluateCDTSize(const FunctionDecl *FD, |
| 8805 | ArrayRef<ParamAttrTy> ParamAttrs) { |
| 8806 | // Every vector variant of a SIMD-enabled function has a vector length (VLEN). |
| 8807 | // If OpenMP clause "simdlen" is used, the VLEN is the value of the argument |
| 8808 | // of that clause. The VLEN value must be power of 2. |
| 8809 | // In other case the notion of the function`s "characteristic data type" (CDT) |
| 8810 | // is used to compute the vector length. |
| 8811 | // CDT is defined in the following order: |
| 8812 | // a) For non-void function, the CDT is the return type. |
| 8813 | // b) If the function has any non-uniform, non-linear parameters, then the |
| 8814 | // CDT is the type of the first such parameter. |
| 8815 | // c) If the CDT determined by a) or b) above is struct, union, or class |
| 8816 | // type which is pass-by-value (except for the type that maps to the |
| 8817 | // built-in complex data type), the characteristic data type is int. |
| 8818 | // d) If none of the above three cases is applicable, the CDT is int. |
| 8819 | // The VLEN is then determined based on the CDT and the size of vector |
| 8820 | // register of that ISA for which current vector version is generated. The |
| 8821 | // VLEN is computed using the formula below: |
| 8822 | // VLEN = sizeof(vector_register) / sizeof(CDT), |
| 8823 | // where vector register size specified in section 3.2.1 Registers and the |
| 8824 | // Stack Frame of original AMD64 ABI document. |
| 8825 | QualType RetType = FD->getReturnType(); |
| 8826 | if (RetType.isNull()) |
| 8827 | return 0; |
| 8828 | ASTContext &C = FD->getASTContext(); |
| 8829 | QualType CDT; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8830 | if (!RetType.isNull() && !RetType->isVoidType()) { |
Alexey Bataev | c7a82b4 | 2016-05-06 09:40:08 +0000 | [diff] [blame] | 8831 | CDT = RetType; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8832 | } else { |
Alexey Bataev | c7a82b4 | 2016-05-06 09:40:08 +0000 | [diff] [blame] | 8833 | unsigned Offset = 0; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8834 | if (const auto *MD = dyn_cast<CXXMethodDecl>(FD)) { |
Alexey Bataev | c7a82b4 | 2016-05-06 09:40:08 +0000 | [diff] [blame] | 8835 | if (ParamAttrs[Offset].Kind == Vector) |
| 8836 | CDT = C.getPointerType(C.getRecordType(MD->getParent())); |
| 8837 | ++Offset; |
| 8838 | } |
| 8839 | if (CDT.isNull()) { |
| 8840 | for (unsigned I = 0, E = FD->getNumParams(); I < E; ++I) { |
| 8841 | if (ParamAttrs[I + Offset].Kind == Vector) { |
| 8842 | CDT = FD->getParamDecl(I)->getType(); |
| 8843 | break; |
| 8844 | } |
| 8845 | } |
| 8846 | } |
| 8847 | } |
| 8848 | if (CDT.isNull()) |
| 8849 | CDT = C.IntTy; |
| 8850 | CDT = CDT->getCanonicalTypeUnqualified(); |
| 8851 | if (CDT->isRecordType() || CDT->isUnionType()) |
| 8852 | CDT = C.IntTy; |
| 8853 | return C.getTypeSize(CDT); |
| 8854 | } |
| 8855 | |
| 8856 | static void |
| 8857 | emitX86DeclareSimdFunction(const FunctionDecl *FD, llvm::Function *Fn, |
Benjamin Kramer | 81cb4b7 | 2016-11-24 16:01:20 +0000 | [diff] [blame] | 8858 | const llvm::APSInt &VLENVal, |
Alexey Bataev | c7a82b4 | 2016-05-06 09:40:08 +0000 | [diff] [blame] | 8859 | ArrayRef<ParamAttrTy> ParamAttrs, |
| 8860 | OMPDeclareSimdDeclAttr::BranchStateTy State) { |
| 8861 | struct ISADataTy { |
| 8862 | char ISA; |
| 8863 | unsigned VecRegSize; |
| 8864 | }; |
| 8865 | ISADataTy ISAData[] = { |
| 8866 | { |
| 8867 | 'b', 128 |
| 8868 | }, // SSE |
| 8869 | { |
| 8870 | 'c', 256 |
| 8871 | }, // AVX |
| 8872 | { |
| 8873 | 'd', 256 |
| 8874 | }, // AVX2 |
| 8875 | { |
| 8876 | 'e', 512 |
| 8877 | }, // AVX512 |
| 8878 | }; |
| 8879 | llvm::SmallVector<char, 2> Masked; |
| 8880 | switch (State) { |
| 8881 | case OMPDeclareSimdDeclAttr::BS_Undefined: |
| 8882 | Masked.push_back('N'); |
| 8883 | Masked.push_back('M'); |
| 8884 | break; |
| 8885 | case OMPDeclareSimdDeclAttr::BS_Notinbranch: |
| 8886 | Masked.push_back('N'); |
| 8887 | break; |
| 8888 | case OMPDeclareSimdDeclAttr::BS_Inbranch: |
| 8889 | Masked.push_back('M'); |
| 8890 | break; |
| 8891 | } |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8892 | for (char Mask : Masked) { |
| 8893 | for (const ISADataTy &Data : ISAData) { |
Alexey Bataev | c7a82b4 | 2016-05-06 09:40:08 +0000 | [diff] [blame] | 8894 | SmallString<256> Buffer; |
| 8895 | llvm::raw_svector_ostream Out(Buffer); |
| 8896 | Out << "_ZGV" << Data.ISA << Mask; |
| 8897 | if (!VLENVal) { |
| 8898 | Out << llvm::APSInt::getUnsigned(Data.VecRegSize / |
| 8899 | evaluateCDTSize(FD, ParamAttrs)); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8900 | } else { |
Alexey Bataev | c7a82b4 | 2016-05-06 09:40:08 +0000 | [diff] [blame] | 8901 | Out << VLENVal; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8902 | } |
| 8903 | for (const ParamAttrTy &ParamAttr : ParamAttrs) { |
Alexey Bataev | c7a82b4 | 2016-05-06 09:40:08 +0000 | [diff] [blame] | 8904 | switch (ParamAttr.Kind){ |
| 8905 | case LinearWithVarStride: |
| 8906 | Out << 's' << ParamAttr.StrideOrArg; |
| 8907 | break; |
| 8908 | case Linear: |
| 8909 | Out << 'l'; |
| 8910 | if (!!ParamAttr.StrideOrArg) |
| 8911 | Out << ParamAttr.StrideOrArg; |
| 8912 | break; |
| 8913 | case Uniform: |
| 8914 | Out << 'u'; |
| 8915 | break; |
| 8916 | case Vector: |
| 8917 | Out << 'v'; |
| 8918 | break; |
| 8919 | } |
| 8920 | if (!!ParamAttr.Alignment) |
| 8921 | Out << 'a' << ParamAttr.Alignment; |
| 8922 | } |
| 8923 | Out << '_' << Fn->getName(); |
| 8924 | Fn->addFnAttr(Out.str()); |
| 8925 | } |
| 8926 | } |
| 8927 | } |
| 8928 | |
| 8929 | void CGOpenMPRuntime::emitDeclareSimdFunction(const FunctionDecl *FD, |
| 8930 | llvm::Function *Fn) { |
| 8931 | ASTContext &C = CGM.getContext(); |
Alexey Bataev | 20cf67c | 2018-03-02 18:07:00 +0000 | [diff] [blame] | 8932 | FD = FD->getMostRecentDecl(); |
Alexey Bataev | c7a82b4 | 2016-05-06 09:40:08 +0000 | [diff] [blame] | 8933 | // Map params to their positions in function decl. |
| 8934 | llvm::DenseMap<const Decl *, unsigned> ParamPositions; |
| 8935 | if (isa<CXXMethodDecl>(FD)) |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8936 | ParamPositions.try_emplace(FD, 0); |
Alexey Bataev | c7a82b4 | 2016-05-06 09:40:08 +0000 | [diff] [blame] | 8937 | unsigned ParamPos = ParamPositions.size(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8938 | for (const ParmVarDecl *P : FD->parameters()) { |
| 8939 | ParamPositions.try_emplace(P->getCanonicalDecl(), ParamPos); |
Alexey Bataev | c7a82b4 | 2016-05-06 09:40:08 +0000 | [diff] [blame] | 8940 | ++ParamPos; |
| 8941 | } |
Alexey Bataev | 20cf67c | 2018-03-02 18:07:00 +0000 | [diff] [blame] | 8942 | while (FD) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8943 | for (const auto *Attr : FD->specific_attrs<OMPDeclareSimdDeclAttr>()) { |
Alexey Bataev | 20cf67c | 2018-03-02 18:07:00 +0000 | [diff] [blame] | 8944 | llvm::SmallVector<ParamAttrTy, 8> ParamAttrs(ParamPositions.size()); |
| 8945 | // Mark uniform parameters. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8946 | for (const Expr *E : Attr->uniforms()) { |
Alexey Bataev | 20cf67c | 2018-03-02 18:07:00 +0000 | [diff] [blame] | 8947 | E = E->IgnoreParenImpCasts(); |
| 8948 | unsigned Pos; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8949 | if (isa<CXXThisExpr>(E)) { |
Alexey Bataev | 20cf67c | 2018-03-02 18:07:00 +0000 | [diff] [blame] | 8950 | Pos = ParamPositions[FD]; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8951 | } else { |
| 8952 | const auto *PVD = cast<ParmVarDecl>(cast<DeclRefExpr>(E)->getDecl()) |
| 8953 | ->getCanonicalDecl(); |
Alexey Bataev | 20cf67c | 2018-03-02 18:07:00 +0000 | [diff] [blame] | 8954 | Pos = ParamPositions[PVD]; |
| 8955 | } |
| 8956 | ParamAttrs[Pos].Kind = Uniform; |
Alexey Bataev | c7a82b4 | 2016-05-06 09:40:08 +0000 | [diff] [blame] | 8957 | } |
Alexey Bataev | 20cf67c | 2018-03-02 18:07:00 +0000 | [diff] [blame] | 8958 | // Get alignment info. |
| 8959 | auto NI = Attr->alignments_begin(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8960 | for (const Expr *E : Attr->aligneds()) { |
Alexey Bataev | 20cf67c | 2018-03-02 18:07:00 +0000 | [diff] [blame] | 8961 | E = E->IgnoreParenImpCasts(); |
| 8962 | unsigned Pos; |
| 8963 | QualType ParmTy; |
| 8964 | if (isa<CXXThisExpr>(E)) { |
| 8965 | Pos = ParamPositions[FD]; |
| 8966 | ParmTy = E->getType(); |
| 8967 | } else { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8968 | const auto *PVD = cast<ParmVarDecl>(cast<DeclRefExpr>(E)->getDecl()) |
| 8969 | ->getCanonicalDecl(); |
Alexey Bataev | 20cf67c | 2018-03-02 18:07:00 +0000 | [diff] [blame] | 8970 | Pos = ParamPositions[PVD]; |
| 8971 | ParmTy = PVD->getType(); |
| 8972 | } |
| 8973 | ParamAttrs[Pos].Alignment = |
| 8974 | (*NI) |
| 8975 | ? (*NI)->EvaluateKnownConstInt(C) |
Alexey Bataev | c7a82b4 | 2016-05-06 09:40:08 +0000 | [diff] [blame] | 8976 | : llvm::APSInt::getUnsigned( |
| 8977 | C.toCharUnitsFromBits(C.getOpenMPDefaultSimdAlign(ParmTy)) |
| 8978 | .getQuantity()); |
Alexey Bataev | 20cf67c | 2018-03-02 18:07:00 +0000 | [diff] [blame] | 8979 | ++NI; |
Alexey Bataev | c7a82b4 | 2016-05-06 09:40:08 +0000 | [diff] [blame] | 8980 | } |
Alexey Bataev | 20cf67c | 2018-03-02 18:07:00 +0000 | [diff] [blame] | 8981 | // Mark linear parameters. |
| 8982 | auto SI = Attr->steps_begin(); |
| 8983 | auto MI = Attr->modifiers_begin(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8984 | for (const Expr *E : Attr->linears()) { |
Alexey Bataev | 20cf67c | 2018-03-02 18:07:00 +0000 | [diff] [blame] | 8985 | E = E->IgnoreParenImpCasts(); |
| 8986 | unsigned Pos; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8987 | if (isa<CXXThisExpr>(E)) { |
Alexey Bataev | 20cf67c | 2018-03-02 18:07:00 +0000 | [diff] [blame] | 8988 | Pos = ParamPositions[FD]; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8989 | } else { |
| 8990 | const auto *PVD = cast<ParmVarDecl>(cast<DeclRefExpr>(E)->getDecl()) |
| 8991 | ->getCanonicalDecl(); |
Alexey Bataev | 20cf67c | 2018-03-02 18:07:00 +0000 | [diff] [blame] | 8992 | Pos = ParamPositions[PVD]; |
| 8993 | } |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8994 | ParamAttrTy &ParamAttr = ParamAttrs[Pos]; |
Alexey Bataev | 20cf67c | 2018-03-02 18:07:00 +0000 | [diff] [blame] | 8995 | ParamAttr.Kind = Linear; |
| 8996 | if (*SI) { |
| 8997 | if (!(*SI)->EvaluateAsInt(ParamAttr.StrideOrArg, C, |
| 8998 | Expr::SE_AllowSideEffects)) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8999 | if (const auto *DRE = |
| 9000 | cast<DeclRefExpr>((*SI)->IgnoreParenImpCasts())) { |
| 9001 | if (const auto *StridePVD = cast<ParmVarDecl>(DRE->getDecl())) { |
Alexey Bataev | 20cf67c | 2018-03-02 18:07:00 +0000 | [diff] [blame] | 9002 | ParamAttr.Kind = LinearWithVarStride; |
| 9003 | ParamAttr.StrideOrArg = llvm::APSInt::getUnsigned( |
| 9004 | ParamPositions[StridePVD->getCanonicalDecl()]); |
| 9005 | } |
Alexey Bataev | c7a82b4 | 2016-05-06 09:40:08 +0000 | [diff] [blame] | 9006 | } |
| 9007 | } |
| 9008 | } |
Alexey Bataev | 20cf67c | 2018-03-02 18:07:00 +0000 | [diff] [blame] | 9009 | ++SI; |
| 9010 | ++MI; |
Alexey Bataev | c7a82b4 | 2016-05-06 09:40:08 +0000 | [diff] [blame] | 9011 | } |
Alexey Bataev | 20cf67c | 2018-03-02 18:07:00 +0000 | [diff] [blame] | 9012 | llvm::APSInt VLENVal; |
| 9013 | if (const Expr *VLEN = Attr->getSimdlen()) |
| 9014 | VLENVal = VLEN->EvaluateKnownConstInt(C); |
| 9015 | OMPDeclareSimdDeclAttr::BranchStateTy State = Attr->getBranchState(); |
| 9016 | if (CGM.getTriple().getArch() == llvm::Triple::x86 || |
| 9017 | CGM.getTriple().getArch() == llvm::Triple::x86_64) |
| 9018 | emitX86DeclareSimdFunction(FD, Fn, VLENVal, ParamAttrs, State); |
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 | FD = FD->getPreviousDecl(); |
Alexey Bataev | c7a82b4 | 2016-05-06 09:40:08 +0000 | [diff] [blame] | 9021 | } |
| 9022 | } |
Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 9023 | |
| 9024 | namespace { |
| 9025 | /// Cleanup action for doacross support. |
| 9026 | class DoacrossCleanupTy final : public EHScopeStack::Cleanup { |
| 9027 | public: |
| 9028 | static const int DoacrossFinArgs = 2; |
| 9029 | |
| 9030 | private: |
| 9031 | llvm::Value *RTLFn; |
| 9032 | llvm::Value *Args[DoacrossFinArgs]; |
| 9033 | |
| 9034 | public: |
| 9035 | DoacrossCleanupTy(llvm::Value *RTLFn, ArrayRef<llvm::Value *> CallArgs) |
| 9036 | : RTLFn(RTLFn) { |
| 9037 | assert(CallArgs.size() == DoacrossFinArgs); |
| 9038 | std::copy(CallArgs.begin(), CallArgs.end(), std::begin(Args)); |
| 9039 | } |
| 9040 | void Emit(CodeGenFunction &CGF, Flags /*flags*/) override { |
| 9041 | if (!CGF.HaveInsertPoint()) |
| 9042 | return; |
| 9043 | CGF.EmitRuntimeCall(RTLFn, Args); |
| 9044 | } |
| 9045 | }; |
| 9046 | } // namespace |
| 9047 | |
| 9048 | void CGOpenMPRuntime::emitDoacrossInit(CodeGenFunction &CGF, |
Alexey Bataev | f138fda | 2018-08-13 19:04:24 +0000 | [diff] [blame] | 9049 | const OMPLoopDirective &D, |
| 9050 | ArrayRef<Expr *> NumIterations) { |
Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 9051 | if (!CGF.HaveInsertPoint()) |
| 9052 | return; |
| 9053 | |
| 9054 | ASTContext &C = CGM.getContext(); |
| 9055 | QualType Int64Ty = C.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/true); |
| 9056 | RecordDecl *RD; |
| 9057 | if (KmpDimTy.isNull()) { |
| 9058 | // Build struct kmp_dim { // loop bounds info casted to kmp_int64 |
| 9059 | // kmp_int64 lo; // lower |
| 9060 | // kmp_int64 up; // upper |
| 9061 | // kmp_int64 st; // stride |
| 9062 | // }; |
| 9063 | RD = C.buildImplicitRecord("kmp_dim"); |
| 9064 | RD->startDefinition(); |
| 9065 | addFieldToRecordDecl(C, RD, Int64Ty); |
| 9066 | addFieldToRecordDecl(C, RD, Int64Ty); |
| 9067 | addFieldToRecordDecl(C, RD, Int64Ty); |
| 9068 | RD->completeDefinition(); |
| 9069 | KmpDimTy = C.getRecordType(RD); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 9070 | } else { |
Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 9071 | RD = cast<RecordDecl>(KmpDimTy->getAsTagDecl()); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 9072 | } |
Alexey Bataev | f138fda | 2018-08-13 19:04:24 +0000 | [diff] [blame] | 9073 | llvm::APInt Size(/*numBits=*/32, NumIterations.size()); |
| 9074 | QualType ArrayTy = |
| 9075 | C.getConstantArrayType(KmpDimTy, Size, ArrayType::Normal, 0); |
Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 9076 | |
Alexey Bataev | f138fda | 2018-08-13 19:04:24 +0000 | [diff] [blame] | 9077 | Address DimsAddr = CGF.CreateMemTemp(ArrayTy, "dims"); |
| 9078 | CGF.EmitNullInitialization(DimsAddr, ArrayTy); |
Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 9079 | enum { LowerFD = 0, UpperFD, StrideFD }; |
| 9080 | // Fill dims with data. |
Alexey Bataev | f138fda | 2018-08-13 19:04:24 +0000 | [diff] [blame] | 9081 | for (unsigned I = 0, E = NumIterations.size(); I < E; ++I) { |
| 9082 | LValue DimsLVal = |
| 9083 | CGF.MakeAddrLValue(CGF.Builder.CreateConstArrayGEP( |
| 9084 | DimsAddr, I, C.getTypeSizeInChars(KmpDimTy)), |
| 9085 | KmpDimTy); |
| 9086 | // dims.upper = num_iterations; |
| 9087 | LValue UpperLVal = CGF.EmitLValueForField( |
| 9088 | DimsLVal, *std::next(RD->field_begin(), UpperFD)); |
| 9089 | llvm::Value *NumIterVal = |
| 9090 | CGF.EmitScalarConversion(CGF.EmitScalarExpr(NumIterations[I]), |
| 9091 | D.getNumIterations()->getType(), Int64Ty, |
| 9092 | D.getNumIterations()->getExprLoc()); |
| 9093 | CGF.EmitStoreOfScalar(NumIterVal, UpperLVal); |
| 9094 | // dims.stride = 1; |
| 9095 | LValue StrideLVal = CGF.EmitLValueForField( |
| 9096 | DimsLVal, *std::next(RD->field_begin(), StrideFD)); |
| 9097 | CGF.EmitStoreOfScalar(llvm::ConstantInt::getSigned(CGM.Int64Ty, /*V=*/1), |
| 9098 | StrideLVal); |
| 9099 | } |
Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 9100 | |
| 9101 | // Build call void __kmpc_doacross_init(ident_t *loc, kmp_int32 gtid, |
| 9102 | // kmp_int32 num_dims, struct kmp_dim * dims); |
Alexey Bataev | f138fda | 2018-08-13 19:04:24 +0000 | [diff] [blame] | 9103 | llvm::Value *Args[] = { |
| 9104 | emitUpdateLocation(CGF, D.getBeginLoc()), |
| 9105 | getThreadID(CGF, D.getBeginLoc()), |
| 9106 | llvm::ConstantInt::getSigned(CGM.Int32Ty, NumIterations.size()), |
| 9107 | CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
| 9108 | CGF.Builder |
| 9109 | .CreateConstArrayGEP(DimsAddr, 0, C.getTypeSizeInChars(KmpDimTy)) |
| 9110 | .getPointer(), |
| 9111 | CGM.VoidPtrTy)}; |
Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 9112 | |
| 9113 | llvm::Value *RTLFn = createRuntimeFunction(OMPRTL__kmpc_doacross_init); |
| 9114 | CGF.EmitRuntimeCall(RTLFn, Args); |
| 9115 | llvm::Value *FiniArgs[DoacrossCleanupTy::DoacrossFinArgs] = { |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 9116 | emitUpdateLocation(CGF, D.getEndLoc()), getThreadID(CGF, D.getEndLoc())}; |
Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 9117 | llvm::Value *FiniRTLFn = createRuntimeFunction(OMPRTL__kmpc_doacross_fini); |
| 9118 | CGF.EHStack.pushCleanup<DoacrossCleanupTy>(NormalAndEHCleanup, FiniRTLFn, |
| 9119 | llvm::makeArrayRef(FiniArgs)); |
| 9120 | } |
| 9121 | |
| 9122 | void CGOpenMPRuntime::emitDoacrossOrdered(CodeGenFunction &CGF, |
| 9123 | const OMPDependClause *C) { |
| 9124 | QualType Int64Ty = |
| 9125 | CGM.getContext().getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1); |
Alexey Bataev | f138fda | 2018-08-13 19:04:24 +0000 | [diff] [blame] | 9126 | llvm::APInt Size(/*numBits=*/32, C->getNumLoops()); |
| 9127 | QualType ArrayTy = CGM.getContext().getConstantArrayType( |
| 9128 | Int64Ty, Size, ArrayType::Normal, 0); |
| 9129 | Address CntAddr = CGF.CreateMemTemp(ArrayTy, ".cnt.addr"); |
| 9130 | for (unsigned I = 0, E = C->getNumLoops(); I < E; ++I) { |
| 9131 | const Expr *CounterVal = C->getLoopData(I); |
| 9132 | assert(CounterVal); |
| 9133 | llvm::Value *CntVal = CGF.EmitScalarConversion( |
| 9134 | CGF.EmitScalarExpr(CounterVal), CounterVal->getType(), Int64Ty, |
| 9135 | CounterVal->getExprLoc()); |
| 9136 | CGF.EmitStoreOfScalar( |
| 9137 | CntVal, |
| 9138 | CGF.Builder.CreateConstArrayGEP( |
| 9139 | CntAddr, I, CGM.getContext().getTypeSizeInChars(Int64Ty)), |
| 9140 | /*Volatile=*/false, Int64Ty); |
| 9141 | } |
| 9142 | llvm::Value *Args[] = { |
| 9143 | emitUpdateLocation(CGF, C->getBeginLoc()), |
| 9144 | getThreadID(CGF, C->getBeginLoc()), |
| 9145 | CGF.Builder |
| 9146 | .CreateConstArrayGEP(CntAddr, 0, |
| 9147 | CGM.getContext().getTypeSizeInChars(Int64Ty)) |
| 9148 | .getPointer()}; |
Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 9149 | llvm::Value *RTLFn; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 9150 | if (C->getDependencyKind() == OMPC_DEPEND_source) { |
Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 9151 | RTLFn = createRuntimeFunction(OMPRTL__kmpc_doacross_post); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 9152 | } else { |
Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 9153 | assert(C->getDependencyKind() == OMPC_DEPEND_sink); |
| 9154 | RTLFn = createRuntimeFunction(OMPRTL__kmpc_doacross_wait); |
| 9155 | } |
| 9156 | CGF.EmitRuntimeCall(RTLFn, Args); |
| 9157 | } |
| 9158 | |
Alexey Bataev | 7ef47a6 | 2018-02-22 18:33:31 +0000 | [diff] [blame] | 9159 | void CGOpenMPRuntime::emitCall(CodeGenFunction &CGF, SourceLocation Loc, |
| 9160 | llvm::Value *Callee, |
| 9161 | ArrayRef<llvm::Value *> Args) const { |
| 9162 | assert(Loc.isValid() && "Outlined function call location must be valid."); |
Alexey Bataev | 3c595a6 | 2017-08-14 15:01:03 +0000 | [diff] [blame] | 9163 | auto DL = ApplyDebugLocation::CreateDefaultArtificial(CGF, Loc); |
| 9164 | |
| 9165 | if (auto *Fn = dyn_cast<llvm::Function>(Callee)) { |
Alexey Bataev | 2c7eee5 | 2017-08-04 19:10:54 +0000 | [diff] [blame] | 9166 | if (Fn->doesNotThrow()) { |
Alexey Bataev | 3c595a6 | 2017-08-14 15:01:03 +0000 | [diff] [blame] | 9167 | CGF.EmitNounwindRuntimeCall(Fn, Args); |
Alexey Bataev | 2c7eee5 | 2017-08-04 19:10:54 +0000 | [diff] [blame] | 9168 | return; |
| 9169 | } |
| 9170 | } |
Alexey Bataev | 3c595a6 | 2017-08-14 15:01:03 +0000 | [diff] [blame] | 9171 | CGF.EmitRuntimeCall(Callee, Args); |
| 9172 | } |
| 9173 | |
| 9174 | void CGOpenMPRuntime::emitOutlinedFunctionCall( |
| 9175 | CodeGenFunction &CGF, SourceLocation Loc, llvm::Value *OutlinedFn, |
| 9176 | ArrayRef<llvm::Value *> Args) const { |
Alexey Bataev | 7ef47a6 | 2018-02-22 18:33:31 +0000 | [diff] [blame] | 9177 | emitCall(CGF, Loc, OutlinedFn, Args); |
Alexey Bataev | 2c7eee5 | 2017-08-04 19:10:54 +0000 | [diff] [blame] | 9178 | } |
Alexey Bataev | 3b8d558 | 2017-08-08 18:04:06 +0000 | [diff] [blame] | 9179 | |
| 9180 | Address CGOpenMPRuntime::getParameterAddress(CodeGenFunction &CGF, |
| 9181 | const VarDecl *NativeParam, |
| 9182 | const VarDecl *TargetParam) const { |
| 9183 | return CGF.GetAddrOfLocalVar(NativeParam); |
| 9184 | } |
Alexey Bataev | a8a9153a | 2017-12-29 18:07:07 +0000 | [diff] [blame] | 9185 | |
Gheorghe-Teodor Bercea | d3dcf2f | 2018-03-14 14:17:45 +0000 | [diff] [blame] | 9186 | Address CGOpenMPRuntime::getAddressOfLocalVariable(CodeGenFunction &CGF, |
| 9187 | const VarDecl *VD) { |
| 9188 | return Address::invalid(); |
| 9189 | } |
| 9190 | |
Alexey Bataev | a8a9153a | 2017-12-29 18:07:07 +0000 | [diff] [blame] | 9191 | llvm::Value *CGOpenMPSIMDRuntime::emitParallelOutlinedFunction( |
| 9192 | const OMPExecutableDirective &D, const VarDecl *ThreadIDVar, |
| 9193 | OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen) { |
| 9194 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 9195 | } |
| 9196 | |
| 9197 | llvm::Value *CGOpenMPSIMDRuntime::emitTeamsOutlinedFunction( |
| 9198 | const OMPExecutableDirective &D, const VarDecl *ThreadIDVar, |
| 9199 | OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen) { |
| 9200 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 9201 | } |
| 9202 | |
| 9203 | llvm::Value *CGOpenMPSIMDRuntime::emitTaskOutlinedFunction( |
| 9204 | const OMPExecutableDirective &D, const VarDecl *ThreadIDVar, |
| 9205 | const VarDecl *PartIDVar, const VarDecl *TaskTVar, |
| 9206 | OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen, |
| 9207 | bool Tied, unsigned &NumberOfParts) { |
| 9208 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 9209 | } |
| 9210 | |
| 9211 | void CGOpenMPSIMDRuntime::emitParallelCall(CodeGenFunction &CGF, |
| 9212 | SourceLocation Loc, |
| 9213 | llvm::Value *OutlinedFn, |
| 9214 | ArrayRef<llvm::Value *> CapturedVars, |
| 9215 | const Expr *IfCond) { |
| 9216 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 9217 | } |
| 9218 | |
| 9219 | void CGOpenMPSIMDRuntime::emitCriticalRegion( |
| 9220 | CodeGenFunction &CGF, StringRef CriticalName, |
| 9221 | const RegionCodeGenTy &CriticalOpGen, SourceLocation Loc, |
| 9222 | const Expr *Hint) { |
| 9223 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 9224 | } |
| 9225 | |
| 9226 | void CGOpenMPSIMDRuntime::emitMasterRegion(CodeGenFunction &CGF, |
| 9227 | const RegionCodeGenTy &MasterOpGen, |
| 9228 | SourceLocation Loc) { |
| 9229 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 9230 | } |
| 9231 | |
| 9232 | void CGOpenMPSIMDRuntime::emitTaskyieldCall(CodeGenFunction &CGF, |
| 9233 | SourceLocation Loc) { |
| 9234 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 9235 | } |
| 9236 | |
| 9237 | void CGOpenMPSIMDRuntime::emitTaskgroupRegion( |
| 9238 | CodeGenFunction &CGF, const RegionCodeGenTy &TaskgroupOpGen, |
| 9239 | SourceLocation Loc) { |
| 9240 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 9241 | } |
| 9242 | |
| 9243 | void CGOpenMPSIMDRuntime::emitSingleRegion( |
| 9244 | CodeGenFunction &CGF, const RegionCodeGenTy &SingleOpGen, |
| 9245 | SourceLocation Loc, ArrayRef<const Expr *> CopyprivateVars, |
| 9246 | ArrayRef<const Expr *> DestExprs, ArrayRef<const Expr *> SrcExprs, |
| 9247 | ArrayRef<const Expr *> AssignmentOps) { |
| 9248 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 9249 | } |
| 9250 | |
| 9251 | void CGOpenMPSIMDRuntime::emitOrderedRegion(CodeGenFunction &CGF, |
| 9252 | const RegionCodeGenTy &OrderedOpGen, |
| 9253 | SourceLocation Loc, |
| 9254 | bool IsThreads) { |
| 9255 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 9256 | } |
| 9257 | |
| 9258 | void CGOpenMPSIMDRuntime::emitBarrierCall(CodeGenFunction &CGF, |
| 9259 | SourceLocation Loc, |
| 9260 | OpenMPDirectiveKind Kind, |
| 9261 | bool EmitChecks, |
| 9262 | bool ForceSimpleCall) { |
| 9263 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 9264 | } |
| 9265 | |
| 9266 | void CGOpenMPSIMDRuntime::emitForDispatchInit( |
| 9267 | CodeGenFunction &CGF, SourceLocation Loc, |
| 9268 | const OpenMPScheduleTy &ScheduleKind, unsigned IVSize, bool IVSigned, |
| 9269 | bool Ordered, const DispatchRTInput &DispatchValues) { |
| 9270 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 9271 | } |
| 9272 | |
| 9273 | void CGOpenMPSIMDRuntime::emitForStaticInit( |
| 9274 | CodeGenFunction &CGF, SourceLocation Loc, OpenMPDirectiveKind DKind, |
| 9275 | const OpenMPScheduleTy &ScheduleKind, const StaticRTInput &Values) { |
| 9276 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 9277 | } |
| 9278 | |
| 9279 | void CGOpenMPSIMDRuntime::emitDistributeStaticInit( |
| 9280 | CodeGenFunction &CGF, SourceLocation Loc, |
| 9281 | OpenMPDistScheduleClauseKind SchedKind, const StaticRTInput &Values) { |
| 9282 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 9283 | } |
| 9284 | |
| 9285 | void CGOpenMPSIMDRuntime::emitForOrderedIterationEnd(CodeGenFunction &CGF, |
| 9286 | SourceLocation Loc, |
| 9287 | unsigned IVSize, |
| 9288 | bool IVSigned) { |
| 9289 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 9290 | } |
| 9291 | |
| 9292 | void CGOpenMPSIMDRuntime::emitForStaticFinish(CodeGenFunction &CGF, |
| 9293 | SourceLocation Loc, |
| 9294 | OpenMPDirectiveKind DKind) { |
| 9295 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 9296 | } |
| 9297 | |
| 9298 | llvm::Value *CGOpenMPSIMDRuntime::emitForNext(CodeGenFunction &CGF, |
| 9299 | SourceLocation Loc, |
| 9300 | unsigned IVSize, bool IVSigned, |
| 9301 | Address IL, Address LB, |
| 9302 | Address UB, Address ST) { |
| 9303 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 9304 | } |
| 9305 | |
| 9306 | void CGOpenMPSIMDRuntime::emitNumThreadsClause(CodeGenFunction &CGF, |
| 9307 | llvm::Value *NumThreads, |
| 9308 | SourceLocation Loc) { |
| 9309 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 9310 | } |
| 9311 | |
| 9312 | void CGOpenMPSIMDRuntime::emitProcBindClause(CodeGenFunction &CGF, |
| 9313 | OpenMPProcBindClauseKind ProcBind, |
| 9314 | SourceLocation Loc) { |
| 9315 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 9316 | } |
| 9317 | |
| 9318 | Address CGOpenMPSIMDRuntime::getAddrOfThreadPrivate(CodeGenFunction &CGF, |
| 9319 | const VarDecl *VD, |
| 9320 | Address VDAddr, |
| 9321 | SourceLocation Loc) { |
| 9322 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 9323 | } |
| 9324 | |
| 9325 | llvm::Function *CGOpenMPSIMDRuntime::emitThreadPrivateVarDefinition( |
| 9326 | const VarDecl *VD, Address VDAddr, SourceLocation Loc, bool PerformInit, |
| 9327 | CodeGenFunction *CGF) { |
| 9328 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 9329 | } |
| 9330 | |
| 9331 | Address CGOpenMPSIMDRuntime::getAddrOfArtificialThreadPrivate( |
| 9332 | CodeGenFunction &CGF, QualType VarType, StringRef Name) { |
| 9333 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 9334 | } |
| 9335 | |
| 9336 | void CGOpenMPSIMDRuntime::emitFlush(CodeGenFunction &CGF, |
| 9337 | ArrayRef<const Expr *> Vars, |
| 9338 | SourceLocation Loc) { |
| 9339 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 9340 | } |
| 9341 | |
| 9342 | void CGOpenMPSIMDRuntime::emitTaskCall(CodeGenFunction &CGF, SourceLocation Loc, |
| 9343 | const OMPExecutableDirective &D, |
| 9344 | llvm::Value *TaskFunction, |
| 9345 | QualType SharedsTy, Address Shareds, |
| 9346 | const Expr *IfCond, |
| 9347 | const OMPTaskDataTy &Data) { |
| 9348 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 9349 | } |
| 9350 | |
| 9351 | void CGOpenMPSIMDRuntime::emitTaskLoopCall( |
| 9352 | CodeGenFunction &CGF, SourceLocation Loc, const OMPLoopDirective &D, |
| 9353 | llvm::Value *TaskFunction, QualType SharedsTy, Address Shareds, |
| 9354 | const Expr *IfCond, const OMPTaskDataTy &Data) { |
| 9355 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 9356 | } |
| 9357 | |
| 9358 | void CGOpenMPSIMDRuntime::emitReduction( |
| 9359 | CodeGenFunction &CGF, SourceLocation Loc, ArrayRef<const Expr *> Privates, |
| 9360 | ArrayRef<const Expr *> LHSExprs, ArrayRef<const Expr *> RHSExprs, |
| 9361 | ArrayRef<const Expr *> ReductionOps, ReductionOptionsTy Options) { |
| 9362 | assert(Options.SimpleReduction && "Only simple reduction is expected."); |
| 9363 | CGOpenMPRuntime::emitReduction(CGF, Loc, Privates, LHSExprs, RHSExprs, |
| 9364 | ReductionOps, Options); |
| 9365 | } |
| 9366 | |
| 9367 | llvm::Value *CGOpenMPSIMDRuntime::emitTaskReductionInit( |
| 9368 | CodeGenFunction &CGF, SourceLocation Loc, ArrayRef<const Expr *> LHSExprs, |
| 9369 | ArrayRef<const Expr *> RHSExprs, const OMPTaskDataTy &Data) { |
| 9370 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 9371 | } |
| 9372 | |
| 9373 | void CGOpenMPSIMDRuntime::emitTaskReductionFixups(CodeGenFunction &CGF, |
| 9374 | SourceLocation Loc, |
| 9375 | ReductionCodeGen &RCG, |
| 9376 | unsigned N) { |
| 9377 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 9378 | } |
| 9379 | |
| 9380 | Address CGOpenMPSIMDRuntime::getTaskReductionItem(CodeGenFunction &CGF, |
| 9381 | SourceLocation Loc, |
| 9382 | llvm::Value *ReductionsPtr, |
| 9383 | LValue SharedLVal) { |
| 9384 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 9385 | } |
| 9386 | |
| 9387 | void CGOpenMPSIMDRuntime::emitTaskwaitCall(CodeGenFunction &CGF, |
| 9388 | SourceLocation Loc) { |
| 9389 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 9390 | } |
| 9391 | |
| 9392 | void CGOpenMPSIMDRuntime::emitCancellationPointCall( |
| 9393 | CodeGenFunction &CGF, SourceLocation Loc, |
| 9394 | OpenMPDirectiveKind CancelRegion) { |
| 9395 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 9396 | } |
| 9397 | |
| 9398 | void CGOpenMPSIMDRuntime::emitCancelCall(CodeGenFunction &CGF, |
| 9399 | SourceLocation Loc, const Expr *IfCond, |
| 9400 | OpenMPDirectiveKind CancelRegion) { |
| 9401 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 9402 | } |
| 9403 | |
| 9404 | void CGOpenMPSIMDRuntime::emitTargetOutlinedFunction( |
| 9405 | const OMPExecutableDirective &D, StringRef ParentName, |
| 9406 | llvm::Function *&OutlinedFn, llvm::Constant *&OutlinedFnID, |
| 9407 | bool IsOffloadEntry, const RegionCodeGenTy &CodeGen) { |
| 9408 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 9409 | } |
| 9410 | |
| 9411 | void CGOpenMPSIMDRuntime::emitTargetCall(CodeGenFunction &CGF, |
| 9412 | const OMPExecutableDirective &D, |
| 9413 | llvm::Value *OutlinedFn, |
| 9414 | llvm::Value *OutlinedFnID, |
Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 9415 | const Expr *IfCond, const Expr *Device) { |
Alexey Bataev | a8a9153a | 2017-12-29 18:07:07 +0000 | [diff] [blame] | 9416 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 9417 | } |
| 9418 | |
| 9419 | bool CGOpenMPSIMDRuntime::emitTargetFunctions(GlobalDecl GD) { |
| 9420 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 9421 | } |
| 9422 | |
| 9423 | bool CGOpenMPSIMDRuntime::emitTargetGlobalVariable(GlobalDecl GD) { |
| 9424 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 9425 | } |
| 9426 | |
| 9427 | bool CGOpenMPSIMDRuntime::emitTargetGlobal(GlobalDecl GD) { |
| 9428 | return false; |
| 9429 | } |
| 9430 | |
| 9431 | llvm::Function *CGOpenMPSIMDRuntime::emitRegistrationFunction() { |
| 9432 | return nullptr; |
| 9433 | } |
| 9434 | |
| 9435 | void CGOpenMPSIMDRuntime::emitTeamsCall(CodeGenFunction &CGF, |
| 9436 | const OMPExecutableDirective &D, |
| 9437 | SourceLocation Loc, |
| 9438 | llvm::Value *OutlinedFn, |
| 9439 | ArrayRef<llvm::Value *> CapturedVars) { |
| 9440 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 9441 | } |
| 9442 | |
| 9443 | void CGOpenMPSIMDRuntime::emitNumTeamsClause(CodeGenFunction &CGF, |
| 9444 | const Expr *NumTeams, |
| 9445 | const Expr *ThreadLimit, |
| 9446 | SourceLocation Loc) { |
| 9447 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 9448 | } |
| 9449 | |
| 9450 | void CGOpenMPSIMDRuntime::emitTargetDataCalls( |
| 9451 | CodeGenFunction &CGF, const OMPExecutableDirective &D, const Expr *IfCond, |
| 9452 | const Expr *Device, const RegionCodeGenTy &CodeGen, TargetDataInfo &Info) { |
| 9453 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 9454 | } |
| 9455 | |
| 9456 | void CGOpenMPSIMDRuntime::emitTargetDataStandAloneCall( |
| 9457 | CodeGenFunction &CGF, const OMPExecutableDirective &D, const Expr *IfCond, |
| 9458 | const Expr *Device) { |
| 9459 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 9460 | } |
| 9461 | |
| 9462 | void CGOpenMPSIMDRuntime::emitDoacrossInit(CodeGenFunction &CGF, |
Alexey Bataev | f138fda | 2018-08-13 19:04:24 +0000 | [diff] [blame] | 9463 | const OMPLoopDirective &D, |
| 9464 | ArrayRef<Expr *> NumIterations) { |
Alexey Bataev | a8a9153a | 2017-12-29 18:07:07 +0000 | [diff] [blame] | 9465 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 9466 | } |
| 9467 | |
| 9468 | void CGOpenMPSIMDRuntime::emitDoacrossOrdered(CodeGenFunction &CGF, |
| 9469 | const OMPDependClause *C) { |
| 9470 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 9471 | } |
| 9472 | |
| 9473 | const VarDecl * |
| 9474 | CGOpenMPSIMDRuntime::translateParameter(const FieldDecl *FD, |
| 9475 | const VarDecl *NativeParam) const { |
| 9476 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 9477 | } |
| 9478 | |
| 9479 | Address |
| 9480 | CGOpenMPSIMDRuntime::getParameterAddress(CodeGenFunction &CGF, |
| 9481 | const VarDecl *NativeParam, |
| 9482 | const VarDecl *TargetParam) const { |
| 9483 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 9484 | } |
| 9485 | |