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 | |
Alexey Bataev | 92327c5 | 2018-03-26 16:40:55 +0000 | [diff] [blame] | 900 | static llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> |
| 901 | isDeclareTargetDeclaration(const ValueDecl *VD) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 902 | for (const Decl *D : VD->redecls()) { |
Alexey Bataev | 92327c5 | 2018-03-26 16:40:55 +0000 | [diff] [blame] | 903 | if (!D->hasAttrs()) |
| 904 | continue; |
| 905 | if (const auto *Attr = D->getAttr<OMPDeclareTargetDeclAttr>()) |
| 906 | return Attr->getMapType(); |
| 907 | } |
Alexey Bataev | fb38828 | 2018-05-01 14:09:46 +0000 | [diff] [blame] | 908 | if (const auto *V = dyn_cast<VarDecl>(VD)) { |
| 909 | if (const VarDecl *TD = V->getTemplateInstantiationPattern()) |
| 910 | return isDeclareTargetDeclaration(TD); |
| 911 | } else if (const auto *FD = dyn_cast<FunctionDecl>(VD)) { |
| 912 | if (const auto *TD = FD->getTemplateInstantiationPattern()) |
| 913 | return isDeclareTargetDeclaration(TD); |
| 914 | } |
| 915 | |
Alexey Bataev | 92327c5 | 2018-03-26 16:40:55 +0000 | [diff] [blame] | 916 | return llvm::None; |
| 917 | } |
| 918 | |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 919 | LValue ReductionCodeGen::emitSharedLValue(CodeGenFunction &CGF, const Expr *E) { |
Alexey Bataev | f47c4b4 | 2017-09-26 13:47:31 +0000 | [diff] [blame] | 920 | return CGF.EmitOMPSharedLValue(E); |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 921 | } |
| 922 | |
| 923 | LValue ReductionCodeGen::emitSharedLValueUB(CodeGenFunction &CGF, |
| 924 | const Expr *E) { |
| 925 | if (const auto *OASE = dyn_cast<OMPArraySectionExpr>(E)) |
| 926 | return CGF.EmitOMPArraySectionExpr(OASE, /*IsLowerBound=*/false); |
| 927 | return LValue(); |
| 928 | } |
| 929 | |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 930 | void ReductionCodeGen::emitAggregateInitialization( |
| 931 | CodeGenFunction &CGF, unsigned N, Address PrivateAddr, LValue SharedLVal, |
| 932 | const OMPDeclareReductionDecl *DRD) { |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 933 | // Emit VarDecl with copy init for arrays. |
| 934 | // Get the address of the original variable captured in current |
| 935 | // captured region. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 936 | const auto *PrivateVD = |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 937 | cast<VarDecl>(cast<DeclRefExpr>(ClausesData[N].Private)->getDecl()); |
Alexey Bataev | a7b1915 | 2017-10-12 20:03:39 +0000 | [diff] [blame] | 938 | bool EmitDeclareReductionInit = |
| 939 | DRD && (DRD->getInitializer() || !PrivateVD->hasInit()); |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 940 | EmitOMPAggregateInit(CGF, PrivateAddr, PrivateVD->getType(), |
Alexey Bataev | a7b1915 | 2017-10-12 20:03:39 +0000 | [diff] [blame] | 941 | EmitDeclareReductionInit, |
| 942 | EmitDeclareReductionInit ? ClausesData[N].ReductionOp |
| 943 | : PrivateVD->getInit(), |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 944 | DRD, SharedLVal.getAddress()); |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 945 | } |
| 946 | |
| 947 | ReductionCodeGen::ReductionCodeGen(ArrayRef<const Expr *> Shareds, |
| 948 | ArrayRef<const Expr *> Privates, |
| 949 | ArrayRef<const Expr *> ReductionOps) { |
| 950 | ClausesData.reserve(Shareds.size()); |
| 951 | SharedAddresses.reserve(Shareds.size()); |
| 952 | Sizes.reserve(Shareds.size()); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 953 | BaseDecls.reserve(Shareds.size()); |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 954 | auto IPriv = Privates.begin(); |
| 955 | auto IRed = ReductionOps.begin(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 956 | for (const Expr *Ref : Shareds) { |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 957 | ClausesData.emplace_back(Ref, *IPriv, *IRed); |
| 958 | std::advance(IPriv, 1); |
| 959 | std::advance(IRed, 1); |
| 960 | } |
| 961 | } |
| 962 | |
| 963 | void ReductionCodeGen::emitSharedLValue(CodeGenFunction &CGF, unsigned N) { |
| 964 | assert(SharedAddresses.size() == N && |
| 965 | "Number of generated lvalues must be exactly N."); |
Jonas Hahnfeld | 4525c82 | 2017-10-23 19:01:35 +0000 | [diff] [blame] | 966 | LValue First = emitSharedLValue(CGF, ClausesData[N].Ref); |
| 967 | LValue Second = emitSharedLValueUB(CGF, ClausesData[N].Ref); |
| 968 | SharedAddresses.emplace_back(First, Second); |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 969 | } |
| 970 | |
| 971 | void ReductionCodeGen::emitAggregateType(CodeGenFunction &CGF, unsigned N) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 972 | const auto *PrivateVD = |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 973 | cast<VarDecl>(cast<DeclRefExpr>(ClausesData[N].Private)->getDecl()); |
| 974 | QualType PrivateType = PrivateVD->getType(); |
| 975 | bool AsArraySection = isa<OMPArraySectionExpr>(ClausesData[N].Ref); |
Jonas Hahnfeld | 4525c82 | 2017-10-23 19:01:35 +0000 | [diff] [blame] | 976 | if (!PrivateType->isVariablyModifiedType()) { |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 977 | Sizes.emplace_back( |
| 978 | CGF.getTypeSize( |
| 979 | SharedAddresses[N].first.getType().getNonReferenceType()), |
| 980 | nullptr); |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 981 | return; |
| 982 | } |
| 983 | llvm::Value *Size; |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 984 | llvm::Value *SizeInChars; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 985 | auto *ElemType = |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 986 | cast<llvm::PointerType>(SharedAddresses[N].first.getPointer()->getType()) |
| 987 | ->getElementType(); |
| 988 | auto *ElemSizeOf = llvm::ConstantExpr::getSizeOf(ElemType); |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 989 | if (AsArraySection) { |
| 990 | Size = CGF.Builder.CreatePtrDiff(SharedAddresses[N].second.getPointer(), |
| 991 | SharedAddresses[N].first.getPointer()); |
| 992 | Size = CGF.Builder.CreateNUWAdd( |
| 993 | Size, llvm::ConstantInt::get(Size->getType(), /*V=*/1)); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 994 | SizeInChars = CGF.Builder.CreateNUWMul(Size, ElemSizeOf); |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 995 | } else { |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 996 | SizeInChars = CGF.getTypeSize( |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 997 | SharedAddresses[N].first.getType().getNonReferenceType()); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 998 | Size = CGF.Builder.CreateExactUDiv(SizeInChars, ElemSizeOf); |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 999 | } |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 1000 | Sizes.emplace_back(SizeInChars, Size); |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 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::emitAggregateType(CodeGenFunction &CGF, unsigned N, |
| 1010 | llvm::Value *Size) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1011 | const auto *PrivateVD = |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1012 | cast<VarDecl>(cast<DeclRefExpr>(ClausesData[N].Private)->getDecl()); |
| 1013 | QualType PrivateType = PrivateVD->getType(); |
Jonas Hahnfeld | 4525c82 | 2017-10-23 19:01:35 +0000 | [diff] [blame] | 1014 | if (!PrivateType->isVariablyModifiedType()) { |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 1015 | assert(!Size && !Sizes[N].second && |
Jonas Hahnfeld | 4525c82 | 2017-10-23 19:01:35 +0000 | [diff] [blame] | 1016 | "Size should be nullptr for non-variably modified reduction " |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1017 | "items."); |
| 1018 | return; |
| 1019 | } |
| 1020 | CodeGenFunction::OpaqueValueMapping OpaqueMap( |
| 1021 | CGF, |
| 1022 | cast<OpaqueValueExpr>( |
| 1023 | CGF.getContext().getAsVariableArrayType(PrivateType)->getSizeExpr()), |
| 1024 | RValue::get(Size)); |
| 1025 | CGF.EmitVariablyModifiedType(PrivateType); |
| 1026 | } |
| 1027 | |
| 1028 | void ReductionCodeGen::emitInitialization( |
| 1029 | CodeGenFunction &CGF, unsigned N, Address PrivateAddr, LValue SharedLVal, |
| 1030 | llvm::function_ref<bool(CodeGenFunction &)> DefaultInit) { |
| 1031 | assert(SharedAddresses.size() > N && "No variable was generated"); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1032 | const auto *PrivateVD = |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1033 | cast<VarDecl>(cast<DeclRefExpr>(ClausesData[N].Private)->getDecl()); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1034 | const OMPDeclareReductionDecl *DRD = |
| 1035 | getReductionInit(ClausesData[N].ReductionOp); |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1036 | QualType PrivateType = PrivateVD->getType(); |
| 1037 | PrivateAddr = CGF.Builder.CreateElementBitCast( |
| 1038 | PrivateAddr, CGF.ConvertTypeForMem(PrivateType)); |
| 1039 | QualType SharedType = SharedAddresses[N].first.getType(); |
| 1040 | SharedLVal = CGF.MakeAddrLValue( |
| 1041 | CGF.Builder.CreateElementBitCast(SharedLVal.getAddress(), |
| 1042 | CGF.ConvertTypeForMem(SharedType)), |
Ivan A. Kosarev | f5f2046 | 2017-10-12 11:29:46 +0000 | [diff] [blame] | 1043 | SharedType, SharedAddresses[N].first.getBaseInfo(), |
Ivan A. Kosarev | b9c59f3 | 2017-10-31 11:05:34 +0000 | [diff] [blame] | 1044 | CGF.CGM.getTBAAInfoForSubobject(SharedAddresses[N].first, SharedType)); |
Jonas Hahnfeld | 4525c82 | 2017-10-23 19:01:35 +0000 | [diff] [blame] | 1045 | if (CGF.getContext().getAsArrayType(PrivateVD->getType())) { |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 1046 | emitAggregateInitialization(CGF, N, PrivateAddr, SharedLVal, DRD); |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1047 | } else if (DRD && (DRD->getInitializer() || !PrivateVD->hasInit())) { |
| 1048 | emitInitWithReductionInitializer(CGF, DRD, ClausesData[N].ReductionOp, |
| 1049 | PrivateAddr, SharedLVal.getAddress(), |
| 1050 | SharedLVal.getType()); |
| 1051 | } else if (!DefaultInit(CGF) && PrivateVD->hasInit() && |
| 1052 | !CGF.isTrivialInitializer(PrivateVD->getInit())) { |
| 1053 | CGF.EmitAnyExprToMem(PrivateVD->getInit(), PrivateAddr, |
| 1054 | PrivateVD->getType().getQualifiers(), |
| 1055 | /*IsInitializer=*/false); |
| 1056 | } |
| 1057 | } |
| 1058 | |
| 1059 | bool ReductionCodeGen::needCleanups(unsigned N) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1060 | const auto *PrivateVD = |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1061 | cast<VarDecl>(cast<DeclRefExpr>(ClausesData[N].Private)->getDecl()); |
| 1062 | QualType PrivateType = PrivateVD->getType(); |
| 1063 | QualType::DestructionKind DTorKind = PrivateType.isDestructedType(); |
| 1064 | return DTorKind != QualType::DK_none; |
| 1065 | } |
| 1066 | |
| 1067 | void ReductionCodeGen::emitCleanups(CodeGenFunction &CGF, unsigned N, |
| 1068 | Address PrivateAddr) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1069 | const auto *PrivateVD = |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1070 | cast<VarDecl>(cast<DeclRefExpr>(ClausesData[N].Private)->getDecl()); |
| 1071 | QualType PrivateType = PrivateVD->getType(); |
| 1072 | QualType::DestructionKind DTorKind = PrivateType.isDestructedType(); |
| 1073 | if (needCleanups(N)) { |
| 1074 | PrivateAddr = CGF.Builder.CreateElementBitCast( |
| 1075 | PrivateAddr, CGF.ConvertTypeForMem(PrivateType)); |
| 1076 | CGF.pushDestroy(DTorKind, PrivateAddr, PrivateType); |
| 1077 | } |
| 1078 | } |
| 1079 | |
| 1080 | static LValue loadToBegin(CodeGenFunction &CGF, QualType BaseTy, QualType ElTy, |
| 1081 | LValue BaseLV) { |
| 1082 | BaseTy = BaseTy.getNonReferenceType(); |
| 1083 | while ((BaseTy->isPointerType() || BaseTy->isReferenceType()) && |
| 1084 | !CGF.getContext().hasSameType(BaseTy, ElTy)) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1085 | if (const auto *PtrTy = BaseTy->getAs<PointerType>()) { |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1086 | BaseLV = CGF.EmitLoadOfPointerLValue(BaseLV.getAddress(), PtrTy); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1087 | } else { |
Ivan A. Kosarev | 9f9d157 | 2017-10-30 11:49:31 +0000 | [diff] [blame] | 1088 | LValue RefLVal = CGF.MakeAddrLValue(BaseLV.getAddress(), BaseTy); |
| 1089 | BaseLV = CGF.EmitLoadOfReferenceLValue(RefLVal); |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1090 | } |
| 1091 | BaseTy = BaseTy->getPointeeType(); |
| 1092 | } |
| 1093 | return CGF.MakeAddrLValue( |
| 1094 | CGF.Builder.CreateElementBitCast(BaseLV.getAddress(), |
| 1095 | CGF.ConvertTypeForMem(ElTy)), |
Ivan A. Kosarev | f5f2046 | 2017-10-12 11:29:46 +0000 | [diff] [blame] | 1096 | BaseLV.getType(), BaseLV.getBaseInfo(), |
Ivan A. Kosarev | b9c59f3 | 2017-10-31 11:05:34 +0000 | [diff] [blame] | 1097 | CGF.CGM.getTBAAInfoForSubobject(BaseLV, BaseLV.getType())); |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1098 | } |
| 1099 | |
| 1100 | static Address castToBase(CodeGenFunction &CGF, QualType BaseTy, QualType ElTy, |
| 1101 | llvm::Type *BaseLVType, CharUnits BaseLVAlignment, |
| 1102 | llvm::Value *Addr) { |
| 1103 | Address Tmp = Address::invalid(); |
| 1104 | Address TopTmp = Address::invalid(); |
| 1105 | Address MostTopTmp = Address::invalid(); |
| 1106 | BaseTy = BaseTy.getNonReferenceType(); |
| 1107 | while ((BaseTy->isPointerType() || BaseTy->isReferenceType()) && |
| 1108 | !CGF.getContext().hasSameType(BaseTy, ElTy)) { |
| 1109 | Tmp = CGF.CreateMemTemp(BaseTy); |
| 1110 | if (TopTmp.isValid()) |
| 1111 | CGF.Builder.CreateStore(Tmp.getPointer(), TopTmp); |
| 1112 | else |
| 1113 | MostTopTmp = Tmp; |
| 1114 | TopTmp = Tmp; |
| 1115 | BaseTy = BaseTy->getPointeeType(); |
| 1116 | } |
| 1117 | llvm::Type *Ty = BaseLVType; |
| 1118 | if (Tmp.isValid()) |
| 1119 | Ty = Tmp.getElementType(); |
| 1120 | Addr = CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(Addr, Ty); |
| 1121 | if (Tmp.isValid()) { |
| 1122 | CGF.Builder.CreateStore(Addr, Tmp); |
| 1123 | return MostTopTmp; |
| 1124 | } |
| 1125 | return Address(Addr, BaseLVAlignment); |
| 1126 | } |
| 1127 | |
Alexey Bataev | 1c44e15 | 2018-03-06 18:59:43 +0000 | [diff] [blame] | 1128 | static const VarDecl *getBaseDecl(const Expr *Ref, const DeclRefExpr *&DE) { |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1129 | const VarDecl *OrigVD = nullptr; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1130 | if (const auto *OASE = dyn_cast<OMPArraySectionExpr>(Ref)) { |
| 1131 | const Expr *Base = OASE->getBase()->IgnoreParenImpCasts(); |
| 1132 | while (const auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base)) |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1133 | Base = TempOASE->getBase()->IgnoreParenImpCasts(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1134 | while (const auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base)) |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1135 | Base = TempASE->getBase()->IgnoreParenImpCasts(); |
| 1136 | DE = cast<DeclRefExpr>(Base); |
| 1137 | OrigVD = cast<VarDecl>(DE->getDecl()); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1138 | } else if (const auto *ASE = dyn_cast<ArraySubscriptExpr>(Ref)) { |
| 1139 | const Expr *Base = ASE->getBase()->IgnoreParenImpCasts(); |
| 1140 | while (const auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base)) |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1141 | Base = TempASE->getBase()->IgnoreParenImpCasts(); |
| 1142 | DE = cast<DeclRefExpr>(Base); |
| 1143 | OrigVD = cast<VarDecl>(DE->getDecl()); |
| 1144 | } |
Alexey Bataev | 1c44e15 | 2018-03-06 18:59:43 +0000 | [diff] [blame] | 1145 | return OrigVD; |
| 1146 | } |
| 1147 | |
| 1148 | Address ReductionCodeGen::adjustPrivateAddress(CodeGenFunction &CGF, unsigned N, |
| 1149 | Address PrivateAddr) { |
| 1150 | const DeclRefExpr *DE; |
| 1151 | if (const VarDecl *OrigVD = ::getBaseDecl(ClausesData[N].Ref, DE)) { |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1152 | BaseDecls.emplace_back(OrigVD); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1153 | LValue OriginalBaseLValue = CGF.EmitLValue(DE); |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1154 | LValue BaseLValue = |
| 1155 | loadToBegin(CGF, OrigVD->getType(), SharedAddresses[N].first.getType(), |
| 1156 | OriginalBaseLValue); |
| 1157 | llvm::Value *Adjustment = CGF.Builder.CreatePtrDiff( |
| 1158 | BaseLValue.getPointer(), SharedAddresses[N].first.getPointer()); |
Jonas Hahnfeld | 273d261 | 2017-12-06 19:15:28 +0000 | [diff] [blame] | 1159 | llvm::Value *PrivatePointer = |
| 1160 | CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
| 1161 | PrivateAddr.getPointer(), |
| 1162 | SharedAddresses[N].first.getAddress().getType()); |
| 1163 | llvm::Value *Ptr = CGF.Builder.CreateGEP(PrivatePointer, Adjustment); |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1164 | return castToBase(CGF, OrigVD->getType(), |
| 1165 | SharedAddresses[N].first.getType(), |
Jonas Hahnfeld | 273d261 | 2017-12-06 19:15:28 +0000 | [diff] [blame] | 1166 | OriginalBaseLValue.getAddress().getType(), |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1167 | OriginalBaseLValue.getAlignment(), Ptr); |
| 1168 | } |
| 1169 | BaseDecls.emplace_back( |
| 1170 | cast<VarDecl>(cast<DeclRefExpr>(ClausesData[N].Ref)->getDecl())); |
| 1171 | return PrivateAddr; |
| 1172 | } |
| 1173 | |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 1174 | bool ReductionCodeGen::usesReductionInitializer(unsigned N) const { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1175 | const OMPDeclareReductionDecl *DRD = |
| 1176 | getReductionInit(ClausesData[N].ReductionOp); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 1177 | return DRD && DRD->getInitializer(); |
| 1178 | } |
| 1179 | |
Alexey Bataev | 1809571 | 2014-10-10 12:19:54 +0000 | [diff] [blame] | 1180 | LValue CGOpenMPRegionInfo::getThreadIDVariableLValue(CodeGenFunction &CGF) { |
Alexey Bataev | 31300ed | 2016-02-04 11:27:03 +0000 | [diff] [blame] | 1181 | return CGF.EmitLoadOfPointerLValue( |
| 1182 | CGF.GetAddrOfLocalVar(getThreadIDVariable()), |
| 1183 | getThreadIDVariable()->getType()->castAs<PointerType>()); |
Alexey Bataev | 1809571 | 2014-10-10 12:19:54 +0000 | [diff] [blame] | 1184 | } |
| 1185 | |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 1186 | void CGOpenMPRegionInfo::EmitBody(CodeGenFunction &CGF, const Stmt * /*S*/) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 1187 | if (!CGF.HaveInsertPoint()) |
| 1188 | return; |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 1189 | // 1.2.2 OpenMP Language Terminology |
| 1190 | // Structured block - An executable statement with a single entry at the |
| 1191 | // top and a single exit at the bottom. |
| 1192 | // The point of exit cannot be a branch out of the structured block. |
| 1193 | // longjmp() and throw() must not violate the entry/exit criteria. |
| 1194 | CGF.EHStack.pushTerminate(); |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 1195 | CodeGen(CGF); |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 1196 | CGF.EHStack.popTerminate(); |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 1197 | } |
| 1198 | |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 1199 | LValue CGOpenMPTaskOutlinedRegionInfo::getThreadIDVariableLValue( |
| 1200 | CodeGenFunction &CGF) { |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 1201 | return CGF.MakeAddrLValue(CGF.GetAddrOfLocalVar(getThreadIDVariable()), |
| 1202 | getThreadIDVariable()->getType(), |
Ivan A. Kosarev | 5f8c0ca | 2017-10-10 09:39:32 +0000 | [diff] [blame] | 1203 | AlignmentSource::Decl); |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 1204 | } |
| 1205 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1206 | static FieldDecl *addFieldToRecordDecl(ASTContext &C, DeclContext *DC, |
| 1207 | QualType FieldTy) { |
| 1208 | auto *Field = FieldDecl::Create( |
| 1209 | C, DC, SourceLocation(), SourceLocation(), /*Id=*/nullptr, FieldTy, |
| 1210 | C.getTrivialTypeSourceInfo(FieldTy, SourceLocation()), |
| 1211 | /*BW=*/nullptr, /*Mutable=*/false, /*InitStyle=*/ICIS_NoInit); |
| 1212 | Field->setAccess(AS_public); |
| 1213 | DC->addDecl(Field); |
| 1214 | return Field; |
| 1215 | } |
| 1216 | |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 1217 | CGOpenMPRuntime::CGOpenMPRuntime(CodeGenModule &CGM, StringRef FirstSeparator, |
| 1218 | StringRef Separator) |
| 1219 | : CGM(CGM), FirstSeparator(FirstSeparator), Separator(Separator), |
| 1220 | OffloadEntriesInfoManager(CGM) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1221 | ASTContext &C = CGM.getContext(); |
| 1222 | RecordDecl *RD = C.buildImplicitRecord("ident_t"); |
| 1223 | QualType KmpInt32Ty = C.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1); |
| 1224 | RD->startDefinition(); |
| 1225 | // reserved_1 |
| 1226 | addFieldToRecordDecl(C, RD, KmpInt32Ty); |
| 1227 | // flags |
| 1228 | addFieldToRecordDecl(C, RD, KmpInt32Ty); |
| 1229 | // reserved_2 |
| 1230 | addFieldToRecordDecl(C, RD, KmpInt32Ty); |
| 1231 | // reserved_3 |
| 1232 | addFieldToRecordDecl(C, RD, KmpInt32Ty); |
| 1233 | // psource |
| 1234 | addFieldToRecordDecl(C, RD, C.VoidPtrTy); |
| 1235 | RD->completeDefinition(); |
| 1236 | IdentQTy = C.getRecordType(RD); |
| 1237 | IdentTy = CGM.getTypes().ConvertRecordDeclType(RD); |
Alexey Bataev | 3a3bf0b | 2014-09-22 10:01:53 +0000 | [diff] [blame] | 1238 | KmpCriticalNameTy = llvm::ArrayType::get(CGM.Int32Ty, /*NumElements*/ 8); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 1239 | |
| 1240 | loadOffloadInfoMetadata(); |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1241 | } |
| 1242 | |
Alexey Bataev | 9179755 | 2015-03-18 04:13:55 +0000 | [diff] [blame] | 1243 | void CGOpenMPRuntime::clear() { |
| 1244 | InternalVars.clear(); |
| 1245 | } |
| 1246 | |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 1247 | std::string CGOpenMPRuntime::getName(ArrayRef<StringRef> Parts) const { |
| 1248 | SmallString<128> Buffer; |
| 1249 | llvm::raw_svector_ostream OS(Buffer); |
| 1250 | StringRef Sep = FirstSeparator; |
| 1251 | for (StringRef Part : Parts) { |
| 1252 | OS << Sep << Part; |
| 1253 | Sep = Separator; |
| 1254 | } |
| 1255 | return OS.str(); |
| 1256 | } |
| 1257 | |
Alexey Bataev | c5b1d32 | 2016-03-04 09:22:22 +0000 | [diff] [blame] | 1258 | static llvm::Function * |
| 1259 | emitCombinerOrInitializer(CodeGenModule &CGM, QualType Ty, |
| 1260 | const Expr *CombinerInitializer, const VarDecl *In, |
| 1261 | const VarDecl *Out, bool IsCombiner) { |
| 1262 | // void .omp_combiner.(Ty *in, Ty *out); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1263 | ASTContext &C = CGM.getContext(); |
Alexey Bataev | c5b1d32 | 2016-03-04 09:22:22 +0000 | [diff] [blame] | 1264 | QualType PtrTy = C.getPointerType(Ty).withRestrict(); |
| 1265 | FunctionArgList Args; |
Alexey Bataev | c5b1d32 | 2016-03-04 09:22:22 +0000 | [diff] [blame] | 1266 | ImplicitParamDecl OmpOutParm(C, /*DC=*/nullptr, Out->getLocation(), |
Alexey Bataev | 5622323 | 2017-06-09 13:40:18 +0000 | [diff] [blame] | 1267 | /*Id=*/nullptr, PtrTy, ImplicitParamDecl::Other); |
Alexey Bataev | a839ddd | 2016-03-17 10:19:46 +0000 | [diff] [blame] | 1268 | ImplicitParamDecl OmpInParm(C, /*DC=*/nullptr, In->getLocation(), |
Alexey Bataev | 5622323 | 2017-06-09 13:40:18 +0000 | [diff] [blame] | 1269 | /*Id=*/nullptr, PtrTy, ImplicitParamDecl::Other); |
Alexey Bataev | c5b1d32 | 2016-03-04 09:22:22 +0000 | [diff] [blame] | 1270 | Args.push_back(&OmpOutParm); |
Alexey Bataev | a839ddd | 2016-03-17 10:19:46 +0000 | [diff] [blame] | 1271 | Args.push_back(&OmpInParm); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1272 | const CGFunctionInfo &FnInfo = |
John McCall | c56a8b3 | 2016-03-11 04:30:31 +0000 | [diff] [blame] | 1273 | CGM.getTypes().arrangeBuiltinFunctionDeclaration(C.VoidTy, Args); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1274 | llvm::FunctionType *FnTy = CGM.getTypes().GetFunctionType(FnInfo); |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 1275 | std::string Name = CGM.getOpenMPRuntime().getName( |
| 1276 | {IsCombiner ? "omp_combiner" : "omp_initializer", ""}); |
| 1277 | auto *Fn = llvm::Function::Create(FnTy, llvm::GlobalValue::InternalLinkage, |
| 1278 | Name, &CGM.getModule()); |
Rafael Espindola | 51ec5a9 | 2018-02-28 23:46:35 +0000 | [diff] [blame] | 1279 | CGM.SetInternalFunctionAttributes(GlobalDecl(), Fn, FnInfo); |
Chandler Carruth | fcd3314 | 2016-12-23 01:24:49 +0000 | [diff] [blame] | 1280 | Fn->removeFnAttr(llvm::Attribute::NoInline); |
Mehdi Amini | 6aa9e9b | 2017-05-29 05:38:20 +0000 | [diff] [blame] | 1281 | Fn->removeFnAttr(llvm::Attribute::OptimizeNone); |
Alexey Bataev | a839ddd | 2016-03-17 10:19:46 +0000 | [diff] [blame] | 1282 | Fn->addFnAttr(llvm::Attribute::AlwaysInline); |
Alexey Bataev | c5b1d32 | 2016-03-04 09:22:22 +0000 | [diff] [blame] | 1283 | CodeGenFunction CGF(CGM); |
| 1284 | // Map "T omp_in;" variable to "*omp_in_parm" value in all expressions. |
| 1285 | // 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] | 1286 | CGF.StartFunction(GlobalDecl(), C.VoidTy, Fn, FnInfo, Args, In->getLocation(), |
| 1287 | Out->getLocation()); |
Alexey Bataev | c5b1d32 | 2016-03-04 09:22:22 +0000 | [diff] [blame] | 1288 | CodeGenFunction::OMPPrivateScope Scope(CGF); |
| 1289 | Address AddrIn = CGF.GetAddrOfLocalVar(&OmpInParm); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1290 | Scope.addPrivate(In, [&CGF, AddrIn, PtrTy]() { |
Alexey Bataev | c5b1d32 | 2016-03-04 09:22:22 +0000 | [diff] [blame] | 1291 | return CGF.EmitLoadOfPointerLValue(AddrIn, PtrTy->castAs<PointerType>()) |
| 1292 | .getAddress(); |
| 1293 | }); |
| 1294 | Address AddrOut = CGF.GetAddrOfLocalVar(&OmpOutParm); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1295 | Scope.addPrivate(Out, [&CGF, AddrOut, PtrTy]() { |
Alexey Bataev | c5b1d32 | 2016-03-04 09:22:22 +0000 | [diff] [blame] | 1296 | return CGF.EmitLoadOfPointerLValue(AddrOut, PtrTy->castAs<PointerType>()) |
| 1297 | .getAddress(); |
| 1298 | }); |
| 1299 | (void)Scope.Privatize(); |
Alexey Bataev | 070f43a | 2017-09-06 14:49:58 +0000 | [diff] [blame] | 1300 | if (!IsCombiner && Out->hasInit() && |
| 1301 | !CGF.isTrivialInitializer(Out->getInit())) { |
| 1302 | CGF.EmitAnyExprToMem(Out->getInit(), CGF.GetAddrOfLocalVar(Out), |
| 1303 | Out->getType().getQualifiers(), |
| 1304 | /*IsInitializer=*/true); |
| 1305 | } |
| 1306 | if (CombinerInitializer) |
| 1307 | CGF.EmitIgnoredExpr(CombinerInitializer); |
Alexey Bataev | c5b1d32 | 2016-03-04 09:22:22 +0000 | [diff] [blame] | 1308 | Scope.ForceCleanup(); |
| 1309 | CGF.FinishFunction(); |
| 1310 | return Fn; |
| 1311 | } |
| 1312 | |
| 1313 | void CGOpenMPRuntime::emitUserDefinedReduction( |
| 1314 | CodeGenFunction *CGF, const OMPDeclareReductionDecl *D) { |
| 1315 | if (UDRMap.count(D) > 0) |
| 1316 | return; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1317 | ASTContext &C = CGM.getContext(); |
Alexey Bataev | c5b1d32 | 2016-03-04 09:22:22 +0000 | [diff] [blame] | 1318 | if (!In || !Out) { |
| 1319 | In = &C.Idents.get("omp_in"); |
| 1320 | Out = &C.Idents.get("omp_out"); |
| 1321 | } |
| 1322 | llvm::Function *Combiner = emitCombinerOrInitializer( |
| 1323 | CGM, D->getType(), D->getCombiner(), cast<VarDecl>(D->lookup(In).front()), |
| 1324 | cast<VarDecl>(D->lookup(Out).front()), |
| 1325 | /*IsCombiner=*/true); |
| 1326 | llvm::Function *Initializer = nullptr; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1327 | if (const Expr *Init = D->getInitializer()) { |
Alexey Bataev | c5b1d32 | 2016-03-04 09:22:22 +0000 | [diff] [blame] | 1328 | if (!Priv || !Orig) { |
| 1329 | Priv = &C.Idents.get("omp_priv"); |
| 1330 | Orig = &C.Idents.get("omp_orig"); |
| 1331 | } |
| 1332 | Initializer = emitCombinerOrInitializer( |
Alexey Bataev | 070f43a | 2017-09-06 14:49:58 +0000 | [diff] [blame] | 1333 | CGM, D->getType(), |
| 1334 | D->getInitializerKind() == OMPDeclareReductionDecl::CallInit ? Init |
| 1335 | : nullptr, |
| 1336 | cast<VarDecl>(D->lookup(Orig).front()), |
Alexey Bataev | c5b1d32 | 2016-03-04 09:22:22 +0000 | [diff] [blame] | 1337 | cast<VarDecl>(D->lookup(Priv).front()), |
| 1338 | /*IsCombiner=*/false); |
| 1339 | } |
Alexey Bataev | 43a919f | 2018-04-13 17:48:43 +0000 | [diff] [blame] | 1340 | UDRMap.try_emplace(D, Combiner, Initializer); |
Alexey Bataev | c5b1d32 | 2016-03-04 09:22:22 +0000 | [diff] [blame] | 1341 | if (CGF) { |
| 1342 | auto &Decls = FunctionUDRMap.FindAndConstruct(CGF->CurFn); |
| 1343 | Decls.second.push_back(D); |
| 1344 | } |
| 1345 | } |
| 1346 | |
Alexey Bataev | a839ddd | 2016-03-17 10:19:46 +0000 | [diff] [blame] | 1347 | std::pair<llvm::Function *, llvm::Function *> |
| 1348 | CGOpenMPRuntime::getUserDefinedReduction(const OMPDeclareReductionDecl *D) { |
| 1349 | auto I = UDRMap.find(D); |
| 1350 | if (I != UDRMap.end()) |
| 1351 | return I->second; |
| 1352 | emitUserDefinedReduction(/*CGF=*/nullptr, D); |
| 1353 | return UDRMap.lookup(D); |
| 1354 | } |
| 1355 | |
Arpith Chacko Jacob | 19b911c | 2017-01-18 18:18:53 +0000 | [diff] [blame] | 1356 | static llvm::Value *emitParallelOrTeamsOutlinedFunction( |
| 1357 | CodeGenModule &CGM, const OMPExecutableDirective &D, const CapturedStmt *CS, |
| 1358 | const VarDecl *ThreadIDVar, OpenMPDirectiveKind InnermostKind, |
| 1359 | const StringRef OutlinedHelperName, const RegionCodeGenTy &CodeGen) { |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 1360 | assert(ThreadIDVar->getType()->isPointerType() && |
| 1361 | "thread id variable must be of type kmp_int32 *"); |
Alexey Bataev | 1809571 | 2014-10-10 12:19:54 +0000 | [diff] [blame] | 1362 | CodeGenFunction CGF(CGM, true); |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 1363 | bool HasCancel = false; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1364 | if (const auto *OPD = dyn_cast<OMPParallelDirective>(&D)) |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 1365 | HasCancel = OPD->hasCancel(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1366 | else if (const auto *OPSD = dyn_cast<OMPParallelSectionsDirective>(&D)) |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 1367 | HasCancel = OPSD->hasCancel(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1368 | else if (const auto *OPFD = dyn_cast<OMPParallelForDirective>(&D)) |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 1369 | HasCancel = OPFD->hasCancel(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1370 | else if (const auto *OPFD = dyn_cast<OMPTargetParallelForDirective>(&D)) |
Alexey Bataev | 2139ed6 | 2017-11-16 18:20:21 +0000 | [diff] [blame] | 1371 | HasCancel = OPFD->hasCancel(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1372 | else if (const auto *OPFD = dyn_cast<OMPDistributeParallelForDirective>(&D)) |
Alexey Bataev | 10a5431 | 2017-11-27 16:54:08 +0000 | [diff] [blame] | 1373 | HasCancel = OPFD->hasCancel(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1374 | else if (const auto *OPFD = |
| 1375 | dyn_cast<OMPTeamsDistributeParallelForDirective>(&D)) |
Alexey Bataev | 10a5431 | 2017-11-27 16:54:08 +0000 | [diff] [blame] | 1376 | HasCancel = OPFD->hasCancel(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1377 | else if (const auto *OPFD = |
Alexey Bataev | 10a5431 | 2017-11-27 16:54:08 +0000 | [diff] [blame] | 1378 | dyn_cast<OMPTargetTeamsDistributeParallelForDirective>(&D)) |
| 1379 | HasCancel = OPFD->hasCancel(); |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 1380 | CGOpenMPOutlinedRegionInfo CGInfo(*CS, ThreadIDVar, CodeGen, InnermostKind, |
Arpith Chacko Jacob | 19b911c | 2017-01-18 18:18:53 +0000 | [diff] [blame] | 1381 | HasCancel, OutlinedHelperName); |
Alexey Bataev | d157d47 | 2015-06-24 03:35:38 +0000 | [diff] [blame] | 1382 | CodeGenFunction::CGCapturedStmtRAII CapInfoRAII(CGF, &CGInfo); |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 1383 | return CGF.GenerateOpenMPCapturedStmtFunction(*CS); |
Alexey Bataev | 1809571 | 2014-10-10 12:19:54 +0000 | [diff] [blame] | 1384 | } |
| 1385 | |
Arpith Chacko Jacob | 19b911c | 2017-01-18 18:18:53 +0000 | [diff] [blame] | 1386 | llvm::Value *CGOpenMPRuntime::emitParallelOutlinedFunction( |
| 1387 | const OMPExecutableDirective &D, const VarDecl *ThreadIDVar, |
| 1388 | OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen) { |
| 1389 | const CapturedStmt *CS = D.getCapturedStmt(OMPD_parallel); |
| 1390 | return emitParallelOrTeamsOutlinedFunction( |
| 1391 | CGM, D, CS, ThreadIDVar, InnermostKind, getOutlinedHelperName(), CodeGen); |
| 1392 | } |
| 1393 | |
| 1394 | llvm::Value *CGOpenMPRuntime::emitTeamsOutlinedFunction( |
| 1395 | const OMPExecutableDirective &D, const VarDecl *ThreadIDVar, |
| 1396 | OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen) { |
| 1397 | const CapturedStmt *CS = D.getCapturedStmt(OMPD_teams); |
| 1398 | return emitParallelOrTeamsOutlinedFunction( |
| 1399 | CGM, D, CS, ThreadIDVar, InnermostKind, getOutlinedHelperName(), CodeGen); |
| 1400 | } |
| 1401 | |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 1402 | llvm::Value *CGOpenMPRuntime::emitTaskOutlinedFunction( |
| 1403 | const OMPExecutableDirective &D, const VarDecl *ThreadIDVar, |
Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 1404 | const VarDecl *PartIDVar, const VarDecl *TaskTVar, |
| 1405 | OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen, |
| 1406 | bool Tied, unsigned &NumberOfParts) { |
| 1407 | auto &&UntiedCodeGen = [this, &D, TaskTVar](CodeGenFunction &CGF, |
| 1408 | PrePostActionTy &) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1409 | llvm::Value *ThreadID = getThreadID(CGF, D.getLocStart()); |
| 1410 | llvm::Value *UpLoc = emitUpdateLocation(CGF, D.getLocStart()); |
Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 1411 | llvm::Value *TaskArgs[] = { |
| 1412 | UpLoc, ThreadID, |
| 1413 | CGF.EmitLoadOfPointerLValue(CGF.GetAddrOfLocalVar(TaskTVar), |
| 1414 | TaskTVar->getType()->castAs<PointerType>()) |
| 1415 | .getPointer()}; |
| 1416 | CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_omp_task), TaskArgs); |
| 1417 | }; |
| 1418 | CGOpenMPTaskOutlinedRegionInfo::UntiedTaskActionTy Action(Tied, PartIDVar, |
| 1419 | UntiedCodeGen); |
| 1420 | CodeGen.setAction(Action); |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 1421 | assert(!ThreadIDVar->getType()->isPointerType() && |
| 1422 | "thread id variable must be of type kmp_int32 for tasks"); |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 1423 | const OpenMPDirectiveKind Region = |
| 1424 | isOpenMPTaskLoopDirective(D.getDirectiveKind()) ? OMPD_taskloop |
| 1425 | : OMPD_task; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1426 | const CapturedStmt *CS = D.getCapturedStmt(Region); |
| 1427 | const auto *TD = dyn_cast<OMPTaskDirective>(&D); |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 1428 | CodeGenFunction CGF(CGM, true); |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 1429 | CGOpenMPTaskOutlinedRegionInfo CGInfo(*CS, ThreadIDVar, CodeGen, |
| 1430 | InnermostKind, |
| 1431 | TD ? TD->hasCancel() : false, Action); |
Alexey Bataev | d157d47 | 2015-06-24 03:35:38 +0000 | [diff] [blame] | 1432 | CodeGenFunction::CGCapturedStmtRAII CapInfoRAII(CGF, &CGInfo); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1433 | llvm::Value *Res = CGF.GenerateCapturedStmtFunction(*CS); |
Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 1434 | if (!Tied) |
| 1435 | NumberOfParts = Action.getNumberOfParts(); |
| 1436 | return Res; |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 1437 | } |
| 1438 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1439 | static void buildStructValue(ConstantStructBuilder &Fields, CodeGenModule &CGM, |
| 1440 | const RecordDecl *RD, const CGRecordLayout &RL, |
| 1441 | ArrayRef<llvm::Constant *> Data) { |
| 1442 | llvm::StructType *StructTy = RL.getLLVMType(); |
| 1443 | unsigned PrevIdx = 0; |
| 1444 | ConstantInitBuilder CIBuilder(CGM); |
| 1445 | auto DI = Data.begin(); |
| 1446 | for (const FieldDecl *FD : RD->fields()) { |
| 1447 | unsigned Idx = RL.getLLVMFieldNo(FD); |
| 1448 | // Fill the alignment. |
| 1449 | for (unsigned I = PrevIdx; I < Idx; ++I) |
| 1450 | Fields.add(llvm::Constant::getNullValue(StructTy->getElementType(I))); |
| 1451 | PrevIdx = Idx + 1; |
| 1452 | Fields.add(*DI); |
| 1453 | ++DI; |
| 1454 | } |
| 1455 | } |
| 1456 | |
| 1457 | template <class... As> |
| 1458 | static llvm::GlobalVariable * |
| 1459 | createConstantGlobalStruct(CodeGenModule &CGM, QualType Ty, |
| 1460 | ArrayRef<llvm::Constant *> Data, const Twine &Name, |
| 1461 | As &&... Args) { |
| 1462 | const auto *RD = cast<RecordDecl>(Ty->getAsTagDecl()); |
| 1463 | const CGRecordLayout &RL = CGM.getTypes().getCGRecordLayout(RD); |
| 1464 | ConstantInitBuilder CIBuilder(CGM); |
| 1465 | ConstantStructBuilder Fields = CIBuilder.beginStruct(RL.getLLVMType()); |
| 1466 | buildStructValue(Fields, CGM, RD, RL, Data); |
| 1467 | return Fields.finishAndCreateGlobal( |
| 1468 | Name, CGM.getContext().getAlignOfGlobalVarInChars(Ty), |
| 1469 | /*isConstant=*/true, std::forward<As>(Args)...); |
| 1470 | } |
| 1471 | |
| 1472 | template <typename T> |
Benjamin Kramer | 651d0bf | 2018-05-15 21:26:47 +0000 | [diff] [blame] | 1473 | static void |
| 1474 | createConstantGlobalStructAndAddToParent(CodeGenModule &CGM, QualType Ty, |
| 1475 | ArrayRef<llvm::Constant *> Data, |
| 1476 | T &Parent) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1477 | const auto *RD = cast<RecordDecl>(Ty->getAsTagDecl()); |
| 1478 | const CGRecordLayout &RL = CGM.getTypes().getCGRecordLayout(RD); |
| 1479 | ConstantStructBuilder Fields = Parent.beginStruct(RL.getLLVMType()); |
| 1480 | buildStructValue(Fields, CGM, RD, RL, Data); |
| 1481 | Fields.finishAndAddTo(Parent); |
| 1482 | } |
| 1483 | |
Alexey Bataev | 50b3c95 | 2016-02-19 10:38:26 +0000 | [diff] [blame] | 1484 | Address CGOpenMPRuntime::getOrCreateDefaultLocation(unsigned Flags) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1485 | CharUnits Align = CGM.getContext().getTypeAlignInChars(IdentQTy); |
Alexey Bataev | 15007ba | 2014-05-07 06:18:01 +0000 | [diff] [blame] | 1486 | llvm::Value *Entry = OpenMPDefaultLocMap.lookup(Flags); |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1487 | if (!Entry) { |
| 1488 | if (!DefaultOpenMPPSource) { |
| 1489 | // Initialize default location for psource field of ident_t structure of |
| 1490 | // all ident_t objects. Format is ";file;function;line;column;;". |
| 1491 | // Taken from |
| 1492 | // http://llvm.org/svn/llvm-project/openmp/trunk/runtime/src/kmp_str.c |
| 1493 | DefaultOpenMPPSource = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1494 | CGM.GetAddrOfConstantCString(";unknown;unknown;0;0;;").getPointer(); |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1495 | DefaultOpenMPPSource = |
| 1496 | llvm::ConstantExpr::getBitCast(DefaultOpenMPPSource, CGM.Int8PtrTy); |
| 1497 | } |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1498 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1499 | llvm::Constant *Data[] = {llvm::ConstantInt::getNullValue(CGM.Int32Ty), |
| 1500 | llvm::ConstantInt::get(CGM.Int32Ty, Flags), |
| 1501 | llvm::ConstantInt::getNullValue(CGM.Int32Ty), |
| 1502 | llvm::ConstantInt::getNullValue(CGM.Int32Ty), |
| 1503 | DefaultOpenMPPSource}; |
| 1504 | llvm::GlobalValue *DefaultOpenMPLocation = createConstantGlobalStruct( |
| 1505 | CGM, IdentQTy, Data, "", llvm::GlobalValue::PrivateLinkage); |
| 1506 | DefaultOpenMPLocation->setUnnamedAddr( |
| 1507 | llvm::GlobalValue::UnnamedAddr::Global); |
John McCall | 6c9f1fdb | 2016-11-19 08:17:24 +0000 | [diff] [blame] | 1508 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1509 | OpenMPDefaultLocMap[Flags] = Entry = DefaultOpenMPLocation; |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1510 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1511 | return Address(Entry, Align); |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1512 | } |
| 1513 | |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 1514 | llvm::Value *CGOpenMPRuntime::emitUpdateLocation(CodeGenFunction &CGF, |
| 1515 | SourceLocation Loc, |
Alexey Bataev | 50b3c95 | 2016-02-19 10:38:26 +0000 | [diff] [blame] | 1516 | unsigned Flags) { |
| 1517 | Flags |= OMP_IDENT_KMPC; |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1518 | // If no debug info is generated - return global default location. |
Benjamin Kramer | 8c30592 | 2016-02-02 11:06:51 +0000 | [diff] [blame] | 1519 | if (CGM.getCodeGenOpts().getDebugInfo() == codegenoptions::NoDebugInfo || |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1520 | Loc.isInvalid()) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1521 | return getOrCreateDefaultLocation(Flags).getPointer(); |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1522 | |
| 1523 | assert(CGF.CurFn && "No function in current CodeGenFunction."); |
| 1524 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1525 | CharUnits Align = CGM.getContext().getTypeAlignInChars(IdentQTy); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1526 | Address LocValue = Address::invalid(); |
Alexey Bataev | 1e4b713 | 2014-12-03 12:11:24 +0000 | [diff] [blame] | 1527 | auto I = OpenMPLocThreadIDMap.find(CGF.CurFn); |
| 1528 | if (I != OpenMPLocThreadIDMap.end()) |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1529 | LocValue = Address(I->second.DebugLoc, Align); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1530 | |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 1531 | // OpenMPLocThreadIDMap may have null DebugLoc and non-null ThreadID, if |
| 1532 | // GetOpenMPThreadID was called before this routine. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1533 | if (!LocValue.isValid()) { |
Alexey Bataev | 15007ba | 2014-05-07 06:18:01 +0000 | [diff] [blame] | 1534 | // Generate "ident_t .kmpc_loc.addr;" |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1535 | Address AI = CGF.CreateMemTemp(IdentQTy, ".kmpc_loc.addr"); |
Alexey Bataev | 1809571 | 2014-10-10 12:19:54 +0000 | [diff] [blame] | 1536 | auto &Elem = OpenMPLocThreadIDMap.FindAndConstruct(CGF.CurFn); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1537 | Elem.second.DebugLoc = AI.getPointer(); |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1538 | LocValue = AI; |
| 1539 | |
| 1540 | CGBuilderTy::InsertPointGuard IPG(CGF.Builder); |
| 1541 | CGF.Builder.SetInsertPoint(CGF.AllocaInsertPt); |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 1542 | CGF.Builder.CreateMemCpy(LocValue, getOrCreateDefaultLocation(Flags), |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1543 | CGF.getTypeSize(IdentQTy)); |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1544 | } |
| 1545 | |
| 1546 | // char **psource = &.kmpc_loc_<flags>.addr.psource; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1547 | LValue Base = CGF.MakeAddrLValue(LocValue, IdentQTy); |
| 1548 | auto Fields = cast<RecordDecl>(IdentQTy->getAsTagDecl())->field_begin(); |
| 1549 | LValue PSource = |
| 1550 | CGF.EmitLValueForField(Base, *std::next(Fields, IdentField_PSource)); |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1551 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1552 | llvm::Value *OMPDebugLoc = OpenMPDebugLocMap.lookup(Loc.getRawEncoding()); |
Alexey Bataev | f002aca | 2014-05-30 05:48:40 +0000 | [diff] [blame] | 1553 | if (OMPDebugLoc == nullptr) { |
| 1554 | SmallString<128> Buffer2; |
| 1555 | llvm::raw_svector_ostream OS2(Buffer2); |
| 1556 | // Build debug location |
| 1557 | PresumedLoc PLoc = CGF.getContext().getSourceManager().getPresumedLoc(Loc); |
| 1558 | OS2 << ";" << PLoc.getFilename() << ";"; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1559 | if (const auto *FD = dyn_cast_or_null<FunctionDecl>(CGF.CurFuncDecl)) |
Alexey Bataev | f002aca | 2014-05-30 05:48:40 +0000 | [diff] [blame] | 1560 | OS2 << FD->getQualifiedNameAsString(); |
Alexey Bataev | f002aca | 2014-05-30 05:48:40 +0000 | [diff] [blame] | 1561 | OS2 << ";" << PLoc.getLine() << ";" << PLoc.getColumn() << ";;"; |
| 1562 | OMPDebugLoc = CGF.Builder.CreateGlobalStringPtr(OS2.str()); |
| 1563 | OpenMPDebugLocMap[Loc.getRawEncoding()] = OMPDebugLoc; |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1564 | } |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1565 | // *psource = ";<File>;<Function>;<Line>;<Column>;;"; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1566 | CGF.EmitStoreOfScalar(OMPDebugLoc, PSource); |
Alexey Bataev | f002aca | 2014-05-30 05:48:40 +0000 | [diff] [blame] | 1567 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1568 | // Our callers always pass this to a runtime function, so for |
| 1569 | // convenience, go ahead and return a naked pointer. |
| 1570 | return LocValue.getPointer(); |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1571 | } |
| 1572 | |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 1573 | llvm::Value *CGOpenMPRuntime::getThreadID(CodeGenFunction &CGF, |
| 1574 | SourceLocation Loc) { |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1575 | assert(CGF.CurFn && "No function in current CodeGenFunction."); |
| 1576 | |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 1577 | llvm::Value *ThreadID = nullptr; |
Alexey Bataev | 1809571 | 2014-10-10 12:19:54 +0000 | [diff] [blame] | 1578 | // Check whether we've already cached a load of the thread id in this |
| 1579 | // function. |
Alexey Bataev | 1e4b713 | 2014-12-03 12:11:24 +0000 | [diff] [blame] | 1580 | auto I = OpenMPLocThreadIDMap.find(CGF.CurFn); |
Alexey Bataev | 1809571 | 2014-10-10 12:19:54 +0000 | [diff] [blame] | 1581 | if (I != OpenMPLocThreadIDMap.end()) { |
| 1582 | ThreadID = I->second.ThreadID; |
Alexey Bataev | 03b340a | 2014-10-21 03:16:40 +0000 | [diff] [blame] | 1583 | if (ThreadID != nullptr) |
| 1584 | return ThreadID; |
| 1585 | } |
Alexey Bataev | aee1855 | 2017-08-16 14:01:00 +0000 | [diff] [blame] | 1586 | // If exceptions are enabled, do not use parameter to avoid possible crash. |
Alexey Bataev | 5d2c9a4 | 2017-11-02 18:55:05 +0000 | [diff] [blame] | 1587 | if (!CGF.EHStack.requiresLandingPad() || !CGF.getLangOpts().Exceptions || |
| 1588 | !CGF.getLangOpts().CXXExceptions || |
Alexey Bataev | 0e1b458 | 2017-11-02 14:25:34 +0000 | [diff] [blame] | 1589 | CGF.Builder.GetInsertBlock() == CGF.AllocaInsertPt->getParent()) { |
Alexey Bataev | aee1855 | 2017-08-16 14:01:00 +0000 | [diff] [blame] | 1590 | if (auto *OMPRegionInfo = |
| 1591 | dyn_cast_or_null<CGOpenMPRegionInfo>(CGF.CapturedStmtInfo)) { |
| 1592 | if (OMPRegionInfo->getThreadIDVariable()) { |
| 1593 | // Check if this an outlined function with thread id passed as argument. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1594 | LValue LVal = OMPRegionInfo->getThreadIDVariableLValue(CGF); |
Alexey Bataev | 1e49137 | 2018-01-23 18:44:14 +0000 | [diff] [blame] | 1595 | ThreadID = CGF.EmitLoadOfScalar(LVal, Loc); |
Alexey Bataev | aee1855 | 2017-08-16 14:01:00 +0000 | [diff] [blame] | 1596 | // If value loaded in entry block, cache it and use it everywhere in |
| 1597 | // function. |
| 1598 | if (CGF.Builder.GetInsertBlock() == CGF.AllocaInsertPt->getParent()) { |
| 1599 | auto &Elem = OpenMPLocThreadIDMap.FindAndConstruct(CGF.CurFn); |
| 1600 | Elem.second.ThreadID = ThreadID; |
| 1601 | } |
| 1602 | return ThreadID; |
Alexey Bataev | 8cbe0a6 | 2015-02-26 10:27:34 +0000 | [diff] [blame] | 1603 | } |
Alexey Bataev | d6c5755 | 2014-07-25 07:55:17 +0000 | [diff] [blame] | 1604 | } |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1605 | } |
Alexey Bataev | 8cbe0a6 | 2015-02-26 10:27:34 +0000 | [diff] [blame] | 1606 | |
| 1607 | // This is not an outlined function region - need to call __kmpc_int32 |
| 1608 | // kmpc_global_thread_num(ident_t *loc). |
| 1609 | // Generate thread id value and cache this value for use across the |
| 1610 | // function. |
| 1611 | CGBuilderTy::InsertPointGuard IPG(CGF.Builder); |
| 1612 | CGF.Builder.SetInsertPoint(CGF.AllocaInsertPt); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1613 | llvm::CallInst *Call = CGF.Builder.CreateCall( |
Alexey Bataev | 0e1b458 | 2017-11-02 14:25:34 +0000 | [diff] [blame] | 1614 | createRuntimeFunction(OMPRTL__kmpc_global_thread_num), |
| 1615 | emitUpdateLocation(CGF, Loc)); |
| 1616 | Call->setCallingConv(CGF.getRuntimeCC()); |
Alexey Bataev | 8cbe0a6 | 2015-02-26 10:27:34 +0000 | [diff] [blame] | 1617 | auto &Elem = OpenMPLocThreadIDMap.FindAndConstruct(CGF.CurFn); |
Alexey Bataev | 0e1b458 | 2017-11-02 14:25:34 +0000 | [diff] [blame] | 1618 | Elem.second.ThreadID = Call; |
| 1619 | return Call; |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1620 | } |
| 1621 | |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 1622 | void CGOpenMPRuntime::functionFinished(CodeGenFunction &CGF) { |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1623 | assert(CGF.CurFn && "No function in current CodeGenFunction."); |
Alexey Bataev | 03b340a | 2014-10-21 03:16:40 +0000 | [diff] [blame] | 1624 | if (OpenMPLocThreadIDMap.count(CGF.CurFn)) |
| 1625 | OpenMPLocThreadIDMap.erase(CGF.CurFn); |
Alexey Bataev | c5b1d32 | 2016-03-04 09:22:22 +0000 | [diff] [blame] | 1626 | if (FunctionUDRMap.count(CGF.CurFn) > 0) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1627 | for(auto *D : FunctionUDRMap[CGF.CurFn]) |
Alexey Bataev | c5b1d32 | 2016-03-04 09:22:22 +0000 | [diff] [blame] | 1628 | UDRMap.erase(D); |
Alexey Bataev | c5b1d32 | 2016-03-04 09:22:22 +0000 | [diff] [blame] | 1629 | FunctionUDRMap.erase(CGF.CurFn); |
| 1630 | } |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1631 | } |
| 1632 | |
| 1633 | llvm::Type *CGOpenMPRuntime::getIdentTyPointerTy() { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1634 | return IdentTy->getPointerTo(); |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1635 | } |
| 1636 | |
| 1637 | llvm::Type *CGOpenMPRuntime::getKmpc_MicroPointerTy() { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 1638 | if (!Kmpc_MicroTy) { |
| 1639 | // Build void (*kmpc_micro)(kmp_int32 *global_tid, kmp_int32 *bound_tid,...) |
| 1640 | llvm::Type *MicroParams[] = {llvm::PointerType::getUnqual(CGM.Int32Ty), |
| 1641 | llvm::PointerType::getUnqual(CGM.Int32Ty)}; |
| 1642 | Kmpc_MicroTy = llvm::FunctionType::get(CGM.VoidTy, MicroParams, true); |
| 1643 | } |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1644 | return llvm::PointerType::getUnqual(Kmpc_MicroTy); |
| 1645 | } |
| 1646 | |
| 1647 | llvm::Constant * |
Alexey Bataev | 50b3c95 | 2016-02-19 10:38:26 +0000 | [diff] [blame] | 1648 | CGOpenMPRuntime::createRuntimeFunction(unsigned Function) { |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1649 | llvm::Constant *RTLFn = nullptr; |
Alexey Bataev | 50b3c95 | 2016-02-19 10:38:26 +0000 | [diff] [blame] | 1650 | switch (static_cast<OpenMPRTLFunction>(Function)) { |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1651 | case OMPRTL__kmpc_fork_call: { |
| 1652 | // Build void __kmpc_fork_call(ident_t *loc, kmp_int32 argc, kmpc_micro |
| 1653 | // microtask, ...); |
Alexey Bataev | 23b6942 | 2014-06-18 07:08:49 +0000 | [diff] [blame] | 1654 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty, |
| 1655 | getKmpc_MicroPointerTy()}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1656 | auto *FnTy = |
Alexey Bataev | d74d060 | 2014-10-13 06:02:40 +0000 | [diff] [blame] | 1657 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ true); |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1658 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_fork_call"); |
| 1659 | break; |
| 1660 | } |
| 1661 | case OMPRTL__kmpc_global_thread_num: { |
| 1662 | // Build kmp_int32 __kmpc_global_thread_num(ident_t *loc); |
Alexey Bataev | 23b6942 | 2014-06-18 07:08:49 +0000 | [diff] [blame] | 1663 | llvm::Type *TypeParams[] = {getIdentTyPointerTy()}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1664 | auto *FnTy = |
Alexey Bataev | d74d060 | 2014-10-13 06:02:40 +0000 | [diff] [blame] | 1665 | llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg*/ false); |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1666 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_global_thread_num"); |
| 1667 | break; |
| 1668 | } |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 1669 | case OMPRTL__kmpc_threadprivate_cached: { |
| 1670 | // Build void *__kmpc_threadprivate_cached(ident_t *loc, |
| 1671 | // kmp_int32 global_tid, void *data, size_t size, void ***cache); |
| 1672 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty, |
| 1673 | CGM.VoidPtrTy, CGM.SizeTy, |
| 1674 | CGM.VoidPtrTy->getPointerTo()->getPointerTo()}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1675 | auto *FnTy = |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 1676 | llvm::FunctionType::get(CGM.VoidPtrTy, TypeParams, /*isVarArg*/ false); |
| 1677 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_threadprivate_cached"); |
| 1678 | break; |
| 1679 | } |
Alexey Bataev | 3a3bf0b | 2014-09-22 10:01:53 +0000 | [diff] [blame] | 1680 | case OMPRTL__kmpc_critical: { |
Alexey Bataev | f947218 | 2014-09-22 12:32:31 +0000 | [diff] [blame] | 1681 | // Build void __kmpc_critical(ident_t *loc, kmp_int32 global_tid, |
| 1682 | // kmp_critical_name *crit); |
Alexey Bataev | 3a3bf0b | 2014-09-22 10:01:53 +0000 | [diff] [blame] | 1683 | llvm::Type *TypeParams[] = { |
| 1684 | getIdentTyPointerTy(), CGM.Int32Ty, |
| 1685 | llvm::PointerType::getUnqual(KmpCriticalNameTy)}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1686 | auto *FnTy = |
Alexey Bataev | 3a3bf0b | 2014-09-22 10:01:53 +0000 | [diff] [blame] | 1687 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false); |
| 1688 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_critical"); |
| 1689 | break; |
| 1690 | } |
Alexey Bataev | fc57d16 | 2015-12-15 10:55:09 +0000 | [diff] [blame] | 1691 | case OMPRTL__kmpc_critical_with_hint: { |
| 1692 | // Build void __kmpc_critical_with_hint(ident_t *loc, kmp_int32 global_tid, |
| 1693 | // kmp_critical_name *crit, uintptr_t hint); |
| 1694 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty, |
| 1695 | llvm::PointerType::getUnqual(KmpCriticalNameTy), |
| 1696 | CGM.IntPtrTy}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1697 | auto *FnTy = |
Alexey Bataev | fc57d16 | 2015-12-15 10:55:09 +0000 | [diff] [blame] | 1698 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false); |
| 1699 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_critical_with_hint"); |
| 1700 | break; |
| 1701 | } |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 1702 | case OMPRTL__kmpc_threadprivate_register: { |
| 1703 | // Build void __kmpc_threadprivate_register(ident_t *, void *data, |
| 1704 | // kmpc_ctor ctor, kmpc_cctor cctor, kmpc_dtor dtor); |
| 1705 | // typedef void *(*kmpc_ctor)(void *); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1706 | auto *KmpcCtorTy = |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 1707 | llvm::FunctionType::get(CGM.VoidPtrTy, CGM.VoidPtrTy, |
| 1708 | /*isVarArg*/ false)->getPointerTo(); |
| 1709 | // typedef void *(*kmpc_cctor)(void *, void *); |
| 1710 | llvm::Type *KmpcCopyCtorTyArgs[] = {CGM.VoidPtrTy, CGM.VoidPtrTy}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1711 | auto *KmpcCopyCtorTy = |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 1712 | llvm::FunctionType::get(CGM.VoidPtrTy, KmpcCopyCtorTyArgs, |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1713 | /*isVarArg*/ false) |
| 1714 | ->getPointerTo(); |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 1715 | // typedef void (*kmpc_dtor)(void *); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1716 | auto *KmpcDtorTy = |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 1717 | llvm::FunctionType::get(CGM.VoidTy, CGM.VoidPtrTy, /*isVarArg*/ false) |
| 1718 | ->getPointerTo(); |
| 1719 | llvm::Type *FnTyArgs[] = {getIdentTyPointerTy(), CGM.VoidPtrTy, KmpcCtorTy, |
| 1720 | KmpcCopyCtorTy, KmpcDtorTy}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1721 | auto *FnTy = llvm::FunctionType::get(CGM.VoidTy, FnTyArgs, |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 1722 | /*isVarArg*/ false); |
| 1723 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_threadprivate_register"); |
| 1724 | break; |
| 1725 | } |
Alexey Bataev | 3a3bf0b | 2014-09-22 10:01:53 +0000 | [diff] [blame] | 1726 | case OMPRTL__kmpc_end_critical: { |
Alexey Bataev | f947218 | 2014-09-22 12:32:31 +0000 | [diff] [blame] | 1727 | // Build void __kmpc_end_critical(ident_t *loc, kmp_int32 global_tid, |
| 1728 | // kmp_critical_name *crit); |
Alexey Bataev | 3a3bf0b | 2014-09-22 10:01:53 +0000 | [diff] [blame] | 1729 | llvm::Type *TypeParams[] = { |
| 1730 | getIdentTyPointerTy(), CGM.Int32Ty, |
| 1731 | llvm::PointerType::getUnqual(KmpCriticalNameTy)}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1732 | auto *FnTy = |
Alexey Bataev | 3a3bf0b | 2014-09-22 10:01:53 +0000 | [diff] [blame] | 1733 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false); |
| 1734 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_end_critical"); |
| 1735 | break; |
| 1736 | } |
Alexey Bataev | 8f7c1b0 | 2014-12-05 04:09:23 +0000 | [diff] [blame] | 1737 | case OMPRTL__kmpc_cancel_barrier: { |
| 1738 | // Build kmp_int32 __kmpc_cancel_barrier(ident_t *loc, kmp_int32 |
| 1739 | // global_tid); |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 1740 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1741 | auto *FnTy = |
Alexey Bataev | 8f7c1b0 | 2014-12-05 04:09:23 +0000 | [diff] [blame] | 1742 | llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg*/ false); |
| 1743 | RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name*/ "__kmpc_cancel_barrier"); |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 1744 | break; |
| 1745 | } |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 1746 | case OMPRTL__kmpc_barrier: { |
Alexey Bataev | 7d5d33e | 2015-07-06 05:50:32 +0000 | [diff] [blame] | 1747 | // Build void __kmpc_barrier(ident_t *loc, kmp_int32 global_tid); |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 1748 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1749 | auto *FnTy = |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 1750 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false); |
| 1751 | RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name*/ "__kmpc_barrier"); |
| 1752 | break; |
| 1753 | } |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 1754 | case OMPRTL__kmpc_for_static_fini: { |
| 1755 | // Build void __kmpc_for_static_fini(ident_t *loc, kmp_int32 global_tid); |
| 1756 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1757 | auto *FnTy = |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 1758 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false); |
| 1759 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_for_static_fini"); |
| 1760 | break; |
| 1761 | } |
Alexey Bataev | b205978 | 2014-10-13 08:23:51 +0000 | [diff] [blame] | 1762 | case OMPRTL__kmpc_push_num_threads: { |
| 1763 | // Build void __kmpc_push_num_threads(ident_t *loc, kmp_int32 global_tid, |
| 1764 | // kmp_int32 num_threads) |
| 1765 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty, |
| 1766 | CGM.Int32Ty}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1767 | auto *FnTy = |
Alexey Bataev | b205978 | 2014-10-13 08:23:51 +0000 | [diff] [blame] | 1768 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false); |
| 1769 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_push_num_threads"); |
| 1770 | break; |
| 1771 | } |
Alexey Bataev | d74d060 | 2014-10-13 06:02:40 +0000 | [diff] [blame] | 1772 | case OMPRTL__kmpc_serialized_parallel: { |
| 1773 | // Build void __kmpc_serialized_parallel(ident_t *loc, kmp_int32 |
| 1774 | // global_tid); |
| 1775 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1776 | auto *FnTy = |
Alexey Bataev | d74d060 | 2014-10-13 06:02:40 +0000 | [diff] [blame] | 1777 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false); |
| 1778 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_serialized_parallel"); |
| 1779 | break; |
| 1780 | } |
| 1781 | case OMPRTL__kmpc_end_serialized_parallel: { |
| 1782 | // Build void __kmpc_end_serialized_parallel(ident_t *loc, kmp_int32 |
| 1783 | // global_tid); |
| 1784 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1785 | auto *FnTy = |
Alexey Bataev | d74d060 | 2014-10-13 06:02:40 +0000 | [diff] [blame] | 1786 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false); |
| 1787 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_end_serialized_parallel"); |
| 1788 | break; |
| 1789 | } |
Alexey Bataev | cc37cc1 | 2014-11-20 04:34:54 +0000 | [diff] [blame] | 1790 | case OMPRTL__kmpc_flush: { |
Alexey Bataev | d76df6d | 2015-02-24 12:55:09 +0000 | [diff] [blame] | 1791 | // Build void __kmpc_flush(ident_t *loc); |
Alexey Bataev | cc37cc1 | 2014-11-20 04:34:54 +0000 | [diff] [blame] | 1792 | llvm::Type *TypeParams[] = {getIdentTyPointerTy()}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1793 | auto *FnTy = |
Alexey Bataev | d76df6d | 2015-02-24 12:55:09 +0000 | [diff] [blame] | 1794 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false); |
Alexey Bataev | cc37cc1 | 2014-11-20 04:34:54 +0000 | [diff] [blame] | 1795 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_flush"); |
| 1796 | break; |
| 1797 | } |
Alexey Bataev | 8d69065 | 2014-12-04 07:23:53 +0000 | [diff] [blame] | 1798 | case OMPRTL__kmpc_master: { |
| 1799 | // Build kmp_int32 __kmpc_master(ident_t *loc, kmp_int32 global_tid); |
| 1800 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1801 | auto *FnTy = |
Alexey Bataev | 8d69065 | 2014-12-04 07:23:53 +0000 | [diff] [blame] | 1802 | llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg=*/false); |
| 1803 | RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_master"); |
| 1804 | break; |
| 1805 | } |
| 1806 | case OMPRTL__kmpc_end_master: { |
| 1807 | // Build void __kmpc_end_master(ident_t *loc, kmp_int32 global_tid); |
| 1808 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1809 | auto *FnTy = |
Alexey Bataev | 8d69065 | 2014-12-04 07:23:53 +0000 | [diff] [blame] | 1810 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false); |
| 1811 | RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_end_master"); |
| 1812 | break; |
| 1813 | } |
Alexey Bataev | 9f797f3 | 2015-02-05 05:57:51 +0000 | [diff] [blame] | 1814 | case OMPRTL__kmpc_omp_taskyield: { |
| 1815 | // Build kmp_int32 __kmpc_omp_taskyield(ident_t *, kmp_int32 global_tid, |
| 1816 | // int end_part); |
| 1817 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty, CGM.IntTy}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1818 | auto *FnTy = |
Alexey Bataev | 9f797f3 | 2015-02-05 05:57:51 +0000 | [diff] [blame] | 1819 | llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg=*/false); |
| 1820 | RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_omp_taskyield"); |
| 1821 | break; |
| 1822 | } |
Alexey Bataev | 6956e2e | 2015-02-05 06:35:41 +0000 | [diff] [blame] | 1823 | case OMPRTL__kmpc_single: { |
| 1824 | // Build kmp_int32 __kmpc_single(ident_t *loc, kmp_int32 global_tid); |
| 1825 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1826 | auto *FnTy = |
Alexey Bataev | 6956e2e | 2015-02-05 06:35:41 +0000 | [diff] [blame] | 1827 | llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg=*/false); |
| 1828 | RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_single"); |
| 1829 | break; |
| 1830 | } |
| 1831 | case OMPRTL__kmpc_end_single: { |
| 1832 | // Build void __kmpc_end_single(ident_t *loc, kmp_int32 global_tid); |
| 1833 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1834 | auto *FnTy = |
Alexey Bataev | 6956e2e | 2015-02-05 06:35:41 +0000 | [diff] [blame] | 1835 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false); |
| 1836 | RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_end_single"); |
| 1837 | break; |
| 1838 | } |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 1839 | case OMPRTL__kmpc_omp_task_alloc: { |
| 1840 | // Build kmp_task_t *__kmpc_omp_task_alloc(ident_t *, kmp_int32 gtid, |
| 1841 | // kmp_int32 flags, size_t sizeof_kmp_task_t, size_t sizeof_shareds, |
| 1842 | // kmp_routine_entry_t *task_entry); |
| 1843 | assert(KmpRoutineEntryPtrTy != nullptr && |
| 1844 | "Type kmp_routine_entry_t must be created."); |
| 1845 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty, CGM.Int32Ty, |
| 1846 | CGM.SizeTy, CGM.SizeTy, KmpRoutineEntryPtrTy}; |
| 1847 | // Return void * and then cast to particular kmp_task_t type. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1848 | auto *FnTy = |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 1849 | llvm::FunctionType::get(CGM.VoidPtrTy, TypeParams, /*isVarArg=*/false); |
| 1850 | RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_omp_task_alloc"); |
| 1851 | break; |
| 1852 | } |
| 1853 | case OMPRTL__kmpc_omp_task: { |
| 1854 | // Build kmp_int32 __kmpc_omp_task(ident_t *, kmp_int32 gtid, kmp_task_t |
| 1855 | // *new_task); |
| 1856 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty, |
| 1857 | CGM.VoidPtrTy}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1858 | auto *FnTy = |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 1859 | llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg=*/false); |
| 1860 | RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_omp_task"); |
| 1861 | break; |
| 1862 | } |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 1863 | case OMPRTL__kmpc_copyprivate: { |
| 1864 | // Build void __kmpc_copyprivate(ident_t *loc, kmp_int32 global_tid, |
Alexey Bataev | 66beaa9 | 2015-04-30 03:47:32 +0000 | [diff] [blame] | 1865 | // size_t cpy_size, void *cpy_data, void(*cpy_func)(void *, void *), |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 1866 | // kmp_int32 didit); |
| 1867 | llvm::Type *CpyTypeParams[] = {CGM.VoidPtrTy, CGM.VoidPtrTy}; |
| 1868 | auto *CpyFnTy = |
| 1869 | llvm::FunctionType::get(CGM.VoidTy, CpyTypeParams, /*isVarArg=*/false); |
Alexey Bataev | 66beaa9 | 2015-04-30 03:47:32 +0000 | [diff] [blame] | 1870 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty, CGM.SizeTy, |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 1871 | CGM.VoidPtrTy, CpyFnTy->getPointerTo(), |
| 1872 | CGM.Int32Ty}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1873 | auto *FnTy = |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 1874 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false); |
| 1875 | RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_copyprivate"); |
| 1876 | break; |
| 1877 | } |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 1878 | case OMPRTL__kmpc_reduce: { |
| 1879 | // Build kmp_int32 __kmpc_reduce(ident_t *loc, kmp_int32 global_tid, |
| 1880 | // kmp_int32 num_vars, size_t reduce_size, void *reduce_data, void |
| 1881 | // (*reduce_func)(void *lhs_data, void *rhs_data), kmp_critical_name *lck); |
| 1882 | llvm::Type *ReduceTypeParams[] = {CGM.VoidPtrTy, CGM.VoidPtrTy}; |
| 1883 | auto *ReduceFnTy = llvm::FunctionType::get(CGM.VoidTy, ReduceTypeParams, |
| 1884 | /*isVarArg=*/false); |
| 1885 | llvm::Type *TypeParams[] = { |
| 1886 | getIdentTyPointerTy(), CGM.Int32Ty, CGM.Int32Ty, CGM.SizeTy, |
| 1887 | CGM.VoidPtrTy, ReduceFnTy->getPointerTo(), |
| 1888 | llvm::PointerType::getUnqual(KmpCriticalNameTy)}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1889 | auto *FnTy = |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 1890 | llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg=*/false); |
| 1891 | RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_reduce"); |
| 1892 | break; |
| 1893 | } |
| 1894 | case OMPRTL__kmpc_reduce_nowait: { |
| 1895 | // Build kmp_int32 __kmpc_reduce_nowait(ident_t *loc, kmp_int32 |
| 1896 | // global_tid, kmp_int32 num_vars, size_t reduce_size, void *reduce_data, |
| 1897 | // void (*reduce_func)(void *lhs_data, void *rhs_data), kmp_critical_name |
| 1898 | // *lck); |
| 1899 | llvm::Type *ReduceTypeParams[] = {CGM.VoidPtrTy, CGM.VoidPtrTy}; |
| 1900 | auto *ReduceFnTy = llvm::FunctionType::get(CGM.VoidTy, ReduceTypeParams, |
| 1901 | /*isVarArg=*/false); |
| 1902 | llvm::Type *TypeParams[] = { |
| 1903 | getIdentTyPointerTy(), CGM.Int32Ty, CGM.Int32Ty, CGM.SizeTy, |
| 1904 | CGM.VoidPtrTy, ReduceFnTy->getPointerTo(), |
| 1905 | llvm::PointerType::getUnqual(KmpCriticalNameTy)}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1906 | auto *FnTy = |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 1907 | llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg=*/false); |
| 1908 | RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_reduce_nowait"); |
| 1909 | break; |
| 1910 | } |
| 1911 | case OMPRTL__kmpc_end_reduce: { |
| 1912 | // Build void __kmpc_end_reduce(ident_t *loc, kmp_int32 global_tid, |
| 1913 | // kmp_critical_name *lck); |
| 1914 | llvm::Type *TypeParams[] = { |
| 1915 | getIdentTyPointerTy(), CGM.Int32Ty, |
| 1916 | llvm::PointerType::getUnqual(KmpCriticalNameTy)}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1917 | auto *FnTy = |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 1918 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false); |
| 1919 | RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_end_reduce"); |
| 1920 | break; |
| 1921 | } |
| 1922 | case OMPRTL__kmpc_end_reduce_nowait: { |
| 1923 | // Build __kmpc_end_reduce_nowait(ident_t *loc, kmp_int32 global_tid, |
| 1924 | // kmp_critical_name *lck); |
| 1925 | llvm::Type *TypeParams[] = { |
| 1926 | getIdentTyPointerTy(), CGM.Int32Ty, |
| 1927 | llvm::PointerType::getUnqual(KmpCriticalNameTy)}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1928 | auto *FnTy = |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 1929 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false); |
| 1930 | RTLFn = |
| 1931 | CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_end_reduce_nowait"); |
| 1932 | break; |
| 1933 | } |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 1934 | case OMPRTL__kmpc_omp_task_begin_if0: { |
| 1935 | // Build void __kmpc_omp_task(ident_t *, kmp_int32 gtid, kmp_task_t |
| 1936 | // *new_task); |
| 1937 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty, |
| 1938 | CGM.VoidPtrTy}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1939 | auto *FnTy = |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 1940 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false); |
| 1941 | RTLFn = |
| 1942 | CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_omp_task_begin_if0"); |
| 1943 | break; |
| 1944 | } |
| 1945 | case OMPRTL__kmpc_omp_task_complete_if0: { |
| 1946 | // Build void __kmpc_omp_task(ident_t *, kmp_int32 gtid, kmp_task_t |
| 1947 | // *new_task); |
| 1948 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty, |
| 1949 | CGM.VoidPtrTy}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1950 | auto *FnTy = |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 1951 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false); |
| 1952 | RTLFn = CGM.CreateRuntimeFunction(FnTy, |
| 1953 | /*Name=*/"__kmpc_omp_task_complete_if0"); |
| 1954 | break; |
| 1955 | } |
Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 1956 | case OMPRTL__kmpc_ordered: { |
| 1957 | // Build void __kmpc_ordered(ident_t *loc, kmp_int32 global_tid); |
| 1958 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1959 | auto *FnTy = |
Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 1960 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false); |
| 1961 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_ordered"); |
| 1962 | break; |
| 1963 | } |
| 1964 | case OMPRTL__kmpc_end_ordered: { |
Alexey Bataev | c30dd2d | 2015-06-18 12:14:09 +0000 | [diff] [blame] | 1965 | // Build void __kmpc_end_ordered(ident_t *loc, kmp_int32 global_tid); |
Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 1966 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1967 | auto *FnTy = |
Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 1968 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false); |
| 1969 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_end_ordered"); |
| 1970 | break; |
| 1971 | } |
Alexey Bataev | 8b8e202 | 2015-04-27 05:22:09 +0000 | [diff] [blame] | 1972 | case OMPRTL__kmpc_omp_taskwait: { |
| 1973 | // Build kmp_int32 __kmpc_omp_taskwait(ident_t *loc, kmp_int32 global_tid); |
| 1974 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1975 | auto *FnTy = |
Alexey Bataev | 8b8e202 | 2015-04-27 05:22:09 +0000 | [diff] [blame] | 1976 | llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg=*/false); |
| 1977 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_omp_taskwait"); |
| 1978 | break; |
| 1979 | } |
Alexey Bataev | c30dd2d | 2015-06-18 12:14:09 +0000 | [diff] [blame] | 1980 | case OMPRTL__kmpc_taskgroup: { |
| 1981 | // Build void __kmpc_taskgroup(ident_t *loc, kmp_int32 global_tid); |
| 1982 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1983 | auto *FnTy = |
Alexey Bataev | c30dd2d | 2015-06-18 12:14:09 +0000 | [diff] [blame] | 1984 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false); |
| 1985 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_taskgroup"); |
| 1986 | break; |
| 1987 | } |
| 1988 | case OMPRTL__kmpc_end_taskgroup: { |
| 1989 | // Build void __kmpc_end_taskgroup(ident_t *loc, kmp_int32 global_tid); |
| 1990 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1991 | auto *FnTy = |
Alexey Bataev | c30dd2d | 2015-06-18 12:14:09 +0000 | [diff] [blame] | 1992 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false); |
| 1993 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_end_taskgroup"); |
| 1994 | break; |
| 1995 | } |
Alexey Bataev | 7f210c6 | 2015-06-18 13:40:03 +0000 | [diff] [blame] | 1996 | case OMPRTL__kmpc_push_proc_bind: { |
| 1997 | // Build void __kmpc_push_proc_bind(ident_t *loc, kmp_int32 global_tid, |
| 1998 | // int proc_bind) |
| 1999 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty, CGM.IntTy}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2000 | auto *FnTy = |
Alexey Bataev | 7f210c6 | 2015-06-18 13:40:03 +0000 | [diff] [blame] | 2001 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false); |
| 2002 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_push_proc_bind"); |
| 2003 | break; |
| 2004 | } |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 2005 | case OMPRTL__kmpc_omp_task_with_deps: { |
| 2006 | // Build kmp_int32 __kmpc_omp_task_with_deps(ident_t *, kmp_int32 gtid, |
| 2007 | // kmp_task_t *new_task, kmp_int32 ndeps, kmp_depend_info_t *dep_list, |
| 2008 | // kmp_int32 ndeps_noalias, kmp_depend_info_t *noalias_dep_list); |
| 2009 | llvm::Type *TypeParams[] = { |
| 2010 | getIdentTyPointerTy(), CGM.Int32Ty, CGM.VoidPtrTy, CGM.Int32Ty, |
| 2011 | CGM.VoidPtrTy, CGM.Int32Ty, CGM.VoidPtrTy}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2012 | auto *FnTy = |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 2013 | llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg=*/false); |
| 2014 | RTLFn = |
| 2015 | CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_omp_task_with_deps"); |
| 2016 | break; |
| 2017 | } |
| 2018 | case OMPRTL__kmpc_omp_wait_deps: { |
| 2019 | // Build void __kmpc_omp_wait_deps(ident_t *, kmp_int32 gtid, |
| 2020 | // kmp_int32 ndeps, kmp_depend_info_t *dep_list, kmp_int32 ndeps_noalias, |
| 2021 | // kmp_depend_info_t *noalias_dep_list); |
| 2022 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty, |
| 2023 | CGM.Int32Ty, CGM.VoidPtrTy, |
| 2024 | CGM.Int32Ty, CGM.VoidPtrTy}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2025 | auto *FnTy = |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 2026 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false); |
| 2027 | RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_omp_wait_deps"); |
| 2028 | break; |
| 2029 | } |
Alexey Bataev | 0f34da1 | 2015-07-02 04:17:07 +0000 | [diff] [blame] | 2030 | case OMPRTL__kmpc_cancellationpoint: { |
| 2031 | // Build kmp_int32 __kmpc_cancellationpoint(ident_t *loc, kmp_int32 |
| 2032 | // global_tid, kmp_int32 cncl_kind) |
| 2033 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty, CGM.IntTy}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2034 | auto *FnTy = |
Alexey Bataev | 0f34da1 | 2015-07-02 04:17:07 +0000 | [diff] [blame] | 2035 | llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg*/ false); |
| 2036 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_cancellationpoint"); |
| 2037 | break; |
| 2038 | } |
Alexey Bataev | 7d5d33e | 2015-07-06 05:50:32 +0000 | [diff] [blame] | 2039 | case OMPRTL__kmpc_cancel: { |
| 2040 | // Build kmp_int32 __kmpc_cancel(ident_t *loc, kmp_int32 global_tid, |
| 2041 | // kmp_int32 cncl_kind) |
| 2042 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty, CGM.IntTy}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2043 | auto *FnTy = |
Alexey Bataev | 7d5d33e | 2015-07-06 05:50:32 +0000 | [diff] [blame] | 2044 | llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg*/ false); |
| 2045 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_cancel"); |
| 2046 | break; |
| 2047 | } |
Carlo Bertolli | 430d8ec | 2016-03-03 20:34:23 +0000 | [diff] [blame] | 2048 | case OMPRTL__kmpc_push_num_teams: { |
| 2049 | // Build void kmpc_push_num_teams (ident_t loc, kmp_int32 global_tid, |
| 2050 | // kmp_int32 num_teams, kmp_int32 num_threads) |
| 2051 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty, CGM.Int32Ty, |
| 2052 | CGM.Int32Ty}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2053 | auto *FnTy = |
Carlo Bertolli | 430d8ec | 2016-03-03 20:34:23 +0000 | [diff] [blame] | 2054 | llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg*/ false); |
| 2055 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_push_num_teams"); |
| 2056 | break; |
| 2057 | } |
| 2058 | case OMPRTL__kmpc_fork_teams: { |
| 2059 | // Build void __kmpc_fork_teams(ident_t *loc, kmp_int32 argc, kmpc_micro |
| 2060 | // microtask, ...); |
| 2061 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty, |
| 2062 | getKmpc_MicroPointerTy()}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2063 | auto *FnTy = |
Carlo Bertolli | 430d8ec | 2016-03-03 20:34:23 +0000 | [diff] [blame] | 2064 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ true); |
| 2065 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_fork_teams"); |
| 2066 | break; |
| 2067 | } |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 2068 | case OMPRTL__kmpc_taskloop: { |
| 2069 | // Build void __kmpc_taskloop(ident_t *loc, int gtid, kmp_task_t *task, int |
| 2070 | // if_val, kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st, int nogroup, int |
| 2071 | // sched, kmp_uint64 grainsize, void *task_dup); |
| 2072 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), |
| 2073 | CGM.IntTy, |
| 2074 | CGM.VoidPtrTy, |
| 2075 | CGM.IntTy, |
| 2076 | CGM.Int64Ty->getPointerTo(), |
| 2077 | CGM.Int64Ty->getPointerTo(), |
| 2078 | CGM.Int64Ty, |
| 2079 | CGM.IntTy, |
| 2080 | CGM.IntTy, |
| 2081 | CGM.Int64Ty, |
| 2082 | CGM.VoidPtrTy}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2083 | auto *FnTy = |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 2084 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false); |
| 2085 | RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_taskloop"); |
| 2086 | break; |
| 2087 | } |
Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 2088 | case OMPRTL__kmpc_doacross_init: { |
| 2089 | // Build void __kmpc_doacross_init(ident_t *loc, kmp_int32 gtid, kmp_int32 |
| 2090 | // num_dims, struct kmp_dim *dims); |
| 2091 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), |
| 2092 | CGM.Int32Ty, |
| 2093 | CGM.Int32Ty, |
| 2094 | CGM.VoidPtrTy}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2095 | auto *FnTy = |
Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 2096 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false); |
| 2097 | RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_doacross_init"); |
| 2098 | break; |
| 2099 | } |
| 2100 | case OMPRTL__kmpc_doacross_fini: { |
| 2101 | // Build void __kmpc_doacross_fini(ident_t *loc, kmp_int32 gtid); |
| 2102 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2103 | auto *FnTy = |
Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 2104 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false); |
| 2105 | RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_doacross_fini"); |
| 2106 | break; |
| 2107 | } |
| 2108 | case OMPRTL__kmpc_doacross_post: { |
| 2109 | // Build void __kmpc_doacross_post(ident_t *loc, kmp_int32 gtid, kmp_int64 |
| 2110 | // *vec); |
| 2111 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty, |
| 2112 | CGM.Int64Ty->getPointerTo()}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2113 | auto *FnTy = |
Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 2114 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false); |
| 2115 | RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_doacross_post"); |
| 2116 | break; |
| 2117 | } |
| 2118 | case OMPRTL__kmpc_doacross_wait: { |
| 2119 | // Build void __kmpc_doacross_wait(ident_t *loc, kmp_int32 gtid, kmp_int64 |
| 2120 | // *vec); |
| 2121 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty, |
| 2122 | CGM.Int64Ty->getPointerTo()}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2123 | auto *FnTy = |
Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 2124 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false); |
| 2125 | RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_doacross_wait"); |
| 2126 | break; |
| 2127 | } |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 2128 | case OMPRTL__kmpc_task_reduction_init: { |
| 2129 | // Build void *__kmpc_task_reduction_init(int gtid, int num_data, void |
| 2130 | // *data); |
| 2131 | llvm::Type *TypeParams[] = {CGM.IntTy, CGM.IntTy, CGM.VoidPtrTy}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2132 | auto *FnTy = |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 2133 | llvm::FunctionType::get(CGM.VoidPtrTy, TypeParams, /*isVarArg=*/false); |
| 2134 | RTLFn = |
| 2135 | CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_task_reduction_init"); |
| 2136 | break; |
| 2137 | } |
| 2138 | case OMPRTL__kmpc_task_reduction_get_th_data: { |
| 2139 | // Build void *__kmpc_task_reduction_get_th_data(int gtid, void *tg, void |
| 2140 | // *d); |
| 2141 | llvm::Type *TypeParams[] = {CGM.IntTy, CGM.VoidPtrTy, CGM.VoidPtrTy}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2142 | auto *FnTy = |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 2143 | llvm::FunctionType::get(CGM.VoidPtrTy, TypeParams, /*isVarArg=*/false); |
| 2144 | RTLFn = CGM.CreateRuntimeFunction( |
| 2145 | FnTy, /*Name=*/"__kmpc_task_reduction_get_th_data"); |
| 2146 | break; |
| 2147 | } |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 2148 | case OMPRTL__tgt_target: { |
George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 2149 | // Build int32_t __tgt_target(int64_t device_id, void *host_ptr, int32_t |
| 2150 | // 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] | 2151 | // *arg_types); |
George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 2152 | llvm::Type *TypeParams[] = {CGM.Int64Ty, |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 2153 | CGM.VoidPtrTy, |
| 2154 | CGM.Int32Ty, |
| 2155 | CGM.VoidPtrPtrTy, |
| 2156 | CGM.VoidPtrPtrTy, |
| 2157 | CGM.SizeTy->getPointerTo(), |
George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 2158 | CGM.Int64Ty->getPointerTo()}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2159 | auto *FnTy = |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 2160 | llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg*/ false); |
| 2161 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__tgt_target"); |
| 2162 | break; |
| 2163 | } |
Alexey Bataev | a9f77c6 | 2017-12-13 21:04:20 +0000 | [diff] [blame] | 2164 | case OMPRTL__tgt_target_nowait: { |
| 2165 | // Build int32_t __tgt_target_nowait(int64_t device_id, void *host_ptr, |
| 2166 | // int32_t arg_num, void** args_base, void **args, size_t *arg_sizes, |
| 2167 | // int64_t *arg_types); |
| 2168 | llvm::Type *TypeParams[] = {CGM.Int64Ty, |
| 2169 | CGM.VoidPtrTy, |
| 2170 | CGM.Int32Ty, |
| 2171 | CGM.VoidPtrPtrTy, |
| 2172 | CGM.VoidPtrPtrTy, |
| 2173 | CGM.SizeTy->getPointerTo(), |
| 2174 | CGM.Int64Ty->getPointerTo()}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2175 | auto *FnTy = |
Alexey Bataev | a9f77c6 | 2017-12-13 21:04:20 +0000 | [diff] [blame] | 2176 | llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg*/ false); |
| 2177 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__tgt_target_nowait"); |
| 2178 | break; |
| 2179 | } |
Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 2180 | case OMPRTL__tgt_target_teams: { |
George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 2181 | // 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] | 2182 | // 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] | 2183 | // int64_t *arg_types, int32_t num_teams, int32_t thread_limit); |
| 2184 | llvm::Type *TypeParams[] = {CGM.Int64Ty, |
Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 2185 | CGM.VoidPtrTy, |
| 2186 | CGM.Int32Ty, |
| 2187 | CGM.VoidPtrPtrTy, |
| 2188 | CGM.VoidPtrPtrTy, |
| 2189 | CGM.SizeTy->getPointerTo(), |
George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 2190 | CGM.Int64Ty->getPointerTo(), |
Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 2191 | CGM.Int32Ty, |
| 2192 | CGM.Int32Ty}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2193 | auto *FnTy = |
Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 2194 | llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg*/ false); |
| 2195 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__tgt_target_teams"); |
| 2196 | break; |
| 2197 | } |
Alexey Bataev | a9f77c6 | 2017-12-13 21:04:20 +0000 | [diff] [blame] | 2198 | case OMPRTL__tgt_target_teams_nowait: { |
| 2199 | // Build int32_t __tgt_target_teams_nowait(int64_t device_id, void |
| 2200 | // *host_ptr, int32_t arg_num, void** args_base, void **args, size_t |
| 2201 | // *arg_sizes, int64_t *arg_types, int32_t num_teams, int32_t thread_limit); |
| 2202 | llvm::Type *TypeParams[] = {CGM.Int64Ty, |
| 2203 | CGM.VoidPtrTy, |
| 2204 | CGM.Int32Ty, |
| 2205 | CGM.VoidPtrPtrTy, |
| 2206 | CGM.VoidPtrPtrTy, |
| 2207 | CGM.SizeTy->getPointerTo(), |
| 2208 | CGM.Int64Ty->getPointerTo(), |
| 2209 | CGM.Int32Ty, |
| 2210 | CGM.Int32Ty}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2211 | auto *FnTy = |
Alexey Bataev | a9f77c6 | 2017-12-13 21:04:20 +0000 | [diff] [blame] | 2212 | llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg*/ false); |
| 2213 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__tgt_target_teams_nowait"); |
| 2214 | break; |
| 2215 | } |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 2216 | case OMPRTL__tgt_register_lib: { |
| 2217 | // Build void __tgt_register_lib(__tgt_bin_desc *desc); |
| 2218 | QualType ParamTy = |
| 2219 | CGM.getContext().getPointerType(getTgtBinaryDescriptorQTy()); |
| 2220 | llvm::Type *TypeParams[] = {CGM.getTypes().ConvertTypeForMem(ParamTy)}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2221 | auto *FnTy = |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 2222 | llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg*/ false); |
| 2223 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__tgt_register_lib"); |
| 2224 | break; |
| 2225 | } |
| 2226 | case OMPRTL__tgt_unregister_lib: { |
| 2227 | // Build void __tgt_unregister_lib(__tgt_bin_desc *desc); |
| 2228 | QualType ParamTy = |
| 2229 | CGM.getContext().getPointerType(getTgtBinaryDescriptorQTy()); |
| 2230 | llvm::Type *TypeParams[] = {CGM.getTypes().ConvertTypeForMem(ParamTy)}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2231 | auto *FnTy = |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 2232 | llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg*/ false); |
| 2233 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__tgt_unregister_lib"); |
| 2234 | break; |
| 2235 | } |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 2236 | case OMPRTL__tgt_target_data_begin: { |
George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 2237 | // Build void __tgt_target_data_begin(int64_t device_id, int32_t arg_num, |
| 2238 | // void** args_base, void **args, size_t *arg_sizes, int64_t *arg_types); |
| 2239 | llvm::Type *TypeParams[] = {CGM.Int64Ty, |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 2240 | CGM.Int32Ty, |
| 2241 | CGM.VoidPtrPtrTy, |
| 2242 | CGM.VoidPtrPtrTy, |
| 2243 | CGM.SizeTy->getPointerTo(), |
George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 2244 | CGM.Int64Ty->getPointerTo()}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2245 | auto *FnTy = |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 2246 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false); |
| 2247 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__tgt_target_data_begin"); |
| 2248 | break; |
| 2249 | } |
Alexey Bataev | 0cc6b8e | 2017-12-14 17:00:17 +0000 | [diff] [blame] | 2250 | case OMPRTL__tgt_target_data_begin_nowait: { |
| 2251 | // Build void __tgt_target_data_begin_nowait(int64_t device_id, int32_t |
| 2252 | // arg_num, void** args_base, void **args, size_t *arg_sizes, int64_t |
| 2253 | // *arg_types); |
| 2254 | llvm::Type *TypeParams[] = {CGM.Int64Ty, |
| 2255 | CGM.Int32Ty, |
| 2256 | CGM.VoidPtrPtrTy, |
| 2257 | CGM.VoidPtrPtrTy, |
| 2258 | CGM.SizeTy->getPointerTo(), |
| 2259 | CGM.Int64Ty->getPointerTo()}; |
| 2260 | auto *FnTy = |
| 2261 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false); |
| 2262 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__tgt_target_data_begin_nowait"); |
| 2263 | break; |
| 2264 | } |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 2265 | case OMPRTL__tgt_target_data_end: { |
George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 2266 | // Build void __tgt_target_data_end(int64_t device_id, int32_t arg_num, |
| 2267 | // void** args_base, void **args, size_t *arg_sizes, int64_t *arg_types); |
| 2268 | llvm::Type *TypeParams[] = {CGM.Int64Ty, |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 2269 | CGM.Int32Ty, |
| 2270 | CGM.VoidPtrPtrTy, |
| 2271 | CGM.VoidPtrPtrTy, |
| 2272 | CGM.SizeTy->getPointerTo(), |
George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 2273 | CGM.Int64Ty->getPointerTo()}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2274 | auto *FnTy = |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 2275 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false); |
| 2276 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__tgt_target_data_end"); |
| 2277 | break; |
| 2278 | } |
Alexey Bataev | 0cc6b8e | 2017-12-14 17:00:17 +0000 | [diff] [blame] | 2279 | case OMPRTL__tgt_target_data_end_nowait: { |
| 2280 | // Build void __tgt_target_data_end_nowait(int64_t device_id, int32_t |
| 2281 | // arg_num, void** args_base, void **args, size_t *arg_sizes, int64_t |
| 2282 | // *arg_types); |
| 2283 | llvm::Type *TypeParams[] = {CGM.Int64Ty, |
| 2284 | CGM.Int32Ty, |
| 2285 | CGM.VoidPtrPtrTy, |
| 2286 | CGM.VoidPtrPtrTy, |
| 2287 | CGM.SizeTy->getPointerTo(), |
| 2288 | CGM.Int64Ty->getPointerTo()}; |
| 2289 | auto *FnTy = |
| 2290 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false); |
| 2291 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__tgt_target_data_end_nowait"); |
| 2292 | break; |
| 2293 | } |
Samuel Antao | 8d2d730 | 2016-05-26 18:30:22 +0000 | [diff] [blame] | 2294 | case OMPRTL__tgt_target_data_update: { |
George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 2295 | // Build void __tgt_target_data_update(int64_t device_id, int32_t arg_num, |
| 2296 | // void** args_base, void **args, size_t *arg_sizes, int64_t *arg_types); |
| 2297 | llvm::Type *TypeParams[] = {CGM.Int64Ty, |
Samuel Antao | 8d2d730 | 2016-05-26 18:30:22 +0000 | [diff] [blame] | 2298 | CGM.Int32Ty, |
| 2299 | CGM.VoidPtrPtrTy, |
| 2300 | CGM.VoidPtrPtrTy, |
| 2301 | CGM.SizeTy->getPointerTo(), |
George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 2302 | CGM.Int64Ty->getPointerTo()}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2303 | auto *FnTy = |
Samuel Antao | 8d2d730 | 2016-05-26 18:30:22 +0000 | [diff] [blame] | 2304 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false); |
| 2305 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__tgt_target_data_update"); |
| 2306 | break; |
| 2307 | } |
Alexey Bataev | 0cc6b8e | 2017-12-14 17:00:17 +0000 | [diff] [blame] | 2308 | case OMPRTL__tgt_target_data_update_nowait: { |
| 2309 | // Build void __tgt_target_data_update_nowait(int64_t device_id, int32_t |
| 2310 | // arg_num, void** args_base, void **args, size_t *arg_sizes, int64_t |
| 2311 | // *arg_types); |
| 2312 | llvm::Type *TypeParams[] = {CGM.Int64Ty, |
| 2313 | CGM.Int32Ty, |
| 2314 | CGM.VoidPtrPtrTy, |
| 2315 | CGM.VoidPtrPtrTy, |
| 2316 | CGM.SizeTy->getPointerTo(), |
| 2317 | CGM.Int64Ty->getPointerTo()}; |
| 2318 | auto *FnTy = |
| 2319 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false); |
| 2320 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__tgt_target_data_update_nowait"); |
| 2321 | break; |
| 2322 | } |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 2323 | } |
Alexey Bataev | 50b3c95 | 2016-02-19 10:38:26 +0000 | [diff] [blame] | 2324 | assert(RTLFn && "Unable to find OpenMP runtime function"); |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 2325 | return RTLFn; |
| 2326 | } |
Alexey Bataev | 3a3bf0b | 2014-09-22 10:01:53 +0000 | [diff] [blame] | 2327 | |
Alexander Musman | 21212e4 | 2015-03-13 10:38:23 +0000 | [diff] [blame] | 2328 | llvm::Constant *CGOpenMPRuntime::createForStaticInitFunction(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 = IVSize == 32 ? (IVSigned ? "__kmpc_for_static_init_4" |
| 2333 | : "__kmpc_for_static_init_4u") |
| 2334 | : (IVSigned ? "__kmpc_for_static_init_8" |
| 2335 | : "__kmpc_for_static_init_8u"); |
| 2336 | llvm::Type *ITy = IVSize == 32 ? CGM.Int32Ty : CGM.Int64Ty; |
| 2337 | auto *PtrTy = llvm::PointerType::getUnqual(ITy); |
Alexander Musman | 21212e4 | 2015-03-13 10:38:23 +0000 | [diff] [blame] | 2338 | llvm::Type *TypeParams[] = { |
| 2339 | getIdentTyPointerTy(), // loc |
| 2340 | CGM.Int32Ty, // tid |
| 2341 | CGM.Int32Ty, // schedtype |
| 2342 | llvm::PointerType::getUnqual(CGM.Int32Ty), // p_lastiter |
| 2343 | PtrTy, // p_lower |
| 2344 | PtrTy, // p_upper |
| 2345 | PtrTy, // p_stride |
| 2346 | ITy, // incr |
| 2347 | ITy // chunk |
| 2348 | }; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2349 | auto *FnTy = |
Alexander Musman | 21212e4 | 2015-03-13 10:38:23 +0000 | [diff] [blame] | 2350 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false); |
| 2351 | return CGM.CreateRuntimeFunction(FnTy, Name); |
| 2352 | } |
| 2353 | |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 2354 | llvm::Constant *CGOpenMPRuntime::createDispatchInitFunction(unsigned IVSize, |
| 2355 | bool IVSigned) { |
| 2356 | assert((IVSize == 32 || IVSize == 64) && |
| 2357 | "IV size is not compatible with the omp runtime"); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2358 | StringRef Name = |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 2359 | IVSize == 32 |
| 2360 | ? (IVSigned ? "__kmpc_dispatch_init_4" : "__kmpc_dispatch_init_4u") |
| 2361 | : (IVSigned ? "__kmpc_dispatch_init_8" : "__kmpc_dispatch_init_8u"); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2362 | llvm::Type *ITy = IVSize == 32 ? CGM.Int32Ty : CGM.Int64Ty; |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 2363 | llvm::Type *TypeParams[] = { getIdentTyPointerTy(), // loc |
| 2364 | CGM.Int32Ty, // tid |
| 2365 | CGM.Int32Ty, // schedtype |
| 2366 | ITy, // lower |
| 2367 | ITy, // upper |
| 2368 | ITy, // stride |
| 2369 | ITy // chunk |
| 2370 | }; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2371 | auto *FnTy = |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 2372 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false); |
| 2373 | return CGM.CreateRuntimeFunction(FnTy, Name); |
| 2374 | } |
| 2375 | |
Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 2376 | llvm::Constant *CGOpenMPRuntime::createDispatchFiniFunction(unsigned IVSize, |
| 2377 | bool IVSigned) { |
| 2378 | assert((IVSize == 32 || IVSize == 64) && |
| 2379 | "IV size is not compatible with the omp runtime"); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2380 | StringRef Name = |
Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 2381 | IVSize == 32 |
| 2382 | ? (IVSigned ? "__kmpc_dispatch_fini_4" : "__kmpc_dispatch_fini_4u") |
| 2383 | : (IVSigned ? "__kmpc_dispatch_fini_8" : "__kmpc_dispatch_fini_8u"); |
| 2384 | llvm::Type *TypeParams[] = { |
| 2385 | getIdentTyPointerTy(), // loc |
| 2386 | CGM.Int32Ty, // tid |
| 2387 | }; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2388 | auto *FnTy = |
Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 2389 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false); |
| 2390 | return CGM.CreateRuntimeFunction(FnTy, Name); |
| 2391 | } |
| 2392 | |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 2393 | llvm::Constant *CGOpenMPRuntime::createDispatchNextFunction(unsigned IVSize, |
| 2394 | bool IVSigned) { |
| 2395 | assert((IVSize == 32 || IVSize == 64) && |
| 2396 | "IV size is not compatible with the omp runtime"); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2397 | StringRef Name = |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 2398 | IVSize == 32 |
| 2399 | ? (IVSigned ? "__kmpc_dispatch_next_4" : "__kmpc_dispatch_next_4u") |
| 2400 | : (IVSigned ? "__kmpc_dispatch_next_8" : "__kmpc_dispatch_next_8u"); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2401 | llvm::Type *ITy = IVSize == 32 ? CGM.Int32Ty : CGM.Int64Ty; |
| 2402 | auto *PtrTy = llvm::PointerType::getUnqual(ITy); |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 2403 | llvm::Type *TypeParams[] = { |
| 2404 | getIdentTyPointerTy(), // loc |
| 2405 | CGM.Int32Ty, // tid |
| 2406 | llvm::PointerType::getUnqual(CGM.Int32Ty), // p_lastiter |
| 2407 | PtrTy, // p_lower |
| 2408 | PtrTy, // p_upper |
| 2409 | PtrTy // p_stride |
| 2410 | }; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2411 | auto *FnTy = |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 2412 | llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg*/ false); |
| 2413 | return CGM.CreateRuntimeFunction(FnTy, Name); |
| 2414 | } |
| 2415 | |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 2416 | Address CGOpenMPRuntime::getAddrOfDeclareTargetLink(const VarDecl *VD) { |
| 2417 | if (CGM.getLangOpts().OpenMPSimd) |
| 2418 | return Address::invalid(); |
Alexey Bataev | 92327c5 | 2018-03-26 16:40:55 +0000 | [diff] [blame] | 2419 | llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res = |
| 2420 | isDeclareTargetDeclaration(VD); |
| 2421 | if (Res && *Res == OMPDeclareTargetDeclAttr::MT_Link) { |
| 2422 | SmallString<64> PtrName; |
| 2423 | { |
| 2424 | llvm::raw_svector_ostream OS(PtrName); |
| 2425 | OS << CGM.getMangledName(GlobalDecl(VD)) << "_decl_tgt_link_ptr"; |
| 2426 | } |
| 2427 | llvm::Value *Ptr = CGM.getModule().getNamedValue(PtrName); |
| 2428 | if (!Ptr) { |
| 2429 | QualType PtrTy = CGM.getContext().getPointerType(VD->getType()); |
| 2430 | Ptr = getOrCreateInternalVariable(CGM.getTypes().ConvertTypeForMem(PtrTy), |
| 2431 | PtrName); |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 2432 | if (!CGM.getLangOpts().OpenMPIsDevice) { |
| 2433 | auto *GV = cast<llvm::GlobalVariable>(Ptr); |
| 2434 | GV->setLinkage(llvm::GlobalValue::ExternalLinkage); |
| 2435 | GV->setInitializer(CGM.GetAddrOfGlobal(VD)); |
| 2436 | } |
| 2437 | CGM.addUsedGlobal(cast<llvm::GlobalValue>(Ptr)); |
| 2438 | registerTargetGlobalVariable(VD, cast<llvm::Constant>(Ptr)); |
Alexey Bataev | 92327c5 | 2018-03-26 16:40:55 +0000 | [diff] [blame] | 2439 | } |
| 2440 | return Address(Ptr, CGM.getContext().getDeclAlign(VD)); |
| 2441 | } |
| 2442 | return Address::invalid(); |
| 2443 | } |
| 2444 | |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2445 | llvm::Constant * |
| 2446 | CGOpenMPRuntime::getOrCreateThreadPrivateCache(const VarDecl *VD) { |
Samuel Antao | f8b5012 | 2015-07-13 22:54:53 +0000 | [diff] [blame] | 2447 | assert(!CGM.getLangOpts().OpenMPUseTLS || |
| 2448 | !CGM.getContext().getTargetInfo().isTLSSupported()); |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2449 | // Lookup the entry, lazily creating it if necessary. |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 2450 | std::string Suffix = getName({"cache", ""}); |
| 2451 | return getOrCreateInternalVariable( |
| 2452 | CGM.Int8PtrPtrTy, Twine(CGM.getMangledName(VD)).concat(Suffix)); |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2453 | } |
| 2454 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2455 | Address CGOpenMPRuntime::getAddrOfThreadPrivate(CodeGenFunction &CGF, |
| 2456 | const VarDecl *VD, |
| 2457 | Address VDAddr, |
| 2458 | SourceLocation Loc) { |
Samuel Antao | f8b5012 | 2015-07-13 22:54:53 +0000 | [diff] [blame] | 2459 | if (CGM.getLangOpts().OpenMPUseTLS && |
| 2460 | CGM.getContext().getTargetInfo().isTLSSupported()) |
| 2461 | return VDAddr; |
| 2462 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2463 | llvm::Type *VarTy = VDAddr.getElementType(); |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 2464 | llvm::Value *Args[] = {emitUpdateLocation(CGF, Loc), getThreadID(CGF, Loc), |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2465 | CGF.Builder.CreatePointerCast(VDAddr.getPointer(), |
| 2466 | CGM.Int8PtrTy), |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2467 | CGM.getSize(CGM.GetTargetTypeStoreSize(VarTy)), |
| 2468 | getOrCreateThreadPrivateCache(VD)}; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2469 | return Address(CGF.EmitRuntimeCall( |
| 2470 | createRuntimeFunction(OMPRTL__kmpc_threadprivate_cached), Args), |
| 2471 | VDAddr.getAlignment()); |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2472 | } |
| 2473 | |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 2474 | void CGOpenMPRuntime::emitThreadPrivateVarInit( |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2475 | CodeGenFunction &CGF, Address VDAddr, llvm::Value *Ctor, |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2476 | llvm::Value *CopyCtor, llvm::Value *Dtor, SourceLocation Loc) { |
| 2477 | // Call kmp_int32 __kmpc_global_thread_num(&loc) to init OpenMP runtime |
| 2478 | // library. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2479 | llvm::Value *OMPLoc = emitUpdateLocation(CGF, Loc); |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 2480 | CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_global_thread_num), |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2481 | OMPLoc); |
| 2482 | // Call __kmpc_threadprivate_register(&loc, &var, ctor, cctor/*NULL*/, dtor) |
| 2483 | // to register constructor/destructor for variable. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2484 | llvm::Value *Args[] = { |
| 2485 | OMPLoc, CGF.Builder.CreatePointerCast(VDAddr.getPointer(), CGM.VoidPtrTy), |
| 2486 | Ctor, CopyCtor, Dtor}; |
Alexey Bataev | 1e4b713 | 2014-12-03 12:11:24 +0000 | [diff] [blame] | 2487 | CGF.EmitRuntimeCall( |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 2488 | createRuntimeFunction(OMPRTL__kmpc_threadprivate_register), Args); |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2489 | } |
| 2490 | |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 2491 | llvm::Function *CGOpenMPRuntime::emitThreadPrivateVarDefinition( |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2492 | const VarDecl *VD, Address VDAddr, SourceLocation Loc, |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2493 | bool PerformInit, CodeGenFunction *CGF) { |
Samuel Antao | f8b5012 | 2015-07-13 22:54:53 +0000 | [diff] [blame] | 2494 | if (CGM.getLangOpts().OpenMPUseTLS && |
| 2495 | CGM.getContext().getTargetInfo().isTLSSupported()) |
| 2496 | return nullptr; |
| 2497 | |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2498 | VD = VD->getDefinition(CGM.getContext()); |
| 2499 | if (VD && ThreadPrivateWithDefinition.count(VD) == 0) { |
| 2500 | ThreadPrivateWithDefinition.insert(VD); |
| 2501 | QualType ASTTy = VD->getType(); |
| 2502 | |
| 2503 | llvm::Value *Ctor = nullptr, *CopyCtor = nullptr, *Dtor = nullptr; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2504 | const Expr *Init = VD->getAnyInitializer(); |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2505 | if (CGM.getLangOpts().CPlusPlus && PerformInit) { |
| 2506 | // Generate function that re-emits the declaration's initializer into the |
| 2507 | // threadprivate copy of the variable VD |
| 2508 | CodeGenFunction CtorCGF(CGM); |
| 2509 | FunctionArgList Args; |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 2510 | ImplicitParamDecl Dst(CGM.getContext(), /*DC=*/nullptr, Loc, |
| 2511 | /*Id=*/nullptr, CGM.getContext().VoidPtrTy, |
Alexey Bataev | 5622323 | 2017-06-09 13:40:18 +0000 | [diff] [blame] | 2512 | ImplicitParamDecl::Other); |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2513 | Args.push_back(&Dst); |
| 2514 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2515 | const auto &FI = CGM.getTypes().arrangeBuiltinFunctionDeclaration( |
John McCall | c56a8b3 | 2016-03-11 04:30:31 +0000 | [diff] [blame] | 2516 | CGM.getContext().VoidPtrTy, Args); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2517 | llvm::FunctionType *FTy = CGM.getTypes().GetFunctionType(FI); |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 2518 | std::string Name = getName({"__kmpc_global_ctor_", ""}); |
| 2519 | llvm::Function *Fn = |
| 2520 | CGM.CreateGlobalInitOrDestructFunction(FTy, Name, FI, Loc); |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2521 | CtorCGF.StartFunction(GlobalDecl(), CGM.getContext().VoidPtrTy, Fn, FI, |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 2522 | Args, Loc, Loc); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2523 | llvm::Value *ArgVal = CtorCGF.EmitLoadOfScalar( |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2524 | CtorCGF.GetAddrOfLocalVar(&Dst), /*Volatile=*/false, |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2525 | CGM.getContext().VoidPtrTy, Dst.getLocation()); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2526 | Address Arg = Address(ArgVal, VDAddr.getAlignment()); |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 2527 | Arg = CtorCGF.Builder.CreateElementBitCast( |
| 2528 | Arg, CtorCGF.ConvertTypeForMem(ASTTy)); |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2529 | CtorCGF.EmitAnyExprToMem(Init, Arg, Init->getType().getQualifiers(), |
| 2530 | /*IsInitializer=*/true); |
| 2531 | ArgVal = CtorCGF.EmitLoadOfScalar( |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2532 | CtorCGF.GetAddrOfLocalVar(&Dst), /*Volatile=*/false, |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2533 | CGM.getContext().VoidPtrTy, Dst.getLocation()); |
| 2534 | CtorCGF.Builder.CreateStore(ArgVal, CtorCGF.ReturnValue); |
| 2535 | CtorCGF.FinishFunction(); |
| 2536 | Ctor = Fn; |
| 2537 | } |
| 2538 | if (VD->getType().isDestructedType() != QualType::DK_none) { |
| 2539 | // Generate function that emits destructor call for the threadprivate copy |
| 2540 | // of the variable VD |
| 2541 | CodeGenFunction DtorCGF(CGM); |
| 2542 | FunctionArgList Args; |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 2543 | ImplicitParamDecl Dst(CGM.getContext(), /*DC=*/nullptr, Loc, |
| 2544 | /*Id=*/nullptr, CGM.getContext().VoidPtrTy, |
Alexey Bataev | 5622323 | 2017-06-09 13:40:18 +0000 | [diff] [blame] | 2545 | ImplicitParamDecl::Other); |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2546 | Args.push_back(&Dst); |
| 2547 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2548 | const auto &FI = CGM.getTypes().arrangeBuiltinFunctionDeclaration( |
John McCall | c56a8b3 | 2016-03-11 04:30:31 +0000 | [diff] [blame] | 2549 | CGM.getContext().VoidTy, Args); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2550 | llvm::FunctionType *FTy = CGM.getTypes().GetFunctionType(FI); |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 2551 | std::string Name = getName({"__kmpc_global_dtor_", ""}); |
| 2552 | llvm::Function *Fn = |
| 2553 | CGM.CreateGlobalInitOrDestructFunction(FTy, Name, FI, Loc); |
Adrian Prantl | 1858c66 | 2016-04-24 22:22:29 +0000 | [diff] [blame] | 2554 | auto NL = ApplyDebugLocation::CreateEmpty(DtorCGF); |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2555 | DtorCGF.StartFunction(GlobalDecl(), CGM.getContext().VoidTy, Fn, FI, Args, |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 2556 | Loc, Loc); |
Adrian Prantl | 1858c66 | 2016-04-24 22:22:29 +0000 | [diff] [blame] | 2557 | // Create a scope with an artificial location for the body of this function. |
| 2558 | auto AL = ApplyDebugLocation::CreateArtificial(DtorCGF); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2559 | llvm::Value *ArgVal = DtorCGF.EmitLoadOfScalar( |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2560 | DtorCGF.GetAddrOfLocalVar(&Dst), |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2561 | /*Volatile=*/false, CGM.getContext().VoidPtrTy, Dst.getLocation()); |
| 2562 | DtorCGF.emitDestroy(Address(ArgVal, VDAddr.getAlignment()), ASTTy, |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2563 | DtorCGF.getDestroyer(ASTTy.isDestructedType()), |
| 2564 | DtorCGF.needsEHCleanup(ASTTy.isDestructedType())); |
| 2565 | DtorCGF.FinishFunction(); |
| 2566 | Dtor = Fn; |
| 2567 | } |
| 2568 | // Do not emit init function if it is not required. |
| 2569 | if (!Ctor && !Dtor) |
| 2570 | return nullptr; |
| 2571 | |
| 2572 | llvm::Type *CopyCtorTyArgs[] = {CGM.VoidPtrTy, CGM.VoidPtrTy}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2573 | auto *CopyCtorTy = llvm::FunctionType::get(CGM.VoidPtrTy, CopyCtorTyArgs, |
| 2574 | /*isVarArg=*/false) |
| 2575 | ->getPointerTo(); |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2576 | // Copying constructor for the threadprivate variable. |
| 2577 | // Must be NULL - reserved by runtime, but currently it requires that this |
| 2578 | // parameter is always NULL. Otherwise it fires assertion. |
| 2579 | CopyCtor = llvm::Constant::getNullValue(CopyCtorTy); |
| 2580 | if (Ctor == nullptr) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2581 | auto *CtorTy = llvm::FunctionType::get(CGM.VoidPtrTy, CGM.VoidPtrTy, |
| 2582 | /*isVarArg=*/false) |
| 2583 | ->getPointerTo(); |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2584 | Ctor = llvm::Constant::getNullValue(CtorTy); |
| 2585 | } |
| 2586 | if (Dtor == nullptr) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2587 | auto *DtorTy = llvm::FunctionType::get(CGM.VoidTy, CGM.VoidPtrTy, |
| 2588 | /*isVarArg=*/false) |
| 2589 | ->getPointerTo(); |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2590 | Dtor = llvm::Constant::getNullValue(DtorTy); |
| 2591 | } |
| 2592 | if (!CGF) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2593 | auto *InitFunctionTy = |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2594 | llvm::FunctionType::get(CGM.VoidTy, /*isVarArg*/ false); |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 2595 | std::string Name = getName({"__omp_threadprivate_init_", ""}); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2596 | llvm::Function *InitFunction = CGM.CreateGlobalInitOrDestructFunction( |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 2597 | InitFunctionTy, Name, CGM.getTypes().arrangeNullaryFunction()); |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2598 | CodeGenFunction InitCGF(CGM); |
| 2599 | FunctionArgList ArgList; |
| 2600 | InitCGF.StartFunction(GlobalDecl(), CGM.getContext().VoidTy, InitFunction, |
| 2601 | CGM.getTypes().arrangeNullaryFunction(), ArgList, |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 2602 | Loc, Loc); |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 2603 | emitThreadPrivateVarInit(InitCGF, VDAddr, Ctor, CopyCtor, Dtor, Loc); |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2604 | InitCGF.FinishFunction(); |
| 2605 | return InitFunction; |
| 2606 | } |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 2607 | emitThreadPrivateVarInit(*CGF, VDAddr, Ctor, CopyCtor, Dtor, Loc); |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2608 | } |
| 2609 | return nullptr; |
| 2610 | } |
| 2611 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2612 | /// Obtain information that uniquely identifies a target entry. This |
Alexey Bataev | 34f8a70 | 2018-03-28 14:28:54 +0000 | [diff] [blame] | 2613 | /// consists of the file and device IDs as well as line number associated with |
| 2614 | /// the relevant entry source location. |
| 2615 | static void getTargetEntryUniqueInfo(ASTContext &C, SourceLocation Loc, |
| 2616 | unsigned &DeviceID, unsigned &FileID, |
| 2617 | unsigned &LineNum) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2618 | SourceManager &SM = C.getSourceManager(); |
Alexey Bataev | 34f8a70 | 2018-03-28 14:28:54 +0000 | [diff] [blame] | 2619 | |
| 2620 | // The loc should be always valid and have a file ID (the user cannot use |
| 2621 | // #pragma directives in macros) |
| 2622 | |
| 2623 | assert(Loc.isValid() && "Source location is expected to be always valid."); |
Alexey Bataev | 34f8a70 | 2018-03-28 14:28:54 +0000 | [diff] [blame] | 2624 | |
| 2625 | PresumedLoc PLoc = SM.getPresumedLoc(Loc); |
| 2626 | assert(PLoc.isValid() && "Source location is expected to be always valid."); |
| 2627 | |
| 2628 | llvm::sys::fs::UniqueID ID; |
Alexey Bataev | 64e62dc | 2018-04-30 16:26:57 +0000 | [diff] [blame] | 2629 | if (auto EC = llvm::sys::fs::getUniqueID(PLoc.getFilename(), ID)) |
| 2630 | SM.getDiagnostics().Report(diag::err_cannot_open_file) |
| 2631 | << PLoc.getFilename() << EC.message(); |
Alexey Bataev | 34f8a70 | 2018-03-28 14:28:54 +0000 | [diff] [blame] | 2632 | |
| 2633 | DeviceID = ID.getDevice(); |
| 2634 | FileID = ID.getFile(); |
| 2635 | LineNum = PLoc.getLine(); |
| 2636 | } |
| 2637 | |
| 2638 | bool CGOpenMPRuntime::emitDeclareTargetVarDefinition(const VarDecl *VD, |
| 2639 | llvm::GlobalVariable *Addr, |
| 2640 | bool PerformInit) { |
| 2641 | Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res = |
| 2642 | isDeclareTargetDeclaration(VD); |
| 2643 | if (!Res || *Res == OMPDeclareTargetDeclAttr::MT_Link) |
| 2644 | return false; |
| 2645 | VD = VD->getDefinition(CGM.getContext()); |
| 2646 | if (VD && !DeclareTargetWithDefinition.insert(VD).second) |
| 2647 | return CGM.getLangOpts().OpenMPIsDevice; |
| 2648 | |
| 2649 | QualType ASTTy = VD->getType(); |
| 2650 | |
| 2651 | SourceLocation Loc = VD->getCanonicalDecl()->getLocStart(); |
| 2652 | // Produce the unique prefix to identify the new target regions. We use |
| 2653 | // the source location of the variable declaration which we know to not |
| 2654 | // conflict with any target region. |
| 2655 | unsigned DeviceID; |
| 2656 | unsigned FileID; |
| 2657 | unsigned Line; |
| 2658 | getTargetEntryUniqueInfo(CGM.getContext(), Loc, DeviceID, FileID, Line); |
| 2659 | SmallString<128> Buffer, Out; |
| 2660 | { |
| 2661 | llvm::raw_svector_ostream OS(Buffer); |
| 2662 | OS << "__omp_offloading_" << llvm::format("_%x", DeviceID) |
| 2663 | << llvm::format("_%x_", FileID) << VD->getName() << "_l" << Line; |
| 2664 | } |
| 2665 | |
| 2666 | const Expr *Init = VD->getAnyInitializer(); |
| 2667 | if (CGM.getLangOpts().CPlusPlus && PerformInit) { |
| 2668 | llvm::Constant *Ctor; |
| 2669 | llvm::Constant *ID; |
| 2670 | if (CGM.getLangOpts().OpenMPIsDevice) { |
| 2671 | // Generate function that re-emits the declaration's initializer into |
| 2672 | // the threadprivate copy of the variable VD |
| 2673 | CodeGenFunction CtorCGF(CGM); |
| 2674 | |
| 2675 | const CGFunctionInfo &FI = CGM.getTypes().arrangeNullaryFunction(); |
| 2676 | llvm::FunctionType *FTy = CGM.getTypes().GetFunctionType(FI); |
| 2677 | llvm::Function *Fn = CGM.CreateGlobalInitOrDestructFunction( |
| 2678 | FTy, Twine(Buffer, "_ctor"), FI, Loc); |
| 2679 | auto NL = ApplyDebugLocation::CreateEmpty(CtorCGF); |
| 2680 | CtorCGF.StartFunction(GlobalDecl(), CGM.getContext().VoidTy, Fn, FI, |
| 2681 | FunctionArgList(), Loc, Loc); |
| 2682 | auto AL = ApplyDebugLocation::CreateArtificial(CtorCGF); |
| 2683 | CtorCGF.EmitAnyExprToMem(Init, |
| 2684 | Address(Addr, CGM.getContext().getDeclAlign(VD)), |
| 2685 | Init->getType().getQualifiers(), |
| 2686 | /*IsInitializer=*/true); |
| 2687 | CtorCGF.FinishFunction(); |
| 2688 | Ctor = Fn; |
| 2689 | ID = llvm::ConstantExpr::getBitCast(Fn, CGM.Int8PtrTy); |
Alexey Bataev | e253f2f | 2018-05-09 14:15:18 +0000 | [diff] [blame] | 2690 | CGM.addUsedGlobal(cast<llvm::GlobalValue>(Ctor)); |
Alexey Bataev | 34f8a70 | 2018-03-28 14:28:54 +0000 | [diff] [blame] | 2691 | } else { |
| 2692 | Ctor = new llvm::GlobalVariable( |
| 2693 | CGM.getModule(), CGM.Int8Ty, /*isConstant=*/true, |
| 2694 | llvm::GlobalValue::PrivateLinkage, |
| 2695 | llvm::Constant::getNullValue(CGM.Int8Ty), Twine(Buffer, "_ctor")); |
| 2696 | ID = Ctor; |
| 2697 | } |
| 2698 | |
| 2699 | // Register the information for the entry associated with the constructor. |
| 2700 | Out.clear(); |
| 2701 | OffloadEntriesInfoManager.registerTargetRegionEntryInfo( |
| 2702 | DeviceID, FileID, Twine(Buffer, "_ctor").toStringRef(Out), Line, Ctor, |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 2703 | ID, OffloadEntriesInfoManagerTy::OMPTargetRegionEntryCtor); |
Alexey Bataev | 34f8a70 | 2018-03-28 14:28:54 +0000 | [diff] [blame] | 2704 | } |
| 2705 | if (VD->getType().isDestructedType() != QualType::DK_none) { |
| 2706 | llvm::Constant *Dtor; |
| 2707 | llvm::Constant *ID; |
| 2708 | if (CGM.getLangOpts().OpenMPIsDevice) { |
| 2709 | // Generate function that emits destructor call for the threadprivate |
| 2710 | // copy of the variable VD |
| 2711 | CodeGenFunction DtorCGF(CGM); |
| 2712 | |
| 2713 | const CGFunctionInfo &FI = CGM.getTypes().arrangeNullaryFunction(); |
| 2714 | llvm::FunctionType *FTy = CGM.getTypes().GetFunctionType(FI); |
| 2715 | llvm::Function *Fn = CGM.CreateGlobalInitOrDestructFunction( |
| 2716 | FTy, Twine(Buffer, "_dtor"), FI, Loc); |
| 2717 | auto NL = ApplyDebugLocation::CreateEmpty(DtorCGF); |
| 2718 | DtorCGF.StartFunction(GlobalDecl(), CGM.getContext().VoidTy, Fn, FI, |
| 2719 | FunctionArgList(), Loc, Loc); |
| 2720 | // Create a scope with an artificial location for the body of this |
| 2721 | // function. |
| 2722 | auto AL = ApplyDebugLocation::CreateArtificial(DtorCGF); |
| 2723 | DtorCGF.emitDestroy(Address(Addr, CGM.getContext().getDeclAlign(VD)), |
| 2724 | ASTTy, DtorCGF.getDestroyer(ASTTy.isDestructedType()), |
| 2725 | DtorCGF.needsEHCleanup(ASTTy.isDestructedType())); |
| 2726 | DtorCGF.FinishFunction(); |
| 2727 | Dtor = Fn; |
| 2728 | ID = llvm::ConstantExpr::getBitCast(Fn, CGM.Int8PtrTy); |
Alexey Bataev | e253f2f | 2018-05-09 14:15:18 +0000 | [diff] [blame] | 2729 | CGM.addUsedGlobal(cast<llvm::GlobalValue>(Dtor)); |
Alexey Bataev | 34f8a70 | 2018-03-28 14:28:54 +0000 | [diff] [blame] | 2730 | } else { |
| 2731 | Dtor = new llvm::GlobalVariable( |
| 2732 | CGM.getModule(), CGM.Int8Ty, /*isConstant=*/true, |
| 2733 | llvm::GlobalValue::PrivateLinkage, |
| 2734 | llvm::Constant::getNullValue(CGM.Int8Ty), Twine(Buffer, "_dtor")); |
| 2735 | ID = Dtor; |
| 2736 | } |
| 2737 | // Register the information for the entry associated with the destructor. |
| 2738 | Out.clear(); |
| 2739 | OffloadEntriesInfoManager.registerTargetRegionEntryInfo( |
| 2740 | DeviceID, FileID, Twine(Buffer, "_dtor").toStringRef(Out), Line, Dtor, |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 2741 | ID, OffloadEntriesInfoManagerTy::OMPTargetRegionEntryDtor); |
Alexey Bataev | 34f8a70 | 2018-03-28 14:28:54 +0000 | [diff] [blame] | 2742 | } |
| 2743 | return CGM.getLangOpts().OpenMPIsDevice; |
| 2744 | } |
| 2745 | |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 2746 | Address CGOpenMPRuntime::getAddrOfArtificialThreadPrivate(CodeGenFunction &CGF, |
| 2747 | QualType VarType, |
| 2748 | StringRef Name) { |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 2749 | std::string Suffix = getName({"artificial", ""}); |
| 2750 | std::string CacheSuffix = getName({"cache", ""}); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 2751 | llvm::Type *VarLVType = CGF.ConvertTypeForMem(VarType); |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 2752 | llvm::Value *GAddr = |
| 2753 | getOrCreateInternalVariable(VarLVType, Twine(Name).concat(Suffix)); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 2754 | llvm::Value *Args[] = { |
| 2755 | emitUpdateLocation(CGF, SourceLocation()), |
| 2756 | getThreadID(CGF, SourceLocation()), |
| 2757 | CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(GAddr, CGM.VoidPtrTy), |
| 2758 | CGF.Builder.CreateIntCast(CGF.getTypeSize(VarType), CGM.SizeTy, |
| 2759 | /*IsSigned=*/false), |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 2760 | getOrCreateInternalVariable( |
| 2761 | CGM.VoidPtrPtrTy, Twine(Name).concat(Suffix).concat(CacheSuffix))}; |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 2762 | return Address( |
| 2763 | CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
| 2764 | CGF.EmitRuntimeCall( |
| 2765 | createRuntimeFunction(OMPRTL__kmpc_threadprivate_cached), Args), |
| 2766 | VarLVType->getPointerTo(/*AddrSpace=*/0)), |
| 2767 | CGM.getPointerAlign()); |
| 2768 | } |
| 2769 | |
Arpith Chacko Jacob | bb36fe8 | 2017-01-10 15:42:51 +0000 | [diff] [blame] | 2770 | void CGOpenMPRuntime::emitOMPIfClause(CodeGenFunction &CGF, const Expr *Cond, |
| 2771 | const RegionCodeGenTy &ThenGen, |
| 2772 | const RegionCodeGenTy &ElseGen) { |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 2773 | CodeGenFunction::LexicalScope ConditionScope(CGF, Cond->getSourceRange()); |
| 2774 | |
| 2775 | // If the condition constant folds and can be elided, try to avoid emitting |
| 2776 | // the condition and the dead arm of the if/else. |
| 2777 | bool CondConstant; |
| 2778 | if (CGF.ConstantFoldsToSimpleInteger(Cond, CondConstant)) { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2779 | if (CondConstant) |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 2780 | ThenGen(CGF); |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2781 | else |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 2782 | ElseGen(CGF); |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 2783 | return; |
| 2784 | } |
| 2785 | |
| 2786 | // Otherwise, the condition did not fold, or we couldn't elide it. Just |
| 2787 | // emit the conditional branch. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2788 | llvm::BasicBlock *ThenBlock = CGF.createBasicBlock("omp_if.then"); |
| 2789 | llvm::BasicBlock *ElseBlock = CGF.createBasicBlock("omp_if.else"); |
| 2790 | llvm::BasicBlock *ContBlock = CGF.createBasicBlock("omp_if.end"); |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 2791 | CGF.EmitBranchOnBoolExpr(Cond, ThenBlock, ElseBlock, /*TrueCount=*/0); |
| 2792 | |
| 2793 | // Emit the 'then' code. |
| 2794 | CGF.EmitBlock(ThenBlock); |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2795 | ThenGen(CGF); |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 2796 | CGF.EmitBranch(ContBlock); |
| 2797 | // Emit the 'else' code if present. |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2798 | // There is no need to emit line number for unconditional branch. |
| 2799 | (void)ApplyDebugLocation::CreateEmpty(CGF); |
| 2800 | CGF.EmitBlock(ElseBlock); |
| 2801 | ElseGen(CGF); |
| 2802 | // There is no need to emit line number for unconditional branch. |
| 2803 | (void)ApplyDebugLocation::CreateEmpty(CGF); |
| 2804 | CGF.EmitBranch(ContBlock); |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 2805 | // Emit the continuation block for code after the if. |
| 2806 | CGF.EmitBlock(ContBlock, /*IsFinished=*/true); |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 2807 | } |
| 2808 | |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 2809 | void CGOpenMPRuntime::emitParallelCall(CodeGenFunction &CGF, SourceLocation Loc, |
| 2810 | llvm::Value *OutlinedFn, |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 2811 | ArrayRef<llvm::Value *> CapturedVars, |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 2812 | const Expr *IfCond) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 2813 | if (!CGF.HaveInsertPoint()) |
| 2814 | return; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2815 | llvm::Value *RTLoc = emitUpdateLocation(CGF, Loc); |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2816 | auto &&ThenGen = [OutlinedFn, CapturedVars, RTLoc](CodeGenFunction &CGF, |
| 2817 | PrePostActionTy &) { |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 2818 | // Build call __kmpc_fork_call(loc, n, microtask, var1, .., varn); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2819 | CGOpenMPRuntime &RT = CGF.CGM.getOpenMPRuntime(); |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 2820 | llvm::Value *Args[] = { |
| 2821 | RTLoc, |
| 2822 | CGF.Builder.getInt32(CapturedVars.size()), // Number of captured vars |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2823 | CGF.Builder.CreateBitCast(OutlinedFn, RT.getKmpc_MicroPointerTy())}; |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 2824 | llvm::SmallVector<llvm::Value *, 16> RealArgs; |
| 2825 | RealArgs.append(std::begin(Args), std::end(Args)); |
| 2826 | RealArgs.append(CapturedVars.begin(), CapturedVars.end()); |
| 2827 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2828 | llvm::Value *RTLFn = RT.createRuntimeFunction(OMPRTL__kmpc_fork_call); |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 2829 | CGF.EmitRuntimeCall(RTLFn, RealArgs); |
| 2830 | }; |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2831 | auto &&ElseGen = [OutlinedFn, CapturedVars, RTLoc, Loc](CodeGenFunction &CGF, |
| 2832 | PrePostActionTy &) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2833 | CGOpenMPRuntime &RT = CGF.CGM.getOpenMPRuntime(); |
| 2834 | llvm::Value *ThreadID = RT.getThreadID(CGF, Loc); |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 2835 | // Build calls: |
| 2836 | // __kmpc_serialized_parallel(&Loc, GTid); |
| 2837 | llvm::Value *Args[] = {RTLoc, ThreadID}; |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2838 | CGF.EmitRuntimeCall( |
| 2839 | RT.createRuntimeFunction(OMPRTL__kmpc_serialized_parallel), Args); |
Alexey Bataev | d74d060 | 2014-10-13 06:02:40 +0000 | [diff] [blame] | 2840 | |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 2841 | // OutlinedFn(>id, &zero, CapturedStruct); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2842 | Address ThreadIDAddr = RT.emitThreadIDAddress(CGF, Loc); |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 2843 | Address ZeroAddr = CGF.CreateDefaultAlignTempAlloca(CGF.Int32Ty, |
| 2844 | /*Name*/ ".zero.addr"); |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 2845 | CGF.InitTempAlloca(ZeroAddr, CGF.Builder.getInt32(/*C*/ 0)); |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 2846 | llvm::SmallVector<llvm::Value *, 16> OutlinedFnArgs; |
| 2847 | OutlinedFnArgs.push_back(ThreadIDAddr.getPointer()); |
| 2848 | OutlinedFnArgs.push_back(ZeroAddr.getPointer()); |
| 2849 | OutlinedFnArgs.append(CapturedVars.begin(), CapturedVars.end()); |
Alexey Bataev | 3c595a6 | 2017-08-14 15:01:03 +0000 | [diff] [blame] | 2850 | RT.emitOutlinedFunctionCall(CGF, Loc, OutlinedFn, OutlinedFnArgs); |
Alexey Bataev | d74d060 | 2014-10-13 06:02:40 +0000 | [diff] [blame] | 2851 | |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 2852 | // __kmpc_end_serialized_parallel(&Loc, GTid); |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2853 | llvm::Value *EndArgs[] = {RT.emitUpdateLocation(CGF, Loc), ThreadID}; |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 2854 | CGF.EmitRuntimeCall( |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2855 | RT.createRuntimeFunction(OMPRTL__kmpc_end_serialized_parallel), |
| 2856 | EndArgs); |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 2857 | }; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2858 | if (IfCond) { |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 2859 | emitOMPIfClause(CGF, IfCond, ThenGen, ElseGen); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2860 | } else { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2861 | RegionCodeGenTy ThenRCG(ThenGen); |
| 2862 | ThenRCG(CGF); |
Alexey Bataev | f539faa | 2016-03-28 12:58:34 +0000 | [diff] [blame] | 2863 | } |
Alexey Bataev | d74d060 | 2014-10-13 06:02:40 +0000 | [diff] [blame] | 2864 | } |
| 2865 | |
NAKAMURA Takumi | 59c74b22 | 2014-10-27 08:08:18 +0000 | [diff] [blame] | 2866 | // 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] | 2867 | // thread-ID variable (it is passed in a first argument of the outlined function |
| 2868 | // as "kmp_int32 *gtid"). Otherwise, if we're not inside parallel region, but in |
| 2869 | // regular serial code region, get thread ID by calling kmp_int32 |
| 2870 | // kmpc_global_thread_num(ident_t *loc), stash this thread ID in a temporary and |
| 2871 | // return the address of that temp. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2872 | Address CGOpenMPRuntime::emitThreadIDAddress(CodeGenFunction &CGF, |
| 2873 | SourceLocation Loc) { |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 2874 | if (auto *OMPRegionInfo = |
Alexey Bataev | d74d060 | 2014-10-13 06:02:40 +0000 | [diff] [blame] | 2875 | dyn_cast_or_null<CGOpenMPRegionInfo>(CGF.CapturedStmtInfo)) |
Alexey Bataev | 8cbe0a6 | 2015-02-26 10:27:34 +0000 | [diff] [blame] | 2876 | if (OMPRegionInfo->getThreadIDVariable()) |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 2877 | return OMPRegionInfo->getThreadIDVariableLValue(CGF).getAddress(); |
Alexey Bataev | 8cbe0a6 | 2015-02-26 10:27:34 +0000 | [diff] [blame] | 2878 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2879 | llvm::Value *ThreadID = getThreadID(CGF, Loc); |
| 2880 | QualType Int32Ty = |
Alexey Bataev | d74d060 | 2014-10-13 06:02:40 +0000 | [diff] [blame] | 2881 | CGF.getContext().getIntTypeForBitwidth(/*DestWidth*/ 32, /*Signed*/ true); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2882 | Address ThreadIDTemp = CGF.CreateMemTemp(Int32Ty, /*Name*/ ".threadid_temp."); |
Alexey Bataev | d74d060 | 2014-10-13 06:02:40 +0000 | [diff] [blame] | 2883 | CGF.EmitStoreOfScalar(ThreadID, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2884 | CGF.MakeAddrLValue(ThreadIDTemp, Int32Ty)); |
Alexey Bataev | d74d060 | 2014-10-13 06:02:40 +0000 | [diff] [blame] | 2885 | |
| 2886 | return ThreadIDTemp; |
| 2887 | } |
| 2888 | |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2889 | llvm::Constant * |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 2890 | CGOpenMPRuntime::getOrCreateInternalVariable(llvm::Type *Ty, |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2891 | const llvm::Twine &Name) { |
Alexey Bataev | 3a3bf0b | 2014-09-22 10:01:53 +0000 | [diff] [blame] | 2892 | SmallString<256> Buffer; |
| 2893 | llvm::raw_svector_ostream Out(Buffer); |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2894 | Out << Name; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2895 | StringRef RuntimeName = Out.str(); |
Alexey Bataev | 43a919f | 2018-04-13 17:48:43 +0000 | [diff] [blame] | 2896 | auto &Elem = *InternalVars.try_emplace(RuntimeName, nullptr).first; |
David Blaikie | 13156b6 | 2014-11-19 03:06:06 +0000 | [diff] [blame] | 2897 | if (Elem.second) { |
| 2898 | assert(Elem.second->getType()->getPointerElementType() == Ty && |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2899 | "OMP internal variable has different type than requested"); |
David Blaikie | 13156b6 | 2014-11-19 03:06:06 +0000 | [diff] [blame] | 2900 | return &*Elem.second; |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2901 | } |
Alexey Bataev | 3a3bf0b | 2014-09-22 10:01:53 +0000 | [diff] [blame] | 2902 | |
David Blaikie | 13156b6 | 2014-11-19 03:06:06 +0000 | [diff] [blame] | 2903 | return Elem.second = new llvm::GlobalVariable( |
| 2904 | CGM.getModule(), Ty, /*IsConstant*/ false, |
| 2905 | llvm::GlobalValue::CommonLinkage, llvm::Constant::getNullValue(Ty), |
| 2906 | Elem.first()); |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2907 | } |
| 2908 | |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 2909 | llvm::Value *CGOpenMPRuntime::getCriticalRegionLock(StringRef CriticalName) { |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 2910 | std::string Prefix = Twine("gomp_critical_user_", CriticalName).str(); |
| 2911 | std::string Name = getName({Prefix, "var"}); |
| 2912 | return getOrCreateInternalVariable(KmpCriticalNameTy, Name); |
Alexey Bataev | 3a3bf0b | 2014-09-22 10:01:53 +0000 | [diff] [blame] | 2913 | } |
| 2914 | |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 2915 | namespace { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2916 | /// Common pre(post)-action for different OpenMP constructs. |
| 2917 | class CommonActionTy final : public PrePostActionTy { |
| 2918 | llvm::Value *EnterCallee; |
| 2919 | ArrayRef<llvm::Value *> EnterArgs; |
| 2920 | llvm::Value *ExitCallee; |
| 2921 | ArrayRef<llvm::Value *> ExitArgs; |
| 2922 | bool Conditional; |
| 2923 | llvm::BasicBlock *ContBlock = nullptr; |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 2924 | |
| 2925 | public: |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2926 | CommonActionTy(llvm::Value *EnterCallee, ArrayRef<llvm::Value *> EnterArgs, |
| 2927 | llvm::Value *ExitCallee, ArrayRef<llvm::Value *> ExitArgs, |
| 2928 | bool Conditional = false) |
| 2929 | : EnterCallee(EnterCallee), EnterArgs(EnterArgs), ExitCallee(ExitCallee), |
| 2930 | ExitArgs(ExitArgs), Conditional(Conditional) {} |
| 2931 | void Enter(CodeGenFunction &CGF) override { |
| 2932 | llvm::Value *EnterRes = CGF.EmitRuntimeCall(EnterCallee, EnterArgs); |
| 2933 | if (Conditional) { |
| 2934 | llvm::Value *CallBool = CGF.Builder.CreateIsNotNull(EnterRes); |
| 2935 | auto *ThenBlock = CGF.createBasicBlock("omp_if.then"); |
| 2936 | ContBlock = CGF.createBasicBlock("omp_if.end"); |
| 2937 | // Generate the branch (If-stmt) |
| 2938 | CGF.Builder.CreateCondBr(CallBool, ThenBlock, ContBlock); |
| 2939 | CGF.EmitBlock(ThenBlock); |
| 2940 | } |
Alexey Bataev | a744ff5 | 2015-05-05 09:24:37 +0000 | [diff] [blame] | 2941 | } |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2942 | void Done(CodeGenFunction &CGF) { |
| 2943 | // Emit the rest of blocks/branches |
| 2944 | CGF.EmitBranch(ContBlock); |
| 2945 | CGF.EmitBlock(ContBlock, true); |
| 2946 | } |
| 2947 | void Exit(CodeGenFunction &CGF) override { |
| 2948 | CGF.EmitRuntimeCall(ExitCallee, ExitArgs); |
Alexey Bataev | 3e6124b | 2015-04-10 07:48:12 +0000 | [diff] [blame] | 2949 | } |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 2950 | }; |
Hans Wennborg | 7eb5464 | 2015-09-10 17:07:54 +0000 | [diff] [blame] | 2951 | } // anonymous namespace |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 2952 | |
| 2953 | void CGOpenMPRuntime::emitCriticalRegion(CodeGenFunction &CGF, |
| 2954 | StringRef CriticalName, |
| 2955 | const RegionCodeGenTy &CriticalOpGen, |
Alexey Bataev | fc57d16 | 2015-12-15 10:55:09 +0000 | [diff] [blame] | 2956 | SourceLocation Loc, const Expr *Hint) { |
| 2957 | // __kmpc_critical[_with_hint](ident_t *, gtid, Lock[, hint]); |
Alexey Bataev | 75ddfab | 2014-12-01 11:32:38 +0000 | [diff] [blame] | 2958 | // CriticalOpGen(); |
| 2959 | // __kmpc_end_critical(ident_t *, gtid, Lock); |
| 2960 | // Prepare arguments and build a call to __kmpc_critical |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 2961 | if (!CGF.HaveInsertPoint()) |
| 2962 | return; |
Alexey Bataev | fc57d16 | 2015-12-15 10:55:09 +0000 | [diff] [blame] | 2963 | llvm::Value *Args[] = {emitUpdateLocation(CGF, Loc), getThreadID(CGF, Loc), |
| 2964 | getCriticalRegionLock(CriticalName)}; |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2965 | llvm::SmallVector<llvm::Value *, 4> EnterArgs(std::begin(Args), |
| 2966 | std::end(Args)); |
Alexey Bataev | fc57d16 | 2015-12-15 10:55:09 +0000 | [diff] [blame] | 2967 | if (Hint) { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2968 | EnterArgs.push_back(CGF.Builder.CreateIntCast( |
| 2969 | CGF.EmitScalarExpr(Hint), CGM.IntPtrTy, /*isSigned=*/false)); |
| 2970 | } |
| 2971 | CommonActionTy Action( |
| 2972 | createRuntimeFunction(Hint ? OMPRTL__kmpc_critical_with_hint |
| 2973 | : OMPRTL__kmpc_critical), |
| 2974 | EnterArgs, createRuntimeFunction(OMPRTL__kmpc_end_critical), Args); |
| 2975 | CriticalOpGen.setAction(Action); |
Alexey Bataev | fc57d16 | 2015-12-15 10:55:09 +0000 | [diff] [blame] | 2976 | emitInlinedDirective(CGF, OMPD_critical, CriticalOpGen); |
Alexey Bataev | 3a3bf0b | 2014-09-22 10:01:53 +0000 | [diff] [blame] | 2977 | } |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 2978 | |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 2979 | void CGOpenMPRuntime::emitMasterRegion(CodeGenFunction &CGF, |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 2980 | const RegionCodeGenTy &MasterOpGen, |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 2981 | SourceLocation Loc) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 2982 | if (!CGF.HaveInsertPoint()) |
| 2983 | return; |
Alexey Bataev | 8d69065 | 2014-12-04 07:23:53 +0000 | [diff] [blame] | 2984 | // if(__kmpc_master(ident_t *, gtid)) { |
| 2985 | // MasterOpGen(); |
| 2986 | // __kmpc_end_master(ident_t *, gtid); |
| 2987 | // } |
| 2988 | // Prepare arguments and build a call to __kmpc_master |
Alexey Bataev | d7614fb | 2015-04-10 06:33:45 +0000 | [diff] [blame] | 2989 | llvm::Value *Args[] = {emitUpdateLocation(CGF, Loc), getThreadID(CGF, Loc)}; |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2990 | CommonActionTy Action(createRuntimeFunction(OMPRTL__kmpc_master), Args, |
| 2991 | createRuntimeFunction(OMPRTL__kmpc_end_master), Args, |
| 2992 | /*Conditional=*/true); |
| 2993 | MasterOpGen.setAction(Action); |
| 2994 | emitInlinedDirective(CGF, OMPD_master, MasterOpGen); |
| 2995 | Action.Done(CGF); |
Alexey Bataev | 8d69065 | 2014-12-04 07:23:53 +0000 | [diff] [blame] | 2996 | } |
| 2997 | |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 2998 | void CGOpenMPRuntime::emitTaskyieldCall(CodeGenFunction &CGF, |
| 2999 | SourceLocation Loc) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 3000 | if (!CGF.HaveInsertPoint()) |
| 3001 | return; |
Alexey Bataev | 9f797f3 | 2015-02-05 05:57:51 +0000 | [diff] [blame] | 3002 | // Build call __kmpc_omp_taskyield(loc, thread_id, 0); |
| 3003 | llvm::Value *Args[] = { |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 3004 | emitUpdateLocation(CGF, Loc), getThreadID(CGF, Loc), |
Alexey Bataev | 9f797f3 | 2015-02-05 05:57:51 +0000 | [diff] [blame] | 3005 | llvm::ConstantInt::get(CGM.IntTy, /*V=*/0, /*isSigned=*/true)}; |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 3006 | CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_omp_taskyield), Args); |
Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 3007 | if (auto *Region = dyn_cast_or_null<CGOpenMPRegionInfo>(CGF.CapturedStmtInfo)) |
| 3008 | Region->emitUntiedSwitch(CGF); |
Alexey Bataev | 9f797f3 | 2015-02-05 05:57:51 +0000 | [diff] [blame] | 3009 | } |
| 3010 | |
Alexey Bataev | c30dd2d | 2015-06-18 12:14:09 +0000 | [diff] [blame] | 3011 | void CGOpenMPRuntime::emitTaskgroupRegion(CodeGenFunction &CGF, |
| 3012 | const RegionCodeGenTy &TaskgroupOpGen, |
| 3013 | SourceLocation Loc) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 3014 | if (!CGF.HaveInsertPoint()) |
| 3015 | return; |
Alexey Bataev | c30dd2d | 2015-06-18 12:14:09 +0000 | [diff] [blame] | 3016 | // __kmpc_taskgroup(ident_t *, gtid); |
| 3017 | // TaskgroupOpGen(); |
| 3018 | // __kmpc_end_taskgroup(ident_t *, gtid); |
| 3019 | // Prepare arguments and build a call to __kmpc_taskgroup |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 3020 | llvm::Value *Args[] = {emitUpdateLocation(CGF, Loc), getThreadID(CGF, Loc)}; |
| 3021 | CommonActionTy Action(createRuntimeFunction(OMPRTL__kmpc_taskgroup), Args, |
| 3022 | createRuntimeFunction(OMPRTL__kmpc_end_taskgroup), |
| 3023 | Args); |
| 3024 | TaskgroupOpGen.setAction(Action); |
| 3025 | emitInlinedDirective(CGF, OMPD_taskgroup, TaskgroupOpGen); |
Alexey Bataev | c30dd2d | 2015-06-18 12:14:09 +0000 | [diff] [blame] | 3026 | } |
| 3027 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3028 | /// Given an array of pointers to variables, project the address of a |
| 3029 | /// given variable. |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 3030 | static Address emitAddrOfVarFromArray(CodeGenFunction &CGF, Address Array, |
| 3031 | unsigned Index, const VarDecl *Var) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3032 | // Pull out the pointer to the variable. |
| 3033 | Address PtrAddr = |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 3034 | CGF.Builder.CreateConstArrayGEP(Array, Index, CGF.getPointerSize()); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3035 | llvm::Value *Ptr = CGF.Builder.CreateLoad(PtrAddr); |
| 3036 | |
| 3037 | Address Addr = Address(Ptr, CGF.getContext().getDeclAlign(Var)); |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 3038 | Addr = CGF.Builder.CreateElementBitCast( |
| 3039 | Addr, CGF.ConvertTypeForMem(Var->getType())); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3040 | return Addr; |
| 3041 | } |
| 3042 | |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3043 | static llvm::Value *emitCopyprivateCopyFunction( |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 3044 | CodeGenModule &CGM, llvm::Type *ArgsType, |
| 3045 | ArrayRef<const Expr *> CopyprivateVars, ArrayRef<const Expr *> DestExprs, |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 3046 | ArrayRef<const Expr *> SrcExprs, ArrayRef<const Expr *> AssignmentOps, |
| 3047 | SourceLocation Loc) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3048 | ASTContext &C = CGM.getContext(); |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3049 | // void copy_func(void *LHSArg, void *RHSArg); |
| 3050 | FunctionArgList Args; |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 3051 | ImplicitParamDecl LHSArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, C.VoidPtrTy, |
| 3052 | ImplicitParamDecl::Other); |
| 3053 | ImplicitParamDecl RHSArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, C.VoidPtrTy, |
| 3054 | ImplicitParamDecl::Other); |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3055 | Args.push_back(&LHSArg); |
| 3056 | Args.push_back(&RHSArg); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3057 | const auto &CGFI = |
| 3058 | CGM.getTypes().arrangeBuiltinFunctionDeclaration(C.VoidTy, Args); |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 3059 | std::string Name = |
| 3060 | CGM.getOpenMPRuntime().getName({"omp", "copyprivate", "copy_func"}); |
| 3061 | auto *Fn = llvm::Function::Create(CGM.getTypes().GetFunctionType(CGFI), |
| 3062 | llvm::GlobalValue::InternalLinkage, Name, |
| 3063 | &CGM.getModule()); |
Rafael Espindola | 51ec5a9 | 2018-02-28 23:46:35 +0000 | [diff] [blame] | 3064 | CGM.SetInternalFunctionAttributes(GlobalDecl(), Fn, CGFI); |
Alexey Bataev | c0f879b | 2018-04-10 20:10:53 +0000 | [diff] [blame] | 3065 | Fn->setDoesNotRecurse(); |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3066 | CodeGenFunction CGF(CGM); |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 3067 | CGF.StartFunction(GlobalDecl(), C.VoidTy, Fn, CGFI, Args, Loc, Loc); |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 3068 | // Dest = (void*[n])(LHSArg); |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3069 | // Src = (void*[n])(RHSArg); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3070 | Address LHS(CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
| 3071 | CGF.Builder.CreateLoad(CGF.GetAddrOfLocalVar(&LHSArg)), |
| 3072 | ArgsType), CGF.getPointerAlign()); |
| 3073 | Address RHS(CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
| 3074 | CGF.Builder.CreateLoad(CGF.GetAddrOfLocalVar(&RHSArg)), |
| 3075 | ArgsType), CGF.getPointerAlign()); |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3076 | // *(Type0*)Dst[0] = *(Type0*)Src[0]; |
| 3077 | // *(Type1*)Dst[1] = *(Type1*)Src[1]; |
| 3078 | // ... |
| 3079 | // *(Typen*)Dst[n] = *(Typen*)Src[n]; |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3080 | for (unsigned I = 0, E = AssignmentOps.size(); I < E; ++I) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3081 | const auto *DestVar = |
| 3082 | cast<VarDecl>(cast<DeclRefExpr>(DestExprs[I])->getDecl()); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3083 | Address DestAddr = emitAddrOfVarFromArray(CGF, LHS, I, DestVar); |
| 3084 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3085 | const auto *SrcVar = |
| 3086 | cast<VarDecl>(cast<DeclRefExpr>(SrcExprs[I])->getDecl()); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3087 | Address SrcAddr = emitAddrOfVarFromArray(CGF, RHS, I, SrcVar); |
| 3088 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3089 | const auto *VD = cast<DeclRefExpr>(CopyprivateVars[I])->getDecl(); |
Alexey Bataev | 1d9c15c | 2015-05-19 12:31:28 +0000 | [diff] [blame] | 3090 | QualType Type = VD->getType(); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3091 | CGF.EmitOMPCopy(Type, DestAddr, SrcAddr, DestVar, SrcVar, AssignmentOps[I]); |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3092 | } |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3093 | CGF.FinishFunction(); |
| 3094 | return Fn; |
| 3095 | } |
| 3096 | |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 3097 | void CGOpenMPRuntime::emitSingleRegion(CodeGenFunction &CGF, |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 3098 | const RegionCodeGenTy &SingleOpGen, |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3099 | SourceLocation Loc, |
| 3100 | ArrayRef<const Expr *> CopyprivateVars, |
| 3101 | ArrayRef<const Expr *> SrcExprs, |
| 3102 | ArrayRef<const Expr *> DstExprs, |
| 3103 | ArrayRef<const Expr *> AssignmentOps) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 3104 | if (!CGF.HaveInsertPoint()) |
| 3105 | return; |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3106 | assert(CopyprivateVars.size() == SrcExprs.size() && |
| 3107 | CopyprivateVars.size() == DstExprs.size() && |
| 3108 | CopyprivateVars.size() == AssignmentOps.size()); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3109 | ASTContext &C = CGM.getContext(); |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3110 | // int32 did_it = 0; |
Alexey Bataev | 6956e2e | 2015-02-05 06:35:41 +0000 | [diff] [blame] | 3111 | // if(__kmpc_single(ident_t *, gtid)) { |
| 3112 | // SingleOpGen(); |
| 3113 | // __kmpc_end_single(ident_t *, gtid); |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3114 | // did_it = 1; |
Alexey Bataev | 6956e2e | 2015-02-05 06:35:41 +0000 | [diff] [blame] | 3115 | // } |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3116 | // call __kmpc_copyprivate(ident_t *, gtid, <buf_size>, <copyprivate list>, |
| 3117 | // <copy_func>, did_it); |
| 3118 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3119 | Address DidIt = Address::invalid(); |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3120 | if (!CopyprivateVars.empty()) { |
| 3121 | // int32 did_it = 0; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3122 | QualType KmpInt32Ty = |
| 3123 | C.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1); |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3124 | DidIt = CGF.CreateMemTemp(KmpInt32Ty, ".omp.copyprivate.did_it"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3125 | CGF.Builder.CreateStore(CGF.Builder.getInt32(0), DidIt); |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3126 | } |
Alexey Bataev | 6956e2e | 2015-02-05 06:35:41 +0000 | [diff] [blame] | 3127 | // Prepare arguments and build a call to __kmpc_single |
Alexey Bataev | d7614fb | 2015-04-10 06:33:45 +0000 | [diff] [blame] | 3128 | llvm::Value *Args[] = {emitUpdateLocation(CGF, Loc), getThreadID(CGF, Loc)}; |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 3129 | CommonActionTy Action(createRuntimeFunction(OMPRTL__kmpc_single), Args, |
| 3130 | createRuntimeFunction(OMPRTL__kmpc_end_single), Args, |
| 3131 | /*Conditional=*/true); |
| 3132 | SingleOpGen.setAction(Action); |
| 3133 | emitInlinedDirective(CGF, OMPD_single, SingleOpGen); |
| 3134 | if (DidIt.isValid()) { |
| 3135 | // did_it = 1; |
| 3136 | CGF.Builder.CreateStore(CGF.Builder.getInt32(1), DidIt); |
| 3137 | } |
| 3138 | Action.Done(CGF); |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3139 | // call __kmpc_copyprivate(ident_t *, gtid, <buf_size>, <copyprivate list>, |
| 3140 | // <copy_func>, did_it); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3141 | if (DidIt.isValid()) { |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3142 | llvm::APInt ArraySize(/*unsigned int numBits=*/32, CopyprivateVars.size()); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3143 | QualType CopyprivateArrayTy = |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3144 | C.getConstantArrayType(C.VoidPtrTy, ArraySize, ArrayType::Normal, |
| 3145 | /*IndexTypeQuals=*/0); |
| 3146 | // Create a list of all private variables for copyprivate. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3147 | Address CopyprivateList = |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3148 | CGF.CreateMemTemp(CopyprivateArrayTy, ".omp.copyprivate.cpr_list"); |
| 3149 | for (unsigned I = 0, E = CopyprivateVars.size(); I < E; ++I) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3150 | Address Elem = CGF.Builder.CreateConstArrayGEP( |
| 3151 | CopyprivateList, I, CGF.getPointerSize()); |
| 3152 | CGF.Builder.CreateStore( |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3153 | CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3154 | CGF.EmitLValue(CopyprivateVars[I]).getPointer(), CGF.VoidPtrTy), |
| 3155 | Elem); |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3156 | } |
| 3157 | // Build function that copies private values from single region to all other |
| 3158 | // threads in the corresponding parallel region. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3159 | llvm::Value *CpyFn = emitCopyprivateCopyFunction( |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3160 | CGM, CGF.ConvertTypeForMem(CopyprivateArrayTy)->getPointerTo(), |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 3161 | CopyprivateVars, SrcExprs, DstExprs, AssignmentOps, Loc); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3162 | llvm::Value *BufSize = CGF.getTypeSize(CopyprivateArrayTy); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3163 | Address CL = |
| 3164 | CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(CopyprivateList, |
| 3165 | CGF.VoidPtrTy); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3166 | llvm::Value *DidItVal = CGF.Builder.CreateLoad(DidIt); |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3167 | llvm::Value *Args[] = { |
| 3168 | emitUpdateLocation(CGF, Loc), // ident_t *<loc> |
| 3169 | getThreadID(CGF, Loc), // i32 <gtid> |
Alexey Bataev | 66beaa9 | 2015-04-30 03:47:32 +0000 | [diff] [blame] | 3170 | BufSize, // size_t <buf_size> |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3171 | CL.getPointer(), // void *<copyprivate list> |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3172 | CpyFn, // void (*) (void *, void *) <copy_func> |
| 3173 | DidItVal // i32 did_it |
| 3174 | }; |
| 3175 | CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_copyprivate), Args); |
| 3176 | } |
Alexey Bataev | 6956e2e | 2015-02-05 06:35:41 +0000 | [diff] [blame] | 3177 | } |
| 3178 | |
Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 3179 | void CGOpenMPRuntime::emitOrderedRegion(CodeGenFunction &CGF, |
| 3180 | const RegionCodeGenTy &OrderedOpGen, |
Alexey Bataev | 5f600d6 | 2015-09-29 03:48:57 +0000 | [diff] [blame] | 3181 | SourceLocation Loc, bool IsThreads) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 3182 | if (!CGF.HaveInsertPoint()) |
| 3183 | return; |
Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 3184 | // __kmpc_ordered(ident_t *, gtid); |
| 3185 | // OrderedOpGen(); |
| 3186 | // __kmpc_end_ordered(ident_t *, gtid); |
| 3187 | // Prepare arguments and build a call to __kmpc_ordered |
Alexey Bataev | 5f600d6 | 2015-09-29 03:48:57 +0000 | [diff] [blame] | 3188 | if (IsThreads) { |
Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 3189 | llvm::Value *Args[] = {emitUpdateLocation(CGF, Loc), getThreadID(CGF, Loc)}; |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 3190 | CommonActionTy Action(createRuntimeFunction(OMPRTL__kmpc_ordered), Args, |
| 3191 | createRuntimeFunction(OMPRTL__kmpc_end_ordered), |
| 3192 | Args); |
| 3193 | OrderedOpGen.setAction(Action); |
| 3194 | emitInlinedDirective(CGF, OMPD_ordered, OrderedOpGen); |
| 3195 | return; |
Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 3196 | } |
Alexey Bataev | 5f600d6 | 2015-09-29 03:48:57 +0000 | [diff] [blame] | 3197 | emitInlinedDirective(CGF, OMPD_ordered, OrderedOpGen); |
Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 3198 | } |
| 3199 | |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 3200 | void CGOpenMPRuntime::emitBarrierCall(CodeGenFunction &CGF, SourceLocation Loc, |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 3201 | OpenMPDirectiveKind Kind, bool EmitChecks, |
| 3202 | bool ForceSimpleCall) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 3203 | if (!CGF.HaveInsertPoint()) |
| 3204 | return; |
Alexey Bataev | 8f7c1b0 | 2014-12-05 04:09:23 +0000 | [diff] [blame] | 3205 | // Build call __kmpc_cancel_barrier(loc, thread_id); |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 3206 | // Build call __kmpc_barrier(loc, thread_id); |
Alexey Bataev | 50b3c95 | 2016-02-19 10:38:26 +0000 | [diff] [blame] | 3207 | unsigned Flags; |
| 3208 | if (Kind == OMPD_for) |
| 3209 | Flags = OMP_IDENT_BARRIER_IMPL_FOR; |
| 3210 | else if (Kind == OMPD_sections) |
| 3211 | Flags = OMP_IDENT_BARRIER_IMPL_SECTIONS; |
| 3212 | else if (Kind == OMPD_single) |
| 3213 | Flags = OMP_IDENT_BARRIER_IMPL_SINGLE; |
| 3214 | else if (Kind == OMPD_barrier) |
| 3215 | Flags = OMP_IDENT_BARRIER_EXPL; |
| 3216 | else |
| 3217 | Flags = OMP_IDENT_BARRIER_IMPL; |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 3218 | // Build call __kmpc_cancel_barrier(loc, thread_id) or __kmpc_barrier(loc, |
| 3219 | // thread_id); |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 3220 | llvm::Value *Args[] = {emitUpdateLocation(CGF, Loc, Flags), |
| 3221 | getThreadID(CGF, Loc)}; |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 3222 | if (auto *OMPRegionInfo = |
| 3223 | dyn_cast_or_null<CGOpenMPRegionInfo>(CGF.CapturedStmtInfo)) { |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 3224 | if (!ForceSimpleCall && OMPRegionInfo->hasCancel()) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3225 | llvm::Value *Result = CGF.EmitRuntimeCall( |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 3226 | createRuntimeFunction(OMPRTL__kmpc_cancel_barrier), Args); |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 3227 | if (EmitChecks) { |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 3228 | // if (__kmpc_cancel_barrier()) { |
| 3229 | // exit from construct; |
| 3230 | // } |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3231 | llvm::BasicBlock *ExitBB = CGF.createBasicBlock(".cancel.exit"); |
| 3232 | llvm::BasicBlock *ContBB = CGF.createBasicBlock(".cancel.continue"); |
| 3233 | llvm::Value *Cmp = CGF.Builder.CreateIsNotNull(Result); |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 3234 | CGF.Builder.CreateCondBr(Cmp, ExitBB, ContBB); |
| 3235 | CGF.EmitBlock(ExitBB); |
| 3236 | // exit from construct; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3237 | CodeGenFunction::JumpDest CancelDestination = |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 3238 | CGF.getOMPCancelDestination(OMPRegionInfo->getDirectiveKind()); |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 3239 | CGF.EmitBranchThroughCleanup(CancelDestination); |
| 3240 | CGF.EmitBlock(ContBB, /*IsFinished=*/true); |
| 3241 | } |
| 3242 | return; |
| 3243 | } |
| 3244 | } |
| 3245 | CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_barrier), Args); |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 3246 | } |
| 3247 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 3248 | /// Map the OpenMP loop schedule to the runtime enumeration. |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 3249 | static OpenMPSchedType getRuntimeSchedule(OpenMPScheduleClauseKind ScheduleKind, |
Alexey Bataev | d7589ffe | 2015-05-20 13:12:48 +0000 | [diff] [blame] | 3250 | bool Chunked, bool Ordered) { |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 3251 | switch (ScheduleKind) { |
| 3252 | case OMPC_SCHEDULE_static: |
Alexey Bataev | d7589ffe | 2015-05-20 13:12:48 +0000 | [diff] [blame] | 3253 | return Chunked ? (Ordered ? OMP_ord_static_chunked : OMP_sch_static_chunked) |
| 3254 | : (Ordered ? OMP_ord_static : OMP_sch_static); |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 3255 | case OMPC_SCHEDULE_dynamic: |
Alexey Bataev | d7589ffe | 2015-05-20 13:12:48 +0000 | [diff] [blame] | 3256 | return Ordered ? OMP_ord_dynamic_chunked : OMP_sch_dynamic_chunked; |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 3257 | case OMPC_SCHEDULE_guided: |
Alexey Bataev | d7589ffe | 2015-05-20 13:12:48 +0000 | [diff] [blame] | 3258 | return Ordered ? OMP_ord_guided_chunked : OMP_sch_guided_chunked; |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 3259 | case OMPC_SCHEDULE_runtime: |
Alexey Bataev | d7589ffe | 2015-05-20 13:12:48 +0000 | [diff] [blame] | 3260 | return Ordered ? OMP_ord_runtime : OMP_sch_runtime; |
| 3261 | case OMPC_SCHEDULE_auto: |
| 3262 | return Ordered ? OMP_ord_auto : OMP_sch_auto; |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 3263 | case OMPC_SCHEDULE_unknown: |
| 3264 | assert(!Chunked && "chunk was specified but schedule kind not known"); |
Alexey Bataev | d7589ffe | 2015-05-20 13:12:48 +0000 | [diff] [blame] | 3265 | return Ordered ? OMP_ord_static : OMP_sch_static; |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 3266 | } |
| 3267 | llvm_unreachable("Unexpected runtime schedule"); |
| 3268 | } |
| 3269 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 3270 | /// Map the OpenMP distribute schedule to the runtime enumeration. |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3271 | static OpenMPSchedType |
| 3272 | getRuntimeSchedule(OpenMPDistScheduleClauseKind ScheduleKind, bool Chunked) { |
| 3273 | // only static is allowed for dist_schedule |
| 3274 | return Chunked ? OMP_dist_sch_static_chunked : OMP_dist_sch_static; |
| 3275 | } |
| 3276 | |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 3277 | bool CGOpenMPRuntime::isStaticNonchunked(OpenMPScheduleClauseKind ScheduleKind, |
| 3278 | bool Chunked) const { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3279 | OpenMPSchedType Schedule = |
| 3280 | getRuntimeSchedule(ScheduleKind, Chunked, /*Ordered=*/false); |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 3281 | return Schedule == OMP_sch_static; |
| 3282 | } |
| 3283 | |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3284 | bool CGOpenMPRuntime::isStaticNonchunked( |
| 3285 | OpenMPDistScheduleClauseKind ScheduleKind, bool Chunked) const { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3286 | OpenMPSchedType Schedule = getRuntimeSchedule(ScheduleKind, Chunked); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3287 | return Schedule == OMP_dist_sch_static; |
| 3288 | } |
| 3289 | |
| 3290 | |
Alexander Musman | df7a8e2 | 2015-01-22 08:49:35 +0000 | [diff] [blame] | 3291 | bool CGOpenMPRuntime::isDynamic(OpenMPScheduleClauseKind ScheduleKind) const { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3292 | OpenMPSchedType Schedule = |
Alexey Bataev | d7589ffe | 2015-05-20 13:12:48 +0000 | [diff] [blame] | 3293 | getRuntimeSchedule(ScheduleKind, /*Chunked=*/false, /*Ordered=*/false); |
Alexander Musman | df7a8e2 | 2015-01-22 08:49:35 +0000 | [diff] [blame] | 3294 | assert(Schedule != OMP_sch_static_chunked && "cannot be chunked here"); |
| 3295 | return Schedule != OMP_sch_static; |
| 3296 | } |
| 3297 | |
Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 3298 | static int addMonoNonMonoModifier(OpenMPSchedType Schedule, |
| 3299 | OpenMPScheduleClauseModifier M1, |
| 3300 | OpenMPScheduleClauseModifier M2) { |
Alexey Bataev | 6cff624 | 2016-05-30 13:05:14 +0000 | [diff] [blame] | 3301 | int Modifier = 0; |
Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 3302 | switch (M1) { |
| 3303 | case OMPC_SCHEDULE_MODIFIER_monotonic: |
Alexey Bataev | 6cff624 | 2016-05-30 13:05:14 +0000 | [diff] [blame] | 3304 | Modifier = OMP_sch_modifier_monotonic; |
| 3305 | break; |
Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 3306 | case OMPC_SCHEDULE_MODIFIER_nonmonotonic: |
Alexey Bataev | 6cff624 | 2016-05-30 13:05:14 +0000 | [diff] [blame] | 3307 | Modifier = OMP_sch_modifier_nonmonotonic; |
| 3308 | break; |
Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 3309 | case OMPC_SCHEDULE_MODIFIER_simd: |
Alexey Bataev | 6cff624 | 2016-05-30 13:05:14 +0000 | [diff] [blame] | 3310 | if (Schedule == OMP_sch_static_chunked) |
| 3311 | Schedule = OMP_sch_static_balanced_chunked; |
| 3312 | break; |
Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 3313 | case OMPC_SCHEDULE_MODIFIER_last: |
| 3314 | case OMPC_SCHEDULE_MODIFIER_unknown: |
| 3315 | break; |
| 3316 | } |
| 3317 | switch (M2) { |
| 3318 | case OMPC_SCHEDULE_MODIFIER_monotonic: |
Alexey Bataev | 6cff624 | 2016-05-30 13:05:14 +0000 | [diff] [blame] | 3319 | Modifier = OMP_sch_modifier_monotonic; |
| 3320 | break; |
Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 3321 | case OMPC_SCHEDULE_MODIFIER_nonmonotonic: |
Alexey Bataev | 6cff624 | 2016-05-30 13:05:14 +0000 | [diff] [blame] | 3322 | Modifier = OMP_sch_modifier_nonmonotonic; |
| 3323 | break; |
Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 3324 | case OMPC_SCHEDULE_MODIFIER_simd: |
Alexey Bataev | 6cff624 | 2016-05-30 13:05:14 +0000 | [diff] [blame] | 3325 | if (Schedule == OMP_sch_static_chunked) |
| 3326 | Schedule = OMP_sch_static_balanced_chunked; |
| 3327 | break; |
Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 3328 | case OMPC_SCHEDULE_MODIFIER_last: |
| 3329 | case OMPC_SCHEDULE_MODIFIER_unknown: |
| 3330 | break; |
| 3331 | } |
Alexey Bataev | 6cff624 | 2016-05-30 13:05:14 +0000 | [diff] [blame] | 3332 | return Schedule | Modifier; |
Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 3333 | } |
| 3334 | |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 3335 | void CGOpenMPRuntime::emitForDispatchInit( |
| 3336 | CodeGenFunction &CGF, SourceLocation Loc, |
| 3337 | const OpenMPScheduleTy &ScheduleKind, unsigned IVSize, bool IVSigned, |
| 3338 | bool Ordered, const DispatchRTInput &DispatchValues) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 3339 | if (!CGF.HaveInsertPoint()) |
| 3340 | return; |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 3341 | OpenMPSchedType Schedule = getRuntimeSchedule( |
| 3342 | ScheduleKind.Schedule, DispatchValues.Chunk != nullptr, Ordered); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3343 | assert(Ordered || |
| 3344 | (Schedule != OMP_sch_static && Schedule != OMP_sch_static_chunked && |
Alexey Bataev | 6cff624 | 2016-05-30 13:05:14 +0000 | [diff] [blame] | 3345 | Schedule != OMP_ord_static && Schedule != OMP_ord_static_chunked && |
| 3346 | Schedule != OMP_sch_static_balanced_chunked)); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3347 | // Call __kmpc_dispatch_init( |
| 3348 | // ident_t *loc, kmp_int32 tid, kmp_int32 schedule, |
| 3349 | // kmp_int[32|64] lower, kmp_int[32|64] upper, |
| 3350 | // kmp_int[32|64] stride, kmp_int[32|64] chunk); |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 3351 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3352 | // 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] | 3353 | llvm::Value *Chunk = DispatchValues.Chunk ? DispatchValues.Chunk |
| 3354 | : CGF.Builder.getIntN(IVSize, 1); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3355 | llvm::Value *Args[] = { |
Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 3356 | emitUpdateLocation(CGF, Loc), getThreadID(CGF, Loc), |
| 3357 | CGF.Builder.getInt32(addMonoNonMonoModifier( |
| 3358 | Schedule, ScheduleKind.M1, ScheduleKind.M2)), // Schedule type |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 3359 | DispatchValues.LB, // Lower |
| 3360 | DispatchValues.UB, // Upper |
Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 3361 | CGF.Builder.getIntN(IVSize, 1), // Stride |
| 3362 | Chunk // Chunk |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3363 | }; |
| 3364 | CGF.EmitRuntimeCall(createDispatchInitFunction(IVSize, IVSigned), Args); |
| 3365 | } |
| 3366 | |
Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 3367 | static void emitForStaticInitCall( |
| 3368 | CodeGenFunction &CGF, llvm::Value *UpdateLocation, llvm::Value *ThreadId, |
| 3369 | llvm::Constant *ForStaticInitFunction, OpenMPSchedType Schedule, |
| 3370 | OpenMPScheduleClauseModifier M1, OpenMPScheduleClauseModifier M2, |
Alexey Bataev | 0f87dbe | 2017-08-14 17:56:13 +0000 | [diff] [blame] | 3371 | const CGOpenMPRuntime::StaticRTInput &Values) { |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3372 | if (!CGF.HaveInsertPoint()) |
Alexey Bataev | 0f87dbe | 2017-08-14 17:56:13 +0000 | [diff] [blame] | 3373 | return; |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3374 | |
Alexey Bataev | 0f87dbe | 2017-08-14 17:56:13 +0000 | [diff] [blame] | 3375 | assert(!Values.Ordered); |
| 3376 | assert(Schedule == OMP_sch_static || Schedule == OMP_sch_static_chunked || |
| 3377 | Schedule == OMP_sch_static_balanced_chunked || |
| 3378 | Schedule == OMP_ord_static || Schedule == OMP_ord_static_chunked || |
| 3379 | Schedule == OMP_dist_sch_static || |
| 3380 | Schedule == OMP_dist_sch_static_chunked); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3381 | |
Alexey Bataev | 0f87dbe | 2017-08-14 17:56:13 +0000 | [diff] [blame] | 3382 | // Call __kmpc_for_static_init( |
| 3383 | // ident_t *loc, kmp_int32 tid, kmp_int32 schedtype, |
| 3384 | // kmp_int32 *p_lastiter, kmp_int[32|64] *p_lower, |
| 3385 | // kmp_int[32|64] *p_upper, kmp_int[32|64] *p_stride, |
| 3386 | // kmp_int[32|64] incr, kmp_int[32|64] chunk); |
| 3387 | llvm::Value *Chunk = Values.Chunk; |
| 3388 | if (Chunk == nullptr) { |
| 3389 | assert((Schedule == OMP_sch_static || Schedule == OMP_ord_static || |
| 3390 | Schedule == OMP_dist_sch_static) && |
| 3391 | "expected static non-chunked schedule"); |
| 3392 | // If the Chunk was not specified in the clause - use default value 1. |
| 3393 | Chunk = CGF.Builder.getIntN(Values.IVSize, 1); |
| 3394 | } else { |
| 3395 | assert((Schedule == OMP_sch_static_chunked || |
| 3396 | Schedule == OMP_sch_static_balanced_chunked || |
| 3397 | Schedule == OMP_ord_static_chunked || |
| 3398 | Schedule == OMP_dist_sch_static_chunked) && |
| 3399 | "expected static chunked schedule"); |
| 3400 | } |
| 3401 | llvm::Value *Args[] = { |
| 3402 | UpdateLocation, |
| 3403 | ThreadId, |
| 3404 | CGF.Builder.getInt32(addMonoNonMonoModifier(Schedule, M1, |
| 3405 | M2)), // Schedule type |
| 3406 | Values.IL.getPointer(), // &isLastIter |
| 3407 | Values.LB.getPointer(), // &LB |
| 3408 | Values.UB.getPointer(), // &UB |
| 3409 | Values.ST.getPointer(), // &Stride |
| 3410 | CGF.Builder.getIntN(Values.IVSize, 1), // Incr |
| 3411 | Chunk // Chunk |
| 3412 | }; |
| 3413 | CGF.EmitRuntimeCall(ForStaticInitFunction, Args); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3414 | } |
| 3415 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3416 | void CGOpenMPRuntime::emitForStaticInit(CodeGenFunction &CGF, |
| 3417 | SourceLocation Loc, |
Alexey Bataev | 0f87dbe | 2017-08-14 17:56:13 +0000 | [diff] [blame] | 3418 | OpenMPDirectiveKind DKind, |
Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 3419 | const OpenMPScheduleTy &ScheduleKind, |
Alexey Bataev | 0f87dbe | 2017-08-14 17:56:13 +0000 | [diff] [blame] | 3420 | const StaticRTInput &Values) { |
| 3421 | OpenMPSchedType ScheduleNum = getRuntimeSchedule( |
| 3422 | ScheduleKind.Schedule, Values.Chunk != nullptr, Values.Ordered); |
| 3423 | assert(isOpenMPWorksharingDirective(DKind) && |
| 3424 | "Expected loop-based or sections-based directive."); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3425 | llvm::Value *UpdatedLocation = emitUpdateLocation(CGF, Loc, |
Alexey Bataev | 0f87dbe | 2017-08-14 17:56:13 +0000 | [diff] [blame] | 3426 | isOpenMPLoopDirective(DKind) |
| 3427 | ? OMP_IDENT_WORK_LOOP |
| 3428 | : OMP_IDENT_WORK_SECTIONS); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3429 | llvm::Value *ThreadId = getThreadID(CGF, Loc); |
| 3430 | llvm::Constant *StaticInitFunction = |
Alexey Bataev | 0f87dbe | 2017-08-14 17:56:13 +0000 | [diff] [blame] | 3431 | createForStaticInitFunction(Values.IVSize, Values.IVSigned); |
Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 3432 | emitForStaticInitCall(CGF, UpdatedLocation, ThreadId, StaticInitFunction, |
Alexey Bataev | 0f87dbe | 2017-08-14 17:56:13 +0000 | [diff] [blame] | 3433 | ScheduleNum, ScheduleKind.M1, ScheduleKind.M2, Values); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3434 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3435 | |
Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 3436 | void CGOpenMPRuntime::emitDistributeStaticInit( |
| 3437 | CodeGenFunction &CGF, SourceLocation Loc, |
Alexey Bataev | 0f87dbe | 2017-08-14 17:56:13 +0000 | [diff] [blame] | 3438 | OpenMPDistScheduleClauseKind SchedKind, |
| 3439 | const CGOpenMPRuntime::StaticRTInput &Values) { |
| 3440 | OpenMPSchedType ScheduleNum = |
| 3441 | getRuntimeSchedule(SchedKind, Values.Chunk != nullptr); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3442 | llvm::Value *UpdatedLocation = |
Alexey Bataev | 0f87dbe | 2017-08-14 17:56:13 +0000 | [diff] [blame] | 3443 | emitUpdateLocation(CGF, Loc, OMP_IDENT_WORK_DISTRIBUTE); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3444 | llvm::Value *ThreadId = getThreadID(CGF, Loc); |
| 3445 | llvm::Constant *StaticInitFunction = |
Alexey Bataev | 0f87dbe | 2017-08-14 17:56:13 +0000 | [diff] [blame] | 3446 | createForStaticInitFunction(Values.IVSize, Values.IVSigned); |
Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 3447 | emitForStaticInitCall(CGF, UpdatedLocation, ThreadId, StaticInitFunction, |
| 3448 | ScheduleNum, OMPC_SCHEDULE_MODIFIER_unknown, |
Alexey Bataev | 0f87dbe | 2017-08-14 17:56:13 +0000 | [diff] [blame] | 3449 | OMPC_SCHEDULE_MODIFIER_unknown, Values); |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 3450 | } |
| 3451 | |
Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 3452 | void CGOpenMPRuntime::emitForStaticFinish(CodeGenFunction &CGF, |
Alexey Bataev | f43f714 | 2017-09-06 16:17:35 +0000 | [diff] [blame] | 3453 | SourceLocation Loc, |
| 3454 | OpenMPDirectiveKind DKind) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 3455 | if (!CGF.HaveInsertPoint()) |
| 3456 | return; |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 3457 | // Call __kmpc_for_static_fini(ident_t *loc, kmp_int32 tid); |
Alexey Bataev | f43f714 | 2017-09-06 16:17:35 +0000 | [diff] [blame] | 3458 | llvm::Value *Args[] = { |
| 3459 | emitUpdateLocation(CGF, Loc, |
| 3460 | isOpenMPDistributeDirective(DKind) |
| 3461 | ? OMP_IDENT_WORK_DISTRIBUTE |
| 3462 | : isOpenMPLoopDirective(DKind) |
| 3463 | ? OMP_IDENT_WORK_LOOP |
| 3464 | : OMP_IDENT_WORK_SECTIONS), |
| 3465 | getThreadID(CGF, Loc)}; |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 3466 | CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_for_static_fini), |
| 3467 | Args); |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 3468 | } |
| 3469 | |
Alexey Bataev | d7589ffe | 2015-05-20 13:12:48 +0000 | [diff] [blame] | 3470 | void CGOpenMPRuntime::emitForOrderedIterationEnd(CodeGenFunction &CGF, |
| 3471 | SourceLocation Loc, |
| 3472 | unsigned IVSize, |
| 3473 | bool IVSigned) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 3474 | if (!CGF.HaveInsertPoint()) |
| 3475 | return; |
Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 3476 | // 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] | 3477 | llvm::Value *Args[] = {emitUpdateLocation(CGF, Loc), getThreadID(CGF, Loc)}; |
Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 3478 | CGF.EmitRuntimeCall(createDispatchFiniFunction(IVSize, IVSigned), Args); |
| 3479 | } |
| 3480 | |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 3481 | llvm::Value *CGOpenMPRuntime::emitForNext(CodeGenFunction &CGF, |
| 3482 | SourceLocation Loc, unsigned IVSize, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3483 | bool IVSigned, Address IL, |
| 3484 | Address LB, Address UB, |
| 3485 | Address ST) { |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 3486 | // Call __kmpc_dispatch_next( |
| 3487 | // ident_t *loc, kmp_int32 tid, kmp_int32 *p_lastiter, |
| 3488 | // kmp_int[32|64] *p_lower, kmp_int[32|64] *p_upper, |
| 3489 | // kmp_int[32|64] *p_stride); |
| 3490 | llvm::Value *Args[] = { |
Alexey Bataev | 50b3c95 | 2016-02-19 10:38:26 +0000 | [diff] [blame] | 3491 | emitUpdateLocation(CGF, Loc), |
| 3492 | getThreadID(CGF, Loc), |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3493 | IL.getPointer(), // &isLastIter |
| 3494 | LB.getPointer(), // &Lower |
| 3495 | UB.getPointer(), // &Upper |
| 3496 | ST.getPointer() // &Stride |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 3497 | }; |
| 3498 | llvm::Value *Call = |
| 3499 | CGF.EmitRuntimeCall(createDispatchNextFunction(IVSize, IVSigned), Args); |
| 3500 | return CGF.EmitScalarConversion( |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3501 | Call, CGF.getContext().getIntTypeForBitwidth(32, /*Signed=*/1), |
Filipe Cabecinhas | 7af183d | 2015-08-11 04:19:28 +0000 | [diff] [blame] | 3502 | CGF.getContext().BoolTy, Loc); |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 3503 | } |
| 3504 | |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 3505 | void CGOpenMPRuntime::emitNumThreadsClause(CodeGenFunction &CGF, |
| 3506 | llvm::Value *NumThreads, |
| 3507 | SourceLocation Loc) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 3508 | if (!CGF.HaveInsertPoint()) |
| 3509 | return; |
Alexey Bataev | b205978 | 2014-10-13 08:23:51 +0000 | [diff] [blame] | 3510 | // Build call __kmpc_push_num_threads(&loc, global_tid, num_threads) |
| 3511 | llvm::Value *Args[] = { |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 3512 | emitUpdateLocation(CGF, Loc), getThreadID(CGF, Loc), |
Alexey Bataev | b205978 | 2014-10-13 08:23:51 +0000 | [diff] [blame] | 3513 | CGF.Builder.CreateIntCast(NumThreads, CGF.Int32Ty, /*isSigned*/ true)}; |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 3514 | CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_push_num_threads), |
| 3515 | Args); |
Alexey Bataev | b205978 | 2014-10-13 08:23:51 +0000 | [diff] [blame] | 3516 | } |
| 3517 | |
Alexey Bataev | 7f210c6 | 2015-06-18 13:40:03 +0000 | [diff] [blame] | 3518 | void CGOpenMPRuntime::emitProcBindClause(CodeGenFunction &CGF, |
| 3519 | OpenMPProcBindClauseKind ProcBind, |
| 3520 | SourceLocation Loc) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 3521 | if (!CGF.HaveInsertPoint()) |
| 3522 | return; |
Alexey Bataev | 7f210c6 | 2015-06-18 13:40:03 +0000 | [diff] [blame] | 3523 | // Constants for proc bind value accepted by the runtime. |
| 3524 | enum ProcBindTy { |
| 3525 | ProcBindFalse = 0, |
| 3526 | ProcBindTrue, |
| 3527 | ProcBindMaster, |
| 3528 | ProcBindClose, |
| 3529 | ProcBindSpread, |
| 3530 | ProcBindIntel, |
| 3531 | ProcBindDefault |
| 3532 | } RuntimeProcBind; |
| 3533 | switch (ProcBind) { |
| 3534 | case OMPC_PROC_BIND_master: |
| 3535 | RuntimeProcBind = ProcBindMaster; |
| 3536 | break; |
| 3537 | case OMPC_PROC_BIND_close: |
| 3538 | RuntimeProcBind = ProcBindClose; |
| 3539 | break; |
| 3540 | case OMPC_PROC_BIND_spread: |
| 3541 | RuntimeProcBind = ProcBindSpread; |
| 3542 | break; |
| 3543 | case OMPC_PROC_BIND_unknown: |
| 3544 | llvm_unreachable("Unsupported proc_bind value."); |
| 3545 | } |
| 3546 | // Build call __kmpc_push_proc_bind(&loc, global_tid, proc_bind) |
| 3547 | llvm::Value *Args[] = { |
| 3548 | emitUpdateLocation(CGF, Loc), getThreadID(CGF, Loc), |
| 3549 | llvm::ConstantInt::get(CGM.IntTy, RuntimeProcBind, /*isSigned=*/true)}; |
| 3550 | CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_push_proc_bind), Args); |
| 3551 | } |
| 3552 | |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 3553 | void CGOpenMPRuntime::emitFlush(CodeGenFunction &CGF, ArrayRef<const Expr *>, |
| 3554 | SourceLocation Loc) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 3555 | if (!CGF.HaveInsertPoint()) |
| 3556 | return; |
Alexey Bataev | d76df6d | 2015-02-24 12:55:09 +0000 | [diff] [blame] | 3557 | // Build call void __kmpc_flush(ident_t *loc) |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 3558 | CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_flush), |
| 3559 | emitUpdateLocation(CGF, Loc)); |
Alexey Bataev | cc37cc1 | 2014-11-20 04:34:54 +0000 | [diff] [blame] | 3560 | } |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 3561 | |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 3562 | namespace { |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 3563 | /// Indexes of fields for type kmp_task_t. |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 3564 | enum KmpTaskTFields { |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 3565 | /// List of shared variables. |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 3566 | KmpTaskTShareds, |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 3567 | /// Task routine. |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 3568 | KmpTaskTRoutine, |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 3569 | /// Partition id for the untied tasks. |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 3570 | KmpTaskTPartId, |
Alexey Bataev | ad537bb | 2016-05-30 09:06:50 +0000 | [diff] [blame] | 3571 | /// Function with call of destructors for private variables. |
| 3572 | Data1, |
| 3573 | /// Task priority. |
| 3574 | Data2, |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 3575 | /// (Taskloops only) Lower bound. |
| 3576 | KmpTaskTLowerBound, |
| 3577 | /// (Taskloops only) Upper bound. |
| 3578 | KmpTaskTUpperBound, |
| 3579 | /// (Taskloops only) Stride. |
| 3580 | KmpTaskTStride, |
| 3581 | /// (Taskloops only) Is last iteration flag. |
| 3582 | KmpTaskTLastIter, |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 3583 | /// (Taskloops only) Reduction data. |
| 3584 | KmpTaskTReductions, |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 3585 | }; |
Hans Wennborg | 7eb5464 | 2015-09-10 17:07:54 +0000 | [diff] [blame] | 3586 | } // anonymous namespace |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 3587 | |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3588 | bool CGOpenMPRuntime::OffloadEntriesInfoManagerTy::empty() const { |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 3589 | return OffloadEntriesTargetRegion.empty() && |
| 3590 | OffloadEntriesDeviceGlobalVar.empty(); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3591 | } |
| 3592 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 3593 | /// Initialize target region entry. |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3594 | void CGOpenMPRuntime::OffloadEntriesInfoManagerTy:: |
| 3595 | initializeTargetRegionEntryInfo(unsigned DeviceID, unsigned FileID, |
| 3596 | StringRef ParentName, unsigned LineNum, |
Samuel Antao | 2de62b0 | 2016-02-13 23:35:10 +0000 | [diff] [blame] | 3597 | unsigned Order) { |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3598 | assert(CGM.getLangOpts().OpenMPIsDevice && "Initialization of entries is " |
| 3599 | "only required for the device " |
| 3600 | "code generation."); |
Samuel Antao | 2de62b0 | 2016-02-13 23:35:10 +0000 | [diff] [blame] | 3601 | OffloadEntriesTargetRegion[DeviceID][FileID][ParentName][LineNum] = |
Samuel Antao | f83efdb | 2017-01-05 16:02:49 +0000 | [diff] [blame] | 3602 | OffloadEntryInfoTargetRegion(Order, /*Addr=*/nullptr, /*ID=*/nullptr, |
Alexey Bataev | 34f8a70 | 2018-03-28 14:28:54 +0000 | [diff] [blame] | 3603 | OMPTargetRegionEntryTargetRegion); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3604 | ++OffloadingEntriesNum; |
| 3605 | } |
| 3606 | |
| 3607 | void CGOpenMPRuntime::OffloadEntriesInfoManagerTy:: |
| 3608 | registerTargetRegionEntryInfo(unsigned DeviceID, unsigned FileID, |
| 3609 | StringRef ParentName, unsigned LineNum, |
Samuel Antao | f83efdb | 2017-01-05 16:02:49 +0000 | [diff] [blame] | 3610 | llvm::Constant *Addr, llvm::Constant *ID, |
Alexey Bataev | 34f8a70 | 2018-03-28 14:28:54 +0000 | [diff] [blame] | 3611 | OMPTargetRegionEntryKind Flags) { |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3612 | // If we are emitting code for a target, the entry is already initialized, |
| 3613 | // only has to be registered. |
| 3614 | if (CGM.getLangOpts().OpenMPIsDevice) { |
Alexey Bataev | 64e62dc | 2018-04-30 16:26:57 +0000 | [diff] [blame] | 3615 | if (!hasTargetRegionEntryInfo(DeviceID, FileID, ParentName, LineNum)) { |
| 3616 | unsigned DiagID = CGM.getDiags().getCustomDiagID( |
| 3617 | DiagnosticsEngine::Error, |
| 3618 | "Unable to find target region on line '%0' in the device code."); |
| 3619 | CGM.getDiags().Report(DiagID) << LineNum; |
| 3620 | return; |
| 3621 | } |
Samuel Antao | 2de62b0 | 2016-02-13 23:35:10 +0000 | [diff] [blame] | 3622 | auto &Entry = |
| 3623 | OffloadEntriesTargetRegion[DeviceID][FileID][ParentName][LineNum]; |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3624 | assert(Entry.isValid() && "Entry not initialized!"); |
| 3625 | Entry.setAddress(Addr); |
| 3626 | Entry.setID(ID); |
Samuel Antao | f83efdb | 2017-01-05 16:02:49 +0000 | [diff] [blame] | 3627 | Entry.setFlags(Flags); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3628 | } else { |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 3629 | OffloadEntryInfoTargetRegion Entry(OffloadingEntriesNum, Addr, ID, Flags); |
Samuel Antao | 2de62b0 | 2016-02-13 23:35:10 +0000 | [diff] [blame] | 3630 | OffloadEntriesTargetRegion[DeviceID][FileID][ParentName][LineNum] = Entry; |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 3631 | ++OffloadingEntriesNum; |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3632 | } |
| 3633 | } |
| 3634 | |
| 3635 | bool CGOpenMPRuntime::OffloadEntriesInfoManagerTy::hasTargetRegionEntryInfo( |
Samuel Antao | 2de62b0 | 2016-02-13 23:35:10 +0000 | [diff] [blame] | 3636 | unsigned DeviceID, unsigned FileID, StringRef ParentName, |
| 3637 | unsigned LineNum) const { |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3638 | auto PerDevice = OffloadEntriesTargetRegion.find(DeviceID); |
| 3639 | if (PerDevice == OffloadEntriesTargetRegion.end()) |
| 3640 | return false; |
| 3641 | auto PerFile = PerDevice->second.find(FileID); |
| 3642 | if (PerFile == PerDevice->second.end()) |
| 3643 | return false; |
| 3644 | auto PerParentName = PerFile->second.find(ParentName); |
| 3645 | if (PerParentName == PerFile->second.end()) |
| 3646 | return false; |
| 3647 | auto PerLine = PerParentName->second.find(LineNum); |
| 3648 | if (PerLine == PerParentName->second.end()) |
| 3649 | return false; |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3650 | // Fail if this entry is already registered. |
Samuel Antao | 2de62b0 | 2016-02-13 23:35:10 +0000 | [diff] [blame] | 3651 | if (PerLine->second.getAddress() || PerLine->second.getID()) |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3652 | return false; |
| 3653 | return true; |
| 3654 | } |
| 3655 | |
| 3656 | void CGOpenMPRuntime::OffloadEntriesInfoManagerTy::actOnTargetRegionEntriesInfo( |
| 3657 | const OffloadTargetRegionEntryInfoActTy &Action) { |
| 3658 | // Scan all target region entries and perform the provided action. |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 3659 | for (const auto &D : OffloadEntriesTargetRegion) |
| 3660 | for (const auto &F : D.second) |
| 3661 | for (const auto &P : F.second) |
| 3662 | for (const auto &L : P.second) |
Samuel Antao | 2de62b0 | 2016-02-13 23:35:10 +0000 | [diff] [blame] | 3663 | Action(D.first, F.first, P.first(), L.first, L.second); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3664 | } |
| 3665 | |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 3666 | void CGOpenMPRuntime::OffloadEntriesInfoManagerTy:: |
| 3667 | initializeDeviceGlobalVarEntryInfo(StringRef Name, |
| 3668 | OMPTargetGlobalVarEntryKind Flags, |
| 3669 | unsigned Order) { |
| 3670 | assert(CGM.getLangOpts().OpenMPIsDevice && "Initialization of entries is " |
| 3671 | "only required for the device " |
| 3672 | "code generation."); |
| 3673 | OffloadEntriesDeviceGlobalVar.try_emplace(Name, Order, Flags); |
| 3674 | ++OffloadingEntriesNum; |
| 3675 | } |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3676 | |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 3677 | void CGOpenMPRuntime::OffloadEntriesInfoManagerTy:: |
| 3678 | registerDeviceGlobalVarEntryInfo(StringRef VarName, llvm::Constant *Addr, |
| 3679 | CharUnits VarSize, |
| 3680 | OMPTargetGlobalVarEntryKind Flags, |
| 3681 | llvm::GlobalValue::LinkageTypes Linkage) { |
| 3682 | if (CGM.getLangOpts().OpenMPIsDevice) { |
| 3683 | auto &Entry = OffloadEntriesDeviceGlobalVar[VarName]; |
| 3684 | assert(Entry.isValid() && Entry.getFlags() == Flags && |
| 3685 | "Entry not initialized!"); |
| 3686 | assert((!Entry.getAddress() || Entry.getAddress() == Addr) && |
| 3687 | "Resetting with the new address."); |
| 3688 | if (Entry.getAddress() && hasDeviceGlobalVarEntryInfo(VarName)) |
| 3689 | return; |
| 3690 | Entry.setAddress(Addr); |
| 3691 | Entry.setVarSize(VarSize); |
| 3692 | Entry.setLinkage(Linkage); |
| 3693 | } else { |
| 3694 | if (hasDeviceGlobalVarEntryInfo(VarName)) |
| 3695 | return; |
| 3696 | OffloadEntriesDeviceGlobalVar.try_emplace( |
| 3697 | VarName, OffloadingEntriesNum, Addr, VarSize, Flags, Linkage); |
| 3698 | ++OffloadingEntriesNum; |
| 3699 | } |
| 3700 | } |
| 3701 | |
| 3702 | void CGOpenMPRuntime::OffloadEntriesInfoManagerTy:: |
| 3703 | actOnDeviceGlobalVarEntriesInfo( |
| 3704 | const OffloadDeviceGlobalVarEntryInfoActTy &Action) { |
| 3705 | // Scan all target region entries and perform the provided action. |
| 3706 | for (const auto &E : OffloadEntriesDeviceGlobalVar) |
| 3707 | Action(E.getKey(), E.getValue()); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3708 | } |
| 3709 | |
| 3710 | llvm::Function * |
| 3711 | CGOpenMPRuntime::createOffloadingBinaryDescriptorRegistration() { |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3712 | // If we don't have entries or if we are emitting code for the device, we |
| 3713 | // don't need to do anything. |
| 3714 | if (CGM.getLangOpts().OpenMPIsDevice || OffloadEntriesInfoManager.empty()) |
| 3715 | return nullptr; |
| 3716 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3717 | llvm::Module &M = CGM.getModule(); |
| 3718 | ASTContext &C = CGM.getContext(); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3719 | |
| 3720 | // Get list of devices we care about |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3721 | const std::vector<llvm::Triple> &Devices = CGM.getLangOpts().OMPTargetTriples; |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3722 | |
| 3723 | // We should be creating an offloading descriptor only if there are devices |
| 3724 | // specified. |
| 3725 | assert(!Devices.empty() && "No OpenMP offloading devices??"); |
| 3726 | |
| 3727 | // Create the external variables that will point to the begin and end of the |
| 3728 | // host entries section. These will be defined by the linker. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3729 | llvm::Type *OffloadEntryTy = |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3730 | CGM.getTypes().ConvertTypeForMem(getTgtOffloadEntryQTy()); |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 3731 | std::string EntriesBeginName = getName({"omp_offloading", "entries_begin"}); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3732 | auto *HostEntriesBegin = new llvm::GlobalVariable( |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3733 | M, OffloadEntryTy, /*isConstant=*/true, |
Eugene Zelenko | 1660a5d | 2016-01-26 19:01:06 +0000 | [diff] [blame] | 3734 | llvm::GlobalValue::ExternalLinkage, /*Initializer=*/nullptr, |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 3735 | EntriesBeginName); |
| 3736 | std::string EntriesEndName = getName({"omp_offloading", "entries_end"}); |
| 3737 | auto *HostEntriesEnd = |
| 3738 | new llvm::GlobalVariable(M, OffloadEntryTy, /*isConstant=*/true, |
| 3739 | llvm::GlobalValue::ExternalLinkage, |
| 3740 | /*Initializer=*/nullptr, EntriesEndName); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3741 | |
| 3742 | // Create all device images |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3743 | auto *DeviceImageTy = cast<llvm::StructType>( |
| 3744 | CGM.getTypes().ConvertTypeForMem(getTgtDeviceImageQTy())); |
John McCall | 23c9dc6 | 2016-11-28 22:18:27 +0000 | [diff] [blame] | 3745 | ConstantInitBuilder DeviceImagesBuilder(CGM); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3746 | ConstantArrayBuilder DeviceImagesEntries = |
| 3747 | DeviceImagesBuilder.beginArray(DeviceImageTy); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3748 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3749 | for (const llvm::Triple &Device : Devices) { |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 3750 | StringRef T = Device.getTriple(); |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 3751 | std::string BeginName = getName({"omp_offloading", "img_start", ""}); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3752 | auto *ImgBegin = new llvm::GlobalVariable( |
| 3753 | M, CGM.Int8Ty, /*isConstant=*/true, llvm::GlobalValue::ExternalLinkage, |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 3754 | /*Initializer=*/nullptr, Twine(BeginName).concat(T)); |
| 3755 | std::string EndName = getName({"omp_offloading", "img_end", ""}); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3756 | auto *ImgEnd = new llvm::GlobalVariable( |
| 3757 | M, CGM.Int8Ty, /*isConstant=*/true, llvm::GlobalValue::ExternalLinkage, |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 3758 | /*Initializer=*/nullptr, Twine(EndName).concat(T)); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3759 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3760 | llvm::Constant *Data[] = {ImgBegin, ImgEnd, HostEntriesBegin, |
| 3761 | HostEntriesEnd}; |
| 3762 | createConstantGlobalStructAndAddToParent(CGM, getTgtDeviceImageQTy(), Data, |
| 3763 | DeviceImagesEntries); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3764 | } |
| 3765 | |
| 3766 | // Create device images global array. |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 3767 | std::string ImagesName = getName({"omp_offloading", "device_images"}); |
John McCall | 6c9f1fdb | 2016-11-19 08:17:24 +0000 | [diff] [blame] | 3768 | llvm::GlobalVariable *DeviceImages = |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 3769 | DeviceImagesEntries.finishAndCreateGlobal(ImagesName, |
| 3770 | CGM.getPointerAlign(), |
| 3771 | /*isConstant=*/true); |
Peter Collingbourne | bcf909d | 2016-06-14 21:02:05 +0000 | [diff] [blame] | 3772 | DeviceImages->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3773 | |
| 3774 | // This is a Zero array to be used in the creation of the constant expressions |
| 3775 | llvm::Constant *Index[] = {llvm::Constant::getNullValue(CGM.Int32Ty), |
| 3776 | llvm::Constant::getNullValue(CGM.Int32Ty)}; |
| 3777 | |
| 3778 | // Create the target region descriptor. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3779 | llvm::Constant *Data[] = { |
| 3780 | llvm::ConstantInt::get(CGM.Int32Ty, Devices.size()), |
| 3781 | llvm::ConstantExpr::getGetElementPtr(DeviceImages->getValueType(), |
| 3782 | DeviceImages, Index), |
| 3783 | HostEntriesBegin, HostEntriesEnd}; |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 3784 | std::string Descriptor = getName({"omp_offloading", "descriptor"}); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3785 | llvm::GlobalVariable *Desc = createConstantGlobalStruct( |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 3786 | CGM, getTgtBinaryDescriptorQTy(), Data, Descriptor); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3787 | |
| 3788 | // Emit code to register or unregister the descriptor at execution |
| 3789 | // startup or closing, respectively. |
| 3790 | |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 3791 | llvm::Function *UnRegFn; |
| 3792 | { |
| 3793 | FunctionArgList Args; |
| 3794 | ImplicitParamDecl DummyPtr(C, C.VoidPtrTy, ImplicitParamDecl::Other); |
| 3795 | Args.push_back(&DummyPtr); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3796 | |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 3797 | CodeGenFunction CGF(CGM); |
| 3798 | // Disable debug info for global (de-)initializer because they are not part |
| 3799 | // of some particular construct. |
| 3800 | CGF.disableDebugInfo(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3801 | const auto &FI = |
| 3802 | CGM.getTypes().arrangeBuiltinFunctionDeclaration(C.VoidTy, Args); |
| 3803 | llvm::FunctionType *FTy = CGM.getTypes().GetFunctionType(FI); |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 3804 | std::string UnregName = getName({"omp_offloading", "descriptor_unreg"}); |
| 3805 | UnRegFn = CGM.CreateGlobalInitOrDestructFunction(FTy, UnregName, FI); |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 3806 | CGF.StartFunction(GlobalDecl(), C.VoidTy, UnRegFn, FI, Args); |
| 3807 | CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__tgt_unregister_lib), |
| 3808 | Desc); |
| 3809 | CGF.FinishFunction(); |
| 3810 | } |
| 3811 | llvm::Function *RegFn; |
| 3812 | { |
| 3813 | CodeGenFunction CGF(CGM); |
| 3814 | // Disable debug info for global (de-)initializer because they are not part |
| 3815 | // of some particular construct. |
| 3816 | CGF.disableDebugInfo(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3817 | const auto &FI = CGM.getTypes().arrangeNullaryFunction(); |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 3818 | llvm::FunctionType *FTy = CGM.getTypes().GetFunctionType(FI); |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 3819 | std::string Descriptor = getName({"omp_offloading", "descriptor_reg"}); |
| 3820 | RegFn = CGM.CreateGlobalInitOrDestructFunction(FTy, Descriptor, FI); |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 3821 | CGF.StartFunction(GlobalDecl(), C.VoidTy, RegFn, FI, FunctionArgList()); |
| 3822 | CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__tgt_register_lib), Desc); |
| 3823 | // Create a variable to drive the registration and unregistration of the |
| 3824 | // descriptor, so we can reuse the logic that emits Ctors and Dtors. |
| 3825 | ImplicitParamDecl RegUnregVar(C, C.getTranslationUnitDecl(), |
| 3826 | SourceLocation(), nullptr, C.CharTy, |
| 3827 | ImplicitParamDecl::Other); |
| 3828 | CGM.getCXXABI().registerGlobalDtor(CGF, RegUnregVar, UnRegFn, Desc); |
| 3829 | CGF.FinishFunction(); |
| 3830 | } |
George Rokos | 29d0f00 | 2017-05-27 03:03:13 +0000 | [diff] [blame] | 3831 | if (CGM.supportsCOMDAT()) { |
| 3832 | // It is sufficient to call registration function only once, so create a |
| 3833 | // COMDAT group for registration/unregistration functions and associated |
| 3834 | // data. That would reduce startup time and code size. Registration |
| 3835 | // function serves as a COMDAT group key. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3836 | llvm::Comdat *ComdatKey = M.getOrInsertComdat(RegFn->getName()); |
George Rokos | 29d0f00 | 2017-05-27 03:03:13 +0000 | [diff] [blame] | 3837 | RegFn->setLinkage(llvm::GlobalValue::LinkOnceAnyLinkage); |
| 3838 | RegFn->setVisibility(llvm::GlobalValue::HiddenVisibility); |
| 3839 | RegFn->setComdat(ComdatKey); |
| 3840 | UnRegFn->setComdat(ComdatKey); |
| 3841 | DeviceImages->setComdat(ComdatKey); |
| 3842 | Desc->setComdat(ComdatKey); |
| 3843 | } |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3844 | return RegFn; |
| 3845 | } |
| 3846 | |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 3847 | void CGOpenMPRuntime::createOffloadEntry( |
| 3848 | llvm::Constant *ID, llvm::Constant *Addr, uint64_t Size, int32_t Flags, |
| 3849 | llvm::GlobalValue::LinkageTypes Linkage) { |
Samuel Antao | 2de62b0 | 2016-02-13 23:35:10 +0000 | [diff] [blame] | 3850 | StringRef Name = Addr->getName(); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3851 | llvm::Module &M = CGM.getModule(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3852 | llvm::LLVMContext &C = M.getContext(); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3853 | |
| 3854 | // Create constant string with the name. |
| 3855 | llvm::Constant *StrPtrInit = llvm::ConstantDataArray::getString(C, Name); |
| 3856 | |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 3857 | std::string StringName = getName({"omp_offloading", "entry_name"}); |
| 3858 | auto *Str = new llvm::GlobalVariable( |
| 3859 | M, StrPtrInit->getType(), /*isConstant=*/true, |
| 3860 | llvm::GlobalValue::InternalLinkage, StrPtrInit, StringName); |
Peter Collingbourne | bcf909d | 2016-06-14 21:02:05 +0000 | [diff] [blame] | 3861 | Str->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3862 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3863 | llvm::Constant *Data[] = {llvm::ConstantExpr::getBitCast(ID, CGM.VoidPtrTy), |
| 3864 | llvm::ConstantExpr::getBitCast(Str, CGM.Int8PtrTy), |
| 3865 | llvm::ConstantInt::get(CGM.SizeTy, Size), |
| 3866 | llvm::ConstantInt::get(CGM.Int32Ty, Flags), |
| 3867 | llvm::ConstantInt::get(CGM.Int32Ty, 0)}; |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 3868 | std::string EntryName = getName({"omp_offloading", "entry", ""}); |
Alexey Bataev | 9a70017 | 2018-05-08 14:16:57 +0000 | [diff] [blame] | 3869 | llvm::GlobalVariable *Entry = createConstantGlobalStruct( |
| 3870 | CGM, getTgtOffloadEntryQTy(), Data, Twine(EntryName).concat(Name), |
| 3871 | llvm::GlobalValue::WeakAnyLinkage); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3872 | |
| 3873 | // 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] | 3874 | std::string Section = getName({"omp_offloading", "entries"}); |
| 3875 | Entry->setSection(Section); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3876 | } |
| 3877 | |
| 3878 | void CGOpenMPRuntime::createOffloadEntriesAndInfoMetadata() { |
| 3879 | // Emit the offloading entries and metadata so that the device codegen side |
Samuel Antao | 4c8035b | 2016-12-12 18:00:20 +0000 | [diff] [blame] | 3880 | // can easily figure out what to emit. The produced metadata looks like |
| 3881 | // this: |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3882 | // |
| 3883 | // !omp_offload.info = !{!1, ...} |
| 3884 | // |
| 3885 | // Right now we only generate metadata for function that contain target |
| 3886 | // regions. |
| 3887 | |
Alexander Kornienko | 2a8c18d | 2018-04-06 15:14:32 +0000 | [diff] [blame] | 3888 | // 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] | 3889 | if (OffloadEntriesInfoManager.empty()) |
| 3890 | return; |
| 3891 | |
| 3892 | llvm::Module &M = CGM.getModule(); |
| 3893 | llvm::LLVMContext &C = M.getContext(); |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 3894 | SmallVector<const OffloadEntriesInfoManagerTy::OffloadEntryInfo *, 16> |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3895 | OrderedEntries(OffloadEntriesInfoManager.size()); |
| 3896 | |
Simon Pilgrim | 2c51880 | 2017-03-30 14:13:19 +0000 | [diff] [blame] | 3897 | // Auxiliary methods to create metadata values and strings. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3898 | auto &&GetMDInt = [this](unsigned V) { |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3899 | return llvm::ConstantAsMetadata::get( |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3900 | llvm::ConstantInt::get(CGM.Int32Ty, V)); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3901 | }; |
| 3902 | |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 3903 | auto &&GetMDString = [&C](StringRef V) { return llvm::MDString::get(C, V); }; |
| 3904 | |
| 3905 | // Create the offloading info metadata node. |
| 3906 | llvm::NamedMDNode *MD = M.getOrInsertNamedMetadata("omp_offload.info"); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3907 | |
| 3908 | // Create function that emits metadata for each target region entry; |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 3909 | auto &&TargetRegionMetadataEmitter = |
| 3910 | [&C, MD, &OrderedEntries, &GetMDInt, &GetMDString]( |
| 3911 | unsigned DeviceID, unsigned FileID, StringRef ParentName, |
| 3912 | unsigned Line, |
| 3913 | const OffloadEntriesInfoManagerTy::OffloadEntryInfoTargetRegion &E) { |
| 3914 | // Generate metadata for target regions. Each entry of this metadata |
| 3915 | // contains: |
| 3916 | // - Entry 0 -> Kind of this type of metadata (0). |
| 3917 | // - Entry 1 -> Device ID of the file where the entry was identified. |
| 3918 | // - Entry 2 -> File ID of the file where the entry was identified. |
| 3919 | // - Entry 3 -> Mangled name of the function where the entry was |
| 3920 | // identified. |
| 3921 | // - Entry 4 -> Line in the file where the entry was identified. |
| 3922 | // - Entry 5 -> Order the entry was created. |
| 3923 | // The first element of the metadata node is the kind. |
| 3924 | llvm::Metadata *Ops[] = {GetMDInt(E.getKind()), GetMDInt(DeviceID), |
| 3925 | GetMDInt(FileID), GetMDString(ParentName), |
| 3926 | GetMDInt(Line), GetMDInt(E.getOrder())}; |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3927 | |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 3928 | // Save this entry in the right position of the ordered entries array. |
| 3929 | OrderedEntries[E.getOrder()] = &E; |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3930 | |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 3931 | // Add metadata to the named metadata node. |
| 3932 | MD->addOperand(llvm::MDNode::get(C, Ops)); |
| 3933 | }; |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3934 | |
| 3935 | OffloadEntriesInfoManager.actOnTargetRegionEntriesInfo( |
| 3936 | TargetRegionMetadataEmitter); |
| 3937 | |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 3938 | // Create function that emits metadata for each device global variable entry; |
| 3939 | auto &&DeviceGlobalVarMetadataEmitter = |
| 3940 | [&C, &OrderedEntries, &GetMDInt, &GetMDString, |
| 3941 | MD](StringRef MangledName, |
| 3942 | const OffloadEntriesInfoManagerTy::OffloadEntryInfoDeviceGlobalVar |
| 3943 | &E) { |
| 3944 | // Generate metadata for global variables. Each entry of this metadata |
| 3945 | // contains: |
| 3946 | // - Entry 0 -> Kind of this type of metadata (1). |
| 3947 | // - Entry 1 -> Mangled name of the variable. |
| 3948 | // - Entry 2 -> Declare target kind. |
| 3949 | // - Entry 3 -> Order the entry was created. |
| 3950 | // The first element of the metadata node is the kind. |
| 3951 | llvm::Metadata *Ops[] = { |
| 3952 | GetMDInt(E.getKind()), GetMDString(MangledName), |
| 3953 | GetMDInt(E.getFlags()), GetMDInt(E.getOrder())}; |
| 3954 | |
| 3955 | // Save this entry in the right position of the ordered entries array. |
| 3956 | OrderedEntries[E.getOrder()] = &E; |
| 3957 | |
| 3958 | // Add metadata to the named metadata node. |
| 3959 | MD->addOperand(llvm::MDNode::get(C, Ops)); |
| 3960 | }; |
| 3961 | |
| 3962 | OffloadEntriesInfoManager.actOnDeviceGlobalVarEntriesInfo( |
| 3963 | DeviceGlobalVarMetadataEmitter); |
| 3964 | |
| 3965 | for (const auto *E : OrderedEntries) { |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3966 | assert(E && "All ordered entries must exist!"); |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 3967 | if (const auto *CE = |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3968 | dyn_cast<OffloadEntriesInfoManagerTy::OffloadEntryInfoTargetRegion>( |
| 3969 | E)) { |
Alexey Bataev | 64e62dc | 2018-04-30 16:26:57 +0000 | [diff] [blame] | 3970 | if (!CE->getID() || !CE->getAddress()) { |
| 3971 | unsigned DiagID = CGM.getDiags().getCustomDiagID( |
| 3972 | DiagnosticsEngine::Error, |
| 3973 | "Offloading entry for target region is incorect: either the " |
| 3974 | "address or the ID is invalid."); |
| 3975 | CGM.getDiags().Report(DiagID); |
| 3976 | continue; |
| 3977 | } |
Alexey Bataev | 34f8a70 | 2018-03-28 14:28:54 +0000 | [diff] [blame] | 3978 | createOffloadEntry(CE->getID(), CE->getAddress(), /*Size=*/0, |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 3979 | CE->getFlags(), llvm::GlobalValue::WeakAnyLinkage); |
| 3980 | } else if (const auto *CE = |
| 3981 | dyn_cast<OffloadEntriesInfoManagerTy:: |
| 3982 | OffloadEntryInfoDeviceGlobalVar>(E)) { |
Alexey Bataev | 64e62dc | 2018-04-30 16:26:57 +0000 | [diff] [blame] | 3983 | if (!CE->getAddress()) { |
| 3984 | unsigned DiagID = CGM.getDiags().getCustomDiagID( |
| 3985 | DiagnosticsEngine::Error, |
| 3986 | "Offloading entry for declare target varible is inccorect: the " |
| 3987 | "address is invalid."); |
| 3988 | CGM.getDiags().Report(DiagID); |
| 3989 | continue; |
| 3990 | } |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 3991 | createOffloadEntry(CE->getAddress(), CE->getAddress(), |
| 3992 | CE->getVarSize().getQuantity(), CE->getFlags(), |
| 3993 | CE->getLinkage()); |
| 3994 | } else { |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3995 | llvm_unreachable("Unsupported entry kind."); |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 3996 | } |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3997 | } |
| 3998 | } |
| 3999 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4000 | /// Loads all the offload entries information from the host IR |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4001 | /// metadata. |
| 4002 | void CGOpenMPRuntime::loadOffloadInfoMetadata() { |
| 4003 | // If we are in target mode, load the metadata from the host IR. This code has |
| 4004 | // to match the metadaata creation in createOffloadEntriesAndInfoMetadata(). |
| 4005 | |
| 4006 | if (!CGM.getLangOpts().OpenMPIsDevice) |
| 4007 | return; |
| 4008 | |
| 4009 | if (CGM.getLangOpts().OMPHostIRFile.empty()) |
| 4010 | return; |
| 4011 | |
| 4012 | auto Buf = llvm::MemoryBuffer::getFile(CGM.getLangOpts().OMPHostIRFile); |
Alexey Bataev | 64e62dc | 2018-04-30 16:26:57 +0000 | [diff] [blame] | 4013 | if (auto EC = Buf.getError()) { |
| 4014 | CGM.getDiags().Report(diag::err_cannot_open_file) |
| 4015 | << CGM.getLangOpts().OMPHostIRFile << EC.message(); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4016 | return; |
Alexey Bataev | 64e62dc | 2018-04-30 16:26:57 +0000 | [diff] [blame] | 4017 | } |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4018 | |
| 4019 | llvm::LLVMContext C; |
Peter Collingbourne | d9445c4 | 2016-11-13 07:00:17 +0000 | [diff] [blame] | 4020 | auto ME = expectedToErrorOrAndEmitErrors( |
| 4021 | C, llvm::parseBitcodeFile(Buf.get()->getMemBufferRef(), C)); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4022 | |
Alexey Bataev | 64e62dc | 2018-04-30 16:26:57 +0000 | [diff] [blame] | 4023 | if (auto EC = ME.getError()) { |
| 4024 | unsigned DiagID = CGM.getDiags().getCustomDiagID( |
| 4025 | DiagnosticsEngine::Error, "Unable to parse host IR file '%0':'%1'"); |
| 4026 | CGM.getDiags().Report(DiagID) |
| 4027 | << CGM.getLangOpts().OMPHostIRFile << EC.message(); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4028 | return; |
Alexey Bataev | 64e62dc | 2018-04-30 16:26:57 +0000 | [diff] [blame] | 4029 | } |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4030 | |
| 4031 | llvm::NamedMDNode *MD = ME.get()->getNamedMetadata("omp_offload.info"); |
| 4032 | if (!MD) |
| 4033 | return; |
| 4034 | |
George Burgess IV | 00f70bd | 2018-03-01 05:43:23 +0000 | [diff] [blame] | 4035 | for (llvm::MDNode *MN : MD->operands()) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4036 | auto &&GetMDInt = [MN](unsigned Idx) { |
| 4037 | auto *V = cast<llvm::ConstantAsMetadata>(MN->getOperand(Idx)); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4038 | return cast<llvm::ConstantInt>(V->getValue())->getZExtValue(); |
| 4039 | }; |
| 4040 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4041 | auto &&GetMDString = [MN](unsigned Idx) { |
| 4042 | auto *V = cast<llvm::MDString>(MN->getOperand(Idx)); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4043 | return V->getString(); |
| 4044 | }; |
| 4045 | |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 4046 | switch (GetMDInt(0)) { |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4047 | default: |
| 4048 | llvm_unreachable("Unexpected metadata!"); |
| 4049 | break; |
| 4050 | case OffloadEntriesInfoManagerTy::OffloadEntryInfo:: |
Alexey Bataev | 34f8a70 | 2018-03-28 14:28:54 +0000 | [diff] [blame] | 4051 | OffloadingEntryInfoTargetRegion: |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4052 | OffloadEntriesInfoManager.initializeTargetRegionEntryInfo( |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 4053 | /*DeviceID=*/GetMDInt(1), /*FileID=*/GetMDInt(2), |
| 4054 | /*ParentName=*/GetMDString(3), /*Line=*/GetMDInt(4), |
| 4055 | /*Order=*/GetMDInt(5)); |
| 4056 | break; |
| 4057 | case OffloadEntriesInfoManagerTy::OffloadEntryInfo:: |
| 4058 | OffloadingEntryInfoDeviceGlobalVar: |
| 4059 | OffloadEntriesInfoManager.initializeDeviceGlobalVarEntryInfo( |
| 4060 | /*MangledName=*/GetMDString(1), |
| 4061 | static_cast<OffloadEntriesInfoManagerTy::OMPTargetGlobalVarEntryKind>( |
| 4062 | /*Flags=*/GetMDInt(2)), |
| 4063 | /*Order=*/GetMDInt(3)); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4064 | break; |
| 4065 | } |
| 4066 | } |
| 4067 | } |
| 4068 | |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 4069 | void CGOpenMPRuntime::emitKmpRoutineEntryT(QualType KmpInt32Ty) { |
| 4070 | if (!KmpRoutineEntryPtrTy) { |
| 4071 | // Build typedef kmp_int32 (* kmp_routine_entry_t)(kmp_int32, void *); type. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4072 | ASTContext &C = CGM.getContext(); |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 4073 | QualType KmpRoutineEntryTyArgs[] = {KmpInt32Ty, C.VoidPtrTy}; |
| 4074 | FunctionProtoType::ExtProtoInfo EPI; |
| 4075 | KmpRoutineEntryPtrQTy = C.getPointerType( |
| 4076 | C.getFunctionType(KmpInt32Ty, KmpRoutineEntryTyArgs, EPI)); |
| 4077 | KmpRoutineEntryPtrTy = CGM.getTypes().ConvertType(KmpRoutineEntryPtrQTy); |
| 4078 | } |
| 4079 | } |
| 4080 | |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4081 | QualType CGOpenMPRuntime::getTgtOffloadEntryQTy() { |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4082 | // Make sure the type of the entry is already created. This is the type we |
| 4083 | // have to create: |
| 4084 | // struct __tgt_offload_entry{ |
| 4085 | // void *addr; // Pointer to the offload entry info. |
| 4086 | // // (function or global) |
| 4087 | // char *name; // Name of the function or global. |
| 4088 | // 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] | 4089 | // int32_t flags; // Flags associated with the entry, e.g. 'link'. |
| 4090 | // int32_t reserved; // Reserved, to use by the runtime library. |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4091 | // }; |
| 4092 | if (TgtOffloadEntryQTy.isNull()) { |
| 4093 | ASTContext &C = CGM.getContext(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4094 | RecordDecl *RD = C.buildImplicitRecord("__tgt_offload_entry"); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4095 | RD->startDefinition(); |
| 4096 | addFieldToRecordDecl(C, RD, C.VoidPtrTy); |
| 4097 | addFieldToRecordDecl(C, RD, C.getPointerType(C.CharTy)); |
| 4098 | addFieldToRecordDecl(C, RD, C.getSizeType()); |
Samuel Antao | f83efdb | 2017-01-05 16:02:49 +0000 | [diff] [blame] | 4099 | addFieldToRecordDecl( |
| 4100 | C, RD, C.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/true)); |
| 4101 | addFieldToRecordDecl( |
| 4102 | C, RD, C.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/true)); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4103 | RD->completeDefinition(); |
Jonas Hahnfeld | 5e4df28 | 2018-01-18 15:38:03 +0000 | [diff] [blame] | 4104 | RD->addAttr(PackedAttr::CreateImplicit(C)); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4105 | TgtOffloadEntryQTy = C.getRecordType(RD); |
| 4106 | } |
| 4107 | return TgtOffloadEntryQTy; |
| 4108 | } |
| 4109 | |
| 4110 | QualType CGOpenMPRuntime::getTgtDeviceImageQTy() { |
| 4111 | // These are the types we need to build: |
| 4112 | // struct __tgt_device_image{ |
| 4113 | // void *ImageStart; // Pointer to the target code start. |
| 4114 | // void *ImageEnd; // Pointer to the target code end. |
| 4115 | // // We also add the host entries to the device image, as it may be useful |
| 4116 | // // for the target runtime to have access to that information. |
| 4117 | // __tgt_offload_entry *EntriesBegin; // Begin of the table with all |
| 4118 | // // the entries. |
| 4119 | // __tgt_offload_entry *EntriesEnd; // End of the table with all the |
| 4120 | // // entries (non inclusive). |
| 4121 | // }; |
| 4122 | if (TgtDeviceImageQTy.isNull()) { |
| 4123 | ASTContext &C = CGM.getContext(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4124 | RecordDecl *RD = C.buildImplicitRecord("__tgt_device_image"); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4125 | RD->startDefinition(); |
| 4126 | addFieldToRecordDecl(C, RD, C.VoidPtrTy); |
| 4127 | addFieldToRecordDecl(C, RD, C.VoidPtrTy); |
| 4128 | addFieldToRecordDecl(C, RD, C.getPointerType(getTgtOffloadEntryQTy())); |
| 4129 | addFieldToRecordDecl(C, RD, C.getPointerType(getTgtOffloadEntryQTy())); |
| 4130 | RD->completeDefinition(); |
| 4131 | TgtDeviceImageQTy = C.getRecordType(RD); |
| 4132 | } |
| 4133 | return TgtDeviceImageQTy; |
| 4134 | } |
| 4135 | |
| 4136 | QualType CGOpenMPRuntime::getTgtBinaryDescriptorQTy() { |
| 4137 | // struct __tgt_bin_desc{ |
| 4138 | // int32_t NumDevices; // Number of devices supported. |
| 4139 | // __tgt_device_image *DeviceImages; // Arrays of device images |
| 4140 | // // (one per device). |
| 4141 | // __tgt_offload_entry *EntriesBegin; // Begin of the table with all the |
| 4142 | // // entries. |
| 4143 | // __tgt_offload_entry *EntriesEnd; // End of the table with all the |
| 4144 | // // entries (non inclusive). |
| 4145 | // }; |
| 4146 | if (TgtBinaryDescriptorQTy.isNull()) { |
| 4147 | ASTContext &C = CGM.getContext(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4148 | RecordDecl *RD = C.buildImplicitRecord("__tgt_bin_desc"); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4149 | RD->startDefinition(); |
| 4150 | addFieldToRecordDecl( |
| 4151 | C, RD, C.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/true)); |
| 4152 | addFieldToRecordDecl(C, RD, C.getPointerType(getTgtDeviceImageQTy())); |
| 4153 | addFieldToRecordDecl(C, RD, C.getPointerType(getTgtOffloadEntryQTy())); |
| 4154 | addFieldToRecordDecl(C, RD, C.getPointerType(getTgtOffloadEntryQTy())); |
| 4155 | RD->completeDefinition(); |
| 4156 | TgtBinaryDescriptorQTy = C.getRecordType(RD); |
| 4157 | } |
| 4158 | return TgtBinaryDescriptorQTy; |
| 4159 | } |
| 4160 | |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 4161 | namespace { |
Alexey Bataev | 9e03404 | 2015-05-05 04:05:12 +0000 | [diff] [blame] | 4162 | struct PrivateHelpersTy { |
| 4163 | PrivateHelpersTy(const VarDecl *Original, const VarDecl *PrivateCopy, |
| 4164 | const VarDecl *PrivateElemInit) |
| 4165 | : Original(Original), PrivateCopy(PrivateCopy), |
| 4166 | PrivateElemInit(PrivateElemInit) {} |
| 4167 | const VarDecl *Original; |
| 4168 | const VarDecl *PrivateCopy; |
| 4169 | const VarDecl *PrivateElemInit; |
| 4170 | }; |
| 4171 | typedef std::pair<CharUnits /*Align*/, PrivateHelpersTy> PrivateDataTy; |
Hans Wennborg | 7eb5464 | 2015-09-10 17:07:54 +0000 | [diff] [blame] | 4172 | } // anonymous namespace |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 4173 | |
Alexey Bataev | 9e03404 | 2015-05-05 04:05:12 +0000 | [diff] [blame] | 4174 | static RecordDecl * |
Craig Topper | 8674c5c | 2015-09-29 04:30:07 +0000 | [diff] [blame] | 4175 | createPrivatesRecordDecl(CodeGenModule &CGM, ArrayRef<PrivateDataTy> Privates) { |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 4176 | if (!Privates.empty()) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4177 | ASTContext &C = CGM.getContext(); |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 4178 | // Build struct .kmp_privates_t. { |
| 4179 | // /* private vars */ |
| 4180 | // }; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4181 | RecordDecl *RD = C.buildImplicitRecord(".kmp_privates.t"); |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 4182 | RD->startDefinition(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4183 | for (const auto &Pair : Privates) { |
| 4184 | const VarDecl *VD = Pair.second.Original; |
| 4185 | QualType Type = VD->getType().getNonReferenceType(); |
| 4186 | FieldDecl *FD = addFieldToRecordDecl(C, RD, Type); |
Alexey Bataev | c71a409 | 2015-09-11 10:29:41 +0000 | [diff] [blame] | 4187 | if (VD->hasAttrs()) { |
| 4188 | for (specific_attr_iterator<AlignedAttr> I(VD->getAttrs().begin()), |
| 4189 | E(VD->getAttrs().end()); |
| 4190 | I != E; ++I) |
| 4191 | FD->addAttr(*I); |
| 4192 | } |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 4193 | } |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 4194 | RD->completeDefinition(); |
| 4195 | return RD; |
| 4196 | } |
| 4197 | return nullptr; |
| 4198 | } |
| 4199 | |
Alexey Bataev | 9e03404 | 2015-05-05 04:05:12 +0000 | [diff] [blame] | 4200 | static RecordDecl * |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4201 | createKmpTaskTRecordDecl(CodeGenModule &CGM, OpenMPDirectiveKind Kind, |
| 4202 | QualType KmpInt32Ty, |
Alexey Bataev | 8fc69dc | 2015-05-18 07:54:53 +0000 | [diff] [blame] | 4203 | QualType KmpRoutineEntryPointerQTy) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4204 | ASTContext &C = CGM.getContext(); |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 4205 | // Build struct kmp_task_t { |
| 4206 | // void * shareds; |
| 4207 | // kmp_routine_entry_t routine; |
| 4208 | // kmp_int32 part_id; |
Alexey Bataev | ad537bb | 2016-05-30 09:06:50 +0000 | [diff] [blame] | 4209 | // kmp_cmplrdata_t data1; |
| 4210 | // kmp_cmplrdata_t data2; |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4211 | // For taskloops additional fields: |
| 4212 | // kmp_uint64 lb; |
| 4213 | // kmp_uint64 ub; |
| 4214 | // kmp_int64 st; |
| 4215 | // kmp_int32 liter; |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 4216 | // void * reductions; |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 4217 | // }; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4218 | RecordDecl *UD = C.buildImplicitRecord("kmp_cmplrdata_t", TTK_Union); |
Alexey Bataev | ad537bb | 2016-05-30 09:06:50 +0000 | [diff] [blame] | 4219 | UD->startDefinition(); |
| 4220 | addFieldToRecordDecl(C, UD, KmpInt32Ty); |
| 4221 | addFieldToRecordDecl(C, UD, KmpRoutineEntryPointerQTy); |
| 4222 | UD->completeDefinition(); |
| 4223 | QualType KmpCmplrdataTy = C.getRecordType(UD); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4224 | RecordDecl *RD = C.buildImplicitRecord("kmp_task_t"); |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 4225 | RD->startDefinition(); |
| 4226 | addFieldToRecordDecl(C, RD, C.VoidPtrTy); |
| 4227 | addFieldToRecordDecl(C, RD, KmpRoutineEntryPointerQTy); |
| 4228 | addFieldToRecordDecl(C, RD, KmpInt32Ty); |
Alexey Bataev | ad537bb | 2016-05-30 09:06:50 +0000 | [diff] [blame] | 4229 | addFieldToRecordDecl(C, RD, KmpCmplrdataTy); |
| 4230 | addFieldToRecordDecl(C, RD, KmpCmplrdataTy); |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4231 | if (isOpenMPTaskLoopDirective(Kind)) { |
| 4232 | QualType KmpUInt64Ty = |
| 4233 | CGM.getContext().getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0); |
| 4234 | QualType KmpInt64Ty = |
| 4235 | CGM.getContext().getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1); |
| 4236 | addFieldToRecordDecl(C, RD, KmpUInt64Ty); |
| 4237 | addFieldToRecordDecl(C, RD, KmpUInt64Ty); |
| 4238 | addFieldToRecordDecl(C, RD, KmpInt64Ty); |
| 4239 | addFieldToRecordDecl(C, RD, KmpInt32Ty); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 4240 | addFieldToRecordDecl(C, RD, C.VoidPtrTy); |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4241 | } |
Alexey Bataev | 8fc69dc | 2015-05-18 07:54:53 +0000 | [diff] [blame] | 4242 | RD->completeDefinition(); |
| 4243 | return RD; |
| 4244 | } |
| 4245 | |
| 4246 | static RecordDecl * |
| 4247 | createKmpTaskTWithPrivatesRecordDecl(CodeGenModule &CGM, QualType KmpTaskTQTy, |
Craig Topper | 8674c5c | 2015-09-29 04:30:07 +0000 | [diff] [blame] | 4248 | ArrayRef<PrivateDataTy> Privates) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4249 | ASTContext &C = CGM.getContext(); |
Alexey Bataev | 8fc69dc | 2015-05-18 07:54:53 +0000 | [diff] [blame] | 4250 | // Build struct kmp_task_t_with_privates { |
| 4251 | // kmp_task_t task_data; |
| 4252 | // .kmp_privates_t. privates; |
| 4253 | // }; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4254 | RecordDecl *RD = C.buildImplicitRecord("kmp_task_t_with_privates"); |
Alexey Bataev | 8fc69dc | 2015-05-18 07:54:53 +0000 | [diff] [blame] | 4255 | RD->startDefinition(); |
| 4256 | addFieldToRecordDecl(C, RD, KmpTaskTQTy); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4257 | if (const RecordDecl *PrivateRD = createPrivatesRecordDecl(CGM, Privates)) |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 4258 | addFieldToRecordDecl(C, RD, C.getRecordType(PrivateRD)); |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 4259 | RD->completeDefinition(); |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 4260 | return RD; |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 4261 | } |
| 4262 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4263 | /// Emit a proxy function which accepts kmp_task_t as the second |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 4264 | /// argument. |
| 4265 | /// \code |
| 4266 | /// kmp_int32 .omp_task_entry.(kmp_int32 gtid, kmp_task_t *tt) { |
Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 4267 | /// TaskFunction(gtid, tt->part_id, &tt->privates, task_privates_map, tt, |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4268 | /// For taskloops: |
| 4269 | /// 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] | 4270 | /// tt->reductions, tt->shareds); |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 4271 | /// return 0; |
| 4272 | /// } |
| 4273 | /// \endcode |
| 4274 | static llvm::Value * |
| 4275 | emitProxyTaskFunction(CodeGenModule &CGM, SourceLocation Loc, |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4276 | OpenMPDirectiveKind Kind, QualType KmpInt32Ty, |
| 4277 | QualType KmpTaskTWithPrivatesPtrQTy, |
Alexey Bataev | 8fc69dc | 2015-05-18 07:54:53 +0000 | [diff] [blame] | 4278 | QualType KmpTaskTWithPrivatesQTy, QualType KmpTaskTQTy, |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4279 | QualType SharedsPtrTy, llvm::Value *TaskFunction, |
| 4280 | llvm::Value *TaskPrivatesMap) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4281 | ASTContext &C = CGM.getContext(); |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 4282 | FunctionArgList Args; |
Alexey Bataev | 5622323 | 2017-06-09 13:40:18 +0000 | [diff] [blame] | 4283 | ImplicitParamDecl GtidArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, KmpInt32Ty, |
| 4284 | ImplicitParamDecl::Other); |
| 4285 | ImplicitParamDecl TaskTypeArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, |
| 4286 | KmpTaskTWithPrivatesPtrQTy.withRestrict(), |
| 4287 | ImplicitParamDecl::Other); |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 4288 | Args.push_back(&GtidArg); |
| 4289 | Args.push_back(&TaskTypeArg); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4290 | const auto &TaskEntryFnInfo = |
John McCall | c56a8b3 | 2016-03-11 04:30:31 +0000 | [diff] [blame] | 4291 | CGM.getTypes().arrangeBuiltinFunctionDeclaration(KmpInt32Ty, Args); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4292 | llvm::FunctionType *TaskEntryTy = |
| 4293 | CGM.getTypes().GetFunctionType(TaskEntryFnInfo); |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 4294 | std::string Name = CGM.getOpenMPRuntime().getName({"omp_task_entry", ""}); |
| 4295 | auto *TaskEntry = llvm::Function::Create( |
| 4296 | TaskEntryTy, llvm::GlobalValue::InternalLinkage, Name, &CGM.getModule()); |
Rafael Espindola | 51ec5a9 | 2018-02-28 23:46:35 +0000 | [diff] [blame] | 4297 | CGM.SetInternalFunctionAttributes(GlobalDecl(), TaskEntry, TaskEntryFnInfo); |
Alexey Bataev | c0f879b | 2018-04-10 20:10:53 +0000 | [diff] [blame] | 4298 | TaskEntry->setDoesNotRecurse(); |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 4299 | CodeGenFunction CGF(CGM); |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 4300 | CGF.StartFunction(GlobalDecl(), KmpInt32Ty, TaskEntry, TaskEntryFnInfo, Args, |
| 4301 | Loc, Loc); |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 4302 | |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4303 | // TaskFunction(gtid, tt->task_data.part_id, &tt->privates, task_privates_map, |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4304 | // tt, |
| 4305 | // For taskloops: |
| 4306 | // tt->task_data.lb, tt->task_data.ub, tt->task_data.st, tt->task_data.liter, |
| 4307 | // tt->task_data.shareds); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4308 | llvm::Value *GtidParam = CGF.EmitLoadOfScalar( |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4309 | CGF.GetAddrOfLocalVar(&GtidArg), /*Volatile=*/false, KmpInt32Ty, Loc); |
Alexey Bataev | 31300ed | 2016-02-04 11:27:03 +0000 | [diff] [blame] | 4310 | LValue TDBase = CGF.EmitLoadOfPointerLValue( |
| 4311 | CGF.GetAddrOfLocalVar(&TaskTypeArg), |
| 4312 | KmpTaskTWithPrivatesPtrQTy->castAs<PointerType>()); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4313 | const auto *KmpTaskTWithPrivatesQTyRD = |
Alexey Bataev | 8fc69dc | 2015-05-18 07:54:53 +0000 | [diff] [blame] | 4314 | cast<RecordDecl>(KmpTaskTWithPrivatesQTy->getAsTagDecl()); |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4315 | LValue Base = |
| 4316 | CGF.EmitLValueForField(TDBase, *KmpTaskTWithPrivatesQTyRD->field_begin()); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4317 | const auto *KmpTaskTQTyRD = cast<RecordDecl>(KmpTaskTQTy->getAsTagDecl()); |
Alexey Bataev | 8fc69dc | 2015-05-18 07:54:53 +0000 | [diff] [blame] | 4318 | auto PartIdFI = std::next(KmpTaskTQTyRD->field_begin(), KmpTaskTPartId); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4319 | LValue PartIdLVal = CGF.EmitLValueForField(Base, *PartIdFI); |
| 4320 | llvm::Value *PartidParam = PartIdLVal.getPointer(); |
Alexey Bataev | 8fc69dc | 2015-05-18 07:54:53 +0000 | [diff] [blame] | 4321 | |
| 4322 | auto SharedsFI = std::next(KmpTaskTQTyRD->field_begin(), KmpTaskTShareds); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4323 | LValue SharedsLVal = CGF.EmitLValueForField(Base, *SharedsFI); |
| 4324 | llvm::Value *SharedsParam = CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
Alexey Bataev | 1e49137 | 2018-01-23 18:44:14 +0000 | [diff] [blame] | 4325 | CGF.EmitLoadOfScalar(SharedsLVal, Loc), |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 4326 | CGF.ConvertTypeForMem(SharedsPtrTy)); |
| 4327 | |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4328 | auto PrivatesFI = std::next(KmpTaskTWithPrivatesQTyRD->field_begin(), 1); |
| 4329 | llvm::Value *PrivatesParam; |
| 4330 | if (PrivatesFI != KmpTaskTWithPrivatesQTyRD->field_end()) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4331 | LValue PrivatesLVal = CGF.EmitLValueForField(TDBase, *PrivatesFI); |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4332 | PrivatesParam = CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4333 | PrivatesLVal.getPointer(), CGF.VoidPtrTy); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4334 | } else { |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4335 | PrivatesParam = llvm::ConstantPointerNull::get(CGF.VoidPtrTy); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4336 | } |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4337 | |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4338 | llvm::Value *CommonArgs[] = {GtidParam, PartidParam, PrivatesParam, |
| 4339 | TaskPrivatesMap, |
| 4340 | CGF.Builder |
| 4341 | .CreatePointerBitCastOrAddrSpaceCast( |
| 4342 | TDBase.getAddress(), CGF.VoidPtrTy) |
| 4343 | .getPointer()}; |
| 4344 | SmallVector<llvm::Value *, 16> CallArgs(std::begin(CommonArgs), |
| 4345 | std::end(CommonArgs)); |
| 4346 | if (isOpenMPTaskLoopDirective(Kind)) { |
| 4347 | auto LBFI = std::next(KmpTaskTQTyRD->field_begin(), KmpTaskTLowerBound); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4348 | LValue LBLVal = CGF.EmitLValueForField(Base, *LBFI); |
| 4349 | llvm::Value *LBParam = CGF.EmitLoadOfScalar(LBLVal, Loc); |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4350 | auto UBFI = std::next(KmpTaskTQTyRD->field_begin(), KmpTaskTUpperBound); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4351 | LValue UBLVal = CGF.EmitLValueForField(Base, *UBFI); |
| 4352 | llvm::Value *UBParam = CGF.EmitLoadOfScalar(UBLVal, Loc); |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4353 | auto StFI = std::next(KmpTaskTQTyRD->field_begin(), KmpTaskTStride); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4354 | LValue StLVal = CGF.EmitLValueForField(Base, *StFI); |
| 4355 | llvm::Value *StParam = CGF.EmitLoadOfScalar(StLVal, Loc); |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4356 | auto LIFI = std::next(KmpTaskTQTyRD->field_begin(), KmpTaskTLastIter); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4357 | LValue LILVal = CGF.EmitLValueForField(Base, *LIFI); |
| 4358 | llvm::Value *LIParam = CGF.EmitLoadOfScalar(LILVal, Loc); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 4359 | auto RFI = std::next(KmpTaskTQTyRD->field_begin(), KmpTaskTReductions); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4360 | LValue RLVal = CGF.EmitLValueForField(Base, *RFI); |
| 4361 | llvm::Value *RParam = CGF.EmitLoadOfScalar(RLVal, Loc); |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4362 | CallArgs.push_back(LBParam); |
| 4363 | CallArgs.push_back(UBParam); |
| 4364 | CallArgs.push_back(StParam); |
| 4365 | CallArgs.push_back(LIParam); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 4366 | CallArgs.push_back(RParam); |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4367 | } |
| 4368 | CallArgs.push_back(SharedsParam); |
| 4369 | |
Alexey Bataev | 3c595a6 | 2017-08-14 15:01:03 +0000 | [diff] [blame] | 4370 | CGM.getOpenMPRuntime().emitOutlinedFunctionCall(CGF, Loc, TaskFunction, |
| 4371 | CallArgs); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4372 | CGF.EmitStoreThroughLValue(RValue::get(CGF.Builder.getInt32(/*C=*/0)), |
| 4373 | CGF.MakeAddrLValue(CGF.ReturnValue, KmpInt32Ty)); |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 4374 | CGF.FinishFunction(); |
| 4375 | return TaskEntry; |
| 4376 | } |
| 4377 | |
Alexey Bataev | 8fc69dc | 2015-05-18 07:54:53 +0000 | [diff] [blame] | 4378 | static llvm::Value *emitDestructorsFunction(CodeGenModule &CGM, |
| 4379 | SourceLocation Loc, |
| 4380 | QualType KmpInt32Ty, |
| 4381 | QualType KmpTaskTWithPrivatesPtrQTy, |
| 4382 | QualType KmpTaskTWithPrivatesQTy) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4383 | ASTContext &C = CGM.getContext(); |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 4384 | FunctionArgList Args; |
Alexey Bataev | 5622323 | 2017-06-09 13:40:18 +0000 | [diff] [blame] | 4385 | ImplicitParamDecl GtidArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, KmpInt32Ty, |
| 4386 | ImplicitParamDecl::Other); |
| 4387 | ImplicitParamDecl TaskTypeArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, |
| 4388 | KmpTaskTWithPrivatesPtrQTy.withRestrict(), |
| 4389 | ImplicitParamDecl::Other); |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 4390 | Args.push_back(&GtidArg); |
| 4391 | Args.push_back(&TaskTypeArg); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4392 | const auto &DestructorFnInfo = |
John McCall | c56a8b3 | 2016-03-11 04:30:31 +0000 | [diff] [blame] | 4393 | CGM.getTypes().arrangeBuiltinFunctionDeclaration(KmpInt32Ty, Args); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4394 | llvm::FunctionType *DestructorFnTy = |
| 4395 | CGM.getTypes().GetFunctionType(DestructorFnInfo); |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 4396 | std::string Name = |
| 4397 | CGM.getOpenMPRuntime().getName({"omp_task_destructor", ""}); |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 4398 | auto *DestructorFn = |
| 4399 | llvm::Function::Create(DestructorFnTy, llvm::GlobalValue::InternalLinkage, |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 4400 | Name, &CGM.getModule()); |
Rafael Espindola | 51ec5a9 | 2018-02-28 23:46:35 +0000 | [diff] [blame] | 4401 | CGM.SetInternalFunctionAttributes(GlobalDecl(), DestructorFn, |
Akira Hatanaka | 44a59f8 | 2015-10-28 02:30:47 +0000 | [diff] [blame] | 4402 | DestructorFnInfo); |
Alexey Bataev | c0f879b | 2018-04-10 20:10:53 +0000 | [diff] [blame] | 4403 | DestructorFn->setDoesNotRecurse(); |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 4404 | CodeGenFunction CGF(CGM); |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 4405 | CGF.StartFunction(GlobalDecl(), KmpInt32Ty, DestructorFn, DestructorFnInfo, |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 4406 | Args, Loc, Loc); |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 4407 | |
Alexey Bataev | 31300ed | 2016-02-04 11:27:03 +0000 | [diff] [blame] | 4408 | LValue Base = CGF.EmitLoadOfPointerLValue( |
| 4409 | CGF.GetAddrOfLocalVar(&TaskTypeArg), |
| 4410 | KmpTaskTWithPrivatesPtrQTy->castAs<PointerType>()); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4411 | const auto *KmpTaskTWithPrivatesQTyRD = |
Alexey Bataev | 8fc69dc | 2015-05-18 07:54:53 +0000 | [diff] [blame] | 4412 | cast<RecordDecl>(KmpTaskTWithPrivatesQTy->getAsTagDecl()); |
| 4413 | auto FI = std::next(KmpTaskTWithPrivatesQTyRD->field_begin()); |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 4414 | Base = CGF.EmitLValueForField(Base, *FI); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4415 | for (const auto *Field : |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 4416 | cast<RecordDecl>(FI->getType()->getAsTagDecl())->fields()) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4417 | if (QualType::DestructionKind DtorKind = |
| 4418 | Field->getType().isDestructedType()) { |
| 4419 | LValue FieldLValue = CGF.EmitLValueForField(Base, Field); |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 4420 | CGF.pushDestroy(DtorKind, FieldLValue.getAddress(), Field->getType()); |
| 4421 | } |
| 4422 | } |
| 4423 | CGF.FinishFunction(); |
| 4424 | return DestructorFn; |
| 4425 | } |
| 4426 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4427 | /// Emit a privates mapping function for correct handling of private and |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4428 | /// firstprivate variables. |
| 4429 | /// \code |
| 4430 | /// void .omp_task_privates_map.(const .privates. *noalias privs, <ty1> |
| 4431 | /// **noalias priv1,..., <tyn> **noalias privn) { |
| 4432 | /// *priv1 = &.privates.priv1; |
| 4433 | /// ...; |
| 4434 | /// *privn = &.privates.privn; |
| 4435 | /// } |
| 4436 | /// \endcode |
| 4437 | static llvm::Value * |
| 4438 | emitTaskPrivateMappingFunction(CodeGenModule &CGM, SourceLocation Loc, |
Craig Topper | 8674c5c | 2015-09-29 04:30:07 +0000 | [diff] [blame] | 4439 | ArrayRef<const Expr *> PrivateVars, |
| 4440 | ArrayRef<const Expr *> FirstprivateVars, |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4441 | ArrayRef<const Expr *> LastprivateVars, |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4442 | QualType PrivatesQTy, |
Craig Topper | 8674c5c | 2015-09-29 04:30:07 +0000 | [diff] [blame] | 4443 | ArrayRef<PrivateDataTy> Privates) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4444 | ASTContext &C = CGM.getContext(); |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4445 | FunctionArgList Args; |
| 4446 | ImplicitParamDecl TaskPrivatesArg( |
| 4447 | C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, |
Alexey Bataev | 5622323 | 2017-06-09 13:40:18 +0000 | [diff] [blame] | 4448 | C.getPointerType(PrivatesQTy).withConst().withRestrict(), |
| 4449 | ImplicitParamDecl::Other); |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4450 | Args.push_back(&TaskPrivatesArg); |
| 4451 | llvm::DenseMap<const VarDecl *, unsigned> PrivateVarsPos; |
| 4452 | unsigned Counter = 1; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4453 | for (const Expr *E : PrivateVars) { |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4454 | Args.push_back(ImplicitParamDecl::Create( |
Alexey Bataev | 5622323 | 2017-06-09 13:40:18 +0000 | [diff] [blame] | 4455 | C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, |
| 4456 | C.getPointerType(C.getPointerType(E->getType())) |
| 4457 | .withConst() |
| 4458 | .withRestrict(), |
| 4459 | ImplicitParamDecl::Other)); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4460 | const auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl()); |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4461 | PrivateVarsPos[VD] = Counter; |
| 4462 | ++Counter; |
| 4463 | } |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4464 | for (const Expr *E : FirstprivateVars) { |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4465 | Args.push_back(ImplicitParamDecl::Create( |
Alexey Bataev | 5622323 | 2017-06-09 13:40:18 +0000 | [diff] [blame] | 4466 | C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, |
| 4467 | C.getPointerType(C.getPointerType(E->getType())) |
| 4468 | .withConst() |
| 4469 | .withRestrict(), |
| 4470 | ImplicitParamDecl::Other)); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4471 | const auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl()); |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4472 | PrivateVarsPos[VD] = Counter; |
| 4473 | ++Counter; |
| 4474 | } |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4475 | for (const Expr *E : LastprivateVars) { |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4476 | Args.push_back(ImplicitParamDecl::Create( |
Alexey Bataev | 5622323 | 2017-06-09 13:40:18 +0000 | [diff] [blame] | 4477 | C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, |
| 4478 | C.getPointerType(C.getPointerType(E->getType())) |
| 4479 | .withConst() |
| 4480 | .withRestrict(), |
| 4481 | ImplicitParamDecl::Other)); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4482 | const auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl()); |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4483 | PrivateVarsPos[VD] = Counter; |
| 4484 | ++Counter; |
| 4485 | } |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4486 | const auto &TaskPrivatesMapFnInfo = |
John McCall | c56a8b3 | 2016-03-11 04:30:31 +0000 | [diff] [blame] | 4487 | CGM.getTypes().arrangeBuiltinFunctionDeclaration(C.VoidTy, Args); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4488 | llvm::FunctionType *TaskPrivatesMapTy = |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4489 | CGM.getTypes().GetFunctionType(TaskPrivatesMapFnInfo); |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 4490 | std::string Name = |
| 4491 | CGM.getOpenMPRuntime().getName({"omp_task_privates_map", ""}); |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4492 | auto *TaskPrivatesMap = llvm::Function::Create( |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 4493 | TaskPrivatesMapTy, llvm::GlobalValue::InternalLinkage, Name, |
| 4494 | &CGM.getModule()); |
Rafael Espindola | 51ec5a9 | 2018-02-28 23:46:35 +0000 | [diff] [blame] | 4495 | CGM.SetInternalFunctionAttributes(GlobalDecl(), TaskPrivatesMap, |
Akira Hatanaka | 44a59f8 | 2015-10-28 02:30:47 +0000 | [diff] [blame] | 4496 | TaskPrivatesMapFnInfo); |
Chandler Carruth | fcd3314 | 2016-12-23 01:24:49 +0000 | [diff] [blame] | 4497 | TaskPrivatesMap->removeFnAttr(llvm::Attribute::NoInline); |
Mehdi Amini | 6aa9e9b | 2017-05-29 05:38:20 +0000 | [diff] [blame] | 4498 | TaskPrivatesMap->removeFnAttr(llvm::Attribute::OptimizeNone); |
Evgeniy Stepanov | 6b2a61d | 2015-09-14 21:35:16 +0000 | [diff] [blame] | 4499 | TaskPrivatesMap->addFnAttr(llvm::Attribute::AlwaysInline); |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4500 | CodeGenFunction CGF(CGM); |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4501 | CGF.StartFunction(GlobalDecl(), C.VoidTy, TaskPrivatesMap, |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 4502 | TaskPrivatesMapFnInfo, Args, Loc, Loc); |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4503 | |
| 4504 | // *privi = &.privates.privi; |
Alexey Bataev | 31300ed | 2016-02-04 11:27:03 +0000 | [diff] [blame] | 4505 | LValue Base = CGF.EmitLoadOfPointerLValue( |
| 4506 | CGF.GetAddrOfLocalVar(&TaskPrivatesArg), |
| 4507 | TaskPrivatesArg.getType()->castAs<PointerType>()); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4508 | const auto *PrivatesQTyRD = cast<RecordDecl>(PrivatesQTy->getAsTagDecl()); |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4509 | Counter = 0; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4510 | for (const FieldDecl *Field : PrivatesQTyRD->fields()) { |
| 4511 | LValue FieldLVal = CGF.EmitLValueForField(Base, Field); |
| 4512 | const VarDecl *VD = Args[PrivateVarsPos[Privates[Counter].second.Original]]; |
| 4513 | LValue RefLVal = |
| 4514 | CGF.MakeAddrLValue(CGF.GetAddrOfLocalVar(VD), VD->getType()); |
| 4515 | LValue RefLoadLVal = CGF.EmitLoadOfPointerLValue( |
Alexey Bataev | 31300ed | 2016-02-04 11:27:03 +0000 | [diff] [blame] | 4516 | RefLVal.getAddress(), RefLVal.getType()->castAs<PointerType>()); |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 4517 | CGF.EmitStoreOfScalar(FieldLVal.getPointer(), RefLoadLVal); |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4518 | ++Counter; |
| 4519 | } |
| 4520 | CGF.FinishFunction(); |
| 4521 | return TaskPrivatesMap; |
| 4522 | } |
| 4523 | |
Mandeep Singh Grang | b14fb6a2 | 2017-11-28 20:41:13 +0000 | [diff] [blame] | 4524 | static bool stable_sort_comparator(const PrivateDataTy P1, |
| 4525 | const PrivateDataTy P2) { |
| 4526 | return P1.first > P2.first; |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 4527 | } |
| 4528 | |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4529 | /// Emit initialization for private variables in task-based directives. |
Alexey Bataev | 8a83159 | 2016-05-10 10:36:51 +0000 | [diff] [blame] | 4530 | static void emitPrivatesInit(CodeGenFunction &CGF, |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4531 | const OMPExecutableDirective &D, |
| 4532 | Address KmpTaskSharedsPtr, LValue TDBase, |
| 4533 | const RecordDecl *KmpTaskTWithPrivatesQTyRD, |
| 4534 | QualType SharedsTy, QualType SharedsPtrTy, |
| 4535 | const OMPTaskDataTy &Data, |
| 4536 | ArrayRef<PrivateDataTy> Privates, bool ForDup) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4537 | ASTContext &C = CGF.getContext(); |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4538 | auto FI = std::next(KmpTaskTWithPrivatesQTyRD->field_begin()); |
| 4539 | LValue PrivatesBase = CGF.EmitLValueForField(TDBase, *FI); |
Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 4540 | OpenMPDirectiveKind Kind = isOpenMPTaskLoopDirective(D.getDirectiveKind()) |
| 4541 | ? OMPD_taskloop |
| 4542 | : OMPD_task; |
| 4543 | const CapturedStmt &CS = *D.getCapturedStmt(Kind); |
| 4544 | CodeGenFunction::CGCapturedStmtInfo CapturesInfo(CS); |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4545 | LValue SrcBase; |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 4546 | bool IsTargetTask = |
| 4547 | isOpenMPTargetDataManagementDirective(D.getDirectiveKind()) || |
| 4548 | isOpenMPTargetExecutionDirective(D.getDirectiveKind()); |
| 4549 | // For target-based directives skip 3 firstprivate arrays BasePointersArray, |
| 4550 | // PointersArray and SizesArray. The original variables for these arrays are |
| 4551 | // not captured and we get their addresses explicitly. |
| 4552 | if ((!IsTargetTask && !Data.FirstprivateVars.empty()) || |
Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 4553 | (IsTargetTask && KmpTaskSharedsPtr.isValid())) { |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4554 | SrcBase = CGF.MakeAddrLValue( |
| 4555 | CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
| 4556 | KmpTaskSharedsPtr, CGF.ConvertTypeForMem(SharedsPtrTy)), |
| 4557 | SharedsTy); |
| 4558 | } |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4559 | FI = cast<RecordDecl>(FI->getType()->getAsTagDecl())->field_begin(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4560 | for (const PrivateDataTy &Pair : Privates) { |
| 4561 | const VarDecl *VD = Pair.second.PrivateCopy; |
| 4562 | const Expr *Init = VD->getAnyInitializer(); |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4563 | if (Init && (!ForDup || (isa<CXXConstructExpr>(Init) && |
| 4564 | !CGF.isTrivialInitializer(Init)))) { |
Alexey Bataev | 8a83159 | 2016-05-10 10:36:51 +0000 | [diff] [blame] | 4565 | LValue PrivateLValue = CGF.EmitLValueForField(PrivatesBase, *FI); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4566 | if (const VarDecl *Elem = Pair.second.PrivateElemInit) { |
| 4567 | const VarDecl *OriginalVD = Pair.second.Original; |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 4568 | // Check if the variable is the target-based BasePointersArray, |
| 4569 | // PointersArray or SizesArray. |
| 4570 | LValue SharedRefLValue; |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4571 | QualType Type = OriginalVD->getType(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4572 | const FieldDecl *SharedField = CapturesInfo.lookup(OriginalVD); |
Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 4573 | if (IsTargetTask && !SharedField) { |
| 4574 | assert(isa<ImplicitParamDecl>(OriginalVD) && |
| 4575 | isa<CapturedDecl>(OriginalVD->getDeclContext()) && |
| 4576 | cast<CapturedDecl>(OriginalVD->getDeclContext()) |
| 4577 | ->getNumParams() == 0 && |
| 4578 | isa<TranslationUnitDecl>( |
| 4579 | cast<CapturedDecl>(OriginalVD->getDeclContext()) |
| 4580 | ->getDeclContext()) && |
| 4581 | "Expected artificial target data variable."); |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 4582 | SharedRefLValue = |
| 4583 | CGF.MakeAddrLValue(CGF.GetAddrOfLocalVar(OriginalVD), Type); |
| 4584 | } else { |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 4585 | SharedRefLValue = CGF.EmitLValueForField(SrcBase, SharedField); |
| 4586 | SharedRefLValue = CGF.MakeAddrLValue( |
| 4587 | Address(SharedRefLValue.getPointer(), C.getDeclAlign(OriginalVD)), |
| 4588 | SharedRefLValue.getType(), LValueBaseInfo(AlignmentSource::Decl), |
| 4589 | SharedRefLValue.getTBAAInfo()); |
| 4590 | } |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4591 | if (Type->isArrayType()) { |
| 4592 | // Initialize firstprivate array. |
| 4593 | if (!isa<CXXConstructExpr>(Init) || CGF.isTrivialInitializer(Init)) { |
| 4594 | // Perform simple memcpy. |
Ivan A. Kosarev | 1860b52 | 2018-01-25 14:21:55 +0000 | [diff] [blame] | 4595 | CGF.EmitAggregateAssign(PrivateLValue, SharedRefLValue, Type); |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4596 | } else { |
| 4597 | // Initialize firstprivate array using element-by-element |
Simon Pilgrim | 2c51880 | 2017-03-30 14:13:19 +0000 | [diff] [blame] | 4598 | // initialization. |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4599 | CGF.EmitOMPAggregateAssign( |
| 4600 | PrivateLValue.getAddress(), SharedRefLValue.getAddress(), Type, |
| 4601 | [&CGF, Elem, Init, &CapturesInfo](Address DestElement, |
| 4602 | Address SrcElement) { |
| 4603 | // Clean up any temporaries needed by the initialization. |
| 4604 | CodeGenFunction::OMPPrivateScope InitScope(CGF); |
| 4605 | InitScope.addPrivate( |
| 4606 | Elem, [SrcElement]() -> Address { return SrcElement; }); |
| 4607 | (void)InitScope.Privatize(); |
| 4608 | // Emit initialization for single element. |
| 4609 | CodeGenFunction::CGCapturedStmtRAII CapInfoRAII( |
| 4610 | CGF, &CapturesInfo); |
| 4611 | CGF.EmitAnyExprToMem(Init, DestElement, |
| 4612 | Init->getType().getQualifiers(), |
| 4613 | /*IsInitializer=*/false); |
| 4614 | }); |
| 4615 | } |
| 4616 | } else { |
| 4617 | CodeGenFunction::OMPPrivateScope InitScope(CGF); |
| 4618 | InitScope.addPrivate(Elem, [SharedRefLValue]() -> Address { |
| 4619 | return SharedRefLValue.getAddress(); |
| 4620 | }); |
| 4621 | (void)InitScope.Privatize(); |
| 4622 | CodeGenFunction::CGCapturedStmtRAII CapInfoRAII(CGF, &CapturesInfo); |
| 4623 | CGF.EmitExprAsInit(Init, VD, PrivateLValue, |
| 4624 | /*capturedByInit=*/false); |
| 4625 | } |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4626 | } else { |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4627 | CGF.EmitExprAsInit(Init, VD, PrivateLValue, /*capturedByInit=*/false); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4628 | } |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4629 | } |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4630 | ++FI; |
| 4631 | } |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4632 | } |
| 4633 | |
| 4634 | /// Check if duplication function is required for taskloops. |
| 4635 | static bool checkInitIsRequired(CodeGenFunction &CGF, |
| 4636 | ArrayRef<PrivateDataTy> Privates) { |
| 4637 | bool InitRequired = false; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4638 | for (const PrivateDataTy &Pair : Privates) { |
| 4639 | const VarDecl *VD = Pair.second.PrivateCopy; |
| 4640 | const Expr *Init = VD->getAnyInitializer(); |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4641 | InitRequired = InitRequired || (Init && isa<CXXConstructExpr>(Init) && |
| 4642 | !CGF.isTrivialInitializer(Init)); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4643 | if (InitRequired) |
| 4644 | break; |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4645 | } |
| 4646 | return InitRequired; |
| 4647 | } |
| 4648 | |
| 4649 | |
| 4650 | /// Emit task_dup function (for initialization of |
| 4651 | /// private/firstprivate/lastprivate vars and last_iter flag) |
| 4652 | /// \code |
| 4653 | /// void __task_dup_entry(kmp_task_t *task_dst, const kmp_task_t *task_src, int |
| 4654 | /// lastpriv) { |
| 4655 | /// // setup lastprivate flag |
| 4656 | /// task_dst->last = lastpriv; |
| 4657 | /// // could be constructor calls here... |
| 4658 | /// } |
| 4659 | /// \endcode |
| 4660 | static llvm::Value * |
| 4661 | emitTaskDupFunction(CodeGenModule &CGM, SourceLocation Loc, |
| 4662 | const OMPExecutableDirective &D, |
| 4663 | QualType KmpTaskTWithPrivatesPtrQTy, |
| 4664 | const RecordDecl *KmpTaskTWithPrivatesQTyRD, |
| 4665 | const RecordDecl *KmpTaskTQTyRD, QualType SharedsTy, |
| 4666 | QualType SharedsPtrTy, const OMPTaskDataTy &Data, |
| 4667 | ArrayRef<PrivateDataTy> Privates, bool WithLastIter) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4668 | ASTContext &C = CGM.getContext(); |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4669 | FunctionArgList Args; |
Alexey Bataev | 5622323 | 2017-06-09 13:40:18 +0000 | [diff] [blame] | 4670 | ImplicitParamDecl DstArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, |
| 4671 | KmpTaskTWithPrivatesPtrQTy, |
| 4672 | ImplicitParamDecl::Other); |
| 4673 | ImplicitParamDecl SrcArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, |
| 4674 | KmpTaskTWithPrivatesPtrQTy, |
| 4675 | ImplicitParamDecl::Other); |
| 4676 | ImplicitParamDecl LastprivArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, C.IntTy, |
| 4677 | ImplicitParamDecl::Other); |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4678 | Args.push_back(&DstArg); |
| 4679 | Args.push_back(&SrcArg); |
| 4680 | Args.push_back(&LastprivArg); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4681 | const auto &TaskDupFnInfo = |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4682 | CGM.getTypes().arrangeBuiltinFunctionDeclaration(C.VoidTy, Args); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4683 | llvm::FunctionType *TaskDupTy = CGM.getTypes().GetFunctionType(TaskDupFnInfo); |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 4684 | std::string Name = CGM.getOpenMPRuntime().getName({"omp_task_dup", ""}); |
| 4685 | auto *TaskDup = llvm::Function::Create( |
| 4686 | TaskDupTy, llvm::GlobalValue::InternalLinkage, Name, &CGM.getModule()); |
Rafael Espindola | 51ec5a9 | 2018-02-28 23:46:35 +0000 | [diff] [blame] | 4687 | CGM.SetInternalFunctionAttributes(GlobalDecl(), TaskDup, TaskDupFnInfo); |
Alexey Bataev | c0f879b | 2018-04-10 20:10:53 +0000 | [diff] [blame] | 4688 | TaskDup->setDoesNotRecurse(); |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4689 | CodeGenFunction CGF(CGM); |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 4690 | CGF.StartFunction(GlobalDecl(), C.VoidTy, TaskDup, TaskDupFnInfo, Args, Loc, |
| 4691 | Loc); |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4692 | |
| 4693 | LValue TDBase = CGF.EmitLoadOfPointerLValue( |
| 4694 | CGF.GetAddrOfLocalVar(&DstArg), |
| 4695 | KmpTaskTWithPrivatesPtrQTy->castAs<PointerType>()); |
| 4696 | // task_dst->liter = lastpriv; |
| 4697 | if (WithLastIter) { |
| 4698 | auto LIFI = std::next(KmpTaskTQTyRD->field_begin(), KmpTaskTLastIter); |
| 4699 | LValue Base = CGF.EmitLValueForField( |
| 4700 | TDBase, *KmpTaskTWithPrivatesQTyRD->field_begin()); |
| 4701 | LValue LILVal = CGF.EmitLValueForField(Base, *LIFI); |
| 4702 | llvm::Value *Lastpriv = CGF.EmitLoadOfScalar( |
| 4703 | CGF.GetAddrOfLocalVar(&LastprivArg), /*Volatile=*/false, C.IntTy, Loc); |
| 4704 | CGF.EmitStoreOfScalar(Lastpriv, LILVal); |
| 4705 | } |
| 4706 | |
| 4707 | // Emit initial values for private copies (if any). |
| 4708 | assert(!Privates.empty()); |
| 4709 | Address KmpTaskSharedsPtr = Address::invalid(); |
| 4710 | if (!Data.FirstprivateVars.empty()) { |
| 4711 | LValue TDBase = CGF.EmitLoadOfPointerLValue( |
| 4712 | CGF.GetAddrOfLocalVar(&SrcArg), |
| 4713 | KmpTaskTWithPrivatesPtrQTy->castAs<PointerType>()); |
| 4714 | LValue Base = CGF.EmitLValueForField( |
| 4715 | TDBase, *KmpTaskTWithPrivatesQTyRD->field_begin()); |
| 4716 | KmpTaskSharedsPtr = Address( |
| 4717 | CGF.EmitLoadOfScalar(CGF.EmitLValueForField( |
| 4718 | Base, *std::next(KmpTaskTQTyRD->field_begin(), |
| 4719 | KmpTaskTShareds)), |
| 4720 | Loc), |
| 4721 | CGF.getNaturalTypeAlignment(SharedsTy)); |
| 4722 | } |
Alexey Bataev | 8a83159 | 2016-05-10 10:36:51 +0000 | [diff] [blame] | 4723 | emitPrivatesInit(CGF, D, KmpTaskSharedsPtr, TDBase, KmpTaskTWithPrivatesQTyRD, |
| 4724 | SharedsTy, SharedsPtrTy, Data, Privates, /*ForDup=*/true); |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4725 | CGF.FinishFunction(); |
| 4726 | return TaskDup; |
| 4727 | } |
| 4728 | |
Alexey Bataev | 8a83159 | 2016-05-10 10:36:51 +0000 | [diff] [blame] | 4729 | /// Checks if destructor function is required to be generated. |
| 4730 | /// \return true if cleanups are required, false otherwise. |
| 4731 | static bool |
| 4732 | checkDestructorsRequired(const RecordDecl *KmpTaskTWithPrivatesQTyRD) { |
| 4733 | bool NeedsCleanup = false; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4734 | auto FI = std::next(KmpTaskTWithPrivatesQTyRD->field_begin(), 1); |
| 4735 | const auto *PrivateRD = cast<RecordDecl>(FI->getType()->getAsTagDecl()); |
| 4736 | for (const FieldDecl *FD : PrivateRD->fields()) { |
Alexey Bataev | 8a83159 | 2016-05-10 10:36:51 +0000 | [diff] [blame] | 4737 | NeedsCleanup = NeedsCleanup || FD->getType().isDestructedType(); |
| 4738 | if (NeedsCleanup) |
| 4739 | break; |
| 4740 | } |
| 4741 | return NeedsCleanup; |
| 4742 | } |
| 4743 | |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 4744 | CGOpenMPRuntime::TaskResultTy |
| 4745 | CGOpenMPRuntime::emitTaskInit(CodeGenFunction &CGF, SourceLocation Loc, |
| 4746 | const OMPExecutableDirective &D, |
| 4747 | llvm::Value *TaskFunction, QualType SharedsTy, |
| 4748 | Address Shareds, const OMPTaskDataTy &Data) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4749 | ASTContext &C = CGM.getContext(); |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4750 | llvm::SmallVector<PrivateDataTy, 4> Privates; |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4751 | // Aggregate privates and sort them by the alignment. |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 4752 | auto I = Data.PrivateCopies.begin(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4753 | for (const Expr *E : Data.PrivateVars) { |
| 4754 | const auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl()); |
Alexey Bataev | 43a919f | 2018-04-13 17:48:43 +0000 | [diff] [blame] | 4755 | Privates.emplace_back( |
Alexey Bataev | c71a409 | 2015-09-11 10:29:41 +0000 | [diff] [blame] | 4756 | C.getDeclAlign(VD), |
Alexey Bataev | 9e03404 | 2015-05-05 04:05:12 +0000 | [diff] [blame] | 4757 | PrivateHelpersTy(VD, cast<VarDecl>(cast<DeclRefExpr>(*I)->getDecl()), |
Alexey Bataev | 43a919f | 2018-04-13 17:48:43 +0000 | [diff] [blame] | 4758 | /*PrivateElemInit=*/nullptr)); |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 4759 | ++I; |
| 4760 | } |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 4761 | I = Data.FirstprivateCopies.begin(); |
| 4762 | auto IElemInitRef = Data.FirstprivateInits.begin(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4763 | for (const Expr *E : Data.FirstprivateVars) { |
| 4764 | const auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl()); |
Alexey Bataev | 43a919f | 2018-04-13 17:48:43 +0000 | [diff] [blame] | 4765 | Privates.emplace_back( |
Alexey Bataev | c71a409 | 2015-09-11 10:29:41 +0000 | [diff] [blame] | 4766 | C.getDeclAlign(VD), |
Alexey Bataev | 9e03404 | 2015-05-05 04:05:12 +0000 | [diff] [blame] | 4767 | PrivateHelpersTy( |
| 4768 | VD, cast<VarDecl>(cast<DeclRefExpr>(*I)->getDecl()), |
Alexey Bataev | 43a919f | 2018-04-13 17:48:43 +0000 | [diff] [blame] | 4769 | cast<VarDecl>(cast<DeclRefExpr>(*IElemInitRef)->getDecl()))); |
Richard Trieu | cc3949d | 2016-02-18 22:34:54 +0000 | [diff] [blame] | 4770 | ++I; |
| 4771 | ++IElemInitRef; |
Alexey Bataev | 9e03404 | 2015-05-05 04:05:12 +0000 | [diff] [blame] | 4772 | } |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4773 | I = Data.LastprivateCopies.begin(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4774 | for (const Expr *E : Data.LastprivateVars) { |
| 4775 | const auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl()); |
Alexey Bataev | 43a919f | 2018-04-13 17:48:43 +0000 | [diff] [blame] | 4776 | Privates.emplace_back( |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4777 | C.getDeclAlign(VD), |
| 4778 | PrivateHelpersTy(VD, cast<VarDecl>(cast<DeclRefExpr>(*I)->getDecl()), |
Alexey Bataev | 43a919f | 2018-04-13 17:48:43 +0000 | [diff] [blame] | 4779 | /*PrivateElemInit=*/nullptr)); |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4780 | ++I; |
| 4781 | } |
Mandeep Singh Grang | b14fb6a2 | 2017-11-28 20:41:13 +0000 | [diff] [blame] | 4782 | std::stable_sort(Privates.begin(), Privates.end(), stable_sort_comparator); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4783 | QualType KmpInt32Ty = C.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1); |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 4784 | // Build type kmp_routine_entry_t (if not built yet). |
| 4785 | emitKmpRoutineEntryT(KmpInt32Ty); |
Alexey Bataev | 8fc69dc | 2015-05-18 07:54:53 +0000 | [diff] [blame] | 4786 | // Build type kmp_task_t (if not built yet). |
Alexey Bataev | e213f3e | 2017-10-11 15:29:40 +0000 | [diff] [blame] | 4787 | if (isOpenMPTaskLoopDirective(D.getDirectiveKind())) { |
| 4788 | if (SavedKmpTaskloopTQTy.isNull()) { |
| 4789 | SavedKmpTaskloopTQTy = C.getRecordType(createKmpTaskTRecordDecl( |
| 4790 | CGM, D.getDirectiveKind(), KmpInt32Ty, KmpRoutineEntryPtrQTy)); |
| 4791 | } |
| 4792 | KmpTaskTQTy = SavedKmpTaskloopTQTy; |
Alexey Bataev | 3a03a7f | 2017-10-11 15:56:38 +0000 | [diff] [blame] | 4793 | } else { |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 4794 | assert((D.getDirectiveKind() == OMPD_task || |
| 4795 | isOpenMPTargetExecutionDirective(D.getDirectiveKind()) || |
| 4796 | isOpenMPTargetDataManagementDirective(D.getDirectiveKind())) && |
| 4797 | "Expected taskloop, task or target directive"); |
Alexey Bataev | e213f3e | 2017-10-11 15:29:40 +0000 | [diff] [blame] | 4798 | if (SavedKmpTaskTQTy.isNull()) { |
| 4799 | SavedKmpTaskTQTy = C.getRecordType(createKmpTaskTRecordDecl( |
| 4800 | CGM, D.getDirectiveKind(), KmpInt32Ty, KmpRoutineEntryPtrQTy)); |
| 4801 | } |
| 4802 | KmpTaskTQTy = SavedKmpTaskTQTy; |
Alexey Bataev | 8fc69dc | 2015-05-18 07:54:53 +0000 | [diff] [blame] | 4803 | } |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4804 | const auto *KmpTaskTQTyRD = cast<RecordDecl>(KmpTaskTQTy->getAsTagDecl()); |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 4805 | // Build particular struct kmp_task_t for the given task. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4806 | const RecordDecl *KmpTaskTWithPrivatesQTyRD = |
Alexey Bataev | 8fc69dc | 2015-05-18 07:54:53 +0000 | [diff] [blame] | 4807 | createKmpTaskTWithPrivatesRecordDecl(CGM, KmpTaskTQTy, Privates); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4808 | QualType KmpTaskTWithPrivatesQTy = C.getRecordType(KmpTaskTWithPrivatesQTyRD); |
Alexey Bataev | 8fc69dc | 2015-05-18 07:54:53 +0000 | [diff] [blame] | 4809 | QualType KmpTaskTWithPrivatesPtrQTy = |
| 4810 | C.getPointerType(KmpTaskTWithPrivatesQTy); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4811 | llvm::Type *KmpTaskTWithPrivatesTy = CGF.ConvertType(KmpTaskTWithPrivatesQTy); |
| 4812 | llvm::Type *KmpTaskTWithPrivatesPtrTy = |
| 4813 | KmpTaskTWithPrivatesTy->getPointerTo(); |
| 4814 | llvm::Value *KmpTaskTWithPrivatesTySize = |
| 4815 | CGF.getTypeSize(KmpTaskTWithPrivatesQTy); |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 4816 | QualType SharedsPtrTy = C.getPointerType(SharedsTy); |
| 4817 | |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4818 | // Emit initial values for private copies (if any). |
| 4819 | llvm::Value *TaskPrivatesMap = nullptr; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4820 | llvm::Type *TaskPrivatesMapTy = |
Reid Kleckner | e258c44 | 2017-03-16 18:55:46 +0000 | [diff] [blame] | 4821 | std::next(cast<llvm::Function>(TaskFunction)->arg_begin(), 3)->getType(); |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4822 | if (!Privates.empty()) { |
| 4823 | auto FI = std::next(KmpTaskTWithPrivatesQTyRD->field_begin()); |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4824 | TaskPrivatesMap = emitTaskPrivateMappingFunction( |
| 4825 | CGM, Loc, Data.PrivateVars, Data.FirstprivateVars, Data.LastprivateVars, |
| 4826 | FI->getType(), Privates); |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4827 | TaskPrivatesMap = CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
| 4828 | TaskPrivatesMap, TaskPrivatesMapTy); |
| 4829 | } else { |
| 4830 | TaskPrivatesMap = llvm::ConstantPointerNull::get( |
| 4831 | cast<llvm::PointerType>(TaskPrivatesMapTy)); |
| 4832 | } |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 4833 | // Build a proxy function kmp_int32 .omp_task_entry.(kmp_int32 gtid, |
| 4834 | // kmp_task_t *tt); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4835 | llvm::Value *TaskEntry = emitProxyTaskFunction( |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4836 | CGM, Loc, D.getDirectiveKind(), KmpInt32Ty, KmpTaskTWithPrivatesPtrQTy, |
| 4837 | KmpTaskTWithPrivatesQTy, KmpTaskTQTy, SharedsPtrTy, TaskFunction, |
| 4838 | TaskPrivatesMap); |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 4839 | |
| 4840 | // Build call kmp_task_t * __kmpc_omp_task_alloc(ident_t *, kmp_int32 gtid, |
| 4841 | // kmp_int32 flags, size_t sizeof_kmp_task_t, size_t sizeof_shareds, |
| 4842 | // kmp_routine_entry_t *task_entry); |
| 4843 | // Task flags. Format is taken from |
| 4844 | // http://llvm.org/svn/llvm-project/openmp/trunk/runtime/src/kmp.h, |
| 4845 | // description of kmp_tasking_flags struct. |
Alexey Bataev | 1e1e286 | 2016-05-10 12:21:02 +0000 | [diff] [blame] | 4846 | enum { |
| 4847 | TiedFlag = 0x1, |
| 4848 | FinalFlag = 0x2, |
| 4849 | DestructorsFlag = 0x8, |
| 4850 | PriorityFlag = 0x20 |
| 4851 | }; |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 4852 | unsigned Flags = Data.Tied ? TiedFlag : 0; |
Alexey Bataev | 8a83159 | 2016-05-10 10:36:51 +0000 | [diff] [blame] | 4853 | bool NeedsCleanup = false; |
| 4854 | if (!Privates.empty()) { |
| 4855 | NeedsCleanup = checkDestructorsRequired(KmpTaskTWithPrivatesQTyRD); |
| 4856 | if (NeedsCleanup) |
| 4857 | Flags = Flags | DestructorsFlag; |
| 4858 | } |
Alexey Bataev | 1e1e286 | 2016-05-10 12:21:02 +0000 | [diff] [blame] | 4859 | if (Data.Priority.getInt()) |
| 4860 | Flags = Flags | PriorityFlag; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4861 | llvm::Value *TaskFlags = |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 4862 | Data.Final.getPointer() |
| 4863 | ? CGF.Builder.CreateSelect(Data.Final.getPointer(), |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 4864 | CGF.Builder.getInt32(FinalFlag), |
| 4865 | CGF.Builder.getInt32(/*C=*/0)) |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 4866 | : CGF.Builder.getInt32(Data.Final.getInt() ? FinalFlag : 0); |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 4867 | TaskFlags = CGF.Builder.CreateOr(TaskFlags, CGF.Builder.getInt32(Flags)); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4868 | llvm::Value *SharedsSize = CGM.getSize(C.getTypeSizeInChars(SharedsTy)); |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 4869 | llvm::Value *AllocArgs[] = {emitUpdateLocation(CGF, Loc), |
| 4870 | getThreadID(CGF, Loc), TaskFlags, |
| 4871 | KmpTaskTWithPrivatesTySize, SharedsSize, |
| 4872 | CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
| 4873 | TaskEntry, KmpRoutineEntryPtrTy)}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4874 | llvm::Value *NewTask = CGF.EmitRuntimeCall( |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 4875 | createRuntimeFunction(OMPRTL__kmpc_omp_task_alloc), AllocArgs); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4876 | llvm::Value *NewTaskNewTaskTTy = |
| 4877 | CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
| 4878 | NewTask, KmpTaskTWithPrivatesPtrTy); |
Alexey Bataev | 8fc69dc | 2015-05-18 07:54:53 +0000 | [diff] [blame] | 4879 | LValue Base = CGF.MakeNaturalAlignAddrLValue(NewTaskNewTaskTTy, |
| 4880 | KmpTaskTWithPrivatesQTy); |
| 4881 | LValue TDBase = |
| 4882 | CGF.EmitLValueForField(Base, *KmpTaskTWithPrivatesQTyRD->field_begin()); |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 4883 | // Fill the data in the resulting kmp_task_t record. |
| 4884 | // Copy shareds if there are any. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4885 | Address KmpTaskSharedsPtr = Address::invalid(); |
Alexey Bataev | 8fc69dc | 2015-05-18 07:54:53 +0000 | [diff] [blame] | 4886 | if (!SharedsTy->getAsStructureType()->getDecl()->field_empty()) { |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 4887 | KmpTaskSharedsPtr = |
| 4888 | Address(CGF.EmitLoadOfScalar( |
| 4889 | CGF.EmitLValueForField( |
| 4890 | TDBase, *std::next(KmpTaskTQTyRD->field_begin(), |
| 4891 | KmpTaskTShareds)), |
| 4892 | Loc), |
| 4893 | CGF.getNaturalTypeAlignment(SharedsTy)); |
Ivan A. Kosarev | 1860b52 | 2018-01-25 14:21:55 +0000 | [diff] [blame] | 4894 | LValue Dest = CGF.MakeAddrLValue(KmpTaskSharedsPtr, SharedsTy); |
| 4895 | LValue Src = CGF.MakeAddrLValue(Shareds, SharedsTy); |
Richard Smith | e78fac5 | 2018-04-05 20:52:58 +0000 | [diff] [blame] | 4896 | CGF.EmitAggregateCopy(Dest, Src, SharedsTy, AggValueSlot::DoesNotOverlap); |
Alexey Bataev | 8fc69dc | 2015-05-18 07:54:53 +0000 | [diff] [blame] | 4897 | } |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 4898 | // Emit initial values for private copies (if any). |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4899 | TaskResultTy Result; |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 4900 | if (!Privates.empty()) { |
Alexey Bataev | 8a83159 | 2016-05-10 10:36:51 +0000 | [diff] [blame] | 4901 | emitPrivatesInit(CGF, D, KmpTaskSharedsPtr, Base, KmpTaskTWithPrivatesQTyRD, |
| 4902 | SharedsTy, SharedsPtrTy, Data, Privates, |
| 4903 | /*ForDup=*/false); |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4904 | if (isOpenMPTaskLoopDirective(D.getDirectiveKind()) && |
| 4905 | (!Data.LastprivateVars.empty() || checkInitIsRequired(CGF, Privates))) { |
| 4906 | Result.TaskDupFn = emitTaskDupFunction( |
| 4907 | CGM, Loc, D, KmpTaskTWithPrivatesPtrQTy, KmpTaskTWithPrivatesQTyRD, |
| 4908 | KmpTaskTQTyRD, SharedsTy, SharedsPtrTy, Data, Privates, |
| 4909 | /*WithLastIter=*/!Data.LastprivateVars.empty()); |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 4910 | } |
| 4911 | } |
Alexey Bataev | ad537bb | 2016-05-30 09:06:50 +0000 | [diff] [blame] | 4912 | // Fields of union "kmp_cmplrdata_t" for destructors and priority. |
| 4913 | enum { Priority = 0, Destructors = 1 }; |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 4914 | // Provide pointer to function with destructors for privates. |
Alexey Bataev | ad537bb | 2016-05-30 09:06:50 +0000 | [diff] [blame] | 4915 | auto FI = std::next(KmpTaskTQTyRD->field_begin(), Data1); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4916 | const RecordDecl *KmpCmplrdataUD = |
| 4917 | (*FI)->getType()->getAsUnionType()->getDecl(); |
Alexey Bataev | ad537bb | 2016-05-30 09:06:50 +0000 | [diff] [blame] | 4918 | if (NeedsCleanup) { |
| 4919 | llvm::Value *DestructorFn = emitDestructorsFunction( |
| 4920 | CGM, Loc, KmpInt32Ty, KmpTaskTWithPrivatesPtrQTy, |
| 4921 | KmpTaskTWithPrivatesQTy); |
| 4922 | LValue Data1LV = CGF.EmitLValueForField(TDBase, *FI); |
| 4923 | LValue DestructorsLV = CGF.EmitLValueForField( |
| 4924 | Data1LV, *std::next(KmpCmplrdataUD->field_begin(), Destructors)); |
| 4925 | CGF.EmitStoreOfScalar(CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
| 4926 | DestructorFn, KmpRoutineEntryPtrTy), |
| 4927 | DestructorsLV); |
| 4928 | } |
| 4929 | // Set priority. |
| 4930 | if (Data.Priority.getInt()) { |
| 4931 | LValue Data2LV = CGF.EmitLValueForField( |
| 4932 | TDBase, *std::next(KmpTaskTQTyRD->field_begin(), Data2)); |
| 4933 | LValue PriorityLV = CGF.EmitLValueForField( |
| 4934 | Data2LV, *std::next(KmpCmplrdataUD->field_begin(), Priority)); |
| 4935 | CGF.EmitStoreOfScalar(Data.Priority.getPointer(), PriorityLV); |
| 4936 | } |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 4937 | Result.NewTask = NewTask; |
| 4938 | Result.TaskEntry = TaskEntry; |
| 4939 | Result.NewTaskNewTaskTTy = NewTaskNewTaskTTy; |
| 4940 | Result.TDBase = TDBase; |
| 4941 | Result.KmpTaskTQTyRD = KmpTaskTQTyRD; |
| 4942 | return Result; |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4943 | } |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 4944 | |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 4945 | void CGOpenMPRuntime::emitTaskCall(CodeGenFunction &CGF, SourceLocation Loc, |
| 4946 | const OMPExecutableDirective &D, |
| 4947 | llvm::Value *TaskFunction, |
| 4948 | QualType SharedsTy, Address Shareds, |
| 4949 | const Expr *IfCond, |
| 4950 | const OMPTaskDataTy &Data) { |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4951 | if (!CGF.HaveInsertPoint()) |
| 4952 | return; |
| 4953 | |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 4954 | TaskResultTy Result = |
| 4955 | emitTaskInit(CGF, Loc, D, TaskFunction, SharedsTy, Shareds, Data); |
| 4956 | llvm::Value *NewTask = Result.NewTask; |
| 4957 | llvm::Value *TaskEntry = Result.TaskEntry; |
| 4958 | llvm::Value *NewTaskNewTaskTTy = Result.NewTaskNewTaskTTy; |
| 4959 | LValue TDBase = Result.TDBase; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4960 | const RecordDecl *KmpTaskTQTyRD = Result.KmpTaskTQTyRD; |
| 4961 | ASTContext &C = CGM.getContext(); |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 4962 | // Process list of dependences. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4963 | Address DependenciesArray = Address::invalid(); |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 4964 | unsigned NumDependencies = Data.Dependences.size(); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4965 | if (NumDependencies) { |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 4966 | // Dependence kind for RTL. |
Alexey Bataev | 92e82f9 | 2015-11-23 13:33:42 +0000 | [diff] [blame] | 4967 | enum RTLDependenceKindTy { DepIn = 0x01, DepInOut = 0x3 }; |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 4968 | enum RTLDependInfoFieldsTy { BaseAddr, Len, Flags }; |
| 4969 | RecordDecl *KmpDependInfoRD; |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 4970 | QualType FlagsTy = |
| 4971 | C.getIntTypeForBitwidth(C.getTypeSize(C.BoolTy), /*Signed=*/false); |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 4972 | llvm::Type *LLVMFlagsTy = CGF.ConvertTypeForMem(FlagsTy); |
| 4973 | if (KmpDependInfoTy.isNull()) { |
| 4974 | KmpDependInfoRD = C.buildImplicitRecord("kmp_depend_info"); |
| 4975 | KmpDependInfoRD->startDefinition(); |
| 4976 | addFieldToRecordDecl(C, KmpDependInfoRD, C.getIntPtrType()); |
| 4977 | addFieldToRecordDecl(C, KmpDependInfoRD, C.getSizeType()); |
| 4978 | addFieldToRecordDecl(C, KmpDependInfoRD, FlagsTy); |
| 4979 | KmpDependInfoRD->completeDefinition(); |
| 4980 | KmpDependInfoTy = C.getRecordType(KmpDependInfoRD); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4981 | } else { |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 4982 | KmpDependInfoRD = cast<RecordDecl>(KmpDependInfoTy->getAsTagDecl()); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4983 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4984 | CharUnits DependencySize = C.getTypeSizeInChars(KmpDependInfoTy); |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 4985 | // Define type kmp_depend_info[<Dependences.size()>]; |
| 4986 | QualType KmpDependInfoArrayTy = C.getConstantArrayType( |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4987 | KmpDependInfoTy, llvm::APInt(/*numBits=*/64, NumDependencies), |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 4988 | ArrayType::Normal, /*IndexTypeQuals=*/0); |
| 4989 | // kmp_depend_info[<Dependences.size()>] deps; |
Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 4990 | DependenciesArray = |
| 4991 | CGF.CreateMemTemp(KmpDependInfoArrayTy, ".dep.arr.addr"); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4992 | for (unsigned I = 0; I < NumDependencies; ++I) { |
| 4993 | const Expr *E = Data.Dependences[I].second; |
| 4994 | LValue Addr = CGF.EmitLValue(E); |
Alexey Bataev | d6fdc8b | 2015-08-31 07:32:19 +0000 | [diff] [blame] | 4995 | llvm::Value *Size; |
| 4996 | QualType Ty = E->getType(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4997 | if (const auto *ASE = |
| 4998 | dyn_cast<OMPArraySectionExpr>(E->IgnoreParenImpCasts())) { |
Alexey Bataev | d6fdc8b | 2015-08-31 07:32:19 +0000 | [diff] [blame] | 4999 | LValue UpAddrLVal = |
| 5000 | CGF.EmitOMPArraySectionExpr(ASE, /*LowerBound=*/false); |
| 5001 | llvm::Value *UpAddr = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5002 | CGF.Builder.CreateConstGEP1_32(UpAddrLVal.getPointer(), /*Idx0=*/1); |
Alexey Bataev | d6fdc8b | 2015-08-31 07:32:19 +0000 | [diff] [blame] | 5003 | llvm::Value *LowIntPtr = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5004 | CGF.Builder.CreatePtrToInt(Addr.getPointer(), CGM.SizeTy); |
Alexey Bataev | d6fdc8b | 2015-08-31 07:32:19 +0000 | [diff] [blame] | 5005 | llvm::Value *UpIntPtr = CGF.Builder.CreatePtrToInt(UpAddr, CGM.SizeTy); |
| 5006 | Size = CGF.Builder.CreateNUWSub(UpIntPtr, LowIntPtr); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5007 | } else { |
Alexey Bataev | 1189bd0 | 2016-01-26 12:20:39 +0000 | [diff] [blame] | 5008 | Size = CGF.getTypeSize(Ty); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5009 | } |
| 5010 | LValue Base = CGF.MakeAddrLValue( |
| 5011 | CGF.Builder.CreateConstArrayGEP(DependenciesArray, I, DependencySize), |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 5012 | KmpDependInfoTy); |
| 5013 | // deps[i].base_addr = &<Dependences[i].second>; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5014 | LValue BaseAddrLVal = CGF.EmitLValueForField( |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 5015 | Base, *std::next(KmpDependInfoRD->field_begin(), BaseAddr)); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5016 | CGF.EmitStoreOfScalar( |
| 5017 | CGF.Builder.CreatePtrToInt(Addr.getPointer(), CGF.IntPtrTy), |
| 5018 | BaseAddrLVal); |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 5019 | // deps[i].len = sizeof(<Dependences[i].second>); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5020 | LValue LenLVal = CGF.EmitLValueForField( |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 5021 | Base, *std::next(KmpDependInfoRD->field_begin(), Len)); |
| 5022 | CGF.EmitStoreOfScalar(Size, LenLVal); |
| 5023 | // deps[i].flags = <Dependences[i].first>; |
| 5024 | RTLDependenceKindTy DepKind; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5025 | switch (Data.Dependences[I].first) { |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 5026 | case OMPC_DEPEND_in: |
| 5027 | DepKind = DepIn; |
| 5028 | break; |
Alexey Bataev | 92e82f9 | 2015-11-23 13:33:42 +0000 | [diff] [blame] | 5029 | // Out and InOut dependencies must use the same code. |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 5030 | case OMPC_DEPEND_out: |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 5031 | case OMPC_DEPEND_inout: |
| 5032 | DepKind = DepInOut; |
| 5033 | break; |
Alexey Bataev | eb48235 | 2015-12-18 05:05:56 +0000 | [diff] [blame] | 5034 | case OMPC_DEPEND_source: |
Alexey Bataev | a636c7f | 2015-12-23 10:27:45 +0000 | [diff] [blame] | 5035 | case OMPC_DEPEND_sink: |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 5036 | case OMPC_DEPEND_unknown: |
| 5037 | llvm_unreachable("Unknown task dependence type"); |
| 5038 | } |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5039 | LValue FlagsLVal = CGF.EmitLValueForField( |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 5040 | Base, *std::next(KmpDependInfoRD->field_begin(), Flags)); |
| 5041 | CGF.EmitStoreOfScalar(llvm::ConstantInt::get(LLVMFlagsTy, DepKind), |
| 5042 | FlagsLVal); |
| 5043 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5044 | DependenciesArray = CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
| 5045 | CGF.Builder.CreateStructGEP(DependenciesArray, 0, CharUnits::Zero()), |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 5046 | CGF.VoidPtrTy); |
| 5047 | } |
| 5048 | |
Alexander Kornienko | 2a8c18d | 2018-04-06 15:14:32 +0000 | [diff] [blame] | 5049 | // 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] | 5050 | // libcall. |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 5051 | // Build kmp_int32 __kmpc_omp_task_with_deps(ident_t *, kmp_int32 gtid, |
| 5052 | // kmp_task_t *new_task, kmp_int32 ndeps, kmp_depend_info_t *dep_list, |
| 5053 | // kmp_int32 ndeps_noalias, kmp_depend_info_t *noalias_dep_list) if dependence |
| 5054 | // list is not empty |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5055 | llvm::Value *ThreadID = getThreadID(CGF, Loc); |
| 5056 | llvm::Value *UpLoc = emitUpdateLocation(CGF, Loc); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5057 | llvm::Value *TaskArgs[] = { UpLoc, ThreadID, NewTask }; |
| 5058 | llvm::Value *DepTaskArgs[7]; |
| 5059 | if (NumDependencies) { |
| 5060 | DepTaskArgs[0] = UpLoc; |
| 5061 | DepTaskArgs[1] = ThreadID; |
| 5062 | DepTaskArgs[2] = NewTask; |
| 5063 | DepTaskArgs[3] = CGF.Builder.getInt32(NumDependencies); |
| 5064 | DepTaskArgs[4] = DependenciesArray.getPointer(); |
| 5065 | DepTaskArgs[5] = CGF.Builder.getInt32(0); |
| 5066 | DepTaskArgs[6] = llvm::ConstantPointerNull::get(CGF.VoidPtrTy); |
| 5067 | } |
Malcolm Parsons | c6e4583 | 2017-01-13 18:55:32 +0000 | [diff] [blame] | 5068 | auto &&ThenCodeGen = [this, &Data, TDBase, KmpTaskTQTyRD, NumDependencies, |
| 5069 | &TaskArgs, |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5070 | &DepTaskArgs](CodeGenFunction &CGF, PrePostActionTy &) { |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 5071 | if (!Data.Tied) { |
Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 5072 | auto PartIdFI = std::next(KmpTaskTQTyRD->field_begin(), KmpTaskTPartId); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5073 | LValue PartIdLVal = CGF.EmitLValueForField(TDBase, *PartIdFI); |
Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 5074 | CGF.EmitStoreOfScalar(CGF.Builder.getInt32(0), PartIdLVal); |
| 5075 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5076 | if (NumDependencies) { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5077 | CGF.EmitRuntimeCall( |
Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 5078 | createRuntimeFunction(OMPRTL__kmpc_omp_task_with_deps), DepTaskArgs); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5079 | } else { |
Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 5080 | CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_omp_task), |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5081 | TaskArgs); |
| 5082 | } |
Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 5083 | // Check if parent region is untied and build return for untied task; |
| 5084 | if (auto *Region = |
| 5085 | dyn_cast_or_null<CGOpenMPRegionInfo>(CGF.CapturedStmtInfo)) |
| 5086 | Region->emitUntiedSwitch(CGF); |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 5087 | }; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5088 | |
| 5089 | llvm::Value *DepWaitTaskArgs[6]; |
| 5090 | if (NumDependencies) { |
| 5091 | DepWaitTaskArgs[0] = UpLoc; |
| 5092 | DepWaitTaskArgs[1] = ThreadID; |
| 5093 | DepWaitTaskArgs[2] = CGF.Builder.getInt32(NumDependencies); |
| 5094 | DepWaitTaskArgs[3] = DependenciesArray.getPointer(); |
| 5095 | DepWaitTaskArgs[4] = CGF.Builder.getInt32(0); |
| 5096 | DepWaitTaskArgs[5] = llvm::ConstantPointerNull::get(CGF.VoidPtrTy); |
| 5097 | } |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5098 | auto &&ElseCodeGen = [&TaskArgs, ThreadID, NewTaskNewTaskTTy, TaskEntry, |
Alexey Bataev | 3c595a6 | 2017-08-14 15:01:03 +0000 | [diff] [blame] | 5099 | NumDependencies, &DepWaitTaskArgs, |
| 5100 | Loc](CodeGenFunction &CGF, PrePostActionTy &) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5101 | CGOpenMPRuntime &RT = CGF.CGM.getOpenMPRuntime(); |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 5102 | CodeGenFunction::RunCleanupsScope LocalScope(CGF); |
| 5103 | // Build void __kmpc_omp_wait_deps(ident_t *, kmp_int32 gtid, |
| 5104 | // kmp_int32 ndeps, kmp_depend_info_t *dep_list, kmp_int32 |
| 5105 | // ndeps_noalias, kmp_depend_info_t *noalias_dep_list); if dependence info |
| 5106 | // is specified. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5107 | if (NumDependencies) |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5108 | CGF.EmitRuntimeCall(RT.createRuntimeFunction(OMPRTL__kmpc_omp_wait_deps), |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 5109 | DepWaitTaskArgs); |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5110 | // Call proxy_task_entry(gtid, new_task); |
Alexey Bataev | 3c595a6 | 2017-08-14 15:01:03 +0000 | [diff] [blame] | 5111 | auto &&CodeGen = [TaskEntry, ThreadID, NewTaskNewTaskTTy, |
| 5112 | Loc](CodeGenFunction &CGF, PrePostActionTy &Action) { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5113 | Action.Enter(CGF); |
| 5114 | llvm::Value *OutlinedFnArgs[] = {ThreadID, NewTaskNewTaskTTy}; |
Alexey Bataev | 3c595a6 | 2017-08-14 15:01:03 +0000 | [diff] [blame] | 5115 | CGF.CGM.getOpenMPRuntime().emitOutlinedFunctionCall(CGF, Loc, TaskEntry, |
Alexey Bataev | 2c7eee5 | 2017-08-04 19:10:54 +0000 | [diff] [blame] | 5116 | OutlinedFnArgs); |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5117 | }; |
| 5118 | |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 5119 | // Build void __kmpc_omp_task_begin_if0(ident_t *, kmp_int32 gtid, |
| 5120 | // kmp_task_t *new_task); |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 5121 | // Build void __kmpc_omp_task_complete_if0(ident_t *, kmp_int32 gtid, |
| 5122 | // kmp_task_t *new_task); |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5123 | RegionCodeGenTy RCG(CodeGen); |
| 5124 | CommonActionTy Action( |
| 5125 | RT.createRuntimeFunction(OMPRTL__kmpc_omp_task_begin_if0), TaskArgs, |
| 5126 | RT.createRuntimeFunction(OMPRTL__kmpc_omp_task_complete_if0), TaskArgs); |
| 5127 | RCG.setAction(Action); |
| 5128 | RCG(CGF); |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 5129 | }; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5130 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5131 | if (IfCond) { |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 5132 | emitOMPIfClause(CGF, IfCond, ThenCodeGen, ElseCodeGen); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5133 | } else { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5134 | RegionCodeGenTy ThenRCG(ThenCodeGen); |
| 5135 | ThenRCG(CGF); |
Alexey Bataev | f539faa | 2016-03-28 12:58:34 +0000 | [diff] [blame] | 5136 | } |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 5137 | } |
| 5138 | |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 5139 | void CGOpenMPRuntime::emitTaskLoopCall(CodeGenFunction &CGF, SourceLocation Loc, |
| 5140 | const OMPLoopDirective &D, |
| 5141 | llvm::Value *TaskFunction, |
| 5142 | QualType SharedsTy, Address Shareds, |
| 5143 | const Expr *IfCond, |
| 5144 | const OMPTaskDataTy &Data) { |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 5145 | if (!CGF.HaveInsertPoint()) |
| 5146 | return; |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 5147 | TaskResultTy Result = |
| 5148 | emitTaskInit(CGF, Loc, D, TaskFunction, SharedsTy, Shareds, Data); |
Alexander Kornienko | 2a8c18d | 2018-04-06 15:14:32 +0000 | [diff] [blame] | 5149 | // 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] | 5150 | // libcall. |
| 5151 | // Call to void __kmpc_taskloop(ident_t *loc, int gtid, kmp_task_t *task, int |
| 5152 | // if_val, kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st, int nogroup, int |
| 5153 | // sched, kmp_uint64 grainsize, void *task_dup); |
| 5154 | llvm::Value *ThreadID = getThreadID(CGF, Loc); |
| 5155 | llvm::Value *UpLoc = emitUpdateLocation(CGF, Loc); |
| 5156 | llvm::Value *IfVal; |
| 5157 | if (IfCond) { |
| 5158 | IfVal = CGF.Builder.CreateIntCast(CGF.EvaluateExprAsBool(IfCond), CGF.IntTy, |
| 5159 | /*isSigned=*/true); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5160 | } else { |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 5161 | IfVal = llvm::ConstantInt::getSigned(CGF.IntTy, /*V=*/1); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5162 | } |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 5163 | |
| 5164 | LValue LBLVal = CGF.EmitLValueForField( |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 5165 | Result.TDBase, |
| 5166 | *std::next(Result.KmpTaskTQTyRD->field_begin(), KmpTaskTLowerBound)); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5167 | const auto *LBVar = |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 5168 | cast<VarDecl>(cast<DeclRefExpr>(D.getLowerBoundVariable())->getDecl()); |
| 5169 | CGF.EmitAnyExprToMem(LBVar->getInit(), LBLVal.getAddress(), LBLVal.getQuals(), |
| 5170 | /*IsInitializer=*/true); |
| 5171 | LValue UBLVal = CGF.EmitLValueForField( |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 5172 | Result.TDBase, |
| 5173 | *std::next(Result.KmpTaskTQTyRD->field_begin(), KmpTaskTUpperBound)); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5174 | const auto *UBVar = |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 5175 | cast<VarDecl>(cast<DeclRefExpr>(D.getUpperBoundVariable())->getDecl()); |
| 5176 | CGF.EmitAnyExprToMem(UBVar->getInit(), UBLVal.getAddress(), UBLVal.getQuals(), |
| 5177 | /*IsInitializer=*/true); |
| 5178 | LValue StLVal = CGF.EmitLValueForField( |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 5179 | Result.TDBase, |
| 5180 | *std::next(Result.KmpTaskTQTyRD->field_begin(), KmpTaskTStride)); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5181 | const auto *StVar = |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 5182 | cast<VarDecl>(cast<DeclRefExpr>(D.getStrideVariable())->getDecl()); |
| 5183 | CGF.EmitAnyExprToMem(StVar->getInit(), StLVal.getAddress(), StLVal.getQuals(), |
| 5184 | /*IsInitializer=*/true); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 5185 | // Store reductions address. |
| 5186 | LValue RedLVal = CGF.EmitLValueForField( |
| 5187 | Result.TDBase, |
| 5188 | *std::next(Result.KmpTaskTQTyRD->field_begin(), KmpTaskTReductions)); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5189 | if (Data.Reductions) { |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 5190 | CGF.EmitStoreOfScalar(Data.Reductions, RedLVal); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5191 | } else { |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 5192 | CGF.EmitNullInitialization(RedLVal.getAddress(), |
| 5193 | CGF.getContext().VoidPtrTy); |
| 5194 | } |
Alexey Bataev | 2b19a6f | 2016-04-28 09:15:06 +0000 | [diff] [blame] | 5195 | enum { NoSchedule = 0, Grainsize = 1, NumTasks = 2 }; |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 5196 | llvm::Value *TaskArgs[] = { |
Alexey Bataev | 3344603 | 2017-07-12 18:09:32 +0000 | [diff] [blame] | 5197 | UpLoc, |
| 5198 | ThreadID, |
| 5199 | Result.NewTask, |
| 5200 | IfVal, |
| 5201 | LBLVal.getPointer(), |
| 5202 | UBLVal.getPointer(), |
Alexey Bataev | a9b9cc0 | 2018-01-23 18:12:38 +0000 | [diff] [blame] | 5203 | CGF.EmitLoadOfScalar(StLVal, Loc), |
Alexey Bataev | 3344603 | 2017-07-12 18:09:32 +0000 | [diff] [blame] | 5204 | llvm::ConstantInt::getNullValue( |
| 5205 | CGF.IntTy), // Always 0 because taskgroup emitted by the compiler |
Alexey Bataev | 2b19a6f | 2016-04-28 09:15:06 +0000 | [diff] [blame] | 5206 | llvm::ConstantInt::getSigned( |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 5207 | CGF.IntTy, Data.Schedule.getPointer() |
| 5208 | ? Data.Schedule.getInt() ? NumTasks : Grainsize |
Alexey Bataev | 2b19a6f | 2016-04-28 09:15:06 +0000 | [diff] [blame] | 5209 | : NoSchedule), |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 5210 | Data.Schedule.getPointer() |
| 5211 | ? CGF.Builder.CreateIntCast(Data.Schedule.getPointer(), CGF.Int64Ty, |
Alexey Bataev | 2b19a6f | 2016-04-28 09:15:06 +0000 | [diff] [blame] | 5212 | /*isSigned=*/false) |
| 5213 | : llvm::ConstantInt::get(CGF.Int64Ty, /*V=*/0), |
Alexey Bataev | 3344603 | 2017-07-12 18:09:32 +0000 | [diff] [blame] | 5214 | Result.TaskDupFn ? CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
| 5215 | Result.TaskDupFn, CGF.VoidPtrTy) |
| 5216 | : llvm::ConstantPointerNull::get(CGF.VoidPtrTy)}; |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 5217 | CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_taskloop), TaskArgs); |
| 5218 | } |
| 5219 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 5220 | /// Emit reduction operation for each element of array (required for |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5221 | /// array sections) LHS op = RHS. |
| 5222 | /// \param Type Type of array. |
| 5223 | /// \param LHSVar Variable on the left side of the reduction operation |
| 5224 | /// (references element of array in original variable). |
| 5225 | /// \param RHSVar Variable on the right side of the reduction operation |
| 5226 | /// (references element of array in original variable). |
| 5227 | /// \param RedOpGen Generator of reduction operation with use of LHSVar and |
| 5228 | /// RHSVar. |
Benjamin Kramer | e003ca2 | 2015-10-28 13:54:16 +0000 | [diff] [blame] | 5229 | static void EmitOMPAggregateReduction( |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5230 | CodeGenFunction &CGF, QualType Type, const VarDecl *LHSVar, |
| 5231 | const VarDecl *RHSVar, |
| 5232 | const llvm::function_ref<void(CodeGenFunction &CGF, const Expr *, |
| 5233 | const Expr *, const Expr *)> &RedOpGen, |
| 5234 | const Expr *XExpr = nullptr, const Expr *EExpr = nullptr, |
| 5235 | const Expr *UpExpr = nullptr) { |
| 5236 | // Perform element-by-element initialization. |
| 5237 | QualType ElementTy; |
| 5238 | Address LHSAddr = CGF.GetAddrOfLocalVar(LHSVar); |
| 5239 | Address RHSAddr = CGF.GetAddrOfLocalVar(RHSVar); |
| 5240 | |
| 5241 | // Drill down to the base element type on both arrays. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5242 | const ArrayType *ArrayTy = Type->getAsArrayTypeUnsafe(); |
| 5243 | llvm::Value *NumElements = CGF.emitArrayLength(ArrayTy, ElementTy, LHSAddr); |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5244 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5245 | llvm::Value *RHSBegin = RHSAddr.getPointer(); |
| 5246 | llvm::Value *LHSBegin = LHSAddr.getPointer(); |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5247 | // Cast from pointer to array type to pointer to single element. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5248 | llvm::Value *LHSEnd = CGF.Builder.CreateGEP(LHSBegin, NumElements); |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5249 | // The basic structure here is a while-do loop. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5250 | llvm::BasicBlock *BodyBB = CGF.createBasicBlock("omp.arraycpy.body"); |
| 5251 | llvm::BasicBlock *DoneBB = CGF.createBasicBlock("omp.arraycpy.done"); |
| 5252 | llvm::Value *IsEmpty = |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5253 | CGF.Builder.CreateICmpEQ(LHSBegin, LHSEnd, "omp.arraycpy.isempty"); |
| 5254 | CGF.Builder.CreateCondBr(IsEmpty, DoneBB, BodyBB); |
| 5255 | |
| 5256 | // Enter the loop body, making that address the current address. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5257 | llvm::BasicBlock *EntryBB = CGF.Builder.GetInsertBlock(); |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5258 | CGF.EmitBlock(BodyBB); |
| 5259 | |
| 5260 | CharUnits ElementSize = CGF.getContext().getTypeSizeInChars(ElementTy); |
| 5261 | |
| 5262 | llvm::PHINode *RHSElementPHI = CGF.Builder.CreatePHI( |
| 5263 | RHSBegin->getType(), 2, "omp.arraycpy.srcElementPast"); |
| 5264 | RHSElementPHI->addIncoming(RHSBegin, EntryBB); |
| 5265 | Address RHSElementCurrent = |
| 5266 | Address(RHSElementPHI, |
| 5267 | RHSAddr.getAlignment().alignmentOfArrayElement(ElementSize)); |
| 5268 | |
| 5269 | llvm::PHINode *LHSElementPHI = CGF.Builder.CreatePHI( |
| 5270 | LHSBegin->getType(), 2, "omp.arraycpy.destElementPast"); |
| 5271 | LHSElementPHI->addIncoming(LHSBegin, EntryBB); |
| 5272 | Address LHSElementCurrent = |
| 5273 | Address(LHSElementPHI, |
| 5274 | LHSAddr.getAlignment().alignmentOfArrayElement(ElementSize)); |
| 5275 | |
| 5276 | // Emit copy. |
| 5277 | CodeGenFunction::OMPPrivateScope Scope(CGF); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5278 | Scope.addPrivate(LHSVar, [=]() { return LHSElementCurrent; }); |
| 5279 | Scope.addPrivate(RHSVar, [=]() { return RHSElementCurrent; }); |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5280 | Scope.Privatize(); |
| 5281 | RedOpGen(CGF, XExpr, EExpr, UpExpr); |
| 5282 | Scope.ForceCleanup(); |
| 5283 | |
| 5284 | // Shift the address forward by one element. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5285 | llvm::Value *LHSElementNext = CGF.Builder.CreateConstGEP1_32( |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5286 | LHSElementPHI, /*Idx0=*/1, "omp.arraycpy.dest.element"); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5287 | llvm::Value *RHSElementNext = CGF.Builder.CreateConstGEP1_32( |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5288 | RHSElementPHI, /*Idx0=*/1, "omp.arraycpy.src.element"); |
| 5289 | // Check whether we've reached the end. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5290 | llvm::Value *Done = |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5291 | CGF.Builder.CreateICmpEQ(LHSElementNext, LHSEnd, "omp.arraycpy.done"); |
| 5292 | CGF.Builder.CreateCondBr(Done, DoneBB, BodyBB); |
| 5293 | LHSElementPHI->addIncoming(LHSElementNext, CGF.Builder.GetInsertBlock()); |
| 5294 | RHSElementPHI->addIncoming(RHSElementNext, CGF.Builder.GetInsertBlock()); |
| 5295 | |
| 5296 | // Done. |
| 5297 | CGF.EmitBlock(DoneBB, /*IsFinished=*/true); |
| 5298 | } |
| 5299 | |
Alexey Bataev | a839ddd | 2016-03-17 10:19:46 +0000 | [diff] [blame] | 5300 | /// Emit reduction combiner. If the combiner is a simple expression emit it as |
| 5301 | /// is, otherwise consider it as combiner of UDR decl and emit it as a call of |
| 5302 | /// UDR combiner function. |
| 5303 | static void emitReductionCombiner(CodeGenFunction &CGF, |
| 5304 | const Expr *ReductionOp) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5305 | if (const auto *CE = dyn_cast<CallExpr>(ReductionOp)) |
| 5306 | if (const auto *OVE = dyn_cast<OpaqueValueExpr>(CE->getCallee())) |
| 5307 | if (const auto *DRE = |
Alexey Bataev | a839ddd | 2016-03-17 10:19:46 +0000 | [diff] [blame] | 5308 | dyn_cast<DeclRefExpr>(OVE->getSourceExpr()->IgnoreImpCasts())) |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5309 | if (const auto *DRD = |
| 5310 | dyn_cast<OMPDeclareReductionDecl>(DRE->getDecl())) { |
Alexey Bataev | a839ddd | 2016-03-17 10:19:46 +0000 | [diff] [blame] | 5311 | std::pair<llvm::Function *, llvm::Function *> Reduction = |
| 5312 | CGF.CGM.getOpenMPRuntime().getUserDefinedReduction(DRD); |
| 5313 | RValue Func = RValue::get(Reduction.first); |
| 5314 | CodeGenFunction::OpaqueValueMapping Map(CGF, OVE, Func); |
| 5315 | CGF.EmitIgnoredExpr(ReductionOp); |
| 5316 | return; |
| 5317 | } |
| 5318 | CGF.EmitIgnoredExpr(ReductionOp); |
| 5319 | } |
| 5320 | |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 5321 | llvm::Value *CGOpenMPRuntime::emitReductionFunction( |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 5322 | CodeGenModule &CGM, SourceLocation Loc, llvm::Type *ArgsType, |
| 5323 | ArrayRef<const Expr *> Privates, ArrayRef<const Expr *> LHSExprs, |
| 5324 | ArrayRef<const Expr *> RHSExprs, ArrayRef<const Expr *> ReductionOps) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5325 | ASTContext &C = CGM.getContext(); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5326 | |
| 5327 | // void reduction_func(void *LHSArg, void *RHSArg); |
| 5328 | FunctionArgList Args; |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 5329 | ImplicitParamDecl LHSArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, C.VoidPtrTy, |
| 5330 | ImplicitParamDecl::Other); |
| 5331 | ImplicitParamDecl RHSArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, C.VoidPtrTy, |
| 5332 | ImplicitParamDecl::Other); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5333 | Args.push_back(&LHSArg); |
| 5334 | Args.push_back(&RHSArg); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5335 | const auto &CGFI = |
| 5336 | CGM.getTypes().arrangeBuiltinFunctionDeclaration(C.VoidTy, Args); |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 5337 | std::string Name = getName({"omp", "reduction", "reduction_func"}); |
| 5338 | auto *Fn = llvm::Function::Create(CGM.getTypes().GetFunctionType(CGFI), |
| 5339 | llvm::GlobalValue::InternalLinkage, Name, |
| 5340 | &CGM.getModule()); |
Rafael Espindola | 51ec5a9 | 2018-02-28 23:46:35 +0000 | [diff] [blame] | 5341 | CGM.SetInternalFunctionAttributes(GlobalDecl(), Fn, CGFI); |
Alexey Bataev | c0f879b | 2018-04-10 20:10:53 +0000 | [diff] [blame] | 5342 | Fn->setDoesNotRecurse(); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5343 | CodeGenFunction CGF(CGM); |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 5344 | CGF.StartFunction(GlobalDecl(), C.VoidTy, Fn, CGFI, Args, Loc, Loc); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5345 | |
| 5346 | // Dst = (void*[n])(LHSArg); |
| 5347 | // Src = (void*[n])(RHSArg); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5348 | Address LHS(CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
| 5349 | CGF.Builder.CreateLoad(CGF.GetAddrOfLocalVar(&LHSArg)), |
| 5350 | ArgsType), CGF.getPointerAlign()); |
| 5351 | Address RHS(CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
| 5352 | CGF.Builder.CreateLoad(CGF.GetAddrOfLocalVar(&RHSArg)), |
| 5353 | ArgsType), CGF.getPointerAlign()); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5354 | |
| 5355 | // ... |
| 5356 | // *(Type<i>*)lhs[i] = RedOp<i>(*(Type<i>*)lhs[i], *(Type<i>*)rhs[i]); |
| 5357 | // ... |
| 5358 | CodeGenFunction::OMPPrivateScope Scope(CGF); |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5359 | auto IPriv = Privates.begin(); |
| 5360 | unsigned Idx = 0; |
| 5361 | for (unsigned I = 0, E = ReductionOps.size(); I < E; ++I, ++IPriv, ++Idx) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5362 | const auto *RHSVar = |
| 5363 | cast<VarDecl>(cast<DeclRefExpr>(RHSExprs[I])->getDecl()); |
| 5364 | Scope.addPrivate(RHSVar, [&CGF, RHS, Idx, RHSVar]() { |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5365 | return emitAddrOfVarFromArray(CGF, RHS, Idx, RHSVar); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5366 | }); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5367 | const auto *LHSVar = |
| 5368 | cast<VarDecl>(cast<DeclRefExpr>(LHSExprs[I])->getDecl()); |
| 5369 | Scope.addPrivate(LHSVar, [&CGF, LHS, Idx, LHSVar]() { |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5370 | return emitAddrOfVarFromArray(CGF, LHS, Idx, LHSVar); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5371 | }); |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5372 | QualType PrivTy = (*IPriv)->getType(); |
Alexey Bataev | 1189bd0 | 2016-01-26 12:20:39 +0000 | [diff] [blame] | 5373 | if (PrivTy->isVariablyModifiedType()) { |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5374 | // Get array size and emit VLA type. |
| 5375 | ++Idx; |
| 5376 | Address Elem = |
| 5377 | CGF.Builder.CreateConstArrayGEP(LHS, Idx, CGF.getPointerSize()); |
| 5378 | llvm::Value *Ptr = CGF.Builder.CreateLoad(Elem); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5379 | const VariableArrayType *VLA = |
| 5380 | CGF.getContext().getAsVariableArrayType(PrivTy); |
| 5381 | const auto *OVE = cast<OpaqueValueExpr>(VLA->getSizeExpr()); |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5382 | CodeGenFunction::OpaqueValueMapping OpaqueMap( |
Alexey Bataev | 1189bd0 | 2016-01-26 12:20:39 +0000 | [diff] [blame] | 5383 | CGF, OVE, RValue::get(CGF.Builder.CreatePtrToInt(Ptr, CGF.SizeTy))); |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5384 | CGF.EmitVariablyModifiedType(PrivTy); |
| 5385 | } |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5386 | } |
| 5387 | Scope.Privatize(); |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5388 | IPriv = Privates.begin(); |
| 5389 | auto ILHS = LHSExprs.begin(); |
| 5390 | auto IRHS = RHSExprs.begin(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5391 | for (const Expr *E : ReductionOps) { |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5392 | if ((*IPriv)->getType()->isArrayType()) { |
| 5393 | // Emit reduction for array section. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5394 | const auto *LHSVar = cast<VarDecl>(cast<DeclRefExpr>(*ILHS)->getDecl()); |
| 5395 | const auto *RHSVar = cast<VarDecl>(cast<DeclRefExpr>(*IRHS)->getDecl()); |
Alexey Bataev | a839ddd | 2016-03-17 10:19:46 +0000 | [diff] [blame] | 5396 | EmitOMPAggregateReduction( |
| 5397 | CGF, (*IPriv)->getType(), LHSVar, RHSVar, |
| 5398 | [=](CodeGenFunction &CGF, const Expr *, const Expr *, const Expr *) { |
| 5399 | emitReductionCombiner(CGF, E); |
| 5400 | }); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5401 | } else { |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5402 | // Emit reduction for array subscript or single variable. |
Alexey Bataev | a839ddd | 2016-03-17 10:19:46 +0000 | [diff] [blame] | 5403 | emitReductionCombiner(CGF, E); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5404 | } |
Richard Trieu | cc3949d | 2016-02-18 22:34:54 +0000 | [diff] [blame] | 5405 | ++IPriv; |
| 5406 | ++ILHS; |
| 5407 | ++IRHS; |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5408 | } |
| 5409 | Scope.ForceCleanup(); |
| 5410 | CGF.FinishFunction(); |
| 5411 | return Fn; |
| 5412 | } |
| 5413 | |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 5414 | void CGOpenMPRuntime::emitSingleReductionCombiner(CodeGenFunction &CGF, |
| 5415 | const Expr *ReductionOp, |
| 5416 | const Expr *PrivateRef, |
| 5417 | const DeclRefExpr *LHS, |
| 5418 | const DeclRefExpr *RHS) { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5419 | if (PrivateRef->getType()->isArrayType()) { |
| 5420 | // Emit reduction for array section. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5421 | const auto *LHSVar = cast<VarDecl>(LHS->getDecl()); |
| 5422 | const auto *RHSVar = cast<VarDecl>(RHS->getDecl()); |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5423 | EmitOMPAggregateReduction( |
| 5424 | CGF, PrivateRef->getType(), LHSVar, RHSVar, |
| 5425 | [=](CodeGenFunction &CGF, const Expr *, const Expr *, const Expr *) { |
| 5426 | emitReductionCombiner(CGF, ReductionOp); |
| 5427 | }); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5428 | } else { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5429 | // Emit reduction for array subscript or single variable. |
| 5430 | emitReductionCombiner(CGF, ReductionOp); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5431 | } |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5432 | } |
| 5433 | |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5434 | void CGOpenMPRuntime::emitReduction(CodeGenFunction &CGF, SourceLocation Loc, |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5435 | ArrayRef<const Expr *> Privates, |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5436 | ArrayRef<const Expr *> LHSExprs, |
| 5437 | ArrayRef<const Expr *> RHSExprs, |
| 5438 | ArrayRef<const Expr *> ReductionOps, |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 5439 | ReductionOptionsTy Options) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 5440 | if (!CGF.HaveInsertPoint()) |
| 5441 | return; |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 5442 | |
| 5443 | bool WithNowait = Options.WithNowait; |
| 5444 | bool SimpleReduction = Options.SimpleReduction; |
| 5445 | |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5446 | // Next code should be emitted for reduction: |
| 5447 | // |
| 5448 | // static kmp_critical_name lock = { 0 }; |
| 5449 | // |
| 5450 | // void reduce_func(void *lhs[<n>], void *rhs[<n>]) { |
| 5451 | // *(Type0*)lhs[0] = ReductionOperation0(*(Type0*)lhs[0], *(Type0*)rhs[0]); |
| 5452 | // ... |
| 5453 | // *(Type<n>-1*)lhs[<n>-1] = ReductionOperation<n>-1(*(Type<n>-1*)lhs[<n>-1], |
| 5454 | // *(Type<n>-1*)rhs[<n>-1]); |
| 5455 | // } |
| 5456 | // |
| 5457 | // ... |
| 5458 | // void *RedList[<n>] = {&<RHSExprs>[0], ..., &<RHSExprs>[<n>-1]}; |
| 5459 | // switch (__kmpc_reduce{_nowait}(<loc>, <gtid>, <n>, sizeof(RedList), |
| 5460 | // RedList, reduce_func, &<lock>)) { |
| 5461 | // case 1: |
| 5462 | // ... |
| 5463 | // <LHSExprs>[i] = RedOp<i>(*<LHSExprs>[i], *<RHSExprs>[i]); |
| 5464 | // ... |
| 5465 | // __kmpc_end_reduce{_nowait}(<loc>, <gtid>, &<lock>); |
| 5466 | // break; |
| 5467 | // case 2: |
| 5468 | // ... |
| 5469 | // Atomic(<LHSExprs>[i] = RedOp<i>(*<LHSExprs>[i], *<RHSExprs>[i])); |
| 5470 | // ... |
Alexey Bataev | 69a4779 | 2015-05-07 03:54:03 +0000 | [diff] [blame] | 5471 | // [__kmpc_end_reduce(<loc>, <gtid>, &<lock>);] |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5472 | // break; |
| 5473 | // default:; |
| 5474 | // } |
Alexey Bataev | 89e7e8e | 2015-06-17 06:21:39 +0000 | [diff] [blame] | 5475 | // |
| 5476 | // if SimpleReduction is true, only the next code is generated: |
| 5477 | // ... |
| 5478 | // <LHSExprs>[i] = RedOp<i>(*<LHSExprs>[i], *<RHSExprs>[i]); |
| 5479 | // ... |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5480 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5481 | ASTContext &C = CGM.getContext(); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5482 | |
Alexey Bataev | 89e7e8e | 2015-06-17 06:21:39 +0000 | [diff] [blame] | 5483 | if (SimpleReduction) { |
| 5484 | CodeGenFunction::RunCleanupsScope Scope(CGF); |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5485 | auto IPriv = Privates.begin(); |
| 5486 | auto ILHS = LHSExprs.begin(); |
| 5487 | auto IRHS = RHSExprs.begin(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5488 | for (const Expr *E : ReductionOps) { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5489 | emitSingleReductionCombiner(CGF, E, *IPriv, cast<DeclRefExpr>(*ILHS), |
| 5490 | cast<DeclRefExpr>(*IRHS)); |
Richard Trieu | cc3949d | 2016-02-18 22:34:54 +0000 | [diff] [blame] | 5491 | ++IPriv; |
| 5492 | ++ILHS; |
| 5493 | ++IRHS; |
Alexey Bataev | 89e7e8e | 2015-06-17 06:21:39 +0000 | [diff] [blame] | 5494 | } |
| 5495 | return; |
| 5496 | } |
| 5497 | |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5498 | // 1. Build a list of reduction variables. |
| 5499 | // void *RedList[<n>] = {<ReductionVars>[0], ..., <ReductionVars>[<n>-1]}; |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5500 | auto Size = RHSExprs.size(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5501 | for (const Expr *E : Privates) { |
Alexey Bataev | 1189bd0 | 2016-01-26 12:20:39 +0000 | [diff] [blame] | 5502 | if (E->getType()->isVariablyModifiedType()) |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5503 | // Reserve place for array size. |
| 5504 | ++Size; |
| 5505 | } |
| 5506 | llvm::APInt ArraySize(/*unsigned int numBits=*/32, Size); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5507 | QualType ReductionArrayTy = |
| 5508 | C.getConstantArrayType(C.VoidPtrTy, ArraySize, ArrayType::Normal, |
| 5509 | /*IndexTypeQuals=*/0); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5510 | Address ReductionList = |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5511 | CGF.CreateMemTemp(ReductionArrayTy, ".omp.reduction.red_list"); |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5512 | auto IPriv = Privates.begin(); |
| 5513 | unsigned Idx = 0; |
| 5514 | for (unsigned I = 0, E = RHSExprs.size(); I < E; ++I, ++IPriv, ++Idx) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5515 | Address Elem = |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5516 | CGF.Builder.CreateConstArrayGEP(ReductionList, Idx, CGF.getPointerSize()); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5517 | CGF.Builder.CreateStore( |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5518 | CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5519 | CGF.EmitLValue(RHSExprs[I]).getPointer(), CGF.VoidPtrTy), |
| 5520 | Elem); |
Alexey Bataev | 1189bd0 | 2016-01-26 12:20:39 +0000 | [diff] [blame] | 5521 | if ((*IPriv)->getType()->isVariablyModifiedType()) { |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5522 | // Store array size. |
| 5523 | ++Idx; |
| 5524 | Elem = CGF.Builder.CreateConstArrayGEP(ReductionList, Idx, |
| 5525 | CGF.getPointerSize()); |
Alexey Bataev | 1189bd0 | 2016-01-26 12:20:39 +0000 | [diff] [blame] | 5526 | llvm::Value *Size = CGF.Builder.CreateIntCast( |
| 5527 | CGF.getVLASize( |
| 5528 | CGF.getContext().getAsVariableArrayType((*IPriv)->getType())) |
Sander de Smalen | 891af03a | 2018-02-03 13:55:59 +0000 | [diff] [blame] | 5529 | .NumElts, |
Alexey Bataev | 1189bd0 | 2016-01-26 12:20:39 +0000 | [diff] [blame] | 5530 | CGF.SizeTy, /*isSigned=*/false); |
| 5531 | CGF.Builder.CreateStore(CGF.Builder.CreateIntToPtr(Size, CGF.VoidPtrTy), |
| 5532 | Elem); |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5533 | } |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5534 | } |
| 5535 | |
| 5536 | // 2. Emit reduce_func(). |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5537 | llvm::Value *ReductionFn = emitReductionFunction( |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 5538 | CGM, Loc, CGF.ConvertTypeForMem(ReductionArrayTy)->getPointerTo(), |
| 5539 | Privates, LHSExprs, RHSExprs, ReductionOps); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5540 | |
| 5541 | // 3. Create static kmp_critical_name lock = { 0 }; |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 5542 | std::string Name = getName({"reduction"}); |
| 5543 | llvm::Value *Lock = getCriticalRegionLock(Name); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5544 | |
| 5545 | // 4. Build res = __kmpc_reduce{_nowait}(<loc>, <gtid>, <n>, sizeof(RedList), |
| 5546 | // RedList, reduce_func, &<lock>); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5547 | llvm::Value *IdentTLoc = emitUpdateLocation(CGF, Loc, OMP_ATOMIC_REDUCE); |
| 5548 | llvm::Value *ThreadId = getThreadID(CGF, Loc); |
| 5549 | llvm::Value *ReductionArrayTySize = CGF.getTypeSize(ReductionArrayTy); |
| 5550 | llvm::Value *RL = CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
Samuel Antao | 4c8035b | 2016-12-12 18:00:20 +0000 | [diff] [blame] | 5551 | ReductionList.getPointer(), CGF.VoidPtrTy); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5552 | llvm::Value *Args[] = { |
| 5553 | IdentTLoc, // ident_t *<loc> |
| 5554 | ThreadId, // i32 <gtid> |
| 5555 | CGF.Builder.getInt32(RHSExprs.size()), // i32 <n> |
| 5556 | ReductionArrayTySize, // size_type sizeof(RedList) |
| 5557 | RL, // void *RedList |
| 5558 | ReductionFn, // void (*) (void *, void *) <reduce_func> |
| 5559 | Lock // kmp_critical_name *&<lock> |
| 5560 | }; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5561 | llvm::Value *Res = CGF.EmitRuntimeCall( |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5562 | createRuntimeFunction(WithNowait ? OMPRTL__kmpc_reduce_nowait |
| 5563 | : OMPRTL__kmpc_reduce), |
| 5564 | Args); |
| 5565 | |
| 5566 | // 5. Build switch(res) |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5567 | llvm::BasicBlock *DefaultBB = CGF.createBasicBlock(".omp.reduction.default"); |
| 5568 | llvm::SwitchInst *SwInst = |
| 5569 | CGF.Builder.CreateSwitch(Res, DefaultBB, /*NumCases=*/2); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5570 | |
| 5571 | // 6. Build case 1: |
| 5572 | // ... |
| 5573 | // <LHSExprs>[i] = RedOp<i>(*<LHSExprs>[i], *<RHSExprs>[i]); |
| 5574 | // ... |
| 5575 | // __kmpc_end_reduce{_nowait}(<loc>, <gtid>, &<lock>); |
| 5576 | // break; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5577 | llvm::BasicBlock *Case1BB = CGF.createBasicBlock(".omp.reduction.case1"); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5578 | SwInst->addCase(CGF.Builder.getInt32(1), Case1BB); |
| 5579 | CGF.EmitBlock(Case1BB); |
| 5580 | |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5581 | // Add emission of __kmpc_end_reduce{_nowait}(<loc>, <gtid>, &<lock>); |
| 5582 | llvm::Value *EndArgs[] = { |
| 5583 | IdentTLoc, // ident_t *<loc> |
| 5584 | ThreadId, // i32 <gtid> |
| 5585 | Lock // kmp_critical_name *&<lock> |
| 5586 | }; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5587 | auto &&CodeGen = [Privates, LHSExprs, RHSExprs, ReductionOps]( |
| 5588 | CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 5589 | CGOpenMPRuntime &RT = CGF.CGM.getOpenMPRuntime(); |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5590 | auto IPriv = Privates.begin(); |
| 5591 | auto ILHS = LHSExprs.begin(); |
| 5592 | auto IRHS = RHSExprs.begin(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5593 | for (const Expr *E : ReductionOps) { |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 5594 | RT.emitSingleReductionCombiner(CGF, E, *IPriv, cast<DeclRefExpr>(*ILHS), |
| 5595 | cast<DeclRefExpr>(*IRHS)); |
Richard Trieu | cc3949d | 2016-02-18 22:34:54 +0000 | [diff] [blame] | 5596 | ++IPriv; |
| 5597 | ++ILHS; |
| 5598 | ++IRHS; |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5599 | } |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5600 | }; |
| 5601 | RegionCodeGenTy RCG(CodeGen); |
| 5602 | CommonActionTy Action( |
| 5603 | nullptr, llvm::None, |
| 5604 | createRuntimeFunction(WithNowait ? OMPRTL__kmpc_end_reduce_nowait |
| 5605 | : OMPRTL__kmpc_end_reduce), |
| 5606 | EndArgs); |
| 5607 | RCG.setAction(Action); |
| 5608 | RCG(CGF); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5609 | |
| 5610 | CGF.EmitBranch(DefaultBB); |
| 5611 | |
| 5612 | // 7. Build case 2: |
| 5613 | // ... |
| 5614 | // Atomic(<LHSExprs>[i] = RedOp<i>(*<LHSExprs>[i], *<RHSExprs>[i])); |
| 5615 | // ... |
| 5616 | // break; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5617 | llvm::BasicBlock *Case2BB = CGF.createBasicBlock(".omp.reduction.case2"); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5618 | SwInst->addCase(CGF.Builder.getInt32(2), Case2BB); |
| 5619 | CGF.EmitBlock(Case2BB); |
| 5620 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5621 | auto &&AtomicCodeGen = [Loc, Privates, LHSExprs, RHSExprs, ReductionOps]( |
| 5622 | CodeGenFunction &CGF, PrePostActionTy &Action) { |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5623 | auto ILHS = LHSExprs.begin(); |
| 5624 | auto IRHS = RHSExprs.begin(); |
| 5625 | auto IPriv = Privates.begin(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5626 | for (const Expr *E : ReductionOps) { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5627 | const Expr *XExpr = nullptr; |
| 5628 | const Expr *EExpr = nullptr; |
| 5629 | const Expr *UpExpr = nullptr; |
| 5630 | BinaryOperatorKind BO = BO_Comma; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5631 | if (const auto *BO = dyn_cast<BinaryOperator>(E)) { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5632 | if (BO->getOpcode() == BO_Assign) { |
| 5633 | XExpr = BO->getLHS(); |
| 5634 | UpExpr = BO->getRHS(); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5635 | } |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5636 | } |
| 5637 | // Try to emit update expression as a simple atomic. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5638 | const Expr *RHSExpr = UpExpr; |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5639 | if (RHSExpr) { |
| 5640 | // Analyze RHS part of the whole expression. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5641 | if (const auto *ACO = dyn_cast<AbstractConditionalOperator>( |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5642 | RHSExpr->IgnoreParenImpCasts())) { |
| 5643 | // If this is a conditional operator, analyze its condition for |
| 5644 | // min/max reduction operator. |
| 5645 | RHSExpr = ACO->getCond(); |
Alexey Bataev | 69a4779 | 2015-05-07 03:54:03 +0000 | [diff] [blame] | 5646 | } |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5647 | if (const auto *BORHS = |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5648 | dyn_cast<BinaryOperator>(RHSExpr->IgnoreParenImpCasts())) { |
| 5649 | EExpr = BORHS->getRHS(); |
| 5650 | BO = BORHS->getOpcode(); |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5651 | } |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5652 | } |
| 5653 | if (XExpr) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5654 | const auto *VD = cast<VarDecl>(cast<DeclRefExpr>(*ILHS)->getDecl()); |
Malcolm Parsons | c6e4583 | 2017-01-13 18:55:32 +0000 | [diff] [blame] | 5655 | auto &&AtomicRedGen = [BO, VD, |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5656 | Loc](CodeGenFunction &CGF, const Expr *XExpr, |
| 5657 | const Expr *EExpr, const Expr *UpExpr) { |
| 5658 | LValue X = CGF.EmitLValue(XExpr); |
| 5659 | RValue E; |
| 5660 | if (EExpr) |
| 5661 | E = CGF.EmitAnyExpr(EExpr); |
| 5662 | CGF.EmitOMPAtomicSimpleUpdateExpr( |
JF Bastien | 92f4ef1 | 2016-04-06 17:26:42 +0000 | [diff] [blame] | 5663 | X, E, BO, /*IsXLHSInRHSPart=*/true, |
| 5664 | llvm::AtomicOrdering::Monotonic, Loc, |
Malcolm Parsons | c6e4583 | 2017-01-13 18:55:32 +0000 | [diff] [blame] | 5665 | [&CGF, UpExpr, VD, Loc](RValue XRValue) { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5666 | CodeGenFunction::OMPPrivateScope PrivateScope(CGF); |
| 5667 | PrivateScope.addPrivate( |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5668 | VD, [&CGF, VD, XRValue, Loc]() { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5669 | Address LHSTemp = CGF.CreateMemTemp(VD->getType()); |
| 5670 | CGF.emitOMPSimpleStore( |
| 5671 | CGF.MakeAddrLValue(LHSTemp, VD->getType()), XRValue, |
| 5672 | VD->getType().getNonReferenceType(), Loc); |
| 5673 | return LHSTemp; |
| 5674 | }); |
| 5675 | (void)PrivateScope.Privatize(); |
| 5676 | return CGF.EmitAnyExpr(UpExpr); |
| 5677 | }); |
| 5678 | }; |
| 5679 | if ((*IPriv)->getType()->isArrayType()) { |
| 5680 | // Emit atomic reduction for array section. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5681 | const auto *RHSVar = |
| 5682 | cast<VarDecl>(cast<DeclRefExpr>(*IRHS)->getDecl()); |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5683 | EmitOMPAggregateReduction(CGF, (*IPriv)->getType(), VD, RHSVar, |
| 5684 | AtomicRedGen, XExpr, EExpr, UpExpr); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5685 | } else { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5686 | // Emit atomic reduction for array subscript or single variable. |
| 5687 | AtomicRedGen(CGF, XExpr, EExpr, UpExpr); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5688 | } |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5689 | } else { |
| 5690 | // Emit as a critical region. |
| 5691 | auto &&CritRedGen = [E, Loc](CodeGenFunction &CGF, const Expr *, |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 5692 | const Expr *, const Expr *) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5693 | CGOpenMPRuntime &RT = CGF.CGM.getOpenMPRuntime(); |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 5694 | std::string Name = RT.getName({"atomic_reduction"}); |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5695 | RT.emitCriticalRegion( |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 5696 | CGF, Name, |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5697 | [=](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 5698 | Action.Enter(CGF); |
| 5699 | emitReductionCombiner(CGF, E); |
| 5700 | }, |
| 5701 | Loc); |
| 5702 | }; |
| 5703 | if ((*IPriv)->getType()->isArrayType()) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5704 | const auto *LHSVar = |
| 5705 | cast<VarDecl>(cast<DeclRefExpr>(*ILHS)->getDecl()); |
| 5706 | const auto *RHSVar = |
| 5707 | cast<VarDecl>(cast<DeclRefExpr>(*IRHS)->getDecl()); |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5708 | EmitOMPAggregateReduction(CGF, (*IPriv)->getType(), LHSVar, RHSVar, |
| 5709 | CritRedGen); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5710 | } else { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5711 | CritRedGen(CGF, nullptr, nullptr, nullptr); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5712 | } |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5713 | } |
Richard Trieu | cc3949d | 2016-02-18 22:34:54 +0000 | [diff] [blame] | 5714 | ++ILHS; |
| 5715 | ++IRHS; |
| 5716 | ++IPriv; |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5717 | } |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5718 | }; |
| 5719 | RegionCodeGenTy AtomicRCG(AtomicCodeGen); |
| 5720 | if (!WithNowait) { |
| 5721 | // Add emission of __kmpc_end_reduce(<loc>, <gtid>, &<lock>); |
| 5722 | llvm::Value *EndArgs[] = { |
| 5723 | IdentTLoc, // ident_t *<loc> |
| 5724 | ThreadId, // i32 <gtid> |
| 5725 | Lock // kmp_critical_name *&<lock> |
| 5726 | }; |
| 5727 | CommonActionTy Action(nullptr, llvm::None, |
| 5728 | createRuntimeFunction(OMPRTL__kmpc_end_reduce), |
| 5729 | EndArgs); |
| 5730 | AtomicRCG.setAction(Action); |
| 5731 | AtomicRCG(CGF); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5732 | } else { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5733 | AtomicRCG(CGF); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5734 | } |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5735 | |
| 5736 | CGF.EmitBranch(DefaultBB); |
| 5737 | CGF.EmitBlock(DefaultBB, /*IsFinished=*/true); |
| 5738 | } |
| 5739 | |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 5740 | /// Generates unique name for artificial threadprivate variables. |
Alexey Bataev | 1c44e15 | 2018-03-06 18:59:43 +0000 | [diff] [blame] | 5741 | /// Format is: <Prefix> "." <Decl_mangled_name> "_" "<Decl_start_loc_raw_enc>" |
| 5742 | static std::string generateUniqueName(CodeGenModule &CGM, StringRef Prefix, |
| 5743 | const Expr *Ref) { |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 5744 | SmallString<256> Buffer; |
| 5745 | llvm::raw_svector_ostream Out(Buffer); |
Alexey Bataev | 1c44e15 | 2018-03-06 18:59:43 +0000 | [diff] [blame] | 5746 | const clang::DeclRefExpr *DE; |
| 5747 | const VarDecl *D = ::getBaseDecl(Ref, DE); |
| 5748 | if (!D) |
| 5749 | D = cast<VarDecl>(cast<DeclRefExpr>(Ref)->getDecl()); |
| 5750 | D = D->getCanonicalDecl(); |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 5751 | std::string Name = CGM.getOpenMPRuntime().getName( |
| 5752 | {D->isLocalVarDeclOrParm() ? D->getName() : CGM.getMangledName(D)}); |
| 5753 | Out << Prefix << Name << "_" |
| 5754 | << D->getCanonicalDecl()->getLocStart().getRawEncoding(); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 5755 | return Out.str(); |
| 5756 | } |
| 5757 | |
| 5758 | /// Emits reduction initializer function: |
| 5759 | /// \code |
| 5760 | /// void @.red_init(void* %arg) { |
| 5761 | /// %0 = bitcast void* %arg to <type>* |
| 5762 | /// store <type> <init>, <type>* %0 |
| 5763 | /// ret void |
| 5764 | /// } |
| 5765 | /// \endcode |
| 5766 | static llvm::Value *emitReduceInitFunction(CodeGenModule &CGM, |
| 5767 | SourceLocation Loc, |
| 5768 | ReductionCodeGen &RCG, unsigned N) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5769 | ASTContext &C = CGM.getContext(); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 5770 | FunctionArgList Args; |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 5771 | ImplicitParamDecl Param(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, C.VoidPtrTy, |
| 5772 | ImplicitParamDecl::Other); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 5773 | Args.emplace_back(&Param); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5774 | const auto &FnInfo = |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 5775 | CGM.getTypes().arrangeBuiltinFunctionDeclaration(C.VoidTy, Args); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5776 | llvm::FunctionType *FnTy = CGM.getTypes().GetFunctionType(FnInfo); |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 5777 | std::string Name = CGM.getOpenMPRuntime().getName({"red_init", ""}); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 5778 | auto *Fn = llvm::Function::Create(FnTy, llvm::GlobalValue::InternalLinkage, |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 5779 | Name, &CGM.getModule()); |
Rafael Espindola | 51ec5a9 | 2018-02-28 23:46:35 +0000 | [diff] [blame] | 5780 | CGM.SetInternalFunctionAttributes(GlobalDecl(), Fn, FnInfo); |
Alexey Bataev | c0f879b | 2018-04-10 20:10:53 +0000 | [diff] [blame] | 5781 | Fn->setDoesNotRecurse(); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 5782 | CodeGenFunction CGF(CGM); |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 5783 | CGF.StartFunction(GlobalDecl(), C.VoidTy, Fn, FnInfo, Args, Loc, Loc); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 5784 | Address PrivateAddr = CGF.EmitLoadOfPointer( |
| 5785 | CGF.GetAddrOfLocalVar(&Param), |
| 5786 | C.getPointerType(C.VoidPtrTy).castAs<PointerType>()); |
| 5787 | llvm::Value *Size = nullptr; |
| 5788 | // If the size of the reduction item is non-constant, load it from global |
| 5789 | // threadprivate variable. |
| 5790 | if (RCG.getSizes(N).second) { |
| 5791 | Address SizeAddr = CGM.getOpenMPRuntime().getAddrOfArtificialThreadPrivate( |
| 5792 | CGF, CGM.getContext().getSizeType(), |
Alexey Bataev | 1c44e15 | 2018-03-06 18:59:43 +0000 | [diff] [blame] | 5793 | generateUniqueName(CGM, "reduction_size", RCG.getRefExpr(N))); |
Alexey Bataev | a9b9cc0 | 2018-01-23 18:12:38 +0000 | [diff] [blame] | 5794 | Size = CGF.EmitLoadOfScalar(SizeAddr, /*Volatile=*/false, |
| 5795 | CGM.getContext().getSizeType(), Loc); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 5796 | } |
| 5797 | RCG.emitAggregateType(CGF, N, Size); |
| 5798 | LValue SharedLVal; |
| 5799 | // If initializer uses initializer from declare reduction construct, emit a |
| 5800 | // pointer to the address of the original reduction item (reuired by reduction |
| 5801 | // initializer) |
| 5802 | if (RCG.usesReductionInitializer(N)) { |
| 5803 | Address SharedAddr = |
| 5804 | CGM.getOpenMPRuntime().getAddrOfArtificialThreadPrivate( |
| 5805 | CGF, CGM.getContext().VoidPtrTy, |
Alexey Bataev | 1c44e15 | 2018-03-06 18:59:43 +0000 | [diff] [blame] | 5806 | generateUniqueName(CGM, "reduction", RCG.getRefExpr(N))); |
Alexey Bataev | 21dab12 | 2018-03-09 15:20:30 +0000 | [diff] [blame] | 5807 | SharedAddr = CGF.EmitLoadOfPointer( |
| 5808 | SharedAddr, |
| 5809 | CGM.getContext().VoidPtrTy.castAs<PointerType>()->getTypePtr()); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 5810 | SharedLVal = CGF.MakeAddrLValue(SharedAddr, CGM.getContext().VoidPtrTy); |
| 5811 | } else { |
| 5812 | SharedLVal = CGF.MakeNaturalAlignAddrLValue( |
| 5813 | llvm::ConstantPointerNull::get(CGM.VoidPtrTy), |
| 5814 | CGM.getContext().VoidPtrTy); |
| 5815 | } |
| 5816 | // Emit the initializer: |
| 5817 | // %0 = bitcast void* %arg to <type>* |
| 5818 | // store <type> <init>, <type>* %0 |
| 5819 | RCG.emitInitialization(CGF, N, PrivateAddr, SharedLVal, |
| 5820 | [](CodeGenFunction &) { return false; }); |
| 5821 | CGF.FinishFunction(); |
| 5822 | return Fn; |
| 5823 | } |
| 5824 | |
| 5825 | /// Emits reduction combiner function: |
| 5826 | /// \code |
| 5827 | /// void @.red_comb(void* %arg0, void* %arg1) { |
| 5828 | /// %lhs = bitcast void* %arg0 to <type>* |
| 5829 | /// %rhs = bitcast void* %arg1 to <type>* |
| 5830 | /// %2 = <ReductionOp>(<type>* %lhs, <type>* %rhs) |
| 5831 | /// store <type> %2, <type>* %lhs |
| 5832 | /// ret void |
| 5833 | /// } |
| 5834 | /// \endcode |
| 5835 | static llvm::Value *emitReduceCombFunction(CodeGenModule &CGM, |
| 5836 | SourceLocation Loc, |
| 5837 | ReductionCodeGen &RCG, unsigned N, |
| 5838 | const Expr *ReductionOp, |
| 5839 | const Expr *LHS, const Expr *RHS, |
| 5840 | const Expr *PrivateRef) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5841 | ASTContext &C = CGM.getContext(); |
| 5842 | const auto *LHSVD = cast<VarDecl>(cast<DeclRefExpr>(LHS)->getDecl()); |
| 5843 | const auto *RHSVD = cast<VarDecl>(cast<DeclRefExpr>(RHS)->getDecl()); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 5844 | FunctionArgList Args; |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 5845 | ImplicitParamDecl ParamInOut(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, |
| 5846 | C.VoidPtrTy, ImplicitParamDecl::Other); |
| 5847 | ImplicitParamDecl ParamIn(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, C.VoidPtrTy, |
| 5848 | ImplicitParamDecl::Other); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 5849 | Args.emplace_back(&ParamInOut); |
| 5850 | Args.emplace_back(&ParamIn); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5851 | const auto &FnInfo = |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 5852 | CGM.getTypes().arrangeBuiltinFunctionDeclaration(C.VoidTy, Args); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5853 | llvm::FunctionType *FnTy = CGM.getTypes().GetFunctionType(FnInfo); |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 5854 | std::string Name = CGM.getOpenMPRuntime().getName({"red_comb", ""}); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 5855 | auto *Fn = llvm::Function::Create(FnTy, llvm::GlobalValue::InternalLinkage, |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 5856 | Name, &CGM.getModule()); |
Rafael Espindola | 51ec5a9 | 2018-02-28 23:46:35 +0000 | [diff] [blame] | 5857 | CGM.SetInternalFunctionAttributes(GlobalDecl(), Fn, FnInfo); |
Alexey Bataev | c0f879b | 2018-04-10 20:10:53 +0000 | [diff] [blame] | 5858 | Fn->setDoesNotRecurse(); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 5859 | CodeGenFunction CGF(CGM); |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 5860 | CGF.StartFunction(GlobalDecl(), C.VoidTy, Fn, FnInfo, Args, Loc, Loc); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 5861 | llvm::Value *Size = nullptr; |
| 5862 | // If the size of the reduction item is non-constant, load it from global |
| 5863 | // threadprivate variable. |
| 5864 | if (RCG.getSizes(N).second) { |
| 5865 | Address SizeAddr = CGM.getOpenMPRuntime().getAddrOfArtificialThreadPrivate( |
| 5866 | CGF, CGM.getContext().getSizeType(), |
Alexey Bataev | 1c44e15 | 2018-03-06 18:59:43 +0000 | [diff] [blame] | 5867 | generateUniqueName(CGM, "reduction_size", RCG.getRefExpr(N))); |
Alexey Bataev | a9b9cc0 | 2018-01-23 18:12:38 +0000 | [diff] [blame] | 5868 | Size = CGF.EmitLoadOfScalar(SizeAddr, /*Volatile=*/false, |
| 5869 | CGM.getContext().getSizeType(), Loc); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 5870 | } |
| 5871 | RCG.emitAggregateType(CGF, N, Size); |
| 5872 | // Remap lhs and rhs variables to the addresses of the function arguments. |
| 5873 | // %lhs = bitcast void* %arg0 to <type>* |
| 5874 | // %rhs = bitcast void* %arg1 to <type>* |
| 5875 | CodeGenFunction::OMPPrivateScope PrivateScope(CGF); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5876 | PrivateScope.addPrivate(LHSVD, [&C, &CGF, &ParamInOut, LHSVD]() { |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 5877 | // Pull out the pointer to the variable. |
| 5878 | Address PtrAddr = CGF.EmitLoadOfPointer( |
| 5879 | CGF.GetAddrOfLocalVar(&ParamInOut), |
| 5880 | C.getPointerType(C.VoidPtrTy).castAs<PointerType>()); |
| 5881 | return CGF.Builder.CreateElementBitCast( |
| 5882 | PtrAddr, CGF.ConvertTypeForMem(LHSVD->getType())); |
| 5883 | }); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5884 | PrivateScope.addPrivate(RHSVD, [&C, &CGF, &ParamIn, RHSVD]() { |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 5885 | // Pull out the pointer to the variable. |
| 5886 | Address PtrAddr = CGF.EmitLoadOfPointer( |
| 5887 | CGF.GetAddrOfLocalVar(&ParamIn), |
| 5888 | C.getPointerType(C.VoidPtrTy).castAs<PointerType>()); |
| 5889 | return CGF.Builder.CreateElementBitCast( |
| 5890 | PtrAddr, CGF.ConvertTypeForMem(RHSVD->getType())); |
| 5891 | }); |
| 5892 | PrivateScope.Privatize(); |
| 5893 | // Emit the combiner body: |
| 5894 | // %2 = <ReductionOp>(<type> *%lhs, <type> *%rhs) |
| 5895 | // store <type> %2, <type>* %lhs |
| 5896 | CGM.getOpenMPRuntime().emitSingleReductionCombiner( |
| 5897 | CGF, ReductionOp, PrivateRef, cast<DeclRefExpr>(LHS), |
| 5898 | cast<DeclRefExpr>(RHS)); |
| 5899 | CGF.FinishFunction(); |
| 5900 | return Fn; |
| 5901 | } |
| 5902 | |
| 5903 | /// Emits reduction finalizer function: |
| 5904 | /// \code |
| 5905 | /// void @.red_fini(void* %arg) { |
| 5906 | /// %0 = bitcast void* %arg to <type>* |
| 5907 | /// <destroy>(<type>* %0) |
| 5908 | /// ret void |
| 5909 | /// } |
| 5910 | /// \endcode |
| 5911 | static llvm::Value *emitReduceFiniFunction(CodeGenModule &CGM, |
| 5912 | SourceLocation Loc, |
| 5913 | ReductionCodeGen &RCG, unsigned N) { |
| 5914 | if (!RCG.needCleanups(N)) |
| 5915 | return nullptr; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5916 | ASTContext &C = CGM.getContext(); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 5917 | FunctionArgList Args; |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 5918 | ImplicitParamDecl Param(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, C.VoidPtrTy, |
| 5919 | ImplicitParamDecl::Other); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 5920 | Args.emplace_back(&Param); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5921 | const auto &FnInfo = |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 5922 | CGM.getTypes().arrangeBuiltinFunctionDeclaration(C.VoidTy, Args); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5923 | llvm::FunctionType *FnTy = CGM.getTypes().GetFunctionType(FnInfo); |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 5924 | std::string Name = CGM.getOpenMPRuntime().getName({"red_fini", ""}); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 5925 | auto *Fn = llvm::Function::Create(FnTy, llvm::GlobalValue::InternalLinkage, |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 5926 | Name, &CGM.getModule()); |
Rafael Espindola | 51ec5a9 | 2018-02-28 23:46:35 +0000 | [diff] [blame] | 5927 | CGM.SetInternalFunctionAttributes(GlobalDecl(), Fn, FnInfo); |
Alexey Bataev | c0f879b | 2018-04-10 20:10:53 +0000 | [diff] [blame] | 5928 | Fn->setDoesNotRecurse(); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 5929 | CodeGenFunction CGF(CGM); |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 5930 | CGF.StartFunction(GlobalDecl(), C.VoidTy, Fn, FnInfo, Args, Loc, Loc); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 5931 | Address PrivateAddr = CGF.EmitLoadOfPointer( |
| 5932 | CGF.GetAddrOfLocalVar(&Param), |
| 5933 | C.getPointerType(C.VoidPtrTy).castAs<PointerType>()); |
| 5934 | llvm::Value *Size = nullptr; |
| 5935 | // If the size of the reduction item is non-constant, load it from global |
| 5936 | // threadprivate variable. |
| 5937 | if (RCG.getSizes(N).second) { |
| 5938 | Address SizeAddr = CGM.getOpenMPRuntime().getAddrOfArtificialThreadPrivate( |
| 5939 | CGF, CGM.getContext().getSizeType(), |
Alexey Bataev | 1c44e15 | 2018-03-06 18:59:43 +0000 | [diff] [blame] | 5940 | generateUniqueName(CGM, "reduction_size", RCG.getRefExpr(N))); |
Alexey Bataev | a9b9cc0 | 2018-01-23 18:12:38 +0000 | [diff] [blame] | 5941 | Size = CGF.EmitLoadOfScalar(SizeAddr, /*Volatile=*/false, |
| 5942 | CGM.getContext().getSizeType(), Loc); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 5943 | } |
| 5944 | RCG.emitAggregateType(CGF, N, Size); |
| 5945 | // Emit the finalizer body: |
| 5946 | // <destroy>(<type>* %0) |
| 5947 | RCG.emitCleanups(CGF, N, PrivateAddr); |
| 5948 | CGF.FinishFunction(); |
| 5949 | return Fn; |
| 5950 | } |
| 5951 | |
| 5952 | llvm::Value *CGOpenMPRuntime::emitTaskReductionInit( |
| 5953 | CodeGenFunction &CGF, SourceLocation Loc, ArrayRef<const Expr *> LHSExprs, |
| 5954 | ArrayRef<const Expr *> RHSExprs, const OMPTaskDataTy &Data) { |
| 5955 | if (!CGF.HaveInsertPoint() || Data.ReductionVars.empty()) |
| 5956 | return nullptr; |
| 5957 | |
| 5958 | // Build typedef struct: |
| 5959 | // kmp_task_red_input { |
| 5960 | // void *reduce_shar; // shared reduction item |
| 5961 | // size_t reduce_size; // size of data item |
| 5962 | // void *reduce_init; // data initialization routine |
| 5963 | // void *reduce_fini; // data finalization routine |
| 5964 | // void *reduce_comb; // data combiner routine |
| 5965 | // kmp_task_red_flags_t flags; // flags for additional info from compiler |
| 5966 | // } kmp_task_red_input_t; |
| 5967 | ASTContext &C = CGM.getContext(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5968 | RecordDecl *RD = C.buildImplicitRecord("kmp_task_red_input_t"); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 5969 | RD->startDefinition(); |
| 5970 | const FieldDecl *SharedFD = addFieldToRecordDecl(C, RD, C.VoidPtrTy); |
| 5971 | const FieldDecl *SizeFD = addFieldToRecordDecl(C, RD, C.getSizeType()); |
| 5972 | const FieldDecl *InitFD = addFieldToRecordDecl(C, RD, C.VoidPtrTy); |
| 5973 | const FieldDecl *FiniFD = addFieldToRecordDecl(C, RD, C.VoidPtrTy); |
| 5974 | const FieldDecl *CombFD = addFieldToRecordDecl(C, RD, C.VoidPtrTy); |
| 5975 | const FieldDecl *FlagsFD = addFieldToRecordDecl( |
| 5976 | C, RD, C.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/false)); |
| 5977 | RD->completeDefinition(); |
| 5978 | QualType RDType = C.getRecordType(RD); |
| 5979 | unsigned Size = Data.ReductionVars.size(); |
| 5980 | llvm::APInt ArraySize(/*numBits=*/64, Size); |
| 5981 | QualType ArrayRDType = C.getConstantArrayType( |
| 5982 | RDType, ArraySize, ArrayType::Normal, /*IndexTypeQuals=*/0); |
| 5983 | // kmp_task_red_input_t .rd_input.[Size]; |
| 5984 | Address TaskRedInput = CGF.CreateMemTemp(ArrayRDType, ".rd_input."); |
| 5985 | ReductionCodeGen RCG(Data.ReductionVars, Data.ReductionCopies, |
| 5986 | Data.ReductionOps); |
| 5987 | for (unsigned Cnt = 0; Cnt < Size; ++Cnt) { |
| 5988 | // kmp_task_red_input_t &ElemLVal = .rd_input.[Cnt]; |
| 5989 | llvm::Value *Idxs[] = {llvm::ConstantInt::get(CGM.SizeTy, /*V=*/0), |
| 5990 | llvm::ConstantInt::get(CGM.SizeTy, Cnt)}; |
| 5991 | llvm::Value *GEP = CGF.EmitCheckedInBoundsGEP( |
| 5992 | TaskRedInput.getPointer(), Idxs, |
| 5993 | /*SignedIndices=*/false, /*IsSubtraction=*/false, Loc, |
| 5994 | ".rd_input.gep."); |
| 5995 | LValue ElemLVal = CGF.MakeNaturalAlignAddrLValue(GEP, RDType); |
| 5996 | // ElemLVal.reduce_shar = &Shareds[Cnt]; |
| 5997 | LValue SharedLVal = CGF.EmitLValueForField(ElemLVal, SharedFD); |
| 5998 | RCG.emitSharedLValue(CGF, Cnt); |
| 5999 | llvm::Value *CastedShared = |
| 6000 | CGF.EmitCastToVoidPtr(RCG.getSharedLValue(Cnt).getPointer()); |
| 6001 | CGF.EmitStoreOfScalar(CastedShared, SharedLVal); |
| 6002 | RCG.emitAggregateType(CGF, Cnt); |
| 6003 | llvm::Value *SizeValInChars; |
| 6004 | llvm::Value *SizeVal; |
| 6005 | std::tie(SizeValInChars, SizeVal) = RCG.getSizes(Cnt); |
| 6006 | // We use delayed creation/initialization for VLAs, array sections and |
| 6007 | // custom reduction initializations. It is required because runtime does not |
| 6008 | // provide the way to pass the sizes of VLAs/array sections to |
| 6009 | // initializer/combiner/finalizer functions and does not pass the pointer to |
| 6010 | // original reduction item to the initializer. Instead threadprivate global |
| 6011 | // variables are used to store these values and use them in the functions. |
| 6012 | bool DelayedCreation = !!SizeVal; |
| 6013 | SizeValInChars = CGF.Builder.CreateIntCast(SizeValInChars, CGM.SizeTy, |
| 6014 | /*isSigned=*/false); |
| 6015 | LValue SizeLVal = CGF.EmitLValueForField(ElemLVal, SizeFD); |
| 6016 | CGF.EmitStoreOfScalar(SizeValInChars, SizeLVal); |
| 6017 | // ElemLVal.reduce_init = init; |
| 6018 | LValue InitLVal = CGF.EmitLValueForField(ElemLVal, InitFD); |
| 6019 | llvm::Value *InitAddr = |
| 6020 | CGF.EmitCastToVoidPtr(emitReduceInitFunction(CGM, Loc, RCG, Cnt)); |
| 6021 | CGF.EmitStoreOfScalar(InitAddr, InitLVal); |
| 6022 | DelayedCreation = DelayedCreation || RCG.usesReductionInitializer(Cnt); |
| 6023 | // ElemLVal.reduce_fini = fini; |
| 6024 | LValue FiniLVal = CGF.EmitLValueForField(ElemLVal, FiniFD); |
| 6025 | llvm::Value *Fini = emitReduceFiniFunction(CGM, Loc, RCG, Cnt); |
| 6026 | llvm::Value *FiniAddr = Fini |
| 6027 | ? CGF.EmitCastToVoidPtr(Fini) |
| 6028 | : llvm::ConstantPointerNull::get(CGM.VoidPtrTy); |
| 6029 | CGF.EmitStoreOfScalar(FiniAddr, FiniLVal); |
| 6030 | // ElemLVal.reduce_comb = comb; |
| 6031 | LValue CombLVal = CGF.EmitLValueForField(ElemLVal, CombFD); |
| 6032 | llvm::Value *CombAddr = CGF.EmitCastToVoidPtr(emitReduceCombFunction( |
| 6033 | CGM, Loc, RCG, Cnt, Data.ReductionOps[Cnt], LHSExprs[Cnt], |
| 6034 | RHSExprs[Cnt], Data.ReductionCopies[Cnt])); |
| 6035 | CGF.EmitStoreOfScalar(CombAddr, CombLVal); |
| 6036 | // ElemLVal.flags = 0; |
| 6037 | LValue FlagsLVal = CGF.EmitLValueForField(ElemLVal, FlagsFD); |
| 6038 | if (DelayedCreation) { |
| 6039 | CGF.EmitStoreOfScalar( |
| 6040 | llvm::ConstantInt::get(CGM.Int32Ty, /*V=*/1, /*IsSigned=*/true), |
| 6041 | FlagsLVal); |
| 6042 | } else |
| 6043 | CGF.EmitNullInitialization(FlagsLVal.getAddress(), FlagsLVal.getType()); |
| 6044 | } |
| 6045 | // Build call void *__kmpc_task_reduction_init(int gtid, int num_data, void |
| 6046 | // *data); |
| 6047 | llvm::Value *Args[] = { |
| 6048 | CGF.Builder.CreateIntCast(getThreadID(CGF, Loc), CGM.IntTy, |
| 6049 | /*isSigned=*/true), |
| 6050 | llvm::ConstantInt::get(CGM.IntTy, Size, /*isSigned=*/true), |
| 6051 | CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(TaskRedInput.getPointer(), |
| 6052 | CGM.VoidPtrTy)}; |
| 6053 | return CGF.EmitRuntimeCall( |
| 6054 | createRuntimeFunction(OMPRTL__kmpc_task_reduction_init), Args); |
| 6055 | } |
| 6056 | |
| 6057 | void CGOpenMPRuntime::emitTaskReductionFixups(CodeGenFunction &CGF, |
| 6058 | SourceLocation Loc, |
| 6059 | ReductionCodeGen &RCG, |
| 6060 | unsigned N) { |
| 6061 | auto Sizes = RCG.getSizes(N); |
| 6062 | // Emit threadprivate global variable if the type is non-constant |
| 6063 | // (Sizes.second = nullptr). |
| 6064 | if (Sizes.second) { |
| 6065 | llvm::Value *SizeVal = CGF.Builder.CreateIntCast(Sizes.second, CGM.SizeTy, |
| 6066 | /*isSigned=*/false); |
| 6067 | Address SizeAddr = getAddrOfArtificialThreadPrivate( |
| 6068 | CGF, CGM.getContext().getSizeType(), |
Alexey Bataev | 1c44e15 | 2018-03-06 18:59:43 +0000 | [diff] [blame] | 6069 | generateUniqueName(CGM, "reduction_size", RCG.getRefExpr(N))); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 6070 | CGF.Builder.CreateStore(SizeVal, SizeAddr, /*IsVolatile=*/false); |
| 6071 | } |
| 6072 | // Store address of the original reduction item if custom initializer is used. |
| 6073 | if (RCG.usesReductionInitializer(N)) { |
| 6074 | Address SharedAddr = getAddrOfArtificialThreadPrivate( |
| 6075 | CGF, CGM.getContext().VoidPtrTy, |
Alexey Bataev | 1c44e15 | 2018-03-06 18:59:43 +0000 | [diff] [blame] | 6076 | generateUniqueName(CGM, "reduction", RCG.getRefExpr(N))); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 6077 | CGF.Builder.CreateStore( |
| 6078 | CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
| 6079 | RCG.getSharedLValue(N).getPointer(), CGM.VoidPtrTy), |
| 6080 | SharedAddr, /*IsVolatile=*/false); |
| 6081 | } |
| 6082 | } |
| 6083 | |
| 6084 | Address CGOpenMPRuntime::getTaskReductionItem(CodeGenFunction &CGF, |
| 6085 | SourceLocation Loc, |
| 6086 | llvm::Value *ReductionsPtr, |
| 6087 | LValue SharedLVal) { |
| 6088 | // Build call void *__kmpc_task_reduction_get_th_data(int gtid, void *tg, void |
| 6089 | // *d); |
| 6090 | llvm::Value *Args[] = { |
| 6091 | CGF.Builder.CreateIntCast(getThreadID(CGF, Loc), CGM.IntTy, |
| 6092 | /*isSigned=*/true), |
| 6093 | ReductionsPtr, |
| 6094 | CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(SharedLVal.getPointer(), |
| 6095 | CGM.VoidPtrTy)}; |
| 6096 | return Address( |
| 6097 | CGF.EmitRuntimeCall( |
| 6098 | createRuntimeFunction(OMPRTL__kmpc_task_reduction_get_th_data), Args), |
| 6099 | SharedLVal.getAlignment()); |
| 6100 | } |
| 6101 | |
Alexey Bataev | 8b8e202 | 2015-04-27 05:22:09 +0000 | [diff] [blame] | 6102 | void CGOpenMPRuntime::emitTaskwaitCall(CodeGenFunction &CGF, |
| 6103 | SourceLocation Loc) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 6104 | if (!CGF.HaveInsertPoint()) |
| 6105 | return; |
Alexey Bataev | 8b8e202 | 2015-04-27 05:22:09 +0000 | [diff] [blame] | 6106 | // Build call kmp_int32 __kmpc_omp_taskwait(ident_t *loc, kmp_int32 |
| 6107 | // global_tid); |
| 6108 | llvm::Value *Args[] = {emitUpdateLocation(CGF, Loc), getThreadID(CGF, Loc)}; |
| 6109 | // Ignore return result until untied tasks are supported. |
| 6110 | CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_omp_taskwait), Args); |
Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 6111 | if (auto *Region = dyn_cast_or_null<CGOpenMPRegionInfo>(CGF.CapturedStmtInfo)) |
| 6112 | Region->emitUntiedSwitch(CGF); |
Alexey Bataev | 8b8e202 | 2015-04-27 05:22:09 +0000 | [diff] [blame] | 6113 | } |
| 6114 | |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 6115 | void CGOpenMPRuntime::emitInlinedDirective(CodeGenFunction &CGF, |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 6116 | OpenMPDirectiveKind InnerKind, |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 6117 | const RegionCodeGenTy &CodeGen, |
| 6118 | bool HasCancel) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 6119 | if (!CGF.HaveInsertPoint()) |
| 6120 | return; |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 6121 | InlinedOpenMPRegionRAII Region(CGF, CodeGen, InnerKind, HasCancel); |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 6122 | CGF.CapturedStmtInfo->EmitBody(CGF, /*S=*/nullptr); |
Alexey Bataev | 8cbe0a6 | 2015-02-26 10:27:34 +0000 | [diff] [blame] | 6123 | } |
| 6124 | |
Alexey Bataev | 7d5d33e | 2015-07-06 05:50:32 +0000 | [diff] [blame] | 6125 | namespace { |
| 6126 | enum RTCancelKind { |
| 6127 | CancelNoreq = 0, |
| 6128 | CancelParallel = 1, |
| 6129 | CancelLoop = 2, |
| 6130 | CancelSections = 3, |
| 6131 | CancelTaskgroup = 4 |
| 6132 | }; |
Eugene Zelenko | 0a4f3f4 | 2016-02-10 19:11:58 +0000 | [diff] [blame] | 6133 | } // anonymous namespace |
Alexey Bataev | 7d5d33e | 2015-07-06 05:50:32 +0000 | [diff] [blame] | 6134 | |
| 6135 | static RTCancelKind getCancellationKind(OpenMPDirectiveKind CancelRegion) { |
| 6136 | RTCancelKind CancelKind = CancelNoreq; |
Alexey Bataev | 0f34da1 | 2015-07-02 04:17:07 +0000 | [diff] [blame] | 6137 | if (CancelRegion == OMPD_parallel) |
| 6138 | CancelKind = CancelParallel; |
| 6139 | else if (CancelRegion == OMPD_for) |
| 6140 | CancelKind = CancelLoop; |
| 6141 | else if (CancelRegion == OMPD_sections) |
| 6142 | CancelKind = CancelSections; |
| 6143 | else { |
| 6144 | assert(CancelRegion == OMPD_taskgroup); |
| 6145 | CancelKind = CancelTaskgroup; |
| 6146 | } |
Alexey Bataev | 7d5d33e | 2015-07-06 05:50:32 +0000 | [diff] [blame] | 6147 | return CancelKind; |
| 6148 | } |
| 6149 | |
| 6150 | void CGOpenMPRuntime::emitCancellationPointCall( |
| 6151 | CodeGenFunction &CGF, SourceLocation Loc, |
| 6152 | OpenMPDirectiveKind CancelRegion) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 6153 | if (!CGF.HaveInsertPoint()) |
| 6154 | return; |
Alexey Bataev | 7d5d33e | 2015-07-06 05:50:32 +0000 | [diff] [blame] | 6155 | // Build call kmp_int32 __kmpc_cancellationpoint(ident_t *loc, kmp_int32 |
| 6156 | // global_tid, kmp_int32 cncl_kind); |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 6157 | if (auto *OMPRegionInfo = |
| 6158 | dyn_cast_or_null<CGOpenMPRegionInfo>(CGF.CapturedStmtInfo)) { |
Jonas Hahnfeld | b07931f | 2017-02-17 18:32:58 +0000 | [diff] [blame] | 6159 | // For 'cancellation point taskgroup', the task region info may not have a |
| 6160 | // cancel. This may instead happen in another adjacent task. |
| 6161 | if (CancelRegion == OMPD_taskgroup || OMPRegionInfo->hasCancel()) { |
Alexey Bataev | 7d5d33e | 2015-07-06 05:50:32 +0000 | [diff] [blame] | 6162 | llvm::Value *Args[] = { |
| 6163 | emitUpdateLocation(CGF, Loc), getThreadID(CGF, Loc), |
| 6164 | CGF.Builder.getInt32(getCancellationKind(CancelRegion))}; |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 6165 | // Ignore return result until untied tasks are supported. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6166 | llvm::Value *Result = CGF.EmitRuntimeCall( |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 6167 | createRuntimeFunction(OMPRTL__kmpc_cancellationpoint), Args); |
| 6168 | // if (__kmpc_cancellationpoint()) { |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 6169 | // exit from construct; |
| 6170 | // } |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6171 | llvm::BasicBlock *ExitBB = CGF.createBasicBlock(".cancel.exit"); |
| 6172 | llvm::BasicBlock *ContBB = CGF.createBasicBlock(".cancel.continue"); |
| 6173 | llvm::Value *Cmp = CGF.Builder.CreateIsNotNull(Result); |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 6174 | CGF.Builder.CreateCondBr(Cmp, ExitBB, ContBB); |
| 6175 | CGF.EmitBlock(ExitBB); |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 6176 | // exit from construct; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6177 | CodeGenFunction::JumpDest CancelDest = |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 6178 | CGF.getOMPCancelDestination(OMPRegionInfo->getDirectiveKind()); |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 6179 | CGF.EmitBranchThroughCleanup(CancelDest); |
| 6180 | CGF.EmitBlock(ContBB, /*IsFinished=*/true); |
| 6181 | } |
Alexey Bataev | 0f34da1 | 2015-07-02 04:17:07 +0000 | [diff] [blame] | 6182 | } |
Alexey Bataev | 0f34da1 | 2015-07-02 04:17:07 +0000 | [diff] [blame] | 6183 | } |
| 6184 | |
Alexey Bataev | 7d5d33e | 2015-07-06 05:50:32 +0000 | [diff] [blame] | 6185 | void CGOpenMPRuntime::emitCancelCall(CodeGenFunction &CGF, SourceLocation Loc, |
Alexey Bataev | 87933c7 | 2015-09-18 08:07:34 +0000 | [diff] [blame] | 6186 | const Expr *IfCond, |
Alexey Bataev | 7d5d33e | 2015-07-06 05:50:32 +0000 | [diff] [blame] | 6187 | OpenMPDirectiveKind CancelRegion) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 6188 | if (!CGF.HaveInsertPoint()) |
| 6189 | return; |
Alexey Bataev | 7d5d33e | 2015-07-06 05:50:32 +0000 | [diff] [blame] | 6190 | // Build call kmp_int32 __kmpc_cancel(ident_t *loc, kmp_int32 global_tid, |
| 6191 | // kmp_int32 cncl_kind); |
| 6192 | if (auto *OMPRegionInfo = |
| 6193 | dyn_cast_or_null<CGOpenMPRegionInfo>(CGF.CapturedStmtInfo)) { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 6194 | auto &&ThenGen = [Loc, CancelRegion, OMPRegionInfo](CodeGenFunction &CGF, |
| 6195 | PrePostActionTy &) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6196 | CGOpenMPRuntime &RT = CGF.CGM.getOpenMPRuntime(); |
Alexey Bataev | 87933c7 | 2015-09-18 08:07:34 +0000 | [diff] [blame] | 6197 | llvm::Value *Args[] = { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 6198 | RT.emitUpdateLocation(CGF, Loc), RT.getThreadID(CGF, Loc), |
Alexey Bataev | 87933c7 | 2015-09-18 08:07:34 +0000 | [diff] [blame] | 6199 | CGF.Builder.getInt32(getCancellationKind(CancelRegion))}; |
| 6200 | // Ignore return result until untied tasks are supported. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6201 | llvm::Value *Result = CGF.EmitRuntimeCall( |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 6202 | RT.createRuntimeFunction(OMPRTL__kmpc_cancel), Args); |
Alexey Bataev | 87933c7 | 2015-09-18 08:07:34 +0000 | [diff] [blame] | 6203 | // if (__kmpc_cancel()) { |
Alexey Bataev | 87933c7 | 2015-09-18 08:07:34 +0000 | [diff] [blame] | 6204 | // exit from construct; |
| 6205 | // } |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6206 | llvm::BasicBlock *ExitBB = CGF.createBasicBlock(".cancel.exit"); |
| 6207 | llvm::BasicBlock *ContBB = CGF.createBasicBlock(".cancel.continue"); |
| 6208 | llvm::Value *Cmp = CGF.Builder.CreateIsNotNull(Result); |
Alexey Bataev | 87933c7 | 2015-09-18 08:07:34 +0000 | [diff] [blame] | 6209 | CGF.Builder.CreateCondBr(Cmp, ExitBB, ContBB); |
| 6210 | CGF.EmitBlock(ExitBB); |
Alexey Bataev | 87933c7 | 2015-09-18 08:07:34 +0000 | [diff] [blame] | 6211 | // exit from construct; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6212 | CodeGenFunction::JumpDest CancelDest = |
Alexey Bataev | 87933c7 | 2015-09-18 08:07:34 +0000 | [diff] [blame] | 6213 | CGF.getOMPCancelDestination(OMPRegionInfo->getDirectiveKind()); |
| 6214 | CGF.EmitBranchThroughCleanup(CancelDest); |
| 6215 | CGF.EmitBlock(ContBB, /*IsFinished=*/true); |
| 6216 | }; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6217 | if (IfCond) { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 6218 | emitOMPIfClause(CGF, IfCond, ThenGen, |
| 6219 | [](CodeGenFunction &, PrePostActionTy &) {}); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6220 | } else { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 6221 | RegionCodeGenTy ThenRCG(ThenGen); |
| 6222 | ThenRCG(CGF); |
| 6223 | } |
Alexey Bataev | 7d5d33e | 2015-07-06 05:50:32 +0000 | [diff] [blame] | 6224 | } |
| 6225 | } |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 6226 | |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 6227 | void CGOpenMPRuntime::emitTargetOutlinedFunction( |
| 6228 | const OMPExecutableDirective &D, StringRef ParentName, |
| 6229 | llvm::Function *&OutlinedFn, llvm::Constant *&OutlinedFnID, |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 6230 | bool IsOffloadEntry, const RegionCodeGenTy &CodeGen) { |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 6231 | assert(!ParentName.empty() && "Invalid target region parent name!"); |
Arpith Chacko Jacob | 5c309e4 | 2016-03-22 01:48:56 +0000 | [diff] [blame] | 6232 | emitTargetOutlinedFunctionHelper(D, ParentName, OutlinedFn, OutlinedFnID, |
| 6233 | IsOffloadEntry, CodeGen); |
| 6234 | } |
| 6235 | |
| 6236 | void CGOpenMPRuntime::emitTargetOutlinedFunctionHelper( |
| 6237 | const OMPExecutableDirective &D, StringRef ParentName, |
| 6238 | llvm::Function *&OutlinedFn, llvm::Constant *&OutlinedFnID, |
| 6239 | bool IsOffloadEntry, const RegionCodeGenTy &CodeGen) { |
Samuel Antao | 2de62b0 | 2016-02-13 23:35:10 +0000 | [diff] [blame] | 6240 | // Create a unique name for the entry function using the source location |
| 6241 | // information of the current target region. The name will be something like: |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 6242 | // |
Samuel Antao | 2de62b0 | 2016-02-13 23:35:10 +0000 | [diff] [blame] | 6243 | // __omp_offloading_DD_FFFF_PP_lBB |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 6244 | // |
| 6245 | // 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] | 6246 | // mangled name of the function that encloses the target region and BB is the |
| 6247 | // line number of the target region. |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 6248 | |
| 6249 | unsigned DeviceID; |
| 6250 | unsigned FileID; |
| 6251 | unsigned Line; |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 6252 | getTargetEntryUniqueInfo(CGM.getContext(), D.getLocStart(), DeviceID, FileID, |
Samuel Antao | 2de62b0 | 2016-02-13 23:35:10 +0000 | [diff] [blame] | 6253 | Line); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 6254 | SmallString<64> EntryFnName; |
| 6255 | { |
| 6256 | llvm::raw_svector_ostream OS(EntryFnName); |
Samuel Antao | 2de62b0 | 2016-02-13 23:35:10 +0000 | [diff] [blame] | 6257 | OS << "__omp_offloading" << llvm::format("_%x", DeviceID) |
| 6258 | << llvm::format("_%x_", FileID) << ParentName << "_l" << Line; |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 6259 | } |
| 6260 | |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 6261 | const CapturedStmt &CS = *D.getCapturedStmt(OMPD_target); |
Arpith Chacko Jacob | 5c309e4 | 2016-03-22 01:48:56 +0000 | [diff] [blame] | 6262 | |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 6263 | CodeGenFunction CGF(CGM, true); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 6264 | CGOpenMPTargetRegionInfo CGInfo(CS, CodeGen, EntryFnName); |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 6265 | CodeGenFunction::CGCapturedStmtRAII CapInfoRAII(CGF, &CGInfo); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 6266 | |
Samuel Antao | 6d00426 | 2016-06-16 18:39:34 +0000 | [diff] [blame] | 6267 | OutlinedFn = CGF.GenerateOpenMPCapturedStmtFunction(CS); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 6268 | |
| 6269 | // If this target outline function is not an offload entry, we don't need to |
| 6270 | // register it. |
| 6271 | if (!IsOffloadEntry) |
| 6272 | return; |
| 6273 | |
| 6274 | // The target region ID is used by the runtime library to identify the current |
| 6275 | // target region, so it only has to be unique and not necessarily point to |
| 6276 | // anything. It could be the pointer to the outlined function that implements |
| 6277 | // the target region, but we aren't using that so that the compiler doesn't |
| 6278 | // need to keep that, and could therefore inline the host function if proven |
| 6279 | // worthwhile during optimization. In the other hand, if emitting code for the |
| 6280 | // device, the ID has to be the function address so that it can retrieved from |
| 6281 | // the offloading entry and launched by the runtime library. We also mark the |
| 6282 | // outlined function to have external linkage in case we are emitting code for |
| 6283 | // the device, because these functions will be entry points to the device. |
| 6284 | |
| 6285 | if (CGM.getLangOpts().OpenMPIsDevice) { |
| 6286 | OutlinedFnID = llvm::ConstantExpr::getBitCast(OutlinedFn, CGM.Int8PtrTy); |
Alexey Bataev | 9a70017 | 2018-05-08 14:16:57 +0000 | [diff] [blame] | 6287 | OutlinedFn->setLinkage(llvm::GlobalValue::WeakAnyLinkage); |
Rafael Espindola | cbca487 | 2018-01-11 22:15:12 +0000 | [diff] [blame] | 6288 | OutlinedFn->setDSOLocal(false); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6289 | } else { |
Alexey Bataev | c15ea70 | 2018-05-09 18:02:37 +0000 | [diff] [blame] | 6290 | std::string Name = getName({EntryFnName, "region_id"}); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 6291 | OutlinedFnID = new llvm::GlobalVariable( |
| 6292 | CGM.getModule(), CGM.Int8Ty, /*isConstant=*/true, |
Alexey Bataev | 9a70017 | 2018-05-08 14:16:57 +0000 | [diff] [blame] | 6293 | llvm::GlobalValue::WeakAnyLinkage, |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 6294 | llvm::Constant::getNullValue(CGM.Int8Ty), Name); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6295 | } |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 6296 | |
| 6297 | // Register the information for the entry associated with this target region. |
| 6298 | OffloadEntriesInfoManager.registerTargetRegionEntryInfo( |
Samuel Antao | f83efdb | 2017-01-05 16:02:49 +0000 | [diff] [blame] | 6299 | DeviceID, FileID, ParentName, Line, OutlinedFn, OutlinedFnID, |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 6300 | OffloadEntriesInfoManagerTy::OMPTargetRegionEntryTargetRegion); |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 6301 | } |
| 6302 | |
Carlo Bertolli | 6eee906 | 2016-04-29 01:37:30 +0000 | [diff] [blame] | 6303 | /// discard all CompoundStmts intervening between two constructs |
| 6304 | static const Stmt *ignoreCompoundStmts(const Stmt *Body) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6305 | while (const auto *CS = dyn_cast_or_null<CompoundStmt>(Body)) |
Carlo Bertolli | 6eee906 | 2016-04-29 01:37:30 +0000 | [diff] [blame] | 6306 | Body = CS->body_front(); |
| 6307 | |
| 6308 | return Body; |
| 6309 | } |
| 6310 | |
Arpith Chacko Jacob | 33c849a | 2017-01-25 00:57:16 +0000 | [diff] [blame] | 6311 | /// Emit the number of teams for a target directive. Inspect the num_teams |
| 6312 | /// clause associated with a teams construct combined or closely nested |
| 6313 | /// with the target directive. |
| 6314 | /// |
| 6315 | /// Emit a team of size one for directives such as 'target parallel' that |
| 6316 | /// have no associated teams construct. |
| 6317 | /// |
| 6318 | /// Otherwise, return nullptr. |
Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 6319 | static llvm::Value * |
Arpith Chacko Jacob | 33c849a | 2017-01-25 00:57:16 +0000 | [diff] [blame] | 6320 | emitNumTeamsForTargetDirective(CGOpenMPRuntime &OMPRuntime, |
| 6321 | CodeGenFunction &CGF, |
| 6322 | const OMPExecutableDirective &D) { |
Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 6323 | assert(!CGF.getLangOpts().OpenMPIsDevice && "Clauses associated with the " |
| 6324 | "teams directive expected to be " |
| 6325 | "emitted only for the host!"); |
| 6326 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6327 | CGBuilderTy &Bld = CGF.Builder; |
Arpith Chacko Jacob | 99a1e0e | 2017-01-25 02:18:43 +0000 | [diff] [blame] | 6328 | |
| 6329 | // If the target directive is combined with a teams directive: |
| 6330 | // Return the value in the num_teams clause, if any. |
| 6331 | // Otherwise, return 0 to denote the runtime default. |
| 6332 | if (isOpenMPTeamsDirective(D.getDirectiveKind())) { |
| 6333 | if (const auto *NumTeamsClause = D.getSingleClause<OMPNumTeamsClause>()) { |
| 6334 | CodeGenFunction::RunCleanupsScope NumTeamsScope(CGF); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6335 | llvm::Value *NumTeams = CGF.EmitScalarExpr(NumTeamsClause->getNumTeams(), |
| 6336 | /*IgnoreResultAssign*/ true); |
Arpith Chacko Jacob | 99a1e0e | 2017-01-25 02:18:43 +0000 | [diff] [blame] | 6337 | return Bld.CreateIntCast(NumTeams, CGF.Int32Ty, |
| 6338 | /*IsSigned=*/true); |
| 6339 | } |
| 6340 | |
| 6341 | // The default value is 0. |
| 6342 | return Bld.getInt32(0); |
| 6343 | } |
| 6344 | |
Arpith Chacko Jacob | 33c849a | 2017-01-25 00:57:16 +0000 | [diff] [blame] | 6345 | // If the target directive is combined with a parallel directive but not a |
| 6346 | // teams directive, start one team. |
Arpith Chacko Jacob | 99a1e0e | 2017-01-25 02:18:43 +0000 | [diff] [blame] | 6347 | if (isOpenMPParallelDirective(D.getDirectiveKind())) |
| 6348 | return Bld.getInt32(1); |
Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 6349 | |
| 6350 | // If the current target region has a teams region enclosed, we need to get |
| 6351 | // the number of teams to pass to the runtime function call. This is done |
| 6352 | // by generating the expression in a inlined region. This is required because |
| 6353 | // the expression is captured in the enclosing target environment when the |
| 6354 | // teams directive is not combined with target. |
| 6355 | |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 6356 | const CapturedStmt &CS = *D.getCapturedStmt(OMPD_target); |
Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 6357 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6358 | if (const auto *TeamsDir = dyn_cast_or_null<OMPExecutableDirective>( |
Carlo Bertolli | 6eee906 | 2016-04-29 01:37:30 +0000 | [diff] [blame] | 6359 | ignoreCompoundStmts(CS.getCapturedStmt()))) { |
Alexey Bataev | 50a1c78 | 2017-12-01 21:31:08 +0000 | [diff] [blame] | 6360 | if (isOpenMPTeamsDirective(TeamsDir->getDirectiveKind())) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6361 | if (const auto *NTE = TeamsDir->getSingleClause<OMPNumTeamsClause>()) { |
Alexey Bataev | 50a1c78 | 2017-12-01 21:31:08 +0000 | [diff] [blame] | 6362 | CGOpenMPInnerExprInfo CGInfo(CGF, CS); |
| 6363 | CodeGenFunction::CGCapturedStmtRAII CapInfoRAII(CGF, &CGInfo); |
| 6364 | llvm::Value *NumTeams = CGF.EmitScalarExpr(NTE->getNumTeams()); |
| 6365 | return Bld.CreateIntCast(NumTeams, CGF.Int32Ty, |
| 6366 | /*IsSigned=*/true); |
| 6367 | } |
Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 6368 | |
Alexey Bataev | 50a1c78 | 2017-12-01 21:31:08 +0000 | [diff] [blame] | 6369 | // If we have an enclosed teams directive but no num_teams clause we use |
| 6370 | // the default value 0. |
| 6371 | return Bld.getInt32(0); |
| 6372 | } |
Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 6373 | } |
| 6374 | |
| 6375 | // No teams associated with the directive. |
| 6376 | return nullptr; |
| 6377 | } |
| 6378 | |
Arpith Chacko Jacob | 33c849a | 2017-01-25 00:57:16 +0000 | [diff] [blame] | 6379 | /// Emit the number of threads for a target directive. Inspect the |
| 6380 | /// thread_limit clause associated with a teams construct combined or closely |
| 6381 | /// nested with the target directive. |
| 6382 | /// |
| 6383 | /// Emit the num_threads clause for directives such as 'target parallel' that |
| 6384 | /// have no associated teams construct. |
| 6385 | /// |
| 6386 | /// Otherwise, return nullptr. |
Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 6387 | static llvm::Value * |
Arpith Chacko Jacob | 33c849a | 2017-01-25 00:57:16 +0000 | [diff] [blame] | 6388 | emitNumThreadsForTargetDirective(CGOpenMPRuntime &OMPRuntime, |
| 6389 | CodeGenFunction &CGF, |
| 6390 | const OMPExecutableDirective &D) { |
Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 6391 | assert(!CGF.getLangOpts().OpenMPIsDevice && "Clauses associated with the " |
| 6392 | "teams directive expected to be " |
| 6393 | "emitted only for the host!"); |
| 6394 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6395 | CGBuilderTy &Bld = CGF.Builder; |
Arpith Chacko Jacob | 33c849a | 2017-01-25 00:57:16 +0000 | [diff] [blame] | 6396 | |
| 6397 | // |
| 6398 | // If the target directive is combined with a teams directive: |
| 6399 | // Return the value in the thread_limit clause, if any. |
| 6400 | // |
| 6401 | // If the target directive is combined with a parallel directive: |
| 6402 | // Return the value in the num_threads clause, if any. |
| 6403 | // |
| 6404 | // If both clauses are set, select the minimum of the two. |
| 6405 | // |
| 6406 | // If neither teams or parallel combined directives set the number of threads |
| 6407 | // in a team, return 0 to denote the runtime default. |
| 6408 | // |
| 6409 | // If this is not a teams directive return nullptr. |
| 6410 | |
Arpith Chacko Jacob | 99a1e0e | 2017-01-25 02:18:43 +0000 | [diff] [blame] | 6411 | if (isOpenMPTeamsDirective(D.getDirectiveKind()) || |
| 6412 | isOpenMPParallelDirective(D.getDirectiveKind())) { |
Arpith Chacko Jacob | 33c849a | 2017-01-25 00:57:16 +0000 | [diff] [blame] | 6413 | llvm::Value *DefaultThreadLimitVal = Bld.getInt32(0); |
| 6414 | llvm::Value *NumThreadsVal = nullptr; |
Arpith Chacko Jacob | 99a1e0e | 2017-01-25 02:18:43 +0000 | [diff] [blame] | 6415 | llvm::Value *ThreadLimitVal = nullptr; |
| 6416 | |
| 6417 | if (const auto *ThreadLimitClause = |
| 6418 | D.getSingleClause<OMPThreadLimitClause>()) { |
| 6419 | CodeGenFunction::RunCleanupsScope ThreadLimitScope(CGF); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6420 | llvm::Value *ThreadLimit = |
| 6421 | CGF.EmitScalarExpr(ThreadLimitClause->getThreadLimit(), |
| 6422 | /*IgnoreResultAssign*/ true); |
Arpith Chacko Jacob | 99a1e0e | 2017-01-25 02:18:43 +0000 | [diff] [blame] | 6423 | ThreadLimitVal = Bld.CreateIntCast(ThreadLimit, CGF.Int32Ty, |
| 6424 | /*IsSigned=*/true); |
| 6425 | } |
Arpith Chacko Jacob | 33c849a | 2017-01-25 00:57:16 +0000 | [diff] [blame] | 6426 | |
| 6427 | if (const auto *NumThreadsClause = |
| 6428 | D.getSingleClause<OMPNumThreadsClause>()) { |
| 6429 | CodeGenFunction::RunCleanupsScope NumThreadsScope(CGF); |
| 6430 | llvm::Value *NumThreads = |
| 6431 | CGF.EmitScalarExpr(NumThreadsClause->getNumThreads(), |
| 6432 | /*IgnoreResultAssign*/ true); |
| 6433 | NumThreadsVal = |
| 6434 | Bld.CreateIntCast(NumThreads, CGF.Int32Ty, /*IsSigned=*/true); |
| 6435 | } |
| 6436 | |
Arpith Chacko Jacob | 99a1e0e | 2017-01-25 02:18:43 +0000 | [diff] [blame] | 6437 | // Select the lesser of thread_limit and num_threads. |
| 6438 | if (NumThreadsVal) |
| 6439 | ThreadLimitVal = ThreadLimitVal |
| 6440 | ? Bld.CreateSelect(Bld.CreateICmpSLT(NumThreadsVal, |
| 6441 | ThreadLimitVal), |
| 6442 | NumThreadsVal, ThreadLimitVal) |
| 6443 | : NumThreadsVal; |
Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 6444 | |
Arpith Chacko Jacob | 99a1e0e | 2017-01-25 02:18:43 +0000 | [diff] [blame] | 6445 | // Set default value passed to the runtime if either teams or a target |
| 6446 | // parallel type directive is found but no clause is specified. |
| 6447 | if (!ThreadLimitVal) |
| 6448 | ThreadLimitVal = DefaultThreadLimitVal; |
| 6449 | |
| 6450 | return ThreadLimitVal; |
| 6451 | } |
Arpith Chacko Jacob | 86f9e46 | 2017-01-25 01:45:59 +0000 | [diff] [blame] | 6452 | |
Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 6453 | // If the current target region has a teams region enclosed, we need to get |
| 6454 | // the thread limit to pass to the runtime function call. This is done |
| 6455 | // by generating the expression in a inlined region. This is required because |
| 6456 | // the expression is captured in the enclosing target environment when the |
| 6457 | // teams directive is not combined with target. |
| 6458 | |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 6459 | const CapturedStmt &CS = *D.getCapturedStmt(OMPD_target); |
Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 6460 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6461 | if (const auto *TeamsDir = dyn_cast_or_null<OMPExecutableDirective>( |
Carlo Bertolli | 6eee906 | 2016-04-29 01:37:30 +0000 | [diff] [blame] | 6462 | ignoreCompoundStmts(CS.getCapturedStmt()))) { |
Alexey Bataev | 50a1c78 | 2017-12-01 21:31:08 +0000 | [diff] [blame] | 6463 | if (isOpenMPTeamsDirective(TeamsDir->getDirectiveKind())) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6464 | if (const auto *TLE = TeamsDir->getSingleClause<OMPThreadLimitClause>()) { |
Alexey Bataev | 50a1c78 | 2017-12-01 21:31:08 +0000 | [diff] [blame] | 6465 | CGOpenMPInnerExprInfo CGInfo(CGF, CS); |
| 6466 | CodeGenFunction::CGCapturedStmtRAII CapInfoRAII(CGF, &CGInfo); |
| 6467 | llvm::Value *ThreadLimit = CGF.EmitScalarExpr(TLE->getThreadLimit()); |
| 6468 | return CGF.Builder.CreateIntCast(ThreadLimit, CGF.Int32Ty, |
| 6469 | /*IsSigned=*/true); |
| 6470 | } |
Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 6471 | |
Alexey Bataev | 50a1c78 | 2017-12-01 21:31:08 +0000 | [diff] [blame] | 6472 | // If we have an enclosed teams directive but no thread_limit clause we |
| 6473 | // use the default value 0. |
| 6474 | return CGF.Builder.getInt32(0); |
| 6475 | } |
Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 6476 | } |
| 6477 | |
| 6478 | // No teams associated with the directive. |
| 6479 | return nullptr; |
| 6480 | } |
| 6481 | |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6482 | namespace { |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 6483 | // Utility to handle information from clauses associated with a given |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6484 | // construct that use mappable expressions (e.g. 'map' clause, 'to' clause). |
| 6485 | // It provides a convenient interface to obtain the information and generate |
| 6486 | // code for that information. |
| 6487 | class MappableExprsHandler { |
| 6488 | public: |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 6489 | /// Values for bit flags used to specify the mapping type for |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6490 | /// offloading. |
| 6491 | enum OpenMPOffloadMappingFlags { |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 6492 | /// Allocate memory on the device and move data from host to device. |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6493 | OMP_MAP_TO = 0x01, |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 6494 | /// Allocate memory on the device and move data from device to host. |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6495 | OMP_MAP_FROM = 0x02, |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 6496 | /// Always perform the requested mapping action on the element, even |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6497 | /// if it was already mapped before. |
| 6498 | OMP_MAP_ALWAYS = 0x04, |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 6499 | /// Delete the element from the device environment, ignoring the |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6500 | /// current reference count associated with the element. |
Samuel Antao | 6782e94 | 2016-05-26 16:48:10 +0000 | [diff] [blame] | 6501 | OMP_MAP_DELETE = 0x08, |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 6502 | /// The element being mapped is a pointer-pointee pair; both the |
George Rokos | 065755d | 2017-11-07 18:27:04 +0000 | [diff] [blame] | 6503 | /// pointer and the pointee should be mapped. |
| 6504 | OMP_MAP_PTR_AND_OBJ = 0x10, |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 6505 | /// This flags signals that the base address of an entry should be |
George Rokos | 065755d | 2017-11-07 18:27:04 +0000 | [diff] [blame] | 6506 | /// passed to the target kernel as an argument. |
| 6507 | OMP_MAP_TARGET_PARAM = 0x20, |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 6508 | /// Signal that the runtime library has to return the device pointer |
George Rokos | 065755d | 2017-11-07 18:27:04 +0000 | [diff] [blame] | 6509 | /// in the current position for the data being mapped. Used when we have the |
| 6510 | /// use_device_ptr clause. |
| 6511 | OMP_MAP_RETURN_PARAM = 0x40, |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 6512 | /// This flag signals that the reference being passed is a pointer to |
Samuel Antao | d486f84 | 2016-05-26 16:53:38 +0000 | [diff] [blame] | 6513 | /// private data. |
George Rokos | 065755d | 2017-11-07 18:27:04 +0000 | [diff] [blame] | 6514 | OMP_MAP_PRIVATE = 0x80, |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 6515 | /// Pass the element to the device by value. |
George Rokos | 065755d | 2017-11-07 18:27:04 +0000 | [diff] [blame] | 6516 | OMP_MAP_LITERAL = 0x100, |
Alexey Bataev | f47c4b4 | 2017-09-26 13:47:31 +0000 | [diff] [blame] | 6517 | /// Implicit map |
| 6518 | OMP_MAP_IMPLICIT = 0x200, |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6519 | }; |
| 6520 | |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 6521 | /// Class that associates information with a base pointer to be passed to the |
| 6522 | /// runtime library. |
| 6523 | class BasePointerInfo { |
| 6524 | /// The base pointer. |
| 6525 | llvm::Value *Ptr = nullptr; |
| 6526 | /// The base declaration that refers to this device pointer, or null if |
| 6527 | /// there is none. |
| 6528 | const ValueDecl *DevPtrDecl = nullptr; |
| 6529 | |
| 6530 | public: |
| 6531 | BasePointerInfo(llvm::Value *Ptr, const ValueDecl *DevPtrDecl = nullptr) |
| 6532 | : Ptr(Ptr), DevPtrDecl(DevPtrDecl) {} |
| 6533 | llvm::Value *operator*() const { return Ptr; } |
| 6534 | const ValueDecl *getDevicePtrDecl() const { return DevPtrDecl; } |
| 6535 | void setDevicePtrDecl(const ValueDecl *D) { DevPtrDecl = D; } |
| 6536 | }; |
| 6537 | |
| 6538 | typedef SmallVector<BasePointerInfo, 16> MapBaseValuesArrayTy; |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6539 | typedef SmallVector<llvm::Value *, 16> MapValuesArrayTy; |
George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 6540 | typedef SmallVector<uint64_t, 16> MapFlagsArrayTy; |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6541 | |
| 6542 | private: |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 6543 | /// Directive from where the map clauses were extracted. |
Samuel Antao | 44bcdb3 | 2016-07-28 15:31:29 +0000 | [diff] [blame] | 6544 | const OMPExecutableDirective &CurDir; |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6545 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 6546 | /// Function the directive is being generated for. |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6547 | CodeGenFunction &CGF; |
| 6548 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 6549 | /// Set of all first private variables in the current directive. |
Samuel Antao | d486f84 | 2016-05-26 16:53:38 +0000 | [diff] [blame] | 6550 | llvm::SmallPtrSet<const VarDecl *, 8> FirstPrivateDecls; |
Alexey Bataev | 3f96fe6 | 2017-12-13 17:31:39 +0000 | [diff] [blame] | 6551 | /// Set of all reduction variables in the current directive. |
| 6552 | llvm::SmallPtrSet<const VarDecl *, 8> ReductionDecls; |
Samuel Antao | d486f84 | 2016-05-26 16:53:38 +0000 | [diff] [blame] | 6553 | |
Samuel Antao | 6890b09 | 2016-07-28 14:25:09 +0000 | [diff] [blame] | 6554 | /// Map between device pointer declarations and their expression components. |
| 6555 | /// The key value for declarations in 'this' is null. |
| 6556 | llvm::DenseMap< |
| 6557 | const ValueDecl *, |
| 6558 | SmallVector<OMPClauseMappableExprCommon::MappableExprComponentListRef, 4>> |
| 6559 | DevPointersMap; |
| 6560 | |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6561 | llvm::Value *getExprTypeSize(const Expr *E) const { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6562 | QualType ExprTy = E->getType().getCanonicalType(); |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6563 | |
| 6564 | // Reference types are ignored for mapping purposes. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6565 | if (const auto *RefTy = ExprTy->getAs<ReferenceType>()) |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6566 | ExprTy = RefTy->getPointeeType().getCanonicalType(); |
| 6567 | |
| 6568 | // Given that an array section is considered a built-in type, we need to |
| 6569 | // do the calculation based on the length of the section instead of relying |
| 6570 | // on CGF.getTypeSize(E->getType()). |
| 6571 | if (const auto *OAE = dyn_cast<OMPArraySectionExpr>(E)) { |
| 6572 | QualType BaseTy = OMPArraySectionExpr::getBaseOriginalType( |
| 6573 | OAE->getBase()->IgnoreParenImpCasts()) |
| 6574 | .getCanonicalType(); |
| 6575 | |
| 6576 | // If there is no length associated with the expression, that means we |
| 6577 | // are using the whole length of the base. |
| 6578 | if (!OAE->getLength() && OAE->getColonLoc().isValid()) |
| 6579 | return CGF.getTypeSize(BaseTy); |
| 6580 | |
| 6581 | llvm::Value *ElemSize; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6582 | if (const auto *PTy = BaseTy->getAs<PointerType>()) { |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6583 | ElemSize = CGF.getTypeSize(PTy->getPointeeType().getCanonicalType()); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6584 | } else { |
| 6585 | const auto *ATy = cast<ArrayType>(BaseTy.getTypePtr()); |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6586 | assert(ATy && "Expecting array type if not a pointer type."); |
| 6587 | ElemSize = CGF.getTypeSize(ATy->getElementType().getCanonicalType()); |
| 6588 | } |
| 6589 | |
| 6590 | // If we don't have a length at this point, that is because we have an |
| 6591 | // array section with a single element. |
| 6592 | if (!OAE->getLength()) |
| 6593 | return ElemSize; |
| 6594 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6595 | llvm::Value *LengthVal = CGF.EmitScalarExpr(OAE->getLength()); |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6596 | LengthVal = |
| 6597 | CGF.Builder.CreateIntCast(LengthVal, CGF.SizeTy, /*isSigned=*/false); |
| 6598 | return CGF.Builder.CreateNUWMul(LengthVal, ElemSize); |
| 6599 | } |
| 6600 | return CGF.getTypeSize(ExprTy); |
| 6601 | } |
| 6602 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 6603 | /// Return the corresponding bits for a given map clause modifier. Add |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6604 | /// 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] | 6605 | /// map as the first one of a series of maps that relate to the same map |
| 6606 | /// expression. |
George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 6607 | uint64_t getMapTypeBits(OpenMPMapClauseKind MapType, |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6608 | OpenMPMapClauseKind MapTypeModifier, bool AddPtrFlag, |
George Rokos | 065755d | 2017-11-07 18:27:04 +0000 | [diff] [blame] | 6609 | bool AddIsTargetParamFlag) const { |
George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 6610 | uint64_t Bits = 0u; |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6611 | switch (MapType) { |
| 6612 | case OMPC_MAP_alloc: |
Samuel Antao | 6782e94 | 2016-05-26 16:48:10 +0000 | [diff] [blame] | 6613 | case OMPC_MAP_release: |
| 6614 | // alloc and release is the default behavior in the runtime library, i.e. |
| 6615 | // if we don't pass any bits alloc/release that is what the runtime is |
| 6616 | // going to do. Therefore, we don't need to signal anything for these two |
| 6617 | // type modifiers. |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6618 | break; |
| 6619 | case OMPC_MAP_to: |
| 6620 | Bits = OMP_MAP_TO; |
| 6621 | break; |
| 6622 | case OMPC_MAP_from: |
| 6623 | Bits = OMP_MAP_FROM; |
| 6624 | break; |
| 6625 | case OMPC_MAP_tofrom: |
| 6626 | Bits = OMP_MAP_TO | OMP_MAP_FROM; |
| 6627 | break; |
| 6628 | case OMPC_MAP_delete: |
| 6629 | Bits = OMP_MAP_DELETE; |
| 6630 | break; |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6631 | default: |
| 6632 | llvm_unreachable("Unexpected map type!"); |
| 6633 | break; |
| 6634 | } |
| 6635 | if (AddPtrFlag) |
George Rokos | 065755d | 2017-11-07 18:27:04 +0000 | [diff] [blame] | 6636 | Bits |= OMP_MAP_PTR_AND_OBJ; |
| 6637 | if (AddIsTargetParamFlag) |
| 6638 | Bits |= OMP_MAP_TARGET_PARAM; |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6639 | if (MapTypeModifier == OMPC_MAP_always) |
| 6640 | Bits |= OMP_MAP_ALWAYS; |
| 6641 | return Bits; |
| 6642 | } |
| 6643 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 6644 | /// Return true if the provided expression is a final array section. A |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6645 | /// final array section, is one whose length can't be proved to be one. |
| 6646 | bool isFinalArraySectionExpression(const Expr *E) const { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6647 | const auto *OASE = dyn_cast<OMPArraySectionExpr>(E); |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6648 | |
| 6649 | // It is not an array section and therefore not a unity-size one. |
| 6650 | if (!OASE) |
| 6651 | return false; |
| 6652 | |
| 6653 | // An array section with no colon always refer to a single element. |
| 6654 | if (OASE->getColonLoc().isInvalid()) |
| 6655 | return false; |
| 6656 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6657 | const Expr *Length = OASE->getLength(); |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6658 | |
| 6659 | // If we don't have a length we have to check if the array has size 1 |
| 6660 | // for this dimension. Also, we should always expect a length if the |
| 6661 | // base type is pointer. |
| 6662 | if (!Length) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6663 | QualType BaseQTy = OMPArraySectionExpr::getBaseOriginalType( |
| 6664 | OASE->getBase()->IgnoreParenImpCasts()) |
| 6665 | .getCanonicalType(); |
| 6666 | if (const auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr())) |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6667 | return ATy->getSize().getSExtValue() != 1; |
| 6668 | // If we don't have a constant dimension length, we have to consider |
| 6669 | // the current section as having any size, so it is not necessarily |
| 6670 | // unitary. If it happen to be unity size, that's user fault. |
| 6671 | return true; |
| 6672 | } |
| 6673 | |
| 6674 | // Check if the length evaluates to 1. |
| 6675 | llvm::APSInt ConstLength; |
| 6676 | if (!Length->EvaluateAsInt(ConstLength, CGF.getContext())) |
| 6677 | return true; // Can have more that size 1. |
| 6678 | |
| 6679 | return ConstLength.getSExtValue() != 1; |
| 6680 | } |
| 6681 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 6682 | /// Return the adjusted map modifiers if the declaration a capture |
Alexey Bataev | 92327c5 | 2018-03-26 16:40:55 +0000 | [diff] [blame] | 6683 | /// refers to appears in a first-private clause. This is expected to be used |
| 6684 | /// only with directives that start with 'target'. |
| 6685 | unsigned adjustMapModifiersForPrivateClauses(const CapturedStmt::Capture &Cap, |
| 6686 | unsigned CurrentModifiers) { |
| 6687 | assert(Cap.capturesVariable() && "Expected capture by reference only!"); |
| 6688 | |
| 6689 | // A first private variable captured by reference will use only the |
| 6690 | // 'private ptr' and 'map to' flag. Return the right flags if the captured |
| 6691 | // declaration is known as first-private in this handler. |
| 6692 | if (FirstPrivateDecls.count(Cap.getCapturedVar())) |
| 6693 | return MappableExprsHandler::OMP_MAP_PRIVATE | |
| 6694 | MappableExprsHandler::OMP_MAP_TO; |
| 6695 | // Reduction variable will use only the 'private ptr' and 'map to_from' |
| 6696 | // flag. |
| 6697 | if (ReductionDecls.count(Cap.getCapturedVar())) { |
| 6698 | return MappableExprsHandler::OMP_MAP_TO | |
| 6699 | MappableExprsHandler::OMP_MAP_FROM; |
| 6700 | } |
| 6701 | |
| 6702 | // We didn't modify anything. |
| 6703 | return CurrentModifiers; |
| 6704 | } |
| 6705 | |
| 6706 | public: |
| 6707 | MappableExprsHandler(const OMPExecutableDirective &Dir, CodeGenFunction &CGF) |
| 6708 | : CurDir(Dir), CGF(CGF) { |
| 6709 | // Extract firstprivate clause information. |
| 6710 | for (const auto *C : Dir.getClausesOfKind<OMPFirstprivateClause>()) |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6711 | for (const Expr *D : C->varlists()) |
Alexey Bataev | 92327c5 | 2018-03-26 16:40:55 +0000 | [diff] [blame] | 6712 | FirstPrivateDecls.insert( |
| 6713 | cast<VarDecl>(cast<DeclRefExpr>(D)->getDecl())->getCanonicalDecl()); |
| 6714 | for (const auto *C : Dir.getClausesOfKind<OMPReductionClause>()) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6715 | for (const Expr *D : C->varlists()) { |
Alexey Bataev | 92327c5 | 2018-03-26 16:40:55 +0000 | [diff] [blame] | 6716 | ReductionDecls.insert( |
| 6717 | cast<VarDecl>(cast<DeclRefExpr>(D)->getDecl())->getCanonicalDecl()); |
| 6718 | } |
| 6719 | } |
| 6720 | // Extract device pointer clause information. |
| 6721 | for (const auto *C : Dir.getClausesOfKind<OMPIsDevicePtrClause>()) |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6722 | for (const auto &L : C->component_lists()) |
Alexey Bataev | 92327c5 | 2018-03-26 16:40:55 +0000 | [diff] [blame] | 6723 | DevPointersMap[L.first].push_back(L.second); |
| 6724 | } |
| 6725 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 6726 | /// Generate the base pointers, section pointers, sizes and map type |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6727 | /// bits for the provided map type, map modifier, and expression components. |
| 6728 | /// \a IsFirstComponent should be set to true if the provided set of |
| 6729 | /// components is the first associated with a capture. |
| 6730 | void generateInfoForComponentList( |
| 6731 | OpenMPMapClauseKind MapType, OpenMPMapClauseKind MapTypeModifier, |
| 6732 | OMPClauseMappableExprCommon::MappableExprComponentListRef Components, |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 6733 | MapBaseValuesArrayTy &BasePointers, MapValuesArrayTy &Pointers, |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6734 | MapValuesArrayTy &Sizes, MapFlagsArrayTy &Types, |
Alexey Bataev | f47c4b4 | 2017-09-26 13:47:31 +0000 | [diff] [blame] | 6735 | bool IsFirstComponentList, bool IsImplicit) const { |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6736 | |
| 6737 | // 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] | 6738 | // types below. The generated information is expressed in this order: |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6739 | // base pointer, section pointer, size, flags |
| 6740 | // (to add to the ones that come from the map type and modifier). |
| 6741 | // |
| 6742 | // double d; |
| 6743 | // int i[100]; |
| 6744 | // float *p; |
| 6745 | // |
| 6746 | // struct S1 { |
| 6747 | // int i; |
| 6748 | // float f[50]; |
| 6749 | // } |
| 6750 | // struct S2 { |
| 6751 | // int i; |
| 6752 | // float f[50]; |
| 6753 | // S1 s; |
| 6754 | // double *p; |
| 6755 | // struct S2 *ps; |
| 6756 | // } |
| 6757 | // S2 s; |
| 6758 | // S2 *ps; |
| 6759 | // |
| 6760 | // map(d) |
| 6761 | // &d, &d, sizeof(double), noflags |
| 6762 | // |
| 6763 | // map(i) |
| 6764 | // &i, &i, 100*sizeof(int), noflags |
| 6765 | // |
| 6766 | // map(i[1:23]) |
| 6767 | // &i(=&i[0]), &i[1], 23*sizeof(int), noflags |
| 6768 | // |
| 6769 | // map(p) |
| 6770 | // &p, &p, sizeof(float*), noflags |
| 6771 | // |
| 6772 | // map(p[1:24]) |
| 6773 | // p, &p[1], 24*sizeof(float), noflags |
| 6774 | // |
| 6775 | // map(s) |
| 6776 | // &s, &s, sizeof(S2), noflags |
| 6777 | // |
| 6778 | // map(s.i) |
| 6779 | // &s, &(s.i), sizeof(int), noflags |
| 6780 | // |
| 6781 | // map(s.s.f) |
| 6782 | // &s, &(s.i.f), 50*sizeof(int), noflags |
| 6783 | // |
| 6784 | // map(s.p) |
| 6785 | // &s, &(s.p), sizeof(double*), noflags |
| 6786 | // |
| 6787 | // map(s.p[:22], s.a s.b) |
| 6788 | // &s, &(s.p), sizeof(double*), noflags |
George Rokos | 065755d | 2017-11-07 18:27:04 +0000 | [diff] [blame] | 6789 | // &(s.p), &(s.p[0]), 22*sizeof(double), ptr_flag |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6790 | // |
| 6791 | // map(s.ps) |
| 6792 | // &s, &(s.ps), sizeof(S2*), noflags |
| 6793 | // |
| 6794 | // map(s.ps->s.i) |
| 6795 | // &s, &(s.ps), sizeof(S2*), noflags |
George Rokos | 065755d | 2017-11-07 18:27:04 +0000 | [diff] [blame] | 6796 | // &(s.ps), &(s.ps->s.i), sizeof(int), ptr_flag |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6797 | // |
| 6798 | // map(s.ps->ps) |
| 6799 | // &s, &(s.ps), sizeof(S2*), noflags |
George Rokos | 065755d | 2017-11-07 18:27:04 +0000 | [diff] [blame] | 6800 | // &(s.ps), &(s.ps->ps), sizeof(S2*), ptr_flag |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6801 | // |
| 6802 | // map(s.ps->ps->ps) |
| 6803 | // &s, &(s.ps), sizeof(S2*), noflags |
George Rokos | 065755d | 2017-11-07 18:27:04 +0000 | [diff] [blame] | 6804 | // &(s.ps), &(s.ps->ps), sizeof(S2*), ptr_flag |
| 6805 | // &(s.ps->ps), &(s.ps->ps->ps), sizeof(S2*), ptr_flag |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6806 | // |
| 6807 | // map(s.ps->ps->s.f[:22]) |
| 6808 | // &s, &(s.ps), sizeof(S2*), noflags |
George Rokos | 065755d | 2017-11-07 18:27:04 +0000 | [diff] [blame] | 6809 | // &(s.ps), &(s.ps->ps), sizeof(S2*), ptr_flag |
| 6810 | // &(s.ps->ps), &(s.ps->ps->s.f[0]), 22*sizeof(float), ptr_flag |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6811 | // |
| 6812 | // map(ps) |
| 6813 | // &ps, &ps, sizeof(S2*), noflags |
| 6814 | // |
| 6815 | // map(ps->i) |
| 6816 | // ps, &(ps->i), sizeof(int), noflags |
| 6817 | // |
| 6818 | // map(ps->s.f) |
| 6819 | // ps, &(ps->s.f[0]), 50*sizeof(float), noflags |
| 6820 | // |
| 6821 | // map(ps->p) |
| 6822 | // ps, &(ps->p), sizeof(double*), noflags |
| 6823 | // |
| 6824 | // map(ps->p[:22]) |
| 6825 | // ps, &(ps->p), sizeof(double*), noflags |
George Rokos | 065755d | 2017-11-07 18:27:04 +0000 | [diff] [blame] | 6826 | // &(ps->p), &(ps->p[0]), 22*sizeof(double), ptr_flag |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6827 | // |
| 6828 | // map(ps->ps) |
| 6829 | // ps, &(ps->ps), sizeof(S2*), noflags |
| 6830 | // |
| 6831 | // map(ps->ps->s.i) |
| 6832 | // ps, &(ps->ps), sizeof(S2*), noflags |
George Rokos | 065755d | 2017-11-07 18:27:04 +0000 | [diff] [blame] | 6833 | // &(ps->ps), &(ps->ps->s.i), sizeof(int), ptr_flag |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6834 | // |
| 6835 | // map(ps->ps->ps) |
| 6836 | // ps, &(ps->ps), sizeof(S2*), noflags |
George Rokos | 065755d | 2017-11-07 18:27:04 +0000 | [diff] [blame] | 6837 | // &(ps->ps), &(ps->ps->ps), sizeof(S2*), ptr_flag |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6838 | // |
| 6839 | // map(ps->ps->ps->ps) |
| 6840 | // ps, &(ps->ps), sizeof(S2*), noflags |
George Rokos | 065755d | 2017-11-07 18:27:04 +0000 | [diff] [blame] | 6841 | // &(ps->ps), &(ps->ps->ps), sizeof(S2*), ptr_flag |
| 6842 | // &(ps->ps->ps), &(ps->ps->ps->ps), sizeof(S2*), ptr_flag |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6843 | // |
| 6844 | // map(ps->ps->ps->s.f[:22]) |
| 6845 | // ps, &(ps->ps), sizeof(S2*), noflags |
George Rokos | 065755d | 2017-11-07 18:27:04 +0000 | [diff] [blame] | 6846 | // &(ps->ps), &(ps->ps->ps), sizeof(S2*), ptr_flag |
| 6847 | // &(ps->ps->ps), &(ps->ps->ps->s.f[0]), 22*sizeof(float), ptr_flag |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6848 | |
| 6849 | // Track if the map information being generated is the first for a capture. |
| 6850 | bool IsCaptureFirstInfo = IsFirstComponentList; |
Alexey Bataev | 92327c5 | 2018-03-26 16:40:55 +0000 | [diff] [blame] | 6851 | bool IsLink = false; // Is this variable a "declare target link"? |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6852 | |
| 6853 | // Scan the components from the base to the complete expression. |
| 6854 | auto CI = Components.rbegin(); |
| 6855 | auto CE = Components.rend(); |
| 6856 | auto I = CI; |
| 6857 | |
| 6858 | // Track if the map information being generated is the first for a list of |
| 6859 | // components. |
| 6860 | bool IsExpressionFirstInfo = true; |
| 6861 | llvm::Value *BP = nullptr; |
| 6862 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6863 | if (const auto *ME = dyn_cast<MemberExpr>(I->getAssociatedExpression())) { |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6864 | // The base is the 'this' pointer. The content of the pointer is going |
| 6865 | // to be the base of the field being mapped. |
| 6866 | BP = CGF.EmitScalarExpr(ME->getBase()); |
| 6867 | } else { |
| 6868 | // The base is the reference to the variable. |
| 6869 | // BP = &Var. |
Alexey Bataev | f47c4b4 | 2017-09-26 13:47:31 +0000 | [diff] [blame] | 6870 | BP = CGF.EmitOMPSharedLValue(I->getAssociatedExpression()).getPointer(); |
Alexey Bataev | 92327c5 | 2018-03-26 16:40:55 +0000 | [diff] [blame] | 6871 | if (const auto *VD = |
| 6872 | dyn_cast_or_null<VarDecl>(I->getAssociatedDeclaration())) { |
| 6873 | if (llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res = |
Alexey Bataev | 2c1dffe | 2018-04-16 20:34:41 +0000 | [diff] [blame] | 6874 | isDeclareTargetDeclaration(VD)) |
| 6875 | if (*Res == OMPDeclareTargetDeclAttr::MT_Link) { |
| 6876 | IsLink = true; |
| 6877 | BP = CGF.CGM.getOpenMPRuntime() |
| 6878 | .getAddrOfDeclareTargetLink(VD) |
| 6879 | .getPointer(); |
| 6880 | } |
Alexey Bataev | 92327c5 | 2018-03-26 16:40:55 +0000 | [diff] [blame] | 6881 | } |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6882 | |
| 6883 | // 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] | 6884 | // 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] | 6885 | // reference. References are ignored for mapping purposes. |
| 6886 | QualType Ty = |
| 6887 | I->getAssociatedDeclaration()->getType().getNonReferenceType(); |
| 6888 | if (Ty->isAnyPointerType() && std::next(I) != CE) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6889 | LValue PtrAddr = CGF.MakeNaturalAlignAddrLValue(BP, Ty); |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6890 | BP = CGF.EmitLoadOfPointerLValue(PtrAddr.getAddress(), |
Samuel Antao | 403ffd4 | 2016-07-27 22:49:49 +0000 | [diff] [blame] | 6891 | Ty->castAs<PointerType>()) |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6892 | .getPointer(); |
| 6893 | |
| 6894 | // We do not need to generate individual map information for the |
| 6895 | // pointer, it can be associated with the combined storage. |
| 6896 | ++I; |
| 6897 | } |
| 6898 | } |
| 6899 | |
George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 6900 | uint64_t DefaultFlags = IsImplicit ? OMP_MAP_IMPLICIT : 0; |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6901 | for (; I != CE; ++I) { |
| 6902 | auto Next = std::next(I); |
| 6903 | |
| 6904 | // We need to generate the addresses and sizes if this is the last |
| 6905 | // component, if the component is a pointer or if it is an array section |
| 6906 | // whose length can't be proved to be one. If this is a pointer, it |
| 6907 | // becomes the base address for the following components. |
| 6908 | |
| 6909 | // A final array section, is one whose length can't be proved to be one. |
| 6910 | bool IsFinalArraySection = |
| 6911 | isFinalArraySectionExpression(I->getAssociatedExpression()); |
| 6912 | |
| 6913 | // Get information on whether the element is a pointer. Have to do a |
| 6914 | // special treatment for array sections given that they are built-in |
| 6915 | // types. |
| 6916 | const auto *OASE = |
| 6917 | dyn_cast<OMPArraySectionExpr>(I->getAssociatedExpression()); |
| 6918 | bool IsPointer = |
| 6919 | (OASE && |
| 6920 | OMPArraySectionExpr::getBaseOriginalType(OASE) |
| 6921 | .getCanonicalType() |
| 6922 | ->isAnyPointerType()) || |
| 6923 | I->getAssociatedExpression()->getType()->isAnyPointerType(); |
| 6924 | |
| 6925 | if (Next == CE || IsPointer || IsFinalArraySection) { |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6926 | // If this is not the last component, we expect the pointer to be |
| 6927 | // associated with an array expression or member expression. |
| 6928 | assert((Next == CE || |
| 6929 | isa<MemberExpr>(Next->getAssociatedExpression()) || |
| 6930 | isa<ArraySubscriptExpr>(Next->getAssociatedExpression()) || |
| 6931 | isa<OMPArraySectionExpr>(Next->getAssociatedExpression())) && |
| 6932 | "Unexpected expression"); |
| 6933 | |
Alexey Bataev | f47c4b4 | 2017-09-26 13:47:31 +0000 | [diff] [blame] | 6934 | llvm::Value *LB = |
| 6935 | CGF.EmitOMPSharedLValue(I->getAssociatedExpression()).getPointer(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6936 | llvm::Value *Size = getExprTypeSize(I->getAssociatedExpression()); |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6937 | |
Samuel Antao | 03a3cec | 2016-07-27 22:52:16 +0000 | [diff] [blame] | 6938 | // If we have a member expression and the current component is a |
| 6939 | // reference, we have to map the reference too. Whenever we have a |
| 6940 | // reference, the section that reference refers to is going to be a |
| 6941 | // load instruction from the storage assigned to the reference. |
| 6942 | if (isa<MemberExpr>(I->getAssociatedExpression()) && |
| 6943 | I->getAssociatedDeclaration()->getType()->isReferenceType()) { |
| 6944 | auto *LI = cast<llvm::LoadInst>(LB); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6945 | llvm::Value *RefAddr = LI->getPointerOperand(); |
Samuel Antao | 03a3cec | 2016-07-27 22:52:16 +0000 | [diff] [blame] | 6946 | |
| 6947 | BasePointers.push_back(BP); |
| 6948 | Pointers.push_back(RefAddr); |
| 6949 | Sizes.push_back(CGF.getTypeSize(CGF.getContext().VoidPtrTy)); |
Alexey Bataev | f47c4b4 | 2017-09-26 13:47:31 +0000 | [diff] [blame] | 6950 | Types.push_back(DefaultFlags | |
| 6951 | getMapTypeBits( |
| 6952 | /*MapType*/ OMPC_MAP_alloc, |
| 6953 | /*MapTypeModifier=*/OMPC_MAP_unknown, |
| 6954 | !IsExpressionFirstInfo, IsCaptureFirstInfo)); |
Samuel Antao | 03a3cec | 2016-07-27 22:52:16 +0000 | [diff] [blame] | 6955 | IsExpressionFirstInfo = false; |
| 6956 | IsCaptureFirstInfo = false; |
| 6957 | // The reference will be the next base address. |
| 6958 | BP = RefAddr; |
| 6959 | } |
| 6960 | |
| 6961 | BasePointers.push_back(BP); |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6962 | Pointers.push_back(LB); |
| 6963 | Sizes.push_back(Size); |
Samuel Antao | 03a3cec | 2016-07-27 22:52:16 +0000 | [diff] [blame] | 6964 | |
Samuel Antao | 6782e94 | 2016-05-26 16:48:10 +0000 | [diff] [blame] | 6965 | // We need to add a pointer flag for each map that comes from the |
| 6966 | // same expression except for the first one. We also need to signal |
| 6967 | // this map is the first one that relates with the current capture |
| 6968 | // (there is a set of entries for each capture). |
Alexey Bataev | 92327c5 | 2018-03-26 16:40:55 +0000 | [diff] [blame] | 6969 | Types.push_back(DefaultFlags | |
| 6970 | getMapTypeBits(MapType, MapTypeModifier, |
| 6971 | !IsExpressionFirstInfo || IsLink, |
| 6972 | IsCaptureFirstInfo && !IsLink)); |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6973 | |
| 6974 | // If we have a final array section, we are done with this expression. |
| 6975 | if (IsFinalArraySection) |
| 6976 | break; |
| 6977 | |
| 6978 | // The pointer becomes the base for the next element. |
| 6979 | if (Next != CE) |
| 6980 | BP = LB; |
| 6981 | |
| 6982 | IsExpressionFirstInfo = false; |
| 6983 | IsCaptureFirstInfo = false; |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6984 | } |
| 6985 | } |
| 6986 | } |
| 6987 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 6988 | /// Generate all the base pointers, section pointers, sizes and map |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 6989 | /// types for the extracted mappable expressions. Also, for each item that |
| 6990 | /// relates with a device pointer, a pair of the relevant declaration and |
| 6991 | /// index where it occurs is appended to the device pointers info array. |
| 6992 | void generateAllInfo(MapBaseValuesArrayTy &BasePointers, |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 6993 | MapValuesArrayTy &Pointers, MapValuesArrayTy &Sizes, |
| 6994 | MapFlagsArrayTy &Types) const { |
| 6995 | BasePointers.clear(); |
| 6996 | Pointers.clear(); |
| 6997 | Sizes.clear(); |
| 6998 | Types.clear(); |
| 6999 | |
| 7000 | struct MapInfo { |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7001 | /// Kind that defines how a device pointer has to be returned. |
| 7002 | enum ReturnPointerKind { |
| 7003 | // Don't have to return any pointer. |
| 7004 | RPK_None, |
| 7005 | // Pointer is the base of the declaration. |
| 7006 | RPK_Base, |
| 7007 | // Pointer is a member of the base declaration - 'this' |
| 7008 | RPK_Member, |
| 7009 | // Pointer is a reference and a member of the base declaration - 'this' |
| 7010 | RPK_MemberReference, |
| 7011 | }; |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7012 | OMPClauseMappableExprCommon::MappableExprComponentListRef Components; |
Alexey Bataev | f47c4b4 | 2017-09-26 13:47:31 +0000 | [diff] [blame] | 7013 | OpenMPMapClauseKind MapType = OMPC_MAP_unknown; |
| 7014 | OpenMPMapClauseKind MapTypeModifier = OMPC_MAP_unknown; |
| 7015 | ReturnPointerKind ReturnDevicePointer = RPK_None; |
| 7016 | bool IsImplicit = false; |
Hans Wennborg | bc1b58d | 2016-07-30 00:41:37 +0000 | [diff] [blame] | 7017 | |
Alexey Bataev | f47c4b4 | 2017-09-26 13:47:31 +0000 | [diff] [blame] | 7018 | MapInfo() = default; |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7019 | MapInfo( |
| 7020 | OMPClauseMappableExprCommon::MappableExprComponentListRef Components, |
| 7021 | OpenMPMapClauseKind MapType, OpenMPMapClauseKind MapTypeModifier, |
Alexey Bataev | f47c4b4 | 2017-09-26 13:47:31 +0000 | [diff] [blame] | 7022 | ReturnPointerKind ReturnDevicePointer, bool IsImplicit) |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7023 | : Components(Components), MapType(MapType), |
| 7024 | MapTypeModifier(MapTypeModifier), |
Alexey Bataev | f47c4b4 | 2017-09-26 13:47:31 +0000 | [diff] [blame] | 7025 | ReturnDevicePointer(ReturnDevicePointer), IsImplicit(IsImplicit) {} |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7026 | }; |
| 7027 | |
| 7028 | // We have to process the component lists that relate with the same |
| 7029 | // declaration in a single chunk so that we can generate the map flags |
| 7030 | // correctly. Therefore, we organize all lists in a map. |
Alexey Bataev | 5d1c3f6 | 2017-06-27 15:46:42 +0000 | [diff] [blame] | 7031 | llvm::MapVector<const ValueDecl *, SmallVector<MapInfo, 8>> Info; |
Samuel Antao | 8d2d730 | 2016-05-26 18:30:22 +0000 | [diff] [blame] | 7032 | |
| 7033 | // Helper function to fill the information map for the different supported |
| 7034 | // clauses. |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7035 | auto &&InfoGen = [&Info]( |
| 7036 | const ValueDecl *D, |
| 7037 | OMPClauseMappableExprCommon::MappableExprComponentListRef L, |
| 7038 | OpenMPMapClauseKind MapType, OpenMPMapClauseKind MapModifier, |
Alexey Bataev | f47c4b4 | 2017-09-26 13:47:31 +0000 | [diff] [blame] | 7039 | MapInfo::ReturnPointerKind ReturnDevicePointer, bool IsImplicit) { |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7040 | const ValueDecl *VD = |
| 7041 | D ? cast<ValueDecl>(D->getCanonicalDecl()) : nullptr; |
Alexey Bataev | f47c4b4 | 2017-09-26 13:47:31 +0000 | [diff] [blame] | 7042 | Info[VD].emplace_back(L, MapType, MapModifier, ReturnDevicePointer, |
| 7043 | IsImplicit); |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7044 | }; |
Samuel Antao | 8d2d730 | 2016-05-26 18:30:22 +0000 | [diff] [blame] | 7045 | |
Paul Robinson | 78fb132 | 2016-08-01 22:12:46 +0000 | [diff] [blame] | 7046 | // FIXME: MSVC 2013 seems to require this-> to find member CurDir. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 7047 | for (const auto *C : this->CurDir.getClausesOfKind<OMPMapClause>()) |
| 7048 | for (const auto &L : C->component_lists()) { |
Samuel Antao | cf3f83e | 2016-07-28 14:47:35 +0000 | [diff] [blame] | 7049 | InfoGen(L.first, L.second, C->getMapType(), C->getMapTypeModifier(), |
Alexey Bataev | f47c4b4 | 2017-09-26 13:47:31 +0000 | [diff] [blame] | 7050 | MapInfo::RPK_None, C->isImplicit()); |
| 7051 | } |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 7052 | for (const auto *C : this->CurDir.getClausesOfKind<OMPToClause>()) |
| 7053 | for (const auto &L : C->component_lists()) { |
Samuel Antao | cf3f83e | 2016-07-28 14:47:35 +0000 | [diff] [blame] | 7054 | InfoGen(L.first, L.second, OMPC_MAP_to, OMPC_MAP_unknown, |
Alexey Bataev | f47c4b4 | 2017-09-26 13:47:31 +0000 | [diff] [blame] | 7055 | MapInfo::RPK_None, C->isImplicit()); |
| 7056 | } |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 7057 | for (const auto *C : this->CurDir.getClausesOfKind<OMPFromClause>()) |
| 7058 | for (const auto &L : C->component_lists()) { |
Samuel Antao | cf3f83e | 2016-07-28 14:47:35 +0000 | [diff] [blame] | 7059 | InfoGen(L.first, L.second, OMPC_MAP_from, OMPC_MAP_unknown, |
Alexey Bataev | f47c4b4 | 2017-09-26 13:47:31 +0000 | [diff] [blame] | 7060 | MapInfo::RPK_None, C->isImplicit()); |
| 7061 | } |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7062 | |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7063 | // Look at the use_device_ptr clause information and mark the existing map |
| 7064 | // entries as such. If there is no map information for an entry in the |
| 7065 | // use_device_ptr list, we create one with map type 'alloc' and zero size |
| 7066 | // section. It is the user fault if that was not mapped before. |
Paul Robinson | 78fb132 | 2016-08-01 22:12:46 +0000 | [diff] [blame] | 7067 | // FIXME: MSVC 2013 seems to require this-> to find member CurDir. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 7068 | for (const auto *C : this->CurDir.getClausesOfKind<OMPUseDevicePtrClause>()) |
| 7069 | for (const auto &L : C->component_lists()) { |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7070 | assert(!L.second.empty() && "Not expecting empty list of components!"); |
| 7071 | const ValueDecl *VD = L.second.back().getAssociatedDeclaration(); |
| 7072 | VD = cast<ValueDecl>(VD->getCanonicalDecl()); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 7073 | const Expr *IE = L.second.back().getAssociatedExpression(); |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7074 | // If the first component is a member expression, we have to look into |
| 7075 | // 'this', which maps to null in the map of map information. Otherwise |
| 7076 | // look directly for the information. |
| 7077 | auto It = Info.find(isa<MemberExpr>(IE) ? nullptr : VD); |
| 7078 | |
| 7079 | // We potentially have map information for this declaration already. |
| 7080 | // Look for the first set of components that refer to it. |
| 7081 | if (It != Info.end()) { |
| 7082 | auto CI = std::find_if( |
| 7083 | It->second.begin(), It->second.end(), [VD](const MapInfo &MI) { |
| 7084 | return MI.Components.back().getAssociatedDeclaration() == VD; |
| 7085 | }); |
| 7086 | // If we found a map entry, signal that the pointer has to be returned |
| 7087 | // and move on to the next declaration. |
| 7088 | if (CI != It->second.end()) { |
| 7089 | CI->ReturnDevicePointer = isa<MemberExpr>(IE) |
| 7090 | ? (VD->getType()->isReferenceType() |
| 7091 | ? MapInfo::RPK_MemberReference |
| 7092 | : MapInfo::RPK_Member) |
| 7093 | : MapInfo::RPK_Base; |
| 7094 | continue; |
| 7095 | } |
| 7096 | } |
| 7097 | |
| 7098 | // We didn't find any match in our map information - generate a zero |
| 7099 | // size array section. |
Paul Robinson | 78fb132 | 2016-08-01 22:12:46 +0000 | [diff] [blame] | 7100 | // FIXME: MSVC 2013 seems to require this-> to find member CGF. |
Alexey Bataev | 1e49137 | 2018-01-23 18:44:14 +0000 | [diff] [blame] | 7101 | llvm::Value *Ptr = this->CGF.EmitLoadOfScalar(this->CGF.EmitLValue(IE), |
| 7102 | IE->getExprLoc()); |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7103 | BasePointers.push_back({Ptr, VD}); |
| 7104 | Pointers.push_back(Ptr); |
Paul Robinson | 15c8400 | 2016-07-29 20:46:16 +0000 | [diff] [blame] | 7105 | Sizes.push_back(llvm::Constant::getNullValue(this->CGF.SizeTy)); |
George Rokos | 065755d | 2017-11-07 18:27:04 +0000 | [diff] [blame] | 7106 | Types.push_back(OMP_MAP_RETURN_PARAM | OMP_MAP_TARGET_PARAM); |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7107 | } |
| 7108 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 7109 | for (const auto &M : Info) { |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7110 | // We need to know when we generate information for the first component |
| 7111 | // associated with a capture, because the mapping flags depend on it. |
| 7112 | bool IsFirstComponentList = true; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 7113 | for (const MapInfo &L : M.second) { |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7114 | assert(!L.Components.empty() && |
| 7115 | "Not expecting declaration with no component lists."); |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7116 | |
| 7117 | // Remember the current base pointer index. |
| 7118 | unsigned CurrentBasePointersIdx = BasePointers.size(); |
Paul Robinson | 78fb132 | 2016-08-01 22:12:46 +0000 | [diff] [blame] | 7119 | // FIXME: MSVC 2013 seems to require this-> to find the member method. |
Alexey Bataev | f47c4b4 | 2017-09-26 13:47:31 +0000 | [diff] [blame] | 7120 | this->generateInfoForComponentList( |
| 7121 | L.MapType, L.MapTypeModifier, L.Components, BasePointers, Pointers, |
| 7122 | Sizes, Types, IsFirstComponentList, L.IsImplicit); |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7123 | |
| 7124 | // If this entry relates with a device pointer, set the relevant |
| 7125 | // declaration and add the 'return pointer' flag. |
| 7126 | if (IsFirstComponentList && |
| 7127 | L.ReturnDevicePointer != MapInfo::RPK_None) { |
| 7128 | // If the pointer is not the base of the map, we need to skip the |
| 7129 | // base. If it is a reference in a member field, we also need to skip |
| 7130 | // the map of the reference. |
| 7131 | if (L.ReturnDevicePointer != MapInfo::RPK_Base) { |
| 7132 | ++CurrentBasePointersIdx; |
| 7133 | if (L.ReturnDevicePointer == MapInfo::RPK_MemberReference) |
| 7134 | ++CurrentBasePointersIdx; |
| 7135 | } |
| 7136 | assert(BasePointers.size() > CurrentBasePointersIdx && |
| 7137 | "Unexpected number of mapped base pointers."); |
| 7138 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 7139 | const ValueDecl *RelevantVD = |
| 7140 | L.Components.back().getAssociatedDeclaration(); |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7141 | assert(RelevantVD && |
| 7142 | "No relevant declaration related with device pointer??"); |
| 7143 | |
| 7144 | BasePointers[CurrentBasePointersIdx].setDevicePtrDecl(RelevantVD); |
George Rokos | 065755d | 2017-11-07 18:27:04 +0000 | [diff] [blame] | 7145 | Types[CurrentBasePointersIdx] |= OMP_MAP_RETURN_PARAM; |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7146 | } |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7147 | IsFirstComponentList = false; |
| 7148 | } |
| 7149 | } |
| 7150 | } |
| 7151 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 7152 | /// Generate the base pointers, section pointers, sizes and map types |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7153 | /// associated to a given capture. |
| 7154 | void generateInfoForCapture(const CapturedStmt::Capture *Cap, |
Samuel Antao | 6890b09 | 2016-07-28 14:25:09 +0000 | [diff] [blame] | 7155 | llvm::Value *Arg, |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7156 | MapBaseValuesArrayTy &BasePointers, |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7157 | MapValuesArrayTy &Pointers, |
| 7158 | MapValuesArrayTy &Sizes, |
| 7159 | MapFlagsArrayTy &Types) const { |
| 7160 | assert(!Cap->capturesVariableArrayType() && |
| 7161 | "Not expecting to generate map info for a variable array type!"); |
| 7162 | |
| 7163 | BasePointers.clear(); |
| 7164 | Pointers.clear(); |
| 7165 | Sizes.clear(); |
| 7166 | Types.clear(); |
| 7167 | |
Samuel Antao | 6890b09 | 2016-07-28 14:25:09 +0000 | [diff] [blame] | 7168 | // We need to know when we generating information for the first component |
| 7169 | // associated with a capture, because the mapping flags depend on it. |
| 7170 | bool IsFirstComponentList = true; |
| 7171 | |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7172 | const ValueDecl *VD = |
| 7173 | Cap->capturesThis() |
| 7174 | ? nullptr |
George Burgess IV | 00f70bd | 2018-03-01 05:43:23 +0000 | [diff] [blame] | 7175 | : Cap->getCapturedVar()->getCanonicalDecl(); |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7176 | |
Samuel Antao | 6890b09 | 2016-07-28 14:25:09 +0000 | [diff] [blame] | 7177 | // If this declaration appears in a is_device_ptr clause we just have to |
| 7178 | // pass the pointer by value. If it is a reference to a declaration, we just |
| 7179 | // pass its value, otherwise, if it is a member expression, we need to map |
| 7180 | // 'to' the field. |
| 7181 | if (!VD) { |
| 7182 | auto It = DevPointersMap.find(VD); |
| 7183 | if (It != DevPointersMap.end()) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 7184 | for (ArrayRef<OMPClauseMappableExprCommon::MappableComponent> L : |
| 7185 | It->second) { |
Samuel Antao | 6890b09 | 2016-07-28 14:25:09 +0000 | [diff] [blame] | 7186 | generateInfoForComponentList( |
| 7187 | /*MapType=*/OMPC_MAP_to, /*MapTypeModifier=*/OMPC_MAP_unknown, L, |
Alexey Bataev | f47c4b4 | 2017-09-26 13:47:31 +0000 | [diff] [blame] | 7188 | BasePointers, Pointers, Sizes, Types, IsFirstComponentList, |
| 7189 | /*IsImplicit=*/false); |
Samuel Antao | 6890b09 | 2016-07-28 14:25:09 +0000 | [diff] [blame] | 7190 | IsFirstComponentList = false; |
| 7191 | } |
| 7192 | return; |
| 7193 | } |
| 7194 | } else if (DevPointersMap.count(VD)) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 7195 | BasePointers.emplace_back(Arg, VD); |
Samuel Antao | 6890b09 | 2016-07-28 14:25:09 +0000 | [diff] [blame] | 7196 | Pointers.push_back(Arg); |
| 7197 | Sizes.push_back(CGF.getTypeSize(CGF.getContext().VoidPtrTy)); |
George Rokos | 065755d | 2017-11-07 18:27:04 +0000 | [diff] [blame] | 7198 | Types.push_back(OMP_MAP_LITERAL | OMP_MAP_TARGET_PARAM); |
Samuel Antao | 6890b09 | 2016-07-28 14:25:09 +0000 | [diff] [blame] | 7199 | return; |
| 7200 | } |
| 7201 | |
Paul Robinson | 78fb132 | 2016-08-01 22:12:46 +0000 | [diff] [blame] | 7202 | // FIXME: MSVC 2013 seems to require this-> to find member CurDir. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 7203 | for (const auto *C : this->CurDir.getClausesOfKind<OMPMapClause>()) |
| 7204 | for (const auto &L : C->decl_component_lists(VD)) { |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7205 | assert(L.first == VD && |
| 7206 | "We got information for the wrong declaration??"); |
| 7207 | assert(!L.second.empty() && |
| 7208 | "Not expecting declaration with no component lists."); |
Alexey Bataev | f47c4b4 | 2017-09-26 13:47:31 +0000 | [diff] [blame] | 7209 | generateInfoForComponentList( |
| 7210 | C->getMapType(), C->getMapTypeModifier(), L.second, BasePointers, |
| 7211 | Pointers, Sizes, Types, IsFirstComponentList, C->isImplicit()); |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7212 | IsFirstComponentList = false; |
| 7213 | } |
| 7214 | |
| 7215 | return; |
| 7216 | } |
Samuel Antao | d486f84 | 2016-05-26 16:53:38 +0000 | [diff] [blame] | 7217 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 7218 | /// Generate the default map information for a given capture \a CI, |
Samuel Antao | d486f84 | 2016-05-26 16:53:38 +0000 | [diff] [blame] | 7219 | /// record field declaration \a RI and captured value \a CV. |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7220 | void generateDefaultMapInfo(const CapturedStmt::Capture &CI, |
| 7221 | const FieldDecl &RI, llvm::Value *CV, |
| 7222 | MapBaseValuesArrayTy &CurBasePointers, |
| 7223 | MapValuesArrayTy &CurPointers, |
| 7224 | MapValuesArrayTy &CurSizes, |
| 7225 | MapFlagsArrayTy &CurMapTypes) { |
Samuel Antao | d486f84 | 2016-05-26 16:53:38 +0000 | [diff] [blame] | 7226 | |
| 7227 | // Do the default mapping. |
| 7228 | if (CI.capturesThis()) { |
| 7229 | CurBasePointers.push_back(CV); |
| 7230 | CurPointers.push_back(CV); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 7231 | const auto *PtrTy = cast<PointerType>(RI.getType().getTypePtr()); |
Samuel Antao | d486f84 | 2016-05-26 16:53:38 +0000 | [diff] [blame] | 7232 | CurSizes.push_back(CGF.getTypeSize(PtrTy->getPointeeType())); |
| 7233 | // Default map type. |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7234 | CurMapTypes.push_back(OMP_MAP_TO | OMP_MAP_FROM); |
Samuel Antao | d486f84 | 2016-05-26 16:53:38 +0000 | [diff] [blame] | 7235 | } else if (CI.capturesVariableByCopy()) { |
Samuel Antao | 6d00426 | 2016-06-16 18:39:34 +0000 | [diff] [blame] | 7236 | CurBasePointers.push_back(CV); |
| 7237 | CurPointers.push_back(CV); |
Samuel Antao | d486f84 | 2016-05-26 16:53:38 +0000 | [diff] [blame] | 7238 | if (!RI.getType()->isAnyPointerType()) { |
Samuel Antao | 6d00426 | 2016-06-16 18:39:34 +0000 | [diff] [blame] | 7239 | // We have to signal to the runtime captures passed by value that are |
| 7240 | // not pointers. |
George Rokos | 065755d | 2017-11-07 18:27:04 +0000 | [diff] [blame] | 7241 | CurMapTypes.push_back(OMP_MAP_LITERAL); |
Samuel Antao | d486f84 | 2016-05-26 16:53:38 +0000 | [diff] [blame] | 7242 | CurSizes.push_back(CGF.getTypeSize(RI.getType())); |
| 7243 | } else { |
| 7244 | // Pointers are implicitly mapped with a zero size and no flags |
| 7245 | // (other than first map that is added for all implicit maps). |
| 7246 | CurMapTypes.push_back(0u); |
Samuel Antao | d486f84 | 2016-05-26 16:53:38 +0000 | [diff] [blame] | 7247 | CurSizes.push_back(llvm::Constant::getNullValue(CGF.SizeTy)); |
| 7248 | } |
| 7249 | } else { |
| 7250 | assert(CI.capturesVariable() && "Expected captured reference."); |
| 7251 | CurBasePointers.push_back(CV); |
| 7252 | CurPointers.push_back(CV); |
| 7253 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 7254 | const auto *PtrTy = cast<ReferenceType>(RI.getType().getTypePtr()); |
Samuel Antao | d486f84 | 2016-05-26 16:53:38 +0000 | [diff] [blame] | 7255 | QualType ElementType = PtrTy->getPointeeType(); |
| 7256 | CurSizes.push_back(CGF.getTypeSize(ElementType)); |
| 7257 | // The default map type for a scalar/complex type is 'to' because by |
| 7258 | // default the value doesn't have to be retrieved. For an aggregate |
| 7259 | // type, the default is 'tofrom'. |
Alexey Bataev | 3f96fe6 | 2017-12-13 17:31:39 +0000 | [diff] [blame] | 7260 | CurMapTypes.emplace_back(adjustMapModifiersForPrivateClauses( |
| 7261 | CI, ElementType->isAggregateType() ? (OMP_MAP_TO | OMP_MAP_FROM) |
| 7262 | : OMP_MAP_TO)); |
Samuel Antao | d486f84 | 2016-05-26 16:53:38 +0000 | [diff] [blame] | 7263 | } |
George Rokos | 065755d | 2017-11-07 18:27:04 +0000 | [diff] [blame] | 7264 | // Every default map produces a single argument which is a target parameter. |
| 7265 | CurMapTypes.back() |= OMP_MAP_TARGET_PARAM; |
Samuel Antao | d486f84 | 2016-05-26 16:53:38 +0000 | [diff] [blame] | 7266 | } |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7267 | }; |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 7268 | |
| 7269 | enum OpenMPOffloadingReservedDeviceIDs { |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 7270 | /// Device ID if the device was not defined, runtime should get it |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 7271 | /// from environment variables in the spec. |
| 7272 | OMP_DEVICEID_UNDEF = -1, |
| 7273 | }; |
| 7274 | } // anonymous namespace |
| 7275 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 7276 | /// 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] | 7277 | /// offloading runtime library. If there is no map or capture information, |
| 7278 | /// return nullptr by reference. |
| 7279 | static void |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7280 | emitOffloadingArrays(CodeGenFunction &CGF, |
| 7281 | MappableExprsHandler::MapBaseValuesArrayTy &BasePointers, |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 7282 | MappableExprsHandler::MapValuesArrayTy &Pointers, |
| 7283 | MappableExprsHandler::MapValuesArrayTy &Sizes, |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7284 | MappableExprsHandler::MapFlagsArrayTy &MapTypes, |
| 7285 | CGOpenMPRuntime::TargetDataInfo &Info) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 7286 | CodeGenModule &CGM = CGF.CGM; |
| 7287 | ASTContext &Ctx = CGF.getContext(); |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 7288 | |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7289 | // Reset the array information. |
| 7290 | Info.clearArrayInfo(); |
| 7291 | Info.NumberOfPtrs = BasePointers.size(); |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 7292 | |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7293 | if (Info.NumberOfPtrs) { |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 7294 | // Detect if we have any capture size requiring runtime evaluation of the |
| 7295 | // size so that a constant array could be eventually used. |
| 7296 | bool hasRuntimeEvaluationCaptureSize = false; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 7297 | for (llvm::Value *S : Sizes) |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 7298 | if (!isa<llvm::Constant>(S)) { |
| 7299 | hasRuntimeEvaluationCaptureSize = true; |
| 7300 | break; |
| 7301 | } |
| 7302 | |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7303 | llvm::APInt PointerNumAP(32, Info.NumberOfPtrs, /*isSigned=*/true); |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 7304 | QualType PointerArrayType = |
| 7305 | Ctx.getConstantArrayType(Ctx.VoidPtrTy, PointerNumAP, ArrayType::Normal, |
| 7306 | /*IndexTypeQuals=*/0); |
| 7307 | |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7308 | Info.BasePointersArray = |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 7309 | CGF.CreateMemTemp(PointerArrayType, ".offload_baseptrs").getPointer(); |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7310 | Info.PointersArray = |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 7311 | CGF.CreateMemTemp(PointerArrayType, ".offload_ptrs").getPointer(); |
| 7312 | |
| 7313 | // If we don't have any VLA types or other types that require runtime |
| 7314 | // evaluation, we can use a constant array for the map sizes, otherwise we |
| 7315 | // need to fill up the arrays as we do for the pointers. |
| 7316 | if (hasRuntimeEvaluationCaptureSize) { |
| 7317 | QualType SizeArrayType = Ctx.getConstantArrayType( |
| 7318 | Ctx.getSizeType(), PointerNumAP, ArrayType::Normal, |
| 7319 | /*IndexTypeQuals=*/0); |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7320 | Info.SizesArray = |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 7321 | CGF.CreateMemTemp(SizeArrayType, ".offload_sizes").getPointer(); |
| 7322 | } else { |
| 7323 | // We expect all the sizes to be constant, so we collect them to create |
| 7324 | // a constant array. |
| 7325 | SmallVector<llvm::Constant *, 16> ConstSizes; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 7326 | for (llvm::Value *S : Sizes) |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 7327 | ConstSizes.push_back(cast<llvm::Constant>(S)); |
| 7328 | |
| 7329 | auto *SizesArrayInit = llvm::ConstantArray::get( |
| 7330 | llvm::ArrayType::get(CGM.SizeTy, ConstSizes.size()), ConstSizes); |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 7331 | std::string Name = CGM.getOpenMPRuntime().getName({"offload_sizes"}); |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 7332 | auto *SizesArrayGbl = new llvm::GlobalVariable( |
| 7333 | CGM.getModule(), SizesArrayInit->getType(), |
| 7334 | /*isConstant=*/true, llvm::GlobalValue::PrivateLinkage, |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 7335 | SizesArrayInit, Name); |
Peter Collingbourne | bcf909d | 2016-06-14 21:02:05 +0000 | [diff] [blame] | 7336 | SizesArrayGbl->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global); |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7337 | Info.SizesArray = SizesArrayGbl; |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 7338 | } |
| 7339 | |
| 7340 | // The map types are always constant so we don't need to generate code to |
| 7341 | // fill arrays. Instead, we create an array constant. |
| 7342 | llvm::Constant *MapTypesArrayInit = |
| 7343 | llvm::ConstantDataArray::get(CGF.Builder.getContext(), MapTypes); |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 7344 | std::string MaptypesName = |
| 7345 | CGM.getOpenMPRuntime().getName({"offload_maptypes"}); |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 7346 | auto *MapTypesArrayGbl = new llvm::GlobalVariable( |
| 7347 | CGM.getModule(), MapTypesArrayInit->getType(), |
| 7348 | /*isConstant=*/true, llvm::GlobalValue::PrivateLinkage, |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 7349 | MapTypesArrayInit, MaptypesName); |
Peter Collingbourne | bcf909d | 2016-06-14 21:02:05 +0000 | [diff] [blame] | 7350 | MapTypesArrayGbl->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global); |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7351 | Info.MapTypesArray = MapTypesArrayGbl; |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 7352 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 7353 | for (unsigned I = 0; I < Info.NumberOfPtrs; ++I) { |
| 7354 | llvm::Value *BPVal = *BasePointers[I]; |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 7355 | llvm::Value *BP = CGF.Builder.CreateConstInBoundsGEP2_32( |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7356 | llvm::ArrayType::get(CGM.VoidPtrTy, Info.NumberOfPtrs), |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 7357 | Info.BasePointersArray, 0, I); |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 7358 | BP = CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
| 7359 | BP, BPVal->getType()->getPointerTo(/*AddrSpace=*/0)); |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 7360 | Address BPAddr(BP, Ctx.getTypeAlignInChars(Ctx.VoidPtrTy)); |
| 7361 | CGF.Builder.CreateStore(BPVal, BPAddr); |
| 7362 | |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7363 | if (Info.requiresDevicePointerInfo()) |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 7364 | if (const ValueDecl *DevVD = BasePointers[I].getDevicePtrDecl()) |
Alexey Bataev | 43a919f | 2018-04-13 17:48:43 +0000 | [diff] [blame] | 7365 | Info.CaptureDeviceAddrMap.try_emplace(DevVD, BPAddr); |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7366 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 7367 | llvm::Value *PVal = Pointers[I]; |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 7368 | llvm::Value *P = CGF.Builder.CreateConstInBoundsGEP2_32( |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7369 | llvm::ArrayType::get(CGM.VoidPtrTy, Info.NumberOfPtrs), |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 7370 | Info.PointersArray, 0, I); |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 7371 | P = CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
| 7372 | P, PVal->getType()->getPointerTo(/*AddrSpace=*/0)); |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 7373 | Address PAddr(P, Ctx.getTypeAlignInChars(Ctx.VoidPtrTy)); |
| 7374 | CGF.Builder.CreateStore(PVal, PAddr); |
| 7375 | |
| 7376 | if (hasRuntimeEvaluationCaptureSize) { |
| 7377 | llvm::Value *S = CGF.Builder.CreateConstInBoundsGEP2_32( |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7378 | llvm::ArrayType::get(CGM.SizeTy, Info.NumberOfPtrs), |
| 7379 | Info.SizesArray, |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 7380 | /*Idx0=*/0, |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 7381 | /*Idx1=*/I); |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 7382 | Address SAddr(S, Ctx.getTypeAlignInChars(Ctx.getSizeType())); |
| 7383 | CGF.Builder.CreateStore( |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 7384 | CGF.Builder.CreateIntCast(Sizes[I], CGM.SizeTy, /*isSigned=*/true), |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 7385 | SAddr); |
| 7386 | } |
| 7387 | } |
| 7388 | } |
| 7389 | } |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 7390 | /// 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] | 7391 | /// arrays of pointers, sizes and map types. |
| 7392 | static void emitOffloadingArraysArgument( |
| 7393 | CodeGenFunction &CGF, llvm::Value *&BasePointersArrayArg, |
| 7394 | llvm::Value *&PointersArrayArg, llvm::Value *&SizesArrayArg, |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7395 | llvm::Value *&MapTypesArrayArg, CGOpenMPRuntime::TargetDataInfo &Info) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 7396 | CodeGenModule &CGM = CGF.CGM; |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7397 | if (Info.NumberOfPtrs) { |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 7398 | BasePointersArrayArg = CGF.Builder.CreateConstInBoundsGEP2_32( |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7399 | llvm::ArrayType::get(CGM.VoidPtrTy, Info.NumberOfPtrs), |
| 7400 | Info.BasePointersArray, |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 7401 | /*Idx0=*/0, /*Idx1=*/0); |
| 7402 | PointersArrayArg = CGF.Builder.CreateConstInBoundsGEP2_32( |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7403 | llvm::ArrayType::get(CGM.VoidPtrTy, Info.NumberOfPtrs), |
| 7404 | Info.PointersArray, |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 7405 | /*Idx0=*/0, |
| 7406 | /*Idx1=*/0); |
| 7407 | SizesArrayArg = CGF.Builder.CreateConstInBoundsGEP2_32( |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7408 | llvm::ArrayType::get(CGM.SizeTy, Info.NumberOfPtrs), Info.SizesArray, |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 7409 | /*Idx0=*/0, /*Idx1=*/0); |
| 7410 | MapTypesArrayArg = CGF.Builder.CreateConstInBoundsGEP2_32( |
George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 7411 | llvm::ArrayType::get(CGM.Int64Ty, Info.NumberOfPtrs), |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7412 | Info.MapTypesArray, |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 7413 | /*Idx0=*/0, |
| 7414 | /*Idx1=*/0); |
| 7415 | } else { |
| 7416 | BasePointersArrayArg = llvm::ConstantPointerNull::get(CGM.VoidPtrPtrTy); |
| 7417 | PointersArrayArg = llvm::ConstantPointerNull::get(CGM.VoidPtrPtrTy); |
| 7418 | SizesArrayArg = llvm::ConstantPointerNull::get(CGM.SizeTy->getPointerTo()); |
| 7419 | MapTypesArrayArg = |
George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 7420 | llvm::ConstantPointerNull::get(CGM.Int64Ty->getPointerTo()); |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 7421 | } |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7422 | } |
| 7423 | |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 7424 | void CGOpenMPRuntime::emitTargetCall(CodeGenFunction &CGF, |
| 7425 | const OMPExecutableDirective &D, |
| 7426 | llvm::Value *OutlinedFn, |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 7427 | llvm::Value *OutlinedFnID, |
Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 7428 | const Expr *IfCond, const Expr *Device) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 7429 | if (!CGF.HaveInsertPoint()) |
| 7430 | return; |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 7431 | |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 7432 | assert(OutlinedFn && "Invalid outlined function!"); |
| 7433 | |
Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 7434 | const bool RequiresOuterTask = D.hasClausesOfKind<OMPDependClause>(); |
| 7435 | llvm::SmallVector<llvm::Value *, 16> CapturedVars; |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 7436 | const CapturedStmt &CS = *D.getCapturedStmt(OMPD_target); |
Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 7437 | auto &&ArgsCodegen = [&CS, &CapturedVars](CodeGenFunction &CGF, |
| 7438 | PrePostActionTy &) { |
| 7439 | CGF.GenerateOpenMPCapturedVars(CS, CapturedVars); |
| 7440 | }; |
| 7441 | emitInlinedDirective(CGF, OMPD_unknown, ArgsCodegen); |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7442 | |
Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 7443 | CodeGenFunction::OMPTargetDataInfo InputInfo; |
| 7444 | llvm::Value *MapTypesArray = nullptr; |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 7445 | // Fill up the pointer arrays and transfer execution to the device. |
Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 7446 | auto &&ThenGen = [this, Device, OutlinedFn, OutlinedFnID, &D, &InputInfo, |
| 7447 | &MapTypesArray, &CS, RequiresOuterTask, |
| 7448 | &CapturedVars](CodeGenFunction &CGF, PrePostActionTy &) { |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 7449 | // On top of the arrays that were filled up, the target offloading call |
| 7450 | // takes as arguments the device id as well as the host pointer. The host |
| 7451 | // pointer is used by the runtime library to identify the current target |
| 7452 | // region, so it only has to be unique and not necessarily point to |
| 7453 | // anything. It could be the pointer to the outlined function that |
| 7454 | // implements the target region, but we aren't using that so that the |
| 7455 | // compiler doesn't need to keep that, and could therefore inline the host |
| 7456 | // function if proven worthwhile during optimization. |
| 7457 | |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 7458 | // From this point on, we need to have an ID of the target region defined. |
| 7459 | assert(OutlinedFnID && "Invalid outlined function ID!"); |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 7460 | |
| 7461 | // Emit device ID if any. |
| 7462 | llvm::Value *DeviceID; |
George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 7463 | if (Device) { |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 7464 | DeviceID = CGF.Builder.CreateIntCast(CGF.EmitScalarExpr(Device), |
George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 7465 | CGF.Int64Ty, /*isSigned=*/true); |
| 7466 | } else { |
| 7467 | DeviceID = CGF.Builder.getInt64(OMP_DEVICEID_UNDEF); |
| 7468 | } |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 7469 | |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 7470 | // Emit the number of elements in the offloading arrays. |
Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 7471 | llvm::Value *PointerNum = |
| 7472 | CGF.Builder.getInt32(InputInfo.NumberOfTargetItems); |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 7473 | |
Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 7474 | // Return value of the runtime offloading call. |
| 7475 | llvm::Value *Return; |
| 7476 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 7477 | llvm::Value *NumTeams = emitNumTeamsForTargetDirective(*this, CGF, D); |
| 7478 | llvm::Value *NumThreads = emitNumThreadsForTargetDirective(*this, CGF, D); |
Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 7479 | |
Alexey Bataev | a9f77c6 | 2017-12-13 21:04:20 +0000 | [diff] [blame] | 7480 | bool HasNowait = D.hasClausesOfKind<OMPNowaitClause>(); |
Arpith Chacko Jacob | 33c849a | 2017-01-25 00:57:16 +0000 | [diff] [blame] | 7481 | // The target region is an outlined function launched by the runtime |
| 7482 | // via calls __tgt_target() or __tgt_target_teams(). |
| 7483 | // |
| 7484 | // __tgt_target() launches a target region with one team and one thread, |
| 7485 | // executing a serial region. This master thread may in turn launch |
| 7486 | // more threads within its team upon encountering a parallel region, |
| 7487 | // however, no additional teams can be launched on the device. |
| 7488 | // |
| 7489 | // __tgt_target_teams() launches a target region with one or more teams, |
| 7490 | // each with one or more threads. This call is required for target |
| 7491 | // constructs such as: |
| 7492 | // 'target teams' |
| 7493 | // 'target' / 'teams' |
| 7494 | // 'target teams distribute parallel for' |
| 7495 | // 'target parallel' |
| 7496 | // and so on. |
| 7497 | // |
| 7498 | // Note that on the host and CPU targets, the runtime implementation of |
| 7499 | // these calls simply call the outlined function without forking threads. |
| 7500 | // The outlined functions themselves have runtime calls to |
| 7501 | // __kmpc_fork_teams() and __kmpc_fork() for this purpose, codegen'd by |
| 7502 | // the compiler in emitTeamsCall() and emitParallelCall(). |
| 7503 | // |
| 7504 | // In contrast, on the NVPTX target, the implementation of |
| 7505 | // __tgt_target_teams() launches a GPU kernel with the requested number |
| 7506 | // 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] | 7507 | if (NumTeams) { |
Arpith Chacko Jacob | 33c849a | 2017-01-25 00:57:16 +0000 | [diff] [blame] | 7508 | // If we have NumTeams defined this means that we have an enclosed teams |
| 7509 | // region. Therefore we also expect to have NumThreads defined. These two |
| 7510 | // values should be defined in the presence of a teams directive, |
| 7511 | // regardless of having any clauses associated. If the user is using teams |
| 7512 | // but no clauses, these two values will be the default that should be |
| 7513 | // passed to the runtime library - a 32-bit integer with the value zero. |
| 7514 | assert(NumThreads && "Thread limit expression should be available along " |
| 7515 | "with number of teams."); |
Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 7516 | llvm::Value *OffloadingArgs[] = {DeviceID, |
| 7517 | OutlinedFnID, |
| 7518 | PointerNum, |
| 7519 | InputInfo.BasePointersArray.getPointer(), |
| 7520 | InputInfo.PointersArray.getPointer(), |
| 7521 | InputInfo.SizesArray.getPointer(), |
| 7522 | MapTypesArray, |
| 7523 | NumTeams, |
| 7524 | NumThreads}; |
Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 7525 | Return = CGF.EmitRuntimeCall( |
Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 7526 | createRuntimeFunction(HasNowait ? OMPRTL__tgt_target_teams_nowait |
| 7527 | : OMPRTL__tgt_target_teams), |
Alexey Bataev | a9f77c6 | 2017-12-13 21:04:20 +0000 | [diff] [blame] | 7528 | OffloadingArgs); |
Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 7529 | } else { |
Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 7530 | llvm::Value *OffloadingArgs[] = {DeviceID, |
| 7531 | OutlinedFnID, |
| 7532 | PointerNum, |
| 7533 | InputInfo.BasePointersArray.getPointer(), |
| 7534 | InputInfo.PointersArray.getPointer(), |
| 7535 | InputInfo.SizesArray.getPointer(), |
| 7536 | MapTypesArray}; |
Alexey Bataev | a9f77c6 | 2017-12-13 21:04:20 +0000 | [diff] [blame] | 7537 | Return = CGF.EmitRuntimeCall( |
Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 7538 | createRuntimeFunction(HasNowait ? OMPRTL__tgt_target_nowait |
| 7539 | : OMPRTL__tgt_target), |
Alexey Bataev | a9f77c6 | 2017-12-13 21:04:20 +0000 | [diff] [blame] | 7540 | OffloadingArgs); |
Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 7541 | } |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 7542 | |
Alexey Bataev | 2a007e0 | 2017-10-02 14:20:58 +0000 | [diff] [blame] | 7543 | // Check the error code and execute the host version if required. |
| 7544 | llvm::BasicBlock *OffloadFailedBlock = |
| 7545 | CGF.createBasicBlock("omp_offload.failed"); |
| 7546 | llvm::BasicBlock *OffloadContBlock = |
| 7547 | CGF.createBasicBlock("omp_offload.cont"); |
| 7548 | llvm::Value *Failed = CGF.Builder.CreateIsNotNull(Return); |
| 7549 | CGF.Builder.CreateCondBr(Failed, OffloadFailedBlock, OffloadContBlock); |
| 7550 | |
| 7551 | CGF.EmitBlock(OffloadFailedBlock); |
Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 7552 | if (RequiresOuterTask) { |
| 7553 | CapturedVars.clear(); |
| 7554 | CGF.GenerateOpenMPCapturedVars(CS, CapturedVars); |
| 7555 | } |
| 7556 | emitOutlinedFunctionCall(CGF, D.getLocStart(), OutlinedFn, CapturedVars); |
Alexey Bataev | 2a007e0 | 2017-10-02 14:20:58 +0000 | [diff] [blame] | 7557 | CGF.EmitBranch(OffloadContBlock); |
| 7558 | |
| 7559 | CGF.EmitBlock(OffloadContBlock, /*IsFinished=*/true); |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 7560 | }; |
| 7561 | |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 7562 | // Notify that the host version must be executed. |
Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 7563 | auto &&ElseGen = [this, &D, OutlinedFn, &CS, &CapturedVars, |
| 7564 | RequiresOuterTask](CodeGenFunction &CGF, |
| 7565 | PrePostActionTy &) { |
| 7566 | if (RequiresOuterTask) { |
| 7567 | CapturedVars.clear(); |
| 7568 | CGF.GenerateOpenMPCapturedVars(CS, CapturedVars); |
| 7569 | } |
| 7570 | emitOutlinedFunctionCall(CGF, D.getLocStart(), OutlinedFn, CapturedVars); |
| 7571 | }; |
| 7572 | |
| 7573 | auto &&TargetThenGen = [this, &ThenGen, &D, &InputInfo, &MapTypesArray, |
| 7574 | &CapturedVars, RequiresOuterTask, |
| 7575 | &CS](CodeGenFunction &CGF, PrePostActionTy &) { |
| 7576 | // Fill up the arrays with all the captured variables. |
| 7577 | MappableExprsHandler::MapBaseValuesArrayTy BasePointers; |
| 7578 | MappableExprsHandler::MapValuesArrayTy Pointers; |
| 7579 | MappableExprsHandler::MapValuesArrayTy Sizes; |
| 7580 | MappableExprsHandler::MapFlagsArrayTy MapTypes; |
| 7581 | |
| 7582 | MappableExprsHandler::MapBaseValuesArrayTy CurBasePointers; |
| 7583 | MappableExprsHandler::MapValuesArrayTy CurPointers; |
| 7584 | MappableExprsHandler::MapValuesArrayTy CurSizes; |
| 7585 | MappableExprsHandler::MapFlagsArrayTy CurMapTypes; |
| 7586 | |
| 7587 | // Get mappable expression information. |
| 7588 | MappableExprsHandler MEHandler(D, CGF); |
| 7589 | |
| 7590 | auto RI = CS.getCapturedRecordDecl()->field_begin(); |
| 7591 | auto CV = CapturedVars.begin(); |
| 7592 | for (CapturedStmt::const_capture_iterator CI = CS.capture_begin(), |
| 7593 | CE = CS.capture_end(); |
| 7594 | CI != CE; ++CI, ++RI, ++CV) { |
| 7595 | CurBasePointers.clear(); |
| 7596 | CurPointers.clear(); |
| 7597 | CurSizes.clear(); |
| 7598 | CurMapTypes.clear(); |
| 7599 | |
| 7600 | // VLA sizes are passed to the outlined region by copy and do not have map |
| 7601 | // information associated. |
| 7602 | if (CI->capturesVariableArrayType()) { |
| 7603 | CurBasePointers.push_back(*CV); |
| 7604 | CurPointers.push_back(*CV); |
| 7605 | CurSizes.push_back(CGF.getTypeSize(RI->getType())); |
| 7606 | // Copy to the device as an argument. No need to retrieve it. |
| 7607 | CurMapTypes.push_back(MappableExprsHandler::OMP_MAP_LITERAL | |
| 7608 | MappableExprsHandler::OMP_MAP_TARGET_PARAM); |
| 7609 | } else { |
| 7610 | // If we have any information in the map clause, we use it, otherwise we |
| 7611 | // just do a default mapping. |
| 7612 | MEHandler.generateInfoForCapture(CI, *CV, CurBasePointers, CurPointers, |
| 7613 | CurSizes, CurMapTypes); |
| 7614 | if (CurBasePointers.empty()) |
| 7615 | MEHandler.generateDefaultMapInfo(*CI, **RI, *CV, CurBasePointers, |
| 7616 | CurPointers, CurSizes, CurMapTypes); |
| 7617 | } |
| 7618 | // We expect to have at least an element of information for this capture. |
| 7619 | assert(!CurBasePointers.empty() && |
| 7620 | "Non-existing map pointer for capture!"); |
| 7621 | assert(CurBasePointers.size() == CurPointers.size() && |
| 7622 | CurBasePointers.size() == CurSizes.size() && |
| 7623 | CurBasePointers.size() == CurMapTypes.size() && |
| 7624 | "Inconsistent map information sizes!"); |
| 7625 | |
| 7626 | // We need to append the results of this capture to what we already have. |
| 7627 | BasePointers.append(CurBasePointers.begin(), CurBasePointers.end()); |
| 7628 | Pointers.append(CurPointers.begin(), CurPointers.end()); |
| 7629 | Sizes.append(CurSizes.begin(), CurSizes.end()); |
| 7630 | MapTypes.append(CurMapTypes.begin(), CurMapTypes.end()); |
| 7631 | } |
Alexey Bataev | 92327c5 | 2018-03-26 16:40:55 +0000 | [diff] [blame] | 7632 | // Map other list items in the map clause which are not captured variables |
| 7633 | // but "declare target link" global variables. |
| 7634 | for (const auto *C : D.getClausesOfKind<OMPMapClause>()) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 7635 | for (const auto &L : C->component_lists()) { |
Alexey Bataev | 92327c5 | 2018-03-26 16:40:55 +0000 | [diff] [blame] | 7636 | if (!L.first) |
| 7637 | continue; |
| 7638 | const auto *VD = dyn_cast<VarDecl>(L.first); |
| 7639 | if (!VD) |
| 7640 | continue; |
| 7641 | llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res = |
| 7642 | isDeclareTargetDeclaration(VD); |
| 7643 | if (!Res || *Res != OMPDeclareTargetDeclAttr::MT_Link) |
| 7644 | continue; |
| 7645 | MEHandler.generateInfoForComponentList( |
| 7646 | C->getMapType(), C->getMapTypeModifier(), L.second, BasePointers, |
| 7647 | Pointers, Sizes, MapTypes, /*IsFirstComponentList=*/true, |
| 7648 | C->isImplicit()); |
| 7649 | } |
| 7650 | } |
Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 7651 | |
| 7652 | TargetDataInfo Info; |
| 7653 | // Fill up the arrays and create the arguments. |
| 7654 | emitOffloadingArrays(CGF, BasePointers, Pointers, Sizes, MapTypes, Info); |
| 7655 | emitOffloadingArraysArgument(CGF, Info.BasePointersArray, |
| 7656 | Info.PointersArray, Info.SizesArray, |
| 7657 | Info.MapTypesArray, Info); |
| 7658 | InputInfo.NumberOfTargetItems = Info.NumberOfPtrs; |
| 7659 | InputInfo.BasePointersArray = |
| 7660 | Address(Info.BasePointersArray, CGM.getPointerAlign()); |
| 7661 | InputInfo.PointersArray = |
| 7662 | Address(Info.PointersArray, CGM.getPointerAlign()); |
| 7663 | InputInfo.SizesArray = Address(Info.SizesArray, CGM.getPointerAlign()); |
| 7664 | MapTypesArray = Info.MapTypesArray; |
| 7665 | if (RequiresOuterTask) |
| 7666 | CGF.EmitOMPTargetTaskBasedDirective(D, ThenGen, InputInfo); |
| 7667 | else |
| 7668 | emitInlinedDirective(CGF, D.getDirectiveKind(), ThenGen); |
| 7669 | }; |
| 7670 | |
| 7671 | auto &&TargetElseGen = [this, &ElseGen, &D, RequiresOuterTask]( |
| 7672 | CodeGenFunction &CGF, PrePostActionTy &) { |
| 7673 | if (RequiresOuterTask) { |
| 7674 | CodeGenFunction::OMPTargetDataInfo InputInfo; |
| 7675 | CGF.EmitOMPTargetTaskBasedDirective(D, ElseGen, InputInfo); |
| 7676 | } else { |
| 7677 | emitInlinedDirective(CGF, D.getDirectiveKind(), ElseGen); |
| 7678 | } |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 7679 | }; |
| 7680 | |
| 7681 | // If we have a target function ID it means that we need to support |
| 7682 | // offloading, otherwise, just execute on the host. We need to execute on host |
| 7683 | // regardless of the conditional in the if clause if, e.g., the user do not |
| 7684 | // specify target triples. |
| 7685 | if (OutlinedFnID) { |
Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 7686 | if (IfCond) { |
| 7687 | emitOMPIfClause(CGF, IfCond, TargetThenGen, TargetElseGen); |
| 7688 | } else { |
| 7689 | RegionCodeGenTy ThenRCG(TargetThenGen); |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 7690 | ThenRCG(CGF); |
Alexey Bataev | f539faa | 2016-03-28 12:58:34 +0000 | [diff] [blame] | 7691 | } |
| 7692 | } else { |
Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 7693 | RegionCodeGenTy ElseRCG(TargetElseGen); |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 7694 | ElseRCG(CGF); |
Alexey Bataev | f539faa | 2016-03-28 12:58:34 +0000 | [diff] [blame] | 7695 | } |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 7696 | } |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 7697 | |
| 7698 | void CGOpenMPRuntime::scanForTargetRegionsFunctions(const Stmt *S, |
| 7699 | StringRef ParentName) { |
| 7700 | if (!S) |
| 7701 | return; |
| 7702 | |
Arpith Chacko Jacob | 43a8b7b | 2017-01-16 15:26:02 +0000 | [diff] [blame] | 7703 | // Codegen OMP target directives that offload compute to the device. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 7704 | bool RequiresDeviceCodegen = |
Arpith Chacko Jacob | 43a8b7b | 2017-01-16 15:26:02 +0000 | [diff] [blame] | 7705 | isa<OMPExecutableDirective>(S) && |
| 7706 | isOpenMPTargetExecutionDirective( |
| 7707 | cast<OMPExecutableDirective>(S)->getDirectiveKind()); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 7708 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 7709 | if (RequiresDeviceCodegen) { |
| 7710 | const auto &E = *cast<OMPExecutableDirective>(S); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 7711 | unsigned DeviceID; |
| 7712 | unsigned FileID; |
| 7713 | unsigned Line; |
Arpith Chacko Jacob | 43a8b7b | 2017-01-16 15:26:02 +0000 | [diff] [blame] | 7714 | getTargetEntryUniqueInfo(CGM.getContext(), E.getLocStart(), DeviceID, |
Samuel Antao | 2de62b0 | 2016-02-13 23:35:10 +0000 | [diff] [blame] | 7715 | FileID, Line); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 7716 | |
| 7717 | // Is this a target region that should not be emitted as an entry point? If |
| 7718 | // so just signal we are done with this target region. |
Samuel Antao | 2de62b0 | 2016-02-13 23:35:10 +0000 | [diff] [blame] | 7719 | if (!OffloadEntriesInfoManager.hasTargetRegionEntryInfo(DeviceID, FileID, |
| 7720 | ParentName, Line)) |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 7721 | return; |
| 7722 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 7723 | switch (E.getDirectiveKind()) { |
| 7724 | case OMPD_target: |
| 7725 | CodeGenFunction::EmitOMPTargetDeviceFunction(CGM, ParentName, |
| 7726 | cast<OMPTargetDirective>(E)); |
Arpith Chacko Jacob | 43a8b7b | 2017-01-16 15:26:02 +0000 | [diff] [blame] | 7727 | break; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 7728 | case OMPD_target_parallel: |
Arpith Chacko Jacob | 19b911c | 2017-01-18 18:18:53 +0000 | [diff] [blame] | 7729 | CodeGenFunction::EmitOMPTargetParallelDeviceFunction( |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 7730 | CGM, ParentName, cast<OMPTargetParallelDirective>(E)); |
Arpith Chacko Jacob | 19b911c | 2017-01-18 18:18:53 +0000 | [diff] [blame] | 7731 | break; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 7732 | case OMPD_target_teams: |
Arpith Chacko Jacob | 99a1e0e | 2017-01-25 02:18:43 +0000 | [diff] [blame] | 7733 | CodeGenFunction::EmitOMPTargetTeamsDeviceFunction( |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 7734 | CGM, ParentName, cast<OMPTargetTeamsDirective>(E)); |
Arpith Chacko Jacob | 99a1e0e | 2017-01-25 02:18:43 +0000 | [diff] [blame] | 7735 | break; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 7736 | case OMPD_target_teams_distribute: |
Alexey Bataev | dfa430f | 2017-12-08 15:03:50 +0000 | [diff] [blame] | 7737 | CodeGenFunction::EmitOMPTargetTeamsDistributeDeviceFunction( |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 7738 | CGM, ParentName, cast<OMPTargetTeamsDistributeDirective>(E)); |
Alexey Bataev | dfa430f | 2017-12-08 15:03:50 +0000 | [diff] [blame] | 7739 | break; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 7740 | case OMPD_target_teams_distribute_simd: |
Alexey Bataev | fbe17fb | 2017-12-13 19:45:06 +0000 | [diff] [blame] | 7741 | CodeGenFunction::EmitOMPTargetTeamsDistributeSimdDeviceFunction( |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 7742 | CGM, ParentName, cast<OMPTargetTeamsDistributeSimdDirective>(E)); |
Alexey Bataev | fbe17fb | 2017-12-13 19:45:06 +0000 | [diff] [blame] | 7743 | break; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 7744 | case OMPD_target_parallel_for: |
Alexey Bataev | fb0ebec | 2017-11-08 20:16:14 +0000 | [diff] [blame] | 7745 | CodeGenFunction::EmitOMPTargetParallelForDeviceFunction( |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 7746 | CGM, ParentName, cast<OMPTargetParallelForDirective>(E)); |
Alexey Bataev | fb0ebec | 2017-11-08 20:16:14 +0000 | [diff] [blame] | 7747 | break; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 7748 | case OMPD_target_parallel_for_simd: |
Alexey Bataev | 5d7edca | 2017-11-09 17:32:15 +0000 | [diff] [blame] | 7749 | CodeGenFunction::EmitOMPTargetParallelForSimdDeviceFunction( |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 7750 | CGM, ParentName, cast<OMPTargetParallelForSimdDirective>(E)); |
Alexey Bataev | 5d7edca | 2017-11-09 17:32:15 +0000 | [diff] [blame] | 7751 | break; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 7752 | case OMPD_target_simd: |
Alexey Bataev | f836537 | 2017-11-17 17:57:25 +0000 | [diff] [blame] | 7753 | CodeGenFunction::EmitOMPTargetSimdDeviceFunction( |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 7754 | CGM, ParentName, cast<OMPTargetSimdDirective>(E)); |
Alexey Bataev | f836537 | 2017-11-17 17:57:25 +0000 | [diff] [blame] | 7755 | break; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 7756 | case OMPD_target_teams_distribute_parallel_for: |
Carlo Bertolli | 52978c3 | 2018-01-03 21:12:44 +0000 | [diff] [blame] | 7757 | CodeGenFunction::EmitOMPTargetTeamsDistributeParallelForDeviceFunction( |
| 7758 | CGM, ParentName, |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 7759 | cast<OMPTargetTeamsDistributeParallelForDirective>(E)); |
Carlo Bertolli | 52978c3 | 2018-01-03 21:12:44 +0000 | [diff] [blame] | 7760 | break; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 7761 | case OMPD_target_teams_distribute_parallel_for_simd: |
Alexey Bataev | 647dd84 | 2018-01-15 20:59:40 +0000 | [diff] [blame] | 7762 | CodeGenFunction:: |
| 7763 | EmitOMPTargetTeamsDistributeParallelForSimdDeviceFunction( |
| 7764 | CGM, ParentName, |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 7765 | cast<OMPTargetTeamsDistributeParallelForSimdDirective>(E)); |
Alexey Bataev | 647dd84 | 2018-01-15 20:59:40 +0000 | [diff] [blame] | 7766 | break; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 7767 | case OMPD_parallel: |
| 7768 | case OMPD_for: |
| 7769 | case OMPD_parallel_for: |
| 7770 | case OMPD_parallel_sections: |
| 7771 | case OMPD_for_simd: |
| 7772 | case OMPD_parallel_for_simd: |
| 7773 | case OMPD_cancel: |
| 7774 | case OMPD_cancellation_point: |
| 7775 | case OMPD_ordered: |
| 7776 | case OMPD_threadprivate: |
| 7777 | case OMPD_task: |
| 7778 | case OMPD_simd: |
| 7779 | case OMPD_sections: |
| 7780 | case OMPD_section: |
| 7781 | case OMPD_single: |
| 7782 | case OMPD_master: |
| 7783 | case OMPD_critical: |
| 7784 | case OMPD_taskyield: |
| 7785 | case OMPD_barrier: |
| 7786 | case OMPD_taskwait: |
| 7787 | case OMPD_taskgroup: |
| 7788 | case OMPD_atomic: |
| 7789 | case OMPD_flush: |
| 7790 | case OMPD_teams: |
| 7791 | case OMPD_target_data: |
| 7792 | case OMPD_target_exit_data: |
| 7793 | case OMPD_target_enter_data: |
| 7794 | case OMPD_distribute: |
| 7795 | case OMPD_distribute_simd: |
| 7796 | case OMPD_distribute_parallel_for: |
| 7797 | case OMPD_distribute_parallel_for_simd: |
| 7798 | case OMPD_teams_distribute: |
| 7799 | case OMPD_teams_distribute_simd: |
| 7800 | case OMPD_teams_distribute_parallel_for: |
| 7801 | case OMPD_teams_distribute_parallel_for_simd: |
| 7802 | case OMPD_target_update: |
| 7803 | case OMPD_declare_simd: |
| 7804 | case OMPD_declare_target: |
| 7805 | case OMPD_end_declare_target: |
| 7806 | case OMPD_declare_reduction: |
| 7807 | case OMPD_taskloop: |
| 7808 | case OMPD_taskloop_simd: |
| 7809 | case OMPD_unknown: |
Arpith Chacko Jacob | 43a8b7b | 2017-01-16 15:26:02 +0000 | [diff] [blame] | 7810 | llvm_unreachable("Unknown target directive for OpenMP device codegen."); |
| 7811 | } |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 7812 | return; |
| 7813 | } |
| 7814 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 7815 | if (const auto *E = dyn_cast<OMPExecutableDirective>(S)) { |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 7816 | if (!E->hasAssociatedStmt() || !E->getAssociatedStmt()) |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 7817 | return; |
| 7818 | |
| 7819 | scanForTargetRegionsFunctions( |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 7820 | E->getInnermostCapturedStmt()->getCapturedStmt(), ParentName); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 7821 | return; |
| 7822 | } |
| 7823 | |
| 7824 | // If this is a lambda function, look into its body. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 7825 | if (const auto *L = dyn_cast<LambdaExpr>(S)) |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 7826 | S = L->getBody(); |
| 7827 | |
| 7828 | // Keep looking for target regions recursively. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 7829 | for (const Stmt *II : S->children()) |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 7830 | scanForTargetRegionsFunctions(II, ParentName); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 7831 | } |
| 7832 | |
| 7833 | bool CGOpenMPRuntime::emitTargetFunctions(GlobalDecl GD) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 7834 | const auto *FD = cast<FunctionDecl>(GD.getDecl()); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 7835 | |
| 7836 | // If emitting code for the host, we do not process FD here. Instead we do |
| 7837 | // the normal code generation. |
| 7838 | if (!CGM.getLangOpts().OpenMPIsDevice) |
| 7839 | return false; |
| 7840 | |
| 7841 | // Try to detect target regions in the function. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 7842 | scanForTargetRegionsFunctions(FD->getBody(), CGM.getMangledName(GD)); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 7843 | |
Alexey Bataev | 4f4bf7c | 2018-03-15 15:47:20 +0000 | [diff] [blame] | 7844 | // Do not to emit function if it is not marked as declare target. |
Alexey Bataev | fb38828 | 2018-05-01 14:09:46 +0000 | [diff] [blame] | 7845 | return !isDeclareTargetDeclaration(FD) && |
| 7846 | AlreadyEmittedTargetFunctions.count(FD->getCanonicalDecl()) == 0; |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 7847 | } |
| 7848 | |
| 7849 | bool CGOpenMPRuntime::emitTargetGlobalVariable(GlobalDecl GD) { |
| 7850 | if (!CGM.getLangOpts().OpenMPIsDevice) |
| 7851 | return false; |
| 7852 | |
| 7853 | // Check if there are Ctors/Dtors in this declaration and look for target |
| 7854 | // regions in it. We use the complete variant to produce the kernel name |
| 7855 | // mangling. |
| 7856 | QualType RDTy = cast<VarDecl>(GD.getDecl())->getType(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 7857 | if (const auto *RD = RDTy->getBaseElementTypeUnsafe()->getAsCXXRecordDecl()) { |
| 7858 | for (const CXXConstructorDecl *Ctor : RD->ctors()) { |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 7859 | StringRef ParentName = |
| 7860 | CGM.getMangledName(GlobalDecl(Ctor, Ctor_Complete)); |
| 7861 | scanForTargetRegionsFunctions(Ctor->getBody(), ParentName); |
| 7862 | } |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 7863 | if (const CXXDestructorDecl *Dtor = RD->getDestructor()) { |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 7864 | StringRef ParentName = |
| 7865 | CGM.getMangledName(GlobalDecl(Dtor, Dtor_Complete)); |
| 7866 | scanForTargetRegionsFunctions(Dtor->getBody(), ParentName); |
| 7867 | } |
| 7868 | } |
| 7869 | |
Alexey Bataev | 4f4bf7c | 2018-03-15 15:47:20 +0000 | [diff] [blame] | 7870 | // 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] | 7871 | llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res = |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 7872 | isDeclareTargetDeclaration(cast<VarDecl>(GD.getDecl())); |
Alexey Bataev | 92327c5 | 2018-03-26 16:40:55 +0000 | [diff] [blame] | 7873 | return !Res || *Res == OMPDeclareTargetDeclAttr::MT_Link; |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 7874 | } |
| 7875 | |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 7876 | void CGOpenMPRuntime::registerTargetGlobalVariable(const VarDecl *VD, |
| 7877 | llvm::Constant *Addr) { |
| 7878 | if (llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res = |
| 7879 | isDeclareTargetDeclaration(VD)) { |
| 7880 | OffloadEntriesInfoManagerTy::OMPTargetGlobalVarEntryKind Flags; |
| 7881 | StringRef VarName; |
| 7882 | CharUnits VarSize; |
| 7883 | llvm::GlobalValue::LinkageTypes Linkage; |
| 7884 | switch (*Res) { |
| 7885 | case OMPDeclareTargetDeclAttr::MT_To: |
| 7886 | Flags = OffloadEntriesInfoManagerTy::OMPTargetGlobalVarEntryTo; |
| 7887 | VarName = CGM.getMangledName(VD); |
| 7888 | VarSize = CGM.getContext().getTypeSizeInChars(VD->getType()); |
| 7889 | Linkage = CGM.getLLVMLinkageVarDefinition(VD, /*IsConstant=*/false); |
| 7890 | break; |
| 7891 | case OMPDeclareTargetDeclAttr::MT_Link: |
| 7892 | // Map type 'to' because we do not map the original variable but the |
| 7893 | // reference. |
| 7894 | Flags = OffloadEntriesInfoManagerTy::OMPTargetGlobalVarEntryTo; |
| 7895 | if (!CGM.getLangOpts().OpenMPIsDevice) { |
| 7896 | Addr = |
| 7897 | cast<llvm::Constant>(getAddrOfDeclareTargetLink(VD).getPointer()); |
| 7898 | } |
| 7899 | VarName = Addr->getName(); |
| 7900 | VarSize = CGM.getPointerSize(); |
| 7901 | Linkage = llvm::GlobalValue::WeakAnyLinkage; |
| 7902 | break; |
| 7903 | } |
| 7904 | OffloadEntriesInfoManager.registerDeviceGlobalVarEntryInfo( |
| 7905 | VarName, Addr, VarSize, Flags, Linkage); |
| 7906 | } |
| 7907 | } |
| 7908 | |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 7909 | bool CGOpenMPRuntime::emitTargetGlobal(GlobalDecl GD) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 7910 | if (isa<FunctionDecl>(GD.getDecl())) |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 7911 | return emitTargetFunctions(GD); |
| 7912 | |
| 7913 | return emitTargetGlobalVariable(GD); |
| 7914 | } |
| 7915 | |
Alexey Bataev | 4f4bf7c | 2018-03-15 15:47:20 +0000 | [diff] [blame] | 7916 | CGOpenMPRuntime::DisableAutoDeclareTargetRAII::DisableAutoDeclareTargetRAII( |
| 7917 | CodeGenModule &CGM) |
| 7918 | : CGM(CGM) { |
| 7919 | if (CGM.getLangOpts().OpenMPIsDevice) { |
| 7920 | SavedShouldMarkAsGlobal = CGM.getOpenMPRuntime().ShouldMarkAsGlobal; |
| 7921 | CGM.getOpenMPRuntime().ShouldMarkAsGlobal = false; |
| 7922 | } |
| 7923 | } |
| 7924 | |
| 7925 | CGOpenMPRuntime::DisableAutoDeclareTargetRAII::~DisableAutoDeclareTargetRAII() { |
| 7926 | if (CGM.getLangOpts().OpenMPIsDevice) |
| 7927 | CGM.getOpenMPRuntime().ShouldMarkAsGlobal = SavedShouldMarkAsGlobal; |
| 7928 | } |
| 7929 | |
Alexey Bataev | 6d94410 | 2018-05-02 15:45:28 +0000 | [diff] [blame] | 7930 | bool CGOpenMPRuntime::markAsGlobalTarget(GlobalDecl GD) { |
Alexey Bataev | 4f4bf7c | 2018-03-15 15:47:20 +0000 | [diff] [blame] | 7931 | if (!CGM.getLangOpts().OpenMPIsDevice || !ShouldMarkAsGlobal) |
| 7932 | return true; |
Alexey Bataev | 4f4bf7c | 2018-03-15 15:47:20 +0000 | [diff] [blame] | 7933 | |
Alexey Bataev | 6d94410 | 2018-05-02 15:45:28 +0000 | [diff] [blame] | 7934 | const auto *D = cast<FunctionDecl>(GD.getDecl()); |
Alexey Bataev | 4f4bf7c | 2018-03-15 15:47:20 +0000 | [diff] [blame] | 7935 | const FunctionDecl *FD = D->getCanonicalDecl(); |
Alexey Bataev | 34f8a70 | 2018-03-28 14:28:54 +0000 | [diff] [blame] | 7936 | // Do not to emit function if it is marked as declare target as it was already |
| 7937 | // emitted. |
| 7938 | if (isDeclareTargetDeclaration(D)) { |
| 7939 | if (D->hasBody() && AlreadyEmittedTargetFunctions.count(FD) == 0) { |
| 7940 | if (auto *F = dyn_cast_or_null<llvm::Function>( |
Alexey Bataev | 6d94410 | 2018-05-02 15:45:28 +0000 | [diff] [blame] | 7941 | CGM.GetGlobalValue(CGM.getMangledName(GD)))) |
Alexey Bataev | 34f8a70 | 2018-03-28 14:28:54 +0000 | [diff] [blame] | 7942 | return !F->isDeclaration(); |
| 7943 | return false; |
| 7944 | } |
| 7945 | return true; |
| 7946 | } |
| 7947 | |
Alexey Bataev | 4f4bf7c | 2018-03-15 15:47:20 +0000 | [diff] [blame] | 7948 | return !AlreadyEmittedTargetFunctions.insert(FD).second; |
| 7949 | } |
| 7950 | |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 7951 | llvm::Function *CGOpenMPRuntime::emitRegistrationFunction() { |
| 7952 | // If we have offloading in the current module, we need to emit the entries |
| 7953 | // now and register the offloading descriptor. |
| 7954 | createOffloadEntriesAndInfoMetadata(); |
| 7955 | |
| 7956 | // Create and register the offloading binary descriptors. This is the main |
| 7957 | // entity that captures all the information about offloading in the current |
| 7958 | // compilation unit. |
| 7959 | return createOffloadingBinaryDescriptorRegistration(); |
| 7960 | } |
Carlo Bertolli | 430d8ec | 2016-03-03 20:34:23 +0000 | [diff] [blame] | 7961 | |
| 7962 | void CGOpenMPRuntime::emitTeamsCall(CodeGenFunction &CGF, |
| 7963 | const OMPExecutableDirective &D, |
| 7964 | SourceLocation Loc, |
| 7965 | llvm::Value *OutlinedFn, |
| 7966 | ArrayRef<llvm::Value *> CapturedVars) { |
| 7967 | if (!CGF.HaveInsertPoint()) |
| 7968 | return; |
| 7969 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 7970 | llvm::Value *RTLoc = emitUpdateLocation(CGF, Loc); |
Carlo Bertolli | 430d8ec | 2016-03-03 20:34:23 +0000 | [diff] [blame] | 7971 | CodeGenFunction::RunCleanupsScope Scope(CGF); |
| 7972 | |
| 7973 | // Build call __kmpc_fork_teams(loc, n, microtask, var1, .., varn); |
| 7974 | llvm::Value *Args[] = { |
| 7975 | RTLoc, |
| 7976 | CGF.Builder.getInt32(CapturedVars.size()), // Number of captured vars |
| 7977 | CGF.Builder.CreateBitCast(OutlinedFn, getKmpc_MicroPointerTy())}; |
| 7978 | llvm::SmallVector<llvm::Value *, 16> RealArgs; |
| 7979 | RealArgs.append(std::begin(Args), std::end(Args)); |
| 7980 | RealArgs.append(CapturedVars.begin(), CapturedVars.end()); |
| 7981 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 7982 | llvm::Value *RTLFn = createRuntimeFunction(OMPRTL__kmpc_fork_teams); |
Carlo Bertolli | 430d8ec | 2016-03-03 20:34:23 +0000 | [diff] [blame] | 7983 | CGF.EmitRuntimeCall(RTLFn, RealArgs); |
| 7984 | } |
| 7985 | |
| 7986 | void CGOpenMPRuntime::emitNumTeamsClause(CodeGenFunction &CGF, |
Carlo Bertolli | c687225 | 2016-04-04 15:55:02 +0000 | [diff] [blame] | 7987 | const Expr *NumTeams, |
| 7988 | const Expr *ThreadLimit, |
Carlo Bertolli | 430d8ec | 2016-03-03 20:34:23 +0000 | [diff] [blame] | 7989 | SourceLocation Loc) { |
| 7990 | if (!CGF.HaveInsertPoint()) |
| 7991 | return; |
| 7992 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 7993 | llvm::Value *RTLoc = emitUpdateLocation(CGF, Loc); |
Carlo Bertolli | 430d8ec | 2016-03-03 20:34:23 +0000 | [diff] [blame] | 7994 | |
Carlo Bertolli | c687225 | 2016-04-04 15:55:02 +0000 | [diff] [blame] | 7995 | llvm::Value *NumTeamsVal = |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 7996 | NumTeams |
Carlo Bertolli | c687225 | 2016-04-04 15:55:02 +0000 | [diff] [blame] | 7997 | ? CGF.Builder.CreateIntCast(CGF.EmitScalarExpr(NumTeams), |
| 7998 | CGF.CGM.Int32Ty, /* isSigned = */ true) |
| 7999 | : CGF.Builder.getInt32(0); |
| 8000 | |
| 8001 | llvm::Value *ThreadLimitVal = |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8002 | ThreadLimit |
Carlo Bertolli | c687225 | 2016-04-04 15:55:02 +0000 | [diff] [blame] | 8003 | ? CGF.Builder.CreateIntCast(CGF.EmitScalarExpr(ThreadLimit), |
| 8004 | CGF.CGM.Int32Ty, /* isSigned = */ true) |
| 8005 | : CGF.Builder.getInt32(0); |
| 8006 | |
Carlo Bertolli | 430d8ec | 2016-03-03 20:34:23 +0000 | [diff] [blame] | 8007 | // 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] | 8008 | llvm::Value *PushNumTeamsArgs[] = {RTLoc, getThreadID(CGF, Loc), NumTeamsVal, |
| 8009 | ThreadLimitVal}; |
Carlo Bertolli | 430d8ec | 2016-03-03 20:34:23 +0000 | [diff] [blame] | 8010 | CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_push_num_teams), |
| 8011 | PushNumTeamsArgs); |
| 8012 | } |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8013 | |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 8014 | void CGOpenMPRuntime::emitTargetDataCalls( |
| 8015 | CodeGenFunction &CGF, const OMPExecutableDirective &D, const Expr *IfCond, |
| 8016 | const Expr *Device, const RegionCodeGenTy &CodeGen, TargetDataInfo &Info) { |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8017 | if (!CGF.HaveInsertPoint()) |
| 8018 | return; |
| 8019 | |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 8020 | // Action used to replace the default codegen action and turn privatization |
| 8021 | // off. |
| 8022 | PrePostActionTy NoPrivAction; |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8023 | |
| 8024 | // Generate the code for the opening of the data environment. Capture all the |
| 8025 | // arguments of the runtime call by reference because they are used in the |
| 8026 | // closing of the region. |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 8027 | auto &&BeginThenGen = [this, &D, Device, &Info, |
| 8028 | &CodeGen](CodeGenFunction &CGF, PrePostActionTy &) { |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8029 | // Fill up the arrays with all the mapped variables. |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 8030 | MappableExprsHandler::MapBaseValuesArrayTy BasePointers; |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8031 | MappableExprsHandler::MapValuesArrayTy Pointers; |
| 8032 | MappableExprsHandler::MapValuesArrayTy Sizes; |
| 8033 | MappableExprsHandler::MapFlagsArrayTy MapTypes; |
| 8034 | |
| 8035 | // Get map clause information. |
| 8036 | MappableExprsHandler MCHandler(D, CGF); |
| 8037 | MCHandler.generateAllInfo(BasePointers, Pointers, Sizes, MapTypes); |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8038 | |
| 8039 | // Fill up the arrays and create the arguments. |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 8040 | emitOffloadingArrays(CGF, BasePointers, Pointers, Sizes, MapTypes, Info); |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8041 | |
| 8042 | llvm::Value *BasePointersArrayArg = nullptr; |
| 8043 | llvm::Value *PointersArrayArg = nullptr; |
| 8044 | llvm::Value *SizesArrayArg = nullptr; |
| 8045 | llvm::Value *MapTypesArrayArg = nullptr; |
| 8046 | emitOffloadingArraysArgument(CGF, BasePointersArrayArg, PointersArrayArg, |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 8047 | SizesArrayArg, MapTypesArrayArg, Info); |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8048 | |
| 8049 | // Emit device ID if any. |
| 8050 | llvm::Value *DeviceID = nullptr; |
George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 8051 | if (Device) { |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8052 | DeviceID = CGF.Builder.CreateIntCast(CGF.EmitScalarExpr(Device), |
George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 8053 | CGF.Int64Ty, /*isSigned=*/true); |
| 8054 | } else { |
| 8055 | DeviceID = CGF.Builder.getInt64(OMP_DEVICEID_UNDEF); |
| 8056 | } |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8057 | |
| 8058 | // Emit the number of elements in the offloading arrays. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8059 | llvm::Value *PointerNum = CGF.Builder.getInt32(Info.NumberOfPtrs); |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8060 | |
| 8061 | llvm::Value *OffloadingArgs[] = { |
| 8062 | DeviceID, PointerNum, BasePointersArrayArg, |
| 8063 | PointersArrayArg, SizesArrayArg, MapTypesArrayArg}; |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 8064 | CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__tgt_target_data_begin), |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8065 | OffloadingArgs); |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 8066 | |
| 8067 | // If device pointer privatization is required, emit the body of the region |
| 8068 | // here. It will have to be duplicated: with and without privatization. |
| 8069 | if (!Info.CaptureDeviceAddrMap.empty()) |
| 8070 | CodeGen(CGF); |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8071 | }; |
| 8072 | |
| 8073 | // Generate code for the closing of the data region. |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 8074 | auto &&EndThenGen = [this, Device, &Info](CodeGenFunction &CGF, |
| 8075 | PrePostActionTy &) { |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 8076 | assert(Info.isValid() && "Invalid data environment closing arguments."); |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8077 | |
| 8078 | llvm::Value *BasePointersArrayArg = nullptr; |
| 8079 | llvm::Value *PointersArrayArg = nullptr; |
| 8080 | llvm::Value *SizesArrayArg = nullptr; |
| 8081 | llvm::Value *MapTypesArrayArg = nullptr; |
| 8082 | emitOffloadingArraysArgument(CGF, BasePointersArrayArg, PointersArrayArg, |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 8083 | SizesArrayArg, MapTypesArrayArg, Info); |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8084 | |
| 8085 | // Emit device ID if any. |
| 8086 | llvm::Value *DeviceID = nullptr; |
George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 8087 | if (Device) { |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8088 | DeviceID = CGF.Builder.CreateIntCast(CGF.EmitScalarExpr(Device), |
George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 8089 | CGF.Int64Ty, /*isSigned=*/true); |
| 8090 | } else { |
| 8091 | DeviceID = CGF.Builder.getInt64(OMP_DEVICEID_UNDEF); |
| 8092 | } |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8093 | |
| 8094 | // Emit the number of elements in the offloading arrays. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8095 | llvm::Value *PointerNum = CGF.Builder.getInt32(Info.NumberOfPtrs); |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8096 | |
| 8097 | llvm::Value *OffloadingArgs[] = { |
| 8098 | DeviceID, PointerNum, BasePointersArrayArg, |
| 8099 | PointersArrayArg, SizesArrayArg, MapTypesArrayArg}; |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 8100 | CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__tgt_target_data_end), |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8101 | OffloadingArgs); |
| 8102 | }; |
| 8103 | |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 8104 | // If we need device pointer privatization, we need to emit the body of the |
| 8105 | // region with no privatization in the 'else' branch of the conditional. |
| 8106 | // Otherwise, we don't have to do anything. |
| 8107 | auto &&BeginElseGen = [&Info, &CodeGen, &NoPrivAction](CodeGenFunction &CGF, |
| 8108 | PrePostActionTy &) { |
| 8109 | if (!Info.CaptureDeviceAddrMap.empty()) { |
| 8110 | CodeGen.setAction(NoPrivAction); |
| 8111 | CodeGen(CGF); |
| 8112 | } |
| 8113 | }; |
| 8114 | |
| 8115 | // We don't have to do anything to close the region if the if clause evaluates |
| 8116 | // to false. |
| 8117 | auto &&EndElseGen = [](CodeGenFunction &CGF, PrePostActionTy &) {}; |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8118 | |
| 8119 | if (IfCond) { |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 8120 | emitOMPIfClause(CGF, IfCond, BeginThenGen, BeginElseGen); |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8121 | } else { |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 8122 | RegionCodeGenTy RCG(BeginThenGen); |
| 8123 | RCG(CGF); |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8124 | } |
| 8125 | |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 8126 | // If we don't require privatization of device pointers, we emit the body in |
| 8127 | // between the runtime calls. This avoids duplicating the body code. |
| 8128 | if (Info.CaptureDeviceAddrMap.empty()) { |
| 8129 | CodeGen.setAction(NoPrivAction); |
| 8130 | CodeGen(CGF); |
| 8131 | } |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8132 | |
| 8133 | if (IfCond) { |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 8134 | emitOMPIfClause(CGF, IfCond, EndThenGen, EndElseGen); |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8135 | } else { |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 8136 | RegionCodeGenTy RCG(EndThenGen); |
| 8137 | RCG(CGF); |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8138 | } |
| 8139 | } |
Samuel Antao | bd0ae2e | 2016-04-27 23:07:29 +0000 | [diff] [blame] | 8140 | |
Samuel Antao | 8d2d730 | 2016-05-26 18:30:22 +0000 | [diff] [blame] | 8141 | void CGOpenMPRuntime::emitTargetDataStandAloneCall( |
Samuel Antao | 8dd6628 | 2016-04-27 23:14:30 +0000 | [diff] [blame] | 8142 | CodeGenFunction &CGF, const OMPExecutableDirective &D, const Expr *IfCond, |
| 8143 | const Expr *Device) { |
Samuel Antao | bd0ae2e | 2016-04-27 23:07:29 +0000 | [diff] [blame] | 8144 | if (!CGF.HaveInsertPoint()) |
| 8145 | return; |
| 8146 | |
Samuel Antao | 8dd6628 | 2016-04-27 23:14:30 +0000 | [diff] [blame] | 8147 | assert((isa<OMPTargetEnterDataDirective>(D) || |
Samuel Antao | 8d2d730 | 2016-05-26 18:30:22 +0000 | [diff] [blame] | 8148 | isa<OMPTargetExitDataDirective>(D) || |
| 8149 | isa<OMPTargetUpdateDirective>(D)) && |
| 8150 | "Expecting either target enter, exit data, or update directives."); |
Samuel Antao | 8dd6628 | 2016-04-27 23:14:30 +0000 | [diff] [blame] | 8151 | |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 8152 | CodeGenFunction::OMPTargetDataInfo InputInfo; |
| 8153 | llvm::Value *MapTypesArray = nullptr; |
Samuel Antao | bd0ae2e | 2016-04-27 23:07:29 +0000 | [diff] [blame] | 8154 | // Generate the code for the opening of the data environment. |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 8155 | auto &&ThenGen = [this, &D, Device, &InputInfo, |
| 8156 | &MapTypesArray](CodeGenFunction &CGF, PrePostActionTy &) { |
Samuel Antao | bd0ae2e | 2016-04-27 23:07:29 +0000 | [diff] [blame] | 8157 | // Emit device ID if any. |
| 8158 | llvm::Value *DeviceID = nullptr; |
George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 8159 | if (Device) { |
Samuel Antao | bd0ae2e | 2016-04-27 23:07:29 +0000 | [diff] [blame] | 8160 | DeviceID = CGF.Builder.CreateIntCast(CGF.EmitScalarExpr(Device), |
George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 8161 | CGF.Int64Ty, /*isSigned=*/true); |
| 8162 | } else { |
| 8163 | DeviceID = CGF.Builder.getInt64(OMP_DEVICEID_UNDEF); |
| 8164 | } |
Samuel Antao | bd0ae2e | 2016-04-27 23:07:29 +0000 | [diff] [blame] | 8165 | |
| 8166 | // Emit the number of elements in the offloading arrays. |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 8167 | llvm::Constant *PointerNum = |
| 8168 | CGF.Builder.getInt32(InputInfo.NumberOfTargetItems); |
Samuel Antao | bd0ae2e | 2016-04-27 23:07:29 +0000 | [diff] [blame] | 8169 | |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 8170 | llvm::Value *OffloadingArgs[] = {DeviceID, |
| 8171 | PointerNum, |
| 8172 | InputInfo.BasePointersArray.getPointer(), |
| 8173 | InputInfo.PointersArray.getPointer(), |
| 8174 | InputInfo.SizesArray.getPointer(), |
| 8175 | MapTypesArray}; |
Samuel Antao | 8d2d730 | 2016-05-26 18:30:22 +0000 | [diff] [blame] | 8176 | |
Samuel Antao | 8d2d730 | 2016-05-26 18:30:22 +0000 | [diff] [blame] | 8177 | // Select the right runtime function call for each expected standalone |
| 8178 | // directive. |
Alexey Bataev | 0cc6b8e | 2017-12-14 17:00:17 +0000 | [diff] [blame] | 8179 | const bool HasNowait = D.hasClausesOfKind<OMPNowaitClause>(); |
Samuel Antao | 8d2d730 | 2016-05-26 18:30:22 +0000 | [diff] [blame] | 8180 | OpenMPRTLFunction RTLFn; |
| 8181 | switch (D.getDirectiveKind()) { |
Samuel Antao | 8d2d730 | 2016-05-26 18:30:22 +0000 | [diff] [blame] | 8182 | case OMPD_target_enter_data: |
Alexey Bataev | 0cc6b8e | 2017-12-14 17:00:17 +0000 | [diff] [blame] | 8183 | RTLFn = HasNowait ? OMPRTL__tgt_target_data_begin_nowait |
| 8184 | : OMPRTL__tgt_target_data_begin; |
Samuel Antao | 8d2d730 | 2016-05-26 18:30:22 +0000 | [diff] [blame] | 8185 | break; |
| 8186 | case OMPD_target_exit_data: |
Alexey Bataev | 0cc6b8e | 2017-12-14 17:00:17 +0000 | [diff] [blame] | 8187 | RTLFn = HasNowait ? OMPRTL__tgt_target_data_end_nowait |
| 8188 | : OMPRTL__tgt_target_data_end; |
Samuel Antao | 8d2d730 | 2016-05-26 18:30:22 +0000 | [diff] [blame] | 8189 | break; |
| 8190 | case OMPD_target_update: |
Alexey Bataev | 0cc6b8e | 2017-12-14 17:00:17 +0000 | [diff] [blame] | 8191 | RTLFn = HasNowait ? OMPRTL__tgt_target_data_update_nowait |
| 8192 | : OMPRTL__tgt_target_data_update; |
Samuel Antao | 8d2d730 | 2016-05-26 18:30:22 +0000 | [diff] [blame] | 8193 | break; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8194 | case OMPD_parallel: |
| 8195 | case OMPD_for: |
| 8196 | case OMPD_parallel_for: |
| 8197 | case OMPD_parallel_sections: |
| 8198 | case OMPD_for_simd: |
| 8199 | case OMPD_parallel_for_simd: |
| 8200 | case OMPD_cancel: |
| 8201 | case OMPD_cancellation_point: |
| 8202 | case OMPD_ordered: |
| 8203 | case OMPD_threadprivate: |
| 8204 | case OMPD_task: |
| 8205 | case OMPD_simd: |
| 8206 | case OMPD_sections: |
| 8207 | case OMPD_section: |
| 8208 | case OMPD_single: |
| 8209 | case OMPD_master: |
| 8210 | case OMPD_critical: |
| 8211 | case OMPD_taskyield: |
| 8212 | case OMPD_barrier: |
| 8213 | case OMPD_taskwait: |
| 8214 | case OMPD_taskgroup: |
| 8215 | case OMPD_atomic: |
| 8216 | case OMPD_flush: |
| 8217 | case OMPD_teams: |
| 8218 | case OMPD_target_data: |
| 8219 | case OMPD_distribute: |
| 8220 | case OMPD_distribute_simd: |
| 8221 | case OMPD_distribute_parallel_for: |
| 8222 | case OMPD_distribute_parallel_for_simd: |
| 8223 | case OMPD_teams_distribute: |
| 8224 | case OMPD_teams_distribute_simd: |
| 8225 | case OMPD_teams_distribute_parallel_for: |
| 8226 | case OMPD_teams_distribute_parallel_for_simd: |
| 8227 | case OMPD_declare_simd: |
| 8228 | case OMPD_declare_target: |
| 8229 | case OMPD_end_declare_target: |
| 8230 | case OMPD_declare_reduction: |
| 8231 | case OMPD_taskloop: |
| 8232 | case OMPD_taskloop_simd: |
| 8233 | case OMPD_target: |
| 8234 | case OMPD_target_simd: |
| 8235 | case OMPD_target_teams_distribute: |
| 8236 | case OMPD_target_teams_distribute_simd: |
| 8237 | case OMPD_target_teams_distribute_parallel_for: |
| 8238 | case OMPD_target_teams_distribute_parallel_for_simd: |
| 8239 | case OMPD_target_teams: |
| 8240 | case OMPD_target_parallel: |
| 8241 | case OMPD_target_parallel_for: |
| 8242 | case OMPD_target_parallel_for_simd: |
| 8243 | case OMPD_unknown: |
| 8244 | llvm_unreachable("Unexpected standalone target data directive."); |
| 8245 | break; |
Samuel Antao | 8d2d730 | 2016-05-26 18:30:22 +0000 | [diff] [blame] | 8246 | } |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 8247 | CGF.EmitRuntimeCall(createRuntimeFunction(RTLFn), OffloadingArgs); |
Samuel Antao | bd0ae2e | 2016-04-27 23:07:29 +0000 | [diff] [blame] | 8248 | }; |
| 8249 | |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 8250 | auto &&TargetThenGen = [this, &ThenGen, &D, &InputInfo, &MapTypesArray]( |
| 8251 | CodeGenFunction &CGF, PrePostActionTy &) { |
| 8252 | // Fill up the arrays with all the mapped variables. |
| 8253 | MappableExprsHandler::MapBaseValuesArrayTy BasePointers; |
| 8254 | MappableExprsHandler::MapValuesArrayTy Pointers; |
| 8255 | MappableExprsHandler::MapValuesArrayTy Sizes; |
| 8256 | MappableExprsHandler::MapFlagsArrayTy MapTypes; |
Samuel Antao | bd0ae2e | 2016-04-27 23:07:29 +0000 | [diff] [blame] | 8257 | |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 8258 | // Get map clause information. |
| 8259 | MappableExprsHandler MEHandler(D, CGF); |
| 8260 | MEHandler.generateAllInfo(BasePointers, Pointers, Sizes, MapTypes); |
| 8261 | |
| 8262 | TargetDataInfo Info; |
| 8263 | // Fill up the arrays and create the arguments. |
| 8264 | emitOffloadingArrays(CGF, BasePointers, Pointers, Sizes, MapTypes, Info); |
| 8265 | emitOffloadingArraysArgument(CGF, Info.BasePointersArray, |
| 8266 | Info.PointersArray, Info.SizesArray, |
| 8267 | Info.MapTypesArray, Info); |
| 8268 | InputInfo.NumberOfTargetItems = Info.NumberOfPtrs; |
| 8269 | InputInfo.BasePointersArray = |
| 8270 | Address(Info.BasePointersArray, CGM.getPointerAlign()); |
| 8271 | InputInfo.PointersArray = |
| 8272 | Address(Info.PointersArray, CGM.getPointerAlign()); |
| 8273 | InputInfo.SizesArray = |
| 8274 | Address(Info.SizesArray, CGM.getPointerAlign()); |
| 8275 | MapTypesArray = Info.MapTypesArray; |
| 8276 | if (D.hasClausesOfKind<OMPDependClause>()) |
| 8277 | CGF.EmitOMPTargetTaskBasedDirective(D, ThenGen, InputInfo); |
| 8278 | else |
Alexey Bataev | 768f1f2 | 2018-01-09 19:59:25 +0000 | [diff] [blame] | 8279 | emitInlinedDirective(CGF, D.getDirectiveKind(), ThenGen); |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 8280 | }; |
| 8281 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8282 | if (IfCond) { |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 8283 | emitOMPIfClause(CGF, IfCond, TargetThenGen, |
| 8284 | [](CodeGenFunction &CGF, PrePostActionTy &) {}); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8285 | } else { |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 8286 | RegionCodeGenTy ThenRCG(TargetThenGen); |
| 8287 | ThenRCG(CGF); |
Samuel Antao | bd0ae2e | 2016-04-27 23:07:29 +0000 | [diff] [blame] | 8288 | } |
| 8289 | } |
Alexey Bataev | c7a82b4 | 2016-05-06 09:40:08 +0000 | [diff] [blame] | 8290 | |
| 8291 | namespace { |
| 8292 | /// Kind of parameter in a function with 'declare simd' directive. |
| 8293 | enum ParamKindTy { LinearWithVarStride, Linear, Uniform, Vector }; |
| 8294 | /// Attribute set of the parameter. |
| 8295 | struct ParamAttrTy { |
| 8296 | ParamKindTy Kind = Vector; |
| 8297 | llvm::APSInt StrideOrArg; |
| 8298 | llvm::APSInt Alignment; |
| 8299 | }; |
| 8300 | } // namespace |
| 8301 | |
| 8302 | static unsigned evaluateCDTSize(const FunctionDecl *FD, |
| 8303 | ArrayRef<ParamAttrTy> ParamAttrs) { |
| 8304 | // Every vector variant of a SIMD-enabled function has a vector length (VLEN). |
| 8305 | // If OpenMP clause "simdlen" is used, the VLEN is the value of the argument |
| 8306 | // of that clause. The VLEN value must be power of 2. |
| 8307 | // In other case the notion of the function`s "characteristic data type" (CDT) |
| 8308 | // is used to compute the vector length. |
| 8309 | // CDT is defined in the following order: |
| 8310 | // a) For non-void function, the CDT is the return type. |
| 8311 | // b) If the function has any non-uniform, non-linear parameters, then the |
| 8312 | // CDT is the type of the first such parameter. |
| 8313 | // c) If the CDT determined by a) or b) above is struct, union, or class |
| 8314 | // type which is pass-by-value (except for the type that maps to the |
| 8315 | // built-in complex data type), the characteristic data type is int. |
| 8316 | // d) If none of the above three cases is applicable, the CDT is int. |
| 8317 | // The VLEN is then determined based on the CDT and the size of vector |
| 8318 | // register of that ISA for which current vector version is generated. The |
| 8319 | // VLEN is computed using the formula below: |
| 8320 | // VLEN = sizeof(vector_register) / sizeof(CDT), |
| 8321 | // where vector register size specified in section 3.2.1 Registers and the |
| 8322 | // Stack Frame of original AMD64 ABI document. |
| 8323 | QualType RetType = FD->getReturnType(); |
| 8324 | if (RetType.isNull()) |
| 8325 | return 0; |
| 8326 | ASTContext &C = FD->getASTContext(); |
| 8327 | QualType CDT; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8328 | if (!RetType.isNull() && !RetType->isVoidType()) { |
Alexey Bataev | c7a82b4 | 2016-05-06 09:40:08 +0000 | [diff] [blame] | 8329 | CDT = RetType; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8330 | } else { |
Alexey Bataev | c7a82b4 | 2016-05-06 09:40:08 +0000 | [diff] [blame] | 8331 | unsigned Offset = 0; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8332 | if (const auto *MD = dyn_cast<CXXMethodDecl>(FD)) { |
Alexey Bataev | c7a82b4 | 2016-05-06 09:40:08 +0000 | [diff] [blame] | 8333 | if (ParamAttrs[Offset].Kind == Vector) |
| 8334 | CDT = C.getPointerType(C.getRecordType(MD->getParent())); |
| 8335 | ++Offset; |
| 8336 | } |
| 8337 | if (CDT.isNull()) { |
| 8338 | for (unsigned I = 0, E = FD->getNumParams(); I < E; ++I) { |
| 8339 | if (ParamAttrs[I + Offset].Kind == Vector) { |
| 8340 | CDT = FD->getParamDecl(I)->getType(); |
| 8341 | break; |
| 8342 | } |
| 8343 | } |
| 8344 | } |
| 8345 | } |
| 8346 | if (CDT.isNull()) |
| 8347 | CDT = C.IntTy; |
| 8348 | CDT = CDT->getCanonicalTypeUnqualified(); |
| 8349 | if (CDT->isRecordType() || CDT->isUnionType()) |
| 8350 | CDT = C.IntTy; |
| 8351 | return C.getTypeSize(CDT); |
| 8352 | } |
| 8353 | |
| 8354 | static void |
| 8355 | emitX86DeclareSimdFunction(const FunctionDecl *FD, llvm::Function *Fn, |
Benjamin Kramer | 81cb4b7 | 2016-11-24 16:01:20 +0000 | [diff] [blame] | 8356 | const llvm::APSInt &VLENVal, |
Alexey Bataev | c7a82b4 | 2016-05-06 09:40:08 +0000 | [diff] [blame] | 8357 | ArrayRef<ParamAttrTy> ParamAttrs, |
| 8358 | OMPDeclareSimdDeclAttr::BranchStateTy State) { |
| 8359 | struct ISADataTy { |
| 8360 | char ISA; |
| 8361 | unsigned VecRegSize; |
| 8362 | }; |
| 8363 | ISADataTy ISAData[] = { |
| 8364 | { |
| 8365 | 'b', 128 |
| 8366 | }, // SSE |
| 8367 | { |
| 8368 | 'c', 256 |
| 8369 | }, // AVX |
| 8370 | { |
| 8371 | 'd', 256 |
| 8372 | }, // AVX2 |
| 8373 | { |
| 8374 | 'e', 512 |
| 8375 | }, // AVX512 |
| 8376 | }; |
| 8377 | llvm::SmallVector<char, 2> Masked; |
| 8378 | switch (State) { |
| 8379 | case OMPDeclareSimdDeclAttr::BS_Undefined: |
| 8380 | Masked.push_back('N'); |
| 8381 | Masked.push_back('M'); |
| 8382 | break; |
| 8383 | case OMPDeclareSimdDeclAttr::BS_Notinbranch: |
| 8384 | Masked.push_back('N'); |
| 8385 | break; |
| 8386 | case OMPDeclareSimdDeclAttr::BS_Inbranch: |
| 8387 | Masked.push_back('M'); |
| 8388 | break; |
| 8389 | } |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8390 | for (char Mask : Masked) { |
| 8391 | for (const ISADataTy &Data : ISAData) { |
Alexey Bataev | c7a82b4 | 2016-05-06 09:40:08 +0000 | [diff] [blame] | 8392 | SmallString<256> Buffer; |
| 8393 | llvm::raw_svector_ostream Out(Buffer); |
| 8394 | Out << "_ZGV" << Data.ISA << Mask; |
| 8395 | if (!VLENVal) { |
| 8396 | Out << llvm::APSInt::getUnsigned(Data.VecRegSize / |
| 8397 | evaluateCDTSize(FD, ParamAttrs)); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8398 | } else { |
Alexey Bataev | c7a82b4 | 2016-05-06 09:40:08 +0000 | [diff] [blame] | 8399 | Out << VLENVal; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8400 | } |
| 8401 | for (const ParamAttrTy &ParamAttr : ParamAttrs) { |
Alexey Bataev | c7a82b4 | 2016-05-06 09:40:08 +0000 | [diff] [blame] | 8402 | switch (ParamAttr.Kind){ |
| 8403 | case LinearWithVarStride: |
| 8404 | Out << 's' << ParamAttr.StrideOrArg; |
| 8405 | break; |
| 8406 | case Linear: |
| 8407 | Out << 'l'; |
| 8408 | if (!!ParamAttr.StrideOrArg) |
| 8409 | Out << ParamAttr.StrideOrArg; |
| 8410 | break; |
| 8411 | case Uniform: |
| 8412 | Out << 'u'; |
| 8413 | break; |
| 8414 | case Vector: |
| 8415 | Out << 'v'; |
| 8416 | break; |
| 8417 | } |
| 8418 | if (!!ParamAttr.Alignment) |
| 8419 | Out << 'a' << ParamAttr.Alignment; |
| 8420 | } |
| 8421 | Out << '_' << Fn->getName(); |
| 8422 | Fn->addFnAttr(Out.str()); |
| 8423 | } |
| 8424 | } |
| 8425 | } |
| 8426 | |
| 8427 | void CGOpenMPRuntime::emitDeclareSimdFunction(const FunctionDecl *FD, |
| 8428 | llvm::Function *Fn) { |
| 8429 | ASTContext &C = CGM.getContext(); |
Alexey Bataev | 20cf67c | 2018-03-02 18:07:00 +0000 | [diff] [blame] | 8430 | FD = FD->getMostRecentDecl(); |
Alexey Bataev | c7a82b4 | 2016-05-06 09:40:08 +0000 | [diff] [blame] | 8431 | // Map params to their positions in function decl. |
| 8432 | llvm::DenseMap<const Decl *, unsigned> ParamPositions; |
| 8433 | if (isa<CXXMethodDecl>(FD)) |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8434 | ParamPositions.try_emplace(FD, 0); |
Alexey Bataev | c7a82b4 | 2016-05-06 09:40:08 +0000 | [diff] [blame] | 8435 | unsigned ParamPos = ParamPositions.size(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8436 | for (const ParmVarDecl *P : FD->parameters()) { |
| 8437 | ParamPositions.try_emplace(P->getCanonicalDecl(), ParamPos); |
Alexey Bataev | c7a82b4 | 2016-05-06 09:40:08 +0000 | [diff] [blame] | 8438 | ++ParamPos; |
| 8439 | } |
Alexey Bataev | 20cf67c | 2018-03-02 18:07:00 +0000 | [diff] [blame] | 8440 | while (FD) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8441 | for (const auto *Attr : FD->specific_attrs<OMPDeclareSimdDeclAttr>()) { |
Alexey Bataev | 20cf67c | 2018-03-02 18:07:00 +0000 | [diff] [blame] | 8442 | llvm::SmallVector<ParamAttrTy, 8> ParamAttrs(ParamPositions.size()); |
| 8443 | // Mark uniform parameters. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8444 | for (const Expr *E : Attr->uniforms()) { |
Alexey Bataev | 20cf67c | 2018-03-02 18:07:00 +0000 | [diff] [blame] | 8445 | E = E->IgnoreParenImpCasts(); |
| 8446 | unsigned Pos; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8447 | if (isa<CXXThisExpr>(E)) { |
Alexey Bataev | 20cf67c | 2018-03-02 18:07:00 +0000 | [diff] [blame] | 8448 | Pos = ParamPositions[FD]; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8449 | } else { |
| 8450 | const auto *PVD = cast<ParmVarDecl>(cast<DeclRefExpr>(E)->getDecl()) |
| 8451 | ->getCanonicalDecl(); |
Alexey Bataev | 20cf67c | 2018-03-02 18:07:00 +0000 | [diff] [blame] | 8452 | Pos = ParamPositions[PVD]; |
| 8453 | } |
| 8454 | ParamAttrs[Pos].Kind = Uniform; |
Alexey Bataev | c7a82b4 | 2016-05-06 09:40:08 +0000 | [diff] [blame] | 8455 | } |
Alexey Bataev | 20cf67c | 2018-03-02 18:07:00 +0000 | [diff] [blame] | 8456 | // Get alignment info. |
| 8457 | auto NI = Attr->alignments_begin(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8458 | for (const Expr *E : Attr->aligneds()) { |
Alexey Bataev | 20cf67c | 2018-03-02 18:07:00 +0000 | [diff] [blame] | 8459 | E = E->IgnoreParenImpCasts(); |
| 8460 | unsigned Pos; |
| 8461 | QualType ParmTy; |
| 8462 | if (isa<CXXThisExpr>(E)) { |
| 8463 | Pos = ParamPositions[FD]; |
| 8464 | ParmTy = E->getType(); |
| 8465 | } else { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8466 | const auto *PVD = cast<ParmVarDecl>(cast<DeclRefExpr>(E)->getDecl()) |
| 8467 | ->getCanonicalDecl(); |
Alexey Bataev | 20cf67c | 2018-03-02 18:07:00 +0000 | [diff] [blame] | 8468 | Pos = ParamPositions[PVD]; |
| 8469 | ParmTy = PVD->getType(); |
| 8470 | } |
| 8471 | ParamAttrs[Pos].Alignment = |
| 8472 | (*NI) |
| 8473 | ? (*NI)->EvaluateKnownConstInt(C) |
Alexey Bataev | c7a82b4 | 2016-05-06 09:40:08 +0000 | [diff] [blame] | 8474 | : llvm::APSInt::getUnsigned( |
| 8475 | C.toCharUnitsFromBits(C.getOpenMPDefaultSimdAlign(ParmTy)) |
| 8476 | .getQuantity()); |
Alexey Bataev | 20cf67c | 2018-03-02 18:07:00 +0000 | [diff] [blame] | 8477 | ++NI; |
Alexey Bataev | c7a82b4 | 2016-05-06 09:40:08 +0000 | [diff] [blame] | 8478 | } |
Alexey Bataev | 20cf67c | 2018-03-02 18:07:00 +0000 | [diff] [blame] | 8479 | // Mark linear parameters. |
| 8480 | auto SI = Attr->steps_begin(); |
| 8481 | auto MI = Attr->modifiers_begin(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8482 | for (const Expr *E : Attr->linears()) { |
Alexey Bataev | 20cf67c | 2018-03-02 18:07:00 +0000 | [diff] [blame] | 8483 | E = E->IgnoreParenImpCasts(); |
| 8484 | unsigned Pos; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8485 | if (isa<CXXThisExpr>(E)) { |
Alexey Bataev | 20cf67c | 2018-03-02 18:07:00 +0000 | [diff] [blame] | 8486 | Pos = ParamPositions[FD]; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8487 | } else { |
| 8488 | const auto *PVD = cast<ParmVarDecl>(cast<DeclRefExpr>(E)->getDecl()) |
| 8489 | ->getCanonicalDecl(); |
Alexey Bataev | 20cf67c | 2018-03-02 18:07:00 +0000 | [diff] [blame] | 8490 | Pos = ParamPositions[PVD]; |
| 8491 | } |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8492 | ParamAttrTy &ParamAttr = ParamAttrs[Pos]; |
Alexey Bataev | 20cf67c | 2018-03-02 18:07:00 +0000 | [diff] [blame] | 8493 | ParamAttr.Kind = Linear; |
| 8494 | if (*SI) { |
| 8495 | if (!(*SI)->EvaluateAsInt(ParamAttr.StrideOrArg, C, |
| 8496 | Expr::SE_AllowSideEffects)) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8497 | if (const auto *DRE = |
| 8498 | cast<DeclRefExpr>((*SI)->IgnoreParenImpCasts())) { |
| 8499 | if (const auto *StridePVD = cast<ParmVarDecl>(DRE->getDecl())) { |
Alexey Bataev | 20cf67c | 2018-03-02 18:07:00 +0000 | [diff] [blame] | 8500 | ParamAttr.Kind = LinearWithVarStride; |
| 8501 | ParamAttr.StrideOrArg = llvm::APSInt::getUnsigned( |
| 8502 | ParamPositions[StridePVD->getCanonicalDecl()]); |
| 8503 | } |
Alexey Bataev | c7a82b4 | 2016-05-06 09:40:08 +0000 | [diff] [blame] | 8504 | } |
| 8505 | } |
| 8506 | } |
Alexey Bataev | 20cf67c | 2018-03-02 18:07:00 +0000 | [diff] [blame] | 8507 | ++SI; |
| 8508 | ++MI; |
Alexey Bataev | c7a82b4 | 2016-05-06 09:40:08 +0000 | [diff] [blame] | 8509 | } |
Alexey Bataev | 20cf67c | 2018-03-02 18:07:00 +0000 | [diff] [blame] | 8510 | llvm::APSInt VLENVal; |
| 8511 | if (const Expr *VLEN = Attr->getSimdlen()) |
| 8512 | VLENVal = VLEN->EvaluateKnownConstInt(C); |
| 8513 | OMPDeclareSimdDeclAttr::BranchStateTy State = Attr->getBranchState(); |
| 8514 | if (CGM.getTriple().getArch() == llvm::Triple::x86 || |
| 8515 | CGM.getTriple().getArch() == llvm::Triple::x86_64) |
| 8516 | emitX86DeclareSimdFunction(FD, Fn, VLENVal, ParamAttrs, State); |
Alexey Bataev | c7a82b4 | 2016-05-06 09:40:08 +0000 | [diff] [blame] | 8517 | } |
Alexey Bataev | 20cf67c | 2018-03-02 18:07:00 +0000 | [diff] [blame] | 8518 | FD = FD->getPreviousDecl(); |
Alexey Bataev | c7a82b4 | 2016-05-06 09:40:08 +0000 | [diff] [blame] | 8519 | } |
| 8520 | } |
Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 8521 | |
| 8522 | namespace { |
| 8523 | /// Cleanup action for doacross support. |
| 8524 | class DoacrossCleanupTy final : public EHScopeStack::Cleanup { |
| 8525 | public: |
| 8526 | static const int DoacrossFinArgs = 2; |
| 8527 | |
| 8528 | private: |
| 8529 | llvm::Value *RTLFn; |
| 8530 | llvm::Value *Args[DoacrossFinArgs]; |
| 8531 | |
| 8532 | public: |
| 8533 | DoacrossCleanupTy(llvm::Value *RTLFn, ArrayRef<llvm::Value *> CallArgs) |
| 8534 | : RTLFn(RTLFn) { |
| 8535 | assert(CallArgs.size() == DoacrossFinArgs); |
| 8536 | std::copy(CallArgs.begin(), CallArgs.end(), std::begin(Args)); |
| 8537 | } |
| 8538 | void Emit(CodeGenFunction &CGF, Flags /*flags*/) override { |
| 8539 | if (!CGF.HaveInsertPoint()) |
| 8540 | return; |
| 8541 | CGF.EmitRuntimeCall(RTLFn, Args); |
| 8542 | } |
| 8543 | }; |
| 8544 | } // namespace |
| 8545 | |
| 8546 | void CGOpenMPRuntime::emitDoacrossInit(CodeGenFunction &CGF, |
| 8547 | const OMPLoopDirective &D) { |
| 8548 | if (!CGF.HaveInsertPoint()) |
| 8549 | return; |
| 8550 | |
| 8551 | ASTContext &C = CGM.getContext(); |
| 8552 | QualType Int64Ty = C.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/true); |
| 8553 | RecordDecl *RD; |
| 8554 | if (KmpDimTy.isNull()) { |
| 8555 | // Build struct kmp_dim { // loop bounds info casted to kmp_int64 |
| 8556 | // kmp_int64 lo; // lower |
| 8557 | // kmp_int64 up; // upper |
| 8558 | // kmp_int64 st; // stride |
| 8559 | // }; |
| 8560 | RD = C.buildImplicitRecord("kmp_dim"); |
| 8561 | RD->startDefinition(); |
| 8562 | addFieldToRecordDecl(C, RD, Int64Ty); |
| 8563 | addFieldToRecordDecl(C, RD, Int64Ty); |
| 8564 | addFieldToRecordDecl(C, RD, Int64Ty); |
| 8565 | RD->completeDefinition(); |
| 8566 | KmpDimTy = C.getRecordType(RD); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8567 | } else { |
Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 8568 | RD = cast<RecordDecl>(KmpDimTy->getAsTagDecl()); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8569 | } |
Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 8570 | |
| 8571 | Address DimsAddr = CGF.CreateMemTemp(KmpDimTy, "dims"); |
| 8572 | CGF.EmitNullInitialization(DimsAddr, KmpDimTy); |
| 8573 | enum { LowerFD = 0, UpperFD, StrideFD }; |
| 8574 | // Fill dims with data. |
| 8575 | LValue DimsLVal = CGF.MakeAddrLValue(DimsAddr, KmpDimTy); |
| 8576 | // dims.upper = num_iterations; |
| 8577 | LValue UpperLVal = |
| 8578 | CGF.EmitLValueForField(DimsLVal, *std::next(RD->field_begin(), UpperFD)); |
| 8579 | llvm::Value *NumIterVal = CGF.EmitScalarConversion( |
| 8580 | CGF.EmitScalarExpr(D.getNumIterations()), D.getNumIterations()->getType(), |
| 8581 | Int64Ty, D.getNumIterations()->getExprLoc()); |
| 8582 | CGF.EmitStoreOfScalar(NumIterVal, UpperLVal); |
| 8583 | // dims.stride = 1; |
| 8584 | LValue StrideLVal = |
| 8585 | CGF.EmitLValueForField(DimsLVal, *std::next(RD->field_begin(), StrideFD)); |
| 8586 | CGF.EmitStoreOfScalar(llvm::ConstantInt::getSigned(CGM.Int64Ty, /*V=*/1), |
| 8587 | StrideLVal); |
| 8588 | |
| 8589 | // Build call void __kmpc_doacross_init(ident_t *loc, kmp_int32 gtid, |
| 8590 | // kmp_int32 num_dims, struct kmp_dim * dims); |
| 8591 | llvm::Value *Args[] = {emitUpdateLocation(CGF, D.getLocStart()), |
| 8592 | getThreadID(CGF, D.getLocStart()), |
| 8593 | llvm::ConstantInt::getSigned(CGM.Int32Ty, 1), |
| 8594 | CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
| 8595 | DimsAddr.getPointer(), CGM.VoidPtrTy)}; |
| 8596 | |
| 8597 | llvm::Value *RTLFn = createRuntimeFunction(OMPRTL__kmpc_doacross_init); |
| 8598 | CGF.EmitRuntimeCall(RTLFn, Args); |
| 8599 | llvm::Value *FiniArgs[DoacrossCleanupTy::DoacrossFinArgs] = { |
| 8600 | emitUpdateLocation(CGF, D.getLocEnd()), getThreadID(CGF, D.getLocEnd())}; |
| 8601 | llvm::Value *FiniRTLFn = createRuntimeFunction(OMPRTL__kmpc_doacross_fini); |
| 8602 | CGF.EHStack.pushCleanup<DoacrossCleanupTy>(NormalAndEHCleanup, FiniRTLFn, |
| 8603 | llvm::makeArrayRef(FiniArgs)); |
| 8604 | } |
| 8605 | |
| 8606 | void CGOpenMPRuntime::emitDoacrossOrdered(CodeGenFunction &CGF, |
| 8607 | const OMPDependClause *C) { |
| 8608 | QualType Int64Ty = |
| 8609 | CGM.getContext().getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1); |
| 8610 | const Expr *CounterVal = C->getCounterValue(); |
| 8611 | assert(CounterVal); |
| 8612 | llvm::Value *CntVal = CGF.EmitScalarConversion(CGF.EmitScalarExpr(CounterVal), |
| 8613 | CounterVal->getType(), Int64Ty, |
| 8614 | CounterVal->getExprLoc()); |
| 8615 | Address CntAddr = CGF.CreateMemTemp(Int64Ty, ".cnt.addr"); |
| 8616 | CGF.EmitStoreOfScalar(CntVal, CntAddr, /*Volatile=*/false, Int64Ty); |
| 8617 | llvm::Value *Args[] = {emitUpdateLocation(CGF, C->getLocStart()), |
| 8618 | getThreadID(CGF, C->getLocStart()), |
| 8619 | CntAddr.getPointer()}; |
| 8620 | llvm::Value *RTLFn; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8621 | if (C->getDependencyKind() == OMPC_DEPEND_source) { |
Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 8622 | RTLFn = createRuntimeFunction(OMPRTL__kmpc_doacross_post); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8623 | } else { |
Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 8624 | assert(C->getDependencyKind() == OMPC_DEPEND_sink); |
| 8625 | RTLFn = createRuntimeFunction(OMPRTL__kmpc_doacross_wait); |
| 8626 | } |
| 8627 | CGF.EmitRuntimeCall(RTLFn, Args); |
| 8628 | } |
| 8629 | |
Alexey Bataev | 7ef47a6 | 2018-02-22 18:33:31 +0000 | [diff] [blame] | 8630 | void CGOpenMPRuntime::emitCall(CodeGenFunction &CGF, SourceLocation Loc, |
| 8631 | llvm::Value *Callee, |
| 8632 | ArrayRef<llvm::Value *> Args) const { |
| 8633 | assert(Loc.isValid() && "Outlined function call location must be valid."); |
Alexey Bataev | 3c595a6 | 2017-08-14 15:01:03 +0000 | [diff] [blame] | 8634 | auto DL = ApplyDebugLocation::CreateDefaultArtificial(CGF, Loc); |
| 8635 | |
| 8636 | if (auto *Fn = dyn_cast<llvm::Function>(Callee)) { |
Alexey Bataev | 2c7eee5 | 2017-08-04 19:10:54 +0000 | [diff] [blame] | 8637 | if (Fn->doesNotThrow()) { |
Alexey Bataev | 3c595a6 | 2017-08-14 15:01:03 +0000 | [diff] [blame] | 8638 | CGF.EmitNounwindRuntimeCall(Fn, Args); |
Alexey Bataev | 2c7eee5 | 2017-08-04 19:10:54 +0000 | [diff] [blame] | 8639 | return; |
| 8640 | } |
| 8641 | } |
Alexey Bataev | 3c595a6 | 2017-08-14 15:01:03 +0000 | [diff] [blame] | 8642 | CGF.EmitRuntimeCall(Callee, Args); |
| 8643 | } |
| 8644 | |
| 8645 | void CGOpenMPRuntime::emitOutlinedFunctionCall( |
| 8646 | CodeGenFunction &CGF, SourceLocation Loc, llvm::Value *OutlinedFn, |
| 8647 | ArrayRef<llvm::Value *> Args) const { |
Alexey Bataev | 7ef47a6 | 2018-02-22 18:33:31 +0000 | [diff] [blame] | 8648 | emitCall(CGF, Loc, OutlinedFn, Args); |
Alexey Bataev | 2c7eee5 | 2017-08-04 19:10:54 +0000 | [diff] [blame] | 8649 | } |
Alexey Bataev | 3b8d558 | 2017-08-08 18:04:06 +0000 | [diff] [blame] | 8650 | |
| 8651 | Address CGOpenMPRuntime::getParameterAddress(CodeGenFunction &CGF, |
| 8652 | const VarDecl *NativeParam, |
| 8653 | const VarDecl *TargetParam) const { |
| 8654 | return CGF.GetAddrOfLocalVar(NativeParam); |
| 8655 | } |
Alexey Bataev | a8a9153a | 2017-12-29 18:07:07 +0000 | [diff] [blame] | 8656 | |
Gheorghe-Teodor Bercea | d3dcf2f | 2018-03-14 14:17:45 +0000 | [diff] [blame] | 8657 | Address CGOpenMPRuntime::getAddressOfLocalVariable(CodeGenFunction &CGF, |
| 8658 | const VarDecl *VD) { |
| 8659 | return Address::invalid(); |
| 8660 | } |
| 8661 | |
Alexey Bataev | a8a9153a | 2017-12-29 18:07:07 +0000 | [diff] [blame] | 8662 | llvm::Value *CGOpenMPSIMDRuntime::emitParallelOutlinedFunction( |
| 8663 | const OMPExecutableDirective &D, const VarDecl *ThreadIDVar, |
| 8664 | OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen) { |
| 8665 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 8666 | } |
| 8667 | |
| 8668 | llvm::Value *CGOpenMPSIMDRuntime::emitTeamsOutlinedFunction( |
| 8669 | const OMPExecutableDirective &D, const VarDecl *ThreadIDVar, |
| 8670 | OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen) { |
| 8671 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 8672 | } |
| 8673 | |
| 8674 | llvm::Value *CGOpenMPSIMDRuntime::emitTaskOutlinedFunction( |
| 8675 | const OMPExecutableDirective &D, const VarDecl *ThreadIDVar, |
| 8676 | const VarDecl *PartIDVar, const VarDecl *TaskTVar, |
| 8677 | OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen, |
| 8678 | bool Tied, unsigned &NumberOfParts) { |
| 8679 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 8680 | } |
| 8681 | |
| 8682 | void CGOpenMPSIMDRuntime::emitParallelCall(CodeGenFunction &CGF, |
| 8683 | SourceLocation Loc, |
| 8684 | llvm::Value *OutlinedFn, |
| 8685 | ArrayRef<llvm::Value *> CapturedVars, |
| 8686 | const Expr *IfCond) { |
| 8687 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 8688 | } |
| 8689 | |
| 8690 | void CGOpenMPSIMDRuntime::emitCriticalRegion( |
| 8691 | CodeGenFunction &CGF, StringRef CriticalName, |
| 8692 | const RegionCodeGenTy &CriticalOpGen, SourceLocation Loc, |
| 8693 | const Expr *Hint) { |
| 8694 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 8695 | } |
| 8696 | |
| 8697 | void CGOpenMPSIMDRuntime::emitMasterRegion(CodeGenFunction &CGF, |
| 8698 | const RegionCodeGenTy &MasterOpGen, |
| 8699 | SourceLocation Loc) { |
| 8700 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 8701 | } |
| 8702 | |
| 8703 | void CGOpenMPSIMDRuntime::emitTaskyieldCall(CodeGenFunction &CGF, |
| 8704 | SourceLocation Loc) { |
| 8705 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 8706 | } |
| 8707 | |
| 8708 | void CGOpenMPSIMDRuntime::emitTaskgroupRegion( |
| 8709 | CodeGenFunction &CGF, const RegionCodeGenTy &TaskgroupOpGen, |
| 8710 | SourceLocation Loc) { |
| 8711 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 8712 | } |
| 8713 | |
| 8714 | void CGOpenMPSIMDRuntime::emitSingleRegion( |
| 8715 | CodeGenFunction &CGF, const RegionCodeGenTy &SingleOpGen, |
| 8716 | SourceLocation Loc, ArrayRef<const Expr *> CopyprivateVars, |
| 8717 | ArrayRef<const Expr *> DestExprs, ArrayRef<const Expr *> SrcExprs, |
| 8718 | ArrayRef<const Expr *> AssignmentOps) { |
| 8719 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 8720 | } |
| 8721 | |
| 8722 | void CGOpenMPSIMDRuntime::emitOrderedRegion(CodeGenFunction &CGF, |
| 8723 | const RegionCodeGenTy &OrderedOpGen, |
| 8724 | SourceLocation Loc, |
| 8725 | bool IsThreads) { |
| 8726 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 8727 | } |
| 8728 | |
| 8729 | void CGOpenMPSIMDRuntime::emitBarrierCall(CodeGenFunction &CGF, |
| 8730 | SourceLocation Loc, |
| 8731 | OpenMPDirectiveKind Kind, |
| 8732 | bool EmitChecks, |
| 8733 | bool ForceSimpleCall) { |
| 8734 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 8735 | } |
| 8736 | |
| 8737 | void CGOpenMPSIMDRuntime::emitForDispatchInit( |
| 8738 | CodeGenFunction &CGF, SourceLocation Loc, |
| 8739 | const OpenMPScheduleTy &ScheduleKind, unsigned IVSize, bool IVSigned, |
| 8740 | bool Ordered, const DispatchRTInput &DispatchValues) { |
| 8741 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 8742 | } |
| 8743 | |
| 8744 | void CGOpenMPSIMDRuntime::emitForStaticInit( |
| 8745 | CodeGenFunction &CGF, SourceLocation Loc, OpenMPDirectiveKind DKind, |
| 8746 | const OpenMPScheduleTy &ScheduleKind, const StaticRTInput &Values) { |
| 8747 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 8748 | } |
| 8749 | |
| 8750 | void CGOpenMPSIMDRuntime::emitDistributeStaticInit( |
| 8751 | CodeGenFunction &CGF, SourceLocation Loc, |
| 8752 | OpenMPDistScheduleClauseKind SchedKind, const StaticRTInput &Values) { |
| 8753 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 8754 | } |
| 8755 | |
| 8756 | void CGOpenMPSIMDRuntime::emitForOrderedIterationEnd(CodeGenFunction &CGF, |
| 8757 | SourceLocation Loc, |
| 8758 | unsigned IVSize, |
| 8759 | bool IVSigned) { |
| 8760 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 8761 | } |
| 8762 | |
| 8763 | void CGOpenMPSIMDRuntime::emitForStaticFinish(CodeGenFunction &CGF, |
| 8764 | SourceLocation Loc, |
| 8765 | OpenMPDirectiveKind DKind) { |
| 8766 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 8767 | } |
| 8768 | |
| 8769 | llvm::Value *CGOpenMPSIMDRuntime::emitForNext(CodeGenFunction &CGF, |
| 8770 | SourceLocation Loc, |
| 8771 | unsigned IVSize, bool IVSigned, |
| 8772 | Address IL, Address LB, |
| 8773 | Address UB, Address ST) { |
| 8774 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 8775 | } |
| 8776 | |
| 8777 | void CGOpenMPSIMDRuntime::emitNumThreadsClause(CodeGenFunction &CGF, |
| 8778 | llvm::Value *NumThreads, |
| 8779 | SourceLocation Loc) { |
| 8780 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 8781 | } |
| 8782 | |
| 8783 | void CGOpenMPSIMDRuntime::emitProcBindClause(CodeGenFunction &CGF, |
| 8784 | OpenMPProcBindClauseKind ProcBind, |
| 8785 | SourceLocation Loc) { |
| 8786 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 8787 | } |
| 8788 | |
| 8789 | Address CGOpenMPSIMDRuntime::getAddrOfThreadPrivate(CodeGenFunction &CGF, |
| 8790 | const VarDecl *VD, |
| 8791 | Address VDAddr, |
| 8792 | SourceLocation Loc) { |
| 8793 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 8794 | } |
| 8795 | |
| 8796 | llvm::Function *CGOpenMPSIMDRuntime::emitThreadPrivateVarDefinition( |
| 8797 | const VarDecl *VD, Address VDAddr, SourceLocation Loc, bool PerformInit, |
| 8798 | CodeGenFunction *CGF) { |
| 8799 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 8800 | } |
| 8801 | |
| 8802 | Address CGOpenMPSIMDRuntime::getAddrOfArtificialThreadPrivate( |
| 8803 | CodeGenFunction &CGF, QualType VarType, StringRef Name) { |
| 8804 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 8805 | } |
| 8806 | |
| 8807 | void CGOpenMPSIMDRuntime::emitFlush(CodeGenFunction &CGF, |
| 8808 | ArrayRef<const Expr *> Vars, |
| 8809 | SourceLocation Loc) { |
| 8810 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 8811 | } |
| 8812 | |
| 8813 | void CGOpenMPSIMDRuntime::emitTaskCall(CodeGenFunction &CGF, SourceLocation Loc, |
| 8814 | const OMPExecutableDirective &D, |
| 8815 | llvm::Value *TaskFunction, |
| 8816 | QualType SharedsTy, Address Shareds, |
| 8817 | const Expr *IfCond, |
| 8818 | const OMPTaskDataTy &Data) { |
| 8819 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 8820 | } |
| 8821 | |
| 8822 | void CGOpenMPSIMDRuntime::emitTaskLoopCall( |
| 8823 | CodeGenFunction &CGF, SourceLocation Loc, const OMPLoopDirective &D, |
| 8824 | llvm::Value *TaskFunction, QualType SharedsTy, Address Shareds, |
| 8825 | const Expr *IfCond, const OMPTaskDataTy &Data) { |
| 8826 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 8827 | } |
| 8828 | |
| 8829 | void CGOpenMPSIMDRuntime::emitReduction( |
| 8830 | CodeGenFunction &CGF, SourceLocation Loc, ArrayRef<const Expr *> Privates, |
| 8831 | ArrayRef<const Expr *> LHSExprs, ArrayRef<const Expr *> RHSExprs, |
| 8832 | ArrayRef<const Expr *> ReductionOps, ReductionOptionsTy Options) { |
| 8833 | assert(Options.SimpleReduction && "Only simple reduction is expected."); |
| 8834 | CGOpenMPRuntime::emitReduction(CGF, Loc, Privates, LHSExprs, RHSExprs, |
| 8835 | ReductionOps, Options); |
| 8836 | } |
| 8837 | |
| 8838 | llvm::Value *CGOpenMPSIMDRuntime::emitTaskReductionInit( |
| 8839 | CodeGenFunction &CGF, SourceLocation Loc, ArrayRef<const Expr *> LHSExprs, |
| 8840 | ArrayRef<const Expr *> RHSExprs, const OMPTaskDataTy &Data) { |
| 8841 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 8842 | } |
| 8843 | |
| 8844 | void CGOpenMPSIMDRuntime::emitTaskReductionFixups(CodeGenFunction &CGF, |
| 8845 | SourceLocation Loc, |
| 8846 | ReductionCodeGen &RCG, |
| 8847 | unsigned N) { |
| 8848 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 8849 | } |
| 8850 | |
| 8851 | Address CGOpenMPSIMDRuntime::getTaskReductionItem(CodeGenFunction &CGF, |
| 8852 | SourceLocation Loc, |
| 8853 | llvm::Value *ReductionsPtr, |
| 8854 | LValue SharedLVal) { |
| 8855 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 8856 | } |
| 8857 | |
| 8858 | void CGOpenMPSIMDRuntime::emitTaskwaitCall(CodeGenFunction &CGF, |
| 8859 | SourceLocation Loc) { |
| 8860 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 8861 | } |
| 8862 | |
| 8863 | void CGOpenMPSIMDRuntime::emitCancellationPointCall( |
| 8864 | CodeGenFunction &CGF, SourceLocation Loc, |
| 8865 | OpenMPDirectiveKind CancelRegion) { |
| 8866 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 8867 | } |
| 8868 | |
| 8869 | void CGOpenMPSIMDRuntime::emitCancelCall(CodeGenFunction &CGF, |
| 8870 | SourceLocation Loc, const Expr *IfCond, |
| 8871 | OpenMPDirectiveKind CancelRegion) { |
| 8872 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 8873 | } |
| 8874 | |
| 8875 | void CGOpenMPSIMDRuntime::emitTargetOutlinedFunction( |
| 8876 | const OMPExecutableDirective &D, StringRef ParentName, |
| 8877 | llvm::Function *&OutlinedFn, llvm::Constant *&OutlinedFnID, |
| 8878 | bool IsOffloadEntry, const RegionCodeGenTy &CodeGen) { |
| 8879 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 8880 | } |
| 8881 | |
| 8882 | void CGOpenMPSIMDRuntime::emitTargetCall(CodeGenFunction &CGF, |
| 8883 | const OMPExecutableDirective &D, |
| 8884 | llvm::Value *OutlinedFn, |
| 8885 | llvm::Value *OutlinedFnID, |
Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 8886 | const Expr *IfCond, const Expr *Device) { |
Alexey Bataev | a8a9153a | 2017-12-29 18:07:07 +0000 | [diff] [blame] | 8887 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 8888 | } |
| 8889 | |
| 8890 | bool CGOpenMPSIMDRuntime::emitTargetFunctions(GlobalDecl GD) { |
| 8891 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 8892 | } |
| 8893 | |
| 8894 | bool CGOpenMPSIMDRuntime::emitTargetGlobalVariable(GlobalDecl GD) { |
| 8895 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 8896 | } |
| 8897 | |
| 8898 | bool CGOpenMPSIMDRuntime::emitTargetGlobal(GlobalDecl GD) { |
| 8899 | return false; |
| 8900 | } |
| 8901 | |
| 8902 | llvm::Function *CGOpenMPSIMDRuntime::emitRegistrationFunction() { |
| 8903 | return nullptr; |
| 8904 | } |
| 8905 | |
| 8906 | void CGOpenMPSIMDRuntime::emitTeamsCall(CodeGenFunction &CGF, |
| 8907 | const OMPExecutableDirective &D, |
| 8908 | SourceLocation Loc, |
| 8909 | llvm::Value *OutlinedFn, |
| 8910 | ArrayRef<llvm::Value *> CapturedVars) { |
| 8911 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 8912 | } |
| 8913 | |
| 8914 | void CGOpenMPSIMDRuntime::emitNumTeamsClause(CodeGenFunction &CGF, |
| 8915 | const Expr *NumTeams, |
| 8916 | const Expr *ThreadLimit, |
| 8917 | SourceLocation Loc) { |
| 8918 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 8919 | } |
| 8920 | |
| 8921 | void CGOpenMPSIMDRuntime::emitTargetDataCalls( |
| 8922 | CodeGenFunction &CGF, const OMPExecutableDirective &D, const Expr *IfCond, |
| 8923 | const Expr *Device, const RegionCodeGenTy &CodeGen, TargetDataInfo &Info) { |
| 8924 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 8925 | } |
| 8926 | |
| 8927 | void CGOpenMPSIMDRuntime::emitTargetDataStandAloneCall( |
| 8928 | CodeGenFunction &CGF, const OMPExecutableDirective &D, const Expr *IfCond, |
| 8929 | const Expr *Device) { |
| 8930 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 8931 | } |
| 8932 | |
| 8933 | void CGOpenMPSIMDRuntime::emitDoacrossInit(CodeGenFunction &CGF, |
| 8934 | const OMPLoopDirective &D) { |
| 8935 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 8936 | } |
| 8937 | |
| 8938 | void CGOpenMPSIMDRuntime::emitDoacrossOrdered(CodeGenFunction &CGF, |
| 8939 | const OMPDependClause *C) { |
| 8940 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 8941 | } |
| 8942 | |
| 8943 | const VarDecl * |
| 8944 | CGOpenMPSIMDRuntime::translateParameter(const FieldDecl *FD, |
| 8945 | const VarDecl *NativeParam) const { |
| 8946 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 8947 | } |
| 8948 | |
| 8949 | Address |
| 8950 | CGOpenMPSIMDRuntime::getParameterAddress(CodeGenFunction &CGF, |
| 8951 | const VarDecl *NativeParam, |
| 8952 | const VarDecl *TargetParam) const { |
| 8953 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 8954 | } |
| 8955 | |