Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1 | //===----- CGOpenMPRuntime.cpp - Interface to OpenMP Runtimes -------------===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This provides a class for OpenMP runtime code generation. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 13 | #include "CGCXXABI.h" |
| 14 | #include "CGCleanup.h" |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 15 | #include "CGOpenMPRuntime.h" |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 16 | #include "CGRecordLayout.h" |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 17 | #include "CodeGenFunction.h" |
John McCall | 5ad7407 | 2017-03-02 20:04:19 +0000 | [diff] [blame] | 18 | #include "clang/CodeGen/ConstantInitBuilder.h" |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 19 | #include "clang/AST/Decl.h" |
Chandler Carruth | 0d9593d | 2015-01-14 11:29:14 +0000 | [diff] [blame] | 20 | #include "clang/AST/StmtOpenMP.h" |
Richard Smith | bf5bcf2 | 2018-06-26 23:20:26 +0000 | [diff] [blame] | 21 | #include "clang/Basic/BitmaskEnum.h" |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/ArrayRef.h" |
Teresa Johnson | ffc4e24 | 2016-11-11 05:35:12 +0000 | [diff] [blame] | 23 | #include "llvm/Bitcode/BitcodeReader.h" |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 24 | #include "llvm/IR/DerivedTypes.h" |
| 25 | #include "llvm/IR/GlobalValue.h" |
| 26 | #include "llvm/IR/Value.h" |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 27 | #include "llvm/Support/Format.h" |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 28 | #include "llvm/Support/raw_ostream.h" |
Alexey Bataev | 23b6942 | 2014-06-18 07:08:49 +0000 | [diff] [blame] | 29 | #include <cassert> |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 30 | |
| 31 | using namespace clang; |
| 32 | using namespace CodeGen; |
| 33 | |
Benjamin Kramer | c52193f | 2014-10-10 13:57:57 +0000 | [diff] [blame] | 34 | namespace { |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 35 | /// Base class for handling code generation inside OpenMP regions. |
Alexey Bataev | 1809571 | 2014-10-10 12:19:54 +0000 | [diff] [blame] | 36 | class CGOpenMPRegionInfo : public CodeGenFunction::CGCapturedStmtInfo { |
| 37 | public: |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 38 | /// Kinds of OpenMP regions used in codegen. |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 39 | enum CGOpenMPRegionKind { |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 40 | /// Region with outlined function for standalone 'parallel' |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 41 | /// directive. |
| 42 | ParallelOutlinedRegion, |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 43 | /// Region with outlined function for standalone 'task' directive. |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 44 | TaskOutlinedRegion, |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 45 | /// Region for constructs that do not require function outlining, |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 46 | /// like 'for', 'sections', 'atomic' etc. directives. |
| 47 | InlinedRegion, |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 48 | /// Region with outlined function for standalone 'target' directive. |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 49 | TargetRegion, |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 50 | }; |
Alexey Bataev | 1809571 | 2014-10-10 12:19:54 +0000 | [diff] [blame] | 51 | |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 52 | CGOpenMPRegionInfo(const CapturedStmt &CS, |
| 53 | const CGOpenMPRegionKind RegionKind, |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 54 | const RegionCodeGenTy &CodeGen, OpenMPDirectiveKind Kind, |
| 55 | bool HasCancel) |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 56 | : CGCapturedStmtInfo(CS, CR_OpenMP), RegionKind(RegionKind), |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 57 | CodeGen(CodeGen), Kind(Kind), HasCancel(HasCancel) {} |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 58 | |
| 59 | CGOpenMPRegionInfo(const CGOpenMPRegionKind RegionKind, |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 60 | const RegionCodeGenTy &CodeGen, OpenMPDirectiveKind Kind, |
| 61 | bool HasCancel) |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 62 | : CGCapturedStmtInfo(CR_OpenMP), RegionKind(RegionKind), CodeGen(CodeGen), |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 63 | Kind(Kind), HasCancel(HasCancel) {} |
Alexey Bataev | 8cbe0a6 | 2015-02-26 10:27:34 +0000 | [diff] [blame] | 64 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 65 | /// Get a variable or parameter for storing global thread id |
Alexey Bataev | 1809571 | 2014-10-10 12:19:54 +0000 | [diff] [blame] | 66 | /// inside OpenMP construct. |
Alexey Bataev | 8cbe0a6 | 2015-02-26 10:27:34 +0000 | [diff] [blame] | 67 | virtual const VarDecl *getThreadIDVariable() const = 0; |
Alexey Bataev | 1809571 | 2014-10-10 12:19:54 +0000 | [diff] [blame] | 68 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 69 | /// Emit the captured statement body. |
Hans Wennborg | 7eb5464 | 2015-09-10 17:07:54 +0000 | [diff] [blame] | 70 | void EmitBody(CodeGenFunction &CGF, const Stmt *S) override; |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 71 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 72 | /// Get an LValue for the current ThreadID variable. |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 73 | /// \return LValue for thread id variable. This LValue always has type int32*. |
| 74 | virtual LValue getThreadIDVariableLValue(CodeGenFunction &CGF); |
Alexey Bataev | 1809571 | 2014-10-10 12:19:54 +0000 | [diff] [blame] | 75 | |
Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 76 | virtual void emitUntiedSwitch(CodeGenFunction & /*CGF*/) {} |
| 77 | |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 78 | CGOpenMPRegionKind getRegionKind() const { return RegionKind; } |
Alexey Bataev | 8cbe0a6 | 2015-02-26 10:27:34 +0000 | [diff] [blame] | 79 | |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 80 | OpenMPDirectiveKind getDirectiveKind() const { return Kind; } |
| 81 | |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 82 | bool hasCancel() const { return HasCancel; } |
| 83 | |
Alexey Bataev | 1809571 | 2014-10-10 12:19:54 +0000 | [diff] [blame] | 84 | static bool classof(const CGCapturedStmtInfo *Info) { |
| 85 | return Info->getKind() == CR_OpenMP; |
| 86 | } |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 87 | |
Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 88 | ~CGOpenMPRegionInfo() override = default; |
| 89 | |
Alexey Bataev | 8cbe0a6 | 2015-02-26 10:27:34 +0000 | [diff] [blame] | 90 | protected: |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 91 | CGOpenMPRegionKind RegionKind; |
Hans Wennborg | 45c7439 | 2016-01-12 20:54:36 +0000 | [diff] [blame] | 92 | RegionCodeGenTy CodeGen; |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 93 | OpenMPDirectiveKind Kind; |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 94 | bool HasCancel; |
Alexey Bataev | 8cbe0a6 | 2015-02-26 10:27:34 +0000 | [diff] [blame] | 95 | }; |
Alexey Bataev | 1809571 | 2014-10-10 12:19:54 +0000 | [diff] [blame] | 96 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 97 | /// API for captured statement code generation in OpenMP constructs. |
Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 98 | class CGOpenMPOutlinedRegionInfo final : public CGOpenMPRegionInfo { |
Alexey Bataev | 8cbe0a6 | 2015-02-26 10:27:34 +0000 | [diff] [blame] | 99 | public: |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 100 | CGOpenMPOutlinedRegionInfo(const CapturedStmt &CS, const VarDecl *ThreadIDVar, |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 101 | const RegionCodeGenTy &CodeGen, |
Arpith Chacko Jacob | bb36fe8 | 2017-01-10 15:42:51 +0000 | [diff] [blame] | 102 | OpenMPDirectiveKind Kind, bool HasCancel, |
| 103 | StringRef HelperName) |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 104 | : CGOpenMPRegionInfo(CS, ParallelOutlinedRegion, CodeGen, Kind, |
| 105 | HasCancel), |
Arpith Chacko Jacob | bb36fe8 | 2017-01-10 15:42:51 +0000 | [diff] [blame] | 106 | ThreadIDVar(ThreadIDVar), HelperName(HelperName) { |
Alexey Bataev | 8cbe0a6 | 2015-02-26 10:27:34 +0000 | [diff] [blame] | 107 | assert(ThreadIDVar != nullptr && "No ThreadID in OpenMP region."); |
| 108 | } |
Eugene Zelenko | 0a4f3f4 | 2016-02-10 19:11:58 +0000 | [diff] [blame] | 109 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 110 | /// Get a variable or parameter for storing global thread id |
Alexey Bataev | 8cbe0a6 | 2015-02-26 10:27:34 +0000 | [diff] [blame] | 111 | /// inside OpenMP construct. |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 112 | const VarDecl *getThreadIDVariable() const override { return ThreadIDVar; } |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 113 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 114 | /// Get the name of the capture helper. |
Arpith Chacko Jacob | bb36fe8 | 2017-01-10 15:42:51 +0000 | [diff] [blame] | 115 | StringRef getHelperName() const override { return HelperName; } |
Alexey Bataev | 1809571 | 2014-10-10 12:19:54 +0000 | [diff] [blame] | 116 | |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 117 | static bool classof(const CGCapturedStmtInfo *Info) { |
| 118 | return CGOpenMPRegionInfo::classof(Info) && |
| 119 | cast<CGOpenMPRegionInfo>(Info)->getRegionKind() == |
| 120 | ParallelOutlinedRegion; |
| 121 | } |
| 122 | |
Alexey Bataev | 1809571 | 2014-10-10 12:19:54 +0000 | [diff] [blame] | 123 | private: |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 124 | /// A variable or parameter storing global thread id for OpenMP |
Alexey Bataev | 1809571 | 2014-10-10 12:19:54 +0000 | [diff] [blame] | 125 | /// constructs. |
| 126 | const VarDecl *ThreadIDVar; |
Arpith Chacko Jacob | bb36fe8 | 2017-01-10 15:42:51 +0000 | [diff] [blame] | 127 | StringRef HelperName; |
Alexey Bataev | 8cbe0a6 | 2015-02-26 10:27:34 +0000 | [diff] [blame] | 128 | }; |
| 129 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 130 | /// API for captured statement code generation in OpenMP constructs. |
Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 131 | class CGOpenMPTaskOutlinedRegionInfo final : public CGOpenMPRegionInfo { |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 132 | public: |
Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 133 | class UntiedTaskActionTy final : public PrePostActionTy { |
| 134 | bool Untied; |
| 135 | const VarDecl *PartIDVar; |
| 136 | const RegionCodeGenTy UntiedCodeGen; |
| 137 | llvm::SwitchInst *UntiedSwitch = nullptr; |
| 138 | |
| 139 | public: |
| 140 | UntiedTaskActionTy(bool Tied, const VarDecl *PartIDVar, |
| 141 | const RegionCodeGenTy &UntiedCodeGen) |
| 142 | : Untied(!Tied), PartIDVar(PartIDVar), UntiedCodeGen(UntiedCodeGen) {} |
| 143 | void Enter(CodeGenFunction &CGF) override { |
| 144 | if (Untied) { |
| 145 | // Emit task switching point. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 146 | LValue PartIdLVal = CGF.EmitLoadOfPointerLValue( |
Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 147 | CGF.GetAddrOfLocalVar(PartIDVar), |
| 148 | PartIDVar->getType()->castAs<PointerType>()); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 149 | llvm::Value *Res = |
| 150 | CGF.EmitLoadOfScalar(PartIdLVal, PartIDVar->getLocation()); |
| 151 | llvm::BasicBlock *DoneBB = CGF.createBasicBlock(".untied.done."); |
Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 152 | UntiedSwitch = CGF.Builder.CreateSwitch(Res, DoneBB); |
| 153 | CGF.EmitBlock(DoneBB); |
| 154 | CGF.EmitBranchThroughCleanup(CGF.ReturnBlock); |
| 155 | CGF.EmitBlock(CGF.createBasicBlock(".untied.jmp.")); |
| 156 | UntiedSwitch->addCase(CGF.Builder.getInt32(0), |
| 157 | CGF.Builder.GetInsertBlock()); |
| 158 | emitUntiedSwitch(CGF); |
| 159 | } |
| 160 | } |
| 161 | void emitUntiedSwitch(CodeGenFunction &CGF) const { |
| 162 | if (Untied) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 163 | LValue PartIdLVal = CGF.EmitLoadOfPointerLValue( |
Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 164 | CGF.GetAddrOfLocalVar(PartIDVar), |
| 165 | PartIDVar->getType()->castAs<PointerType>()); |
| 166 | CGF.EmitStoreOfScalar(CGF.Builder.getInt32(UntiedSwitch->getNumCases()), |
| 167 | PartIdLVal); |
| 168 | UntiedCodeGen(CGF); |
| 169 | CodeGenFunction::JumpDest CurPoint = |
| 170 | CGF.getJumpDestInCurrentScope(".untied.next."); |
| 171 | CGF.EmitBranchThroughCleanup(CGF.ReturnBlock); |
| 172 | CGF.EmitBlock(CGF.createBasicBlock(".untied.jmp.")); |
| 173 | UntiedSwitch->addCase(CGF.Builder.getInt32(UntiedSwitch->getNumCases()), |
| 174 | CGF.Builder.GetInsertBlock()); |
| 175 | CGF.EmitBranchThroughCleanup(CurPoint); |
| 176 | CGF.EmitBlock(CurPoint.getBlock()); |
| 177 | } |
| 178 | } |
| 179 | unsigned getNumberOfParts() const { return UntiedSwitch->getNumCases(); } |
| 180 | }; |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 181 | CGOpenMPTaskOutlinedRegionInfo(const CapturedStmt &CS, |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 182 | const VarDecl *ThreadIDVar, |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 183 | const RegionCodeGenTy &CodeGen, |
Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 184 | OpenMPDirectiveKind Kind, bool HasCancel, |
| 185 | const UntiedTaskActionTy &Action) |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 186 | : CGOpenMPRegionInfo(CS, TaskOutlinedRegion, CodeGen, Kind, HasCancel), |
Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 187 | ThreadIDVar(ThreadIDVar), Action(Action) { |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 188 | assert(ThreadIDVar != nullptr && "No ThreadID in OpenMP region."); |
| 189 | } |
Eugene Zelenko | 0a4f3f4 | 2016-02-10 19:11:58 +0000 | [diff] [blame] | 190 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 191 | /// Get a variable or parameter for storing global thread id |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 192 | /// inside OpenMP construct. |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 193 | const VarDecl *getThreadIDVariable() const override { return ThreadIDVar; } |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 194 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 195 | /// Get an LValue for the current ThreadID variable. |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 196 | LValue getThreadIDVariableLValue(CodeGenFunction &CGF) override; |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 197 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 198 | /// Get the name of the capture helper. |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 199 | StringRef getHelperName() const override { return ".omp_outlined."; } |
| 200 | |
Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 201 | void emitUntiedSwitch(CodeGenFunction &CGF) override { |
| 202 | Action.emitUntiedSwitch(CGF); |
| 203 | } |
| 204 | |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 205 | static bool classof(const CGCapturedStmtInfo *Info) { |
| 206 | return CGOpenMPRegionInfo::classof(Info) && |
| 207 | cast<CGOpenMPRegionInfo>(Info)->getRegionKind() == |
| 208 | TaskOutlinedRegion; |
| 209 | } |
| 210 | |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 211 | private: |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 212 | /// A variable or parameter storing global thread id for OpenMP |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 213 | /// constructs. |
| 214 | const VarDecl *ThreadIDVar; |
Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 215 | /// Action for emitting code for untied tasks. |
| 216 | const UntiedTaskActionTy &Action; |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 217 | }; |
| 218 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 219 | /// API for inlined captured statement code generation in OpenMP |
Alexey Bataev | 8cbe0a6 | 2015-02-26 10:27:34 +0000 | [diff] [blame] | 220 | /// constructs. |
| 221 | class CGOpenMPInlinedRegionInfo : public CGOpenMPRegionInfo { |
| 222 | public: |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 223 | CGOpenMPInlinedRegionInfo(CodeGenFunction::CGCapturedStmtInfo *OldCSI, |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 224 | const RegionCodeGenTy &CodeGen, |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 225 | OpenMPDirectiveKind Kind, bool HasCancel) |
| 226 | : CGOpenMPRegionInfo(InlinedRegion, CodeGen, Kind, HasCancel), |
| 227 | OldCSI(OldCSI), |
Alexey Bataev | 8cbe0a6 | 2015-02-26 10:27:34 +0000 | [diff] [blame] | 228 | OuterRegionInfo(dyn_cast_or_null<CGOpenMPRegionInfo>(OldCSI)) {} |
Eugene Zelenko | 0a4f3f4 | 2016-02-10 19:11:58 +0000 | [diff] [blame] | 229 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 230 | // Retrieve the value of the context parameter. |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 231 | llvm::Value *getContextValue() const override { |
Alexey Bataev | 8cbe0a6 | 2015-02-26 10:27:34 +0000 | [diff] [blame] | 232 | if (OuterRegionInfo) |
| 233 | return OuterRegionInfo->getContextValue(); |
| 234 | llvm_unreachable("No context value for inlined OpenMP region"); |
| 235 | } |
Eugene Zelenko | 0a4f3f4 | 2016-02-10 19:11:58 +0000 | [diff] [blame] | 236 | |
Hans Wennborg | 7eb5464 | 2015-09-10 17:07:54 +0000 | [diff] [blame] | 237 | void setContextValue(llvm::Value *V) override { |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 238 | if (OuterRegionInfo) { |
| 239 | OuterRegionInfo->setContextValue(V); |
| 240 | return; |
| 241 | } |
| 242 | llvm_unreachable("No context value for inlined OpenMP region"); |
| 243 | } |
Eugene Zelenko | 0a4f3f4 | 2016-02-10 19:11:58 +0000 | [diff] [blame] | 244 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 245 | /// Lookup the captured field decl for a variable. |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 246 | const FieldDecl *lookup(const VarDecl *VD) const override { |
Alexey Bataev | 8cbe0a6 | 2015-02-26 10:27:34 +0000 | [diff] [blame] | 247 | if (OuterRegionInfo) |
| 248 | return OuterRegionInfo->lookup(VD); |
Alexey Bataev | 69c62a9 | 2015-04-15 04:52:20 +0000 | [diff] [blame] | 249 | // If there is no outer outlined region,no need to lookup in a list of |
| 250 | // captured variables, we can use the original one. |
| 251 | return nullptr; |
Alexey Bataev | 8cbe0a6 | 2015-02-26 10:27:34 +0000 | [diff] [blame] | 252 | } |
Eugene Zelenko | 0a4f3f4 | 2016-02-10 19:11:58 +0000 | [diff] [blame] | 253 | |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 254 | FieldDecl *getThisFieldDecl() const override { |
Alexey Bataev | 8cbe0a6 | 2015-02-26 10:27:34 +0000 | [diff] [blame] | 255 | if (OuterRegionInfo) |
| 256 | return OuterRegionInfo->getThisFieldDecl(); |
| 257 | return nullptr; |
| 258 | } |
Eugene Zelenko | 0a4f3f4 | 2016-02-10 19:11:58 +0000 | [diff] [blame] | 259 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 260 | /// Get a variable or parameter for storing global thread id |
Alexey Bataev | 8cbe0a6 | 2015-02-26 10:27:34 +0000 | [diff] [blame] | 261 | /// inside OpenMP construct. |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 262 | const VarDecl *getThreadIDVariable() const override { |
Alexey Bataev | 8cbe0a6 | 2015-02-26 10:27:34 +0000 | [diff] [blame] | 263 | if (OuterRegionInfo) |
| 264 | return OuterRegionInfo->getThreadIDVariable(); |
| 265 | return nullptr; |
| 266 | } |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 267 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 268 | /// Get an LValue for the current ThreadID variable. |
Alexey Bataev | 311a928 | 2017-10-12 13:51:32 +0000 | [diff] [blame] | 269 | LValue getThreadIDVariableLValue(CodeGenFunction &CGF) override { |
| 270 | if (OuterRegionInfo) |
| 271 | return OuterRegionInfo->getThreadIDVariableLValue(CGF); |
| 272 | llvm_unreachable("No LValue for inlined OpenMP construct"); |
| 273 | } |
| 274 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 275 | /// Get the name of the capture helper. |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 276 | StringRef getHelperName() const override { |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 277 | if (auto *OuterRegionInfo = getOldCSI()) |
| 278 | return OuterRegionInfo->getHelperName(); |
Alexey Bataev | 8cbe0a6 | 2015-02-26 10:27:34 +0000 | [diff] [blame] | 279 | llvm_unreachable("No helper name for inlined OpenMP construct"); |
| 280 | } |
| 281 | |
Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 282 | void emitUntiedSwitch(CodeGenFunction &CGF) override { |
| 283 | if (OuterRegionInfo) |
| 284 | OuterRegionInfo->emitUntiedSwitch(CGF); |
| 285 | } |
| 286 | |
Alexey Bataev | 8cbe0a6 | 2015-02-26 10:27:34 +0000 | [diff] [blame] | 287 | CodeGenFunction::CGCapturedStmtInfo *getOldCSI() const { return OldCSI; } |
| 288 | |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 289 | static bool classof(const CGCapturedStmtInfo *Info) { |
| 290 | return CGOpenMPRegionInfo::classof(Info) && |
| 291 | cast<CGOpenMPRegionInfo>(Info)->getRegionKind() == InlinedRegion; |
| 292 | } |
| 293 | |
Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 294 | ~CGOpenMPInlinedRegionInfo() override = default; |
| 295 | |
Alexey Bataev | 8cbe0a6 | 2015-02-26 10:27:34 +0000 | [diff] [blame] | 296 | private: |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 297 | /// CodeGen info about outer OpenMP region. |
Alexey Bataev | 8cbe0a6 | 2015-02-26 10:27:34 +0000 | [diff] [blame] | 298 | CodeGenFunction::CGCapturedStmtInfo *OldCSI; |
| 299 | CGOpenMPRegionInfo *OuterRegionInfo; |
Alexey Bataev | 1809571 | 2014-10-10 12:19:54 +0000 | [diff] [blame] | 300 | }; |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 301 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 302 | /// API for captured statement code generation in OpenMP target |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 303 | /// constructs. For this captures, implicit parameters are used instead of the |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 304 | /// captured fields. The name of the target region has to be unique in a given |
| 305 | /// application so it is provided by the client, because only the client has |
| 306 | /// the information to generate that. |
Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 307 | class CGOpenMPTargetRegionInfo final : public CGOpenMPRegionInfo { |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 308 | public: |
| 309 | CGOpenMPTargetRegionInfo(const CapturedStmt &CS, |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 310 | const RegionCodeGenTy &CodeGen, StringRef HelperName) |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 311 | : CGOpenMPRegionInfo(CS, TargetRegion, CodeGen, OMPD_target, |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 312 | /*HasCancel=*/false), |
| 313 | HelperName(HelperName) {} |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 314 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 315 | /// This is unused for target regions because each starts executing |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 316 | /// with a single thread. |
| 317 | const VarDecl *getThreadIDVariable() const override { return nullptr; } |
| 318 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 319 | /// Get the name of the capture helper. |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 320 | StringRef getHelperName() const override { return HelperName; } |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 321 | |
| 322 | static bool classof(const CGCapturedStmtInfo *Info) { |
| 323 | return CGOpenMPRegionInfo::classof(Info) && |
| 324 | cast<CGOpenMPRegionInfo>(Info)->getRegionKind() == TargetRegion; |
| 325 | } |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 326 | |
| 327 | private: |
| 328 | StringRef HelperName; |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 329 | }; |
| 330 | |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 331 | static void EmptyCodeGen(CodeGenFunction &, PrePostActionTy &) { |
Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 332 | llvm_unreachable("No codegen for expressions"); |
| 333 | } |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 334 | /// API for generation of expressions captured in a innermost OpenMP |
Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 335 | /// region. |
Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 336 | class CGOpenMPInnerExprInfo final : public CGOpenMPInlinedRegionInfo { |
Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 337 | public: |
| 338 | CGOpenMPInnerExprInfo(CodeGenFunction &CGF, const CapturedStmt &CS) |
| 339 | : CGOpenMPInlinedRegionInfo(CGF.CapturedStmtInfo, EmptyCodeGen, |
| 340 | OMPD_unknown, |
| 341 | /*HasCancel=*/false), |
| 342 | PrivScope(CGF) { |
| 343 | // Make sure the globals captured in the provided statement are local by |
| 344 | // using the privatization logic. We assume the same variable is not |
| 345 | // captured more than once. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 346 | for (const auto &C : CS.captures()) { |
Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 347 | if (!C.capturesVariable() && !C.capturesVariableByCopy()) |
| 348 | continue; |
| 349 | |
| 350 | const VarDecl *VD = C.getCapturedVar(); |
| 351 | if (VD->isLocalVarDeclOrParm()) |
| 352 | continue; |
| 353 | |
Bruno Ricci | 5fc4db7 | 2018-12-21 14:10:18 +0000 | [diff] [blame] | 354 | DeclRefExpr DRE(CGF.getContext(), const_cast<VarDecl *>(VD), |
Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 355 | /*RefersToEnclosingVariableOrCapture=*/false, |
| 356 | VD->getType().getNonReferenceType(), VK_LValue, |
Alexey Bataev | a9b9cc0 | 2018-01-23 18:12:38 +0000 | [diff] [blame] | 357 | C.getLocation()); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 358 | PrivScope.addPrivate( |
| 359 | VD, [&CGF, &DRE]() { return CGF.EmitLValue(&DRE).getAddress(); }); |
Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 360 | } |
| 361 | (void)PrivScope.Privatize(); |
| 362 | } |
| 363 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 364 | /// Lookup the captured field decl for a variable. |
Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 365 | const FieldDecl *lookup(const VarDecl *VD) const override { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 366 | if (const FieldDecl *FD = CGOpenMPInlinedRegionInfo::lookup(VD)) |
Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 367 | return FD; |
| 368 | return nullptr; |
| 369 | } |
| 370 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 371 | /// Emit the captured statement body. |
Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 372 | void EmitBody(CodeGenFunction &CGF, const Stmt *S) override { |
| 373 | llvm_unreachable("No body for expressions"); |
| 374 | } |
| 375 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 376 | /// Get a variable or parameter for storing global thread id |
Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 377 | /// inside OpenMP construct. |
| 378 | const VarDecl *getThreadIDVariable() const override { |
| 379 | llvm_unreachable("No thread id for expressions"); |
| 380 | } |
| 381 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 382 | /// Get the name of the capture helper. |
Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 383 | StringRef getHelperName() const override { |
| 384 | llvm_unreachable("No helper name for expressions"); |
| 385 | } |
| 386 | |
| 387 | static bool classof(const CGCapturedStmtInfo *Info) { return false; } |
| 388 | |
| 389 | private: |
| 390 | /// Private scope to capture global variables. |
| 391 | CodeGenFunction::OMPPrivateScope PrivScope; |
| 392 | }; |
| 393 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 394 | /// RAII for emitting code of OpenMP constructs. |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 395 | class InlinedOpenMPRegionRAII { |
| 396 | CodeGenFunction &CGF; |
Alexey Bataev | 4ba78a4 | 2016-04-27 07:56:03 +0000 | [diff] [blame] | 397 | llvm::DenseMap<const VarDecl *, FieldDecl *> LambdaCaptureFields; |
| 398 | FieldDecl *LambdaThisCaptureField = nullptr; |
Alexey Bataev | a8a9153a | 2017-12-29 18:07:07 +0000 | [diff] [blame] | 399 | const CodeGen::CGBlockInfo *BlockInfo = nullptr; |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 400 | |
| 401 | public: |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 402 | /// Constructs region for combined constructs. |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 403 | /// \param CodeGen Code generation sequence for combined directives. Includes |
| 404 | /// a list of functions used for code generation of implicitly inlined |
| 405 | /// regions. |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 406 | InlinedOpenMPRegionRAII(CodeGenFunction &CGF, const RegionCodeGenTy &CodeGen, |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 407 | OpenMPDirectiveKind Kind, bool HasCancel) |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 408 | : CGF(CGF) { |
| 409 | // Start emission for the construct. |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 410 | CGF.CapturedStmtInfo = new CGOpenMPInlinedRegionInfo( |
| 411 | CGF.CapturedStmtInfo, CodeGen, Kind, HasCancel); |
Alexey Bataev | 4ba78a4 | 2016-04-27 07:56:03 +0000 | [diff] [blame] | 412 | std::swap(CGF.LambdaCaptureFields, LambdaCaptureFields); |
| 413 | LambdaThisCaptureField = CGF.LambdaThisCaptureField; |
| 414 | CGF.LambdaThisCaptureField = nullptr; |
Alexey Bataev | a8a9153a | 2017-12-29 18:07:07 +0000 | [diff] [blame] | 415 | BlockInfo = CGF.BlockInfo; |
| 416 | CGF.BlockInfo = nullptr; |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 417 | } |
Eugene Zelenko | 0a4f3f4 | 2016-02-10 19:11:58 +0000 | [diff] [blame] | 418 | |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 419 | ~InlinedOpenMPRegionRAII() { |
| 420 | // Restore original CapturedStmtInfo only if we're done with code emission. |
| 421 | auto *OldCSI = |
| 422 | cast<CGOpenMPInlinedRegionInfo>(CGF.CapturedStmtInfo)->getOldCSI(); |
| 423 | delete CGF.CapturedStmtInfo; |
| 424 | CGF.CapturedStmtInfo = OldCSI; |
Alexey Bataev | 4ba78a4 | 2016-04-27 07:56:03 +0000 | [diff] [blame] | 425 | std::swap(CGF.LambdaCaptureFields, LambdaCaptureFields); |
| 426 | CGF.LambdaThisCaptureField = LambdaThisCaptureField; |
Alexey Bataev | a8a9153a | 2017-12-29 18:07:07 +0000 | [diff] [blame] | 427 | CGF.BlockInfo = BlockInfo; |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 428 | } |
| 429 | }; |
| 430 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 431 | /// 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] | 432 | /// All enumeric elements are named and described in accordance with the code |
James Y Knight | 5d71fc5 | 2019-01-29 16:37:27 +0000 | [diff] [blame] | 433 | /// from https://github.com/llvm/llvm-project/blob/master/openmp/runtime/src/kmp.h |
Alexey Bataev | 0f87dbe | 2017-08-14 17:56:13 +0000 | [diff] [blame] | 434 | enum OpenMPLocationFlags : unsigned { |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 435 | /// Use trampoline for internal microtask. |
Alexey Bataev | 50b3c95 | 2016-02-19 10:38:26 +0000 | [diff] [blame] | 436 | OMP_IDENT_IMD = 0x01, |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 437 | /// Use c-style ident structure. |
Alexey Bataev | 50b3c95 | 2016-02-19 10:38:26 +0000 | [diff] [blame] | 438 | OMP_IDENT_KMPC = 0x02, |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 439 | /// Atomic reduction option for kmpc_reduce. |
Alexey Bataev | 50b3c95 | 2016-02-19 10:38:26 +0000 | [diff] [blame] | 440 | OMP_ATOMIC_REDUCE = 0x10, |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 441 | /// Explicit 'barrier' directive. |
Alexey Bataev | 50b3c95 | 2016-02-19 10:38:26 +0000 | [diff] [blame] | 442 | OMP_IDENT_BARRIER_EXPL = 0x20, |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 443 | /// Implicit barrier in code. |
Alexey Bataev | 50b3c95 | 2016-02-19 10:38:26 +0000 | [diff] [blame] | 444 | OMP_IDENT_BARRIER_IMPL = 0x40, |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 445 | /// Implicit barrier in 'for' directive. |
Alexey Bataev | 50b3c95 | 2016-02-19 10:38:26 +0000 | [diff] [blame] | 446 | OMP_IDENT_BARRIER_IMPL_FOR = 0x40, |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 447 | /// Implicit barrier in 'sections' directive. |
Alexey Bataev | 50b3c95 | 2016-02-19 10:38:26 +0000 | [diff] [blame] | 448 | OMP_IDENT_BARRIER_IMPL_SECTIONS = 0xC0, |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 449 | /// Implicit barrier in 'single' directive. |
Alexey Bataev | 0f87dbe | 2017-08-14 17:56:13 +0000 | [diff] [blame] | 450 | OMP_IDENT_BARRIER_IMPL_SINGLE = 0x140, |
| 451 | /// Call of __kmp_for_static_init for static loop. |
| 452 | OMP_IDENT_WORK_LOOP = 0x200, |
| 453 | /// Call of __kmp_for_static_init for sections. |
| 454 | OMP_IDENT_WORK_SECTIONS = 0x400, |
| 455 | /// Call of __kmp_for_static_init for distribute. |
| 456 | OMP_IDENT_WORK_DISTRIBUTE = 0x800, |
| 457 | LLVM_MARK_AS_BITMASK_ENUM(/*LargestValue=*/OMP_IDENT_WORK_DISTRIBUTE) |
Alexey Bataev | 50b3c95 | 2016-02-19 10:38:26 +0000 | [diff] [blame] | 458 | }; |
| 459 | |
Gheorghe-Teodor Bercea | 66cdbb47 | 2019-05-21 19:42:01 +0000 | [diff] [blame] | 460 | namespace { |
| 461 | LLVM_ENABLE_BITMASK_ENUMS_IN_NAMESPACE(); |
| 462 | /// Values for bit flags for marking which requires clauses have been used. |
| 463 | enum OpenMPOffloadingRequiresDirFlags : int64_t { |
| 464 | /// flag undefined. |
| 465 | OMP_REQ_UNDEFINED = 0x000, |
| 466 | /// no requires clause present. |
| 467 | OMP_REQ_NONE = 0x001, |
| 468 | /// reverse_offload clause. |
| 469 | OMP_REQ_REVERSE_OFFLOAD = 0x002, |
| 470 | /// unified_address clause. |
| 471 | OMP_REQ_UNIFIED_ADDRESS = 0x004, |
| 472 | /// unified_shared_memory clause. |
| 473 | OMP_REQ_UNIFIED_SHARED_MEMORY = 0x008, |
| 474 | /// dynamic_allocators clause. |
| 475 | OMP_REQ_DYNAMIC_ALLOCATORS = 0x010, |
| 476 | LLVM_MARK_AS_BITMASK_ENUM(/*LargestValue=*/OMP_REQ_DYNAMIC_ALLOCATORS) |
| 477 | }; |
Gheorghe-Teodor Bercea | 545a9fe | 2019-06-14 20:19:54 +0000 | [diff] [blame] | 478 | |
| 479 | enum OpenMPOffloadingReservedDeviceIDs { |
| 480 | /// Device ID if the device was not defined, runtime should get it |
| 481 | /// from environment variables in the spec. |
| 482 | OMP_DEVICEID_UNDEF = -1, |
| 483 | }; |
Gheorghe-Teodor Bercea | 66cdbb47 | 2019-05-21 19:42:01 +0000 | [diff] [blame] | 484 | } // anonymous namespace |
| 485 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 486 | /// Describes ident structure that describes a source location. |
Alexey Bataev | 50b3c95 | 2016-02-19 10:38:26 +0000 | [diff] [blame] | 487 | /// All descriptions are taken from |
James Y Knight | 5d71fc5 | 2019-01-29 16:37:27 +0000 | [diff] [blame] | 488 | /// https://github.com/llvm/llvm-project/blob/master/openmp/runtime/src/kmp.h |
Alexey Bataev | 50b3c95 | 2016-02-19 10:38:26 +0000 | [diff] [blame] | 489 | /// Original structure: |
| 490 | /// typedef struct ident { |
| 491 | /// kmp_int32 reserved_1; /**< might be used in Fortran; |
| 492 | /// see above */ |
| 493 | /// kmp_int32 flags; /**< also f.flags; KMP_IDENT_xxx flags; |
| 494 | /// KMP_IDENT_KMPC identifies this union |
| 495 | /// member */ |
| 496 | /// kmp_int32 reserved_2; /**< not really used in Fortran any more; |
| 497 | /// see above */ |
| 498 | ///#if USE_ITT_BUILD |
| 499 | /// /* but currently used for storing |
| 500 | /// region-specific ITT */ |
| 501 | /// /* contextual information. */ |
| 502 | ///#endif /* USE_ITT_BUILD */ |
| 503 | /// kmp_int32 reserved_3; /**< source[4] in Fortran, do not use for |
| 504 | /// C++ */ |
| 505 | /// char const *psource; /**< String describing the source location. |
| 506 | /// The string is composed of semi-colon separated |
| 507 | // fields which describe the source file, |
| 508 | /// the function and a pair of line numbers that |
| 509 | /// delimit the construct. |
| 510 | /// */ |
| 511 | /// } ident_t; |
| 512 | enum IdentFieldIndex { |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 513 | /// might be used in Fortran |
Alexey Bataev | 50b3c95 | 2016-02-19 10:38:26 +0000 | [diff] [blame] | 514 | IdentField_Reserved_1, |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 515 | /// OMP_IDENT_xxx flags; OMP_IDENT_KMPC identifies this union member. |
Alexey Bataev | 50b3c95 | 2016-02-19 10:38:26 +0000 | [diff] [blame] | 516 | IdentField_Flags, |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 517 | /// Not really used in Fortran any more |
Alexey Bataev | 50b3c95 | 2016-02-19 10:38:26 +0000 | [diff] [blame] | 518 | IdentField_Reserved_2, |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 519 | /// Source[4] in Fortran, do not use for C++ |
Alexey Bataev | 50b3c95 | 2016-02-19 10:38:26 +0000 | [diff] [blame] | 520 | IdentField_Reserved_3, |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 521 | /// String describing the source location. The string is composed of |
Alexey Bataev | 50b3c95 | 2016-02-19 10:38:26 +0000 | [diff] [blame] | 522 | /// semi-colon separated fields which describe the source file, the function |
| 523 | /// and a pair of line numbers that delimit the construct. |
| 524 | IdentField_PSource |
| 525 | }; |
| 526 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 527 | /// Schedule types for 'omp for' loops (these enumerators are taken from |
Alexey Bataev | 50b3c95 | 2016-02-19 10:38:26 +0000 | [diff] [blame] | 528 | /// the enum sched_type in kmp.h). |
| 529 | enum OpenMPSchedType { |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 530 | /// Lower bound for default (unordered) versions. |
Alexey Bataev | 50b3c95 | 2016-02-19 10:38:26 +0000 | [diff] [blame] | 531 | OMP_sch_lower = 32, |
| 532 | OMP_sch_static_chunked = 33, |
| 533 | OMP_sch_static = 34, |
| 534 | OMP_sch_dynamic_chunked = 35, |
| 535 | OMP_sch_guided_chunked = 36, |
| 536 | OMP_sch_runtime = 37, |
| 537 | OMP_sch_auto = 38, |
Alexey Bataev | 6cff624 | 2016-05-30 13:05:14 +0000 | [diff] [blame] | 538 | /// static with chunk adjustment (e.g., simd) |
Samuel Antao | 4c8035b | 2016-12-12 18:00:20 +0000 | [diff] [blame] | 539 | OMP_sch_static_balanced_chunked = 45, |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 540 | /// Lower bound for 'ordered' versions. |
Alexey Bataev | 50b3c95 | 2016-02-19 10:38:26 +0000 | [diff] [blame] | 541 | OMP_ord_lower = 64, |
| 542 | OMP_ord_static_chunked = 65, |
| 543 | OMP_ord_static = 66, |
| 544 | OMP_ord_dynamic_chunked = 67, |
| 545 | OMP_ord_guided_chunked = 68, |
| 546 | OMP_ord_runtime = 69, |
| 547 | OMP_ord_auto = 70, |
| 548 | OMP_sch_default = OMP_sch_static, |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 549 | /// dist_schedule types |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 550 | OMP_dist_sch_static_chunked = 91, |
| 551 | OMP_dist_sch_static = 92, |
Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 552 | /// Support for OpenMP 4.5 monotonic and nonmonotonic schedule modifiers. |
| 553 | /// Set if the monotonic schedule modifier was present. |
| 554 | OMP_sch_modifier_monotonic = (1 << 29), |
| 555 | /// Set if the nonmonotonic schedule modifier was present. |
| 556 | OMP_sch_modifier_nonmonotonic = (1 << 30), |
Alexey Bataev | 50b3c95 | 2016-02-19 10:38:26 +0000 | [diff] [blame] | 557 | }; |
| 558 | |
| 559 | enum OpenMPRTLFunction { |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 560 | /// Call to void __kmpc_fork_call(ident_t *loc, kmp_int32 argc, |
Alexey Bataev | 50b3c95 | 2016-02-19 10:38:26 +0000 | [diff] [blame] | 561 | /// kmpc_micro microtask, ...); |
| 562 | OMPRTL__kmpc_fork_call, |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 563 | /// Call to void *__kmpc_threadprivate_cached(ident_t *loc, |
Alexey Bataev | 50b3c95 | 2016-02-19 10:38:26 +0000 | [diff] [blame] | 564 | /// kmp_int32 global_tid, void *data, size_t size, void ***cache); |
| 565 | OMPRTL__kmpc_threadprivate_cached, |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 566 | /// Call to void __kmpc_threadprivate_register( ident_t *, |
Alexey Bataev | 50b3c95 | 2016-02-19 10:38:26 +0000 | [diff] [blame] | 567 | /// void *data, kmpc_ctor ctor, kmpc_cctor cctor, kmpc_dtor dtor); |
| 568 | OMPRTL__kmpc_threadprivate_register, |
| 569 | // Call to __kmpc_int32 kmpc_global_thread_num(ident_t *loc); |
| 570 | OMPRTL__kmpc_global_thread_num, |
| 571 | // Call to void __kmpc_critical(ident_t *loc, kmp_int32 global_tid, |
| 572 | // kmp_critical_name *crit); |
| 573 | OMPRTL__kmpc_critical, |
| 574 | // Call to void __kmpc_critical_with_hint(ident_t *loc, kmp_int32 |
| 575 | // global_tid, kmp_critical_name *crit, uintptr_t hint); |
| 576 | OMPRTL__kmpc_critical_with_hint, |
| 577 | // Call to void __kmpc_end_critical(ident_t *loc, kmp_int32 global_tid, |
| 578 | // kmp_critical_name *crit); |
| 579 | OMPRTL__kmpc_end_critical, |
| 580 | // Call to kmp_int32 __kmpc_cancel_barrier(ident_t *loc, kmp_int32 |
| 581 | // global_tid); |
| 582 | OMPRTL__kmpc_cancel_barrier, |
| 583 | // Call to void __kmpc_barrier(ident_t *loc, kmp_int32 global_tid); |
| 584 | OMPRTL__kmpc_barrier, |
| 585 | // Call to void __kmpc_for_static_fini(ident_t *loc, kmp_int32 global_tid); |
| 586 | OMPRTL__kmpc_for_static_fini, |
| 587 | // Call to void __kmpc_serialized_parallel(ident_t *loc, kmp_int32 |
| 588 | // global_tid); |
| 589 | OMPRTL__kmpc_serialized_parallel, |
| 590 | // Call to void __kmpc_end_serialized_parallel(ident_t *loc, kmp_int32 |
| 591 | // global_tid); |
| 592 | OMPRTL__kmpc_end_serialized_parallel, |
| 593 | // Call to void __kmpc_push_num_threads(ident_t *loc, kmp_int32 global_tid, |
| 594 | // kmp_int32 num_threads); |
| 595 | OMPRTL__kmpc_push_num_threads, |
| 596 | // Call to void __kmpc_flush(ident_t *loc); |
| 597 | OMPRTL__kmpc_flush, |
| 598 | // Call to kmp_int32 __kmpc_master(ident_t *, kmp_int32 global_tid); |
| 599 | OMPRTL__kmpc_master, |
| 600 | // Call to void __kmpc_end_master(ident_t *, kmp_int32 global_tid); |
| 601 | OMPRTL__kmpc_end_master, |
| 602 | // Call to kmp_int32 __kmpc_omp_taskyield(ident_t *, kmp_int32 global_tid, |
| 603 | // int end_part); |
| 604 | OMPRTL__kmpc_omp_taskyield, |
| 605 | // Call to kmp_int32 __kmpc_single(ident_t *, kmp_int32 global_tid); |
| 606 | OMPRTL__kmpc_single, |
| 607 | // Call to void __kmpc_end_single(ident_t *, kmp_int32 global_tid); |
| 608 | OMPRTL__kmpc_end_single, |
| 609 | // Call to kmp_task_t * __kmpc_omp_task_alloc(ident_t *, kmp_int32 gtid, |
| 610 | // kmp_int32 flags, size_t sizeof_kmp_task_t, size_t sizeof_shareds, |
| 611 | // kmp_routine_entry_t *task_entry); |
| 612 | OMPRTL__kmpc_omp_task_alloc, |
Gheorghe-Teodor Bercea | 545a9fe | 2019-06-14 20:19:54 +0000 | [diff] [blame] | 613 | // Call to kmp_task_t * __kmpc_omp_target_task_alloc(ident_t *, |
| 614 | // kmp_int32 gtid, kmp_int32 flags, size_t sizeof_kmp_task_t, |
| 615 | // size_t sizeof_shareds, kmp_routine_entry_t *task_entry, |
| 616 | // kmp_int64 device_id); |
| 617 | OMPRTL__kmpc_omp_target_task_alloc, |
Alexey Bataev | 50b3c95 | 2016-02-19 10:38:26 +0000 | [diff] [blame] | 618 | // Call to kmp_int32 __kmpc_omp_task(ident_t *, kmp_int32 gtid, kmp_task_t * |
| 619 | // new_task); |
| 620 | OMPRTL__kmpc_omp_task, |
| 621 | // Call to void __kmpc_copyprivate(ident_t *loc, kmp_int32 global_tid, |
| 622 | // size_t cpy_size, void *cpy_data, void(*cpy_func)(void *, void *), |
| 623 | // kmp_int32 didit); |
| 624 | OMPRTL__kmpc_copyprivate, |
| 625 | // Call to kmp_int32 __kmpc_reduce(ident_t *loc, kmp_int32 global_tid, |
| 626 | // kmp_int32 num_vars, size_t reduce_size, void *reduce_data, void |
| 627 | // (*reduce_func)(void *lhs_data, void *rhs_data), kmp_critical_name *lck); |
| 628 | OMPRTL__kmpc_reduce, |
| 629 | // Call to kmp_int32 __kmpc_reduce_nowait(ident_t *loc, kmp_int32 |
| 630 | // global_tid, kmp_int32 num_vars, size_t reduce_size, void *reduce_data, |
| 631 | // void (*reduce_func)(void *lhs_data, void *rhs_data), kmp_critical_name |
| 632 | // *lck); |
| 633 | OMPRTL__kmpc_reduce_nowait, |
| 634 | // Call to void __kmpc_end_reduce(ident_t *loc, kmp_int32 global_tid, |
| 635 | // kmp_critical_name *lck); |
| 636 | OMPRTL__kmpc_end_reduce, |
| 637 | // Call to void __kmpc_end_reduce_nowait(ident_t *loc, kmp_int32 global_tid, |
| 638 | // kmp_critical_name *lck); |
| 639 | OMPRTL__kmpc_end_reduce_nowait, |
| 640 | // Call to void __kmpc_omp_task_begin_if0(ident_t *, kmp_int32 gtid, |
| 641 | // kmp_task_t * new_task); |
| 642 | OMPRTL__kmpc_omp_task_begin_if0, |
| 643 | // Call to void __kmpc_omp_task_complete_if0(ident_t *, kmp_int32 gtid, |
| 644 | // kmp_task_t * new_task); |
| 645 | OMPRTL__kmpc_omp_task_complete_if0, |
| 646 | // Call to void __kmpc_ordered(ident_t *loc, kmp_int32 global_tid); |
| 647 | OMPRTL__kmpc_ordered, |
| 648 | // Call to void __kmpc_end_ordered(ident_t *loc, kmp_int32 global_tid); |
| 649 | OMPRTL__kmpc_end_ordered, |
| 650 | // Call to kmp_int32 __kmpc_omp_taskwait(ident_t *loc, kmp_int32 |
| 651 | // global_tid); |
| 652 | OMPRTL__kmpc_omp_taskwait, |
| 653 | // Call to void __kmpc_taskgroup(ident_t *loc, kmp_int32 global_tid); |
| 654 | OMPRTL__kmpc_taskgroup, |
| 655 | // Call to void __kmpc_end_taskgroup(ident_t *loc, kmp_int32 global_tid); |
| 656 | OMPRTL__kmpc_end_taskgroup, |
| 657 | // Call to void __kmpc_push_proc_bind(ident_t *loc, kmp_int32 global_tid, |
| 658 | // int proc_bind); |
| 659 | OMPRTL__kmpc_push_proc_bind, |
| 660 | // Call to kmp_int32 __kmpc_omp_task_with_deps(ident_t *loc_ref, kmp_int32 |
| 661 | // gtid, kmp_task_t * new_task, kmp_int32 ndeps, kmp_depend_info_t |
| 662 | // *dep_list, kmp_int32 ndeps_noalias, kmp_depend_info_t *noalias_dep_list); |
| 663 | OMPRTL__kmpc_omp_task_with_deps, |
| 664 | // Call to void __kmpc_omp_wait_deps(ident_t *loc_ref, kmp_int32 |
| 665 | // gtid, kmp_int32 ndeps, kmp_depend_info_t *dep_list, kmp_int32 |
| 666 | // ndeps_noalias, kmp_depend_info_t *noalias_dep_list); |
| 667 | OMPRTL__kmpc_omp_wait_deps, |
| 668 | // Call to kmp_int32 __kmpc_cancellationpoint(ident_t *loc, kmp_int32 |
| 669 | // global_tid, kmp_int32 cncl_kind); |
| 670 | OMPRTL__kmpc_cancellationpoint, |
| 671 | // Call to kmp_int32 __kmpc_cancel(ident_t *loc, kmp_int32 global_tid, |
| 672 | // kmp_int32 cncl_kind); |
| 673 | OMPRTL__kmpc_cancel, |
Carlo Bertolli | 430d8ec | 2016-03-03 20:34:23 +0000 | [diff] [blame] | 674 | // Call to void __kmpc_push_num_teams(ident_t *loc, kmp_int32 global_tid, |
| 675 | // kmp_int32 num_teams, kmp_int32 thread_limit); |
| 676 | OMPRTL__kmpc_push_num_teams, |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 677 | // Call to void __kmpc_fork_teams(ident_t *loc, kmp_int32 argc, kmpc_micro |
| 678 | // microtask, ...); |
Carlo Bertolli | 430d8ec | 2016-03-03 20:34:23 +0000 | [diff] [blame] | 679 | OMPRTL__kmpc_fork_teams, |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 680 | // Call to void __kmpc_taskloop(ident_t *loc, int gtid, kmp_task_t *task, int |
| 681 | // if_val, kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st, int nogroup, int |
| 682 | // sched, kmp_uint64 grainsize, void *task_dup); |
| 683 | OMPRTL__kmpc_taskloop, |
Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 684 | // Call to void __kmpc_doacross_init(ident_t *loc, kmp_int32 gtid, kmp_int32 |
| 685 | // num_dims, struct kmp_dim *dims); |
| 686 | OMPRTL__kmpc_doacross_init, |
| 687 | // Call to void __kmpc_doacross_fini(ident_t *loc, kmp_int32 gtid); |
| 688 | OMPRTL__kmpc_doacross_fini, |
| 689 | // Call to void __kmpc_doacross_post(ident_t *loc, kmp_int32 gtid, kmp_int64 |
| 690 | // *vec); |
| 691 | OMPRTL__kmpc_doacross_post, |
| 692 | // Call to void __kmpc_doacross_wait(ident_t *loc, kmp_int32 gtid, kmp_int64 |
| 693 | // *vec); |
| 694 | OMPRTL__kmpc_doacross_wait, |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 695 | // Call to void *__kmpc_task_reduction_init(int gtid, int num_data, void |
| 696 | // *data); |
| 697 | OMPRTL__kmpc_task_reduction_init, |
| 698 | // Call to void *__kmpc_task_reduction_get_th_data(int gtid, void *tg, void |
| 699 | // *d); |
| 700 | OMPRTL__kmpc_task_reduction_get_th_data, |
Alexey Bataev | 6cf7b71 | 2019-04-08 19:06:42 +0000 | [diff] [blame] | 701 | // Call to void *__kmpc_alloc(int gtid, size_t sz, omp_allocator_handle_t al); |
Alexey Bataev | 4f680db | 2019-03-19 16:41:16 +0000 | [diff] [blame] | 702 | OMPRTL__kmpc_alloc, |
Alexey Bataev | 6cf7b71 | 2019-04-08 19:06:42 +0000 | [diff] [blame] | 703 | // Call to void __kmpc_free(int gtid, void *ptr, omp_allocator_handle_t al); |
Alexey Bataev | 4f680db | 2019-03-19 16:41:16 +0000 | [diff] [blame] | 704 | OMPRTL__kmpc_free, |
Alexey Bataev | 50b3c95 | 2016-02-19 10:38:26 +0000 | [diff] [blame] | 705 | |
| 706 | // |
| 707 | // Offloading related calls |
| 708 | // |
Alexey Bataev | 7bb3353 | 2019-01-07 21:30:43 +0000 | [diff] [blame] | 709 | // Call to void __kmpc_push_target_tripcount(int64_t device_id, kmp_uint64 |
| 710 | // size); |
| 711 | OMPRTL__kmpc_push_target_tripcount, |
George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 712 | // Call to int32_t __tgt_target(int64_t device_id, void *host_ptr, int32_t |
| 713 | // 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] | 714 | // *arg_types); |
| 715 | OMPRTL__tgt_target, |
Alexey Bataev | a9f77c6 | 2017-12-13 21:04:20 +0000 | [diff] [blame] | 716 | // Call to int32_t __tgt_target_nowait(int64_t device_id, void *host_ptr, |
| 717 | // int32_t arg_num, void** args_base, void **args, size_t *arg_sizes, int64_t |
| 718 | // *arg_types); |
| 719 | OMPRTL__tgt_target_nowait, |
George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 720 | // Call to int32_t __tgt_target_teams(int64_t device_id, void *host_ptr, |
| 721 | // int32_t arg_num, void** args_base, void **args, size_t *arg_sizes, int64_t |
| 722 | // *arg_types, int32_t num_teams, int32_t thread_limit); |
Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 723 | OMPRTL__tgt_target_teams, |
Alexey Bataev | a9f77c6 | 2017-12-13 21:04:20 +0000 | [diff] [blame] | 724 | // Call to int32_t __tgt_target_teams_nowait(int64_t device_id, void |
| 725 | // *host_ptr, int32_t arg_num, void** args_base, void **args, size_t |
| 726 | // *arg_sizes, int64_t *arg_types, int32_t num_teams, int32_t thread_limit); |
| 727 | OMPRTL__tgt_target_teams_nowait, |
Gheorghe-Teodor Bercea | 66cdbb47 | 2019-05-21 19:42:01 +0000 | [diff] [blame] | 728 | // Call to void __tgt_register_requires(int64_t flags); |
| 729 | OMPRTL__tgt_register_requires, |
Alexey Bataev | 50b3c95 | 2016-02-19 10:38:26 +0000 | [diff] [blame] | 730 | // Call to void __tgt_register_lib(__tgt_bin_desc *desc); |
| 731 | OMPRTL__tgt_register_lib, |
| 732 | // Call to void __tgt_unregister_lib(__tgt_bin_desc *desc); |
| 733 | OMPRTL__tgt_unregister_lib, |
George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 734 | // Call to void __tgt_target_data_begin(int64_t device_id, int32_t arg_num, |
| 735 | // 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] | 736 | OMPRTL__tgt_target_data_begin, |
Alexey Bataev | 0cc6b8e | 2017-12-14 17:00:17 +0000 | [diff] [blame] | 737 | // Call to void __tgt_target_data_begin_nowait(int64_t device_id, int32_t |
| 738 | // arg_num, void** args_base, void **args, size_t *arg_sizes, int64_t |
| 739 | // *arg_types); |
| 740 | OMPRTL__tgt_target_data_begin_nowait, |
George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 741 | // Call to void __tgt_target_data_end(int64_t device_id, int32_t arg_num, |
| 742 | // 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] | 743 | OMPRTL__tgt_target_data_end, |
Alexey Bataev | 0cc6b8e | 2017-12-14 17:00:17 +0000 | [diff] [blame] | 744 | // Call to void __tgt_target_data_end_nowait(int64_t device_id, int32_t |
| 745 | // arg_num, void** args_base, void **args, size_t *arg_sizes, int64_t |
| 746 | // *arg_types); |
| 747 | OMPRTL__tgt_target_data_end_nowait, |
George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 748 | // Call to void __tgt_target_data_update(int64_t device_id, int32_t arg_num, |
| 749 | // 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] | 750 | OMPRTL__tgt_target_data_update, |
Alexey Bataev | 0cc6b8e | 2017-12-14 17:00:17 +0000 | [diff] [blame] | 751 | // Call to void __tgt_target_data_update_nowait(int64_t device_id, int32_t |
| 752 | // arg_num, void** args_base, void **args, size_t *arg_sizes, int64_t |
| 753 | // *arg_types); |
| 754 | OMPRTL__tgt_target_data_update_nowait, |
Alexey Bataev | 50b3c95 | 2016-02-19 10:38:26 +0000 | [diff] [blame] | 755 | }; |
| 756 | |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 757 | /// A basic class for pre|post-action for advanced codegen sequence for OpenMP |
| 758 | /// region. |
| 759 | class CleanupTy final : public EHScopeStack::Cleanup { |
| 760 | PrePostActionTy *Action; |
| 761 | |
| 762 | public: |
| 763 | explicit CleanupTy(PrePostActionTy *Action) : Action(Action) {} |
| 764 | void Emit(CodeGenFunction &CGF, Flags /*flags*/) override { |
| 765 | if (!CGF.HaveInsertPoint()) |
| 766 | return; |
| 767 | Action->Exit(CGF); |
| 768 | } |
| 769 | }; |
| 770 | |
Hans Wennborg | 7eb5464 | 2015-09-10 17:07:54 +0000 | [diff] [blame] | 771 | } // anonymous namespace |
Alexey Bataev | 1809571 | 2014-10-10 12:19:54 +0000 | [diff] [blame] | 772 | |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 773 | void RegionCodeGenTy::operator()(CodeGenFunction &CGF) const { |
| 774 | CodeGenFunction::RunCleanupsScope Scope(CGF); |
| 775 | if (PrePostAction) { |
| 776 | CGF.EHStack.pushCleanup<CleanupTy>(NormalAndEHCleanup, PrePostAction); |
| 777 | Callback(CodeGen, CGF, *PrePostAction); |
| 778 | } else { |
| 779 | PrePostActionTy Action; |
| 780 | Callback(CodeGen, CGF, Action); |
| 781 | } |
| 782 | } |
| 783 | |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 784 | /// Check if the combiner is a call to UDR combiner and if it is so return the |
| 785 | /// UDR decl used for reduction. |
| 786 | static const OMPDeclareReductionDecl * |
| 787 | getReductionInit(const Expr *ReductionOp) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 788 | if (const auto *CE = dyn_cast<CallExpr>(ReductionOp)) |
| 789 | if (const auto *OVE = dyn_cast<OpaqueValueExpr>(CE->getCallee())) |
| 790 | if (const auto *DRE = |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 791 | dyn_cast<DeclRefExpr>(OVE->getSourceExpr()->IgnoreImpCasts())) |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 792 | if (const auto *DRD = dyn_cast<OMPDeclareReductionDecl>(DRE->getDecl())) |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 793 | return DRD; |
| 794 | return nullptr; |
| 795 | } |
| 796 | |
| 797 | static void emitInitWithReductionInitializer(CodeGenFunction &CGF, |
| 798 | const OMPDeclareReductionDecl *DRD, |
| 799 | const Expr *InitOp, |
| 800 | Address Private, Address Original, |
| 801 | QualType Ty) { |
| 802 | if (DRD->getInitializer()) { |
| 803 | std::pair<llvm::Function *, llvm::Function *> Reduction = |
| 804 | CGF.CGM.getOpenMPRuntime().getUserDefinedReduction(DRD); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 805 | const auto *CE = cast<CallExpr>(InitOp); |
| 806 | const auto *OVE = cast<OpaqueValueExpr>(CE->getCallee()); |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 807 | const Expr *LHS = CE->getArg(/*Arg=*/0)->IgnoreParenImpCasts(); |
| 808 | const Expr *RHS = CE->getArg(/*Arg=*/1)->IgnoreParenImpCasts(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 809 | const auto *LHSDRE = |
| 810 | cast<DeclRefExpr>(cast<UnaryOperator>(LHS)->getSubExpr()); |
| 811 | const auto *RHSDRE = |
| 812 | cast<DeclRefExpr>(cast<UnaryOperator>(RHS)->getSubExpr()); |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 813 | CodeGenFunction::OMPPrivateScope PrivateScope(CGF); |
| 814 | PrivateScope.addPrivate(cast<VarDecl>(LHSDRE->getDecl()), |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 815 | [=]() { return Private; }); |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 816 | PrivateScope.addPrivate(cast<VarDecl>(RHSDRE->getDecl()), |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 817 | [=]() { return Original; }); |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 818 | (void)PrivateScope.Privatize(); |
| 819 | RValue Func = RValue::get(Reduction.second); |
| 820 | CodeGenFunction::OpaqueValueMapping Map(CGF, OVE, Func); |
| 821 | CGF.EmitIgnoredExpr(InitOp); |
| 822 | } else { |
| 823 | llvm::Constant *Init = CGF.CGM.EmitNullConstant(Ty); |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 824 | std::string Name = CGF.CGM.getOpenMPRuntime().getName({"init"}); |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 825 | auto *GV = new llvm::GlobalVariable( |
| 826 | CGF.CGM.getModule(), Init->getType(), /*isConstant=*/true, |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 827 | llvm::GlobalValue::PrivateLinkage, Init, Name); |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 828 | LValue LV = CGF.MakeNaturalAlignAddrLValue(GV, Ty); |
| 829 | RValue InitRVal; |
| 830 | switch (CGF.getEvaluationKind(Ty)) { |
| 831 | case TEK_Scalar: |
Alexey Bataev | a9b9cc0 | 2018-01-23 18:12:38 +0000 | [diff] [blame] | 832 | InitRVal = CGF.EmitLoadOfLValue(LV, DRD->getLocation()); |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 833 | break; |
| 834 | case TEK_Complex: |
| 835 | InitRVal = |
Alexey Bataev | a9b9cc0 | 2018-01-23 18:12:38 +0000 | [diff] [blame] | 836 | RValue::getComplex(CGF.EmitLoadOfComplex(LV, DRD->getLocation())); |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 837 | break; |
| 838 | case TEK_Aggregate: |
| 839 | InitRVal = RValue::getAggregate(LV.getAddress()); |
| 840 | break; |
| 841 | } |
Alexey Bataev | a9b9cc0 | 2018-01-23 18:12:38 +0000 | [diff] [blame] | 842 | OpaqueValueExpr OVE(DRD->getLocation(), Ty, VK_RValue); |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 843 | CodeGenFunction::OpaqueValueMapping OpaqueMap(CGF, &OVE, InitRVal); |
| 844 | CGF.EmitAnyExprToMem(&OVE, Private, Ty.getQualifiers(), |
| 845 | /*IsInitializer=*/false); |
| 846 | } |
| 847 | } |
| 848 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 849 | /// Emit initialization of arrays of complex types. |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 850 | /// \param DestAddr Address of the array. |
| 851 | /// \param Type Type of array. |
| 852 | /// \param Init Initial expression of array. |
| 853 | /// \param SrcAddr Address of the original array. |
| 854 | static void EmitOMPAggregateInit(CodeGenFunction &CGF, Address DestAddr, |
Alexey Bataev | a7b1915 | 2017-10-12 20:03:39 +0000 | [diff] [blame] | 855 | QualType Type, bool EmitDeclareReductionInit, |
| 856 | const Expr *Init, |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 857 | const OMPDeclareReductionDecl *DRD, |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 858 | Address SrcAddr = Address::invalid()) { |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 859 | // Perform element-by-element initialization. |
| 860 | QualType ElementTy; |
| 861 | |
| 862 | // Drill down to the base element type on both arrays. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 863 | const ArrayType *ArrayTy = Type->getAsArrayTypeUnsafe(); |
| 864 | llvm::Value *NumElements = CGF.emitArrayLength(ArrayTy, ElementTy, DestAddr); |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 865 | DestAddr = |
| 866 | CGF.Builder.CreateElementBitCast(DestAddr, DestAddr.getElementType()); |
| 867 | if (DRD) |
| 868 | SrcAddr = |
| 869 | CGF.Builder.CreateElementBitCast(SrcAddr, DestAddr.getElementType()); |
| 870 | |
| 871 | llvm::Value *SrcBegin = nullptr; |
| 872 | if (DRD) |
| 873 | SrcBegin = SrcAddr.getPointer(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 874 | llvm::Value *DestBegin = DestAddr.getPointer(); |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 875 | // Cast from pointer to array type to pointer to single element. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 876 | llvm::Value *DestEnd = CGF.Builder.CreateGEP(DestBegin, NumElements); |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 877 | // The basic structure here is a while-do loop. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 878 | llvm::BasicBlock *BodyBB = CGF.createBasicBlock("omp.arrayinit.body"); |
| 879 | llvm::BasicBlock *DoneBB = CGF.createBasicBlock("omp.arrayinit.done"); |
| 880 | llvm::Value *IsEmpty = |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 881 | CGF.Builder.CreateICmpEQ(DestBegin, DestEnd, "omp.arrayinit.isempty"); |
| 882 | CGF.Builder.CreateCondBr(IsEmpty, DoneBB, BodyBB); |
| 883 | |
| 884 | // Enter the loop body, making that address the current address. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 885 | llvm::BasicBlock *EntryBB = CGF.Builder.GetInsertBlock(); |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 886 | CGF.EmitBlock(BodyBB); |
| 887 | |
| 888 | CharUnits ElementSize = CGF.getContext().getTypeSizeInChars(ElementTy); |
| 889 | |
| 890 | llvm::PHINode *SrcElementPHI = nullptr; |
| 891 | Address SrcElementCurrent = Address::invalid(); |
| 892 | if (DRD) { |
| 893 | SrcElementPHI = CGF.Builder.CreatePHI(SrcBegin->getType(), 2, |
| 894 | "omp.arraycpy.srcElementPast"); |
| 895 | SrcElementPHI->addIncoming(SrcBegin, EntryBB); |
| 896 | SrcElementCurrent = |
| 897 | Address(SrcElementPHI, |
| 898 | SrcAddr.getAlignment().alignmentOfArrayElement(ElementSize)); |
| 899 | } |
| 900 | llvm::PHINode *DestElementPHI = CGF.Builder.CreatePHI( |
| 901 | DestBegin->getType(), 2, "omp.arraycpy.destElementPast"); |
| 902 | DestElementPHI->addIncoming(DestBegin, EntryBB); |
| 903 | Address DestElementCurrent = |
| 904 | Address(DestElementPHI, |
| 905 | DestAddr.getAlignment().alignmentOfArrayElement(ElementSize)); |
| 906 | |
| 907 | // Emit copy. |
| 908 | { |
| 909 | CodeGenFunction::RunCleanupsScope InitScope(CGF); |
Alexey Bataev | a7b1915 | 2017-10-12 20:03:39 +0000 | [diff] [blame] | 910 | if (EmitDeclareReductionInit) { |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 911 | emitInitWithReductionInitializer(CGF, DRD, Init, DestElementCurrent, |
| 912 | SrcElementCurrent, ElementTy); |
| 913 | } else |
| 914 | CGF.EmitAnyExprToMem(Init, DestElementCurrent, ElementTy.getQualifiers(), |
| 915 | /*IsInitializer=*/false); |
| 916 | } |
| 917 | |
| 918 | if (DRD) { |
| 919 | // Shift the address forward by one element. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 920 | llvm::Value *SrcElementNext = CGF.Builder.CreateConstGEP1_32( |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 921 | SrcElementPHI, /*Idx0=*/1, "omp.arraycpy.dest.element"); |
| 922 | SrcElementPHI->addIncoming(SrcElementNext, CGF.Builder.GetInsertBlock()); |
| 923 | } |
| 924 | |
| 925 | // Shift the address forward by one element. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 926 | llvm::Value *DestElementNext = CGF.Builder.CreateConstGEP1_32( |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 927 | DestElementPHI, /*Idx0=*/1, "omp.arraycpy.dest.element"); |
| 928 | // Check whether we've reached the end. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 929 | llvm::Value *Done = |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 930 | CGF.Builder.CreateICmpEQ(DestElementNext, DestEnd, "omp.arraycpy.done"); |
| 931 | CGF.Builder.CreateCondBr(Done, DoneBB, BodyBB); |
| 932 | DestElementPHI->addIncoming(DestElementNext, CGF.Builder.GetInsertBlock()); |
| 933 | |
| 934 | // Done. |
| 935 | CGF.EmitBlock(DoneBB, /*IsFinished=*/true); |
| 936 | } |
| 937 | |
| 938 | LValue ReductionCodeGen::emitSharedLValue(CodeGenFunction &CGF, const Expr *E) { |
Alexey Bataev | f47c4b4 | 2017-09-26 13:47:31 +0000 | [diff] [blame] | 939 | return CGF.EmitOMPSharedLValue(E); |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 940 | } |
| 941 | |
| 942 | LValue ReductionCodeGen::emitSharedLValueUB(CodeGenFunction &CGF, |
| 943 | const Expr *E) { |
| 944 | if (const auto *OASE = dyn_cast<OMPArraySectionExpr>(E)) |
| 945 | return CGF.EmitOMPArraySectionExpr(OASE, /*IsLowerBound=*/false); |
| 946 | return LValue(); |
| 947 | } |
| 948 | |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 949 | void ReductionCodeGen::emitAggregateInitialization( |
| 950 | CodeGenFunction &CGF, unsigned N, Address PrivateAddr, LValue SharedLVal, |
| 951 | const OMPDeclareReductionDecl *DRD) { |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 952 | // Emit VarDecl with copy init for arrays. |
| 953 | // Get the address of the original variable captured in current |
| 954 | // captured region. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 955 | const auto *PrivateVD = |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 956 | cast<VarDecl>(cast<DeclRefExpr>(ClausesData[N].Private)->getDecl()); |
Alexey Bataev | a7b1915 | 2017-10-12 20:03:39 +0000 | [diff] [blame] | 957 | bool EmitDeclareReductionInit = |
| 958 | DRD && (DRD->getInitializer() || !PrivateVD->hasInit()); |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 959 | EmitOMPAggregateInit(CGF, PrivateAddr, PrivateVD->getType(), |
Alexey Bataev | a7b1915 | 2017-10-12 20:03:39 +0000 | [diff] [blame] | 960 | EmitDeclareReductionInit, |
| 961 | EmitDeclareReductionInit ? ClausesData[N].ReductionOp |
| 962 | : PrivateVD->getInit(), |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 963 | DRD, SharedLVal.getAddress()); |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 964 | } |
| 965 | |
| 966 | ReductionCodeGen::ReductionCodeGen(ArrayRef<const Expr *> Shareds, |
| 967 | ArrayRef<const Expr *> Privates, |
| 968 | ArrayRef<const Expr *> ReductionOps) { |
| 969 | ClausesData.reserve(Shareds.size()); |
| 970 | SharedAddresses.reserve(Shareds.size()); |
| 971 | Sizes.reserve(Shareds.size()); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 972 | BaseDecls.reserve(Shareds.size()); |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 973 | auto IPriv = Privates.begin(); |
| 974 | auto IRed = ReductionOps.begin(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 975 | for (const Expr *Ref : Shareds) { |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 976 | ClausesData.emplace_back(Ref, *IPriv, *IRed); |
| 977 | std::advance(IPriv, 1); |
| 978 | std::advance(IRed, 1); |
| 979 | } |
| 980 | } |
| 981 | |
| 982 | void ReductionCodeGen::emitSharedLValue(CodeGenFunction &CGF, unsigned N) { |
| 983 | assert(SharedAddresses.size() == N && |
| 984 | "Number of generated lvalues must be exactly N."); |
Jonas Hahnfeld | 4525c82 | 2017-10-23 19:01:35 +0000 | [diff] [blame] | 985 | LValue First = emitSharedLValue(CGF, ClausesData[N].Ref); |
| 986 | LValue Second = emitSharedLValueUB(CGF, ClausesData[N].Ref); |
| 987 | SharedAddresses.emplace_back(First, Second); |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 988 | } |
| 989 | |
| 990 | void ReductionCodeGen::emitAggregateType(CodeGenFunction &CGF, unsigned N) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 991 | const auto *PrivateVD = |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 992 | cast<VarDecl>(cast<DeclRefExpr>(ClausesData[N].Private)->getDecl()); |
| 993 | QualType PrivateType = PrivateVD->getType(); |
| 994 | bool AsArraySection = isa<OMPArraySectionExpr>(ClausesData[N].Ref); |
Jonas Hahnfeld | 4525c82 | 2017-10-23 19:01:35 +0000 | [diff] [blame] | 995 | if (!PrivateType->isVariablyModifiedType()) { |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 996 | Sizes.emplace_back( |
| 997 | CGF.getTypeSize( |
| 998 | SharedAddresses[N].first.getType().getNonReferenceType()), |
| 999 | nullptr); |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1000 | return; |
| 1001 | } |
| 1002 | llvm::Value *Size; |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 1003 | llvm::Value *SizeInChars; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1004 | auto *ElemType = |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 1005 | cast<llvm::PointerType>(SharedAddresses[N].first.getPointer()->getType()) |
| 1006 | ->getElementType(); |
| 1007 | auto *ElemSizeOf = llvm::ConstantExpr::getSizeOf(ElemType); |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1008 | if (AsArraySection) { |
| 1009 | Size = CGF.Builder.CreatePtrDiff(SharedAddresses[N].second.getPointer(), |
| 1010 | SharedAddresses[N].first.getPointer()); |
| 1011 | Size = CGF.Builder.CreateNUWAdd( |
| 1012 | Size, llvm::ConstantInt::get(Size->getType(), /*V=*/1)); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 1013 | SizeInChars = CGF.Builder.CreateNUWMul(Size, ElemSizeOf); |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1014 | } else { |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 1015 | SizeInChars = CGF.getTypeSize( |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1016 | SharedAddresses[N].first.getType().getNonReferenceType()); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 1017 | Size = CGF.Builder.CreateExactUDiv(SizeInChars, ElemSizeOf); |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1018 | } |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 1019 | Sizes.emplace_back(SizeInChars, Size); |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 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::emitAggregateType(CodeGenFunction &CGF, unsigned N, |
| 1029 | llvm::Value *Size) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1030 | const auto *PrivateVD = |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1031 | cast<VarDecl>(cast<DeclRefExpr>(ClausesData[N].Private)->getDecl()); |
| 1032 | QualType PrivateType = PrivateVD->getType(); |
Jonas Hahnfeld | 4525c82 | 2017-10-23 19:01:35 +0000 | [diff] [blame] | 1033 | if (!PrivateType->isVariablyModifiedType()) { |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 1034 | assert(!Size && !Sizes[N].second && |
Jonas Hahnfeld | 4525c82 | 2017-10-23 19:01:35 +0000 | [diff] [blame] | 1035 | "Size should be nullptr for non-variably modified reduction " |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1036 | "items."); |
| 1037 | return; |
| 1038 | } |
| 1039 | CodeGenFunction::OpaqueValueMapping OpaqueMap( |
| 1040 | CGF, |
| 1041 | cast<OpaqueValueExpr>( |
| 1042 | CGF.getContext().getAsVariableArrayType(PrivateType)->getSizeExpr()), |
| 1043 | RValue::get(Size)); |
| 1044 | CGF.EmitVariablyModifiedType(PrivateType); |
| 1045 | } |
| 1046 | |
| 1047 | void ReductionCodeGen::emitInitialization( |
| 1048 | CodeGenFunction &CGF, unsigned N, Address PrivateAddr, LValue SharedLVal, |
| 1049 | llvm::function_ref<bool(CodeGenFunction &)> DefaultInit) { |
| 1050 | assert(SharedAddresses.size() > N && "No variable was generated"); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1051 | const auto *PrivateVD = |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1052 | cast<VarDecl>(cast<DeclRefExpr>(ClausesData[N].Private)->getDecl()); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1053 | const OMPDeclareReductionDecl *DRD = |
| 1054 | getReductionInit(ClausesData[N].ReductionOp); |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1055 | QualType PrivateType = PrivateVD->getType(); |
| 1056 | PrivateAddr = CGF.Builder.CreateElementBitCast( |
| 1057 | PrivateAddr, CGF.ConvertTypeForMem(PrivateType)); |
| 1058 | QualType SharedType = SharedAddresses[N].first.getType(); |
| 1059 | SharedLVal = CGF.MakeAddrLValue( |
| 1060 | CGF.Builder.CreateElementBitCast(SharedLVal.getAddress(), |
| 1061 | CGF.ConvertTypeForMem(SharedType)), |
Ivan A. Kosarev | f5f2046 | 2017-10-12 11:29:46 +0000 | [diff] [blame] | 1062 | SharedType, SharedAddresses[N].first.getBaseInfo(), |
Ivan A. Kosarev | b9c59f3 | 2017-10-31 11:05:34 +0000 | [diff] [blame] | 1063 | CGF.CGM.getTBAAInfoForSubobject(SharedAddresses[N].first, SharedType)); |
Jonas Hahnfeld | 4525c82 | 2017-10-23 19:01:35 +0000 | [diff] [blame] | 1064 | if (CGF.getContext().getAsArrayType(PrivateVD->getType())) { |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 1065 | emitAggregateInitialization(CGF, N, PrivateAddr, SharedLVal, DRD); |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1066 | } else if (DRD && (DRD->getInitializer() || !PrivateVD->hasInit())) { |
| 1067 | emitInitWithReductionInitializer(CGF, DRD, ClausesData[N].ReductionOp, |
| 1068 | PrivateAddr, SharedLVal.getAddress(), |
| 1069 | SharedLVal.getType()); |
| 1070 | } else if (!DefaultInit(CGF) && PrivateVD->hasInit() && |
| 1071 | !CGF.isTrivialInitializer(PrivateVD->getInit())) { |
| 1072 | CGF.EmitAnyExprToMem(PrivateVD->getInit(), PrivateAddr, |
| 1073 | PrivateVD->getType().getQualifiers(), |
| 1074 | /*IsInitializer=*/false); |
| 1075 | } |
| 1076 | } |
| 1077 | |
| 1078 | bool ReductionCodeGen::needCleanups(unsigned N) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1079 | const auto *PrivateVD = |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1080 | cast<VarDecl>(cast<DeclRefExpr>(ClausesData[N].Private)->getDecl()); |
| 1081 | QualType PrivateType = PrivateVD->getType(); |
| 1082 | QualType::DestructionKind DTorKind = PrivateType.isDestructedType(); |
| 1083 | return DTorKind != QualType::DK_none; |
| 1084 | } |
| 1085 | |
| 1086 | void ReductionCodeGen::emitCleanups(CodeGenFunction &CGF, unsigned N, |
| 1087 | Address PrivateAddr) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1088 | const auto *PrivateVD = |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1089 | cast<VarDecl>(cast<DeclRefExpr>(ClausesData[N].Private)->getDecl()); |
| 1090 | QualType PrivateType = PrivateVD->getType(); |
| 1091 | QualType::DestructionKind DTorKind = PrivateType.isDestructedType(); |
| 1092 | if (needCleanups(N)) { |
| 1093 | PrivateAddr = CGF.Builder.CreateElementBitCast( |
| 1094 | PrivateAddr, CGF.ConvertTypeForMem(PrivateType)); |
| 1095 | CGF.pushDestroy(DTorKind, PrivateAddr, PrivateType); |
| 1096 | } |
| 1097 | } |
| 1098 | |
| 1099 | static LValue loadToBegin(CodeGenFunction &CGF, QualType BaseTy, QualType ElTy, |
| 1100 | LValue BaseLV) { |
| 1101 | BaseTy = BaseTy.getNonReferenceType(); |
| 1102 | while ((BaseTy->isPointerType() || BaseTy->isReferenceType()) && |
| 1103 | !CGF.getContext().hasSameType(BaseTy, ElTy)) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1104 | if (const auto *PtrTy = BaseTy->getAs<PointerType>()) { |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1105 | BaseLV = CGF.EmitLoadOfPointerLValue(BaseLV.getAddress(), PtrTy); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1106 | } else { |
Ivan A. Kosarev | 9f9d157 | 2017-10-30 11:49:31 +0000 | [diff] [blame] | 1107 | LValue RefLVal = CGF.MakeAddrLValue(BaseLV.getAddress(), BaseTy); |
| 1108 | BaseLV = CGF.EmitLoadOfReferenceLValue(RefLVal); |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1109 | } |
| 1110 | BaseTy = BaseTy->getPointeeType(); |
| 1111 | } |
| 1112 | return CGF.MakeAddrLValue( |
| 1113 | CGF.Builder.CreateElementBitCast(BaseLV.getAddress(), |
| 1114 | CGF.ConvertTypeForMem(ElTy)), |
Ivan A. Kosarev | f5f2046 | 2017-10-12 11:29:46 +0000 | [diff] [blame] | 1115 | BaseLV.getType(), BaseLV.getBaseInfo(), |
Ivan A. Kosarev | b9c59f3 | 2017-10-31 11:05:34 +0000 | [diff] [blame] | 1116 | CGF.CGM.getTBAAInfoForSubobject(BaseLV, BaseLV.getType())); |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1117 | } |
| 1118 | |
| 1119 | static Address castToBase(CodeGenFunction &CGF, QualType BaseTy, QualType ElTy, |
| 1120 | llvm::Type *BaseLVType, CharUnits BaseLVAlignment, |
| 1121 | llvm::Value *Addr) { |
| 1122 | Address Tmp = Address::invalid(); |
| 1123 | Address TopTmp = Address::invalid(); |
| 1124 | Address MostTopTmp = Address::invalid(); |
| 1125 | BaseTy = BaseTy.getNonReferenceType(); |
| 1126 | while ((BaseTy->isPointerType() || BaseTy->isReferenceType()) && |
| 1127 | !CGF.getContext().hasSameType(BaseTy, ElTy)) { |
| 1128 | Tmp = CGF.CreateMemTemp(BaseTy); |
| 1129 | if (TopTmp.isValid()) |
| 1130 | CGF.Builder.CreateStore(Tmp.getPointer(), TopTmp); |
| 1131 | else |
| 1132 | MostTopTmp = Tmp; |
| 1133 | TopTmp = Tmp; |
| 1134 | BaseTy = BaseTy->getPointeeType(); |
| 1135 | } |
| 1136 | llvm::Type *Ty = BaseLVType; |
| 1137 | if (Tmp.isValid()) |
| 1138 | Ty = Tmp.getElementType(); |
| 1139 | Addr = CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(Addr, Ty); |
| 1140 | if (Tmp.isValid()) { |
| 1141 | CGF.Builder.CreateStore(Addr, Tmp); |
| 1142 | return MostTopTmp; |
| 1143 | } |
| 1144 | return Address(Addr, BaseLVAlignment); |
| 1145 | } |
| 1146 | |
Alexey Bataev | 1c44e15 | 2018-03-06 18:59:43 +0000 | [diff] [blame] | 1147 | static const VarDecl *getBaseDecl(const Expr *Ref, const DeclRefExpr *&DE) { |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1148 | const VarDecl *OrigVD = nullptr; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1149 | if (const auto *OASE = dyn_cast<OMPArraySectionExpr>(Ref)) { |
| 1150 | const Expr *Base = OASE->getBase()->IgnoreParenImpCasts(); |
| 1151 | while (const auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base)) |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1152 | Base = TempOASE->getBase()->IgnoreParenImpCasts(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1153 | while (const auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base)) |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1154 | Base = TempASE->getBase()->IgnoreParenImpCasts(); |
| 1155 | DE = cast<DeclRefExpr>(Base); |
| 1156 | OrigVD = cast<VarDecl>(DE->getDecl()); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1157 | } else if (const auto *ASE = dyn_cast<ArraySubscriptExpr>(Ref)) { |
| 1158 | const Expr *Base = ASE->getBase()->IgnoreParenImpCasts(); |
| 1159 | while (const auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base)) |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1160 | Base = TempASE->getBase()->IgnoreParenImpCasts(); |
| 1161 | DE = cast<DeclRefExpr>(Base); |
| 1162 | OrigVD = cast<VarDecl>(DE->getDecl()); |
| 1163 | } |
Alexey Bataev | 1c44e15 | 2018-03-06 18:59:43 +0000 | [diff] [blame] | 1164 | return OrigVD; |
| 1165 | } |
| 1166 | |
| 1167 | Address ReductionCodeGen::adjustPrivateAddress(CodeGenFunction &CGF, unsigned N, |
| 1168 | Address PrivateAddr) { |
| 1169 | const DeclRefExpr *DE; |
| 1170 | if (const VarDecl *OrigVD = ::getBaseDecl(ClausesData[N].Ref, DE)) { |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1171 | BaseDecls.emplace_back(OrigVD); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1172 | LValue OriginalBaseLValue = CGF.EmitLValue(DE); |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1173 | LValue BaseLValue = |
| 1174 | loadToBegin(CGF, OrigVD->getType(), SharedAddresses[N].first.getType(), |
| 1175 | OriginalBaseLValue); |
| 1176 | llvm::Value *Adjustment = CGF.Builder.CreatePtrDiff( |
| 1177 | BaseLValue.getPointer(), SharedAddresses[N].first.getPointer()); |
Jonas Hahnfeld | 273d261 | 2017-12-06 19:15:28 +0000 | [diff] [blame] | 1178 | llvm::Value *PrivatePointer = |
| 1179 | CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
| 1180 | PrivateAddr.getPointer(), |
| 1181 | SharedAddresses[N].first.getAddress().getType()); |
| 1182 | llvm::Value *Ptr = CGF.Builder.CreateGEP(PrivatePointer, Adjustment); |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1183 | return castToBase(CGF, OrigVD->getType(), |
| 1184 | SharedAddresses[N].first.getType(), |
Jonas Hahnfeld | 273d261 | 2017-12-06 19:15:28 +0000 | [diff] [blame] | 1185 | OriginalBaseLValue.getAddress().getType(), |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1186 | OriginalBaseLValue.getAlignment(), Ptr); |
| 1187 | } |
| 1188 | BaseDecls.emplace_back( |
| 1189 | cast<VarDecl>(cast<DeclRefExpr>(ClausesData[N].Ref)->getDecl())); |
| 1190 | return PrivateAddr; |
| 1191 | } |
| 1192 | |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 1193 | bool ReductionCodeGen::usesReductionInitializer(unsigned N) const { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1194 | const OMPDeclareReductionDecl *DRD = |
| 1195 | getReductionInit(ClausesData[N].ReductionOp); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 1196 | return DRD && DRD->getInitializer(); |
| 1197 | } |
| 1198 | |
Alexey Bataev | 1809571 | 2014-10-10 12:19:54 +0000 | [diff] [blame] | 1199 | LValue CGOpenMPRegionInfo::getThreadIDVariableLValue(CodeGenFunction &CGF) { |
Alexey Bataev | 31300ed | 2016-02-04 11:27:03 +0000 | [diff] [blame] | 1200 | return CGF.EmitLoadOfPointerLValue( |
| 1201 | CGF.GetAddrOfLocalVar(getThreadIDVariable()), |
| 1202 | getThreadIDVariable()->getType()->castAs<PointerType>()); |
Alexey Bataev | 1809571 | 2014-10-10 12:19:54 +0000 | [diff] [blame] | 1203 | } |
| 1204 | |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 1205 | void CGOpenMPRegionInfo::EmitBody(CodeGenFunction &CGF, const Stmt * /*S*/) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 1206 | if (!CGF.HaveInsertPoint()) |
| 1207 | return; |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 1208 | // 1.2.2 OpenMP Language Terminology |
| 1209 | // Structured block - An executable statement with a single entry at the |
| 1210 | // top and a single exit at the bottom. |
| 1211 | // The point of exit cannot be a branch out of the structured block. |
| 1212 | // longjmp() and throw() must not violate the entry/exit criteria. |
| 1213 | CGF.EHStack.pushTerminate(); |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 1214 | CodeGen(CGF); |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 1215 | CGF.EHStack.popTerminate(); |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 1216 | } |
| 1217 | |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 1218 | LValue CGOpenMPTaskOutlinedRegionInfo::getThreadIDVariableLValue( |
| 1219 | CodeGenFunction &CGF) { |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 1220 | return CGF.MakeAddrLValue(CGF.GetAddrOfLocalVar(getThreadIDVariable()), |
| 1221 | getThreadIDVariable()->getType(), |
Ivan A. Kosarev | 5f8c0ca | 2017-10-10 09:39:32 +0000 | [diff] [blame] | 1222 | AlignmentSource::Decl); |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 1223 | } |
| 1224 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1225 | static FieldDecl *addFieldToRecordDecl(ASTContext &C, DeclContext *DC, |
| 1226 | QualType FieldTy) { |
| 1227 | auto *Field = FieldDecl::Create( |
| 1228 | C, DC, SourceLocation(), SourceLocation(), /*Id=*/nullptr, FieldTy, |
| 1229 | C.getTrivialTypeSourceInfo(FieldTy, SourceLocation()), |
| 1230 | /*BW=*/nullptr, /*Mutable=*/false, /*InitStyle=*/ICIS_NoInit); |
| 1231 | Field->setAccess(AS_public); |
| 1232 | DC->addDecl(Field); |
| 1233 | return Field; |
| 1234 | } |
| 1235 | |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 1236 | CGOpenMPRuntime::CGOpenMPRuntime(CodeGenModule &CGM, StringRef FirstSeparator, |
| 1237 | StringRef Separator) |
| 1238 | : CGM(CGM), FirstSeparator(FirstSeparator), Separator(Separator), |
| 1239 | OffloadEntriesInfoManager(CGM) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1240 | ASTContext &C = CGM.getContext(); |
| 1241 | RecordDecl *RD = C.buildImplicitRecord("ident_t"); |
| 1242 | QualType KmpInt32Ty = C.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1); |
| 1243 | RD->startDefinition(); |
| 1244 | // reserved_1 |
| 1245 | addFieldToRecordDecl(C, RD, KmpInt32Ty); |
| 1246 | // flags |
| 1247 | addFieldToRecordDecl(C, RD, KmpInt32Ty); |
| 1248 | // reserved_2 |
| 1249 | addFieldToRecordDecl(C, RD, KmpInt32Ty); |
| 1250 | // reserved_3 |
| 1251 | addFieldToRecordDecl(C, RD, KmpInt32Ty); |
| 1252 | // psource |
| 1253 | addFieldToRecordDecl(C, RD, C.VoidPtrTy); |
| 1254 | RD->completeDefinition(); |
| 1255 | IdentQTy = C.getRecordType(RD); |
| 1256 | IdentTy = CGM.getTypes().ConvertRecordDeclType(RD); |
Alexey Bataev | 3a3bf0b | 2014-09-22 10:01:53 +0000 | [diff] [blame] | 1257 | KmpCriticalNameTy = llvm::ArrayType::get(CGM.Int32Ty, /*NumElements*/ 8); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 1258 | |
| 1259 | loadOffloadInfoMetadata(); |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1260 | } |
| 1261 | |
Alexey Bataev | 9179755 | 2015-03-18 04:13:55 +0000 | [diff] [blame] | 1262 | void CGOpenMPRuntime::clear() { |
| 1263 | InternalVars.clear(); |
Alexey Bataev | 2a6f3f5 | 2018-11-07 19:11:14 +0000 | [diff] [blame] | 1264 | // Clean non-target variable declarations possibly used only in debug info. |
| 1265 | for (const auto &Data : EmittedNonTargetVariables) { |
| 1266 | if (!Data.getValue().pointsToAliveValue()) |
| 1267 | continue; |
| 1268 | auto *GV = dyn_cast<llvm::GlobalVariable>(Data.getValue()); |
| 1269 | if (!GV) |
| 1270 | continue; |
| 1271 | if (!GV->isDeclaration() || GV->getNumUses() > 0) |
| 1272 | continue; |
| 1273 | GV->eraseFromParent(); |
| 1274 | } |
Alexey Bataev | 9179755 | 2015-03-18 04:13:55 +0000 | [diff] [blame] | 1275 | } |
| 1276 | |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 1277 | std::string CGOpenMPRuntime::getName(ArrayRef<StringRef> Parts) const { |
| 1278 | SmallString<128> Buffer; |
| 1279 | llvm::raw_svector_ostream OS(Buffer); |
| 1280 | StringRef Sep = FirstSeparator; |
| 1281 | for (StringRef Part : Parts) { |
| 1282 | OS << Sep << Part; |
| 1283 | Sep = Separator; |
| 1284 | } |
| 1285 | return OS.str(); |
| 1286 | } |
| 1287 | |
Alexey Bataev | c5b1d32 | 2016-03-04 09:22:22 +0000 | [diff] [blame] | 1288 | static llvm::Function * |
| 1289 | emitCombinerOrInitializer(CodeGenModule &CGM, QualType Ty, |
| 1290 | const Expr *CombinerInitializer, const VarDecl *In, |
| 1291 | const VarDecl *Out, bool IsCombiner) { |
| 1292 | // void .omp_combiner.(Ty *in, Ty *out); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1293 | ASTContext &C = CGM.getContext(); |
Alexey Bataev | c5b1d32 | 2016-03-04 09:22:22 +0000 | [diff] [blame] | 1294 | QualType PtrTy = C.getPointerType(Ty).withRestrict(); |
| 1295 | FunctionArgList Args; |
Alexey Bataev | c5b1d32 | 2016-03-04 09:22:22 +0000 | [diff] [blame] | 1296 | ImplicitParamDecl OmpOutParm(C, /*DC=*/nullptr, Out->getLocation(), |
Alexey Bataev | 5622323 | 2017-06-09 13:40:18 +0000 | [diff] [blame] | 1297 | /*Id=*/nullptr, PtrTy, ImplicitParamDecl::Other); |
Alexey Bataev | a839ddd | 2016-03-17 10:19:46 +0000 | [diff] [blame] | 1298 | ImplicitParamDecl OmpInParm(C, /*DC=*/nullptr, In->getLocation(), |
Alexey Bataev | 5622323 | 2017-06-09 13:40:18 +0000 | [diff] [blame] | 1299 | /*Id=*/nullptr, PtrTy, ImplicitParamDecl::Other); |
Alexey Bataev | c5b1d32 | 2016-03-04 09:22:22 +0000 | [diff] [blame] | 1300 | Args.push_back(&OmpOutParm); |
Alexey Bataev | a839ddd | 2016-03-17 10:19:46 +0000 | [diff] [blame] | 1301 | Args.push_back(&OmpInParm); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1302 | const CGFunctionInfo &FnInfo = |
John McCall | c56a8b3 | 2016-03-11 04:30:31 +0000 | [diff] [blame] | 1303 | CGM.getTypes().arrangeBuiltinFunctionDeclaration(C.VoidTy, Args); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1304 | llvm::FunctionType *FnTy = CGM.getTypes().GetFunctionType(FnInfo); |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 1305 | std::string Name = CGM.getOpenMPRuntime().getName( |
| 1306 | {IsCombiner ? "omp_combiner" : "omp_initializer", ""}); |
| 1307 | auto *Fn = llvm::Function::Create(FnTy, llvm::GlobalValue::InternalLinkage, |
| 1308 | Name, &CGM.getModule()); |
Rafael Espindola | 51ec5a9 | 2018-02-28 23:46:35 +0000 | [diff] [blame] | 1309 | CGM.SetInternalFunctionAttributes(GlobalDecl(), Fn, FnInfo); |
Alexey Bataev | 8c5555c | 2019-05-21 15:11:58 +0000 | [diff] [blame] | 1310 | if (CGM.getLangOpts().Optimize) { |
| 1311 | Fn->removeFnAttr(llvm::Attribute::NoInline); |
| 1312 | Fn->removeFnAttr(llvm::Attribute::OptimizeNone); |
| 1313 | Fn->addFnAttr(llvm::Attribute::AlwaysInline); |
| 1314 | } |
Alexey Bataev | c5b1d32 | 2016-03-04 09:22:22 +0000 | [diff] [blame] | 1315 | CodeGenFunction CGF(CGM); |
| 1316 | // Map "T omp_in;" variable to "*omp_in_parm" value in all expressions. |
| 1317 | // 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] | 1318 | CGF.StartFunction(GlobalDecl(), C.VoidTy, Fn, FnInfo, Args, In->getLocation(), |
| 1319 | Out->getLocation()); |
Alexey Bataev | c5b1d32 | 2016-03-04 09:22:22 +0000 | [diff] [blame] | 1320 | CodeGenFunction::OMPPrivateScope Scope(CGF); |
| 1321 | Address AddrIn = CGF.GetAddrOfLocalVar(&OmpInParm); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1322 | Scope.addPrivate(In, [&CGF, AddrIn, PtrTy]() { |
Alexey Bataev | c5b1d32 | 2016-03-04 09:22:22 +0000 | [diff] [blame] | 1323 | return CGF.EmitLoadOfPointerLValue(AddrIn, PtrTy->castAs<PointerType>()) |
| 1324 | .getAddress(); |
| 1325 | }); |
| 1326 | Address AddrOut = CGF.GetAddrOfLocalVar(&OmpOutParm); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1327 | Scope.addPrivate(Out, [&CGF, AddrOut, PtrTy]() { |
Alexey Bataev | c5b1d32 | 2016-03-04 09:22:22 +0000 | [diff] [blame] | 1328 | return CGF.EmitLoadOfPointerLValue(AddrOut, PtrTy->castAs<PointerType>()) |
| 1329 | .getAddress(); |
| 1330 | }); |
| 1331 | (void)Scope.Privatize(); |
Alexey Bataev | 070f43a | 2017-09-06 14:49:58 +0000 | [diff] [blame] | 1332 | if (!IsCombiner && Out->hasInit() && |
| 1333 | !CGF.isTrivialInitializer(Out->getInit())) { |
| 1334 | CGF.EmitAnyExprToMem(Out->getInit(), CGF.GetAddrOfLocalVar(Out), |
| 1335 | Out->getType().getQualifiers(), |
| 1336 | /*IsInitializer=*/true); |
| 1337 | } |
| 1338 | if (CombinerInitializer) |
| 1339 | CGF.EmitIgnoredExpr(CombinerInitializer); |
Alexey Bataev | c5b1d32 | 2016-03-04 09:22:22 +0000 | [diff] [blame] | 1340 | Scope.ForceCleanup(); |
| 1341 | CGF.FinishFunction(); |
| 1342 | return Fn; |
| 1343 | } |
| 1344 | |
| 1345 | void CGOpenMPRuntime::emitUserDefinedReduction( |
| 1346 | CodeGenFunction *CGF, const OMPDeclareReductionDecl *D) { |
| 1347 | if (UDRMap.count(D) > 0) |
| 1348 | return; |
Alexey Bataev | c5b1d32 | 2016-03-04 09:22:22 +0000 | [diff] [blame] | 1349 | llvm::Function *Combiner = emitCombinerOrInitializer( |
Alexey Bataev | e6aa469 | 2018-09-13 16:54:05 +0000 | [diff] [blame] | 1350 | CGM, D->getType(), D->getCombiner(), |
| 1351 | cast<VarDecl>(cast<DeclRefExpr>(D->getCombinerIn())->getDecl()), |
| 1352 | cast<VarDecl>(cast<DeclRefExpr>(D->getCombinerOut())->getDecl()), |
Alexey Bataev | c5b1d32 | 2016-03-04 09:22:22 +0000 | [diff] [blame] | 1353 | /*IsCombiner=*/true); |
| 1354 | llvm::Function *Initializer = nullptr; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1355 | if (const Expr *Init = D->getInitializer()) { |
Alexey Bataev | c5b1d32 | 2016-03-04 09:22:22 +0000 | [diff] [blame] | 1356 | Initializer = emitCombinerOrInitializer( |
Alexey Bataev | 070f43a | 2017-09-06 14:49:58 +0000 | [diff] [blame] | 1357 | CGM, D->getType(), |
| 1358 | D->getInitializerKind() == OMPDeclareReductionDecl::CallInit ? Init |
| 1359 | : nullptr, |
Alexey Bataev | e6aa469 | 2018-09-13 16:54:05 +0000 | [diff] [blame] | 1360 | cast<VarDecl>(cast<DeclRefExpr>(D->getInitOrig())->getDecl()), |
| 1361 | cast<VarDecl>(cast<DeclRefExpr>(D->getInitPriv())->getDecl()), |
Alexey Bataev | c5b1d32 | 2016-03-04 09:22:22 +0000 | [diff] [blame] | 1362 | /*IsCombiner=*/false); |
| 1363 | } |
Alexey Bataev | 43a919f | 2018-04-13 17:48:43 +0000 | [diff] [blame] | 1364 | UDRMap.try_emplace(D, Combiner, Initializer); |
Alexey Bataev | c5b1d32 | 2016-03-04 09:22:22 +0000 | [diff] [blame] | 1365 | if (CGF) { |
| 1366 | auto &Decls = FunctionUDRMap.FindAndConstruct(CGF->CurFn); |
| 1367 | Decls.second.push_back(D); |
| 1368 | } |
| 1369 | } |
| 1370 | |
Alexey Bataev | a839ddd | 2016-03-17 10:19:46 +0000 | [diff] [blame] | 1371 | std::pair<llvm::Function *, llvm::Function *> |
| 1372 | CGOpenMPRuntime::getUserDefinedReduction(const OMPDeclareReductionDecl *D) { |
| 1373 | auto I = UDRMap.find(D); |
| 1374 | if (I != UDRMap.end()) |
| 1375 | return I->second; |
| 1376 | emitUserDefinedReduction(/*CGF=*/nullptr, D); |
| 1377 | return UDRMap.lookup(D); |
| 1378 | } |
| 1379 | |
James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 1380 | static llvm::Function *emitParallelOrTeamsOutlinedFunction( |
Arpith Chacko Jacob | 19b911c | 2017-01-18 18:18:53 +0000 | [diff] [blame] | 1381 | CodeGenModule &CGM, const OMPExecutableDirective &D, const CapturedStmt *CS, |
| 1382 | const VarDecl *ThreadIDVar, OpenMPDirectiveKind InnermostKind, |
| 1383 | const StringRef OutlinedHelperName, const RegionCodeGenTy &CodeGen) { |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 1384 | assert(ThreadIDVar->getType()->isPointerType() && |
| 1385 | "thread id variable must be of type kmp_int32 *"); |
Alexey Bataev | 1809571 | 2014-10-10 12:19:54 +0000 | [diff] [blame] | 1386 | CodeGenFunction CGF(CGM, true); |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 1387 | bool HasCancel = false; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1388 | if (const auto *OPD = dyn_cast<OMPParallelDirective>(&D)) |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 1389 | HasCancel = OPD->hasCancel(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1390 | else if (const auto *OPSD = dyn_cast<OMPParallelSectionsDirective>(&D)) |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 1391 | HasCancel = OPSD->hasCancel(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1392 | else if (const auto *OPFD = dyn_cast<OMPParallelForDirective>(&D)) |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 1393 | HasCancel = OPFD->hasCancel(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1394 | else if (const auto *OPFD = dyn_cast<OMPTargetParallelForDirective>(&D)) |
Alexey Bataev | 2139ed6 | 2017-11-16 18:20:21 +0000 | [diff] [blame] | 1395 | HasCancel = OPFD->hasCancel(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1396 | else if (const auto *OPFD = dyn_cast<OMPDistributeParallelForDirective>(&D)) |
Alexey Bataev | 10a5431 | 2017-11-27 16:54:08 +0000 | [diff] [blame] | 1397 | HasCancel = OPFD->hasCancel(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1398 | else if (const auto *OPFD = |
| 1399 | dyn_cast<OMPTeamsDistributeParallelForDirective>(&D)) |
Alexey Bataev | 10a5431 | 2017-11-27 16:54:08 +0000 | [diff] [blame] | 1400 | HasCancel = OPFD->hasCancel(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1401 | else if (const auto *OPFD = |
Alexey Bataev | 10a5431 | 2017-11-27 16:54:08 +0000 | [diff] [blame] | 1402 | dyn_cast<OMPTargetTeamsDistributeParallelForDirective>(&D)) |
| 1403 | HasCancel = OPFD->hasCancel(); |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 1404 | CGOpenMPOutlinedRegionInfo CGInfo(*CS, ThreadIDVar, CodeGen, InnermostKind, |
Arpith Chacko Jacob | 19b911c | 2017-01-18 18:18:53 +0000 | [diff] [blame] | 1405 | HasCancel, OutlinedHelperName); |
Alexey Bataev | d157d47 | 2015-06-24 03:35:38 +0000 | [diff] [blame] | 1406 | CodeGenFunction::CGCapturedStmtRAII CapInfoRAII(CGF, &CGInfo); |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 1407 | return CGF.GenerateOpenMPCapturedStmtFunction(*CS); |
Alexey Bataev | 1809571 | 2014-10-10 12:19:54 +0000 | [diff] [blame] | 1408 | } |
| 1409 | |
James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 1410 | llvm::Function *CGOpenMPRuntime::emitParallelOutlinedFunction( |
Arpith Chacko Jacob | 19b911c | 2017-01-18 18:18:53 +0000 | [diff] [blame] | 1411 | const OMPExecutableDirective &D, const VarDecl *ThreadIDVar, |
| 1412 | OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen) { |
| 1413 | const CapturedStmt *CS = D.getCapturedStmt(OMPD_parallel); |
| 1414 | return emitParallelOrTeamsOutlinedFunction( |
| 1415 | CGM, D, CS, ThreadIDVar, InnermostKind, getOutlinedHelperName(), CodeGen); |
| 1416 | } |
| 1417 | |
James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 1418 | llvm::Function *CGOpenMPRuntime::emitTeamsOutlinedFunction( |
Arpith Chacko Jacob | 19b911c | 2017-01-18 18:18:53 +0000 | [diff] [blame] | 1419 | const OMPExecutableDirective &D, const VarDecl *ThreadIDVar, |
| 1420 | OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen) { |
| 1421 | const CapturedStmt *CS = D.getCapturedStmt(OMPD_teams); |
| 1422 | return emitParallelOrTeamsOutlinedFunction( |
| 1423 | CGM, D, CS, ThreadIDVar, InnermostKind, getOutlinedHelperName(), CodeGen); |
| 1424 | } |
| 1425 | |
James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 1426 | llvm::Function *CGOpenMPRuntime::emitTaskOutlinedFunction( |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 1427 | const OMPExecutableDirective &D, const VarDecl *ThreadIDVar, |
Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 1428 | const VarDecl *PartIDVar, const VarDecl *TaskTVar, |
| 1429 | OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen, |
| 1430 | bool Tied, unsigned &NumberOfParts) { |
| 1431 | auto &&UntiedCodeGen = [this, &D, TaskTVar](CodeGenFunction &CGF, |
| 1432 | PrePostActionTy &) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1433 | llvm::Value *ThreadID = getThreadID(CGF, D.getBeginLoc()); |
| 1434 | llvm::Value *UpLoc = emitUpdateLocation(CGF, D.getBeginLoc()); |
Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 1435 | llvm::Value *TaskArgs[] = { |
| 1436 | UpLoc, ThreadID, |
| 1437 | CGF.EmitLoadOfPointerLValue(CGF.GetAddrOfLocalVar(TaskTVar), |
| 1438 | TaskTVar->getType()->castAs<PointerType>()) |
| 1439 | .getPointer()}; |
| 1440 | CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_omp_task), TaskArgs); |
| 1441 | }; |
| 1442 | CGOpenMPTaskOutlinedRegionInfo::UntiedTaskActionTy Action(Tied, PartIDVar, |
| 1443 | UntiedCodeGen); |
| 1444 | CodeGen.setAction(Action); |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 1445 | assert(!ThreadIDVar->getType()->isPointerType() && |
| 1446 | "thread id variable must be of type kmp_int32 for tasks"); |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 1447 | const OpenMPDirectiveKind Region = |
| 1448 | isOpenMPTaskLoopDirective(D.getDirectiveKind()) ? OMPD_taskloop |
| 1449 | : OMPD_task; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1450 | const CapturedStmt *CS = D.getCapturedStmt(Region); |
| 1451 | const auto *TD = dyn_cast<OMPTaskDirective>(&D); |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 1452 | CodeGenFunction CGF(CGM, true); |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 1453 | CGOpenMPTaskOutlinedRegionInfo CGInfo(*CS, ThreadIDVar, CodeGen, |
| 1454 | InnermostKind, |
| 1455 | TD ? TD->hasCancel() : false, Action); |
Alexey Bataev | d157d47 | 2015-06-24 03:35:38 +0000 | [diff] [blame] | 1456 | CodeGenFunction::CGCapturedStmtRAII CapInfoRAII(CGF, &CGInfo); |
James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 1457 | llvm::Function *Res = CGF.GenerateCapturedStmtFunction(*CS); |
Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 1458 | if (!Tied) |
| 1459 | NumberOfParts = Action.getNumberOfParts(); |
| 1460 | return Res; |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 1461 | } |
| 1462 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1463 | static void buildStructValue(ConstantStructBuilder &Fields, CodeGenModule &CGM, |
| 1464 | const RecordDecl *RD, const CGRecordLayout &RL, |
| 1465 | ArrayRef<llvm::Constant *> Data) { |
| 1466 | llvm::StructType *StructTy = RL.getLLVMType(); |
| 1467 | unsigned PrevIdx = 0; |
| 1468 | ConstantInitBuilder CIBuilder(CGM); |
| 1469 | auto DI = Data.begin(); |
| 1470 | for (const FieldDecl *FD : RD->fields()) { |
| 1471 | unsigned Idx = RL.getLLVMFieldNo(FD); |
| 1472 | // Fill the alignment. |
| 1473 | for (unsigned I = PrevIdx; I < Idx; ++I) |
| 1474 | Fields.add(llvm::Constant::getNullValue(StructTy->getElementType(I))); |
| 1475 | PrevIdx = Idx + 1; |
| 1476 | Fields.add(*DI); |
| 1477 | ++DI; |
| 1478 | } |
| 1479 | } |
| 1480 | |
| 1481 | template <class... As> |
| 1482 | static llvm::GlobalVariable * |
Mike Rice | e1ca7b6 | 2018-08-29 15:45:11 +0000 | [diff] [blame] | 1483 | createGlobalStruct(CodeGenModule &CGM, QualType Ty, bool IsConstant, |
| 1484 | ArrayRef<llvm::Constant *> Data, const Twine &Name, |
| 1485 | As &&... Args) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1486 | const auto *RD = cast<RecordDecl>(Ty->getAsTagDecl()); |
| 1487 | const CGRecordLayout &RL = CGM.getTypes().getCGRecordLayout(RD); |
| 1488 | ConstantInitBuilder CIBuilder(CGM); |
| 1489 | ConstantStructBuilder Fields = CIBuilder.beginStruct(RL.getLLVMType()); |
| 1490 | buildStructValue(Fields, CGM, RD, RL, Data); |
| 1491 | return Fields.finishAndCreateGlobal( |
Mike Rice | e1ca7b6 | 2018-08-29 15:45:11 +0000 | [diff] [blame] | 1492 | Name, CGM.getContext().getAlignOfGlobalVarInChars(Ty), IsConstant, |
| 1493 | std::forward<As>(Args)...); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1494 | } |
| 1495 | |
| 1496 | template <typename T> |
Benjamin Kramer | 651d0bf | 2018-05-15 21:26:47 +0000 | [diff] [blame] | 1497 | static void |
| 1498 | createConstantGlobalStructAndAddToParent(CodeGenModule &CGM, QualType Ty, |
| 1499 | ArrayRef<llvm::Constant *> Data, |
| 1500 | T &Parent) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1501 | const auto *RD = cast<RecordDecl>(Ty->getAsTagDecl()); |
| 1502 | const CGRecordLayout &RL = CGM.getTypes().getCGRecordLayout(RD); |
| 1503 | ConstantStructBuilder Fields = Parent.beginStruct(RL.getLLVMType()); |
| 1504 | buildStructValue(Fields, CGM, RD, RL, Data); |
| 1505 | Fields.finishAndAddTo(Parent); |
| 1506 | } |
| 1507 | |
Alexey Bataev | 50b3c95 | 2016-02-19 10:38:26 +0000 | [diff] [blame] | 1508 | Address CGOpenMPRuntime::getOrCreateDefaultLocation(unsigned Flags) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1509 | CharUnits Align = CGM.getContext().getTypeAlignInChars(IdentQTy); |
Alexey Bataev | ceeaa48 | 2018-11-21 21:04:34 +0000 | [diff] [blame] | 1510 | unsigned Reserved2Flags = getDefaultLocationReserved2Flags(); |
| 1511 | FlagsTy FlagsKey(Flags, Reserved2Flags); |
| 1512 | llvm::Value *Entry = OpenMPDefaultLocMap.lookup(FlagsKey); |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1513 | if (!Entry) { |
| 1514 | if (!DefaultOpenMPPSource) { |
| 1515 | // Initialize default location for psource field of ident_t structure of |
| 1516 | // all ident_t objects. Format is ";file;function;line;column;;". |
| 1517 | // Taken from |
James Y Knight | 5d71fc5 | 2019-01-29 16:37:27 +0000 | [diff] [blame] | 1518 | // https://github.com/llvm/llvm-project/blob/master/openmp/runtime/src/kmp_str.cpp |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1519 | DefaultOpenMPPSource = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1520 | CGM.GetAddrOfConstantCString(";unknown;unknown;0;0;;").getPointer(); |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1521 | DefaultOpenMPPSource = |
| 1522 | llvm::ConstantExpr::getBitCast(DefaultOpenMPPSource, CGM.Int8PtrTy); |
| 1523 | } |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1524 | |
Alexey Bataev | ceeaa48 | 2018-11-21 21:04:34 +0000 | [diff] [blame] | 1525 | llvm::Constant *Data[] = { |
| 1526 | llvm::ConstantInt::getNullValue(CGM.Int32Ty), |
| 1527 | llvm::ConstantInt::get(CGM.Int32Ty, Flags), |
| 1528 | llvm::ConstantInt::get(CGM.Int32Ty, Reserved2Flags), |
| 1529 | llvm::ConstantInt::getNullValue(CGM.Int32Ty), DefaultOpenMPPSource}; |
Mike Rice | e1ca7b6 | 2018-08-29 15:45:11 +0000 | [diff] [blame] | 1530 | llvm::GlobalValue *DefaultOpenMPLocation = |
Alexey Bataev | ceeaa48 | 2018-11-21 21:04:34 +0000 | [diff] [blame] | 1531 | createGlobalStruct(CGM, IdentQTy, isDefaultLocationConstant(), Data, "", |
Mike Rice | e1ca7b6 | 2018-08-29 15:45:11 +0000 | [diff] [blame] | 1532 | llvm::GlobalValue::PrivateLinkage); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1533 | DefaultOpenMPLocation->setUnnamedAddr( |
| 1534 | llvm::GlobalValue::UnnamedAddr::Global); |
John McCall | 6c9f1fdb | 2016-11-19 08:17:24 +0000 | [diff] [blame] | 1535 | |
Alexey Bataev | ceeaa48 | 2018-11-21 21:04:34 +0000 | [diff] [blame] | 1536 | OpenMPDefaultLocMap[FlagsKey] = Entry = DefaultOpenMPLocation; |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1537 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1538 | return Address(Entry, Align); |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1539 | } |
| 1540 | |
Alexey Bataev | fd006c4 | 2018-10-05 15:08:53 +0000 | [diff] [blame] | 1541 | void CGOpenMPRuntime::setLocThreadIdInsertPt(CodeGenFunction &CGF, |
| 1542 | bool AtCurrentPoint) { |
| 1543 | auto &Elem = OpenMPLocThreadIDMap.FindAndConstruct(CGF.CurFn); |
| 1544 | assert(!Elem.second.ServiceInsertPt && "Insert point is set already."); |
| 1545 | |
| 1546 | llvm::Value *Undef = llvm::UndefValue::get(CGF.Int32Ty); |
| 1547 | if (AtCurrentPoint) { |
| 1548 | Elem.second.ServiceInsertPt = new llvm::BitCastInst( |
| 1549 | Undef, CGF.Int32Ty, "svcpt", CGF.Builder.GetInsertBlock()); |
| 1550 | } else { |
| 1551 | Elem.second.ServiceInsertPt = |
| 1552 | new llvm::BitCastInst(Undef, CGF.Int32Ty, "svcpt"); |
| 1553 | Elem.second.ServiceInsertPt->insertAfter(CGF.AllocaInsertPt); |
| 1554 | } |
| 1555 | } |
| 1556 | |
| 1557 | void CGOpenMPRuntime::clearLocThreadIdInsertPt(CodeGenFunction &CGF) { |
| 1558 | auto &Elem = OpenMPLocThreadIDMap.FindAndConstruct(CGF.CurFn); |
| 1559 | if (Elem.second.ServiceInsertPt) { |
| 1560 | llvm::Instruction *Ptr = Elem.second.ServiceInsertPt; |
| 1561 | Elem.second.ServiceInsertPt = nullptr; |
| 1562 | Ptr->eraseFromParent(); |
| 1563 | } |
| 1564 | } |
| 1565 | |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 1566 | llvm::Value *CGOpenMPRuntime::emitUpdateLocation(CodeGenFunction &CGF, |
| 1567 | SourceLocation Loc, |
Alexey Bataev | 50b3c95 | 2016-02-19 10:38:26 +0000 | [diff] [blame] | 1568 | unsigned Flags) { |
| 1569 | Flags |= OMP_IDENT_KMPC; |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1570 | // If no debug info is generated - return global default location. |
Benjamin Kramer | 8c30592 | 2016-02-02 11:06:51 +0000 | [diff] [blame] | 1571 | if (CGM.getCodeGenOpts().getDebugInfo() == codegenoptions::NoDebugInfo || |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1572 | Loc.isInvalid()) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1573 | return getOrCreateDefaultLocation(Flags).getPointer(); |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1574 | |
| 1575 | assert(CGF.CurFn && "No function in current CodeGenFunction."); |
| 1576 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1577 | CharUnits Align = CGM.getContext().getTypeAlignInChars(IdentQTy); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1578 | Address LocValue = Address::invalid(); |
Alexey Bataev | 1e4b713 | 2014-12-03 12:11:24 +0000 | [diff] [blame] | 1579 | auto I = OpenMPLocThreadIDMap.find(CGF.CurFn); |
| 1580 | if (I != OpenMPLocThreadIDMap.end()) |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1581 | LocValue = Address(I->second.DebugLoc, Align); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1582 | |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 1583 | // OpenMPLocThreadIDMap may have null DebugLoc and non-null ThreadID, if |
| 1584 | // GetOpenMPThreadID was called before this routine. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1585 | if (!LocValue.isValid()) { |
Alexey Bataev | 15007ba | 2014-05-07 06:18:01 +0000 | [diff] [blame] | 1586 | // Generate "ident_t .kmpc_loc.addr;" |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1587 | Address AI = CGF.CreateMemTemp(IdentQTy, ".kmpc_loc.addr"); |
Alexey Bataev | 1809571 | 2014-10-10 12:19:54 +0000 | [diff] [blame] | 1588 | auto &Elem = OpenMPLocThreadIDMap.FindAndConstruct(CGF.CurFn); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1589 | Elem.second.DebugLoc = AI.getPointer(); |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1590 | LocValue = AI; |
| 1591 | |
Alexey Bataev | fd006c4 | 2018-10-05 15:08:53 +0000 | [diff] [blame] | 1592 | if (!Elem.second.ServiceInsertPt) |
| 1593 | setLocThreadIdInsertPt(CGF); |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1594 | CGBuilderTy::InsertPointGuard IPG(CGF.Builder); |
Alexey Bataev | fd006c4 | 2018-10-05 15:08:53 +0000 | [diff] [blame] | 1595 | CGF.Builder.SetInsertPoint(Elem.second.ServiceInsertPt); |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 1596 | CGF.Builder.CreateMemCpy(LocValue, getOrCreateDefaultLocation(Flags), |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1597 | CGF.getTypeSize(IdentQTy)); |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1598 | } |
| 1599 | |
| 1600 | // char **psource = &.kmpc_loc_<flags>.addr.psource; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1601 | LValue Base = CGF.MakeAddrLValue(LocValue, IdentQTy); |
| 1602 | auto Fields = cast<RecordDecl>(IdentQTy->getAsTagDecl())->field_begin(); |
| 1603 | LValue PSource = |
| 1604 | CGF.EmitLValueForField(Base, *std::next(Fields, IdentField_PSource)); |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1605 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1606 | llvm::Value *OMPDebugLoc = OpenMPDebugLocMap.lookup(Loc.getRawEncoding()); |
Alexey Bataev | f002aca | 2014-05-30 05:48:40 +0000 | [diff] [blame] | 1607 | if (OMPDebugLoc == nullptr) { |
| 1608 | SmallString<128> Buffer2; |
| 1609 | llvm::raw_svector_ostream OS2(Buffer2); |
| 1610 | // Build debug location |
| 1611 | PresumedLoc PLoc = CGF.getContext().getSourceManager().getPresumedLoc(Loc); |
| 1612 | OS2 << ";" << PLoc.getFilename() << ";"; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1613 | if (const auto *FD = dyn_cast_or_null<FunctionDecl>(CGF.CurFuncDecl)) |
Alexey Bataev | f002aca | 2014-05-30 05:48:40 +0000 | [diff] [blame] | 1614 | OS2 << FD->getQualifiedNameAsString(); |
Alexey Bataev | f002aca | 2014-05-30 05:48:40 +0000 | [diff] [blame] | 1615 | OS2 << ";" << PLoc.getLine() << ";" << PLoc.getColumn() << ";;"; |
| 1616 | OMPDebugLoc = CGF.Builder.CreateGlobalStringPtr(OS2.str()); |
| 1617 | OpenMPDebugLocMap[Loc.getRawEncoding()] = OMPDebugLoc; |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1618 | } |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1619 | // *psource = ";<File>;<Function>;<Line>;<Column>;;"; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1620 | CGF.EmitStoreOfScalar(OMPDebugLoc, PSource); |
Alexey Bataev | f002aca | 2014-05-30 05:48:40 +0000 | [diff] [blame] | 1621 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1622 | // Our callers always pass this to a runtime function, so for |
| 1623 | // convenience, go ahead and return a naked pointer. |
| 1624 | return LocValue.getPointer(); |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1625 | } |
| 1626 | |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 1627 | llvm::Value *CGOpenMPRuntime::getThreadID(CodeGenFunction &CGF, |
| 1628 | SourceLocation Loc) { |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1629 | assert(CGF.CurFn && "No function in current CodeGenFunction."); |
| 1630 | |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 1631 | llvm::Value *ThreadID = nullptr; |
Alexey Bataev | 1809571 | 2014-10-10 12:19:54 +0000 | [diff] [blame] | 1632 | // Check whether we've already cached a load of the thread id in this |
| 1633 | // function. |
Alexey Bataev | 1e4b713 | 2014-12-03 12:11:24 +0000 | [diff] [blame] | 1634 | auto I = OpenMPLocThreadIDMap.find(CGF.CurFn); |
Alexey Bataev | 1809571 | 2014-10-10 12:19:54 +0000 | [diff] [blame] | 1635 | if (I != OpenMPLocThreadIDMap.end()) { |
| 1636 | ThreadID = I->second.ThreadID; |
Alexey Bataev | 03b340a | 2014-10-21 03:16:40 +0000 | [diff] [blame] | 1637 | if (ThreadID != nullptr) |
| 1638 | return ThreadID; |
| 1639 | } |
Alexey Bataev | aee1855 | 2017-08-16 14:01:00 +0000 | [diff] [blame] | 1640 | // If exceptions are enabled, do not use parameter to avoid possible crash. |
Alexey Bataev | 5d2c9a4 | 2017-11-02 18:55:05 +0000 | [diff] [blame] | 1641 | if (!CGF.EHStack.requiresLandingPad() || !CGF.getLangOpts().Exceptions || |
| 1642 | !CGF.getLangOpts().CXXExceptions || |
Alexey Bataev | 0e1b458 | 2017-11-02 14:25:34 +0000 | [diff] [blame] | 1643 | CGF.Builder.GetInsertBlock() == CGF.AllocaInsertPt->getParent()) { |
Alexey Bataev | aee1855 | 2017-08-16 14:01:00 +0000 | [diff] [blame] | 1644 | if (auto *OMPRegionInfo = |
| 1645 | dyn_cast_or_null<CGOpenMPRegionInfo>(CGF.CapturedStmtInfo)) { |
| 1646 | if (OMPRegionInfo->getThreadIDVariable()) { |
| 1647 | // Check if this an outlined function with thread id passed as argument. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1648 | LValue LVal = OMPRegionInfo->getThreadIDVariableLValue(CGF); |
Alexey Bataev | 1e49137 | 2018-01-23 18:44:14 +0000 | [diff] [blame] | 1649 | ThreadID = CGF.EmitLoadOfScalar(LVal, Loc); |
Alexey Bataev | aee1855 | 2017-08-16 14:01:00 +0000 | [diff] [blame] | 1650 | // If value loaded in entry block, cache it and use it everywhere in |
| 1651 | // function. |
| 1652 | if (CGF.Builder.GetInsertBlock() == CGF.AllocaInsertPt->getParent()) { |
| 1653 | auto &Elem = OpenMPLocThreadIDMap.FindAndConstruct(CGF.CurFn); |
| 1654 | Elem.second.ThreadID = ThreadID; |
| 1655 | } |
| 1656 | return ThreadID; |
Alexey Bataev | 8cbe0a6 | 2015-02-26 10:27:34 +0000 | [diff] [blame] | 1657 | } |
Alexey Bataev | d6c5755 | 2014-07-25 07:55:17 +0000 | [diff] [blame] | 1658 | } |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1659 | } |
Alexey Bataev | 8cbe0a6 | 2015-02-26 10:27:34 +0000 | [diff] [blame] | 1660 | |
| 1661 | // This is not an outlined function region - need to call __kmpc_int32 |
| 1662 | // kmpc_global_thread_num(ident_t *loc). |
| 1663 | // Generate thread id value and cache this value for use across the |
| 1664 | // function. |
Alexey Bataev | fd006c4 | 2018-10-05 15:08:53 +0000 | [diff] [blame] | 1665 | auto &Elem = OpenMPLocThreadIDMap.FindAndConstruct(CGF.CurFn); |
| 1666 | if (!Elem.second.ServiceInsertPt) |
| 1667 | setLocThreadIdInsertPt(CGF); |
Alexey Bataev | 8cbe0a6 | 2015-02-26 10:27:34 +0000 | [diff] [blame] | 1668 | CGBuilderTy::InsertPointGuard IPG(CGF.Builder); |
Alexey Bataev | fd006c4 | 2018-10-05 15:08:53 +0000 | [diff] [blame] | 1669 | CGF.Builder.SetInsertPoint(Elem.second.ServiceInsertPt); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1670 | llvm::CallInst *Call = CGF.Builder.CreateCall( |
Alexey Bataev | 0e1b458 | 2017-11-02 14:25:34 +0000 | [diff] [blame] | 1671 | createRuntimeFunction(OMPRTL__kmpc_global_thread_num), |
| 1672 | emitUpdateLocation(CGF, Loc)); |
| 1673 | Call->setCallingConv(CGF.getRuntimeCC()); |
Alexey Bataev | 0e1b458 | 2017-11-02 14:25:34 +0000 | [diff] [blame] | 1674 | Elem.second.ThreadID = Call; |
| 1675 | return Call; |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1676 | } |
| 1677 | |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 1678 | void CGOpenMPRuntime::functionFinished(CodeGenFunction &CGF) { |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1679 | assert(CGF.CurFn && "No function in current CodeGenFunction."); |
Alexey Bataev | fd006c4 | 2018-10-05 15:08:53 +0000 | [diff] [blame] | 1680 | if (OpenMPLocThreadIDMap.count(CGF.CurFn)) { |
| 1681 | clearLocThreadIdInsertPt(CGF); |
Alexey Bataev | 03b340a | 2014-10-21 03:16:40 +0000 | [diff] [blame] | 1682 | OpenMPLocThreadIDMap.erase(CGF.CurFn); |
Alexey Bataev | fd006c4 | 2018-10-05 15:08:53 +0000 | [diff] [blame] | 1683 | } |
Alexey Bataev | c5b1d32 | 2016-03-04 09:22:22 +0000 | [diff] [blame] | 1684 | if (FunctionUDRMap.count(CGF.CurFn) > 0) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1685 | for(auto *D : FunctionUDRMap[CGF.CurFn]) |
Alexey Bataev | c5b1d32 | 2016-03-04 09:22:22 +0000 | [diff] [blame] | 1686 | UDRMap.erase(D); |
Alexey Bataev | c5b1d32 | 2016-03-04 09:22:22 +0000 | [diff] [blame] | 1687 | FunctionUDRMap.erase(CGF.CurFn); |
| 1688 | } |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1689 | } |
| 1690 | |
| 1691 | llvm::Type *CGOpenMPRuntime::getIdentTyPointerTy() { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1692 | return IdentTy->getPointerTo(); |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1693 | } |
| 1694 | |
| 1695 | llvm::Type *CGOpenMPRuntime::getKmpc_MicroPointerTy() { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 1696 | if (!Kmpc_MicroTy) { |
| 1697 | // Build void (*kmpc_micro)(kmp_int32 *global_tid, kmp_int32 *bound_tid,...) |
| 1698 | llvm::Type *MicroParams[] = {llvm::PointerType::getUnqual(CGM.Int32Ty), |
| 1699 | llvm::PointerType::getUnqual(CGM.Int32Ty)}; |
| 1700 | Kmpc_MicroTy = llvm::FunctionType::get(CGM.VoidTy, MicroParams, true); |
| 1701 | } |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1702 | return llvm::PointerType::getUnqual(Kmpc_MicroTy); |
| 1703 | } |
| 1704 | |
James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 1705 | llvm::FunctionCallee CGOpenMPRuntime::createRuntimeFunction(unsigned Function) { |
| 1706 | llvm::FunctionCallee RTLFn = nullptr; |
Alexey Bataev | 50b3c95 | 2016-02-19 10:38:26 +0000 | [diff] [blame] | 1707 | switch (static_cast<OpenMPRTLFunction>(Function)) { |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1708 | case OMPRTL__kmpc_fork_call: { |
| 1709 | // Build void __kmpc_fork_call(ident_t *loc, kmp_int32 argc, kmpc_micro |
| 1710 | // microtask, ...); |
Alexey Bataev | 23b6942 | 2014-06-18 07:08:49 +0000 | [diff] [blame] | 1711 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty, |
| 1712 | getKmpc_MicroPointerTy()}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1713 | auto *FnTy = |
Alexey Bataev | d74d060 | 2014-10-13 06:02:40 +0000 | [diff] [blame] | 1714 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ true); |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1715 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_fork_call"); |
James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 1716 | if (auto *F = dyn_cast<llvm::Function>(RTLFn.getCallee())) { |
Johannes Doerfert | ac991bb | 2019-01-19 05:36:54 +0000 | [diff] [blame] | 1717 | if (!F->hasMetadata(llvm::LLVMContext::MD_callback)) { |
| 1718 | llvm::LLVMContext &Ctx = F->getContext(); |
| 1719 | llvm::MDBuilder MDB(Ctx); |
| 1720 | // Annotate the callback behavior of the __kmpc_fork_call: |
| 1721 | // - The callback callee is argument number 2 (microtask). |
| 1722 | // - The first two arguments of the callback callee are unknown (-1). |
| 1723 | // - All variadic arguments to the __kmpc_fork_call are passed to the |
| 1724 | // callback callee. |
| 1725 | F->addMetadata( |
| 1726 | llvm::LLVMContext::MD_callback, |
| 1727 | *llvm::MDNode::get(Ctx, {MDB.createCallbackEncoding( |
| 1728 | 2, {-1, -1}, |
| 1729 | /* VarArgsArePassed */ true)})); |
| 1730 | } |
| 1731 | } |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1732 | break; |
| 1733 | } |
| 1734 | case OMPRTL__kmpc_global_thread_num: { |
| 1735 | // Build kmp_int32 __kmpc_global_thread_num(ident_t *loc); |
Alexey Bataev | 23b6942 | 2014-06-18 07:08:49 +0000 | [diff] [blame] | 1736 | llvm::Type *TypeParams[] = {getIdentTyPointerTy()}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1737 | auto *FnTy = |
Alexey Bataev | d74d060 | 2014-10-13 06:02:40 +0000 | [diff] [blame] | 1738 | llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg*/ false); |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1739 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_global_thread_num"); |
| 1740 | break; |
| 1741 | } |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 1742 | case OMPRTL__kmpc_threadprivate_cached: { |
| 1743 | // Build void *__kmpc_threadprivate_cached(ident_t *loc, |
| 1744 | // kmp_int32 global_tid, void *data, size_t size, void ***cache); |
| 1745 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty, |
| 1746 | CGM.VoidPtrTy, CGM.SizeTy, |
| 1747 | CGM.VoidPtrTy->getPointerTo()->getPointerTo()}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1748 | auto *FnTy = |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 1749 | llvm::FunctionType::get(CGM.VoidPtrTy, TypeParams, /*isVarArg*/ false); |
| 1750 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_threadprivate_cached"); |
| 1751 | break; |
| 1752 | } |
Alexey Bataev | 3a3bf0b | 2014-09-22 10:01:53 +0000 | [diff] [blame] | 1753 | case OMPRTL__kmpc_critical: { |
Alexey Bataev | f947218 | 2014-09-22 12:32:31 +0000 | [diff] [blame] | 1754 | // Build void __kmpc_critical(ident_t *loc, kmp_int32 global_tid, |
| 1755 | // kmp_critical_name *crit); |
Alexey Bataev | 3a3bf0b | 2014-09-22 10:01:53 +0000 | [diff] [blame] | 1756 | llvm::Type *TypeParams[] = { |
| 1757 | getIdentTyPointerTy(), CGM.Int32Ty, |
| 1758 | llvm::PointerType::getUnqual(KmpCriticalNameTy)}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1759 | auto *FnTy = |
Alexey Bataev | 3a3bf0b | 2014-09-22 10:01:53 +0000 | [diff] [blame] | 1760 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false); |
| 1761 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_critical"); |
| 1762 | break; |
| 1763 | } |
Alexey Bataev | fc57d16 | 2015-12-15 10:55:09 +0000 | [diff] [blame] | 1764 | case OMPRTL__kmpc_critical_with_hint: { |
| 1765 | // Build void __kmpc_critical_with_hint(ident_t *loc, kmp_int32 global_tid, |
| 1766 | // kmp_critical_name *crit, uintptr_t hint); |
| 1767 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty, |
| 1768 | llvm::PointerType::getUnqual(KmpCriticalNameTy), |
| 1769 | CGM.IntPtrTy}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1770 | auto *FnTy = |
Alexey Bataev | fc57d16 | 2015-12-15 10:55:09 +0000 | [diff] [blame] | 1771 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false); |
| 1772 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_critical_with_hint"); |
| 1773 | break; |
| 1774 | } |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 1775 | case OMPRTL__kmpc_threadprivate_register: { |
| 1776 | // Build void __kmpc_threadprivate_register(ident_t *, void *data, |
| 1777 | // kmpc_ctor ctor, kmpc_cctor cctor, kmpc_dtor dtor); |
| 1778 | // typedef void *(*kmpc_ctor)(void *); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1779 | auto *KmpcCtorTy = |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 1780 | llvm::FunctionType::get(CGM.VoidPtrTy, CGM.VoidPtrTy, |
| 1781 | /*isVarArg*/ false)->getPointerTo(); |
| 1782 | // typedef void *(*kmpc_cctor)(void *, void *); |
| 1783 | llvm::Type *KmpcCopyCtorTyArgs[] = {CGM.VoidPtrTy, CGM.VoidPtrTy}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1784 | auto *KmpcCopyCtorTy = |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 1785 | llvm::FunctionType::get(CGM.VoidPtrTy, KmpcCopyCtorTyArgs, |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1786 | /*isVarArg*/ false) |
| 1787 | ->getPointerTo(); |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 1788 | // typedef void (*kmpc_dtor)(void *); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1789 | auto *KmpcDtorTy = |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 1790 | llvm::FunctionType::get(CGM.VoidTy, CGM.VoidPtrTy, /*isVarArg*/ false) |
| 1791 | ->getPointerTo(); |
| 1792 | llvm::Type *FnTyArgs[] = {getIdentTyPointerTy(), CGM.VoidPtrTy, KmpcCtorTy, |
| 1793 | KmpcCopyCtorTy, KmpcDtorTy}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1794 | auto *FnTy = llvm::FunctionType::get(CGM.VoidTy, FnTyArgs, |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 1795 | /*isVarArg*/ false); |
| 1796 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_threadprivate_register"); |
| 1797 | break; |
| 1798 | } |
Alexey Bataev | 3a3bf0b | 2014-09-22 10:01:53 +0000 | [diff] [blame] | 1799 | case OMPRTL__kmpc_end_critical: { |
Alexey Bataev | f947218 | 2014-09-22 12:32:31 +0000 | [diff] [blame] | 1800 | // Build void __kmpc_end_critical(ident_t *loc, kmp_int32 global_tid, |
| 1801 | // kmp_critical_name *crit); |
Alexey Bataev | 3a3bf0b | 2014-09-22 10:01:53 +0000 | [diff] [blame] | 1802 | llvm::Type *TypeParams[] = { |
| 1803 | getIdentTyPointerTy(), CGM.Int32Ty, |
| 1804 | llvm::PointerType::getUnqual(KmpCriticalNameTy)}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1805 | auto *FnTy = |
Alexey Bataev | 3a3bf0b | 2014-09-22 10:01:53 +0000 | [diff] [blame] | 1806 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false); |
| 1807 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_end_critical"); |
| 1808 | break; |
| 1809 | } |
Alexey Bataev | 8f7c1b0 | 2014-12-05 04:09:23 +0000 | [diff] [blame] | 1810 | case OMPRTL__kmpc_cancel_barrier: { |
| 1811 | // Build kmp_int32 __kmpc_cancel_barrier(ident_t *loc, kmp_int32 |
| 1812 | // global_tid); |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 1813 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1814 | auto *FnTy = |
Alexey Bataev | 8f7c1b0 | 2014-12-05 04:09:23 +0000 | [diff] [blame] | 1815 | llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg*/ false); |
| 1816 | RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name*/ "__kmpc_cancel_barrier"); |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 1817 | break; |
| 1818 | } |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 1819 | case OMPRTL__kmpc_barrier: { |
Alexey Bataev | 7d5d33e | 2015-07-06 05:50:32 +0000 | [diff] [blame] | 1820 | // Build void __kmpc_barrier(ident_t *loc, kmp_int32 global_tid); |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 1821 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1822 | auto *FnTy = |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 1823 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false); |
| 1824 | RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name*/ "__kmpc_barrier"); |
| 1825 | break; |
| 1826 | } |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 1827 | case OMPRTL__kmpc_for_static_fini: { |
| 1828 | // Build void __kmpc_for_static_fini(ident_t *loc, kmp_int32 global_tid); |
| 1829 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1830 | auto *FnTy = |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 1831 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false); |
| 1832 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_for_static_fini"); |
| 1833 | break; |
| 1834 | } |
Alexey Bataev | b205978 | 2014-10-13 08:23:51 +0000 | [diff] [blame] | 1835 | case OMPRTL__kmpc_push_num_threads: { |
| 1836 | // Build void __kmpc_push_num_threads(ident_t *loc, kmp_int32 global_tid, |
| 1837 | // kmp_int32 num_threads) |
| 1838 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty, |
| 1839 | CGM.Int32Ty}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1840 | auto *FnTy = |
Alexey Bataev | b205978 | 2014-10-13 08:23:51 +0000 | [diff] [blame] | 1841 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false); |
| 1842 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_push_num_threads"); |
| 1843 | break; |
| 1844 | } |
Alexey Bataev | d74d060 | 2014-10-13 06:02:40 +0000 | [diff] [blame] | 1845 | case OMPRTL__kmpc_serialized_parallel: { |
| 1846 | // Build void __kmpc_serialized_parallel(ident_t *loc, kmp_int32 |
| 1847 | // global_tid); |
| 1848 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1849 | auto *FnTy = |
Alexey Bataev | d74d060 | 2014-10-13 06:02:40 +0000 | [diff] [blame] | 1850 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false); |
| 1851 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_serialized_parallel"); |
| 1852 | break; |
| 1853 | } |
| 1854 | case OMPRTL__kmpc_end_serialized_parallel: { |
| 1855 | // Build void __kmpc_end_serialized_parallel(ident_t *loc, kmp_int32 |
| 1856 | // global_tid); |
| 1857 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1858 | auto *FnTy = |
Alexey Bataev | d74d060 | 2014-10-13 06:02:40 +0000 | [diff] [blame] | 1859 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false); |
| 1860 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_end_serialized_parallel"); |
| 1861 | break; |
| 1862 | } |
Alexey Bataev | cc37cc1 | 2014-11-20 04:34:54 +0000 | [diff] [blame] | 1863 | case OMPRTL__kmpc_flush: { |
Alexey Bataev | d76df6d | 2015-02-24 12:55:09 +0000 | [diff] [blame] | 1864 | // Build void __kmpc_flush(ident_t *loc); |
Alexey Bataev | cc37cc1 | 2014-11-20 04:34:54 +0000 | [diff] [blame] | 1865 | llvm::Type *TypeParams[] = {getIdentTyPointerTy()}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1866 | auto *FnTy = |
Alexey Bataev | d76df6d | 2015-02-24 12:55:09 +0000 | [diff] [blame] | 1867 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false); |
Alexey Bataev | cc37cc1 | 2014-11-20 04:34:54 +0000 | [diff] [blame] | 1868 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_flush"); |
| 1869 | break; |
| 1870 | } |
Alexey Bataev | 8d69065 | 2014-12-04 07:23:53 +0000 | [diff] [blame] | 1871 | case OMPRTL__kmpc_master: { |
| 1872 | // Build kmp_int32 __kmpc_master(ident_t *loc, kmp_int32 global_tid); |
| 1873 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1874 | auto *FnTy = |
Alexey Bataev | 8d69065 | 2014-12-04 07:23:53 +0000 | [diff] [blame] | 1875 | llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg=*/false); |
| 1876 | RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_master"); |
| 1877 | break; |
| 1878 | } |
| 1879 | case OMPRTL__kmpc_end_master: { |
| 1880 | // Build void __kmpc_end_master(ident_t *loc, kmp_int32 global_tid); |
| 1881 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1882 | auto *FnTy = |
Alexey Bataev | 8d69065 | 2014-12-04 07:23:53 +0000 | [diff] [blame] | 1883 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false); |
| 1884 | RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_end_master"); |
| 1885 | break; |
| 1886 | } |
Alexey Bataev | 9f797f3 | 2015-02-05 05:57:51 +0000 | [diff] [blame] | 1887 | case OMPRTL__kmpc_omp_taskyield: { |
| 1888 | // Build kmp_int32 __kmpc_omp_taskyield(ident_t *, kmp_int32 global_tid, |
| 1889 | // int end_part); |
| 1890 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty, CGM.IntTy}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1891 | auto *FnTy = |
Alexey Bataev | 9f797f3 | 2015-02-05 05:57:51 +0000 | [diff] [blame] | 1892 | llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg=*/false); |
| 1893 | RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_omp_taskyield"); |
| 1894 | break; |
| 1895 | } |
Alexey Bataev | 6956e2e | 2015-02-05 06:35:41 +0000 | [diff] [blame] | 1896 | case OMPRTL__kmpc_single: { |
| 1897 | // Build kmp_int32 __kmpc_single(ident_t *loc, kmp_int32 global_tid); |
| 1898 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1899 | auto *FnTy = |
Alexey Bataev | 6956e2e | 2015-02-05 06:35:41 +0000 | [diff] [blame] | 1900 | llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg=*/false); |
| 1901 | RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_single"); |
| 1902 | break; |
| 1903 | } |
| 1904 | case OMPRTL__kmpc_end_single: { |
| 1905 | // Build void __kmpc_end_single(ident_t *loc, kmp_int32 global_tid); |
| 1906 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1907 | auto *FnTy = |
Alexey Bataev | 6956e2e | 2015-02-05 06:35:41 +0000 | [diff] [blame] | 1908 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false); |
| 1909 | RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_end_single"); |
| 1910 | break; |
| 1911 | } |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 1912 | case OMPRTL__kmpc_omp_task_alloc: { |
| 1913 | // Build kmp_task_t *__kmpc_omp_task_alloc(ident_t *, kmp_int32 gtid, |
| 1914 | // kmp_int32 flags, size_t sizeof_kmp_task_t, size_t sizeof_shareds, |
| 1915 | // kmp_routine_entry_t *task_entry); |
| 1916 | assert(KmpRoutineEntryPtrTy != nullptr && |
| 1917 | "Type kmp_routine_entry_t must be created."); |
| 1918 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty, CGM.Int32Ty, |
| 1919 | CGM.SizeTy, CGM.SizeTy, KmpRoutineEntryPtrTy}; |
| 1920 | // Return void * and then cast to particular kmp_task_t type. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1921 | auto *FnTy = |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 1922 | llvm::FunctionType::get(CGM.VoidPtrTy, TypeParams, /*isVarArg=*/false); |
| 1923 | RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_omp_task_alloc"); |
| 1924 | break; |
| 1925 | } |
Gheorghe-Teodor Bercea | 545a9fe | 2019-06-14 20:19:54 +0000 | [diff] [blame] | 1926 | case OMPRTL__kmpc_omp_target_task_alloc: { |
| 1927 | // Build kmp_task_t *__kmpc_omp_target_task_alloc(ident_t *, kmp_int32 gtid, |
| 1928 | // kmp_int32 flags, size_t sizeof_kmp_task_t, size_t sizeof_shareds, |
| 1929 | // kmp_routine_entry_t *task_entry, kmp_int64 device_id); |
| 1930 | assert(KmpRoutineEntryPtrTy != nullptr && |
| 1931 | "Type kmp_routine_entry_t must be created."); |
| 1932 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty, CGM.Int32Ty, |
| 1933 | CGM.SizeTy, CGM.SizeTy, KmpRoutineEntryPtrTy, |
| 1934 | CGM.Int64Ty}; |
| 1935 | // Return void * and then cast to particular kmp_task_t type. |
| 1936 | auto *FnTy = |
| 1937 | llvm::FunctionType::get(CGM.VoidPtrTy, TypeParams, /*isVarArg=*/false); |
| 1938 | RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_omp_target_task_alloc"); |
| 1939 | break; |
| 1940 | } |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 1941 | case OMPRTL__kmpc_omp_task: { |
| 1942 | // Build kmp_int32 __kmpc_omp_task(ident_t *, kmp_int32 gtid, kmp_task_t |
| 1943 | // *new_task); |
| 1944 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty, |
| 1945 | CGM.VoidPtrTy}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1946 | auto *FnTy = |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 1947 | llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg=*/false); |
| 1948 | RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_omp_task"); |
| 1949 | break; |
| 1950 | } |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 1951 | case OMPRTL__kmpc_copyprivate: { |
| 1952 | // Build void __kmpc_copyprivate(ident_t *loc, kmp_int32 global_tid, |
Alexey Bataev | 66beaa9 | 2015-04-30 03:47:32 +0000 | [diff] [blame] | 1953 | // size_t cpy_size, void *cpy_data, void(*cpy_func)(void *, void *), |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 1954 | // kmp_int32 didit); |
| 1955 | llvm::Type *CpyTypeParams[] = {CGM.VoidPtrTy, CGM.VoidPtrTy}; |
| 1956 | auto *CpyFnTy = |
| 1957 | llvm::FunctionType::get(CGM.VoidTy, CpyTypeParams, /*isVarArg=*/false); |
Alexey Bataev | 66beaa9 | 2015-04-30 03:47:32 +0000 | [diff] [blame] | 1958 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty, CGM.SizeTy, |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 1959 | CGM.VoidPtrTy, CpyFnTy->getPointerTo(), |
| 1960 | CGM.Int32Ty}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1961 | auto *FnTy = |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 1962 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false); |
| 1963 | RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_copyprivate"); |
| 1964 | break; |
| 1965 | } |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 1966 | case OMPRTL__kmpc_reduce: { |
| 1967 | // Build kmp_int32 __kmpc_reduce(ident_t *loc, kmp_int32 global_tid, |
| 1968 | // kmp_int32 num_vars, size_t reduce_size, void *reduce_data, void |
| 1969 | // (*reduce_func)(void *lhs_data, void *rhs_data), kmp_critical_name *lck); |
| 1970 | llvm::Type *ReduceTypeParams[] = {CGM.VoidPtrTy, CGM.VoidPtrTy}; |
| 1971 | auto *ReduceFnTy = llvm::FunctionType::get(CGM.VoidTy, ReduceTypeParams, |
| 1972 | /*isVarArg=*/false); |
| 1973 | llvm::Type *TypeParams[] = { |
| 1974 | getIdentTyPointerTy(), CGM.Int32Ty, CGM.Int32Ty, CGM.SizeTy, |
| 1975 | CGM.VoidPtrTy, ReduceFnTy->getPointerTo(), |
| 1976 | llvm::PointerType::getUnqual(KmpCriticalNameTy)}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1977 | auto *FnTy = |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 1978 | llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg=*/false); |
| 1979 | RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_reduce"); |
| 1980 | break; |
| 1981 | } |
| 1982 | case OMPRTL__kmpc_reduce_nowait: { |
| 1983 | // Build kmp_int32 __kmpc_reduce_nowait(ident_t *loc, kmp_int32 |
| 1984 | // global_tid, kmp_int32 num_vars, size_t reduce_size, void *reduce_data, |
| 1985 | // void (*reduce_func)(void *lhs_data, void *rhs_data), kmp_critical_name |
| 1986 | // *lck); |
| 1987 | llvm::Type *ReduceTypeParams[] = {CGM.VoidPtrTy, CGM.VoidPtrTy}; |
| 1988 | auto *ReduceFnTy = llvm::FunctionType::get(CGM.VoidTy, ReduceTypeParams, |
| 1989 | /*isVarArg=*/false); |
| 1990 | llvm::Type *TypeParams[] = { |
| 1991 | getIdentTyPointerTy(), CGM.Int32Ty, CGM.Int32Ty, CGM.SizeTy, |
| 1992 | CGM.VoidPtrTy, ReduceFnTy->getPointerTo(), |
| 1993 | llvm::PointerType::getUnqual(KmpCriticalNameTy)}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 1994 | auto *FnTy = |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 1995 | llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg=*/false); |
| 1996 | RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_reduce_nowait"); |
| 1997 | break; |
| 1998 | } |
| 1999 | case OMPRTL__kmpc_end_reduce: { |
| 2000 | // Build void __kmpc_end_reduce(ident_t *loc, kmp_int32 global_tid, |
| 2001 | // kmp_critical_name *lck); |
| 2002 | llvm::Type *TypeParams[] = { |
| 2003 | getIdentTyPointerTy(), CGM.Int32Ty, |
| 2004 | llvm::PointerType::getUnqual(KmpCriticalNameTy)}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2005 | auto *FnTy = |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 2006 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false); |
| 2007 | RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_end_reduce"); |
| 2008 | break; |
| 2009 | } |
| 2010 | case OMPRTL__kmpc_end_reduce_nowait: { |
| 2011 | // Build __kmpc_end_reduce_nowait(ident_t *loc, kmp_int32 global_tid, |
| 2012 | // kmp_critical_name *lck); |
| 2013 | llvm::Type *TypeParams[] = { |
| 2014 | getIdentTyPointerTy(), CGM.Int32Ty, |
| 2015 | llvm::PointerType::getUnqual(KmpCriticalNameTy)}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2016 | auto *FnTy = |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 2017 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false); |
| 2018 | RTLFn = |
| 2019 | CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_end_reduce_nowait"); |
| 2020 | break; |
| 2021 | } |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 2022 | case OMPRTL__kmpc_omp_task_begin_if0: { |
| 2023 | // Build void __kmpc_omp_task(ident_t *, kmp_int32 gtid, kmp_task_t |
| 2024 | // *new_task); |
| 2025 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty, |
| 2026 | CGM.VoidPtrTy}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2027 | auto *FnTy = |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 2028 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false); |
| 2029 | RTLFn = |
| 2030 | CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_omp_task_begin_if0"); |
| 2031 | break; |
| 2032 | } |
| 2033 | case OMPRTL__kmpc_omp_task_complete_if0: { |
| 2034 | // Build void __kmpc_omp_task(ident_t *, kmp_int32 gtid, kmp_task_t |
| 2035 | // *new_task); |
| 2036 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty, |
| 2037 | CGM.VoidPtrTy}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2038 | auto *FnTy = |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 2039 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false); |
| 2040 | RTLFn = CGM.CreateRuntimeFunction(FnTy, |
| 2041 | /*Name=*/"__kmpc_omp_task_complete_if0"); |
| 2042 | break; |
| 2043 | } |
Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 2044 | case OMPRTL__kmpc_ordered: { |
| 2045 | // Build void __kmpc_ordered(ident_t *loc, kmp_int32 global_tid); |
| 2046 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2047 | auto *FnTy = |
Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 2048 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false); |
| 2049 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_ordered"); |
| 2050 | break; |
| 2051 | } |
| 2052 | case OMPRTL__kmpc_end_ordered: { |
Alexey Bataev | c30dd2d | 2015-06-18 12:14:09 +0000 | [diff] [blame] | 2053 | // Build void __kmpc_end_ordered(ident_t *loc, kmp_int32 global_tid); |
Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 2054 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2055 | auto *FnTy = |
Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 2056 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false); |
| 2057 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_end_ordered"); |
| 2058 | break; |
| 2059 | } |
Alexey Bataev | 8b8e202 | 2015-04-27 05:22:09 +0000 | [diff] [blame] | 2060 | case OMPRTL__kmpc_omp_taskwait: { |
| 2061 | // Build kmp_int32 __kmpc_omp_taskwait(ident_t *loc, kmp_int32 global_tid); |
| 2062 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2063 | auto *FnTy = |
Alexey Bataev | 8b8e202 | 2015-04-27 05:22:09 +0000 | [diff] [blame] | 2064 | llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg=*/false); |
| 2065 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_omp_taskwait"); |
| 2066 | break; |
| 2067 | } |
Alexey Bataev | c30dd2d | 2015-06-18 12:14:09 +0000 | [diff] [blame] | 2068 | case OMPRTL__kmpc_taskgroup: { |
| 2069 | // Build void __kmpc_taskgroup(ident_t *loc, kmp_int32 global_tid); |
| 2070 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2071 | auto *FnTy = |
Alexey Bataev | c30dd2d | 2015-06-18 12:14:09 +0000 | [diff] [blame] | 2072 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false); |
| 2073 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_taskgroup"); |
| 2074 | break; |
| 2075 | } |
| 2076 | case OMPRTL__kmpc_end_taskgroup: { |
| 2077 | // Build void __kmpc_end_taskgroup(ident_t *loc, kmp_int32 global_tid); |
| 2078 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2079 | auto *FnTy = |
Alexey Bataev | c30dd2d | 2015-06-18 12:14:09 +0000 | [diff] [blame] | 2080 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false); |
| 2081 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_end_taskgroup"); |
| 2082 | break; |
| 2083 | } |
Alexey Bataev | 7f210c6 | 2015-06-18 13:40:03 +0000 | [diff] [blame] | 2084 | case OMPRTL__kmpc_push_proc_bind: { |
| 2085 | // Build void __kmpc_push_proc_bind(ident_t *loc, kmp_int32 global_tid, |
| 2086 | // int proc_bind) |
| 2087 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty, CGM.IntTy}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2088 | auto *FnTy = |
Alexey Bataev | 7f210c6 | 2015-06-18 13:40:03 +0000 | [diff] [blame] | 2089 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false); |
| 2090 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_push_proc_bind"); |
| 2091 | break; |
| 2092 | } |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 2093 | case OMPRTL__kmpc_omp_task_with_deps: { |
| 2094 | // Build kmp_int32 __kmpc_omp_task_with_deps(ident_t *, kmp_int32 gtid, |
| 2095 | // kmp_task_t *new_task, kmp_int32 ndeps, kmp_depend_info_t *dep_list, |
| 2096 | // kmp_int32 ndeps_noalias, kmp_depend_info_t *noalias_dep_list); |
| 2097 | llvm::Type *TypeParams[] = { |
| 2098 | getIdentTyPointerTy(), CGM.Int32Ty, CGM.VoidPtrTy, CGM.Int32Ty, |
| 2099 | CGM.VoidPtrTy, CGM.Int32Ty, CGM.VoidPtrTy}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2100 | auto *FnTy = |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 2101 | llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg=*/false); |
| 2102 | RTLFn = |
| 2103 | CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_omp_task_with_deps"); |
| 2104 | break; |
| 2105 | } |
| 2106 | case OMPRTL__kmpc_omp_wait_deps: { |
| 2107 | // Build void __kmpc_omp_wait_deps(ident_t *, kmp_int32 gtid, |
| 2108 | // kmp_int32 ndeps, kmp_depend_info_t *dep_list, kmp_int32 ndeps_noalias, |
| 2109 | // kmp_depend_info_t *noalias_dep_list); |
| 2110 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty, |
| 2111 | CGM.Int32Ty, CGM.VoidPtrTy, |
| 2112 | CGM.Int32Ty, CGM.VoidPtrTy}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2113 | auto *FnTy = |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 2114 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false); |
| 2115 | RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_omp_wait_deps"); |
| 2116 | break; |
| 2117 | } |
Alexey Bataev | 0f34da1 | 2015-07-02 04:17:07 +0000 | [diff] [blame] | 2118 | case OMPRTL__kmpc_cancellationpoint: { |
| 2119 | // Build kmp_int32 __kmpc_cancellationpoint(ident_t *loc, kmp_int32 |
| 2120 | // global_tid, kmp_int32 cncl_kind) |
| 2121 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty, CGM.IntTy}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2122 | auto *FnTy = |
Alexey Bataev | 0f34da1 | 2015-07-02 04:17:07 +0000 | [diff] [blame] | 2123 | llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg*/ false); |
| 2124 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_cancellationpoint"); |
| 2125 | break; |
| 2126 | } |
Alexey Bataev | 7d5d33e | 2015-07-06 05:50:32 +0000 | [diff] [blame] | 2127 | case OMPRTL__kmpc_cancel: { |
| 2128 | // Build kmp_int32 __kmpc_cancel(ident_t *loc, kmp_int32 global_tid, |
| 2129 | // kmp_int32 cncl_kind) |
| 2130 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty, CGM.IntTy}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2131 | auto *FnTy = |
Alexey Bataev | 7d5d33e | 2015-07-06 05:50:32 +0000 | [diff] [blame] | 2132 | llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg*/ false); |
| 2133 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_cancel"); |
| 2134 | break; |
| 2135 | } |
Carlo Bertolli | 430d8ec | 2016-03-03 20:34:23 +0000 | [diff] [blame] | 2136 | case OMPRTL__kmpc_push_num_teams: { |
| 2137 | // Build void kmpc_push_num_teams (ident_t loc, kmp_int32 global_tid, |
| 2138 | // kmp_int32 num_teams, kmp_int32 num_threads) |
| 2139 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty, CGM.Int32Ty, |
| 2140 | CGM.Int32Ty}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2141 | auto *FnTy = |
Carlo Bertolli | 430d8ec | 2016-03-03 20:34:23 +0000 | [diff] [blame] | 2142 | llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg*/ false); |
| 2143 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_push_num_teams"); |
| 2144 | break; |
| 2145 | } |
| 2146 | case OMPRTL__kmpc_fork_teams: { |
| 2147 | // Build void __kmpc_fork_teams(ident_t *loc, kmp_int32 argc, kmpc_micro |
| 2148 | // microtask, ...); |
| 2149 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty, |
| 2150 | getKmpc_MicroPointerTy()}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2151 | auto *FnTy = |
Carlo Bertolli | 430d8ec | 2016-03-03 20:34:23 +0000 | [diff] [blame] | 2152 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ true); |
| 2153 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_fork_teams"); |
James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 2154 | if (auto *F = dyn_cast<llvm::Function>(RTLFn.getCallee())) { |
Johannes Doerfert | ac991bb | 2019-01-19 05:36:54 +0000 | [diff] [blame] | 2155 | if (!F->hasMetadata(llvm::LLVMContext::MD_callback)) { |
| 2156 | llvm::LLVMContext &Ctx = F->getContext(); |
| 2157 | llvm::MDBuilder MDB(Ctx); |
| 2158 | // Annotate the callback behavior of the __kmpc_fork_teams: |
| 2159 | // - The callback callee is argument number 2 (microtask). |
| 2160 | // - The first two arguments of the callback callee are unknown (-1). |
| 2161 | // - All variadic arguments to the __kmpc_fork_teams are passed to the |
| 2162 | // callback callee. |
| 2163 | F->addMetadata( |
| 2164 | llvm::LLVMContext::MD_callback, |
| 2165 | *llvm::MDNode::get(Ctx, {MDB.createCallbackEncoding( |
| 2166 | 2, {-1, -1}, |
| 2167 | /* VarArgsArePassed */ true)})); |
| 2168 | } |
| 2169 | } |
Carlo Bertolli | 430d8ec | 2016-03-03 20:34:23 +0000 | [diff] [blame] | 2170 | break; |
| 2171 | } |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 2172 | case OMPRTL__kmpc_taskloop: { |
| 2173 | // Build void __kmpc_taskloop(ident_t *loc, int gtid, kmp_task_t *task, int |
| 2174 | // if_val, kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st, int nogroup, int |
| 2175 | // sched, kmp_uint64 grainsize, void *task_dup); |
| 2176 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), |
| 2177 | CGM.IntTy, |
| 2178 | CGM.VoidPtrTy, |
| 2179 | CGM.IntTy, |
| 2180 | CGM.Int64Ty->getPointerTo(), |
| 2181 | CGM.Int64Ty->getPointerTo(), |
| 2182 | CGM.Int64Ty, |
| 2183 | CGM.IntTy, |
| 2184 | CGM.IntTy, |
| 2185 | CGM.Int64Ty, |
| 2186 | CGM.VoidPtrTy}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2187 | auto *FnTy = |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 2188 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false); |
| 2189 | RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_taskloop"); |
| 2190 | break; |
| 2191 | } |
Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 2192 | case OMPRTL__kmpc_doacross_init: { |
| 2193 | // Build void __kmpc_doacross_init(ident_t *loc, kmp_int32 gtid, kmp_int32 |
| 2194 | // num_dims, struct kmp_dim *dims); |
| 2195 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), |
| 2196 | CGM.Int32Ty, |
| 2197 | CGM.Int32Ty, |
| 2198 | CGM.VoidPtrTy}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2199 | auto *FnTy = |
Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 2200 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false); |
| 2201 | RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_doacross_init"); |
| 2202 | break; |
| 2203 | } |
| 2204 | case OMPRTL__kmpc_doacross_fini: { |
| 2205 | // Build void __kmpc_doacross_fini(ident_t *loc, kmp_int32 gtid); |
| 2206 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2207 | auto *FnTy = |
Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 2208 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false); |
| 2209 | RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_doacross_fini"); |
| 2210 | break; |
| 2211 | } |
| 2212 | case OMPRTL__kmpc_doacross_post: { |
| 2213 | // Build void __kmpc_doacross_post(ident_t *loc, kmp_int32 gtid, kmp_int64 |
| 2214 | // *vec); |
| 2215 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty, |
| 2216 | CGM.Int64Ty->getPointerTo()}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2217 | auto *FnTy = |
Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 2218 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false); |
| 2219 | RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_doacross_post"); |
| 2220 | break; |
| 2221 | } |
| 2222 | case OMPRTL__kmpc_doacross_wait: { |
| 2223 | // Build void __kmpc_doacross_wait(ident_t *loc, kmp_int32 gtid, kmp_int64 |
| 2224 | // *vec); |
| 2225 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty, |
| 2226 | CGM.Int64Ty->getPointerTo()}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2227 | auto *FnTy = |
Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 2228 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false); |
| 2229 | RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_doacross_wait"); |
| 2230 | break; |
| 2231 | } |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 2232 | case OMPRTL__kmpc_task_reduction_init: { |
| 2233 | // Build void *__kmpc_task_reduction_init(int gtid, int num_data, void |
| 2234 | // *data); |
| 2235 | llvm::Type *TypeParams[] = {CGM.IntTy, CGM.IntTy, CGM.VoidPtrTy}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2236 | auto *FnTy = |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 2237 | llvm::FunctionType::get(CGM.VoidPtrTy, TypeParams, /*isVarArg=*/false); |
| 2238 | RTLFn = |
| 2239 | CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_task_reduction_init"); |
| 2240 | break; |
| 2241 | } |
| 2242 | case OMPRTL__kmpc_task_reduction_get_th_data: { |
| 2243 | // Build void *__kmpc_task_reduction_get_th_data(int gtid, void *tg, void |
| 2244 | // *d); |
| 2245 | llvm::Type *TypeParams[] = {CGM.IntTy, CGM.VoidPtrTy, CGM.VoidPtrTy}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2246 | auto *FnTy = |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 2247 | llvm::FunctionType::get(CGM.VoidPtrTy, TypeParams, /*isVarArg=*/false); |
| 2248 | RTLFn = CGM.CreateRuntimeFunction( |
| 2249 | FnTy, /*Name=*/"__kmpc_task_reduction_get_th_data"); |
| 2250 | break; |
| 2251 | } |
Alexey Bataev | 4f680db | 2019-03-19 16:41:16 +0000 | [diff] [blame] | 2252 | case OMPRTL__kmpc_alloc: { |
Alexey Bataev | 6cf7b71 | 2019-04-08 19:06:42 +0000 | [diff] [blame] | 2253 | // Build to void *__kmpc_alloc(int gtid, size_t sz, omp_allocator_handle_t |
| 2254 | // al); omp_allocator_handle_t type is void *. |
| 2255 | llvm::Type *TypeParams[] = {CGM.IntTy, CGM.SizeTy, CGM.VoidPtrTy}; |
Alexey Bataev | 4f680db | 2019-03-19 16:41:16 +0000 | [diff] [blame] | 2256 | auto *FnTy = |
| 2257 | llvm::FunctionType::get(CGM.VoidPtrTy, TypeParams, /*isVarArg=*/false); |
| 2258 | RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_alloc"); |
| 2259 | break; |
| 2260 | } |
| 2261 | case OMPRTL__kmpc_free: { |
Alexey Bataev | 6cf7b71 | 2019-04-08 19:06:42 +0000 | [diff] [blame] | 2262 | // Build to void __kmpc_free(int gtid, void *ptr, omp_allocator_handle_t |
| 2263 | // al); omp_allocator_handle_t type is void *. |
| 2264 | llvm::Type *TypeParams[] = {CGM.IntTy, CGM.VoidPtrTy, CGM.VoidPtrTy}; |
Alexey Bataev | 4f680db | 2019-03-19 16:41:16 +0000 | [diff] [blame] | 2265 | auto *FnTy = |
| 2266 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false); |
| 2267 | RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_free"); |
| 2268 | break; |
| 2269 | } |
Alexey Bataev | 7bb3353 | 2019-01-07 21:30:43 +0000 | [diff] [blame] | 2270 | case OMPRTL__kmpc_push_target_tripcount: { |
| 2271 | // Build void __kmpc_push_target_tripcount(int64_t device_id, kmp_uint64 |
| 2272 | // size); |
| 2273 | llvm::Type *TypeParams[] = {CGM.Int64Ty, CGM.Int64Ty}; |
| 2274 | llvm::FunctionType *FnTy = |
| 2275 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false); |
| 2276 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_push_target_tripcount"); |
| 2277 | break; |
| 2278 | } |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 2279 | case OMPRTL__tgt_target: { |
George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 2280 | // Build int32_t __tgt_target(int64_t device_id, void *host_ptr, int32_t |
| 2281 | // 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] | 2282 | // *arg_types); |
George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 2283 | llvm::Type *TypeParams[] = {CGM.Int64Ty, |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 2284 | CGM.VoidPtrTy, |
| 2285 | CGM.Int32Ty, |
| 2286 | CGM.VoidPtrPtrTy, |
| 2287 | CGM.VoidPtrPtrTy, |
| 2288 | CGM.SizeTy->getPointerTo(), |
George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 2289 | CGM.Int64Ty->getPointerTo()}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2290 | auto *FnTy = |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 2291 | llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg*/ false); |
| 2292 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__tgt_target"); |
| 2293 | break; |
| 2294 | } |
Alexey Bataev | a9f77c6 | 2017-12-13 21:04:20 +0000 | [diff] [blame] | 2295 | case OMPRTL__tgt_target_nowait: { |
| 2296 | // Build int32_t __tgt_target_nowait(int64_t device_id, void *host_ptr, |
| 2297 | // int32_t arg_num, void** args_base, void **args, size_t *arg_sizes, |
| 2298 | // int64_t *arg_types); |
| 2299 | llvm::Type *TypeParams[] = {CGM.Int64Ty, |
| 2300 | CGM.VoidPtrTy, |
| 2301 | CGM.Int32Ty, |
| 2302 | CGM.VoidPtrPtrTy, |
| 2303 | CGM.VoidPtrPtrTy, |
| 2304 | CGM.SizeTy->getPointerTo(), |
| 2305 | CGM.Int64Ty->getPointerTo()}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2306 | auto *FnTy = |
Alexey Bataev | a9f77c6 | 2017-12-13 21:04:20 +0000 | [diff] [blame] | 2307 | llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg*/ false); |
| 2308 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__tgt_target_nowait"); |
| 2309 | break; |
| 2310 | } |
Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 2311 | case OMPRTL__tgt_target_teams: { |
George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 2312 | // 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] | 2313 | // 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] | 2314 | // int64_t *arg_types, int32_t num_teams, int32_t thread_limit); |
| 2315 | llvm::Type *TypeParams[] = {CGM.Int64Ty, |
Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 2316 | CGM.VoidPtrTy, |
| 2317 | CGM.Int32Ty, |
| 2318 | CGM.VoidPtrPtrTy, |
| 2319 | CGM.VoidPtrPtrTy, |
| 2320 | CGM.SizeTy->getPointerTo(), |
George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 2321 | CGM.Int64Ty->getPointerTo(), |
Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 2322 | CGM.Int32Ty, |
| 2323 | CGM.Int32Ty}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2324 | auto *FnTy = |
Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 2325 | llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg*/ false); |
| 2326 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__tgt_target_teams"); |
| 2327 | break; |
| 2328 | } |
Alexey Bataev | a9f77c6 | 2017-12-13 21:04:20 +0000 | [diff] [blame] | 2329 | case OMPRTL__tgt_target_teams_nowait: { |
| 2330 | // Build int32_t __tgt_target_teams_nowait(int64_t device_id, void |
| 2331 | // *host_ptr, int32_t arg_num, void** args_base, void **args, size_t |
| 2332 | // *arg_sizes, int64_t *arg_types, int32_t num_teams, int32_t thread_limit); |
| 2333 | llvm::Type *TypeParams[] = {CGM.Int64Ty, |
| 2334 | CGM.VoidPtrTy, |
| 2335 | CGM.Int32Ty, |
| 2336 | CGM.VoidPtrPtrTy, |
| 2337 | CGM.VoidPtrPtrTy, |
| 2338 | CGM.SizeTy->getPointerTo(), |
| 2339 | CGM.Int64Ty->getPointerTo(), |
| 2340 | CGM.Int32Ty, |
| 2341 | CGM.Int32Ty}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2342 | auto *FnTy = |
Alexey Bataev | a9f77c6 | 2017-12-13 21:04:20 +0000 | [diff] [blame] | 2343 | llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg*/ false); |
| 2344 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__tgt_target_teams_nowait"); |
| 2345 | break; |
| 2346 | } |
Gheorghe-Teodor Bercea | 66cdbb47 | 2019-05-21 19:42:01 +0000 | [diff] [blame] | 2347 | case OMPRTL__tgt_register_requires: { |
| 2348 | // Build void __tgt_register_requires(int64_t flags); |
| 2349 | llvm::Type *TypeParams[] = {CGM.Int64Ty}; |
| 2350 | auto *FnTy = |
| 2351 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false); |
| 2352 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__tgt_register_requires"); |
| 2353 | break; |
| 2354 | } |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 2355 | case OMPRTL__tgt_register_lib: { |
| 2356 | // Build void __tgt_register_lib(__tgt_bin_desc *desc); |
| 2357 | QualType ParamTy = |
| 2358 | CGM.getContext().getPointerType(getTgtBinaryDescriptorQTy()); |
| 2359 | llvm::Type *TypeParams[] = {CGM.getTypes().ConvertTypeForMem(ParamTy)}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2360 | auto *FnTy = |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 2361 | llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg*/ false); |
| 2362 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__tgt_register_lib"); |
| 2363 | break; |
| 2364 | } |
| 2365 | case OMPRTL__tgt_unregister_lib: { |
| 2366 | // Build void __tgt_unregister_lib(__tgt_bin_desc *desc); |
| 2367 | QualType ParamTy = |
| 2368 | CGM.getContext().getPointerType(getTgtBinaryDescriptorQTy()); |
| 2369 | llvm::Type *TypeParams[] = {CGM.getTypes().ConvertTypeForMem(ParamTy)}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2370 | auto *FnTy = |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 2371 | llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg*/ false); |
| 2372 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__tgt_unregister_lib"); |
| 2373 | break; |
| 2374 | } |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 2375 | case OMPRTL__tgt_target_data_begin: { |
George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 2376 | // Build void __tgt_target_data_begin(int64_t device_id, int32_t arg_num, |
| 2377 | // void** args_base, void **args, size_t *arg_sizes, int64_t *arg_types); |
| 2378 | llvm::Type *TypeParams[] = {CGM.Int64Ty, |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 2379 | CGM.Int32Ty, |
| 2380 | CGM.VoidPtrPtrTy, |
| 2381 | CGM.VoidPtrPtrTy, |
| 2382 | CGM.SizeTy->getPointerTo(), |
George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 2383 | CGM.Int64Ty->getPointerTo()}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2384 | auto *FnTy = |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 2385 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false); |
| 2386 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__tgt_target_data_begin"); |
| 2387 | break; |
| 2388 | } |
Alexey Bataev | 0cc6b8e | 2017-12-14 17:00:17 +0000 | [diff] [blame] | 2389 | case OMPRTL__tgt_target_data_begin_nowait: { |
| 2390 | // Build void __tgt_target_data_begin_nowait(int64_t device_id, int32_t |
| 2391 | // arg_num, void** args_base, void **args, size_t *arg_sizes, int64_t |
| 2392 | // *arg_types); |
| 2393 | llvm::Type *TypeParams[] = {CGM.Int64Ty, |
| 2394 | CGM.Int32Ty, |
| 2395 | CGM.VoidPtrPtrTy, |
| 2396 | CGM.VoidPtrPtrTy, |
| 2397 | CGM.SizeTy->getPointerTo(), |
| 2398 | CGM.Int64Ty->getPointerTo()}; |
| 2399 | auto *FnTy = |
| 2400 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false); |
| 2401 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__tgt_target_data_begin_nowait"); |
| 2402 | break; |
| 2403 | } |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 2404 | case OMPRTL__tgt_target_data_end: { |
George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 2405 | // Build void __tgt_target_data_end(int64_t device_id, int32_t arg_num, |
| 2406 | // void** args_base, void **args, size_t *arg_sizes, int64_t *arg_types); |
| 2407 | llvm::Type *TypeParams[] = {CGM.Int64Ty, |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 2408 | CGM.Int32Ty, |
| 2409 | CGM.VoidPtrPtrTy, |
| 2410 | CGM.VoidPtrPtrTy, |
| 2411 | CGM.SizeTy->getPointerTo(), |
George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 2412 | CGM.Int64Ty->getPointerTo()}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2413 | auto *FnTy = |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 2414 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false); |
| 2415 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__tgt_target_data_end"); |
| 2416 | break; |
| 2417 | } |
Alexey Bataev | 0cc6b8e | 2017-12-14 17:00:17 +0000 | [diff] [blame] | 2418 | case OMPRTL__tgt_target_data_end_nowait: { |
| 2419 | // Build void __tgt_target_data_end_nowait(int64_t device_id, int32_t |
| 2420 | // arg_num, void** args_base, void **args, size_t *arg_sizes, int64_t |
| 2421 | // *arg_types); |
| 2422 | llvm::Type *TypeParams[] = {CGM.Int64Ty, |
| 2423 | CGM.Int32Ty, |
| 2424 | CGM.VoidPtrPtrTy, |
| 2425 | CGM.VoidPtrPtrTy, |
| 2426 | CGM.SizeTy->getPointerTo(), |
| 2427 | CGM.Int64Ty->getPointerTo()}; |
| 2428 | auto *FnTy = |
| 2429 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false); |
| 2430 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__tgt_target_data_end_nowait"); |
| 2431 | break; |
| 2432 | } |
Samuel Antao | 8d2d730 | 2016-05-26 18:30:22 +0000 | [diff] [blame] | 2433 | case OMPRTL__tgt_target_data_update: { |
George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 2434 | // Build void __tgt_target_data_update(int64_t device_id, int32_t arg_num, |
| 2435 | // void** args_base, void **args, size_t *arg_sizes, int64_t *arg_types); |
| 2436 | llvm::Type *TypeParams[] = {CGM.Int64Ty, |
Samuel Antao | 8d2d730 | 2016-05-26 18:30:22 +0000 | [diff] [blame] | 2437 | CGM.Int32Ty, |
| 2438 | CGM.VoidPtrPtrTy, |
| 2439 | CGM.VoidPtrPtrTy, |
| 2440 | CGM.SizeTy->getPointerTo(), |
George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 2441 | CGM.Int64Ty->getPointerTo()}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2442 | auto *FnTy = |
Samuel Antao | 8d2d730 | 2016-05-26 18:30:22 +0000 | [diff] [blame] | 2443 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false); |
| 2444 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__tgt_target_data_update"); |
| 2445 | break; |
| 2446 | } |
Alexey Bataev | 0cc6b8e | 2017-12-14 17:00:17 +0000 | [diff] [blame] | 2447 | case OMPRTL__tgt_target_data_update_nowait: { |
| 2448 | // Build void __tgt_target_data_update_nowait(int64_t device_id, int32_t |
| 2449 | // arg_num, void** args_base, void **args, size_t *arg_sizes, int64_t |
| 2450 | // *arg_types); |
| 2451 | llvm::Type *TypeParams[] = {CGM.Int64Ty, |
| 2452 | CGM.Int32Ty, |
| 2453 | CGM.VoidPtrPtrTy, |
| 2454 | CGM.VoidPtrPtrTy, |
| 2455 | CGM.SizeTy->getPointerTo(), |
| 2456 | CGM.Int64Ty->getPointerTo()}; |
| 2457 | auto *FnTy = |
| 2458 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false); |
| 2459 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__tgt_target_data_update_nowait"); |
| 2460 | break; |
| 2461 | } |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 2462 | } |
Alexey Bataev | 50b3c95 | 2016-02-19 10:38:26 +0000 | [diff] [blame] | 2463 | assert(RTLFn && "Unable to find OpenMP runtime function"); |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 2464 | return RTLFn; |
| 2465 | } |
Alexey Bataev | 3a3bf0b | 2014-09-22 10:01:53 +0000 | [diff] [blame] | 2466 | |
James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 2467 | llvm::FunctionCallee |
| 2468 | CGOpenMPRuntime::createForStaticInitFunction(unsigned IVSize, bool IVSigned) { |
Alexander Musman | 21212e4 | 2015-03-13 10:38:23 +0000 | [diff] [blame] | 2469 | assert((IVSize == 32 || IVSize == 64) && |
| 2470 | "IV size is not compatible with the omp runtime"); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2471 | StringRef Name = IVSize == 32 ? (IVSigned ? "__kmpc_for_static_init_4" |
| 2472 | : "__kmpc_for_static_init_4u") |
| 2473 | : (IVSigned ? "__kmpc_for_static_init_8" |
| 2474 | : "__kmpc_for_static_init_8u"); |
| 2475 | llvm::Type *ITy = IVSize == 32 ? CGM.Int32Ty : CGM.Int64Ty; |
| 2476 | auto *PtrTy = llvm::PointerType::getUnqual(ITy); |
Alexander Musman | 21212e4 | 2015-03-13 10:38:23 +0000 | [diff] [blame] | 2477 | llvm::Type *TypeParams[] = { |
| 2478 | getIdentTyPointerTy(), // loc |
| 2479 | CGM.Int32Ty, // tid |
| 2480 | CGM.Int32Ty, // schedtype |
| 2481 | llvm::PointerType::getUnqual(CGM.Int32Ty), // p_lastiter |
| 2482 | PtrTy, // p_lower |
| 2483 | PtrTy, // p_upper |
| 2484 | PtrTy, // p_stride |
| 2485 | ITy, // incr |
| 2486 | ITy // chunk |
| 2487 | }; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2488 | auto *FnTy = |
Alexander Musman | 21212e4 | 2015-03-13 10:38:23 +0000 | [diff] [blame] | 2489 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false); |
| 2490 | return CGM.CreateRuntimeFunction(FnTy, Name); |
| 2491 | } |
| 2492 | |
James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 2493 | llvm::FunctionCallee |
| 2494 | CGOpenMPRuntime::createDispatchInitFunction(unsigned IVSize, bool IVSigned) { |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 2495 | assert((IVSize == 32 || IVSize == 64) && |
| 2496 | "IV size is not compatible with the omp runtime"); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2497 | StringRef Name = |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 2498 | IVSize == 32 |
| 2499 | ? (IVSigned ? "__kmpc_dispatch_init_4" : "__kmpc_dispatch_init_4u") |
| 2500 | : (IVSigned ? "__kmpc_dispatch_init_8" : "__kmpc_dispatch_init_8u"); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2501 | llvm::Type *ITy = IVSize == 32 ? CGM.Int32Ty : CGM.Int64Ty; |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 2502 | llvm::Type *TypeParams[] = { getIdentTyPointerTy(), // loc |
| 2503 | CGM.Int32Ty, // tid |
| 2504 | CGM.Int32Ty, // schedtype |
| 2505 | ITy, // lower |
| 2506 | ITy, // upper |
| 2507 | ITy, // stride |
| 2508 | ITy // chunk |
| 2509 | }; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2510 | auto *FnTy = |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 2511 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false); |
| 2512 | return CGM.CreateRuntimeFunction(FnTy, Name); |
| 2513 | } |
| 2514 | |
James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 2515 | llvm::FunctionCallee |
| 2516 | CGOpenMPRuntime::createDispatchFiniFunction(unsigned IVSize, bool IVSigned) { |
Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 2517 | assert((IVSize == 32 || IVSize == 64) && |
| 2518 | "IV size is not compatible with the omp runtime"); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2519 | StringRef Name = |
Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 2520 | IVSize == 32 |
| 2521 | ? (IVSigned ? "__kmpc_dispatch_fini_4" : "__kmpc_dispatch_fini_4u") |
| 2522 | : (IVSigned ? "__kmpc_dispatch_fini_8" : "__kmpc_dispatch_fini_8u"); |
| 2523 | llvm::Type *TypeParams[] = { |
| 2524 | getIdentTyPointerTy(), // loc |
| 2525 | CGM.Int32Ty, // tid |
| 2526 | }; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2527 | auto *FnTy = |
Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 2528 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false); |
| 2529 | return CGM.CreateRuntimeFunction(FnTy, Name); |
| 2530 | } |
| 2531 | |
James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 2532 | llvm::FunctionCallee |
| 2533 | CGOpenMPRuntime::createDispatchNextFunction(unsigned IVSize, bool IVSigned) { |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 2534 | assert((IVSize == 32 || IVSize == 64) && |
| 2535 | "IV size is not compatible with the omp runtime"); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2536 | StringRef Name = |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 2537 | IVSize == 32 |
| 2538 | ? (IVSigned ? "__kmpc_dispatch_next_4" : "__kmpc_dispatch_next_4u") |
| 2539 | : (IVSigned ? "__kmpc_dispatch_next_8" : "__kmpc_dispatch_next_8u"); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2540 | llvm::Type *ITy = IVSize == 32 ? CGM.Int32Ty : CGM.Int64Ty; |
| 2541 | auto *PtrTy = llvm::PointerType::getUnqual(ITy); |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 2542 | llvm::Type *TypeParams[] = { |
| 2543 | getIdentTyPointerTy(), // loc |
| 2544 | CGM.Int32Ty, // tid |
| 2545 | llvm::PointerType::getUnqual(CGM.Int32Ty), // p_lastiter |
| 2546 | PtrTy, // p_lower |
| 2547 | PtrTy, // p_upper |
| 2548 | PtrTy // p_stride |
| 2549 | }; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2550 | auto *FnTy = |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 2551 | llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg*/ false); |
| 2552 | return CGM.CreateRuntimeFunction(FnTy, Name); |
| 2553 | } |
| 2554 | |
Gheorghe-Teodor Bercea | 0034e84 | 2019-06-20 18:04:47 +0000 | [diff] [blame] | 2555 | Address CGOpenMPRuntime::getAddrOfDeclareTargetVar(const VarDecl *VD) { |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 2556 | if (CGM.getLangOpts().OpenMPSimd) |
| 2557 | return Address::invalid(); |
Alexey Bataev | 92327c5 | 2018-03-26 16:40:55 +0000 | [diff] [blame] | 2558 | llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res = |
Alexey Bataev | 97b7221 | 2018-08-14 18:31:20 +0000 | [diff] [blame] | 2559 | OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD); |
Gheorghe-Teodor Bercea | 0034e84 | 2019-06-20 18:04:47 +0000 | [diff] [blame] | 2560 | if (Res && (*Res == OMPDeclareTargetDeclAttr::MT_Link || |
| 2561 | (*Res == OMPDeclareTargetDeclAttr::MT_To && |
| 2562 | HasRequiresUnifiedSharedMemory))) { |
Alexey Bataev | 92327c5 | 2018-03-26 16:40:55 +0000 | [diff] [blame] | 2563 | SmallString<64> PtrName; |
| 2564 | { |
| 2565 | llvm::raw_svector_ostream OS(PtrName); |
Gheorghe-Teodor Bercea | 0034e84 | 2019-06-20 18:04:47 +0000 | [diff] [blame] | 2566 | OS << CGM.getMangledName(GlobalDecl(VD)) << "_decl_tgt_ref_ptr"; |
Alexey Bataev | 92327c5 | 2018-03-26 16:40:55 +0000 | [diff] [blame] | 2567 | } |
| 2568 | llvm::Value *Ptr = CGM.getModule().getNamedValue(PtrName); |
| 2569 | if (!Ptr) { |
| 2570 | QualType PtrTy = CGM.getContext().getPointerType(VD->getType()); |
| 2571 | Ptr = getOrCreateInternalVariable(CGM.getTypes().ConvertTypeForMem(PtrTy), |
| 2572 | PtrName); |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 2573 | if (!CGM.getLangOpts().OpenMPIsDevice) { |
| 2574 | auto *GV = cast<llvm::GlobalVariable>(Ptr); |
| 2575 | GV->setLinkage(llvm::GlobalValue::ExternalLinkage); |
| 2576 | GV->setInitializer(CGM.GetAddrOfGlobal(VD)); |
| 2577 | } |
| 2578 | CGM.addUsedGlobal(cast<llvm::GlobalValue>(Ptr)); |
| 2579 | registerTargetGlobalVariable(VD, cast<llvm::Constant>(Ptr)); |
Alexey Bataev | 92327c5 | 2018-03-26 16:40:55 +0000 | [diff] [blame] | 2580 | } |
| 2581 | return Address(Ptr, CGM.getContext().getDeclAlign(VD)); |
| 2582 | } |
| 2583 | return Address::invalid(); |
| 2584 | } |
| 2585 | |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2586 | llvm::Constant * |
| 2587 | CGOpenMPRuntime::getOrCreateThreadPrivateCache(const VarDecl *VD) { |
Samuel Antao | f8b5012 | 2015-07-13 22:54:53 +0000 | [diff] [blame] | 2588 | assert(!CGM.getLangOpts().OpenMPUseTLS || |
| 2589 | !CGM.getContext().getTargetInfo().isTLSSupported()); |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2590 | // Lookup the entry, lazily creating it if necessary. |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 2591 | std::string Suffix = getName({"cache", ""}); |
| 2592 | return getOrCreateInternalVariable( |
| 2593 | CGM.Int8PtrPtrTy, Twine(CGM.getMangledName(VD)).concat(Suffix)); |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2594 | } |
| 2595 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2596 | Address CGOpenMPRuntime::getAddrOfThreadPrivate(CodeGenFunction &CGF, |
| 2597 | const VarDecl *VD, |
| 2598 | Address VDAddr, |
| 2599 | SourceLocation Loc) { |
Samuel Antao | f8b5012 | 2015-07-13 22:54:53 +0000 | [diff] [blame] | 2600 | if (CGM.getLangOpts().OpenMPUseTLS && |
| 2601 | CGM.getContext().getTargetInfo().isTLSSupported()) |
| 2602 | return VDAddr; |
| 2603 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2604 | llvm::Type *VarTy = VDAddr.getElementType(); |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 2605 | llvm::Value *Args[] = {emitUpdateLocation(CGF, Loc), getThreadID(CGF, Loc), |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2606 | CGF.Builder.CreatePointerCast(VDAddr.getPointer(), |
| 2607 | CGM.Int8PtrTy), |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2608 | CGM.getSize(CGM.GetTargetTypeStoreSize(VarTy)), |
| 2609 | getOrCreateThreadPrivateCache(VD)}; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2610 | return Address(CGF.EmitRuntimeCall( |
| 2611 | createRuntimeFunction(OMPRTL__kmpc_threadprivate_cached), Args), |
| 2612 | VDAddr.getAlignment()); |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2613 | } |
| 2614 | |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 2615 | void CGOpenMPRuntime::emitThreadPrivateVarInit( |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2616 | CodeGenFunction &CGF, Address VDAddr, llvm::Value *Ctor, |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2617 | llvm::Value *CopyCtor, llvm::Value *Dtor, SourceLocation Loc) { |
| 2618 | // Call kmp_int32 __kmpc_global_thread_num(&loc) to init OpenMP runtime |
| 2619 | // library. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2620 | llvm::Value *OMPLoc = emitUpdateLocation(CGF, Loc); |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 2621 | CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_global_thread_num), |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2622 | OMPLoc); |
| 2623 | // Call __kmpc_threadprivate_register(&loc, &var, ctor, cctor/*NULL*/, dtor) |
| 2624 | // to register constructor/destructor for variable. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2625 | llvm::Value *Args[] = { |
| 2626 | OMPLoc, CGF.Builder.CreatePointerCast(VDAddr.getPointer(), CGM.VoidPtrTy), |
| 2627 | Ctor, CopyCtor, Dtor}; |
Alexey Bataev | 1e4b713 | 2014-12-03 12:11:24 +0000 | [diff] [blame] | 2628 | CGF.EmitRuntimeCall( |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 2629 | createRuntimeFunction(OMPRTL__kmpc_threadprivate_register), Args); |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2630 | } |
| 2631 | |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 2632 | llvm::Function *CGOpenMPRuntime::emitThreadPrivateVarDefinition( |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2633 | const VarDecl *VD, Address VDAddr, SourceLocation Loc, |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2634 | bool PerformInit, CodeGenFunction *CGF) { |
Samuel Antao | f8b5012 | 2015-07-13 22:54:53 +0000 | [diff] [blame] | 2635 | if (CGM.getLangOpts().OpenMPUseTLS && |
| 2636 | CGM.getContext().getTargetInfo().isTLSSupported()) |
| 2637 | return nullptr; |
| 2638 | |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2639 | VD = VD->getDefinition(CGM.getContext()); |
Alexey Bataev | 2a6f3f5 | 2018-11-07 19:11:14 +0000 | [diff] [blame] | 2640 | if (VD && ThreadPrivateWithDefinition.insert(CGM.getMangledName(VD)).second) { |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2641 | QualType ASTTy = VD->getType(); |
| 2642 | |
| 2643 | llvm::Value *Ctor = nullptr, *CopyCtor = nullptr, *Dtor = nullptr; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2644 | const Expr *Init = VD->getAnyInitializer(); |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2645 | if (CGM.getLangOpts().CPlusPlus && PerformInit) { |
| 2646 | // Generate function that re-emits the declaration's initializer into the |
| 2647 | // threadprivate copy of the variable VD |
| 2648 | CodeGenFunction CtorCGF(CGM); |
| 2649 | FunctionArgList Args; |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 2650 | ImplicitParamDecl Dst(CGM.getContext(), /*DC=*/nullptr, Loc, |
| 2651 | /*Id=*/nullptr, CGM.getContext().VoidPtrTy, |
Alexey Bataev | 5622323 | 2017-06-09 13:40:18 +0000 | [diff] [blame] | 2652 | ImplicitParamDecl::Other); |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2653 | Args.push_back(&Dst); |
| 2654 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2655 | const auto &FI = CGM.getTypes().arrangeBuiltinFunctionDeclaration( |
John McCall | c56a8b3 | 2016-03-11 04:30:31 +0000 | [diff] [blame] | 2656 | CGM.getContext().VoidPtrTy, Args); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2657 | llvm::FunctionType *FTy = CGM.getTypes().GetFunctionType(FI); |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 2658 | std::string Name = getName({"__kmpc_global_ctor_", ""}); |
| 2659 | llvm::Function *Fn = |
| 2660 | CGM.CreateGlobalInitOrDestructFunction(FTy, Name, FI, Loc); |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2661 | CtorCGF.StartFunction(GlobalDecl(), CGM.getContext().VoidPtrTy, Fn, FI, |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 2662 | Args, Loc, Loc); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2663 | llvm::Value *ArgVal = CtorCGF.EmitLoadOfScalar( |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2664 | CtorCGF.GetAddrOfLocalVar(&Dst), /*Volatile=*/false, |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2665 | CGM.getContext().VoidPtrTy, Dst.getLocation()); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2666 | Address Arg = Address(ArgVal, VDAddr.getAlignment()); |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 2667 | Arg = CtorCGF.Builder.CreateElementBitCast( |
| 2668 | Arg, CtorCGF.ConvertTypeForMem(ASTTy)); |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2669 | CtorCGF.EmitAnyExprToMem(Init, Arg, Init->getType().getQualifiers(), |
| 2670 | /*IsInitializer=*/true); |
| 2671 | ArgVal = CtorCGF.EmitLoadOfScalar( |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2672 | CtorCGF.GetAddrOfLocalVar(&Dst), /*Volatile=*/false, |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2673 | CGM.getContext().VoidPtrTy, Dst.getLocation()); |
| 2674 | CtorCGF.Builder.CreateStore(ArgVal, CtorCGF.ReturnValue); |
| 2675 | CtorCGF.FinishFunction(); |
| 2676 | Ctor = Fn; |
| 2677 | } |
| 2678 | if (VD->getType().isDestructedType() != QualType::DK_none) { |
| 2679 | // Generate function that emits destructor call for the threadprivate copy |
| 2680 | // of the variable VD |
| 2681 | CodeGenFunction DtorCGF(CGM); |
| 2682 | FunctionArgList Args; |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 2683 | ImplicitParamDecl Dst(CGM.getContext(), /*DC=*/nullptr, Loc, |
| 2684 | /*Id=*/nullptr, CGM.getContext().VoidPtrTy, |
Alexey Bataev | 5622323 | 2017-06-09 13:40:18 +0000 | [diff] [blame] | 2685 | ImplicitParamDecl::Other); |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2686 | Args.push_back(&Dst); |
| 2687 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2688 | const auto &FI = CGM.getTypes().arrangeBuiltinFunctionDeclaration( |
John McCall | c56a8b3 | 2016-03-11 04:30:31 +0000 | [diff] [blame] | 2689 | CGM.getContext().VoidTy, Args); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2690 | llvm::FunctionType *FTy = CGM.getTypes().GetFunctionType(FI); |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 2691 | std::string Name = getName({"__kmpc_global_dtor_", ""}); |
| 2692 | llvm::Function *Fn = |
| 2693 | CGM.CreateGlobalInitOrDestructFunction(FTy, Name, FI, Loc); |
Adrian Prantl | 1858c66 | 2016-04-24 22:22:29 +0000 | [diff] [blame] | 2694 | auto NL = ApplyDebugLocation::CreateEmpty(DtorCGF); |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2695 | DtorCGF.StartFunction(GlobalDecl(), CGM.getContext().VoidTy, Fn, FI, Args, |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 2696 | Loc, Loc); |
Adrian Prantl | 1858c66 | 2016-04-24 22:22:29 +0000 | [diff] [blame] | 2697 | // Create a scope with an artificial location for the body of this function. |
| 2698 | auto AL = ApplyDebugLocation::CreateArtificial(DtorCGF); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2699 | llvm::Value *ArgVal = DtorCGF.EmitLoadOfScalar( |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2700 | DtorCGF.GetAddrOfLocalVar(&Dst), |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2701 | /*Volatile=*/false, CGM.getContext().VoidPtrTy, Dst.getLocation()); |
| 2702 | DtorCGF.emitDestroy(Address(ArgVal, VDAddr.getAlignment()), ASTTy, |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2703 | DtorCGF.getDestroyer(ASTTy.isDestructedType()), |
| 2704 | DtorCGF.needsEHCleanup(ASTTy.isDestructedType())); |
| 2705 | DtorCGF.FinishFunction(); |
| 2706 | Dtor = Fn; |
| 2707 | } |
| 2708 | // Do not emit init function if it is not required. |
| 2709 | if (!Ctor && !Dtor) |
| 2710 | return nullptr; |
| 2711 | |
| 2712 | llvm::Type *CopyCtorTyArgs[] = {CGM.VoidPtrTy, CGM.VoidPtrTy}; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2713 | auto *CopyCtorTy = llvm::FunctionType::get(CGM.VoidPtrTy, CopyCtorTyArgs, |
| 2714 | /*isVarArg=*/false) |
| 2715 | ->getPointerTo(); |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2716 | // Copying constructor for the threadprivate variable. |
| 2717 | // Must be NULL - reserved by runtime, but currently it requires that this |
| 2718 | // parameter is always NULL. Otherwise it fires assertion. |
| 2719 | CopyCtor = llvm::Constant::getNullValue(CopyCtorTy); |
| 2720 | if (Ctor == nullptr) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2721 | auto *CtorTy = llvm::FunctionType::get(CGM.VoidPtrTy, CGM.VoidPtrTy, |
| 2722 | /*isVarArg=*/false) |
| 2723 | ->getPointerTo(); |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2724 | Ctor = llvm::Constant::getNullValue(CtorTy); |
| 2725 | } |
| 2726 | if (Dtor == nullptr) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2727 | auto *DtorTy = llvm::FunctionType::get(CGM.VoidTy, CGM.VoidPtrTy, |
| 2728 | /*isVarArg=*/false) |
| 2729 | ->getPointerTo(); |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2730 | Dtor = llvm::Constant::getNullValue(DtorTy); |
| 2731 | } |
| 2732 | if (!CGF) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2733 | auto *InitFunctionTy = |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2734 | llvm::FunctionType::get(CGM.VoidTy, /*isVarArg*/ false); |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 2735 | std::string Name = getName({"__omp_threadprivate_init_", ""}); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2736 | llvm::Function *InitFunction = CGM.CreateGlobalInitOrDestructFunction( |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 2737 | InitFunctionTy, Name, CGM.getTypes().arrangeNullaryFunction()); |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2738 | CodeGenFunction InitCGF(CGM); |
| 2739 | FunctionArgList ArgList; |
| 2740 | InitCGF.StartFunction(GlobalDecl(), CGM.getContext().VoidTy, InitFunction, |
| 2741 | CGM.getTypes().arrangeNullaryFunction(), ArgList, |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 2742 | Loc, Loc); |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 2743 | emitThreadPrivateVarInit(InitCGF, VDAddr, Ctor, CopyCtor, Dtor, Loc); |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2744 | InitCGF.FinishFunction(); |
| 2745 | return InitFunction; |
| 2746 | } |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 2747 | emitThreadPrivateVarInit(*CGF, VDAddr, Ctor, CopyCtor, Dtor, Loc); |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 2748 | } |
| 2749 | return nullptr; |
| 2750 | } |
| 2751 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2752 | /// Obtain information that uniquely identifies a target entry. This |
Alexey Bataev | 34f8a70 | 2018-03-28 14:28:54 +0000 | [diff] [blame] | 2753 | /// consists of the file and device IDs as well as line number associated with |
| 2754 | /// the relevant entry source location. |
| 2755 | static void getTargetEntryUniqueInfo(ASTContext &C, SourceLocation Loc, |
| 2756 | unsigned &DeviceID, unsigned &FileID, |
| 2757 | unsigned &LineNum) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2758 | SourceManager &SM = C.getSourceManager(); |
Alexey Bataev | 34f8a70 | 2018-03-28 14:28:54 +0000 | [diff] [blame] | 2759 | |
| 2760 | // The loc should be always valid and have a file ID (the user cannot use |
| 2761 | // #pragma directives in macros) |
| 2762 | |
| 2763 | assert(Loc.isValid() && "Source location is expected to be always valid."); |
Alexey Bataev | 34f8a70 | 2018-03-28 14:28:54 +0000 | [diff] [blame] | 2764 | |
| 2765 | PresumedLoc PLoc = SM.getPresumedLoc(Loc); |
| 2766 | assert(PLoc.isValid() && "Source location is expected to be always valid."); |
| 2767 | |
| 2768 | llvm::sys::fs::UniqueID ID; |
Alexey Bataev | 64e62dc | 2018-04-30 16:26:57 +0000 | [diff] [blame] | 2769 | if (auto EC = llvm::sys::fs::getUniqueID(PLoc.getFilename(), ID)) |
| 2770 | SM.getDiagnostics().Report(diag::err_cannot_open_file) |
| 2771 | << PLoc.getFilename() << EC.message(); |
Alexey Bataev | 34f8a70 | 2018-03-28 14:28:54 +0000 | [diff] [blame] | 2772 | |
| 2773 | DeviceID = ID.getDevice(); |
| 2774 | FileID = ID.getFile(); |
| 2775 | LineNum = PLoc.getLine(); |
| 2776 | } |
| 2777 | |
| 2778 | bool CGOpenMPRuntime::emitDeclareTargetVarDefinition(const VarDecl *VD, |
| 2779 | llvm::GlobalVariable *Addr, |
| 2780 | bool PerformInit) { |
| 2781 | Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res = |
Alexey Bataev | 97b7221 | 2018-08-14 18:31:20 +0000 | [diff] [blame] | 2782 | OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD); |
Gheorghe-Teodor Bercea | 0034e84 | 2019-06-20 18:04:47 +0000 | [diff] [blame] | 2783 | if (!Res || *Res == OMPDeclareTargetDeclAttr::MT_Link || |
| 2784 | (*Res == OMPDeclareTargetDeclAttr::MT_To && |
| 2785 | HasRequiresUnifiedSharedMemory)) |
Alexey Bataev | d01b749 | 2018-08-15 19:45:12 +0000 | [diff] [blame] | 2786 | return CGM.getLangOpts().OpenMPIsDevice; |
Alexey Bataev | 34f8a70 | 2018-03-28 14:28:54 +0000 | [diff] [blame] | 2787 | VD = VD->getDefinition(CGM.getContext()); |
Alexey Bataev | 2a6f3f5 | 2018-11-07 19:11:14 +0000 | [diff] [blame] | 2788 | if (VD && !DeclareTargetWithDefinition.insert(CGM.getMangledName(VD)).second) |
Alexey Bataev | 34f8a70 | 2018-03-28 14:28:54 +0000 | [diff] [blame] | 2789 | return CGM.getLangOpts().OpenMPIsDevice; |
| 2790 | |
| 2791 | QualType ASTTy = VD->getType(); |
| 2792 | |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2793 | SourceLocation Loc = VD->getCanonicalDecl()->getBeginLoc(); |
Alexey Bataev | 34f8a70 | 2018-03-28 14:28:54 +0000 | [diff] [blame] | 2794 | // Produce the unique prefix to identify the new target regions. We use |
| 2795 | // the source location of the variable declaration which we know to not |
| 2796 | // conflict with any target region. |
| 2797 | unsigned DeviceID; |
| 2798 | unsigned FileID; |
| 2799 | unsigned Line; |
| 2800 | getTargetEntryUniqueInfo(CGM.getContext(), Loc, DeviceID, FileID, Line); |
| 2801 | SmallString<128> Buffer, Out; |
| 2802 | { |
| 2803 | llvm::raw_svector_ostream OS(Buffer); |
| 2804 | OS << "__omp_offloading_" << llvm::format("_%x", DeviceID) |
| 2805 | << llvm::format("_%x_", FileID) << VD->getName() << "_l" << Line; |
| 2806 | } |
| 2807 | |
| 2808 | const Expr *Init = VD->getAnyInitializer(); |
| 2809 | if (CGM.getLangOpts().CPlusPlus && PerformInit) { |
| 2810 | llvm::Constant *Ctor; |
| 2811 | llvm::Constant *ID; |
| 2812 | if (CGM.getLangOpts().OpenMPIsDevice) { |
| 2813 | // Generate function that re-emits the declaration's initializer into |
| 2814 | // the threadprivate copy of the variable VD |
| 2815 | CodeGenFunction CtorCGF(CGM); |
| 2816 | |
| 2817 | const CGFunctionInfo &FI = CGM.getTypes().arrangeNullaryFunction(); |
| 2818 | llvm::FunctionType *FTy = CGM.getTypes().GetFunctionType(FI); |
| 2819 | llvm::Function *Fn = CGM.CreateGlobalInitOrDestructFunction( |
| 2820 | FTy, Twine(Buffer, "_ctor"), FI, Loc); |
| 2821 | auto NL = ApplyDebugLocation::CreateEmpty(CtorCGF); |
| 2822 | CtorCGF.StartFunction(GlobalDecl(), CGM.getContext().VoidTy, Fn, FI, |
| 2823 | FunctionArgList(), Loc, Loc); |
| 2824 | auto AL = ApplyDebugLocation::CreateArtificial(CtorCGF); |
| 2825 | CtorCGF.EmitAnyExprToMem(Init, |
| 2826 | Address(Addr, CGM.getContext().getDeclAlign(VD)), |
| 2827 | Init->getType().getQualifiers(), |
| 2828 | /*IsInitializer=*/true); |
| 2829 | CtorCGF.FinishFunction(); |
| 2830 | Ctor = Fn; |
| 2831 | ID = llvm::ConstantExpr::getBitCast(Fn, CGM.Int8PtrTy); |
Alexey Bataev | e253f2f | 2018-05-09 14:15:18 +0000 | [diff] [blame] | 2832 | CGM.addUsedGlobal(cast<llvm::GlobalValue>(Ctor)); |
Alexey Bataev | 34f8a70 | 2018-03-28 14:28:54 +0000 | [diff] [blame] | 2833 | } else { |
| 2834 | Ctor = new llvm::GlobalVariable( |
| 2835 | CGM.getModule(), CGM.Int8Ty, /*isConstant=*/true, |
| 2836 | llvm::GlobalValue::PrivateLinkage, |
| 2837 | llvm::Constant::getNullValue(CGM.Int8Ty), Twine(Buffer, "_ctor")); |
| 2838 | ID = Ctor; |
| 2839 | } |
| 2840 | |
| 2841 | // Register the information for the entry associated with the constructor. |
| 2842 | Out.clear(); |
| 2843 | OffloadEntriesInfoManager.registerTargetRegionEntryInfo( |
| 2844 | DeviceID, FileID, Twine(Buffer, "_ctor").toStringRef(Out), Line, Ctor, |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 2845 | ID, OffloadEntriesInfoManagerTy::OMPTargetRegionEntryCtor); |
Alexey Bataev | 34f8a70 | 2018-03-28 14:28:54 +0000 | [diff] [blame] | 2846 | } |
| 2847 | if (VD->getType().isDestructedType() != QualType::DK_none) { |
| 2848 | llvm::Constant *Dtor; |
| 2849 | llvm::Constant *ID; |
| 2850 | if (CGM.getLangOpts().OpenMPIsDevice) { |
| 2851 | // Generate function that emits destructor call for the threadprivate |
| 2852 | // copy of the variable VD |
| 2853 | CodeGenFunction DtorCGF(CGM); |
| 2854 | |
| 2855 | const CGFunctionInfo &FI = CGM.getTypes().arrangeNullaryFunction(); |
| 2856 | llvm::FunctionType *FTy = CGM.getTypes().GetFunctionType(FI); |
| 2857 | llvm::Function *Fn = CGM.CreateGlobalInitOrDestructFunction( |
| 2858 | FTy, Twine(Buffer, "_dtor"), FI, Loc); |
| 2859 | auto NL = ApplyDebugLocation::CreateEmpty(DtorCGF); |
| 2860 | DtorCGF.StartFunction(GlobalDecl(), CGM.getContext().VoidTy, Fn, FI, |
| 2861 | FunctionArgList(), Loc, Loc); |
| 2862 | // Create a scope with an artificial location for the body of this |
| 2863 | // function. |
| 2864 | auto AL = ApplyDebugLocation::CreateArtificial(DtorCGF); |
| 2865 | DtorCGF.emitDestroy(Address(Addr, CGM.getContext().getDeclAlign(VD)), |
| 2866 | ASTTy, DtorCGF.getDestroyer(ASTTy.isDestructedType()), |
| 2867 | DtorCGF.needsEHCleanup(ASTTy.isDestructedType())); |
| 2868 | DtorCGF.FinishFunction(); |
| 2869 | Dtor = Fn; |
| 2870 | ID = llvm::ConstantExpr::getBitCast(Fn, CGM.Int8PtrTy); |
Alexey Bataev | e253f2f | 2018-05-09 14:15:18 +0000 | [diff] [blame] | 2871 | CGM.addUsedGlobal(cast<llvm::GlobalValue>(Dtor)); |
Alexey Bataev | 34f8a70 | 2018-03-28 14:28:54 +0000 | [diff] [blame] | 2872 | } else { |
| 2873 | Dtor = new llvm::GlobalVariable( |
| 2874 | CGM.getModule(), CGM.Int8Ty, /*isConstant=*/true, |
| 2875 | llvm::GlobalValue::PrivateLinkage, |
| 2876 | llvm::Constant::getNullValue(CGM.Int8Ty), Twine(Buffer, "_dtor")); |
| 2877 | ID = Dtor; |
| 2878 | } |
| 2879 | // Register the information for the entry associated with the destructor. |
| 2880 | Out.clear(); |
| 2881 | OffloadEntriesInfoManager.registerTargetRegionEntryInfo( |
| 2882 | DeviceID, FileID, Twine(Buffer, "_dtor").toStringRef(Out), Line, Dtor, |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 2883 | ID, OffloadEntriesInfoManagerTy::OMPTargetRegionEntryDtor); |
Alexey Bataev | 34f8a70 | 2018-03-28 14:28:54 +0000 | [diff] [blame] | 2884 | } |
| 2885 | return CGM.getLangOpts().OpenMPIsDevice; |
| 2886 | } |
| 2887 | |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 2888 | Address CGOpenMPRuntime::getAddrOfArtificialThreadPrivate(CodeGenFunction &CGF, |
| 2889 | QualType VarType, |
| 2890 | StringRef Name) { |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 2891 | std::string Suffix = getName({"artificial", ""}); |
| 2892 | std::string CacheSuffix = getName({"cache", ""}); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 2893 | llvm::Type *VarLVType = CGF.ConvertTypeForMem(VarType); |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 2894 | llvm::Value *GAddr = |
| 2895 | getOrCreateInternalVariable(VarLVType, Twine(Name).concat(Suffix)); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 2896 | llvm::Value *Args[] = { |
| 2897 | emitUpdateLocation(CGF, SourceLocation()), |
| 2898 | getThreadID(CGF, SourceLocation()), |
| 2899 | CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(GAddr, CGM.VoidPtrTy), |
| 2900 | CGF.Builder.CreateIntCast(CGF.getTypeSize(VarType), CGM.SizeTy, |
| 2901 | /*IsSigned=*/false), |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 2902 | getOrCreateInternalVariable( |
| 2903 | CGM.VoidPtrPtrTy, Twine(Name).concat(Suffix).concat(CacheSuffix))}; |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 2904 | return Address( |
| 2905 | CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
| 2906 | CGF.EmitRuntimeCall( |
| 2907 | createRuntimeFunction(OMPRTL__kmpc_threadprivate_cached), Args), |
| 2908 | VarLVType->getPointerTo(/*AddrSpace=*/0)), |
| 2909 | CGM.getPointerAlign()); |
| 2910 | } |
| 2911 | |
Arpith Chacko Jacob | bb36fe8 | 2017-01-10 15:42:51 +0000 | [diff] [blame] | 2912 | void CGOpenMPRuntime::emitOMPIfClause(CodeGenFunction &CGF, const Expr *Cond, |
| 2913 | const RegionCodeGenTy &ThenGen, |
| 2914 | const RegionCodeGenTy &ElseGen) { |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 2915 | CodeGenFunction::LexicalScope ConditionScope(CGF, Cond->getSourceRange()); |
| 2916 | |
| 2917 | // If the condition constant folds and can be elided, try to avoid emitting |
| 2918 | // the condition and the dead arm of the if/else. |
| 2919 | bool CondConstant; |
| 2920 | if (CGF.ConstantFoldsToSimpleInteger(Cond, CondConstant)) { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2921 | if (CondConstant) |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 2922 | ThenGen(CGF); |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2923 | else |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 2924 | ElseGen(CGF); |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 2925 | return; |
| 2926 | } |
| 2927 | |
| 2928 | // Otherwise, the condition did not fold, or we couldn't elide it. Just |
| 2929 | // emit the conditional branch. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2930 | llvm::BasicBlock *ThenBlock = CGF.createBasicBlock("omp_if.then"); |
| 2931 | llvm::BasicBlock *ElseBlock = CGF.createBasicBlock("omp_if.else"); |
| 2932 | llvm::BasicBlock *ContBlock = CGF.createBasicBlock("omp_if.end"); |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 2933 | CGF.EmitBranchOnBoolExpr(Cond, ThenBlock, ElseBlock, /*TrueCount=*/0); |
| 2934 | |
| 2935 | // Emit the 'then' code. |
| 2936 | CGF.EmitBlock(ThenBlock); |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2937 | ThenGen(CGF); |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 2938 | CGF.EmitBranch(ContBlock); |
| 2939 | // Emit the 'else' code if present. |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2940 | // There is no need to emit line number for unconditional branch. |
| 2941 | (void)ApplyDebugLocation::CreateEmpty(CGF); |
| 2942 | CGF.EmitBlock(ElseBlock); |
| 2943 | ElseGen(CGF); |
| 2944 | // There is no need to emit line number for unconditional branch. |
| 2945 | (void)ApplyDebugLocation::CreateEmpty(CGF); |
| 2946 | CGF.EmitBranch(ContBlock); |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 2947 | // Emit the continuation block for code after the if. |
| 2948 | CGF.EmitBlock(ContBlock, /*IsFinished=*/true); |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 2949 | } |
| 2950 | |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 2951 | void CGOpenMPRuntime::emitParallelCall(CodeGenFunction &CGF, SourceLocation Loc, |
James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 2952 | llvm::Function *OutlinedFn, |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 2953 | ArrayRef<llvm::Value *> CapturedVars, |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 2954 | const Expr *IfCond) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 2955 | if (!CGF.HaveInsertPoint()) |
| 2956 | return; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2957 | llvm::Value *RTLoc = emitUpdateLocation(CGF, Loc); |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2958 | auto &&ThenGen = [OutlinedFn, CapturedVars, RTLoc](CodeGenFunction &CGF, |
| 2959 | PrePostActionTy &) { |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 2960 | // Build call __kmpc_fork_call(loc, n, microtask, var1, .., varn); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2961 | CGOpenMPRuntime &RT = CGF.CGM.getOpenMPRuntime(); |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 2962 | llvm::Value *Args[] = { |
| 2963 | RTLoc, |
| 2964 | CGF.Builder.getInt32(CapturedVars.size()), // Number of captured vars |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2965 | CGF.Builder.CreateBitCast(OutlinedFn, RT.getKmpc_MicroPointerTy())}; |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 2966 | llvm::SmallVector<llvm::Value *, 16> RealArgs; |
| 2967 | RealArgs.append(std::begin(Args), std::end(Args)); |
| 2968 | RealArgs.append(CapturedVars.begin(), CapturedVars.end()); |
| 2969 | |
James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 2970 | llvm::FunctionCallee RTLFn = |
| 2971 | RT.createRuntimeFunction(OMPRTL__kmpc_fork_call); |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 2972 | CGF.EmitRuntimeCall(RTLFn, RealArgs); |
| 2973 | }; |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2974 | auto &&ElseGen = [OutlinedFn, CapturedVars, RTLoc, Loc](CodeGenFunction &CGF, |
| 2975 | PrePostActionTy &) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 2976 | CGOpenMPRuntime &RT = CGF.CGM.getOpenMPRuntime(); |
| 2977 | llvm::Value *ThreadID = RT.getThreadID(CGF, Loc); |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 2978 | // Build calls: |
| 2979 | // __kmpc_serialized_parallel(&Loc, GTid); |
| 2980 | llvm::Value *Args[] = {RTLoc, ThreadID}; |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2981 | CGF.EmitRuntimeCall( |
| 2982 | RT.createRuntimeFunction(OMPRTL__kmpc_serialized_parallel), Args); |
Alexey Bataev | d74d060 | 2014-10-13 06:02:40 +0000 | [diff] [blame] | 2983 | |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 2984 | // OutlinedFn(>id, &zero, CapturedStruct); |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 2985 | Address ZeroAddr = CGF.CreateDefaultAlignTempAlloca(CGF.Int32Ty, |
| 2986 | /*Name*/ ".zero.addr"); |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 2987 | CGF.InitTempAlloca(ZeroAddr, CGF.Builder.getInt32(/*C*/ 0)); |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 2988 | llvm::SmallVector<llvm::Value *, 16> OutlinedFnArgs; |
Alexey Bataev | 8521ff6 | 2018-07-25 20:03:01 +0000 | [diff] [blame] | 2989 | // ThreadId for serialized parallels is 0. |
| 2990 | OutlinedFnArgs.push_back(ZeroAddr.getPointer()); |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 2991 | OutlinedFnArgs.push_back(ZeroAddr.getPointer()); |
| 2992 | OutlinedFnArgs.append(CapturedVars.begin(), CapturedVars.end()); |
Alexey Bataev | 3c595a6 | 2017-08-14 15:01:03 +0000 | [diff] [blame] | 2993 | RT.emitOutlinedFunctionCall(CGF, Loc, OutlinedFn, OutlinedFnArgs); |
Alexey Bataev | d74d060 | 2014-10-13 06:02:40 +0000 | [diff] [blame] | 2994 | |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 2995 | // __kmpc_end_serialized_parallel(&Loc, GTid); |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2996 | llvm::Value *EndArgs[] = {RT.emitUpdateLocation(CGF, Loc), ThreadID}; |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 2997 | CGF.EmitRuntimeCall( |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2998 | RT.createRuntimeFunction(OMPRTL__kmpc_end_serialized_parallel), |
| 2999 | EndArgs); |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 3000 | }; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3001 | if (IfCond) { |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 3002 | emitOMPIfClause(CGF, IfCond, ThenGen, ElseGen); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3003 | } else { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 3004 | RegionCodeGenTy ThenRCG(ThenGen); |
| 3005 | ThenRCG(CGF); |
Alexey Bataev | f539faa | 2016-03-28 12:58:34 +0000 | [diff] [blame] | 3006 | } |
Alexey Bataev | d74d060 | 2014-10-13 06:02:40 +0000 | [diff] [blame] | 3007 | } |
| 3008 | |
NAKAMURA Takumi | 59c74b22 | 2014-10-27 08:08:18 +0000 | [diff] [blame] | 3009 | // 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] | 3010 | // thread-ID variable (it is passed in a first argument of the outlined function |
| 3011 | // as "kmp_int32 *gtid"). Otherwise, if we're not inside parallel region, but in |
| 3012 | // regular serial code region, get thread ID by calling kmp_int32 |
| 3013 | // kmpc_global_thread_num(ident_t *loc), stash this thread ID in a temporary and |
| 3014 | // return the address of that temp. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3015 | Address CGOpenMPRuntime::emitThreadIDAddress(CodeGenFunction &CGF, |
| 3016 | SourceLocation Loc) { |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 3017 | if (auto *OMPRegionInfo = |
Alexey Bataev | d74d060 | 2014-10-13 06:02:40 +0000 | [diff] [blame] | 3018 | dyn_cast_or_null<CGOpenMPRegionInfo>(CGF.CapturedStmtInfo)) |
Alexey Bataev | 8cbe0a6 | 2015-02-26 10:27:34 +0000 | [diff] [blame] | 3019 | if (OMPRegionInfo->getThreadIDVariable()) |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 3020 | return OMPRegionInfo->getThreadIDVariableLValue(CGF).getAddress(); |
Alexey Bataev | 8cbe0a6 | 2015-02-26 10:27:34 +0000 | [diff] [blame] | 3021 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3022 | llvm::Value *ThreadID = getThreadID(CGF, Loc); |
| 3023 | QualType Int32Ty = |
Alexey Bataev | d74d060 | 2014-10-13 06:02:40 +0000 | [diff] [blame] | 3024 | CGF.getContext().getIntTypeForBitwidth(/*DestWidth*/ 32, /*Signed*/ true); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3025 | Address ThreadIDTemp = CGF.CreateMemTemp(Int32Ty, /*Name*/ ".threadid_temp."); |
Alexey Bataev | d74d060 | 2014-10-13 06:02:40 +0000 | [diff] [blame] | 3026 | CGF.EmitStoreOfScalar(ThreadID, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3027 | CGF.MakeAddrLValue(ThreadIDTemp, Int32Ty)); |
Alexey Bataev | d74d060 | 2014-10-13 06:02:40 +0000 | [diff] [blame] | 3028 | |
| 3029 | return ThreadIDTemp; |
| 3030 | } |
| 3031 | |
Alexey Bataev | 1af5bd5 | 2019-03-05 17:47:18 +0000 | [diff] [blame] | 3032 | llvm::Constant *CGOpenMPRuntime::getOrCreateInternalVariable( |
| 3033 | llvm::Type *Ty, const llvm::Twine &Name, unsigned AddressSpace) { |
Alexey Bataev | 3a3bf0b | 2014-09-22 10:01:53 +0000 | [diff] [blame] | 3034 | SmallString<256> Buffer; |
| 3035 | llvm::raw_svector_ostream Out(Buffer); |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 3036 | Out << Name; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3037 | StringRef RuntimeName = Out.str(); |
Alexey Bataev | 43a919f | 2018-04-13 17:48:43 +0000 | [diff] [blame] | 3038 | auto &Elem = *InternalVars.try_emplace(RuntimeName, nullptr).first; |
David Blaikie | 13156b6 | 2014-11-19 03:06:06 +0000 | [diff] [blame] | 3039 | if (Elem.second) { |
| 3040 | assert(Elem.second->getType()->getPointerElementType() == Ty && |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 3041 | "OMP internal variable has different type than requested"); |
David Blaikie | 13156b6 | 2014-11-19 03:06:06 +0000 | [diff] [blame] | 3042 | return &*Elem.second; |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 3043 | } |
Alexey Bataev | 3a3bf0b | 2014-09-22 10:01:53 +0000 | [diff] [blame] | 3044 | |
David Blaikie | 13156b6 | 2014-11-19 03:06:06 +0000 | [diff] [blame] | 3045 | return Elem.second = new llvm::GlobalVariable( |
| 3046 | CGM.getModule(), Ty, /*IsConstant*/ false, |
| 3047 | llvm::GlobalValue::CommonLinkage, llvm::Constant::getNullValue(Ty), |
Alexey Bataev | 1af5bd5 | 2019-03-05 17:47:18 +0000 | [diff] [blame] | 3048 | Elem.first(), /*InsertBefore=*/nullptr, |
| 3049 | llvm::GlobalValue::NotThreadLocal, AddressSpace); |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 3050 | } |
| 3051 | |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 3052 | llvm::Value *CGOpenMPRuntime::getCriticalRegionLock(StringRef CriticalName) { |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 3053 | std::string Prefix = Twine("gomp_critical_user_", CriticalName).str(); |
| 3054 | std::string Name = getName({Prefix, "var"}); |
| 3055 | return getOrCreateInternalVariable(KmpCriticalNameTy, Name); |
Alexey Bataev | 3a3bf0b | 2014-09-22 10:01:53 +0000 | [diff] [blame] | 3056 | } |
| 3057 | |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 3058 | namespace { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 3059 | /// Common pre(post)-action for different OpenMP constructs. |
| 3060 | class CommonActionTy final : public PrePostActionTy { |
James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 3061 | llvm::FunctionCallee EnterCallee; |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 3062 | ArrayRef<llvm::Value *> EnterArgs; |
James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 3063 | llvm::FunctionCallee ExitCallee; |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 3064 | ArrayRef<llvm::Value *> ExitArgs; |
| 3065 | bool Conditional; |
| 3066 | llvm::BasicBlock *ContBlock = nullptr; |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 3067 | |
| 3068 | public: |
James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 3069 | CommonActionTy(llvm::FunctionCallee EnterCallee, |
| 3070 | ArrayRef<llvm::Value *> EnterArgs, |
| 3071 | llvm::FunctionCallee ExitCallee, |
| 3072 | ArrayRef<llvm::Value *> ExitArgs, bool Conditional = false) |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 3073 | : EnterCallee(EnterCallee), EnterArgs(EnterArgs), ExitCallee(ExitCallee), |
| 3074 | ExitArgs(ExitArgs), Conditional(Conditional) {} |
| 3075 | void Enter(CodeGenFunction &CGF) override { |
| 3076 | llvm::Value *EnterRes = CGF.EmitRuntimeCall(EnterCallee, EnterArgs); |
| 3077 | if (Conditional) { |
| 3078 | llvm::Value *CallBool = CGF.Builder.CreateIsNotNull(EnterRes); |
| 3079 | auto *ThenBlock = CGF.createBasicBlock("omp_if.then"); |
| 3080 | ContBlock = CGF.createBasicBlock("omp_if.end"); |
| 3081 | // Generate the branch (If-stmt) |
| 3082 | CGF.Builder.CreateCondBr(CallBool, ThenBlock, ContBlock); |
| 3083 | CGF.EmitBlock(ThenBlock); |
| 3084 | } |
Alexey Bataev | a744ff5 | 2015-05-05 09:24:37 +0000 | [diff] [blame] | 3085 | } |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 3086 | void Done(CodeGenFunction &CGF) { |
| 3087 | // Emit the rest of blocks/branches |
| 3088 | CGF.EmitBranch(ContBlock); |
| 3089 | CGF.EmitBlock(ContBlock, true); |
| 3090 | } |
| 3091 | void Exit(CodeGenFunction &CGF) override { |
| 3092 | CGF.EmitRuntimeCall(ExitCallee, ExitArgs); |
Alexey Bataev | 3e6124b | 2015-04-10 07:48:12 +0000 | [diff] [blame] | 3093 | } |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 3094 | }; |
Hans Wennborg | 7eb5464 | 2015-09-10 17:07:54 +0000 | [diff] [blame] | 3095 | } // anonymous namespace |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 3096 | |
| 3097 | void CGOpenMPRuntime::emitCriticalRegion(CodeGenFunction &CGF, |
| 3098 | StringRef CriticalName, |
| 3099 | const RegionCodeGenTy &CriticalOpGen, |
Alexey Bataev | fc57d16 | 2015-12-15 10:55:09 +0000 | [diff] [blame] | 3100 | SourceLocation Loc, const Expr *Hint) { |
| 3101 | // __kmpc_critical[_with_hint](ident_t *, gtid, Lock[, hint]); |
Alexey Bataev | 75ddfab | 2014-12-01 11:32:38 +0000 | [diff] [blame] | 3102 | // CriticalOpGen(); |
| 3103 | // __kmpc_end_critical(ident_t *, gtid, Lock); |
| 3104 | // Prepare arguments and build a call to __kmpc_critical |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 3105 | if (!CGF.HaveInsertPoint()) |
| 3106 | return; |
Alexey Bataev | fc57d16 | 2015-12-15 10:55:09 +0000 | [diff] [blame] | 3107 | llvm::Value *Args[] = {emitUpdateLocation(CGF, Loc), getThreadID(CGF, Loc), |
| 3108 | getCriticalRegionLock(CriticalName)}; |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 3109 | llvm::SmallVector<llvm::Value *, 4> EnterArgs(std::begin(Args), |
| 3110 | std::end(Args)); |
Alexey Bataev | fc57d16 | 2015-12-15 10:55:09 +0000 | [diff] [blame] | 3111 | if (Hint) { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 3112 | EnterArgs.push_back(CGF.Builder.CreateIntCast( |
| 3113 | CGF.EmitScalarExpr(Hint), CGM.IntPtrTy, /*isSigned=*/false)); |
| 3114 | } |
| 3115 | CommonActionTy Action( |
| 3116 | createRuntimeFunction(Hint ? OMPRTL__kmpc_critical_with_hint |
| 3117 | : OMPRTL__kmpc_critical), |
| 3118 | EnterArgs, createRuntimeFunction(OMPRTL__kmpc_end_critical), Args); |
| 3119 | CriticalOpGen.setAction(Action); |
Alexey Bataev | fc57d16 | 2015-12-15 10:55:09 +0000 | [diff] [blame] | 3120 | emitInlinedDirective(CGF, OMPD_critical, CriticalOpGen); |
Alexey Bataev | 3a3bf0b | 2014-09-22 10:01:53 +0000 | [diff] [blame] | 3121 | } |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 3122 | |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 3123 | void CGOpenMPRuntime::emitMasterRegion(CodeGenFunction &CGF, |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 3124 | const RegionCodeGenTy &MasterOpGen, |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 3125 | SourceLocation Loc) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 3126 | if (!CGF.HaveInsertPoint()) |
| 3127 | return; |
Alexey Bataev | 8d69065 | 2014-12-04 07:23:53 +0000 | [diff] [blame] | 3128 | // if(__kmpc_master(ident_t *, gtid)) { |
| 3129 | // MasterOpGen(); |
| 3130 | // __kmpc_end_master(ident_t *, gtid); |
| 3131 | // } |
| 3132 | // Prepare arguments and build a call to __kmpc_master |
Alexey Bataev | d7614fb | 2015-04-10 06:33:45 +0000 | [diff] [blame] | 3133 | llvm::Value *Args[] = {emitUpdateLocation(CGF, Loc), getThreadID(CGF, Loc)}; |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 3134 | CommonActionTy Action(createRuntimeFunction(OMPRTL__kmpc_master), Args, |
| 3135 | createRuntimeFunction(OMPRTL__kmpc_end_master), Args, |
| 3136 | /*Conditional=*/true); |
| 3137 | MasterOpGen.setAction(Action); |
| 3138 | emitInlinedDirective(CGF, OMPD_master, MasterOpGen); |
| 3139 | Action.Done(CGF); |
Alexey Bataev | 8d69065 | 2014-12-04 07:23:53 +0000 | [diff] [blame] | 3140 | } |
| 3141 | |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 3142 | void CGOpenMPRuntime::emitTaskyieldCall(CodeGenFunction &CGF, |
| 3143 | SourceLocation Loc) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 3144 | if (!CGF.HaveInsertPoint()) |
| 3145 | return; |
Alexey Bataev | 9f797f3 | 2015-02-05 05:57:51 +0000 | [diff] [blame] | 3146 | // Build call __kmpc_omp_taskyield(loc, thread_id, 0); |
| 3147 | llvm::Value *Args[] = { |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 3148 | emitUpdateLocation(CGF, Loc), getThreadID(CGF, Loc), |
Alexey Bataev | 9f797f3 | 2015-02-05 05:57:51 +0000 | [diff] [blame] | 3149 | llvm::ConstantInt::get(CGM.IntTy, /*V=*/0, /*isSigned=*/true)}; |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 3150 | CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_omp_taskyield), Args); |
Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 3151 | if (auto *Region = dyn_cast_or_null<CGOpenMPRegionInfo>(CGF.CapturedStmtInfo)) |
| 3152 | Region->emitUntiedSwitch(CGF); |
Alexey Bataev | 9f797f3 | 2015-02-05 05:57:51 +0000 | [diff] [blame] | 3153 | } |
| 3154 | |
Alexey Bataev | c30dd2d | 2015-06-18 12:14:09 +0000 | [diff] [blame] | 3155 | void CGOpenMPRuntime::emitTaskgroupRegion(CodeGenFunction &CGF, |
| 3156 | const RegionCodeGenTy &TaskgroupOpGen, |
| 3157 | SourceLocation Loc) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 3158 | if (!CGF.HaveInsertPoint()) |
| 3159 | return; |
Alexey Bataev | c30dd2d | 2015-06-18 12:14:09 +0000 | [diff] [blame] | 3160 | // __kmpc_taskgroup(ident_t *, gtid); |
| 3161 | // TaskgroupOpGen(); |
| 3162 | // __kmpc_end_taskgroup(ident_t *, gtid); |
| 3163 | // Prepare arguments and build a call to __kmpc_taskgroup |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 3164 | llvm::Value *Args[] = {emitUpdateLocation(CGF, Loc), getThreadID(CGF, Loc)}; |
| 3165 | CommonActionTy Action(createRuntimeFunction(OMPRTL__kmpc_taskgroup), Args, |
| 3166 | createRuntimeFunction(OMPRTL__kmpc_end_taskgroup), |
| 3167 | Args); |
| 3168 | TaskgroupOpGen.setAction(Action); |
| 3169 | emitInlinedDirective(CGF, OMPD_taskgroup, TaskgroupOpGen); |
Alexey Bataev | c30dd2d | 2015-06-18 12:14:09 +0000 | [diff] [blame] | 3170 | } |
| 3171 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3172 | /// Given an array of pointers to variables, project the address of a |
| 3173 | /// given variable. |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 3174 | static Address emitAddrOfVarFromArray(CodeGenFunction &CGF, Address Array, |
| 3175 | unsigned Index, const VarDecl *Var) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3176 | // Pull out the pointer to the variable. |
James Y Knight | 751fe28 | 2019-02-09 22:22:28 +0000 | [diff] [blame] | 3177 | Address PtrAddr = CGF.Builder.CreateConstArrayGEP(Array, Index); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3178 | llvm::Value *Ptr = CGF.Builder.CreateLoad(PtrAddr); |
| 3179 | |
| 3180 | Address Addr = Address(Ptr, CGF.getContext().getDeclAlign(Var)); |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 3181 | Addr = CGF.Builder.CreateElementBitCast( |
| 3182 | Addr, CGF.ConvertTypeForMem(Var->getType())); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3183 | return Addr; |
| 3184 | } |
| 3185 | |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3186 | static llvm::Value *emitCopyprivateCopyFunction( |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 3187 | CodeGenModule &CGM, llvm::Type *ArgsType, |
| 3188 | ArrayRef<const Expr *> CopyprivateVars, ArrayRef<const Expr *> DestExprs, |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 3189 | ArrayRef<const Expr *> SrcExprs, ArrayRef<const Expr *> AssignmentOps, |
| 3190 | SourceLocation Loc) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3191 | ASTContext &C = CGM.getContext(); |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3192 | // void copy_func(void *LHSArg, void *RHSArg); |
| 3193 | FunctionArgList Args; |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 3194 | ImplicitParamDecl LHSArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, C.VoidPtrTy, |
| 3195 | ImplicitParamDecl::Other); |
| 3196 | ImplicitParamDecl RHSArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, C.VoidPtrTy, |
| 3197 | ImplicitParamDecl::Other); |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3198 | Args.push_back(&LHSArg); |
| 3199 | Args.push_back(&RHSArg); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3200 | const auto &CGFI = |
| 3201 | CGM.getTypes().arrangeBuiltinFunctionDeclaration(C.VoidTy, Args); |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 3202 | std::string Name = |
| 3203 | CGM.getOpenMPRuntime().getName({"omp", "copyprivate", "copy_func"}); |
| 3204 | auto *Fn = llvm::Function::Create(CGM.getTypes().GetFunctionType(CGFI), |
| 3205 | llvm::GlobalValue::InternalLinkage, Name, |
| 3206 | &CGM.getModule()); |
Rafael Espindola | 51ec5a9 | 2018-02-28 23:46:35 +0000 | [diff] [blame] | 3207 | CGM.SetInternalFunctionAttributes(GlobalDecl(), Fn, CGFI); |
Alexey Bataev | c0f879b | 2018-04-10 20:10:53 +0000 | [diff] [blame] | 3208 | Fn->setDoesNotRecurse(); |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3209 | CodeGenFunction CGF(CGM); |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 3210 | CGF.StartFunction(GlobalDecl(), C.VoidTy, Fn, CGFI, Args, Loc, Loc); |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 3211 | // Dest = (void*[n])(LHSArg); |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3212 | // Src = (void*[n])(RHSArg); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3213 | Address LHS(CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
| 3214 | CGF.Builder.CreateLoad(CGF.GetAddrOfLocalVar(&LHSArg)), |
| 3215 | ArgsType), CGF.getPointerAlign()); |
| 3216 | Address RHS(CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
| 3217 | CGF.Builder.CreateLoad(CGF.GetAddrOfLocalVar(&RHSArg)), |
| 3218 | ArgsType), CGF.getPointerAlign()); |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3219 | // *(Type0*)Dst[0] = *(Type0*)Src[0]; |
| 3220 | // *(Type1*)Dst[1] = *(Type1*)Src[1]; |
| 3221 | // ... |
| 3222 | // *(Typen*)Dst[n] = *(Typen*)Src[n]; |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3223 | for (unsigned I = 0, E = AssignmentOps.size(); I < E; ++I) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3224 | const auto *DestVar = |
| 3225 | cast<VarDecl>(cast<DeclRefExpr>(DestExprs[I])->getDecl()); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3226 | Address DestAddr = emitAddrOfVarFromArray(CGF, LHS, I, DestVar); |
| 3227 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3228 | const auto *SrcVar = |
| 3229 | cast<VarDecl>(cast<DeclRefExpr>(SrcExprs[I])->getDecl()); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3230 | Address SrcAddr = emitAddrOfVarFromArray(CGF, RHS, I, SrcVar); |
| 3231 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3232 | const auto *VD = cast<DeclRefExpr>(CopyprivateVars[I])->getDecl(); |
Alexey Bataev | 1d9c15c | 2015-05-19 12:31:28 +0000 | [diff] [blame] | 3233 | QualType Type = VD->getType(); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3234 | CGF.EmitOMPCopy(Type, DestAddr, SrcAddr, DestVar, SrcVar, AssignmentOps[I]); |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3235 | } |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3236 | CGF.FinishFunction(); |
| 3237 | return Fn; |
| 3238 | } |
| 3239 | |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 3240 | void CGOpenMPRuntime::emitSingleRegion(CodeGenFunction &CGF, |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 3241 | const RegionCodeGenTy &SingleOpGen, |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3242 | SourceLocation Loc, |
| 3243 | ArrayRef<const Expr *> CopyprivateVars, |
| 3244 | ArrayRef<const Expr *> SrcExprs, |
| 3245 | ArrayRef<const Expr *> DstExprs, |
| 3246 | ArrayRef<const Expr *> AssignmentOps) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 3247 | if (!CGF.HaveInsertPoint()) |
| 3248 | return; |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3249 | assert(CopyprivateVars.size() == SrcExprs.size() && |
| 3250 | CopyprivateVars.size() == DstExprs.size() && |
| 3251 | CopyprivateVars.size() == AssignmentOps.size()); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3252 | ASTContext &C = CGM.getContext(); |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3253 | // int32 did_it = 0; |
Alexey Bataev | 6956e2e | 2015-02-05 06:35:41 +0000 | [diff] [blame] | 3254 | // if(__kmpc_single(ident_t *, gtid)) { |
| 3255 | // SingleOpGen(); |
| 3256 | // __kmpc_end_single(ident_t *, gtid); |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3257 | // did_it = 1; |
Alexey Bataev | 6956e2e | 2015-02-05 06:35:41 +0000 | [diff] [blame] | 3258 | // } |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3259 | // call __kmpc_copyprivate(ident_t *, gtid, <buf_size>, <copyprivate list>, |
| 3260 | // <copy_func>, did_it); |
| 3261 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3262 | Address DidIt = Address::invalid(); |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3263 | if (!CopyprivateVars.empty()) { |
| 3264 | // int32 did_it = 0; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3265 | QualType KmpInt32Ty = |
| 3266 | C.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1); |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3267 | DidIt = CGF.CreateMemTemp(KmpInt32Ty, ".omp.copyprivate.did_it"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3268 | CGF.Builder.CreateStore(CGF.Builder.getInt32(0), DidIt); |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3269 | } |
Alexey Bataev | 6956e2e | 2015-02-05 06:35:41 +0000 | [diff] [blame] | 3270 | // Prepare arguments and build a call to __kmpc_single |
Alexey Bataev | d7614fb | 2015-04-10 06:33:45 +0000 | [diff] [blame] | 3271 | llvm::Value *Args[] = {emitUpdateLocation(CGF, Loc), getThreadID(CGF, Loc)}; |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 3272 | CommonActionTy Action(createRuntimeFunction(OMPRTL__kmpc_single), Args, |
| 3273 | createRuntimeFunction(OMPRTL__kmpc_end_single), Args, |
| 3274 | /*Conditional=*/true); |
| 3275 | SingleOpGen.setAction(Action); |
| 3276 | emitInlinedDirective(CGF, OMPD_single, SingleOpGen); |
| 3277 | if (DidIt.isValid()) { |
| 3278 | // did_it = 1; |
| 3279 | CGF.Builder.CreateStore(CGF.Builder.getInt32(1), DidIt); |
| 3280 | } |
| 3281 | Action.Done(CGF); |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3282 | // call __kmpc_copyprivate(ident_t *, gtid, <buf_size>, <copyprivate list>, |
| 3283 | // <copy_func>, did_it); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3284 | if (DidIt.isValid()) { |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3285 | llvm::APInt ArraySize(/*unsigned int numBits=*/32, CopyprivateVars.size()); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3286 | QualType CopyprivateArrayTy = |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3287 | C.getConstantArrayType(C.VoidPtrTy, ArraySize, ArrayType::Normal, |
| 3288 | /*IndexTypeQuals=*/0); |
| 3289 | // Create a list of all private variables for copyprivate. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3290 | Address CopyprivateList = |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3291 | CGF.CreateMemTemp(CopyprivateArrayTy, ".omp.copyprivate.cpr_list"); |
| 3292 | for (unsigned I = 0, E = CopyprivateVars.size(); I < E; ++I) { |
James Y Knight | 751fe28 | 2019-02-09 22:22:28 +0000 | [diff] [blame] | 3293 | Address Elem = CGF.Builder.CreateConstArrayGEP(CopyprivateList, I); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3294 | CGF.Builder.CreateStore( |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3295 | CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3296 | CGF.EmitLValue(CopyprivateVars[I]).getPointer(), CGF.VoidPtrTy), |
| 3297 | Elem); |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3298 | } |
| 3299 | // Build function that copies private values from single region to all other |
| 3300 | // threads in the corresponding parallel region. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3301 | llvm::Value *CpyFn = emitCopyprivateCopyFunction( |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3302 | CGM, CGF.ConvertTypeForMem(CopyprivateArrayTy)->getPointerTo(), |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 3303 | CopyprivateVars, SrcExprs, DstExprs, AssignmentOps, Loc); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3304 | llvm::Value *BufSize = CGF.getTypeSize(CopyprivateArrayTy); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3305 | Address CL = |
| 3306 | CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(CopyprivateList, |
| 3307 | CGF.VoidPtrTy); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3308 | llvm::Value *DidItVal = CGF.Builder.CreateLoad(DidIt); |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3309 | llvm::Value *Args[] = { |
| 3310 | emitUpdateLocation(CGF, Loc), // ident_t *<loc> |
| 3311 | getThreadID(CGF, Loc), // i32 <gtid> |
Alexey Bataev | 66beaa9 | 2015-04-30 03:47:32 +0000 | [diff] [blame] | 3312 | BufSize, // size_t <buf_size> |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3313 | CL.getPointer(), // void *<copyprivate list> |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3314 | CpyFn, // void (*) (void *, void *) <copy_func> |
| 3315 | DidItVal // i32 did_it |
| 3316 | }; |
| 3317 | CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_copyprivate), Args); |
| 3318 | } |
Alexey Bataev | 6956e2e | 2015-02-05 06:35:41 +0000 | [diff] [blame] | 3319 | } |
| 3320 | |
Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 3321 | void CGOpenMPRuntime::emitOrderedRegion(CodeGenFunction &CGF, |
| 3322 | const RegionCodeGenTy &OrderedOpGen, |
Alexey Bataev | 5f600d6 | 2015-09-29 03:48:57 +0000 | [diff] [blame] | 3323 | SourceLocation Loc, bool IsThreads) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 3324 | if (!CGF.HaveInsertPoint()) |
| 3325 | return; |
Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 3326 | // __kmpc_ordered(ident_t *, gtid); |
| 3327 | // OrderedOpGen(); |
| 3328 | // __kmpc_end_ordered(ident_t *, gtid); |
| 3329 | // Prepare arguments and build a call to __kmpc_ordered |
Alexey Bataev | 5f600d6 | 2015-09-29 03:48:57 +0000 | [diff] [blame] | 3330 | if (IsThreads) { |
Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 3331 | llvm::Value *Args[] = {emitUpdateLocation(CGF, Loc), getThreadID(CGF, Loc)}; |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 3332 | CommonActionTy Action(createRuntimeFunction(OMPRTL__kmpc_ordered), Args, |
| 3333 | createRuntimeFunction(OMPRTL__kmpc_end_ordered), |
| 3334 | Args); |
| 3335 | OrderedOpGen.setAction(Action); |
| 3336 | emitInlinedDirective(CGF, OMPD_ordered, OrderedOpGen); |
| 3337 | return; |
Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 3338 | } |
Alexey Bataev | 5f600d6 | 2015-09-29 03:48:57 +0000 | [diff] [blame] | 3339 | emitInlinedDirective(CGF, OMPD_ordered, OrderedOpGen); |
Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 3340 | } |
| 3341 | |
Alexey Bataev | c3028ca | 2018-12-04 15:03:25 +0000 | [diff] [blame] | 3342 | unsigned CGOpenMPRuntime::getDefaultFlagsForBarriers(OpenMPDirectiveKind Kind) { |
Alexey Bataev | 50b3c95 | 2016-02-19 10:38:26 +0000 | [diff] [blame] | 3343 | unsigned Flags; |
| 3344 | if (Kind == OMPD_for) |
| 3345 | Flags = OMP_IDENT_BARRIER_IMPL_FOR; |
| 3346 | else if (Kind == OMPD_sections) |
| 3347 | Flags = OMP_IDENT_BARRIER_IMPL_SECTIONS; |
| 3348 | else if (Kind == OMPD_single) |
| 3349 | Flags = OMP_IDENT_BARRIER_IMPL_SINGLE; |
| 3350 | else if (Kind == OMPD_barrier) |
| 3351 | Flags = OMP_IDENT_BARRIER_EXPL; |
| 3352 | else |
| 3353 | Flags = OMP_IDENT_BARRIER_IMPL; |
Alexey Bataev | c3028ca | 2018-12-04 15:03:25 +0000 | [diff] [blame] | 3354 | return Flags; |
| 3355 | } |
| 3356 | |
Alexey Bataev | f6a53d6 | 2019-03-18 18:40:00 +0000 | [diff] [blame] | 3357 | void CGOpenMPRuntime::getDefaultScheduleAndChunk( |
| 3358 | CodeGenFunction &CGF, const OMPLoopDirective &S, |
| 3359 | OpenMPScheduleClauseKind &ScheduleKind, const Expr *&ChunkExpr) const { |
| 3360 | // Check if the loop directive is actually a doacross loop directive. In this |
| 3361 | // case choose static, 1 schedule. |
| 3362 | if (llvm::any_of( |
| 3363 | S.getClausesOfKind<OMPOrderedClause>(), |
| 3364 | [](const OMPOrderedClause *C) { return C->getNumForLoops(); })) { |
| 3365 | ScheduleKind = OMPC_SCHEDULE_static; |
| 3366 | // Chunk size is 1 in this case. |
| 3367 | llvm::APInt ChunkSize(32, 1); |
| 3368 | ChunkExpr = IntegerLiteral::Create( |
| 3369 | CGF.getContext(), ChunkSize, |
| 3370 | CGF.getContext().getIntTypeForBitwidth(32, /*Signed=*/0), |
| 3371 | SourceLocation()); |
| 3372 | } |
| 3373 | } |
| 3374 | |
Alexey Bataev | c3028ca | 2018-12-04 15:03:25 +0000 | [diff] [blame] | 3375 | void CGOpenMPRuntime::emitBarrierCall(CodeGenFunction &CGF, SourceLocation Loc, |
| 3376 | OpenMPDirectiveKind Kind, bool EmitChecks, |
| 3377 | bool ForceSimpleCall) { |
| 3378 | if (!CGF.HaveInsertPoint()) |
| 3379 | return; |
| 3380 | // Build call __kmpc_cancel_barrier(loc, thread_id); |
| 3381 | // Build call __kmpc_barrier(loc, thread_id); |
| 3382 | unsigned Flags = getDefaultFlagsForBarriers(Kind); |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 3383 | // Build call __kmpc_cancel_barrier(loc, thread_id) or __kmpc_barrier(loc, |
| 3384 | // thread_id); |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 3385 | llvm::Value *Args[] = {emitUpdateLocation(CGF, Loc, Flags), |
| 3386 | getThreadID(CGF, Loc)}; |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 3387 | if (auto *OMPRegionInfo = |
| 3388 | dyn_cast_or_null<CGOpenMPRegionInfo>(CGF.CapturedStmtInfo)) { |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 3389 | if (!ForceSimpleCall && OMPRegionInfo->hasCancel()) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3390 | llvm::Value *Result = CGF.EmitRuntimeCall( |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 3391 | createRuntimeFunction(OMPRTL__kmpc_cancel_barrier), Args); |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 3392 | if (EmitChecks) { |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 3393 | // if (__kmpc_cancel_barrier()) { |
| 3394 | // exit from construct; |
| 3395 | // } |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3396 | llvm::BasicBlock *ExitBB = CGF.createBasicBlock(".cancel.exit"); |
| 3397 | llvm::BasicBlock *ContBB = CGF.createBasicBlock(".cancel.continue"); |
| 3398 | llvm::Value *Cmp = CGF.Builder.CreateIsNotNull(Result); |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 3399 | CGF.Builder.CreateCondBr(Cmp, ExitBB, ContBB); |
| 3400 | CGF.EmitBlock(ExitBB); |
| 3401 | // exit from construct; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3402 | CodeGenFunction::JumpDest CancelDestination = |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 3403 | CGF.getOMPCancelDestination(OMPRegionInfo->getDirectiveKind()); |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 3404 | CGF.EmitBranchThroughCleanup(CancelDestination); |
| 3405 | CGF.EmitBlock(ContBB, /*IsFinished=*/true); |
| 3406 | } |
| 3407 | return; |
| 3408 | } |
| 3409 | } |
| 3410 | CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_barrier), Args); |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 3411 | } |
| 3412 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 3413 | /// Map the OpenMP loop schedule to the runtime enumeration. |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 3414 | static OpenMPSchedType getRuntimeSchedule(OpenMPScheduleClauseKind ScheduleKind, |
Alexey Bataev | d7589ffe | 2015-05-20 13:12:48 +0000 | [diff] [blame] | 3415 | bool Chunked, bool Ordered) { |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 3416 | switch (ScheduleKind) { |
| 3417 | case OMPC_SCHEDULE_static: |
Alexey Bataev | d7589ffe | 2015-05-20 13:12:48 +0000 | [diff] [blame] | 3418 | return Chunked ? (Ordered ? OMP_ord_static_chunked : OMP_sch_static_chunked) |
| 3419 | : (Ordered ? OMP_ord_static : OMP_sch_static); |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 3420 | case OMPC_SCHEDULE_dynamic: |
Alexey Bataev | d7589ffe | 2015-05-20 13:12:48 +0000 | [diff] [blame] | 3421 | return Ordered ? OMP_ord_dynamic_chunked : OMP_sch_dynamic_chunked; |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 3422 | case OMPC_SCHEDULE_guided: |
Alexey Bataev | d7589ffe | 2015-05-20 13:12:48 +0000 | [diff] [blame] | 3423 | return Ordered ? OMP_ord_guided_chunked : OMP_sch_guided_chunked; |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 3424 | case OMPC_SCHEDULE_runtime: |
Alexey Bataev | d7589ffe | 2015-05-20 13:12:48 +0000 | [diff] [blame] | 3425 | return Ordered ? OMP_ord_runtime : OMP_sch_runtime; |
| 3426 | case OMPC_SCHEDULE_auto: |
| 3427 | return Ordered ? OMP_ord_auto : OMP_sch_auto; |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 3428 | case OMPC_SCHEDULE_unknown: |
| 3429 | assert(!Chunked && "chunk was specified but schedule kind not known"); |
Alexey Bataev | d7589ffe | 2015-05-20 13:12:48 +0000 | [diff] [blame] | 3430 | return Ordered ? OMP_ord_static : OMP_sch_static; |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 3431 | } |
| 3432 | llvm_unreachable("Unexpected runtime schedule"); |
| 3433 | } |
| 3434 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 3435 | /// Map the OpenMP distribute schedule to the runtime enumeration. |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3436 | static OpenMPSchedType |
| 3437 | getRuntimeSchedule(OpenMPDistScheduleClauseKind ScheduleKind, bool Chunked) { |
| 3438 | // only static is allowed for dist_schedule |
| 3439 | return Chunked ? OMP_dist_sch_static_chunked : OMP_dist_sch_static; |
| 3440 | } |
| 3441 | |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 3442 | bool CGOpenMPRuntime::isStaticNonchunked(OpenMPScheduleClauseKind ScheduleKind, |
| 3443 | bool Chunked) const { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3444 | OpenMPSchedType Schedule = |
| 3445 | getRuntimeSchedule(ScheduleKind, Chunked, /*Ordered=*/false); |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 3446 | return Schedule == OMP_sch_static; |
| 3447 | } |
| 3448 | |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3449 | bool CGOpenMPRuntime::isStaticNonchunked( |
| 3450 | OpenMPDistScheduleClauseKind ScheduleKind, bool Chunked) const { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3451 | OpenMPSchedType Schedule = getRuntimeSchedule(ScheduleKind, Chunked); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3452 | return Schedule == OMP_dist_sch_static; |
| 3453 | } |
| 3454 | |
Gheorghe-Teodor Bercea | e925676 | 2018-10-29 15:45:47 +0000 | [diff] [blame] | 3455 | bool CGOpenMPRuntime::isStaticChunked(OpenMPScheduleClauseKind ScheduleKind, |
| 3456 | bool Chunked) const { |
| 3457 | OpenMPSchedType Schedule = |
| 3458 | getRuntimeSchedule(ScheduleKind, Chunked, /*Ordered=*/false); |
| 3459 | return Schedule == OMP_sch_static_chunked; |
| 3460 | } |
| 3461 | |
| 3462 | bool CGOpenMPRuntime::isStaticChunked( |
| 3463 | OpenMPDistScheduleClauseKind ScheduleKind, bool Chunked) const { |
| 3464 | OpenMPSchedType Schedule = getRuntimeSchedule(ScheduleKind, Chunked); |
| 3465 | return Schedule == OMP_dist_sch_static_chunked; |
| 3466 | } |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3467 | |
Alexander Musman | df7a8e2 | 2015-01-22 08:49:35 +0000 | [diff] [blame] | 3468 | bool CGOpenMPRuntime::isDynamic(OpenMPScheduleClauseKind ScheduleKind) const { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3469 | OpenMPSchedType Schedule = |
Alexey Bataev | d7589ffe | 2015-05-20 13:12:48 +0000 | [diff] [blame] | 3470 | getRuntimeSchedule(ScheduleKind, /*Chunked=*/false, /*Ordered=*/false); |
Alexander Musman | df7a8e2 | 2015-01-22 08:49:35 +0000 | [diff] [blame] | 3471 | assert(Schedule != OMP_sch_static_chunked && "cannot be chunked here"); |
| 3472 | return Schedule != OMP_sch_static; |
| 3473 | } |
| 3474 | |
Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 3475 | static int addMonoNonMonoModifier(OpenMPSchedType Schedule, |
| 3476 | OpenMPScheduleClauseModifier M1, |
| 3477 | OpenMPScheduleClauseModifier M2) { |
Alexey Bataev | 6cff624 | 2016-05-30 13:05:14 +0000 | [diff] [blame] | 3478 | int Modifier = 0; |
Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 3479 | switch (M1) { |
| 3480 | case OMPC_SCHEDULE_MODIFIER_monotonic: |
Alexey Bataev | 6cff624 | 2016-05-30 13:05:14 +0000 | [diff] [blame] | 3481 | Modifier = OMP_sch_modifier_monotonic; |
| 3482 | break; |
Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 3483 | case OMPC_SCHEDULE_MODIFIER_nonmonotonic: |
Alexey Bataev | 6cff624 | 2016-05-30 13:05:14 +0000 | [diff] [blame] | 3484 | Modifier = OMP_sch_modifier_nonmonotonic; |
| 3485 | break; |
Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 3486 | case OMPC_SCHEDULE_MODIFIER_simd: |
Alexey Bataev | 6cff624 | 2016-05-30 13:05:14 +0000 | [diff] [blame] | 3487 | if (Schedule == OMP_sch_static_chunked) |
| 3488 | Schedule = OMP_sch_static_balanced_chunked; |
| 3489 | break; |
Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 3490 | case OMPC_SCHEDULE_MODIFIER_last: |
| 3491 | case OMPC_SCHEDULE_MODIFIER_unknown: |
| 3492 | break; |
| 3493 | } |
| 3494 | switch (M2) { |
| 3495 | case OMPC_SCHEDULE_MODIFIER_monotonic: |
Alexey Bataev | 6cff624 | 2016-05-30 13:05:14 +0000 | [diff] [blame] | 3496 | Modifier = OMP_sch_modifier_monotonic; |
| 3497 | break; |
Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 3498 | case OMPC_SCHEDULE_MODIFIER_nonmonotonic: |
Alexey Bataev | 6cff624 | 2016-05-30 13:05:14 +0000 | [diff] [blame] | 3499 | Modifier = OMP_sch_modifier_nonmonotonic; |
| 3500 | break; |
Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 3501 | case OMPC_SCHEDULE_MODIFIER_simd: |
Alexey Bataev | 6cff624 | 2016-05-30 13:05:14 +0000 | [diff] [blame] | 3502 | if (Schedule == OMP_sch_static_chunked) |
| 3503 | Schedule = OMP_sch_static_balanced_chunked; |
| 3504 | break; |
Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 3505 | case OMPC_SCHEDULE_MODIFIER_last: |
| 3506 | case OMPC_SCHEDULE_MODIFIER_unknown: |
| 3507 | break; |
| 3508 | } |
Alexey Bataev | 6cff624 | 2016-05-30 13:05:14 +0000 | [diff] [blame] | 3509 | return Schedule | Modifier; |
Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 3510 | } |
| 3511 | |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 3512 | void CGOpenMPRuntime::emitForDispatchInit( |
| 3513 | CodeGenFunction &CGF, SourceLocation Loc, |
| 3514 | const OpenMPScheduleTy &ScheduleKind, unsigned IVSize, bool IVSigned, |
| 3515 | bool Ordered, const DispatchRTInput &DispatchValues) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 3516 | if (!CGF.HaveInsertPoint()) |
| 3517 | return; |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 3518 | OpenMPSchedType Schedule = getRuntimeSchedule( |
| 3519 | ScheduleKind.Schedule, DispatchValues.Chunk != nullptr, Ordered); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3520 | assert(Ordered || |
| 3521 | (Schedule != OMP_sch_static && Schedule != OMP_sch_static_chunked && |
Alexey Bataev | 6cff624 | 2016-05-30 13:05:14 +0000 | [diff] [blame] | 3522 | Schedule != OMP_ord_static && Schedule != OMP_ord_static_chunked && |
| 3523 | Schedule != OMP_sch_static_balanced_chunked)); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3524 | // Call __kmpc_dispatch_init( |
| 3525 | // ident_t *loc, kmp_int32 tid, kmp_int32 schedule, |
| 3526 | // kmp_int[32|64] lower, kmp_int[32|64] upper, |
| 3527 | // kmp_int[32|64] stride, kmp_int[32|64] chunk); |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 3528 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3529 | // 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] | 3530 | llvm::Value *Chunk = DispatchValues.Chunk ? DispatchValues.Chunk |
| 3531 | : CGF.Builder.getIntN(IVSize, 1); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3532 | llvm::Value *Args[] = { |
Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 3533 | emitUpdateLocation(CGF, Loc), getThreadID(CGF, Loc), |
| 3534 | CGF.Builder.getInt32(addMonoNonMonoModifier( |
| 3535 | Schedule, ScheduleKind.M1, ScheduleKind.M2)), // Schedule type |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 3536 | DispatchValues.LB, // Lower |
| 3537 | DispatchValues.UB, // Upper |
Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 3538 | CGF.Builder.getIntN(IVSize, 1), // Stride |
| 3539 | Chunk // Chunk |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3540 | }; |
| 3541 | CGF.EmitRuntimeCall(createDispatchInitFunction(IVSize, IVSigned), Args); |
| 3542 | } |
| 3543 | |
Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 3544 | static void emitForStaticInitCall( |
| 3545 | CodeGenFunction &CGF, llvm::Value *UpdateLocation, llvm::Value *ThreadId, |
James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 3546 | llvm::FunctionCallee ForStaticInitFunction, OpenMPSchedType Schedule, |
Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 3547 | OpenMPScheduleClauseModifier M1, OpenMPScheduleClauseModifier M2, |
Alexey Bataev | 0f87dbe | 2017-08-14 17:56:13 +0000 | [diff] [blame] | 3548 | const CGOpenMPRuntime::StaticRTInput &Values) { |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3549 | if (!CGF.HaveInsertPoint()) |
Alexey Bataev | 0f87dbe | 2017-08-14 17:56:13 +0000 | [diff] [blame] | 3550 | return; |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3551 | |
Alexey Bataev | 0f87dbe | 2017-08-14 17:56:13 +0000 | [diff] [blame] | 3552 | assert(!Values.Ordered); |
| 3553 | assert(Schedule == OMP_sch_static || Schedule == OMP_sch_static_chunked || |
| 3554 | Schedule == OMP_sch_static_balanced_chunked || |
| 3555 | Schedule == OMP_ord_static || Schedule == OMP_ord_static_chunked || |
| 3556 | Schedule == OMP_dist_sch_static || |
| 3557 | Schedule == OMP_dist_sch_static_chunked); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3558 | |
Alexey Bataev | 0f87dbe | 2017-08-14 17:56:13 +0000 | [diff] [blame] | 3559 | // Call __kmpc_for_static_init( |
| 3560 | // ident_t *loc, kmp_int32 tid, kmp_int32 schedtype, |
| 3561 | // kmp_int32 *p_lastiter, kmp_int[32|64] *p_lower, |
| 3562 | // kmp_int[32|64] *p_upper, kmp_int[32|64] *p_stride, |
| 3563 | // kmp_int[32|64] incr, kmp_int[32|64] chunk); |
| 3564 | llvm::Value *Chunk = Values.Chunk; |
| 3565 | if (Chunk == nullptr) { |
| 3566 | assert((Schedule == OMP_sch_static || Schedule == OMP_ord_static || |
| 3567 | Schedule == OMP_dist_sch_static) && |
| 3568 | "expected static non-chunked schedule"); |
| 3569 | // If the Chunk was not specified in the clause - use default value 1. |
| 3570 | Chunk = CGF.Builder.getIntN(Values.IVSize, 1); |
| 3571 | } else { |
| 3572 | assert((Schedule == OMP_sch_static_chunked || |
| 3573 | Schedule == OMP_sch_static_balanced_chunked || |
| 3574 | Schedule == OMP_ord_static_chunked || |
| 3575 | Schedule == OMP_dist_sch_static_chunked) && |
| 3576 | "expected static chunked schedule"); |
| 3577 | } |
| 3578 | llvm::Value *Args[] = { |
| 3579 | UpdateLocation, |
| 3580 | ThreadId, |
| 3581 | CGF.Builder.getInt32(addMonoNonMonoModifier(Schedule, M1, |
| 3582 | M2)), // Schedule type |
| 3583 | Values.IL.getPointer(), // &isLastIter |
| 3584 | Values.LB.getPointer(), // &LB |
| 3585 | Values.UB.getPointer(), // &UB |
| 3586 | Values.ST.getPointer(), // &Stride |
| 3587 | CGF.Builder.getIntN(Values.IVSize, 1), // Incr |
| 3588 | Chunk // Chunk |
| 3589 | }; |
| 3590 | CGF.EmitRuntimeCall(ForStaticInitFunction, Args); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3591 | } |
| 3592 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3593 | void CGOpenMPRuntime::emitForStaticInit(CodeGenFunction &CGF, |
| 3594 | SourceLocation Loc, |
Alexey Bataev | 0f87dbe | 2017-08-14 17:56:13 +0000 | [diff] [blame] | 3595 | OpenMPDirectiveKind DKind, |
Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 3596 | const OpenMPScheduleTy &ScheduleKind, |
Alexey Bataev | 0f87dbe | 2017-08-14 17:56:13 +0000 | [diff] [blame] | 3597 | const StaticRTInput &Values) { |
| 3598 | OpenMPSchedType ScheduleNum = getRuntimeSchedule( |
| 3599 | ScheduleKind.Schedule, Values.Chunk != nullptr, Values.Ordered); |
| 3600 | assert(isOpenMPWorksharingDirective(DKind) && |
| 3601 | "Expected loop-based or sections-based directive."); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3602 | llvm::Value *UpdatedLocation = emitUpdateLocation(CGF, Loc, |
Alexey Bataev | 0f87dbe | 2017-08-14 17:56:13 +0000 | [diff] [blame] | 3603 | isOpenMPLoopDirective(DKind) |
| 3604 | ? OMP_IDENT_WORK_LOOP |
| 3605 | : OMP_IDENT_WORK_SECTIONS); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3606 | llvm::Value *ThreadId = getThreadID(CGF, Loc); |
James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 3607 | llvm::FunctionCallee StaticInitFunction = |
Alexey Bataev | 0f87dbe | 2017-08-14 17:56:13 +0000 | [diff] [blame] | 3608 | createForStaticInitFunction(Values.IVSize, Values.IVSigned); |
Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 3609 | emitForStaticInitCall(CGF, UpdatedLocation, ThreadId, StaticInitFunction, |
Alexey Bataev | 0f87dbe | 2017-08-14 17:56:13 +0000 | [diff] [blame] | 3610 | ScheduleNum, ScheduleKind.M1, ScheduleKind.M2, Values); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3611 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3612 | |
Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 3613 | void CGOpenMPRuntime::emitDistributeStaticInit( |
| 3614 | CodeGenFunction &CGF, SourceLocation Loc, |
Alexey Bataev | 0f87dbe | 2017-08-14 17:56:13 +0000 | [diff] [blame] | 3615 | OpenMPDistScheduleClauseKind SchedKind, |
| 3616 | const CGOpenMPRuntime::StaticRTInput &Values) { |
| 3617 | OpenMPSchedType ScheduleNum = |
| 3618 | getRuntimeSchedule(SchedKind, Values.Chunk != nullptr); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3619 | llvm::Value *UpdatedLocation = |
Alexey Bataev | 0f87dbe | 2017-08-14 17:56:13 +0000 | [diff] [blame] | 3620 | emitUpdateLocation(CGF, Loc, OMP_IDENT_WORK_DISTRIBUTE); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3621 | llvm::Value *ThreadId = getThreadID(CGF, Loc); |
James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 3622 | llvm::FunctionCallee StaticInitFunction = |
Alexey Bataev | 0f87dbe | 2017-08-14 17:56:13 +0000 | [diff] [blame] | 3623 | createForStaticInitFunction(Values.IVSize, Values.IVSigned); |
Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 3624 | emitForStaticInitCall(CGF, UpdatedLocation, ThreadId, StaticInitFunction, |
| 3625 | ScheduleNum, OMPC_SCHEDULE_MODIFIER_unknown, |
Alexey Bataev | 0f87dbe | 2017-08-14 17:56:13 +0000 | [diff] [blame] | 3626 | OMPC_SCHEDULE_MODIFIER_unknown, Values); |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 3627 | } |
| 3628 | |
Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 3629 | void CGOpenMPRuntime::emitForStaticFinish(CodeGenFunction &CGF, |
Alexey Bataev | f43f714 | 2017-09-06 16:17:35 +0000 | [diff] [blame] | 3630 | SourceLocation Loc, |
| 3631 | OpenMPDirectiveKind DKind) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 3632 | if (!CGF.HaveInsertPoint()) |
| 3633 | return; |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 3634 | // Call __kmpc_for_static_fini(ident_t *loc, kmp_int32 tid); |
Alexey Bataev | f43f714 | 2017-09-06 16:17:35 +0000 | [diff] [blame] | 3635 | llvm::Value *Args[] = { |
| 3636 | emitUpdateLocation(CGF, Loc, |
| 3637 | isOpenMPDistributeDirective(DKind) |
| 3638 | ? OMP_IDENT_WORK_DISTRIBUTE |
| 3639 | : isOpenMPLoopDirective(DKind) |
| 3640 | ? OMP_IDENT_WORK_LOOP |
| 3641 | : OMP_IDENT_WORK_SECTIONS), |
| 3642 | getThreadID(CGF, Loc)}; |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 3643 | CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_for_static_fini), |
| 3644 | Args); |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 3645 | } |
| 3646 | |
Alexey Bataev | d7589ffe | 2015-05-20 13:12:48 +0000 | [diff] [blame] | 3647 | void CGOpenMPRuntime::emitForOrderedIterationEnd(CodeGenFunction &CGF, |
| 3648 | SourceLocation Loc, |
| 3649 | unsigned IVSize, |
| 3650 | bool IVSigned) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 3651 | if (!CGF.HaveInsertPoint()) |
| 3652 | return; |
Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 3653 | // 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] | 3654 | llvm::Value *Args[] = {emitUpdateLocation(CGF, Loc), getThreadID(CGF, Loc)}; |
Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 3655 | CGF.EmitRuntimeCall(createDispatchFiniFunction(IVSize, IVSigned), Args); |
| 3656 | } |
| 3657 | |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 3658 | llvm::Value *CGOpenMPRuntime::emitForNext(CodeGenFunction &CGF, |
| 3659 | SourceLocation Loc, unsigned IVSize, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3660 | bool IVSigned, Address IL, |
| 3661 | Address LB, Address UB, |
| 3662 | Address ST) { |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 3663 | // Call __kmpc_dispatch_next( |
| 3664 | // ident_t *loc, kmp_int32 tid, kmp_int32 *p_lastiter, |
| 3665 | // kmp_int[32|64] *p_lower, kmp_int[32|64] *p_upper, |
| 3666 | // kmp_int[32|64] *p_stride); |
| 3667 | llvm::Value *Args[] = { |
Alexey Bataev | 50b3c95 | 2016-02-19 10:38:26 +0000 | [diff] [blame] | 3668 | emitUpdateLocation(CGF, Loc), |
| 3669 | getThreadID(CGF, Loc), |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3670 | IL.getPointer(), // &isLastIter |
| 3671 | LB.getPointer(), // &Lower |
| 3672 | UB.getPointer(), // &Upper |
| 3673 | ST.getPointer() // &Stride |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 3674 | }; |
| 3675 | llvm::Value *Call = |
| 3676 | CGF.EmitRuntimeCall(createDispatchNextFunction(IVSize, IVSigned), Args); |
| 3677 | return CGF.EmitScalarConversion( |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3678 | Call, CGF.getContext().getIntTypeForBitwidth(32, /*Signed=*/1), |
Filipe Cabecinhas | 7af183d | 2015-08-11 04:19:28 +0000 | [diff] [blame] | 3679 | CGF.getContext().BoolTy, Loc); |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 3680 | } |
| 3681 | |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 3682 | void CGOpenMPRuntime::emitNumThreadsClause(CodeGenFunction &CGF, |
| 3683 | llvm::Value *NumThreads, |
| 3684 | SourceLocation Loc) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 3685 | if (!CGF.HaveInsertPoint()) |
| 3686 | return; |
Alexey Bataev | b205978 | 2014-10-13 08:23:51 +0000 | [diff] [blame] | 3687 | // Build call __kmpc_push_num_threads(&loc, global_tid, num_threads) |
| 3688 | llvm::Value *Args[] = { |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 3689 | emitUpdateLocation(CGF, Loc), getThreadID(CGF, Loc), |
Alexey Bataev | b205978 | 2014-10-13 08:23:51 +0000 | [diff] [blame] | 3690 | CGF.Builder.CreateIntCast(NumThreads, CGF.Int32Ty, /*isSigned*/ true)}; |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 3691 | CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_push_num_threads), |
| 3692 | Args); |
Alexey Bataev | b205978 | 2014-10-13 08:23:51 +0000 | [diff] [blame] | 3693 | } |
| 3694 | |
Alexey Bataev | 7f210c6 | 2015-06-18 13:40:03 +0000 | [diff] [blame] | 3695 | void CGOpenMPRuntime::emitProcBindClause(CodeGenFunction &CGF, |
| 3696 | OpenMPProcBindClauseKind ProcBind, |
| 3697 | SourceLocation Loc) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 3698 | if (!CGF.HaveInsertPoint()) |
| 3699 | return; |
Alexey Bataev | 7f210c6 | 2015-06-18 13:40:03 +0000 | [diff] [blame] | 3700 | // Constants for proc bind value accepted by the runtime. |
| 3701 | enum ProcBindTy { |
| 3702 | ProcBindFalse = 0, |
| 3703 | ProcBindTrue, |
| 3704 | ProcBindMaster, |
| 3705 | ProcBindClose, |
| 3706 | ProcBindSpread, |
| 3707 | ProcBindIntel, |
| 3708 | ProcBindDefault |
| 3709 | } RuntimeProcBind; |
| 3710 | switch (ProcBind) { |
| 3711 | case OMPC_PROC_BIND_master: |
| 3712 | RuntimeProcBind = ProcBindMaster; |
| 3713 | break; |
| 3714 | case OMPC_PROC_BIND_close: |
| 3715 | RuntimeProcBind = ProcBindClose; |
| 3716 | break; |
| 3717 | case OMPC_PROC_BIND_spread: |
| 3718 | RuntimeProcBind = ProcBindSpread; |
| 3719 | break; |
| 3720 | case OMPC_PROC_BIND_unknown: |
| 3721 | llvm_unreachable("Unsupported proc_bind value."); |
| 3722 | } |
| 3723 | // Build call __kmpc_push_proc_bind(&loc, global_tid, proc_bind) |
| 3724 | llvm::Value *Args[] = { |
| 3725 | emitUpdateLocation(CGF, Loc), getThreadID(CGF, Loc), |
| 3726 | llvm::ConstantInt::get(CGM.IntTy, RuntimeProcBind, /*isSigned=*/true)}; |
| 3727 | CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_push_proc_bind), Args); |
| 3728 | } |
| 3729 | |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 3730 | void CGOpenMPRuntime::emitFlush(CodeGenFunction &CGF, ArrayRef<const Expr *>, |
| 3731 | SourceLocation Loc) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 3732 | if (!CGF.HaveInsertPoint()) |
| 3733 | return; |
Alexey Bataev | d76df6d | 2015-02-24 12:55:09 +0000 | [diff] [blame] | 3734 | // Build call void __kmpc_flush(ident_t *loc) |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 3735 | CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_flush), |
| 3736 | emitUpdateLocation(CGF, Loc)); |
Alexey Bataev | cc37cc1 | 2014-11-20 04:34:54 +0000 | [diff] [blame] | 3737 | } |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 3738 | |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 3739 | namespace { |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 3740 | /// Indexes of fields for type kmp_task_t. |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 3741 | enum KmpTaskTFields { |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 3742 | /// List of shared variables. |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 3743 | KmpTaskTShareds, |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 3744 | /// Task routine. |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 3745 | KmpTaskTRoutine, |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 3746 | /// Partition id for the untied tasks. |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 3747 | KmpTaskTPartId, |
Alexey Bataev | ad537bb | 2016-05-30 09:06:50 +0000 | [diff] [blame] | 3748 | /// Function with call of destructors for private variables. |
| 3749 | Data1, |
| 3750 | /// Task priority. |
| 3751 | Data2, |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 3752 | /// (Taskloops only) Lower bound. |
| 3753 | KmpTaskTLowerBound, |
| 3754 | /// (Taskloops only) Upper bound. |
| 3755 | KmpTaskTUpperBound, |
| 3756 | /// (Taskloops only) Stride. |
| 3757 | KmpTaskTStride, |
| 3758 | /// (Taskloops only) Is last iteration flag. |
| 3759 | KmpTaskTLastIter, |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 3760 | /// (Taskloops only) Reduction data. |
| 3761 | KmpTaskTReductions, |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 3762 | }; |
Hans Wennborg | 7eb5464 | 2015-09-10 17:07:54 +0000 | [diff] [blame] | 3763 | } // anonymous namespace |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 3764 | |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3765 | bool CGOpenMPRuntime::OffloadEntriesInfoManagerTy::empty() const { |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 3766 | return OffloadEntriesTargetRegion.empty() && |
| 3767 | OffloadEntriesDeviceGlobalVar.empty(); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3768 | } |
| 3769 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 3770 | /// Initialize target region entry. |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3771 | void CGOpenMPRuntime::OffloadEntriesInfoManagerTy:: |
| 3772 | initializeTargetRegionEntryInfo(unsigned DeviceID, unsigned FileID, |
| 3773 | StringRef ParentName, unsigned LineNum, |
Samuel Antao | 2de62b0 | 2016-02-13 23:35:10 +0000 | [diff] [blame] | 3774 | unsigned Order) { |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3775 | assert(CGM.getLangOpts().OpenMPIsDevice && "Initialization of entries is " |
| 3776 | "only required for the device " |
| 3777 | "code generation."); |
Samuel Antao | 2de62b0 | 2016-02-13 23:35:10 +0000 | [diff] [blame] | 3778 | OffloadEntriesTargetRegion[DeviceID][FileID][ParentName][LineNum] = |
Samuel Antao | f83efdb | 2017-01-05 16:02:49 +0000 | [diff] [blame] | 3779 | OffloadEntryInfoTargetRegion(Order, /*Addr=*/nullptr, /*ID=*/nullptr, |
Alexey Bataev | 34f8a70 | 2018-03-28 14:28:54 +0000 | [diff] [blame] | 3780 | OMPTargetRegionEntryTargetRegion); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3781 | ++OffloadingEntriesNum; |
| 3782 | } |
| 3783 | |
| 3784 | void CGOpenMPRuntime::OffloadEntriesInfoManagerTy:: |
| 3785 | registerTargetRegionEntryInfo(unsigned DeviceID, unsigned FileID, |
| 3786 | StringRef ParentName, unsigned LineNum, |
Samuel Antao | f83efdb | 2017-01-05 16:02:49 +0000 | [diff] [blame] | 3787 | llvm::Constant *Addr, llvm::Constant *ID, |
Alexey Bataev | 34f8a70 | 2018-03-28 14:28:54 +0000 | [diff] [blame] | 3788 | OMPTargetRegionEntryKind Flags) { |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3789 | // If we are emitting code for a target, the entry is already initialized, |
| 3790 | // only has to be registered. |
| 3791 | if (CGM.getLangOpts().OpenMPIsDevice) { |
Alexey Bataev | 64e62dc | 2018-04-30 16:26:57 +0000 | [diff] [blame] | 3792 | if (!hasTargetRegionEntryInfo(DeviceID, FileID, ParentName, LineNum)) { |
| 3793 | unsigned DiagID = CGM.getDiags().getCustomDiagID( |
| 3794 | DiagnosticsEngine::Error, |
| 3795 | "Unable to find target region on line '%0' in the device code."); |
| 3796 | CGM.getDiags().Report(DiagID) << LineNum; |
| 3797 | return; |
| 3798 | } |
Samuel Antao | 2de62b0 | 2016-02-13 23:35:10 +0000 | [diff] [blame] | 3799 | auto &Entry = |
| 3800 | OffloadEntriesTargetRegion[DeviceID][FileID][ParentName][LineNum]; |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3801 | assert(Entry.isValid() && "Entry not initialized!"); |
| 3802 | Entry.setAddress(Addr); |
| 3803 | Entry.setID(ID); |
Samuel Antao | f83efdb | 2017-01-05 16:02:49 +0000 | [diff] [blame] | 3804 | Entry.setFlags(Flags); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3805 | } else { |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 3806 | OffloadEntryInfoTargetRegion Entry(OffloadingEntriesNum, Addr, ID, Flags); |
Samuel Antao | 2de62b0 | 2016-02-13 23:35:10 +0000 | [diff] [blame] | 3807 | OffloadEntriesTargetRegion[DeviceID][FileID][ParentName][LineNum] = Entry; |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 3808 | ++OffloadingEntriesNum; |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3809 | } |
| 3810 | } |
| 3811 | |
| 3812 | bool CGOpenMPRuntime::OffloadEntriesInfoManagerTy::hasTargetRegionEntryInfo( |
Samuel Antao | 2de62b0 | 2016-02-13 23:35:10 +0000 | [diff] [blame] | 3813 | unsigned DeviceID, unsigned FileID, StringRef ParentName, |
| 3814 | unsigned LineNum) const { |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3815 | auto PerDevice = OffloadEntriesTargetRegion.find(DeviceID); |
| 3816 | if (PerDevice == OffloadEntriesTargetRegion.end()) |
| 3817 | return false; |
| 3818 | auto PerFile = PerDevice->second.find(FileID); |
| 3819 | if (PerFile == PerDevice->second.end()) |
| 3820 | return false; |
| 3821 | auto PerParentName = PerFile->second.find(ParentName); |
| 3822 | if (PerParentName == PerFile->second.end()) |
| 3823 | return false; |
| 3824 | auto PerLine = PerParentName->second.find(LineNum); |
| 3825 | if (PerLine == PerParentName->second.end()) |
| 3826 | return false; |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3827 | // Fail if this entry is already registered. |
Samuel Antao | 2de62b0 | 2016-02-13 23:35:10 +0000 | [diff] [blame] | 3828 | if (PerLine->second.getAddress() || PerLine->second.getID()) |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3829 | return false; |
| 3830 | return true; |
| 3831 | } |
| 3832 | |
| 3833 | void CGOpenMPRuntime::OffloadEntriesInfoManagerTy::actOnTargetRegionEntriesInfo( |
| 3834 | const OffloadTargetRegionEntryInfoActTy &Action) { |
| 3835 | // Scan all target region entries and perform the provided action. |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 3836 | for (const auto &D : OffloadEntriesTargetRegion) |
| 3837 | for (const auto &F : D.second) |
| 3838 | for (const auto &P : F.second) |
| 3839 | for (const auto &L : P.second) |
Samuel Antao | 2de62b0 | 2016-02-13 23:35:10 +0000 | [diff] [blame] | 3840 | Action(D.first, F.first, P.first(), L.first, L.second); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3841 | } |
| 3842 | |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 3843 | void CGOpenMPRuntime::OffloadEntriesInfoManagerTy:: |
| 3844 | initializeDeviceGlobalVarEntryInfo(StringRef Name, |
| 3845 | OMPTargetGlobalVarEntryKind Flags, |
| 3846 | unsigned Order) { |
| 3847 | assert(CGM.getLangOpts().OpenMPIsDevice && "Initialization of entries is " |
| 3848 | "only required for the device " |
| 3849 | "code generation."); |
| 3850 | OffloadEntriesDeviceGlobalVar.try_emplace(Name, Order, Flags); |
| 3851 | ++OffloadingEntriesNum; |
| 3852 | } |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3853 | |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 3854 | void CGOpenMPRuntime::OffloadEntriesInfoManagerTy:: |
| 3855 | registerDeviceGlobalVarEntryInfo(StringRef VarName, llvm::Constant *Addr, |
| 3856 | CharUnits VarSize, |
| 3857 | OMPTargetGlobalVarEntryKind Flags, |
| 3858 | llvm::GlobalValue::LinkageTypes Linkage) { |
| 3859 | if (CGM.getLangOpts().OpenMPIsDevice) { |
| 3860 | auto &Entry = OffloadEntriesDeviceGlobalVar[VarName]; |
| 3861 | assert(Entry.isValid() && Entry.getFlags() == Flags && |
| 3862 | "Entry not initialized!"); |
| 3863 | assert((!Entry.getAddress() || Entry.getAddress() == Addr) && |
| 3864 | "Resetting with the new address."); |
Alexey Bataev | 8259cc3 | 2019-03-12 20:05:17 +0000 | [diff] [blame] | 3865 | if (Entry.getAddress() && hasDeviceGlobalVarEntryInfo(VarName)) { |
| 3866 | if (Entry.getVarSize().isZero()) { |
| 3867 | Entry.setVarSize(VarSize); |
| 3868 | Entry.setLinkage(Linkage); |
| 3869 | } |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 3870 | return; |
Alexey Bataev | 8259cc3 | 2019-03-12 20:05:17 +0000 | [diff] [blame] | 3871 | } |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 3872 | Entry.setVarSize(VarSize); |
| 3873 | Entry.setLinkage(Linkage); |
Alexey Bataev | 8259cc3 | 2019-03-12 20:05:17 +0000 | [diff] [blame] | 3874 | Entry.setAddress(Addr); |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 3875 | } else { |
Alexey Bataev | 8259cc3 | 2019-03-12 20:05:17 +0000 | [diff] [blame] | 3876 | if (hasDeviceGlobalVarEntryInfo(VarName)) { |
| 3877 | auto &Entry = OffloadEntriesDeviceGlobalVar[VarName]; |
| 3878 | assert(Entry.isValid() && Entry.getFlags() == Flags && |
| 3879 | "Entry not initialized!"); |
| 3880 | assert((!Entry.getAddress() || Entry.getAddress() == Addr) && |
| 3881 | "Resetting with the new address."); |
| 3882 | if (Entry.getVarSize().isZero()) { |
| 3883 | Entry.setVarSize(VarSize); |
| 3884 | Entry.setLinkage(Linkage); |
| 3885 | } |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 3886 | return; |
Alexey Bataev | 8259cc3 | 2019-03-12 20:05:17 +0000 | [diff] [blame] | 3887 | } |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 3888 | OffloadEntriesDeviceGlobalVar.try_emplace( |
| 3889 | VarName, OffloadingEntriesNum, Addr, VarSize, Flags, Linkage); |
| 3890 | ++OffloadingEntriesNum; |
| 3891 | } |
| 3892 | } |
| 3893 | |
| 3894 | void CGOpenMPRuntime::OffloadEntriesInfoManagerTy:: |
| 3895 | actOnDeviceGlobalVarEntriesInfo( |
| 3896 | const OffloadDeviceGlobalVarEntryInfoActTy &Action) { |
| 3897 | // Scan all target region entries and perform the provided action. |
| 3898 | for (const auto &E : OffloadEntriesDeviceGlobalVar) |
| 3899 | Action(E.getKey(), E.getValue()); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3900 | } |
| 3901 | |
| 3902 | llvm::Function * |
| 3903 | CGOpenMPRuntime::createOffloadingBinaryDescriptorRegistration() { |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3904 | // If we don't have entries or if we are emitting code for the device, we |
| 3905 | // don't need to do anything. |
| 3906 | if (CGM.getLangOpts().OpenMPIsDevice || OffloadEntriesInfoManager.empty()) |
| 3907 | return nullptr; |
| 3908 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3909 | llvm::Module &M = CGM.getModule(); |
| 3910 | ASTContext &C = CGM.getContext(); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3911 | |
| 3912 | // Get list of devices we care about |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3913 | const std::vector<llvm::Triple> &Devices = CGM.getLangOpts().OMPTargetTriples; |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3914 | |
| 3915 | // We should be creating an offloading descriptor only if there are devices |
| 3916 | // specified. |
| 3917 | assert(!Devices.empty() && "No OpenMP offloading devices??"); |
| 3918 | |
| 3919 | // Create the external variables that will point to the begin and end of the |
| 3920 | // host entries section. These will be defined by the linker. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3921 | llvm::Type *OffloadEntryTy = |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3922 | CGM.getTypes().ConvertTypeForMem(getTgtOffloadEntryQTy()); |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 3923 | std::string EntriesBeginName = getName({"omp_offloading", "entries_begin"}); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3924 | auto *HostEntriesBegin = new llvm::GlobalVariable( |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3925 | M, OffloadEntryTy, /*isConstant=*/true, |
Eugene Zelenko | 1660a5d | 2016-01-26 19:01:06 +0000 | [diff] [blame] | 3926 | llvm::GlobalValue::ExternalLinkage, /*Initializer=*/nullptr, |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 3927 | EntriesBeginName); |
| 3928 | std::string EntriesEndName = getName({"omp_offloading", "entries_end"}); |
| 3929 | auto *HostEntriesEnd = |
| 3930 | new llvm::GlobalVariable(M, OffloadEntryTy, /*isConstant=*/true, |
| 3931 | llvm::GlobalValue::ExternalLinkage, |
| 3932 | /*Initializer=*/nullptr, EntriesEndName); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3933 | |
| 3934 | // Create all device images |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3935 | auto *DeviceImageTy = cast<llvm::StructType>( |
| 3936 | CGM.getTypes().ConvertTypeForMem(getTgtDeviceImageQTy())); |
John McCall | 23c9dc6 | 2016-11-28 22:18:27 +0000 | [diff] [blame] | 3937 | ConstantInitBuilder DeviceImagesBuilder(CGM); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3938 | ConstantArrayBuilder DeviceImagesEntries = |
| 3939 | DeviceImagesBuilder.beginArray(DeviceImageTy); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3940 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3941 | for (const llvm::Triple &Device : Devices) { |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 3942 | StringRef T = Device.getTriple(); |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 3943 | std::string BeginName = getName({"omp_offloading", "img_start", ""}); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3944 | auto *ImgBegin = new llvm::GlobalVariable( |
Alexey Bataev | 62a4cb0 | 2018-07-31 18:27:42 +0000 | [diff] [blame] | 3945 | M, CGM.Int8Ty, /*isConstant=*/true, |
| 3946 | llvm::GlobalValue::ExternalWeakLinkage, |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 3947 | /*Initializer=*/nullptr, Twine(BeginName).concat(T)); |
| 3948 | std::string EndName = getName({"omp_offloading", "img_end", ""}); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3949 | auto *ImgEnd = new llvm::GlobalVariable( |
Alexey Bataev | 62a4cb0 | 2018-07-31 18:27:42 +0000 | [diff] [blame] | 3950 | M, CGM.Int8Ty, /*isConstant=*/true, |
| 3951 | llvm::GlobalValue::ExternalWeakLinkage, |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 3952 | /*Initializer=*/nullptr, Twine(EndName).concat(T)); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3953 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3954 | llvm::Constant *Data[] = {ImgBegin, ImgEnd, HostEntriesBegin, |
| 3955 | HostEntriesEnd}; |
| 3956 | createConstantGlobalStructAndAddToParent(CGM, getTgtDeviceImageQTy(), Data, |
| 3957 | DeviceImagesEntries); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3958 | } |
| 3959 | |
| 3960 | // Create device images global array. |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 3961 | std::string ImagesName = getName({"omp_offloading", "device_images"}); |
John McCall | 6c9f1fdb | 2016-11-19 08:17:24 +0000 | [diff] [blame] | 3962 | llvm::GlobalVariable *DeviceImages = |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 3963 | DeviceImagesEntries.finishAndCreateGlobal(ImagesName, |
| 3964 | CGM.getPointerAlign(), |
| 3965 | /*isConstant=*/true); |
Peter Collingbourne | bcf909d | 2016-06-14 21:02:05 +0000 | [diff] [blame] | 3966 | DeviceImages->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3967 | |
| 3968 | // This is a Zero array to be used in the creation of the constant expressions |
| 3969 | llvm::Constant *Index[] = {llvm::Constant::getNullValue(CGM.Int32Ty), |
| 3970 | llvm::Constant::getNullValue(CGM.Int32Ty)}; |
| 3971 | |
| 3972 | // Create the target region descriptor. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3973 | llvm::Constant *Data[] = { |
| 3974 | llvm::ConstantInt::get(CGM.Int32Ty, Devices.size()), |
| 3975 | llvm::ConstantExpr::getGetElementPtr(DeviceImages->getValueType(), |
| 3976 | DeviceImages, Index), |
| 3977 | HostEntriesBegin, HostEntriesEnd}; |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 3978 | std::string Descriptor = getName({"omp_offloading", "descriptor"}); |
Mike Rice | e1ca7b6 | 2018-08-29 15:45:11 +0000 | [diff] [blame] | 3979 | llvm::GlobalVariable *Desc = createGlobalStruct( |
| 3980 | CGM, getTgtBinaryDescriptorQTy(), /*IsConstant=*/true, Data, Descriptor); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3981 | |
| 3982 | // Emit code to register or unregister the descriptor at execution |
| 3983 | // startup or closing, respectively. |
| 3984 | |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 3985 | llvm::Function *UnRegFn; |
| 3986 | { |
| 3987 | FunctionArgList Args; |
| 3988 | ImplicitParamDecl DummyPtr(C, C.VoidPtrTy, ImplicitParamDecl::Other); |
| 3989 | Args.push_back(&DummyPtr); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3990 | |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 3991 | CodeGenFunction CGF(CGM); |
| 3992 | // Disable debug info for global (de-)initializer because they are not part |
| 3993 | // of some particular construct. |
| 3994 | CGF.disableDebugInfo(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 3995 | const auto &FI = |
| 3996 | CGM.getTypes().arrangeBuiltinFunctionDeclaration(C.VoidTy, Args); |
| 3997 | llvm::FunctionType *FTy = CGM.getTypes().GetFunctionType(FI); |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 3998 | std::string UnregName = getName({"omp_offloading", "descriptor_unreg"}); |
| 3999 | UnRegFn = CGM.CreateGlobalInitOrDestructFunction(FTy, UnregName, FI); |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 4000 | CGF.StartFunction(GlobalDecl(), C.VoidTy, UnRegFn, FI, Args); |
| 4001 | CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__tgt_unregister_lib), |
| 4002 | Desc); |
| 4003 | CGF.FinishFunction(); |
| 4004 | } |
| 4005 | llvm::Function *RegFn; |
| 4006 | { |
| 4007 | CodeGenFunction CGF(CGM); |
| 4008 | // Disable debug info for global (de-)initializer because they are not part |
| 4009 | // of some particular construct. |
| 4010 | CGF.disableDebugInfo(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4011 | const auto &FI = CGM.getTypes().arrangeNullaryFunction(); |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 4012 | llvm::FunctionType *FTy = CGM.getTypes().GetFunctionType(FI); |
Sergey Dmitriev | bde9cf9 | 2018-08-03 20:19:28 +0000 | [diff] [blame] | 4013 | |
| 4014 | // Encode offload target triples into the registration function name. It |
| 4015 | // will serve as a comdat key for the registration/unregistration code for |
| 4016 | // this particular combination of offloading targets. |
| 4017 | SmallVector<StringRef, 4U> RegFnNameParts(Devices.size() + 2U); |
| 4018 | RegFnNameParts[0] = "omp_offloading"; |
| 4019 | RegFnNameParts[1] = "descriptor_reg"; |
| 4020 | llvm::transform(Devices, std::next(RegFnNameParts.begin(), 2), |
| 4021 | [](const llvm::Triple &T) -> const std::string& { |
| 4022 | return T.getTriple(); |
| 4023 | }); |
| 4024 | llvm::sort(std::next(RegFnNameParts.begin(), 2), RegFnNameParts.end()); |
| 4025 | std::string Descriptor = getName(RegFnNameParts); |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 4026 | RegFn = CGM.CreateGlobalInitOrDestructFunction(FTy, Descriptor, FI); |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 4027 | CGF.StartFunction(GlobalDecl(), C.VoidTy, RegFn, FI, FunctionArgList()); |
| 4028 | CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__tgt_register_lib), Desc); |
| 4029 | // Create a variable to drive the registration and unregistration of the |
| 4030 | // descriptor, so we can reuse the logic that emits Ctors and Dtors. |
| 4031 | ImplicitParamDecl RegUnregVar(C, C.getTranslationUnitDecl(), |
| 4032 | SourceLocation(), nullptr, C.CharTy, |
| 4033 | ImplicitParamDecl::Other); |
| 4034 | CGM.getCXXABI().registerGlobalDtor(CGF, RegUnregVar, UnRegFn, Desc); |
| 4035 | CGF.FinishFunction(); |
| 4036 | } |
George Rokos | 29d0f00 | 2017-05-27 03:03:13 +0000 | [diff] [blame] | 4037 | if (CGM.supportsCOMDAT()) { |
| 4038 | // It is sufficient to call registration function only once, so create a |
| 4039 | // COMDAT group for registration/unregistration functions and associated |
| 4040 | // data. That would reduce startup time and code size. Registration |
| 4041 | // function serves as a COMDAT group key. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4042 | llvm::Comdat *ComdatKey = M.getOrInsertComdat(RegFn->getName()); |
George Rokos | 29d0f00 | 2017-05-27 03:03:13 +0000 | [diff] [blame] | 4043 | RegFn->setLinkage(llvm::GlobalValue::LinkOnceAnyLinkage); |
| 4044 | RegFn->setVisibility(llvm::GlobalValue::HiddenVisibility); |
| 4045 | RegFn->setComdat(ComdatKey); |
| 4046 | UnRegFn->setComdat(ComdatKey); |
| 4047 | DeviceImages->setComdat(ComdatKey); |
| 4048 | Desc->setComdat(ComdatKey); |
| 4049 | } |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4050 | return RegFn; |
| 4051 | } |
| 4052 | |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 4053 | void CGOpenMPRuntime::createOffloadEntry( |
| 4054 | llvm::Constant *ID, llvm::Constant *Addr, uint64_t Size, int32_t Flags, |
| 4055 | llvm::GlobalValue::LinkageTypes Linkage) { |
Samuel Antao | 2de62b0 | 2016-02-13 23:35:10 +0000 | [diff] [blame] | 4056 | StringRef Name = Addr->getName(); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4057 | llvm::Module &M = CGM.getModule(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4058 | llvm::LLVMContext &C = M.getContext(); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4059 | |
| 4060 | // Create constant string with the name. |
| 4061 | llvm::Constant *StrPtrInit = llvm::ConstantDataArray::getString(C, Name); |
| 4062 | |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 4063 | std::string StringName = getName({"omp_offloading", "entry_name"}); |
| 4064 | auto *Str = new llvm::GlobalVariable( |
| 4065 | M, StrPtrInit->getType(), /*isConstant=*/true, |
| 4066 | llvm::GlobalValue::InternalLinkage, StrPtrInit, StringName); |
Peter Collingbourne | bcf909d | 2016-06-14 21:02:05 +0000 | [diff] [blame] | 4067 | Str->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4068 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4069 | llvm::Constant *Data[] = {llvm::ConstantExpr::getBitCast(ID, CGM.VoidPtrTy), |
| 4070 | llvm::ConstantExpr::getBitCast(Str, CGM.Int8PtrTy), |
| 4071 | llvm::ConstantInt::get(CGM.SizeTy, Size), |
| 4072 | llvm::ConstantInt::get(CGM.Int32Ty, Flags), |
| 4073 | llvm::ConstantInt::get(CGM.Int32Ty, 0)}; |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 4074 | std::string EntryName = getName({"omp_offloading", "entry", ""}); |
Mike Rice | e1ca7b6 | 2018-08-29 15:45:11 +0000 | [diff] [blame] | 4075 | llvm::GlobalVariable *Entry = createGlobalStruct( |
| 4076 | CGM, getTgtOffloadEntryQTy(), /*IsConstant=*/true, Data, |
| 4077 | Twine(EntryName).concat(Name), llvm::GlobalValue::WeakAnyLinkage); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4078 | |
| 4079 | // 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] | 4080 | std::string Section = getName({"omp_offloading", "entries"}); |
| 4081 | Entry->setSection(Section); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4082 | } |
| 4083 | |
| 4084 | void CGOpenMPRuntime::createOffloadEntriesAndInfoMetadata() { |
| 4085 | // Emit the offloading entries and metadata so that the device codegen side |
Samuel Antao | 4c8035b | 2016-12-12 18:00:20 +0000 | [diff] [blame] | 4086 | // can easily figure out what to emit. The produced metadata looks like |
| 4087 | // this: |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4088 | // |
| 4089 | // !omp_offload.info = !{!1, ...} |
| 4090 | // |
| 4091 | // Right now we only generate metadata for function that contain target |
| 4092 | // regions. |
| 4093 | |
Alexander Kornienko | 2a8c18d | 2018-04-06 15:14:32 +0000 | [diff] [blame] | 4094 | // 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] | 4095 | if (OffloadEntriesInfoManager.empty()) |
| 4096 | return; |
| 4097 | |
| 4098 | llvm::Module &M = CGM.getModule(); |
| 4099 | llvm::LLVMContext &C = M.getContext(); |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 4100 | SmallVector<const OffloadEntriesInfoManagerTy::OffloadEntryInfo *, 16> |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4101 | OrderedEntries(OffloadEntriesInfoManager.size()); |
Alexey Bataev | 2a6f3f5 | 2018-11-07 19:11:14 +0000 | [diff] [blame] | 4102 | llvm::SmallVector<StringRef, 16> ParentFunctions( |
| 4103 | OffloadEntriesInfoManager.size()); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4104 | |
Simon Pilgrim | 2c51880 | 2017-03-30 14:13:19 +0000 | [diff] [blame] | 4105 | // Auxiliary methods to create metadata values and strings. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4106 | auto &&GetMDInt = [this](unsigned V) { |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4107 | return llvm::ConstantAsMetadata::get( |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4108 | llvm::ConstantInt::get(CGM.Int32Ty, V)); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4109 | }; |
| 4110 | |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 4111 | auto &&GetMDString = [&C](StringRef V) { return llvm::MDString::get(C, V); }; |
| 4112 | |
| 4113 | // Create the offloading info metadata node. |
| 4114 | llvm::NamedMDNode *MD = M.getOrInsertNamedMetadata("omp_offload.info"); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4115 | |
| 4116 | // Create function that emits metadata for each target region entry; |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 4117 | auto &&TargetRegionMetadataEmitter = |
Alexey Bataev | 2a6f3f5 | 2018-11-07 19:11:14 +0000 | [diff] [blame] | 4118 | [&C, MD, &OrderedEntries, &ParentFunctions, &GetMDInt, &GetMDString]( |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 4119 | unsigned DeviceID, unsigned FileID, StringRef ParentName, |
| 4120 | unsigned Line, |
| 4121 | const OffloadEntriesInfoManagerTy::OffloadEntryInfoTargetRegion &E) { |
| 4122 | // Generate metadata for target regions. Each entry of this metadata |
| 4123 | // contains: |
| 4124 | // - Entry 0 -> Kind of this type of metadata (0). |
| 4125 | // - Entry 1 -> Device ID of the file where the entry was identified. |
| 4126 | // - Entry 2 -> File ID of the file where the entry was identified. |
| 4127 | // - Entry 3 -> Mangled name of the function where the entry was |
| 4128 | // identified. |
| 4129 | // - Entry 4 -> Line in the file where the entry was identified. |
| 4130 | // - Entry 5 -> Order the entry was created. |
| 4131 | // The first element of the metadata node is the kind. |
| 4132 | llvm::Metadata *Ops[] = {GetMDInt(E.getKind()), GetMDInt(DeviceID), |
| 4133 | GetMDInt(FileID), GetMDString(ParentName), |
| 4134 | GetMDInt(Line), GetMDInt(E.getOrder())}; |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4135 | |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 4136 | // Save this entry in the right position of the ordered entries array. |
| 4137 | OrderedEntries[E.getOrder()] = &E; |
Alexey Bataev | 2a6f3f5 | 2018-11-07 19:11:14 +0000 | [diff] [blame] | 4138 | ParentFunctions[E.getOrder()] = ParentName; |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4139 | |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 4140 | // Add metadata to the named metadata node. |
| 4141 | MD->addOperand(llvm::MDNode::get(C, Ops)); |
| 4142 | }; |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4143 | |
| 4144 | OffloadEntriesInfoManager.actOnTargetRegionEntriesInfo( |
| 4145 | TargetRegionMetadataEmitter); |
| 4146 | |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 4147 | // Create function that emits metadata for each device global variable entry; |
| 4148 | auto &&DeviceGlobalVarMetadataEmitter = |
| 4149 | [&C, &OrderedEntries, &GetMDInt, &GetMDString, |
| 4150 | MD](StringRef MangledName, |
| 4151 | const OffloadEntriesInfoManagerTy::OffloadEntryInfoDeviceGlobalVar |
| 4152 | &E) { |
| 4153 | // Generate metadata for global variables. Each entry of this metadata |
| 4154 | // contains: |
| 4155 | // - Entry 0 -> Kind of this type of metadata (1). |
| 4156 | // - Entry 1 -> Mangled name of the variable. |
| 4157 | // - Entry 2 -> Declare target kind. |
| 4158 | // - Entry 3 -> Order the entry was created. |
| 4159 | // The first element of the metadata node is the kind. |
| 4160 | llvm::Metadata *Ops[] = { |
| 4161 | GetMDInt(E.getKind()), GetMDString(MangledName), |
| 4162 | GetMDInt(E.getFlags()), GetMDInt(E.getOrder())}; |
| 4163 | |
| 4164 | // Save this entry in the right position of the ordered entries array. |
| 4165 | OrderedEntries[E.getOrder()] = &E; |
| 4166 | |
| 4167 | // Add metadata to the named metadata node. |
| 4168 | MD->addOperand(llvm::MDNode::get(C, Ops)); |
| 4169 | }; |
| 4170 | |
| 4171 | OffloadEntriesInfoManager.actOnDeviceGlobalVarEntriesInfo( |
| 4172 | DeviceGlobalVarMetadataEmitter); |
| 4173 | |
| 4174 | for (const auto *E : OrderedEntries) { |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4175 | assert(E && "All ordered entries must exist!"); |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 4176 | if (const auto *CE = |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4177 | dyn_cast<OffloadEntriesInfoManagerTy::OffloadEntryInfoTargetRegion>( |
| 4178 | E)) { |
Alexey Bataev | 64e62dc | 2018-04-30 16:26:57 +0000 | [diff] [blame] | 4179 | if (!CE->getID() || !CE->getAddress()) { |
Alexey Bataev | 2a6f3f5 | 2018-11-07 19:11:14 +0000 | [diff] [blame] | 4180 | // Do not blame the entry if the parent funtion is not emitted. |
| 4181 | StringRef FnName = ParentFunctions[CE->getOrder()]; |
| 4182 | if (!CGM.GetGlobalValue(FnName)) |
| 4183 | continue; |
Alexey Bataev | 64e62dc | 2018-04-30 16:26:57 +0000 | [diff] [blame] | 4184 | unsigned DiagID = CGM.getDiags().getCustomDiagID( |
| 4185 | DiagnosticsEngine::Error, |
Alexey Bataev | 7f01d20 | 2018-07-16 18:12:18 +0000 | [diff] [blame] | 4186 | "Offloading entry for target region is incorrect: either the " |
Alexey Bataev | 64e62dc | 2018-04-30 16:26:57 +0000 | [diff] [blame] | 4187 | "address or the ID is invalid."); |
| 4188 | CGM.getDiags().Report(DiagID); |
| 4189 | continue; |
| 4190 | } |
Alexey Bataev | 34f8a70 | 2018-03-28 14:28:54 +0000 | [diff] [blame] | 4191 | createOffloadEntry(CE->getID(), CE->getAddress(), /*Size=*/0, |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 4192 | CE->getFlags(), llvm::GlobalValue::WeakAnyLinkage); |
| 4193 | } else if (const auto *CE = |
| 4194 | dyn_cast<OffloadEntriesInfoManagerTy:: |
| 4195 | OffloadEntryInfoDeviceGlobalVar>(E)) { |
Alexey Bataev | c52f01d | 2018-07-16 20:05:25 +0000 | [diff] [blame] | 4196 | OffloadEntriesInfoManagerTy::OMPTargetGlobalVarEntryKind Flags = |
| 4197 | static_cast<OffloadEntriesInfoManagerTy::OMPTargetGlobalVarEntryKind>( |
| 4198 | CE->getFlags()); |
| 4199 | switch (Flags) { |
| 4200 | case OffloadEntriesInfoManagerTy::OMPTargetGlobalVarEntryTo: { |
Gheorghe-Teodor Bercea | 0034e84 | 2019-06-20 18:04:47 +0000 | [diff] [blame] | 4201 | if (CGM.getLangOpts().OpenMPIsDevice && |
| 4202 | CGM.getOpenMPRuntime().hasRequiresUnifiedSharedMemory()) |
| 4203 | continue; |
Alexey Bataev | c52f01d | 2018-07-16 20:05:25 +0000 | [diff] [blame] | 4204 | if (!CE->getAddress()) { |
| 4205 | unsigned DiagID = CGM.getDiags().getCustomDiagID( |
| 4206 | DiagnosticsEngine::Error, |
| 4207 | "Offloading entry for declare target variable is incorrect: the " |
| 4208 | "address is invalid."); |
| 4209 | CGM.getDiags().Report(DiagID); |
| 4210 | continue; |
| 4211 | } |
Alexey Bataev | b4dd6d2 | 2018-08-29 20:41:37 +0000 | [diff] [blame] | 4212 | // The vaiable has no definition - no need to add the entry. |
| 4213 | if (CE->getVarSize().isZero()) |
| 4214 | continue; |
Alexey Bataev | c52f01d | 2018-07-16 20:05:25 +0000 | [diff] [blame] | 4215 | break; |
| 4216 | } |
| 4217 | case OffloadEntriesInfoManagerTy::OMPTargetGlobalVarEntryLink: |
| 4218 | assert(((CGM.getLangOpts().OpenMPIsDevice && !CE->getAddress()) || |
| 4219 | (!CGM.getLangOpts().OpenMPIsDevice && CE->getAddress())) && |
| 4220 | "Declaret target link address is set."); |
| 4221 | if (CGM.getLangOpts().OpenMPIsDevice) |
| 4222 | continue; |
| 4223 | if (!CE->getAddress()) { |
| 4224 | unsigned DiagID = CGM.getDiags().getCustomDiagID( |
| 4225 | DiagnosticsEngine::Error, |
| 4226 | "Offloading entry for declare target variable is incorrect: the " |
| 4227 | "address is invalid."); |
| 4228 | CGM.getDiags().Report(DiagID); |
| 4229 | continue; |
| 4230 | } |
| 4231 | break; |
Alexey Bataev | 64e62dc | 2018-04-30 16:26:57 +0000 | [diff] [blame] | 4232 | } |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 4233 | createOffloadEntry(CE->getAddress(), CE->getAddress(), |
Alexey Bataev | c52f01d | 2018-07-16 20:05:25 +0000 | [diff] [blame] | 4234 | CE->getVarSize().getQuantity(), Flags, |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 4235 | CE->getLinkage()); |
| 4236 | } else { |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4237 | llvm_unreachable("Unsupported entry kind."); |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 4238 | } |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4239 | } |
| 4240 | } |
| 4241 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4242 | /// Loads all the offload entries information from the host IR |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4243 | /// metadata. |
| 4244 | void CGOpenMPRuntime::loadOffloadInfoMetadata() { |
| 4245 | // If we are in target mode, load the metadata from the host IR. This code has |
| 4246 | // to match the metadaata creation in createOffloadEntriesAndInfoMetadata(). |
| 4247 | |
| 4248 | if (!CGM.getLangOpts().OpenMPIsDevice) |
| 4249 | return; |
| 4250 | |
| 4251 | if (CGM.getLangOpts().OMPHostIRFile.empty()) |
| 4252 | return; |
| 4253 | |
| 4254 | auto Buf = llvm::MemoryBuffer::getFile(CGM.getLangOpts().OMPHostIRFile); |
Alexey Bataev | 64e62dc | 2018-04-30 16:26:57 +0000 | [diff] [blame] | 4255 | if (auto EC = Buf.getError()) { |
| 4256 | CGM.getDiags().Report(diag::err_cannot_open_file) |
| 4257 | << CGM.getLangOpts().OMPHostIRFile << EC.message(); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4258 | return; |
Alexey Bataev | 64e62dc | 2018-04-30 16:26:57 +0000 | [diff] [blame] | 4259 | } |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4260 | |
| 4261 | llvm::LLVMContext C; |
Peter Collingbourne | d9445c4 | 2016-11-13 07:00:17 +0000 | [diff] [blame] | 4262 | auto ME = expectedToErrorOrAndEmitErrors( |
| 4263 | C, llvm::parseBitcodeFile(Buf.get()->getMemBufferRef(), C)); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4264 | |
Alexey Bataev | 64e62dc | 2018-04-30 16:26:57 +0000 | [diff] [blame] | 4265 | if (auto EC = ME.getError()) { |
| 4266 | unsigned DiagID = CGM.getDiags().getCustomDiagID( |
| 4267 | DiagnosticsEngine::Error, "Unable to parse host IR file '%0':'%1'"); |
| 4268 | CGM.getDiags().Report(DiagID) |
| 4269 | << CGM.getLangOpts().OMPHostIRFile << EC.message(); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4270 | return; |
Alexey Bataev | 64e62dc | 2018-04-30 16:26:57 +0000 | [diff] [blame] | 4271 | } |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4272 | |
| 4273 | llvm::NamedMDNode *MD = ME.get()->getNamedMetadata("omp_offload.info"); |
| 4274 | if (!MD) |
| 4275 | return; |
| 4276 | |
George Burgess IV | 00f70bd | 2018-03-01 05:43:23 +0000 | [diff] [blame] | 4277 | for (llvm::MDNode *MN : MD->operands()) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4278 | auto &&GetMDInt = [MN](unsigned Idx) { |
| 4279 | auto *V = cast<llvm::ConstantAsMetadata>(MN->getOperand(Idx)); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4280 | return cast<llvm::ConstantInt>(V->getValue())->getZExtValue(); |
| 4281 | }; |
| 4282 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4283 | auto &&GetMDString = [MN](unsigned Idx) { |
| 4284 | auto *V = cast<llvm::MDString>(MN->getOperand(Idx)); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4285 | return V->getString(); |
| 4286 | }; |
| 4287 | |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 4288 | switch (GetMDInt(0)) { |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4289 | default: |
| 4290 | llvm_unreachable("Unexpected metadata!"); |
| 4291 | break; |
| 4292 | case OffloadEntriesInfoManagerTy::OffloadEntryInfo:: |
Alexey Bataev | 34f8a70 | 2018-03-28 14:28:54 +0000 | [diff] [blame] | 4293 | OffloadingEntryInfoTargetRegion: |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4294 | OffloadEntriesInfoManager.initializeTargetRegionEntryInfo( |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 4295 | /*DeviceID=*/GetMDInt(1), /*FileID=*/GetMDInt(2), |
| 4296 | /*ParentName=*/GetMDString(3), /*Line=*/GetMDInt(4), |
| 4297 | /*Order=*/GetMDInt(5)); |
| 4298 | break; |
| 4299 | case OffloadEntriesInfoManagerTy::OffloadEntryInfo:: |
| 4300 | OffloadingEntryInfoDeviceGlobalVar: |
| 4301 | OffloadEntriesInfoManager.initializeDeviceGlobalVarEntryInfo( |
| 4302 | /*MangledName=*/GetMDString(1), |
| 4303 | static_cast<OffloadEntriesInfoManagerTy::OMPTargetGlobalVarEntryKind>( |
| 4304 | /*Flags=*/GetMDInt(2)), |
| 4305 | /*Order=*/GetMDInt(3)); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4306 | break; |
| 4307 | } |
| 4308 | } |
| 4309 | } |
| 4310 | |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 4311 | void CGOpenMPRuntime::emitKmpRoutineEntryT(QualType KmpInt32Ty) { |
| 4312 | if (!KmpRoutineEntryPtrTy) { |
| 4313 | // Build typedef kmp_int32 (* kmp_routine_entry_t)(kmp_int32, void *); type. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4314 | ASTContext &C = CGM.getContext(); |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 4315 | QualType KmpRoutineEntryTyArgs[] = {KmpInt32Ty, C.VoidPtrTy}; |
| 4316 | FunctionProtoType::ExtProtoInfo EPI; |
| 4317 | KmpRoutineEntryPtrQTy = C.getPointerType( |
| 4318 | C.getFunctionType(KmpInt32Ty, KmpRoutineEntryTyArgs, EPI)); |
| 4319 | KmpRoutineEntryPtrTy = CGM.getTypes().ConvertType(KmpRoutineEntryPtrQTy); |
| 4320 | } |
| 4321 | } |
| 4322 | |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4323 | QualType CGOpenMPRuntime::getTgtOffloadEntryQTy() { |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4324 | // Make sure the type of the entry is already created. This is the type we |
| 4325 | // have to create: |
| 4326 | // struct __tgt_offload_entry{ |
| 4327 | // void *addr; // Pointer to the offload entry info. |
| 4328 | // // (function or global) |
| 4329 | // char *name; // Name of the function or global. |
| 4330 | // 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] | 4331 | // int32_t flags; // Flags associated with the entry, e.g. 'link'. |
| 4332 | // int32_t reserved; // Reserved, to use by the runtime library. |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4333 | // }; |
| 4334 | if (TgtOffloadEntryQTy.isNull()) { |
| 4335 | ASTContext &C = CGM.getContext(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4336 | RecordDecl *RD = C.buildImplicitRecord("__tgt_offload_entry"); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4337 | RD->startDefinition(); |
| 4338 | addFieldToRecordDecl(C, RD, C.VoidPtrTy); |
| 4339 | addFieldToRecordDecl(C, RD, C.getPointerType(C.CharTy)); |
| 4340 | addFieldToRecordDecl(C, RD, C.getSizeType()); |
Samuel Antao | f83efdb | 2017-01-05 16:02:49 +0000 | [diff] [blame] | 4341 | addFieldToRecordDecl( |
| 4342 | C, RD, C.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/true)); |
| 4343 | addFieldToRecordDecl( |
| 4344 | C, RD, C.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/true)); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4345 | RD->completeDefinition(); |
Jonas Hahnfeld | 5e4df28 | 2018-01-18 15:38:03 +0000 | [diff] [blame] | 4346 | RD->addAttr(PackedAttr::CreateImplicit(C)); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4347 | TgtOffloadEntryQTy = C.getRecordType(RD); |
| 4348 | } |
| 4349 | return TgtOffloadEntryQTy; |
| 4350 | } |
| 4351 | |
| 4352 | QualType CGOpenMPRuntime::getTgtDeviceImageQTy() { |
| 4353 | // These are the types we need to build: |
| 4354 | // struct __tgt_device_image{ |
| 4355 | // void *ImageStart; // Pointer to the target code start. |
| 4356 | // void *ImageEnd; // Pointer to the target code end. |
| 4357 | // // We also add the host entries to the device image, as it may be useful |
| 4358 | // // for the target runtime to have access to that information. |
| 4359 | // __tgt_offload_entry *EntriesBegin; // Begin of the table with all |
| 4360 | // // the entries. |
| 4361 | // __tgt_offload_entry *EntriesEnd; // End of the table with all the |
| 4362 | // // entries (non inclusive). |
| 4363 | // }; |
| 4364 | if (TgtDeviceImageQTy.isNull()) { |
| 4365 | ASTContext &C = CGM.getContext(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4366 | RecordDecl *RD = C.buildImplicitRecord("__tgt_device_image"); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4367 | RD->startDefinition(); |
| 4368 | addFieldToRecordDecl(C, RD, C.VoidPtrTy); |
| 4369 | addFieldToRecordDecl(C, RD, C.VoidPtrTy); |
| 4370 | addFieldToRecordDecl(C, RD, C.getPointerType(getTgtOffloadEntryQTy())); |
| 4371 | addFieldToRecordDecl(C, RD, C.getPointerType(getTgtOffloadEntryQTy())); |
| 4372 | RD->completeDefinition(); |
| 4373 | TgtDeviceImageQTy = C.getRecordType(RD); |
| 4374 | } |
| 4375 | return TgtDeviceImageQTy; |
| 4376 | } |
| 4377 | |
| 4378 | QualType CGOpenMPRuntime::getTgtBinaryDescriptorQTy() { |
| 4379 | // struct __tgt_bin_desc{ |
| 4380 | // int32_t NumDevices; // Number of devices supported. |
| 4381 | // __tgt_device_image *DeviceImages; // Arrays of device images |
| 4382 | // // (one per device). |
| 4383 | // __tgt_offload_entry *EntriesBegin; // Begin of the table with all the |
| 4384 | // // entries. |
| 4385 | // __tgt_offload_entry *EntriesEnd; // End of the table with all the |
| 4386 | // // entries (non inclusive). |
| 4387 | // }; |
| 4388 | if (TgtBinaryDescriptorQTy.isNull()) { |
| 4389 | ASTContext &C = CGM.getContext(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4390 | RecordDecl *RD = C.buildImplicitRecord("__tgt_bin_desc"); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4391 | RD->startDefinition(); |
| 4392 | addFieldToRecordDecl( |
| 4393 | C, RD, C.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/true)); |
| 4394 | addFieldToRecordDecl(C, RD, C.getPointerType(getTgtDeviceImageQTy())); |
| 4395 | addFieldToRecordDecl(C, RD, C.getPointerType(getTgtOffloadEntryQTy())); |
| 4396 | addFieldToRecordDecl(C, RD, C.getPointerType(getTgtOffloadEntryQTy())); |
| 4397 | RD->completeDefinition(); |
| 4398 | TgtBinaryDescriptorQTy = C.getRecordType(RD); |
| 4399 | } |
| 4400 | return TgtBinaryDescriptorQTy; |
| 4401 | } |
| 4402 | |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 4403 | namespace { |
Alexey Bataev | 9e03404 | 2015-05-05 04:05:12 +0000 | [diff] [blame] | 4404 | struct PrivateHelpersTy { |
| 4405 | PrivateHelpersTy(const VarDecl *Original, const VarDecl *PrivateCopy, |
| 4406 | const VarDecl *PrivateElemInit) |
| 4407 | : Original(Original), PrivateCopy(PrivateCopy), |
| 4408 | PrivateElemInit(PrivateElemInit) {} |
| 4409 | const VarDecl *Original; |
| 4410 | const VarDecl *PrivateCopy; |
| 4411 | const VarDecl *PrivateElemInit; |
| 4412 | }; |
| 4413 | typedef std::pair<CharUnits /*Align*/, PrivateHelpersTy> PrivateDataTy; |
Hans Wennborg | 7eb5464 | 2015-09-10 17:07:54 +0000 | [diff] [blame] | 4414 | } // anonymous namespace |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 4415 | |
Alexey Bataev | 9e03404 | 2015-05-05 04:05:12 +0000 | [diff] [blame] | 4416 | static RecordDecl * |
Craig Topper | 8674c5c | 2015-09-29 04:30:07 +0000 | [diff] [blame] | 4417 | createPrivatesRecordDecl(CodeGenModule &CGM, ArrayRef<PrivateDataTy> Privates) { |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 4418 | if (!Privates.empty()) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4419 | ASTContext &C = CGM.getContext(); |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 4420 | // Build struct .kmp_privates_t. { |
| 4421 | // /* private vars */ |
| 4422 | // }; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4423 | RecordDecl *RD = C.buildImplicitRecord(".kmp_privates.t"); |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 4424 | RD->startDefinition(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4425 | for (const auto &Pair : Privates) { |
| 4426 | const VarDecl *VD = Pair.second.Original; |
| 4427 | QualType Type = VD->getType().getNonReferenceType(); |
| 4428 | FieldDecl *FD = addFieldToRecordDecl(C, RD, Type); |
Alexey Bataev | c71a409 | 2015-09-11 10:29:41 +0000 | [diff] [blame] | 4429 | if (VD->hasAttrs()) { |
| 4430 | for (specific_attr_iterator<AlignedAttr> I(VD->getAttrs().begin()), |
| 4431 | E(VD->getAttrs().end()); |
| 4432 | I != E; ++I) |
| 4433 | FD->addAttr(*I); |
| 4434 | } |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 4435 | } |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 4436 | RD->completeDefinition(); |
| 4437 | return RD; |
| 4438 | } |
| 4439 | return nullptr; |
| 4440 | } |
| 4441 | |
Alexey Bataev | 9e03404 | 2015-05-05 04:05:12 +0000 | [diff] [blame] | 4442 | static RecordDecl * |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4443 | createKmpTaskTRecordDecl(CodeGenModule &CGM, OpenMPDirectiveKind Kind, |
| 4444 | QualType KmpInt32Ty, |
Alexey Bataev | 8fc69dc | 2015-05-18 07:54:53 +0000 | [diff] [blame] | 4445 | QualType KmpRoutineEntryPointerQTy) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4446 | ASTContext &C = CGM.getContext(); |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 4447 | // Build struct kmp_task_t { |
| 4448 | // void * shareds; |
| 4449 | // kmp_routine_entry_t routine; |
| 4450 | // kmp_int32 part_id; |
Alexey Bataev | ad537bb | 2016-05-30 09:06:50 +0000 | [diff] [blame] | 4451 | // kmp_cmplrdata_t data1; |
| 4452 | // kmp_cmplrdata_t data2; |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4453 | // For taskloops additional fields: |
| 4454 | // kmp_uint64 lb; |
| 4455 | // kmp_uint64 ub; |
| 4456 | // kmp_int64 st; |
| 4457 | // kmp_int32 liter; |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 4458 | // void * reductions; |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 4459 | // }; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4460 | RecordDecl *UD = C.buildImplicitRecord("kmp_cmplrdata_t", TTK_Union); |
Alexey Bataev | ad537bb | 2016-05-30 09:06:50 +0000 | [diff] [blame] | 4461 | UD->startDefinition(); |
| 4462 | addFieldToRecordDecl(C, UD, KmpInt32Ty); |
| 4463 | addFieldToRecordDecl(C, UD, KmpRoutineEntryPointerQTy); |
| 4464 | UD->completeDefinition(); |
| 4465 | QualType KmpCmplrdataTy = C.getRecordType(UD); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4466 | RecordDecl *RD = C.buildImplicitRecord("kmp_task_t"); |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 4467 | RD->startDefinition(); |
| 4468 | addFieldToRecordDecl(C, RD, C.VoidPtrTy); |
| 4469 | addFieldToRecordDecl(C, RD, KmpRoutineEntryPointerQTy); |
| 4470 | addFieldToRecordDecl(C, RD, KmpInt32Ty); |
Alexey Bataev | ad537bb | 2016-05-30 09:06:50 +0000 | [diff] [blame] | 4471 | addFieldToRecordDecl(C, RD, KmpCmplrdataTy); |
| 4472 | addFieldToRecordDecl(C, RD, KmpCmplrdataTy); |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4473 | if (isOpenMPTaskLoopDirective(Kind)) { |
| 4474 | QualType KmpUInt64Ty = |
| 4475 | CGM.getContext().getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0); |
| 4476 | QualType KmpInt64Ty = |
| 4477 | CGM.getContext().getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1); |
| 4478 | addFieldToRecordDecl(C, RD, KmpUInt64Ty); |
| 4479 | addFieldToRecordDecl(C, RD, KmpUInt64Ty); |
| 4480 | addFieldToRecordDecl(C, RD, KmpInt64Ty); |
| 4481 | addFieldToRecordDecl(C, RD, KmpInt32Ty); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 4482 | addFieldToRecordDecl(C, RD, C.VoidPtrTy); |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4483 | } |
Alexey Bataev | 8fc69dc | 2015-05-18 07:54:53 +0000 | [diff] [blame] | 4484 | RD->completeDefinition(); |
| 4485 | return RD; |
| 4486 | } |
| 4487 | |
| 4488 | static RecordDecl * |
| 4489 | createKmpTaskTWithPrivatesRecordDecl(CodeGenModule &CGM, QualType KmpTaskTQTy, |
Craig Topper | 8674c5c | 2015-09-29 04:30:07 +0000 | [diff] [blame] | 4490 | ArrayRef<PrivateDataTy> Privates) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4491 | ASTContext &C = CGM.getContext(); |
Alexey Bataev | 8fc69dc | 2015-05-18 07:54:53 +0000 | [diff] [blame] | 4492 | // Build struct kmp_task_t_with_privates { |
| 4493 | // kmp_task_t task_data; |
| 4494 | // .kmp_privates_t. privates; |
| 4495 | // }; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4496 | RecordDecl *RD = C.buildImplicitRecord("kmp_task_t_with_privates"); |
Alexey Bataev | 8fc69dc | 2015-05-18 07:54:53 +0000 | [diff] [blame] | 4497 | RD->startDefinition(); |
| 4498 | addFieldToRecordDecl(C, RD, KmpTaskTQTy); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4499 | if (const RecordDecl *PrivateRD = createPrivatesRecordDecl(CGM, Privates)) |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 4500 | addFieldToRecordDecl(C, RD, C.getRecordType(PrivateRD)); |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 4501 | RD->completeDefinition(); |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 4502 | return RD; |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 4503 | } |
| 4504 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4505 | /// Emit a proxy function which accepts kmp_task_t as the second |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 4506 | /// argument. |
| 4507 | /// \code |
| 4508 | /// kmp_int32 .omp_task_entry.(kmp_int32 gtid, kmp_task_t *tt) { |
Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 4509 | /// TaskFunction(gtid, tt->part_id, &tt->privates, task_privates_map, tt, |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4510 | /// For taskloops: |
| 4511 | /// 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] | 4512 | /// tt->reductions, tt->shareds); |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 4513 | /// return 0; |
| 4514 | /// } |
| 4515 | /// \endcode |
James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 4516 | static llvm::Function * |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 4517 | emitProxyTaskFunction(CodeGenModule &CGM, SourceLocation Loc, |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4518 | OpenMPDirectiveKind Kind, QualType KmpInt32Ty, |
| 4519 | QualType KmpTaskTWithPrivatesPtrQTy, |
Alexey Bataev | 8fc69dc | 2015-05-18 07:54:53 +0000 | [diff] [blame] | 4520 | QualType KmpTaskTWithPrivatesQTy, QualType KmpTaskTQTy, |
James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 4521 | QualType SharedsPtrTy, llvm::Function *TaskFunction, |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4522 | llvm::Value *TaskPrivatesMap) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4523 | ASTContext &C = CGM.getContext(); |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 4524 | FunctionArgList Args; |
Alexey Bataev | 5622323 | 2017-06-09 13:40:18 +0000 | [diff] [blame] | 4525 | ImplicitParamDecl GtidArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, KmpInt32Ty, |
| 4526 | ImplicitParamDecl::Other); |
| 4527 | ImplicitParamDecl TaskTypeArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, |
| 4528 | KmpTaskTWithPrivatesPtrQTy.withRestrict(), |
| 4529 | ImplicitParamDecl::Other); |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 4530 | Args.push_back(&GtidArg); |
| 4531 | Args.push_back(&TaskTypeArg); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4532 | const auto &TaskEntryFnInfo = |
John McCall | c56a8b3 | 2016-03-11 04:30:31 +0000 | [diff] [blame] | 4533 | CGM.getTypes().arrangeBuiltinFunctionDeclaration(KmpInt32Ty, Args); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4534 | llvm::FunctionType *TaskEntryTy = |
| 4535 | CGM.getTypes().GetFunctionType(TaskEntryFnInfo); |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 4536 | std::string Name = CGM.getOpenMPRuntime().getName({"omp_task_entry", ""}); |
| 4537 | auto *TaskEntry = llvm::Function::Create( |
| 4538 | TaskEntryTy, llvm::GlobalValue::InternalLinkage, Name, &CGM.getModule()); |
Rafael Espindola | 51ec5a9 | 2018-02-28 23:46:35 +0000 | [diff] [blame] | 4539 | CGM.SetInternalFunctionAttributes(GlobalDecl(), TaskEntry, TaskEntryFnInfo); |
Alexey Bataev | c0f879b | 2018-04-10 20:10:53 +0000 | [diff] [blame] | 4540 | TaskEntry->setDoesNotRecurse(); |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 4541 | CodeGenFunction CGF(CGM); |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 4542 | CGF.StartFunction(GlobalDecl(), KmpInt32Ty, TaskEntry, TaskEntryFnInfo, Args, |
| 4543 | Loc, Loc); |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 4544 | |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4545 | // TaskFunction(gtid, tt->task_data.part_id, &tt->privates, task_privates_map, |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4546 | // tt, |
| 4547 | // For taskloops: |
| 4548 | // tt->task_data.lb, tt->task_data.ub, tt->task_data.st, tt->task_data.liter, |
| 4549 | // tt->task_data.shareds); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4550 | llvm::Value *GtidParam = CGF.EmitLoadOfScalar( |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4551 | CGF.GetAddrOfLocalVar(&GtidArg), /*Volatile=*/false, KmpInt32Ty, Loc); |
Alexey Bataev | 31300ed | 2016-02-04 11:27:03 +0000 | [diff] [blame] | 4552 | LValue TDBase = CGF.EmitLoadOfPointerLValue( |
| 4553 | CGF.GetAddrOfLocalVar(&TaskTypeArg), |
| 4554 | KmpTaskTWithPrivatesPtrQTy->castAs<PointerType>()); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4555 | const auto *KmpTaskTWithPrivatesQTyRD = |
Alexey Bataev | 8fc69dc | 2015-05-18 07:54:53 +0000 | [diff] [blame] | 4556 | cast<RecordDecl>(KmpTaskTWithPrivatesQTy->getAsTagDecl()); |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4557 | LValue Base = |
| 4558 | CGF.EmitLValueForField(TDBase, *KmpTaskTWithPrivatesQTyRD->field_begin()); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4559 | const auto *KmpTaskTQTyRD = cast<RecordDecl>(KmpTaskTQTy->getAsTagDecl()); |
Alexey Bataev | 8fc69dc | 2015-05-18 07:54:53 +0000 | [diff] [blame] | 4560 | auto PartIdFI = std::next(KmpTaskTQTyRD->field_begin(), KmpTaskTPartId); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4561 | LValue PartIdLVal = CGF.EmitLValueForField(Base, *PartIdFI); |
| 4562 | llvm::Value *PartidParam = PartIdLVal.getPointer(); |
Alexey Bataev | 8fc69dc | 2015-05-18 07:54:53 +0000 | [diff] [blame] | 4563 | |
| 4564 | auto SharedsFI = std::next(KmpTaskTQTyRD->field_begin(), KmpTaskTShareds); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4565 | LValue SharedsLVal = CGF.EmitLValueForField(Base, *SharedsFI); |
| 4566 | llvm::Value *SharedsParam = CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
Alexey Bataev | 1e49137 | 2018-01-23 18:44:14 +0000 | [diff] [blame] | 4567 | CGF.EmitLoadOfScalar(SharedsLVal, Loc), |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 4568 | CGF.ConvertTypeForMem(SharedsPtrTy)); |
| 4569 | |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4570 | auto PrivatesFI = std::next(KmpTaskTWithPrivatesQTyRD->field_begin(), 1); |
| 4571 | llvm::Value *PrivatesParam; |
| 4572 | if (PrivatesFI != KmpTaskTWithPrivatesQTyRD->field_end()) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4573 | LValue PrivatesLVal = CGF.EmitLValueForField(TDBase, *PrivatesFI); |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4574 | PrivatesParam = CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4575 | PrivatesLVal.getPointer(), CGF.VoidPtrTy); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4576 | } else { |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4577 | PrivatesParam = llvm::ConstantPointerNull::get(CGF.VoidPtrTy); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4578 | } |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4579 | |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4580 | llvm::Value *CommonArgs[] = {GtidParam, PartidParam, PrivatesParam, |
| 4581 | TaskPrivatesMap, |
| 4582 | CGF.Builder |
| 4583 | .CreatePointerBitCastOrAddrSpaceCast( |
| 4584 | TDBase.getAddress(), CGF.VoidPtrTy) |
| 4585 | .getPointer()}; |
| 4586 | SmallVector<llvm::Value *, 16> CallArgs(std::begin(CommonArgs), |
| 4587 | std::end(CommonArgs)); |
| 4588 | if (isOpenMPTaskLoopDirective(Kind)) { |
| 4589 | auto LBFI = std::next(KmpTaskTQTyRD->field_begin(), KmpTaskTLowerBound); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4590 | LValue LBLVal = CGF.EmitLValueForField(Base, *LBFI); |
| 4591 | llvm::Value *LBParam = CGF.EmitLoadOfScalar(LBLVal, Loc); |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4592 | auto UBFI = std::next(KmpTaskTQTyRD->field_begin(), KmpTaskTUpperBound); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4593 | LValue UBLVal = CGF.EmitLValueForField(Base, *UBFI); |
| 4594 | llvm::Value *UBParam = CGF.EmitLoadOfScalar(UBLVal, Loc); |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4595 | auto StFI = std::next(KmpTaskTQTyRD->field_begin(), KmpTaskTStride); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4596 | LValue StLVal = CGF.EmitLValueForField(Base, *StFI); |
| 4597 | llvm::Value *StParam = CGF.EmitLoadOfScalar(StLVal, Loc); |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4598 | auto LIFI = std::next(KmpTaskTQTyRD->field_begin(), KmpTaskTLastIter); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4599 | LValue LILVal = CGF.EmitLValueForField(Base, *LIFI); |
| 4600 | llvm::Value *LIParam = CGF.EmitLoadOfScalar(LILVal, Loc); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 4601 | auto RFI = std::next(KmpTaskTQTyRD->field_begin(), KmpTaskTReductions); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4602 | LValue RLVal = CGF.EmitLValueForField(Base, *RFI); |
| 4603 | llvm::Value *RParam = CGF.EmitLoadOfScalar(RLVal, Loc); |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4604 | CallArgs.push_back(LBParam); |
| 4605 | CallArgs.push_back(UBParam); |
| 4606 | CallArgs.push_back(StParam); |
| 4607 | CallArgs.push_back(LIParam); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 4608 | CallArgs.push_back(RParam); |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4609 | } |
| 4610 | CallArgs.push_back(SharedsParam); |
| 4611 | |
Alexey Bataev | 3c595a6 | 2017-08-14 15:01:03 +0000 | [diff] [blame] | 4612 | CGM.getOpenMPRuntime().emitOutlinedFunctionCall(CGF, Loc, TaskFunction, |
| 4613 | CallArgs); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4614 | CGF.EmitStoreThroughLValue(RValue::get(CGF.Builder.getInt32(/*C=*/0)), |
| 4615 | CGF.MakeAddrLValue(CGF.ReturnValue, KmpInt32Ty)); |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 4616 | CGF.FinishFunction(); |
| 4617 | return TaskEntry; |
| 4618 | } |
| 4619 | |
Alexey Bataev | 8fc69dc | 2015-05-18 07:54:53 +0000 | [diff] [blame] | 4620 | static llvm::Value *emitDestructorsFunction(CodeGenModule &CGM, |
| 4621 | SourceLocation Loc, |
| 4622 | QualType KmpInt32Ty, |
| 4623 | QualType KmpTaskTWithPrivatesPtrQTy, |
| 4624 | QualType KmpTaskTWithPrivatesQTy) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4625 | ASTContext &C = CGM.getContext(); |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 4626 | FunctionArgList Args; |
Alexey Bataev | 5622323 | 2017-06-09 13:40:18 +0000 | [diff] [blame] | 4627 | ImplicitParamDecl GtidArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, KmpInt32Ty, |
| 4628 | ImplicitParamDecl::Other); |
| 4629 | ImplicitParamDecl TaskTypeArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, |
| 4630 | KmpTaskTWithPrivatesPtrQTy.withRestrict(), |
| 4631 | ImplicitParamDecl::Other); |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 4632 | Args.push_back(&GtidArg); |
| 4633 | Args.push_back(&TaskTypeArg); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4634 | const auto &DestructorFnInfo = |
John McCall | c56a8b3 | 2016-03-11 04:30:31 +0000 | [diff] [blame] | 4635 | CGM.getTypes().arrangeBuiltinFunctionDeclaration(KmpInt32Ty, Args); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4636 | llvm::FunctionType *DestructorFnTy = |
| 4637 | CGM.getTypes().GetFunctionType(DestructorFnInfo); |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 4638 | std::string Name = |
| 4639 | CGM.getOpenMPRuntime().getName({"omp_task_destructor", ""}); |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 4640 | auto *DestructorFn = |
| 4641 | llvm::Function::Create(DestructorFnTy, llvm::GlobalValue::InternalLinkage, |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 4642 | Name, &CGM.getModule()); |
Rafael Espindola | 51ec5a9 | 2018-02-28 23:46:35 +0000 | [diff] [blame] | 4643 | CGM.SetInternalFunctionAttributes(GlobalDecl(), DestructorFn, |
Akira Hatanaka | 44a59f8 | 2015-10-28 02:30:47 +0000 | [diff] [blame] | 4644 | DestructorFnInfo); |
Alexey Bataev | c0f879b | 2018-04-10 20:10:53 +0000 | [diff] [blame] | 4645 | DestructorFn->setDoesNotRecurse(); |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 4646 | CodeGenFunction CGF(CGM); |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 4647 | CGF.StartFunction(GlobalDecl(), KmpInt32Ty, DestructorFn, DestructorFnInfo, |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 4648 | Args, Loc, Loc); |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 4649 | |
Alexey Bataev | 31300ed | 2016-02-04 11:27:03 +0000 | [diff] [blame] | 4650 | LValue Base = CGF.EmitLoadOfPointerLValue( |
| 4651 | CGF.GetAddrOfLocalVar(&TaskTypeArg), |
| 4652 | KmpTaskTWithPrivatesPtrQTy->castAs<PointerType>()); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4653 | const auto *KmpTaskTWithPrivatesQTyRD = |
Alexey Bataev | 8fc69dc | 2015-05-18 07:54:53 +0000 | [diff] [blame] | 4654 | cast<RecordDecl>(KmpTaskTWithPrivatesQTy->getAsTagDecl()); |
| 4655 | auto FI = std::next(KmpTaskTWithPrivatesQTyRD->field_begin()); |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 4656 | Base = CGF.EmitLValueForField(Base, *FI); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4657 | for (const auto *Field : |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 4658 | cast<RecordDecl>(FI->getType()->getAsTagDecl())->fields()) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4659 | if (QualType::DestructionKind DtorKind = |
| 4660 | Field->getType().isDestructedType()) { |
| 4661 | LValue FieldLValue = CGF.EmitLValueForField(Base, Field); |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 4662 | CGF.pushDestroy(DtorKind, FieldLValue.getAddress(), Field->getType()); |
| 4663 | } |
| 4664 | } |
| 4665 | CGF.FinishFunction(); |
| 4666 | return DestructorFn; |
| 4667 | } |
| 4668 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4669 | /// Emit a privates mapping function for correct handling of private and |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4670 | /// firstprivate variables. |
| 4671 | /// \code |
| 4672 | /// void .omp_task_privates_map.(const .privates. *noalias privs, <ty1> |
| 4673 | /// **noalias priv1,..., <tyn> **noalias privn) { |
| 4674 | /// *priv1 = &.privates.priv1; |
| 4675 | /// ...; |
| 4676 | /// *privn = &.privates.privn; |
| 4677 | /// } |
| 4678 | /// \endcode |
| 4679 | static llvm::Value * |
| 4680 | emitTaskPrivateMappingFunction(CodeGenModule &CGM, SourceLocation Loc, |
Craig Topper | 8674c5c | 2015-09-29 04:30:07 +0000 | [diff] [blame] | 4681 | ArrayRef<const Expr *> PrivateVars, |
| 4682 | ArrayRef<const Expr *> FirstprivateVars, |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4683 | ArrayRef<const Expr *> LastprivateVars, |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4684 | QualType PrivatesQTy, |
Craig Topper | 8674c5c | 2015-09-29 04:30:07 +0000 | [diff] [blame] | 4685 | ArrayRef<PrivateDataTy> Privates) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4686 | ASTContext &C = CGM.getContext(); |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4687 | FunctionArgList Args; |
| 4688 | ImplicitParamDecl TaskPrivatesArg( |
| 4689 | C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, |
Alexey Bataev | 5622323 | 2017-06-09 13:40:18 +0000 | [diff] [blame] | 4690 | C.getPointerType(PrivatesQTy).withConst().withRestrict(), |
| 4691 | ImplicitParamDecl::Other); |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4692 | Args.push_back(&TaskPrivatesArg); |
| 4693 | llvm::DenseMap<const VarDecl *, unsigned> PrivateVarsPos; |
| 4694 | unsigned Counter = 1; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4695 | for (const Expr *E : PrivateVars) { |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4696 | Args.push_back(ImplicitParamDecl::Create( |
Alexey Bataev | 5622323 | 2017-06-09 13:40:18 +0000 | [diff] [blame] | 4697 | C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, |
| 4698 | C.getPointerType(C.getPointerType(E->getType())) |
| 4699 | .withConst() |
| 4700 | .withRestrict(), |
| 4701 | ImplicitParamDecl::Other)); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4702 | const auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl()); |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4703 | PrivateVarsPos[VD] = Counter; |
| 4704 | ++Counter; |
| 4705 | } |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4706 | for (const Expr *E : FirstprivateVars) { |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4707 | Args.push_back(ImplicitParamDecl::Create( |
Alexey Bataev | 5622323 | 2017-06-09 13:40:18 +0000 | [diff] [blame] | 4708 | C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, |
| 4709 | C.getPointerType(C.getPointerType(E->getType())) |
| 4710 | .withConst() |
| 4711 | .withRestrict(), |
| 4712 | ImplicitParamDecl::Other)); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4713 | const auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl()); |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4714 | PrivateVarsPos[VD] = Counter; |
| 4715 | ++Counter; |
| 4716 | } |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4717 | for (const Expr *E : LastprivateVars) { |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4718 | Args.push_back(ImplicitParamDecl::Create( |
Alexey Bataev | 5622323 | 2017-06-09 13:40:18 +0000 | [diff] [blame] | 4719 | C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, |
| 4720 | C.getPointerType(C.getPointerType(E->getType())) |
| 4721 | .withConst() |
| 4722 | .withRestrict(), |
| 4723 | ImplicitParamDecl::Other)); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4724 | const auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl()); |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4725 | PrivateVarsPos[VD] = Counter; |
| 4726 | ++Counter; |
| 4727 | } |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4728 | const auto &TaskPrivatesMapFnInfo = |
John McCall | c56a8b3 | 2016-03-11 04:30:31 +0000 | [diff] [blame] | 4729 | CGM.getTypes().arrangeBuiltinFunctionDeclaration(C.VoidTy, Args); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4730 | llvm::FunctionType *TaskPrivatesMapTy = |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4731 | CGM.getTypes().GetFunctionType(TaskPrivatesMapFnInfo); |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 4732 | std::string Name = |
| 4733 | CGM.getOpenMPRuntime().getName({"omp_task_privates_map", ""}); |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4734 | auto *TaskPrivatesMap = llvm::Function::Create( |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 4735 | TaskPrivatesMapTy, llvm::GlobalValue::InternalLinkage, Name, |
| 4736 | &CGM.getModule()); |
Rafael Espindola | 51ec5a9 | 2018-02-28 23:46:35 +0000 | [diff] [blame] | 4737 | CGM.SetInternalFunctionAttributes(GlobalDecl(), TaskPrivatesMap, |
Akira Hatanaka | 44a59f8 | 2015-10-28 02:30:47 +0000 | [diff] [blame] | 4738 | TaskPrivatesMapFnInfo); |
Alexey Bataev | 8c5555c | 2019-05-21 15:11:58 +0000 | [diff] [blame] | 4739 | if (CGM.getLangOpts().Optimize) { |
| 4740 | TaskPrivatesMap->removeFnAttr(llvm::Attribute::NoInline); |
| 4741 | TaskPrivatesMap->removeFnAttr(llvm::Attribute::OptimizeNone); |
| 4742 | TaskPrivatesMap->addFnAttr(llvm::Attribute::AlwaysInline); |
| 4743 | } |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4744 | CodeGenFunction CGF(CGM); |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4745 | CGF.StartFunction(GlobalDecl(), C.VoidTy, TaskPrivatesMap, |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 4746 | TaskPrivatesMapFnInfo, Args, Loc, Loc); |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4747 | |
| 4748 | // *privi = &.privates.privi; |
Alexey Bataev | 31300ed | 2016-02-04 11:27:03 +0000 | [diff] [blame] | 4749 | LValue Base = CGF.EmitLoadOfPointerLValue( |
| 4750 | CGF.GetAddrOfLocalVar(&TaskPrivatesArg), |
| 4751 | TaskPrivatesArg.getType()->castAs<PointerType>()); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4752 | const auto *PrivatesQTyRD = cast<RecordDecl>(PrivatesQTy->getAsTagDecl()); |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4753 | Counter = 0; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4754 | for (const FieldDecl *Field : PrivatesQTyRD->fields()) { |
| 4755 | LValue FieldLVal = CGF.EmitLValueForField(Base, Field); |
| 4756 | const VarDecl *VD = Args[PrivateVarsPos[Privates[Counter].second.Original]]; |
| 4757 | LValue RefLVal = |
| 4758 | CGF.MakeAddrLValue(CGF.GetAddrOfLocalVar(VD), VD->getType()); |
| 4759 | LValue RefLoadLVal = CGF.EmitLoadOfPointerLValue( |
Alexey Bataev | 31300ed | 2016-02-04 11:27:03 +0000 | [diff] [blame] | 4760 | RefLVal.getAddress(), RefLVal.getType()->castAs<PointerType>()); |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 4761 | CGF.EmitStoreOfScalar(FieldLVal.getPointer(), RefLoadLVal); |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4762 | ++Counter; |
| 4763 | } |
| 4764 | CGF.FinishFunction(); |
| 4765 | return TaskPrivatesMap; |
| 4766 | } |
| 4767 | |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4768 | /// Emit initialization for private variables in task-based directives. |
Alexey Bataev | 8a83159 | 2016-05-10 10:36:51 +0000 | [diff] [blame] | 4769 | static void emitPrivatesInit(CodeGenFunction &CGF, |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4770 | const OMPExecutableDirective &D, |
| 4771 | Address KmpTaskSharedsPtr, LValue TDBase, |
| 4772 | const RecordDecl *KmpTaskTWithPrivatesQTyRD, |
| 4773 | QualType SharedsTy, QualType SharedsPtrTy, |
| 4774 | const OMPTaskDataTy &Data, |
| 4775 | ArrayRef<PrivateDataTy> Privates, bool ForDup) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4776 | ASTContext &C = CGF.getContext(); |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4777 | auto FI = std::next(KmpTaskTWithPrivatesQTyRD->field_begin()); |
| 4778 | LValue PrivatesBase = CGF.EmitLValueForField(TDBase, *FI); |
Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 4779 | OpenMPDirectiveKind Kind = isOpenMPTaskLoopDirective(D.getDirectiveKind()) |
| 4780 | ? OMPD_taskloop |
| 4781 | : OMPD_task; |
| 4782 | const CapturedStmt &CS = *D.getCapturedStmt(Kind); |
| 4783 | CodeGenFunction::CGCapturedStmtInfo CapturesInfo(CS); |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4784 | LValue SrcBase; |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 4785 | bool IsTargetTask = |
| 4786 | isOpenMPTargetDataManagementDirective(D.getDirectiveKind()) || |
| 4787 | isOpenMPTargetExecutionDirective(D.getDirectiveKind()); |
| 4788 | // For target-based directives skip 3 firstprivate arrays BasePointersArray, |
| 4789 | // PointersArray and SizesArray. The original variables for these arrays are |
| 4790 | // not captured and we get their addresses explicitly. |
| 4791 | if ((!IsTargetTask && !Data.FirstprivateVars.empty()) || |
Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 4792 | (IsTargetTask && KmpTaskSharedsPtr.isValid())) { |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4793 | SrcBase = CGF.MakeAddrLValue( |
| 4794 | CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
| 4795 | KmpTaskSharedsPtr, CGF.ConvertTypeForMem(SharedsPtrTy)), |
| 4796 | SharedsTy); |
| 4797 | } |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4798 | FI = cast<RecordDecl>(FI->getType()->getAsTagDecl())->field_begin(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4799 | for (const PrivateDataTy &Pair : Privates) { |
| 4800 | const VarDecl *VD = Pair.second.PrivateCopy; |
| 4801 | const Expr *Init = VD->getAnyInitializer(); |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4802 | if (Init && (!ForDup || (isa<CXXConstructExpr>(Init) && |
| 4803 | !CGF.isTrivialInitializer(Init)))) { |
Alexey Bataev | 8a83159 | 2016-05-10 10:36:51 +0000 | [diff] [blame] | 4804 | LValue PrivateLValue = CGF.EmitLValueForField(PrivatesBase, *FI); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4805 | if (const VarDecl *Elem = Pair.second.PrivateElemInit) { |
| 4806 | const VarDecl *OriginalVD = Pair.second.Original; |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 4807 | // Check if the variable is the target-based BasePointersArray, |
| 4808 | // PointersArray or SizesArray. |
| 4809 | LValue SharedRefLValue; |
Alexey Bataev | ab41ea6 | 2019-03-13 20:46:28 +0000 | [diff] [blame] | 4810 | QualType Type = PrivateLValue.getType(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4811 | const FieldDecl *SharedField = CapturesInfo.lookup(OriginalVD); |
Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 4812 | if (IsTargetTask && !SharedField) { |
| 4813 | assert(isa<ImplicitParamDecl>(OriginalVD) && |
| 4814 | isa<CapturedDecl>(OriginalVD->getDeclContext()) && |
| 4815 | cast<CapturedDecl>(OriginalVD->getDeclContext()) |
| 4816 | ->getNumParams() == 0 && |
| 4817 | isa<TranslationUnitDecl>( |
| 4818 | cast<CapturedDecl>(OriginalVD->getDeclContext()) |
| 4819 | ->getDeclContext()) && |
| 4820 | "Expected artificial target data variable."); |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 4821 | SharedRefLValue = |
| 4822 | CGF.MakeAddrLValue(CGF.GetAddrOfLocalVar(OriginalVD), Type); |
| 4823 | } else { |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 4824 | SharedRefLValue = CGF.EmitLValueForField(SrcBase, SharedField); |
| 4825 | SharedRefLValue = CGF.MakeAddrLValue( |
| 4826 | Address(SharedRefLValue.getPointer(), C.getDeclAlign(OriginalVD)), |
| 4827 | SharedRefLValue.getType(), LValueBaseInfo(AlignmentSource::Decl), |
| 4828 | SharedRefLValue.getTBAAInfo()); |
| 4829 | } |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4830 | if (Type->isArrayType()) { |
| 4831 | // Initialize firstprivate array. |
| 4832 | if (!isa<CXXConstructExpr>(Init) || CGF.isTrivialInitializer(Init)) { |
| 4833 | // Perform simple memcpy. |
Ivan A. Kosarev | 1860b52 | 2018-01-25 14:21:55 +0000 | [diff] [blame] | 4834 | CGF.EmitAggregateAssign(PrivateLValue, SharedRefLValue, Type); |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4835 | } else { |
| 4836 | // Initialize firstprivate array using element-by-element |
Simon Pilgrim | 2c51880 | 2017-03-30 14:13:19 +0000 | [diff] [blame] | 4837 | // initialization. |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4838 | CGF.EmitOMPAggregateAssign( |
| 4839 | PrivateLValue.getAddress(), SharedRefLValue.getAddress(), Type, |
| 4840 | [&CGF, Elem, Init, &CapturesInfo](Address DestElement, |
| 4841 | Address SrcElement) { |
| 4842 | // Clean up any temporaries needed by the initialization. |
| 4843 | CodeGenFunction::OMPPrivateScope InitScope(CGF); |
| 4844 | InitScope.addPrivate( |
| 4845 | Elem, [SrcElement]() -> Address { return SrcElement; }); |
| 4846 | (void)InitScope.Privatize(); |
| 4847 | // Emit initialization for single element. |
| 4848 | CodeGenFunction::CGCapturedStmtRAII CapInfoRAII( |
| 4849 | CGF, &CapturesInfo); |
| 4850 | CGF.EmitAnyExprToMem(Init, DestElement, |
| 4851 | Init->getType().getQualifiers(), |
| 4852 | /*IsInitializer=*/false); |
| 4853 | }); |
| 4854 | } |
| 4855 | } else { |
| 4856 | CodeGenFunction::OMPPrivateScope InitScope(CGF); |
| 4857 | InitScope.addPrivate(Elem, [SharedRefLValue]() -> Address { |
| 4858 | return SharedRefLValue.getAddress(); |
| 4859 | }); |
| 4860 | (void)InitScope.Privatize(); |
| 4861 | CodeGenFunction::CGCapturedStmtRAII CapInfoRAII(CGF, &CapturesInfo); |
| 4862 | CGF.EmitExprAsInit(Init, VD, PrivateLValue, |
| 4863 | /*capturedByInit=*/false); |
| 4864 | } |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4865 | } else { |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4866 | CGF.EmitExprAsInit(Init, VD, PrivateLValue, /*capturedByInit=*/false); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4867 | } |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4868 | } |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4869 | ++FI; |
| 4870 | } |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4871 | } |
| 4872 | |
| 4873 | /// Check if duplication function is required for taskloops. |
| 4874 | static bool checkInitIsRequired(CodeGenFunction &CGF, |
| 4875 | ArrayRef<PrivateDataTy> Privates) { |
| 4876 | bool InitRequired = false; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4877 | for (const PrivateDataTy &Pair : Privates) { |
| 4878 | const VarDecl *VD = Pair.second.PrivateCopy; |
| 4879 | const Expr *Init = VD->getAnyInitializer(); |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4880 | InitRequired = InitRequired || (Init && isa<CXXConstructExpr>(Init) && |
| 4881 | !CGF.isTrivialInitializer(Init)); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4882 | if (InitRequired) |
| 4883 | break; |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4884 | } |
| 4885 | return InitRequired; |
| 4886 | } |
| 4887 | |
| 4888 | |
| 4889 | /// Emit task_dup function (for initialization of |
| 4890 | /// private/firstprivate/lastprivate vars and last_iter flag) |
| 4891 | /// \code |
| 4892 | /// void __task_dup_entry(kmp_task_t *task_dst, const kmp_task_t *task_src, int |
| 4893 | /// lastpriv) { |
| 4894 | /// // setup lastprivate flag |
| 4895 | /// task_dst->last = lastpriv; |
| 4896 | /// // could be constructor calls here... |
| 4897 | /// } |
| 4898 | /// \endcode |
| 4899 | static llvm::Value * |
| 4900 | emitTaskDupFunction(CodeGenModule &CGM, SourceLocation Loc, |
| 4901 | const OMPExecutableDirective &D, |
| 4902 | QualType KmpTaskTWithPrivatesPtrQTy, |
| 4903 | const RecordDecl *KmpTaskTWithPrivatesQTyRD, |
| 4904 | const RecordDecl *KmpTaskTQTyRD, QualType SharedsTy, |
| 4905 | QualType SharedsPtrTy, const OMPTaskDataTy &Data, |
| 4906 | ArrayRef<PrivateDataTy> Privates, bool WithLastIter) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4907 | ASTContext &C = CGM.getContext(); |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4908 | FunctionArgList Args; |
Alexey Bataev | 5622323 | 2017-06-09 13:40:18 +0000 | [diff] [blame] | 4909 | ImplicitParamDecl DstArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, |
| 4910 | KmpTaskTWithPrivatesPtrQTy, |
| 4911 | ImplicitParamDecl::Other); |
| 4912 | ImplicitParamDecl SrcArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, |
| 4913 | KmpTaskTWithPrivatesPtrQTy, |
| 4914 | ImplicitParamDecl::Other); |
| 4915 | ImplicitParamDecl LastprivArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, C.IntTy, |
| 4916 | ImplicitParamDecl::Other); |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4917 | Args.push_back(&DstArg); |
| 4918 | Args.push_back(&SrcArg); |
| 4919 | Args.push_back(&LastprivArg); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4920 | const auto &TaskDupFnInfo = |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4921 | CGM.getTypes().arrangeBuiltinFunctionDeclaration(C.VoidTy, Args); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4922 | llvm::FunctionType *TaskDupTy = CGM.getTypes().GetFunctionType(TaskDupFnInfo); |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 4923 | std::string Name = CGM.getOpenMPRuntime().getName({"omp_task_dup", ""}); |
| 4924 | auto *TaskDup = llvm::Function::Create( |
| 4925 | TaskDupTy, llvm::GlobalValue::InternalLinkage, Name, &CGM.getModule()); |
Rafael Espindola | 51ec5a9 | 2018-02-28 23:46:35 +0000 | [diff] [blame] | 4926 | CGM.SetInternalFunctionAttributes(GlobalDecl(), TaskDup, TaskDupFnInfo); |
Alexey Bataev | c0f879b | 2018-04-10 20:10:53 +0000 | [diff] [blame] | 4927 | TaskDup->setDoesNotRecurse(); |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4928 | CodeGenFunction CGF(CGM); |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 4929 | CGF.StartFunction(GlobalDecl(), C.VoidTy, TaskDup, TaskDupFnInfo, Args, Loc, |
| 4930 | Loc); |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4931 | |
| 4932 | LValue TDBase = CGF.EmitLoadOfPointerLValue( |
| 4933 | CGF.GetAddrOfLocalVar(&DstArg), |
| 4934 | KmpTaskTWithPrivatesPtrQTy->castAs<PointerType>()); |
| 4935 | // task_dst->liter = lastpriv; |
| 4936 | if (WithLastIter) { |
| 4937 | auto LIFI = std::next(KmpTaskTQTyRD->field_begin(), KmpTaskTLastIter); |
| 4938 | LValue Base = CGF.EmitLValueForField( |
| 4939 | TDBase, *KmpTaskTWithPrivatesQTyRD->field_begin()); |
| 4940 | LValue LILVal = CGF.EmitLValueForField(Base, *LIFI); |
| 4941 | llvm::Value *Lastpriv = CGF.EmitLoadOfScalar( |
| 4942 | CGF.GetAddrOfLocalVar(&LastprivArg), /*Volatile=*/false, C.IntTy, Loc); |
| 4943 | CGF.EmitStoreOfScalar(Lastpriv, LILVal); |
| 4944 | } |
| 4945 | |
| 4946 | // Emit initial values for private copies (if any). |
| 4947 | assert(!Privates.empty()); |
| 4948 | Address KmpTaskSharedsPtr = Address::invalid(); |
| 4949 | if (!Data.FirstprivateVars.empty()) { |
| 4950 | LValue TDBase = CGF.EmitLoadOfPointerLValue( |
| 4951 | CGF.GetAddrOfLocalVar(&SrcArg), |
| 4952 | KmpTaskTWithPrivatesPtrQTy->castAs<PointerType>()); |
| 4953 | LValue Base = CGF.EmitLValueForField( |
| 4954 | TDBase, *KmpTaskTWithPrivatesQTyRD->field_begin()); |
| 4955 | KmpTaskSharedsPtr = Address( |
| 4956 | CGF.EmitLoadOfScalar(CGF.EmitLValueForField( |
| 4957 | Base, *std::next(KmpTaskTQTyRD->field_begin(), |
| 4958 | KmpTaskTShareds)), |
| 4959 | Loc), |
| 4960 | CGF.getNaturalTypeAlignment(SharedsTy)); |
| 4961 | } |
Alexey Bataev | 8a83159 | 2016-05-10 10:36:51 +0000 | [diff] [blame] | 4962 | emitPrivatesInit(CGF, D, KmpTaskSharedsPtr, TDBase, KmpTaskTWithPrivatesQTyRD, |
| 4963 | SharedsTy, SharedsPtrTy, Data, Privates, /*ForDup=*/true); |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4964 | CGF.FinishFunction(); |
| 4965 | return TaskDup; |
| 4966 | } |
| 4967 | |
Alexey Bataev | 8a83159 | 2016-05-10 10:36:51 +0000 | [diff] [blame] | 4968 | /// Checks if destructor function is required to be generated. |
| 4969 | /// \return true if cleanups are required, false otherwise. |
| 4970 | static bool |
| 4971 | checkDestructorsRequired(const RecordDecl *KmpTaskTWithPrivatesQTyRD) { |
| 4972 | bool NeedsCleanup = false; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4973 | auto FI = std::next(KmpTaskTWithPrivatesQTyRD->field_begin(), 1); |
| 4974 | const auto *PrivateRD = cast<RecordDecl>(FI->getType()->getAsTagDecl()); |
| 4975 | for (const FieldDecl *FD : PrivateRD->fields()) { |
Alexey Bataev | 8a83159 | 2016-05-10 10:36:51 +0000 | [diff] [blame] | 4976 | NeedsCleanup = NeedsCleanup || FD->getType().isDestructedType(); |
| 4977 | if (NeedsCleanup) |
| 4978 | break; |
| 4979 | } |
| 4980 | return NeedsCleanup; |
| 4981 | } |
| 4982 | |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 4983 | CGOpenMPRuntime::TaskResultTy |
| 4984 | CGOpenMPRuntime::emitTaskInit(CodeGenFunction &CGF, SourceLocation Loc, |
| 4985 | const OMPExecutableDirective &D, |
James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 4986 | llvm::Function *TaskFunction, QualType SharedsTy, |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 4987 | Address Shareds, const OMPTaskDataTy &Data) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4988 | ASTContext &C = CGM.getContext(); |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4989 | llvm::SmallVector<PrivateDataTy, 4> Privates; |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 4990 | // Aggregate privates and sort them by the alignment. |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 4991 | auto I = Data.PrivateCopies.begin(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 4992 | for (const Expr *E : Data.PrivateVars) { |
| 4993 | const auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl()); |
Alexey Bataev | 43a919f | 2018-04-13 17:48:43 +0000 | [diff] [blame] | 4994 | Privates.emplace_back( |
Alexey Bataev | c71a409 | 2015-09-11 10:29:41 +0000 | [diff] [blame] | 4995 | C.getDeclAlign(VD), |
Alexey Bataev | 9e03404 | 2015-05-05 04:05:12 +0000 | [diff] [blame] | 4996 | PrivateHelpersTy(VD, cast<VarDecl>(cast<DeclRefExpr>(*I)->getDecl()), |
Alexey Bataev | 43a919f | 2018-04-13 17:48:43 +0000 | [diff] [blame] | 4997 | /*PrivateElemInit=*/nullptr)); |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 4998 | ++I; |
| 4999 | } |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 5000 | I = Data.FirstprivateCopies.begin(); |
| 5001 | auto IElemInitRef = Data.FirstprivateInits.begin(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5002 | for (const Expr *E : Data.FirstprivateVars) { |
| 5003 | const auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl()); |
Alexey Bataev | 43a919f | 2018-04-13 17:48:43 +0000 | [diff] [blame] | 5004 | Privates.emplace_back( |
Alexey Bataev | c71a409 | 2015-09-11 10:29:41 +0000 | [diff] [blame] | 5005 | C.getDeclAlign(VD), |
Alexey Bataev | 9e03404 | 2015-05-05 04:05:12 +0000 | [diff] [blame] | 5006 | PrivateHelpersTy( |
| 5007 | VD, cast<VarDecl>(cast<DeclRefExpr>(*I)->getDecl()), |
Alexey Bataev | 43a919f | 2018-04-13 17:48:43 +0000 | [diff] [blame] | 5008 | cast<VarDecl>(cast<DeclRefExpr>(*IElemInitRef)->getDecl()))); |
Richard Trieu | cc3949d | 2016-02-18 22:34:54 +0000 | [diff] [blame] | 5009 | ++I; |
| 5010 | ++IElemInitRef; |
Alexey Bataev | 9e03404 | 2015-05-05 04:05:12 +0000 | [diff] [blame] | 5011 | } |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 5012 | I = Data.LastprivateCopies.begin(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5013 | for (const Expr *E : Data.LastprivateVars) { |
| 5014 | const auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl()); |
Alexey Bataev | 43a919f | 2018-04-13 17:48:43 +0000 | [diff] [blame] | 5015 | Privates.emplace_back( |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 5016 | C.getDeclAlign(VD), |
| 5017 | PrivateHelpersTy(VD, cast<VarDecl>(cast<DeclRefExpr>(*I)->getDecl()), |
Alexey Bataev | 43a919f | 2018-04-13 17:48:43 +0000 | [diff] [blame] | 5018 | /*PrivateElemInit=*/nullptr)); |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 5019 | ++I; |
| 5020 | } |
Fangrui Song | 899d139 | 2019-04-24 14:43:05 +0000 | [diff] [blame] | 5021 | llvm::stable_sort(Privates, [](PrivateDataTy L, PrivateDataTy R) { |
| 5022 | return L.first > R.first; |
| 5023 | }); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5024 | QualType KmpInt32Ty = C.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1); |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 5025 | // Build type kmp_routine_entry_t (if not built yet). |
| 5026 | emitKmpRoutineEntryT(KmpInt32Ty); |
Alexey Bataev | 8fc69dc | 2015-05-18 07:54:53 +0000 | [diff] [blame] | 5027 | // Build type kmp_task_t (if not built yet). |
Alexey Bataev | e213f3e | 2017-10-11 15:29:40 +0000 | [diff] [blame] | 5028 | if (isOpenMPTaskLoopDirective(D.getDirectiveKind())) { |
| 5029 | if (SavedKmpTaskloopTQTy.isNull()) { |
| 5030 | SavedKmpTaskloopTQTy = C.getRecordType(createKmpTaskTRecordDecl( |
| 5031 | CGM, D.getDirectiveKind(), KmpInt32Ty, KmpRoutineEntryPtrQTy)); |
| 5032 | } |
| 5033 | KmpTaskTQTy = SavedKmpTaskloopTQTy; |
Alexey Bataev | 3a03a7f | 2017-10-11 15:56:38 +0000 | [diff] [blame] | 5034 | } else { |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 5035 | assert((D.getDirectiveKind() == OMPD_task || |
| 5036 | isOpenMPTargetExecutionDirective(D.getDirectiveKind()) || |
| 5037 | isOpenMPTargetDataManagementDirective(D.getDirectiveKind())) && |
| 5038 | "Expected taskloop, task or target directive"); |
Alexey Bataev | e213f3e | 2017-10-11 15:29:40 +0000 | [diff] [blame] | 5039 | if (SavedKmpTaskTQTy.isNull()) { |
| 5040 | SavedKmpTaskTQTy = C.getRecordType(createKmpTaskTRecordDecl( |
| 5041 | CGM, D.getDirectiveKind(), KmpInt32Ty, KmpRoutineEntryPtrQTy)); |
| 5042 | } |
| 5043 | KmpTaskTQTy = SavedKmpTaskTQTy; |
Alexey Bataev | 8fc69dc | 2015-05-18 07:54:53 +0000 | [diff] [blame] | 5044 | } |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5045 | const auto *KmpTaskTQTyRD = cast<RecordDecl>(KmpTaskTQTy->getAsTagDecl()); |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 5046 | // Build particular struct kmp_task_t for the given task. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5047 | const RecordDecl *KmpTaskTWithPrivatesQTyRD = |
Alexey Bataev | 8fc69dc | 2015-05-18 07:54:53 +0000 | [diff] [blame] | 5048 | createKmpTaskTWithPrivatesRecordDecl(CGM, KmpTaskTQTy, Privates); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5049 | QualType KmpTaskTWithPrivatesQTy = C.getRecordType(KmpTaskTWithPrivatesQTyRD); |
Alexey Bataev | 8fc69dc | 2015-05-18 07:54:53 +0000 | [diff] [blame] | 5050 | QualType KmpTaskTWithPrivatesPtrQTy = |
| 5051 | C.getPointerType(KmpTaskTWithPrivatesQTy); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5052 | llvm::Type *KmpTaskTWithPrivatesTy = CGF.ConvertType(KmpTaskTWithPrivatesQTy); |
| 5053 | llvm::Type *KmpTaskTWithPrivatesPtrTy = |
| 5054 | KmpTaskTWithPrivatesTy->getPointerTo(); |
| 5055 | llvm::Value *KmpTaskTWithPrivatesTySize = |
| 5056 | CGF.getTypeSize(KmpTaskTWithPrivatesQTy); |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 5057 | QualType SharedsPtrTy = C.getPointerType(SharedsTy); |
| 5058 | |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 5059 | // Emit initial values for private copies (if any). |
| 5060 | llvm::Value *TaskPrivatesMap = nullptr; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5061 | llvm::Type *TaskPrivatesMapTy = |
James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 5062 | std::next(TaskFunction->arg_begin(), 3)->getType(); |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 5063 | if (!Privates.empty()) { |
| 5064 | auto FI = std::next(KmpTaskTWithPrivatesQTyRD->field_begin()); |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 5065 | TaskPrivatesMap = emitTaskPrivateMappingFunction( |
| 5066 | CGM, Loc, Data.PrivateVars, Data.FirstprivateVars, Data.LastprivateVars, |
| 5067 | FI->getType(), Privates); |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 5068 | TaskPrivatesMap = CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
| 5069 | TaskPrivatesMap, TaskPrivatesMapTy); |
| 5070 | } else { |
| 5071 | TaskPrivatesMap = llvm::ConstantPointerNull::get( |
| 5072 | cast<llvm::PointerType>(TaskPrivatesMapTy)); |
| 5073 | } |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 5074 | // Build a proxy function kmp_int32 .omp_task_entry.(kmp_int32 gtid, |
| 5075 | // kmp_task_t *tt); |
James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 5076 | llvm::Function *TaskEntry = emitProxyTaskFunction( |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 5077 | CGM, Loc, D.getDirectiveKind(), KmpInt32Ty, KmpTaskTWithPrivatesPtrQTy, |
| 5078 | KmpTaskTWithPrivatesQTy, KmpTaskTQTy, SharedsPtrTy, TaskFunction, |
| 5079 | TaskPrivatesMap); |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 5080 | |
| 5081 | // Build call kmp_task_t * __kmpc_omp_task_alloc(ident_t *, kmp_int32 gtid, |
| 5082 | // kmp_int32 flags, size_t sizeof_kmp_task_t, size_t sizeof_shareds, |
| 5083 | // kmp_routine_entry_t *task_entry); |
| 5084 | // Task flags. Format is taken from |
James Y Knight | 5d71fc5 | 2019-01-29 16:37:27 +0000 | [diff] [blame] | 5085 | // https://github.com/llvm/llvm-project/blob/master/openmp/runtime/src/kmp.h, |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 5086 | // description of kmp_tasking_flags struct. |
Alexey Bataev | 1e1e286 | 2016-05-10 12:21:02 +0000 | [diff] [blame] | 5087 | enum { |
| 5088 | TiedFlag = 0x1, |
| 5089 | FinalFlag = 0x2, |
| 5090 | DestructorsFlag = 0x8, |
| 5091 | PriorityFlag = 0x20 |
| 5092 | }; |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 5093 | unsigned Flags = Data.Tied ? TiedFlag : 0; |
Alexey Bataev | 8a83159 | 2016-05-10 10:36:51 +0000 | [diff] [blame] | 5094 | bool NeedsCleanup = false; |
| 5095 | if (!Privates.empty()) { |
| 5096 | NeedsCleanup = checkDestructorsRequired(KmpTaskTWithPrivatesQTyRD); |
| 5097 | if (NeedsCleanup) |
| 5098 | Flags = Flags | DestructorsFlag; |
| 5099 | } |
Alexey Bataev | 1e1e286 | 2016-05-10 12:21:02 +0000 | [diff] [blame] | 5100 | if (Data.Priority.getInt()) |
| 5101 | Flags = Flags | PriorityFlag; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5102 | llvm::Value *TaskFlags = |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 5103 | Data.Final.getPointer() |
| 5104 | ? CGF.Builder.CreateSelect(Data.Final.getPointer(), |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 5105 | CGF.Builder.getInt32(FinalFlag), |
| 5106 | CGF.Builder.getInt32(/*C=*/0)) |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 5107 | : CGF.Builder.getInt32(Data.Final.getInt() ? FinalFlag : 0); |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 5108 | TaskFlags = CGF.Builder.CreateOr(TaskFlags, CGF.Builder.getInt32(Flags)); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5109 | llvm::Value *SharedsSize = CGM.getSize(C.getTypeSizeInChars(SharedsTy)); |
Gheorghe-Teodor Bercea | 545a9fe | 2019-06-14 20:19:54 +0000 | [diff] [blame] | 5110 | SmallVector<llvm::Value *, 8> AllocArgs = {emitUpdateLocation(CGF, Loc), |
| 5111 | getThreadID(CGF, Loc), TaskFlags, KmpTaskTWithPrivatesTySize, |
| 5112 | SharedsSize, CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
| 5113 | TaskEntry, KmpRoutineEntryPtrTy)}; |
| 5114 | llvm::Value *NewTask; |
| 5115 | if (D.hasClausesOfKind<OMPNowaitClause>()) { |
| 5116 | // Check if we have any device clause associated with the directive. |
| 5117 | const Expr *Device = nullptr; |
| 5118 | if (auto *C = D.getSingleClause<OMPDeviceClause>()) |
| 5119 | Device = C->getDevice(); |
| 5120 | // Emit device ID if any otherwise use default value. |
| 5121 | llvm::Value *DeviceID; |
| 5122 | if (Device) |
| 5123 | DeviceID = CGF.Builder.CreateIntCast(CGF.EmitScalarExpr(Device), |
| 5124 | CGF.Int64Ty, /*isSigned=*/true); |
| 5125 | else |
| 5126 | DeviceID = CGF.Builder.getInt64(OMP_DEVICEID_UNDEF); |
| 5127 | AllocArgs.push_back(DeviceID); |
| 5128 | NewTask = CGF.EmitRuntimeCall( |
| 5129 | createRuntimeFunction(OMPRTL__kmpc_omp_target_task_alloc), AllocArgs); |
| 5130 | } else { |
| 5131 | NewTask = CGF.EmitRuntimeCall( |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 5132 | createRuntimeFunction(OMPRTL__kmpc_omp_task_alloc), AllocArgs); |
Gheorghe-Teodor Bercea | 545a9fe | 2019-06-14 20:19:54 +0000 | [diff] [blame] | 5133 | } |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5134 | llvm::Value *NewTaskNewTaskTTy = |
| 5135 | CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
| 5136 | NewTask, KmpTaskTWithPrivatesPtrTy); |
Alexey Bataev | 8fc69dc | 2015-05-18 07:54:53 +0000 | [diff] [blame] | 5137 | LValue Base = CGF.MakeNaturalAlignAddrLValue(NewTaskNewTaskTTy, |
| 5138 | KmpTaskTWithPrivatesQTy); |
| 5139 | LValue TDBase = |
| 5140 | CGF.EmitLValueForField(Base, *KmpTaskTWithPrivatesQTyRD->field_begin()); |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 5141 | // Fill the data in the resulting kmp_task_t record. |
| 5142 | // Copy shareds if there are any. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5143 | Address KmpTaskSharedsPtr = Address::invalid(); |
Alexey Bataev | 8fc69dc | 2015-05-18 07:54:53 +0000 | [diff] [blame] | 5144 | if (!SharedsTy->getAsStructureType()->getDecl()->field_empty()) { |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 5145 | KmpTaskSharedsPtr = |
| 5146 | Address(CGF.EmitLoadOfScalar( |
| 5147 | CGF.EmitLValueForField( |
| 5148 | TDBase, *std::next(KmpTaskTQTyRD->field_begin(), |
| 5149 | KmpTaskTShareds)), |
| 5150 | Loc), |
| 5151 | CGF.getNaturalTypeAlignment(SharedsTy)); |
Ivan A. Kosarev | 1860b52 | 2018-01-25 14:21:55 +0000 | [diff] [blame] | 5152 | LValue Dest = CGF.MakeAddrLValue(KmpTaskSharedsPtr, SharedsTy); |
| 5153 | LValue Src = CGF.MakeAddrLValue(Shareds, SharedsTy); |
Richard Smith | e78fac5 | 2018-04-05 20:52:58 +0000 | [diff] [blame] | 5154 | CGF.EmitAggregateCopy(Dest, Src, SharedsTy, AggValueSlot::DoesNotOverlap); |
Alexey Bataev | 8fc69dc | 2015-05-18 07:54:53 +0000 | [diff] [blame] | 5155 | } |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 5156 | // Emit initial values for private copies (if any). |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 5157 | TaskResultTy Result; |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 5158 | if (!Privates.empty()) { |
Alexey Bataev | 8a83159 | 2016-05-10 10:36:51 +0000 | [diff] [blame] | 5159 | emitPrivatesInit(CGF, D, KmpTaskSharedsPtr, Base, KmpTaskTWithPrivatesQTyRD, |
| 5160 | SharedsTy, SharedsPtrTy, Data, Privates, |
| 5161 | /*ForDup=*/false); |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 5162 | if (isOpenMPTaskLoopDirective(D.getDirectiveKind()) && |
| 5163 | (!Data.LastprivateVars.empty() || checkInitIsRequired(CGF, Privates))) { |
| 5164 | Result.TaskDupFn = emitTaskDupFunction( |
| 5165 | CGM, Loc, D, KmpTaskTWithPrivatesPtrQTy, KmpTaskTWithPrivatesQTyRD, |
| 5166 | KmpTaskTQTyRD, SharedsTy, SharedsPtrTy, Data, Privates, |
| 5167 | /*WithLastIter=*/!Data.LastprivateVars.empty()); |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 5168 | } |
| 5169 | } |
Alexey Bataev | ad537bb | 2016-05-30 09:06:50 +0000 | [diff] [blame] | 5170 | // Fields of union "kmp_cmplrdata_t" for destructors and priority. |
| 5171 | enum { Priority = 0, Destructors = 1 }; |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 5172 | // Provide pointer to function with destructors for privates. |
Alexey Bataev | ad537bb | 2016-05-30 09:06:50 +0000 | [diff] [blame] | 5173 | auto FI = std::next(KmpTaskTQTyRD->field_begin(), Data1); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5174 | const RecordDecl *KmpCmplrdataUD = |
| 5175 | (*FI)->getType()->getAsUnionType()->getDecl(); |
Alexey Bataev | ad537bb | 2016-05-30 09:06:50 +0000 | [diff] [blame] | 5176 | if (NeedsCleanup) { |
| 5177 | llvm::Value *DestructorFn = emitDestructorsFunction( |
| 5178 | CGM, Loc, KmpInt32Ty, KmpTaskTWithPrivatesPtrQTy, |
| 5179 | KmpTaskTWithPrivatesQTy); |
| 5180 | LValue Data1LV = CGF.EmitLValueForField(TDBase, *FI); |
| 5181 | LValue DestructorsLV = CGF.EmitLValueForField( |
| 5182 | Data1LV, *std::next(KmpCmplrdataUD->field_begin(), Destructors)); |
| 5183 | CGF.EmitStoreOfScalar(CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
| 5184 | DestructorFn, KmpRoutineEntryPtrTy), |
| 5185 | DestructorsLV); |
| 5186 | } |
| 5187 | // Set priority. |
| 5188 | if (Data.Priority.getInt()) { |
| 5189 | LValue Data2LV = CGF.EmitLValueForField( |
| 5190 | TDBase, *std::next(KmpTaskTQTyRD->field_begin(), Data2)); |
| 5191 | LValue PriorityLV = CGF.EmitLValueForField( |
| 5192 | Data2LV, *std::next(KmpCmplrdataUD->field_begin(), Priority)); |
| 5193 | CGF.EmitStoreOfScalar(Data.Priority.getPointer(), PriorityLV); |
| 5194 | } |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 5195 | Result.NewTask = NewTask; |
| 5196 | Result.TaskEntry = TaskEntry; |
| 5197 | Result.NewTaskNewTaskTTy = NewTaskNewTaskTTy; |
| 5198 | Result.TDBase = TDBase; |
| 5199 | Result.KmpTaskTQTyRD = KmpTaskTQTyRD; |
| 5200 | return Result; |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 5201 | } |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 5202 | |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 5203 | void CGOpenMPRuntime::emitTaskCall(CodeGenFunction &CGF, SourceLocation Loc, |
| 5204 | const OMPExecutableDirective &D, |
James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 5205 | llvm::Function *TaskFunction, |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 5206 | QualType SharedsTy, Address Shareds, |
| 5207 | const Expr *IfCond, |
| 5208 | const OMPTaskDataTy &Data) { |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 5209 | if (!CGF.HaveInsertPoint()) |
| 5210 | return; |
| 5211 | |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 5212 | TaskResultTy Result = |
| 5213 | emitTaskInit(CGF, Loc, D, TaskFunction, SharedsTy, Shareds, Data); |
| 5214 | llvm::Value *NewTask = Result.NewTask; |
James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 5215 | llvm::Function *TaskEntry = Result.TaskEntry; |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 5216 | llvm::Value *NewTaskNewTaskTTy = Result.NewTaskNewTaskTTy; |
| 5217 | LValue TDBase = Result.TDBase; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5218 | const RecordDecl *KmpTaskTQTyRD = Result.KmpTaskTQTyRD; |
| 5219 | ASTContext &C = CGM.getContext(); |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 5220 | // Process list of dependences. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5221 | Address DependenciesArray = Address::invalid(); |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 5222 | unsigned NumDependencies = Data.Dependences.size(); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5223 | if (NumDependencies) { |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 5224 | // Dependence kind for RTL. |
Sergi Mateo Bellido | 31df1ad | 2019-02-04 07:33:19 +0000 | [diff] [blame] | 5225 | enum RTLDependenceKindTy { DepIn = 0x01, DepInOut = 0x3, DepMutexInOutSet = 0x4 }; |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 5226 | enum RTLDependInfoFieldsTy { BaseAddr, Len, Flags }; |
| 5227 | RecordDecl *KmpDependInfoRD; |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5228 | QualType FlagsTy = |
| 5229 | C.getIntTypeForBitwidth(C.getTypeSize(C.BoolTy), /*Signed=*/false); |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 5230 | llvm::Type *LLVMFlagsTy = CGF.ConvertTypeForMem(FlagsTy); |
| 5231 | if (KmpDependInfoTy.isNull()) { |
| 5232 | KmpDependInfoRD = C.buildImplicitRecord("kmp_depend_info"); |
| 5233 | KmpDependInfoRD->startDefinition(); |
| 5234 | addFieldToRecordDecl(C, KmpDependInfoRD, C.getIntPtrType()); |
| 5235 | addFieldToRecordDecl(C, KmpDependInfoRD, C.getSizeType()); |
| 5236 | addFieldToRecordDecl(C, KmpDependInfoRD, FlagsTy); |
| 5237 | KmpDependInfoRD->completeDefinition(); |
| 5238 | KmpDependInfoTy = C.getRecordType(KmpDependInfoRD); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5239 | } else { |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 5240 | KmpDependInfoRD = cast<RecordDecl>(KmpDependInfoTy->getAsTagDecl()); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5241 | } |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 5242 | // Define type kmp_depend_info[<Dependences.size()>]; |
| 5243 | QualType KmpDependInfoArrayTy = C.getConstantArrayType( |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5244 | KmpDependInfoTy, llvm::APInt(/*numBits=*/64, NumDependencies), |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 5245 | ArrayType::Normal, /*IndexTypeQuals=*/0); |
| 5246 | // kmp_depend_info[<Dependences.size()>] deps; |
Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 5247 | DependenciesArray = |
| 5248 | CGF.CreateMemTemp(KmpDependInfoArrayTy, ".dep.arr.addr"); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5249 | for (unsigned I = 0; I < NumDependencies; ++I) { |
| 5250 | const Expr *E = Data.Dependences[I].second; |
| 5251 | LValue Addr = CGF.EmitLValue(E); |
Alexey Bataev | d6fdc8b | 2015-08-31 07:32:19 +0000 | [diff] [blame] | 5252 | llvm::Value *Size; |
| 5253 | QualType Ty = E->getType(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5254 | if (const auto *ASE = |
| 5255 | dyn_cast<OMPArraySectionExpr>(E->IgnoreParenImpCasts())) { |
Alexey Bataev | d6fdc8b | 2015-08-31 07:32:19 +0000 | [diff] [blame] | 5256 | LValue UpAddrLVal = |
| 5257 | CGF.EmitOMPArraySectionExpr(ASE, /*LowerBound=*/false); |
| 5258 | llvm::Value *UpAddr = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5259 | CGF.Builder.CreateConstGEP1_32(UpAddrLVal.getPointer(), /*Idx0=*/1); |
Alexey Bataev | d6fdc8b | 2015-08-31 07:32:19 +0000 | [diff] [blame] | 5260 | llvm::Value *LowIntPtr = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5261 | CGF.Builder.CreatePtrToInt(Addr.getPointer(), CGM.SizeTy); |
Alexey Bataev | d6fdc8b | 2015-08-31 07:32:19 +0000 | [diff] [blame] | 5262 | llvm::Value *UpIntPtr = CGF.Builder.CreatePtrToInt(UpAddr, CGM.SizeTy); |
| 5263 | Size = CGF.Builder.CreateNUWSub(UpIntPtr, LowIntPtr); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5264 | } else { |
Alexey Bataev | 1189bd0 | 2016-01-26 12:20:39 +0000 | [diff] [blame] | 5265 | Size = CGF.getTypeSize(Ty); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5266 | } |
| 5267 | LValue Base = CGF.MakeAddrLValue( |
James Y Knight | 751fe28 | 2019-02-09 22:22:28 +0000 | [diff] [blame] | 5268 | CGF.Builder.CreateConstArrayGEP(DependenciesArray, I), |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 5269 | KmpDependInfoTy); |
| 5270 | // deps[i].base_addr = &<Dependences[i].second>; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5271 | LValue BaseAddrLVal = CGF.EmitLValueForField( |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 5272 | Base, *std::next(KmpDependInfoRD->field_begin(), BaseAddr)); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5273 | CGF.EmitStoreOfScalar( |
| 5274 | CGF.Builder.CreatePtrToInt(Addr.getPointer(), CGF.IntPtrTy), |
| 5275 | BaseAddrLVal); |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 5276 | // deps[i].len = sizeof(<Dependences[i].second>); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5277 | LValue LenLVal = CGF.EmitLValueForField( |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 5278 | Base, *std::next(KmpDependInfoRD->field_begin(), Len)); |
| 5279 | CGF.EmitStoreOfScalar(Size, LenLVal); |
| 5280 | // deps[i].flags = <Dependences[i].first>; |
| 5281 | RTLDependenceKindTy DepKind; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5282 | switch (Data.Dependences[I].first) { |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 5283 | case OMPC_DEPEND_in: |
| 5284 | DepKind = DepIn; |
| 5285 | break; |
Alexey Bataev | 92e82f9 | 2015-11-23 13:33:42 +0000 | [diff] [blame] | 5286 | // Out and InOut dependencies must use the same code. |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 5287 | case OMPC_DEPEND_out: |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 5288 | case OMPC_DEPEND_inout: |
| 5289 | DepKind = DepInOut; |
| 5290 | break; |
Sergi Mateo Bellido | 31df1ad | 2019-02-04 07:33:19 +0000 | [diff] [blame] | 5291 | case OMPC_DEPEND_mutexinoutset: |
| 5292 | DepKind = DepMutexInOutSet; |
| 5293 | break; |
Alexey Bataev | eb48235 | 2015-12-18 05:05:56 +0000 | [diff] [blame] | 5294 | case OMPC_DEPEND_source: |
Alexey Bataev | a636c7f | 2015-12-23 10:27:45 +0000 | [diff] [blame] | 5295 | case OMPC_DEPEND_sink: |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 5296 | case OMPC_DEPEND_unknown: |
| 5297 | llvm_unreachable("Unknown task dependence type"); |
| 5298 | } |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5299 | LValue FlagsLVal = CGF.EmitLValueForField( |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 5300 | Base, *std::next(KmpDependInfoRD->field_begin(), Flags)); |
| 5301 | CGF.EmitStoreOfScalar(llvm::ConstantInt::get(LLVMFlagsTy, DepKind), |
| 5302 | FlagsLVal); |
| 5303 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5304 | DependenciesArray = CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
James Y Knight | f5f1b0e | 2019-02-08 15:34:12 +0000 | [diff] [blame] | 5305 | CGF.Builder.CreateConstArrayGEP(DependenciesArray, 0), CGF.VoidPtrTy); |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 5306 | } |
| 5307 | |
Alexander Kornienko | 2a8c18d | 2018-04-06 15:14:32 +0000 | [diff] [blame] | 5308 | // 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] | 5309 | // libcall. |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 5310 | // Build kmp_int32 __kmpc_omp_task_with_deps(ident_t *, kmp_int32 gtid, |
| 5311 | // kmp_task_t *new_task, kmp_int32 ndeps, kmp_depend_info_t *dep_list, |
| 5312 | // kmp_int32 ndeps_noalias, kmp_depend_info_t *noalias_dep_list) if dependence |
| 5313 | // list is not empty |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5314 | llvm::Value *ThreadID = getThreadID(CGF, Loc); |
| 5315 | llvm::Value *UpLoc = emitUpdateLocation(CGF, Loc); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5316 | llvm::Value *TaskArgs[] = { UpLoc, ThreadID, NewTask }; |
| 5317 | llvm::Value *DepTaskArgs[7]; |
| 5318 | if (NumDependencies) { |
| 5319 | DepTaskArgs[0] = UpLoc; |
| 5320 | DepTaskArgs[1] = ThreadID; |
| 5321 | DepTaskArgs[2] = NewTask; |
| 5322 | DepTaskArgs[3] = CGF.Builder.getInt32(NumDependencies); |
| 5323 | DepTaskArgs[4] = DependenciesArray.getPointer(); |
| 5324 | DepTaskArgs[5] = CGF.Builder.getInt32(0); |
| 5325 | DepTaskArgs[6] = llvm::ConstantPointerNull::get(CGF.VoidPtrTy); |
| 5326 | } |
Malcolm Parsons | c6e4583 | 2017-01-13 18:55:32 +0000 | [diff] [blame] | 5327 | auto &&ThenCodeGen = [this, &Data, TDBase, KmpTaskTQTyRD, NumDependencies, |
| 5328 | &TaskArgs, |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5329 | &DepTaskArgs](CodeGenFunction &CGF, PrePostActionTy &) { |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 5330 | if (!Data.Tied) { |
Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 5331 | auto PartIdFI = std::next(KmpTaskTQTyRD->field_begin(), KmpTaskTPartId); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5332 | LValue PartIdLVal = CGF.EmitLValueForField(TDBase, *PartIdFI); |
Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 5333 | CGF.EmitStoreOfScalar(CGF.Builder.getInt32(0), PartIdLVal); |
| 5334 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5335 | if (NumDependencies) { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5336 | CGF.EmitRuntimeCall( |
Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 5337 | createRuntimeFunction(OMPRTL__kmpc_omp_task_with_deps), DepTaskArgs); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5338 | } else { |
Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 5339 | CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_omp_task), |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5340 | TaskArgs); |
| 5341 | } |
Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 5342 | // Check if parent region is untied and build return for untied task; |
| 5343 | if (auto *Region = |
| 5344 | dyn_cast_or_null<CGOpenMPRegionInfo>(CGF.CapturedStmtInfo)) |
| 5345 | Region->emitUntiedSwitch(CGF); |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 5346 | }; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5347 | |
| 5348 | llvm::Value *DepWaitTaskArgs[6]; |
| 5349 | if (NumDependencies) { |
| 5350 | DepWaitTaskArgs[0] = UpLoc; |
| 5351 | DepWaitTaskArgs[1] = ThreadID; |
| 5352 | DepWaitTaskArgs[2] = CGF.Builder.getInt32(NumDependencies); |
| 5353 | DepWaitTaskArgs[3] = DependenciesArray.getPointer(); |
| 5354 | DepWaitTaskArgs[4] = CGF.Builder.getInt32(0); |
| 5355 | DepWaitTaskArgs[5] = llvm::ConstantPointerNull::get(CGF.VoidPtrTy); |
| 5356 | } |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5357 | auto &&ElseCodeGen = [&TaskArgs, ThreadID, NewTaskNewTaskTTy, TaskEntry, |
Alexey Bataev | 3c595a6 | 2017-08-14 15:01:03 +0000 | [diff] [blame] | 5358 | NumDependencies, &DepWaitTaskArgs, |
| 5359 | Loc](CodeGenFunction &CGF, PrePostActionTy &) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5360 | CGOpenMPRuntime &RT = CGF.CGM.getOpenMPRuntime(); |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 5361 | CodeGenFunction::RunCleanupsScope LocalScope(CGF); |
| 5362 | // Build void __kmpc_omp_wait_deps(ident_t *, kmp_int32 gtid, |
| 5363 | // kmp_int32 ndeps, kmp_depend_info_t *dep_list, kmp_int32 |
| 5364 | // ndeps_noalias, kmp_depend_info_t *noalias_dep_list); if dependence info |
| 5365 | // is specified. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5366 | if (NumDependencies) |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5367 | CGF.EmitRuntimeCall(RT.createRuntimeFunction(OMPRTL__kmpc_omp_wait_deps), |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 5368 | DepWaitTaskArgs); |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5369 | // Call proxy_task_entry(gtid, new_task); |
Alexey Bataev | 3c595a6 | 2017-08-14 15:01:03 +0000 | [diff] [blame] | 5370 | auto &&CodeGen = [TaskEntry, ThreadID, NewTaskNewTaskTTy, |
| 5371 | Loc](CodeGenFunction &CGF, PrePostActionTy &Action) { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5372 | Action.Enter(CGF); |
| 5373 | llvm::Value *OutlinedFnArgs[] = {ThreadID, NewTaskNewTaskTTy}; |
Alexey Bataev | 3c595a6 | 2017-08-14 15:01:03 +0000 | [diff] [blame] | 5374 | CGF.CGM.getOpenMPRuntime().emitOutlinedFunctionCall(CGF, Loc, TaskEntry, |
Alexey Bataev | 2c7eee5 | 2017-08-04 19:10:54 +0000 | [diff] [blame] | 5375 | OutlinedFnArgs); |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5376 | }; |
| 5377 | |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 5378 | // Build void __kmpc_omp_task_begin_if0(ident_t *, kmp_int32 gtid, |
| 5379 | // kmp_task_t *new_task); |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 5380 | // Build void __kmpc_omp_task_complete_if0(ident_t *, kmp_int32 gtid, |
| 5381 | // kmp_task_t *new_task); |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5382 | RegionCodeGenTy RCG(CodeGen); |
| 5383 | CommonActionTy Action( |
| 5384 | RT.createRuntimeFunction(OMPRTL__kmpc_omp_task_begin_if0), TaskArgs, |
| 5385 | RT.createRuntimeFunction(OMPRTL__kmpc_omp_task_complete_if0), TaskArgs); |
| 5386 | RCG.setAction(Action); |
| 5387 | RCG(CGF); |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 5388 | }; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5389 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5390 | if (IfCond) { |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 5391 | emitOMPIfClause(CGF, IfCond, ThenCodeGen, ElseCodeGen); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5392 | } else { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5393 | RegionCodeGenTy ThenRCG(ThenCodeGen); |
| 5394 | ThenRCG(CGF); |
Alexey Bataev | f539faa | 2016-03-28 12:58:34 +0000 | [diff] [blame] | 5395 | } |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 5396 | } |
| 5397 | |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 5398 | void CGOpenMPRuntime::emitTaskLoopCall(CodeGenFunction &CGF, SourceLocation Loc, |
| 5399 | const OMPLoopDirective &D, |
James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 5400 | llvm::Function *TaskFunction, |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 5401 | QualType SharedsTy, Address Shareds, |
| 5402 | const Expr *IfCond, |
| 5403 | const OMPTaskDataTy &Data) { |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 5404 | if (!CGF.HaveInsertPoint()) |
| 5405 | return; |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 5406 | TaskResultTy Result = |
| 5407 | emitTaskInit(CGF, Loc, D, TaskFunction, SharedsTy, Shareds, Data); |
Alexander Kornienko | 2a8c18d | 2018-04-06 15:14:32 +0000 | [diff] [blame] | 5408 | // 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] | 5409 | // libcall. |
| 5410 | // Call to void __kmpc_taskloop(ident_t *loc, int gtid, kmp_task_t *task, int |
| 5411 | // if_val, kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st, int nogroup, int |
| 5412 | // sched, kmp_uint64 grainsize, void *task_dup); |
| 5413 | llvm::Value *ThreadID = getThreadID(CGF, Loc); |
| 5414 | llvm::Value *UpLoc = emitUpdateLocation(CGF, Loc); |
| 5415 | llvm::Value *IfVal; |
| 5416 | if (IfCond) { |
| 5417 | IfVal = CGF.Builder.CreateIntCast(CGF.EvaluateExprAsBool(IfCond), CGF.IntTy, |
| 5418 | /*isSigned=*/true); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5419 | } else { |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 5420 | IfVal = llvm::ConstantInt::getSigned(CGF.IntTy, /*V=*/1); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5421 | } |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 5422 | |
| 5423 | LValue LBLVal = CGF.EmitLValueForField( |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 5424 | Result.TDBase, |
| 5425 | *std::next(Result.KmpTaskTQTyRD->field_begin(), KmpTaskTLowerBound)); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5426 | const auto *LBVar = |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 5427 | cast<VarDecl>(cast<DeclRefExpr>(D.getLowerBoundVariable())->getDecl()); |
| 5428 | CGF.EmitAnyExprToMem(LBVar->getInit(), LBLVal.getAddress(), LBLVal.getQuals(), |
| 5429 | /*IsInitializer=*/true); |
| 5430 | LValue UBLVal = CGF.EmitLValueForField( |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 5431 | Result.TDBase, |
| 5432 | *std::next(Result.KmpTaskTQTyRD->field_begin(), KmpTaskTUpperBound)); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5433 | const auto *UBVar = |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 5434 | cast<VarDecl>(cast<DeclRefExpr>(D.getUpperBoundVariable())->getDecl()); |
| 5435 | CGF.EmitAnyExprToMem(UBVar->getInit(), UBLVal.getAddress(), UBLVal.getQuals(), |
| 5436 | /*IsInitializer=*/true); |
| 5437 | LValue StLVal = CGF.EmitLValueForField( |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 5438 | Result.TDBase, |
| 5439 | *std::next(Result.KmpTaskTQTyRD->field_begin(), KmpTaskTStride)); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5440 | const auto *StVar = |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 5441 | cast<VarDecl>(cast<DeclRefExpr>(D.getStrideVariable())->getDecl()); |
| 5442 | CGF.EmitAnyExprToMem(StVar->getInit(), StLVal.getAddress(), StLVal.getQuals(), |
| 5443 | /*IsInitializer=*/true); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 5444 | // Store reductions address. |
| 5445 | LValue RedLVal = CGF.EmitLValueForField( |
| 5446 | Result.TDBase, |
| 5447 | *std::next(Result.KmpTaskTQTyRD->field_begin(), KmpTaskTReductions)); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5448 | if (Data.Reductions) { |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 5449 | CGF.EmitStoreOfScalar(Data.Reductions, RedLVal); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5450 | } else { |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 5451 | CGF.EmitNullInitialization(RedLVal.getAddress(), |
| 5452 | CGF.getContext().VoidPtrTy); |
| 5453 | } |
Alexey Bataev | 2b19a6f | 2016-04-28 09:15:06 +0000 | [diff] [blame] | 5454 | enum { NoSchedule = 0, Grainsize = 1, NumTasks = 2 }; |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 5455 | llvm::Value *TaskArgs[] = { |
Alexey Bataev | 3344603 | 2017-07-12 18:09:32 +0000 | [diff] [blame] | 5456 | UpLoc, |
| 5457 | ThreadID, |
| 5458 | Result.NewTask, |
| 5459 | IfVal, |
| 5460 | LBLVal.getPointer(), |
| 5461 | UBLVal.getPointer(), |
Alexey Bataev | a9b9cc0 | 2018-01-23 18:12:38 +0000 | [diff] [blame] | 5462 | CGF.EmitLoadOfScalar(StLVal, Loc), |
Alexey Bataev | ac6e4de | 2018-10-24 19:06:37 +0000 | [diff] [blame] | 5463 | llvm::ConstantInt::getSigned( |
| 5464 | CGF.IntTy, 1), // Always 1 because taskgroup emitted by the compiler |
Alexey Bataev | 2b19a6f | 2016-04-28 09:15:06 +0000 | [diff] [blame] | 5465 | llvm::ConstantInt::getSigned( |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 5466 | CGF.IntTy, Data.Schedule.getPointer() |
| 5467 | ? Data.Schedule.getInt() ? NumTasks : Grainsize |
Alexey Bataev | 2b19a6f | 2016-04-28 09:15:06 +0000 | [diff] [blame] | 5468 | : NoSchedule), |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 5469 | Data.Schedule.getPointer() |
| 5470 | ? CGF.Builder.CreateIntCast(Data.Schedule.getPointer(), CGF.Int64Ty, |
Alexey Bataev | 2b19a6f | 2016-04-28 09:15:06 +0000 | [diff] [blame] | 5471 | /*isSigned=*/false) |
| 5472 | : llvm::ConstantInt::get(CGF.Int64Ty, /*V=*/0), |
Alexey Bataev | 3344603 | 2017-07-12 18:09:32 +0000 | [diff] [blame] | 5473 | Result.TaskDupFn ? CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
| 5474 | Result.TaskDupFn, CGF.VoidPtrTy) |
| 5475 | : llvm::ConstantPointerNull::get(CGF.VoidPtrTy)}; |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 5476 | CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_taskloop), TaskArgs); |
| 5477 | } |
| 5478 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 5479 | /// Emit reduction operation for each element of array (required for |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5480 | /// array sections) LHS op = RHS. |
| 5481 | /// \param Type Type of array. |
| 5482 | /// \param LHSVar Variable on the left side of the reduction operation |
| 5483 | /// (references element of array in original variable). |
| 5484 | /// \param RHSVar Variable on the right side of the reduction operation |
| 5485 | /// (references element of array in original variable). |
| 5486 | /// \param RedOpGen Generator of reduction operation with use of LHSVar and |
| 5487 | /// RHSVar. |
Benjamin Kramer | e003ca2 | 2015-10-28 13:54:16 +0000 | [diff] [blame] | 5488 | static void EmitOMPAggregateReduction( |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5489 | CodeGenFunction &CGF, QualType Type, const VarDecl *LHSVar, |
| 5490 | const VarDecl *RHSVar, |
| 5491 | const llvm::function_ref<void(CodeGenFunction &CGF, const Expr *, |
| 5492 | const Expr *, const Expr *)> &RedOpGen, |
| 5493 | const Expr *XExpr = nullptr, const Expr *EExpr = nullptr, |
| 5494 | const Expr *UpExpr = nullptr) { |
| 5495 | // Perform element-by-element initialization. |
| 5496 | QualType ElementTy; |
| 5497 | Address LHSAddr = CGF.GetAddrOfLocalVar(LHSVar); |
| 5498 | Address RHSAddr = CGF.GetAddrOfLocalVar(RHSVar); |
| 5499 | |
| 5500 | // Drill down to the base element type on both arrays. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5501 | const ArrayType *ArrayTy = Type->getAsArrayTypeUnsafe(); |
| 5502 | llvm::Value *NumElements = CGF.emitArrayLength(ArrayTy, ElementTy, LHSAddr); |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5503 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5504 | llvm::Value *RHSBegin = RHSAddr.getPointer(); |
| 5505 | llvm::Value *LHSBegin = LHSAddr.getPointer(); |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5506 | // Cast from pointer to array type to pointer to single element. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5507 | llvm::Value *LHSEnd = CGF.Builder.CreateGEP(LHSBegin, NumElements); |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5508 | // The basic structure here is a while-do loop. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5509 | llvm::BasicBlock *BodyBB = CGF.createBasicBlock("omp.arraycpy.body"); |
| 5510 | llvm::BasicBlock *DoneBB = CGF.createBasicBlock("omp.arraycpy.done"); |
| 5511 | llvm::Value *IsEmpty = |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5512 | CGF.Builder.CreateICmpEQ(LHSBegin, LHSEnd, "omp.arraycpy.isempty"); |
| 5513 | CGF.Builder.CreateCondBr(IsEmpty, DoneBB, BodyBB); |
| 5514 | |
| 5515 | // Enter the loop body, making that address the current address. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5516 | llvm::BasicBlock *EntryBB = CGF.Builder.GetInsertBlock(); |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5517 | CGF.EmitBlock(BodyBB); |
| 5518 | |
| 5519 | CharUnits ElementSize = CGF.getContext().getTypeSizeInChars(ElementTy); |
| 5520 | |
| 5521 | llvm::PHINode *RHSElementPHI = CGF.Builder.CreatePHI( |
| 5522 | RHSBegin->getType(), 2, "omp.arraycpy.srcElementPast"); |
| 5523 | RHSElementPHI->addIncoming(RHSBegin, EntryBB); |
| 5524 | Address RHSElementCurrent = |
| 5525 | Address(RHSElementPHI, |
| 5526 | RHSAddr.getAlignment().alignmentOfArrayElement(ElementSize)); |
| 5527 | |
| 5528 | llvm::PHINode *LHSElementPHI = CGF.Builder.CreatePHI( |
| 5529 | LHSBegin->getType(), 2, "omp.arraycpy.destElementPast"); |
| 5530 | LHSElementPHI->addIncoming(LHSBegin, EntryBB); |
| 5531 | Address LHSElementCurrent = |
| 5532 | Address(LHSElementPHI, |
| 5533 | LHSAddr.getAlignment().alignmentOfArrayElement(ElementSize)); |
| 5534 | |
| 5535 | // Emit copy. |
| 5536 | CodeGenFunction::OMPPrivateScope Scope(CGF); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5537 | Scope.addPrivate(LHSVar, [=]() { return LHSElementCurrent; }); |
| 5538 | Scope.addPrivate(RHSVar, [=]() { return RHSElementCurrent; }); |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5539 | Scope.Privatize(); |
| 5540 | RedOpGen(CGF, XExpr, EExpr, UpExpr); |
| 5541 | Scope.ForceCleanup(); |
| 5542 | |
| 5543 | // Shift the address forward by one element. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5544 | llvm::Value *LHSElementNext = CGF.Builder.CreateConstGEP1_32( |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5545 | LHSElementPHI, /*Idx0=*/1, "omp.arraycpy.dest.element"); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5546 | llvm::Value *RHSElementNext = CGF.Builder.CreateConstGEP1_32( |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5547 | RHSElementPHI, /*Idx0=*/1, "omp.arraycpy.src.element"); |
| 5548 | // Check whether we've reached the end. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5549 | llvm::Value *Done = |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5550 | CGF.Builder.CreateICmpEQ(LHSElementNext, LHSEnd, "omp.arraycpy.done"); |
| 5551 | CGF.Builder.CreateCondBr(Done, DoneBB, BodyBB); |
| 5552 | LHSElementPHI->addIncoming(LHSElementNext, CGF.Builder.GetInsertBlock()); |
| 5553 | RHSElementPHI->addIncoming(RHSElementNext, CGF.Builder.GetInsertBlock()); |
| 5554 | |
| 5555 | // Done. |
| 5556 | CGF.EmitBlock(DoneBB, /*IsFinished=*/true); |
| 5557 | } |
| 5558 | |
Alexey Bataev | a839ddd | 2016-03-17 10:19:46 +0000 | [diff] [blame] | 5559 | /// Emit reduction combiner. If the combiner is a simple expression emit it as |
| 5560 | /// is, otherwise consider it as combiner of UDR decl and emit it as a call of |
| 5561 | /// UDR combiner function. |
| 5562 | static void emitReductionCombiner(CodeGenFunction &CGF, |
| 5563 | const Expr *ReductionOp) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5564 | if (const auto *CE = dyn_cast<CallExpr>(ReductionOp)) |
| 5565 | if (const auto *OVE = dyn_cast<OpaqueValueExpr>(CE->getCallee())) |
| 5566 | if (const auto *DRE = |
Alexey Bataev | a839ddd | 2016-03-17 10:19:46 +0000 | [diff] [blame] | 5567 | dyn_cast<DeclRefExpr>(OVE->getSourceExpr()->IgnoreImpCasts())) |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5568 | if (const auto *DRD = |
| 5569 | dyn_cast<OMPDeclareReductionDecl>(DRE->getDecl())) { |
Alexey Bataev | a839ddd | 2016-03-17 10:19:46 +0000 | [diff] [blame] | 5570 | std::pair<llvm::Function *, llvm::Function *> Reduction = |
| 5571 | CGF.CGM.getOpenMPRuntime().getUserDefinedReduction(DRD); |
| 5572 | RValue Func = RValue::get(Reduction.first); |
| 5573 | CodeGenFunction::OpaqueValueMapping Map(CGF, OVE, Func); |
| 5574 | CGF.EmitIgnoredExpr(ReductionOp); |
| 5575 | return; |
| 5576 | } |
| 5577 | CGF.EmitIgnoredExpr(ReductionOp); |
| 5578 | } |
| 5579 | |
James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 5580 | llvm::Function *CGOpenMPRuntime::emitReductionFunction( |
Alexey Bataev | 982a35e | 2019-03-19 17:09:52 +0000 | [diff] [blame] | 5581 | SourceLocation Loc, llvm::Type *ArgsType, ArrayRef<const Expr *> Privates, |
| 5582 | ArrayRef<const Expr *> LHSExprs, ArrayRef<const Expr *> RHSExprs, |
| 5583 | ArrayRef<const Expr *> ReductionOps) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5584 | ASTContext &C = CGM.getContext(); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5585 | |
| 5586 | // void reduction_func(void *LHSArg, void *RHSArg); |
| 5587 | FunctionArgList Args; |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 5588 | ImplicitParamDecl LHSArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, C.VoidPtrTy, |
| 5589 | ImplicitParamDecl::Other); |
| 5590 | ImplicitParamDecl RHSArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, C.VoidPtrTy, |
| 5591 | ImplicitParamDecl::Other); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5592 | Args.push_back(&LHSArg); |
| 5593 | Args.push_back(&RHSArg); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5594 | const auto &CGFI = |
| 5595 | CGM.getTypes().arrangeBuiltinFunctionDeclaration(C.VoidTy, Args); |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 5596 | std::string Name = getName({"omp", "reduction", "reduction_func"}); |
| 5597 | auto *Fn = llvm::Function::Create(CGM.getTypes().GetFunctionType(CGFI), |
| 5598 | llvm::GlobalValue::InternalLinkage, Name, |
| 5599 | &CGM.getModule()); |
Rafael Espindola | 51ec5a9 | 2018-02-28 23:46:35 +0000 | [diff] [blame] | 5600 | CGM.SetInternalFunctionAttributes(GlobalDecl(), Fn, CGFI); |
Alexey Bataev | c0f879b | 2018-04-10 20:10:53 +0000 | [diff] [blame] | 5601 | Fn->setDoesNotRecurse(); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5602 | CodeGenFunction CGF(CGM); |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 5603 | CGF.StartFunction(GlobalDecl(), C.VoidTy, Fn, CGFI, Args, Loc, Loc); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5604 | |
| 5605 | // Dst = (void*[n])(LHSArg); |
| 5606 | // Src = (void*[n])(RHSArg); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5607 | Address LHS(CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
| 5608 | CGF.Builder.CreateLoad(CGF.GetAddrOfLocalVar(&LHSArg)), |
| 5609 | ArgsType), CGF.getPointerAlign()); |
| 5610 | Address RHS(CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
| 5611 | CGF.Builder.CreateLoad(CGF.GetAddrOfLocalVar(&RHSArg)), |
| 5612 | ArgsType), CGF.getPointerAlign()); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5613 | |
| 5614 | // ... |
| 5615 | // *(Type<i>*)lhs[i] = RedOp<i>(*(Type<i>*)lhs[i], *(Type<i>*)rhs[i]); |
| 5616 | // ... |
| 5617 | CodeGenFunction::OMPPrivateScope Scope(CGF); |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5618 | auto IPriv = Privates.begin(); |
| 5619 | unsigned Idx = 0; |
| 5620 | for (unsigned I = 0, E = ReductionOps.size(); I < E; ++I, ++IPriv, ++Idx) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5621 | const auto *RHSVar = |
| 5622 | cast<VarDecl>(cast<DeclRefExpr>(RHSExprs[I])->getDecl()); |
| 5623 | Scope.addPrivate(RHSVar, [&CGF, RHS, Idx, RHSVar]() { |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5624 | return emitAddrOfVarFromArray(CGF, RHS, Idx, RHSVar); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5625 | }); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5626 | const auto *LHSVar = |
| 5627 | cast<VarDecl>(cast<DeclRefExpr>(LHSExprs[I])->getDecl()); |
| 5628 | Scope.addPrivate(LHSVar, [&CGF, LHS, Idx, LHSVar]() { |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5629 | return emitAddrOfVarFromArray(CGF, LHS, Idx, LHSVar); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5630 | }); |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5631 | QualType PrivTy = (*IPriv)->getType(); |
Alexey Bataev | 1189bd0 | 2016-01-26 12:20:39 +0000 | [diff] [blame] | 5632 | if (PrivTy->isVariablyModifiedType()) { |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5633 | // Get array size and emit VLA type. |
| 5634 | ++Idx; |
James Y Knight | 751fe28 | 2019-02-09 22:22:28 +0000 | [diff] [blame] | 5635 | Address Elem = CGF.Builder.CreateConstArrayGEP(LHS, Idx); |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5636 | llvm::Value *Ptr = CGF.Builder.CreateLoad(Elem); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5637 | const VariableArrayType *VLA = |
| 5638 | CGF.getContext().getAsVariableArrayType(PrivTy); |
| 5639 | const auto *OVE = cast<OpaqueValueExpr>(VLA->getSizeExpr()); |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5640 | CodeGenFunction::OpaqueValueMapping OpaqueMap( |
Alexey Bataev | 1189bd0 | 2016-01-26 12:20:39 +0000 | [diff] [blame] | 5641 | CGF, OVE, RValue::get(CGF.Builder.CreatePtrToInt(Ptr, CGF.SizeTy))); |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5642 | CGF.EmitVariablyModifiedType(PrivTy); |
| 5643 | } |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5644 | } |
| 5645 | Scope.Privatize(); |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5646 | IPriv = Privates.begin(); |
| 5647 | auto ILHS = LHSExprs.begin(); |
| 5648 | auto IRHS = RHSExprs.begin(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5649 | for (const Expr *E : ReductionOps) { |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5650 | if ((*IPriv)->getType()->isArrayType()) { |
| 5651 | // Emit reduction for array section. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5652 | const auto *LHSVar = cast<VarDecl>(cast<DeclRefExpr>(*ILHS)->getDecl()); |
| 5653 | const auto *RHSVar = cast<VarDecl>(cast<DeclRefExpr>(*IRHS)->getDecl()); |
Alexey Bataev | a839ddd | 2016-03-17 10:19:46 +0000 | [diff] [blame] | 5654 | EmitOMPAggregateReduction( |
| 5655 | CGF, (*IPriv)->getType(), LHSVar, RHSVar, |
| 5656 | [=](CodeGenFunction &CGF, const Expr *, const Expr *, const Expr *) { |
| 5657 | emitReductionCombiner(CGF, E); |
| 5658 | }); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5659 | } else { |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5660 | // Emit reduction for array subscript or single variable. |
Alexey Bataev | a839ddd | 2016-03-17 10:19:46 +0000 | [diff] [blame] | 5661 | emitReductionCombiner(CGF, E); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5662 | } |
Richard Trieu | cc3949d | 2016-02-18 22:34:54 +0000 | [diff] [blame] | 5663 | ++IPriv; |
| 5664 | ++ILHS; |
| 5665 | ++IRHS; |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5666 | } |
| 5667 | Scope.ForceCleanup(); |
| 5668 | CGF.FinishFunction(); |
| 5669 | return Fn; |
| 5670 | } |
| 5671 | |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 5672 | void CGOpenMPRuntime::emitSingleReductionCombiner(CodeGenFunction &CGF, |
| 5673 | const Expr *ReductionOp, |
| 5674 | const Expr *PrivateRef, |
| 5675 | const DeclRefExpr *LHS, |
| 5676 | const DeclRefExpr *RHS) { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5677 | if (PrivateRef->getType()->isArrayType()) { |
| 5678 | // Emit reduction for array section. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5679 | const auto *LHSVar = cast<VarDecl>(LHS->getDecl()); |
| 5680 | const auto *RHSVar = cast<VarDecl>(RHS->getDecl()); |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5681 | EmitOMPAggregateReduction( |
| 5682 | CGF, PrivateRef->getType(), LHSVar, RHSVar, |
| 5683 | [=](CodeGenFunction &CGF, const Expr *, const Expr *, const Expr *) { |
| 5684 | emitReductionCombiner(CGF, ReductionOp); |
| 5685 | }); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5686 | } else { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5687 | // Emit reduction for array subscript or single variable. |
| 5688 | emitReductionCombiner(CGF, ReductionOp); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5689 | } |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5690 | } |
| 5691 | |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5692 | void CGOpenMPRuntime::emitReduction(CodeGenFunction &CGF, SourceLocation Loc, |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5693 | ArrayRef<const Expr *> Privates, |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5694 | ArrayRef<const Expr *> LHSExprs, |
| 5695 | ArrayRef<const Expr *> RHSExprs, |
| 5696 | ArrayRef<const Expr *> ReductionOps, |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 5697 | ReductionOptionsTy Options) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 5698 | if (!CGF.HaveInsertPoint()) |
| 5699 | return; |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 5700 | |
| 5701 | bool WithNowait = Options.WithNowait; |
| 5702 | bool SimpleReduction = Options.SimpleReduction; |
| 5703 | |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5704 | // Next code should be emitted for reduction: |
| 5705 | // |
| 5706 | // static kmp_critical_name lock = { 0 }; |
| 5707 | // |
| 5708 | // void reduce_func(void *lhs[<n>], void *rhs[<n>]) { |
| 5709 | // *(Type0*)lhs[0] = ReductionOperation0(*(Type0*)lhs[0], *(Type0*)rhs[0]); |
| 5710 | // ... |
| 5711 | // *(Type<n>-1*)lhs[<n>-1] = ReductionOperation<n>-1(*(Type<n>-1*)lhs[<n>-1], |
| 5712 | // *(Type<n>-1*)rhs[<n>-1]); |
| 5713 | // } |
| 5714 | // |
| 5715 | // ... |
| 5716 | // void *RedList[<n>] = {&<RHSExprs>[0], ..., &<RHSExprs>[<n>-1]}; |
| 5717 | // switch (__kmpc_reduce{_nowait}(<loc>, <gtid>, <n>, sizeof(RedList), |
| 5718 | // RedList, reduce_func, &<lock>)) { |
| 5719 | // case 1: |
| 5720 | // ... |
| 5721 | // <LHSExprs>[i] = RedOp<i>(*<LHSExprs>[i], *<RHSExprs>[i]); |
| 5722 | // ... |
| 5723 | // __kmpc_end_reduce{_nowait}(<loc>, <gtid>, &<lock>); |
| 5724 | // break; |
| 5725 | // case 2: |
| 5726 | // ... |
| 5727 | // Atomic(<LHSExprs>[i] = RedOp<i>(*<LHSExprs>[i], *<RHSExprs>[i])); |
| 5728 | // ... |
Alexey Bataev | 69a4779 | 2015-05-07 03:54:03 +0000 | [diff] [blame] | 5729 | // [__kmpc_end_reduce(<loc>, <gtid>, &<lock>);] |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5730 | // break; |
| 5731 | // default:; |
| 5732 | // } |
Alexey Bataev | 89e7e8e | 2015-06-17 06:21:39 +0000 | [diff] [blame] | 5733 | // |
| 5734 | // if SimpleReduction is true, only the next code is generated: |
| 5735 | // ... |
| 5736 | // <LHSExprs>[i] = RedOp<i>(*<LHSExprs>[i], *<RHSExprs>[i]); |
| 5737 | // ... |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5738 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5739 | ASTContext &C = CGM.getContext(); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5740 | |
Alexey Bataev | 89e7e8e | 2015-06-17 06:21:39 +0000 | [diff] [blame] | 5741 | if (SimpleReduction) { |
| 5742 | CodeGenFunction::RunCleanupsScope Scope(CGF); |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5743 | auto IPriv = Privates.begin(); |
| 5744 | auto ILHS = LHSExprs.begin(); |
| 5745 | auto IRHS = RHSExprs.begin(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5746 | for (const Expr *E : ReductionOps) { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5747 | emitSingleReductionCombiner(CGF, E, *IPriv, cast<DeclRefExpr>(*ILHS), |
| 5748 | cast<DeclRefExpr>(*IRHS)); |
Richard Trieu | cc3949d | 2016-02-18 22:34:54 +0000 | [diff] [blame] | 5749 | ++IPriv; |
| 5750 | ++ILHS; |
| 5751 | ++IRHS; |
Alexey Bataev | 89e7e8e | 2015-06-17 06:21:39 +0000 | [diff] [blame] | 5752 | } |
| 5753 | return; |
| 5754 | } |
| 5755 | |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5756 | // 1. Build a list of reduction variables. |
| 5757 | // void *RedList[<n>] = {<ReductionVars>[0], ..., <ReductionVars>[<n>-1]}; |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5758 | auto Size = RHSExprs.size(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5759 | for (const Expr *E : Privates) { |
Alexey Bataev | 1189bd0 | 2016-01-26 12:20:39 +0000 | [diff] [blame] | 5760 | if (E->getType()->isVariablyModifiedType()) |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5761 | // Reserve place for array size. |
| 5762 | ++Size; |
| 5763 | } |
| 5764 | llvm::APInt ArraySize(/*unsigned int numBits=*/32, Size); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5765 | QualType ReductionArrayTy = |
| 5766 | C.getConstantArrayType(C.VoidPtrTy, ArraySize, ArrayType::Normal, |
| 5767 | /*IndexTypeQuals=*/0); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5768 | Address ReductionList = |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5769 | CGF.CreateMemTemp(ReductionArrayTy, ".omp.reduction.red_list"); |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5770 | auto IPriv = Privates.begin(); |
| 5771 | unsigned Idx = 0; |
| 5772 | for (unsigned I = 0, E = RHSExprs.size(); I < E; ++I, ++IPriv, ++Idx) { |
James Y Knight | 751fe28 | 2019-02-09 22:22:28 +0000 | [diff] [blame] | 5773 | Address Elem = CGF.Builder.CreateConstArrayGEP(ReductionList, Idx); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5774 | CGF.Builder.CreateStore( |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5775 | CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5776 | CGF.EmitLValue(RHSExprs[I]).getPointer(), CGF.VoidPtrTy), |
| 5777 | Elem); |
Alexey Bataev | 1189bd0 | 2016-01-26 12:20:39 +0000 | [diff] [blame] | 5778 | if ((*IPriv)->getType()->isVariablyModifiedType()) { |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5779 | // Store array size. |
| 5780 | ++Idx; |
James Y Knight | 751fe28 | 2019-02-09 22:22:28 +0000 | [diff] [blame] | 5781 | Elem = CGF.Builder.CreateConstArrayGEP(ReductionList, Idx); |
Alexey Bataev | 1189bd0 | 2016-01-26 12:20:39 +0000 | [diff] [blame] | 5782 | llvm::Value *Size = CGF.Builder.CreateIntCast( |
| 5783 | CGF.getVLASize( |
| 5784 | CGF.getContext().getAsVariableArrayType((*IPriv)->getType())) |
Sander de Smalen | 891af03a | 2018-02-03 13:55:59 +0000 | [diff] [blame] | 5785 | .NumElts, |
Alexey Bataev | 1189bd0 | 2016-01-26 12:20:39 +0000 | [diff] [blame] | 5786 | CGF.SizeTy, /*isSigned=*/false); |
| 5787 | CGF.Builder.CreateStore(CGF.Builder.CreateIntToPtr(Size, CGF.VoidPtrTy), |
| 5788 | Elem); |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5789 | } |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5790 | } |
| 5791 | |
| 5792 | // 2. Emit reduce_func(). |
James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 5793 | llvm::Function *ReductionFn = emitReductionFunction( |
Alexey Bataev | 982a35e | 2019-03-19 17:09:52 +0000 | [diff] [blame] | 5794 | Loc, CGF.ConvertTypeForMem(ReductionArrayTy)->getPointerTo(), Privates, |
| 5795 | LHSExprs, RHSExprs, ReductionOps); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5796 | |
| 5797 | // 3. Create static kmp_critical_name lock = { 0 }; |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 5798 | std::string Name = getName({"reduction"}); |
| 5799 | llvm::Value *Lock = getCriticalRegionLock(Name); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5800 | |
| 5801 | // 4. Build res = __kmpc_reduce{_nowait}(<loc>, <gtid>, <n>, sizeof(RedList), |
| 5802 | // RedList, reduce_func, &<lock>); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5803 | llvm::Value *IdentTLoc = emitUpdateLocation(CGF, Loc, OMP_ATOMIC_REDUCE); |
| 5804 | llvm::Value *ThreadId = getThreadID(CGF, Loc); |
| 5805 | llvm::Value *ReductionArrayTySize = CGF.getTypeSize(ReductionArrayTy); |
| 5806 | llvm::Value *RL = CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
Samuel Antao | 4c8035b | 2016-12-12 18:00:20 +0000 | [diff] [blame] | 5807 | ReductionList.getPointer(), CGF.VoidPtrTy); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5808 | llvm::Value *Args[] = { |
| 5809 | IdentTLoc, // ident_t *<loc> |
| 5810 | ThreadId, // i32 <gtid> |
| 5811 | CGF.Builder.getInt32(RHSExprs.size()), // i32 <n> |
| 5812 | ReductionArrayTySize, // size_type sizeof(RedList) |
| 5813 | RL, // void *RedList |
| 5814 | ReductionFn, // void (*) (void *, void *) <reduce_func> |
| 5815 | Lock // kmp_critical_name *&<lock> |
| 5816 | }; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5817 | llvm::Value *Res = CGF.EmitRuntimeCall( |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5818 | createRuntimeFunction(WithNowait ? OMPRTL__kmpc_reduce_nowait |
| 5819 | : OMPRTL__kmpc_reduce), |
| 5820 | Args); |
| 5821 | |
| 5822 | // 5. Build switch(res) |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5823 | llvm::BasicBlock *DefaultBB = CGF.createBasicBlock(".omp.reduction.default"); |
| 5824 | llvm::SwitchInst *SwInst = |
| 5825 | CGF.Builder.CreateSwitch(Res, DefaultBB, /*NumCases=*/2); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5826 | |
| 5827 | // 6. Build case 1: |
| 5828 | // ... |
| 5829 | // <LHSExprs>[i] = RedOp<i>(*<LHSExprs>[i], *<RHSExprs>[i]); |
| 5830 | // ... |
| 5831 | // __kmpc_end_reduce{_nowait}(<loc>, <gtid>, &<lock>); |
| 5832 | // break; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5833 | llvm::BasicBlock *Case1BB = CGF.createBasicBlock(".omp.reduction.case1"); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5834 | SwInst->addCase(CGF.Builder.getInt32(1), Case1BB); |
| 5835 | CGF.EmitBlock(Case1BB); |
| 5836 | |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5837 | // Add emission of __kmpc_end_reduce{_nowait}(<loc>, <gtid>, &<lock>); |
| 5838 | llvm::Value *EndArgs[] = { |
| 5839 | IdentTLoc, // ident_t *<loc> |
| 5840 | ThreadId, // i32 <gtid> |
| 5841 | Lock // kmp_critical_name *&<lock> |
| 5842 | }; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5843 | auto &&CodeGen = [Privates, LHSExprs, RHSExprs, ReductionOps]( |
| 5844 | CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 5845 | CGOpenMPRuntime &RT = CGF.CGM.getOpenMPRuntime(); |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5846 | auto IPriv = Privates.begin(); |
| 5847 | auto ILHS = LHSExprs.begin(); |
| 5848 | auto IRHS = RHSExprs.begin(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5849 | for (const Expr *E : ReductionOps) { |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 5850 | RT.emitSingleReductionCombiner(CGF, E, *IPriv, cast<DeclRefExpr>(*ILHS), |
| 5851 | cast<DeclRefExpr>(*IRHS)); |
Richard Trieu | cc3949d | 2016-02-18 22:34:54 +0000 | [diff] [blame] | 5852 | ++IPriv; |
| 5853 | ++ILHS; |
| 5854 | ++IRHS; |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5855 | } |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5856 | }; |
| 5857 | RegionCodeGenTy RCG(CodeGen); |
| 5858 | CommonActionTy Action( |
| 5859 | nullptr, llvm::None, |
| 5860 | createRuntimeFunction(WithNowait ? OMPRTL__kmpc_end_reduce_nowait |
| 5861 | : OMPRTL__kmpc_end_reduce), |
| 5862 | EndArgs); |
| 5863 | RCG.setAction(Action); |
| 5864 | RCG(CGF); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5865 | |
| 5866 | CGF.EmitBranch(DefaultBB); |
| 5867 | |
| 5868 | // 7. Build case 2: |
| 5869 | // ... |
| 5870 | // Atomic(<LHSExprs>[i] = RedOp<i>(*<LHSExprs>[i], *<RHSExprs>[i])); |
| 5871 | // ... |
| 5872 | // break; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5873 | llvm::BasicBlock *Case2BB = CGF.createBasicBlock(".omp.reduction.case2"); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5874 | SwInst->addCase(CGF.Builder.getInt32(2), Case2BB); |
| 5875 | CGF.EmitBlock(Case2BB); |
| 5876 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5877 | auto &&AtomicCodeGen = [Loc, Privates, LHSExprs, RHSExprs, ReductionOps]( |
| 5878 | CodeGenFunction &CGF, PrePostActionTy &Action) { |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5879 | auto ILHS = LHSExprs.begin(); |
| 5880 | auto IRHS = RHSExprs.begin(); |
| 5881 | auto IPriv = Privates.begin(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5882 | for (const Expr *E : ReductionOps) { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5883 | const Expr *XExpr = nullptr; |
| 5884 | const Expr *EExpr = nullptr; |
| 5885 | const Expr *UpExpr = nullptr; |
| 5886 | BinaryOperatorKind BO = BO_Comma; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5887 | if (const auto *BO = dyn_cast<BinaryOperator>(E)) { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5888 | if (BO->getOpcode() == BO_Assign) { |
| 5889 | XExpr = BO->getLHS(); |
| 5890 | UpExpr = BO->getRHS(); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5891 | } |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5892 | } |
| 5893 | // Try to emit update expression as a simple atomic. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5894 | const Expr *RHSExpr = UpExpr; |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5895 | if (RHSExpr) { |
| 5896 | // Analyze RHS part of the whole expression. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5897 | if (const auto *ACO = dyn_cast<AbstractConditionalOperator>( |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5898 | RHSExpr->IgnoreParenImpCasts())) { |
| 5899 | // If this is a conditional operator, analyze its condition for |
| 5900 | // min/max reduction operator. |
| 5901 | RHSExpr = ACO->getCond(); |
Alexey Bataev | 69a4779 | 2015-05-07 03:54:03 +0000 | [diff] [blame] | 5902 | } |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5903 | if (const auto *BORHS = |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5904 | dyn_cast<BinaryOperator>(RHSExpr->IgnoreParenImpCasts())) { |
| 5905 | EExpr = BORHS->getRHS(); |
| 5906 | BO = BORHS->getOpcode(); |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 5907 | } |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5908 | } |
| 5909 | if (XExpr) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5910 | const auto *VD = cast<VarDecl>(cast<DeclRefExpr>(*ILHS)->getDecl()); |
Malcolm Parsons | c6e4583 | 2017-01-13 18:55:32 +0000 | [diff] [blame] | 5911 | auto &&AtomicRedGen = [BO, VD, |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5912 | Loc](CodeGenFunction &CGF, const Expr *XExpr, |
| 5913 | const Expr *EExpr, const Expr *UpExpr) { |
| 5914 | LValue X = CGF.EmitLValue(XExpr); |
| 5915 | RValue E; |
| 5916 | if (EExpr) |
| 5917 | E = CGF.EmitAnyExpr(EExpr); |
| 5918 | CGF.EmitOMPAtomicSimpleUpdateExpr( |
JF Bastien | 92f4ef1 | 2016-04-06 17:26:42 +0000 | [diff] [blame] | 5919 | X, E, BO, /*IsXLHSInRHSPart=*/true, |
| 5920 | llvm::AtomicOrdering::Monotonic, Loc, |
Malcolm Parsons | c6e4583 | 2017-01-13 18:55:32 +0000 | [diff] [blame] | 5921 | [&CGF, UpExpr, VD, Loc](RValue XRValue) { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5922 | CodeGenFunction::OMPPrivateScope PrivateScope(CGF); |
| 5923 | PrivateScope.addPrivate( |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5924 | VD, [&CGF, VD, XRValue, Loc]() { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5925 | Address LHSTemp = CGF.CreateMemTemp(VD->getType()); |
| 5926 | CGF.emitOMPSimpleStore( |
| 5927 | CGF.MakeAddrLValue(LHSTemp, VD->getType()), XRValue, |
| 5928 | VD->getType().getNonReferenceType(), Loc); |
| 5929 | return LHSTemp; |
| 5930 | }); |
| 5931 | (void)PrivateScope.Privatize(); |
| 5932 | return CGF.EmitAnyExpr(UpExpr); |
| 5933 | }); |
| 5934 | }; |
| 5935 | if ((*IPriv)->getType()->isArrayType()) { |
| 5936 | // Emit atomic reduction for array section. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5937 | const auto *RHSVar = |
| 5938 | cast<VarDecl>(cast<DeclRefExpr>(*IRHS)->getDecl()); |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5939 | EmitOMPAggregateReduction(CGF, (*IPriv)->getType(), VD, RHSVar, |
| 5940 | AtomicRedGen, XExpr, EExpr, UpExpr); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5941 | } else { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5942 | // Emit atomic reduction for array subscript or single variable. |
| 5943 | AtomicRedGen(CGF, XExpr, EExpr, UpExpr); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5944 | } |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5945 | } else { |
| 5946 | // Emit as a critical region. |
| 5947 | auto &&CritRedGen = [E, Loc](CodeGenFunction &CGF, const Expr *, |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 5948 | const Expr *, const Expr *) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5949 | CGOpenMPRuntime &RT = CGF.CGM.getOpenMPRuntime(); |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 5950 | std::string Name = RT.getName({"atomic_reduction"}); |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5951 | RT.emitCriticalRegion( |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 5952 | CGF, Name, |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5953 | [=](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 5954 | Action.Enter(CGF); |
| 5955 | emitReductionCombiner(CGF, E); |
| 5956 | }, |
| 5957 | Loc); |
| 5958 | }; |
| 5959 | if ((*IPriv)->getType()->isArrayType()) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5960 | const auto *LHSVar = |
| 5961 | cast<VarDecl>(cast<DeclRefExpr>(*ILHS)->getDecl()); |
| 5962 | const auto *RHSVar = |
| 5963 | cast<VarDecl>(cast<DeclRefExpr>(*IRHS)->getDecl()); |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5964 | EmitOMPAggregateReduction(CGF, (*IPriv)->getType(), LHSVar, RHSVar, |
| 5965 | CritRedGen); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5966 | } else { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5967 | CritRedGen(CGF, nullptr, nullptr, nullptr); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5968 | } |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5969 | } |
Richard Trieu | cc3949d | 2016-02-18 22:34:54 +0000 | [diff] [blame] | 5970 | ++ILHS; |
| 5971 | ++IRHS; |
| 5972 | ++IPriv; |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5973 | } |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5974 | }; |
| 5975 | RegionCodeGenTy AtomicRCG(AtomicCodeGen); |
| 5976 | if (!WithNowait) { |
| 5977 | // Add emission of __kmpc_end_reduce(<loc>, <gtid>, &<lock>); |
| 5978 | llvm::Value *EndArgs[] = { |
| 5979 | IdentTLoc, // ident_t *<loc> |
| 5980 | ThreadId, // i32 <gtid> |
| 5981 | Lock // kmp_critical_name *&<lock> |
| 5982 | }; |
| 5983 | CommonActionTy Action(nullptr, llvm::None, |
| 5984 | createRuntimeFunction(OMPRTL__kmpc_end_reduce), |
| 5985 | EndArgs); |
| 5986 | AtomicRCG.setAction(Action); |
| 5987 | AtomicRCG(CGF); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5988 | } else { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 5989 | AtomicRCG(CGF); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 5990 | } |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 5991 | |
| 5992 | CGF.EmitBranch(DefaultBB); |
| 5993 | CGF.EmitBlock(DefaultBB, /*IsFinished=*/true); |
| 5994 | } |
| 5995 | |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 5996 | /// Generates unique name for artificial threadprivate variables. |
Alexey Bataev | 1c44e15 | 2018-03-06 18:59:43 +0000 | [diff] [blame] | 5997 | /// Format is: <Prefix> "." <Decl_mangled_name> "_" "<Decl_start_loc_raw_enc>" |
| 5998 | static std::string generateUniqueName(CodeGenModule &CGM, StringRef Prefix, |
| 5999 | const Expr *Ref) { |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 6000 | SmallString<256> Buffer; |
| 6001 | llvm::raw_svector_ostream Out(Buffer); |
Alexey Bataev | 1c44e15 | 2018-03-06 18:59:43 +0000 | [diff] [blame] | 6002 | const clang::DeclRefExpr *DE; |
| 6003 | const VarDecl *D = ::getBaseDecl(Ref, DE); |
| 6004 | if (!D) |
| 6005 | D = cast<VarDecl>(cast<DeclRefExpr>(Ref)->getDecl()); |
| 6006 | D = D->getCanonicalDecl(); |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 6007 | std::string Name = CGM.getOpenMPRuntime().getName( |
| 6008 | {D->isLocalVarDeclOrParm() ? D->getName() : CGM.getMangledName(D)}); |
| 6009 | Out << Prefix << Name << "_" |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 6010 | << D->getCanonicalDecl()->getBeginLoc().getRawEncoding(); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 6011 | return Out.str(); |
| 6012 | } |
| 6013 | |
| 6014 | /// Emits reduction initializer function: |
| 6015 | /// \code |
| 6016 | /// void @.red_init(void* %arg) { |
| 6017 | /// %0 = bitcast void* %arg to <type>* |
| 6018 | /// store <type> <init>, <type>* %0 |
| 6019 | /// ret void |
| 6020 | /// } |
| 6021 | /// \endcode |
| 6022 | static llvm::Value *emitReduceInitFunction(CodeGenModule &CGM, |
| 6023 | SourceLocation Loc, |
| 6024 | ReductionCodeGen &RCG, unsigned N) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6025 | ASTContext &C = CGM.getContext(); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 6026 | FunctionArgList Args; |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 6027 | ImplicitParamDecl Param(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, C.VoidPtrTy, |
| 6028 | ImplicitParamDecl::Other); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 6029 | Args.emplace_back(&Param); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6030 | const auto &FnInfo = |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 6031 | CGM.getTypes().arrangeBuiltinFunctionDeclaration(C.VoidTy, Args); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6032 | llvm::FunctionType *FnTy = CGM.getTypes().GetFunctionType(FnInfo); |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 6033 | std::string Name = CGM.getOpenMPRuntime().getName({"red_init", ""}); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 6034 | auto *Fn = llvm::Function::Create(FnTy, llvm::GlobalValue::InternalLinkage, |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 6035 | Name, &CGM.getModule()); |
Rafael Espindola | 51ec5a9 | 2018-02-28 23:46:35 +0000 | [diff] [blame] | 6036 | CGM.SetInternalFunctionAttributes(GlobalDecl(), Fn, FnInfo); |
Alexey Bataev | c0f879b | 2018-04-10 20:10:53 +0000 | [diff] [blame] | 6037 | Fn->setDoesNotRecurse(); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 6038 | CodeGenFunction CGF(CGM); |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 6039 | CGF.StartFunction(GlobalDecl(), C.VoidTy, Fn, FnInfo, Args, Loc, Loc); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 6040 | Address PrivateAddr = CGF.EmitLoadOfPointer( |
| 6041 | CGF.GetAddrOfLocalVar(&Param), |
| 6042 | C.getPointerType(C.VoidPtrTy).castAs<PointerType>()); |
| 6043 | llvm::Value *Size = nullptr; |
| 6044 | // If the size of the reduction item is non-constant, load it from global |
| 6045 | // threadprivate variable. |
| 6046 | if (RCG.getSizes(N).second) { |
| 6047 | Address SizeAddr = CGM.getOpenMPRuntime().getAddrOfArtificialThreadPrivate( |
| 6048 | CGF, CGM.getContext().getSizeType(), |
Alexey Bataev | 1c44e15 | 2018-03-06 18:59:43 +0000 | [diff] [blame] | 6049 | generateUniqueName(CGM, "reduction_size", RCG.getRefExpr(N))); |
Alexey Bataev | a9b9cc0 | 2018-01-23 18:12:38 +0000 | [diff] [blame] | 6050 | Size = CGF.EmitLoadOfScalar(SizeAddr, /*Volatile=*/false, |
| 6051 | CGM.getContext().getSizeType(), Loc); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 6052 | } |
| 6053 | RCG.emitAggregateType(CGF, N, Size); |
| 6054 | LValue SharedLVal; |
| 6055 | // If initializer uses initializer from declare reduction construct, emit a |
| 6056 | // pointer to the address of the original reduction item (reuired by reduction |
| 6057 | // initializer) |
| 6058 | if (RCG.usesReductionInitializer(N)) { |
| 6059 | Address SharedAddr = |
| 6060 | CGM.getOpenMPRuntime().getAddrOfArtificialThreadPrivate( |
| 6061 | CGF, CGM.getContext().VoidPtrTy, |
Alexey Bataev | 1c44e15 | 2018-03-06 18:59:43 +0000 | [diff] [blame] | 6062 | generateUniqueName(CGM, "reduction", RCG.getRefExpr(N))); |
Alexey Bataev | 21dab12 | 2018-03-09 15:20:30 +0000 | [diff] [blame] | 6063 | SharedAddr = CGF.EmitLoadOfPointer( |
| 6064 | SharedAddr, |
| 6065 | CGM.getContext().VoidPtrTy.castAs<PointerType>()->getTypePtr()); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 6066 | SharedLVal = CGF.MakeAddrLValue(SharedAddr, CGM.getContext().VoidPtrTy); |
| 6067 | } else { |
| 6068 | SharedLVal = CGF.MakeNaturalAlignAddrLValue( |
| 6069 | llvm::ConstantPointerNull::get(CGM.VoidPtrTy), |
| 6070 | CGM.getContext().VoidPtrTy); |
| 6071 | } |
| 6072 | // Emit the initializer: |
| 6073 | // %0 = bitcast void* %arg to <type>* |
| 6074 | // store <type> <init>, <type>* %0 |
| 6075 | RCG.emitInitialization(CGF, N, PrivateAddr, SharedLVal, |
| 6076 | [](CodeGenFunction &) { return false; }); |
| 6077 | CGF.FinishFunction(); |
| 6078 | return Fn; |
| 6079 | } |
| 6080 | |
| 6081 | /// Emits reduction combiner function: |
| 6082 | /// \code |
| 6083 | /// void @.red_comb(void* %arg0, void* %arg1) { |
| 6084 | /// %lhs = bitcast void* %arg0 to <type>* |
| 6085 | /// %rhs = bitcast void* %arg1 to <type>* |
| 6086 | /// %2 = <ReductionOp>(<type>* %lhs, <type>* %rhs) |
| 6087 | /// store <type> %2, <type>* %lhs |
| 6088 | /// ret void |
| 6089 | /// } |
| 6090 | /// \endcode |
| 6091 | static llvm::Value *emitReduceCombFunction(CodeGenModule &CGM, |
| 6092 | SourceLocation Loc, |
| 6093 | ReductionCodeGen &RCG, unsigned N, |
| 6094 | const Expr *ReductionOp, |
| 6095 | const Expr *LHS, const Expr *RHS, |
| 6096 | const Expr *PrivateRef) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6097 | ASTContext &C = CGM.getContext(); |
| 6098 | const auto *LHSVD = cast<VarDecl>(cast<DeclRefExpr>(LHS)->getDecl()); |
| 6099 | const auto *RHSVD = cast<VarDecl>(cast<DeclRefExpr>(RHS)->getDecl()); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 6100 | FunctionArgList Args; |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 6101 | ImplicitParamDecl ParamInOut(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, |
| 6102 | C.VoidPtrTy, ImplicitParamDecl::Other); |
| 6103 | ImplicitParamDecl ParamIn(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, C.VoidPtrTy, |
| 6104 | ImplicitParamDecl::Other); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 6105 | Args.emplace_back(&ParamInOut); |
| 6106 | Args.emplace_back(&ParamIn); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6107 | const auto &FnInfo = |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 6108 | CGM.getTypes().arrangeBuiltinFunctionDeclaration(C.VoidTy, Args); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6109 | llvm::FunctionType *FnTy = CGM.getTypes().GetFunctionType(FnInfo); |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 6110 | std::string Name = CGM.getOpenMPRuntime().getName({"red_comb", ""}); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 6111 | auto *Fn = llvm::Function::Create(FnTy, llvm::GlobalValue::InternalLinkage, |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 6112 | Name, &CGM.getModule()); |
Rafael Espindola | 51ec5a9 | 2018-02-28 23:46:35 +0000 | [diff] [blame] | 6113 | CGM.SetInternalFunctionAttributes(GlobalDecl(), Fn, FnInfo); |
Alexey Bataev | c0f879b | 2018-04-10 20:10:53 +0000 | [diff] [blame] | 6114 | Fn->setDoesNotRecurse(); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 6115 | CodeGenFunction CGF(CGM); |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 6116 | CGF.StartFunction(GlobalDecl(), C.VoidTy, Fn, FnInfo, Args, Loc, Loc); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 6117 | llvm::Value *Size = nullptr; |
| 6118 | // If the size of the reduction item is non-constant, load it from global |
| 6119 | // threadprivate variable. |
| 6120 | if (RCG.getSizes(N).second) { |
| 6121 | Address SizeAddr = CGM.getOpenMPRuntime().getAddrOfArtificialThreadPrivate( |
| 6122 | CGF, CGM.getContext().getSizeType(), |
Alexey Bataev | 1c44e15 | 2018-03-06 18:59:43 +0000 | [diff] [blame] | 6123 | generateUniqueName(CGM, "reduction_size", RCG.getRefExpr(N))); |
Alexey Bataev | a9b9cc0 | 2018-01-23 18:12:38 +0000 | [diff] [blame] | 6124 | Size = CGF.EmitLoadOfScalar(SizeAddr, /*Volatile=*/false, |
| 6125 | CGM.getContext().getSizeType(), Loc); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 6126 | } |
| 6127 | RCG.emitAggregateType(CGF, N, Size); |
| 6128 | // Remap lhs and rhs variables to the addresses of the function arguments. |
| 6129 | // %lhs = bitcast void* %arg0 to <type>* |
| 6130 | // %rhs = bitcast void* %arg1 to <type>* |
| 6131 | CodeGenFunction::OMPPrivateScope PrivateScope(CGF); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6132 | PrivateScope.addPrivate(LHSVD, [&C, &CGF, &ParamInOut, LHSVD]() { |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 6133 | // Pull out the pointer to the variable. |
| 6134 | Address PtrAddr = CGF.EmitLoadOfPointer( |
| 6135 | CGF.GetAddrOfLocalVar(&ParamInOut), |
| 6136 | C.getPointerType(C.VoidPtrTy).castAs<PointerType>()); |
| 6137 | return CGF.Builder.CreateElementBitCast( |
| 6138 | PtrAddr, CGF.ConvertTypeForMem(LHSVD->getType())); |
| 6139 | }); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6140 | PrivateScope.addPrivate(RHSVD, [&C, &CGF, &ParamIn, RHSVD]() { |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 6141 | // Pull out the pointer to the variable. |
| 6142 | Address PtrAddr = CGF.EmitLoadOfPointer( |
| 6143 | CGF.GetAddrOfLocalVar(&ParamIn), |
| 6144 | C.getPointerType(C.VoidPtrTy).castAs<PointerType>()); |
| 6145 | return CGF.Builder.CreateElementBitCast( |
| 6146 | PtrAddr, CGF.ConvertTypeForMem(RHSVD->getType())); |
| 6147 | }); |
| 6148 | PrivateScope.Privatize(); |
| 6149 | // Emit the combiner body: |
| 6150 | // %2 = <ReductionOp>(<type> *%lhs, <type> *%rhs) |
| 6151 | // store <type> %2, <type>* %lhs |
| 6152 | CGM.getOpenMPRuntime().emitSingleReductionCombiner( |
| 6153 | CGF, ReductionOp, PrivateRef, cast<DeclRefExpr>(LHS), |
| 6154 | cast<DeclRefExpr>(RHS)); |
| 6155 | CGF.FinishFunction(); |
| 6156 | return Fn; |
| 6157 | } |
| 6158 | |
| 6159 | /// Emits reduction finalizer function: |
| 6160 | /// \code |
| 6161 | /// void @.red_fini(void* %arg) { |
| 6162 | /// %0 = bitcast void* %arg to <type>* |
| 6163 | /// <destroy>(<type>* %0) |
| 6164 | /// ret void |
| 6165 | /// } |
| 6166 | /// \endcode |
| 6167 | static llvm::Value *emitReduceFiniFunction(CodeGenModule &CGM, |
| 6168 | SourceLocation Loc, |
| 6169 | ReductionCodeGen &RCG, unsigned N) { |
| 6170 | if (!RCG.needCleanups(N)) |
| 6171 | return nullptr; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6172 | ASTContext &C = CGM.getContext(); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 6173 | FunctionArgList Args; |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 6174 | ImplicitParamDecl Param(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, C.VoidPtrTy, |
| 6175 | ImplicitParamDecl::Other); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 6176 | Args.emplace_back(&Param); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6177 | const auto &FnInfo = |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 6178 | CGM.getTypes().arrangeBuiltinFunctionDeclaration(C.VoidTy, Args); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6179 | llvm::FunctionType *FnTy = CGM.getTypes().GetFunctionType(FnInfo); |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 6180 | std::string Name = CGM.getOpenMPRuntime().getName({"red_fini", ""}); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 6181 | auto *Fn = llvm::Function::Create(FnTy, llvm::GlobalValue::InternalLinkage, |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 6182 | Name, &CGM.getModule()); |
Rafael Espindola | 51ec5a9 | 2018-02-28 23:46:35 +0000 | [diff] [blame] | 6183 | CGM.SetInternalFunctionAttributes(GlobalDecl(), Fn, FnInfo); |
Alexey Bataev | c0f879b | 2018-04-10 20:10:53 +0000 | [diff] [blame] | 6184 | Fn->setDoesNotRecurse(); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 6185 | CodeGenFunction CGF(CGM); |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 6186 | CGF.StartFunction(GlobalDecl(), C.VoidTy, Fn, FnInfo, Args, Loc, Loc); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 6187 | Address PrivateAddr = CGF.EmitLoadOfPointer( |
| 6188 | CGF.GetAddrOfLocalVar(&Param), |
| 6189 | C.getPointerType(C.VoidPtrTy).castAs<PointerType>()); |
| 6190 | llvm::Value *Size = nullptr; |
| 6191 | // If the size of the reduction item is non-constant, load it from global |
| 6192 | // threadprivate variable. |
| 6193 | if (RCG.getSizes(N).second) { |
| 6194 | Address SizeAddr = CGM.getOpenMPRuntime().getAddrOfArtificialThreadPrivate( |
| 6195 | CGF, CGM.getContext().getSizeType(), |
Alexey Bataev | 1c44e15 | 2018-03-06 18:59:43 +0000 | [diff] [blame] | 6196 | generateUniqueName(CGM, "reduction_size", RCG.getRefExpr(N))); |
Alexey Bataev | a9b9cc0 | 2018-01-23 18:12:38 +0000 | [diff] [blame] | 6197 | Size = CGF.EmitLoadOfScalar(SizeAddr, /*Volatile=*/false, |
| 6198 | CGM.getContext().getSizeType(), Loc); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 6199 | } |
| 6200 | RCG.emitAggregateType(CGF, N, Size); |
| 6201 | // Emit the finalizer body: |
| 6202 | // <destroy>(<type>* %0) |
| 6203 | RCG.emitCleanups(CGF, N, PrivateAddr); |
| 6204 | CGF.FinishFunction(); |
| 6205 | return Fn; |
| 6206 | } |
| 6207 | |
| 6208 | llvm::Value *CGOpenMPRuntime::emitTaskReductionInit( |
| 6209 | CodeGenFunction &CGF, SourceLocation Loc, ArrayRef<const Expr *> LHSExprs, |
| 6210 | ArrayRef<const Expr *> RHSExprs, const OMPTaskDataTy &Data) { |
| 6211 | if (!CGF.HaveInsertPoint() || Data.ReductionVars.empty()) |
| 6212 | return nullptr; |
| 6213 | |
| 6214 | // Build typedef struct: |
| 6215 | // kmp_task_red_input { |
| 6216 | // void *reduce_shar; // shared reduction item |
| 6217 | // size_t reduce_size; // size of data item |
| 6218 | // void *reduce_init; // data initialization routine |
| 6219 | // void *reduce_fini; // data finalization routine |
| 6220 | // void *reduce_comb; // data combiner routine |
| 6221 | // kmp_task_red_flags_t flags; // flags for additional info from compiler |
| 6222 | // } kmp_task_red_input_t; |
| 6223 | ASTContext &C = CGM.getContext(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6224 | RecordDecl *RD = C.buildImplicitRecord("kmp_task_red_input_t"); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 6225 | RD->startDefinition(); |
| 6226 | const FieldDecl *SharedFD = addFieldToRecordDecl(C, RD, C.VoidPtrTy); |
| 6227 | const FieldDecl *SizeFD = addFieldToRecordDecl(C, RD, C.getSizeType()); |
| 6228 | const FieldDecl *InitFD = addFieldToRecordDecl(C, RD, C.VoidPtrTy); |
| 6229 | const FieldDecl *FiniFD = addFieldToRecordDecl(C, RD, C.VoidPtrTy); |
| 6230 | const FieldDecl *CombFD = addFieldToRecordDecl(C, RD, C.VoidPtrTy); |
| 6231 | const FieldDecl *FlagsFD = addFieldToRecordDecl( |
| 6232 | C, RD, C.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/false)); |
| 6233 | RD->completeDefinition(); |
| 6234 | QualType RDType = C.getRecordType(RD); |
| 6235 | unsigned Size = Data.ReductionVars.size(); |
| 6236 | llvm::APInt ArraySize(/*numBits=*/64, Size); |
| 6237 | QualType ArrayRDType = C.getConstantArrayType( |
| 6238 | RDType, ArraySize, ArrayType::Normal, /*IndexTypeQuals=*/0); |
| 6239 | // kmp_task_red_input_t .rd_input.[Size]; |
| 6240 | Address TaskRedInput = CGF.CreateMemTemp(ArrayRDType, ".rd_input."); |
| 6241 | ReductionCodeGen RCG(Data.ReductionVars, Data.ReductionCopies, |
| 6242 | Data.ReductionOps); |
| 6243 | for (unsigned Cnt = 0; Cnt < Size; ++Cnt) { |
| 6244 | // kmp_task_red_input_t &ElemLVal = .rd_input.[Cnt]; |
| 6245 | llvm::Value *Idxs[] = {llvm::ConstantInt::get(CGM.SizeTy, /*V=*/0), |
| 6246 | llvm::ConstantInt::get(CGM.SizeTy, Cnt)}; |
| 6247 | llvm::Value *GEP = CGF.EmitCheckedInBoundsGEP( |
| 6248 | TaskRedInput.getPointer(), Idxs, |
| 6249 | /*SignedIndices=*/false, /*IsSubtraction=*/false, Loc, |
| 6250 | ".rd_input.gep."); |
| 6251 | LValue ElemLVal = CGF.MakeNaturalAlignAddrLValue(GEP, RDType); |
| 6252 | // ElemLVal.reduce_shar = &Shareds[Cnt]; |
| 6253 | LValue SharedLVal = CGF.EmitLValueForField(ElemLVal, SharedFD); |
| 6254 | RCG.emitSharedLValue(CGF, Cnt); |
| 6255 | llvm::Value *CastedShared = |
| 6256 | CGF.EmitCastToVoidPtr(RCG.getSharedLValue(Cnt).getPointer()); |
| 6257 | CGF.EmitStoreOfScalar(CastedShared, SharedLVal); |
| 6258 | RCG.emitAggregateType(CGF, Cnt); |
| 6259 | llvm::Value *SizeValInChars; |
| 6260 | llvm::Value *SizeVal; |
| 6261 | std::tie(SizeValInChars, SizeVal) = RCG.getSizes(Cnt); |
| 6262 | // We use delayed creation/initialization for VLAs, array sections and |
| 6263 | // custom reduction initializations. It is required because runtime does not |
| 6264 | // provide the way to pass the sizes of VLAs/array sections to |
| 6265 | // initializer/combiner/finalizer functions and does not pass the pointer to |
| 6266 | // original reduction item to the initializer. Instead threadprivate global |
| 6267 | // variables are used to store these values and use them in the functions. |
| 6268 | bool DelayedCreation = !!SizeVal; |
| 6269 | SizeValInChars = CGF.Builder.CreateIntCast(SizeValInChars, CGM.SizeTy, |
| 6270 | /*isSigned=*/false); |
| 6271 | LValue SizeLVal = CGF.EmitLValueForField(ElemLVal, SizeFD); |
| 6272 | CGF.EmitStoreOfScalar(SizeValInChars, SizeLVal); |
| 6273 | // ElemLVal.reduce_init = init; |
| 6274 | LValue InitLVal = CGF.EmitLValueForField(ElemLVal, InitFD); |
| 6275 | llvm::Value *InitAddr = |
| 6276 | CGF.EmitCastToVoidPtr(emitReduceInitFunction(CGM, Loc, RCG, Cnt)); |
| 6277 | CGF.EmitStoreOfScalar(InitAddr, InitLVal); |
| 6278 | DelayedCreation = DelayedCreation || RCG.usesReductionInitializer(Cnt); |
| 6279 | // ElemLVal.reduce_fini = fini; |
| 6280 | LValue FiniLVal = CGF.EmitLValueForField(ElemLVal, FiniFD); |
| 6281 | llvm::Value *Fini = emitReduceFiniFunction(CGM, Loc, RCG, Cnt); |
| 6282 | llvm::Value *FiniAddr = Fini |
| 6283 | ? CGF.EmitCastToVoidPtr(Fini) |
| 6284 | : llvm::ConstantPointerNull::get(CGM.VoidPtrTy); |
| 6285 | CGF.EmitStoreOfScalar(FiniAddr, FiniLVal); |
| 6286 | // ElemLVal.reduce_comb = comb; |
| 6287 | LValue CombLVal = CGF.EmitLValueForField(ElemLVal, CombFD); |
| 6288 | llvm::Value *CombAddr = CGF.EmitCastToVoidPtr(emitReduceCombFunction( |
| 6289 | CGM, Loc, RCG, Cnt, Data.ReductionOps[Cnt], LHSExprs[Cnt], |
| 6290 | RHSExprs[Cnt], Data.ReductionCopies[Cnt])); |
| 6291 | CGF.EmitStoreOfScalar(CombAddr, CombLVal); |
| 6292 | // ElemLVal.flags = 0; |
| 6293 | LValue FlagsLVal = CGF.EmitLValueForField(ElemLVal, FlagsFD); |
| 6294 | if (DelayedCreation) { |
| 6295 | CGF.EmitStoreOfScalar( |
| 6296 | llvm::ConstantInt::get(CGM.Int32Ty, /*V=*/1, /*IsSigned=*/true), |
| 6297 | FlagsLVal); |
| 6298 | } else |
| 6299 | CGF.EmitNullInitialization(FlagsLVal.getAddress(), FlagsLVal.getType()); |
| 6300 | } |
| 6301 | // Build call void *__kmpc_task_reduction_init(int gtid, int num_data, void |
| 6302 | // *data); |
| 6303 | llvm::Value *Args[] = { |
| 6304 | CGF.Builder.CreateIntCast(getThreadID(CGF, Loc), CGM.IntTy, |
| 6305 | /*isSigned=*/true), |
| 6306 | llvm::ConstantInt::get(CGM.IntTy, Size, /*isSigned=*/true), |
| 6307 | CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(TaskRedInput.getPointer(), |
| 6308 | CGM.VoidPtrTy)}; |
| 6309 | return CGF.EmitRuntimeCall( |
| 6310 | createRuntimeFunction(OMPRTL__kmpc_task_reduction_init), Args); |
| 6311 | } |
| 6312 | |
| 6313 | void CGOpenMPRuntime::emitTaskReductionFixups(CodeGenFunction &CGF, |
| 6314 | SourceLocation Loc, |
| 6315 | ReductionCodeGen &RCG, |
| 6316 | unsigned N) { |
| 6317 | auto Sizes = RCG.getSizes(N); |
| 6318 | // Emit threadprivate global variable if the type is non-constant |
| 6319 | // (Sizes.second = nullptr). |
| 6320 | if (Sizes.second) { |
| 6321 | llvm::Value *SizeVal = CGF.Builder.CreateIntCast(Sizes.second, CGM.SizeTy, |
| 6322 | /*isSigned=*/false); |
| 6323 | Address SizeAddr = getAddrOfArtificialThreadPrivate( |
| 6324 | CGF, CGM.getContext().getSizeType(), |
Alexey Bataev | 1c44e15 | 2018-03-06 18:59:43 +0000 | [diff] [blame] | 6325 | generateUniqueName(CGM, "reduction_size", RCG.getRefExpr(N))); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 6326 | CGF.Builder.CreateStore(SizeVal, SizeAddr, /*IsVolatile=*/false); |
| 6327 | } |
| 6328 | // Store address of the original reduction item if custom initializer is used. |
| 6329 | if (RCG.usesReductionInitializer(N)) { |
| 6330 | Address SharedAddr = getAddrOfArtificialThreadPrivate( |
| 6331 | CGF, CGM.getContext().VoidPtrTy, |
Alexey Bataev | 1c44e15 | 2018-03-06 18:59:43 +0000 | [diff] [blame] | 6332 | generateUniqueName(CGM, "reduction", RCG.getRefExpr(N))); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 6333 | CGF.Builder.CreateStore( |
| 6334 | CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
| 6335 | RCG.getSharedLValue(N).getPointer(), CGM.VoidPtrTy), |
| 6336 | SharedAddr, /*IsVolatile=*/false); |
| 6337 | } |
| 6338 | } |
| 6339 | |
| 6340 | Address CGOpenMPRuntime::getTaskReductionItem(CodeGenFunction &CGF, |
| 6341 | SourceLocation Loc, |
| 6342 | llvm::Value *ReductionsPtr, |
| 6343 | LValue SharedLVal) { |
| 6344 | // Build call void *__kmpc_task_reduction_get_th_data(int gtid, void *tg, void |
| 6345 | // *d); |
| 6346 | llvm::Value *Args[] = { |
| 6347 | CGF.Builder.CreateIntCast(getThreadID(CGF, Loc), CGM.IntTy, |
| 6348 | /*isSigned=*/true), |
| 6349 | ReductionsPtr, |
| 6350 | CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(SharedLVal.getPointer(), |
| 6351 | CGM.VoidPtrTy)}; |
| 6352 | return Address( |
| 6353 | CGF.EmitRuntimeCall( |
| 6354 | createRuntimeFunction(OMPRTL__kmpc_task_reduction_get_th_data), Args), |
| 6355 | SharedLVal.getAlignment()); |
| 6356 | } |
| 6357 | |
Alexey Bataev | 8b8e202 | 2015-04-27 05:22:09 +0000 | [diff] [blame] | 6358 | void CGOpenMPRuntime::emitTaskwaitCall(CodeGenFunction &CGF, |
| 6359 | SourceLocation Loc) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 6360 | if (!CGF.HaveInsertPoint()) |
| 6361 | return; |
Alexey Bataev | 8b8e202 | 2015-04-27 05:22:09 +0000 | [diff] [blame] | 6362 | // Build call kmp_int32 __kmpc_omp_taskwait(ident_t *loc, kmp_int32 |
| 6363 | // global_tid); |
| 6364 | llvm::Value *Args[] = {emitUpdateLocation(CGF, Loc), getThreadID(CGF, Loc)}; |
| 6365 | // Ignore return result until untied tasks are supported. |
| 6366 | CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_omp_taskwait), Args); |
Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 6367 | if (auto *Region = dyn_cast_or_null<CGOpenMPRegionInfo>(CGF.CapturedStmtInfo)) |
| 6368 | Region->emitUntiedSwitch(CGF); |
Alexey Bataev | 8b8e202 | 2015-04-27 05:22:09 +0000 | [diff] [blame] | 6369 | } |
| 6370 | |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 6371 | void CGOpenMPRuntime::emitInlinedDirective(CodeGenFunction &CGF, |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 6372 | OpenMPDirectiveKind InnerKind, |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 6373 | const RegionCodeGenTy &CodeGen, |
| 6374 | bool HasCancel) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 6375 | if (!CGF.HaveInsertPoint()) |
| 6376 | return; |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 6377 | InlinedOpenMPRegionRAII Region(CGF, CodeGen, InnerKind, HasCancel); |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 6378 | CGF.CapturedStmtInfo->EmitBody(CGF, /*S=*/nullptr); |
Alexey Bataev | 8cbe0a6 | 2015-02-26 10:27:34 +0000 | [diff] [blame] | 6379 | } |
| 6380 | |
Alexey Bataev | 7d5d33e | 2015-07-06 05:50:32 +0000 | [diff] [blame] | 6381 | namespace { |
| 6382 | enum RTCancelKind { |
| 6383 | CancelNoreq = 0, |
| 6384 | CancelParallel = 1, |
| 6385 | CancelLoop = 2, |
| 6386 | CancelSections = 3, |
| 6387 | CancelTaskgroup = 4 |
| 6388 | }; |
Eugene Zelenko | 0a4f3f4 | 2016-02-10 19:11:58 +0000 | [diff] [blame] | 6389 | } // anonymous namespace |
Alexey Bataev | 7d5d33e | 2015-07-06 05:50:32 +0000 | [diff] [blame] | 6390 | |
| 6391 | static RTCancelKind getCancellationKind(OpenMPDirectiveKind CancelRegion) { |
| 6392 | RTCancelKind CancelKind = CancelNoreq; |
Alexey Bataev | 0f34da1 | 2015-07-02 04:17:07 +0000 | [diff] [blame] | 6393 | if (CancelRegion == OMPD_parallel) |
| 6394 | CancelKind = CancelParallel; |
| 6395 | else if (CancelRegion == OMPD_for) |
| 6396 | CancelKind = CancelLoop; |
| 6397 | else if (CancelRegion == OMPD_sections) |
| 6398 | CancelKind = CancelSections; |
| 6399 | else { |
| 6400 | assert(CancelRegion == OMPD_taskgroup); |
| 6401 | CancelKind = CancelTaskgroup; |
| 6402 | } |
Alexey Bataev | 7d5d33e | 2015-07-06 05:50:32 +0000 | [diff] [blame] | 6403 | return CancelKind; |
| 6404 | } |
| 6405 | |
| 6406 | void CGOpenMPRuntime::emitCancellationPointCall( |
| 6407 | CodeGenFunction &CGF, SourceLocation Loc, |
| 6408 | OpenMPDirectiveKind CancelRegion) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 6409 | if (!CGF.HaveInsertPoint()) |
| 6410 | return; |
Alexey Bataev | 7d5d33e | 2015-07-06 05:50:32 +0000 | [diff] [blame] | 6411 | // Build call kmp_int32 __kmpc_cancellationpoint(ident_t *loc, kmp_int32 |
| 6412 | // global_tid, kmp_int32 cncl_kind); |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 6413 | if (auto *OMPRegionInfo = |
| 6414 | dyn_cast_or_null<CGOpenMPRegionInfo>(CGF.CapturedStmtInfo)) { |
Jonas Hahnfeld | b07931f | 2017-02-17 18:32:58 +0000 | [diff] [blame] | 6415 | // For 'cancellation point taskgroup', the task region info may not have a |
| 6416 | // cancel. This may instead happen in another adjacent task. |
| 6417 | if (CancelRegion == OMPD_taskgroup || OMPRegionInfo->hasCancel()) { |
Alexey Bataev | 7d5d33e | 2015-07-06 05:50:32 +0000 | [diff] [blame] | 6418 | llvm::Value *Args[] = { |
| 6419 | emitUpdateLocation(CGF, Loc), getThreadID(CGF, Loc), |
| 6420 | CGF.Builder.getInt32(getCancellationKind(CancelRegion))}; |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 6421 | // Ignore return result until untied tasks are supported. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6422 | llvm::Value *Result = CGF.EmitRuntimeCall( |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 6423 | createRuntimeFunction(OMPRTL__kmpc_cancellationpoint), Args); |
| 6424 | // if (__kmpc_cancellationpoint()) { |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 6425 | // exit from construct; |
| 6426 | // } |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6427 | llvm::BasicBlock *ExitBB = CGF.createBasicBlock(".cancel.exit"); |
| 6428 | llvm::BasicBlock *ContBB = CGF.createBasicBlock(".cancel.continue"); |
| 6429 | llvm::Value *Cmp = CGF.Builder.CreateIsNotNull(Result); |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 6430 | CGF.Builder.CreateCondBr(Cmp, ExitBB, ContBB); |
| 6431 | CGF.EmitBlock(ExitBB); |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 6432 | // exit from construct; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6433 | CodeGenFunction::JumpDest CancelDest = |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 6434 | CGF.getOMPCancelDestination(OMPRegionInfo->getDirectiveKind()); |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 6435 | CGF.EmitBranchThroughCleanup(CancelDest); |
| 6436 | CGF.EmitBlock(ContBB, /*IsFinished=*/true); |
| 6437 | } |
Alexey Bataev | 0f34da1 | 2015-07-02 04:17:07 +0000 | [diff] [blame] | 6438 | } |
Alexey Bataev | 0f34da1 | 2015-07-02 04:17:07 +0000 | [diff] [blame] | 6439 | } |
| 6440 | |
Alexey Bataev | 7d5d33e | 2015-07-06 05:50:32 +0000 | [diff] [blame] | 6441 | void CGOpenMPRuntime::emitCancelCall(CodeGenFunction &CGF, SourceLocation Loc, |
Alexey Bataev | 87933c7 | 2015-09-18 08:07:34 +0000 | [diff] [blame] | 6442 | const Expr *IfCond, |
Alexey Bataev | 7d5d33e | 2015-07-06 05:50:32 +0000 | [diff] [blame] | 6443 | OpenMPDirectiveKind CancelRegion) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 6444 | if (!CGF.HaveInsertPoint()) |
| 6445 | return; |
Alexey Bataev | 7d5d33e | 2015-07-06 05:50:32 +0000 | [diff] [blame] | 6446 | // Build call kmp_int32 __kmpc_cancel(ident_t *loc, kmp_int32 global_tid, |
| 6447 | // kmp_int32 cncl_kind); |
| 6448 | if (auto *OMPRegionInfo = |
| 6449 | dyn_cast_or_null<CGOpenMPRegionInfo>(CGF.CapturedStmtInfo)) { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 6450 | auto &&ThenGen = [Loc, CancelRegion, OMPRegionInfo](CodeGenFunction &CGF, |
| 6451 | PrePostActionTy &) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6452 | CGOpenMPRuntime &RT = CGF.CGM.getOpenMPRuntime(); |
Alexey Bataev | 87933c7 | 2015-09-18 08:07:34 +0000 | [diff] [blame] | 6453 | llvm::Value *Args[] = { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 6454 | RT.emitUpdateLocation(CGF, Loc), RT.getThreadID(CGF, Loc), |
Alexey Bataev | 87933c7 | 2015-09-18 08:07:34 +0000 | [diff] [blame] | 6455 | CGF.Builder.getInt32(getCancellationKind(CancelRegion))}; |
| 6456 | // Ignore return result until untied tasks are supported. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6457 | llvm::Value *Result = CGF.EmitRuntimeCall( |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 6458 | RT.createRuntimeFunction(OMPRTL__kmpc_cancel), Args); |
Alexey Bataev | 87933c7 | 2015-09-18 08:07:34 +0000 | [diff] [blame] | 6459 | // if (__kmpc_cancel()) { |
Alexey Bataev | 87933c7 | 2015-09-18 08:07:34 +0000 | [diff] [blame] | 6460 | // exit from construct; |
| 6461 | // } |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6462 | llvm::BasicBlock *ExitBB = CGF.createBasicBlock(".cancel.exit"); |
| 6463 | llvm::BasicBlock *ContBB = CGF.createBasicBlock(".cancel.continue"); |
| 6464 | llvm::Value *Cmp = CGF.Builder.CreateIsNotNull(Result); |
Alexey Bataev | 87933c7 | 2015-09-18 08:07:34 +0000 | [diff] [blame] | 6465 | CGF.Builder.CreateCondBr(Cmp, ExitBB, ContBB); |
| 6466 | CGF.EmitBlock(ExitBB); |
Alexey Bataev | 87933c7 | 2015-09-18 08:07:34 +0000 | [diff] [blame] | 6467 | // exit from construct; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6468 | CodeGenFunction::JumpDest CancelDest = |
Alexey Bataev | 87933c7 | 2015-09-18 08:07:34 +0000 | [diff] [blame] | 6469 | CGF.getOMPCancelDestination(OMPRegionInfo->getDirectiveKind()); |
| 6470 | CGF.EmitBranchThroughCleanup(CancelDest); |
| 6471 | CGF.EmitBlock(ContBB, /*IsFinished=*/true); |
| 6472 | }; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6473 | if (IfCond) { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 6474 | emitOMPIfClause(CGF, IfCond, ThenGen, |
| 6475 | [](CodeGenFunction &, PrePostActionTy &) {}); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6476 | } else { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 6477 | RegionCodeGenTy ThenRCG(ThenGen); |
| 6478 | ThenRCG(CGF); |
| 6479 | } |
Alexey Bataev | 7d5d33e | 2015-07-06 05:50:32 +0000 | [diff] [blame] | 6480 | } |
| 6481 | } |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 6482 | |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 6483 | void CGOpenMPRuntime::emitTargetOutlinedFunction( |
| 6484 | const OMPExecutableDirective &D, StringRef ParentName, |
| 6485 | llvm::Function *&OutlinedFn, llvm::Constant *&OutlinedFnID, |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 6486 | bool IsOffloadEntry, const RegionCodeGenTy &CodeGen) { |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 6487 | assert(!ParentName.empty() && "Invalid target region parent name!"); |
Gheorghe-Teodor Bercea | 66cdbb47 | 2019-05-21 19:42:01 +0000 | [diff] [blame] | 6488 | HasEmittedTargetRegion = true; |
Arpith Chacko Jacob | 5c309e4 | 2016-03-22 01:48:56 +0000 | [diff] [blame] | 6489 | emitTargetOutlinedFunctionHelper(D, ParentName, OutlinedFn, OutlinedFnID, |
| 6490 | IsOffloadEntry, CodeGen); |
| 6491 | } |
| 6492 | |
| 6493 | void CGOpenMPRuntime::emitTargetOutlinedFunctionHelper( |
| 6494 | const OMPExecutableDirective &D, StringRef ParentName, |
| 6495 | llvm::Function *&OutlinedFn, llvm::Constant *&OutlinedFnID, |
| 6496 | bool IsOffloadEntry, const RegionCodeGenTy &CodeGen) { |
Samuel Antao | 2de62b0 | 2016-02-13 23:35:10 +0000 | [diff] [blame] | 6497 | // Create a unique name for the entry function using the source location |
| 6498 | // information of the current target region. The name will be something like: |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 6499 | // |
Samuel Antao | 2de62b0 | 2016-02-13 23:35:10 +0000 | [diff] [blame] | 6500 | // __omp_offloading_DD_FFFF_PP_lBB |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 6501 | // |
| 6502 | // 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] | 6503 | // mangled name of the function that encloses the target region and BB is the |
| 6504 | // line number of the target region. |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 6505 | |
| 6506 | unsigned DeviceID; |
| 6507 | unsigned FileID; |
| 6508 | unsigned Line; |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 6509 | getTargetEntryUniqueInfo(CGM.getContext(), D.getBeginLoc(), DeviceID, FileID, |
Samuel Antao | 2de62b0 | 2016-02-13 23:35:10 +0000 | [diff] [blame] | 6510 | Line); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 6511 | SmallString<64> EntryFnName; |
| 6512 | { |
| 6513 | llvm::raw_svector_ostream OS(EntryFnName); |
Samuel Antao | 2de62b0 | 2016-02-13 23:35:10 +0000 | [diff] [blame] | 6514 | OS << "__omp_offloading" << llvm::format("_%x", DeviceID) |
| 6515 | << llvm::format("_%x_", FileID) << ParentName << "_l" << Line; |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 6516 | } |
| 6517 | |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 6518 | const CapturedStmt &CS = *D.getCapturedStmt(OMPD_target); |
Arpith Chacko Jacob | 5c309e4 | 2016-03-22 01:48:56 +0000 | [diff] [blame] | 6519 | |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 6520 | CodeGenFunction CGF(CGM, true); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 6521 | CGOpenMPTargetRegionInfo CGInfo(CS, CodeGen, EntryFnName); |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 6522 | CodeGenFunction::CGCapturedStmtRAII CapInfoRAII(CGF, &CGInfo); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 6523 | |
Samuel Antao | 6d00426 | 2016-06-16 18:39:34 +0000 | [diff] [blame] | 6524 | OutlinedFn = CGF.GenerateOpenMPCapturedStmtFunction(CS); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 6525 | |
| 6526 | // If this target outline function is not an offload entry, we don't need to |
| 6527 | // register it. |
| 6528 | if (!IsOffloadEntry) |
| 6529 | return; |
| 6530 | |
| 6531 | // The target region ID is used by the runtime library to identify the current |
| 6532 | // target region, so it only has to be unique and not necessarily point to |
| 6533 | // anything. It could be the pointer to the outlined function that implements |
| 6534 | // the target region, but we aren't using that so that the compiler doesn't |
| 6535 | // need to keep that, and could therefore inline the host function if proven |
| 6536 | // worthwhile during optimization. In the other hand, if emitting code for the |
| 6537 | // device, the ID has to be the function address so that it can retrieved from |
| 6538 | // the offloading entry and launched by the runtime library. We also mark the |
| 6539 | // outlined function to have external linkage in case we are emitting code for |
| 6540 | // the device, because these functions will be entry points to the device. |
| 6541 | |
| 6542 | if (CGM.getLangOpts().OpenMPIsDevice) { |
| 6543 | OutlinedFnID = llvm::ConstantExpr::getBitCast(OutlinedFn, CGM.Int8PtrTy); |
Alexey Bataev | 9a70017 | 2018-05-08 14:16:57 +0000 | [diff] [blame] | 6544 | OutlinedFn->setLinkage(llvm::GlobalValue::WeakAnyLinkage); |
Rafael Espindola | cbca487 | 2018-01-11 22:15:12 +0000 | [diff] [blame] | 6545 | OutlinedFn->setDSOLocal(false); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6546 | } else { |
Alexey Bataev | c15ea70 | 2018-05-09 18:02:37 +0000 | [diff] [blame] | 6547 | std::string Name = getName({EntryFnName, "region_id"}); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 6548 | OutlinedFnID = new llvm::GlobalVariable( |
| 6549 | CGM.getModule(), CGM.Int8Ty, /*isConstant=*/true, |
Alexey Bataev | 9a70017 | 2018-05-08 14:16:57 +0000 | [diff] [blame] | 6550 | llvm::GlobalValue::WeakAnyLinkage, |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 6551 | llvm::Constant::getNullValue(CGM.Int8Ty), Name); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6552 | } |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 6553 | |
| 6554 | // Register the information for the entry associated with this target region. |
| 6555 | OffloadEntriesInfoManager.registerTargetRegionEntryInfo( |
Samuel Antao | f83efdb | 2017-01-05 16:02:49 +0000 | [diff] [blame] | 6556 | DeviceID, FileID, ParentName, Line, OutlinedFn, OutlinedFnID, |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 6557 | OffloadEntriesInfoManagerTy::OMPTargetRegionEntryTargetRegion); |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 6558 | } |
| 6559 | |
Alexey Bataev | 5c42736 | 2019-04-10 19:11:33 +0000 | [diff] [blame] | 6560 | /// Checks if the expression is constant or does not have non-trivial function |
| 6561 | /// calls. |
| 6562 | static bool isTrivial(ASTContext &Ctx, const Expr * E) { |
| 6563 | // We can skip constant expressions. |
| 6564 | // We can skip expressions with trivial calls or simple expressions. |
| 6565 | return (E->isEvaluatable(Ctx, Expr::SE_AllowUndefinedBehavior) || |
| 6566 | !E->hasNonTrivialCall(Ctx)) && |
| 6567 | !E->HasSideEffects(Ctx, /*IncludePossibleEffects=*/true); |
| 6568 | } |
Carlo Bertolli | 6eee906 | 2016-04-29 01:37:30 +0000 | [diff] [blame] | 6569 | |
Alexey Bataev | 5c42736 | 2019-04-10 19:11:33 +0000 | [diff] [blame] | 6570 | const Stmt *CGOpenMPRuntime::getSingleCompoundChild(ASTContext &Ctx, |
| 6571 | const Stmt *Body) { |
| 6572 | const Stmt *Child = Body->IgnoreContainers(); |
| 6573 | while (const auto *C = dyn_cast_or_null<CompoundStmt>(Child)) { |
| 6574 | Child = nullptr; |
| 6575 | for (const Stmt *S : C->body()) { |
| 6576 | if (const auto *E = dyn_cast<Expr>(S)) { |
| 6577 | if (isTrivial(Ctx, E)) |
| 6578 | continue; |
| 6579 | } |
| 6580 | // Some of the statements can be ignored. |
| 6581 | if (isa<AsmStmt>(S) || isa<NullStmt>(S) || isa<OMPFlushDirective>(S) || |
| 6582 | isa<OMPBarrierDirective>(S) || isa<OMPTaskyieldDirective>(S)) |
| 6583 | continue; |
| 6584 | // Analyze declarations. |
| 6585 | if (const auto *DS = dyn_cast<DeclStmt>(S)) { |
| 6586 | if (llvm::all_of(DS->decls(), [&Ctx](const Decl *D) { |
| 6587 | if (isa<EmptyDecl>(D) || isa<DeclContext>(D) || |
| 6588 | isa<TypeDecl>(D) || isa<PragmaCommentDecl>(D) || |
| 6589 | isa<PragmaDetectMismatchDecl>(D) || isa<UsingDecl>(D) || |
| 6590 | isa<UsingDirectiveDecl>(D) || |
| 6591 | isa<OMPDeclareReductionDecl>(D) || |
| 6592 | isa<OMPThreadPrivateDecl>(D) || isa<OMPAllocateDecl>(D)) |
| 6593 | return true; |
| 6594 | const auto *VD = dyn_cast<VarDecl>(D); |
| 6595 | if (!VD) |
| 6596 | return false; |
| 6597 | return VD->isConstexpr() || |
| 6598 | ((VD->getType().isTrivialType(Ctx) || |
| 6599 | VD->getType()->isReferenceType()) && |
| 6600 | (!VD->hasInit() || isTrivial(Ctx, VD->getInit()))); |
| 6601 | })) |
| 6602 | continue; |
| 6603 | } |
| 6604 | // Found multiple children - cannot get the one child only. |
| 6605 | if (Child) |
| 6606 | return nullptr; |
| 6607 | Child = S; |
| 6608 | } |
| 6609 | if (Child) |
| 6610 | Child = Child->IgnoreContainers(); |
| 6611 | } |
| 6612 | return Child; |
Carlo Bertolli | 6eee906 | 2016-04-29 01:37:30 +0000 | [diff] [blame] | 6613 | } |
| 6614 | |
Arpith Chacko Jacob | 33c849a | 2017-01-25 00:57:16 +0000 | [diff] [blame] | 6615 | /// Emit the number of teams for a target directive. Inspect the num_teams |
| 6616 | /// clause associated with a teams construct combined or closely nested |
| 6617 | /// with the target directive. |
| 6618 | /// |
| 6619 | /// Emit a team of size one for directives such as 'target parallel' that |
| 6620 | /// have no associated teams construct. |
| 6621 | /// |
| 6622 | /// Otherwise, return nullptr. |
Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 6623 | static llvm::Value * |
Alexey Bataev | 5c42736 | 2019-04-10 19:11:33 +0000 | [diff] [blame] | 6624 | emitNumTeamsForTargetDirective(CodeGenFunction &CGF, |
Arpith Chacko Jacob | 33c849a | 2017-01-25 00:57:16 +0000 | [diff] [blame] | 6625 | const OMPExecutableDirective &D) { |
Alexey Bataev | 5c42736 | 2019-04-10 19:11:33 +0000 | [diff] [blame] | 6626 | assert(!CGF.getLangOpts().OpenMPIsDevice && |
| 6627 | "Clauses associated with the teams directive expected to be emitted " |
| 6628 | "only for the host!"); |
| 6629 | OpenMPDirectiveKind DirectiveKind = D.getDirectiveKind(); |
| 6630 | assert(isOpenMPTargetExecutionDirective(DirectiveKind) && |
| 6631 | "Expected target-based executable directive."); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6632 | CGBuilderTy &Bld = CGF.Builder; |
Alexey Bataev | 5c42736 | 2019-04-10 19:11:33 +0000 | [diff] [blame] | 6633 | switch (DirectiveKind) { |
| 6634 | case OMPD_target: { |
| 6635 | const auto *CS = D.getInnermostCapturedStmt(); |
| 6636 | const auto *Body = |
| 6637 | CS->getCapturedStmt()->IgnoreContainers(/*IgnoreCaptured=*/true); |
| 6638 | const Stmt *ChildStmt = |
| 6639 | CGOpenMPRuntime::getSingleCompoundChild(CGF.getContext(), Body); |
| 6640 | if (const auto *NestedDir = |
| 6641 | dyn_cast_or_null<OMPExecutableDirective>(ChildStmt)) { |
| 6642 | if (isOpenMPTeamsDirective(NestedDir->getDirectiveKind())) { |
| 6643 | if (NestedDir->hasClausesOfKind<OMPNumTeamsClause>()) { |
| 6644 | CGOpenMPInnerExprInfo CGInfo(CGF, *CS); |
| 6645 | CodeGenFunction::CGCapturedStmtRAII CapInfoRAII(CGF, &CGInfo); |
| 6646 | const Expr *NumTeams = |
| 6647 | NestedDir->getSingleClause<OMPNumTeamsClause>()->getNumTeams(); |
| 6648 | llvm::Value *NumTeamsVal = |
| 6649 | CGF.EmitScalarExpr(NumTeams, |
| 6650 | /*IgnoreResultAssign*/ true); |
| 6651 | return Bld.CreateIntCast(NumTeamsVal, CGF.Int32Ty, |
| 6652 | /*IsSigned=*/true); |
| 6653 | } |
| 6654 | return Bld.getInt32(0); |
Alexey Bataev | 50a1c78 | 2017-12-01 21:31:08 +0000 | [diff] [blame] | 6655 | } |
Alexey Bataev | 5c42736 | 2019-04-10 19:11:33 +0000 | [diff] [blame] | 6656 | if (isOpenMPParallelDirective(NestedDir->getDirectiveKind()) || |
| 6657 | isOpenMPSimdDirective(NestedDir->getDirectiveKind())) |
| 6658 | return Bld.getInt32(1); |
Alexey Bataev | 50a1c78 | 2017-12-01 21:31:08 +0000 | [diff] [blame] | 6659 | return Bld.getInt32(0); |
| 6660 | } |
Alexey Bataev | 5c42736 | 2019-04-10 19:11:33 +0000 | [diff] [blame] | 6661 | return nullptr; |
Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 6662 | } |
Alexey Bataev | 5c42736 | 2019-04-10 19:11:33 +0000 | [diff] [blame] | 6663 | case OMPD_target_teams: |
| 6664 | case OMPD_target_teams_distribute: |
| 6665 | case OMPD_target_teams_distribute_simd: |
| 6666 | case OMPD_target_teams_distribute_parallel_for: |
| 6667 | case OMPD_target_teams_distribute_parallel_for_simd: { |
| 6668 | if (D.hasClausesOfKind<OMPNumTeamsClause>()) { |
| 6669 | CodeGenFunction::RunCleanupsScope NumTeamsScope(CGF); |
| 6670 | const Expr *NumTeams = |
| 6671 | D.getSingleClause<OMPNumTeamsClause>()->getNumTeams(); |
| 6672 | llvm::Value *NumTeamsVal = |
| 6673 | CGF.EmitScalarExpr(NumTeams, |
| 6674 | /*IgnoreResultAssign*/ true); |
| 6675 | return Bld.CreateIntCast(NumTeamsVal, CGF.Int32Ty, |
| 6676 | /*IsSigned=*/true); |
| 6677 | } |
| 6678 | return Bld.getInt32(0); |
| 6679 | } |
| 6680 | case OMPD_target_parallel: |
| 6681 | case OMPD_target_parallel_for: |
| 6682 | case OMPD_target_parallel_for_simd: |
| 6683 | case OMPD_target_simd: |
| 6684 | return Bld.getInt32(1); |
| 6685 | case OMPD_parallel: |
| 6686 | case OMPD_for: |
| 6687 | case OMPD_parallel_for: |
| 6688 | case OMPD_parallel_sections: |
| 6689 | case OMPD_for_simd: |
| 6690 | case OMPD_parallel_for_simd: |
| 6691 | case OMPD_cancel: |
| 6692 | case OMPD_cancellation_point: |
| 6693 | case OMPD_ordered: |
| 6694 | case OMPD_threadprivate: |
| 6695 | case OMPD_allocate: |
| 6696 | case OMPD_task: |
| 6697 | case OMPD_simd: |
| 6698 | case OMPD_sections: |
| 6699 | case OMPD_section: |
| 6700 | case OMPD_single: |
| 6701 | case OMPD_master: |
| 6702 | case OMPD_critical: |
| 6703 | case OMPD_taskyield: |
| 6704 | case OMPD_barrier: |
| 6705 | case OMPD_taskwait: |
| 6706 | case OMPD_taskgroup: |
| 6707 | case OMPD_atomic: |
| 6708 | case OMPD_flush: |
| 6709 | case OMPD_teams: |
| 6710 | case OMPD_target_data: |
| 6711 | case OMPD_target_exit_data: |
| 6712 | case OMPD_target_enter_data: |
| 6713 | case OMPD_distribute: |
| 6714 | case OMPD_distribute_simd: |
| 6715 | case OMPD_distribute_parallel_for: |
| 6716 | case OMPD_distribute_parallel_for_simd: |
| 6717 | case OMPD_teams_distribute: |
| 6718 | case OMPD_teams_distribute_simd: |
| 6719 | case OMPD_teams_distribute_parallel_for: |
| 6720 | case OMPD_teams_distribute_parallel_for_simd: |
| 6721 | case OMPD_target_update: |
| 6722 | case OMPD_declare_simd: |
| 6723 | case OMPD_declare_target: |
| 6724 | case OMPD_end_declare_target: |
| 6725 | case OMPD_declare_reduction: |
| 6726 | case OMPD_declare_mapper: |
| 6727 | case OMPD_taskloop: |
| 6728 | case OMPD_taskloop_simd: |
| 6729 | case OMPD_requires: |
| 6730 | case OMPD_unknown: |
| 6731 | break; |
| 6732 | } |
| 6733 | llvm_unreachable("Unexpected directive kind."); |
| 6734 | } |
Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 6735 | |
Alexey Bataev | 5c42736 | 2019-04-10 19:11:33 +0000 | [diff] [blame] | 6736 | static llvm::Value *getNumThreads(CodeGenFunction &CGF, const CapturedStmt *CS, |
| 6737 | llvm::Value *DefaultThreadLimitVal) { |
| 6738 | const Stmt *Child = CGOpenMPRuntime::getSingleCompoundChild( |
| 6739 | CGF.getContext(), CS->getCapturedStmt()); |
| 6740 | if (const auto *Dir = dyn_cast_or_null<OMPExecutableDirective>(Child)) { |
| 6741 | if (isOpenMPParallelDirective(Dir->getDirectiveKind())) { |
Alexey Bataev | dc9e7dc | 2019-04-17 16:53:08 +0000 | [diff] [blame] | 6742 | llvm::Value *NumThreads = nullptr; |
| 6743 | llvm::Value *CondVal = nullptr; |
| 6744 | // Handle if clause. If if clause present, the number of threads is |
| 6745 | // calculated as <cond> ? (<numthreads> ? <numthreads> : 0 ) : 1. |
| 6746 | if (Dir->hasClausesOfKind<OMPIfClause>()) { |
| 6747 | CGOpenMPInnerExprInfo CGInfo(CGF, *CS); |
| 6748 | CodeGenFunction::CGCapturedStmtRAII CapInfoRAII(CGF, &CGInfo); |
| 6749 | const OMPIfClause *IfClause = nullptr; |
| 6750 | for (const auto *C : Dir->getClausesOfKind<OMPIfClause>()) { |
| 6751 | if (C->getNameModifier() == OMPD_unknown || |
| 6752 | C->getNameModifier() == OMPD_parallel) { |
| 6753 | IfClause = C; |
| 6754 | break; |
| 6755 | } |
| 6756 | } |
| 6757 | if (IfClause) { |
| 6758 | const Expr *Cond = IfClause->getCondition(); |
| 6759 | bool Result; |
| 6760 | if (Cond->EvaluateAsBooleanCondition(Result, CGF.getContext())) { |
| 6761 | if (!Result) |
| 6762 | return CGF.Builder.getInt32(1); |
| 6763 | } else { |
| 6764 | CodeGenFunction::LexicalScope Scope(CGF, Cond->getSourceRange()); |
| 6765 | if (const auto *PreInit = |
| 6766 | cast_or_null<DeclStmt>(IfClause->getPreInitStmt())) { |
| 6767 | for (const auto *I : PreInit->decls()) { |
| 6768 | if (!I->hasAttr<OMPCaptureNoInitAttr>()) { |
| 6769 | CGF.EmitVarDecl(cast<VarDecl>(*I)); |
| 6770 | } else { |
| 6771 | CodeGenFunction::AutoVarEmission Emission = |
| 6772 | CGF.EmitAutoVarAlloca(cast<VarDecl>(*I)); |
| 6773 | CGF.EmitAutoVarCleanups(Emission); |
| 6774 | } |
| 6775 | } |
| 6776 | } |
| 6777 | CondVal = CGF.EvaluateExprAsBool(Cond); |
| 6778 | } |
| 6779 | } |
| 6780 | } |
| 6781 | // Check the value of num_threads clause iff if clause was not specified |
| 6782 | // or is not evaluated to false. |
Alexey Bataev | 5c42736 | 2019-04-10 19:11:33 +0000 | [diff] [blame] | 6783 | if (Dir->hasClausesOfKind<OMPNumThreadsClause>()) { |
| 6784 | CGOpenMPInnerExprInfo CGInfo(CGF, *CS); |
| 6785 | CodeGenFunction::CGCapturedStmtRAII CapInfoRAII(CGF, &CGInfo); |
| 6786 | const auto *NumThreadsClause = |
| 6787 | Dir->getSingleClause<OMPNumThreadsClause>(); |
| 6788 | CodeGenFunction::LexicalScope Scope( |
| 6789 | CGF, NumThreadsClause->getNumThreads()->getSourceRange()); |
| 6790 | if (const auto *PreInit = |
| 6791 | cast_or_null<DeclStmt>(NumThreadsClause->getPreInitStmt())) { |
| 6792 | for (const auto *I : PreInit->decls()) { |
| 6793 | if (!I->hasAttr<OMPCaptureNoInitAttr>()) { |
| 6794 | CGF.EmitVarDecl(cast<VarDecl>(*I)); |
| 6795 | } else { |
| 6796 | CodeGenFunction::AutoVarEmission Emission = |
| 6797 | CGF.EmitAutoVarAlloca(cast<VarDecl>(*I)); |
| 6798 | CGF.EmitAutoVarCleanups(Emission); |
| 6799 | } |
| 6800 | } |
| 6801 | } |
Alexey Bataev | dc9e7dc | 2019-04-17 16:53:08 +0000 | [diff] [blame] | 6802 | NumThreads = CGF.EmitScalarExpr(NumThreadsClause->getNumThreads()); |
Alexey Bataev | 5c42736 | 2019-04-10 19:11:33 +0000 | [diff] [blame] | 6803 | NumThreads = CGF.Builder.CreateIntCast(NumThreads, CGF.Int32Ty, |
Alexey Bataev | dc9e7dc | 2019-04-17 16:53:08 +0000 | [diff] [blame] | 6804 | /*IsSigned=*/false); |
| 6805 | if (DefaultThreadLimitVal) |
| 6806 | NumThreads = CGF.Builder.CreateSelect( |
| 6807 | CGF.Builder.CreateICmpULT(DefaultThreadLimitVal, NumThreads), |
| 6808 | DefaultThreadLimitVal, NumThreads); |
| 6809 | } else { |
| 6810 | NumThreads = DefaultThreadLimitVal ? DefaultThreadLimitVal |
| 6811 | : CGF.Builder.getInt32(0); |
Alexey Bataev | 5c42736 | 2019-04-10 19:11:33 +0000 | [diff] [blame] | 6812 | } |
Alexey Bataev | dc9e7dc | 2019-04-17 16:53:08 +0000 | [diff] [blame] | 6813 | // Process condition of the if clause. |
| 6814 | if (CondVal) { |
| 6815 | NumThreads = CGF.Builder.CreateSelect(CondVal, NumThreads, |
| 6816 | CGF.Builder.getInt32(1)); |
| 6817 | } |
| 6818 | return NumThreads; |
Alexey Bataev | 5c42736 | 2019-04-10 19:11:33 +0000 | [diff] [blame] | 6819 | } |
| 6820 | if (isOpenMPSimdDirective(Dir->getDirectiveKind())) |
| 6821 | return CGF.Builder.getInt32(1); |
| 6822 | return DefaultThreadLimitVal; |
| 6823 | } |
| 6824 | return DefaultThreadLimitVal ? DefaultThreadLimitVal |
| 6825 | : CGF.Builder.getInt32(0); |
Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 6826 | } |
| 6827 | |
Arpith Chacko Jacob | 33c849a | 2017-01-25 00:57:16 +0000 | [diff] [blame] | 6828 | /// Emit the number of threads for a target directive. Inspect the |
| 6829 | /// thread_limit clause associated with a teams construct combined or closely |
| 6830 | /// nested with the target directive. |
| 6831 | /// |
| 6832 | /// Emit the num_threads clause for directives such as 'target parallel' that |
| 6833 | /// have no associated teams construct. |
| 6834 | /// |
| 6835 | /// Otherwise, return nullptr. |
Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 6836 | static llvm::Value * |
Alexey Bataev | 5c42736 | 2019-04-10 19:11:33 +0000 | [diff] [blame] | 6837 | emitNumThreadsForTargetDirective(CodeGenFunction &CGF, |
Arpith Chacko Jacob | 33c849a | 2017-01-25 00:57:16 +0000 | [diff] [blame] | 6838 | const OMPExecutableDirective &D) { |
Alexey Bataev | 5c42736 | 2019-04-10 19:11:33 +0000 | [diff] [blame] | 6839 | assert(!CGF.getLangOpts().OpenMPIsDevice && |
| 6840 | "Clauses associated with the teams directive expected to be emitted " |
| 6841 | "only for the host!"); |
| 6842 | OpenMPDirectiveKind DirectiveKind = D.getDirectiveKind(); |
| 6843 | assert(isOpenMPTargetExecutionDirective(DirectiveKind) && |
| 6844 | "Expected target-based executable directive."); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 6845 | CGBuilderTy &Bld = CGF.Builder; |
Alexey Bataev | 5c42736 | 2019-04-10 19:11:33 +0000 | [diff] [blame] | 6846 | llvm::Value *ThreadLimitVal = nullptr; |
| 6847 | llvm::Value *NumThreadsVal = nullptr; |
| 6848 | switch (DirectiveKind) { |
| 6849 | case OMPD_target: { |
| 6850 | const CapturedStmt *CS = D.getInnermostCapturedStmt(); |
| 6851 | if (llvm::Value *NumThreads = getNumThreads(CGF, CS, ThreadLimitVal)) |
| 6852 | return NumThreads; |
| 6853 | const Stmt *Child = CGOpenMPRuntime::getSingleCompoundChild( |
| 6854 | CGF.getContext(), CS->getCapturedStmt()); |
| 6855 | if (const auto *Dir = dyn_cast_or_null<OMPExecutableDirective>(Child)) { |
| 6856 | if (Dir->hasClausesOfKind<OMPThreadLimitClause>()) { |
| 6857 | CGOpenMPInnerExprInfo CGInfo(CGF, *CS); |
| 6858 | CodeGenFunction::CGCapturedStmtRAII CapInfoRAII(CGF, &CGInfo); |
| 6859 | const auto *ThreadLimitClause = |
| 6860 | Dir->getSingleClause<OMPThreadLimitClause>(); |
| 6861 | CodeGenFunction::LexicalScope Scope( |
| 6862 | CGF, ThreadLimitClause->getThreadLimit()->getSourceRange()); |
| 6863 | if (const auto *PreInit = |
| 6864 | cast_or_null<DeclStmt>(ThreadLimitClause->getPreInitStmt())) { |
| 6865 | for (const auto *I : PreInit->decls()) { |
| 6866 | if (!I->hasAttr<OMPCaptureNoInitAttr>()) { |
| 6867 | CGF.EmitVarDecl(cast<VarDecl>(*I)); |
| 6868 | } else { |
| 6869 | CodeGenFunction::AutoVarEmission Emission = |
| 6870 | CGF.EmitAutoVarAlloca(cast<VarDecl>(*I)); |
| 6871 | CGF.EmitAutoVarCleanups(Emission); |
| 6872 | } |
| 6873 | } |
| 6874 | } |
| 6875 | llvm::Value *ThreadLimit = CGF.EmitScalarExpr( |
| 6876 | ThreadLimitClause->getThreadLimit(), /*IgnoreResultAssign=*/true); |
| 6877 | ThreadLimitVal = |
Alexey Bataev | dc9e7dc | 2019-04-17 16:53:08 +0000 | [diff] [blame] | 6878 | Bld.CreateIntCast(ThreadLimit, CGF.Int32Ty, /*IsSigned=*/false); |
Alexey Bataev | 5c42736 | 2019-04-10 19:11:33 +0000 | [diff] [blame] | 6879 | } |
| 6880 | if (isOpenMPTeamsDirective(Dir->getDirectiveKind()) && |
| 6881 | !isOpenMPDistributeDirective(Dir->getDirectiveKind())) { |
| 6882 | CS = Dir->getInnermostCapturedStmt(); |
| 6883 | const Stmt *Child = CGOpenMPRuntime::getSingleCompoundChild( |
| 6884 | CGF.getContext(), CS->getCapturedStmt()); |
| 6885 | Dir = dyn_cast_or_null<OMPExecutableDirective>(Child); |
| 6886 | } |
| 6887 | if (Dir && isOpenMPDistributeDirective(Dir->getDirectiveKind()) && |
| 6888 | !isOpenMPSimdDirective(Dir->getDirectiveKind())) { |
| 6889 | CS = Dir->getInnermostCapturedStmt(); |
| 6890 | if (llvm::Value *NumThreads = getNumThreads(CGF, CS, ThreadLimitVal)) |
| 6891 | return NumThreads; |
| 6892 | } |
| 6893 | if (Dir && isOpenMPSimdDirective(Dir->getDirectiveKind())) |
| 6894 | return Bld.getInt32(1); |
Arpith Chacko Jacob | 99a1e0e | 2017-01-25 02:18:43 +0000 | [diff] [blame] | 6895 | } |
Alexey Bataev | 5c42736 | 2019-04-10 19:11:33 +0000 | [diff] [blame] | 6896 | return ThreadLimitVal ? ThreadLimitVal : Bld.getInt32(0); |
| 6897 | } |
| 6898 | case OMPD_target_teams: { |
| 6899 | if (D.hasClausesOfKind<OMPThreadLimitClause>()) { |
| 6900 | CodeGenFunction::RunCleanupsScope ThreadLimitScope(CGF); |
| 6901 | const auto *ThreadLimitClause = D.getSingleClause<OMPThreadLimitClause>(); |
| 6902 | llvm::Value *ThreadLimit = CGF.EmitScalarExpr( |
| 6903 | ThreadLimitClause->getThreadLimit(), /*IgnoreResultAssign=*/true); |
| 6904 | ThreadLimitVal = |
Alexey Bataev | dc9e7dc | 2019-04-17 16:53:08 +0000 | [diff] [blame] | 6905 | Bld.CreateIntCast(ThreadLimit, CGF.Int32Ty, /*IsSigned=*/false); |
Alexey Bataev | 5c42736 | 2019-04-10 19:11:33 +0000 | [diff] [blame] | 6906 | } |
| 6907 | const CapturedStmt *CS = D.getInnermostCapturedStmt(); |
| 6908 | if (llvm::Value *NumThreads = getNumThreads(CGF, CS, ThreadLimitVal)) |
| 6909 | return NumThreads; |
| 6910 | const Stmt *Child = CGOpenMPRuntime::getSingleCompoundChild( |
| 6911 | CGF.getContext(), CS->getCapturedStmt()); |
| 6912 | if (const auto *Dir = dyn_cast_or_null<OMPExecutableDirective>(Child)) { |
| 6913 | if (Dir->getDirectiveKind() == OMPD_distribute) { |
| 6914 | CS = Dir->getInnermostCapturedStmt(); |
| 6915 | if (llvm::Value *NumThreads = getNumThreads(CGF, CS, ThreadLimitVal)) |
| 6916 | return NumThreads; |
| 6917 | } |
| 6918 | } |
| 6919 | return ThreadLimitVal ? ThreadLimitVal : Bld.getInt32(0); |
| 6920 | } |
| 6921 | case OMPD_target_teams_distribute: |
| 6922 | if (D.hasClausesOfKind<OMPThreadLimitClause>()) { |
| 6923 | CodeGenFunction::RunCleanupsScope ThreadLimitScope(CGF); |
| 6924 | const auto *ThreadLimitClause = D.getSingleClause<OMPThreadLimitClause>(); |
| 6925 | llvm::Value *ThreadLimit = CGF.EmitScalarExpr( |
| 6926 | ThreadLimitClause->getThreadLimit(), /*IgnoreResultAssign=*/true); |
| 6927 | ThreadLimitVal = |
Alexey Bataev | dc9e7dc | 2019-04-17 16:53:08 +0000 | [diff] [blame] | 6928 | Bld.CreateIntCast(ThreadLimit, CGF.Int32Ty, /*IsSigned=*/false); |
Alexey Bataev | 5c42736 | 2019-04-10 19:11:33 +0000 | [diff] [blame] | 6929 | } |
| 6930 | return getNumThreads(CGF, D.getInnermostCapturedStmt(), ThreadLimitVal); |
| 6931 | case OMPD_target_parallel: |
| 6932 | case OMPD_target_parallel_for: |
| 6933 | case OMPD_target_parallel_for_simd: |
| 6934 | case OMPD_target_teams_distribute_parallel_for: |
Alexey Bataev | dc9e7dc | 2019-04-17 16:53:08 +0000 | [diff] [blame] | 6935 | case OMPD_target_teams_distribute_parallel_for_simd: { |
| 6936 | llvm::Value *CondVal = nullptr; |
| 6937 | // Handle if clause. If if clause present, the number of threads is |
| 6938 | // calculated as <cond> ? (<numthreads> ? <numthreads> : 0 ) : 1. |
| 6939 | if (D.hasClausesOfKind<OMPIfClause>()) { |
| 6940 | const OMPIfClause *IfClause = nullptr; |
| 6941 | for (const auto *C : D.getClausesOfKind<OMPIfClause>()) { |
| 6942 | if (C->getNameModifier() == OMPD_unknown || |
| 6943 | C->getNameModifier() == OMPD_parallel) { |
| 6944 | IfClause = C; |
| 6945 | break; |
| 6946 | } |
| 6947 | } |
| 6948 | if (IfClause) { |
| 6949 | const Expr *Cond = IfClause->getCondition(); |
| 6950 | bool Result; |
| 6951 | if (Cond->EvaluateAsBooleanCondition(Result, CGF.getContext())) { |
| 6952 | if (!Result) |
| 6953 | return Bld.getInt32(1); |
| 6954 | } else { |
| 6955 | CodeGenFunction::RunCleanupsScope Scope(CGF); |
| 6956 | CondVal = CGF.EvaluateExprAsBool(Cond); |
| 6957 | } |
| 6958 | } |
| 6959 | } |
Alexey Bataev | 5c42736 | 2019-04-10 19:11:33 +0000 | [diff] [blame] | 6960 | if (D.hasClausesOfKind<OMPThreadLimitClause>()) { |
| 6961 | CodeGenFunction::RunCleanupsScope ThreadLimitScope(CGF); |
| 6962 | const auto *ThreadLimitClause = D.getSingleClause<OMPThreadLimitClause>(); |
| 6963 | llvm::Value *ThreadLimit = CGF.EmitScalarExpr( |
| 6964 | ThreadLimitClause->getThreadLimit(), /*IgnoreResultAssign=*/true); |
| 6965 | ThreadLimitVal = |
Alexey Bataev | dc9e7dc | 2019-04-17 16:53:08 +0000 | [diff] [blame] | 6966 | Bld.CreateIntCast(ThreadLimit, CGF.Int32Ty, /*IsSigned=*/false); |
Alexey Bataev | 5c42736 | 2019-04-10 19:11:33 +0000 | [diff] [blame] | 6967 | } |
| 6968 | if (D.hasClausesOfKind<OMPNumThreadsClause>()) { |
Arpith Chacko Jacob | 33c849a | 2017-01-25 00:57:16 +0000 | [diff] [blame] | 6969 | CodeGenFunction::RunCleanupsScope NumThreadsScope(CGF); |
Alexey Bataev | 5c42736 | 2019-04-10 19:11:33 +0000 | [diff] [blame] | 6970 | const auto *NumThreadsClause = D.getSingleClause<OMPNumThreadsClause>(); |
| 6971 | llvm::Value *NumThreads = CGF.EmitScalarExpr( |
| 6972 | NumThreadsClause->getNumThreads(), /*IgnoreResultAssign=*/true); |
Arpith Chacko Jacob | 33c849a | 2017-01-25 00:57:16 +0000 | [diff] [blame] | 6973 | NumThreadsVal = |
Alexey Bataev | dc9e7dc | 2019-04-17 16:53:08 +0000 | [diff] [blame] | 6974 | Bld.CreateIntCast(NumThreads, CGF.Int32Ty, /*IsSigned=*/false); |
Arpith Chacko Jacob | 99a1e0e | 2017-01-25 02:18:43 +0000 | [diff] [blame] | 6975 | ThreadLimitVal = ThreadLimitVal |
Alexey Bataev | 5c42736 | 2019-04-10 19:11:33 +0000 | [diff] [blame] | 6976 | ? Bld.CreateSelect(Bld.CreateICmpULT(NumThreadsVal, |
Arpith Chacko Jacob | 99a1e0e | 2017-01-25 02:18:43 +0000 | [diff] [blame] | 6977 | ThreadLimitVal), |
| 6978 | NumThreadsVal, ThreadLimitVal) |
| 6979 | : NumThreadsVal; |
Alexey Bataev | 50a1c78 | 2017-12-01 21:31:08 +0000 | [diff] [blame] | 6980 | } |
Alexey Bataev | dc9e7dc | 2019-04-17 16:53:08 +0000 | [diff] [blame] | 6981 | if (!ThreadLimitVal) |
| 6982 | ThreadLimitVal = Bld.getInt32(0); |
| 6983 | if (CondVal) |
| 6984 | return Bld.CreateSelect(CondVal, ThreadLimitVal, Bld.getInt32(1)); |
| 6985 | return ThreadLimitVal; |
| 6986 | } |
Alexey Bataev | 5c42736 | 2019-04-10 19:11:33 +0000 | [diff] [blame] | 6987 | case OMPD_target_teams_distribute_simd: |
| 6988 | case OMPD_target_simd: |
| 6989 | return Bld.getInt32(1); |
| 6990 | case OMPD_parallel: |
| 6991 | case OMPD_for: |
| 6992 | case OMPD_parallel_for: |
| 6993 | case OMPD_parallel_sections: |
| 6994 | case OMPD_for_simd: |
| 6995 | case OMPD_parallel_for_simd: |
| 6996 | case OMPD_cancel: |
| 6997 | case OMPD_cancellation_point: |
| 6998 | case OMPD_ordered: |
| 6999 | case OMPD_threadprivate: |
| 7000 | case OMPD_allocate: |
| 7001 | case OMPD_task: |
| 7002 | case OMPD_simd: |
| 7003 | case OMPD_sections: |
| 7004 | case OMPD_section: |
| 7005 | case OMPD_single: |
| 7006 | case OMPD_master: |
| 7007 | case OMPD_critical: |
| 7008 | case OMPD_taskyield: |
| 7009 | case OMPD_barrier: |
| 7010 | case OMPD_taskwait: |
| 7011 | case OMPD_taskgroup: |
| 7012 | case OMPD_atomic: |
| 7013 | case OMPD_flush: |
| 7014 | case OMPD_teams: |
| 7015 | case OMPD_target_data: |
| 7016 | case OMPD_target_exit_data: |
| 7017 | case OMPD_target_enter_data: |
| 7018 | case OMPD_distribute: |
| 7019 | case OMPD_distribute_simd: |
| 7020 | case OMPD_distribute_parallel_for: |
| 7021 | case OMPD_distribute_parallel_for_simd: |
| 7022 | case OMPD_teams_distribute: |
| 7023 | case OMPD_teams_distribute_simd: |
| 7024 | case OMPD_teams_distribute_parallel_for: |
| 7025 | case OMPD_teams_distribute_parallel_for_simd: |
| 7026 | case OMPD_target_update: |
| 7027 | case OMPD_declare_simd: |
| 7028 | case OMPD_declare_target: |
| 7029 | case OMPD_end_declare_target: |
| 7030 | case OMPD_declare_reduction: |
| 7031 | case OMPD_declare_mapper: |
| 7032 | case OMPD_taskloop: |
| 7033 | case OMPD_taskloop_simd: |
| 7034 | case OMPD_requires: |
| 7035 | case OMPD_unknown: |
| 7036 | break; |
Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 7037 | } |
Alexey Bataev | 5c42736 | 2019-04-10 19:11:33 +0000 | [diff] [blame] | 7038 | llvm_unreachable("Unsupported directive kind."); |
Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 7039 | } |
| 7040 | |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7041 | namespace { |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7042 | LLVM_ENABLE_BITMASK_ENUMS_IN_NAMESPACE(); |
| 7043 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 7044 | // Utility to handle information from clauses associated with a given |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7045 | // construct that use mappable expressions (e.g. 'map' clause, 'to' clause). |
| 7046 | // It provides a convenient interface to obtain the information and generate |
| 7047 | // code for that information. |
| 7048 | class MappableExprsHandler { |
| 7049 | public: |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 7050 | /// Values for bit flags used to specify the mapping type for |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7051 | /// offloading. |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7052 | enum OpenMPOffloadMappingFlags : uint64_t { |
| 7053 | /// No flags |
| 7054 | OMP_MAP_NONE = 0x0, |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 7055 | /// Allocate memory on the device and move data from host to device. |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7056 | OMP_MAP_TO = 0x01, |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 7057 | /// Allocate memory on the device and move data from device to host. |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7058 | OMP_MAP_FROM = 0x02, |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 7059 | /// Always perform the requested mapping action on the element, even |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7060 | /// if it was already mapped before. |
| 7061 | OMP_MAP_ALWAYS = 0x04, |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 7062 | /// Delete the element from the device environment, ignoring the |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7063 | /// current reference count associated with the element. |
Samuel Antao | 6782e94 | 2016-05-26 16:48:10 +0000 | [diff] [blame] | 7064 | OMP_MAP_DELETE = 0x08, |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 7065 | /// The element being mapped is a pointer-pointee pair; both the |
George Rokos | 065755d | 2017-11-07 18:27:04 +0000 | [diff] [blame] | 7066 | /// pointer and the pointee should be mapped. |
| 7067 | OMP_MAP_PTR_AND_OBJ = 0x10, |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 7068 | /// This flags signals that the base address of an entry should be |
George Rokos | 065755d | 2017-11-07 18:27:04 +0000 | [diff] [blame] | 7069 | /// passed to the target kernel as an argument. |
| 7070 | OMP_MAP_TARGET_PARAM = 0x20, |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 7071 | /// Signal that the runtime library has to return the device pointer |
George Rokos | 065755d | 2017-11-07 18:27:04 +0000 | [diff] [blame] | 7072 | /// in the current position for the data being mapped. Used when we have the |
| 7073 | /// use_device_ptr clause. |
| 7074 | OMP_MAP_RETURN_PARAM = 0x40, |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 7075 | /// This flag signals that the reference being passed is a pointer to |
Samuel Antao | d486f84 | 2016-05-26 16:53:38 +0000 | [diff] [blame] | 7076 | /// private data. |
George Rokos | 065755d | 2017-11-07 18:27:04 +0000 | [diff] [blame] | 7077 | OMP_MAP_PRIVATE = 0x80, |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 7078 | /// Pass the element to the device by value. |
George Rokos | 065755d | 2017-11-07 18:27:04 +0000 | [diff] [blame] | 7079 | OMP_MAP_LITERAL = 0x100, |
Alexey Bataev | f47c4b4 | 2017-09-26 13:47:31 +0000 | [diff] [blame] | 7080 | /// Implicit map |
| 7081 | OMP_MAP_IMPLICIT = 0x200, |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7082 | /// The 16 MSBs of the flags indicate whether the entry is member of some |
| 7083 | /// struct/class. |
| 7084 | OMP_MAP_MEMBER_OF = 0xffff000000000000, |
| 7085 | LLVM_MARK_AS_BITMASK_ENUM(/* LargestFlag = */ OMP_MAP_MEMBER_OF), |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7086 | }; |
| 7087 | |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7088 | /// Class that associates information with a base pointer to be passed to the |
| 7089 | /// runtime library. |
| 7090 | class BasePointerInfo { |
| 7091 | /// The base pointer. |
| 7092 | llvm::Value *Ptr = nullptr; |
| 7093 | /// The base declaration that refers to this device pointer, or null if |
| 7094 | /// there is none. |
| 7095 | const ValueDecl *DevPtrDecl = nullptr; |
| 7096 | |
| 7097 | public: |
| 7098 | BasePointerInfo(llvm::Value *Ptr, const ValueDecl *DevPtrDecl = nullptr) |
| 7099 | : Ptr(Ptr), DevPtrDecl(DevPtrDecl) {} |
| 7100 | llvm::Value *operator*() const { return Ptr; } |
| 7101 | const ValueDecl *getDevicePtrDecl() const { return DevPtrDecl; } |
| 7102 | void setDevicePtrDecl(const ValueDecl *D) { DevPtrDecl = D; } |
| 7103 | }; |
| 7104 | |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7105 | using MapBaseValuesArrayTy = SmallVector<BasePointerInfo, 4>; |
| 7106 | using MapValuesArrayTy = SmallVector<llvm::Value *, 4>; |
| 7107 | using MapFlagsArrayTy = SmallVector<OpenMPOffloadMappingFlags, 4>; |
| 7108 | |
| 7109 | /// Map between a struct and the its lowest & highest elements which have been |
| 7110 | /// mapped. |
| 7111 | /// [ValueDecl *] --> {LE(FieldIndex, Pointer), |
| 7112 | /// HE(FieldIndex, Pointer)} |
| 7113 | struct StructRangeInfoTy { |
| 7114 | std::pair<unsigned /*FieldIndex*/, Address /*Pointer*/> LowestElem = { |
| 7115 | 0, Address::invalid()}; |
| 7116 | std::pair<unsigned /*FieldIndex*/, Address /*Pointer*/> HighestElem = { |
| 7117 | 0, Address::invalid()}; |
| 7118 | Address Base = Address::invalid(); |
| 7119 | }; |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7120 | |
| 7121 | private: |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7122 | /// Kind that defines how a device pointer has to be returned. |
| 7123 | struct MapInfo { |
| 7124 | OMPClauseMappableExprCommon::MappableExprComponentListRef Components; |
| 7125 | OpenMPMapClauseKind MapType = OMPC_MAP_unknown; |
Kelvin Li | ef57943 | 2018-12-18 22:18:41 +0000 | [diff] [blame] | 7126 | ArrayRef<OpenMPMapModifierKind> MapModifiers; |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7127 | bool ReturnDevicePointer = false; |
| 7128 | bool IsImplicit = false; |
| 7129 | |
| 7130 | MapInfo() = default; |
| 7131 | MapInfo( |
| 7132 | OMPClauseMappableExprCommon::MappableExprComponentListRef Components, |
Kelvin Li | ef57943 | 2018-12-18 22:18:41 +0000 | [diff] [blame] | 7133 | OpenMPMapClauseKind MapType, |
| 7134 | ArrayRef<OpenMPMapModifierKind> MapModifiers, |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7135 | bool ReturnDevicePointer, bool IsImplicit) |
Kelvin Li | ef57943 | 2018-12-18 22:18:41 +0000 | [diff] [blame] | 7136 | : Components(Components), MapType(MapType), MapModifiers(MapModifiers), |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7137 | ReturnDevicePointer(ReturnDevicePointer), IsImplicit(IsImplicit) {} |
| 7138 | }; |
| 7139 | |
| 7140 | /// If use_device_ptr is used on a pointer which is a struct member and there |
| 7141 | /// is no map information about it, then emission of that entry is deferred |
| 7142 | /// until the whole struct has been processed. |
| 7143 | struct DeferredDevicePtrEntryTy { |
| 7144 | const Expr *IE = nullptr; |
| 7145 | const ValueDecl *VD = nullptr; |
| 7146 | |
| 7147 | DeferredDevicePtrEntryTy(const Expr *IE, const ValueDecl *VD) |
| 7148 | : IE(IE), VD(VD) {} |
| 7149 | }; |
| 7150 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 7151 | /// Directive from where the map clauses were extracted. |
Samuel Antao | 44bcdb3 | 2016-07-28 15:31:29 +0000 | [diff] [blame] | 7152 | const OMPExecutableDirective &CurDir; |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7153 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 7154 | /// Function the directive is being generated for. |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7155 | CodeGenFunction &CGF; |
| 7156 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 7157 | /// Set of all first private variables in the current directive. |
Samuel Antao | d486f84 | 2016-05-26 16:53:38 +0000 | [diff] [blame] | 7158 | llvm::SmallPtrSet<const VarDecl *, 8> FirstPrivateDecls; |
| 7159 | |
Samuel Antao | 6890b09 | 2016-07-28 14:25:09 +0000 | [diff] [blame] | 7160 | /// Map between device pointer declarations and their expression components. |
| 7161 | /// The key value for declarations in 'this' is null. |
| 7162 | llvm::DenseMap< |
| 7163 | const ValueDecl *, |
| 7164 | SmallVector<OMPClauseMappableExprCommon::MappableExprComponentListRef, 4>> |
| 7165 | DevPointersMap; |
| 7166 | |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7167 | llvm::Value *getExprTypeSize(const Expr *E) const { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 7168 | QualType ExprTy = E->getType().getCanonicalType(); |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7169 | |
| 7170 | // Reference types are ignored for mapping purposes. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 7171 | if (const auto *RefTy = ExprTy->getAs<ReferenceType>()) |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7172 | ExprTy = RefTy->getPointeeType().getCanonicalType(); |
| 7173 | |
| 7174 | // Given that an array section is considered a built-in type, we need to |
| 7175 | // do the calculation based on the length of the section instead of relying |
| 7176 | // on CGF.getTypeSize(E->getType()). |
| 7177 | if (const auto *OAE = dyn_cast<OMPArraySectionExpr>(E)) { |
| 7178 | QualType BaseTy = OMPArraySectionExpr::getBaseOriginalType( |
| 7179 | OAE->getBase()->IgnoreParenImpCasts()) |
| 7180 | .getCanonicalType(); |
| 7181 | |
| 7182 | // If there is no length associated with the expression, that means we |
| 7183 | // are using the whole length of the base. |
| 7184 | if (!OAE->getLength() && OAE->getColonLoc().isValid()) |
| 7185 | return CGF.getTypeSize(BaseTy); |
| 7186 | |
| 7187 | llvm::Value *ElemSize; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 7188 | if (const auto *PTy = BaseTy->getAs<PointerType>()) { |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7189 | ElemSize = CGF.getTypeSize(PTy->getPointeeType().getCanonicalType()); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 7190 | } else { |
| 7191 | const auto *ATy = cast<ArrayType>(BaseTy.getTypePtr()); |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7192 | assert(ATy && "Expecting array type if not a pointer type."); |
| 7193 | ElemSize = CGF.getTypeSize(ATy->getElementType().getCanonicalType()); |
| 7194 | } |
| 7195 | |
| 7196 | // If we don't have a length at this point, that is because we have an |
| 7197 | // array section with a single element. |
| 7198 | if (!OAE->getLength()) |
| 7199 | return ElemSize; |
| 7200 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 7201 | llvm::Value *LengthVal = CGF.EmitScalarExpr(OAE->getLength()); |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7202 | LengthVal = |
| 7203 | CGF.Builder.CreateIntCast(LengthVal, CGF.SizeTy, /*isSigned=*/false); |
| 7204 | return CGF.Builder.CreateNUWMul(LengthVal, ElemSize); |
| 7205 | } |
| 7206 | return CGF.getTypeSize(ExprTy); |
| 7207 | } |
| 7208 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 7209 | /// Return the corresponding bits for a given map clause modifier. Add |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7210 | /// 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] | 7211 | /// map as the first one of a series of maps that relate to the same map |
| 7212 | /// expression. |
Kelvin Li | ef57943 | 2018-12-18 22:18:41 +0000 | [diff] [blame] | 7213 | OpenMPOffloadMappingFlags getMapTypeBits( |
| 7214 | OpenMPMapClauseKind MapType, ArrayRef<OpenMPMapModifierKind> MapModifiers, |
| 7215 | bool IsImplicit, bool AddPtrFlag, bool AddIsTargetParamFlag) const { |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7216 | OpenMPOffloadMappingFlags Bits = |
| 7217 | IsImplicit ? OMP_MAP_IMPLICIT : OMP_MAP_NONE; |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7218 | switch (MapType) { |
| 7219 | case OMPC_MAP_alloc: |
Samuel Antao | 6782e94 | 2016-05-26 16:48:10 +0000 | [diff] [blame] | 7220 | case OMPC_MAP_release: |
| 7221 | // alloc and release is the default behavior in the runtime library, i.e. |
| 7222 | // if we don't pass any bits alloc/release that is what the runtime is |
| 7223 | // going to do. Therefore, we don't need to signal anything for these two |
| 7224 | // type modifiers. |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7225 | break; |
| 7226 | case OMPC_MAP_to: |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7227 | Bits |= OMP_MAP_TO; |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7228 | break; |
| 7229 | case OMPC_MAP_from: |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7230 | Bits |= OMP_MAP_FROM; |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7231 | break; |
| 7232 | case OMPC_MAP_tofrom: |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7233 | Bits |= OMP_MAP_TO | OMP_MAP_FROM; |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7234 | break; |
| 7235 | case OMPC_MAP_delete: |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7236 | Bits |= OMP_MAP_DELETE; |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7237 | break; |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7238 | case OMPC_MAP_unknown: |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7239 | llvm_unreachable("Unexpected map type!"); |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7240 | } |
| 7241 | if (AddPtrFlag) |
George Rokos | 065755d | 2017-11-07 18:27:04 +0000 | [diff] [blame] | 7242 | Bits |= OMP_MAP_PTR_AND_OBJ; |
| 7243 | if (AddIsTargetParamFlag) |
| 7244 | Bits |= OMP_MAP_TARGET_PARAM; |
Kelvin Li | ef57943 | 2018-12-18 22:18:41 +0000 | [diff] [blame] | 7245 | if (llvm::find(MapModifiers, OMPC_MAP_MODIFIER_always) |
| 7246 | != MapModifiers.end()) |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7247 | Bits |= OMP_MAP_ALWAYS; |
| 7248 | return Bits; |
| 7249 | } |
| 7250 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 7251 | /// Return true if the provided expression is a final array section. A |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7252 | /// final array section, is one whose length can't be proved to be one. |
| 7253 | bool isFinalArraySectionExpression(const Expr *E) const { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 7254 | const auto *OASE = dyn_cast<OMPArraySectionExpr>(E); |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7255 | |
| 7256 | // It is not an array section and therefore not a unity-size one. |
| 7257 | if (!OASE) |
| 7258 | return false; |
| 7259 | |
| 7260 | // An array section with no colon always refer to a single element. |
| 7261 | if (OASE->getColonLoc().isInvalid()) |
| 7262 | return false; |
| 7263 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 7264 | const Expr *Length = OASE->getLength(); |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7265 | |
| 7266 | // If we don't have a length we have to check if the array has size 1 |
| 7267 | // for this dimension. Also, we should always expect a length if the |
| 7268 | // base type is pointer. |
| 7269 | if (!Length) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 7270 | QualType BaseQTy = OMPArraySectionExpr::getBaseOriginalType( |
| 7271 | OASE->getBase()->IgnoreParenImpCasts()) |
| 7272 | .getCanonicalType(); |
| 7273 | if (const auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr())) |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7274 | return ATy->getSize().getSExtValue() != 1; |
| 7275 | // If we don't have a constant dimension length, we have to consider |
| 7276 | // the current section as having any size, so it is not necessarily |
| 7277 | // unitary. If it happen to be unity size, that's user fault. |
| 7278 | return true; |
| 7279 | } |
| 7280 | |
| 7281 | // Check if the length evaluates to 1. |
Fangrui Song | 407659a | 2018-11-30 23:41:18 +0000 | [diff] [blame] | 7282 | Expr::EvalResult Result; |
| 7283 | if (!Length->EvaluateAsInt(Result, CGF.getContext())) |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7284 | return true; // Can have more that size 1. |
| 7285 | |
Fangrui Song | 407659a | 2018-11-30 23:41:18 +0000 | [diff] [blame] | 7286 | llvm::APSInt ConstLength = Result.Val.getInt(); |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7287 | return ConstLength.getSExtValue() != 1; |
| 7288 | } |
| 7289 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 7290 | /// Generate the base pointers, section pointers, sizes and map type |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7291 | /// bits for the provided map type, map modifier, and expression components. |
| 7292 | /// \a IsFirstComponent should be set to true if the provided set of |
| 7293 | /// components is the first associated with a capture. |
| 7294 | void generateInfoForComponentList( |
Kelvin Li | ef57943 | 2018-12-18 22:18:41 +0000 | [diff] [blame] | 7295 | OpenMPMapClauseKind MapType, |
| 7296 | ArrayRef<OpenMPMapModifierKind> MapModifiers, |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7297 | OMPClauseMappableExprCommon::MappableExprComponentListRef Components, |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7298 | MapBaseValuesArrayTy &BasePointers, MapValuesArrayTy &Pointers, |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7299 | MapValuesArrayTy &Sizes, MapFlagsArrayTy &Types, |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7300 | StructRangeInfoTy &PartialStruct, bool IsFirstComponentList, |
Alexey Bataev | e82445f | 2018-09-20 13:54:02 +0000 | [diff] [blame] | 7301 | bool IsImplicit, |
| 7302 | ArrayRef<OMPClauseMappableExprCommon::MappableExprComponentListRef> |
| 7303 | OverlappedElements = llvm::None) const { |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7304 | // 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] | 7305 | // types below. The generated information is expressed in this order: |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7306 | // base pointer, section pointer, size, flags |
| 7307 | // (to add to the ones that come from the map type and modifier). |
| 7308 | // |
| 7309 | // double d; |
| 7310 | // int i[100]; |
| 7311 | // float *p; |
| 7312 | // |
| 7313 | // struct S1 { |
| 7314 | // int i; |
| 7315 | // float f[50]; |
| 7316 | // } |
| 7317 | // struct S2 { |
| 7318 | // int i; |
| 7319 | // float f[50]; |
| 7320 | // S1 s; |
| 7321 | // double *p; |
| 7322 | // struct S2 *ps; |
| 7323 | // } |
| 7324 | // S2 s; |
| 7325 | // S2 *ps; |
| 7326 | // |
| 7327 | // map(d) |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7328 | // &d, &d, sizeof(double), TARGET_PARAM | TO | FROM |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7329 | // |
| 7330 | // map(i) |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7331 | // &i, &i, 100*sizeof(int), TARGET_PARAM | TO | FROM |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7332 | // |
| 7333 | // map(i[1:23]) |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7334 | // &i(=&i[0]), &i[1], 23*sizeof(int), TARGET_PARAM | TO | FROM |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7335 | // |
| 7336 | // map(p) |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7337 | // &p, &p, sizeof(float*), TARGET_PARAM | TO | FROM |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7338 | // |
| 7339 | // map(p[1:24]) |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7340 | // p, &p[1], 24*sizeof(float), TARGET_PARAM | TO | FROM |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7341 | // |
| 7342 | // map(s) |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7343 | // &s, &s, sizeof(S2), TARGET_PARAM | TO | FROM |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7344 | // |
| 7345 | // map(s.i) |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7346 | // &s, &(s.i), sizeof(int), TARGET_PARAM | TO | FROM |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7347 | // |
| 7348 | // map(s.s.f) |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7349 | // &s, &(s.s.f[0]), 50*sizeof(float), TARGET_PARAM | TO | FROM |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7350 | // |
| 7351 | // map(s.p) |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7352 | // &s, &(s.p), sizeof(double*), TARGET_PARAM | TO | FROM |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7353 | // |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7354 | // map(to: s.p[:22]) |
| 7355 | // &s, &(s.p), sizeof(double*), TARGET_PARAM (*) |
| 7356 | // &s, &(s.p), sizeof(double*), MEMBER_OF(1) (**) |
| 7357 | // &(s.p), &(s.p[0]), 22*sizeof(double), |
| 7358 | // MEMBER_OF(1) | PTR_AND_OBJ | TO (***) |
| 7359 | // (*) alloc space for struct members, only this is a target parameter |
| 7360 | // (**) map the pointer (nothing to be mapped in this example) (the compiler |
| 7361 | // optimizes this entry out, same in the examples below) |
| 7362 | // (***) map the pointee (map: to) |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7363 | // |
| 7364 | // map(s.ps) |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7365 | // &s, &(s.ps), sizeof(S2*), TARGET_PARAM | TO | FROM |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7366 | // |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7367 | // map(from: s.ps->s.i) |
| 7368 | // &s, &(s.ps), sizeof(S2*), TARGET_PARAM |
| 7369 | // &s, &(s.ps), sizeof(S2*), MEMBER_OF(1) |
| 7370 | // &(s.ps), &(s.ps->s.i), sizeof(int), MEMBER_OF(1) | PTR_AND_OBJ | FROM |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7371 | // |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7372 | // map(to: s.ps->ps) |
| 7373 | // &s, &(s.ps), sizeof(S2*), TARGET_PARAM |
| 7374 | // &s, &(s.ps), sizeof(S2*), MEMBER_OF(1) |
| 7375 | // &(s.ps), &(s.ps->ps), sizeof(S2*), MEMBER_OF(1) | PTR_AND_OBJ | TO |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7376 | // |
| 7377 | // map(s.ps->ps->ps) |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7378 | // &s, &(s.ps), sizeof(S2*), TARGET_PARAM |
| 7379 | // &s, &(s.ps), sizeof(S2*), MEMBER_OF(1) |
| 7380 | // &(s.ps), &(s.ps->ps), sizeof(S2*), MEMBER_OF(1) | PTR_AND_OBJ |
| 7381 | // &(s.ps->ps), &(s.ps->ps->ps), sizeof(S2*), PTR_AND_OBJ | TO | FROM |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7382 | // |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7383 | // map(to: s.ps->ps->s.f[:22]) |
| 7384 | // &s, &(s.ps), sizeof(S2*), TARGET_PARAM |
| 7385 | // &s, &(s.ps), sizeof(S2*), MEMBER_OF(1) |
| 7386 | // &(s.ps), &(s.ps->ps), sizeof(S2*), MEMBER_OF(1) | PTR_AND_OBJ |
| 7387 | // &(s.ps->ps), &(s.ps->ps->s.f[0]), 22*sizeof(float), PTR_AND_OBJ | TO |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7388 | // |
| 7389 | // map(ps) |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7390 | // &ps, &ps, sizeof(S2*), TARGET_PARAM | TO | FROM |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7391 | // |
| 7392 | // map(ps->i) |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7393 | // ps, &(ps->i), sizeof(int), TARGET_PARAM | TO | FROM |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7394 | // |
| 7395 | // map(ps->s.f) |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7396 | // ps, &(ps->s.f[0]), 50*sizeof(float), TARGET_PARAM | TO | FROM |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7397 | // |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7398 | // map(from: ps->p) |
| 7399 | // ps, &(ps->p), sizeof(double*), TARGET_PARAM | FROM |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7400 | // |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7401 | // map(to: ps->p[:22]) |
| 7402 | // ps, &(ps->p), sizeof(double*), TARGET_PARAM |
| 7403 | // ps, &(ps->p), sizeof(double*), MEMBER_OF(1) |
| 7404 | // &(ps->p), &(ps->p[0]), 22*sizeof(double), MEMBER_OF(1) | PTR_AND_OBJ | TO |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7405 | // |
| 7406 | // map(ps->ps) |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7407 | // ps, &(ps->ps), sizeof(S2*), TARGET_PARAM | TO | FROM |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7408 | // |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7409 | // map(from: ps->ps->s.i) |
| 7410 | // ps, &(ps->ps), sizeof(S2*), TARGET_PARAM |
| 7411 | // ps, &(ps->ps), sizeof(S2*), MEMBER_OF(1) |
| 7412 | // &(ps->ps), &(ps->ps->s.i), sizeof(int), MEMBER_OF(1) | PTR_AND_OBJ | FROM |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7413 | // |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7414 | // map(from: ps->ps->ps) |
| 7415 | // ps, &(ps->ps), sizeof(S2*), TARGET_PARAM |
| 7416 | // ps, &(ps->ps), sizeof(S2*), MEMBER_OF(1) |
| 7417 | // &(ps->ps), &(ps->ps->ps), sizeof(S2*), MEMBER_OF(1) | PTR_AND_OBJ | FROM |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7418 | // |
| 7419 | // map(ps->ps->ps->ps) |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7420 | // ps, &(ps->ps), sizeof(S2*), TARGET_PARAM |
| 7421 | // ps, &(ps->ps), sizeof(S2*), MEMBER_OF(1) |
| 7422 | // &(ps->ps), &(ps->ps->ps), sizeof(S2*), MEMBER_OF(1) | PTR_AND_OBJ |
| 7423 | // &(ps->ps->ps), &(ps->ps->ps->ps), sizeof(S2*), PTR_AND_OBJ | TO | FROM |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7424 | // |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7425 | // map(to: ps->ps->ps->s.f[:22]) |
| 7426 | // ps, &(ps->ps), sizeof(S2*), TARGET_PARAM |
| 7427 | // ps, &(ps->ps), sizeof(S2*), MEMBER_OF(1) |
| 7428 | // &(ps->ps), &(ps->ps->ps), sizeof(S2*), MEMBER_OF(1) | PTR_AND_OBJ |
| 7429 | // &(ps->ps->ps), &(ps->ps->ps->s.f[0]), 22*sizeof(float), PTR_AND_OBJ | TO |
| 7430 | // |
| 7431 | // map(to: s.f[:22]) map(from: s.p[:33]) |
| 7432 | // &s, &(s.f[0]), 50*sizeof(float) + sizeof(struct S1) + |
| 7433 | // sizeof(double*) (**), TARGET_PARAM |
| 7434 | // &s, &(s.f[0]), 22*sizeof(float), MEMBER_OF(1) | TO |
| 7435 | // &s, &(s.p), sizeof(double*), MEMBER_OF(1) |
| 7436 | // &(s.p), &(s.p[0]), 33*sizeof(double), MEMBER_OF(1) | PTR_AND_OBJ | FROM |
| 7437 | // (*) allocate contiguous space needed to fit all mapped members even if |
| 7438 | // we allocate space for members not mapped (in this example, |
| 7439 | // s.f[22..49] and s.s are not mapped, yet we must allocate space for |
| 7440 | // them as well because they fall between &s.f[0] and &s.p) |
| 7441 | // |
| 7442 | // map(from: s.f[:22]) map(to: ps->p[:33]) |
| 7443 | // &s, &(s.f[0]), 22*sizeof(float), TARGET_PARAM | FROM |
| 7444 | // ps, &(ps->p), sizeof(S2*), TARGET_PARAM |
| 7445 | // ps, &(ps->p), sizeof(double*), MEMBER_OF(2) (*) |
| 7446 | // &(ps->p), &(ps->p[0]), 33*sizeof(double), MEMBER_OF(2) | PTR_AND_OBJ | TO |
| 7447 | // (*) the struct this entry pertains to is the 2nd element in the list of |
| 7448 | // arguments, hence MEMBER_OF(2) |
| 7449 | // |
| 7450 | // map(from: s.f[:22], s.s) map(to: ps->p[:33]) |
| 7451 | // &s, &(s.f[0]), 50*sizeof(float) + sizeof(struct S1), TARGET_PARAM |
| 7452 | // &s, &(s.f[0]), 22*sizeof(float), MEMBER_OF(1) | FROM |
| 7453 | // &s, &(s.s), sizeof(struct S1), MEMBER_OF(1) | FROM |
| 7454 | // ps, &(ps->p), sizeof(S2*), TARGET_PARAM |
| 7455 | // ps, &(ps->p), sizeof(double*), MEMBER_OF(4) (*) |
| 7456 | // &(ps->p), &(ps->p[0]), 33*sizeof(double), MEMBER_OF(4) | PTR_AND_OBJ | TO |
| 7457 | // (*) the struct this entry pertains to is the 4th element in the list |
| 7458 | // of arguments, hence MEMBER_OF(4) |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7459 | |
| 7460 | // Track if the map information being generated is the first for a capture. |
| 7461 | bool IsCaptureFirstInfo = IsFirstComponentList; |
Gheorghe-Teodor Bercea | 0034e84 | 2019-06-20 18:04:47 +0000 | [diff] [blame] | 7462 | // When the variable is on a declare target link or in a to clause with |
| 7463 | // unified memory, a reference is needed to hold the host/device address |
| 7464 | // of the variable. |
| 7465 | bool RequiresReference = false; |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7466 | |
| 7467 | // Scan the components from the base to the complete expression. |
| 7468 | auto CI = Components.rbegin(); |
| 7469 | auto CE = Components.rend(); |
| 7470 | auto I = CI; |
| 7471 | |
| 7472 | // Track if the map information being generated is the first for a list of |
| 7473 | // components. |
| 7474 | bool IsExpressionFirstInfo = true; |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7475 | Address BP = Address::invalid(); |
Patrick Lyster | e13b1e3 | 2019-01-02 19:28:48 +0000 | [diff] [blame] | 7476 | const Expr *AssocExpr = I->getAssociatedExpression(); |
| 7477 | const auto *AE = dyn_cast<ArraySubscriptExpr>(AssocExpr); |
| 7478 | const auto *OASE = dyn_cast<OMPArraySectionExpr>(AssocExpr); |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7479 | |
Patrick Lyster | e13b1e3 | 2019-01-02 19:28:48 +0000 | [diff] [blame] | 7480 | if (isa<MemberExpr>(AssocExpr)) { |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7481 | // The base is the 'this' pointer. The content of the pointer is going |
| 7482 | // to be the base of the field being mapped. |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7483 | BP = CGF.LoadCXXThisAddress(); |
Patrick Lyster | e13b1e3 | 2019-01-02 19:28:48 +0000 | [diff] [blame] | 7484 | } else if ((AE && isa<CXXThisExpr>(AE->getBase()->IgnoreParenImpCasts())) || |
| 7485 | (OASE && |
| 7486 | isa<CXXThisExpr>(OASE->getBase()->IgnoreParenImpCasts()))) { |
| 7487 | BP = CGF.EmitOMPSharedLValue(AssocExpr).getAddress(); |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7488 | } else { |
| 7489 | // The base is the reference to the variable. |
| 7490 | // BP = &Var. |
Patrick Lyster | e13b1e3 | 2019-01-02 19:28:48 +0000 | [diff] [blame] | 7491 | BP = CGF.EmitOMPSharedLValue(AssocExpr).getAddress(); |
Alexey Bataev | 92327c5 | 2018-03-26 16:40:55 +0000 | [diff] [blame] | 7492 | if (const auto *VD = |
| 7493 | dyn_cast_or_null<VarDecl>(I->getAssociatedDeclaration())) { |
| 7494 | if (llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res = |
Gheorghe-Teodor Bercea | 0034e84 | 2019-06-20 18:04:47 +0000 | [diff] [blame] | 7495 | OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD)) { |
| 7496 | if ((*Res == OMPDeclareTargetDeclAttr::MT_Link) || |
| 7497 | (*Res == OMPDeclareTargetDeclAttr::MT_To && |
| 7498 | CGF.CGM.getOpenMPRuntime().hasRequiresUnifiedSharedMemory())) { |
| 7499 | RequiresReference = true; |
| 7500 | BP = CGF.CGM.getOpenMPRuntime().getAddrOfDeclareTargetVar(VD); |
Alexey Bataev | 2c1dffe | 2018-04-16 20:34:41 +0000 | [diff] [blame] | 7501 | } |
Gheorghe-Teodor Bercea | 0034e84 | 2019-06-20 18:04:47 +0000 | [diff] [blame] | 7502 | } |
Alexey Bataev | 92327c5 | 2018-03-26 16:40:55 +0000 | [diff] [blame] | 7503 | } |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7504 | |
| 7505 | // 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] | 7506 | // 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] | 7507 | // reference. References are ignored for mapping purposes. |
| 7508 | QualType Ty = |
| 7509 | I->getAssociatedDeclaration()->getType().getNonReferenceType(); |
| 7510 | if (Ty->isAnyPointerType() && std::next(I) != CE) { |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7511 | BP = CGF.EmitLoadOfPointer(BP, Ty->castAs<PointerType>()); |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7512 | |
| 7513 | // We do not need to generate individual map information for the |
| 7514 | // pointer, it can be associated with the combined storage. |
| 7515 | ++I; |
| 7516 | } |
| 7517 | } |
| 7518 | |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7519 | // Track whether a component of the list should be marked as MEMBER_OF some |
| 7520 | // combined entry (for partial structs). Only the first PTR_AND_OBJ entry |
| 7521 | // in a component list should be marked as MEMBER_OF, all subsequent entries |
| 7522 | // do not belong to the base struct. E.g. |
| 7523 | // struct S2 s; |
| 7524 | // s.ps->ps->ps->f[:] |
| 7525 | // (1) (2) (3) (4) |
| 7526 | // ps(1) is a member pointer, ps(2) is a pointee of ps(1), so it is a |
| 7527 | // PTR_AND_OBJ entry; the PTR is ps(1), so MEMBER_OF the base struct. ps(3) |
| 7528 | // is the pointee of ps(2) which is not member of struct s, so it should not |
| 7529 | // be marked as such (it is still PTR_AND_OBJ). |
| 7530 | // The variable is initialized to false so that PTR_AND_OBJ entries which |
| 7531 | // are not struct members are not considered (e.g. array of pointers to |
| 7532 | // data). |
| 7533 | bool ShouldBeMemberOf = false; |
| 7534 | |
| 7535 | // Variable keeping track of whether or not we have encountered a component |
| 7536 | // in the component list which is a member expression. Useful when we have a |
| 7537 | // pointer or a final array section, in which case it is the previous |
| 7538 | // component in the list which tells us whether we have a member expression. |
| 7539 | // E.g. X.f[:] |
| 7540 | // While processing the final array section "[:]" it is "f" which tells us |
| 7541 | // whether we are dealing with a member of a declared struct. |
| 7542 | const MemberExpr *EncounteredME = nullptr; |
| 7543 | |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7544 | for (; I != CE; ++I) { |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7545 | // If the current component is member of a struct (parent struct) mark it. |
| 7546 | if (!EncounteredME) { |
| 7547 | EncounteredME = dyn_cast<MemberExpr>(I->getAssociatedExpression()); |
| 7548 | // If we encounter a PTR_AND_OBJ entry from now on it should be marked |
| 7549 | // as MEMBER_OF the parent struct. |
| 7550 | if (EncounteredME) |
| 7551 | ShouldBeMemberOf = true; |
| 7552 | } |
| 7553 | |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7554 | auto Next = std::next(I); |
| 7555 | |
| 7556 | // We need to generate the addresses and sizes if this is the last |
| 7557 | // component, if the component is a pointer or if it is an array section |
| 7558 | // whose length can't be proved to be one. If this is a pointer, it |
| 7559 | // becomes the base address for the following components. |
| 7560 | |
| 7561 | // A final array section, is one whose length can't be proved to be one. |
| 7562 | bool IsFinalArraySection = |
| 7563 | isFinalArraySectionExpression(I->getAssociatedExpression()); |
| 7564 | |
| 7565 | // Get information on whether the element is a pointer. Have to do a |
| 7566 | // special treatment for array sections given that they are built-in |
| 7567 | // types. |
| 7568 | const auto *OASE = |
| 7569 | dyn_cast<OMPArraySectionExpr>(I->getAssociatedExpression()); |
| 7570 | bool IsPointer = |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7571 | (OASE && OMPArraySectionExpr::getBaseOriginalType(OASE) |
| 7572 | .getCanonicalType() |
| 7573 | ->isAnyPointerType()) || |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7574 | I->getAssociatedExpression()->getType()->isAnyPointerType(); |
| 7575 | |
| 7576 | if (Next == CE || IsPointer || IsFinalArraySection) { |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7577 | // If this is not the last component, we expect the pointer to be |
| 7578 | // associated with an array expression or member expression. |
| 7579 | assert((Next == CE || |
| 7580 | isa<MemberExpr>(Next->getAssociatedExpression()) || |
| 7581 | isa<ArraySubscriptExpr>(Next->getAssociatedExpression()) || |
| 7582 | isa<OMPArraySectionExpr>(Next->getAssociatedExpression())) && |
| 7583 | "Unexpected expression"); |
| 7584 | |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7585 | Address LB = |
| 7586 | CGF.EmitOMPSharedLValue(I->getAssociatedExpression()).getAddress(); |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7587 | |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7588 | // If this component is a pointer inside the base struct then we don't |
| 7589 | // need to create any entry for it - it will be combined with the object |
| 7590 | // it is pointing to into a single PTR_AND_OBJ entry. |
| 7591 | bool IsMemberPointer = |
| 7592 | IsPointer && EncounteredME && |
| 7593 | (dyn_cast<MemberExpr>(I->getAssociatedExpression()) == |
| 7594 | EncounteredME); |
Alexey Bataev | e82445f | 2018-09-20 13:54:02 +0000 | [diff] [blame] | 7595 | if (!OverlappedElements.empty()) { |
| 7596 | // Handle base element with the info for overlapped elements. |
| 7597 | assert(!PartialStruct.Base.isValid() && "The base element is set."); |
| 7598 | assert(Next == CE && |
| 7599 | "Expected last element for the overlapped elements."); |
| 7600 | assert(!IsPointer && |
| 7601 | "Unexpected base element with the pointer type."); |
| 7602 | // Mark the whole struct as the struct that requires allocation on the |
| 7603 | // device. |
| 7604 | PartialStruct.LowestElem = {0, LB}; |
| 7605 | CharUnits TypeSize = CGF.getContext().getTypeSizeInChars( |
| 7606 | I->getAssociatedExpression()->getType()); |
| 7607 | Address HB = CGF.Builder.CreateConstGEP( |
| 7608 | CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(LB, |
| 7609 | CGF.VoidPtrTy), |
James Y Knight | 751fe28 | 2019-02-09 22:22:28 +0000 | [diff] [blame] | 7610 | TypeSize.getQuantity() - 1); |
Alexey Bataev | e82445f | 2018-09-20 13:54:02 +0000 | [diff] [blame] | 7611 | PartialStruct.HighestElem = { |
| 7612 | std::numeric_limits<decltype( |
| 7613 | PartialStruct.HighestElem.first)>::max(), |
| 7614 | HB}; |
| 7615 | PartialStruct.Base = BP; |
| 7616 | // Emit data for non-overlapped data. |
| 7617 | OpenMPOffloadMappingFlags Flags = |
| 7618 | OMP_MAP_MEMBER_OF | |
Kelvin Li | ef57943 | 2018-12-18 22:18:41 +0000 | [diff] [blame] | 7619 | getMapTypeBits(MapType, MapModifiers, IsImplicit, |
Alexey Bataev | e82445f | 2018-09-20 13:54:02 +0000 | [diff] [blame] | 7620 | /*AddPtrFlag=*/false, |
| 7621 | /*AddIsTargetParamFlag=*/false); |
| 7622 | LB = BP; |
| 7623 | llvm::Value *Size = nullptr; |
| 7624 | // Do bitcopy of all non-overlapped structure elements. |
| 7625 | for (OMPClauseMappableExprCommon::MappableExprComponentListRef |
| 7626 | Component : OverlappedElements) { |
| 7627 | Address ComponentLB = Address::invalid(); |
| 7628 | for (const OMPClauseMappableExprCommon::MappableComponent &MC : |
| 7629 | Component) { |
| 7630 | if (MC.getAssociatedDeclaration()) { |
| 7631 | ComponentLB = |
| 7632 | CGF.EmitOMPSharedLValue(MC.getAssociatedExpression()) |
| 7633 | .getAddress(); |
| 7634 | Size = CGF.Builder.CreatePtrDiff( |
| 7635 | CGF.EmitCastToVoidPtr(ComponentLB.getPointer()), |
| 7636 | CGF.EmitCastToVoidPtr(LB.getPointer())); |
| 7637 | break; |
| 7638 | } |
| 7639 | } |
| 7640 | BasePointers.push_back(BP.getPointer()); |
| 7641 | Pointers.push_back(LB.getPointer()); |
| 7642 | Sizes.push_back(Size); |
| 7643 | Types.push_back(Flags); |
James Y Knight | 751fe28 | 2019-02-09 22:22:28 +0000 | [diff] [blame] | 7644 | LB = CGF.Builder.CreateConstGEP(ComponentLB, 1); |
Alexey Bataev | e82445f | 2018-09-20 13:54:02 +0000 | [diff] [blame] | 7645 | } |
| 7646 | BasePointers.push_back(BP.getPointer()); |
| 7647 | Pointers.push_back(LB.getPointer()); |
| 7648 | Size = CGF.Builder.CreatePtrDiff( |
| 7649 | CGF.EmitCastToVoidPtr( |
James Y Knight | 751fe28 | 2019-02-09 22:22:28 +0000 | [diff] [blame] | 7650 | CGF.Builder.CreateConstGEP(HB, 1).getPointer()), |
Alexey Bataev | e82445f | 2018-09-20 13:54:02 +0000 | [diff] [blame] | 7651 | CGF.EmitCastToVoidPtr(LB.getPointer())); |
| 7652 | Sizes.push_back(Size); |
| 7653 | Types.push_back(Flags); |
| 7654 | break; |
| 7655 | } |
| 7656 | llvm::Value *Size = getExprTypeSize(I->getAssociatedExpression()); |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7657 | if (!IsMemberPointer) { |
| 7658 | BasePointers.push_back(BP.getPointer()); |
| 7659 | Pointers.push_back(LB.getPointer()); |
| 7660 | Sizes.push_back(Size); |
Samuel Antao | 03a3cec | 2016-07-27 22:52:16 +0000 | [diff] [blame] | 7661 | |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7662 | // We need to add a pointer flag for each map that comes from the |
| 7663 | // same expression except for the first one. We also need to signal |
| 7664 | // this map is the first one that relates with the current capture |
| 7665 | // (there is a set of entries for each capture). |
| 7666 | OpenMPOffloadMappingFlags Flags = getMapTypeBits( |
Kelvin Li | ef57943 | 2018-12-18 22:18:41 +0000 | [diff] [blame] | 7667 | MapType, MapModifiers, IsImplicit, |
Gheorghe-Teodor Bercea | 0034e84 | 2019-06-20 18:04:47 +0000 | [diff] [blame] | 7668 | !IsExpressionFirstInfo || RequiresReference, |
| 7669 | IsCaptureFirstInfo && !RequiresReference); |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7670 | |
| 7671 | if (!IsExpressionFirstInfo) { |
| 7672 | // If we have a PTR_AND_OBJ pair where the OBJ is a pointer as well, |
| 7673 | // then we reset the TO/FROM/ALWAYS/DELETE flags. |
| 7674 | if (IsPointer) |
| 7675 | Flags &= ~(OMP_MAP_TO | OMP_MAP_FROM | OMP_MAP_ALWAYS | |
| 7676 | OMP_MAP_DELETE); |
| 7677 | |
| 7678 | if (ShouldBeMemberOf) { |
| 7679 | // Set placeholder value MEMBER_OF=FFFF to indicate that the flag |
| 7680 | // should be later updated with the correct value of MEMBER_OF. |
| 7681 | Flags |= OMP_MAP_MEMBER_OF; |
| 7682 | // From now on, all subsequent PTR_AND_OBJ entries should not be |
| 7683 | // marked as MEMBER_OF. |
| 7684 | ShouldBeMemberOf = false; |
| 7685 | } |
| 7686 | } |
| 7687 | |
| 7688 | Types.push_back(Flags); |
Samuel Antao | 03a3cec | 2016-07-27 22:52:16 +0000 | [diff] [blame] | 7689 | } |
| 7690 | |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7691 | // If we have encountered a member expression so far, keep track of the |
| 7692 | // mapped member. If the parent is "*this", then the value declaration |
| 7693 | // is nullptr. |
| 7694 | if (EncounteredME) { |
| 7695 | const auto *FD = dyn_cast<FieldDecl>(EncounteredME->getMemberDecl()); |
| 7696 | unsigned FieldIndex = FD->getFieldIndex(); |
Samuel Antao | 03a3cec | 2016-07-27 22:52:16 +0000 | [diff] [blame] | 7697 | |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7698 | // Update info about the lowest and highest elements for this struct |
| 7699 | if (!PartialStruct.Base.isValid()) { |
| 7700 | PartialStruct.LowestElem = {FieldIndex, LB}; |
| 7701 | PartialStruct.HighestElem = {FieldIndex, LB}; |
| 7702 | PartialStruct.Base = BP; |
| 7703 | } else if (FieldIndex < PartialStruct.LowestElem.first) { |
| 7704 | PartialStruct.LowestElem = {FieldIndex, LB}; |
| 7705 | } else if (FieldIndex > PartialStruct.HighestElem.first) { |
| 7706 | PartialStruct.HighestElem = {FieldIndex, LB}; |
| 7707 | } |
| 7708 | } |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7709 | |
| 7710 | // If we have a final array section, we are done with this expression. |
| 7711 | if (IsFinalArraySection) |
| 7712 | break; |
| 7713 | |
| 7714 | // The pointer becomes the base for the next element. |
| 7715 | if (Next != CE) |
| 7716 | BP = LB; |
| 7717 | |
| 7718 | IsExpressionFirstInfo = false; |
| 7719 | IsCaptureFirstInfo = false; |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7720 | } |
| 7721 | } |
| 7722 | } |
| 7723 | |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7724 | /// Return the adjusted map modifiers if the declaration a capture refers to |
| 7725 | /// appears in a first-private clause. This is expected to be used only with |
| 7726 | /// directives that start with 'target'. |
| 7727 | MappableExprsHandler::OpenMPOffloadMappingFlags |
| 7728 | getMapModifiersForPrivateClauses(const CapturedStmt::Capture &Cap) const { |
| 7729 | assert(Cap.capturesVariable() && "Expected capture by reference only!"); |
| 7730 | |
| 7731 | // A first private variable captured by reference will use only the |
| 7732 | // 'private ptr' and 'map to' flag. Return the right flags if the captured |
| 7733 | // declaration is known as first-private in this handler. |
Alexey Bataev | 1af5bd5 | 2019-03-05 17:47:18 +0000 | [diff] [blame] | 7734 | if (FirstPrivateDecls.count(Cap.getCapturedVar())) { |
| 7735 | if (Cap.getCapturedVar()->getType().isConstant(CGF.getContext()) && |
| 7736 | Cap.getCaptureKind() == CapturedStmt::VCK_ByRef) |
| 7737 | return MappableExprsHandler::OMP_MAP_ALWAYS | |
| 7738 | MappableExprsHandler::OMP_MAP_TO; |
Alexey Bataev | c2b831f | 2019-04-02 16:03:40 +0000 | [diff] [blame] | 7739 | if (Cap.getCapturedVar()->getType()->isAnyPointerType()) |
| 7740 | return MappableExprsHandler::OMP_MAP_TO | |
| 7741 | MappableExprsHandler::OMP_MAP_PTR_AND_OBJ; |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7742 | return MappableExprsHandler::OMP_MAP_PRIVATE | |
| 7743 | MappableExprsHandler::OMP_MAP_TO; |
Alexey Bataev | 1af5bd5 | 2019-03-05 17:47:18 +0000 | [diff] [blame] | 7744 | } |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7745 | return MappableExprsHandler::OMP_MAP_TO | |
| 7746 | MappableExprsHandler::OMP_MAP_FROM; |
| 7747 | } |
| 7748 | |
| 7749 | static OpenMPOffloadMappingFlags getMemberOfFlag(unsigned Position) { |
| 7750 | // Member of is given by the 16 MSB of the flag, so rotate by 48 bits. |
| 7751 | return static_cast<OpenMPOffloadMappingFlags>(((uint64_t)Position + 1) |
| 7752 | << 48); |
| 7753 | } |
| 7754 | |
| 7755 | static void setCorrectMemberOfFlag(OpenMPOffloadMappingFlags &Flags, |
| 7756 | OpenMPOffloadMappingFlags MemberOfFlag) { |
| 7757 | // If the entry is PTR_AND_OBJ but has not been marked with the special |
| 7758 | // placeholder value 0xFFFF in the MEMBER_OF field, then it should not be |
| 7759 | // marked as MEMBER_OF. |
| 7760 | if ((Flags & OMP_MAP_PTR_AND_OBJ) && |
| 7761 | ((Flags & OMP_MAP_MEMBER_OF) != OMP_MAP_MEMBER_OF)) |
| 7762 | return; |
| 7763 | |
| 7764 | // Reset the placeholder value to prepare the flag for the assignment of the |
| 7765 | // proper MEMBER_OF value. |
| 7766 | Flags &= ~OMP_MAP_MEMBER_OF; |
| 7767 | Flags |= MemberOfFlag; |
| 7768 | } |
| 7769 | |
Alexey Bataev | e82445f | 2018-09-20 13:54:02 +0000 | [diff] [blame] | 7770 | void getPlainLayout(const CXXRecordDecl *RD, |
| 7771 | llvm::SmallVectorImpl<const FieldDecl *> &Layout, |
| 7772 | bool AsBase) const { |
| 7773 | const CGRecordLayout &RL = CGF.getTypes().getCGRecordLayout(RD); |
| 7774 | |
| 7775 | llvm::StructType *St = |
| 7776 | AsBase ? RL.getBaseSubobjectLLVMType() : RL.getLLVMType(); |
| 7777 | |
| 7778 | unsigned NumElements = St->getNumElements(); |
| 7779 | llvm::SmallVector< |
| 7780 | llvm::PointerUnion<const CXXRecordDecl *, const FieldDecl *>, 4> |
| 7781 | RecordLayout(NumElements); |
| 7782 | |
| 7783 | // Fill bases. |
| 7784 | for (const auto &I : RD->bases()) { |
| 7785 | if (I.isVirtual()) |
| 7786 | continue; |
| 7787 | const auto *Base = I.getType()->getAsCXXRecordDecl(); |
| 7788 | // Ignore empty bases. |
| 7789 | if (Base->isEmpty() || CGF.getContext() |
| 7790 | .getASTRecordLayout(Base) |
| 7791 | .getNonVirtualSize() |
| 7792 | .isZero()) |
| 7793 | continue; |
| 7794 | |
| 7795 | unsigned FieldIndex = RL.getNonVirtualBaseLLVMFieldNo(Base); |
| 7796 | RecordLayout[FieldIndex] = Base; |
| 7797 | } |
| 7798 | // Fill in virtual bases. |
| 7799 | for (const auto &I : RD->vbases()) { |
| 7800 | const auto *Base = I.getType()->getAsCXXRecordDecl(); |
| 7801 | // Ignore empty bases. |
| 7802 | if (Base->isEmpty()) |
| 7803 | continue; |
| 7804 | unsigned FieldIndex = RL.getVirtualBaseIndex(Base); |
| 7805 | if (RecordLayout[FieldIndex]) |
| 7806 | continue; |
| 7807 | RecordLayout[FieldIndex] = Base; |
| 7808 | } |
| 7809 | // Fill in all the fields. |
| 7810 | assert(!RD->isUnion() && "Unexpected union."); |
| 7811 | for (const auto *Field : RD->fields()) { |
| 7812 | // Fill in non-bitfields. (Bitfields always use a zero pattern, which we |
| 7813 | // will fill in later.) |
Richard Smith | 78b239e | 2019-06-20 20:44:45 +0000 | [diff] [blame^] | 7814 | if (!Field->isBitField() && !Field->isZeroSize(CGF.getContext())) { |
Alexey Bataev | e82445f | 2018-09-20 13:54:02 +0000 | [diff] [blame] | 7815 | unsigned FieldIndex = RL.getLLVMFieldNo(Field); |
| 7816 | RecordLayout[FieldIndex] = Field; |
| 7817 | } |
| 7818 | } |
| 7819 | for (const llvm::PointerUnion<const CXXRecordDecl *, const FieldDecl *> |
| 7820 | &Data : RecordLayout) { |
| 7821 | if (Data.isNull()) |
| 7822 | continue; |
| 7823 | if (const auto *Base = Data.dyn_cast<const CXXRecordDecl *>()) |
| 7824 | getPlainLayout(Base, Layout, /*AsBase=*/true); |
| 7825 | else |
| 7826 | Layout.push_back(Data.get<const FieldDecl *>()); |
| 7827 | } |
| 7828 | } |
| 7829 | |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7830 | public: |
| 7831 | MappableExprsHandler(const OMPExecutableDirective &Dir, CodeGenFunction &CGF) |
| 7832 | : CurDir(Dir), CGF(CGF) { |
| 7833 | // Extract firstprivate clause information. |
| 7834 | for (const auto *C : Dir.getClausesOfKind<OMPFirstprivateClause>()) |
| 7835 | for (const auto *D : C->varlists()) |
| 7836 | FirstPrivateDecls.insert( |
| 7837 | cast<VarDecl>(cast<DeclRefExpr>(D)->getDecl())->getCanonicalDecl()); |
| 7838 | // Extract device pointer clause information. |
| 7839 | for (const auto *C : Dir.getClausesOfKind<OMPIsDevicePtrClause>()) |
| 7840 | for (auto L : C->component_lists()) |
| 7841 | DevPointersMap[L.first].push_back(L.second); |
| 7842 | } |
| 7843 | |
| 7844 | /// Generate code for the combined entry if we have a partially mapped struct |
| 7845 | /// and take care of the mapping flags of the arguments corresponding to |
| 7846 | /// individual struct members. |
| 7847 | void emitCombinedEntry(MapBaseValuesArrayTy &BasePointers, |
| 7848 | MapValuesArrayTy &Pointers, MapValuesArrayTy &Sizes, |
| 7849 | MapFlagsArrayTy &Types, MapFlagsArrayTy &CurTypes, |
| 7850 | const StructRangeInfoTy &PartialStruct) const { |
| 7851 | // Base is the base of the struct |
| 7852 | BasePointers.push_back(PartialStruct.Base.getPointer()); |
| 7853 | // Pointer is the address of the lowest element |
| 7854 | llvm::Value *LB = PartialStruct.LowestElem.second.getPointer(); |
| 7855 | Pointers.push_back(LB); |
| 7856 | // Size is (addr of {highest+1} element) - (addr of lowest element) |
| 7857 | llvm::Value *HB = PartialStruct.HighestElem.second.getPointer(); |
| 7858 | llvm::Value *HAddr = CGF.Builder.CreateConstGEP1_32(HB, /*Idx0=*/1); |
| 7859 | llvm::Value *CLAddr = CGF.Builder.CreatePointerCast(LB, CGF.VoidPtrTy); |
| 7860 | llvm::Value *CHAddr = CGF.Builder.CreatePointerCast(HAddr, CGF.VoidPtrTy); |
| 7861 | llvm::Value *Diff = CGF.Builder.CreatePtrDiff(CHAddr, CLAddr); |
| 7862 | llvm::Value *Size = CGF.Builder.CreateIntCast(Diff, CGF.SizeTy, |
| 7863 | /*isSinged=*/false); |
| 7864 | Sizes.push_back(Size); |
| 7865 | // Map type is always TARGET_PARAM |
| 7866 | Types.push_back(OMP_MAP_TARGET_PARAM); |
| 7867 | // Remove TARGET_PARAM flag from the first element |
| 7868 | (*CurTypes.begin()) &= ~OMP_MAP_TARGET_PARAM; |
| 7869 | |
| 7870 | // All other current entries will be MEMBER_OF the combined entry |
| 7871 | // (except for PTR_AND_OBJ entries which do not have a placeholder value |
| 7872 | // 0xFFFF in the MEMBER_OF field). |
| 7873 | OpenMPOffloadMappingFlags MemberOfFlag = |
| 7874 | getMemberOfFlag(BasePointers.size() - 1); |
| 7875 | for (auto &M : CurTypes) |
| 7876 | setCorrectMemberOfFlag(M, MemberOfFlag); |
| 7877 | } |
| 7878 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 7879 | /// Generate all the base pointers, section pointers, sizes and map |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7880 | /// types for the extracted mappable expressions. Also, for each item that |
| 7881 | /// relates with a device pointer, a pair of the relevant declaration and |
| 7882 | /// index where it occurs is appended to the device pointers info array. |
| 7883 | void generateAllInfo(MapBaseValuesArrayTy &BasePointers, |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7884 | MapValuesArrayTy &Pointers, MapValuesArrayTy &Sizes, |
| 7885 | MapFlagsArrayTy &Types) const { |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7886 | // We have to process the component lists that relate with the same |
| 7887 | // declaration in a single chunk so that we can generate the map flags |
| 7888 | // correctly. Therefore, we organize all lists in a map. |
Alexey Bataev | 5d1c3f6 | 2017-06-27 15:46:42 +0000 | [diff] [blame] | 7889 | llvm::MapVector<const ValueDecl *, SmallVector<MapInfo, 8>> Info; |
Samuel Antao | 8d2d730 | 2016-05-26 18:30:22 +0000 | [diff] [blame] | 7890 | |
| 7891 | // Helper function to fill the information map for the different supported |
| 7892 | // clauses. |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7893 | auto &&InfoGen = [&Info]( |
| 7894 | const ValueDecl *D, |
| 7895 | OMPClauseMappableExprCommon::MappableExprComponentListRef L, |
Kelvin Li | ef57943 | 2018-12-18 22:18:41 +0000 | [diff] [blame] | 7896 | OpenMPMapClauseKind MapType, |
| 7897 | ArrayRef<OpenMPMapModifierKind> MapModifiers, |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7898 | bool ReturnDevicePointer, bool IsImplicit) { |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7899 | const ValueDecl *VD = |
| 7900 | D ? cast<ValueDecl>(D->getCanonicalDecl()) : nullptr; |
Kelvin Li | ef57943 | 2018-12-18 22:18:41 +0000 | [diff] [blame] | 7901 | Info[VD].emplace_back(L, MapType, MapModifiers, ReturnDevicePointer, |
Alexey Bataev | f47c4b4 | 2017-09-26 13:47:31 +0000 | [diff] [blame] | 7902 | IsImplicit); |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7903 | }; |
Samuel Antao | 8d2d730 | 2016-05-26 18:30:22 +0000 | [diff] [blame] | 7904 | |
Paul Robinson | 78fb132 | 2016-08-01 22:12:46 +0000 | [diff] [blame] | 7905 | // FIXME: MSVC 2013 seems to require this-> to find member CurDir. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 7906 | for (const auto *C : this->CurDir.getClausesOfKind<OMPMapClause>()) |
| 7907 | for (const auto &L : C->component_lists()) { |
Kelvin Li | ef57943 | 2018-12-18 22:18:41 +0000 | [diff] [blame] | 7908 | InfoGen(L.first, L.second, C->getMapType(), C->getMapTypeModifiers(), |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7909 | /*ReturnDevicePointer=*/false, C->isImplicit()); |
Alexey Bataev | f47c4b4 | 2017-09-26 13:47:31 +0000 | [diff] [blame] | 7910 | } |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 7911 | for (const auto *C : this->CurDir.getClausesOfKind<OMPToClause>()) |
| 7912 | for (const auto &L : C->component_lists()) { |
Kelvin Li | ef57943 | 2018-12-18 22:18:41 +0000 | [diff] [blame] | 7913 | InfoGen(L.first, L.second, OMPC_MAP_to, llvm::None, |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7914 | /*ReturnDevicePointer=*/false, C->isImplicit()); |
Alexey Bataev | f47c4b4 | 2017-09-26 13:47:31 +0000 | [diff] [blame] | 7915 | } |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 7916 | for (const auto *C : this->CurDir.getClausesOfKind<OMPFromClause>()) |
| 7917 | for (const auto &L : C->component_lists()) { |
Kelvin Li | ef57943 | 2018-12-18 22:18:41 +0000 | [diff] [blame] | 7918 | InfoGen(L.first, L.second, OMPC_MAP_from, llvm::None, |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7919 | /*ReturnDevicePointer=*/false, C->isImplicit()); |
Alexey Bataev | f47c4b4 | 2017-09-26 13:47:31 +0000 | [diff] [blame] | 7920 | } |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7921 | |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7922 | // Look at the use_device_ptr clause information and mark the existing map |
| 7923 | // entries as such. If there is no map information for an entry in the |
| 7924 | // use_device_ptr list, we create one with map type 'alloc' and zero size |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7925 | // section. It is the user fault if that was not mapped before. If there is |
| 7926 | // no map information and the pointer is a struct member, then we defer the |
| 7927 | // emission of that entry until the whole struct has been processed. |
| 7928 | llvm::MapVector<const ValueDecl *, SmallVector<DeferredDevicePtrEntryTy, 4>> |
| 7929 | DeferredInfo; |
| 7930 | |
Paul Robinson | 78fb132 | 2016-08-01 22:12:46 +0000 | [diff] [blame] | 7931 | // FIXME: MSVC 2013 seems to require this-> to find member CurDir. |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7932 | for (const auto *C : |
| 7933 | this->CurDir.getClausesOfKind<OMPUseDevicePtrClause>()) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 7934 | for (const auto &L : C->component_lists()) { |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7935 | assert(!L.second.empty() && "Not expecting empty list of components!"); |
| 7936 | const ValueDecl *VD = L.second.back().getAssociatedDeclaration(); |
| 7937 | VD = cast<ValueDecl>(VD->getCanonicalDecl()); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 7938 | const Expr *IE = L.second.back().getAssociatedExpression(); |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7939 | // If the first component is a member expression, we have to look into |
| 7940 | // 'this', which maps to null in the map of map information. Otherwise |
| 7941 | // look directly for the information. |
| 7942 | auto It = Info.find(isa<MemberExpr>(IE) ? nullptr : VD); |
| 7943 | |
| 7944 | // We potentially have map information for this declaration already. |
| 7945 | // Look for the first set of components that refer to it. |
| 7946 | if (It != Info.end()) { |
| 7947 | auto CI = std::find_if( |
| 7948 | It->second.begin(), It->second.end(), [VD](const MapInfo &MI) { |
| 7949 | return MI.Components.back().getAssociatedDeclaration() == VD; |
| 7950 | }); |
| 7951 | // If we found a map entry, signal that the pointer has to be returned |
| 7952 | // and move on to the next declaration. |
| 7953 | if (CI != It->second.end()) { |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7954 | CI->ReturnDevicePointer = true; |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7955 | continue; |
| 7956 | } |
| 7957 | } |
| 7958 | |
| 7959 | // We didn't find any match in our map information - generate a zero |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7960 | // size array section - if the pointer is a struct member we defer this |
| 7961 | // action until the whole struct has been processed. |
Paul Robinson | 78fb132 | 2016-08-01 22:12:46 +0000 | [diff] [blame] | 7962 | // FIXME: MSVC 2013 seems to require this-> to find member CGF. |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7963 | if (isa<MemberExpr>(IE)) { |
| 7964 | // Insert the pointer into Info to be processed by |
| 7965 | // generateInfoForComponentList. Because it is a member pointer |
| 7966 | // without a pointee, no entry will be generated for it, therefore |
| 7967 | // we need to generate one after the whole struct has been processed. |
| 7968 | // Nonetheless, generateInfoForComponentList must be called to take |
| 7969 | // the pointer into account for the calculation of the range of the |
| 7970 | // partial struct. |
Kelvin Li | ef57943 | 2018-12-18 22:18:41 +0000 | [diff] [blame] | 7971 | InfoGen(nullptr, L.second, OMPC_MAP_unknown, llvm::None, |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7972 | /*ReturnDevicePointer=*/false, C->isImplicit()); |
| 7973 | DeferredInfo[nullptr].emplace_back(IE, VD); |
| 7974 | } else { |
| 7975 | llvm::Value *Ptr = this->CGF.EmitLoadOfScalar( |
| 7976 | this->CGF.EmitLValue(IE), IE->getExprLoc()); |
| 7977 | BasePointers.emplace_back(Ptr, VD); |
| 7978 | Pointers.push_back(Ptr); |
| 7979 | Sizes.push_back(llvm::Constant::getNullValue(this->CGF.SizeTy)); |
| 7980 | Types.push_back(OMP_MAP_RETURN_PARAM | OMP_MAP_TARGET_PARAM); |
| 7981 | } |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7982 | } |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7983 | } |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 7984 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 7985 | for (const auto &M : Info) { |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7986 | // We need to know when we generate information for the first component |
| 7987 | // associated with a capture, because the mapping flags depend on it. |
| 7988 | bool IsFirstComponentList = true; |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 7989 | |
| 7990 | // Temporary versions of arrays |
| 7991 | MapBaseValuesArrayTy CurBasePointers; |
| 7992 | MapValuesArrayTy CurPointers; |
| 7993 | MapValuesArrayTy CurSizes; |
| 7994 | MapFlagsArrayTy CurTypes; |
| 7995 | StructRangeInfoTy PartialStruct; |
| 7996 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 7997 | for (const MapInfo &L : M.second) { |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 7998 | assert(!L.Components.empty() && |
| 7999 | "Not expecting declaration with no component lists."); |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 8000 | |
| 8001 | // Remember the current base pointer index. |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 8002 | unsigned CurrentBasePointersIdx = CurBasePointers.size(); |
Paul Robinson | 78fb132 | 2016-08-01 22:12:46 +0000 | [diff] [blame] | 8003 | // FIXME: MSVC 2013 seems to require this-> to find the member method. |
Alexey Bataev | f47c4b4 | 2017-09-26 13:47:31 +0000 | [diff] [blame] | 8004 | this->generateInfoForComponentList( |
Kelvin Li | ef57943 | 2018-12-18 22:18:41 +0000 | [diff] [blame] | 8005 | L.MapType, L.MapModifiers, L.Components, CurBasePointers, |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 8006 | CurPointers, CurSizes, CurTypes, PartialStruct, |
| 8007 | IsFirstComponentList, L.IsImplicit); |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 8008 | |
| 8009 | // If this entry relates with a device pointer, set the relevant |
| 8010 | // declaration and add the 'return pointer' flag. |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 8011 | if (L.ReturnDevicePointer) { |
| 8012 | assert(CurBasePointers.size() > CurrentBasePointersIdx && |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 8013 | "Unexpected number of mapped base pointers."); |
| 8014 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8015 | const ValueDecl *RelevantVD = |
| 8016 | L.Components.back().getAssociatedDeclaration(); |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 8017 | assert(RelevantVD && |
| 8018 | "No relevant declaration related with device pointer??"); |
| 8019 | |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 8020 | CurBasePointers[CurrentBasePointersIdx].setDevicePtrDecl(RelevantVD); |
| 8021 | CurTypes[CurrentBasePointersIdx] |= OMP_MAP_RETURN_PARAM; |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 8022 | } |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 8023 | IsFirstComponentList = false; |
| 8024 | } |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 8025 | |
| 8026 | // Append any pending zero-length pointers which are struct members and |
| 8027 | // used with use_device_ptr. |
| 8028 | auto CI = DeferredInfo.find(M.first); |
| 8029 | if (CI != DeferredInfo.end()) { |
| 8030 | for (const DeferredDevicePtrEntryTy &L : CI->second) { |
| 8031 | llvm::Value *BasePtr = this->CGF.EmitLValue(L.IE).getPointer(); |
| 8032 | llvm::Value *Ptr = this->CGF.EmitLoadOfScalar( |
| 8033 | this->CGF.EmitLValue(L.IE), L.IE->getExprLoc()); |
| 8034 | CurBasePointers.emplace_back(BasePtr, L.VD); |
| 8035 | CurPointers.push_back(Ptr); |
| 8036 | CurSizes.push_back(llvm::Constant::getNullValue(this->CGF.SizeTy)); |
| 8037 | // Entry is PTR_AND_OBJ and RETURN_PARAM. Also, set the placeholder |
| 8038 | // value MEMBER_OF=FFFF so that the entry is later updated with the |
| 8039 | // correct value of MEMBER_OF. |
| 8040 | CurTypes.push_back(OMP_MAP_PTR_AND_OBJ | OMP_MAP_RETURN_PARAM | |
| 8041 | OMP_MAP_MEMBER_OF); |
| 8042 | } |
| 8043 | } |
| 8044 | |
| 8045 | // If there is an entry in PartialStruct it means we have a struct with |
| 8046 | // individual members mapped. Emit an extra combined entry. |
| 8047 | if (PartialStruct.Base.isValid()) |
| 8048 | emitCombinedEntry(BasePointers, Pointers, Sizes, Types, CurTypes, |
| 8049 | PartialStruct); |
| 8050 | |
| 8051 | // We need to append the results of this capture to what we already have. |
| 8052 | BasePointers.append(CurBasePointers.begin(), CurBasePointers.end()); |
| 8053 | Pointers.append(CurPointers.begin(), CurPointers.end()); |
| 8054 | Sizes.append(CurSizes.begin(), CurSizes.end()); |
| 8055 | Types.append(CurTypes.begin(), CurTypes.end()); |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 8056 | } |
| 8057 | } |
| 8058 | |
Alexey Bataev | 6070542 | 2018-10-30 15:50:12 +0000 | [diff] [blame] | 8059 | /// Emit capture info for lambdas for variables captured by reference. |
Alexey Bataev | 969dbc0 | 2018-11-08 15:47:39 +0000 | [diff] [blame] | 8060 | void generateInfoForLambdaCaptures( |
| 8061 | const ValueDecl *VD, llvm::Value *Arg, MapBaseValuesArrayTy &BasePointers, |
| 8062 | MapValuesArrayTy &Pointers, MapValuesArrayTy &Sizes, |
| 8063 | MapFlagsArrayTy &Types, |
| 8064 | llvm::DenseMap<llvm::Value *, llvm::Value *> &LambdaPointers) const { |
Alexey Bataev | 6070542 | 2018-10-30 15:50:12 +0000 | [diff] [blame] | 8065 | const auto *RD = VD->getType() |
| 8066 | .getCanonicalType() |
| 8067 | .getNonReferenceType() |
| 8068 | ->getAsCXXRecordDecl(); |
| 8069 | if (!RD || !RD->isLambda()) |
| 8070 | return; |
| 8071 | Address VDAddr = Address(Arg, CGF.getContext().getDeclAlign(VD)); |
| 8072 | LValue VDLVal = CGF.MakeAddrLValue( |
| 8073 | VDAddr, VD->getType().getCanonicalType().getNonReferenceType()); |
| 8074 | llvm::DenseMap<const VarDecl *, FieldDecl *> Captures; |
| 8075 | FieldDecl *ThisCapture = nullptr; |
| 8076 | RD->getCaptureFields(Captures, ThisCapture); |
| 8077 | if (ThisCapture) { |
| 8078 | LValue ThisLVal = |
| 8079 | CGF.EmitLValueForFieldInitialization(VDLVal, ThisCapture); |
Alexey Bataev | 969dbc0 | 2018-11-08 15:47:39 +0000 | [diff] [blame] | 8080 | LValue ThisLValVal = CGF.EmitLValueForField(VDLVal, ThisCapture); |
| 8081 | LambdaPointers.try_emplace(ThisLVal.getPointer(), VDLVal.getPointer()); |
| 8082 | BasePointers.push_back(ThisLVal.getPointer()); |
| 8083 | Pointers.push_back(ThisLValVal.getPointer()); |
Alexey Bataev | 6070542 | 2018-10-30 15:50:12 +0000 | [diff] [blame] | 8084 | Sizes.push_back(CGF.getTypeSize(CGF.getContext().VoidPtrTy)); |
Alexey Bataev | 2dc07d0 | 2018-11-02 15:25:06 +0000 | [diff] [blame] | 8085 | Types.push_back(OMP_MAP_PTR_AND_OBJ | OMP_MAP_LITERAL | |
Alexey Bataev | 6070542 | 2018-10-30 15:50:12 +0000 | [diff] [blame] | 8086 | OMP_MAP_MEMBER_OF | OMP_MAP_IMPLICIT); |
| 8087 | } |
| 8088 | for (const LambdaCapture &LC : RD->captures()) { |
| 8089 | if (LC.getCaptureKind() != LCK_ByRef) |
| 8090 | continue; |
| 8091 | const VarDecl *VD = LC.getCapturedVar(); |
| 8092 | auto It = Captures.find(VD); |
| 8093 | assert(It != Captures.end() && "Found lambda capture without field."); |
| 8094 | LValue VarLVal = CGF.EmitLValueForFieldInitialization(VDLVal, It->second); |
Alexey Bataev | 969dbc0 | 2018-11-08 15:47:39 +0000 | [diff] [blame] | 8095 | LValue VarLValVal = CGF.EmitLValueForField(VDLVal, It->second); |
| 8096 | LambdaPointers.try_emplace(VarLVal.getPointer(), VDLVal.getPointer()); |
| 8097 | BasePointers.push_back(VarLVal.getPointer()); |
| 8098 | Pointers.push_back(VarLValVal.getPointer()); |
Alexey Bataev | 6070542 | 2018-10-30 15:50:12 +0000 | [diff] [blame] | 8099 | Sizes.push_back(CGF.getTypeSize( |
| 8100 | VD->getType().getCanonicalType().getNonReferenceType())); |
Alexey Bataev | 2dc07d0 | 2018-11-02 15:25:06 +0000 | [diff] [blame] | 8101 | Types.push_back(OMP_MAP_PTR_AND_OBJ | OMP_MAP_LITERAL | |
Alexey Bataev | 6070542 | 2018-10-30 15:50:12 +0000 | [diff] [blame] | 8102 | OMP_MAP_MEMBER_OF | OMP_MAP_IMPLICIT); |
| 8103 | } |
| 8104 | } |
| 8105 | |
| 8106 | /// Set correct indices for lambdas captures. |
Alexey Bataev | 969dbc0 | 2018-11-08 15:47:39 +0000 | [diff] [blame] | 8107 | void adjustMemberOfForLambdaCaptures( |
| 8108 | const llvm::DenseMap<llvm::Value *, llvm::Value *> &LambdaPointers, |
| 8109 | MapBaseValuesArrayTy &BasePointers, MapValuesArrayTy &Pointers, |
| 8110 | MapFlagsArrayTy &Types) const { |
Alexey Bataev | 6070542 | 2018-10-30 15:50:12 +0000 | [diff] [blame] | 8111 | for (unsigned I = 0, E = Types.size(); I < E; ++I) { |
| 8112 | // Set correct member_of idx for all implicit lambda captures. |
Alexey Bataev | 2dc07d0 | 2018-11-02 15:25:06 +0000 | [diff] [blame] | 8113 | if (Types[I] != (OMP_MAP_PTR_AND_OBJ | OMP_MAP_LITERAL | |
Alexey Bataev | 6070542 | 2018-10-30 15:50:12 +0000 | [diff] [blame] | 8114 | OMP_MAP_MEMBER_OF | OMP_MAP_IMPLICIT)) |
| 8115 | continue; |
Alexey Bataev | 969dbc0 | 2018-11-08 15:47:39 +0000 | [diff] [blame] | 8116 | llvm::Value *BasePtr = LambdaPointers.lookup(*BasePointers[I]); |
| 8117 | assert(BasePtr && "Unable to find base lambda address."); |
Alexey Bataev | 6070542 | 2018-10-30 15:50:12 +0000 | [diff] [blame] | 8118 | int TgtIdx = -1; |
| 8119 | for (unsigned J = I; J > 0; --J) { |
| 8120 | unsigned Idx = J - 1; |
| 8121 | if (Pointers[Idx] != BasePtr) |
| 8122 | continue; |
| 8123 | TgtIdx = Idx; |
| 8124 | break; |
| 8125 | } |
| 8126 | assert(TgtIdx != -1 && "Unable to find parent lambda."); |
| 8127 | // All other current entries will be MEMBER_OF the combined entry |
| 8128 | // (except for PTR_AND_OBJ entries which do not have a placeholder value |
| 8129 | // 0xFFFF in the MEMBER_OF field). |
| 8130 | OpenMPOffloadMappingFlags MemberOfFlag = getMemberOfFlag(TgtIdx); |
| 8131 | setCorrectMemberOfFlag(Types[I], MemberOfFlag); |
| 8132 | } |
| 8133 | } |
| 8134 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 8135 | /// Generate the base pointers, section pointers, sizes and map types |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 8136 | /// associated to a given capture. |
| 8137 | void generateInfoForCapture(const CapturedStmt::Capture *Cap, |
Samuel Antao | 6890b09 | 2016-07-28 14:25:09 +0000 | [diff] [blame] | 8138 | llvm::Value *Arg, |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 8139 | MapBaseValuesArrayTy &BasePointers, |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 8140 | MapValuesArrayTy &Pointers, |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 8141 | MapValuesArrayTy &Sizes, MapFlagsArrayTy &Types, |
| 8142 | StructRangeInfoTy &PartialStruct) const { |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 8143 | assert(!Cap->capturesVariableArrayType() && |
| 8144 | "Not expecting to generate map info for a variable array type!"); |
| 8145 | |
Samuel Antao | 6890b09 | 2016-07-28 14:25:09 +0000 | [diff] [blame] | 8146 | // We need to know when we generating information for the first component |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 8147 | const ValueDecl *VD = Cap->capturesThis() |
| 8148 | ? nullptr |
| 8149 | : Cap->getCapturedVar()->getCanonicalDecl(); |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 8150 | |
Samuel Antao | 6890b09 | 2016-07-28 14:25:09 +0000 | [diff] [blame] | 8151 | // If this declaration appears in a is_device_ptr clause we just have to |
| 8152 | // pass the pointer by value. If it is a reference to a declaration, we just |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 8153 | // pass its value. |
| 8154 | if (DevPointersMap.count(VD)) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8155 | BasePointers.emplace_back(Arg, VD); |
Samuel Antao | 6890b09 | 2016-07-28 14:25:09 +0000 | [diff] [blame] | 8156 | Pointers.push_back(Arg); |
| 8157 | Sizes.push_back(CGF.getTypeSize(CGF.getContext().VoidPtrTy)); |
George Rokos | 065755d | 2017-11-07 18:27:04 +0000 | [diff] [blame] | 8158 | Types.push_back(OMP_MAP_LITERAL | OMP_MAP_TARGET_PARAM); |
Samuel Antao | 6890b09 | 2016-07-28 14:25:09 +0000 | [diff] [blame] | 8159 | return; |
| 8160 | } |
| 8161 | |
Alexey Bataev | e82445f | 2018-09-20 13:54:02 +0000 | [diff] [blame] | 8162 | using MapData = |
| 8163 | std::tuple<OMPClauseMappableExprCommon::MappableExprComponentListRef, |
Kelvin Li | ef57943 | 2018-12-18 22:18:41 +0000 | [diff] [blame] | 8164 | OpenMPMapClauseKind, ArrayRef<OpenMPMapModifierKind>, bool>; |
Alexey Bataev | e82445f | 2018-09-20 13:54:02 +0000 | [diff] [blame] | 8165 | SmallVector<MapData, 4> DeclComponentLists; |
Paul Robinson | 78fb132 | 2016-08-01 22:12:46 +0000 | [diff] [blame] | 8166 | // FIXME: MSVC 2013 seems to require this-> to find member CurDir. |
Alexey Bataev | e82445f | 2018-09-20 13:54:02 +0000 | [diff] [blame] | 8167 | for (const auto *C : this->CurDir.getClausesOfKind<OMPMapClause>()) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8168 | for (const auto &L : C->decl_component_lists(VD)) { |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 8169 | assert(L.first == VD && |
| 8170 | "We got information for the wrong declaration??"); |
| 8171 | assert(!L.second.empty() && |
| 8172 | "Not expecting declaration with no component lists."); |
Alexey Bataev | e82445f | 2018-09-20 13:54:02 +0000 | [diff] [blame] | 8173 | DeclComponentLists.emplace_back(L.second, C->getMapType(), |
Kelvin Li | ef57943 | 2018-12-18 22:18:41 +0000 | [diff] [blame] | 8174 | C->getMapTypeModifiers(), |
Alexey Bataev | e82445f | 2018-09-20 13:54:02 +0000 | [diff] [blame] | 8175 | C->isImplicit()); |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 8176 | } |
Alexey Bataev | e82445f | 2018-09-20 13:54:02 +0000 | [diff] [blame] | 8177 | } |
| 8178 | |
| 8179 | // Find overlapping elements (including the offset from the base element). |
| 8180 | llvm::SmallDenseMap< |
| 8181 | const MapData *, |
| 8182 | llvm::SmallVector< |
| 8183 | OMPClauseMappableExprCommon::MappableExprComponentListRef, 4>, |
| 8184 | 4> |
| 8185 | OverlappedData; |
| 8186 | size_t Count = 0; |
| 8187 | for (const MapData &L : DeclComponentLists) { |
| 8188 | OMPClauseMappableExprCommon::MappableExprComponentListRef Components; |
| 8189 | OpenMPMapClauseKind MapType; |
Kelvin Li | ef57943 | 2018-12-18 22:18:41 +0000 | [diff] [blame] | 8190 | ArrayRef<OpenMPMapModifierKind> MapModifiers; |
Alexey Bataev | e82445f | 2018-09-20 13:54:02 +0000 | [diff] [blame] | 8191 | bool IsImplicit; |
Kelvin Li | ef57943 | 2018-12-18 22:18:41 +0000 | [diff] [blame] | 8192 | std::tie(Components, MapType, MapModifiers, IsImplicit) = L; |
Alexey Bataev | e82445f | 2018-09-20 13:54:02 +0000 | [diff] [blame] | 8193 | ++Count; |
| 8194 | for (const MapData &L1 : makeArrayRef(DeclComponentLists).slice(Count)) { |
| 8195 | OMPClauseMappableExprCommon::MappableExprComponentListRef Components1; |
Kelvin Li | ef57943 | 2018-12-18 22:18:41 +0000 | [diff] [blame] | 8196 | std::tie(Components1, MapType, MapModifiers, IsImplicit) = L1; |
Alexey Bataev | e82445f | 2018-09-20 13:54:02 +0000 | [diff] [blame] | 8197 | auto CI = Components.rbegin(); |
| 8198 | auto CE = Components.rend(); |
| 8199 | auto SI = Components1.rbegin(); |
| 8200 | auto SE = Components1.rend(); |
| 8201 | for (; CI != CE && SI != SE; ++CI, ++SI) { |
| 8202 | if (CI->getAssociatedExpression()->getStmtClass() != |
| 8203 | SI->getAssociatedExpression()->getStmtClass()) |
| 8204 | break; |
| 8205 | // Are we dealing with different variables/fields? |
| 8206 | if (CI->getAssociatedDeclaration() != SI->getAssociatedDeclaration()) |
| 8207 | break; |
| 8208 | } |
| 8209 | // Found overlapping if, at least for one component, reached the head of |
| 8210 | // the components list. |
| 8211 | if (CI == CE || SI == SE) { |
| 8212 | assert((CI != CE || SI != SE) && |
| 8213 | "Unexpected full match of the mapping components."); |
| 8214 | const MapData &BaseData = CI == CE ? L : L1; |
| 8215 | OMPClauseMappableExprCommon::MappableExprComponentListRef SubData = |
| 8216 | SI == SE ? Components : Components1; |
Alexey Bataev | e82445f | 2018-09-20 13:54:02 +0000 | [diff] [blame] | 8217 | auto &OverlappedElements = OverlappedData.FindAndConstruct(&BaseData); |
| 8218 | OverlappedElements.getSecond().push_back(SubData); |
| 8219 | } |
| 8220 | } |
| 8221 | } |
| 8222 | // Sort the overlapped elements for each item. |
| 8223 | llvm::SmallVector<const FieldDecl *, 4> Layout; |
| 8224 | if (!OverlappedData.empty()) { |
| 8225 | if (const auto *CRD = |
| 8226 | VD->getType().getCanonicalType()->getAsCXXRecordDecl()) |
| 8227 | getPlainLayout(CRD, Layout, /*AsBase=*/false); |
| 8228 | else { |
| 8229 | const auto *RD = VD->getType().getCanonicalType()->getAsRecordDecl(); |
| 8230 | Layout.append(RD->field_begin(), RD->field_end()); |
| 8231 | } |
| 8232 | } |
| 8233 | for (auto &Pair : OverlappedData) { |
| 8234 | llvm::sort( |
| 8235 | Pair.getSecond(), |
| 8236 | [&Layout]( |
| 8237 | OMPClauseMappableExprCommon::MappableExprComponentListRef First, |
| 8238 | OMPClauseMappableExprCommon::MappableExprComponentListRef |
| 8239 | Second) { |
| 8240 | auto CI = First.rbegin(); |
| 8241 | auto CE = First.rend(); |
| 8242 | auto SI = Second.rbegin(); |
| 8243 | auto SE = Second.rend(); |
| 8244 | for (; CI != CE && SI != SE; ++CI, ++SI) { |
| 8245 | if (CI->getAssociatedExpression()->getStmtClass() != |
| 8246 | SI->getAssociatedExpression()->getStmtClass()) |
| 8247 | break; |
| 8248 | // Are we dealing with different variables/fields? |
| 8249 | if (CI->getAssociatedDeclaration() != |
| 8250 | SI->getAssociatedDeclaration()) |
| 8251 | break; |
| 8252 | } |
Richard Trieu | 5061e83 | 2018-09-21 21:20:33 +0000 | [diff] [blame] | 8253 | |
| 8254 | // Lists contain the same elements. |
| 8255 | if (CI == CE && SI == SE) |
| 8256 | return false; |
| 8257 | |
| 8258 | // List with less elements is less than list with more elements. |
| 8259 | if (CI == CE || SI == SE) |
| 8260 | return CI == CE; |
| 8261 | |
Alexey Bataev | e82445f | 2018-09-20 13:54:02 +0000 | [diff] [blame] | 8262 | const auto *FD1 = cast<FieldDecl>(CI->getAssociatedDeclaration()); |
| 8263 | const auto *FD2 = cast<FieldDecl>(SI->getAssociatedDeclaration()); |
| 8264 | if (FD1->getParent() == FD2->getParent()) |
| 8265 | return FD1->getFieldIndex() < FD2->getFieldIndex(); |
| 8266 | const auto It = |
| 8267 | llvm::find_if(Layout, [FD1, FD2](const FieldDecl *FD) { |
| 8268 | return FD == FD1 || FD == FD2; |
| 8269 | }); |
| 8270 | return *It == FD1; |
| 8271 | }); |
| 8272 | } |
| 8273 | |
| 8274 | // Associated with a capture, because the mapping flags depend on it. |
| 8275 | // Go through all of the elements with the overlapped elements. |
| 8276 | for (const auto &Pair : OverlappedData) { |
| 8277 | const MapData &L = *Pair.getFirst(); |
| 8278 | OMPClauseMappableExprCommon::MappableExprComponentListRef Components; |
| 8279 | OpenMPMapClauseKind MapType; |
Kelvin Li | ef57943 | 2018-12-18 22:18:41 +0000 | [diff] [blame] | 8280 | ArrayRef<OpenMPMapModifierKind> MapModifiers; |
Alexey Bataev | e82445f | 2018-09-20 13:54:02 +0000 | [diff] [blame] | 8281 | bool IsImplicit; |
Kelvin Li | ef57943 | 2018-12-18 22:18:41 +0000 | [diff] [blame] | 8282 | std::tie(Components, MapType, MapModifiers, IsImplicit) = L; |
Alexey Bataev | e82445f | 2018-09-20 13:54:02 +0000 | [diff] [blame] | 8283 | ArrayRef<OMPClauseMappableExprCommon::MappableExprComponentListRef> |
| 8284 | OverlappedComponents = Pair.getSecond(); |
| 8285 | bool IsFirstComponentList = true; |
Kelvin Li | ef57943 | 2018-12-18 22:18:41 +0000 | [diff] [blame] | 8286 | generateInfoForComponentList(MapType, MapModifiers, Components, |
Alexey Bataev | e82445f | 2018-09-20 13:54:02 +0000 | [diff] [blame] | 8287 | BasePointers, Pointers, Sizes, Types, |
| 8288 | PartialStruct, IsFirstComponentList, |
| 8289 | IsImplicit, OverlappedComponents); |
| 8290 | } |
| 8291 | // Go through other elements without overlapped elements. |
| 8292 | bool IsFirstComponentList = OverlappedData.empty(); |
| 8293 | for (const MapData &L : DeclComponentLists) { |
| 8294 | OMPClauseMappableExprCommon::MappableExprComponentListRef Components; |
| 8295 | OpenMPMapClauseKind MapType; |
Kelvin Li | ef57943 | 2018-12-18 22:18:41 +0000 | [diff] [blame] | 8296 | ArrayRef<OpenMPMapModifierKind> MapModifiers; |
Alexey Bataev | e82445f | 2018-09-20 13:54:02 +0000 | [diff] [blame] | 8297 | bool IsImplicit; |
Kelvin Li | ef57943 | 2018-12-18 22:18:41 +0000 | [diff] [blame] | 8298 | std::tie(Components, MapType, MapModifiers, IsImplicit) = L; |
Alexey Bataev | e82445f | 2018-09-20 13:54:02 +0000 | [diff] [blame] | 8299 | auto It = OverlappedData.find(&L); |
| 8300 | if (It == OverlappedData.end()) |
Kelvin Li | ef57943 | 2018-12-18 22:18:41 +0000 | [diff] [blame] | 8301 | generateInfoForComponentList(MapType, MapModifiers, Components, |
Alexey Bataev | e82445f | 2018-09-20 13:54:02 +0000 | [diff] [blame] | 8302 | BasePointers, Pointers, Sizes, Types, |
| 8303 | PartialStruct, IsFirstComponentList, |
| 8304 | IsImplicit); |
| 8305 | IsFirstComponentList = false; |
| 8306 | } |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 8307 | } |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 8308 | |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 8309 | /// Generate the base pointers, section pointers, sizes and map types |
| 8310 | /// associated with the declare target link variables. |
| 8311 | void generateInfoForDeclareTargetLink(MapBaseValuesArrayTy &BasePointers, |
| 8312 | MapValuesArrayTy &Pointers, |
| 8313 | MapValuesArrayTy &Sizes, |
| 8314 | MapFlagsArrayTy &Types) const { |
| 8315 | // Map other list items in the map clause which are not captured variables |
Gheorghe-Teodor Bercea | 66cdbb47 | 2019-05-21 19:42:01 +0000 | [diff] [blame] | 8316 | // but "declare target link" global variables. |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 8317 | for (const auto *C : this->CurDir.getClausesOfKind<OMPMapClause>()) { |
| 8318 | for (const auto &L : C->component_lists()) { |
| 8319 | if (!L.first) |
| 8320 | continue; |
| 8321 | const auto *VD = dyn_cast<VarDecl>(L.first); |
| 8322 | if (!VD) |
| 8323 | continue; |
| 8324 | llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res = |
Alexey Bataev | 97b7221 | 2018-08-14 18:31:20 +0000 | [diff] [blame] | 8325 | OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD); |
Gheorghe-Teodor Bercea | 5254f0a | 2019-06-14 17:58:26 +0000 | [diff] [blame] | 8326 | if (CGF.CGM.getOpenMPRuntime().hasRequiresUnifiedSharedMemory() || |
| 8327 | !Res || *Res != OMPDeclareTargetDeclAttr::MT_Link) |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 8328 | continue; |
| 8329 | StructRangeInfoTy PartialStruct; |
| 8330 | generateInfoForComponentList( |
Kelvin Li | ef57943 | 2018-12-18 22:18:41 +0000 | [diff] [blame] | 8331 | C->getMapType(), C->getMapTypeModifiers(), L.second, BasePointers, |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 8332 | Pointers, Sizes, Types, PartialStruct, |
| 8333 | /*IsFirstComponentList=*/true, C->isImplicit()); |
| 8334 | assert(!PartialStruct.Base.isValid() && |
| 8335 | "No partial structs for declare target link expected."); |
| 8336 | } |
| 8337 | } |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 8338 | } |
Samuel Antao | d486f84 | 2016-05-26 16:53:38 +0000 | [diff] [blame] | 8339 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 8340 | /// Generate the default map information for a given capture \a CI, |
Samuel Antao | d486f84 | 2016-05-26 16:53:38 +0000 | [diff] [blame] | 8341 | /// record field declaration \a RI and captured value \a CV. |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 8342 | void generateDefaultMapInfo(const CapturedStmt::Capture &CI, |
| 8343 | const FieldDecl &RI, llvm::Value *CV, |
| 8344 | MapBaseValuesArrayTy &CurBasePointers, |
| 8345 | MapValuesArrayTy &CurPointers, |
| 8346 | MapValuesArrayTy &CurSizes, |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 8347 | MapFlagsArrayTy &CurMapTypes) const { |
Samuel Antao | d486f84 | 2016-05-26 16:53:38 +0000 | [diff] [blame] | 8348 | // Do the default mapping. |
| 8349 | if (CI.capturesThis()) { |
| 8350 | CurBasePointers.push_back(CV); |
| 8351 | CurPointers.push_back(CV); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8352 | const auto *PtrTy = cast<PointerType>(RI.getType().getTypePtr()); |
Samuel Antao | d486f84 | 2016-05-26 16:53:38 +0000 | [diff] [blame] | 8353 | CurSizes.push_back(CGF.getTypeSize(PtrTy->getPointeeType())); |
| 8354 | // Default map type. |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 8355 | CurMapTypes.push_back(OMP_MAP_TO | OMP_MAP_FROM); |
Samuel Antao | d486f84 | 2016-05-26 16:53:38 +0000 | [diff] [blame] | 8356 | } else if (CI.capturesVariableByCopy()) { |
Samuel Antao | 6d00426 | 2016-06-16 18:39:34 +0000 | [diff] [blame] | 8357 | CurBasePointers.push_back(CV); |
| 8358 | CurPointers.push_back(CV); |
Samuel Antao | d486f84 | 2016-05-26 16:53:38 +0000 | [diff] [blame] | 8359 | if (!RI.getType()->isAnyPointerType()) { |
Samuel Antao | 6d00426 | 2016-06-16 18:39:34 +0000 | [diff] [blame] | 8360 | // We have to signal to the runtime captures passed by value that are |
| 8361 | // not pointers. |
George Rokos | 065755d | 2017-11-07 18:27:04 +0000 | [diff] [blame] | 8362 | CurMapTypes.push_back(OMP_MAP_LITERAL); |
Samuel Antao | d486f84 | 2016-05-26 16:53:38 +0000 | [diff] [blame] | 8363 | CurSizes.push_back(CGF.getTypeSize(RI.getType())); |
| 8364 | } else { |
| 8365 | // Pointers are implicitly mapped with a zero size and no flags |
| 8366 | // (other than first map that is added for all implicit maps). |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 8367 | CurMapTypes.push_back(OMP_MAP_NONE); |
Samuel Antao | d486f84 | 2016-05-26 16:53:38 +0000 | [diff] [blame] | 8368 | CurSizes.push_back(llvm::Constant::getNullValue(CGF.SizeTy)); |
| 8369 | } |
| 8370 | } else { |
| 8371 | assert(CI.capturesVariable() && "Expected captured reference."); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8372 | const auto *PtrTy = cast<ReferenceType>(RI.getType().getTypePtr()); |
Samuel Antao | d486f84 | 2016-05-26 16:53:38 +0000 | [diff] [blame] | 8373 | QualType ElementType = PtrTy->getPointeeType(); |
| 8374 | CurSizes.push_back(CGF.getTypeSize(ElementType)); |
| 8375 | // The default map type for a scalar/complex type is 'to' because by |
| 8376 | // default the value doesn't have to be retrieved. For an aggregate |
| 8377 | // type, the default is 'tofrom'. |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 8378 | CurMapTypes.push_back(getMapModifiersForPrivateClauses(CI)); |
Alexey Bataev | 1af5bd5 | 2019-03-05 17:47:18 +0000 | [diff] [blame] | 8379 | const VarDecl *VD = CI.getCapturedVar(); |
| 8380 | if (FirstPrivateDecls.count(VD) && |
| 8381 | VD->getType().isConstant(CGF.getContext())) { |
| 8382 | llvm::Constant *Addr = |
| 8383 | CGF.CGM.getOpenMPRuntime().registerTargetFirstprivateCopy(CGF, VD); |
| 8384 | // Copy the value of the original variable to the new global copy. |
| 8385 | CGF.Builder.CreateMemCpy( |
| 8386 | CGF.MakeNaturalAlignAddrLValue(Addr, ElementType).getAddress(), |
| 8387 | Address(CV, CGF.getContext().getTypeAlignInChars(ElementType)), |
Alexey Bataev | c2b831f | 2019-04-02 16:03:40 +0000 | [diff] [blame] | 8388 | CurSizes.back(), /*isVolatile=*/false); |
Alexey Bataev | 1af5bd5 | 2019-03-05 17:47:18 +0000 | [diff] [blame] | 8389 | // Use new global variable as the base pointers. |
| 8390 | CurBasePointers.push_back(Addr); |
| 8391 | CurPointers.push_back(Addr); |
| 8392 | } else { |
| 8393 | CurBasePointers.push_back(CV); |
Alexey Bataev | c2b831f | 2019-04-02 16:03:40 +0000 | [diff] [blame] | 8394 | if (FirstPrivateDecls.count(VD) && ElementType->isAnyPointerType()) { |
| 8395 | Address PtrAddr = CGF.EmitLoadOfReference(CGF.MakeAddrLValue( |
| 8396 | CV, ElementType, CGF.getContext().getDeclAlign(VD), |
| 8397 | AlignmentSource::Decl)); |
| 8398 | CurPointers.push_back(PtrAddr.getPointer()); |
| 8399 | } else { |
| 8400 | CurPointers.push_back(CV); |
| 8401 | } |
Alexey Bataev | 1af5bd5 | 2019-03-05 17:47:18 +0000 | [diff] [blame] | 8402 | } |
Samuel Antao | d486f84 | 2016-05-26 16:53:38 +0000 | [diff] [blame] | 8403 | } |
George Rokos | 065755d | 2017-11-07 18:27:04 +0000 | [diff] [blame] | 8404 | // Every default map produces a single argument which is a target parameter. |
| 8405 | CurMapTypes.back() |= OMP_MAP_TARGET_PARAM; |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 8406 | |
| 8407 | // Add flag stating this is an implicit map. |
| 8408 | CurMapTypes.back() |= OMP_MAP_IMPLICIT; |
Samuel Antao | d486f84 | 2016-05-26 16:53:38 +0000 | [diff] [blame] | 8409 | } |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 8410 | }; |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8411 | } // anonymous namespace |
| 8412 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 8413 | /// 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] | 8414 | /// offloading runtime library. If there is no map or capture information, |
| 8415 | /// return nullptr by reference. |
| 8416 | static void |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 8417 | emitOffloadingArrays(CodeGenFunction &CGF, |
| 8418 | MappableExprsHandler::MapBaseValuesArrayTy &BasePointers, |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8419 | MappableExprsHandler::MapValuesArrayTy &Pointers, |
| 8420 | MappableExprsHandler::MapValuesArrayTy &Sizes, |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 8421 | MappableExprsHandler::MapFlagsArrayTy &MapTypes, |
| 8422 | CGOpenMPRuntime::TargetDataInfo &Info) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8423 | CodeGenModule &CGM = CGF.CGM; |
| 8424 | ASTContext &Ctx = CGF.getContext(); |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8425 | |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 8426 | // Reset the array information. |
| 8427 | Info.clearArrayInfo(); |
| 8428 | Info.NumberOfPtrs = BasePointers.size(); |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8429 | |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 8430 | if (Info.NumberOfPtrs) { |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8431 | // Detect if we have any capture size requiring runtime evaluation of the |
| 8432 | // size so that a constant array could be eventually used. |
| 8433 | bool hasRuntimeEvaluationCaptureSize = false; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8434 | for (llvm::Value *S : Sizes) |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8435 | if (!isa<llvm::Constant>(S)) { |
| 8436 | hasRuntimeEvaluationCaptureSize = true; |
| 8437 | break; |
| 8438 | } |
| 8439 | |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 8440 | llvm::APInt PointerNumAP(32, Info.NumberOfPtrs, /*isSigned=*/true); |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8441 | QualType PointerArrayType = |
| 8442 | Ctx.getConstantArrayType(Ctx.VoidPtrTy, PointerNumAP, ArrayType::Normal, |
| 8443 | /*IndexTypeQuals=*/0); |
| 8444 | |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 8445 | Info.BasePointersArray = |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8446 | CGF.CreateMemTemp(PointerArrayType, ".offload_baseptrs").getPointer(); |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 8447 | Info.PointersArray = |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8448 | CGF.CreateMemTemp(PointerArrayType, ".offload_ptrs").getPointer(); |
| 8449 | |
| 8450 | // If we don't have any VLA types or other types that require runtime |
| 8451 | // evaluation, we can use a constant array for the map sizes, otherwise we |
| 8452 | // need to fill up the arrays as we do for the pointers. |
| 8453 | if (hasRuntimeEvaluationCaptureSize) { |
| 8454 | QualType SizeArrayType = Ctx.getConstantArrayType( |
| 8455 | Ctx.getSizeType(), PointerNumAP, ArrayType::Normal, |
| 8456 | /*IndexTypeQuals=*/0); |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 8457 | Info.SizesArray = |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8458 | CGF.CreateMemTemp(SizeArrayType, ".offload_sizes").getPointer(); |
| 8459 | } else { |
| 8460 | // We expect all the sizes to be constant, so we collect them to create |
| 8461 | // a constant array. |
| 8462 | SmallVector<llvm::Constant *, 16> ConstSizes; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8463 | for (llvm::Value *S : Sizes) |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8464 | ConstSizes.push_back(cast<llvm::Constant>(S)); |
| 8465 | |
| 8466 | auto *SizesArrayInit = llvm::ConstantArray::get( |
| 8467 | llvm::ArrayType::get(CGM.SizeTy, ConstSizes.size()), ConstSizes); |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 8468 | std::string Name = CGM.getOpenMPRuntime().getName({"offload_sizes"}); |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8469 | auto *SizesArrayGbl = new llvm::GlobalVariable( |
| 8470 | CGM.getModule(), SizesArrayInit->getType(), |
| 8471 | /*isConstant=*/true, llvm::GlobalValue::PrivateLinkage, |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 8472 | SizesArrayInit, Name); |
Peter Collingbourne | bcf909d | 2016-06-14 21:02:05 +0000 | [diff] [blame] | 8473 | SizesArrayGbl->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global); |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 8474 | Info.SizesArray = SizesArrayGbl; |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8475 | } |
| 8476 | |
| 8477 | // The map types are always constant so we don't need to generate code to |
| 8478 | // fill arrays. Instead, we create an array constant. |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 8479 | SmallVector<uint64_t, 4> Mapping(MapTypes.size(), 0); |
| 8480 | llvm::copy(MapTypes, Mapping.begin()); |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8481 | llvm::Constant *MapTypesArrayInit = |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 8482 | llvm::ConstantDataArray::get(CGF.Builder.getContext(), Mapping); |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 8483 | std::string MaptypesName = |
| 8484 | CGM.getOpenMPRuntime().getName({"offload_maptypes"}); |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8485 | auto *MapTypesArrayGbl = new llvm::GlobalVariable( |
| 8486 | CGM.getModule(), MapTypesArrayInit->getType(), |
| 8487 | /*isConstant=*/true, llvm::GlobalValue::PrivateLinkage, |
Alexey Bataev | 18fa232 | 2018-05-02 14:20:50 +0000 | [diff] [blame] | 8488 | MapTypesArrayInit, MaptypesName); |
Peter Collingbourne | bcf909d | 2016-06-14 21:02:05 +0000 | [diff] [blame] | 8489 | MapTypesArrayGbl->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global); |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 8490 | Info.MapTypesArray = MapTypesArrayGbl; |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8491 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8492 | for (unsigned I = 0; I < Info.NumberOfPtrs; ++I) { |
| 8493 | llvm::Value *BPVal = *BasePointers[I]; |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8494 | llvm::Value *BP = CGF.Builder.CreateConstInBoundsGEP2_32( |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 8495 | llvm::ArrayType::get(CGM.VoidPtrTy, Info.NumberOfPtrs), |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8496 | Info.BasePointersArray, 0, I); |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 8497 | BP = CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
| 8498 | BP, BPVal->getType()->getPointerTo(/*AddrSpace=*/0)); |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8499 | Address BPAddr(BP, Ctx.getTypeAlignInChars(Ctx.VoidPtrTy)); |
| 8500 | CGF.Builder.CreateStore(BPVal, BPAddr); |
| 8501 | |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 8502 | if (Info.requiresDevicePointerInfo()) |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8503 | if (const ValueDecl *DevVD = BasePointers[I].getDevicePtrDecl()) |
Alexey Bataev | 43a919f | 2018-04-13 17:48:43 +0000 | [diff] [blame] | 8504 | Info.CaptureDeviceAddrMap.try_emplace(DevVD, BPAddr); |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 8505 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8506 | llvm::Value *PVal = Pointers[I]; |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8507 | llvm::Value *P = CGF.Builder.CreateConstInBoundsGEP2_32( |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 8508 | llvm::ArrayType::get(CGM.VoidPtrTy, Info.NumberOfPtrs), |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8509 | Info.PointersArray, 0, I); |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 8510 | P = CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
| 8511 | P, PVal->getType()->getPointerTo(/*AddrSpace=*/0)); |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8512 | Address PAddr(P, Ctx.getTypeAlignInChars(Ctx.VoidPtrTy)); |
| 8513 | CGF.Builder.CreateStore(PVal, PAddr); |
| 8514 | |
| 8515 | if (hasRuntimeEvaluationCaptureSize) { |
| 8516 | llvm::Value *S = CGF.Builder.CreateConstInBoundsGEP2_32( |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 8517 | llvm::ArrayType::get(CGM.SizeTy, Info.NumberOfPtrs), |
| 8518 | Info.SizesArray, |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8519 | /*Idx0=*/0, |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8520 | /*Idx1=*/I); |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8521 | Address SAddr(S, Ctx.getTypeAlignInChars(Ctx.getSizeType())); |
| 8522 | CGF.Builder.CreateStore( |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8523 | CGF.Builder.CreateIntCast(Sizes[I], CGM.SizeTy, /*isSigned=*/true), |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8524 | SAddr); |
| 8525 | } |
| 8526 | } |
| 8527 | } |
| 8528 | } |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 8529 | /// 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] | 8530 | /// arrays of pointers, sizes and map types. |
| 8531 | static void emitOffloadingArraysArgument( |
| 8532 | CodeGenFunction &CGF, llvm::Value *&BasePointersArrayArg, |
| 8533 | llvm::Value *&PointersArrayArg, llvm::Value *&SizesArrayArg, |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 8534 | llvm::Value *&MapTypesArrayArg, CGOpenMPRuntime::TargetDataInfo &Info) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8535 | CodeGenModule &CGM = CGF.CGM; |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 8536 | if (Info.NumberOfPtrs) { |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8537 | BasePointersArrayArg = CGF.Builder.CreateConstInBoundsGEP2_32( |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 8538 | llvm::ArrayType::get(CGM.VoidPtrTy, Info.NumberOfPtrs), |
| 8539 | Info.BasePointersArray, |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8540 | /*Idx0=*/0, /*Idx1=*/0); |
| 8541 | PointersArrayArg = CGF.Builder.CreateConstInBoundsGEP2_32( |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 8542 | llvm::ArrayType::get(CGM.VoidPtrTy, Info.NumberOfPtrs), |
| 8543 | Info.PointersArray, |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8544 | /*Idx0=*/0, |
| 8545 | /*Idx1=*/0); |
| 8546 | SizesArrayArg = CGF.Builder.CreateConstInBoundsGEP2_32( |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 8547 | llvm::ArrayType::get(CGM.SizeTy, Info.NumberOfPtrs), Info.SizesArray, |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8548 | /*Idx0=*/0, /*Idx1=*/0); |
| 8549 | MapTypesArrayArg = CGF.Builder.CreateConstInBoundsGEP2_32( |
George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 8550 | llvm::ArrayType::get(CGM.Int64Ty, Info.NumberOfPtrs), |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 8551 | Info.MapTypesArray, |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8552 | /*Idx0=*/0, |
| 8553 | /*Idx1=*/0); |
| 8554 | } else { |
| 8555 | BasePointersArrayArg = llvm::ConstantPointerNull::get(CGM.VoidPtrPtrTy); |
| 8556 | PointersArrayArg = llvm::ConstantPointerNull::get(CGM.VoidPtrPtrTy); |
| 8557 | SizesArrayArg = llvm::ConstantPointerNull::get(CGM.SizeTy->getPointerTo()); |
| 8558 | MapTypesArrayArg = |
George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 8559 | llvm::ConstantPointerNull::get(CGM.Int64Ty->getPointerTo()); |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8560 | } |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 8561 | } |
| 8562 | |
Alexey Bataev | 7bb3353 | 2019-01-07 21:30:43 +0000 | [diff] [blame] | 8563 | /// Check for inner distribute directive. |
| 8564 | static const OMPExecutableDirective * |
| 8565 | getNestedDistributeDirective(ASTContext &Ctx, const OMPExecutableDirective &D) { |
| 8566 | const auto *CS = D.getInnermostCapturedStmt(); |
| 8567 | const auto *Body = |
| 8568 | CS->getCapturedStmt()->IgnoreContainers(/*IgnoreCaptured=*/true); |
Alexey Bataev | 5c42736 | 2019-04-10 19:11:33 +0000 | [diff] [blame] | 8569 | const Stmt *ChildStmt = |
| 8570 | CGOpenMPSIMDRuntime::getSingleCompoundChild(Ctx, Body); |
Alexey Bataev | 7bb3353 | 2019-01-07 21:30:43 +0000 | [diff] [blame] | 8571 | |
Alexey Bataev | 5c42736 | 2019-04-10 19:11:33 +0000 | [diff] [blame] | 8572 | if (const auto *NestedDir = |
| 8573 | dyn_cast_or_null<OMPExecutableDirective>(ChildStmt)) { |
Alexey Bataev | 7bb3353 | 2019-01-07 21:30:43 +0000 | [diff] [blame] | 8574 | OpenMPDirectiveKind DKind = NestedDir->getDirectiveKind(); |
| 8575 | switch (D.getDirectiveKind()) { |
| 8576 | case OMPD_target: |
| 8577 | if (isOpenMPDistributeDirective(DKind)) |
| 8578 | return NestedDir; |
| 8579 | if (DKind == OMPD_teams) { |
| 8580 | Body = NestedDir->getInnermostCapturedStmt()->IgnoreContainers( |
| 8581 | /*IgnoreCaptured=*/true); |
| 8582 | if (!Body) |
| 8583 | return nullptr; |
Alexey Bataev | 5c42736 | 2019-04-10 19:11:33 +0000 | [diff] [blame] | 8584 | ChildStmt = CGOpenMPSIMDRuntime::getSingleCompoundChild(Ctx, Body); |
| 8585 | if (const auto *NND = |
| 8586 | dyn_cast_or_null<OMPExecutableDirective>(ChildStmt)) { |
Alexey Bataev | 7bb3353 | 2019-01-07 21:30:43 +0000 | [diff] [blame] | 8587 | DKind = NND->getDirectiveKind(); |
| 8588 | if (isOpenMPDistributeDirective(DKind)) |
| 8589 | return NND; |
| 8590 | } |
| 8591 | } |
| 8592 | return nullptr; |
| 8593 | case OMPD_target_teams: |
| 8594 | if (isOpenMPDistributeDirective(DKind)) |
| 8595 | return NestedDir; |
| 8596 | return nullptr; |
| 8597 | case OMPD_target_parallel: |
| 8598 | case OMPD_target_simd: |
| 8599 | case OMPD_target_parallel_for: |
| 8600 | case OMPD_target_parallel_for_simd: |
| 8601 | return nullptr; |
| 8602 | case OMPD_target_teams_distribute: |
| 8603 | case OMPD_target_teams_distribute_simd: |
| 8604 | case OMPD_target_teams_distribute_parallel_for: |
| 8605 | case OMPD_target_teams_distribute_parallel_for_simd: |
| 8606 | case OMPD_parallel: |
| 8607 | case OMPD_for: |
| 8608 | case OMPD_parallel_for: |
| 8609 | case OMPD_parallel_sections: |
| 8610 | case OMPD_for_simd: |
| 8611 | case OMPD_parallel_for_simd: |
| 8612 | case OMPD_cancel: |
| 8613 | case OMPD_cancellation_point: |
| 8614 | case OMPD_ordered: |
| 8615 | case OMPD_threadprivate: |
Alexey Bataev | 25ed0c0 | 2019-03-07 17:54:44 +0000 | [diff] [blame] | 8616 | case OMPD_allocate: |
Alexey Bataev | 7bb3353 | 2019-01-07 21:30:43 +0000 | [diff] [blame] | 8617 | case OMPD_task: |
| 8618 | case OMPD_simd: |
| 8619 | case OMPD_sections: |
| 8620 | case OMPD_section: |
| 8621 | case OMPD_single: |
| 8622 | case OMPD_master: |
| 8623 | case OMPD_critical: |
| 8624 | case OMPD_taskyield: |
| 8625 | case OMPD_barrier: |
| 8626 | case OMPD_taskwait: |
| 8627 | case OMPD_taskgroup: |
| 8628 | case OMPD_atomic: |
| 8629 | case OMPD_flush: |
| 8630 | case OMPD_teams: |
| 8631 | case OMPD_target_data: |
| 8632 | case OMPD_target_exit_data: |
| 8633 | case OMPD_target_enter_data: |
| 8634 | case OMPD_distribute: |
| 8635 | case OMPD_distribute_simd: |
| 8636 | case OMPD_distribute_parallel_for: |
| 8637 | case OMPD_distribute_parallel_for_simd: |
| 8638 | case OMPD_teams_distribute: |
| 8639 | case OMPD_teams_distribute_simd: |
| 8640 | case OMPD_teams_distribute_parallel_for: |
| 8641 | case OMPD_teams_distribute_parallel_for_simd: |
| 8642 | case OMPD_target_update: |
| 8643 | case OMPD_declare_simd: |
| 8644 | case OMPD_declare_target: |
| 8645 | case OMPD_end_declare_target: |
| 8646 | case OMPD_declare_reduction: |
Michael Kruse | 251e148 | 2019-02-01 20:25:04 +0000 | [diff] [blame] | 8647 | case OMPD_declare_mapper: |
Alexey Bataev | 7bb3353 | 2019-01-07 21:30:43 +0000 | [diff] [blame] | 8648 | case OMPD_taskloop: |
| 8649 | case OMPD_taskloop_simd: |
| 8650 | case OMPD_requires: |
| 8651 | case OMPD_unknown: |
| 8652 | llvm_unreachable("Unexpected directive."); |
| 8653 | } |
| 8654 | } |
| 8655 | |
| 8656 | return nullptr; |
| 8657 | } |
| 8658 | |
| 8659 | void CGOpenMPRuntime::emitTargetNumIterationsCall( |
| 8660 | CodeGenFunction &CGF, const OMPExecutableDirective &D, const Expr *Device, |
| 8661 | const llvm::function_ref<llvm::Value *( |
| 8662 | CodeGenFunction &CGF, const OMPLoopDirective &D)> &SizeEmitter) { |
| 8663 | OpenMPDirectiveKind Kind = D.getDirectiveKind(); |
| 8664 | const OMPExecutableDirective *TD = &D; |
| 8665 | // Get nested teams distribute kind directive, if any. |
| 8666 | if (!isOpenMPDistributeDirective(Kind) || !isOpenMPTeamsDirective(Kind)) |
| 8667 | TD = getNestedDistributeDirective(CGM.getContext(), D); |
| 8668 | if (!TD) |
| 8669 | return; |
| 8670 | const auto *LD = cast<OMPLoopDirective>(TD); |
| 8671 | auto &&CodeGen = [LD, &Device, &SizeEmitter, this](CodeGenFunction &CGF, |
| 8672 | PrePostActionTy &) { |
| 8673 | llvm::Value *NumIterations = SizeEmitter(CGF, *LD); |
| 8674 | |
| 8675 | // Emit device ID if any. |
| 8676 | llvm::Value *DeviceID; |
| 8677 | if (Device) |
| 8678 | DeviceID = CGF.Builder.CreateIntCast(CGF.EmitScalarExpr(Device), |
| 8679 | CGF.Int64Ty, /*isSigned=*/true); |
| 8680 | else |
| 8681 | DeviceID = CGF.Builder.getInt64(OMP_DEVICEID_UNDEF); |
| 8682 | |
| 8683 | llvm::Value *Args[] = {DeviceID, NumIterations}; |
| 8684 | CGF.EmitRuntimeCall( |
| 8685 | createRuntimeFunction(OMPRTL__kmpc_push_target_tripcount), Args); |
| 8686 | }; |
| 8687 | emitInlinedDirective(CGF, OMPD_unknown, CodeGen); |
| 8688 | } |
| 8689 | |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 8690 | void CGOpenMPRuntime::emitTargetCall(CodeGenFunction &CGF, |
| 8691 | const OMPExecutableDirective &D, |
James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 8692 | llvm::Function *OutlinedFn, |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 8693 | llvm::Value *OutlinedFnID, |
Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 8694 | const Expr *IfCond, const Expr *Device) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 8695 | if (!CGF.HaveInsertPoint()) |
| 8696 | return; |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 8697 | |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 8698 | assert(OutlinedFn && "Invalid outlined function!"); |
| 8699 | |
Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 8700 | const bool RequiresOuterTask = D.hasClausesOfKind<OMPDependClause>(); |
| 8701 | llvm::SmallVector<llvm::Value *, 16> CapturedVars; |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 8702 | const CapturedStmt &CS = *D.getCapturedStmt(OMPD_target); |
Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 8703 | auto &&ArgsCodegen = [&CS, &CapturedVars](CodeGenFunction &CGF, |
| 8704 | PrePostActionTy &) { |
| 8705 | CGF.GenerateOpenMPCapturedVars(CS, CapturedVars); |
| 8706 | }; |
| 8707 | emitInlinedDirective(CGF, OMPD_unknown, ArgsCodegen); |
Samuel Antao | 86ace55 | 2016-04-27 22:40:57 +0000 | [diff] [blame] | 8708 | |
Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 8709 | CodeGenFunction::OMPTargetDataInfo InputInfo; |
| 8710 | llvm::Value *MapTypesArray = nullptr; |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 8711 | // Fill up the pointer arrays and transfer execution to the device. |
Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 8712 | auto &&ThenGen = [this, Device, OutlinedFn, OutlinedFnID, &D, &InputInfo, |
| 8713 | &MapTypesArray, &CS, RequiresOuterTask, |
| 8714 | &CapturedVars](CodeGenFunction &CGF, PrePostActionTy &) { |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 8715 | // On top of the arrays that were filled up, the target offloading call |
| 8716 | // takes as arguments the device id as well as the host pointer. The host |
| 8717 | // pointer is used by the runtime library to identify the current target |
| 8718 | // region, so it only has to be unique and not necessarily point to |
| 8719 | // anything. It could be the pointer to the outlined function that |
| 8720 | // implements the target region, but we aren't using that so that the |
| 8721 | // compiler doesn't need to keep that, and could therefore inline the host |
| 8722 | // function if proven worthwhile during optimization. |
| 8723 | |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 8724 | // From this point on, we need to have an ID of the target region defined. |
| 8725 | assert(OutlinedFnID && "Invalid outlined function ID!"); |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 8726 | |
| 8727 | // Emit device ID if any. |
| 8728 | llvm::Value *DeviceID; |
George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 8729 | if (Device) { |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 8730 | DeviceID = CGF.Builder.CreateIntCast(CGF.EmitScalarExpr(Device), |
George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 8731 | CGF.Int64Ty, /*isSigned=*/true); |
| 8732 | } else { |
| 8733 | DeviceID = CGF.Builder.getInt64(OMP_DEVICEID_UNDEF); |
| 8734 | } |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 8735 | |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8736 | // Emit the number of elements in the offloading arrays. |
Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 8737 | llvm::Value *PointerNum = |
| 8738 | CGF.Builder.getInt32(InputInfo.NumberOfTargetItems); |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 8739 | |
Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 8740 | // Return value of the runtime offloading call. |
| 8741 | llvm::Value *Return; |
| 8742 | |
Alexey Bataev | 5c42736 | 2019-04-10 19:11:33 +0000 | [diff] [blame] | 8743 | llvm::Value *NumTeams = emitNumTeamsForTargetDirective(CGF, D); |
| 8744 | llvm::Value *NumThreads = emitNumThreadsForTargetDirective(CGF, D); |
Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 8745 | |
Alexey Bataev | a9f77c6 | 2017-12-13 21:04:20 +0000 | [diff] [blame] | 8746 | bool HasNowait = D.hasClausesOfKind<OMPNowaitClause>(); |
Arpith Chacko Jacob | 33c849a | 2017-01-25 00:57:16 +0000 | [diff] [blame] | 8747 | // The target region is an outlined function launched by the runtime |
| 8748 | // via calls __tgt_target() or __tgt_target_teams(). |
| 8749 | // |
| 8750 | // __tgt_target() launches a target region with one team and one thread, |
| 8751 | // executing a serial region. This master thread may in turn launch |
| 8752 | // more threads within its team upon encountering a parallel region, |
| 8753 | // however, no additional teams can be launched on the device. |
| 8754 | // |
| 8755 | // __tgt_target_teams() launches a target region with one or more teams, |
| 8756 | // each with one or more threads. This call is required for target |
| 8757 | // constructs such as: |
| 8758 | // 'target teams' |
| 8759 | // 'target' / 'teams' |
| 8760 | // 'target teams distribute parallel for' |
| 8761 | // 'target parallel' |
| 8762 | // and so on. |
| 8763 | // |
| 8764 | // Note that on the host and CPU targets, the runtime implementation of |
| 8765 | // these calls simply call the outlined function without forking threads. |
| 8766 | // The outlined functions themselves have runtime calls to |
| 8767 | // __kmpc_fork_teams() and __kmpc_fork() for this purpose, codegen'd by |
| 8768 | // the compiler in emitTeamsCall() and emitParallelCall(). |
| 8769 | // |
| 8770 | // In contrast, on the NVPTX target, the implementation of |
| 8771 | // __tgt_target_teams() launches a GPU kernel with the requested number |
| 8772 | // 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] | 8773 | if (NumTeams) { |
Arpith Chacko Jacob | 33c849a | 2017-01-25 00:57:16 +0000 | [diff] [blame] | 8774 | // If we have NumTeams defined this means that we have an enclosed teams |
| 8775 | // region. Therefore we also expect to have NumThreads defined. These two |
| 8776 | // values should be defined in the presence of a teams directive, |
| 8777 | // regardless of having any clauses associated. If the user is using teams |
| 8778 | // but no clauses, these two values will be the default that should be |
| 8779 | // passed to the runtime library - a 32-bit integer with the value zero. |
| 8780 | assert(NumThreads && "Thread limit expression should be available along " |
| 8781 | "with number of teams."); |
Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 8782 | llvm::Value *OffloadingArgs[] = {DeviceID, |
| 8783 | OutlinedFnID, |
| 8784 | PointerNum, |
| 8785 | InputInfo.BasePointersArray.getPointer(), |
| 8786 | InputInfo.PointersArray.getPointer(), |
| 8787 | InputInfo.SizesArray.getPointer(), |
| 8788 | MapTypesArray, |
| 8789 | NumTeams, |
| 8790 | NumThreads}; |
Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 8791 | Return = CGF.EmitRuntimeCall( |
Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 8792 | createRuntimeFunction(HasNowait ? OMPRTL__tgt_target_teams_nowait |
| 8793 | : OMPRTL__tgt_target_teams), |
Alexey Bataev | a9f77c6 | 2017-12-13 21:04:20 +0000 | [diff] [blame] | 8794 | OffloadingArgs); |
Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 8795 | } else { |
Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 8796 | llvm::Value *OffloadingArgs[] = {DeviceID, |
| 8797 | OutlinedFnID, |
| 8798 | PointerNum, |
| 8799 | InputInfo.BasePointersArray.getPointer(), |
| 8800 | InputInfo.PointersArray.getPointer(), |
| 8801 | InputInfo.SizesArray.getPointer(), |
| 8802 | MapTypesArray}; |
Alexey Bataev | a9f77c6 | 2017-12-13 21:04:20 +0000 | [diff] [blame] | 8803 | Return = CGF.EmitRuntimeCall( |
Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 8804 | createRuntimeFunction(HasNowait ? OMPRTL__tgt_target_nowait |
| 8805 | : OMPRTL__tgt_target), |
Alexey Bataev | a9f77c6 | 2017-12-13 21:04:20 +0000 | [diff] [blame] | 8806 | OffloadingArgs); |
Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 8807 | } |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 8808 | |
Alexey Bataev | 2a007e0 | 2017-10-02 14:20:58 +0000 | [diff] [blame] | 8809 | // Check the error code and execute the host version if required. |
| 8810 | llvm::BasicBlock *OffloadFailedBlock = |
| 8811 | CGF.createBasicBlock("omp_offload.failed"); |
| 8812 | llvm::BasicBlock *OffloadContBlock = |
| 8813 | CGF.createBasicBlock("omp_offload.cont"); |
| 8814 | llvm::Value *Failed = CGF.Builder.CreateIsNotNull(Return); |
| 8815 | CGF.Builder.CreateCondBr(Failed, OffloadFailedBlock, OffloadContBlock); |
| 8816 | |
| 8817 | CGF.EmitBlock(OffloadFailedBlock); |
Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 8818 | if (RequiresOuterTask) { |
| 8819 | CapturedVars.clear(); |
| 8820 | CGF.GenerateOpenMPCapturedVars(CS, CapturedVars); |
| 8821 | } |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 8822 | emitOutlinedFunctionCall(CGF, D.getBeginLoc(), OutlinedFn, CapturedVars); |
Alexey Bataev | 2a007e0 | 2017-10-02 14:20:58 +0000 | [diff] [blame] | 8823 | CGF.EmitBranch(OffloadContBlock); |
| 8824 | |
| 8825 | CGF.EmitBlock(OffloadContBlock, /*IsFinished=*/true); |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 8826 | }; |
| 8827 | |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 8828 | // Notify that the host version must be executed. |
Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 8829 | auto &&ElseGen = [this, &D, OutlinedFn, &CS, &CapturedVars, |
| 8830 | RequiresOuterTask](CodeGenFunction &CGF, |
| 8831 | PrePostActionTy &) { |
| 8832 | if (RequiresOuterTask) { |
| 8833 | CapturedVars.clear(); |
| 8834 | CGF.GenerateOpenMPCapturedVars(CS, CapturedVars); |
| 8835 | } |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 8836 | emitOutlinedFunctionCall(CGF, D.getBeginLoc(), OutlinedFn, CapturedVars); |
Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 8837 | }; |
| 8838 | |
| 8839 | auto &&TargetThenGen = [this, &ThenGen, &D, &InputInfo, &MapTypesArray, |
| 8840 | &CapturedVars, RequiresOuterTask, |
| 8841 | &CS](CodeGenFunction &CGF, PrePostActionTy &) { |
| 8842 | // Fill up the arrays with all the captured variables. |
| 8843 | MappableExprsHandler::MapBaseValuesArrayTy BasePointers; |
| 8844 | MappableExprsHandler::MapValuesArrayTy Pointers; |
| 8845 | MappableExprsHandler::MapValuesArrayTy Sizes; |
| 8846 | MappableExprsHandler::MapFlagsArrayTy MapTypes; |
| 8847 | |
Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 8848 | // Get mappable expression information. |
| 8849 | MappableExprsHandler MEHandler(D, CGF); |
Alexey Bataev | 969dbc0 | 2018-11-08 15:47:39 +0000 | [diff] [blame] | 8850 | llvm::DenseMap<llvm::Value *, llvm::Value *> LambdaPointers; |
Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 8851 | |
| 8852 | auto RI = CS.getCapturedRecordDecl()->field_begin(); |
| 8853 | auto CV = CapturedVars.begin(); |
| 8854 | for (CapturedStmt::const_capture_iterator CI = CS.capture_begin(), |
| 8855 | CE = CS.capture_end(); |
| 8856 | CI != CE; ++CI, ++RI, ++CV) { |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 8857 | MappableExprsHandler::MapBaseValuesArrayTy CurBasePointers; |
| 8858 | MappableExprsHandler::MapValuesArrayTy CurPointers; |
| 8859 | MappableExprsHandler::MapValuesArrayTy CurSizes; |
| 8860 | MappableExprsHandler::MapFlagsArrayTy CurMapTypes; |
| 8861 | MappableExprsHandler::StructRangeInfoTy PartialStruct; |
Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 8862 | |
| 8863 | // VLA sizes are passed to the outlined region by copy and do not have map |
| 8864 | // information associated. |
| 8865 | if (CI->capturesVariableArrayType()) { |
| 8866 | CurBasePointers.push_back(*CV); |
| 8867 | CurPointers.push_back(*CV); |
| 8868 | CurSizes.push_back(CGF.getTypeSize(RI->getType())); |
| 8869 | // Copy to the device as an argument. No need to retrieve it. |
| 8870 | CurMapTypes.push_back(MappableExprsHandler::OMP_MAP_LITERAL | |
| 8871 | MappableExprsHandler::OMP_MAP_TARGET_PARAM); |
| 8872 | } else { |
| 8873 | // If we have any information in the map clause, we use it, otherwise we |
| 8874 | // just do a default mapping. |
| 8875 | MEHandler.generateInfoForCapture(CI, *CV, CurBasePointers, CurPointers, |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 8876 | CurSizes, CurMapTypes, PartialStruct); |
Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 8877 | if (CurBasePointers.empty()) |
| 8878 | MEHandler.generateDefaultMapInfo(*CI, **RI, *CV, CurBasePointers, |
| 8879 | CurPointers, CurSizes, CurMapTypes); |
Alexey Bataev | 6070542 | 2018-10-30 15:50:12 +0000 | [diff] [blame] | 8880 | // Generate correct mapping for variables captured by reference in |
| 8881 | // lambdas. |
| 8882 | if (CI->capturesVariable()) |
Alexey Bataev | 969dbc0 | 2018-11-08 15:47:39 +0000 | [diff] [blame] | 8883 | MEHandler.generateInfoForLambdaCaptures( |
| 8884 | CI->getCapturedVar(), *CV, CurBasePointers, CurPointers, CurSizes, |
| 8885 | CurMapTypes, LambdaPointers); |
Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 8886 | } |
| 8887 | // We expect to have at least an element of information for this capture. |
| 8888 | assert(!CurBasePointers.empty() && |
| 8889 | "Non-existing map pointer for capture!"); |
| 8890 | assert(CurBasePointers.size() == CurPointers.size() && |
| 8891 | CurBasePointers.size() == CurSizes.size() && |
| 8892 | CurBasePointers.size() == CurMapTypes.size() && |
| 8893 | "Inconsistent map information sizes!"); |
| 8894 | |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 8895 | // If there is an entry in PartialStruct it means we have a struct with |
| 8896 | // individual members mapped. Emit an extra combined entry. |
| 8897 | if (PartialStruct.Base.isValid()) |
| 8898 | MEHandler.emitCombinedEntry(BasePointers, Pointers, Sizes, MapTypes, |
| 8899 | CurMapTypes, PartialStruct); |
| 8900 | |
Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 8901 | // We need to append the results of this capture to what we already have. |
| 8902 | BasePointers.append(CurBasePointers.begin(), CurBasePointers.end()); |
| 8903 | Pointers.append(CurPointers.begin(), CurPointers.end()); |
| 8904 | Sizes.append(CurSizes.begin(), CurSizes.end()); |
| 8905 | MapTypes.append(CurMapTypes.begin(), CurMapTypes.end()); |
| 8906 | } |
Alexey Bataev | 6070542 | 2018-10-30 15:50:12 +0000 | [diff] [blame] | 8907 | // Adjust MEMBER_OF flags for the lambdas captures. |
Alexey Bataev | 969dbc0 | 2018-11-08 15:47:39 +0000 | [diff] [blame] | 8908 | MEHandler.adjustMemberOfForLambdaCaptures(LambdaPointers, BasePointers, |
| 8909 | Pointers, MapTypes); |
Alexey Bataev | 92327c5 | 2018-03-26 16:40:55 +0000 | [diff] [blame] | 8910 | // Map other list items in the map clause which are not captured variables |
| 8911 | // but "declare target link" global variables. |
Alexey Bataev | b363813 | 2018-07-19 16:34:13 +0000 | [diff] [blame] | 8912 | MEHandler.generateInfoForDeclareTargetLink(BasePointers, Pointers, Sizes, |
| 8913 | MapTypes); |
Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 8914 | |
| 8915 | TargetDataInfo Info; |
| 8916 | // Fill up the arrays and create the arguments. |
| 8917 | emitOffloadingArrays(CGF, BasePointers, Pointers, Sizes, MapTypes, Info); |
| 8918 | emitOffloadingArraysArgument(CGF, Info.BasePointersArray, |
| 8919 | Info.PointersArray, Info.SizesArray, |
| 8920 | Info.MapTypesArray, Info); |
| 8921 | InputInfo.NumberOfTargetItems = Info.NumberOfPtrs; |
| 8922 | InputInfo.BasePointersArray = |
| 8923 | Address(Info.BasePointersArray, CGM.getPointerAlign()); |
| 8924 | InputInfo.PointersArray = |
| 8925 | Address(Info.PointersArray, CGM.getPointerAlign()); |
| 8926 | InputInfo.SizesArray = Address(Info.SizesArray, CGM.getPointerAlign()); |
| 8927 | MapTypesArray = Info.MapTypesArray; |
| 8928 | if (RequiresOuterTask) |
| 8929 | CGF.EmitOMPTargetTaskBasedDirective(D, ThenGen, InputInfo); |
| 8930 | else |
| 8931 | emitInlinedDirective(CGF, D.getDirectiveKind(), ThenGen); |
| 8932 | }; |
| 8933 | |
| 8934 | auto &&TargetElseGen = [this, &ElseGen, &D, RequiresOuterTask]( |
| 8935 | CodeGenFunction &CGF, PrePostActionTy &) { |
| 8936 | if (RequiresOuterTask) { |
| 8937 | CodeGenFunction::OMPTargetDataInfo InputInfo; |
| 8938 | CGF.EmitOMPTargetTaskBasedDirective(D, ElseGen, InputInfo); |
| 8939 | } else { |
| 8940 | emitInlinedDirective(CGF, D.getDirectiveKind(), ElseGen); |
| 8941 | } |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 8942 | }; |
| 8943 | |
| 8944 | // If we have a target function ID it means that we need to support |
| 8945 | // offloading, otherwise, just execute on the host. We need to execute on host |
| 8946 | // regardless of the conditional in the if clause if, e.g., the user do not |
| 8947 | // specify target triples. |
| 8948 | if (OutlinedFnID) { |
Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 8949 | if (IfCond) { |
| 8950 | emitOMPIfClause(CGF, IfCond, TargetThenGen, TargetElseGen); |
| 8951 | } else { |
| 8952 | RegionCodeGenTy ThenRCG(TargetThenGen); |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 8953 | ThenRCG(CGF); |
Alexey Bataev | f539faa | 2016-03-28 12:58:34 +0000 | [diff] [blame] | 8954 | } |
| 8955 | } else { |
Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 8956 | RegionCodeGenTy ElseRCG(TargetElseGen); |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 8957 | ElseRCG(CGF); |
Alexey Bataev | f539faa | 2016-03-28 12:58:34 +0000 | [diff] [blame] | 8958 | } |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 8959 | } |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 8960 | |
| 8961 | void CGOpenMPRuntime::scanForTargetRegionsFunctions(const Stmt *S, |
| 8962 | StringRef ParentName) { |
| 8963 | if (!S) |
| 8964 | return; |
| 8965 | |
Arpith Chacko Jacob | 43a8b7b | 2017-01-16 15:26:02 +0000 | [diff] [blame] | 8966 | // Codegen OMP target directives that offload compute to the device. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8967 | bool RequiresDeviceCodegen = |
Arpith Chacko Jacob | 43a8b7b | 2017-01-16 15:26:02 +0000 | [diff] [blame] | 8968 | isa<OMPExecutableDirective>(S) && |
| 8969 | isOpenMPTargetExecutionDirective( |
| 8970 | cast<OMPExecutableDirective>(S)->getDirectiveKind()); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 8971 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8972 | if (RequiresDeviceCodegen) { |
| 8973 | const auto &E = *cast<OMPExecutableDirective>(S); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 8974 | unsigned DeviceID; |
| 8975 | unsigned FileID; |
| 8976 | unsigned Line; |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 8977 | getTargetEntryUniqueInfo(CGM.getContext(), E.getBeginLoc(), DeviceID, |
Samuel Antao | 2de62b0 | 2016-02-13 23:35:10 +0000 | [diff] [blame] | 8978 | FileID, Line); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 8979 | |
| 8980 | // Is this a target region that should not be emitted as an entry point? If |
| 8981 | // so just signal we are done with this target region. |
Samuel Antao | 2de62b0 | 2016-02-13 23:35:10 +0000 | [diff] [blame] | 8982 | if (!OffloadEntriesInfoManager.hasTargetRegionEntryInfo(DeviceID, FileID, |
| 8983 | ParentName, Line)) |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 8984 | return; |
| 8985 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8986 | switch (E.getDirectiveKind()) { |
| 8987 | case OMPD_target: |
| 8988 | CodeGenFunction::EmitOMPTargetDeviceFunction(CGM, ParentName, |
| 8989 | cast<OMPTargetDirective>(E)); |
Arpith Chacko Jacob | 43a8b7b | 2017-01-16 15:26:02 +0000 | [diff] [blame] | 8990 | break; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8991 | case OMPD_target_parallel: |
Arpith Chacko Jacob | 19b911c | 2017-01-18 18:18:53 +0000 | [diff] [blame] | 8992 | CodeGenFunction::EmitOMPTargetParallelDeviceFunction( |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8993 | CGM, ParentName, cast<OMPTargetParallelDirective>(E)); |
Arpith Chacko Jacob | 19b911c | 2017-01-18 18:18:53 +0000 | [diff] [blame] | 8994 | break; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8995 | case OMPD_target_teams: |
Arpith Chacko Jacob | 99a1e0e | 2017-01-25 02:18:43 +0000 | [diff] [blame] | 8996 | CodeGenFunction::EmitOMPTargetTeamsDeviceFunction( |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8997 | CGM, ParentName, cast<OMPTargetTeamsDirective>(E)); |
Arpith Chacko Jacob | 99a1e0e | 2017-01-25 02:18:43 +0000 | [diff] [blame] | 8998 | break; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 8999 | case OMPD_target_teams_distribute: |
Alexey Bataev | dfa430f | 2017-12-08 15:03:50 +0000 | [diff] [blame] | 9000 | CodeGenFunction::EmitOMPTargetTeamsDistributeDeviceFunction( |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 9001 | CGM, ParentName, cast<OMPTargetTeamsDistributeDirective>(E)); |
Alexey Bataev | dfa430f | 2017-12-08 15:03:50 +0000 | [diff] [blame] | 9002 | break; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 9003 | case OMPD_target_teams_distribute_simd: |
Alexey Bataev | fbe17fb | 2017-12-13 19:45:06 +0000 | [diff] [blame] | 9004 | CodeGenFunction::EmitOMPTargetTeamsDistributeSimdDeviceFunction( |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 9005 | CGM, ParentName, cast<OMPTargetTeamsDistributeSimdDirective>(E)); |
Alexey Bataev | fbe17fb | 2017-12-13 19:45:06 +0000 | [diff] [blame] | 9006 | break; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 9007 | case OMPD_target_parallel_for: |
Alexey Bataev | fb0ebec | 2017-11-08 20:16:14 +0000 | [diff] [blame] | 9008 | CodeGenFunction::EmitOMPTargetParallelForDeviceFunction( |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 9009 | CGM, ParentName, cast<OMPTargetParallelForDirective>(E)); |
Alexey Bataev | fb0ebec | 2017-11-08 20:16:14 +0000 | [diff] [blame] | 9010 | break; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 9011 | case OMPD_target_parallel_for_simd: |
Alexey Bataev | 5d7edca | 2017-11-09 17:32:15 +0000 | [diff] [blame] | 9012 | CodeGenFunction::EmitOMPTargetParallelForSimdDeviceFunction( |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 9013 | CGM, ParentName, cast<OMPTargetParallelForSimdDirective>(E)); |
Alexey Bataev | 5d7edca | 2017-11-09 17:32:15 +0000 | [diff] [blame] | 9014 | break; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 9015 | case OMPD_target_simd: |
Alexey Bataev | f836537 | 2017-11-17 17:57:25 +0000 | [diff] [blame] | 9016 | CodeGenFunction::EmitOMPTargetSimdDeviceFunction( |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 9017 | CGM, ParentName, cast<OMPTargetSimdDirective>(E)); |
Alexey Bataev | f836537 | 2017-11-17 17:57:25 +0000 | [diff] [blame] | 9018 | break; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 9019 | case OMPD_target_teams_distribute_parallel_for: |
Carlo Bertolli | 52978c3 | 2018-01-03 21:12:44 +0000 | [diff] [blame] | 9020 | CodeGenFunction::EmitOMPTargetTeamsDistributeParallelForDeviceFunction( |
| 9021 | CGM, ParentName, |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 9022 | cast<OMPTargetTeamsDistributeParallelForDirective>(E)); |
Carlo Bertolli | 52978c3 | 2018-01-03 21:12:44 +0000 | [diff] [blame] | 9023 | break; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 9024 | case OMPD_target_teams_distribute_parallel_for_simd: |
Alexey Bataev | 647dd84 | 2018-01-15 20:59:40 +0000 | [diff] [blame] | 9025 | CodeGenFunction:: |
| 9026 | EmitOMPTargetTeamsDistributeParallelForSimdDeviceFunction( |
| 9027 | CGM, ParentName, |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 9028 | cast<OMPTargetTeamsDistributeParallelForSimdDirective>(E)); |
Alexey Bataev | 647dd84 | 2018-01-15 20:59:40 +0000 | [diff] [blame] | 9029 | break; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 9030 | case OMPD_parallel: |
| 9031 | case OMPD_for: |
| 9032 | case OMPD_parallel_for: |
| 9033 | case OMPD_parallel_sections: |
| 9034 | case OMPD_for_simd: |
| 9035 | case OMPD_parallel_for_simd: |
| 9036 | case OMPD_cancel: |
| 9037 | case OMPD_cancellation_point: |
| 9038 | case OMPD_ordered: |
| 9039 | case OMPD_threadprivate: |
Alexey Bataev | 25ed0c0 | 2019-03-07 17:54:44 +0000 | [diff] [blame] | 9040 | case OMPD_allocate: |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 9041 | case OMPD_task: |
| 9042 | case OMPD_simd: |
| 9043 | case OMPD_sections: |
| 9044 | case OMPD_section: |
| 9045 | case OMPD_single: |
| 9046 | case OMPD_master: |
| 9047 | case OMPD_critical: |
| 9048 | case OMPD_taskyield: |
| 9049 | case OMPD_barrier: |
| 9050 | case OMPD_taskwait: |
| 9051 | case OMPD_taskgroup: |
| 9052 | case OMPD_atomic: |
| 9053 | case OMPD_flush: |
| 9054 | case OMPD_teams: |
| 9055 | case OMPD_target_data: |
| 9056 | case OMPD_target_exit_data: |
| 9057 | case OMPD_target_enter_data: |
| 9058 | case OMPD_distribute: |
| 9059 | case OMPD_distribute_simd: |
| 9060 | case OMPD_distribute_parallel_for: |
| 9061 | case OMPD_distribute_parallel_for_simd: |
| 9062 | case OMPD_teams_distribute: |
| 9063 | case OMPD_teams_distribute_simd: |
| 9064 | case OMPD_teams_distribute_parallel_for: |
| 9065 | case OMPD_teams_distribute_parallel_for_simd: |
| 9066 | case OMPD_target_update: |
| 9067 | case OMPD_declare_simd: |
| 9068 | case OMPD_declare_target: |
| 9069 | case OMPD_end_declare_target: |
| 9070 | case OMPD_declare_reduction: |
Michael Kruse | 251e148 | 2019-02-01 20:25:04 +0000 | [diff] [blame] | 9071 | case OMPD_declare_mapper: |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 9072 | case OMPD_taskloop: |
| 9073 | case OMPD_taskloop_simd: |
Kelvin Li | 1408f91 | 2018-09-26 04:28:39 +0000 | [diff] [blame] | 9074 | case OMPD_requires: |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 9075 | case OMPD_unknown: |
Arpith Chacko Jacob | 43a8b7b | 2017-01-16 15:26:02 +0000 | [diff] [blame] | 9076 | llvm_unreachable("Unknown target directive for OpenMP device codegen."); |
| 9077 | } |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 9078 | return; |
| 9079 | } |
| 9080 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 9081 | if (const auto *E = dyn_cast<OMPExecutableDirective>(S)) { |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 9082 | if (!E->hasAssociatedStmt() || !E->getAssociatedStmt()) |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 9083 | return; |
| 9084 | |
| 9085 | scanForTargetRegionsFunctions( |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 9086 | E->getInnermostCapturedStmt()->getCapturedStmt(), ParentName); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 9087 | return; |
| 9088 | } |
| 9089 | |
| 9090 | // If this is a lambda function, look into its body. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 9091 | if (const auto *L = dyn_cast<LambdaExpr>(S)) |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 9092 | S = L->getBody(); |
| 9093 | |
| 9094 | // Keep looking for target regions recursively. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 9095 | for (const Stmt *II : S->children()) |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 9096 | scanForTargetRegionsFunctions(II, ParentName); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 9097 | } |
| 9098 | |
| 9099 | bool CGOpenMPRuntime::emitTargetFunctions(GlobalDecl GD) { |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 9100 | // If emitting code for the host, we do not process FD here. Instead we do |
| 9101 | // the normal code generation. |
| 9102 | if (!CGM.getLangOpts().OpenMPIsDevice) |
| 9103 | return false; |
| 9104 | |
Alexey Bataev | e6aa469 | 2018-09-13 16:54:05 +0000 | [diff] [blame] | 9105 | const ValueDecl *VD = cast<ValueDecl>(GD.getDecl()); |
Alexey Bataev | 2a6f3f5 | 2018-11-07 19:11:14 +0000 | [diff] [blame] | 9106 | StringRef Name = CGM.getMangledName(GD); |
| 9107 | // Try to detect target regions in the function. |
Alexey Bataev | e6aa469 | 2018-09-13 16:54:05 +0000 | [diff] [blame] | 9108 | if (const auto *FD = dyn_cast<FunctionDecl>(VD)) |
Alexey Bataev | 2a6f3f5 | 2018-11-07 19:11:14 +0000 | [diff] [blame] | 9109 | scanForTargetRegionsFunctions(FD->getBody(), Name); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 9110 | |
Alexey Bataev | 4f4bf7c | 2018-03-15 15:47:20 +0000 | [diff] [blame] | 9111 | // Do not to emit function if it is not marked as declare target. |
Alexey Bataev | e6aa469 | 2018-09-13 16:54:05 +0000 | [diff] [blame] | 9112 | return !OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD) && |
Alexey Bataev | 2a6f3f5 | 2018-11-07 19:11:14 +0000 | [diff] [blame] | 9113 | AlreadyEmittedTargetFunctions.count(Name) == 0; |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 9114 | } |
| 9115 | |
| 9116 | bool CGOpenMPRuntime::emitTargetGlobalVariable(GlobalDecl GD) { |
| 9117 | if (!CGM.getLangOpts().OpenMPIsDevice) |
| 9118 | return false; |
| 9119 | |
| 9120 | // Check if there are Ctors/Dtors in this declaration and look for target |
| 9121 | // regions in it. We use the complete variant to produce the kernel name |
| 9122 | // mangling. |
| 9123 | QualType RDTy = cast<VarDecl>(GD.getDecl())->getType(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 9124 | if (const auto *RD = RDTy->getBaseElementTypeUnsafe()->getAsCXXRecordDecl()) { |
| 9125 | for (const CXXConstructorDecl *Ctor : RD->ctors()) { |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 9126 | StringRef ParentName = |
| 9127 | CGM.getMangledName(GlobalDecl(Ctor, Ctor_Complete)); |
| 9128 | scanForTargetRegionsFunctions(Ctor->getBody(), ParentName); |
| 9129 | } |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 9130 | if (const CXXDestructorDecl *Dtor = RD->getDestructor()) { |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 9131 | StringRef ParentName = |
| 9132 | CGM.getMangledName(GlobalDecl(Dtor, Dtor_Complete)); |
| 9133 | scanForTargetRegionsFunctions(Dtor->getBody(), ParentName); |
| 9134 | } |
| 9135 | } |
| 9136 | |
Alexey Bataev | 4f4bf7c | 2018-03-15 15:47:20 +0000 | [diff] [blame] | 9137 | // 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] | 9138 | llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res = |
Alexey Bataev | 97b7221 | 2018-08-14 18:31:20 +0000 | [diff] [blame] | 9139 | OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration( |
| 9140 | cast<VarDecl>(GD.getDecl())); |
Gheorghe-Teodor Bercea | 0034e84 | 2019-06-20 18:04:47 +0000 | [diff] [blame] | 9141 | if (!Res || *Res == OMPDeclareTargetDeclAttr::MT_Link || |
| 9142 | (*Res == OMPDeclareTargetDeclAttr::MT_To && |
| 9143 | HasRequiresUnifiedSharedMemory)) { |
Alexey Bataev | d01b749 | 2018-08-15 19:45:12 +0000 | [diff] [blame] | 9144 | DeferredGlobalVariables.insert(cast<VarDecl>(GD.getDecl())); |
Alexey Bataev | bf8fe71 | 2018-08-07 16:14:36 +0000 | [diff] [blame] | 9145 | return true; |
| 9146 | } |
| 9147 | return false; |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 9148 | } |
| 9149 | |
Alexey Bataev | 1af5bd5 | 2019-03-05 17:47:18 +0000 | [diff] [blame] | 9150 | llvm::Constant * |
| 9151 | CGOpenMPRuntime::registerTargetFirstprivateCopy(CodeGenFunction &CGF, |
| 9152 | const VarDecl *VD) { |
| 9153 | assert(VD->getType().isConstant(CGM.getContext()) && |
| 9154 | "Expected constant variable."); |
| 9155 | StringRef VarName; |
| 9156 | llvm::Constant *Addr; |
| 9157 | llvm::GlobalValue::LinkageTypes Linkage; |
| 9158 | QualType Ty = VD->getType(); |
| 9159 | SmallString<128> Buffer; |
| 9160 | { |
| 9161 | unsigned DeviceID; |
| 9162 | unsigned FileID; |
| 9163 | unsigned Line; |
| 9164 | getTargetEntryUniqueInfo(CGM.getContext(), VD->getLocation(), DeviceID, |
| 9165 | FileID, Line); |
| 9166 | llvm::raw_svector_ostream OS(Buffer); |
| 9167 | OS << "__omp_offloading_firstprivate_" << llvm::format("_%x", DeviceID) |
| 9168 | << llvm::format("_%x_", FileID) << VD->getName() << "_l" << Line; |
| 9169 | VarName = OS.str(); |
| 9170 | } |
| 9171 | Linkage = llvm::GlobalValue::InternalLinkage; |
| 9172 | Addr = |
| 9173 | getOrCreateInternalVariable(CGM.getTypes().ConvertTypeForMem(Ty), VarName, |
| 9174 | getDefaultFirstprivateAddressSpace()); |
| 9175 | cast<llvm::GlobalValue>(Addr)->setLinkage(Linkage); |
| 9176 | CharUnits VarSize = CGM.getContext().getTypeSizeInChars(Ty); |
| 9177 | CGM.addCompilerUsedGlobal(cast<llvm::GlobalValue>(Addr)); |
| 9178 | OffloadEntriesInfoManager.registerDeviceGlobalVarEntryInfo( |
| 9179 | VarName, Addr, VarSize, |
| 9180 | OffloadEntriesInfoManagerTy::OMPTargetGlobalVarEntryTo, Linkage); |
| 9181 | return Addr; |
| 9182 | } |
| 9183 | |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 9184 | void CGOpenMPRuntime::registerTargetGlobalVariable(const VarDecl *VD, |
| 9185 | llvm::Constant *Addr) { |
Alexey Bataev | 2a6f3f5 | 2018-11-07 19:11:14 +0000 | [diff] [blame] | 9186 | llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res = |
| 9187 | OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD); |
| 9188 | if (!Res) { |
| 9189 | if (CGM.getLangOpts().OpenMPIsDevice) { |
| 9190 | // Register non-target variables being emitted in device code (debug info |
| 9191 | // may cause this). |
| 9192 | StringRef VarName = CGM.getMangledName(VD); |
| 9193 | EmittedNonTargetVariables.try_emplace(VarName, Addr); |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 9194 | } |
Alexey Bataev | 2a6f3f5 | 2018-11-07 19:11:14 +0000 | [diff] [blame] | 9195 | return; |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 9196 | } |
Alexey Bataev | 2a6f3f5 | 2018-11-07 19:11:14 +0000 | [diff] [blame] | 9197 | // Register declare target variables. |
| 9198 | OffloadEntriesInfoManagerTy::OMPTargetGlobalVarEntryKind Flags; |
| 9199 | StringRef VarName; |
| 9200 | CharUnits VarSize; |
| 9201 | llvm::GlobalValue::LinkageTypes Linkage; |
Gheorghe-Teodor Bercea | 0034e84 | 2019-06-20 18:04:47 +0000 | [diff] [blame] | 9202 | |
| 9203 | if (*Res == OMPDeclareTargetDeclAttr::MT_To && |
| 9204 | !HasRequiresUnifiedSharedMemory) { |
Alexey Bataev | 2a6f3f5 | 2018-11-07 19:11:14 +0000 | [diff] [blame] | 9205 | Flags = OffloadEntriesInfoManagerTy::OMPTargetGlobalVarEntryTo; |
| 9206 | VarName = CGM.getMangledName(VD); |
| 9207 | if (VD->hasDefinition(CGM.getContext()) != VarDecl::DeclarationOnly) { |
| 9208 | VarSize = CGM.getContext().getTypeSizeInChars(VD->getType()); |
| 9209 | assert(!VarSize.isZero() && "Expected non-zero size of the variable"); |
| 9210 | } else { |
| 9211 | VarSize = CharUnits::Zero(); |
| 9212 | } |
| 9213 | Linkage = CGM.getLLVMLinkageVarDefinition(VD, /*IsConstant=*/false); |
| 9214 | // Temp solution to prevent optimizations of the internal variables. |
| 9215 | if (CGM.getLangOpts().OpenMPIsDevice && !VD->isExternallyVisible()) { |
| 9216 | std::string RefName = getName({VarName, "ref"}); |
| 9217 | if (!CGM.GetGlobalValue(RefName)) { |
| 9218 | llvm::Constant *AddrRef = |
| 9219 | getOrCreateInternalVariable(Addr->getType(), RefName); |
| 9220 | auto *GVAddrRef = cast<llvm::GlobalVariable>(AddrRef); |
| 9221 | GVAddrRef->setConstant(/*Val=*/true); |
| 9222 | GVAddrRef->setLinkage(llvm::GlobalValue::InternalLinkage); |
| 9223 | GVAddrRef->setInitializer(Addr); |
| 9224 | CGM.addCompilerUsedGlobal(GVAddrRef); |
| 9225 | } |
| 9226 | } |
Gheorghe-Teodor Bercea | 0034e84 | 2019-06-20 18:04:47 +0000 | [diff] [blame] | 9227 | } else { |
| 9228 | assert(((*Res == OMPDeclareTargetDeclAttr::MT_Link) || |
| 9229 | (*Res == OMPDeclareTargetDeclAttr::MT_To && |
| 9230 | HasRequiresUnifiedSharedMemory)) && |
| 9231 | "Declare target attribute must link or to with unified memory."); |
| 9232 | if (*Res == OMPDeclareTargetDeclAttr::MT_Link) |
| 9233 | Flags = OffloadEntriesInfoManagerTy::OMPTargetGlobalVarEntryLink; |
| 9234 | else |
| 9235 | Flags = OffloadEntriesInfoManagerTy::OMPTargetGlobalVarEntryTo; |
| 9236 | |
Alexey Bataev | 2a6f3f5 | 2018-11-07 19:11:14 +0000 | [diff] [blame] | 9237 | if (CGM.getLangOpts().OpenMPIsDevice) { |
| 9238 | VarName = Addr->getName(); |
| 9239 | Addr = nullptr; |
| 9240 | } else { |
Gheorghe-Teodor Bercea | 0034e84 | 2019-06-20 18:04:47 +0000 | [diff] [blame] | 9241 | VarName = getAddrOfDeclareTargetVar(VD).getName(); |
| 9242 | Addr = cast<llvm::Constant>(getAddrOfDeclareTargetVar(VD).getPointer()); |
Alexey Bataev | 2a6f3f5 | 2018-11-07 19:11:14 +0000 | [diff] [blame] | 9243 | } |
| 9244 | VarSize = CGM.getPointerSize(); |
| 9245 | Linkage = llvm::GlobalValue::WeakAnyLinkage; |
Alexey Bataev | 2a6f3f5 | 2018-11-07 19:11:14 +0000 | [diff] [blame] | 9246 | } |
Gheorghe-Teodor Bercea | 0034e84 | 2019-06-20 18:04:47 +0000 | [diff] [blame] | 9247 | |
Alexey Bataev | 2a6f3f5 | 2018-11-07 19:11:14 +0000 | [diff] [blame] | 9248 | OffloadEntriesInfoManager.registerDeviceGlobalVarEntryInfo( |
| 9249 | VarName, Addr, VarSize, Flags, Linkage); |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 9250 | } |
| 9251 | |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 9252 | bool CGOpenMPRuntime::emitTargetGlobal(GlobalDecl GD) { |
Alexey Bataev | e6aa469 | 2018-09-13 16:54:05 +0000 | [diff] [blame] | 9253 | if (isa<FunctionDecl>(GD.getDecl()) || |
| 9254 | isa<OMPDeclareReductionDecl>(GD.getDecl())) |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 9255 | return emitTargetFunctions(GD); |
| 9256 | |
| 9257 | return emitTargetGlobalVariable(GD); |
| 9258 | } |
| 9259 | |
Alexey Bataev | bf8fe71 | 2018-08-07 16:14:36 +0000 | [diff] [blame] | 9260 | void CGOpenMPRuntime::emitDeferredTargetDecls() const { |
| 9261 | for (const VarDecl *VD : DeferredGlobalVariables) { |
| 9262 | llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res = |
Alexey Bataev | 97b7221 | 2018-08-14 18:31:20 +0000 | [diff] [blame] | 9263 | OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD); |
Alexey Bataev | d01b749 | 2018-08-15 19:45:12 +0000 | [diff] [blame] | 9264 | if (!Res) |
| 9265 | continue; |
Gheorghe-Teodor Bercea | 0034e84 | 2019-06-20 18:04:47 +0000 | [diff] [blame] | 9266 | if (*Res == OMPDeclareTargetDeclAttr::MT_To && |
| 9267 | !HasRequiresUnifiedSharedMemory) { |
Alexey Bataev | bf8fe71 | 2018-08-07 16:14:36 +0000 | [diff] [blame] | 9268 | CGM.EmitGlobal(VD); |
Alexey Bataev | d01b749 | 2018-08-15 19:45:12 +0000 | [diff] [blame] | 9269 | } else { |
Gheorghe-Teodor Bercea | 0034e84 | 2019-06-20 18:04:47 +0000 | [diff] [blame] | 9270 | assert((*Res == OMPDeclareTargetDeclAttr::MT_Link || |
| 9271 | (*Res == OMPDeclareTargetDeclAttr::MT_To && |
| 9272 | HasRequiresUnifiedSharedMemory)) && |
| 9273 | "Expected link clause or to clause with unified memory."); |
| 9274 | (void)CGM.getOpenMPRuntime().getAddrOfDeclareTargetVar(VD); |
Alexey Bataev | bf8fe71 | 2018-08-07 16:14:36 +0000 | [diff] [blame] | 9275 | } |
| 9276 | } |
| 9277 | } |
| 9278 | |
Alexey Bataev | 6070542 | 2018-10-30 15:50:12 +0000 | [diff] [blame] | 9279 | void CGOpenMPRuntime::adjustTargetSpecificDataForLambdas( |
| 9280 | CodeGenFunction &CGF, const OMPExecutableDirective &D) const { |
| 9281 | assert(isOpenMPTargetExecutionDirective(D.getDirectiveKind()) && |
| 9282 | " Expected target-based directive."); |
| 9283 | } |
| 9284 | |
Gheorghe-Teodor Bercea | 66cdbb47 | 2019-05-21 19:42:01 +0000 | [diff] [blame] | 9285 | void CGOpenMPRuntime::checkArchForUnifiedAddressing( |
| 9286 | const OMPRequiresDecl *D) { |
| 9287 | for (const OMPClause *Clause : D->clauselists()) { |
| 9288 | if (Clause->getClauseKind() == OMPC_unified_shared_memory) { |
| 9289 | HasRequiresUnifiedSharedMemory = true; |
| 9290 | break; |
| 9291 | } |
| 9292 | } |
| 9293 | } |
| 9294 | |
Alexey Bataev | c568725 | 2019-03-21 19:35:27 +0000 | [diff] [blame] | 9295 | bool CGOpenMPRuntime::hasAllocateAttributeForGlobalVar(const VarDecl *VD, |
| 9296 | LangAS &AS) { |
| 9297 | if (!VD || !VD->hasAttr<OMPAllocateDeclAttr>()) |
| 9298 | return false; |
| 9299 | const auto *A = VD->getAttr<OMPAllocateDeclAttr>(); |
| 9300 | switch(A->getAllocatorType()) { |
| 9301 | case OMPAllocateDeclAttr::OMPDefaultMemAlloc: |
| 9302 | // Not supported, fallback to the default mem space. |
| 9303 | case OMPAllocateDeclAttr::OMPLargeCapMemAlloc: |
| 9304 | case OMPAllocateDeclAttr::OMPCGroupMemAlloc: |
| 9305 | case OMPAllocateDeclAttr::OMPHighBWMemAlloc: |
| 9306 | case OMPAllocateDeclAttr::OMPLowLatMemAlloc: |
| 9307 | case OMPAllocateDeclAttr::OMPThreadMemAlloc: |
| 9308 | case OMPAllocateDeclAttr::OMPConstMemAlloc: |
| 9309 | case OMPAllocateDeclAttr::OMPPTeamMemAlloc: |
| 9310 | AS = LangAS::Default; |
| 9311 | return true; |
| 9312 | case OMPAllocateDeclAttr::OMPUserDefinedMemAlloc: |
| 9313 | llvm_unreachable("Expected predefined allocator for the variables with the " |
| 9314 | "static storage."); |
| 9315 | } |
| 9316 | return false; |
| 9317 | } |
| 9318 | |
Gheorghe-Teodor Bercea | 5254f0a | 2019-06-14 17:58:26 +0000 | [diff] [blame] | 9319 | bool CGOpenMPRuntime::hasRequiresUnifiedSharedMemory() const { |
| 9320 | return HasRequiresUnifiedSharedMemory; |
| 9321 | } |
| 9322 | |
Alexey Bataev | 4f4bf7c | 2018-03-15 15:47:20 +0000 | [diff] [blame] | 9323 | CGOpenMPRuntime::DisableAutoDeclareTargetRAII::DisableAutoDeclareTargetRAII( |
| 9324 | CodeGenModule &CGM) |
| 9325 | : CGM(CGM) { |
| 9326 | if (CGM.getLangOpts().OpenMPIsDevice) { |
| 9327 | SavedShouldMarkAsGlobal = CGM.getOpenMPRuntime().ShouldMarkAsGlobal; |
| 9328 | CGM.getOpenMPRuntime().ShouldMarkAsGlobal = false; |
| 9329 | } |
| 9330 | } |
| 9331 | |
| 9332 | CGOpenMPRuntime::DisableAutoDeclareTargetRAII::~DisableAutoDeclareTargetRAII() { |
| 9333 | if (CGM.getLangOpts().OpenMPIsDevice) |
| 9334 | CGM.getOpenMPRuntime().ShouldMarkAsGlobal = SavedShouldMarkAsGlobal; |
| 9335 | } |
| 9336 | |
Alexey Bataev | 6d94410 | 2018-05-02 15:45:28 +0000 | [diff] [blame] | 9337 | bool CGOpenMPRuntime::markAsGlobalTarget(GlobalDecl GD) { |
Alexey Bataev | 4f4bf7c | 2018-03-15 15:47:20 +0000 | [diff] [blame] | 9338 | if (!CGM.getLangOpts().OpenMPIsDevice || !ShouldMarkAsGlobal) |
| 9339 | return true; |
Alexey Bataev | 4f4bf7c | 2018-03-15 15:47:20 +0000 | [diff] [blame] | 9340 | |
Alexey Bataev | 2a6f3f5 | 2018-11-07 19:11:14 +0000 | [diff] [blame] | 9341 | StringRef Name = CGM.getMangledName(GD); |
Alexey Bataev | 6d94410 | 2018-05-02 15:45:28 +0000 | [diff] [blame] | 9342 | const auto *D = cast<FunctionDecl>(GD.getDecl()); |
Alexey Bataev | 34f8a70 | 2018-03-28 14:28:54 +0000 | [diff] [blame] | 9343 | // Do not to emit function if it is marked as declare target as it was already |
| 9344 | // emitted. |
Alexey Bataev | 97b7221 | 2018-08-14 18:31:20 +0000 | [diff] [blame] | 9345 | if (OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(D)) { |
Alexey Bataev | 2a6f3f5 | 2018-11-07 19:11:14 +0000 | [diff] [blame] | 9346 | if (D->hasBody() && AlreadyEmittedTargetFunctions.count(Name) == 0) { |
| 9347 | if (auto *F = dyn_cast_or_null<llvm::Function>(CGM.GetGlobalValue(Name))) |
Alexey Bataev | 34f8a70 | 2018-03-28 14:28:54 +0000 | [diff] [blame] | 9348 | return !F->isDeclaration(); |
| 9349 | return false; |
| 9350 | } |
| 9351 | return true; |
| 9352 | } |
| 9353 | |
Alexey Bataev | 2a6f3f5 | 2018-11-07 19:11:14 +0000 | [diff] [blame] | 9354 | return !AlreadyEmittedTargetFunctions.insert(Name).second; |
Alexey Bataev | 4f4bf7c | 2018-03-15 15:47:20 +0000 | [diff] [blame] | 9355 | } |
| 9356 | |
Gheorghe-Teodor Bercea | 66cdbb47 | 2019-05-21 19:42:01 +0000 | [diff] [blame] | 9357 | llvm::Function *CGOpenMPRuntime::emitRequiresDirectiveRegFun() { |
| 9358 | // If we don't have entries or if we are emitting code for the device, we |
| 9359 | // don't need to do anything. |
| 9360 | if (CGM.getLangOpts().OMPTargetTriples.empty() || |
| 9361 | CGM.getLangOpts().OpenMPSimd || CGM.getLangOpts().OpenMPIsDevice || |
| 9362 | (OffloadEntriesInfoManager.empty() && |
| 9363 | !HasEmittedDeclareTargetRegion && |
| 9364 | !HasEmittedTargetRegion)) |
| 9365 | return nullptr; |
| 9366 | |
| 9367 | // Create and register the function that handles the requires directives. |
| 9368 | ASTContext &C = CGM.getContext(); |
| 9369 | |
| 9370 | llvm::Function *RequiresRegFn; |
| 9371 | { |
| 9372 | CodeGenFunction CGF(CGM); |
| 9373 | const auto &FI = CGM.getTypes().arrangeNullaryFunction(); |
| 9374 | llvm::FunctionType *FTy = CGM.getTypes().GetFunctionType(FI); |
| 9375 | std::string ReqName = getName({"omp_offloading", "requires_reg"}); |
| 9376 | RequiresRegFn = CGM.CreateGlobalInitOrDestructFunction(FTy, ReqName, FI); |
| 9377 | CGF.StartFunction(GlobalDecl(), C.VoidTy, RequiresRegFn, FI, {}); |
| 9378 | OpenMPOffloadingRequiresDirFlags Flags = OMP_REQ_NONE; |
| 9379 | // TODO: check for other requires clauses. |
| 9380 | // The requires directive takes effect only when a target region is |
| 9381 | // present in the compilation unit. Otherwise it is ignored and not |
| 9382 | // passed to the runtime. This avoids the runtime from throwing an error |
| 9383 | // for mismatching requires clauses across compilation units that don't |
| 9384 | // contain at least 1 target region. |
| 9385 | assert((HasEmittedTargetRegion || |
| 9386 | HasEmittedDeclareTargetRegion || |
| 9387 | !OffloadEntriesInfoManager.empty()) && |
| 9388 | "Target or declare target region expected."); |
| 9389 | if (HasRequiresUnifiedSharedMemory) |
| 9390 | Flags = OMP_REQ_UNIFIED_SHARED_MEMORY; |
| 9391 | CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__tgt_register_requires), |
| 9392 | llvm::ConstantInt::get(CGM.Int64Ty, Flags)); |
| 9393 | CGF.FinishFunction(); |
| 9394 | } |
| 9395 | return RequiresRegFn; |
| 9396 | } |
| 9397 | |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 9398 | llvm::Function *CGOpenMPRuntime::emitRegistrationFunction() { |
| 9399 | // If we have offloading in the current module, we need to emit the entries |
| 9400 | // now and register the offloading descriptor. |
| 9401 | createOffloadEntriesAndInfoMetadata(); |
| 9402 | |
| 9403 | // Create and register the offloading binary descriptors. This is the main |
| 9404 | // entity that captures all the information about offloading in the current |
| 9405 | // compilation unit. |
| 9406 | return createOffloadingBinaryDescriptorRegistration(); |
| 9407 | } |
Carlo Bertolli | 430d8ec | 2016-03-03 20:34:23 +0000 | [diff] [blame] | 9408 | |
| 9409 | void CGOpenMPRuntime::emitTeamsCall(CodeGenFunction &CGF, |
| 9410 | const OMPExecutableDirective &D, |
| 9411 | SourceLocation Loc, |
James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 9412 | llvm::Function *OutlinedFn, |
Carlo Bertolli | 430d8ec | 2016-03-03 20:34:23 +0000 | [diff] [blame] | 9413 | ArrayRef<llvm::Value *> CapturedVars) { |
| 9414 | if (!CGF.HaveInsertPoint()) |
| 9415 | return; |
| 9416 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 9417 | llvm::Value *RTLoc = emitUpdateLocation(CGF, Loc); |
Carlo Bertolli | 430d8ec | 2016-03-03 20:34:23 +0000 | [diff] [blame] | 9418 | CodeGenFunction::RunCleanupsScope Scope(CGF); |
| 9419 | |
| 9420 | // Build call __kmpc_fork_teams(loc, n, microtask, var1, .., varn); |
| 9421 | llvm::Value *Args[] = { |
| 9422 | RTLoc, |
| 9423 | CGF.Builder.getInt32(CapturedVars.size()), // Number of captured vars |
| 9424 | CGF.Builder.CreateBitCast(OutlinedFn, getKmpc_MicroPointerTy())}; |
| 9425 | llvm::SmallVector<llvm::Value *, 16> RealArgs; |
| 9426 | RealArgs.append(std::begin(Args), std::end(Args)); |
| 9427 | RealArgs.append(CapturedVars.begin(), CapturedVars.end()); |
| 9428 | |
James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 9429 | llvm::FunctionCallee RTLFn = createRuntimeFunction(OMPRTL__kmpc_fork_teams); |
Carlo Bertolli | 430d8ec | 2016-03-03 20:34:23 +0000 | [diff] [blame] | 9430 | CGF.EmitRuntimeCall(RTLFn, RealArgs); |
| 9431 | } |
| 9432 | |
| 9433 | void CGOpenMPRuntime::emitNumTeamsClause(CodeGenFunction &CGF, |
Carlo Bertolli | c687225 | 2016-04-04 15:55:02 +0000 | [diff] [blame] | 9434 | const Expr *NumTeams, |
| 9435 | const Expr *ThreadLimit, |
Carlo Bertolli | 430d8ec | 2016-03-03 20:34:23 +0000 | [diff] [blame] | 9436 | SourceLocation Loc) { |
| 9437 | if (!CGF.HaveInsertPoint()) |
| 9438 | return; |
| 9439 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 9440 | llvm::Value *RTLoc = emitUpdateLocation(CGF, Loc); |
Carlo Bertolli | 430d8ec | 2016-03-03 20:34:23 +0000 | [diff] [blame] | 9441 | |
Carlo Bertolli | c687225 | 2016-04-04 15:55:02 +0000 | [diff] [blame] | 9442 | llvm::Value *NumTeamsVal = |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 9443 | NumTeams |
Carlo Bertolli | c687225 | 2016-04-04 15:55:02 +0000 | [diff] [blame] | 9444 | ? CGF.Builder.CreateIntCast(CGF.EmitScalarExpr(NumTeams), |
| 9445 | CGF.CGM.Int32Ty, /* isSigned = */ true) |
| 9446 | : CGF.Builder.getInt32(0); |
| 9447 | |
| 9448 | llvm::Value *ThreadLimitVal = |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 9449 | ThreadLimit |
Carlo Bertolli | c687225 | 2016-04-04 15:55:02 +0000 | [diff] [blame] | 9450 | ? CGF.Builder.CreateIntCast(CGF.EmitScalarExpr(ThreadLimit), |
| 9451 | CGF.CGM.Int32Ty, /* isSigned = */ true) |
| 9452 | : CGF.Builder.getInt32(0); |
| 9453 | |
Carlo Bertolli | 430d8ec | 2016-03-03 20:34:23 +0000 | [diff] [blame] | 9454 | // 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] | 9455 | llvm::Value *PushNumTeamsArgs[] = {RTLoc, getThreadID(CGF, Loc), NumTeamsVal, |
| 9456 | ThreadLimitVal}; |
Carlo Bertolli | 430d8ec | 2016-03-03 20:34:23 +0000 | [diff] [blame] | 9457 | CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_push_num_teams), |
| 9458 | PushNumTeamsArgs); |
| 9459 | } |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 9460 | |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 9461 | void CGOpenMPRuntime::emitTargetDataCalls( |
| 9462 | CodeGenFunction &CGF, const OMPExecutableDirective &D, const Expr *IfCond, |
| 9463 | const Expr *Device, const RegionCodeGenTy &CodeGen, TargetDataInfo &Info) { |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 9464 | if (!CGF.HaveInsertPoint()) |
| 9465 | return; |
| 9466 | |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 9467 | // Action used to replace the default codegen action and turn privatization |
| 9468 | // off. |
| 9469 | PrePostActionTy NoPrivAction; |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 9470 | |
| 9471 | // Generate the code for the opening of the data environment. Capture all the |
| 9472 | // arguments of the runtime call by reference because they are used in the |
| 9473 | // closing of the region. |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 9474 | auto &&BeginThenGen = [this, &D, Device, &Info, |
| 9475 | &CodeGen](CodeGenFunction &CGF, PrePostActionTy &) { |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 9476 | // Fill up the arrays with all the mapped variables. |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 9477 | MappableExprsHandler::MapBaseValuesArrayTy BasePointers; |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 9478 | MappableExprsHandler::MapValuesArrayTy Pointers; |
| 9479 | MappableExprsHandler::MapValuesArrayTy Sizes; |
| 9480 | MappableExprsHandler::MapFlagsArrayTy MapTypes; |
| 9481 | |
| 9482 | // Get map clause information. |
| 9483 | MappableExprsHandler MCHandler(D, CGF); |
| 9484 | MCHandler.generateAllInfo(BasePointers, Pointers, Sizes, MapTypes); |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 9485 | |
| 9486 | // Fill up the arrays and create the arguments. |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 9487 | emitOffloadingArrays(CGF, BasePointers, Pointers, Sizes, MapTypes, Info); |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 9488 | |
| 9489 | llvm::Value *BasePointersArrayArg = nullptr; |
| 9490 | llvm::Value *PointersArrayArg = nullptr; |
| 9491 | llvm::Value *SizesArrayArg = nullptr; |
| 9492 | llvm::Value *MapTypesArrayArg = nullptr; |
| 9493 | emitOffloadingArraysArgument(CGF, BasePointersArrayArg, PointersArrayArg, |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 9494 | SizesArrayArg, MapTypesArrayArg, Info); |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 9495 | |
| 9496 | // Emit device ID if any. |
| 9497 | llvm::Value *DeviceID = nullptr; |
George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 9498 | if (Device) { |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 9499 | DeviceID = CGF.Builder.CreateIntCast(CGF.EmitScalarExpr(Device), |
George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 9500 | CGF.Int64Ty, /*isSigned=*/true); |
| 9501 | } else { |
| 9502 | DeviceID = CGF.Builder.getInt64(OMP_DEVICEID_UNDEF); |
| 9503 | } |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 9504 | |
| 9505 | // Emit the number of elements in the offloading arrays. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 9506 | llvm::Value *PointerNum = CGF.Builder.getInt32(Info.NumberOfPtrs); |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 9507 | |
| 9508 | llvm::Value *OffloadingArgs[] = { |
| 9509 | DeviceID, PointerNum, BasePointersArrayArg, |
| 9510 | PointersArrayArg, SizesArrayArg, MapTypesArrayArg}; |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 9511 | CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__tgt_target_data_begin), |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 9512 | OffloadingArgs); |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 9513 | |
| 9514 | // If device pointer privatization is required, emit the body of the region |
| 9515 | // here. It will have to be duplicated: with and without privatization. |
| 9516 | if (!Info.CaptureDeviceAddrMap.empty()) |
| 9517 | CodeGen(CGF); |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 9518 | }; |
| 9519 | |
| 9520 | // Generate code for the closing of the data region. |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 9521 | auto &&EndThenGen = [this, Device, &Info](CodeGenFunction &CGF, |
| 9522 | PrePostActionTy &) { |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 9523 | assert(Info.isValid() && "Invalid data environment closing arguments."); |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 9524 | |
| 9525 | llvm::Value *BasePointersArrayArg = nullptr; |
| 9526 | llvm::Value *PointersArrayArg = nullptr; |
| 9527 | llvm::Value *SizesArrayArg = nullptr; |
| 9528 | llvm::Value *MapTypesArrayArg = nullptr; |
| 9529 | emitOffloadingArraysArgument(CGF, BasePointersArrayArg, PointersArrayArg, |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 9530 | SizesArrayArg, MapTypesArrayArg, Info); |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 9531 | |
| 9532 | // Emit device ID if any. |
| 9533 | llvm::Value *DeviceID = nullptr; |
George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 9534 | if (Device) { |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 9535 | DeviceID = CGF.Builder.CreateIntCast(CGF.EmitScalarExpr(Device), |
George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 9536 | CGF.Int64Ty, /*isSigned=*/true); |
| 9537 | } else { |
| 9538 | DeviceID = CGF.Builder.getInt64(OMP_DEVICEID_UNDEF); |
| 9539 | } |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 9540 | |
| 9541 | // Emit the number of elements in the offloading arrays. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 9542 | llvm::Value *PointerNum = CGF.Builder.getInt32(Info.NumberOfPtrs); |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 9543 | |
| 9544 | llvm::Value *OffloadingArgs[] = { |
| 9545 | DeviceID, PointerNum, BasePointersArrayArg, |
| 9546 | PointersArrayArg, SizesArrayArg, MapTypesArrayArg}; |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 9547 | CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__tgt_target_data_end), |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 9548 | OffloadingArgs); |
| 9549 | }; |
| 9550 | |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 9551 | // If we need device pointer privatization, we need to emit the body of the |
| 9552 | // region with no privatization in the 'else' branch of the conditional. |
| 9553 | // Otherwise, we don't have to do anything. |
| 9554 | auto &&BeginElseGen = [&Info, &CodeGen, &NoPrivAction](CodeGenFunction &CGF, |
| 9555 | PrePostActionTy &) { |
| 9556 | if (!Info.CaptureDeviceAddrMap.empty()) { |
| 9557 | CodeGen.setAction(NoPrivAction); |
| 9558 | CodeGen(CGF); |
| 9559 | } |
| 9560 | }; |
| 9561 | |
| 9562 | // We don't have to do anything to close the region if the if clause evaluates |
| 9563 | // to false. |
| 9564 | auto &&EndElseGen = [](CodeGenFunction &CGF, PrePostActionTy &) {}; |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 9565 | |
| 9566 | if (IfCond) { |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 9567 | emitOMPIfClause(CGF, IfCond, BeginThenGen, BeginElseGen); |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 9568 | } else { |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 9569 | RegionCodeGenTy RCG(BeginThenGen); |
| 9570 | RCG(CGF); |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 9571 | } |
| 9572 | |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 9573 | // If we don't require privatization of device pointers, we emit the body in |
| 9574 | // between the runtime calls. This avoids duplicating the body code. |
| 9575 | if (Info.CaptureDeviceAddrMap.empty()) { |
| 9576 | CodeGen.setAction(NoPrivAction); |
| 9577 | CodeGen(CGF); |
| 9578 | } |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 9579 | |
| 9580 | if (IfCond) { |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 9581 | emitOMPIfClause(CGF, IfCond, EndThenGen, EndElseGen); |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 9582 | } else { |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 9583 | RegionCodeGenTy RCG(EndThenGen); |
| 9584 | RCG(CGF); |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 9585 | } |
| 9586 | } |
Samuel Antao | bd0ae2e | 2016-04-27 23:07:29 +0000 | [diff] [blame] | 9587 | |
Samuel Antao | 8d2d730 | 2016-05-26 18:30:22 +0000 | [diff] [blame] | 9588 | void CGOpenMPRuntime::emitTargetDataStandAloneCall( |
Samuel Antao | 8dd6628 | 2016-04-27 23:14:30 +0000 | [diff] [blame] | 9589 | CodeGenFunction &CGF, const OMPExecutableDirective &D, const Expr *IfCond, |
| 9590 | const Expr *Device) { |
Samuel Antao | bd0ae2e | 2016-04-27 23:07:29 +0000 | [diff] [blame] | 9591 | if (!CGF.HaveInsertPoint()) |
| 9592 | return; |
| 9593 | |
Samuel Antao | 8dd6628 | 2016-04-27 23:14:30 +0000 | [diff] [blame] | 9594 | assert((isa<OMPTargetEnterDataDirective>(D) || |
Samuel Antao | 8d2d730 | 2016-05-26 18:30:22 +0000 | [diff] [blame] | 9595 | isa<OMPTargetExitDataDirective>(D) || |
| 9596 | isa<OMPTargetUpdateDirective>(D)) && |
| 9597 | "Expecting either target enter, exit data, or update directives."); |
Samuel Antao | 8dd6628 | 2016-04-27 23:14:30 +0000 | [diff] [blame] | 9598 | |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 9599 | CodeGenFunction::OMPTargetDataInfo InputInfo; |
| 9600 | llvm::Value *MapTypesArray = nullptr; |
Samuel Antao | bd0ae2e | 2016-04-27 23:07:29 +0000 | [diff] [blame] | 9601 | // Generate the code for the opening of the data environment. |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 9602 | auto &&ThenGen = [this, &D, Device, &InputInfo, |
| 9603 | &MapTypesArray](CodeGenFunction &CGF, PrePostActionTy &) { |
Samuel Antao | bd0ae2e | 2016-04-27 23:07:29 +0000 | [diff] [blame] | 9604 | // Emit device ID if any. |
| 9605 | llvm::Value *DeviceID = nullptr; |
George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 9606 | if (Device) { |
Samuel Antao | bd0ae2e | 2016-04-27 23:07:29 +0000 | [diff] [blame] | 9607 | DeviceID = CGF.Builder.CreateIntCast(CGF.EmitScalarExpr(Device), |
George Rokos | 63bc9d6 | 2017-11-21 18:25:12 +0000 | [diff] [blame] | 9608 | CGF.Int64Ty, /*isSigned=*/true); |
| 9609 | } else { |
| 9610 | DeviceID = CGF.Builder.getInt64(OMP_DEVICEID_UNDEF); |
| 9611 | } |
Samuel Antao | bd0ae2e | 2016-04-27 23:07:29 +0000 | [diff] [blame] | 9612 | |
| 9613 | // Emit the number of elements in the offloading arrays. |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 9614 | llvm::Constant *PointerNum = |
| 9615 | CGF.Builder.getInt32(InputInfo.NumberOfTargetItems); |
Samuel Antao | bd0ae2e | 2016-04-27 23:07:29 +0000 | [diff] [blame] | 9616 | |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 9617 | llvm::Value *OffloadingArgs[] = {DeviceID, |
| 9618 | PointerNum, |
| 9619 | InputInfo.BasePointersArray.getPointer(), |
| 9620 | InputInfo.PointersArray.getPointer(), |
| 9621 | InputInfo.SizesArray.getPointer(), |
| 9622 | MapTypesArray}; |
Samuel Antao | 8d2d730 | 2016-05-26 18:30:22 +0000 | [diff] [blame] | 9623 | |
Samuel Antao | 8d2d730 | 2016-05-26 18:30:22 +0000 | [diff] [blame] | 9624 | // Select the right runtime function call for each expected standalone |
| 9625 | // directive. |
Alexey Bataev | 0cc6b8e | 2017-12-14 17:00:17 +0000 | [diff] [blame] | 9626 | const bool HasNowait = D.hasClausesOfKind<OMPNowaitClause>(); |
Samuel Antao | 8d2d730 | 2016-05-26 18:30:22 +0000 | [diff] [blame] | 9627 | OpenMPRTLFunction RTLFn; |
| 9628 | switch (D.getDirectiveKind()) { |
Samuel Antao | 8d2d730 | 2016-05-26 18:30:22 +0000 | [diff] [blame] | 9629 | case OMPD_target_enter_data: |
Alexey Bataev | 0cc6b8e | 2017-12-14 17:00:17 +0000 | [diff] [blame] | 9630 | RTLFn = HasNowait ? OMPRTL__tgt_target_data_begin_nowait |
| 9631 | : OMPRTL__tgt_target_data_begin; |
Samuel Antao | 8d2d730 | 2016-05-26 18:30:22 +0000 | [diff] [blame] | 9632 | break; |
| 9633 | case OMPD_target_exit_data: |
Alexey Bataev | 0cc6b8e | 2017-12-14 17:00:17 +0000 | [diff] [blame] | 9634 | RTLFn = HasNowait ? OMPRTL__tgt_target_data_end_nowait |
| 9635 | : OMPRTL__tgt_target_data_end; |
Samuel Antao | 8d2d730 | 2016-05-26 18:30:22 +0000 | [diff] [blame] | 9636 | break; |
| 9637 | case OMPD_target_update: |
Alexey Bataev | 0cc6b8e | 2017-12-14 17:00:17 +0000 | [diff] [blame] | 9638 | RTLFn = HasNowait ? OMPRTL__tgt_target_data_update_nowait |
| 9639 | : OMPRTL__tgt_target_data_update; |
Samuel Antao | 8d2d730 | 2016-05-26 18:30:22 +0000 | [diff] [blame] | 9640 | break; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 9641 | case OMPD_parallel: |
| 9642 | case OMPD_for: |
| 9643 | case OMPD_parallel_for: |
| 9644 | case OMPD_parallel_sections: |
| 9645 | case OMPD_for_simd: |
| 9646 | case OMPD_parallel_for_simd: |
| 9647 | case OMPD_cancel: |
| 9648 | case OMPD_cancellation_point: |
| 9649 | case OMPD_ordered: |
| 9650 | case OMPD_threadprivate: |
Alexey Bataev | 25ed0c0 | 2019-03-07 17:54:44 +0000 | [diff] [blame] | 9651 | case OMPD_allocate: |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 9652 | case OMPD_task: |
| 9653 | case OMPD_simd: |
| 9654 | case OMPD_sections: |
| 9655 | case OMPD_section: |
| 9656 | case OMPD_single: |
| 9657 | case OMPD_master: |
| 9658 | case OMPD_critical: |
| 9659 | case OMPD_taskyield: |
| 9660 | case OMPD_barrier: |
| 9661 | case OMPD_taskwait: |
| 9662 | case OMPD_taskgroup: |
| 9663 | case OMPD_atomic: |
| 9664 | case OMPD_flush: |
| 9665 | case OMPD_teams: |
| 9666 | case OMPD_target_data: |
| 9667 | case OMPD_distribute: |
| 9668 | case OMPD_distribute_simd: |
| 9669 | case OMPD_distribute_parallel_for: |
| 9670 | case OMPD_distribute_parallel_for_simd: |
| 9671 | case OMPD_teams_distribute: |
| 9672 | case OMPD_teams_distribute_simd: |
| 9673 | case OMPD_teams_distribute_parallel_for: |
| 9674 | case OMPD_teams_distribute_parallel_for_simd: |
| 9675 | case OMPD_declare_simd: |
| 9676 | case OMPD_declare_target: |
| 9677 | case OMPD_end_declare_target: |
| 9678 | case OMPD_declare_reduction: |
Michael Kruse | 251e148 | 2019-02-01 20:25:04 +0000 | [diff] [blame] | 9679 | case OMPD_declare_mapper: |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 9680 | case OMPD_taskloop: |
| 9681 | case OMPD_taskloop_simd: |
| 9682 | case OMPD_target: |
| 9683 | case OMPD_target_simd: |
| 9684 | case OMPD_target_teams_distribute: |
| 9685 | case OMPD_target_teams_distribute_simd: |
| 9686 | case OMPD_target_teams_distribute_parallel_for: |
| 9687 | case OMPD_target_teams_distribute_parallel_for_simd: |
| 9688 | case OMPD_target_teams: |
| 9689 | case OMPD_target_parallel: |
| 9690 | case OMPD_target_parallel_for: |
| 9691 | case OMPD_target_parallel_for_simd: |
Kelvin Li | 1408f91 | 2018-09-26 04:28:39 +0000 | [diff] [blame] | 9692 | case OMPD_requires: |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 9693 | case OMPD_unknown: |
| 9694 | llvm_unreachable("Unexpected standalone target data directive."); |
| 9695 | break; |
Samuel Antao | 8d2d730 | 2016-05-26 18:30:22 +0000 | [diff] [blame] | 9696 | } |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 9697 | CGF.EmitRuntimeCall(createRuntimeFunction(RTLFn), OffloadingArgs); |
Samuel Antao | bd0ae2e | 2016-04-27 23:07:29 +0000 | [diff] [blame] | 9698 | }; |
| 9699 | |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 9700 | auto &&TargetThenGen = [this, &ThenGen, &D, &InputInfo, &MapTypesArray]( |
| 9701 | CodeGenFunction &CGF, PrePostActionTy &) { |
| 9702 | // Fill up the arrays with all the mapped variables. |
| 9703 | MappableExprsHandler::MapBaseValuesArrayTy BasePointers; |
| 9704 | MappableExprsHandler::MapValuesArrayTy Pointers; |
| 9705 | MappableExprsHandler::MapValuesArrayTy Sizes; |
| 9706 | MappableExprsHandler::MapFlagsArrayTy MapTypes; |
Samuel Antao | bd0ae2e | 2016-04-27 23:07:29 +0000 | [diff] [blame] | 9707 | |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 9708 | // Get map clause information. |
| 9709 | MappableExprsHandler MEHandler(D, CGF); |
| 9710 | MEHandler.generateAllInfo(BasePointers, Pointers, Sizes, MapTypes); |
| 9711 | |
| 9712 | TargetDataInfo Info; |
| 9713 | // Fill up the arrays and create the arguments. |
| 9714 | emitOffloadingArrays(CGF, BasePointers, Pointers, Sizes, MapTypes, Info); |
| 9715 | emitOffloadingArraysArgument(CGF, Info.BasePointersArray, |
| 9716 | Info.PointersArray, Info.SizesArray, |
| 9717 | Info.MapTypesArray, Info); |
| 9718 | InputInfo.NumberOfTargetItems = Info.NumberOfPtrs; |
| 9719 | InputInfo.BasePointersArray = |
| 9720 | Address(Info.BasePointersArray, CGM.getPointerAlign()); |
| 9721 | InputInfo.PointersArray = |
| 9722 | Address(Info.PointersArray, CGM.getPointerAlign()); |
| 9723 | InputInfo.SizesArray = |
| 9724 | Address(Info.SizesArray, CGM.getPointerAlign()); |
| 9725 | MapTypesArray = Info.MapTypesArray; |
| 9726 | if (D.hasClausesOfKind<OMPDependClause>()) |
| 9727 | CGF.EmitOMPTargetTaskBasedDirective(D, ThenGen, InputInfo); |
| 9728 | else |
Alexey Bataev | 768f1f2 | 2018-01-09 19:59:25 +0000 | [diff] [blame] | 9729 | emitInlinedDirective(CGF, D.getDirectiveKind(), ThenGen); |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 9730 | }; |
| 9731 | |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 9732 | if (IfCond) { |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 9733 | emitOMPIfClause(CGF, IfCond, TargetThenGen, |
| 9734 | [](CodeGenFunction &CGF, PrePostActionTy &) {}); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 9735 | } else { |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 9736 | RegionCodeGenTy ThenRCG(TargetThenGen); |
| 9737 | ThenRCG(CGF); |
Samuel Antao | bd0ae2e | 2016-04-27 23:07:29 +0000 | [diff] [blame] | 9738 | } |
| 9739 | } |
Alexey Bataev | c7a82b4 | 2016-05-06 09:40:08 +0000 | [diff] [blame] | 9740 | |
| 9741 | namespace { |
| 9742 | /// Kind of parameter in a function with 'declare simd' directive. |
| 9743 | enum ParamKindTy { LinearWithVarStride, Linear, Uniform, Vector }; |
| 9744 | /// Attribute set of the parameter. |
| 9745 | struct ParamAttrTy { |
| 9746 | ParamKindTy Kind = Vector; |
| 9747 | llvm::APSInt StrideOrArg; |
| 9748 | llvm::APSInt Alignment; |
| 9749 | }; |
| 9750 | } // namespace |
| 9751 | |
| 9752 | static unsigned evaluateCDTSize(const FunctionDecl *FD, |
| 9753 | ArrayRef<ParamAttrTy> ParamAttrs) { |
| 9754 | // Every vector variant of a SIMD-enabled function has a vector length (VLEN). |
| 9755 | // If OpenMP clause "simdlen" is used, the VLEN is the value of the argument |
| 9756 | // of that clause. The VLEN value must be power of 2. |
| 9757 | // In other case the notion of the function`s "characteristic data type" (CDT) |
| 9758 | // is used to compute the vector length. |
| 9759 | // CDT is defined in the following order: |
| 9760 | // a) For non-void function, the CDT is the return type. |
| 9761 | // b) If the function has any non-uniform, non-linear parameters, then the |
| 9762 | // CDT is the type of the first such parameter. |
| 9763 | // c) If the CDT determined by a) or b) above is struct, union, or class |
| 9764 | // type which is pass-by-value (except for the type that maps to the |
| 9765 | // built-in complex data type), the characteristic data type is int. |
| 9766 | // d) If none of the above three cases is applicable, the CDT is int. |
| 9767 | // The VLEN is then determined based on the CDT and the size of vector |
| 9768 | // register of that ISA for which current vector version is generated. The |
| 9769 | // VLEN is computed using the formula below: |
| 9770 | // VLEN = sizeof(vector_register) / sizeof(CDT), |
| 9771 | // where vector register size specified in section 3.2.1 Registers and the |
| 9772 | // Stack Frame of original AMD64 ABI document. |
| 9773 | QualType RetType = FD->getReturnType(); |
| 9774 | if (RetType.isNull()) |
| 9775 | return 0; |
| 9776 | ASTContext &C = FD->getASTContext(); |
| 9777 | QualType CDT; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 9778 | if (!RetType.isNull() && !RetType->isVoidType()) { |
Alexey Bataev | c7a82b4 | 2016-05-06 09:40:08 +0000 | [diff] [blame] | 9779 | CDT = RetType; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 9780 | } else { |
Alexey Bataev | c7a82b4 | 2016-05-06 09:40:08 +0000 | [diff] [blame] | 9781 | unsigned Offset = 0; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 9782 | if (const auto *MD = dyn_cast<CXXMethodDecl>(FD)) { |
Alexey Bataev | c7a82b4 | 2016-05-06 09:40:08 +0000 | [diff] [blame] | 9783 | if (ParamAttrs[Offset].Kind == Vector) |
| 9784 | CDT = C.getPointerType(C.getRecordType(MD->getParent())); |
| 9785 | ++Offset; |
| 9786 | } |
| 9787 | if (CDT.isNull()) { |
| 9788 | for (unsigned I = 0, E = FD->getNumParams(); I < E; ++I) { |
| 9789 | if (ParamAttrs[I + Offset].Kind == Vector) { |
| 9790 | CDT = FD->getParamDecl(I)->getType(); |
| 9791 | break; |
| 9792 | } |
| 9793 | } |
| 9794 | } |
| 9795 | } |
| 9796 | if (CDT.isNull()) |
| 9797 | CDT = C.IntTy; |
| 9798 | CDT = CDT->getCanonicalTypeUnqualified(); |
| 9799 | if (CDT->isRecordType() || CDT->isUnionType()) |
| 9800 | CDT = C.IntTy; |
| 9801 | return C.getTypeSize(CDT); |
| 9802 | } |
| 9803 | |
| 9804 | static void |
| 9805 | emitX86DeclareSimdFunction(const FunctionDecl *FD, llvm::Function *Fn, |
Benjamin Kramer | 81cb4b7 | 2016-11-24 16:01:20 +0000 | [diff] [blame] | 9806 | const llvm::APSInt &VLENVal, |
Alexey Bataev | c7a82b4 | 2016-05-06 09:40:08 +0000 | [diff] [blame] | 9807 | ArrayRef<ParamAttrTy> ParamAttrs, |
| 9808 | OMPDeclareSimdDeclAttr::BranchStateTy State) { |
| 9809 | struct ISADataTy { |
| 9810 | char ISA; |
| 9811 | unsigned VecRegSize; |
| 9812 | }; |
| 9813 | ISADataTy ISAData[] = { |
| 9814 | { |
| 9815 | 'b', 128 |
| 9816 | }, // SSE |
| 9817 | { |
| 9818 | 'c', 256 |
| 9819 | }, // AVX |
| 9820 | { |
| 9821 | 'd', 256 |
| 9822 | }, // AVX2 |
| 9823 | { |
| 9824 | 'e', 512 |
| 9825 | }, // AVX512 |
| 9826 | }; |
| 9827 | llvm::SmallVector<char, 2> Masked; |
| 9828 | switch (State) { |
| 9829 | case OMPDeclareSimdDeclAttr::BS_Undefined: |
| 9830 | Masked.push_back('N'); |
| 9831 | Masked.push_back('M'); |
| 9832 | break; |
| 9833 | case OMPDeclareSimdDeclAttr::BS_Notinbranch: |
| 9834 | Masked.push_back('N'); |
| 9835 | break; |
| 9836 | case OMPDeclareSimdDeclAttr::BS_Inbranch: |
| 9837 | Masked.push_back('M'); |
| 9838 | break; |
| 9839 | } |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 9840 | for (char Mask : Masked) { |
| 9841 | for (const ISADataTy &Data : ISAData) { |
Alexey Bataev | c7a82b4 | 2016-05-06 09:40:08 +0000 | [diff] [blame] | 9842 | SmallString<256> Buffer; |
| 9843 | llvm::raw_svector_ostream Out(Buffer); |
| 9844 | Out << "_ZGV" << Data.ISA << Mask; |
| 9845 | if (!VLENVal) { |
Simon Pilgrim | 823a99c | 2019-05-22 13:02:19 +0000 | [diff] [blame] | 9846 | unsigned NumElts = evaluateCDTSize(FD, ParamAttrs); |
| 9847 | assert(NumElts && "Non-zero simdlen/cdtsize expected"); |
| 9848 | Out << llvm::APSInt::getUnsigned(Data.VecRegSize / NumElts); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 9849 | } else { |
Alexey Bataev | c7a82b4 | 2016-05-06 09:40:08 +0000 | [diff] [blame] | 9850 | Out << VLENVal; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 9851 | } |
| 9852 | for (const ParamAttrTy &ParamAttr : ParamAttrs) { |
Alexey Bataev | c7a82b4 | 2016-05-06 09:40:08 +0000 | [diff] [blame] | 9853 | switch (ParamAttr.Kind){ |
| 9854 | case LinearWithVarStride: |
| 9855 | Out << 's' << ParamAttr.StrideOrArg; |
| 9856 | break; |
| 9857 | case Linear: |
| 9858 | Out << 'l'; |
| 9859 | if (!!ParamAttr.StrideOrArg) |
| 9860 | Out << ParamAttr.StrideOrArg; |
| 9861 | break; |
| 9862 | case Uniform: |
| 9863 | Out << 'u'; |
| 9864 | break; |
| 9865 | case Vector: |
| 9866 | Out << 'v'; |
| 9867 | break; |
| 9868 | } |
| 9869 | if (!!ParamAttr.Alignment) |
| 9870 | Out << 'a' << ParamAttr.Alignment; |
| 9871 | } |
| 9872 | Out << '_' << Fn->getName(); |
| 9873 | Fn->addFnAttr(Out.str()); |
| 9874 | } |
| 9875 | } |
| 9876 | } |
| 9877 | |
Alexey Bataev | a0a2264 | 2019-04-16 13:56:21 +0000 | [diff] [blame] | 9878 | // This are the Functions that are needed to mangle the name of the |
| 9879 | // vector functions generated by the compiler, according to the rules |
| 9880 | // defined in the "Vector Function ABI specifications for AArch64", |
| 9881 | // available at |
| 9882 | // https://developer.arm.com/products/software-development-tools/hpc/arm-compiler-for-hpc/vector-function-abi. |
| 9883 | |
| 9884 | /// Maps To Vector (MTV), as defined in 3.1.1 of the AAVFABI. |
| 9885 | /// |
| 9886 | /// TODO: Need to implement the behavior for reference marked with a |
| 9887 | /// var or no linear modifiers (1.b in the section). For this, we |
| 9888 | /// need to extend ParamKindTy to support the linear modifiers. |
| 9889 | static bool getAArch64MTV(QualType QT, ParamKindTy Kind) { |
| 9890 | QT = QT.getCanonicalType(); |
| 9891 | |
| 9892 | if (QT->isVoidType()) |
| 9893 | return false; |
| 9894 | |
| 9895 | if (Kind == ParamKindTy::Uniform) |
| 9896 | return false; |
| 9897 | |
| 9898 | if (Kind == ParamKindTy::Linear) |
| 9899 | return false; |
| 9900 | |
| 9901 | // TODO: Handle linear references with modifiers |
| 9902 | |
| 9903 | if (Kind == ParamKindTy::LinearWithVarStride) |
| 9904 | return false; |
| 9905 | |
| 9906 | return true; |
| 9907 | } |
| 9908 | |
| 9909 | /// Pass By Value (PBV), as defined in 3.1.2 of the AAVFABI. |
| 9910 | static bool getAArch64PBV(QualType QT, ASTContext &C) { |
| 9911 | QT = QT.getCanonicalType(); |
| 9912 | unsigned Size = C.getTypeSize(QT); |
| 9913 | |
| 9914 | // Only scalars and complex within 16 bytes wide set PVB to true. |
| 9915 | if (Size != 8 && Size != 16 && Size != 32 && Size != 64 && Size != 128) |
| 9916 | return false; |
| 9917 | |
| 9918 | if (QT->isFloatingType()) |
| 9919 | return true; |
| 9920 | |
| 9921 | if (QT->isIntegerType()) |
| 9922 | return true; |
| 9923 | |
| 9924 | if (QT->isPointerType()) |
| 9925 | return true; |
| 9926 | |
| 9927 | // TODO: Add support for complex types (section 3.1.2, item 2). |
| 9928 | |
| 9929 | return false; |
| 9930 | } |
| 9931 | |
| 9932 | /// Computes the lane size (LS) of a return type or of an input parameter, |
| 9933 | /// as defined by `LS(P)` in 3.2.1 of the AAVFABI. |
| 9934 | /// TODO: Add support for references, section 3.2.1, item 1. |
| 9935 | static unsigned getAArch64LS(QualType QT, ParamKindTy Kind, ASTContext &C) { |
| 9936 | if (getAArch64MTV(QT, Kind) && QT.getCanonicalType()->isPointerType()) { |
| 9937 | QualType PTy = QT.getCanonicalType()->getPointeeType(); |
| 9938 | if (getAArch64PBV(PTy, C)) |
| 9939 | return C.getTypeSize(PTy); |
| 9940 | } |
| 9941 | if (getAArch64PBV(QT, C)) |
| 9942 | return C.getTypeSize(QT); |
| 9943 | |
| 9944 | return C.getTypeSize(C.getUIntPtrType()); |
| 9945 | } |
| 9946 | |
| 9947 | // Get Narrowest Data Size (NDS) and Widest Data Size (WDS) from the |
| 9948 | // signature of the scalar function, as defined in 3.2.2 of the |
| 9949 | // AAVFABI. |
| 9950 | static std::tuple<unsigned, unsigned, bool> |
| 9951 | getNDSWDS(const FunctionDecl *FD, ArrayRef<ParamAttrTy> ParamAttrs) { |
| 9952 | QualType RetType = FD->getReturnType().getCanonicalType(); |
| 9953 | |
| 9954 | ASTContext &C = FD->getASTContext(); |
| 9955 | |
| 9956 | bool OutputBecomesInput = false; |
| 9957 | |
| 9958 | llvm::SmallVector<unsigned, 8> Sizes; |
| 9959 | if (!RetType->isVoidType()) { |
| 9960 | Sizes.push_back(getAArch64LS(RetType, ParamKindTy::Vector, C)); |
| 9961 | if (!getAArch64PBV(RetType, C) && getAArch64MTV(RetType, {})) |
| 9962 | OutputBecomesInput = true; |
| 9963 | } |
| 9964 | for (unsigned I = 0, E = FD->getNumParams(); I < E; ++I) { |
| 9965 | QualType QT = FD->getParamDecl(I)->getType().getCanonicalType(); |
| 9966 | Sizes.push_back(getAArch64LS(QT, ParamAttrs[I].Kind, C)); |
| 9967 | } |
| 9968 | |
| 9969 | assert(!Sizes.empty() && "Unable to determine NDS and WDS."); |
| 9970 | // The LS of a function parameter / return value can only be a power |
| 9971 | // of 2, starting from 8 bits, up to 128. |
| 9972 | assert(std::all_of(Sizes.begin(), Sizes.end(), |
| 9973 | [](unsigned Size) { |
| 9974 | return Size == 8 || Size == 16 || Size == 32 || |
| 9975 | Size == 64 || Size == 128; |
| 9976 | }) && |
| 9977 | "Invalid size"); |
| 9978 | |
| 9979 | return std::make_tuple(*std::min_element(std::begin(Sizes), std::end(Sizes)), |
| 9980 | *std::max_element(std::begin(Sizes), std::end(Sizes)), |
| 9981 | OutputBecomesInput); |
| 9982 | } |
| 9983 | |
| 9984 | /// Mangle the parameter part of the vector function name according to |
| 9985 | /// their OpenMP classification. The mangling function is defined in |
| 9986 | /// section 3.5 of the AAVFABI. |
| 9987 | static std::string mangleVectorParameters(ArrayRef<ParamAttrTy> ParamAttrs) { |
| 9988 | SmallString<256> Buffer; |
| 9989 | llvm::raw_svector_ostream Out(Buffer); |
| 9990 | for (const auto &ParamAttr : ParamAttrs) { |
| 9991 | switch (ParamAttr.Kind) { |
| 9992 | case LinearWithVarStride: |
| 9993 | Out << "ls" << ParamAttr.StrideOrArg; |
| 9994 | break; |
| 9995 | case Linear: |
| 9996 | Out << 'l'; |
| 9997 | // Don't print the step value if it is not present or if it is |
| 9998 | // equal to 1. |
| 9999 | if (!!ParamAttr.StrideOrArg && ParamAttr.StrideOrArg != 1) |
| 10000 | Out << ParamAttr.StrideOrArg; |
| 10001 | break; |
| 10002 | case Uniform: |
| 10003 | Out << 'u'; |
| 10004 | break; |
| 10005 | case Vector: |
| 10006 | Out << 'v'; |
| 10007 | break; |
| 10008 | } |
| 10009 | |
| 10010 | if (!!ParamAttr.Alignment) |
| 10011 | Out << 'a' << ParamAttr.Alignment; |
| 10012 | } |
| 10013 | |
| 10014 | return Out.str(); |
| 10015 | } |
| 10016 | |
| 10017 | // Function used to add the attribute. The parameter `VLEN` is |
| 10018 | // templated to allow the use of "x" when targeting scalable functions |
| 10019 | // for SVE. |
| 10020 | template <typename T> |
| 10021 | static void addAArch64VectorName(T VLEN, StringRef LMask, StringRef Prefix, |
| 10022 | char ISA, StringRef ParSeq, |
| 10023 | StringRef MangledName, bool OutputBecomesInput, |
| 10024 | llvm::Function *Fn) { |
| 10025 | SmallString<256> Buffer; |
| 10026 | llvm::raw_svector_ostream Out(Buffer); |
| 10027 | Out << Prefix << ISA << LMask << VLEN; |
| 10028 | if (OutputBecomesInput) |
| 10029 | Out << "v"; |
| 10030 | Out << ParSeq << "_" << MangledName; |
| 10031 | Fn->addFnAttr(Out.str()); |
| 10032 | } |
| 10033 | |
| 10034 | // Helper function to generate the Advanced SIMD names depending on |
| 10035 | // the value of the NDS when simdlen is not present. |
| 10036 | static void addAArch64AdvSIMDNDSNames(unsigned NDS, StringRef Mask, |
| 10037 | StringRef Prefix, char ISA, |
| 10038 | StringRef ParSeq, StringRef MangledName, |
| 10039 | bool OutputBecomesInput, |
| 10040 | llvm::Function *Fn) { |
| 10041 | switch (NDS) { |
| 10042 | case 8: |
| 10043 | addAArch64VectorName(8, Mask, Prefix, ISA, ParSeq, MangledName, |
| 10044 | OutputBecomesInput, Fn); |
| 10045 | addAArch64VectorName(16, Mask, Prefix, ISA, ParSeq, MangledName, |
| 10046 | OutputBecomesInput, Fn); |
| 10047 | break; |
| 10048 | case 16: |
| 10049 | addAArch64VectorName(4, Mask, Prefix, ISA, ParSeq, MangledName, |
| 10050 | OutputBecomesInput, Fn); |
| 10051 | addAArch64VectorName(8, Mask, Prefix, ISA, ParSeq, MangledName, |
| 10052 | OutputBecomesInput, Fn); |
| 10053 | break; |
| 10054 | case 32: |
| 10055 | addAArch64VectorName(2, Mask, Prefix, ISA, ParSeq, MangledName, |
| 10056 | OutputBecomesInput, Fn); |
| 10057 | addAArch64VectorName(4, Mask, Prefix, ISA, ParSeq, MangledName, |
| 10058 | OutputBecomesInput, Fn); |
| 10059 | break; |
| 10060 | case 64: |
| 10061 | case 128: |
| 10062 | addAArch64VectorName(2, Mask, Prefix, ISA, ParSeq, MangledName, |
| 10063 | OutputBecomesInput, Fn); |
| 10064 | break; |
| 10065 | default: |
| 10066 | llvm_unreachable("Scalar type is too wide."); |
| 10067 | } |
| 10068 | } |
| 10069 | |
| 10070 | /// Emit vector function attributes for AArch64, as defined in the AAVFABI. |
| 10071 | static void emitAArch64DeclareSimdFunction( |
| 10072 | CodeGenModule &CGM, const FunctionDecl *FD, unsigned UserVLEN, |
| 10073 | ArrayRef<ParamAttrTy> ParamAttrs, |
| 10074 | OMPDeclareSimdDeclAttr::BranchStateTy State, StringRef MangledName, |
| 10075 | char ISA, unsigned VecRegSize, llvm::Function *Fn, SourceLocation SLoc) { |
| 10076 | |
| 10077 | // Get basic data for building the vector signature. |
| 10078 | const auto Data = getNDSWDS(FD, ParamAttrs); |
| 10079 | const unsigned NDS = std::get<0>(Data); |
| 10080 | const unsigned WDS = std::get<1>(Data); |
| 10081 | const bool OutputBecomesInput = std::get<2>(Data); |
| 10082 | |
| 10083 | // Check the values provided via `simdlen` by the user. |
| 10084 | // 1. A `simdlen(1)` doesn't produce vector signatures, |
| 10085 | if (UserVLEN == 1) { |
| 10086 | unsigned DiagID = CGM.getDiags().getCustomDiagID( |
| 10087 | DiagnosticsEngine::Warning, |
| 10088 | "The clause simdlen(1) has no effect when targeting aarch64."); |
| 10089 | CGM.getDiags().Report(SLoc, DiagID); |
| 10090 | return; |
| 10091 | } |
| 10092 | |
| 10093 | // 2. Section 3.3.1, item 1: user input must be a power of 2 for |
| 10094 | // Advanced SIMD output. |
| 10095 | if (ISA == 'n' && UserVLEN && !llvm::isPowerOf2_32(UserVLEN)) { |
| 10096 | unsigned DiagID = CGM.getDiags().getCustomDiagID( |
| 10097 | DiagnosticsEngine::Warning, "The value specified in simdlen must be a " |
| 10098 | "power of 2 when targeting Advanced SIMD."); |
| 10099 | CGM.getDiags().Report(SLoc, DiagID); |
| 10100 | return; |
| 10101 | } |
| 10102 | |
| 10103 | // 3. Section 3.4.1. SVE fixed lengh must obey the architectural |
| 10104 | // limits. |
| 10105 | if (ISA == 's' && UserVLEN != 0) { |
| 10106 | if ((UserVLEN * WDS > 2048) || (UserVLEN * WDS % 128 != 0)) { |
| 10107 | unsigned DiagID = CGM.getDiags().getCustomDiagID( |
| 10108 | DiagnosticsEngine::Warning, "The clause simdlen must fit the %0-bit " |
| 10109 | "lanes in the architectural constraints " |
| 10110 | "for SVE (min is 128-bit, max is " |
| 10111 | "2048-bit, by steps of 128-bit)"); |
| 10112 | CGM.getDiags().Report(SLoc, DiagID) << WDS; |
| 10113 | return; |
| 10114 | } |
| 10115 | } |
| 10116 | |
| 10117 | // Sort out parameter sequence. |
| 10118 | const std::string ParSeq = mangleVectorParameters(ParamAttrs); |
| 10119 | StringRef Prefix = "_ZGV"; |
| 10120 | // Generate simdlen from user input (if any). |
| 10121 | if (UserVLEN) { |
| 10122 | if (ISA == 's') { |
| 10123 | // SVE generates only a masked function. |
| 10124 | addAArch64VectorName(UserVLEN, "M", Prefix, ISA, ParSeq, MangledName, |
| 10125 | OutputBecomesInput, Fn); |
| 10126 | } else { |
| 10127 | assert(ISA == 'n' && "Expected ISA either 's' or 'n'."); |
| 10128 | // Advanced SIMD generates one or two functions, depending on |
| 10129 | // the `[not]inbranch` clause. |
| 10130 | switch (State) { |
| 10131 | case OMPDeclareSimdDeclAttr::BS_Undefined: |
| 10132 | addAArch64VectorName(UserVLEN, "N", Prefix, ISA, ParSeq, MangledName, |
| 10133 | OutputBecomesInput, Fn); |
| 10134 | addAArch64VectorName(UserVLEN, "M", Prefix, ISA, ParSeq, MangledName, |
| 10135 | OutputBecomesInput, Fn); |
| 10136 | break; |
| 10137 | case OMPDeclareSimdDeclAttr::BS_Notinbranch: |
| 10138 | addAArch64VectorName(UserVLEN, "N", Prefix, ISA, ParSeq, MangledName, |
| 10139 | OutputBecomesInput, Fn); |
| 10140 | break; |
| 10141 | case OMPDeclareSimdDeclAttr::BS_Inbranch: |
| 10142 | addAArch64VectorName(UserVLEN, "M", Prefix, ISA, ParSeq, MangledName, |
| 10143 | OutputBecomesInput, Fn); |
| 10144 | break; |
| 10145 | } |
| 10146 | } |
| 10147 | } else { |
| 10148 | // If no user simdlen is provided, follow the AAVFABI rules for |
| 10149 | // generating the vector length. |
| 10150 | if (ISA == 's') { |
| 10151 | // SVE, section 3.4.1, item 1. |
| 10152 | addAArch64VectorName("x", "M", Prefix, ISA, ParSeq, MangledName, |
| 10153 | OutputBecomesInput, Fn); |
| 10154 | } else { |
| 10155 | assert(ISA == 'n' && "Expected ISA either 's' or 'n'."); |
| 10156 | // Advanced SIMD, Section 3.3.1 of the AAVFABI, generates one or |
| 10157 | // two vector names depending on the use of the clause |
| 10158 | // `[not]inbranch`. |
| 10159 | switch (State) { |
| 10160 | case OMPDeclareSimdDeclAttr::BS_Undefined: |
| 10161 | addAArch64AdvSIMDNDSNames(NDS, "N", Prefix, ISA, ParSeq, MangledName, |
| 10162 | OutputBecomesInput, Fn); |
| 10163 | addAArch64AdvSIMDNDSNames(NDS, "M", Prefix, ISA, ParSeq, MangledName, |
| 10164 | OutputBecomesInput, Fn); |
| 10165 | break; |
| 10166 | case OMPDeclareSimdDeclAttr::BS_Notinbranch: |
| 10167 | addAArch64AdvSIMDNDSNames(NDS, "N", Prefix, ISA, ParSeq, MangledName, |
| 10168 | OutputBecomesInput, Fn); |
| 10169 | break; |
| 10170 | case OMPDeclareSimdDeclAttr::BS_Inbranch: |
| 10171 | addAArch64AdvSIMDNDSNames(NDS, "M", Prefix, ISA, ParSeq, MangledName, |
| 10172 | OutputBecomesInput, Fn); |
| 10173 | break; |
| 10174 | } |
| 10175 | } |
| 10176 | } |
| 10177 | } |
| 10178 | |
Alexey Bataev | c7a82b4 | 2016-05-06 09:40:08 +0000 | [diff] [blame] | 10179 | void CGOpenMPRuntime::emitDeclareSimdFunction(const FunctionDecl *FD, |
| 10180 | llvm::Function *Fn) { |
| 10181 | ASTContext &C = CGM.getContext(); |
Alexey Bataev | 20cf67c | 2018-03-02 18:07:00 +0000 | [diff] [blame] | 10182 | FD = FD->getMostRecentDecl(); |
Alexey Bataev | c7a82b4 | 2016-05-06 09:40:08 +0000 | [diff] [blame] | 10183 | // Map params to their positions in function decl. |
| 10184 | llvm::DenseMap<const Decl *, unsigned> ParamPositions; |
| 10185 | if (isa<CXXMethodDecl>(FD)) |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 10186 | ParamPositions.try_emplace(FD, 0); |
Alexey Bataev | c7a82b4 | 2016-05-06 09:40:08 +0000 | [diff] [blame] | 10187 | unsigned ParamPos = ParamPositions.size(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 10188 | for (const ParmVarDecl *P : FD->parameters()) { |
| 10189 | ParamPositions.try_emplace(P->getCanonicalDecl(), ParamPos); |
Alexey Bataev | c7a82b4 | 2016-05-06 09:40:08 +0000 | [diff] [blame] | 10190 | ++ParamPos; |
| 10191 | } |
Alexey Bataev | 20cf67c | 2018-03-02 18:07:00 +0000 | [diff] [blame] | 10192 | while (FD) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 10193 | for (const auto *Attr : FD->specific_attrs<OMPDeclareSimdDeclAttr>()) { |
Alexey Bataev | 20cf67c | 2018-03-02 18:07:00 +0000 | [diff] [blame] | 10194 | llvm::SmallVector<ParamAttrTy, 8> ParamAttrs(ParamPositions.size()); |
| 10195 | // Mark uniform parameters. |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 10196 | for (const Expr *E : Attr->uniforms()) { |
Alexey Bataev | 20cf67c | 2018-03-02 18:07:00 +0000 | [diff] [blame] | 10197 | E = E->IgnoreParenImpCasts(); |
| 10198 | unsigned Pos; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 10199 | if (isa<CXXThisExpr>(E)) { |
Alexey Bataev | 20cf67c | 2018-03-02 18:07:00 +0000 | [diff] [blame] | 10200 | Pos = ParamPositions[FD]; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 10201 | } else { |
| 10202 | const auto *PVD = cast<ParmVarDecl>(cast<DeclRefExpr>(E)->getDecl()) |
| 10203 | ->getCanonicalDecl(); |
Alexey Bataev | 20cf67c | 2018-03-02 18:07:00 +0000 | [diff] [blame] | 10204 | Pos = ParamPositions[PVD]; |
| 10205 | } |
| 10206 | ParamAttrs[Pos].Kind = Uniform; |
Alexey Bataev | c7a82b4 | 2016-05-06 09:40:08 +0000 | [diff] [blame] | 10207 | } |
Alexey Bataev | 20cf67c | 2018-03-02 18:07:00 +0000 | [diff] [blame] | 10208 | // Get alignment info. |
| 10209 | auto NI = Attr->alignments_begin(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 10210 | for (const Expr *E : Attr->aligneds()) { |
Alexey Bataev | 20cf67c | 2018-03-02 18:07:00 +0000 | [diff] [blame] | 10211 | E = E->IgnoreParenImpCasts(); |
| 10212 | unsigned Pos; |
| 10213 | QualType ParmTy; |
| 10214 | if (isa<CXXThisExpr>(E)) { |
| 10215 | Pos = ParamPositions[FD]; |
| 10216 | ParmTy = E->getType(); |
| 10217 | } else { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 10218 | const auto *PVD = cast<ParmVarDecl>(cast<DeclRefExpr>(E)->getDecl()) |
| 10219 | ->getCanonicalDecl(); |
Alexey Bataev | 20cf67c | 2018-03-02 18:07:00 +0000 | [diff] [blame] | 10220 | Pos = ParamPositions[PVD]; |
| 10221 | ParmTy = PVD->getType(); |
| 10222 | } |
| 10223 | ParamAttrs[Pos].Alignment = |
| 10224 | (*NI) |
| 10225 | ? (*NI)->EvaluateKnownConstInt(C) |
Alexey Bataev | c7a82b4 | 2016-05-06 09:40:08 +0000 | [diff] [blame] | 10226 | : llvm::APSInt::getUnsigned( |
| 10227 | C.toCharUnitsFromBits(C.getOpenMPDefaultSimdAlign(ParmTy)) |
| 10228 | .getQuantity()); |
Alexey Bataev | 20cf67c | 2018-03-02 18:07:00 +0000 | [diff] [blame] | 10229 | ++NI; |
Alexey Bataev | c7a82b4 | 2016-05-06 09:40:08 +0000 | [diff] [blame] | 10230 | } |
Alexey Bataev | 20cf67c | 2018-03-02 18:07:00 +0000 | [diff] [blame] | 10231 | // Mark linear parameters. |
| 10232 | auto SI = Attr->steps_begin(); |
| 10233 | auto MI = Attr->modifiers_begin(); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 10234 | for (const Expr *E : Attr->linears()) { |
Alexey Bataev | 20cf67c | 2018-03-02 18:07:00 +0000 | [diff] [blame] | 10235 | E = E->IgnoreParenImpCasts(); |
| 10236 | unsigned Pos; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 10237 | if (isa<CXXThisExpr>(E)) { |
Alexey Bataev | 20cf67c | 2018-03-02 18:07:00 +0000 | [diff] [blame] | 10238 | Pos = ParamPositions[FD]; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 10239 | } else { |
| 10240 | const auto *PVD = cast<ParmVarDecl>(cast<DeclRefExpr>(E)->getDecl()) |
| 10241 | ->getCanonicalDecl(); |
Alexey Bataev | 20cf67c | 2018-03-02 18:07:00 +0000 | [diff] [blame] | 10242 | Pos = ParamPositions[PVD]; |
| 10243 | } |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 10244 | ParamAttrTy &ParamAttr = ParamAttrs[Pos]; |
Alexey Bataev | 20cf67c | 2018-03-02 18:07:00 +0000 | [diff] [blame] | 10245 | ParamAttr.Kind = Linear; |
| 10246 | if (*SI) { |
Fangrui Song | 407659a | 2018-11-30 23:41:18 +0000 | [diff] [blame] | 10247 | Expr::EvalResult Result; |
| 10248 | if (!(*SI)->EvaluateAsInt(Result, C, Expr::SE_AllowSideEffects)) { |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 10249 | if (const auto *DRE = |
| 10250 | cast<DeclRefExpr>((*SI)->IgnoreParenImpCasts())) { |
| 10251 | if (const auto *StridePVD = cast<ParmVarDecl>(DRE->getDecl())) { |
Alexey Bataev | 20cf67c | 2018-03-02 18:07:00 +0000 | [diff] [blame] | 10252 | ParamAttr.Kind = LinearWithVarStride; |
| 10253 | ParamAttr.StrideOrArg = llvm::APSInt::getUnsigned( |
| 10254 | ParamPositions[StridePVD->getCanonicalDecl()]); |
| 10255 | } |
Alexey Bataev | c7a82b4 | 2016-05-06 09:40:08 +0000 | [diff] [blame] | 10256 | } |
Fangrui Song | 407659a | 2018-11-30 23:41:18 +0000 | [diff] [blame] | 10257 | } else { |
| 10258 | ParamAttr.StrideOrArg = Result.Val.getInt(); |
Alexey Bataev | c7a82b4 | 2016-05-06 09:40:08 +0000 | [diff] [blame] | 10259 | } |
| 10260 | } |
Alexey Bataev | 20cf67c | 2018-03-02 18:07:00 +0000 | [diff] [blame] | 10261 | ++SI; |
| 10262 | ++MI; |
Alexey Bataev | c7a82b4 | 2016-05-06 09:40:08 +0000 | [diff] [blame] | 10263 | } |
Alexey Bataev | 20cf67c | 2018-03-02 18:07:00 +0000 | [diff] [blame] | 10264 | llvm::APSInt VLENVal; |
Alexey Bataev | a0a2264 | 2019-04-16 13:56:21 +0000 | [diff] [blame] | 10265 | SourceLocation ExprLoc; |
| 10266 | const Expr *VLENExpr = Attr->getSimdlen(); |
| 10267 | if (VLENExpr) { |
| 10268 | VLENVal = VLENExpr->EvaluateKnownConstInt(C); |
| 10269 | ExprLoc = VLENExpr->getExprLoc(); |
| 10270 | } |
Alexey Bataev | 20cf67c | 2018-03-02 18:07:00 +0000 | [diff] [blame] | 10271 | OMPDeclareSimdDeclAttr::BranchStateTy State = Attr->getBranchState(); |
| 10272 | if (CGM.getTriple().getArch() == llvm::Triple::x86 || |
Alexey Bataev | a0a2264 | 2019-04-16 13:56:21 +0000 | [diff] [blame] | 10273 | CGM.getTriple().getArch() == llvm::Triple::x86_64) { |
Alexey Bataev | 20cf67c | 2018-03-02 18:07:00 +0000 | [diff] [blame] | 10274 | emitX86DeclareSimdFunction(FD, Fn, VLENVal, ParamAttrs, State); |
Alexey Bataev | a0a2264 | 2019-04-16 13:56:21 +0000 | [diff] [blame] | 10275 | } else if (CGM.getTriple().getArch() == llvm::Triple::aarch64) { |
| 10276 | unsigned VLEN = VLENVal.getExtValue(); |
| 10277 | StringRef MangledName = Fn->getName(); |
| 10278 | if (CGM.getTarget().hasFeature("sve")) |
| 10279 | emitAArch64DeclareSimdFunction(CGM, FD, VLEN, ParamAttrs, State, |
| 10280 | MangledName, 's', 128, Fn, ExprLoc); |
| 10281 | if (CGM.getTarget().hasFeature("neon")) |
| 10282 | emitAArch64DeclareSimdFunction(CGM, FD, VLEN, ParamAttrs, State, |
| 10283 | MangledName, 'n', 128, Fn, ExprLoc); |
| 10284 | } |
Alexey Bataev | c7a82b4 | 2016-05-06 09:40:08 +0000 | [diff] [blame] | 10285 | } |
Alexey Bataev | 20cf67c | 2018-03-02 18:07:00 +0000 | [diff] [blame] | 10286 | FD = FD->getPreviousDecl(); |
Alexey Bataev | c7a82b4 | 2016-05-06 09:40:08 +0000 | [diff] [blame] | 10287 | } |
| 10288 | } |
Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 10289 | |
| 10290 | namespace { |
| 10291 | /// Cleanup action for doacross support. |
| 10292 | class DoacrossCleanupTy final : public EHScopeStack::Cleanup { |
| 10293 | public: |
| 10294 | static const int DoacrossFinArgs = 2; |
| 10295 | |
| 10296 | private: |
James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 10297 | llvm::FunctionCallee RTLFn; |
Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 10298 | llvm::Value *Args[DoacrossFinArgs]; |
| 10299 | |
| 10300 | public: |
James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 10301 | DoacrossCleanupTy(llvm::FunctionCallee RTLFn, |
| 10302 | ArrayRef<llvm::Value *> CallArgs) |
Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 10303 | : RTLFn(RTLFn) { |
| 10304 | assert(CallArgs.size() == DoacrossFinArgs); |
| 10305 | std::copy(CallArgs.begin(), CallArgs.end(), std::begin(Args)); |
| 10306 | } |
| 10307 | void Emit(CodeGenFunction &CGF, Flags /*flags*/) override { |
| 10308 | if (!CGF.HaveInsertPoint()) |
| 10309 | return; |
| 10310 | CGF.EmitRuntimeCall(RTLFn, Args); |
| 10311 | } |
| 10312 | }; |
| 10313 | } // namespace |
| 10314 | |
| 10315 | void CGOpenMPRuntime::emitDoacrossInit(CodeGenFunction &CGF, |
Alexey Bataev | f138fda | 2018-08-13 19:04:24 +0000 | [diff] [blame] | 10316 | const OMPLoopDirective &D, |
| 10317 | ArrayRef<Expr *> NumIterations) { |
Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 10318 | if (!CGF.HaveInsertPoint()) |
| 10319 | return; |
| 10320 | |
| 10321 | ASTContext &C = CGM.getContext(); |
| 10322 | QualType Int64Ty = C.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/true); |
| 10323 | RecordDecl *RD; |
| 10324 | if (KmpDimTy.isNull()) { |
| 10325 | // Build struct kmp_dim { // loop bounds info casted to kmp_int64 |
| 10326 | // kmp_int64 lo; // lower |
| 10327 | // kmp_int64 up; // upper |
| 10328 | // kmp_int64 st; // stride |
| 10329 | // }; |
| 10330 | RD = C.buildImplicitRecord("kmp_dim"); |
| 10331 | RD->startDefinition(); |
| 10332 | addFieldToRecordDecl(C, RD, Int64Ty); |
| 10333 | addFieldToRecordDecl(C, RD, Int64Ty); |
| 10334 | addFieldToRecordDecl(C, RD, Int64Ty); |
| 10335 | RD->completeDefinition(); |
| 10336 | KmpDimTy = C.getRecordType(RD); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 10337 | } else { |
Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 10338 | RD = cast<RecordDecl>(KmpDimTy->getAsTagDecl()); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 10339 | } |
Alexey Bataev | f138fda | 2018-08-13 19:04:24 +0000 | [diff] [blame] | 10340 | llvm::APInt Size(/*numBits=*/32, NumIterations.size()); |
| 10341 | QualType ArrayTy = |
| 10342 | C.getConstantArrayType(KmpDimTy, Size, ArrayType::Normal, 0); |
Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 10343 | |
Alexey Bataev | f138fda | 2018-08-13 19:04:24 +0000 | [diff] [blame] | 10344 | Address DimsAddr = CGF.CreateMemTemp(ArrayTy, "dims"); |
| 10345 | CGF.EmitNullInitialization(DimsAddr, ArrayTy); |
Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 10346 | enum { LowerFD = 0, UpperFD, StrideFD }; |
| 10347 | // Fill dims with data. |
Alexey Bataev | f138fda | 2018-08-13 19:04:24 +0000 | [diff] [blame] | 10348 | for (unsigned I = 0, E = NumIterations.size(); I < E; ++I) { |
James Y Knight | 751fe28 | 2019-02-09 22:22:28 +0000 | [diff] [blame] | 10349 | LValue DimsLVal = CGF.MakeAddrLValue( |
| 10350 | CGF.Builder.CreateConstArrayGEP(DimsAddr, I), KmpDimTy); |
Alexey Bataev | f138fda | 2018-08-13 19:04:24 +0000 | [diff] [blame] | 10351 | // dims.upper = num_iterations; |
| 10352 | LValue UpperLVal = CGF.EmitLValueForField( |
| 10353 | DimsLVal, *std::next(RD->field_begin(), UpperFD)); |
| 10354 | llvm::Value *NumIterVal = |
| 10355 | CGF.EmitScalarConversion(CGF.EmitScalarExpr(NumIterations[I]), |
| 10356 | D.getNumIterations()->getType(), Int64Ty, |
| 10357 | D.getNumIterations()->getExprLoc()); |
| 10358 | CGF.EmitStoreOfScalar(NumIterVal, UpperLVal); |
| 10359 | // dims.stride = 1; |
| 10360 | LValue StrideLVal = CGF.EmitLValueForField( |
| 10361 | DimsLVal, *std::next(RD->field_begin(), StrideFD)); |
| 10362 | CGF.EmitStoreOfScalar(llvm::ConstantInt::getSigned(CGM.Int64Ty, /*V=*/1), |
| 10363 | StrideLVal); |
| 10364 | } |
Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 10365 | |
| 10366 | // Build call void __kmpc_doacross_init(ident_t *loc, kmp_int32 gtid, |
| 10367 | // kmp_int32 num_dims, struct kmp_dim * dims); |
Alexey Bataev | f138fda | 2018-08-13 19:04:24 +0000 | [diff] [blame] | 10368 | llvm::Value *Args[] = { |
| 10369 | emitUpdateLocation(CGF, D.getBeginLoc()), |
| 10370 | getThreadID(CGF, D.getBeginLoc()), |
| 10371 | llvm::ConstantInt::getSigned(CGM.Int32Ty, NumIterations.size()), |
| 10372 | CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
James Y Knight | 751fe28 | 2019-02-09 22:22:28 +0000 | [diff] [blame] | 10373 | CGF.Builder.CreateConstArrayGEP(DimsAddr, 0).getPointer(), |
Alexey Bataev | f138fda | 2018-08-13 19:04:24 +0000 | [diff] [blame] | 10374 | CGM.VoidPtrTy)}; |
Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 10375 | |
James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 10376 | llvm::FunctionCallee RTLFn = |
| 10377 | createRuntimeFunction(OMPRTL__kmpc_doacross_init); |
Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 10378 | CGF.EmitRuntimeCall(RTLFn, Args); |
| 10379 | llvm::Value *FiniArgs[DoacrossCleanupTy::DoacrossFinArgs] = { |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 10380 | emitUpdateLocation(CGF, D.getEndLoc()), getThreadID(CGF, D.getEndLoc())}; |
James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 10381 | llvm::FunctionCallee FiniRTLFn = |
| 10382 | createRuntimeFunction(OMPRTL__kmpc_doacross_fini); |
Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 10383 | CGF.EHStack.pushCleanup<DoacrossCleanupTy>(NormalAndEHCleanup, FiniRTLFn, |
| 10384 | llvm::makeArrayRef(FiniArgs)); |
| 10385 | } |
| 10386 | |
| 10387 | void CGOpenMPRuntime::emitDoacrossOrdered(CodeGenFunction &CGF, |
| 10388 | const OMPDependClause *C) { |
| 10389 | QualType Int64Ty = |
| 10390 | CGM.getContext().getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1); |
Alexey Bataev | f138fda | 2018-08-13 19:04:24 +0000 | [diff] [blame] | 10391 | llvm::APInt Size(/*numBits=*/32, C->getNumLoops()); |
| 10392 | QualType ArrayTy = CGM.getContext().getConstantArrayType( |
| 10393 | Int64Ty, Size, ArrayType::Normal, 0); |
| 10394 | Address CntAddr = CGF.CreateMemTemp(ArrayTy, ".cnt.addr"); |
| 10395 | for (unsigned I = 0, E = C->getNumLoops(); I < E; ++I) { |
| 10396 | const Expr *CounterVal = C->getLoopData(I); |
| 10397 | assert(CounterVal); |
| 10398 | llvm::Value *CntVal = CGF.EmitScalarConversion( |
| 10399 | CGF.EmitScalarExpr(CounterVal), CounterVal->getType(), Int64Ty, |
| 10400 | CounterVal->getExprLoc()); |
James Y Knight | 751fe28 | 2019-02-09 22:22:28 +0000 | [diff] [blame] | 10401 | CGF.EmitStoreOfScalar(CntVal, CGF.Builder.CreateConstArrayGEP(CntAddr, I), |
| 10402 | /*Volatile=*/false, Int64Ty); |
Alexey Bataev | f138fda | 2018-08-13 19:04:24 +0000 | [diff] [blame] | 10403 | } |
| 10404 | llvm::Value *Args[] = { |
| 10405 | emitUpdateLocation(CGF, C->getBeginLoc()), |
| 10406 | getThreadID(CGF, C->getBeginLoc()), |
James Y Knight | 751fe28 | 2019-02-09 22:22:28 +0000 | [diff] [blame] | 10407 | CGF.Builder.CreateConstArrayGEP(CntAddr, 0).getPointer()}; |
James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 10408 | llvm::FunctionCallee RTLFn; |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 10409 | if (C->getDependencyKind() == OMPC_DEPEND_source) { |
Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 10410 | RTLFn = createRuntimeFunction(OMPRTL__kmpc_doacross_post); |
Alexey Bataev | a4fa0b8 | 2018-04-16 17:59:34 +0000 | [diff] [blame] | 10411 | } else { |
Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 10412 | assert(C->getDependencyKind() == OMPC_DEPEND_sink); |
| 10413 | RTLFn = createRuntimeFunction(OMPRTL__kmpc_doacross_wait); |
| 10414 | } |
| 10415 | CGF.EmitRuntimeCall(RTLFn, Args); |
| 10416 | } |
| 10417 | |
Alexey Bataev | 7ef47a6 | 2018-02-22 18:33:31 +0000 | [diff] [blame] | 10418 | void CGOpenMPRuntime::emitCall(CodeGenFunction &CGF, SourceLocation Loc, |
James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 10419 | llvm::FunctionCallee Callee, |
Alexey Bataev | 7ef47a6 | 2018-02-22 18:33:31 +0000 | [diff] [blame] | 10420 | ArrayRef<llvm::Value *> Args) const { |
| 10421 | assert(Loc.isValid() && "Outlined function call location must be valid."); |
Alexey Bataev | 3c595a6 | 2017-08-14 15:01:03 +0000 | [diff] [blame] | 10422 | auto DL = ApplyDebugLocation::CreateDefaultArtificial(CGF, Loc); |
| 10423 | |
James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 10424 | if (auto *Fn = dyn_cast<llvm::Function>(Callee.getCallee())) { |
Alexey Bataev | 2c7eee5 | 2017-08-04 19:10:54 +0000 | [diff] [blame] | 10425 | if (Fn->doesNotThrow()) { |
Alexey Bataev | 3c595a6 | 2017-08-14 15:01:03 +0000 | [diff] [blame] | 10426 | CGF.EmitNounwindRuntimeCall(Fn, Args); |
Alexey Bataev | 2c7eee5 | 2017-08-04 19:10:54 +0000 | [diff] [blame] | 10427 | return; |
| 10428 | } |
| 10429 | } |
Alexey Bataev | 3c595a6 | 2017-08-14 15:01:03 +0000 | [diff] [blame] | 10430 | CGF.EmitRuntimeCall(Callee, Args); |
| 10431 | } |
| 10432 | |
| 10433 | void CGOpenMPRuntime::emitOutlinedFunctionCall( |
James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 10434 | CodeGenFunction &CGF, SourceLocation Loc, llvm::FunctionCallee OutlinedFn, |
Alexey Bataev | 3c595a6 | 2017-08-14 15:01:03 +0000 | [diff] [blame] | 10435 | ArrayRef<llvm::Value *> Args) const { |
Alexey Bataev | 7ef47a6 | 2018-02-22 18:33:31 +0000 | [diff] [blame] | 10436 | emitCall(CGF, Loc, OutlinedFn, Args); |
Alexey Bataev | 2c7eee5 | 2017-08-04 19:10:54 +0000 | [diff] [blame] | 10437 | } |
Alexey Bataev | 3b8d558 | 2017-08-08 18:04:06 +0000 | [diff] [blame] | 10438 | |
Gheorghe-Teodor Bercea | 66cdbb47 | 2019-05-21 19:42:01 +0000 | [diff] [blame] | 10439 | void CGOpenMPRuntime::emitFunctionProlog(CodeGenFunction &CGF, const Decl *D) { |
| 10440 | if (const auto *FD = dyn_cast<FunctionDecl>(D)) |
| 10441 | if (OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(FD)) |
| 10442 | HasEmittedDeclareTargetRegion = true; |
| 10443 | } |
| 10444 | |
Alexey Bataev | 3b8d558 | 2017-08-08 18:04:06 +0000 | [diff] [blame] | 10445 | Address CGOpenMPRuntime::getParameterAddress(CodeGenFunction &CGF, |
| 10446 | const VarDecl *NativeParam, |
| 10447 | const VarDecl *TargetParam) const { |
| 10448 | return CGF.GetAddrOfLocalVar(NativeParam); |
| 10449 | } |
Alexey Bataev | a8a9153a | 2017-12-29 18:07:07 +0000 | [diff] [blame] | 10450 | |
Alexey Bataev | 4f680db | 2019-03-19 16:41:16 +0000 | [diff] [blame] | 10451 | namespace { |
| 10452 | /// Cleanup action for allocate support. |
| 10453 | class OMPAllocateCleanupTy final : public EHScopeStack::Cleanup { |
| 10454 | public: |
| 10455 | static const int CleanupArgs = 3; |
| 10456 | |
| 10457 | private: |
| 10458 | llvm::FunctionCallee RTLFn; |
| 10459 | llvm::Value *Args[CleanupArgs]; |
| 10460 | |
| 10461 | public: |
| 10462 | OMPAllocateCleanupTy(llvm::FunctionCallee RTLFn, |
| 10463 | ArrayRef<llvm::Value *> CallArgs) |
| 10464 | : RTLFn(RTLFn) { |
| 10465 | assert(CallArgs.size() == CleanupArgs && |
| 10466 | "Size of arguments does not match."); |
| 10467 | std::copy(CallArgs.begin(), CallArgs.end(), std::begin(Args)); |
| 10468 | } |
| 10469 | void Emit(CodeGenFunction &CGF, Flags /*flags*/) override { |
| 10470 | if (!CGF.HaveInsertPoint()) |
| 10471 | return; |
| 10472 | CGF.EmitRuntimeCall(RTLFn, Args); |
| 10473 | } |
| 10474 | }; |
| 10475 | } // namespace |
| 10476 | |
Gheorghe-Teodor Bercea | d3dcf2f | 2018-03-14 14:17:45 +0000 | [diff] [blame] | 10477 | Address CGOpenMPRuntime::getAddressOfLocalVariable(CodeGenFunction &CGF, |
| 10478 | const VarDecl *VD) { |
Alexey Bataev | 084b0c2f0 | 2019-03-21 20:36:16 +0000 | [diff] [blame] | 10479 | if (!VD) |
| 10480 | return Address::invalid(); |
Alexey Bataev | 4f680db | 2019-03-19 16:41:16 +0000 | [diff] [blame] | 10481 | const VarDecl *CVD = VD->getCanonicalDecl(); |
| 10482 | if (!CVD->hasAttr<OMPAllocateDeclAttr>()) |
| 10483 | return Address::invalid(); |
Alexey Bataev | 084b0c2f0 | 2019-03-21 20:36:16 +0000 | [diff] [blame] | 10484 | const auto *AA = CVD->getAttr<OMPAllocateDeclAttr>(); |
| 10485 | // Use the default allocation. |
Alexey Bataev | 0fd3c68 | 2019-04-02 19:44:46 +0000 | [diff] [blame] | 10486 | if (AA->getAllocatorType() == OMPAllocateDeclAttr::OMPDefaultMemAlloc && |
| 10487 | !AA->getAllocator()) |
Alexey Bataev | 084b0c2f0 | 2019-03-21 20:36:16 +0000 | [diff] [blame] | 10488 | return Address::invalid(); |
Alexey Bataev | 084b0c2f0 | 2019-03-21 20:36:16 +0000 | [diff] [blame] | 10489 | llvm::Value *Size; |
| 10490 | CharUnits Align = CGM.getContext().getDeclAlign(CVD); |
| 10491 | if (CVD->getType()->isVariablyModifiedType()) { |
| 10492 | Size = CGF.getTypeSize(CVD->getType()); |
Alexey Bataev | 9c39781 | 2019-04-03 17:57:06 +0000 | [diff] [blame] | 10493 | // Align the size: ((size + align - 1) / align) * align |
| 10494 | Size = CGF.Builder.CreateNUWAdd( |
| 10495 | Size, CGM.getSize(Align - CharUnits::fromQuantity(1))); |
| 10496 | Size = CGF.Builder.CreateUDiv(Size, CGM.getSize(Align)); |
| 10497 | Size = CGF.Builder.CreateNUWMul(Size, CGM.getSize(Align)); |
Alexey Bataev | 084b0c2f0 | 2019-03-21 20:36:16 +0000 | [diff] [blame] | 10498 | } else { |
| 10499 | CharUnits Sz = CGM.getContext().getTypeSizeInChars(CVD->getType()); |
Alexey Bataev | 084b0c2f0 | 2019-03-21 20:36:16 +0000 | [diff] [blame] | 10500 | Size = CGM.getSize(Sz.alignTo(Align)); |
Alexey Bataev | 4f680db | 2019-03-19 16:41:16 +0000 | [diff] [blame] | 10501 | } |
Alexey Bataev | 084b0c2f0 | 2019-03-21 20:36:16 +0000 | [diff] [blame] | 10502 | llvm::Value *ThreadID = getThreadID(CGF, CVD->getBeginLoc()); |
| 10503 | assert(AA->getAllocator() && |
| 10504 | "Expected allocator expression for non-default allocator."); |
| 10505 | llvm::Value *Allocator = CGF.EmitScalarExpr(AA->getAllocator()); |
Alexey Bataev | 6cf7b71 | 2019-04-08 19:06:42 +0000 | [diff] [blame] | 10506 | // According to the standard, the original allocator type is a enum (integer). |
| 10507 | // Convert to pointer type, if required. |
| 10508 | if (Allocator->getType()->isIntegerTy()) |
| 10509 | Allocator = CGF.Builder.CreateIntToPtr(Allocator, CGM.VoidPtrTy); |
| 10510 | else if (Allocator->getType()->isPointerTy()) |
| 10511 | Allocator = CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(Allocator, |
| 10512 | CGM.VoidPtrTy); |
Alexey Bataev | 084b0c2f0 | 2019-03-21 20:36:16 +0000 | [diff] [blame] | 10513 | llvm::Value *Args[] = {ThreadID, Size, Allocator}; |
| 10514 | |
| 10515 | llvm::Value *Addr = |
| 10516 | CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_alloc), Args, |
| 10517 | CVD->getName() + ".void.addr"); |
| 10518 | llvm::Value *FiniArgs[OMPAllocateCleanupTy::CleanupArgs] = {ThreadID, Addr, |
| 10519 | Allocator}; |
| 10520 | llvm::FunctionCallee FiniRTLFn = createRuntimeFunction(OMPRTL__kmpc_free); |
| 10521 | |
| 10522 | CGF.EHStack.pushCleanup<OMPAllocateCleanupTy>(NormalAndEHCleanup, FiniRTLFn, |
| 10523 | llvm::makeArrayRef(FiniArgs)); |
| 10524 | Addr = CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
| 10525 | Addr, |
| 10526 | CGF.ConvertTypeForMem(CGM.getContext().getPointerType(CVD->getType())), |
| 10527 | CVD->getName() + ".addr"); |
| 10528 | return Address(Addr, Align); |
Gheorghe-Teodor Bercea | d3dcf2f | 2018-03-14 14:17:45 +0000 | [diff] [blame] | 10529 | } |
| 10530 | |
James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 10531 | llvm::Function *CGOpenMPSIMDRuntime::emitParallelOutlinedFunction( |
Alexey Bataev | a8a9153a | 2017-12-29 18:07:07 +0000 | [diff] [blame] | 10532 | const OMPExecutableDirective &D, const VarDecl *ThreadIDVar, |
| 10533 | OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen) { |
| 10534 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 10535 | } |
| 10536 | |
James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 10537 | llvm::Function *CGOpenMPSIMDRuntime::emitTeamsOutlinedFunction( |
Alexey Bataev | a8a9153a | 2017-12-29 18:07:07 +0000 | [diff] [blame] | 10538 | const OMPExecutableDirective &D, const VarDecl *ThreadIDVar, |
| 10539 | OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen) { |
| 10540 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 10541 | } |
| 10542 | |
James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 10543 | llvm::Function *CGOpenMPSIMDRuntime::emitTaskOutlinedFunction( |
Alexey Bataev | a8a9153a | 2017-12-29 18:07:07 +0000 | [diff] [blame] | 10544 | const OMPExecutableDirective &D, const VarDecl *ThreadIDVar, |
| 10545 | const VarDecl *PartIDVar, const VarDecl *TaskTVar, |
| 10546 | OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen, |
| 10547 | bool Tied, unsigned &NumberOfParts) { |
| 10548 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 10549 | } |
| 10550 | |
| 10551 | void CGOpenMPSIMDRuntime::emitParallelCall(CodeGenFunction &CGF, |
| 10552 | SourceLocation Loc, |
James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 10553 | llvm::Function *OutlinedFn, |
Alexey Bataev | a8a9153a | 2017-12-29 18:07:07 +0000 | [diff] [blame] | 10554 | ArrayRef<llvm::Value *> CapturedVars, |
| 10555 | const Expr *IfCond) { |
| 10556 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 10557 | } |
| 10558 | |
| 10559 | void CGOpenMPSIMDRuntime::emitCriticalRegion( |
| 10560 | CodeGenFunction &CGF, StringRef CriticalName, |
| 10561 | const RegionCodeGenTy &CriticalOpGen, SourceLocation Loc, |
| 10562 | const Expr *Hint) { |
| 10563 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 10564 | } |
| 10565 | |
| 10566 | void CGOpenMPSIMDRuntime::emitMasterRegion(CodeGenFunction &CGF, |
| 10567 | const RegionCodeGenTy &MasterOpGen, |
| 10568 | SourceLocation Loc) { |
| 10569 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 10570 | } |
| 10571 | |
| 10572 | void CGOpenMPSIMDRuntime::emitTaskyieldCall(CodeGenFunction &CGF, |
| 10573 | SourceLocation Loc) { |
| 10574 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 10575 | } |
| 10576 | |
| 10577 | void CGOpenMPSIMDRuntime::emitTaskgroupRegion( |
| 10578 | CodeGenFunction &CGF, const RegionCodeGenTy &TaskgroupOpGen, |
| 10579 | SourceLocation Loc) { |
| 10580 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 10581 | } |
| 10582 | |
| 10583 | void CGOpenMPSIMDRuntime::emitSingleRegion( |
| 10584 | CodeGenFunction &CGF, const RegionCodeGenTy &SingleOpGen, |
| 10585 | SourceLocation Loc, ArrayRef<const Expr *> CopyprivateVars, |
| 10586 | ArrayRef<const Expr *> DestExprs, ArrayRef<const Expr *> SrcExprs, |
| 10587 | ArrayRef<const Expr *> AssignmentOps) { |
| 10588 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 10589 | } |
| 10590 | |
| 10591 | void CGOpenMPSIMDRuntime::emitOrderedRegion(CodeGenFunction &CGF, |
| 10592 | const RegionCodeGenTy &OrderedOpGen, |
| 10593 | SourceLocation Loc, |
| 10594 | bool IsThreads) { |
| 10595 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 10596 | } |
| 10597 | |
| 10598 | void CGOpenMPSIMDRuntime::emitBarrierCall(CodeGenFunction &CGF, |
| 10599 | SourceLocation Loc, |
| 10600 | OpenMPDirectiveKind Kind, |
| 10601 | bool EmitChecks, |
| 10602 | bool ForceSimpleCall) { |
| 10603 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 10604 | } |
| 10605 | |
| 10606 | void CGOpenMPSIMDRuntime::emitForDispatchInit( |
| 10607 | CodeGenFunction &CGF, SourceLocation Loc, |
| 10608 | const OpenMPScheduleTy &ScheduleKind, unsigned IVSize, bool IVSigned, |
| 10609 | bool Ordered, const DispatchRTInput &DispatchValues) { |
| 10610 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 10611 | } |
| 10612 | |
| 10613 | void CGOpenMPSIMDRuntime::emitForStaticInit( |
| 10614 | CodeGenFunction &CGF, SourceLocation Loc, OpenMPDirectiveKind DKind, |
| 10615 | const OpenMPScheduleTy &ScheduleKind, const StaticRTInput &Values) { |
| 10616 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 10617 | } |
| 10618 | |
| 10619 | void CGOpenMPSIMDRuntime::emitDistributeStaticInit( |
| 10620 | CodeGenFunction &CGF, SourceLocation Loc, |
| 10621 | OpenMPDistScheduleClauseKind SchedKind, const StaticRTInput &Values) { |
| 10622 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 10623 | } |
| 10624 | |
| 10625 | void CGOpenMPSIMDRuntime::emitForOrderedIterationEnd(CodeGenFunction &CGF, |
| 10626 | SourceLocation Loc, |
| 10627 | unsigned IVSize, |
| 10628 | bool IVSigned) { |
| 10629 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 10630 | } |
| 10631 | |
| 10632 | void CGOpenMPSIMDRuntime::emitForStaticFinish(CodeGenFunction &CGF, |
| 10633 | SourceLocation Loc, |
| 10634 | OpenMPDirectiveKind DKind) { |
| 10635 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 10636 | } |
| 10637 | |
| 10638 | llvm::Value *CGOpenMPSIMDRuntime::emitForNext(CodeGenFunction &CGF, |
| 10639 | SourceLocation Loc, |
| 10640 | unsigned IVSize, bool IVSigned, |
| 10641 | Address IL, Address LB, |
| 10642 | Address UB, Address ST) { |
| 10643 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 10644 | } |
| 10645 | |
| 10646 | void CGOpenMPSIMDRuntime::emitNumThreadsClause(CodeGenFunction &CGF, |
| 10647 | llvm::Value *NumThreads, |
| 10648 | SourceLocation Loc) { |
| 10649 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 10650 | } |
| 10651 | |
| 10652 | void CGOpenMPSIMDRuntime::emitProcBindClause(CodeGenFunction &CGF, |
| 10653 | OpenMPProcBindClauseKind ProcBind, |
| 10654 | SourceLocation Loc) { |
| 10655 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 10656 | } |
| 10657 | |
| 10658 | Address CGOpenMPSIMDRuntime::getAddrOfThreadPrivate(CodeGenFunction &CGF, |
| 10659 | const VarDecl *VD, |
| 10660 | Address VDAddr, |
| 10661 | SourceLocation Loc) { |
| 10662 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 10663 | } |
| 10664 | |
| 10665 | llvm::Function *CGOpenMPSIMDRuntime::emitThreadPrivateVarDefinition( |
| 10666 | const VarDecl *VD, Address VDAddr, SourceLocation Loc, bool PerformInit, |
| 10667 | CodeGenFunction *CGF) { |
| 10668 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 10669 | } |
| 10670 | |
| 10671 | Address CGOpenMPSIMDRuntime::getAddrOfArtificialThreadPrivate( |
| 10672 | CodeGenFunction &CGF, QualType VarType, StringRef Name) { |
| 10673 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 10674 | } |
| 10675 | |
| 10676 | void CGOpenMPSIMDRuntime::emitFlush(CodeGenFunction &CGF, |
| 10677 | ArrayRef<const Expr *> Vars, |
| 10678 | SourceLocation Loc) { |
| 10679 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 10680 | } |
| 10681 | |
| 10682 | void CGOpenMPSIMDRuntime::emitTaskCall(CodeGenFunction &CGF, SourceLocation Loc, |
| 10683 | const OMPExecutableDirective &D, |
James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 10684 | llvm::Function *TaskFunction, |
Alexey Bataev | a8a9153a | 2017-12-29 18:07:07 +0000 | [diff] [blame] | 10685 | QualType SharedsTy, Address Shareds, |
| 10686 | const Expr *IfCond, |
| 10687 | const OMPTaskDataTy &Data) { |
| 10688 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 10689 | } |
| 10690 | |
| 10691 | void CGOpenMPSIMDRuntime::emitTaskLoopCall( |
| 10692 | CodeGenFunction &CGF, SourceLocation Loc, const OMPLoopDirective &D, |
James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 10693 | llvm::Function *TaskFunction, QualType SharedsTy, Address Shareds, |
Alexey Bataev | a8a9153a | 2017-12-29 18:07:07 +0000 | [diff] [blame] | 10694 | const Expr *IfCond, const OMPTaskDataTy &Data) { |
| 10695 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 10696 | } |
| 10697 | |
| 10698 | void CGOpenMPSIMDRuntime::emitReduction( |
| 10699 | CodeGenFunction &CGF, SourceLocation Loc, ArrayRef<const Expr *> Privates, |
| 10700 | ArrayRef<const Expr *> LHSExprs, ArrayRef<const Expr *> RHSExprs, |
| 10701 | ArrayRef<const Expr *> ReductionOps, ReductionOptionsTy Options) { |
| 10702 | assert(Options.SimpleReduction && "Only simple reduction is expected."); |
| 10703 | CGOpenMPRuntime::emitReduction(CGF, Loc, Privates, LHSExprs, RHSExprs, |
| 10704 | ReductionOps, Options); |
| 10705 | } |
| 10706 | |
| 10707 | llvm::Value *CGOpenMPSIMDRuntime::emitTaskReductionInit( |
| 10708 | CodeGenFunction &CGF, SourceLocation Loc, ArrayRef<const Expr *> LHSExprs, |
| 10709 | ArrayRef<const Expr *> RHSExprs, const OMPTaskDataTy &Data) { |
| 10710 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 10711 | } |
| 10712 | |
| 10713 | void CGOpenMPSIMDRuntime::emitTaskReductionFixups(CodeGenFunction &CGF, |
| 10714 | SourceLocation Loc, |
| 10715 | ReductionCodeGen &RCG, |
| 10716 | unsigned N) { |
| 10717 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 10718 | } |
| 10719 | |
| 10720 | Address CGOpenMPSIMDRuntime::getTaskReductionItem(CodeGenFunction &CGF, |
| 10721 | SourceLocation Loc, |
| 10722 | llvm::Value *ReductionsPtr, |
| 10723 | LValue SharedLVal) { |
| 10724 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 10725 | } |
| 10726 | |
| 10727 | void CGOpenMPSIMDRuntime::emitTaskwaitCall(CodeGenFunction &CGF, |
| 10728 | SourceLocation Loc) { |
| 10729 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 10730 | } |
| 10731 | |
| 10732 | void CGOpenMPSIMDRuntime::emitCancellationPointCall( |
| 10733 | CodeGenFunction &CGF, SourceLocation Loc, |
| 10734 | OpenMPDirectiveKind CancelRegion) { |
| 10735 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 10736 | } |
| 10737 | |
| 10738 | void CGOpenMPSIMDRuntime::emitCancelCall(CodeGenFunction &CGF, |
| 10739 | SourceLocation Loc, const Expr *IfCond, |
| 10740 | OpenMPDirectiveKind CancelRegion) { |
| 10741 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 10742 | } |
| 10743 | |
| 10744 | void CGOpenMPSIMDRuntime::emitTargetOutlinedFunction( |
| 10745 | const OMPExecutableDirective &D, StringRef ParentName, |
| 10746 | llvm::Function *&OutlinedFn, llvm::Constant *&OutlinedFnID, |
| 10747 | bool IsOffloadEntry, const RegionCodeGenTy &CodeGen) { |
| 10748 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 10749 | } |
| 10750 | |
| 10751 | void CGOpenMPSIMDRuntime::emitTargetCall(CodeGenFunction &CGF, |
| 10752 | const OMPExecutableDirective &D, |
James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 10753 | llvm::Function *OutlinedFn, |
Alexey Bataev | a8a9153a | 2017-12-29 18:07:07 +0000 | [diff] [blame] | 10754 | llvm::Value *OutlinedFnID, |
James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 10755 | const Expr *IfCond, |
| 10756 | const Expr *Device) { |
Alexey Bataev | a8a9153a | 2017-12-29 18:07:07 +0000 | [diff] [blame] | 10757 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 10758 | } |
| 10759 | |
| 10760 | bool CGOpenMPSIMDRuntime::emitTargetFunctions(GlobalDecl GD) { |
| 10761 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 10762 | } |
| 10763 | |
| 10764 | bool CGOpenMPSIMDRuntime::emitTargetGlobalVariable(GlobalDecl GD) { |
| 10765 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 10766 | } |
| 10767 | |
| 10768 | bool CGOpenMPSIMDRuntime::emitTargetGlobal(GlobalDecl GD) { |
| 10769 | return false; |
| 10770 | } |
| 10771 | |
| 10772 | llvm::Function *CGOpenMPSIMDRuntime::emitRegistrationFunction() { |
| 10773 | return nullptr; |
| 10774 | } |
| 10775 | |
| 10776 | void CGOpenMPSIMDRuntime::emitTeamsCall(CodeGenFunction &CGF, |
| 10777 | const OMPExecutableDirective &D, |
| 10778 | SourceLocation Loc, |
James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 10779 | llvm::Function *OutlinedFn, |
Alexey Bataev | a8a9153a | 2017-12-29 18:07:07 +0000 | [diff] [blame] | 10780 | ArrayRef<llvm::Value *> CapturedVars) { |
| 10781 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 10782 | } |
| 10783 | |
| 10784 | void CGOpenMPSIMDRuntime::emitNumTeamsClause(CodeGenFunction &CGF, |
| 10785 | const Expr *NumTeams, |
| 10786 | const Expr *ThreadLimit, |
| 10787 | SourceLocation Loc) { |
| 10788 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 10789 | } |
| 10790 | |
| 10791 | void CGOpenMPSIMDRuntime::emitTargetDataCalls( |
| 10792 | CodeGenFunction &CGF, const OMPExecutableDirective &D, const Expr *IfCond, |
| 10793 | const Expr *Device, const RegionCodeGenTy &CodeGen, TargetDataInfo &Info) { |
| 10794 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 10795 | } |
| 10796 | |
| 10797 | void CGOpenMPSIMDRuntime::emitTargetDataStandAloneCall( |
| 10798 | CodeGenFunction &CGF, const OMPExecutableDirective &D, const Expr *IfCond, |
| 10799 | const Expr *Device) { |
| 10800 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 10801 | } |
| 10802 | |
| 10803 | void CGOpenMPSIMDRuntime::emitDoacrossInit(CodeGenFunction &CGF, |
Alexey Bataev | f138fda | 2018-08-13 19:04:24 +0000 | [diff] [blame] | 10804 | const OMPLoopDirective &D, |
| 10805 | ArrayRef<Expr *> NumIterations) { |
Alexey Bataev | a8a9153a | 2017-12-29 18:07:07 +0000 | [diff] [blame] | 10806 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 10807 | } |
| 10808 | |
| 10809 | void CGOpenMPSIMDRuntime::emitDoacrossOrdered(CodeGenFunction &CGF, |
| 10810 | const OMPDependClause *C) { |
| 10811 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 10812 | } |
| 10813 | |
| 10814 | const VarDecl * |
| 10815 | CGOpenMPSIMDRuntime::translateParameter(const FieldDecl *FD, |
| 10816 | const VarDecl *NativeParam) const { |
| 10817 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 10818 | } |
| 10819 | |
| 10820 | Address |
| 10821 | CGOpenMPSIMDRuntime::getParameterAddress(CodeGenFunction &CGF, |
| 10822 | const VarDecl *NativeParam, |
| 10823 | const VarDecl *TargetParam) const { |
| 10824 | llvm_unreachable("Not supported in SIMD-only mode"); |
| 10825 | } |